1 /*- 2 * Copyright (c) 1982, 1986, 1988, 1993 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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95 30 * $FreeBSD$ 31 */ 32 33 #include "opt_inet6.h" 34 #include "opt_ipsec.h" 35 #include "opt_mac.h" 36 37 #include <sys/param.h> 38 #include <sys/jail.h> 39 #include <sys/kernel.h> 40 #include <sys/lock.h> 41 #include <sys/malloc.h> 42 #include <sys/mbuf.h> 43 #include <sys/priv.h> 44 #include <sys/proc.h> 45 #include <sys/protosw.h> 46 #include <sys/signalvar.h> 47 #include <sys/socket.h> 48 #include <sys/socketvar.h> 49 #include <sys/sx.h> 50 #include <sys/sysctl.h> 51 #include <sys/systm.h> 52 53 #include <vm/uma.h> 54 55 #include <net/if.h> 56 #include <net/route.h> 57 58 #include <netinet/in.h> 59 #include <netinet/in_systm.h> 60 #include <netinet/in_pcb.h> 61 #include <netinet/in_var.h> 62 #include <netinet/ip.h> 63 #include <netinet/ip_var.h> 64 #include <netinet/ip_mroute.h> 65 66 #include <netinet/ip_fw.h> 67 #include <netinet/ip_dummynet.h> 68 69 #ifdef FAST_IPSEC 70 #include <netipsec/ipsec.h> 71 #endif /*FAST_IPSEC*/ 72 73 #ifdef IPSEC 74 #include <netinet6/ipsec.h> 75 #endif /*IPSEC*/ 76 77 #include <security/mac/mac_framework.h> 78 79 struct inpcbhead ripcb; 80 struct inpcbinfo ripcbinfo; 81 82 /* control hooks for ipfw and dummynet */ 83 ip_fw_ctl_t *ip_fw_ctl_ptr = NULL; 84 ip_dn_ctl_t *ip_dn_ctl_ptr = NULL; 85 86 /* 87 * hooks for multicast routing. They all default to NULL, 88 * so leave them not initialized and rely on BSS being set to 0. 89 */ 90 91 /* The socket used to communicate with the multicast routing daemon. */ 92 struct socket *ip_mrouter; 93 94 /* The various mrouter and rsvp functions */ 95 int (*ip_mrouter_set)(struct socket *, struct sockopt *); 96 int (*ip_mrouter_get)(struct socket *, struct sockopt *); 97 int (*ip_mrouter_done)(void); 98 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *, 99 struct ip_moptions *); 100 int (*mrt_ioctl)(int, caddr_t); 101 int (*legal_vif_num)(int); 102 u_long (*ip_mcast_src)(int); 103 104 void (*rsvp_input_p)(struct mbuf *m, int off); 105 int (*ip_rsvp_vif)(struct socket *, struct sockopt *); 106 void (*ip_rsvp_force_done)(struct socket *); 107 108 /* 109 * Raw interface to IP protocol. 110 */ 111 112 /* 113 * Initialize raw connection block q. 114 */ 115 static void 116 rip_zone_change(void *tag) 117 { 118 119 uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets); 120 } 121 122 static int 123 rip_inpcb_init(void *mem, int size, int flags) 124 { 125 struct inpcb *inp = mem; 126 127 INP_LOCK_INIT(inp, "inp", "rawinp"); 128 return (0); 129 } 130 131 void 132 rip_init(void) 133 { 134 135 INP_INFO_LOCK_INIT(&ripcbinfo, "rip"); 136 LIST_INIT(&ripcb); 137 ripcbinfo.ipi_listhead = &ripcb; 138 /* 139 * XXX We don't use the hash list for raw IP, but it's easier 140 * to allocate a one entry hash list than it is to check all 141 * over the place for hashbase == NULL. 142 */ 143 ripcbinfo.ipi_hashbase = hashinit(1, M_PCB, &ripcbinfo.ipi_hashmask); 144 ripcbinfo.ipi_porthashbase = hashinit(1, M_PCB, 145 &ripcbinfo.ipi_porthashmask); 146 ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb), 147 NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 148 uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets); 149 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, 150 NULL, EVENTHANDLER_PRI_ANY); 151 } 152 153 static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; 154 155 static int 156 raw_append(struct inpcb *last, struct ip *ip, struct mbuf *n) 157 { 158 int policyfail = 0; 159 160 INP_LOCK_ASSERT(last); 161 162 #if defined(IPSEC) || defined(FAST_IPSEC) 163 /* check AH/ESP integrity. */ 164 if (ipsec4_in_reject(n, last)) { 165 policyfail = 1; 166 #ifdef IPSEC 167 ipsecstat.in_polvio++; 168 #endif /*IPSEC*/ 169 /* do not inject data to pcb */ 170 } 171 #endif /*IPSEC || FAST_IPSEC*/ 172 #ifdef MAC 173 if (!policyfail && mac_check_inpcb_deliver(last, n) != 0) 174 policyfail = 1; 175 #endif 176 /* Check the minimum TTL for socket. */ 177 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl) 178 policyfail = 1; 179 if (!policyfail) { 180 struct mbuf *opts = NULL; 181 struct socket *so; 182 183 so = last->inp_socket; 184 if ((last->inp_flags & INP_CONTROLOPTS) || 185 (so->so_options & (SO_TIMESTAMP | SO_BINTIME))) 186 ip_savecontrol(last, &opts, ip, n); 187 SOCKBUF_LOCK(&so->so_rcv); 188 if (sbappendaddr_locked(&so->so_rcv, 189 (struct sockaddr *)&ripsrc, n, opts) == 0) { 190 /* should notify about lost packet */ 191 m_freem(n); 192 if (opts) 193 m_freem(opts); 194 SOCKBUF_UNLOCK(&so->so_rcv); 195 } else 196 sorwakeup_locked(so); 197 } else 198 m_freem(n); 199 return policyfail; 200 } 201 202 /* 203 * Setup generic address and protocol structures 204 * for raw_input routine, then pass them along with 205 * mbuf chain. 206 */ 207 void 208 rip_input(struct mbuf *m, int off) 209 { 210 struct ip *ip = mtod(m, struct ip *); 211 int proto = ip->ip_p; 212 struct inpcb *inp, *last; 213 214 INP_INFO_RLOCK(&ripcbinfo); 215 ripsrc.sin_addr = ip->ip_src; 216 last = NULL; 217 LIST_FOREACH(inp, &ripcb, inp_list) { 218 INP_LOCK(inp); 219 if (inp->inp_ip_p && inp->inp_ip_p != proto) { 220 docontinue: 221 INP_UNLOCK(inp); 222 continue; 223 } 224 #ifdef INET6 225 if ((inp->inp_vflag & INP_IPV4) == 0) 226 goto docontinue; 227 #endif 228 if (inp->inp_laddr.s_addr && 229 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 230 goto docontinue; 231 if (inp->inp_faddr.s_addr && 232 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 233 goto docontinue; 234 if (jailed(inp->inp_socket->so_cred)) 235 if (htonl(prison_getip(inp->inp_socket->so_cred)) != 236 ip->ip_dst.s_addr) 237 goto docontinue; 238 if (last) { 239 struct mbuf *n; 240 241 n = m_copy(m, 0, (int)M_COPYALL); 242 if (n != NULL) 243 (void) raw_append(last, ip, n); 244 /* XXX count dropped packet */ 245 INP_UNLOCK(last); 246 } 247 last = inp; 248 } 249 if (last != NULL) { 250 if (raw_append(last, ip, m) != 0) 251 ipstat.ips_delivered--; 252 INP_UNLOCK(last); 253 } else { 254 m_freem(m); 255 ipstat.ips_noproto++; 256 ipstat.ips_delivered--; 257 } 258 INP_INFO_RUNLOCK(&ripcbinfo); 259 } 260 261 /* 262 * Generate IP header and pass packet to ip_output. 263 * Tack on options user may have setup with control call. 264 */ 265 int 266 rip_output(struct mbuf *m, struct socket *so, u_long dst) 267 { 268 struct ip *ip; 269 int error; 270 struct inpcb *inp = sotoinpcb(so); 271 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) | 272 IP_ALLOWBROADCAST; 273 274 /* 275 * If the user handed us a complete IP packet, use it. 276 * Otherwise, allocate an mbuf for a header and fill it in. 277 */ 278 if ((inp->inp_flags & INP_HDRINCL) == 0) { 279 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { 280 m_freem(m); 281 return(EMSGSIZE); 282 } 283 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); 284 if (m == NULL) 285 return(ENOBUFS); 286 287 INP_LOCK(inp); 288 ip = mtod(m, struct ip *); 289 ip->ip_tos = inp->inp_ip_tos; 290 if (inp->inp_flags & INP_DONTFRAG) 291 ip->ip_off = IP_DF; 292 else 293 ip->ip_off = 0; 294 ip->ip_p = inp->inp_ip_p; 295 ip->ip_len = m->m_pkthdr.len; 296 if (jailed(inp->inp_socket->so_cred)) 297 ip->ip_src.s_addr = 298 htonl(prison_getip(inp->inp_socket->so_cred)); 299 else 300 ip->ip_src = inp->inp_laddr; 301 ip->ip_dst.s_addr = dst; 302 ip->ip_ttl = inp->inp_ip_ttl; 303 } else { 304 if (m->m_pkthdr.len > IP_MAXPACKET) { 305 m_freem(m); 306 return(EMSGSIZE); 307 } 308 INP_LOCK(inp); 309 ip = mtod(m, struct ip *); 310 if (jailed(inp->inp_socket->so_cred)) { 311 if (ip->ip_src.s_addr != 312 htonl(prison_getip(inp->inp_socket->so_cred))) { 313 INP_UNLOCK(inp); 314 m_freem(m); 315 return (EPERM); 316 } 317 } 318 /* don't allow both user specified and setsockopt options, 319 and don't allow packet length sizes that will crash */ 320 if (((ip->ip_hl != (sizeof (*ip) >> 2)) 321 && inp->inp_options) 322 || (ip->ip_len > m->m_pkthdr.len) 323 || (ip->ip_len < (ip->ip_hl << 2))) { 324 INP_UNLOCK(inp); 325 m_freem(m); 326 return EINVAL; 327 } 328 if (ip->ip_id == 0) 329 ip->ip_id = ip_newid(); 330 /* XXX prevent ip_output from overwriting header fields */ 331 flags |= IP_RAWOUTPUT; 332 ipstat.ips_rawout++; 333 } 334 335 if (inp->inp_flags & INP_ONESBCAST) 336 flags |= IP_SENDONES; 337 338 #ifdef MAC 339 mac_create_mbuf_from_inpcb(inp, m); 340 #endif 341 342 error = ip_output(m, inp->inp_options, NULL, flags, 343 inp->inp_moptions, inp); 344 INP_UNLOCK(inp); 345 return error; 346 } 347 348 /* 349 * Raw IP socket option processing. 350 * 351 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could 352 * only be created by a privileged process, and as such, socket option 353 * operations to manage system properties on any raw socket were allowed to 354 * take place without explicit additional access control checks. However, 355 * raw sockets can now also be created in jail(), and therefore explicit 356 * checks are now required. Likewise, raw sockets can be used by a process 357 * after it gives up privilege, so some caution is required. For options 358 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be 359 * performed in ip_ctloutput() and therefore no check occurs here. 360 * Unilaterally checking suser() here breaks normal IP socket option 361 * operations on raw sockets. 362 * 363 * When adding new socket options here, make sure to add access control 364 * checks here as necessary. 365 */ 366 int 367 rip_ctloutput(struct socket *so, struct sockopt *sopt) 368 { 369 struct inpcb *inp = sotoinpcb(so); 370 int error, optval; 371 372 if (sopt->sopt_level != IPPROTO_IP) 373 return (EINVAL); 374 375 error = 0; 376 switch (sopt->sopt_dir) { 377 case SOPT_GET: 378 switch (sopt->sopt_name) { 379 case IP_HDRINCL: 380 optval = inp->inp_flags & INP_HDRINCL; 381 error = sooptcopyout(sopt, &optval, sizeof optval); 382 break; 383 384 case IP_FW_ADD: /* ADD actually returns the body... */ 385 case IP_FW_GET: 386 case IP_FW_TABLE_GETSIZE: 387 case IP_FW_TABLE_LIST: 388 case IP_FW_NAT_GET_CONFIG: 389 case IP_FW_NAT_GET_LOG: 390 /* 391 * XXXRW: Isn't this checked one layer down? Yes, it 392 * is. 393 */ 394 error = priv_check(curthread, PRIV_NETINET_IPFW); 395 if (error != 0) 396 return (error); 397 if (ip_fw_ctl_ptr != NULL) 398 error = ip_fw_ctl_ptr(sopt); 399 else 400 error = ENOPROTOOPT; 401 break; 402 403 case IP_DUMMYNET_GET: 404 error = priv_check(curthread, PRIV_NETINET_DUMMYNET); 405 if (error != 0) 406 return (error); 407 if (ip_dn_ctl_ptr != NULL) 408 error = ip_dn_ctl_ptr(sopt); 409 else 410 error = ENOPROTOOPT; 411 break ; 412 413 case MRT_INIT: 414 case MRT_DONE: 415 case MRT_ADD_VIF: 416 case MRT_DEL_VIF: 417 case MRT_ADD_MFC: 418 case MRT_DEL_MFC: 419 case MRT_VERSION: 420 case MRT_ASSERT: 421 case MRT_API_SUPPORT: 422 case MRT_API_CONFIG: 423 case MRT_ADD_BW_UPCALL: 424 case MRT_DEL_BW_UPCALL: 425 error = priv_check(curthread, PRIV_NETINET_MROUTE); 426 if (error != 0) 427 return (error); 428 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) : 429 EOPNOTSUPP; 430 break; 431 432 default: 433 error = ip_ctloutput(so, sopt); 434 break; 435 } 436 break; 437 438 case SOPT_SET: 439 switch (sopt->sopt_name) { 440 case IP_HDRINCL: 441 error = sooptcopyin(sopt, &optval, sizeof optval, 442 sizeof optval); 443 if (error) 444 break; 445 if (optval) 446 inp->inp_flags |= INP_HDRINCL; 447 else 448 inp->inp_flags &= ~INP_HDRINCL; 449 break; 450 451 case IP_FW_ADD: 452 case IP_FW_DEL: 453 case IP_FW_FLUSH: 454 case IP_FW_ZERO: 455 case IP_FW_RESETLOG: 456 case IP_FW_TABLE_ADD: 457 case IP_FW_TABLE_DEL: 458 case IP_FW_TABLE_FLUSH: 459 case IP_FW_NAT_CFG: 460 case IP_FW_NAT_DEL: 461 /* 462 * XXXRW: Isn't this checked one layer down? 463 */ 464 error = priv_check(curthread, PRIV_NETINET_IPFW); 465 if (error != 0) 466 return (error); 467 if (ip_fw_ctl_ptr != NULL) 468 error = ip_fw_ctl_ptr(sopt); 469 else 470 error = ENOPROTOOPT; 471 break; 472 473 case IP_DUMMYNET_CONFIGURE: 474 case IP_DUMMYNET_DEL: 475 case IP_DUMMYNET_FLUSH: 476 error = priv_check(curthread, PRIV_NETINET_DUMMYNET); 477 if (error != 0) 478 return (error); 479 if (ip_dn_ctl_ptr != NULL) 480 error = ip_dn_ctl_ptr(sopt); 481 else 482 error = ENOPROTOOPT ; 483 break ; 484 485 case IP_RSVP_ON: 486 error = priv_check(curthread, PRIV_NETINET_MROUTE); 487 if (error != 0) 488 return (error); 489 error = ip_rsvp_init(so); 490 break; 491 492 case IP_RSVP_OFF: 493 error = priv_check(curthread, PRIV_NETINET_MROUTE); 494 if (error != 0) 495 return (error); 496 error = ip_rsvp_done(); 497 break; 498 499 case IP_RSVP_VIF_ON: 500 case IP_RSVP_VIF_OFF: 501 error = priv_check(curthread, PRIV_NETINET_MROUTE); 502 if (error != 0) 503 return (error); 504 error = ip_rsvp_vif ? 505 ip_rsvp_vif(so, sopt) : EINVAL; 506 break; 507 508 case MRT_INIT: 509 case MRT_DONE: 510 case MRT_ADD_VIF: 511 case MRT_DEL_VIF: 512 case MRT_ADD_MFC: 513 case MRT_DEL_MFC: 514 case MRT_VERSION: 515 case MRT_ASSERT: 516 case MRT_API_SUPPORT: 517 case MRT_API_CONFIG: 518 case MRT_ADD_BW_UPCALL: 519 case MRT_DEL_BW_UPCALL: 520 error = priv_check(curthread, PRIV_NETINET_MROUTE); 521 if (error != 0) 522 return (error); 523 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) : 524 EOPNOTSUPP; 525 break; 526 527 default: 528 error = ip_ctloutput(so, sopt); 529 break; 530 } 531 break; 532 } 533 534 return (error); 535 } 536 537 /* 538 * This function exists solely to receive the PRC_IFDOWN messages which 539 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, 540 * and calls in_ifadown() to remove all routes corresponding to that address. 541 * It also receives the PRC_IFUP messages from if_up() and reinstalls the 542 * interface routes. 543 */ 544 void 545 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip) 546 { 547 struct in_ifaddr *ia; 548 struct ifnet *ifp; 549 int err; 550 int flags; 551 552 switch (cmd) { 553 case PRC_IFDOWN: 554 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 555 if (ia->ia_ifa.ifa_addr == sa 556 && (ia->ia_flags & IFA_ROUTE)) { 557 /* 558 * in_ifscrub kills the interface route. 559 */ 560 in_ifscrub(ia->ia_ifp, ia); 561 /* 562 * in_ifadown gets rid of all the rest of 563 * the routes. This is not quite the right 564 * thing to do, but at least if we are running 565 * a routing process they will come back. 566 */ 567 in_ifadown(&ia->ia_ifa, 0); 568 break; 569 } 570 } 571 break; 572 573 case PRC_IFUP: 574 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 575 if (ia->ia_ifa.ifa_addr == sa) 576 break; 577 } 578 if (ia == 0 || (ia->ia_flags & IFA_ROUTE)) 579 return; 580 flags = RTF_UP; 581 ifp = ia->ia_ifa.ifa_ifp; 582 583 if ((ifp->if_flags & IFF_LOOPBACK) 584 || (ifp->if_flags & IFF_POINTOPOINT)) 585 flags |= RTF_HOST; 586 587 err = rtinit(&ia->ia_ifa, RTM_ADD, flags); 588 if (err == 0) 589 ia->ia_flags |= IFA_ROUTE; 590 break; 591 } 592 } 593 594 u_long rip_sendspace = 9216; 595 u_long rip_recvspace = 9216; 596 597 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, 598 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); 599 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, 600 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams"); 601 602 static int 603 rip_attach(struct socket *so, int proto, struct thread *td) 604 { 605 struct inpcb *inp; 606 int error; 607 608 inp = sotoinpcb(so); 609 KASSERT(inp == NULL, ("rip_attach: inp != NULL")); 610 /* 611 * XXXRW: Centralize privilege decision in kern_jail.c. 612 */ 613 if (jailed(td->td_ucred) && !jail_allow_raw_sockets) 614 return (EPERM); 615 error = priv_check_cred(td->td_ucred, PRIV_NETINET_RAW, 616 SUSER_ALLOWJAIL); 617 if (error) 618 return error; 619 if (proto >= IPPROTO_MAX || proto < 0) 620 return EPROTONOSUPPORT; 621 error = soreserve(so, rip_sendspace, rip_recvspace); 622 if (error) 623 return error; 624 INP_INFO_WLOCK(&ripcbinfo); 625 error = in_pcballoc(so, &ripcbinfo); 626 if (error) { 627 INP_INFO_WUNLOCK(&ripcbinfo); 628 return error; 629 } 630 inp = (struct inpcb *)so->so_pcb; 631 INP_INFO_WUNLOCK(&ripcbinfo); 632 inp->inp_vflag |= INP_IPV4; 633 inp->inp_ip_p = proto; 634 inp->inp_ip_ttl = ip_defttl; 635 INP_UNLOCK(inp); 636 return 0; 637 } 638 639 static void 640 rip_detach(struct socket *so) 641 { 642 struct inpcb *inp; 643 644 inp = sotoinpcb(so); 645 KASSERT(inp != NULL, ("rip_detach: inp == NULL")); 646 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 647 ("rip_detach: not closed")); 648 649 INP_INFO_WLOCK(&ripcbinfo); 650 INP_LOCK(inp); 651 if (so == ip_mrouter && ip_mrouter_done) 652 ip_mrouter_done(); 653 if (ip_rsvp_force_done) 654 ip_rsvp_force_done(so); 655 if (so == ip_rsvpd) 656 ip_rsvp_done(); 657 in_pcbdetach(inp); 658 in_pcbfree(inp); 659 INP_INFO_WUNLOCK(&ripcbinfo); 660 } 661 662 static void 663 rip_dodisconnect(struct socket *so, struct inpcb *inp) 664 { 665 666 INP_LOCK_ASSERT(inp); 667 668 inp->inp_faddr.s_addr = INADDR_ANY; 669 SOCK_LOCK(so); 670 so->so_state &= ~SS_ISCONNECTED; 671 SOCK_UNLOCK(so); 672 } 673 674 static void 675 rip_abort(struct socket *so) 676 { 677 struct inpcb *inp; 678 679 inp = sotoinpcb(so); 680 KASSERT(inp != NULL, ("rip_abort: inp == NULL")); 681 682 INP_INFO_WLOCK(&ripcbinfo); 683 INP_LOCK(inp); 684 rip_dodisconnect(so, inp); 685 INP_UNLOCK(inp); 686 INP_INFO_WUNLOCK(&ripcbinfo); 687 } 688 689 static void 690 rip_close(struct socket *so) 691 { 692 struct inpcb *inp; 693 694 inp = sotoinpcb(so); 695 KASSERT(inp != NULL, ("rip_close: inp == NULL")); 696 697 INP_INFO_WLOCK(&ripcbinfo); 698 INP_LOCK(inp); 699 rip_dodisconnect(so, inp); 700 INP_UNLOCK(inp); 701 INP_INFO_WUNLOCK(&ripcbinfo); 702 } 703 704 static int 705 rip_disconnect(struct socket *so) 706 { 707 struct inpcb *inp; 708 709 if ((so->so_state & SS_ISCONNECTED) == 0) 710 return ENOTCONN; 711 712 inp = sotoinpcb(so); 713 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL")); 714 INP_INFO_WLOCK(&ripcbinfo); 715 INP_LOCK(inp); 716 rip_dodisconnect(so, inp); 717 INP_UNLOCK(inp); 718 INP_INFO_WUNLOCK(&ripcbinfo); 719 return (0); 720 } 721 722 static int 723 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 724 { 725 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 726 struct inpcb *inp; 727 728 if (nam->sa_len != sizeof(*addr)) 729 return EINVAL; 730 731 if (jailed(td->td_ucred)) { 732 if (addr->sin_addr.s_addr == INADDR_ANY) 733 addr->sin_addr.s_addr = 734 htonl(prison_getip(td->td_ucred)); 735 if (htonl(prison_getip(td->td_ucred)) != addr->sin_addr.s_addr) 736 return (EADDRNOTAVAIL); 737 } 738 739 if (TAILQ_EMPTY(&ifnet) || 740 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) || 741 (addr->sin_addr.s_addr && 742 ifa_ifwithaddr((struct sockaddr *)addr) == 0)) 743 return EADDRNOTAVAIL; 744 745 inp = sotoinpcb(so); 746 KASSERT(inp != NULL, ("rip_bind: inp == NULL")); 747 INP_INFO_WLOCK(&ripcbinfo); 748 INP_LOCK(inp); 749 inp->inp_laddr = addr->sin_addr; 750 INP_UNLOCK(inp); 751 INP_INFO_WUNLOCK(&ripcbinfo); 752 return 0; 753 } 754 755 static int 756 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 757 { 758 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 759 struct inpcb *inp; 760 761 if (nam->sa_len != sizeof(*addr)) 762 return EINVAL; 763 if (TAILQ_EMPTY(&ifnet)) 764 return EADDRNOTAVAIL; 765 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) 766 return EAFNOSUPPORT; 767 768 inp = sotoinpcb(so); 769 KASSERT(inp != NULL, ("rip_connect: inp == NULL")); 770 INP_INFO_WLOCK(&ripcbinfo); 771 INP_LOCK(inp); 772 inp->inp_faddr = addr->sin_addr; 773 soisconnected(so); 774 INP_UNLOCK(inp); 775 INP_INFO_WUNLOCK(&ripcbinfo); 776 return 0; 777 } 778 779 static int 780 rip_shutdown(struct socket *so) 781 { 782 struct inpcb *inp; 783 784 inp = sotoinpcb(so); 785 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL")); 786 INP_LOCK(inp); 787 socantsendmore(so); 788 INP_UNLOCK(inp); 789 return 0; 790 } 791 792 static int 793 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 794 struct mbuf *control, struct thread *td) 795 { 796 struct inpcb *inp; 797 u_long dst; 798 799 inp = sotoinpcb(so); 800 KASSERT(inp != NULL, ("rip_send: inp == NULL")); 801 /* 802 * Note: 'dst' reads below are unlocked. 803 */ 804 if (so->so_state & SS_ISCONNECTED) { 805 if (nam) { 806 m_freem(m); 807 return EISCONN; 808 } 809 dst = inp->inp_faddr.s_addr; /* Unlocked read. */ 810 } else { 811 if (nam == NULL) { 812 m_freem(m); 813 return ENOTCONN; 814 } 815 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr; 816 } 817 return rip_output(m, so, dst); 818 } 819 820 static int 821 rip_pcblist(SYSCTL_HANDLER_ARGS) 822 { 823 int error, i, n; 824 struct inpcb *inp, **inp_list; 825 inp_gen_t gencnt; 826 struct xinpgen xig; 827 828 /* 829 * The process of preparing the TCB list is too time-consuming and 830 * resource-intensive to repeat twice on every request. 831 */ 832 if (req->oldptr == 0) { 833 n = ripcbinfo.ipi_count; 834 req->oldidx = 2 * (sizeof xig) 835 + (n + n/8) * sizeof(struct xinpcb); 836 return 0; 837 } 838 839 if (req->newptr != 0) 840 return EPERM; 841 842 /* 843 * OK, now we're committed to doing something. 844 */ 845 INP_INFO_RLOCK(&ripcbinfo); 846 gencnt = ripcbinfo.ipi_gencnt; 847 n = ripcbinfo.ipi_count; 848 INP_INFO_RUNLOCK(&ripcbinfo); 849 850 xig.xig_len = sizeof xig; 851 xig.xig_count = n; 852 xig.xig_gen = gencnt; 853 xig.xig_sogen = so_gencnt; 854 error = SYSCTL_OUT(req, &xig, sizeof xig); 855 if (error) 856 return error; 857 858 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 859 if (inp_list == 0) 860 return ENOMEM; 861 862 INP_INFO_RLOCK(&ripcbinfo); 863 for (inp = LIST_FIRST(ripcbinfo.ipi_listhead), i = 0; inp && i < n; 864 inp = LIST_NEXT(inp, inp_list)) { 865 INP_LOCK(inp); 866 if (inp->inp_gencnt <= gencnt && 867 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) { 868 /* XXX held references? */ 869 inp_list[i++] = inp; 870 } 871 INP_UNLOCK(inp); 872 } 873 INP_INFO_RUNLOCK(&ripcbinfo); 874 n = i; 875 876 error = 0; 877 for (i = 0; i < n; i++) { 878 inp = inp_list[i]; 879 INP_LOCK(inp); 880 if (inp->inp_gencnt <= gencnt) { 881 struct xinpcb xi; 882 bzero(&xi, sizeof(xi)); 883 xi.xi_len = sizeof xi; 884 /* XXX should avoid extra copy */ 885 bcopy(inp, &xi.xi_inp, sizeof *inp); 886 if (inp->inp_socket) 887 sotoxsocket(inp->inp_socket, &xi.xi_socket); 888 INP_UNLOCK(inp); 889 error = SYSCTL_OUT(req, &xi, sizeof xi); 890 } else 891 INP_UNLOCK(inp); 892 } 893 if (!error) { 894 /* 895 * Give the user an updated idea of our state. 896 * If the generation differs from what we told 897 * her before, she knows that something happened 898 * while we were processing this request, and it 899 * might be necessary to retry. 900 */ 901 INP_INFO_RLOCK(&ripcbinfo); 902 xig.xig_gen = ripcbinfo.ipi_gencnt; 903 xig.xig_sogen = so_gencnt; 904 xig.xig_count = ripcbinfo.ipi_count; 905 INP_INFO_RUNLOCK(&ripcbinfo); 906 error = SYSCTL_OUT(req, &xig, sizeof xig); 907 } 908 free(inp_list, M_TEMP); 909 return error; 910 } 911 912 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0, 913 rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); 914 915 struct pr_usrreqs rip_usrreqs = { 916 .pru_abort = rip_abort, 917 .pru_attach = rip_attach, 918 .pru_bind = rip_bind, 919 .pru_connect = rip_connect, 920 .pru_control = in_control, 921 .pru_detach = rip_detach, 922 .pru_disconnect = rip_disconnect, 923 .pru_peeraddr = in_getpeeraddr, 924 .pru_send = rip_send, 925 .pru_shutdown = rip_shutdown, 926 .pru_sockaddr = in_getsockaddr, 927 .pru_sosetlabel = in_pcbsosetlabel, 928 .pru_close = rip_close, 929 }; 930