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