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