]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/in_pcb.c
Merge gnu cpio 2.6 -> 2.8 changes. Unfortunately, we have massive
[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 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_inet6.h"
40 #include "opt_mac.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/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/jail.h>
53 #include <sys/kernel.h>
54 #include <sys/sysctl.h>
55
56 #ifdef DDB
57 #include <ddb/ddb.h>
58 #endif
59
60 #include <vm/uma.h>
61
62 #include <net/if.h>
63 #include <net/if_types.h>
64 #include <net/route.h>
65
66 #include <netinet/in.h>
67 #include <netinet/in_pcb.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/tcp_var.h>
71 #include <netinet/udp.h>
72 #include <netinet/udp_var.h>
73 #ifdef INET6
74 #include <netinet/ip6.h>
75 #include <netinet6/ip6_var.h>
76 #endif /* INET6 */
77
78
79 #ifdef IPSEC
80 #include <netipsec/ipsec.h>
81 #include <netipsec/key.h>
82 #endif /* IPSEC */
83
84 #include <security/mac/mac_framework.h>
85
86 /*
87  * These configure the range of local port addresses assigned to
88  * "unspecified" outgoing connections/packets/whatever.
89  */
90 int     ipport_lowfirstauto  = IPPORT_RESERVED - 1;     /* 1023 */
91 int     ipport_lowlastauto = IPPORT_RESERVEDSTART;      /* 600 */
92 int     ipport_firstauto = IPPORT_EPHEMERALFIRST;       /* 10000 */
93 int     ipport_lastauto  = IPPORT_EPHEMERALLAST;        /* 65535 */
94 int     ipport_hifirstauto = IPPORT_HIFIRSTAUTO;        /* 49152 */
95 int     ipport_hilastauto  = IPPORT_HILASTAUTO;         /* 65535 */
96
97 /*
98  * Reserved ports accessible only to root. There are significant
99  * security considerations that must be accounted for when changing these,
100  * but the security benefits can be great. Please be careful.
101  */
102 int     ipport_reservedhigh = IPPORT_RESERVED - 1;      /* 1023 */
103 int     ipport_reservedlow = 0;
104
105 /* Variables dealing with random ephemeral port allocation. */
106 int     ipport_randomized = 1;  /* user controlled via sysctl */
107 int     ipport_randomcps = 10;  /* user controlled via sysctl */
108 int     ipport_randomtime = 45; /* user controlled via sysctl */
109 int     ipport_stoprandom = 0;  /* toggled by ipport_tick */
110 int     ipport_tcpallocs;
111 int     ipport_tcplastcount;
112
113 #define RANGECHK(var, min, max) \
114         if ((var) < (min)) { (var) = (min); } \
115         else if ((var) > (max)) { (var) = (max); }
116
117 static int
118 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
119 {
120         int error;
121
122         error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
123         if (error == 0) {
124                 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
125                 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
126                 RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
127                 RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
128                 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
129                 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
130         }
131         return (error);
132 }
133
134 #undef RANGECHK
135
136 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
137
138 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
139            &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
140 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
141            &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
142 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
143            &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
144 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
145            &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
146 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
147            &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
148 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
149            &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
150 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
151            CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
152 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
153            CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
154 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
155            &ipport_randomized, 0, "Enable random port allocation");
156 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
157            &ipport_randomcps, 0, "Maximum number of random port "
158            "allocations before switching to a sequental one");
159 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
160            &ipport_randomtime, 0, "Minimum time to keep sequental port "
161            "allocation before switching to a random one");
162
163 /*
164  * in_pcb.c: manage the Protocol Control Blocks.
165  *
166  * NOTE: It is assumed that most of these functions will be called with
167  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
168  * functions often modify hash chains or addresses in pcbs.
169  */
170
171 /*
172  * Allocate a PCB and associate it with the socket.
173  * On success return with the PCB locked.
174  */
175 int
176 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
177 {
178         struct inpcb *inp;
179         int error;
180
181         INP_INFO_WLOCK_ASSERT(pcbinfo);
182         error = 0;
183         inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
184         if (inp == NULL)
185                 return (ENOBUFS);
186         bzero(inp, inp_zero_size);
187         inp->inp_pcbinfo = pcbinfo;
188         inp->inp_socket = so;
189         inp->inp_inc.inc_fibnum = so->so_fibnum;
190 #ifdef MAC
191         error = mac_inpcb_init(inp, M_NOWAIT);
192         if (error != 0)
193                 goto out;
194         SOCK_LOCK(so);
195         mac_inpcb_create(so, inp);
196         SOCK_UNLOCK(so);
197 #endif
198
199 #ifdef IPSEC
200         error = ipsec_init_policy(so, &inp->inp_sp);
201         if (error != 0) {
202 #ifdef MAC
203                 mac_inpcb_destroy(inp);
204 #endif
205                 goto out;
206         }
207 #endif /*IPSEC*/
208 #ifdef INET6
209         if (INP_SOCKAF(so) == AF_INET6) {
210                 inp->inp_vflag |= INP_IPV6PROTO;
211                 if (ip6_v6only)
212                         inp->inp_flags |= IN6P_IPV6_V6ONLY;
213         }
214 #endif
215         LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
216         pcbinfo->ipi_count++;
217         so->so_pcb = (caddr_t)inp;
218 #ifdef INET6
219         if (ip6_auto_flowlabel)
220                 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
221 #endif
222         INP_WLOCK(inp);
223         inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
224         
225 #if defined(IPSEC) || defined(MAC)
226 out:
227         if (error != 0)
228                 uma_zfree(pcbinfo->ipi_zone, inp);
229 #endif
230         return (error);
231 }
232
233 int
234 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
235 {
236         int anonport, error;
237
238         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
239         INP_WLOCK_ASSERT(inp);
240
241         if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
242                 return (EINVAL);
243         anonport = inp->inp_lport == 0 && (nam == NULL ||
244             ((struct sockaddr_in *)nam)->sin_port == 0);
245         error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
246             &inp->inp_lport, cred);
247         if (error)
248                 return (error);
249         if (in_pcbinshash(inp) != 0) {
250                 inp->inp_laddr.s_addr = INADDR_ANY;
251                 inp->inp_lport = 0;
252                 return (EAGAIN);
253         }
254         if (anonport)
255                 inp->inp_flags |= INP_ANONPORT;
256         return (0);
257 }
258
259 /*
260  * Set up a bind operation on a PCB, performing port allocation
261  * as required, but do not actually modify the PCB. Callers can
262  * either complete the bind by setting inp_laddr/inp_lport and
263  * calling in_pcbinshash(), or they can just use the resulting
264  * port and address to authorise the sending of a once-off packet.
265  *
266  * On error, the values of *laddrp and *lportp are not changed.
267  */
268 int
269 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
270     u_short *lportp, struct ucred *cred)
271 {
272         struct socket *so = inp->inp_socket;
273         unsigned short *lastport;
274         struct sockaddr_in *sin;
275         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
276         struct in_addr laddr;
277         u_short lport = 0;
278         int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
279         int error, prison = 0;
280         int dorandom;
281
282         /*
283          * Because no actual state changes occur here, a write global write
284          * lock on the pcbinfo isn't required.
285          */
286         INP_INFO_LOCK_ASSERT(pcbinfo);
287         INP_LOCK_ASSERT(inp);
288
289         if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
290                 return (EADDRNOTAVAIL);
291         laddr.s_addr = *laddrp;
292         if (nam != NULL && laddr.s_addr != INADDR_ANY)
293                 return (EINVAL);
294         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
295                 wild = INPLOOKUP_WILDCARD;
296         if (nam) {
297                 sin = (struct sockaddr_in *)nam;
298                 if (nam->sa_len != sizeof (*sin))
299                         return (EINVAL);
300 #ifdef notdef
301                 /*
302                  * We should check the family, but old programs
303                  * incorrectly fail to initialize it.
304                  */
305                 if (sin->sin_family != AF_INET)
306                         return (EAFNOSUPPORT);
307 #endif
308                 if (sin->sin_addr.s_addr != INADDR_ANY)
309                         if (prison_ip(cred, 0, &sin->sin_addr.s_addr))
310                                 return(EINVAL);
311                 if (sin->sin_port != *lportp) {
312                         /* Don't allow the port to change. */
313                         if (*lportp != 0)
314                                 return (EINVAL);
315                         lport = sin->sin_port;
316                 }
317                 /* NB: lport is left as 0 if the port isn't being changed. */
318                 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
319                         /*
320                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
321                          * allow complete duplication of binding if
322                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
323                          * and a multicast address is bound on both
324                          * new and duplicated sockets.
325                          */
326                         if (so->so_options & SO_REUSEADDR)
327                                 reuseport = SO_REUSEADDR|SO_REUSEPORT;
328                 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
329                         sin->sin_port = 0;              /* yech... */
330                         bzero(&sin->sin_zero, sizeof(sin->sin_zero));
331                         if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
332                                 return (EADDRNOTAVAIL);
333                 }
334                 laddr = sin->sin_addr;
335                 if (lport) {
336                         struct inpcb *t;
337                         struct tcptw *tw;
338
339                         /* GROSS */
340                         if (ntohs(lport) <= ipport_reservedhigh &&
341                             ntohs(lport) >= ipport_reservedlow &&
342                             priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
343                             0))
344                                 return (EACCES);
345                         if (jailed(cred))
346                                 prison = 1;
347                         if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
348                             priv_check_cred(so->so_cred,
349                             PRIV_NETINET_REUSEPORT, 0) != 0) {
350                                 t = in_pcblookup_local(pcbinfo,
351                                     sin->sin_addr, lport,
352                                     prison ? 0 :  INPLOOKUP_WILDCARD);
353         /*
354          * XXX
355          * This entire block sorely needs a rewrite.
356          */
357                                 if (t &&
358                                     ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
359                                     (so->so_type != SOCK_STREAM ||
360                                      ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
361                                     (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
362                                      ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
363                                      (t->inp_socket->so_options &
364                                          SO_REUSEPORT) == 0) &&
365                                     (so->so_cred->cr_uid !=
366                                      t->inp_socket->so_cred->cr_uid))
367                                         return (EADDRINUSE);
368                         }
369                         if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr))
370                                 return (EADDRNOTAVAIL);
371                         t = in_pcblookup_local(pcbinfo, sin->sin_addr,
372                             lport, prison ? 0 : wild);
373                         if (t && (t->inp_vflag & INP_TIMEWAIT)) {
374                                 /*
375                                  * XXXRW: If an incpb has had its timewait
376                                  * state recycled, we treat the address as
377                                  * being in use (for now).  This is better
378                                  * than a panic, but not desirable.
379                                  */
380                                 tw = intotw(inp);
381                                 if (tw == NULL ||
382                                     (reuseport & tw->tw_so_options) == 0)
383                                         return (EADDRINUSE);
384                         } else if (t &&
385                             (reuseport & t->inp_socket->so_options) == 0) {
386 #ifdef INET6
387                                 if (ntohl(sin->sin_addr.s_addr) !=
388                                     INADDR_ANY ||
389                                     ntohl(t->inp_laddr.s_addr) !=
390                                     INADDR_ANY ||
391                                     INP_SOCKAF(so) ==
392                                     INP_SOCKAF(t->inp_socket))
393 #endif
394                                 return (EADDRINUSE);
395                         }
396                 }
397         }
398         if (*lportp != 0)
399                 lport = *lportp;
400         if (lport == 0) {
401                 u_short first, last, aux;
402                 int count;
403
404                 if (laddr.s_addr != INADDR_ANY)
405                         if (prison_ip(cred, 0, &laddr.s_addr))
406                                 return (EINVAL);
407
408                 if (inp->inp_flags & INP_HIGHPORT) {
409                         first = ipport_hifirstauto;     /* sysctl */
410                         last  = ipport_hilastauto;
411                         lastport = &pcbinfo->ipi_lasthi;
412                 } else if (inp->inp_flags & INP_LOWPORT) {
413                         error = priv_check_cred(cred,
414                             PRIV_NETINET_RESERVEDPORT, 0);
415                         if (error)
416                                 return error;
417                         first = ipport_lowfirstauto;    /* 1023 */
418                         last  = ipport_lowlastauto;     /* 600 */
419                         lastport = &pcbinfo->ipi_lastlow;
420                 } else {
421                         first = ipport_firstauto;       /* sysctl */
422                         last  = ipport_lastauto;
423                         lastport = &pcbinfo->ipi_lastport;
424                 }
425                 /*
426                  * For UDP, use random port allocation as long as the user
427                  * allows it.  For TCP (and as of yet unknown) connections,
428                  * use random port allocation only if the user allows it AND
429                  * ipport_tick() allows it.
430                  */
431                 if (ipport_randomized &&
432                         (!ipport_stoprandom || pcbinfo == &udbinfo))
433                         dorandom = 1;
434                 else
435                         dorandom = 0;
436                 /*
437                  * It makes no sense to do random port allocation if
438                  * we have the only port available.
439                  */
440                 if (first == last)
441                         dorandom = 0;
442                 /* Make sure to not include UDP packets in the count. */
443                 if (pcbinfo != &udbinfo)
444                         ipport_tcpallocs++;
445                 /*
446                  * Simple check to ensure all ports are not used up causing
447                  * a deadlock here.
448                  */
449                 if (first > last) {
450                         aux = first;
451                         first = last;
452                         last = aux;
453                 }
454
455                 if (dorandom)
456                         *lastport = first +
457                                     (arc4random() % (last - first));
458
459                 count = last - first;
460
461                 do {
462                         if (count-- < 0)        /* completely used? */
463                                 return (EADDRNOTAVAIL);
464                         ++*lastport;
465                         if (*lastport < first || *lastport > last)
466                                 *lastport = first;
467                         lport = htons(*lastport);
468                 } while (in_pcblookup_local(pcbinfo, laddr, lport,
469                     wild));
470         }
471         if (prison_ip(cred, 0, &laddr.s_addr))
472                 return (EINVAL);
473         *laddrp = laddr.s_addr;
474         *lportp = lport;
475         return (0);
476 }
477
478 /*
479  * Connect from a socket to a specified address.
480  * Both address and port must be specified in argument sin.
481  * If don't have a local address for this socket yet,
482  * then pick one.
483  */
484 int
485 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
486 {
487         u_short lport, fport;
488         in_addr_t laddr, faddr;
489         int anonport, error;
490
491         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
492         INP_WLOCK_ASSERT(inp);
493
494         lport = inp->inp_lport;
495         laddr = inp->inp_laddr.s_addr;
496         anonport = (lport == 0);
497         error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
498             NULL, cred);
499         if (error)
500                 return (error);
501
502         /* Do the initial binding of the local address if required. */
503         if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
504                 inp->inp_lport = lport;
505                 inp->inp_laddr.s_addr = laddr;
506                 if (in_pcbinshash(inp) != 0) {
507                         inp->inp_laddr.s_addr = INADDR_ANY;
508                         inp->inp_lport = 0;
509                         return (EAGAIN);
510                 }
511         }
512
513         /* Commit the remaining changes. */
514         inp->inp_lport = lport;
515         inp->inp_laddr.s_addr = laddr;
516         inp->inp_faddr.s_addr = faddr;
517         inp->inp_fport = fport;
518         in_pcbrehash(inp);
519
520         if (anonport)
521                 inp->inp_flags |= INP_ANONPORT;
522         return (0);
523 }
524
525 /*
526  * Set up for a connect from a socket to the specified address.
527  * On entry, *laddrp and *lportp should contain the current local
528  * address and port for the PCB; these are updated to the values
529  * that should be placed in inp_laddr and inp_lport to complete
530  * the connect.
531  *
532  * On success, *faddrp and *fportp will be set to the remote address
533  * and port. These are not updated in the error case.
534  *
535  * If the operation fails because the connection already exists,
536  * *oinpp will be set to the PCB of that connection so that the
537  * caller can decide to override it. In all other cases, *oinpp
538  * is set to NULL.
539  */
540 int
541 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
542     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
543     struct inpcb **oinpp, struct ucred *cred)
544 {
545         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
546         struct in_ifaddr *ia;
547         struct sockaddr_in sa;
548         struct ucred *socred;
549         struct inpcb *oinp;
550         struct in_addr laddr, faddr;
551         u_short lport, fport;
552         int error;
553
554         /*
555          * Because a global state change doesn't actually occur here, a read
556          * lock is sufficient.
557          */
558         INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo);
559         INP_LOCK_ASSERT(inp);
560
561         if (oinpp != NULL)
562                 *oinpp = NULL;
563         if (nam->sa_len != sizeof (*sin))
564                 return (EINVAL);
565         if (sin->sin_family != AF_INET)
566                 return (EAFNOSUPPORT);
567         if (sin->sin_port == 0)
568                 return (EADDRNOTAVAIL);
569         laddr.s_addr = *laddrp;
570         lport = *lportp;
571         faddr = sin->sin_addr;
572         fport = sin->sin_port;
573         socred = inp->inp_socket->so_cred;
574         if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
575                 bzero(&sa, sizeof(sa));
576                 sa.sin_addr.s_addr = htonl(prison_getip(socred));
577                 sa.sin_len = sizeof(sa);
578                 sa.sin_family = AF_INET;
579                 error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
580                     &laddr.s_addr, &lport, cred);
581                 if (error)
582                         return (error);
583         }
584         if (!TAILQ_EMPTY(&in_ifaddrhead)) {
585                 /*
586                  * If the destination address is INADDR_ANY,
587                  * use the primary local address.
588                  * If the supplied address is INADDR_BROADCAST,
589                  * and the primary interface supports broadcast,
590                  * choose the broadcast address for that interface.
591                  */
592                 if (faddr.s_addr == INADDR_ANY)
593                         faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
594                 else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
595                     (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
596                     IFF_BROADCAST))
597                         faddr = satosin(&TAILQ_FIRST(
598                             &in_ifaddrhead)->ia_broadaddr)->sin_addr;
599         }
600         if (laddr.s_addr == INADDR_ANY) {
601                 ia = (struct in_ifaddr *)0;
602                 /*
603                  * If route is known our src addr is taken from the i/f,
604                  * else punt.
605                  *
606                  * Find out route to destination
607                  */
608                 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
609                         ia = ip_rtaddr(faddr, inp->inp_inc.inc_fibnum);
610                 /*
611                  * If we found a route, use the address corresponding to
612                  * the outgoing interface.
613                  * 
614                  * Otherwise assume faddr is reachable on a directly connected
615                  * network and try to find a corresponding interface to take
616                  * the source address from.
617                  */
618                 if (ia == 0) {
619                         bzero(&sa, sizeof(sa));
620                         sa.sin_addr = faddr;
621                         sa.sin_len = sizeof(sa);
622                         sa.sin_family = AF_INET;
623
624                         ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
625                         if (ia == 0)
626                                 ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
627                         if (ia == 0)
628                                 return (ENETUNREACH);
629                 }
630                 /*
631                  * If the destination address is multicast and an outgoing
632                  * interface has been set as a multicast option, use the
633                  * address of that interface as our source address.
634                  */
635                 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
636                     inp->inp_moptions != NULL) {
637                         struct ip_moptions *imo;
638                         struct ifnet *ifp;
639
640                         imo = inp->inp_moptions;
641                         if (imo->imo_multicast_ifp != NULL) {
642                                 ifp = imo->imo_multicast_ifp;
643                                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
644                                         if (ia->ia_ifp == ifp)
645                                                 break;
646                                 if (ia == 0)
647                                         return (EADDRNOTAVAIL);
648                         }
649                 }
650                 laddr = ia->ia_addr.sin_addr;
651         }
652
653         oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
654             0, NULL);
655         if (oinp != NULL) {
656                 if (oinpp != NULL)
657                         *oinpp = oinp;
658                 return (EADDRINUSE);
659         }
660         if (lport == 0) {
661                 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
662                     cred);
663                 if (error)
664                         return (error);
665         }
666         *laddrp = laddr.s_addr;
667         *lportp = lport;
668         *faddrp = faddr.s_addr;
669         *fportp = fport;
670         return (0);
671 }
672
673 void
674 in_pcbdisconnect(struct inpcb *inp)
675 {
676
677         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
678         INP_WLOCK_ASSERT(inp);
679
680         inp->inp_faddr.s_addr = INADDR_ANY;
681         inp->inp_fport = 0;
682         in_pcbrehash(inp);
683 }
684
685 /*
686  * In the old world order, in_pcbdetach() served two functions: to detach the
687  * pcb from the socket/potentially free the socket, and to free the pcb
688  * itself.  In the new world order, the protocol code is responsible for
689  * managing the relationship with the socket, and this code simply frees the
690  * pcb.
691  */
692 void
693 in_pcbdetach(struct inpcb *inp)
694 {
695
696         KASSERT(inp->inp_socket != NULL, ("in_pcbdetach: inp_socket == NULL"));
697         inp->inp_socket->so_pcb = NULL;
698         inp->inp_socket = NULL;
699 }
700
701 void
702 in_pcbfree(struct inpcb *inp)
703 {
704         struct inpcbinfo *ipi = inp->inp_pcbinfo;
705
706         KASSERT(inp->inp_socket == NULL, ("in_pcbfree: inp_socket != NULL"));
707
708         INP_INFO_WLOCK_ASSERT(ipi);
709         INP_WLOCK_ASSERT(inp);
710
711 #ifdef IPSEC
712         ipsec4_delete_pcbpolicy(inp);
713 #endif /*IPSEC*/
714         inp->inp_gencnt = ++ipi->ipi_gencnt;
715         in_pcbremlists(inp);
716         if (inp->inp_options)
717                 (void)m_free(inp->inp_options);
718         if (inp->inp_moptions != NULL)
719                 inp_freemoptions(inp->inp_moptions);
720         inp->inp_vflag = 0;
721         
722 #ifdef MAC
723         mac_inpcb_destroy(inp);
724 #endif
725         INP_WUNLOCK(inp);
726         uma_zfree(ipi->ipi_zone, inp);
727 }
728
729 /*
730  * TCP needs to maintain its inpcb structure after the TCP connection has
731  * been torn down.  However, it must be disconnected from the inpcb hashes as
732  * it must not prevent binding of future connections to the same port/ip
733  * combination by other inpcbs.
734  */
735 void
736 in_pcbdrop(struct inpcb *inp)
737 {
738
739         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
740         INP_WLOCK_ASSERT(inp);
741
742         inp->inp_vflag |= INP_DROPPED;
743         if (inp->inp_lport) {
744                 struct inpcbport *phd = inp->inp_phd;
745
746                 LIST_REMOVE(inp, inp_hash);
747                 LIST_REMOVE(inp, inp_portlist);
748                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
749                         LIST_REMOVE(phd, phd_hash);
750                         free(phd, M_PCB);
751                 }
752                 inp->inp_lport = 0;
753         }
754 }
755
756 /*
757  * Common routines to return the socket addresses associated with inpcbs.
758  */
759 struct sockaddr *
760 in_sockaddr(in_port_t port, struct in_addr *addr_p)
761 {
762         struct sockaddr_in *sin;
763
764         MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
765                 M_WAITOK | M_ZERO);
766         sin->sin_family = AF_INET;
767         sin->sin_len = sizeof(*sin);
768         sin->sin_addr = *addr_p;
769         sin->sin_port = port;
770
771         return (struct sockaddr *)sin;
772 }
773
774 int
775 in_getsockaddr(struct socket *so, struct sockaddr **nam)
776 {
777         struct inpcb *inp;
778         struct in_addr addr;
779         in_port_t port;
780
781         inp = sotoinpcb(so);
782         KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
783
784         INP_RLOCK(inp);
785         port = inp->inp_lport;
786         addr = inp->inp_laddr;
787         INP_RUNLOCK(inp);
788
789         *nam = in_sockaddr(port, &addr);
790         return 0;
791 }
792
793 int
794 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
795 {
796         struct inpcb *inp;
797         struct in_addr addr;
798         in_port_t port;
799
800         inp = sotoinpcb(so);
801         KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
802
803         INP_RLOCK(inp);
804         port = inp->inp_fport;
805         addr = inp->inp_faddr;
806         INP_RUNLOCK(inp);
807
808         *nam = in_sockaddr(port, &addr);
809         return 0;
810 }
811
812 void
813 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
814     struct inpcb *(*notify)(struct inpcb *, int))
815 {
816         struct inpcb *inp, *inp_temp;
817
818         INP_INFO_WLOCK(pcbinfo);
819         LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
820                 INP_WLOCK(inp);
821 #ifdef INET6
822                 if ((inp->inp_vflag & INP_IPV4) == 0) {
823                         INP_WUNLOCK(inp);
824                         continue;
825                 }
826 #endif
827                 if (inp->inp_faddr.s_addr != faddr.s_addr ||
828                     inp->inp_socket == NULL) {
829                         INP_WUNLOCK(inp);
830                         continue;
831                 }
832                 if ((*notify)(inp, errno))
833                         INP_WUNLOCK(inp);
834         }
835         INP_INFO_WUNLOCK(pcbinfo);
836 }
837
838 void
839 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
840 {
841         struct inpcb *inp;
842         struct ip_moptions *imo;
843         int i, gap;
844
845         INP_INFO_RLOCK(pcbinfo);
846         LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
847                 INP_WLOCK(inp);
848                 imo = inp->inp_moptions;
849                 if ((inp->inp_vflag & INP_IPV4) &&
850                     imo != NULL) {
851                         /*
852                          * Unselect the outgoing interface if it is being
853                          * detached.
854                          */
855                         if (imo->imo_multicast_ifp == ifp)
856                                 imo->imo_multicast_ifp = NULL;
857
858                         /*
859                          * Drop multicast group membership if we joined
860                          * through the interface being detached.
861                          */
862                         for (i = 0, gap = 0; i < imo->imo_num_memberships;
863                             i++) {
864                                 if (imo->imo_membership[i]->inm_ifp == ifp) {
865                                         in_delmulti(imo->imo_membership[i]);
866                                         gap++;
867                                 } else if (gap != 0)
868                                         imo->imo_membership[i - gap] =
869                                             imo->imo_membership[i];
870                         }
871                         imo->imo_num_memberships -= gap;
872                 }
873                 INP_WUNLOCK(inp);
874         }
875         INP_INFO_RUNLOCK(pcbinfo);
876 }
877
878 /*
879  * Lookup a PCB based on the local address and port.
880  */
881 #define INP_LOOKUP_MAPPED_PCB_COST      3
882 struct inpcb *
883 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
884     u_int lport_arg, int wild_okay)
885 {
886         struct inpcb *inp;
887 #ifdef INET6
888         int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
889 #else
890         int matchwild = 3;
891 #endif
892         int wildcard;
893         u_short lport = lport_arg;
894
895         INP_INFO_LOCK_ASSERT(pcbinfo);
896
897         if (!wild_okay) {
898                 struct inpcbhead *head;
899                 /*
900                  * Look for an unconnected (wildcard foreign addr) PCB that
901                  * matches the local address and port we're looking for.
902                  */
903                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
904                     0, pcbinfo->ipi_hashmask)];
905                 LIST_FOREACH(inp, head, inp_hash) {
906 #ifdef INET6
907                         if ((inp->inp_vflag & INP_IPV4) == 0)
908                                 continue;
909 #endif
910                         if (inp->inp_faddr.s_addr == INADDR_ANY &&
911                             inp->inp_laddr.s_addr == laddr.s_addr &&
912                             inp->inp_lport == lport) {
913                                 /*
914                                  * Found.
915                                  */
916                                 return (inp);
917                         }
918                 }
919                 /*
920                  * Not found.
921                  */
922                 return (NULL);
923         } else {
924                 struct inpcbporthead *porthash;
925                 struct inpcbport *phd;
926                 struct inpcb *match = NULL;
927                 /*
928                  * Best fit PCB lookup.
929                  *
930                  * First see if this local port is in use by looking on the
931                  * port hash list.
932                  */
933                 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
934                     pcbinfo->ipi_porthashmask)];
935                 LIST_FOREACH(phd, porthash, phd_hash) {
936                         if (phd->phd_port == lport)
937                                 break;
938                 }
939                 if (phd != NULL) {
940                         /*
941                          * Port is in use by one or more PCBs. Look for best
942                          * fit.
943                          */
944                         LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
945                                 wildcard = 0;
946 #ifdef INET6
947                                 if ((inp->inp_vflag & INP_IPV4) == 0)
948                                         continue;
949                                 /*
950                                  * We never select the PCB that has
951                                  * INP_IPV6 flag and is bound to :: if
952                                  * we have another PCB which is bound
953                                  * to 0.0.0.0.  If a PCB has the
954                                  * INP_IPV6 flag, then we set its cost
955                                  * higher than IPv4 only PCBs.
956                                  *
957                                  * Note that the case only happens
958                                  * when a socket is bound to ::, under
959                                  * the condition that the use of the
960                                  * mapped address is allowed.
961                                  */
962                                 if ((inp->inp_vflag & INP_IPV6) != 0)
963                                         wildcard += INP_LOOKUP_MAPPED_PCB_COST;
964 #endif
965                                 if (inp->inp_faddr.s_addr != INADDR_ANY)
966                                         wildcard++;
967                                 if (inp->inp_laddr.s_addr != INADDR_ANY) {
968                                         if (laddr.s_addr == INADDR_ANY)
969                                                 wildcard++;
970                                         else if (inp->inp_laddr.s_addr != laddr.s_addr)
971                                                 continue;
972                                 } else {
973                                         if (laddr.s_addr != INADDR_ANY)
974                                                 wildcard++;
975                                 }
976                                 if (wildcard < matchwild) {
977                                         match = inp;
978                                         matchwild = wildcard;
979                                         if (matchwild == 0) {
980                                                 break;
981                                         }
982                                 }
983                         }
984                 }
985                 return (match);
986         }
987 }
988 #undef INP_LOOKUP_MAPPED_PCB_COST
989
990 /*
991  * Lookup PCB in hash list.
992  */
993 struct inpcb *
994 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
995     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
996     struct ifnet *ifp)
997 {
998         struct inpcbhead *head;
999         struct inpcb *inp;
1000         u_short fport = fport_arg, lport = lport_arg;
1001
1002         INP_INFO_LOCK_ASSERT(pcbinfo);
1003
1004         /*
1005          * First look for an exact match.
1006          */
1007         head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1008             pcbinfo->ipi_hashmask)];
1009         LIST_FOREACH(inp, head, inp_hash) {
1010 #ifdef INET6
1011                 if ((inp->inp_vflag & INP_IPV4) == 0)
1012                         continue;
1013 #endif
1014                 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1015                     inp->inp_laddr.s_addr == laddr.s_addr &&
1016                     inp->inp_fport == fport &&
1017                     inp->inp_lport == lport)
1018                         return (inp);
1019         }
1020
1021         /*
1022          * Then look for a wildcard match, if requested.
1023          */
1024         if (wildcard) {
1025                 struct inpcb *local_wild = NULL;
1026 #ifdef INET6
1027                 struct inpcb *local_wild_mapped = NULL;
1028 #endif
1029
1030                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1031                     0, pcbinfo->ipi_hashmask)];
1032                 LIST_FOREACH(inp, head, inp_hash) {
1033 #ifdef INET6
1034                         if ((inp->inp_vflag & INP_IPV4) == 0)
1035                                 continue;
1036 #endif
1037                         if (inp->inp_faddr.s_addr == INADDR_ANY &&
1038                             inp->inp_lport == lport) {
1039                                 if (ifp && ifp->if_type == IFT_FAITH &&
1040                                     (inp->inp_flags & INP_FAITH) == 0)
1041                                         continue;
1042                                 if (inp->inp_laddr.s_addr == laddr.s_addr)
1043                                         return (inp);
1044                                 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1045 #ifdef INET6
1046                                         if (INP_CHECK_SOCKAF(inp->inp_socket,
1047                                                              AF_INET6))
1048                                                 local_wild_mapped = inp;
1049                                         else
1050 #endif
1051                                                 local_wild = inp;
1052                                 }
1053                         }
1054                 }
1055 #ifdef INET6
1056                 if (local_wild == NULL)
1057                         return (local_wild_mapped);
1058 #endif
1059                 return (local_wild);
1060         }
1061         return (NULL);
1062 }
1063
1064 /*
1065  * Insert PCB onto various hash lists.
1066  */
1067 int
1068 in_pcbinshash(struct inpcb *inp)
1069 {
1070         struct inpcbhead *pcbhash;
1071         struct inpcbporthead *pcbporthash;
1072         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1073         struct inpcbport *phd;
1074         u_int32_t hashkey_faddr;
1075
1076         INP_INFO_WLOCK_ASSERT(pcbinfo);
1077         INP_WLOCK_ASSERT(inp);
1078
1079 #ifdef INET6
1080         if (inp->inp_vflag & INP_IPV6)
1081                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1082         else
1083 #endif /* INET6 */
1084         hashkey_faddr = inp->inp_faddr.s_addr;
1085
1086         pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1087                  inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1088
1089         pcbporthash = &pcbinfo->ipi_porthashbase[
1090             INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1091
1092         /*
1093          * Go through port list and look for a head for this lport.
1094          */
1095         LIST_FOREACH(phd, pcbporthash, phd_hash) {
1096                 if (phd->phd_port == inp->inp_lport)
1097                         break;
1098         }
1099         /*
1100          * If none exists, malloc one and tack it on.
1101          */
1102         if (phd == NULL) {
1103                 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1104                 if (phd == NULL) {
1105                         return (ENOBUFS); /* XXX */
1106                 }
1107                 phd->phd_port = inp->inp_lport;
1108                 LIST_INIT(&phd->phd_pcblist);
1109                 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1110         }
1111         inp->inp_phd = phd;
1112         LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1113         LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1114         return (0);
1115 }
1116
1117 /*
1118  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1119  * changed. NOTE: This does not handle the case of the lport changing (the
1120  * hashed port list would have to be updated as well), so the lport must
1121  * not change after in_pcbinshash() has been called.
1122  */
1123 void
1124 in_pcbrehash(struct inpcb *inp)
1125 {
1126         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1127         struct inpcbhead *head;
1128         u_int32_t hashkey_faddr;
1129
1130         INP_INFO_WLOCK_ASSERT(pcbinfo);
1131         INP_WLOCK_ASSERT(inp);
1132
1133 #ifdef INET6
1134         if (inp->inp_vflag & INP_IPV6)
1135                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1136         else
1137 #endif /* INET6 */
1138         hashkey_faddr = inp->inp_faddr.s_addr;
1139
1140         head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1141                 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1142
1143         LIST_REMOVE(inp, inp_hash);
1144         LIST_INSERT_HEAD(head, inp, inp_hash);
1145 }
1146
1147 /*
1148  * Remove PCB from various lists.
1149  */
1150 void
1151 in_pcbremlists(struct inpcb *inp)
1152 {
1153         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1154
1155         INP_INFO_WLOCK_ASSERT(pcbinfo);
1156         INP_WLOCK_ASSERT(inp);
1157
1158         inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1159         if (inp->inp_lport) {
1160                 struct inpcbport *phd = inp->inp_phd;
1161
1162                 LIST_REMOVE(inp, inp_hash);
1163                 LIST_REMOVE(inp, inp_portlist);
1164                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1165                         LIST_REMOVE(phd, phd_hash);
1166                         free(phd, M_PCB);
1167                 }
1168         }
1169         LIST_REMOVE(inp, inp_list);
1170         pcbinfo->ipi_count--;
1171 }
1172
1173 /*
1174  * A set label operation has occurred at the socket layer, propagate the
1175  * label change into the in_pcb for the socket.
1176  */
1177 void
1178 in_pcbsosetlabel(struct socket *so)
1179 {
1180 #ifdef MAC
1181         struct inpcb *inp;
1182
1183         inp = sotoinpcb(so);
1184         KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1185
1186         INP_WLOCK(inp);
1187         SOCK_LOCK(so);
1188         mac_inpcb_sosetlabel(so, inp);
1189         SOCK_UNLOCK(so);
1190         INP_WUNLOCK(inp);
1191 #endif
1192 }
1193
1194 /*
1195  * ipport_tick runs once per second, determining if random port allocation
1196  * should be continued.  If more than ipport_randomcps ports have been
1197  * allocated in the last second, then we return to sequential port
1198  * allocation. We return to random allocation only once we drop below
1199  * ipport_randomcps for at least ipport_randomtime seconds.
1200  */
1201 void
1202 ipport_tick(void *xtp)
1203 {
1204
1205         if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) {
1206                 if (ipport_stoprandom > 0)
1207                         ipport_stoprandom--;
1208         } else
1209                 ipport_stoprandom = ipport_randomtime;
1210         ipport_tcplastcount = ipport_tcpallocs;
1211         callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
1212 }
1213
1214 void
1215 inp_wlock(struct inpcb *inp)
1216 {
1217
1218         INP_WLOCK(inp);
1219 }
1220
1221 void
1222 inp_wunlock(struct inpcb *inp)
1223 {
1224
1225         INP_WUNLOCK(inp);
1226 }
1227
1228 void
1229 inp_rlock(struct inpcb *inp)
1230 {
1231
1232         INP_RLOCK(inp);
1233 }
1234
1235 void
1236 inp_runlock(struct inpcb *inp)
1237 {
1238
1239         INP_RUNLOCK(inp);
1240 }
1241
1242 #ifdef INVARIANTS
1243 void
1244 inp_lock_assert(struct inpcb *inp)
1245 {
1246
1247         INP_WLOCK_ASSERT(inp);
1248 }
1249
1250 void
1251 inp_unlock_assert(struct inpcb *inp)
1252 {
1253
1254         INP_UNLOCK_ASSERT(inp);
1255 }
1256 #endif
1257
1258 #ifdef DDB
1259 static void
1260 db_print_indent(int indent)
1261 {
1262         int i;
1263
1264         for (i = 0; i < indent; i++)
1265                 db_printf(" ");
1266 }
1267
1268 static void
1269 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1270 {
1271         char faddr_str[48], laddr_str[48];
1272
1273         db_print_indent(indent);
1274         db_printf("%s at %p\n", name, inc);
1275
1276         indent += 2;
1277
1278 #ifdef INET6
1279         if (inc->inc_flags == 1) {
1280                 /* IPv6. */
1281                 ip6_sprintf(laddr_str, &inc->inc6_laddr);
1282                 ip6_sprintf(faddr_str, &inc->inc6_faddr);
1283         } else {
1284 #endif
1285                 /* IPv4. */
1286                 inet_ntoa_r(inc->inc_laddr, laddr_str);
1287                 inet_ntoa_r(inc->inc_faddr, faddr_str);
1288 #ifdef INET6
1289         }
1290 #endif
1291         db_print_indent(indent);
1292         db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
1293             ntohs(inc->inc_lport));
1294         db_print_indent(indent);
1295         db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
1296             ntohs(inc->inc_fport));
1297 }
1298
1299 static void
1300 db_print_inpflags(int inp_flags)
1301 {
1302         int comma;
1303
1304         comma = 0;
1305         if (inp_flags & INP_RECVOPTS) {
1306                 db_printf("%sINP_RECVOPTS", comma ? ", " : "");
1307                 comma = 1;
1308         }
1309         if (inp_flags & INP_RECVRETOPTS) {
1310                 db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
1311                 comma = 1;
1312         }
1313         if (inp_flags & INP_RECVDSTADDR) {
1314                 db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
1315                 comma = 1;
1316         }
1317         if (inp_flags & INP_HDRINCL) {
1318                 db_printf("%sINP_HDRINCL", comma ? ", " : "");
1319                 comma = 1;
1320         }
1321         if (inp_flags & INP_HIGHPORT) {
1322                 db_printf("%sINP_HIGHPORT", comma ? ", " : "");
1323                 comma = 1;
1324         }
1325         if (inp_flags & INP_LOWPORT) {
1326                 db_printf("%sINP_LOWPORT", comma ? ", " : "");
1327                 comma = 1;
1328         }
1329         if (inp_flags & INP_ANONPORT) {
1330                 db_printf("%sINP_ANONPORT", comma ? ", " : "");
1331                 comma = 1;
1332         }
1333         if (inp_flags & INP_RECVIF) {
1334                 db_printf("%sINP_RECVIF", comma ? ", " : "");
1335                 comma = 1;
1336         }
1337         if (inp_flags & INP_MTUDISC) {
1338                 db_printf("%sINP_MTUDISC", comma ? ", " : "");
1339                 comma = 1;
1340         }
1341         if (inp_flags & INP_FAITH) {
1342                 db_printf("%sINP_FAITH", comma ? ", " : "");
1343                 comma = 1;
1344         }
1345         if (inp_flags & INP_RECVTTL) {
1346                 db_printf("%sINP_RECVTTL", comma ? ", " : "");
1347                 comma = 1;
1348         }
1349         if (inp_flags & INP_DONTFRAG) {
1350                 db_printf("%sINP_DONTFRAG", comma ? ", " : "");
1351                 comma = 1;
1352         }
1353         if (inp_flags & IN6P_IPV6_V6ONLY) {
1354                 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
1355                 comma = 1;
1356         }
1357         if (inp_flags & IN6P_PKTINFO) {
1358                 db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
1359                 comma = 1;
1360         }
1361         if (inp_flags & IN6P_HOPLIMIT) {
1362                 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
1363                 comma = 1;
1364         }
1365         if (inp_flags & IN6P_HOPOPTS) {
1366                 db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
1367                 comma = 1;
1368         }
1369         if (inp_flags & IN6P_DSTOPTS) {
1370                 db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
1371                 comma = 1;
1372         }
1373         if (inp_flags & IN6P_RTHDR) {
1374                 db_printf("%sIN6P_RTHDR", comma ? ", " : "");
1375                 comma = 1;
1376         }
1377         if (inp_flags & IN6P_RTHDRDSTOPTS) {
1378                 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
1379                 comma = 1;
1380         }
1381         if (inp_flags & IN6P_TCLASS) {
1382                 db_printf("%sIN6P_TCLASS", comma ? ", " : "");
1383                 comma = 1;
1384         }
1385         if (inp_flags & IN6P_AUTOFLOWLABEL) {
1386                 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
1387                 comma = 1;
1388         }
1389         if (inp_flags & IN6P_RFC2292) {
1390                 db_printf("%sIN6P_RFC2292", comma ? ", " : "");
1391                 comma = 1;
1392         }
1393         if (inp_flags & IN6P_MTU) {
1394                 db_printf("IN6P_MTU%s", comma ? ", " : "");
1395                 comma = 1;
1396         }
1397 }
1398
1399 static void
1400 db_print_inpvflag(u_char inp_vflag)
1401 {
1402         int comma;
1403
1404         comma = 0;
1405         if (inp_vflag & INP_IPV4) {
1406                 db_printf("%sINP_IPV4", comma ? ", " : "");
1407                 comma  = 1;
1408         }
1409         if (inp_vflag & INP_IPV6) {
1410                 db_printf("%sINP_IPV6", comma ? ", " : "");
1411                 comma  = 1;
1412         }
1413         if (inp_vflag & INP_IPV6PROTO) {
1414                 db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
1415                 comma  = 1;
1416         }
1417         if (inp_vflag & INP_TIMEWAIT) {
1418                 db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
1419                 comma  = 1;
1420         }
1421         if (inp_vflag & INP_ONESBCAST) {
1422                 db_printf("%sINP_ONESBCAST", comma ? ", " : "");
1423                 comma  = 1;
1424         }
1425         if (inp_vflag & INP_DROPPED) {
1426                 db_printf("%sINP_DROPPED", comma ? ", " : "");
1427                 comma  = 1;
1428         }
1429         if (inp_vflag & INP_SOCKREF) {
1430                 db_printf("%sINP_SOCKREF", comma ? ", " : "");
1431                 comma  = 1;
1432         }
1433 }
1434
1435 void
1436 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
1437 {
1438
1439         db_print_indent(indent);
1440         db_printf("%s at %p\n", name, inp);
1441
1442         indent += 2;
1443
1444         db_print_indent(indent);
1445         db_printf("inp_flow: 0x%x\n", inp->inp_flow);
1446
1447         db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
1448
1449         db_print_indent(indent);
1450         db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
1451             inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
1452
1453         db_print_indent(indent);
1454         db_printf("inp_label: %p   inp_flags: 0x%x (",
1455            inp->inp_label, inp->inp_flags);
1456         db_print_inpflags(inp->inp_flags);
1457         db_printf(")\n");
1458
1459         db_print_indent(indent);
1460         db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
1461             inp->inp_vflag);
1462         db_print_inpvflag(inp->inp_vflag);
1463         db_printf(")\n");
1464
1465         db_print_indent(indent);
1466         db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
1467             inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
1468
1469         db_print_indent(indent);
1470 #ifdef INET6
1471         if (inp->inp_vflag & INP_IPV6) {
1472                 db_printf("in6p_options: %p   in6p_outputopts: %p   "
1473                     "in6p_moptions: %p\n", inp->in6p_options,
1474                     inp->in6p_outputopts, inp->in6p_moptions);
1475                 db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
1476                     "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
1477                     inp->in6p_hops);
1478         } else
1479 #endif
1480         {
1481                 db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
1482                     "inp_ip_moptions: %p\n", inp->inp_ip_tos,
1483                     inp->inp_options, inp->inp_moptions);
1484         }
1485
1486         db_print_indent(indent);
1487         db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
1488             (uintmax_t)inp->inp_gencnt);
1489 }
1490
1491 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
1492 {
1493         struct inpcb *inp;
1494
1495         if (!have_addr) {
1496                 db_printf("usage: show inpcb <addr>\n");
1497                 return;
1498         }
1499         inp = (struct inpcb *)addr;
1500
1501         db_print_inpcb(inp, "inpcb", 0);
1502 }
1503 #endif