1 /* $NetBSD: in_pcb.c,v 1.156 2015/04/03 20:01:07 rtr Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1998, 2011 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Coyote Point Systems, Inc. 38 * This code is derived from software contributed to The NetBSD Foundation 39 * by Public Access Networks Corporation ("Panix"). It was developed under 40 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 52 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 53 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 54 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 55 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 56 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 57 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 58 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 59 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 61 * POSSIBILITY OF SUCH DAMAGE. 62 */ 63 64 /* 65 * Copyright (c) 1982, 1986, 1991, 1993, 1995 66 * The Regents of the University of California. All rights reserved. 67 * 68 * Redistribution and use in source and binary forms, with or without 69 * modification, are permitted provided that the following conditions 70 * are met: 71 * 1. Redistributions of source code must retain the above copyright 72 * notice, this list of conditions and the following disclaimer. 73 * 2. Redistributions in binary form must reproduce the above copyright 74 * notice, this list of conditions and the following disclaimer in the 75 * documentation and/or other materials provided with the distribution. 76 * 3. Neither the name of the University nor the names of its contributors 77 * may be used to endorse or promote products derived from this software 78 * without specific prior written permission. 79 * 80 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 81 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 82 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 83 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 84 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 85 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 86 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 87 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 88 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 89 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 90 * SUCH DAMAGE. 91 * 92 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95 93 */ 94 95 #include <sys/cdefs.h> 96 __KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.156 2015/04/03 20:01:07 rtr Exp $"); 97 98 #include "opt_inet.h" 99 #include "opt_ipsec.h" 100 101 #include <sys/param.h> 102 #include <sys/systm.h> 103 #include <sys/mbuf.h> 104 #include <sys/protosw.h> 105 #include <sys/socket.h> 106 #include <sys/socketvar.h> 107 #include <sys/ioctl.h> 108 #include <sys/errno.h> 109 #include <sys/time.h> 110 #include <sys/once.h> 111 #include <sys/pool.h> 112 #include <sys/proc.h> 113 #include <sys/kauth.h> 114 #include <sys/uidinfo.h> 115 #include <sys/domain.h> 116 117 #include <net/if.h> 118 #include <net/route.h> 119 120 #include <netinet/in.h> 121 #include <netinet/in_systm.h> 122 #include <netinet/ip.h> 123 #include <netinet/in_pcb.h> 124 #include <netinet/in_var.h> 125 #include <netinet/ip_var.h> 126 #include <netinet/portalgo.h> 127 128 #ifdef INET6 129 #include <netinet/ip6.h> 130 #include <netinet6/ip6_var.h> 131 #include <netinet6/in6_pcb.h> 132 #endif 133 134 #ifdef IPSEC 135 #include <netipsec/ipsec.h> 136 #include <netipsec/key.h> 137 #endif /* IPSEC */ 138 139 #include <netinet/tcp_vtw.h> 140 141 struct in_addr zeroin_addr; 142 143 #define INPCBHASH_PORT(table, lport) \ 144 &(table)->inpt_porthashtbl[ntohs(lport) & (table)->inpt_porthash] 145 #define INPCBHASH_BIND(table, laddr, lport) \ 146 &(table)->inpt_bindhashtbl[ \ 147 ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash] 148 #define INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \ 149 &(table)->inpt_connecthashtbl[ \ 150 ((ntohl((faddr).s_addr) + ntohs(fport)) + \ 151 (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash] 152 153 int anonportmin = IPPORT_ANONMIN; 154 int anonportmax = IPPORT_ANONMAX; 155 int lowportmin = IPPORT_RESERVEDMIN; 156 int lowportmax = IPPORT_RESERVEDMAX; 157 158 static struct pool inpcb_pool; 159 160 static int 161 inpcb_poolinit(void) 162 { 163 164 pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 0, 0, "inpcbpl", NULL, 165 IPL_NET); 166 return 0; 167 } 168 169 void 170 in_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize) 171 { 172 static ONCE_DECL(control); 173 174 TAILQ_INIT(&table->inpt_queue); 175 table->inpt_porthashtbl = hashinit(bindhashsize, HASH_LIST, true, 176 &table->inpt_porthash); 177 table->inpt_bindhashtbl = hashinit(bindhashsize, HASH_LIST, true, 178 &table->inpt_bindhash); 179 table->inpt_connecthashtbl = hashinit(connecthashsize, HASH_LIST, true, 180 &table->inpt_connecthash); 181 table->inpt_lastlow = IPPORT_RESERVEDMAX; 182 table->inpt_lastport = (u_int16_t)anonportmax; 183 184 RUN_ONCE(&control, inpcb_poolinit); 185 } 186 187 int 188 in_pcballoc(struct socket *so, void *v) 189 { 190 struct inpcbtable *table = v; 191 struct inpcb *inp; 192 int s; 193 194 s = splnet(); 195 inp = pool_get(&inpcb_pool, PR_NOWAIT); 196 splx(s); 197 if (inp == NULL) 198 return (ENOBUFS); 199 memset(inp, 0, sizeof(*inp)); 200 inp->inp_af = AF_INET; 201 inp->inp_table = table; 202 inp->inp_socket = so; 203 inp->inp_errormtu = -1; 204 inp->inp_portalgo = PORTALGO_DEFAULT; 205 inp->inp_bindportonsend = false; 206 #if defined(IPSEC) 207 if (ipsec_enabled) { 208 int error = ipsec_init_pcbpolicy(so, &inp->inp_sp); 209 if (error != 0) { 210 s = splnet(); 211 pool_put(&inpcb_pool, inp); 212 splx(s); 213 return error; 214 } 215 } 216 #endif 217 so->so_pcb = inp; 218 s = splnet(); 219 TAILQ_INSERT_HEAD(&table->inpt_queue, &inp->inp_head, inph_queue); 220 LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head, 221 inph_lhash); 222 in_pcbstate(inp, INP_ATTACHED); 223 splx(s); 224 return (0); 225 } 226 227 static int 228 in_pcbsetport(struct sockaddr_in *sin, struct inpcb *inp, kauth_cred_t cred) 229 { 230 struct inpcbtable *table = inp->inp_table; 231 struct socket *so = inp->inp_socket; 232 u_int16_t *lastport; 233 u_int16_t lport = 0; 234 enum kauth_network_req req; 235 int error; 236 237 if (inp->inp_flags & INP_LOWPORT) { 238 #ifndef IPNOPRIVPORTS 239 req = KAUTH_REQ_NETWORK_BIND_PRIVPORT; 240 #else 241 req = KAUTH_REQ_NETWORK_BIND_PORT; 242 #endif 243 244 lastport = &table->inpt_lastlow; 245 } else { 246 req = KAUTH_REQ_NETWORK_BIND_PORT; 247 248 lastport = &table->inpt_lastport; 249 } 250 251 /* XXX-kauth: KAUTH_REQ_NETWORK_BIND_AUTOASSIGN_{,PRIV}PORT */ 252 error = kauth_authorize_network(cred, KAUTH_NETWORK_BIND, req, so, sin, 253 NULL); 254 if (error) 255 return (EACCES); 256 257 /* 258 * Use RFC6056 randomized port selection 259 */ 260 error = portalgo_randport(&lport, &inp->inp_head, cred); 261 if (error) 262 return error; 263 264 inp->inp_flags |= INP_ANONPORT; 265 *lastport = lport; 266 lport = htons(lport); 267 inp->inp_lport = lport; 268 in_pcbstate(inp, INP_BOUND); 269 270 return (0); 271 } 272 273 static int 274 in_pcbbind_addr(struct inpcb *inp, struct sockaddr_in *sin, kauth_cred_t cred) 275 { 276 if (sin->sin_family != AF_INET) 277 return (EAFNOSUPPORT); 278 279 if (IN_MULTICAST(sin->sin_addr.s_addr)) { 280 /* Always succeed; port reuse handled in in_pcbbind_port(). */ 281 } else if (!in_nullhost(sin->sin_addr)) { 282 struct in_ifaddr *ia = NULL; 283 284 INADDR_TO_IA(sin->sin_addr, ia); 285 /* check for broadcast addresses */ 286 if (ia == NULL) 287 ia = ifatoia(ifa_ifwithaddr(sintosa(sin))); 288 if (ia == NULL) 289 return (EADDRNOTAVAIL); 290 } 291 292 inp->inp_laddr = sin->sin_addr; 293 294 return (0); 295 } 296 297 static int 298 in_pcbbind_port(struct inpcb *inp, struct sockaddr_in *sin, kauth_cred_t cred) 299 { 300 struct inpcbtable *table = inp->inp_table; 301 struct socket *so = inp->inp_socket; 302 int reuseport = (so->so_options & SO_REUSEPORT); 303 int wild = 0, error; 304 305 if (IN_MULTICAST(sin->sin_addr.s_addr)) { 306 /* 307 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 308 * allow complete duplication of binding if 309 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 310 * and a multicast address is bound on both 311 * new and duplicated sockets. 312 */ 313 if (so->so_options & (SO_REUSEADDR | SO_REUSEPORT)) 314 reuseport = SO_REUSEADDR|SO_REUSEPORT; 315 } 316 317 if (sin->sin_port == 0) { 318 error = in_pcbsetport(sin, inp, cred); 319 if (error) 320 return (error); 321 } else { 322 struct inpcb *t; 323 vestigial_inpcb_t vestige; 324 #ifdef INET6 325 struct in6pcb *t6; 326 struct in6_addr mapped; 327 #endif 328 enum kauth_network_req req; 329 330 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 331 wild = 1; 332 333 #ifndef IPNOPRIVPORTS 334 if (ntohs(sin->sin_port) < IPPORT_RESERVED) 335 req = KAUTH_REQ_NETWORK_BIND_PRIVPORT; 336 else 337 #endif /* !IPNOPRIVPORTS */ 338 req = KAUTH_REQ_NETWORK_BIND_PORT; 339 340 error = kauth_authorize_network(cred, KAUTH_NETWORK_BIND, req, 341 so, sin, NULL); 342 if (error) 343 return (EACCES); 344 345 #ifdef INET6 346 memset(&mapped, 0, sizeof(mapped)); 347 mapped.s6_addr16[5] = 0xffff; 348 memcpy(&mapped.s6_addr32[3], &sin->sin_addr, 349 sizeof(mapped.s6_addr32[3])); 350 t6 = in6_pcblookup_port(table, &mapped, sin->sin_port, wild, &vestige); 351 if (t6 && (reuseport & t6->in6p_socket->so_options) == 0) 352 return (EADDRINUSE); 353 if (!t6 && vestige.valid) { 354 if (!!reuseport != !!vestige.reuse_port) { 355 return EADDRINUSE; 356 } 357 } 358 #endif 359 360 /* XXX-kauth */ 361 if (so->so_uidinfo->ui_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) { 362 t = in_pcblookup_port(table, sin->sin_addr, sin->sin_port, 1, &vestige); 363 /* 364 * XXX: investigate ramifications of loosening this 365 * restriction so that as long as both ports have 366 * SO_REUSEPORT allow the bind 367 */ 368 if (t && 369 (!in_nullhost(sin->sin_addr) || 370 !in_nullhost(t->inp_laddr) || 371 (t->inp_socket->so_options & SO_REUSEPORT) == 0) 372 && (so->so_uidinfo->ui_uid != t->inp_socket->so_uidinfo->ui_uid)) { 373 return (EADDRINUSE); 374 } 375 if (!t && vestige.valid) { 376 if ((!in_nullhost(sin->sin_addr) 377 || !in_nullhost(vestige.laddr.v4) 378 || !vestige.reuse_port) 379 && so->so_uidinfo->ui_uid != vestige.uid) { 380 return EADDRINUSE; 381 } 382 } 383 } 384 t = in_pcblookup_port(table, sin->sin_addr, sin->sin_port, wild, &vestige); 385 if (t && (reuseport & t->inp_socket->so_options) == 0) 386 return (EADDRINUSE); 387 if (!t 388 && vestige.valid 389 && !(reuseport && vestige.reuse_port)) 390 return EADDRINUSE; 391 392 inp->inp_lport = sin->sin_port; 393 in_pcbstate(inp, INP_BOUND); 394 } 395 396 LIST_REMOVE(&inp->inp_head, inph_lhash); 397 LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head, 398 inph_lhash); 399 400 return (0); 401 } 402 403 int 404 in_pcbbind(void *v, struct sockaddr_in *sin, struct lwp *l) 405 { 406 struct inpcb *inp = v; 407 struct sockaddr_in lsin; 408 int error; 409 410 if (inp->inp_af != AF_INET) 411 return (EINVAL); 412 413 if (TAILQ_FIRST(&in_ifaddrhead) == 0) 414 return (EADDRNOTAVAIL); 415 if (inp->inp_lport || !in_nullhost(inp->inp_laddr)) 416 return (EINVAL); 417 418 if (NULL != sin) { 419 if (sin->sin_len != sizeof(*sin)) 420 return (EINVAL); 421 } else { 422 lsin = *((const struct sockaddr_in *) 423 inp->inp_socket->so_proto->pr_domain->dom_sa_any); 424 sin = &lsin; 425 } 426 427 /* Bind address. */ 428 error = in_pcbbind_addr(inp, sin, l->l_cred); 429 if (error) 430 return (error); 431 432 /* Bind port. */ 433 error = in_pcbbind_port(inp, sin, l->l_cred); 434 if (error) { 435 inp->inp_laddr.s_addr = INADDR_ANY; 436 437 return (error); 438 } 439 440 return (0); 441 } 442 443 /* 444 * Connect from a socket to a specified address. 445 * Both address and port must be specified in argument sin. 446 * If don't have a local address for this socket yet, 447 * then pick one. 448 */ 449 int 450 in_pcbconnect(void *v, struct mbuf *nam, struct lwp *l) 451 { 452 struct inpcb *inp = v; 453 struct in_ifaddr *ia = NULL; 454 struct sockaddr_in *ifaddr = NULL; 455 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 456 vestigial_inpcb_t vestige; 457 int error; 458 459 if (inp->inp_af != AF_INET) 460 return (EINVAL); 461 462 if (nam->m_len != sizeof (*sin)) 463 return (EINVAL); 464 if (sin->sin_family != AF_INET) 465 return (EAFNOSUPPORT); 466 if (sin->sin_port == 0) 467 return (EADDRNOTAVAIL); 468 469 if (IN_MULTICAST(sin->sin_addr.s_addr) && 470 inp->inp_socket->so_type == SOCK_STREAM) 471 return EADDRNOTAVAIL; 472 473 if (TAILQ_FIRST(&in_ifaddrhead) != 0) { 474 /* 475 * If the destination address is INADDR_ANY, 476 * use any local address (likely loopback). 477 * If the supplied address is INADDR_BROADCAST, 478 * use the broadcast address of an interface 479 * which supports broadcast. (loopback does not) 480 */ 481 482 if (in_nullhost(sin->sin_addr)) { 483 sin->sin_addr = 484 TAILQ_FIRST(&in_ifaddrhead)->ia_addr.sin_addr; 485 } else if (sin->sin_addr.s_addr == INADDR_BROADCAST) { 486 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 487 if (ia->ia_ifp->if_flags & IFF_BROADCAST) { 488 sin->sin_addr = 489 ia->ia_broadaddr.sin_addr; 490 break; 491 } 492 } 493 } 494 } 495 /* 496 * If we haven't bound which network number to use as ours, 497 * we will use the number of the outgoing interface. 498 * This depends on having done a routing lookup, which 499 * we will probably have to do anyway, so we might 500 * as well do it now. On the other hand if we are 501 * sending to multiple destinations we may have already 502 * done the lookup, so see if we can use the route 503 * from before. In any case, we only 504 * chose a port number once, even if sending to multiple 505 * destinations. 506 */ 507 if (in_nullhost(inp->inp_laddr)) { 508 int xerror; 509 ifaddr = in_selectsrc(sin, &inp->inp_route, 510 inp->inp_socket->so_options, inp->inp_moptions, &xerror); 511 if (ifaddr == NULL) { 512 if (xerror == 0) 513 xerror = EADDRNOTAVAIL; 514 return xerror; 515 } 516 INADDR_TO_IA(ifaddr->sin_addr, ia); 517 if (ia == NULL) 518 return (EADDRNOTAVAIL); 519 } 520 if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port, 521 !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr, 522 inp->inp_lport, &vestige) != 0 523 || vestige.valid) 524 return (EADDRINUSE); 525 if (in_nullhost(inp->inp_laddr)) { 526 if (inp->inp_lport == 0) { 527 error = in_pcbbind(inp, NULL, l); 528 /* 529 * This used to ignore the return value 530 * completely, but we need to check for 531 * ephemeral port shortage. 532 * And attempts to request low ports if not root. 533 */ 534 if (error != 0) 535 return (error); 536 } 537 inp->inp_laddr = ifaddr->sin_addr; 538 } 539 inp->inp_faddr = sin->sin_addr; 540 inp->inp_fport = sin->sin_port; 541 542 /* Late bind, if needed */ 543 if (inp->inp_bindportonsend) { 544 struct sockaddr_in lsin = *((const struct sockaddr_in *) 545 inp->inp_socket->so_proto->pr_domain->dom_sa_any); 546 lsin.sin_addr = inp->inp_laddr; 547 lsin.sin_port = 0; 548 549 if ((error = in_pcbbind_port(inp, &lsin, l->l_cred)) != 0) 550 return error; 551 } 552 553 in_pcbstate(inp, INP_CONNECTED); 554 #if defined(IPSEC) 555 if (ipsec_enabled && inp->inp_socket->so_type == SOCK_STREAM) 556 ipsec_pcbconn(inp->inp_sp); 557 #endif 558 return (0); 559 } 560 561 void 562 in_pcbdisconnect(void *v) 563 { 564 struct inpcb *inp = v; 565 566 if (inp->inp_af != AF_INET) 567 return; 568 569 inp->inp_faddr = zeroin_addr; 570 inp->inp_fport = 0; 571 in_pcbstate(inp, INP_BOUND); 572 #if defined(IPSEC) 573 if (ipsec_enabled) 574 ipsec_pcbdisconn(inp->inp_sp); 575 #endif 576 if (inp->inp_socket->so_state & SS_NOFDREF) 577 in_pcbdetach(inp); 578 } 579 580 void 581 in_pcbdetach(void *v) 582 { 583 struct inpcb *inp = v; 584 struct socket *so = inp->inp_socket; 585 int s; 586 587 if (inp->inp_af != AF_INET) 588 return; 589 590 #if defined(IPSEC) 591 if (ipsec_enabled) 592 ipsec4_delete_pcbpolicy(inp); 593 #endif 594 so->so_pcb = NULL; 595 596 s = splnet(); 597 in_pcbstate(inp, INP_ATTACHED); 598 LIST_REMOVE(&inp->inp_head, inph_lhash); 599 TAILQ_REMOVE(&inp->inp_table->inpt_queue, &inp->inp_head, inph_queue); 600 splx(s); 601 602 if (inp->inp_options) { 603 m_free(inp->inp_options); 604 } 605 rtcache_free(&inp->inp_route); 606 ip_freemoptions(inp->inp_moptions); 607 sofree(so); /* drops the socket's lock */ 608 609 pool_put(&inpcb_pool, inp); 610 mutex_enter(softnet_lock); /* reacquire the softnet_lock */ 611 } 612 613 void 614 in_setsockaddr(struct inpcb *inp, struct mbuf *nam) 615 { 616 struct sockaddr_in *sin; 617 618 if (inp->inp_af != AF_INET) 619 return; 620 621 sin = mtod(nam, struct sockaddr_in *); 622 sockaddr_in_init(sin, &inp->inp_laddr, inp->inp_lport); 623 nam->m_len = sin->sin_len; 624 } 625 626 void 627 in_setpeeraddr(struct inpcb *inp, struct mbuf *nam) 628 { 629 struct sockaddr_in *sin; 630 631 if (inp->inp_af != AF_INET) 632 return; 633 634 sin = mtod(nam, struct sockaddr_in *); 635 sockaddr_in_init(sin, &inp->inp_faddr, inp->inp_fport); 636 nam->m_len = sin->sin_len; 637 } 638 639 /* 640 * Pass some notification to all connections of a protocol 641 * associated with address dst. The local address and/or port numbers 642 * may be specified to limit the search. The "usual action" will be 643 * taken, depending on the ctlinput cmd. The caller must filter any 644 * cmds that are uninteresting (e.g., no error in the map). 645 * Call the protocol specific routine (if any) to report 646 * any errors for each matching socket. 647 * 648 * Must be called at splsoftnet. 649 */ 650 int 651 in_pcbnotify(struct inpcbtable *table, struct in_addr faddr, u_int fport_arg, 652 struct in_addr laddr, u_int lport_arg, int errno, 653 void (*notify)(struct inpcb *, int)) 654 { 655 struct inpcbhead *head; 656 struct inpcb *inp, *ninp; 657 u_int16_t fport = fport_arg, lport = lport_arg; 658 int nmatch; 659 660 if (in_nullhost(faddr) || notify == 0) 661 return (0); 662 663 nmatch = 0; 664 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport); 665 for (inp = (struct inpcb *)LIST_FIRST(head); inp != NULL; inp = ninp) { 666 ninp = (struct inpcb *)LIST_NEXT(inp, inp_hash); 667 if (inp->inp_af != AF_INET) 668 continue; 669 if (in_hosteq(inp->inp_faddr, faddr) && 670 inp->inp_fport == fport && 671 inp->inp_lport == lport && 672 in_hosteq(inp->inp_laddr, laddr)) { 673 (*notify)(inp, errno); 674 nmatch++; 675 } 676 } 677 return (nmatch); 678 } 679 680 void 681 in_pcbnotifyall(struct inpcbtable *table, struct in_addr faddr, int errno, 682 void (*notify)(struct inpcb *, int)) 683 { 684 struct inpcb_hdr *inph, *ninph; 685 686 if (in_nullhost(faddr) || notify == 0) 687 return; 688 689 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) { 690 struct inpcb *inp = (struct inpcb *)inph; 691 if (inp->inp_af != AF_INET) 692 continue; 693 if (in_hosteq(inp->inp_faddr, faddr)) 694 (*notify)(inp, errno); 695 } 696 } 697 698 void 699 in_purgeifmcast(struct ip_moptions *imo, struct ifnet *ifp) 700 { 701 int i, gap; 702 703 if (imo == NULL) 704 return; 705 706 /* 707 * Unselect the outgoing interface if it is being 708 * detached. 709 */ 710 if (imo->imo_multicast_ifp == ifp) 711 imo->imo_multicast_ifp = NULL; 712 713 /* 714 * Drop multicast group membership if we joined 715 * through the interface being detached. 716 */ 717 for (i = 0, gap = 0; i < imo->imo_num_memberships; i++) { 718 if (imo->imo_membership[i]->inm_ifp == ifp) { 719 in_delmulti(imo->imo_membership[i]); 720 gap++; 721 } else if (gap != 0) 722 imo->imo_membership[i - gap] = imo->imo_membership[i]; 723 } 724 imo->imo_num_memberships -= gap; 725 } 726 727 void 728 in_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp) 729 { 730 struct inpcb_hdr *inph, *ninph; 731 732 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) { 733 struct inpcb *inp = (struct inpcb *)inph; 734 if (inp->inp_af != AF_INET) 735 continue; 736 in_purgeifmcast(inp->inp_moptions, ifp); 737 } 738 } 739 740 void 741 in_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp) 742 { 743 struct rtentry *rt; 744 struct inpcb_hdr *inph, *ninph; 745 746 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) { 747 struct inpcb *inp = (struct inpcb *)inph; 748 if (inp->inp_af != AF_INET) 749 continue; 750 if ((rt = rtcache_validate(&inp->inp_route)) != NULL && 751 rt->rt_ifp == ifp) 752 in_rtchange(inp, 0); 753 } 754 } 755 756 /* 757 * Check for alternatives when higher level complains 758 * about service problems. For now, invalidate cached 759 * routing information. If the route was created dynamically 760 * (by a redirect), time to try a default gateway again. 761 */ 762 void 763 in_losing(struct inpcb *inp) 764 { 765 struct rtentry *rt; 766 struct rt_addrinfo info; 767 768 if (inp->inp_af != AF_INET) 769 return; 770 771 if ((rt = rtcache_validate(&inp->inp_route)) == NULL) 772 return; 773 774 memset(&info, 0, sizeof(info)); 775 info.rti_info[RTAX_DST] = rtcache_getdst(&inp->inp_route); 776 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 777 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 778 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 779 if (rt->rt_flags & RTF_DYNAMIC) 780 (void) rtrequest(RTM_DELETE, rt_getkey(rt), 781 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 782 NULL); 783 /* 784 * A new route can be allocated 785 * the next time output is attempted. 786 */ 787 rtcache_free(&inp->inp_route); 788 } 789 790 /* 791 * After a routing change, flush old routing. A new route can be 792 * allocated the next time output is attempted. 793 */ 794 void 795 in_rtchange(struct inpcb *inp, int errno) 796 { 797 798 if (inp->inp_af != AF_INET) 799 return; 800 801 rtcache_free(&inp->inp_route); 802 803 /* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */ 804 } 805 806 struct inpcb * 807 in_pcblookup_port(struct inpcbtable *table, struct in_addr laddr, 808 u_int lport_arg, int lookup_wildcard, vestigial_inpcb_t *vp) 809 { 810 struct inpcbhead *head; 811 struct inpcb_hdr *inph; 812 struct inpcb *match = NULL; 813 int matchwild = 3; 814 int wildcard; 815 u_int16_t lport = lport_arg; 816 817 if (vp) 818 vp->valid = 0; 819 820 head = INPCBHASH_PORT(table, lport); 821 LIST_FOREACH(inph, head, inph_lhash) { 822 struct inpcb * const inp = (struct inpcb *)inph; 823 824 if (inp->inp_af != AF_INET) 825 continue; 826 if (inp->inp_lport != lport) 827 continue; 828 /* 829 * check if inp's faddr and laddr match with ours. 830 * our faddr is considered null. 831 * count the number of wildcard matches. (0 - 2) 832 * 833 * null null match 834 * A null wildcard match 835 * null B wildcard match 836 * A B non match 837 * A A match 838 */ 839 wildcard = 0; 840 if (!in_nullhost(inp->inp_faddr)) 841 wildcard++; 842 if (in_nullhost(inp->inp_laddr)) { 843 if (!in_nullhost(laddr)) 844 wildcard++; 845 } else { 846 if (in_nullhost(laddr)) 847 wildcard++; 848 else { 849 if (!in_hosteq(inp->inp_laddr, laddr)) 850 continue; 851 } 852 } 853 if (wildcard && !lookup_wildcard) 854 continue; 855 /* 856 * prefer an address with less wildcards. 857 */ 858 if (wildcard < matchwild) { 859 match = inp; 860 matchwild = wildcard; 861 if (matchwild == 0) 862 break; 863 } 864 } 865 if (match && matchwild == 0) 866 return match; 867 868 if (vp && table->vestige) { 869 void *state = (*table->vestige->init_ports4)(laddr, lport_arg, lookup_wildcard); 870 vestigial_inpcb_t better; 871 872 while (table->vestige 873 && (*table->vestige->next_port4)(state, vp)) { 874 875 if (vp->lport != lport) 876 continue; 877 wildcard = 0; 878 if (!in_nullhost(vp->faddr.v4)) 879 wildcard++; 880 if (in_nullhost(vp->laddr.v4)) { 881 if (!in_nullhost(laddr)) 882 wildcard++; 883 } else { 884 if (in_nullhost(laddr)) 885 wildcard++; 886 else { 887 if (!in_hosteq(vp->laddr.v4, laddr)) 888 continue; 889 } 890 } 891 if (wildcard && !lookup_wildcard) 892 continue; 893 if (wildcard < matchwild) { 894 better = *vp; 895 match = (void*)&better; 896 897 matchwild = wildcard; 898 if (matchwild == 0) 899 break; 900 } 901 } 902 903 if (match) { 904 if (match != (void*)&better) 905 return match; 906 else { 907 *vp = better; 908 return 0; 909 } 910 } 911 } 912 913 return (match); 914 } 915 916 #ifdef DIAGNOSTIC 917 int in_pcbnotifymiss = 0; 918 #endif 919 920 struct inpcb * 921 in_pcblookup_connect(struct inpcbtable *table, 922 struct in_addr faddr, u_int fport_arg, 923 struct in_addr laddr, u_int lport_arg, 924 vestigial_inpcb_t *vp) 925 { 926 struct inpcbhead *head; 927 struct inpcb_hdr *inph; 928 struct inpcb *inp; 929 u_int16_t fport = fport_arg, lport = lport_arg; 930 931 if (vp) 932 vp->valid = 0; 933 934 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport); 935 LIST_FOREACH(inph, head, inph_hash) { 936 inp = (struct inpcb *)inph; 937 if (inp->inp_af != AF_INET) 938 continue; 939 940 if (in_hosteq(inp->inp_faddr, faddr) && 941 inp->inp_fport == fport && 942 inp->inp_lport == lport && 943 in_hosteq(inp->inp_laddr, laddr)) 944 goto out; 945 } 946 if (vp && table->vestige) { 947 if ((*table->vestige->lookup4)(faddr, fport_arg, 948 laddr, lport_arg, vp)) 949 return 0; 950 } 951 952 #ifdef DIAGNOSTIC 953 if (in_pcbnotifymiss) { 954 printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n", 955 ntohl(faddr.s_addr), ntohs(fport), 956 ntohl(laddr.s_addr), ntohs(lport)); 957 } 958 #endif 959 return (0); 960 961 out: 962 /* Move this PCB to the head of hash chain. */ 963 inph = &inp->inp_head; 964 if (inph != LIST_FIRST(head)) { 965 LIST_REMOVE(inph, inph_hash); 966 LIST_INSERT_HEAD(head, inph, inph_hash); 967 } 968 return (inp); 969 } 970 971 struct inpcb * 972 in_pcblookup_bind(struct inpcbtable *table, 973 struct in_addr laddr, u_int lport_arg) 974 { 975 struct inpcbhead *head; 976 struct inpcb_hdr *inph; 977 struct inpcb *inp; 978 u_int16_t lport = lport_arg; 979 980 head = INPCBHASH_BIND(table, laddr, lport); 981 LIST_FOREACH(inph, head, inph_hash) { 982 inp = (struct inpcb *)inph; 983 if (inp->inp_af != AF_INET) 984 continue; 985 986 if (inp->inp_lport == lport && 987 in_hosteq(inp->inp_laddr, laddr)) 988 goto out; 989 } 990 head = INPCBHASH_BIND(table, zeroin_addr, lport); 991 LIST_FOREACH(inph, head, inph_hash) { 992 inp = (struct inpcb *)inph; 993 if (inp->inp_af != AF_INET) 994 continue; 995 996 if (inp->inp_lport == lport && 997 in_hosteq(inp->inp_laddr, zeroin_addr)) 998 goto out; 999 } 1000 #ifdef DIAGNOSTIC 1001 if (in_pcbnotifymiss) { 1002 printf("in_pcblookup_bind: laddr=%08x lport=%d\n", 1003 ntohl(laddr.s_addr), ntohs(lport)); 1004 } 1005 #endif 1006 return (0); 1007 1008 out: 1009 /* Move this PCB to the head of hash chain. */ 1010 inph = &inp->inp_head; 1011 if (inph != LIST_FIRST(head)) { 1012 LIST_REMOVE(inph, inph_hash); 1013 LIST_INSERT_HEAD(head, inph, inph_hash); 1014 } 1015 return (inp); 1016 } 1017 1018 void 1019 in_pcbstate(struct inpcb *inp, int state) 1020 { 1021 1022 if (inp->inp_af != AF_INET) 1023 return; 1024 1025 if (inp->inp_state > INP_ATTACHED) 1026 LIST_REMOVE(&inp->inp_head, inph_hash); 1027 1028 switch (state) { 1029 case INP_BOUND: 1030 LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table, 1031 inp->inp_laddr, inp->inp_lport), &inp->inp_head, 1032 inph_hash); 1033 break; 1034 case INP_CONNECTED: 1035 LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table, 1036 inp->inp_faddr, inp->inp_fport, 1037 inp->inp_laddr, inp->inp_lport), &inp->inp_head, 1038 inph_hash); 1039 break; 1040 } 1041 1042 inp->inp_state = state; 1043 } 1044 1045 struct rtentry * 1046 in_pcbrtentry(struct inpcb *inp) 1047 { 1048 struct route *ro; 1049 union { 1050 struct sockaddr dst; 1051 struct sockaddr_in dst4; 1052 } u; 1053 1054 if (inp->inp_af != AF_INET) 1055 return (NULL); 1056 1057 ro = &inp->inp_route; 1058 1059 sockaddr_in_init(&u.dst4, &inp->inp_faddr, 0); 1060 return rtcache_lookup(ro, &u.dst); 1061 } 1062