in6_pcb.c revision 132714
1/* $FreeBSD: head/sys/netinet6/in6_pcb.c 132714 2004-07-27 23:44:03Z rwatson $ */ 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 * 4. 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#include "opt_ipsec.h" 68#include "opt_random_ip_id.h" 69 70#include <sys/param.h> 71#include <sys/systm.h> 72#include <sys/malloc.h> 73#include <sys/mbuf.h> 74#include <sys/domain.h> 75#include <sys/protosw.h> 76#include <sys/socket.h> 77#include <sys/socketvar.h> 78#include <sys/sockio.h> 79#include <sys/errno.h> 80#include <sys/time.h> 81#include <sys/proc.h> 82#include <sys/jail.h> 83 84#include <vm/uma.h> 85 86#include <net/if.h> 87#include <net/if_types.h> 88#include <net/route.h> 89 90#include <netinet/in.h> 91#include <netinet/in_var.h> 92#include <netinet/in_systm.h> 93#include <netinet/tcp_var.h> 94#include <netinet/ip6.h> 95#include <netinet/ip_var.h> 96#include <netinet6/ip6_var.h> 97#include <netinet6/nd6.h> 98#include <netinet/in_pcb.h> 99#include <netinet6/in6_pcb.h> 100 101#ifdef IPSEC 102#include <netinet6/ipsec.h> 103#ifdef INET6 104#include <netinet6/ipsec6.h> 105#endif 106#include <netinet6/ah.h> 107#ifdef INET6 108#include <netinet6/ah6.h> 109#endif 110#include <netkey/key.h> 111#endif /* IPSEC */ 112 113#ifdef FAST_IPSEC 114#include <netipsec/ipsec.h> 115#include <netipsec/ipsec6.h> 116#include <netipsec/key.h> 117#endif /* FAST_IPSEC */ 118 119struct in6_addr zeroin6_addr; 120 121int 122in6_pcbbind(inp, nam, cred) 123 register struct inpcb *inp; 124 struct sockaddr *nam; 125 struct ucred *cred; 126{ 127 struct socket *so = inp->inp_socket; 128 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 129 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 130 u_short lport = 0; 131 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 132 133 INP_INFO_WLOCK_ASSERT(pcbinfo); 134 INP_LOCK_ASSERT(inp); 135 136 if (!in6_ifaddr) /* XXX broken! */ 137 return (EADDRNOTAVAIL); 138 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 139 return (EINVAL); 140 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 141 wild = 1; 142 if (nam) { 143 sin6 = (struct sockaddr_in6 *)nam; 144 if (nam->sa_len != sizeof(*sin6)) 145 return (EINVAL); 146 /* 147 * family check. 148 */ 149 if (nam->sa_family != AF_INET6) 150 return (EAFNOSUPPORT); 151 152 /* KAME hack: embed scopeid */ 153 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0) 154 return EINVAL; 155 /* this must be cleared for ifa_ifwithaddr() */ 156 sin6->sin6_scope_id = 0; 157 158 lport = sin6->sin6_port; 159 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 160 /* 161 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 162 * allow compepte duplication of binding if 163 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 164 * and a multicast address is bound on both 165 * new and duplicated sockets. 166 */ 167 if (so->so_options & SO_REUSEADDR) 168 reuseport = SO_REUSEADDR|SO_REUSEPORT; 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 dares 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 && 193 suser_cred(cred, SUSER_ALLOWJAIL)) 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 && 201 ((t->inp_vflag & INP_TIMEWAIT) == 0) && 202 (so->so_type != SOCK_STREAM || 203 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) && 204 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 205 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || 206 (t->inp_socket->so_options & SO_REUSEPORT) 207 == 0) && (so->so_cred->cr_uid != 208 t->inp_socket->so_cred->cr_uid)) 209 return (EADDRINUSE); 210 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 211 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 212 struct sockaddr_in sin; 213 214 in6_sin6_2_sin(&sin, sin6); 215 t = in_pcblookup_local(pcbinfo, 216 sin.sin_addr, lport, 217 INPLOOKUP_WILDCARD); 218 if (t && 219 ((t->inp_vflag & 220 INP_TIMEWAIT) == 0) && 221 (so->so_type != SOCK_STREAM || 222 ntohl(t->inp_faddr.s_addr) == 223 INADDR_ANY) && 224 (so->so_cred->cr_uid != 225 t->inp_socket->so_cred->cr_uid) && 226 (ntohl(t->inp_laddr.s_addr) != 227 INADDR_ANY || 228 INP_SOCKAF(so) == 229 INP_SOCKAF(t->inp_socket))) 230 return (EADDRINUSE); 231 } 232 } 233 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, 234 lport, wild); 235 if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ? 236 intotw(t)->tw_so_options : 237 t->inp_socket->so_options)) == 0) 238 return (EADDRINUSE); 239 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 240 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 241 struct sockaddr_in sin; 242 243 in6_sin6_2_sin(&sin, sin6); 244 t = in_pcblookup_local(pcbinfo, sin.sin_addr, 245 lport, wild); 246 if (t && t->inp_vflag & INP_TIMEWAIT) { 247 if ((reuseport & 248 intotw(t)->tw_so_options) == 0 && 249 (ntohl(t->inp_laddr.s_addr) != 250 INADDR_ANY || ((inp->inp_vflag & 251 INP_IPV6PROTO) == 252 (t->inp_vflag & INP_IPV6PROTO)))) 253 return (EADDRINUSE); 254 } 255 else if (t && 256 (reuseport & t->inp_socket->so_options) 257 == 0 && (ntohl(t->inp_laddr.s_addr) != 258 INADDR_ANY || INP_SOCKAF(so) == 259 INP_SOCKAF(t->inp_socket))) 260 return (EADDRINUSE); 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, cred)) != 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 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 311 INP_LOCK_ASSERT(inp); 312 313 /* KAME hack: embed scopeid */ 314 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0) 315 return EINVAL; 316 317 if (in6_ifaddr) { 318 /* 319 * If the destination address is UNSPECIFIED addr, 320 * use the loopback addr, e.g ::1. 321 */ 322 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 323 sin6->sin6_addr = in6addr_loopback; 324 } 325 { 326 /* 327 * XXX: in6_selectsrc might replace the bound local address 328 * with the address specified by setsockopt(IPV6_PKTINFO). 329 * Is it the intended behavior? 330 */ 331 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts, 332 inp->in6p_moptions, NULL, 333 &inp->in6p_laddr, &error); 334 if (*plocal_addr6 == 0) { 335 if (error == 0) 336 error = EADDRNOTAVAIL; 337 return (error); 338 } 339 /* 340 * Don't do pcblookup call here; return interface in 341 * plocal_addr6 342 * and exit to caller, that will do the lookup. 343 */ 344 } 345 return (0); 346} 347 348/* 349 * Outer subroutine: 350 * Connect from a socket to a specified address. 351 * Both address and port must be specified in argument sin. 352 * If don't have a local address for this socket yet, 353 * then pick one. 354 */ 355int 356in6_pcbconnect(inp, nam, cred) 357 register struct inpcb *inp; 358 struct sockaddr *nam; 359 struct ucred *cred; 360{ 361 struct in6_addr *addr6; 362 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 363 int error; 364 365 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 366 INP_LOCK_ASSERT(inp); 367 368 /* 369 * Call inner routine, to assign local interface address. 370 * in6_pcbladdr() may automatically fill in sin6_scope_id. 371 */ 372 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0) 373 return (error); 374 375 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, 376 sin6->sin6_port, 377 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 378 ? addr6 : &inp->in6p_laddr, 379 inp->inp_lport, 0, NULL) != NULL) { 380 return (EADDRINUSE); 381 } 382 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 383 if (inp->inp_lport == 0) { 384 error = in6_pcbbind(inp, (struct sockaddr *)0, cred); 385 if (error) 386 return (error); 387 } 388 inp->in6p_laddr = *addr6; 389 } 390 inp->in6p_faddr = sin6->sin6_addr; 391 inp->inp_fport = sin6->sin6_port; 392 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 393 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 394 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 395 inp->in6p_flowinfo |= 396#ifdef RANDOM_IP_ID 397 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 398#else 399 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK); 400#endif 401 402 in_pcbrehash(inp); 403#ifdef IPSEC 404 if (inp->inp_socket->so_type == SOCK_STREAM) 405 ipsec_pcbconn(inp->inp_sp); 406#endif 407 return (0); 408} 409 410void 411in6_pcbdisconnect(inp) 412 struct inpcb *inp; 413{ 414 415 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 416 INP_LOCK_ASSERT(inp); 417 418 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr)); 419 inp->inp_fport = 0; 420 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 421 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 422 in_pcbrehash(inp); 423#ifdef IPSEC 424 ipsec_pcbdisconn(inp->inp_sp); 425#endif 426 if (inp->inp_socket->so_state & SS_NOFDREF) 427 in6_pcbdetach(inp); 428} 429 430void 431in6_pcbdetach(inp) 432 struct inpcb *inp; 433{ 434 struct socket *so = inp->inp_socket; 435 struct inpcbinfo *ipi = inp->inp_pcbinfo; 436 437 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 438 INP_LOCK_ASSERT(inp); 439 440#if defined(IPSEC) || defined(FAST_IPSEC) 441 if (inp->in6p_sp != NULL) 442 ipsec6_delete_pcbpolicy(inp); 443#endif /* IPSEC */ 444 inp->inp_gencnt = ++ipi->ipi_gencnt; 445 in_pcbremlists(inp); 446 447 if (so) { 448 SOCK_LOCK(so); 449 so->so_pcb = NULL; 450 sotryfree(so); 451 } 452 453 ip6_freepcbopts(inp->in6p_outputopts); 454 ip6_freemoptions(inp->in6p_moptions); 455 /* Check and free IPv4 related resources in case of mapped addr */ 456 if (inp->inp_options) 457 (void)m_free(inp->inp_options); 458 ip_freemoptions(inp->inp_moptions); 459 inp->inp_vflag = 0; 460 INP_LOCK_DESTROY(inp); 461 uma_zfree(ipi->ipi_zone, inp); 462} 463 464struct sockaddr * 465in6_sockaddr(port, addr_p) 466 in_port_t port; 467 struct in6_addr *addr_p; 468{ 469 struct sockaddr_in6 *sin6; 470 471 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK); 472 bzero(sin6, sizeof *sin6); 473 sin6->sin6_family = AF_INET6; 474 sin6->sin6_len = sizeof(*sin6); 475 sin6->sin6_port = port; 476 sin6->sin6_addr = *addr_p; 477 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 478 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 479 else 480 sin6->sin6_scope_id = 0; /*XXX*/ 481 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 482 sin6->sin6_addr.s6_addr16[1] = 0; 483 484 return (struct sockaddr *)sin6; 485} 486 487struct sockaddr * 488in6_v4mapsin6_sockaddr(port, addr_p) 489 in_port_t port; 490 struct in_addr *addr_p; 491{ 492 struct sockaddr_in sin; 493 struct sockaddr_in6 *sin6_p; 494 495 bzero(&sin, sizeof sin); 496 sin.sin_family = AF_INET; 497 sin.sin_len = sizeof(sin); 498 sin.sin_port = port; 499 sin.sin_addr = *addr_p; 500 501 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME, 502 M_WAITOK); 503 in6_sin_2_v4mapsin6(&sin, sin6_p); 504 505 return (struct sockaddr *)sin6_p; 506} 507 508/* 509 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was 510 * modified to match the pru_sockaddr() and pru_peeraddr() entry points 511 * in struct pr_usrreqs, so that protocols can just reference then directly 512 * without the need for a wrapper function. The socket must have a valid 513 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one 514 * except through a kernel programming error, so it is acceptable to panic 515 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap 516 * because there actually /is/ a programming error somewhere... XXX) 517 */ 518int 519in6_setsockaddr(so, nam) 520 struct socket *so; 521 struct sockaddr **nam; 522{ 523 int s; 524 register struct inpcb *inp; 525 struct in6_addr addr; 526 in_port_t port; 527 528 s = splnet(); 529 inp = sotoinpcb(so); 530 if (!inp) { 531 splx(s); 532 return EINVAL; 533 } 534 port = inp->inp_lport; 535 addr = inp->in6p_laddr; 536 splx(s); 537 538 *nam = in6_sockaddr(port, &addr); 539 return 0; 540} 541 542int 543in6_setpeeraddr(so, nam) 544 struct socket *so; 545 struct sockaddr **nam; 546{ 547 int s; 548 struct inpcb *inp; 549 struct in6_addr addr; 550 in_port_t port; 551 552 s = splnet(); 553 inp = sotoinpcb(so); 554 if (!inp) { 555 splx(s); 556 return EINVAL; 557 } 558 port = inp->inp_fport; 559 addr = inp->in6p_faddr; 560 splx(s); 561 562 *nam = in6_sockaddr(port, &addr); 563 return 0; 564} 565 566int 567in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 568{ 569 struct inpcb *inp = sotoinpcb(so); 570 int error; 571 572 if (inp == NULL) 573 return EINVAL; 574 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 575 error = in_setsockaddr(so, nam, &tcbinfo); 576 if (error == 0) 577 in6_sin_2_v4mapsin6_in_sock(nam); 578 } else { 579 /* scope issues will be handled in in6_setsockaddr(). */ 580 error = in6_setsockaddr(so, nam); 581 } 582 583 return error; 584} 585 586int 587in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 588{ 589 struct inpcb *inp = sotoinpcb(so); 590 int error; 591 592 if (inp == NULL) 593 return EINVAL; 594 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 595 error = in_setpeeraddr(so, nam, &tcbinfo); 596 if (error == 0) 597 in6_sin_2_v4mapsin6_in_sock(nam); 598 } else 599 /* scope issues will be handled in in6_setpeeraddr(). */ 600 error = in6_setpeeraddr(so, nam); 601 602 return error; 603} 604 605/* 606 * Pass some notification to all connections of a protocol 607 * associated with address dst. The local address and/or port numbers 608 * may be specified to limit the search. The "usual action" will be 609 * taken, depending on the ctlinput cmd. The caller must filter any 610 * cmds that are uninteresting (e.g., no error in the map). 611 * Call the protocol specific routine (if any) to report 612 * any errors for each matching socket. 613 * 614 * Must be called at splnet. 615 */ 616void 617in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify) 618 struct inpcbhead *head; 619 struct sockaddr *dst; 620 const struct sockaddr *src; 621 u_int fport_arg, lport_arg; 622 int cmd; 623 void *cmdarg; 624 struct inpcb *(*notify) __P((struct inpcb *, int)); 625{ 626 struct inpcb *inp, *ninp; 627 struct sockaddr_in6 sa6_src, *sa6_dst; 628 u_short fport = fport_arg, lport = lport_arg; 629 u_int32_t flowinfo; 630 int errno, s; 631 632 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6) 633 return; 634 635 sa6_dst = (struct sockaddr_in6 *)dst; 636 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 637 return; 638 639 /* 640 * note that src can be NULL when we get notify by local fragmentation. 641 */ 642 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src; 643 flowinfo = sa6_src.sin6_flowinfo; 644 645 /* 646 * Redirects go to all references to the destination, 647 * and use in6_rtchange to invalidate the route cache. 648 * Dead host indications: also use in6_rtchange to invalidate 649 * the cache, and deliver the error to all the sockets. 650 * Otherwise, if we have knowledge of the local port and address, 651 * deliver only to that socket. 652 */ 653 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 654 fport = 0; 655 lport = 0; 656 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 657 658 if (cmd != PRC_HOSTDEAD) 659 notify = in6_rtchange; 660 } 661 errno = inet6ctlerrmap[cmd]; 662 s = splnet(); 663 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) { 664 ninp = LIST_NEXT(inp, inp_list); 665 666 if ((inp->inp_vflag & INP_IPV6) == 0) 667 continue; 668 669 /* 670 * If the error designates a new path MTU for a destination 671 * and the application (associated with this socket) wanted to 672 * know the value, notify. Note that we notify for all 673 * disconnected sockets if the corresponding application 674 * wanted. This is because some UDP applications keep sending 675 * sockets disconnected. 676 * XXX: should we avoid to notify the value to TCP sockets? 677 */ 678 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 && 679 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 680 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) { 681 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst, 682 (u_int32_t *)cmdarg); 683 } 684 685 /* 686 * Detect if we should notify the error. If no source and 687 * destination ports are specifed, but non-zero flowinfo and 688 * local address match, notify the error. This is the case 689 * when the error is delivered with an encrypted buffer 690 * by ESP. Otherwise, just compare addresses and ports 691 * as usual. 692 */ 693 if (lport == 0 && fport == 0 && flowinfo && 694 inp->inp_socket != NULL && 695 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && 696 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 697 goto do_notify; 698 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 699 &sa6_dst->sin6_addr) || 700 inp->inp_socket == 0 || 701 (lport && inp->inp_lport != lport) || 702 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 703 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 704 &sa6_src.sin6_addr)) || 705 (fport && inp->inp_fport != fport)) 706 continue; 707 708 do_notify: 709 if (notify) 710 (*notify)(inp, errno); 711 } 712 splx(s); 713} 714 715/* 716 * Lookup a PCB based on the local address and port. 717 */ 718struct inpcb * 719in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay) 720 struct inpcbinfo *pcbinfo; 721 struct in6_addr *laddr; 722 u_int lport_arg; 723 int wild_okay; 724{ 725 register struct inpcb *inp; 726 int matchwild = 3, wildcard; 727 u_short lport = lport_arg; 728 729 if (!wild_okay) { 730 struct inpcbhead *head; 731 /* 732 * Look for an unconnected (wildcard foreign addr) PCB that 733 * matches the local address and port we're looking for. 734 */ 735 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, 736 pcbinfo->hashmask)]; 737 LIST_FOREACH(inp, head, inp_hash) { 738 if ((inp->inp_vflag & INP_IPV6) == 0) 739 continue; 740 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 741 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 742 inp->inp_lport == lport) { 743 /* 744 * Found. 745 */ 746 return (inp); 747 } 748 } 749 /* 750 * Not found. 751 */ 752 return (NULL); 753 } else { 754 struct inpcbporthead *porthash; 755 struct inpcbport *phd; 756 struct inpcb *match = NULL; 757 /* 758 * Best fit PCB lookup. 759 * 760 * First see if this local port is in use by looking on the 761 * port hash list. 762 */ 763 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport, 764 pcbinfo->porthashmask)]; 765 LIST_FOREACH(phd, porthash, phd_hash) { 766 if (phd->phd_port == lport) 767 break; 768 } 769 if (phd != NULL) { 770 /* 771 * Port is in use by one or more PCBs. Look for best 772 * fit. 773 */ 774 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 775 wildcard = 0; 776 if ((inp->inp_vflag & INP_IPV6) == 0) 777 continue; 778 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 779 wildcard++; 780 if (!IN6_IS_ADDR_UNSPECIFIED( 781 &inp->in6p_laddr)) { 782 if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 783 wildcard++; 784 else if (!IN6_ARE_ADDR_EQUAL( 785 &inp->in6p_laddr, laddr)) 786 continue; 787 } else { 788 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 789 wildcard++; 790 } 791 if (wildcard < matchwild) { 792 match = inp; 793 matchwild = wildcard; 794 if (matchwild == 0) { 795 break; 796 } 797 } 798 } 799 } 800 return (match); 801 } 802} 803 804void 805in6_pcbpurgeif0(head, ifp) 806 struct in6pcb *head; 807 struct ifnet *ifp; 808{ 809 struct in6pcb *in6p; 810 struct ip6_moptions *im6o; 811 struct in6_multi_mship *imm, *nimm; 812 813 for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) { 814 im6o = in6p->in6p_moptions; 815 if ((in6p->inp_vflag & INP_IPV6) && 816 im6o) { 817 /* 818 * Unselect the outgoing interface if it is being 819 * detached. 820 */ 821 if (im6o->im6o_multicast_ifp == ifp) 822 im6o->im6o_multicast_ifp = NULL; 823 824 /* 825 * Drop multicast group membership if we joined 826 * through the interface being detached. 827 * XXX controversial - is it really legal for kernel 828 * to force this? 829 */ 830 for (imm = im6o->im6o_memberships.lh_first; 831 imm != NULL; imm = nimm) { 832 nimm = imm->i6mm_chain.le_next; 833 if (imm->i6mm_maddr->in6m_ifp == ifp) { 834 LIST_REMOVE(imm, i6mm_chain); 835 in6_delmulti(imm->i6mm_maddr); 836 free(imm, M_IPMADDR); 837 } 838 } 839 } 840 } 841} 842 843/* 844 * Check for alternatives when higher level complains 845 * about service problems. For now, invalidate cached 846 * routing information. If the route was created dynamically 847 * (by a redirect), time to try a default gateway again. 848 */ 849void 850in6_losing(in6p) 851 struct inpcb *in6p; 852{ 853 /* 854 * We don't store route pointers in the routing table anymore 855 */ 856 return; 857} 858 859/* 860 * After a routing change, flush old routing 861 * and allocate a (hopefully) better one. 862 */ 863struct inpcb * 864in6_rtchange(inp, errno) 865 struct inpcb *inp; 866 int errno; 867{ 868 /* 869 * We don't store route pointers in the routing table anymore 870 */ 871 return inp; 872} 873 874/* 875 * Lookup PCB in hash list. 876 */ 877struct inpcb * 878in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp) 879 struct inpcbinfo *pcbinfo; 880 struct in6_addr *faddr, *laddr; 881 u_int fport_arg, lport_arg; 882 int wildcard; 883 struct ifnet *ifp; 884{ 885 struct inpcbhead *head; 886 register struct inpcb *inp; 887 u_short fport = fport_arg, lport = lport_arg; 888 int faith; 889 890 if (faithprefix_p != NULL) 891 faith = (*faithprefix_p)(laddr); 892 else 893 faith = 0; 894 895 /* 896 * First look for an exact match. 897 */ 898 head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, 899 lport, fport, 900 pcbinfo->hashmask)]; 901 LIST_FOREACH(inp, head, inp_hash) { 902 if ((inp->inp_vflag & INP_IPV6) == 0) 903 continue; 904 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 905 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 906 inp->inp_fport == fport && 907 inp->inp_lport == lport) { 908 /* 909 * Found. 910 */ 911 return (inp); 912 } 913 } 914 if (wildcard) { 915 struct inpcb *local_wild = NULL; 916 917 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, 918 pcbinfo->hashmask)]; 919 LIST_FOREACH(inp, head, inp_hash) { 920 if ((inp->inp_vflag & INP_IPV6) == 0) 921 continue; 922 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 923 inp->inp_lport == lport) { 924 if (faith && (inp->inp_flags & INP_FAITH) == 0) 925 continue; 926 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 927 laddr)) 928 return (inp); 929 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 930 local_wild = inp; 931 } 932 } 933 return (local_wild); 934 } 935 936 /* 937 * Not found. 938 */ 939 return (NULL); 940} 941 942void 943init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m) 944{ 945 struct ip6_hdr *ip; 946 947 ip = mtod(m, struct ip6_hdr *); 948 bzero(sin6, sizeof(*sin6)); 949 sin6->sin6_len = sizeof(*sin6); 950 sin6->sin6_family = AF_INET6; 951 sin6->sin6_addr = ip->ip6_src; 952 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 953 sin6->sin6_addr.s6_addr16[1] = 0; 954 sin6->sin6_scope_id = 955 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 956 ? m->m_pkthdr.rcvif->if_index : 0; 957 958 return; 959} 960