]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/netinet6/in6_pcb.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / netinet6 / in6_pcb.c
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * Copyright (c) 2010-2011 Juniper Networks, Inc.
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Robert N. M. Watson under
7  * contract to Juniper Networks, Inc.
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  *      $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
34  */
35
36 /*-
37  * Copyright (c) 1982, 1986, 1991, 1993
38  *      The Regents of the University of California.  All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 4. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  *      @(#)in_pcb.c    8.2 (Berkeley) 1/4/94
65  */
66
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
69
70 #include "opt_inet.h"
71 #include "opt_inet6.h"
72 #include "opt_ipsec.h"
73 #include "opt_pcbgroup.h"
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/malloc.h>
78 #include <sys/mbuf.h>
79 #include <sys/domain.h>
80 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/sockio.h>
84 #include <sys/errno.h>
85 #include <sys/time.h>
86 #include <sys/priv.h>
87 #include <sys/proc.h>
88 #include <sys/jail.h>
89
90 #include <vm/uma.h>
91
92 #include <net/if.h>
93 #include <net/if_types.h>
94 #include <net/route.h>
95
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/tcp_var.h>
100 #include <netinet/ip6.h>
101 #include <netinet/ip_var.h>
102
103 #include <netinet6/ip6_var.h>
104 #include <netinet6/nd6.h>
105 #include <netinet/in_pcb.h>
106 #include <netinet6/in6_pcb.h>
107 #include <netinet6/scope6_var.h>
108
109 int
110 in6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
111     struct ucred *cred)
112 {
113         struct socket *so = inp->inp_socket;
114         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
115         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
116         u_short lport = 0;
117         int error, lookupflags = 0;
118         int reuseport = (so->so_options & SO_REUSEPORT);
119
120         INP_WLOCK_ASSERT(inp);
121         INP_HASH_WLOCK_ASSERT(pcbinfo);
122
123         if (TAILQ_EMPTY(&V_in6_ifaddrhead))     /* XXX broken! */
124                 return (EADDRNOTAVAIL);
125         if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
126                 return (EINVAL);
127         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
128                 lookupflags = INPLOOKUP_WILDCARD;
129         if (nam == NULL) {
130                 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
131                     ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
132                         return (error);
133         } else {
134                 sin6 = (struct sockaddr_in6 *)nam;
135                 if (nam->sa_len != sizeof(*sin6))
136                         return (EINVAL);
137                 /*
138                  * family check.
139                  */
140                 if (nam->sa_family != AF_INET6)
141                         return (EAFNOSUPPORT);
142
143                 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
144                         return(error);
145
146                 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
147                     ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
148                         return (error);
149
150                 lport = sin6->sin6_port;
151                 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
152                         /*
153                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
154                          * allow compepte duplication of binding if
155                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
156                          * and a multicast address is bound on both
157                          * new and duplicated sockets.
158                          */
159                         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
160                                 reuseport = SO_REUSEADDR|SO_REUSEPORT;
161                 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
162                         struct ifaddr *ifa;
163
164                         sin6->sin6_port = 0;            /* yech... */
165                         if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
166                             NULL &&
167                             (inp->inp_flags & INP_BINDANY) == 0) {
168                                 return (EADDRNOTAVAIL);
169                         }
170
171                         /*
172                          * XXX: bind to an anycast address might accidentally
173                          * cause sending a packet with anycast source address.
174                          * We should allow to bind to a deprecated address, since
175                          * the application dares to use it.
176                          */
177                         if (ifa != NULL &&
178                             ((struct in6_ifaddr *)ifa)->ia6_flags &
179                             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
180                                 ifa_free(ifa);
181                                 return (EADDRNOTAVAIL);
182                         }
183                         if (ifa != NULL)
184                                 ifa_free(ifa);
185                 }
186                 if (lport) {
187                         struct inpcb *t;
188                         struct tcptw *tw;
189
190                         /* GROSS */
191                         if (ntohs(lport) <= V_ipport_reservedhigh &&
192                             ntohs(lport) >= V_ipport_reservedlow &&
193                             priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
194                             0))
195                                 return (EACCES);
196                         if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
197                             priv_check_cred(inp->inp_cred,
198                             PRIV_NETINET_REUSEPORT, 0) != 0) {
199                                 t = in6_pcblookup_local(pcbinfo,
200                                     &sin6->sin6_addr, lport,
201                                     INPLOOKUP_WILDCARD, cred);
202                                 if (t &&
203                                     ((t->inp_flags & INP_TIMEWAIT) == 0) &&
204                                     (so->so_type != SOCK_STREAM ||
205                                      IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
206                                     (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
207                                      !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
208                                      (t->inp_flags2 & INP_REUSEPORT) == 0) &&
209                                     (inp->inp_cred->cr_uid !=
210                                      t->inp_cred->cr_uid))
211                                         return (EADDRINUSE);
212 #ifdef INET
213                                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
214                                     IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
215                                         struct sockaddr_in sin;
216
217                                         in6_sin6_2_sin(&sin, sin6);
218                                         t = in_pcblookup_local(pcbinfo,
219                                             sin.sin_addr, lport,
220                                             INPLOOKUP_WILDCARD, cred);
221                                         if (t &&
222                                             ((t->inp_flags &
223                                               INP_TIMEWAIT) == 0) &&
224                                             (so->so_type != SOCK_STREAM ||
225                                              ntohl(t->inp_faddr.s_addr) ==
226                                               INADDR_ANY) &&
227                                             (inp->inp_cred->cr_uid !=
228                                              t->inp_cred->cr_uid))
229                                                 return (EADDRINUSE);
230                                 }
231 #endif
232                         }
233                         t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
234                             lport, lookupflags, cred);
235                         if (t && (t->inp_flags & INP_TIMEWAIT)) {
236                                 /*
237                                  * XXXRW: If an incpb has had its timewait
238                                  * state recycled, we treat the address as
239                                  * being in use (for now).  This is better
240                                  * than a panic, but not desirable.
241                                  */
242                                 tw = intotw(t);
243                                 if (tw == NULL ||
244                                     (reuseport & tw->tw_so_options) == 0)
245                                         return (EADDRINUSE);
246                         } else if (t && (reuseport & inp_so_options(t)) == 0) {
247                                 return (EADDRINUSE);
248                         }
249 #ifdef INET
250                         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
251                             IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
252                                 struct sockaddr_in sin;
253
254                                 in6_sin6_2_sin(&sin, sin6);
255                                 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
256                                     lport, lookupflags, cred);
257                                 if (t && t->inp_flags & INP_TIMEWAIT) {
258                                         tw = intotw(t);
259                                         if (tw == NULL)
260                                                 return (EADDRINUSE);
261                                         if ((reuseport & tw->tw_so_options) == 0
262                                             && (ntohl(t->inp_laddr.s_addr) !=
263                                              INADDR_ANY || ((inp->inp_vflag &
264                                              INP_IPV6PROTO) ==
265                                              (t->inp_vflag & INP_IPV6PROTO))))
266                                                 return (EADDRINUSE);
267                                 } else if (t &&
268                                     (reuseport & inp_so_options(t)) == 0 &&
269                                     (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
270                                     (t->inp_vflag & INP_IPV6PROTO) != 0))
271                                         return (EADDRINUSE);
272                         }
273 #endif
274                 }
275                 inp->in6p_laddr = sin6->sin6_addr;
276         }
277         if (lport == 0) {
278                 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
279                         /* Undo an address bind that may have occurred. */
280                         inp->in6p_laddr = in6addr_any;
281                         return (error);
282                 }
283         } else {
284                 inp->inp_lport = lport;
285                 if (in_pcbinshash(inp) != 0) {
286                         inp->in6p_laddr = in6addr_any;
287                         inp->inp_lport = 0;
288                         return (EAGAIN);
289                 }
290         }
291         return (0);
292 }
293
294 /*
295  *   Transform old in6_pcbconnect() into an inner subroutine for new
296  *   in6_pcbconnect(): Do some validity-checking on the remote
297  *   address (in mbuf 'nam') and then determine local host address
298  *   (i.e., which interface) to use to access that remote host.
299  *
300  *   This preserves definition of in6_pcbconnect(), while supporting a
301  *   slightly different version for T/TCP.  (This is more than
302  *   a bit of a kludge, but cleaning up the internal interfaces would
303  *   have forced minor changes in every protocol).
304  */
305 int
306 in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
307     struct in6_addr *plocal_addr6)
308 {
309         register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
310         int error = 0;
311         struct ifnet *ifp = NULL;
312         int scope_ambiguous = 0;
313         struct in6_addr in6a;
314
315         INP_WLOCK_ASSERT(inp);
316         INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);        /* XXXRW: why? */
317
318         if (nam->sa_len != sizeof (*sin6))
319                 return (EINVAL);
320         if (sin6->sin6_family != AF_INET6)
321                 return (EAFNOSUPPORT);
322         if (sin6->sin6_port == 0)
323                 return (EADDRNOTAVAIL);
324
325         if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
326                 scope_ambiguous = 1;
327         if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
328                 return(error);
329
330         if (!TAILQ_EMPTY(&V_in6_ifaddrhead)) {
331                 /*
332                  * If the destination address is UNSPECIFIED addr,
333                  * use the loopback addr, e.g ::1.
334                  */
335                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
336                         sin6->sin6_addr = in6addr_loopback;
337         }
338         if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
339                 return (error);
340
341         error = in6_selectsrc(sin6, inp->in6p_outputopts,
342             inp, NULL, inp->inp_cred, &ifp, &in6a);
343         if (error)
344                 return (error);
345
346         if (ifp && scope_ambiguous &&
347             (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
348                 return(error);
349         }
350
351         /*
352          * Do not update this earlier, in case we return with an error.
353          *
354          * XXX: this in6_selectsrc result might replace the bound local
355          * address with the address specified by setsockopt(IPV6_PKTINFO).
356          * Is it the intended behavior?
357          */
358         *plocal_addr6 = in6a;
359
360         /*
361          * Don't do pcblookup call here; return interface in
362          * plocal_addr6
363          * and exit to caller, that will do the lookup.
364          */
365
366         return (0);
367 }
368
369 /*
370  * Outer subroutine:
371  * Connect from a socket to a specified address.
372  * Both address and port must be specified in argument sin.
373  * If don't have a local address for this socket yet,
374  * then pick one.
375  */
376 int
377 in6_pcbconnect_mbuf(register struct inpcb *inp, struct sockaddr *nam,
378     struct ucred *cred, struct mbuf *m)
379 {
380         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
381         register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
382         struct in6_addr addr6;
383         int error;
384
385         INP_WLOCK_ASSERT(inp);
386         INP_HASH_WLOCK_ASSERT(pcbinfo);
387
388         /*
389          * Call inner routine, to assign local interface address.
390          * in6_pcbladdr() may automatically fill in sin6_scope_id.
391          */
392         if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
393                 return (error);
394
395         if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
396                                sin6->sin6_port,
397                               IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
398                               ? &addr6 : &inp->in6p_laddr,
399                               inp->inp_lport, 0, NULL) != NULL) {
400                 return (EADDRINUSE);
401         }
402         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
403                 if (inp->inp_lport == 0) {
404                         error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
405                         if (error)
406                                 return (error);
407                 }
408                 inp->in6p_laddr = addr6;
409         }
410         inp->in6p_faddr = sin6->sin6_addr;
411         inp->inp_fport = sin6->sin6_port;
412         /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
413         inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
414         if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
415                 inp->inp_flow |=
416                     (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
417
418         in_pcbrehash_mbuf(inp, m);
419
420         return (0);
421 }
422
423 int
424 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
425 {
426
427         return (in6_pcbconnect_mbuf(inp, nam, cred, NULL));
428 }
429
430 void
431 in6_pcbdisconnect(struct inpcb *inp)
432 {
433
434         INP_WLOCK_ASSERT(inp);
435         INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
436
437         bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
438         inp->inp_fport = 0;
439         /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
440         inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
441         in_pcbrehash(inp);
442 }
443
444 struct sockaddr *
445 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
446 {
447         struct sockaddr_in6 *sin6;
448
449         sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
450         bzero(sin6, sizeof *sin6);
451         sin6->sin6_family = AF_INET6;
452         sin6->sin6_len = sizeof(*sin6);
453         sin6->sin6_port = port;
454         sin6->sin6_addr = *addr_p;
455         (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
456
457         return (struct sockaddr *)sin6;
458 }
459
460 struct sockaddr *
461 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
462 {
463         struct sockaddr_in sin;
464         struct sockaddr_in6 *sin6_p;
465
466         bzero(&sin, sizeof sin);
467         sin.sin_family = AF_INET;
468         sin.sin_len = sizeof(sin);
469         sin.sin_port = port;
470         sin.sin_addr = *addr_p;
471
472         sin6_p = malloc(sizeof *sin6_p, M_SONAME,
473                 M_WAITOK);
474         in6_sin_2_v4mapsin6(&sin, sin6_p);
475
476         return (struct sockaddr *)sin6_p;
477 }
478
479 int
480 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
481 {
482         register struct inpcb *inp;
483         struct in6_addr addr;
484         in_port_t port;
485
486         inp = sotoinpcb(so);
487         KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
488
489         INP_RLOCK(inp);
490         port = inp->inp_lport;
491         addr = inp->in6p_laddr;
492         INP_RUNLOCK(inp);
493
494         *nam = in6_sockaddr(port, &addr);
495         return 0;
496 }
497
498 int
499 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
500 {
501         struct inpcb *inp;
502         struct in6_addr addr;
503         in_port_t port;
504
505         inp = sotoinpcb(so);
506         KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
507
508         INP_RLOCK(inp);
509         port = inp->inp_fport;
510         addr = inp->in6p_faddr;
511         INP_RUNLOCK(inp);
512
513         *nam = in6_sockaddr(port, &addr);
514         return 0;
515 }
516
517 int
518 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
519 {
520         struct  inpcb *inp;
521         int     error;
522
523         inp = sotoinpcb(so);
524         KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
525
526 #ifdef INET
527         if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
528                 error = in_getsockaddr(so, nam);
529                 if (error == 0)
530                         in6_sin_2_v4mapsin6_in_sock(nam);
531         } else
532 #endif
533         {
534                 /* scope issues will be handled in in6_getsockaddr(). */
535                 error = in6_getsockaddr(so, nam);
536         }
537
538         return error;
539 }
540
541 int
542 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
543 {
544         struct  inpcb *inp;
545         int     error;
546
547         inp = sotoinpcb(so);
548         KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
549
550 #ifdef INET
551         if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
552                 error = in_getpeeraddr(so, nam);
553                 if (error == 0)
554                         in6_sin_2_v4mapsin6_in_sock(nam);
555         } else
556 #endif
557         /* scope issues will be handled in in6_getpeeraddr(). */
558         error = in6_getpeeraddr(so, nam);
559
560         return error;
561 }
562
563 /*
564  * Pass some notification to all connections of a protocol
565  * associated with address dst.  The local address and/or port numbers
566  * may be specified to limit the search.  The "usual action" will be
567  * taken, depending on the ctlinput cmd.  The caller must filter any
568  * cmds that are uninteresting (e.g., no error in the map).
569  * Call the protocol specific routine (if any) to report
570  * any errors for each matching socket.
571  */
572 void
573 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
574     u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
575     int cmd, void *cmdarg,
576     struct inpcb *(*notify)(struct inpcb *, int))
577 {
578         struct inpcb *inp, *inp_temp;
579         struct sockaddr_in6 sa6_src, *sa6_dst;
580         u_short fport = fport_arg, lport = lport_arg;
581         u_int32_t flowinfo;
582         int errno;
583
584         if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
585                 return;
586
587         sa6_dst = (struct sockaddr_in6 *)dst;
588         if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
589                 return;
590
591         /*
592          * note that src can be NULL when we get notify by local fragmentation.
593          */
594         sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
595         flowinfo = sa6_src.sin6_flowinfo;
596
597         /*
598          * Redirects go to all references to the destination,
599          * and use in6_rtchange to invalidate the route cache.
600          * Dead host indications: also use in6_rtchange to invalidate
601          * the cache, and deliver the error to all the sockets.
602          * Otherwise, if we have knowledge of the local port and address,
603          * deliver only to that socket.
604          */
605         if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
606                 fport = 0;
607                 lport = 0;
608                 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
609
610                 if (cmd != PRC_HOSTDEAD)
611                         notify = in6_rtchange;
612         }
613         errno = inet6ctlerrmap[cmd];
614         INP_INFO_WLOCK(pcbinfo);
615         LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
616                 INP_WLOCK(inp);
617                 if ((inp->inp_vflag & INP_IPV6) == 0) {
618                         INP_WUNLOCK(inp);
619                         continue;
620                 }
621
622                 /*
623                  * If the error designates a new path MTU for a destination
624                  * and the application (associated with this socket) wanted to
625                  * know the value, notify.
626                  * XXX: should we avoid to notify the value to TCP sockets?
627                  */
628                 if (cmd == PRC_MSGSIZE && cmdarg != NULL)
629                         ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
630                                         *(u_int32_t *)cmdarg);
631
632                 /*
633                  * Detect if we should notify the error. If no source and
634                  * destination ports are specifed, but non-zero flowinfo and
635                  * local address match, notify the error. This is the case
636                  * when the error is delivered with an encrypted buffer
637                  * by ESP. Otherwise, just compare addresses and ports
638                  * as usual.
639                  */
640                 if (lport == 0 && fport == 0 && flowinfo &&
641                     inp->inp_socket != NULL &&
642                     flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
643                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
644                         goto do_notify;
645                 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
646                                              &sa6_dst->sin6_addr) ||
647                          inp->inp_socket == 0 ||
648                          (lport && inp->inp_lport != lport) ||
649                          (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
650                           !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
651                                               &sa6_src.sin6_addr)) ||
652                          (fport && inp->inp_fport != fport)) {
653                         INP_WUNLOCK(inp);
654                         continue;
655                 }
656
657           do_notify:
658                 if (notify) {
659                         if ((*notify)(inp, errno))
660                                 INP_WUNLOCK(inp);
661                 } else
662                         INP_WUNLOCK(inp);
663         }
664         INP_INFO_WUNLOCK(pcbinfo);
665 }
666
667 /*
668  * Lookup a PCB based on the local address and port.  Caller must hold the
669  * hash lock.  No inpcb locks or references are acquired.
670  */
671 struct inpcb *
672 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
673     u_short lport, int lookupflags, struct ucred *cred)
674 {
675         register struct inpcb *inp;
676         int matchwild = 3, wildcard;
677
678         KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
679             ("%s: invalid lookup flags %d", __func__, lookupflags));
680
681         INP_HASH_WLOCK_ASSERT(pcbinfo);
682
683         if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
684                 struct inpcbhead *head;
685                 /*
686                  * Look for an unconnected (wildcard foreign addr) PCB that
687                  * matches the local address and port we're looking for.
688                  */
689                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
690                     0, pcbinfo->ipi_hashmask)];
691                 LIST_FOREACH(inp, head, inp_hash) {
692                         /* XXX inp locking */
693                         if ((inp->inp_vflag & INP_IPV6) == 0)
694                                 continue;
695                         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
696                             IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
697                             inp->inp_lport == lport) {
698                                 /* Found. */
699                                 if (cred == NULL ||
700                                     prison_equal_ip6(cred->cr_prison,
701                                         inp->inp_cred->cr_prison))
702                                         return (inp);
703                         }
704                 }
705                 /*
706                  * Not found.
707                  */
708                 return (NULL);
709         } else {
710                 struct inpcbporthead *porthash;
711                 struct inpcbport *phd;
712                 struct inpcb *match = NULL;
713                 /*
714                  * Best fit PCB lookup.
715                  *
716                  * First see if this local port is in use by looking on the
717                  * port hash list.
718                  */
719                 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
720                     pcbinfo->ipi_porthashmask)];
721                 LIST_FOREACH(phd, porthash, phd_hash) {
722                         if (phd->phd_port == lport)
723                                 break;
724                 }
725                 if (phd != NULL) {
726                         /*
727                          * Port is in use by one or more PCBs. Look for best
728                          * fit.
729                          */
730                         LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
731                                 wildcard = 0;
732                                 if (cred != NULL &&
733                                     !prison_equal_ip6(cred->cr_prison,
734                                         inp->inp_cred->cr_prison))
735                                         continue;
736                                 /* XXX inp locking */
737                                 if ((inp->inp_vflag & INP_IPV6) == 0)
738                                         continue;
739                                 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
740                                         wildcard++;
741                                 if (!IN6_IS_ADDR_UNSPECIFIED(
742                                         &inp->in6p_laddr)) {
743                                         if (IN6_IS_ADDR_UNSPECIFIED(laddr))
744                                                 wildcard++;
745                                         else if (!IN6_ARE_ADDR_EQUAL(
746                                             &inp->in6p_laddr, laddr))
747                                                 continue;
748                                 } else {
749                                         if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
750                                                 wildcard++;
751                                 }
752                                 if (wildcard < matchwild) {
753                                         match = inp;
754                                         matchwild = wildcard;
755                                         if (matchwild == 0)
756                                                 break;
757                                 }
758                         }
759                 }
760                 return (match);
761         }
762 }
763
764 void
765 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
766 {
767         struct inpcb *in6p;
768         struct ip6_moptions *im6o;
769         int i, gap;
770
771         INP_INFO_RLOCK(pcbinfo);
772         LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
773                 INP_WLOCK(in6p);
774                 im6o = in6p->in6p_moptions;
775                 if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
776                         /*
777                          * Unselect the outgoing ifp for multicast if it
778                          * is being detached.
779                          */
780                         if (im6o->im6o_multicast_ifp == ifp)
781                                 im6o->im6o_multicast_ifp = NULL;
782                         /*
783                          * Drop multicast group membership if we joined
784                          * through the interface being detached.
785                          */
786                         gap = 0;
787                         for (i = 0; i < im6o->im6o_num_memberships; i++) {
788                                 if (im6o->im6o_membership[i]->in6m_ifp ==
789                                     ifp) {
790                                         in6_mc_leave(im6o->im6o_membership[i],
791                                             NULL);
792                                         gap++;
793                                 } else if (gap != 0) {
794                                         im6o->im6o_membership[i - gap] =
795                                             im6o->im6o_membership[i];
796                                 }
797                         }
798                         im6o->im6o_num_memberships -= gap;
799                 }
800                 INP_WUNLOCK(in6p);
801         }
802         INP_INFO_RUNLOCK(pcbinfo);
803 }
804
805 /*
806  * Check for alternatives when higher level complains
807  * about service problems.  For now, invalidate cached
808  * routing information.  If the route was created dynamically
809  * (by a redirect), time to try a default gateway again.
810  */
811 void
812 in6_losing(struct inpcb *in6p)
813 {
814
815         /*
816          * We don't store route pointers in the routing table anymore
817          */
818         return;
819 }
820
821 /*
822  * After a routing change, flush old routing
823  * and allocate a (hopefully) better one.
824  */
825 struct inpcb *
826 in6_rtchange(struct inpcb *inp, int errno)
827 {
828         /*
829          * We don't store route pointers in the routing table anymore
830          */
831         return inp;
832 }
833
834 #ifdef PCBGROUP
835 /*
836  * Lookup PCB in hash list, using pcbgroup tables.
837  */
838 static struct inpcb *
839 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
840     struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
841     u_int lport_arg, int lookupflags, struct ifnet *ifp)
842 {
843         struct inpcbhead *head;
844         struct inpcb *inp, *tmpinp;
845         u_short fport = fport_arg, lport = lport_arg;
846         int faith;
847
848         if (faithprefix_p != NULL)
849                 faith = (*faithprefix_p)(laddr);
850         else
851                 faith = 0;
852
853         /*
854          * First look for an exact match.
855          */
856         tmpinp = NULL;
857         INP_GROUP_LOCK(pcbgroup);
858         head = &pcbgroup->ipg_hashbase[
859             INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
860             pcbgroup->ipg_hashmask)];
861         LIST_FOREACH(inp, head, inp_pcbgrouphash) {
862                 /* XXX inp locking */
863                 if ((inp->inp_vflag & INP_IPV6) == 0)
864                         continue;
865                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
866                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
867                     inp->inp_fport == fport &&
868                     inp->inp_lport == lport) {
869                         /*
870                          * XXX We should be able to directly return
871                          * the inp here, without any checks.
872                          * Well unless both bound with SO_REUSEPORT?
873                          */
874                         if (prison_flag(inp->inp_cred, PR_IP6))
875                                 goto found;
876                         if (tmpinp == NULL)
877                                 tmpinp = inp;
878                 }
879         }
880         if (tmpinp != NULL) {
881                 inp = tmpinp;
882                 goto found;
883         }
884
885         /*
886          * Then look for a wildcard match, if requested.
887          */
888         if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
889                 struct inpcb *local_wild = NULL, *local_exact = NULL;
890                 struct inpcb *jail_wild = NULL;
891                 int injail;
892
893                 /*
894                  * Order of socket selection - we always prefer jails.
895                  *      1. jailed, non-wild.
896                  *      2. jailed, wild.
897                  *      3. non-jailed, non-wild.
898                  *      4. non-jailed, wild.
899                  */
900                 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
901                     0, pcbinfo->ipi_wildmask)];
902                 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
903                         /* XXX inp locking */
904                         if ((inp->inp_vflag & INP_IPV6) == 0)
905                                 continue;
906
907                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
908                             inp->inp_lport != lport) {
909                                 continue;
910                         }
911
912                         /* XXX inp locking */
913                         if (faith && (inp->inp_flags & INP_FAITH) == 0)
914                                 continue;
915
916                         injail = prison_flag(inp->inp_cred, PR_IP6);
917                         if (injail) {
918                                 if (prison_check_ip6(inp->inp_cred,
919                                     laddr) != 0)
920                                         continue;
921                         } else {
922                                 if (local_exact != NULL)
923                                         continue;
924                         }
925
926                         if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
927                                 if (injail)
928                                         goto found;
929                                 else
930                                         local_exact = inp;
931                         } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
932                                 if (injail)
933                                         jail_wild = inp;
934                                 else
935                                         local_wild = inp;
936                         }
937                 } /* LIST_FOREACH */
938
939                 inp = jail_wild;
940                 if (inp == NULL)
941                         inp = jail_wild;
942                 if (inp == NULL)
943                         inp = local_exact;
944                 if (inp == NULL)
945                         inp = local_wild;
946                 if (inp != NULL)
947                         goto found;
948         } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
949         INP_GROUP_UNLOCK(pcbgroup);
950         return (NULL);
951
952 found:
953         in_pcbref(inp);
954         INP_GROUP_UNLOCK(pcbgroup);
955         if (lookupflags & INPLOOKUP_WLOCKPCB) {
956                 INP_WLOCK(inp);
957                 if (in_pcbrele_wlocked(inp))
958                         return (NULL);
959         } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
960                 INP_RLOCK(inp);
961                 if (in_pcbrele_rlocked(inp))
962                         return (NULL);
963         } else
964                 panic("%s: locking buf", __func__);
965         return (inp);
966 }
967 #endif /* PCBGROUP */
968
969 /*
970  * Lookup PCB in hash list.
971  */
972 struct inpcb *
973 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
974     u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
975     int lookupflags, struct ifnet *ifp)
976 {
977         struct inpcbhead *head;
978         struct inpcb *inp, *tmpinp;
979         u_short fport = fport_arg, lport = lport_arg;
980         int faith;
981
982         KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
983             ("%s: invalid lookup flags %d", __func__, lookupflags));
984
985         INP_HASH_LOCK_ASSERT(pcbinfo);
986
987         if (faithprefix_p != NULL)
988                 faith = (*faithprefix_p)(laddr);
989         else
990                 faith = 0;
991
992         /*
993          * First look for an exact match.
994          */
995         tmpinp = NULL;
996         head = &pcbinfo->ipi_hashbase[
997             INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
998             pcbinfo->ipi_hashmask)];
999         LIST_FOREACH(inp, head, inp_hash) {
1000                 /* XXX inp locking */
1001                 if ((inp->inp_vflag & INP_IPV6) == 0)
1002                         continue;
1003                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1004                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1005                     inp->inp_fport == fport &&
1006                     inp->inp_lport == lport) {
1007                         /*
1008                          * XXX We should be able to directly return
1009                          * the inp here, without any checks.
1010                          * Well unless both bound with SO_REUSEPORT?
1011                          */
1012                         if (prison_flag(inp->inp_cred, PR_IP6))
1013                                 return (inp);
1014                         if (tmpinp == NULL)
1015                                 tmpinp = inp;
1016                 }
1017         }
1018         if (tmpinp != NULL)
1019                 return (tmpinp);
1020
1021         /*
1022          * Then look for a wildcard match, if requested.
1023          */
1024         if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1025                 struct inpcb *local_wild = NULL, *local_exact = NULL;
1026                 struct inpcb *jail_wild = NULL;
1027                 int injail;
1028
1029                 /*
1030                  * Order of socket selection - we always prefer jails.
1031                  *      1. jailed, non-wild.
1032                  *      2. jailed, wild.
1033                  *      3. non-jailed, non-wild.
1034                  *      4. non-jailed, wild.
1035                  */
1036                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1037                     0, pcbinfo->ipi_hashmask)];
1038                 LIST_FOREACH(inp, head, inp_hash) {
1039                         /* XXX inp locking */
1040                         if ((inp->inp_vflag & INP_IPV6) == 0)
1041                                 continue;
1042
1043                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1044                             inp->inp_lport != lport) {
1045                                 continue;
1046                         }
1047
1048                         /* XXX inp locking */
1049                         if (faith && (inp->inp_flags & INP_FAITH) == 0)
1050                                 continue;
1051
1052                         injail = prison_flag(inp->inp_cred, PR_IP6);
1053                         if (injail) {
1054                                 if (prison_check_ip6(inp->inp_cred,
1055                                     laddr) != 0)
1056                                         continue;
1057                         } else {
1058                                 if (local_exact != NULL)
1059                                         continue;
1060                         }
1061
1062                         if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1063                                 if (injail)
1064                                         return (inp);
1065                                 else
1066                                         local_exact = inp;
1067                         } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1068                                 if (injail)
1069                                         jail_wild = inp;
1070                                 else
1071                                         local_wild = inp;
1072                         }
1073                 } /* LIST_FOREACH */
1074
1075                 if (jail_wild != NULL)
1076                         return (jail_wild);
1077                 if (local_exact != NULL)
1078                         return (local_exact);
1079                 if (local_wild != NULL)
1080                         return (local_wild);
1081         } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1082
1083         /*
1084          * Not found.
1085          */
1086         return (NULL);
1087 }
1088
1089 /*
1090  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
1091  * hash list lock, and will return the inpcb locked (i.e., requires
1092  * INPLOOKUP_LOCKPCB).
1093  */
1094 static struct inpcb *
1095 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1096     u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1097     struct ifnet *ifp)
1098 {
1099         struct inpcb *inp;
1100
1101         INP_HASH_RLOCK(pcbinfo);
1102         inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1103             (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1104         if (inp != NULL) {
1105                 in_pcbref(inp);
1106                 INP_HASH_RUNLOCK(pcbinfo);
1107                 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1108                         INP_WLOCK(inp);
1109                         if (in_pcbrele_wlocked(inp))
1110                                 return (NULL);
1111                 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1112                         INP_RLOCK(inp);
1113                         if (in_pcbrele_rlocked(inp))
1114                                 return (NULL);
1115                 } else
1116                         panic("%s: locking bug", __func__);
1117         } else
1118                 INP_HASH_RUNLOCK(pcbinfo);
1119         return (inp);
1120 }
1121
1122 /*
1123  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1124  * from which a pre-calculated hash value may be extracted.
1125  *
1126  * Possibly more of this logic should be in in6_pcbgroup.c.
1127  */
1128 struct inpcb *
1129 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1130     struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1131 {
1132 #if defined(PCBGROUP)
1133         struct inpcbgroup *pcbgroup;
1134 #endif
1135
1136         KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1137             ("%s: invalid lookup flags %d", __func__, lookupflags));
1138         KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1139             ("%s: LOCKPCB not set", __func__));
1140
1141 #if defined(PCBGROUP)
1142         if (in_pcbgroup_enabled(pcbinfo)) {
1143                 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1144                     fport);
1145                 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1146                     laddr, lport, lookupflags, ifp));
1147         }
1148 #endif
1149         return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1150             lookupflags, ifp));
1151 }
1152
1153 struct inpcb *
1154 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1155     u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1156     struct ifnet *ifp, struct mbuf *m)
1157 {
1158 #ifdef PCBGROUP
1159         struct inpcbgroup *pcbgroup;
1160 #endif
1161
1162         KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1163             ("%s: invalid lookup flags %d", __func__, lookupflags));
1164         KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1165             ("%s: LOCKPCB not set", __func__));
1166
1167 #ifdef PCBGROUP
1168         if (in_pcbgroup_enabled(pcbinfo)) {
1169                 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1170                     m->m_pkthdr.flowid);
1171                 if (pcbgroup != NULL)
1172                         return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1173                             fport, laddr, lport, lookupflags, ifp));
1174                 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1175                     fport);
1176                 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1177                     laddr, lport, lookupflags, ifp));
1178         }
1179 #endif
1180         return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1181             lookupflags, ifp));
1182 }
1183
1184 void
1185 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1186 {
1187         struct ip6_hdr *ip;
1188
1189         ip = mtod(m, struct ip6_hdr *);
1190         bzero(sin6, sizeof(*sin6));
1191         sin6->sin6_len = sizeof(*sin6);
1192         sin6->sin6_family = AF_INET6;
1193         sin6->sin6_addr = ip->ip6_src;
1194
1195         (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
1196
1197         return;
1198 }