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