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