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