in6_pcb.c revision 55871
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: head/sys/netinet6/in6_pcb.c 55871 2000-01-13 05:01:27Z shin $ 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: head/sys/netinet6/in6_pcb.c 55871 2000-01-13 05:01:27Z shin $ 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 115struct in6_addr zeroin6_addr; 116 117int 118in6_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 (ip6_mapped_addr_on != 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 (ip6_mapped_addr_on != 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 362int 363in6_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 */ 455int 456in6_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_flowlable != 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 */ 508struct in6_addr * 509in6_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*/ 690int 691in6_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 703void 704in6_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 714void 715in6_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 (sotoinpcb(so) != 0) 723 key_freeso(so); 724 ipsec6_delete_pcbpolicy(inp); 725#endif /* IPSEC */ 726 inp->inp_gencnt = ++ipi->ipi_gencnt; 727 in_pcbremlists(inp); 728 sotoinpcb(so) = 0; 729 sofree(so); 730 if (inp->in6p_options) 731 m_freem(inp->in6p_options); 732 if (inp->in6p_outputopts) { 733 if (inp->in6p_outputopts->ip6po_rthdr && 734 inp->in6p_outputopts->ip6po_route.ro_rt) 735 RTFREE(inp->in6p_outputopts->ip6po_route.ro_rt); 736 if (inp->in6p_outputopts->ip6po_m) 737 (void)m_free(inp->in6p_outputopts->ip6po_m); 738 free(inp->in6p_outputopts, M_IP6OPT); 739 } 740 if (inp->in6p_route.ro_rt) 741 rtfree(inp->in6p_route.ro_rt); 742 ip6_freemoptions(inp->in6p_moptions); 743 744 /* Check and free IPv4 related resources in case of mapped addr */ 745 if (inp->inp_options) 746 (void)m_free(inp->inp_options); 747 if (inp->inp_route.ro_rt) 748 rtfree(inp->inp_route.ro_rt); 749 ip_freemoptions(inp->inp_moptions); 750 751 inp->inp_vflag = 0; 752 zfreei(ipi->ipi_zone, inp); 753} 754 755/* 756 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was 757 * modified to match the pru_sockaddr() and pru_peeraddr() entry points 758 * in struct pr_usrreqs, so that protocols can just reference then directly 759 * without the need for a wrapper function. The socket must have a valid 760 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one 761 * except through a kernel programming error, so it is acceptable to panic 762 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap 763 * because there actually /is/ a programming error somewhere... XXX) 764 */ 765int 766in6_setsockaddr(so, nam) 767 struct socket *so; 768 struct sockaddr **nam; 769{ 770 int s; 771 register struct inpcb *inp; 772 register struct sockaddr_in6 *sin6; 773 774 /* 775 * Do the malloc first in case it blocks. 776 */ 777 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK); 778 bzero(sin6, sizeof *sin6); 779 sin6->sin6_family = AF_INET6; 780 sin6->sin6_len = sizeof(*sin6); 781 782 s = splnet(); 783 inp = sotoinpcb(so); 784 if (!inp) { 785 splx(s); 786 free(sin6, M_SONAME); 787 return EINVAL; 788 } 789 sin6->sin6_port = inp->inp_lport; 790 sin6->sin6_addr = inp->in6p_laddr; 791 splx(s); 792 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 793 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 794 else 795 sin6->sin6_scope_id = 0; /*XXX*/ 796 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 797 sin6->sin6_addr.s6_addr16[1] = 0; 798 799 *nam = (struct sockaddr *)sin6; 800 return 0; 801} 802 803int 804in6_setpeeraddr(so, nam) 805 struct socket *so; 806 struct sockaddr **nam; 807{ 808 int s; 809 struct inpcb *inp; 810 register struct sockaddr_in6 *sin6; 811 812 /* 813 * Do the malloc first in case it blocks. 814 */ 815 MALLOC(sin6, struct sockaddr_in6 *, sizeof(*sin6), M_SONAME, M_WAITOK); 816 bzero((caddr_t)sin6, sizeof (*sin6)); 817 sin6->sin6_family = AF_INET6; 818 sin6->sin6_len = sizeof(struct sockaddr_in6); 819 820 s = splnet(); 821 inp = sotoinpcb(so); 822 if (!inp) { 823 splx(s); 824 free(sin6, M_SONAME); 825 return EINVAL; 826 } 827 sin6->sin6_port = inp->inp_fport; 828 sin6->sin6_addr = inp->in6p_faddr; 829 splx(s); 830 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 831 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 832 else 833 sin6->sin6_scope_id = 0; /*XXX*/ 834 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 835 sin6->sin6_addr.s6_addr16[1] = 0; 836 837 *nam = (struct sockaddr *)sin6; 838 return 0; 839} 840 841int 842in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 843{ 844 struct inpcb *inp = sotoinpcb(so); 845 int error; 846 847 if (inp == NULL) 848 return EINVAL; 849 if (inp->inp_vflag & INP_IPV4) { 850 error = in_setsockaddr(so, nam); 851 if (error == 0) 852 in6_sin_2_v4mapsin6_in_sock(nam); 853 } else 854 error = in6_setsockaddr(so, nam); 855 856 return error; 857} 858 859int 860in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 861{ 862 struct inpcb *inp = sotoinpcb(so); 863 int error; 864 865 if (inp == NULL) 866 return EINVAL; 867 if (inp->inp_vflag & INP_IPV4) { 868 error = in_setpeeraddr(so, nam); 869 if (error == 0) 870 in6_sin_2_v4mapsin6_in_sock(nam); 871 } else 872 error = in6_setpeeraddr(so, nam); 873 874 return error; 875} 876 877/* 878 * Pass some notification to all connections of a protocol 879 * associated with address dst. The local address and/or port numbers 880 * may be specified to limit the search. The "usual action" will be 881 * taken, depending on the ctlinput cmd. The caller must filter any 882 * cmds that are uninteresting (e.g., no error in the map). 883 * Call the protocol specific routine (if any) to report 884 * any errors for each matching socket. 885 * 886 * Must be called at splnet. 887 */ 888void 889in6_pcbnotify(head, dst, fport_arg, laddr6, lport_arg, cmd, notify) 890 struct inpcbhead *head; 891 struct sockaddr *dst; 892 u_int fport_arg, lport_arg; 893 struct in6_addr *laddr6; 894 int cmd; 895 void (*notify) __P((struct inpcb *, int)); 896{ 897 struct inpcb *inp, *oinp; 898 struct in6_addr faddr6; 899 u_short fport = fport_arg, lport = lport_arg; 900 int errno, s; 901 902 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6) 903 return; 904 faddr6 = ((struct sockaddr_in6 *)dst)->sin6_addr; 905 if (IN6_IS_ADDR_UNSPECIFIED(&faddr6)) 906 return; 907 908 /* 909 * Redirects go to all references to the destination, 910 * and use in_rtchange to invalidate the route cache. 911 * Dead host indications: notify all references to the destination. 912 * Otherwise, if we have knowledge of the local port and address, 913 * deliver only to that socket. 914 */ 915 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 916 fport = 0; 917 lport = 0; 918 bzero((caddr_t)laddr6, sizeof(*laddr6)); 919 if (cmd != PRC_HOSTDEAD) 920 notify = in6_rtchange; 921 } 922 errno = inet6ctlerrmap[cmd]; 923 s = splnet(); 924 for (inp = LIST_FIRST(head); inp != NULL;) { 925 if ((inp->inp_vflag & INP_IPV6) == 0) { 926 inp = LIST_NEXT(inp, inp_list); 927 continue; 928 } 929 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &faddr6) || 930 inp->inp_socket == 0 || 931 (lport && inp->inp_lport != lport) || 932 (!IN6_IS_ADDR_UNSPECIFIED(laddr6) && 933 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr6)) || 934 (fport && inp->inp_fport != fport)) { 935 inp = LIST_NEXT(inp, inp_list); 936 continue; 937 } 938 oinp = inp; 939 inp = LIST_NEXT(inp, inp_list); 940 if (notify) 941 (*notify)(oinp, errno); 942 } 943 splx(s); 944} 945 946/* 947 * Lookup a PCB based on the local address and port. 948 */ 949struct inpcb * 950in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay) 951 struct inpcbinfo *pcbinfo; 952 struct in6_addr *laddr; 953 u_int lport_arg; 954 int wild_okay; 955{ 956 register struct inpcb *inp; 957 int matchwild = 3, wildcard; 958 u_short lport = lport_arg; 959 960 if (!wild_okay) { 961 struct inpcbhead *head; 962 /* 963 * Look for an unconnected (wildcard foreign addr) PCB that 964 * matches the local address and port we're looking for. 965 */ 966 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, 967 pcbinfo->hashmask)]; 968 LIST_FOREACH(inp, head, inp_hash) { 969 if ((inp->inp_vflag & INP_IPV6) == 0) 970 continue; 971 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 972 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 973 inp->inp_lport == lport) { 974 /* 975 * Found. 976 */ 977 return (inp); 978 } 979 } 980 /* 981 * Not found. 982 */ 983 return (NULL); 984 } else { 985 struct inpcbporthead *porthash; 986 struct inpcbport *phd; 987 struct inpcb *match = NULL; 988 /* 989 * Best fit PCB lookup. 990 * 991 * First see if this local port is in use by looking on the 992 * port hash list. 993 */ 994 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport, 995 pcbinfo->porthashmask)]; 996 LIST_FOREACH(phd, porthash, phd_hash) { 997 if (phd->phd_port == lport) 998 break; 999 } 1000 if (phd != NULL) { 1001 /* 1002 * Port is in use by one or more PCBs. Look for best 1003 * fit. 1004 */ 1005 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1006 wildcard = 0; 1007 if ((inp->inp_vflag & INP_IPV6) == 0) 1008 continue; 1009 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 1010 wildcard++; 1011 if (!IN6_IS_ADDR_UNSPECIFIED( 1012 &inp->in6p_laddr)) { 1013 if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 1014 wildcard++; 1015 else if (!IN6_ARE_ADDR_EQUAL( 1016 &inp->in6p_laddr, laddr)) 1017 continue; 1018 } else { 1019 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 1020 wildcard++; 1021 } 1022 if (wildcard < matchwild) { 1023 match = inp; 1024 matchwild = wildcard; 1025 if (matchwild == 0) { 1026 break; 1027 } 1028 } 1029 } 1030 } 1031 return (match); 1032 } 1033} 1034 1035/* 1036 * Check for alternatives when higher level complains 1037 * about service problems. For now, invalidate cached 1038 * routing information. If the route was created dynamically 1039 * (by a redirect), time to try a default gateway again. 1040 */ 1041void 1042in6_losing(in6p) 1043 struct inpcb *in6p; 1044{ 1045 struct rtentry *rt; 1046 struct rt_addrinfo info; 1047 1048 if ((rt = in6p->in6p_route.ro_rt) != NULL) { 1049 in6p->in6p_route.ro_rt = 0; 1050 bzero((caddr_t)&info, sizeof(info)); 1051 info.rti_info[RTAX_DST] = 1052 (struct sockaddr *)&in6p->in6p_route.ro_dst; 1053 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1054 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1055 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 1056 if (rt->rt_flags & RTF_DYNAMIC) 1057 (void)rtrequest(RTM_DELETE, rt_key(rt), 1058 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 1059 (struct rtentry **)0); 1060 else 1061 /* 1062 * A new route can be allocated 1063 * the next time output is attempted. 1064 */ 1065 rtfree(rt); 1066 } 1067} 1068 1069/* 1070 * After a routing change, flush old routing 1071 * and allocate a (hopefully) better one. 1072 */ 1073void 1074in6_rtchange(inp, errno) 1075 struct inpcb *inp; 1076 int errno; 1077{ 1078 if (inp->in6p_route.ro_rt) { 1079 rtfree(inp->in6p_route.ro_rt); 1080 inp->in6p_route.ro_rt = 0; 1081 /* 1082 * A new route can be allocated the next time 1083 * output is attempted. 1084 */ 1085 } 1086} 1087 1088/* 1089 * Lookup PCB in hash list. 1090 */ 1091struct inpcb * 1092in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp) 1093 struct inpcbinfo *pcbinfo; 1094 struct in6_addr *faddr, *laddr; 1095 u_int fport_arg, lport_arg; 1096 int wildcard; 1097 struct ifnet *ifp; 1098{ 1099 struct inpcbhead *head; 1100 register struct inpcb *inp; 1101 u_short fport = fport_arg, lport = lport_arg; 1102 1103 /* 1104 * First look for an exact match. 1105 */ 1106 head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, 1107 lport, fport, 1108 pcbinfo->hashmask)]; 1109 LIST_FOREACH(inp, head, inp_hash) { 1110 if ((inp->inp_vflag & INP_IPV6) == 0) 1111 continue; 1112 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 1113 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 1114 inp->inp_fport == fport && 1115 inp->inp_lport == lport) { 1116 /* 1117 * Found. 1118 */ 1119 return (inp); 1120 } 1121 } 1122 if (wildcard) { 1123 struct inpcb *local_wild = NULL; 1124 1125 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, 1126 pcbinfo->hashmask)]; 1127 LIST_FOREACH(inp, head, inp_hash) { 1128 if ((inp->inp_vflag & INP_IPV6) == 0) 1129 continue; 1130 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 1131 inp->inp_lport == lport) { 1132#if defined(NFAITH) && NFAITH > 0 1133 if (ifp && ifp->if_type == IFT_FAITH && 1134 (inp->inp_flags & INP_FAITH) == 0) 1135 continue; 1136#endif 1137 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 1138 laddr)) 1139 return (inp); 1140 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1141 local_wild = inp; 1142 } 1143 } 1144 return (local_wild); 1145 } 1146 1147 /* 1148 * Not found. 1149 */ 1150 return (NULL); 1151} 1152 1153void 1154init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m) 1155{ 1156 struct ip6_hdr *ip; 1157 1158 ip = mtod(m, struct ip6_hdr *); 1159 bzero(sin6, sizeof(*sin6)); 1160 sin6->sin6_len = sizeof(*sin6); 1161 sin6->sin6_family = AF_INET6; 1162 sin6->sin6_addr = ip->ip6_src; 1163 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 1164 sin6->sin6_addr.s6_addr16[1] = 0; 1165 sin6->sin6_scope_id = 1166 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 1167 ? m->m_pkthdr.rcvif->if_index : 0; 1168 1169 return; 1170} 1171