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