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