1 /* 2 * Copyright (c) 1982, 1986, 1991, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95 34 * $FreeBSD$ 35 */ 36 37 #include "opt_ipsec.h" 38 #include "opt_inet6.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/malloc.h> 43 #include <sys/mbuf.h> 44 #include <sys/domain.h> 45 #include <sys/protosw.h> 46 #include <sys/socket.h> 47 #include <sys/socketvar.h> 48 #include <sys/proc.h> 49 #include <sys/jail.h> 50 #include <sys/kernel.h> 51 #include <sys/sysctl.h> 52 53 #include <machine/limits.h> 54 55 #include <vm/vm_zone.h> 56 57 #include <net/if.h> 58 #include <net/if_types.h> 59 #include <net/route.h> 60 61 #include <netinet/in.h> 62 #include <netinet/in_pcb.h> 63 #include <netinet/in_var.h> 64 #include <netinet/ip_var.h> 65 #ifdef INET6 66 #include <netinet/ip6.h> 67 #include <netinet6/ip6_var.h> 68 #endif /* INET6 */ 69 70 #include "faith.h" 71 72 #ifdef IPSEC 73 #include <netinet6/ipsec.h> 74 #include <netkey/key.h> 75 #endif /* IPSEC */ 76 77 struct in_addr zeroin_addr; 78 79 /* 80 * These configure the range of local port addresses assigned to 81 * "unspecified" outgoing connections/packets/whatever. 82 */ 83 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */ 84 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */ 85 int ipport_firstauto = IPPORT_RESERVED; /* 1024 */ 86 int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */ 87 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */ 88 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */ 89 90 #define RANGECHK(var, min, max) \ 91 if ((var) < (min)) { (var) = (min); } \ 92 else if ((var) > (max)) { (var) = (max); } 93 94 static int 95 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 96 { 97 int error = sysctl_handle_int(oidp, 98 oidp->oid_arg1, oidp->oid_arg2, req); 99 if (!error) { 100 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 101 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 102 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX); 103 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX); 104 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX); 105 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX); 106 } 107 return error; 108 } 109 110 #undef RANGECHK 111 112 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports"); 113 114 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW, 115 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", ""); 116 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW, 117 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", ""); 118 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW, 119 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", ""); 120 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW, 121 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", ""); 122 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW, 123 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", ""); 124 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW, 125 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", ""); 126 127 /* 128 * in_pcb.c: manage the Protocol Control Blocks. 129 * 130 * NOTE: It is assumed that most of these functions will be called at 131 * splnet(). XXX - There are, unfortunately, a few exceptions to this 132 * rule that should be fixed. 133 */ 134 135 /* 136 * Allocate a PCB and associate it with the socket. 137 */ 138 int 139 in_pcballoc(so, pcbinfo, p) 140 struct socket *so; 141 struct inpcbinfo *pcbinfo; 142 struct proc *p; 143 { 144 register struct inpcb *inp; 145 #ifdef IPSEC 146 int error; 147 #endif 148 149 inp = zalloc(pcbinfo->ipi_zone); 150 if (inp == NULL) 151 return (ENOBUFS); 152 bzero((caddr_t)inp, sizeof(*inp)); 153 inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 154 inp->inp_pcbinfo = pcbinfo; 155 inp->inp_socket = so; 156 #ifdef IPSEC 157 error = ipsec_init_policy(so, &inp->inp_sp); 158 if (error != 0) { 159 zfree(pcbinfo->ipi_zone, inp); 160 return error; 161 } 162 #endif /*IPSEC*/ 163 #if defined(INET6) 164 if (INP_SOCKAF(so) == AF_INET6 && !ip6_mapped_addr_on) 165 inp->inp_flags |= IN6P_IPV6_V6ONLY; 166 #endif 167 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list); 168 pcbinfo->ipi_count++; 169 so->so_pcb = (caddr_t)inp; 170 #ifdef INET6 171 if (ip6_auto_flowlabel) 172 inp->inp_flags |= IN6P_AUTOFLOWLABEL; 173 #endif 174 return (0); 175 } 176 177 int 178 in_pcbbind(inp, nam, p) 179 register struct inpcb *inp; 180 struct sockaddr *nam; 181 struct proc *p; 182 { 183 register struct socket *so = inp->inp_socket; 184 unsigned short *lastport; 185 struct sockaddr_in *sin; 186 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 187 u_short lport = 0; 188 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 189 int error, prison = 0; 190 191 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */ 192 return (EADDRNOTAVAIL); 193 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY) 194 return (EINVAL); 195 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 196 wild = 1; 197 if (nam) { 198 sin = (struct sockaddr_in *)nam; 199 if (nam->sa_len != sizeof (*sin)) 200 return (EINVAL); 201 #ifdef notdef 202 /* 203 * We should check the family, but old programs 204 * incorrectly fail to initialize it. 205 */ 206 if (sin->sin_family != AF_INET) 207 return (EAFNOSUPPORT); 208 #endif 209 if (sin->sin_addr.s_addr != INADDR_ANY) 210 if (prison_ip(p->p_ucred, 0, &sin->sin_addr.s_addr)) 211 return(EINVAL); 212 lport = sin->sin_port; 213 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 214 /* 215 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 216 * allow complete duplication of binding if 217 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 218 * and a multicast address is bound on both 219 * new and duplicated sockets. 220 */ 221 if (so->so_options & SO_REUSEADDR) 222 reuseport = SO_REUSEADDR|SO_REUSEPORT; 223 } else if (sin->sin_addr.s_addr != INADDR_ANY) { 224 sin->sin_port = 0; /* yech... */ 225 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0) 226 return (EADDRNOTAVAIL); 227 } 228 if (lport) { 229 struct inpcb *t; 230 /* GROSS */ 231 if (ntohs(lport) < IPPORT_RESERVED && p && 232 suser_xxx(0, p, PRISON_ROOT)) 233 return (EACCES); 234 if (p && jailed(p->p_ucred)) 235 prison = 1; 236 if (so->so_cred->cr_uid != 0 && 237 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 238 t = in_pcblookup_local(inp->inp_pcbinfo, 239 sin->sin_addr, lport, 240 prison ? 0 : INPLOOKUP_WILDCARD); 241 if (t && 242 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 243 ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 244 (t->inp_socket->so_options & 245 SO_REUSEPORT) == 0) && 246 (so->so_cred->cr_uid != 247 t->inp_socket->so_cred->cr_uid)) { 248 #if defined(INET6) 249 if (ntohl(sin->sin_addr.s_addr) != 250 INADDR_ANY || 251 ntohl(t->inp_laddr.s_addr) != 252 INADDR_ANY || 253 INP_SOCKAF(so) == 254 INP_SOCKAF(t->inp_socket)) 255 #endif /* defined(INET6) */ 256 return (EADDRINUSE); 257 } 258 } 259 if (prison && 260 prison_ip(p->p_ucred, 0, &sin->sin_addr.s_addr)) 261 return (EADDRNOTAVAIL); 262 t = in_pcblookup_local(pcbinfo, sin->sin_addr, 263 lport, prison ? 0 : wild); 264 if (t && 265 (reuseport & t->inp_socket->so_options) == 0) { 266 #if defined(INET6) 267 if (ntohl(sin->sin_addr.s_addr) != 268 INADDR_ANY || 269 ntohl(t->inp_laddr.s_addr) != 270 INADDR_ANY || 271 INP_SOCKAF(so) == 272 INP_SOCKAF(t->inp_socket)) 273 #endif /* defined(INET6) */ 274 return (EADDRINUSE); 275 } 276 } 277 inp->inp_laddr = sin->sin_addr; 278 } 279 if (lport == 0) { 280 ushort first, last; 281 int count; 282 283 if (inp->inp_laddr.s_addr != INADDR_ANY) 284 if (prison_ip(p->p_ucred, 0, &inp->inp_laddr.s_addr )) { 285 inp->inp_laddr.s_addr = INADDR_ANY; 286 return (EINVAL); 287 } 288 inp->inp_flags |= INP_ANONPORT; 289 290 if (inp->inp_flags & INP_HIGHPORT) { 291 first = ipport_hifirstauto; /* sysctl */ 292 last = ipport_hilastauto; 293 lastport = &pcbinfo->lasthi; 294 } else if (inp->inp_flags & INP_LOWPORT) { 295 if (p && (error = suser_xxx(0, p, PRISON_ROOT))) { 296 inp->inp_laddr.s_addr = INADDR_ANY; 297 return error; 298 } 299 first = ipport_lowfirstauto; /* 1023 */ 300 last = ipport_lowlastauto; /* 600 */ 301 lastport = &pcbinfo->lastlow; 302 } else { 303 first = ipport_firstauto; /* sysctl */ 304 last = ipport_lastauto; 305 lastport = &pcbinfo->lastport; 306 } 307 /* 308 * Simple check to ensure all ports are not used up causing 309 * a deadlock here. 310 * 311 * We split the two cases (up and down) so that the direction 312 * is not being tested on each round of the loop. 313 */ 314 if (first > last) { 315 /* 316 * counting down 317 */ 318 count = first - last; 319 320 do { 321 if (count-- < 0) { /* completely used? */ 322 inp->inp_laddr.s_addr = INADDR_ANY; 323 return (EADDRNOTAVAIL); 324 } 325 --*lastport; 326 if (*lastport > first || *lastport < last) 327 *lastport = first; 328 lport = htons(*lastport); 329 } while (in_pcblookup_local(pcbinfo, 330 inp->inp_laddr, lport, wild)); 331 } else { 332 /* 333 * counting up 334 */ 335 count = last - first; 336 337 do { 338 if (count-- < 0) { /* completely used? */ 339 /* 340 * Undo any address bind that may have 341 * occurred above. 342 */ 343 inp->inp_laddr.s_addr = INADDR_ANY; 344 return (EADDRNOTAVAIL); 345 } 346 ++*lastport; 347 if (*lastport < first || *lastport > last) 348 *lastport = first; 349 lport = htons(*lastport); 350 } while (in_pcblookup_local(pcbinfo, 351 inp->inp_laddr, lport, wild)); 352 } 353 } 354 inp->inp_lport = lport; 355 if (prison_ip(p->p_ucred, 0, &inp->inp_laddr.s_addr)) { 356 inp->inp_laddr.s_addr = INADDR_ANY; 357 inp->inp_lport = 0; 358 return(EINVAL); 359 } 360 if (in_pcbinshash(inp) != 0) { 361 inp->inp_laddr.s_addr = INADDR_ANY; 362 inp->inp_lport = 0; 363 return (EAGAIN); 364 } 365 return (0); 366 } 367 368 /* 369 * Transform old in_pcbconnect() into an inner subroutine for new 370 * in_pcbconnect(): Do some validity-checking on the remote 371 * address (in mbuf 'nam') and then determine local host address 372 * (i.e., which interface) to use to access that remote host. 373 * 374 * This preserves definition of in_pcbconnect(), while supporting a 375 * slightly different version for T/TCP. (This is more than 376 * a bit of a kludge, but cleaning up the internal interfaces would 377 * have forced minor changes in every protocol). 378 */ 379 380 int 381 in_pcbladdr(inp, nam, plocal_sin) 382 register struct inpcb *inp; 383 struct sockaddr *nam; 384 struct sockaddr_in **plocal_sin; 385 { 386 struct in_ifaddr *ia; 387 register struct sockaddr_in *sin = (struct sockaddr_in *)nam; 388 389 if (nam->sa_len != sizeof (*sin)) 390 return (EINVAL); 391 if (sin->sin_family != AF_INET) 392 return (EAFNOSUPPORT); 393 if (sin->sin_port == 0) 394 return (EADDRNOTAVAIL); 395 if (!TAILQ_EMPTY(&in_ifaddrhead)) { 396 /* 397 * If the destination address is INADDR_ANY, 398 * use the primary local address. 399 * If the supplied address is INADDR_BROADCAST, 400 * and the primary interface supports broadcast, 401 * choose the broadcast address for that interface. 402 */ 403 #define satosin(sa) ((struct sockaddr_in *)(sa)) 404 #define sintosa(sin) ((struct sockaddr *)(sin)) 405 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa)) 406 if (sin->sin_addr.s_addr == INADDR_ANY) 407 sin->sin_addr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr; 408 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST && 409 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & IFF_BROADCAST)) 410 sin->sin_addr = satosin(&TAILQ_FIRST(&in_ifaddrhead)->ia_broadaddr)->sin_addr; 411 } 412 if (inp->inp_laddr.s_addr == INADDR_ANY) { 413 register struct route *ro; 414 415 ia = (struct in_ifaddr *)0; 416 /* 417 * If route is known or can be allocated now, 418 * our src addr is taken from the i/f, else punt. 419 */ 420 ro = &inp->inp_route; 421 if (ro->ro_rt && 422 (satosin(&ro->ro_dst)->sin_addr.s_addr != 423 sin->sin_addr.s_addr || 424 inp->inp_socket->so_options & SO_DONTROUTE)) { 425 RTFREE(ro->ro_rt); 426 ro->ro_rt = (struct rtentry *)0; 427 } 428 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/ 429 (ro->ro_rt == (struct rtentry *)0 || 430 ro->ro_rt->rt_ifp == (struct ifnet *)0)) { 431 /* No route yet, so try to acquire one */ 432 ro->ro_dst.sa_family = AF_INET; 433 ro->ro_dst.sa_len = sizeof(struct sockaddr_in); 434 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 435 sin->sin_addr; 436 rtalloc(ro); 437 } 438 /* 439 * If we found a route, use the address 440 * corresponding to the outgoing interface 441 * unless it is the loopback (in case a route 442 * to our address on another net goes to loopback). 443 */ 444 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) 445 ia = ifatoia(ro->ro_rt->rt_ifa); 446 if (ia == 0) { 447 u_short fport = sin->sin_port; 448 449 sin->sin_port = 0; 450 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin))); 451 if (ia == 0) 452 ia = ifatoia(ifa_ifwithnet(sintosa(sin))); 453 sin->sin_port = fport; 454 if (ia == 0) 455 ia = TAILQ_FIRST(&in_ifaddrhead); 456 if (ia == 0) 457 return (EADDRNOTAVAIL); 458 } 459 /* 460 * If the destination address is multicast and an outgoing 461 * interface has been set as a multicast option, use the 462 * address of that interface as our source address. 463 */ 464 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 465 inp->inp_moptions != NULL) { 466 struct ip_moptions *imo; 467 struct ifnet *ifp; 468 469 imo = inp->inp_moptions; 470 if (imo->imo_multicast_ifp != NULL) { 471 ifp = imo->imo_multicast_ifp; 472 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) 473 if (ia->ia_ifp == ifp) 474 break; 475 if (ia == 0) 476 return (EADDRNOTAVAIL); 477 } 478 } 479 /* 480 * Don't do pcblookup call here; return interface in plocal_sin 481 * and exit to caller, that will do the lookup. 482 */ 483 *plocal_sin = &ia->ia_addr; 484 485 } 486 return(0); 487 } 488 489 /* 490 * Outer subroutine: 491 * Connect from a socket to a specified address. 492 * Both address and port must be specified in argument sin. 493 * If don't have a local address for this socket yet, 494 * then pick one. 495 */ 496 int 497 in_pcbconnect(inp, nam, p) 498 register struct inpcb *inp; 499 struct sockaddr *nam; 500 struct proc *p; 501 { 502 struct sockaddr_in *ifaddr; 503 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 504 struct sockaddr_in sa; 505 struct ucred *cred; 506 int error; 507 508 cred = inp->inp_socket->so_cred; 509 if (inp->inp_laddr.s_addr == INADDR_ANY && jailed(cred)) { 510 bzero(&sa, sizeof (sa)); 511 sa.sin_addr.s_addr = htonl(cred->cr_prison->pr_ip); 512 sa.sin_len=sizeof (sa); 513 sa.sin_family = AF_INET; 514 error = in_pcbbind(inp, (struct sockaddr *)&sa, p); 515 if (error) 516 return (error); 517 } 518 /* 519 * Call inner routine, to assign local interface address. 520 */ 521 if ((error = in_pcbladdr(inp, nam, &ifaddr)) != 0) 522 return(error); 523 524 if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port, 525 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr, 526 inp->inp_lport, 0, NULL) != NULL) { 527 return (EADDRINUSE); 528 } 529 if (inp->inp_laddr.s_addr == INADDR_ANY) { 530 if (inp->inp_lport == 0) { 531 error = in_pcbbind(inp, (struct sockaddr *)0, p); 532 if (error) 533 return (error); 534 } 535 inp->inp_laddr = ifaddr->sin_addr; 536 } 537 inp->inp_faddr = sin->sin_addr; 538 inp->inp_fport = sin->sin_port; 539 in_pcbrehash(inp); 540 return (0); 541 } 542 543 void 544 in_pcbdisconnect(inp) 545 struct inpcb *inp; 546 { 547 548 inp->inp_faddr.s_addr = INADDR_ANY; 549 inp->inp_fport = 0; 550 in_pcbrehash(inp); 551 if (inp->inp_socket->so_state & SS_NOFDREF) 552 in_pcbdetach(inp); 553 } 554 555 void 556 in_pcbdetach(inp) 557 struct inpcb *inp; 558 { 559 struct socket *so = inp->inp_socket; 560 struct inpcbinfo *ipi = inp->inp_pcbinfo; 561 struct rtentry *rt = inp->inp_route.ro_rt; 562 563 #ifdef IPSEC 564 ipsec4_delete_pcbpolicy(inp); 565 #endif /*IPSEC*/ 566 inp->inp_gencnt = ++ipi->ipi_gencnt; 567 in_pcbremlists(inp); 568 so->so_pcb = 0; 569 sofree(so); 570 if (inp->inp_options) 571 (void)m_free(inp->inp_options); 572 if (rt) { 573 /* 574 * route deletion requires reference count to be <= zero 575 */ 576 if ((rt->rt_flags & RTF_DELCLONE) && 577 (rt->rt_flags & RTF_WASCLONED) && 578 (rt->rt_refcnt <= 1)) { 579 rt->rt_refcnt--; 580 rt->rt_flags &= ~RTF_UP; 581 rtrequest(RTM_DELETE, rt_key(rt), 582 rt->rt_gateway, rt_mask(rt), 583 rt->rt_flags, (struct rtentry **)0); 584 } 585 else 586 rtfree(rt); 587 } 588 ip_freemoptions(inp->inp_moptions); 589 inp->inp_vflag = 0; 590 zfree(ipi->ipi_zone, inp); 591 } 592 593 /* 594 * The calling convention of in_setsockaddr() and in_setpeeraddr() was 595 * modified to match the pru_sockaddr() and pru_peeraddr() entry points 596 * in struct pr_usrreqs, so that protocols can just reference then directly 597 * without the need for a wrapper function. The socket must have a valid 598 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one 599 * except through a kernel programming error, so it is acceptable to panic 600 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap 601 * because there actually /is/ a programming error somewhere... XXX) 602 */ 603 int 604 in_setsockaddr(so, nam) 605 struct socket *so; 606 struct sockaddr **nam; 607 { 608 int s; 609 register struct inpcb *inp; 610 register struct sockaddr_in *sin; 611 612 /* 613 * Do the malloc first in case it blocks. 614 */ 615 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 616 M_WAITOK | M_ZERO); 617 sin->sin_family = AF_INET; 618 sin->sin_len = sizeof(*sin); 619 620 s = splnet(); 621 inp = sotoinpcb(so); 622 if (!inp) { 623 splx(s); 624 free(sin, M_SONAME); 625 return ECONNRESET; 626 } 627 sin->sin_port = inp->inp_lport; 628 sin->sin_addr = inp->inp_laddr; 629 splx(s); 630 631 *nam = (struct sockaddr *)sin; 632 return 0; 633 } 634 635 int 636 in_setpeeraddr(so, nam) 637 struct socket *so; 638 struct sockaddr **nam; 639 { 640 int s; 641 struct inpcb *inp; 642 register struct sockaddr_in *sin; 643 644 /* 645 * Do the malloc first in case it blocks. 646 */ 647 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 648 M_WAITOK | M_ZERO); 649 sin->sin_family = AF_INET; 650 sin->sin_len = sizeof(*sin); 651 652 s = splnet(); 653 inp = sotoinpcb(so); 654 if (!inp) { 655 splx(s); 656 free(sin, M_SONAME); 657 return ECONNRESET; 658 } 659 sin->sin_port = inp->inp_fport; 660 sin->sin_addr = inp->inp_faddr; 661 splx(s); 662 663 *nam = (struct sockaddr *)sin; 664 return 0; 665 } 666 667 void 668 in_pcbnotifyall(head, faddr, errno, notify) 669 struct inpcbhead *head; 670 struct in_addr faddr; 671 int errno; 672 void (*notify) __P((struct inpcb *, int)); 673 { 674 struct inpcb *inp, *ninp; 675 int s; 676 677 s = splnet(); 678 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) { 679 ninp = LIST_NEXT(inp, inp_list); 680 #ifdef INET6 681 if ((inp->inp_vflag & INP_IPV4) == 0) 682 continue; 683 #endif 684 if (inp->inp_faddr.s_addr != faddr.s_addr || 685 inp->inp_socket == NULL) 686 continue; 687 (*notify)(inp, errno); 688 } 689 splx(s); 690 } 691 692 void 693 in_pcbpurgeif0(head, ifp) 694 struct inpcb *head; 695 struct ifnet *ifp; 696 { 697 struct inpcb *inp; 698 struct ip_moptions *imo; 699 int i, gap; 700 701 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) { 702 imo = inp->inp_moptions; 703 if ((inp->inp_vflag & INP_IPV4) && 704 imo != NULL) { 705 /* 706 * Unselect the outgoing interface if it is being 707 * detached. 708 */ 709 if (imo->imo_multicast_ifp == ifp) 710 imo->imo_multicast_ifp = NULL; 711 712 /* 713 * Drop multicast group membership if we joined 714 * through the interface being detached. 715 */ 716 for (i = 0, gap = 0; i < imo->imo_num_memberships; 717 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] = 723 imo->imo_membership[i]; 724 } 725 imo->imo_num_memberships -= gap; 726 } 727 } 728 } 729 730 /* 731 * Check for alternatives when higher level complains 732 * about service problems. For now, invalidate cached 733 * routing information. If the route was created dynamically 734 * (by a redirect), time to try a default gateway again. 735 */ 736 void 737 in_losing(inp) 738 struct inpcb *inp; 739 { 740 register struct rtentry *rt; 741 struct rt_addrinfo info; 742 743 if ((rt = inp->inp_route.ro_rt)) { 744 bzero((caddr_t)&info, sizeof(info)); 745 info.rti_info[RTAX_DST] = 746 (struct sockaddr *)&inp->inp_route.ro_dst; 747 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 748 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 749 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 750 if (rt->rt_flags & RTF_DYNAMIC) 751 (void) rtrequest(RTM_DELETE, rt_key(rt), 752 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 753 (struct rtentry **)0); 754 inp->inp_route.ro_rt = 0; 755 rtfree(rt); 756 /* 757 * A new route can be allocated 758 * the next time output is attempted. 759 */ 760 } 761 } 762 763 /* 764 * After a routing change, flush old routing 765 * and allocate a (hopefully) better one. 766 */ 767 void 768 in_rtchange(inp, errno) 769 register struct inpcb *inp; 770 int errno; 771 { 772 if (inp->inp_route.ro_rt) { 773 rtfree(inp->inp_route.ro_rt); 774 inp->inp_route.ro_rt = 0; 775 /* 776 * A new route can be allocated the next time 777 * output is attempted. 778 */ 779 } 780 } 781 782 /* 783 * Lookup a PCB based on the local address and port. 784 */ 785 struct inpcb * 786 in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay) 787 struct inpcbinfo *pcbinfo; 788 struct in_addr laddr; 789 u_int lport_arg; 790 int wild_okay; 791 { 792 register struct inpcb *inp; 793 int matchwild = 3, wildcard; 794 u_short lport = lport_arg; 795 796 if (!wild_okay) { 797 struct inpcbhead *head; 798 /* 799 * Look for an unconnected (wildcard foreign addr) PCB that 800 * matches the local address and port we're looking for. 801 */ 802 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)]; 803 LIST_FOREACH(inp, head, inp_hash) { 804 #ifdef INET6 805 if ((inp->inp_vflag & INP_IPV4) == 0) 806 continue; 807 #endif 808 if (inp->inp_faddr.s_addr == INADDR_ANY && 809 inp->inp_laddr.s_addr == laddr.s_addr && 810 inp->inp_lport == lport) { 811 /* 812 * Found. 813 */ 814 return (inp); 815 } 816 } 817 /* 818 * Not found. 819 */ 820 return (NULL); 821 } else { 822 struct inpcbporthead *porthash; 823 struct inpcbport *phd; 824 struct inpcb *match = NULL; 825 /* 826 * Best fit PCB lookup. 827 * 828 * First see if this local port is in use by looking on the 829 * port hash list. 830 */ 831 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport, 832 pcbinfo->porthashmask)]; 833 LIST_FOREACH(phd, porthash, phd_hash) { 834 if (phd->phd_port == lport) 835 break; 836 } 837 if (phd != NULL) { 838 /* 839 * Port is in use by one or more PCBs. Look for best 840 * fit. 841 */ 842 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 843 wildcard = 0; 844 #ifdef INET6 845 if ((inp->inp_vflag & INP_IPV4) == 0) 846 continue; 847 #endif 848 if (inp->inp_faddr.s_addr != INADDR_ANY) 849 wildcard++; 850 if (inp->inp_laddr.s_addr != INADDR_ANY) { 851 if (laddr.s_addr == INADDR_ANY) 852 wildcard++; 853 else if (inp->inp_laddr.s_addr != laddr.s_addr) 854 continue; 855 } else { 856 if (laddr.s_addr != INADDR_ANY) 857 wildcard++; 858 } 859 if (wildcard < matchwild) { 860 match = inp; 861 matchwild = wildcard; 862 if (matchwild == 0) { 863 break; 864 } 865 } 866 } 867 } 868 return (match); 869 } 870 } 871 872 /* 873 * Lookup PCB in hash list. 874 */ 875 struct inpcb * 876 in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, 877 ifp) 878 struct inpcbinfo *pcbinfo; 879 struct in_addr faddr, laddr; 880 u_int fport_arg, lport_arg; 881 int wildcard; 882 struct ifnet *ifp; 883 { 884 struct inpcbhead *head; 885 register struct inpcb *inp; 886 u_short fport = fport_arg, lport = lport_arg; 887 888 /* 889 * First look for an exact match. 890 */ 891 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)]; 892 LIST_FOREACH(inp, head, inp_hash) { 893 #ifdef INET6 894 if ((inp->inp_vflag & INP_IPV4) == 0) 895 continue; 896 #endif 897 if (inp->inp_faddr.s_addr == faddr.s_addr && 898 inp->inp_laddr.s_addr == laddr.s_addr && 899 inp->inp_fport == fport && 900 inp->inp_lport == lport) { 901 /* 902 * Found. 903 */ 904 return (inp); 905 } 906 } 907 if (wildcard) { 908 struct inpcb *local_wild = NULL; 909 #if defined(INET6) 910 struct inpcb *local_wild_mapped = NULL; 911 #endif /* defined(INET6) */ 912 913 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)]; 914 LIST_FOREACH(inp, head, inp_hash) { 915 #ifdef INET6 916 if ((inp->inp_vflag & INP_IPV4) == 0) 917 continue; 918 #endif 919 if (inp->inp_faddr.s_addr == INADDR_ANY && 920 inp->inp_lport == lport) { 921 #if defined(NFAITH) && NFAITH > 0 922 if (ifp && ifp->if_type == IFT_FAITH && 923 (inp->inp_flags & INP_FAITH) == 0) 924 continue; 925 #endif 926 if (inp->inp_laddr.s_addr == laddr.s_addr) 927 return (inp); 928 else if (inp->inp_laddr.s_addr == INADDR_ANY) { 929 #if defined(INET6) 930 if (INP_CHECK_SOCKAF(inp->inp_socket, 931 AF_INET6)) 932 local_wild_mapped = inp; 933 else 934 #endif /* defined(INET6) */ 935 local_wild = inp; 936 } 937 } 938 } 939 #if defined(INET6) 940 if (local_wild == NULL) 941 return (local_wild_mapped); 942 #endif /* defined(INET6) */ 943 return (local_wild); 944 } 945 946 /* 947 * Not found. 948 */ 949 return (NULL); 950 } 951 952 /* 953 * Insert PCB onto various hash lists. 954 */ 955 int 956 in_pcbinshash(inp) 957 struct inpcb *inp; 958 { 959 struct inpcbhead *pcbhash; 960 struct inpcbporthead *pcbporthash; 961 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 962 struct inpcbport *phd; 963 u_int32_t hashkey_faddr; 964 965 #ifdef INET6 966 if (inp->inp_vflag & INP_IPV6) 967 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 968 else 969 #endif /* INET6 */ 970 hashkey_faddr = inp->inp_faddr.s_addr; 971 972 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr, 973 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)]; 974 975 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport, 976 pcbinfo->porthashmask)]; 977 978 /* 979 * Go through port list and look for a head for this lport. 980 */ 981 LIST_FOREACH(phd, pcbporthash, phd_hash) { 982 if (phd->phd_port == inp->inp_lport) 983 break; 984 } 985 /* 986 * If none exists, malloc one and tack it on. 987 */ 988 if (phd == NULL) { 989 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT); 990 if (phd == NULL) { 991 return (ENOBUFS); /* XXX */ 992 } 993 phd->phd_port = inp->inp_lport; 994 LIST_INIT(&phd->phd_pcblist); 995 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 996 } 997 inp->inp_phd = phd; 998 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 999 LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 1000 return (0); 1001 } 1002 1003 /* 1004 * Move PCB to the proper hash bucket when { faddr, fport } have been 1005 * changed. NOTE: This does not handle the case of the lport changing (the 1006 * hashed port list would have to be updated as well), so the lport must 1007 * not change after in_pcbinshash() has been called. 1008 */ 1009 void 1010 in_pcbrehash(inp) 1011 struct inpcb *inp; 1012 { 1013 struct inpcbhead *head; 1014 u_int32_t hashkey_faddr; 1015 1016 #ifdef INET6 1017 if (inp->inp_vflag & INP_IPV6) 1018 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1019 else 1020 #endif /* INET6 */ 1021 hashkey_faddr = inp->inp_faddr.s_addr; 1022 1023 head = &inp->inp_pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr, 1024 inp->inp_lport, inp->inp_fport, inp->inp_pcbinfo->hashmask)]; 1025 1026 LIST_REMOVE(inp, inp_hash); 1027 LIST_INSERT_HEAD(head, inp, inp_hash); 1028 } 1029 1030 /* 1031 * Remove PCB from various lists. 1032 */ 1033 void 1034 in_pcbremlists(inp) 1035 struct inpcb *inp; 1036 { 1037 inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt; 1038 if (inp->inp_lport) { 1039 struct inpcbport *phd = inp->inp_phd; 1040 1041 LIST_REMOVE(inp, inp_hash); 1042 LIST_REMOVE(inp, inp_portlist); 1043 if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 1044 LIST_REMOVE(phd, phd_hash); 1045 free(phd, M_PCB); 1046 } 1047 } 1048 LIST_REMOVE(inp, inp_list); 1049 inp->inp_pcbinfo->ipi_count--; 1050 } 1051 1052 int 1053 prison_xinpcb(struct proc *p, struct inpcb *inp) 1054 { 1055 if (!jailed(p->p_ucred)) 1056 return (0); 1057 if (ntohl(inp->inp_laddr.s_addr) == p->p_ucred->cr_prison->pr_ip) 1058 return (0); 1059 return (1); 1060 } 1061