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