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