1 /* $NetBSD: in6_pcb.c,v 1.69 2006/01/21 00:15:36 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.69 2006/01/21 00:15:36 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 * Detect if we should notify the error. If no source and 684 * destination ports are specified, but non-zero flowinfo and 685 * local address match, notify the error. This is the case 686 * when the error is delivered with an encrypted buffer 687 * by ESP. Otherwise, just compare addresses and ports 688 * as usual. 689 */ 690 if (lport == 0 && fport == 0 && flowinfo && 691 in6p->in6p_socket != NULL && 692 flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && 693 IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr)) 694 goto do_notify; 695 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, 696 &sa6_dst->sin6_addr) || 697 in6p->in6p_socket == 0 || 698 (lport && in6p->in6p_lport != lport) || 699 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 700 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, 701 &sa6_src.sin6_addr)) || 702 (fport && in6p->in6p_fport != fport)) 703 continue; 704 705 do_notify: 706 if (notify) 707 (*notify)(in6p, errno); 708 nmatch++; 709 } 710 return nmatch; 711 } 712 713 void 714 in6_pcbpurgeif0(table, ifp) 715 struct inpcbtable *table; 716 struct ifnet *ifp; 717 { 718 struct in6pcb *in6p, *nin6p; 719 struct ip6_moptions *im6o; 720 struct in6_multi_mship *imm, *nimm; 721 722 for (in6p = (struct in6pcb *)CIRCLEQ_FIRST(&table->inpt_queue); 723 in6p != (void *)&table->inpt_queue; 724 in6p = nin6p) { 725 nin6p = (struct in6pcb *)CIRCLEQ_NEXT(in6p, in6p_queue); 726 if (in6p->in6p_af != AF_INET6) 727 continue; 728 729 im6o = in6p->in6p_moptions; 730 if (im6o) { 731 /* 732 * Unselect the outgoing interface if it is being 733 * detached. 734 */ 735 if (im6o->im6o_multicast_ifp == ifp) 736 im6o->im6o_multicast_ifp = NULL; 737 738 /* 739 * Drop multicast group membership if we joined 740 * through the interface being detached. 741 * XXX controversial - is it really legal for kernel 742 * to force this? 743 */ 744 for (imm = im6o->im6o_memberships.lh_first; 745 imm != NULL; imm = nimm) { 746 nimm = imm->i6mm_chain.le_next; 747 if (imm->i6mm_maddr->in6m_ifp == ifp) { 748 LIST_REMOVE(imm, i6mm_chain); 749 in6_leavegroup(imm); 750 } 751 } 752 } 753 } 754 } 755 756 void 757 in6_pcbpurgeif(table, ifp) 758 struct inpcbtable *table; 759 struct ifnet *ifp; 760 { 761 struct in6pcb *in6p, *nin6p; 762 763 for (in6p = (struct in6pcb *)CIRCLEQ_FIRST(&table->inpt_queue); 764 in6p != (void *)&table->inpt_queue; 765 in6p = nin6p) { 766 nin6p = (struct in6pcb *)CIRCLEQ_NEXT(in6p, in6p_queue); 767 if (in6p->in6p_af != AF_INET6) 768 continue; 769 if (in6p->in6p_route.ro_rt != NULL && 770 in6p->in6p_route.ro_rt->rt_ifp == ifp) 771 in6_rtchange(in6p, 0); 772 } 773 } 774 775 /* 776 * Check for alternatives when higher level complains 777 * about service problems. For now, invalidate cached 778 * routing information. If the route was created dynamically 779 * (by a redirect), time to try a default gateway again. 780 */ 781 void 782 in6_losing(in6p) 783 struct in6pcb *in6p; 784 { 785 struct rtentry *rt; 786 struct rt_addrinfo info; 787 788 if (in6p->in6p_af != AF_INET6) 789 return; 790 791 if ((rt = in6p->in6p_route.ro_rt) != NULL) { 792 in6p->in6p_route.ro_rt = 0; 793 bzero((caddr_t)&info, sizeof(info)); 794 info.rti_info[RTAX_DST] = 795 (struct sockaddr *)&in6p->in6p_route.ro_dst; 796 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 797 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 798 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 799 if (rt->rt_flags & RTF_DYNAMIC) { 800 (void)rtrequest(RTM_DELETE, rt_key(rt), 801 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 802 (struct rtentry **)0); 803 } else { 804 /* 805 * A new route can be allocated 806 * the next time output is attempted. 807 */ 808 rtfree(rt); 809 } 810 } 811 } 812 813 /* 814 * After a routing change, flush old routing 815 * and allocate a (hopefully) better one. 816 */ 817 void 818 in6_rtchange(in6p, errno) 819 struct in6pcb *in6p; 820 int errno; 821 { 822 if (in6p->in6p_af != AF_INET6) 823 return; 824 825 if (in6p->in6p_route.ro_rt) { 826 rtfree(in6p->in6p_route.ro_rt); 827 in6p->in6p_route.ro_rt = 0; 828 /* 829 * A new route can be allocated the next time 830 * output is attempted. 831 */ 832 } 833 } 834 835 struct in6pcb * 836 in6_pcblookup_port(table, laddr6, lport_arg, lookup_wildcard) 837 struct inpcbtable *table; 838 struct in6_addr *laddr6; 839 u_int lport_arg; 840 int lookup_wildcard; 841 { 842 struct inpcbhead *head; 843 struct inpcb_hdr *inph; 844 struct in6pcb *in6p, *match = 0; 845 int matchwild = 3, wildcard; 846 u_int16_t lport = lport_arg; 847 848 head = IN6PCBHASH_PORT(table, lport); 849 LIST_FOREACH(inph, head, inph_lhash) { 850 in6p = (struct in6pcb *)inph; 851 if (in6p->in6p_af != AF_INET6) 852 continue; 853 854 if (in6p->in6p_lport != lport) 855 continue; 856 wildcard = 0; 857 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) { 858 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 859 continue; 860 } 861 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) 862 wildcard++; 863 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) { 864 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 865 continue; 866 if (!IN6_IS_ADDR_V4MAPPED(laddr6)) 867 continue; 868 869 /* duplicate of IPv4 logic */ 870 wildcard = 0; 871 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) && 872 in6p->in6p_faddr.s6_addr32[3]) 873 wildcard++; 874 if (!in6p->in6p_laddr.s6_addr32[3]) { 875 if (laddr6->s6_addr32[3]) 876 wildcard++; 877 } else { 878 if (!laddr6->s6_addr32[3]) 879 wildcard++; 880 else { 881 if (in6p->in6p_laddr.s6_addr32[3] != 882 laddr6->s6_addr32[3]) 883 continue; 884 } 885 } 886 } else if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 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_IS_ADDR_V4MAPPED(laddr6)) { 895 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 896 continue; 897 } 898 if (IN6_IS_ADDR_UNSPECIFIED(laddr6)) 899 wildcard++; 900 else { 901 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, 902 laddr6)) 903 continue; 904 } 905 } 906 if (wildcard && !lookup_wildcard) 907 continue; 908 if (wildcard < matchwild) { 909 match = in6p; 910 matchwild = wildcard; 911 if (matchwild == 0) 912 break; 913 } 914 } 915 return (match); 916 } 917 #undef continue 918 919 /* 920 * WARNING: return value (rtentry) could be IPv4 one if in6pcb is connected to 921 * IPv4 mapped address. 922 */ 923 struct rtentry * 924 in6_pcbrtentry(in6p) 925 struct in6pcb *in6p; 926 { 927 struct route_in6 *ro; 928 struct sockaddr_in6 *dst6; 929 930 ro = &in6p->in6p_route; 931 dst6 = (struct sockaddr_in6 *)&ro->ro_dst; 932 933 if (in6p->in6p_af != AF_INET6) 934 return (NULL); 935 936 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 937 !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &in6p->in6p_faddr))) { 938 RTFREE(ro->ro_rt); 939 ro->ro_rt = (struct rtentry *)NULL; 940 } 941 #ifdef INET 942 if (ro->ro_rt == (struct rtentry *)NULL && 943 IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) { 944 struct sockaddr_in *dst = (struct sockaddr_in *)&ro->ro_dst; 945 946 bzero(dst, sizeof(*dst)); 947 dst->sin_family = AF_INET; 948 dst->sin_len = sizeof(struct sockaddr_in); 949 bcopy(&in6p->in6p_faddr.s6_addr32[3], &dst->sin_addr, 950 sizeof(dst->sin_addr)); 951 rtalloc((struct route *)ro); 952 } else 953 #endif 954 if (ro->ro_rt == (struct rtentry *)NULL && 955 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 956 bzero(dst6, sizeof(*dst6)); 957 dst6->sin6_family = AF_INET6; 958 dst6->sin6_len = sizeof(struct sockaddr_in6); 959 dst6->sin6_addr = in6p->in6p_faddr; 960 rtalloc((struct route *)ro); 961 } 962 return (ro->ro_rt); 963 } 964 965 struct in6pcb * 966 in6_pcblookup_connect(table, faddr6, fport_arg, laddr6, lport_arg, faith) 967 struct inpcbtable *table; 968 struct in6_addr *faddr6; 969 const struct in6_addr *laddr6; 970 u_int fport_arg, lport_arg; 971 int faith; 972 { 973 struct inpcbhead *head; 974 struct inpcb_hdr *inph; 975 struct in6pcb *in6p; 976 u_int16_t fport = fport_arg, lport = lport_arg; 977 978 head = IN6PCBHASH_CONNECT(table, faddr6, fport, laddr6, lport); 979 LIST_FOREACH(inph, head, inph_hash) { 980 in6p = (struct in6pcb *)inph; 981 if (in6p->in6p_af != AF_INET6) 982 continue; 983 984 /* find exact match on both source and dest */ 985 if (in6p->in6p_fport != fport) 986 continue; 987 if (in6p->in6p_lport != lport) 988 continue; 989 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) 990 continue; 991 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)) 992 continue; 993 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 994 continue; 995 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) 996 continue; 997 if ((IN6_IS_ADDR_V4MAPPED(laddr6) || 998 IN6_IS_ADDR_V4MAPPED(faddr6)) && 999 (in6p->in6p_flags & IN6P_IPV6_V6ONLY)) 1000 continue; 1001 return in6p; 1002 } 1003 return NULL; 1004 } 1005 1006 struct in6pcb * 1007 in6_pcblookup_bind(table, laddr6, lport_arg, faith) 1008 struct inpcbtable *table; 1009 struct in6_addr *laddr6; 1010 u_int lport_arg; 1011 int faith; 1012 { 1013 struct inpcbhead *head; 1014 struct inpcb_hdr *inph; 1015 struct in6pcb *in6p; 1016 u_int16_t lport = lport_arg; 1017 #ifdef INET 1018 struct in6_addr zero_mapped; 1019 #endif 1020 1021 head = IN6PCBHASH_BIND(table, laddr6, lport); 1022 LIST_FOREACH(inph, head, inph_hash) { 1023 in6p = (struct in6pcb *)inph; 1024 if (in6p->in6p_af != AF_INET6) 1025 continue; 1026 1027 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) 1028 continue; 1029 if (in6p->in6p_fport != 0) 1030 continue; 1031 if (in6p->in6p_lport != lport) 1032 continue; 1033 if (IN6_IS_ADDR_V4MAPPED(laddr6) && 1034 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 1035 continue; 1036 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) 1037 goto out; 1038 } 1039 #ifdef INET 1040 if (IN6_IS_ADDR_V4MAPPED(laddr6)) { 1041 memset(&zero_mapped, 0, sizeof(zero_mapped)); 1042 zero_mapped.s6_addr16[5] = 0xffff; 1043 head = IN6PCBHASH_BIND(table, &zero_mapped, lport); 1044 LIST_FOREACH(inph, head, inph_hash) { 1045 in6p = (struct in6pcb *)inph; 1046 if (in6p->in6p_af != AF_INET6) 1047 continue; 1048 1049 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) 1050 continue; 1051 if (in6p->in6p_fport != 0) 1052 continue; 1053 if (in6p->in6p_lport != lport) 1054 continue; 1055 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 1056 continue; 1057 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zero_mapped)) 1058 goto out; 1059 } 1060 } 1061 #endif 1062 head = IN6PCBHASH_BIND(table, &zeroin6_addr, lport); 1063 LIST_FOREACH(inph, head, inph_hash) { 1064 in6p = (struct in6pcb *)inph; 1065 if (in6p->in6p_af != AF_INET6) 1066 continue; 1067 1068 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) 1069 continue; 1070 if (in6p->in6p_fport != 0) 1071 continue; 1072 if (in6p->in6p_lport != lport) 1073 continue; 1074 if (IN6_IS_ADDR_V4MAPPED(laddr6) && 1075 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 1076 continue; 1077 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zeroin6_addr)) 1078 goto out; 1079 } 1080 return (NULL); 1081 1082 out: 1083 inph = &in6p->in6p_head; 1084 if (inph != LIST_FIRST(head)) { 1085 LIST_REMOVE(inph, inph_hash); 1086 LIST_INSERT_HEAD(head, inph, inph_hash); 1087 } 1088 return in6p; 1089 } 1090 1091 void 1092 in6_pcbstate(in6p, state) 1093 struct in6pcb *in6p; 1094 int state; 1095 { 1096 1097 if (in6p->in6p_af != AF_INET6) 1098 return; 1099 1100 if (in6p->in6p_state > IN6P_ATTACHED) 1101 LIST_REMOVE(&in6p->in6p_head, inph_hash); 1102 1103 switch (state) { 1104 case IN6P_BOUND: 1105 LIST_INSERT_HEAD(IN6PCBHASH_BIND(in6p->in6p_table, 1106 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head, 1107 inph_hash); 1108 break; 1109 case IN6P_CONNECTED: 1110 LIST_INSERT_HEAD(IN6PCBHASH_CONNECT(in6p->in6p_table, 1111 &in6p->in6p_faddr, in6p->in6p_fport, 1112 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head, 1113 inph_hash); 1114 break; 1115 } 1116 1117 in6p->in6p_state = state; 1118 } 1119