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
67 #include <sys/cdefs.h>
68 #include "opt_inet.h"
69 #include "opt_inet6.h"
70 #include "opt_ipsec.h"
71 #include "opt_route.h"
72 #include "opt_rss.h"
73
74 #include <sys/hash.h>
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/malloc.h>
78 #include <sys/mbuf.h>
79 #include <sys/domain.h>
80 #include <sys/proc.h>
81 #include <sys/protosw.h>
82 #include <sys/smr.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/sockio.h>
86 #include <sys/sysctl.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 #include <net/route/nhop.h>
101 #include <net/vnet.h>
102
103 #include <netinet/in.h>
104 #include <netinet/in_var.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/ip6.h>
107 #include <netinet/ip_var.h>
108
109 #include <netinet6/ip6_var.h>
110 #include <netinet6/nd6.h>
111 #include <netinet/in_pcb.h>
112 #include <netinet/in_pcb_var.h>
113 #include <netinet6/in6_pcb.h>
114 #include <netinet6/in6_fib.h>
115 #include <netinet6/scope6_var.h>
116
117 SYSCTL_DECL(_net_inet6);
118 SYSCTL_DECL(_net_inet6_ip6);
119 VNET_DEFINE_STATIC(int, connect_in6addr_wild) = 1;
120 #define V_connect_in6addr_wild VNET(connect_in6addr_wild)
121 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, connect_in6addr_wild,
122 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(connect_in6addr_wild), 0,
123 "Allow connecting to the unspecified address for connect(2)");
124
125 int
in6_pcbsetport(struct in6_addr * laddr,struct inpcb * inp,struct ucred * cred)126 in6_pcbsetport(struct in6_addr *laddr, struct inpcb *inp, struct ucred *cred)
127 {
128 struct socket *so = inp->inp_socket;
129 u_int16_t lport = 0;
130 int error, lookupflags = 0;
131 #ifdef INVARIANTS
132 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
133 #endif
134
135 INP_WLOCK_ASSERT(inp);
136 INP_HASH_WLOCK_ASSERT(pcbinfo);
137
138 error = prison_local_ip6(cred, laddr,
139 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0));
140 if (error)
141 return(error);
142
143 /* XXX: this is redundant when called from in6_pcbbind */
144 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
145 lookupflags = INPLOOKUP_WILDCARD;
146
147 inp->inp_flags |= INP_ANONPORT;
148
149 error = in_pcb_lport(inp, NULL, &lport, cred, lookupflags);
150 if (error != 0)
151 return (error);
152
153 inp->inp_lport = lport;
154 if (in_pcbinshash(inp) != 0) {
155 inp->in6p_laddr = in6addr_any;
156 inp->inp_lport = 0;
157 return (EAGAIN);
158 }
159
160 return (0);
161 }
162
163 /*
164 * Determine whether the inpcb can be bound to the specified address/port tuple.
165 */
166 static int
in6_pcbbind_avail(struct inpcb * inp,const struct sockaddr_in6 * sin6,int fib,int sooptions,int lookupflags,struct ucred * cred)167 in6_pcbbind_avail(struct inpcb *inp, const struct sockaddr_in6 *sin6, int fib,
168 int sooptions, int lookupflags, struct ucred *cred)
169 {
170 const struct in6_addr *laddr;
171 int reuseport, reuseport_lb;
172 u_short lport;
173
174 INP_LOCK_ASSERT(inp);
175 INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
176
177 laddr = &sin6->sin6_addr;
178 lport = sin6->sin6_port;
179
180 reuseport = (sooptions & SO_REUSEPORT);
181 reuseport_lb = (sooptions & SO_REUSEPORT_LB);
182
183 if (IN6_IS_ADDR_MULTICAST(laddr)) {
184 /*
185 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
186 * allow compepte duplication of binding if
187 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
188 * and a multicast address is bound on both
189 * new and duplicated sockets.
190 */
191 if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT)) != 0)
192 reuseport = SO_REUSEADDR | SO_REUSEPORT;
193 /*
194 * XXX: How to deal with SO_REUSEPORT_LB here?
195 * Treat same as SO_REUSEPORT for now.
196 */
197 if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT_LB)) != 0)
198 reuseport_lb = SO_REUSEADDR | SO_REUSEPORT_LB;
199 } else if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) {
200 struct sockaddr_in6 sin6;
201 struct epoch_tracker et;
202 struct ifaddr *ifa;
203
204 memset(&sin6, 0, sizeof(sin6));
205 sin6.sin6_family = AF_INET6;
206 sin6.sin6_len = sizeof(sin6);
207 sin6.sin6_addr = *laddr;
208
209 NET_EPOCH_ENTER(et);
210 if ((ifa = ifa_ifwithaddr((const struct sockaddr *)&sin6)) ==
211 NULL && (inp->inp_flags & INP_BINDANY) == 0) {
212 NET_EPOCH_EXIT(et);
213 return (EADDRNOTAVAIL);
214 }
215
216 /*
217 * XXX: bind to an anycast address might accidentally
218 * cause sending a packet with anycast source address.
219 * We should allow to bind to a deprecated address, since
220 * the application dares to use it.
221 */
222 if (ifa != NULL &&
223 ((struct in6_ifaddr *)ifa)->ia6_flags &
224 (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY | IN6_IFF_DETACHED)) {
225 NET_EPOCH_EXIT(et);
226 return (EADDRNOTAVAIL);
227 }
228 NET_EPOCH_EXIT(et);
229 }
230
231 if (lport != 0) {
232 struct inpcb *t;
233
234 if (ntohs(lport) <= V_ipport_reservedhigh &&
235 ntohs(lport) >= V_ipport_reservedlow &&
236 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT))
237 return (EACCES);
238
239 if (!IN6_IS_ADDR_MULTICAST(laddr) &&
240 priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) !=
241 0) {
242 /*
243 * If a socket owned by a different user is already
244 * bound to this port, fail. In particular, SO_REUSE*
245 * can only be used to share a port among sockets owned
246 * by the same user.
247 *
248 * However, we can share a port with a connected socket
249 * which has a unique 4-tuple.
250 */
251 t = in6_pcblookup_local(inp->inp_pcbinfo, laddr, lport,
252 RT_ALL_FIBS, INPLOOKUP_WILDCARD, cred);
253 if (t != NULL &&
254 (inp->inp_socket->so_type != SOCK_STREAM ||
255 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
256 (inp->inp_cred->cr_uid != t->inp_cred->cr_uid))
257 return (EADDRINUSE);
258
259 #ifdef INET
260 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
261 IN6_IS_ADDR_UNSPECIFIED(laddr)) {
262 struct sockaddr_in sin;
263
264 in6_sin6_2_sin(&sin, sin6);
265 t = in_pcblookup_local(inp->inp_pcbinfo,
266 sin.sin_addr, lport, RT_ALL_FIBS,
267 INPLOOKUP_WILDCARD, cred);
268 if (t != NULL &&
269 (inp->inp_socket->so_type != SOCK_STREAM ||
270 in_nullhost(t->inp_faddr)) &&
271 (inp->inp_cred->cr_uid !=
272 t->inp_cred->cr_uid))
273 return (EADDRINUSE);
274 }
275 #endif
276 }
277 t = in6_pcblookup_local(inp->inp_pcbinfo, laddr, lport,
278 fib, lookupflags, cred);
279 if (t != NULL && ((reuseport | reuseport_lb) &
280 t->inp_socket->so_options) == 0)
281 return (EADDRINUSE);
282 #ifdef INET
283 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
284 IN6_IS_ADDR_UNSPECIFIED(laddr)) {
285 struct sockaddr_in sin;
286
287 in6_sin6_2_sin(&sin, sin6);
288 t = in_pcblookup_local(inp->inp_pcbinfo, sin.sin_addr,
289 lport, RT_ALL_FIBS, lookupflags, cred);
290 if (t != NULL && ((reuseport | reuseport_lb) &
291 t->inp_socket->so_options) == 0 &&
292 (!in_nullhost(t->inp_laddr) ||
293 (t->inp_vflag & INP_IPV6PROTO) != 0)) {
294 return (EADDRINUSE);
295 }
296 }
297 #endif
298 }
299 return (0);
300 }
301
302 int
in6_pcbbind(struct inpcb * inp,struct sockaddr_in6 * sin6,int flags,struct ucred * cred)303 in6_pcbbind(struct inpcb *inp, struct sockaddr_in6 *sin6, int flags,
304 struct ucred *cred)
305 {
306 struct socket *so = inp->inp_socket;
307 u_short lport = 0;
308 int error, fib, lookupflags, sooptions;
309
310 INP_WLOCK_ASSERT(inp);
311 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
312
313 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
314 return (EINVAL);
315
316 lookupflags = 0;
317 sooptions = atomic_load_int(&so->so_options);
318 if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT | SO_REUSEPORT_LB)) == 0)
319 lookupflags = INPLOOKUP_WILDCARD;
320 if (sin6 == NULL) {
321 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
322 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
323 return (error);
324 } else {
325 KASSERT(sin6->sin6_family == AF_INET6,
326 ("%s: invalid address family for %p", __func__, sin6));
327 KASSERT(sin6->sin6_len == sizeof(*sin6),
328 ("%s: invalid address length for %p", __func__, sin6));
329
330 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
331 return(error);
332
333 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
334 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
335 return (error);
336
337 fib = (flags & INPBIND_FIB) != 0 ? inp->inp_inc.inc_fibnum :
338 RT_ALL_FIBS;
339
340 /* See if this address/port combo is available. */
341 error = in6_pcbbind_avail(inp, sin6, fib, sooptions, lookupflags,
342 cred);
343 if (error != 0)
344 return (error);
345
346 lport = sin6->sin6_port;
347 inp->in6p_laddr = sin6->sin6_addr;
348 }
349 if ((flags & INPBIND_FIB) != 0)
350 inp->inp_flags |= INP_BOUNDFIB;
351 if (lport == 0) {
352 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
353 /* Undo an address bind that may have occurred. */
354 inp->inp_flags &= ~INP_BOUNDFIB;
355 inp->in6p_laddr = in6addr_any;
356 return (error);
357 }
358 } else {
359 inp->inp_lport = lport;
360 if (in_pcbinshash(inp) != 0) {
361 inp->inp_flags &= ~INP_BOUNDFIB;
362 inp->in6p_laddr = in6addr_any;
363 inp->inp_lport = 0;
364 return (EAGAIN);
365 }
366 }
367 return (0);
368 }
369
370 /*
371 * Transform old in6_pcbconnect() into an inner subroutine for new
372 * in6_pcbconnect(): Do some validity-checking on the remote
373 * address (in mbuf 'nam') and then determine local host address
374 * (i.e., which interface) to use to access that remote host.
375 *
376 * This preserves definition of in6_pcbconnect(), while supporting a
377 * slightly different version for T/TCP. (This is more than
378 * a bit of a kludge, but cleaning up the internal interfaces would
379 * have forced minor changes in every protocol).
380 */
381 static int
in6_pcbladdr(struct inpcb * inp,struct sockaddr_in6 * sin6,struct in6_addr * plocal_addr6,bool sas_required)382 in6_pcbladdr(struct inpcb *inp, struct sockaddr_in6 *sin6,
383 struct in6_addr *plocal_addr6, bool sas_required)
384 {
385 int error = 0;
386 int scope_ambiguous = 0;
387 struct in6_addr in6a;
388
389 NET_EPOCH_ASSERT();
390 INP_WLOCK_ASSERT(inp);
391 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */
392
393 if (sin6->sin6_port == 0)
394 return (EADDRNOTAVAIL);
395
396 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
397 scope_ambiguous = 1;
398 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
399 return(error);
400
401 if (V_connect_in6addr_wild && !CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) {
402 /*
403 * If the destination address is UNSPECIFIED addr,
404 * use the loopback addr, e.g ::1.
405 */
406 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
407 sin6->sin6_addr = in6addr_loopback;
408 } else if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
409 return (ENETUNREACH);
410 }
411
412 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
413 return (error);
414
415 if (sas_required) {
416 error = in6_selectsrc_socket(sin6, inp->in6p_outputopts,
417 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
418 if (error)
419 return (error);
420 } else {
421 /*
422 * Source address selection isn't required when syncache
423 * has already established connection and both source and
424 * destination addresses was chosen.
425 *
426 * This also includes the case when fwd_tag was used to
427 * select source address in tcp_input().
428 */
429 in6a = inp->in6p_laddr;
430 }
431
432 if (IN6_IS_ADDR_UNSPECIFIED(&in6a))
433 return (EHOSTUNREACH);
434 /*
435 * Do not update this earlier, in case we return with an error.
436 *
437 * XXX: this in6_selectsrc_socket result might replace the bound local
438 * address with the address specified by setsockopt(IPV6_PKTINFO).
439 * Is it the intended behavior?
440 */
441 *plocal_addr6 = in6a;
442
443 /*
444 * Don't do pcblookup call here; return interface in
445 * plocal_addr6
446 * and exit to caller, that will do the lookup.
447 */
448
449 return (0);
450 }
451
452 /*
453 * Outer subroutine:
454 * Connect from a socket to a specified address.
455 * Both address and port must be specified in argument sin.
456 * If don't have a local address for this socket yet,
457 * then pick one.
458 */
459 int
in6_pcbconnect(struct inpcb * inp,struct sockaddr_in6 * sin6,struct ucred * cred,bool sas_required)460 in6_pcbconnect(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred,
461 bool sas_required)
462 {
463 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
464 struct sockaddr_in6 laddr6;
465 int error;
466
467 NET_EPOCH_ASSERT();
468 INP_WLOCK_ASSERT(inp);
469 INP_HASH_WLOCK_ASSERT(pcbinfo);
470 KASSERT(sin6->sin6_family == AF_INET6,
471 ("%s: invalid address family for %p", __func__, sin6));
472 KASSERT(sin6->sin6_len == sizeof(*sin6),
473 ("%s: invalid address length for %p", __func__, sin6));
474 KASSERT(IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr),
475 ("%s: inp is already connected", __func__));
476
477 bzero(&laddr6, sizeof(laddr6));
478 laddr6.sin6_family = AF_INET6;
479
480 #ifdef ROUTE_MPATH
481 if (CALC_FLOWID_OUTBOUND) {
482 uint32_t hash_type, hash_val;
483
484 hash_val = fib6_calc_software_hash(&inp->in6p_laddr,
485 &sin6->sin6_addr, 0, sin6->sin6_port,
486 inp->inp_socket->so_proto->pr_protocol, &hash_type);
487 inp->inp_flowid = hash_val;
488 inp->inp_flowtype = hash_type;
489 }
490 #endif
491 /*
492 * Call inner routine, to assign local interface address.
493 * in6_pcbladdr() may automatically fill in sin6_scope_id.
494 */
495 if ((error = in6_pcbladdr(inp, sin6, &laddr6.sin6_addr,
496 sas_required)) != 0)
497 return (error);
498
499 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
500 sin6->sin6_port, IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
501 &laddr6.sin6_addr : &inp->in6p_laddr, inp->inp_lport, 0,
502 M_NODOM, RT_ALL_FIBS) != NULL)
503 return (EADDRINUSE);
504 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
505 if (inp->inp_lport == 0) {
506 error = in_pcb_lport_dest(inp,
507 (struct sockaddr *) &laddr6, &inp->inp_lport,
508 (struct sockaddr *) sin6, sin6->sin6_port, cred,
509 INPLOOKUP_WILDCARD);
510 if (error)
511 return (error);
512 }
513 inp->in6p_laddr = laddr6.sin6_addr;
514 }
515 inp->in6p_faddr = sin6->sin6_addr;
516 inp->inp_fport = sin6->sin6_port;
517 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
518 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
519 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
520 inp->inp_flow |=
521 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
522
523 if ((inp->inp_flags & INP_INHASHLIST) != 0) {
524 in_pcbrehash(inp);
525 } else {
526 error = in_pcbinshash(inp);
527 MPASS(error == 0);
528 }
529
530 return (0);
531 }
532
533 void
in6_pcbdisconnect(struct inpcb * inp)534 in6_pcbdisconnect(struct inpcb *inp)
535 {
536
537 INP_WLOCK_ASSERT(inp);
538 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
539 KASSERT(inp->inp_smr == SMR_SEQ_INVALID,
540 ("%s: inp %p was already disconnected", __func__, inp));
541
542 in_pcbremhash_locked(inp);
543
544 /* See the comment in in_pcbinshash(). */
545 inp->inp_smr = smr_advance(inp->inp_pcbinfo->ipi_smr);
546
547 /* XXX-MJ torn writes are visible to SMR lookup */
548 memset(&inp->in6p_laddr, 0, sizeof(inp->in6p_laddr));
549 memset(&inp->in6p_faddr, 0, sizeof(inp->in6p_faddr));
550 inp->inp_fport = 0;
551 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
552 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
553 }
554
555 int
in6_getsockaddr(struct socket * so,struct sockaddr * sa)556 in6_getsockaddr(struct socket *so, struct sockaddr *sa)
557 {
558 struct inpcb *inp;
559
560 inp = sotoinpcb(so);
561 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
562
563 *(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){
564 .sin6_len = sizeof(struct sockaddr_in6),
565 .sin6_family = AF_INET6,
566 .sin6_port = inp->inp_lport,
567 .sin6_addr = inp->in6p_laddr,
568 };
569 /* XXX: should catch errors */
570 (void)sa6_recoverscope((struct sockaddr_in6 *)sa);
571
572 return (0);
573 }
574
575 int
in6_getpeeraddr(struct socket * so,struct sockaddr * sa)576 in6_getpeeraddr(struct socket *so, struct sockaddr *sa)
577 {
578 struct inpcb *inp;
579
580 inp = sotoinpcb(so);
581 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
582
583 *(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){
584 .sin6_len = sizeof(struct sockaddr_in6),
585 .sin6_family = AF_INET6,
586 .sin6_port = inp->inp_fport,
587 .sin6_addr = inp->in6p_faddr,
588 };
589 /* XXX: should catch errors */
590 (void)sa6_recoverscope((struct sockaddr_in6 *)sa);
591
592 return (0);
593 }
594
595 int
in6_mapped_sockaddr(struct socket * so,struct sockaddr * sa)596 in6_mapped_sockaddr(struct socket *so, struct sockaddr *sa)
597 {
598 int error;
599 #ifdef INET
600 struct inpcb *inp;
601
602 inp = sotoinpcb(so);
603 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
604
605 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
606 struct sockaddr_in sin;
607
608 error = in_getsockaddr(so, (struct sockaddr *)&sin);
609 if (error == 0)
610 in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
611 } else
612 #endif
613 {
614 /* scope issues will be handled in in6_getsockaddr(). */
615 error = in6_getsockaddr(so, sa);
616 }
617
618 return error;
619 }
620
621 int
in6_mapped_peeraddr(struct socket * so,struct sockaddr * sa)622 in6_mapped_peeraddr(struct socket *so, struct sockaddr *sa)
623 {
624 int error;
625 #ifdef INET
626 struct inpcb *inp;
627
628 inp = sotoinpcb(so);
629 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
630
631 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
632 struct sockaddr_in sin;
633
634 error = in_getpeeraddr(so, (struct sockaddr *)&sin);
635 if (error == 0)
636 in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
637 } else
638 #endif
639 {
640 /* scope issues will be handled in in6_getpeeraddr(). */
641 error = in6_getpeeraddr(so, sa);
642 }
643
644 return error;
645 }
646
647 /*
648 * Pass some notification to all connections of a protocol
649 * associated with address dst. The local address and/or port numbers
650 * may be specified to limit the search. The "usual action" will be
651 * taken, depending on the ctlinput cmd. The caller must filter any
652 * cmds that are uninteresting (e.g., no error in the map).
653 * Call the protocol specific routine (if any) to report
654 * any errors for each matching socket.
655 */
656 static bool
inp_match6(const struct inpcb * inp,void * v __unused)657 inp_match6(const struct inpcb *inp, void *v __unused)
658 {
659
660 return ((inp->inp_vflag & INP_IPV6) != 0);
661 }
662
663 void
in6_pcbnotify(struct inpcbinfo * pcbinfo,struct sockaddr_in6 * sa6_dst,u_int fport_arg,const struct sockaddr_in6 * src,u_int lport_arg,int errno,void * cmdarg,struct inpcb * (* notify)(struct inpcb *,int))664 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr_in6 *sa6_dst,
665 u_int fport_arg, const struct sockaddr_in6 *src, u_int lport_arg,
666 int errno, void *cmdarg,
667 struct inpcb *(*notify)(struct inpcb *, int))
668 {
669 struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB,
670 inp_match6, NULL);
671 struct inpcb *inp;
672 struct sockaddr_in6 sa6_src;
673 u_short fport = fport_arg, lport = lport_arg;
674 u_int32_t flowinfo;
675
676 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
677 return;
678
679 /*
680 * note that src can be NULL when we get notify by local fragmentation.
681 */
682 sa6_src = (src == NULL) ? sa6_any : *src;
683 flowinfo = sa6_src.sin6_flowinfo;
684
685 while ((inp = inp_next(&inpi)) != NULL) {
686 INP_WLOCK_ASSERT(inp);
687 /*
688 * If the error designates a new path MTU for a destination
689 * and the application (associated with this socket) wanted to
690 * know the value, notify.
691 * XXX: should we avoid to notify the value to TCP sockets?
692 */
693 if (errno == EMSGSIZE && cmdarg != NULL)
694 ip6_notify_pmtu(inp, sa6_dst, *(uint32_t *)cmdarg);
695
696 /*
697 * Detect if we should notify the error. If no source and
698 * destination ports are specified, but non-zero flowinfo and
699 * local address match, notify the error. This is the case
700 * when the error is delivered with an encrypted buffer
701 * by ESP. Otherwise, just compare addresses and ports
702 * as usual.
703 */
704 if (lport == 0 && fport == 0 && flowinfo &&
705 inp->inp_socket != NULL &&
706 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
707 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
708 goto do_notify;
709 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
710 &sa6_dst->sin6_addr) ||
711 inp->inp_socket == 0 ||
712 (lport && inp->inp_lport != lport) ||
713 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
714 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
715 &sa6_src.sin6_addr)) ||
716 (fport && inp->inp_fport != fport)) {
717 continue;
718 }
719
720 do_notify:
721 if (notify)
722 (*notify)(inp, errno);
723 }
724 }
725
726 /*
727 * Lookup a PCB based on the local address and port. Caller must hold the
728 * hash lock. No inpcb locks or references are acquired.
729 */
730 struct inpcb *
in6_pcblookup_local(struct inpcbinfo * pcbinfo,const struct in6_addr * laddr,u_short lport,int fib,int lookupflags,struct ucred * cred)731 in6_pcblookup_local(struct inpcbinfo *pcbinfo, const struct in6_addr *laddr,
732 u_short lport, int fib, int lookupflags, struct ucred *cred)
733 {
734 struct inpcb *inp;
735 int matchwild = 3, wildcard;
736
737 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
738 ("%s: invalid lookup flags %d", __func__, lookupflags));
739 KASSERT(fib == RT_ALL_FIBS || (fib >= 0 && fib < V_rt_numfibs),
740 ("%s: invalid fib %d", __func__, fib));
741
742 INP_HASH_LOCK_ASSERT(pcbinfo);
743
744 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
745 struct inpcbhead *head;
746 /*
747 * Look for an unconnected (wildcard foreign addr) PCB that
748 * matches the local address and port we're looking for.
749 */
750 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
751 pcbinfo->ipi_hashmask)];
752 CK_LIST_FOREACH(inp, head, inp_hash_wild) {
753 /* XXX inp locking */
754 if ((inp->inp_vflag & INP_IPV6) == 0)
755 continue;
756 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
757 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
758 inp->inp_lport == lport && (fib == RT_ALL_FIBS ||
759 inp->inp_inc.inc_fibnum == fib)) {
760 /* Found. */
761 if (prison_equal_ip6(cred->cr_prison,
762 inp->inp_cred->cr_prison))
763 return (inp);
764 }
765 }
766 /*
767 * Not found.
768 */
769 return (NULL);
770 } else {
771 struct inpcbhead *porthash;
772 struct inpcb *match = NULL;
773
774 /*
775 * Port is in use by one or more PCBs. Look for best
776 * fit.
777 */
778 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
779 pcbinfo->ipi_porthashmask)];
780 CK_LIST_FOREACH(inp, porthash, inp_portlist) {
781 if (inp->inp_lport != lport)
782 continue;
783 if (!prison_equal_ip6(cred->cr_prison,
784 inp->inp_cred->cr_prison))
785 continue;
786 /* XXX inp locking */
787 if ((inp->inp_vflag & INP_IPV6) == 0)
788 continue;
789 if (fib != RT_ALL_FIBS &&
790 inp->inp_inc.inc_fibnum != fib)
791 continue;
792 wildcard = 0;
793 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
794 wildcard++;
795 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
796 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
797 wildcard++;
798 else if (!IN6_ARE_ADDR_EQUAL(
799 &inp->in6p_laddr, laddr))
800 continue;
801 } else {
802 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
803 wildcard++;
804 }
805 if (wildcard < matchwild) {
806 match = inp;
807 matchwild = wildcard;
808 if (matchwild == 0)
809 break;
810 }
811 }
812 return (match);
813 }
814 }
815
816 static bool
in6_multi_match(const struct inpcb * inp,void * v __unused)817 in6_multi_match(const struct inpcb *inp, void *v __unused)
818 {
819
820 if ((inp->inp_vflag & INP_IPV6) && inp->in6p_moptions != NULL)
821 return (true);
822 else
823 return (false);
824 }
825
826 void
in6_pcbpurgeif0(struct inpcbinfo * pcbinfo,struct ifnet * ifp)827 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
828 {
829 struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_RLOCKPCB,
830 in6_multi_match, NULL);
831 struct inpcb *inp;
832 struct in6_multi *inm;
833 struct in6_mfilter *imf;
834 struct ip6_moptions *im6o;
835
836 IN6_MULTI_LOCK_ASSERT();
837
838 while ((inp = inp_next(&inpi)) != NULL) {
839 INP_RLOCK_ASSERT(inp);
840
841 im6o = inp->in6p_moptions;
842 /*
843 * Unselect the outgoing ifp for multicast if it
844 * is being detached.
845 */
846 if (im6o->im6o_multicast_ifp == ifp)
847 im6o->im6o_multicast_ifp = NULL;
848 /*
849 * Drop multicast group membership if we joined
850 * through the interface being detached.
851 */
852 restart:
853 IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) {
854 if ((inm = imf->im6f_in6m) == NULL)
855 continue;
856 if (inm->in6m_ifp != ifp)
857 continue;
858 ip6_mfilter_remove(&im6o->im6o_head, imf);
859 in6_leavegroup_locked(inm, NULL);
860 ip6_mfilter_free(imf);
861 goto restart;
862 }
863 }
864 }
865
866 /*
867 * Check for alternatives when higher level complains
868 * about service problems. For now, invalidate cached
869 * routing information. If the route was created dynamically
870 * (by a redirect), time to try a default gateway again.
871 */
872 void
in6_losing(struct inpcb * inp)873 in6_losing(struct inpcb *inp)
874 {
875
876 RO_INVALIDATE_CACHE(&inp->inp_route6);
877 }
878
879 /*
880 * After a routing change, flush old routing
881 * and allocate a (hopefully) better one.
882 */
883 struct inpcb *
in6_rtchange(struct inpcb * inp,int errno __unused)884 in6_rtchange(struct inpcb *inp, int errno __unused)
885 {
886
887 RO_INVALIDATE_CACHE(&inp->inp_route6);
888 return inp;
889 }
890
891 static bool
in6_pcblookup_lb_match(const struct inpcblbgroup * grp,int domain,int fib)892 in6_pcblookup_lb_match(const struct inpcblbgroup *grp, int domain, int fib)
893 {
894 return ((domain == M_NODOM || domain == grp->il_numa_domain) &&
895 (fib == RT_ALL_FIBS || fib == grp->il_fibnum));
896 }
897
898 static struct inpcb *
in6_pcblookup_lbgroup(const struct inpcbinfo * pcbinfo,const struct in6_addr * faddr,uint16_t fport,const struct in6_addr * laddr,uint16_t lport,uint8_t domain,int fib)899 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
900 const struct in6_addr *faddr, uint16_t fport, const struct in6_addr *laddr,
901 uint16_t lport, uint8_t domain, int fib)
902 {
903 const struct inpcblbgrouphead *hdr;
904 struct inpcblbgroup *grp;
905 struct inpcblbgroup *jail_exact, *jail_wild, *local_exact, *local_wild;
906 struct inpcb *inp;
907 u_int count;
908
909 INP_HASH_LOCK_ASSERT(pcbinfo);
910 NET_EPOCH_ASSERT();
911
912 hdr = &pcbinfo->ipi_lbgrouphashbase[
913 INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
914
915 /*
916 * Search for an LB group match based on the following criteria:
917 * - prefer jailed groups to non-jailed groups
918 * - prefer exact source address matches to wildcard matches
919 * - prefer groups bound to the specified NUMA domain
920 */
921 jail_exact = jail_wild = local_exact = local_wild = NULL;
922 CK_LIST_FOREACH(grp, hdr, il_list) {
923 bool injail;
924
925 #ifdef INET
926 if (!(grp->il_vflag & INP_IPV6))
927 continue;
928 #endif
929 if (grp->il_lport != lport)
930 continue;
931
932 injail = prison_flag(grp->il_cred, PR_IP6) != 0;
933 if (injail && prison_check_ip6_locked(grp->il_cred->cr_prison,
934 laddr) != 0)
935 continue;
936
937 if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr)) {
938 if (injail) {
939 jail_exact = grp;
940 if (in6_pcblookup_lb_match(grp, domain, fib))
941 /* This is a perfect match. */
942 goto out;
943 } else if (local_exact == NULL ||
944 in6_pcblookup_lb_match(grp, domain, fib)) {
945 local_exact = grp;
946 }
947 } else if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr)) {
948 if (injail) {
949 if (jail_wild == NULL ||
950 in6_pcblookup_lb_match(grp, domain, fib))
951 jail_wild = grp;
952 } else if (local_wild == NULL ||
953 in6_pcblookup_lb_match(grp, domain, fib)) {
954 local_wild = grp;
955 }
956 }
957 }
958
959 if (jail_exact != NULL)
960 grp = jail_exact;
961 else if (jail_wild != NULL)
962 grp = jail_wild;
963 else if (local_exact != NULL)
964 grp = local_exact;
965 else
966 grp = local_wild;
967 if (grp == NULL)
968 return (NULL);
969 out:
970 /*
971 * Synchronize with in_pcblbgroup_insert().
972 */
973 count = atomic_load_acq_int(&grp->il_inpcnt);
974 if (count == 0)
975 return (NULL);
976 inp = grp->il_inp[INP6_PCBLBGROUP_PKTHASH(faddr, lport, fport) % count];
977 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
978 return (inp);
979 }
980
981 static bool
in6_pcblookup_exact_match(const struct inpcb * inp,const struct in6_addr * faddr,u_short fport,const struct in6_addr * laddr,u_short lport)982 in6_pcblookup_exact_match(const struct inpcb *inp, const struct in6_addr *faddr,
983 u_short fport, const struct in6_addr *laddr, u_short lport)
984 {
985 /* XXX inp locking */
986 if ((inp->inp_vflag & INP_IPV6) == 0)
987 return (false);
988 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
989 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
990 inp->inp_fport == fport && inp->inp_lport == lport)
991 return (true);
992 return (false);
993 }
994
995 static struct inpcb *
in6_pcblookup_hash_exact(struct inpcbinfo * pcbinfo,const struct in6_addr * faddr,u_short fport,const struct in6_addr * laddr,u_short lport)996 in6_pcblookup_hash_exact(struct inpcbinfo *pcbinfo,
997 const struct in6_addr *faddr, u_short fport,
998 const struct in6_addr *laddr, u_short lport)
999 {
1000 struct inpcbhead *head;
1001 struct inpcb *inp;
1002
1003 INP_HASH_LOCK_ASSERT(pcbinfo);
1004
1005 /*
1006 * First look for an exact match.
1007 */
1008 head = &pcbinfo->ipi_hash_exact[INP6_PCBHASH(faddr, lport, fport,
1009 pcbinfo->ipi_hashmask)];
1010 CK_LIST_FOREACH(inp, head, inp_hash_exact) {
1011 if (in6_pcblookup_exact_match(inp, faddr, fport, laddr, lport))
1012 return (inp);
1013 }
1014 return (NULL);
1015 }
1016
1017 typedef enum {
1018 INPLOOKUP_MATCH_NONE = 0,
1019 INPLOOKUP_MATCH_WILD = 1,
1020 INPLOOKUP_MATCH_LADDR = 2,
1021 } inp_lookup_match_t;
1022
1023 static inp_lookup_match_t
in6_pcblookup_wild_match(const struct inpcb * inp,const struct in6_addr * laddr,u_short lport,int fib)1024 in6_pcblookup_wild_match(const struct inpcb *inp, const struct in6_addr *laddr,
1025 u_short lport, int fib)
1026 {
1027 /* XXX inp locking */
1028 if ((inp->inp_vflag & INP_IPV6) == 0)
1029 return (INPLOOKUP_MATCH_NONE);
1030 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1031 inp->inp_lport != lport)
1032 return (INPLOOKUP_MATCH_NONE);
1033 if (fib != RT_ALL_FIBS && inp->inp_inc.inc_fibnum != fib)
1034 return (INPLOOKUP_MATCH_NONE);
1035 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1036 return (INPLOOKUP_MATCH_WILD);
1037 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr))
1038 return (INPLOOKUP_MATCH_LADDR);
1039 return (INPLOOKUP_MATCH_NONE);
1040 }
1041
1042 #define INP_LOOKUP_AGAIN ((struct inpcb *)(uintptr_t)-1)
1043
1044 static struct inpcb *
in6_pcblookup_hash_wild_smr(struct inpcbinfo * pcbinfo,const struct in6_addr * laddr,u_short lport,int fib,const inp_lookup_t lockflags)1045 in6_pcblookup_hash_wild_smr(struct inpcbinfo *pcbinfo,
1046 const struct in6_addr *laddr, u_short lport, int fib,
1047 const inp_lookup_t lockflags)
1048 {
1049 struct inpcbhead *head;
1050 struct inpcb *inp;
1051
1052 KASSERT(SMR_ENTERED(pcbinfo->ipi_smr),
1053 ("%s: not in SMR read section", __func__));
1054
1055 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
1056 pcbinfo->ipi_hashmask)];
1057 CK_LIST_FOREACH(inp, head, inp_hash_wild) {
1058 inp_lookup_match_t match;
1059
1060 match = in6_pcblookup_wild_match(inp, laddr, lport, fib);
1061 if (match == INPLOOKUP_MATCH_NONE)
1062 continue;
1063
1064 if (__predict_true(inp_smr_lock(inp, lockflags))) {
1065 match = in6_pcblookup_wild_match(inp, laddr, lport,
1066 fib);
1067 if (match != INPLOOKUP_MATCH_NONE &&
1068 prison_check_ip6_locked(inp->inp_cred->cr_prison,
1069 laddr) == 0)
1070 return (inp);
1071 inp_unlock(inp, lockflags);
1072 }
1073
1074 /*
1075 * The matching socket disappeared out from under us. Fall back
1076 * to a serialized lookup.
1077 */
1078 return (INP_LOOKUP_AGAIN);
1079 }
1080 return (NULL);
1081 }
1082
1083 static struct inpcb *
in6_pcblookup_hash_wild_locked(struct inpcbinfo * pcbinfo,const struct in6_addr * laddr,u_short lport,int fib)1084 in6_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo,
1085 const struct in6_addr *laddr, u_short lport, int fib)
1086 {
1087 struct inpcbhead *head;
1088 struct inpcb *inp, *jail_wild, *local_exact, *local_wild;
1089
1090 INP_HASH_LOCK_ASSERT(pcbinfo);
1091
1092 /*
1093 * Order of socket selection - we always prefer jails.
1094 * 1. jailed, non-wild.
1095 * 2. jailed, wild.
1096 * 3. non-jailed, non-wild.
1097 * 4. non-jailed, wild.
1098 */
1099 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
1100 pcbinfo->ipi_hashmask)];
1101 local_wild = local_exact = jail_wild = NULL;
1102 CK_LIST_FOREACH(inp, head, inp_hash_wild) {
1103 inp_lookup_match_t match;
1104 bool injail;
1105
1106 match = in6_pcblookup_wild_match(inp, laddr, lport, fib);
1107 if (match == INPLOOKUP_MATCH_NONE)
1108 continue;
1109
1110 injail = prison_flag(inp->inp_cred, PR_IP6) != 0;
1111 if (injail) {
1112 if (prison_check_ip6_locked(
1113 inp->inp_cred->cr_prison, laddr) != 0)
1114 continue;
1115 } else {
1116 if (local_exact != NULL)
1117 continue;
1118 }
1119
1120 if (match == INPLOOKUP_MATCH_LADDR) {
1121 if (injail)
1122 return (inp);
1123 else
1124 local_exact = inp;
1125 } else {
1126 if (injail)
1127 jail_wild = inp;
1128 else
1129 local_wild = inp;
1130 }
1131 }
1132
1133 if (jail_wild != NULL)
1134 return (jail_wild);
1135 if (local_exact != NULL)
1136 return (local_exact);
1137 if (local_wild != NULL)
1138 return (local_wild);
1139 return (NULL);
1140 }
1141
1142 struct inpcb *
in6_pcblookup_hash_locked(struct inpcbinfo * pcbinfo,const struct in6_addr * faddr,u_int fport_arg,const struct in6_addr * laddr,u_int lport_arg,int lookupflags,uint8_t numa_domain,int fib)1143 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
1144 const struct in6_addr *faddr, u_int fport_arg,
1145 const struct in6_addr *laddr, u_int lport_arg,
1146 int lookupflags, uint8_t numa_domain, int fib)
1147 {
1148 struct inpcb *inp;
1149 u_short fport = fport_arg, lport = lport_arg;
1150
1151 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD | INPLOOKUP_FIB)) == 0,
1152 ("%s: invalid lookup flags %d", __func__, lookupflags));
1153 KASSERT(!IN6_IS_ADDR_UNSPECIFIED(faddr),
1154 ("%s: invalid foreign address", __func__));
1155 KASSERT(!IN6_IS_ADDR_UNSPECIFIED(laddr),
1156 ("%s: invalid local address", __func__));
1157 INP_HASH_LOCK_ASSERT(pcbinfo);
1158
1159 inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport);
1160 if (inp != NULL)
1161 return (inp);
1162
1163 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1164 inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport, laddr,
1165 lport, numa_domain, fib);
1166 if (inp == NULL) {
1167 inp = in6_pcblookup_hash_wild_locked(pcbinfo,
1168 laddr, lport, fib);
1169 }
1170 }
1171 return (inp);
1172 }
1173
1174 static struct inpcb *
in6_pcblookup_hash(struct inpcbinfo * pcbinfo,const struct in6_addr * faddr,u_int fport,const struct in6_addr * laddr,u_int lport,int lookupflags,uint8_t numa_domain,int fib)1175 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr,
1176 u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags,
1177 uint8_t numa_domain, int fib)
1178 {
1179 struct inpcb *inp;
1180 const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK;
1181
1182 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1183 ("%s: LOCKPCB not set", __func__));
1184
1185 INP_HASH_WLOCK(pcbinfo);
1186 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1187 lookupflags & ~INPLOOKUP_LOCKMASK, numa_domain, fib);
1188 if (inp != NULL && !inp_trylock(inp, lockflags)) {
1189 in_pcbref(inp);
1190 INP_HASH_WUNLOCK(pcbinfo);
1191 inp_lock(inp, lockflags);
1192 if (in_pcbrele(inp, lockflags))
1193 /* XXX-MJ or retry until we get a negative match? */
1194 inp = NULL;
1195 } else {
1196 INP_HASH_WUNLOCK(pcbinfo);
1197 }
1198 return (inp);
1199 }
1200
1201 static struct inpcb *
in6_pcblookup_hash_smr(struct inpcbinfo * pcbinfo,const struct in6_addr * faddr,u_int fport_arg,const struct in6_addr * laddr,u_int lport_arg,int lookupflags,uint8_t numa_domain,int fib)1202 in6_pcblookup_hash_smr(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr,
1203 u_int fport_arg, const struct in6_addr *laddr, u_int lport_arg,
1204 int lookupflags, uint8_t numa_domain, int fib)
1205 {
1206 struct inpcb *inp;
1207 const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK;
1208 const u_short fport = fport_arg, lport = lport_arg;
1209
1210 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1211 ("%s: invalid lookup flags %d", __func__, lookupflags));
1212 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1213 ("%s: LOCKPCB not set", __func__));
1214
1215 smr_enter(pcbinfo->ipi_smr);
1216 inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport);
1217 if (inp != NULL) {
1218 if (__predict_true(inp_smr_lock(inp, lockflags))) {
1219 if (__predict_true(in6_pcblookup_exact_match(inp,
1220 faddr, fport, laddr, lport)))
1221 return (inp);
1222 inp_unlock(inp, lockflags);
1223 }
1224 /*
1225 * We failed to lock the inpcb, or its connection state changed
1226 * out from under us. Fall back to a precise search.
1227 */
1228 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1229 lookupflags, numa_domain, fib));
1230 }
1231
1232 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1233 inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport,
1234 laddr, lport, numa_domain, fib);
1235 if (inp != NULL) {
1236 if (__predict_true(inp_smr_lock(inp, lockflags))) {
1237 if (__predict_true(in6_pcblookup_wild_match(inp,
1238 laddr, lport, fib) != INPLOOKUP_MATCH_NONE))
1239 return (inp);
1240 inp_unlock(inp, lockflags);
1241 }
1242 inp = INP_LOOKUP_AGAIN;
1243 } else {
1244 inp = in6_pcblookup_hash_wild_smr(pcbinfo, laddr, lport,
1245 fib, lockflags);
1246 }
1247 if (inp == INP_LOOKUP_AGAIN) {
1248 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr,
1249 lport, lookupflags, numa_domain, fib));
1250 }
1251 }
1252
1253 if (inp == NULL)
1254 smr_exit(pcbinfo->ipi_smr);
1255
1256 return (inp);
1257 }
1258
1259 /*
1260 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1261 * from which a pre-calculated hash value may be extracted.
1262 */
1263 struct inpcb *
in6_pcblookup(struct inpcbinfo * pcbinfo,const struct in6_addr * faddr,u_int fport,const struct in6_addr * laddr,u_int lport,int lookupflags,struct ifnet * ifp)1264 in6_pcblookup(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr,
1265 u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags,
1266 struct ifnet *ifp)
1267 {
1268 int fib;
1269
1270 fib = (lookupflags & INPLOOKUP_FIB) ? if_getfib(ifp) : RT_ALL_FIBS;
1271 return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport,
1272 lookupflags, M_NODOM, fib));
1273 }
1274
1275 struct inpcb *
in6_pcblookup_mbuf(struct inpcbinfo * pcbinfo,const struct in6_addr * faddr,u_int fport,const struct in6_addr * laddr,u_int lport,int lookupflags,struct ifnet * ifp __unused,struct mbuf * m)1276 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr,
1277 u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags,
1278 struct ifnet *ifp __unused, struct mbuf *m)
1279 {
1280 int fib;
1281
1282 M_ASSERTPKTHDR(m);
1283 fib = (lookupflags & INPLOOKUP_FIB) ? M_GETFIB(m) : RT_ALL_FIBS;
1284 return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport,
1285 lookupflags, m->m_pkthdr.numa_domain, fib));
1286 }
1287
1288 void
init_sin6(struct sockaddr_in6 * sin6,struct mbuf * m,int srcordst)1289 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
1290 {
1291 struct ip6_hdr *ip;
1292
1293 ip = mtod(m, struct ip6_hdr *);
1294 bzero(sin6, sizeof(*sin6));
1295 sin6->sin6_len = sizeof(*sin6);
1296 sin6->sin6_family = AF_INET6;
1297 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src;
1298
1299 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
1300
1301 return;
1302 }
1303