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