in6_pcb.c revision 1.170 1 /* $NetBSD: in6_pcb.c,v 1.170 2022/08/29 09:14:02 knakahara Exp $ */
2 /* $KAME: in6_pcb.c,v 1.84 2001/02/08 18:02:08 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
62 */
63
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: in6_pcb.c,v 1.170 2022/08/29 09:14:02 knakahara Exp $");
66
67 #ifdef _KERNEL_OPT
68 #include "opt_inet.h"
69 #include "opt_ipsec.h"
70 #endif
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/mbuf.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/ioctl.h>
79 #include <sys/errno.h>
80 #include <sys/time.h>
81 #include <sys/proc.h>
82 #include <sys/kauth.h>
83 #include <sys/domain.h>
84 #include <sys/once.h>
85
86 #include <net/if.h>
87 #include <net/route.h>
88
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/ip6.h>
95 #include <netinet/portalgo.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet6/in6_pcb.h>
98 #include <netinet6/scope6_var.h>
99
100 #include "faith.h"
101
102 #ifdef IPSEC
103 #include <netipsec/ipsec.h>
104 #include <netipsec/ipsec6.h>
105 #include <netipsec/key.h>
106 #endif /* IPSEC */
107
108 #include <netinet/tcp_vtw.h>
109
110 const struct in6_addr zeroin6_addr;
111
112 #define IN6PCBHASH_PORT(table, lport) \
113 &(table)->inpt_porthashtbl[ntohs(lport) & (table)->inpt_porthash]
114 #define IN6PCBHASH_BIND(table, laddr, lport) \
115 &(table)->inpt_bindhashtbl[ \
116 (((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \
117 (laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + ntohs(lport)) & \
118 (table)->inpt_bindhash]
119 #define IN6PCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
120 &(table)->inpt_bindhashtbl[ \
121 ((((faddr)->s6_addr32[0] ^ (faddr)->s6_addr32[1] ^ \
122 (faddr)->s6_addr32[2] ^ (faddr)->s6_addr32[3]) + ntohs(fport)) + \
123 (((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \
124 (laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + \
125 ntohs(lport))) & (table)->inpt_bindhash]
126
127 int ip6_anonportmin = IPV6PORT_ANONMIN;
128 int ip6_anonportmax = IPV6PORT_ANONMAX;
129 int ip6_lowportmin = IPV6PORT_RESERVEDMIN;
130 int ip6_lowportmax = IPV6PORT_RESERVEDMAX;
131
132 static struct pool in6pcb_pool;
133
134 static int
135 in6pcb_poolinit(void)
136 {
137
138 pool_init(&in6pcb_pool, sizeof(struct in6pcb), 0, 0, 0, "in6pcbpl",
139 NULL, IPL_SOFTNET);
140 return 0;
141 }
142
143 void
144 in6_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize)
145 {
146 static ONCE_DECL(control);
147
148 in_pcbinit(table, bindhashsize, connecthashsize);
149 table->inpt_lastport = (u_int16_t)ip6_anonportmax;
150
151 RUN_ONCE(&control, in6pcb_poolinit);
152 }
153
154 int
155 in6_pcballoc(struct socket *so, void *v)
156 {
157 struct inpcbtable *table = v;
158 struct in6pcb *in6p;
159 int s;
160
161 KASSERT(so->so_proto->pr_domain->dom_family == AF_INET6);
162
163 in6p = pool_get(&in6pcb_pool, PR_NOWAIT);
164 if (in6p == NULL)
165 return (ENOBUFS);
166 memset((void *)in6p, 0, sizeof(*in6p));
167 in6p->in6p_af = AF_INET6;
168 in6p->in6p_table = table;
169 in6p->in6p_socket = so;
170 in6p->in6p_hops = -1; /* use kernel default */
171 in6p->in6p_icmp6filt = NULL;
172 in6p->in6p_portalgo = PORTALGO_DEFAULT;
173 in6p->in6p_bindportonsend = false;
174 #if defined(IPSEC)
175 if (ipsec_enabled) {
176 int error = ipsec_init_pcbpolicy(so, &in6p->in6p_sp);
177 if (error != 0) {
178 pool_put(&in6pcb_pool, in6p);
179 return error;
180 }
181 in6p->in6p_sp->sp_inph = (struct inpcb_hdr *)in6p;
182 }
183 #endif /* IPSEC */
184 s = splsoftnet();
185 TAILQ_INSERT_HEAD(&table->inpt_queue, (struct inpcb_hdr*)in6p,
186 inph_queue);
187 LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport),
188 &in6p->in6p_head, inph_lhash);
189 in6_pcbstate(in6p, IN6P_ATTACHED);
190 splx(s);
191 if (ip6_v6only)
192 in6p->in6p_flags |= IN6P_IPV6_V6ONLY;
193 so->so_pcb = (void *)in6p;
194 return (0);
195 }
196
197 /*
198 * Bind address from sin6 to in6p.
199 */
200 static int
201 in6_pcbbind_addr(struct in6pcb *in6p, struct sockaddr_in6 *sin6, struct lwp *l)
202 {
203 int error;
204 int s;
205
206 /*
207 * We should check the family, but old programs
208 * incorrectly fail to initialize it.
209 */
210 if (sin6->sin6_family != AF_INET6)
211 return (EAFNOSUPPORT);
212
213 #ifndef INET
214 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
215 return (EADDRNOTAVAIL);
216 #endif
217
218 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
219 return (error);
220
221 s = pserialize_read_enter();
222 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
223 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) {
224 error = EINVAL;
225 goto out;
226 }
227 if (sin6->sin6_addr.s6_addr32[3]) {
228 struct sockaddr_in sin;
229
230 memset(&sin, 0, sizeof(sin));
231 sin.sin_len = sizeof(sin);
232 sin.sin_family = AF_INET;
233 bcopy(&sin6->sin6_addr.s6_addr32[3],
234 &sin.sin_addr, sizeof(sin.sin_addr));
235 if (!IN_MULTICAST(sin.sin_addr.s_addr)) {
236 struct ifaddr *ifa;
237 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
238 if (ifa == NULL &&
239 (in6p->in6p_flags & IN6P_BINDANY) == 0) {
240 error = EADDRNOTAVAIL;
241 goto out;
242 }
243 }
244 }
245 } else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
246 // succeed
247 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
248 struct ifaddr *ifa = NULL;
249
250 if ((in6p->in6p_flags & IN6P_FAITH) == 0) {
251 ifa = ifa_ifwithaddr(sin6tosa(sin6));
252 if (ifa == NULL &&
253 (in6p->in6p_flags & IN6P_BINDANY) == 0) {
254 error = EADDRNOTAVAIL;
255 goto out;
256 }
257 }
258
259 /*
260 * bind to an anycast address might accidentally
261 * cause sending a packet with an anycast source
262 * address, so we forbid it.
263 *
264 * We should allow to bind to a deprecated address,
265 * since the application dare to use it.
266 * But, can we assume that they are careful enough
267 * to check if the address is deprecated or not?
268 * Maybe, as a safeguard, we should have a setsockopt
269 * flag to control the bind(2) behavior against
270 * deprecated addresses (default: forbid bind(2)).
271 */
272 if (ifa &&
273 ifatoia6(ifa)->ia6_flags &
274 (IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED)) {
275 error = EADDRNOTAVAIL;
276 goto out;
277 }
278 }
279 in6p->in6p_laddr = sin6->sin6_addr;
280 error = 0;
281 out:
282 pserialize_read_exit(s);
283 return error;
284 }
285
286 /*
287 * Bind port from sin6 to in6p.
288 */
289 static int
290 in6_pcbbind_port(struct in6pcb *in6p, struct sockaddr_in6 *sin6, struct lwp *l)
291 {
292 struct inpcbtable *table = in6p->in6p_table;
293 struct socket *so = in6p->in6p_socket;
294 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
295 int error;
296
297 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
298 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
299 (so->so_options & SO_ACCEPTCONN) == 0))
300 wild = 1;
301
302 if (sin6->sin6_port != 0) {
303 enum kauth_network_req req;
304
305 #ifndef IPNOPRIVPORTS
306 if (ntohs(sin6->sin6_port) < IPV6PORT_RESERVED)
307 req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
308 else
309 #endif /* IPNOPRIVPORTS */
310 req = KAUTH_REQ_NETWORK_BIND_PORT;
311
312 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_BIND,
313 req, so, sin6, NULL);
314 if (error)
315 return (EACCES);
316 }
317
318 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
319 /*
320 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
321 * allow compepte duplication of binding if
322 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
323 * and a multicast address is bound on both
324 * new and duplicated sockets.
325 */
326 if (so->so_options & (SO_REUSEADDR | SO_REUSEPORT))
327 reuseport = SO_REUSEADDR|SO_REUSEPORT;
328 }
329
330 if (sin6->sin6_port != 0) {
331 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
332 #ifdef INET
333 struct inpcb *t;
334 struct vestigial_inpcb vestige;
335
336 t = in_pcblookup_port(table,
337 *(struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
338 sin6->sin6_port, wild, &vestige);
339 if (t && (reuseport & t->inp_socket->so_options) == 0)
340 return (EADDRINUSE);
341 if (!t
342 && vestige.valid
343 && !(reuseport && vestige.reuse_port))
344 return EADDRINUSE;
345 #else
346 return (EADDRNOTAVAIL);
347 #endif
348 }
349
350 {
351 struct in6pcb *t;
352 struct vestigial_inpcb vestige;
353
354 t = in6_pcblookup_port(table, &sin6->sin6_addr,
355 sin6->sin6_port, wild, &vestige);
356 if (t && (reuseport & t->in6p_socket->so_options) == 0)
357 return (EADDRINUSE);
358 if (!t
359 && vestige.valid
360 && !(reuseport && vestige.reuse_port))
361 return EADDRINUSE;
362 }
363 }
364
365 if (sin6->sin6_port == 0) {
366 int e;
367 e = in6_pcbsetport(sin6, in6p, l);
368 if (e != 0)
369 return (e);
370 } else {
371 in6p->in6p_lport = sin6->sin6_port;
372 in6_pcbstate(in6p, IN6P_BOUND);
373 }
374
375 LIST_REMOVE(&in6p->in6p_head, inph_lhash);
376 LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport),
377 &in6p->in6p_head, inph_lhash);
378
379 return (0);
380 }
381
382 int
383 in6_pcbbind(void *v, struct sockaddr_in6 *sin6, struct lwp *l)
384 {
385 struct in6pcb *in6p = v;
386 struct sockaddr_in6 lsin6;
387 int error;
388
389 if (in6p->in6p_af != AF_INET6)
390 return (EINVAL);
391
392 /*
393 * If we already have a local port or a local address it means we're
394 * bounded.
395 */
396 if (in6p->in6p_lport || !(IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ||
397 (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
398 in6p->in6p_laddr.s6_addr32[3] == 0)))
399 return (EINVAL);
400
401 if (NULL != sin6) {
402 /* We were provided a sockaddr_in6 to use. */
403 if (sin6->sin6_len != sizeof(*sin6))
404 return (EINVAL);
405 } else {
406 /* We always bind to *something*, even if it's "anything". */
407 lsin6 = *((const struct sockaddr_in6 *)
408 in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
409 sin6 = &lsin6;
410 }
411
412 /* Bind address. */
413 error = in6_pcbbind_addr(in6p, sin6, l);
414 if (error)
415 return (error);
416
417 /* Bind port. */
418 error = in6_pcbbind_port(in6p, sin6, l);
419 if (error) {
420 /*
421 * Reset the address here to "any" so we don't "leak" the
422 * in6pcb.
423 */
424 in6p->in6p_laddr = in6addr_any;
425
426 return (error);
427 }
428
429
430 #if 0
431 in6p->in6p_flowinfo = 0; /* XXX */
432 #endif
433 return (0);
434 }
435
436 /*
437 * Connect from a socket to a specified address.
438 * Both address and port must be specified in argument sin6.
439 * If don't have a local address for this socket yet,
440 * then pick one.
441 */
442 int
443 in6_pcbconnect(void *v, struct sockaddr_in6 *sin6, struct lwp *l)
444 {
445 struct in6pcb *in6p = v;
446 struct in6_addr *in6a = NULL;
447 struct in6_addr ia6;
448 struct ifnet *ifp = NULL; /* outgoing interface */
449 int error = 0;
450 int scope_ambiguous = 0;
451 #ifdef INET
452 struct in6_addr mapped;
453 #endif
454 struct sockaddr_in6 tmp;
455 struct vestigial_inpcb vestige;
456 struct psref psref;
457 int bound;
458
459 (void)&in6a; /* XXX fool gcc */
460
461 if (in6p->in6p_af != AF_INET6)
462 return (EINVAL);
463
464 if (sin6->sin6_len != sizeof(*sin6))
465 return (EINVAL);
466 if (sin6->sin6_family != AF_INET6)
467 return (EAFNOSUPPORT);
468 if (sin6->sin6_port == 0)
469 return (EADDRNOTAVAIL);
470
471 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
472 in6p->in6p_socket->so_type == SOCK_STREAM)
473 return EADDRNOTAVAIL;
474
475 if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
476 scope_ambiguous = 1;
477 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
478 return(error);
479
480 /* sanity check for mapped address case */
481 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
482 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
483 return EINVAL;
484 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
485 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
486 if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
487 return EINVAL;
488 } else
489 {
490 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
491 return EINVAL;
492 }
493
494 /* protect *sin6 from overwrites */
495 tmp = *sin6;
496 sin6 = &tmp;
497
498 bound = curlwp_bind();
499 /* Source address selection. */
500 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
501 in6p->in6p_laddr.s6_addr32[3] == 0) {
502 #ifdef INET
503 struct sockaddr_in sin;
504 struct in_ifaddr *ia4;
505 struct psref _psref;
506
507 memset(&sin, 0, sizeof(sin));
508 sin.sin_len = sizeof(sin);
509 sin.sin_family = AF_INET;
510 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr32[3],
511 sizeof(sin.sin_addr));
512 ia4 = in_selectsrc(&sin, &in6p->in6p_route,
513 in6p->in6p_socket->so_options, NULL, &error, &_psref);
514 if (ia4 == NULL) {
515 if (error == 0)
516 error = EADDRNOTAVAIL;
517 curlwp_bindx(bound);
518 return (error);
519 }
520 memset(&mapped, 0, sizeof(mapped));
521 mapped.s6_addr16[5] = htons(0xffff);
522 memcpy(&mapped.s6_addr32[3], &IA_SIN(ia4)->sin_addr,
523 sizeof(IA_SIN(ia4)->sin_addr));
524 ia4_release(ia4, &_psref);
525 in6a = &mapped;
526 #else
527 curlwp_bindx(bound);
528 return EADDRNOTAVAIL;
529 #endif
530 } else {
531 /*
532 * XXX: in6_selectsrc might replace the bound local address
533 * with the address specified by setsockopt(IPV6_PKTINFO).
534 * Is it the intended behavior?
535 */
536 error = in6_selectsrc(sin6, in6p->in6p_outputopts,
537 in6p->in6p_moptions, &in6p->in6p_route, &in6p->in6p_laddr,
538 &ifp, &psref, &ia6);
539 if (error == 0)
540 in6a = &ia6;
541 if (ifp && scope_ambiguous &&
542 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
543 if_put(ifp, &psref);
544 curlwp_bindx(bound);
545 return error;
546 }
547
548 if (in6a == NULL) {
549 if_put(ifp, &psref);
550 curlwp_bindx(bound);
551 if (error == 0)
552 error = EADDRNOTAVAIL;
553 return error;
554 }
555 }
556
557 if (ifp != NULL) {
558 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
559 if_put(ifp, &psref);
560 } else
561 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim_rt(in6p);
562 curlwp_bindx(bound);
563
564 if (in6_pcblookup_connect(in6p->in6p_table, &sin6->sin6_addr,
565 sin6->sin6_port,
566 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ? in6a : &in6p->in6p_laddr,
567 in6p->in6p_lport, 0, &vestige)
568 || vestige.valid)
569 return (EADDRINUSE);
570 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ||
571 (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
572 in6p->in6p_laddr.s6_addr32[3] == 0))
573 {
574 if (in6p->in6p_lport == 0) {
575 error = in6_pcbbind(in6p, NULL, l);
576 if (error != 0)
577 return error;
578 }
579 in6p->in6p_laddr = *in6a;
580 }
581 in6p->in6p_faddr = sin6->sin6_addr;
582 in6p->in6p_fport = sin6->sin6_port;
583
584 /* Late bind, if needed */
585 if (in6p->in6p_bindportonsend) {
586 struct sockaddr_in6 lsin = *((const struct sockaddr_in6 *)
587 in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
588 lsin.sin6_addr = in6p->in6p_laddr;
589 lsin.sin6_port = 0;
590
591 if ((error = in6_pcbbind_port(in6p, &lsin, l)) != 0)
592 return error;
593 }
594
595 in6_pcbstate(in6p, IN6P_CONNECTED);
596 in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
597 if (ip6_auto_flowlabel)
598 in6p->in6p_flowinfo |=
599 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
600 #if defined(IPSEC)
601 if (ipsec_enabled && in6p->in6p_socket->so_type == SOCK_STREAM)
602 ipsec_pcbconn(in6p->in6p_sp);
603 #endif
604 return (0);
605 }
606
607 void
608 in6_pcbdisconnect(struct in6pcb *in6p)
609 {
610 memset((void *)&in6p->in6p_faddr, 0, sizeof(in6p->in6p_faddr));
611 in6p->in6p_fport = 0;
612 in6_pcbstate(in6p, IN6P_BOUND);
613 in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
614 #if defined(IPSEC)
615 if (ipsec_enabled)
616 ipsec_pcbdisconn(in6p->in6p_sp);
617 #endif
618 if (in6p->in6p_socket->so_state & SS_NOFDREF)
619 in6_pcbdetach(in6p);
620 }
621
622 void
623 in6_pcbdetach(struct in6pcb *in6p)
624 {
625 struct socket *so = in6p->in6p_socket;
626 int s;
627
628 if (in6p->in6p_af != AF_INET6)
629 return;
630
631 #if defined(IPSEC)
632 if (ipsec_enabled)
633 ipsec_delete_pcbpolicy(in6p);
634 #endif
635 so->so_pcb = NULL;
636
637 s = splsoftnet();
638 in6_pcbstate(in6p, IN6P_ATTACHED);
639 LIST_REMOVE(&in6p->in6p_head, inph_lhash);
640 TAILQ_REMOVE(&in6p->in6p_table->inpt_queue, &in6p->in6p_head,
641 inph_queue);
642 splx(s);
643
644 if (in6p->in6p_options) {
645 m_freem(in6p->in6p_options);
646 }
647 if (in6p->in6p_outputopts != NULL) {
648 ip6_clearpktopts(in6p->in6p_outputopts, -1);
649 free(in6p->in6p_outputopts, M_IP6OPT);
650 }
651 rtcache_free(&in6p->in6p_route);
652 ip6_freemoptions(in6p->in6p_moptions);
653 ip_freemoptions(in6p->in6p_v4moptions);
654 sofree(so); /* drops the socket's lock */
655
656 pool_put(&in6pcb_pool, in6p);
657 mutex_enter(softnet_lock); /* reacquire it */
658 }
659
660 void
661 in6_setsockaddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6)
662 {
663
664 if (in6p->in6p_af != AF_INET6)
665 return;
666
667 sockaddr_in6_init(sin6, &in6p->in6p_laddr, in6p->in6p_lport, 0, 0);
668 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
669 }
670
671 void
672 in6_setpeeraddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6)
673 {
674
675 if (in6p->in6p_af != AF_INET6)
676 return;
677
678 sockaddr_in6_init(sin6, &in6p->in6p_faddr, in6p->in6p_fport, 0, 0);
679 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
680 }
681
682 /*
683 * Pass some notification to all connections of a protocol
684 * associated with address dst. The local address and/or port numbers
685 * may be specified to limit the search. The "usual action" will be
686 * taken, depending on the ctlinput cmd. The caller must filter any
687 * cmds that are uninteresting (e.g., no error in the map).
688 * Call the protocol specific routine (if any) to report
689 * any errors for each matching socket.
690 *
691 * Must be called at splsoftnet.
692 *
693 * Note: src (4th arg) carries the flowlabel value on the original IPv6
694 * header, in sin6_flowinfo member.
695 */
696 int
697 in6_pcbnotify(struct inpcbtable *table, const struct sockaddr *dst,
698 u_int fport_arg, const struct sockaddr *src, u_int lport_arg, int cmd,
699 void *cmdarg, void (*notify)(struct in6pcb *, int))
700 {
701 struct inpcb_hdr *inph;
702 struct sockaddr_in6 sa6_src;
703 const struct sockaddr_in6 *sa6_dst;
704 u_int16_t fport = fport_arg, lport = lport_arg;
705 int errno;
706 int nmatch = 0;
707 u_int32_t flowinfo;
708
709 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
710 return 0;
711
712 sa6_dst = (const struct sockaddr_in6 *)dst;
713 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
714 return 0;
715
716 /*
717 * note that src can be NULL when we get notify by local fragmentation.
718 */
719 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
720 flowinfo = sa6_src.sin6_flowinfo;
721
722 /*
723 * Redirects go to all references to the destination,
724 * and use in6_rtchange to invalidate the route cache.
725 * Dead host indications: also use in6_rtchange to invalidate
726 * the cache, and deliver the error to all the sockets.
727 * Otherwise, if we have knowledge of the local port and address,
728 * deliver only to that socket.
729 */
730 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
731 fport = 0;
732 lport = 0;
733 memset((void *)&sa6_src.sin6_addr, 0, sizeof(sa6_src.sin6_addr));
734
735 if (cmd != PRC_HOSTDEAD)
736 notify = in6_rtchange;
737 }
738
739 errno = inet6ctlerrmap[cmd];
740 TAILQ_FOREACH(inph, &table->inpt_queue, inph_queue) {
741 struct in6pcb *in6p = (struct in6pcb *)inph;
742 struct rtentry *rt = NULL;
743
744 if (in6p->in6p_af != AF_INET6)
745 continue;
746
747 /*
748 * Under the following condition, notify of redirects
749 * to the pcb, without making address matches against inpcb.
750 * - redirect notification is arrived.
751 * - the inpcb is unconnected.
752 * - the inpcb is caching !RTF_HOST routing entry.
753 * - the ICMPv6 notification is from the gateway cached in the
754 * inpcb. i.e. ICMPv6 notification is from nexthop gateway
755 * the inpcb used very recently.
756 *
757 * This is to improve interaction between netbsd/openbsd
758 * redirect handling code, and inpcb route cache code.
759 * without the clause, !RTF_HOST routing entry (which carries
760 * gateway used by inpcb right before the ICMPv6 redirect)
761 * will be cached forever in unconnected inpcb.
762 *
763 * There still is a question regarding to what is TRT:
764 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
765 * generated on packet output. inpcb will always cache
766 * RTF_HOST routing entry so there's no need for the clause
767 * (ICMPv6 redirect will update RTF_HOST routing entry,
768 * and inpcb is caching it already).
769 * However, bsdi/freebsd are vulnerable to local DoS attacks
770 * due to the cloned routing entries.
771 * - Specwise, "destination cache" is mentioned in RFC2461.
772 * Jinmei says that it implies bsdi/freebsd behavior, itojun
773 * is not really convinced.
774 * - Having hiwat/lowat on # of cloned host route (redirect/
775 * pmtud) may be a good idea. netbsd/openbsd has it. see
776 * icmp6_mtudisc_update().
777 */
778 if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
779 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
780 (rt = rtcache_validate(&in6p->in6p_route)) != NULL &&
781 !(rt->rt_flags & RTF_HOST)) {
782 const struct sockaddr_in6 *dst6;
783
784 dst6 = (const struct sockaddr_in6 *)
785 rtcache_getdst(&in6p->in6p_route);
786 if (dst6 == NULL)
787 ;
788 else if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
789 &sa6_dst->sin6_addr)) {
790 rtcache_unref(rt, &in6p->in6p_route);
791 goto do_notify;
792 }
793 }
794 rtcache_unref(rt, &in6p->in6p_route);
795
796 /*
797 * If the error designates a new path MTU for a destination
798 * and the application (associated with this socket) wanted to
799 * know the value, notify. Note that we notify for all
800 * disconnected sockets if the corresponding application
801 * wanted. This is because some UDP applications keep sending
802 * sockets disconnected.
803 * XXX: should we avoid to notify the value to TCP sockets?
804 */
805 if (cmd == PRC_MSGSIZE && (in6p->in6p_flags & IN6P_MTU) != 0 &&
806 (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) ||
807 IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &sa6_dst->sin6_addr))) {
808 ip6_notify_pmtu(in6p, (const struct sockaddr_in6 *)dst,
809 (u_int32_t *)cmdarg);
810 }
811
812 /*
813 * Detect if we should notify the error. If no source and
814 * destination ports are specified, but non-zero flowinfo and
815 * local address match, notify the error. This is the case
816 * when the error is delivered with an encrypted buffer
817 * by ESP. Otherwise, just compare addresses and ports
818 * as usual.
819 */
820 if (lport == 0 && fport == 0 && flowinfo &&
821 in6p->in6p_socket != NULL &&
822 flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
823 IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr))
824 goto do_notify;
825 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
826 &sa6_dst->sin6_addr) ||
827 in6p->in6p_socket == NULL ||
828 (lport && in6p->in6p_lport != lport) ||
829 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
830 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
831 &sa6_src.sin6_addr)) ||
832 (fport && in6p->in6p_fport != fport))
833 continue;
834
835 do_notify:
836 if (notify)
837 (*notify)(in6p, errno);
838 nmatch++;
839 }
840 return nmatch;
841 }
842
843 void
844 in6_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp)
845 {
846 struct inpcb_hdr *inph;
847 struct ip6_moptions *im6o;
848 struct in6_multi_mship *imm, *nimm;
849
850 KASSERT(ifp != NULL);
851
852 TAILQ_FOREACH(inph, &table->inpt_queue, inph_queue) {
853 struct in6pcb *in6p = (struct in6pcb *)inph;
854 bool need_unlock = false;
855 if (in6p->in6p_af != AF_INET6)
856 continue;
857
858 /* The caller holds either one of in6ps' lock */
859 if (!in6p_locked(in6p)) {
860 in6p_lock(in6p);
861 need_unlock = true;
862 }
863 im6o = in6p->in6p_moptions;
864 if (im6o) {
865 /*
866 * Unselect the outgoing interface if it is being
867 * detached.
868 */
869 if (im6o->im6o_multicast_if_index == ifp->if_index)
870 im6o->im6o_multicast_if_index = 0;
871
872 /*
873 * Drop multicast group membership if we joined
874 * through the interface being detached.
875 * XXX controversial - is it really legal for kernel
876 * to force this?
877 */
878 LIST_FOREACH_SAFE(imm, &im6o->im6o_memberships,
879 i6mm_chain, nimm) {
880 if (imm->i6mm_maddr->in6m_ifp == ifp) {
881 LIST_REMOVE(imm, i6mm_chain);
882 in6_leavegroup(imm);
883 }
884 }
885 }
886
887 in_purgeifmcast(in6p->in6p_v4moptions, ifp);
888
889 if (need_unlock)
890 in6p_unlock(in6p);
891 }
892 }
893
894 void
895 in6_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp)
896 {
897 struct rtentry *rt;
898 struct inpcb_hdr *inph;
899
900 TAILQ_FOREACH(inph, &table->inpt_queue, inph_queue) {
901 struct in6pcb *in6p = (struct in6pcb *)inph;
902 if (in6p->in6p_af != AF_INET6)
903 continue;
904 if ((rt = rtcache_validate(&in6p->in6p_route)) != NULL &&
905 rt->rt_ifp == ifp) {
906 rtcache_unref(rt, &in6p->in6p_route);
907 in6_rtchange(in6p, 0);
908 } else
909 rtcache_unref(rt, &in6p->in6p_route);
910 }
911 }
912
913 /*
914 * Check for alternatives when higher level complains
915 * about service problems. For now, invalidate cached
916 * routing information. If the route was created dynamically
917 * (by a redirect), time to try a default gateway again.
918 */
919 void
920 in6_losing(struct in6pcb *in6p)
921 {
922 struct rtentry *rt;
923 struct rt_addrinfo info;
924
925 if (in6p->in6p_af != AF_INET6)
926 return;
927
928 if ((rt = rtcache_validate(&in6p->in6p_route)) == NULL)
929 return;
930
931 memset(&info, 0, sizeof(info));
932 info.rti_info[RTAX_DST] = rtcache_getdst(&in6p->in6p_route);
933 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
934 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
935 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
936 if (rt->rt_flags & RTF_DYNAMIC) {
937 int error;
938 struct rtentry *nrt;
939
940 error = rtrequest(RTM_DELETE, rt_getkey(rt),
941 rt->rt_gateway, rt_mask(rt), rt->rt_flags, &nrt);
942 rtcache_unref(rt, &in6p->in6p_route);
943 if (error == 0) {
944 rt_newmsg_dynamic(RTM_DELETE, nrt);
945 rt_free(nrt);
946 }
947 } else
948 rtcache_unref(rt, &in6p->in6p_route);
949 /*
950 * A new route can be allocated
951 * the next time output is attempted.
952 */
953 rtcache_free(&in6p->in6p_route);
954 }
955
956 /*
957 * After a routing change, flush old routing. A new route can be
958 * allocated the next time output is attempted.
959 */
960 void
961 in6_rtchange(struct in6pcb *in6p, int errno)
962 {
963 if (in6p->in6p_af != AF_INET6)
964 return;
965
966 rtcache_free(&in6p->in6p_route);
967 /*
968 * A new route can be allocated the next time
969 * output is attempted.
970 */
971 }
972
973 struct in6pcb *
974 in6_pcblookup_port(struct inpcbtable *table, struct in6_addr *laddr6,
975 u_int lport_arg, int lookup_wildcard, struct vestigial_inpcb *vp)
976 {
977 struct inpcbhead *head;
978 struct inpcb_hdr *inph;
979 struct in6pcb *in6p, *match = NULL;
980 int matchwild = 3, wildcard;
981 u_int16_t lport = lport_arg;
982
983 if (vp)
984 vp->valid = 0;
985
986 head = IN6PCBHASH_PORT(table, lport);
987 LIST_FOREACH(inph, head, inph_lhash) {
988 in6p = (struct in6pcb *)inph;
989 if (in6p->in6p_af != AF_INET6)
990 continue;
991
992 if (in6p->in6p_lport != lport)
993 continue;
994 wildcard = 0;
995 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
996 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
997 continue;
998 }
999 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
1000 wildcard++;
1001 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
1002 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1003 continue;
1004 if (!IN6_IS_ADDR_V4MAPPED(laddr6))
1005 continue;
1006
1007 /* duplicate of IPv4 logic */
1008 wildcard = 0;
1009 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) &&
1010 in6p->in6p_faddr.s6_addr32[3])
1011 wildcard++;
1012 if (!in6p->in6p_laddr.s6_addr32[3]) {
1013 if (laddr6->s6_addr32[3])
1014 wildcard++;
1015 } else {
1016 if (!laddr6->s6_addr32[3])
1017 wildcard++;
1018 else {
1019 if (in6p->in6p_laddr.s6_addr32[3] !=
1020 laddr6->s6_addr32[3])
1021 continue;
1022 }
1023 }
1024 } else if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1025 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1026 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1027 continue;
1028 }
1029 if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
1030 wildcard++;
1031 } else {
1032 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1033 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1034 continue;
1035 }
1036 if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
1037 wildcard++;
1038 else {
1039 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
1040 laddr6))
1041 continue;
1042 }
1043 }
1044 if (wildcard && !lookup_wildcard)
1045 continue;
1046 if (wildcard < matchwild) {
1047 match = in6p;
1048 matchwild = wildcard;
1049 if (matchwild == 0)
1050 break;
1051 }
1052 }
1053 if (match && matchwild == 0)
1054 return match;
1055
1056 if (vp && table->vestige && table->vestige->init_ports6) {
1057 struct vestigial_inpcb better;
1058 void *state;
1059
1060 state = (*table->vestige->init_ports6)(laddr6,
1061 lport_arg,
1062 lookup_wildcard);
1063 while (table->vestige
1064 && (*table->vestige->next_port6)(state, vp)) {
1065
1066 if (vp->lport != lport)
1067 continue;
1068 wildcard = 0;
1069 if (!IN6_IS_ADDR_UNSPECIFIED(&vp->faddr.v6))
1070 wildcard++;
1071 if (IN6_IS_ADDR_UNSPECIFIED(&vp->laddr.v6)) {
1072 if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
1073 wildcard++;
1074 } else {
1075 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1076 if (vp->v6only)
1077 continue;
1078 }
1079 if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
1080 wildcard++;
1081 else {
1082 if (!IN6_ARE_ADDR_EQUAL(&vp->laddr.v6, laddr6))
1083 continue;
1084 }
1085 }
1086 if (wildcard && !lookup_wildcard)
1087 continue;
1088 if (wildcard < matchwild) {
1089 better = *vp;
1090 match = (void*)&better;
1091
1092 matchwild = wildcard;
1093 if (matchwild == 0)
1094 break;
1095 }
1096 }
1097
1098 if (match) {
1099 if (match != (void*)&better)
1100 return match;
1101 else {
1102 *vp = better;
1103 return 0;
1104 }
1105 }
1106 }
1107 return (match);
1108 }
1109
1110 /*
1111 * WARNING: return value (rtentry) could be IPv4 one if in6pcb is connected to
1112 * IPv4 mapped address.
1113 */
1114 struct rtentry *
1115 in6_pcbrtentry(struct in6pcb *in6p)
1116 {
1117 struct rtentry *rt;
1118 struct route *ro;
1119 union {
1120 const struct sockaddr *sa;
1121 const struct sockaddr_in6 *sa6;
1122 #ifdef INET
1123 const struct sockaddr_in *sa4;
1124 #endif
1125 } cdst;
1126
1127 ro = &in6p->in6p_route;
1128
1129 if (in6p->in6p_af != AF_INET6)
1130 return (NULL);
1131
1132 cdst.sa = rtcache_getdst(ro);
1133 if (cdst.sa == NULL)
1134 ;
1135 #ifdef INET
1136 else if (cdst.sa->sa_family == AF_INET) {
1137 KASSERT(IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr));
1138 if (cdst.sa4->sin_addr.s_addr != in6p->in6p_faddr.s6_addr32[3])
1139 rtcache_free(ro);
1140 }
1141 #endif
1142 else {
1143 if (!IN6_ARE_ADDR_EQUAL(&cdst.sa6->sin6_addr,
1144 &in6p->in6p_faddr))
1145 rtcache_free(ro);
1146 }
1147 if ((rt = rtcache_validate(ro)) == NULL)
1148 rt = rtcache_update(ro, 1);
1149 #ifdef INET
1150 if (rt == NULL && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
1151 union {
1152 struct sockaddr dst;
1153 struct sockaddr_in dst4;
1154 } u;
1155 struct in_addr addr;
1156
1157 addr.s_addr = in6p->in6p_faddr.s6_addr32[3];
1158
1159 sockaddr_in_init(&u.dst4, &addr, 0);
1160 if (rtcache_setdst(ro, &u.dst) != 0)
1161 return NULL;
1162
1163 rt = rtcache_init(ro);
1164 } else
1165 #endif
1166 if (rt == NULL && !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
1167 union {
1168 struct sockaddr dst;
1169 struct sockaddr_in6 dst6;
1170 } u;
1171
1172 sockaddr_in6_init(&u.dst6, &in6p->in6p_faddr, 0, 0, 0);
1173 if (rtcache_setdst(ro, &u.dst) != 0)
1174 return NULL;
1175
1176 rt = rtcache_init(ro);
1177 }
1178 return rt;
1179 }
1180
1181 void
1182 in6_pcbrtentry_unref(struct rtentry *rt, struct in6pcb *in6p)
1183 {
1184
1185 rtcache_unref(rt, &in6p->in6p_route);
1186 }
1187
1188 struct in6pcb *
1189 in6_pcblookup_connect(struct inpcbtable *table, const struct in6_addr *faddr6,
1190 u_int fport_arg, const struct in6_addr *laddr6, u_int lport_arg,
1191 int faith,
1192 struct vestigial_inpcb *vp)
1193 {
1194 struct inpcbhead *head;
1195 struct inpcb_hdr *inph;
1196 struct in6pcb *in6p;
1197 u_int16_t fport = fport_arg, lport = lport_arg;
1198
1199 if (vp)
1200 vp->valid = 0;
1201
1202 head = IN6PCBHASH_CONNECT(table, faddr6, fport, laddr6, lport);
1203 LIST_FOREACH(inph, head, inph_hash) {
1204 in6p = (struct in6pcb *)inph;
1205 if (in6p->in6p_af != AF_INET6)
1206 continue;
1207
1208 /* find exact match on both source and dest */
1209 if (in6p->in6p_fport != fport)
1210 continue;
1211 if (in6p->in6p_lport != lport)
1212 continue;
1213 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
1214 continue;
1215 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
1216 continue;
1217 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1218 continue;
1219 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
1220 continue;
1221 if ((IN6_IS_ADDR_V4MAPPED(laddr6) ||
1222 IN6_IS_ADDR_V4MAPPED(faddr6)) &&
1223 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1224 continue;
1225 return in6p;
1226 }
1227 if (vp && table->vestige) {
1228 if ((*table->vestige->lookup6)(faddr6, fport_arg,
1229 laddr6, lport_arg, vp))
1230 return NULL;
1231 }
1232
1233 return NULL;
1234 }
1235
1236 struct in6pcb *
1237 in6_pcblookup_bind(struct inpcbtable *table, const struct in6_addr *laddr6,
1238 u_int lport_arg, int faith)
1239 {
1240 struct inpcbhead *head;
1241 struct inpcb_hdr *inph;
1242 struct in6pcb *in6p;
1243 u_int16_t lport = lport_arg;
1244 #ifdef INET
1245 struct in6_addr zero_mapped;
1246 #endif
1247
1248 head = IN6PCBHASH_BIND(table, laddr6, lport);
1249 LIST_FOREACH(inph, head, inph_hash) {
1250 in6p = (struct in6pcb *)inph;
1251 if (in6p->in6p_af != AF_INET6)
1252 continue;
1253
1254 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1255 continue;
1256 if (in6p->in6p_fport != 0)
1257 continue;
1258 if (in6p->in6p_lport != lport)
1259 continue;
1260 if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
1261 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1262 continue;
1263 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
1264 goto out;
1265 }
1266 #ifdef INET
1267 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1268 memset(&zero_mapped, 0, sizeof(zero_mapped));
1269 zero_mapped.s6_addr16[5] = 0xffff;
1270 head = IN6PCBHASH_BIND(table, &zero_mapped, lport);
1271 LIST_FOREACH(inph, head, inph_hash) {
1272 in6p = (struct in6pcb *)inph;
1273 if (in6p->in6p_af != AF_INET6)
1274 continue;
1275
1276 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1277 continue;
1278 if (in6p->in6p_fport != 0)
1279 continue;
1280 if (in6p->in6p_lport != lport)
1281 continue;
1282 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1283 continue;
1284 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zero_mapped))
1285 goto out;
1286 }
1287 }
1288 #endif
1289 head = IN6PCBHASH_BIND(table, &zeroin6_addr, lport);
1290 LIST_FOREACH(inph, head, inph_hash) {
1291 in6p = (struct in6pcb *)inph;
1292 if (in6p->in6p_af != AF_INET6)
1293 continue;
1294
1295 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1296 continue;
1297 if (in6p->in6p_fport != 0)
1298 continue;
1299 if (in6p->in6p_lport != lport)
1300 continue;
1301 if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
1302 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1303 continue;
1304 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zeroin6_addr))
1305 goto out;
1306 }
1307 return (NULL);
1308
1309 out:
1310 inph = &in6p->in6p_head;
1311 if (inph != LIST_FIRST(head)) {
1312 LIST_REMOVE(inph, inph_hash);
1313 LIST_INSERT_HEAD(head, inph, inph_hash);
1314 }
1315 return in6p;
1316 }
1317
1318 void
1319 in6_pcbstate(struct in6pcb *in6p, int state)
1320 {
1321
1322 if (in6p->in6p_af != AF_INET6)
1323 return;
1324
1325 if (in6p->in6p_state > IN6P_ATTACHED)
1326 LIST_REMOVE(&in6p->in6p_head, inph_hash);
1327
1328 switch (state) {
1329 case IN6P_BOUND:
1330 LIST_INSERT_HEAD(IN6PCBHASH_BIND(in6p->in6p_table,
1331 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
1332 inph_hash);
1333 break;
1334 case IN6P_CONNECTED:
1335 LIST_INSERT_HEAD(IN6PCBHASH_CONNECT(in6p->in6p_table,
1336 &in6p->in6p_faddr, in6p->in6p_fport,
1337 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
1338 inph_hash);
1339 break;
1340 }
1341
1342 in6p->in6p_state = state;
1343 }
1344