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