]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/in_pcb.c
This commit was generated by cvs2svn to compensate for changes in r159609,
[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
741         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
742         INP_LOCK_ASSERT(inp);
743
744         inp->inp_vflag |= INP_DROPPED;
745         if (inp->inp_lport) {
746                 struct inpcbport *phd = inp->inp_phd;
747
748                 LIST_REMOVE(inp, inp_hash);
749                 LIST_REMOVE(inp, inp_portlist);
750                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
751                         LIST_REMOVE(phd, phd_hash);
752                         free(phd, M_PCB);
753                 }
754                 inp->inp_lport = 0;
755         }
756 }
757
758 struct sockaddr *
759 in_sockaddr(in_port_t port, struct in_addr *addr_p)
760 {
761         struct sockaddr_in *sin;
762
763         MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
764                 M_WAITOK | M_ZERO);
765         sin->sin_family = AF_INET;
766         sin->sin_len = sizeof(*sin);
767         sin->sin_addr = *addr_p;
768         sin->sin_port = port;
769
770         return (struct sockaddr *)sin;
771 }
772
773 /*
774  * The wrapper function will pass down the pcbinfo for this function to lock.
775  * The socket must have a valid
776  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
777  * except through a kernel programming error, so it is acceptable to panic
778  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
779  * because there actually /is/ a programming error somewhere... XXX)
780  */
781 int
782 in_setsockaddr(struct socket *so, struct sockaddr **nam,
783     struct inpcbinfo *pcbinfo)
784 {
785         struct inpcb *inp;
786         struct in_addr addr;
787         in_port_t port;
788
789         inp = sotoinpcb(so);
790         KASSERT(inp != NULL, ("in_setsockaddr: inp == NULL"));
791
792         INP_LOCK(inp);
793         port = inp->inp_lport;
794         addr = inp->inp_laddr;
795         INP_UNLOCK(inp);
796
797         *nam = in_sockaddr(port, &addr);
798         return 0;
799 }
800
801 /*
802  * The wrapper function will pass down the pcbinfo for this function to lock.
803  */
804 int
805 in_setpeeraddr(struct socket *so, struct sockaddr **nam,
806     struct inpcbinfo *pcbinfo)
807 {
808         struct inpcb *inp;
809         struct in_addr addr;
810         in_port_t port;
811
812         inp = sotoinpcb(so);
813         KASSERT(inp != NULL, ("in_setpeeraddr: inp == NULL"));
814
815         INP_LOCK(inp);
816         port = inp->inp_fport;
817         addr = inp->inp_faddr;
818         INP_UNLOCK(inp);
819
820         *nam = in_sockaddr(port, &addr);
821         return 0;
822 }
823
824 void
825 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
826     struct inpcb *(*notify)(struct inpcb *, int))
827 {
828         struct inpcb *inp, *ninp;
829         struct inpcbhead *head;
830
831         INP_INFO_WLOCK(pcbinfo);
832         head = pcbinfo->listhead;
833         for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
834                 INP_LOCK(inp);
835                 ninp = LIST_NEXT(inp, inp_list);
836 #ifdef INET6
837                 if ((inp->inp_vflag & INP_IPV4) == 0) {
838                         INP_UNLOCK(inp);
839                         continue;
840                 }
841 #endif
842                 if (inp->inp_faddr.s_addr != faddr.s_addr ||
843                     inp->inp_socket == NULL) {
844                         INP_UNLOCK(inp);
845                         continue;
846                 }
847                 if ((*notify)(inp, errno))
848                         INP_UNLOCK(inp);
849         }
850         INP_INFO_WUNLOCK(pcbinfo);
851 }
852
853 void
854 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
855 {
856         struct inpcb *inp;
857         struct ip_moptions *imo;
858         int i, gap;
859
860         INP_INFO_RLOCK(pcbinfo);
861         LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
862                 INP_LOCK(inp);
863                 imo = inp->inp_moptions;
864                 if ((inp->inp_vflag & INP_IPV4) &&
865                     imo != NULL) {
866                         /*
867                          * Unselect the outgoing interface if it is being
868                          * detached.
869                          */
870                         if (imo->imo_multicast_ifp == ifp)
871                                 imo->imo_multicast_ifp = NULL;
872
873                         /*
874                          * Drop multicast group membership if we joined
875                          * through the interface being detached.
876                          */
877                         for (i = 0, gap = 0; i < imo->imo_num_memberships;
878                             i++) {
879                                 if (imo->imo_membership[i]->inm_ifp == ifp) {
880                                         in_delmulti(imo->imo_membership[i]);
881                                         gap++;
882                                 } else if (gap != 0)
883                                         imo->imo_membership[i - gap] =
884                                             imo->imo_membership[i];
885                         }
886                         imo->imo_num_memberships -= gap;
887                 }
888                 INP_UNLOCK(inp);
889         }
890         INP_INFO_RUNLOCK(pcbinfo);
891 }
892
893 /*
894  * Lookup a PCB based on the local address and port.
895  */
896 #define INP_LOOKUP_MAPPED_PCB_COST      3
897 struct inpcb *
898 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
899     u_int lport_arg, int wild_okay)
900 {
901         struct inpcb *inp;
902         struct tcptw *tw;
903 #ifdef INET6
904         int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
905 #else
906         int matchwild = 3;
907 #endif
908         int wildcard;
909         u_short lport = lport_arg;
910
911         INP_INFO_WLOCK_ASSERT(pcbinfo);
912
913         if (!wild_okay) {
914                 struct inpcbhead *head;
915                 /*
916                  * Look for an unconnected (wildcard foreign addr) PCB that
917                  * matches the local address and port we're looking for.
918                  */
919                 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
920                 LIST_FOREACH(inp, head, inp_hash) {
921 #ifdef INET6
922                         if ((inp->inp_vflag & INP_IPV4) == 0)
923                                 continue;
924 #endif
925                         if (inp->inp_faddr.s_addr == INADDR_ANY &&
926                             inp->inp_laddr.s_addr == laddr.s_addr &&
927                             inp->inp_lport == lport) {
928                                 /*
929                                  * Found.
930                                  */
931                                 return (inp);
932                         }
933                 }
934                 /*
935                  * Not found.
936                  */
937                 return (NULL);
938         } else {
939                 struct inpcbporthead *porthash;
940                 struct inpcbport *phd;
941                 struct inpcb *match = NULL;
942                 /*
943                  * Best fit PCB lookup.
944                  *
945                  * First see if this local port is in use by looking on the
946                  * port hash list.
947                  */
948                 retrylookup:
949                 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
950                     pcbinfo->porthashmask)];
951                 LIST_FOREACH(phd, porthash, phd_hash) {
952                         if (phd->phd_port == lport)
953                                 break;
954                 }
955                 if (phd != NULL) {
956                         /*
957                          * Port is in use by one or more PCBs. Look for best
958                          * fit.
959                          */
960                         LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
961                                 wildcard = 0;
962 #ifdef INET6
963                                 if ((inp->inp_vflag & INP_IPV4) == 0)
964                                         continue;
965                                 /*
966                                  * We never select the PCB that has
967                                  * INP_IPV6 flag and is bound to :: if
968                                  * we have another PCB which is bound
969                                  * to 0.0.0.0.  If a PCB has the
970                                  * INP_IPV6 flag, then we set its cost
971                                  * higher than IPv4 only PCBs.
972                                  *
973                                  * Note that the case only happens
974                                  * when a socket is bound to ::, under
975                                  * the condition that the use of the
976                                  * mapped address is allowed.
977                                  */
978                                 if ((inp->inp_vflag & INP_IPV6) != 0)
979                                         wildcard += INP_LOOKUP_MAPPED_PCB_COST;
980 #endif
981                                 /*
982                                  * Clean out old time_wait sockets if they
983                                  * are clogging up needed local ports.
984                                  */
985                                 if ((inp->inp_vflag & INP_TIMEWAIT) != 0) {
986                                         tw = intotw(inp);
987                                         if (tw != NULL &&
988                                             tcp_twrecycleable(tw)) {
989                                                 INP_LOCK(inp);
990                                                 tcp_twclose(tw, 0);
991                                                 match = NULL;
992                                                 goto retrylookup;
993                                         }
994                                 }
995                                 if (inp->inp_faddr.s_addr != INADDR_ANY)
996                                         wildcard++;
997                                 if (inp->inp_laddr.s_addr != INADDR_ANY) {
998                                         if (laddr.s_addr == INADDR_ANY)
999                                                 wildcard++;
1000                                         else if (inp->inp_laddr.s_addr != laddr.s_addr)
1001                                                 continue;
1002                                 } else {
1003                                         if (laddr.s_addr != INADDR_ANY)
1004                                                 wildcard++;
1005                                 }
1006                                 if (wildcard < matchwild) {
1007                                         match = inp;
1008                                         matchwild = wildcard;
1009                                         if (matchwild == 0) {
1010                                                 break;
1011                                         }
1012                                 }
1013                         }
1014                 }
1015                 return (match);
1016         }
1017 }
1018 #undef INP_LOOKUP_MAPPED_PCB_COST
1019
1020 /*
1021  * Lookup PCB in hash list.
1022  */
1023 struct inpcb *
1024 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1025     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1026     struct ifnet *ifp)
1027 {
1028         struct inpcbhead *head;
1029         struct inpcb *inp;
1030         u_short fport = fport_arg, lport = lport_arg;
1031
1032         INP_INFO_RLOCK_ASSERT(pcbinfo);
1033
1034         /*
1035          * First look for an exact match.
1036          */
1037         head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
1038         LIST_FOREACH(inp, head, inp_hash) {
1039 #ifdef INET6
1040                 if ((inp->inp_vflag & INP_IPV4) == 0)
1041                         continue;
1042 #endif
1043                 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1044                     inp->inp_laddr.s_addr == laddr.s_addr &&
1045                     inp->inp_fport == fport &&
1046                     inp->inp_lport == lport) {
1047                         /*
1048                          * Found.
1049                          */
1050                         return (inp);
1051                 }
1052         }
1053         if (wildcard) {
1054                 struct inpcb *local_wild = NULL;
1055 #if defined(INET6)
1056                 struct inpcb *local_wild_mapped = NULL;
1057 #endif /* defined(INET6) */
1058
1059                 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1060                 LIST_FOREACH(inp, head, inp_hash) {
1061 #ifdef INET6
1062                         if ((inp->inp_vflag & INP_IPV4) == 0)
1063                                 continue;
1064 #endif
1065                         if (inp->inp_faddr.s_addr == INADDR_ANY &&
1066                             inp->inp_lport == lport) {
1067                                 if (ifp && ifp->if_type == IFT_FAITH &&
1068                                     (inp->inp_flags & INP_FAITH) == 0)
1069                                         continue;
1070                                 if (inp->inp_laddr.s_addr == laddr.s_addr)
1071                                         return (inp);
1072                                 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1073 #if defined(INET6)
1074                                         if (INP_CHECK_SOCKAF(inp->inp_socket,
1075                                                              AF_INET6))
1076                                                 local_wild_mapped = inp;
1077                                         else
1078 #endif /* defined(INET6) */
1079                                         local_wild = inp;
1080                                 }
1081                         }
1082                 }
1083 #if defined(INET6)
1084                 if (local_wild == NULL)
1085                         return (local_wild_mapped);
1086 #endif /* defined(INET6) */
1087                 return (local_wild);
1088         }
1089
1090         /*
1091          * Not found.
1092          */
1093         return (NULL);
1094 }
1095
1096 /*
1097  * Insert PCB onto various hash lists.
1098  */
1099 int
1100 in_pcbinshash(struct inpcb *inp)
1101 {
1102         struct inpcbhead *pcbhash;
1103         struct inpcbporthead *pcbporthash;
1104         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1105         struct inpcbport *phd;
1106         u_int32_t hashkey_faddr;
1107
1108         INP_INFO_WLOCK_ASSERT(pcbinfo);
1109         INP_LOCK_ASSERT(inp);
1110
1111 #ifdef INET6
1112         if (inp->inp_vflag & INP_IPV6)
1113                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1114         else
1115 #endif /* INET6 */
1116         hashkey_faddr = inp->inp_faddr.s_addr;
1117
1118         pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1119                  inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1120
1121         pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1122             pcbinfo->porthashmask)];
1123
1124         /*
1125          * Go through port list and look for a head for this lport.
1126          */
1127         LIST_FOREACH(phd, pcbporthash, phd_hash) {
1128                 if (phd->phd_port == inp->inp_lport)
1129                         break;
1130         }
1131         /*
1132          * If none exists, malloc one and tack it on.
1133          */
1134         if (phd == NULL) {
1135                 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1136                 if (phd == NULL) {
1137                         return (ENOBUFS); /* XXX */
1138                 }
1139                 phd->phd_port = inp->inp_lport;
1140                 LIST_INIT(&phd->phd_pcblist);
1141                 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1142         }
1143         inp->inp_phd = phd;
1144         LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1145         LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1146         return (0);
1147 }
1148
1149 /*
1150  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1151  * changed. NOTE: This does not handle the case of the lport changing (the
1152  * hashed port list would have to be updated as well), so the lport must
1153  * not change after in_pcbinshash() has been called.
1154  */
1155 void
1156 in_pcbrehash(struct inpcb *inp)
1157 {
1158         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1159         struct inpcbhead *head;
1160         u_int32_t hashkey_faddr;
1161
1162         INP_INFO_WLOCK_ASSERT(pcbinfo);
1163         INP_LOCK_ASSERT(inp);
1164
1165 #ifdef INET6
1166         if (inp->inp_vflag & INP_IPV6)
1167                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1168         else
1169 #endif /* INET6 */
1170         hashkey_faddr = inp->inp_faddr.s_addr;
1171
1172         head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1173                 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1174
1175         LIST_REMOVE(inp, inp_hash);
1176         LIST_INSERT_HEAD(head, inp, inp_hash);
1177 }
1178
1179 /*
1180  * Remove PCB from various lists.
1181  */
1182 void
1183 in_pcbremlists(struct inpcb *inp)
1184 {
1185         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1186
1187         INP_INFO_WLOCK_ASSERT(pcbinfo);
1188         INP_LOCK_ASSERT(inp);
1189
1190         inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1191         if (inp->inp_lport) {
1192                 struct inpcbport *phd = inp->inp_phd;
1193
1194                 LIST_REMOVE(inp, inp_hash);
1195                 LIST_REMOVE(inp, inp_portlist);
1196                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1197                         LIST_REMOVE(phd, phd_hash);
1198                         free(phd, M_PCB);
1199                 }
1200         }
1201         LIST_REMOVE(inp, inp_list);
1202         pcbinfo->ipi_count--;
1203 }
1204
1205 /*
1206  * A set label operation has occurred at the socket layer, propagate the
1207  * label change into the in_pcb for the socket.
1208  */
1209 void
1210 in_pcbsosetlabel(struct socket *so)
1211 {
1212 #ifdef MAC
1213         struct inpcb *inp;
1214
1215         inp = sotoinpcb(so);
1216         KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1217
1218         INP_LOCK(inp);
1219         SOCK_LOCK(so);
1220         mac_inpcb_sosetlabel(so, inp);
1221         SOCK_UNLOCK(so);
1222         INP_UNLOCK(inp);
1223 #endif
1224 }
1225
1226 /*
1227  * ipport_tick runs once per second, determining if random port allocation
1228  * should be continued.  If more than ipport_randomcps ports have been
1229  * allocated in the last second, then we return to sequential port
1230  * allocation. We return to random allocation only once we drop below
1231  * ipport_randomcps for at least ipport_randomtime seconds.
1232  */
1233 void
1234 ipport_tick(void *xtp)
1235 {
1236
1237         if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) {
1238                 if (ipport_stoprandom > 0)
1239                         ipport_stoprandom--;
1240         } else
1241                 ipport_stoprandom = ipport_randomtime;
1242         ipport_tcplastcount = ipport_tcpallocs;
1243         callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
1244 }