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