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