1 /* $NetBSD: in6_pcb.c,v 1.53 2002/11/05 21:46:42 perry 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. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: in6_pcb.c,v 1.53 2002/11/05 21:46:42 perry Exp $"); 70 71 #include "opt_inet.h" 72 #include "opt_ipsec.h" 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/malloc.h> 77 #include <sys/mbuf.h> 78 #include <sys/protosw.h> 79 #include <sys/socket.h> 80 #include <sys/socketvar.h> 81 #include <sys/ioctl.h> 82 #include <sys/errno.h> 83 #include <sys/time.h> 84 #include <sys/proc.h> 85 86 #include <net/if.h> 87 #include <net/route.h> 88 89 #include <netinet/in.h> 90 #include <netinet/in_var.h> 91 #include <netinet/in_systm.h> 92 #include <netinet/ip.h> 93 #include <netinet/in_pcb.h> 94 #include <netinet/ip6.h> 95 #include <netinet6/ip6_var.h> 96 #include <netinet6/in6_pcb.h> 97 #include <netinet6/nd6.h> 98 99 #include "loop.h" 100 extern struct ifnet loif[NLOOP]; 101 #include "faith.h" 102 103 #ifdef IPSEC 104 #include <netinet6/ipsec.h> 105 #include <netkey/key.h> 106 #endif /* IPSEC */ 107 108 struct in6_addr zeroin6_addr; 109 110 int ip6_anonportmin = IPV6PORT_ANONMIN; 111 int ip6_anonportmax = IPV6PORT_ANONMAX; 112 int ip6_lowportmin = IPV6PORT_RESERVEDMIN; 113 int ip6_lowportmax = IPV6PORT_RESERVEDMAX; 114 115 int 116 in6_pcballoc(so, head) 117 struct socket *so; 118 struct in6pcb *head; 119 { 120 struct in6pcb *in6p; 121 int s; 122 #ifdef IPSEC 123 int error; 124 #endif 125 126 MALLOC(in6p, struct in6pcb *, sizeof(*in6p), M_PCB, M_NOWAIT); 127 if (in6p == NULL) 128 return (ENOBUFS); 129 bzero((caddr_t)in6p, sizeof(*in6p)); 130 in6p->in6p_head = head; 131 in6p->in6p_socket = so; 132 in6p->in6p_hops = -1; /* use kernel default */ 133 in6p->in6p_icmp6filt = NULL; 134 #ifdef IPSEC 135 error = ipsec_init_pcbpolicy(so, &in6p->in6p_sp); 136 if (error != 0) { 137 FREE(in6p, M_PCB); 138 return error; 139 } 140 #endif /* IPSEC */ 141 s = splnet(); 142 in6p->in6p_next = head->in6p_next; 143 head->in6p_next = in6p; 144 in6p->in6p_prev = head; 145 in6p->in6p_next->in6p_prev = in6p; 146 splx(s); 147 if (ip6_v6only) 148 in6p->in6p_flags |= IN6P_IPV6_V6ONLY; 149 so->so_pcb = (caddr_t)in6p; 150 return (0); 151 } 152 153 int 154 in6_pcbbind(in6p, nam, p) 155 struct in6pcb *in6p; 156 struct mbuf *nam; 157 struct proc *p; 158 { 159 struct socket *so = in6p->in6p_socket; 160 struct in6pcb *head = in6p->in6p_head; 161 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 162 u_int16_t lport = 0; 163 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 164 165 if (in6p->in6p_lport || !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 166 return (EINVAL); 167 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 && 168 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 || 169 (so->so_options & SO_ACCEPTCONN) == 0)) 170 wild = IN6PLOOKUP_WILDCARD; 171 if (nam) { 172 sin6 = mtod(nam, struct sockaddr_in6 *); 173 if (nam->m_len != sizeof(*sin6)) 174 return (EINVAL); 175 /* 176 * We should check the family, but old programs 177 * incorrectly fail to intialize it. 178 */ 179 if (sin6->sin6_family != AF_INET6) 180 return (EAFNOSUPPORT); 181 182 /* 183 * since we do not check port number duplicate with IPv4 space, 184 * we reject it at this moment. If we leave it, we would 185 * mistakenly allow normal users to hijack tcp/udp ports from 186 * other users. 187 */ 188 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 189 return (EADDRNOTAVAIL); 190 191 /* KAME hack: embed scopeid */ 192 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) 193 return EINVAL; 194 /* this must be cleared for ifa_ifwithaddr() */ 195 sin6->sin6_scope_id = 0; 196 197 lport = sin6->sin6_port; 198 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 199 /* 200 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 201 * allow compepte duplication of binding if 202 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 203 * and a multicast address is bound on both 204 * new and duplicated sockets. 205 */ 206 if (so->so_options & SO_REUSEADDR) 207 reuseport = SO_REUSEADDR|SO_REUSEPORT; 208 } 209 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 210 struct sockaddr_in sin; 211 212 bzero(&sin, sizeof(sin)); 213 sin.sin_len = sizeof(sin); 214 sin.sin_family = AF_INET; 215 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr, 216 sizeof(sin.sin_addr)); 217 if (ifa_ifwithaddr((struct sockaddr *)&sin) == 0) 218 return EADDRNOTAVAIL; 219 } 220 else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 221 struct ifaddr *ia = NULL; 222 223 sin6->sin6_port = 0; /* yech... */ 224 if ((in6p->in6p_flags & IN6P_FAITH) == 0 && 225 (ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) 226 return (EADDRNOTAVAIL); 227 228 /* 229 * bind to an anycast address might accidentally 230 * cause sending a packet with an anycast source 231 * address, so we forbid it. 232 * 233 * We should allow to bind to a deprecated address, 234 * since the application dare to use it. 235 * But, can we assume that they are careful enough 236 * to check if the address is deprecated or not? 237 * Maybe, as a safeguard, we should have a setsockopt 238 * flag to control the bind(2) behavior against 239 * deprecated addresses (default: forbid bind(2)). 240 */ 241 if (ia && 242 ((struct in6_ifaddr *)ia)->ia6_flags & 243 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) 244 return (EADDRNOTAVAIL); 245 } 246 if (lport) { 247 #ifndef IPNOPRIVPORTS 248 int priv; 249 250 /* 251 * NOTE: all operating systems use suser() for 252 * privilege check! do not rewrite it into SS_PRIV. 253 */ 254 priv = (p && !suser(p->p_ucred, &p->p_acflag)) ? 1 : 0; 255 /* GROSS */ 256 if (ntohs(lport) < IPV6PORT_RESERVED && !priv) 257 return (EACCES); 258 #endif 259 260 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 261 /* should check this but we can't ... */ 262 #if 0 263 struct inpcb *t; 264 265 t = in_pcblookup_bind(&tcbtable, 266 (struct in_addr *)&sin6->sin6_addr.s6_addr32[3], 267 lport); 268 if (t && (reuseport & t->inp_socket->so_options) == 0) 269 return EADDRINUSE; 270 #endif 271 } else 272 { 273 struct in6pcb *t; 274 275 t = in6_pcblookup(head, &zeroin6_addr, 0, 276 &sin6->sin6_addr, lport, wild); 277 if (t && (reuseport & t->in6p_socket->so_options) == 0) 278 return (EADDRINUSE); 279 } 280 } 281 in6p->in6p_laddr = sin6->sin6_addr; 282 } 283 284 if (lport == 0) { 285 int e; 286 if ((e = in6_pcbsetport(&in6p->in6p_laddr, in6p, p)) != 0) 287 return (e); 288 } 289 else 290 in6p->in6p_lport = lport; 291 292 in6p->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0; /*XXX*/ 293 return (0); 294 } 295 296 /* 297 * Connect from a socket to a specified address. 298 * Both address and port must be specified in argument sin6. 299 * If don't have a local address for this socket yet, 300 * then pick one. 301 */ 302 int 303 in6_pcbconnect(in6p, nam) 304 struct in6pcb *in6p; 305 struct mbuf *nam; 306 { 307 struct in6_addr *in6a = NULL; 308 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *); 309 struct ifnet *ifp = NULL; /* outgoing interface */ 310 int error = 0; 311 #ifdef INET 312 struct in6_addr mapped; 313 #endif 314 struct sockaddr_in6 tmp; 315 316 (void)&in6a; /* XXX fool gcc */ 317 318 if (nam->m_len != sizeof(*sin6)) 319 return (EINVAL); 320 if (sin6->sin6_family != AF_INET6) 321 return (EAFNOSUPPORT); 322 if (sin6->sin6_port == 0) 323 return (EADDRNOTAVAIL); 324 325 /* sanity check for mapped address case */ 326 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 327 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) 328 return EINVAL; 329 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 330 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff); 331 if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) 332 return EINVAL; 333 } else 334 { 335 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) 336 return EINVAL; 337 } 338 339 /* protect *sin6 from overwrites */ 340 tmp = *sin6; 341 sin6 = &tmp; 342 343 /* KAME hack: embed scopeid */ 344 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, &ifp) != 0) 345 return EINVAL; 346 347 /* Source address selection. */ 348 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) 349 && in6p->in6p_laddr.s6_addr32[3] == 0) { 350 #ifdef INET 351 struct sockaddr_in sin, *sinp; 352 353 bzero(&sin, sizeof(sin)); 354 sin.sin_len = sizeof(sin); 355 sin.sin_family = AF_INET; 356 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr, 357 sizeof(sin.sin_addr)); 358 sinp = in_selectsrc(&sin, (struct route *)&in6p->in6p_route, 359 in6p->in6p_socket->so_options, NULL, &error); 360 if (sinp == 0) { 361 if (error == 0) 362 error = EADDRNOTAVAIL; 363 return (error); 364 } 365 bzero(&mapped, sizeof(mapped)); 366 mapped.s6_addr16[5] = htons(0xffff); 367 bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr)); 368 in6a = &mapped; 369 #else 370 return EADDRNOTAVAIL; 371 #endif 372 } else 373 { 374 /* 375 * XXX: in6_selectsrc might replace the bound local address 376 * with the address specified by setsockopt(IPV6_PKTINFO). 377 * Is it the intended behavior? 378 */ 379 in6a = in6_selectsrc(sin6, in6p->in6p_outputopts, 380 in6p->in6p_moptions, 381 &in6p->in6p_route, 382 &in6p->in6p_laddr, &error); 383 if (in6a == 0) { 384 if (error == 0) 385 error = EADDRNOTAVAIL; 386 return (error); 387 } 388 } 389 if (in6p->in6p_route.ro_rt) 390 ifp = in6p->in6p_route.ro_rt->rt_ifp; 391 392 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp); 393 394 if (in6_pcblookup(in6p->in6p_head, 395 &sin6->sin6_addr, 396 sin6->sin6_port, 397 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ? 398 in6a : &in6p->in6p_laddr, 399 in6p->in6p_lport, 400 0)) 401 return (EADDRINUSE); 402 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) 403 || (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) 404 && in6p->in6p_laddr.s6_addr32[3] == 0)) 405 { 406 /* 407 * XXX in IPv4 mapped address case, we should grab fresh 408 * local port number by in_pcbbind, not in6_pcbbind. 409 * if we are in bad luck, we may assign conflicting port number 410 * between IPv4 and IPv6 unwillingly. 411 */ 412 if (in6p->in6p_lport == 0) { 413 (void)in6_pcbbind(in6p, (struct mbuf *)0, 414 (struct proc *)0); 415 } 416 in6p->in6p_laddr = *in6a; 417 } 418 in6p->in6p_faddr = sin6->sin6_addr; 419 in6p->in6p_fport = sin6->sin6_port; 420 /* 421 * xxx kazu flowlabel is necessary for connect? 422 * but if this line is missing, the garbage value remains. 423 */ 424 in6p->in6p_flowinfo = sin6->sin6_flowinfo; 425 #ifdef IPSEC 426 if (in6p->in6p_socket->so_type == SOCK_STREAM) 427 ipsec_pcbconn(in6p->in6p_sp); 428 #endif 429 return (0); 430 } 431 432 void 433 in6_pcbdisconnect(in6p) 434 struct in6pcb *in6p; 435 { 436 bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr)); 437 in6p->in6p_fport = 0; 438 if (in6p->in6p_socket->so_state & SS_NOFDREF) 439 in6_pcbdetach(in6p); 440 #ifdef IPSEC 441 ipsec_pcbdisconn(in6p->in6p_sp); 442 #endif 443 } 444 445 void 446 in6_pcbdetach(in6p) 447 struct in6pcb *in6p; 448 { 449 struct socket *so = in6p->in6p_socket; 450 int s; 451 452 #ifdef IPSEC 453 ipsec6_delete_pcbpolicy(in6p); 454 #endif /* IPSEC */ 455 sotoin6pcb(so) = 0; 456 sofree(so); 457 if (in6p->in6p_options) 458 m_freem(in6p->in6p_options); 459 if (in6p->in6p_outputopts) { 460 if (in6p->in6p_outputopts->ip6po_rthdr && 461 in6p->in6p_outputopts->ip6po_route.ro_rt) 462 RTFREE(in6p->in6p_outputopts->ip6po_route.ro_rt); 463 if (in6p->in6p_outputopts->ip6po_m) 464 (void)m_free(in6p->in6p_outputopts->ip6po_m); 465 free(in6p->in6p_outputopts, M_IP6OPT); 466 } 467 if (in6p->in6p_route.ro_rt) 468 rtfree(in6p->in6p_route.ro_rt); 469 ip6_freemoptions(in6p->in6p_moptions); 470 s = splnet(); 471 in6p->in6p_next->in6p_prev = in6p->in6p_prev; 472 in6p->in6p_prev->in6p_next = in6p->in6p_next; 473 in6p->in6p_prev = NULL; 474 splx(s); 475 FREE(in6p, M_PCB); 476 } 477 478 void 479 in6_setsockaddr(in6p, nam) 480 struct in6pcb *in6p; 481 struct mbuf *nam; 482 { 483 struct sockaddr_in6 *sin6; 484 485 nam->m_len = sizeof(*sin6); 486 sin6 = mtod(nam, struct sockaddr_in6 *); 487 bzero((caddr_t)sin6, sizeof(*sin6)); 488 sin6->sin6_family = AF_INET6; 489 sin6->sin6_len = sizeof(struct sockaddr_in6); 490 sin6->sin6_port = in6p->in6p_lport; 491 /* KAME hack: recover scopeid */ 492 (void)in6_recoverscope(sin6, &in6p->in6p_laddr, NULL); 493 } 494 495 void 496 in6_setpeeraddr(in6p, nam) 497 struct in6pcb *in6p; 498 struct mbuf *nam; 499 { 500 struct sockaddr_in6 *sin6; 501 502 nam->m_len = sizeof(*sin6); 503 sin6 = mtod(nam, struct sockaddr_in6 *); 504 bzero((caddr_t)sin6, sizeof(*sin6)); 505 sin6->sin6_family = AF_INET6; 506 sin6->sin6_len = sizeof(struct sockaddr_in6); 507 sin6->sin6_port = in6p->in6p_fport; 508 /* KAME hack: recover scopeid */ 509 (void)in6_recoverscope(sin6, &in6p->in6p_faddr, NULL); 510 } 511 512 /* 513 * Pass some notification to all connections of a protocol 514 * associated with address dst. The local address and/or port numbers 515 * may be specified to limit the search. The "usual action" will be 516 * taken, depending on the ctlinput cmd. The caller must filter any 517 * cmds that are uninteresting (e.g., no error in the map). 518 * Call the protocol specific routine (if any) to report 519 * any errors for each matching socket. 520 * 521 * Must be called at splsoftnet. 522 * 523 * Note: src (4th arg) carries the flowlabel value on the original IPv6 524 * header, in sin6_flowinfo member. 525 */ 526 int 527 in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify) 528 struct in6pcb *head; 529 struct sockaddr *dst, *src; 530 u_int fport_arg, lport_arg; 531 int cmd; 532 void *cmdarg; 533 void (*notify) __P((struct in6pcb *, int)); 534 { 535 struct in6pcb *in6p, *nin6p; 536 struct sockaddr_in6 sa6_src, *sa6_dst; 537 u_int16_t fport = fport_arg, lport = lport_arg; 538 int errno; 539 int nmatch = 0; 540 u_int32_t flowinfo; 541 542 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6) 543 return 0; 544 545 sa6_dst = (struct sockaddr_in6 *)dst; 546 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 547 return 0; 548 549 /* 550 * note that src can be NULL when we get notify by local fragmentation. 551 */ 552 sa6_src = (src == NULL) ? sa6_any : *(struct sockaddr_in6 *)src; 553 flowinfo = sa6_src.sin6_flowinfo; 554 555 /* 556 * Redirects go to all references to the destination, 557 * and use in6_rtchange to invalidate the route cache. 558 * Dead host indications: also use in6_rtchange to invalidate 559 * the cache, and deliver the error to all the sockets. 560 * Otherwise, if we have knowledge of the local port and address, 561 * deliver only to that socket. 562 */ 563 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 564 fport = 0; 565 lport = 0; 566 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 567 568 if (cmd != PRC_HOSTDEAD) 569 notify = in6_rtchange; 570 } 571 572 errno = inet6ctlerrmap[cmd]; 573 for (in6p = head->in6p_next; in6p != head; in6p = nin6p) { 574 nin6p = in6p->in6p_next; 575 576 /* 577 * Under the following condition, notify of redirects 578 * to the pcb, without making address matches against inpcb. 579 * - redirect notification is arrived. 580 * - the inpcb is unconnected. 581 * - the inpcb is caching !RTF_HOST routing entry. 582 * - the ICMPv6 notification is from the gateway cached in the 583 * inpcb. i.e. ICMPv6 notification is from nexthop gateway 584 * the inpcb used very recently. 585 * 586 * This is to improve interaction between netbsd/openbsd 587 * redirect handling code, and inpcb route cache code. 588 * without the clause, !RTF_HOST routing entry (which carries 589 * gateway used by inpcb right before the ICMPv6 redirect) 590 * will be cached forever in unconnected inpcb. 591 * 592 * There still is a question regarding to what is TRT: 593 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be 594 * generated on packet output. inpcb will always cache 595 * RTF_HOST routing entry so there's no need for the clause 596 * (ICMPv6 redirect will update RTF_HOST routing entry, 597 * and inpcb is caching it already). 598 * However, bsdi/freebsd are vulnerable to local DoS attacks 599 * due to the cloned routing entries. 600 * - Specwise, "destination cache" is mentioned in RFC2461. 601 * Jinmei says that it implies bsdi/freebsd behavior, itojun 602 * is not really convinced. 603 * - Having hiwat/lowat on # of cloned host route (redirect/ 604 * pmtud) may be a good idea. netbsd/openbsd has it. see 605 * icmp6_mtudisc_update(). 606 */ 607 if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) && 608 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 609 in6p->in6p_route.ro_rt && 610 !(in6p->in6p_route.ro_rt->rt_flags & RTF_HOST)) { 611 struct sockaddr_in6 *dst6; 612 613 dst6 = (struct sockaddr_in6 *)&in6p->in6p_route.ro_dst; 614 if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, 615 &sa6_dst->sin6_addr)) 616 goto do_notify; 617 } 618 619 /* 620 * Detect if we should notify the error. If no source and 621 * destination ports are specified, but non-zero flowinfo and 622 * local address match, notify the error. This is the case 623 * when the error is delivered with an encrypted buffer 624 * by ESP. Otherwise, just compare addresses and ports 625 * as usual. 626 */ 627 if (lport == 0 && fport == 0 && flowinfo && 628 in6p->in6p_socket != NULL && 629 flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && 630 IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr)) 631 goto do_notify; 632 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, 633 &sa6_dst->sin6_addr) || 634 in6p->in6p_socket == 0 || 635 (lport && in6p->in6p_lport != lport) || 636 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 637 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, 638 &sa6_src.sin6_addr)) || 639 (fport && in6p->in6p_fport != fport)) 640 continue; 641 642 do_notify: 643 if (notify) 644 (*notify)(in6p, errno); 645 nmatch++; 646 } 647 return nmatch; 648 } 649 650 void 651 in6_pcbpurgeif0(head, ifp) 652 struct in6pcb *head; 653 struct ifnet *ifp; 654 { 655 struct in6pcb *in6p, *nin6p; 656 struct ip6_moptions *im6o; 657 struct in6_multi_mship *imm, *nimm; 658 659 for (in6p = head->in6p_next; in6p != head; in6p = nin6p) { 660 nin6p = in6p->in6p_next; 661 im6o = in6p->in6p_moptions; 662 if (im6o) { 663 /* 664 * Unselect the outgoing interface if it is being 665 * detached. 666 */ 667 if (im6o->im6o_multicast_ifp == ifp) 668 im6o->im6o_multicast_ifp = NULL; 669 670 /* 671 * Drop multicast group membership if we joined 672 * through the interface being detached. 673 * XXX controversial - is it really legal for kernel 674 * to force this? 675 */ 676 for (imm = im6o->im6o_memberships.lh_first; 677 imm != NULL; imm = nimm) { 678 nimm = imm->i6mm_chain.le_next; 679 if (imm->i6mm_maddr->in6m_ifp == ifp) { 680 LIST_REMOVE(imm, i6mm_chain); 681 in6_leavegroup(imm); 682 } 683 } 684 } 685 } 686 } 687 688 void 689 in6_pcbpurgeif(head, ifp) 690 struct in6pcb *head; 691 struct ifnet *ifp; 692 { 693 struct in6pcb *in6p, *nin6p; 694 695 for (in6p = head->in6p_next; in6p != head; in6p = nin6p) { 696 nin6p = in6p->in6p_next; 697 if (in6p->in6p_route.ro_rt != NULL && 698 in6p->in6p_route.ro_rt->rt_ifp == ifp) 699 in6_rtchange(in6p, 0); 700 } 701 } 702 703 /* 704 * Check for alternatives when higher level complains 705 * about service problems. For now, invalidate cached 706 * routing information. If the route was created dynamically 707 * (by a redirect), time to try a default gateway again. 708 */ 709 void 710 in6_losing(in6p) 711 struct in6pcb *in6p; 712 { 713 struct rtentry *rt; 714 struct rt_addrinfo info; 715 716 if ((rt = in6p->in6p_route.ro_rt) != NULL) { 717 in6p->in6p_route.ro_rt = 0; 718 bzero((caddr_t)&info, sizeof(info)); 719 info.rti_info[RTAX_DST] = 720 (struct sockaddr *)&in6p->in6p_route.ro_dst; 721 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 722 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 723 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 724 if (rt->rt_flags & RTF_DYNAMIC) { 725 (void)rtrequest(RTM_DELETE, rt_key(rt), 726 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 727 (struct rtentry **)0); 728 } else { 729 /* 730 * A new route can be allocated 731 * the next time output is attempted. 732 */ 733 rtfree(rt); 734 } 735 } 736 } 737 738 /* 739 * After a routing change, flush old routing 740 * and allocate a (hopefully) better one. 741 */ 742 void 743 in6_rtchange(in6p, errno) 744 struct in6pcb *in6p; 745 int errno; 746 { 747 if (in6p->in6p_route.ro_rt) { 748 rtfree(in6p->in6p_route.ro_rt); 749 in6p->in6p_route.ro_rt = 0; 750 /* 751 * A new route can be allocated the next time 752 * output is attempted. 753 */ 754 } 755 } 756 757 struct in6pcb * 758 in6_pcblookup(head, faddr6, fport_arg, laddr6, lport_arg, flags) 759 struct in6pcb *head; 760 struct in6_addr *faddr6, *laddr6; 761 u_int fport_arg, lport_arg; 762 int flags; 763 { 764 struct in6pcb *in6p, *match = 0; 765 int matchwild = 3, wildcard; 766 u_int16_t fport = fport_arg, lport = lport_arg; 767 768 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) { 769 if (in6p->in6p_lport != lport) 770 continue; 771 wildcard = 0; 772 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 773 if (IN6_IS_ADDR_UNSPECIFIED(laddr6)) 774 wildcard++; 775 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) 776 continue; 777 } 778 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) 779 && in6p->in6p_laddr.s6_addr32[3] == 0) { 780 if (!IN6_IS_ADDR_V4MAPPED(laddr6)) 781 continue; 782 if (laddr6->s6_addr32[3] == 0) 783 ; 784 else 785 wildcard++; 786 } 787 else { 788 if (IN6_IS_ADDR_V4MAPPED(laddr6)) { 789 if (in6p->in6p_flags & IN6P_IPV6_V6ONLY) 790 continue; 791 else 792 wildcard++; 793 } else if (!IN6_IS_ADDR_UNSPECIFIED(laddr6)) 794 wildcard++; 795 } 796 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 797 if (IN6_IS_ADDR_UNSPECIFIED(faddr6)) 798 wildcard++; 799 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6) 800 || in6p->in6p_fport != fport) 801 continue; 802 } 803 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) 804 && in6p->in6p_faddr.s6_addr32[3] == 0) { 805 if (!IN6_IS_ADDR_V4MAPPED(faddr6)) 806 continue; 807 if (faddr6->s6_addr32[3] == 0) 808 ; 809 else 810 wildcard++; 811 } 812 else { 813 if (IN6_IS_ADDR_V4MAPPED(faddr6)) { 814 if (in6p->in6p_flags & IN6P_IPV6_V6ONLY) 815 continue; 816 else 817 wildcard++; 818 } else if (!IN6_IS_ADDR_UNSPECIFIED(faddr6)) 819 wildcard++; 820 } 821 822 if (wildcard && (flags & IN6PLOOKUP_WILDCARD) == 0) 823 continue; 824 if (wildcard < matchwild) { 825 match = in6p; 826 matchwild = wildcard; 827 if (matchwild == 0) 828 break; 829 } 830 } 831 return (match); 832 } 833 834 struct rtentry * 835 in6_pcbrtentry(in6p) 836 struct in6pcb *in6p; 837 { 838 struct route_in6 *ro; 839 struct sockaddr_in6 *dst6; 840 841 ro = &in6p->in6p_route; 842 dst6 = (struct sockaddr_in6 *)&ro->ro_dst; 843 844 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 845 !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &in6p->in6p_faddr))) { 846 RTFREE(ro->ro_rt); 847 ro->ro_rt = (struct rtentry *)NULL; 848 } 849 if (ro->ro_rt == (struct rtentry *)NULL && 850 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 851 bzero(dst6, sizeof(*dst6)); 852 dst6->sin6_family = AF_INET6; 853 dst6->sin6_len = sizeof(struct sockaddr_in6); 854 dst6->sin6_addr = in6p->in6p_faddr; 855 rtalloc((struct route *)ro); 856 } 857 return (ro->ro_rt); 858 } 859 860 struct in6pcb * 861 in6_pcblookup_connect(head, faddr6, fport_arg, laddr6, lport_arg, faith) 862 struct in6pcb *head; 863 struct in6_addr *faddr6, *laddr6; 864 u_int fport_arg, lport_arg; 865 int faith; 866 { 867 struct in6pcb *in6p; 868 u_int16_t fport = fport_arg, lport = lport_arg; 869 870 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) { 871 /* find exact match on both source and dest */ 872 if (in6p->in6p_fport != fport) 873 continue; 874 if (in6p->in6p_lport != lport) 875 continue; 876 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) 877 continue; 878 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)) 879 continue; 880 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 881 continue; 882 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) 883 continue; 884 if ((IN6_IS_ADDR_V4MAPPED(laddr6) || 885 IN6_IS_ADDR_V4MAPPED(faddr6)) && 886 (in6p->in6p_flags & IN6P_IPV6_V6ONLY)) 887 continue; 888 return in6p; 889 } 890 return NULL; 891 } 892 893 struct in6pcb * 894 in6_pcblookup_bind(head, laddr6, lport_arg, faith) 895 struct in6pcb *head; 896 struct in6_addr *laddr6; 897 u_int lport_arg; 898 int faith; 899 { 900 struct in6pcb *in6p, *match; 901 u_int16_t lport = lport_arg; 902 903 match = NULL; 904 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) { 905 /* 906 * find destination match. exact match is preferred 907 * against wildcard match. 908 */ 909 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) 910 continue; 911 if (in6p->in6p_fport != 0) 912 continue; 913 if (in6p->in6p_lport != lport) 914 continue; 915 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 916 if (IN6_IS_ADDR_V4MAPPED(laddr6)) { 917 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) 918 continue; 919 else 920 match = in6p; 921 } else 922 match = in6p; 923 } 924 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) && 925 !(in6p->in6p_flags & IN6P_IPV6_V6ONLY) && 926 in6p->in6p_laddr.s6_addr32[3] == 0) { 927 if (IN6_IS_ADDR_V4MAPPED(laddr6) && 928 laddr6->s6_addr32[3] != 0) 929 match = in6p; 930 } 931 else if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) 932 return in6p; 933 } 934 return match; 935 } 936