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