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