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