1 /* $NetBSD: in6_pcb.c,v 1.21 2000/03/02 07:15:39 itojun Exp $ */ 2 /* $KAME: in6_pcb.c,v 1.35 2000/03/02 06:32:18 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) 138 register struct in6pcb *in6p; 139 struct mbuf *nam; 140 { 141 struct socket *so = in6p->in6p_socket; 142 struct in6pcb *head = in6p->in6p_head; 143 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 144 struct proc *p = curproc; /* XXX */ 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 * If the scope of the destination is link-local, embed the 168 * interface index in the address. 169 */ 170 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) { 171 /* XXX boundary check is assumed to be already done. */ 172 /* XXX sin6_scope_id is weaker than advanced-api. */ 173 struct in6_pktinfo *pi; 174 if (in6p->in6p_outputopts && 175 (pi = in6p->in6p_outputopts->ip6po_pktinfo) && 176 pi->ipi6_ifindex) { 177 sin6->sin6_addr.s6_addr16[1] 178 = htons(pi->ipi6_ifindex); 179 } else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) 180 && in6p->in6p_moptions 181 && in6p->in6p_moptions->im6o_multicast_ifp) { 182 sin6->sin6_addr.s6_addr16[1] = 183 htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index); 184 } else if (sin6->sin6_scope_id) { 185 /* boundary check */ 186 if (sin6->sin6_scope_id < 0 187 || if_index < sin6->sin6_scope_id) { 188 return ENXIO; /* XXX EINVAL? */ 189 } 190 sin6->sin6_addr.s6_addr16[1] 191 = htons(sin6->sin6_scope_id & 0xffff);/*XXX*/ 192 /* this must be cleared for ifa_ifwithaddr() */ 193 sin6->sin6_scope_id = 0; 194 } 195 } 196 197 lport = sin6->sin6_port; 198 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 199 /* 200 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 201 * allow compepte duplication of binding if 202 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 203 * and a multicast address is bound on both 204 * new and duplicated sockets. 205 */ 206 if (so->so_options & SO_REUSEADDR) 207 reuseport = SO_REUSEADDR|SO_REUSEPORT; 208 } else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 209 struct sockaddr_in sin; 210 211 bzero(&sin, sizeof(sin)); 212 sin.sin_len = sizeof(sin); 213 sin.sin_family = AF_INET; 214 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr, 215 sizeof(sin.sin_addr)); 216 if (ifa_ifwithaddr((struct sockaddr *)&sin) == 0) 217 return EADDRNOTAVAIL; 218 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 219 struct ifaddr *ia = NULL; 220 221 sin6->sin6_port = 0; /* yech... */ 222 #if defined(NFAITH) && NFAITH > 0 223 if ((in6p->in6p_flags & IN6P_FAITH) == 0 224 && (ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) 225 #else 226 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) 227 #endif 228 return(EADDRNOTAVAIL); 229 230 /* 231 * XXX: bind to an anycast address might accidentally 232 * cause sending a packet with anycast source address. 233 */ 234 if (ia && 235 ((struct in6_ifaddr *)ia)->ia6_flags & 236 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 237 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 238 return(EADDRNOTAVAIL); 239 } 240 } 241 if (lport) { 242 #ifndef IPNOPRIVPORTS 243 /* GROSS */ 244 if (ntohs(lport) < IPV6PORT_RESERVED && 245 (p == 0 || 246 (error = suser(p->p_ucred, &p->p_acflag)))) 247 return(EACCES); 248 #endif 249 250 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 251 /* should check this but we can't ... */ 252 #if 0 253 struct inpcb *t; 254 255 t = in_pcblookup_bind(&tcbtable, 256 (struct in_addr *)&sin6->sin6_addr.s6_addr32[3], 257 lport); 258 if (t && (reuseport & t->inp_socket->so_options) == 0) 259 return EADDRINUSE; 260 #endif 261 } else { 262 struct in6pcb *t; 263 264 t = in6_pcblookup(head, &zeroin6_addr, 0, 265 &sin6->sin6_addr, lport, wild); 266 if (t && (reuseport & t->in6p_socket->so_options) == 0) 267 return(EADDRINUSE); 268 } 269 } 270 in6p->in6p_laddr = sin6->sin6_addr; 271 } 272 273 if (lport == 0) { 274 int e; 275 if ((e = in6_pcbsetport(&in6p->in6p_laddr, in6p)) != 0) 276 return(e); 277 } 278 else 279 in6p->in6p_lport = lport; 280 281 in6p->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0; /*XXX*/ 282 return(0); 283 } 284 285 /* 286 * Find an empty port and set it to the specified PCB. 287 */ 288 int 289 in6_pcbsetport(laddr, in6p) 290 struct in6_addr *laddr; 291 struct in6pcb *in6p; 292 { 293 struct socket *so = in6p->in6p_socket; 294 struct in6pcb *head = in6p->in6p_head; 295 u_int16_t last_port, lport = 0; 296 int wild = 0; 297 void *t; 298 u_int16_t min, max; 299 #ifndef IPNOPRIVPORTS 300 struct proc *p = curproc; /*XXX*/ 301 #endif 302 303 /* XXX: this is redundant when called from in6_pcbbind */ 304 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 && 305 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 || 306 (so->so_options & SO_ACCEPTCONN) == 0)) 307 wild = IN6PLOOKUP_WILDCARD; 308 309 if (in6p->in6p_flags & IN6P_LOWPORT) { 310 #ifndef IPNOPRIVPORTS 311 if (p == 0 || (suser(p->p_ucred, &p->p_acflag) != 0)) 312 return (EACCES); 313 #endif 314 min = IPV6PORT_RESERVEDMIN; 315 max = IPV6PORT_RESERVEDMAX; 316 } else { 317 min = IPV6PORT_ANONMIN; 318 max = IPV6PORT_ANONMAX; 319 } 320 321 /* value out of range */ 322 if (head->in6p_lport < min) 323 head->in6p_lport = min; 324 else if (head->in6p_lport > max) 325 head->in6p_lport = min; 326 last_port = head->in6p_lport; 327 goto startover; /*to randomize*/ 328 for (;;) { 329 lport = htons(head->in6p_lport); 330 if (IN6_IS_ADDR_V4MAPPED(laddr)) { 331 #if 0 332 t = in_pcblookup_bind(&tcbtable, 333 (struct in_addr *)&in6p->in6p_laddr.s6_addr32[3], 334 lport); 335 #else 336 t = NULL; 337 #endif 338 } else { 339 t = in6_pcblookup(head, &zeroin6_addr, 0, laddr, 340 lport, wild); 341 } 342 if (t == 0) 343 break; 344 startover: 345 if (head->in6p_lport >= max) 346 head->in6p_lport = min; 347 else 348 head->in6p_lport++; 349 if (head->in6p_lport == last_port) 350 return (EADDRINUSE); 351 } 352 353 in6p->in6p_lport = lport; 354 return(0); /* success */ 355 } 356 357 /* 358 * Connect from a socket to a specified address. 359 * Both address and port must be specified in argument sin6. 360 * If don't have a local address for this socket yet, 361 * then pick one. 362 */ 363 int 364 in6_pcbconnect(in6p, nam) 365 struct in6pcb *in6p; 366 struct mbuf *nam; 367 { 368 struct in6_addr *in6a = NULL; 369 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *); 370 struct in6_pktinfo *pi; 371 struct ifnet *ifp = NULL; /* outgoing interface */ 372 int error = 0; 373 struct in6_addr mapped; 374 375 (void)&in6a; /* XXX fool gcc */ 376 377 if (nam->m_len != sizeof(*sin6)) 378 return(EINVAL); 379 if (sin6->sin6_family != AF_INET6) 380 return(EAFNOSUPPORT); 381 if (sin6->sin6_port == 0) 382 return(EADDRNOTAVAIL); 383 384 /* sanity check for mapped address case */ 385 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 386 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 387 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff); 388 if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) 389 return EINVAL; 390 } else { 391 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) 392 return EINVAL; 393 } 394 395 /* 396 * If the scope of the destination is link-local, embed the interface 397 * index in the address. 398 */ 399 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) { 400 /* XXX boundary check is assumed to be already done. */ 401 /* XXX sin6_scope_id is weaker than advanced-api. */ 402 if (in6p->in6p_outputopts && 403 (pi = in6p->in6p_outputopts->ip6po_pktinfo) && 404 pi->ipi6_ifindex) { 405 sin6->sin6_addr.s6_addr16[1] = htons(pi->ipi6_ifindex); 406 ifp = ifindex2ifnet[pi->ipi6_ifindex]; 407 } 408 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) && 409 in6p->in6p_moptions && 410 in6p->in6p_moptions->im6o_multicast_ifp) { 411 sin6->sin6_addr.s6_addr16[1] = 412 htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index); 413 ifp = ifindex2ifnet[in6p->in6p_moptions->im6o_multicast_ifp->if_index]; 414 } else if (sin6->sin6_scope_id) { 415 /* boundary check */ 416 if (sin6->sin6_scope_id < 0 417 || if_index < sin6->sin6_scope_id) { 418 return ENXIO; /* XXX EINVAL? */ 419 } 420 sin6->sin6_addr.s6_addr16[1] 421 = htons(sin6->sin6_scope_id & 0xffff);/*XXX*/ 422 ifp = ifindex2ifnet[sin6->sin6_scope_id]; 423 } 424 } 425 426 /* Source address selection. */ 427 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) 428 && in6p->in6p_laddr.s6_addr32[3] == 0) { 429 struct sockaddr_in sin, *sinp; 430 431 bzero(&sin, sizeof(sin)); 432 sin.sin_len = sizeof(sin); 433 sin.sin_family = AF_INET; 434 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr, 435 sizeof(sin.sin_addr)); 436 sinp = in_selectsrc(&sin, (struct route *)&in6p->in6p_route, 437 in6p->in6p_socket->so_options, NULL, &error); 438 if (sinp == 0) { 439 if (error == 0) 440 error = EADDRNOTAVAIL; 441 return(error); 442 } 443 bzero(&mapped, sizeof(mapped)); 444 mapped.s6_addr16[5] = htons(0xffff); 445 bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr)); 446 in6a = &mapped; 447 } else { 448 /* 449 * XXX: in6_selectsrc might replace the bound local address 450 * with the address specified by setsockopt(IPV6_PKTINFO). 451 * Is it the intended behavior? 452 */ 453 in6a = in6_selectsrc(sin6, in6p->in6p_outputopts, 454 in6p->in6p_moptions, 455 &in6p->in6p_route, 456 &in6p->in6p_laddr, &error); 457 if (in6a == 0) { 458 if (error == 0) 459 error = EADDRNOTAVAIL; 460 return(error); 461 } 462 } 463 if (in6p->in6p_route.ro_rt) 464 ifp = in6p->in6p_route.ro_rt->rt_ifp; 465 466 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp); 467 468 if (in6_pcblookup(in6p->in6p_head, 469 &sin6->sin6_addr, 470 sin6->sin6_port, 471 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ? 472 in6a : &in6p->in6p_laddr, 473 in6p->in6p_lport, 474 0)) 475 return(EADDRINUSE); 476 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) 477 || (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) 478 && in6p->in6p_laddr.s6_addr32[3] == 0)) { 479 if (in6p->in6p_lport == 0) 480 (void)in6_pcbbind(in6p, (struct mbuf *)0); 481 in6p->in6p_laddr = *in6a; 482 } 483 in6p->in6p_faddr = sin6->sin6_addr; 484 in6p->in6p_fport = sin6->sin6_port; 485 /* 486 * xxx kazu flowlabel is necessary for connect? 487 * but if this line is missing, the garbage value remains. 488 */ 489 in6p->in6p_flowinfo = sin6->sin6_flowinfo; 490 return(0); 491 } 492 493 /* 494 * Return an IPv6 address, which is the most appropriate for given 495 * destination and user specified options. 496 * If necessary, this function lookups the routing table and return 497 * an entry to the caller for later use. 498 */ 499 struct in6_addr * 500 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp) 501 struct sockaddr_in6 *dstsock; 502 struct ip6_pktopts *opts; 503 struct ip6_moptions *mopts; 504 struct route_in6 *ro; 505 struct in6_addr *laddr; 506 int *errorp; 507 { 508 struct in6_addr *dst; 509 struct in6_ifaddr *ia6 = 0; 510 struct in6_pktinfo *pi = NULL; 511 512 dst = &dstsock->sin6_addr; 513 *errorp = 0; 514 515 /* 516 * If the source address is explicitly specified by the caller, 517 * use it. 518 */ 519 if (opts && (pi = opts->ip6po_pktinfo) && 520 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) 521 return(&pi->ipi6_addr); 522 523 /* 524 * If the source address is not specified but the socket(if any) 525 * is already bound, use the bound address. 526 */ 527 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) 528 return(laddr); 529 530 /* 531 * If the caller doesn't specify the source address but 532 * the outgoing interface, use an address associated with 533 * the interface. 534 */ 535 if (pi && pi->ipi6_ifindex) { 536 /* XXX boundary check is assumed to be already done. */ 537 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex], 538 dst); 539 if (ia6 == 0) { 540 *errorp = EADDRNOTAVAIL; 541 return(0); 542 } 543 return(&satosin6(&ia6->ia_addr)->sin6_addr); 544 } 545 546 /* 547 * If the destination address is a link-local unicast address or 548 * a multicast address, and if the outgoing interface is specified 549 * by the sin6_scope_id filed, use an address associated with the 550 * interface. 551 * XXX: We're now trying to define more specific semantics of 552 * sin6_scope_id field, so this part will be rewritten in 553 * the near future. 554 */ 555 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) && 556 dstsock->sin6_scope_id) { 557 /* 558 * I'm not sure if boundary check for scope_id is done 559 * somewhere... 560 */ 561 if (dstsock->sin6_scope_id < 0 || 562 if_index < dstsock->sin6_scope_id) { 563 *errorp = ENXIO; /* XXX: better error? */ 564 return(0); 565 } 566 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id], 567 dst); 568 if (ia6 == 0) { 569 *errorp = EADDRNOTAVAIL; 570 return(0); 571 } 572 return(&satosin6(&ia6->ia_addr)->sin6_addr); 573 } 574 575 /* 576 * If the destination address is a multicast address and 577 * the outgoing interface for the address is specified 578 * by the caller, use an address associated with the interface. 579 * There is a sanity check here; if the destination has node-local 580 * scope, the outgoing interfacde should be a loopback address. 581 * Even if the outgoing interface is not specified, we also 582 * choose a loopback interface as the outgoing interface. 583 */ 584 if (IN6_IS_ADDR_MULTICAST(dst)) { 585 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL; 586 587 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) { 588 ifp = &loif[0]; 589 } 590 591 if (ifp) { 592 ia6 = in6_ifawithscope(ifp, dst); 593 if (ia6 == 0) { 594 *errorp = EADDRNOTAVAIL; 595 return(0); 596 } 597 return(&satosin6(&ia6->ia_addr)->sin6_addr); 598 } 599 } 600 601 /* 602 * If the next hop address for the packet is specified 603 * by caller, use an address associated with the route 604 * to the next hop. 605 */ 606 { 607 struct sockaddr_in6 *sin6_next; 608 struct rtentry *rt; 609 610 if (opts && opts->ip6po_nexthop) { 611 sin6_next = satosin6(opts->ip6po_nexthop); 612 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL); 613 if (rt) { 614 ia6 = in6_ifawithscope(rt->rt_ifp, dst); 615 if (ia6 == 0) 616 ia6 = ifatoia6(rt->rt_ifa); 617 } 618 if (ia6 == 0) { 619 *errorp = EADDRNOTAVAIL; 620 return(0); 621 } 622 return(&satosin6(&ia6->ia_addr)->sin6_addr); 623 } 624 } 625 626 /* 627 * If route is known or can be allocated now, 628 * our src addr is taken from the i/f, else punt. 629 */ 630 if (ro) { 631 if (ro->ro_rt && 632 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) { 633 RTFREE(ro->ro_rt); 634 ro->ro_rt = (struct rtentry *)0; 635 } 636 if (ro->ro_rt == (struct rtentry *)0 || 637 ro->ro_rt->rt_ifp == (struct ifnet *)0) { 638 /* No route yet, so try to acquire one */ 639 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 640 ro->ro_dst.sin6_family = AF_INET6; 641 ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6); 642 ro->ro_dst.sin6_addr = *dst; 643 if (IN6_IS_ADDR_MULTICAST(dst)) { 644 ro->ro_rt = rtalloc1(&((struct route *)ro) 645 ->ro_dst, 0); 646 } else { 647 rtalloc((struct route *)ro); 648 } 649 650 } 651 652 /* 653 * in_pcbconnect() checks out IFF_LOOPBACK to skip using 654 * the address. But we don't know why it does so. 655 * It is necessary to ensure the scope even for lo0 656 * so doesn't check out IFF_LOOPBACK. 657 */ 658 659 if (ro->ro_rt) { 660 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst); 661 if (ia6 == 0) /* xxx scope error ?*/ 662 ia6 = ifatoia6(ro->ro_rt->rt_ifa); 663 } 664 #if 0 665 /* 666 * xxx The followings are necessary? (kazu) 667 * I don't think so. 668 * It's for SO_DONTROUTE option in IPv4.(jinmei) 669 */ 670 if (ia6 == 0) { 671 struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0}; 672 673 sin6->sin6_addr = *dst; 674 675 ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6))); 676 if (ia6 == 0) 677 ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6))); 678 if (ia6 == 0) 679 return(0); 680 return(&satosin6(&ia6->ia_addr)->sin6_addr); 681 } 682 #endif /* 0 */ 683 if (ia6 == 0) { 684 *errorp = EHOSTUNREACH; /* no route */ 685 return(0); 686 } 687 return(&satosin6(&ia6->ia_addr)->sin6_addr); 688 } 689 690 *errorp = EADDRNOTAVAIL; 691 return(0); 692 } 693 694 /* 695 * Default hop limit selection. The precedence is as follows: 696 * 1. Hoplimit valued specified via ioctl. 697 * 2. (If the outgoing interface is detected) the current 698 * hop limit of the interface specified by router advertisement. 699 * 3. The system default hoplimit. 700 */ 701 int 702 in6_selecthlim(in6p, ifp) 703 struct in6pcb *in6p; 704 struct ifnet *ifp; 705 { 706 if (in6p && in6p->in6p_hops >= 0) 707 return(in6p->in6p_hops); 708 else if (ifp) 709 return(nd_ifinfo[ifp->if_index].chlim); 710 else 711 return(ip6_defhlim); 712 } 713 714 void 715 in6_pcbdisconnect(in6p) 716 struct in6pcb *in6p; 717 { 718 bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr)); 719 in6p->in6p_fport = 0; 720 if (in6p->in6p_socket->so_state & SS_NOFDREF) 721 in6_pcbdetach(in6p); 722 } 723 724 void 725 in6_pcbdetach(in6p) 726 struct in6pcb *in6p; 727 { 728 struct socket *so = in6p->in6p_socket; 729 730 #ifdef IPSEC 731 ipsec6_delete_pcbpolicy(in6p); 732 #endif /* IPSEC */ 733 sotoin6pcb(so) = 0; 734 sofree(so); 735 if (in6p->in6p_options) 736 m_freem(in6p->in6p_options); 737 if (in6p->in6p_outputopts) { 738 if (in6p->in6p_outputopts->ip6po_rthdr && 739 in6p->in6p_outputopts->ip6po_route.ro_rt) 740 RTFREE(in6p->in6p_outputopts->ip6po_route.ro_rt); 741 if (in6p->in6p_outputopts->ip6po_m) 742 (void)m_free(in6p->in6p_outputopts->ip6po_m); 743 free(in6p->in6p_outputopts, M_IP6OPT); 744 } 745 if (in6p->in6p_route.ro_rt) 746 rtfree(in6p->in6p_route.ro_rt); 747 ip6_freemoptions(in6p->in6p_moptions); 748 in6p->in6p_next->in6p_prev = in6p->in6p_prev; 749 in6p->in6p_prev->in6p_next = in6p->in6p_next; 750 in6p->in6p_prev = NULL; 751 FREE(in6p, M_PCB); 752 } 753 754 void 755 in6_setsockaddr(in6p, nam) 756 struct in6pcb *in6p; 757 struct mbuf *nam; 758 { 759 struct sockaddr_in6 *sin6; 760 761 nam->m_len = sizeof(*sin6); 762 sin6 = mtod(nam, struct sockaddr_in6 *); 763 bzero((caddr_t)sin6, sizeof(*sin6)); 764 sin6->sin6_family = AF_INET6; 765 sin6->sin6_len = sizeof(struct sockaddr_in6); 766 sin6->sin6_port = in6p->in6p_lport; 767 sin6->sin6_addr = in6p->in6p_laddr; 768 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 769 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 770 else 771 sin6->sin6_scope_id = 0; /*XXX*/ 772 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 773 sin6->sin6_addr.s6_addr16[1] = 0; 774 } 775 776 void 777 in6_setpeeraddr(in6p, nam) 778 struct in6pcb *in6p; 779 struct mbuf *nam; 780 { 781 struct sockaddr_in6 *sin6; 782 783 nam->m_len = sizeof(*sin6); 784 sin6 = mtod(nam, struct sockaddr_in6 *); 785 bzero((caddr_t)sin6, sizeof(*sin6)); 786 sin6->sin6_family = AF_INET6; 787 sin6->sin6_len = sizeof(struct sockaddr_in6); 788 sin6->sin6_port = in6p->in6p_fport; 789 sin6->sin6_addr = in6p->in6p_faddr; 790 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 791 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 792 else 793 sin6->sin6_scope_id = 0; /*XXX*/ 794 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 795 sin6->sin6_addr.s6_addr16[1] = 0; 796 } 797 798 /* 799 * Pass some notification to all connections of a protocol 800 * associated with address dst. The local address and/or port numbers 801 * may be specified to limit the search. The "usual action" will be 802 * taken, depending on the ctlinput cmd. The caller must filter any 803 * cmds that are uninteresting (e.g., no error in the map). 804 * Call the protocol specific routine (if any) to report 805 * any errors for each matching socket. 806 * 807 * Must be called at splsoftnet. 808 */ 809 int 810 in6_pcbnotify(head, dst, fport_arg, laddr6, lport_arg, cmd, notify) 811 struct in6pcb *head; 812 struct sockaddr *dst; 813 u_int fport_arg, lport_arg; 814 struct in6_addr *laddr6; 815 int cmd; 816 void (*notify) __P((struct in6pcb *, int)); 817 { 818 struct in6pcb *in6p, *nin6p; 819 struct in6_addr faddr6; 820 u_int16_t fport = fport_arg, lport = lport_arg; 821 int errno; 822 int nmatch = 0; 823 void (*notify2) __P((struct in6pcb *, int)); 824 825 notify2 = NULL; 826 827 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6) 828 return 0; 829 faddr6 = ((struct sockaddr_in6 *)dst)->sin6_addr; 830 if (IN6_IS_ADDR_UNSPECIFIED(&faddr6)) 831 return 0; 832 833 /* 834 * Redirects go to all references to the destination, 835 * and use in6_rtchange to invalidate the route cache. 836 * Dead host indications: also use in6_rtchange to invalidate 837 * the cache, and deliver the error to all the sockets. 838 * Otherwise, if we have knowledge of the local port and address, 839 * deliver only to that socket. 840 */ 841 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 842 fport = 0; 843 lport = 0; 844 bzero((caddr_t)laddr6, sizeof(*laddr6)); 845 846 /* 847 * Keep the old notify function to store a soft error 848 * in each PCB. 849 */ 850 if (cmd == PRC_HOSTDEAD && notify != in6_rtchange) 851 notify2 = notify; 852 853 notify = in6_rtchange; 854 } 855 856 if (notify == NULL) 857 return 0; 858 859 errno = inet6ctlerrmap[cmd]; 860 for (in6p = head->in6p_next; in6p != head; in6p = nin6p) { 861 nin6p = in6p->in6p_next; 862 863 if (notify == in6_rtchange) { 864 /* 865 * Since a non-connected PCB might have a cached route, 866 * we always call in6_rtchange without matching 867 * the PCB to the src/dst pair. 868 * 869 * XXX: we assume in6_rtchange does not free the PCB. 870 */ 871 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_route.ro_dst.sin6_addr, 872 &faddr6)) 873 in6_rtchange(in6p, errno); 874 875 if (notify2 == NULL) 876 continue; 877 878 notify = notify2; 879 } 880 881 /* at this point, we can assume that NOTIFY is not NULL. */ 882 883 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &faddr6) || 884 in6p->in6p_socket == 0 || 885 (lport && in6p->in6p_lport != lport) || 886 (!IN6_IS_ADDR_UNSPECIFIED(laddr6) && 887 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) || 888 (fport && in6p->in6p_fport != fport)) 889 continue; 890 891 (*notify)(in6p, errno); 892 nmatch++; 893 } 894 return nmatch; 895 } 896 897 void 898 in6_pcbpurgeif(head, ifp) 899 struct in6pcb *head; 900 struct ifnet *ifp; 901 { 902 struct in6pcb *in6p, *nin6p; 903 904 for (in6p = head->in6p_next; in6p != head; in6p = nin6p) { 905 nin6p = in6p->in6p_next; 906 if (in6p->in6p_route.ro_rt != NULL && 907 in6p->in6p_route.ro_rt->rt_ifp == ifp) 908 in6_rtchange(in6p, 0); 909 } 910 } 911 912 /* 913 * Check for alternatives when higher level complains 914 * about service problems. For now, invalidate cached 915 * routing information. If the route was created dynamically 916 * (by a redirect), time to try a default gateway again. 917 */ 918 void 919 in6_losing(in6p) 920 struct in6pcb *in6p; 921 { 922 struct rtentry *rt; 923 struct rt_addrinfo info; 924 925 if ((rt = in6p->in6p_route.ro_rt) != NULL) { 926 in6p->in6p_route.ro_rt = 0; 927 bzero((caddr_t)&info, sizeof(info)); 928 info.rti_info[RTAX_DST] = 929 (struct sockaddr *)&in6p->in6p_route.ro_dst; 930 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 931 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 932 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 933 if (rt->rt_flags & RTF_DYNAMIC) 934 (void)rtrequest(RTM_DELETE, rt_key(rt), 935 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 936 (struct rtentry **)0); 937 else 938 /* 939 * A new route can be allocated 940 * the next time output is attempted. 941 */ 942 rtfree(rt); 943 } 944 } 945 946 /* 947 * After a routing change, flush old routing 948 * and allocate a (hopefully) better one. 949 */ 950 void 951 in6_rtchange(in6p, errno) 952 struct in6pcb *in6p; 953 int errno; 954 { 955 if (in6p->in6p_route.ro_rt) { 956 rtfree(in6p->in6p_route.ro_rt); 957 in6p->in6p_route.ro_rt = 0; 958 /* 959 * A new route can be allocated the next time 960 * output is attempted. 961 */ 962 } 963 } 964 965 struct in6pcb * 966 in6_pcblookup(head, faddr6, fport_arg, laddr6, lport_arg, flags) 967 struct in6pcb *head; 968 struct in6_addr *faddr6, *laddr6; 969 u_int fport_arg, lport_arg; 970 int flags; 971 { 972 struct in6pcb *in6p, *match = 0; 973 int matchwild = 3, wildcard; 974 u_int16_t fport = fport_arg, lport = lport_arg; 975 976 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) { 977 if (in6p->in6p_lport != lport) 978 continue; 979 wildcard = 0; 980 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 981 if (IN6_IS_ADDR_UNSPECIFIED(laddr6)) 982 wildcard++; 983 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) 984 continue; 985 } 986 #ifndef TCP6 987 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) 988 && in6p->in6p_laddr.s6_addr32[3] == 0) { 989 if (!IN6_IS_ADDR_V4MAPPED(laddr6)) 990 continue; 991 if (laddr6->s6_addr32[3] == 0) 992 ; 993 else 994 wildcard++; 995 } 996 #endif 997 else { 998 if (IN6_IS_ADDR_V4MAPPED(laddr6)) { 999 #if !defined(TCP6) && !defined(INET6_BINDV6ONLY) 1000 if (in6p->in6p_flags & IN6P_BINDV6ONLY) 1001 continue; 1002 else 1003 wildcard++; 1004 #else 1005 continue; 1006 #endif 1007 } else if (!IN6_IS_ADDR_UNSPECIFIED(laddr6)) 1008 wildcard++; 1009 } 1010 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 1011 if (IN6_IS_ADDR_UNSPECIFIED(faddr6)) 1012 wildcard++; 1013 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6) 1014 || in6p->in6p_fport != fport) 1015 continue; 1016 } 1017 #ifndef TCP6 1018 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) 1019 && in6p->in6p_faddr.s6_addr32[3] == 0) { 1020 if (!IN6_IS_ADDR_V4MAPPED(faddr6)) 1021 continue; 1022 if (faddr6->s6_addr32[3] == 0) 1023 ; 1024 else 1025 wildcard++; 1026 } 1027 #endif 1028 else { 1029 if (IN6_IS_ADDR_V4MAPPED(faddr6)) { 1030 #if !defined(TCP6) && !defined(INET6_BINDV6ONLY) 1031 if (in6p->in6p_flags & IN6P_BINDV6ONLY) 1032 continue; 1033 else 1034 wildcard++; 1035 #else 1036 continue; 1037 #endif 1038 } else if (!IN6_IS_ADDR_UNSPECIFIED(faddr6)) 1039 wildcard++; 1040 } 1041 1042 if (wildcard && (flags & IN6PLOOKUP_WILDCARD) == 0) 1043 continue; 1044 if (wildcard < matchwild) { 1045 match = in6p; 1046 matchwild = wildcard; 1047 if (matchwild == 0) 1048 break; 1049 } 1050 } 1051 return(match); 1052 } 1053 1054 #ifndef TCP6 1055 struct rtentry * 1056 in6_pcbrtentry(in6p) 1057 struct in6pcb *in6p; 1058 { 1059 struct route_in6 *ro; 1060 1061 ro = &in6p->in6p_route; 1062 1063 if (ro->ro_rt == NULL) { 1064 /* 1065 * No route yet, so try to acquire one. 1066 */ 1067 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 1068 bzero(&ro->ro_dst, sizeof(ro->ro_dst)); 1069 ro->ro_dst.sin6_family = AF_INET6; 1070 ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6); 1071 satosin6(&ro->ro_dst)->sin6_addr = in6p->in6p_faddr; 1072 rtalloc((struct route *)ro); 1073 } 1074 } 1075 return (ro->ro_rt); 1076 } 1077 1078 struct in6pcb * 1079 in6_pcblookup_connect(head, faddr6, fport_arg, laddr6, lport_arg, faith) 1080 struct in6pcb *head; 1081 struct in6_addr *faddr6, *laddr6; 1082 u_int fport_arg, lport_arg; 1083 int faith; 1084 { 1085 struct in6pcb *in6p; 1086 u_int16_t fport = fport_arg, lport = lport_arg; 1087 1088 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) { 1089 #if defined(NFAITH) && NFAITH > 0 1090 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) 1091 continue; 1092 #endif 1093 /* find exact match on both source and dest */ 1094 if (in6p->in6p_fport != fport) 1095 continue; 1096 if (in6p->in6p_lport != lport) 1097 continue; 1098 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) 1099 continue; 1100 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)) 1101 continue; 1102 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) 1103 continue; 1104 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) 1105 continue; 1106 return in6p; 1107 } 1108 return NULL; 1109 } 1110 1111 struct in6pcb * 1112 in6_pcblookup_bind(head, laddr6, lport_arg, faith) 1113 struct in6pcb *head; 1114 struct in6_addr *laddr6; 1115 u_int lport_arg; 1116 int faith; 1117 { 1118 struct in6pcb *in6p, *match; 1119 u_int16_t lport = lport_arg; 1120 1121 match = NULL; 1122 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) { 1123 /* 1124 * find destination match. exact match is preferred 1125 * against wildcard match. 1126 */ 1127 #if defined(NFAITH) && NFAITH > 0 1128 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) 1129 continue; 1130 #endif 1131 if (in6p->in6p_fport != 0) 1132 continue; 1133 if (in6p->in6p_lport != lport) 1134 continue; 1135 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 1136 if (IN6_IS_ADDR_V4MAPPED(laddr6)) { 1137 #ifndef INET6_BINDV6ONLY 1138 if (in6p->in6p_flags & IN6P_BINDV6ONLY) 1139 continue; 1140 else 1141 match = in6p; 1142 #else 1143 continue; 1144 #endif 1145 } else 1146 match = in6p; 1147 } 1148 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) && 1149 in6p->in6p_laddr.s6_addr32[3] == 0) { 1150 if (IN6_IS_ADDR_V4MAPPED(laddr6) && 1151 laddr6->s6_addr32[3] != 0) 1152 match = in6p; 1153 } 1154 else if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) 1155 return in6p; 1156 } 1157 return match; 1158 } 1159 #endif 1160