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