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