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