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