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