in_pcb.c revision 1.58.4.2 1 /* $NetBSD: in_pcb.c,v 1.58.4.2 1999/08/02 22:34:57 thorpej Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Public Access Networks Corporation ("Panix"). It was developed under
38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the NetBSD
51 * Foundation, Inc. and its contributors.
52 * 4. Neither the name of The NetBSD Foundation nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 /*
70 * Copyright (c) 1982, 1986, 1991, 1993, 1995
71 * The Regents of the University of California. All rights reserved.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. All advertising materials mentioning features or use of this software
82 * must display the following acknowledgement:
83 * This product includes software developed by the University of
84 * California, Berkeley and its contributors.
85 * 4. Neither the name of the University nor the names of its contributors
86 * may be used to endorse or promote products derived from this software
87 * without specific prior written permission.
88 *
89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
92 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99 * SUCH DAMAGE.
100 *
101 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
102 */
103
104 #include "opt_ipsec.h"
105
106 #include <sys/param.h>
107 #include <sys/systm.h>
108 #include <sys/malloc.h>
109 #include <sys/mbuf.h>
110 #include <sys/protosw.h>
111 #include <sys/socket.h>
112 #include <sys/socketvar.h>
113 #include <sys/ioctl.h>
114 #include <sys/errno.h>
115 #include <sys/time.h>
116 #include <sys/pool.h>
117 #include <sys/proc.h>
118
119 #include <net/if.h>
120 #include <net/route.h>
121
122 #include <netinet/in.h>
123 #include <netinet/in_systm.h>
124 #include <netinet/ip.h>
125 #include <netinet/in_pcb.h>
126 #include <netinet/in_var.h>
127 #include <netinet/ip_var.h>
128
129 #ifdef IPSEC
130 #include <netinet6/ipsec.h>
131 #include <netkey/key.h>
132 #include <netkey/key_debug.h>
133 #endif /* IPSEC */
134
135 struct in_addr zeroin_addr;
136
137 #define INPCBHASH_BIND(table, laddr, lport) \
138 &(table)->inpt_bindhashtbl[ \
139 ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash]
140 #define INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
141 &(table)->inpt_connecthashtbl[ \
142 ((ntohl((faddr).s_addr) + ntohs(fport)) + \
143 (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash]
144
145 struct inpcb *
146 in_pcblookup_port __P((struct inpcbtable *,
147 struct in_addr, u_int, int));
148
149 int anonportmin = IPPORT_ANONMIN;
150 int anonportmax = IPPORT_ANONMAX;
151
152 struct pool inpcb_pool;
153
154 void
155 in_pcbinit(table, bindhashsize, connecthashsize)
156 struct inpcbtable *table;
157 int bindhashsize, connecthashsize;
158 {
159 static int inpcb_pool_initialized;
160
161 if (inpcb_pool_initialized == 0) {
162 pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 0, 0,
163 "inpcbpl", 0, NULL, NULL, M_PCB);
164 inpcb_pool_initialized = 1;
165 }
166
167 CIRCLEQ_INIT(&table->inpt_queue);
168 table->inpt_bindhashtbl =
169 hashinit(bindhashsize, M_PCB, M_WAITOK, &table->inpt_bindhash);
170 table->inpt_connecthashtbl =
171 hashinit(connecthashsize, M_PCB, M_WAITOK, &table->inpt_connecthash);
172 table->inpt_lastlow = IPPORT_RESERVEDMAX;
173 table->inpt_lastport = (u_int16_t)anonportmax;
174 }
175
176 int
177 in_pcballoc(so, v)
178 struct socket *so;
179 void *v;
180 {
181 struct inpcbtable *table = v;
182 register struct inpcb *inp;
183 int s;
184
185 inp = pool_get(&inpcb_pool, PR_NOWAIT);
186 if (inp == NULL)
187 return (ENOBUFS);
188 bzero((caddr_t)inp, sizeof(*inp));
189 inp->inp_table = table;
190 inp->inp_socket = so;
191 inp->inp_errormtu = -1;
192 so->so_pcb = inp;
193 s = splnet();
194 CIRCLEQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue);
195 in_pcbstate(inp, INP_ATTACHED);
196 splx(s);
197 return (0);
198 }
199
200 int
201 in_pcbbind(v, nam, p)
202 void *v;
203 struct mbuf *nam;
204 struct proc *p;
205 {
206 register struct inpcb *inp = v;
207 register struct socket *so = inp->inp_socket;
208 register struct inpcbtable *table = inp->inp_table;
209 register struct sockaddr_in *sin;
210 u_int16_t lport = 0;
211 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
212 #ifndef IPNOPRIVPORTS
213 int error;
214 #endif
215
216 if (in_ifaddr.tqh_first == 0)
217 return (EADDRNOTAVAIL);
218 if (inp->inp_lport || !in_nullhost(inp->inp_laddr))
219 return (EINVAL);
220 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
221 wild = 1;
222 if (nam == 0)
223 goto noname;
224 sin = mtod(nam, struct sockaddr_in *);
225 if (nam->m_len != sizeof (*sin))
226 return (EINVAL);
227 #ifdef notdef
228 /*
229 * We should check the family, but old programs
230 * incorrectly fail to initialize it.
231 */
232 if (sin->sin_family != AF_INET)
233 return (EAFNOSUPPORT);
234 #endif
235 lport = sin->sin_port;
236 if (IN_MULTICAST(sin->sin_addr.s_addr)) {
237 /*
238 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
239 * allow complete duplication of binding if
240 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
241 * and a multicast address is bound on both
242 * new and duplicated sockets.
243 */
244 if (so->so_options & SO_REUSEADDR)
245 reuseport = SO_REUSEADDR|SO_REUSEPORT;
246 } else if (!in_nullhost(sin->sin_addr)) {
247 sin->sin_port = 0; /* yech... */
248 if (ifa_ifwithaddr(sintosa(sin)) == 0)
249 return (EADDRNOTAVAIL);
250 }
251 if (lport) {
252 struct inpcb *t;
253 #ifndef IPNOPRIVPORTS
254 /* GROSS */
255 if (ntohs(lport) < IPPORT_RESERVED &&
256 (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))))
257 return (EACCES);
258 #endif
259 if (so->so_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) {
260 t = in_pcblookup_port(table, sin->sin_addr, lport, 1);
261 /*
262 * XXX: investigate ramifications of loosening this
263 * restriction so that as long as both ports have
264 * SO_REUSEPORT allow the bind
265 */
266 if (t &&
267 (!in_nullhost(sin->sin_addr) ||
268 !in_nullhost(t->inp_laddr) ||
269 (t->inp_socket->so_options & SO_REUSEPORT) == 0)
270 && (so->so_uid != t->inp_socket->so_uid)) {
271 return (EADDRINUSE);
272 }
273 }
274 t = in_pcblookup_port(table, sin->sin_addr, lport, wild);
275 if (t && (reuseport & t->inp_socket->so_options) == 0)
276 return (EADDRINUSE);
277 }
278 inp->inp_laddr = sin->sin_addr;
279
280 noname:
281 if (lport == 0) {
282 int cnt;
283 u_int16_t min, max;
284 u_int16_t *lastport;
285
286 if (inp->inp_flags & INP_LOWPORT) {
287 #ifndef IPNOPRIVPORTS
288 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
289 return (EACCES);
290 #endif
291 min = IPPORT_RESERVEDMIN;
292 max = IPPORT_RESERVEDMAX;
293 lastport = &table->inpt_lastlow;
294 } else {
295 min = anonportmin;
296 max = anonportmax;
297 lastport = &table->inpt_lastport;
298 }
299 if (min > max) { /* sanity check */
300 u_int16_t swp;
301
302 swp = min;
303 min = max;
304 max = swp;
305 }
306
307 lport = *lastport - 1;
308 for (cnt = max - min + 1; cnt; cnt--, lport--) {
309 if (lport < min || lport > max)
310 lport = max;
311 if (!in_pcblookup_port(table, inp->inp_laddr,
312 htons(lport), 1))
313 goto found;
314 }
315 if (!in_nullhost(inp->inp_laddr))
316 inp->inp_laddr.s_addr = INADDR_ANY;
317 return (EAGAIN);
318 found:
319 inp->inp_flags |= INP_ANONPORT;
320 *lastport = lport;
321 lport = htons(lport);
322 }
323 inp->inp_lport = lport;
324 in_pcbstate(inp, INP_BOUND);
325 return (0);
326 }
327
328 /*
329 * Connect from a socket to a specified address.
330 * Both address and port must be specified in argument sin.
331 * If don't have a local address for this socket yet,
332 * then pick one.
333 */
334 int
335 in_pcbconnect(v, nam)
336 register void *v;
337 struct mbuf *nam;
338 {
339 register struct inpcb *inp = v;
340 struct in_ifaddr *ia;
341 struct sockaddr_in *ifaddr = NULL;
342 register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
343 int error;
344
345 if (nam->m_len != sizeof (*sin))
346 return (EINVAL);
347 if (sin->sin_family != AF_INET)
348 return (EAFNOSUPPORT);
349 if (sin->sin_port == 0)
350 return (EADDRNOTAVAIL);
351 if (in_ifaddr.tqh_first != 0) {
352 /*
353 * If the destination address is INADDR_ANY,
354 * use any local address (likely loopback).
355 * If the supplied address is INADDR_BROADCAST,
356 * use the broadcast address of an interface
357 * which supports broadcast. (loopback does not)
358 */
359
360 if (in_nullhost(sin->sin_addr))
361 sin->sin_addr = in_ifaddr.tqh_first->ia_addr.sin_addr;
362 else if (sin->sin_addr.s_addr == INADDR_BROADCAST)
363 for (ia = in_ifaddr.tqh_first; ia != NULL;
364 ia = ia->ia_list.tqe_next)
365 if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
366 sin->sin_addr = ia->ia_broadaddr.sin_addr;
367 break;
368 }
369 }
370 /*
371 * If we haven't bound which network number to use as ours,
372 * we will use the number of the outgoing interface.
373 * This depends on having done a routing lookup, which
374 * we will probably have to do anyway, so we might
375 * as well do it now. On the other hand if we are
376 * sending to multiple destinations we may have already
377 * done the lookup, so see if we can use the route
378 * from before. In any case, we only
379 * chose a port number once, even if sending to multiple
380 * destinations.
381 */
382 if (in_nullhost(inp->inp_laddr)) {
383 #if 0
384 register struct route *ro;
385
386 ia = (struct in_ifaddr *)0;
387 /*
388 * If route is known or can be allocated now,
389 * our src addr is taken from the i/f, else punt.
390 */
391 ro = &inp->inp_route;
392 if (ro->ro_rt &&
393 (!in_hosteq(satosin(&ro->ro_dst)->sin_addr,
394 sin->sin_addr) ||
395 inp->inp_socket->so_options & SO_DONTROUTE)) {
396 RTFREE(ro->ro_rt);
397 ro->ro_rt = (struct rtentry *)0;
398 }
399 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
400 (ro->ro_rt == (struct rtentry *)0 ||
401 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
402 /* No route yet, so try to acquire one */
403 ro->ro_dst.sa_family = AF_INET;
404 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
405 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
406 rtalloc(ro);
407 }
408 /*
409 * If we found a route, use the address
410 * corresponding to the outgoing interface
411 * unless it is the loopback (in case a route
412 * to our address on another net goes to loopback).
413 *
414 * XXX Is this still true? Do we care?
415 */
416 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
417 ia = ifatoia(ro->ro_rt->rt_ifa);
418 if (ia == 0) {
419 u_int16_t fport = sin->sin_port;
420
421 sin->sin_port = 0;
422 ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
423 sin->sin_port = fport;
424 if (ia == 0)
425 /* Find 1st non-loopback AF_INET address */
426 for (ia = in_ifaddr.tqh_first ; ia != NULL ;
427 ia = ia->ia_list.tqe_next)
428 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
429 break;
430 if (ia == 0)
431 return (EADDRNOTAVAIL);
432 }
433 /*
434 * If the destination address is multicast and an outgoing
435 * interface has been set as a multicast option, use the
436 * address of that interface as our source address.
437 */
438 if (IN_MULTICAST(sin->sin_addr.s_addr) &&
439 inp->inp_moptions != NULL) {
440 struct ip_moptions *imo;
441 struct ifnet *ifp;
442
443 imo = inp->inp_moptions;
444 if (imo->imo_multicast_ifp != NULL) {
445 ifp = imo->imo_multicast_ifp;
446 IFP_TO_IA(ifp, ia); /* XXX */
447 if (ia == 0)
448 return (EADDRNOTAVAIL);
449 }
450 }
451 ifaddr = satosin(&ia->ia_addr);
452 #else
453 int error;
454 ifaddr = in_selectsrc(sin, &inp->inp_route,
455 inp->inp_socket->so_options, inp->inp_moptions, &error);
456 if (ifaddr == NULL) {
457 if (error == 0)
458 error = EADDRNOTAVAIL;
459 return error;
460 }
461 #endif
462 }
463 if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
464 !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr,
465 inp->inp_lport) != 0)
466 return (EADDRINUSE);
467 if (in_nullhost(inp->inp_laddr)) {
468 if (inp->inp_lport == 0) {
469 error = in_pcbbind(inp, (struct mbuf *)0,
470 (struct proc *)0);
471 /*
472 * This used to ignore the return value
473 * completely, but we need to check for
474 * ephemeral port shortage.
475 * XXX Should we check for other errors, too?
476 */
477 if (error == EAGAIN)
478 return (error);
479 }
480 inp->inp_laddr = ifaddr->sin_addr;
481 }
482 inp->inp_faddr = sin->sin_addr;
483 inp->inp_fport = sin->sin_port;
484 in_pcbstate(inp, INP_CONNECTED);
485 return (0);
486 }
487
488 void
489 in_pcbdisconnect(v)
490 void *v;
491 {
492 struct inpcb *inp = v;
493
494 inp->inp_faddr = zeroin_addr;
495 inp->inp_fport = 0;
496 in_pcbstate(inp, INP_BOUND);
497 if (inp->inp_socket->so_state & SS_NOFDREF)
498 in_pcbdetach(inp);
499 }
500
501 void
502 in_pcbdetach(v)
503 void *v;
504 {
505 struct inpcb *inp = v;
506 struct socket *so = inp->inp_socket;
507 int s;
508
509 #ifdef IPSEC
510 if (so->so_pcb) {
511 KEYDEBUG(KEYDEBUG_KEY_STAMP,
512 printf("DP call free SO=%p from in_pcbdetach\n", so));
513 key_freeso(so);
514 }
515 ipsec4_delete_pcbpolicy(inp);
516 #endif /*IPSEC*/
517 so->so_pcb = 0;
518 sofree(so);
519 if (inp->inp_options)
520 (void)m_free(inp->inp_options);
521 if (inp->inp_route.ro_rt)
522 rtfree(inp->inp_route.ro_rt);
523 ip_freemoptions(inp->inp_moptions);
524 s = splnet();
525 in_pcbstate(inp, INP_ATTACHED);
526 CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue);
527 splx(s);
528 pool_put(&inpcb_pool, inp);
529 }
530
531 void
532 in_setsockaddr(inp, nam)
533 register struct inpcb *inp;
534 struct mbuf *nam;
535 {
536 register struct sockaddr_in *sin;
537
538 nam->m_len = sizeof (*sin);
539 sin = mtod(nam, struct sockaddr_in *);
540 bzero((caddr_t)sin, sizeof (*sin));
541 sin->sin_family = AF_INET;
542 sin->sin_len = sizeof(*sin);
543 sin->sin_port = inp->inp_lport;
544 sin->sin_addr = inp->inp_laddr;
545 }
546
547 void
548 in_setpeeraddr(inp, nam)
549 struct inpcb *inp;
550 struct mbuf *nam;
551 {
552 register struct sockaddr_in *sin;
553
554 nam->m_len = sizeof (*sin);
555 sin = mtod(nam, struct sockaddr_in *);
556 bzero((caddr_t)sin, sizeof (*sin));
557 sin->sin_family = AF_INET;
558 sin->sin_len = sizeof(*sin);
559 sin->sin_port = inp->inp_fport;
560 sin->sin_addr = inp->inp_faddr;
561 }
562
563 /*
564 * Pass some notification to all connections of a protocol
565 * associated with address dst. The local address and/or port numbers
566 * may be specified to limit the search. The "usual action" will be
567 * taken, depending on the ctlinput cmd. The caller must filter any
568 * cmds that are uninteresting (e.g., no error in the map).
569 * Call the protocol specific routine (if any) to report
570 * any errors for each matching socket.
571 *
572 * Must be called at splsoftnet.
573 */
574 int
575 in_pcbnotify(table, faddr, fport_arg, laddr, lport_arg, errno, notify)
576 struct inpcbtable *table;
577 struct in_addr faddr, laddr;
578 u_int fport_arg, lport_arg;
579 int errno;
580 void (*notify) __P((struct inpcb *, int));
581 {
582 struct inpcbhead *head;
583 register struct inpcb *inp, *ninp;
584 u_int16_t fport = fport_arg, lport = lport_arg;
585 int nmatch;
586
587 if (in_nullhost(faddr) || notify == 0)
588 return (0);
589
590 nmatch = 0;
591 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
592 for (inp = head->lh_first; inp != NULL; inp = ninp) {
593 ninp = inp->inp_hash.le_next;
594 if (in_hosteq(inp->inp_faddr, faddr) &&
595 inp->inp_fport == fport &&
596 inp->inp_lport == lport &&
597 in_hosteq(inp->inp_laddr, laddr)) {
598 (*notify)(inp, errno);
599 nmatch++;
600 }
601 }
602 return (nmatch);
603 }
604
605 void
606 in_pcbnotifyall(table, faddr, errno, notify)
607 struct inpcbtable *table;
608 struct in_addr faddr;
609 int errno;
610 void (*notify) __P((struct inpcb *, int));
611 {
612 register struct inpcb *inp, *ninp;
613
614 if (in_nullhost(faddr) || notify == 0)
615 return;
616
617 for (inp = table->inpt_queue.cqh_first;
618 inp != (struct inpcb *)&table->inpt_queue;
619 inp = ninp) {
620 ninp = inp->inp_queue.cqe_next;
621 if (in_hosteq(inp->inp_faddr, faddr))
622 (*notify)(inp, errno);
623 }
624 }
625
626 /*
627 * Check for alternatives when higher level complains
628 * about service problems. For now, invalidate cached
629 * routing information. If the route was created dynamically
630 * (by a redirect), time to try a default gateway again.
631 */
632 void
633 in_losing(inp)
634 struct inpcb *inp;
635 {
636 register struct rtentry *rt;
637 struct rt_addrinfo info;
638
639 if ((rt = inp->inp_route.ro_rt)) {
640 inp->inp_route.ro_rt = 0;
641 bzero((caddr_t)&info, sizeof(info));
642 info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst;
643 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
644 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
645 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
646 if (rt->rt_flags & RTF_DYNAMIC)
647 (void) rtrequest(RTM_DELETE, rt_key(rt),
648 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
649 (struct rtentry **)0);
650 else
651 /*
652 * A new route can be allocated
653 * the next time output is attempted.
654 */
655 rtfree(rt);
656 }
657 }
658
659 /*
660 * After a routing change, flush old routing
661 * and allocate a (hopefully) better one.
662 */
663 void
664 in_rtchange(inp, errno)
665 register struct inpcb *inp;
666 int errno;
667 {
668
669 if (inp->inp_route.ro_rt) {
670 rtfree(inp->inp_route.ro_rt);
671 inp->inp_route.ro_rt = 0;
672 /*
673 * A new route can be allocated the next time
674 * output is attempted.
675 */
676 }
677 /* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
678 }
679
680 struct inpcb *
681 in_pcblookup_port(table, laddr, lport_arg, lookup_wildcard)
682 struct inpcbtable *table;
683 struct in_addr laddr;
684 u_int lport_arg;
685 int lookup_wildcard;
686 {
687 register struct inpcb *inp, *match = 0;
688 int matchwild = 3, wildcard;
689 u_int16_t lport = lport_arg;
690
691 for (inp = table->inpt_queue.cqh_first;
692 inp != (struct inpcb *)&table->inpt_queue;
693 inp = inp->inp_queue.cqe_next) {
694 if (inp->inp_lport != lport)
695 continue;
696 wildcard = 0;
697 if (!in_nullhost(inp->inp_faddr))
698 wildcard++;
699 if (in_nullhost(inp->inp_laddr)) {
700 if (!in_nullhost(laddr))
701 wildcard++;
702 } else {
703 if (in_nullhost(laddr))
704 wildcard++;
705 else {
706 if (!in_hosteq(inp->inp_laddr, laddr))
707 continue;
708 }
709 }
710 if (wildcard && !lookup_wildcard)
711 continue;
712 if (wildcard < matchwild) {
713 match = inp;
714 matchwild = wildcard;
715 if (matchwild == 0)
716 break;
717 }
718 }
719 return (match);
720 }
721
722 #ifdef DIAGNOSTIC
723 int in_pcbnotifymiss = 0;
724 #endif
725
726 struct inpcb *
727 in_pcblookup_connect(table, faddr, fport_arg, laddr, lport_arg)
728 struct inpcbtable *table;
729 struct in_addr faddr, laddr;
730 u_int fport_arg, lport_arg;
731 {
732 struct inpcbhead *head;
733 register struct inpcb *inp;
734 u_int16_t fport = fport_arg, lport = lport_arg;
735
736 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
737 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
738 if (in_hosteq(inp->inp_faddr, faddr) &&
739 inp->inp_fport == fport &&
740 inp->inp_lport == lport &&
741 in_hosteq(inp->inp_laddr, laddr))
742 goto out;
743 }
744 #ifdef DIAGNOSTIC
745 if (in_pcbnotifymiss) {
746 printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
747 ntohl(faddr.s_addr), ntohs(fport),
748 ntohl(laddr.s_addr), ntohs(lport));
749 }
750 #endif
751 return (0);
752
753 out:
754 /* Move this PCB to the head of hash chain. */
755 if (inp != head->lh_first) {
756 LIST_REMOVE(inp, inp_hash);
757 LIST_INSERT_HEAD(head, inp, inp_hash);
758 }
759 return (inp);
760 }
761
762 struct inpcb *
763 in_pcblookup_bind(table, laddr, lport_arg)
764 struct inpcbtable *table;
765 struct in_addr laddr;
766 u_int lport_arg;
767 {
768 struct inpcbhead *head;
769 register struct inpcb *inp;
770 u_int16_t lport = lport_arg;
771
772 head = INPCBHASH_BIND(table, laddr, lport);
773 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
774 if (inp->inp_lport == lport &&
775 in_hosteq(inp->inp_laddr, laddr))
776 goto out;
777 }
778 head = INPCBHASH_BIND(table, zeroin_addr, lport);
779 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
780 if (inp->inp_lport == lport &&
781 in_hosteq(inp->inp_laddr, zeroin_addr))
782 goto out;
783 }
784 #ifdef DIAGNOSTIC
785 if (in_pcbnotifymiss) {
786 printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
787 ntohl(laddr.s_addr), ntohs(lport));
788 }
789 #endif
790 return (0);
791
792 out:
793 /* Move this PCB to the head of hash chain. */
794 if (inp != head->lh_first) {
795 LIST_REMOVE(inp, inp_hash);
796 LIST_INSERT_HEAD(head, inp, inp_hash);
797 }
798 return (inp);
799 }
800
801 void
802 in_pcbstate(inp, state)
803 struct inpcb *inp;
804 int state;
805 {
806
807 if (inp->inp_state > INP_ATTACHED)
808 LIST_REMOVE(inp, inp_hash);
809
810 switch (state) {
811 case INP_BOUND:
812 LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
813 inp->inp_laddr, inp->inp_lport), inp, inp_hash);
814 break;
815 case INP_CONNECTED:
816 LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
817 inp->inp_faddr, inp->inp_fport,
818 inp->inp_laddr, inp->inp_lport), inp, inp_hash);
819 break;
820 }
821
822 inp->inp_state = state;
823 }
824
825 struct rtentry *
826 in_pcbrtentry(inp)
827 struct inpcb *inp;
828 {
829 struct route *ro;
830
831 ro = &inp->inp_route;
832
833 if (ro->ro_rt == NULL) {
834 /*
835 * No route yet, so try to acquire one.
836 */
837 if (!in_nullhost(inp->inp_faddr)) {
838 ro->ro_dst.sa_family = AF_INET;
839 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
840 satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr;
841 rtalloc(ro);
842 }
843 }
844 return (ro->ro_rt);
845 }
846
847 struct sockaddr_in *
848 in_selectsrc(sin, ro, soopts, mopts, errorp)
849 struct sockaddr_in *sin;
850 struct route *ro;
851 int soopts;
852 struct ip_moptions *mopts;
853 int *errorp;
854 {
855 struct in_ifaddr *ia;
856
857 ia = (struct in_ifaddr *)0;
858 /*
859 * If route is known or can be allocated now,
860 * our src addr is taken from the i/f, else punt.
861 */
862 if (ro->ro_rt &&
863 (!in_hosteq(satosin(&ro->ro_dst)->sin_addr, sin->sin_addr) ||
864 soopts & SO_DONTROUTE)) {
865 RTFREE(ro->ro_rt);
866 ro->ro_rt = (struct rtentry *)0;
867 }
868 if ((soopts & SO_DONTROUTE) == 0 && /*XXX*/
869 (ro->ro_rt == (struct rtentry *)0 ||
870 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
871 /* No route yet, so try to acquire one */
872 ro->ro_dst.sa_family = AF_INET;
873 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
874 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
875 rtalloc(ro);
876 }
877 /*
878 * If we found a route, use the address
879 * corresponding to the outgoing interface
880 * unless it is the loopback (in case a route
881 * to our address on another net goes to loopback).
882 *
883 * XXX Is this still true? Do we care?
884 */
885 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
886 ia = ifatoia(ro->ro_rt->rt_ifa);
887 if (ia == 0) {
888 u_int16_t fport = sin->sin_port;
889
890 sin->sin_port = 0;
891 ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
892 sin->sin_port = fport;
893 if (ia == 0) {
894 /* Find 1st non-loopback AF_INET address */
895 for (ia = in_ifaddr.tqh_first;
896 ia != NULL;
897 ia = ia->ia_list.tqe_next)
898 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
899 break;
900 }
901 if (ia == 0) {
902 *errorp = EADDRNOTAVAIL;
903 return NULL;
904 }
905 }
906 /*
907 * If the destination address is multicast and an outgoing
908 * interface has been set as a multicast option, use the
909 * address of that interface as our source address.
910 */
911 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
912 struct ip_moptions *imo;
913 struct ifnet *ifp;
914
915 imo = mopts;
916 if (imo->imo_multicast_ifp != NULL) {
917 ifp = imo->imo_multicast_ifp;
918 IFP_TO_IA(ifp, ia); /* XXX */
919 if (ia == 0) {
920 *errorp = EADDRNOTAVAIL;
921 return NULL;
922 }
923 }
924 }
925 return satosin(&ia->ia_addr);
926 }
927