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