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