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