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