Merge branch 'vendor/BINUTILS221'
[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.35 2008/09/04 09:08:22 hasso 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/priv.h>
87 #include <sys/jail.h>
88
89 #include <sys/thread2.h>
90 #include <sys/msgport2.h>
91
92 #include <vm/vm_zone.h>
93
94 #include <net/if.h>
95 #include <net/if_types.h>
96 #include <net/route.h>
97
98 #include <netinet/in.h>
99 #include <netinet/in_var.h>
100 #include <netinet/in_systm.h>
101 #include <netinet/ip6.h>
102 #include <netinet/ip_var.h>
103 #include <netinet6/ip6_var.h>
104 #include <netinet6/nd6.h>
105 #include <netinet/in_pcb.h>
106 #include <netinet6/in6_pcb.h>
107
108 #ifdef IPSEC
109 #include <netinet6/ipsec.h>
110 #ifdef INET6
111 #include <netinet6/ipsec6.h>
112 #endif
113 #include <netinet6/ah.h>
114 #ifdef INET6
115 #include <netinet6/ah6.h>
116 #endif
117 #include <netproto/key/key.h>
118 #endif /* IPSEC */
119
120 #ifdef FAST_IPSEC
121 #include <netproto/ipsec/ipsec.h>
122 #include <netproto/ipsec/ipsec6.h>
123 #include <netproto/ipsec/key.h>
124 #define IPSEC
125 #endif /* FAST_IPSEC */
126
127 struct  in6_addr zeroin6_addr;
128
129 int
130 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
131 {
132         struct socket *so = inp->inp_socket;
133         struct sockaddr_in6 *sin6 = NULL;
134         struct sockaddr_in6 jsin6;
135         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
136         struct proc *p = td->td_proc;
137         struct ucred *cred = NULL;
138         u_short lport = 0;
139         int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
140
141         if (!in6_ifaddr) /* XXX broken! */
142                 return (EADDRNOTAVAIL);
143         if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
144                 return (EINVAL);
145         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
146                 wild = 1;
147         if (p)
148                 cred = p->p_ucred;
149         if (nam) {
150                 sin6 = (struct sockaddr_in6 *)nam;
151                 if (nam->sa_len != sizeof(*sin6))
152                         return (EINVAL);
153                 /*
154                  * family check.
155                  */
156                 if (nam->sa_family != AF_INET6)
157                         return (EAFNOSUPPORT);
158
159                 if (!prison_replace_wildcards(td, nam))
160                         return (EINVAL);
161
162                 /* KAME hack: embed scopeid */
163                 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
164                         return EINVAL;
165                 /* this must be cleared for ifa_ifwithaddr() */
166                 sin6->sin6_scope_id = 0;
167
168                 lport = sin6->sin6_port;
169                 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
170                         /*
171                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
172                          * allow compepte duplication of binding if
173                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
174                          * and a multicast address is bound on both
175                          * new and duplicated sockets.
176                          */
177                         if (so->so_options & SO_REUSEADDR)
178                                 reuseport = SO_REUSEADDR|SO_REUSEPORT;
179                 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
180                         struct ifaddr *ia = NULL;
181
182                         sin6->sin6_port = 0;            /* yech... */
183                         if (!prison_replace_wildcards(td, (struct sockaddr *)sin6)) {
184                                 sin6->sin6_addr = kin6addr_any;
185                                 return(EINVAL);
186                         }
187                         if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
188                                 return (EADDRNOTAVAIL);
189
190                         /*
191                          * XXX: bind to an anycast address might accidentally
192                          * cause sending a packet with anycast source address.
193                          * We should allow to bind to a deprecated address, since
194                          * the application dare to use it.
195                          */
196                         if (ia &&
197                             ((struct in6_ifaddr *)ia)->ia6_flags &
198                             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
199                                 return (EADDRNOTAVAIL);
200                         }
201                 }
202                 if (lport) {
203                         struct inpcb *t;
204
205                         /* GROSS */
206                         if (ntohs(lport) < IPV6PORT_RESERVED && cred &&
207                             priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))
208                                 return (EACCES);
209                         if (so->so_cred->cr_uid != 0 &&
210                             !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
211                                 t = in6_pcblookup_local(pcbinfo,
212                                     &sin6->sin6_addr, lport,
213                                     INPLOOKUP_WILDCARD, cred);
214                                 if (t &&
215                                     (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
216                                      !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
217                                      (t->inp_socket->so_options &
218                                       SO_REUSEPORT) == 0) &&
219                                     (so->so_cred->cr_uid !=
220                                      t->inp_socket->so_cred->cr_uid))
221                                         return (EADDRINUSE);
222                                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
223                                     IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
224                                         struct sockaddr_in sin;
225
226                                         in6_sin6_2_sin(&sin, sin6);
227                                         t = in_pcblookup_local(pcbinfo,
228                                                 sin.sin_addr, lport,
229                                                 INPLOOKUP_WILDCARD, cred);
230                                         if (t &&
231                                             (so->so_cred->cr_uid !=
232                                              t->inp_socket->so_cred->cr_uid) &&
233                                             (ntohl(t->inp_laddr.s_addr) !=
234                                              INADDR_ANY ||
235                                              INP_SOCKAF(so) ==
236                                              INP_SOCKAF(t->inp_socket)))
237                                                 return (EADDRINUSE);
238                                 }
239                         }
240                         if (cred && cred->cr_prison &&
241                             !prison_replace_wildcards(td, nam))
242                                 return(EADDRNOTAVAIL);
243                         t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
244                                                 lport, wild, cred);
245                         if (t && (reuseport & t->inp_socket->so_options) == 0)
246                                 return (EADDRINUSE);
247                         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
248                             IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
249                                 struct sockaddr_in sin;
250
251                                 in6_sin6_2_sin(&sin, sin6);
252                                 t = in_pcblookup_local(pcbinfo,
253                                                        sin.sin_addr, lport,
254                                                        wild, cred);
255                                 if (t &&
256                                     (reuseport & t->inp_socket->so_options)
257                                     == 0 &&
258                                     (ntohl(t->inp_laddr.s_addr)
259                                      != INADDR_ANY ||
260                                      INP_SOCKAF(so) ==
261                                      INP_SOCKAF(t->inp_socket)))
262                                         return (EADDRINUSE);
263                         }
264                 }
265                 inp->in6p_laddr = sin6->sin6_addr;
266         }
267         if (lport == 0) {
268                 int e;
269
270                 jsin6.sin6_addr = inp->in6p_laddr;
271                 jsin6.sin6_family = AF_INET6;
272                 if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin6)) {
273                         inp->in6p_laddr = kin6addr_any;
274                         inp->inp_lport = 0;
275                         return (EINVAL);
276                 }
277
278                 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, td)) != 0)
279                         return (e);
280         }
281         else {
282                 inp->inp_lport = lport;
283                 if (in_pcbinsporthash(inp) != 0) {
284                         inp->in6p_laddr = kin6addr_any;
285                         inp->inp_lport = 0;
286                         return (EAGAIN);
287                 }
288         }
289         return (0);
290 }
291
292 /*
293  *   Transform old in6_pcbconnect() into an inner subroutine for new
294  *   in6_pcbconnect(): Do some validity-checking on the remote
295  *   address (in mbuf 'nam') and then determine local host address
296  *   (i.e., which interface) to use to access that remote host.
297  *
298  *   This preserves definition of in6_pcbconnect(), while supporting a
299  *   slightly different version for T/TCP.  (This is more than
300  *   a bit of a kludge, but cleaning up the internal interfaces would
301  *   have forced minor changes in every protocol).
302  */
303
304 int
305 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
306              struct in6_addr **plocal_addr6, struct thread *td)
307 {
308         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
309         struct ifnet *ifp = NULL;
310         int error = 0;
311
312         if (nam->sa_len != sizeof (*sin6))
313                 return (EINVAL);
314         if (sin6->sin6_family != AF_INET6)
315                 return (EAFNOSUPPORT);
316         if (sin6->sin6_port == 0)
317                 return (EADDRNOTAVAIL);
318
319         /* KAME hack: embed scopeid */
320         if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
321                 return EINVAL;
322
323         if (in6_ifaddr) {
324                 /*
325                  * If the destination address is UNSPECIFIED addr,
326                  * use the loopback addr, e.g ::1.
327                  */
328                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
329                         sin6->sin6_addr = kin6addr_loopback;
330         }
331         {
332                 /*
333                  * XXX: in6_selectsrc might replace the bound local address
334                  * with the address specified by setsockopt(IPV6_PKTINFO).
335                  * Is it the intended behavior?
336                  */
337                 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
338                                               inp->in6p_moptions,
339                                               &inp->in6p_route,
340                                               &inp->in6p_laddr, &error, td);
341                 if (*plocal_addr6 == 0) {
342                         if (error == 0)
343                                 error = EADDRNOTAVAIL;
344                         return (error);
345                 }
346                 /*
347                  * Don't do pcblookup call here; return interface in
348                  * plocal_addr6
349                  * and exit to caller, that will do the lookup.
350                  */
351         }
352
353         if (inp->in6p_route.ro_rt)
354                 ifp = inp->in6p_route.ro_rt->rt_ifp;
355
356         return (0);
357 }
358
359 /*
360  * Outer subroutine:
361  * Connect from a socket to a specified address.
362  * Both address and port must be specified in argument sin.
363  * If don't have a local address for this socket yet,
364  * then pick one.
365  */
366 int
367 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
368 {
369         struct in6_addr *addr6;
370         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
371         int error;
372
373         /*
374          * Call inner routine, to assign local interface address.
375          * in6_pcbladdr() may automatically fill in sin6_scope_id.
376          */
377         if ((error = in6_pcbladdr(inp, nam, &addr6, td)) != 0)
378                 return (error);
379
380         if (in6_pcblookup_hash(inp->inp_cpcbinfo, &sin6->sin6_addr,
381                                sin6->sin6_port,
382                               IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
383                               ? addr6 : &inp->in6p_laddr,
384                               inp->inp_lport, 0, NULL) != NULL) {
385                 return (EADDRINUSE);
386         }
387         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
388                 if (inp->inp_lport == 0) {
389                         error = in6_pcbbind(inp, NULL, td);
390                         if (error)
391                                 return (error);
392                 }
393                 inp->in6p_laddr = *addr6;
394         }
395         inp->in6p_faddr = sin6->sin6_addr;
396         inp->inp_fport = sin6->sin6_port;
397         /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
398         inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
399         if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
400                 inp->in6p_flowinfo |=
401                     (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
402
403         in_pcbinsconnhash(inp);
404         return (0);
405 }
406
407 #if 0
408 /*
409  * Return an IPv6 address, which is the most appropriate for given
410  * destination and user specified options.
411  * If necessary, this function lookups the routing table and return
412  * an entry to the caller for later use.
413  */
414 struct in6_addr *
415 in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
416               struct ip6_moptions *mopts, struct route_in6 *ro,
417               struct in6_addr *laddr, int *errorp, struct thread *td)
418 {
419         struct sockaddr_in6 jsin6;
420         struct ucred *cred = NULL;
421         struct in6_addr *dst;
422         struct in6_ifaddr *ia6 = 0;
423         struct in6_pktinfo *pi = NULL;
424         int jailed = 0;
425
426         if (td && td->td_proc && td->td_proc->p_ucred)
427                 cred = td->td_proc->p_ucred;
428         if (cred && cred->cr_prison)
429                 jailed = 1;
430         jsin6.sin6_family = AF_INET6;
431         dst = &dstsock->sin6_addr;
432         *errorp = 0;
433
434         /*
435          * If the source address is explicitly specified by the caller,
436          * use it.
437          */
438         if (opts && (pi = opts->ip6po_pktinfo) &&
439             !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
440                 jsin6.sin6_addr = pi->ipi6_addr;
441                 if (jailed && !jailed_ip(cred->cr_prison,
442                     (struct sockaddr *)&jsin6)) {
443                         return(0);
444                 } else {
445                         return (&pi->ipi6_addr);
446                 }
447         }
448
449         /*
450          * If the source address is not specified but the socket(if any)
451          * is already bound, use the bound address.
452          */
453         if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
454                 jsin6.sin6_addr = *laddr;
455                 if (jailed && !jailed_ip(cred->cr_prison,
456                     (struct sockaddr *)&jsin6)) {
457                         return(0);
458                 } else {
459                         return (laddr);
460                 }
461         }
462
463         /*
464          * If the caller doesn't specify the source address but
465          * the outgoing interface, use an address associated with
466          * the interface.
467          */
468         if (pi && pi->ipi6_ifindex) {
469                 /* XXX boundary check is assumed to be already done. */
470                 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
471                                        dst);
472
473                 if (ia6 && jailed) {
474                         jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
475                         if (!jailed_ip(cred->cr_prison,
476                                 (struct sockaddr *)&jsin6))
477                                 ia6 = 0;
478                 }
479
480                 if (ia6 == 0) {
481                         *errorp = EADDRNOTAVAIL;
482                         return (0);
483                 }
484                 return (&satosin6(&ia6->ia_addr)->sin6_addr);
485         }
486
487         /*
488          * If the destination address is a link-local unicast address or
489          * a multicast address, and if the outgoing interface is specified
490          * by the sin6_scope_id filed, use an address associated with the
491          * interface.
492          * XXX: We're now trying to define more specific semantics of
493          *      sin6_scope_id field, so this part will be rewritten in
494          *      the near future.
495          */
496         if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
497             dstsock->sin6_scope_id) {
498                 /*
499                  * I'm not sure if boundary check for scope_id is done
500                  * somewhere...
501                  */
502                 if (dstsock->sin6_scope_id < 0 ||
503                     if_index < dstsock->sin6_scope_id) {
504                         *errorp = ENXIO; /* XXX: better error? */
505                         return (0);
506                 }
507                 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
508                                        dst);
509
510                 if (ia6 && jailed) {
511                         jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
512                         if (!jailed_ip(cred->cr_prison,
513                                 (struct sockaddr *)&jsin6))
514                                 ia6 = 0;
515                 }
516
517                 if (ia6 == 0) {
518                         *errorp = EADDRNOTAVAIL;
519                         return (0);
520                 }
521                 return (&satosin6(&ia6->ia_addr)->sin6_addr);
522         }
523
524         /*
525          * If the destination address is a multicast address and
526          * the outgoing interface for the address is specified
527          * by the caller, use an address associated with the interface.
528          * There is a sanity check here; if the destination has node-local
529          * scope, the outgoing interfacde should be a loopback address.
530          * Even if the outgoing interface is not specified, we also
531          * choose a loopback interface as the outgoing interface.
532          */
533         if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
534                 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
535
536                 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
537                         ifp = &loif[0];
538                 }
539
540                 if (ifp) {
541                         ia6 = in6_ifawithscope(ifp, dst);
542                         if (ia6 == 0) {
543                                 *errorp = EADDRNOTAVAIL;
544                                 return (0);
545                         }
546                         return (&ia6->ia_addr.sin6_addr);
547                 }
548         }
549
550         /*
551          * If the next hop address for the packet is specified
552          * by caller, use an address associated with the route
553          * to the next hop.
554          */
555         {
556                 struct sockaddr_in6 *sin6_next;
557                 struct rtentry *rt;
558
559                 if (opts && opts->ip6po_nexthop) {
560                         sin6_next = satosin6(opts->ip6po_nexthop);
561                         rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
562                         if (rt) {
563                                 ia6 = in6_ifawithscope(rt->rt_ifp, dst);
564                                 if (ia6 == 0)
565                                         ia6 = ifatoia6(rt->rt_ifa);
566                         }
567                         if (ia6 && jailed) {
568                                 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
569                                 if (!jailed_ip(cred->cr_prison,
570                                         (struct sockaddr *)&jsin6))
571                                         ia6 = 0;
572                         }
573
574                         if (ia6 == 0) {
575                                 *errorp = EADDRNOTAVAIL;
576                                 return (0);
577                         }
578                         return (&satosin6(&ia6->ia_addr)->sin6_addr);
579                 }
580         }
581
582         /*
583          * If route is known or can be allocated now,
584          * our src addr is taken from the i/f, else punt.
585          */
586         if (ro) {
587                 if (ro->ro_rt &&
588                     !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
589                         RTFREE(ro->ro_rt);
590                         ro->ro_rt = NULL;
591                 }
592                 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
593                         struct sockaddr_in6 *dst6;
594
595                         /* No route yet, so try to acquire one */
596                         bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
597                         dst6 = &ro->ro_dst;
598                         dst6->sin6_family = AF_INET6;
599                         dst6->sin6_len = sizeof(struct sockaddr_in6);
600                         dst6->sin6_addr = *dst;
601                         if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
602                                 ro->ro_rt =
603                                   rtpurelookup((struct sockaddr *)&ro->ro_dst);
604                         } else {
605                                 rtalloc((struct route *)ro);
606                         }
607                 }
608
609                 /*
610                  * in_pcbconnect() checks out IFF_LOOPBACK to skip using
611                  * the address. But we don't know why it does so.
612                  * It is necessary to ensure the scope even for lo0
613                  * so doesn't check out IFF_LOOPBACK.
614                  */
615
616                 if (ro->ro_rt) {
617                         ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
618                         if (ia6 && jailed) {
619                                 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
620                                 if (!jailed_ip(cred->cr_prison,
621                                         (struct sockaddr *)&jsin6))
622                                         ia6 = 0;
623                         }
624
625                         if (ia6 == 0) /* xxx scope error ?*/
626                                 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
627
628                         if (ia6 && jailed) {
629                                 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
630                                 if (!jailed_ip(cred->cr_prison,
631                                         (struct sockaddr *)&jsin6))
632                                         ia6 = 0;
633                         }
634                 }
635                 if (ia6 == 0) {
636                         *errorp = EHOSTUNREACH; /* no route */
637                         return (0);
638                 }
639                 return (&satosin6(&ia6->ia_addr)->sin6_addr);
640         }
641
642         *errorp = EADDRNOTAVAIL;
643         return (0);
644 }
645
646 /*
647  * Default hop limit selection. The precedence is as follows:
648  * 1. Hoplimit valued specified via ioctl.
649  * 2. (If the outgoing interface is detected) the current
650  *     hop limit of the interface specified by router advertisement.
651  * 3. The system default hoplimit.
652 */
653 int
654 in6_selecthlim(struct in6pcb *in6p, struct ifnet *ifp)
655 {
656         if (in6p && in6p->in6p_hops >= 0)
657                 return (in6p->in6p_hops);
658         else if (ifp)
659                 return (ND_IFINFO(ifp)->chlim);
660         else
661                 return (ip6_defhlim);
662 }
663 #endif
664
665 void
666 in6_pcbdisconnect(struct inpcb *inp)
667 {
668         bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
669         inp->inp_fport = 0;
670         /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
671         inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
672         in_pcbremconnhash(inp);
673         if (inp->inp_socket->so_state & SS_NOFDREF)
674                 in6_pcbdetach(inp);
675 }
676
677 void
678 in6_pcbdetach(struct inpcb *inp)
679 {
680         struct socket *so = inp->inp_socket;
681         struct inpcbinfo *ipi = inp->inp_pcbinfo;
682
683 #ifdef IPSEC
684         if (inp->in6p_sp != NULL)
685                 ipsec6_delete_pcbpolicy(inp);
686 #endif /* IPSEC */
687         inp->inp_gencnt = ++ipi->ipi_gencnt;
688         in_pcbremlists(inp);
689         so->so_pcb = NULL;
690         KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
691         sofree(so);             /* remove pcb ref */
692
693         if (inp->in6p_options)
694                 m_freem(inp->in6p_options);
695         ip6_freepcbopts(inp->in6p_outputopts);
696         ip6_freemoptions(inp->in6p_moptions);
697         if (inp->in6p_route.ro_rt)
698                 rtfree(inp->in6p_route.ro_rt);
699         /* Check and free IPv4 related resources in case of mapped addr */
700         if (inp->inp_options)
701                 m_free(inp->inp_options);
702         ip_freemoptions(inp->inp_moptions);
703
704         inp->inp_vflag = 0;
705         kfree(inp, M_PCB);
706 }
707
708 /*
709  * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
710  * modified to match the pru_sockaddr() and pru_peeraddr() entry points
711  * in struct pr_usrreqs, so that protocols can just reference then directly
712  * without the need for a wrapper function.  The socket must have a valid
713  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
714  * except through a kernel programming error, so it is acceptable to panic
715  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
716  * because there actually /is/ a programming error somewhere... XXX)
717  */
718 void
719 in6_setsockaddr_dispatch(netmsg_t msg)
720 {
721         int error;
722
723         error = in6_setsockaddr(msg->sockaddr.base.nm_so, msg->sockaddr.nm_nam);
724         lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
725 }
726
727 int
728 in6_setsockaddr(struct socket *so, struct sockaddr **nam)
729 {
730         struct inpcb *inp;
731         struct sockaddr_in6 *sin6;
732
733         /*
734          * Do the malloc first in case it blocks.
735          */
736         MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME,
737             M_WAITOK | M_ZERO);
738         sin6->sin6_family = AF_INET6;
739         sin6->sin6_len = sizeof(*sin6);
740
741         crit_enter();
742         inp = so->so_pcb;
743         if (!inp) {
744                 crit_exit();
745                 kfree(sin6, M_SONAME);
746                 return EINVAL;
747         }
748         sin6->sin6_port = inp->inp_lport;
749         sin6->sin6_addr = inp->in6p_laddr;
750         crit_exit();
751         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
752                 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
753         else
754                 sin6->sin6_scope_id = 0;        /*XXX*/
755         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
756                 sin6->sin6_addr.s6_addr16[1] = 0;
757
758         *nam = (struct sockaddr *)sin6;
759         return 0;
760 }
761
762 void
763 in6_setpeeraddr_dispatch(netmsg_t msg)
764 {
765         int error;
766
767         error = in6_setpeeraddr(msg->peeraddr.base.nm_so, msg->peeraddr.nm_nam);
768         lwkt_replymsg(&msg->peeraddr.base.lmsg, error);
769 }
770
771 int
772 in6_setpeeraddr(struct socket *so, struct sockaddr **nam)
773 {
774         struct inpcb *inp;
775         struct sockaddr_in6 *sin6;
776
777         /*
778          * Do the malloc first in case it blocks.
779          */
780         MALLOC(sin6, struct sockaddr_in6 *, sizeof(*sin6), M_SONAME,
781             M_WAITOK | M_ZERO);
782         sin6->sin6_family = AF_INET6;
783         sin6->sin6_len = sizeof(struct sockaddr_in6);
784
785         crit_enter();
786         inp = so->so_pcb;
787         if (!inp) {
788                 crit_exit();
789                 kfree(sin6, M_SONAME);
790                 return EINVAL;
791         }
792         sin6->sin6_port = inp->inp_fport;
793         sin6->sin6_addr = inp->in6p_faddr;
794         crit_exit();
795         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
796                 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
797         else
798                 sin6->sin6_scope_id = 0;        /*XXX*/
799         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
800                 sin6->sin6_addr.s6_addr16[1] = 0;
801
802         *nam = (struct sockaddr *)sin6;
803         return 0;
804 }
805
806 void
807 in6_mapped_sockaddr_dispatch(netmsg_t msg)
808 {
809         int error;
810
811         error = in6_mapped_sockaddr(msg->sockaddr.base.nm_so,
812                                     msg->sockaddr.nm_nam);
813         lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
814 }
815
816 int
817 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
818 {
819         struct  inpcb *inp = so->so_pcb;
820         int     error;
821
822         if (inp == NULL)
823                 return EINVAL;
824         if (inp->inp_vflag & INP_IPV4) {
825                 error = in_setsockaddr(so, nam);
826                 if (error == 0)
827                         in6_sin_2_v4mapsin6_in_sock(nam);
828         } else
829         /* scope issues will be handled in in6_setsockaddr(). */
830         error = in6_setsockaddr(so, nam);
831
832         return error;
833 }
834
835 int
836 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
837 {
838         struct  inpcb *inp = so->so_pcb;
839         int     error;
840
841         if (inp == NULL)
842                 return EINVAL;
843         if (inp->inp_vflag & INP_IPV4) {
844                 error = in_setpeeraddr(so, nam);
845                 if (error == 0)
846                         in6_sin_2_v4mapsin6_in_sock(nam);
847         } else
848         /* scope issues will be handled in in6_setpeeraddr(). */
849         error = in6_setpeeraddr(so, nam);
850
851         return error;
852 }
853
854 void
855 in6_mapped_peeraddr_dispatch(netmsg_t msg)
856 {
857         int error;
858
859         error = in6_mapped_peeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
860         lwkt_replymsg(&msg->base.lmsg, error);
861 }
862
863 /*
864  * Pass some notification to all connections of a protocol
865  * associated with address dst.  The local address and/or port numbers
866  * may be specified to limit the search.  The "usual action" will be
867  * taken, depending on the ctlinput cmd.  The caller must filter any
868  * cmds that are uninteresting (e.g., no error in the map).
869  * Call the protocol specific routine (if any) to report
870  * any errors for each matching socket.
871  *
872  * Must be called under crit_enter().
873  */
874 void
875 in6_pcbnotify(struct inpcbhead *head, struct sockaddr *dst, in_port_t fport,
876               const struct sockaddr *src, in_port_t lport, int cmd, int arg,
877               void (*notify) (struct inpcb *, int))
878 {
879         struct inpcb *inp, *ninp;
880         struct sockaddr_in6 sa6_src, *sa6_dst;
881         u_int32_t flowinfo;
882
883         if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
884                 return;
885
886         sa6_dst = (struct sockaddr_in6 *)dst;
887         if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
888                 return;
889
890         /*
891          * note that src can be NULL when we get notify by local fragmentation.
892          */
893         sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
894         flowinfo = sa6_src.sin6_flowinfo;
895
896         /*
897          * Redirects go to all references to the destination,
898          * and use in6_rtchange to invalidate the route cache.
899          * Dead host indications: also use in6_rtchange to invalidate
900          * the cache, and deliver the error to all the sockets.
901          * Otherwise, if we have knowledge of the local port and address,
902          * deliver only to that socket.
903          */
904         if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
905                 fport = 0;
906                 lport = 0;
907                 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
908
909                 if (cmd != PRC_HOSTDEAD)
910                         notify = in6_rtchange;
911         }
912         if (cmd != PRC_MSGSIZE)
913                 arg = inet6ctlerrmap[cmd];
914         crit_enter();
915         for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
916                 ninp = LIST_NEXT(inp, inp_list);
917
918                 if (inp->inp_flags & INP_PLACEMARKER)
919                         continue;
920
921                 if ((inp->inp_vflag & INP_IPV6) == 0)
922                         continue;
923                 /*
924                  * If the error designates a new path MTU for a destination
925                  * and the application (associated with this socket) wanted to
926                  * know the value, notify. Note that we notify for all
927                  * disconnected sockets if the corresponding application
928                  * wanted. This is because some UDP applications keep sending
929                  * sockets disconnected.
930                  * XXX: should we avoid to notify the value to TCP sockets?
931                  */
932                 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
933                     (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
934                      IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
935                         ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst, &arg);
936                 }
937
938                 /*
939                  * Detect if we should notify the error. If no source and
940                  * destination ports are specifed, but non-zero flowinfo and
941                  * local address match, notify the error. This is the case
942                  * when the error is delivered with an encrypted buffer
943                  * by ESP. Otherwise, just compare addresses and ports
944                  * as usual.
945                  */
946                 if (lport == 0 && fport == 0 && flowinfo &&
947                     inp->inp_socket != NULL &&
948                     flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
949                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
950                         goto do_notify;
951                 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
952                                              &sa6_dst->sin6_addr) ||
953                          inp->inp_socket == 0 ||
954                          (lport && inp->inp_lport != lport) ||
955                          (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
956                           !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
957                                               &sa6_src.sin6_addr)) ||
958                          (fport && inp->inp_fport != fport))
959                         continue;
960
961 do_notify:
962                 if (notify)
963                         (*notify)(inp, arg);
964         }
965         crit_exit();
966 }
967
968 /*
969  * Lookup a PCB based on the local address and port.
970  */
971 struct inpcb *
972 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
973                     u_int lport_arg, int wild_okay, struct ucred *cred)
974 {
975         struct inpcb *inp;
976         int matchwild = 3, wildcard;
977         u_short lport = lport_arg;
978         struct inpcbporthead *porthash;
979         struct inpcbport *phd;
980         struct inpcb *match = NULL;
981
982         /*
983          * If the porthashbase is shared across several cpus we need
984          * to lock.
985          */
986         if (pcbinfo->porttoken)
987                 lwkt_gettoken(pcbinfo->porttoken);
988
989         /*
990          * Best fit PCB lookup.
991          *
992          * First see if this local port is in use by looking on the
993          * port hash list.
994          */
995         porthash = &pcbinfo->porthashbase[
996                                 INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
997         LIST_FOREACH(phd, porthash, phd_hash) {
998                 if (phd->phd_port == lport)
999                         break;
1000         }
1001
1002         if (phd != NULL) {
1003                 /*
1004                  * Port is in use by one or more PCBs. Look for best
1005                  * fit.
1006                  */
1007                 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1008                         wildcard = 0;
1009                         if ((inp->inp_vflag & INP_IPV6) == 0)
1010                                 continue;
1011                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
1012                                 wildcard++;
1013                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1014                                 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
1015                                         wildcard++;
1016                                 else if (!IN6_ARE_ADDR_EQUAL(
1017                                         &inp->in6p_laddr, laddr))
1018                                         continue;
1019                         } else {
1020                                 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
1021                                         wildcard++;
1022                         }
1023                         if (wildcard && !wild_okay)
1024                                 continue;
1025                         if (wildcard < matchwild &&
1026                             (cred == NULL ||
1027                              cred->cr_prison == 
1028                                         inp->inp_socket->so_cred->cr_prison)) {
1029                                 match = inp;
1030                                 matchwild = wildcard;
1031                                 if (wildcard == 0)
1032                                         break;
1033                                 else
1034                                         matchwild = wildcard;
1035                         }
1036                 }
1037         }
1038         if (pcbinfo->porttoken)
1039                 lwkt_reltoken(pcbinfo->porttoken);
1040
1041         return (match);
1042 }
1043
1044 void
1045 in6_pcbpurgeif0(struct in6pcb *head, struct ifnet *ifp)
1046 {
1047         struct in6pcb *in6p;
1048         struct ip6_moptions *im6o;
1049         struct in6_multi_mship *imm, *nimm;
1050
1051         for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
1052                 if (in6p->in6p_flags & INP_PLACEMARKER)
1053                         continue;
1054                 im6o = in6p->in6p_moptions;
1055                 if ((in6p->inp_vflag & INP_IPV6) &&
1056                     im6o) {
1057                         /*
1058                          * Unselect the outgoing interface if it is being
1059                          * detached.
1060                          */
1061                         if (im6o->im6o_multicast_ifp == ifp)
1062                                 im6o->im6o_multicast_ifp = NULL;
1063
1064                         /*
1065                          * Drop multicast group membership if we joined
1066                          * through the interface being detached.
1067                          * XXX controversial - is it really legal for kernel
1068                          * to force this?
1069                          */
1070                         for (imm = im6o->im6o_memberships.lh_first;
1071                              imm != NULL; imm = nimm) {
1072                                 nimm = imm->i6mm_chain.le_next;
1073                                 if (imm->i6mm_maddr->in6m_ifp == ifp) {
1074                                         LIST_REMOVE(imm, i6mm_chain);
1075                                         in6_delmulti(imm->i6mm_maddr);
1076                                         kfree(imm, M_IPMADDR);
1077                                 }
1078                         }
1079                 }
1080         }
1081 }
1082
1083 /*
1084  * Check for alternatives when higher level complains
1085  * about service problems.  For now, invalidate cached
1086  * routing information.  If the route was created dynamically
1087  * (by a redirect), time to try a default gateway again.
1088  */
1089 void
1090 in6_losing(struct inpcb *in6p)
1091 {
1092         struct rtentry *rt;
1093         struct rt_addrinfo info;
1094
1095         if ((rt = in6p->in6p_route.ro_rt) != NULL) {
1096                 bzero((caddr_t)&info, sizeof(info));
1097                 info.rti_flags = rt->rt_flags;
1098                 info.rti_info[RTAX_DST] = rt_key(rt);
1099                 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1100                 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1101                 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
1102                 if (rt->rt_flags & RTF_DYNAMIC)
1103                         rtrequest1_global(RTM_DELETE, &info, NULL, NULL);
1104                 in6p->in6p_route.ro_rt = NULL;
1105                 rtfree(rt);
1106                 /*
1107                  * A new route can be allocated
1108                  * the next time output is attempted.
1109                  */
1110         }
1111 }
1112
1113 /*
1114  * After a routing change, flush old routing
1115  * and allocate a (hopefully) better one.
1116  */
1117 void
1118 in6_rtchange(struct inpcb *inp, int error)
1119 {
1120         if (inp->in6p_route.ro_rt) {
1121                 rtfree(inp->in6p_route.ro_rt);
1122                 inp->in6p_route.ro_rt = 0;
1123                 /*
1124                  * A new route can be allocated the next time
1125                  * output is attempted.
1126                  */
1127         }
1128 }
1129
1130 /*
1131  * Lookup PCB in hash list.
1132  */
1133 struct inpcb *
1134 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1135                    u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1136                    int wildcard, struct ifnet *ifp)
1137 {
1138         struct inpcbhead *head;
1139         struct inpcb *inp;
1140         struct inpcb *jinp = NULL;
1141         u_short fport = fport_arg, lport = lport_arg;
1142         int faith;
1143
1144         if (faithprefix_p != NULL)
1145                 faith = (*faithprefix_p)(laddr);
1146         else
1147                 faith = 0;
1148
1149         /*
1150          * First look for an exact match.
1151          */
1152         head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr->s6_addr32[3] /* XXX */,
1153                                               fport,
1154                                               laddr->s6_addr32[3], /* XXX JH */
1155                                               lport,
1156                                               pcbinfo->hashmask)];
1157         LIST_FOREACH(inp, head, inp_hash) {
1158                 if ((inp->inp_vflag & INP_IPV6) == 0)
1159                         continue;
1160                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1161                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1162                     inp->inp_fport == fport &&
1163                     inp->inp_lport == lport) {
1164                         /*
1165                          * Found.
1166                          */
1167                         if (inp->inp_socket == NULL ||
1168                         inp->inp_socket->so_cred->cr_prison == NULL) {
1169                                 return (inp);
1170                         } else {
1171                                 if  (jinp == NULL)
1172                                         jinp = inp;
1173                         }
1174                 }
1175         }
1176         if (jinp != NULL)
1177                 return(jinp);
1178         if (wildcard) {
1179                 struct inpcontainerhead *chead;
1180                 struct inpcontainer *ic;
1181                 struct inpcb *local_wild = NULL;
1182                 struct inpcb *jinp_wild = NULL;
1183                 struct sockaddr_in6 jsin6;
1184                 struct ucred *cred;
1185
1186                 /*
1187                  * Order of socket selection:
1188                  * 1. non-jailed, non-wild.
1189                  * 2. non-jailed, wild.
1190                  * 3. jailed, non-wild.
1191                  * 4. jailed, wild.
1192                  */
1193                 jsin6.sin6_family = AF_INET6;
1194                 chead = &pcbinfo->wildcardhashbase[INP_PCBWILDCARDHASH(lport,
1195                     pcbinfo->wildcardhashmask)];
1196                 LIST_FOREACH(ic, chead, ic_list) {
1197                         inp = ic->ic_inp;
1198
1199                         if (!(inp->inp_vflag & INP_IPV6))
1200                                 continue;
1201                         if (inp->inp_socket != NULL)
1202                                 cred = inp->inp_socket->so_cred;
1203                         else
1204                                 cred = NULL;
1205
1206                         if (cred != NULL && jailed(cred)) {
1207                                 if (jinp != NULL) {
1208                                         continue;
1209                                 } else {
1210                                         jsin6.sin6_addr = *laddr;
1211                                         if (!jailed_ip(cred->cr_prison,
1212                                             (struct sockaddr *)&jsin6))
1213                                                 continue;
1214                                 }
1215                         }
1216                         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1217                             inp->inp_lport == lport) {
1218                                 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1219                                         continue;
1220                                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
1221                                                        laddr)) {
1222                                         if (cred != NULL && jailed(cred))
1223                                                 jinp = inp;
1224                                         else
1225                                                 return (inp);
1226                                 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1227                                         if (cred != NULL && jailed(cred))
1228                                                 jinp_wild = inp;
1229                                         else
1230                                                 local_wild = inp;
1231                                 }
1232                         }
1233                 }
1234                 if (local_wild != NULL)
1235                         return (local_wild);
1236                 if (jinp != NULL)
1237                         return (jinp);
1238                 return (jinp_wild);
1239         }
1240
1241         /*
1242          * Not found.
1243          */
1244         return (NULL);
1245 }
1246
1247 void
1248 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1249 {
1250         struct ip6_hdr *ip;
1251
1252         ip = mtod(m, struct ip6_hdr *);
1253         bzero(sin6, sizeof(*sin6));
1254         sin6->sin6_len = sizeof(*sin6);
1255         sin6->sin6_family = AF_INET6;
1256         sin6->sin6_addr = ip->ip6_src;
1257         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1258                 sin6->sin6_addr.s6_addr16[1] = 0;
1259         sin6->sin6_scope_id =
1260                 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1261                 ? m->m_pkthdr.rcvif->if_index : 0;
1262
1263         return;
1264 }