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 #include "opt_inet.h" 34 #include "opt_inet6.h" 35 #include "opt_sctp.h" 36 37 #include <sys/param.h> 38 #include <sys/eventhandler.h> 39 #include <sys/kernel.h> 40 #include <sys/lock.h> 41 #include <sys/malloc.h> 42 #include <sys/mbuf.h> 43 #include <sys/module.h> 44 #include <sys/kernel.h> 45 #include <sys/priv.h> 46 #include <sys/proc.h> 47 #include <sys/domain.h> 48 #include <sys/protosw.h> 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/sysctl.h> 52 #include <net/vnet.h> 53 54 #include <net/if.h> 55 #include <net/if_var.h> 56 #include <net/if_private.h> 57 #include <net/netisr.h> 58 59 #include <netinet/in.h> 60 #include <netinet/in_pcb.h> 61 #include <netinet/in_systm.h> 62 #include <netinet/in_var.h> 63 #include <netinet/ip.h> 64 #include <netinet/ip_var.h> 65 #include <netinet/ip_divert.h> 66 #ifdef INET6 67 #include <netinet/ip6.h> 68 #include <netinet6/ip6_var.h> 69 #endif 70 #if defined(SCTP) || defined(SCTP_SUPPORT) 71 #include <netinet/sctp_crc32.h> 72 #endif 73 74 #include <security/mac/mac_framework.h> 75 /* 76 * Divert sockets 77 */ 78 79 /* 80 * Allocate enough space to hold a full IP packet 81 */ 82 #define DIVSNDQ (65536 + 100) 83 #define DIVRCVQ (65536 + 100) 84 85 /* 86 * Usually a system has very few divert ports. Previous implementation 87 * used a linked list. 88 */ 89 #define DIVHASHSIZE (1 << 3) /* 8 entries, one cache line. */ 90 #define DIVHASH(port) (port % DIVHASHSIZE) 91 #define DCBHASH(dcb) ((dcb)->dcb_port % DIVHASHSIZE) 92 93 /* 94 * Divert sockets work in conjunction with ipfw or other packet filters, 95 * see the divert(4) manpage for features. 96 * Packets are selected by the packet filter and tagged with an 97 * MTAG_IPFW_RULE tag carrying the 'divert port' number (as set by 98 * the packet filter) and information on the matching filter rule for 99 * subsequent reinjection. The divert_port is used to put the packet 100 * on the corresponding divert socket, while the rule number is passed 101 * up (at least partially) as the sin_port in the struct sockaddr. 102 * 103 * Packets written to the divert socket carry in sin_addr a 104 * destination address, and in sin_port the number of the filter rule 105 * after which to continue processing. 106 * If the destination address is INADDR_ANY, the packet is treated as 107 * as outgoing and sent to ip_output(); otherwise it is treated as 108 * incoming and sent to ip_input(). 109 * Further, sin_zero carries some information on the interface, 110 * which can be used in the reinject -- see comments in the code. 111 * 112 * On reinjection, processing in ip_input() and ip_output() 113 * will be exactly the same as for the original packet, except that 114 * packet filter processing will start at the rule number after the one 115 * written in the sin_port (ipfw does not allow a rule #0, so sin_port=0 116 * will apply the entire ruleset to the packet). 117 */ 118 static SYSCTL_NODE(_net_inet, OID_AUTO, divert, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 119 "divert(4)"); 120 121 VNET_PCPUSTAT_DEFINE_STATIC(struct divstat, divstat); 122 VNET_PCPUSTAT_SYSINIT(divstat); 123 #ifdef VIMAGE 124 VNET_PCPUSTAT_SYSUNINIT(divstat); 125 #endif 126 SYSCTL_VNET_PCPUSTAT(_net_inet_divert, OID_AUTO, stats, struct divstat, 127 divstat, "divert(4) socket statistics"); 128 #define DIVSTAT_INC(name) \ 129 VNET_PCPUSTAT_ADD(struct divstat, divstat, div_ ## name, 1) 130 131 static u_long div_sendspace = DIVSNDQ; /* XXX sysctl ? */ 132 static u_long div_recvspace = DIVRCVQ; /* XXX sysctl ? */ 133 134 static int div_output_inbound(int fmaily, struct socket *so, struct mbuf *m, 135 struct sockaddr_in *sin); 136 static int div_output_outbound(int family, struct socket *so, struct mbuf *m); 137 138 struct divcb { 139 union { 140 SLIST_ENTRY(divcb) dcb_next; 141 intptr_t dcb_bound; 142 #define DCB_UNBOUND ((intptr_t)-1) 143 }; 144 struct socket *dcb_socket; 145 uint16_t dcb_port; 146 uint64_t dcb_gencnt; 147 struct epoch_context dcb_epochctx; 148 }; 149 150 SLIST_HEAD(divhashhead, divcb); 151 152 VNET_DEFINE_STATIC(struct divhashhead, divhash[DIVHASHSIZE]) = {}; 153 #define V_divhash VNET(divhash) 154 VNET_DEFINE_STATIC(uint64_t, dcb_count) = 0; 155 #define V_dcb_count VNET(dcb_count) 156 VNET_DEFINE_STATIC(uint64_t, dcb_gencnt) = 0; 157 #define V_dcb_gencnt VNET(dcb_gencnt) 158 159 static struct mtx divert_mtx; 160 MTX_SYSINIT(divert, &divert_mtx, "divert(4) socket pcb lists", MTX_DEF); 161 #define DIVERT_LOCK() mtx_lock(&divert_mtx) 162 #define DIVERT_UNLOCK() mtx_unlock(&divert_mtx) 163 164 /* 165 * Divert a packet by passing it up to the divert socket at port 'port'. 166 */ 167 static void 168 divert_packet(struct mbuf *m, bool incoming) 169 { 170 struct divcb *dcb; 171 u_int16_t nport; 172 struct sockaddr_in divsrc; 173 struct m_tag *mtag; 174 175 NET_EPOCH_ASSERT(); 176 177 mtag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL); 178 if (mtag == NULL) { 179 m_freem(m); 180 return; 181 } 182 /* Assure header */ 183 if (m->m_len < sizeof(struct ip) && 184 (m = m_pullup(m, sizeof(struct ip))) == NULL) 185 return; 186 #ifdef INET 187 /* Delayed checksums are currently not compatible with divert. */ 188 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 189 in_delayed_cksum(m); 190 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 191 } 192 #if defined(SCTP) || defined(SCTP_SUPPORT) 193 if (m->m_pkthdr.csum_flags & CSUM_SCTP) { 194 struct ip *ip; 195 196 ip = mtod(m, struct ip *); 197 sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 198 m->m_pkthdr.csum_flags &= ~CSUM_SCTP; 199 } 200 #endif 201 #endif 202 #ifdef INET6 203 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) { 204 in6_delayed_cksum(m, m->m_pkthdr.len - 205 sizeof(struct ip6_hdr), sizeof(struct ip6_hdr)); 206 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6; 207 } 208 #if defined(SCTP) || defined(SCTP_SUPPORT) 209 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) { 210 sctp_delayed_cksum(m, sizeof(struct ip6_hdr)); 211 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6; 212 } 213 #endif 214 #endif /* INET6 */ 215 bzero(&divsrc, sizeof(divsrc)); 216 divsrc.sin_len = sizeof(divsrc); 217 divsrc.sin_family = AF_INET; 218 /* record matching rule, in host format */ 219 divsrc.sin_port = ((struct ipfw_rule_ref *)(mtag+1))->rulenum; 220 /* 221 * Record receive interface address, if any. 222 * But only for incoming packets. 223 */ 224 if (incoming) { 225 struct ifaddr *ifa; 226 struct ifnet *ifp; 227 228 /* Sanity check */ 229 M_ASSERTPKTHDR(m); 230 231 /* Find IP address for receive interface */ 232 ifp = m->m_pkthdr.rcvif; 233 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 234 if (ifa->ifa_addr->sa_family != AF_INET) 235 continue; 236 divsrc.sin_addr = 237 ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr; 238 break; 239 } 240 } 241 /* 242 * Record the incoming interface name whenever we have one. 243 */ 244 if (m->m_pkthdr.rcvif) { 245 /* 246 * Hide the actual interface name in there in the 247 * sin_zero array. XXX This needs to be moved to a 248 * different sockaddr type for divert, e.g. 249 * sockaddr_div with multiple fields like 250 * sockaddr_dl. Presently we have only 7 bytes 251 * but that will do for now as most interfaces 252 * are 4 or less + 2 or less bytes for unit. 253 * There is probably a faster way of doing this, 254 * possibly taking it from the sockaddr_dl on the iface. 255 * This solves the problem of a P2P link and a LAN interface 256 * having the same address, which can result in the wrong 257 * interface being assigned to the packet when fed back 258 * into the divert socket. Theoretically if the daemon saves 259 * and re-uses the sockaddr_in as suggested in the man pages, 260 * this iface name will come along for the ride. 261 * (see div_output for the other half of this.) 262 */ 263 strlcpy(divsrc.sin_zero, m->m_pkthdr.rcvif->if_xname, 264 sizeof(divsrc.sin_zero)); 265 } 266 267 /* Put packet on socket queue, if any */ 268 nport = htons((uint16_t)(((struct ipfw_rule_ref *)(mtag+1))->info)); 269 SLIST_FOREACH(dcb, &V_divhash[DIVHASH(nport)], dcb_next) 270 if (dcb->dcb_port == nport) 271 break; 272 273 if (dcb != NULL) { 274 struct socket *sa = dcb->dcb_socket; 275 276 SOCKBUF_LOCK(&sa->so_rcv); 277 if (sbappendaddr_locked(&sa->so_rcv, 278 (struct sockaddr *)&divsrc, m, NULL) == 0) { 279 soroverflow_locked(sa); 280 m_freem(m); 281 } else { 282 sorwakeup_locked(sa); 283 DIVSTAT_INC(diverted); 284 } 285 } else { 286 DIVSTAT_INC(noport); 287 m_freem(m); 288 } 289 } 290 291 /* 292 * Deliver packet back into the IP processing machinery. 293 * 294 * If no address specified, or address is 0.0.0.0, send to ip_output(); 295 * otherwise, send to ip_input() and mark as having been received on 296 * the interface with that address. 297 */ 298 static int 299 div_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 300 struct mbuf *control, struct thread *td) 301 { 302 struct epoch_tracker et; 303 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 304 const struct ip *ip; 305 struct m_tag *mtag; 306 struct ipfw_rule_ref *dt; 307 int error, family; 308 309 if (control) 310 m_freem(control); 311 312 /* Packet must have a header (but that's about it) */ 313 if (m->m_len < sizeof (struct ip) && 314 (m = m_pullup(m, sizeof (struct ip))) == NULL) { 315 m_freem(m); 316 return (EINVAL); 317 } 318 319 if (sin != NULL) { 320 if (sin->sin_family != AF_INET) { 321 m_freem(m); 322 return (EAFNOSUPPORT); 323 } 324 if (sin->sin_len != sizeof(*sin)) { 325 m_freem(m); 326 return (EINVAL); 327 } 328 } 329 330 /* 331 * An mbuf may hasn't come from userland, but we pretend 332 * that it has. 333 */ 334 m->m_pkthdr.rcvif = NULL; 335 m->m_nextpkt = NULL; 336 M_SETFIB(m, so->so_fibnum); 337 338 mtag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL); 339 if (mtag == NULL) { 340 /* this should be normal */ 341 mtag = m_tag_alloc(MTAG_IPFW_RULE, 0, 342 sizeof(struct ipfw_rule_ref), M_NOWAIT | M_ZERO); 343 if (mtag == NULL) { 344 m_freem(m); 345 return (ENOBUFS); 346 } 347 m_tag_prepend(m, mtag); 348 } 349 dt = (struct ipfw_rule_ref *)(mtag+1); 350 351 /* Loopback avoidance and state recovery */ 352 if (sin) { 353 int i; 354 355 /* set the starting point. We provide a non-zero slot, 356 * but a non_matching chain_id to skip that info and use 357 * the rulenum/rule_id. 358 */ 359 dt->slot = 1; /* dummy, chain_id is invalid */ 360 dt->chain_id = 0; 361 dt->rulenum = sin->sin_port+1; /* host format ? */ 362 dt->rule_id = 0; 363 /* XXX: broken for IPv6 */ 364 /* 365 * Find receive interface with the given name, stuffed 366 * (if it exists) in the sin_zero[] field. 367 * The name is user supplied data so don't trust its size 368 * or that it is zero terminated. 369 */ 370 for (i = 0; i < sizeof(sin->sin_zero) && sin->sin_zero[i]; i++) 371 ; 372 if ( i > 0 && i < sizeof(sin->sin_zero)) 373 m->m_pkthdr.rcvif = ifunit(sin->sin_zero); 374 } 375 376 ip = mtod(m, struct ip *); 377 switch (ip->ip_v) { 378 #ifdef INET 379 case IPVERSION: 380 family = AF_INET; 381 break; 382 #endif 383 #ifdef INET6 384 case IPV6_VERSION >> 4: 385 family = AF_INET6; 386 break; 387 #endif 388 default: 389 m_freem(m); 390 return (EAFNOSUPPORT); 391 } 392 393 /* Reinject packet into the system as incoming or outgoing */ 394 NET_EPOCH_ENTER(et); 395 if (!sin || sin->sin_addr.s_addr == 0) { 396 dt->info |= IPFW_IS_DIVERT | IPFW_INFO_OUT; 397 error = div_output_outbound(family, so, m); 398 } else { 399 dt->info |= IPFW_IS_DIVERT | IPFW_INFO_IN; 400 error = div_output_inbound(family, so, m, sin); 401 } 402 NET_EPOCH_EXIT(et); 403 404 return (error); 405 } 406 407 /* 408 * Sends mbuf @m to the wire via ip[6]_output(). 409 * 410 * Returns 0 on success or an errno value on failure. @m is always consumed. 411 */ 412 static int 413 div_output_outbound(int family, struct socket *so, struct mbuf *m) 414 { 415 int error; 416 417 switch (family) { 418 #ifdef INET 419 case AF_INET: 420 { 421 struct ip *const ip = mtod(m, struct ip *); 422 423 /* Don't allow packet length sizes that will crash. */ 424 if (((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) { 425 m_freem(m); 426 return (EINVAL); 427 } 428 break; 429 } 430 #endif 431 #ifdef INET6 432 case AF_INET6: 433 { 434 struct ip6_hdr *const ip6 = mtod(m, struct ip6_hdr *); 435 436 /* Don't allow packet length sizes that will crash */ 437 if (((u_short)ntohs(ip6->ip6_plen) > m->m_pkthdr.len)) { 438 m_freem(m); 439 return (EINVAL); 440 } 441 break; 442 } 443 #endif 444 } 445 446 #ifdef MAC 447 mac_socket_create_mbuf(so, m); 448 #endif 449 450 error = 0; 451 switch (family) { 452 #ifdef INET 453 case AF_INET: 454 error = ip_output(m, NULL, NULL, 455 ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) 456 | IP_ALLOWBROADCAST | IP_RAWOUTPUT, NULL, NULL); 457 break; 458 #endif 459 #ifdef INET6 460 case AF_INET6: 461 error = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL); 462 break; 463 #endif 464 } 465 if (error == 0) 466 DIVSTAT_INC(outbound); 467 468 return (error); 469 } 470 471 /* 472 * Schedules mbuf @m for local processing via IPv4/IPv6 netisr queue. 473 * 474 * Returns 0 on success or an errno value on failure. @m is always consumed. 475 */ 476 static int 477 div_output_inbound(int family, struct socket *so, struct mbuf *m, 478 struct sockaddr_in *sin) 479 { 480 struct ifaddr *ifa; 481 482 if (m->m_pkthdr.rcvif == NULL) { 483 /* 484 * No luck with the name, check by IP address. 485 * Clear the port and the ifname to make sure 486 * there are no distractions for ifa_ifwithaddr. 487 */ 488 489 /* XXX: broken for IPv6 */ 490 bzero(sin->sin_zero, sizeof(sin->sin_zero)); 491 sin->sin_port = 0; 492 ifa = ifa_ifwithaddr((struct sockaddr *) sin); 493 if (ifa == NULL) { 494 m_freem(m); 495 return (EADDRNOTAVAIL); 496 } 497 m->m_pkthdr.rcvif = ifa->ifa_ifp; 498 } 499 #ifdef MAC 500 mac_socket_create_mbuf(so, m); 501 #endif 502 /* Send packet to input processing via netisr */ 503 switch (family) { 504 #ifdef INET 505 case AF_INET: 506 { 507 const struct ip *ip; 508 509 ip = mtod(m, struct ip *); 510 /* 511 * Restore M_BCAST flag when destination address is 512 * broadcast. It is expected by ip_tryforward(). 513 */ 514 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) 515 m->m_flags |= M_MCAST; 516 else if (in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 517 m->m_flags |= M_BCAST; 518 netisr_queue_src(NETISR_IP, (uintptr_t)so, m); 519 DIVSTAT_INC(inbound); 520 break; 521 } 522 #endif 523 #ifdef INET6 524 case AF_INET6: 525 netisr_queue_src(NETISR_IPV6, (uintptr_t)so, m); 526 DIVSTAT_INC(inbound); 527 break; 528 #endif 529 default: 530 m_freem(m); 531 return (EINVAL); 532 } 533 534 return (0); 535 } 536 537 static int 538 div_attach(struct socket *so, int proto, struct thread *td) 539 { 540 struct divcb *dcb; 541 int error; 542 543 if (td != NULL) { 544 error = priv_check(td, PRIV_NETINET_DIVERT); 545 if (error) 546 return (error); 547 } 548 error = soreserve(so, div_sendspace, div_recvspace); 549 if (error) 550 return error; 551 dcb = malloc(sizeof(*dcb), M_PCB, M_WAITOK); 552 dcb->dcb_bound = DCB_UNBOUND; 553 dcb->dcb_socket = so; 554 DIVERT_LOCK(); 555 V_dcb_count++; 556 dcb->dcb_gencnt = ++V_dcb_gencnt; 557 DIVERT_UNLOCK(); 558 so->so_pcb = dcb; 559 560 return (0); 561 } 562 563 static void 564 div_free(epoch_context_t ctx) 565 { 566 struct divcb *dcb = __containerof(ctx, struct divcb, dcb_epochctx); 567 568 free(dcb, M_PCB); 569 } 570 571 static void 572 div_detach(struct socket *so) 573 { 574 struct divcb *dcb = so->so_pcb; 575 576 so->so_pcb = NULL; 577 DIVERT_LOCK(); 578 if (dcb->dcb_bound != DCB_UNBOUND) 579 SLIST_REMOVE(&V_divhash[DCBHASH(dcb)], dcb, divcb, dcb_next); 580 V_dcb_count--; 581 V_dcb_gencnt++; 582 DIVERT_UNLOCK(); 583 NET_EPOCH_CALL(div_free, &dcb->dcb_epochctx); 584 } 585 586 static int 587 div_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 588 { 589 struct divcb *dcb; 590 uint16_t port; 591 592 if (nam->sa_family != AF_INET) 593 return EAFNOSUPPORT; 594 if (nam->sa_len != sizeof(struct sockaddr_in)) 595 return EINVAL; 596 port = ((struct sockaddr_in *)nam)->sin_port; 597 DIVERT_LOCK(); 598 SLIST_FOREACH(dcb, &V_divhash[DIVHASH(port)], dcb_next) 599 if (dcb->dcb_port == port) { 600 DIVERT_UNLOCK(); 601 return (EADDRINUSE); 602 } 603 dcb = so->so_pcb; 604 if (dcb->dcb_bound != DCB_UNBOUND) 605 SLIST_REMOVE(&V_divhash[DCBHASH(dcb)], dcb, divcb, dcb_next); 606 dcb->dcb_port = port; 607 SLIST_INSERT_HEAD(&V_divhash[DIVHASH(port)], dcb, dcb_next); 608 DIVERT_UNLOCK(); 609 610 return (0); 611 } 612 613 static int 614 div_shutdown(struct socket *so) 615 { 616 617 socantsendmore(so); 618 return 0; 619 } 620 621 static int 622 div_pcblist(SYSCTL_HANDLER_ARGS) 623 { 624 struct xinpgen xig; 625 struct divcb *dcb; 626 int error; 627 628 if (req->newptr != 0) 629 return EPERM; 630 631 if (req->oldptr == 0) { 632 u_int n; 633 634 n = V_dcb_count; 635 n += imax(n / 8, 10); 636 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb); 637 return 0; 638 } 639 640 if ((error = sysctl_wire_old_buffer(req, 0)) != 0) 641 return (error); 642 643 bzero(&xig, sizeof(xig)); 644 xig.xig_len = sizeof xig; 645 xig.xig_count = V_dcb_count; 646 xig.xig_gen = V_dcb_gencnt; 647 xig.xig_sogen = so_gencnt; 648 error = SYSCTL_OUT(req, &xig, sizeof xig); 649 if (error) 650 return error; 651 652 DIVERT_LOCK(); 653 for (int i = 0; i < DIVHASHSIZE; i++) 654 SLIST_FOREACH(dcb, &V_divhash[i], dcb_next) { 655 if (dcb->dcb_gencnt <= xig.xig_gen) { 656 struct xinpcb xi; 657 658 bzero(&xi, sizeof(xi)); 659 xi.xi_len = sizeof(struct xinpcb); 660 sotoxsocket(dcb->dcb_socket, &xi.xi_socket); 661 xi.inp_gencnt = dcb->dcb_gencnt; 662 xi.inp_vflag = INP_IPV4; /* XXX: netstat(1) */ 663 xi.inp_inc.inc_ie.ie_lport = dcb->dcb_port; 664 error = SYSCTL_OUT(req, &xi, sizeof xi); 665 if (error) 666 goto errout; 667 } 668 } 669 670 /* 671 * Give the user an updated idea of our state. 672 * If the generation differs from what we told 673 * her before, she knows that something happened 674 * while we were processing this request, and it 675 * might be necessary to retry. 676 */ 677 xig.xig_gen = V_dcb_gencnt; 678 xig.xig_sogen = so_gencnt; 679 xig.xig_count = V_dcb_count; 680 error = SYSCTL_OUT(req, &xig, sizeof xig); 681 682 errout: 683 DIVERT_UNLOCK(); 684 685 return (error); 686 } 687 SYSCTL_PROC(_net_inet_divert, OID_AUTO, pcblist, 688 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, div_pcblist, 689 "S,xinpcb", "List of active divert sockets"); 690 691 static struct protosw div_protosw = { 692 .pr_type = SOCK_RAW, 693 .pr_flags = PR_ATOMIC|PR_ADDR, 694 .pr_attach = div_attach, 695 .pr_bind = div_bind, 696 .pr_detach = div_detach, 697 .pr_send = div_send, 698 .pr_shutdown = div_shutdown, 699 }; 700 701 static struct domain divertdomain = { 702 .dom_family = PF_DIVERT, 703 .dom_name = "divert", 704 .dom_nprotosw = 1, 705 .dom_protosw = { &div_protosw }, 706 }; 707 708 static int 709 div_modevent(module_t mod, int type, void *unused) 710 { 711 int err = 0; 712 713 switch (type) { 714 case MOD_LOAD: 715 domain_add(&divertdomain); 716 ip_divert_ptr = divert_packet; 717 break; 718 case MOD_QUIESCE: 719 /* 720 * IPDIVERT may normally not be unloaded because of the 721 * potential race conditions. Tell kldunload we can't be 722 * unloaded unless the unload is forced. 723 */ 724 err = EPERM; 725 break; 726 case MOD_UNLOAD: 727 /* 728 * Forced unload. 729 * 730 * Module ipdivert can only be unloaded if no sockets are 731 * connected. Maybe this can be changed later to forcefully 732 * disconnect any open sockets. 733 * 734 * XXXRW: Note that there is a slight race here, as a new 735 * socket open request could be spinning on the lock and then 736 * we destroy the lock. 737 * 738 * XXXGL: One more reason this code is incorrect is that it 739 * checks only the current vnet. 740 */ 741 DIVERT_LOCK(); 742 if (V_dcb_count != 0) { 743 DIVERT_UNLOCK(); 744 err = EBUSY; 745 break; 746 } 747 DIVERT_UNLOCK(); 748 ip_divert_ptr = NULL; 749 domain_remove(&divertdomain); 750 break; 751 default: 752 err = EOPNOTSUPP; 753 break; 754 } 755 return err; 756 } 757 758 static moduledata_t ipdivertmod = { 759 "ipdivert", 760 div_modevent, 761 0 762 }; 763 764 DECLARE_MODULE(ipdivert, ipdivertmod, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY); 765 MODULE_VERSION(ipdivert, 1); 766