in6_pcb.c revision 120913
1/* $FreeBSD: head/sys/netinet6/in6_pcb.c 120913 2003-10-08 18:26:08Z 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 dares 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 416void 417in6_pcbdisconnect(inp) 418 struct inpcb *inp; 419{ 420 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr)); 421 inp->inp_fport = 0; 422 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 423 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 424 in_pcbrehash(inp); 425 if (inp->inp_socket->so_state & SS_NOFDREF) 426 in6_pcbdetach(inp); 427} 428 429void 430in6_pcbdetach(inp) 431 struct inpcb *inp; 432{ 433 struct socket *so = inp->inp_socket; 434 struct inpcbinfo *ipi = inp->inp_pcbinfo; 435 436#ifdef IPSEC 437 if (inp->in6p_sp != NULL) 438 ipsec6_delete_pcbpolicy(inp); 439#endif /* IPSEC */ 440 inp->inp_gencnt = ++ipi->ipi_gencnt; 441 in_pcbremlists(inp); 442 443 if (so) { 444 so->so_pcb = NULL; 445 sotryfree(so); 446 } 447 if (inp->in6p_options) 448 m_freem(inp->in6p_options); 449 ip6_freepcbopts(inp->in6p_outputopts); 450 ip6_freemoptions(inp->in6p_moptions); 451 if (inp->in6p_route.ro_rt) 452 RTFREE(inp->in6p_route.ro_rt); 453 /* Check and free IPv4 related resources in case of mapped addr */ 454 if (inp->inp_options) 455 (void)m_free(inp->inp_options); 456 ip_freemoptions(inp->inp_moptions); 457 inp->inp_vflag = 0; 458 INP_LOCK_DESTROY(inp); 459 uma_zfree(ipi->ipi_zone, inp); 460} 461 462struct sockaddr * 463in6_sockaddr(port, addr_p) 464 in_port_t port; 465 struct in6_addr *addr_p; 466{ 467 struct sockaddr_in6 *sin6; 468 469 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK); 470 bzero(sin6, sizeof *sin6); 471 sin6->sin6_family = AF_INET6; 472 sin6->sin6_len = sizeof(*sin6); 473 sin6->sin6_port = port; 474 sin6->sin6_addr = *addr_p; 475 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 476 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 477 else 478 sin6->sin6_scope_id = 0; /*XXX*/ 479 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 480 sin6->sin6_addr.s6_addr16[1] = 0; 481 482 return (struct sockaddr *)sin6; 483} 484 485struct sockaddr * 486in6_v4mapsin6_sockaddr(port, addr_p) 487 in_port_t port; 488 struct in_addr *addr_p; 489{ 490 struct sockaddr_in sin; 491 struct sockaddr_in6 *sin6_p; 492 493 bzero(&sin, sizeof sin); 494 sin.sin_family = AF_INET; 495 sin.sin_len = sizeof(sin); 496 sin.sin_port = port; 497 sin.sin_addr = *addr_p; 498 499 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME, 500 M_WAITOK); 501 in6_sin_2_v4mapsin6(&sin, sin6_p); 502 503 return (struct sockaddr *)sin6_p; 504} 505 506/* 507 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was 508 * modified to match the pru_sockaddr() and pru_peeraddr() entry points 509 * in struct pr_usrreqs, so that protocols can just reference then directly 510 * without the need for a wrapper function. The socket must have a valid 511 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one 512 * except through a kernel programming error, so it is acceptable to panic 513 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap 514 * because there actually /is/ a programming error somewhere... XXX) 515 */ 516int 517in6_setsockaddr(so, nam) 518 struct socket *so; 519 struct sockaddr **nam; 520{ 521 int s; 522 register struct inpcb *inp; 523 struct in6_addr addr; 524 in_port_t port; 525 526 s = splnet(); 527 inp = sotoinpcb(so); 528 if (!inp) { 529 splx(s); 530 return EINVAL; 531 } 532 port = inp->inp_lport; 533 addr = inp->in6p_laddr; 534 splx(s); 535 536 *nam = in6_sockaddr(port, &addr); 537 return 0; 538} 539 540int 541in6_setpeeraddr(so, nam) 542 struct socket *so; 543 struct sockaddr **nam; 544{ 545 int s; 546 struct inpcb *inp; 547 struct in6_addr addr; 548 in_port_t port; 549 550 s = splnet(); 551 inp = sotoinpcb(so); 552 if (!inp) { 553 splx(s); 554 return EINVAL; 555 } 556 port = inp->inp_fport; 557 addr = inp->in6p_faddr; 558 splx(s); 559 560 *nam = in6_sockaddr(port, &addr); 561 return 0; 562} 563 564int 565in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 566{ 567 struct inpcb *inp = sotoinpcb(so); 568 int error; 569 570 if (inp == NULL) 571 return EINVAL; 572 if (inp->inp_vflag & INP_IPV4) { 573 error = in_setsockaddr(so, nam, &tcbinfo); 574 if (error == 0) 575 in6_sin_2_v4mapsin6_in_sock(nam); 576 } else { 577 /* scope issues will be handled in in6_setsockaddr(). */ 578 error = in6_setsockaddr(so, nam); 579 } 580 581 return error; 582} 583 584int 585in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 586{ 587 struct inpcb *inp = sotoinpcb(so); 588 int error; 589 590 if (inp == NULL) 591 return EINVAL; 592 if (inp->inp_vflag & INP_IPV4) { 593 error = in_setpeeraddr(so, nam, &tcbinfo); 594 if (error == 0) 595 in6_sin_2_v4mapsin6_in_sock(nam); 596 } else 597 /* scope issues will be handled in in6_setpeeraddr(). */ 598 error = in6_setpeeraddr(so, nam); 599 600 return error; 601} 602 603/* 604 * Pass some notification to all connections of a protocol 605 * associated with address dst. The local address and/or port numbers 606 * may be specified to limit the search. The "usual action" will be 607 * taken, depending on the ctlinput cmd. The caller must filter any 608 * cmds that are uninteresting (e.g., no error in the map). 609 * Call the protocol specific routine (if any) to report 610 * any errors for each matching socket. 611 * 612 * Must be called at splnet. 613 */ 614void 615in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, notify) 616 struct inpcbhead *head; 617 struct sockaddr *dst; 618 const struct sockaddr *src; 619 u_int fport_arg, lport_arg; 620 int cmd; 621 struct inpcb *(*notify) __P((struct inpcb *, int)); 622{ 623 struct inpcb *inp, *ninp; 624 struct sockaddr_in6 sa6_src, *sa6_dst; 625 u_short fport = fport_arg, lport = lport_arg; 626 u_int32_t flowinfo; 627 int errno, s; 628 629 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6) 630 return; 631 632 sa6_dst = (struct sockaddr_in6 *)dst; 633 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 634 return; 635 636 /* 637 * note that src can be NULL when we get notify by local fragmentation. 638 */ 639 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src; 640 flowinfo = sa6_src.sin6_flowinfo; 641 642 /* 643 * Redirects go to all references to the destination, 644 * and use in6_rtchange to invalidate the route cache. 645 * Dead host indications: also use in6_rtchange to invalidate 646 * the cache, and deliver the error to all the sockets. 647 * Otherwise, if we have knowledge of the local port and address, 648 * deliver only to that socket. 649 */ 650 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 651 fport = 0; 652 lport = 0; 653 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 654 655 if (cmd != PRC_HOSTDEAD) 656 notify = in6_rtchange; 657 } 658 errno = inet6ctlerrmap[cmd]; 659 s = splnet(); 660 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) { 661 ninp = LIST_NEXT(inp, inp_list); 662 663 if ((inp->inp_vflag & INP_IPV6) == 0) 664 continue; 665 666 /* 667 * Detect if we should notify the error. If no source and 668 * destination ports are specifed, but non-zero flowinfo and 669 * local address match, notify the error. This is the case 670 * when the error is delivered with an encrypted buffer 671 * by ESP. Otherwise, just compare addresses and ports 672 * as usual. 673 */ 674 if (lport == 0 && fport == 0 && flowinfo && 675 inp->inp_socket != NULL && 676 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && 677 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 678 goto do_notify; 679 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 680 &sa6_dst->sin6_addr) || 681 inp->inp_socket == 0 || 682 (lport && inp->inp_lport != lport) || 683 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 684 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 685 &sa6_src.sin6_addr)) || 686 (fport && inp->inp_fport != fport)) 687 continue; 688 689 do_notify: 690 if (notify) 691 (*notify)(inp, errno); 692 } 693 splx(s); 694} 695 696/* 697 * Lookup a PCB based on the local address and port. 698 */ 699struct inpcb * 700in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay) 701 struct inpcbinfo *pcbinfo; 702 struct in6_addr *laddr; 703 u_int lport_arg; 704 int wild_okay; 705{ 706 register struct inpcb *inp; 707 int matchwild = 3, wildcard; 708 u_short lport = lport_arg; 709 710 if (!wild_okay) { 711 struct inpcbhead *head; 712 /* 713 * Look for an unconnected (wildcard foreign addr) PCB that 714 * matches the local address and port we're looking for. 715 */ 716 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, 717 pcbinfo->hashmask)]; 718 LIST_FOREACH(inp, head, inp_hash) { 719 if ((inp->inp_vflag & INP_IPV6) == 0) 720 continue; 721 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 722 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 723 inp->inp_lport == lport) { 724 /* 725 * Found. 726 */ 727 return (inp); 728 } 729 } 730 /* 731 * Not found. 732 */ 733 return (NULL); 734 } else { 735 struct inpcbporthead *porthash; 736 struct inpcbport *phd; 737 struct inpcb *match = NULL; 738 /* 739 * Best fit PCB lookup. 740 * 741 * First see if this local port is in use by looking on the 742 * port hash list. 743 */ 744 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport, 745 pcbinfo->porthashmask)]; 746 LIST_FOREACH(phd, porthash, phd_hash) { 747 if (phd->phd_port == lport) 748 break; 749 } 750 if (phd != NULL) { 751 /* 752 * Port is in use by one or more PCBs. Look for best 753 * fit. 754 */ 755 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 756 wildcard = 0; 757 if ((inp->inp_vflag & INP_IPV6) == 0) 758 continue; 759 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 760 wildcard++; 761 if (!IN6_IS_ADDR_UNSPECIFIED( 762 &inp->in6p_laddr)) { 763 if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 764 wildcard++; 765 else if (!IN6_ARE_ADDR_EQUAL( 766 &inp->in6p_laddr, laddr)) 767 continue; 768 } else { 769 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 770 wildcard++; 771 } 772 if (wildcard < matchwild) { 773 match = inp; 774 matchwild = wildcard; 775 if (matchwild == 0) { 776 break; 777 } 778 } 779 } 780 } 781 return (match); 782 } 783} 784 785void 786in6_pcbpurgeif0(head, ifp) 787 struct in6pcb *head; 788 struct ifnet *ifp; 789{ 790 struct in6pcb *in6p; 791 struct ip6_moptions *im6o; 792 struct in6_multi_mship *imm, *nimm; 793 794 for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) { 795 im6o = in6p->in6p_moptions; 796 if ((in6p->inp_vflag & INP_IPV6) && 797 im6o) { 798 /* 799 * Unselect the outgoing interface if it is being 800 * detached. 801 */ 802 if (im6o->im6o_multicast_ifp == ifp) 803 im6o->im6o_multicast_ifp = NULL; 804 805 /* 806 * Drop multicast group membership if we joined 807 * through the interface being detached. 808 * XXX controversial - is it really legal for kernel 809 * to force this? 810 */ 811 for (imm = im6o->im6o_memberships.lh_first; 812 imm != NULL; imm = nimm) { 813 nimm = imm->i6mm_chain.le_next; 814 if (imm->i6mm_maddr->in6m_ifp == ifp) { 815 LIST_REMOVE(imm, i6mm_chain); 816 in6_delmulti(imm->i6mm_maddr); 817 free(imm, M_IPMADDR); 818 } 819 } 820 } 821 } 822} 823 824/* 825 * Check for alternatives when higher level complains 826 * about service problems. For now, invalidate cached 827 * routing information. If the route was created dynamically 828 * (by a redirect), time to try a default gateway again. 829 */ 830void 831in6_losing(in6p) 832 struct inpcb *in6p; 833{ 834 struct rtentry *rt; 835 struct rt_addrinfo info; 836 837 if ((rt = in6p->in6p_route.ro_rt) != NULL) { 838 RT_LOCK(rt); 839 in6p->in6p_route.ro_rt = NULL; 840 bzero((caddr_t)&info, sizeof(info)); 841 info.rti_flags = rt->rt_flags; 842 info.rti_info[RTAX_DST] = rt_key(rt); 843 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 844 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 845 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 846 if (rt->rt_flags & RTF_DYNAMIC) { 847 RT_UNLOCK(rt); /* XXX refcnt? */ 848 (void)rtrequest1(RTM_DELETE, &info, NULL); 849 } else 850 rtfree(rt); 851 /* 852 * A new route can be allocated 853 * the next time output is attempted. 854 */ 855 } 856} 857 858/* 859 * After a routing change, flush old routing 860 * and allocate a (hopefully) better one. 861 */ 862struct inpcb * 863in6_rtchange(inp, errno) 864 struct inpcb *inp; 865 int errno; 866{ 867 if (inp->in6p_route.ro_rt) { 868 RTFREE(inp->in6p_route.ro_rt); 869 inp->in6p_route.ro_rt = 0; 870 /* 871 * A new route can be allocated the next time 872 * output is attempted. 873 */ 874 } 875 return inp; 876} 877 878/* 879 * Lookup PCB in hash list. 880 */ 881struct inpcb * 882in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp) 883 struct inpcbinfo *pcbinfo; 884 struct in6_addr *faddr, *laddr; 885 u_int fport_arg, lport_arg; 886 int wildcard; 887 struct ifnet *ifp; 888{ 889 struct inpcbhead *head; 890 register struct inpcb *inp; 891 u_short fport = fport_arg, lport = lport_arg; 892 int faith; 893 894 if (faithprefix_p != NULL) 895 faith = (*faithprefix_p)(laddr); 896 else 897 faith = 0; 898 899 /* 900 * First look for an exact match. 901 */ 902 head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, 903 lport, fport, 904 pcbinfo->hashmask)]; 905 LIST_FOREACH(inp, head, inp_hash) { 906 if ((inp->inp_vflag & INP_IPV6) == 0) 907 continue; 908 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 909 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 910 inp->inp_fport == fport && 911 inp->inp_lport == lport) { 912 /* 913 * Found. 914 */ 915 return (inp); 916 } 917 } 918 if (wildcard) { 919 struct inpcb *local_wild = NULL; 920 921 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, 922 pcbinfo->hashmask)]; 923 LIST_FOREACH(inp, head, inp_hash) { 924 if ((inp->inp_vflag & INP_IPV6) == 0) 925 continue; 926 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 927 inp->inp_lport == lport) { 928 if (faith && (inp->inp_flags & INP_FAITH) == 0) 929 continue; 930 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 931 laddr)) 932 return (inp); 933 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 934 local_wild = inp; 935 } 936 } 937 return (local_wild); 938 } 939 940 /* 941 * Not found. 942 */ 943 return (NULL); 944} 945 946void 947init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m) 948{ 949 struct ip6_hdr *ip; 950 951 ip = mtod(m, struct ip6_hdr *); 952 bzero(sin6, sizeof(*sin6)); 953 sin6->sin6_len = sizeof(*sin6); 954 sin6->sin6_family = AF_INET6; 955 sin6->sin6_addr = ip->ip6_src; 956 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 957 sin6->sin6_addr.s6_addr16[1] = 0; 958 sin6->sin6_scope_id = 959 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 960 ? m->m_pkthdr.rcvif->if_index : 0; 961 962 return; 963} 964