No empty .Rs/.Re
[netbsd-mini2440.git] / sys / netinet / in_pcb.c
blob883af0c1a6b7ecd1c4b60e8a6441d66663c60670
1 /* $NetBSD: in_pcb.c,v 1.136 2009/05/09 20:54:52 elad Exp $ */
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
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.
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.
32 /*-
33 * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
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.
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.
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.
63 * Copyright (c) 1982, 1986, 1991, 1993, 1995
64 * The Regents of the University of California. All rights reserved.
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.
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.
90 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
93 #include <sys/cdefs.h>
94 __KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.136 2009/05/09 20:54:52 elad Exp $");
96 #include "opt_inet.h"
97 #include "opt_ipsec.h"
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/once.h>
110 #include <sys/pool.h>
111 #include <sys/proc.h>
112 #include <sys/kauth.h>
113 #include <sys/uidinfo.h>
114 #include <sys/domain.h>
116 #include <net/if.h>
117 #include <net/route.h>
119 #include <netinet/in.h>
120 #include <netinet/in_systm.h>
121 #include <netinet/ip.h>
122 #include <netinet/in_pcb.h>
123 #include <netinet/in_var.h>
124 #include <netinet/ip_var.h>
126 #ifdef INET6
127 #include <netinet/ip6.h>
128 #include <netinet6/ip6_var.h>
129 #include <netinet6/in6_pcb.h>
130 #endif
132 #ifdef IPSEC
133 #include <netinet6/ipsec.h>
134 #include <netkey/key.h>
135 #elif FAST_IPSEC
136 #include <netipsec/ipsec.h>
137 #include <netipsec/key.h>
138 #endif /* IPSEC */
140 struct in_addr zeroin_addr;
142 #define INPCBHASH_PORT(table, lport) \
143 &(table)->inpt_porthashtbl[ntohs(lport) & (table)->inpt_porthash]
144 #define INPCBHASH_BIND(table, laddr, lport) \
145 &(table)->inpt_bindhashtbl[ \
146 ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash]
147 #define INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
148 &(table)->inpt_connecthashtbl[ \
149 ((ntohl((faddr).s_addr) + ntohs(fport)) + \
150 (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash]
152 int anonportmin = IPPORT_ANONMIN;
153 int anonportmax = IPPORT_ANONMAX;
154 int lowportmin = IPPORT_RESERVEDMIN;
155 int lowportmax = IPPORT_RESERVEDMAX;
157 static struct pool inpcb_pool;
159 static int
160 inpcb_poolinit(void)
163 pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 0, 0, "inpcbpl", NULL,
164 IPL_NET);
165 return 0;
168 void
169 in_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize)
171 static ONCE_DECL(control);
173 CIRCLEQ_INIT(&table->inpt_queue);
174 table->inpt_porthashtbl = hashinit(bindhashsize, HASH_LIST, true,
175 &table->inpt_porthash);
176 table->inpt_bindhashtbl = hashinit(bindhashsize, HASH_LIST, true,
177 &table->inpt_bindhash);
178 table->inpt_connecthashtbl = hashinit(connecthashsize, HASH_LIST, true,
179 &table->inpt_connecthash);
180 table->inpt_lastlow = IPPORT_RESERVEDMAX;
181 table->inpt_lastport = (u_int16_t)anonportmax;
183 RUN_ONCE(&control, inpcb_poolinit);
187 in_pcballoc(struct socket *so, void *v)
189 struct inpcbtable *table = v;
190 struct inpcb *inp;
191 int s;
192 #if defined(IPSEC) || defined(FAST_IPSEC)
193 int error;
194 #endif
196 s = splnet();
197 inp = pool_get(&inpcb_pool, PR_NOWAIT);
198 splx(s);
199 if (inp == NULL)
200 return (ENOBUFS);
201 memset((void *)inp, 0, sizeof(*inp));
202 inp->inp_af = AF_INET;
203 inp->inp_table = table;
204 inp->inp_socket = so;
205 inp->inp_errormtu = -1;
206 #if defined(IPSEC) || defined(FAST_IPSEC)
207 error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
208 if (error != 0) {
209 s = splnet();
210 pool_put(&inpcb_pool, inp);
211 splx(s);
212 return error;
214 #endif
215 so->so_pcb = inp;
216 s = splnet();
217 CIRCLEQ_INSERT_HEAD(&table->inpt_queue, &inp->inp_head,
218 inph_queue);
219 LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head,
220 inph_lhash);
221 in_pcbstate(inp, INP_ATTACHED);
222 splx(s);
223 return (0);
226 static int
227 in_pcbsetport(struct sockaddr_in *sin, struct inpcb *inp, kauth_cred_t cred)
229 struct inpcbtable *table = inp->inp_table;
230 struct socket *so = inp->inp_socket;
231 int cnt;
232 u_int16_t mymin, mymax;
233 u_int16_t *lastport;
234 u_int16_t lport = 0;
235 enum kauth_network_req req;
236 int error;
238 if (inp->inp_flags & INP_LOWPORT) {
239 #ifndef IPNOPRIVPORTS
240 req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
241 #else
242 req = KAUTH_REQ_NETWORK_BIND_PORT;
243 #endif
245 mymin = lowportmin;
246 mymax = lowportmax;
247 lastport = &table->inpt_lastlow;
248 } else {
249 req = KAUTH_REQ_NETWORK_BIND_PORT;
251 mymin = anonportmin;
252 mymax = anonportmax;
253 lastport = &table->inpt_lastport;
256 /* XXX-kauth: KAUTH_REQ_NETWORK_BIND_AUTOASSIGN_{,PRIV}PORT */
257 error = kauth_authorize_network(cred, KAUTH_NETWORK_BIND, req, so, sin,
258 NULL);
259 if (error)
260 return (EACCES);
262 if (mymin > mymax) { /* sanity check */
263 u_int16_t swp;
265 swp = mymin;
266 mymin = mymax;
267 mymax = swp;
270 lport = *lastport - 1;
271 for (cnt = mymax - mymin + 1; cnt; cnt--, lport--) {
272 if (lport < mymin || lport > mymax)
273 lport = mymax;
274 if (!in_pcblookup_port(table, sin->sin_addr, htons(lport), 1)) {
275 /* We have a free port, check with the secmodel(s). */
276 sin->sin_port = lport;
277 error = kauth_authorize_network(cred,
278 KAUTH_NETWORK_BIND, req, so, sin, NULL);
279 if (error) {
280 /* Secmodel says no. Keep looking. */
281 continue;
284 goto found;
288 return (EAGAIN);
290 found:
291 inp->inp_flags |= INP_ANONPORT;
292 *lastport = lport;
293 lport = htons(lport);
294 inp->inp_lport = lport;
295 in_pcbstate(inp, INP_BOUND);
297 return (0);
300 static int
301 in_pcbbind_addr(struct inpcb *inp, struct sockaddr_in *sin, kauth_cred_t cred)
303 if (sin->sin_family != AF_INET)
304 return (EAFNOSUPPORT);
306 if (IN_MULTICAST(sin->sin_addr.s_addr)) {
307 /* Always succeed; port reuse handled in in_pcbbind_port(). */
308 } else if (!in_nullhost(sin->sin_addr)) {
309 struct in_ifaddr *ia = NULL;
311 INADDR_TO_IA(sin->sin_addr, ia);
312 /* check for broadcast addresses */
313 if (ia == NULL)
314 ia = ifatoia(ifa_ifwithaddr(sintosa(sin)));
315 if (ia == NULL)
316 return (EADDRNOTAVAIL);
319 inp->inp_laddr = sin->sin_addr;
321 return (0);
324 static int
325 in_pcbbind_port(struct inpcb *inp, struct sockaddr_in *sin, kauth_cred_t cred)
327 struct inpcbtable *table = inp->inp_table;
328 struct socket *so = inp->inp_socket;
329 int reuseport = (so->so_options & SO_REUSEPORT);
330 int wild = 0, error;
332 if (IN_MULTICAST(sin->sin_addr.s_addr)) {
334 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
335 * allow complete duplication of binding if
336 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
337 * and a multicast address is bound on both
338 * new and duplicated sockets.
340 if (so->so_options & SO_REUSEADDR)
341 reuseport = SO_REUSEADDR|SO_REUSEPORT;
344 if (sin->sin_port == 0) {
345 error = in_pcbsetport(sin, inp, cred);
346 if (error)
347 return (error);
348 } else {
349 struct inpcb *t;
350 #ifdef INET6
351 struct in6pcb *t6;
352 struct in6_addr mapped;
353 #endif
354 enum kauth_network_req req;
356 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
357 wild = 1;
359 #ifndef IPNOPRIVPORTS
360 if (ntohs(sin->sin_port) < IPPORT_RESERVED)
361 req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
362 else
363 #endif /* !IPNOPRIVPORTS */
364 req = KAUTH_REQ_NETWORK_BIND_PORT;
366 error = kauth_authorize_network(cred, KAUTH_NETWORK_BIND, req,
367 so, sin, NULL);
368 if (error)
369 return (EACCES);
371 #ifdef INET6
372 memset(&mapped, 0, sizeof(mapped));
373 mapped.s6_addr16[5] = 0xffff;
374 memcpy(&mapped.s6_addr32[3], &sin->sin_addr,
375 sizeof(mapped.s6_addr32[3]));
376 t6 = in6_pcblookup_port(table, &mapped, sin->sin_port, wild);
377 if (t6 && (reuseport & t6->in6p_socket->so_options) == 0)
378 return (EADDRINUSE);
379 #endif
381 /* XXX-kauth */
382 if (so->so_uidinfo->ui_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) {
383 t = in_pcblookup_port(table, sin->sin_addr, sin->sin_port, 1);
385 * XXX: investigate ramifications of loosening this
386 * restriction so that as long as both ports have
387 * SO_REUSEPORT allow the bind
389 if (t &&
390 (!in_nullhost(sin->sin_addr) ||
391 !in_nullhost(t->inp_laddr) ||
392 (t->inp_socket->so_options & SO_REUSEPORT) == 0)
393 && (so->so_uidinfo->ui_uid != t->inp_socket->so_uidinfo->ui_uid)) {
394 return (EADDRINUSE);
397 t = in_pcblookup_port(table, sin->sin_addr, sin->sin_port, wild);
398 if (t && (reuseport & t->inp_socket->so_options) == 0)
399 return (EADDRINUSE);
401 inp->inp_lport = sin->sin_port;
402 in_pcbstate(inp, INP_BOUND);
405 LIST_REMOVE(&inp->inp_head, inph_lhash);
406 LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head,
407 inph_lhash);
409 return (0);
413 in_pcbbind(void *v, struct mbuf *nam, struct lwp *l)
415 struct inpcb *inp = v;
416 struct sockaddr_in *sin = NULL; /* XXXGCC */
417 struct sockaddr_in lsin;
418 int error;
420 if (inp->inp_af != AF_INET)
421 return (EINVAL);
423 if (TAILQ_FIRST(&in_ifaddrhead) == 0)
424 return (EADDRNOTAVAIL);
425 if (inp->inp_lport || !in_nullhost(inp->inp_laddr))
426 return (EINVAL);
428 if (nam != NULL) {
429 sin = mtod(nam, struct sockaddr_in *);
430 if (nam->m_len != sizeof (*sin))
431 return (EINVAL);
432 } else {
433 lsin = *((const struct sockaddr_in *)
434 inp->inp_socket->so_proto->pr_domain->dom_sa_any);
435 sin = &lsin;
438 /* Bind address. */
439 error = in_pcbbind_addr(inp, sin, l->l_cred);
440 if (error)
441 return (error);
443 /* Bind port. */
444 error = in_pcbbind_port(inp, sin, l->l_cred);
445 if (error) {
446 inp->inp_laddr.s_addr = INADDR_ANY;
448 return (error);
451 return (0);
455 * Connect from a socket to a specified address.
456 * Both address and port must be specified in argument sin.
457 * If don't have a local address for this socket yet,
458 * then pick one.
461 in_pcbconnect(void *v, struct mbuf *nam, struct lwp *l)
463 struct inpcb *inp = v;
464 struct in_ifaddr *ia = NULL;
465 struct sockaddr_in *ifaddr = NULL;
466 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
467 int error;
469 if (inp->inp_af != AF_INET)
470 return (EINVAL);
472 if (nam->m_len != sizeof (*sin))
473 return (EINVAL);
474 if (sin->sin_family != AF_INET)
475 return (EAFNOSUPPORT);
476 if (sin->sin_port == 0)
477 return (EADDRNOTAVAIL);
478 if (TAILQ_FIRST(&in_ifaddrhead) != 0) {
480 * If the destination address is INADDR_ANY,
481 * use any local address (likely loopback).
482 * If the supplied address is INADDR_BROADCAST,
483 * use the broadcast address of an interface
484 * which supports broadcast. (loopback does not)
487 if (in_nullhost(sin->sin_addr)) {
488 sin->sin_addr =
489 TAILQ_FIRST(&in_ifaddrhead)->ia_addr.sin_addr;
490 } else if (sin->sin_addr.s_addr == INADDR_BROADCAST) {
491 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
492 if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
493 sin->sin_addr =
494 ia->ia_broadaddr.sin_addr;
495 break;
501 * If we haven't bound which network number to use as ours,
502 * we will use the number of the outgoing interface.
503 * This depends on having done a routing lookup, which
504 * we will probably have to do anyway, so we might
505 * as well do it now. On the other hand if we are
506 * sending to multiple destinations we may have already
507 * done the lookup, so see if we can use the route
508 * from before. In any case, we only
509 * chose a port number once, even if sending to multiple
510 * destinations.
512 if (in_nullhost(inp->inp_laddr)) {
513 int xerror;
514 ifaddr = in_selectsrc(sin, &inp->inp_route,
515 inp->inp_socket->so_options, inp->inp_moptions, &xerror);
516 if (ifaddr == NULL) {
517 if (xerror == 0)
518 xerror = EADDRNOTAVAIL;
519 return xerror;
521 INADDR_TO_IA(ifaddr->sin_addr, ia);
522 if (ia == NULL)
523 return (EADDRNOTAVAIL);
525 if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
526 !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr,
527 inp->inp_lport) != 0)
528 return (EADDRINUSE);
529 if (in_nullhost(inp->inp_laddr)) {
530 if (inp->inp_lport == 0) {
531 error = in_pcbbind(inp, NULL, l);
533 * This used to ignore the return value
534 * completely, but we need to check for
535 * ephemeral port shortage.
536 * And attempts to request low ports if not root.
538 if (error != 0)
539 return (error);
541 inp->inp_laddr = ifaddr->sin_addr;
543 inp->inp_faddr = sin->sin_addr;
544 inp->inp_fport = sin->sin_port;
545 in_pcbstate(inp, INP_CONNECTED);
546 #if defined(IPSEC) || defined(FAST_IPSEC)
547 if (inp->inp_socket->so_type == SOCK_STREAM)
548 ipsec_pcbconn(inp->inp_sp);
549 #endif
550 return (0);
553 void
554 in_pcbdisconnect(void *v)
556 struct inpcb *inp = v;
558 if (inp->inp_af != AF_INET)
559 return;
561 inp->inp_faddr = zeroin_addr;
562 inp->inp_fport = 0;
563 in_pcbstate(inp, INP_BOUND);
564 #if defined(IPSEC) || defined(FAST_IPSEC)
565 ipsec_pcbdisconn(inp->inp_sp);
566 #endif
567 if (inp->inp_socket->so_state & SS_NOFDREF)
568 in_pcbdetach(inp);
571 void
572 in_pcbdetach(void *v)
574 struct inpcb *inp = v;
575 struct socket *so = inp->inp_socket;
576 int s;
578 if (inp->inp_af != AF_INET)
579 return;
581 #if defined(IPSEC) || defined(FAST_IPSEC)
582 ipsec4_delete_pcbpolicy(inp);
583 #endif /*IPSEC*/
584 so->so_pcb = 0;
585 if (inp->inp_options)
586 (void)m_free(inp->inp_options);
587 rtcache_free(&inp->inp_route);
588 ip_freemoptions(inp->inp_moptions);
589 s = splnet();
590 in_pcbstate(inp, INP_ATTACHED);
591 LIST_REMOVE(&inp->inp_head, inph_lhash);
592 CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, &inp->inp_head,
593 inph_queue);
594 pool_put(&inpcb_pool, inp);
595 splx(s);
596 sofree(so); /* drops the socket's lock */
597 mutex_enter(softnet_lock); /* reacquire the softnet_lock */
600 void
601 in_setsockaddr(struct inpcb *inp, struct mbuf *nam)
603 struct sockaddr_in *sin;
605 if (inp->inp_af != AF_INET)
606 return;
608 sin = mtod(nam, struct sockaddr_in *);
609 sockaddr_in_init(sin, &inp->inp_laddr, inp->inp_lport);
610 nam->m_len = sin->sin_len;
613 void
614 in_setpeeraddr(struct inpcb *inp, struct mbuf *nam)
616 struct sockaddr_in *sin;
618 if (inp->inp_af != AF_INET)
619 return;
621 sin = mtod(nam, struct sockaddr_in *);
622 sockaddr_in_init(sin, &inp->inp_faddr, inp->inp_fport);
623 nam->m_len = sin->sin_len;
627 * Pass some notification to all connections of a protocol
628 * associated with address dst. The local address and/or port numbers
629 * may be specified to limit the search. The "usual action" will be
630 * taken, depending on the ctlinput cmd. The caller must filter any
631 * cmds that are uninteresting (e.g., no error in the map).
632 * Call the protocol specific routine (if any) to report
633 * any errors for each matching socket.
635 * Must be called at splsoftnet.
638 in_pcbnotify(struct inpcbtable *table, struct in_addr faddr, u_int fport_arg,
639 struct in_addr laddr, u_int lport_arg, int errno,
640 void (*notify)(struct inpcb *, int))
642 struct inpcbhead *head;
643 struct inpcb *inp, *ninp;
644 u_int16_t fport = fport_arg, lport = lport_arg;
645 int nmatch;
647 if (in_nullhost(faddr) || notify == 0)
648 return (0);
650 nmatch = 0;
651 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
652 for (inp = (struct inpcb *)LIST_FIRST(head); inp != NULL; inp = ninp) {
653 ninp = (struct inpcb *)LIST_NEXT(inp, inp_hash);
654 if (inp->inp_af != AF_INET)
655 continue;
656 if (in_hosteq(inp->inp_faddr, faddr) &&
657 inp->inp_fport == fport &&
658 inp->inp_lport == lport &&
659 in_hosteq(inp->inp_laddr, laddr)) {
660 (*notify)(inp, errno);
661 nmatch++;
664 return (nmatch);
667 void
668 in_pcbnotifyall(struct inpcbtable *table, struct in_addr faddr, int errno,
669 void (*notify)(struct inpcb *, int))
671 struct inpcb *inp, *ninp;
673 if (in_nullhost(faddr) || notify == 0)
674 return;
676 for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
677 inp != (void *)&table->inpt_queue;
678 inp = ninp) {
679 ninp = (struct inpcb *)CIRCLEQ_NEXT(inp, inp_queue);
680 if (inp->inp_af != AF_INET)
681 continue;
682 if (in_hosteq(inp->inp_faddr, faddr))
683 (*notify)(inp, errno);
687 void
688 in_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp)
690 struct inpcb *inp, *ninp;
691 struct ip_moptions *imo;
692 int i, gap;
694 for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
695 inp != (void *)&table->inpt_queue;
696 inp = ninp) {
697 ninp = (struct inpcb *)CIRCLEQ_NEXT(inp, inp_queue);
698 if (inp->inp_af != AF_INET)
699 continue;
700 imo = inp->inp_moptions;
701 if (imo != NULL) {
703 * Unselect the outgoing interface if it is being
704 * detached.
706 if (imo->imo_multicast_ifp == ifp)
707 imo->imo_multicast_ifp = NULL;
710 * Drop multicast group membership if we joined
711 * through the interface being detached.
713 for (i = 0, gap = 0; i < imo->imo_num_memberships;
714 i++) {
715 if (imo->imo_membership[i]->inm_ifp == ifp) {
716 in_delmulti(imo->imo_membership[i]);
717 gap++;
718 } else if (gap != 0)
719 imo->imo_membership[i - gap] =
720 imo->imo_membership[i];
722 imo->imo_num_memberships -= gap;
727 void
728 in_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp)
730 struct rtentry *rt;
731 struct inpcb *inp, *ninp;
733 for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
734 inp != (void *)&table->inpt_queue;
735 inp = ninp) {
736 ninp = (struct inpcb *)CIRCLEQ_NEXT(inp, inp_queue);
737 if (inp->inp_af != AF_INET)
738 continue;
739 if ((rt = rtcache_validate(&inp->inp_route)) != NULL &&
740 rt->rt_ifp == ifp)
741 in_rtchange(inp, 0);
746 * Check for alternatives when higher level complains
747 * about service problems. For now, invalidate cached
748 * routing information. If the route was created dynamically
749 * (by a redirect), time to try a default gateway again.
751 void
752 in_losing(struct inpcb *inp)
754 struct rtentry *rt;
755 struct rt_addrinfo info;
757 if (inp->inp_af != AF_INET)
758 return;
760 if ((rt = rtcache_validate(&inp->inp_route)) == NULL)
761 return;
763 memset(&info, 0, sizeof(info));
764 info.rti_info[RTAX_DST] = rtcache_getdst(&inp->inp_route);
765 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
766 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
767 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
768 if (rt->rt_flags & RTF_DYNAMIC)
769 (void) rtrequest(RTM_DELETE, rt_getkey(rt),
770 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
771 NULL);
773 * A new route can be allocated
774 * the next time output is attempted.
776 rtcache_free(&inp->inp_route);
780 * After a routing change, flush old routing. A new route can be
781 * allocated the next time output is attempted.
783 void
784 in_rtchange(struct inpcb *inp, int errno)
787 if (inp->inp_af != AF_INET)
788 return;
790 rtcache_free(&inp->inp_route);
792 /* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
795 struct inpcb *
796 in_pcblookup_port(struct inpcbtable *table, struct in_addr laddr,
797 u_int lport_arg, int lookup_wildcard)
799 struct inpcbhead *head;
800 struct inpcb_hdr *inph;
801 struct inpcb *inp, *match = 0;
802 int matchwild = 3, wildcard;
803 u_int16_t lport = lport_arg;
805 head = INPCBHASH_PORT(table, lport);
806 LIST_FOREACH(inph, head, inph_lhash) {
807 inp = (struct inpcb *)inph;
808 if (inp->inp_af != AF_INET)
809 continue;
811 if (inp->inp_lport != lport)
812 continue;
813 wildcard = 0;
814 if (!in_nullhost(inp->inp_faddr))
815 wildcard++;
816 if (in_nullhost(inp->inp_laddr)) {
817 if (!in_nullhost(laddr))
818 wildcard++;
819 } else {
820 if (in_nullhost(laddr))
821 wildcard++;
822 else {
823 if (!in_hosteq(inp->inp_laddr, laddr))
824 continue;
827 if (wildcard && !lookup_wildcard)
828 continue;
829 if (wildcard < matchwild) {
830 match = inp;
831 matchwild = wildcard;
832 if (matchwild == 0)
833 break;
836 return (match);
839 #ifdef DIAGNOSTIC
840 int in_pcbnotifymiss = 0;
841 #endif
843 struct inpcb *
844 in_pcblookup_connect(struct inpcbtable *table,
845 struct in_addr faddr, u_int fport_arg,
846 struct in_addr laddr, u_int lport_arg)
848 struct inpcbhead *head;
849 struct inpcb_hdr *inph;
850 struct inpcb *inp;
851 u_int16_t fport = fport_arg, lport = lport_arg;
853 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
854 LIST_FOREACH(inph, head, inph_hash) {
855 inp = (struct inpcb *)inph;
856 if (inp->inp_af != AF_INET)
857 continue;
859 if (in_hosteq(inp->inp_faddr, faddr) &&
860 inp->inp_fport == fport &&
861 inp->inp_lport == lport &&
862 in_hosteq(inp->inp_laddr, laddr))
863 goto out;
865 #ifdef DIAGNOSTIC
866 if (in_pcbnotifymiss) {
867 printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
868 ntohl(faddr.s_addr), ntohs(fport),
869 ntohl(laddr.s_addr), ntohs(lport));
871 #endif
872 return (0);
874 out:
875 /* Move this PCB to the head of hash chain. */
876 inph = &inp->inp_head;
877 if (inph != LIST_FIRST(head)) {
878 LIST_REMOVE(inph, inph_hash);
879 LIST_INSERT_HEAD(head, inph, inph_hash);
881 return (inp);
884 struct inpcb *
885 in_pcblookup_bind(struct inpcbtable *table,
886 struct in_addr laddr, u_int lport_arg)
888 struct inpcbhead *head;
889 struct inpcb_hdr *inph;
890 struct inpcb *inp;
891 u_int16_t lport = lport_arg;
893 head = INPCBHASH_BIND(table, laddr, lport);
894 LIST_FOREACH(inph, head, inph_hash) {
895 inp = (struct inpcb *)inph;
896 if (inp->inp_af != AF_INET)
897 continue;
899 if (inp->inp_lport == lport &&
900 in_hosteq(inp->inp_laddr, laddr))
901 goto out;
903 head = INPCBHASH_BIND(table, zeroin_addr, lport);
904 LIST_FOREACH(inph, head, inph_hash) {
905 inp = (struct inpcb *)inph;
906 if (inp->inp_af != AF_INET)
907 continue;
909 if (inp->inp_lport == lport &&
910 in_hosteq(inp->inp_laddr, zeroin_addr))
911 goto out;
913 #ifdef DIAGNOSTIC
914 if (in_pcbnotifymiss) {
915 printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
916 ntohl(laddr.s_addr), ntohs(lport));
918 #endif
919 return (0);
921 out:
922 /* Move this PCB to the head of hash chain. */
923 inph = &inp->inp_head;
924 if (inph != LIST_FIRST(head)) {
925 LIST_REMOVE(inph, inph_hash);
926 LIST_INSERT_HEAD(head, inph, inph_hash);
928 return (inp);
931 void
932 in_pcbstate(struct inpcb *inp, int state)
935 if (inp->inp_af != AF_INET)
936 return;
938 if (inp->inp_state > INP_ATTACHED)
939 LIST_REMOVE(&inp->inp_head, inph_hash);
941 switch (state) {
942 case INP_BOUND:
943 LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
944 inp->inp_laddr, inp->inp_lport), &inp->inp_head,
945 inph_hash);
946 break;
947 case INP_CONNECTED:
948 LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
949 inp->inp_faddr, inp->inp_fport,
950 inp->inp_laddr, inp->inp_lport), &inp->inp_head,
951 inph_hash);
952 break;
955 inp->inp_state = state;
958 struct rtentry *
959 in_pcbrtentry(struct inpcb *inp)
961 struct route *ro;
962 union {
963 struct sockaddr dst;
964 struct sockaddr_in dst4;
965 } u;
967 if (inp->inp_af != AF_INET)
968 return (NULL);
970 ro = &inp->inp_route;
972 sockaddr_in_init(&u.dst4, &inp->inp_faddr, 0);
973 return rtcache_lookup(ro, &u.dst);
976 struct sockaddr_in *
977 in_selectsrc(struct sockaddr_in *sin, struct route *ro,
978 int soopts, struct ip_moptions *mopts, int *errorp)
980 struct rtentry *rt = NULL;
981 struct in_ifaddr *ia = NULL;
984 * If route is known or can be allocated now, take the
985 * source address from the interface. Otherwise, punt.
987 if ((soopts & SO_DONTROUTE) != 0)
988 rtcache_free(ro);
989 else {
990 union {
991 struct sockaddr dst;
992 struct sockaddr_in dst4;
993 } u;
995 sockaddr_in_init(&u.dst4, &sin->sin_addr, 0);
996 rt = rtcache_lookup(ro, &u.dst);
999 * If we found a route, use the address
1000 * corresponding to the outgoing interface
1001 * unless it is the loopback (in case a route
1002 * to our address on another net goes to loopback).
1004 * XXX Is this still true? Do we care?
1006 if (rt != NULL && (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
1007 ia = ifatoia(rt->rt_ifa);
1008 if (ia == NULL) {
1009 u_int16_t fport = sin->sin_port;
1011 sin->sin_port = 0;
1012 ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
1013 sin->sin_port = fport;
1014 if (ia == NULL) {
1015 /* Find 1st non-loopback AF_INET address */
1016 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
1017 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
1018 break;
1021 if (ia == NULL) {
1022 *errorp = EADDRNOTAVAIL;
1023 return NULL;
1027 * If the destination address is multicast and an outgoing
1028 * interface has been set as a multicast option, use the
1029 * address of that interface as our source address.
1031 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
1032 struct ip_moptions *imo;
1033 struct ifnet *ifp;
1035 imo = mopts;
1036 if (imo->imo_multicast_ifp != NULL) {
1037 ifp = imo->imo_multicast_ifp;
1038 IFP_TO_IA(ifp, ia); /* XXX */
1039 if (ia == 0) {
1040 *errorp = EADDRNOTAVAIL;
1041 return NULL;
1045 if (ia->ia_ifa.ifa_getifa != NULL) {
1046 ia = ifatoia((*ia->ia_ifa.ifa_getifa)(&ia->ia_ifa,
1047 sintosa(sin)));
1049 #ifdef GETIFA_DEBUG
1050 else
1051 printf("%s: missing ifa_getifa\n", __func__);
1052 #endif
1053 return satosin(&ia->ia_addr);