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