in6_pcb.c revision 1.138 1 /* $NetBSD: in6_pcb.c,v 1.138 2015/04/27 02:59:44 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.138 2015/04/27 02:59:44 ozaki-r 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 * Connect from a socket to a specified address.
422 * Both address and port must be specified in argument sin6.
423 * If don't have a local address for this socket yet,
424 * then pick one.
425 */
426 int
427 in6_pcbconnect(void *v, struct mbuf *nam, struct lwp *l)
428 {
429 struct in6pcb *in6p = v;
430 struct in6_addr *in6a = NULL;
431 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
432 struct ifnet *ifp = NULL; /* outgoing interface */
433 int error = 0;
434 int scope_ambiguous = 0;
435 #ifdef INET
436 struct in6_addr mapped;
437 #endif
438 struct sockaddr_in6 tmp;
439 struct vestigial_inpcb vestige;
440
441 (void)&in6a; /* XXX fool gcc */
442
443 if (in6p->in6p_af != AF_INET6)
444 return (EINVAL);
445
446 if (nam->m_len != sizeof(*sin6))
447 return (EINVAL);
448 if (sin6->sin6_len != sizeof(*sin6))
449 return (EINVAL);
450 if (sin6->sin6_family != AF_INET6)
451 return (EAFNOSUPPORT);
452 if (sin6->sin6_port == 0)
453 return (EADDRNOTAVAIL);
454
455 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
456 in6p->in6p_socket->so_type == SOCK_STREAM)
457 return EADDRNOTAVAIL;
458
459 if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
460 scope_ambiguous = 1;
461 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
462 return(error);
463
464 /* sanity check for mapped address case */
465 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
466 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
467 return EINVAL;
468 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
469 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
470 if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
471 return EINVAL;
472 } else
473 {
474 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
475 return EINVAL;
476 }
477
478 /* protect *sin6 from overwrites */
479 tmp = *sin6;
480 sin6 = &tmp;
481
482 /* Source address selection. */
483 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
484 in6p->in6p_laddr.s6_addr32[3] == 0) {
485 #ifdef INET
486 struct sockaddr_in sin, *sinp;
487
488 memset(&sin, 0, sizeof(sin));
489 sin.sin_len = sizeof(sin);
490 sin.sin_family = AF_INET;
491 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr32[3],
492 sizeof(sin.sin_addr));
493 sinp = in_selectsrc(&sin, &in6p->in6p_route,
494 in6p->in6p_socket->so_options, NULL, &error);
495 if (sinp == 0) {
496 if (error == 0)
497 error = EADDRNOTAVAIL;
498 return (error);
499 }
500 memset(&mapped, 0, sizeof(mapped));
501 mapped.s6_addr16[5] = htons(0xffff);
502 memcpy(&mapped.s6_addr32[3], &sinp->sin_addr, sizeof(sinp->sin_addr));
503 in6a = &mapped;
504 #else
505 return EADDRNOTAVAIL;
506 #endif
507 } else {
508 /*
509 * XXX: in6_selectsrc might replace the bound local address
510 * with the address specified by setsockopt(IPV6_PKTINFO).
511 * Is it the intended behavior?
512 */
513 in6a = in6_selectsrc(sin6, in6p->in6p_outputopts,
514 in6p->in6p_moptions,
515 &in6p->in6p_route,
516 &in6p->in6p_laddr, &ifp, &error);
517 if (ifp && scope_ambiguous &&
518 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
519 return(error);
520 }
521
522 if (in6a == 0) {
523 if (error == 0)
524 error = EADDRNOTAVAIL;
525 return (error);
526 }
527 }
528
529 if (ifp != NULL)
530 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
531 else
532 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim_rt(in6p);
533
534 if (in6_pcblookup_connect(in6p->in6p_table, &sin6->sin6_addr,
535 sin6->sin6_port,
536 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ? in6a : &in6p->in6p_laddr,
537 in6p->in6p_lport, 0, &vestige)
538 || vestige.valid)
539 return (EADDRINUSE);
540 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ||
541 (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
542 in6p->in6p_laddr.s6_addr32[3] == 0))
543 {
544 if (in6p->in6p_lport == 0) {
545 error = in6_pcbbind(in6p, NULL, l);
546 if (error != 0)
547 return error;
548 }
549 in6p->in6p_laddr = *in6a;
550 }
551 in6p->in6p_faddr = sin6->sin6_addr;
552 in6p->in6p_fport = sin6->sin6_port;
553
554 /* Late bind, if needed */
555 if (in6p->in6p_bindportonsend) {
556 struct sockaddr_in6 lsin = *((const struct sockaddr_in6 *)
557 in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
558 lsin.sin6_addr = in6p->in6p_laddr;
559 lsin.sin6_port = 0;
560
561 if ((error = in6_pcbbind_port(in6p, &lsin, l)) != 0)
562 return error;
563 }
564
565 in6_pcbstate(in6p, IN6P_CONNECTED);
566 in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
567 if (ip6_auto_flowlabel)
568 in6p->in6p_flowinfo |=
569 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
570 #if defined(IPSEC)
571 if (ipsec_enabled && in6p->in6p_socket->so_type == SOCK_STREAM)
572 ipsec_pcbconn(in6p->in6p_sp);
573 #endif
574 return (0);
575 }
576
577 void
578 in6_pcbdisconnect(struct in6pcb *in6p)
579 {
580 memset((void *)&in6p->in6p_faddr, 0, sizeof(in6p->in6p_faddr));
581 in6p->in6p_fport = 0;
582 in6_pcbstate(in6p, IN6P_BOUND);
583 in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
584 #if defined(IPSEC)
585 if (ipsec_enabled)
586 ipsec_pcbdisconn(in6p->in6p_sp);
587 #endif
588 if (in6p->in6p_socket->so_state & SS_NOFDREF)
589 in6_pcbdetach(in6p);
590 }
591
592 void
593 in6_pcbdetach(struct in6pcb *in6p)
594 {
595 struct socket *so = in6p->in6p_socket;
596 int s;
597
598 if (in6p->in6p_af != AF_INET6)
599 return;
600
601 #if defined(IPSEC)
602 if (ipsec_enabled)
603 ipsec6_delete_pcbpolicy(in6p);
604 #endif
605 so->so_pcb = NULL;
606
607 s = splnet();
608 in6_pcbstate(in6p, IN6P_ATTACHED);
609 LIST_REMOVE(&in6p->in6p_head, inph_lhash);
610 TAILQ_REMOVE(&in6p->in6p_table->inpt_queue, &in6p->in6p_head,
611 inph_queue);
612 splx(s);
613
614 if (in6p->in6p_options) {
615 m_freem(in6p->in6p_options);
616 }
617 if (in6p->in6p_outputopts != NULL) {
618 ip6_clearpktopts(in6p->in6p_outputopts, -1);
619 free(in6p->in6p_outputopts, M_IP6OPT);
620 }
621 rtcache_free(&in6p->in6p_route);
622 ip6_freemoptions(in6p->in6p_moptions);
623 ip_freemoptions(in6p->in6p_v4moptions);
624 sofree(so); /* drops the socket's lock */
625
626 pool_put(&in6pcb_pool, in6p);
627 mutex_enter(softnet_lock); /* reacquire it */
628 }
629
630 void
631 in6_setsockaddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6)
632 {
633
634 if (in6p->in6p_af != AF_INET6)
635 return;
636
637 sockaddr_in6_init(sin6, &in6p->in6p_laddr, in6p->in6p_lport, 0, 0);
638 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
639 }
640
641 void
642 in6_setpeeraddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6)
643 {
644
645 if (in6p->in6p_af != AF_INET6)
646 return;
647
648 sockaddr_in6_init(sin6, &in6p->in6p_faddr, in6p->in6p_fport, 0, 0);
649 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
650 }
651
652 /*
653 * Pass some notification to all connections of a protocol
654 * associated with address dst. The local address and/or port numbers
655 * may be specified to limit the search. The "usual action" will be
656 * taken, depending on the ctlinput cmd. The caller must filter any
657 * cmds that are uninteresting (e.g., no error in the map).
658 * Call the protocol specific routine (if any) to report
659 * any errors for each matching socket.
660 *
661 * Must be called at splsoftnet.
662 *
663 * Note: src (4th arg) carries the flowlabel value on the original IPv6
664 * header, in sin6_flowinfo member.
665 */
666 int
667 in6_pcbnotify(struct inpcbtable *table, const struct sockaddr *dst,
668 u_int fport_arg, const struct sockaddr *src, u_int lport_arg, int cmd,
669 void *cmdarg, void (*notify)(struct in6pcb *, int))
670 {
671 struct rtentry *rt;
672 struct inpcb_hdr *inph, *ninph;
673 struct sockaddr_in6 sa6_src;
674 const struct sockaddr_in6 *sa6_dst;
675 u_int16_t fport = fport_arg, lport = lport_arg;
676 int errno;
677 int nmatch = 0;
678 u_int32_t flowinfo;
679
680 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
681 return 0;
682
683 sa6_dst = (const struct sockaddr_in6 *)dst;
684 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
685 return 0;
686
687 /*
688 * note that src can be NULL when we get notify by local fragmentation.
689 */
690 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
691 flowinfo = sa6_src.sin6_flowinfo;
692
693 /*
694 * Redirects go to all references to the destination,
695 * and use in6_rtchange to invalidate the route cache.
696 * Dead host indications: also use in6_rtchange to invalidate
697 * the cache, and deliver the error to all the sockets.
698 * Otherwise, if we have knowledge of the local port and address,
699 * deliver only to that socket.
700 */
701 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
702 fport = 0;
703 lport = 0;
704 memset((void *)&sa6_src.sin6_addr, 0, sizeof(sa6_src.sin6_addr));
705
706 if (cmd != PRC_HOSTDEAD)
707 notify = in6_rtchange;
708 }
709
710 errno = inet6ctlerrmap[cmd];
711 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
712 struct in6pcb *in6p = (struct in6pcb *)inph;
713 if (in6p->in6p_af != AF_INET6)
714 continue;
715
716 /*
717 * Under the following condition, notify of redirects
718 * to the pcb, without making address matches against inpcb.
719 * - redirect notification is arrived.
720 * - the inpcb is unconnected.
721 * - the inpcb is caching !RTF_HOST routing entry.
722 * - the ICMPv6 notification is from the gateway cached in the
723 * inpcb. i.e. ICMPv6 notification is from nexthop gateway
724 * the inpcb used very recently.
725 *
726 * This is to improve interaction between netbsd/openbsd
727 * redirect handling code, and inpcb route cache code.
728 * without the clause, !RTF_HOST routing entry (which carries
729 * gateway used by inpcb right before the ICMPv6 redirect)
730 * will be cached forever in unconnected inpcb.
731 *
732 * There still is a question regarding to what is TRT:
733 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
734 * generated on packet output. inpcb will always cache
735 * RTF_HOST routing entry so there's no need for the clause
736 * (ICMPv6 redirect will update RTF_HOST routing entry,
737 * and inpcb is caching it already).
738 * However, bsdi/freebsd are vulnerable to local DoS attacks
739 * due to the cloned routing entries.
740 * - Specwise, "destination cache" is mentioned in RFC2461.
741 * Jinmei says that it implies bsdi/freebsd behavior, itojun
742 * is not really convinced.
743 * - Having hiwat/lowat on # of cloned host route (redirect/
744 * pmtud) may be a good idea. netbsd/openbsd has it. see
745 * icmp6_mtudisc_update().
746 */
747 if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
748 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
749 (rt = rtcache_validate(&in6p->in6p_route)) != NULL &&
750 !(rt->rt_flags & RTF_HOST)) {
751 const struct sockaddr_in6 *dst6;
752
753 dst6 = (const struct sockaddr_in6 *)
754 rtcache_getdst(&in6p->in6p_route);
755 if (dst6 == NULL)
756 ;
757 else if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
758 &sa6_dst->sin6_addr))
759 goto do_notify;
760 }
761
762 /*
763 * If the error designates a new path MTU for a destination
764 * and the application (associated with this socket) wanted to
765 * know the value, notify. Note that we notify for all
766 * disconnected sockets if the corresponding application
767 * wanted. This is because some UDP applications keep sending
768 * sockets disconnected.
769 * XXX: should we avoid to notify the value to TCP sockets?
770 */
771 if (cmd == PRC_MSGSIZE && (in6p->in6p_flags & IN6P_MTU) != 0 &&
772 (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) ||
773 IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &sa6_dst->sin6_addr))) {
774 ip6_notify_pmtu(in6p, (const struct sockaddr_in6 *)dst,
775 (u_int32_t *)cmdarg);
776 }
777
778 /*
779 * Detect if we should notify the error. If no source and
780 * destination ports are specified, but non-zero flowinfo and
781 * local address match, notify the error. This is the case
782 * when the error is delivered with an encrypted buffer
783 * by ESP. Otherwise, just compare addresses and ports
784 * as usual.
785 */
786 if (lport == 0 && fport == 0 && flowinfo &&
787 in6p->in6p_socket != NULL &&
788 flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
789 IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr))
790 goto do_notify;
791 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
792 &sa6_dst->sin6_addr) ||
793 in6p->in6p_socket == 0 ||
794 (lport && in6p->in6p_lport != lport) ||
795 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
796 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
797 &sa6_src.sin6_addr)) ||
798 (fport && in6p->in6p_fport != fport))
799 continue;
800
801 do_notify:
802 if (notify)
803 (*notify)(in6p, errno);
804 nmatch++;
805 }
806 return nmatch;
807 }
808
809 void
810 in6_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp)
811 {
812 struct inpcb_hdr *inph, *ninph;
813 struct ip6_moptions *im6o;
814 struct in6_multi_mship *imm, *nimm;
815
816 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
817 struct in6pcb *in6p = (struct in6pcb *)inph;
818 if (in6p->in6p_af != AF_INET6)
819 continue;
820
821 im6o = in6p->in6p_moptions;
822 if (im6o) {
823 /*
824 * Unselect the outgoing interface if it is being
825 * detached.
826 */
827 if (im6o->im6o_multicast_ifp == ifp)
828 im6o->im6o_multicast_ifp = NULL;
829
830 /*
831 * Drop multicast group membership if we joined
832 * through the interface being detached.
833 * XXX controversial - is it really legal for kernel
834 * to force this?
835 */
836 for (imm = im6o->im6o_memberships.lh_first;
837 imm != NULL; imm = nimm) {
838 nimm = imm->i6mm_chain.le_next;
839 if (imm->i6mm_maddr->in6m_ifp == ifp) {
840 LIST_REMOVE(imm, i6mm_chain);
841 in6_leavegroup(imm);
842 }
843 }
844 }
845 in_purgeifmcast(in6p->in6p_v4moptions, ifp);
846 }
847 }
848
849 void
850 in6_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp)
851 {
852 struct rtentry *rt;
853 struct inpcb_hdr *inph, *ninph;
854
855 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
856 struct in6pcb *in6p = (struct in6pcb *)inph;
857 if (in6p->in6p_af != AF_INET6)
858 continue;
859 if ((rt = rtcache_validate(&in6p->in6p_route)) != NULL &&
860 rt->rt_ifp == ifp)
861 in6_rtchange(in6p, 0);
862 }
863 }
864
865 /*
866 * Check for alternatives when higher level complains
867 * about service problems. For now, invalidate cached
868 * routing information. If the route was created dynamically
869 * (by a redirect), time to try a default gateway again.
870 */
871 void
872 in6_losing(struct in6pcb *in6p)
873 {
874 struct rtentry *rt;
875 struct rt_addrinfo info;
876
877 if (in6p->in6p_af != AF_INET6)
878 return;
879
880 if ((rt = rtcache_validate(&in6p->in6p_route)) == NULL)
881 return;
882
883 memset(&info, 0, sizeof(info));
884 info.rti_info[RTAX_DST] = rtcache_getdst(&in6p->in6p_route);
885 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
886 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
887 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
888 if (rt->rt_flags & RTF_DYNAMIC) {
889 (void)rtrequest(RTM_DELETE, rt_getkey(rt),
890 rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
891 }
892 /*
893 * A new route can be allocated
894 * the next time output is attempted.
895 */
896 rtcache_free(&in6p->in6p_route);
897 }
898
899 /*
900 * After a routing change, flush old routing. A new route can be
901 * allocated the next time output is attempted.
902 */
903 void
904 in6_rtchange(struct in6pcb *in6p, int errno)
905 {
906 if (in6p->in6p_af != AF_INET6)
907 return;
908
909 rtcache_free(&in6p->in6p_route);
910 /*
911 * A new route can be allocated the next time
912 * output is attempted.
913 */
914 }
915
916 struct in6pcb *
917 in6_pcblookup_port(struct inpcbtable *table, struct in6_addr *laddr6,
918 u_int lport_arg, int lookup_wildcard, struct vestigial_inpcb *vp)
919 {
920 struct inpcbhead *head;
921 struct inpcb_hdr *inph;
922 struct in6pcb *in6p, *match = 0;
923 int matchwild = 3, wildcard;
924 u_int16_t lport = lport_arg;
925
926 if (vp)
927 vp->valid = 0;
928
929 head = IN6PCBHASH_PORT(table, lport);
930 LIST_FOREACH(inph, head, inph_lhash) {
931 in6p = (struct in6pcb *)inph;
932 if (in6p->in6p_af != AF_INET6)
933 continue;
934
935 if (in6p->in6p_lport != lport)
936 continue;
937 wildcard = 0;
938 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
939 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
940 continue;
941 }
942 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
943 wildcard++;
944 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
945 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
946 continue;
947 if (!IN6_IS_ADDR_V4MAPPED(laddr6))
948 continue;
949
950 /* duplicate of IPv4 logic */
951 wildcard = 0;
952 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) &&
953 in6p->in6p_faddr.s6_addr32[3])
954 wildcard++;
955 if (!in6p->in6p_laddr.s6_addr32[3]) {
956 if (laddr6->s6_addr32[3])
957 wildcard++;
958 } else {
959 if (!laddr6->s6_addr32[3])
960 wildcard++;
961 else {
962 if (in6p->in6p_laddr.s6_addr32[3] !=
963 laddr6->s6_addr32[3])
964 continue;
965 }
966 }
967 } else if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
968 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
969 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
970 continue;
971 }
972 if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
973 wildcard++;
974 } else {
975 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
976 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
977 continue;
978 }
979 if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
980 wildcard++;
981 else {
982 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
983 laddr6))
984 continue;
985 }
986 }
987 if (wildcard && !lookup_wildcard)
988 continue;
989 if (wildcard < matchwild) {
990 match = in6p;
991 matchwild = wildcard;
992 if (matchwild == 0)
993 break;
994 }
995 }
996 if (match && matchwild == 0)
997 return match;
998
999 if (vp && table->vestige && table->vestige->init_ports6) {
1000 struct vestigial_inpcb better;
1001 void *state;
1002
1003 state = (*table->vestige->init_ports6)(laddr6,
1004 lport_arg,
1005 lookup_wildcard);
1006 while (table->vestige
1007 && (*table->vestige->next_port6)(state, vp)) {
1008
1009 if (vp->lport != lport)
1010 continue;
1011 wildcard = 0;
1012 if (!IN6_IS_ADDR_UNSPECIFIED(&vp->faddr.v6))
1013 wildcard++;
1014 if (IN6_IS_ADDR_UNSPECIFIED(&vp->laddr.v6)) {
1015 if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
1016 wildcard++;
1017 } else {
1018 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1019 if (vp->v6only)
1020 continue;
1021 }
1022 if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
1023 wildcard++;
1024 else {
1025 if (!IN6_ARE_ADDR_EQUAL(&vp->laddr.v6, laddr6))
1026 continue;
1027 }
1028 }
1029 if (wildcard && !lookup_wildcard)
1030 continue;
1031 if (wildcard < matchwild) {
1032 better = *vp;
1033 match = (void*)&better;
1034
1035 matchwild = wildcard;
1036 if (matchwild == 0)
1037 break;
1038 }
1039 }
1040
1041 if (match) {
1042 if (match != (void*)&better)
1043 return match;
1044 else {
1045 *vp = better;
1046 return 0;
1047 }
1048 }
1049 }
1050 return (match);
1051 }
1052
1053 /*
1054 * WARNING: return value (rtentry) could be IPv4 one if in6pcb is connected to
1055 * IPv4 mapped address.
1056 */
1057 struct rtentry *
1058 in6_pcbrtentry(struct in6pcb *in6p)
1059 {
1060 struct rtentry *rt;
1061 struct route *ro;
1062 union {
1063 const struct sockaddr *sa;
1064 const struct sockaddr_in6 *sa6;
1065 #ifdef INET
1066 const struct sockaddr_in *sa4;
1067 #endif
1068 } cdst;
1069
1070 ro = &in6p->in6p_route;
1071
1072 if (in6p->in6p_af != AF_INET6)
1073 return (NULL);
1074
1075 cdst.sa = rtcache_getdst(ro);
1076 if (cdst.sa == NULL)
1077 ;
1078 #ifdef INET
1079 else if (cdst.sa->sa_family == AF_INET) {
1080 KASSERT(IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr));
1081 if (cdst.sa4->sin_addr.s_addr != in6p->in6p_faddr.s6_addr32[3])
1082 rtcache_free(ro);
1083 }
1084 #endif
1085 else {
1086 if (!IN6_ARE_ADDR_EQUAL(&cdst.sa6->sin6_addr,
1087 &in6p->in6p_faddr))
1088 rtcache_free(ro);
1089 }
1090 if ((rt = rtcache_validate(ro)) == NULL)
1091 rt = rtcache_update(ro, 1);
1092 #ifdef INET
1093 if (rt == NULL && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
1094 union {
1095 struct sockaddr dst;
1096 struct sockaddr_in dst4;
1097 } u;
1098 struct in_addr addr;
1099
1100 addr.s_addr = in6p->in6p_faddr.s6_addr32[3];
1101
1102 sockaddr_in_init(&u.dst4, &addr, 0);
1103 rtcache_setdst(ro, &u.dst);
1104
1105 rt = rtcache_init(ro);
1106 } else
1107 #endif
1108 if (rt == NULL && !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
1109 union {
1110 struct sockaddr dst;
1111 struct sockaddr_in6 dst6;
1112 } u;
1113
1114 sockaddr_in6_init(&u.dst6, &in6p->in6p_faddr, 0, 0, 0);
1115 rtcache_setdst(ro, &u.dst);
1116
1117 rt = rtcache_init(ro);
1118 }
1119 return rt;
1120 }
1121
1122 struct in6pcb *
1123 in6_pcblookup_connect(struct inpcbtable *table, const struct in6_addr *faddr6,
1124 u_int fport_arg, const struct in6_addr *laddr6, u_int lport_arg,
1125 int faith,
1126 struct vestigial_inpcb *vp)
1127 {
1128 struct inpcbhead *head;
1129 struct inpcb_hdr *inph;
1130 struct in6pcb *in6p;
1131 u_int16_t fport = fport_arg, lport = lport_arg;
1132
1133 if (vp)
1134 vp->valid = 0;
1135
1136 head = IN6PCBHASH_CONNECT(table, faddr6, fport, laddr6, lport);
1137 LIST_FOREACH(inph, head, inph_hash) {
1138 in6p = (struct in6pcb *)inph;
1139 if (in6p->in6p_af != AF_INET6)
1140 continue;
1141
1142 /* find exact match on both source and dest */
1143 if (in6p->in6p_fport != fport)
1144 continue;
1145 if (in6p->in6p_lport != lport)
1146 continue;
1147 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
1148 continue;
1149 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
1150 continue;
1151 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1152 continue;
1153 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
1154 continue;
1155 if ((IN6_IS_ADDR_V4MAPPED(laddr6) ||
1156 IN6_IS_ADDR_V4MAPPED(faddr6)) &&
1157 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1158 continue;
1159 return in6p;
1160 }
1161 if (vp && table->vestige) {
1162 if ((*table->vestige->lookup6)(faddr6, fport_arg,
1163 laddr6, lport_arg, vp))
1164 return 0;
1165 }
1166
1167 return NULL;
1168 }
1169
1170 struct in6pcb *
1171 in6_pcblookup_bind(struct inpcbtable *table, const struct in6_addr *laddr6,
1172 u_int lport_arg, int faith)
1173 {
1174 struct inpcbhead *head;
1175 struct inpcb_hdr *inph;
1176 struct in6pcb *in6p;
1177 u_int16_t lport = lport_arg;
1178 #ifdef INET
1179 struct in6_addr zero_mapped;
1180 #endif
1181
1182 head = IN6PCBHASH_BIND(table, laddr6, lport);
1183 LIST_FOREACH(inph, head, inph_hash) {
1184 in6p = (struct in6pcb *)inph;
1185 if (in6p->in6p_af != AF_INET6)
1186 continue;
1187
1188 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1189 continue;
1190 if (in6p->in6p_fport != 0)
1191 continue;
1192 if (in6p->in6p_lport != lport)
1193 continue;
1194 if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
1195 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1196 continue;
1197 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
1198 goto out;
1199 }
1200 #ifdef INET
1201 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1202 memset(&zero_mapped, 0, sizeof(zero_mapped));
1203 zero_mapped.s6_addr16[5] = 0xffff;
1204 head = IN6PCBHASH_BIND(table, &zero_mapped, lport);
1205 LIST_FOREACH(inph, head, inph_hash) {
1206 in6p = (struct in6pcb *)inph;
1207 if (in6p->in6p_af != AF_INET6)
1208 continue;
1209
1210 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1211 continue;
1212 if (in6p->in6p_fport != 0)
1213 continue;
1214 if (in6p->in6p_lport != lport)
1215 continue;
1216 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1217 continue;
1218 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zero_mapped))
1219 goto out;
1220 }
1221 }
1222 #endif
1223 head = IN6PCBHASH_BIND(table, &zeroin6_addr, lport);
1224 LIST_FOREACH(inph, head, inph_hash) {
1225 in6p = (struct in6pcb *)inph;
1226 if (in6p->in6p_af != AF_INET6)
1227 continue;
1228
1229 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1230 continue;
1231 if (in6p->in6p_fport != 0)
1232 continue;
1233 if (in6p->in6p_lport != lport)
1234 continue;
1235 if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
1236 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1237 continue;
1238 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zeroin6_addr))
1239 goto out;
1240 }
1241 return (NULL);
1242
1243 out:
1244 inph = &in6p->in6p_head;
1245 if (inph != LIST_FIRST(head)) {
1246 LIST_REMOVE(inph, inph_hash);
1247 LIST_INSERT_HEAD(head, inph, inph_hash);
1248 }
1249 return in6p;
1250 }
1251
1252 void
1253 in6_pcbstate(struct in6pcb *in6p, int state)
1254 {
1255
1256 if (in6p->in6p_af != AF_INET6)
1257 return;
1258
1259 if (in6p->in6p_state > IN6P_ATTACHED)
1260 LIST_REMOVE(&in6p->in6p_head, inph_hash);
1261
1262 switch (state) {
1263 case IN6P_BOUND:
1264 LIST_INSERT_HEAD(IN6PCBHASH_BIND(in6p->in6p_table,
1265 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
1266 inph_hash);
1267 break;
1268 case IN6P_CONNECTED:
1269 LIST_INSERT_HEAD(IN6PCBHASH_CONNECT(in6p->in6p_table,
1270 &in6p->in6p_faddr, in6p->in6p_fport,
1271 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
1272 inph_hash);
1273 break;
1274 }
1275
1276 in6p->in6p_state = state;
1277 }
1278