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