in6_pcb.c revision 185370
1/*- 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $ 30 */ 31 32/*- 33 * Copyright (c) 1982, 1986, 1991, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 4. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 61 */ 62 63#include <sys/cdefs.h> 64__FBSDID("$FreeBSD: head/sys/netinet6/in6_pcb.c 185370 2008-11-27 12:04:35Z bz $"); 65 66#include "opt_inet.h" 67#include "opt_inet6.h" 68#include "opt_ipsec.h" 69#include "opt_mac.h" 70 71#include <sys/param.h> 72#include <sys/systm.h> 73#include <sys/malloc.h> 74#include <sys/mbuf.h> 75#include <sys/domain.h> 76#include <sys/protosw.h> 77#include <sys/socket.h> 78#include <sys/socketvar.h> 79#include <sys/sockio.h> 80#include <sys/errno.h> 81#include <sys/time.h> 82#include <sys/priv.h> 83#include <sys/proc.h> 84#include <sys/jail.h> 85#include <sys/vimage.h> 86 87#include <vm/uma.h> 88 89#include <net/if.h> 90#include <net/if_types.h> 91#include <net/route.h> 92 93#include <netinet/in.h> 94#include <netinet/in_var.h> 95#include <netinet/in_systm.h> 96#include <netinet/tcp_var.h> 97#include <netinet/ip6.h> 98#include <netinet/ip_var.h> 99 100#include <netinet6/ip6_var.h> 101#include <netinet6/nd6.h> 102#include <netinet/in_pcb.h> 103#include <netinet6/in6_pcb.h> 104#include <netinet6/scope6_var.h> 105 106#include <security/mac/mac_framework.h> 107 108struct in6_addr zeroin6_addr; 109 110int 111in6_pcbbind(register struct inpcb *inp, struct sockaddr *nam, 112 struct ucred *cred) 113{ 114 INIT_VNET_INET6(inp->inp_vnet); 115 INIT_VNET_INET(inp->inp_vnet); 116 struct socket *so = inp->inp_socket; 117 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 118 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 119 u_short lport = 0; 120 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 121 122 INP_INFO_WLOCK_ASSERT(pcbinfo); 123 INP_WLOCK_ASSERT(inp); 124 125 if (!V_in6_ifaddr) /* XXX broken! */ 126 return (EADDRNOTAVAIL); 127 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 128 return (EINVAL); 129 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 130 wild = INPLOOKUP_WILDCARD; 131 if (nam) { 132 int error; 133 134 sin6 = (struct sockaddr_in6 *)nam; 135 if (nam->sa_len != sizeof(*sin6)) 136 return (EINVAL); 137 /* 138 * family check. 139 */ 140 if (nam->sa_family != AF_INET6) 141 return (EAFNOSUPPORT); 142 143 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 144 return(error); 145 146 lport = sin6->sin6_port; 147 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 148 /* 149 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 150 * allow compepte duplication of binding if 151 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 152 * and a multicast address is bound on both 153 * new and duplicated sockets. 154 */ 155 if (so->so_options & SO_REUSEADDR) 156 reuseport = SO_REUSEADDR|SO_REUSEPORT; 157 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 158 struct ifaddr *ia = NULL; 159 160 sin6->sin6_port = 0; /* yech... */ 161 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) 162 return (EADDRNOTAVAIL); 163 164 /* 165 * XXX: bind to an anycast address might accidentally 166 * cause sending a packet with anycast source address. 167 * We should allow to bind to a deprecated address, since 168 * the application dares to use it. 169 */ 170 if (ia && 171 ((struct in6_ifaddr *)ia)->ia6_flags & 172 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) { 173 return (EADDRNOTAVAIL); 174 } 175 } 176 if (lport) { 177 struct inpcb *t; 178 179 /* GROSS */ 180 if (ntohs(lport) <= V_ipport_reservedhigh && 181 ntohs(lport) >= V_ipport_reservedlow && 182 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 183 0)) 184 return (EACCES); 185 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) && 186 priv_check_cred(inp->inp_cred, 187 PRIV_NETINET_REUSEPORT, 0) != 0) { 188 t = in6_pcblookup_local(pcbinfo, 189 &sin6->sin6_addr, lport, 190 INPLOOKUP_WILDCARD, cred); 191 if (t && 192 ((t->inp_vflag & INP_TIMEWAIT) == 0) && 193 (so->so_type != SOCK_STREAM || 194 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) && 195 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 196 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || 197 (t->inp_socket->so_options & SO_REUSEPORT) 198 == 0) && (inp->inp_cred->cr_uid != 199 t->inp_cred->cr_uid)) 200 return (EADDRINUSE); 201 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 202 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 203 struct sockaddr_in sin; 204 205 in6_sin6_2_sin(&sin, sin6); 206 t = in_pcblookup_local(pcbinfo, 207 sin.sin_addr, lport, 208 INPLOOKUP_WILDCARD, cred); 209 if (t && 210 ((t->inp_vflag & 211 INP_TIMEWAIT) == 0) && 212 (so->so_type != SOCK_STREAM || 213 ntohl(t->inp_faddr.s_addr) == 214 INADDR_ANY) && 215 (inp->inp_cred->cr_uid != 216 t->inp_cred->cr_uid)) 217 return (EADDRINUSE); 218 } 219 } 220 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, 221 lport, wild, cred); 222 if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ? 223 intotw(t)->tw_so_options : 224 t->inp_socket->so_options)) == 0) 225 return (EADDRINUSE); 226 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 227 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 228 struct sockaddr_in sin; 229 230 in6_sin6_2_sin(&sin, sin6); 231 t = in_pcblookup_local(pcbinfo, sin.sin_addr, 232 lport, wild, cred); 233 if (t && t->inp_vflag & INP_TIMEWAIT) { 234 if ((reuseport & 235 intotw(t)->tw_so_options) == 0 && 236 (ntohl(t->inp_laddr.s_addr) != 237 INADDR_ANY || ((inp->inp_vflag & 238 INP_IPV6PROTO) == 239 (t->inp_vflag & INP_IPV6PROTO)))) 240 return (EADDRINUSE); 241 } 242 else if (t && 243 (reuseport & t->inp_socket->so_options) 244 == 0 && (ntohl(t->inp_laddr.s_addr) != 245 INADDR_ANY || INP_SOCKAF(so) == 246 INP_SOCKAF(t->inp_socket))) 247 return (EADDRINUSE); 248 } 249 } 250 inp->in6p_laddr = sin6->sin6_addr; 251 } 252 if (lport == 0) { 253 int e; 254 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) 255 return (e); 256 } else { 257 inp->inp_lport = lport; 258 if (in_pcbinshash(inp) != 0) { 259 inp->in6p_laddr = in6addr_any; 260 inp->inp_lport = 0; 261 return (EAGAIN); 262 } 263 } 264 return (0); 265} 266 267/* 268 * Transform old in6_pcbconnect() into an inner subroutine for new 269 * in6_pcbconnect(): Do some validity-checking on the remote 270 * address (in mbuf 'nam') and then determine local host address 271 * (i.e., which interface) to use to access that remote host. 272 * 273 * This preserves definition of in6_pcbconnect(), while supporting a 274 * slightly different version for T/TCP. (This is more than 275 * a bit of a kludge, but cleaning up the internal interfaces would 276 * have forced minor changes in every protocol). 277 */ 278int 279in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam, 280 struct in6_addr **plocal_addr6) 281{ 282 INIT_VNET_INET6(inp->inp_vnet); 283 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 284 int error = 0; 285 struct ifnet *ifp = NULL; 286 int scope_ambiguous = 0; 287 288 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 289 INP_WLOCK_ASSERT(inp); 290 291 if (nam->sa_len != sizeof (*sin6)) 292 return (EINVAL); 293 if (sin6->sin6_family != AF_INET6) 294 return (EAFNOSUPPORT); 295 if (sin6->sin6_port == 0) 296 return (EADDRNOTAVAIL); 297 298 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone) 299 scope_ambiguous = 1; 300 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 301 return(error); 302 303 if (V_in6_ifaddr) { 304 /* 305 * If the destination address is UNSPECIFIED addr, 306 * use the loopback addr, e.g ::1. 307 */ 308 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 309 sin6->sin6_addr = in6addr_loopback; 310 } 311 312 /* 313 * XXX: in6_selectsrc might replace the bound local address 314 * with the address specified by setsockopt(IPV6_PKTINFO). 315 * Is it the intended behavior? 316 */ 317 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts, 318 inp, NULL, 319 inp->inp_cred, 320 &ifp, &error); 321 if (ifp && scope_ambiguous && 322 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) { 323 return(error); 324 } 325 326 if (*plocal_addr6 == NULL) { 327 if (error == 0) 328 error = EADDRNOTAVAIL; 329 return (error); 330 } 331 /* 332 * Don't do pcblookup call here; return interface in 333 * plocal_addr6 334 * and exit to caller, that will do the lookup. 335 */ 336 337 return (0); 338} 339 340/* 341 * Outer subroutine: 342 * Connect from a socket to a specified address. 343 * Both address and port must be specified in argument sin. 344 * If don't have a local address for this socket yet, 345 * then pick one. 346 */ 347int 348in6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam, 349 struct ucred *cred) 350{ 351 struct in6_addr *addr6; 352 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 353 int error; 354 355 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 356 INP_WLOCK_ASSERT(inp); 357 358 /* 359 * Call inner routine, to assign local interface address. 360 * in6_pcbladdr() may automatically fill in sin6_scope_id. 361 */ 362 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0) 363 return (error); 364 365 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, 366 sin6->sin6_port, 367 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 368 ? addr6 : &inp->in6p_laddr, 369 inp->inp_lport, 0, NULL) != NULL) { 370 return (EADDRINUSE); 371 } 372 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 373 if (inp->inp_lport == 0) { 374 error = in6_pcbbind(inp, (struct sockaddr *)0, cred); 375 if (error) 376 return (error); 377 } 378 inp->in6p_laddr = *addr6; 379 } 380 inp->in6p_faddr = sin6->sin6_addr; 381 inp->inp_fport = sin6->sin6_port; 382 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 383 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 384 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 385 inp->in6p_flowinfo |= 386 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 387 388 in_pcbrehash(inp); 389 390 return (0); 391} 392 393void 394in6_pcbdisconnect(struct inpcb *inp) 395{ 396 397 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 398 INP_WLOCK_ASSERT(inp); 399 400 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr)); 401 inp->inp_fport = 0; 402 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 403 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 404 in_pcbrehash(inp); 405} 406 407struct sockaddr * 408in6_sockaddr(in_port_t port, struct in6_addr *addr_p) 409{ 410 struct sockaddr_in6 *sin6; 411 412 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK); 413 bzero(sin6, sizeof *sin6); 414 sin6->sin6_family = AF_INET6; 415 sin6->sin6_len = sizeof(*sin6); 416 sin6->sin6_port = port; 417 sin6->sin6_addr = *addr_p; 418 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */ 419 420 return (struct sockaddr *)sin6; 421} 422 423struct sockaddr * 424in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p) 425{ 426 struct sockaddr_in sin; 427 struct sockaddr_in6 *sin6_p; 428 429 bzero(&sin, sizeof sin); 430 sin.sin_family = AF_INET; 431 sin.sin_len = sizeof(sin); 432 sin.sin_port = port; 433 sin.sin_addr = *addr_p; 434 435 sin6_p = malloc(sizeof *sin6_p, M_SONAME, 436 M_WAITOK); 437 in6_sin_2_v4mapsin6(&sin, sin6_p); 438 439 return (struct sockaddr *)sin6_p; 440} 441 442int 443in6_getsockaddr(struct socket *so, struct sockaddr **nam) 444{ 445 register struct inpcb *inp; 446 struct in6_addr addr; 447 in_port_t port; 448 449 inp = sotoinpcb(so); 450 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL")); 451 452 INP_RLOCK(inp); 453 port = inp->inp_lport; 454 addr = inp->in6p_laddr; 455 INP_RUNLOCK(inp); 456 457 *nam = in6_sockaddr(port, &addr); 458 return 0; 459} 460 461int 462in6_getpeeraddr(struct socket *so, struct sockaddr **nam) 463{ 464 struct inpcb *inp; 465 struct in6_addr addr; 466 in_port_t port; 467 468 inp = sotoinpcb(so); 469 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL")); 470 471 INP_RLOCK(inp); 472 port = inp->inp_fport; 473 addr = inp->in6p_faddr; 474 INP_RUNLOCK(inp); 475 476 *nam = in6_sockaddr(port, &addr); 477 return 0; 478} 479 480int 481in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 482{ 483 struct inpcb *inp; 484 int error; 485 486 inp = sotoinpcb(so); 487 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL")); 488 489 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 490 error = in_getsockaddr(so, nam); 491 if (error == 0) 492 in6_sin_2_v4mapsin6_in_sock(nam); 493 } else { 494 /* scope issues will be handled in in6_getsockaddr(). */ 495 error = in6_getsockaddr(so, nam); 496 } 497 498 return error; 499} 500 501int 502in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 503{ 504 struct inpcb *inp; 505 int error; 506 507 inp = sotoinpcb(so); 508 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL")); 509 510 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 511 error = in_getpeeraddr(so, nam); 512 if (error == 0) 513 in6_sin_2_v4mapsin6_in_sock(nam); 514 } else 515 /* scope issues will be handled in in6_getpeeraddr(). */ 516 error = in6_getpeeraddr(so, nam); 517 518 return error; 519} 520 521/* 522 * Pass some notification to all connections of a protocol 523 * associated with address dst. The local address and/or port numbers 524 * may be specified to limit the search. The "usual action" will be 525 * taken, depending on the ctlinput cmd. The caller must filter any 526 * cmds that are uninteresting (e.g., no error in the map). 527 * Call the protocol specific routine (if any) to report 528 * any errors for each matching socket. 529 */ 530void 531in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst, 532 u_int fport_arg, const struct sockaddr *src, u_int lport_arg, 533 int cmd, void *cmdarg, 534 struct inpcb *(*notify)(struct inpcb *, int)) 535{ 536 struct inpcb *inp, *inp_temp; 537 struct sockaddr_in6 sa6_src, *sa6_dst; 538 u_short fport = fport_arg, lport = lport_arg; 539 u_int32_t flowinfo; 540 int errno; 541 542 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6) 543 return; 544 545 sa6_dst = (struct sockaddr_in6 *)dst; 546 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 547 return; 548 549 /* 550 * note that src can be NULL when we get notify by local fragmentation. 551 */ 552 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src; 553 flowinfo = sa6_src.sin6_flowinfo; 554 555 /* 556 * Redirects go to all references to the destination, 557 * and use in6_rtchange to invalidate the route cache. 558 * Dead host indications: also use in6_rtchange to invalidate 559 * the cache, and deliver the error to all the sockets. 560 * Otherwise, if we have knowledge of the local port and address, 561 * deliver only to that socket. 562 */ 563 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 564 fport = 0; 565 lport = 0; 566 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 567 568 if (cmd != PRC_HOSTDEAD) 569 notify = in6_rtchange; 570 } 571 errno = inet6ctlerrmap[cmd]; 572 INP_INFO_WLOCK(pcbinfo); 573 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 574 INP_WLOCK(inp); 575 if ((inp->inp_vflag & INP_IPV6) == 0) { 576 INP_WUNLOCK(inp); 577 continue; 578 } 579 580 /* 581 * If the error designates a new path MTU for a destination 582 * and the application (associated with this socket) wanted to 583 * know the value, notify. Note that we notify for all 584 * disconnected sockets if the corresponding application 585 * wanted. This is because some UDP applications keep sending 586 * sockets disconnected. 587 * XXX: should we avoid to notify the value to TCP sockets? 588 */ 589 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 && 590 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 591 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) { 592 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst, 593 (u_int32_t *)cmdarg); 594 } 595 596 /* 597 * Detect if we should notify the error. If no source and 598 * destination ports are specifed, but non-zero flowinfo and 599 * local address match, notify the error. This is the case 600 * when the error is delivered with an encrypted buffer 601 * by ESP. Otherwise, just compare addresses and ports 602 * as usual. 603 */ 604 if (lport == 0 && fport == 0 && flowinfo && 605 inp->inp_socket != NULL && 606 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && 607 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 608 goto do_notify; 609 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 610 &sa6_dst->sin6_addr) || 611 inp->inp_socket == 0 || 612 (lport && inp->inp_lport != lport) || 613 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 614 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 615 &sa6_src.sin6_addr)) || 616 (fport && inp->inp_fport != fport)) { 617 INP_WUNLOCK(inp); 618 continue; 619 } 620 621 do_notify: 622 if (notify) { 623 if ((*notify)(inp, errno)) 624 INP_WUNLOCK(inp); 625 } else 626 INP_WUNLOCK(inp); 627 } 628 INP_INFO_WUNLOCK(pcbinfo); 629} 630 631/* 632 * Lookup a PCB based on the local address and port. 633 */ 634struct inpcb * 635in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr, 636 u_short lport, int wild_okay, struct ucred *cred) 637{ 638 register struct inpcb *inp; 639 int matchwild = 3, wildcard; 640 641 INP_INFO_WLOCK_ASSERT(pcbinfo); 642 643 if (!wild_okay) { 644 struct inpcbhead *head; 645 /* 646 * Look for an unconnected (wildcard foreign addr) PCB that 647 * matches the local address and port we're looking for. 648 */ 649 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 650 0, pcbinfo->ipi_hashmask)]; 651 LIST_FOREACH(inp, head, inp_hash) { 652 if ((inp->inp_vflag & INP_IPV6) == 0) 653 continue; 654 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 655 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 656 inp->inp_lport == lport) { 657 /* 658 * Found. 659 */ 660 return (inp); 661 } 662 } 663 /* 664 * Not found. 665 */ 666 return (NULL); 667 } else { 668 struct inpcbporthead *porthash; 669 struct inpcbport *phd; 670 struct inpcb *match = NULL; 671 /* 672 * Best fit PCB lookup. 673 * 674 * First see if this local port is in use by looking on the 675 * port hash list. 676 */ 677 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 678 pcbinfo->ipi_porthashmask)]; 679 LIST_FOREACH(phd, porthash, phd_hash) { 680 if (phd->phd_port == lport) 681 break; 682 } 683 if (phd != NULL) { 684 /* 685 * Port is in use by one or more PCBs. Look for best 686 * fit. 687 */ 688 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 689 wildcard = 0; 690 if ((inp->inp_vflag & INP_IPV6) == 0) 691 continue; 692 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 693 wildcard++; 694 if (!IN6_IS_ADDR_UNSPECIFIED( 695 &inp->in6p_laddr)) { 696 if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 697 wildcard++; 698 else if (!IN6_ARE_ADDR_EQUAL( 699 &inp->in6p_laddr, laddr)) 700 continue; 701 } else { 702 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 703 wildcard++; 704 } 705 if (wildcard < matchwild) { 706 match = inp; 707 matchwild = wildcard; 708 if (matchwild == 0) { 709 break; 710 } 711 } 712 } 713 } 714 return (match); 715 } 716} 717 718void 719in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 720{ 721 struct in6pcb *in6p; 722 struct ip6_moptions *im6o; 723 struct in6_multi_mship *imm, *nimm; 724 725 INP_INFO_RLOCK(pcbinfo); 726 LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) { 727 INP_WLOCK(in6p); 728 im6o = in6p->in6p_moptions; 729 if ((in6p->inp_vflag & INP_IPV6) && 730 im6o) { 731 /* 732 * Unselect the outgoing interface if it is being 733 * detached. 734 */ 735 if (im6o->im6o_multicast_ifp == ifp) 736 im6o->im6o_multicast_ifp = NULL; 737 738 /* 739 * Drop multicast group membership if we joined 740 * through the interface being detached. 741 * XXX controversial - is it really legal for kernel 742 * to force this? 743 */ 744 for (imm = im6o->im6o_memberships.lh_first; 745 imm != NULL; imm = nimm) { 746 nimm = imm->i6mm_chain.le_next; 747 if (imm->i6mm_maddr->in6m_ifp == ifp) { 748 LIST_REMOVE(imm, i6mm_chain); 749 in6_delmulti(imm->i6mm_maddr); 750 free(imm, M_IP6MADDR); 751 } 752 } 753 } 754 INP_WUNLOCK(in6p); 755 } 756 INP_INFO_RUNLOCK(pcbinfo); 757} 758 759/* 760 * Check for alternatives when higher level complains 761 * about service problems. For now, invalidate cached 762 * routing information. If the route was created dynamically 763 * (by a redirect), time to try a default gateway again. 764 */ 765void 766in6_losing(struct inpcb *in6p) 767{ 768 769 /* 770 * We don't store route pointers in the routing table anymore 771 */ 772 return; 773} 774 775/* 776 * After a routing change, flush old routing 777 * and allocate a (hopefully) better one. 778 */ 779struct inpcb * 780in6_rtchange(struct inpcb *inp, int errno) 781{ 782 /* 783 * We don't store route pointers in the routing table anymore 784 */ 785 return inp; 786} 787 788/* 789 * Lookup PCB in hash list. 790 */ 791struct inpcb * 792in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 793 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, 794 int wildcard, struct ifnet *ifp) 795{ 796 struct inpcbhead *head; 797 register struct inpcb *inp; 798 u_short fport = fport_arg, lport = lport_arg; 799 int faith; 800 801 INP_INFO_LOCK_ASSERT(pcbinfo); 802 803 if (faithprefix_p != NULL) 804 faith = (*faithprefix_p)(laddr); 805 else 806 faith = 0; 807 808 /* 809 * First look for an exact match. 810 */ 811 head = &pcbinfo->ipi_hashbase[ 812 INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport, 813 pcbinfo->ipi_hashmask)]; 814 LIST_FOREACH(inp, head, inp_hash) { 815 if ((inp->inp_vflag & INP_IPV6) == 0) 816 continue; 817 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 818 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 819 inp->inp_fport == fport && 820 inp->inp_lport == lport) { 821 /* 822 * Found. 823 */ 824 return (inp); 825 } 826 } 827 if (wildcard) { 828 struct inpcb *local_wild = NULL; 829 830 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 831 0, pcbinfo->ipi_hashmask)]; 832 LIST_FOREACH(inp, head, inp_hash) { 833 if ((inp->inp_vflag & INP_IPV6) == 0) 834 continue; 835 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 836 inp->inp_lport == lport) { 837 if (faith && (inp->inp_flags & INP_FAITH) == 0) 838 continue; 839 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 840 laddr)) 841 return (inp); 842 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 843 local_wild = inp; 844 } 845 } 846 return (local_wild); 847 } 848 849 /* 850 * Not found. 851 */ 852 return (NULL); 853} 854 855void 856init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m) 857{ 858 struct ip6_hdr *ip; 859 860 ip = mtod(m, struct ip6_hdr *); 861 bzero(sin6, sizeof(*sin6)); 862 sin6->sin6_len = sizeof(*sin6); 863 sin6->sin6_family = AF_INET6; 864 sin6->sin6_addr = ip->ip6_src; 865 866 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */ 867 868 return; 869} 870