Parallelize in_ifaddrhead operation
[dragonfly.git] / sys / netinet / in_pcb.c
CommitLineData
984263bc 1/*
c9ecd5e9 2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
66d6c637 3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
f23061d4 4 *
66d6c637
JH
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
f23061d4 7 *
66d6c637
JH
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.
f23061d4 19 *
66d6c637
JH
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.
32 */
33
66d6c637 34/*
984263bc
MD
35 * Copyright (c) 1982, 1986, 1991, 1993, 1995
36 * The Regents of the University of California. All rights reserved.
37 *
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.
53 *
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.
65 *
66 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
73bc1178 67 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.27 2004/01/02 04:06:42 ambrisko Exp $
1b562c24 68 * $DragonFly: src/sys/netinet/in_pcb.c,v 1.46 2008/06/08 08:38:05 sephe Exp $
984263bc
MD
69 */
70
71#include "opt_ipsec.h"
72#include "opt_inet6.h"
73
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/jail.h>
84#include <sys/kernel.h>
85#include <sys/sysctl.h>
1cae611f 86#include <sys/thread2.h>
984263bc
MD
87
88#include <machine/limits.h>
89
90#include <vm/vm_zone.h>
91
92#include <net/if.h>
93#include <net/if_types.h>
94#include <net/route.h>
95
96#include <netinet/in.h>
97#include <netinet/in_pcb.h>
98#include <netinet/in_var.h>
99#include <netinet/ip_var.h>
100#ifdef INET6
101#include <netinet/ip6.h>
102#include <netinet6/ip6_var.h>
103#endif /* INET6 */
104
105#ifdef IPSEC
106#include <netinet6/ipsec.h>
d2438d69 107#include <netproto/key/key.h>
80dff869 108#endif
984263bc
MD
109
110#ifdef FAST_IPSEC
111#if defined(IPSEC) || defined(IPSEC_ESP)
112#error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
113#endif
114
bf844ffa
JH
115#include <netproto/ipsec/ipsec.h>
116#include <netproto/ipsec/key.h>
984263bc
MD
117#define IPSEC
118#endif /* FAST_IPSEC */
119
80dff869 120struct in_addr zeroin_addr;
984263bc
MD
121
122/*
123 * These configure the range of local port addresses assigned to
124 * "unspecified" outgoing connections/packets/whatever.
125 */
80dff869
JH
126int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
127int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
128
129int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
130int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
984263bc 131
80dff869
JH
132int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
133int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
134
135static __inline void
136RANGECHK(int var, int min, int max)
137{
138 if (var < min)
139 var = min;
140 else if (var > max)
141 var = max;
142}
984263bc
MD
143
144static int
145sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
146{
80dff869
JH
147 int error;
148
149 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
984263bc
MD
150 if (!error) {
151 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
152 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
13d8907a 153
984263bc
MD
154 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
155 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
13d8907a 156
984263bc
MD
157 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
158 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
159 }
80dff869 160 return (error);
984263bc
MD
161}
162
984263bc
MD
163SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
164
165SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
166 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
167SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
168 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
169SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
170 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
171SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
172 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
173SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
174 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
175SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
176 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
177
178/*
179 * in_pcb.c: manage the Protocol Control Blocks.
180 *
1cae611f
MD
181 * NOTE: It is assumed that most of these functions will be called from
182 * a critical section. XXX - There are, unfortunately, a few exceptions
183 * to this rule that should be fixed.
eb594563
MD
184 *
185 * NOTE: The caller should initialize the cpu field to the cpu running the
186 * protocol stack associated with this inpcbinfo.
984263bc
MD
187 */
188
d2e9e54c
MD
189void
190in_pcbinfo_init(struct inpcbinfo *pcbinfo)
191{
192 LIST_INIT(&pcbinfo->pcblisthead);
eb594563 193 pcbinfo->cpu = -1;
d2e9e54c
MD
194}
195
984263bc
MD
196/*
197 * Allocate a PCB and associate it with the socket.
198 */
199int
e4700d00 200in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
984263bc 201{
dadab5e9 202 struct inpcb *inp;
984263bc
MD
203#ifdef IPSEC
204 int error;
205#endif
206
8a8d5d85 207 inp = zalloc(pcbinfo->ipi_zone);
984263bc
MD
208 if (inp == NULL)
209 return (ENOBUFS);
f23061d4 210 bzero(inp, sizeof *inp);
984263bc 211 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
7fe56515 212 inp->inp_pcbinfo = inp->inp_cpcbinfo = pcbinfo;
984263bc
MD
213 inp->inp_socket = so;
214#ifdef IPSEC
215 error = ipsec_init_policy(so, &inp->inp_sp);
216 if (error != 0) {
8a8d5d85 217 zfree(pcbinfo->ipi_zone, inp);
80dff869 218 return (error);
984263bc 219 }
80dff869
JH
220#endif
221#ifdef INET6
984263bc
MD
222 if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
223 inp->inp_flags |= IN6P_IPV6_V6ONLY;
80dff869
JH
224 if (ip6_auto_flowlabel)
225 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
984263bc 226#endif
f23061d4 227 so->so_pcb = inp;
d2e9e54c 228 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
984263bc 229 pcbinfo->ipi_count++;
984263bc
MD
230 return (0);
231}
232
233int
dadab5e9 234in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
984263bc 235{
dadab5e9
MD
236 struct socket *so = inp->inp_socket;
237 struct proc *p = td->td_proc;
984263bc
MD
238 unsigned short *lastport;
239 struct sockaddr_in *sin;
3e4150ef 240 struct sockaddr_in jsin;
984263bc 241 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
3e4150ef 242 struct ucred *cred = NULL;
984263bc
MD
243 u_short lport = 0;
244 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
3e4150ef 245 int error;
984263bc 246
dadab5e9
MD
247 KKASSERT(p);
248
1b562c24 249 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
984263bc 250 return (EADDRNOTAVAIL);
13d8907a
JH
251 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
252 return (EINVAL); /* already bound */
80dff869 253 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
13d8907a 254 wild = 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
3e4150ef
VBD
255 if (p)
256 cred = p->p_ucred;
13d8907a 257 if (nam != NULL) {
984263bc 258 sin = (struct sockaddr_in *)nam;
80dff869 259 if (nam->sa_len != sizeof *sin)
984263bc
MD
260 return (EINVAL);
261#ifdef notdef
262 /*
263 * We should check the family, but old programs
264 * incorrectly fail to initialize it.
265 */
266 if (sin->sin_family != AF_INET)
267 return (EAFNOSUPPORT);
268#endif
3e4150ef 269 if (!prison_replace_wildcards(td, nam))
80dff869 270 return (EINVAL);
984263bc
MD
271 lport = sin->sin_port;
272 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
273 /*
274 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
275 * allow complete duplication of binding if
276 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
277 * and a multicast address is bound on both
278 * new and duplicated sockets.
279 */
280 if (so->so_options & SO_REUSEADDR)
13d8907a 281 reuseport = SO_REUSEADDR | SO_REUSEPORT;
984263bc
MD
282 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
283 sin->sin_port = 0; /* yech... */
80dff869
JH
284 bzero(&sin->sin_zero, sizeof sin->sin_zero);
285 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL)
984263bc
MD
286 return (EADDRNOTAVAIL);
287 }
13d8907a 288 if (lport != 0) {
984263bc
MD
289 struct inpcb *t;
290
291 /* GROSS */
13d8907a 292 if (ntohs(lport) < IPPORT_RESERVED &&
3e4150ef 293 cred && suser_cred(cred, PRISON_ROOT))
984263bc 294 return (EACCES);
984263bc
MD
295 if (so->so_cred->cr_uid != 0 &&
296 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
297 t = in_pcblookup_local(inp->inp_pcbinfo,
298 sin->sin_addr, lport,
3e4150ef 299 INPLOOKUP_WILDCARD, cred);
984263bc 300 if (t &&
80dff869
JH
301 (!in_nullhost(sin->sin_addr) ||
302 !in_nullhost(t->inp_laddr) ||
984263bc
MD
303 (t->inp_socket->so_options &
304 SO_REUSEPORT) == 0) &&
305 (so->so_cred->cr_uid !=
306 t->inp_socket->so_cred->cr_uid)) {
80dff869
JH
307#ifdef INET6
308 if (!in_nullhost(sin->sin_addr) ||
309 !in_nullhost(t->inp_laddr) ||
984263bc
MD
310 INP_SOCKAF(so) ==
311 INP_SOCKAF(t->inp_socket))
80dff869 312#endif
984263bc
MD
313 return (EADDRINUSE);
314 }
315 }
3e4150ef 316 if (cred && !prison_replace_wildcards(td, nam))
984263bc 317 return (EADDRNOTAVAIL);
3e4150ef
VBD
318 t = in_pcblookup_local(pcbinfo, sin->sin_addr, lport,
319 wild, cred);
80dff869
JH
320 if (t && !(reuseport & t->inp_socket->so_options)) {
321#ifdef INET6
322 if (!in_nullhost(sin->sin_addr) ||
323 !in_nullhost(t->inp_laddr) ||
324 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
325#endif
984263bc
MD
326 return (EADDRINUSE);
327 }
328 }
329 inp->inp_laddr = sin->sin_addr;
330 }
331 if (lport == 0) {
332 ushort first, last;
333 int count;
334
3e4150ef
VBD
335 jsin.sin_family = AF_INET;
336 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
337 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
13d8907a
JH
338 inp->inp_laddr.s_addr = INADDR_ANY;
339 return (EINVAL);
340 }
3e4150ef
VBD
341 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
342
984263bc
MD
343 inp->inp_flags |= INP_ANONPORT;
344
345 if (inp->inp_flags & INP_HIGHPORT) {
346 first = ipport_hifirstauto; /* sysctl */
347 last = ipport_hilastauto;
348 lastport = &pcbinfo->lasthi;
349 } else if (inp->inp_flags & INP_LOWPORT) {
3e4150ef
VBD
350 if (cred &&
351 (error = suser_cred(cred, PRISON_ROOT))) {
984263bc 352 inp->inp_laddr.s_addr = INADDR_ANY;
80dff869 353 return (error);
984263bc
MD
354 }
355 first = ipport_lowfirstauto; /* 1023 */
356 last = ipport_lowlastauto; /* 600 */
357 lastport = &pcbinfo->lastlow;
358 } else {
359 first = ipport_firstauto; /* sysctl */
360 last = ipport_lastauto;
361 lastport = &pcbinfo->lastport;
362 }
363 /*
364 * Simple check to ensure all ports are not used up causing
365 * a deadlock here.
366 *
367 * We split the two cases (up and down) so that the direction
368 * is not being tested on each round of the loop.
369 */
370 if (first > last) {
371 /*
372 * counting down
373 */
374 count = first - last;
375
376 do {
377 if (count-- < 0) { /* completely used? */
378 inp->inp_laddr.s_addr = INADDR_ANY;
379 return (EADDRNOTAVAIL);
380 }
381 --*lastport;
382 if (*lastport > first || *lastport < last)
383 *lastport = first;
384 lport = htons(*lastport);
3e4150ef
VBD
385 } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
386 lport, wild, cred));
984263bc
MD
387 } else {
388 /*
389 * counting up
390 */
391 count = last - first;
392
393 do {
394 if (count-- < 0) { /* completely used? */
395 inp->inp_laddr.s_addr = INADDR_ANY;
396 return (EADDRNOTAVAIL);
397 }
398 ++*lastport;
399 if (*lastport < first || *lastport > last)
400 *lastport = first;
401 lport = htons(*lastport);
3e4150ef
VBD
402 } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
403 lport, wild, cred));
984263bc
MD
404 }
405 }
406 inp->inp_lport = lport;
3e4150ef
VBD
407
408 jsin.sin_family = AF_INET;
409 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
410 if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin)) {
984263bc
MD
411 inp->inp_laddr.s_addr = INADDR_ANY;
412 inp->inp_lport = 0;
413 return (EINVAL);
414 }
3e4150ef
VBD
415 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
416
13d8907a 417 if (in_pcbinsporthash(inp) != 0) {
984263bc
MD
418 inp->inp_laddr.s_addr = INADDR_ANY;
419 inp->inp_lport = 0;
420 return (EAGAIN);
421 }
422 return (0);
423}
424
425/*
426 * Transform old in_pcbconnect() into an inner subroutine for new
427 * in_pcbconnect(): Do some validity-checking on the remote
428 * address (in mbuf 'nam') and then determine local host address
429 * (i.e., which interface) to use to access that remote host.
430 *
431 * This preserves definition of in_pcbconnect(), while supporting a
432 * slightly different version for T/TCP. (This is more than
433 * a bit of a kludge, but cleaning up the internal interfaces would
434 * have forced minor changes in every protocol).
435 */
984263bc 436int
3e4150ef
VBD
437in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
438 struct sockaddr_in **plocal_sin, struct thread *td)
984263bc
MD
439{
440 struct in_ifaddr *ia;
3e4150ef 441 struct ucred *cred = NULL;
2256ba69 442 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
bd544276 443 struct sockaddr *jsin;
9b37b73b 444 int jailed = 0, alloc_route = 0;
984263bc 445
80dff869 446 if (nam->sa_len != sizeof *sin)
984263bc
MD
447 return (EINVAL);
448 if (sin->sin_family != AF_INET)
449 return (EAFNOSUPPORT);
450 if (sin->sin_port == 0)
451 return (EADDRNOTAVAIL);
3e4150ef
VBD
452 if (td && td->td_proc && td->td_proc->p_ucred)
453 cred = td->td_proc->p_ucred;
454 if (cred && cred->cr_prison)
455 jailed = 1;
1b562c24
SZ
456 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
457 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
984263bc
MD
458 /*
459 * If the destination address is INADDR_ANY,
460 * use the primary local address.
461 * If the supplied address is INADDR_BROADCAST,
462 * and the primary interface supports broadcast,
463 * choose the broadcast address for that interface.
464 */
465 if (sin->sin_addr.s_addr == INADDR_ANY)
80dff869 466 sin->sin_addr = IA_SIN(ia)->sin_addr;
984263bc 467 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
80dff869
JH
468 (ia->ia_ifp->if_flags & IFF_BROADCAST))
469 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
984263bc
MD
470 }
471 if (inp->inp_laddr.s_addr == INADDR_ANY) {
2256ba69 472 struct route *ro;
984263bc 473
80dff869 474 ia = (struct in_ifaddr *)NULL;
984263bc
MD
475 /*
476 * If route is known or can be allocated now,
477 * our src addr is taken from the i/f, else punt.
478 * Note that we should check the address family of the cached
479 * destination, in case of sharing the cache with IPv6.
480 */
481 ro = &inp->inp_route;
80dff869
JH
482 if (ro->ro_rt &&
483 (!(ro->ro_rt->rt_flags & RTF_UP) ||
484 ro->ro_dst.sa_family != AF_INET ||
485 satosin(&ro->ro_dst)->sin_addr.s_addr !=
f23061d4 486 sin->sin_addr.s_addr ||
80dff869 487 inp->inp_socket->so_options & SO_DONTROUTE)) {
984263bc 488 RTFREE(ro->ro_rt);
80dff869 489 ro->ro_rt = (struct rtentry *)NULL;
984263bc 490 }
80dff869
JH
491 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
492 (ro->ro_rt == (struct rtentry *)NULL ||
493 ro->ro_rt->rt_ifp == (struct ifnet *)NULL)) {
984263bc
MD
494 /* No route yet, so try to acquire one */
495 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
496 ro->ro_dst.sa_family = AF_INET;
497 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
498 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
499 sin->sin_addr;
500 rtalloc(ro);
9b37b73b 501 alloc_route = 1;
984263bc
MD
502 }
503 /*
504 * If we found a route, use the address
505 * corresponding to the outgoing interface
506 * unless it is the loopback (in case a route
507 * to our address on another net goes to loopback).
508 */
3e4150ef
VBD
509 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
510 if (jailed) {
511 if (jailed_ip(cred->cr_prison,
512 ro->ro_rt->rt_ifa->ifa_addr)) {
513 ia = ifatoia(ro->ro_rt->rt_ifa);
514 }
515 } else {
516 ia = ifatoia(ro->ro_rt->rt_ifa);
517 }
518 }
80dff869 519 if (ia == NULL) {
984263bc
MD
520 u_short fport = sin->sin_port;
521
522 sin->sin_port = 0;
523 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
3e4150ef
VBD
524 if (ia && jailed && !jailed_ip(cred->cr_prison,
525 sintosa(&ia->ia_addr)))
526 ia = NULL;
80dff869 527 if (ia == NULL)
984263bc 528 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
3e4150ef
VBD
529 if (ia && jailed && !jailed_ip(cred->cr_prison,
530 sintosa(&ia->ia_addr)))
531 ia = NULL;
984263bc 532 sin->sin_port = fport;
1b562c24
SZ
533 if (ia == NULL &&
534 !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
535 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
3e4150ef
VBD
536 if (ia && jailed && !jailed_ip(cred->cr_prison,
537 sintosa(&ia->ia_addr)))
538 ia = NULL;
539
540 if (!jailed && ia == NULL)
9b37b73b 541 goto fail;
984263bc
MD
542 }
543 /*
544 * If the destination address is multicast and an outgoing
545 * interface has been set as a multicast option, use the
546 * address of that interface as our source address.
547 */
3e4150ef 548 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
984263bc
MD
549 inp->inp_moptions != NULL) {
550 struct ip_moptions *imo;
551 struct ifnet *ifp;
552
553 imo = inp->inp_moptions;
554 if (imo->imo_multicast_ifp != NULL) {
1b562c24
SZ
555 struct in_ifaddr_container *iac;
556
984263bc 557 ifp = imo->imo_multicast_ifp;
1b562c24
SZ
558 ia = NULL;
559 TAILQ_FOREACH(iac,
560 &in_ifaddrheads[mycpuid], ia_link) {
561 if (iac->ia->ia_ifp == ifp) {
562 ia = iac->ia;
984263bc 563 break;
1b562c24
SZ
564 }
565 }
80dff869 566 if (ia == NULL)
9b37b73b 567 goto fail;
984263bc
MD
568 }
569 }
80dff869
JH
570 /*
571 * Don't do pcblookup call here; return interface in plocal_sin
572 * and exit to caller, that will do the lookup.
573 */
bd544276
VBD
574 if (ia == NULL && jailed) {
575 if ((jsin = prison_get_nonlocal(cred->cr_prison, AF_INET, NULL)) != NULL ||
9b37b73b 576 (jsin = prison_get_local(cred->cr_prison, AF_INET, NULL)) != NULL) {
bd544276 577 *plocal_sin = satosin(jsin);
9b37b73b 578 } else {
bd544276 579 /* IPv6 only Jail */
9b37b73b
SZ
580 goto fail;
581 }
3e4150ef
VBD
582 } else {
583 *plocal_sin = &ia->ia_addr;
584 }
984263bc 585 }
80dff869 586 return (0);
9b37b73b
SZ
587fail:
588 if (alloc_route) {
589 struct route *ro = &inp->inp_route;
590
591 if (ro->ro_rt != NULL)
592 RTFREE(ro->ro_rt);
593 bzero(ro, sizeof(*ro));
594 }
595 return (EADDRNOTAVAIL);
984263bc
MD
596}
597
598/*
599 * Outer subroutine:
600 * Connect from a socket to a specified address.
601 * Both address and port must be specified in argument sin.
602 * If don't have a local address for this socket yet,
603 * then pick one.
604 */
605int
dadab5e9 606in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
984263bc 607{
13d8907a 608 struct sockaddr_in *if_sin;
984263bc 609 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
984263bc
MD
610 int error;
611
80dff869 612 /* Call inner routine to assign local interface address. */
3e4150ef 613 if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
80dff869 614 return (error);
984263bc 615
7fe56515 616 if (in_pcblookup_hash(inp->inp_cpcbinfo, sin->sin_addr, sin->sin_port,
13d8907a 617 inp->inp_laddr.s_addr ? inp->inp_laddr : if_sin->sin_addr,
80dff869 618 inp->inp_lport, FALSE, NULL) != NULL) {
984263bc
MD
619 return (EADDRINUSE);
620 }
621 if (inp->inp_laddr.s_addr == INADDR_ANY) {
622 if (inp->inp_lport == 0) {
13d8907a 623 error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
984263bc 624 if (error)
80dff869 625 return (error);
984263bc 626 }
13d8907a 627 inp->inp_laddr = if_sin->sin_addr;
984263bc
MD
628 }
629 inp->inp_faddr = sin->sin_addr;
630 inp->inp_fport = sin->sin_port;
2322f6ca 631 in_pcbinsconnhash(inp);
984263bc
MD
632 return (0);
633}
634
635void
f3f70f0d 636in_pcbdisconnect(struct inpcb *inp)
984263bc
MD
637{
638
639 inp->inp_faddr.s_addr = INADDR_ANY;
640 inp->inp_fport = 0;
13d8907a 641 in_pcbremconnhash(inp);
984263bc
MD
642 if (inp->inp_socket->so_state & SS_NOFDREF)
643 in_pcbdetach(inp);
644}
645
646void
f3f70f0d 647in_pcbdetach(struct inpcb *inp)
984263bc
MD
648{
649 struct socket *so = inp->inp_socket;
650 struct inpcbinfo *ipi = inp->inp_pcbinfo;
651
652#ifdef IPSEC
653 ipsec4_delete_pcbpolicy(inp);
654#endif /*IPSEC*/
655 inp->inp_gencnt = ++ipi->ipi_gencnt;
656 in_pcbremlists(inp);
657 so->so_pcb = 0;
658 sofree(so);
659 if (inp->inp_options)
f23061d4 660 m_free(inp->inp_options);
984263bc
MD
661 if (inp->inp_route.ro_rt)
662 rtfree(inp->inp_route.ro_rt);
663 ip_freemoptions(inp->inp_moptions);
664 inp->inp_vflag = 0;
8a8d5d85 665 zfree(ipi->ipi_zone, inp);
984263bc
MD
666}
667
668/*
669 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
670 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
671 * in struct pr_usrreqs, so that protocols can just reference then directly
672 * without the need for a wrapper function. The socket must have a valid
673 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
674 * except through a kernel programming error, so it is acceptable to panic
675 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
676 * because there actually /is/ a programming error somewhere... XXX)
677 */
678int
f3f70f0d 679in_setsockaddr(struct socket *so, struct sockaddr **nam)
984263bc 680{
2256ba69
RG
681 struct inpcb *inp;
682 struct sockaddr_in *sin;
984263bc
MD
683
684 /*
685 * Do the malloc first in case it blocks.
686 */
687 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
688 M_WAITOK | M_ZERO);
689 sin->sin_family = AF_INET;
80dff869 690 sin->sin_len = sizeof *sin;
984263bc 691
1cae611f 692 crit_enter();
ed894f8c 693 inp = so->so_pcb;
984263bc 694 if (!inp) {
1cae611f 695 crit_exit();
efda3bd0 696 kfree(sin, M_SONAME);
80dff869 697 return (ECONNRESET);
984263bc
MD
698 }
699 sin->sin_port = inp->inp_lport;
700 sin->sin_addr = inp->inp_laddr;
1cae611f 701 crit_exit();
984263bc
MD
702
703 *nam = (struct sockaddr *)sin;
80dff869 704 return (0);
984263bc
MD
705}
706
707int
f3f70f0d 708in_setpeeraddr(struct socket *so, struct sockaddr **nam)
984263bc 709{
984263bc 710 struct inpcb *inp;
2256ba69 711 struct sockaddr_in *sin;
984263bc
MD
712
713 /*
714 * Do the malloc first in case it blocks.
715 */
716 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
717 M_WAITOK | M_ZERO);
718 sin->sin_family = AF_INET;
80dff869 719 sin->sin_len = sizeof *sin;
984263bc 720
1cae611f 721 crit_enter();
ed894f8c 722 inp = so->so_pcb;
984263bc 723 if (!inp) {
1cae611f 724 crit_exit();
efda3bd0 725 kfree(sin, M_SONAME);
80dff869 726 return (ECONNRESET);
984263bc
MD
727 }
728 sin->sin_port = inp->inp_fport;
729 sin->sin_addr = inp->inp_faddr;
1cae611f 730 crit_exit();
984263bc
MD
731
732 *nam = (struct sockaddr *)sin;
80dff869 733 return (0);
984263bc
MD
734}
735
736void
71f385dc 737in_pcbnotifyall(struct inpcbhead *head, struct in_addr faddr, int err,
f3f70f0d 738 void (*notify)(struct inpcb *, int))
984263bc
MD
739{
740 struct inpcb *inp, *ninp;
984263bc 741
d2e9e54c
MD
742 /*
743 * note: if INP_PLACEMARKER is set we must ignore the rest of
744 * the structure and skip it.
745 */
1cae611f 746 crit_enter();
0c3c561c 747 LIST_FOREACH_MUTABLE(inp, head, inp_list, ninp) {
d2e9e54c
MD
748 if (inp->inp_flags & INP_PLACEMARKER)
749 continue;
984263bc 750#ifdef INET6
80dff869 751 if (!(inp->inp_vflag & INP_IPV4))
984263bc
MD
752 continue;
753#endif
754 if (inp->inp_faddr.s_addr != faddr.s_addr ||
755 inp->inp_socket == NULL)
80dff869 756 continue;
71f385dc 757 (*notify)(inp, err); /* can remove inp from list! */
984263bc 758 }
1cae611f 759 crit_exit();
984263bc
MD
760}
761
762void
f3f70f0d 763in_pcbpurgeif0(struct inpcb *head, struct ifnet *ifp)
984263bc
MD
764{
765 struct inpcb *inp;
766 struct ip_moptions *imo;
767 int i, gap;
768
769 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
d2e9e54c
MD
770 if (inp->inp_flags & INP_PLACEMARKER)
771 continue;
984263bc 772 imo = inp->inp_moptions;
80dff869 773 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
984263bc
MD
774 /*
775 * Unselect the outgoing interface if it is being
776 * detached.
777 */
778 if (imo->imo_multicast_ifp == ifp)
779 imo->imo_multicast_ifp = NULL;
780
781 /*
782 * Drop multicast group membership if we joined
783 * through the interface being detached.
784 */
785 for (i = 0, gap = 0; i < imo->imo_num_memberships;
786 i++) {
787 if (imo->imo_membership[i]->inm_ifp == ifp) {
788 in_delmulti(imo->imo_membership[i]);
789 gap++;
790 } else if (gap != 0)
791 imo->imo_membership[i - gap] =
792 imo->imo_membership[i];
793 }
794 imo->imo_num_memberships -= gap;
795 }
796 }
797}
798
799/*
800 * Check for alternatives when higher level complains
801 * about service problems. For now, invalidate cached
802 * routing information. If the route was created dynamically
803 * (by a redirect), time to try a default gateway again.
804 */
805void
be23faf1 806in_losing(struct inpcb *inp)
984263bc 807{
2256ba69 808 struct rtentry *rt;
be23faf1 809 struct rt_addrinfo rtinfo;
984263bc
MD
810
811 if ((rt = inp->inp_route.ro_rt)) {
be23faf1
JH
812 bzero(&rtinfo, sizeof(struct rt_addrinfo));
813 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
814 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
815 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
816 rtinfo.rti_flags = rt->rt_flags;
817 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
984263bc 818 if (rt->rt_flags & RTF_DYNAMIC)
ecdefdda 819 rtrequest1_global(RTM_DELETE, &rtinfo, NULL, NULL);
984263bc
MD
820 inp->inp_route.ro_rt = NULL;
821 rtfree(rt);
822 /*
823 * A new route can be allocated
824 * the next time output is attempted.
825 */
826 }
827}
828
829/*
830 * After a routing change, flush old routing
831 * and allocate a (hopefully) better one.
832 */
833void
71f385dc 834in_rtchange(struct inpcb *inp, int err)
984263bc
MD
835{
836 if (inp->inp_route.ro_rt) {
837 rtfree(inp->inp_route.ro_rt);
be23faf1 838 inp->inp_route.ro_rt = NULL;
984263bc
MD
839 /*
840 * A new route can be allocated the next time
841 * output is attempted.
842 */
843 }
844}
845
846/*
847 * Lookup a PCB based on the local address and port.
848 */
849struct inpcb *
3e4150ef
VBD
850in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
851 u_int lport_arg, int wild_okay, struct ucred *cred)
984263bc 852{
2256ba69 853 struct inpcb *inp;
984263bc
MD
854 int matchwild = 3, wildcard;
855 u_short lport = lport_arg;
856
13d8907a
JH
857 struct inpcbporthead *porthash;
858 struct inpcbport *phd;
859 struct inpcb *match = NULL;
860
861 /*
862 * Best fit PCB lookup.
863 *
864 * First see if this local port is in use by looking on the
865 * port hash list.
866 */
867 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
868 pcbinfo->porthashmask)];
869 LIST_FOREACH(phd, porthash, phd_hash) {
870 if (phd->phd_port == lport)
871 break;
872 }
873 if (phd != NULL) {
984263bc 874 /*
13d8907a
JH
875 * Port is in use by one or more PCBs. Look for best
876 * fit.
984263bc 877 */
13d8907a
JH
878 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
879 wildcard = 0;
984263bc
MD
880#ifdef INET6
881 if ((inp->inp_vflag & INP_IPV4) == 0)
882 continue;
883#endif
13d8907a
JH
884 if (inp->inp_faddr.s_addr != INADDR_ANY)
885 wildcard++;
886 if (inp->inp_laddr.s_addr != INADDR_ANY) {
887 if (laddr.s_addr == INADDR_ANY)
888 wildcard++;
889 else if (inp->inp_laddr.s_addr != laddr.s_addr)
984263bc 890 continue;
13d8907a
JH
891 } else {
892 if (laddr.s_addr != INADDR_ANY)
984263bc 893 wildcard++;
13d8907a
JH
894 }
895 if (wildcard && !wild_okay)
896 continue;
3e4150ef
VBD
897 if (wildcard < matchwild &&
898 (cred == NULL ||
899 cred->cr_prison ==
900 inp->inp_socket->so_cred->cr_prison)) {
13d8907a
JH
901 match = inp;
902 matchwild = wildcard;
903 if (matchwild == 0) {
904 break;
984263bc
MD
905 }
906 }
907 }
984263bc 908 }
13d8907a 909 return (match);
984263bc
MD
910}
911
912/*
913 * Lookup PCB in hash list.
914 */
915struct inpcb *
f3f70f0d
SW
916in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport_arg,
917 struct in_addr laddr, u_int lport_arg, boolean_t wildcard,
918 struct ifnet *ifp)
984263bc
MD
919{
920 struct inpcbhead *head;
3e4150ef 921 struct inpcb *inp, *jinp=NULL;
984263bc
MD
922 u_short fport = fport_arg, lport = lport_arg;
923
924 /*
925 * First look for an exact match.
926 */
13d8907a
JH
927 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
928 laddr.s_addr, lport, pcbinfo->hashmask)];
984263bc
MD
929 LIST_FOREACH(inp, head, inp_hash) {
930#ifdef INET6
80dff869 931 if (!(inp->inp_vflag & INP_IPV4))
984263bc
MD
932 continue;
933#endif
80dff869
JH
934 if (in_hosteq(inp->inp_faddr, faddr) &&
935 in_hosteq(inp->inp_laddr, laddr) &&
936 inp->inp_fport == fport && inp->inp_lport == lport) {
f23061d4 937 /* found */
3e4150ef
VBD
938 if (inp->inp_socket == NULL ||
939 inp->inp_socket->so_cred->cr_prison == NULL) {
940 return (inp);
941 } else {
942 if (jinp == NULL)
943 jinp = inp;
944 }
984263bc
MD
945 }
946 }
3e4150ef
VBD
947 if (jinp != NULL)
948 return (jinp);
984263bc
MD
949 if (wildcard) {
950 struct inpcb *local_wild = NULL;
3e4150ef 951 struct inpcb *jinp_wild = NULL;
80dff869 952#ifdef INET6
984263bc 953 struct inpcb *local_wild_mapped = NULL;
80dff869 954#endif
ef962575
JH
955 struct inpcontainer *ic;
956 struct inpcontainerhead *chead;
3e4150ef
VBD
957 struct sockaddr_in jsin;
958 struct ucred *cred;
984263bc 959
3e4150ef
VBD
960 /*
961 * Order of socket selection:
962 * 1. non-jailed, non-wild.
963 * 2. non-jailed, wild.
964 * 3. jailed, non-wild.
965 * 4. jailed, wild.
966 */
967 jsin.sin_family = AF_INET;
ef962575
JH
968 chead = &pcbinfo->wildcardhashbase[
969 INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
970 LIST_FOREACH(ic, chead, ic_list) {
971 inp = ic->ic_inp;
3e4150ef 972 jsin.sin_addr.s_addr = laddr.s_addr;
984263bc 973#ifdef INET6
80dff869 974 if (!(inp->inp_vflag & INP_IPV4))
984263bc
MD
975 continue;
976#endif
3e4150ef
VBD
977 if (inp->inp_socket != NULL)
978 cred = inp->inp_socket->so_cred;
979 else
980 cred = NULL;
981 if (cred != NULL && jailed(cred)) {
982 if (jinp != NULL)
983 continue;
984 else
985 if (!jailed_ip(cred->cr_prison,
986 (struct sockaddr *)&jsin))
987 continue;
988 }
13d8907a 989 if (inp->inp_lport == lport) {
984263bc 990 if (ifp && ifp->if_type == IFT_FAITH &&
80dff869 991 !(inp->inp_flags & INP_FAITH))
984263bc 992 continue;
3e4150ef
VBD
993 if (inp->inp_laddr.s_addr == laddr.s_addr) {
994 if (cred != NULL && jailed(cred))
995 jinp = inp;
996 else
997 return (inp);
998 }
13d8907a 999 if (inp->inp_laddr.s_addr == INADDR_ANY) {
80dff869 1000#ifdef INET6
984263bc
MD
1001 if (INP_CHECK_SOCKAF(inp->inp_socket,
1002 AF_INET6))
1003 local_wild_mapped = inp;
1004 else
80dff869 1005#endif
3e4150ef
VBD
1006 if (cred != NULL &&
1007 jailed(cred))
1008 jinp_wild = inp;
1009 else
1010 local_wild = inp;
984263bc
MD
1011 }
1012 }
1013 }
3e4150ef
VBD
1014 if (local_wild != NULL)
1015 return (local_wild);
80dff869 1016#ifdef INET6
3e4150ef 1017 if (local_wild_mapped != NULL)
984263bc 1018 return (local_wild_mapped);
80dff869 1019#endif
3e4150ef
VBD
1020 if (jinp != NULL)
1021 return (jinp);
1022 return (jinp_wild);
984263bc
MD
1023 }
1024
1025 /*
1026 * Not found.
1027 */
1028 return (NULL);
1029}
1030
1031/*
13d8907a 1032 * Insert PCB into connection hash table.
984263bc 1033 */
13d8907a
JH
1034void
1035in_pcbinsconnhash(struct inpcb *inp)
984263bc 1036{
7fe56515 1037 struct inpcbinfo *pcbinfo = inp->inp_cpcbinfo;
13d8907a
JH
1038 struct inpcbhead *bucket;
1039 u_int32_t hashkey_faddr, hashkey_laddr;
984263bc
MD
1040
1041#ifdef INET6
13d8907a
JH
1042 if (inp->inp_vflag & INP_IPV6) {
1043 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1044 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1045 } else {
80dff869
JH
1046#endif
1047 hashkey_faddr = inp->inp_faddr.s_addr;
13d8907a
JH
1048 hashkey_laddr = inp->inp_laddr.s_addr;
1049#ifdef INET6
1050 }
1051#endif
984263bc 1052
8affadf8 1053 KASSERT(!(inp->inp_flags & INP_CONNECTED), ("already on hash list"));
13d8907a
JH
1054 inp->inp_flags |= INP_CONNECTED;
1055
1056 /*
1057 * Insert into the connection hash table.
1058 */
1059 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
d24ce1dc 1060 inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
13d8907a
JH
1061 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1062}
1063
1064/*
1065 * Remove PCB from connection hash table.
1066 */
1067void
1068in_pcbremconnhash(struct inpcb *inp)
1069{
1070 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1071 LIST_REMOVE(inp, inp_hash);
1072 inp->inp_flags &= ~INP_CONNECTED;
1073}
1074
1075/*
1076 * Insert PCB into port hash table.
1077 */
1078int
1079in_pcbinsporthash(struct inpcb *inp)
1080{
1081 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1082 struct inpcbporthead *pcbporthash;
1083 struct inpcbport *phd;
1084
1085 /*
1086 * Insert into the port hash table.
1087 */
1088 pcbporthash = &pcbinfo->porthashbase[
1089 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->porthashmask)];
984263bc 1090
80dff869
JH
1091 /* Go through port list and look for a head for this lport. */
1092 LIST_FOREACH(phd, pcbporthash, phd_hash)
984263bc
MD
1093 if (phd->phd_port == inp->inp_lport)
1094 break;
80dff869
JH
1095
1096 /* If none exists, malloc one and tack it on. */
984263bc 1097 if (phd == NULL) {
80dff869 1098 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport),
ba4e3dbe 1099 M_PCB, M_INTWAIT | M_NULLOK);
80dff869 1100 if (phd == NULL)
984263bc 1101 return (ENOBUFS); /* XXX */
984263bc
MD
1102 phd->phd_port = inp->inp_lport;
1103 LIST_INIT(&phd->phd_pcblist);
1104 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1105 }
80dff869 1106
984263bc
MD
1107 inp->inp_phd = phd;
1108 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
13d8907a 1109
984263bc
MD
1110 return (0);
1111}
1112
984263bc 1113void
8affadf8 1114in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
984263bc 1115{
ef962575
JH
1116 struct inpcontainer *ic;
1117 struct inpcontainerhead *bucket;
984263bc 1118
2322f6ca
JH
1119 bucket = &pcbinfo->wildcardhashbase[
1120 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
984263bc 1121
efda3bd0 1122 ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
ef962575
JH
1123 ic->ic_inp = inp;
1124 LIST_INSERT_HEAD(bucket, ic, ic_list);
13d8907a 1125}
984263bc 1126
13d8907a 1127/*
8affadf8 1128 * Insert PCB into wildcard hash table.
13d8907a
JH
1129 */
1130void
8affadf8 1131in_pcbinswildcardhash(struct inpcb *inp)
13d8907a 1132{
ef962575 1133 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
a219bfbc
HP
1134
1135 KKASSERT(pcbinfo != NULL);
ef962575 1136
8affadf8
JH
1137 in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1138 inp->inp_flags |= INP_WILDCARD;
1139}
1140
1141void
1142in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1143{
1144 struct inpcontainer *ic;
1145 struct inpcontainerhead *head;
13d8907a 1146
ef962575
JH
1147 /* find bucket */
1148 head = &pcbinfo->wildcardhashbase[
1149 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
8affadf8 1150
ef962575
JH
1151 LIST_FOREACH(ic, head, ic_list) {
1152 if (ic->ic_inp == inp)
1153 goto found;
13d8907a 1154 }
ef962575
JH
1155 return; /* not found! */
1156
1157found:
8affadf8 1158 LIST_REMOVE(ic, ic_list); /* remove container from bucket chain */
efda3bd0 1159 kfree(ic, M_TEMP); /* deallocate container */
8affadf8 1160}
ef962575 1161
8affadf8
JH
1162/*
1163 * Remove PCB from wildcard hash table.
1164 */
1165void
1166in_pcbremwildcardhash(struct inpcb *inp)
1167{
1168 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
ef962575 1169
8affadf8
JH
1170 KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
1171 in_pcbremwildcardhash_oncpu(inp, pcbinfo);
ef962575 1172 inp->inp_flags &= ~INP_WILDCARD;
13d8907a
JH
1173}
1174
984263bc
MD
1175/*
1176 * Remove PCB from various lists.
1177 */
1178void
f3f70f0d 1179in_pcbremlists(struct inpcb *inp)
984263bc 1180{
984263bc
MD
1181 if (inp->inp_lport) {
1182 struct inpcbport *phd = inp->inp_phd;
1183
984263bc
MD
1184 LIST_REMOVE(inp, inp_portlist);
1185 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1186 LIST_REMOVE(phd, phd_hash);
efda3bd0 1187 kfree(phd, M_PCB);
984263bc
MD
1188 }
1189 }
ef962575
JH
1190 if (inp->inp_flags & INP_WILDCARD) {
1191 in_pcbremwildcardhash(inp);
1192 } else if (inp->inp_flags & INP_CONNECTED) {
1193 in_pcbremconnhash(inp);
1194 }
984263bc
MD
1195 LIST_REMOVE(inp, inp_list);
1196 inp->inp_pcbinfo->ipi_count--;
1197}
1198
1199int
dadab5e9 1200prison_xinpcb(struct thread *td, struct inpcb *inp)
984263bc 1201{
dadab5e9
MD
1202 struct ucred *cr;
1203
1204 if (td->td_proc == NULL)
1205 return (0);
1206 cr = td->td_proc->p_ucred;
1207 if (cr->cr_prison == NULL)
984263bc 1208 return (0);
3e4150ef
VBD
1209 if (inp->inp_socket && inp->inp_socket->so_cred &&
1210 inp->inp_socket->so_cred->cr_prison &&
1211 cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
984263bc
MD
1212 return (0);
1213 return (1);
1214}
48a2763f
JS
1215
1216int
1217in_pcblist_global(SYSCTL_HANDLER_ARGS)
1218{
1219 struct inpcbinfo *pcbinfo = arg1;
1220 struct inpcb *inp, *marker;
1221 struct xinpcb xi;
48a2763f
JS
1222 int error, i, n;
1223 inp_gen_t gencnt;
1224
1225 /*
1226 * The process of preparing the TCB list is too time-consuming and
1227 * resource-intensive to repeat twice on every request.
1228 */
7e7850e7 1229 if (req->oldptr == NULL) {
48a2763f 1230 n = pcbinfo->ipi_count;
8d7c364e 1231 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
48a2763f
JS
1232 return 0;
1233 }
1234
7e7850e7 1235 if (req->newptr != NULL)
48a2763f
JS
1236 return EPERM;
1237
1238 /*
7e7850e7
MD
1239 * OK, now we're committed to doing something. Re-fetch ipi_count
1240 * after obtaining the generation count.
48a2763f
JS
1241 */
1242 gencnt = pcbinfo->ipi_gencnt;
1243 n = pcbinfo->ipi_count;
1244
efda3bd0 1245 marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
48a2763f 1246 marker->inp_flags |= INP_PLACEMARKER;
48a2763f 1247 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
7e7850e7 1248
48a2763f 1249 i = 0;
7e7850e7
MD
1250 error = 0;
1251
48a2763f
JS
1252 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
1253 LIST_REMOVE(marker, inp_list);
1254 LIST_INSERT_AFTER(inp, marker, inp_list);
1255
1256 if (inp->inp_flags & INP_PLACEMARKER)
1257 continue;
1258 if (inp->inp_gencnt > gencnt)
1259 continue;
1260 if (prison_xinpcb(req->td, inp))
1261 continue;
8573f096 1262 bzero(&xi, sizeof xi);
48a2763f
JS
1263 xi.xi_len = sizeof xi;
1264 bcopy(inp, &xi.xi_inp, sizeof *inp);
1265 if (inp->inp_socket)
1266 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1267 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
1268 break;
1269 ++i;
1270 }
1271 LIST_REMOVE(marker, inp_list);
1272 if (error == 0 && i < n) {
0c3c561c
JH
1273 bzero(&xi, sizeof xi);
1274 xi.xi_len = sizeof xi;
48a2763f 1275 while (i < n) {
f23061d4 1276 error = SYSCTL_OUT(req, &xi, sizeof xi);
48a2763f
JS
1277 ++i;
1278 }
1279 }
efda3bd0 1280 kfree(marker, M_TEMP);
48a2763f
JS
1281 return(error);
1282}