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