1 /* $NetBSD: in6_pcb.c,v 1.25 2000/06/05 08:09:48 itojun Exp $ */ 2 /* $KAME: in6_pcb.c,v 1.45 2000/06/05 00:41:58 itojun 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 * Copyright (c) 1982, 1986, 1991, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 66 */ 67 68 #include "opt_ipsec.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/protosw.h> 75 #include <sys/socket.h> 76 #include <sys/socketvar.h> 77 #include <sys/ioctl.h> 78 #include <sys/errno.h> 79 #include <sys/time.h> 80 #include <sys/proc.h> 81 82 #include <net/if.h> 83 #include <net/route.h> 84 85 #include <netinet/in.h> 86 #include <netinet/in_var.h> 87 #include <netinet/in_systm.h> 88 #include <netinet/ip.h> 89 #include <netinet/in_pcb.h> 90 #include <netinet/ip6.h> 91 #include <netinet6/ip6_var.h> 92 #include <netinet6/in6_pcb.h> 93 #include <netinet6/nd6.h> 94 95 #include "loop.h" 96 extern struct ifnet loif[NLOOP]; 97 #include "faith.h" 98 99 #ifdef IPSEC 100 #include <netinet6/ipsec.h> 101 #include <netkey/key.h> 102 #include <netkey/key_debug.h> 103 #endif /* IPSEC */ 104 105 struct in6_addr zeroin6_addr; 106 107 int 108 in6_pcballoc(so, head) 109 struct socket *so; 110 struct in6pcb *head; 111 { 112 struct in6pcb *in6p; 113 114 MALLOC(in6p, struct in6pcb *, sizeof(*in6p), M_PCB, M_NOWAIT); 115 if (in6p == NULL) 116 return(ENOBUFS); 117 bzero((caddr_t)in6p, sizeof(*in6p)); 118 in6p->in6p_head = head; 119 in6p->in6p_socket = so; 120 in6p->in6p_hops = -1; /* use kernel default */ 121 in6p->in6p_icmp6filt = NULL; 122 in6p->in6p_next = head->in6p_next; 123 head->in6p_next = in6p; 124 in6p->in6p_prev = head; 125 in6p->in6p_next->in6p_prev = in6p; 126 #ifndef INET6_BINDV6ONLY 127 if (ip6_bindv6only) 128 in6p->in6p_flags |= IN6P_BINDV6ONLY; 129 #else 130 in6p->in6p_flags |= IN6P_BINDV6ONLY; /*just for safety*/ 131 #endif 132 so->so_pcb = (caddr_t)in6p; 133 return(0); 134 } 135 136 int 137 in6_pcbbind(in6p, nam, p) 138 register struct in6pcb *in6p; 139 struct mbuf *nam; 140 struct proc *p; 141 { 142 struct socket *so = in6p->in6p_socket; 143 struct in6pcb *head = in6p->in6p_head; 144 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 145 u_int16_t lport = 0; 146 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 147 int error; 148 149 if (in6p->in6p_lport || !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 150 return(EINVAL); 151 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 && 152 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 || 153 (so->so_options & SO_ACCEPTCONN) == 0)) 154 wild = IN6PLOOKUP_WILDCARD; 155 if (nam) { 156 sin6 = mtod(nam, struct sockaddr_in6 *); 157 if (nam->m_len != sizeof(*sin6)) 158 return(EINVAL); 159 /* 160 * We should check the family, but old programs 161 * incorrectly fail to intialize it. 162 */ 163 if (sin6->sin6_family != AF_INET6) 164 return(EAFNOSUPPORT); 165 166 /* 167 * since we do not check port number duplicate with IPv4 space, 168 * we reject it at this moment. If we leave it, we make normal 169 * user to hijack port number from other users. 170 */ 171 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 172 return(EADDRNOTAVAIL); 173 174 /* 175 * If the scope of the destination is link-local, embed the 176 * interface index in the address. 177 */ 178 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) { 179 /* XXX boundary check is assumed to be already done. */ 180 /* XXX sin6_scope_id is weaker than advanced-api. */ 181 struct in6_pktinfo *pi; 182 if (in6p->in6p_outputopts && 183 (pi = in6p->in6p_outputopts->ip6po_pktinfo) && 184 pi->ipi6_ifindex) { 185 sin6->sin6_addr.s6_addr16[1] 186 = htons(pi->ipi6_ifindex); 187 } else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) 188 && in6p->in6p_moptions 189 && in6p->in6p_moptions->im6o_multicast_ifp) { 190 sin6->sin6_addr.s6_addr16[1] = 191 htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index); 192 } else if (sin6->sin6_scope_id) { 193 /* boundary check */ 194 if (sin6->sin6_scope_id < 0 195 || if_index < sin6->sin6_scope_id) { 196 return ENXIO; /* XXX EINVAL? */ 197 } 198 sin6->sin6_addr.s6_addr16[1] 199 = htons(sin6->sin6_scope_id & 0xffff);/*XXX*/ 200 /* this must be cleared for ifa_ifwithaddr() */ 201 sin6->sin6_scope_id = 0; 202 } 203 } 204 205 lport = sin6->sin6_port; 206 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 207 /* 208 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 209 * allow compepte duplication of binding if 210 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 211 * and a multicast address is bound on both 212 * new and duplicated sockets. 213 */ 214 if (so->so_options & SO_REUSEADDR) 215 reuseport = SO_REUSEADDR|SO_REUSEPORT; 216 } else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 217 struct sockaddr_in sin; 218 219 bzero(&sin, sizeof(sin)); 220 sin.sin_len = sizeof(sin); 221 sin.sin_family = AF_INET; 222 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr, 223 sizeof(sin.sin_addr)); 224 if (ifa_ifwithaddr((struct sockaddr *)&sin) == 0) 225 return EADDRNOTAVAIL; 226 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 227 struct ifaddr *ia = NULL; 228 229 sin6->sin6_port = 0; /* yech... */ 230 #if defined(NFAITH) && NFAITH > 0 231 if ((in6p->in6p_flags & IN6P_FAITH) == 0 232 && (ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) 233 #else 234 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) 235 #endif 236 return(EADDRNOTAVAIL); 237 238 /* 239 * XXX: bind to an anycast address might accidentally 240 * cause sending a packet with anycast source address. 241 */ 242 if (ia && 243 ((struct in6_ifaddr *)ia)->ia6_flags & 244 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 245 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 246 return(EADDRNOTAVAIL); 247 } 248 } 249 if (lport) { 250 #ifndef IPNOPRIVPORTS 251 /* GROSS */ 252 if (ntohs(lport) < IPV6PORT_RESERVED && 253 (p == 0 || 254 (error = suser(p->p_ucred, &p->p_acflag)))) 255 return(EACCES); 256 #endif 257 258 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 259 /* should check this but we can't ... */ 260 #if 0 261 struct inpcb *t; 262 263 t = in_pcblookup_bind(&tcbtable, 264 (struct in_addr *)&sin6->sin6_addr.s6_addr32[3], 265 lport); 266 if (t && (reuseport & t->inp_socket->so_options) == 0) 267 return EADDRINUSE; 268 #endif 269 } else { 270 struct in6pcb *t; 271 272 t = in6_pcblookup(head, &zeroin6_addr, 0, 273 &sin6->sin6_addr, lport, wild); 274 if (t && (reuseport & t->in6p_socket->so_options) == 0) 275 return(EADDRINUSE); 276 } 277 } 278 in6p->in6p_laddr = sin6->sin6_addr; 279 } 280 281 if (lport == 0) { 282 int e; 283 if ((e = in6_pcbsetport(&in6p->in6p_laddr, in6p)) != 0) 284 return(e); 285 } 286 else 287 in6p->in6p_lport = lport; 288 289 in6p->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0; /*XXX*/ 290 return(0); 291 } 292 293 /* 294 * Connect from a socket to a specified address. 295 * Both address and port must be specified in argument sin6. 296 * If don't have a local address for this socket yet, 297 * then pick one. 298 */ 299 int 300 in6_pcbconnect(in6p, nam) 301 struct in6pcb *in6p; 302 struct mbuf *nam; 303 { 304 struct in6_addr *in6a = NULL; 305 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *); 306 struct in6_pktinfo *pi; 307 struct ifnet *ifp = NULL; /* outgoing interface */ 308 int error = 0; 309 struct in6_addr mapped; 310 311 (void)&in6a; /* XXX fool gcc */ 312 313 if (nam->m_len != sizeof(*sin6)) 314 return(EINVAL); 315 if (sin6->sin6_family != AF_INET6) 316 return(EAFNOSUPPORT); 317 if (sin6->sin6_port == 0) 318 return(EADDRNOTAVAIL); 319 320 /* sanity check for mapped address case */ 321 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 322 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 323 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff); 324 if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) 325 return EINVAL; 326 } else { 327 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) 328 return EINVAL; 329 } 330 331 /* 332 * If the scope of the destination is link-local, embed the interface 333 * index in the address. 334 */ 335 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) { 336 /* XXX boundary check is assumed to be already done. */ 337 /* XXX sin6_scope_id is weaker than advanced-api. */ 338 if (in6p->in6p_outputopts && 339 (pi = in6p->in6p_outputopts->ip6po_pktinfo) && 340 pi->ipi6_ifindex) { 341 sin6->sin6_addr.s6_addr16[1] = htons(pi->ipi6_ifindex); 342 ifp = ifindex2ifnet[pi->ipi6_ifindex]; 343 } 344 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) && 345 in6p->in6p_moptions && 346 in6p->in6p_moptions->im6o_multicast_ifp) { 347 sin6->sin6_addr.s6_addr16[1] = 348 htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index); 349 ifp = ifindex2ifnet[in6p->in6p_moptions->im6o_multicast_ifp->if_index]; 350 } else if (sin6->sin6_scope_id) { 351 /* boundary check */ 352 if (sin6->sin6_scope_id < 0 353 || if_index < sin6->sin6_scope_id) { 354 return ENXIO; /* XXX EINVAL? */ 355 } 356 sin6->sin6_addr.s6_addr16[1] 357 = htons(sin6->sin6_scope_id & 0xffff);/*XXX*/ 358 ifp = ifindex2ifnet[sin6->sin6_scope_id]; 359 } 360 } 361 362 /* Source address selection. */ 363 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) 364 && in6p->in6p_laddr.s6_addr32[3] == 0) { 365 struct sockaddr_in sin, *sinp; 366 367 bzero(&sin, sizeof(sin)); 368 sin.sin_len = sizeof(sin); 369 sin.sin_family = AF_INET; 370 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr, 371 sizeof(sin.sin_addr)); 372 sinp = in_selectsrc(&sin, (struct route *)&in6p->in6p_route, 373 in6p->in6p_socket->so_options, NULL, &error); 374 if (sinp == 0) { 375 if (error == 0) 376 error = EADDRNOTAVAIL; 377 return(error); 378 } 379 bzero(&mapped, sizeof(mapped)); 380 mapped.s6_addr16[5] = htons(0xffff); 381 bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr)); 382 in6a = &mapped; 383 } else { 384 /* 385 * XXX: in6_selectsrc might replace the bound local address 386 * with the address specified by setsockopt(IPV6_PKTINFO). 387 * Is it the intended behavior? 388 */ 389 in6a = in6_selectsrc(sin6, in6p->in6p_outputopts, 390 in6p->in6p_moptions, 391 &in6p->in6p_route, 392 &in6p->in6p_laddr, &error); 393 if (in6a == 0) { 394 if (error == 0) 395 error = EADDRNOTAVAIL; 396 return(error); 397 } 398 } 399 if (in6p->in6p_route.ro_rt) 400 ifp = in6p->in6p_route.ro_rt->rt_ifp; 401 402 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp); 403 404 if (in6_pcblookup(in6p->in6p_head, 405 &sin6->sin6_addr, 406 sin6->sin6_port, 407 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ? 408 in6a : &in6p->in6p_laddr, 409 in6p->in6p_lport, 410 0)) 411 return(EADDRINUSE); 412 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) 413 || (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) 414 && in6p->in6p_laddr.s6_addr32[3] == 0)) { 415 if (in6p->in6p_lport == 0) 416 (void)in6_pcbbind(in6p, (struct mbuf *)0, 417 (struct proc *)0); 418 in6p->in6p_laddr = *in6a; 419 } 420 in6p->in6p_faddr = sin6->sin6_addr; 421 in6p->in6p_fport = sin6->sin6_port; 422 /* 423 * xxx kazu flowlabel is necessary for connect? 424 * but if this line is missing, the garbage value remains. 425 */ 426 in6p->in6p_flowinfo = sin6->sin6_flowinfo; 427 return(0); 428 } 429 430 void 431 in6_pcbdisconnect(in6p) 432 struct in6pcb *in6p; 433 { 434 bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr)); 435 in6p->in6p_fport = 0; 436 if (in6p->in6p_socket->so_state & SS_NOFDREF) 437 in6_pcbdetach(in6p); 438 } 439 440 void 441 in6_pcbdetach(in6p) 442 struct in6pcb *in6p; 443 { 444 struct socket *so = in6p->in6p_socket; 445 446 #ifdef IPSEC 447 ipsec6_delete_pcbpolicy(in6p); 448 #endif /* IPSEC */ 449 sotoin6pcb(so) = 0; 450 sofree(so); 451 if (in6p->in6p_options) 452 m_freem(in6p->in6p_options); 453 if (in6p->in6p_outputopts) { 454 if (in6p->in6p_outputopts->ip6po_rthdr && 455 in6p->in6p_outputopts->ip6po_route.ro_rt) 456 RTFREE(in6p->in6p_outputopts->ip6po_route.ro_rt); 457 if (in6p->in6p_outputopts->ip6po_m) 458 (void)m_free(in6p->in6p_outputopts->ip6po_m); 459 free(in6p->in6p_outputopts, M_IP6OPT); 460 } 461 if (in6p->in6p_route.ro_rt) 462 rtfree(in6p->in6p_route.ro_rt); 463 ip6_freemoptions(in6p->in6p_moptions); 464 in6p->in6p_next->in6p_prev = in6p->in6p_prev; 465 in6p->in6p_prev->in6p_next = in6p->in6p_next; 466 in6p->in6p_prev = NULL; 467 FREE(in6p, M_PCB); 468 } 469 470 void 471 in6_setsockaddr(in6p, nam) 472 struct in6pcb *in6p; 473 struct mbuf *nam; 474 { 475 struct sockaddr_in6 *sin6; 476 477 nam->m_len = sizeof(*sin6); 478 sin6 = mtod(nam, struct sockaddr_in6 *); 479 bzero((caddr_t)sin6, sizeof(*sin6)); 480 sin6->sin6_family = AF_INET6; 481 sin6->sin6_len = sizeof(struct sockaddr_in6); 482 sin6->sin6_port = in6p->in6p_lport; 483 sin6->sin6_addr = in6p->in6p_laddr; 484 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 485 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 486 else 487 sin6->sin6_scope_id = 0; /*XXX*/ 488 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 489 sin6->sin6_addr.s6_addr16[1] = 0; 490 } 491 492 void 493 in6_setpeeraddr(in6p, nam) 494 struct in6pcb *in6p; 495 struct mbuf *nam; 496 { 497 struct sockaddr_in6 *sin6; 498 499 nam->m_len = sizeof(*sin6); 500 sin6 = mtod(nam, struct sockaddr_in6 *); 501 bzero((caddr_t)sin6, sizeof(*sin6)); 502 sin6->sin6_family = AF_INET6; 503 sin6->sin6_len = sizeof(struct sockaddr_in6); 504 sin6->sin6_port = in6p->in6p_fport; 505 sin6->sin6_addr = in6p->in6p_faddr; 506 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 507 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 508 else 509 sin6->sin6_scope_id = 0; /*XXX*/ 510 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 511 sin6->sin6_addr.s6_addr16[1] = 0; 512 } 513 514 /* 515 * Pass some notification to all connections of a protocol 516 * associated with address dst. The local address and/or port numbers 517 * may be specified to limit the search. The "usual action" will be 518 * taken, depending on the ctlinput cmd. The caller must filter any 519 * cmds that are uninteresting (e.g., no error in the map). 520 * Call the protocol specific routine (if any) to report 521 * any errors for each matching socket. 522 * 523 * Must be called at splsoftnet. 524 */ 525 int 526 in6_pcbnotify(head, dst, fport_arg, laddr6, lport_arg, cmd, notify) 527 struct in6pcb *head; 528 struct sockaddr *dst; 529 u_int fport_arg, lport_arg; 530 struct in6_addr *laddr6; 531 int cmd; 532 void (*notify) __P((struct in6pcb *, int)); 533 { 534 struct in6pcb *in6p, *nin6p; 535 struct in6_addr faddr6; 536 u_int16_t fport = fport_arg, lport = lport_arg; 537 int errno; 538 int nmatch = 0; 539 void (*notify2) __P((struct in6pcb *, int)); 540 541 notify2 = NULL; 542 543 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6) 544 return 0; 545 faddr6 = ((struct sockaddr_in6 *)dst)->sin6_addr; 546 if (IN6_IS_ADDR_UNSPECIFIED(&faddr6)) 547 return 0; 548 549 /* 550 * Redirects go to all references to the destination, 551 * and use in6_rtchange to invalidate the route cache. 552 * Dead host indications: also use in6_rtchange to invalidate 553 * the cache, and deliver the error to all the sockets. 554 * Otherwise, if we have knowledge of the local port and address, 555 * deliver only to that socket. 556 */ 557 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 558 fport = 0; 559 lport = 0; 560 bzero((caddr_t)laddr6, sizeof(*laddr6)); 561 562 /* 563 * Keep the old notify function to store a soft error 564 * in each PCB. 565 */ 566 if (cmd == PRC_HOSTDEAD && notify != in6_rtchange) 567 notify2 = notify; 568 569 notify = in6_rtchange; 570 } 571 572 if (notify == NULL) 573 return 0; 574 575 errno = inet6ctlerrmap[cmd]; 576 for (in6p = head->in6p_next; in6p != head; in6p = nin6p) { 577 nin6p = in6p->in6p_next; 578 579 if (notify == in6_rtchange) { 580 /* 581 * Since a non-connected PCB might have a cached route, 582 * we always call in6_rtchange without matching 583 * the PCB to the src/dst pair. 584 * 585 * XXX: we assume in6_rtchange does not free the PCB. 586 */ 587 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_route.ro_dst.sin6_addr, 588 &faddr6)) 589 in6_rtchange(in6p, errno); 590 591 if (notify2 == NULL) 592 continue; 593 594 notify = notify2; 595 } 596 597 /* at this point, we can assume that NOTIFY is not NULL. */ 598 599 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &faddr6) || 600 in6p->in6p_socket == 0 || 601 (lport && in6p->in6p_lport != lport) || 602 (!IN6_IS_ADDR_UNSPECIFIED(laddr6) && 603 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) || 604 (fport && in6p->in6p_fport != fport)) 605 continue; 606 607 (*notify)(in6p, errno); 608 nmatch++; 609 } 610 return nmatch; 611 } 612 613 void 614 in6_pcbpurgeif(head, ifp) 615 struct in6pcb *head; 616 struct ifnet *ifp; 617 { 618 struct in6pcb *in6p, *nin6p; 619 620 for (in6p = head->in6p_next; in6p != head; in6p = nin6p) { 621 nin6p = in6p->in6p_next; 622 if (in6p->in6p_route.ro_rt != NULL && 623 in6p->in6p_route.ro_rt->rt_ifp == ifp) 624 in6_rtchange(in6p, 0); 625 } 626 } 627 628 /* 629 * Check for alternatives when higher level complains 630 * about service problems. For now, invalidate cached 631 * routing information. If the route was created dynamically 632 * (by a redirect), time to try a default gateway again. 633 */ 634 void 635 in6_losing(in6p) 636 struct in6pcb *in6p; 637 { 638 struct rtentry *rt; 639 struct rt_addrinfo info; 640 641 if ((rt = in6p->in6p_route.ro_rt) != NULL) { 642 in6p->in6p_route.ro_rt = 0; 643 bzero((caddr_t)&info, sizeof(info)); 644 info.rti_info[RTAX_DST] = 645 (struct sockaddr *)&in6p->in6p_route.ro_dst; 646 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 647 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 648 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 649 if (rt->rt_flags & RTF_DYNAMIC) 650 (void)rtrequest(RTM_DELETE, rt_key(rt), 651 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 652 (struct rtentry **)0); 653 else 654 /* 655 * A new route can be allocated 656 * the next time output is attempted. 657 */ 658 rtfree(rt); 659 } 660 } 661 662 /* 663 * After a routing change, flush old routing 664 * and allocate a (hopefully) better one. 665 */ 666 void 667 in6_rtchange(in6p, errno) 668 struct in6pcb *in6p; 669 int errno; 670 { 671 if (in6p->in6p_route.ro_rt) { 672 rtfree(in6p->in6p_route.ro_rt); 673 in6p->in6p_route.ro_rt = 0; 674 /* 675 * A new route can be allocated the next time 676 * output is attempted. 677 */ 678 } 679 } 680 681 struct in6pcb * 682 in6_pcblookup(head, faddr6, fport_arg, laddr6, lport_arg, flags) 683 struct in6pcb *head; 684 struct in6_addr *faddr6, *laddr6; 685 u_int fport_arg, lport_arg; 686 int flags; 687 { 688 struct in6pcb *in6p, *match = 0; 689 int matchwild = 3, wildcard; 690 u_int16_t fport = fport_arg, lport = lport_arg; 691 692 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) { 693 if (in6p->in6p_lport != lport) 694 continue; 695 wildcard = 0; 696 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 697 if (IN6_IS_ADDR_UNSPECIFIED(laddr6)) 698 wildcard++; 699 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) 700 continue; 701 } 702 #ifndef TCP6 703 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) 704 && in6p->in6p_laddr.s6_addr32[3] == 0) { 705 if (!IN6_IS_ADDR_V4MAPPED(laddr6)) 706 continue; 707 if (laddr6->s6_addr32[3] == 0) 708 ; 709 else 710 wildcard++; 711 } 712 #endif 713 else { 714 if (IN6_IS_ADDR_V4MAPPED(laddr6)) { 715 #if !defined(TCP6) && !defined(INET6_BINDV6ONLY) 716 if (in6p->in6p_flags & IN6P_BINDV6ONLY) 717 continue; 718 else 719 wildcard++; 720 #else 721 continue; 722 #endif 723 } else if (!IN6_IS_ADDR_UNSPECIFIED(laddr6)) 724 wildcard++; 725 } 726 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 727 if (IN6_IS_ADDR_UNSPECIFIED(faddr6)) 728 wildcard++; 729 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6) 730 || in6p->in6p_fport != fport) 731 continue; 732 } 733 #ifndef TCP6 734 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) 735 && in6p->in6p_faddr.s6_addr32[3] == 0) { 736 if (!IN6_IS_ADDR_V4MAPPED(faddr6)) 737 continue; 738 if (faddr6->s6_addr32[3] == 0) 739 ; 740 else 741 wildcard++; 742 } 743 #endif 744 else { 745 if (IN6_IS_ADDR_V4MAPPED(faddr6)) { 746 #if !defined(TCP6) && !defined(INET6_BINDV6ONLY) 747 if (in6p->in6p_flags & IN6P_BINDV6ONLY) 748 continue; 749 else 750 wildcard++; 751 #else 752 continue; 753 #endif 754 } else if (!IN6_IS_ADDR_UNSPECIFIED(faddr6)) 755 wildcard++; 756 } 757 758 if (wildcard && (flags & IN6PLOOKUP_WILDCARD) == 0) 759 continue; 760 if (wildcard < matchwild) { 761 match = in6p; 762 matchwild = wildcard; 763 if (matchwild == 0) 764 break; 765 } 766 } 767 return(match); 768 } 769 770 #ifndef TCP6 771 struct rtentry * 772 in6_pcbrtentry(in6p) 773 struct in6pcb *in6p; 774 { 775 struct route_in6 *ro; 776 777 ro = &in6p->in6p_route; 778 779 if (ro->ro_rt == NULL) { 780 /* 781 * No route yet, so try to acquire one. 782 */ 783 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 784 bzero(&ro->ro_dst, sizeof(ro->ro_dst)); 785 ro->ro_dst.sin6_family = AF_INET6; 786 ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6); 787 satosin6(&ro->ro_dst)->sin6_addr = in6p->in6p_faddr; 788 rtalloc((struct route *)ro); 789 } 790 } 791 return (ro->ro_rt); 792 } 793 794 struct in6pcb * 795 in6_pcblookup_connect(head, faddr6, fport_arg, laddr6, lport_arg, faith) 796 struct in6pcb *head; 797 struct in6_addr *faddr6, *laddr6; 798 u_int fport_arg, lport_arg; 799 int faith; 800 { 801 struct in6pcb *in6p; 802 u_int16_t fport = fport_arg, lport = lport_arg; 803 804 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) { 805 #if defined(NFAITH) && NFAITH > 0 806 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) 807 continue; 808 #endif 809 /* find exact match on both source and dest */ 810 if (in6p->in6p_fport != fport) 811 continue; 812 if (in6p->in6p_lport != lport) 813 continue; 814 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) 815 continue; 816 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)) 817 continue; 818 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 819 continue; 820 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) 821 continue; 822 return in6p; 823 } 824 return NULL; 825 } 826 827 struct in6pcb * 828 in6_pcblookup_bind(head, laddr6, lport_arg, faith) 829 struct in6pcb *head; 830 struct in6_addr *laddr6; 831 u_int lport_arg; 832 int faith; 833 { 834 struct in6pcb *in6p, *match; 835 u_int16_t lport = lport_arg; 836 837 match = NULL; 838 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) { 839 /* 840 * find destination match. exact match is preferred 841 * against wildcard match. 842 */ 843 #if defined(NFAITH) && NFAITH > 0 844 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) 845 continue; 846 #endif 847 if (in6p->in6p_fport != 0) 848 continue; 849 if (in6p->in6p_lport != lport) 850 continue; 851 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 852 if (IN6_IS_ADDR_V4MAPPED(laddr6)) { 853 #ifndef INET6_BINDV6ONLY 854 if (in6p->in6p_flags & IN6P_BINDV6ONLY) 855 continue; 856 else 857 match = in6p; 858 #else 859 continue; 860 #endif 861 } else 862 match = in6p; 863 } 864 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) && 865 in6p->in6p_laddr.s6_addr32[3] == 0) { 866 if (IN6_IS_ADDR_V4MAPPED(laddr6) && 867 laddr6->s6_addr32[3] != 0) 868 match = in6p; 869 } 870 else if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) 871 return in6p; 872 } 873 return match; 874 } 875 #endif 876