xref: /freebsd/sys/netpfil/ipfw/ip_fw_pfil.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004 Andre Oppermann, Internet Business Solutions AG
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_ipfw.h"
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #ifndef INET
36 #error IPFIREWALL requires INET.
37 #endif /* INET */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/module.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/rwlock.h>
47 #include <sys/socket.h>
48 #include <sys/sysctl.h>
49 
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <net/route.h>
53 #include <net/ethernet.h>
54 #include <net/pfil.h>
55 #include <net/vnet.h>
56 
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/ip_fw.h>
62 #ifdef INET6
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet6/scope6_var.h>
66 #endif
67 
68 #include <netgraph/ng_ipfw.h>
69 
70 #include <netpfil/ipfw/ip_fw_private.h>
71 
72 #include <machine/in_cksum.h>
73 
74 VNET_DEFINE_STATIC(int, fw_enable) = 1;
75 #define V_fw_enable	VNET(fw_enable)
76 
77 #ifdef INET6
78 VNET_DEFINE_STATIC(int, fw6_enable) = 1;
79 #define V_fw6_enable	VNET(fw6_enable)
80 #endif
81 
82 VNET_DEFINE_STATIC(int, fwlink_enable) = 0;
83 #define V_fwlink_enable	VNET(fwlink_enable)
84 
85 int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
86 
87 /* Forward declarations. */
88 static int ipfw_divert(struct mbuf **, int, struct ipfw_rule_ref *, int);
89 
90 #ifdef SYSCTL_NODE
91 
92 SYSBEGIN(f1)
93 
94 SYSCTL_DECL(_net_inet_ip_fw);
95 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, enable,
96     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3,
97     &VNET_NAME(fw_enable), 0, ipfw_chg_hook, "I", "Enable ipfw");
98 #ifdef INET6
99 SYSCTL_DECL(_net_inet6_ip6_fw);
100 SYSCTL_PROC(_net_inet6_ip6_fw, OID_AUTO, enable,
101     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3,
102     &VNET_NAME(fw6_enable), 0, ipfw_chg_hook, "I", "Enable ipfw+6");
103 #endif /* INET6 */
104 
105 SYSCTL_DECL(_net_link_ether);
106 SYSCTL_PROC(_net_link_ether, OID_AUTO, ipfw,
107     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3,
108     &VNET_NAME(fwlink_enable), 0, ipfw_chg_hook, "I",
109     "Pass ether pkts through firewall");
110 
111 SYSEND
112 
113 #endif /* SYSCTL_NODE */
114 
115 /*
116  * The pfilter hook to pass packets to ipfw_chk and then to
117  * dummynet, divert, netgraph or other modules.
118  * The packet may be consumed.
119  */
120 static pfil_return_t
121 ipfw_check_packet(struct mbuf **m0, struct ifnet *ifp, int dir,
122     void *ruleset __unused, struct inpcb *inp)
123 {
124 	struct ip_fw_args args;
125 	struct m_tag *tag;
126 	pfil_return_t ret;
127 	int ipfw;
128 
129 	/* convert dir to IPFW values */
130 	dir = (dir & PFIL_IN) ? DIR_IN : DIR_OUT;
131 	args.flags = 0;
132 again:
133 	/*
134 	 * extract and remove the tag if present. If we are left
135 	 * with onepass, optimize the outgoing path.
136 	 */
137 	tag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
138 	if (tag != NULL) {
139 		args.rule = *((struct ipfw_rule_ref *)(tag+1));
140 		m_tag_delete(*m0, tag);
141 		if (args.rule.info & IPFW_ONEPASS)
142 			return (0);
143 		args.flags |= IPFW_ARGS_REF;
144 	}
145 
146 	args.m = *m0;
147 	args.oif = dir == DIR_OUT ? ifp : NULL;
148 	args.inp = inp;
149 
150 	ipfw = ipfw_chk(&args);
151 	*m0 = args.m;
152 
153 	KASSERT(*m0 != NULL || ipfw == IP_FW_DENY, ("%s: m0 is NULL",
154 	    __func__));
155 
156 	ret = PFIL_PASS;
157 	switch (ipfw) {
158 	case IP_FW_PASS:
159 		/* next_hop may be set by ipfw_chk */
160 		if ((args.flags & (IPFW_ARGS_NH4 | IPFW_ARGS_NH4PTR |
161 		    IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) == 0)
162 			break;
163 #if (!defined(INET6) && !defined(INET))
164 		ret = PFIL_DROPPED;
165 #else
166 	    {
167 		void *psa;
168 		size_t len;
169 #ifdef INET
170 		if (args.flags & (IPFW_ARGS_NH4 | IPFW_ARGS_NH4PTR)) {
171 			MPASS((args.flags & (IPFW_ARGS_NH4 |
172 			    IPFW_ARGS_NH4PTR)) != (IPFW_ARGS_NH4 |
173 			    IPFW_ARGS_NH4PTR));
174 			MPASS((args.flags & (IPFW_ARGS_NH6 |
175 			    IPFW_ARGS_NH6PTR)) == 0);
176 			len = sizeof(struct sockaddr_in);
177 			psa = (args.flags & IPFW_ARGS_NH4) ?
178 			    &args.hopstore : args.next_hop;
179 			if (in_localip(satosin(psa)->sin_addr))
180 				(*m0)->m_flags |= M_FASTFWD_OURS;
181 			(*m0)->m_flags |= M_IP_NEXTHOP;
182 		}
183 #endif /* INET */
184 #ifdef INET6
185 		if (args.flags & (IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) {
186 			MPASS((args.flags & (IPFW_ARGS_NH6 |
187 			    IPFW_ARGS_NH6PTR)) != (IPFW_ARGS_NH6 |
188 			    IPFW_ARGS_NH6PTR));
189 			MPASS((args.flags & (IPFW_ARGS_NH4 |
190 			    IPFW_ARGS_NH4PTR)) == 0);
191 			len = sizeof(struct sockaddr_in6);
192 			psa = args.next_hop6;
193 			(*m0)->m_flags |= M_IP6_NEXTHOP;
194 		}
195 #endif /* INET6 */
196 		/*
197 		 * Incoming packets should not be tagged so we do not
198 		 * m_tag_find. Outgoing packets may be tagged, so we
199 		 * reuse the tag if present.
200 		 */
201 		tag = (dir == DIR_IN) ? NULL :
202 			m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
203 		if (tag != NULL) {
204 			m_tag_unlink(*m0, tag);
205 		} else {
206 			tag = m_tag_get(PACKET_TAG_IPFORWARD, len,
207 			    M_NOWAIT);
208 			if (tag == NULL) {
209 				ret = PFIL_DROPPED;
210 				break;
211 			}
212 		}
213 		if ((args.flags & IPFW_ARGS_NH6) == 0)
214 			bcopy(psa, tag + 1, len);
215 		m_tag_prepend(*m0, tag);
216 		ret = 0;
217 #ifdef INET6
218 		/* IPv6 next hop needs additional handling */
219 		if (args.flags & (IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) {
220 			struct sockaddr_in6 *sa6;
221 
222 			sa6 = satosin6(tag + 1);
223 			if (args.flags & IPFW_ARGS_NH6) {
224 				sa6->sin6_family = AF_INET6;
225 				sa6->sin6_len = sizeof(*sa6);
226 				sa6->sin6_addr = args.hopstore6.sin6_addr;
227 				sa6->sin6_port = args.hopstore6.sin6_port;
228 				sa6->sin6_scope_id =
229 				    args.hopstore6.sin6_scope_id;
230 			}
231 			/*
232 			 * If nh6 address is link-local we should convert
233 			 * it to kernel internal form before doing any
234 			 * comparisons.
235 			 */
236 			if (sa6_embedscope(sa6, V_ip6_use_defzone) != 0) {
237 				ret = PFIL_DROPPED;
238 				break;
239 			}
240 			if (in6_localip(&sa6->sin6_addr))
241 				(*m0)->m_flags |= M_FASTFWD_OURS;
242 		}
243 #endif /* INET6 */
244 	    }
245 #endif /* INET || INET6 */
246 		break;
247 
248 	case IP_FW_DENY:
249 		ret = PFIL_DROPPED;
250 		break;
251 
252 	case IP_FW_DUMMYNET:
253 		if (ip_dn_io_ptr == NULL) {
254 			ret = PFIL_DROPPED;
255 			break;
256 		}
257 		MPASS(args.flags & IPFW_ARGS_REF);
258 		if (mtod(*m0, struct ip *)->ip_v == 4)
259 			(void )ip_dn_io_ptr(m0, dir, &args);
260 		else if (mtod(*m0, struct ip *)->ip_v == 6)
261 			(void )ip_dn_io_ptr(m0, dir | PROTO_IPV6, &args);
262 		else {
263 			ret = PFIL_DROPPED;
264 			break;
265 		}
266 		/*
267 		 * XXX should read the return value.
268 		 * dummynet normally eats the packet and sets *m0=NULL
269 		 * unless the packet can be sent immediately. In this
270 		 * case args is updated and we should re-run the
271 		 * check without clearing args.
272 		 */
273 		if (*m0 != NULL)
274 			goto again;
275 		ret = PFIL_CONSUMED;
276 		break;
277 
278 	case IP_FW_TEE:
279 	case IP_FW_DIVERT:
280 		if (ip_divert_ptr == NULL) {
281 			ret = PFIL_DROPPED;
282 			break;
283 		}
284 		MPASS(args.flags & IPFW_ARGS_REF);
285 		(void )ipfw_divert(m0, dir, &args.rule,
286 			(ipfw == IP_FW_TEE) ? 1 : 0);
287 		/* continue processing for the original packet (tee). */
288 		if (*m0)
289 			goto again;
290 		ret = PFIL_CONSUMED;
291 		break;
292 
293 	case IP_FW_NGTEE:
294 	case IP_FW_NETGRAPH:
295 		if (ng_ipfw_input_p == NULL) {
296 			ret = PFIL_DROPPED;
297 			break;
298 		}
299 		MPASS(args.flags & IPFW_ARGS_REF);
300 		(void )ng_ipfw_input_p(m0, dir, &args,
301 			(ipfw == IP_FW_NGTEE) ? 1 : 0);
302 		if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */
303 			goto again;	/* continue with packet */
304 		ret = PFIL_CONSUMED;
305 		break;
306 
307 	case IP_FW_NAT:
308 		/* honor one-pass in case of successful nat */
309 		if (V_fw_one_pass)
310 			break;
311 		goto again;
312 
313 	case IP_FW_REASS:
314 		goto again;		/* continue with packet */
315 
316 	default:
317 		KASSERT(0, ("%s: unknown retval", __func__));
318 	}
319 
320 	if (ret != PFIL_PASS) {
321 		if (*m0)
322 			FREE_PKT(*m0);
323 		*m0 = NULL;
324 	}
325 
326 	return (ret);
327 }
328 
329 /*
330  * ipfw processing for ethernet packets (in and out).
331  */
332 static pfil_return_t
333 ipfw_check_frame(struct mbuf **m0, struct ifnet *ifp, int dir,
334     void *ruleset __unused, struct inpcb *inp)
335 {
336 	struct ip_fw_args args;
337 	struct ether_header save_eh;
338 	struct ether_header *eh;
339 	struct m_tag *mtag;
340 	struct mbuf *m;
341 	pfil_return_t ret;
342 	int i;
343 
344 	args.flags = IPFW_ARGS_ETHER;
345 again:
346 	/* fetch start point from rule, if any.  remove the tag if present. */
347 	mtag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
348 	if (mtag != NULL) {
349 		args.rule = *((struct ipfw_rule_ref *)(mtag+1));
350 		m_tag_delete(*m0, mtag);
351 		if (args.rule.info & IPFW_ONEPASS)
352 			return (0);
353 		args.flags |= IPFW_ARGS_REF;
354 	}
355 
356 	/* I need some amt of data to be contiguous */
357 	m = *m0;
358 	i = min(m->m_pkthdr.len, max_protohdr);
359 	if (m->m_len < i) {
360 		m = m_pullup(m, i);
361 		if (m == NULL) {
362 			*m0 = m;
363 			return (0);
364 		}
365 	}
366 	eh = mtod(m, struct ether_header *);
367 	save_eh = *eh;			/* save copy for restore below */
368 	m_adj(m, ETHER_HDR_LEN);	/* strip ethernet header */
369 
370 	args.m = m;		/* the packet we are looking at		*/
371 	args.oif = dir & PFIL_OUT ? ifp: NULL;	/* destination, if any	*/
372 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
373 	args.inp = inp;	/* used by ipfw uid/gid/jail rules	*/
374 	i = ipfw_chk(&args);
375 	m = args.m;
376 	if (m != NULL) {
377 		/*
378 		 * Restore Ethernet header, as needed, in case the
379 		 * mbuf chain was replaced by ipfw.
380 		 */
381 		M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
382 		if (m == NULL) {
383 			*m0 = NULL;
384 			return (0);
385 		}
386 		if (eh != mtod(m, struct ether_header *))
387 			bcopy(&save_eh, mtod(m, struct ether_header *),
388 				ETHER_HDR_LEN);
389 	}
390 	*m0 = m;
391 
392 	ret = PFIL_PASS;
393 	/* Check result of ipfw_chk() */
394 	switch (i) {
395 	case IP_FW_PASS:
396 		break;
397 
398 	case IP_FW_DENY:
399 		ret = PFIL_DROPPED;
400 		break;
401 
402 	case IP_FW_DUMMYNET:
403 		if (ip_dn_io_ptr == NULL) {
404 			ret = PFIL_DROPPED;
405 			break;
406 		}
407 		*m0 = NULL;
408 		dir = (dir & PFIL_IN) ? DIR_IN : DIR_OUT;
409 		MPASS(args.flags & IPFW_ARGS_REF);
410 		ip_dn_io_ptr(&m, dir | PROTO_LAYER2, &args);
411 		return (PFIL_CONSUMED);
412 
413 	case IP_FW_NGTEE:
414 	case IP_FW_NETGRAPH:
415 		if (ng_ipfw_input_p == NULL) {
416 			ret = PFIL_DROPPED;
417 			break;
418 		}
419 		MPASS(args.flags & IPFW_ARGS_REF);
420 		(void )ng_ipfw_input_p(m0, (dir & PFIL_IN) ? DIR_IN : DIR_OUT,
421 			&args, (i == IP_FW_NGTEE) ? 1 : 0);
422 		if (i == IP_FW_NGTEE) /* ignore errors for NGTEE */
423 			goto again;	/* continue with packet */
424 		ret = PFIL_CONSUMED;
425 		break;
426 
427 	default:
428 		KASSERT(0, ("%s: unknown retval", __func__));
429 	}
430 
431 	if (ret != PFIL_PASS) {
432 		if (*m0)
433 			FREE_PKT(*m0);
434 		*m0 = NULL;
435 	}
436 
437 	return (ret);
438 }
439 
440 /* do the divert, return 1 on error 0 on success */
441 static int
442 ipfw_divert(struct mbuf **m0, int incoming, struct ipfw_rule_ref *rule,
443 	int tee)
444 {
445 	/*
446 	 * ipfw_chk() has already tagged the packet with the divert tag.
447 	 * If tee is set, copy packet and return original.
448 	 * If not tee, consume packet and send it to divert socket.
449 	 */
450 	struct mbuf *clone;
451 	struct ip *ip = mtod(*m0, struct ip *);
452 	struct m_tag *tag;
453 
454 	/* Cloning needed for tee? */
455 	if (tee == 0) {
456 		clone = *m0;	/* use the original mbuf */
457 		*m0 = NULL;
458 	} else {
459 		clone = m_dup(*m0, M_NOWAIT);
460 		/* If we cannot duplicate the mbuf, we sacrifice the divert
461 		 * chain and continue with the tee-ed packet.
462 		 */
463 		if (clone == NULL)
464 			return 1;
465 	}
466 
467 	/*
468 	 * Divert listeners can normally handle non-fragmented packets,
469 	 * but we can only reass in the non-tee case.
470 	 * This means that listeners on a tee rule may get fragments,
471 	 * and have to live with that.
472 	 * Note that we now have the 'reass' ipfw option so if we care
473 	 * we can do it before a 'tee'.
474 	 */
475 	if (!tee) switch (ip->ip_v) {
476 	case IPVERSION:
477 	    if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) {
478 		int hlen;
479 		struct mbuf *reass;
480 
481 		reass = ip_reass(clone); /* Reassemble packet. */
482 		if (reass == NULL)
483 			return 0; /* not an error */
484 		/* if reass = NULL then it was consumed by ip_reass */
485 		/*
486 		 * IP header checksum fixup after reassembly and leave header
487 		 * in network byte order.
488 		 */
489 		ip = mtod(reass, struct ip *);
490 		hlen = ip->ip_hl << 2;
491 		ip->ip_sum = 0;
492 		if (hlen == sizeof(struct ip))
493 			ip->ip_sum = in_cksum_hdr(ip);
494 		else
495 			ip->ip_sum = in_cksum(reass, hlen);
496 		clone = reass;
497 	    }
498 	    break;
499 #ifdef INET6
500 	case IPV6_VERSION >> 4:
501 	    {
502 	    struct ip6_hdr *const ip6 = mtod(clone, struct ip6_hdr *);
503 
504 		if (ip6->ip6_nxt == IPPROTO_FRAGMENT) {
505 			int nxt, off;
506 
507 			off = sizeof(struct ip6_hdr);
508 			nxt = frag6_input(&clone, &off, 0);
509 			if (nxt == IPPROTO_DONE)
510 				return (0);
511 		}
512 		break;
513 	    }
514 #endif
515 	}
516 
517 	/* attach a tag to the packet with the reinject info */
518 	tag = m_tag_alloc(MTAG_IPFW_RULE, 0,
519 		    sizeof(struct ipfw_rule_ref), M_NOWAIT);
520 	if (tag == NULL) {
521 		FREE_PKT(clone);
522 		return 1;
523 	}
524 	*((struct ipfw_rule_ref *)(tag+1)) = *rule;
525 	m_tag_prepend(clone, tag);
526 
527 	/* Do the dirty job... */
528 	ip_divert_ptr(clone, incoming);
529 	return 0;
530 }
531 
532 /*
533  * attach or detach hooks for a given protocol family
534  */
535 VNET_DEFINE_STATIC(pfil_hook_t, ipfw_inet_hook);
536 #define	V_ipfw_inet_hook	VNET(ipfw_inet_hook)
537 #ifdef INET6
538 VNET_DEFINE_STATIC(pfil_hook_t, ipfw_inet6_hook);
539 #define	V_ipfw_inet6_hook	VNET(ipfw_inet6_hook)
540 #endif
541 VNET_DEFINE_STATIC(pfil_hook_t, ipfw_link_hook);
542 #define	V_ipfw_link_hook	VNET(ipfw_link_hook)
543 
544 static int
545 ipfw_hook(int onoff, int pf)
546 {
547 	struct pfil_hook_args pha;
548 	struct pfil_link_args pla;
549 	pfil_hook_t *h;
550 
551 	pha.pa_version = PFIL_VERSION;
552 	pha.pa_flags = PFIL_IN | PFIL_OUT;
553 	pha.pa_modname = "ipfw";
554 	pha.pa_ruleset = NULL;
555 
556 	pla.pa_version = PFIL_VERSION;
557 	pla.pa_flags = PFIL_IN | PFIL_OUT |
558 	    PFIL_HEADPTR | PFIL_HOOKPTR;
559 
560 	switch (pf) {
561 	case AF_INET:
562 		pha.pa_func = ipfw_check_packet;
563 		pha.pa_type = PFIL_TYPE_IP4;
564 		pha.pa_rulname = "default";
565 		h = &V_ipfw_inet_hook;
566 		pla.pa_head = V_inet_pfil_head;
567 		break;
568 #ifdef INET6
569 	case AF_INET6:
570 		pha.pa_func = ipfw_check_packet;
571 		pha.pa_type = PFIL_TYPE_IP6;
572 		pha.pa_rulname = "default6";
573 		h = &V_ipfw_inet6_hook;
574 		pla.pa_head = V_inet6_pfil_head;
575 		break;
576 #endif
577 	case AF_LINK:
578 		pha.pa_func = ipfw_check_frame;
579 		pha.pa_type = PFIL_TYPE_ETHERNET;
580 		pha.pa_rulname = "default-link";
581 		h = &V_ipfw_link_hook;
582 		pla.pa_head = V_link_pfil_head;
583 		break;
584 	}
585 
586 	if (onoff) {
587 		*h = pfil_add_hook(&pha);
588 		pla.pa_hook = *h;
589 		(void)pfil_link(&pla);
590 	} else
591 		if (*h != NULL)
592 			pfil_remove_hook(*h);
593 
594 	return 0;
595 }
596 
597 int
598 ipfw_attach_hooks(int arg)
599 {
600 	int error = 0;
601 
602 	if (arg == 0) /* detach */
603 		ipfw_hook(0, AF_INET);
604 	else if (V_fw_enable && ipfw_hook(1, AF_INET) != 0) {
605                 error = ENOENT; /* see ip_fw_pfil.c::ipfw_hook() */
606                 printf("ipfw_hook() error\n");
607         }
608 #ifdef INET6
609 	if (arg == 0) /* detach */
610 		ipfw_hook(0, AF_INET6);
611 	else if (V_fw6_enable && ipfw_hook(1, AF_INET6) != 0) {
612                 error = ENOENT;
613                 printf("ipfw6_hook() error\n");
614         }
615 #endif
616 	if (arg == 0) /* detach */
617 		ipfw_hook(0, AF_LINK);
618 	else if (V_fwlink_enable && ipfw_hook(1, AF_LINK) != 0) {
619                 error = ENOENT;
620                 printf("ipfw_link_hook() error\n");
621         }
622 	return error;
623 }
624 
625 int
626 ipfw_chg_hook(SYSCTL_HANDLER_ARGS)
627 {
628 	int newval;
629 	int error;
630 	int af;
631 
632 	if (arg1 == &V_fw_enable)
633 		af = AF_INET;
634 #ifdef INET6
635 	else if (arg1 == &V_fw6_enable)
636 		af = AF_INET6;
637 #endif
638 	else if (arg1 == &V_fwlink_enable)
639 		af = AF_LINK;
640 	else
641 		return (EINVAL);
642 
643 	newval = *(int *)arg1;
644 	/* Handle sysctl change */
645 	error = sysctl_handle_int(oidp, &newval, 0, req);
646 
647 	if (error)
648 		return (error);
649 
650 	/* Formalize new value */
651 	newval = (newval) ? 1 : 0;
652 
653 	if (*(int *)arg1 == newval)
654 		return (0);
655 
656 	error = ipfw_hook(newval, af);
657 	if (error)
658 		return (error);
659 	*(int *)arg1 = newval;
660 
661 	return (0);
662 }
663 /* end of file */
664