Merge branch 'vendor/TCSH'
[dragonfly.git] / sys / netinet / in_pcb.c
1 /*
2  * Copyright (c) 2004 Jeffrey M. Hsu.  All rights reserved.
3  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
4  *
5  * This code is derived from software contributed to The DragonFly Project
6  * by Jeffrey M. Hsu.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of The DragonFly Project nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific, prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /*
35  * Copyright (c) 1982, 1986, 1991, 1993, 1995
36  *      The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 4. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  *
62  *      @(#)in_pcb.c    8.4 (Berkeley) 5/24/95
63  * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.27 2004/01/02 04:06:42 ambrisko Exp $
64  */
65
66 #include "opt_ipsec.h"
67 #include "opt_inet6.h"
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/domain.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/proc.h>
78 #include <sys/priv.h>
79 #include <sys/jail.h>
80 #include <sys/kernel.h>
81 #include <sys/sysctl.h>
82
83 #include <sys/thread2.h>
84 #include <sys/socketvar2.h>
85 #include <sys/msgport2.h>
86
87 #include <machine/limits.h>
88
89 #include <net/if.h>
90 #include <net/if_types.h>
91 #include <net/route.h>
92
93 #include <netinet/in.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip_var.h>
97 #ifdef INET6
98 #include <netinet/ip6.h>
99 #include <netinet6/ip6_var.h>
100 #endif /* INET6 */
101
102 #ifdef IPSEC
103 #include <netinet6/ipsec.h>
104 #include <netproto/key/key.h>
105 #include <netproto/ipsec/esp_var.h>
106 #endif
107
108 #ifdef FAST_IPSEC
109 #if defined(IPSEC) || defined(IPSEC_ESP)
110 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
111 #endif
112
113 #include <netproto/ipsec/ipsec.h>
114 #include <netproto/ipsec/key.h>
115 #define IPSEC
116 #endif /* FAST_IPSEC */
117
118 struct in_addr zeroin_addr;
119
120 /*
121  * These configure the range of local port addresses assigned to
122  * "unspecified" outgoing connections/packets/whatever.
123  */
124 int ipport_lowfirstauto = IPPORT_RESERVED - 1;  /* 1023 */
125 int ipport_lowlastauto = IPPORT_RESERVEDSTART;  /* 600 */
126
127 int ipport_firstauto = IPPORT_RESERVED;         /* 1024 */
128 int ipport_lastauto = IPPORT_USERRESERVED;      /* 5000 */
129
130 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO;    /* 49152 */
131 int ipport_hilastauto = IPPORT_HILASTAUTO;      /* 65535 */
132
133 #define RANGECHK(var, min, max) \
134         if ((var) < (min)) { (var) = (min); } \
135         else if ((var) > (max)) { (var) = (max); }
136
137 int udpencap_enable = 1;        /* enabled by default */
138 int udpencap_port = 4500;       /* triggers decapsulation */
139
140 static int
141 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
142 {
143         int error;
144
145         error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
146         if (!error) {
147                 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
148                 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
149
150                 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
151                 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
152
153                 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
154                 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
155         }
156         return (error);
157 }
158
159 #undef RANGECHK
160
161 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
162
163 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
164            &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
165 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
166            &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
167 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
168            &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
169 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
170            &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
171 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
172            &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
173 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
174            &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
175
176 /*
177  * in_pcb.c: manage the Protocol Control Blocks.
178  *
179  * NOTE: It is assumed that most of these functions will be called from
180  * a critical section.  XXX - There are, unfortunately, a few exceptions
181  * to this rule that should be fixed.
182  *
183  * NOTE: The caller should initialize the cpu field to the cpu running the
184  * protocol stack associated with this inpcbinfo.
185  */
186
187 void
188 in_pcbinfo_init(struct inpcbinfo *pcbinfo)
189 {
190         LIST_INIT(&pcbinfo->pcblisthead);
191         pcbinfo->cpu = -1;
192         pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave), M_PCB,
193                                     M_WAITOK | M_ZERO);
194 }
195
196 struct baddynamicports baddynamicports;
197
198 /*
199  * Check if the specified port is invalid for dynamic allocation.
200  */
201 int
202 in_baddynamic(u_int16_t port, u_int16_t proto)
203 {
204         switch (proto) {
205         case IPPROTO_TCP:
206                 return (DP_ISSET(baddynamicports.tcp, port));
207         case IPPROTO_UDP:
208 #ifdef IPSEC
209                 /* Cannot preset this as it is a sysctl */
210                 if (port == udpencap_port)
211                         return (1);
212 #endif
213                 return (DP_ISSET(baddynamicports.udp, port));
214         default:
215                 return (0);
216         }
217 }
218
219
220 /*
221  * Allocate a PCB and associate it with the socket.
222  */
223 int
224 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
225 {
226         struct inpcb *inp;
227 #ifdef IPSEC
228         int error;
229 #endif
230
231         inp = kmalloc(pcbinfo->ipi_size, M_PCB, M_WAITOK|M_ZERO);
232         inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
233         inp->inp_pcbinfo = inp->inp_cpcbinfo = pcbinfo;
234         inp->inp_socket = so;
235 #ifdef IPSEC
236         error = ipsec_init_policy(so, &inp->inp_sp);
237         if (error != 0) {
238                 kfree(inp, M_PCB);
239                 return (error);
240         }
241 #endif
242 #ifdef INET6
243         if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
244                 inp->inp_flags |= IN6P_IPV6_V6ONLY;
245         if (ip6_auto_flowlabel)
246                 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
247 #endif
248         soreference(so);
249         so->so_pcb = inp;
250         LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
251         pcbinfo->ipi_count++;
252         return (0);
253 }
254
255 /*
256  * Unlink a pcb with the intention of moving it to another cpu with a
257  * different pcbinfo.  While unlinked nothing should attempt to dereference
258  * inp_pcbinfo, NULL it out so we assert if it does.
259  */
260 void
261 in_pcbunlink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
262 {
263         KKASSERT(inp->inp_pcbinfo == pcbinfo);
264
265         LIST_REMOVE(inp, inp_list);
266         pcbinfo->ipi_count--;
267         inp->inp_pcbinfo = NULL;
268 }
269
270 /*
271  * Relink a pcb into a new pcbinfo.
272  */
273 void
274 in_pcblink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
275 {
276         KKASSERT(inp->inp_pcbinfo == NULL);
277         inp->inp_pcbinfo = pcbinfo;
278         LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
279         pcbinfo->ipi_count++;
280 }
281
282 int
283 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
284 {
285         struct socket *so = inp->inp_socket;
286         unsigned short *lastport;
287         struct sockaddr_in *sin;
288         struct sockaddr_in jsin;
289         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
290         struct ucred *cred = NULL;
291         u_short lport = 0;
292         int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
293         int error;
294
295         if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
296                 return (EADDRNOTAVAIL);
297         if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
298                 return (EINVAL);        /* already bound */
299
300         if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
301                 wild = 1;    /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
302         if (td->td_proc)
303                 cred = td->td_proc->p_ucred;
304
305         /*
306          * This has to be atomic.  If the porthash is shared across multiple
307          * protocol threads (aka tcp) then the token will be non-NULL.
308          */
309         if (pcbinfo->porttoken)
310                 lwkt_gettoken(pcbinfo->porttoken);
311
312         if (nam != NULL) {
313                 sin = (struct sockaddr_in *)nam;
314                 if (nam->sa_len != sizeof *sin) {
315                         error = EINVAL;
316                         goto done;
317                 }
318 #ifdef notdef
319                 /*
320                  * We should check the family, but old programs
321                  * incorrectly fail to initialize it.
322                  */
323                 if (sin->sin_family != AF_INET) {
324                         error = EAFNOSUPPORT;
325                         goto done;
326                 }
327 #endif
328                 if (!prison_replace_wildcards(td, nam)) {
329                         error = EINVAL;
330                         goto done;
331                 }
332                 lport = sin->sin_port;
333                 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
334                         /*
335                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
336                          * allow complete duplication of binding if
337                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
338                          * and a multicast address is bound on both
339                          * new and duplicated sockets.
340                          */
341                         if (so->so_options & SO_REUSEADDR)
342                                 reuseport = SO_REUSEADDR | SO_REUSEPORT;
343                 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
344                         sin->sin_port = 0;              /* yech... */
345                         bzero(&sin->sin_zero, sizeof sin->sin_zero);
346                         if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL) {
347                                 error = EADDRNOTAVAIL;
348                                 goto done;
349                         }
350                 }
351                 if (lport != 0) {
352                         struct inpcb *t;
353
354                         /* GROSS */
355                         if (ntohs(lport) < IPPORT_RESERVED &&
356                             cred &&
357                             priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0)) {
358                                 error = EACCES;
359                                 goto done;
360                         }
361                         if (so->so_cred->cr_uid != 0 &&
362                             !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
363                                 t = in_pcblookup_local(pcbinfo,
364                                                        sin->sin_addr,
365                                                        lport,
366                                                        INPLOOKUP_WILDCARD,
367                                                        cred);
368                                 if (t &&
369                                     (!in_nullhost(sin->sin_addr) ||
370                                      !in_nullhost(t->inp_laddr) ||
371                                      (t->inp_socket->so_options &
372                                          SO_REUSEPORT) == 0) &&
373                                     (so->so_cred->cr_uid !=
374                                      t->inp_socket->so_cred->cr_uid)) {
375 #ifdef INET6
376                                         if (!in_nullhost(sin->sin_addr) ||
377                                             !in_nullhost(t->inp_laddr) ||
378                                             INP_SOCKAF(so) ==
379                                             INP_SOCKAF(t->inp_socket))
380 #endif
381                                         {
382                                                 error = EADDRINUSE;
383                                                 goto done;
384                                         }
385                                 }
386                         }
387                         if (cred && !prison_replace_wildcards(td, nam)) {
388                                 error = EADDRNOTAVAIL;
389                                 goto done;
390                         }
391                         t = in_pcblookup_local(pcbinfo, sin->sin_addr, lport,
392                                                wild, cred);
393                         if (t && !(reuseport & t->inp_socket->so_options)) {
394 #ifdef INET6
395                                 if (!in_nullhost(sin->sin_addr) ||
396                                     !in_nullhost(t->inp_laddr) ||
397                                     INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
398 #endif
399                                 {
400                                         error = EADDRINUSE;
401                                         goto done;
402                                 }
403                         }
404                 }
405                 inp->inp_laddr = sin->sin_addr;
406         }
407         if (lport == 0) {
408                 ushort first, last;
409                 int count;
410
411                 jsin.sin_family = AF_INET;
412                 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
413                 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
414                         inp->inp_laddr.s_addr = INADDR_ANY;
415                         error = EINVAL;
416                         goto done;
417                 }
418                 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
419
420                 inp->inp_flags |= INP_ANONPORT;
421
422                 if (inp->inp_flags & INP_HIGHPORT) {
423                         first = ipport_hifirstauto;     /* sysctl */
424                         last  = ipport_hilastauto;
425                         lastport = &pcbinfo->lasthi;
426                 } else if (inp->inp_flags & INP_LOWPORT) {
427                         if (cred &&
428                             (error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
429                                 inp->inp_laddr.s_addr = INADDR_ANY;
430                                 goto done;
431                         }
432                         first = ipport_lowfirstauto;    /* 1023 */
433                         last  = ipport_lowlastauto;     /* 600 */
434                         lastport = &pcbinfo->lastlow;
435                 } else {
436                         first = ipport_firstauto;       /* sysctl */
437                         last  = ipport_lastauto;
438                         lastport = &pcbinfo->lastport;
439                 }
440                 /*
441                  * Simple check to ensure all ports are not used up causing
442                  * a deadlock here.
443                  *
444                  * We split the two cases (up and down) so that the direction
445                  * is not being tested on each round of the loop.
446                  */
447                 if (first > last) {
448                         /*
449                          * counting down
450                          */
451                         count = first - last;
452
453                         do {
454                                 if (count-- < 0) {      /* completely used? */
455                                         inp->inp_laddr.s_addr = INADDR_ANY;
456                                         error = EADDRNOTAVAIL;
457                                         goto done;
458                                 }
459                                 --*lastport;
460                                 if (*lastport > first || *lastport < last)
461                                         *lastport = first;
462                                 lport = htons(*lastport);
463                         } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
464                                                     lport, wild, cred));
465                 } else {
466                         /*
467                          * counting up
468                          */
469                         count = last - first;
470
471                         do {
472                                 if (count-- < 0) {      /* completely used? */
473                                         inp->inp_laddr.s_addr = INADDR_ANY;
474                                         error = EADDRNOTAVAIL;
475                                         goto done;
476                                 }
477                                 ++*lastport;
478                                 if (*lastport < first || *lastport > last)
479                                         *lastport = first;
480                                 lport = htons(*lastport);
481                         } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
482                                                     lport, wild, cred));
483                 }
484         }
485         inp->inp_lport = lport;
486
487         jsin.sin_family = AF_INET;
488         jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
489         if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin)) {
490                 inp->inp_laddr.s_addr = INADDR_ANY;
491                 inp->inp_lport = 0;
492                 error = EINVAL;
493                 goto done;
494         }
495         inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
496
497         if (in_pcbinsporthash(inp) != 0) {
498                 inp->inp_laddr.s_addr = INADDR_ANY;
499                 inp->inp_lport = 0;
500                 error = EAGAIN;
501                 goto done;
502         }
503         error = 0;
504 done:
505         if (pcbinfo->porttoken)
506                 lwkt_reltoken(pcbinfo->porttoken);
507         return error;
508 }
509
510 static struct inpcb *
511 in_pcblookup_addrport(struct inpcbinfo *pcbinfo, struct in_addr laddr,
512     u_short lport, struct in_addr faddr, u_short fport, struct ucred *cred)
513 {
514         struct inpcb *inp;
515         struct inpcbporthead *porthash;
516         struct inpcbport *phd;
517         struct inpcb *match = NULL;
518
519         /*
520          * If the porthashbase is shared across several cpus we need
521          * to lock.
522          */
523         if (pcbinfo->porttoken)
524                 lwkt_gettoken(pcbinfo->porttoken);
525
526         /*
527          * Best fit PCB lookup.
528          *
529          * First see if this local port is in use by looking on the
530          * port hash list.
531          */
532         porthash = &pcbinfo->porthashbase[
533                         INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
534         LIST_FOREACH(phd, porthash, phd_hash) {
535                 if (phd->phd_port == lport)
536                         break;
537         }
538         if (phd != NULL) {
539                 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
540 #ifdef INET6
541                         if ((inp->inp_vflag & INP_IPV4) == 0)
542                                 continue;
543 #endif
544                         if (inp->inp_laddr.s_addr != INADDR_ANY &&
545                             inp->inp_laddr.s_addr != laddr.s_addr)
546                                 continue;
547
548                         if (inp->inp_faddr.s_addr != INADDR_ANY &&
549                             inp->inp_faddr.s_addr != faddr.s_addr)
550                                 continue;
551
552                         if (inp->inp_fport != 0 && inp->inp_fport != fport)
553                                 continue;
554
555                         if (cred == NULL ||
556                             cred->cr_prison ==
557                             inp->inp_socket->so_cred->cr_prison) {
558                                 match = inp;
559                                 break;
560                         }
561                 }
562         }
563         if (pcbinfo->porttoken)
564                 lwkt_reltoken(pcbinfo->porttoken);
565         return (match);
566 }
567
568 int
569 in_pcbconn_bind(struct inpcb *inp, const struct sockaddr *nam,
570     struct thread *td)
571 {
572         struct proc *p = td->td_proc;
573         unsigned short *lastport;
574         const struct sockaddr_in *sin = (const struct sockaddr_in *)nam;
575         struct sockaddr_in jsin;
576         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
577         struct ucred *cred = NULL;
578         u_short lport = 0;
579         ushort first, last;
580         int count, error, dup = 0;
581
582         if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
583                 return (EADDRNOTAVAIL);
584
585         KKASSERT(inp->inp_laddr.s_addr != INADDR_ANY);
586         if (inp->inp_lport != 0)
587                 return (EINVAL);        /* already bound */
588
589         KKASSERT(p);
590         cred = p->p_ucred;
591
592         /*
593          * This has to be atomic.  If the porthash is shared across multiple
594          * protocol threads (aka tcp) then the token will be non-NULL.
595          */
596         if (pcbinfo->porttoken)
597                 lwkt_gettoken(pcbinfo->porttoken);
598
599         jsin.sin_family = AF_INET;
600         jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
601         if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
602                 inp->inp_laddr.s_addr = INADDR_ANY;
603                 error = EINVAL;
604                 goto done;
605         }
606         inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
607
608         inp->inp_flags |= INP_ANONPORT;
609
610         if (inp->inp_flags & INP_HIGHPORT) {
611                 first = ipport_hifirstauto;     /* sysctl */
612                 last  = ipport_hilastauto;
613                 lastport = &pcbinfo->lasthi;
614         } else if (inp->inp_flags & INP_LOWPORT) {
615                 if (cred &&
616                     (error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
617                         inp->inp_laddr.s_addr = INADDR_ANY;
618                         goto done;
619                 }
620                 first = ipport_lowfirstauto;    /* 1023 */
621                 last  = ipport_lowlastauto;     /* 600 */
622                 lastport = &pcbinfo->lastlow;
623         } else {
624                 first = ipport_firstauto;       /* sysctl */
625                 last  = ipport_lastauto;
626                 lastport = &pcbinfo->lastport;
627         }
628
629 again:
630         /*
631          * Simple check to ensure all ports are not used up causing
632          * a deadlock here.
633          *
634          * We split the two cases (up and down) so that the direction
635          * is not being tested on each round of the loop.
636          */
637         if (first > last) {
638                 /*
639                  * counting down
640                  */
641                 count = first - last;
642
643                 do {
644                         if (count-- < 0) {      /* completely used? */
645                                 inp->inp_laddr.s_addr = INADDR_ANY;
646                                 error = EADDRNOTAVAIL;
647                                 goto done;
648                         }
649                         --*lastport;
650                         if (*lastport > first || *lastport < last)
651                                 *lastport = first;
652                         lport = htons(*lastport);
653                 } while (in_pcblookup_addrport(pcbinfo, inp->inp_laddr, lport,
654                                 sin->sin_addr, sin->sin_port, cred));
655         } else {
656                 /*
657                  * counting up
658                  */
659                 count = last - first;
660
661                 do {
662                         if (count-- < 0) {      /* completely used? */
663                                 inp->inp_laddr.s_addr = INADDR_ANY;
664                                 error = EADDRNOTAVAIL;
665                                 goto done;
666                         }
667                         ++*lastport;
668                         if (*lastport < first || *lastport > last)
669                                 *lastport = first;
670                         lport = htons(*lastport);
671                 } while (in_pcblookup_addrport(pcbinfo, inp->inp_laddr, lport,
672                                 sin->sin_addr, sin->sin_port, cred));
673         }
674
675         /* This could happen on loopback interface */
676         if (sin->sin_port == lport &&
677             sin->sin_addr.s_addr == inp->inp_laddr.s_addr) {
678                 if (dup) {
679                         /*
680                          * Duplicate again; give up
681                          */
682                         inp->inp_laddr.s_addr = INADDR_ANY;
683                         error = EADDRNOTAVAIL;
684                         goto done;
685                 }
686                 dup = 1;
687                 goto again;
688         }
689         inp->inp_lport = lport;
690
691         jsin.sin_family = AF_INET;
692         jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
693         if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin)) {
694                 inp->inp_laddr.s_addr = INADDR_ANY;
695                 inp->inp_lport = 0;
696                 error = EINVAL;
697                 goto done;
698         }
699         inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
700
701         if (in_pcbinsporthash(inp) != 0) {
702                 inp->inp_laddr.s_addr = INADDR_ANY;
703                 inp->inp_lport = 0;
704                 error = EAGAIN;
705                 goto done;
706         }
707         error = 0;
708 done:
709         if (pcbinfo->porttoken)
710                 lwkt_reltoken(pcbinfo->porttoken);
711         return error;
712 }
713
714 /*
715  *   Transform old in_pcbconnect() into an inner subroutine for new
716  *   in_pcbconnect(): Do some validity-checking on the remote
717  *   address (in mbuf 'nam') and then determine local host address
718  *   (i.e., which interface) to use to access that remote host.
719  *
720  *   This preserves definition of in_pcbconnect(), while supporting a
721  *   slightly different version for T/TCP.  (This is more than
722  *   a bit of a kludge, but cleaning up the internal interfaces would
723  *   have forced minor changes in every protocol).
724  */
725 int
726 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
727         struct sockaddr_in **plocal_sin, struct thread *td)
728 {
729         struct in_ifaddr *ia;
730         struct ucred *cred = NULL;
731         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
732         struct sockaddr *jsin;
733         int jailed = 0, alloc_route = 0;
734
735         if (nam->sa_len != sizeof *sin)
736                 return (EINVAL);
737         if (sin->sin_family != AF_INET)
738                 return (EAFNOSUPPORT);
739         if (sin->sin_port == 0)
740                 return (EADDRNOTAVAIL);
741         if (td && td->td_proc && td->td_proc->p_ucred)
742                 cred = td->td_proc->p_ucred;
743         if (cred && cred->cr_prison)
744                 jailed = 1;
745         if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
746                 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
747                 /*
748                  * If the destination address is INADDR_ANY,
749                  * use the primary local address.
750                  * If the supplied address is INADDR_BROADCAST,
751                  * and the primary interface supports broadcast,
752                  * choose the broadcast address for that interface.
753                  */
754                 if (sin->sin_addr.s_addr == INADDR_ANY)
755                         sin->sin_addr = IA_SIN(ia)->sin_addr;
756                 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
757                     (ia->ia_ifp->if_flags & IFF_BROADCAST))
758                         sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
759         }
760         if (inp->inp_laddr.s_addr == INADDR_ANY) {
761                 struct route *ro;
762
763                 ia = NULL;
764                 /*
765                  * If route is known or can be allocated now,
766                  * our src addr is taken from the i/f, else punt.
767                  * Note that we should check the address family of the cached
768                  * destination, in case of sharing the cache with IPv6.
769                  */
770                 ro = &inp->inp_route;
771                 if (ro->ro_rt &&
772                     (!(ro->ro_rt->rt_flags & RTF_UP) ||
773                      ro->ro_dst.sa_family != AF_INET ||
774                      satosin(&ro->ro_dst)->sin_addr.s_addr !=
775                                       sin->sin_addr.s_addr ||
776                      inp->inp_socket->so_options & SO_DONTROUTE)) {
777                         RTFREE(ro->ro_rt);
778                         ro->ro_rt = NULL;
779                 }
780                 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
781                     (ro->ro_rt == NULL ||
782                     ro->ro_rt->rt_ifp == NULL)) {
783                         /* No route yet, so try to acquire one */
784                         bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
785                         ro->ro_dst.sa_family = AF_INET;
786                         ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
787                         ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
788                                 sin->sin_addr;
789                         rtalloc(ro);
790                         alloc_route = 1;
791                 }
792                 /*
793                  * If we found a route, use the address
794                  * corresponding to the outgoing interface
795                  * unless it is the loopback (in case a route
796                  * to our address on another net goes to loopback).
797                  */
798                 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
799                         if (jailed) {
800                                 if (jailed_ip(cred->cr_prison, 
801                                     ro->ro_rt->rt_ifa->ifa_addr)) {
802                                         ia = ifatoia(ro->ro_rt->rt_ifa);
803                                 }
804                         } else {
805                                 ia = ifatoia(ro->ro_rt->rt_ifa);
806                         }
807                 }
808                 if (ia == NULL) {
809                         u_short fport = sin->sin_port;
810
811                         sin->sin_port = 0;
812                         ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
813                         if (ia && jailed && !jailed_ip(cred->cr_prison,
814                             sintosa(&ia->ia_addr)))
815                                 ia = NULL;
816                         if (ia == NULL)
817                                 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
818                         if (ia && jailed && !jailed_ip(cred->cr_prison,
819                             sintosa(&ia->ia_addr)))
820                                 ia = NULL;
821                         sin->sin_port = fport;
822                         if (ia == NULL &&
823                             !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
824                                 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
825                         if (ia && jailed && !jailed_ip(cred->cr_prison,
826                             sintosa(&ia->ia_addr)))
827                                 ia = NULL;
828
829                         if (!jailed && ia == NULL)
830                                 goto fail;
831                 }
832                 /*
833                  * If the destination address is multicast and an outgoing
834                  * interface has been set as a multicast option, use the
835                  * address of that interface as our source address.
836                  */
837                 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
838                     inp->inp_moptions != NULL) {
839                         struct ip_moptions *imo;
840                         struct ifnet *ifp;
841
842                         imo = inp->inp_moptions;
843                         if (imo->imo_multicast_ifp != NULL) {
844                                 struct in_ifaddr_container *iac;
845
846                                 ifp = imo->imo_multicast_ifp;
847                                 ia = NULL;
848                                 TAILQ_FOREACH(iac,
849                                 &in_ifaddrheads[mycpuid], ia_link) {
850                                         if (iac->ia->ia_ifp == ifp) {
851                                                 ia = iac->ia;
852                                                 break;
853                                         }
854                                 }
855                                 if (ia == NULL)
856                                         goto fail;
857                         }
858                 }
859                 /*
860                  * Don't do pcblookup call here; return interface in plocal_sin
861                  * and exit to caller, that will do the lookup.
862                  */
863                 if (ia == NULL && jailed) {
864                         if ((jsin = prison_get_nonlocal(cred->cr_prison, AF_INET, NULL)) != NULL ||
865                             (jsin = prison_get_local(cred->cr_prison, AF_INET, NULL)) != NULL) {
866                                 *plocal_sin = satosin(jsin);
867                         } else {
868                                 /* IPv6 only Jail */
869                                 goto fail;
870                         }
871                 } else {
872                         *plocal_sin = &ia->ia_addr;
873                 }
874         }
875         return (0);
876 fail:
877         if (alloc_route) {
878                 struct route *ro = &inp->inp_route;
879
880                 if (ro->ro_rt != NULL)
881                         RTFREE(ro->ro_rt);
882                 bzero(ro, sizeof(*ro));
883         }
884         return (EADDRNOTAVAIL);
885 }
886
887 /*
888  * Outer subroutine:
889  * Connect from a socket to a specified address.
890  * Both address and port must be specified in argument sin.
891  * If don't have a local address for this socket yet,
892  * then pick one.
893  */
894 int
895 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
896 {
897         struct sockaddr_in *if_sin;
898         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
899         int error;
900
901         /* Call inner routine to assign local interface address. */
902         if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
903                 return (error);
904
905         if (in_pcblookup_hash(inp->inp_cpcbinfo, sin->sin_addr, sin->sin_port,
906                               inp->inp_laddr.s_addr ?
907                                 inp->inp_laddr : if_sin->sin_addr,
908                               inp->inp_lport, FALSE, NULL) != NULL) {
909                 return (EADDRINUSE);
910         }
911         if (inp->inp_laddr.s_addr == INADDR_ANY) {
912                 if (inp->inp_lport == 0) {
913                         error = in_pcbbind(inp, NULL, td);
914                         if (error)
915                                 return (error);
916                 }
917                 inp->inp_laddr = if_sin->sin_addr;
918         }
919         inp->inp_faddr = sin->sin_addr;
920         inp->inp_fport = sin->sin_port;
921         in_pcbinsconnhash(inp);
922         return (0);
923 }
924
925 void
926 in_pcbdisconnect(struct inpcb *inp)
927 {
928
929         inp->inp_faddr.s_addr = INADDR_ANY;
930         inp->inp_fport = 0;
931         in_pcbremconnhash(inp);
932         if (inp->inp_socket->so_state & SS_NOFDREF)
933                 in_pcbdetach(inp);
934 }
935
936 void
937 in_pcbdetach(struct inpcb *inp)
938 {
939         struct socket *so = inp->inp_socket;
940         struct inpcbinfo *ipi = inp->inp_pcbinfo;
941
942 #ifdef IPSEC
943         ipsec4_delete_pcbpolicy(inp);
944 #endif /*IPSEC*/
945         inp->inp_gencnt = ++ipi->ipi_gencnt;
946         KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
947         in_pcbremlists(inp);
948         so->so_pcb = NULL;
949         sofree(so);                     /* remove pcb ref */
950         if (inp->inp_options)
951                 m_free(inp->inp_options);
952         if (inp->inp_route.ro_rt)
953                 rtfree(inp->inp_route.ro_rt);
954         ip_freemoptions(inp->inp_moptions);
955         inp->inp_vflag = 0;
956         kfree(inp, M_PCB);
957 }
958
959 /*
960  * The calling convention of in_setsockaddr() and in_setpeeraddr() was
961  * modified to match the pru_sockaddr() and pru_peeraddr() entry points
962  * in struct pr_usrreqs, so that protocols can just reference then directly
963  * without the need for a wrapper function.  The socket must have a valid
964  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
965  * except through a kernel programming error, so it is acceptable to panic
966  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
967  * because there actually /is/ a programming error somewhere... XXX)
968  */
969 int
970 in_setsockaddr(struct socket *so, struct sockaddr **nam)
971 {
972         struct inpcb *inp;
973         struct sockaddr_in *sin;
974
975         /*
976          * Do the malloc first in case it blocks.
977          */
978         sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
979         sin->sin_family = AF_INET;
980         sin->sin_len = sizeof *sin;
981
982         crit_enter();
983         inp = so->so_pcb;
984         if (!inp) {
985                 crit_exit();
986                 kfree(sin, M_SONAME);
987                 return (ECONNRESET);
988         }
989         sin->sin_port = inp->inp_lport;
990         sin->sin_addr = inp->inp_laddr;
991         crit_exit();
992
993         *nam = (struct sockaddr *)sin;
994         return (0);
995 }
996
997 void
998 in_setsockaddr_dispatch(netmsg_t msg)
999 {
1000         int error;
1001
1002         error = in_setsockaddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1003         lwkt_replymsg(&msg->lmsg, error);
1004 }
1005
1006 int
1007 in_setpeeraddr(struct socket *so, struct sockaddr **nam)
1008 {
1009         struct inpcb *inp;
1010         struct sockaddr_in *sin;
1011
1012         /*
1013          * Do the malloc first in case it blocks.
1014          */
1015         sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1016         sin->sin_family = AF_INET;
1017         sin->sin_len = sizeof *sin;
1018
1019         crit_enter();
1020         inp = so->so_pcb;
1021         if (!inp) {
1022                 crit_exit();
1023                 kfree(sin, M_SONAME);
1024                 return (ECONNRESET);
1025         }
1026         sin->sin_port = inp->inp_fport;
1027         sin->sin_addr = inp->inp_faddr;
1028         crit_exit();
1029
1030         *nam = (struct sockaddr *)sin;
1031         return (0);
1032 }
1033
1034 void
1035 in_setpeeraddr_dispatch(netmsg_t msg)
1036 {
1037         int error;
1038
1039         error = in_setpeeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1040         lwkt_replymsg(&msg->lmsg, error);
1041 }
1042
1043 void
1044 in_pcbnotifyall(struct inpcbhead *head, struct in_addr faddr, int err,
1045                 void (*notify)(struct inpcb *, int))
1046 {
1047         struct inpcb *inp, *ninp;
1048
1049         /*
1050          * note: if INP_PLACEMARKER is set we must ignore the rest of
1051          * the structure and skip it.
1052          */
1053         crit_enter();
1054         LIST_FOREACH_MUTABLE(inp, head, inp_list, ninp) {
1055                 if (inp->inp_flags & INP_PLACEMARKER)
1056                         continue;
1057 #ifdef INET6
1058                 if (!(inp->inp_vflag & INP_IPV4))
1059                         continue;
1060 #endif
1061                 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1062                     inp->inp_socket == NULL)
1063                         continue;
1064                 (*notify)(inp, err);            /* can remove inp from list! */
1065         }
1066         crit_exit();
1067 }
1068
1069 void
1070 in_pcbpurgeif0(struct inpcb *head, struct ifnet *ifp)
1071 {
1072         struct inpcb *inp;
1073         struct ip_moptions *imo;
1074         int i, gap;
1075
1076         for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
1077                 if (inp->inp_flags & INP_PLACEMARKER)
1078                         continue;
1079                 imo = inp->inp_moptions;
1080                 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
1081                         /*
1082                          * Unselect the outgoing interface if it is being
1083                          * detached.
1084                          */
1085                         if (imo->imo_multicast_ifp == ifp)
1086                                 imo->imo_multicast_ifp = NULL;
1087
1088                         /*
1089                          * Drop multicast group membership if we joined
1090                          * through the interface being detached.
1091                          */
1092                         for (i = 0, gap = 0; i < imo->imo_num_memberships;
1093                             i++) {
1094                                 if (imo->imo_membership[i]->inm_ifp == ifp) {
1095                                         in_delmulti(imo->imo_membership[i]);
1096                                         gap++;
1097                                 } else if (gap != 0)
1098                                         imo->imo_membership[i - gap] =
1099                                             imo->imo_membership[i];
1100                         }
1101                         imo->imo_num_memberships -= gap;
1102                 }
1103         }
1104 }
1105
1106 /*
1107  * Check for alternatives when higher level complains
1108  * about service problems.  For now, invalidate cached
1109  * routing information.  If the route was created dynamically
1110  * (by a redirect), time to try a default gateway again.
1111  */
1112 void
1113 in_losing(struct inpcb *inp)
1114 {
1115         struct rtentry *rt;
1116         struct rt_addrinfo rtinfo;
1117
1118         if ((rt = inp->inp_route.ro_rt)) {
1119                 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1120                 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
1121                 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1122                 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
1123                 rtinfo.rti_flags = rt->rt_flags;
1124                 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
1125                 if (rt->rt_flags & RTF_DYNAMIC)
1126                         rtrequest1_global(RTM_DELETE, &rtinfo, NULL, NULL);
1127                 inp->inp_route.ro_rt = NULL;
1128                 rtfree(rt);
1129                 /*
1130                  * A new route can be allocated
1131                  * the next time output is attempted.
1132                  */
1133         }
1134 }
1135
1136 /*
1137  * After a routing change, flush old routing
1138  * and allocate a (hopefully) better one.
1139  */
1140 void
1141 in_rtchange(struct inpcb *inp, int err)
1142 {
1143         if (inp->inp_route.ro_rt) {
1144                 rtfree(inp->inp_route.ro_rt);
1145                 inp->inp_route.ro_rt = NULL;
1146                 /*
1147                  * A new route can be allocated the next time
1148                  * output is attempted.
1149                  */
1150         }
1151 }
1152
1153 /*
1154  * Lookup a PCB based on the local address and port.
1155  */
1156 struct inpcb *
1157 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1158                    u_int lport_arg, int wild_okay, struct ucred *cred)
1159 {
1160         struct inpcb *inp;
1161         int matchwild = 3, wildcard;
1162         u_short lport = lport_arg;
1163         struct inpcbporthead *porthash;
1164         struct inpcbport *phd;
1165         struct inpcb *match = NULL;
1166
1167         /*
1168          * If the porthashbase is shared across several cpus we need
1169          * to lock.
1170          */
1171         if (pcbinfo->porttoken)
1172                 lwkt_gettoken(pcbinfo->porttoken);
1173
1174         /*
1175          * Best fit PCB lookup.
1176          *
1177          * First see if this local port is in use by looking on the
1178          * port hash list.
1179          */
1180         porthash = &pcbinfo->porthashbase[
1181                         INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
1182         LIST_FOREACH(phd, porthash, phd_hash) {
1183                 if (phd->phd_port == lport)
1184                         break;
1185         }
1186         if (phd != NULL) {
1187                 /*
1188                  * Port is in use by one or more PCBs. Look for best
1189                  * fit.
1190                  */
1191                 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1192                         wildcard = 0;
1193 #ifdef INET6
1194                         if ((inp->inp_vflag & INP_IPV4) == 0)
1195                                 continue;
1196 #endif
1197                         if (inp->inp_faddr.s_addr != INADDR_ANY)
1198                                 wildcard++;
1199                         if (inp->inp_laddr.s_addr != INADDR_ANY) {
1200                                 if (laddr.s_addr == INADDR_ANY)
1201                                         wildcard++;
1202                                 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1203                                         continue;
1204                         } else {
1205                                 if (laddr.s_addr != INADDR_ANY)
1206                                         wildcard++;
1207                         }
1208                         if (wildcard && !wild_okay)
1209                                 continue;
1210                         if (wildcard < matchwild &&
1211                             (cred == NULL ||
1212                              cred->cr_prison == 
1213                                         inp->inp_socket->so_cred->cr_prison)) {
1214                                 match = inp;
1215                                 matchwild = wildcard;
1216                                 if (matchwild == 0) {
1217                                         break;
1218                                 }
1219                         }
1220                 }
1221         }
1222         if (pcbinfo->porttoken)
1223                 lwkt_reltoken(pcbinfo->porttoken);
1224         return (match);
1225 }
1226
1227 /*
1228  * Lookup PCB in hash list.
1229  */
1230 struct inpcb *
1231 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1232                   u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1233                   boolean_t wildcard, struct ifnet *ifp)
1234 {
1235         struct inpcbhead *head;
1236         struct inpcb *inp, *jinp=NULL;
1237         u_short fport = fport_arg, lport = lport_arg;
1238
1239         /*
1240          * First look for an exact match.
1241          */
1242         head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
1243             laddr.s_addr, lport, pcbinfo->hashmask)];
1244         LIST_FOREACH(inp, head, inp_hash) {
1245 #ifdef INET6
1246                 if (!(inp->inp_vflag & INP_IPV4))
1247                         continue;
1248 #endif
1249                 if (in_hosteq(inp->inp_faddr, faddr) &&
1250                     in_hosteq(inp->inp_laddr, laddr) &&
1251                     inp->inp_fport == fport && inp->inp_lport == lport) {
1252                         /* found */
1253                         if (inp->inp_socket == NULL ||
1254                             inp->inp_socket->so_cred->cr_prison == NULL) {
1255                                 return (inp);
1256                         } else {
1257                                 if  (jinp == NULL)
1258                                         jinp = inp;
1259                         }
1260                 }
1261         }
1262         if (jinp != NULL)
1263                 return (jinp);
1264         if (wildcard) {
1265                 struct inpcb *local_wild = NULL;
1266                 struct inpcb *jinp_wild = NULL;
1267 #ifdef INET6
1268                 struct inpcb *local_wild_mapped = NULL;
1269 #endif
1270                 struct inpcontainer *ic;
1271                 struct inpcontainerhead *chead;
1272                 struct sockaddr_in jsin;
1273                 struct ucred *cred;
1274
1275                 /*
1276                  * Order of socket selection:
1277                  * 1. non-jailed, non-wild.
1278                  * 2. non-jailed, wild.
1279                  * 3. jailed, non-wild.
1280                  * 4. jailed, wild.
1281                  */
1282                 jsin.sin_family = AF_INET;
1283                 chead = &pcbinfo->wildcardhashbase[
1284                     INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
1285                 LIST_FOREACH(ic, chead, ic_list) {
1286                         inp = ic->ic_inp;
1287                         jsin.sin_addr.s_addr = laddr.s_addr;
1288 #ifdef INET6
1289                         if (!(inp->inp_vflag & INP_IPV4))
1290                                 continue;
1291 #endif
1292                         if (inp->inp_socket != NULL)
1293                                 cred = inp->inp_socket->so_cred;
1294                         else
1295                                 cred = NULL;
1296                         if (cred != NULL && jailed(cred)) {
1297                                 if (jinp != NULL)
1298                                         continue;
1299                                 else
1300                                         if (!jailed_ip(cred->cr_prison,
1301                                             (struct sockaddr *)&jsin))
1302                                                 continue;
1303                         }
1304                         if (inp->inp_lport == lport) {
1305                                 if (ifp && ifp->if_type == IFT_FAITH &&
1306                                     !(inp->inp_flags & INP_FAITH))
1307                                         continue;
1308                                 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1309                                         if (cred != NULL && jailed(cred))
1310                                                 jinp = inp;
1311                                         else
1312                                                 return (inp);
1313                                 }
1314                                 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1315 #ifdef INET6
1316                                         if (INP_CHECK_SOCKAF(inp->inp_socket,
1317                                                              AF_INET6))
1318                                                 local_wild_mapped = inp;
1319                                         else
1320 #endif
1321                                                 if (cred != NULL &&
1322                                                     jailed(cred))
1323                                                         jinp_wild = inp;
1324                                                 else
1325                                                         local_wild = inp;
1326                                 }
1327                         }
1328                 }
1329                 if (local_wild != NULL)
1330                         return (local_wild);
1331 #ifdef INET6
1332                 if (local_wild_mapped != NULL)
1333                         return (local_wild_mapped);
1334 #endif
1335                 if (jinp != NULL)
1336                         return (jinp);
1337                 return (jinp_wild);
1338         }
1339
1340         /*
1341          * Not found.
1342          */
1343         return (NULL);
1344 }
1345
1346 /*
1347  * Insert PCB into connection hash table.
1348  */
1349 void
1350 in_pcbinsconnhash(struct inpcb *inp)
1351 {
1352         struct inpcbinfo *pcbinfo = inp->inp_cpcbinfo;
1353         struct inpcbhead *bucket;
1354         u_int32_t hashkey_faddr, hashkey_laddr;
1355
1356 #ifdef INET6
1357         if (inp->inp_vflag & INP_IPV6) {
1358                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1359                 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1360         } else {
1361 #endif
1362                 hashkey_faddr = inp->inp_faddr.s_addr;
1363                 hashkey_laddr = inp->inp_laddr.s_addr;
1364 #ifdef INET6
1365         }
1366 #endif
1367
1368         KASSERT(!(inp->inp_flags & INP_WILDCARD),
1369                 ("already on wildcardhash"));
1370         KASSERT(!(inp->inp_flags & INP_CONNECTED),
1371                 ("already on connhash"));
1372         inp->inp_flags |= INP_CONNECTED;
1373
1374         /*
1375          * Insert into the connection hash table.
1376          */
1377         bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1378             inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1379         LIST_INSERT_HEAD(bucket, inp, inp_hash);
1380 }
1381
1382 /*
1383  * Remove PCB from connection hash table.
1384  */
1385 void
1386 in_pcbremconnhash(struct inpcb *inp)
1387 {
1388         KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1389         LIST_REMOVE(inp, inp_hash);
1390         inp->inp_flags &= ~INP_CONNECTED;
1391 }
1392
1393 /*
1394  * Insert PCB into port hash table.
1395  */
1396 int
1397 in_pcbinsporthash(struct inpcb *inp)
1398 {
1399         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1400         struct inpcbporthead *pcbporthash;
1401         struct inpcbport *phd;
1402
1403         /*
1404          * If the porthashbase is shared across several cpus we need
1405          * to lock.
1406          */
1407         if (pcbinfo->porttoken)
1408                 lwkt_gettoken(pcbinfo->porttoken);
1409
1410         /*
1411          * Insert into the port hash table.
1412          */
1413         pcbporthash = &pcbinfo->porthashbase[
1414             INP_PCBPORTHASH(inp->inp_lport, pcbinfo->porthashmask)];
1415
1416         /* Go through port list and look for a head for this lport. */
1417         LIST_FOREACH(phd, pcbporthash, phd_hash) {
1418                 if (phd->phd_port == inp->inp_lport)
1419                         break;
1420         }
1421
1422         /* If none exists, malloc one and tack it on. */
1423         if (phd == NULL) {
1424                 KKASSERT(pcbinfo->portsave != NULL);
1425                 phd = pcbinfo->portsave;
1426                 pcbinfo->portsave = NULL;
1427                 phd->phd_port = inp->inp_lport;
1428                 LIST_INIT(&phd->phd_pcblist);
1429                 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1430         }
1431
1432         inp->inp_phd = phd;
1433         LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1434
1435         if (pcbinfo->porttoken)
1436                 lwkt_reltoken(pcbinfo->porttoken);
1437         if (pcbinfo->portsave == NULL) {
1438                 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave),
1439                                             M_PCB, M_INTWAIT | M_ZERO);
1440         }
1441         return (0);
1442 }
1443
1444 void
1445 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1446 {
1447         struct inpcontainer *ic;
1448         struct inpcontainerhead *bucket;
1449
1450         bucket = &pcbinfo->wildcardhashbase[
1451             INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1452
1453         ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1454         ic->ic_inp = inp;
1455         LIST_INSERT_HEAD(bucket, ic, ic_list);
1456 }
1457
1458 /*
1459  * Insert PCB into wildcard hash table.
1460  */
1461 void
1462 in_pcbinswildcardhash(struct inpcb *inp)
1463 {
1464         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1465
1466         KASSERT(!(inp->inp_flags & INP_CONNECTED),
1467                 ("already on connhash"));
1468         KASSERT(!(inp->inp_flags & INP_WILDCARD),
1469                 ("already on wildcardhash"));
1470         inp->inp_flags |= INP_WILDCARD;
1471
1472         in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1473 }
1474
1475 void
1476 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1477 {
1478         struct inpcontainer *ic;
1479         struct inpcontainerhead *head;
1480
1481         /* find bucket */
1482         head = &pcbinfo->wildcardhashbase[
1483             INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1484
1485         LIST_FOREACH(ic, head, ic_list) {
1486                 if (ic->ic_inp == inp)
1487                         goto found;
1488         }
1489         return;                 /* not found! */
1490
1491 found:
1492         LIST_REMOVE(ic, ic_list);       /* remove container from bucket chain */
1493         kfree(ic, M_TEMP);              /* deallocate container */
1494 }
1495
1496 /*
1497  * Remove PCB from wildcard hash table.
1498  */
1499 void
1500 in_pcbremwildcardhash(struct inpcb *inp)
1501 {
1502         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1503
1504         KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
1505         in_pcbremwildcardhash_oncpu(inp, pcbinfo);
1506         inp->inp_flags &= ~INP_WILDCARD;
1507 }
1508
1509 /*
1510  * Remove PCB from various lists.
1511  */
1512 void
1513 in_pcbremlists(struct inpcb *inp)
1514 {
1515         struct inpcbinfo *pcbinfo;
1516
1517         if (inp->inp_lport) {
1518                 struct inpcbport *phd;
1519
1520                 pcbinfo = inp->inp_pcbinfo;
1521                 if (pcbinfo->porttoken)
1522                         lwkt_gettoken(pcbinfo->porttoken);
1523
1524                 phd = inp->inp_phd;
1525                 LIST_REMOVE(inp, inp_portlist);
1526                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1527                         LIST_REMOVE(phd, phd_hash);
1528                         kfree(phd, M_PCB);
1529                 }
1530                 if (pcbinfo->porttoken)
1531                         lwkt_reltoken(pcbinfo->porttoken);
1532         }
1533         if (inp->inp_flags & INP_WILDCARD) {
1534                 in_pcbremwildcardhash(inp);
1535         } else if (inp->inp_flags & INP_CONNECTED) {
1536                 in_pcbremconnhash(inp);
1537         }
1538         LIST_REMOVE(inp, inp_list);
1539         inp->inp_pcbinfo->ipi_count--;
1540 }
1541
1542 int
1543 prison_xinpcb(struct thread *td, struct inpcb *inp)
1544 {
1545         struct ucred *cr;
1546
1547         if (td->td_proc == NULL)
1548                 return (0);
1549         cr = td->td_proc->p_ucred;
1550         if (cr->cr_prison == NULL)
1551                 return (0);
1552         if (inp->inp_socket && inp->inp_socket->so_cred &&
1553             inp->inp_socket->so_cred->cr_prison &&
1554             cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
1555                 return (0);
1556         return (1);
1557 }
1558
1559 int
1560 in_pcblist_global(SYSCTL_HANDLER_ARGS)
1561 {
1562         struct inpcbinfo *pcbinfo = arg1;
1563         struct inpcb *inp, *marker;
1564         struct xinpcb xi;
1565         int error, i, n;
1566
1567         /*
1568          * The process of preparing the TCB list is too time-consuming and
1569          * resource-intensive to repeat twice on every request.
1570          */
1571         if (req->oldptr == NULL) {
1572                 n = pcbinfo->ipi_count;
1573                 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1574                 return 0;
1575         }
1576
1577         if (req->newptr != NULL)
1578                 return EPERM;
1579
1580         /*
1581          * OK, now we're committed to doing something.  Re-fetch ipi_count
1582          * after obtaining the generation count.
1583          */
1584         n = pcbinfo->ipi_count;
1585
1586         marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
1587         marker->inp_flags |= INP_PLACEMARKER;
1588         LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1589
1590         i = 0;
1591         error = 0;
1592
1593         while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
1594                 LIST_REMOVE(marker, inp_list);
1595                 LIST_INSERT_AFTER(inp, marker, inp_list);
1596
1597                 if (inp->inp_flags & INP_PLACEMARKER)
1598                         continue;
1599                 if (prison_xinpcb(req->td, inp))
1600                         continue;
1601                 bzero(&xi, sizeof xi);
1602                 xi.xi_len = sizeof xi;
1603                 bcopy(inp, &xi.xi_inp, sizeof *inp);
1604                 if (inp->inp_socket)
1605                         sotoxsocket(inp->inp_socket, &xi.xi_socket);
1606                 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
1607                         break;
1608                 ++i;
1609         }
1610         LIST_REMOVE(marker, inp_list);
1611         if (error == 0 && i < n) {
1612                 bzero(&xi, sizeof xi);
1613                 xi.xi_len = sizeof xi;
1614                 while (i < n) {
1615                         error = SYSCTL_OUT(req, &xi, sizeof xi);
1616                         ++i;
1617                 }
1618         }
1619         kfree(marker, M_TEMP);
1620         return(error);
1621 }
1622
1623 int
1624 in_pcblist_global_nomarker(SYSCTL_HANDLER_ARGS, struct xinpcb **xi0, int *nxi0)
1625 {
1626         struct inpcbinfo *pcbinfo = arg1;
1627         struct inpcb *inp;
1628         struct xinpcb *xi;
1629         int nxi;
1630
1631         *nxi0 = 0;
1632         *xi0 = NULL;
1633
1634         /*
1635          * The process of preparing the PCB list is too time-consuming and
1636          * resource-intensive to repeat twice on every request.
1637          */
1638         if (req->oldptr == NULL) {
1639                 int n = pcbinfo->ipi_count;
1640
1641                 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1642                 return 0;
1643         }
1644
1645         if (req->newptr != NULL)
1646                 return EPERM;
1647
1648         if (pcbinfo->ipi_count == 0)
1649                 return 0;
1650
1651         nxi = 0;
1652         xi = kmalloc(pcbinfo->ipi_count * sizeof(*xi), M_TEMP,
1653                      M_WAITOK | M_ZERO | M_NULLOK);
1654         if (xi == NULL)
1655                 return ENOMEM;
1656
1657         LIST_FOREACH(inp, &pcbinfo->pcblisthead, inp_list) {
1658                 struct xinpcb *xi_ptr = &xi[nxi];
1659
1660                 if (prison_xinpcb(req->td, inp))
1661                         continue;
1662
1663                 xi_ptr->xi_len = sizeof(*xi_ptr);
1664                 bcopy(inp, &xi_ptr->xi_inp, sizeof(*inp));
1665                 if (inp->inp_socket)
1666                         sotoxsocket(inp->inp_socket, &xi_ptr->xi_socket);
1667                 ++nxi;
1668         }
1669
1670         if (nxi == 0) {
1671                 kfree(xi, M_TEMP);
1672                 return 0;
1673         }
1674
1675         *nxi0 = nxi;
1676         *xi0 = xi;
1677
1678         return 0;
1679 }
1680
1681 void
1682 in_savefaddr(struct socket *so, const struct sockaddr *faddr)
1683 {
1684         struct sockaddr_in *sin;
1685
1686         KASSERT(faddr->sa_family == AF_INET,
1687             ("not AF_INET faddr %d", faddr->sa_family));
1688
1689         sin = kmalloc(sizeof(*sin), M_SONAME, M_WAITOK | M_ZERO);
1690         sin->sin_family = AF_INET;
1691         sin->sin_len = sizeof(*sin);
1692         sin->sin_port = ((const struct sockaddr_in *)faddr)->sin_port;
1693         sin->sin_addr = ((const struct sockaddr_in *)faddr)->sin_addr;
1694
1695         so->so_faddr = (struct sockaddr *)sin;
1696 }