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 73#include <sys/param.h> 74#include <sys/systm.h> 75#include <sys/malloc.h> 76#include <sys/mbuf.h> 77#include <sys/domain.h> 78#include <sys/protosw.h> 79#include <sys/socket.h> 80#include <sys/socketvar.h> 81#include <sys/sockio.h> 82#include <sys/errno.h> 83#include <sys/time.h> 84#include <sys/proc.h> 85#include <sys/jail.h> 86 87#include <vm/vm_zone.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/ip6.h> 97#include <netinet/ip_var.h> 98#include <netinet6/ip6_var.h> 99#include <netinet6/nd6.h> 100#include <netinet/in_pcb.h> 101#include <netinet6/in6_pcb.h> 102
| 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 73#include <sys/param.h> 74#include <sys/systm.h> 75#include <sys/malloc.h> 76#include <sys/mbuf.h> 77#include <sys/domain.h> 78#include <sys/protosw.h> 79#include <sys/socket.h> 80#include <sys/socketvar.h> 81#include <sys/sockio.h> 82#include <sys/errno.h> 83#include <sys/time.h> 84#include <sys/proc.h> 85#include <sys/jail.h> 86 87#include <vm/vm_zone.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/ip6.h> 97#include <netinet/ip_var.h> 98#include <netinet6/ip6_var.h> 99#include <netinet6/nd6.h> 100#include <netinet/in_pcb.h> 101#include <netinet6/in6_pcb.h> 102
|
108#ifdef IPSEC 109#include <netinet6/ipsec.h> 110#ifdef INET6 111#include <netinet6/ipsec6.h> 112#endif 113#include <netinet6/ah.h> 114#ifdef INET6 115#include <netinet6/ah6.h> 116#endif 117#include <netkey/key.h> 118#endif /* IPSEC */ 119 120struct in6_addr zeroin6_addr; 121 122int 123in6_pcbbind(inp, nam, td) 124 register struct inpcb *inp; 125 struct sockaddr *nam; 126 struct thread *td; 127{ 128 struct socket *so = inp->inp_socket; 129 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 130 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 131 u_short lport = 0; 132 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 133 134 if (!in6_ifaddr) /* XXX broken! */ 135 return (EADDRNOTAVAIL); 136 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 137 return(EINVAL); 138 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 139 wild = 1; 140 if (nam) { 141 sin6 = (struct sockaddr_in6 *)nam; 142 if (nam->sa_len != sizeof(*sin6)) 143 return(EINVAL); 144 /* 145 * family check. 146 */ 147 if (nam->sa_family != AF_INET6) 148 return(EAFNOSUPPORT); 149 150 /* KAME hack: embed scopeid */ 151 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0) 152 return EINVAL; 153 /* this must be cleared for ifa_ifwithaddr() */ 154 sin6->sin6_scope_id = 0; 155 156 lport = sin6->sin6_port; 157 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 158 /* 159 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 160 * allow compepte duplication of binding if 161 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 162 * and a multicast address is bound on both 163 * new and duplicated sockets. 164 */ 165 if (so->so_options & SO_REUSEADDR) 166 reuseport = SO_REUSEADDR|SO_REUSEPORT; 167 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 168 struct ifaddr *ia = NULL; 169 170 sin6->sin6_port = 0; /* yech... */ 171 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) 172 return(EADDRNOTAVAIL); 173 174 /* 175 * XXX: bind to an anycast address might accidentally 176 * cause sending a packet with anycast source address. 177 * We should allow to bind to a deprecated address, since 178 * the application dare to use it. 179 */ 180 if (ia && 181 ((struct in6_ifaddr *)ia)->ia6_flags & 182 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) { 183 return(EADDRNOTAVAIL); 184 } 185 } 186 if (lport) { 187 struct inpcb *t; 188 189 /* GROSS */ 190 if (ntohs(lport) < IPV6PORT_RESERVED && td && 191 suser_xxx(0, td->td_proc, PRISON_ROOT)) 192 return(EACCES); 193 if (so->so_cred->cr_uid != 0 && 194 !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 195 t = in6_pcblookup_local(pcbinfo, 196 &sin6->sin6_addr, lport, 197 INPLOOKUP_WILDCARD); 198 if (t && 199 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 200 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || 201 (t->inp_socket->so_options & 202 SO_REUSEPORT) == 0) && 203 (so->so_cred->cr_uid != 204 t->inp_socket->so_cred->cr_uid)) 205 return (EADDRINUSE); 206 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 207 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 208 struct sockaddr_in sin; 209 210 in6_sin6_2_sin(&sin, sin6); 211 t = in_pcblookup_local(pcbinfo, 212 sin.sin_addr, lport, 213 INPLOOKUP_WILDCARD); 214 if (t && 215 (so->so_cred->cr_uid != 216 t->inp_socket->so_cred->cr_uid) && 217 (ntohl(t->inp_laddr.s_addr) != 218 INADDR_ANY || 219 INP_SOCKAF(so) == 220 INP_SOCKAF(t->inp_socket))) 221 return (EADDRINUSE); 222 } 223 } 224 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, 225 lport, wild); 226 if (t && (reuseport & t->inp_socket->so_options) == 0) 227 return(EADDRINUSE); 228 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 229 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 230 struct sockaddr_in sin; 231 232 in6_sin6_2_sin(&sin, sin6); 233 t = in_pcblookup_local(pcbinfo, sin.sin_addr, 234 lport, wild); 235 if (t && 236 (reuseport & t->inp_socket->so_options) 237 == 0 && 238 (ntohl(t->inp_laddr.s_addr) 239 != INADDR_ANY || 240 INP_SOCKAF(so) == 241 INP_SOCKAF(t->inp_socket))) 242 return (EADDRINUSE); 243 } 244 } 245 inp->in6p_laddr = sin6->sin6_addr; 246 } 247 if (lport == 0) { 248 int e; 249 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, td)) != 0) 250 return(e); 251 } 252 else { 253 inp->inp_lport = lport; 254 if (in_pcbinshash(inp) != 0) { 255 inp->in6p_laddr = in6addr_any; 256 inp->inp_lport = 0; 257 return (EAGAIN); 258 } 259 } 260 return(0); 261} 262 263/* 264 * Transform old in6_pcbconnect() into an inner subroutine for new 265 * in6_pcbconnect(): Do some validity-checking on the remote 266 * address (in mbuf 'nam') and then determine local host address 267 * (i.e., which interface) to use to access that remote host. 268 * 269 * This preserves definition of in6_pcbconnect(), while supporting a 270 * slightly different version for T/TCP. (This is more than 271 * a bit of a kludge, but cleaning up the internal interfaces would 272 * have forced minor changes in every protocol). 273 */ 274 275int 276in6_pcbladdr(inp, nam, plocal_addr6) 277 register struct inpcb *inp; 278 struct sockaddr *nam; 279 struct in6_addr **plocal_addr6; 280{ 281 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 282 struct ifnet *ifp = NULL; 283 int error = 0; 284 285 if (nam->sa_len != sizeof (*sin6)) 286 return (EINVAL); 287 if (sin6->sin6_family != AF_INET6) 288 return (EAFNOSUPPORT); 289 if (sin6->sin6_port == 0) 290 return (EADDRNOTAVAIL); 291 292 /* KAME hack: embed scopeid */ 293 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0) 294 return EINVAL; 295 296 if (in6_ifaddr) { 297 /* 298 * If the destination address is UNSPECIFIED addr, 299 * use the loopback addr, e.g ::1. 300 */ 301 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 302 sin6->sin6_addr = in6addr_loopback; 303 } 304 { 305 /* 306 * XXX: in6_selectsrc might replace the bound local address 307 * with the address specified by setsockopt(IPV6_PKTINFO). 308 * Is it the intended behavior? 309 */ 310 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts, 311 inp->in6p_moptions, 312 &inp->in6p_route, 313 &inp->in6p_laddr, &error); 314 if (*plocal_addr6 == 0) { 315 if (error == 0) 316 error = EADDRNOTAVAIL; 317 return(error); 318 } 319 /* 320 * Don't do pcblookup call here; return interface in 321 * plocal_addr6 322 * and exit to caller, that will do the lookup. 323 */ 324 } 325 326 if (inp->in6p_route.ro_rt) 327 ifp = inp->in6p_route.ro_rt->rt_ifp; 328 329 return(0); 330} 331 332/* 333 * Outer subroutine: 334 * Connect from a socket to a specified address. 335 * Both address and port must be specified in argument sin. 336 * If don't have a local address for this socket yet, 337 * then pick one. 338 */ 339int 340in6_pcbconnect(inp, nam, td) 341 register struct inpcb *inp; 342 struct sockaddr *nam; 343 struct thread *td; 344{ 345 struct in6_addr *addr6; 346 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 347 int error; 348 349 /* 350 * Call inner routine, to assign local interface address. 351 */ 352 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0) 353 return(error); 354 355 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, 356 sin6->sin6_port, 357 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 358 ? addr6 : &inp->in6p_laddr, 359 inp->inp_lport, 0, NULL) != NULL) { 360 return (EADDRINUSE); 361 } 362 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 363 if (inp->inp_lport == 0) { 364 error = in6_pcbbind(inp, (struct sockaddr *)0, td); 365 if (error) 366 return (error); 367 } 368 inp->in6p_laddr = *addr6; 369 } 370 inp->in6p_faddr = sin6->sin6_addr; 371 inp->inp_fport = sin6->sin6_port; 372 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 373 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 374 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 375 inp->in6p_flowinfo |= 376 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK); 377 378 in_pcbrehash(inp); 379 return (0); 380} 381 382#if 0 383/* 384 * Return an IPv6 address, which is the most appropriate for given 385 * destination and user specified options. 386 * If necessary, this function lookups the routing table and return 387 * an entry to the caller for later use. 388 */ 389struct in6_addr * 390in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp) 391 struct sockaddr_in6 *dstsock; 392 struct ip6_pktopts *opts; 393 struct ip6_moptions *mopts; 394 struct route_in6 *ro; 395 struct in6_addr *laddr; 396 int *errorp; 397{ 398 struct in6_addr *dst; 399 struct in6_ifaddr *ia6 = 0; 400 struct in6_pktinfo *pi = NULL; 401 402 dst = &dstsock->sin6_addr; 403 *errorp = 0; 404 405 /* 406 * If the source address is explicitly specified by the caller, 407 * use it. 408 */ 409 if (opts && (pi = opts->ip6po_pktinfo) && 410 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) 411 return(&pi->ipi6_addr); 412 413 /* 414 * If the source address is not specified but the socket(if any) 415 * is already bound, use the bound address. 416 */ 417 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) 418 return(laddr); 419 420 /* 421 * If the caller doesn't specify the source address but 422 * the outgoing interface, use an address associated with 423 * the interface. 424 */ 425 if (pi && pi->ipi6_ifindex) { 426 /* XXX boundary check is assumed to be already done. */ 427 ia6 = in6_ifawithscope(ifnet_byindex(pi->ipi6_ifindex), dst); 428 if (ia6 == 0) { 429 *errorp = EADDRNOTAVAIL; 430 return(0); 431 } 432 return(&satosin6(&ia6->ia_addr)->sin6_addr); 433 } 434 435 /* 436 * If the destination address is a link-local unicast address or 437 * a multicast address, and if the outgoing interface is specified 438 * by the sin6_scope_id filed, use an address associated with the 439 * interface. 440 * XXX: We're now trying to define more specific semantics of 441 * sin6_scope_id field, so this part will be rewritten in 442 * the near future. 443 */ 444 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) && 445 dstsock->sin6_scope_id) { 446 /* 447 * I'm not sure if boundary check for scope_id is done 448 * somewhere... 449 */ 450 if (dstsock->sin6_scope_id < 0 || 451 if_index < dstsock->sin6_scope_id) { 452 *errorp = ENXIO; /* XXX: better error? */ 453 return(0); 454 } 455 ia6 = in6_ifawithscope(ifnet_byindex(dstsock->sin6_scope_id), 456 dst); 457 if (ia6 == 0) { 458 *errorp = EADDRNOTAVAIL; 459 return(0); 460 } 461 return(&satosin6(&ia6->ia_addr)->sin6_addr); 462 } 463 464 /* 465 * If the destination address is a multicast address and 466 * the outgoing interface for the address is specified 467 * by the caller, use an address associated with the interface. 468 * There is a sanity check here; if the destination has node-local 469 * scope, the outgoing interfacde should be a loopback address. 470 * Even if the outgoing interface is not specified, we also 471 * choose a loopback interface as the outgoing interface. 472 */ 473 if (IN6_IS_ADDR_MULTICAST(dst)) { 474 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL; 475 476 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) { 477 ifp = &loif[0]; 478 } 479 480 if (ifp) { 481 ia6 = in6_ifawithscope(ifp, dst); 482 if (ia6 == 0) { 483 *errorp = EADDRNOTAVAIL; 484 return(0); 485 } 486 return(&ia6->ia_addr.sin6_addr); 487 } 488 } 489 490 /* 491 * If the next hop address for the packet is specified 492 * by caller, use an address associated with the route 493 * to the next hop. 494 */ 495 { 496 struct sockaddr_in6 *sin6_next; 497 struct rtentry *rt; 498 499 if (opts && opts->ip6po_nexthop) { 500 sin6_next = satosin6(opts->ip6po_nexthop); 501 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL); 502 if (rt) { 503 ia6 = in6_ifawithscope(rt->rt_ifp, dst); 504 if (ia6 == 0) 505 ia6 = ifatoia6(rt->rt_ifa); 506 } 507 if (ia6 == 0) { 508 *errorp = EADDRNOTAVAIL; 509 return(0); 510 } 511 return(&satosin6(&ia6->ia_addr)->sin6_addr); 512 } 513 } 514 515 /* 516 * If route is known or can be allocated now, 517 * our src addr is taken from the i/f, else punt. 518 */ 519 if (ro) { 520 if (ro->ro_rt && 521 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) { 522 RTFREE(ro->ro_rt); 523 ro->ro_rt = (struct rtentry *)0; 524 } 525 if (ro->ro_rt == (struct rtentry *)0 || 526 ro->ro_rt->rt_ifp == (struct ifnet *)0) { 527 struct sockaddr_in6 *dst6; 528 529 /* No route yet, so try to acquire one */ 530 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 531 dst6 = (struct sockaddr_in6 *)&ro->ro_dst; 532 dst6->sin6_family = AF_INET6; 533 dst6->sin6_len = sizeof(struct sockaddr_in6); 534 dst6->sin6_addr = *dst; 535 if (IN6_IS_ADDR_MULTICAST(dst)) { 536 ro->ro_rt = rtalloc1(&((struct route *)ro) 537 ->ro_dst, 0, 0UL); 538 } else { 539 rtalloc((struct route *)ro); 540 } 541 } 542 543 /* 544 * in_pcbconnect() checks out IFF_LOOPBACK to skip using 545 * the address. But we don't know why it does so. 546 * It is necessary to ensure the scope even for lo0 547 * so doesn't check out IFF_LOOPBACK. 548 */ 549 550 if (ro->ro_rt) { 551 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst); 552 if (ia6 == 0) /* xxx scope error ?*/ 553 ia6 = ifatoia6(ro->ro_rt->rt_ifa); 554 } 555 if (ia6 == 0) { 556 *errorp = EHOSTUNREACH; /* no route */ 557 return(0); 558 } 559 return(&satosin6(&ia6->ia_addr)->sin6_addr); 560 } 561 562 *errorp = EADDRNOTAVAIL; 563 return(0); 564} 565 566/* 567 * Default hop limit selection. The precedence is as follows: 568 * 1. Hoplimit valued specified via ioctl. 569 * 2. (If the outgoing interface is detected) the current 570 * hop limit of the interface specified by router advertisement. 571 * 3. The system default hoplimit. 572*/ 573int 574in6_selecthlim(in6p, ifp) 575 struct in6pcb *in6p; 576 struct ifnet *ifp; 577{ 578 if (in6p && in6p->in6p_hops >= 0) 579 return(in6p->in6p_hops); 580 else if (ifp) 581 return(nd_ifinfo[ifp->if_index].chlim); 582 else 583 return(ip6_defhlim); 584} 585#endif 586 587void 588in6_pcbdisconnect(inp) 589 struct inpcb *inp; 590{ 591 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr)); 592 inp->inp_fport = 0; 593 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 594 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 595 in_pcbrehash(inp); 596 if (inp->inp_socket->so_state & SS_NOFDREF) 597 in6_pcbdetach(inp); 598} 599 600void 601in6_pcbdetach(inp) 602 struct inpcb *inp; 603{ 604 struct socket *so = inp->inp_socket; 605 struct inpcbinfo *ipi = inp->inp_pcbinfo; 606 607#ifdef IPSEC 608 if (inp->in6p_sp != NULL) 609 ipsec6_delete_pcbpolicy(inp); 610#endif /* IPSEC */ 611 inp->inp_gencnt = ++ipi->ipi_gencnt; 612 in_pcbremlists(inp); 613 sotoinpcb(so) = 0; 614 sofree(so); 615 616 if (inp->in6p_options) 617 m_freem(inp->in6p_options); 618 ip6_freepcbopts(inp->in6p_outputopts); 619 ip6_freemoptions(inp->in6p_moptions); 620 if (inp->in6p_route.ro_rt) 621 rtfree(inp->in6p_route.ro_rt); 622 /* Check and free IPv4 related resources in case of mapped addr */ 623 if (inp->inp_options) 624 (void)m_free(inp->inp_options); 625 ip_freemoptions(inp->inp_moptions); 626 627 inp->inp_vflag = 0; 628 zfree(ipi->ipi_zone, inp); 629} 630 631/* 632 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was 633 * modified to match the pru_sockaddr() and pru_peeraddr() entry points 634 * in struct pr_usrreqs, so that protocols can just reference then directly 635 * without the need for a wrapper function. The socket must have a valid 636 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one 637 * except through a kernel programming error, so it is acceptable to panic 638 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap 639 * because there actually /is/ a programming error somewhere... XXX) 640 */ 641int 642in6_setsockaddr(so, nam) 643 struct socket *so; 644 struct sockaddr **nam; 645{ 646 int s; 647 register struct inpcb *inp; 648 register struct sockaddr_in6 *sin6; 649 650 /* 651 * Do the malloc first in case it blocks. 652 */ 653 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK); 654 bzero(sin6, sizeof *sin6); 655 sin6->sin6_family = AF_INET6; 656 sin6->sin6_len = sizeof(*sin6); 657 658 s = splnet(); 659 inp = sotoinpcb(so); 660 if (!inp) { 661 splx(s); 662 free(sin6, M_SONAME); 663 return EINVAL; 664 } 665 sin6->sin6_port = inp->inp_lport; 666 sin6->sin6_addr = inp->in6p_laddr; 667 splx(s); 668 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 669 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 670 else 671 sin6->sin6_scope_id = 0; /*XXX*/ 672 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 673 sin6->sin6_addr.s6_addr16[1] = 0; 674 675 *nam = (struct sockaddr *)sin6; 676 return 0; 677} 678 679int 680in6_setpeeraddr(so, nam) 681 struct socket *so; 682 struct sockaddr **nam; 683{ 684 int s; 685 struct inpcb *inp; 686 register struct sockaddr_in6 *sin6; 687 688 /* 689 * Do the malloc first in case it blocks. 690 */ 691 MALLOC(sin6, struct sockaddr_in6 *, sizeof(*sin6), M_SONAME, M_WAITOK); 692 bzero((caddr_t)sin6, sizeof (*sin6)); 693 sin6->sin6_family = AF_INET6; 694 sin6->sin6_len = sizeof(struct sockaddr_in6); 695 696 s = splnet(); 697 inp = sotoinpcb(so); 698 if (!inp) { 699 splx(s); 700 free(sin6, M_SONAME); 701 return EINVAL; 702 } 703 sin6->sin6_port = inp->inp_fport; 704 sin6->sin6_addr = inp->in6p_faddr; 705 splx(s); 706 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 707 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 708 else 709 sin6->sin6_scope_id = 0; /*XXX*/ 710 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 711 sin6->sin6_addr.s6_addr16[1] = 0; 712 713 *nam = (struct sockaddr *)sin6; 714 return 0; 715} 716 717int 718in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 719{ 720 struct inpcb *inp = sotoinpcb(so); 721 int error; 722 723 if (inp == NULL) 724 return EINVAL; 725 if (inp->inp_vflag & INP_IPV4) { 726 error = in_setsockaddr(so, nam); 727 if (error == 0) 728 in6_sin_2_v4mapsin6_in_sock(nam); 729 } else 730 error = in6_setsockaddr(so, nam); 731 732 return error; 733} 734 735int 736in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 737{ 738 struct inpcb *inp = sotoinpcb(so); 739 int error; 740 741 if (inp == NULL) 742 return EINVAL; 743 if (inp->inp_vflag & INP_IPV4) { 744 error = in_setpeeraddr(so, nam); 745 if (error == 0) 746 in6_sin_2_v4mapsin6_in_sock(nam); 747 } else 748 error = in6_setpeeraddr(so, nam); 749 750 return error; 751} 752 753/* 754 * Pass some notification to all connections of a protocol 755 * associated with address dst. The local address and/or port numbers 756 * may be specified to limit the search. The "usual action" will be 757 * taken, depending on the ctlinput cmd. The caller must filter any 758 * cmds that are uninteresting (e.g., no error in the map). 759 * Call the protocol specific routine (if any) to report 760 * any errors for each matching socket. 761 * 762 * Must be called at splnet. 763 */ 764void 765in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, notify) 766 struct inpcbhead *head; 767 struct sockaddr *dst, *src; 768 u_int fport_arg, lport_arg; 769 int cmd; 770 void (*notify) __P((struct inpcb *, int)); 771{ 772 struct inpcb *inp, *ninp; 773 struct sockaddr_in6 sa6_src, *sa6_dst; 774 u_short fport = fport_arg, lport = lport_arg; 775 u_int32_t flowinfo; 776 int errno, s; 777 778 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6) 779 return; 780 781 sa6_dst = (struct sockaddr_in6 *)dst; 782 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 783 return; 784 785 /* 786 * note that src can be NULL when we get notify by local fragmentation. 787 */ 788 sa6_src = (src == NULL) ? sa6_any : *(struct sockaddr_in6 *)src; 789 flowinfo = sa6_src.sin6_flowinfo; 790 791 /* 792 * Redirects go to all references to the destination, 793 * and use in6_rtchange to invalidate the route cache. 794 * Dead host indications: also use in6_rtchange to invalidate 795 * the cache, and deliver the error to all the sockets. 796 * Otherwise, if we have knowledge of the local port and address, 797 * deliver only to that socket. 798 */ 799 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 800 fport = 0; 801 lport = 0; 802 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 803 804 if (cmd != PRC_HOSTDEAD) 805 notify = in6_rtchange; 806 } 807 errno = inet6ctlerrmap[cmd]; 808 s = splnet(); 809 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) { 810 ninp = LIST_NEXT(inp, inp_list); 811 812 if ((inp->inp_vflag & INP_IPV6) == 0) 813 continue; 814 815 /* 816 * Detect if we should notify the error. If no source and 817 * destination ports are specifed, but non-zero flowinfo and 818 * local address match, notify the error. This is the case 819 * when the error is delivered with an encrypted buffer 820 * by ESP. Otherwise, just compare addresses and ports 821 * as usual. 822 */ 823 if (lport == 0 && fport == 0 && flowinfo && 824 inp->inp_socket != NULL && 825 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && 826 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 827 goto do_notify; 828 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 829 &sa6_dst->sin6_addr) || 830 inp->inp_socket == 0 || 831 (lport && inp->inp_lport != lport) || 832 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 833 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 834 &sa6_src.sin6_addr)) || 835 (fport && inp->inp_fport != fport)) 836 continue; 837 838 do_notify: 839 if (notify) 840 (*notify)(inp, errno); 841 } 842 splx(s); 843} 844 845/* 846 * Lookup a PCB based on the local address and port. 847 */ 848struct inpcb * 849in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay) 850 struct inpcbinfo *pcbinfo; 851 struct in6_addr *laddr; 852 u_int lport_arg; 853 int wild_okay; 854{ 855 register struct inpcb *inp; 856 int matchwild = 3, wildcard; 857 u_short lport = lport_arg; 858 859 if (!wild_okay) { 860 struct inpcbhead *head; 861 /* 862 * Look for an unconnected (wildcard foreign addr) PCB that 863 * matches the local address and port we're looking for. 864 */ 865 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, 866 pcbinfo->hashmask)]; 867 LIST_FOREACH(inp, head, inp_hash) { 868 if ((inp->inp_vflag & INP_IPV6) == 0) 869 continue; 870 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 871 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 872 inp->inp_lport == lport) { 873 /* 874 * Found. 875 */ 876 return (inp); 877 } 878 } 879 /* 880 * Not found. 881 */ 882 return (NULL); 883 } else { 884 struct inpcbporthead *porthash; 885 struct inpcbport *phd; 886 struct inpcb *match = NULL; 887 /* 888 * Best fit PCB lookup. 889 * 890 * First see if this local port is in use by looking on the 891 * port hash list. 892 */ 893 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport, 894 pcbinfo->porthashmask)]; 895 LIST_FOREACH(phd, porthash, phd_hash) { 896 if (phd->phd_port == lport) 897 break; 898 } 899 if (phd != NULL) { 900 /* 901 * Port is in use by one or more PCBs. Look for best 902 * fit. 903 */ 904 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 905 wildcard = 0; 906 if ((inp->inp_vflag & INP_IPV6) == 0) 907 continue; 908 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 909 wildcard++; 910 if (!IN6_IS_ADDR_UNSPECIFIED( 911 &inp->in6p_laddr)) { 912 if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 913 wildcard++; 914 else if (!IN6_ARE_ADDR_EQUAL( 915 &inp->in6p_laddr, laddr)) 916 continue; 917 } else { 918 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 919 wildcard++; 920 } 921 if (wildcard < matchwild) { 922 match = inp; 923 matchwild = wildcard; 924 if (matchwild == 0) { 925 break; 926 } 927 } 928 } 929 } 930 return (match); 931 } 932} 933 934void 935in6_pcbpurgeif0(head, ifp) 936 struct in6pcb *head; 937 struct ifnet *ifp; 938{ 939 struct in6pcb *in6p; 940 struct ip6_moptions *im6o; 941 struct in6_multi_mship *imm, *nimm; 942 943 for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) { 944 im6o = in6p->in6p_moptions; 945 if ((in6p->inp_vflag & INP_IPV6) && 946 im6o) { 947 /* 948 * Unselect the outgoing interface if it is being 949 * detached. 950 */ 951 if (im6o->im6o_multicast_ifp == ifp) 952 im6o->im6o_multicast_ifp = NULL; 953 954 /* 955 * Drop multicast group membership if we joined 956 * through the interface being detached. 957 * XXX controversial - is it really legal for kernel 958 * to force this? 959 */ 960 for (imm = im6o->im6o_memberships.lh_first; 961 imm != NULL; imm = nimm) { 962 nimm = imm->i6mm_chain.le_next; 963 if (imm->i6mm_maddr->in6m_ifp == ifp) { 964 LIST_REMOVE(imm, i6mm_chain); 965 in6_delmulti(imm->i6mm_maddr); 966 free(imm, M_IPMADDR); 967 } 968 } 969 } 970 } 971} 972 973/* 974 * Check for alternatives when higher level complains 975 * about service problems. For now, invalidate cached 976 * routing information. If the route was created dynamically 977 * (by a redirect), time to try a default gateway again. 978 */ 979void 980in6_losing(in6p) 981 struct inpcb *in6p; 982{ 983 struct rtentry *rt; 984 struct rt_addrinfo info; 985 986 if ((rt = in6p->in6p_route.ro_rt) != NULL) { 987 in6p->in6p_route.ro_rt = 0; 988 bzero((caddr_t)&info, sizeof(info)); 989 info.rti_info[RTAX_DST] = 990 (struct sockaddr *)&in6p->in6p_route.ro_dst; 991 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 992 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 993 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 994 if (rt->rt_flags & RTF_DYNAMIC) 995 (void)rtrequest(RTM_DELETE, rt_key(rt), 996 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 997 (struct rtentry **)0); 998 else 999 /* 1000 * A new route can be allocated 1001 * the next time output is attempted. 1002 */ 1003 rtfree(rt); 1004 } 1005} 1006 1007/* 1008 * After a routing change, flush old routing 1009 * and allocate a (hopefully) better one. 1010 */ 1011void 1012in6_rtchange(inp, errno) 1013 struct inpcb *inp; 1014 int errno; 1015{ 1016 if (inp->in6p_route.ro_rt) { 1017 rtfree(inp->in6p_route.ro_rt); 1018 inp->in6p_route.ro_rt = 0; 1019 /* 1020 * A new route can be allocated the next time 1021 * output is attempted. 1022 */ 1023 } 1024} 1025 1026/* 1027 * Lookup PCB in hash list. 1028 */ 1029struct inpcb * 1030in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp) 1031 struct inpcbinfo *pcbinfo; 1032 struct in6_addr *faddr, *laddr; 1033 u_int fport_arg, lport_arg; 1034 int wildcard; 1035 struct ifnet *ifp; 1036{ 1037 struct inpcbhead *head; 1038 register struct inpcb *inp; 1039 u_short fport = fport_arg, lport = lport_arg; 1040 int faith; 1041
| 103#ifdef IPSEC 104#include <netinet6/ipsec.h> 105#ifdef INET6 106#include <netinet6/ipsec6.h> 107#endif 108#include <netinet6/ah.h> 109#ifdef INET6 110#include <netinet6/ah6.h> 111#endif 112#include <netkey/key.h> 113#endif /* IPSEC */ 114 115struct in6_addr zeroin6_addr; 116 117int 118in6_pcbbind(inp, nam, td) 119 register struct inpcb *inp; 120 struct sockaddr *nam; 121 struct thread *td; 122{ 123 struct socket *so = inp->inp_socket; 124 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 125 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 126 u_short lport = 0; 127 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 128 129 if (!in6_ifaddr) /* XXX broken! */ 130 return (EADDRNOTAVAIL); 131 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 132 return(EINVAL); 133 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 134 wild = 1; 135 if (nam) { 136 sin6 = (struct sockaddr_in6 *)nam; 137 if (nam->sa_len != sizeof(*sin6)) 138 return(EINVAL); 139 /* 140 * family check. 141 */ 142 if (nam->sa_family != AF_INET6) 143 return(EAFNOSUPPORT); 144 145 /* KAME hack: embed scopeid */ 146 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0) 147 return EINVAL; 148 /* this must be cleared for ifa_ifwithaddr() */ 149 sin6->sin6_scope_id = 0; 150 151 lport = sin6->sin6_port; 152 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 153 /* 154 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 155 * allow compepte duplication of binding if 156 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 157 * and a multicast address is bound on both 158 * new and duplicated sockets. 159 */ 160 if (so->so_options & SO_REUSEADDR) 161 reuseport = SO_REUSEADDR|SO_REUSEPORT; 162 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 163 struct ifaddr *ia = NULL; 164 165 sin6->sin6_port = 0; /* yech... */ 166 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) 167 return(EADDRNOTAVAIL); 168 169 /* 170 * XXX: bind to an anycast address might accidentally 171 * cause sending a packet with anycast source address. 172 * We should allow to bind to a deprecated address, since 173 * the application dare to use it. 174 */ 175 if (ia && 176 ((struct in6_ifaddr *)ia)->ia6_flags & 177 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) { 178 return(EADDRNOTAVAIL); 179 } 180 } 181 if (lport) { 182 struct inpcb *t; 183 184 /* GROSS */ 185 if (ntohs(lport) < IPV6PORT_RESERVED && td && 186 suser_xxx(0, td->td_proc, PRISON_ROOT)) 187 return(EACCES); 188 if (so->so_cred->cr_uid != 0 && 189 !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 190 t = in6_pcblookup_local(pcbinfo, 191 &sin6->sin6_addr, lport, 192 INPLOOKUP_WILDCARD); 193 if (t && 194 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 195 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || 196 (t->inp_socket->so_options & 197 SO_REUSEPORT) == 0) && 198 (so->so_cred->cr_uid != 199 t->inp_socket->so_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); 209 if (t && 210 (so->so_cred->cr_uid != 211 t->inp_socket->so_cred->cr_uid) && 212 (ntohl(t->inp_laddr.s_addr) != 213 INADDR_ANY || 214 INP_SOCKAF(so) == 215 INP_SOCKAF(t->inp_socket))) 216 return (EADDRINUSE); 217 } 218 } 219 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, 220 lport, wild); 221 if (t && (reuseport & t->inp_socket->so_options) == 0) 222 return(EADDRINUSE); 223 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 224 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 225 struct sockaddr_in sin; 226 227 in6_sin6_2_sin(&sin, sin6); 228 t = in_pcblookup_local(pcbinfo, sin.sin_addr, 229 lport, wild); 230 if (t && 231 (reuseport & t->inp_socket->so_options) 232 == 0 && 233 (ntohl(t->inp_laddr.s_addr) 234 != INADDR_ANY || 235 INP_SOCKAF(so) == 236 INP_SOCKAF(t->inp_socket))) 237 return (EADDRINUSE); 238 } 239 } 240 inp->in6p_laddr = sin6->sin6_addr; 241 } 242 if (lport == 0) { 243 int e; 244 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, td)) != 0) 245 return(e); 246 } 247 else { 248 inp->inp_lport = lport; 249 if (in_pcbinshash(inp) != 0) { 250 inp->in6p_laddr = in6addr_any; 251 inp->inp_lport = 0; 252 return (EAGAIN); 253 } 254 } 255 return(0); 256} 257 258/* 259 * Transform old in6_pcbconnect() into an inner subroutine for new 260 * in6_pcbconnect(): Do some validity-checking on the remote 261 * address (in mbuf 'nam') and then determine local host address 262 * (i.e., which interface) to use to access that remote host. 263 * 264 * This preserves definition of in6_pcbconnect(), while supporting a 265 * slightly different version for T/TCP. (This is more than 266 * a bit of a kludge, but cleaning up the internal interfaces would 267 * have forced minor changes in every protocol). 268 */ 269 270int 271in6_pcbladdr(inp, nam, plocal_addr6) 272 register struct inpcb *inp; 273 struct sockaddr *nam; 274 struct in6_addr **plocal_addr6; 275{ 276 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 277 struct ifnet *ifp = NULL; 278 int error = 0; 279 280 if (nam->sa_len != sizeof (*sin6)) 281 return (EINVAL); 282 if (sin6->sin6_family != AF_INET6) 283 return (EAFNOSUPPORT); 284 if (sin6->sin6_port == 0) 285 return (EADDRNOTAVAIL); 286 287 /* KAME hack: embed scopeid */ 288 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0) 289 return EINVAL; 290 291 if (in6_ifaddr) { 292 /* 293 * If the destination address is UNSPECIFIED addr, 294 * use the loopback addr, e.g ::1. 295 */ 296 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 297 sin6->sin6_addr = in6addr_loopback; 298 } 299 { 300 /* 301 * XXX: in6_selectsrc might replace the bound local address 302 * with the address specified by setsockopt(IPV6_PKTINFO). 303 * Is it the intended behavior? 304 */ 305 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts, 306 inp->in6p_moptions, 307 &inp->in6p_route, 308 &inp->in6p_laddr, &error); 309 if (*plocal_addr6 == 0) { 310 if (error == 0) 311 error = EADDRNOTAVAIL; 312 return(error); 313 } 314 /* 315 * Don't do pcblookup call here; return interface in 316 * plocal_addr6 317 * and exit to caller, that will do the lookup. 318 */ 319 } 320 321 if (inp->in6p_route.ro_rt) 322 ifp = inp->in6p_route.ro_rt->rt_ifp; 323 324 return(0); 325} 326 327/* 328 * Outer subroutine: 329 * Connect from a socket to a specified address. 330 * Both address and port must be specified in argument sin. 331 * If don't have a local address for this socket yet, 332 * then pick one. 333 */ 334int 335in6_pcbconnect(inp, nam, td) 336 register struct inpcb *inp; 337 struct sockaddr *nam; 338 struct thread *td; 339{ 340 struct in6_addr *addr6; 341 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 342 int error; 343 344 /* 345 * Call inner routine, to assign local interface address. 346 */ 347 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0) 348 return(error); 349 350 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, 351 sin6->sin6_port, 352 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 353 ? addr6 : &inp->in6p_laddr, 354 inp->inp_lport, 0, NULL) != NULL) { 355 return (EADDRINUSE); 356 } 357 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 358 if (inp->inp_lport == 0) { 359 error = in6_pcbbind(inp, (struct sockaddr *)0, td); 360 if (error) 361 return (error); 362 } 363 inp->in6p_laddr = *addr6; 364 } 365 inp->in6p_faddr = sin6->sin6_addr; 366 inp->inp_fport = sin6->sin6_port; 367 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 368 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 369 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 370 inp->in6p_flowinfo |= 371 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK); 372 373 in_pcbrehash(inp); 374 return (0); 375} 376 377#if 0 378/* 379 * Return an IPv6 address, which is the most appropriate for given 380 * destination and user specified options. 381 * If necessary, this function lookups the routing table and return 382 * an entry to the caller for later use. 383 */ 384struct in6_addr * 385in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp) 386 struct sockaddr_in6 *dstsock; 387 struct ip6_pktopts *opts; 388 struct ip6_moptions *mopts; 389 struct route_in6 *ro; 390 struct in6_addr *laddr; 391 int *errorp; 392{ 393 struct in6_addr *dst; 394 struct in6_ifaddr *ia6 = 0; 395 struct in6_pktinfo *pi = NULL; 396 397 dst = &dstsock->sin6_addr; 398 *errorp = 0; 399 400 /* 401 * If the source address is explicitly specified by the caller, 402 * use it. 403 */ 404 if (opts && (pi = opts->ip6po_pktinfo) && 405 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) 406 return(&pi->ipi6_addr); 407 408 /* 409 * If the source address is not specified but the socket(if any) 410 * is already bound, use the bound address. 411 */ 412 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) 413 return(laddr); 414 415 /* 416 * If the caller doesn't specify the source address but 417 * the outgoing interface, use an address associated with 418 * the interface. 419 */ 420 if (pi && pi->ipi6_ifindex) { 421 /* XXX boundary check is assumed to be already done. */ 422 ia6 = in6_ifawithscope(ifnet_byindex(pi->ipi6_ifindex), dst); 423 if (ia6 == 0) { 424 *errorp = EADDRNOTAVAIL; 425 return(0); 426 } 427 return(&satosin6(&ia6->ia_addr)->sin6_addr); 428 } 429 430 /* 431 * If the destination address is a link-local unicast address or 432 * a multicast address, and if the outgoing interface is specified 433 * by the sin6_scope_id filed, use an address associated with the 434 * interface. 435 * XXX: We're now trying to define more specific semantics of 436 * sin6_scope_id field, so this part will be rewritten in 437 * the near future. 438 */ 439 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) && 440 dstsock->sin6_scope_id) { 441 /* 442 * I'm not sure if boundary check for scope_id is done 443 * somewhere... 444 */ 445 if (dstsock->sin6_scope_id < 0 || 446 if_index < dstsock->sin6_scope_id) { 447 *errorp = ENXIO; /* XXX: better error? */ 448 return(0); 449 } 450 ia6 = in6_ifawithscope(ifnet_byindex(dstsock->sin6_scope_id), 451 dst); 452 if (ia6 == 0) { 453 *errorp = EADDRNOTAVAIL; 454 return(0); 455 } 456 return(&satosin6(&ia6->ia_addr)->sin6_addr); 457 } 458 459 /* 460 * If the destination address is a multicast address and 461 * the outgoing interface for the address is specified 462 * by the caller, use an address associated with the interface. 463 * There is a sanity check here; if the destination has node-local 464 * scope, the outgoing interfacde should be a loopback address. 465 * Even if the outgoing interface is not specified, we also 466 * choose a loopback interface as the outgoing interface. 467 */ 468 if (IN6_IS_ADDR_MULTICAST(dst)) { 469 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL; 470 471 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) { 472 ifp = &loif[0]; 473 } 474 475 if (ifp) { 476 ia6 = in6_ifawithscope(ifp, dst); 477 if (ia6 == 0) { 478 *errorp = EADDRNOTAVAIL; 479 return(0); 480 } 481 return(&ia6->ia_addr.sin6_addr); 482 } 483 } 484 485 /* 486 * If the next hop address for the packet is specified 487 * by caller, use an address associated with the route 488 * to the next hop. 489 */ 490 { 491 struct sockaddr_in6 *sin6_next; 492 struct rtentry *rt; 493 494 if (opts && opts->ip6po_nexthop) { 495 sin6_next = satosin6(opts->ip6po_nexthop); 496 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL); 497 if (rt) { 498 ia6 = in6_ifawithscope(rt->rt_ifp, dst); 499 if (ia6 == 0) 500 ia6 = ifatoia6(rt->rt_ifa); 501 } 502 if (ia6 == 0) { 503 *errorp = EADDRNOTAVAIL; 504 return(0); 505 } 506 return(&satosin6(&ia6->ia_addr)->sin6_addr); 507 } 508 } 509 510 /* 511 * If route is known or can be allocated now, 512 * our src addr is taken from the i/f, else punt. 513 */ 514 if (ro) { 515 if (ro->ro_rt && 516 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) { 517 RTFREE(ro->ro_rt); 518 ro->ro_rt = (struct rtentry *)0; 519 } 520 if (ro->ro_rt == (struct rtentry *)0 || 521 ro->ro_rt->rt_ifp == (struct ifnet *)0) { 522 struct sockaddr_in6 *dst6; 523 524 /* No route yet, so try to acquire one */ 525 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); 526 dst6 = (struct sockaddr_in6 *)&ro->ro_dst; 527 dst6->sin6_family = AF_INET6; 528 dst6->sin6_len = sizeof(struct sockaddr_in6); 529 dst6->sin6_addr = *dst; 530 if (IN6_IS_ADDR_MULTICAST(dst)) { 531 ro->ro_rt = rtalloc1(&((struct route *)ro) 532 ->ro_dst, 0, 0UL); 533 } else { 534 rtalloc((struct route *)ro); 535 } 536 } 537 538 /* 539 * in_pcbconnect() checks out IFF_LOOPBACK to skip using 540 * the address. But we don't know why it does so. 541 * It is necessary to ensure the scope even for lo0 542 * so doesn't check out IFF_LOOPBACK. 543 */ 544 545 if (ro->ro_rt) { 546 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst); 547 if (ia6 == 0) /* xxx scope error ?*/ 548 ia6 = ifatoia6(ro->ro_rt->rt_ifa); 549 } 550 if (ia6 == 0) { 551 *errorp = EHOSTUNREACH; /* no route */ 552 return(0); 553 } 554 return(&satosin6(&ia6->ia_addr)->sin6_addr); 555 } 556 557 *errorp = EADDRNOTAVAIL; 558 return(0); 559} 560 561/* 562 * Default hop limit selection. The precedence is as follows: 563 * 1. Hoplimit valued specified via ioctl. 564 * 2. (If the outgoing interface is detected) the current 565 * hop limit of the interface specified by router advertisement. 566 * 3. The system default hoplimit. 567*/ 568int 569in6_selecthlim(in6p, ifp) 570 struct in6pcb *in6p; 571 struct ifnet *ifp; 572{ 573 if (in6p && in6p->in6p_hops >= 0) 574 return(in6p->in6p_hops); 575 else if (ifp) 576 return(nd_ifinfo[ifp->if_index].chlim); 577 else 578 return(ip6_defhlim); 579} 580#endif 581 582void 583in6_pcbdisconnect(inp) 584 struct inpcb *inp; 585{ 586 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr)); 587 inp->inp_fport = 0; 588 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 589 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 590 in_pcbrehash(inp); 591 if (inp->inp_socket->so_state & SS_NOFDREF) 592 in6_pcbdetach(inp); 593} 594 595void 596in6_pcbdetach(inp) 597 struct inpcb *inp; 598{ 599 struct socket *so = inp->inp_socket; 600 struct inpcbinfo *ipi = inp->inp_pcbinfo; 601 602#ifdef IPSEC 603 if (inp->in6p_sp != NULL) 604 ipsec6_delete_pcbpolicy(inp); 605#endif /* IPSEC */ 606 inp->inp_gencnt = ++ipi->ipi_gencnt; 607 in_pcbremlists(inp); 608 sotoinpcb(so) = 0; 609 sofree(so); 610 611 if (inp->in6p_options) 612 m_freem(inp->in6p_options); 613 ip6_freepcbopts(inp->in6p_outputopts); 614 ip6_freemoptions(inp->in6p_moptions); 615 if (inp->in6p_route.ro_rt) 616 rtfree(inp->in6p_route.ro_rt); 617 /* Check and free IPv4 related resources in case of mapped addr */ 618 if (inp->inp_options) 619 (void)m_free(inp->inp_options); 620 ip_freemoptions(inp->inp_moptions); 621 622 inp->inp_vflag = 0; 623 zfree(ipi->ipi_zone, inp); 624} 625 626/* 627 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was 628 * modified to match the pru_sockaddr() and pru_peeraddr() entry points 629 * in struct pr_usrreqs, so that protocols can just reference then directly 630 * without the need for a wrapper function. The socket must have a valid 631 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one 632 * except through a kernel programming error, so it is acceptable to panic 633 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap 634 * because there actually /is/ a programming error somewhere... XXX) 635 */ 636int 637in6_setsockaddr(so, nam) 638 struct socket *so; 639 struct sockaddr **nam; 640{ 641 int s; 642 register struct inpcb *inp; 643 register struct sockaddr_in6 *sin6; 644 645 /* 646 * Do the malloc first in case it blocks. 647 */ 648 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK); 649 bzero(sin6, sizeof *sin6); 650 sin6->sin6_family = AF_INET6; 651 sin6->sin6_len = sizeof(*sin6); 652 653 s = splnet(); 654 inp = sotoinpcb(so); 655 if (!inp) { 656 splx(s); 657 free(sin6, M_SONAME); 658 return EINVAL; 659 } 660 sin6->sin6_port = inp->inp_lport; 661 sin6->sin6_addr = inp->in6p_laddr; 662 splx(s); 663 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 664 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 665 else 666 sin6->sin6_scope_id = 0; /*XXX*/ 667 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 668 sin6->sin6_addr.s6_addr16[1] = 0; 669 670 *nam = (struct sockaddr *)sin6; 671 return 0; 672} 673 674int 675in6_setpeeraddr(so, nam) 676 struct socket *so; 677 struct sockaddr **nam; 678{ 679 int s; 680 struct inpcb *inp; 681 register struct sockaddr_in6 *sin6; 682 683 /* 684 * Do the malloc first in case it blocks. 685 */ 686 MALLOC(sin6, struct sockaddr_in6 *, sizeof(*sin6), M_SONAME, M_WAITOK); 687 bzero((caddr_t)sin6, sizeof (*sin6)); 688 sin6->sin6_family = AF_INET6; 689 sin6->sin6_len = sizeof(struct sockaddr_in6); 690 691 s = splnet(); 692 inp = sotoinpcb(so); 693 if (!inp) { 694 splx(s); 695 free(sin6, M_SONAME); 696 return EINVAL; 697 } 698 sin6->sin6_port = inp->inp_fport; 699 sin6->sin6_addr = inp->in6p_faddr; 700 splx(s); 701 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 702 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); 703 else 704 sin6->sin6_scope_id = 0; /*XXX*/ 705 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) 706 sin6->sin6_addr.s6_addr16[1] = 0; 707 708 *nam = (struct sockaddr *)sin6; 709 return 0; 710} 711 712int 713in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 714{ 715 struct inpcb *inp = sotoinpcb(so); 716 int error; 717 718 if (inp == NULL) 719 return EINVAL; 720 if (inp->inp_vflag & INP_IPV4) { 721 error = in_setsockaddr(so, nam); 722 if (error == 0) 723 in6_sin_2_v4mapsin6_in_sock(nam); 724 } else 725 error = in6_setsockaddr(so, nam); 726 727 return error; 728} 729 730int 731in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 732{ 733 struct inpcb *inp = sotoinpcb(so); 734 int error; 735 736 if (inp == NULL) 737 return EINVAL; 738 if (inp->inp_vflag & INP_IPV4) { 739 error = in_setpeeraddr(so, nam); 740 if (error == 0) 741 in6_sin_2_v4mapsin6_in_sock(nam); 742 } else 743 error = in6_setpeeraddr(so, nam); 744 745 return error; 746} 747 748/* 749 * Pass some notification to all connections of a protocol 750 * associated with address dst. The local address and/or port numbers 751 * may be specified to limit the search. The "usual action" will be 752 * taken, depending on the ctlinput cmd. The caller must filter any 753 * cmds that are uninteresting (e.g., no error in the map). 754 * Call the protocol specific routine (if any) to report 755 * any errors for each matching socket. 756 * 757 * Must be called at splnet. 758 */ 759void 760in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, notify) 761 struct inpcbhead *head; 762 struct sockaddr *dst, *src; 763 u_int fport_arg, lport_arg; 764 int cmd; 765 void (*notify) __P((struct inpcb *, int)); 766{ 767 struct inpcb *inp, *ninp; 768 struct sockaddr_in6 sa6_src, *sa6_dst; 769 u_short fport = fport_arg, lport = lport_arg; 770 u_int32_t flowinfo; 771 int errno, s; 772 773 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6) 774 return; 775 776 sa6_dst = (struct sockaddr_in6 *)dst; 777 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 778 return; 779 780 /* 781 * note that src can be NULL when we get notify by local fragmentation. 782 */ 783 sa6_src = (src == NULL) ? sa6_any : *(struct sockaddr_in6 *)src; 784 flowinfo = sa6_src.sin6_flowinfo; 785 786 /* 787 * Redirects go to all references to the destination, 788 * and use in6_rtchange to invalidate the route cache. 789 * Dead host indications: also use in6_rtchange to invalidate 790 * the cache, and deliver the error to all the sockets. 791 * Otherwise, if we have knowledge of the local port and address, 792 * deliver only to that socket. 793 */ 794 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 795 fport = 0; 796 lport = 0; 797 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 798 799 if (cmd != PRC_HOSTDEAD) 800 notify = in6_rtchange; 801 } 802 errno = inet6ctlerrmap[cmd]; 803 s = splnet(); 804 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) { 805 ninp = LIST_NEXT(inp, inp_list); 806 807 if ((inp->inp_vflag & INP_IPV6) == 0) 808 continue; 809 810 /* 811 * Detect if we should notify the error. If no source and 812 * destination ports are specifed, but non-zero flowinfo and 813 * local address match, notify the error. This is the case 814 * when the error is delivered with an encrypted buffer 815 * by ESP. Otherwise, just compare addresses and ports 816 * as usual. 817 */ 818 if (lport == 0 && fport == 0 && flowinfo && 819 inp->inp_socket != NULL && 820 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && 821 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 822 goto do_notify; 823 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 824 &sa6_dst->sin6_addr) || 825 inp->inp_socket == 0 || 826 (lport && inp->inp_lport != lport) || 827 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 828 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 829 &sa6_src.sin6_addr)) || 830 (fport && inp->inp_fport != fport)) 831 continue; 832 833 do_notify: 834 if (notify) 835 (*notify)(inp, errno); 836 } 837 splx(s); 838} 839 840/* 841 * Lookup a PCB based on the local address and port. 842 */ 843struct inpcb * 844in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay) 845 struct inpcbinfo *pcbinfo; 846 struct in6_addr *laddr; 847 u_int lport_arg; 848 int wild_okay; 849{ 850 register struct inpcb *inp; 851 int matchwild = 3, wildcard; 852 u_short lport = lport_arg; 853 854 if (!wild_okay) { 855 struct inpcbhead *head; 856 /* 857 * Look for an unconnected (wildcard foreign addr) PCB that 858 * matches the local address and port we're looking for. 859 */ 860 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, 861 pcbinfo->hashmask)]; 862 LIST_FOREACH(inp, head, inp_hash) { 863 if ((inp->inp_vflag & INP_IPV6) == 0) 864 continue; 865 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 866 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 867 inp->inp_lport == lport) { 868 /* 869 * Found. 870 */ 871 return (inp); 872 } 873 } 874 /* 875 * Not found. 876 */ 877 return (NULL); 878 } else { 879 struct inpcbporthead *porthash; 880 struct inpcbport *phd; 881 struct inpcb *match = NULL; 882 /* 883 * Best fit PCB lookup. 884 * 885 * First see if this local port is in use by looking on the 886 * port hash list. 887 */ 888 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport, 889 pcbinfo->porthashmask)]; 890 LIST_FOREACH(phd, porthash, phd_hash) { 891 if (phd->phd_port == lport) 892 break; 893 } 894 if (phd != NULL) { 895 /* 896 * Port is in use by one or more PCBs. Look for best 897 * fit. 898 */ 899 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 900 wildcard = 0; 901 if ((inp->inp_vflag & INP_IPV6) == 0) 902 continue; 903 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 904 wildcard++; 905 if (!IN6_IS_ADDR_UNSPECIFIED( 906 &inp->in6p_laddr)) { 907 if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 908 wildcard++; 909 else if (!IN6_ARE_ADDR_EQUAL( 910 &inp->in6p_laddr, laddr)) 911 continue; 912 } else { 913 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 914 wildcard++; 915 } 916 if (wildcard < matchwild) { 917 match = inp; 918 matchwild = wildcard; 919 if (matchwild == 0) { 920 break; 921 } 922 } 923 } 924 } 925 return (match); 926 } 927} 928 929void 930in6_pcbpurgeif0(head, ifp) 931 struct in6pcb *head; 932 struct ifnet *ifp; 933{ 934 struct in6pcb *in6p; 935 struct ip6_moptions *im6o; 936 struct in6_multi_mship *imm, *nimm; 937 938 for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) { 939 im6o = in6p->in6p_moptions; 940 if ((in6p->inp_vflag & INP_IPV6) && 941 im6o) { 942 /* 943 * Unselect the outgoing interface if it is being 944 * detached. 945 */ 946 if (im6o->im6o_multicast_ifp == ifp) 947 im6o->im6o_multicast_ifp = NULL; 948 949 /* 950 * Drop multicast group membership if we joined 951 * through the interface being detached. 952 * XXX controversial - is it really legal for kernel 953 * to force this? 954 */ 955 for (imm = im6o->im6o_memberships.lh_first; 956 imm != NULL; imm = nimm) { 957 nimm = imm->i6mm_chain.le_next; 958 if (imm->i6mm_maddr->in6m_ifp == ifp) { 959 LIST_REMOVE(imm, i6mm_chain); 960 in6_delmulti(imm->i6mm_maddr); 961 free(imm, M_IPMADDR); 962 } 963 } 964 } 965 } 966} 967 968/* 969 * Check for alternatives when higher level complains 970 * about service problems. For now, invalidate cached 971 * routing information. If the route was created dynamically 972 * (by a redirect), time to try a default gateway again. 973 */ 974void 975in6_losing(in6p) 976 struct inpcb *in6p; 977{ 978 struct rtentry *rt; 979 struct rt_addrinfo info; 980 981 if ((rt = in6p->in6p_route.ro_rt) != NULL) { 982 in6p->in6p_route.ro_rt = 0; 983 bzero((caddr_t)&info, sizeof(info)); 984 info.rti_info[RTAX_DST] = 985 (struct sockaddr *)&in6p->in6p_route.ro_dst; 986 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 987 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 988 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 989 if (rt->rt_flags & RTF_DYNAMIC) 990 (void)rtrequest(RTM_DELETE, rt_key(rt), 991 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 992 (struct rtentry **)0); 993 else 994 /* 995 * A new route can be allocated 996 * the next time output is attempted. 997 */ 998 rtfree(rt); 999 } 1000} 1001 1002/* 1003 * After a routing change, flush old routing 1004 * and allocate a (hopefully) better one. 1005 */ 1006void 1007in6_rtchange(inp, errno) 1008 struct inpcb *inp; 1009 int errno; 1010{ 1011 if (inp->in6p_route.ro_rt) { 1012 rtfree(inp->in6p_route.ro_rt); 1013 inp->in6p_route.ro_rt = 0; 1014 /* 1015 * A new route can be allocated the next time 1016 * output is attempted. 1017 */ 1018 } 1019} 1020 1021/* 1022 * Lookup PCB in hash list. 1023 */ 1024struct inpcb * 1025in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp) 1026 struct inpcbinfo *pcbinfo; 1027 struct in6_addr *faddr, *laddr; 1028 u_int fport_arg, lport_arg; 1029 int wildcard; 1030 struct ifnet *ifp; 1031{ 1032 struct inpcbhead *head; 1033 register struct inpcb *inp; 1034 u_short fport = fport_arg, lport = lport_arg; 1035 int faith; 1036
|