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