FreeBSD/Linux Kernel Cross Reference
sys/netinet/in_pcb.c
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
30 * $FreeBSD: releng/6.2/sys/netinet/in_pcb.c 162314 2006-09-15 09:51:05Z glebius $
31 */
32
33 #include "opt_ipsec.h"
34 #include "opt_inet6.h"
35 #include "opt_mac.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mac.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/domain.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/proc.h>
47 #include <sys/jail.h>
48 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
50
51 #include <vm/uma.h>
52
53 #include <net/if.h>
54 #include <net/if_types.h>
55 #include <net/route.h>
56
57 #include <netinet/in.h>
58 #include <netinet/in_pcb.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/tcp_var.h>
62 #include <netinet/udp.h>
63 #include <netinet/udp_var.h>
64 #ifdef INET6
65 #include <netinet/ip6.h>
66 #include <netinet6/ip6_var.h>
67 #endif /* INET6 */
68
69 #ifdef IPSEC
70 #include <netinet6/ipsec.h>
71 #include <netkey/key.h>
72 #endif /* IPSEC */
73
74 #ifdef FAST_IPSEC
75 #if defined(IPSEC) || defined(IPSEC_ESP)
76 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
77 #endif
78
79 #include <netipsec/ipsec.h>
80 #include <netipsec/key.h>
81 #endif /* FAST_IPSEC */
82
83 /*
84 * These configure the range of local port addresses assigned to
85 * "unspecified" outgoing connections/packets/whatever.
86 */
87 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
88 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
89 int ipport_firstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
90 int ipport_lastauto = IPPORT_HILASTAUTO; /* 65535 */
91 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
92 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
93
94 /*
95 * Reserved ports accessible only to root. There are significant
96 * security considerations that must be accounted for when changing these,
97 * but the security benefits can be great. Please be careful.
98 */
99 int ipport_reservedhigh = IPPORT_RESERVED - 1; /* 1023 */
100 int ipport_reservedlow = 0;
101
102 /* Variables dealing with random ephemeral port allocation. */
103 int ipport_randomized = 1; /* user controlled via sysctl */
104 int ipport_randomcps = 10; /* user controlled via sysctl */
105 int ipport_randomtime = 45; /* user controlled via sysctl */
106 int ipport_stoprandom = 0; /* toggled by ipport_tick */
107 int ipport_tcpallocs;
108 int ipport_tcplastcount;
109
110 #define RANGECHK(var, min, max) \
111 if ((var) < (min)) { (var) = (min); } \
112 else if ((var) > (max)) { (var) = (max); }
113
114 static int
115 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
116 {
117 int error;
118
119 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
120 if (error == 0) {
121 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
122 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
123 RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
124 RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
125 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
126 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
127 }
128 return (error);
129 }
130
131 #undef RANGECHK
132
133 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
134
135 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
136 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
137 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
138 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
139 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
140 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
141 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
142 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
143 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
144 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
145 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
146 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
147 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
148 CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
149 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
150 CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
151 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
152 &ipport_randomized, 0, "Enable random port allocation");
153 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
154 &ipport_randomcps, 0, "Maximum number of random port "
155 "allocations before switching to a sequental one");
156 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
157 &ipport_randomtime, 0, "Minimum time to keep sequental port "
158 "allocation before switching to a random one");
159
160 /*
161 * in_pcb.c: manage the Protocol Control Blocks.
162 *
163 * NOTE: It is assumed that most of these functions will be called with
164 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
165 * functions often modify hash chains or addresses in pcbs.
166 */
167
168 /*
169 * Allocate a PCB and associate it with the socket.
170 */
171 int
172 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo, const char *type)
173 {
174 struct inpcb *inp;
175 int error;
176
177 INP_INFO_WLOCK_ASSERT(pcbinfo);
178 error = 0;
179 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT | M_ZERO);
180 if (inp == NULL)
181 return (ENOBUFS);
182 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
183 inp->inp_pcbinfo = pcbinfo;
184 inp->inp_socket = so;
185 #ifdef MAC
186 error = mac_init_inpcb(inp, M_NOWAIT);
187 if (error != 0)
188 goto out;
189 SOCK_LOCK(so);
190 mac_create_inpcb_from_socket(so, inp);
191 SOCK_UNLOCK(so);
192 #endif
193 #if defined(IPSEC) || defined(FAST_IPSEC)
194 #ifdef FAST_IPSEC
195 error = ipsec_init_policy(so, &inp->inp_sp);
196 #else
197 error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
198 #endif
199 if (error != 0)
200 goto out;
201 #endif /*IPSEC*/
202 #if defined(INET6)
203 if (INP_SOCKAF(so) == AF_INET6) {
204 inp->inp_vflag |= INP_IPV6PROTO;
205 if (ip6_v6only)
206 inp->inp_flags |= IN6P_IPV6_V6ONLY;
207 }
208 #endif
209 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
210 pcbinfo->ipi_count++;
211 so->so_pcb = (caddr_t)inp;
212 INP_LOCK_INIT(inp, "inp", type);
213 #ifdef INET6
214 if (ip6_auto_flowlabel)
215 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
216 #endif
217 #if defined(IPSEC) || defined(FAST_IPSEC) || defined(MAC)
218 out:
219 if (error != 0)
220 uma_zfree(pcbinfo->ipi_zone, inp);
221 #endif
222 return (error);
223 }
224
225 int
226 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
227 {
228 int anonport, error;
229
230 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
231 INP_LOCK_ASSERT(inp);
232
233 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
234 return (EINVAL);
235 anonport = inp->inp_lport == 0 && (nam == NULL ||
236 ((struct sockaddr_in *)nam)->sin_port == 0);
237 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
238 &inp->inp_lport, cred);
239 if (error)
240 return (error);
241 if (in_pcbinshash(inp) != 0) {
242 inp->inp_laddr.s_addr = INADDR_ANY;
243 inp->inp_lport = 0;
244 return (EAGAIN);
245 }
246 if (anonport)
247 inp->inp_flags |= INP_ANONPORT;
248 return (0);
249 }
250
251 /*
252 * Set up a bind operation on a PCB, performing port allocation
253 * as required, but do not actually modify the PCB. Callers can
254 * either complete the bind by setting inp_laddr/inp_lport and
255 * calling in_pcbinshash(), or they can just use the resulting
256 * port and address to authorise the sending of a once-off packet.
257 *
258 * On error, the values of *laddrp and *lportp are not changed.
259 */
260 int
261 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
262 u_short *lportp, struct ucred *cred)
263 {
264 struct socket *so = inp->inp_socket;
265 unsigned short *lastport;
266 struct sockaddr_in *sin;
267 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
268 struct in_addr laddr;
269 u_short lport = 0;
270 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
271 int error, prison = 0;
272 int dorandom;
273
274 INP_INFO_WLOCK_ASSERT(pcbinfo);
275 INP_LOCK_ASSERT(inp);
276
277 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
278 return (EADDRNOTAVAIL);
279 laddr.s_addr = *laddrp;
280 if (nam != NULL && laddr.s_addr != INADDR_ANY)
281 return (EINVAL);
282 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
283 wild = 1;
284 if (nam) {
285 sin = (struct sockaddr_in *)nam;
286 if (nam->sa_len != sizeof (*sin))
287 return (EINVAL);
288 #ifdef notdef
289 /*
290 * We should check the family, but old programs
291 * incorrectly fail to initialize it.
292 */
293 if (sin->sin_family != AF_INET)
294 return (EAFNOSUPPORT);
295 #endif
296 if (sin->sin_addr.s_addr != INADDR_ANY)
297 if (prison_ip(cred, 0, &sin->sin_addr.s_addr))
298 return(EINVAL);
299 if (sin->sin_port != *lportp) {
300 /* Don't allow the port to change. */
301 if (*lportp != 0)
302 return (EINVAL);
303 lport = sin->sin_port;
304 }
305 /* NB: lport is left as 0 if the port isn't being changed. */
306 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
307 /*
308 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
309 * allow complete duplication of binding if
310 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
311 * and a multicast address is bound on both
312 * new and duplicated sockets.
313 */
314 if (so->so_options & SO_REUSEADDR)
315 reuseport = SO_REUSEADDR|SO_REUSEPORT;
316 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
317 sin->sin_port = 0; /* yech... */
318 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
319 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
320 return (EADDRNOTAVAIL);
321 }
322 laddr = sin->sin_addr;
323 if (lport) {
324 struct inpcb *t;
325 /* GROSS */
326 if (ntohs(lport) <= ipport_reservedhigh &&
327 ntohs(lport) >= ipport_reservedlow &&
328 suser_cred(cred, SUSER_ALLOWJAIL))
329 return (EACCES);
330 if (jailed(cred))
331 prison = 1;
332 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
333 suser_cred(so->so_cred, SUSER_ALLOWJAIL) != 0) {
334 t = in_pcblookup_local(inp->inp_pcbinfo,
335 sin->sin_addr, lport,
336 prison ? 0 : INPLOOKUP_WILDCARD);
337 /*
338 * XXX
339 * This entire block sorely needs a rewrite.
340 */
341 if (t &&
342 ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
343 (so->so_type != SOCK_STREAM ||
344 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
345 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
346 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
347 (t->inp_socket->so_options &
348 SO_REUSEPORT) == 0) &&
349 (so->so_cred->cr_uid !=
350 t->inp_socket->so_cred->cr_uid))
351 return (EADDRINUSE);
352 }
353 if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr))
354 return (EADDRNOTAVAIL);
355 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
356 lport, prison ? 0 : wild);
357 if (t && (t->inp_vflag & INP_TIMEWAIT)) {
358 if ((reuseport & intotw(t)->tw_so_options) == 0)
359 return (EADDRINUSE);
360 } else
361 if (t &&
362 (reuseport & t->inp_socket->so_options) == 0) {
363 #if defined(INET6)
364 if (ntohl(sin->sin_addr.s_addr) !=
365 INADDR_ANY ||
366 ntohl(t->inp_laddr.s_addr) !=
367 INADDR_ANY ||
368 INP_SOCKAF(so) ==
369 INP_SOCKAF(t->inp_socket))
370 #endif /* defined(INET6) */
371 return (EADDRINUSE);
372 }
373 }
374 }
375 if (*lportp != 0)
376 lport = *lportp;
377 if (lport == 0) {
378 u_short first, last;
379 int count;
380
381 if (laddr.s_addr != INADDR_ANY)
382 if (prison_ip(cred, 0, &laddr.s_addr))
383 return (EINVAL);
384
385 if (inp->inp_flags & INP_HIGHPORT) {
386 first = ipport_hifirstauto; /* sysctl */
387 last = ipport_hilastauto;
388 lastport = &pcbinfo->lasthi;
389 } else if (inp->inp_flags & INP_LOWPORT) {
390 if ((error = suser_cred(cred, SUSER_ALLOWJAIL)) != 0)
391 return error;
392 first = ipport_lowfirstauto; /* 1023 */
393 last = ipport_lowlastauto; /* 600 */
394 lastport = &pcbinfo->lastlow;
395 } else {
396 first = ipport_firstauto; /* sysctl */
397 last = ipport_lastauto;
398 lastport = &pcbinfo->lastport;
399 }
400 /*
401 * For UDP, use random port allocation as long as the user
402 * allows it. For TCP (and as of yet unknown) connections,
403 * use random port allocation only if the user allows it AND
404 * ipport_tick() allows it.
405 */
406 if (ipport_randomized &&
407 (!ipport_stoprandom || pcbinfo == &udbinfo))
408 dorandom = 1;
409 else
410 dorandom = 0;
411 /*
412 * It makes no sense to do random port allocation if
413 * we have the only port available.
414 */
415 if (first == last)
416 dorandom = 0;
417 /* Make sure to not include UDP packets in the count. */
418 if (pcbinfo != &udbinfo)
419 ipport_tcpallocs++;
420 /*
421 * Simple check to ensure all ports are not used up causing
422 * a deadlock here.
423 *
424 * We split the two cases (up and down) so that the direction
425 * is not being tested on each round of the loop.
426 */
427 if (first > last) {
428 /*
429 * counting down
430 */
431 if (dorandom)
432 *lastport = first -
433 (arc4random() % (first - last));
434 count = first - last;
435
436 do {
437 if (count-- < 0) /* completely used? */
438 return (EADDRNOTAVAIL);
439 --*lastport;
440 if (*lastport > first || *lastport < last)
441 *lastport = first;
442 lport = htons(*lastport);
443 } while (in_pcblookup_local(pcbinfo, laddr, lport,
444 wild));
445 } else {
446 /*
447 * counting up
448 */
449 if (dorandom)
450 *lastport = first +
451 (arc4random() % (last - first));
452 count = last - first;
453
454 do {
455 if (count-- < 0) /* completely used? */
456 return (EADDRNOTAVAIL);
457 ++*lastport;
458 if (*lastport < first || *lastport > last)
459 *lastport = first;
460 lport = htons(*lastport);
461 } while (in_pcblookup_local(pcbinfo, laddr, lport,
462 wild));
463 }
464 }
465 if (prison_ip(cred, 0, &laddr.s_addr))
466 return (EINVAL);
467 *laddrp = laddr.s_addr;
468 *lportp = lport;
469 return (0);
470 }
471
472 /*
473 * Connect from a socket to a specified address.
474 * Both address and port must be specified in argument sin.
475 * If don't have a local address for this socket yet,
476 * then pick one.
477 */
478 int
479 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
480 {
481 u_short lport, fport;
482 in_addr_t laddr, faddr;
483 int anonport, error;
484
485 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
486 INP_LOCK_ASSERT(inp);
487
488 lport = inp->inp_lport;
489 laddr = inp->inp_laddr.s_addr;
490 anonport = (lport == 0);
491 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
492 NULL, cred);
493 if (error)
494 return (error);
495
496 /* Do the initial binding of the local address if required. */
497 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
498 inp->inp_lport = lport;
499 inp->inp_laddr.s_addr = laddr;
500 if (in_pcbinshash(inp) != 0) {
501 inp->inp_laddr.s_addr = INADDR_ANY;
502 inp->inp_lport = 0;
503 return (EAGAIN);
504 }
505 }
506
507 /* Commit the remaining changes. */
508 inp->inp_lport = lport;
509 inp->inp_laddr.s_addr = laddr;
510 inp->inp_faddr.s_addr = faddr;
511 inp->inp_fport = fport;
512 in_pcbrehash(inp);
513 #ifdef IPSEC
514 if (inp->inp_socket->so_type == SOCK_STREAM)
515 ipsec_pcbconn(inp->inp_sp);
516 #endif
517 if (anonport)
518 inp->inp_flags |= INP_ANONPORT;
519 return (0);
520 }
521
522 /*
523 * Set up for a connect from a socket to the specified address.
524 * On entry, *laddrp and *lportp should contain the current local
525 * address and port for the PCB; these are updated to the values
526 * that should be placed in inp_laddr and inp_lport to complete
527 * the connect.
528 *
529 * On success, *faddrp and *fportp will be set to the remote address
530 * and port. These are not updated in the error case.
531 *
532 * If the operation fails because the connection already exists,
533 * *oinpp will be set to the PCB of that connection so that the
534 * caller can decide to override it. In all other cases, *oinpp
535 * is set to NULL.
536 */
537 int
538 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
539 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
540 struct inpcb **oinpp, struct ucred *cred)
541 {
542 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
543 struct in_ifaddr *ia;
544 struct sockaddr_in sa;
545 struct ucred *socred;
546 struct inpcb *oinp;
547 struct in_addr laddr, faddr;
548 u_short lport, fport;
549 int error;
550
551 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
552 INP_LOCK_ASSERT(inp);
553
554 if (oinpp != NULL)
555 *oinpp = NULL;
556 if (nam->sa_len != sizeof (*sin))
557 return (EINVAL);
558 if (sin->sin_family != AF_INET)
559 return (EAFNOSUPPORT);
560 if (sin->sin_port == 0)
561 return (EADDRNOTAVAIL);
562 laddr.s_addr = *laddrp;
563 lport = *lportp;
564 faddr = sin->sin_addr;
565 fport = sin->sin_port;
566 socred = inp->inp_socket->so_cred;
567 if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
568 bzero(&sa, sizeof(sa));
569 sa.sin_addr.s_addr = htonl(prison_getip(socred));
570 sa.sin_len = sizeof(sa);
571 sa.sin_family = AF_INET;
572 error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
573 &laddr.s_addr, &lport, cred);
574 if (error)
575 return (error);
576 }
577 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
578 /*
579 * If the destination address is INADDR_ANY,
580 * use the primary local address.
581 * If the supplied address is INADDR_BROADCAST,
582 * and the primary interface supports broadcast,
583 * choose the broadcast address for that interface.
584 */
585 if (faddr.s_addr == INADDR_ANY)
586 faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
587 else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
588 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
589 IFF_BROADCAST))
590 faddr = satosin(&TAILQ_FIRST(
591 &in_ifaddrhead)->ia_broadaddr)->sin_addr;
592 }
593 if (laddr.s_addr == INADDR_ANY) {
594 struct route sro;
595
596 bzero(&sro, sizeof(sro));
597 ia = (struct in_ifaddr *)0;
598 /*
599 * If route is known our src addr is taken from the i/f,
600 * else punt.
601 */
602 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) {
603 /* Find out route to destination */
604 sro.ro_dst.sa_family = AF_INET;
605 sro.ro_dst.sa_len = sizeof(struct sockaddr_in);
606 ((struct sockaddr_in *)&sro.ro_dst)->sin_addr = faddr;
607 rtalloc_ign(&sro, RTF_CLONING);
608 }
609 /*
610 * If we found a route, use the address
611 * corresponding to the outgoing interface.
612 */
613 if (sro.ro_rt) {
614 ia = ifatoia(sro.ro_rt->rt_ifa);
615 RTFREE(sro.ro_rt);
616 }
617 if (ia == 0) {
618 bzero(&sa, sizeof(sa));
619 sa.sin_addr = faddr;
620 sa.sin_len = sizeof(sa);
621 sa.sin_family = AF_INET;
622
623 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
624 if (ia == 0)
625 ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
626 if (ia == 0)
627 return (ENETUNREACH);
628 }
629 /*
630 * If the destination address is multicast and an outgoing
631 * interface has been set as a multicast option, use the
632 * address of that interface as our source address.
633 */
634 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
635 inp->inp_moptions != NULL) {
636 struct ip_moptions *imo;
637 struct ifnet *ifp;
638
639 imo = inp->inp_moptions;
640 if (imo->imo_multicast_ifp != NULL) {
641 ifp = imo->imo_multicast_ifp;
642 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
643 if (ia->ia_ifp == ifp)
644 break;
645 if (ia == 0)
646 return (EADDRNOTAVAIL);
647 }
648 }
649 laddr = ia->ia_addr.sin_addr;
650 }
651
652 oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
653 0, NULL);
654 if (oinp != NULL) {
655 if (oinpp != NULL)
656 *oinpp = oinp;
657 return (EADDRINUSE);
658 }
659 if (lport == 0) {
660 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
661 cred);
662 if (error)
663 return (error);
664 }
665 *laddrp = laddr.s_addr;
666 *lportp = lport;
667 *faddrp = faddr.s_addr;
668 *fportp = fport;
669 return (0);
670 }
671
672 void
673 in_pcbdisconnect(struct inpcb *inp)
674 {
675
676 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
677 INP_LOCK_ASSERT(inp);
678
679 inp->inp_faddr.s_addr = INADDR_ANY;
680 inp->inp_fport = 0;
681 in_pcbrehash(inp);
682 #ifdef IPSEC
683 ipsec_pcbdisconn(inp->inp_sp);
684 #endif
685 if (inp->inp_socket->so_state & SS_NOFDREF)
686 in_pcbdetach(inp);
687 }
688
689 void
690 in_pcbdetach(struct inpcb *inp)
691 {
692 struct socket *so = inp->inp_socket;
693 struct inpcbinfo *ipi = inp->inp_pcbinfo;
694
695 INP_INFO_WLOCK_ASSERT(ipi);
696 INP_LOCK_ASSERT(inp);
697
698 #if defined(IPSEC) || defined(FAST_IPSEC)
699 ipsec4_delete_pcbpolicy(inp);
700 #endif /*IPSEC*/
701 inp->inp_gencnt = ++ipi->ipi_gencnt;
702 in_pcbremlists(inp);
703 if (so) {
704 ACCEPT_LOCK();
705 SOCK_LOCK(so);
706 so->so_pcb = NULL;
707 sotryfree(so);
708 }
709 if (inp->inp_options)
710 (void)m_free(inp->inp_options);
711 ip_freemoptions(inp->inp_moptions);
712 inp->inp_vflag = 0;
713 INP_LOCK_DESTROY(inp);
714 #ifdef MAC
715 mac_destroy_inpcb(inp);
716 #endif
717 uma_zfree(ipi->ipi_zone, inp);
718 }
719
720 struct sockaddr *
721 in_sockaddr(in_port_t port, struct in_addr *addr_p)
722 {
723 struct sockaddr_in *sin;
724
725 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
726 M_WAITOK | M_ZERO);
727 sin->sin_family = AF_INET;
728 sin->sin_len = sizeof(*sin);
729 sin->sin_addr = *addr_p;
730 sin->sin_port = port;
731
732 return (struct sockaddr *)sin;
733 }
734
735 /*
736 * The wrapper function will pass down the pcbinfo for this function to lock.
737 * The socket must have a valid
738 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
739 * except through a kernel programming error, so it is acceptable to panic
740 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
741 * because there actually /is/ a programming error somewhere... XXX)
742 */
743 int
744 in_setsockaddr(struct socket *so, struct sockaddr **nam,
745 struct inpcbinfo *pcbinfo)
746 {
747 struct inpcb *inp;
748 struct in_addr addr;
749 in_port_t port;
750
751 INP_INFO_RLOCK(pcbinfo);
752 inp = sotoinpcb(so);
753 if (!inp) {
754 INP_INFO_RUNLOCK(pcbinfo);
755 return ECONNRESET;
756 }
757 INP_LOCK(inp);
758 port = inp->inp_lport;
759 addr = inp->inp_laddr;
760 INP_UNLOCK(inp);
761 INP_INFO_RUNLOCK(pcbinfo);
762
763 *nam = in_sockaddr(port, &addr);
764 return 0;
765 }
766
767 /*
768 * The wrapper function will pass down the pcbinfo for this function to lock.
769 */
770 int
771 in_setpeeraddr(struct socket *so, struct sockaddr **nam,
772 struct inpcbinfo *pcbinfo)
773 {
774 struct inpcb *inp;
775 struct in_addr addr;
776 in_port_t port;
777
778 INP_INFO_RLOCK(pcbinfo);
779 inp = sotoinpcb(so);
780 if (!inp) {
781 INP_INFO_RUNLOCK(pcbinfo);
782 return ECONNRESET;
783 }
784 INP_LOCK(inp);
785 port = inp->inp_fport;
786 addr = inp->inp_faddr;
787 INP_UNLOCK(inp);
788 INP_INFO_RUNLOCK(pcbinfo);
789
790 *nam = in_sockaddr(port, &addr);
791 return 0;
792 }
793
794 void
795 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
796 struct inpcb *(*notify)(struct inpcb *, int))
797 {
798 struct inpcb *inp, *ninp;
799 struct inpcbhead *head;
800
801 INP_INFO_WLOCK(pcbinfo);
802 head = pcbinfo->listhead;
803 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
804 INP_LOCK(inp);
805 ninp = LIST_NEXT(inp, inp_list);
806 #ifdef INET6
807 if ((inp->inp_vflag & INP_IPV4) == 0) {
808 INP_UNLOCK(inp);
809 continue;
810 }
811 #endif
812 if (inp->inp_faddr.s_addr != faddr.s_addr ||
813 inp->inp_socket == NULL) {
814 INP_UNLOCK(inp);
815 continue;
816 }
817 if ((*notify)(inp, errno))
818 INP_UNLOCK(inp);
819 }
820 INP_INFO_WUNLOCK(pcbinfo);
821 }
822
823 void
824 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
825 {
826 struct inpcb *inp;
827 struct ip_moptions *imo;
828 int i, gap;
829
830 INP_INFO_RLOCK(pcbinfo);
831 LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
832 INP_LOCK(inp);
833 imo = inp->inp_moptions;
834 if ((inp->inp_vflag & INP_IPV4) &&
835 imo != NULL) {
836 /*
837 * Unselect the outgoing interface if it is being
838 * detached.
839 */
840 if (imo->imo_multicast_ifp == ifp)
841 imo->imo_multicast_ifp = NULL;
842
843 /*
844 * Drop multicast group membership if we joined
845 * through the interface being detached.
846 */
847 for (i = 0, gap = 0; i < imo->imo_num_memberships;
848 i++) {
849 if (imo->imo_membership[i]->inm_ifp == ifp) {
850 in_delmulti(imo->imo_membership[i]);
851 gap++;
852 } else if (gap != 0)
853 imo->imo_membership[i - gap] =
854 imo->imo_membership[i];
855 }
856 imo->imo_num_memberships -= gap;
857 }
858 INP_UNLOCK(inp);
859 }
860 INP_INFO_RUNLOCK(pcbinfo);
861 }
862
863 /*
864 * Lookup a PCB based on the local address and port.
865 */
866 #define INP_LOOKUP_MAPPED_PCB_COST 3
867 struct inpcb *
868 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
869 u_int lport_arg, int wild_okay)
870 {
871 struct inpcb *inp;
872 #ifdef INET6
873 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
874 #else
875 int matchwild = 3;
876 #endif
877 int wildcard;
878 u_short lport = lport_arg;
879
880 INP_INFO_WLOCK_ASSERT(pcbinfo);
881
882 if (!wild_okay) {
883 struct inpcbhead *head;
884 /*
885 * Look for an unconnected (wildcard foreign addr) PCB that
886 * matches the local address and port we're looking for.
887 */
888 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
889 LIST_FOREACH(inp, head, inp_hash) {
890 #ifdef INET6
891 if ((inp->inp_vflag & INP_IPV4) == 0)
892 continue;
893 #endif
894 if (inp->inp_faddr.s_addr == INADDR_ANY &&
895 inp->inp_laddr.s_addr == laddr.s_addr &&
896 inp->inp_lport == lport) {
897 /*
898 * Found.
899 */
900 return (inp);
901 }
902 }
903 /*
904 * Not found.
905 */
906 return (NULL);
907 } else {
908 struct inpcbporthead *porthash;
909 struct inpcbport *phd;
910 struct inpcb *match = NULL;
911 /*
912 * Best fit PCB lookup.
913 *
914 * First see if this local port is in use by looking on the
915 * port hash list.
916 */
917 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
918 pcbinfo->porthashmask)];
919 LIST_FOREACH(phd, porthash, phd_hash) {
920 if (phd->phd_port == lport)
921 break;
922 }
923 if (phd != NULL) {
924 /*
925 * Port is in use by one or more PCBs. Look for best
926 * fit.
927 */
928 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
929 wildcard = 0;
930 #ifdef INET6
931 if ((inp->inp_vflag & INP_IPV4) == 0)
932 continue;
933 /*
934 * We never select the PCB that has
935 * INP_IPV6 flag and is bound to :: if
936 * we have another PCB which is bound
937 * to 0.0.0.0. If a PCB has the
938 * INP_IPV6 flag, then we set its cost
939 * higher than IPv4 only PCBs.
940 *
941 * Note that the case only happens
942 * when a socket is bound to ::, under
943 * the condition that the use of the
944 * mapped address is allowed.
945 */
946 if ((inp->inp_vflag & INP_IPV6) != 0)
947 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
948 #endif
949 if (inp->inp_faddr.s_addr != INADDR_ANY)
950 wildcard++;
951 if (inp->inp_laddr.s_addr != INADDR_ANY) {
952 if (laddr.s_addr == INADDR_ANY)
953 wildcard++;
954 else if (inp->inp_laddr.s_addr != laddr.s_addr)
955 continue;
956 } else {
957 if (laddr.s_addr != INADDR_ANY)
958 wildcard++;
959 }
960 if (wildcard < matchwild) {
961 match = inp;
962 matchwild = wildcard;
963 if (matchwild == 0) {
964 break;
965 }
966 }
967 }
968 }
969 return (match);
970 }
971 }
972 #undef INP_LOOKUP_MAPPED_PCB_COST
973
974 /*
975 * Lookup PCB in hash list.
976 */
977 struct inpcb *
978 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
979 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
980 struct ifnet *ifp)
981 {
982 struct inpcbhead *head;
983 struct inpcb *inp;
984 u_short fport = fport_arg, lport = lport_arg;
985
986 INP_INFO_RLOCK_ASSERT(pcbinfo);
987 /*
988 * First look for an exact match.
989 */
990 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
991 LIST_FOREACH(inp, head, inp_hash) {
992 #ifdef INET6
993 if ((inp->inp_vflag & INP_IPV4) == 0)
994 continue;
995 #endif
996 if (inp->inp_faddr.s_addr == faddr.s_addr &&
997 inp->inp_laddr.s_addr == laddr.s_addr &&
998 inp->inp_fport == fport &&
999 inp->inp_lport == lport) {
1000 /*
1001 * Found.
1002 */
1003 return (inp);
1004 }
1005 }
1006 if (wildcard) {
1007 struct inpcb *local_wild = NULL;
1008 #if defined(INET6)
1009 struct inpcb *local_wild_mapped = NULL;
1010 #endif /* defined(INET6) */
1011
1012 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1013 LIST_FOREACH(inp, head, inp_hash) {
1014 #ifdef INET6
1015 if ((inp->inp_vflag & INP_IPV4) == 0)
1016 continue;
1017 #endif
1018 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1019 inp->inp_lport == lport) {
1020 if (ifp && ifp->if_type == IFT_FAITH &&
1021 (inp->inp_flags & INP_FAITH) == 0)
1022 continue;
1023 if (inp->inp_laddr.s_addr == laddr.s_addr)
1024 return (inp);
1025 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1026 #if defined(INET6)
1027 if (INP_CHECK_SOCKAF(inp->inp_socket,
1028 AF_INET6))
1029 local_wild_mapped = inp;
1030 else
1031 #endif /* defined(INET6) */
1032 local_wild = inp;
1033 }
1034 }
1035 }
1036 #if defined(INET6)
1037 if (local_wild == NULL)
1038 return (local_wild_mapped);
1039 #endif /* defined(INET6) */
1040 return (local_wild);
1041 }
1042
1043 /*
1044 * Not found.
1045 */
1046 return (NULL);
1047 }
1048
1049 /*
1050 * Insert PCB onto various hash lists.
1051 */
1052 int
1053 in_pcbinshash(struct inpcb *inp)
1054 {
1055 struct inpcbhead *pcbhash;
1056 struct inpcbporthead *pcbporthash;
1057 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1058 struct inpcbport *phd;
1059 u_int32_t hashkey_faddr;
1060
1061 INP_INFO_WLOCK_ASSERT(pcbinfo);
1062 #ifdef INET6
1063 if (inp->inp_vflag & INP_IPV6)
1064 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1065 else
1066 #endif /* INET6 */
1067 hashkey_faddr = inp->inp_faddr.s_addr;
1068
1069 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1070 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1071
1072 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1073 pcbinfo->porthashmask)];
1074
1075 /*
1076 * Go through port list and look for a head for this lport.
1077 */
1078 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1079 if (phd->phd_port == inp->inp_lport)
1080 break;
1081 }
1082 /*
1083 * If none exists, malloc one and tack it on.
1084 */
1085 if (phd == NULL) {
1086 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1087 if (phd == NULL) {
1088 return (ENOBUFS); /* XXX */
1089 }
1090 phd->phd_port = inp->inp_lport;
1091 LIST_INIT(&phd->phd_pcblist);
1092 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1093 }
1094 inp->inp_phd = phd;
1095 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1096 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1097 return (0);
1098 }
1099
1100 /*
1101 * Move PCB to the proper hash bucket when { faddr, fport } have been
1102 * changed. NOTE: This does not handle the case of the lport changing (the
1103 * hashed port list would have to be updated as well), so the lport must
1104 * not change after in_pcbinshash() has been called.
1105 */
1106 void
1107 in_pcbrehash(struct inpcb *inp)
1108 {
1109 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1110 struct inpcbhead *head;
1111 u_int32_t hashkey_faddr;
1112
1113 INP_INFO_WLOCK_ASSERT(pcbinfo);
1114 INP_LOCK_ASSERT(inp);
1115 #ifdef INET6
1116 if (inp->inp_vflag & INP_IPV6)
1117 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1118 else
1119 #endif /* INET6 */
1120 hashkey_faddr = inp->inp_faddr.s_addr;
1121
1122 head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1123 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1124
1125 LIST_REMOVE(inp, inp_hash);
1126 LIST_INSERT_HEAD(head, inp, inp_hash);
1127 }
1128
1129 /*
1130 * Remove PCB from various lists.
1131 */
1132 void
1133 in_pcbremlists(struct inpcb *inp)
1134 {
1135 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1136
1137 INP_INFO_WLOCK_ASSERT(pcbinfo);
1138 INP_LOCK_ASSERT(inp);
1139
1140 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1141 if (inp->inp_lport) {
1142 struct inpcbport *phd = inp->inp_phd;
1143
1144 LIST_REMOVE(inp, inp_hash);
1145 LIST_REMOVE(inp, inp_portlist);
1146 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1147 LIST_REMOVE(phd, phd_hash);
1148 free(phd, M_PCB);
1149 }
1150 }
1151 LIST_REMOVE(inp, inp_list);
1152 pcbinfo->ipi_count--;
1153 }
1154
1155 /*
1156 * A set label operation has occurred at the socket layer, propagate the
1157 * label change into the in_pcb for the socket.
1158 */
1159 void
1160 in_pcbsosetlabel(struct socket *so)
1161 {
1162 #ifdef MAC
1163 struct inpcb *inp;
1164
1165 inp = (struct inpcb *)so->so_pcb;
1166 INP_LOCK(inp);
1167 SOCK_LOCK(so);
1168 mac_inpcb_sosetlabel(so, inp);
1169 SOCK_UNLOCK(so);
1170 INP_UNLOCK(inp);
1171 #endif
1172 }
1173
1174 /*
1175 * ipport_tick runs once per second, determining if random port
1176 * allocation should be continued. If more than ipport_randomcps
1177 * ports have been allocated in the last second, then we return to
1178 * sequential port allocation. We return to random allocation only
1179 * once we drop below ipport_randomcps for at least ipport_randomtime
1180 * seconds.
1181 */
1182
1183 void
1184 ipport_tick(void *xtp)
1185 {
1186 if (ipport_tcpallocs > ipport_tcplastcount + ipport_randomcps) {
1187 ipport_stoprandom = ipport_randomtime;
1188 } else {
1189 if (ipport_stoprandom > 0)
1190 ipport_stoprandom--;
1191 }
1192 ipport_tcplastcount = ipport_tcpallocs;
1193 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
1194 }
Cache object: 92d50a41fe1230e61538bc2f9bc30f21
|