raw_ip.c revision 241913
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 241913 2012-10-22 21:09:03Z glebius $"); 35 36#include "opt_inet.h" 37#include "opt_inet6.h" 38#include "opt_ipsec.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/rwlock.h> 50#include <sys/signalvar.h> 51#include <sys/socket.h> 52#include <sys/socketvar.h> 53#include <sys/sx.h> 54#include <sys/sysctl.h> 55#include <sys/systm.h> 56 57#include <vm/uma.h> 58 59#include <net/if.h> 60#include <net/route.h> 61#include <net/vnet.h> 62 63#include <netinet/in.h> 64#include <netinet/in_systm.h> 65#include <netinet/in_pcb.h> 66#include <netinet/in_var.h> 67#include <netinet/if_ether.h> 68#include <netinet/ip.h> 69#include <netinet/ip_var.h> 70#include <netinet/ip_mroute.h> 71 72#ifdef IPSEC 73#include <netipsec/ipsec.h> 74#endif /*IPSEC*/ 75 76#include <security/mac/mac_framework.h> 77 78VNET_DEFINE(int, ip_defttl) = IPDEFTTL; 79SYSCTL_VNET_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW, 80 &VNET_NAME(ip_defttl), 0, 81 "Maximum TTL on IP packets"); 82 83VNET_DEFINE(struct inpcbhead, ripcb); 84VNET_DEFINE(struct inpcbinfo, ripcbinfo); 85 86#define V_ripcb VNET(ripcb) 87#define V_ripcbinfo VNET(ripcbinfo) 88 89/* 90 * Control and data hooks for ipfw, dummynet, divert and so on. 91 * The data hooks are not used here but it is convenient 92 * to keep them all in one place. 93 */ 94VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL; 95VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL; 96 97int (*ip_dn_ctl_ptr)(struct sockopt *); 98int (*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *); 99void (*ip_divert_ptr)(struct mbuf *, int); 100int (*ng_ipfw_input_p)(struct mbuf **, int, 101 struct ip_fw_args *, int); 102 103#ifdef INET 104/* 105 * Hooks for multicast routing. They all default to NULL, so leave them not 106 * initialized and rely on BSS being set to 0. 107 */ 108 109/* 110 * The socket used to communicate with the multicast routing daemon. 111 */ 112VNET_DEFINE(struct socket *, ip_mrouter); 113 114/* 115 * The various mrouter and rsvp functions. 116 */ 117int (*ip_mrouter_set)(struct socket *, struct sockopt *); 118int (*ip_mrouter_get)(struct socket *, struct sockopt *); 119int (*ip_mrouter_done)(void); 120int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *, 121 struct ip_moptions *); 122int (*mrt_ioctl)(u_long, caddr_t, int); 123int (*legal_vif_num)(int); 124u_long (*ip_mcast_src)(int); 125 126void (*rsvp_input_p)(struct mbuf *m, int off); 127int (*ip_rsvp_vif)(struct socket *, struct sockopt *); 128void (*ip_rsvp_force_done)(struct socket *); 129#endif /* INET */ 130 131u_long rip_sendspace = 9216; 132SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, 133 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); 134 135u_long rip_recvspace = 9216; 136SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, 137 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams"); 138 139/* 140 * Hash functions 141 */ 142 143#define INP_PCBHASH_RAW_SIZE 256 144#define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \ 145 (((proto) + (laddr) + (faddr)) % (mask) + 1) 146 147#ifdef INET 148static void 149rip_inshash(struct inpcb *inp) 150{ 151 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 152 struct inpcbhead *pcbhash; 153 int hash; 154 155 INP_INFO_WLOCK_ASSERT(pcbinfo); 156 INP_WLOCK_ASSERT(inp); 157 158 if (inp->inp_ip_p != 0 && 159 inp->inp_laddr.s_addr != INADDR_ANY && 160 inp->inp_faddr.s_addr != INADDR_ANY) { 161 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr, 162 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask); 163 } else 164 hash = 0; 165 pcbhash = &pcbinfo->ipi_hashbase[hash]; 166 LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 167} 168 169static void 170rip_delhash(struct inpcb *inp) 171{ 172 173 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 174 INP_WLOCK_ASSERT(inp); 175 176 LIST_REMOVE(inp, inp_hash); 177} 178#endif /* INET */ 179 180/* 181 * Raw interface to IP protocol. 182 */ 183 184/* 185 * Initialize raw connection block q. 186 */ 187static void 188rip_zone_change(void *tag) 189{ 190 191 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets); 192} 193 194static int 195rip_inpcb_init(void *mem, int size, int flags) 196{ 197 struct inpcb *inp = mem; 198 199 INP_LOCK_INIT(inp, "inp", "rawinp"); 200 return (0); 201} 202 203void 204rip_init(void) 205{ 206 207 in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE, 208 1, "ripcb", rip_inpcb_init, NULL, UMA_ZONE_NOFREE, 209 IPI_HASHFIELDS_NONE); 210 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL, 211 EVENTHANDLER_PRI_ANY); 212} 213 214#ifdef VIMAGE 215void 216rip_destroy(void) 217{ 218 219 in_pcbinfo_destroy(&V_ripcbinfo); 220} 221#endif 222 223#ifdef INET 224static int 225rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n, 226 struct sockaddr_in *ripsrc) 227{ 228 int policyfail = 0; 229 230 INP_LOCK_ASSERT(last); 231 232#ifdef IPSEC 233 /* check AH/ESP integrity. */ 234 if (ipsec4_in_reject(n, last)) { 235 policyfail = 1; 236 } 237#endif /* IPSEC */ 238#ifdef MAC 239 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0) 240 policyfail = 1; 241#endif 242 /* Check the minimum TTL for socket. */ 243 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl) 244 policyfail = 1; 245 if (!policyfail) { 246 struct mbuf *opts = NULL; 247 struct socket *so; 248 249 so = last->inp_socket; 250 if ((last->inp_flags & INP_CONTROLOPTS) || 251 (so->so_options & (SO_TIMESTAMP | SO_BINTIME))) 252 ip_savecontrol(last, &opts, ip, n); 253 SOCKBUF_LOCK(&so->so_rcv); 254 if (sbappendaddr_locked(&so->so_rcv, 255 (struct sockaddr *)ripsrc, n, opts) == 0) { 256 /* should notify about lost packet */ 257 m_freem(n); 258 if (opts) 259 m_freem(opts); 260 SOCKBUF_UNLOCK(&so->so_rcv); 261 } else 262 sorwakeup_locked(so); 263 } else 264 m_freem(n); 265 return (policyfail); 266} 267 268/* 269 * Setup generic address and protocol structures for raw_input routine, then 270 * pass them along with mbuf chain. 271 */ 272void 273rip_input(struct mbuf *m, int off) 274{ 275 struct ifnet *ifp; 276 struct ip *ip = mtod(m, struct ip *); 277 int proto = ip->ip_p; 278 struct inpcb *inp, *last; 279 struct sockaddr_in ripsrc; 280 int hash; 281 282 bzero(&ripsrc, sizeof(ripsrc)); 283 ripsrc.sin_len = sizeof(ripsrc); 284 ripsrc.sin_family = AF_INET; 285 ripsrc.sin_addr = ip->ip_src; 286 last = NULL; 287 288 ifp = m->m_pkthdr.rcvif; 289 /* 290 * Add back the IP header length which was 291 * removed by ip_input(). Raw sockets do 292 * not modify the packet except for some 293 * byte order swaps. 294 */ 295 ip->ip_len = ntohs(ip->ip_len) + off; 296 ip->ip_off = ntohs(ip->ip_off); 297 298 hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr, 299 ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask); 300 INP_INFO_RLOCK(&V_ripcbinfo); 301 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) { 302 if (inp->inp_ip_p != proto) 303 continue; 304#ifdef INET6 305 /* XXX inp locking */ 306 if ((inp->inp_vflag & INP_IPV4) == 0) 307 continue; 308#endif 309 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 310 continue; 311 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr) 312 continue; 313 if (jailed_without_vnet(inp->inp_cred)) { 314 /* 315 * XXX: If faddr was bound to multicast group, 316 * jailed raw socket will drop datagram. 317 */ 318 if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0) 319 continue; 320 } 321 if (last != NULL) { 322 struct mbuf *n; 323 324 n = m_copy(m, 0, (int)M_COPYALL); 325 if (n != NULL) 326 (void) rip_append(last, ip, n, &ripsrc); 327 /* XXX count dropped packet */ 328 INP_RUNLOCK(last); 329 } 330 INP_RLOCK(inp); 331 last = inp; 332 } 333 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) { 334 if (inp->inp_ip_p && inp->inp_ip_p != proto) 335 continue; 336#ifdef INET6 337 /* XXX inp locking */ 338 if ((inp->inp_vflag & INP_IPV4) == 0) 339 continue; 340#endif 341 if (!in_nullhost(inp->inp_laddr) && 342 !in_hosteq(inp->inp_laddr, ip->ip_dst)) 343 continue; 344 if (!in_nullhost(inp->inp_faddr) && 345 !in_hosteq(inp->inp_faddr, ip->ip_src)) 346 continue; 347 if (jailed_without_vnet(inp->inp_cred)) { 348 /* 349 * Allow raw socket in jail to receive multicast; 350 * assume process had PRIV_NETINET_RAW at attach, 351 * and fall through into normal filter path if so. 352 */ 353 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 354 prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0) 355 continue; 356 } 357 /* 358 * If this raw socket has multicast state, and we 359 * have received a multicast, check if this socket 360 * should receive it, as multicast filtering is now 361 * the responsibility of the transport layer. 362 */ 363 if (inp->inp_moptions != NULL && 364 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 365 /* 366 * If the incoming datagram is for IGMP, allow it 367 * through unconditionally to the raw socket. 368 * 369 * In the case of IGMPv2, we may not have explicitly 370 * joined the group, and may have set IFF_ALLMULTI 371 * on the interface. imo_multi_filter() may discard 372 * control traffic we actually need to see. 373 * 374 * Userland multicast routing daemons should continue 375 * filter the control traffic appropriately. 376 */ 377 int blocked; 378 379 blocked = MCAST_PASS; 380 if (proto != IPPROTO_IGMP) { 381 struct sockaddr_in group; 382 383 bzero(&group, sizeof(struct sockaddr_in)); 384 group.sin_len = sizeof(struct sockaddr_in); 385 group.sin_family = AF_INET; 386 group.sin_addr = ip->ip_dst; 387 388 blocked = imo_multi_filter(inp->inp_moptions, 389 ifp, 390 (struct sockaddr *)&group, 391 (struct sockaddr *)&ripsrc); 392 } 393 394 if (blocked != MCAST_PASS) { 395 IPSTAT_INC(ips_notmember); 396 continue; 397 } 398 } 399 if (last != NULL) { 400 struct mbuf *n; 401 402 n = m_copy(m, 0, (int)M_COPYALL); 403 if (n != NULL) 404 (void) rip_append(last, ip, n, &ripsrc); 405 /* XXX count dropped packet */ 406 INP_RUNLOCK(last); 407 } 408 INP_RLOCK(inp); 409 last = inp; 410 } 411 INP_INFO_RUNLOCK(&V_ripcbinfo); 412 if (last != NULL) { 413 if (rip_append(last, ip, m, &ripsrc) != 0) 414 IPSTAT_INC(ips_delivered); 415 INP_RUNLOCK(last); 416 } else { 417 m_freem(m); 418 IPSTAT_INC(ips_noproto); 419 IPSTAT_DEC(ips_delivered); 420 } 421} 422 423/* 424 * Generate IP header and pass packet to ip_output. Tack on options user may 425 * have setup with control call. 426 */ 427int 428rip_output(struct mbuf *m, struct socket *so, u_long dst) 429{ 430 struct ip *ip; 431 int error; 432 struct inpcb *inp = sotoinpcb(so); 433 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) | 434 IP_ALLOWBROADCAST; 435 436 /* 437 * If the user handed us a complete IP packet, use it. Otherwise, 438 * allocate an mbuf for a header and fill it in. 439 */ 440 if ((inp->inp_flags & INP_HDRINCL) == 0) { 441 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { 442 m_freem(m); 443 return(EMSGSIZE); 444 } 445 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); 446 if (m == NULL) 447 return(ENOBUFS); 448 449 INP_RLOCK(inp); 450 ip = mtod(m, struct ip *); 451 ip->ip_tos = inp->inp_ip_tos; 452 if (inp->inp_flags & INP_DONTFRAG) 453 ip->ip_off = htons(IP_DF); 454 else 455 ip->ip_off = htons(0); 456 ip->ip_p = inp->inp_ip_p; 457 ip->ip_len = htons(m->m_pkthdr.len); 458 ip->ip_src = inp->inp_laddr; 459 if (jailed(inp->inp_cred)) { 460 /* 461 * prison_local_ip4() would be good enough but would 462 * let a source of INADDR_ANY pass, which we do not 463 * want to see from jails. We do not go through the 464 * pain of in_pcbladdr() for raw sockets. 465 */ 466 if (ip->ip_src.s_addr == INADDR_ANY) 467 error = prison_get_ip4(inp->inp_cred, 468 &ip->ip_src); 469 else 470 error = prison_local_ip4(inp->inp_cred, 471 &ip->ip_src); 472 if (error != 0) { 473 INP_RUNLOCK(inp); 474 m_freem(m); 475 return (error); 476 } 477 } 478 ip->ip_dst.s_addr = dst; 479 ip->ip_ttl = inp->inp_ip_ttl; 480 } else { 481 if (m->m_pkthdr.len > IP_MAXPACKET) { 482 m_freem(m); 483 return(EMSGSIZE); 484 } 485 INP_RLOCK(inp); 486 ip = mtod(m, struct ip *); 487 error = prison_check_ip4(inp->inp_cred, &ip->ip_src); 488 if (error != 0) { 489 INP_RUNLOCK(inp); 490 m_freem(m); 491 return (error); 492 } 493 494 /* 495 * Don't allow both user specified and setsockopt options, 496 * and don't allow packet length sizes that will crash. 497 */ 498 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) 499 || (ip->ip_len > m->m_pkthdr.len) 500 || (ip->ip_len < (ip->ip_hl << 2))) { 501 INP_RUNLOCK(inp); 502 m_freem(m); 503 return (EINVAL); 504 } 505 if (ip->ip_id == 0) 506 ip->ip_id = ip_newid(); 507 508 /* 509 * Applications on raw sockets expect host byte order. 510 */ 511 ip->ip_len = htons(ip->ip_len); 512 ip->ip_off = htons(ip->ip_off); 513 514 /* 515 * XXX prevent ip_output from overwriting header fields. 516 */ 517 flags |= IP_RAWOUTPUT; 518 IPSTAT_INC(ips_rawout); 519 } 520 521 if (inp->inp_flags & INP_ONESBCAST) 522 flags |= IP_SENDONES; 523 524#ifdef MAC 525 mac_inpcb_create_mbuf(inp, m); 526#endif 527 528 error = ip_output(m, inp->inp_options, NULL, flags, 529 inp->inp_moptions, inp); 530 INP_RUNLOCK(inp); 531 return (error); 532} 533 534/* 535 * Raw IP socket option processing. 536 * 537 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could 538 * only be created by a privileged process, and as such, socket option 539 * operations to manage system properties on any raw socket were allowed to 540 * take place without explicit additional access control checks. However, 541 * raw sockets can now also be created in jail(), and therefore explicit 542 * checks are now required. Likewise, raw sockets can be used by a process 543 * after it gives up privilege, so some caution is required. For options 544 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be 545 * performed in ip_ctloutput() and therefore no check occurs here. 546 * Unilaterally checking priv_check() here breaks normal IP socket option 547 * operations on raw sockets. 548 * 549 * When adding new socket options here, make sure to add access control 550 * checks here as necessary. 551 * 552 * XXX-BZ inp locking? 553 */ 554int 555rip_ctloutput(struct socket *so, struct sockopt *sopt) 556{ 557 struct inpcb *inp = sotoinpcb(so); 558 int error, optval; 559 560 if (sopt->sopt_level != IPPROTO_IP) { 561 if ((sopt->sopt_level == SOL_SOCKET) && 562 (sopt->sopt_name == SO_SETFIB)) { 563 inp->inp_inc.inc_fibnum = so->so_fibnum; 564 return (0); 565 } 566 return (EINVAL); 567 } 568 569 error = 0; 570 switch (sopt->sopt_dir) { 571 case SOPT_GET: 572 switch (sopt->sopt_name) { 573 case IP_HDRINCL: 574 optval = inp->inp_flags & INP_HDRINCL; 575 error = sooptcopyout(sopt, &optval, sizeof optval); 576 break; 577 578 case IP_FW3: /* generic ipfw v.3 functions */ 579 case IP_FW_ADD: /* ADD actually returns the body... */ 580 case IP_FW_GET: 581 case IP_FW_TABLE_GETSIZE: 582 case IP_FW_TABLE_LIST: 583 case IP_FW_NAT_GET_CONFIG: 584 case IP_FW_NAT_GET_LOG: 585 if (V_ip_fw_ctl_ptr != NULL) 586 error = V_ip_fw_ctl_ptr(sopt); 587 else 588 error = ENOPROTOOPT; 589 break; 590 591 case IP_DUMMYNET3: /* generic dummynet v.3 functions */ 592 case IP_DUMMYNET_GET: 593 if (ip_dn_ctl_ptr != NULL) 594 error = ip_dn_ctl_ptr(sopt); 595 else 596 error = ENOPROTOOPT; 597 break ; 598 599 case MRT_INIT: 600 case MRT_DONE: 601 case MRT_ADD_VIF: 602 case MRT_DEL_VIF: 603 case MRT_ADD_MFC: 604 case MRT_DEL_MFC: 605 case MRT_VERSION: 606 case MRT_ASSERT: 607 case MRT_API_SUPPORT: 608 case MRT_API_CONFIG: 609 case MRT_ADD_BW_UPCALL: 610 case MRT_DEL_BW_UPCALL: 611 error = priv_check(curthread, PRIV_NETINET_MROUTE); 612 if (error != 0) 613 return (error); 614 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) : 615 EOPNOTSUPP; 616 break; 617 618 default: 619 error = ip_ctloutput(so, sopt); 620 break; 621 } 622 break; 623 624 case SOPT_SET: 625 switch (sopt->sopt_name) { 626 case IP_HDRINCL: 627 error = sooptcopyin(sopt, &optval, sizeof optval, 628 sizeof optval); 629 if (error) 630 break; 631 if (optval) 632 inp->inp_flags |= INP_HDRINCL; 633 else 634 inp->inp_flags &= ~INP_HDRINCL; 635 break; 636 637 case IP_FW3: /* generic ipfw v.3 functions */ 638 case IP_FW_ADD: 639 case IP_FW_DEL: 640 case IP_FW_FLUSH: 641 case IP_FW_ZERO: 642 case IP_FW_RESETLOG: 643 case IP_FW_TABLE_ADD: 644 case IP_FW_TABLE_DEL: 645 case IP_FW_TABLE_FLUSH: 646 case IP_FW_NAT_CFG: 647 case IP_FW_NAT_DEL: 648 if (V_ip_fw_ctl_ptr != NULL) 649 error = V_ip_fw_ctl_ptr(sopt); 650 else 651 error = ENOPROTOOPT; 652 break; 653 654 case IP_DUMMYNET3: /* generic dummynet v.3 functions */ 655 case IP_DUMMYNET_CONFIGURE: 656 case IP_DUMMYNET_DEL: 657 case IP_DUMMYNET_FLUSH: 658 if (ip_dn_ctl_ptr != NULL) 659 error = ip_dn_ctl_ptr(sopt); 660 else 661 error = ENOPROTOOPT ; 662 break ; 663 664 case IP_RSVP_ON: 665 error = priv_check(curthread, PRIV_NETINET_MROUTE); 666 if (error != 0) 667 return (error); 668 error = ip_rsvp_init(so); 669 break; 670 671 case IP_RSVP_OFF: 672 error = priv_check(curthread, PRIV_NETINET_MROUTE); 673 if (error != 0) 674 return (error); 675 error = ip_rsvp_done(); 676 break; 677 678 case IP_RSVP_VIF_ON: 679 case IP_RSVP_VIF_OFF: 680 error = priv_check(curthread, PRIV_NETINET_MROUTE); 681 if (error != 0) 682 return (error); 683 error = ip_rsvp_vif ? 684 ip_rsvp_vif(so, sopt) : EINVAL; 685 break; 686 687 case MRT_INIT: 688 case MRT_DONE: 689 case MRT_ADD_VIF: 690 case MRT_DEL_VIF: 691 case MRT_ADD_MFC: 692 case MRT_DEL_MFC: 693 case MRT_VERSION: 694 case MRT_ASSERT: 695 case MRT_API_SUPPORT: 696 case MRT_API_CONFIG: 697 case MRT_ADD_BW_UPCALL: 698 case MRT_DEL_BW_UPCALL: 699 error = priv_check(curthread, PRIV_NETINET_MROUTE); 700 if (error != 0) 701 return (error); 702 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) : 703 EOPNOTSUPP; 704 break; 705 706 default: 707 error = ip_ctloutput(so, sopt); 708 break; 709 } 710 break; 711 } 712 713 return (error); 714} 715 716/* 717 * This function exists solely to receive the PRC_IFDOWN messages which are 718 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls 719 * in_ifadown() to remove all routes corresponding to that address. It also 720 * receives the PRC_IFUP messages from if_up() and reinstalls the interface 721 * routes. 722 */ 723void 724rip_ctlinput(int cmd, struct sockaddr *sa, void *vip) 725{ 726 struct in_ifaddr *ia; 727 struct ifnet *ifp; 728 int err; 729 int flags; 730 731 switch (cmd) { 732 case PRC_IFDOWN: 733 IN_IFADDR_RLOCK(); 734 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 735 if (ia->ia_ifa.ifa_addr == sa 736 && (ia->ia_flags & IFA_ROUTE)) { 737 ifa_ref(&ia->ia_ifa); 738 IN_IFADDR_RUNLOCK(); 739 /* 740 * in_ifscrub kills the interface route. 741 */ 742 in_ifscrub(ia->ia_ifp, ia, 0); 743 /* 744 * in_ifadown gets rid of all the rest of the 745 * routes. This is not quite the right thing 746 * to do, but at least if we are running a 747 * routing process they will come back. 748 */ 749 in_ifadown(&ia->ia_ifa, 0); 750 ifa_free(&ia->ia_ifa); 751 break; 752 } 753 } 754 if (ia == NULL) /* If ia matched, already unlocked. */ 755 IN_IFADDR_RUNLOCK(); 756 break; 757 758 case PRC_IFUP: 759 IN_IFADDR_RLOCK(); 760 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 761 if (ia->ia_ifa.ifa_addr == sa) 762 break; 763 } 764 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) { 765 IN_IFADDR_RUNLOCK(); 766 return; 767 } 768 ifa_ref(&ia->ia_ifa); 769 IN_IFADDR_RUNLOCK(); 770 flags = RTF_UP; 771 ifp = ia->ia_ifa.ifa_ifp; 772 773 if ((ifp->if_flags & IFF_LOOPBACK) 774 || (ifp->if_flags & IFF_POINTOPOINT)) 775 flags |= RTF_HOST; 776 777 err = ifa_del_loopback_route((struct ifaddr *)ia, sa); 778 if (err == 0) 779 ia->ia_flags &= ~IFA_RTSELF; 780 781 err = rtinit(&ia->ia_ifa, RTM_ADD, flags); 782 if (err == 0) 783 ia->ia_flags |= IFA_ROUTE; 784 785 err = ifa_add_loopback_route((struct ifaddr *)ia, sa); 786 if (err == 0) 787 ia->ia_flags |= IFA_RTSELF; 788 789 ifa_free(&ia->ia_ifa); 790 break; 791 } 792} 793 794static int 795rip_attach(struct socket *so, int proto, struct thread *td) 796{ 797 struct inpcb *inp; 798 int error; 799 800 inp = sotoinpcb(so); 801 KASSERT(inp == NULL, ("rip_attach: inp != NULL")); 802 803 error = priv_check(td, PRIV_NETINET_RAW); 804 if (error) 805 return (error); 806 if (proto >= IPPROTO_MAX || proto < 0) 807 return EPROTONOSUPPORT; 808 error = soreserve(so, rip_sendspace, rip_recvspace); 809 if (error) 810 return (error); 811 INP_INFO_WLOCK(&V_ripcbinfo); 812 error = in_pcballoc(so, &V_ripcbinfo); 813 if (error) { 814 INP_INFO_WUNLOCK(&V_ripcbinfo); 815 return (error); 816 } 817 inp = (struct inpcb *)so->so_pcb; 818 inp->inp_vflag |= INP_IPV4; 819 inp->inp_ip_p = proto; 820 inp->inp_ip_ttl = V_ip_defttl; 821 rip_inshash(inp); 822 INP_INFO_WUNLOCK(&V_ripcbinfo); 823 INP_WUNLOCK(inp); 824 return (0); 825} 826 827static void 828rip_detach(struct socket *so) 829{ 830 struct inpcb *inp; 831 832 inp = sotoinpcb(so); 833 KASSERT(inp != NULL, ("rip_detach: inp == NULL")); 834 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 835 ("rip_detach: not closed")); 836 837 INP_INFO_WLOCK(&V_ripcbinfo); 838 INP_WLOCK(inp); 839 rip_delhash(inp); 840 if (so == V_ip_mrouter && ip_mrouter_done) 841 ip_mrouter_done(); 842 if (ip_rsvp_force_done) 843 ip_rsvp_force_done(so); 844 if (so == V_ip_rsvpd) 845 ip_rsvp_done(); 846 in_pcbdetach(inp); 847 in_pcbfree(inp); 848 INP_INFO_WUNLOCK(&V_ripcbinfo); 849} 850 851static void 852rip_dodisconnect(struct socket *so, struct inpcb *inp) 853{ 854 struct inpcbinfo *pcbinfo; 855 856 pcbinfo = inp->inp_pcbinfo; 857 INP_INFO_WLOCK(pcbinfo); 858 INP_WLOCK(inp); 859 rip_delhash(inp); 860 inp->inp_faddr.s_addr = INADDR_ANY; 861 rip_inshash(inp); 862 SOCK_LOCK(so); 863 so->so_state &= ~SS_ISCONNECTED; 864 SOCK_UNLOCK(so); 865 INP_WUNLOCK(inp); 866 INP_INFO_WUNLOCK(pcbinfo); 867} 868 869static void 870rip_abort(struct socket *so) 871{ 872 struct inpcb *inp; 873 874 inp = sotoinpcb(so); 875 KASSERT(inp != NULL, ("rip_abort: inp == NULL")); 876 877 rip_dodisconnect(so, inp); 878} 879 880static void 881rip_close(struct socket *so) 882{ 883 struct inpcb *inp; 884 885 inp = sotoinpcb(so); 886 KASSERT(inp != NULL, ("rip_close: inp == NULL")); 887 888 rip_dodisconnect(so, inp); 889} 890 891static int 892rip_disconnect(struct socket *so) 893{ 894 struct inpcb *inp; 895 896 if ((so->so_state & SS_ISCONNECTED) == 0) 897 return (ENOTCONN); 898 899 inp = sotoinpcb(so); 900 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL")); 901 902 rip_dodisconnect(so, inp); 903 return (0); 904} 905 906static int 907rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 908{ 909 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 910 struct inpcb *inp; 911 int error; 912 913 if (nam->sa_len != sizeof(*addr)) 914 return (EINVAL); 915 916 error = prison_check_ip4(td->td_ucred, &addr->sin_addr); 917 if (error != 0) 918 return (error); 919 920 inp = sotoinpcb(so); 921 KASSERT(inp != NULL, ("rip_bind: inp == NULL")); 922 923 if (TAILQ_EMPTY(&V_ifnet) || 924 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) || 925 (addr->sin_addr.s_addr && 926 (inp->inp_flags & INP_BINDANY) == 0 && 927 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0)) 928 return (EADDRNOTAVAIL); 929 930 INP_INFO_WLOCK(&V_ripcbinfo); 931 INP_WLOCK(inp); 932 rip_delhash(inp); 933 inp->inp_laddr = addr->sin_addr; 934 rip_inshash(inp); 935 INP_WUNLOCK(inp); 936 INP_INFO_WUNLOCK(&V_ripcbinfo); 937 return (0); 938} 939 940static int 941rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 942{ 943 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 944 struct inpcb *inp; 945 946 if (nam->sa_len != sizeof(*addr)) 947 return (EINVAL); 948 if (TAILQ_EMPTY(&V_ifnet)) 949 return (EADDRNOTAVAIL); 950 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) 951 return (EAFNOSUPPORT); 952 953 inp = sotoinpcb(so); 954 KASSERT(inp != NULL, ("rip_connect: inp == NULL")); 955 956 INP_INFO_WLOCK(&V_ripcbinfo); 957 INP_WLOCK(inp); 958 rip_delhash(inp); 959 inp->inp_faddr = addr->sin_addr; 960 rip_inshash(inp); 961 soisconnected(so); 962 INP_WUNLOCK(inp); 963 INP_INFO_WUNLOCK(&V_ripcbinfo); 964 return (0); 965} 966 967static int 968rip_shutdown(struct socket *so) 969{ 970 struct inpcb *inp; 971 972 inp = sotoinpcb(so); 973 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL")); 974 975 INP_WLOCK(inp); 976 socantsendmore(so); 977 INP_WUNLOCK(inp); 978 return (0); 979} 980 981static int 982rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 983 struct mbuf *control, struct thread *td) 984{ 985 struct inpcb *inp; 986 u_long dst; 987 988 inp = sotoinpcb(so); 989 KASSERT(inp != NULL, ("rip_send: inp == NULL")); 990 991 /* 992 * Note: 'dst' reads below are unlocked. 993 */ 994 if (so->so_state & SS_ISCONNECTED) { 995 if (nam) { 996 m_freem(m); 997 return (EISCONN); 998 } 999 dst = inp->inp_faddr.s_addr; /* Unlocked read. */ 1000 } else { 1001 if (nam == NULL) { 1002 m_freem(m); 1003 return (ENOTCONN); 1004 } 1005 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr; 1006 } 1007 return (rip_output(m, so, dst)); 1008} 1009#endif /* INET */ 1010 1011static int 1012rip_pcblist(SYSCTL_HANDLER_ARGS) 1013{ 1014 int error, i, n; 1015 struct inpcb *inp, **inp_list; 1016 inp_gen_t gencnt; 1017 struct xinpgen xig; 1018 1019 /* 1020 * The process of preparing the TCB list is too time-consuming and 1021 * resource-intensive to repeat twice on every request. 1022 */ 1023 if (req->oldptr == 0) { 1024 n = V_ripcbinfo.ipi_count; 1025 n += imax(n / 8, 10); 1026 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb); 1027 return (0); 1028 } 1029 1030 if (req->newptr != 0) 1031 return (EPERM); 1032 1033 /* 1034 * OK, now we're committed to doing something. 1035 */ 1036 INP_INFO_RLOCK(&V_ripcbinfo); 1037 gencnt = V_ripcbinfo.ipi_gencnt; 1038 n = V_ripcbinfo.ipi_count; 1039 INP_INFO_RUNLOCK(&V_ripcbinfo); 1040 1041 xig.xig_len = sizeof xig; 1042 xig.xig_count = n; 1043 xig.xig_gen = gencnt; 1044 xig.xig_sogen = so_gencnt; 1045 error = SYSCTL_OUT(req, &xig, sizeof xig); 1046 if (error) 1047 return (error); 1048 1049 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 1050 if (inp_list == 0) 1051 return (ENOMEM); 1052 1053 INP_INFO_RLOCK(&V_ripcbinfo); 1054 for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n; 1055 inp = LIST_NEXT(inp, inp_list)) { 1056 INP_WLOCK(inp); 1057 if (inp->inp_gencnt <= gencnt && 1058 cr_canseeinpcb(req->td->td_ucred, inp) == 0) { 1059 in_pcbref(inp); 1060 inp_list[i++] = inp; 1061 } 1062 INP_WUNLOCK(inp); 1063 } 1064 INP_INFO_RUNLOCK(&V_ripcbinfo); 1065 n = i; 1066 1067 error = 0; 1068 for (i = 0; i < n; i++) { 1069 inp = inp_list[i]; 1070 INP_RLOCK(inp); 1071 if (inp->inp_gencnt <= gencnt) { 1072 struct xinpcb xi; 1073 1074 bzero(&xi, sizeof(xi)); 1075 xi.xi_len = sizeof xi; 1076 /* XXX should avoid extra copy */ 1077 bcopy(inp, &xi.xi_inp, sizeof *inp); 1078 if (inp->inp_socket) 1079 sotoxsocket(inp->inp_socket, &xi.xi_socket); 1080 INP_RUNLOCK(inp); 1081 error = SYSCTL_OUT(req, &xi, sizeof xi); 1082 } else 1083 INP_RUNLOCK(inp); 1084 } 1085 INP_INFO_WLOCK(&V_ripcbinfo); 1086 for (i = 0; i < n; i++) { 1087 inp = inp_list[i]; 1088 INP_RLOCK(inp); 1089 if (!in_pcbrele_rlocked(inp)) 1090 INP_RUNLOCK(inp); 1091 } 1092 INP_INFO_WUNLOCK(&V_ripcbinfo); 1093 1094 if (!error) { 1095 /* 1096 * Give the user an updated idea of our state. If the 1097 * generation differs from what we told her before, she knows 1098 * that something happened while we were processing this 1099 * request, and it might be necessary to retry. 1100 */ 1101 INP_INFO_RLOCK(&V_ripcbinfo); 1102 xig.xig_gen = V_ripcbinfo.ipi_gencnt; 1103 xig.xig_sogen = so_gencnt; 1104 xig.xig_count = V_ripcbinfo.ipi_count; 1105 INP_INFO_RUNLOCK(&V_ripcbinfo); 1106 error = SYSCTL_OUT(req, &xig, sizeof xig); 1107 } 1108 free(inp_list, M_TEMP); 1109 return (error); 1110} 1111 1112SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, 1113 CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0, 1114 rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); 1115 1116#ifdef INET 1117struct pr_usrreqs rip_usrreqs = { 1118 .pru_abort = rip_abort, 1119 .pru_attach = rip_attach, 1120 .pru_bind = rip_bind, 1121 .pru_connect = rip_connect, 1122 .pru_control = in_control, 1123 .pru_detach = rip_detach, 1124 .pru_disconnect = rip_disconnect, 1125 .pru_peeraddr = in_getpeeraddr, 1126 .pru_send = rip_send, 1127 .pru_shutdown = rip_shutdown, 1128 .pru_sockaddr = in_getsockaddr, 1129 .pru_sosetlabel = in_pcbsosetlabel, 1130 .pru_close = rip_close, 1131}; 1132#endif /* INET */ 1133