in_pcb.c revision 1.70 1 /* $NetBSD: in_pcb.c,v 1.70 2001/07/25 23:28:02 itojun 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 (in_ifaddr.tqh_first == 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 (in_ifaddr.tqh_first != 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 = in_ifaddr.tqh_first->ia_addr.sin_addr;
373 else if (sin->sin_addr.s_addr == INADDR_BROADCAST)
374 for (ia = in_ifaddr.tqh_first; ia != NULL;
375 ia = ia->ia_list.tqe_next)
376 if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
377 sin->sin_addr = ia->ia_broadaddr.sin_addr;
378 break;
379 }
380 }
381 /*
382 * If we haven't bound which network number to use as ours,
383 * we will use the number of the outgoing interface.
384 * This depends on having done a routing lookup, which
385 * we will probably have to do anyway, so we might
386 * as well do it now. On the other hand if we are
387 * sending to multiple destinations we may have already
388 * done the lookup, so see if we can use the route
389 * from before. In any case, we only
390 * chose a port number once, even if sending to multiple
391 * destinations.
392 */
393 if (in_nullhost(inp->inp_laddr)) {
394 #if 0
395 struct route *ro;
396
397 ia = (struct in_ifaddr *)0;
398 /*
399 * If route is known or can be allocated now,
400 * our src addr is taken from the i/f, else punt.
401 */
402 ro = &inp->inp_route;
403 if (ro->ro_rt &&
404 (!in_hosteq(satosin(&ro->ro_dst)->sin_addr,
405 sin->sin_addr) ||
406 inp->inp_socket->so_options & SO_DONTROUTE)) {
407 RTFREE(ro->ro_rt);
408 ro->ro_rt = (struct rtentry *)0;
409 }
410 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
411 (ro->ro_rt == (struct rtentry *)0 ||
412 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
413 /* No route yet, so try to acquire one */
414 ro->ro_dst.sa_family = AF_INET;
415 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
416 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
417 rtalloc(ro);
418 }
419 /*
420 * If we found a route, use the address
421 * corresponding to the outgoing interface
422 * unless it is the loopback (in case a route
423 * to our address on another net goes to loopback).
424 *
425 * XXX Is this still true? Do we care?
426 */
427 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
428 ia = ifatoia(ro->ro_rt->rt_ifa);
429 if (ia == NULL) {
430 u_int16_t fport = sin->sin_port;
431
432 sin->sin_port = 0;
433 ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
434 sin->sin_port = fport;
435 if (ia == 0) {
436 /* Find 1st non-loopback AF_INET address */
437 for (ia = in_ifaddr.tqh_first ; ia != NULL;
438 ia = ia->ia_list.tqe_next) {
439 if ((ia->ia_ifp->if_flags &
440 IFF_LOOPBACK) == 0)
441 break;
442 }
443 }
444 if (ia == NULL)
445 return (EADDRNOTAVAIL);
446 }
447 /*
448 * If the destination address is multicast and an outgoing
449 * interface has been set as a multicast option, use the
450 * address of that interface as our source address.
451 */
452 if (IN_MULTICAST(sin->sin_addr.s_addr) &&
453 inp->inp_moptions != NULL) {
454 struct ip_moptions *imo;
455 struct ifnet *ifp;
456
457 imo = inp->inp_moptions;
458 if (imo->imo_multicast_ifp != NULL) {
459 ifp = imo->imo_multicast_ifp;
460 IFP_TO_IA(ifp, ia); /* XXX */
461 if (ia == 0)
462 return (EADDRNOTAVAIL);
463 }
464 }
465 ifaddr = satosin(&ia->ia_addr);
466 #else
467 int error;
468 ifaddr = in_selectsrc(sin, &inp->inp_route,
469 inp->inp_socket->so_options, inp->inp_moptions, &error);
470 if (ifaddr == NULL) {
471 if (error == 0)
472 error = EADDRNOTAVAIL;
473 return error;
474 }
475 #endif
476 }
477 if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
478 !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr,
479 inp->inp_lport) != 0)
480 return (EADDRINUSE);
481 if (in_nullhost(inp->inp_laddr)) {
482 if (inp->inp_lport == 0) {
483 error = in_pcbbind(inp, (struct mbuf *)0,
484 (struct proc *)0);
485 /*
486 * This used to ignore the return value
487 * completely, but we need to check for
488 * ephemeral port shortage.
489 * XXX Should we check for other errors, too?
490 */
491 if (error == EAGAIN)
492 return (error);
493 }
494 inp->inp_laddr = ifaddr->sin_addr;
495 }
496 inp->inp_faddr = sin->sin_addr;
497 inp->inp_fport = sin->sin_port;
498 in_pcbstate(inp, INP_CONNECTED);
499 return (0);
500 }
501
502 void
503 in_pcbdisconnect(v)
504 void *v;
505 {
506 struct inpcb *inp = v;
507
508 inp->inp_faddr = zeroin_addr;
509 inp->inp_fport = 0;
510 in_pcbstate(inp, INP_BOUND);
511 if (inp->inp_socket->so_state & SS_NOFDREF)
512 in_pcbdetach(inp);
513 }
514
515 void
516 in_pcbdetach(v)
517 void *v;
518 {
519 struct inpcb *inp = v;
520 struct socket *so = inp->inp_socket;
521 int s;
522
523 #ifdef IPSEC
524 ipsec4_delete_pcbpolicy(inp);
525 #endif /*IPSEC*/
526 so->so_pcb = 0;
527 sofree(so);
528 if (inp->inp_options)
529 (void)m_free(inp->inp_options);
530 if (inp->inp_route.ro_rt)
531 rtfree(inp->inp_route.ro_rt);
532 ip_freemoptions(inp->inp_moptions);
533 s = splnet();
534 in_pcbstate(inp, INP_ATTACHED);
535 CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue);
536 splx(s);
537 pool_put(&inpcb_pool, inp);
538 }
539
540 void
541 in_setsockaddr(inp, nam)
542 struct inpcb *inp;
543 struct mbuf *nam;
544 {
545 struct sockaddr_in *sin;
546
547 nam->m_len = sizeof (*sin);
548 sin = mtod(nam, struct sockaddr_in *);
549 bzero((caddr_t)sin, sizeof (*sin));
550 sin->sin_family = AF_INET;
551 sin->sin_len = sizeof(*sin);
552 sin->sin_port = inp->inp_lport;
553 sin->sin_addr = inp->inp_laddr;
554 }
555
556 void
557 in_setpeeraddr(inp, nam)
558 struct inpcb *inp;
559 struct mbuf *nam;
560 {
561 struct sockaddr_in *sin;
562
563 nam->m_len = sizeof (*sin);
564 sin = mtod(nam, struct sockaddr_in *);
565 bzero((caddr_t)sin, sizeof (*sin));
566 sin->sin_family = AF_INET;
567 sin->sin_len = sizeof(*sin);
568 sin->sin_port = inp->inp_fport;
569 sin->sin_addr = inp->inp_faddr;
570 }
571
572 /*
573 * Pass some notification to all connections of a protocol
574 * associated with address dst. The local address and/or port numbers
575 * may be specified to limit the search. The "usual action" will be
576 * taken, depending on the ctlinput cmd. The caller must filter any
577 * cmds that are uninteresting (e.g., no error in the map).
578 * Call the protocol specific routine (if any) to report
579 * any errors for each matching socket.
580 *
581 * Must be called at splsoftnet.
582 */
583 int
584 in_pcbnotify(table, faddr, fport_arg, laddr, lport_arg, errno, notify)
585 struct inpcbtable *table;
586 struct in_addr faddr, laddr;
587 u_int fport_arg, lport_arg;
588 int errno;
589 void (*notify) __P((struct inpcb *, int));
590 {
591 struct inpcbhead *head;
592 struct inpcb *inp, *ninp;
593 u_int16_t fport = fport_arg, lport = lport_arg;
594 int nmatch;
595
596 if (in_nullhost(faddr) || notify == 0)
597 return (0);
598
599 nmatch = 0;
600 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
601 for (inp = head->lh_first; inp != NULL; inp = ninp) {
602 ninp = inp->inp_hash.le_next;
603 if (in_hosteq(inp->inp_faddr, faddr) &&
604 inp->inp_fport == fport &&
605 inp->inp_lport == lport &&
606 in_hosteq(inp->inp_laddr, laddr)) {
607 (*notify)(inp, errno);
608 nmatch++;
609 }
610 }
611 return (nmatch);
612 }
613
614 void
615 in_pcbnotifyall(table, faddr, errno, notify)
616 struct inpcbtable *table;
617 struct in_addr faddr;
618 int errno;
619 void (*notify) __P((struct inpcb *, int));
620 {
621 struct inpcb *inp, *ninp;
622
623 if (in_nullhost(faddr) || notify == 0)
624 return;
625
626 for (inp = table->inpt_queue.cqh_first;
627 inp != (struct inpcb *)&table->inpt_queue;
628 inp = ninp) {
629 ninp = inp->inp_queue.cqe_next;
630 if (in_hosteq(inp->inp_faddr, faddr))
631 (*notify)(inp, errno);
632 }
633 }
634
635 void
636 in_pcbpurgeif0(table, ifp)
637 struct inpcbtable *table;
638 struct ifnet *ifp;
639 {
640 struct inpcb *inp, *ninp;
641 struct ip_moptions *imo;
642 int i, gap;
643
644 for (inp = table->inpt_queue.cqh_first;
645 inp != (struct inpcb *)&table->inpt_queue;
646 inp = ninp) {
647 ninp = inp->inp_queue.cqe_next;
648 imo = inp->inp_moptions;
649 if (imo != NULL) {
650 /*
651 * Unselect the outgoing interface if it is being
652 * detached.
653 */
654 if (imo->imo_multicast_ifp == ifp)
655 imo->imo_multicast_ifp = NULL;
656
657 /*
658 * Drop multicast group membership if we joined
659 * through the interface being detached.
660 */
661 for (i = 0, gap = 0; i < imo->imo_num_memberships;
662 i++) {
663 if (imo->imo_membership[i]->inm_ifp == ifp) {
664 in_delmulti(imo->imo_membership[i]);
665 gap++;
666 } else if (gap != 0)
667 imo->imo_membership[i - gap] =
668 imo->imo_membership[i];
669 }
670 imo->imo_num_memberships -= gap;
671 }
672 }
673 }
674
675 void
676 in_pcbpurgeif(table, ifp)
677 struct inpcbtable *table;
678 struct ifnet *ifp;
679 {
680 struct inpcb *inp, *ninp;
681
682 for (inp = table->inpt_queue.cqh_first;
683 inp != (struct inpcb *)&table->inpt_queue;
684 inp = ninp) {
685 ninp = inp->inp_queue.cqe_next;
686 if (inp->inp_route.ro_rt != NULL &&
687 inp->inp_route.ro_rt->rt_ifp == ifp)
688 in_rtchange(inp, 0);
689 }
690 }
691
692 /*
693 * Check for alternatives when higher level complains
694 * about service problems. For now, invalidate cached
695 * routing information. If the route was created dynamically
696 * (by a redirect), time to try a default gateway again.
697 */
698 void
699 in_losing(inp)
700 struct inpcb *inp;
701 {
702 struct rtentry *rt;
703 struct rt_addrinfo info;
704
705 if ((rt = inp->inp_route.ro_rt)) {
706 inp->inp_route.ro_rt = 0;
707 bzero((caddr_t)&info, sizeof(info));
708 info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst;
709 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
710 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
711 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
712 if (rt->rt_flags & RTF_DYNAMIC)
713 (void) rtrequest(RTM_DELETE, rt_key(rt),
714 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
715 (struct rtentry **)0);
716 else
717 /*
718 * A new route can be allocated
719 * the next time output is attempted.
720 */
721 rtfree(rt);
722 }
723 }
724
725 /*
726 * After a routing change, flush old routing
727 * and allocate a (hopefully) better one.
728 */
729 void
730 in_rtchange(inp, errno)
731 struct inpcb *inp;
732 int errno;
733 {
734
735 if (inp->inp_route.ro_rt) {
736 rtfree(inp->inp_route.ro_rt);
737 inp->inp_route.ro_rt = 0;
738 /*
739 * A new route can be allocated the next time
740 * output is attempted.
741 */
742 }
743 /* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
744 }
745
746 struct inpcb *
747 in_pcblookup_port(table, laddr, lport_arg, lookup_wildcard)
748 struct inpcbtable *table;
749 struct in_addr laddr;
750 u_int lport_arg;
751 int lookup_wildcard;
752 {
753 struct inpcb *inp, *match = 0;
754 int matchwild = 3, wildcard;
755 u_int16_t lport = lport_arg;
756
757 for (inp = table->inpt_queue.cqh_first;
758 inp != (struct inpcb *)&table->inpt_queue;
759 inp = inp->inp_queue.cqe_next) {
760 if (inp->inp_lport != lport)
761 continue;
762 wildcard = 0;
763 if (!in_nullhost(inp->inp_faddr))
764 wildcard++;
765 if (in_nullhost(inp->inp_laddr)) {
766 if (!in_nullhost(laddr))
767 wildcard++;
768 } else {
769 if (in_nullhost(laddr))
770 wildcard++;
771 else {
772 if (!in_hosteq(inp->inp_laddr, laddr))
773 continue;
774 }
775 }
776 if (wildcard && !lookup_wildcard)
777 continue;
778 if (wildcard < matchwild) {
779 match = inp;
780 matchwild = wildcard;
781 if (matchwild == 0)
782 break;
783 }
784 }
785 return (match);
786 }
787
788 #ifdef DIAGNOSTIC
789 int in_pcbnotifymiss = 0;
790 #endif
791
792 struct inpcb *
793 in_pcblookup_connect(table, faddr, fport_arg, laddr, lport_arg)
794 struct inpcbtable *table;
795 struct in_addr faddr, laddr;
796 u_int fport_arg, lport_arg;
797 {
798 struct inpcbhead *head;
799 struct inpcb *inp;
800 u_int16_t fport = fport_arg, lport = lport_arg;
801
802 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
803 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
804 if (in_hosteq(inp->inp_faddr, faddr) &&
805 inp->inp_fport == fport &&
806 inp->inp_lport == lport &&
807 in_hosteq(inp->inp_laddr, laddr))
808 goto out;
809 }
810 #ifdef DIAGNOSTIC
811 if (in_pcbnotifymiss) {
812 printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
813 ntohl(faddr.s_addr), ntohs(fport),
814 ntohl(laddr.s_addr), ntohs(lport));
815 }
816 #endif
817 return (0);
818
819 out:
820 /* Move this PCB to the head of hash chain. */
821 if (inp != head->lh_first) {
822 LIST_REMOVE(inp, inp_hash);
823 LIST_INSERT_HEAD(head, inp, inp_hash);
824 }
825 return (inp);
826 }
827
828 struct inpcb *
829 in_pcblookup_bind(table, laddr, lport_arg)
830 struct inpcbtable *table;
831 struct in_addr laddr;
832 u_int lport_arg;
833 {
834 struct inpcbhead *head;
835 struct inpcb *inp;
836 u_int16_t lport = lport_arg;
837
838 head = INPCBHASH_BIND(table, laddr, lport);
839 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
840 if (inp->inp_lport == lport &&
841 in_hosteq(inp->inp_laddr, laddr))
842 goto out;
843 }
844 head = INPCBHASH_BIND(table, zeroin_addr, lport);
845 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
846 if (inp->inp_lport == lport &&
847 in_hosteq(inp->inp_laddr, zeroin_addr))
848 goto out;
849 }
850 #ifdef DIAGNOSTIC
851 if (in_pcbnotifymiss) {
852 printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
853 ntohl(laddr.s_addr), ntohs(lport));
854 }
855 #endif
856 return (0);
857
858 out:
859 /* Move this PCB to the head of hash chain. */
860 if (inp != head->lh_first) {
861 LIST_REMOVE(inp, inp_hash);
862 LIST_INSERT_HEAD(head, inp, inp_hash);
863 }
864 return (inp);
865 }
866
867 void
868 in_pcbstate(inp, state)
869 struct inpcb *inp;
870 int state;
871 {
872
873 if (inp->inp_state > INP_ATTACHED)
874 LIST_REMOVE(inp, inp_hash);
875
876 switch (state) {
877 case INP_BOUND:
878 LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
879 inp->inp_laddr, inp->inp_lport), inp, inp_hash);
880 break;
881 case INP_CONNECTED:
882 LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
883 inp->inp_faddr, inp->inp_fport,
884 inp->inp_laddr, inp->inp_lport), inp, inp_hash);
885 break;
886 }
887
888 inp->inp_state = state;
889 }
890
891 struct rtentry *
892 in_pcbrtentry(inp)
893 struct inpcb *inp;
894 {
895 struct route *ro;
896
897 ro = &inp->inp_route;
898
899 if (ro->ro_rt == NULL) {
900 /*
901 * No route yet, so try to acquire one.
902 */
903 if (!in_nullhost(inp->inp_faddr)) {
904 ro->ro_dst.sa_family = AF_INET;
905 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
906 satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr;
907 rtalloc(ro);
908 }
909 }
910 return (ro->ro_rt);
911 }
912
913 struct sockaddr_in *
914 in_selectsrc(sin, ro, soopts, mopts, errorp)
915 struct sockaddr_in *sin;
916 struct route *ro;
917 int soopts;
918 struct ip_moptions *mopts;
919 int *errorp;
920 {
921 struct in_ifaddr *ia;
922
923 ia = (struct in_ifaddr *)0;
924 /*
925 * If route is known or can be allocated now,
926 * our src addr is taken from the i/f, else punt.
927 */
928 if (ro->ro_rt &&
929 (!in_hosteq(satosin(&ro->ro_dst)->sin_addr, sin->sin_addr) ||
930 soopts & SO_DONTROUTE)) {
931 RTFREE(ro->ro_rt);
932 ro->ro_rt = (struct rtentry *)0;
933 }
934 if ((soopts & SO_DONTROUTE) == 0 && /*XXX*/
935 (ro->ro_rt == (struct rtentry *)0 ||
936 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
937 /* No route yet, so try to acquire one */
938 ro->ro_dst.sa_family = AF_INET;
939 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
940 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
941 rtalloc(ro);
942 }
943 /*
944 * If we found a route, use the address
945 * corresponding to the outgoing interface
946 * unless it is the loopback (in case a route
947 * to our address on another net goes to loopback).
948 *
949 * XXX Is this still true? Do we care?
950 */
951 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
952 ia = ifatoia(ro->ro_rt->rt_ifa);
953 if (ia == NULL) {
954 u_int16_t fport = sin->sin_port;
955
956 sin->sin_port = 0;
957 ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
958 sin->sin_port = fport;
959 if (ia == 0) {
960 /* Find 1st non-loopback AF_INET address */
961 for (ia = in_ifaddr.tqh_first;
962 ia != NULL;
963 ia = ia->ia_list.tqe_next) {
964 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
965 break;
966 }
967 }
968 if (ia == NULL) {
969 *errorp = EADDRNOTAVAIL;
970 return NULL;
971 }
972 }
973 /*
974 * If the destination address is multicast and an outgoing
975 * interface has been set as a multicast option, use the
976 * address of that interface as our source address.
977 */
978 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
979 struct ip_moptions *imo;
980 struct ifnet *ifp;
981
982 imo = mopts;
983 if (imo->imo_multicast_ifp != NULL) {
984 ifp = imo->imo_multicast_ifp;
985 IFP_TO_IA(ifp, ia); /* XXX */
986 if (ia == 0) {
987 *errorp = EADDRNOTAVAIL;
988 return NULL;
989 }
990 }
991 }
992 return satosin(&ia->ia_addr);
993 }
994