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