]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/in_pcb.c
Abstract inpcb drop logic, previously just setting of INP_DROPPED in TCP,
[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                         struct tcptw *tw;
326
327                         /* GROSS */
328                         if (ntohs(lport) <= ipport_reservedhigh &&
329                             ntohs(lport) >= ipport_reservedlow &&
330                             suser_cred(cred, SUSER_ALLOWJAIL))
331                                 return (EACCES);
332                         if (jailed(cred))
333                                 prison = 1;
334                         if (so->so_cred->cr_uid != 0 &&
335                             !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
336                                 t = in_pcblookup_local(inp->inp_pcbinfo,
337                                     sin->sin_addr, lport,
338                                     prison ? 0 :  INPLOOKUP_WILDCARD);
339         /*
340          * XXX
341          * This entire block sorely needs a rewrite.
342          */
343                                 if (t &&
344                                     ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
345                                     (so->so_type != SOCK_STREAM ||
346                                      ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
347                                     (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
348                                      ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
349                                      (t->inp_socket->so_options &
350                                          SO_REUSEPORT) == 0) &&
351                                     (so->so_cred->cr_uid !=
352                                      t->inp_socket->so_cred->cr_uid))
353                                         return (EADDRINUSE);
354                         }
355                         if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr))
356                                 return (EADDRNOTAVAIL);
357                         t = in_pcblookup_local(pcbinfo, sin->sin_addr,
358                             lport, prison ? 0 : wild);
359                         if (t && (t->inp_vflag & INP_TIMEWAIT)) {
360                                 /*
361                                  * XXXRW: If an incpb has had its timewait
362                                  * state recycled, we treat the address as
363                                  * being in use (for now).  This is better
364                                  * than a panic, but not desirable.
365                                  */
366                                 tw = intotw(inp);
367                                 if (tw == NULL ||
368                                     (reuseport & tw->tw_so_options) == 0)
369                                         return (EADDRINUSE);
370                         } else if (t &&
371                             (reuseport & t->inp_socket->so_options) == 0) {
372 #if defined(INET6)
373                                 if (ntohl(sin->sin_addr.s_addr) !=
374                                     INADDR_ANY ||
375                                     ntohl(t->inp_laddr.s_addr) !=
376                                     INADDR_ANY ||
377                                     INP_SOCKAF(so) ==
378                                     INP_SOCKAF(t->inp_socket))
379 #endif /* defined(INET6) */
380                                 return (EADDRINUSE);
381                         }
382                 }
383         }
384         if (*lportp != 0)
385                 lport = *lportp;
386         if (lport == 0) {
387                 u_short first, last;
388                 int count;
389
390                 if (laddr.s_addr != INADDR_ANY)
391                         if (prison_ip(cred, 0, &laddr.s_addr))
392                                 return (EINVAL);
393
394                 if (inp->inp_flags & INP_HIGHPORT) {
395                         first = ipport_hifirstauto;     /* sysctl */
396                         last  = ipport_hilastauto;
397                         lastport = &pcbinfo->lasthi;
398                 } else if (inp->inp_flags & INP_LOWPORT) {
399                         if ((error = suser_cred(cred, SUSER_ALLOWJAIL)) != 0)
400                                 return error;
401                         first = ipport_lowfirstauto;    /* 1023 */
402                         last  = ipport_lowlastauto;     /* 600 */
403                         lastport = &pcbinfo->lastlow;
404                 } else {
405                         first = ipport_firstauto;       /* sysctl */
406                         last  = ipport_lastauto;
407                         lastport = &pcbinfo->lastport;
408                 }
409                 /*
410                  * For UDP, use random port allocation as long as the user
411                  * allows it.  For TCP (and as of yet unknown) connections,
412                  * use random port allocation only if the user allows it AND
413                  * ipport_tick() allows it.
414                  */
415                 if (ipport_randomized &&
416                         (!ipport_stoprandom || pcbinfo == &udbinfo))
417                         dorandom = 1;
418                 else
419                         dorandom = 0;
420                 /*
421                  * It makes no sense to do random port allocation if
422                  * we have the only port available.
423                  */
424                 if (first == last)
425                         dorandom = 0;
426                 /* Make sure to not include UDP packets in the count. */
427                 if (pcbinfo != &udbinfo)
428                         ipport_tcpallocs++;
429                 /*
430                  * Simple check to ensure all ports are not used up causing
431                  * a deadlock here.
432                  *
433                  * We split the two cases (up and down) so that the direction
434                  * is not being tested on each round of the loop.
435                  */
436                 if (first > last) {
437                         /*
438                          * counting down
439                          */
440                         if (dorandom)
441                                 *lastport = first -
442                                             (arc4random() % (first - last));
443                         count = first - last;
444
445                         do {
446                                 if (count-- < 0)        /* completely used? */
447                                         return (EADDRNOTAVAIL);
448                                 --*lastport;
449                                 if (*lastport > first || *lastport < last)
450                                         *lastport = first;
451                                 lport = htons(*lastport);
452                         } while (in_pcblookup_local(pcbinfo, laddr, lport,
453                             wild));
454                 } else {
455                         /*
456                          * counting up
457                          */
458                         if (dorandom)
459                                 *lastport = first +
460                                             (arc4random() % (last - first));
461                         count = last - first;
462
463                         do {
464                                 if (count-- < 0)        /* completely used? */
465                                         return (EADDRNOTAVAIL);
466                                 ++*lastport;
467                                 if (*lastport < first || *lastport > last)
468                                         *lastport = first;
469                                 lport = htons(*lastport);
470                         } while (in_pcblookup_local(pcbinfo, laddr, lport,
471                             wild));
472                 }
473         }
474         if (prison_ip(cred, 0, &laddr.s_addr))
475                 return (EINVAL);
476         *laddrp = laddr.s_addr;
477         *lportp = lport;
478         return (0);
479 }
480
481 /*
482  * Connect from a socket to a specified address.
483  * Both address and port must be specified in argument sin.
484  * If don't have a local address for this socket yet,
485  * then pick one.
486  */
487 int
488 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
489 {
490         u_short lport, fport;
491         in_addr_t laddr, faddr;
492         int anonport, error;
493
494         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
495         INP_LOCK_ASSERT(inp);
496
497         lport = inp->inp_lport;
498         laddr = inp->inp_laddr.s_addr;
499         anonport = (lport == 0);
500         error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
501             NULL, cred);
502         if (error)
503                 return (error);
504
505         /* Do the initial binding of the local address if required. */
506         if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
507                 inp->inp_lport = lport;
508                 inp->inp_laddr.s_addr = laddr;
509                 if (in_pcbinshash(inp) != 0) {
510                         inp->inp_laddr.s_addr = INADDR_ANY;
511                         inp->inp_lport = 0;
512                         return (EAGAIN);
513                 }
514         }
515
516         /* Commit the remaining changes. */
517         inp->inp_lport = lport;
518         inp->inp_laddr.s_addr = laddr;
519         inp->inp_faddr.s_addr = faddr;
520         inp->inp_fport = fport;
521         in_pcbrehash(inp);
522 #ifdef IPSEC
523         if (inp->inp_socket->so_type == SOCK_STREAM)
524                 ipsec_pcbconn(inp->inp_sp);
525 #endif
526         if (anonport)
527                 inp->inp_flags |= INP_ANONPORT;
528         return (0);
529 }
530
531 /*
532  * Set up for a connect from a socket to the specified address.
533  * On entry, *laddrp and *lportp should contain the current local
534  * address and port for the PCB; these are updated to the values
535  * that should be placed in inp_laddr and inp_lport to complete
536  * the connect.
537  *
538  * On success, *faddrp and *fportp will be set to the remote address
539  * and port. These are not updated in the error case.
540  *
541  * If the operation fails because the connection already exists,
542  * *oinpp will be set to the PCB of that connection so that the
543  * caller can decide to override it. In all other cases, *oinpp
544  * is set to NULL.
545  */
546 int
547 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
548     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
549     struct inpcb **oinpp, struct ucred *cred)
550 {
551         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
552         struct in_ifaddr *ia;
553         struct sockaddr_in sa;
554         struct ucred *socred;
555         struct inpcb *oinp;
556         struct in_addr laddr, faddr;
557         u_short lport, fport;
558         int error;
559
560         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
561         INP_LOCK_ASSERT(inp);
562
563         if (oinpp != NULL)
564                 *oinpp = NULL;
565         if (nam->sa_len != sizeof (*sin))
566                 return (EINVAL);
567         if (sin->sin_family != AF_INET)
568                 return (EAFNOSUPPORT);
569         if (sin->sin_port == 0)
570                 return (EADDRNOTAVAIL);
571         laddr.s_addr = *laddrp;
572         lport = *lportp;
573         faddr = sin->sin_addr;
574         fport = sin->sin_port;
575         socred = inp->inp_socket->so_cred;
576         if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
577                 bzero(&sa, sizeof(sa));
578                 sa.sin_addr.s_addr = htonl(prison_getip(socred));
579                 sa.sin_len = sizeof(sa);
580                 sa.sin_family = AF_INET;
581                 error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
582                     &laddr.s_addr, &lport, cred);
583                 if (error)
584                         return (error);
585         }
586         if (!TAILQ_EMPTY(&in_ifaddrhead)) {
587                 /*
588                  * If the destination address is INADDR_ANY,
589                  * use the primary local address.
590                  * If the supplied address is INADDR_BROADCAST,
591                  * and the primary interface supports broadcast,
592                  * choose the broadcast address for that interface.
593                  */
594                 if (faddr.s_addr == INADDR_ANY)
595                         faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
596                 else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
597                     (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
598                     IFF_BROADCAST))
599                         faddr = satosin(&TAILQ_FIRST(
600                             &in_ifaddrhead)->ia_broadaddr)->sin_addr;
601         }
602         if (laddr.s_addr == INADDR_ANY) {
603                 ia = (struct in_ifaddr *)0;
604                 /*
605                  * If route is known our src addr is taken from the i/f,
606                  * else punt.
607                  *
608                  * Find out route to destination
609                  */
610                 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
611                         ia = ip_rtaddr(faddr);
612                 /*
613                  * If we found a route, use the address corresponding to
614                  * the outgoing interface.
615                  * 
616                  * Otherwise assume faddr is reachable on a directly connected
617                  * network and try to find a corresponding interface to take
618                  * the source address from.
619                  */
620                 if (ia == 0) {
621                         bzero(&sa, sizeof(sa));
622                         sa.sin_addr = faddr;
623                         sa.sin_len = sizeof(sa);
624                         sa.sin_family = AF_INET;
625
626                         ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
627                         if (ia == 0)
628                                 ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
629                         if (ia == 0)
630                                 return (ENETUNREACH);
631                 }
632                 /*
633                  * If the destination address is multicast and an outgoing
634                  * interface has been set as a multicast option, use the
635                  * address of that interface as our source address.
636                  */
637                 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
638                     inp->inp_moptions != NULL) {
639                         struct ip_moptions *imo;
640                         struct ifnet *ifp;
641
642                         imo = inp->inp_moptions;
643                         if (imo->imo_multicast_ifp != NULL) {
644                                 ifp = imo->imo_multicast_ifp;
645                                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
646                                         if (ia->ia_ifp == ifp)
647                                                 break;
648                                 if (ia == 0)
649                                         return (EADDRNOTAVAIL);
650                         }
651                 }
652                 laddr = ia->ia_addr.sin_addr;
653         }
654
655         oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
656             0, NULL);
657         if (oinp != NULL) {
658                 if (oinpp != NULL)
659                         *oinpp = oinp;
660                 return (EADDRINUSE);
661         }
662         if (lport == 0) {
663                 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
664                     cred);
665                 if (error)
666                         return (error);
667         }
668         *laddrp = laddr.s_addr;
669         *lportp = lport;
670         *faddrp = faddr.s_addr;
671         *fportp = fport;
672         return (0);
673 }
674
675 void
676 in_pcbdisconnect(struct inpcb *inp)
677 {
678
679         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
680         INP_LOCK_ASSERT(inp);
681
682         inp->inp_faddr.s_addr = INADDR_ANY;
683         inp->inp_fport = 0;
684         in_pcbrehash(inp);
685 #ifdef IPSEC
686         ipsec_pcbdisconn(inp->inp_sp);
687 #endif
688 }
689
690 /*
691  * In the old world order, in_pcbdetach() served two functions: to detach the
692  * pcb from the socket/potentially free the socket, and to free the pcb
693  * itself.  In the new world order, the protocol code is responsible for
694  * managing the relationship with the socket, and this code simply frees the
695  * pcb.
696  */
697 void
698 in_pcbdetach(struct inpcb *inp)
699 {
700
701         KASSERT(inp->inp_socket != NULL, ("in_pcbdetach: inp_socket == NULL"));
702         inp->inp_socket->so_pcb = NULL;
703         inp->inp_socket = NULL;
704 }
705
706 void
707 in_pcbfree(struct inpcb *inp)
708 {
709         struct inpcbinfo *ipi = inp->inp_pcbinfo;
710
711         KASSERT(inp->inp_socket == NULL, ("in_pcbfree: inp_socket != NULL"));
712         INP_INFO_WLOCK_ASSERT(ipi);
713         INP_LOCK_ASSERT(inp);
714
715 #if defined(IPSEC) || defined(FAST_IPSEC)
716         ipsec4_delete_pcbpolicy(inp);
717 #endif /*IPSEC*/
718         inp->inp_gencnt = ++ipi->ipi_gencnt;
719         in_pcbremlists(inp);
720         if (inp->inp_options)
721                 (void)m_free(inp->inp_options);
722         ip_freemoptions(inp->inp_moptions);
723         inp->inp_vflag = 0;
724         INP_LOCK_DESTROY(inp);
725 #ifdef MAC
726         mac_destroy_inpcb(inp);
727 #endif
728         uma_zfree(ipi->ipi_zone, inp);
729 }
730
731 /*
732  * TCP needs to maintain its inpcb structure after the TCP connection has
733  * been torn down.  However, it must be disconnected from the inpcb hashes as
734  * it must not prevent binding of future connections to the same port/ip
735  * combination by other inpcbs.
736  */
737 void
738 in_pcbdrop(struct inpcb *inp)
739 {
740         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
741
742         INP_INFO_WLOCK_ASSERT(pcbinfo);
743         INP_LOCK_ASSERT(inp);
744
745         inp->inp_vflag |= INP_DROPPED;
746         if (inp->inp_lport) {
747                 struct inpcbport *phd = inp->inp_phd;
748
749                 LIST_REMOVE(inp, inp_hash);
750                 LIST_REMOVE(inp, inp_portlist);
751                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
752                         LIST_REMOVE(phd, phd_hash);
753                         free(phd, M_PCB);
754                 }
755                 inp->inp_lport = 0;
756         }
757 }
758
759 struct sockaddr *
760 in_sockaddr(in_port_t port, struct in_addr *addr_p)
761 {
762         struct sockaddr_in *sin;
763
764         MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
765                 M_WAITOK | M_ZERO);
766         sin->sin_family = AF_INET;
767         sin->sin_len = sizeof(*sin);
768         sin->sin_addr = *addr_p;
769         sin->sin_port = port;
770
771         return (struct sockaddr *)sin;
772 }
773
774 /*
775  * The wrapper function will pass down the pcbinfo for this function to lock.
776  * The socket must have a valid
777  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
778  * except through a kernel programming error, so it is acceptable to panic
779  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
780  * because there actually /is/ a programming error somewhere... XXX)
781  */
782 int
783 in_setsockaddr(struct socket *so, struct sockaddr **nam,
784     struct inpcbinfo *pcbinfo)
785 {
786         struct inpcb *inp;
787         struct in_addr addr;
788         in_port_t port;
789
790         inp = sotoinpcb(so);
791         KASSERT(inp != NULL, ("in_setsockaddr: inp == NULL"));
792
793         INP_LOCK(inp);
794         port = inp->inp_lport;
795         addr = inp->inp_laddr;
796         INP_UNLOCK(inp);
797
798         *nam = in_sockaddr(port, &addr);
799         return 0;
800 }
801
802 /*
803  * The wrapper function will pass down the pcbinfo for this function to lock.
804  */
805 int
806 in_setpeeraddr(struct socket *so, struct sockaddr **nam,
807     struct inpcbinfo *pcbinfo)
808 {
809         struct inpcb *inp;
810         struct in_addr addr;
811         in_port_t port;
812
813         inp = sotoinpcb(so);
814         KASSERT(inp != NULL, ("in_setpeeraddr: inp == NULL"));
815
816         INP_LOCK(inp);
817         port = inp->inp_fport;
818         addr = inp->inp_faddr;
819         INP_UNLOCK(inp);
820
821         *nam = in_sockaddr(port, &addr);
822         return 0;
823 }
824
825 void
826 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
827     struct inpcb *(*notify)(struct inpcb *, int))
828 {
829         struct inpcb *inp, *ninp;
830         struct inpcbhead *head;
831
832         INP_INFO_WLOCK(pcbinfo);
833         head = pcbinfo->listhead;
834         for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
835                 INP_LOCK(inp);
836                 ninp = LIST_NEXT(inp, inp_list);
837 #ifdef INET6
838                 if ((inp->inp_vflag & INP_IPV4) == 0) {
839                         INP_UNLOCK(inp);
840                         continue;
841                 }
842 #endif
843                 if (inp->inp_faddr.s_addr != faddr.s_addr ||
844                     inp->inp_socket == NULL) {
845                         INP_UNLOCK(inp);
846                         continue;
847                 }
848                 if ((*notify)(inp, errno))
849                         INP_UNLOCK(inp);
850         }
851         INP_INFO_WUNLOCK(pcbinfo);
852 }
853
854 void
855 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
856 {
857         struct inpcb *inp;
858         struct ip_moptions *imo;
859         int i, gap;
860
861         INP_INFO_RLOCK(pcbinfo);
862         LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
863                 INP_LOCK(inp);
864                 imo = inp->inp_moptions;
865                 if ((inp->inp_vflag & INP_IPV4) &&
866                     imo != NULL) {
867                         /*
868                          * Unselect the outgoing interface if it is being
869                          * detached.
870                          */
871                         if (imo->imo_multicast_ifp == ifp)
872                                 imo->imo_multicast_ifp = NULL;
873
874                         /*
875                          * Drop multicast group membership if we joined
876                          * through the interface being detached.
877                          */
878                         for (i = 0, gap = 0; i < imo->imo_num_memberships;
879                             i++) {
880                                 if (imo->imo_membership[i]->inm_ifp == ifp) {
881                                         in_delmulti(imo->imo_membership[i]);
882                                         gap++;
883                                 } else if (gap != 0)
884                                         imo->imo_membership[i - gap] =
885                                             imo->imo_membership[i];
886                         }
887                         imo->imo_num_memberships -= gap;
888                 }
889                 INP_UNLOCK(inp);
890         }
891         INP_INFO_RUNLOCK(pcbinfo);
892 }
893
894 /*
895  * Lookup a PCB based on the local address and port.
896  */
897 #define INP_LOOKUP_MAPPED_PCB_COST      3
898 struct inpcb *
899 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
900     u_int lport_arg, int wild_okay)
901 {
902         struct inpcb *inp;
903         struct tcptw *tw;
904 #ifdef INET6
905         int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
906 #else
907         int matchwild = 3;
908 #endif
909         int wildcard;
910         u_short lport = lport_arg;
911
912         INP_INFO_WLOCK_ASSERT(pcbinfo);
913
914         if (!wild_okay) {
915                 struct inpcbhead *head;
916                 /*
917                  * Look for an unconnected (wildcard foreign addr) PCB that
918                  * matches the local address and port we're looking for.
919                  */
920                 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
921                 LIST_FOREACH(inp, head, inp_hash) {
922 #ifdef INET6
923                         if ((inp->inp_vflag & INP_IPV4) == 0)
924                                 continue;
925 #endif
926                         if (inp->inp_faddr.s_addr == INADDR_ANY &&
927                             inp->inp_laddr.s_addr == laddr.s_addr &&
928                             inp->inp_lport == lport) {
929                                 /*
930                                  * Found.
931                                  */
932                                 return (inp);
933                         }
934                 }
935                 /*
936                  * Not found.
937                  */
938                 return (NULL);
939         } else {
940                 struct inpcbporthead *porthash;
941                 struct inpcbport *phd;
942                 struct inpcb *match = NULL;
943                 /*
944                  * Best fit PCB lookup.
945                  *
946                  * First see if this local port is in use by looking on the
947                  * port hash list.
948                  */
949                 retrylookup:
950                 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
951                     pcbinfo->porthashmask)];
952                 LIST_FOREACH(phd, porthash, phd_hash) {
953                         if (phd->phd_port == lport)
954                                 break;
955                 }
956                 if (phd != NULL) {
957                         /*
958                          * Port is in use by one or more PCBs. Look for best
959                          * fit.
960                          */
961                         LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
962                                 wildcard = 0;
963 #ifdef INET6
964                                 if ((inp->inp_vflag & INP_IPV4) == 0)
965                                         continue;
966                                 /*
967                                  * We never select the PCB that has
968                                  * INP_IPV6 flag and is bound to :: if
969                                  * we have another PCB which is bound
970                                  * to 0.0.0.0.  If a PCB has the
971                                  * INP_IPV6 flag, then we set its cost
972                                  * higher than IPv4 only PCBs.
973                                  *
974                                  * Note that the case only happens
975                                  * when a socket is bound to ::, under
976                                  * the condition that the use of the
977                                  * mapped address is allowed.
978                                  */
979                                 if ((inp->inp_vflag & INP_IPV6) != 0)
980                                         wildcard += INP_LOOKUP_MAPPED_PCB_COST;
981 #endif
982                                 /*
983                                  * Clean out old time_wait sockets if they
984                                  * are clogging up needed local ports.
985                                  */
986                                 if ((inp->inp_vflag & INP_TIMEWAIT) != 0) {
987                                         tw = intotw(inp);
988                                         if (tw != NULL &&
989                                             tcp_twrecycleable(tw)) {
990                                                 INP_LOCK(inp);
991                                                 tcp_twclose(tw, 0);
992                                                 match = NULL;
993                                                 goto retrylookup;
994                                         }
995                                 }
996                                 if (inp->inp_faddr.s_addr != INADDR_ANY)
997                                         wildcard++;
998                                 if (inp->inp_laddr.s_addr != INADDR_ANY) {
999                                         if (laddr.s_addr == INADDR_ANY)
1000                                                 wildcard++;
1001                                         else if (inp->inp_laddr.s_addr != laddr.s_addr)
1002                                                 continue;
1003                                 } else {
1004                                         if (laddr.s_addr != INADDR_ANY)
1005                                                 wildcard++;
1006                                 }
1007                                 if (wildcard < matchwild) {
1008                                         match = inp;
1009                                         matchwild = wildcard;
1010                                         if (matchwild == 0) {
1011                                                 break;
1012                                         }
1013                                 }
1014                         }
1015                 }
1016                 return (match);
1017         }
1018 }
1019 #undef INP_LOOKUP_MAPPED_PCB_COST
1020
1021 /*
1022  * Lookup PCB in hash list.
1023  */
1024 struct inpcb *
1025 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1026     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1027     struct ifnet *ifp)
1028 {
1029         struct inpcbhead *head;
1030         struct inpcb *inp;
1031         u_short fport = fport_arg, lport = lport_arg;
1032
1033         INP_INFO_RLOCK_ASSERT(pcbinfo);
1034
1035         /*
1036          * First look for an exact match.
1037          */
1038         head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
1039         LIST_FOREACH(inp, head, inp_hash) {
1040 #ifdef INET6
1041                 if ((inp->inp_vflag & INP_IPV4) == 0)
1042                         continue;
1043 #endif
1044                 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1045                     inp->inp_laddr.s_addr == laddr.s_addr &&
1046                     inp->inp_fport == fport &&
1047                     inp->inp_lport == lport) {
1048                         /*
1049                          * Found.
1050                          */
1051                         return (inp);
1052                 }
1053         }
1054         if (wildcard) {
1055                 struct inpcb *local_wild = NULL;
1056 #if defined(INET6)
1057                 struct inpcb *local_wild_mapped = NULL;
1058 #endif /* defined(INET6) */
1059
1060                 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1061                 LIST_FOREACH(inp, head, inp_hash) {
1062 #ifdef INET6
1063                         if ((inp->inp_vflag & INP_IPV4) == 0)
1064                                 continue;
1065 #endif
1066                         if (inp->inp_faddr.s_addr == INADDR_ANY &&
1067                             inp->inp_lport == lport) {
1068                                 if (ifp && ifp->if_type == IFT_FAITH &&
1069                                     (inp->inp_flags & INP_FAITH) == 0)
1070                                         continue;
1071                                 if (inp->inp_laddr.s_addr == laddr.s_addr)
1072                                         return (inp);
1073                                 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1074 #if defined(INET6)
1075                                         if (INP_CHECK_SOCKAF(inp->inp_socket,
1076                                                              AF_INET6))
1077                                                 local_wild_mapped = inp;
1078                                         else
1079 #endif /* defined(INET6) */
1080                                         local_wild = inp;
1081                                 }
1082                         }
1083                 }
1084 #if defined(INET6)
1085                 if (local_wild == NULL)
1086                         return (local_wild_mapped);
1087 #endif /* defined(INET6) */
1088                 return (local_wild);
1089         }
1090
1091         /*
1092          * Not found.
1093          */
1094         return (NULL);
1095 }
1096
1097 /*
1098  * Insert PCB onto various hash lists.
1099  */
1100 int
1101 in_pcbinshash(struct inpcb *inp)
1102 {
1103         struct inpcbhead *pcbhash;
1104         struct inpcbporthead *pcbporthash;
1105         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1106         struct inpcbport *phd;
1107         u_int32_t hashkey_faddr;
1108
1109         INP_INFO_WLOCK_ASSERT(pcbinfo);
1110         INP_LOCK_ASSERT(inp);
1111
1112 #ifdef INET6
1113         if (inp->inp_vflag & INP_IPV6)
1114                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1115         else
1116 #endif /* INET6 */
1117         hashkey_faddr = inp->inp_faddr.s_addr;
1118
1119         pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1120                  inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1121
1122         pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1123             pcbinfo->porthashmask)];
1124
1125         /*
1126          * Go through port list and look for a head for this lport.
1127          */
1128         LIST_FOREACH(phd, pcbporthash, phd_hash) {
1129                 if (phd->phd_port == inp->inp_lport)
1130                         break;
1131         }
1132         /*
1133          * If none exists, malloc one and tack it on.
1134          */
1135         if (phd == NULL) {
1136                 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1137                 if (phd == NULL) {
1138                         return (ENOBUFS); /* XXX */
1139                 }
1140                 phd->phd_port = inp->inp_lport;
1141                 LIST_INIT(&phd->phd_pcblist);
1142                 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1143         }
1144         inp->inp_phd = phd;
1145         LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1146         LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1147         return (0);
1148 }
1149
1150 /*
1151  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1152  * changed. NOTE: This does not handle the case of the lport changing (the
1153  * hashed port list would have to be updated as well), so the lport must
1154  * not change after in_pcbinshash() has been called.
1155  */
1156 void
1157 in_pcbrehash(struct inpcb *inp)
1158 {
1159         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1160         struct inpcbhead *head;
1161         u_int32_t hashkey_faddr;
1162
1163         INP_INFO_WLOCK_ASSERT(pcbinfo);
1164         INP_LOCK_ASSERT(inp);
1165
1166 #ifdef INET6
1167         if (inp->inp_vflag & INP_IPV6)
1168                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1169         else
1170 #endif /* INET6 */
1171         hashkey_faddr = inp->inp_faddr.s_addr;
1172
1173         head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1174                 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1175
1176         LIST_REMOVE(inp, inp_hash);
1177         LIST_INSERT_HEAD(head, inp, inp_hash);
1178 }
1179
1180 /*
1181  * Remove PCB from various lists.
1182  */
1183 void
1184 in_pcbremlists(struct inpcb *inp)
1185 {
1186         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1187
1188         INP_INFO_WLOCK_ASSERT(pcbinfo);
1189         INP_LOCK_ASSERT(inp);
1190
1191         inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1192         if (inp->inp_lport) {
1193                 struct inpcbport *phd = inp->inp_phd;
1194
1195                 LIST_REMOVE(inp, inp_hash);
1196                 LIST_REMOVE(inp, inp_portlist);
1197                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1198                         LIST_REMOVE(phd, phd_hash);
1199                         free(phd, M_PCB);
1200                 }
1201         }
1202         LIST_REMOVE(inp, inp_list);
1203         pcbinfo->ipi_count--;
1204 }
1205
1206 /*
1207  * A set label operation has occurred at the socket layer, propagate the
1208  * label change into the in_pcb for the socket.
1209  */
1210 void
1211 in_pcbsosetlabel(struct socket *so)
1212 {
1213 #ifdef MAC
1214         struct inpcb *inp;
1215
1216         inp = sotoinpcb(so);
1217         KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1218
1219         INP_LOCK(inp);
1220         SOCK_LOCK(so);
1221         mac_inpcb_sosetlabel(so, inp);
1222         SOCK_UNLOCK(so);
1223         INP_UNLOCK(inp);
1224 #endif
1225 }
1226
1227 /*
1228  * ipport_tick runs once per second, determining if random port
1229  * allocation should be continued.  If more than ipport_randomcps
1230  * ports have been allocated in the last second, then we return to
1231  * sequential port allocation. We return to random allocation only
1232  * once we drop below ipport_randomcps for at least ipport_randomtime
1233  * seconds.
1234  */
1235
1236 void
1237 ipport_tick(void *xtp)
1238 {
1239         if (ipport_tcpallocs > ipport_tcplastcount + ipport_randomcps) {
1240                 ipport_stoprandom = ipport_randomtime;
1241         } else {
1242                 if (ipport_stoprandom > 0)
1243                         ipport_stoprandom--;
1244         }
1245         ipport_tcplastcount = ipport_tcpallocs;
1246         callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
1247 }