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