]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/in6_pcb.c
Make linux_ptrace() use linux_msg() instead of printf().
[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 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)
415 {
416         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
417         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
418         struct in6_addr addr6;
419         int error;
420
421         INP_WLOCK_ASSERT(inp);
422         INP_HASH_WLOCK_ASSERT(pcbinfo);
423
424         /*
425          * Call inner routine, to assign local interface address.
426          * in6_pcbladdr() may automatically fill in sin6_scope_id.
427          */
428         if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
429                 return (error);
430
431         if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
432                                sin6->sin6_port,
433                               IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
434                               ? &addr6 : &inp->in6p_laddr,
435                               inp->inp_lport, 0, NULL) != NULL) {
436                 return (EADDRINUSE);
437         }
438         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
439                 if (inp->inp_lport == 0) {
440                         error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
441                         if (error)
442                                 return (error);
443                 }
444                 inp->in6p_laddr = addr6;
445         }
446         inp->in6p_faddr = sin6->sin6_addr;
447         inp->inp_fport = sin6->sin6_port;
448         /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
449         inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
450         if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
451                 inp->inp_flow |=
452                     (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
453
454         in_pcbrehash_mbuf(inp, m);
455
456         return (0);
457 }
458
459 int
460 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
461 {
462
463         return (in6_pcbconnect_mbuf(inp, nam, cred, NULL));
464 }
465
466 void
467 in6_pcbdisconnect(struct inpcb *inp)
468 {
469
470         INP_WLOCK_ASSERT(inp);
471         INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
472
473         bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
474         inp->inp_fport = 0;
475         /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
476         inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
477         in_pcbrehash(inp);
478 }
479
480 struct sockaddr *
481 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
482 {
483         struct sockaddr_in6 *sin6;
484
485         sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
486         bzero(sin6, sizeof *sin6);
487         sin6->sin6_family = AF_INET6;
488         sin6->sin6_len = sizeof(*sin6);
489         sin6->sin6_port = port;
490         sin6->sin6_addr = *addr_p;
491         (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
492
493         return (struct sockaddr *)sin6;
494 }
495
496 struct sockaddr *
497 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
498 {
499         struct sockaddr_in sin;
500         struct sockaddr_in6 *sin6_p;
501
502         bzero(&sin, sizeof sin);
503         sin.sin_family = AF_INET;
504         sin.sin_len = sizeof(sin);
505         sin.sin_port = port;
506         sin.sin_addr = *addr_p;
507
508         sin6_p = malloc(sizeof *sin6_p, M_SONAME,
509                 M_WAITOK);
510         in6_sin_2_v4mapsin6(&sin, sin6_p);
511
512         return (struct sockaddr *)sin6_p;
513 }
514
515 int
516 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
517 {
518         struct inpcb *inp;
519         struct in6_addr addr;
520         in_port_t port;
521
522         inp = sotoinpcb(so);
523         KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
524
525         INP_RLOCK(inp);
526         port = inp->inp_lport;
527         addr = inp->in6p_laddr;
528         INP_RUNLOCK(inp);
529
530         *nam = in6_sockaddr(port, &addr);
531         return 0;
532 }
533
534 int
535 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
536 {
537         struct inpcb *inp;
538         struct in6_addr addr;
539         in_port_t port;
540
541         inp = sotoinpcb(so);
542         KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
543
544         INP_RLOCK(inp);
545         port = inp->inp_fport;
546         addr = inp->in6p_faddr;
547         INP_RUNLOCK(inp);
548
549         *nam = in6_sockaddr(port, &addr);
550         return 0;
551 }
552
553 int
554 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
555 {
556         struct  inpcb *inp;
557         int     error;
558
559         inp = sotoinpcb(so);
560         KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
561
562 #ifdef INET
563         if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
564                 error = in_getsockaddr(so, nam);
565                 if (error == 0)
566                         in6_sin_2_v4mapsin6_in_sock(nam);
567         } else
568 #endif
569         {
570                 /* scope issues will be handled in in6_getsockaddr(). */
571                 error = in6_getsockaddr(so, nam);
572         }
573
574         return error;
575 }
576
577 int
578 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
579 {
580         struct  inpcb *inp;
581         int     error;
582
583         inp = sotoinpcb(so);
584         KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
585
586 #ifdef INET
587         if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
588                 error = in_getpeeraddr(so, nam);
589                 if (error == 0)
590                         in6_sin_2_v4mapsin6_in_sock(nam);
591         } else
592 #endif
593         /* scope issues will be handled in in6_getpeeraddr(). */
594         error = in6_getpeeraddr(so, nam);
595
596         return error;
597 }
598
599 /*
600  * Pass some notification to all connections of a protocol
601  * associated with address dst.  The local address and/or port numbers
602  * may be specified to limit the search.  The "usual action" will be
603  * taken, depending on the ctlinput cmd.  The caller must filter any
604  * cmds that are uninteresting (e.g., no error in the map).
605  * Call the protocol specific routine (if any) to report
606  * any errors for each matching socket.
607  */
608 void
609 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
610     u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
611     int cmd, void *cmdarg,
612     struct inpcb *(*notify)(struct inpcb *, int))
613 {
614         struct inpcb *inp, *inp_temp;
615         struct sockaddr_in6 sa6_src, *sa6_dst;
616         u_short fport = fport_arg, lport = lport_arg;
617         u_int32_t flowinfo;
618         int errno;
619
620         if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
621                 return;
622
623         sa6_dst = (struct sockaddr_in6 *)dst;
624         if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
625                 return;
626
627         /*
628          * note that src can be NULL when we get notify by local fragmentation.
629          */
630         sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
631         flowinfo = sa6_src.sin6_flowinfo;
632
633         /*
634          * Redirects go to all references to the destination,
635          * and use in6_rtchange to invalidate the route cache.
636          * Dead host indications: also use in6_rtchange to invalidate
637          * the cache, and deliver the error to all the sockets.
638          * Otherwise, if we have knowledge of the local port and address,
639          * deliver only to that socket.
640          */
641         if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
642                 fport = 0;
643                 lport = 0;
644                 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
645
646                 if (cmd != PRC_HOSTDEAD)
647                         notify = in6_rtchange;
648         }
649         errno = inet6ctlerrmap[cmd];
650         INP_INFO_WLOCK(pcbinfo);
651         CK_LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
652                 INP_WLOCK(inp);
653                 if ((inp->inp_vflag & INP_IPV6) == 0) {
654                         INP_WUNLOCK(inp);
655                         continue;
656                 }
657
658                 /*
659                  * If the error designates a new path MTU for a destination
660                  * and the application (associated with this socket) wanted to
661                  * know the value, notify.
662                  * XXX: should we avoid to notify the value to TCP sockets?
663                  */
664                 if (cmd == PRC_MSGSIZE && cmdarg != NULL)
665                         ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
666                                         *(u_int32_t *)cmdarg);
667
668                 /*
669                  * Detect if we should notify the error. If no source and
670                  * destination ports are specifed, but non-zero flowinfo and
671                  * local address match, notify the error. This is the case
672                  * when the error is delivered with an encrypted buffer
673                  * by ESP. Otherwise, just compare addresses and ports
674                  * as usual.
675                  */
676                 if (lport == 0 && fport == 0 && flowinfo &&
677                     inp->inp_socket != NULL &&
678                     flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
679                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
680                         goto do_notify;
681                 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
682                                              &sa6_dst->sin6_addr) ||
683                          inp->inp_socket == 0 ||
684                          (lport && inp->inp_lport != lport) ||
685                          (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
686                           !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
687                                               &sa6_src.sin6_addr)) ||
688                          (fport && inp->inp_fport != fport)) {
689                         INP_WUNLOCK(inp);
690                         continue;
691                 }
692
693           do_notify:
694                 if (notify) {
695                         if ((*notify)(inp, errno))
696                                 INP_WUNLOCK(inp);
697                 } else
698                         INP_WUNLOCK(inp);
699         }
700         INP_INFO_WUNLOCK(pcbinfo);
701 }
702
703 /*
704  * Lookup a PCB based on the local address and port.  Caller must hold the
705  * hash lock.  No inpcb locks or references are acquired.
706  */
707 struct inpcb *
708 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
709     u_short lport, int lookupflags, struct ucred *cred)
710 {
711         struct inpcb *inp;
712         int matchwild = 3, wildcard;
713
714         KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
715             ("%s: invalid lookup flags %d", __func__, lookupflags));
716
717         INP_HASH_WLOCK_ASSERT(pcbinfo);
718
719         if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
720                 struct inpcbhead *head;
721                 /*
722                  * Look for an unconnected (wildcard foreign addr) PCB that
723                  * matches the local address and port we're looking for.
724                  */
725                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
726                     INP6_PCBHASHKEY(&in6addr_any), lport, 0,
727                     pcbinfo->ipi_hashmask)];
728                 CK_LIST_FOREACH(inp, head, inp_hash) {
729                         /* XXX inp locking */
730                         if ((inp->inp_vflag & INP_IPV6) == 0)
731                                 continue;
732                         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
733                             IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
734                             inp->inp_lport == lport) {
735                                 /* Found. */
736                                 if (cred == NULL ||
737                                     prison_equal_ip6(cred->cr_prison,
738                                         inp->inp_cred->cr_prison))
739                                         return (inp);
740                         }
741                 }
742                 /*
743                  * Not found.
744                  */
745                 return (NULL);
746         } else {
747                 struct inpcbporthead *porthash;
748                 struct inpcbport *phd;
749                 struct inpcb *match = NULL;
750                 /*
751                  * Best fit PCB lookup.
752                  *
753                  * First see if this local port is in use by looking on the
754                  * port hash list.
755                  */
756                 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
757                     pcbinfo->ipi_porthashmask)];
758                 CK_LIST_FOREACH(phd, porthash, phd_hash) {
759                         if (phd->phd_port == lport)
760                                 break;
761                 }
762                 if (phd != NULL) {
763                         /*
764                          * Port is in use by one or more PCBs. Look for best
765                          * fit.
766                          */
767                         CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
768                                 wildcard = 0;
769                                 if (cred != NULL &&
770                                     !prison_equal_ip6(cred->cr_prison,
771                                         inp->inp_cred->cr_prison))
772                                         continue;
773                                 /* XXX inp locking */
774                                 if ((inp->inp_vflag & INP_IPV6) == 0)
775                                         continue;
776                                 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
777                                         wildcard++;
778                                 if (!IN6_IS_ADDR_UNSPECIFIED(
779                                         &inp->in6p_laddr)) {
780                                         if (IN6_IS_ADDR_UNSPECIFIED(laddr))
781                                                 wildcard++;
782                                         else if (!IN6_ARE_ADDR_EQUAL(
783                                             &inp->in6p_laddr, laddr))
784                                                 continue;
785                                 } else {
786                                         if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
787                                                 wildcard++;
788                                 }
789                                 if (wildcard < matchwild) {
790                                         match = inp;
791                                         matchwild = wildcard;
792                                         if (matchwild == 0)
793                                                 break;
794                                 }
795                         }
796                 }
797                 return (match);
798         }
799 }
800
801 void
802 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
803 {
804         struct inpcb *in6p;
805         struct ip6_moptions *im6o;
806         int i, gap;
807
808         INP_INFO_WLOCK(pcbinfo);
809         CK_LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
810                 INP_WLOCK(in6p);
811                 if (__predict_false(in6p->inp_flags2 & INP_FREED)) {
812                         INP_WUNLOCK(in6p);
813                         continue;
814                 }
815                 im6o = in6p->in6p_moptions;
816                 if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
817                         /*
818                          * Unselect the outgoing ifp for multicast if it
819                          * is being detached.
820                          */
821                         if (im6o->im6o_multicast_ifp == ifp)
822                                 im6o->im6o_multicast_ifp = NULL;
823                         /*
824                          * Drop multicast group membership if we joined
825                          * through the interface being detached.
826                          */
827                         gap = 0;
828                         for (i = 0; i < im6o->im6o_num_memberships; i++) {
829                                 if (im6o->im6o_membership[i]->in6m_ifp ==
830                                     ifp) {
831                                         in6_leavegroup(im6o->im6o_membership[i], NULL);
832                                         gap++;
833                                 } else if (gap != 0) {
834                                         im6o->im6o_membership[i - gap] =
835                                             im6o->im6o_membership[i];
836                                 }
837                         }
838                         im6o->im6o_num_memberships -= gap;
839                 }
840                 INP_WUNLOCK(in6p);
841         }
842         INP_INFO_WUNLOCK(pcbinfo);
843 }
844
845 /*
846  * Check for alternatives when higher level complains
847  * about service problems.  For now, invalidate cached
848  * routing information.  If the route was created dynamically
849  * (by a redirect), time to try a default gateway again.
850  */
851 void
852 in6_losing(struct inpcb *inp)
853 {
854
855         RO_INVALIDATE_CACHE(&inp->inp_route6);
856 }
857
858 /*
859  * After a routing change, flush old routing
860  * and allocate a (hopefully) better one.
861  */
862 struct inpcb *
863 in6_rtchange(struct inpcb *inp, int errno __unused)
864 {
865
866         RO_INVALIDATE_CACHE(&inp->inp_route6);
867         return inp;
868 }
869
870 static struct inpcb *
871 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
872     const struct in6_addr *laddr, uint16_t lport, const struct in6_addr *faddr,
873     uint16_t fport, int lookupflags)
874 {
875         struct inpcb *local_wild;
876         const struct inpcblbgrouphead *hdr;
877         struct inpcblbgroup *grp;
878         uint32_t idx;
879
880         INP_HASH_LOCK_ASSERT(pcbinfo);
881
882         hdr = &pcbinfo->ipi_lbgrouphashbase[
883             INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
884
885         /*
886          * Order of socket selection:
887          * 1. non-wild.
888          * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD).
889          *
890          * NOTE:
891          * - Load balanced group does not contain jailed sockets.
892          * - Load balanced does not contain IPv4 mapped INET6 wild sockets.
893          */
894         local_wild = NULL;
895         CK_LIST_FOREACH(grp, hdr, il_list) {
896 #ifdef INET
897                 if (!(grp->il_vflag & INP_IPV6))
898                         continue;
899 #endif
900                 if (grp->il_lport != lport)
901                         continue;
902
903                 idx = INP_PCBLBGROUP_PKTHASH(INP6_PCBHASHKEY(faddr), lport,
904                     fport) % grp->il_inpcnt;
905                 if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr))
906                         return (grp->il_inp[idx]);
907                 if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr) &&
908                     (lookupflags & INPLOOKUP_WILDCARD) != 0)
909                         local_wild = grp->il_inp[idx];
910         }
911         return (local_wild);
912 }
913
914 #ifdef PCBGROUP
915 /*
916  * Lookup PCB in hash list, using pcbgroup tables.
917  */
918 static struct inpcb *
919 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
920     struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
921     u_int lport_arg, int lookupflags, struct ifnet *ifp)
922 {
923         struct inpcbhead *head;
924         struct inpcb *inp, *tmpinp;
925         u_short fport = fport_arg, lport = lport_arg;
926         bool locked;
927
928         /*
929          * First look for an exact match.
930          */
931         tmpinp = NULL;
932         INP_GROUP_LOCK(pcbgroup);
933         head = &pcbgroup->ipg_hashbase[INP_PCBHASH(
934             INP6_PCBHASHKEY(faddr), lport, fport, pcbgroup->ipg_hashmask)];
935         CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
936                 /* XXX inp locking */
937                 if ((inp->inp_vflag & INP_IPV6) == 0)
938                         continue;
939                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
940                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
941                     inp->inp_fport == fport &&
942                     inp->inp_lport == lport) {
943                         /*
944                          * XXX We should be able to directly return
945                          * the inp here, without any checks.
946                          * Well unless both bound with SO_REUSEPORT?
947                          */
948                         if (prison_flag(inp->inp_cred, PR_IP6))
949                                 goto found;
950                         if (tmpinp == NULL)
951                                 tmpinp = inp;
952                 }
953         }
954         if (tmpinp != NULL) {
955                 inp = tmpinp;
956                 goto found;
957         }
958
959         /*
960          * Then look for a wildcard match in the pcbgroup.
961          */
962         if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
963                 struct inpcb *local_wild = NULL, *local_exact = NULL;
964                 struct inpcb *jail_wild = NULL;
965                 int injail;
966
967                 /*
968                  * Order of socket selection - we always prefer jails.
969                  *      1. jailed, non-wild.
970                  *      2. jailed, wild.
971                  *      3. non-jailed, non-wild.
972                  *      4. non-jailed, wild.
973                  */
974                 head = &pcbgroup->ipg_hashbase[
975                     INP_PCBHASH(INADDR_ANY, lport, 0, pcbgroup->ipg_hashmask)];
976                 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
977                         /* XXX inp locking */
978                         if ((inp->inp_vflag & INP_IPV6) == 0)
979                                 continue;
980
981                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
982                             inp->inp_lport != lport) {
983                                 continue;
984                         }
985
986                         injail = prison_flag(inp->inp_cred, PR_IP6);
987                         if (injail) {
988                                 if (prison_check_ip6(inp->inp_cred,
989                                     laddr) != 0)
990                                         continue;
991                         } else {
992                                 if (local_exact != NULL)
993                                         continue;
994                         }
995
996                         if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
997                                 if (injail)
998                                         goto found;
999                                 else
1000                                         local_exact = inp;
1001                         } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1002                                 if (injail)
1003                                         jail_wild = inp;
1004                                 else
1005                                         local_wild = inp;
1006                         }
1007                 } /* LIST_FOREACH */
1008
1009                 inp = jail_wild;
1010                 if (inp == NULL)
1011                         inp = jail_wild;
1012                 if (inp == NULL)
1013                         inp = local_exact;
1014                 if (inp == NULL)
1015                         inp = local_wild;
1016                 if (inp != NULL)
1017                         goto found;
1018         }
1019
1020         /*
1021          * Then look for a wildcard match, if requested.
1022          */
1023         if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1024                 struct inpcb *local_wild = NULL, *local_exact = NULL;
1025                 struct inpcb *jail_wild = NULL;
1026                 int injail;
1027
1028                 /*
1029                  * Order of socket selection - we always prefer jails.
1030                  *      1. jailed, non-wild.
1031                  *      2. jailed, wild.
1032                  *      3. non-jailed, non-wild.
1033                  *      4. non-jailed, wild.
1034                  */
1035                 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(
1036                     INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1037                     pcbinfo->ipi_wildmask)];
1038                 CK_LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1039                         /* XXX inp locking */
1040                         if ((inp->inp_vflag & INP_IPV6) == 0)
1041                                 continue;
1042
1043                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1044                             inp->inp_lport != lport) {
1045                                 continue;
1046                         }
1047
1048                         injail = prison_flag(inp->inp_cred, PR_IP6);
1049                         if (injail) {
1050                                 if (prison_check_ip6(inp->inp_cred,
1051                                     laddr) != 0)
1052                                         continue;
1053                         } else {
1054                                 if (local_exact != NULL)
1055                                         continue;
1056                         }
1057
1058                         if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1059                                 if (injail)
1060                                         goto found;
1061                                 else
1062                                         local_exact = inp;
1063                         } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1064                                 if (injail)
1065                                         jail_wild = inp;
1066                                 else
1067                                         local_wild = inp;
1068                         }
1069                 } /* LIST_FOREACH */
1070
1071                 inp = jail_wild;
1072                 if (inp == NULL)
1073                         inp = jail_wild;
1074                 if (inp == NULL)
1075                         inp = local_exact;
1076                 if (inp == NULL)
1077                         inp = local_wild;
1078                 if (inp != NULL)
1079                         goto found;
1080         } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1081         INP_GROUP_UNLOCK(pcbgroup);
1082         return (NULL);
1083
1084 found:
1085         if (lookupflags & INPLOOKUP_WLOCKPCB)
1086                 locked = INP_TRY_WLOCK(inp);
1087         else if (lookupflags & INPLOOKUP_RLOCKPCB)
1088                 locked = INP_TRY_RLOCK(inp);
1089         else
1090                 panic("%s: locking buf", __func__);
1091         if (!locked)
1092                 in_pcbref(inp);
1093         INP_GROUP_UNLOCK(pcbgroup);
1094         if (!locked) {
1095                 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1096                         INP_WLOCK(inp);
1097                         if (in_pcbrele_wlocked(inp))
1098                                 return (NULL);
1099                 } else {
1100                         INP_RLOCK(inp);
1101                         if (in_pcbrele_rlocked(inp))
1102                                 return (NULL);
1103                 }
1104         }
1105 #ifdef INVARIANTS
1106         if (lookupflags & INPLOOKUP_WLOCKPCB)
1107                 INP_WLOCK_ASSERT(inp);
1108         else
1109                 INP_RLOCK_ASSERT(inp);
1110 #endif
1111         return (inp);
1112 }
1113 #endif /* PCBGROUP */
1114
1115 /*
1116  * Lookup PCB in hash list.
1117  */
1118 static struct inpcb *
1119 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1120     u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1121     int lookupflags, struct ifnet *ifp)
1122 {
1123         struct inpcbhead *head;
1124         struct inpcb *inp, *tmpinp;
1125         u_short fport = fport_arg, lport = lport_arg;
1126
1127         KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1128             ("%s: invalid lookup flags %d", __func__, lookupflags));
1129
1130         INP_HASH_LOCK_ASSERT(pcbinfo);
1131
1132         /*
1133          * First look for an exact match.
1134          */
1135         tmpinp = NULL;
1136         head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1137             INP6_PCBHASHKEY(faddr), lport, fport, pcbinfo->ipi_hashmask)];
1138         CK_LIST_FOREACH(inp, head, inp_hash) {
1139                 /* XXX inp locking */
1140                 if ((inp->inp_vflag & INP_IPV6) == 0)
1141                         continue;
1142                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1143                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1144                     inp->inp_fport == fport &&
1145                     inp->inp_lport == lport) {
1146                         /*
1147                          * XXX We should be able to directly return
1148                          * the inp here, without any checks.
1149                          * Well unless both bound with SO_REUSEPORT?
1150                          */
1151                         if (prison_flag(inp->inp_cred, PR_IP6))
1152                                 return (inp);
1153                         if (tmpinp == NULL)
1154                                 tmpinp = inp;
1155                 }
1156         }
1157         if (tmpinp != NULL)
1158                 return (tmpinp);
1159
1160         /*
1161          * Then look in lb group (for wildcard match).
1162          */
1163         if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1164                 inp = in6_pcblookup_lbgroup(pcbinfo, laddr, lport, faddr,
1165                     fport, lookupflags);
1166                 if (inp != NULL)
1167                         return (inp);
1168         }
1169
1170         /*
1171          * Then look for a wildcard match, if requested.
1172          */
1173         if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1174                 struct inpcb *local_wild = NULL, *local_exact = NULL;
1175                 struct inpcb *jail_wild = NULL;
1176                 int injail;
1177
1178                 /*
1179                  * Order of socket selection - we always prefer jails.
1180                  *      1. jailed, non-wild.
1181                  *      2. jailed, wild.
1182                  *      3. non-jailed, non-wild.
1183                  *      4. non-jailed, wild.
1184                  */
1185                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1186                     INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1187                     pcbinfo->ipi_hashmask)];
1188                 CK_LIST_FOREACH(inp, head, inp_hash) {
1189                         /* XXX inp locking */
1190                         if ((inp->inp_vflag & INP_IPV6) == 0)
1191                                 continue;
1192
1193                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1194                             inp->inp_lport != lport) {
1195                                 continue;
1196                         }
1197
1198                         injail = prison_flag(inp->inp_cred, PR_IP6);
1199                         if (injail) {
1200                                 if (prison_check_ip6(inp->inp_cred,
1201                                     laddr) != 0)
1202                                         continue;
1203                         } else {
1204                                 if (local_exact != NULL)
1205                                         continue;
1206                         }
1207
1208                         if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1209                                 if (injail)
1210                                         return (inp);
1211                                 else
1212                                         local_exact = inp;
1213                         } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1214                                 if (injail)
1215                                         jail_wild = inp;
1216                                 else
1217                                         local_wild = inp;
1218                         }
1219                 } /* LIST_FOREACH */
1220
1221                 if (jail_wild != NULL)
1222                         return (jail_wild);
1223                 if (local_exact != NULL)
1224                         return (local_exact);
1225                 if (local_wild != NULL)
1226                         return (local_wild);
1227         } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1228
1229         /*
1230          * Not found.
1231          */
1232         return (NULL);
1233 }
1234
1235 /*
1236  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
1237  * hash list lock, and will return the inpcb locked (i.e., requires
1238  * INPLOOKUP_LOCKPCB).
1239  */
1240 static struct inpcb *
1241 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1242     u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1243     struct ifnet *ifp)
1244 {
1245         struct inpcb *inp;
1246
1247         INP_HASH_RLOCK(pcbinfo);
1248         inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1249             (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1250         if (inp != NULL) {
1251                 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1252                         INP_WLOCK(inp);
1253                         if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1254                                 INP_WUNLOCK(inp);
1255                                 inp = NULL;
1256                         }
1257                 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1258                         INP_RLOCK(inp);
1259                         if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1260                                 INP_RUNLOCK(inp);
1261                                 inp = NULL;
1262                         }
1263                 } else
1264                         panic("%s: locking bug", __func__);
1265 #ifdef INVARIANTS
1266                 if (inp != NULL) {
1267                         if (lookupflags & INPLOOKUP_WLOCKPCB)
1268                                 INP_WLOCK_ASSERT(inp);
1269                         else
1270                                 INP_RLOCK_ASSERT(inp);
1271                 }
1272 #endif
1273         }
1274         INP_HASH_RUNLOCK(pcbinfo);
1275         return (inp);
1276 }
1277
1278 /*
1279  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1280  * from which a pre-calculated hash value may be extracted.
1281  *
1282  * Possibly more of this logic should be in in6_pcbgroup.c.
1283  */
1284 struct inpcb *
1285 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1286     struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1287 {
1288 #if defined(PCBGROUP) && !defined(RSS)
1289         struct inpcbgroup *pcbgroup;
1290 #endif
1291
1292         KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1293             ("%s: invalid lookup flags %d", __func__, lookupflags));
1294         KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1295             ("%s: LOCKPCB not set", __func__));
1296
1297         /*
1298          * When not using RSS, use connection groups in preference to the
1299          * reservation table when looking up 4-tuples.  When using RSS, just
1300          * use the reservation table, due to the cost of the Toeplitz hash
1301          * in software.
1302          *
1303          * XXXRW: This policy belongs in the pcbgroup code, as in principle
1304          * we could be doing RSS with a non-Toeplitz hash that is affordable
1305          * in software.
1306          */
1307 #if defined(PCBGROUP) && !defined(RSS)
1308         if (in_pcbgroup_enabled(pcbinfo)) {
1309                 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1310                     fport);
1311                 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1312                     laddr, lport, lookupflags, ifp));
1313         }
1314 #endif
1315         return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1316             lookupflags, ifp));
1317 }
1318
1319 struct inpcb *
1320 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1321     u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1322     struct ifnet *ifp, struct mbuf *m)
1323 {
1324 #ifdef PCBGROUP
1325         struct inpcbgroup *pcbgroup;
1326 #endif
1327
1328         KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1329             ("%s: invalid lookup flags %d", __func__, lookupflags));
1330         KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1331             ("%s: LOCKPCB not set", __func__));
1332
1333 #ifdef PCBGROUP
1334         /*
1335          * If we can use a hardware-generated hash to look up the connection
1336          * group, use that connection group to find the inpcb.  Otherwise
1337          * fall back on a software hash -- or the reservation table if we're
1338          * using RSS.
1339          *
1340          * XXXRW: As above, that policy belongs in the pcbgroup code.
1341          */
1342         if (in_pcbgroup_enabled(pcbinfo) &&
1343             M_HASHTYPE_TEST(m, M_HASHTYPE_NONE) == 0) {
1344                 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1345                     m->m_pkthdr.flowid);
1346                 if (pcbgroup != NULL)
1347                         return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1348                             fport, laddr, lport, lookupflags, ifp));
1349 #ifndef RSS
1350                 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1351                     fport);
1352                 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1353                     laddr, lport, lookupflags, ifp));
1354 #endif
1355         }
1356 #endif
1357         return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1358             lookupflags, ifp));
1359 }
1360
1361 void
1362 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
1363 {
1364         struct ip6_hdr *ip;
1365
1366         ip = mtod(m, struct ip6_hdr *);
1367         bzero(sin6, sizeof(*sin6));
1368         sin6->sin6_len = sizeof(*sin6);
1369         sin6->sin6_family = AF_INET6;
1370         sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src;
1371
1372         (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
1373
1374         return;
1375 }