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