Deleted Added
full compact
in_pcb.c (83366) in_pcb.c (83934)
1/*
2 * Copyright (c) 1982, 1986, 1991, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
1/*
2 * Copyright (c) 1982, 1986, 1991, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
34 * $FreeBSD: head/sys/netinet/in_pcb.c 83366 2001-09-12 08:38:13Z julian $
34 * $FreeBSD: head/sys/netinet/in_pcb.c 83934 2001-09-25 18:40:52Z brooks $
35 */
36
37#include "opt_ipsec.h"
38#include "opt_inet6.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/domain.h>
45#include <sys/protosw.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
48#include <sys/proc.h>
49#include <sys/jail.h>
50#include <sys/kernel.h>
51#include <sys/sysctl.h>
52
53#include <machine/limits.h>
54
55#include <vm/vm_zone.h>
56
57#include <net/if.h>
58#include <net/if_types.h>
59#include <net/route.h>
60
61#include <netinet/in.h>
62#include <netinet/in_pcb.h>
63#include <netinet/in_var.h>
64#include <netinet/ip_var.h>
65#ifdef INET6
66#include <netinet/ip6.h>
67#include <netinet6/ip6_var.h>
68#endif /* INET6 */
69
35 */
36
37#include "opt_ipsec.h"
38#include "opt_inet6.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/domain.h>
45#include <sys/protosw.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
48#include <sys/proc.h>
49#include <sys/jail.h>
50#include <sys/kernel.h>
51#include <sys/sysctl.h>
52
53#include <machine/limits.h>
54
55#include <vm/vm_zone.h>
56
57#include <net/if.h>
58#include <net/if_types.h>
59#include <net/route.h>
60
61#include <netinet/in.h>
62#include <netinet/in_pcb.h>
63#include <netinet/in_var.h>
64#include <netinet/ip_var.h>
65#ifdef INET6
66#include <netinet/ip6.h>
67#include <netinet6/ip6_var.h>
68#endif /* INET6 */
69
70#include "faith.h"
71
72#ifdef IPSEC
73#include <netinet6/ipsec.h>
74#include <netkey/key.h>
75#endif /* IPSEC */
76
77struct in_addr zeroin_addr;
78
79/*
80 * These configure the range of local port addresses assigned to
81 * "unspecified" outgoing connections/packets/whatever.
82 */
83int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
84int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
85int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
86int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
87int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
88int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
89
90#define RANGECHK(var, min, max) \
91 if ((var) < (min)) { (var) = (min); } \
92 else if ((var) > (max)) { (var) = (max); }
93
94static int
95sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
96{
97 int error = sysctl_handle_int(oidp,
98 oidp->oid_arg1, oidp->oid_arg2, req);
99 if (!error) {
100 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
101 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
102 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
103 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
104 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
105 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
106 }
107 return error;
108}
109
110#undef RANGECHK
111
112SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
113
114SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
115 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
116SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
117 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
118SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
119 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
120SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
121 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
122SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
123 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
124SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
125 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
126
127/*
128 * in_pcb.c: manage the Protocol Control Blocks.
129 *
130 * NOTE: It is assumed that most of these functions will be called at
131 * splnet(). XXX - There are, unfortunately, a few exceptions to this
132 * rule that should be fixed.
133 */
134
135/*
136 * Allocate a PCB and associate it with the socket.
137 */
138int
139in_pcballoc(so, pcbinfo, td)
140 struct socket *so;
141 struct inpcbinfo *pcbinfo;
142 struct thread *td;
143{
144 register struct inpcb *inp;
145#ifdef IPSEC
146 int error;
147#endif
148
149 inp = zalloc(pcbinfo->ipi_zone);
150 if (inp == NULL)
151 return (ENOBUFS);
152 bzero((caddr_t)inp, sizeof(*inp));
153 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
154 inp->inp_pcbinfo = pcbinfo;
155 inp->inp_socket = so;
156#ifdef IPSEC
157 error = ipsec_init_policy(so, &inp->inp_sp);
158 if (error != 0) {
159 zfree(pcbinfo->ipi_zone, inp);
160 return error;
161 }
162#endif /*IPSEC*/
163#if defined(INET6)
164 if (INP_SOCKAF(so) == AF_INET6 && !ip6_mapped_addr_on)
165 inp->inp_flags |= IN6P_IPV6_V6ONLY;
166#endif
167 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
168 pcbinfo->ipi_count++;
169 so->so_pcb = (caddr_t)inp;
170#ifdef INET6
171 if (ip6_auto_flowlabel)
172 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
173#endif
174 return (0);
175}
176
177int
178in_pcbbind(inp, nam, td)
179 register struct inpcb *inp;
180 struct sockaddr *nam;
181 struct thread *td;
182{
183 struct proc *p = td->td_proc;
184 register struct socket *so = inp->inp_socket;
185 unsigned short *lastport;
186 struct sockaddr_in *sin;
187 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
188 u_short lport = 0;
189 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
190 int error, prison = 0;
191
192 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
193 return (EADDRNOTAVAIL);
194 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
195 return (EINVAL);
196 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
197 wild = 1;
198 if (nam) {
199 sin = (struct sockaddr_in *)nam;
200 if (nam->sa_len != sizeof (*sin))
201 return (EINVAL);
202#ifdef notdef
203 /*
204 * We should check the family, but old programs
205 * incorrectly fail to initialize it.
206 */
207 if (sin->sin_family != AF_INET)
208 return (EAFNOSUPPORT);
209#endif
210 if (sin->sin_addr.s_addr != INADDR_ANY)
211 if (prison_ip(p->p_ucred, 0, &sin->sin_addr.s_addr))
212 return(EINVAL);
213 lport = sin->sin_port;
214 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
215 /*
216 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
217 * allow complete duplication of binding if
218 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
219 * and a multicast address is bound on both
220 * new and duplicated sockets.
221 */
222 if (so->so_options & SO_REUSEADDR)
223 reuseport = SO_REUSEADDR|SO_REUSEPORT;
224 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
225 sin->sin_port = 0; /* yech... */
226 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
227 return (EADDRNOTAVAIL);
228 }
229 if (lport) {
230 struct inpcb *t;
231 /* GROSS */
232 if (ntohs(lport) < IPPORT_RESERVED && p &&
233 suser_xxx(0, p, PRISON_ROOT))
234 return (EACCES);
235 if (p && jailed(p->p_ucred))
236 prison = 1;
237 if (so->so_cred->cr_uid != 0 &&
238 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
239 t = in_pcblookup_local(inp->inp_pcbinfo,
240 sin->sin_addr, lport,
241 prison ? 0 : INPLOOKUP_WILDCARD);
242 if (t &&
243 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
244 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
245 (t->inp_socket->so_options &
246 SO_REUSEPORT) == 0) &&
247 (so->so_cred->cr_uid !=
248 t->inp_socket->so_cred->cr_uid)) {
249#if defined(INET6)
250 if (ntohl(sin->sin_addr.s_addr) !=
251 INADDR_ANY ||
252 ntohl(t->inp_laddr.s_addr) !=
253 INADDR_ANY ||
254 INP_SOCKAF(so) ==
255 INP_SOCKAF(t->inp_socket))
256#endif /* defined(INET6) */
257 return (EADDRINUSE);
258 }
259 }
260 if (prison &&
261 prison_ip(p->p_ucred, 0, &sin->sin_addr.s_addr))
262 return (EADDRNOTAVAIL);
263 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
264 lport, prison ? 0 : wild);
265 if (t &&
266 (reuseport & t->inp_socket->so_options) == 0) {
267#if defined(INET6)
268 if (ntohl(sin->sin_addr.s_addr) !=
269 INADDR_ANY ||
270 ntohl(t->inp_laddr.s_addr) !=
271 INADDR_ANY ||
272 INP_SOCKAF(so) ==
273 INP_SOCKAF(t->inp_socket))
274#endif /* defined(INET6) */
275 return (EADDRINUSE);
276 }
277 }
278 inp->inp_laddr = sin->sin_addr;
279 }
280 if (lport == 0) {
281 ushort first, last;
282 int count;
283
284 if (inp->inp_laddr.s_addr != INADDR_ANY)
285 if (prison_ip(p->p_ucred, 0, &inp->inp_laddr.s_addr )) {
286 inp->inp_laddr.s_addr = INADDR_ANY;
287 return (EINVAL);
288 }
289 inp->inp_flags |= INP_ANONPORT;
290
291 if (inp->inp_flags & INP_HIGHPORT) {
292 first = ipport_hifirstauto; /* sysctl */
293 last = ipport_hilastauto;
294 lastport = &pcbinfo->lasthi;
295 } else if (inp->inp_flags & INP_LOWPORT) {
296 if (p && (error = suser_xxx(0, p, PRISON_ROOT))) {
297 inp->inp_laddr.s_addr = INADDR_ANY;
298 return error;
299 }
300 first = ipport_lowfirstauto; /* 1023 */
301 last = ipport_lowlastauto; /* 600 */
302 lastport = &pcbinfo->lastlow;
303 } else {
304 first = ipport_firstauto; /* sysctl */
305 last = ipport_lastauto;
306 lastport = &pcbinfo->lastport;
307 }
308 /*
309 * Simple check to ensure all ports are not used up causing
310 * a deadlock here.
311 *
312 * We split the two cases (up and down) so that the direction
313 * is not being tested on each round of the loop.
314 */
315 if (first > last) {
316 /*
317 * counting down
318 */
319 count = first - last;
320
321 do {
322 if (count-- < 0) { /* completely used? */
323 inp->inp_laddr.s_addr = INADDR_ANY;
324 return (EADDRNOTAVAIL);
325 }
326 --*lastport;
327 if (*lastport > first || *lastport < last)
328 *lastport = first;
329 lport = htons(*lastport);
330 } while (in_pcblookup_local(pcbinfo,
331 inp->inp_laddr, lport, wild));
332 } else {
333 /*
334 * counting up
335 */
336 count = last - first;
337
338 do {
339 if (count-- < 0) { /* completely used? */
340 /*
341 * Undo any address bind that may have
342 * occurred above.
343 */
344 inp->inp_laddr.s_addr = INADDR_ANY;
345 return (EADDRNOTAVAIL);
346 }
347 ++*lastport;
348 if (*lastport < first || *lastport > last)
349 *lastport = first;
350 lport = htons(*lastport);
351 } while (in_pcblookup_local(pcbinfo,
352 inp->inp_laddr, lport, wild));
353 }
354 }
355 inp->inp_lport = lport;
356 if (prison_ip(p->p_ucred, 0, &inp->inp_laddr.s_addr)) {
357 inp->inp_laddr.s_addr = INADDR_ANY;
358 inp->inp_lport = 0;
359 return(EINVAL);
360 }
361 if (in_pcbinshash(inp) != 0) {
362 inp->inp_laddr.s_addr = INADDR_ANY;
363 inp->inp_lport = 0;
364 return (EAGAIN);
365 }
366 return (0);
367}
368
369/*
370 * Transform old in_pcbconnect() into an inner subroutine for new
371 * in_pcbconnect(): Do some validity-checking on the remote
372 * address (in mbuf 'nam') and then determine local host address
373 * (i.e., which interface) to use to access that remote host.
374 *
375 * This preserves definition of in_pcbconnect(), while supporting a
376 * slightly different version for T/TCP. (This is more than
377 * a bit of a kludge, but cleaning up the internal interfaces would
378 * have forced minor changes in every protocol).
379 */
380
381int
382in_pcbladdr(inp, nam, plocal_sin)
383 register struct inpcb *inp;
384 struct sockaddr *nam;
385 struct sockaddr_in **plocal_sin;
386{
387 struct in_ifaddr *ia;
388 register struct sockaddr_in *sin = (struct sockaddr_in *)nam;
389
390 if (nam->sa_len != sizeof (*sin))
391 return (EINVAL);
392 if (sin->sin_family != AF_INET)
393 return (EAFNOSUPPORT);
394 if (sin->sin_port == 0)
395 return (EADDRNOTAVAIL);
396 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
397 /*
398 * If the destination address is INADDR_ANY,
399 * use the primary local address.
400 * If the supplied address is INADDR_BROADCAST,
401 * and the primary interface supports broadcast,
402 * choose the broadcast address for that interface.
403 */
404#define satosin(sa) ((struct sockaddr_in *)(sa))
405#define sintosa(sin) ((struct sockaddr *)(sin))
406#define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
407 if (sin->sin_addr.s_addr == INADDR_ANY)
408 sin->sin_addr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
409 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
410 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & IFF_BROADCAST))
411 sin->sin_addr = satosin(&TAILQ_FIRST(&in_ifaddrhead)->ia_broadaddr)->sin_addr;
412 }
413 if (inp->inp_laddr.s_addr == INADDR_ANY) {
414 register struct route *ro;
415
416 ia = (struct in_ifaddr *)0;
417 /*
418 * If route is known or can be allocated now,
419 * our src addr is taken from the i/f, else punt.
420 */
421 ro = &inp->inp_route;
422 if (ro->ro_rt &&
423 (satosin(&ro->ro_dst)->sin_addr.s_addr !=
424 sin->sin_addr.s_addr ||
425 inp->inp_socket->so_options & SO_DONTROUTE)) {
426 RTFREE(ro->ro_rt);
427 ro->ro_rt = (struct rtentry *)0;
428 }
429 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
430 (ro->ro_rt == (struct rtentry *)0 ||
431 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
432 /* No route yet, so try to acquire one */
433 ro->ro_dst.sa_family = AF_INET;
434 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
435 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
436 sin->sin_addr;
437 rtalloc(ro);
438 }
439 /*
440 * If we found a route, use the address
441 * corresponding to the outgoing interface
442 * unless it is the loopback (in case a route
443 * to our address on another net goes to loopback).
444 */
445 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
446 ia = ifatoia(ro->ro_rt->rt_ifa);
447 if (ia == 0) {
448 u_short fport = sin->sin_port;
449
450 sin->sin_port = 0;
451 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
452 if (ia == 0)
453 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
454 sin->sin_port = fport;
455 if (ia == 0)
456 ia = TAILQ_FIRST(&in_ifaddrhead);
457 if (ia == 0)
458 return (EADDRNOTAVAIL);
459 }
460 /*
461 * If the destination address is multicast and an outgoing
462 * interface has been set as a multicast option, use the
463 * address of that interface as our source address.
464 */
465 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
466 inp->inp_moptions != NULL) {
467 struct ip_moptions *imo;
468 struct ifnet *ifp;
469
470 imo = inp->inp_moptions;
471 if (imo->imo_multicast_ifp != NULL) {
472 ifp = imo->imo_multicast_ifp;
473 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
474 if (ia->ia_ifp == ifp)
475 break;
476 if (ia == 0)
477 return (EADDRNOTAVAIL);
478 }
479 }
480 /*
481 * Don't do pcblookup call here; return interface in plocal_sin
482 * and exit to caller, that will do the lookup.
483 */
484 *plocal_sin = &ia->ia_addr;
485
486 }
487 return(0);
488}
489
490/*
491 * Outer subroutine:
492 * Connect from a socket to a specified address.
493 * Both address and port must be specified in argument sin.
494 * If don't have a local address for this socket yet,
495 * then pick one.
496 */
497int
498in_pcbconnect(inp, nam, td)
499 register struct inpcb *inp;
500 struct sockaddr *nam;
501 struct thread *td;
502{
503 struct sockaddr_in *ifaddr;
504 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
505 struct sockaddr_in sa;
506 struct ucred *cred;
507 int error;
508
509 cred = inp->inp_socket->so_cred;
510 if (inp->inp_laddr.s_addr == INADDR_ANY && jailed(cred)) {
511 bzero(&sa, sizeof (sa));
512 sa.sin_addr.s_addr = htonl(cred->cr_prison->pr_ip);
513 sa.sin_len=sizeof (sa);
514 sa.sin_family = AF_INET;
515 error = in_pcbbind(inp, (struct sockaddr *)&sa, td);
516 if (error)
517 return (error);
518 }
519 /*
520 * Call inner routine, to assign local interface address.
521 */
522 if ((error = in_pcbladdr(inp, nam, &ifaddr)) != 0)
523 return(error);
524
525 if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
526 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
527 inp->inp_lport, 0, NULL) != NULL) {
528 return (EADDRINUSE);
529 }
530 if (inp->inp_laddr.s_addr == INADDR_ANY) {
531 if (inp->inp_lport == 0) {
532 error = in_pcbbind(inp, (struct sockaddr *)0, td);
533 if (error)
534 return (error);
535 }
536 inp->inp_laddr = ifaddr->sin_addr;
537 }
538 inp->inp_faddr = sin->sin_addr;
539 inp->inp_fport = sin->sin_port;
540 in_pcbrehash(inp);
541 return (0);
542}
543
544void
545in_pcbdisconnect(inp)
546 struct inpcb *inp;
547{
548
549 inp->inp_faddr.s_addr = INADDR_ANY;
550 inp->inp_fport = 0;
551 in_pcbrehash(inp);
552 if (inp->inp_socket->so_state & SS_NOFDREF)
553 in_pcbdetach(inp);
554}
555
556void
557in_pcbdetach(inp)
558 struct inpcb *inp;
559{
560 struct socket *so = inp->inp_socket;
561 struct inpcbinfo *ipi = inp->inp_pcbinfo;
562 struct rtentry *rt = inp->inp_route.ro_rt;
563
564#ifdef IPSEC
565 ipsec4_delete_pcbpolicy(inp);
566#endif /*IPSEC*/
567 inp->inp_gencnt = ++ipi->ipi_gencnt;
568 in_pcbremlists(inp);
569 so->so_pcb = 0;
570 sofree(so);
571 if (inp->inp_options)
572 (void)m_free(inp->inp_options);
573 if (rt) {
574 /*
575 * route deletion requires reference count to be <= zero
576 */
577 if ((rt->rt_flags & RTF_DELCLONE) &&
578 (rt->rt_flags & RTF_WASCLONED) &&
579 (rt->rt_refcnt <= 1)) {
580 rt->rt_refcnt--;
581 rt->rt_flags &= ~RTF_UP;
582 rtrequest(RTM_DELETE, rt_key(rt),
583 rt->rt_gateway, rt_mask(rt),
584 rt->rt_flags, (struct rtentry **)0);
585 }
586 else
587 rtfree(rt);
588 }
589 ip_freemoptions(inp->inp_moptions);
590 inp->inp_vflag = 0;
591 zfree(ipi->ipi_zone, inp);
592}
593
594/*
595 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
596 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
597 * in struct pr_usrreqs, so that protocols can just reference then directly
598 * without the need for a wrapper function. The socket must have a valid
599 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
600 * except through a kernel programming error, so it is acceptable to panic
601 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
602 * because there actually /is/ a programming error somewhere... XXX)
603 */
604int
605in_setsockaddr(so, nam)
606 struct socket *so;
607 struct sockaddr **nam;
608{
609 int s;
610 register struct inpcb *inp;
611 register struct sockaddr_in *sin;
612
613 /*
614 * Do the malloc first in case it blocks.
615 */
616 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
617 M_WAITOK | M_ZERO);
618 sin->sin_family = AF_INET;
619 sin->sin_len = sizeof(*sin);
620
621 s = splnet();
622 inp = sotoinpcb(so);
623 if (!inp) {
624 splx(s);
625 free(sin, M_SONAME);
626 return ECONNRESET;
627 }
628 sin->sin_port = inp->inp_lport;
629 sin->sin_addr = inp->inp_laddr;
630 splx(s);
631
632 *nam = (struct sockaddr *)sin;
633 return 0;
634}
635
636int
637in_setpeeraddr(so, nam)
638 struct socket *so;
639 struct sockaddr **nam;
640{
641 int s;
642 struct inpcb *inp;
643 register struct sockaddr_in *sin;
644
645 /*
646 * Do the malloc first in case it blocks.
647 */
648 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
649 M_WAITOK | M_ZERO);
650 sin->sin_family = AF_INET;
651 sin->sin_len = sizeof(*sin);
652
653 s = splnet();
654 inp = sotoinpcb(so);
655 if (!inp) {
656 splx(s);
657 free(sin, M_SONAME);
658 return ECONNRESET;
659 }
660 sin->sin_port = inp->inp_fport;
661 sin->sin_addr = inp->inp_faddr;
662 splx(s);
663
664 *nam = (struct sockaddr *)sin;
665 return 0;
666}
667
668void
669in_pcbnotifyall(head, faddr, errno, notify)
670 struct inpcbhead *head;
671 struct in_addr faddr;
672 int errno;
673 void (*notify) __P((struct inpcb *, int));
674{
675 struct inpcb *inp, *ninp;
676 int s;
677
678 s = splnet();
679 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
680 ninp = LIST_NEXT(inp, inp_list);
681#ifdef INET6
682 if ((inp->inp_vflag & INP_IPV4) == 0)
683 continue;
684#endif
685 if (inp->inp_faddr.s_addr != faddr.s_addr ||
686 inp->inp_socket == NULL)
687 continue;
688 (*notify)(inp, errno);
689 }
690 splx(s);
691}
692
693void
694in_pcbpurgeif0(head, ifp)
695 struct inpcb *head;
696 struct ifnet *ifp;
697{
698 struct inpcb *inp;
699 struct ip_moptions *imo;
700 int i, gap;
701
702 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
703 imo = inp->inp_moptions;
704 if ((inp->inp_vflag & INP_IPV4) &&
705 imo != NULL) {
706 /*
707 * Unselect the outgoing interface if it is being
708 * detached.
709 */
710 if (imo->imo_multicast_ifp == ifp)
711 imo->imo_multicast_ifp = NULL;
712
713 /*
714 * Drop multicast group membership if we joined
715 * through the interface being detached.
716 */
717 for (i = 0, gap = 0; i < imo->imo_num_memberships;
718 i++) {
719 if (imo->imo_membership[i]->inm_ifp == ifp) {
720 in_delmulti(imo->imo_membership[i]);
721 gap++;
722 } else if (gap != 0)
723 imo->imo_membership[i - gap] =
724 imo->imo_membership[i];
725 }
726 imo->imo_num_memberships -= gap;
727 }
728 }
729}
730
731/*
732 * Check for alternatives when higher level complains
733 * about service problems. For now, invalidate cached
734 * routing information. If the route was created dynamically
735 * (by a redirect), time to try a default gateway again.
736 */
737void
738in_losing(inp)
739 struct inpcb *inp;
740{
741 register struct rtentry *rt;
742 struct rt_addrinfo info;
743
744 if ((rt = inp->inp_route.ro_rt)) {
745 bzero((caddr_t)&info, sizeof(info));
746 info.rti_info[RTAX_DST] =
747 (struct sockaddr *)&inp->inp_route.ro_dst;
748 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
749 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
750 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
751 if (rt->rt_flags & RTF_DYNAMIC)
752 (void) rtrequest(RTM_DELETE, rt_key(rt),
753 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
754 (struct rtentry **)0);
755 inp->inp_route.ro_rt = 0;
756 rtfree(rt);
757 /*
758 * A new route can be allocated
759 * the next time output is attempted.
760 */
761 }
762}
763
764/*
765 * After a routing change, flush old routing
766 * and allocate a (hopefully) better one.
767 */
768void
769in_rtchange(inp, errno)
770 register struct inpcb *inp;
771 int errno;
772{
773 if (inp->inp_route.ro_rt) {
774 rtfree(inp->inp_route.ro_rt);
775 inp->inp_route.ro_rt = 0;
776 /*
777 * A new route can be allocated the next time
778 * output is attempted.
779 */
780 }
781}
782
783/*
784 * Lookup a PCB based on the local address and port.
785 */
786struct inpcb *
787in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
788 struct inpcbinfo *pcbinfo;
789 struct in_addr laddr;
790 u_int lport_arg;
791 int wild_okay;
792{
793 register struct inpcb *inp;
794 int matchwild = 3, wildcard;
795 u_short lport = lport_arg;
796
797 if (!wild_okay) {
798 struct inpcbhead *head;
799 /*
800 * Look for an unconnected (wildcard foreign addr) PCB that
801 * matches the local address and port we're looking for.
802 */
803 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
804 LIST_FOREACH(inp, head, inp_hash) {
805#ifdef INET6
806 if ((inp->inp_vflag & INP_IPV4) == 0)
807 continue;
808#endif
809 if (inp->inp_faddr.s_addr == INADDR_ANY &&
810 inp->inp_laddr.s_addr == laddr.s_addr &&
811 inp->inp_lport == lport) {
812 /*
813 * Found.
814 */
815 return (inp);
816 }
817 }
818 /*
819 * Not found.
820 */
821 return (NULL);
822 } else {
823 struct inpcbporthead *porthash;
824 struct inpcbport *phd;
825 struct inpcb *match = NULL;
826 /*
827 * Best fit PCB lookup.
828 *
829 * First see if this local port is in use by looking on the
830 * port hash list.
831 */
832 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
833 pcbinfo->porthashmask)];
834 LIST_FOREACH(phd, porthash, phd_hash) {
835 if (phd->phd_port == lport)
836 break;
837 }
838 if (phd != NULL) {
839 /*
840 * Port is in use by one or more PCBs. Look for best
841 * fit.
842 */
843 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
844 wildcard = 0;
845#ifdef INET6
846 if ((inp->inp_vflag & INP_IPV4) == 0)
847 continue;
848#endif
849 if (inp->inp_faddr.s_addr != INADDR_ANY)
850 wildcard++;
851 if (inp->inp_laddr.s_addr != INADDR_ANY) {
852 if (laddr.s_addr == INADDR_ANY)
853 wildcard++;
854 else if (inp->inp_laddr.s_addr != laddr.s_addr)
855 continue;
856 } else {
857 if (laddr.s_addr != INADDR_ANY)
858 wildcard++;
859 }
860 if (wildcard < matchwild) {
861 match = inp;
862 matchwild = wildcard;
863 if (matchwild == 0) {
864 break;
865 }
866 }
867 }
868 }
869 return (match);
870 }
871}
872
873/*
874 * Lookup PCB in hash list.
875 */
876struct inpcb *
877in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard,
878 ifp)
879 struct inpcbinfo *pcbinfo;
880 struct in_addr faddr, laddr;
881 u_int fport_arg, lport_arg;
882 int wildcard;
883 struct ifnet *ifp;
884{
885 struct inpcbhead *head;
886 register struct inpcb *inp;
887 u_short fport = fport_arg, lport = lport_arg;
888
889 /*
890 * First look for an exact match.
891 */
892 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
893 LIST_FOREACH(inp, head, inp_hash) {
894#ifdef INET6
895 if ((inp->inp_vflag & INP_IPV4) == 0)
896 continue;
897#endif
898 if (inp->inp_faddr.s_addr == faddr.s_addr &&
899 inp->inp_laddr.s_addr == laddr.s_addr &&
900 inp->inp_fport == fport &&
901 inp->inp_lport == lport) {
902 /*
903 * Found.
904 */
905 return (inp);
906 }
907 }
908 if (wildcard) {
909 struct inpcb *local_wild = NULL;
910#if defined(INET6)
911 struct inpcb *local_wild_mapped = NULL;
912#endif /* defined(INET6) */
913
914 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
915 LIST_FOREACH(inp, head, inp_hash) {
916#ifdef INET6
917 if ((inp->inp_vflag & INP_IPV4) == 0)
918 continue;
919#endif
920 if (inp->inp_faddr.s_addr == INADDR_ANY &&
921 inp->inp_lport == lport) {
70#ifdef IPSEC
71#include <netinet6/ipsec.h>
72#include <netkey/key.h>
73#endif /* IPSEC */
74
75struct in_addr zeroin_addr;
76
77/*
78 * These configure the range of local port addresses assigned to
79 * "unspecified" outgoing connections/packets/whatever.
80 */
81int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
82int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
83int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
84int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
85int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
86int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
87
88#define RANGECHK(var, min, max) \
89 if ((var) < (min)) { (var) = (min); } \
90 else if ((var) > (max)) { (var) = (max); }
91
92static int
93sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
94{
95 int error = sysctl_handle_int(oidp,
96 oidp->oid_arg1, oidp->oid_arg2, req);
97 if (!error) {
98 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
99 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
100 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
101 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
102 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
103 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
104 }
105 return error;
106}
107
108#undef RANGECHK
109
110SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
111
112SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
113 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
114SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
115 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
116SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
117 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
118SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
119 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
120SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
121 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
122SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
123 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
124
125/*
126 * in_pcb.c: manage the Protocol Control Blocks.
127 *
128 * NOTE: It is assumed that most of these functions will be called at
129 * splnet(). XXX - There are, unfortunately, a few exceptions to this
130 * rule that should be fixed.
131 */
132
133/*
134 * Allocate a PCB and associate it with the socket.
135 */
136int
137in_pcballoc(so, pcbinfo, td)
138 struct socket *so;
139 struct inpcbinfo *pcbinfo;
140 struct thread *td;
141{
142 register struct inpcb *inp;
143#ifdef IPSEC
144 int error;
145#endif
146
147 inp = zalloc(pcbinfo->ipi_zone);
148 if (inp == NULL)
149 return (ENOBUFS);
150 bzero((caddr_t)inp, sizeof(*inp));
151 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
152 inp->inp_pcbinfo = pcbinfo;
153 inp->inp_socket = so;
154#ifdef IPSEC
155 error = ipsec_init_policy(so, &inp->inp_sp);
156 if (error != 0) {
157 zfree(pcbinfo->ipi_zone, inp);
158 return error;
159 }
160#endif /*IPSEC*/
161#if defined(INET6)
162 if (INP_SOCKAF(so) == AF_INET6 && !ip6_mapped_addr_on)
163 inp->inp_flags |= IN6P_IPV6_V6ONLY;
164#endif
165 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
166 pcbinfo->ipi_count++;
167 so->so_pcb = (caddr_t)inp;
168#ifdef INET6
169 if (ip6_auto_flowlabel)
170 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
171#endif
172 return (0);
173}
174
175int
176in_pcbbind(inp, nam, td)
177 register struct inpcb *inp;
178 struct sockaddr *nam;
179 struct thread *td;
180{
181 struct proc *p = td->td_proc;
182 register struct socket *so = inp->inp_socket;
183 unsigned short *lastport;
184 struct sockaddr_in *sin;
185 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
186 u_short lport = 0;
187 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
188 int error, prison = 0;
189
190 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
191 return (EADDRNOTAVAIL);
192 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
193 return (EINVAL);
194 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
195 wild = 1;
196 if (nam) {
197 sin = (struct sockaddr_in *)nam;
198 if (nam->sa_len != sizeof (*sin))
199 return (EINVAL);
200#ifdef notdef
201 /*
202 * We should check the family, but old programs
203 * incorrectly fail to initialize it.
204 */
205 if (sin->sin_family != AF_INET)
206 return (EAFNOSUPPORT);
207#endif
208 if (sin->sin_addr.s_addr != INADDR_ANY)
209 if (prison_ip(p->p_ucred, 0, &sin->sin_addr.s_addr))
210 return(EINVAL);
211 lport = sin->sin_port;
212 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
213 /*
214 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
215 * allow complete duplication of binding if
216 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
217 * and a multicast address is bound on both
218 * new and duplicated sockets.
219 */
220 if (so->so_options & SO_REUSEADDR)
221 reuseport = SO_REUSEADDR|SO_REUSEPORT;
222 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
223 sin->sin_port = 0; /* yech... */
224 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
225 return (EADDRNOTAVAIL);
226 }
227 if (lport) {
228 struct inpcb *t;
229 /* GROSS */
230 if (ntohs(lport) < IPPORT_RESERVED && p &&
231 suser_xxx(0, p, PRISON_ROOT))
232 return (EACCES);
233 if (p && jailed(p->p_ucred))
234 prison = 1;
235 if (so->so_cred->cr_uid != 0 &&
236 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
237 t = in_pcblookup_local(inp->inp_pcbinfo,
238 sin->sin_addr, lport,
239 prison ? 0 : INPLOOKUP_WILDCARD);
240 if (t &&
241 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
242 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
243 (t->inp_socket->so_options &
244 SO_REUSEPORT) == 0) &&
245 (so->so_cred->cr_uid !=
246 t->inp_socket->so_cred->cr_uid)) {
247#if defined(INET6)
248 if (ntohl(sin->sin_addr.s_addr) !=
249 INADDR_ANY ||
250 ntohl(t->inp_laddr.s_addr) !=
251 INADDR_ANY ||
252 INP_SOCKAF(so) ==
253 INP_SOCKAF(t->inp_socket))
254#endif /* defined(INET6) */
255 return (EADDRINUSE);
256 }
257 }
258 if (prison &&
259 prison_ip(p->p_ucred, 0, &sin->sin_addr.s_addr))
260 return (EADDRNOTAVAIL);
261 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
262 lport, prison ? 0 : wild);
263 if (t &&
264 (reuseport & t->inp_socket->so_options) == 0) {
265#if defined(INET6)
266 if (ntohl(sin->sin_addr.s_addr) !=
267 INADDR_ANY ||
268 ntohl(t->inp_laddr.s_addr) !=
269 INADDR_ANY ||
270 INP_SOCKAF(so) ==
271 INP_SOCKAF(t->inp_socket))
272#endif /* defined(INET6) */
273 return (EADDRINUSE);
274 }
275 }
276 inp->inp_laddr = sin->sin_addr;
277 }
278 if (lport == 0) {
279 ushort first, last;
280 int count;
281
282 if (inp->inp_laddr.s_addr != INADDR_ANY)
283 if (prison_ip(p->p_ucred, 0, &inp->inp_laddr.s_addr )) {
284 inp->inp_laddr.s_addr = INADDR_ANY;
285 return (EINVAL);
286 }
287 inp->inp_flags |= INP_ANONPORT;
288
289 if (inp->inp_flags & INP_HIGHPORT) {
290 first = ipport_hifirstauto; /* sysctl */
291 last = ipport_hilastauto;
292 lastport = &pcbinfo->lasthi;
293 } else if (inp->inp_flags & INP_LOWPORT) {
294 if (p && (error = suser_xxx(0, p, PRISON_ROOT))) {
295 inp->inp_laddr.s_addr = INADDR_ANY;
296 return error;
297 }
298 first = ipport_lowfirstauto; /* 1023 */
299 last = ipport_lowlastauto; /* 600 */
300 lastport = &pcbinfo->lastlow;
301 } else {
302 first = ipport_firstauto; /* sysctl */
303 last = ipport_lastauto;
304 lastport = &pcbinfo->lastport;
305 }
306 /*
307 * Simple check to ensure all ports are not used up causing
308 * a deadlock here.
309 *
310 * We split the two cases (up and down) so that the direction
311 * is not being tested on each round of the loop.
312 */
313 if (first > last) {
314 /*
315 * counting down
316 */
317 count = first - last;
318
319 do {
320 if (count-- < 0) { /* completely used? */
321 inp->inp_laddr.s_addr = INADDR_ANY;
322 return (EADDRNOTAVAIL);
323 }
324 --*lastport;
325 if (*lastport > first || *lastport < last)
326 *lastport = first;
327 lport = htons(*lastport);
328 } while (in_pcblookup_local(pcbinfo,
329 inp->inp_laddr, lport, wild));
330 } else {
331 /*
332 * counting up
333 */
334 count = last - first;
335
336 do {
337 if (count-- < 0) { /* completely used? */
338 /*
339 * Undo any address bind that may have
340 * occurred above.
341 */
342 inp->inp_laddr.s_addr = INADDR_ANY;
343 return (EADDRNOTAVAIL);
344 }
345 ++*lastport;
346 if (*lastport < first || *lastport > last)
347 *lastport = first;
348 lport = htons(*lastport);
349 } while (in_pcblookup_local(pcbinfo,
350 inp->inp_laddr, lport, wild));
351 }
352 }
353 inp->inp_lport = lport;
354 if (prison_ip(p->p_ucred, 0, &inp->inp_laddr.s_addr)) {
355 inp->inp_laddr.s_addr = INADDR_ANY;
356 inp->inp_lport = 0;
357 return(EINVAL);
358 }
359 if (in_pcbinshash(inp) != 0) {
360 inp->inp_laddr.s_addr = INADDR_ANY;
361 inp->inp_lport = 0;
362 return (EAGAIN);
363 }
364 return (0);
365}
366
367/*
368 * Transform old in_pcbconnect() into an inner subroutine for new
369 * in_pcbconnect(): Do some validity-checking on the remote
370 * address (in mbuf 'nam') and then determine local host address
371 * (i.e., which interface) to use to access that remote host.
372 *
373 * This preserves definition of in_pcbconnect(), while supporting a
374 * slightly different version for T/TCP. (This is more than
375 * a bit of a kludge, but cleaning up the internal interfaces would
376 * have forced minor changes in every protocol).
377 */
378
379int
380in_pcbladdr(inp, nam, plocal_sin)
381 register struct inpcb *inp;
382 struct sockaddr *nam;
383 struct sockaddr_in **plocal_sin;
384{
385 struct in_ifaddr *ia;
386 register struct sockaddr_in *sin = (struct sockaddr_in *)nam;
387
388 if (nam->sa_len != sizeof (*sin))
389 return (EINVAL);
390 if (sin->sin_family != AF_INET)
391 return (EAFNOSUPPORT);
392 if (sin->sin_port == 0)
393 return (EADDRNOTAVAIL);
394 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
395 /*
396 * If the destination address is INADDR_ANY,
397 * use the primary local address.
398 * If the supplied address is INADDR_BROADCAST,
399 * and the primary interface supports broadcast,
400 * choose the broadcast address for that interface.
401 */
402#define satosin(sa) ((struct sockaddr_in *)(sa))
403#define sintosa(sin) ((struct sockaddr *)(sin))
404#define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
405 if (sin->sin_addr.s_addr == INADDR_ANY)
406 sin->sin_addr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
407 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
408 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & IFF_BROADCAST))
409 sin->sin_addr = satosin(&TAILQ_FIRST(&in_ifaddrhead)->ia_broadaddr)->sin_addr;
410 }
411 if (inp->inp_laddr.s_addr == INADDR_ANY) {
412 register struct route *ro;
413
414 ia = (struct in_ifaddr *)0;
415 /*
416 * If route is known or can be allocated now,
417 * our src addr is taken from the i/f, else punt.
418 */
419 ro = &inp->inp_route;
420 if (ro->ro_rt &&
421 (satosin(&ro->ro_dst)->sin_addr.s_addr !=
422 sin->sin_addr.s_addr ||
423 inp->inp_socket->so_options & SO_DONTROUTE)) {
424 RTFREE(ro->ro_rt);
425 ro->ro_rt = (struct rtentry *)0;
426 }
427 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
428 (ro->ro_rt == (struct rtentry *)0 ||
429 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
430 /* No route yet, so try to acquire one */
431 ro->ro_dst.sa_family = AF_INET;
432 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
433 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
434 sin->sin_addr;
435 rtalloc(ro);
436 }
437 /*
438 * If we found a route, use the address
439 * corresponding to the outgoing interface
440 * unless it is the loopback (in case a route
441 * to our address on another net goes to loopback).
442 */
443 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
444 ia = ifatoia(ro->ro_rt->rt_ifa);
445 if (ia == 0) {
446 u_short fport = sin->sin_port;
447
448 sin->sin_port = 0;
449 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
450 if (ia == 0)
451 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
452 sin->sin_port = fport;
453 if (ia == 0)
454 ia = TAILQ_FIRST(&in_ifaddrhead);
455 if (ia == 0)
456 return (EADDRNOTAVAIL);
457 }
458 /*
459 * If the destination address is multicast and an outgoing
460 * interface has been set as a multicast option, use the
461 * address of that interface as our source address.
462 */
463 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
464 inp->inp_moptions != NULL) {
465 struct ip_moptions *imo;
466 struct ifnet *ifp;
467
468 imo = inp->inp_moptions;
469 if (imo->imo_multicast_ifp != NULL) {
470 ifp = imo->imo_multicast_ifp;
471 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
472 if (ia->ia_ifp == ifp)
473 break;
474 if (ia == 0)
475 return (EADDRNOTAVAIL);
476 }
477 }
478 /*
479 * Don't do pcblookup call here; return interface in plocal_sin
480 * and exit to caller, that will do the lookup.
481 */
482 *plocal_sin = &ia->ia_addr;
483
484 }
485 return(0);
486}
487
488/*
489 * Outer subroutine:
490 * Connect from a socket to a specified address.
491 * Both address and port must be specified in argument sin.
492 * If don't have a local address for this socket yet,
493 * then pick one.
494 */
495int
496in_pcbconnect(inp, nam, td)
497 register struct inpcb *inp;
498 struct sockaddr *nam;
499 struct thread *td;
500{
501 struct sockaddr_in *ifaddr;
502 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
503 struct sockaddr_in sa;
504 struct ucred *cred;
505 int error;
506
507 cred = inp->inp_socket->so_cred;
508 if (inp->inp_laddr.s_addr == INADDR_ANY && jailed(cred)) {
509 bzero(&sa, sizeof (sa));
510 sa.sin_addr.s_addr = htonl(cred->cr_prison->pr_ip);
511 sa.sin_len=sizeof (sa);
512 sa.sin_family = AF_INET;
513 error = in_pcbbind(inp, (struct sockaddr *)&sa, td);
514 if (error)
515 return (error);
516 }
517 /*
518 * Call inner routine, to assign local interface address.
519 */
520 if ((error = in_pcbladdr(inp, nam, &ifaddr)) != 0)
521 return(error);
522
523 if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
524 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
525 inp->inp_lport, 0, NULL) != NULL) {
526 return (EADDRINUSE);
527 }
528 if (inp->inp_laddr.s_addr == INADDR_ANY) {
529 if (inp->inp_lport == 0) {
530 error = in_pcbbind(inp, (struct sockaddr *)0, td);
531 if (error)
532 return (error);
533 }
534 inp->inp_laddr = ifaddr->sin_addr;
535 }
536 inp->inp_faddr = sin->sin_addr;
537 inp->inp_fport = sin->sin_port;
538 in_pcbrehash(inp);
539 return (0);
540}
541
542void
543in_pcbdisconnect(inp)
544 struct inpcb *inp;
545{
546
547 inp->inp_faddr.s_addr = INADDR_ANY;
548 inp->inp_fport = 0;
549 in_pcbrehash(inp);
550 if (inp->inp_socket->so_state & SS_NOFDREF)
551 in_pcbdetach(inp);
552}
553
554void
555in_pcbdetach(inp)
556 struct inpcb *inp;
557{
558 struct socket *so = inp->inp_socket;
559 struct inpcbinfo *ipi = inp->inp_pcbinfo;
560 struct rtentry *rt = inp->inp_route.ro_rt;
561
562#ifdef IPSEC
563 ipsec4_delete_pcbpolicy(inp);
564#endif /*IPSEC*/
565 inp->inp_gencnt = ++ipi->ipi_gencnt;
566 in_pcbremlists(inp);
567 so->so_pcb = 0;
568 sofree(so);
569 if (inp->inp_options)
570 (void)m_free(inp->inp_options);
571 if (rt) {
572 /*
573 * route deletion requires reference count to be <= zero
574 */
575 if ((rt->rt_flags & RTF_DELCLONE) &&
576 (rt->rt_flags & RTF_WASCLONED) &&
577 (rt->rt_refcnt <= 1)) {
578 rt->rt_refcnt--;
579 rt->rt_flags &= ~RTF_UP;
580 rtrequest(RTM_DELETE, rt_key(rt),
581 rt->rt_gateway, rt_mask(rt),
582 rt->rt_flags, (struct rtentry **)0);
583 }
584 else
585 rtfree(rt);
586 }
587 ip_freemoptions(inp->inp_moptions);
588 inp->inp_vflag = 0;
589 zfree(ipi->ipi_zone, inp);
590}
591
592/*
593 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
594 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
595 * in struct pr_usrreqs, so that protocols can just reference then directly
596 * without the need for a wrapper function. The socket must have a valid
597 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
598 * except through a kernel programming error, so it is acceptable to panic
599 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
600 * because there actually /is/ a programming error somewhere... XXX)
601 */
602int
603in_setsockaddr(so, nam)
604 struct socket *so;
605 struct sockaddr **nam;
606{
607 int s;
608 register struct inpcb *inp;
609 register struct sockaddr_in *sin;
610
611 /*
612 * Do the malloc first in case it blocks.
613 */
614 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
615 M_WAITOK | M_ZERO);
616 sin->sin_family = AF_INET;
617 sin->sin_len = sizeof(*sin);
618
619 s = splnet();
620 inp = sotoinpcb(so);
621 if (!inp) {
622 splx(s);
623 free(sin, M_SONAME);
624 return ECONNRESET;
625 }
626 sin->sin_port = inp->inp_lport;
627 sin->sin_addr = inp->inp_laddr;
628 splx(s);
629
630 *nam = (struct sockaddr *)sin;
631 return 0;
632}
633
634int
635in_setpeeraddr(so, nam)
636 struct socket *so;
637 struct sockaddr **nam;
638{
639 int s;
640 struct inpcb *inp;
641 register struct sockaddr_in *sin;
642
643 /*
644 * Do the malloc first in case it blocks.
645 */
646 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
647 M_WAITOK | M_ZERO);
648 sin->sin_family = AF_INET;
649 sin->sin_len = sizeof(*sin);
650
651 s = splnet();
652 inp = sotoinpcb(so);
653 if (!inp) {
654 splx(s);
655 free(sin, M_SONAME);
656 return ECONNRESET;
657 }
658 sin->sin_port = inp->inp_fport;
659 sin->sin_addr = inp->inp_faddr;
660 splx(s);
661
662 *nam = (struct sockaddr *)sin;
663 return 0;
664}
665
666void
667in_pcbnotifyall(head, faddr, errno, notify)
668 struct inpcbhead *head;
669 struct in_addr faddr;
670 int errno;
671 void (*notify) __P((struct inpcb *, int));
672{
673 struct inpcb *inp, *ninp;
674 int s;
675
676 s = splnet();
677 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
678 ninp = LIST_NEXT(inp, inp_list);
679#ifdef INET6
680 if ((inp->inp_vflag & INP_IPV4) == 0)
681 continue;
682#endif
683 if (inp->inp_faddr.s_addr != faddr.s_addr ||
684 inp->inp_socket == NULL)
685 continue;
686 (*notify)(inp, errno);
687 }
688 splx(s);
689}
690
691void
692in_pcbpurgeif0(head, ifp)
693 struct inpcb *head;
694 struct ifnet *ifp;
695{
696 struct inpcb *inp;
697 struct ip_moptions *imo;
698 int i, gap;
699
700 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
701 imo = inp->inp_moptions;
702 if ((inp->inp_vflag & INP_IPV4) &&
703 imo != NULL) {
704 /*
705 * Unselect the outgoing interface if it is being
706 * detached.
707 */
708 if (imo->imo_multicast_ifp == ifp)
709 imo->imo_multicast_ifp = NULL;
710
711 /*
712 * Drop multicast group membership if we joined
713 * through the interface being detached.
714 */
715 for (i = 0, gap = 0; i < imo->imo_num_memberships;
716 i++) {
717 if (imo->imo_membership[i]->inm_ifp == ifp) {
718 in_delmulti(imo->imo_membership[i]);
719 gap++;
720 } else if (gap != 0)
721 imo->imo_membership[i - gap] =
722 imo->imo_membership[i];
723 }
724 imo->imo_num_memberships -= gap;
725 }
726 }
727}
728
729/*
730 * Check for alternatives when higher level complains
731 * about service problems. For now, invalidate cached
732 * routing information. If the route was created dynamically
733 * (by a redirect), time to try a default gateway again.
734 */
735void
736in_losing(inp)
737 struct inpcb *inp;
738{
739 register struct rtentry *rt;
740 struct rt_addrinfo info;
741
742 if ((rt = inp->inp_route.ro_rt)) {
743 bzero((caddr_t)&info, sizeof(info));
744 info.rti_info[RTAX_DST] =
745 (struct sockaddr *)&inp->inp_route.ro_dst;
746 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
747 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
748 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
749 if (rt->rt_flags & RTF_DYNAMIC)
750 (void) rtrequest(RTM_DELETE, rt_key(rt),
751 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
752 (struct rtentry **)0);
753 inp->inp_route.ro_rt = 0;
754 rtfree(rt);
755 /*
756 * A new route can be allocated
757 * the next time output is attempted.
758 */
759 }
760}
761
762/*
763 * After a routing change, flush old routing
764 * and allocate a (hopefully) better one.
765 */
766void
767in_rtchange(inp, errno)
768 register struct inpcb *inp;
769 int errno;
770{
771 if (inp->inp_route.ro_rt) {
772 rtfree(inp->inp_route.ro_rt);
773 inp->inp_route.ro_rt = 0;
774 /*
775 * A new route can be allocated the next time
776 * output is attempted.
777 */
778 }
779}
780
781/*
782 * Lookup a PCB based on the local address and port.
783 */
784struct inpcb *
785in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
786 struct inpcbinfo *pcbinfo;
787 struct in_addr laddr;
788 u_int lport_arg;
789 int wild_okay;
790{
791 register struct inpcb *inp;
792 int matchwild = 3, wildcard;
793 u_short lport = lport_arg;
794
795 if (!wild_okay) {
796 struct inpcbhead *head;
797 /*
798 * Look for an unconnected (wildcard foreign addr) PCB that
799 * matches the local address and port we're looking for.
800 */
801 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
802 LIST_FOREACH(inp, head, inp_hash) {
803#ifdef INET6
804 if ((inp->inp_vflag & INP_IPV4) == 0)
805 continue;
806#endif
807 if (inp->inp_faddr.s_addr == INADDR_ANY &&
808 inp->inp_laddr.s_addr == laddr.s_addr &&
809 inp->inp_lport == lport) {
810 /*
811 * Found.
812 */
813 return (inp);
814 }
815 }
816 /*
817 * Not found.
818 */
819 return (NULL);
820 } else {
821 struct inpcbporthead *porthash;
822 struct inpcbport *phd;
823 struct inpcb *match = NULL;
824 /*
825 * Best fit PCB lookup.
826 *
827 * First see if this local port is in use by looking on the
828 * port hash list.
829 */
830 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
831 pcbinfo->porthashmask)];
832 LIST_FOREACH(phd, porthash, phd_hash) {
833 if (phd->phd_port == lport)
834 break;
835 }
836 if (phd != NULL) {
837 /*
838 * Port is in use by one or more PCBs. Look for best
839 * fit.
840 */
841 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
842 wildcard = 0;
843#ifdef INET6
844 if ((inp->inp_vflag & INP_IPV4) == 0)
845 continue;
846#endif
847 if (inp->inp_faddr.s_addr != INADDR_ANY)
848 wildcard++;
849 if (inp->inp_laddr.s_addr != INADDR_ANY) {
850 if (laddr.s_addr == INADDR_ANY)
851 wildcard++;
852 else if (inp->inp_laddr.s_addr != laddr.s_addr)
853 continue;
854 } else {
855 if (laddr.s_addr != INADDR_ANY)
856 wildcard++;
857 }
858 if (wildcard < matchwild) {
859 match = inp;
860 matchwild = wildcard;
861 if (matchwild == 0) {
862 break;
863 }
864 }
865 }
866 }
867 return (match);
868 }
869}
870
871/*
872 * Lookup PCB in hash list.
873 */
874struct inpcb *
875in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard,
876 ifp)
877 struct inpcbinfo *pcbinfo;
878 struct in_addr faddr, laddr;
879 u_int fport_arg, lport_arg;
880 int wildcard;
881 struct ifnet *ifp;
882{
883 struct inpcbhead *head;
884 register struct inpcb *inp;
885 u_short fport = fport_arg, lport = lport_arg;
886
887 /*
888 * First look for an exact match.
889 */
890 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
891 LIST_FOREACH(inp, head, inp_hash) {
892#ifdef INET6
893 if ((inp->inp_vflag & INP_IPV4) == 0)
894 continue;
895#endif
896 if (inp->inp_faddr.s_addr == faddr.s_addr &&
897 inp->inp_laddr.s_addr == laddr.s_addr &&
898 inp->inp_fport == fport &&
899 inp->inp_lport == lport) {
900 /*
901 * Found.
902 */
903 return (inp);
904 }
905 }
906 if (wildcard) {
907 struct inpcb *local_wild = NULL;
908#if defined(INET6)
909 struct inpcb *local_wild_mapped = NULL;
910#endif /* defined(INET6) */
911
912 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
913 LIST_FOREACH(inp, head, inp_hash) {
914#ifdef INET6
915 if ((inp->inp_vflag & INP_IPV4) == 0)
916 continue;
917#endif
918 if (inp->inp_faddr.s_addr == INADDR_ANY &&
919 inp->inp_lport == lport) {
922#if defined(NFAITH) && NFAITH > 0
923 if (ifp && ifp->if_type == IFT_FAITH &&
924 (inp->inp_flags & INP_FAITH) == 0)
925 continue;
920 if (ifp && ifp->if_type == IFT_FAITH &&
921 (inp->inp_flags & INP_FAITH) == 0)
922 continue;
926#endif
927 if (inp->inp_laddr.s_addr == laddr.s_addr)
928 return (inp);
929 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
930#if defined(INET6)
931 if (INP_CHECK_SOCKAF(inp->inp_socket,
932 AF_INET6))
933 local_wild_mapped = inp;
934 else
935#endif /* defined(INET6) */
936 local_wild = inp;
937 }
938 }
939 }
940#if defined(INET6)
941 if (local_wild == NULL)
942 return (local_wild_mapped);
943#endif /* defined(INET6) */
944 return (local_wild);
945 }
946
947 /*
948 * Not found.
949 */
950 return (NULL);
951}
952
953/*
954 * Insert PCB onto various hash lists.
955 */
956int
957in_pcbinshash(inp)
958 struct inpcb *inp;
959{
960 struct inpcbhead *pcbhash;
961 struct inpcbporthead *pcbporthash;
962 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
963 struct inpcbport *phd;
964 u_int32_t hashkey_faddr;
965
966#ifdef INET6
967 if (inp->inp_vflag & INP_IPV6)
968 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
969 else
970#endif /* INET6 */
971 hashkey_faddr = inp->inp_faddr.s_addr;
972
973 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
974 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
975
976 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
977 pcbinfo->porthashmask)];
978
979 /*
980 * Go through port list and look for a head for this lport.
981 */
982 LIST_FOREACH(phd, pcbporthash, phd_hash) {
983 if (phd->phd_port == inp->inp_lport)
984 break;
985 }
986 /*
987 * If none exists, malloc one and tack it on.
988 */
989 if (phd == NULL) {
990 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
991 if (phd == NULL) {
992 return (ENOBUFS); /* XXX */
993 }
994 phd->phd_port = inp->inp_lport;
995 LIST_INIT(&phd->phd_pcblist);
996 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
997 }
998 inp->inp_phd = phd;
999 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1000 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1001 return (0);
1002}
1003
1004/*
1005 * Move PCB to the proper hash bucket when { faddr, fport } have been
1006 * changed. NOTE: This does not handle the case of the lport changing (the
1007 * hashed port list would have to be updated as well), so the lport must
1008 * not change after in_pcbinshash() has been called.
1009 */
1010void
1011in_pcbrehash(inp)
1012 struct inpcb *inp;
1013{
1014 struct inpcbhead *head;
1015 u_int32_t hashkey_faddr;
1016
1017#ifdef INET6
1018 if (inp->inp_vflag & INP_IPV6)
1019 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1020 else
1021#endif /* INET6 */
1022 hashkey_faddr = inp->inp_faddr.s_addr;
1023
1024 head = &inp->inp_pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1025 inp->inp_lport, inp->inp_fport, inp->inp_pcbinfo->hashmask)];
1026
1027 LIST_REMOVE(inp, inp_hash);
1028 LIST_INSERT_HEAD(head, inp, inp_hash);
1029}
1030
1031/*
1032 * Remove PCB from various lists.
1033 */
1034void
1035in_pcbremlists(inp)
1036 struct inpcb *inp;
1037{
1038 inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt;
1039 if (inp->inp_lport) {
1040 struct inpcbport *phd = inp->inp_phd;
1041
1042 LIST_REMOVE(inp, inp_hash);
1043 LIST_REMOVE(inp, inp_portlist);
1044 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1045 LIST_REMOVE(phd, phd_hash);
1046 free(phd, M_PCB);
1047 }
1048 }
1049 LIST_REMOVE(inp, inp_list);
1050 inp->inp_pcbinfo->ipi_count--;
1051}
1052
1053int
1054prison_xinpcb(struct proc *p, struct inpcb *inp)
1055{
1056 if (!jailed(p->p_ucred))
1057 return (0);
1058 if (ntohl(inp->inp_laddr.s_addr) == p->p_ucred->cr_prison->pr_ip)
1059 return (0);
1060 return (1);
1061}
923 if (inp->inp_laddr.s_addr == laddr.s_addr)
924 return (inp);
925 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
926#if defined(INET6)
927 if (INP_CHECK_SOCKAF(inp->inp_socket,
928 AF_INET6))
929 local_wild_mapped = inp;
930 else
931#endif /* defined(INET6) */
932 local_wild = inp;
933 }
934 }
935 }
936#if defined(INET6)
937 if (local_wild == NULL)
938 return (local_wild_mapped);
939#endif /* defined(INET6) */
940 return (local_wild);
941 }
942
943 /*
944 * Not found.
945 */
946 return (NULL);
947}
948
949/*
950 * Insert PCB onto various hash lists.
951 */
952int
953in_pcbinshash(inp)
954 struct inpcb *inp;
955{
956 struct inpcbhead *pcbhash;
957 struct inpcbporthead *pcbporthash;
958 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
959 struct inpcbport *phd;
960 u_int32_t hashkey_faddr;
961
962#ifdef INET6
963 if (inp->inp_vflag & INP_IPV6)
964 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
965 else
966#endif /* INET6 */
967 hashkey_faddr = inp->inp_faddr.s_addr;
968
969 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
970 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
971
972 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
973 pcbinfo->porthashmask)];
974
975 /*
976 * Go through port list and look for a head for this lport.
977 */
978 LIST_FOREACH(phd, pcbporthash, phd_hash) {
979 if (phd->phd_port == inp->inp_lport)
980 break;
981 }
982 /*
983 * If none exists, malloc one and tack it on.
984 */
985 if (phd == NULL) {
986 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
987 if (phd == NULL) {
988 return (ENOBUFS); /* XXX */
989 }
990 phd->phd_port = inp->inp_lport;
991 LIST_INIT(&phd->phd_pcblist);
992 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
993 }
994 inp->inp_phd = phd;
995 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
996 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
997 return (0);
998}
999
1000/*
1001 * Move PCB to the proper hash bucket when { faddr, fport } have been
1002 * changed. NOTE: This does not handle the case of the lport changing (the
1003 * hashed port list would have to be updated as well), so the lport must
1004 * not change after in_pcbinshash() has been called.
1005 */
1006void
1007in_pcbrehash(inp)
1008 struct inpcb *inp;
1009{
1010 struct inpcbhead *head;
1011 u_int32_t hashkey_faddr;
1012
1013#ifdef INET6
1014 if (inp->inp_vflag & INP_IPV6)
1015 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1016 else
1017#endif /* INET6 */
1018 hashkey_faddr = inp->inp_faddr.s_addr;
1019
1020 head = &inp->inp_pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1021 inp->inp_lport, inp->inp_fport, inp->inp_pcbinfo->hashmask)];
1022
1023 LIST_REMOVE(inp, inp_hash);
1024 LIST_INSERT_HEAD(head, inp, inp_hash);
1025}
1026
1027/*
1028 * Remove PCB from various lists.
1029 */
1030void
1031in_pcbremlists(inp)
1032 struct inpcb *inp;
1033{
1034 inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt;
1035 if (inp->inp_lport) {
1036 struct inpcbport *phd = inp->inp_phd;
1037
1038 LIST_REMOVE(inp, inp_hash);
1039 LIST_REMOVE(inp, inp_portlist);
1040 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1041 LIST_REMOVE(phd, phd_hash);
1042 free(phd, M_PCB);
1043 }
1044 }
1045 LIST_REMOVE(inp, inp_list);
1046 inp->inp_pcbinfo->ipi_count--;
1047}
1048
1049int
1050prison_xinpcb(struct proc *p, struct inpcb *inp)
1051{
1052 if (!jailed(p->p_ucred))
1053 return (0);
1054 if (ntohl(inp->inp_laddr.s_addr) == p->p_ucred->cr_prison->pr_ip)
1055 return (0);
1056 return (1);
1057}