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