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