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