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