1 /* $FreeBSD$ */ 2 /* $KAME: in6_pcb.c,v 1.8 2000/06/09 00:37:02 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 /* 35 * Copyright (c) 1982, 1986, 1991, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by the University of 49 * California, Berkeley and its contributors. 50 * 4. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 67 */ 68 69 #include "opt_ipsec.h" 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/malloc.h> 74 #include <sys/mbuf.h> 75 #include <sys/domain.h> 76 #include <sys/protosw.h> 77 #include <sys/socket.h> 78 #include <sys/socketvar.h> 79 #include <sys/sockio.h> 80 #include <sys/errno.h> 81 #include <sys/time.h> 82 #include <sys/proc.h> 83 #include <sys/jail.h> 84 85 #include <vm/vm_zone.h> 86 87 #include <net/if.h> 88 #include <net/if_types.h> 89 #include <net/route.h> 90 91 #include <netinet/in.h> 92 #include <netinet/in_var.h> 93 #include <netinet/in_systm.h> 94 #include <netinet/ip6.h> 95 #include <netinet/ip_var.h> 96 #include <netinet6/ip6_var.h> 97 #include <netinet6/nd6.h> 98 #include <netinet/in_pcb.h> 99 #include <netinet6/in6_pcb.h> 100 101 #include "faith.h" 102 103 #ifdef IPSEC 104 #include <netinet6/ipsec.h> 105 #include <netinet6/ah.h> 106 #include <netinet6/ipsec6.h> 107 #include <netinet6/ah6.h> 108 #include <netkey/key.h> 109 #endif /* IPSEC */ 110 111 struct in6_addr zeroin6_addr; 112 113 int 114 in6_pcbbind(inp, nam, p) 115 register struct inpcb *inp; 116 struct sockaddr *nam; 117 struct proc *p; 118 { 119 struct socket *so = inp->inp_socket; 120 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 121 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 122 u_short lport = 0; 123 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 124 125 if (!in6_ifaddr) /* XXX broken! */ 126 return (EADDRNOTAVAIL); 127 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 128 return(EINVAL); 129 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 130 wild = 1; 131 if (nam) { 132 sin6 = (struct sockaddr_in6 *)nam; 133 if (nam->sa_len != sizeof(*sin6)) 134 return(EINVAL); 135 /* 136 * family check. 137 */ 138 if (nam->sa_family != AF_INET6) 139 return(EAFNOSUPPORT); 140 141 /* KAME hack: embed scopeid */ 142 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0) 143 return EINVAL; 144 /* this must be cleared for ifa_ifwithaddr() */ 145 sin6->sin6_scope_id = 0; 146 147 lport = sin6->sin6_port; 148 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 149 /* 150 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 151 * allow compepte duplication of binding if 152 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 153 * and a multicast address is bound on both 154 * new and duplicated sockets. 155 */ 156 if (so->so_options & SO_REUSEADDR) 157 reuseport = SO_REUSEADDR|SO_REUSEPORT; 158 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 159 struct ifaddr *ia = NULL; 160 161 sin6->sin6_port = 0; /* yech... */ 162 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) 163 return(EADDRNOTAVAIL); 164 165 /* 166 * XXX: bind to an anycast address might accidentally 167 * cause sending a packet with anycast source address. 168 */ 169 if (ia && 170 ((struct in6_ifaddr *)ia)->ia6_flags & 171 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 172 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 173 return(EADDRNOTAVAIL); 174 } 175 } 176 if (lport) { 177 struct inpcb *t; 178 179 /* GROSS */ 180 if (ntohs(lport) < IPV6PORT_RESERVED && p && 181 suser_xxx(0, p, PRISON_ROOT)) 182 return(EACCES); 183 if (so->so_cred->cr_uid != 0 && 184 !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 185 t = in6_pcblookup_local(pcbinfo, 186 &sin6->sin6_addr, lport, 187 INPLOOKUP_WILDCARD); 188 if (t && 189 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 190 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || 191 (t->inp_socket->so_options & 192 SO_REUSEPORT) == 0) && 193 (so->so_cred->cr_uid != 194 t->inp_socket->so_cred->cr_uid)) 195 return (EADDRINUSE); 196 if (ip6_mapped_addr_on != 0 && 197 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 198 struct sockaddr_in sin; 199 200 in6_sin6_2_sin(&sin, sin6); 201 t = in_pcblookup_local(pcbinfo, 202 sin.sin_addr, lport, 203 INPLOOKUP_WILDCARD); 204 if (t && 205 (so->so_cred->cr_uid != 206 t->inp_socket->so_cred->cr_uid) && 207 (ntohl(t->inp_laddr.s_addr) != 208 INADDR_ANY || 209 INP_SOCKAF(so) == 210 INP_SOCKAF(t->inp_socket))) 211 return (EADDRINUSE); 212 } 213 } 214 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, 215 lport, wild); 216 if (t && (reuseport & t->inp_socket->so_options) == 0) 217 return(EADDRINUSE); 218 if (ip6_mapped_addr_on != 0 && 219 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 220 struct sockaddr_in sin; 221 222 in6_sin6_2_sin(&sin, sin6); 223 t = in_pcblookup_local(pcbinfo, sin.sin_addr, 224 lport, wild); 225 if (t && 226 (reuseport & t->inp_socket->so_options) 227 == 0 && 228 (ntohl(t->inp_laddr.s_addr) 229 != INADDR_ANY || 230 INP_SOCKAF(so) == 231 INP_SOCKAF(t->inp_socket))) 232 return (EADDRINUSE); 233 } 234 } 235 inp->in6p_laddr = sin6->sin6_addr; 236 } 237 if (lport == 0) { 238 int e; 239 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, p)) != 0) 240 return(e); 241 } 242 else { 243 inp->inp_lport = lport; 244 if (in_pcbinshash(inp) != 0) { 245 inp->in6p_laddr = in6addr_any; 246 inp->inp_lport = 0; 247 return (EAGAIN); 248 } 249 } 250 inp->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0; /*XXX*/ 251 return(0); 252 } 253 254 /* 255 * Transform old in6_pcbconnect() into an inner subroutine for new 256 * in6_pcbconnect(): Do some validity-checking on the remote 257 * address (in mbuf 'nam') and then determine local host address 258 * (i.e., which interface) to use to access that remote host. 259 * 260 * This preserves definition of in6_pcbconnect(), while supporting a 261 * slightly different version for T/TCP. (This is more than 262 * a bit of a kludge, but cleaning up the internal interfaces would 263 * have forced minor changes in every protocol). 264 */ 265 266 int 267 in6_pcbladdr(inp, nam, plocal_addr6) 268 register struct inpcb *inp; 269 struct sockaddr *nam; 270 struct in6_addr **plocal_addr6; 271 { 272 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 273 struct in6_pktinfo *pi; 274 struct ifnet *ifp = NULL; 275 int error = 0; 276 277 if (nam->sa_len != sizeof (*sin6)) 278 return (EINVAL); 279 if (sin6->sin6_family != AF_INET6) 280 return (EAFNOSUPPORT); 281 if (sin6->sin6_port == 0) 282 return (EADDRNOTAVAIL); 283 284 /* KAME hack: embed scopeid */ 285 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0) 286 return EINVAL; 287 288 if (in6_ifaddr) { 289 /* 290 * If the destination address is UNSPECIFIED addr, 291 * use the loopback addr, e.g ::1. 292 */ 293 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 294 sin6->sin6_addr = in6addr_loopback; 295 } 296 { 297 /* 298 * XXX: in6_selectsrc might replace the bound local address 299 * with the address specified by setsockopt(IPV6_PKTINFO). 300 * Is it the intended behavior? 301 */ 302 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts, 303 inp->in6p_moptions, 304 &inp->in6p_route, 305 &inp->in6p_laddr, &error); 306 if (*plocal_addr6 == 0) { 307 if (error == 0) 308 error = EADDRNOTAVAIL; 309 return(error); 310 } 311 /* 312 * Don't do pcblookup call here; return interface in 313 * plocal_addr6 314 * and exit to caller, that will do the lookup. 315 */ 316 } 317 318 if (inp->in6p_route.ro_rt) 319 ifp = inp->in6p_route.ro_rt->rt_ifp; 320 321 return(0); 322 } 323 324 /* 325 * Outer subroutine: 326 * Connect from a socket to a specified address. 327 * Both address and port must be specified in argument sin. 328 * If don't have a local address for this socket yet, 329 * then pick one. 330 */ 331 int 332 in6_pcbconnect(inp, nam, p) 333 register struct inpcb *inp; 334 struct sockaddr *nam; 335 struct proc *p; 336 { 337 struct in6_addr *addr6; 338 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 339 int error; 340 341 /* 342 * Call inner routine, to assign local interface address. 343 */ 344 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0) 345 return(error); 346 347 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, 348 sin6->sin6_port, 349 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 350 ? addr6 : &inp->in6p_laddr, 351 inp->inp_lport, 0, NULL) != NULL) { 352 return (EADDRINUSE); 353 } 354 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 355 if (inp->inp_lport == 0) { 356 error = in6_pcbbind(inp, (struct sockaddr *)0, p); 357 if (error) 358 return (error); 359 } 360 inp->in6p_laddr = *addr6; 361 } 362 inp->in6p_faddr = sin6->sin6_addr; 363 inp->inp_fport = sin6->sin6_port; 364 /* 365 * xxx kazu flowlabel is necessary for connect? 366 * but if this line is missing, the garbage value remains. 367 */ 368 inp->in6p_flowinfo = sin6->sin6_flowinfo; 369 if ((inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) == 0 && 370 ip6_auto_flowlabel != 0) 371 inp->in6p_flowinfo |= 372 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK); 373 374 in_pcbrehash(inp); 375 return (0); 376 } 377 378 #if 0 379 /* 380 * Return an IPv6 address, which is the most appropriate for given 381 * destination and user specified options. 382 * If necessary, this function lookups the routing table and return 383 * an entry to the caller for later use. 384 */ 385 struct in6_addr * 386 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp) 387 struct sockaddr_in6 *dstsock; 388 struct ip6_pktopts *opts; 389 struct ip6_moptions *mopts; 390 struct route_in6 *ro; 391 struct in6_addr *laddr; 392 int *errorp; 393 { 394 struct in6_addr *dst; 395 struct in6_ifaddr *ia6 = 0; 396 struct in6_pktinfo *pi = NULL; 397 398 dst = &dstsock->sin6_addr; 399 *errorp = 0; 400 401 /* 402 * If the source address is explicitly specified by the caller, 403 * use it. 404 */ 405 if (opts && (pi = opts->ip6po_pktinfo) && 406 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) 407 return(&pi->ipi6_addr); 408 409 /* 410 * If the source address is not specified but the socket(if any) 411 * is already bound, use the bound address. 412 */ 413 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) 414 return(laddr); 415 416 /* 417 * If the caller doesn't specify the source address but 418 * the outgoing interface, use an address associated with 419 * the interface. 420 */ 421 if (pi && pi->ipi6_ifindex) { 422 /* XXX boundary check is assumed to be already done. */ 423 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex], 424 dst); 425 if (ia6 == 0) { 426 *errorp = EADDRNOTAVAIL; 427 return(0); 428 } 429 return(&satosin6(&ia6->ia_addr)->sin6_addr); 430 } 431 432 /* 433 * If the destination address is a link-local unicast address or 434 * a multicast address, and if the outgoing interface is specified 435 * by the sin6_scope_id filed, use an address associated with the 436 * interface. 437 * XXX: We're now trying to define more specific semantics of 438 * sin6_scope_id field, so this part will be rewritten in 439 * the near future. 440 */ 441 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) && 442 dstsock->sin6_scope_id) { 443 /* 444 * I'm not sure if boundary check for scope_id is done 445 * somewhere... 446 */ 447 if (dstsock->sin6_scope_id < 0 || 448 if_index < dstsock->sin6_scope_id) { 449 *errorp = ENXIO; /* XXX: better error? */ 450 return(0); 451 } 452 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id], 453 dst); 454 if (ia6 == 0) { 455 *errorp = EADDRNOTAVAIL; 456 return(0); 457 } 458 return(&satosin6(&ia6->ia_addr)->sin6_addr); 459 } 460 461 /* 462 * If the destination address is a multicast address and 463 * the outgoing interface for the address is specified 464 * by the caller, use an address associated with the interface. 465 * There is a sanity check here; if the destination has node-local 466 * scope, the outgoing interfacde should be a loopback address. 467 * Even if the outgoing interface is not specified, we also 468 * choose a loopback interface as the outgoing interface. 469 */ 470 if (IN6_IS_ADDR_MULTICAST(dst)) { 471 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL; 472 473 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) { 474 ifp = &loif[0]; 475 } 476 477 if (ifp) { 478 ia6 = in6_ifawithscope(ifp, dst); 479 if (ia6 == 0) { 480 *errorp = EADDRNOTAVAIL; 481 return(0); 482 } 483 return(&ia6->ia_addr.sin6_addr); 484 } 485 } 486 487 /* 488 * If the next hop address for the packet is specified 489 * by caller, use an address associated with the route 490 * to the next hop. 491 */ 492 { 493 struct sockaddr_in6 *sin6_next; 494 struct rtentry *rt; 495 496 if (opts && opts->ip6po_nexthop) { 497 sin6_next = satosin6(opts->ip6po_nexthop); 498 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL); 499 if (rt) { 500 ia6 = in6_ifawithscope(rt->rt_ifp, dst); 501 if (ia6 == 0) 502 ia6 = ifatoia6(rt->rt_ifa); 503 } 504 if (ia6 == 0) { 505 *errorp = EADDRNOTAVAIL; 506 return(0); 507 } 508 return(&satosin6(&ia6->ia_addr)->sin6_addr); 509 } 510 } 511 512 /* 513 * If route is known or can be allocated now, 514 * our src addr is taken from the i/f, else punt. 515 */ 516 if (ro) { 517 if (ro->ro_rt && 518 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) { 519 RTFREE(ro->ro_rt); 520 ro->ro_rt = (struct rtentry *)0; 521 } 522 if (ro->ro_rt == (struct rtentry *)0 || 523 ro->ro_rt->rt_ifp == (struct ifnet *)0) { 524 /* No route yet, so try to acquire one */ 525 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 526 ro->ro_dst.sin6_family = AF_INET6; 527 ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6); 528 ro->ro_dst.sin6_addr = *dst; 529 if (IN6_IS_ADDR_MULTICAST(dst)) { 530 ro->ro_rt = rtalloc1(&((struct route *)ro) 531 ->ro_dst, 0, 0UL); 532 } else { 533 rtalloc((struct route *)ro); 534 } 535 } 536 537 /* 538 * in_pcbconnect() checks out IFF_LOOPBACK to skip using 539 * the address. But we don't know why it does so. 540 * It is necessary to ensure the scope even for lo0 541 * so doesn't check out IFF_LOOPBACK. 542 */ 543 544 if (ro->ro_rt) { 545 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst); 546 if (ia6 == 0) /* xxx scope error ?*/ 547 ia6 = ifatoia6(ro->ro_rt->rt_ifa); 548 } 549 if (ia6 == 0) { 550 *errorp = EHOSTUNREACH; /* no route */ 551 return(0); 552 } 553 return(&satosin6(&ia6->ia_addr)->sin6_addr); 554 } 555 556 *errorp = EADDRNOTAVAIL; 557 return(0); 558 } 559 560 /* 561 * Default hop limit selection. The precedence is as follows: 562 * 1. Hoplimit valued specified via ioctl. 563 * 2. (If the outgoing interface is detected) the current 564 * hop limit of the interface specified by router advertisement. 565 * 3. The system default hoplimit. 566 */ 567 int 568 in6_selecthlim(in6p, ifp) 569 struct in6pcb *in6p; 570 struct ifnet *ifp; 571 { 572 if (in6p && in6p->in6p_hops >= 0) 573 return(in6p->in6p_hops); 574 else if (ifp) 575 return(nd_ifinfo[ifp->if_index].chlim); 576 else 577 return(ip6_defhlim); 578 } 579 #endif 580 581 void 582 in6_pcbdisconnect(inp) 583 struct inpcb *inp; 584 { 585 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr)); 586 inp->inp_fport = 0; 587 in_pcbrehash(inp); 588 if (inp->inp_socket->so_state & SS_NOFDREF) 589 in6_pcbdetach(inp); 590 } 591 592 void 593 in6_pcbdetach(inp) 594 struct inpcb *inp; 595 { 596 struct socket *so = inp->inp_socket; 597 struct inpcbinfo *ipi = inp->inp_pcbinfo; 598 599 #ifdef IPSEC 600 if (inp->in6p_sp != NULL) 601 ipsec6_delete_pcbpolicy(inp); 602 #endif /* IPSEC */ 603 inp->inp_gencnt = ++ipi->ipi_gencnt; 604 in_pcbremlists(inp); 605 sotoinpcb(so) = 0; 606 sofree(so); 607 if (inp->in6p_options) 608 m_freem(inp->in6p_options); 609 if (inp->in6p_outputopts) { 610 if (inp->in6p_outputopts->ip6po_rthdr && 611 inp->in6p_outputopts->ip6po_route.ro_rt) 612 RTFREE(inp->in6p_outputopts->ip6po_route.ro_rt); 613 if (inp->in6p_outputopts->ip6po_m) 614 (void)m_free(inp->in6p_outputopts->ip6po_m); 615 free(inp->in6p_outputopts, M_IP6OPT); 616 } 617 if (inp->in6p_route.ro_rt) 618 rtfree(inp->in6p_route.ro_rt); 619 ip6_freemoptions(inp->in6p_moptions); 620 621 /* Check and free IPv4 related resources in case of mapped addr */ 622 if (inp->inp_options) 623 (void)m_free(inp->inp_options); 624 ip_freemoptions(inp->inp_moptions); 625 626 inp->inp_vflag = 0; 627 zfreei(ipi->ipi_zone, inp); 628 } 629 630 /* 631 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was 632 * modified to match the pru_sockaddr() and pru_peeraddr() entry points 633 * in struct pr_usrreqs, so that protocols can just reference then directly 634 * without the need for a wrapper function. The socket must have a valid 635 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one 636 * except through a kernel programming error, so it is acceptable to panic 637 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap 638 * because there actually /is/ a programming error somewhere... XXX) 639 */ 640 int 641 in6_setsockaddr(so, nam) 642 struct socket *so; 643 struct sockaddr **nam; 644 { 645 int s; 646 register struct inpcb *inp; 647 register struct sockaddr_in6 *sin6; 648 649 /* 650 * Do the malloc first in case it blocks. 651 */ 652 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK); 653 bzero(sin6, sizeof *sin6); 654 sin6->sin6_family = AF_INET6; 655 sin6->sin6_len = sizeof(*sin6); 656 657 s = splnet(); 658 inp = sotoinpcb(so); 659 if (!inp) { 660 splx(s); 661 free(sin6, M_SONAME); 662 return EINVAL; 663 } 664 sin6->sin6_port = inp->inp_lport; 665 sin6->sin6_addr = inp->in6p_laddr; 666 splx(s); 667 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 668 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 669 else 670 sin6->sin6_scope_id = 0; /*XXX*/ 671 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 672 sin6->sin6_addr.s6_addr16[1] = 0; 673 674 *nam = (struct sockaddr *)sin6; 675 return 0; 676 } 677 678 int 679 in6_setpeeraddr(so, nam) 680 struct socket *so; 681 struct sockaddr **nam; 682 { 683 int s; 684 struct inpcb *inp; 685 register struct sockaddr_in6 *sin6; 686 687 /* 688 * Do the malloc first in case it blocks. 689 */ 690 MALLOC(sin6, struct sockaddr_in6 *, sizeof(*sin6), M_SONAME, M_WAITOK); 691 bzero((caddr_t)sin6, sizeof (*sin6)); 692 sin6->sin6_family = AF_INET6; 693 sin6->sin6_len = sizeof(struct sockaddr_in6); 694 695 s = splnet(); 696 inp = sotoinpcb(so); 697 if (!inp) { 698 splx(s); 699 free(sin6, M_SONAME); 700 return EINVAL; 701 } 702 sin6->sin6_port = inp->inp_fport; 703 sin6->sin6_addr = inp->in6p_faddr; 704 splx(s); 705 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 706 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 707 else 708 sin6->sin6_scope_id = 0; /*XXX*/ 709 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 710 sin6->sin6_addr.s6_addr16[1] = 0; 711 712 *nam = (struct sockaddr *)sin6; 713 return 0; 714 } 715 716 int 717 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 718 { 719 struct inpcb *inp = sotoinpcb(so); 720 int error; 721 722 if (inp == NULL) 723 return EINVAL; 724 if (inp->inp_vflag & INP_IPV4) { 725 error = in_setsockaddr(so, nam); 726 if (error == 0) 727 in6_sin_2_v4mapsin6_in_sock(nam); 728 } else 729 error = in6_setsockaddr(so, nam); 730 731 return error; 732 } 733 734 int 735 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 736 { 737 struct inpcb *inp = sotoinpcb(so); 738 int error; 739 740 if (inp == NULL) 741 return EINVAL; 742 if (inp->inp_vflag & INP_IPV4) { 743 error = in_setpeeraddr(so, nam); 744 if (error == 0) 745 in6_sin_2_v4mapsin6_in_sock(nam); 746 } else 747 error = in6_setpeeraddr(so, nam); 748 749 return error; 750 } 751 752 /* 753 * Pass some notification to all connections of a protocol 754 * associated with address dst. The local address and/or port numbers 755 * may be specified to limit the search. The "usual action" will be 756 * taken, depending on the ctlinput cmd. The caller must filter any 757 * cmds that are uninteresting (e.g., no error in the map). 758 * Call the protocol specific routine (if any) to report 759 * any errors for each matching socket. 760 * 761 * Must be called at splnet. 762 */ 763 void 764 in6_pcbnotify(head, dst, fport_arg, laddr6, lport_arg, cmd, notify) 765 struct inpcbhead *head; 766 struct sockaddr *dst; 767 u_int fport_arg, lport_arg; 768 struct in6_addr *laddr6; 769 int cmd; 770 void (*notify) __P((struct inpcb *, int)); 771 { 772 struct inpcb *inp, *ninp; 773 struct in6_addr faddr6; 774 u_short fport = fport_arg, lport = lport_arg; 775 int errno, s; 776 int do_rtchange = (notify == in6_rtchange); 777 778 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6) 779 return; 780 faddr6 = ((struct sockaddr_in6 *)dst)->sin6_addr; 781 if (IN6_IS_ADDR_UNSPECIFIED(&faddr6)) 782 return; 783 784 /* 785 * Redirects go to all references to the destination, 786 * and use in6_rtchange to invalidate the route cache. 787 * Dead host indications: also use in6_rtchange to invalidate 788 * the cache, and deliver the error to all the sockets. 789 * Otherwise, if we have knowledge of the local port and address, 790 * deliver only to that socket. 791 */ 792 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 793 fport = 0; 794 lport = 0; 795 bzero((caddr_t)laddr6, sizeof(*laddr6)); 796 797 do_rtchange = 1; 798 } 799 errno = inet6ctlerrmap[cmd]; 800 s = splnet(); 801 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) { 802 ninp = LIST_NEXT(inp, inp_list); 803 804 if ((inp->inp_vflag & INP_IPV6) == NULL) 805 continue; 806 807 if (do_rtchange) { 808 /* 809 * Since a non-connected PCB might have a cached route, 810 * we always call in6_rtchange without matching 811 * the PCB to the src/dst pair. 812 * 813 * XXX: we assume in6_rtchange does not free the PCB. 814 */ 815 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_route.ro_dst.sin6_addr, 816 &faddr6)) 817 in6_rtchange(inp, errno); 818 819 if (notify == in6_rtchange) 820 continue; /* there's nothing to do any more */ 821 } 822 823 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &faddr6) || 824 inp->inp_socket == 0 || 825 (lport && inp->inp_lport != lport) || 826 (!IN6_IS_ADDR_UNSPECIFIED(laddr6) && 827 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr6)) || 828 (fport && inp->inp_fport != fport)) 829 continue; 830 831 if (notify) 832 (*notify)(inp, errno); 833 } 834 splx(s); 835 } 836 837 /* 838 * Lookup a PCB based on the local address and port. 839 */ 840 struct inpcb * 841 in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay) 842 struct inpcbinfo *pcbinfo; 843 struct in6_addr *laddr; 844 u_int lport_arg; 845 int wild_okay; 846 { 847 register struct inpcb *inp; 848 int matchwild = 3, wildcard; 849 u_short lport = lport_arg; 850 851 if (!wild_okay) { 852 struct inpcbhead *head; 853 /* 854 * Look for an unconnected (wildcard foreign addr) PCB that 855 * matches the local address and port we're looking for. 856 */ 857 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, 858 pcbinfo->hashmask)]; 859 LIST_FOREACH(inp, head, inp_hash) { 860 if ((inp->inp_vflag & INP_IPV6) == 0) 861 continue; 862 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 863 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 864 inp->inp_lport == lport) { 865 /* 866 * Found. 867 */ 868 return (inp); 869 } 870 } 871 /* 872 * Not found. 873 */ 874 return (NULL); 875 } else { 876 struct inpcbporthead *porthash; 877 struct inpcbport *phd; 878 struct inpcb *match = NULL; 879 /* 880 * Best fit PCB lookup. 881 * 882 * First see if this local port is in use by looking on the 883 * port hash list. 884 */ 885 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport, 886 pcbinfo->porthashmask)]; 887 LIST_FOREACH(phd, porthash, phd_hash) { 888 if (phd->phd_port == lport) 889 break; 890 } 891 if (phd != NULL) { 892 /* 893 * Port is in use by one or more PCBs. Look for best 894 * fit. 895 */ 896 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 897 wildcard = 0; 898 if ((inp->inp_vflag & INP_IPV6) == 0) 899 continue; 900 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 901 wildcard++; 902 if (!IN6_IS_ADDR_UNSPECIFIED( 903 &inp->in6p_laddr)) { 904 if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 905 wildcard++; 906 else if (!IN6_ARE_ADDR_EQUAL( 907 &inp->in6p_laddr, laddr)) 908 continue; 909 } else { 910 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 911 wildcard++; 912 } 913 if (wildcard < matchwild) { 914 match = inp; 915 matchwild = wildcard; 916 if (matchwild == 0) { 917 break; 918 } 919 } 920 } 921 } 922 return (match); 923 } 924 } 925 926 /* 927 * Check for alternatives when higher level complains 928 * about service problems. For now, invalidate cached 929 * routing information. If the route was created dynamically 930 * (by a redirect), time to try a default gateway again. 931 */ 932 void 933 in6_losing(in6p) 934 struct inpcb *in6p; 935 { 936 struct rtentry *rt; 937 struct rt_addrinfo info; 938 939 if ((rt = in6p->in6p_route.ro_rt) != NULL) { 940 in6p->in6p_route.ro_rt = 0; 941 bzero((caddr_t)&info, sizeof(info)); 942 info.rti_info[RTAX_DST] = 943 (struct sockaddr *)&in6p->in6p_route.ro_dst; 944 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 945 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 946 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 947 if (rt->rt_flags & RTF_DYNAMIC) 948 (void)rtrequest(RTM_DELETE, rt_key(rt), 949 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 950 (struct rtentry **)0); 951 else 952 /* 953 * A new route can be allocated 954 * the next time output is attempted. 955 */ 956 rtfree(rt); 957 } 958 } 959 960 /* 961 * After a routing change, flush old routing 962 * and allocate a (hopefully) better one. 963 */ 964 void 965 in6_rtchange(inp, errno) 966 struct inpcb *inp; 967 int errno; 968 { 969 if (inp->in6p_route.ro_rt) { 970 rtfree(inp->in6p_route.ro_rt); 971 inp->in6p_route.ro_rt = 0; 972 /* 973 * A new route can be allocated the next time 974 * output is attempted. 975 */ 976 } 977 } 978 979 /* 980 * Lookup PCB in hash list. 981 */ 982 struct inpcb * 983 in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp) 984 struct inpcbinfo *pcbinfo; 985 struct in6_addr *faddr, *laddr; 986 u_int fport_arg, lport_arg; 987 int wildcard; 988 struct ifnet *ifp; 989 { 990 struct inpcbhead *head; 991 register struct inpcb *inp; 992 u_short fport = fport_arg, lport = lport_arg; 993 994 /* 995 * First look for an exact match. 996 */ 997 head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, 998 lport, fport, 999 pcbinfo->hashmask)]; 1000 LIST_FOREACH(inp, head, inp_hash) { 1001 if ((inp->inp_vflag & INP_IPV6) == 0) 1002 continue; 1003 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 1004 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 1005 inp->inp_fport == fport && 1006 inp->inp_lport == lport) { 1007 /* 1008 * Found. 1009 */ 1010 return (inp); 1011 } 1012 } 1013 if (wildcard) { 1014 struct inpcb *local_wild = NULL; 1015 1016 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, 1017 pcbinfo->hashmask)]; 1018 LIST_FOREACH(inp, head, inp_hash) { 1019 if ((inp->inp_vflag & INP_IPV6) == 0) 1020 continue; 1021 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 1022 inp->inp_lport == lport) { 1023 #if defined(NFAITH) && NFAITH > 0 1024 if (ifp && ifp->if_type == IFT_FAITH && 1025 (inp->inp_flags & INP_FAITH) == 0) 1026 continue; 1027 #endif 1028 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 1029 laddr)) 1030 return (inp); 1031 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1032 local_wild = inp; 1033 } 1034 } 1035 return (local_wild); 1036 } 1037 1038 /* 1039 * Not found. 1040 */ 1041 return (NULL); 1042 } 1043 1044 void 1045 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m) 1046 { 1047 struct ip6_hdr *ip; 1048 1049 ip = mtod(m, struct ip6_hdr *); 1050 bzero(sin6, sizeof(*sin6)); 1051 sin6->sin6_len = sizeof(*sin6); 1052 sin6->sin6_family = AF_INET6; 1053 sin6->sin6_addr = ip->ip6_src; 1054 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 1055 sin6->sin6_addr.s6_addr16[1] = 0; 1056 sin6->sin6_scope_id = 1057 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 1058 ? m->m_pkthdr.rcvif->if_index : 0; 1059 1060 return; 1061 } 1062