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