2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Andre Oppermann, Internet Business Solutions AG
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include "opt_inet6.h"
36 #error IPFIREWALL requires INET.
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/kernel.h>
46 #include <sys/rwlock.h>
47 #include <sys/socket.h>
48 #include <sys/sysctl.h>
51 #include <net/if_var.h>
52 #include <net/route.h>
53 #include <net/ethernet.h>
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>
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet6/scope6_var.h>
68 #include <netgraph/ng_ipfw.h>
70 #include <netpfil/ipfw/ip_fw_private.h>
72 #include <machine/in_cksum.h>
74 VNET_DEFINE_STATIC(int, fw_enable) = 1;
75 #define V_fw_enable VNET(fw_enable)
78 VNET_DEFINE_STATIC(int, fw6_enable) = 1;
79 #define V_fw6_enable VNET(fw6_enable)
82 VNET_DEFINE_STATIC(int, fwlink_enable) = 0;
83 #define V_fwlink_enable VNET(fwlink_enable)
85 int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
87 /* Forward declarations. */
88 static int ipfw_divert(struct mbuf **, struct ip_fw_args *, bool);
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 CTLFLAG_NEEDGIANT, &VNET_NAME(fw_enable), 0, ipfw_chg_hook, "I",
100 SYSCTL_DECL(_net_inet6_ip6_fw);
101 SYSCTL_PROC(_net_inet6_ip6_fw, OID_AUTO, enable,
102 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3 |
103 CTLFLAG_NEEDGIANT, &VNET_NAME(fw6_enable), 0, ipfw_chg_hook, "I",
107 SYSCTL_DECL(_net_link_ether);
108 SYSCTL_PROC(_net_link_ether, OID_AUTO, ipfw,
109 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3 |
110 CTLFLAG_NEEDGIANT, &VNET_NAME(fwlink_enable), 0, ipfw_chg_hook, "I",
111 "Pass ether pkts through firewall");
115 #endif /* SYSCTL_NODE */
118 * The pfilter hook to pass packets to ipfw_chk and then to
119 * dummynet, divert, netgraph or other modules.
120 * The packet may be consumed.
123 ipfw_check_packet(struct mbuf **m0, struct ifnet *ifp, int flags,
124 void *ruleset __unused, struct inpcb *inp)
126 struct ip_fw_args args;
131 args.flags = (flags & PFIL_IN) ? IPFW_ARGS_IN : IPFW_ARGS_OUT;
134 * extract and remove the tag if present. If we are left
135 * with onepass, optimize the outgoing path.
137 tag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
139 args.rule = *((struct ipfw_rule_ref *)(tag+1));
140 m_tag_delete(*m0, tag);
141 if (args.rule.info & IPFW_ONEPASS)
143 args.flags |= IPFW_ARGS_REF;
150 ipfw = ipfw_chk(&args);
153 KASSERT(*m0 != NULL || ipfw == IP_FW_DENY ||
154 ipfw == IP_FW_NAT64, ("%s: m0 is NULL", __func__));
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)
163 #if (!defined(INET6) && !defined(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 |
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;
185 if (args.flags & (IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) {
186 MPASS((args.flags & (IPFW_ARGS_NH6 |
187 IPFW_ARGS_NH6PTR)) != (IPFW_ARGS_NH6 |
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;
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.
201 tag = (flags & PFIL_IN) ? NULL :
202 m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
204 m_tag_unlink(*m0, tag);
206 tag = m_tag_get(PACKET_TAG_IPFORWARD, len,
213 if ((args.flags & IPFW_ARGS_NH6) == 0)
214 bcopy(psa, tag + 1, len);
215 m_tag_prepend(*m0, tag);
218 /* IPv6 next hop needs additional handling */
219 if (args.flags & (IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) {
220 struct sockaddr_in6 *sa6;
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;
229 args.hopstore6.sin6_scope_id;
232 * If nh6 address is link-local we should convert
233 * it to kernel internal form before doing any
236 if (sa6_embedscope(sa6, V_ip6_use_defzone) != 0) {
240 if (in6_localip(&sa6->sin6_addr))
241 (*m0)->m_flags |= M_FASTFWD_OURS;
245 #endif /* INET || INET6 */
253 if (ip_dn_io_ptr == NULL) {
257 MPASS(args.flags & IPFW_ARGS_REF);
258 if (args.flags & (IPFW_ARGS_IP4 | IPFW_ARGS_IP6))
259 (void )ip_dn_io_ptr(m0, &args);
265 * XXX should read the return value.
266 * dummynet normally eats the packet and sets *m0=NULL
267 * unless the packet can be sent immediately. In this
268 * case args is updated and we should re-run the
269 * check without clearing args.
278 if (ip_divert_ptr == NULL) {
282 MPASS(args.flags & IPFW_ARGS_REF);
283 (void )ipfw_divert(m0, &args, ipfw == IP_FW_TEE);
284 /* continue processing for the original packet (tee). */
292 if (ng_ipfw_input_p == NULL) {
296 MPASS(args.flags & IPFW_ARGS_REF);
297 (void )ng_ipfw_input_p(m0, &args, ipfw == IP_FW_NGTEE);
298 if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */
299 goto again; /* continue with packet */
304 /* honor one-pass in case of successful nat */
310 goto again; /* continue with packet */
317 KASSERT(0, ("%s: unknown retval", __func__));
320 if (ret != PFIL_PASS) {
330 * ipfw processing for ethernet packets (in and out).
333 ipfw_check_frame(pfil_packet_t p, struct ifnet *ifp, int flags,
334 void *ruleset __unused, struct inpcb *inp)
336 struct ip_fw_args args;
341 if (flags & PFIL_MEMPTR) {
344 args.flags = PFIL_LENGTH(flags) | IPFW_ARGS_ETHER;
347 mem = realloc = false;
348 args.flags = IPFW_ARGS_ETHER;
350 args.flags |= (flags & PFIL_IN) ? IPFW_ARGS_IN : IPFW_ARGS_OUT;
357 * Fetch start point from rule, if any.
358 * Remove the tag if present.
362 mtag = m_tag_locate(*p.m, MTAG_IPFW_RULE, 0, NULL);
364 args.rule = *((struct ipfw_rule_ref *)(mtag+1));
365 m_tag_delete(*p.m, mtag);
366 if (args.rule.info & IPFW_ONEPASS)
368 args.flags |= IPFW_ARGS_REF;
373 ipfw = ipfw_chk(&args);
385 if (ip_dn_io_ptr == NULL) {
390 if (pfil_realloc(&p, flags, ifp) != 0) {
397 MPASS(args.flags & IPFW_ARGS_REF);
398 ip_dn_io_ptr(p.m, &args);
399 return (PFIL_CONSUMED);
403 if (ng_ipfw_input_p == NULL) {
408 if (pfil_realloc(&p, flags, ifp) != 0) {
415 MPASS(args.flags & IPFW_ARGS_REF);
416 (void )ng_ipfw_input_p(p.m, &args, ipfw == IP_FW_NGTEE);
417 if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */
418 goto again; /* continue with packet */
423 KASSERT(0, ("%s: unknown retval", __func__));
426 if (!mem && ret != PFIL_PASS) {
432 if (realloc && ret == PFIL_PASS)
433 ret = PFIL_REALLOCED;
438 /* do the divert, return 1 on error 0 on success */
440 ipfw_divert(struct mbuf **m0, struct ip_fw_args *args, bool tee)
443 * ipfw_chk() has already tagged the packet with the divert tag.
444 * If tee is set, copy packet and return original.
445 * If not tee, consume packet and send it to divert socket.
448 struct ip *ip = mtod(*m0, struct ip *);
451 /* Cloning needed for tee? */
453 clone = *m0; /* use the original mbuf */
456 clone = m_dup(*m0, M_NOWAIT);
457 /* If we cannot duplicate the mbuf, we sacrifice the divert
458 * chain and continue with the tee-ed packet.
465 * Divert listeners can normally handle non-fragmented packets,
466 * but we can only reass in the non-tee case.
467 * This means that listeners on a tee rule may get fragments,
468 * and have to live with that.
469 * Note that we now have the 'reass' ipfw option so if we care
470 * we can do it before a 'tee'.
472 if (tee == false) switch (ip->ip_v) {
474 if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) {
478 reass = ip_reass(clone); /* Reassemble packet. */
480 return 0; /* not an error */
481 /* if reass = NULL then it was consumed by ip_reass */
483 * IP header checksum fixup after reassembly and leave header
484 * in network byte order.
486 ip = mtod(reass, struct ip *);
487 hlen = ip->ip_hl << 2;
489 if (hlen == sizeof(struct ip))
490 ip->ip_sum = in_cksum_hdr(ip);
492 ip->ip_sum = in_cksum(reass, hlen);
497 case IPV6_VERSION >> 4:
499 struct ip6_hdr *const ip6 = mtod(clone, struct ip6_hdr *);
501 if (ip6->ip6_nxt == IPPROTO_FRAGMENT) {
504 off = sizeof(struct ip6_hdr);
505 nxt = frag6_input(&clone, &off, 0);
506 if (nxt == IPPROTO_DONE)
514 /* attach a tag to the packet with the reinject info */
515 tag = m_tag_alloc(MTAG_IPFW_RULE, 0,
516 sizeof(struct ipfw_rule_ref), M_NOWAIT);
521 *((struct ipfw_rule_ref *)(tag+1)) = args->rule;
522 m_tag_prepend(clone, tag);
524 /* Do the dirty job... */
525 ip_divert_ptr(clone, args->flags & IPFW_ARGS_IN);
530 * attach or detach hooks for a given protocol family
532 VNET_DEFINE_STATIC(pfil_hook_t, ipfw_inet_hook);
533 #define V_ipfw_inet_hook VNET(ipfw_inet_hook)
535 VNET_DEFINE_STATIC(pfil_hook_t, ipfw_inet6_hook);
536 #define V_ipfw_inet6_hook VNET(ipfw_inet6_hook)
538 VNET_DEFINE_STATIC(pfil_hook_t, ipfw_link_hook);
539 #define V_ipfw_link_hook VNET(ipfw_link_hook)
544 struct pfil_hook_args pha;
547 pha.pa_version = PFIL_VERSION;
548 pha.pa_flags = PFIL_IN | PFIL_OUT;
549 pha.pa_modname = "ipfw";
550 pha.pa_ruleset = NULL;
554 pha.pa_func = ipfw_check_packet;
555 pha.pa_type = PFIL_TYPE_IP4;
556 pha.pa_rulname = "default";
557 h = &V_ipfw_inet_hook;
561 pha.pa_func = ipfw_check_packet;
562 pha.pa_type = PFIL_TYPE_IP6;
563 pha.pa_rulname = "default6";
564 h = &V_ipfw_inet6_hook;
568 pha.pa_func = ipfw_check_frame;
569 pha.pa_type = PFIL_TYPE_ETHERNET;
570 pha.pa_rulname = "default-link";
571 pha.pa_flags |= PFIL_MEMPTR;
572 h = &V_ipfw_link_hook;
576 *h = pfil_add_hook(&pha);
585 pfil_remove_hook(V_ipfw_inet_hook);
589 pfil_remove_hook(V_ipfw_inet6_hook);
593 pfil_remove_hook(V_ipfw_link_hook);
599 ipfw_link(int pf, bool unlink)
601 struct pfil_link_args pla;
603 pla.pa_version = PFIL_VERSION;
604 pla.pa_flags = PFIL_IN | PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR;
606 pla.pa_flags |= PFIL_UNLINK;
610 pla.pa_head = V_inet_pfil_head;
611 pla.pa_hook = V_ipfw_inet_hook;
615 pla.pa_head = V_inet6_pfil_head;
616 pla.pa_hook = V_ipfw_inet6_hook;
620 pla.pa_head = V_link_pfil_head;
621 pla.pa_hook = V_ipfw_link_hook;
625 return (pfil_link(&pla));
629 ipfw_attach_hooks(void)
634 TUNABLE_INT_FETCH("net.inet.ip.fw.enable", &V_fw_enable);
635 if (V_fw_enable && (error = ipfw_link(AF_INET, false)) != 0)
636 printf("ipfw_hook() error\n");
639 TUNABLE_INT_FETCH("net.inet6.ip6.fw.enable", &V_fw6_enable);
640 if (V_fw6_enable && (error = ipfw_link(AF_INET6, false)) != 0)
641 printf("ipfw6_hook() error\n");
644 TUNABLE_INT_FETCH("net.link.ether.ipfw", &V_fwlink_enable);
645 if (V_fwlink_enable && (error = ipfw_link(AF_LINK, false)) != 0)
646 printf("ipfw_link_hook() error\n");
652 ipfw_detach_hooks(void)
655 ipfw_unhook(AF_INET);
657 ipfw_unhook(AF_INET6);
659 ipfw_unhook(AF_LINK);
663 ipfw_chg_hook(SYSCTL_HANDLER_ARGS)
669 if (arg1 == &V_fw_enable)
672 else if (arg1 == &V_fw6_enable)
675 else if (arg1 == &V_fwlink_enable)
680 newval = *(int *)arg1;
681 /* Handle sysctl change */
682 error = sysctl_handle_int(oidp, &newval, 0, req);
687 /* Formalize new value */
688 newval = (newval) ? 1 : 0;
690 if (*(int *)arg1 == newval)
693 error = ipfw_link(af, newval == 0 ? true : false);
696 *(int *)arg1 = newval;