in6_pcb.c revision 1.153 1 /* $NetBSD: in6_pcb.c,v 1.153 2016/12/08 05:16:34 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.153 2016/12/08 05:16:34 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 #include <netinet6/nd6.h>
100
101 #include "faith.h"
102
103 #ifdef IPSEC
104 #include <netipsec/ipsec.h>
105 #include <netipsec/ipsec6.h>
106 #include <netipsec/key.h>
107 #endif /* IPSEC */
108
109 #include <netinet/tcp_vtw.h>
110
111 const struct in6_addr zeroin6_addr;
112
113 #define IN6PCBHASH_PORT(table, lport) \
114 &(table)->inpt_porthashtbl[ntohs(lport) & (table)->inpt_porthash]
115 #define IN6PCBHASH_BIND(table, laddr, lport) \
116 &(table)->inpt_bindhashtbl[ \
117 (((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \
118 (laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + ntohs(lport)) & \
119 (table)->inpt_bindhash]
120 #define IN6PCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
121 &(table)->inpt_bindhashtbl[ \
122 ((((faddr)->s6_addr32[0] ^ (faddr)->s6_addr32[1] ^ \
123 (faddr)->s6_addr32[2] ^ (faddr)->s6_addr32[3]) + ntohs(fport)) + \
124 (((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \
125 (laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + \
126 ntohs(lport))) & (table)->inpt_bindhash]
127
128 int ip6_anonportmin = IPV6PORT_ANONMIN;
129 int ip6_anonportmax = IPV6PORT_ANONMAX;
130 int ip6_lowportmin = IPV6PORT_RESERVEDMIN;
131 int ip6_lowportmax = IPV6PORT_RESERVEDMAX;
132
133 static struct pool in6pcb_pool;
134
135 static int
136 in6pcb_poolinit(void)
137 {
138
139 pool_init(&in6pcb_pool, sizeof(struct in6pcb), 0, 0, 0, "in6pcbpl",
140 NULL, IPL_SOFTNET);
141 return 0;
142 }
143
144 void
145 in6_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize)
146 {
147 static ONCE_DECL(control);
148
149 in_pcbinit(table, bindhashsize, connecthashsize);
150 table->inpt_lastport = (u_int16_t)ip6_anonportmax;
151
152 RUN_ONCE(&control, in6pcb_poolinit);
153 }
154
155 int
156 in6_pcballoc(struct socket *so, void *v)
157 {
158 struct inpcbtable *table = v;
159 struct in6pcb *in6p;
160 int s;
161
162 s = splnet();
163 in6p = pool_get(&in6pcb_pool, PR_NOWAIT);
164 splx(s);
165 if (in6p == NULL)
166 return (ENOBUFS);
167 memset((void *)in6p, 0, sizeof(*in6p));
168 in6p->in6p_af = AF_INET6;
169 in6p->in6p_table = table;
170 in6p->in6p_socket = so;
171 in6p->in6p_hops = -1; /* use kernel default */
172 in6p->in6p_icmp6filt = NULL;
173 in6p->in6p_portalgo = PORTALGO_DEFAULT;
174 in6p->in6p_bindportonsend = false;
175 #if defined(IPSEC)
176 if (ipsec_enabled) {
177 int error = ipsec_init_pcbpolicy(so, &in6p->in6p_sp);
178 if (error != 0) {
179 s = splnet();
180 pool_put(&in6pcb_pool, in6p);
181 splx(s);
182 return error;
183 }
184 }
185 #endif /* IPSEC */
186 s = splnet();
187 TAILQ_INSERT_HEAD(&table->inpt_queue, (struct inpcb_hdr*)in6p,
188 inph_queue);
189 LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport),
190 &in6p->in6p_head, inph_lhash);
191 in6_pcbstate(in6p, IN6P_ATTACHED);
192 splx(s);
193 if (ip6_v6only)
194 in6p->in6p_flags |= IN6P_IPV6_V6ONLY;
195 so->so_pcb = (void *)in6p;
196 return (0);
197 }
198
199 /*
200 * Bind address from sin6 to in6p.
201 */
202 static int
203 in6_pcbbind_addr(struct in6pcb *in6p, struct sockaddr_in6 *sin6, struct lwp *l)
204 {
205 int error;
206 int s;
207
208 /*
209 * We should check the family, but old programs
210 * incorrectly fail to intialize it.
211 */
212 if (sin6->sin6_family != AF_INET6)
213 return (EAFNOSUPPORT);
214
215 #ifndef INET
216 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
217 return (EADDRNOTAVAIL);
218 #endif
219
220 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
221 return (error);
222
223 s = pserialize_read_enter();
224 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
225 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) {
226 error = EINVAL;
227 goto out;
228 }
229 if (sin6->sin6_addr.s6_addr32[3]) {
230 struct sockaddr_in sin;
231
232 memset(&sin, 0, sizeof(sin));
233 sin.sin_len = sizeof(sin);
234 sin.sin_family = AF_INET;
235 bcopy(&sin6->sin6_addr.s6_addr32[3],
236 &sin.sin_addr, sizeof(sin.sin_addr));
237 if (!IN_MULTICAST(sin.sin_addr.s_addr)) {
238 struct ifaddr *ifa;
239 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
240 if (ifa == NULL) {
241 error = EADDRNOTAVAIL;
242 goto out;
243 }
244 }
245 }
246 } else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
247 // succeed
248 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
249 struct ifaddr *ifa = NULL;
250
251 if ((in6p->in6p_flags & IN6P_FAITH) == 0) {
252 ifa = ifa_ifwithaddr(sin6tosa(sin6));
253 if (ifa == NULL) {
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 return (error);
518 }
519 memset(&mapped, 0, sizeof(mapped));
520 mapped.s6_addr16[5] = htons(0xffff);
521 memcpy(&mapped.s6_addr32[3], &IA_SIN(ia4)->sin_addr,
522 sizeof(IA_SIN(ia4)->sin_addr));
523 ia4_release(ia4, &_psref);
524 in6a = &mapped;
525 #else
526 return EADDRNOTAVAIL;
527 #endif
528 } else {
529 /*
530 * XXX: in6_selectsrc might replace the bound local address
531 * with the address specified by setsockopt(IPV6_PKTINFO).
532 * Is it the intended behavior?
533 */
534 error = in6_selectsrc(sin6, in6p->in6p_outputopts,
535 in6p->in6p_moptions, &in6p->in6p_route, &in6p->in6p_laddr,
536 &ifp, &psref, &ia6);
537 if (error == 0)
538 in6a = &ia6;
539 if (ifp && scope_ambiguous &&
540 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
541 if_put(ifp, &psref);
542 curlwp_bindx(bound);
543 return error;
544 }
545
546 if (in6a == NULL) {
547 if_put(ifp, &psref);
548 curlwp_bindx(bound);
549 if (error == 0)
550 error = EADDRNOTAVAIL;
551 return error;
552 }
553 }
554
555 if (ifp != NULL) {
556 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
557 if_put(ifp, &psref);
558 } else
559 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim_rt(in6p);
560 curlwp_bindx(bound);
561
562 if (in6_pcblookup_connect(in6p->in6p_table, &sin6->sin6_addr,
563 sin6->sin6_port,
564 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ? in6a : &in6p->in6p_laddr,
565 in6p->in6p_lport, 0, &vestige)
566 || vestige.valid)
567 return (EADDRINUSE);
568 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ||
569 (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
570 in6p->in6p_laddr.s6_addr32[3] == 0))
571 {
572 if (in6p->in6p_lport == 0) {
573 error = in6_pcbbind(in6p, NULL, l);
574 if (error != 0)
575 return error;
576 }
577 in6p->in6p_laddr = *in6a;
578 }
579 in6p->in6p_faddr = sin6->sin6_addr;
580 in6p->in6p_fport = sin6->sin6_port;
581
582 /* Late bind, if needed */
583 if (in6p->in6p_bindportonsend) {
584 struct sockaddr_in6 lsin = *((const struct sockaddr_in6 *)
585 in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
586 lsin.sin6_addr = in6p->in6p_laddr;
587 lsin.sin6_port = 0;
588
589 if ((error = in6_pcbbind_port(in6p, &lsin, l)) != 0)
590 return error;
591 }
592
593 in6_pcbstate(in6p, IN6P_CONNECTED);
594 in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
595 if (ip6_auto_flowlabel)
596 in6p->in6p_flowinfo |=
597 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
598 #if defined(IPSEC)
599 if (ipsec_enabled && in6p->in6p_socket->so_type == SOCK_STREAM)
600 ipsec_pcbconn(in6p->in6p_sp);
601 #endif
602 return (0);
603 }
604
605 void
606 in6_pcbdisconnect(struct in6pcb *in6p)
607 {
608 memset((void *)&in6p->in6p_faddr, 0, sizeof(in6p->in6p_faddr));
609 in6p->in6p_fport = 0;
610 in6_pcbstate(in6p, IN6P_BOUND);
611 in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
612 #if defined(IPSEC)
613 if (ipsec_enabled)
614 ipsec_pcbdisconn(in6p->in6p_sp);
615 #endif
616 if (in6p->in6p_socket->so_state & SS_NOFDREF)
617 in6_pcbdetach(in6p);
618 }
619
620 void
621 in6_pcbdetach(struct in6pcb *in6p)
622 {
623 struct socket *so = in6p->in6p_socket;
624 int s;
625
626 if (in6p->in6p_af != AF_INET6)
627 return;
628
629 #if defined(IPSEC)
630 if (ipsec_enabled)
631 ipsec6_delete_pcbpolicy(in6p);
632 #endif
633 so->so_pcb = NULL;
634
635 s = splnet();
636 in6_pcbstate(in6p, IN6P_ATTACHED);
637 LIST_REMOVE(&in6p->in6p_head, inph_lhash);
638 TAILQ_REMOVE(&in6p->in6p_table->inpt_queue, &in6p->in6p_head,
639 inph_queue);
640 splx(s);
641
642 if (in6p->in6p_options) {
643 m_freem(in6p->in6p_options);
644 }
645 if (in6p->in6p_outputopts != NULL) {
646 ip6_clearpktopts(in6p->in6p_outputopts, -1);
647 free(in6p->in6p_outputopts, M_IP6OPT);
648 }
649 rtcache_free(&in6p->in6p_route);
650 ip6_freemoptions(in6p->in6p_moptions);
651 ip_freemoptions(in6p->in6p_v4moptions);
652 sofree(so); /* drops the socket's lock */
653
654 pool_put(&in6pcb_pool, in6p);
655 mutex_enter(softnet_lock); /* reacquire it */
656 }
657
658 void
659 in6_setsockaddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6)
660 {
661
662 if (in6p->in6p_af != AF_INET6)
663 return;
664
665 sockaddr_in6_init(sin6, &in6p->in6p_laddr, in6p->in6p_lport, 0, 0);
666 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
667 }
668
669 void
670 in6_setpeeraddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6)
671 {
672
673 if (in6p->in6p_af != AF_INET6)
674 return;
675
676 sockaddr_in6_init(sin6, &in6p->in6p_faddr, in6p->in6p_fport, 0, 0);
677 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
678 }
679
680 /*
681 * Pass some notification to all connections of a protocol
682 * associated with address dst. The local address and/or port numbers
683 * may be specified to limit the search. The "usual action" will be
684 * taken, depending on the ctlinput cmd. The caller must filter any
685 * cmds that are uninteresting (e.g., no error in the map).
686 * Call the protocol specific routine (if any) to report
687 * any errors for each matching socket.
688 *
689 * Must be called at splsoftnet.
690 *
691 * Note: src (4th arg) carries the flowlabel value on the original IPv6
692 * header, in sin6_flowinfo member.
693 */
694 int
695 in6_pcbnotify(struct inpcbtable *table, const struct sockaddr *dst,
696 u_int fport_arg, const struct sockaddr *src, u_int lport_arg, int cmd,
697 void *cmdarg, void (*notify)(struct in6pcb *, int))
698 {
699 struct inpcb_hdr *inph, *ninph;
700 struct sockaddr_in6 sa6_src;
701 const struct sockaddr_in6 *sa6_dst;
702 u_int16_t fport = fport_arg, lport = lport_arg;
703 int errno;
704 int nmatch = 0;
705 u_int32_t flowinfo;
706
707 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
708 return 0;
709
710 sa6_dst = (const struct sockaddr_in6 *)dst;
711 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
712 return 0;
713
714 /*
715 * note that src can be NULL when we get notify by local fragmentation.
716 */
717 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
718 flowinfo = sa6_src.sin6_flowinfo;
719
720 /*
721 * Redirects go to all references to the destination,
722 * and use in6_rtchange to invalidate the route cache.
723 * Dead host indications: also use in6_rtchange to invalidate
724 * the cache, and deliver the error to all the sockets.
725 * Otherwise, if we have knowledge of the local port and address,
726 * deliver only to that socket.
727 */
728 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
729 fport = 0;
730 lport = 0;
731 memset((void *)&sa6_src.sin6_addr, 0, sizeof(sa6_src.sin6_addr));
732
733 if (cmd != PRC_HOSTDEAD)
734 notify = in6_rtchange;
735 }
736
737 errno = inet6ctlerrmap[cmd];
738 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
739 struct in6pcb *in6p = (struct in6pcb *)inph;
740 struct rtentry *rt = NULL;
741
742 if (in6p->in6p_af != AF_INET6)
743 continue;
744
745 /*
746 * Under the following condition, notify of redirects
747 * to the pcb, without making address matches against inpcb.
748 * - redirect notification is arrived.
749 * - the inpcb is unconnected.
750 * - the inpcb is caching !RTF_HOST routing entry.
751 * - the ICMPv6 notification is from the gateway cached in the
752 * inpcb. i.e. ICMPv6 notification is from nexthop gateway
753 * the inpcb used very recently.
754 *
755 * This is to improve interaction between netbsd/openbsd
756 * redirect handling code, and inpcb route cache code.
757 * without the clause, !RTF_HOST routing entry (which carries
758 * gateway used by inpcb right before the ICMPv6 redirect)
759 * will be cached forever in unconnected inpcb.
760 *
761 * There still is a question regarding to what is TRT:
762 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
763 * generated on packet output. inpcb will always cache
764 * RTF_HOST routing entry so there's no need for the clause
765 * (ICMPv6 redirect will update RTF_HOST routing entry,
766 * and inpcb is caching it already).
767 * However, bsdi/freebsd are vulnerable to local DoS attacks
768 * due to the cloned routing entries.
769 * - Specwise, "destination cache" is mentioned in RFC2461.
770 * Jinmei says that it implies bsdi/freebsd behavior, itojun
771 * is not really convinced.
772 * - Having hiwat/lowat on # of cloned host route (redirect/
773 * pmtud) may be a good idea. netbsd/openbsd has it. see
774 * icmp6_mtudisc_update().
775 */
776 if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
777 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
778 (rt = rtcache_validate(&in6p->in6p_route)) != NULL &&
779 !(rt->rt_flags & RTF_HOST)) {
780 const struct sockaddr_in6 *dst6;
781
782 dst6 = (const struct sockaddr_in6 *)
783 rtcache_getdst(&in6p->in6p_route);
784 if (dst6 == NULL)
785 ;
786 else if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
787 &sa6_dst->sin6_addr)) {
788 rtcache_unref(rt, &in6p->in6p_route);
789 goto do_notify;
790 }
791 }
792 rtcache_unref(rt, &in6p->in6p_route);
793
794 /*
795 * If the error designates a new path MTU for a destination
796 * and the application (associated with this socket) wanted to
797 * know the value, notify. Note that we notify for all
798 * disconnected sockets if the corresponding application
799 * wanted. This is because some UDP applications keep sending
800 * sockets disconnected.
801 * XXX: should we avoid to notify the value to TCP sockets?
802 */
803 if (cmd == PRC_MSGSIZE && (in6p->in6p_flags & IN6P_MTU) != 0 &&
804 (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) ||
805 IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &sa6_dst->sin6_addr))) {
806 ip6_notify_pmtu(in6p, (const struct sockaddr_in6 *)dst,
807 (u_int32_t *)cmdarg);
808 }
809
810 /*
811 * Detect if we should notify the error. If no source and
812 * destination ports are specified, but non-zero flowinfo and
813 * local address match, notify the error. This is the case
814 * when the error is delivered with an encrypted buffer
815 * by ESP. Otherwise, just compare addresses and ports
816 * as usual.
817 */
818 if (lport == 0 && fport == 0 && flowinfo &&
819 in6p->in6p_socket != NULL &&
820 flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
821 IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr))
822 goto do_notify;
823 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
824 &sa6_dst->sin6_addr) ||
825 in6p->in6p_socket == NULL ||
826 (lport && in6p->in6p_lport != lport) ||
827 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
828 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
829 &sa6_src.sin6_addr)) ||
830 (fport && in6p->in6p_fport != fport))
831 continue;
832
833 do_notify:
834 if (notify)
835 (*notify)(in6p, errno);
836 nmatch++;
837 }
838 return nmatch;
839 }
840
841 void
842 in6_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp)
843 {
844 struct inpcb_hdr *inph, *ninph;
845 struct ip6_moptions *im6o;
846 struct in6_multi_mship *imm, *nimm;
847
848 KASSERT(ifp != NULL);
849
850 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
851 struct in6pcb *in6p = (struct in6pcb *)inph;
852 if (in6p->in6p_af != AF_INET6)
853 continue;
854
855 im6o = in6p->in6p_moptions;
856 if (im6o) {
857 /*
858 * Unselect the outgoing interface if it is being
859 * detached.
860 */
861 if (im6o->im6o_multicast_if_index == ifp->if_index)
862 im6o->im6o_multicast_if_index = 0;
863
864 /*
865 * Drop multicast group membership if we joined
866 * through the interface being detached.
867 * XXX controversial - is it really legal for kernel
868 * to force this?
869 */
870 for (imm = im6o->im6o_memberships.lh_first;
871 imm != NULL; imm = nimm) {
872 nimm = imm->i6mm_chain.le_next;
873 if (imm->i6mm_maddr->in6m_ifp == ifp) {
874 LIST_REMOVE(imm, i6mm_chain);
875 in6_leavegroup(imm);
876 }
877 }
878 }
879 in_purgeifmcast(in6p->in6p_v4moptions, ifp);
880 }
881 }
882
883 void
884 in6_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp)
885 {
886 struct rtentry *rt;
887 struct inpcb_hdr *inph, *ninph;
888
889 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
890 struct in6pcb *in6p = (struct in6pcb *)inph;
891 if (in6p->in6p_af != AF_INET6)
892 continue;
893 if ((rt = rtcache_validate(&in6p->in6p_route)) != NULL &&
894 rt->rt_ifp == ifp) {
895 rtcache_unref(rt, &in6p->in6p_route);
896 in6_rtchange(in6p, 0);
897 } else
898 rtcache_unref(rt, &in6p->in6p_route);
899 }
900 }
901
902 /*
903 * Check for alternatives when higher level complains
904 * about service problems. For now, invalidate cached
905 * routing information. If the route was created dynamically
906 * (by a redirect), time to try a default gateway again.
907 */
908 void
909 in6_losing(struct in6pcb *in6p)
910 {
911 struct rtentry *rt;
912 struct rt_addrinfo info;
913
914 if (in6p->in6p_af != AF_INET6)
915 return;
916
917 if ((rt = rtcache_validate(&in6p->in6p_route)) == NULL)
918 return;
919
920 memset(&info, 0, sizeof(info));
921 info.rti_info[RTAX_DST] = rtcache_getdst(&in6p->in6p_route);
922 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
923 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
924 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
925 if (rt->rt_flags & RTF_DYNAMIC) {
926 int error;
927 struct rtentry *nrt;
928
929 error = rtrequest(RTM_DELETE, rt_getkey(rt),
930 rt->rt_gateway, rt_mask(rt), rt->rt_flags, &nrt);
931 rtcache_unref(rt, &in6p->in6p_route);
932 if (error == 0)
933 rtfree(nrt);
934 } else
935 rtcache_unref(rt, &in6p->in6p_route);
936 /*
937 * A new route can be allocated
938 * the next time output is attempted.
939 */
940 rtcache_free(&in6p->in6p_route);
941 }
942
943 /*
944 * After a routing change, flush old routing. A new route can be
945 * allocated the next time output is attempted.
946 */
947 void
948 in6_rtchange(struct in6pcb *in6p, int errno)
949 {
950 if (in6p->in6p_af != AF_INET6)
951 return;
952
953 rtcache_free(&in6p->in6p_route);
954 /*
955 * A new route can be allocated the next time
956 * output is attempted.
957 */
958 }
959
960 struct in6pcb *
961 in6_pcblookup_port(struct inpcbtable *table, struct in6_addr *laddr6,
962 u_int lport_arg, int lookup_wildcard, struct vestigial_inpcb *vp)
963 {
964 struct inpcbhead *head;
965 struct inpcb_hdr *inph;
966 struct in6pcb *in6p, *match = NULL;
967 int matchwild = 3, wildcard;
968 u_int16_t lport = lport_arg;
969
970 if (vp)
971 vp->valid = 0;
972
973 head = IN6PCBHASH_PORT(table, lport);
974 LIST_FOREACH(inph, head, inph_lhash) {
975 in6p = (struct in6pcb *)inph;
976 if (in6p->in6p_af != AF_INET6)
977 continue;
978
979 if (in6p->in6p_lport != lport)
980 continue;
981 wildcard = 0;
982 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
983 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
984 continue;
985 }
986 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
987 wildcard++;
988 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
989 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
990 continue;
991 if (!IN6_IS_ADDR_V4MAPPED(laddr6))
992 continue;
993
994 /* duplicate of IPv4 logic */
995 wildcard = 0;
996 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) &&
997 in6p->in6p_faddr.s6_addr32[3])
998 wildcard++;
999 if (!in6p->in6p_laddr.s6_addr32[3]) {
1000 if (laddr6->s6_addr32[3])
1001 wildcard++;
1002 } else {
1003 if (!laddr6->s6_addr32[3])
1004 wildcard++;
1005 else {
1006 if (in6p->in6p_laddr.s6_addr32[3] !=
1007 laddr6->s6_addr32[3])
1008 continue;
1009 }
1010 }
1011 } else if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1012 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1013 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1014 continue;
1015 }
1016 if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
1017 wildcard++;
1018 } else {
1019 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1020 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1021 continue;
1022 }
1023 if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
1024 wildcard++;
1025 else {
1026 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
1027 laddr6))
1028 continue;
1029 }
1030 }
1031 if (wildcard && !lookup_wildcard)
1032 continue;
1033 if (wildcard < matchwild) {
1034 match = in6p;
1035 matchwild = wildcard;
1036 if (matchwild == 0)
1037 break;
1038 }
1039 }
1040 if (match && matchwild == 0)
1041 return match;
1042
1043 if (vp && table->vestige && table->vestige->init_ports6) {
1044 struct vestigial_inpcb better;
1045 void *state;
1046
1047 state = (*table->vestige->init_ports6)(laddr6,
1048 lport_arg,
1049 lookup_wildcard);
1050 while (table->vestige
1051 && (*table->vestige->next_port6)(state, vp)) {
1052
1053 if (vp->lport != lport)
1054 continue;
1055 wildcard = 0;
1056 if (!IN6_IS_ADDR_UNSPECIFIED(&vp->faddr.v6))
1057 wildcard++;
1058 if (IN6_IS_ADDR_UNSPECIFIED(&vp->laddr.v6)) {
1059 if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
1060 wildcard++;
1061 } else {
1062 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1063 if (vp->v6only)
1064 continue;
1065 }
1066 if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
1067 wildcard++;
1068 else {
1069 if (!IN6_ARE_ADDR_EQUAL(&vp->laddr.v6, laddr6))
1070 continue;
1071 }
1072 }
1073 if (wildcard && !lookup_wildcard)
1074 continue;
1075 if (wildcard < matchwild) {
1076 better = *vp;
1077 match = (void*)&better;
1078
1079 matchwild = wildcard;
1080 if (matchwild == 0)
1081 break;
1082 }
1083 }
1084
1085 if (match) {
1086 if (match != (void*)&better)
1087 return match;
1088 else {
1089 *vp = better;
1090 return 0;
1091 }
1092 }
1093 }
1094 return (match);
1095 }
1096
1097 /*
1098 * WARNING: return value (rtentry) could be IPv4 one if in6pcb is connected to
1099 * IPv4 mapped address.
1100 */
1101 struct rtentry *
1102 in6_pcbrtentry(struct in6pcb *in6p)
1103 {
1104 struct rtentry *rt;
1105 struct route *ro;
1106 union {
1107 const struct sockaddr *sa;
1108 const struct sockaddr_in6 *sa6;
1109 #ifdef INET
1110 const struct sockaddr_in *sa4;
1111 #endif
1112 } cdst;
1113
1114 ro = &in6p->in6p_route;
1115
1116 if (in6p->in6p_af != AF_INET6)
1117 return (NULL);
1118
1119 cdst.sa = rtcache_getdst(ro);
1120 if (cdst.sa == NULL)
1121 ;
1122 #ifdef INET
1123 else if (cdst.sa->sa_family == AF_INET) {
1124 KASSERT(IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr));
1125 if (cdst.sa4->sin_addr.s_addr != in6p->in6p_faddr.s6_addr32[3])
1126 rtcache_free(ro);
1127 }
1128 #endif
1129 else {
1130 if (!IN6_ARE_ADDR_EQUAL(&cdst.sa6->sin6_addr,
1131 &in6p->in6p_faddr))
1132 rtcache_free(ro);
1133 }
1134 if ((rt = rtcache_validate(ro)) == NULL)
1135 rt = rtcache_update(ro, 1);
1136 #ifdef INET
1137 if (rt == NULL && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
1138 union {
1139 struct sockaddr dst;
1140 struct sockaddr_in dst4;
1141 } u;
1142 struct in_addr addr;
1143
1144 addr.s_addr = in6p->in6p_faddr.s6_addr32[3];
1145
1146 sockaddr_in_init(&u.dst4, &addr, 0);
1147 if (rtcache_setdst(ro, &u.dst) != 0)
1148 return NULL;
1149
1150 rt = rtcache_init(ro);
1151 } else
1152 #endif
1153 if (rt == NULL && !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
1154 union {
1155 struct sockaddr dst;
1156 struct sockaddr_in6 dst6;
1157 } u;
1158
1159 sockaddr_in6_init(&u.dst6, &in6p->in6p_faddr, 0, 0, 0);
1160 if (rtcache_setdst(ro, &u.dst) != 0)
1161 return NULL;
1162
1163 rt = rtcache_init(ro);
1164 }
1165 return rt;
1166 }
1167
1168 void
1169 in6_pcbrtentry_unref(struct rtentry *rt, struct in6pcb *in6p)
1170 {
1171
1172 rtcache_unref(rt, &in6p->in6p_route);
1173 }
1174
1175 struct in6pcb *
1176 in6_pcblookup_connect(struct inpcbtable *table, const struct in6_addr *faddr6,
1177 u_int fport_arg, const struct in6_addr *laddr6, u_int lport_arg,
1178 int faith,
1179 struct vestigial_inpcb *vp)
1180 {
1181 struct inpcbhead *head;
1182 struct inpcb_hdr *inph;
1183 struct in6pcb *in6p;
1184 u_int16_t fport = fport_arg, lport = lport_arg;
1185
1186 if (vp)
1187 vp->valid = 0;
1188
1189 head = IN6PCBHASH_CONNECT(table, faddr6, fport, laddr6, lport);
1190 LIST_FOREACH(inph, head, inph_hash) {
1191 in6p = (struct in6pcb *)inph;
1192 if (in6p->in6p_af != AF_INET6)
1193 continue;
1194
1195 /* find exact match on both source and dest */
1196 if (in6p->in6p_fport != fport)
1197 continue;
1198 if (in6p->in6p_lport != lport)
1199 continue;
1200 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
1201 continue;
1202 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
1203 continue;
1204 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1205 continue;
1206 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
1207 continue;
1208 if ((IN6_IS_ADDR_V4MAPPED(laddr6) ||
1209 IN6_IS_ADDR_V4MAPPED(faddr6)) &&
1210 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1211 continue;
1212 return in6p;
1213 }
1214 if (vp && table->vestige) {
1215 if ((*table->vestige->lookup6)(faddr6, fport_arg,
1216 laddr6, lport_arg, vp))
1217 return NULL;
1218 }
1219
1220 return NULL;
1221 }
1222
1223 struct in6pcb *
1224 in6_pcblookup_bind(struct inpcbtable *table, const struct in6_addr *laddr6,
1225 u_int lport_arg, int faith)
1226 {
1227 struct inpcbhead *head;
1228 struct inpcb_hdr *inph;
1229 struct in6pcb *in6p;
1230 u_int16_t lport = lport_arg;
1231 #ifdef INET
1232 struct in6_addr zero_mapped;
1233 #endif
1234
1235 head = IN6PCBHASH_BIND(table, laddr6, lport);
1236 LIST_FOREACH(inph, head, inph_hash) {
1237 in6p = (struct in6pcb *)inph;
1238 if (in6p->in6p_af != AF_INET6)
1239 continue;
1240
1241 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1242 continue;
1243 if (in6p->in6p_fport != 0)
1244 continue;
1245 if (in6p->in6p_lport != lport)
1246 continue;
1247 if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
1248 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1249 continue;
1250 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
1251 goto out;
1252 }
1253 #ifdef INET
1254 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1255 memset(&zero_mapped, 0, sizeof(zero_mapped));
1256 zero_mapped.s6_addr16[5] = 0xffff;
1257 head = IN6PCBHASH_BIND(table, &zero_mapped, lport);
1258 LIST_FOREACH(inph, head, inph_hash) {
1259 in6p = (struct in6pcb *)inph;
1260 if (in6p->in6p_af != AF_INET6)
1261 continue;
1262
1263 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1264 continue;
1265 if (in6p->in6p_fport != 0)
1266 continue;
1267 if (in6p->in6p_lport != lport)
1268 continue;
1269 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1270 continue;
1271 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zero_mapped))
1272 goto out;
1273 }
1274 }
1275 #endif
1276 head = IN6PCBHASH_BIND(table, &zeroin6_addr, lport);
1277 LIST_FOREACH(inph, head, inph_hash) {
1278 in6p = (struct in6pcb *)inph;
1279 if (in6p->in6p_af != AF_INET6)
1280 continue;
1281
1282 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1283 continue;
1284 if (in6p->in6p_fport != 0)
1285 continue;
1286 if (in6p->in6p_lport != lport)
1287 continue;
1288 if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
1289 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1290 continue;
1291 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zeroin6_addr))
1292 goto out;
1293 }
1294 return (NULL);
1295
1296 out:
1297 inph = &in6p->in6p_head;
1298 if (inph != LIST_FIRST(head)) {
1299 LIST_REMOVE(inph, inph_hash);
1300 LIST_INSERT_HEAD(head, inph, inph_hash);
1301 }
1302 return in6p;
1303 }
1304
1305 void
1306 in6_pcbstate(struct in6pcb *in6p, int state)
1307 {
1308
1309 if (in6p->in6p_af != AF_INET6)
1310 return;
1311
1312 if (in6p->in6p_state > IN6P_ATTACHED)
1313 LIST_REMOVE(&in6p->in6p_head, inph_hash);
1314
1315 switch (state) {
1316 case IN6P_BOUND:
1317 LIST_INSERT_HEAD(IN6PCBHASH_BIND(in6p->in6p_table,
1318 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
1319 inph_hash);
1320 break;
1321 case IN6P_CONNECTED:
1322 LIST_INSERT_HEAD(IN6PCBHASH_CONNECT(in6p->in6p_table,
1323 &in6p->in6p_faddr, in6p->in6p_fport,
1324 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
1325 inph_hash);
1326 break;
1327 }
1328
1329 in6p->in6p_state = state;
1330 }
1331