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