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