]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netpfil/ipfw/ip_fw_pfil.c
Remove 'dir' argument in ng_ipfw_input, since ip_fw_args now has this info.
[FreeBSD/FreeBSD.git] / sys / netpfil / ipfw / ip_fw_pfil.c
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 **, bool, 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 flags,
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, dir;
128
129         args.flags = (flags & PFIL_IN) ? IPFW_ARGS_IN : IPFW_ARGS_OUT;
130         dir = (flags & PFIL_IN) ? DIR_IN : DIR_OUT;
131 again:
132         /*
133          * extract and remove the tag if present. If we are left
134          * with onepass, optimize the outgoing path.
135          */
136         tag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
137         if (tag != NULL) {
138                 args.rule = *((struct ipfw_rule_ref *)(tag+1));
139                 m_tag_delete(*m0, tag);
140                 if (args.rule.info & IPFW_ONEPASS)
141                         return (0);
142                 args.flags |= IPFW_ARGS_REF;
143         }
144
145         args.m = *m0;
146         args.ifp = ifp;
147         args.inp = inp;
148
149         ipfw = ipfw_chk(&args);
150         *m0 = args.m;
151
152         KASSERT(*m0 != NULL || ipfw == IP_FW_DENY, ("%s: m0 is NULL",
153             __func__));
154
155         ret = PFIL_PASS;
156         switch (ipfw) {
157         case IP_FW_PASS:
158                 /* next_hop may be set by ipfw_chk */
159                 if ((args.flags & (IPFW_ARGS_NH4 | IPFW_ARGS_NH4PTR |
160                     IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) == 0)
161                         break;
162 #if (!defined(INET6) && !defined(INET))
163                 ret = PFIL_DROPPED;
164 #else
165             {
166                 void *psa;
167                 size_t len;
168 #ifdef INET
169                 if (args.flags & (IPFW_ARGS_NH4 | IPFW_ARGS_NH4PTR)) {
170                         MPASS((args.flags & (IPFW_ARGS_NH4 |
171                             IPFW_ARGS_NH4PTR)) != (IPFW_ARGS_NH4 |
172                             IPFW_ARGS_NH4PTR));
173                         MPASS((args.flags & (IPFW_ARGS_NH6 |
174                             IPFW_ARGS_NH6PTR)) == 0);
175                         len = sizeof(struct sockaddr_in);
176                         psa = (args.flags & IPFW_ARGS_NH4) ?
177                             &args.hopstore : args.next_hop;
178                         if (in_localip(satosin(psa)->sin_addr))
179                                 (*m0)->m_flags |= M_FASTFWD_OURS;
180                         (*m0)->m_flags |= M_IP_NEXTHOP;
181                 }
182 #endif /* INET */
183 #ifdef INET6
184                 if (args.flags & (IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) {
185                         MPASS((args.flags & (IPFW_ARGS_NH6 |
186                             IPFW_ARGS_NH6PTR)) != (IPFW_ARGS_NH6 |
187                             IPFW_ARGS_NH6PTR));
188                         MPASS((args.flags & (IPFW_ARGS_NH4 |
189                             IPFW_ARGS_NH4PTR)) == 0);
190                         len = sizeof(struct sockaddr_in6);
191                         psa = args.next_hop6;
192                         (*m0)->m_flags |= M_IP6_NEXTHOP;
193                 }
194 #endif /* INET6 */
195                 /*
196                  * Incoming packets should not be tagged so we do not
197                  * m_tag_find. Outgoing packets may be tagged, so we
198                  * reuse the tag if present.
199                  */
200                 tag = (flags & PFIL_IN) ? NULL :
201                         m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
202                 if (tag != NULL) {
203                         m_tag_unlink(*m0, tag);
204                 } else {
205                         tag = m_tag_get(PACKET_TAG_IPFORWARD, len,
206                             M_NOWAIT);
207                         if (tag == NULL) {
208                                 ret = PFIL_DROPPED;
209                                 break;
210                         }
211                 }
212                 if ((args.flags & IPFW_ARGS_NH6) == 0)
213                         bcopy(psa, tag + 1, len);
214                 m_tag_prepend(*m0, tag);
215                 ret = 0;
216 #ifdef INET6
217                 /* IPv6 next hop needs additional handling */
218                 if (args.flags & (IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) {
219                         struct sockaddr_in6 *sa6;
220
221                         sa6 = satosin6(tag + 1);
222                         if (args.flags & IPFW_ARGS_NH6) {
223                                 sa6->sin6_family = AF_INET6;
224                                 sa6->sin6_len = sizeof(*sa6);
225                                 sa6->sin6_addr = args.hopstore6.sin6_addr;
226                                 sa6->sin6_port = args.hopstore6.sin6_port;
227                                 sa6->sin6_scope_id =
228                                     args.hopstore6.sin6_scope_id;
229                         }
230                         /*
231                          * If nh6 address is link-local we should convert
232                          * it to kernel internal form before doing any
233                          * comparisons.
234                          */
235                         if (sa6_embedscope(sa6, V_ip6_use_defzone) != 0) {
236                                 ret = PFIL_DROPPED;
237                                 break;
238                         }
239                         if (in6_localip(&sa6->sin6_addr))
240                                 (*m0)->m_flags |= M_FASTFWD_OURS;
241                 }
242 #endif /* INET6 */
243             }
244 #endif /* INET || INET6 */
245                 break;
246
247         case IP_FW_DENY:
248                 ret = PFIL_DROPPED;
249                 break;
250
251         case IP_FW_DUMMYNET:
252                 if (ip_dn_io_ptr == NULL) {
253                         ret = PFIL_DROPPED;
254                         break;
255                 }
256                 MPASS(args.flags & IPFW_ARGS_REF);
257                 if (mtod(*m0, struct ip *)->ip_v == 4)
258                         (void )ip_dn_io_ptr(m0, dir, &args);
259                 else if (mtod(*m0, struct ip *)->ip_v == 6)
260                         (void )ip_dn_io_ptr(m0, dir | PROTO_IPV6, &args);
261                 else {
262                         ret = PFIL_DROPPED;
263                         break;
264                 }
265                 /*
266                  * XXX should read the return value.
267                  * dummynet normally eats the packet and sets *m0=NULL
268                  * unless the packet can be sent immediately. In this
269                  * case args is updated and we should re-run the
270                  * check without clearing args.
271                  */
272                 if (*m0 != NULL)
273                         goto again;
274                 ret = PFIL_CONSUMED;
275                 break;
276
277         case IP_FW_TEE:
278         case IP_FW_DIVERT:
279                 if (ip_divert_ptr == NULL) {
280                         ret = PFIL_DROPPED;
281                         break;
282                 }
283                 MPASS(args.flags & IPFW_ARGS_REF);
284                 (void )ipfw_divert(m0, dir == DIR_IN, &args.rule,
285                         (ipfw == IP_FW_TEE) ? 1 : 0);
286                 /* continue processing for the original packet (tee). */
287                 if (*m0)
288                         goto again;
289                 ret = PFIL_CONSUMED;
290                 break;
291
292         case IP_FW_NGTEE:
293         case IP_FW_NETGRAPH:
294                 if (ng_ipfw_input_p == NULL) {
295                         ret = PFIL_DROPPED;
296                         break;
297                 }
298                 MPASS(args.flags & IPFW_ARGS_REF);
299                 (void )ng_ipfw_input_p(m0, &args, ipfw == IP_FW_NGTEE);
300                 if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */
301                         goto again;     /* continue with packet */
302                 ret = PFIL_CONSUMED;
303                 break;
304
305         case IP_FW_NAT:
306                 /* honor one-pass in case of successful nat */
307                 if (V_fw_one_pass)
308                         break;
309                 goto again;
310
311         case IP_FW_REASS:
312                 goto again;             /* continue with packet */
313
314         case IP_FW_NAT64:
315                 ret = PFIL_CONSUMED;
316                 break;
317
318         default:
319                 KASSERT(0, ("%s: unknown retval", __func__));
320         }
321
322         if (ret != PFIL_PASS) {
323                 if (*m0)
324                         FREE_PKT(*m0);
325                 *m0 = NULL;
326         }
327
328         return (ret);
329 }
330
331 /*
332  * ipfw processing for ethernet packets (in and out).
333  */
334 static pfil_return_t
335 ipfw_check_frame(struct mbuf **m0, struct ifnet *ifp, int dir,
336     void *ruleset __unused, struct inpcb *inp)
337 {
338         struct ip_fw_args args;
339         struct ether_header save_eh;
340         struct ether_header *eh;
341         struct m_tag *mtag;
342         struct mbuf *m;
343         pfil_return_t ret;
344         int i;
345
346         args.flags = IPFW_ARGS_ETHER;
347         args.flags |= (dir & PFIL_IN) ? IPFW_ARGS_IN : IPFW_ARGS_OUT;
348 again:
349         /* fetch start point from rule, if any.  remove the tag if present. */
350         mtag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
351         if (mtag != NULL) {
352                 args.rule = *((struct ipfw_rule_ref *)(mtag+1));
353                 m_tag_delete(*m0, mtag);
354                 if (args.rule.info & IPFW_ONEPASS)
355                         return (0);
356                 args.flags |= IPFW_ARGS_REF;
357         }
358
359         /* I need some amt of data to be contiguous */
360         m = *m0;
361         i = min(m->m_pkthdr.len, max_protohdr);
362         if (m->m_len < i) {
363                 m = m_pullup(m, i);
364                 if (m == NULL) {
365                         *m0 = m;
366                         return (0);
367                 }
368         }
369         eh = mtod(m, struct ether_header *);
370         save_eh = *eh;                  /* save copy for restore below */
371         m_adj(m, ETHER_HDR_LEN);        /* strip ethernet header */
372
373         args.m = m;             /* the packet we are looking at         */
374         args.ifp = ifp;
375         args.eh = &save_eh;     /* MAC header for bridged/MAC packets   */
376         args.inp = inp; /* used by ipfw uid/gid/jail rules      */
377         i = ipfw_chk(&args);
378         m = args.m;
379         if (m != NULL) {
380                 /*
381                  * Restore Ethernet header, as needed, in case the
382                  * mbuf chain was replaced by ipfw.
383                  */
384                 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
385                 if (m == NULL) {
386                         *m0 = NULL;
387                         return (0);
388                 }
389                 if (eh != mtod(m, struct ether_header *))
390                         bcopy(&save_eh, mtod(m, struct ether_header *),
391                                 ETHER_HDR_LEN);
392         }
393         *m0 = m;
394
395         ret = PFIL_PASS;
396         /* Check result of ipfw_chk() */
397         switch (i) {
398         case IP_FW_PASS:
399                 break;
400
401         case IP_FW_DENY:
402                 ret = PFIL_DROPPED;
403                 break;
404
405         case IP_FW_DUMMYNET:
406                 if (ip_dn_io_ptr == NULL) {
407                         ret = PFIL_DROPPED;
408                         break;
409                 }
410                 *m0 = NULL;
411                 dir = (dir & PFIL_IN) ? DIR_IN : DIR_OUT;
412                 MPASS(args.flags & IPFW_ARGS_REF);
413                 ip_dn_io_ptr(&m, dir | PROTO_LAYER2, &args);
414                 return (PFIL_CONSUMED);
415
416         case IP_FW_NGTEE:
417         case IP_FW_NETGRAPH:
418                 if (ng_ipfw_input_p == NULL) {
419                         ret = PFIL_DROPPED;
420                         break;
421                 }
422                 MPASS(args.flags & IPFW_ARGS_REF);
423                 (void )ng_ipfw_input_p(m0, &args, i == IP_FW_NGTEE);
424                 if (i == IP_FW_NGTEE) /* ignore errors for NGTEE */
425                         goto again;     /* continue with packet */
426                 ret = PFIL_CONSUMED;
427                 break;
428
429         default:
430                 KASSERT(0, ("%s: unknown retval", __func__));
431         }
432
433         if (ret != PFIL_PASS) {
434                 if (*m0)
435                         FREE_PKT(*m0);
436                 *m0 = NULL;
437         }
438
439         return (ret);
440 }
441
442 /* do the divert, return 1 on error 0 on success */
443 static int
444 ipfw_divert(struct mbuf **m0, bool incoming, struct ipfw_rule_ref *rule,
445         int tee)
446 {
447         /*
448          * ipfw_chk() has already tagged the packet with the divert tag.
449          * If tee is set, copy packet and return original.
450          * If not tee, consume packet and send it to divert socket.
451          */
452         struct mbuf *clone;
453         struct ip *ip = mtod(*m0, struct ip *);
454         struct m_tag *tag;
455
456         /* Cloning needed for tee? */
457         if (tee == 0) {
458                 clone = *m0;    /* use the original mbuf */
459                 *m0 = NULL;
460         } else {
461                 clone = m_dup(*m0, M_NOWAIT);
462                 /* If we cannot duplicate the mbuf, we sacrifice the divert
463                  * chain and continue with the tee-ed packet.
464                  */
465                 if (clone == NULL)
466                         return 1;
467         }
468
469         /*
470          * Divert listeners can normally handle non-fragmented packets,
471          * but we can only reass in the non-tee case.
472          * This means that listeners on a tee rule may get fragments,
473          * and have to live with that.
474          * Note that we now have the 'reass' ipfw option so if we care
475          * we can do it before a 'tee'.
476          */
477         if (!tee) switch (ip->ip_v) {
478         case IPVERSION:
479             if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) {
480                 int hlen;
481                 struct mbuf *reass;
482
483                 reass = ip_reass(clone); /* Reassemble packet. */
484                 if (reass == NULL)
485                         return 0; /* not an error */
486                 /* if reass = NULL then it was consumed by ip_reass */
487                 /*
488                  * IP header checksum fixup after reassembly and leave header
489                  * in network byte order.
490                  */
491                 ip = mtod(reass, struct ip *);
492                 hlen = ip->ip_hl << 2;
493                 ip->ip_sum = 0;
494                 if (hlen == sizeof(struct ip))
495                         ip->ip_sum = in_cksum_hdr(ip);
496                 else
497                         ip->ip_sum = in_cksum(reass, hlen);
498                 clone = reass;
499             }
500             break;
501 #ifdef INET6
502         case IPV6_VERSION >> 4:
503             {
504             struct ip6_hdr *const ip6 = mtod(clone, struct ip6_hdr *);
505
506                 if (ip6->ip6_nxt == IPPROTO_FRAGMENT) {
507                         int nxt, off;
508
509                         off = sizeof(struct ip6_hdr);
510                         nxt = frag6_input(&clone, &off, 0);
511                         if (nxt == IPPROTO_DONE)
512                                 return (0);
513                 }
514                 break;
515             }
516 #endif
517         }
518
519         /* attach a tag to the packet with the reinject info */
520         tag = m_tag_alloc(MTAG_IPFW_RULE, 0,
521                     sizeof(struct ipfw_rule_ref), M_NOWAIT);
522         if (tag == NULL) {
523                 FREE_PKT(clone);
524                 return 1;
525         }
526         *((struct ipfw_rule_ref *)(tag+1)) = *rule;
527         m_tag_prepend(clone, tag);
528
529         /* Do the dirty job... */
530         ip_divert_ptr(clone, incoming);
531         return 0;
532 }
533
534 /*
535  * attach or detach hooks for a given protocol family
536  */
537 VNET_DEFINE_STATIC(pfil_hook_t, ipfw_inet_hook);
538 #define V_ipfw_inet_hook        VNET(ipfw_inet_hook)
539 #ifdef INET6
540 VNET_DEFINE_STATIC(pfil_hook_t, ipfw_inet6_hook);
541 #define V_ipfw_inet6_hook       VNET(ipfw_inet6_hook)
542 #endif
543 VNET_DEFINE_STATIC(pfil_hook_t, ipfw_link_hook);
544 #define V_ipfw_link_hook        VNET(ipfw_link_hook)
545
546 static int
547 ipfw_hook(int onoff, int pf)
548 {
549         struct pfil_hook_args pha;
550         struct pfil_link_args pla;
551         pfil_hook_t *h;
552
553         pha.pa_version = PFIL_VERSION;
554         pha.pa_flags = PFIL_IN | PFIL_OUT;
555         pha.pa_modname = "ipfw";
556         pha.pa_ruleset = NULL;
557
558         pla.pa_version = PFIL_VERSION;
559         pla.pa_flags = PFIL_IN | PFIL_OUT |
560             PFIL_HEADPTR | PFIL_HOOKPTR;
561
562         switch (pf) {
563         case AF_INET:
564                 pha.pa_func = ipfw_check_packet;
565                 pha.pa_type = PFIL_TYPE_IP4;
566                 pha.pa_rulname = "default";
567                 h = &V_ipfw_inet_hook;
568                 pla.pa_head = V_inet_pfil_head;
569                 break;
570 #ifdef INET6
571         case AF_INET6:
572                 pha.pa_func = ipfw_check_packet;
573                 pha.pa_type = PFIL_TYPE_IP6;
574                 pha.pa_rulname = "default6";
575                 h = &V_ipfw_inet6_hook;
576                 pla.pa_head = V_inet6_pfil_head;
577                 break;
578 #endif
579         case AF_LINK:
580                 pha.pa_func = ipfw_check_frame;
581                 pha.pa_type = PFIL_TYPE_ETHERNET;
582                 pha.pa_rulname = "default-link";
583                 h = &V_ipfw_link_hook;
584                 pla.pa_head = V_link_pfil_head;
585                 break;
586         }
587
588         if (onoff) {
589                 *h = pfil_add_hook(&pha);
590                 pla.pa_hook = *h;
591                 (void)pfil_link(&pla);
592         } else
593                 if (*h != NULL)
594                         pfil_remove_hook(*h);
595
596         return 0;
597 }
598
599 int
600 ipfw_attach_hooks(int arg)
601 {
602         int error = 0;
603
604         if (arg == 0) /* detach */
605                 ipfw_hook(0, AF_INET);
606         else if (V_fw_enable && ipfw_hook(1, AF_INET) != 0) {
607                 error = ENOENT; /* see ip_fw_pfil.c::ipfw_hook() */
608                 printf("ipfw_hook() error\n");
609         }
610 #ifdef INET6
611         if (arg == 0) /* detach */
612                 ipfw_hook(0, AF_INET6);
613         else if (V_fw6_enable && ipfw_hook(1, AF_INET6) != 0) {
614                 error = ENOENT;
615                 printf("ipfw6_hook() error\n");
616         }
617 #endif
618         if (arg == 0) /* detach */
619                 ipfw_hook(0, AF_LINK);
620         else if (V_fwlink_enable && ipfw_hook(1, AF_LINK) != 0) {
621                 error = ENOENT;
622                 printf("ipfw_link_hook() error\n");
623         }
624         return error;
625 }
626
627 int
628 ipfw_chg_hook(SYSCTL_HANDLER_ARGS)
629 {
630         int newval;
631         int error;
632         int af;
633
634         if (arg1 == &V_fw_enable)
635                 af = AF_INET;
636 #ifdef INET6
637         else if (arg1 == &V_fw6_enable)
638                 af = AF_INET6;
639 #endif
640         else if (arg1 == &V_fwlink_enable)
641                 af = AF_LINK;
642         else 
643                 return (EINVAL);
644
645         newval = *(int *)arg1;
646         /* Handle sysctl change */
647         error = sysctl_handle_int(oidp, &newval, 0, req);
648
649         if (error)
650                 return (error);
651
652         /* Formalize new value */
653         newval = (newval) ? 1 : 0;
654
655         if (*(int *)arg1 == newval)
656                 return (0);
657
658         error = ipfw_hook(newval, af);
659         if (error)
660                 return (error);
661         *(int *)arg1 = newval;
662
663         return (0);
664 }
665 /* end of file */