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