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