Once we distribute socket protocol processing requests to different
[dragonfly.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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)in_pcb.c    8.4 (Berkeley) 5/24/95
34  * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.27 2004/01/02 04:06:42 ambrisko Exp $
35  * $DragonFly: src/sys/netinet/in_pcb.c,v 1.12 2004/03/05 16:57:15 hsu Exp $
36  */
37
38 #include "opt_ipsec.h"
39 #include "opt_inet6.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/domain.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/proc.h>
50 #include <sys/jail.h>
51 #include <sys/kernel.h>
52 #include <sys/sysctl.h>
53
54 #include <machine/limits.h>
55
56 #include <vm/vm_zone.h>
57
58 #include <net/if.h>
59 #include <net/if_types.h>
60 #include <net/route.h>
61
62 #include <netinet/in.h>
63 #include <netinet/in_pcb.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip_var.h>
66 #ifdef INET6
67 #include <netinet/ip6.h>
68 #include <netinet6/ip6_var.h>
69 #endif /* INET6 */
70
71 #ifdef IPSEC
72 #include <netinet6/ipsec.h>
73 #include <netproto/key/key.h>
74 #endif
75
76 #ifdef FAST_IPSEC
77 #if defined(IPSEC) || defined(IPSEC_ESP)
78 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
79 #endif
80
81 #include <netipsec/ipsec.h>
82 #include <netipsec/key.h>
83 #define IPSEC
84 #endif /* FAST_IPSEC */
85
86 struct in_addr zeroin_addr;
87
88 /*
89  * These configure the range of local port addresses assigned to
90  * "unspecified" outgoing connections/packets/whatever.
91  */
92 int ipport_lowfirstauto = IPPORT_RESERVED - 1;  /* 1023 */
93 int ipport_lowlastauto = IPPORT_RESERVEDSTART;  /* 600 */
94
95 int ipport_firstauto = IPPORT_RESERVED;         /* 1024 */
96 int ipport_lastauto = IPPORT_USERRESERVED;      /* 5000 */
97
98 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO;    /* 49152 */
99 int ipport_hilastauto = IPPORT_HILASTAUTO;      /* 65535 */
100
101 static __inline void
102 RANGECHK(int var, int min, int max)
103 {
104         if (var < min)
105                 var = min;
106         else if (var > max)
107                 var = max;
108 }
109
110 static int
111 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
112 {
113         int error;
114
115         error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
116         if (!error) {
117                 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
118                 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
119
120                 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
121                 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
122
123                 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
124                 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
125         }
126         return (error);
127 }
128
129 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
130
131 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
132            &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
133 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
134            &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
135 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
136            &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
137 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
138            &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
139 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
140            &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
141 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
142            &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
143
144 /*
145  * in_pcb.c: manage the Protocol Control Blocks.
146  *
147  * NOTE: It is assumed that most of these functions will be called at
148  * splnet(). XXX - There are, unfortunately, a few exceptions to this
149  * rule that should be fixed.
150  */
151
152 /*
153  * Allocate a PCB and associate it with the socket.
154  */
155 int
156 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
157 {
158         struct inpcb *inp;
159 #ifdef IPSEC
160         int error;
161 #endif
162
163         inp = zalloc(pcbinfo->ipi_zone);
164         if (inp == NULL)
165                 return (ENOBUFS);
166         bzero((caddr_t)inp, sizeof *inp);
167         inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
168         inp->inp_pcbinfo = pcbinfo;
169         inp->inp_socket = so;
170 #ifdef IPSEC
171         error = ipsec_init_policy(so, &inp->inp_sp);
172         if (error != 0) {
173                 zfree(pcbinfo->ipi_zone, inp);
174                 return (error);
175         }
176 #endif
177 #ifdef INET6
178         if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
179                 inp->inp_flags |= IN6P_IPV6_V6ONLY;
180         if (ip6_auto_flowlabel)
181                 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
182 #endif
183         so->so_pcb = (caddr_t)inp;
184         LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
185         pcbinfo->ipi_count++;
186         return (0);
187 }
188
189 int
190 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
191 {
192         struct socket *so = inp->inp_socket;
193         struct proc *p = td->td_proc;
194         unsigned short *lastport;
195         struct sockaddr_in *sin;
196         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
197         u_short lport = 0;
198         int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
199         int error, prison = 0;
200
201         KKASSERT(p);
202
203         if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
204                 return (EADDRNOTAVAIL);
205         if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
206                 return (EINVAL);        /* already bound */
207         if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
208                 wild = 1;    /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
209         if (nam != NULL) {
210                 sin = (struct sockaddr_in *)nam;
211                 if (nam->sa_len != sizeof *sin)
212                         return (EINVAL);
213 #ifdef notdef
214                 /*
215                  * We should check the family, but old programs
216                  * incorrectly fail to initialize it.
217                  */
218                 if (sin->sin_family != AF_INET)
219                         return (EAFNOSUPPORT);
220 #endif
221                 if (sin->sin_addr.s_addr != INADDR_ANY &&
222                     prison_ip(td, 0, &sin->sin_addr.s_addr))
223                                 return (EINVAL);
224                 lport = sin->sin_port;
225                 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
226                         /*
227                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
228                          * allow complete duplication of binding if
229                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
230                          * and a multicast address is bound on both
231                          * new and duplicated sockets.
232                          */
233                         if (so->so_options & SO_REUSEADDR)
234                                 reuseport = SO_REUSEADDR | SO_REUSEPORT;
235                 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
236                         sin->sin_port = 0;              /* yech... */
237                         bzero(&sin->sin_zero, sizeof sin->sin_zero);
238                         if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL)
239                                 return (EADDRNOTAVAIL);
240                 }
241                 if (lport != 0) {
242                         struct inpcb *t;
243
244                         /* GROSS */
245                         if (ntohs(lport) < IPPORT_RESERVED &&
246                             p && suser_cred(p->p_ucred, PRISON_ROOT))
247                                 return (EACCES);
248                         if (p && p->p_ucred->cr_prison)
249                                 prison = 1;
250                         if (so->so_cred->cr_uid != 0 &&
251                             !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
252                                 t = in_pcblookup_local(inp->inp_pcbinfo,
253                                     sin->sin_addr, lport,
254                                     prison ? 0 : INPLOOKUP_WILDCARD);
255                                 if (t &&
256                                     (!in_nullhost(sin->sin_addr) ||
257                                      !in_nullhost(t->inp_laddr) ||
258                                      (t->inp_socket->so_options &
259                                          SO_REUSEPORT) == 0) &&
260                                     (so->so_cred->cr_uid !=
261                                      t->inp_socket->so_cred->cr_uid)) {
262 #ifdef INET6
263                                         if (!in_nullhost(sin->sin_addr) ||
264                                             !in_nullhost(t->inp_laddr) ||
265                                             INP_SOCKAF(so) ==
266                                             INP_SOCKAF(t->inp_socket))
267 #endif
268                                         return (EADDRINUSE);
269                                 }
270                         }
271                         if (prison && prison_ip(td, 0, &sin->sin_addr.s_addr))
272                                 return (EADDRNOTAVAIL);
273                         t = in_pcblookup_local(pcbinfo, sin->sin_addr,
274                             lport, prison ? 0 : wild);
275                         if (t && !(reuseport & t->inp_socket->so_options)) {
276 #ifdef INET6
277                                 if (!in_nullhost(sin->sin_addr) ||
278                                     !in_nullhost(t->inp_laddr) ||
279                                     INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
280 #endif
281                                 return (EADDRINUSE);
282                         }
283                 }
284                 inp->inp_laddr = sin->sin_addr;
285         }
286         if (lport == 0) {
287                 ushort first, last;
288                 int count;
289
290                 if (inp->inp_laddr.s_addr != INADDR_ANY &&
291                     prison_ip(td, 0, &inp->inp_laddr.s_addr )) {
292                         inp->inp_laddr.s_addr = INADDR_ANY;
293                         return (EINVAL);
294                 }
295                 inp->inp_flags |= INP_ANONPORT;
296
297                 if (inp->inp_flags & INP_HIGHPORT) {
298                         first = ipport_hifirstauto;     /* sysctl */
299                         last  = ipport_hilastauto;
300                         lastport = &pcbinfo->lasthi;
301                 } else if (inp->inp_flags & INP_LOWPORT) {
302                         if (p &&
303                             (error = suser_cred(p->p_ucred, PRISON_ROOT))) {
304                                 inp->inp_laddr.s_addr = INADDR_ANY;
305                                 return (error);
306                         }
307                         first = ipport_lowfirstauto;    /* 1023 */
308                         last  = ipport_lowlastauto;     /* 600 */
309                         lastport = &pcbinfo->lastlow;
310                 } else {
311                         first = ipport_firstauto;       /* sysctl */
312                         last  = ipport_lastauto;
313                         lastport = &pcbinfo->lastport;
314                 }
315                 /*
316                  * Simple check to ensure all ports are not used up causing
317                  * a deadlock here.
318                  *
319                  * We split the two cases (up and down) so that the direction
320                  * is not being tested on each round of the loop.
321                  */
322                 if (first > last) {
323                         /*
324                          * counting down
325                          */
326                         count = first - last;
327
328                         do {
329                                 if (count-- < 0) {      /* completely used? */
330                                         inp->inp_laddr.s_addr = INADDR_ANY;
331                                         return (EADDRNOTAVAIL);
332                                 }
333                                 --*lastport;
334                                 if (*lastport > first || *lastport < last)
335                                         *lastport = first;
336                                 lport = htons(*lastport);
337                         } while (in_pcblookup_local(pcbinfo,
338                                  inp->inp_laddr, lport, wild));
339                 } else {
340                         /*
341                          * counting up
342                          */
343                         count = last - first;
344
345                         do {
346                                 if (count-- < 0) {      /* completely used? */
347                                         inp->inp_laddr.s_addr = INADDR_ANY;
348                                         return (EADDRNOTAVAIL);
349                                 }
350                                 ++*lastport;
351                                 if (*lastport < first || *lastport > last)
352                                         *lastport = first;
353                                 lport = htons(*lastport);
354                         } while (in_pcblookup_local(pcbinfo,
355                                  inp->inp_laddr, lport, wild));
356                 }
357         }
358         inp->inp_lport = lport;
359         if (prison_ip(td, 0, &inp->inp_laddr.s_addr)) {
360                 inp->inp_laddr.s_addr = INADDR_ANY;
361                 inp->inp_lport = 0;
362                 return (EINVAL);
363         }
364         if (in_pcbinsporthash(inp) != 0) {
365                 inp->inp_laddr.s_addr = INADDR_ANY;
366                 inp->inp_lport = 0;
367                 return (EAGAIN);
368         }
369         in_pcbinsbindhash(inp);
370         return (0);
371 }
372
373 /*
374  *   Transform old in_pcbconnect() into an inner subroutine for new
375  *   in_pcbconnect(): Do some validity-checking on the remote
376  *   address (in mbuf 'nam') and then determine local host address
377  *   (i.e., which interface) to use to access that remote host.
378  *
379  *   This preserves definition of in_pcbconnect(), while supporting a
380  *   slightly different version for T/TCP.  (This is more than
381  *   a bit of a kludge, but cleaning up the internal interfaces would
382  *   have forced minor changes in every protocol).
383  */
384
385 int
386 in_pcbladdr(inp, nam, plocal_sin)
387         struct inpcb *inp;
388         struct sockaddr *nam;
389         struct sockaddr_in **plocal_sin;
390 {
391         struct in_ifaddr *ia;
392         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
393
394         if (nam->sa_len != sizeof *sin)
395                 return (EINVAL);
396         if (sin->sin_family != AF_INET)
397                 return (EAFNOSUPPORT);
398         if (sin->sin_port == 0)
399                 return (EADDRNOTAVAIL);
400         if (!TAILQ_EMPTY(&in_ifaddrhead)) {
401                 ia = TAILQ_FIRST(&in_ifaddrhead);
402                 /*
403                  * If the destination address is INADDR_ANY,
404                  * use the primary local address.
405                  * If the supplied address is INADDR_BROADCAST,
406                  * and the primary interface supports broadcast,
407                  * choose the broadcast address for that interface.
408                  */
409                 if (sin->sin_addr.s_addr == INADDR_ANY)
410                         sin->sin_addr = IA_SIN(ia)->sin_addr;
411                 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
412                     (ia->ia_ifp->if_flags & IFF_BROADCAST))
413                         sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
414         }
415         if (inp->inp_laddr.s_addr == INADDR_ANY) {
416                 struct route *ro;
417
418                 ia = (struct in_ifaddr *)NULL;
419                 /*
420                  * If route is known or can be allocated now,
421                  * our src addr is taken from the i/f, else punt.
422                  * Note that we should check the address family of the cached
423                  * destination, in case of sharing the cache with IPv6.
424                  */
425                 ro = &inp->inp_route;
426                 if (ro->ro_rt &&
427                     (!(ro->ro_rt->rt_flags & RTF_UP) ||
428                      ro->ro_dst.sa_family != AF_INET ||
429                      satosin(&ro->ro_dst)->sin_addr.s_addr !=
430                          sin->sin_addr.s_addr ||
431                      inp->inp_socket->so_options & SO_DONTROUTE)) {
432                         RTFREE(ro->ro_rt);
433                         ro->ro_rt = (struct rtentry *)NULL;
434                 }
435                 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
436                     (ro->ro_rt == (struct rtentry *)NULL ||
437                     ro->ro_rt->rt_ifp == (struct ifnet *)NULL)) {
438                         /* No route yet, so try to acquire one */
439                         bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
440                         ro->ro_dst.sa_family = AF_INET;
441                         ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
442                         ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
443                                 sin->sin_addr;
444                         rtalloc(ro);
445                 }
446                 /*
447                  * If we found a route, use the address
448                  * corresponding to the outgoing interface
449                  * unless it is the loopback (in case a route
450                  * to our address on another net goes to loopback).
451                  */
452                 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
453                         ia = ifatoia(ro->ro_rt->rt_ifa);
454                 if (ia == NULL) {
455                         u_short fport = sin->sin_port;
456
457                         sin->sin_port = 0;
458                         ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
459                         if (ia == NULL)
460                                 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
461                         sin->sin_port = fport;
462                         if (ia == NULL)
463                                 ia = TAILQ_FIRST(&in_ifaddrhead);
464                         if (ia == NULL)
465                                 return (EADDRNOTAVAIL);
466                 }
467                 /*
468                  * If the destination address is multicast and an outgoing
469                  * interface has been set as a multicast option, use the
470                  * address of that interface as our source address.
471                  */
472                 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
473                     inp->inp_moptions != NULL) {
474                         struct ip_moptions *imo;
475                         struct ifnet *ifp;
476
477                         imo = inp->inp_moptions;
478                         if (imo->imo_multicast_ifp != NULL) {
479                                 ifp = imo->imo_multicast_ifp;
480                                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
481                                         if (ia->ia_ifp == ifp)
482                                                 break;
483                                 if (ia == NULL)
484                                         return (EADDRNOTAVAIL);
485                         }
486                 }
487                 /*
488                  * Don't do pcblookup call here; return interface in plocal_sin
489                  * and exit to caller, that will do the lookup.
490                  */
491                 *plocal_sin = &ia->ia_addr;
492
493         }
494         return (0);
495 }
496
497 /*
498  * Outer subroutine:
499  * Connect from a socket to a specified address.
500  * Both address and port must be specified in argument sin.
501  * If don't have a local address for this socket yet,
502  * then pick one.
503  */
504 int
505 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
506 {
507         struct sockaddr_in *if_sin;
508         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
509         struct sockaddr_in sa;
510         struct ucred *cr = td->td_proc ? td->td_proc->p_ucred : NULL;
511         int error;
512
513         if (cr && cr->cr_prison != NULL && in_nullhost(inp->inp_laddr)) {
514                 bzero(&sa, sizeof sa);
515                 sa.sin_addr.s_addr = htonl(cr->cr_prison->pr_ip);
516                 sa.sin_len = sizeof sa;
517                 sa.sin_family = AF_INET;
518                 error = in_pcbbind(inp, (struct sockaddr *)&sa, td);
519                 if (error)
520                         return (error);
521         }
522
523         /* Call inner routine to assign local interface address. */
524         if ((error = in_pcbladdr(inp, nam, &if_sin)) != 0)
525                 return (error);
526
527         if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
528             inp->inp_laddr.s_addr ? inp->inp_laddr : if_sin->sin_addr,
529             inp->inp_lport, FALSE, NULL) != NULL) {
530                 return (EADDRINUSE);
531         }
532         if (inp->inp_laddr.s_addr == INADDR_ANY) {
533                 if (inp->inp_lport == 0) {
534                         error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
535                         if (error)
536                                 return (error);
537                 }
538                 inp->inp_laddr = if_sin->sin_addr;
539         }
540         inp->inp_faddr = sin->sin_addr;
541         inp->inp_fport = sin->sin_port;
542         in_pcbrehash(inp, INP_CONNECTED);
543         return (0);
544 }
545
546 void
547 in_pcbdisconnect(inp)
548         struct inpcb *inp;
549 {
550
551         inp->inp_faddr.s_addr = INADDR_ANY;
552         inp->inp_fport = 0;
553         in_pcbremconnhash(inp);
554         if (inp->inp_socket->so_state & SS_NOFDREF)
555                 in_pcbdetach(inp);
556 }
557
558 void
559 in_pcbdetach(inp)
560         struct inpcb *inp;
561 {
562         struct socket *so = inp->inp_socket;
563         struct inpcbinfo *ipi = inp->inp_pcbinfo;
564
565 #ifdef IPSEC
566         ipsec4_delete_pcbpolicy(inp);
567 #endif /*IPSEC*/
568         inp->inp_gencnt = ++ipi->ipi_gencnt;
569         in_pcbremlists(inp);
570         so->so_pcb = 0;
571         sofree(so);
572         if (inp->inp_options)
573                 (void)m_free(inp->inp_options);
574         if (inp->inp_route.ro_rt)
575                 rtfree(inp->inp_route.ro_rt);
576         ip_freemoptions(inp->inp_moptions);
577         inp->inp_vflag = 0;
578         zfree(ipi->ipi_zone, inp);
579 }
580
581 /*
582  * The calling convention of in_setsockaddr() and in_setpeeraddr() was
583  * modified to match the pru_sockaddr() and pru_peeraddr() entry points
584  * in struct pr_usrreqs, so that protocols can just reference then directly
585  * without the need for a wrapper function.  The socket must have a valid
586  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
587  * except through a kernel programming error, so it is acceptable to panic
588  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
589  * because there actually /is/ a programming error somewhere... XXX)
590  */
591 int
592 in_setsockaddr(so, nam)
593         struct socket *so;
594         struct sockaddr **nam;
595 {
596         int s;
597         struct inpcb *inp;
598         struct sockaddr_in *sin;
599
600         /*
601          * Do the malloc first in case it blocks.
602          */
603         MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
604                 M_WAITOK | M_ZERO);
605         sin->sin_family = AF_INET;
606         sin->sin_len = sizeof *sin;
607
608         s = splnet();
609         inp = sotoinpcb(so);
610         if (!inp) {
611                 splx(s);
612                 free(sin, M_SONAME);
613                 return (ECONNRESET);
614         }
615         sin->sin_port = inp->inp_lport;
616         sin->sin_addr = inp->inp_laddr;
617         splx(s);
618
619         *nam = (struct sockaddr *)sin;
620         return (0);
621 }
622
623 int
624 in_setpeeraddr(so, nam)
625         struct socket *so;
626         struct sockaddr **nam;
627 {
628         int s;
629         struct inpcb *inp;
630         struct sockaddr_in *sin;
631
632         /*
633          * Do the malloc first in case it blocks.
634          */
635         MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
636                 M_WAITOK | M_ZERO);
637         sin->sin_family = AF_INET;
638         sin->sin_len = sizeof *sin;
639
640         s = splnet();
641         inp = sotoinpcb(so);
642         if (!inp) {
643                 splx(s);
644                 free(sin, M_SONAME);
645                 return (ECONNRESET);
646         }
647         sin->sin_port = inp->inp_fport;
648         sin->sin_addr = inp->inp_faddr;
649         splx(s);
650
651         *nam = (struct sockaddr *)sin;
652         return (0);
653 }
654
655 void
656 in_pcbnotifyall(head, faddr, errno, notify)
657         struct inpcbhead *head;
658         struct in_addr faddr;
659         void (*notify) (struct inpcb *, int);
660 {
661         struct inpcb *inp, *ninp;
662         int s;
663
664         s = splnet();
665         for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
666                 ninp = LIST_NEXT(inp, inp_list);
667 #ifdef INET6
668                 if (!(inp->inp_vflag & INP_IPV4))
669                         continue;
670 #endif
671                 if (inp->inp_faddr.s_addr != faddr.s_addr ||
672                     inp->inp_socket == NULL)
673                         continue;
674                 (*notify)(inp, errno);
675         }
676         splx(s);
677 }
678
679 void
680 in_pcbpurgeif0(head, ifp)
681         struct inpcb *head;
682         struct ifnet *ifp;
683 {
684         struct inpcb *inp;
685         struct ip_moptions *imo;
686         int i, gap;
687
688         for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
689                 imo = inp->inp_moptions;
690                 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
691                         /*
692                          * Unselect the outgoing interface if it is being
693                          * detached.
694                          */
695                         if (imo->imo_multicast_ifp == ifp)
696                                 imo->imo_multicast_ifp = NULL;
697
698                         /*
699                          * Drop multicast group membership if we joined
700                          * through the interface being detached.
701                          */
702                         for (i = 0, gap = 0; i < imo->imo_num_memberships;
703                             i++) {
704                                 if (imo->imo_membership[i]->inm_ifp == ifp) {
705                                         in_delmulti(imo->imo_membership[i]);
706                                         gap++;
707                                 } else if (gap != 0)
708                                         imo->imo_membership[i - gap] =
709                                             imo->imo_membership[i];
710                         }
711                         imo->imo_num_memberships -= gap;
712                 }
713         }
714 }
715
716 /*
717  * Check for alternatives when higher level complains
718  * about service problems.  For now, invalidate cached
719  * routing information.  If the route was created dynamically
720  * (by a redirect), time to try a default gateway again.
721  */
722 void
723 in_losing(inp)
724         struct inpcb *inp;
725 {
726         struct rtentry *rt;
727         struct rt_addrinfo info;
728
729         if ((rt = inp->inp_route.ro_rt)) {
730                 bzero((caddr_t)&info, sizeof info);
731                 info.rti_flags = rt->rt_flags;
732                 info.rti_info[RTAX_DST] = rt_key(rt);
733                 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
734                 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
735                 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
736                 if (rt->rt_flags & RTF_DYNAMIC)
737                         (void) rtrequest1(RTM_DELETE, &info, NULL);
738                 inp->inp_route.ro_rt = NULL;
739                 rtfree(rt);
740                 /*
741                  * A new route can be allocated
742                  * the next time output is attempted.
743                  */
744         }
745 }
746
747 /*
748  * After a routing change, flush old routing
749  * and allocate a (hopefully) better one.
750  */
751 void
752 in_rtchange(inp, errno)
753         struct inpcb *inp;
754         int errno;
755 {
756         if (inp->inp_route.ro_rt) {
757                 rtfree(inp->inp_route.ro_rt);
758                 inp->inp_route.ro_rt = 0;
759                 /*
760                  * A new route can be allocated the next time
761                  * output is attempted.
762                  */
763         }
764 }
765
766 /*
767  * Lookup a PCB based on the local address and port.
768  */
769 struct inpcb *
770 in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
771         struct inpcbinfo *pcbinfo;
772         struct in_addr laddr;
773         u_int lport_arg;
774         int wild_okay;
775 {
776         struct inpcb *inp;
777         int matchwild = 3, wildcard;
778         u_short lport = lport_arg;
779
780         struct inpcbporthead *porthash;
781         struct inpcbport *phd;
782         struct inpcb *match = NULL;
783
784         /*
785          * Best fit PCB lookup.
786          *
787          * First see if this local port is in use by looking on the
788          * port hash list.
789          */
790         porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
791             pcbinfo->porthashmask)];
792         LIST_FOREACH(phd, porthash, phd_hash) {
793                 if (phd->phd_port == lport)
794                         break;
795         }
796         if (phd != NULL) {
797                 /*
798                  * Port is in use by one or more PCBs. Look for best
799                  * fit.
800                  */
801                 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
802                         wildcard = 0;
803 #ifdef INET6
804                         if ((inp->inp_vflag & INP_IPV4) == 0)
805                                 continue;
806 #endif
807                         if (inp->inp_faddr.s_addr != INADDR_ANY)
808                                 wildcard++;
809                         if (inp->inp_laddr.s_addr != INADDR_ANY) {
810                                 if (laddr.s_addr == INADDR_ANY)
811                                         wildcard++;
812                                 else if (inp->inp_laddr.s_addr != laddr.s_addr)
813                                         continue;
814                         } else {
815                                 if (laddr.s_addr != INADDR_ANY)
816                                         wildcard++;
817                         }
818                         if (wildcard && !wild_okay)
819                                 continue;
820                         if (wildcard < matchwild) {
821                                 match = inp;
822                                 matchwild = wildcard;
823                                 if (matchwild == 0) {
824                                         break;
825                                 }
826                         }
827                 }
828         }
829         return (match);
830 }
831
832 /*
833  * Lookup PCB in hash list.
834  */
835 struct inpcb *
836 in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
837         struct inpcbinfo *pcbinfo;
838         struct in_addr faddr, laddr;
839         u_int fport_arg, lport_arg;
840         boolean_t wildcard;
841         struct ifnet *ifp;
842 {
843         struct inpcbhead *head;
844         struct inpcb *inp;
845         u_short fport = fport_arg, lport = lport_arg;
846
847         /*
848          * First look for an exact match.
849          */
850         head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
851             laddr.s_addr, lport, pcbinfo->hashmask)];
852         LIST_FOREACH(inp, head, inp_hash) {
853 #ifdef INET6
854                 if (!(inp->inp_vflag & INP_IPV4))
855                         continue;
856 #endif
857                 if (in_hosteq(inp->inp_faddr, faddr) &&
858                     in_hosteq(inp->inp_laddr, laddr) &&
859                     inp->inp_fport == fport && inp->inp_lport == lport) {
860                         /* found */
861                         return (inp);
862                 }
863         }
864
865         if (wildcard) {
866                 struct inpcb *local_wild = NULL;
867 #ifdef INET6
868                 struct inpcb *local_wild_mapped = NULL;
869 #endif
870
871                 head = &pcbinfo->bindhashbase[INP_PCBBINDHASH(lport,
872                     pcbinfo->bindhashmask)];
873                 LIST_FOREACH(inp, head, inp_hash) {
874 #ifdef INET6
875                         if (!(inp->inp_vflag & INP_IPV4))
876                                 continue;
877 #endif
878                         if (inp->inp_lport == lport) {
879                                 if (ifp && ifp->if_type == IFT_FAITH &&
880                                     !(inp->inp_flags & INP_FAITH))
881                                         continue;
882                                 if (inp->inp_laddr.s_addr == laddr.s_addr)
883                                         return (inp);
884                                 if (inp->inp_laddr.s_addr == INADDR_ANY) {
885 #ifdef INET6
886                                         if (INP_CHECK_SOCKAF(inp->inp_socket,
887                                                              AF_INET6))
888                                                 local_wild_mapped = inp;
889                                         else
890 #endif
891                                                 local_wild = inp;
892                                 }
893                         }
894                 }
895 #ifdef INET6
896                 if (local_wild == NULL)
897                         return (local_wild_mapped);
898 #endif
899                 return (local_wild);
900         }
901
902         /*
903          * Not found.
904          */
905         return (NULL);
906 }
907
908 /*
909  * Insert PCB into connection hash table.
910  */
911 void
912 in_pcbinsconnhash(struct inpcb *inp)
913 {
914         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
915         struct inpcbhead *bucket;
916         u_int32_t hashkey_faddr, hashkey_laddr;
917
918 #ifdef INET6
919         if (inp->inp_vflag & INP_IPV6) {
920                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
921                 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
922         } else {
923 #endif
924                 hashkey_faddr = inp->inp_faddr.s_addr;
925                 hashkey_laddr = inp->inp_laddr.s_addr;
926 #ifdef INET6
927         }
928 #endif
929
930         inp->inp_flags |= INP_CONNECTED;
931
932         /*
933          * Insert into the connection hash table.
934          */
935         bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
936             inp->inp_fport, hashkey_laddr,
937             inp->inp_lport, pcbinfo->hashmask)];
938         LIST_INSERT_HEAD(bucket, inp, inp_hash);
939 }
940
941 /*
942  * Remove PCB from connection hash table.
943  */
944 void
945 in_pcbremconnhash(struct inpcb *inp)
946 {
947         KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
948         LIST_REMOVE(inp, inp_hash);
949         inp->inp_flags &= ~INP_CONNECTED;
950 }
951
952 /*
953  * Insert PCB into port hash table.
954  */
955 int
956 in_pcbinsporthash(struct inpcb *inp)
957 {
958         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
959         struct inpcbporthead *pcbporthash;
960         struct inpcbport *phd;
961
962         /*
963          * Insert into the port hash table.
964          */
965         pcbporthash = &pcbinfo->porthashbase[
966             INP_PCBPORTHASH(inp->inp_lport, pcbinfo->porthashmask)];
967
968         /* Go through port list and look for a head for this lport. */
969         LIST_FOREACH(phd, pcbporthash, phd_hash)
970                 if (phd->phd_port == inp->inp_lport)
971                         break;
972
973         /* If none exists, malloc one and tack it on. */
974         if (phd == NULL) {
975                 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport),
976                     M_PCB, M_NOWAIT);
977                 if (phd == NULL)
978                         return (ENOBUFS); /* XXX */
979                 phd->phd_port = inp->inp_lport;
980                 LIST_INIT(&phd->phd_pcblist);
981                 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
982         }
983
984         inp->inp_phd = phd;
985         LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
986
987         return (0);
988 }
989
990 /*
991  * Insert PCB into bind hash table.
992  */
993 void
994 in_pcbinsbindhash(struct inpcb *inp)
995 {
996         struct inpcbhead *bucket;
997
998         bucket = &inp->inp_pcbinfo->bindhashbase[INP_PCBBINDHASH(inp->inp_lport,
999             inp->inp_pcbinfo->porthashmask)];
1000
1001         inp->inp_flags |= INP_BOUND;
1002         LIST_INSERT_HEAD(bucket, inp, inp_hash);
1003 }
1004
1005 /*
1006  * Remove PCB from bind hash table.
1007  */
1008 void
1009 in_pcbrembindhash(struct inpcb *inp)
1010 {
1011         KASSERT(inp->inp_flags & INP_BOUND, ("inp not bound"));
1012         LIST_REMOVE(inp, inp_hash);
1013         inp->inp_flags &= ~INP_BOUND;
1014 }
1015
1016 static void
1017 in_pcbremhash(struct inpcb *inp)
1018 {
1019         if (inp->inp_flags & (INP_BOUND | INP_CONNECTED)) {
1020                 LIST_REMOVE(inp, inp_hash);
1021                 inp->inp_flags &= ~(INP_BOUND | INP_CONNECTED);
1022         }
1023 }
1024
1025 void
1026 in_pcbrehash(struct inpcb *inp, int state)
1027 {
1028         in_pcbremhash(inp);
1029         if (state == INP_BOUND)
1030                 in_pcbinsbindhash(inp);
1031         else
1032                 in_pcbinsconnhash(inp);
1033 }
1034
1035 /*
1036  * Remove PCB from various lists.
1037  */
1038 void
1039 in_pcbremlists(inp)
1040         struct inpcb *inp;
1041 {
1042         inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt;
1043         if (inp->inp_lport) {
1044                 struct inpcbport *phd = inp->inp_phd;
1045
1046                 LIST_REMOVE(inp, inp_portlist);
1047                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1048                         LIST_REMOVE(phd, phd_hash);
1049                         free(phd, M_PCB);
1050                 }
1051         }
1052         in_pcbremhash(inp);
1053         LIST_REMOVE(inp, inp_list);
1054         inp->inp_pcbinfo->ipi_count--;
1055 }
1056
1057 int
1058 prison_xinpcb(struct thread *td, struct inpcb *inp)
1059 {
1060         struct ucred *cr;
1061
1062         if (td->td_proc == NULL)
1063                 return (0);
1064         cr = td->td_proc->p_ucred;
1065         if (cr->cr_prison == NULL)
1066                 return (0);
1067         if (ntohl(inp->inp_laddr.s_addr) == cr->cr_prison->pr_ip)
1068                 return (0);
1069         return (1);
1070 }