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