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