| 1 | /* $NetBSD: in6_pcb.c,v 1.152 2016/10/31 14:34:32 christos Exp $ */ |
| 2 | /* $KAME: in6_pcb.c,v 1.84 2001/02/08 18:02:08 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. Neither the name of the University nor the names of its contributors |
| 46 | * may be used to endorse or promote products derived from this software |
| 47 | * without specific prior written permission. |
| 48 | * |
| 49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 52 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 53 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 54 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 55 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 56 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 57 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 58 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 59 | * SUCH DAMAGE. |
| 60 | * |
| 61 | * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 |
| 62 | */ |
| 63 | |
| 64 | #include <sys/cdefs.h> |
| 65 | __KERNEL_RCSID(0, "$NetBSD: in6_pcb.c,v 1.152 2016/10/31 14:34:32 christos Exp $" ); |
| 66 | |
| 67 | #ifdef _KERNEL_OPT |
| 68 | #include "opt_inet.h" |
| 69 | #include "opt_ipsec.h" |
| 70 | #endif |
| 71 | |
| 72 | #include <sys/param.h> |
| 73 | #include <sys/systm.h> |
| 74 | #include <sys/mbuf.h> |
| 75 | #include <sys/protosw.h> |
| 76 | #include <sys/socket.h> |
| 77 | #include <sys/socketvar.h> |
| 78 | #include <sys/ioctl.h> |
| 79 | #include <sys/errno.h> |
| 80 | #include <sys/time.h> |
| 81 | #include <sys/proc.h> |
| 82 | #include <sys/kauth.h> |
| 83 | #include <sys/domain.h> |
| 84 | #include <sys/once.h> |
| 85 | |
| 86 | #include <net/if.h> |
| 87 | #include <net/route.h> |
| 88 | |
| 89 | #include <netinet/in.h> |
| 90 | #include <netinet/in_var.h> |
| 91 | #include <netinet/in_systm.h> |
| 92 | #include <netinet/ip.h> |
| 93 | #include <netinet/in_pcb.h> |
| 94 | #include <netinet/ip6.h> |
| 95 | #include <netinet/portalgo.h> |
| 96 | #include <netinet6/ip6_var.h> |
| 97 | #include <netinet6/in6_pcb.h> |
| 98 | #include <netinet6/scope6_var.h> |
| 99 | #include <netinet6/nd6.h> |
| 100 | |
| 101 | #include "faith.h" |
| 102 | |
| 103 | #ifdef IPSEC |
| 104 | #include <netipsec/ipsec.h> |
| 105 | #include <netipsec/ipsec6.h> |
| 106 | #include <netipsec/key.h> |
| 107 | #endif /* IPSEC */ |
| 108 | |
| 109 | #include <netinet/tcp_vtw.h> |
| 110 | |
| 111 | const struct in6_addr zeroin6_addr; |
| 112 | |
| 113 | #define IN6PCBHASH_PORT(table, lport) \ |
| 114 | &(table)->inpt_porthashtbl[ntohs(lport) & (table)->inpt_porthash] |
| 115 | #define IN6PCBHASH_BIND(table, laddr, lport) \ |
| 116 | &(table)->inpt_bindhashtbl[ \ |
| 117 | (((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \ |
| 118 | (laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + ntohs(lport)) & \ |
| 119 | (table)->inpt_bindhash] |
| 120 | #define IN6PCBHASH_CONNECT(table, faddr, fport, laddr, lport) \ |
| 121 | &(table)->inpt_bindhashtbl[ \ |
| 122 | ((((faddr)->s6_addr32[0] ^ (faddr)->s6_addr32[1] ^ \ |
| 123 | (faddr)->s6_addr32[2] ^ (faddr)->s6_addr32[3]) + ntohs(fport)) + \ |
| 124 | (((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \ |
| 125 | (laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + \ |
| 126 | ntohs(lport))) & (table)->inpt_bindhash] |
| 127 | |
| 128 | int ip6_anonportmin = IPV6PORT_ANONMIN; |
| 129 | int ip6_anonportmax = IPV6PORT_ANONMAX; |
| 130 | int ip6_lowportmin = IPV6PORT_RESERVEDMIN; |
| 131 | int ip6_lowportmax = IPV6PORT_RESERVEDMAX; |
| 132 | |
| 133 | static struct pool in6pcb_pool; |
| 134 | |
| 135 | static int |
| 136 | in6pcb_poolinit(void) |
| 137 | { |
| 138 | |
| 139 | pool_init(&in6pcb_pool, sizeof(struct in6pcb), 0, 0, 0, "in6pcbpl" , |
| 140 | NULL, IPL_SOFTNET); |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | in6_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize) |
| 146 | { |
| 147 | static ONCE_DECL(control); |
| 148 | |
| 149 | in_pcbinit(table, bindhashsize, connecthashsize); |
| 150 | table->inpt_lastport = (u_int16_t)ip6_anonportmax; |
| 151 | |
| 152 | RUN_ONCE(&control, in6pcb_poolinit); |
| 153 | } |
| 154 | |
| 155 | int |
| 156 | in6_pcballoc(struct socket *so, void *v) |
| 157 | { |
| 158 | struct inpcbtable *table = v; |
| 159 | struct in6pcb *in6p; |
| 160 | int s; |
| 161 | |
| 162 | s = splnet(); |
| 163 | in6p = pool_get(&in6pcb_pool, PR_NOWAIT); |
| 164 | splx(s); |
| 165 | if (in6p == NULL) |
| 166 | return (ENOBUFS); |
| 167 | memset((void *)in6p, 0, sizeof(*in6p)); |
| 168 | in6p->in6p_af = AF_INET6; |
| 169 | in6p->in6p_table = table; |
| 170 | in6p->in6p_socket = so; |
| 171 | in6p->in6p_hops = -1; /* use kernel default */ |
| 172 | in6p->in6p_icmp6filt = NULL; |
| 173 | in6p->in6p_portalgo = PORTALGO_DEFAULT; |
| 174 | in6p->in6p_bindportonsend = false; |
| 175 | #if defined(IPSEC) |
| 176 | if (ipsec_enabled) { |
| 177 | int error = ipsec_init_pcbpolicy(so, &in6p->in6p_sp); |
| 178 | if (error != 0) { |
| 179 | s = splnet(); |
| 180 | pool_put(&in6pcb_pool, in6p); |
| 181 | splx(s); |
| 182 | return error; |
| 183 | } |
| 184 | } |
| 185 | #endif /* IPSEC */ |
| 186 | s = splnet(); |
| 187 | TAILQ_INSERT_HEAD(&table->inpt_queue, (struct inpcb_hdr*)in6p, |
| 188 | inph_queue); |
| 189 | LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport), |
| 190 | &in6p->in6p_head, inph_lhash); |
| 191 | in6_pcbstate(in6p, IN6P_ATTACHED); |
| 192 | splx(s); |
| 193 | if (ip6_v6only) |
| 194 | in6p->in6p_flags |= IN6P_IPV6_V6ONLY; |
| 195 | so->so_pcb = (void *)in6p; |
| 196 | return (0); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Bind address from sin6 to in6p. |
| 201 | */ |
| 202 | static int |
| 203 | in6_pcbbind_addr(struct in6pcb *in6p, struct sockaddr_in6 *sin6, struct lwp *l) |
| 204 | { |
| 205 | int error; |
| 206 | int s; |
| 207 | |
| 208 | /* |
| 209 | * We should check the family, but old programs |
| 210 | * incorrectly fail to intialize it. |
| 211 | */ |
| 212 | if (sin6->sin6_family != AF_INET6) |
| 213 | return (EAFNOSUPPORT); |
| 214 | |
| 215 | #ifndef INET |
| 216 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) |
| 217 | return (EADDRNOTAVAIL); |
| 218 | #endif |
| 219 | |
| 220 | if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0) |
| 221 | return (error); |
| 222 | |
| 223 | s = pserialize_read_enter(); |
| 224 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { |
| 225 | if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) { |
| 226 | error = EINVAL; |
| 227 | goto out; |
| 228 | } |
| 229 | if (sin6->sin6_addr.s6_addr32[3]) { |
| 230 | struct sockaddr_in sin; |
| 231 | |
| 232 | memset(&sin, 0, sizeof(sin)); |
| 233 | sin.sin_len = sizeof(sin); |
| 234 | sin.sin_family = AF_INET; |
| 235 | bcopy(&sin6->sin6_addr.s6_addr32[3], |
| 236 | &sin.sin_addr, sizeof(sin.sin_addr)); |
| 237 | if (!IN_MULTICAST(sin.sin_addr.s_addr)) { |
| 238 | struct ifaddr *ifa; |
| 239 | ifa = ifa_ifwithaddr((struct sockaddr *)&sin); |
| 240 | if (ifa == NULL) { |
| 241 | error = EADDRNOTAVAIL; |
| 242 | goto out; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { |
| 247 | // succeed |
| 248 | } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
| 249 | struct ifaddr *ifa = NULL; |
| 250 | |
| 251 | if ((in6p->in6p_flags & IN6P_FAITH) == 0) { |
| 252 | ifa = ifa_ifwithaddr(sin6tosa(sin6)); |
| 253 | if (ifa == NULL) { |
| 254 | error = EADDRNOTAVAIL; |
| 255 | goto out; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * bind to an anycast address might accidentally |
| 261 | * cause sending a packet with an anycast source |
| 262 | * address, so we forbid it. |
| 263 | * |
| 264 | * We should allow to bind to a deprecated address, |
| 265 | * since the application dare to use it. |
| 266 | * But, can we assume that they are careful enough |
| 267 | * to check if the address is deprecated or not? |
| 268 | * Maybe, as a safeguard, we should have a setsockopt |
| 269 | * flag to control the bind(2) behavior against |
| 270 | * deprecated addresses (default: forbid bind(2)). |
| 271 | */ |
| 272 | if (ifa && |
| 273 | ifatoia6(ifa)->ia6_flags & |
| 274 | (IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED)) { |
| 275 | error = EADDRNOTAVAIL; |
| 276 | goto out; |
| 277 | } |
| 278 | } |
| 279 | in6p->in6p_laddr = sin6->sin6_addr; |
| 280 | error = 0; |
| 281 | out: |
| 282 | pserialize_read_exit(s); |
| 283 | return error; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Bind port from sin6 to in6p. |
| 288 | */ |
| 289 | static int |
| 290 | in6_pcbbind_port(struct in6pcb *in6p, struct sockaddr_in6 *sin6, struct lwp *l) |
| 291 | { |
| 292 | struct inpcbtable *table = in6p->in6p_table; |
| 293 | struct socket *so = in6p->in6p_socket; |
| 294 | int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); |
| 295 | int error; |
| 296 | |
| 297 | if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 && |
| 298 | ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 || |
| 299 | (so->so_options & SO_ACCEPTCONN) == 0)) |
| 300 | wild = 1; |
| 301 | |
| 302 | if (sin6->sin6_port != 0) { |
| 303 | enum kauth_network_req req; |
| 304 | |
| 305 | #ifndef IPNOPRIVPORTS |
| 306 | if (ntohs(sin6->sin6_port) < IPV6PORT_RESERVED) |
| 307 | req = KAUTH_REQ_NETWORK_BIND_PRIVPORT; |
| 308 | else |
| 309 | #endif /* IPNOPRIVPORTS */ |
| 310 | req = KAUTH_REQ_NETWORK_BIND_PORT; |
| 311 | |
| 312 | error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_BIND, |
| 313 | req, so, sin6, NULL); |
| 314 | if (error) |
| 315 | return (EACCES); |
| 316 | } |
| 317 | |
| 318 | if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { |
| 319 | /* |
| 320 | * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; |
| 321 | * allow compepte duplication of binding if |
| 322 | * SO_REUSEPORT is set, or if SO_REUSEADDR is set |
| 323 | * and a multicast address is bound on both |
| 324 | * new and duplicated sockets. |
| 325 | */ |
| 326 | if (so->so_options & (SO_REUSEADDR | SO_REUSEPORT)) |
| 327 | reuseport = SO_REUSEADDR|SO_REUSEPORT; |
| 328 | } |
| 329 | |
| 330 | if (sin6->sin6_port != 0) { |
| 331 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { |
| 332 | #ifdef INET |
| 333 | struct inpcb *t; |
| 334 | struct vestigial_inpcb vestige; |
| 335 | |
| 336 | t = in_pcblookup_port(table, |
| 337 | *(struct in_addr *)&sin6->sin6_addr.s6_addr32[3], |
| 338 | sin6->sin6_port, wild, &vestige); |
| 339 | if (t && (reuseport & t->inp_socket->so_options) == 0) |
| 340 | return (EADDRINUSE); |
| 341 | if (!t |
| 342 | && vestige.valid |
| 343 | && !(reuseport && vestige.reuse_port)) |
| 344 | return EADDRINUSE; |
| 345 | #else |
| 346 | return (EADDRNOTAVAIL); |
| 347 | #endif |
| 348 | } |
| 349 | |
| 350 | { |
| 351 | struct in6pcb *t; |
| 352 | struct vestigial_inpcb vestige; |
| 353 | |
| 354 | t = in6_pcblookup_port(table, &sin6->sin6_addr, |
| 355 | sin6->sin6_port, wild, &vestige); |
| 356 | if (t && (reuseport & t->in6p_socket->so_options) == 0) |
| 357 | return (EADDRINUSE); |
| 358 | if (!t |
| 359 | && vestige.valid |
| 360 | && !(reuseport && vestige.reuse_port)) |
| 361 | return EADDRINUSE; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | if (sin6->sin6_port == 0) { |
| 366 | int e; |
| 367 | e = in6_pcbsetport(sin6, in6p, l); |
| 368 | if (e != 0) |
| 369 | return (e); |
| 370 | } else { |
| 371 | in6p->in6p_lport = sin6->sin6_port; |
| 372 | in6_pcbstate(in6p, IN6P_BOUND); |
| 373 | } |
| 374 | |
| 375 | LIST_REMOVE(&in6p->in6p_head, inph_lhash); |
| 376 | LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport), |
| 377 | &in6p->in6p_head, inph_lhash); |
| 378 | |
| 379 | return (0); |
| 380 | } |
| 381 | |
| 382 | int |
| 383 | in6_pcbbind(void *v, struct sockaddr_in6 *sin6, struct lwp *l) |
| 384 | { |
| 385 | struct in6pcb *in6p = v; |
| 386 | struct sockaddr_in6 lsin6; |
| 387 | int error; |
| 388 | |
| 389 | if (in6p->in6p_af != AF_INET6) |
| 390 | return (EINVAL); |
| 391 | |
| 392 | /* |
| 393 | * If we already have a local port or a local address it means we're |
| 394 | * bounded. |
| 395 | */ |
| 396 | if (in6p->in6p_lport || !(IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) || |
| 397 | (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) && |
| 398 | in6p->in6p_laddr.s6_addr32[3] == 0))) |
| 399 | return (EINVAL); |
| 400 | |
| 401 | if (NULL != sin6) { |
| 402 | /* We were provided a sockaddr_in6 to use. */ |
| 403 | if (sin6->sin6_len != sizeof(*sin6)) |
| 404 | return (EINVAL); |
| 405 | } else { |
| 406 | /* We always bind to *something*, even if it's "anything". */ |
| 407 | lsin6 = *((const struct sockaddr_in6 *) |
| 408 | in6p->in6p_socket->so_proto->pr_domain->dom_sa_any); |
| 409 | sin6 = &lsin6; |
| 410 | } |
| 411 | |
| 412 | /* Bind address. */ |
| 413 | error = in6_pcbbind_addr(in6p, sin6, l); |
| 414 | if (error) |
| 415 | return (error); |
| 416 | |
| 417 | /* Bind port. */ |
| 418 | error = in6_pcbbind_port(in6p, sin6, l); |
| 419 | if (error) { |
| 420 | /* |
| 421 | * Reset the address here to "any" so we don't "leak" the |
| 422 | * in6pcb. |
| 423 | */ |
| 424 | in6p->in6p_laddr = in6addr_any; |
| 425 | |
| 426 | return (error); |
| 427 | } |
| 428 | |
| 429 | |
| 430 | #if 0 |
| 431 | in6p->in6p_flowinfo = 0; /* XXX */ |
| 432 | #endif |
| 433 | return (0); |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Connect from a socket to a specified address. |
| 438 | * Both address and port must be specified in argument sin6. |
| 439 | * If don't have a local address for this socket yet, |
| 440 | * then pick one. |
| 441 | */ |
| 442 | int |
| 443 | in6_pcbconnect(void *v, struct sockaddr_in6 *sin6, struct lwp *l) |
| 444 | { |
| 445 | struct in6pcb *in6p = v; |
| 446 | struct in6_addr *in6a = NULL; |
| 447 | struct in6_addr ia6; |
| 448 | struct ifnet *ifp = NULL; /* outgoing interface */ |
| 449 | int error = 0; |
| 450 | int scope_ambiguous = 0; |
| 451 | #ifdef INET |
| 452 | struct in6_addr mapped; |
| 453 | #endif |
| 454 | struct sockaddr_in6 tmp; |
| 455 | struct vestigial_inpcb vestige; |
| 456 | struct psref psref; |
| 457 | int bound; |
| 458 | |
| 459 | (void)&in6a; /* XXX fool gcc */ |
| 460 | |
| 461 | if (in6p->in6p_af != AF_INET6) |
| 462 | return (EINVAL); |
| 463 | |
| 464 | if (sin6->sin6_len != sizeof(*sin6)) |
| 465 | return (EINVAL); |
| 466 | if (sin6->sin6_family != AF_INET6) |
| 467 | return (EAFNOSUPPORT); |
| 468 | if (sin6->sin6_port == 0) |
| 469 | return (EADDRNOTAVAIL); |
| 470 | |
| 471 | if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) && |
| 472 | in6p->in6p_socket->so_type == SOCK_STREAM) |
| 473 | return EADDRNOTAVAIL; |
| 474 | |
| 475 | if (sin6->sin6_scope_id == 0 && !ip6_use_defzone) |
| 476 | scope_ambiguous = 1; |
| 477 | if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0) |
| 478 | return(error); |
| 479 | |
| 480 | /* sanity check for mapped address case */ |
| 481 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { |
| 482 | if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) |
| 483 | return EINVAL; |
| 484 | if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) |
| 485 | in6p->in6p_laddr.s6_addr16[5] = htons(0xffff); |
| 486 | if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) |
| 487 | return EINVAL; |
| 488 | } else |
| 489 | { |
| 490 | if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) |
| 491 | return EINVAL; |
| 492 | } |
| 493 | |
| 494 | /* protect *sin6 from overwrites */ |
| 495 | tmp = *sin6; |
| 496 | sin6 = &tmp; |
| 497 | |
| 498 | bound = curlwp_bind(); |
| 499 | /* Source address selection. */ |
| 500 | if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) && |
| 501 | in6p->in6p_laddr.s6_addr32[3] == 0) { |
| 502 | #ifdef INET |
| 503 | struct sockaddr_in sin; |
| 504 | struct in_ifaddr *ia4; |
| 505 | struct psref _psref; |
| 506 | |
| 507 | memset(&sin, 0, sizeof(sin)); |
| 508 | sin.sin_len = sizeof(sin); |
| 509 | sin.sin_family = AF_INET; |
| 510 | memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr32[3], |
| 511 | sizeof(sin.sin_addr)); |
| 512 | ia4 = in_selectsrc(&sin, &in6p->in6p_route, |
| 513 | in6p->in6p_socket->so_options, NULL, &error, &_psref); |
| 514 | if (ia4 == NULL) { |
| 515 | if (error == 0) |
| 516 | error = EADDRNOTAVAIL; |
| 517 | return (error); |
| 518 | } |
| 519 | memset(&mapped, 0, sizeof(mapped)); |
| 520 | mapped.s6_addr16[5] = htons(0xffff); |
| 521 | memcpy(&mapped.s6_addr32[3], &IA_SIN(ia4)->sin_addr, |
| 522 | sizeof(IA_SIN(ia4)->sin_addr)); |
| 523 | ia4_release(ia4, &_psref); |
| 524 | in6a = &mapped; |
| 525 | #else |
| 526 | return EADDRNOTAVAIL; |
| 527 | #endif |
| 528 | } else { |
| 529 | /* |
| 530 | * XXX: in6_selectsrc might replace the bound local address |
| 531 | * with the address specified by setsockopt(IPV6_PKTINFO). |
| 532 | * Is it the intended behavior? |
| 533 | */ |
| 534 | error = in6_selectsrc(sin6, in6p->in6p_outputopts, |
| 535 | in6p->in6p_moptions, &in6p->in6p_route, &in6p->in6p_laddr, |
| 536 | &ifp, &psref, &ia6); |
| 537 | if (error == 0) |
| 538 | in6a = &ia6; |
| 539 | if (ifp && scope_ambiguous && |
| 540 | (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) { |
| 541 | if_put(ifp, &psref); |
| 542 | curlwp_bindx(bound); |
| 543 | return error; |
| 544 | } |
| 545 | |
| 546 | if (in6a == NULL) { |
| 547 | if_put(ifp, &psref); |
| 548 | curlwp_bindx(bound); |
| 549 | if (error == 0) |
| 550 | error = EADDRNOTAVAIL; |
| 551 | return error; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if (ifp != NULL) { |
| 556 | in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp); |
| 557 | if_put(ifp, &psref); |
| 558 | } else |
| 559 | in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim_rt(in6p); |
| 560 | curlwp_bindx(bound); |
| 561 | |
| 562 | if (in6_pcblookup_connect(in6p->in6p_table, &sin6->sin6_addr, |
| 563 | sin6->sin6_port, |
| 564 | IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ? in6a : &in6p->in6p_laddr, |
| 565 | in6p->in6p_lport, 0, &vestige) |
| 566 | || vestige.valid) |
| 567 | return (EADDRINUSE); |
| 568 | if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) || |
| 569 | (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) && |
| 570 | in6p->in6p_laddr.s6_addr32[3] == 0)) |
| 571 | { |
| 572 | if (in6p->in6p_lport == 0) { |
| 573 | error = in6_pcbbind(in6p, NULL, l); |
| 574 | if (error != 0) |
| 575 | return error; |
| 576 | } |
| 577 | in6p->in6p_laddr = *in6a; |
| 578 | } |
| 579 | in6p->in6p_faddr = sin6->sin6_addr; |
| 580 | in6p->in6p_fport = sin6->sin6_port; |
| 581 | |
| 582 | /* Late bind, if needed */ |
| 583 | if (in6p->in6p_bindportonsend) { |
| 584 | struct sockaddr_in6 lsin = *((const struct sockaddr_in6 *) |
| 585 | in6p->in6p_socket->so_proto->pr_domain->dom_sa_any); |
| 586 | lsin.sin6_addr = in6p->in6p_laddr; |
| 587 | lsin.sin6_port = 0; |
| 588 | |
| 589 | if ((error = in6_pcbbind_port(in6p, &lsin, l)) != 0) |
| 590 | return error; |
| 591 | } |
| 592 | |
| 593 | in6_pcbstate(in6p, IN6P_CONNECTED); |
| 594 | in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; |
| 595 | if (ip6_auto_flowlabel) |
| 596 | in6p->in6p_flowinfo |= |
| 597 | (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); |
| 598 | #if defined(IPSEC) |
| 599 | if (ipsec_enabled && in6p->in6p_socket->so_type == SOCK_STREAM) |
| 600 | ipsec_pcbconn(in6p->in6p_sp); |
| 601 | #endif |
| 602 | return (0); |
| 603 | } |
| 604 | |
| 605 | void |
| 606 | in6_pcbdisconnect(struct in6pcb *in6p) |
| 607 | { |
| 608 | memset((void *)&in6p->in6p_faddr, 0, sizeof(in6p->in6p_faddr)); |
| 609 | in6p->in6p_fport = 0; |
| 610 | in6_pcbstate(in6p, IN6P_BOUND); |
| 611 | in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; |
| 612 | #if defined(IPSEC) |
| 613 | if (ipsec_enabled) |
| 614 | ipsec_pcbdisconn(in6p->in6p_sp); |
| 615 | #endif |
| 616 | if (in6p->in6p_socket->so_state & SS_NOFDREF) |
| 617 | in6_pcbdetach(in6p); |
| 618 | } |
| 619 | |
| 620 | void |
| 621 | in6_pcbdetach(struct in6pcb *in6p) |
| 622 | { |
| 623 | struct socket *so = in6p->in6p_socket; |
| 624 | int s; |
| 625 | |
| 626 | if (in6p->in6p_af != AF_INET6) |
| 627 | return; |
| 628 | |
| 629 | #if defined(IPSEC) |
| 630 | if (ipsec_enabled) |
| 631 | ipsec6_delete_pcbpolicy(in6p); |
| 632 | #endif |
| 633 | so->so_pcb = NULL; |
| 634 | |
| 635 | s = splnet(); |
| 636 | in6_pcbstate(in6p, IN6P_ATTACHED); |
| 637 | LIST_REMOVE(&in6p->in6p_head, inph_lhash); |
| 638 | TAILQ_REMOVE(&in6p->in6p_table->inpt_queue, &in6p->in6p_head, |
| 639 | inph_queue); |
| 640 | splx(s); |
| 641 | |
| 642 | if (in6p->in6p_options) { |
| 643 | m_freem(in6p->in6p_options); |
| 644 | } |
| 645 | if (in6p->in6p_outputopts != NULL) { |
| 646 | ip6_clearpktopts(in6p->in6p_outputopts, -1); |
| 647 | free(in6p->in6p_outputopts, M_IP6OPT); |
| 648 | } |
| 649 | rtcache_free(&in6p->in6p_route); |
| 650 | ip6_freemoptions(in6p->in6p_moptions); |
| 651 | ip_freemoptions(in6p->in6p_v4moptions); |
| 652 | sofree(so); /* drops the socket's lock */ |
| 653 | |
| 654 | pool_put(&in6pcb_pool, in6p); |
| 655 | mutex_enter(softnet_lock); /* reacquire it */ |
| 656 | } |
| 657 | |
| 658 | void |
| 659 | in6_setsockaddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6) |
| 660 | { |
| 661 | |
| 662 | if (in6p->in6p_af != AF_INET6) |
| 663 | return; |
| 664 | |
| 665 | sockaddr_in6_init(sin6, &in6p->in6p_laddr, in6p->in6p_lport, 0, 0); |
| 666 | (void)sa6_recoverscope(sin6); /* XXX: should catch errors */ |
| 667 | } |
| 668 | |
| 669 | void |
| 670 | in6_setpeeraddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6) |
| 671 | { |
| 672 | |
| 673 | if (in6p->in6p_af != AF_INET6) |
| 674 | return; |
| 675 | |
| 676 | sockaddr_in6_init(sin6, &in6p->in6p_faddr, in6p->in6p_fport, 0, 0); |
| 677 | (void)sa6_recoverscope(sin6); /* XXX: should catch errors */ |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | * Pass some notification to all connections of a protocol |
| 682 | * associated with address dst. The local address and/or port numbers |
| 683 | * may be specified to limit the search. The "usual action" will be |
| 684 | * taken, depending on the ctlinput cmd. The caller must filter any |
| 685 | * cmds that are uninteresting (e.g., no error in the map). |
| 686 | * Call the protocol specific routine (if any) to report |
| 687 | * any errors for each matching socket. |
| 688 | * |
| 689 | * Must be called at splsoftnet. |
| 690 | * |
| 691 | * Note: src (4th arg) carries the flowlabel value on the original IPv6 |
| 692 | * header, in sin6_flowinfo member. |
| 693 | */ |
| 694 | int |
| 695 | in6_pcbnotify(struct inpcbtable *table, const struct sockaddr *dst, |
| 696 | u_int fport_arg, const struct sockaddr *src, u_int lport_arg, int cmd, |
| 697 | void *cmdarg, void (*notify)(struct in6pcb *, int)) |
| 698 | { |
| 699 | struct rtentry *rt; |
| 700 | struct inpcb_hdr *inph, *ninph; |
| 701 | struct sockaddr_in6 sa6_src; |
| 702 | const struct sockaddr_in6 *sa6_dst; |
| 703 | u_int16_t fport = fport_arg, lport = lport_arg; |
| 704 | int errno; |
| 705 | int nmatch = 0; |
| 706 | u_int32_t flowinfo; |
| 707 | |
| 708 | if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6) |
| 709 | return 0; |
| 710 | |
| 711 | sa6_dst = (const struct sockaddr_in6 *)dst; |
| 712 | if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) |
| 713 | return 0; |
| 714 | |
| 715 | /* |
| 716 | * note that src can be NULL when we get notify by local fragmentation. |
| 717 | */ |
| 718 | sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src; |
| 719 | flowinfo = sa6_src.sin6_flowinfo; |
| 720 | |
| 721 | /* |
| 722 | * Redirects go to all references to the destination, |
| 723 | * and use in6_rtchange to invalidate the route cache. |
| 724 | * Dead host indications: also use in6_rtchange to invalidate |
| 725 | * the cache, and deliver the error to all the sockets. |
| 726 | * Otherwise, if we have knowledge of the local port and address, |
| 727 | * deliver only to that socket. |
| 728 | */ |
| 729 | if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { |
| 730 | fport = 0; |
| 731 | lport = 0; |
| 732 | memset((void *)&sa6_src.sin6_addr, 0, sizeof(sa6_src.sin6_addr)); |
| 733 | |
| 734 | if (cmd != PRC_HOSTDEAD) |
| 735 | notify = in6_rtchange; |
| 736 | } |
| 737 | |
| 738 | errno = inet6ctlerrmap[cmd]; |
| 739 | TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) { |
| 740 | struct in6pcb *in6p = (struct in6pcb *)inph; |
| 741 | if (in6p->in6p_af != AF_INET6) |
| 742 | continue; |
| 743 | |
| 744 | /* |
| 745 | * Under the following condition, notify of redirects |
| 746 | * to the pcb, without making address matches against inpcb. |
| 747 | * - redirect notification is arrived. |
| 748 | * - the inpcb is unconnected. |
| 749 | * - the inpcb is caching !RTF_HOST routing entry. |
| 750 | * - the ICMPv6 notification is from the gateway cached in the |
| 751 | * inpcb. i.e. ICMPv6 notification is from nexthop gateway |
| 752 | * the inpcb used very recently. |
| 753 | * |
| 754 | * This is to improve interaction between netbsd/openbsd |
| 755 | * redirect handling code, and inpcb route cache code. |
| 756 | * without the clause, !RTF_HOST routing entry (which carries |
| 757 | * gateway used by inpcb right before the ICMPv6 redirect) |
| 758 | * will be cached forever in unconnected inpcb. |
| 759 | * |
| 760 | * There still is a question regarding to what is TRT: |
| 761 | * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be |
| 762 | * generated on packet output. inpcb will always cache |
| 763 | * RTF_HOST routing entry so there's no need for the clause |
| 764 | * (ICMPv6 redirect will update RTF_HOST routing entry, |
| 765 | * and inpcb is caching it already). |
| 766 | * However, bsdi/freebsd are vulnerable to local DoS attacks |
| 767 | * due to the cloned routing entries. |
| 768 | * - Specwise, "destination cache" is mentioned in RFC2461. |
| 769 | * Jinmei says that it implies bsdi/freebsd behavior, itojun |
| 770 | * is not really convinced. |
| 771 | * - Having hiwat/lowat on # of cloned host route (redirect/ |
| 772 | * pmtud) may be a good idea. netbsd/openbsd has it. see |
| 773 | * icmp6_mtudisc_update(). |
| 774 | */ |
| 775 | if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) && |
| 776 | IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && |
| 777 | (rt = rtcache_validate(&in6p->in6p_route)) != NULL && |
| 778 | !(rt->rt_flags & RTF_HOST)) { |
| 779 | const struct sockaddr_in6 *dst6; |
| 780 | |
| 781 | dst6 = (const struct sockaddr_in6 *) |
| 782 | rtcache_getdst(&in6p->in6p_route); |
| 783 | if (dst6 == NULL) |
| 784 | ; |
| 785 | else if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, |
| 786 | &sa6_dst->sin6_addr)) |
| 787 | goto do_notify; |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | * If the error designates a new path MTU for a destination |
| 792 | * and the application (associated with this socket) wanted to |
| 793 | * know the value, notify. Note that we notify for all |
| 794 | * disconnected sockets if the corresponding application |
| 795 | * wanted. This is because some UDP applications keep sending |
| 796 | * sockets disconnected. |
| 797 | * XXX: should we avoid to notify the value to TCP sockets? |
| 798 | */ |
| 799 | if (cmd == PRC_MSGSIZE && (in6p->in6p_flags & IN6P_MTU) != 0 && |
| 800 | (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) || |
| 801 | IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &sa6_dst->sin6_addr))) { |
| 802 | ip6_notify_pmtu(in6p, (const struct sockaddr_in6 *)dst, |
| 803 | (u_int32_t *)cmdarg); |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * Detect if we should notify the error. If no source and |
| 808 | * destination ports are specified, but non-zero flowinfo and |
| 809 | * local address match, notify the error. This is the case |
| 810 | * when the error is delivered with an encrypted buffer |
| 811 | * by ESP. Otherwise, just compare addresses and ports |
| 812 | * as usual. |
| 813 | */ |
| 814 | if (lport == 0 && fport == 0 && flowinfo && |
| 815 | in6p->in6p_socket != NULL && |
| 816 | flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && |
| 817 | IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr)) |
| 818 | goto do_notify; |
| 819 | else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, |
| 820 | &sa6_dst->sin6_addr) || |
| 821 | in6p->in6p_socket == NULL || |
| 822 | (lport && in6p->in6p_lport != lport) || |
| 823 | (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && |
| 824 | !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, |
| 825 | &sa6_src.sin6_addr)) || |
| 826 | (fport && in6p->in6p_fport != fport)) |
| 827 | continue; |
| 828 | |
| 829 | do_notify: |
| 830 | if (notify) |
| 831 | (*notify)(in6p, errno); |
| 832 | nmatch++; |
| 833 | } |
| 834 | return nmatch; |
| 835 | } |
| 836 | |
| 837 | void |
| 838 | in6_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp) |
| 839 | { |
| 840 | struct inpcb_hdr *inph, *ninph; |
| 841 | struct ip6_moptions *im6o; |
| 842 | struct in6_multi_mship *imm, *nimm; |
| 843 | |
| 844 | KASSERT(ifp != NULL); |
| 845 | |
| 846 | TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) { |
| 847 | struct in6pcb *in6p = (struct in6pcb *)inph; |
| 848 | if (in6p->in6p_af != AF_INET6) |
| 849 | continue; |
| 850 | |
| 851 | im6o = in6p->in6p_moptions; |
| 852 | if (im6o) { |
| 853 | /* |
| 854 | * Unselect the outgoing interface if it is being |
| 855 | * detached. |
| 856 | */ |
| 857 | if (im6o->im6o_multicast_if_index == ifp->if_index) |
| 858 | im6o->im6o_multicast_if_index = 0; |
| 859 | |
| 860 | /* |
| 861 | * Drop multicast group membership if we joined |
| 862 | * through the interface being detached. |
| 863 | * XXX controversial - is it really legal for kernel |
| 864 | * to force this? |
| 865 | */ |
| 866 | for (imm = im6o->im6o_memberships.lh_first; |
| 867 | imm != NULL; imm = nimm) { |
| 868 | nimm = imm->i6mm_chain.le_next; |
| 869 | if (imm->i6mm_maddr->in6m_ifp == ifp) { |
| 870 | LIST_REMOVE(imm, i6mm_chain); |
| 871 | in6_leavegroup(imm); |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | in_purgeifmcast(in6p->in6p_v4moptions, ifp); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | void |
| 880 | in6_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp) |
| 881 | { |
| 882 | struct rtentry *rt; |
| 883 | struct inpcb_hdr *inph, *ninph; |
| 884 | |
| 885 | TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) { |
| 886 | struct in6pcb *in6p = (struct in6pcb *)inph; |
| 887 | if (in6p->in6p_af != AF_INET6) |
| 888 | continue; |
| 889 | if ((rt = rtcache_validate(&in6p->in6p_route)) != NULL && |
| 890 | rt->rt_ifp == ifp) |
| 891 | in6_rtchange(in6p, 0); |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * Check for alternatives when higher level complains |
| 897 | * about service problems. For now, invalidate cached |
| 898 | * routing information. If the route was created dynamically |
| 899 | * (by a redirect), time to try a default gateway again. |
| 900 | */ |
| 901 | void |
| 902 | in6_losing(struct in6pcb *in6p) |
| 903 | { |
| 904 | struct rtentry *rt; |
| 905 | struct rt_addrinfo info; |
| 906 | |
| 907 | if (in6p->in6p_af != AF_INET6) |
| 908 | return; |
| 909 | |
| 910 | if ((rt = rtcache_validate(&in6p->in6p_route)) == NULL) |
| 911 | return; |
| 912 | |
| 913 | memset(&info, 0, sizeof(info)); |
| 914 | info.rti_info[RTAX_DST] = rtcache_getdst(&in6p->in6p_route); |
| 915 | info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; |
| 916 | info.rti_info[RTAX_NETMASK] = rt_mask(rt); |
| 917 | rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); |
| 918 | if (rt->rt_flags & RTF_DYNAMIC) { |
| 919 | (void)rtrequest(RTM_DELETE, rt_getkey(rt), |
| 920 | rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL); |
| 921 | } |
| 922 | /* |
| 923 | * A new route can be allocated |
| 924 | * the next time output is attempted. |
| 925 | */ |
| 926 | rtcache_free(&in6p->in6p_route); |
| 927 | } |
| 928 | |
| 929 | /* |
| 930 | * After a routing change, flush old routing. A new route can be |
| 931 | * allocated the next time output is attempted. |
| 932 | */ |
| 933 | void |
| 934 | in6_rtchange(struct in6pcb *in6p, int errno) |
| 935 | { |
| 936 | if (in6p->in6p_af != AF_INET6) |
| 937 | return; |
| 938 | |
| 939 | rtcache_free(&in6p->in6p_route); |
| 940 | /* |
| 941 | * A new route can be allocated the next time |
| 942 | * output is attempted. |
| 943 | */ |
| 944 | } |
| 945 | |
| 946 | struct in6pcb * |
| 947 | in6_pcblookup_port(struct inpcbtable *table, struct in6_addr *laddr6, |
| 948 | u_int lport_arg, int lookup_wildcard, struct vestigial_inpcb *vp) |
| 949 | { |
| 950 | struct inpcbhead *head; |
| 951 | struct inpcb_hdr *inph; |
| 952 | struct in6pcb *in6p, *match = NULL; |
| 953 | int matchwild = 3, wildcard; |
| 954 | u_int16_t lport = lport_arg; |
| 955 | |
| 956 | if (vp) |
| 957 | vp->valid = 0; |
| 958 | |
| 959 | head = IN6PCBHASH_PORT(table, lport); |
| 960 | LIST_FOREACH(inph, head, inph_lhash) { |
| 961 | in6p = (struct in6pcb *)inph; |
| 962 | if (in6p->in6p_af != AF_INET6) |
| 963 | continue; |
| 964 | |
| 965 | if (in6p->in6p_lport != lport) |
| 966 | continue; |
| 967 | wildcard = 0; |
| 968 | if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) { |
| 969 | if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) |
| 970 | continue; |
| 971 | } |
| 972 | if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) |
| 973 | wildcard++; |
| 974 | if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) { |
| 975 | if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) |
| 976 | continue; |
| 977 | if (!IN6_IS_ADDR_V4MAPPED(laddr6)) |
| 978 | continue; |
| 979 | |
| 980 | /* duplicate of IPv4 logic */ |
| 981 | wildcard = 0; |
| 982 | if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) && |
| 983 | in6p->in6p_faddr.s6_addr32[3]) |
| 984 | wildcard++; |
| 985 | if (!in6p->in6p_laddr.s6_addr32[3]) { |
| 986 | if (laddr6->s6_addr32[3]) |
| 987 | wildcard++; |
| 988 | } else { |
| 989 | if (!laddr6->s6_addr32[3]) |
| 990 | wildcard++; |
| 991 | else { |
| 992 | if (in6p->in6p_laddr.s6_addr32[3] != |
| 993 | laddr6->s6_addr32[3]) |
| 994 | continue; |
| 995 | } |
| 996 | } |
| 997 | } else if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { |
| 998 | if (IN6_IS_ADDR_V4MAPPED(laddr6)) { |
| 999 | if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) |
| 1000 | continue; |
| 1001 | } |
| 1002 | if (!IN6_IS_ADDR_UNSPECIFIED(laddr6)) |
| 1003 | wildcard++; |
| 1004 | } else { |
| 1005 | if (IN6_IS_ADDR_V4MAPPED(laddr6)) { |
| 1006 | if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) |
| 1007 | continue; |
| 1008 | } |
| 1009 | if (IN6_IS_ADDR_UNSPECIFIED(laddr6)) |
| 1010 | wildcard++; |
| 1011 | else { |
| 1012 | if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, |
| 1013 | laddr6)) |
| 1014 | continue; |
| 1015 | } |
| 1016 | } |
| 1017 | if (wildcard && !lookup_wildcard) |
| 1018 | continue; |
| 1019 | if (wildcard < matchwild) { |
| 1020 | match = in6p; |
| 1021 | matchwild = wildcard; |
| 1022 | if (matchwild == 0) |
| 1023 | break; |
| 1024 | } |
| 1025 | } |
| 1026 | if (match && matchwild == 0) |
| 1027 | return match; |
| 1028 | |
| 1029 | if (vp && table->vestige && table->vestige->init_ports6) { |
| 1030 | struct vestigial_inpcb better; |
| 1031 | void *state; |
| 1032 | |
| 1033 | state = (*table->vestige->init_ports6)(laddr6, |
| 1034 | lport_arg, |
| 1035 | lookup_wildcard); |
| 1036 | while (table->vestige |
| 1037 | && (*table->vestige->next_port6)(state, vp)) { |
| 1038 | |
| 1039 | if (vp->lport != lport) |
| 1040 | continue; |
| 1041 | wildcard = 0; |
| 1042 | if (!IN6_IS_ADDR_UNSPECIFIED(&vp->faddr.v6)) |
| 1043 | wildcard++; |
| 1044 | if (IN6_IS_ADDR_UNSPECIFIED(&vp->laddr.v6)) { |
| 1045 | if (!IN6_IS_ADDR_UNSPECIFIED(laddr6)) |
| 1046 | wildcard++; |
| 1047 | } else { |
| 1048 | if (IN6_IS_ADDR_V4MAPPED(laddr6)) { |
| 1049 | if (vp->v6only) |
| 1050 | continue; |
| 1051 | } |
| 1052 | if (IN6_IS_ADDR_UNSPECIFIED(laddr6)) |
| 1053 | wildcard++; |
| 1054 | else { |
| 1055 | if (!IN6_ARE_ADDR_EQUAL(&vp->laddr.v6, laddr6)) |
| 1056 | continue; |
| 1057 | } |
| 1058 | } |
| 1059 | if (wildcard && !lookup_wildcard) |
| 1060 | continue; |
| 1061 | if (wildcard < matchwild) { |
| 1062 | better = *vp; |
| 1063 | match = (void*)&better; |
| 1064 | |
| 1065 | matchwild = wildcard; |
| 1066 | if (matchwild == 0) |
| 1067 | break; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | if (match) { |
| 1072 | if (match != (void*)&better) |
| 1073 | return match; |
| 1074 | else { |
| 1075 | *vp = better; |
| 1076 | return 0; |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | return (match); |
| 1081 | } |
| 1082 | |
| 1083 | /* |
| 1084 | * WARNING: return value (rtentry) could be IPv4 one if in6pcb is connected to |
| 1085 | * IPv4 mapped address. |
| 1086 | */ |
| 1087 | struct rtentry * |
| 1088 | in6_pcbrtentry(struct in6pcb *in6p) |
| 1089 | { |
| 1090 | struct rtentry *rt; |
| 1091 | struct route *ro; |
| 1092 | union { |
| 1093 | const struct sockaddr *sa; |
| 1094 | const struct sockaddr_in6 *sa6; |
| 1095 | #ifdef INET |
| 1096 | const struct sockaddr_in *sa4; |
| 1097 | #endif |
| 1098 | } cdst; |
| 1099 | |
| 1100 | ro = &in6p->in6p_route; |
| 1101 | |
| 1102 | if (in6p->in6p_af != AF_INET6) |
| 1103 | return (NULL); |
| 1104 | |
| 1105 | cdst.sa = rtcache_getdst(ro); |
| 1106 | if (cdst.sa == NULL) |
| 1107 | ; |
| 1108 | #ifdef INET |
| 1109 | else if (cdst.sa->sa_family == AF_INET) { |
| 1110 | KASSERT(IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)); |
| 1111 | if (cdst.sa4->sin_addr.s_addr != in6p->in6p_faddr.s6_addr32[3]) |
| 1112 | rtcache_free(ro); |
| 1113 | } |
| 1114 | #endif |
| 1115 | else { |
| 1116 | if (!IN6_ARE_ADDR_EQUAL(&cdst.sa6->sin6_addr, |
| 1117 | &in6p->in6p_faddr)) |
| 1118 | rtcache_free(ro); |
| 1119 | } |
| 1120 | if ((rt = rtcache_validate(ro)) == NULL) |
| 1121 | rt = rtcache_update(ro, 1); |
| 1122 | #ifdef INET |
| 1123 | if (rt == NULL && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) { |
| 1124 | union { |
| 1125 | struct sockaddr dst; |
| 1126 | struct sockaddr_in dst4; |
| 1127 | } u; |
| 1128 | struct in_addr addr; |
| 1129 | |
| 1130 | addr.s_addr = in6p->in6p_faddr.s6_addr32[3]; |
| 1131 | |
| 1132 | sockaddr_in_init(&u.dst4, &addr, 0); |
| 1133 | if (rtcache_setdst(ro, &u.dst) != 0) |
| 1134 | return NULL; |
| 1135 | |
| 1136 | rt = rtcache_init(ro); |
| 1137 | } else |
| 1138 | #endif |
| 1139 | if (rt == NULL && !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { |
| 1140 | union { |
| 1141 | struct sockaddr dst; |
| 1142 | struct sockaddr_in6 dst6; |
| 1143 | } u; |
| 1144 | |
| 1145 | sockaddr_in6_init(&u.dst6, &in6p->in6p_faddr, 0, 0, 0); |
| 1146 | if (rtcache_setdst(ro, &u.dst) != 0) |
| 1147 | return NULL; |
| 1148 | |
| 1149 | rt = rtcache_init(ro); |
| 1150 | } |
| 1151 | return rt; |
| 1152 | } |
| 1153 | |
| 1154 | struct in6pcb * |
| 1155 | in6_pcblookup_connect(struct inpcbtable *table, const struct in6_addr *faddr6, |
| 1156 | u_int fport_arg, const struct in6_addr *laddr6, u_int lport_arg, |
| 1157 | int faith, |
| 1158 | struct vestigial_inpcb *vp) |
| 1159 | { |
| 1160 | struct inpcbhead *head; |
| 1161 | struct inpcb_hdr *inph; |
| 1162 | struct in6pcb *in6p; |
| 1163 | u_int16_t fport = fport_arg, lport = lport_arg; |
| 1164 | |
| 1165 | if (vp) |
| 1166 | vp->valid = 0; |
| 1167 | |
| 1168 | head = IN6PCBHASH_CONNECT(table, faddr6, fport, laddr6, lport); |
| 1169 | LIST_FOREACH(inph, head, inph_hash) { |
| 1170 | in6p = (struct in6pcb *)inph; |
| 1171 | if (in6p->in6p_af != AF_INET6) |
| 1172 | continue; |
| 1173 | |
| 1174 | /* find exact match on both source and dest */ |
| 1175 | if (in6p->in6p_fport != fport) |
| 1176 | continue; |
| 1177 | if (in6p->in6p_lport != lport) |
| 1178 | continue; |
| 1179 | if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) |
| 1180 | continue; |
| 1181 | if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)) |
| 1182 | continue; |
| 1183 | if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) |
| 1184 | continue; |
| 1185 | if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) |
| 1186 | continue; |
| 1187 | if ((IN6_IS_ADDR_V4MAPPED(laddr6) || |
| 1188 | IN6_IS_ADDR_V4MAPPED(faddr6)) && |
| 1189 | (in6p->in6p_flags & IN6P_IPV6_V6ONLY)) |
| 1190 | continue; |
| 1191 | return in6p; |
| 1192 | } |
| 1193 | if (vp && table->vestige) { |
| 1194 | if ((*table->vestige->lookup6)(faddr6, fport_arg, |
| 1195 | laddr6, lport_arg, vp)) |
| 1196 | return NULL; |
| 1197 | } |
| 1198 | |
| 1199 | return NULL; |
| 1200 | } |
| 1201 | |
| 1202 | struct in6pcb * |
| 1203 | in6_pcblookup_bind(struct inpcbtable *table, const struct in6_addr *laddr6, |
| 1204 | u_int lport_arg, int faith) |
| 1205 | { |
| 1206 | struct inpcbhead *head; |
| 1207 | struct inpcb_hdr *inph; |
| 1208 | struct in6pcb *in6p; |
| 1209 | u_int16_t lport = lport_arg; |
| 1210 | #ifdef INET |
| 1211 | struct in6_addr zero_mapped; |
| 1212 | #endif |
| 1213 | |
| 1214 | head = IN6PCBHASH_BIND(table, laddr6, lport); |
| 1215 | LIST_FOREACH(inph, head, inph_hash) { |
| 1216 | in6p = (struct in6pcb *)inph; |
| 1217 | if (in6p->in6p_af != AF_INET6) |
| 1218 | continue; |
| 1219 | |
| 1220 | if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) |
| 1221 | continue; |
| 1222 | if (in6p->in6p_fport != 0) |
| 1223 | continue; |
| 1224 | if (in6p->in6p_lport != lport) |
| 1225 | continue; |
| 1226 | if (IN6_IS_ADDR_V4MAPPED(laddr6) && |
| 1227 | (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) |
| 1228 | continue; |
| 1229 | if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) |
| 1230 | goto out; |
| 1231 | } |
| 1232 | #ifdef INET |
| 1233 | if (IN6_IS_ADDR_V4MAPPED(laddr6)) { |
| 1234 | memset(&zero_mapped, 0, sizeof(zero_mapped)); |
| 1235 | zero_mapped.s6_addr16[5] = 0xffff; |
| 1236 | head = IN6PCBHASH_BIND(table, &zero_mapped, lport); |
| 1237 | LIST_FOREACH(inph, head, inph_hash) { |
| 1238 | in6p = (struct in6pcb *)inph; |
| 1239 | if (in6p->in6p_af != AF_INET6) |
| 1240 | continue; |
| 1241 | |
| 1242 | if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) |
| 1243 | continue; |
| 1244 | if (in6p->in6p_fport != 0) |
| 1245 | continue; |
| 1246 | if (in6p->in6p_lport != lport) |
| 1247 | continue; |
| 1248 | if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) |
| 1249 | continue; |
| 1250 | if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zero_mapped)) |
| 1251 | goto out; |
| 1252 | } |
| 1253 | } |
| 1254 | #endif |
| 1255 | head = IN6PCBHASH_BIND(table, &zeroin6_addr, lport); |
| 1256 | LIST_FOREACH(inph, head, inph_hash) { |
| 1257 | in6p = (struct in6pcb *)inph; |
| 1258 | if (in6p->in6p_af != AF_INET6) |
| 1259 | continue; |
| 1260 | |
| 1261 | if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0) |
| 1262 | continue; |
| 1263 | if (in6p->in6p_fport != 0) |
| 1264 | continue; |
| 1265 | if (in6p->in6p_lport != lport) |
| 1266 | continue; |
| 1267 | if (IN6_IS_ADDR_V4MAPPED(laddr6) && |
| 1268 | (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) |
| 1269 | continue; |
| 1270 | if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zeroin6_addr)) |
| 1271 | goto out; |
| 1272 | } |
| 1273 | return (NULL); |
| 1274 | |
| 1275 | out: |
| 1276 | inph = &in6p->in6p_head; |
| 1277 | if (inph != LIST_FIRST(head)) { |
| 1278 | LIST_REMOVE(inph, inph_hash); |
| 1279 | LIST_INSERT_HEAD(head, inph, inph_hash); |
| 1280 | } |
| 1281 | return in6p; |
| 1282 | } |
| 1283 | |
| 1284 | void |
| 1285 | in6_pcbstate(struct in6pcb *in6p, int state) |
| 1286 | { |
| 1287 | |
| 1288 | if (in6p->in6p_af != AF_INET6) |
| 1289 | return; |
| 1290 | |
| 1291 | if (in6p->in6p_state > IN6P_ATTACHED) |
| 1292 | LIST_REMOVE(&in6p->in6p_head, inph_hash); |
| 1293 | |
| 1294 | switch (state) { |
| 1295 | case IN6P_BOUND: |
| 1296 | LIST_INSERT_HEAD(IN6PCBHASH_BIND(in6p->in6p_table, |
| 1297 | &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head, |
| 1298 | inph_hash); |
| 1299 | break; |
| 1300 | case IN6P_CONNECTED: |
| 1301 | LIST_INSERT_HEAD(IN6PCBHASH_CONNECT(in6p->in6p_table, |
| 1302 | &in6p->in6p_faddr, in6p->in6p_fport, |
| 1303 | &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head, |
| 1304 | inph_hash); |
| 1305 | break; |
| 1306 | } |
| 1307 | |
| 1308 | in6p->in6p_state = state; |
| 1309 | } |
| 1310 | |