1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_inet.h" 36 #include "opt_inet6.h" 37 #include "opt_sctp.h" 38 #ifndef INET 39 #error "IPDIVERT requires INET" 40 #endif 41 42 #include <sys/param.h> 43 #include <sys/eventhandler.h> 44 #include <sys/kernel.h> 45 #include <sys/lock.h> 46 #include <sys/malloc.h> 47 #include <sys/mbuf.h> 48 #include <sys/module.h> 49 #include <sys/kernel.h> 50 #include <sys/priv.h> 51 #include <sys/proc.h> 52 #include <sys/protosw.h> 53 #include <sys/socket.h> 54 #include <sys/socketvar.h> 55 #include <sys/sysctl.h> 56 #include <net/vnet.h> 57 58 #include <net/if.h> 59 #include <net/if_var.h> 60 #include <net/netisr.h> 61 62 #include <netinet/in.h> 63 #include <netinet/in_pcb.h> 64 #include <netinet/in_systm.h> 65 #include <netinet/in_var.h> 66 #include <netinet/ip.h> 67 #include <netinet/ip_var.h> 68 #ifdef INET6 69 #include <netinet/ip6.h> 70 #include <netinet6/ip6_var.h> 71 #endif 72 #if defined(SCTP) || defined(SCTP_SUPPORT) 73 #include <netinet/sctp_crc32.h> 74 #endif 75 76 #include <security/mac/mac_framework.h> 77 /* 78 * Divert sockets 79 */ 80 81 /* 82 * Allocate enough space to hold a full IP packet 83 */ 84 #define DIVSNDQ (65536 + 100) 85 #define DIVRCVQ (65536 + 100) 86 87 /* 88 * Divert sockets work in conjunction with ipfw or other packet filters, 89 * see the divert(4) manpage for features. 90 * Packets are selected by the packet filter and tagged with an 91 * MTAG_IPFW_RULE tag carrying the 'divert port' number (as set by 92 * the packet filter) and information on the matching filter rule for 93 * subsequent reinjection. The divert_port is used to put the packet 94 * on the corresponding divert socket, while the rule number is passed 95 * up (at least partially) as the sin_port in the struct sockaddr. 96 * 97 * Packets written to the divert socket carry in sin_addr a 98 * destination address, and in sin_port the number of the filter rule 99 * after which to continue processing. 100 * If the destination address is INADDR_ANY, the packet is treated as 101 * as outgoing and sent to ip_output(); otherwise it is treated as 102 * incoming and sent to ip_input(). 103 * Further, sin_zero carries some information on the interface, 104 * which can be used in the reinject -- see comments in the code. 105 * 106 * On reinjection, processing in ip_input() and ip_output() 107 * will be exactly the same as for the original packet, except that 108 * packet filter processing will start at the rule number after the one 109 * written in the sin_port (ipfw does not allow a rule #0, so sin_port=0 110 * will apply the entire ruleset to the packet). 111 */ 112 113 /* Internal variables. */ 114 VNET_DEFINE_STATIC(struct inpcbhead, divcb); 115 VNET_DEFINE_STATIC(struct inpcbinfo, divcbinfo); 116 117 #define V_divcb VNET(divcb) 118 #define V_divcbinfo VNET(divcbinfo) 119 120 static u_long div_sendspace = DIVSNDQ; /* XXX sysctl ? */ 121 static u_long div_recvspace = DIVRCVQ; /* XXX sysctl ? */ 122 123 static eventhandler_tag ip_divert_event_tag; 124 125 static int div_output_inbound(int fmaily, struct socket *so, struct mbuf *m, 126 struct sockaddr_in *sin); 127 static int div_output_outbound(int family, struct socket *so, struct mbuf *m); 128 129 /* 130 * Initialize divert connection block queue. 131 */ 132 static void 133 div_zone_change(void *tag) 134 { 135 136 uma_zone_set_max(V_divcbinfo.ipi_zone, maxsockets); 137 } 138 139 static int 140 div_inpcb_init(void *mem, int size, int flags) 141 { 142 struct inpcb *inp = mem; 143 144 INP_LOCK_INIT(inp, "inp", "divinp"); 145 return (0); 146 } 147 148 static void 149 div_init(void) 150 { 151 152 /* 153 * XXX We don't use the hash list for divert IP, but it's easier to 154 * allocate one-entry hash lists than it is to check all over the 155 * place for hashbase == NULL. 156 */ 157 in_pcbinfo_init(&V_divcbinfo, "div", &V_divcb, 1, 1, "divcb", 158 div_inpcb_init, IPI_HASHFIELDS_NONE); 159 } 160 161 static void 162 div_destroy(void *unused __unused) 163 { 164 165 in_pcbinfo_destroy(&V_divcbinfo); 166 } 167 VNET_SYSUNINIT(divert, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, 168 div_destroy, NULL); 169 170 /* 171 * IPPROTO_DIVERT is not in the real IP protocol number space; this 172 * function should never be called. Just in case, drop any packets. 173 */ 174 static int 175 div_input(struct mbuf **mp, int *offp, int proto) 176 { 177 struct mbuf *m = *mp; 178 179 KMOD_IPSTAT_INC(ips_noproto); 180 m_freem(m); 181 return (IPPROTO_DONE); 182 } 183 184 /* 185 * Divert a packet by passing it up to the divert socket at port 'port'. 186 * 187 * Setup generic address and protocol structures for div_input routine, 188 * then pass them along with mbuf chain. 189 */ 190 static void 191 divert_packet(struct mbuf *m, bool incoming) 192 { 193 struct ip *ip; 194 struct inpcb *inp; 195 struct socket *sa; 196 u_int16_t nport; 197 struct sockaddr_in divsrc; 198 struct m_tag *mtag; 199 200 NET_EPOCH_ASSERT(); 201 202 mtag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL); 203 if (mtag == NULL) { 204 m_freem(m); 205 return; 206 } 207 /* Assure header */ 208 if (m->m_len < sizeof(struct ip) && 209 (m = m_pullup(m, sizeof(struct ip))) == NULL) 210 return; 211 ip = mtod(m, struct ip *); 212 213 /* Delayed checksums are currently not compatible with divert. */ 214 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 215 in_delayed_cksum(m); 216 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 217 } 218 #if defined(SCTP) || defined(SCTP_SUPPORT) 219 if (m->m_pkthdr.csum_flags & CSUM_SCTP) { 220 sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 221 m->m_pkthdr.csum_flags &= ~CSUM_SCTP; 222 } 223 #endif 224 #ifdef INET6 225 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 226 in6_delayed_cksum(m, m->m_pkthdr.len - 227 sizeof(struct ip6_hdr), sizeof(struct ip6_hdr)); 228 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6; 229 } 230 #if defined(SCTP) || defined(SCTP_SUPPORT) 231 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) { 232 sctp_delayed_cksum(m, sizeof(struct ip6_hdr)); 233 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6; 234 } 235 #endif 236 #endif /* INET6 */ 237 bzero(&divsrc, sizeof(divsrc)); 238 divsrc.sin_len = sizeof(divsrc); 239 divsrc.sin_family = AF_INET; 240 /* record matching rule, in host format */ 241 divsrc.sin_port = ((struct ipfw_rule_ref *)(mtag+1))->rulenum; 242 /* 243 * Record receive interface address, if any. 244 * But only for incoming packets. 245 */ 246 if (incoming) { 247 struct ifaddr *ifa; 248 struct ifnet *ifp; 249 250 /* Sanity check */ 251 M_ASSERTPKTHDR(m); 252 253 /* Find IP address for receive interface */ 254 ifp = m->m_pkthdr.rcvif; 255 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 256 if (ifa->ifa_addr->sa_family != AF_INET) 257 continue; 258 divsrc.sin_addr = 259 ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr; 260 break; 261 } 262 } 263 /* 264 * Record the incoming interface name whenever we have one. 265 */ 266 if (m->m_pkthdr.rcvif) { 267 /* 268 * Hide the actual interface name in there in the 269 * sin_zero array. XXX This needs to be moved to a 270 * different sockaddr type for divert, e.g. 271 * sockaddr_div with multiple fields like 272 * sockaddr_dl. Presently we have only 7 bytes 273 * but that will do for now as most interfaces 274 * are 4 or less + 2 or less bytes for unit. 275 * There is probably a faster way of doing this, 276 * possibly taking it from the sockaddr_dl on the iface. 277 * This solves the problem of a P2P link and a LAN interface 278 * having the same address, which can result in the wrong 279 * interface being assigned to the packet when fed back 280 * into the divert socket. Theoretically if the daemon saves 281 * and re-uses the sockaddr_in as suggested in the man pages, 282 * this iface name will come along for the ride. 283 * (see div_output for the other half of this.) 284 */ 285 strlcpy(divsrc.sin_zero, m->m_pkthdr.rcvif->if_xname, 286 sizeof(divsrc.sin_zero)); 287 } 288 289 /* Put packet on socket queue, if any */ 290 sa = NULL; 291 nport = htons((u_int16_t)(((struct ipfw_rule_ref *)(mtag+1))->info)); 292 CK_LIST_FOREACH(inp, &V_divcb, inp_list) { 293 /* XXX why does only one socket match? */ 294 if (inp->inp_lport == nport) { 295 INP_RLOCK(inp); 296 if (__predict_false(inp->inp_flags2 & INP_FREED)) { 297 INP_RUNLOCK(inp); 298 continue; 299 } 300 sa = inp->inp_socket; 301 SOCKBUF_LOCK(&sa->so_rcv); 302 if (sbappendaddr_locked(&sa->so_rcv, 303 (struct sockaddr *)&divsrc, m, 304 (struct mbuf *)0) == 0) { 305 soroverflow_locked(sa); 306 sa = NULL; /* force mbuf reclaim below */ 307 } else 308 sorwakeup_locked(sa); 309 INP_RUNLOCK(inp); 310 break; 311 } 312 } 313 if (sa == NULL) { 314 m_freem(m); 315 KMOD_IPSTAT_INC(ips_noproto); 316 KMOD_IPSTAT_DEC(ips_delivered); 317 } 318 } 319 320 /* 321 * Deliver packet back into the IP processing machinery. 322 * 323 * If no address specified, or address is 0.0.0.0, send to ip_output(); 324 * otherwise, send to ip_input() and mark as having been received on 325 * the interface with that address. 326 */ 327 static int 328 div_output(struct socket *so, struct mbuf *m, struct sockaddr_in *sin, 329 struct mbuf *control) 330 { 331 struct epoch_tracker et; 332 const struct ip *ip; 333 struct m_tag *mtag; 334 struct ipfw_rule_ref *dt; 335 int error, family; 336 337 if (control) { 338 m_freem(control); /* XXX */ 339 control = NULL; 340 } 341 342 if (sin != NULL) { 343 if (sin->sin_family != AF_INET) { 344 m_freem(m); 345 return (EAFNOSUPPORT); 346 } 347 if (sin->sin_len != sizeof(*sin)) { 348 m_freem(m); 349 return (EINVAL); 350 } 351 } 352 353 /* 354 * An mbuf may hasn't come from userland, but we pretend 355 * that it has. 356 */ 357 m->m_pkthdr.rcvif = NULL; 358 m->m_nextpkt = NULL; 359 M_SETFIB(m, so->so_fibnum); 360 361 mtag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL); 362 if (mtag == NULL) { 363 /* this should be normal */ 364 mtag = m_tag_alloc(MTAG_IPFW_RULE, 0, 365 sizeof(struct ipfw_rule_ref), M_NOWAIT | M_ZERO); 366 if (mtag == NULL) { 367 m_freem(m); 368 return (ENOBUFS); 369 } 370 m_tag_prepend(m, mtag); 371 } 372 dt = (struct ipfw_rule_ref *)(mtag+1); 373 374 /* Loopback avoidance and state recovery */ 375 if (sin) { 376 int i; 377 378 /* set the starting point. We provide a non-zero slot, 379 * but a non_matching chain_id to skip that info and use 380 * the rulenum/rule_id. 381 */ 382 dt->slot = 1; /* dummy, chain_id is invalid */ 383 dt->chain_id = 0; 384 dt->rulenum = sin->sin_port+1; /* host format ? */ 385 dt->rule_id = 0; 386 /* XXX: broken for IPv6 */ 387 /* 388 * Find receive interface with the given name, stuffed 389 * (if it exists) in the sin_zero[] field. 390 * The name is user supplied data so don't trust its size 391 * or that it is zero terminated. 392 */ 393 for (i = 0; i < sizeof(sin->sin_zero) && sin->sin_zero[i]; i++) 394 ; 395 if ( i > 0 && i < sizeof(sin->sin_zero)) 396 m->m_pkthdr.rcvif = ifunit(sin->sin_zero); 397 } 398 399 ip = mtod(m, struct ip *); 400 switch (ip->ip_v) { 401 case IPVERSION: 402 family = AF_INET; 403 break; 404 #ifdef INET6 405 case IPV6_VERSION >> 4: 406 family = AF_INET6; 407 break; 408 #endif 409 default: 410 m_freem(m); 411 return (EAFNOSUPPORT); 412 } 413 414 /* Reinject packet into the system as incoming or outgoing */ 415 NET_EPOCH_ENTER(et); 416 if (!sin || sin->sin_addr.s_addr == 0) { 417 dt->info |= IPFW_IS_DIVERT | IPFW_INFO_OUT; 418 error = div_output_outbound(family, so, m); 419 } else { 420 dt->info |= IPFW_IS_DIVERT | IPFW_INFO_IN; 421 error = div_output_inbound(family, so, m, sin); 422 } 423 NET_EPOCH_EXIT(et); 424 425 return (error); 426 } 427 428 /* 429 * Sends mbuf @m to the wire via ip[6]_output(). 430 * 431 * Returns 0 on success or an errno value on failure. @m is always consumed. 432 */ 433 static int 434 div_output_outbound(int family, struct socket *so, struct mbuf *m) 435 { 436 struct ip *const ip = mtod(m, struct ip *); 437 struct mbuf *options; 438 struct inpcb *inp; 439 int error; 440 441 inp = sotoinpcb(so); 442 INP_RLOCK(inp); 443 switch (family) { 444 case AF_INET: 445 /* 446 * Don't allow both user specified and setsockopt 447 * options, and don't allow packet length sizes that 448 * will crash. 449 */ 450 if ((((ip->ip_hl << 2) != sizeof(struct ip)) && 451 inp->inp_options != NULL) || 452 ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) { 453 INP_RUNLOCK(inp); 454 m_freem(m); 455 return (EINVAL); 456 } 457 break; 458 #ifdef INET6 459 case AF_INET6: 460 { 461 struct ip6_hdr *const ip6 = mtod(m, struct ip6_hdr *); 462 463 /* Don't allow packet length sizes that will crash */ 464 if (((u_short)ntohs(ip6->ip6_plen) > m->m_pkthdr.len)) { 465 INP_RUNLOCK(inp); 466 m_freem(m); 467 return (EINVAL); 468 } 469 break; 470 } 471 #endif 472 } 473 474 /* Send packet to output processing */ 475 KMOD_IPSTAT_INC(ips_rawout); /* XXX */ 476 477 #ifdef MAC 478 mac_inpcb_create_mbuf(inp, m); 479 #endif 480 /* 481 * Get ready to inject the packet into ip_output(). 482 * Just in case socket options were specified on the 483 * divert socket, we duplicate them. This is done 484 * to avoid having to hold the PCB locks over the call 485 * to ip_output(), as doing this results in a number of 486 * lock ordering complexities. 487 * 488 * Note that we set the multicast options argument for 489 * ip_output() to NULL since it should be invariant that 490 * they are not present. 491 */ 492 KASSERT(inp->inp_moptions == NULL, 493 ("multicast options set on a divert socket")); 494 /* 495 * XXXCSJP: It is unclear to me whether or not it makes 496 * sense for divert sockets to have options. However, 497 * for now we will duplicate them with the INP locks 498 * held so we can use them in ip_output() without 499 * requring a reference to the pcb. 500 */ 501 options = NULL; 502 if (inp->inp_options != NULL) { 503 options = m_dup(inp->inp_options, M_NOWAIT); 504 if (options == NULL) { 505 INP_RUNLOCK(inp); 506 m_freem(m); 507 return (ENOBUFS); 508 } 509 } 510 INP_RUNLOCK(inp); 511 512 error = 0; 513 switch (family) { 514 case AF_INET: 515 error = ip_output(m, options, NULL, 516 ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) 517 | IP_ALLOWBROADCAST | IP_RAWOUTPUT, NULL, NULL); 518 break; 519 #ifdef INET6 520 case AF_INET6: 521 error = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL); 522 break; 523 #endif 524 } 525 if (options != NULL) 526 m_freem(options); 527 528 return (error); 529 } 530 531 /* 532 * Schedules mbuf @m for local processing via IPv4/IPv6 netisr queue. 533 * 534 * Returns 0 on success or an errno value on failure. @m is always consumed. 535 */ 536 static int 537 div_output_inbound(int family, struct socket *so, struct mbuf *m, 538 struct sockaddr_in *sin) 539 { 540 const struct ip *ip; 541 struct ifaddr *ifa; 542 543 if (m->m_pkthdr.rcvif == NULL) { 544 /* 545 * No luck with the name, check by IP address. 546 * Clear the port and the ifname to make sure 547 * there are no distractions for ifa_ifwithaddr. 548 */ 549 550 /* XXX: broken for IPv6 */ 551 bzero(sin->sin_zero, sizeof(sin->sin_zero)); 552 sin->sin_port = 0; 553 ifa = ifa_ifwithaddr((struct sockaddr *) sin); 554 if (ifa == NULL) { 555 m_freem(m); 556 return (EADDRNOTAVAIL); 557 } 558 m->m_pkthdr.rcvif = ifa->ifa_ifp; 559 } 560 #ifdef MAC 561 mac_socket_create_mbuf(so, m); 562 #endif 563 /* Send packet to input processing via netisr */ 564 switch (family) { 565 case AF_INET: 566 ip = mtod(m, struct ip *); 567 /* 568 * Restore M_BCAST flag when destination address is 569 * broadcast. It is expected by ip_tryforward(). 570 */ 571 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) 572 m->m_flags |= M_MCAST; 573 else if (in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 574 m->m_flags |= M_BCAST; 575 netisr_queue_src(NETISR_IP, (uintptr_t)so, m); 576 break; 577 #ifdef INET6 578 case AF_INET6: 579 netisr_queue_src(NETISR_IPV6, (uintptr_t)so, m); 580 break; 581 #endif 582 default: 583 m_freem(m); 584 return (EINVAL); 585 } 586 587 return (0); 588 } 589 590 static int 591 div_attach(struct socket *so, int proto, struct thread *td) 592 { 593 struct inpcb *inp; 594 int error; 595 596 inp = sotoinpcb(so); 597 KASSERT(inp == NULL, ("div_attach: inp != NULL")); 598 if (td != NULL) { 599 error = priv_check(td, PRIV_NETINET_DIVERT); 600 if (error) 601 return (error); 602 } 603 error = soreserve(so, div_sendspace, div_recvspace); 604 if (error) 605 return error; 606 INP_INFO_WLOCK(&V_divcbinfo); 607 error = in_pcballoc(so, &V_divcbinfo); 608 if (error) { 609 INP_INFO_WUNLOCK(&V_divcbinfo); 610 return error; 611 } 612 inp = (struct inpcb *)so->so_pcb; 613 INP_INFO_WUNLOCK(&V_divcbinfo); 614 inp->inp_ip_p = proto; 615 inp->inp_vflag |= INP_IPV4; 616 inp->inp_flags |= INP_HDRINCL; 617 INP_WUNLOCK(inp); 618 return 0; 619 } 620 621 static void 622 div_detach(struct socket *so) 623 { 624 struct inpcb *inp; 625 626 inp = sotoinpcb(so); 627 KASSERT(inp != NULL, ("div_detach: inp == NULL")); 628 INP_INFO_WLOCK(&V_divcbinfo); 629 INP_WLOCK(inp); 630 in_pcbdetach(inp); 631 in_pcbfree(inp); 632 INP_INFO_WUNLOCK(&V_divcbinfo); 633 } 634 635 static int 636 div_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 637 { 638 struct inpcb *inp; 639 int error; 640 641 inp = sotoinpcb(so); 642 KASSERT(inp != NULL, ("div_bind: inp == NULL")); 643 /* in_pcbbind assumes that nam is a sockaddr_in 644 * and in_pcbbind requires a valid address. Since divert 645 * sockets don't we need to make sure the address is 646 * filled in properly. 647 * XXX -- divert should not be abusing in_pcbind 648 * and should probably have its own family. 649 */ 650 if (nam->sa_family != AF_INET) 651 return EAFNOSUPPORT; 652 if (nam->sa_len != sizeof(struct sockaddr_in)) 653 return EINVAL; 654 ((struct sockaddr_in *)nam)->sin_addr.s_addr = INADDR_ANY; 655 INP_INFO_WLOCK(&V_divcbinfo); 656 INP_WLOCK(inp); 657 INP_HASH_WLOCK(&V_divcbinfo); 658 error = in_pcbbind(inp, nam, td->td_ucred); 659 INP_HASH_WUNLOCK(&V_divcbinfo); 660 INP_WUNLOCK(inp); 661 INP_INFO_WUNLOCK(&V_divcbinfo); 662 return error; 663 } 664 665 static int 666 div_shutdown(struct socket *so) 667 { 668 struct inpcb *inp; 669 670 inp = sotoinpcb(so); 671 KASSERT(inp != NULL, ("div_shutdown: inp == NULL")); 672 INP_WLOCK(inp); 673 socantsendmore(so); 674 INP_WUNLOCK(inp); 675 return 0; 676 } 677 678 static int 679 div_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 680 struct mbuf *control, struct thread *td) 681 { 682 683 /* Packet must have a header (but that's about it) */ 684 if (m->m_len < sizeof (struct ip) && 685 (m = m_pullup(m, sizeof (struct ip))) == NULL) { 686 KMOD_IPSTAT_INC(ips_toosmall); 687 if (control != NULL) 688 m_freem(control); 689 m_freem(m); 690 return EINVAL; 691 } 692 693 /* Send packet */ 694 return div_output(so, m, (struct sockaddr_in *)nam, control); 695 } 696 697 static int 698 div_pcblist(SYSCTL_HANDLER_ARGS) 699 { 700 struct xinpgen xig; 701 struct epoch_tracker et; 702 struct inpcb *inp; 703 int error; 704 705 if (req->newptr != 0) 706 return EPERM; 707 708 if (req->oldptr == 0) { 709 int n; 710 711 n = V_divcbinfo.ipi_count; 712 n += imax(n / 8, 10); 713 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb); 714 return 0; 715 } 716 717 if ((error = sysctl_wire_old_buffer(req, 0)) != 0) 718 return (error); 719 720 bzero(&xig, sizeof(xig)); 721 xig.xig_len = sizeof xig; 722 xig.xig_count = V_divcbinfo.ipi_count; 723 xig.xig_gen = V_divcbinfo.ipi_gencnt; 724 xig.xig_sogen = so_gencnt; 725 error = SYSCTL_OUT(req, &xig, sizeof xig); 726 if (error) 727 return error; 728 729 NET_EPOCH_ENTER(et); 730 for (inp = CK_LIST_FIRST(V_divcbinfo.ipi_listhead); 731 inp != NULL; 732 inp = CK_LIST_NEXT(inp, inp_list)) { 733 INP_RLOCK(inp); 734 if (inp->inp_gencnt <= xig.xig_gen) { 735 struct xinpcb xi; 736 737 in_pcbtoxinpcb(inp, &xi); 738 INP_RUNLOCK(inp); 739 error = SYSCTL_OUT(req, &xi, sizeof xi); 740 } else 741 INP_RUNLOCK(inp); 742 } 743 NET_EPOCH_EXIT(et); 744 745 if (!error) { 746 /* 747 * Give the user an updated idea of our state. 748 * If the generation differs from what we told 749 * her before, she knows that something happened 750 * while we were processing this request, and it 751 * might be necessary to retry. 752 */ 753 xig.xig_gen = V_divcbinfo.ipi_gencnt; 754 xig.xig_sogen = so_gencnt; 755 xig.xig_count = V_divcbinfo.ipi_count; 756 error = SYSCTL_OUT(req, &xig, sizeof xig); 757 } 758 759 return (error); 760 } 761 762 #ifdef SYSCTL_NODE 763 static SYSCTL_NODE(_net_inet, IPPROTO_DIVERT, divert, 764 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 765 "IPDIVERT"); 766 SYSCTL_PROC(_net_inet_divert, OID_AUTO, pcblist, 767 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, 768 NULL, 0, div_pcblist, "S,xinpcb", 769 "List of active divert sockets"); 770 #endif 771 772 struct pr_usrreqs div_usrreqs = { 773 .pru_attach = div_attach, 774 .pru_bind = div_bind, 775 .pru_control = in_control, 776 .pru_detach = div_detach, 777 .pru_peeraddr = in_getpeeraddr, 778 .pru_send = div_send, 779 .pru_shutdown = div_shutdown, 780 .pru_sockaddr = in_getsockaddr, 781 .pru_sosetlabel = in_pcbsosetlabel 782 }; 783 784 struct protosw div_protosw = { 785 .pr_type = SOCK_RAW, 786 .pr_protocol = IPPROTO_DIVERT, 787 .pr_flags = PR_ATOMIC|PR_ADDR, 788 .pr_input = div_input, 789 .pr_init = div_init, 790 .pr_usrreqs = &div_usrreqs 791 }; 792 793 static int 794 div_modevent(module_t mod, int type, void *unused) 795 { 796 int err = 0; 797 798 switch (type) { 799 case MOD_LOAD: 800 /* 801 * Protocol will be initialized by pf_proto_register(). 802 * We don't have to register ip_protox because we are not 803 * a true IP protocol that goes over the wire. 804 */ 805 err = pf_proto_register(PF_INET, &div_protosw); 806 if (err != 0) 807 return (err); 808 ip_divert_ptr = divert_packet; 809 ip_divert_event_tag = EVENTHANDLER_REGISTER(maxsockets_change, 810 div_zone_change, NULL, EVENTHANDLER_PRI_ANY); 811 break; 812 case MOD_QUIESCE: 813 /* 814 * IPDIVERT may normally not be unloaded because of the 815 * potential race conditions. Tell kldunload we can't be 816 * unloaded unless the unload is forced. 817 */ 818 err = EPERM; 819 break; 820 case MOD_UNLOAD: 821 /* 822 * Forced unload. 823 * 824 * Module ipdivert can only be unloaded if no sockets are 825 * connected. Maybe this can be changed later to forcefully 826 * disconnect any open sockets. 827 * 828 * XXXRW: Note that there is a slight race here, as a new 829 * socket open request could be spinning on the lock and then 830 * we destroy the lock. 831 */ 832 INP_INFO_WLOCK(&V_divcbinfo); 833 if (V_divcbinfo.ipi_count != 0) { 834 err = EBUSY; 835 INP_INFO_WUNLOCK(&V_divcbinfo); 836 break; 837 } 838 ip_divert_ptr = NULL; 839 err = pf_proto_unregister(PF_INET, IPPROTO_DIVERT, SOCK_RAW); 840 INP_INFO_WUNLOCK(&V_divcbinfo); 841 #ifndef VIMAGE 842 div_destroy(NULL); 843 #endif 844 EVENTHANDLER_DEREGISTER(maxsockets_change, ip_divert_event_tag); 845 break; 846 default: 847 err = EOPNOTSUPP; 848 break; 849 } 850 return err; 851 } 852 853 static moduledata_t ipdivertmod = { 854 "ipdivert", 855 div_modevent, 856 0 857 }; 858 859 DECLARE_MODULE(ipdivert, ipdivertmod, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY); 860 MODULE_DEPEND(ipdivert, ipfw, 3, 3, 3); 861 MODULE_VERSION(ipdivert, 1); 862