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