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