proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / netinet6 / in6_pcb.c
1 /*      $FreeBSD: src/sys/netinet6/in6_pcb.c,v 1.10.2.9 2003/01/24 05:11:35 sam Exp $   */
2 /*      $DragonFly: src/sys/netinet6/in6_pcb.c,v 1.4 2003/06/25 03:56:04 dillon Exp $   */
3 /*      $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $        */
4   
5 /*
6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project 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 PROJECT 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 PROJECT 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  */
34
35 /*
36  * Copyright (c) 1982, 1986, 1991, 1993
37  *      The Regents of the University of California.  All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  *      This product includes software developed by the University of
50  *      California, Berkeley and its contributors.
51  * 4. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  *
67  *      @(#)in_pcb.c    8.2 (Berkeley) 1/4/94
68  */
69
70 #include "opt_inet.h"
71 #include "opt_inet6.h"
72 #include "opt_ipsec.h"
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/malloc.h>
77 #include <sys/mbuf.h>
78 #include <sys/domain.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/sockio.h>
83 #include <sys/errno.h>
84 #include <sys/time.h>
85 #include <sys/proc.h>
86 #include <sys/jail.h>
87
88 #include <vm/vm_zone.h>
89
90 #include <net/if.h>
91 #include <net/if_types.h>
92 #include <net/route.h>
93
94 #include <netinet/in.h>
95 #include <netinet/in_var.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/ip6.h>
98 #include <netinet/ip_var.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/nd6.h>
101 #include <netinet/in_pcb.h>
102 #include <netinet6/in6_pcb.h>
103
104 #ifdef IPSEC
105 #include <netinet6/ipsec.h>
106 #ifdef INET6
107 #include <netinet6/ipsec6.h>
108 #endif
109 #include <netinet6/ah.h>
110 #ifdef INET6
111 #include <netinet6/ah6.h>
112 #endif
113 #include <netkey/key.h>
114 #endif /* IPSEC */
115
116 #ifdef FAST_IPSEC
117 #include <netipsec/ipsec.h>
118 #include <netipsec/ipsec6.h>
119 #include <netipsec/key.h>
120 #define IPSEC
121 #endif /* FAST_IPSEC */
122
123 struct  in6_addr zeroin6_addr;
124
125 int
126 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
127 {
128         struct socket *so = inp->inp_socket;
129         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
130         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
131         u_short lport = 0;
132         int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
133
134         if (!in6_ifaddr) /* XXX broken! */
135                 return (EADDRNOTAVAIL);
136         if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
137                 return(EINVAL);
138         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
139                 wild = 1;
140         if (nam) {
141                 sin6 = (struct sockaddr_in6 *)nam;
142                 if (nam->sa_len != sizeof(*sin6))
143                         return(EINVAL);
144                 /*
145                  * family check.
146                  */
147                 if (nam->sa_family != AF_INET6)
148                         return(EAFNOSUPPORT);
149
150                 /* KAME hack: embed scopeid */
151                 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
152                         return EINVAL;
153                 /* this must be cleared for ifa_ifwithaddr() */
154                 sin6->sin6_scope_id = 0;
155
156                 lport = sin6->sin6_port;
157                 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
158                         /*
159                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
160                          * allow compepte duplication of binding if
161                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
162                          * and a multicast address is bound on both
163                          * new and duplicated sockets.
164                          */
165                         if (so->so_options & SO_REUSEADDR)
166                                 reuseport = SO_REUSEADDR|SO_REUSEPORT;
167                 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
168                         struct ifaddr *ia = NULL;
169
170                         sin6->sin6_port = 0;            /* yech... */
171                         if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
172                                 return(EADDRNOTAVAIL);
173
174                         /*
175                          * XXX: bind to an anycast address might accidentally
176                          * cause sending a packet with anycast source address.
177                          * We should allow to bind to a deprecated address, since
178                          * the application dare to use it.
179                          */
180                         if (ia &&
181                             ((struct in6_ifaddr *)ia)->ia6_flags &
182                             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
183                                 return(EADDRNOTAVAIL);
184                         }
185                 }
186                 if (lport) {
187                         struct inpcb *t;
188                         struct proc *p = td->td_proc; /* may be NULL */
189
190                         /* GROSS */
191                         if (ntohs(lport) < IPV6PORT_RESERVED && p &&
192                             suser_cred(p->p_ucred, PRISON_ROOT))
193                                 return(EACCES);
194                         if (so->so_cred->cr_uid != 0 &&
195                             !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
196                                 t = in6_pcblookup_local(pcbinfo,
197                                     &sin6->sin6_addr, lport,
198                                     INPLOOKUP_WILDCARD);
199                                 if (t &&
200                                     (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
201                                      !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
202                                      (t->inp_socket->so_options &
203                                       SO_REUSEPORT) == 0) &&
204                                     (so->so_cred->cr_uid !=
205                                      t->inp_socket->so_cred->cr_uid))
206                                         return (EADDRINUSE);
207                                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
208                                     IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
209                                         struct sockaddr_in sin;
210
211                                         in6_sin6_2_sin(&sin, sin6);
212                                         t = in_pcblookup_local(pcbinfo,
213                                                 sin.sin_addr, lport,
214                                                 INPLOOKUP_WILDCARD);
215                                         if (t &&
216                                             (so->so_cred->cr_uid !=
217                                              t->inp_socket->so_cred->cr_uid) &&
218                                             (ntohl(t->inp_laddr.s_addr) !=
219                                              INADDR_ANY ||
220                                              INP_SOCKAF(so) ==
221                                              INP_SOCKAF(t->inp_socket)))
222                                                 return (EADDRINUSE);
223                                 }
224                         }
225                         t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
226                                                 lport, wild);
227                         if (t && (reuseport & t->inp_socket->so_options) == 0)
228                                 return(EADDRINUSE);
229                         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
230                             IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
231                                 struct sockaddr_in sin;
232
233                                 in6_sin6_2_sin(&sin, sin6);
234                                 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
235                                                        lport, wild);
236                                 if (t &&
237                                     (reuseport & t->inp_socket->so_options)
238                                     == 0 &&
239                                     (ntohl(t->inp_laddr.s_addr)
240                                      != INADDR_ANY ||
241                                      INP_SOCKAF(so) ==
242                                      INP_SOCKAF(t->inp_socket)))
243                                         return (EADDRINUSE);
244                         }
245                 }
246                 inp->in6p_laddr = sin6->sin6_addr;
247         }
248         if (lport == 0) {
249                 int e;
250                 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, td)) != 0)
251                         return(e);
252         }
253         else {
254                 inp->inp_lport = lport;
255                 if (in_pcbinshash(inp) != 0) {
256                         inp->in6p_laddr = in6addr_any;
257                         inp->inp_lport = 0;
258                         return (EAGAIN);
259                 }
260         }
261         return(0);
262 }
263
264 /*
265  *   Transform old in6_pcbconnect() into an inner subroutine for new
266  *   in6_pcbconnect(): Do some validity-checking on the remote
267  *   address (in mbuf 'nam') and then determine local host address
268  *   (i.e., which interface) to use to access that remote host.
269  *
270  *   This preserves definition of in6_pcbconnect(), while supporting a
271  *   slightly different version for T/TCP.  (This is more than
272  *   a bit of a kludge, but cleaning up the internal interfaces would
273  *   have forced minor changes in every protocol).
274  */
275
276 int
277 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
278         struct in6_addr **plocal_addr6)
279 {
280         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
281         struct ifnet *ifp = NULL;
282         int error = 0;
283
284         if (nam->sa_len != sizeof (*sin6))
285                 return (EINVAL);
286         if (sin6->sin6_family != AF_INET6)
287                 return (EAFNOSUPPORT);
288         if (sin6->sin6_port == 0)
289                 return (EADDRNOTAVAIL);
290
291         /* KAME hack: embed scopeid */
292         if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
293                 return EINVAL;
294
295         if (in6_ifaddr) {
296                 /*
297                  * If the destination address is UNSPECIFIED addr,
298                  * use the loopback addr, e.g ::1.
299                  */
300                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
301                         sin6->sin6_addr = in6addr_loopback;
302         }
303         {
304                 /*
305                  * XXX: in6_selectsrc might replace the bound local address
306                  * with the address specified by setsockopt(IPV6_PKTINFO).
307                  * Is it the intended behavior?
308                  */
309                 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
310                                               inp->in6p_moptions,
311                                               &inp->in6p_route,
312                                               &inp->in6p_laddr, &error);
313                 if (*plocal_addr6 == 0) {
314                         if (error == 0)
315                                 error = EADDRNOTAVAIL;
316                         return(error);
317                 }
318                 /*
319                  * Don't do pcblookup call here; return interface in
320                  * plocal_addr6
321                  * and exit to caller, that will do the lookup.
322                  */
323         }
324
325         if (inp->in6p_route.ro_rt)
326                 ifp = inp->in6p_route.ro_rt->rt_ifp;
327
328         return(0);
329 }
330
331 /*
332  * Outer subroutine:
333  * Connect from a socket to a specified address.
334  * Both address and port must be specified in argument sin.
335  * If don't have a local address for this socket yet,
336  * then pick one.
337  */
338 int
339 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
340 {
341         struct in6_addr *addr6;
342         register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
343         int error;
344
345         /*
346          * Call inner routine, to assign local interface address.
347          * in6_pcbladdr() may automatically fill in sin6_scope_id.
348          */
349         if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
350                 return(error);
351
352         if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
353                                sin6->sin6_port,
354                               IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
355                               ? addr6 : &inp->in6p_laddr,
356                               inp->inp_lport, 0, NULL) != NULL) {
357                 return (EADDRINUSE);
358         }
359         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
360                 if (inp->inp_lport == 0) {
361                         error = in6_pcbbind(inp, (struct sockaddr *)0, td);
362                         if (error)
363                                 return (error);
364                 }
365                 inp->in6p_laddr = *addr6;
366         }
367         inp->in6p_faddr = sin6->sin6_addr;
368         inp->inp_fport = sin6->sin6_port;
369         /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
370         inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
371         if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
372                 inp->in6p_flowinfo |=
373                     (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
374
375         in_pcbrehash(inp);
376         return (0);
377 }
378
379 #if 0
380 /*
381  * Return an IPv6 address, which is the most appropriate for given
382  * destination and user specified options.
383  * If necessary, this function lookups the routing table and return
384  * an entry to the caller for later use.
385  */
386 struct in6_addr *
387 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
388         struct sockaddr_in6 *dstsock;
389         struct ip6_pktopts *opts;
390         struct ip6_moptions *mopts;
391         struct route_in6 *ro;
392         struct in6_addr *laddr;
393         int *errorp;
394 {
395         struct in6_addr *dst;
396         struct in6_ifaddr *ia6 = 0;
397         struct in6_pktinfo *pi = NULL;
398
399         dst = &dstsock->sin6_addr;
400         *errorp = 0;
401
402         /*
403          * If the source address is explicitly specified by the caller,
404          * use it.
405          */
406         if (opts && (pi = opts->ip6po_pktinfo) &&
407             !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
408                 return(&pi->ipi6_addr);
409
410         /*
411          * If the source address is not specified but the socket(if any)
412          * is already bound, use the bound address.
413          */
414         if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
415                 return(laddr);
416
417         /*
418          * If the caller doesn't specify the source address but
419          * the outgoing interface, use an address associated with
420          * the interface.
421          */
422         if (pi && pi->ipi6_ifindex) {
423                 /* XXX boundary check is assumed to be already done. */
424                 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
425                                        dst);
426                 if (ia6 == 0) {
427                         *errorp = EADDRNOTAVAIL;
428                         return(0);
429                 }
430                 return(&satosin6(&ia6->ia_addr)->sin6_addr);
431         }
432
433         /*
434          * If the destination address is a link-local unicast address or
435          * a multicast address, and if the outgoing interface is specified
436          * by the sin6_scope_id filed, use an address associated with the
437          * interface.
438          * XXX: We're now trying to define more specific semantics of
439          *      sin6_scope_id field, so this part will be rewritten in
440          *      the near future.
441          */
442         if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
443             dstsock->sin6_scope_id) {
444                 /*
445                  * I'm not sure if boundary check for scope_id is done
446                  * somewhere...
447                  */
448                 if (dstsock->sin6_scope_id < 0 ||
449                     if_index < dstsock->sin6_scope_id) {
450                         *errorp = ENXIO; /* XXX: better error? */
451                         return(0);
452                 }
453                 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
454                                        dst);
455                 if (ia6 == 0) {
456                         *errorp = EADDRNOTAVAIL;
457                         return(0);
458                 }
459                 return(&satosin6(&ia6->ia_addr)->sin6_addr);
460         }
461
462         /*
463          * If the destination address is a multicast address and
464          * the outgoing interface for the address is specified
465          * by the caller, use an address associated with the interface.
466          * There is a sanity check here; if the destination has node-local
467          * scope, the outgoing interfacde should be a loopback address.
468          * Even if the outgoing interface is not specified, we also
469          * choose a loopback interface as the outgoing interface.
470          */
471         if (IN6_IS_ADDR_MULTICAST(dst)) {
472                 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
473
474                 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
475                         ifp = &loif[0];
476                 }
477
478                 if (ifp) {
479                         ia6 = in6_ifawithscope(ifp, dst);
480                         if (ia6 == 0) {
481                                 *errorp = EADDRNOTAVAIL;
482                                 return(0);
483                         }
484                         return(&ia6->ia_addr.sin6_addr);
485                 }
486         }
487
488         /*
489          * If the next hop address for the packet is specified
490          * by caller, use an address associated with the route
491          * to the next hop.
492          */
493         {
494                 struct sockaddr_in6 *sin6_next;
495                 struct rtentry *rt;
496
497                 if (opts && opts->ip6po_nexthop) {
498                         sin6_next = satosin6(opts->ip6po_nexthop);
499                         rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
500                         if (rt) {
501                                 ia6 = in6_ifawithscope(rt->rt_ifp, dst);
502                                 if (ia6 == 0)
503                                         ia6 = ifatoia6(rt->rt_ifa);
504                         }
505                         if (ia6 == 0) {
506                                 *errorp = EADDRNOTAVAIL;
507                                 return(0);
508                         }
509                         return(&satosin6(&ia6->ia_addr)->sin6_addr);
510                 }
511         }
512
513         /*
514          * If route is known or can be allocated now,
515          * our src addr is taken from the i/f, else punt.
516          */
517         if (ro) {
518                 if (ro->ro_rt &&
519                     !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
520                         RTFREE(ro->ro_rt);
521                         ro->ro_rt = (struct rtentry *)0;
522                 }
523                 if (ro->ro_rt == (struct rtentry *)0 ||
524                     ro->ro_rt->rt_ifp == (struct ifnet *)0) {
525                         struct sockaddr_in6 *dst6;
526
527                         /* No route yet, so try to acquire one */
528                         bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
529                         dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
530                         dst6->sin6_family = AF_INET6;
531                         dst6->sin6_len = sizeof(struct sockaddr_in6);
532                         dst6->sin6_addr = *dst;
533                         if (IN6_IS_ADDR_MULTICAST(dst)) {
534                                 ro->ro_rt = rtalloc1(&((struct route *)ro)
535                                                      ->ro_dst, 0, 0UL);
536                         } else {
537                                 rtalloc((struct route *)ro);
538                         }
539                 }
540
541                 /*
542                  * in_pcbconnect() checks out IFF_LOOPBACK to skip using
543                  * the address. But we don't know why it does so.
544                  * It is necessary to ensure the scope even for lo0
545                  * so doesn't check out IFF_LOOPBACK.
546                  */
547
548                 if (ro->ro_rt) {
549                         ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
550                         if (ia6 == 0) /* xxx scope error ?*/
551                                 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
552                 }
553                 if (ia6 == 0) {
554                         *errorp = EHOSTUNREACH; /* no route */
555                         return(0);
556                 }
557                 return(&satosin6(&ia6->ia_addr)->sin6_addr);
558         }
559
560         *errorp = EADDRNOTAVAIL;
561         return(0);
562 }
563
564 /*
565  * Default hop limit selection. The precedence is as follows:
566  * 1. Hoplimit valued specified via ioctl.
567  * 2. (If the outgoing interface is detected) the current
568  *     hop limit of the interface specified by router advertisement.
569  * 3. The system default hoplimit.
570 */
571 int
572 in6_selecthlim(in6p, ifp)
573         struct in6pcb *in6p;
574         struct ifnet *ifp;
575 {
576         if (in6p && in6p->in6p_hops >= 0)
577                 return(in6p->in6p_hops);
578         else if (ifp)
579                 return(nd_ifinfo[ifp->if_index].chlim);
580         else
581                 return(ip6_defhlim);
582 }
583 #endif
584
585 void
586 in6_pcbdisconnect(inp)
587         struct inpcb *inp;
588 {
589         bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
590         inp->inp_fport = 0;
591         /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
592         inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
593         in_pcbrehash(inp);
594         if (inp->inp_socket->so_state & SS_NOFDREF)
595                 in6_pcbdetach(inp);
596 }
597
598 void
599 in6_pcbdetach(inp)
600         struct inpcb *inp;
601 {
602         struct socket *so = inp->inp_socket;
603         struct inpcbinfo *ipi = inp->inp_pcbinfo;
604
605 #ifdef IPSEC
606         if (inp->in6p_sp != NULL)
607                 ipsec6_delete_pcbpolicy(inp);
608 #endif /* IPSEC */
609         inp->inp_gencnt = ++ipi->ipi_gencnt;
610         in_pcbremlists(inp);
611         sotoinpcb(so) = 0;
612         sofree(so);
613
614         if (inp->in6p_options)
615                 m_freem(inp->in6p_options);
616         ip6_freepcbopts(inp->in6p_outputopts);
617         ip6_freemoptions(inp->in6p_moptions);
618         if (inp->in6p_route.ro_rt)
619                 rtfree(inp->in6p_route.ro_rt);
620         /* Check and free IPv4 related resources in case of mapped addr */
621         if (inp->inp_options)
622                 (void)m_free(inp->inp_options);
623         ip_freemoptions(inp->inp_moptions);
624
625         inp->inp_vflag = 0;
626         zfreei(ipi->ipi_zone, inp);
627 }
628
629 /*
630  * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
631  * modified to match the pru_sockaddr() and pru_peeraddr() entry points
632  * in struct pr_usrreqs, so that protocols can just reference then directly
633  * without the need for a wrapper function.  The socket must have a valid
634  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
635  * except through a kernel programming error, so it is acceptable to panic
636  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
637  * because there actually /is/ a programming error somewhere... XXX)
638  */
639 int
640 in6_setsockaddr(so, nam)
641         struct socket *so;
642         struct sockaddr **nam;
643 {
644         int s;
645         register struct inpcb *inp;
646         register struct sockaddr_in6 *sin6;
647
648         /*
649          * Do the malloc first in case it blocks.
650          */
651         MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
652         bzero(sin6, sizeof *sin6);
653         sin6->sin6_family = AF_INET6;
654         sin6->sin6_len = sizeof(*sin6);
655
656         s = splnet();
657         inp = sotoinpcb(so);
658         if (!inp) {
659                 splx(s);
660                 free(sin6, M_SONAME);
661                 return EINVAL;
662         }
663         sin6->sin6_port = inp->inp_lport;
664         sin6->sin6_addr = inp->in6p_laddr;
665         splx(s);
666         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
667                 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
668         else
669                 sin6->sin6_scope_id = 0;        /*XXX*/
670         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
671                 sin6->sin6_addr.s6_addr16[1] = 0;
672
673         *nam = (struct sockaddr *)sin6;
674         return 0;
675 }
676
677 int
678 in6_setpeeraddr(so, nam)
679         struct socket *so;
680         struct sockaddr **nam;
681 {
682         int s;
683         struct inpcb *inp;
684         register struct sockaddr_in6 *sin6;
685
686         /*
687          * Do the malloc first in case it blocks.
688          */
689         MALLOC(sin6, struct sockaddr_in6 *, sizeof(*sin6), M_SONAME, M_WAITOK);
690         bzero((caddr_t)sin6, sizeof (*sin6));
691         sin6->sin6_family = AF_INET6;
692         sin6->sin6_len = sizeof(struct sockaddr_in6);
693
694         s = splnet();
695         inp = sotoinpcb(so);
696         if (!inp) {
697                 splx(s);
698                 free(sin6, M_SONAME);
699                 return EINVAL;
700         }
701         sin6->sin6_port = inp->inp_fport;
702         sin6->sin6_addr = inp->in6p_faddr;
703         splx(s);
704         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
705                 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
706         else
707                 sin6->sin6_scope_id = 0;        /*XXX*/
708         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
709                 sin6->sin6_addr.s6_addr16[1] = 0;
710
711         *nam = (struct sockaddr *)sin6;
712         return 0;
713 }
714
715 int
716 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
717 {
718         struct  inpcb *inp = sotoinpcb(so);
719         int     error;
720
721         if (inp == NULL)
722                 return EINVAL;
723         if (inp->inp_vflag & INP_IPV4) {
724                 error = in_setsockaddr(so, nam);
725                 if (error == 0)
726                         in6_sin_2_v4mapsin6_in_sock(nam);
727         } else
728         /* scope issues will be handled in in6_setsockaddr(). */
729         error = in6_setsockaddr(so, nam);
730
731         return error;
732 }
733
734 int
735 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
736 {
737         struct  inpcb *inp = sotoinpcb(so);
738         int     error;
739
740         if (inp == NULL)
741                 return EINVAL;
742         if (inp->inp_vflag & INP_IPV4) {
743                 error = in_setpeeraddr(so, nam);
744                 if (error == 0)
745                         in6_sin_2_v4mapsin6_in_sock(nam);
746         } else
747         /* scope issues will be handled in in6_setpeeraddr(). */
748         error = in6_setpeeraddr(so, nam);
749
750         return error;
751 }
752
753 /*
754  * Pass some notification to all connections of a protocol
755  * associated with address dst.  The local address and/or port numbers
756  * may be specified to limit the search.  The "usual action" will be
757  * taken, depending on the ctlinput cmd.  The caller must filter any
758  * cmds that are uninteresting (e.g., no error in the map).
759  * Call the protocol specific routine (if any) to report
760  * any errors for each matching socket.
761  *
762  * Must be called at splnet.
763  */
764 void
765 in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, notify)
766         struct inpcbhead *head;
767         struct sockaddr *dst, *src;
768         u_int fport_arg, lport_arg;
769         int cmd;
770         void (*notify) __P((struct inpcb *, int));
771 {
772         struct inpcb *inp, *ninp;
773         struct sockaddr_in6 sa6_src, *sa6_dst;
774         u_short fport = fport_arg, lport = lport_arg;
775         u_int32_t flowinfo;
776         int errno, s;
777
778         if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
779                 return;
780
781         sa6_dst = (struct sockaddr_in6 *)dst;
782         if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
783                 return;
784
785         /*
786          * note that src can be NULL when we get notify by local fragmentation.
787          */
788         sa6_src = (src == NULL) ? sa6_any : *(struct sockaddr_in6 *)src;
789         flowinfo = sa6_src.sin6_flowinfo;
790
791         /*
792          * Redirects go to all references to the destination,
793          * and use in6_rtchange to invalidate the route cache.
794          * Dead host indications: also use in6_rtchange to invalidate
795          * the cache, and deliver the error to all the sockets.
796          * Otherwise, if we have knowledge of the local port and address,
797          * deliver only to that socket.
798          */
799         if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
800                 fport = 0;
801                 lport = 0;
802                 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
803
804                 if (cmd != PRC_HOSTDEAD)
805                         notify = in6_rtchange;
806         }
807         errno = inet6ctlerrmap[cmd];
808         s = splnet();
809         for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
810                 ninp = LIST_NEXT(inp, inp_list);
811
812                 if ((inp->inp_vflag & INP_IPV6) == 0)
813                         continue;
814
815                 /*
816                  * Detect if we should notify the error. If no source and
817                  * destination ports are specifed, but non-zero flowinfo and
818                  * local address match, notify the error. This is the case
819                  * when the error is delivered with an encrypted buffer
820                  * by ESP. Otherwise, just compare addresses and ports
821                  * as usual.
822                  */
823                 if (lport == 0 && fport == 0 && flowinfo &&
824                     inp->inp_socket != NULL &&
825                     flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
826                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
827                         goto do_notify;
828                 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
829                                              &sa6_dst->sin6_addr) ||
830                          inp->inp_socket == 0 ||
831                          (lport && inp->inp_lport != lport) ||
832                          (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
833                           !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
834                                               &sa6_src.sin6_addr)) ||
835                          (fport && inp->inp_fport != fport))
836                         continue;
837
838           do_notify:
839                 if (notify)
840                         (*notify)(inp, errno);
841         }
842         splx(s);
843 }
844
845 /*
846  * Lookup a PCB based on the local address and port.
847  */
848 struct inpcb *
849 in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
850         struct inpcbinfo *pcbinfo;
851         struct in6_addr *laddr;
852         u_int lport_arg;
853         int wild_okay;
854 {
855         register struct inpcb *inp;
856         int matchwild = 3, wildcard;
857         u_short lport = lport_arg;
858
859         if (!wild_okay) {
860                 struct inpcbhead *head;
861                 /*
862                  * Look for an unconnected (wildcard foreign addr) PCB that
863                  * matches the local address and port we're looking for.
864                  */
865                 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
866                                                       pcbinfo->hashmask)];
867                 LIST_FOREACH(inp, head, inp_hash) {
868                         if ((inp->inp_vflag & INP_IPV6) == 0)
869                                 continue;
870                         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
871                             IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
872                             inp->inp_lport == lport) {
873                                 /*
874                                  * Found.
875                                  */
876                                 return (inp);
877                         }
878                 }
879                 /*
880                  * Not found.
881                  */
882                 return (NULL);
883         } else {
884                 struct inpcbporthead *porthash;
885                 struct inpcbport *phd;
886                 struct inpcb *match = NULL;
887                 /*
888                  * Best fit PCB lookup.
889                  *
890                  * First see if this local port is in use by looking on the
891                  * port hash list.
892                  */
893                 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
894                     pcbinfo->porthashmask)];
895                 LIST_FOREACH(phd, porthash, phd_hash) {
896                         if (phd->phd_port == lport)
897                                 break;
898                 }
899                 if (phd != NULL) {
900                         /*
901                          * Port is in use by one or more PCBs. Look for best
902                          * fit.
903                          */
904                         LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
905                                 wildcard = 0;
906                                 if ((inp->inp_vflag & INP_IPV6) == 0)
907                                         continue;
908                                 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
909                                         wildcard++;
910                                 if (!IN6_IS_ADDR_UNSPECIFIED(
911                                         &inp->in6p_laddr)) {
912                                         if (IN6_IS_ADDR_UNSPECIFIED(laddr))
913                                                 wildcard++;
914                                         else if (!IN6_ARE_ADDR_EQUAL(
915                                                 &inp->in6p_laddr, laddr))
916                                                 continue;
917                                 } else {
918                                         if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
919                                                 wildcard++;
920                                 }
921                                 if (wildcard < matchwild) {
922                                         match = inp;
923                                         matchwild = wildcard;
924                                         if (matchwild == 0) {
925                                                 break;
926                                         }
927                                 }
928                         }
929                 }
930                 return (match);
931         }
932 }
933
934 void
935 in6_pcbpurgeif0(head, ifp)
936         struct in6pcb *head;
937         struct ifnet *ifp;
938 {
939         struct in6pcb *in6p;
940         struct ip6_moptions *im6o;
941         struct in6_multi_mship *imm, *nimm;
942
943         for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
944                 im6o = in6p->in6p_moptions;
945                 if ((in6p->inp_vflag & INP_IPV6) &&
946                     im6o) {
947                         /*
948                          * Unselect the outgoing interface if it is being
949                          * detached.
950                          */
951                         if (im6o->im6o_multicast_ifp == ifp)
952                                 im6o->im6o_multicast_ifp = NULL;
953
954                         /*
955                          * Drop multicast group membership if we joined
956                          * through the interface being detached.
957                          * XXX controversial - is it really legal for kernel
958                          * to force this?
959                          */
960                         for (imm = im6o->im6o_memberships.lh_first;
961                              imm != NULL; imm = nimm) {
962                                 nimm = imm->i6mm_chain.le_next;
963                                 if (imm->i6mm_maddr->in6m_ifp == ifp) {
964                                         LIST_REMOVE(imm, i6mm_chain);
965                                         in6_delmulti(imm->i6mm_maddr);
966                                         free(imm, M_IPMADDR);
967                                 }
968                         }
969                 }
970         }
971 }
972
973 /*
974  * Check for alternatives when higher level complains
975  * about service problems.  For now, invalidate cached
976  * routing information.  If the route was created dynamically
977  * (by a redirect), time to try a default gateway again.
978  */
979 void
980 in6_losing(in6p)
981         struct inpcb *in6p;
982 {
983         struct rtentry *rt;
984         struct rt_addrinfo info;
985
986         if ((rt = in6p->in6p_route.ro_rt) != NULL) {
987                 bzero((caddr_t)&info, sizeof(info));
988                 info.rti_flags = rt->rt_flags;
989                 info.rti_info[RTAX_DST] = rt_key(rt);
990                 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
991                 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
992                 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
993                 if (rt->rt_flags & RTF_DYNAMIC)
994                         (void)rtrequest1(RTM_DELETE, &info, NULL);
995                 in6p->in6p_route.ro_rt = NULL;
996                 rtfree(rt);
997                 /*
998                  * A new route can be allocated
999                  * the next time output is attempted.
1000                  */
1001         }
1002 }
1003
1004 /*
1005  * After a routing change, flush old routing
1006  * and allocate a (hopefully) better one.
1007  */
1008 void
1009 in6_rtchange(inp, errno)
1010         struct inpcb *inp;
1011         int errno;
1012 {
1013         if (inp->in6p_route.ro_rt) {
1014                 rtfree(inp->in6p_route.ro_rt);
1015                 inp->in6p_route.ro_rt = 0;
1016                 /*
1017                  * A new route can be allocated the next time
1018                  * output is attempted.
1019                  */
1020         }
1021 }
1022
1023 /*
1024  * Lookup PCB in hash list.
1025  */
1026 struct inpcb *
1027 in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
1028         struct inpcbinfo *pcbinfo;
1029         struct in6_addr *faddr, *laddr;
1030         u_int fport_arg, lport_arg;
1031         int wildcard;
1032         struct ifnet *ifp;
1033 {
1034         struct inpcbhead *head;
1035         register struct inpcb *inp;
1036         u_short fport = fport_arg, lport = lport_arg;
1037         int faith;
1038
1039         if (faithprefix_p != NULL)
1040                 faith = (*faithprefix_p)(laddr);
1041         else
1042                 faith = 0;
1043
1044         /*
1045          * First look for an exact match.
1046          */
1047         head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
1048                                               lport, fport,
1049                                               pcbinfo->hashmask)];
1050         LIST_FOREACH(inp, head, inp_hash) {
1051                 if ((inp->inp_vflag & INP_IPV6) == 0)
1052                         continue;
1053                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1054                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1055                     inp->inp_fport == fport &&
1056                     inp->inp_lport == lport) {
1057                         /*
1058                          * Found.
1059                          */
1060                         return (inp);
1061                 }
1062         }
1063         if (wildcard) {
1064                 struct inpcb *local_wild = NULL;
1065
1066                 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1067                                                       pcbinfo->hashmask)];
1068                 LIST_FOREACH(inp, head, inp_hash) {
1069                         if ((inp->inp_vflag & INP_IPV6) == 0)
1070                                 continue;
1071                         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1072                             inp->inp_lport == lport) {
1073                                 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1074                                         continue;
1075                                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
1076                                                        laddr))
1077                                         return (inp);
1078                                 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1079                                         local_wild = inp;
1080                         }
1081                 }
1082                 return (local_wild);
1083         }
1084
1085         /*
1086          * Not found.
1087          */
1088         return (NULL);
1089 }
1090
1091 void
1092 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1093 {
1094         struct ip6_hdr *ip;
1095
1096         ip = mtod(m, struct ip6_hdr *);
1097         bzero(sin6, sizeof(*sin6));
1098         sin6->sin6_len = sizeof(*sin6);
1099         sin6->sin6_family = AF_INET6;
1100         sin6->sin6_addr = ip->ip6_src;
1101         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1102                 sin6->sin6_addr.s6_addr16[1] = 0;
1103         sin6->sin6_scope_id =
1104                 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1105                 ? m->m_pkthdr.rcvif->if_index : 0;
1106
1107         return;
1108 }