1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1988, 1993 5 * The Regents of the University of California. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include "opt_inet.h" 39 #include "opt_inet6.h" 40 #include "opt_ipsec.h" 41 #include "opt_route.h" 42 43 #include <sys/param.h> 44 #include <sys/jail.h> 45 #include <sys/kernel.h> 46 #include <sys/eventhandler.h> 47 #include <sys/lock.h> 48 #include <sys/malloc.h> 49 #include <sys/mbuf.h> 50 #include <sys/priv.h> 51 #include <sys/proc.h> 52 #include <sys/protosw.h> 53 #include <sys/rwlock.h> 54 #include <sys/signalvar.h> 55 #include <sys/socket.h> 56 #include <sys/socketvar.h> 57 #include <sys/sx.h> 58 #include <sys/sysctl.h> 59 #include <sys/systm.h> 60 61 #include <vm/uma.h> 62 63 #include <net/if.h> 64 #include <net/if_var.h> 65 #include <net/route.h> 66 #include <net/route/route_ctl.h> 67 #include <net/vnet.h> 68 69 #include <netinet/in.h> 70 #include <netinet/in_systm.h> 71 #include <netinet/in_fib.h> 72 #include <netinet/in_pcb.h> 73 #include <netinet/in_var.h> 74 #include <netinet/if_ether.h> 75 #include <netinet/ip.h> 76 #include <netinet/ip_var.h> 77 #include <netinet/ip_mroute.h> 78 #include <netinet/ip_icmp.h> 79 80 #include <netipsec/ipsec_support.h> 81 82 #include <machine/stdarg.h> 83 #include <security/mac/mac_framework.h> 84 85 extern ipproto_input_t *ip_protox[]; 86 87 VNET_DEFINE(int, ip_defttl) = IPDEFTTL; 88 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW, 89 &VNET_NAME(ip_defttl), 0, 90 "Maximum TTL on IP packets"); 91 92 VNET_DEFINE(struct inpcbinfo, ripcbinfo); 93 #define V_ripcbinfo VNET(ripcbinfo) 94 95 /* 96 * Control and data hooks for ipfw, dummynet, divert and so on. 97 * The data hooks are not used here but it is convenient 98 * to keep them all in one place. 99 */ 100 VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL; 101 VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL; 102 103 int (*ip_dn_ctl_ptr)(struct sockopt *); 104 int (*ip_dn_io_ptr)(struct mbuf **, struct ip_fw_args *); 105 void (*ip_divert_ptr)(struct mbuf *, bool); 106 int (*ng_ipfw_input_p)(struct mbuf **, struct ip_fw_args *, bool); 107 108 #ifdef INET 109 /* 110 * Hooks for multicast routing. They all default to NULL, so leave them not 111 * initialized and rely on BSS being set to 0. 112 */ 113 114 /* 115 * The socket used to communicate with the multicast routing daemon. 116 */ 117 VNET_DEFINE(struct socket *, ip_mrouter); 118 119 /* 120 * The various mrouter and rsvp functions. 121 */ 122 int (*ip_mrouter_set)(struct socket *, struct sockopt *); 123 int (*ip_mrouter_get)(struct socket *, struct sockopt *); 124 int (*ip_mrouter_done)(void); 125 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *, 126 struct ip_moptions *); 127 int (*mrt_ioctl)(u_long, caddr_t, int); 128 int (*legal_vif_num)(int); 129 u_long (*ip_mcast_src)(int); 130 131 int (*rsvp_input_p)(struct mbuf **, int *, int); 132 int (*ip_rsvp_vif)(struct socket *, struct sockopt *); 133 void (*ip_rsvp_force_done)(struct socket *); 134 #endif /* INET */ 135 136 u_long rip_sendspace = 9216; 137 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, 138 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); 139 140 u_long rip_recvspace = 9216; 141 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, 142 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams"); 143 144 /* 145 * Hash functions 146 */ 147 148 #define INP_PCBHASH_RAW_SIZE 256 149 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \ 150 (((proto) + (laddr) + (faddr)) % (mask) + 1) 151 152 #ifdef INET 153 static void 154 rip_inshash(struct inpcb *inp) 155 { 156 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 157 struct inpcbhead *pcbhash; 158 int hash; 159 160 INP_HASH_WLOCK_ASSERT(pcbinfo); 161 INP_WLOCK_ASSERT(inp); 162 163 if (inp->inp_ip_p != 0 && 164 inp->inp_laddr.s_addr != INADDR_ANY && 165 inp->inp_faddr.s_addr != INADDR_ANY) { 166 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr, 167 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask); 168 } else 169 hash = 0; 170 pcbhash = &pcbinfo->ipi_hashbase[hash]; 171 CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 172 } 173 174 static void 175 rip_delhash(struct inpcb *inp) 176 { 177 178 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 179 INP_WLOCK_ASSERT(inp); 180 181 CK_LIST_REMOVE(inp, inp_hash); 182 } 183 #endif /* INET */ 184 185 INPCBSTORAGE_DEFINE(ripcbstor, "rawinp", "ripcb", "rip", "riphash"); 186 187 static void 188 rip_init(void *arg __unused) 189 { 190 191 in_pcbinfo_init(&V_ripcbinfo, &ripcbstor, INP_PCBHASH_RAW_SIZE, 1); 192 } 193 VNET_SYSINIT(rip_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rip_init, NULL); 194 195 #ifdef VIMAGE 196 static void 197 rip_destroy(void *unused __unused) 198 { 199 200 in_pcbinfo_destroy(&V_ripcbinfo); 201 } 202 VNET_SYSUNINIT(raw_ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, rip_destroy, NULL); 203 #endif 204 205 #ifdef INET 206 static int 207 rip_append(struct inpcb *inp, struct ip *ip, struct mbuf *m, 208 struct sockaddr_in *ripsrc) 209 { 210 struct socket *so = inp->inp_socket; 211 struct mbuf *n, *opts = NULL; 212 213 INP_LOCK_ASSERT(inp); 214 215 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 216 /* check AH/ESP integrity. */ 217 if (IPSEC_ENABLED(ipv4) && IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) 218 return (0); 219 #endif /* IPSEC */ 220 #ifdef MAC 221 if (mac_inpcb_check_deliver(inp, m) != 0) 222 return (0); 223 #endif 224 /* Check the minimum TTL for socket. */ 225 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) 226 return (0); 227 228 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) 229 return (0); 230 231 if ((inp->inp_flags & INP_CONTROLOPTS) || 232 (so->so_options & (SO_TIMESTAMP | SO_BINTIME))) 233 ip_savecontrol(inp, &opts, ip, n); 234 SOCKBUF_LOCK(&so->so_rcv); 235 if (sbappendaddr_locked(&so->so_rcv, 236 (struct sockaddr *)ripsrc, n, opts) == 0) { 237 soroverflow_locked(so); 238 m_freem(n); 239 if (opts) 240 m_freem(opts); 241 return (0); 242 } 243 sorwakeup_locked(so); 244 245 return (1); 246 } 247 248 struct rip_inp_match_ctx { 249 struct ip *ip; 250 int proto; 251 }; 252 253 static bool 254 rip_inp_match1(const struct inpcb *inp, void *v) 255 { 256 struct rip_inp_match_ctx *ctx = v; 257 258 if (inp->inp_ip_p != ctx->proto) 259 return (false); 260 #ifdef INET6 261 /* XXX inp locking */ 262 if ((inp->inp_vflag & INP_IPV4) == 0) 263 return (false); 264 #endif 265 if (inp->inp_laddr.s_addr != ctx->ip->ip_dst.s_addr) 266 return (false); 267 if (inp->inp_faddr.s_addr != ctx->ip->ip_src.s_addr) 268 return (false); 269 return (true); 270 } 271 272 static bool 273 rip_inp_match2(const struct inpcb *inp, void *v) 274 { 275 struct rip_inp_match_ctx *ctx = v; 276 277 if (inp->inp_ip_p && inp->inp_ip_p != ctx->proto) 278 return (false); 279 #ifdef INET6 280 /* XXX inp locking */ 281 if ((inp->inp_vflag & INP_IPV4) == 0) 282 return (false); 283 #endif 284 if (!in_nullhost(inp->inp_laddr) && 285 !in_hosteq(inp->inp_laddr, ctx->ip->ip_dst)) 286 return (false); 287 if (!in_nullhost(inp->inp_faddr) && 288 !in_hosteq(inp->inp_faddr, ctx->ip->ip_src)) 289 return (false); 290 return (true); 291 } 292 293 /* 294 * Setup generic address and protocol structures for raw_input routine, then 295 * pass them along with mbuf chain. 296 */ 297 int 298 rip_input(struct mbuf **mp, int *offp, int proto) 299 { 300 struct rip_inp_match_ctx ctx = { 301 .ip = mtod(*mp, struct ip *), 302 .proto = proto, 303 }; 304 struct inpcb_iterator inpi = INP_ITERATOR(&V_ripcbinfo, 305 INPLOOKUP_RLOCKPCB, rip_inp_match1, &ctx); 306 struct ifnet *ifp; 307 struct mbuf *m = *mp; 308 struct inpcb *inp; 309 struct sockaddr_in ripsrc; 310 int appended; 311 312 *mp = NULL; 313 appended = 0; 314 315 bzero(&ripsrc, sizeof(ripsrc)); 316 ripsrc.sin_len = sizeof(ripsrc); 317 ripsrc.sin_family = AF_INET; 318 ripsrc.sin_addr = ctx.ip->ip_src; 319 320 ifp = m->m_pkthdr.rcvif; 321 322 inpi.hash = INP_PCBHASH_RAW(proto, ctx.ip->ip_src.s_addr, 323 ctx.ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask); 324 while ((inp = inp_next(&inpi)) != NULL) { 325 INP_RLOCK_ASSERT(inp); 326 if (jailed_without_vnet(inp->inp_cred) && 327 prison_check_ip4(inp->inp_cred, &ctx.ip->ip_dst) != 0) { 328 /* 329 * XXX: If faddr was bound to multicast group, 330 * jailed raw socket will drop datagram. 331 */ 332 continue; 333 } 334 appended += rip_append(inp, ctx.ip, m, &ripsrc); 335 } 336 337 inpi.hash = 0; 338 inpi.match = rip_inp_match2; 339 MPASS(inpi.inp == NULL); 340 while ((inp = inp_next(&inpi)) != NULL) { 341 INP_RLOCK_ASSERT(inp); 342 if (jailed_without_vnet(inp->inp_cred) && 343 !IN_MULTICAST(ntohl(ctx.ip->ip_dst.s_addr)) && 344 prison_check_ip4(inp->inp_cred, &ctx.ip->ip_dst) != 0) 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 continue; 351 /* 352 * If this raw socket has multicast state, and we 353 * have received a multicast, check if this socket 354 * should receive it, as multicast filtering is now 355 * the responsibility of the transport layer. 356 */ 357 if (inp->inp_moptions != NULL && 358 IN_MULTICAST(ntohl(ctx.ip->ip_dst.s_addr))) { 359 /* 360 * If the incoming datagram is for IGMP, allow it 361 * through unconditionally to the raw socket. 362 * 363 * In the case of IGMPv2, we may not have explicitly 364 * joined the group, and may have set IFF_ALLMULTI 365 * on the interface. imo_multi_filter() may discard 366 * control traffic we actually need to see. 367 * 368 * Userland multicast routing daemons should continue 369 * filter the control traffic appropriately. 370 */ 371 int blocked; 372 373 blocked = MCAST_PASS; 374 if (proto != IPPROTO_IGMP) { 375 struct sockaddr_in group; 376 377 bzero(&group, sizeof(struct sockaddr_in)); 378 group.sin_len = sizeof(struct sockaddr_in); 379 group.sin_family = AF_INET; 380 group.sin_addr = ctx.ip->ip_dst; 381 382 blocked = imo_multi_filter(inp->inp_moptions, 383 ifp, 384 (struct sockaddr *)&group, 385 (struct sockaddr *)&ripsrc); 386 } 387 388 if (blocked != MCAST_PASS) { 389 IPSTAT_INC(ips_notmember); 390 continue; 391 } 392 } 393 appended += rip_append(inp, ctx.ip, m, &ripsrc); 394 } 395 if (appended == 0 && ip_protox[ctx.ip->ip_p] == rip_input) { 396 IPSTAT_INC(ips_noproto); 397 IPSTAT_DEC(ips_delivered); 398 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0); 399 } else 400 m_freem(m); 401 return (IPPROTO_DONE); 402 } 403 404 /* 405 * Generate IP header and pass packet to ip_output. Tack on options user may 406 * have setup with control call. 407 */ 408 static int 409 rip_send(struct socket *so, int pruflags, struct mbuf *m, struct sockaddr *nam, 410 struct mbuf *control, struct thread *td) 411 { 412 struct epoch_tracker et; 413 struct ip *ip; 414 struct inpcb *inp; 415 in_addr_t *dst; 416 int error, flags, cnt, hlen; 417 u_char opttype, optlen, *cp; 418 419 inp = sotoinpcb(so); 420 KASSERT(inp != NULL, ("rip_send: inp == NULL")); 421 422 if (control != NULL) { 423 m_freem(control); 424 control = NULL; 425 } 426 427 if (so->so_state & SS_ISCONNECTED) { 428 if (nam) { 429 error = EISCONN; 430 m_freem(m); 431 return (error); 432 } 433 dst = &inp->inp_faddr.s_addr; 434 } else { 435 if (nam == NULL) 436 error = ENOTCONN; 437 else if (nam->sa_family != AF_INET) 438 error = EAFNOSUPPORT; 439 else if (nam->sa_len != sizeof(struct sockaddr_in)) 440 error = EINVAL; 441 else 442 error = 0; 443 if (error != 0) { 444 m_freem(m); 445 return (error); 446 } 447 dst = &((struct sockaddr_in *)nam)->sin_addr.s_addr; 448 } 449 450 flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) | 451 IP_ALLOWBROADCAST; 452 453 /* 454 * If the user handed us a complete IP packet, use it. Otherwise, 455 * allocate an mbuf for a header and fill it in. 456 */ 457 if ((inp->inp_flags & INP_HDRINCL) == 0) { 458 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { 459 m_freem(m); 460 return(EMSGSIZE); 461 } 462 M_PREPEND(m, sizeof(struct ip), M_NOWAIT); 463 if (m == NULL) 464 return(ENOBUFS); 465 466 INP_RLOCK(inp); 467 ip = mtod(m, struct ip *); 468 ip->ip_tos = inp->inp_ip_tos; 469 if (inp->inp_flags & INP_DONTFRAG) 470 ip->ip_off = htons(IP_DF); 471 else 472 ip->ip_off = htons(0); 473 ip->ip_p = inp->inp_ip_p; 474 ip->ip_len = htons(m->m_pkthdr.len); 475 ip->ip_src = inp->inp_laddr; 476 ip->ip_dst.s_addr = *dst; 477 #ifdef ROUTE_MPATH 478 if (CALC_FLOWID_OUTBOUND) { 479 uint32_t hash_type, hash_val; 480 481 hash_val = fib4_calc_software_hash(ip->ip_src, 482 ip->ip_dst, 0, 0, ip->ip_p, &hash_type); 483 m->m_pkthdr.flowid = hash_val; 484 M_HASHTYPE_SET(m, hash_type); 485 flags |= IP_NODEFAULTFLOWID; 486 } 487 #endif 488 if (jailed(inp->inp_cred)) { 489 /* 490 * prison_local_ip4() would be good enough but would 491 * let a source of INADDR_ANY pass, which we do not 492 * want to see from jails. 493 */ 494 if (ip->ip_src.s_addr == INADDR_ANY) { 495 NET_EPOCH_ENTER(et); 496 error = in_pcbladdr(inp, &ip->ip_dst, 497 &ip->ip_src, inp->inp_cred); 498 NET_EPOCH_EXIT(et); 499 } else { 500 error = prison_local_ip4(inp->inp_cred, 501 &ip->ip_src); 502 } 503 if (error != 0) { 504 INP_RUNLOCK(inp); 505 m_freem(m); 506 return (error); 507 } 508 } 509 ip->ip_ttl = inp->inp_ip_ttl; 510 } else { 511 if (m->m_pkthdr.len > IP_MAXPACKET) { 512 m_freem(m); 513 return (EMSGSIZE); 514 } 515 if (m->m_pkthdr.len < sizeof(*ip)) { 516 m_freem(m); 517 return (EINVAL); 518 } 519 m = m_pullup(m, sizeof(*ip)); 520 if (m == NULL) 521 return (ENOMEM); 522 ip = mtod(m, struct ip *); 523 hlen = ip->ip_hl << 2; 524 if (m->m_len < hlen) { 525 m = m_pullup(m, hlen); 526 if (m == NULL) 527 return (EINVAL); 528 ip = mtod(m, struct ip *); 529 } 530 #ifdef ROUTE_MPATH 531 if (CALC_FLOWID_OUTBOUND) { 532 uint32_t hash_type, hash_val; 533 534 hash_val = fib4_calc_software_hash(ip->ip_dst, 535 ip->ip_src, 0, 0, ip->ip_p, &hash_type); 536 m->m_pkthdr.flowid = hash_val; 537 M_HASHTYPE_SET(m, hash_type); 538 flags |= IP_NODEFAULTFLOWID; 539 } 540 #endif 541 INP_RLOCK(inp); 542 /* 543 * Don't allow both user specified and setsockopt options, 544 * and don't allow packet length sizes that will crash. 545 */ 546 if ((hlen < sizeof (*ip)) 547 || ((hlen > sizeof (*ip)) && inp->inp_options) 548 || (ntohs(ip->ip_len) != m->m_pkthdr.len)) { 549 INP_RUNLOCK(inp); 550 m_freem(m); 551 return (EINVAL); 552 } 553 error = prison_check_ip4(inp->inp_cred, &ip->ip_src); 554 if (error != 0) { 555 INP_RUNLOCK(inp); 556 m_freem(m); 557 return (error); 558 } 559 /* 560 * Don't allow IP options which do not have the required 561 * structure as specified in section 3.1 of RFC 791 on 562 * pages 15-23. 563 */ 564 cp = (u_char *)(ip + 1); 565 cnt = hlen - sizeof (struct ip); 566 for (; cnt > 0; cnt -= optlen, cp += optlen) { 567 opttype = cp[IPOPT_OPTVAL]; 568 if (opttype == IPOPT_EOL) 569 break; 570 if (opttype == IPOPT_NOP) { 571 optlen = 1; 572 continue; 573 } 574 if (cnt < IPOPT_OLEN + sizeof(u_char)) { 575 INP_RUNLOCK(inp); 576 m_freem(m); 577 return (EINVAL); 578 } 579 optlen = cp[IPOPT_OLEN]; 580 if (optlen < IPOPT_OLEN + sizeof(u_char) || 581 optlen > cnt) { 582 INP_RUNLOCK(inp); 583 m_freem(m); 584 return (EINVAL); 585 } 586 } 587 /* 588 * This doesn't allow application to specify ID of zero, 589 * but we got this limitation from the beginning of history. 590 */ 591 if (ip->ip_id == 0) 592 ip_fillid(ip); 593 594 /* 595 * XXX prevent ip_output from overwriting header fields. 596 */ 597 flags |= IP_RAWOUTPUT; 598 IPSTAT_INC(ips_rawout); 599 } 600 601 if (inp->inp_flags & INP_ONESBCAST) 602 flags |= IP_SENDONES; 603 604 #ifdef MAC 605 mac_inpcb_create_mbuf(inp, m); 606 #endif 607 608 NET_EPOCH_ENTER(et); 609 error = ip_output(m, inp->inp_options, NULL, flags, 610 inp->inp_moptions, inp); 611 NET_EPOCH_EXIT(et); 612 INP_RUNLOCK(inp); 613 return (error); 614 } 615 616 /* 617 * Raw IP socket option processing. 618 * 619 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could 620 * only be created by a privileged process, and as such, socket option 621 * operations to manage system properties on any raw socket were allowed to 622 * take place without explicit additional access control checks. However, 623 * raw sockets can now also be created in jail(), and therefore explicit 624 * checks are now required. Likewise, raw sockets can be used by a process 625 * after it gives up privilege, so some caution is required. For options 626 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be 627 * performed in ip_ctloutput() and therefore no check occurs here. 628 * Unilaterally checking priv_check() here breaks normal IP socket option 629 * operations on raw sockets. 630 * 631 * When adding new socket options here, make sure to add access control 632 * checks here as necessary. 633 * 634 * XXX-BZ inp locking? 635 */ 636 int 637 rip_ctloutput(struct socket *so, struct sockopt *sopt) 638 { 639 struct inpcb *inp = sotoinpcb(so); 640 int error, optval; 641 642 if (sopt->sopt_level != IPPROTO_IP) { 643 if ((sopt->sopt_level == SOL_SOCKET) && 644 (sopt->sopt_name == SO_SETFIB)) { 645 inp->inp_inc.inc_fibnum = so->so_fibnum; 646 return (0); 647 } 648 return (EINVAL); 649 } 650 651 error = 0; 652 switch (sopt->sopt_dir) { 653 case SOPT_GET: 654 switch (sopt->sopt_name) { 655 case IP_HDRINCL: 656 optval = inp->inp_flags & INP_HDRINCL; 657 error = sooptcopyout(sopt, &optval, sizeof optval); 658 break; 659 660 case IP_FW3: /* generic ipfw v.3 functions */ 661 case IP_FW_ADD: /* ADD actually returns the body... */ 662 case IP_FW_GET: 663 case IP_FW_TABLE_GETSIZE: 664 case IP_FW_TABLE_LIST: 665 case IP_FW_NAT_GET_CONFIG: 666 case IP_FW_NAT_GET_LOG: 667 if (V_ip_fw_ctl_ptr != NULL) 668 error = V_ip_fw_ctl_ptr(sopt); 669 else 670 error = ENOPROTOOPT; 671 break; 672 673 case IP_DUMMYNET3: /* generic dummynet v.3 functions */ 674 case IP_DUMMYNET_GET: 675 if (ip_dn_ctl_ptr != NULL) 676 error = ip_dn_ctl_ptr(sopt); 677 else 678 error = ENOPROTOOPT; 679 break ; 680 681 case MRT_INIT: 682 case MRT_DONE: 683 case MRT_ADD_VIF: 684 case MRT_DEL_VIF: 685 case MRT_ADD_MFC: 686 case MRT_DEL_MFC: 687 case MRT_VERSION: 688 case MRT_ASSERT: 689 case MRT_API_SUPPORT: 690 case MRT_API_CONFIG: 691 case MRT_ADD_BW_UPCALL: 692 case MRT_DEL_BW_UPCALL: 693 error = priv_check(curthread, PRIV_NETINET_MROUTE); 694 if (error != 0) 695 return (error); 696 if (inp->inp_ip_p != IPPROTO_IGMP) 697 return (EOPNOTSUPP); 698 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) : 699 EOPNOTSUPP; 700 break; 701 702 default: 703 error = ip_ctloutput(so, sopt); 704 break; 705 } 706 break; 707 708 case SOPT_SET: 709 switch (sopt->sopt_name) { 710 case IP_HDRINCL: 711 error = sooptcopyin(sopt, &optval, sizeof optval, 712 sizeof optval); 713 if (error) 714 break; 715 if (optval) 716 inp->inp_flags |= INP_HDRINCL; 717 else 718 inp->inp_flags &= ~INP_HDRINCL; 719 break; 720 721 case IP_FW3: /* generic ipfw v.3 functions */ 722 case IP_FW_ADD: 723 case IP_FW_DEL: 724 case IP_FW_FLUSH: 725 case IP_FW_ZERO: 726 case IP_FW_RESETLOG: 727 case IP_FW_TABLE_ADD: 728 case IP_FW_TABLE_DEL: 729 case IP_FW_TABLE_FLUSH: 730 case IP_FW_NAT_CFG: 731 case IP_FW_NAT_DEL: 732 if (V_ip_fw_ctl_ptr != NULL) 733 error = V_ip_fw_ctl_ptr(sopt); 734 else 735 error = ENOPROTOOPT; 736 break; 737 738 case IP_DUMMYNET3: /* generic dummynet v.3 functions */ 739 case IP_DUMMYNET_CONFIGURE: 740 case IP_DUMMYNET_DEL: 741 case IP_DUMMYNET_FLUSH: 742 if (ip_dn_ctl_ptr != NULL) 743 error = ip_dn_ctl_ptr(sopt); 744 else 745 error = ENOPROTOOPT ; 746 break ; 747 748 case IP_RSVP_ON: 749 error = priv_check(curthread, PRIV_NETINET_MROUTE); 750 if (error != 0) 751 return (error); 752 if (inp->inp_ip_p != IPPROTO_RSVP) 753 return (EOPNOTSUPP); 754 error = ip_rsvp_init(so); 755 break; 756 757 case IP_RSVP_OFF: 758 error = priv_check(curthread, PRIV_NETINET_MROUTE); 759 if (error != 0) 760 return (error); 761 error = ip_rsvp_done(); 762 break; 763 764 case IP_RSVP_VIF_ON: 765 case IP_RSVP_VIF_OFF: 766 error = priv_check(curthread, PRIV_NETINET_MROUTE); 767 if (error != 0) 768 return (error); 769 if (inp->inp_ip_p != IPPROTO_RSVP) 770 return (EOPNOTSUPP); 771 error = ip_rsvp_vif ? 772 ip_rsvp_vif(so, sopt) : EINVAL; 773 break; 774 775 case MRT_INIT: 776 case MRT_DONE: 777 case MRT_ADD_VIF: 778 case MRT_DEL_VIF: 779 case MRT_ADD_MFC: 780 case MRT_DEL_MFC: 781 case MRT_VERSION: 782 case MRT_ASSERT: 783 case MRT_API_SUPPORT: 784 case MRT_API_CONFIG: 785 case MRT_ADD_BW_UPCALL: 786 case MRT_DEL_BW_UPCALL: 787 error = priv_check(curthread, PRIV_NETINET_MROUTE); 788 if (error != 0) 789 return (error); 790 if (inp->inp_ip_p != IPPROTO_IGMP) 791 return (EOPNOTSUPP); 792 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) : 793 EOPNOTSUPP; 794 break; 795 796 default: 797 error = ip_ctloutput(so, sopt); 798 break; 799 } 800 break; 801 } 802 803 return (error); 804 } 805 806 void 807 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip) 808 { 809 810 switch (cmd) { 811 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 812 case PRC_MSGSIZE: 813 if (IPSEC_ENABLED(ipv4)) 814 IPSEC_CTLINPUT(ipv4, cmd, sa, vip); 815 break; 816 #endif 817 } 818 } 819 820 static int 821 rip_attach(struct socket *so, int proto, struct thread *td) 822 { 823 struct inpcb *inp; 824 int error; 825 826 inp = sotoinpcb(so); 827 KASSERT(inp == NULL, ("rip_attach: inp != NULL")); 828 829 error = priv_check(td, PRIV_NETINET_RAW); 830 if (error) 831 return (error); 832 if (proto >= IPPROTO_MAX || proto < 0) 833 return EPROTONOSUPPORT; 834 error = soreserve(so, rip_sendspace, rip_recvspace); 835 if (error) 836 return (error); 837 error = in_pcballoc(so, &V_ripcbinfo); 838 if (error) 839 return (error); 840 inp = (struct inpcb *)so->so_pcb; 841 inp->inp_ip_p = proto; 842 inp->inp_ip_ttl = V_ip_defttl; 843 INP_HASH_WLOCK(&V_ripcbinfo); 844 rip_inshash(inp); 845 INP_HASH_WUNLOCK(&V_ripcbinfo); 846 INP_WUNLOCK(inp); 847 return (0); 848 } 849 850 static void 851 rip_detach(struct socket *so) 852 { 853 struct inpcb *inp; 854 855 inp = sotoinpcb(so); 856 KASSERT(inp != NULL, ("rip_detach: inp == NULL")); 857 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 858 ("rip_detach: not closed")); 859 860 /* Disable mrouter first */ 861 if (so == V_ip_mrouter && ip_mrouter_done) 862 ip_mrouter_done(); 863 864 INP_WLOCK(inp); 865 INP_HASH_WLOCK(&V_ripcbinfo); 866 rip_delhash(inp); 867 INP_HASH_WUNLOCK(&V_ripcbinfo); 868 869 if (ip_rsvp_force_done) 870 ip_rsvp_force_done(so); 871 if (so == V_ip_rsvpd) 872 ip_rsvp_done(); 873 in_pcbdetach(inp); 874 in_pcbfree(inp); 875 } 876 877 static void 878 rip_dodisconnect(struct socket *so, struct inpcb *inp) 879 { 880 struct inpcbinfo *pcbinfo; 881 882 pcbinfo = inp->inp_pcbinfo; 883 INP_WLOCK(inp); 884 INP_HASH_WLOCK(pcbinfo); 885 rip_delhash(inp); 886 inp->inp_faddr.s_addr = INADDR_ANY; 887 rip_inshash(inp); 888 INP_HASH_WUNLOCK(pcbinfo); 889 SOCK_LOCK(so); 890 so->so_state &= ~SS_ISCONNECTED; 891 SOCK_UNLOCK(so); 892 INP_WUNLOCK(inp); 893 } 894 895 static void 896 rip_abort(struct socket *so) 897 { 898 struct inpcb *inp; 899 900 inp = sotoinpcb(so); 901 KASSERT(inp != NULL, ("rip_abort: inp == NULL")); 902 903 rip_dodisconnect(so, inp); 904 } 905 906 static void 907 rip_close(struct socket *so) 908 { 909 struct inpcb *inp; 910 911 inp = sotoinpcb(so); 912 KASSERT(inp != NULL, ("rip_close: inp == NULL")); 913 914 rip_dodisconnect(so, inp); 915 } 916 917 static int 918 rip_disconnect(struct socket *so) 919 { 920 struct inpcb *inp; 921 922 if ((so->so_state & SS_ISCONNECTED) == 0) 923 return (ENOTCONN); 924 925 inp = sotoinpcb(so); 926 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL")); 927 928 rip_dodisconnect(so, inp); 929 return (0); 930 } 931 932 static int 933 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 934 { 935 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 936 struct inpcb *inp; 937 int error; 938 939 if (nam->sa_family != AF_INET) 940 return (EAFNOSUPPORT); 941 if (nam->sa_len != sizeof(*addr)) 942 return (EINVAL); 943 944 error = prison_check_ip4(td->td_ucred, &addr->sin_addr); 945 if (error != 0) 946 return (error); 947 948 inp = sotoinpcb(so); 949 KASSERT(inp != NULL, ("rip_bind: inp == NULL")); 950 951 if (CK_STAILQ_EMPTY(&V_ifnet) || 952 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) || 953 (addr->sin_addr.s_addr && 954 (inp->inp_flags & INP_BINDANY) == 0 && 955 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0)) 956 return (EADDRNOTAVAIL); 957 958 INP_WLOCK(inp); 959 INP_HASH_WLOCK(&V_ripcbinfo); 960 rip_delhash(inp); 961 inp->inp_laddr = addr->sin_addr; 962 rip_inshash(inp); 963 INP_HASH_WUNLOCK(&V_ripcbinfo); 964 INP_WUNLOCK(inp); 965 return (0); 966 } 967 968 static int 969 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 970 { 971 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 972 struct inpcb *inp; 973 974 if (nam->sa_len != sizeof(*addr)) 975 return (EINVAL); 976 if (CK_STAILQ_EMPTY(&V_ifnet)) 977 return (EADDRNOTAVAIL); 978 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) 979 return (EAFNOSUPPORT); 980 981 inp = sotoinpcb(so); 982 KASSERT(inp != NULL, ("rip_connect: inp == NULL")); 983 984 INP_WLOCK(inp); 985 INP_HASH_WLOCK(&V_ripcbinfo); 986 rip_delhash(inp); 987 inp->inp_faddr = addr->sin_addr; 988 rip_inshash(inp); 989 INP_HASH_WUNLOCK(&V_ripcbinfo); 990 soisconnected(so); 991 INP_WUNLOCK(inp); 992 return (0); 993 } 994 995 static int 996 rip_shutdown(struct socket *so) 997 { 998 struct inpcb *inp; 999 1000 inp = sotoinpcb(so); 1001 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL")); 1002 1003 INP_WLOCK(inp); 1004 socantsendmore(so); 1005 INP_WUNLOCK(inp); 1006 return (0); 1007 } 1008 #endif /* INET */ 1009 1010 static int 1011 rip_pcblist(SYSCTL_HANDLER_ARGS) 1012 { 1013 struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_ripcbinfo, 1014 INPLOOKUP_RLOCKPCB); 1015 struct xinpgen xig; 1016 struct inpcb *inp; 1017 int error; 1018 1019 if (req->newptr != 0) 1020 return (EPERM); 1021 1022 if (req->oldptr == 0) { 1023 int n; 1024 1025 n = V_ripcbinfo.ipi_count; 1026 n += imax(n / 8, 10); 1027 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb); 1028 return (0); 1029 } 1030 1031 if ((error = sysctl_wire_old_buffer(req, 0)) != 0) 1032 return (error); 1033 1034 bzero(&xig, sizeof(xig)); 1035 xig.xig_len = sizeof xig; 1036 xig.xig_count = V_ripcbinfo.ipi_count; 1037 xig.xig_gen = V_ripcbinfo.ipi_gencnt; 1038 xig.xig_sogen = so_gencnt; 1039 error = SYSCTL_OUT(req, &xig, sizeof xig); 1040 if (error) 1041 return (error); 1042 1043 while ((inp = inp_next(&inpi)) != NULL) { 1044 if (inp->inp_gencnt <= xig.xig_gen && 1045 cr_canseeinpcb(req->td->td_ucred, inp) == 0) { 1046 struct xinpcb xi; 1047 1048 in_pcbtoxinpcb(inp, &xi); 1049 error = SYSCTL_OUT(req, &xi, sizeof xi); 1050 if (error) { 1051 INP_RUNLOCK(inp); 1052 break; 1053 } 1054 } 1055 } 1056 1057 if (!error) { 1058 /* 1059 * Give the user an updated idea of our state. If the 1060 * generation differs from what we told her before, she knows 1061 * that something happened while we were processing this 1062 * request, and it might be necessary to retry. 1063 */ 1064 xig.xig_gen = V_ripcbinfo.ipi_gencnt; 1065 xig.xig_sogen = so_gencnt; 1066 xig.xig_count = V_ripcbinfo.ipi_count; 1067 error = SYSCTL_OUT(req, &xig, sizeof xig); 1068 } 1069 1070 return (error); 1071 } 1072 1073 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, 1074 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 1075 rip_pcblist, "S,xinpcb", 1076 "List of active raw IP sockets"); 1077 1078 #ifdef INET 1079 struct protosw rip_protosw = { 1080 .pr_type = SOCK_RAW, 1081 .pr_flags = PR_ATOMIC|PR_ADDR, 1082 .pr_ctloutput = rip_ctloutput, 1083 .pr_abort = rip_abort, 1084 .pr_attach = rip_attach, 1085 .pr_bind = rip_bind, 1086 .pr_connect = rip_connect, 1087 .pr_control = in_control, 1088 .pr_detach = rip_detach, 1089 .pr_disconnect = rip_disconnect, 1090 .pr_peeraddr = in_getpeeraddr, 1091 .pr_send = rip_send, 1092 .pr_shutdown = rip_shutdown, 1093 .pr_sockaddr = in_getsockaddr, 1094 .pr_sosetlabel = in_pcbsosetlabel, 1095 .pr_close = rip_close 1096 }; 1097 #endif /* INET */ 1098