linprocfs - Introduce /proc/mounts
[dragonfly.git] / sys / netinet / in_pcb.c
blob49ea73ba6c3d928a1d7143187aa5ec7e586ff2b9
1 /*
2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
35 * Copyright (c) 1982, 1986, 1991, 1993, 1995
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
66 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
67 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.27 2004/01/02 04:06:42 ambrisko Exp $
68 * $DragonFly: src/sys/netinet/in_pcb.c,v 1.48 2008/11/08 03:38:23 sephe Exp $
71 #include "opt_ipsec.h"
72 #include "opt_inet6.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/malloc.h>
77 #include <sys/mbuf.h>
78 #include <sys/domain.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/proc.h>
83 #include <sys/priv.h>
84 #include <sys/jail.h>
85 #include <sys/kernel.h>
86 #include <sys/sysctl.h>
87 #include <sys/thread2.h>
89 #include <machine/limits.h>
91 #include <net/if.h>
92 #include <net/if_types.h>
93 #include <net/route.h>
95 #include <netinet/in.h>
96 #include <netinet/in_pcb.h>
97 #include <netinet/in_var.h>
98 #include <netinet/ip_var.h>
99 #ifdef INET6
100 #include <netinet/ip6.h>
101 #include <netinet6/ip6_var.h>
102 #endif /* INET6 */
104 #ifdef IPSEC
105 #include <netinet6/ipsec.h>
106 #include <netproto/key/key.h>
107 #endif
109 #ifdef FAST_IPSEC
110 #if defined(IPSEC) || defined(IPSEC_ESP)
111 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
112 #endif
114 #include <netproto/ipsec/ipsec.h>
115 #include <netproto/ipsec/key.h>
116 #define IPSEC
117 #endif /* FAST_IPSEC */
119 struct in_addr zeroin_addr;
122 * These configure the range of local port addresses assigned to
123 * "unspecified" outgoing connections/packets/whatever.
125 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
126 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
128 int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
129 int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
131 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
132 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
134 static __inline void
135 RANGECHK(int var, int min, int max)
137 if (var < min)
138 var = min;
139 else if (var > max)
140 var = max;
143 static int
144 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
146 int error;
148 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
149 if (!error) {
150 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
151 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
153 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
154 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
156 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
157 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
159 return (error);
162 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
164 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
165 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
166 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
167 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
168 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
169 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
170 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
171 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
172 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
173 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
174 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
175 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
178 * in_pcb.c: manage the Protocol Control Blocks.
180 * NOTE: It is assumed that most of these functions will be called from
181 * a critical section. XXX - There are, unfortunately, a few exceptions
182 * to this rule that should be fixed.
184 * NOTE: The caller should initialize the cpu field to the cpu running the
185 * protocol stack associated with this inpcbinfo.
188 void
189 in_pcbinfo_init(struct inpcbinfo *pcbinfo)
191 LIST_INIT(&pcbinfo->pcblisthead);
192 pcbinfo->cpu = -1;
196 * Allocate a PCB and associate it with the socket.
199 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
201 struct inpcb *inp;
202 #ifdef IPSEC
203 int error;
204 #endif
206 inp = kmalloc(pcbinfo->ipi_size, M_PCB, M_WAITOK|M_ZERO);
207 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
208 inp->inp_pcbinfo = inp->inp_cpcbinfo = pcbinfo;
209 inp->inp_socket = so;
210 #ifdef IPSEC
211 error = ipsec_init_policy(so, &inp->inp_sp);
212 if (error != 0) {
213 kfree(inp, M_PCB);
214 return (error);
216 #endif
217 #ifdef INET6
218 if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
219 inp->inp_flags |= IN6P_IPV6_V6ONLY;
220 if (ip6_auto_flowlabel)
221 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
222 #endif
223 so->so_pcb = inp;
224 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
225 pcbinfo->ipi_count++;
226 return (0);
230 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
232 struct socket *so = inp->inp_socket;
233 struct proc *p = td->td_proc;
234 unsigned short *lastport;
235 struct sockaddr_in *sin;
236 struct sockaddr_in jsin;
237 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
238 struct ucred *cred = NULL;
239 u_short lport = 0;
240 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
241 int error;
243 KKASSERT(p);
245 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
246 return (EADDRNOTAVAIL);
247 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
248 return (EINVAL); /* already bound */
249 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
250 wild = 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
251 if (p)
252 cred = p->p_ucred;
253 if (nam != NULL) {
254 sin = (struct sockaddr_in *)nam;
255 if (nam->sa_len != sizeof *sin)
256 return (EINVAL);
257 #ifdef notdef
259 * We should check the family, but old programs
260 * incorrectly fail to initialize it.
262 if (sin->sin_family != AF_INET)
263 return (EAFNOSUPPORT);
264 #endif
265 if (!prison_replace_wildcards(td, nam))
266 return (EINVAL);
267 lport = sin->sin_port;
268 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
270 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
271 * allow complete duplication of binding if
272 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
273 * and a multicast address is bound on both
274 * new and duplicated sockets.
276 if (so->so_options & SO_REUSEADDR)
277 reuseport = SO_REUSEADDR | SO_REUSEPORT;
278 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
279 sin->sin_port = 0; /* yech... */
280 bzero(&sin->sin_zero, sizeof sin->sin_zero);
281 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL)
282 return (EADDRNOTAVAIL);
284 if (lport != 0) {
285 struct inpcb *t;
287 /* GROSS */
288 if (ntohs(lport) < IPPORT_RESERVED &&
289 cred && priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))
290 return (EACCES);
291 if (so->so_cred->cr_uid != 0 &&
292 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
293 t = in_pcblookup_local(inp->inp_pcbinfo,
294 sin->sin_addr, lport,
295 INPLOOKUP_WILDCARD, cred);
296 if (t &&
297 (!in_nullhost(sin->sin_addr) ||
298 !in_nullhost(t->inp_laddr) ||
299 (t->inp_socket->so_options &
300 SO_REUSEPORT) == 0) &&
301 (so->so_cred->cr_uid !=
302 t->inp_socket->so_cred->cr_uid)) {
303 #ifdef INET6
304 if (!in_nullhost(sin->sin_addr) ||
305 !in_nullhost(t->inp_laddr) ||
306 INP_SOCKAF(so) ==
307 INP_SOCKAF(t->inp_socket))
308 #endif
309 return (EADDRINUSE);
312 if (cred && !prison_replace_wildcards(td, nam))
313 return (EADDRNOTAVAIL);
314 t = in_pcblookup_local(pcbinfo, sin->sin_addr, lport,
315 wild, cred);
316 if (t && !(reuseport & t->inp_socket->so_options)) {
317 #ifdef INET6
318 if (!in_nullhost(sin->sin_addr) ||
319 !in_nullhost(t->inp_laddr) ||
320 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
321 #endif
322 return (EADDRINUSE);
325 inp->inp_laddr = sin->sin_addr;
327 if (lport == 0) {
328 ushort first, last;
329 int count;
331 jsin.sin_family = AF_INET;
332 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
333 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
334 inp->inp_laddr.s_addr = INADDR_ANY;
335 return (EINVAL);
337 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
339 inp->inp_flags |= INP_ANONPORT;
341 if (inp->inp_flags & INP_HIGHPORT) {
342 first = ipport_hifirstauto; /* sysctl */
343 last = ipport_hilastauto;
344 lastport = &pcbinfo->lasthi;
345 } else if (inp->inp_flags & INP_LOWPORT) {
346 if (cred &&
347 (error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
348 inp->inp_laddr.s_addr = INADDR_ANY;
349 return (error);
351 first = ipport_lowfirstauto; /* 1023 */
352 last = ipport_lowlastauto; /* 600 */
353 lastport = &pcbinfo->lastlow;
354 } else {
355 first = ipport_firstauto; /* sysctl */
356 last = ipport_lastauto;
357 lastport = &pcbinfo->lastport;
360 * Simple check to ensure all ports are not used up causing
361 * a deadlock here.
363 * We split the two cases (up and down) so that the direction
364 * is not being tested on each round of the loop.
366 if (first > last) {
368 * counting down
370 count = first - last;
372 do {
373 if (count-- < 0) { /* completely used? */
374 inp->inp_laddr.s_addr = INADDR_ANY;
375 return (EADDRNOTAVAIL);
377 --*lastport;
378 if (*lastport > first || *lastport < last)
379 *lastport = first;
380 lport = htons(*lastport);
381 } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
382 lport, wild, cred));
383 } else {
385 * counting up
387 count = last - first;
389 do {
390 if (count-- < 0) { /* completely used? */
391 inp->inp_laddr.s_addr = INADDR_ANY;
392 return (EADDRNOTAVAIL);
394 ++*lastport;
395 if (*lastport < first || *lastport > last)
396 *lastport = first;
397 lport = htons(*lastport);
398 } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
399 lport, wild, cred));
402 inp->inp_lport = lport;
404 jsin.sin_family = AF_INET;
405 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
406 if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin)) {
407 inp->inp_laddr.s_addr = INADDR_ANY;
408 inp->inp_lport = 0;
409 return (EINVAL);
411 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
413 if (in_pcbinsporthash(inp) != 0) {
414 inp->inp_laddr.s_addr = INADDR_ANY;
415 inp->inp_lport = 0;
416 return (EAGAIN);
418 return (0);
422 * Transform old in_pcbconnect() into an inner subroutine for new
423 * in_pcbconnect(): Do some validity-checking on the remote
424 * address (in mbuf 'nam') and then determine local host address
425 * (i.e., which interface) to use to access that remote host.
427 * This preserves definition of in_pcbconnect(), while supporting a
428 * slightly different version for T/TCP. (This is more than
429 * a bit of a kludge, but cleaning up the internal interfaces would
430 * have forced minor changes in every protocol).
433 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
434 struct sockaddr_in **plocal_sin, struct thread *td)
436 struct in_ifaddr *ia;
437 struct ucred *cred = NULL;
438 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
439 struct sockaddr *jsin;
440 int jailed = 0, alloc_route = 0;
442 if (nam->sa_len != sizeof *sin)
443 return (EINVAL);
444 if (sin->sin_family != AF_INET)
445 return (EAFNOSUPPORT);
446 if (sin->sin_port == 0)
447 return (EADDRNOTAVAIL);
448 if (td && td->td_proc && td->td_proc->p_ucred)
449 cred = td->td_proc->p_ucred;
450 if (cred && cred->cr_prison)
451 jailed = 1;
452 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
453 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
455 * If the destination address is INADDR_ANY,
456 * use the primary local address.
457 * If the supplied address is INADDR_BROADCAST,
458 * and the primary interface supports broadcast,
459 * choose the broadcast address for that interface.
461 if (sin->sin_addr.s_addr == INADDR_ANY)
462 sin->sin_addr = IA_SIN(ia)->sin_addr;
463 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
464 (ia->ia_ifp->if_flags & IFF_BROADCAST))
465 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
467 if (inp->inp_laddr.s_addr == INADDR_ANY) {
468 struct route *ro;
470 ia = NULL;
472 * If route is known or can be allocated now,
473 * our src addr is taken from the i/f, else punt.
474 * Note that we should check the address family of the cached
475 * destination, in case of sharing the cache with IPv6.
477 ro = &inp->inp_route;
478 if (ro->ro_rt &&
479 (!(ro->ro_rt->rt_flags & RTF_UP) ||
480 ro->ro_dst.sa_family != AF_INET ||
481 satosin(&ro->ro_dst)->sin_addr.s_addr !=
482 sin->sin_addr.s_addr ||
483 inp->inp_socket->so_options & SO_DONTROUTE)) {
484 RTFREE(ro->ro_rt);
485 ro->ro_rt = NULL;
487 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
488 (ro->ro_rt == NULL ||
489 ro->ro_rt->rt_ifp == NULL)) {
490 /* No route yet, so try to acquire one */
491 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
492 ro->ro_dst.sa_family = AF_INET;
493 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
494 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
495 sin->sin_addr;
496 rtalloc(ro);
497 alloc_route = 1;
500 * If we found a route, use the address
501 * corresponding to the outgoing interface
502 * unless it is the loopback (in case a route
503 * to our address on another net goes to loopback).
505 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
506 if (jailed) {
507 if (jailed_ip(cred->cr_prison,
508 ro->ro_rt->rt_ifa->ifa_addr)) {
509 ia = ifatoia(ro->ro_rt->rt_ifa);
511 } else {
512 ia = ifatoia(ro->ro_rt->rt_ifa);
515 if (ia == NULL) {
516 u_short fport = sin->sin_port;
518 sin->sin_port = 0;
519 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
520 if (ia && jailed && !jailed_ip(cred->cr_prison,
521 sintosa(&ia->ia_addr)))
522 ia = NULL;
523 if (ia == NULL)
524 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
525 if (ia && jailed && !jailed_ip(cred->cr_prison,
526 sintosa(&ia->ia_addr)))
527 ia = NULL;
528 sin->sin_port = fport;
529 if (ia == NULL &&
530 !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
531 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
532 if (ia && jailed && !jailed_ip(cred->cr_prison,
533 sintosa(&ia->ia_addr)))
534 ia = NULL;
536 if (!jailed && ia == NULL)
537 goto fail;
540 * If the destination address is multicast and an outgoing
541 * interface has been set as a multicast option, use the
542 * address of that interface as our source address.
544 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
545 inp->inp_moptions != NULL) {
546 struct ip_moptions *imo;
547 struct ifnet *ifp;
549 imo = inp->inp_moptions;
550 if (imo->imo_multicast_ifp != NULL) {
551 struct in_ifaddr_container *iac;
553 ifp = imo->imo_multicast_ifp;
554 ia = NULL;
555 TAILQ_FOREACH(iac,
556 &in_ifaddrheads[mycpuid], ia_link) {
557 if (iac->ia->ia_ifp == ifp) {
558 ia = iac->ia;
559 break;
562 if (ia == NULL)
563 goto fail;
567 * Don't do pcblookup call here; return interface in plocal_sin
568 * and exit to caller, that will do the lookup.
570 if (ia == NULL && jailed) {
571 if ((jsin = prison_get_nonlocal(cred->cr_prison, AF_INET, NULL)) != NULL ||
572 (jsin = prison_get_local(cred->cr_prison, AF_INET, NULL)) != NULL) {
573 *plocal_sin = satosin(jsin);
574 } else {
575 /* IPv6 only Jail */
576 goto fail;
578 } else {
579 *plocal_sin = &ia->ia_addr;
582 return (0);
583 fail:
584 if (alloc_route) {
585 struct route *ro = &inp->inp_route;
587 if (ro->ro_rt != NULL)
588 RTFREE(ro->ro_rt);
589 bzero(ro, sizeof(*ro));
591 return (EADDRNOTAVAIL);
595 * Outer subroutine:
596 * Connect from a socket to a specified address.
597 * Both address and port must be specified in argument sin.
598 * If don't have a local address for this socket yet,
599 * then pick one.
602 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
604 struct sockaddr_in *if_sin;
605 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
606 int error;
608 /* Call inner routine to assign local interface address. */
609 if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
610 return (error);
612 if (in_pcblookup_hash(inp->inp_cpcbinfo, sin->sin_addr, sin->sin_port,
613 inp->inp_laddr.s_addr ? inp->inp_laddr : if_sin->sin_addr,
614 inp->inp_lport, FALSE, NULL) != NULL) {
615 return (EADDRINUSE);
617 if (inp->inp_laddr.s_addr == INADDR_ANY) {
618 if (inp->inp_lport == 0) {
619 error = in_pcbbind(inp, NULL, td);
620 if (error)
621 return (error);
623 inp->inp_laddr = if_sin->sin_addr;
625 inp->inp_faddr = sin->sin_addr;
626 inp->inp_fport = sin->sin_port;
627 in_pcbinsconnhash(inp);
628 return (0);
631 void
632 in_pcbdisconnect(struct inpcb *inp)
635 inp->inp_faddr.s_addr = INADDR_ANY;
636 inp->inp_fport = 0;
637 in_pcbremconnhash(inp);
638 if (inp->inp_socket->so_state & SS_NOFDREF)
639 in_pcbdetach(inp);
642 void
643 in_pcbdetach(struct inpcb *inp)
645 struct socket *so = inp->inp_socket;
646 struct inpcbinfo *ipi = inp->inp_pcbinfo;
648 #ifdef IPSEC
649 ipsec4_delete_pcbpolicy(inp);
650 #endif /*IPSEC*/
651 inp->inp_gencnt = ++ipi->ipi_gencnt;
652 in_pcbremlists(inp);
653 so->so_pcb = NULL;
654 sofree(so);
655 if (inp->inp_options)
656 m_free(inp->inp_options);
657 if (inp->inp_route.ro_rt)
658 rtfree(inp->inp_route.ro_rt);
659 ip_freemoptions(inp->inp_moptions);
660 inp->inp_vflag = 0;
661 kfree(inp, M_PCB);
665 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
666 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
667 * in struct pr_usrreqs, so that protocols can just reference then directly
668 * without the need for a wrapper function. The socket must have a valid
669 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
670 * except through a kernel programming error, so it is acceptable to panic
671 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
672 * because there actually /is/ a programming error somewhere... XXX)
675 in_setsockaddr(struct socket *so, struct sockaddr **nam)
677 struct inpcb *inp;
678 struct sockaddr_in *sin;
681 * Do the malloc first in case it blocks.
683 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
684 M_WAITOK | M_ZERO);
685 sin->sin_family = AF_INET;
686 sin->sin_len = sizeof *sin;
688 crit_enter();
689 inp = so->so_pcb;
690 if (!inp) {
691 crit_exit();
692 kfree(sin, M_SONAME);
693 return (ECONNRESET);
695 sin->sin_port = inp->inp_lport;
696 sin->sin_addr = inp->inp_laddr;
697 crit_exit();
699 *nam = (struct sockaddr *)sin;
700 return (0);
704 in_setpeeraddr(struct socket *so, struct sockaddr **nam)
706 struct inpcb *inp;
707 struct sockaddr_in *sin;
710 * Do the malloc first in case it blocks.
712 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
713 M_WAITOK | M_ZERO);
714 sin->sin_family = AF_INET;
715 sin->sin_len = sizeof *sin;
717 crit_enter();
718 inp = so->so_pcb;
719 if (!inp) {
720 crit_exit();
721 kfree(sin, M_SONAME);
722 return (ECONNRESET);
724 sin->sin_port = inp->inp_fport;
725 sin->sin_addr = inp->inp_faddr;
726 crit_exit();
728 *nam = (struct sockaddr *)sin;
729 return (0);
732 void
733 in_pcbnotifyall(struct inpcbhead *head, struct in_addr faddr, int err,
734 void (*notify)(struct inpcb *, int))
736 struct inpcb *inp, *ninp;
739 * note: if INP_PLACEMARKER is set we must ignore the rest of
740 * the structure and skip it.
742 crit_enter();
743 LIST_FOREACH_MUTABLE(inp, head, inp_list, ninp) {
744 if (inp->inp_flags & INP_PLACEMARKER)
745 continue;
746 #ifdef INET6
747 if (!(inp->inp_vflag & INP_IPV4))
748 continue;
749 #endif
750 if (inp->inp_faddr.s_addr != faddr.s_addr ||
751 inp->inp_socket == NULL)
752 continue;
753 (*notify)(inp, err); /* can remove inp from list! */
755 crit_exit();
758 void
759 in_pcbpurgeif0(struct inpcb *head, struct ifnet *ifp)
761 struct inpcb *inp;
762 struct ip_moptions *imo;
763 int i, gap;
765 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
766 if (inp->inp_flags & INP_PLACEMARKER)
767 continue;
768 imo = inp->inp_moptions;
769 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
771 * Unselect the outgoing interface if it is being
772 * detached.
774 if (imo->imo_multicast_ifp == ifp)
775 imo->imo_multicast_ifp = NULL;
778 * Drop multicast group membership if we joined
779 * through the interface being detached.
781 for (i = 0, gap = 0; i < imo->imo_num_memberships;
782 i++) {
783 if (imo->imo_membership[i]->inm_ifp == ifp) {
784 in_delmulti(imo->imo_membership[i]);
785 gap++;
786 } else if (gap != 0)
787 imo->imo_membership[i - gap] =
788 imo->imo_membership[i];
790 imo->imo_num_memberships -= gap;
796 * Check for alternatives when higher level complains
797 * about service problems. For now, invalidate cached
798 * routing information. If the route was created dynamically
799 * (by a redirect), time to try a default gateway again.
801 void
802 in_losing(struct inpcb *inp)
804 struct rtentry *rt;
805 struct rt_addrinfo rtinfo;
807 if ((rt = inp->inp_route.ro_rt)) {
808 bzero(&rtinfo, sizeof(struct rt_addrinfo));
809 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
810 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
811 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
812 rtinfo.rti_flags = rt->rt_flags;
813 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
814 if (rt->rt_flags & RTF_DYNAMIC)
815 rtrequest1_global(RTM_DELETE, &rtinfo, NULL, NULL);
816 inp->inp_route.ro_rt = NULL;
817 rtfree(rt);
819 * A new route can be allocated
820 * the next time output is attempted.
826 * After a routing change, flush old routing
827 * and allocate a (hopefully) better one.
829 void
830 in_rtchange(struct inpcb *inp, int err)
832 if (inp->inp_route.ro_rt) {
833 rtfree(inp->inp_route.ro_rt);
834 inp->inp_route.ro_rt = NULL;
836 * A new route can be allocated the next time
837 * output is attempted.
843 * Lookup a PCB based on the local address and port.
845 struct inpcb *
846 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
847 u_int lport_arg, int wild_okay, struct ucred *cred)
849 struct inpcb *inp;
850 int matchwild = 3, wildcard;
851 u_short lport = lport_arg;
853 struct inpcbporthead *porthash;
854 struct inpcbport *phd;
855 struct inpcb *match = NULL;
858 * Best fit PCB lookup.
860 * First see if this local port is in use by looking on the
861 * port hash list.
863 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
864 pcbinfo->porthashmask)];
865 LIST_FOREACH(phd, porthash, phd_hash) {
866 if (phd->phd_port == lport)
867 break;
869 if (phd != NULL) {
871 * Port is in use by one or more PCBs. Look for best
872 * fit.
874 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
875 wildcard = 0;
876 #ifdef INET6
877 if ((inp->inp_vflag & INP_IPV4) == 0)
878 continue;
879 #endif
880 if (inp->inp_faddr.s_addr != INADDR_ANY)
881 wildcard++;
882 if (inp->inp_laddr.s_addr != INADDR_ANY) {
883 if (laddr.s_addr == INADDR_ANY)
884 wildcard++;
885 else if (inp->inp_laddr.s_addr != laddr.s_addr)
886 continue;
887 } else {
888 if (laddr.s_addr != INADDR_ANY)
889 wildcard++;
891 if (wildcard && !wild_okay)
892 continue;
893 if (wildcard < matchwild &&
894 (cred == NULL ||
895 cred->cr_prison ==
896 inp->inp_socket->so_cred->cr_prison)) {
897 match = inp;
898 matchwild = wildcard;
899 if (matchwild == 0) {
900 break;
905 return (match);
909 * Lookup PCB in hash list.
911 struct inpcb *
912 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport_arg,
913 struct in_addr laddr, u_int lport_arg, boolean_t wildcard,
914 struct ifnet *ifp)
916 struct inpcbhead *head;
917 struct inpcb *inp, *jinp=NULL;
918 u_short fport = fport_arg, lport = lport_arg;
921 * First look for an exact match.
923 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
924 laddr.s_addr, lport, pcbinfo->hashmask)];
925 LIST_FOREACH(inp, head, inp_hash) {
926 #ifdef INET6
927 if (!(inp->inp_vflag & INP_IPV4))
928 continue;
929 #endif
930 if (in_hosteq(inp->inp_faddr, faddr) &&
931 in_hosteq(inp->inp_laddr, laddr) &&
932 inp->inp_fport == fport && inp->inp_lport == lport) {
933 /* found */
934 if (inp->inp_socket == NULL ||
935 inp->inp_socket->so_cred->cr_prison == NULL) {
936 return (inp);
937 } else {
938 if (jinp == NULL)
939 jinp = inp;
943 if (jinp != NULL)
944 return (jinp);
945 if (wildcard) {
946 struct inpcb *local_wild = NULL;
947 struct inpcb *jinp_wild = NULL;
948 #ifdef INET6
949 struct inpcb *local_wild_mapped = NULL;
950 #endif
951 struct inpcontainer *ic;
952 struct inpcontainerhead *chead;
953 struct sockaddr_in jsin;
954 struct ucred *cred;
957 * Order of socket selection:
958 * 1. non-jailed, non-wild.
959 * 2. non-jailed, wild.
960 * 3. jailed, non-wild.
961 * 4. jailed, wild.
963 jsin.sin_family = AF_INET;
964 chead = &pcbinfo->wildcardhashbase[
965 INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
966 LIST_FOREACH(ic, chead, ic_list) {
967 inp = ic->ic_inp;
968 jsin.sin_addr.s_addr = laddr.s_addr;
969 #ifdef INET6
970 if (!(inp->inp_vflag & INP_IPV4))
971 continue;
972 #endif
973 if (inp->inp_socket != NULL)
974 cred = inp->inp_socket->so_cred;
975 else
976 cred = NULL;
977 if (cred != NULL && jailed(cred)) {
978 if (jinp != NULL)
979 continue;
980 else
981 if (!jailed_ip(cred->cr_prison,
982 (struct sockaddr *)&jsin))
983 continue;
985 if (inp->inp_lport == lport) {
986 if (ifp && ifp->if_type == IFT_FAITH &&
987 !(inp->inp_flags & INP_FAITH))
988 continue;
989 if (inp->inp_laddr.s_addr == laddr.s_addr) {
990 if (cred != NULL && jailed(cred))
991 jinp = inp;
992 else
993 return (inp);
995 if (inp->inp_laddr.s_addr == INADDR_ANY) {
996 #ifdef INET6
997 if (INP_CHECK_SOCKAF(inp->inp_socket,
998 AF_INET6))
999 local_wild_mapped = inp;
1000 else
1001 #endif
1002 if (cred != NULL &&
1003 jailed(cred))
1004 jinp_wild = inp;
1005 else
1006 local_wild = inp;
1010 if (local_wild != NULL)
1011 return (local_wild);
1012 #ifdef INET6
1013 if (local_wild_mapped != NULL)
1014 return (local_wild_mapped);
1015 #endif
1016 if (jinp != NULL)
1017 return (jinp);
1018 return (jinp_wild);
1022 * Not found.
1024 return (NULL);
1028 * Insert PCB into connection hash table.
1030 void
1031 in_pcbinsconnhash(struct inpcb *inp)
1033 struct inpcbinfo *pcbinfo = inp->inp_cpcbinfo;
1034 struct inpcbhead *bucket;
1035 u_int32_t hashkey_faddr, hashkey_laddr;
1037 #ifdef INET6
1038 if (inp->inp_vflag & INP_IPV6) {
1039 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1040 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1041 } else {
1042 #endif
1043 hashkey_faddr = inp->inp_faddr.s_addr;
1044 hashkey_laddr = inp->inp_laddr.s_addr;
1045 #ifdef INET6
1047 #endif
1049 KASSERT(!(inp->inp_flags & INP_CONNECTED), ("already on hash list"));
1050 inp->inp_flags |= INP_CONNECTED;
1053 * Insert into the connection hash table.
1055 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1056 inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1057 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1061 * Remove PCB from connection hash table.
1063 void
1064 in_pcbremconnhash(struct inpcb *inp)
1066 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1067 LIST_REMOVE(inp, inp_hash);
1068 inp->inp_flags &= ~INP_CONNECTED;
1072 * Insert PCB into port hash table.
1075 in_pcbinsporthash(struct inpcb *inp)
1077 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1078 struct inpcbporthead *pcbporthash;
1079 struct inpcbport *phd;
1082 * Insert into the port hash table.
1084 pcbporthash = &pcbinfo->porthashbase[
1085 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->porthashmask)];
1087 /* Go through port list and look for a head for this lport. */
1088 LIST_FOREACH(phd, pcbporthash, phd_hash)
1089 if (phd->phd_port == inp->inp_lport)
1090 break;
1092 /* If none exists, malloc one and tack it on. */
1093 if (phd == NULL) {
1094 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport),
1095 M_PCB, M_INTWAIT | M_NULLOK);
1096 if (phd == NULL)
1097 return (ENOBUFS); /* XXX */
1098 phd->phd_port = inp->inp_lport;
1099 LIST_INIT(&phd->phd_pcblist);
1100 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1103 inp->inp_phd = phd;
1104 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1106 return (0);
1109 void
1110 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1112 struct inpcontainer *ic;
1113 struct inpcontainerhead *bucket;
1115 bucket = &pcbinfo->wildcardhashbase[
1116 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1118 ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1119 ic->ic_inp = inp;
1120 LIST_INSERT_HEAD(bucket, ic, ic_list);
1124 * Insert PCB into wildcard hash table.
1126 void
1127 in_pcbinswildcardhash(struct inpcb *inp)
1129 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1131 KKASSERT(pcbinfo != NULL);
1133 in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1134 inp->inp_flags |= INP_WILDCARD;
1137 void
1138 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1140 struct inpcontainer *ic;
1141 struct inpcontainerhead *head;
1143 /* find bucket */
1144 head = &pcbinfo->wildcardhashbase[
1145 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1147 LIST_FOREACH(ic, head, ic_list) {
1148 if (ic->ic_inp == inp)
1149 goto found;
1151 return; /* not found! */
1153 found:
1154 LIST_REMOVE(ic, ic_list); /* remove container from bucket chain */
1155 kfree(ic, M_TEMP); /* deallocate container */
1159 * Remove PCB from wildcard hash table.
1161 void
1162 in_pcbremwildcardhash(struct inpcb *inp)
1164 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1166 KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
1167 in_pcbremwildcardhash_oncpu(inp, pcbinfo);
1168 inp->inp_flags &= ~INP_WILDCARD;
1172 * Remove PCB from various lists.
1174 void
1175 in_pcbremlists(struct inpcb *inp)
1177 if (inp->inp_lport) {
1178 struct inpcbport *phd = inp->inp_phd;
1180 LIST_REMOVE(inp, inp_portlist);
1181 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1182 LIST_REMOVE(phd, phd_hash);
1183 kfree(phd, M_PCB);
1186 if (inp->inp_flags & INP_WILDCARD) {
1187 in_pcbremwildcardhash(inp);
1188 } else if (inp->inp_flags & INP_CONNECTED) {
1189 in_pcbremconnhash(inp);
1191 LIST_REMOVE(inp, inp_list);
1192 inp->inp_pcbinfo->ipi_count--;
1196 prison_xinpcb(struct thread *td, struct inpcb *inp)
1198 struct ucred *cr;
1200 if (td->td_proc == NULL)
1201 return (0);
1202 cr = td->td_proc->p_ucred;
1203 if (cr->cr_prison == NULL)
1204 return (0);
1205 if (inp->inp_socket && inp->inp_socket->so_cred &&
1206 inp->inp_socket->so_cred->cr_prison &&
1207 cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
1208 return (0);
1209 return (1);
1213 in_pcblist_global(SYSCTL_HANDLER_ARGS)
1215 struct inpcbinfo *pcbinfo = arg1;
1216 struct inpcb *inp, *marker;
1217 struct xinpcb xi;
1218 int error, i, n;
1219 inp_gen_t gencnt;
1222 * The process of preparing the TCB list is too time-consuming and
1223 * resource-intensive to repeat twice on every request.
1225 if (req->oldptr == NULL) {
1226 n = pcbinfo->ipi_count;
1227 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1228 return 0;
1231 if (req->newptr != NULL)
1232 return EPERM;
1235 * OK, now we're committed to doing something. Re-fetch ipi_count
1236 * after obtaining the generation count.
1238 gencnt = pcbinfo->ipi_gencnt;
1239 n = pcbinfo->ipi_count;
1241 marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
1242 marker->inp_flags |= INP_PLACEMARKER;
1243 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1245 i = 0;
1246 error = 0;
1248 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
1249 LIST_REMOVE(marker, inp_list);
1250 LIST_INSERT_AFTER(inp, marker, inp_list);
1252 if (inp->inp_flags & INP_PLACEMARKER)
1253 continue;
1254 if (inp->inp_gencnt > gencnt)
1255 continue;
1256 if (prison_xinpcb(req->td, inp))
1257 continue;
1258 bzero(&xi, sizeof xi);
1259 xi.xi_len = sizeof xi;
1260 bcopy(inp, &xi.xi_inp, sizeof *inp);
1261 if (inp->inp_socket)
1262 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1263 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
1264 break;
1265 ++i;
1267 LIST_REMOVE(marker, inp_list);
1268 if (error == 0 && i < n) {
1269 bzero(&xi, sizeof xi);
1270 xi.xi_len = sizeof xi;
1271 while (i < n) {
1272 error = SYSCTL_OUT(req, &xi, sizeof xi);
1273 ++i;
1276 kfree(marker, M_TEMP);
1277 return(error);