]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/netinet/in_pcb.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / netinet / in_pcb.c
1 /*-
2  * Copyright (c) 1982, 1986, 1991, 1993, 1995
3  *      The Regents of the University of California.
4  * Copyright (c) 2007-2009 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 4. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *      @(#)in_pcb.c    8.4 (Berkeley) 5/24/95
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "opt_ddb.h"
38 #include "opt_ipsec.h"
39 #include "opt_inet6.h"
40 #include "opt_mac.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/jail.h>
53 #include <sys/kernel.h>
54 #include <sys/sysctl.h>
55
56 #ifdef DDB
57 #include <ddb/ddb.h>
58 #endif
59
60 #include <vm/uma.h>
61
62 #include <net/if.h>
63 #include <net/if_types.h>
64 #include <net/route.h>
65
66 #include <netinet/in.h>
67 #include <netinet/in_pcb.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/tcp_var.h>
71 #include <netinet/udp.h>
72 #include <netinet/udp_var.h>
73 #ifdef INET6
74 #include <netinet/ip6.h>
75 #include <netinet6/ip6_var.h>
76 #endif /* INET6 */
77
78
79 #ifdef IPSEC
80 #include <netipsec/ipsec.h>
81 #include <netipsec/key.h>
82 #endif /* IPSEC */
83
84 #include <security/mac/mac_framework.h>
85
86 /*
87  * These configure the range of local port addresses assigned to
88  * "unspecified" outgoing connections/packets/whatever.
89  */
90 int     ipport_lowfirstauto  = IPPORT_RESERVED - 1;     /* 1023 */
91 int     ipport_lowlastauto = IPPORT_RESERVEDSTART;      /* 600 */
92 int     ipport_firstauto = IPPORT_HIFIRSTAUTO;          /* 49152 */
93 int     ipport_lastauto  = IPPORT_HILASTAUTO;           /* 65535 */
94 int     ipport_hifirstauto = IPPORT_HIFIRSTAUTO;        /* 49152 */
95 int     ipport_hilastauto  = IPPORT_HILASTAUTO;         /* 65535 */
96
97 /*
98  * Reserved ports accessible only to root. There are significant
99  * security considerations that must be accounted for when changing these,
100  * but the security benefits can be great. Please be careful.
101  */
102 int     ipport_reservedhigh = IPPORT_RESERVED - 1;      /* 1023 */
103 int     ipport_reservedlow = 0;
104
105 /* Variables dealing with random ephemeral port allocation. */
106 int     ipport_randomized = 1;  /* user controlled via sysctl */
107 int     ipport_randomcps = 10;  /* user controlled via sysctl */
108 int     ipport_randomtime = 45; /* user controlled via sysctl */
109 int     ipport_stoprandom = 0;  /* toggled by ipport_tick */
110 int     ipport_tcpallocs;
111 int     ipport_tcplastcount;
112
113 #define RANGECHK(var, min, max) \
114         if ((var) < (min)) { (var) = (min); } \
115         else if ((var) > (max)) { (var) = (max); }
116
117 static int
118 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
119 {
120         int error;
121
122         error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
123         if (error == 0) {
124                 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
125                 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
126                 RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
127                 RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
128                 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
129                 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
130         }
131         return (error);
132 }
133
134 #undef RANGECHK
135
136 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
137
138 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
139            &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
140 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
141            &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
142 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
143            &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
144 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
145            &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
146 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
147            &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
148 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
149            &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
150 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
151            CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
152 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
153            CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
154 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
155            &ipport_randomized, 0, "Enable random port allocation");
156 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
157            &ipport_randomcps, 0, "Maximum number of random port "
158            "allocations before switching to a sequental one");
159 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
160            &ipport_randomtime, 0, "Minimum time to keep sequental port "
161            "allocation before switching to a random one");
162
163 /*
164  * in_pcb.c: manage the Protocol Control Blocks.
165  *
166  * NOTE: It is assumed that most of these functions will be called with
167  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
168  * functions often modify hash chains or addresses in pcbs.
169  */
170
171 /*
172  * Allocate a PCB and associate it with the socket.
173  * On success return with the PCB locked.
174  */
175 int
176 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
177 {
178         struct inpcb *inp;
179         int error;
180
181         INP_INFO_WLOCK_ASSERT(pcbinfo);
182         error = 0;
183         inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
184         if (inp == NULL)
185                 return (ENOBUFS);
186         bzero(inp, inp_zero_size);
187         inp->inp_pcbinfo = pcbinfo;
188         inp->inp_socket = so;
189         inp->inp_cred = crhold(so->so_cred);
190         inp->inp_inc.inc_fibnum = so->so_fibnum;
191 #ifdef MAC
192         error = mac_init_inpcb(inp, M_NOWAIT);
193         if (error != 0)
194                 goto out;
195         SOCK_LOCK(so);
196         mac_create_inpcb_from_socket(so, inp);
197         SOCK_UNLOCK(so);
198 #endif
199 #ifdef IPSEC
200         error = ipsec_init_policy(so, &inp->inp_sp);
201         if (error != 0) {
202 #ifdef MAC
203                 mac_destroy_inpcb(inp);
204 #endif
205                 goto out;
206         }
207 #endif /*IPSEC*/
208 #ifdef INET6
209         if (INP_SOCKAF(so) == AF_INET6) {
210                 inp->inp_vflag |= INP_IPV6PROTO;
211                 if (ip6_v6only)
212                         inp->inp_flags |= IN6P_IPV6_V6ONLY;
213         }
214 #endif
215         LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
216         pcbinfo->ipi_count++;
217         so->so_pcb = (caddr_t)inp;
218 #ifdef INET6
219         if (ip6_auto_flowlabel)
220                 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
221 #endif
222         INP_WLOCK(inp);
223         inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
224 #if defined(IPSEC) || defined(MAC)
225 out:
226         if (error != 0) {
227                 crfree(inp->inp_cred);
228                 uma_zfree(pcbinfo->ipi_zone, inp);
229         }
230 #endif
231         return (error);
232 }
233
234 int
235 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
236 {
237         int anonport, error;
238
239         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
240         INP_WLOCK_ASSERT(inp);
241
242         if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
243                 return (EINVAL);
244         anonport = inp->inp_lport == 0 && (nam == NULL ||
245             ((struct sockaddr_in *)nam)->sin_port == 0);
246         error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
247             &inp->inp_lport, cred);
248         if (error)
249                 return (error);
250         if (in_pcbinshash(inp) != 0) {
251                 inp->inp_laddr.s_addr = INADDR_ANY;
252                 inp->inp_lport = 0;
253                 return (EAGAIN);
254         }
255         if (anonport)
256                 inp->inp_flags |= INP_ANONPORT;
257         return (0);
258 }
259
260 /*
261  * Set up a bind operation on a PCB, performing port allocation
262  * as required, but do not actually modify the PCB. Callers can
263  * either complete the bind by setting inp_laddr/inp_lport and
264  * calling in_pcbinshash(), or they can just use the resulting
265  * port and address to authorise the sending of a once-off packet.
266  *
267  * On error, the values of *laddrp and *lportp are not changed.
268  */
269 int
270 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
271     u_short *lportp, struct ucred *cred)
272 {
273         struct socket *so = inp->inp_socket;
274         unsigned short *lastport;
275         struct sockaddr_in *sin;
276         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
277         struct in_addr laddr;
278         u_short lport = 0;
279         int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
280         int error;
281         int dorandom;
282
283         /*
284          * Because no actual state changes occur here, a global write lock on
285          * the pcbinfo isn't required.
286          */
287         INP_INFO_LOCK_ASSERT(pcbinfo);
288         INP_LOCK_ASSERT(inp);
289
290         if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
291                 return (EADDRNOTAVAIL);
292         laddr.s_addr = *laddrp;
293         if (nam != NULL && laddr.s_addr != INADDR_ANY)
294                 return (EINVAL);
295         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
296                 wild = INPLOOKUP_WILDCARD;
297         if (nam == NULL) {
298                 if ((error = prison_local_ip4(cred, &laddr)) != 0)
299                         return (error);
300         } else {
301                 sin = (struct sockaddr_in *)nam;
302                 if (nam->sa_len != sizeof (*sin))
303                         return (EINVAL);
304 #ifdef notdef
305                 /*
306                  * We should check the family, but old programs
307                  * incorrectly fail to initialize it.
308                  */
309                 if (sin->sin_family != AF_INET)
310                         return (EAFNOSUPPORT);
311 #endif
312                 error = prison_local_ip4(cred, &sin->sin_addr);
313                 if (error)
314                         return (error);
315                 if (sin->sin_port != *lportp) {
316                         /* Don't allow the port to change. */
317                         if (*lportp != 0)
318                                 return (EINVAL);
319                         lport = sin->sin_port;
320                 }
321                 /* NB: lport is left as 0 if the port isn't being changed. */
322                 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
323                         /*
324                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
325                          * allow complete duplication of binding if
326                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
327                          * and a multicast address is bound on both
328                          * new and duplicated sockets.
329                          */
330                         if (so->so_options & SO_REUSEADDR)
331                                 reuseport = SO_REUSEADDR|SO_REUSEPORT;
332                 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
333                         sin->sin_port = 0;              /* yech... */
334                         bzero(&sin->sin_zero, sizeof(sin->sin_zero));
335                         if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
336                                 return (EADDRNOTAVAIL);
337                 }
338                 laddr = sin->sin_addr;
339                 if (lport) {
340                         struct inpcb *t;
341                         struct tcptw *tw;
342
343                         /* GROSS */
344                         if (ntohs(lport) <= ipport_reservedhigh &&
345                             ntohs(lport) >= ipport_reservedlow &&
346                             priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
347                             0))
348                                 return (EACCES);
349                         if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
350                             priv_check_cred(inp->inp_cred,
351                             PRIV_NETINET_REUSEPORT, 0) != 0) {
352                                 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
353                                     lport, INPLOOKUP_WILDCARD, cred);
354         /*
355          * XXX
356          * This entire block sorely needs a rewrite.
357          */
358                                 if (t &&
359                                     ((t->inp_flags & INP_TIMEWAIT) == 0) &&
360                                     (so->so_type != SOCK_STREAM ||
361                                      ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
362                                     (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
363                                      ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
364                                      (t->inp_socket->so_options &
365                                          SO_REUSEPORT) == 0) &&
366                                     (inp->inp_cred->cr_uid !=
367                                      t->inp_cred->cr_uid))
368                                         return (EADDRINUSE);
369                         }
370                         t = in_pcblookup_local(pcbinfo, sin->sin_addr,
371                             lport, wild, cred);
372                         if (t && (t->inp_flags & INP_TIMEWAIT)) {
373                                 /*
374                                  * XXXRW: If an incpb has had its timewait
375                                  * state recycled, we treat the address as
376                                  * being in use (for now).  This is better
377                                  * than a panic, but not desirable.
378                                  */
379                                 tw = intotw(inp);
380                                 if (tw == NULL ||
381                                     (reuseport & tw->tw_so_options) == 0)
382                                         return (EADDRINUSE);
383                         } else if (t &&
384                             (reuseport & t->inp_socket->so_options) == 0) {
385 #ifdef INET6
386                                 if (ntohl(sin->sin_addr.s_addr) !=
387                                     INADDR_ANY ||
388                                     ntohl(t->inp_laddr.s_addr) !=
389                                     INADDR_ANY ||
390                                     INP_SOCKAF(so) ==
391                                     INP_SOCKAF(t->inp_socket))
392 #endif
393                                 return (EADDRINUSE);
394                         }
395                 }
396         }
397         if (*lportp != 0)
398                 lport = *lportp;
399         if (lport == 0) {
400                 u_short first, last;
401                 int count;
402
403                 if (inp->inp_flags & INP_HIGHPORT) {
404                         first = ipport_hifirstauto;     /* sysctl */
405                         last  = ipport_hilastauto;
406                         lastport = &pcbinfo->ipi_lasthi;
407                 } else if (inp->inp_flags & INP_LOWPORT) {
408                         error = priv_check_cred(cred,
409                             PRIV_NETINET_RESERVEDPORT, 0);
410                         if (error)
411                                 return error;
412                         first = ipport_lowfirstauto;    /* 1023 */
413                         last  = ipport_lowlastauto;     /* 600 */
414                         lastport = &pcbinfo->ipi_lastlow;
415                 } else {
416                         first = ipport_firstauto;       /* sysctl */
417                         last  = ipport_lastauto;
418                         lastport = &pcbinfo->ipi_lastport;
419                 }
420                 /*
421                  * For UDP, use random port allocation as long as the user
422                  * allows it.  For TCP (and as of yet unknown) connections,
423                  * use random port allocation only if the user allows it AND
424                  * ipport_tick() allows it.
425                  */
426                 if (ipport_randomized &&
427                         (!ipport_stoprandom || pcbinfo == &udbinfo))
428                         dorandom = 1;
429                 else
430                         dorandom = 0;
431                 /*
432                  * It makes no sense to do random port allocation if
433                  * we have the only port available.
434                  */
435                 if (first == last)
436                         dorandom = 0;
437                 /* Make sure to not include UDP packets in the count. */
438                 if (pcbinfo != &udbinfo)
439                         ipport_tcpallocs++;
440                 /*
441                  * Instead of having two loops further down counting up or down
442                  * make sure that first is always <= last and go with only one
443                  * code path implementing all logic.
444                  *
445                  * We split the two cases (up and down) so that the direction
446                  * is not being tested on each round of the loop.
447                  */
448                 if (first > last) {
449                         /*
450                          * counting down
451                          */
452                         if (dorandom)
453                                 *lastport = first -
454                                             (arc4random() % (first - last));
455                         count = first - last;
456
457                         do {
458                                 if (count-- < 0)        /* completely used? */
459                                         return (EADDRNOTAVAIL);
460                                 --*lastport;
461                                 if (*lastport > first || *lastport < last)
462                                         *lastport = first;
463                                 lport = htons(*lastport);
464                         } while (in_pcblookup_local(pcbinfo, laddr, lport,
465                             wild, cred));
466                 } else {
467                         /*
468                          * counting up
469                          */
470                         if (dorandom)
471                                 *lastport = first +
472                                             (arc4random() % (last - first));
473                         count = last - first;
474
475                         do {
476                                 if (count-- < 0)        /* completely used? */
477                                         return (EADDRNOTAVAIL);
478                                 ++*lastport;
479                                 if (*lastport < first || *lastport > last)
480                                         *lastport = first;
481                                 lport = htons(*lastport);
482                         } while (in_pcblookup_local(pcbinfo, laddr, lport,
483                             wild, cred));
484                 }
485         }
486         *laddrp = laddr.s_addr;
487         *lportp = lport;
488         return (0);
489 }
490
491 /*
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  */
497 int
498 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
499 {
500         u_short lport, fport;
501         in_addr_t laddr, faddr;
502         int anonport, error;
503
504         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
505         INP_WLOCK_ASSERT(inp);
506
507         lport = inp->inp_lport;
508         laddr = inp->inp_laddr.s_addr;
509         anonport = (lport == 0);
510         error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
511             NULL, cred);
512         if (error)
513                 return (error);
514
515         /* Do the initial binding of the local address if required. */
516         if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
517                 inp->inp_lport = lport;
518                 inp->inp_laddr.s_addr = laddr;
519                 if (in_pcbinshash(inp) != 0) {
520                         inp->inp_laddr.s_addr = INADDR_ANY;
521                         inp->inp_lport = 0;
522                         return (EAGAIN);
523                 }
524         }
525
526         /* Commit the remaining changes. */
527         inp->inp_lport = lport;
528         inp->inp_laddr.s_addr = laddr;
529         inp->inp_faddr.s_addr = faddr;
530         inp->inp_fport = fport;
531         in_pcbrehash(inp);
532
533         if (anonport)
534                 inp->inp_flags |= INP_ANONPORT;
535         return (0);
536 }
537
538 /*
539  * Do proper source address selection on an unbound socket in case
540  * of connect. Take jails into account as well.
541  */
542 static int
543 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
544     struct ucred *cred)
545 {
546         struct in_ifaddr *ia;
547         struct ifaddr *ifa;
548         struct sockaddr *sa;
549         struct sockaddr_in *sin;
550         struct route sro;
551         int error;
552
553         KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
554
555         error = 0;
556         ia = NULL;
557         bzero(&sro, sizeof(sro));
558
559         sin = (struct sockaddr_in *)&sro.ro_dst;
560         sin->sin_family = AF_INET;
561         sin->sin_len = sizeof(struct sockaddr_in);
562         sin->sin_addr.s_addr = faddr->s_addr;
563
564         /*
565          * If route is known our src addr is taken from the i/f,
566          * else punt.
567          *
568          * Find out route to destination.
569          */
570         if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
571                 in_rtalloc_ign(&sro, RTF_CLONING, inp->inp_inc.inc_fibnum);
572
573         /*
574          * If we found a route, use the address corresponding to
575          * the outgoing interface.
576          * 
577          * Otherwise assume faddr is reachable on a directly connected
578          * network and try to find a corresponding interface to take
579          * the source address from.
580          */
581         if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
582                 struct ifnet *ifp;
583
584                 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
585                 if (ia == NULL)
586                         ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin));
587                 if (ia == NULL) {
588                         error = ENETUNREACH;
589                         goto done;
590                 }
591
592                 if (cred == NULL || !jailed(cred)) {
593                         laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
594                         goto done;
595                 }
596
597                 ifp = ia->ia_ifp;
598                 ia = NULL;
599                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
600
601                         sa = ifa->ifa_addr;
602                         if (sa->sa_family != AF_INET)
603                                 continue;
604                         sin = (struct sockaddr_in *)sa;
605                         if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
606                                 ia = (struct in_ifaddr *)ifa;
607                                 break;
608                         }
609                 }
610                 if (ia != NULL) {
611                         laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
612                         goto done;
613                 }
614
615                 /* 3. As a last resort return the 'default' jail address. */
616                 error = prison_get_ip4(cred, laddr);
617                 goto done;
618         }
619
620         /*
621          * If the outgoing interface on the route found is not
622          * a loopback interface, use the address from that interface.
623          * In case of jails do those three steps:
624          * 1. check if the interface address belongs to the jail. If so use it.
625          * 2. check if we have any address on the outgoing interface
626          *    belonging to this jail. If so use it.
627          * 3. as a last resort return the 'default' jail address.
628          */
629         if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
630
631                 /* If not jailed, use the default returned. */
632                 if (cred == NULL || !jailed(cred)) {
633                         ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
634                         laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
635                         goto done;
636                 }
637
638                 /* Jailed. */
639                 /* 1. Check if the iface address belongs to the jail. */
640                 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
641                 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
642                         ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
643                         laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
644                         goto done;
645                 }
646
647                 /*
648                  * 2. Check if we have any address on the outgoing interface
649                  *    belonging to this jail.
650                  */
651                 TAILQ_FOREACH(ifa, &sro.ro_rt->rt_ifp->if_addrhead, ifa_link) {
652
653                         sa = ifa->ifa_addr;
654                         if (sa->sa_family != AF_INET)
655                                 continue;
656                         sin = (struct sockaddr_in *)sa;
657                         if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
658                                 ia = (struct in_ifaddr *)ifa;
659                                 break;
660                         }
661                 }
662                 if (ia != NULL) {
663                         laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
664                         goto done;
665                 }
666
667                 /* 3. As a last resort return the 'default' jail address. */
668                 error = prison_get_ip4(cred, laddr);
669                 goto done;
670         }
671
672         /*
673          * The outgoing interface is marked with 'loopback net', so a route
674          * to ourselves is here.
675          * Try to find the interface of the destination address and then
676          * take the address from there. That interface is not necessarily
677          * a loopback interface.
678          * In case of jails, check that it is an address of the jail
679          * and if we cannot find, fall back to the 'default' jail address.
680          */
681         if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
682                 struct sockaddr_in sain;
683
684                 bzero(&sain, sizeof(struct sockaddr_in));
685                 sain.sin_family = AF_INET;
686                 sain.sin_len = sizeof(struct sockaddr_in);
687                 sain.sin_addr.s_addr = faddr->s_addr;
688
689                 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
690                 if (ia == NULL)
691                         ia = ifatoia(ifa_ifwithnet(sintosa(&sain)));
692
693                 if (cred == NULL || !jailed(cred)) {
694 #if __FreeBSD_version < 800000
695                         if (ia == NULL)
696                                 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
697 #endif
698                         if (ia == NULL) {
699                                 error = ENETUNREACH;
700                                 goto done;
701                         }
702                         laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
703                         goto done;
704                 }
705
706                 /* Jailed. */
707                 if (ia != NULL) {
708                         struct ifnet *ifp;
709
710                         ifp = ia->ia_ifp;
711                         ia = NULL;
712                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
713
714                                 sa = ifa->ifa_addr;
715                                 if (sa->sa_family != AF_INET)
716                                         continue;
717                                 sin = (struct sockaddr_in *)sa;
718                                 if (prison_check_ip4(cred,
719                                     &sin->sin_addr) == 0) {
720                                         ia = (struct in_ifaddr *)ifa;
721                                         break;
722                                 }
723                         }
724                         if (ia != NULL) {
725                                 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
726                                 goto done;
727                         }
728                 }
729
730                 /* 3. As a last resort return the 'default' jail address. */
731                 error = prison_get_ip4(cred, laddr);
732                 goto done;
733         }
734
735 done:
736         if (sro.ro_rt != NULL)
737                 RTFREE(sro.ro_rt);
738         return (error);
739 }
740
741 /*
742  * Set up for a connect from a socket to the specified address.
743  * On entry, *laddrp and *lportp should contain the current local
744  * address and port for the PCB; these are updated to the values
745  * that should be placed in inp_laddr and inp_lport to complete
746  * the connect.
747  *
748  * On success, *faddrp and *fportp will be set to the remote address
749  * and port. These are not updated in the error case.
750  *
751  * If the operation fails because the connection already exists,
752  * *oinpp will be set to the PCB of that connection so that the
753  * caller can decide to override it. In all other cases, *oinpp
754  * is set to NULL.
755  */
756 int
757 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
758     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
759     struct inpcb **oinpp, struct ucred *cred)
760 {
761         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
762         struct in_ifaddr *ia;
763         struct inpcb *oinp;
764         struct in_addr laddr, faddr;
765         u_short lport, fport;
766         int error;
767
768         /*
769          * Because a global state change doesn't actually occur here, a read
770          * lock is sufficient.
771          */
772         INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo);
773         INP_LOCK_ASSERT(inp);
774
775         if (oinpp != NULL)
776                 *oinpp = NULL;
777         if (nam->sa_len != sizeof (*sin))
778                 return (EINVAL);
779         if (sin->sin_family != AF_INET)
780                 return (EAFNOSUPPORT);
781         if (sin->sin_port == 0)
782                 return (EADDRNOTAVAIL);
783         laddr.s_addr = *laddrp;
784         lport = *lportp;
785         faddr = sin->sin_addr;
786         fport = sin->sin_port;
787
788         if (!TAILQ_EMPTY(&in_ifaddrhead)) {
789                 /*
790                  * If the destination address is INADDR_ANY,
791                  * use the primary local address.
792                  * If the supplied address is INADDR_BROADCAST,
793                  * and the primary interface supports broadcast,
794                  * choose the broadcast address for that interface.
795                  */
796                 if (faddr.s_addr == INADDR_ANY) {
797                         faddr =
798                             IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
799                         if (cred != NULL &&
800                             (error = prison_get_ip4(cred, &faddr)) != 0)
801                                 return (error);
802                 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
803                     (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
804                     IFF_BROADCAST))
805                         faddr = satosin(&TAILQ_FIRST(
806                             &in_ifaddrhead)->ia_broadaddr)->sin_addr;
807         }
808         if (laddr.s_addr == INADDR_ANY) {
809                 error = in_pcbladdr(inp, &faddr, &laddr, cred);
810                 if (error)
811                         return (error);
812
813                 /*
814                  * If the destination address is multicast and an outgoing
815                  * interface has been set as a multicast option, use the
816                  * address of that interface as our source address.
817                  */
818                 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
819                     inp->inp_moptions != NULL) {
820                         struct ip_moptions *imo;
821                         struct ifnet *ifp;
822
823                         imo = inp->inp_moptions;
824                         if (imo->imo_multicast_ifp != NULL) {
825                                 ifp = imo->imo_multicast_ifp;
826                                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
827                                         if (ia->ia_ifp == ifp)
828                                                 break;
829                                 if (ia == NULL)
830                                         return (EADDRNOTAVAIL);
831                                 laddr = ia->ia_addr.sin_addr;
832                         }
833                 }
834         }
835
836         oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
837             0, NULL);
838         if (oinp != NULL) {
839                 if (oinpp != NULL)
840                         *oinpp = oinp;
841                 return (EADDRINUSE);
842         }
843         if (lport == 0) {
844                 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
845                     cred);
846                 if (error)
847                         return (error);
848         }
849         *laddrp = laddr.s_addr;
850         *lportp = lport;
851         *faddrp = faddr.s_addr;
852         *fportp = fport;
853         return (0);
854 }
855
856 void
857 in_pcbdisconnect(struct inpcb *inp)
858 {
859
860         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
861         INP_WLOCK_ASSERT(inp);
862
863         inp->inp_faddr.s_addr = INADDR_ANY;
864         inp->inp_fport = 0;
865         in_pcbrehash(inp);
866 }
867
868 /*
869  * Historically, in_pcbdetach() included the functionality now found in
870  * in_pcbfree() and in_pcbdrop().  They are now broken out to reflect the
871  * more complex life cycle of TCP.
872  *
873  * in_pcbdetach() is responsibe for disconnecting the socket from an inpcb.
874  * For most protocols, this will be invoked immediately prior to calling
875  * in_pcbfree().  However, for TCP the inpcb may significantly outlive the
876  * socket, in which case in_pcbfree() may be deferred.
877  */
878 void
879 in_pcbdetach(struct inpcb *inp)
880 {
881
882         KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
883
884         inp->inp_socket->so_pcb = NULL;
885         inp->inp_socket = NULL;
886 }
887
888 /*
889  * in_pcbfree() is responsible for freeing an already-detached inpcb, as well
890  * as removing it from any global inpcb lists it might be on.
891  */
892 void
893 in_pcbfree(struct inpcb *inp)
894 {
895         struct inpcbinfo *ipi = inp->inp_pcbinfo;
896
897         KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
898
899         INP_INFO_WLOCK_ASSERT(ipi);
900         INP_WLOCK_ASSERT(inp);
901
902 #ifdef IPSEC
903         if (inp->inp_sp != NULL)
904                 ipsec_delete_pcbpolicy(inp);
905 #endif /* IPSEC */
906         inp->inp_gencnt = ++ipi->ipi_gencnt;
907         in_pcbremlists(inp);
908 #ifdef INET6
909         if (inp->inp_vflag & INP_IPV6PROTO) {
910                 ip6_freepcbopts(inp->in6p_outputopts);
911                 ip6_freemoptions(inp->in6p_moptions);
912         }
913 #endif
914         if (inp->inp_options)
915                 (void)m_free(inp->inp_options);
916         if (inp->inp_moptions != NULL)
917                 inp_freemoptions(inp->inp_moptions);
918         inp->inp_vflag = 0;
919         crfree(inp->inp_cred);
920
921 #ifdef MAC
922         mac_destroy_inpcb(inp);
923 #endif
924         INP_WUNLOCK(inp);
925         uma_zfree(ipi->ipi_zone, inp);
926 }
927
928 /*
929  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
930  * port reservation, and preventing it from being returned by inpcb lookups.
931  *
932  * It is used by TCP to mark an inpcb as unused and avoid future packet
933  * delivery or event notification when a socket remains open but TCP has
934  * closed.  This might occur as a result of a shutdown()-initiated TCP close
935  * or a RST on the wire, and allows the port binding to be reused while still
936  * maintaining the invariant that so_pcb always points to a valid inpcb until
937  * in_pcbdetach().
938  *
939  * XXXRW: An inp_lport of 0 is used to indicate that the inpcb is not on hash
940  * lists, but can lead to confusing netstat output, as open sockets with
941  * closed TCP connections will no longer appear to have their bound port
942  * number.  An explicit flag would be better, as it would allow us to leave
943  * the port number intact after the connection is dropped.
944  *
945  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
946  * in_pcbnotifyall() and in_pcbpurgeif0()?
947  */
948 void
949 in_pcbdrop(struct inpcb *inp)
950 {
951
952         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
953         INP_WLOCK_ASSERT(inp);
954
955         inp->inp_flags |= INP_DROPPED;
956         if (inp->inp_flags & INP_INHASHLIST) {
957                 struct inpcbport *phd = inp->inp_phd;
958
959                 LIST_REMOVE(inp, inp_hash);
960                 LIST_REMOVE(inp, inp_portlist);
961                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
962                         LIST_REMOVE(phd, phd_hash);
963                         free(phd, M_PCB);
964                 }
965                 inp->inp_flags &= ~INP_INHASHLIST;
966         }
967 }
968
969 /*
970  * Common routines to return the socket addresses associated with inpcbs.
971  */
972 struct sockaddr *
973 in_sockaddr(in_port_t port, struct in_addr *addr_p)
974 {
975         struct sockaddr_in *sin;
976
977         MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
978                 M_WAITOK | M_ZERO);
979         sin->sin_family = AF_INET;
980         sin->sin_len = sizeof(*sin);
981         sin->sin_addr = *addr_p;
982         sin->sin_port = port;
983
984         return (struct sockaddr *)sin;
985 }
986
987 int
988 in_getsockaddr(struct socket *so, struct sockaddr **nam)
989 {
990         struct inpcb *inp;
991         struct in_addr addr;
992         in_port_t port;
993
994         inp = sotoinpcb(so);
995         KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
996
997         INP_RLOCK(inp);
998         port = inp->inp_lport;
999         addr = inp->inp_laddr;
1000         INP_RUNLOCK(inp);
1001
1002         *nam = in_sockaddr(port, &addr);
1003         return 0;
1004 }
1005
1006 int
1007 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1008 {
1009         struct inpcb *inp;
1010         struct in_addr addr;
1011         in_port_t port;
1012
1013         inp = sotoinpcb(so);
1014         KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1015
1016         INP_RLOCK(inp);
1017         port = inp->inp_fport;
1018         addr = inp->inp_faddr;
1019         INP_RUNLOCK(inp);
1020
1021         *nam = in_sockaddr(port, &addr);
1022         return 0;
1023 }
1024
1025 void
1026 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1027     struct inpcb *(*notify)(struct inpcb *, int))
1028 {
1029         struct inpcb *inp, *inp_temp;
1030
1031         INP_INFO_WLOCK(pcbinfo);
1032         LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1033                 INP_WLOCK(inp);
1034 #ifdef INET6
1035                 if ((inp->inp_vflag & INP_IPV4) == 0) {
1036                         INP_WUNLOCK(inp);
1037                         continue;
1038                 }
1039 #endif
1040                 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1041                     inp->inp_socket == NULL) {
1042                         INP_WUNLOCK(inp);
1043                         continue;
1044                 }
1045                 if ((*notify)(inp, errno))
1046                         INP_WUNLOCK(inp);
1047         }
1048         INP_INFO_WUNLOCK(pcbinfo);
1049 }
1050
1051 void
1052 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1053 {
1054         struct inpcb *inp;
1055         struct ip_moptions *imo;
1056         int i, gap;
1057
1058         INP_INFO_RLOCK(pcbinfo);
1059         LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1060                 INP_WLOCK(inp);
1061                 imo = inp->inp_moptions;
1062                 if ((inp->inp_vflag & INP_IPV4) &&
1063                     imo != NULL) {
1064                         /*
1065                          * Unselect the outgoing interface if it is being
1066                          * detached.
1067                          */
1068                         if (imo->imo_multicast_ifp == ifp)
1069                                 imo->imo_multicast_ifp = NULL;
1070
1071                         /*
1072                          * Drop multicast group membership if we joined
1073                          * through the interface being detached.
1074                          */
1075                         for (i = 0, gap = 0; i < imo->imo_num_memberships;
1076                             i++) {
1077                                 if (imo->imo_membership[i]->inm_ifp == ifp) {
1078                                         in_delmulti(imo->imo_membership[i]);
1079                                         gap++;
1080                                 } else if (gap != 0)
1081                                         imo->imo_membership[i - gap] =
1082                                             imo->imo_membership[i];
1083                         }
1084                         imo->imo_num_memberships -= gap;
1085                 }
1086                 INP_WUNLOCK(inp);
1087         }
1088         INP_INFO_RUNLOCK(pcbinfo);
1089 }
1090
1091 /*
1092  * Lookup a PCB based on the local address and port.
1093  */
1094 #define INP_LOOKUP_MAPPED_PCB_COST      3
1095 struct inpcb *
1096 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1097     u_short lport, int wild_okay, struct ucred *cred)
1098 {
1099         struct inpcb *inp;
1100 #ifdef INET6
1101         int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1102 #else
1103         int matchwild = 3;
1104 #endif
1105         int wildcard;
1106
1107         INP_INFO_LOCK_ASSERT(pcbinfo);
1108
1109         if (!wild_okay) {
1110                 struct inpcbhead *head;
1111                 /*
1112                  * Look for an unconnected (wildcard foreign addr) PCB that
1113                  * matches the local address and port we're looking for.
1114                  */
1115                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1116                     0, pcbinfo->ipi_hashmask)];
1117                 LIST_FOREACH(inp, head, inp_hash) {
1118 #ifdef INET6
1119                         /* XXX inp locking */
1120                         if ((inp->inp_vflag & INP_IPV4) == 0)
1121                                 continue;
1122 #endif
1123                         if (inp->inp_faddr.s_addr == INADDR_ANY &&
1124                             inp->inp_laddr.s_addr == laddr.s_addr &&
1125                             inp->inp_lport == lport) {
1126                                 /*
1127                                  * Found?
1128                                  */
1129                                 if (cred == NULL ||
1130                                     inp->inp_cred->cr_prison == cred->cr_prison)
1131                                         return (inp);
1132                         }
1133                 }
1134                 /*
1135                  * Not found.
1136                  */
1137                 return (NULL);
1138         } else {
1139                 struct inpcbporthead *porthash;
1140                 struct inpcbport *phd;
1141                 struct inpcb *match = NULL;
1142                 /*
1143                  * Best fit PCB lookup.
1144                  *
1145                  * First see if this local port is in use by looking on the
1146                  * port hash list.
1147                  */
1148                 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1149                     pcbinfo->ipi_porthashmask)];
1150                 LIST_FOREACH(phd, porthash, phd_hash) {
1151                         if (phd->phd_port == lport)
1152                                 break;
1153                 }
1154                 if (phd != NULL) {
1155                         /*
1156                          * Port is in use by one or more PCBs. Look for best
1157                          * fit.
1158                          */
1159                         LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1160                                 wildcard = 0;
1161                                 if (cred != NULL &&
1162                                     inp->inp_cred->cr_prison != cred->cr_prison)
1163                                         continue;
1164 #ifdef INET6
1165                                 /* XXX inp locking */
1166                                 if ((inp->inp_vflag & INP_IPV4) == 0)
1167                                         continue;
1168                                 /*
1169                                  * We never select the PCB that has
1170                                  * INP_IPV6 flag and is bound to :: if
1171                                  * we have another PCB which is bound
1172                                  * to 0.0.0.0.  If a PCB has the
1173                                  * INP_IPV6 flag, then we set its cost
1174                                  * higher than IPv4 only PCBs.
1175                                  *
1176                                  * Note that the case only happens
1177                                  * when a socket is bound to ::, under
1178                                  * the condition that the use of the
1179                                  * mapped address is allowed.
1180                                  */
1181                                 if ((inp->inp_vflag & INP_IPV6) != 0)
1182                                         wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1183 #endif
1184                                 if (inp->inp_faddr.s_addr != INADDR_ANY)
1185                                         wildcard++;
1186                                 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1187                                         if (laddr.s_addr == INADDR_ANY)
1188                                                 wildcard++;
1189                                         else if (inp->inp_laddr.s_addr != laddr.s_addr)
1190                                                 continue;
1191                                 } else {
1192                                         if (laddr.s_addr != INADDR_ANY)
1193                                                 wildcard++;
1194                                 }
1195                                 if (wildcard < matchwild) {
1196                                         match = inp;
1197                                         matchwild = wildcard;
1198                                         if (matchwild == 0)
1199                                                 break;
1200                                 }
1201                         }
1202                 }
1203                 return (match);
1204         }
1205 }
1206 #undef INP_LOOKUP_MAPPED_PCB_COST
1207
1208 /*
1209  * Lookup PCB in hash list.
1210  */
1211 struct inpcb *
1212 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1213     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1214     struct ifnet *ifp)
1215 {
1216         struct inpcbhead *head;
1217         struct inpcb *inp, *tmpinp;
1218         u_short fport = fport_arg, lport = lport_arg;
1219
1220         INP_INFO_LOCK_ASSERT(pcbinfo);
1221
1222         /*
1223          * First look for an exact match.
1224          */
1225         tmpinp = NULL;
1226         head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1227             pcbinfo->ipi_hashmask)];
1228         LIST_FOREACH(inp, head, inp_hash) {
1229 #ifdef INET6
1230                 /* XXX inp locking */
1231                 if ((inp->inp_vflag & INP_IPV4) == 0)
1232                         continue;
1233 #endif
1234                 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1235                     inp->inp_laddr.s_addr == laddr.s_addr &&
1236                     inp->inp_fport == fport &&
1237                     inp->inp_lport == lport) {
1238                         /*
1239                          * XXX We should be able to directly return
1240                          * the inp here, without any checks.
1241                          * Well unless both bound with SO_REUSEPORT?
1242                          */
1243                         if (jailed(inp->inp_cred))
1244                                 return (inp);
1245                         if (tmpinp == NULL)
1246                                 tmpinp = inp;
1247                 }
1248         }
1249         if (tmpinp != NULL)
1250                 return (tmpinp);
1251
1252         /*
1253          * Then look for a wildcard match, if requested.
1254          */
1255         if (wildcard == INPLOOKUP_WILDCARD) {
1256                 struct inpcb *local_wild = NULL, *local_exact = NULL;
1257 #ifdef INET6
1258                 struct inpcb *local_wild_mapped = NULL;
1259 #endif
1260                 struct inpcb *jail_wild = NULL;
1261                 int injail;
1262
1263                 /*
1264                  * Order of socket selection - we always prefer jails.
1265                  *      1. jailed, non-wild.
1266                  *      2. jailed, wild.
1267                  *      3. non-jailed, non-wild.
1268                  *      4. non-jailed, wild.
1269                  */
1270
1271                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1272                     0, pcbinfo->ipi_hashmask)];
1273                 LIST_FOREACH(inp, head, inp_hash) {
1274 #ifdef INET6
1275                         /* XXX inp locking */
1276                         if ((inp->inp_vflag & INP_IPV4) == 0)
1277                                 continue;
1278 #endif
1279                         if (inp->inp_faddr.s_addr != INADDR_ANY ||
1280                             inp->inp_lport != lport)
1281                                 continue;
1282
1283                         /* XXX inp locking */
1284                         if (ifp && ifp->if_type == IFT_FAITH &&
1285                             (inp->inp_flags & INP_FAITH) == 0)
1286                                 continue;
1287
1288                         injail = jailed(inp->inp_cred);
1289                         if (injail) {
1290                                 if (prison_check_ip4(inp->inp_cred,
1291                                     &laddr) != 0)
1292                                         continue;
1293                         } else {
1294                                 if (local_exact != NULL)
1295                                         continue;
1296                         }
1297
1298                         if (inp->inp_laddr.s_addr == laddr.s_addr) {
1299                                 if (injail)
1300                                         return (inp);
1301                                 else
1302                                         local_exact = inp;
1303                         } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1304 #ifdef INET6
1305                                 /* XXX inp locking, NULL check */
1306                                 if (inp->inp_vflag & INP_IPV6PROTO)
1307                                         local_wild_mapped = inp;
1308                                 else
1309 #endif /* INET6 */
1310                                         if (injail)
1311                                                 jail_wild = inp;
1312                                         else
1313                                                 local_wild = inp;
1314                         }
1315                 } /* LIST_FOREACH */
1316                 if (jail_wild != NULL)
1317                         return (jail_wild);
1318                 if (local_exact != NULL)
1319                         return (local_exact);
1320                 if (local_wild != NULL)
1321                         return (local_wild);
1322 #ifdef INET6
1323                 if (local_wild_mapped != NULL)
1324                         return (local_wild_mapped);
1325 #endif /* defined(INET6) */
1326         } /* if (wildcard == INPLOOKUP_WILDCARD) */
1327
1328         return (NULL);
1329 }
1330
1331 /*
1332  * Insert PCB onto various hash lists.
1333  */
1334 int
1335 in_pcbinshash(struct inpcb *inp)
1336 {
1337         struct inpcbhead *pcbhash;
1338         struct inpcbporthead *pcbporthash;
1339         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1340         struct inpcbport *phd;
1341         u_int32_t hashkey_faddr;
1342
1343         INP_INFO_WLOCK_ASSERT(pcbinfo);
1344         INP_WLOCK_ASSERT(inp);
1345         KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
1346             ("in_pcbinshash: INP_INHASHLIST"));
1347
1348 #ifdef INET6
1349         if (inp->inp_vflag & INP_IPV6)
1350                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1351         else
1352 #endif /* INET6 */
1353         hashkey_faddr = inp->inp_faddr.s_addr;
1354
1355         pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1356                  inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1357
1358         pcbporthash = &pcbinfo->ipi_porthashbase[
1359             INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1360
1361         /*
1362          * Go through port list and look for a head for this lport.
1363          */
1364         LIST_FOREACH(phd, pcbporthash, phd_hash) {
1365                 if (phd->phd_port == inp->inp_lport)
1366                         break;
1367         }
1368         /*
1369          * If none exists, malloc one and tack it on.
1370          */
1371         if (phd == NULL) {
1372                 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1373                 if (phd == NULL) {
1374                         return (ENOBUFS); /* XXX */
1375                 }
1376                 phd->phd_port = inp->inp_lport;
1377                 LIST_INIT(&phd->phd_pcblist);
1378                 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1379         }
1380         inp->inp_phd = phd;
1381         LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1382         LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1383         inp->inp_flags |= INP_INHASHLIST;
1384         return (0);
1385 }
1386
1387 /*
1388  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1389  * changed. NOTE: This does not handle the case of the lport changing (the
1390  * hashed port list would have to be updated as well), so the lport must
1391  * not change after in_pcbinshash() has been called.
1392  */
1393 void
1394 in_pcbrehash(struct inpcb *inp)
1395 {
1396         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1397         struct inpcbhead *head;
1398         u_int32_t hashkey_faddr;
1399
1400         INP_INFO_WLOCK_ASSERT(pcbinfo);
1401         INP_WLOCK_ASSERT(inp);
1402         KASSERT(inp->inp_flags & INP_INHASHLIST,
1403             ("in_pcbrehash: !INP_INHASHLIST"));
1404
1405 #ifdef INET6
1406         if (inp->inp_vflag & INP_IPV6)
1407                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1408         else
1409 #endif /* INET6 */
1410         hashkey_faddr = inp->inp_faddr.s_addr;
1411
1412         head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1413                 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1414
1415         LIST_REMOVE(inp, inp_hash);
1416         LIST_INSERT_HEAD(head, inp, inp_hash);
1417 }
1418
1419 /*
1420  * Remove PCB from various lists.
1421  */
1422 void
1423 in_pcbremlists(struct inpcb *inp)
1424 {
1425         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1426
1427         INP_INFO_WLOCK_ASSERT(pcbinfo);
1428         INP_WLOCK_ASSERT(inp);
1429
1430         inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1431         if (inp->inp_flags & INP_INHASHLIST) {
1432                 struct inpcbport *phd = inp->inp_phd;
1433
1434                 LIST_REMOVE(inp, inp_hash);
1435                 LIST_REMOVE(inp, inp_portlist);
1436                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1437                         LIST_REMOVE(phd, phd_hash);
1438                         free(phd, M_PCB);
1439                 }
1440                 inp->inp_flags &= ~INP_INHASHLIST;
1441         }
1442         LIST_REMOVE(inp, inp_list);
1443         pcbinfo->ipi_count--;
1444 }
1445
1446 /*
1447  * A set label operation has occurred at the socket layer, propagate the
1448  * label change into the in_pcb for the socket.
1449  */
1450 void
1451 in_pcbsosetlabel(struct socket *so)
1452 {
1453 #ifdef MAC
1454         struct inpcb *inp;
1455
1456         inp = sotoinpcb(so);
1457         KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1458
1459         INP_WLOCK(inp);
1460         SOCK_LOCK(so);
1461         mac_inpcb_sosetlabel(so, inp);
1462         SOCK_UNLOCK(so);
1463         INP_WUNLOCK(inp);
1464 #endif
1465 }
1466
1467 /*
1468  * ipport_tick runs once per second, determining if random port allocation
1469  * should be continued.  If more than ipport_randomcps ports have been
1470  * allocated in the last second, then we return to sequential port
1471  * allocation. We return to random allocation only once we drop below
1472  * ipport_randomcps for at least ipport_randomtime seconds.
1473  */
1474 void
1475 ipport_tick(void *xtp)
1476 {
1477
1478         if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) {
1479                 if (ipport_stoprandom > 0)
1480                         ipport_stoprandom--;
1481         } else
1482                 ipport_stoprandom = ipport_randomtime;
1483         ipport_tcplastcount = ipport_tcpallocs;
1484         callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
1485 }
1486
1487 void
1488 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
1489 {
1490         struct inpcb *inp;
1491
1492         INP_INFO_RLOCK(&tcbinfo);
1493         LIST_FOREACH(inp, tcbinfo.ipi_listhead, inp_list) {
1494                 INP_WLOCK(inp);
1495                 func(inp, arg);
1496                 INP_WUNLOCK(inp);
1497         }
1498         INP_INFO_RUNLOCK(&tcbinfo);
1499 }
1500
1501 struct socket *
1502 inp_inpcbtosocket(struct inpcb *inp)
1503 {
1504
1505         INP_WLOCK_ASSERT(inp);
1506         return (inp->inp_socket);
1507 }
1508
1509 struct tcpcb *
1510 inp_inpcbtotcpcb(struct inpcb *inp)
1511 {
1512
1513         INP_WLOCK_ASSERT(inp);
1514         return ((struct tcpcb *)inp->inp_ppcb);
1515 }
1516
1517 int
1518 inp_ip_tos_get(const struct inpcb *inp)
1519 {
1520
1521         return (inp->inp_ip_tos);
1522 }
1523
1524 void
1525 inp_ip_tos_set(struct inpcb *inp, int val)
1526 {
1527
1528         inp->inp_ip_tos = val;
1529 }
1530
1531 void
1532 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
1533     uint32_t *faddr, uint16_t *fp)
1534 {
1535
1536         INP_LOCK_ASSERT(inp);
1537         *laddr = inp->inp_laddr.s_addr;
1538         *faddr = inp->inp_faddr.s_addr;
1539         *lp = inp->inp_lport;
1540         *fp = inp->inp_fport;
1541 }
1542
1543 struct inpcb *
1544 so_sotoinpcb(struct socket *so)
1545 {
1546
1547         return (sotoinpcb(so));
1548 }
1549
1550 struct tcpcb *
1551 so_sototcpcb(struct socket *so)
1552 {
1553
1554         return (sototcpcb(so));
1555 }
1556
1557 void
1558 inp_wlock(struct inpcb *inp)
1559 {
1560
1561         INP_WLOCK(inp);
1562 }
1563
1564 void
1565 inp_wunlock(struct inpcb *inp)
1566 {
1567
1568         INP_WUNLOCK(inp);
1569 }
1570
1571 void
1572 inp_rlock(struct inpcb *inp)
1573 {
1574
1575         INP_RLOCK(inp);
1576 }
1577
1578 void
1579 inp_runlock(struct inpcb *inp)
1580 {
1581
1582         INP_RUNLOCK(inp);
1583 }
1584
1585 #ifdef INVARIANTS
1586 void
1587 inp_wlock_assert(struct inpcb *inp)
1588 {
1589
1590         INP_WLOCK_ASSERT(inp);
1591 }
1592
1593 void
1594 inp_rlock_assert(struct inpcb *inp)
1595 {
1596
1597         INP_RLOCK_ASSERT(inp);
1598 }
1599
1600 void
1601 inp_lock_assert(struct inpcb *inp)
1602 {
1603
1604         INP_LOCK_ASSERT(inp);
1605 }
1606
1607 void
1608 inp_unlock_assert(struct inpcb *inp)
1609 {
1610
1611         INP_UNLOCK_ASSERT(inp);
1612 }
1613
1614 #endif
1615
1616 #ifdef DDB
1617 static void
1618 db_print_indent(int indent)
1619 {
1620         int i;
1621
1622         for (i = 0; i < indent; i++)
1623                 db_printf(" ");
1624 }
1625
1626 static void
1627 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1628 {
1629         char faddr_str[48], laddr_str[48];
1630
1631         db_print_indent(indent);
1632         db_printf("%s at %p\n", name, inc);
1633
1634         indent += 2;
1635
1636 #ifdef INET6
1637         if (inc->inc_flags & INC_ISIPV6) {
1638                 /* IPv6. */
1639                 ip6_sprintf(laddr_str, &inc->inc6_laddr);
1640                 ip6_sprintf(faddr_str, &inc->inc6_faddr);
1641         } else {
1642 #endif
1643                 /* IPv4. */
1644                 inet_ntoa_r(inc->inc_laddr, laddr_str);
1645                 inet_ntoa_r(inc->inc_faddr, faddr_str);
1646 #ifdef INET6
1647         }
1648 #endif
1649         db_print_indent(indent);
1650         db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
1651             ntohs(inc->inc_lport));
1652         db_print_indent(indent);
1653         db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
1654             ntohs(inc->inc_fport));
1655 }
1656
1657 static void
1658 db_print_inpflags(int inp_flags)
1659 {
1660         int comma;
1661
1662         comma = 0;
1663         if (inp_flags & INP_RECVOPTS) {
1664                 db_printf("%sINP_RECVOPTS", comma ? ", " : "");
1665                 comma = 1;
1666         }
1667         if (inp_flags & INP_RECVRETOPTS) {
1668                 db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
1669                 comma = 1;
1670         }
1671         if (inp_flags & INP_RECVDSTADDR) {
1672                 db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
1673                 comma = 1;
1674         }
1675         if (inp_flags & INP_HDRINCL) {
1676                 db_printf("%sINP_HDRINCL", comma ? ", " : "");
1677                 comma = 1;
1678         }
1679         if (inp_flags & INP_HIGHPORT) {
1680                 db_printf("%sINP_HIGHPORT", comma ? ", " : "");
1681                 comma = 1;
1682         }
1683         if (inp_flags & INP_LOWPORT) {
1684                 db_printf("%sINP_LOWPORT", comma ? ", " : "");
1685                 comma = 1;
1686         }
1687         if (inp_flags & INP_ANONPORT) {
1688                 db_printf("%sINP_ANONPORT", comma ? ", " : "");
1689                 comma = 1;
1690         }
1691         if (inp_flags & INP_RECVIF) {
1692                 db_printf("%sINP_RECVIF", comma ? ", " : "");
1693                 comma = 1;
1694         }
1695         if (inp_flags & INP_MTUDISC) {
1696                 db_printf("%sINP_MTUDISC", comma ? ", " : "");
1697                 comma = 1;
1698         }
1699         if (inp_flags & INP_FAITH) {
1700                 db_printf("%sINP_FAITH", comma ? ", " : "");
1701                 comma = 1;
1702         }
1703         if (inp_flags & INP_RECVTTL) {
1704                 db_printf("%sINP_RECVTTL", comma ? ", " : "");
1705                 comma = 1;
1706         }
1707         if (inp_flags & INP_DONTFRAG) {
1708                 db_printf("%sINP_DONTFRAG", comma ? ", " : "");
1709                 comma = 1;
1710         }
1711         if (inp_flags & IN6P_IPV6_V6ONLY) {
1712                 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
1713                 comma = 1;
1714         }
1715         if (inp_flags & IN6P_PKTINFO) {
1716                 db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
1717                 comma = 1;
1718         }
1719         if (inp_flags & IN6P_HOPLIMIT) {
1720                 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
1721                 comma = 1;
1722         }
1723         if (inp_flags & IN6P_HOPOPTS) {
1724                 db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
1725                 comma = 1;
1726         }
1727         if (inp_flags & IN6P_DSTOPTS) {
1728                 db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
1729                 comma = 1;
1730         }
1731         if (inp_flags & IN6P_RTHDR) {
1732                 db_printf("%sIN6P_RTHDR", comma ? ", " : "");
1733                 comma = 1;
1734         }
1735         if (inp_flags & IN6P_RTHDRDSTOPTS) {
1736                 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
1737                 comma = 1;
1738         }
1739         if (inp_flags & IN6P_TCLASS) {
1740                 db_printf("%sIN6P_TCLASS", comma ? ", " : "");
1741                 comma = 1;
1742         }
1743         if (inp_flags & IN6P_AUTOFLOWLABEL) {
1744                 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
1745                 comma = 1;
1746         }
1747         if (inp_flags & INP_TIMEWAIT) {
1748                 db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
1749                 comma  = 1;
1750         }
1751         if (inp_flags & INP_ONESBCAST) {
1752                 db_printf("%sINP_ONESBCAST", comma ? ", " : "");
1753                 comma  = 1;
1754         }
1755         if (inp_flags & INP_DROPPED) {
1756                 db_printf("%sINP_DROPPED", comma ? ", " : "");
1757                 comma  = 1;
1758         }
1759         if (inp_flags & INP_SOCKREF) {
1760                 db_printf("%sINP_SOCKREF", comma ? ", " : "");
1761                 comma  = 1;
1762         }
1763         if (inp_flags & IN6P_RFC2292) {
1764                 db_printf("%sIN6P_RFC2292", comma ? ", " : "");
1765                 comma = 1;
1766         }
1767         if (inp_flags & IN6P_MTU) {
1768                 db_printf("IN6P_MTU%s", comma ? ", " : "");
1769                 comma = 1;
1770         }
1771 }
1772
1773 static void
1774 db_print_inpvflag(u_char inp_vflag)
1775 {
1776         int comma;
1777
1778         comma = 0;
1779         if (inp_vflag & INP_IPV4) {
1780                 db_printf("%sINP_IPV4", comma ? ", " : "");
1781                 comma  = 1;
1782         }
1783         if (inp_vflag & INP_IPV6) {
1784                 db_printf("%sINP_IPV6", comma ? ", " : "");
1785                 comma  = 1;
1786         }
1787         if (inp_vflag & INP_IPV6PROTO) {
1788                 db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
1789                 comma  = 1;
1790         }
1791 }
1792
1793 void
1794 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
1795 {
1796
1797         db_print_indent(indent);
1798         db_printf("%s at %p\n", name, inp);
1799
1800         indent += 2;
1801
1802         db_print_indent(indent);
1803         db_printf("inp_flow: 0x%x\n", inp->inp_flow);
1804
1805         db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
1806
1807         db_print_indent(indent);
1808         db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
1809             inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
1810
1811         db_print_indent(indent);
1812         db_printf("inp_label: %p   inp_flags: 0x%x (",
1813            inp->inp_label, inp->inp_flags);
1814         db_print_inpflags(inp->inp_flags);
1815         db_printf(")\n");
1816
1817         db_print_indent(indent);
1818         db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
1819             inp->inp_vflag);
1820         db_print_inpvflag(inp->inp_vflag);
1821         db_printf(")\n");
1822
1823         db_print_indent(indent);
1824         db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
1825             inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
1826
1827         db_print_indent(indent);
1828 #ifdef INET6
1829         if (inp->inp_vflag & INP_IPV6) {
1830                 db_printf("in6p_options: %p   in6p_outputopts: %p   "
1831                     "in6p_moptions: %p\n", inp->in6p_options,
1832                     inp->in6p_outputopts, inp->in6p_moptions);
1833                 db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
1834                     "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
1835                     inp->in6p_hops);
1836         } else
1837 #endif
1838         {
1839                 db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
1840                     "inp_ip_moptions: %p\n", inp->inp_ip_tos,
1841                     inp->inp_options, inp->inp_moptions);
1842         }
1843
1844         db_print_indent(indent);
1845         db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
1846             (uintmax_t)inp->inp_gencnt);
1847 }
1848
1849 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
1850 {
1851         struct inpcb *inp;
1852
1853         if (!have_addr) {
1854                 db_printf("usage: show inpcb <addr>\n");
1855                 return;
1856         }
1857         inp = (struct inpcb *)addr;
1858
1859         db_print_inpcb(inp, "inpcb", 0);
1860 }
1861 #endif