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