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