1 /* $FreeBSD: src/sys/netinet6/in6_pcb.c,v 1.10.2.9 2003/01/24 05:11:35 sam Exp $ */ 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. Neither the name of the University nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 * 62 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 63 */ 64 65 #include "opt_inet.h" 66 #include "opt_inet6.h" 67 68 #include <sys/param.h> 69 #include <sys/systm.h> 70 #include <sys/malloc.h> 71 #include <sys/mbuf.h> 72 #include <sys/domain.h> 73 #include <sys/protosw.h> 74 #include <sys/socket.h> 75 #include <sys/socketvar.h> 76 #include <sys/sockio.h> 77 #include <sys/errno.h> 78 #include <sys/time.h> 79 #include <sys/proc.h> 80 #include <sys/priv.h> 81 #include <sys/jail.h> 82 83 #include <sys/msgport2.h> 84 85 #include <vm/vm_zone.h> 86 87 #include <net/if.h> 88 #include <net/if_types.h> 89 #include <net/route.h> 90 #include <net/netisr2.h> 91 92 #include <netinet/in.h> 93 #include <netinet/in_var.h> 94 #include <netinet/in_systm.h> 95 #include <netinet/ip6.h> 96 #include <netinet/ip_var.h> 97 #include <netinet6/ip6_var.h> 98 #include <netinet6/nd6.h> 99 #include <netinet/in_pcb.h> 100 #include <netinet6/in6_pcb.h> 101 102 struct in6_addr zeroin6_addr; 103 104 int 105 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td) 106 { 107 struct socket *so = inp->inp_socket; 108 struct sockaddr_in6 jsin6; 109 int error; 110 111 if (!in6_ifaddr) /* XXX broken! */ 112 return (EADDRNOTAVAIL); 113 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 114 return (EINVAL); 115 116 if (nam) { 117 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 118 struct inpcbinfo *pcbinfo; 119 struct inpcbportinfo *portinfo; 120 struct inpcbporthead *porthash; 121 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 122 struct ucred *cred = NULL; 123 struct inpcb *t; 124 u_short lport, lport_ho; 125 126 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 127 wild = 1; 128 if (td->td_proc != NULL) 129 cred = td->td_proc->p_ucred; 130 131 if (nam->sa_len != sizeof(*sin6)) 132 return (EINVAL); 133 /* 134 * family check. 135 */ 136 if (nam->sa_family != AF_INET6) 137 return (EAFNOSUPPORT); 138 139 /* Reject v4-mapped address */ 140 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 141 return (EADDRNOTAVAIL); 142 143 if (!prison_replace_wildcards(td, nam)) 144 return (EINVAL); 145 146 /* KAME hack: embed scopeid */ 147 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0) 148 return (EINVAL); 149 /* this must be cleared for ifa_ifwithaddr() */ 150 sin6->sin6_scope_id = 0; 151 152 lport = sin6->sin6_port; 153 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 154 /* 155 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 156 * allow compepte duplication of binding if 157 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 158 * and a multicast address is bound on both 159 * new and duplicated sockets. 160 */ 161 if (so->so_options & SO_REUSEADDR) 162 reuseport = SO_REUSEADDR|SO_REUSEPORT; 163 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 164 struct ifaddr *ia = NULL; 165 166 sin6->sin6_port = 0; /* yech... */ 167 if (!prison_replace_wildcards(td, (struct sockaddr *)sin6)) { 168 sin6->sin6_addr = kin6addr_any; 169 return (EINVAL); 170 } 171 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == NULL) 172 return (EADDRNOTAVAIL); 173 174 /* 175 * XXX: bind to an anycast address might accidentally 176 * cause sending a packet with anycast source address. 177 * We should allow to bind to a deprecated address, since 178 * the application dares to use it. 179 */ 180 if (ia && 181 ((struct in6_ifaddr *)ia)->ia6_flags & 182 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) 183 return (EADDRNOTAVAIL); 184 } 185 186 inp->in6p_laddr = sin6->sin6_addr; 187 188 if (lport == 0) 189 goto auto_select; 190 lport_ho = ntohs(lport); 191 192 /* GROSS */ 193 if (lport_ho < IPV6PORT_RESERVED && cred && 194 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0)) { 195 inp->in6p_laddr = kin6addr_any; 196 return (EACCES); 197 } 198 199 /* 200 * Locate the proper portinfo based on lport 201 */ 202 pcbinfo = inp->inp_pcbinfo; 203 portinfo = 204 &pcbinfo->portinfo[lport_ho % pcbinfo->portinfo_cnt]; 205 KKASSERT((lport_ho % pcbinfo->portinfo_cnt) == 206 portinfo->offset); 207 208 /* 209 * This has to be atomic. If the porthash is shared across 210 * multiple protocol threads (aka tcp) then the token must 211 * be held. 212 */ 213 porthash = in_pcbporthash_head(portinfo, lport); 214 GET_PORTHASH_TOKEN(porthash); 215 216 if (so->so_cred->cr_uid != 0 && 217 !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 218 t = in6_pcblookup_local(porthash, 219 &sin6->sin6_addr, lport, INPLOOKUP_WILDCARD, cred); 220 if (t && 221 (so->so_cred->cr_uid != 222 t->inp_socket->so_cred->cr_uid)) { 223 inp->in6p_laddr = kin6addr_any; 224 error = EADDRINUSE; 225 goto done; 226 } 227 } 228 if (cred && cred->cr_prison && 229 !prison_replace_wildcards(td, nam)) { 230 inp->in6p_laddr = kin6addr_any; 231 error = EADDRNOTAVAIL; 232 goto done; 233 } 234 t = in6_pcblookup_local(porthash, &sin6->sin6_addr, lport, 235 wild, cred); 236 if (t && (reuseport & t->inp_socket->so_options) == 0) { 237 inp->in6p_laddr = kin6addr_any; 238 error = EADDRINUSE; 239 goto done; 240 } 241 242 inp->inp_lport = lport; 243 in_pcbinsporthash(porthash, inp); 244 error = 0; 245 done: 246 REL_PORTHASH_TOKEN(porthash); 247 return (error); 248 } else { 249 auto_select: 250 jsin6.sin6_addr = inp->in6p_laddr; 251 jsin6.sin6_family = AF_INET6; 252 if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin6)) { 253 inp->in6p_laddr = kin6addr_any; 254 inp->inp_lport = 0; 255 return (EINVAL); 256 } 257 258 return in6_pcbsetlport(&inp->in6p_laddr, inp, td); 259 } 260 } 261 262 /* 263 * Transform old in6_pcbconnect() into an inner subroutine for new 264 * in6_pcbconnect(): Do some validity-checking on the remote 265 * address (in mbuf 'nam') and then determine local host address 266 * (i.e., which interface) to use to access that remote host. 267 * 268 * This preserves definition of in6_pcbconnect(), while supporting a 269 * slightly different version for T/TCP. (This is more than 270 * a bit of a kludge, but cleaning up the internal interfaces would 271 * have forced minor changes in every protocol). 272 */ 273 274 int 275 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam, 276 struct in6_addr **plocal_addr6, struct thread *td) 277 { 278 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 279 struct ifnet *ifp = NULL; 280 int error = 0; 281 282 if (nam->sa_len != sizeof (*sin6)) 283 return (EINVAL); 284 if (sin6->sin6_family != AF_INET6) 285 return (EAFNOSUPPORT); 286 if (sin6->sin6_port == 0) 287 return (EADDRNOTAVAIL); 288 289 /* KAME hack: embed scopeid */ 290 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0) 291 return EINVAL; 292 293 if (in6_ifaddr) { 294 /* 295 * If the destination address is UNSPECIFIED addr, 296 * use the loopback addr, e.g ::1. 297 */ 298 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 299 sin6->sin6_addr = kin6addr_loopback; 300 } 301 { 302 /* 303 * XXX: in6_selectsrc might replace the bound local address 304 * with the address specified by setsockopt(IPV6_PKTINFO). 305 * Is it the intended behavior? 306 */ 307 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts, 308 inp->in6p_moptions, 309 &inp->in6p_route, 310 &inp->in6p_laddr, &error, td); 311 if (*plocal_addr6 == NULL) { 312 if (error == 0) 313 error = EADDRNOTAVAIL; 314 return (error); 315 } 316 /* 317 * Don't do pcblookup call here; return interface in 318 * plocal_addr6 319 * and exit to caller, that will do the lookup. 320 */ 321 } 322 323 if (inp->in6p_route.ro_rt) 324 ifp = inp->in6p_route.ro_rt->rt_ifp; 325 326 return (0); 327 } 328 329 /* 330 * Outer subroutine: 331 * Connect from a socket to a specified address. 332 * Both address and port must be specified in argument sin. 333 * If don't have a local address for this socket yet, 334 * then pick one. 335 */ 336 int 337 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td) 338 { 339 struct in6_addr *addr6; 340 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 341 int error; 342 343 /* Reject v4-mapped address */ 344 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 345 return EADDRNOTAVAIL; 346 347 /* 348 * Call inner routine, to assign local interface address. 349 * in6_pcbladdr() may automatically fill in sin6_scope_id. 350 */ 351 if ((error = in6_pcbladdr(inp, nam, &addr6, td)) != 0) 352 return (error); 353 354 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, 355 sin6->sin6_port, 356 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 357 ? addr6 : &inp->in6p_laddr, 358 inp->inp_lport, 0, NULL) != NULL) { 359 return (EADDRINUSE); 360 } 361 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 362 if (inp->inp_lport == 0) { 363 error = in6_pcbbind(inp, NULL, td); 364 if (error) 365 return (error); 366 } 367 inp->in6p_laddr = *addr6; 368 } 369 inp->in6p_faddr = sin6->sin6_addr; 370 inp->inp_fport = sin6->sin6_port; 371 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 372 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 373 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 374 inp->in6p_flowinfo |= 375 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK); 376 377 in_pcbinsconnhash(inp); 378 return (0); 379 } 380 381 void 382 in6_pcbdisconnect(struct inpcb *inp) 383 { 384 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr)); 385 inp->inp_fport = 0; 386 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 387 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 388 in_pcbremconnhash(inp); 389 if (inp->inp_socket->so_state & SS_NOFDREF) 390 in6_pcbdetach(inp); 391 } 392 393 void 394 in6_pcbdetach(struct inpcb *inp) 395 { 396 struct socket *so = inp->inp_socket; 397 struct inpcbinfo *ipi = inp->inp_pcbinfo; 398 399 inp->inp_gencnt = ++ipi->ipi_gencnt; 400 in_pcbremlists(inp); 401 so->so_pcb = NULL; 402 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0); 403 sofree(so); /* remove pcb ref */ 404 405 if (inp->in6p_options) 406 m_freem(inp->in6p_options); 407 ip6_freepcbopts(inp->in6p_outputopts); 408 ip6_freemoptions(inp->in6p_moptions); 409 if (inp->in6p_route.ro_rt) 410 rtfree(inp->in6p_route.ro_rt); 411 /* Check and free IPv4 related resources in case of mapped addr */ 412 if (inp->inp_options) 413 m_free(inp->inp_options); 414 ip_freemoptions(inp->inp_moptions); 415 416 kfree(inp, M_PCB); 417 } 418 419 /* 420 * The socket may have an invalid PCB, i.e. NULL. For example, a TCP 421 * socket received RST. 422 */ 423 static int 424 in6_setsockaddr(struct socket *so, struct sockaddr **nam) 425 { 426 struct inpcb *inp; 427 struct sockaddr_in6 *sin6; 428 429 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr")); 430 inp = so->so_pcb; 431 if (!inp) 432 return EINVAL; 433 434 sin6 = kmalloc(sizeof *sin6, M_SONAME, M_WAITOK | M_ZERO); 435 sin6->sin6_family = AF_INET6; 436 sin6->sin6_len = sizeof(*sin6); 437 sin6->sin6_port = inp->inp_lport; 438 sin6->sin6_addr = inp->in6p_laddr; 439 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 440 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 441 else 442 sin6->sin6_scope_id = 0; /*XXX*/ 443 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 444 sin6->sin6_addr.s6_addr16[1] = 0; 445 446 *nam = (struct sockaddr *)sin6; 447 return 0; 448 } 449 450 void 451 in6_setsockaddr_dispatch(netmsg_t msg) 452 { 453 int error; 454 455 error = in6_setsockaddr(msg->sockaddr.base.nm_so, msg->sockaddr.nm_nam); 456 lwkt_replymsg(&msg->sockaddr.base.lmsg, error); 457 } 458 459 void 460 in6_setpeeraddr_dispatch(netmsg_t msg) 461 { 462 int error; 463 464 error = in6_setpeeraddr(msg->peeraddr.base.nm_so, msg->peeraddr.nm_nam); 465 lwkt_replymsg(&msg->peeraddr.base.lmsg, error); 466 } 467 468 /* 469 * The socket may have an invalid PCB, i.e. NULL. For example, a TCP 470 * socket received RST. 471 */ 472 int 473 in6_setpeeraddr(struct socket *so, struct sockaddr **nam) 474 { 475 struct inpcb *inp; 476 struct sockaddr_in6 *sin6; 477 478 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr")); 479 inp = so->so_pcb; 480 if (!inp) 481 return EINVAL; 482 483 sin6 = kmalloc(sizeof(*sin6), M_SONAME, M_WAITOK | M_ZERO); 484 sin6->sin6_family = AF_INET6; 485 sin6->sin6_len = sizeof(struct sockaddr_in6); 486 sin6->sin6_port = inp->inp_fport; 487 sin6->sin6_addr = inp->in6p_faddr; 488 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 489 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 490 else 491 sin6->sin6_scope_id = 0; /*XXX*/ 492 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 493 sin6->sin6_addr.s6_addr16[1] = 0; 494 495 *nam = (struct sockaddr *)sin6; 496 return 0; 497 } 498 499 /* 500 * Pass some notification to all connections of a protocol 501 * associated with address dst. The local address and/or port numbers 502 * may be specified to limit the search. The "usual action" will be 503 * taken, depending on the ctlinput cmd. The caller must filter any 504 * cmds that are uninteresting (e.g., no error in the map). 505 * Call the protocol specific routine (if any) to report 506 * any errors for each matching socket. 507 */ 508 void 509 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst, in_port_t fport, 510 const struct sockaddr *src, in_port_t lport, int cmd, int arg, 511 inp_notify_t notify) 512 { 513 struct inpcb *inp, *marker; 514 struct sockaddr_in6 sa6_src, *sa6_dst; 515 u_int32_t flowinfo; 516 517 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6) 518 return; 519 520 sa6_dst = (struct sockaddr_in6 *)dst; 521 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 522 return; 523 524 /* 525 * note that src can be NULL when we get notify by local fragmentation. 526 */ 527 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src; 528 flowinfo = sa6_src.sin6_flowinfo; 529 530 /* 531 * Redirects go to all references to the destination, 532 * and use in6_rtchange to invalidate the route cache. 533 * Dead host indications: also use in6_rtchange to invalidate 534 * the cache, and deliver the error to all the sockets. 535 * Otherwise, if we have knowledge of the local port and address, 536 * deliver only to that socket. 537 */ 538 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 539 fport = 0; 540 lport = 0; 541 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 542 543 if (cmd != PRC_HOSTDEAD) 544 notify = in6_rtchange; 545 } 546 if (cmd != PRC_MSGSIZE) 547 arg = inet6ctlerrmap[cmd]; 548 549 marker = in_pcbmarker(); 550 551 GET_PCBINFO_TOKEN(pcbinfo); 552 553 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list); 554 while ((inp = LIST_NEXT(marker, inp_list)) != NULL) { 555 LIST_REMOVE(marker, inp_list); 556 LIST_INSERT_AFTER(inp, marker, inp_list); 557 558 if (inp->inp_flags & INP_PLACEMARKER) 559 continue; 560 561 if (!INP_ISIPV6(inp)) 562 continue; 563 /* 564 * If the error designates a new path MTU for a destination 565 * and the application (associated with this socket) wanted to 566 * know the value, notify. Note that we notify for all 567 * disconnected sockets if the corresponding application 568 * wanted. This is because some UDP applications keep sending 569 * sockets disconnected. 570 * XXX: should we avoid to notify the value to TCP sockets? 571 */ 572 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 && 573 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 574 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) { 575 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst, &arg); 576 } 577 578 /* 579 * Detect if we should notify the error. If no source and 580 * destination ports are specifed, but non-zero flowinfo and 581 * local address match, notify the error. This is the case 582 * when the error is delivered with an encrypted buffer 583 * by ESP. Otherwise, just compare addresses and ports 584 * as usual. 585 */ 586 if (lport == 0 && fport == 0 && flowinfo && 587 inp->inp_socket != NULL && 588 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && 589 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 590 goto do_notify; 591 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 592 &sa6_dst->sin6_addr) || 593 inp->inp_socket == 0 || 594 (lport && inp->inp_lport != lport) || 595 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 596 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 597 &sa6_src.sin6_addr)) || 598 (fport && inp->inp_fport != fport)) 599 continue; 600 601 do_notify: 602 if (notify) 603 (*notify)(inp, arg); 604 } 605 LIST_REMOVE(marker, inp_list); 606 607 REL_PCBINFO_TOKEN(pcbinfo); 608 } 609 610 /* 611 * Lookup a PCB based on the local address and port. 612 */ 613 struct inpcb * 614 in6_pcblookup_local(struct inpcbporthead *porthash, 615 const struct in6_addr *laddr, u_int lport_arg, int wild_okay, 616 struct ucred *cred) 617 { 618 struct inpcb *inp; 619 int matchwild = 3, wildcard; 620 u_short lport = lport_arg; 621 struct inpcbport *phd; 622 struct inpcb *match = NULL; 623 624 /* 625 * If the porthashbase is shared across several cpus, it must 626 * have been locked. 627 */ 628 ASSERT_PORTHASH_TOKEN_HELD(porthash); 629 630 /* 631 * Best fit PCB lookup. 632 * 633 * First see if this local port is in use by looking on the 634 * port hash list. 635 */ 636 LIST_FOREACH(phd, porthash, phd_hash) { 637 if (phd->phd_port == lport) 638 break; 639 } 640 641 if (phd != NULL) { 642 /* 643 * Port is in use by one or more PCBs. Look for best 644 * fit. 645 */ 646 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 647 wildcard = 0; 648 if (!INP_ISIPV6(inp)) 649 continue; 650 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 651 wildcard++; 652 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 653 if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 654 wildcard++; 655 else if (!IN6_ARE_ADDR_EQUAL( 656 &inp->in6p_laddr, laddr)) 657 continue; 658 } else { 659 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 660 wildcard++; 661 } 662 if (wildcard && !wild_okay) 663 continue; 664 if (wildcard < matchwild && 665 (cred == NULL || 666 cred->cr_prison == 667 inp->inp_socket->so_cred->cr_prison)) { 668 match = inp; 669 matchwild = wildcard; 670 if (wildcard == 0) 671 break; 672 else 673 matchwild = wildcard; 674 } 675 } 676 } 677 return (match); 678 } 679 680 void 681 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 682 { 683 struct in6pcb *in6p, *marker; 684 struct ip6_moptions *im6o; 685 struct in6_multi_mship *imm, *nimm; 686 687 /* 688 * We only need to make sure that we are in netisr0, where all 689 * multicast operation happen. We could check inpcbinfo which 690 * does not belong to netisr0 by holding the inpcbinfo's token. 691 * In this case, the pcbinfo must be able to be shared, i.e. 692 * pcbinfo->infotoken is not NULL. 693 */ 694 ASSERT_NETISR0; 695 KASSERT(pcbinfo->cpu == 0 || pcbinfo->infotoken != NULL, 696 ("pcbinfo could not be shared")); 697 698 /* 699 * Get a marker for the current netisr (netisr0). 700 * 701 * It is possible that the multicast address deletion blocks, 702 * which could cause temporary token releasing. So we use 703 * inpcb marker here to get a coherent view of the inpcb list. 704 * 705 * While, on the other hand, moptions are only added and deleted 706 * in netisr0, so we would not see staled moption or miss moption 707 * even if the token was released due to the blocking multicast 708 * address deletion. 709 */ 710 marker = in_pcbmarker(); 711 712 GET_PCBINFO_TOKEN(pcbinfo); 713 714 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list); 715 while ((in6p = LIST_NEXT(marker, inp_list)) != NULL) { 716 LIST_REMOVE(marker, inp_list); 717 LIST_INSERT_AFTER(in6p, marker, inp_list); 718 719 if (in6p->in6p_flags & INP_PLACEMARKER) 720 continue; 721 im6o = in6p->in6p_moptions; 722 if (INP_ISIPV6(in6p) && im6o) { 723 /* 724 * Unselect the outgoing interface if it is being 725 * detached. 726 */ 727 if (im6o->im6o_multicast_ifp == ifp) 728 im6o->im6o_multicast_ifp = NULL; 729 730 /* 731 * Drop multicast group membership if we joined 732 * through the interface being detached. 733 * XXX controversial - is it really legal for kernel 734 * to force this? 735 */ 736 for (imm = im6o->im6o_memberships.lh_first; 737 imm != NULL; imm = nimm) { 738 nimm = imm->i6mm_chain.le_next; 739 if (imm->i6mm_maddr->in6m_ifp == ifp) { 740 LIST_REMOVE(imm, i6mm_chain); 741 in6_delmulti(imm->i6mm_maddr); 742 kfree(imm, M_IPMADDR); 743 } 744 } 745 } 746 } 747 LIST_REMOVE(marker, inp_list); 748 749 REL_PCBINFO_TOKEN(pcbinfo); 750 } 751 752 /* 753 * Check for alternatives when higher level complains 754 * about service problems. For now, invalidate cached 755 * routing information. If the route was created dynamically 756 * (by a redirect), time to try a default gateway again. 757 */ 758 void 759 in6_losing(struct inpcb *in6p) 760 { 761 struct rtentry *rt; 762 struct rt_addrinfo info; 763 764 if ((rt = in6p->in6p_route.ro_rt) != NULL) { 765 bzero((caddr_t)&info, sizeof(info)); 766 info.rti_flags = rt->rt_flags; 767 info.rti_info[RTAX_DST] = rt_key(rt); 768 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 769 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 770 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 771 if (rt->rt_flags & RTF_DYNAMIC) { 772 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 773 rt_mask(rt), rt->rt_flags, NULL); 774 } 775 in6p->in6p_route.ro_rt = NULL; 776 rtfree(rt); 777 /* 778 * A new route can be allocated 779 * the next time output is attempted. 780 */ 781 } 782 } 783 784 /* 785 * After a routing change, flush old routing 786 * and allocate a (hopefully) better one. 787 */ 788 void 789 in6_rtchange(struct inpcb *inp, int error) 790 { 791 if (inp->in6p_route.ro_rt) { 792 rtfree(inp->in6p_route.ro_rt); 793 inp->in6p_route.ro_rt = 0; 794 /* 795 * A new route can be allocated the next time 796 * output is attempted. 797 */ 798 } 799 } 800 801 /* 802 * Lookup PCB in hash list. 803 */ 804 struct inpcb * 805 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 806 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, 807 int wildcard, struct ifnet *ifp) 808 { 809 struct inpcbhead *head; 810 struct inpcb *inp; 811 struct inpcb *jinp = NULL; 812 u_short fport = fport_arg, lport = lport_arg; 813 814 /* 815 * First look for an exact match. 816 */ 817 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr->s6_addr32[3] /* XXX */, 818 fport, 819 laddr->s6_addr32[3], /* XXX JH */ 820 lport, 821 pcbinfo->hashmask)]; 822 LIST_FOREACH(inp, head, inp_hash) { 823 if (!INP_ISIPV6(inp)) 824 continue; 825 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 826 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 827 inp->inp_fport == fport && 828 inp->inp_lport == lport) { 829 /* 830 * Found. 831 */ 832 if (inp->inp_socket == NULL || 833 inp->inp_socket->so_cred->cr_prison == NULL) { 834 return (inp); 835 } else { 836 if (jinp == NULL) 837 jinp = inp; 838 } 839 } 840 } 841 if (jinp != NULL) 842 return(jinp); 843 844 if (wildcard) { 845 struct inpcontainerhead *chead; 846 struct inpcontainer *ic; 847 struct inpcb *local_wild = NULL; 848 struct inpcb *jinp_wild = NULL; 849 struct sockaddr_in6 jsin6; 850 struct ucred *cred; 851 852 /* 853 * Order of socket selection: 854 * 1. non-jailed, non-wild. 855 * 2. non-jailed, wild. 856 * 3. jailed, non-wild. 857 * 4. jailed, wild. 858 */ 859 jsin6.sin6_family = AF_INET6; 860 chead = &pcbinfo->wildcardhashbase[INP_PCBWILDCARDHASH(lport, 861 pcbinfo->wildcardhashmask)]; 862 863 GET_PCBINFO_TOKEN(pcbinfo); 864 LIST_FOREACH(ic, chead, ic_list) { 865 inp = ic->ic_inp; 866 if (inp->inp_flags & INP_PLACEMARKER) 867 continue; 868 869 if (!INP_ISIPV6(inp)) 870 continue; 871 if (inp->inp_socket != NULL) 872 cred = inp->inp_socket->so_cred; 873 else 874 cred = NULL; 875 876 if (cred != NULL && jailed(cred)) { 877 if (jinp != NULL) { 878 continue; 879 } else { 880 jsin6.sin6_addr = *laddr; 881 if (!jailed_ip(cred->cr_prison, 882 (struct sockaddr *)&jsin6)) 883 continue; 884 } 885 } 886 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 887 inp->inp_lport == lport) { 888 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 889 laddr)) { 890 if (cred != NULL && jailed(cred)) { 891 jinp = inp; 892 } else { 893 REL_PCBINFO_TOKEN(pcbinfo); 894 return (inp); 895 } 896 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 897 if (cred != NULL && jailed(cred)) 898 jinp_wild = inp; 899 else 900 local_wild = inp; 901 } 902 } 903 } 904 REL_PCBINFO_TOKEN(pcbinfo); 905 906 if (local_wild != NULL) 907 return (local_wild); 908 if (jinp != NULL) 909 return (jinp); 910 return (jinp_wild); 911 } 912 913 /* 914 * Not found. 915 */ 916 return (NULL); 917 } 918 919 void 920 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m) 921 { 922 struct ip6_hdr *ip; 923 924 ip = mtod(m, struct ip6_hdr *); 925 bzero(sin6, sizeof(*sin6)); 926 sin6->sin6_len = sizeof(*sin6); 927 sin6->sin6_family = AF_INET6; 928 sin6->sin6_addr = ip->ip6_src; 929 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 930 sin6->sin6_addr.s6_addr16[1] = 0; 931 sin6->sin6_scope_id = 932 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 933 ? m->m_pkthdr.rcvif->if_index : 0; 934 935 return; 936 } 937 938 void 939 in6_savefaddr(struct socket *so, const struct sockaddr *faddr) 940 { 941 struct sockaddr_in6 *sin6; 942 943 KASSERT(faddr->sa_family == AF_INET6, 944 ("not AF_INET6 faddr %d", faddr->sa_family)); 945 946 sin6 = kmalloc(sizeof(*sin6), M_SONAME, M_WAITOK | M_ZERO); 947 sin6->sin6_family = AF_INET6; 948 sin6->sin6_len = sizeof(*sin6); 949 950 sin6->sin6_port = ((const struct sockaddr_in6 *)faddr)->sin6_port; 951 sin6->sin6_addr = ((const struct sockaddr_in6 *)faddr)->sin6_addr; 952 953 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 954 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 955 else 956 sin6->sin6_scope_id = 0; /*XXX*/ 957 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 958 sin6->sin6_addr.s6_addr16[1] = 0; 959 960 so->so_faddr = (struct sockaddr *)sin6; 961 } 962