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