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