1 /* $NetBSD: in6_pcb.c,v 1.68 2005/11/15 18:39:46 dsl 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.68 2005/11/15 18:39:46 dsl 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, p) 348 void *v; 349 struct mbuf *nam; 350 struct proc *p; 351 { 352 struct in6pcb *in6p = v; 353 struct in6_addr *in6a = NULL; 354 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *); 355 struct ifnet *ifp = NULL; /* outgoing interface */ 356 int error = 0; 357 #ifdef INET 358 struct in6_addr mapped; 359 #endif 360 struct sockaddr_in6 tmp; 361 362 (void)&in6a; /* XXX fool gcc */ 363 364 if (in6p->in6p_af != AF_INET6) 365 return (EINVAL); 366 367 if (nam->m_len != sizeof(*sin6)) 368 return (EINVAL); 369 if (sin6->sin6_family != AF_INET6) 370 return (EAFNOSUPPORT); 371 if (sin6->sin6_port == 0) 372 return (EADDRNOTAVAIL); 373 374 /* sanity check for mapped address case */ 375 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 376 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 377 return EINVAL; 378 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 379 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff); 380 if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) 381 return EINVAL; 382 } else 383 { 384 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) 385 return EINVAL; 386 } 387 388 /* protect *sin6 from overwrites */ 389 tmp = *sin6; 390 sin6 = &tmp; 391 392 /* KAME hack: embed scopeid */ 393 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, &ifp) != 0) 394 return EINVAL; 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 /* 424 * XXX: in6_selectsrc might replace the bound local address 425 * with the address specified by setsockopt(IPV6_PKTINFO). 426 * Is it the intended behavior? 427 */ 428 in6a = in6_selectsrc(sin6, in6p->in6p_outputopts, 429 in6p->in6p_moptions, 430 &in6p->in6p_route, 431 &in6p->in6p_laddr, &error); 432 if (in6a == 0) { 433 if (error == 0) 434 error = EADDRNOTAVAIL; 435 return (error); 436 } 437 } 438 if (in6p->in6p_route.ro_rt) 439 ifp = in6p->in6p_route.ro_rt->rt_ifp; 440 441 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp); 442 443 if (in6_pcblookup_connect(in6p->in6p_table, &sin6->sin6_addr, 444 sin6->sin6_port, 445 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ? in6a : &in6p->in6p_laddr, 446 in6p->in6p_lport, 0)) 447 return (EADDRINUSE); 448 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) || 449 (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) && 450 in6p->in6p_laddr.s6_addr32[3] == 0)) 451 { 452 if (in6p->in6p_lport == 0) { 453 error = in6_pcbbind(in6p, (struct mbuf *)0, p); 454 if (error != 0) 455 return error; 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 so->so_pcb = 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; 583 const struct sockaddr *src; 584 u_int fport_arg, lport_arg; 585 int cmd; 586 void *cmdarg; 587 void (*notify) __P((struct in6pcb *, int)); 588 { 589 struct in6pcb *in6p, *nin6p; 590 struct sockaddr_in6 sa6_src, *sa6_dst; 591 u_int16_t fport = fport_arg, lport = lport_arg; 592 int errno; 593 int nmatch = 0; 594 u_int32_t flowinfo; 595 596 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6) 597 return 0; 598 599 sa6_dst = (struct sockaddr_in6 *)dst; 600 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 601 return 0; 602 603 /* 604 * note that src can be NULL when we get notify by local fragmentation. 605 */ 606 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src; 607 flowinfo = sa6_src.sin6_flowinfo; 608 609 /* 610 * Redirects go to all references to the destination, 611 * and use in6_rtchange to invalidate the route cache. 612 * Dead host indications: also use in6_rtchange to invalidate 613 * the cache, and deliver the error to all the sockets. 614 * Otherwise, if we have knowledge of the local port and address, 615 * deliver only to that socket. 616 */ 617 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 618 fport = 0; 619 lport = 0; 620 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 621 622 if (cmd != PRC_HOSTDEAD) 623 notify = in6_rtchange; 624 } 625 626 errno = inet6ctlerrmap[cmd]; 627 for (in6p = (struct in6pcb *)CIRCLEQ_FIRST(&table->inpt_queue); 628 in6p != (void *)&table->inpt_queue; 629 in6p = nin6p) { 630 nin6p = (struct in6pcb *)CIRCLEQ_NEXT(in6p, in6p_queue); 631 632 if (in6p->in6p_af != AF_INET6) 633 continue; 634 635 /* 636 * Under the following condition, notify of redirects 637 * to the pcb, without making address matches against inpcb. 638 * - redirect notification is arrived. 639 * - the inpcb is unconnected. 640 * - the inpcb is caching !RTF_HOST routing entry. 641 * - the ICMPv6 notification is from the gateway cached in the 642 * inpcb. i.e. ICMPv6 notification is from nexthop gateway 643 * the inpcb used very recently. 644 * 645 * This is to improve interaction between netbsd/openbsd 646 * redirect handling code, and inpcb route cache code. 647 * without the clause, !RTF_HOST routing entry (which carries 648 * gateway used by inpcb right before the ICMPv6 redirect) 649 * will be cached forever in unconnected inpcb. 650 * 651 * There still is a question regarding to what is TRT: 652 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be 653 * generated on packet output. inpcb will always cache 654 * RTF_HOST routing entry so there's no need for the clause 655 * (ICMPv6 redirect will update RTF_HOST routing entry, 656 * and inpcb is caching it already). 657 * However, bsdi/freebsd are vulnerable to local DoS attacks 658 * due to the cloned routing entries. 659 * - Specwise, "destination cache" is mentioned in RFC2461. 660 * Jinmei says that it implies bsdi/freebsd behavior, itojun 661 * is not really convinced. 662 * - Having hiwat/lowat on # of cloned host route (redirect/ 663 * pmtud) may be a good idea. netbsd/openbsd has it. see 664 * icmp6_mtudisc_update(). 665 */ 666 if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) && 667 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 668 in6p->in6p_route.ro_rt && 669 !(in6p->in6p_route.ro_rt->rt_flags & RTF_HOST)) { 670 struct sockaddr_in6 *dst6; 671 672 dst6 = (struct sockaddr_in6 *)&in6p->in6p_route.ro_dst; 673 if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, 674 &sa6_dst->sin6_addr)) 675 goto do_notify; 676 } 677 678 /* 679 * Detect if we should notify the error. If no source and 680 * destination ports are specified, but non-zero flowinfo and 681 * local address match, notify the error. This is the case 682 * when the error is delivered with an encrypted buffer 683 * by ESP. Otherwise, just compare addresses and ports 684 * as usual. 685 */ 686 if (lport == 0 && fport == 0 && flowinfo && 687 in6p->in6p_socket != NULL && 688 flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && 689 IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr)) 690 goto do_notify; 691 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, 692 &sa6_dst->sin6_addr) || 693 in6p->in6p_socket == 0 || 694 (lport && in6p->in6p_lport != lport) || 695 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 696 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, 697 &sa6_src.sin6_addr)) || 698 (fport && in6p->in6p_fport != fport)) 699 continue; 700 701 do_notify: 702 if (notify) 703 (*notify)(in6p, errno); 704 nmatch++; 705 } 706 return nmatch; 707 } 708 709 void 710 in6_pcbpurgeif0(table, ifp) 711 struct inpcbtable *table; 712 struct ifnet *ifp; 713 { 714 struct in6pcb *in6p, *nin6p; 715 struct ip6_moptions *im6o; 716 struct in6_multi_mship *imm, *nimm; 717 718 for (in6p = (struct in6pcb *)CIRCLEQ_FIRST(&table->inpt_queue); 719 in6p != (void *)&table->inpt_queue; 720 in6p = nin6p) { 721 nin6p = (struct in6pcb *)CIRCLEQ_NEXT(in6p, in6p_queue); 722 if (in6p->in6p_af != AF_INET6) 723 continue; 724 725 im6o = in6p->in6p_moptions; 726 if (im6o) { 727 /* 728 * Unselect the outgoing interface if it is being 729 * detached. 730 */ 731 if (im6o->im6o_multicast_ifp == ifp) 732 im6o->im6o_multicast_ifp = NULL; 733 734 /* 735 * Drop multicast group membership if we joined 736 * through the interface being detached. 737 * XXX controversial - is it really legal for kernel 738 * to force this? 739 */ 740 for (imm = im6o->im6o_memberships.lh_first; 741 imm != NULL; imm = nimm) { 742 nimm = imm->i6mm_chain.le_next; 743 if (imm->i6mm_maddr->in6m_ifp == ifp) { 744 LIST_REMOVE(imm, i6mm_chain); 745 in6_leavegroup(imm); 746 } 747 } 748 } 749 } 750 } 751 752 void 753 in6_pcbpurgeif(table, ifp) 754 struct inpcbtable *table; 755 struct ifnet *ifp; 756 { 757 struct in6pcb *in6p, *nin6p; 758 759 for (in6p = (struct in6pcb *)CIRCLEQ_FIRST(&table->inpt_queue); 760 in6p != (void *)&table->inpt_queue; 761 in6p = nin6p) { 762 nin6p = (struct in6pcb *)CIRCLEQ_NEXT(in6p, in6p_queue); 763 if (in6p->in6p_af != AF_INET6) 764 continue; 765 if (in6p->in6p_route.ro_rt != NULL && 766 in6p->in6p_route.ro_rt->rt_ifp == ifp) 767 in6_rtchange(in6p, 0); 768 } 769 } 770 771 /* 772 * Check for alternatives when higher level complains 773 * about service problems. For now, invalidate cached 774 * routing information. If the route was created dynamically 775 * (by a redirect), time to try a default gateway again. 776 */ 777 void 778 in6_losing(in6p) 779 struct in6pcb *in6p; 780 { 781 struct rtentry *rt; 782 struct rt_addrinfo info; 783 784 if (in6p->in6p_af != AF_INET6) 785 return; 786 787 if ((rt = in6p->in6p_route.ro_rt) != NULL) { 788 in6p->in6p_route.ro_rt = 0; 789 bzero((caddr_t)&info, sizeof(info)); 790 info.rti_info[RTAX_DST] = 791 (struct sockaddr *)&in6p->in6p_route.ro_dst; 792 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 793 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 794 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 795 if (rt->rt_flags & RTF_DYNAMIC) { 796 (void)rtrequest(RTM_DELETE, rt_key(rt), 797 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 798 (struct rtentry **)0); 799 } else { 800 /* 801 * A new route can be allocated 802 * the next time output is attempted. 803 */ 804 rtfree(rt); 805 } 806 } 807 } 808 809 /* 810 * After a routing change, flush old routing 811 * and allocate a (hopefully) better one. 812 */ 813 void 814 in6_rtchange(in6p, errno) 815 struct in6pcb *in6p; 816 int errno; 817 { 818 if (in6p->in6p_af != AF_INET6) 819 return; 820 821 if (in6p->in6p_route.ro_rt) { 822 rtfree(in6p->in6p_route.ro_rt); 823 in6p->in6p_route.ro_rt = 0; 824 /* 825 * A new route can be allocated the next time 826 * output is attempted. 827 */ 828 } 829 } 830 831 struct in6pcb * 832 in6_pcblookup_port(table, laddr6, lport_arg, lookup_wildcard) 833 struct inpcbtable *table; 834 struct in6_addr *laddr6; 835 u_int lport_arg; 836 int lookup_wildcard; 837 { 838 struct inpcbhead *head; 839 struct inpcb_hdr *inph; 840 struct in6pcb *in6p, *match = 0; 841 int matchwild = 3, wildcard; 842 u_int16_t lport = lport_arg; 843 844 head = IN6PCBHASH_PORT(table, lport); 845 LIST_FOREACH(inph, head, inph_lhash) { 846 in6p = (struct in6pcb *)inph; 847 if (in6p->in6p_af != AF_INET6) 848 continue; 849 850 if (in6p->in6p_lport != lport) 851 continue; 852 wildcard = 0; 853 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) { 854 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 855 continue; 856 } 857 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) 858 wildcard++; 859 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) { 860 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 861 continue; 862 if (!IN6_IS_ADDR_V4MAPPED(laddr6)) 863 continue; 864 865 /* duplicate of IPv4 logic */ 866 wildcard = 0; 867 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) && 868 in6p->in6p_faddr.s6_addr32[3]) 869 wildcard++; 870 if (!in6p->in6p_laddr.s6_addr32[3]) { 871 if (laddr6->s6_addr32[3]) 872 wildcard++; 873 } else { 874 if (!laddr6->s6_addr32[3]) 875 wildcard++; 876 else { 877 if (in6p->in6p_laddr.s6_addr32[3] != 878 laddr6->s6_addr32[3]) 879 continue; 880 } 881 } 882 } else if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 883 if (IN6_IS_ADDR_V4MAPPED(laddr6)) { 884 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 885 continue; 886 } 887 if (!IN6_IS_ADDR_UNSPECIFIED(laddr6)) 888 wildcard++; 889 } else { 890 if (IN6_IS_ADDR_V4MAPPED(laddr6)) { 891 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 892 continue; 893 } 894 if (IN6_IS_ADDR_UNSPECIFIED(laddr6)) 895 wildcard++; 896 else { 897 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, 898 laddr6)) 899 continue; 900 } 901 } 902 if (wildcard && !lookup_wildcard) 903 continue; 904 if (wildcard < matchwild) { 905 match = in6p; 906 matchwild = wildcard; 907 if (matchwild == 0) 908 break; 909 } 910 } 911 return (match); 912 } 913 #undef continue 914 915 /* 916 * WARNING: return value (rtentry) could be IPv4 one if in6pcb is connected to 917 * IPv4 mapped address. 918 */ 919 struct rtentry * 920 in6_pcbrtentry(in6p) 921 struct in6pcb *in6p; 922 { 923 struct route_in6 *ro; 924 struct sockaddr_in6 *dst6; 925 926 ro = &in6p->in6p_route; 927 dst6 = (struct sockaddr_in6 *)&ro->ro_dst; 928 929 if (in6p->in6p_af != AF_INET6) 930 return (NULL); 931 932 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 933 !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &in6p->in6p_faddr))) { 934 RTFREE(ro->ro_rt); 935 ro->ro_rt = (struct rtentry *)NULL; 936 } 937 #ifdef INET 938 if (ro->ro_rt == (struct rtentry *)NULL && 939 IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) { 940 struct sockaddr_in *dst = (struct sockaddr_in *)&ro->ro_dst; 941 942 bzero(dst, sizeof(*dst)); 943 dst->sin_family = AF_INET; 944 dst->sin_len = sizeof(struct sockaddr_in); 945 bcopy(&in6p->in6p_faddr.s6_addr32[3], &dst->sin_addr, 946 sizeof(dst->sin_addr)); 947 rtalloc((struct route *)ro); 948 } else 949 #endif 950 if (ro->ro_rt == (struct rtentry *)NULL && 951 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 952 bzero(dst6, sizeof(*dst6)); 953 dst6->sin6_family = AF_INET6; 954 dst6->sin6_len = sizeof(struct sockaddr_in6); 955 dst6->sin6_addr = in6p->in6p_faddr; 956 rtalloc((struct route *)ro); 957 } 958 return (ro->ro_rt); 959 } 960 961 struct in6pcb * 962 in6_pcblookup_connect(table, faddr6, fport_arg, laddr6, lport_arg, faith) 963 struct inpcbtable *table; 964 struct in6_addr *faddr6; 965 const struct in6_addr *laddr6; 966 u_int fport_arg, lport_arg; 967 int faith; 968 { 969 struct inpcbhead *head; 970 struct inpcb_hdr *inph; 971 struct in6pcb *in6p; 972 u_int16_t fport = fport_arg, lport = lport_arg; 973 974 head = IN6PCBHASH_CONNECT(table, faddr6, fport, laddr6, lport); 975 LIST_FOREACH(inph, head, inph_hash) { 976 in6p = (struct in6pcb *)inph; 977 if (in6p->in6p_af != AF_INET6) 978 continue; 979 980 /* find exact match on both source and dest */ 981 if (in6p->in6p_fport != fport) 982 continue; 983 if (in6p->in6p_lport != lport) 984 continue; 985 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) 986 continue; 987 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)) 988 continue; 989 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 990 continue; 991 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) 992 continue; 993 if ((IN6_IS_ADDR_V4MAPPED(laddr6) || 994 IN6_IS_ADDR_V4MAPPED(faddr6)) && 995 (in6p->in6p_flags & IN6P_IPV6_V6ONLY)) 996 continue; 997 return in6p; 998 } 999 return NULL; 1000 } 1001 1002 struct in6pcb * 1003 in6_pcblookup_bind(table, laddr6, lport_arg, faith) 1004 struct inpcbtable *table; 1005 struct in6_addr *laddr6; 1006 u_int lport_arg; 1007 int faith; 1008 { 1009 struct inpcbhead *head; 1010 struct inpcb_hdr *inph; 1011 struct in6pcb *in6p; 1012 u_int16_t lport = lport_arg; 1013 #ifdef INET 1014 struct in6_addr zero_mapped; 1015 #endif 1016 1017 head = IN6PCBHASH_BIND(table, laddr6, lport); 1018 LIST_FOREACH(inph, head, inph_hash) { 1019 in6p = (struct in6pcb *)inph; 1020 if (in6p->in6p_af != AF_INET6) 1021 continue; 1022 1023 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) 1024 continue; 1025 if (in6p->in6p_fport != 0) 1026 continue; 1027 if (in6p->in6p_lport != lport) 1028 continue; 1029 if (IN6_IS_ADDR_V4MAPPED(laddr6) && 1030 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 1031 continue; 1032 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) 1033 goto out; 1034 } 1035 #ifdef INET 1036 if (IN6_IS_ADDR_V4MAPPED(laddr6)) { 1037 memset(&zero_mapped, 0, sizeof(zero_mapped)); 1038 zero_mapped.s6_addr16[5] = 0xffff; 1039 head = IN6PCBHASH_BIND(table, &zero_mapped, lport); 1040 LIST_FOREACH(inph, head, inph_hash) { 1041 in6p = (struct in6pcb *)inph; 1042 if (in6p->in6p_af != AF_INET6) 1043 continue; 1044 1045 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) 1046 continue; 1047 if (in6p->in6p_fport != 0) 1048 continue; 1049 if (in6p->in6p_lport != lport) 1050 continue; 1051 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 1052 continue; 1053 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zero_mapped)) 1054 goto out; 1055 } 1056 } 1057 #endif 1058 head = IN6PCBHASH_BIND(table, &zeroin6_addr, lport); 1059 LIST_FOREACH(inph, head, inph_hash) { 1060 in6p = (struct in6pcb *)inph; 1061 if (in6p->in6p_af != AF_INET6) 1062 continue; 1063 1064 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) 1065 continue; 1066 if (in6p->in6p_fport != 0) 1067 continue; 1068 if (in6p->in6p_lport != lport) 1069 continue; 1070 if (IN6_IS_ADDR_V4MAPPED(laddr6) && 1071 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 1072 continue; 1073 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zeroin6_addr)) 1074 goto out; 1075 } 1076 return (NULL); 1077 1078 out: 1079 inph = &in6p->in6p_head; 1080 if (inph != LIST_FIRST(head)) { 1081 LIST_REMOVE(inph, inph_hash); 1082 LIST_INSERT_HEAD(head, inph, inph_hash); 1083 } 1084 return in6p; 1085 } 1086 1087 void 1088 in6_pcbstate(in6p, state) 1089 struct in6pcb *in6p; 1090 int state; 1091 { 1092 1093 if (in6p->in6p_af != AF_INET6) 1094 return; 1095 1096 if (in6p->in6p_state > IN6P_ATTACHED) 1097 LIST_REMOVE(&in6p->in6p_head, inph_hash); 1098 1099 switch (state) { 1100 case IN6P_BOUND: 1101 LIST_INSERT_HEAD(IN6PCBHASH_BIND(in6p->in6p_table, 1102 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head, 1103 inph_hash); 1104 break; 1105 case IN6P_CONNECTED: 1106 LIST_INSERT_HEAD(IN6PCBHASH_CONNECT(in6p->in6p_table, 1107 &in6p->in6p_faddr, in6p->in6p_fport, 1108 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head, 1109 inph_hash); 1110 break; 1111 } 1112 1113 in6p->in6p_state = state; 1114 } 1115