raw_ip.c revision 185088
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: head/sys/netinet/raw_ip.c 185088 2008-11-19 09:39:34Z zec $"); 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 80struct inpcbhead ripcb; 81struct inpcbinfo ripcbinfo; 82#endif 83 84/* control hooks for ipfw and dummynet */ 85ip_fw_ctl_t *ip_fw_ctl_ptr = NULL; 86ip_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 97struct socket *ip_mrouter; 98#endif 99 100/* 101 * The various mrouter and rsvp functions. 102 */ 103int (*ip_mrouter_set)(struct socket *, struct sockopt *); 104int (*ip_mrouter_get)(struct socket *, struct sockopt *); 105int (*ip_mrouter_done)(void); 106int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *, 107 struct ip_moptions *); 108int (*mrt_ioctl)(int, caddr_t, int); 109int (*legal_vif_num)(int); 110u_long (*ip_mcast_src)(int); 111 112void (*rsvp_input_p)(struct mbuf *m, int off); 113int (*ip_rsvp_vif)(struct socket *, struct sockopt *); 114void (*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 124static void 125rip_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 145static void 146rip_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 */ 162static void 163rip_zone_change(void *tag) 164{ 165 INIT_VNET_INET(curvnet); 166 167 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets); 168} 169 170static int 171rip_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 179void 180rip_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 198static int 199rip_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 */ 246void 247rip_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 (htonl(prison_getip(inp->inp_cred)) != 278 ip->ip_dst.s_addr)) { 279 continue; 280 } 281 if (last) { 282 struct mbuf *n; 283 284 n = m_copy(m, 0, (int)M_COPYALL); 285 if (n != NULL) 286 (void) rip_append(last, ip, n, &ripsrc); 287 /* XXX count dropped packet */ 288 INP_RUNLOCK(last); 289 } 290 INP_RLOCK(inp); 291 last = inp; 292 } 293 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) { 294 if (inp->inp_ip_p && inp->inp_ip_p != proto) 295 continue; 296#ifdef INET6 297 /* XXX inp locking */ 298 if ((inp->inp_vflag & INP_IPV4) == 0) 299 continue; 300#endif 301 if (inp->inp_laddr.s_addr && 302 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 303 continue; 304 if (inp->inp_faddr.s_addr && 305 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 306 continue; 307 if (jailed(inp->inp_cred) && 308 (htonl(prison_getip(inp->inp_cred)) != 309 ip->ip_dst.s_addr)) { 310 continue; 311 } 312 if (last) { 313 struct mbuf *n; 314 315 n = m_copy(m, 0, (int)M_COPYALL); 316 if (n != NULL) 317 (void) rip_append(last, ip, n, &ripsrc); 318 /* XXX count dropped packet */ 319 INP_RUNLOCK(last); 320 } 321 INP_RLOCK(inp); 322 last = inp; 323 } 324 INP_INFO_RUNLOCK(&V_ripcbinfo); 325 if (last != NULL) { 326 if (rip_append(last, ip, m, &ripsrc) != 0) 327 V_ipstat.ips_delivered--; 328 INP_RUNLOCK(last); 329 } else { 330 m_freem(m); 331 V_ipstat.ips_noproto++; 332 V_ipstat.ips_delivered--; 333 } 334} 335 336/* 337 * Generate IP header and pass packet to ip_output. Tack on options user may 338 * have setup with control call. 339 */ 340int 341rip_output(struct mbuf *m, struct socket *so, u_long dst) 342{ 343 INIT_VNET_INET(so->so_vnet); 344 struct ip *ip; 345 int error; 346 struct inpcb *inp = sotoinpcb(so); 347 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) | 348 IP_ALLOWBROADCAST; 349 350 /* 351 * If the user handed us a complete IP packet, use it. Otherwise, 352 * allocate an mbuf for a header and fill it in. 353 */ 354 if ((inp->inp_flags & INP_HDRINCL) == 0) { 355 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { 356 m_freem(m); 357 return(EMSGSIZE); 358 } 359 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); 360 if (m == NULL) 361 return(ENOBUFS); 362 363 INP_RLOCK(inp); 364 ip = mtod(m, struct ip *); 365 ip->ip_tos = inp->inp_ip_tos; 366 if (inp->inp_flags & INP_DONTFRAG) 367 ip->ip_off = IP_DF; 368 else 369 ip->ip_off = 0; 370 ip->ip_p = inp->inp_ip_p; 371 ip->ip_len = m->m_pkthdr.len; 372 if (jailed(inp->inp_cred)) 373 ip->ip_src.s_addr = 374 htonl(prison_getip(inp->inp_cred)); 375 else 376 ip->ip_src = inp->inp_laddr; 377 ip->ip_dst.s_addr = dst; 378 ip->ip_ttl = inp->inp_ip_ttl; 379 } else { 380 if (m->m_pkthdr.len > IP_MAXPACKET) { 381 m_freem(m); 382 return(EMSGSIZE); 383 } 384 INP_RLOCK(inp); 385 ip = mtod(m, struct ip *); 386 if (jailed(inp->inp_cred)) { 387 if (ip->ip_src.s_addr != 388 htonl(prison_getip(inp->inp_cred))) { 389 INP_RUNLOCK(inp); 390 m_freem(m); 391 return (EPERM); 392 } 393 } 394 395 /* 396 * Don't allow both user specified and setsockopt options, 397 * and don't allow packet length sizes that will crash. 398 */ 399 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) 400 || (ip->ip_len > m->m_pkthdr.len) 401 || (ip->ip_len < (ip->ip_hl << 2))) { 402 INP_RUNLOCK(inp); 403 m_freem(m); 404 return (EINVAL); 405 } 406 if (ip->ip_id == 0) 407 ip->ip_id = ip_newid(); 408 409 /* 410 * XXX prevent ip_output from overwriting header fields. 411 */ 412 flags |= IP_RAWOUTPUT; 413 V_ipstat.ips_rawout++; 414 } 415 416 if (inp->inp_flags & INP_ONESBCAST) 417 flags |= IP_SENDONES; 418 419#ifdef MAC 420 mac_inpcb_create_mbuf(inp, m); 421#endif 422 423 error = ip_output(m, inp->inp_options, NULL, flags, 424 inp->inp_moptions, inp); 425 INP_RUNLOCK(inp); 426 return (error); 427} 428 429/* 430 * Raw IP socket option processing. 431 * 432 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could 433 * only be created by a privileged process, and as such, socket option 434 * operations to manage system properties on any raw socket were allowed to 435 * take place without explicit additional access control checks. However, 436 * raw sockets can now also be created in jail(), and therefore explicit 437 * checks are now required. Likewise, raw sockets can be used by a process 438 * after it gives up privilege, so some caution is required. For options 439 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be 440 * performed in ip_ctloutput() and therefore no check occurs here. 441 * Unilaterally checking priv_check() here breaks normal IP socket option 442 * operations on raw sockets. 443 * 444 * When adding new socket options here, make sure to add access control 445 * checks here as necessary. 446 */ 447int 448rip_ctloutput(struct socket *so, struct sockopt *sopt) 449{ 450 struct inpcb *inp = sotoinpcb(so); 451 int error, optval; 452 453 if (sopt->sopt_level != IPPROTO_IP) 454 return (EINVAL); 455 456 error = 0; 457 switch (sopt->sopt_dir) { 458 case SOPT_GET: 459 switch (sopt->sopt_name) { 460 case IP_HDRINCL: 461 optval = inp->inp_flags & INP_HDRINCL; 462 error = sooptcopyout(sopt, &optval, sizeof optval); 463 break; 464 465 case IP_FW_ADD: /* ADD actually returns the body... */ 466 case IP_FW_GET: 467 case IP_FW_TABLE_GETSIZE: 468 case IP_FW_TABLE_LIST: 469 case IP_FW_NAT_GET_CONFIG: 470 case IP_FW_NAT_GET_LOG: 471 if (ip_fw_ctl_ptr != NULL) 472 error = ip_fw_ctl_ptr(sopt); 473 else 474 error = ENOPROTOOPT; 475 break; 476 477 case IP_DUMMYNET_GET: 478 if (ip_dn_ctl_ptr != NULL) 479 error = ip_dn_ctl_ptr(sopt); 480 else 481 error = ENOPROTOOPT; 482 break ; 483 484 case MRT_INIT: 485 case MRT_DONE: 486 case MRT_ADD_VIF: 487 case MRT_DEL_VIF: 488 case MRT_ADD_MFC: 489 case MRT_DEL_MFC: 490 case MRT_VERSION: 491 case MRT_ASSERT: 492 case MRT_API_SUPPORT: 493 case MRT_API_CONFIG: 494 case MRT_ADD_BW_UPCALL: 495 case MRT_DEL_BW_UPCALL: 496 error = priv_check(curthread, PRIV_NETINET_MROUTE); 497 if (error != 0) 498 return (error); 499 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) : 500 EOPNOTSUPP; 501 break; 502 503 default: 504 error = ip_ctloutput(so, sopt); 505 break; 506 } 507 break; 508 509 case SOPT_SET: 510 switch (sopt->sopt_name) { 511 case IP_HDRINCL: 512 error = sooptcopyin(sopt, &optval, sizeof optval, 513 sizeof optval); 514 if (error) 515 break; 516 if (optval) 517 inp->inp_flags |= INP_HDRINCL; 518 else 519 inp->inp_flags &= ~INP_HDRINCL; 520 break; 521 522 case IP_FW_ADD: 523 case IP_FW_DEL: 524 case IP_FW_FLUSH: 525 case IP_FW_ZERO: 526 case IP_FW_RESETLOG: 527 case IP_FW_TABLE_ADD: 528 case IP_FW_TABLE_DEL: 529 case IP_FW_TABLE_FLUSH: 530 case IP_FW_NAT_CFG: 531 case IP_FW_NAT_DEL: 532 if (ip_fw_ctl_ptr != NULL) 533 error = ip_fw_ctl_ptr(sopt); 534 else 535 error = ENOPROTOOPT; 536 break; 537 538 case IP_DUMMYNET_CONFIGURE: 539 case IP_DUMMYNET_DEL: 540 case IP_DUMMYNET_FLUSH: 541 if (ip_dn_ctl_ptr != NULL) 542 error = ip_dn_ctl_ptr(sopt); 543 else 544 error = ENOPROTOOPT ; 545 break ; 546 547 case IP_RSVP_ON: 548 error = priv_check(curthread, PRIV_NETINET_MROUTE); 549 if (error != 0) 550 return (error); 551 error = ip_rsvp_init(so); 552 break; 553 554 case IP_RSVP_OFF: 555 error = priv_check(curthread, PRIV_NETINET_MROUTE); 556 if (error != 0) 557 return (error); 558 error = ip_rsvp_done(); 559 break; 560 561 case IP_RSVP_VIF_ON: 562 case IP_RSVP_VIF_OFF: 563 error = priv_check(curthread, PRIV_NETINET_MROUTE); 564 if (error != 0) 565 return (error); 566 error = ip_rsvp_vif ? 567 ip_rsvp_vif(so, sopt) : EINVAL; 568 break; 569 570 case MRT_INIT: 571 case MRT_DONE: 572 case MRT_ADD_VIF: 573 case MRT_DEL_VIF: 574 case MRT_ADD_MFC: 575 case MRT_DEL_MFC: 576 case MRT_VERSION: 577 case MRT_ASSERT: 578 case MRT_API_SUPPORT: 579 case MRT_API_CONFIG: 580 case MRT_ADD_BW_UPCALL: 581 case MRT_DEL_BW_UPCALL: 582 error = priv_check(curthread, PRIV_NETINET_MROUTE); 583 if (error != 0) 584 return (error); 585 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) : 586 EOPNOTSUPP; 587 break; 588 589 default: 590 error = ip_ctloutput(so, sopt); 591 break; 592 } 593 break; 594 } 595 596 return (error); 597} 598 599/* 600 * This function exists solely to receive the PRC_IFDOWN messages which are 601 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls 602 * in_ifadown() to remove all routes corresponding to that address. It also 603 * receives the PRC_IFUP messages from if_up() and reinstalls the interface 604 * routes. 605 */ 606void 607rip_ctlinput(int cmd, struct sockaddr *sa, void *vip) 608{ 609 INIT_VNET_INET(curvnet); 610 struct in_ifaddr *ia; 611 struct ifnet *ifp; 612 int err; 613 int flags; 614 615 switch (cmd) { 616 case PRC_IFDOWN: 617 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 618 if (ia->ia_ifa.ifa_addr == sa 619 && (ia->ia_flags & IFA_ROUTE)) { 620 /* 621 * in_ifscrub kills the interface route. 622 */ 623 in_ifscrub(ia->ia_ifp, ia); 624 /* 625 * in_ifadown gets rid of all the rest of the 626 * routes. This is not quite the right thing 627 * to do, but at least if we are running a 628 * routing process they will come back. 629 */ 630 in_ifadown(&ia->ia_ifa, 0); 631 break; 632 } 633 } 634 break; 635 636 case PRC_IFUP: 637 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 638 if (ia->ia_ifa.ifa_addr == sa) 639 break; 640 } 641 if (ia == 0 || (ia->ia_flags & IFA_ROUTE)) 642 return; 643 flags = RTF_UP; 644 ifp = ia->ia_ifa.ifa_ifp; 645 646 if ((ifp->if_flags & IFF_LOOPBACK) 647 || (ifp->if_flags & IFF_POINTOPOINT)) 648 flags |= RTF_HOST; 649 650 err = rtinit(&ia->ia_ifa, RTM_ADD, flags); 651 if (err == 0) 652 ia->ia_flags |= IFA_ROUTE; 653 break; 654 } 655} 656 657u_long rip_sendspace = 9216; 658u_long rip_recvspace = 9216; 659 660SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, 661 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); 662SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, 663 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams"); 664 665static int 666rip_attach(struct socket *so, int proto, struct thread *td) 667{ 668 INIT_VNET_INET(so->so_vnet); 669 struct inpcb *inp; 670 int error; 671 672 inp = sotoinpcb(so); 673 KASSERT(inp == NULL, ("rip_attach: inp != NULL")); 674 675 error = priv_check(td, PRIV_NETINET_RAW); 676 if (error) 677 return (error); 678 if (proto >= IPPROTO_MAX || proto < 0) 679 return EPROTONOSUPPORT; 680 error = soreserve(so, rip_sendspace, rip_recvspace); 681 if (error) 682 return (error); 683 INP_INFO_WLOCK(&V_ripcbinfo); 684 error = in_pcballoc(so, &V_ripcbinfo); 685 if (error) { 686 INP_INFO_WUNLOCK(&V_ripcbinfo); 687 return (error); 688 } 689 inp = (struct inpcb *)so->so_pcb; 690 inp->inp_vflag |= INP_IPV4; 691 inp->inp_ip_p = proto; 692 inp->inp_ip_ttl = V_ip_defttl; 693 rip_inshash(inp); 694 INP_INFO_WUNLOCK(&V_ripcbinfo); 695 INP_WUNLOCK(inp); 696 return (0); 697} 698 699static void 700rip_detach(struct socket *so) 701{ 702 INIT_VNET_INET(so->so_vnet); 703 struct inpcb *inp; 704 705 inp = sotoinpcb(so); 706 KASSERT(inp != NULL, ("rip_detach: inp == NULL")); 707 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 708 ("rip_detach: not closed")); 709 710 INP_INFO_WLOCK(&V_ripcbinfo); 711 INP_WLOCK(inp); 712 rip_delhash(inp); 713 if (so == V_ip_mrouter && ip_mrouter_done) 714 ip_mrouter_done(); 715 if (ip_rsvp_force_done) 716 ip_rsvp_force_done(so); 717 if (so == V_ip_rsvpd) 718 ip_rsvp_done(); 719 in_pcbdetach(inp); 720 in_pcbfree(inp); 721 INP_INFO_WUNLOCK(&V_ripcbinfo); 722} 723 724static void 725rip_dodisconnect(struct socket *so, struct inpcb *inp) 726{ 727 728 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 729 INP_WLOCK_ASSERT(inp); 730 731 rip_delhash(inp); 732 inp->inp_faddr.s_addr = INADDR_ANY; 733 rip_inshash(inp); 734 SOCK_LOCK(so); 735 so->so_state &= ~SS_ISCONNECTED; 736 SOCK_UNLOCK(so); 737} 738 739static void 740rip_abort(struct socket *so) 741{ 742 INIT_VNET_INET(so->so_vnet); 743 struct inpcb *inp; 744 745 inp = sotoinpcb(so); 746 KASSERT(inp != NULL, ("rip_abort: inp == NULL")); 747 748 INP_INFO_WLOCK(&V_ripcbinfo); 749 INP_WLOCK(inp); 750 rip_dodisconnect(so, inp); 751 INP_WUNLOCK(inp); 752 INP_INFO_WUNLOCK(&V_ripcbinfo); 753} 754 755static void 756rip_close(struct socket *so) 757{ 758 INIT_VNET_INET(so->so_vnet); 759 struct inpcb *inp; 760 761 inp = sotoinpcb(so); 762 KASSERT(inp != NULL, ("rip_close: inp == NULL")); 763 764 INP_INFO_WLOCK(&V_ripcbinfo); 765 INP_WLOCK(inp); 766 rip_dodisconnect(so, inp); 767 INP_WUNLOCK(inp); 768 INP_INFO_WUNLOCK(&V_ripcbinfo); 769} 770 771static int 772rip_disconnect(struct socket *so) 773{ 774 INIT_VNET_INET(so->so_vnet); 775 struct inpcb *inp; 776 777 if ((so->so_state & SS_ISCONNECTED) == 0) 778 return (ENOTCONN); 779 780 inp = sotoinpcb(so); 781 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL")); 782 783 INP_INFO_WLOCK(&V_ripcbinfo); 784 INP_WLOCK(inp); 785 rip_dodisconnect(so, inp); 786 INP_WUNLOCK(inp); 787 INP_INFO_WUNLOCK(&V_ripcbinfo); 788 return (0); 789} 790 791static int 792rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 793{ 794 INIT_VNET_NET(so->so_vnet); 795 INIT_VNET_INET(so->so_vnet); 796 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 797 struct inpcb *inp; 798 799 if (nam->sa_len != sizeof(*addr)) 800 return (EINVAL); 801 802 if (jailed(td->td_ucred)) { 803 if (addr->sin_addr.s_addr == INADDR_ANY) 804 addr->sin_addr.s_addr = 805 htonl(prison_getip(td->td_ucred)); 806 if (htonl(prison_getip(td->td_ucred)) != addr->sin_addr.s_addr) 807 return (EADDRNOTAVAIL); 808 } 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 829static int 830rip_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 858static int 859rip_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 872static int 873rip_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 901static int 902rip_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 993SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0, 994 rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); 995 996struct 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