1 /* $OpenBSD: in_pcb.c,v 1.127 2012/07/12 15:59:17 claudio Exp $ */ 2 /* $NetBSD: in_pcb.c,v 1.25 1996/02/13 23:41:53 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1991, 1993 6 * The Regents of the University of California. 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 University 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 REGENTS 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 REGENTS 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 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 33 * 34 * NRL grants permission for redistribution and use in source and binary 35 * forms, with or without modification, of the software and documentation 36 * created at NRL provided that the following conditions are met: 37 * 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. All advertising materials mentioning features or use of this software 44 * must display the following acknowledgements: 45 * This product includes software developed by the University of 46 * California, Berkeley and its contributors. 47 * This product includes software developed at the Information 48 * Technology Division, US Naval Research Laboratory. 49 * 4. Neither the name of the NRL nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 54 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 56 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 57 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 58 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 59 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 60 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 61 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 62 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 63 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64 * 65 * The views and conclusions contained in the software and documentation 66 * are those of the authors and should not be interpreted as representing 67 * official policies, either expressed or implied, of the US Naval 68 * Research Laboratory (NRL). 69 */ 70 71 #include "pf.h" 72 73 #include <sys/param.h> 74 #include <sys/systm.h> 75 #include <sys/mbuf.h> 76 #include <sys/protosw.h> 77 #include <sys/socket.h> 78 #include <sys/socketvar.h> 79 #include <sys/proc.h> 80 #include <sys/domain.h> 81 #include <sys/pool.h> 82 83 #include <net/if.h> 84 #include <net/route.h> 85 #include <net/pfvar.h> 86 87 #include <netinet/in.h> 88 #include <netinet/in_systm.h> 89 #include <netinet/ip.h> 90 #include <netinet/in_pcb.h> 91 #include <netinet/in_var.h> 92 #include <netinet/ip_var.h> 93 #include <dev/rndvar.h> 94 95 #include <sys/mount.h> 96 #include <nfs/nfsproto.h> 97 98 #ifdef INET6 99 #include <netinet6/ip6_var.h> 100 #endif /* INET6 */ 101 #ifdef IPSEC 102 #include <netinet/ip_esp.h> 103 #endif /* IPSEC */ 104 105 struct in_addr zeroin_addr; 106 107 extern int ipsec_auth_default_level; 108 extern int ipsec_esp_trans_default_level; 109 extern int ipsec_esp_network_default_level; 110 extern int ipsec_ipcomp_default_level; 111 112 /* 113 * These configure the range of local port addresses assigned to 114 * "unspecified" outgoing connections/packets/whatever. 115 */ 116 int ipport_firstauto = IPPORT_RESERVED; 117 int ipport_lastauto = IPPORT_USERRESERVED; 118 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; 119 int ipport_hilastauto = IPPORT_HILASTAUTO; 120 121 struct baddynamicports baddynamicports; 122 struct pool inpcb_pool; 123 int inpcb_pool_initialized = 0; 124 125 #define INPCBHASH(table, faddr, fport, laddr, lport, rdom) \ 126 &(table)->inpt_hashtbl[(ntohl((faddr)->s_addr) + \ 127 ntohs((fport)) + ntohs((lport)) + (rdom)) & (table->inpt_hash)] 128 129 #define IN6PCBHASH(table, faddr, fport, laddr, lport) \ 130 &(table)->inpt_hashtbl[(ntohl((faddr)->s6_addr32[0] ^ \ 131 (faddr)->s6_addr32[3]) + ntohs((fport)) + ntohs((lport))) & \ 132 (table->inpt_hash)] 133 134 #define INPCBLHASH(table, lport, rdom) \ 135 &(table)->inpt_lhashtbl[(ntohs((lport)) + (rdom)) & table->inpt_lhash] 136 137 void 138 in_pcbinit(struct inpcbtable *table, int hashsize) 139 { 140 141 CIRCLEQ_INIT(&table->inpt_queue); 142 table->inpt_hashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, 143 &table->inpt_hash); 144 if (table->inpt_hashtbl == NULL) 145 panic("in_pcbinit: hashinit failed"); 146 table->inpt_lhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, 147 &table->inpt_lhash); 148 if (table->inpt_lhashtbl == NULL) 149 panic("in_pcbinit: hashinit failed for lport"); 150 table->inpt_lastport = 0; 151 } 152 153 /* 154 * Check if the specified port is invalid for dynamic allocation. 155 */ 156 int 157 in_baddynamic(u_int16_t port, u_int16_t proto) 158 { 159 switch (proto) { 160 case IPPROTO_TCP: 161 return (DP_ISSET(baddynamicports.tcp, port)); 162 case IPPROTO_UDP: 163 #ifdef IPSEC 164 /* Cannot preset this as it is a sysctl */ 165 if (port == udpencap_port) 166 return (1); 167 #endif 168 return (DP_ISSET(baddynamicports.udp, port)); 169 default: 170 return (0); 171 } 172 } 173 174 int 175 in_pcballoc(struct socket *so, void *v) 176 { 177 struct inpcbtable *table = v; 178 struct inpcb *inp; 179 int s; 180 181 if (inpcb_pool_initialized == 0) { 182 pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 0, 0, 183 "inpcbpl", NULL); 184 inpcb_pool_initialized = 1; 185 } 186 inp = pool_get(&inpcb_pool, PR_NOWAIT|PR_ZERO); 187 if (inp == NULL) 188 return (ENOBUFS); 189 inp->inp_table = table; 190 inp->inp_socket = so; 191 inp->inp_seclevel[SL_AUTH] = ipsec_auth_default_level; 192 inp->inp_seclevel[SL_ESP_TRANS] = ipsec_esp_trans_default_level; 193 inp->inp_seclevel[SL_ESP_NETWORK] = ipsec_esp_network_default_level; 194 inp->inp_seclevel[SL_IPCOMP] = ipsec_ipcomp_default_level; 195 inp->inp_rtableid = curproc->p_p->ps_rtableid; 196 s = splnet(); 197 CIRCLEQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue); 198 LIST_INSERT_HEAD(INPCBLHASH(table, inp->inp_lport, 199 inp->inp_rtableid), inp, inp_lhash); 200 LIST_INSERT_HEAD(INPCBHASH(table, &inp->inp_faddr, inp->inp_fport, 201 &inp->inp_laddr, inp->inp_lport, rtable_l2(inp->inp_rtableid)), 202 inp, inp_hash); 203 splx(s); 204 so->so_pcb = inp; 205 inp->inp_hops = -1; 206 207 #ifdef INET6 208 /* 209 * Small change in this function to set the INP_IPV6 flag so routines 210 * outside pcb-specific routines don't need to use sotopf(), and all 211 * of its pointer chasing, later. 212 */ 213 if (sotopf(so) == PF_INET6) 214 inp->inp_flags = INP_IPV6; 215 inp->in6p_cksum = -1; 216 #endif /* INET6 */ 217 return (0); 218 } 219 220 int 221 in_pcbbind(void *v, struct mbuf *nam, struct proc *p) 222 { 223 struct inpcb *inp = v; 224 struct socket *so = inp->inp_socket; 225 struct inpcbtable *table = inp->inp_table; 226 u_int16_t *lastport = &inp->inp_table->inpt_lastport; 227 struct sockaddr_in *sin; 228 u_int16_t lport = 0; 229 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 230 int error; 231 232 #ifdef INET6 233 if (sotopf(so) == PF_INET6) 234 return in6_pcbbind(inp, nam, p); 235 #endif /* INET6 */ 236 237 if (TAILQ_EMPTY(&in_ifaddr)) 238 return (EADDRNOTAVAIL); 239 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY) 240 return (EINVAL); 241 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 && 242 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 || 243 (so->so_options & SO_ACCEPTCONN) == 0)) 244 wild = INPLOOKUP_WILDCARD; 245 if (nam) { 246 sin = mtod(nam, struct sockaddr_in *); 247 if (nam->m_len != sizeof (*sin)) 248 return (EINVAL); 249 #ifdef notdef 250 /* 251 * We should check the family, but old programs 252 * incorrectly fail to initialize it. 253 */ 254 if (sin->sin_family != AF_INET) 255 return (EAFNOSUPPORT); 256 #endif 257 lport = sin->sin_port; 258 if (IN_MULTICAST(sin->sin_addr.s_addr)) { 259 /* 260 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 261 * allow complete duplication of binding if 262 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 263 * and a multicast address is bound on both 264 * new and duplicated sockets. 265 */ 266 if (so->so_options & SO_REUSEADDR) 267 reuseport = SO_REUSEADDR|SO_REUSEPORT; 268 } else if (sin->sin_addr.s_addr != INADDR_ANY) { 269 sin->sin_port = 0; /* yech... */ 270 if (!(so->so_options & SO_BINDANY) && 271 in_iawithaddr(sin->sin_addr, 272 inp->inp_rtableid) == NULL) 273 /* SOCK_RAW does not use in_pcbbind() */ 274 if (!(so->so_type == SOCK_DGRAM && 275 in_broadcast(sin->sin_addr, NULL, 276 inp->inp_rtableid))) 277 return (EADDRNOTAVAIL); 278 } 279 if (lport) { 280 struct inpcb *t; 281 282 /* GROSS */ 283 if (ntohs(lport) < IPPORT_RESERVED && 284 (error = suser(p, 0))) 285 return (EACCES); 286 if (so->so_euid) { 287 t = in_pcblookup(table, &zeroin_addr, 0, 288 &sin->sin_addr, lport, INPLOOKUP_WILDCARD, 289 inp->inp_rtableid); 290 if (t && (so->so_euid != t->inp_socket->so_euid)) 291 return (EADDRINUSE); 292 } 293 t = in_pcblookup(table, &zeroin_addr, 0, 294 &sin->sin_addr, lport, wild, inp->inp_rtableid); 295 if (t && (reuseport & t->inp_socket->so_options) == 0) 296 return (EADDRINUSE); 297 } 298 inp->inp_laddr = sin->sin_addr; 299 } 300 if (lport == 0) { 301 u_int16_t first, last; 302 int count; 303 304 if (inp->inp_flags & INP_HIGHPORT) { 305 first = ipport_hifirstauto; /* sysctl */ 306 last = ipport_hilastauto; 307 } else if (inp->inp_flags & INP_LOWPORT) { 308 if ((error = suser(p, 0))) 309 return (EACCES); 310 first = IPPORT_RESERVED-1; /* 1023 */ 311 last = 600; /* not IPPORT_RESERVED/2 */ 312 } else { 313 first = ipport_firstauto; /* sysctl */ 314 last = ipport_lastauto; 315 } 316 317 /* 318 * Simple check to ensure all ports are not used up causing 319 * a deadlock here. 320 * 321 * We split the two cases (up and down) so that the direction 322 * is not being tested on each round of the loop. 323 */ 324 325 if (first > last) { 326 /* 327 * counting down 328 */ 329 count = first - last; 330 if (count) 331 *lastport = first - arc4random_uniform(count); 332 333 do { 334 if (count-- < 0) /* completely used? */ 335 return (EADDRNOTAVAIL); 336 --*lastport; 337 if (*lastport > first || *lastport < last) 338 *lastport = first; 339 lport = htons(*lastport); 340 } while (in_baddynamic(*lastport, so->so_proto->pr_protocol) || 341 in_pcblookup(table, &zeroin_addr, 0, 342 &inp->inp_laddr, lport, wild, inp->inp_rtableid)); 343 } else { 344 /* 345 * counting up 346 */ 347 count = last - first; 348 if (count) 349 *lastport = first + arc4random_uniform(count); 350 351 do { 352 if (count-- < 0) /* completely used? */ 353 return (EADDRNOTAVAIL); 354 ++*lastport; 355 if (*lastport < first || *lastport > last) 356 *lastport = first; 357 lport = htons(*lastport); 358 } while (in_baddynamic(*lastport, so->so_proto->pr_protocol) || 359 in_pcblookup(table, &zeroin_addr, 0, 360 &inp->inp_laddr, lport, wild, inp->inp_rtableid)); 361 } 362 } 363 inp->inp_lport = lport; 364 in_pcbrehash(inp); 365 return (0); 366 } 367 368 /* 369 * Connect from a socket to a specified address. 370 * Both address and port must be specified in argument sin. 371 * If don't have a local address for this socket yet, 372 * then pick one. 373 */ 374 int 375 in_pcbconnect(void *v, struct mbuf *nam) 376 { 377 struct inpcb *inp = v; 378 struct sockaddr_in *ifaddr = NULL; 379 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 380 381 #ifdef INET6 382 if (sotopf(inp->inp_socket) == PF_INET6) 383 return (in6_pcbconnect(inp, nam)); 384 if ((inp->inp_flags & INP_IPV6) != 0) 385 panic("IPv6 pcb passed into in_pcbconnect"); 386 #endif /* INET6 */ 387 388 if (nam->m_len != sizeof (*sin)) 389 return (EINVAL); 390 if (sin->sin_family != AF_INET) 391 return (EAFNOSUPPORT); 392 if (sin->sin_port == 0) 393 return (EADDRNOTAVAIL); 394 if (!TAILQ_EMPTY(&in_ifaddr)) { 395 /* 396 * If the destination address is INADDR_ANY, 397 * use the primary local address. 398 * If the supplied address is INADDR_BROADCAST, 399 * and the primary interface supports broadcast, 400 * choose the broadcast address for that interface. 401 */ 402 if (sin->sin_addr.s_addr == INADDR_ANY) 403 sin->sin_addr = TAILQ_FIRST(&in_ifaddr)->ia_addr.sin_addr; 404 else if (sin->sin_addr.s_addr == INADDR_BROADCAST && 405 (TAILQ_FIRST(&in_ifaddr)->ia_ifp->if_flags & IFF_BROADCAST) && 406 TAILQ_FIRST(&in_ifaddr)->ia_broadaddr.sin_addr.s_addr) 407 sin->sin_addr = 408 TAILQ_FIRST(&in_ifaddr)->ia_broadaddr.sin_addr; 409 } 410 if (inp->inp_laddr.s_addr == INADDR_ANY) { 411 int error; 412 ifaddr = in_selectsrc(sin, &inp->inp_route, 413 inp->inp_socket->so_options, inp->inp_moptions, &error, 414 inp->inp_rtableid); 415 if (ifaddr == NULL) { 416 if (error == 0) 417 error = EADDRNOTAVAIL; 418 return error; 419 } 420 } 421 if (in_pcbhashlookup(inp->inp_table, sin->sin_addr, sin->sin_port, 422 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr, 423 inp->inp_lport, inp->inp_rtableid) != 0) 424 return (EADDRINUSE); 425 if (inp->inp_laddr.s_addr == INADDR_ANY) { 426 if (inp->inp_lport == 0 && 427 in_pcbbind(inp, NULL, curproc) == EADDRNOTAVAIL) 428 return (EADDRNOTAVAIL); 429 inp->inp_laddr = ifaddr->sin_addr; 430 } 431 inp->inp_faddr = sin->sin_addr; 432 inp->inp_fport = sin->sin_port; 433 in_pcbrehash(inp); 434 #ifdef IPSEC 435 { 436 int error; /* This is just ignored */ 437 438 /* Cause an IPsec SA to be established. */ 439 ipsp_spd_inp(NULL, AF_INET, 0, &error, IPSP_DIRECTION_OUT, 440 NULL, inp, NULL); 441 } 442 #endif 443 return (0); 444 } 445 446 void 447 in_pcbdisconnect(void *v) 448 { 449 struct inpcb *inp = v; 450 451 switch (sotopf(inp->inp_socket)) { 452 #ifdef INET6 453 case PF_INET6: 454 inp->inp_faddr6 = in6addr_any; 455 break; 456 #endif 457 case PF_INET: 458 inp->inp_faddr.s_addr = INADDR_ANY; 459 break; 460 } 461 462 inp->inp_fport = 0; 463 in_pcbrehash(inp); 464 if (inp->inp_socket->so_state & SS_NOFDREF) 465 in_pcbdetach(inp); 466 } 467 468 void 469 in_pcbdetach(void *v) 470 { 471 struct inpcb *inp = v; 472 struct socket *so = inp->inp_socket; 473 int s; 474 475 splsoftassert(IPL_SOFTNET); 476 477 so->so_pcb = 0; 478 sofree(so); 479 if (inp->inp_options) 480 m_freem(inp->inp_options); 481 if (inp->inp_route.ro_rt) 482 rtfree(inp->inp_route.ro_rt); 483 #ifdef INET6 484 if (inp->inp_flags & INP_IPV6) { 485 ip6_freepcbopts(inp->inp_outputopts6); 486 ip6_freemoptions(inp->inp_moptions6); 487 } else 488 #endif 489 ip_freemoptions(inp->inp_moptions); 490 #ifdef IPSEC 491 /* IPsec cleanup here */ 492 s = spltdb(); 493 if (inp->inp_tdb_in) 494 TAILQ_REMOVE(&inp->inp_tdb_in->tdb_inp_in, 495 inp, inp_tdb_in_next); 496 if (inp->inp_tdb_out) 497 TAILQ_REMOVE(&inp->inp_tdb_out->tdb_inp_out, inp, 498 inp_tdb_out_next); 499 if (inp->inp_ipsec_remotecred) 500 ipsp_reffree(inp->inp_ipsec_remotecred); 501 if (inp->inp_ipsec_remoteauth) 502 ipsp_reffree(inp->inp_ipsec_remoteauth); 503 if (inp->inp_ipo) 504 ipsec_delete_policy(inp->inp_ipo); 505 splx(s); 506 #endif 507 #if NPF > 0 508 if (inp->inp_pf_sk) 509 ((struct pf_state_key *)inp->inp_pf_sk)->inp = NULL; 510 #endif 511 s = splnet(); 512 LIST_REMOVE(inp, inp_lhash); 513 LIST_REMOVE(inp, inp_hash); 514 CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue); 515 splx(s); 516 pool_put(&inpcb_pool, inp); 517 } 518 519 void 520 in_setsockaddr(struct inpcb *inp, struct mbuf *nam) 521 { 522 struct sockaddr_in *sin; 523 524 nam->m_len = sizeof (*sin); 525 sin = mtod(nam, struct sockaddr_in *); 526 bzero((caddr_t)sin, sizeof (*sin)); 527 sin->sin_family = AF_INET; 528 sin->sin_len = sizeof(*sin); 529 sin->sin_port = inp->inp_lport; 530 sin->sin_addr = inp->inp_laddr; 531 } 532 533 void 534 in_setpeeraddr(struct inpcb *inp, struct mbuf *nam) 535 { 536 struct sockaddr_in *sin; 537 538 #ifdef INET6 539 if (sotopf(inp->inp_socket) == PF_INET6) { 540 in6_setpeeraddr(inp, nam); 541 return; 542 } 543 #endif /* INET6 */ 544 545 nam->m_len = sizeof (*sin); 546 sin = mtod(nam, struct sockaddr_in *); 547 bzero((caddr_t)sin, sizeof (*sin)); 548 sin->sin_family = AF_INET; 549 sin->sin_len = sizeof(*sin); 550 sin->sin_port = inp->inp_fport; 551 sin->sin_addr = inp->inp_faddr; 552 } 553 554 /* 555 * Pass some notification to all connections of a protocol 556 * associated with address dst. The "usual action" will be 557 * taken, depending on the ctlinput cmd. The caller must filter any 558 * cmds that are uninteresting (e.g., no error in the map). 559 * Call the protocol specific routine (if any) to report 560 * any errors for each matching socket. 561 * 562 * Must be called at splsoftnet. 563 */ 564 void 565 in_pcbnotifyall(struct inpcbtable *table, struct sockaddr *dst, u_int rdomain, 566 int errno, void (*notify)(struct inpcb *, int)) 567 { 568 struct inpcb *inp, *oinp; 569 struct in_addr faddr; 570 571 splsoftassert(IPL_SOFTNET); 572 573 #ifdef INET6 574 /* 575 * See in6_pcbnotify() for IPv6 codepath. By the time this 576 * gets called, the addresses passed are either definitely IPv4 or 577 * IPv6; *_pcbnotify() never gets called with v4-mapped v6 addresses. 578 */ 579 #endif /* INET6 */ 580 581 if (dst->sa_family != AF_INET) 582 return; 583 faddr = satosin(dst)->sin_addr; 584 if (faddr.s_addr == INADDR_ANY) 585 return; 586 587 rdomain = rtable_l2(rdomain); 588 for (inp = CIRCLEQ_FIRST(&table->inpt_queue); 589 inp != CIRCLEQ_END(&table->inpt_queue);) { 590 #ifdef INET6 591 if (inp->inp_flags & INP_IPV6) { 592 inp = CIRCLEQ_NEXT(inp, inp_queue); 593 continue; 594 } 595 #endif 596 if (inp->inp_faddr.s_addr != faddr.s_addr || 597 rtable_l2(inp->inp_rtableid) != rdomain || 598 inp->inp_socket == 0) { 599 inp = CIRCLEQ_NEXT(inp, inp_queue); 600 continue; 601 } 602 oinp = inp; 603 inp = CIRCLEQ_NEXT(inp, inp_queue); 604 if (notify) 605 (*notify)(oinp, errno); 606 } 607 } 608 609 /* 610 * Check for alternatives when higher level complains 611 * about service problems. For now, invalidate cached 612 * routing information. If the route was created dynamically 613 * (by a redirect), time to try a default gateway again. 614 */ 615 void 616 in_losing(struct inpcb *inp) 617 { 618 struct rtentry *rt; 619 struct rt_addrinfo info; 620 621 if ((rt = inp->inp_route.ro_rt)) { 622 inp->inp_route.ro_rt = 0; 623 bzero((caddr_t)&info, sizeof(info)); 624 info.rti_flags = rt->rt_flags; 625 info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst; 626 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 627 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 628 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, rt->rt_ifp, 0, 629 inp->inp_rtableid); 630 if (rt->rt_flags & RTF_DYNAMIC) 631 (void)rtrequest1(RTM_DELETE, &info, rt->rt_priority, 632 (struct rtentry **)0, inp->inp_rtableid); 633 /* 634 * A new route can be allocated 635 * the next time output is attempted. 636 * rtfree() needs to be called in anycase because the inp 637 * is still holding a reference to rt. 638 */ 639 rtfree(rt); 640 } 641 } 642 643 /* 644 * After a routing change, flush old routing 645 * and allocate a (hopefully) better one. 646 */ 647 void 648 in_rtchange(struct inpcb *inp, int errno) 649 { 650 if (inp->inp_route.ro_rt) { 651 rtfree(inp->inp_route.ro_rt); 652 inp->inp_route.ro_rt = 0; 653 /* 654 * A new route can be allocated the next time 655 * output is attempted. 656 */ 657 } 658 } 659 660 struct inpcb * 661 in_pcblookup(struct inpcbtable *table, void *faddrp, u_int fport_arg, 662 void *laddrp, u_int lport_arg, int flags, u_int rdomain) 663 { 664 struct inpcb *inp, *match = 0; 665 int matchwild = 3, wildcard; 666 u_int16_t fport = fport_arg, lport = lport_arg; 667 struct in_addr faddr = *(struct in_addr *)faddrp; 668 struct in_addr laddr = *(struct in_addr *)laddrp; 669 670 rdomain = rtable_l2(rdomain); /* convert passed rtableid to rdomain */ 671 for (inp = LIST_FIRST(INPCBLHASH(table, lport, rdomain)); inp; 672 inp = LIST_NEXT(inp, inp_lhash)) { 673 if (rtable_l2(inp->inp_rtableid) != rdomain) 674 continue; 675 if (inp->inp_lport != lport) 676 continue; 677 wildcard = 0; 678 #ifdef INET6 679 if (flags & INPLOOKUP_IPV6) { 680 struct in6_addr *laddr6 = (struct in6_addr *)laddrp; 681 struct in6_addr *faddr6 = (struct in6_addr *)faddrp; 682 683 if (!(inp->inp_flags & INP_IPV6)) 684 continue; 685 686 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6)) { 687 if (IN6_IS_ADDR_UNSPECIFIED(laddr6)) 688 wildcard++; 689 else if (!IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, laddr6)) 690 continue; 691 } else { 692 if (!IN6_IS_ADDR_UNSPECIFIED(laddr6)) 693 wildcard++; 694 } 695 696 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6)) { 697 if (IN6_IS_ADDR_UNSPECIFIED(faddr6)) 698 wildcard++; 699 else if (!IN6_ARE_ADDR_EQUAL(&inp->inp_faddr6, 700 faddr6) || inp->inp_fport != fport) 701 continue; 702 } else { 703 if (!IN6_IS_ADDR_UNSPECIFIED(faddr6)) 704 wildcard++; 705 } 706 } else 707 #endif /* INET6 */ 708 { 709 #ifdef INET6 710 if (inp->inp_flags & INP_IPV6) 711 continue; 712 #endif /* INET6 */ 713 714 if (inp->inp_faddr.s_addr != INADDR_ANY) { 715 if (faddr.s_addr == INADDR_ANY) 716 wildcard++; 717 else if (inp->inp_faddr.s_addr != faddr.s_addr || 718 inp->inp_fport != fport) 719 continue; 720 } else { 721 if (faddr.s_addr != INADDR_ANY) 722 wildcard++; 723 } 724 if (inp->inp_laddr.s_addr != INADDR_ANY) { 725 if (laddr.s_addr == INADDR_ANY) 726 wildcard++; 727 else if (inp->inp_laddr.s_addr != laddr.s_addr) 728 continue; 729 } else { 730 if (laddr.s_addr != INADDR_ANY) 731 wildcard++; 732 } 733 } 734 if ((!wildcard || (flags & INPLOOKUP_WILDCARD)) && 735 wildcard < matchwild) { 736 match = inp; 737 if ((matchwild = wildcard) == 0) 738 break; 739 } 740 } 741 return (match); 742 } 743 744 struct rtentry * 745 in_pcbrtentry(struct inpcb *inp) 746 { 747 struct route *ro; 748 749 ro = &inp->inp_route; 750 751 /* check if route is still valid */ 752 if (ro->ro_rt && (ro->ro_rt->rt_flags & RTF_UP) == 0) { 753 RTFREE(ro->ro_rt); 754 ro->ro_rt = NULL; 755 } 756 757 /* 758 * No route yet, so try to acquire one. 759 */ 760 if (ro->ro_rt == NULL) { 761 #ifdef INET6 762 bzero(ro, sizeof(struct route_in6)); 763 #else 764 bzero(ro, sizeof(struct route)); 765 #endif 766 767 switch(sotopf(inp->inp_socket)) { 768 #ifdef INET6 769 case PF_INET6: 770 if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6)) 771 break; 772 ro->ro_dst.sa_family = AF_INET6; 773 ro->ro_dst.sa_len = sizeof(struct sockaddr_in6); 774 ((struct sockaddr_in6 *) &ro->ro_dst)->sin6_addr = 775 inp->inp_faddr6; 776 ro->ro_tableid = inp->inp_rtableid; 777 rtalloc_mpath(ro, &inp->inp_laddr6.s6_addr32[0]); 778 break; 779 #endif /* INET6 */ 780 case PF_INET: 781 if (inp->inp_faddr.s_addr == INADDR_ANY) 782 break; 783 ro->ro_dst.sa_family = AF_INET; 784 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 785 ro->ro_tableid = inp->inp_rtableid; 786 satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr; 787 rtalloc_mpath(ro, &inp->inp_laddr.s_addr); 788 break; 789 } 790 } 791 return (ro->ro_rt); 792 } 793 794 struct sockaddr_in * 795 in_selectsrc(struct sockaddr_in *sin, struct route *ro, int soopts, 796 struct ip_moptions *mopts, int *errorp, u_int rtableid) 797 { 798 struct sockaddr_in *sin2; 799 struct in_ifaddr *ia; 800 801 ia = (struct in_ifaddr *)0; 802 /* 803 * If the destination address is multicast and an outgoing 804 * interface has been set as a multicast option, use the 805 * address of that interface as our source address. 806 */ 807 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) { 808 struct ifnet *ifp; 809 810 if (mopts->imo_multicast_ifp != NULL) { 811 ifp = mopts->imo_multicast_ifp; 812 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) 813 if (ia->ia_ifp == ifp && 814 rtable_l2(rtableid) == ifp->if_rdomain) 815 break; 816 if (ia == 0) { 817 *errorp = EADDRNOTAVAIL; 818 return NULL; 819 } 820 return satosin(&ia->ia_addr); 821 } 822 } 823 /* 824 * If route is known or can be allocated now, 825 * our src addr is taken from the i/f, else punt. 826 */ 827 if (ro->ro_rt && (!(ro->ro_rt->rt_flags & RTF_UP) || 828 (satosin(&ro->ro_dst)->sin_addr.s_addr != sin->sin_addr.s_addr || 829 soopts & SO_DONTROUTE))) { 830 RTFREE(ro->ro_rt); 831 ro->ro_rt = NULL; 832 } 833 if ((soopts & SO_DONTROUTE) == 0 && /*XXX*/ 834 (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL)) { 835 /* No route yet, so try to acquire one */ 836 ro->ro_dst.sa_family = AF_INET; 837 ro->ro_dst.sa_len = sizeof(struct sockaddr_in); 838 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr; 839 ro->ro_tableid = rtableid; 840 rtalloc_mpath(ro, NULL); 841 842 /* 843 * It is important to bzero out the rest of the 844 * struct sockaddr_in when mixing v6 & v4! 845 */ 846 sin2 = (struct sockaddr_in *)&ro->ro_dst; 847 bzero(sin2->sin_zero, sizeof(sin2->sin_zero)); 848 } 849 /* 850 * If we found a route, use the address 851 * corresponding to the outgoing interface 852 * unless it is the loopback (in case a route 853 * to our address on another net goes to loopback). 854 */ 855 if (ro->ro_rt && ro->ro_rt->rt_ifp && 856 !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) 857 ia = ifatoia(ro->ro_rt->rt_ifa); 858 if (ia == 0) { 859 u_int16_t fport = sin->sin_port; 860 861 sin->sin_port = 0; 862 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin), rtableid)); 863 if (ia == 0) 864 ia = ifatoia(ifa_ifwithnet(sintosa(sin), rtableid)); 865 sin->sin_port = fport; 866 if (ia == 0) 867 ia = TAILQ_FIRST(&in_ifaddr); 868 if (ia == 0) { 869 *errorp = EADDRNOTAVAIL; 870 return NULL; 871 } 872 } 873 return satosin(&ia->ia_addr); 874 } 875 876 void 877 in_pcbrehash(struct inpcb *inp) 878 { 879 struct inpcbtable *table = inp->inp_table; 880 int s; 881 882 s = splnet(); 883 LIST_REMOVE(inp, inp_lhash); 884 LIST_INSERT_HEAD(INPCBLHASH(table, inp->inp_lport, inp->inp_rtableid), 885 inp, inp_lhash); 886 LIST_REMOVE(inp, inp_hash); 887 #ifdef INET6 888 if (inp->inp_flags & INP_IPV6) { 889 LIST_INSERT_HEAD(IN6PCBHASH(table, &inp->inp_faddr6, 890 inp->inp_fport, &inp->inp_laddr6, inp->inp_lport), 891 inp, inp_hash); 892 } else { 893 #endif /* INET6 */ 894 LIST_INSERT_HEAD(INPCBHASH(table, &inp->inp_faddr, 895 inp->inp_fport, &inp->inp_laddr, inp->inp_lport, 896 rtable_l2(inp->inp_rtableid)), inp, inp_hash); 897 #ifdef INET6 898 } 899 #endif /* INET6 */ 900 splx(s); 901 } 902 903 #ifdef DIAGNOSTIC 904 int in_pcbnotifymiss = 0; 905 #endif 906 907 /* 908 * The in(6)_pcbhashlookup functions are used to locate connected sockets 909 * quickly: 910 * faddr.fport <-> laddr.lport 911 * No wildcard matching is done so that listening sockets are not found. 912 * If the functions return NULL in(6)_pcblookup_listen can be used to 913 * find a listening/bound socket that may accept the connection. 914 * After those two lookups no other are necessary. 915 */ 916 struct inpcb * 917 in_pcbhashlookup(struct inpcbtable *table, struct in_addr faddr, 918 u_int fport_arg, struct in_addr laddr, u_int lport_arg, u_int rdomain) 919 { 920 struct inpcbhead *head; 921 struct inpcb *inp; 922 u_int16_t fport = fport_arg, lport = lport_arg; 923 924 rdomain = rtable_l2(rdomain); /* convert passed rtableid to rdomain */ 925 head = INPCBHASH(table, &faddr, fport, &laddr, lport, rdomain); 926 LIST_FOREACH(inp, head, inp_hash) { 927 #ifdef INET6 928 if (inp->inp_flags & INP_IPV6) 929 continue; /*XXX*/ 930 #endif 931 if (inp->inp_faddr.s_addr == faddr.s_addr && 932 inp->inp_fport == fport && 933 inp->inp_lport == lport && 934 inp->inp_laddr.s_addr == laddr.s_addr && 935 rtable_l2(inp->inp_rtableid) == rdomain) { 936 /* 937 * Move this PCB to the head of hash chain so that 938 * repeated accesses are quicker. This is analogous to 939 * the historic single-entry PCB cache. 940 */ 941 if (inp != LIST_FIRST(head)) { 942 LIST_REMOVE(inp, inp_hash); 943 LIST_INSERT_HEAD(head, inp, inp_hash); 944 } 945 break; 946 } 947 } 948 #ifdef DIAGNOSTIC 949 if (inp == NULL && in_pcbnotifymiss) { 950 printf("in_pcbhashlookup: faddr=%08x fport=%d laddr=%08x lport=%d rdom=%d\n", 951 ntohl(faddr.s_addr), ntohs(fport), 952 ntohl(laddr.s_addr), ntohs(lport), rdomain); 953 } 954 #endif 955 return (inp); 956 } 957 958 #ifdef INET6 959 struct inpcb * 960 in6_pcbhashlookup(struct inpcbtable *table, struct in6_addr *faddr, 961 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg) 962 { 963 struct inpcbhead *head; 964 struct inpcb *inp; 965 u_int16_t fport = fport_arg, lport = lport_arg; 966 967 head = IN6PCBHASH(table, faddr, fport, laddr, lport); 968 LIST_FOREACH(inp, head, inp_hash) { 969 if (!(inp->inp_flags & INP_IPV6)) 970 continue; 971 if (IN6_ARE_ADDR_EQUAL(&inp->inp_faddr6, faddr) && 972 inp->inp_fport == fport && inp->inp_lport == lport && 973 IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, laddr)) { 974 /* 975 * Move this PCB to the head of hash chain so that 976 * repeated accesses are quicker. This is analogous to 977 * the historic single-entry PCB cache. 978 */ 979 if (inp != LIST_FIRST(head)) { 980 LIST_REMOVE(inp, inp_hash); 981 LIST_INSERT_HEAD(head, inp, inp_hash); 982 } 983 break; 984 } 985 } 986 #ifdef DIAGNOSTIC 987 if (inp == NULL && in_pcbnotifymiss) { 988 printf("in6_pcbhashlookup: faddr="); 989 printf(" fport=%d laddr=", ntohs(fport)); 990 printf(" lport=%d\n", ntohs(lport)); 991 } 992 #endif 993 return (inp); 994 } 995 #endif /* INET6 */ 996 997 /* 998 * The in(6)_pcblookup_listen functions are used to locate listening 999 * sockets quickly. This are sockets with unspecified foreign address 1000 * and port: 1001 * *.* <-> laddr.lport 1002 * *.* <-> *.lport 1003 */ 1004 struct inpcb * 1005 in_pcblookup_listen(struct inpcbtable *table, struct in_addr laddr, 1006 u_int lport_arg, int reverse, struct mbuf *m, u_int rdomain) 1007 { 1008 struct inpcbhead *head; 1009 struct in_addr *key1, *key2; 1010 struct inpcb *inp; 1011 u_int16_t lport = lport_arg; 1012 1013 rdomain = rtable_l2(rdomain); /* convert passed rtableid to rdomain */ 1014 #if NPF > 0 1015 if (m && m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) { 1016 struct pf_divert *divert; 1017 1018 if ((divert = pf_find_divert(m)) == NULL) 1019 return (NULL); 1020 key1 = key2 = &divert->addr.v4; 1021 lport = divert->port; 1022 } else 1023 #endif 1024 if (reverse) { 1025 key1 = &zeroin_addr; 1026 key2 = &laddr; 1027 } else { 1028 key1 = &laddr; 1029 key2 = &zeroin_addr; 1030 } 1031 1032 head = INPCBHASH(table, &zeroin_addr, 0, key1, lport, rdomain); 1033 LIST_FOREACH(inp, head, inp_hash) { 1034 #ifdef INET6 1035 if (inp->inp_flags & INP_IPV6) 1036 continue; /*XXX*/ 1037 #endif 1038 if (inp->inp_lport == lport && inp->inp_fport == 0 && 1039 inp->inp_laddr.s_addr == key1->s_addr && 1040 inp->inp_faddr.s_addr == INADDR_ANY && 1041 rtable_l2(inp->inp_rtableid) == rdomain) 1042 break; 1043 } 1044 if (inp == NULL && key1->s_addr != key2->s_addr) { 1045 head = INPCBHASH(table, &zeroin_addr, 0, key2, lport, rdomain); 1046 LIST_FOREACH(inp, head, inp_hash) { 1047 #ifdef INET6 1048 if (inp->inp_flags & INP_IPV6) 1049 continue; /*XXX*/ 1050 #endif 1051 if (inp->inp_lport == lport && inp->inp_fport == 0 && 1052 inp->inp_laddr.s_addr == key2->s_addr && 1053 inp->inp_faddr.s_addr == INADDR_ANY && 1054 rtable_l2(inp->inp_rtableid) == rdomain) 1055 break; 1056 } 1057 } 1058 #ifdef DIAGNOSTIC 1059 if (inp == NULL && in_pcbnotifymiss) { 1060 printf("in_pcblookup_listen: laddr=%08x lport=%d\n", 1061 ntohl(laddr.s_addr), ntohs(lport)); 1062 } 1063 #endif 1064 /* 1065 * Move this PCB to the head of hash chain so that 1066 * repeated accesses are quicker. This is analogous to 1067 * the historic single-entry PCB cache. 1068 */ 1069 if (inp != NULL && inp != LIST_FIRST(head)) { 1070 LIST_REMOVE(inp, inp_hash); 1071 LIST_INSERT_HEAD(head, inp, inp_hash); 1072 } 1073 return (inp); 1074 } 1075 1076 #ifdef INET6 1077 struct inpcb * 1078 in6_pcblookup_listen(struct inpcbtable *table, struct in6_addr *laddr, 1079 u_int lport_arg, int reverse, struct mbuf *m) 1080 { 1081 struct inpcbhead *head; 1082 struct in6_addr *key1, *key2; 1083 struct inpcb *inp; 1084 u_int16_t lport = lport_arg; 1085 1086 #if NPF > 0 1087 if (m && m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) { 1088 struct pf_divert *divert; 1089 1090 if ((divert = pf_find_divert(m)) == NULL) 1091 return (NULL); 1092 key1 = key2 = &divert->addr.v6; 1093 lport = divert->port; 1094 } else 1095 #endif 1096 if (reverse) { 1097 key1 = &zeroin6_addr; 1098 key2 = laddr; 1099 } else { 1100 key1 = laddr; 1101 key2 = &zeroin6_addr; 1102 } 1103 1104 head = IN6PCBHASH(table, &zeroin6_addr, 0, key1, lport); 1105 LIST_FOREACH(inp, head, inp_hash) { 1106 if (!(inp->inp_flags & INP_IPV6)) 1107 continue; 1108 if (inp->inp_lport == lport && inp->inp_fport == 0 && 1109 IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, key1) && 1110 IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6)) 1111 break; 1112 } 1113 if (inp == NULL && ! IN6_ARE_ADDR_EQUAL(key1, key2)) { 1114 head = IN6PCBHASH(table, &zeroin6_addr, 0, key2, lport); 1115 LIST_FOREACH(inp, head, inp_hash) { 1116 if (!(inp->inp_flags & INP_IPV6)) 1117 continue; 1118 if (inp->inp_lport == lport && inp->inp_fport == 0 && 1119 IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, key2) && 1120 IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6)) 1121 break; 1122 } 1123 } 1124 #ifdef DIAGNOSTIC 1125 if (inp == NULL && in_pcbnotifymiss) { 1126 printf("in6_pcblookup_listen: laddr= lport=%d\n", 1127 ntohs(lport)); 1128 } 1129 #endif 1130 /* 1131 * Move this PCB to the head of hash chain so that 1132 * repeated accesses are quicker. This is analogous to 1133 * the historic single-entry PCB cache. 1134 */ 1135 if (inp != NULL && inp != LIST_FIRST(head)) { 1136 LIST_REMOVE(inp, inp_hash); 1137 LIST_INSERT_HEAD(head, inp, inp_hash); 1138 } 1139 return (inp); 1140 } 1141 #endif /* INET6 */ 1142