in_pcb.c revision 1.81 1 /* $NetBSD: in_pcb.c,v 1.81 2003/03/16 03:33:28 lukem 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 <sys/cdefs.h>
105 __KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.81 2003/03/16 03:33:28 lukem Exp $");
106
107 #include "opt_ipsec.h"
108
109 #include <sys/param.h>
110 #include <sys/systm.h>
111 #include <sys/malloc.h>
112 #include <sys/mbuf.h>
113 #include <sys/protosw.h>
114 #include <sys/socket.h>
115 #include <sys/socketvar.h>
116 #include <sys/ioctl.h>
117 #include <sys/errno.h>
118 #include <sys/time.h>
119 #include <sys/pool.h>
120 #include <sys/proc.h>
121
122 #include <net/if.h>
123 #include <net/route.h>
124
125 #include <netinet/in.h>
126 #include <netinet/in_systm.h>
127 #include <netinet/ip.h>
128 #include <netinet/in_pcb.h>
129 #include <netinet/in_var.h>
130 #include <netinet/ip_var.h>
131
132 #ifdef IPSEC
133 #include <netinet6/ipsec.h>
134 #include <netkey/key.h>
135 #endif /* IPSEC */
136
137 struct in_addr zeroin_addr;
138
139 #define INPCBHASH_BIND(table, laddr, lport) \
140 &(table)->inpt_bindhashtbl[ \
141 ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash]
142 #define INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
143 &(table)->inpt_connecthashtbl[ \
144 ((ntohl((faddr).s_addr) + ntohs(fport)) + \
145 (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash]
146
147 struct inpcb *
148 in_pcblookup_port __P((struct inpcbtable *,
149 struct in_addr, u_int, int));
150
151 int anonportmin = IPPORT_ANONMIN;
152 int anonportmax = IPPORT_ANONMAX;
153 int lowportmin = IPPORT_RESERVEDMIN;
154 int lowportmax = IPPORT_RESERVEDMAX;
155
156 struct pool inpcb_pool;
157
158 void
159 in_pcbinit(table, bindhashsize, connecthashsize)
160 struct inpcbtable *table;
161 int bindhashsize, connecthashsize;
162 {
163 static int inpcb_pool_initialized;
164
165 if (inpcb_pool_initialized == 0) {
166 pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 0, 0,
167 "inpcbpl", NULL);
168 inpcb_pool_initialized = 1;
169 }
170
171 CIRCLEQ_INIT(&table->inpt_queue);
172 table->inpt_bindhashtbl = hashinit(bindhashsize, HASH_LIST, M_PCB,
173 M_WAITOK, &table->inpt_bindhash);
174 table->inpt_connecthashtbl = hashinit(connecthashsize, HASH_LIST,
175 M_PCB, M_WAITOK, &table->inpt_connecthash);
176 table->inpt_lastlow = IPPORT_RESERVEDMAX;
177 table->inpt_lastport = (u_int16_t)anonportmax;
178 }
179
180 int
181 in_pcballoc(so, v)
182 struct socket *so;
183 void *v;
184 {
185 struct inpcbtable *table = v;
186 struct inpcb *inp;
187 int s;
188 #ifdef IPSEC
189 int error;
190 #endif
191
192 inp = pool_get(&inpcb_pool, PR_NOWAIT);
193 if (inp == NULL)
194 return (ENOBUFS);
195 bzero((caddr_t)inp, sizeof(*inp));
196 inp->inp_table = table;
197 inp->inp_socket = so;
198 inp->inp_errormtu = -1;
199 #ifdef IPSEC
200 error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
201 if (error != 0) {
202 pool_put(&inpcb_pool, inp);
203 return error;
204 }
205 #endif
206 so->so_pcb = inp;
207 s = splnet();
208 CIRCLEQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue);
209 in_pcbstate(inp, INP_ATTACHED);
210 splx(s);
211 return (0);
212 }
213
214 int
215 in_pcbbind(v, nam, p)
216 void *v;
217 struct mbuf *nam;
218 struct proc *p;
219 {
220 struct inpcb *inp = v;
221 struct socket *so = inp->inp_socket;
222 struct inpcbtable *table = inp->inp_table;
223 struct sockaddr_in *sin;
224 u_int16_t lport = 0;
225 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
226
227 if (TAILQ_FIRST(&in_ifaddr) == 0)
228 return (EADDRNOTAVAIL);
229 if (inp->inp_lport || !in_nullhost(inp->inp_laddr))
230 return (EINVAL);
231 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
232 wild = 1;
233 if (nam == 0)
234 goto noname;
235 sin = mtod(nam, struct sockaddr_in *);
236 if (nam->m_len != sizeof (*sin))
237 return (EINVAL);
238 if (sin->sin_family != AF_INET)
239 return (EAFNOSUPPORT);
240 lport = sin->sin_port;
241 if (IN_MULTICAST(sin->sin_addr.s_addr)) {
242 /*
243 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
244 * allow complete duplication of binding if
245 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
246 * and a multicast address is bound on both
247 * new and duplicated sockets.
248 */
249 if (so->so_options & SO_REUSEADDR)
250 reuseport = SO_REUSEADDR|SO_REUSEPORT;
251 } else if (!in_nullhost(sin->sin_addr)) {
252 sin->sin_port = 0; /* yech... */
253 if (ifa_ifwithaddr(sintosa(sin)) == 0)
254 return (EADDRNOTAVAIL);
255 }
256 if (lport) {
257 struct inpcb *t;
258 #ifndef IPNOPRIVPORTS
259 /* GROSS */
260 if (ntohs(lport) < IPPORT_RESERVED &&
261 (p == 0 || suser(p->p_ucred, &p->p_acflag)))
262 return (EACCES);
263 #endif
264 if (so->so_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) {
265 t = in_pcblookup_port(table, sin->sin_addr, lport, 1);
266 /*
267 * XXX: investigate ramifications of loosening this
268 * restriction so that as long as both ports have
269 * SO_REUSEPORT allow the bind
270 */
271 if (t &&
272 (!in_nullhost(sin->sin_addr) ||
273 !in_nullhost(t->inp_laddr) ||
274 (t->inp_socket->so_options & SO_REUSEPORT) == 0)
275 && (so->so_uid != t->inp_socket->so_uid)) {
276 return (EADDRINUSE);
277 }
278 }
279 t = in_pcblookup_port(table, sin->sin_addr, lport, wild);
280 if (t && (reuseport & t->inp_socket->so_options) == 0)
281 return (EADDRINUSE);
282 }
283 inp->inp_laddr = sin->sin_addr;
284
285 noname:
286 if (lport == 0) {
287 int cnt;
288 u_int16_t min, max;
289 u_int16_t *lastport;
290
291 if (inp->inp_flags & INP_LOWPORT) {
292 #ifndef IPNOPRIVPORTS
293 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
294 return (EACCES);
295 #endif
296 min = lowportmin;
297 max = lowportmax;
298 lastport = &table->inpt_lastlow;
299 } else {
300 min = anonportmin;
301 max = anonportmax;
302 lastport = &table->inpt_lastport;
303 }
304 if (min > max) { /* sanity check */
305 u_int16_t swp;
306
307 swp = min;
308 min = max;
309 max = swp;
310 }
311
312 lport = *lastport - 1;
313 for (cnt = max - min + 1; cnt; cnt--, lport--) {
314 if (lport < min || lport > max)
315 lport = max;
316 if (!in_pcblookup_port(table, inp->inp_laddr,
317 htons(lport), 1))
318 goto found;
319 }
320 if (!in_nullhost(inp->inp_laddr))
321 inp->inp_laddr.s_addr = INADDR_ANY;
322 return (EAGAIN);
323 found:
324 inp->inp_flags |= INP_ANONPORT;
325 *lastport = lport;
326 lport = htons(lport);
327 }
328 inp->inp_lport = lport;
329 in_pcbstate(inp, INP_BOUND);
330 return (0);
331 }
332
333 /*
334 * Connect from a socket to a specified address.
335 * Both address and port must be specified in argument sin.
336 * If don't have a local address for this socket yet,
337 * then pick one.
338 */
339 int
340 in_pcbconnect(v, nam)
341 void *v;
342 struct mbuf *nam;
343 {
344 struct inpcb *inp = v;
345 struct in_ifaddr *ia;
346 struct sockaddr_in *ifaddr = NULL;
347 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
348 int error;
349
350 if (nam->m_len != sizeof (*sin))
351 return (EINVAL);
352 if (sin->sin_family != AF_INET)
353 return (EAFNOSUPPORT);
354 if (sin->sin_port == 0)
355 return (EADDRNOTAVAIL);
356 if (TAILQ_FIRST(&in_ifaddr) != 0) {
357 /*
358 * If the destination address is INADDR_ANY,
359 * use any local address (likely loopback).
360 * If the supplied address is INADDR_BROADCAST,
361 * use the broadcast address of an interface
362 * which supports broadcast. (loopback does not)
363 */
364
365 if (in_nullhost(sin->sin_addr)) {
366 sin->sin_addr =
367 TAILQ_FIRST(&in_ifaddr)->ia_addr.sin_addr;
368 } else if (sin->sin_addr.s_addr == INADDR_BROADCAST) {
369 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
370 if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
371 sin->sin_addr =
372 ia->ia_broadaddr.sin_addr;
373 break;
374 }
375 }
376 }
377 }
378 /*
379 * If we haven't bound which network number to use as ours,
380 * we will use the number of the outgoing interface.
381 * This depends on having done a routing lookup, which
382 * we will probably have to do anyway, so we might
383 * as well do it now. On the other hand if we are
384 * sending to multiple destinations we may have already
385 * done the lookup, so see if we can use the route
386 * from before. In any case, we only
387 * chose a port number once, even if sending to multiple
388 * destinations.
389 */
390 if (in_nullhost(inp->inp_laddr)) {
391 int error;
392 ifaddr = in_selectsrc(sin, &inp->inp_route,
393 inp->inp_socket->so_options, inp->inp_moptions, &error);
394 if (ifaddr == NULL) {
395 if (error == 0)
396 error = EADDRNOTAVAIL;
397 return error;
398 }
399 }
400 if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
401 !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr,
402 inp->inp_lport) != 0)
403 return (EADDRINUSE);
404 if (in_nullhost(inp->inp_laddr)) {
405 if (inp->inp_lport == 0) {
406 error = in_pcbbind(inp, (struct mbuf *)0,
407 (struct proc *)0);
408 /*
409 * This used to ignore the return value
410 * completely, but we need to check for
411 * ephemeral port shortage.
412 * XXX Should we check for other errors, too?
413 */
414 if (error == EAGAIN)
415 return (error);
416 }
417 inp->inp_laddr = ifaddr->sin_addr;
418 }
419 inp->inp_faddr = sin->sin_addr;
420 inp->inp_fport = sin->sin_port;
421 in_pcbstate(inp, INP_CONNECTED);
422 #ifdef IPSEC
423 if (inp->inp_socket->so_type == SOCK_STREAM)
424 ipsec_pcbconn(inp->inp_sp);
425 #endif
426 return (0);
427 }
428
429 void
430 in_pcbdisconnect(v)
431 void *v;
432 {
433 struct inpcb *inp = v;
434
435 inp->inp_faddr = zeroin_addr;
436 inp->inp_fport = 0;
437 in_pcbstate(inp, INP_BOUND);
438 if (inp->inp_socket->so_state & SS_NOFDREF)
439 in_pcbdetach(inp);
440 #ifdef IPSEC
441 ipsec_pcbdisconn(inp->inp_sp);
442 #endif
443 }
444
445 void
446 in_pcbdetach(v)
447 void *v;
448 {
449 struct inpcb *inp = v;
450 struct socket *so = inp->inp_socket;
451 int s;
452
453 #ifdef IPSEC
454 ipsec4_delete_pcbpolicy(inp);
455 #endif /*IPSEC*/
456 so->so_pcb = 0;
457 sofree(so);
458 if (inp->inp_options)
459 (void)m_free(inp->inp_options);
460 if (inp->inp_route.ro_rt)
461 rtfree(inp->inp_route.ro_rt);
462 ip_freemoptions(inp->inp_moptions);
463 s = splnet();
464 in_pcbstate(inp, INP_ATTACHED);
465 CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue);
466 splx(s);
467 pool_put(&inpcb_pool, inp);
468 }
469
470 void
471 in_setsockaddr(inp, nam)
472 struct inpcb *inp;
473 struct mbuf *nam;
474 {
475 struct sockaddr_in *sin;
476
477 nam->m_len = sizeof (*sin);
478 sin = mtod(nam, struct sockaddr_in *);
479 bzero((caddr_t)sin, sizeof (*sin));
480 sin->sin_family = AF_INET;
481 sin->sin_len = sizeof(*sin);
482 sin->sin_port = inp->inp_lport;
483 sin->sin_addr = inp->inp_laddr;
484 }
485
486 void
487 in_setpeeraddr(inp, nam)
488 struct inpcb *inp;
489 struct mbuf *nam;
490 {
491 struct sockaddr_in *sin;
492
493 nam->m_len = sizeof (*sin);
494 sin = mtod(nam, struct sockaddr_in *);
495 bzero((caddr_t)sin, sizeof (*sin));
496 sin->sin_family = AF_INET;
497 sin->sin_len = sizeof(*sin);
498 sin->sin_port = inp->inp_fport;
499 sin->sin_addr = inp->inp_faddr;
500 }
501
502 /*
503 * Pass some notification to all connections of a protocol
504 * associated with address dst. The local address and/or port numbers
505 * may be specified to limit the search. The "usual action" will be
506 * taken, depending on the ctlinput cmd. The caller must filter any
507 * cmds that are uninteresting (e.g., no error in the map).
508 * Call the protocol specific routine (if any) to report
509 * any errors for each matching socket.
510 *
511 * Must be called at splsoftnet.
512 */
513 int
514 in_pcbnotify(table, faddr, fport_arg, laddr, lport_arg, errno, notify)
515 struct inpcbtable *table;
516 struct in_addr faddr, laddr;
517 u_int fport_arg, lport_arg;
518 int errno;
519 void (*notify) __P((struct inpcb *, int));
520 {
521 struct inpcbhead *head;
522 struct inpcb *inp, *ninp;
523 u_int16_t fport = fport_arg, lport = lport_arg;
524 int nmatch;
525
526 if (in_nullhost(faddr) || notify == 0)
527 return (0);
528
529 nmatch = 0;
530 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
531 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
532 ninp = LIST_NEXT(inp, inp_hash);
533 if (in_hosteq(inp->inp_faddr, faddr) &&
534 inp->inp_fport == fport &&
535 inp->inp_lport == lport &&
536 in_hosteq(inp->inp_laddr, laddr)) {
537 (*notify)(inp, errno);
538 nmatch++;
539 }
540 }
541 return (nmatch);
542 }
543
544 void
545 in_pcbnotifyall(table, faddr, errno, notify)
546 struct inpcbtable *table;
547 struct in_addr faddr;
548 int errno;
549 void (*notify) __P((struct inpcb *, int));
550 {
551 struct inpcb *inp, *ninp;
552
553 if (in_nullhost(faddr) || notify == 0)
554 return;
555
556 for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
557 inp != (void *)&table->inpt_queue;
558 inp = ninp) {
559 ninp = CIRCLEQ_NEXT(inp, inp_queue);
560 if (in_hosteq(inp->inp_faddr, faddr))
561 (*notify)(inp, errno);
562 }
563 }
564
565 void
566 in_pcbpurgeif0(table, ifp)
567 struct inpcbtable *table;
568 struct ifnet *ifp;
569 {
570 struct inpcb *inp, *ninp;
571 struct ip_moptions *imo;
572 int i, gap;
573
574 for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
575 inp != (void *)&table->inpt_queue;
576 inp = ninp) {
577 ninp = CIRCLEQ_NEXT(inp, inp_queue);
578 imo = inp->inp_moptions;
579 if (imo != NULL) {
580 /*
581 * Unselect the outgoing interface if it is being
582 * detached.
583 */
584 if (imo->imo_multicast_ifp == ifp)
585 imo->imo_multicast_ifp = NULL;
586
587 /*
588 * Drop multicast group membership if we joined
589 * through the interface being detached.
590 */
591 for (i = 0, gap = 0; i < imo->imo_num_memberships;
592 i++) {
593 if (imo->imo_membership[i]->inm_ifp == ifp) {
594 in_delmulti(imo->imo_membership[i]);
595 gap++;
596 } else if (gap != 0)
597 imo->imo_membership[i - gap] =
598 imo->imo_membership[i];
599 }
600 imo->imo_num_memberships -= gap;
601 }
602 }
603 }
604
605 void
606 in_pcbpurgeif(table, ifp)
607 struct inpcbtable *table;
608 struct ifnet *ifp;
609 {
610 struct inpcb *inp, *ninp;
611
612 for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
613 inp != (void *)&table->inpt_queue;
614 inp = ninp) {
615 ninp = CIRCLEQ_NEXT(inp, inp_queue);
616 if (inp->inp_route.ro_rt != NULL &&
617 inp->inp_route.ro_rt->rt_ifp == ifp)
618 in_rtchange(inp, 0);
619 }
620 }
621
622 /*
623 * Check for alternatives when higher level complains
624 * about service problems. For now, invalidate cached
625 * routing information. If the route was created dynamically
626 * (by a redirect), time to try a default gateway again.
627 */
628 void
629 in_losing(inp)
630 struct inpcb *inp;
631 {
632 struct rtentry *rt;
633 struct rt_addrinfo info;
634
635 if ((rt = inp->inp_route.ro_rt)) {
636 inp->inp_route.ro_rt = 0;
637 bzero((caddr_t)&info, sizeof(info));
638 info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst;
639 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
640 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
641 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
642 if (rt->rt_flags & RTF_DYNAMIC)
643 (void) rtrequest(RTM_DELETE, rt_key(rt),
644 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
645 (struct rtentry **)0);
646 else
647 /*
648 * A new route can be allocated
649 * the next time output is attempted.
650 */
651 rtfree(rt);
652 }
653 }
654
655 /*
656 * After a routing change, flush old routing
657 * and allocate a (hopefully) better one.
658 */
659 void
660 in_rtchange(inp, errno)
661 struct inpcb *inp;
662 int errno;
663 {
664
665 if (inp->inp_route.ro_rt) {
666 rtfree(inp->inp_route.ro_rt);
667 inp->inp_route.ro_rt = 0;
668 /*
669 * A new route can be allocated the next time
670 * output is attempted.
671 */
672 }
673 /* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
674 }
675
676 struct inpcb *
677 in_pcblookup_port(table, laddr, lport_arg, lookup_wildcard)
678 struct inpcbtable *table;
679 struct in_addr laddr;
680 u_int lport_arg;
681 int lookup_wildcard;
682 {
683 struct inpcb *inp, *match = 0;
684 int matchwild = 3, wildcard;
685 u_int16_t lport = lport_arg;
686
687 CIRCLEQ_FOREACH(inp, &table->inpt_queue, inp_queue) {
688 if (inp->inp_lport != lport)
689 continue;
690 wildcard = 0;
691 if (!in_nullhost(inp->inp_faddr))
692 wildcard++;
693 if (in_nullhost(inp->inp_laddr)) {
694 if (!in_nullhost(laddr))
695 wildcard++;
696 } else {
697 if (in_nullhost(laddr))
698 wildcard++;
699 else {
700 if (!in_hosteq(inp->inp_laddr, laddr))
701 continue;
702 }
703 }
704 if (wildcard && !lookup_wildcard)
705 continue;
706 if (wildcard < matchwild) {
707 match = inp;
708 matchwild = wildcard;
709 if (matchwild == 0)
710 break;
711 }
712 }
713 return (match);
714 }
715
716 #ifdef DIAGNOSTIC
717 int in_pcbnotifymiss = 0;
718 #endif
719
720 struct inpcb *
721 in_pcblookup_connect(table, faddr, fport_arg, laddr, lport_arg)
722 struct inpcbtable *table;
723 struct in_addr faddr, laddr;
724 u_int fport_arg, lport_arg;
725 {
726 struct inpcbhead *head;
727 struct inpcb *inp;
728 u_int16_t fport = fport_arg, lport = lport_arg;
729
730 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
731 LIST_FOREACH(inp, head, inp_hash) {
732 if (in_hosteq(inp->inp_faddr, faddr) &&
733 inp->inp_fport == fport &&
734 inp->inp_lport == lport &&
735 in_hosteq(inp->inp_laddr, laddr))
736 goto out;
737 }
738 #ifdef DIAGNOSTIC
739 if (in_pcbnotifymiss) {
740 printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
741 ntohl(faddr.s_addr), ntohs(fport),
742 ntohl(laddr.s_addr), ntohs(lport));
743 }
744 #endif
745 return (0);
746
747 out:
748 /* Move this PCB to the head of hash chain. */
749 if (inp != LIST_FIRST(head)) {
750 LIST_REMOVE(inp, inp_hash);
751 LIST_INSERT_HEAD(head, inp, inp_hash);
752 }
753 return (inp);
754 }
755
756 struct inpcb *
757 in_pcblookup_bind(table, laddr, lport_arg)
758 struct inpcbtable *table;
759 struct in_addr laddr;
760 u_int lport_arg;
761 {
762 struct inpcbhead *head;
763 struct inpcb *inp;
764 u_int16_t lport = lport_arg;
765
766 head = INPCBHASH_BIND(table, laddr, lport);
767 LIST_FOREACH(inp, head, inp_hash) {
768 if (inp->inp_lport == lport &&
769 in_hosteq(inp->inp_laddr, laddr))
770 goto out;
771 }
772 head = INPCBHASH_BIND(table, zeroin_addr, lport);
773 LIST_FOREACH(inp, head, inp_hash) {
774 if (inp->inp_lport == lport &&
775 in_hosteq(inp->inp_laddr, zeroin_addr))
776 goto out;
777 }
778 #ifdef DIAGNOSTIC
779 if (in_pcbnotifymiss) {
780 printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
781 ntohl(laddr.s_addr), ntohs(lport));
782 }
783 #endif
784 return (0);
785
786 out:
787 /* Move this PCB to the head of hash chain. */
788 if (inp != LIST_FIRST(head)) {
789 LIST_REMOVE(inp, inp_hash);
790 LIST_INSERT_HEAD(head, inp, inp_hash);
791 }
792 return (inp);
793 }
794
795 void
796 in_pcbstate(inp, state)
797 struct inpcb *inp;
798 int state;
799 {
800
801 if (inp->inp_state > INP_ATTACHED)
802 LIST_REMOVE(inp, inp_hash);
803
804 switch (state) {
805 case INP_BOUND:
806 LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
807 inp->inp_laddr, inp->inp_lport), inp, inp_hash);
808 break;
809 case INP_CONNECTED:
810 LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
811 inp->inp_faddr, inp->inp_fport,
812 inp->inp_laddr, inp->inp_lport), inp, inp_hash);
813 break;
814 }
815
816 inp->inp_state = state;
817 }
818
819 struct rtentry *
820 in_pcbrtentry(inp)
821 struct inpcb *inp;
822 {
823 struct route *ro;
824
825 ro = &inp->inp_route;
826
827 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
828 !in_hosteq(satosin(&ro->ro_dst)->sin_addr, inp->inp_faddr))) {
829 RTFREE(ro->ro_rt);
830 ro->ro_rt = (struct rtentry *)NULL;
831 }
832 if (ro->ro_rt == (struct rtentry *)NULL &&
833 !in_nullhost(inp->inp_faddr)) {
834 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
835 ro->ro_dst.sa_family = AF_INET;
836 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
837 satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr;
838 rtalloc(ro);
839 }
840 return (ro->ro_rt);
841 }
842
843 struct sockaddr_in *
844 in_selectsrc(sin, ro, soopts, mopts, errorp)
845 struct sockaddr_in *sin;
846 struct route *ro;
847 int soopts;
848 struct ip_moptions *mopts;
849 int *errorp;
850 {
851 struct in_ifaddr *ia;
852
853 ia = (struct in_ifaddr *)0;
854 /*
855 * If route is known or can be allocated now,
856 * our src addr is taken from the i/f, else punt.
857 * Note that we should check the address family of the cached
858 * destination, in case of sharing the cache with IPv6.
859 */
860 if (ro->ro_rt &&
861 (ro->ro_dst.sa_family != AF_INET ||
862 !in_hosteq(satosin(&ro->ro_dst)->sin_addr, sin->sin_addr) ||
863 soopts & SO_DONTROUTE)) {
864 RTFREE(ro->ro_rt);
865 ro->ro_rt = (struct rtentry *)0;
866 }
867 if ((soopts & SO_DONTROUTE) == 0 && /*XXX*/
868 (ro->ro_rt == (struct rtentry *)0 ||
869 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
870 /* No route yet, so try to acquire one */
871 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
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 == NULL) {
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 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
896 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
897 break;
898 }
899 }
900 if (ia == NULL) {
901 *errorp = EADDRNOTAVAIL;
902 return NULL;
903 }
904 }
905 /*
906 * If the destination address is multicast and an outgoing
907 * interface has been set as a multicast option, use the
908 * address of that interface as our source address.
909 */
910 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
911 struct ip_moptions *imo;
912 struct ifnet *ifp;
913
914 imo = mopts;
915 if (imo->imo_multicast_ifp != NULL) {
916 ifp = imo->imo_multicast_ifp;
917 IFP_TO_IA(ifp, ia); /* XXX */
918 if (ia == 0) {
919 *errorp = EADDRNOTAVAIL;
920 return NULL;
921 }
922 }
923 }
924 return satosin(&ia->ia_addr);
925 }
926