]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/in_pcb.c
Never select the PCB that has INP_IPV6 flag and is bound to :: if
[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.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)in_pcb.c    8.4 (Berkeley) 5/24/95
30  * $FreeBSD$
31  */
32
33 #include "opt_ipsec.h"
34 #include "opt_inet6.h"
35 #include "opt_mac.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mac.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/domain.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/proc.h>
47 #include <sys/jail.h>
48 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
50
51 #include <vm/uma.h>
52
53 #include <net/if.h>
54 #include <net/if_types.h>
55 #include <net/route.h>
56
57 #include <netinet/in.h>
58 #include <netinet/in_pcb.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/tcp_var.h>
62 #include <netinet/udp.h>
63 #include <netinet/udp_var.h>
64 #ifdef INET6
65 #include <netinet/ip6.h>
66 #include <netinet6/ip6_var.h>
67 #endif /* INET6 */
68
69 #ifdef IPSEC
70 #include <netinet6/ipsec.h>
71 #include <netkey/key.h>
72 #endif /* IPSEC */
73
74 #ifdef FAST_IPSEC
75 #if defined(IPSEC) || defined(IPSEC_ESP)
76 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
77 #endif
78
79 #include <netipsec/ipsec.h>
80 #include <netipsec/key.h>
81 #endif /* FAST_IPSEC */
82
83 /*
84  * These configure the range of local port addresses assigned to
85  * "unspecified" outgoing connections/packets/whatever.
86  */
87 int     ipport_lowfirstauto  = IPPORT_RESERVED - 1;     /* 1023 */
88 int     ipport_lowlastauto = IPPORT_RESERVEDSTART;      /* 600 */
89 int     ipport_firstauto = IPPORT_HIFIRSTAUTO;          /* 49152 */
90 int     ipport_lastauto  = IPPORT_HILASTAUTO;           /* 65535 */
91 int     ipport_hifirstauto = IPPORT_HIFIRSTAUTO;        /* 49152 */
92 int     ipport_hilastauto  = IPPORT_HILASTAUTO;         /* 65535 */
93
94 /*
95  * Reserved ports accessible only to root. There are significant
96  * security considerations that must be accounted for when changing these,
97  * but the security benefits can be great. Please be careful.
98  */
99 int     ipport_reservedhigh = IPPORT_RESERVED - 1;      /* 1023 */
100 int     ipport_reservedlow = 0;
101
102 /* Variables dealing with random ephemeral port allocation. */
103 int     ipport_randomized = 1;  /* user controlled via sysctl */
104 int     ipport_randomcps = 10;  /* user controlled via sysctl */
105 int     ipport_randomtime = 45; /* user controlled via sysctl */
106 int     ipport_stoprandom = 0;  /* toggled by ipport_tick */
107 int     ipport_tcpallocs;
108 int     ipport_tcplastcount;
109
110 #define RANGECHK(var, min, max) \
111         if ((var) < (min)) { (var) = (min); } \
112         else if ((var) > (max)) { (var) = (max); }
113
114 static int
115 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
116 {
117         int error;
118
119         error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
120         if (error == 0) {
121                 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
122                 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
123                 RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
124                 RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
125                 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
126                 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
127         }
128         return (error);
129 }
130
131 #undef RANGECHK
132
133 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
134
135 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
136            &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
137 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
138            &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
139 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
140            &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
141 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
142            &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
143 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
144            &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
145 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
146            &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
147 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
148            CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
149 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
150            CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
151 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
152            &ipport_randomized, 0, "Enable random port allocation");
153 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
154            &ipport_randomcps, 0, "Maximum number of random port "
155            "allocations before switching to a sequental one");
156 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
157            &ipport_randomtime, 0, "Minimum time to keep sequental port "
158            "allocation before switching to a random one");
159
160 /*
161  * in_pcb.c: manage the Protocol Control Blocks.
162  *
163  * NOTE: It is assumed that most of these functions will be called with
164  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
165  * functions often modify hash chains or addresses in pcbs.
166  */
167
168 /*
169  * Allocate a PCB and associate it with the socket.
170  */
171 int
172 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo, const char *type)
173 {
174         struct inpcb *inp;
175         int error;
176
177         INP_INFO_WLOCK_ASSERT(pcbinfo);
178         error = 0;
179         inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT | M_ZERO);
180         if (inp == NULL)
181                 return (ENOBUFS);
182         inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
183         inp->inp_pcbinfo = pcbinfo;
184         inp->inp_socket = so;
185 #ifdef MAC
186         error = mac_init_inpcb(inp, M_NOWAIT);
187         if (error != 0)
188                 goto out;
189         SOCK_LOCK(so);
190         mac_create_inpcb_from_socket(so, inp);
191         SOCK_UNLOCK(so);
192 #endif
193 #if defined(IPSEC) || defined(FAST_IPSEC)
194 #ifdef FAST_IPSEC
195         error = ipsec_init_policy(so, &inp->inp_sp);
196 #else
197         error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
198 #endif
199         if (error != 0)
200                 goto out;
201 #endif /*IPSEC*/
202 #if defined(INET6)
203         if (INP_SOCKAF(so) == AF_INET6) {
204                 inp->inp_vflag |= INP_IPV6PROTO;
205                 if (ip6_v6only)
206                         inp->inp_flags |= IN6P_IPV6_V6ONLY;
207         }
208 #endif
209         LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
210         pcbinfo->ipi_count++;
211         so->so_pcb = (caddr_t)inp;
212         INP_LOCK_INIT(inp, "inp", type);
213 #ifdef INET6
214         if (ip6_auto_flowlabel)
215                 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
216 #endif
217 #if defined(IPSEC) || defined(FAST_IPSEC) || defined(MAC)
218 out:
219         if (error != 0)
220                 uma_zfree(pcbinfo->ipi_zone, inp);
221 #endif
222         return (error);
223 }
224
225 int
226 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
227 {
228         int anonport, error;
229
230         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
231         INP_LOCK_ASSERT(inp);
232
233         if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
234                 return (EINVAL);
235         anonport = inp->inp_lport == 0 && (nam == NULL ||
236             ((struct sockaddr_in *)nam)->sin_port == 0);
237         error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
238             &inp->inp_lport, cred);
239         if (error)
240                 return (error);
241         if (in_pcbinshash(inp) != 0) {
242                 inp->inp_laddr.s_addr = INADDR_ANY;
243                 inp->inp_lport = 0;
244                 return (EAGAIN);
245         }
246         if (anonport)
247                 inp->inp_flags |= INP_ANONPORT;
248         return (0);
249 }
250
251 /*
252  * Set up a bind operation on a PCB, performing port allocation
253  * as required, but do not actually modify the PCB. Callers can
254  * either complete the bind by setting inp_laddr/inp_lport and
255  * calling in_pcbinshash(), or they can just use the resulting
256  * port and address to authorise the sending of a once-off packet.
257  *
258  * On error, the values of *laddrp and *lportp are not changed.
259  */
260 int
261 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
262     u_short *lportp, struct ucred *cred)
263 {
264         struct socket *so = inp->inp_socket;
265         unsigned short *lastport;
266         struct sockaddr_in *sin;
267         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
268         struct in_addr laddr;
269         u_short lport = 0;
270         int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
271         int error, prison = 0;
272         int dorandom;
273
274         INP_INFO_WLOCK_ASSERT(pcbinfo);
275         INP_LOCK_ASSERT(inp);
276
277         if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
278                 return (EADDRNOTAVAIL);
279         laddr.s_addr = *laddrp;
280         if (nam != NULL && laddr.s_addr != INADDR_ANY)
281                 return (EINVAL);
282         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
283                 wild = 1;
284         if (nam) {
285                 sin = (struct sockaddr_in *)nam;
286                 if (nam->sa_len != sizeof (*sin))
287                         return (EINVAL);
288 #ifdef notdef
289                 /*
290                  * We should check the family, but old programs
291                  * incorrectly fail to initialize it.
292                  */
293                 if (sin->sin_family != AF_INET)
294                         return (EAFNOSUPPORT);
295 #endif
296                 if (sin->sin_addr.s_addr != INADDR_ANY)
297                         if (prison_ip(cred, 0, &sin->sin_addr.s_addr))
298                                 return(EINVAL);
299                 if (sin->sin_port != *lportp) {
300                         /* Don't allow the port to change. */
301                         if (*lportp != 0)
302                                 return (EINVAL);
303                         lport = sin->sin_port;
304                 }
305                 /* NB: lport is left as 0 if the port isn't being changed. */
306                 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
307                         /*
308                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
309                          * allow complete duplication of binding if
310                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
311                          * and a multicast address is bound on both
312                          * new and duplicated sockets.
313                          */
314                         if (so->so_options & SO_REUSEADDR)
315                                 reuseport = SO_REUSEADDR|SO_REUSEPORT;
316                 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
317                         sin->sin_port = 0;              /* yech... */
318                         bzero(&sin->sin_zero, sizeof(sin->sin_zero));
319                         if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
320                                 return (EADDRNOTAVAIL);
321                 }
322                 laddr = sin->sin_addr;
323                 if (lport) {
324                         struct inpcb *t;
325                         /* GROSS */
326                         if (ntohs(lport) <= ipport_reservedhigh &&
327                             ntohs(lport) >= ipport_reservedlow &&
328                             suser_cred(cred, SUSER_ALLOWJAIL))
329                                 return (EACCES);
330                         if (jailed(cred))
331                                 prison = 1;
332                         if (so->so_cred->cr_uid != 0 &&
333                             !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
334                                 t = in_pcblookup_local(inp->inp_pcbinfo,
335                                     sin->sin_addr, lport,
336                                     prison ? 0 :  INPLOOKUP_WILDCARD);
337         /*
338          * XXX
339          * This entire block sorely needs a rewrite.
340          */
341                                 if (t &&
342                                     ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
343                                     (so->so_type != SOCK_STREAM ||
344                                      ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
345                                     (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
346                                      ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
347                                      (t->inp_socket->so_options &
348                                          SO_REUSEPORT) == 0) &&
349                                     (so->so_cred->cr_uid !=
350                                      t->inp_socket->so_cred->cr_uid))
351                                         return (EADDRINUSE);
352                         }
353                         if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr))
354                                 return (EADDRNOTAVAIL);
355                         t = in_pcblookup_local(pcbinfo, sin->sin_addr,
356                             lport, prison ? 0 : wild);
357                         if (t && (t->inp_vflag & INP_TIMEWAIT)) {
358                                 if ((reuseport & intotw(t)->tw_so_options) == 0)
359                                         return (EADDRINUSE);
360                         } else
361                         if (t &&
362                             (reuseport & t->inp_socket->so_options) == 0) {
363 #if defined(INET6)
364                                 if (ntohl(sin->sin_addr.s_addr) !=
365                                     INADDR_ANY ||
366                                     ntohl(t->inp_laddr.s_addr) !=
367                                     INADDR_ANY ||
368                                     INP_SOCKAF(so) ==
369                                     INP_SOCKAF(t->inp_socket))
370 #endif /* defined(INET6) */
371                                 return (EADDRINUSE);
372                         }
373                 }
374         }
375         if (*lportp != 0)
376                 lport = *lportp;
377         if (lport == 0) {
378                 u_short first, last;
379                 int count;
380
381                 if (laddr.s_addr != INADDR_ANY)
382                         if (prison_ip(cred, 0, &laddr.s_addr))
383                                 return (EINVAL);
384
385                 if (inp->inp_flags & INP_HIGHPORT) {
386                         first = ipport_hifirstauto;     /* sysctl */
387                         last  = ipport_hilastauto;
388                         lastport = &pcbinfo->lasthi;
389                 } else if (inp->inp_flags & INP_LOWPORT) {
390                         if ((error = suser_cred(cred, SUSER_ALLOWJAIL)) != 0)
391                                 return error;
392                         first = ipport_lowfirstauto;    /* 1023 */
393                         last  = ipport_lowlastauto;     /* 600 */
394                         lastport = &pcbinfo->lastlow;
395                 } else {
396                         first = ipport_firstauto;       /* sysctl */
397                         last  = ipport_lastauto;
398                         lastport = &pcbinfo->lastport;
399                 }
400                 /*
401                  * For UDP, use random port allocation as long as the user
402                  * allows it.  For TCP (and as of yet unknown) connections,
403                  * use random port allocation only if the user allows it AND
404                  * ipport_tick() allows it.
405                  */
406                 if (ipport_randomized &&
407                         (!ipport_stoprandom || pcbinfo == &udbinfo))
408                         dorandom = 1;
409                 else
410                         dorandom = 0;
411                 /*
412                  * It makes no sense to do random port allocation if
413                  * we have the only port available.
414                  */
415                 if (first == last)
416                         dorandom = 0;
417                 /* Make sure to not include UDP packets in the count. */
418                 if (pcbinfo != &udbinfo)
419                         ipport_tcpallocs++;
420                 /*
421                  * Simple check to ensure all ports are not used up causing
422                  * a deadlock here.
423                  *
424                  * We split the two cases (up and down) so that the direction
425                  * is not being tested on each round of the loop.
426                  */
427                 if (first > last) {
428                         /*
429                          * counting down
430                          */
431                         if (dorandom)
432                                 *lastport = first -
433                                             (arc4random() % (first - last));
434                         count = first - last;
435
436                         do {
437                                 if (count-- < 0)        /* completely used? */
438                                         return (EADDRNOTAVAIL);
439                                 --*lastport;
440                                 if (*lastport > first || *lastport < last)
441                                         *lastport = first;
442                                 lport = htons(*lastport);
443                         } while (in_pcblookup_local(pcbinfo, laddr, lport,
444                             wild));
445                 } else {
446                         /*
447                          * counting up
448                          */
449                         if (dorandom)
450                                 *lastport = first +
451                                             (arc4random() % (last - first));
452                         count = last - first;
453
454                         do {
455                                 if (count-- < 0)        /* completely used? */
456                                         return (EADDRNOTAVAIL);
457                                 ++*lastport;
458                                 if (*lastport < first || *lastport > last)
459                                         *lastport = first;
460                                 lport = htons(*lastport);
461                         } while (in_pcblookup_local(pcbinfo, laddr, lport,
462                             wild));
463                 }
464         }
465         if (prison_ip(cred, 0, &laddr.s_addr))
466                 return (EINVAL);
467         *laddrp = laddr.s_addr;
468         *lportp = lport;
469         return (0);
470 }
471
472 /*
473  * Connect from a socket to a specified address.
474  * Both address and port must be specified in argument sin.
475  * If don't have a local address for this socket yet,
476  * then pick one.
477  */
478 int
479 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
480 {
481         u_short lport, fport;
482         in_addr_t laddr, faddr;
483         int anonport, error;
484
485         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
486         INP_LOCK_ASSERT(inp);
487
488         lport = inp->inp_lport;
489         laddr = inp->inp_laddr.s_addr;
490         anonport = (lport == 0);
491         error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
492             NULL, cred);
493         if (error)
494                 return (error);
495
496         /* Do the initial binding of the local address if required. */
497         if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
498                 inp->inp_lport = lport;
499                 inp->inp_laddr.s_addr = laddr;
500                 if (in_pcbinshash(inp) != 0) {
501                         inp->inp_laddr.s_addr = INADDR_ANY;
502                         inp->inp_lport = 0;
503                         return (EAGAIN);
504                 }
505         }
506
507         /* Commit the remaining changes. */
508         inp->inp_lport = lport;
509         inp->inp_laddr.s_addr = laddr;
510         inp->inp_faddr.s_addr = faddr;
511         inp->inp_fport = fport;
512         in_pcbrehash(inp);
513 #ifdef IPSEC
514         if (inp->inp_socket->so_type == SOCK_STREAM)
515                 ipsec_pcbconn(inp->inp_sp);
516 #endif
517         if (anonport)
518                 inp->inp_flags |= INP_ANONPORT;
519         return (0);
520 }
521
522 /*
523  * Set up for a connect from a socket to the specified address.
524  * On entry, *laddrp and *lportp should contain the current local
525  * address and port for the PCB; these are updated to the values
526  * that should be placed in inp_laddr and inp_lport to complete
527  * the connect.
528  *
529  * On success, *faddrp and *fportp will be set to the remote address
530  * and port. These are not updated in the error case.
531  *
532  * If the operation fails because the connection already exists,
533  * *oinpp will be set to the PCB of that connection so that the
534  * caller can decide to override it. In all other cases, *oinpp
535  * is set to NULL.
536  */
537 int
538 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
539     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
540     struct inpcb **oinpp, struct ucred *cred)
541 {
542         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
543         struct in_ifaddr *ia;
544         struct sockaddr_in sa;
545         struct ucred *socred;
546         struct inpcb *oinp;
547         struct in_addr laddr, faddr;
548         u_short lport, fport;
549         int error;
550
551         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
552         INP_LOCK_ASSERT(inp);
553
554         if (oinpp != NULL)
555                 *oinpp = NULL;
556         if (nam->sa_len != sizeof (*sin))
557                 return (EINVAL);
558         if (sin->sin_family != AF_INET)
559                 return (EAFNOSUPPORT);
560         if (sin->sin_port == 0)
561                 return (EADDRNOTAVAIL);
562         laddr.s_addr = *laddrp;
563         lport = *lportp;
564         faddr = sin->sin_addr;
565         fport = sin->sin_port;
566         socred = inp->inp_socket->so_cred;
567         if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
568                 bzero(&sa, sizeof(sa));
569                 sa.sin_addr.s_addr = htonl(prison_getip(socred));
570                 sa.sin_len = sizeof(sa);
571                 sa.sin_family = AF_INET;
572                 error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
573                     &laddr.s_addr, &lport, cred);
574                 if (error)
575                         return (error);
576         }
577         if (!TAILQ_EMPTY(&in_ifaddrhead)) {
578                 /*
579                  * If the destination address is INADDR_ANY,
580                  * use the primary local address.
581                  * If the supplied address is INADDR_BROADCAST,
582                  * and the primary interface supports broadcast,
583                  * choose the broadcast address for that interface.
584                  */
585                 if (faddr.s_addr == INADDR_ANY)
586                         faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
587                 else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
588                     (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
589                     IFF_BROADCAST))
590                         faddr = satosin(&TAILQ_FIRST(
591                             &in_ifaddrhead)->ia_broadaddr)->sin_addr;
592         }
593         if (laddr.s_addr == INADDR_ANY) {
594                 struct route sro;
595
596                 bzero(&sro, sizeof(sro));
597                 ia = (struct in_ifaddr *)0;
598                 /*
599                  * If route is known our src addr is taken from the i/f,
600                  * else punt.
601                  */
602                 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) {
603                         /* Find out route to destination */
604                         sro.ro_dst.sa_family = AF_INET;
605                         sro.ro_dst.sa_len = sizeof(struct sockaddr_in);
606                         ((struct sockaddr_in *)&sro.ro_dst)->sin_addr = faddr;
607                         rtalloc_ign(&sro, RTF_CLONING);
608                 }
609                 /*
610                  * If we found a route, use the address
611                  * corresponding to the outgoing interface.
612                  */
613                 if (sro.ro_rt) {
614                         ia = ifatoia(sro.ro_rt->rt_ifa);
615                         RTFREE(sro.ro_rt);
616                 }
617                 if (ia == 0) {
618                         bzero(&sa, sizeof(sa));
619                         sa.sin_addr = faddr;
620                         sa.sin_len = sizeof(sa);
621                         sa.sin_family = AF_INET;
622
623                         ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
624                         if (ia == 0)
625                                 ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
626                         if (ia == 0)
627                                 return (ENETUNREACH);
628                 }
629                 /*
630                  * If the destination address is multicast and an outgoing
631                  * interface has been set as a multicast option, use the
632                  * address of that interface as our source address.
633                  */
634                 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
635                     inp->inp_moptions != NULL) {
636                         struct ip_moptions *imo;
637                         struct ifnet *ifp;
638
639                         imo = inp->inp_moptions;
640                         if (imo->imo_multicast_ifp != NULL) {
641                                 ifp = imo->imo_multicast_ifp;
642                                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
643                                         if (ia->ia_ifp == ifp)
644                                                 break;
645                                 if (ia == 0)
646                                         return (EADDRNOTAVAIL);
647                         }
648                 }
649                 laddr = ia->ia_addr.sin_addr;
650         }
651
652         oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
653             0, NULL);
654         if (oinp != NULL) {
655                 if (oinpp != NULL)
656                         *oinpp = oinp;
657                 return (EADDRINUSE);
658         }
659         if (lport == 0) {
660                 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
661                     cred);
662                 if (error)
663                         return (error);
664         }
665         *laddrp = laddr.s_addr;
666         *lportp = lport;
667         *faddrp = faddr.s_addr;
668         *fportp = fport;
669         return (0);
670 }
671
672 void
673 in_pcbdisconnect(struct inpcb *inp)
674 {
675
676         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
677         INP_LOCK_ASSERT(inp);
678
679         inp->inp_faddr.s_addr = INADDR_ANY;
680         inp->inp_fport = 0;
681         in_pcbrehash(inp);
682 #ifdef IPSEC
683         ipsec_pcbdisconn(inp->inp_sp);
684 #endif
685         if (inp->inp_socket->so_state & SS_NOFDREF)
686                 in_pcbdetach(inp);
687 }
688
689 void
690 in_pcbdetach(struct inpcb *inp)
691 {
692         struct socket *so = inp->inp_socket;
693         struct inpcbinfo *ipi = inp->inp_pcbinfo;
694
695         INP_INFO_WLOCK_ASSERT(ipi);
696         INP_LOCK_ASSERT(inp);
697
698 #if defined(IPSEC) || defined(FAST_IPSEC)
699         ipsec4_delete_pcbpolicy(inp);
700 #endif /*IPSEC*/
701         inp->inp_gencnt = ++ipi->ipi_gencnt;
702         in_pcbremlists(inp);
703         if (so) {
704                 ACCEPT_LOCK();
705                 SOCK_LOCK(so);
706                 so->so_pcb = NULL;
707                 sotryfree(so);
708         }
709         if (inp->inp_options)
710                 (void)m_free(inp->inp_options);
711         ip_freemoptions(inp->inp_moptions);
712         inp->inp_vflag = 0;
713         INP_LOCK_DESTROY(inp);
714 #ifdef MAC
715         mac_destroy_inpcb(inp);
716 #endif
717         uma_zfree(ipi->ipi_zone, inp);
718 }
719
720 struct sockaddr *
721 in_sockaddr(in_port_t port, struct in_addr *addr_p)
722 {
723         struct sockaddr_in *sin;
724
725         MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
726                 M_WAITOK | M_ZERO);
727         sin->sin_family = AF_INET;
728         sin->sin_len = sizeof(*sin);
729         sin->sin_addr = *addr_p;
730         sin->sin_port = port;
731
732         return (struct sockaddr *)sin;
733 }
734
735 /*
736  * The wrapper function will pass down the pcbinfo for this function to lock.
737  * The socket must have a valid
738  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
739  * except through a kernel programming error, so it is acceptable to panic
740  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
741  * because there actually /is/ a programming error somewhere... XXX)
742  */
743 int
744 in_setsockaddr(struct socket *so, struct sockaddr **nam,
745     struct inpcbinfo *pcbinfo)
746 {
747         struct inpcb *inp;
748         struct in_addr addr;
749         in_port_t port;
750
751         INP_INFO_RLOCK(pcbinfo);
752         inp = sotoinpcb(so);
753         if (!inp) {
754                 INP_INFO_RUNLOCK(pcbinfo);
755                 return ECONNRESET;
756         }
757         INP_LOCK(inp);
758         port = inp->inp_lport;
759         addr = inp->inp_laddr;
760         INP_UNLOCK(inp);
761         INP_INFO_RUNLOCK(pcbinfo);
762
763         *nam = in_sockaddr(port, &addr);
764         return 0;
765 }
766
767 /*
768  * The wrapper function will pass down the pcbinfo for this function to lock.
769  */
770 int
771 in_setpeeraddr(struct socket *so, struct sockaddr **nam,
772     struct inpcbinfo *pcbinfo)
773 {
774         struct inpcb *inp;
775         struct in_addr addr;
776         in_port_t port;
777
778         INP_INFO_RLOCK(pcbinfo);
779         inp = sotoinpcb(so);
780         if (!inp) {
781                 INP_INFO_RUNLOCK(pcbinfo);
782                 return ECONNRESET;
783         }
784         INP_LOCK(inp);
785         port = inp->inp_fport;
786         addr = inp->inp_faddr;
787         INP_UNLOCK(inp);
788         INP_INFO_RUNLOCK(pcbinfo);
789
790         *nam = in_sockaddr(port, &addr);
791         return 0;
792 }
793
794 void
795 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
796     struct inpcb *(*notify)(struct inpcb *, int))
797 {
798         struct inpcb *inp, *ninp;
799         struct inpcbhead *head;
800
801         INP_INFO_WLOCK(pcbinfo);
802         head = pcbinfo->listhead;
803         for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
804                 INP_LOCK(inp);
805                 ninp = LIST_NEXT(inp, inp_list);
806 #ifdef INET6
807                 if ((inp->inp_vflag & INP_IPV4) == 0) {
808                         INP_UNLOCK(inp);
809                         continue;
810                 }
811 #endif
812                 if (inp->inp_faddr.s_addr != faddr.s_addr ||
813                     inp->inp_socket == NULL) {
814                         INP_UNLOCK(inp);
815                         continue;
816                 }
817                 if ((*notify)(inp, errno))
818                         INP_UNLOCK(inp);
819         }
820         INP_INFO_WUNLOCK(pcbinfo);
821 }
822
823 void
824 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
825 {
826         struct inpcb *inp;
827         struct ip_moptions *imo;
828         int i, gap;
829
830         INP_INFO_RLOCK(pcbinfo);
831         LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
832                 INP_LOCK(inp);
833                 imo = inp->inp_moptions;
834                 if ((inp->inp_vflag & INP_IPV4) &&
835                     imo != NULL) {
836                         /*
837                          * Unselect the outgoing interface if it is being
838                          * detached.
839                          */
840                         if (imo->imo_multicast_ifp == ifp)
841                                 imo->imo_multicast_ifp = NULL;
842
843                         /*
844                          * Drop multicast group membership if we joined
845                          * through the interface being detached.
846                          */
847                         for (i = 0, gap = 0; i < imo->imo_num_memberships;
848                             i++) {
849                                 if (imo->imo_membership[i]->inm_ifp == ifp) {
850                                         in_delmulti(imo->imo_membership[i]);
851                                         gap++;
852                                 } else if (gap != 0)
853                                         imo->imo_membership[i - gap] =
854                                             imo->imo_membership[i];
855                         }
856                         imo->imo_num_memberships -= gap;
857                 }
858                 INP_UNLOCK(inp);
859         }
860         INP_INFO_RUNLOCK(pcbinfo);
861 }
862
863 /*
864  * Lookup a PCB based on the local address and port.
865  */
866 #define INP_LOOKUP_MAPPED_PCB_COST      3
867 struct inpcb *
868 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
869     u_int lport_arg, int wild_okay)
870 {
871         struct inpcb *inp;
872 #ifdef INET6
873         int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
874 #else
875         int matchwild = 3;
876 #endif
877         int wildcard;
878         u_short lport = lport_arg;
879
880         INP_INFO_WLOCK_ASSERT(pcbinfo);
881
882         if (!wild_okay) {
883                 struct inpcbhead *head;
884                 /*
885                  * Look for an unconnected (wildcard foreign addr) PCB that
886                  * matches the local address and port we're looking for.
887                  */
888                 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
889                 LIST_FOREACH(inp, head, inp_hash) {
890 #ifdef INET6
891                         if ((inp->inp_vflag & INP_IPV4) == 0)
892                                 continue;
893 #endif
894                         if (inp->inp_faddr.s_addr == INADDR_ANY &&
895                             inp->inp_laddr.s_addr == laddr.s_addr &&
896                             inp->inp_lport == lport) {
897                                 /*
898                                  * Found.
899                                  */
900                                 return (inp);
901                         }
902                 }
903                 /*
904                  * Not found.
905                  */
906                 return (NULL);
907         } else {
908                 struct inpcbporthead *porthash;
909                 struct inpcbport *phd;
910                 struct inpcb *match = NULL;
911                 /*
912                  * Best fit PCB lookup.
913                  *
914                  * First see if this local port is in use by looking on the
915                  * port hash list.
916                  */
917                 retrylookup:
918                 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
919                     pcbinfo->porthashmask)];
920                 LIST_FOREACH(phd, porthash, phd_hash) {
921                         if (phd->phd_port == lport)
922                                 break;
923                 }
924                 if (phd != NULL) {
925                         /*
926                          * Port is in use by one or more PCBs. Look for best
927                          * fit.
928                          */
929                         LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
930                                 wildcard = 0;
931 #ifdef INET6
932                                 if ((inp->inp_vflag & INP_IPV4) == 0)
933                                         continue;
934                                 /*
935                                  * We never select the PCB that has
936                                  * INP_IPV6 flag and is bound to :: if
937                                  * we have another PCB which is bound
938                                  * to 0.0.0.0.  If a PCB has the
939                                  * INP_IPV6 flag, then we set its cost
940                                  * higher than IPv4 only PCBs.
941                                  *
942                                  * Note that the case only happens
943                                  * when a socket is bound to ::, under
944                                  * the condition that the use of the
945                                  * mapped address is allowed.
946                                  */
947                                 if ((inp->inp_vflag & INP_IPV6) != 0)
948                                         wildcard += INP_LOOKUP_MAPPED_PCB_COST;
949 #endif
950                                 /*
951                                  * Clean out old time_wait sockets if they
952                                  * are clogging up needed local ports.
953                                  */
954                                 if ((inp->inp_vflag & INP_TIMEWAIT) != 0) {
955                                         if (tcp_twrecycleable((struct tcptw *)inp->inp_ppcb)) {
956                                                 INP_LOCK(inp);
957                                                 tcp_twclose((struct tcptw *)inp->inp_ppcb, 0);
958                                                 match = NULL;
959                                                 goto retrylookup;
960                                         }
961                                 }
962                                 if (inp->inp_faddr.s_addr != INADDR_ANY)
963                                         wildcard++;
964                                 if (inp->inp_laddr.s_addr != INADDR_ANY) {
965                                         if (laddr.s_addr == INADDR_ANY)
966                                                 wildcard++;
967                                         else if (inp->inp_laddr.s_addr != laddr.s_addr)
968                                                 continue;
969                                 } else {
970                                         if (laddr.s_addr != INADDR_ANY)
971                                                 wildcard++;
972                                 }
973                                 if (wildcard < matchwild) {
974                                         match = inp;
975                                         matchwild = wildcard;
976                                         if (matchwild == 0) {
977                                                 break;
978                                         }
979                                 }
980                         }
981                 }
982                 return (match);
983         }
984 }
985 #undef INP_LOOKUP_MAPPED_PCB_COST
986
987 /*
988  * Lookup PCB in hash list.
989  */
990 struct inpcb *
991 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
992     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
993     struct ifnet *ifp)
994 {
995         struct inpcbhead *head;
996         struct inpcb *inp;
997         u_short fport = fport_arg, lport = lport_arg;
998
999         INP_INFO_RLOCK_ASSERT(pcbinfo);
1000         /*
1001          * First look for an exact match.
1002          */
1003         head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
1004         LIST_FOREACH(inp, head, inp_hash) {
1005 #ifdef INET6
1006                 if ((inp->inp_vflag & INP_IPV4) == 0)
1007                         continue;
1008 #endif
1009                 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1010                     inp->inp_laddr.s_addr == laddr.s_addr &&
1011                     inp->inp_fport == fport &&
1012                     inp->inp_lport == lport) {
1013                         /*
1014                          * Found.
1015                          */
1016                         return (inp);
1017                 }
1018         }
1019         if (wildcard) {
1020                 struct inpcb *local_wild = NULL;
1021 #if defined(INET6)
1022                 struct inpcb *local_wild_mapped = NULL;
1023 #endif /* defined(INET6) */
1024
1025                 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1026                 LIST_FOREACH(inp, head, inp_hash) {
1027 #ifdef INET6
1028                         if ((inp->inp_vflag & INP_IPV4) == 0)
1029                                 continue;
1030 #endif
1031                         if (inp->inp_faddr.s_addr == INADDR_ANY &&
1032                             inp->inp_lport == lport) {
1033                                 if (ifp && ifp->if_type == IFT_FAITH &&
1034                                     (inp->inp_flags & INP_FAITH) == 0)
1035                                         continue;
1036                                 if (inp->inp_laddr.s_addr == laddr.s_addr)
1037                                         return (inp);
1038                                 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1039 #if defined(INET6)
1040                                         if (INP_CHECK_SOCKAF(inp->inp_socket,
1041                                                              AF_INET6))
1042                                                 local_wild_mapped = inp;
1043                                         else
1044 #endif /* defined(INET6) */
1045                                         local_wild = inp;
1046                                 }
1047                         }
1048                 }
1049 #if defined(INET6)
1050                 if (local_wild == NULL)
1051                         return (local_wild_mapped);
1052 #endif /* defined(INET6) */
1053                 return (local_wild);
1054         }
1055
1056         /*
1057          * Not found.
1058          */
1059         return (NULL);
1060 }
1061
1062 /*
1063  * Insert PCB onto various hash lists.
1064  */
1065 int
1066 in_pcbinshash(struct inpcb *inp)
1067 {
1068         struct inpcbhead *pcbhash;
1069         struct inpcbporthead *pcbporthash;
1070         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1071         struct inpcbport *phd;
1072         u_int32_t hashkey_faddr;
1073
1074         INP_INFO_WLOCK_ASSERT(pcbinfo);
1075 #ifdef INET6
1076         if (inp->inp_vflag & INP_IPV6)
1077                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1078         else
1079 #endif /* INET6 */
1080         hashkey_faddr = inp->inp_faddr.s_addr;
1081
1082         pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1083                  inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1084
1085         pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1086             pcbinfo->porthashmask)];
1087
1088         /*
1089          * Go through port list and look for a head for this lport.
1090          */
1091         LIST_FOREACH(phd, pcbporthash, phd_hash) {
1092                 if (phd->phd_port == inp->inp_lport)
1093                         break;
1094         }
1095         /*
1096          * If none exists, malloc one and tack it on.
1097          */
1098         if (phd == NULL) {
1099                 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1100                 if (phd == NULL) {
1101                         return (ENOBUFS); /* XXX */
1102                 }
1103                 phd->phd_port = inp->inp_lport;
1104                 LIST_INIT(&phd->phd_pcblist);
1105                 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1106         }
1107         inp->inp_phd = phd;
1108         LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1109         LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1110         return (0);
1111 }
1112
1113 /*
1114  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1115  * changed. NOTE: This does not handle the case of the lport changing (the
1116  * hashed port list would have to be updated as well), so the lport must
1117  * not change after in_pcbinshash() has been called.
1118  */
1119 void
1120 in_pcbrehash(struct inpcb *inp)
1121 {
1122         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1123         struct inpcbhead *head;
1124         u_int32_t hashkey_faddr;
1125
1126         INP_INFO_WLOCK_ASSERT(pcbinfo);
1127         INP_LOCK_ASSERT(inp);
1128 #ifdef INET6
1129         if (inp->inp_vflag & INP_IPV6)
1130                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1131         else
1132 #endif /* INET6 */
1133         hashkey_faddr = inp->inp_faddr.s_addr;
1134
1135         head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1136                 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1137
1138         LIST_REMOVE(inp, inp_hash);
1139         LIST_INSERT_HEAD(head, inp, inp_hash);
1140 }
1141
1142 /*
1143  * Remove PCB from various lists.
1144  */
1145 void
1146 in_pcbremlists(struct inpcb *inp)
1147 {
1148         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1149
1150         INP_INFO_WLOCK_ASSERT(pcbinfo);
1151         INP_LOCK_ASSERT(inp);
1152
1153         inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1154         if (inp->inp_lport) {
1155                 struct inpcbport *phd = inp->inp_phd;
1156
1157                 LIST_REMOVE(inp, inp_hash);
1158                 LIST_REMOVE(inp, inp_portlist);
1159                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1160                         LIST_REMOVE(phd, phd_hash);
1161                         free(phd, M_PCB);
1162                 }
1163         }
1164         LIST_REMOVE(inp, inp_list);
1165         pcbinfo->ipi_count--;
1166 }
1167
1168 /*
1169  * A set label operation has occurred at the socket layer, propagate the
1170  * label change into the in_pcb for the socket.
1171  */
1172 void
1173 in_pcbsosetlabel(struct socket *so)
1174 {
1175 #ifdef MAC
1176         struct inpcb *inp;
1177
1178         inp = (struct inpcb *)so->so_pcb;
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
1189  * allocation should be continued.  If more than ipport_randomcps
1190  * ports have been allocated in the last second, then we return to
1191  * sequential port allocation. We return to random allocation only
1192  * once we drop below ipport_randomcps for at least ipport_randomtime
1193  * seconds.
1194  */
1195
1196 void
1197 ipport_tick(void *xtp)
1198 {
1199         if (ipport_tcpallocs > ipport_tcplastcount + ipport_randomcps) {
1200                 ipport_stoprandom = ipport_randomtime;
1201         } else {
1202                 if (ipport_stoprandom > 0)
1203                         ipport_stoprandom--;
1204         }
1205         ipport_tcplastcount = ipport_tcpallocs;
1206         callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
1207 }