in6_pcb.c revision 1.162 1 /* $NetBSD: in6_pcb.c,v 1.162 2017/12/15 04:03:46 ozaki-r 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.162 2017/12/15 04:03:46 ozaki-r 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 intialize 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 error = EADDRNOTAVAIL;
240 goto out;
241 }
242 }
243 }
244 } else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
245 // succeed
246 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
247 struct ifaddr *ifa = NULL;
248
249 if ((in6p->in6p_flags & IN6P_FAITH) == 0) {
250 ifa = ifa_ifwithaddr(sin6tosa(sin6));
251 if (ifa == NULL) {
252 error = EADDRNOTAVAIL;
253 goto out;
254 }
255 }
256
257 /*
258 * bind to an anycast address might accidentally
259 * cause sending a packet with an anycast source
260 * address, so we forbid it.
261 *
262 * We should allow to bind to a deprecated address,
263 * since the application dare to use it.
264 * But, can we assume that they are careful enough
265 * to check if the address is deprecated or not?
266 * Maybe, as a safeguard, we should have a setsockopt
267 * flag to control the bind(2) behavior against
268 * deprecated addresses (default: forbid bind(2)).
269 */
270 if (ifa &&
271 ifatoia6(ifa)->ia6_flags &
272 (IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED)) {
273 error = EADDRNOTAVAIL;
274 goto out;
275 }
276 }
277 in6p->in6p_laddr = sin6->sin6_addr;
278 error = 0;
279 out:
280 pserialize_read_exit(s);
281 return error;
282 }
283
284 /*
285 * Bind port from sin6 to in6p.
286 */
287 static int
288 in6_pcbbind_port(struct in6pcb *in6p, struct sockaddr_in6 *sin6, struct lwp *l)
289 {
290 struct inpcbtable *table = in6p->in6p_table;
291 struct socket *so = in6p->in6p_socket;
292 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
293 int error;
294
295 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
296 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
297 (so->so_options & SO_ACCEPTCONN) == 0))
298 wild = 1;
299
300 if (sin6->sin6_port != 0) {
301 enum kauth_network_req req;
302
303 #ifndef IPNOPRIVPORTS
304 if (ntohs(sin6->sin6_port) < IPV6PORT_RESERVED)
305 req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
306 else
307 #endif /* IPNOPRIVPORTS */
308 req = KAUTH_REQ_NETWORK_BIND_PORT;
309
310 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_BIND,
311 req, so, sin6, NULL);
312 if (error)
313 return (EACCES);
314 }
315
316 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
317 /*
318 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
319 * allow compepte duplication of binding if
320 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
321 * and a multicast address is bound on both
322 * new and duplicated sockets.
323 */
324 if (so->so_options & (SO_REUSEADDR | SO_REUSEPORT))
325 reuseport = SO_REUSEADDR|SO_REUSEPORT;
326 }
327
328 if (sin6->sin6_port != 0) {
329 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
330 #ifdef INET
331 struct inpcb *t;
332 struct vestigial_inpcb vestige;
333
334 t = in_pcblookup_port(table,
335 *(struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
336 sin6->sin6_port, wild, &vestige);
337 if (t && (reuseport & t->inp_socket->so_options) == 0)
338 return (EADDRINUSE);
339 if (!t
340 && vestige.valid
341 && !(reuseport && vestige.reuse_port))
342 return EADDRINUSE;
343 #else
344 return (EADDRNOTAVAIL);
345 #endif
346 }
347
348 {
349 struct in6pcb *t;
350 struct vestigial_inpcb vestige;
351
352 t = in6_pcblookup_port(table, &sin6->sin6_addr,
353 sin6->sin6_port, wild, &vestige);
354 if (t && (reuseport & t->in6p_socket->so_options) == 0)
355 return (EADDRINUSE);
356 if (!t
357 && vestige.valid
358 && !(reuseport && vestige.reuse_port))
359 return EADDRINUSE;
360 }
361 }
362
363 if (sin6->sin6_port == 0) {
364 int e;
365 e = in6_pcbsetport(sin6, in6p, l);
366 if (e != 0)
367 return (e);
368 } else {
369 in6p->in6p_lport = sin6->sin6_port;
370 in6_pcbstate(in6p, IN6P_BOUND);
371 }
372
373 LIST_REMOVE(&in6p->in6p_head, inph_lhash);
374 LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport),
375 &in6p->in6p_head, inph_lhash);
376
377 return (0);
378 }
379
380 int
381 in6_pcbbind(void *v, struct sockaddr_in6 *sin6, struct lwp *l)
382 {
383 struct in6pcb *in6p = v;
384 struct sockaddr_in6 lsin6;
385 int error;
386
387 if (in6p->in6p_af != AF_INET6)
388 return (EINVAL);
389
390 /*
391 * If we already have a local port or a local address it means we're
392 * bounded.
393 */
394 if (in6p->in6p_lport || !(IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ||
395 (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
396 in6p->in6p_laddr.s6_addr32[3] == 0)))
397 return (EINVAL);
398
399 if (NULL != sin6) {
400 /* We were provided a sockaddr_in6 to use. */
401 if (sin6->sin6_len != sizeof(*sin6))
402 return (EINVAL);
403 } else {
404 /* We always bind to *something*, even if it's "anything". */
405 lsin6 = *((const struct sockaddr_in6 *)
406 in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
407 sin6 = &lsin6;
408 }
409
410 /* Bind address. */
411 error = in6_pcbbind_addr(in6p, sin6, l);
412 if (error)
413 return (error);
414
415 /* Bind port. */
416 error = in6_pcbbind_port(in6p, sin6, l);
417 if (error) {
418 /*
419 * Reset the address here to "any" so we don't "leak" the
420 * in6pcb.
421 */
422 in6p->in6p_laddr = in6addr_any;
423
424 return (error);
425 }
426
427
428 #if 0
429 in6p->in6p_flowinfo = 0; /* XXX */
430 #endif
431 return (0);
432 }
433
434 /*
435 * Connect from a socket to a specified address.
436 * Both address and port must be specified in argument sin6.
437 * If don't have a local address for this socket yet,
438 * then pick one.
439 */
440 int
441 in6_pcbconnect(void *v, struct sockaddr_in6 *sin6, struct lwp *l)
442 {
443 struct in6pcb *in6p = v;
444 struct in6_addr *in6a = NULL;
445 struct in6_addr ia6;
446 struct ifnet *ifp = NULL; /* outgoing interface */
447 int error = 0;
448 int scope_ambiguous = 0;
449 #ifdef INET
450 struct in6_addr mapped;
451 #endif
452 struct sockaddr_in6 tmp;
453 struct vestigial_inpcb vestige;
454 struct psref psref;
455 int bound;
456
457 (void)&in6a; /* XXX fool gcc */
458
459 if (in6p->in6p_af != AF_INET6)
460 return (EINVAL);
461
462 if (sin6->sin6_len != sizeof(*sin6))
463 return (EINVAL);
464 if (sin6->sin6_family != AF_INET6)
465 return (EAFNOSUPPORT);
466 if (sin6->sin6_port == 0)
467 return (EADDRNOTAVAIL);
468
469 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
470 in6p->in6p_socket->so_type == SOCK_STREAM)
471 return EADDRNOTAVAIL;
472
473 if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
474 scope_ambiguous = 1;
475 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
476 return(error);
477
478 /* sanity check for mapped address case */
479 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
480 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
481 return EINVAL;
482 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
483 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
484 if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
485 return EINVAL;
486 } else
487 {
488 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
489 return EINVAL;
490 }
491
492 /* protect *sin6 from overwrites */
493 tmp = *sin6;
494 sin6 = &tmp;
495
496 bound = curlwp_bind();
497 /* Source address selection. */
498 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
499 in6p->in6p_laddr.s6_addr32[3] == 0) {
500 #ifdef INET
501 struct sockaddr_in sin;
502 struct in_ifaddr *ia4;
503 struct psref _psref;
504
505 memset(&sin, 0, sizeof(sin));
506 sin.sin_len = sizeof(sin);
507 sin.sin_family = AF_INET;
508 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr32[3],
509 sizeof(sin.sin_addr));
510 ia4 = in_selectsrc(&sin, &in6p->in6p_route,
511 in6p->in6p_socket->so_options, NULL, &error, &_psref);
512 if (ia4 == NULL) {
513 if (error == 0)
514 error = EADDRNOTAVAIL;
515 return (error);
516 }
517 memset(&mapped, 0, sizeof(mapped));
518 mapped.s6_addr16[5] = htons(0xffff);
519 memcpy(&mapped.s6_addr32[3], &IA_SIN(ia4)->sin_addr,
520 sizeof(IA_SIN(ia4)->sin_addr));
521 ia4_release(ia4, &_psref);
522 in6a = &mapped;
523 #else
524 return EADDRNOTAVAIL;
525 #endif
526 } else {
527 /*
528 * XXX: in6_selectsrc might replace the bound local address
529 * with the address specified by setsockopt(IPV6_PKTINFO).
530 * Is it the intended behavior?
531 */
532 error = in6_selectsrc(sin6, in6p->in6p_outputopts,
533 in6p->in6p_moptions, &in6p->in6p_route, &in6p->in6p_laddr,
534 &ifp, &psref, &ia6);
535 if (error == 0)
536 in6a = &ia6;
537 if (ifp && scope_ambiguous &&
538 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
539 if_put(ifp, &psref);
540 curlwp_bindx(bound);
541 return error;
542 }
543
544 if (in6a == NULL) {
545 if_put(ifp, &psref);
546 curlwp_bindx(bound);
547 if (error == 0)
548 error = EADDRNOTAVAIL;
549 return error;
550 }
551 }
552
553 if (ifp != NULL) {
554 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
555 if_put(ifp, &psref);
556 } else
557 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim_rt(in6p);
558 curlwp_bindx(bound);
559
560 if (in6_pcblookup_connect(in6p->in6p_table, &sin6->sin6_addr,
561 sin6->sin6_port,
562 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ? in6a : &in6p->in6p_laddr,
563 in6p->in6p_lport, 0, &vestige)
564 || vestige.valid)
565 return (EADDRINUSE);
566 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ||
567 (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
568 in6p->in6p_laddr.s6_addr32[3] == 0))
569 {
570 if (in6p->in6p_lport == 0) {
571 error = in6_pcbbind(in6p, NULL, l);
572 if (error != 0)
573 return error;
574 }
575 in6p->in6p_laddr = *in6a;
576 }
577 in6p->in6p_faddr = sin6->sin6_addr;
578 in6p->in6p_fport = sin6->sin6_port;
579
580 /* Late bind, if needed */
581 if (in6p->in6p_bindportonsend) {
582 struct sockaddr_in6 lsin = *((const struct sockaddr_in6 *)
583 in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
584 lsin.sin6_addr = in6p->in6p_laddr;
585 lsin.sin6_port = 0;
586
587 if ((error = in6_pcbbind_port(in6p, &lsin, l)) != 0)
588 return error;
589 }
590
591 in6_pcbstate(in6p, IN6P_CONNECTED);
592 in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
593 if (ip6_auto_flowlabel)
594 in6p->in6p_flowinfo |=
595 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
596 #if defined(IPSEC)
597 if (ipsec_enabled && in6p->in6p_socket->so_type == SOCK_STREAM)
598 ipsec_pcbconn(in6p->in6p_sp);
599 #endif
600 return (0);
601 }
602
603 void
604 in6_pcbdisconnect(struct in6pcb *in6p)
605 {
606 memset((void *)&in6p->in6p_faddr, 0, sizeof(in6p->in6p_faddr));
607 in6p->in6p_fport = 0;
608 in6_pcbstate(in6p, IN6P_BOUND);
609 in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
610 #if defined(IPSEC)
611 if (ipsec_enabled)
612 ipsec_pcbdisconn(in6p->in6p_sp);
613 #endif
614 if (in6p->in6p_socket->so_state & SS_NOFDREF)
615 in6_pcbdetach(in6p);
616 }
617
618 void
619 in6_pcbdetach(struct in6pcb *in6p)
620 {
621 struct socket *so = in6p->in6p_socket;
622 int s;
623
624 if (in6p->in6p_af != AF_INET6)
625 return;
626
627 #if defined(IPSEC)
628 if (ipsec_enabled)
629 ipsec6_delete_pcbpolicy(in6p);
630 #endif
631 so->so_pcb = NULL;
632
633 s = splsoftnet();
634 in6_pcbstate(in6p, IN6P_ATTACHED);
635 LIST_REMOVE(&in6p->in6p_head, inph_lhash);
636 TAILQ_REMOVE(&in6p->in6p_table->inpt_queue, &in6p->in6p_head,
637 inph_queue);
638 splx(s);
639
640 if (in6p->in6p_options) {
641 m_freem(in6p->in6p_options);
642 }
643 if (in6p->in6p_outputopts != NULL) {
644 ip6_clearpktopts(in6p->in6p_outputopts, -1);
645 free(in6p->in6p_outputopts, M_IP6OPT);
646 }
647 rtcache_free(&in6p->in6p_route);
648 ip6_freemoptions(in6p->in6p_moptions);
649 ip_freemoptions(in6p->in6p_v4moptions);
650 sofree(so); /* drops the socket's lock */
651
652 pool_put(&in6pcb_pool, in6p);
653 mutex_enter(softnet_lock); /* reacquire it */
654 }
655
656 void
657 in6_setsockaddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6)
658 {
659
660 if (in6p->in6p_af != AF_INET6)
661 return;
662
663 sockaddr_in6_init(sin6, &in6p->in6p_laddr, in6p->in6p_lport, 0, 0);
664 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
665 }
666
667 void
668 in6_setpeeraddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6)
669 {
670
671 if (in6p->in6p_af != AF_INET6)
672 return;
673
674 sockaddr_in6_init(sin6, &in6p->in6p_faddr, in6p->in6p_fport, 0, 0);
675 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
676 }
677
678 /*
679 * Pass some notification to all connections of a protocol
680 * associated with address dst. The local address and/or port numbers
681 * may be specified to limit the search. The "usual action" will be
682 * taken, depending on the ctlinput cmd. The caller must filter any
683 * cmds that are uninteresting (e.g., no error in the map).
684 * Call the protocol specific routine (if any) to report
685 * any errors for each matching socket.
686 *
687 * Must be called at splsoftnet.
688 *
689 * Note: src (4th arg) carries the flowlabel value on the original IPv6
690 * header, in sin6_flowinfo member.
691 */
692 int
693 in6_pcbnotify(struct inpcbtable *table, const struct sockaddr *dst,
694 u_int fport_arg, const struct sockaddr *src, u_int lport_arg, int cmd,
695 void *cmdarg, void (*notify)(struct in6pcb *, int))
696 {
697 struct inpcb_hdr *inph, *ninph;
698 struct sockaddr_in6 sa6_src;
699 const struct sockaddr_in6 *sa6_dst;
700 u_int16_t fport = fport_arg, lport = lport_arg;
701 int errno;
702 int nmatch = 0;
703 u_int32_t flowinfo;
704
705 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
706 return 0;
707
708 sa6_dst = (const struct sockaddr_in6 *)dst;
709 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
710 return 0;
711
712 /*
713 * note that src can be NULL when we get notify by local fragmentation.
714 */
715 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
716 flowinfo = sa6_src.sin6_flowinfo;
717
718 /*
719 * Redirects go to all references to the destination,
720 * and use in6_rtchange to invalidate the route cache.
721 * Dead host indications: also use in6_rtchange to invalidate
722 * the cache, and deliver the error to all the sockets.
723 * Otherwise, if we have knowledge of the local port and address,
724 * deliver only to that socket.
725 */
726 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
727 fport = 0;
728 lport = 0;
729 memset((void *)&sa6_src.sin6_addr, 0, sizeof(sa6_src.sin6_addr));
730
731 if (cmd != PRC_HOSTDEAD)
732 notify = in6_rtchange;
733 }
734
735 errno = inet6ctlerrmap[cmd];
736 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
737 struct in6pcb *in6p = (struct in6pcb *)inph;
738 struct rtentry *rt = NULL;
739
740 if (in6p->in6p_af != AF_INET6)
741 continue;
742
743 /*
744 * Under the following condition, notify of redirects
745 * to the pcb, without making address matches against inpcb.
746 * - redirect notification is arrived.
747 * - the inpcb is unconnected.
748 * - the inpcb is caching !RTF_HOST routing entry.
749 * - the ICMPv6 notification is from the gateway cached in the
750 * inpcb. i.e. ICMPv6 notification is from nexthop gateway
751 * the inpcb used very recently.
752 *
753 * This is to improve interaction between netbsd/openbsd
754 * redirect handling code, and inpcb route cache code.
755 * without the clause, !RTF_HOST routing entry (which carries
756 * gateway used by inpcb right before the ICMPv6 redirect)
757 * will be cached forever in unconnected inpcb.
758 *
759 * There still is a question regarding to what is TRT:
760 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
761 * generated on packet output. inpcb will always cache
762 * RTF_HOST routing entry so there's no need for the clause
763 * (ICMPv6 redirect will update RTF_HOST routing entry,
764 * and inpcb is caching it already).
765 * However, bsdi/freebsd are vulnerable to local DoS attacks
766 * due to the cloned routing entries.
767 * - Specwise, "destination cache" is mentioned in RFC2461.
768 * Jinmei says that it implies bsdi/freebsd behavior, itojun
769 * is not really convinced.
770 * - Having hiwat/lowat on # of cloned host route (redirect/
771 * pmtud) may be a good idea. netbsd/openbsd has it. see
772 * icmp6_mtudisc_update().
773 */
774 if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
775 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
776 (rt = rtcache_validate(&in6p->in6p_route)) != NULL &&
777 !(rt->rt_flags & RTF_HOST)) {
778 const struct sockaddr_in6 *dst6;
779
780 dst6 = (const struct sockaddr_in6 *)
781 rtcache_getdst(&in6p->in6p_route);
782 if (dst6 == NULL)
783 ;
784 else if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
785 &sa6_dst->sin6_addr)) {
786 rtcache_unref(rt, &in6p->in6p_route);
787 goto do_notify;
788 }
789 }
790 rtcache_unref(rt, &in6p->in6p_route);
791
792 /*
793 * If the error designates a new path MTU for a destination
794 * and the application (associated with this socket) wanted to
795 * know the value, notify. Note that we notify for all
796 * disconnected sockets if the corresponding application
797 * wanted. This is because some UDP applications keep sending
798 * sockets disconnected.
799 * XXX: should we avoid to notify the value to TCP sockets?
800 */
801 if (cmd == PRC_MSGSIZE && (in6p->in6p_flags & IN6P_MTU) != 0 &&
802 (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) ||
803 IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &sa6_dst->sin6_addr))) {
804 ip6_notify_pmtu(in6p, (const struct sockaddr_in6 *)dst,
805 (u_int32_t *)cmdarg);
806 }
807
808 /*
809 * Detect if we should notify the error. If no source and
810 * destination ports are specified, but non-zero flowinfo and
811 * local address match, notify the error. This is the case
812 * when the error is delivered with an encrypted buffer
813 * by ESP. Otherwise, just compare addresses and ports
814 * as usual.
815 */
816 if (lport == 0 && fport == 0 && flowinfo &&
817 in6p->in6p_socket != NULL &&
818 flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
819 IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr))
820 goto do_notify;
821 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
822 &sa6_dst->sin6_addr) ||
823 in6p->in6p_socket == NULL ||
824 (lport && in6p->in6p_lport != lport) ||
825 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
826 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
827 &sa6_src.sin6_addr)) ||
828 (fport && in6p->in6p_fport != fport))
829 continue;
830
831 do_notify:
832 if (notify)
833 (*notify)(in6p, errno);
834 nmatch++;
835 }
836 return nmatch;
837 }
838
839 void
840 in6_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp)
841 {
842 struct inpcb_hdr *inph, *ninph;
843 struct ip6_moptions *im6o;
844 struct in6_multi_mship *imm, *nimm;
845
846 KASSERT(ifp != NULL);
847
848 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
849 struct in6pcb *in6p = (struct in6pcb *)inph;
850 bool need_unlock = false;
851 if (in6p->in6p_af != AF_INET6)
852 continue;
853
854 /* The caller holds either one of in6ps' lock */
855 if (!in6p_locked(in6p)) {
856 in6p_lock(in6p);
857 need_unlock = true;
858 }
859 im6o = in6p->in6p_moptions;
860 if (im6o) {
861 /*
862 * Unselect the outgoing interface if it is being
863 * detached.
864 */
865 if (im6o->im6o_multicast_if_index == ifp->if_index)
866 im6o->im6o_multicast_if_index = 0;
867
868 /*
869 * Drop multicast group membership if we joined
870 * through the interface being detached.
871 * XXX controversial - is it really legal for kernel
872 * to force this?
873 */
874 LIST_FOREACH_SAFE(imm, &im6o->im6o_memberships,
875 i6mm_chain, nimm) {
876 if (imm->i6mm_maddr->in6m_ifp == ifp) {
877 LIST_REMOVE(imm, i6mm_chain);
878 IFNET_LOCK(ifp);
879 in6_leavegroup(imm);
880 IFNET_UNLOCK(ifp);
881 }
882 }
883 }
884
885 /* IFNET_LOCK must be taken after solock */
886 IFNET_LOCK(ifp);
887 in_purgeifmcast(in6p->in6p_v4moptions, ifp);
888 IFNET_UNLOCK(ifp);
889
890 if (need_unlock)
891 in6p_unlock(in6p);
892 }
893 }
894
895 void
896 in6_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp)
897 {
898 struct rtentry *rt;
899 struct inpcb_hdr *inph, *ninph;
900
901 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
902 struct in6pcb *in6p = (struct in6pcb *)inph;
903 if (in6p->in6p_af != AF_INET6)
904 continue;
905 if ((rt = rtcache_validate(&in6p->in6p_route)) != NULL &&
906 rt->rt_ifp == ifp) {
907 rtcache_unref(rt, &in6p->in6p_route);
908 in6_rtchange(in6p, 0);
909 } else
910 rtcache_unref(rt, &in6p->in6p_route);
911 }
912 }
913
914 /*
915 * Check for alternatives when higher level complains
916 * about service problems. For now, invalidate cached
917 * routing information. If the route was created dynamically
918 * (by a redirect), time to try a default gateway again.
919 */
920 void
921 in6_losing(struct in6pcb *in6p)
922 {
923 struct rtentry *rt;
924 struct rt_addrinfo info;
925
926 if (in6p->in6p_af != AF_INET6)
927 return;
928
929 if ((rt = rtcache_validate(&in6p->in6p_route)) == NULL)
930 return;
931
932 memset(&info, 0, sizeof(info));
933 info.rti_info[RTAX_DST] = rtcache_getdst(&in6p->in6p_route);
934 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
935 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
936 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
937 if (rt->rt_flags & RTF_DYNAMIC) {
938 int error;
939 struct rtentry *nrt;
940
941 error = rtrequest(RTM_DELETE, rt_getkey(rt),
942 rt->rt_gateway, rt_mask(rt), rt->rt_flags, &nrt);
943 rtcache_unref(rt, &in6p->in6p_route);
944 if (error == 0)
945 rt_free(nrt);
946 } else
947 rtcache_unref(rt, &in6p->in6p_route);
948 /*
949 * A new route can be allocated
950 * the next time output is attempted.
951 */
952 rtcache_free(&in6p->in6p_route);
953 }
954
955 /*
956 * After a routing change, flush old routing. A new route can be
957 * allocated the next time output is attempted.
958 */
959 void
960 in6_rtchange(struct in6pcb *in6p, int errno)
961 {
962 if (in6p->in6p_af != AF_INET6)
963 return;
964
965 rtcache_free(&in6p->in6p_route);
966 /*
967 * A new route can be allocated the next time
968 * output is attempted.
969 */
970 }
971
972 struct in6pcb *
973 in6_pcblookup_port(struct inpcbtable *table, struct in6_addr *laddr6,
974 u_int lport_arg, int lookup_wildcard, struct vestigial_inpcb *vp)
975 {
976 struct inpcbhead *head;
977 struct inpcb_hdr *inph;
978 struct in6pcb *in6p, *match = NULL;
979 int matchwild = 3, wildcard;
980 u_int16_t lport = lport_arg;
981
982 if (vp)
983 vp->valid = 0;
984
985 head = IN6PCBHASH_PORT(table, lport);
986 LIST_FOREACH(inph, head, inph_lhash) {
987 in6p = (struct in6pcb *)inph;
988 if (in6p->in6p_af != AF_INET6)
989 continue;
990
991 if (in6p->in6p_lport != lport)
992 continue;
993 wildcard = 0;
994 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
995 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
996 continue;
997 }
998 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
999 wildcard++;
1000 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
1001 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1002 continue;
1003 if (!IN6_IS_ADDR_V4MAPPED(laddr6))
1004 continue;
1005
1006 /* duplicate of IPv4 logic */
1007 wildcard = 0;
1008 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) &&
1009 in6p->in6p_faddr.s6_addr32[3])
1010 wildcard++;
1011 if (!in6p->in6p_laddr.s6_addr32[3]) {
1012 if (laddr6->s6_addr32[3])
1013 wildcard++;
1014 } else {
1015 if (!laddr6->s6_addr32[3])
1016 wildcard++;
1017 else {
1018 if (in6p->in6p_laddr.s6_addr32[3] !=
1019 laddr6->s6_addr32[3])
1020 continue;
1021 }
1022 }
1023 } else if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1024 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1025 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1026 continue;
1027 }
1028 if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
1029 wildcard++;
1030 } else {
1031 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1032 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1033 continue;
1034 }
1035 if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
1036 wildcard++;
1037 else {
1038 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
1039 laddr6))
1040 continue;
1041 }
1042 }
1043 if (wildcard && !lookup_wildcard)
1044 continue;
1045 if (wildcard < matchwild) {
1046 match = in6p;
1047 matchwild = wildcard;
1048 if (matchwild == 0)
1049 break;
1050 }
1051 }
1052 if (match && matchwild == 0)
1053 return match;
1054
1055 if (vp && table->vestige && table->vestige->init_ports6) {
1056 struct vestigial_inpcb better;
1057 void *state;
1058
1059 state = (*table->vestige->init_ports6)(laddr6,
1060 lport_arg,
1061 lookup_wildcard);
1062 while (table->vestige
1063 && (*table->vestige->next_port6)(state, vp)) {
1064
1065 if (vp->lport != lport)
1066 continue;
1067 wildcard = 0;
1068 if (!IN6_IS_ADDR_UNSPECIFIED(&vp->faddr.v6))
1069 wildcard++;
1070 if (IN6_IS_ADDR_UNSPECIFIED(&vp->laddr.v6)) {
1071 if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
1072 wildcard++;
1073 } else {
1074 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1075 if (vp->v6only)
1076 continue;
1077 }
1078 if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
1079 wildcard++;
1080 else {
1081 if (!IN6_ARE_ADDR_EQUAL(&vp->laddr.v6, laddr6))
1082 continue;
1083 }
1084 }
1085 if (wildcard && !lookup_wildcard)
1086 continue;
1087 if (wildcard < matchwild) {
1088 better = *vp;
1089 match = (void*)&better;
1090
1091 matchwild = wildcard;
1092 if (matchwild == 0)
1093 break;
1094 }
1095 }
1096
1097 if (match) {
1098 if (match != (void*)&better)
1099 return match;
1100 else {
1101 *vp = better;
1102 return 0;
1103 }
1104 }
1105 }
1106 return (match);
1107 }
1108
1109 /*
1110 * WARNING: return value (rtentry) could be IPv4 one if in6pcb is connected to
1111 * IPv4 mapped address.
1112 */
1113 struct rtentry *
1114 in6_pcbrtentry(struct in6pcb *in6p)
1115 {
1116 struct rtentry *rt;
1117 struct route *ro;
1118 union {
1119 const struct sockaddr *sa;
1120 const struct sockaddr_in6 *sa6;
1121 #ifdef INET
1122 const struct sockaddr_in *sa4;
1123 #endif
1124 } cdst;
1125
1126 ro = &in6p->in6p_route;
1127
1128 if (in6p->in6p_af != AF_INET6)
1129 return (NULL);
1130
1131 cdst.sa = rtcache_getdst(ro);
1132 if (cdst.sa == NULL)
1133 ;
1134 #ifdef INET
1135 else if (cdst.sa->sa_family == AF_INET) {
1136 KASSERT(IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr));
1137 if (cdst.sa4->sin_addr.s_addr != in6p->in6p_faddr.s6_addr32[3])
1138 rtcache_free(ro);
1139 }
1140 #endif
1141 else {
1142 if (!IN6_ARE_ADDR_EQUAL(&cdst.sa6->sin6_addr,
1143 &in6p->in6p_faddr))
1144 rtcache_free(ro);
1145 }
1146 if ((rt = rtcache_validate(ro)) == NULL)
1147 rt = rtcache_update(ro, 1);
1148 #ifdef INET
1149 if (rt == NULL && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
1150 union {
1151 struct sockaddr dst;
1152 struct sockaddr_in dst4;
1153 } u;
1154 struct in_addr addr;
1155
1156 addr.s_addr = in6p->in6p_faddr.s6_addr32[3];
1157
1158 sockaddr_in_init(&u.dst4, &addr, 0);
1159 if (rtcache_setdst(ro, &u.dst) != 0)
1160 return NULL;
1161
1162 rt = rtcache_init(ro);
1163 } else
1164 #endif
1165 if (rt == NULL && !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
1166 union {
1167 struct sockaddr dst;
1168 struct sockaddr_in6 dst6;
1169 } u;
1170
1171 sockaddr_in6_init(&u.dst6, &in6p->in6p_faddr, 0, 0, 0);
1172 if (rtcache_setdst(ro, &u.dst) != 0)
1173 return NULL;
1174
1175 rt = rtcache_init(ro);
1176 }
1177 return rt;
1178 }
1179
1180 void
1181 in6_pcbrtentry_unref(struct rtentry *rt, struct in6pcb *in6p)
1182 {
1183
1184 rtcache_unref(rt, &in6p->in6p_route);
1185 }
1186
1187 struct in6pcb *
1188 in6_pcblookup_connect(struct inpcbtable *table, const struct in6_addr *faddr6,
1189 u_int fport_arg, const struct in6_addr *laddr6, u_int lport_arg,
1190 int faith,
1191 struct vestigial_inpcb *vp)
1192 {
1193 struct inpcbhead *head;
1194 struct inpcb_hdr *inph;
1195 struct in6pcb *in6p;
1196 u_int16_t fport = fport_arg, lport = lport_arg;
1197
1198 if (vp)
1199 vp->valid = 0;
1200
1201 head = IN6PCBHASH_CONNECT(table, faddr6, fport, laddr6, lport);
1202 LIST_FOREACH(inph, head, inph_hash) {
1203 in6p = (struct in6pcb *)inph;
1204 if (in6p->in6p_af != AF_INET6)
1205 continue;
1206
1207 /* find exact match on both source and dest */
1208 if (in6p->in6p_fport != fport)
1209 continue;
1210 if (in6p->in6p_lport != lport)
1211 continue;
1212 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
1213 continue;
1214 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
1215 continue;
1216 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1217 continue;
1218 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
1219 continue;
1220 if ((IN6_IS_ADDR_V4MAPPED(laddr6) ||
1221 IN6_IS_ADDR_V4MAPPED(faddr6)) &&
1222 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1223 continue;
1224 return in6p;
1225 }
1226 if (vp && table->vestige) {
1227 if ((*table->vestige->lookup6)(faddr6, fport_arg,
1228 laddr6, lport_arg, vp))
1229 return NULL;
1230 }
1231
1232 return NULL;
1233 }
1234
1235 struct in6pcb *
1236 in6_pcblookup_bind(struct inpcbtable *table, const struct in6_addr *laddr6,
1237 u_int lport_arg, int faith)
1238 {
1239 struct inpcbhead *head;
1240 struct inpcb_hdr *inph;
1241 struct in6pcb *in6p;
1242 u_int16_t lport = lport_arg;
1243 #ifdef INET
1244 struct in6_addr zero_mapped;
1245 #endif
1246
1247 head = IN6PCBHASH_BIND(table, laddr6, lport);
1248 LIST_FOREACH(inph, head, inph_hash) {
1249 in6p = (struct in6pcb *)inph;
1250 if (in6p->in6p_af != AF_INET6)
1251 continue;
1252
1253 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1254 continue;
1255 if (in6p->in6p_fport != 0)
1256 continue;
1257 if (in6p->in6p_lport != lport)
1258 continue;
1259 if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
1260 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1261 continue;
1262 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
1263 goto out;
1264 }
1265 #ifdef INET
1266 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1267 memset(&zero_mapped, 0, sizeof(zero_mapped));
1268 zero_mapped.s6_addr16[5] = 0xffff;
1269 head = IN6PCBHASH_BIND(table, &zero_mapped, lport);
1270 LIST_FOREACH(inph, head, inph_hash) {
1271 in6p = (struct in6pcb *)inph;
1272 if (in6p->in6p_af != AF_INET6)
1273 continue;
1274
1275 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1276 continue;
1277 if (in6p->in6p_fport != 0)
1278 continue;
1279 if (in6p->in6p_lport != lport)
1280 continue;
1281 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1282 continue;
1283 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zero_mapped))
1284 goto out;
1285 }
1286 }
1287 #endif
1288 head = IN6PCBHASH_BIND(table, &zeroin6_addr, lport);
1289 LIST_FOREACH(inph, head, inph_hash) {
1290 in6p = (struct in6pcb *)inph;
1291 if (in6p->in6p_af != AF_INET6)
1292 continue;
1293
1294 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1295 continue;
1296 if (in6p->in6p_fport != 0)
1297 continue;
1298 if (in6p->in6p_lport != lport)
1299 continue;
1300 if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
1301 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1302 continue;
1303 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zeroin6_addr))
1304 goto out;
1305 }
1306 return (NULL);
1307
1308 out:
1309 inph = &in6p->in6p_head;
1310 if (inph != LIST_FIRST(head)) {
1311 LIST_REMOVE(inph, inph_hash);
1312 LIST_INSERT_HEAD(head, inph, inph_hash);
1313 }
1314 return in6p;
1315 }
1316
1317 void
1318 in6_pcbstate(struct in6pcb *in6p, int state)
1319 {
1320
1321 if (in6p->in6p_af != AF_INET6)
1322 return;
1323
1324 if (in6p->in6p_state > IN6P_ATTACHED)
1325 LIST_REMOVE(&in6p->in6p_head, inph_hash);
1326
1327 switch (state) {
1328 case IN6P_BOUND:
1329 LIST_INSERT_HEAD(IN6PCBHASH_BIND(in6p->in6p_table,
1330 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
1331 inph_hash);
1332 break;
1333 case IN6P_CONNECTED:
1334 LIST_INSERT_HEAD(IN6PCBHASH_CONNECT(in6p->in6p_table,
1335 &in6p->in6p_faddr, in6p->in6p_fport,
1336 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
1337 inph_hash);
1338 break;
1339 }
1340
1341 in6p->in6p_state = state;
1342 }
1343