2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #define _GNU_SOURCE /* RTLD_NEXT */
21 /* include userspace headers */
26 #include <netinet/in.h>
27 #include <sys/utsname.h>
28 #include <sys/socket.h>
30 /* prevent indirect inclusion of kernel headers */
35 /* prevent libiptc from including kernel headers */
36 #define _FWCHAINS_KERNEL_HEADERS_H
38 /* finally include libiptc and xtables */
39 #include <libiptc/libiptc.h>
40 #include <libiptc/libip6tc.h>
47 /* xtables interface */
48 #if (XTABLES_VERSION_CODE >= 10)
49 # include "xtables-10.h"
50 #elif (XTABLES_VERSION_CODE == 5)
51 # include "xtables-5.h"
53 # error "Unsupported xtables version"
58 #define XT_LOCK_NAME "/var/run/xtables.lock"
59 static int xt_lock_fd
= -1;
62 struct fw3_ipt_handle
*h
;
69 struct xtables_rule_match
*matches
;
70 struct xtables_target
*target
;
79 static struct option base_opts
[] = {
80 { .name
= "match", .has_arg
= 1, .val
= 'm' },
81 { .name
= "jump", .has_arg
= 1, .val
= 'j' },
82 { .name
= "in-interface", .has_arg
= 1, .val
= 'i' },
83 { .name
= "out-interface", .has_arg
= 1, .val
= 'o' },
84 { .name
= "source", .has_arg
= 1, .val
= 's' },
85 { .name
= "destination", .has_arg
= 1, .val
= 'd' },
90 static jmp_buf fw3_ipt_error_jmp
;
92 static __attribute__((noreturn
))
93 void fw3_ipt_error_handler(enum xtables_exittype status
,
98 fprintf(stderr
, " ! Exception: ");
101 vfprintf(stderr
, fmt
, args
);
104 longjmp(fw3_ipt_error_jmp
, status
);
107 static struct xtables_globals xtg
= {
109 .program_version
= "4",
110 .orig_opts
= base_opts
,
111 .exit_err
= fw3_ipt_error_handler
,
112 #if XTABLES_VERSION_CODE > 10
113 .compat_rev
= xtables_compatible_revision
,
117 static struct xtables_globals xtg6
= {
119 .program_version
= "6",
120 .orig_opts
= base_opts
,
121 .exit_err
= fw3_ipt_error_handler
,
122 #if XTABLES_VERSION_CODE > 10
123 .compat_rev
= xtables_compatible_revision
,
130 struct xtables_match
**matches
;
131 struct xtables_target
**targets
;
132 void (*register_match
)(struct xtables_match
*);
133 void (*register_target
)(struct xtables_target
*);
137 /* Required by certain extensions like SNAT and DNAT */
138 int kernel_version
= 0;
141 get_kernel_version(void)
143 static struct utsname uts
;
144 int x
= 0, y
= 0, z
= 0;
146 if (uname(&uts
) == -1)
147 sprintf(uts
.release
, "3.0.0");
149 sscanf(uts
.release
, "%d.%d.%d", &x
, &y
, &z
);
150 kernel_version
= 0x10000 * x
+ 0x100 * y
+ z
;
153 static void fw3_init_extensions(void)
163 struct fw3_ipt_handle
*
164 fw3_ipt_open(enum fw3_family family
, enum fw3_table table
)
167 struct fw3_ipt_handle
*h
;
169 h
= fw3_alloc(sizeof(*h
));
173 while (!fw3_lock_path(&xt_lock_fd
, XT_LOCK_NAME
)) {
174 warn("Currently busy xtables.lock - wait 1 second");
178 if (family
== FW3_FAMILY_V6
)
181 h
->family
= FW3_FAMILY_V6
;
183 h
->handle
= ip6tc_init(fw3_flag_names
[table
]);
185 xtables_set_params(&xtg6
);
186 xtables_set_nfproto(NFPROTO_IPV6
);
191 h
->family
= FW3_FAMILY_V4
;
193 h
->handle
= iptc_init(fw3_flag_names
[table
]);
195 xtables_set_params(&xtg
);
196 xtables_set_nfproto(NFPROTO_IPV4
);
202 fw3_unlock_path(&xt_lock_fd
, XT_LOCK_NAME
);
207 fw3_init_extensions();
209 if (xext
.register_match
)
210 for (i
= 0; i
< xext
.mcount
; i
++)
211 xext
.register_match(xext
.matches
[i
]);
213 if (xext
.register_target
)
214 for (i
= 0; i
< xext
.tcount
; i
++)
215 xext
.register_target(xext
.targets
[i
]);
221 debug(struct fw3_ipt_handle
*h
, const char *fmt
, ...)
225 printf("%s -t %s ", (h
->family
== FW3_FAMILY_V6
) ? "ip6tables" : "iptables",
226 fw3_flag_names
[h
->table
]);
234 fw3_ipt_set_policy(struct fw3_ipt_handle
*h
, const char *chain
,
235 enum fw3_flag policy
)
238 debug(h
, "-P %s %s\n", chain
, fw3_flag_names
[policy
]);
241 if (h
->family
== FW3_FAMILY_V6
)
242 ip6tc_set_policy(chain
, fw3_flag_names
[policy
], NULL
, h
->handle
);
245 iptc_set_policy(chain
, fw3_flag_names
[policy
], NULL
, h
->handle
);
249 fw3_ipt_flush_chain(struct fw3_ipt_handle
*h
, const char *chain
)
252 debug(h
, "-F %s\n", chain
);
255 if (h
->family
== FW3_FAMILY_V6
)
256 ip6tc_flush_entries(chain
, h
->handle
);
259 iptc_flush_entries(chain
, h
->handle
);
263 delete_rules(struct fw3_ipt_handle
*h
, const char *target
)
266 const struct ipt_entry
*e
;
272 if (h
->family
== FW3_FAMILY_V6
)
274 for (chain
= ip6tc_first_chain(h
->handle
);
276 chain
= ip6tc_next_chain(h
->handle
))
281 const struct ip6t_entry
*e6
;
282 for (num
= 0, e6
= ip6tc_first_rule(chain
, h
->handle
);
284 num
++, e6
= ip6tc_next_rule(e6
, h
->handle
))
286 t
= ip6tc_get_target(e6
, h
->handle
);
288 if (*t
&& !strcmp(t
, target
))
291 debug(h
, "-D %s %u\n", chain
, num
+ 1);
293 ip6tc_delete_num_entry(chain
, num
, h
->handle
);
304 for (chain
= iptc_first_chain(h
->handle
);
306 chain
= iptc_next_chain(h
->handle
))
311 for (num
= 0, e
= iptc_first_rule(chain
, h
->handle
);
313 num
++, e
= iptc_next_rule(e
, h
->handle
))
315 t
= iptc_get_target(e
, h
->handle
);
317 if (*t
&& !strcmp(t
, target
))
320 debug(h
, "-D %s %u\n", chain
, num
+ 1);
322 iptc_delete_num_entry(chain
, num
, h
->handle
);
333 fw3_ipt_delete_chain(struct fw3_ipt_handle
*h
, const char *chain
)
335 delete_rules(h
, chain
);
338 debug(h
, "-X %s\n", chain
);
341 if (h
->family
== FW3_FAMILY_V6
)
342 ip6tc_delete_chain(chain
, h
->handle
);
345 iptc_delete_chain(chain
, h
->handle
);
349 has_rule_tag(const void *base
, unsigned int start
, unsigned int end
)
352 const struct xt_entry_match
*em
;
354 for (i
= start
; i
< end
; i
+= em
->u
.match_size
)
358 if (strcmp(em
->u
.user
.name
, "comment"))
361 if (!memcmp(em
->data
, "!fw3", 4))
369 fw3_ipt_delete_id_rules(struct fw3_ipt_handle
*h
, const char *chain
)
372 const struct ipt_entry
*e
;
376 if (h
->family
== FW3_FAMILY_V6
)
378 if (!ip6tc_is_chain(chain
, h
->handle
))
384 const struct ip6t_entry
*e6
;
385 for (num
= 0, e6
= ip6tc_first_rule(chain
, h
->handle
);
387 num
++, e6
= ip6tc_next_rule(e6
, h
->handle
))
389 if (has_rule_tag(e6
, sizeof(*e6
), e6
->target_offset
))
392 debug(h
, "-D %s %u\n", chain
, num
+ 1);
394 ip6tc_delete_num_entry(chain
, num
, h
->handle
);
404 if (!iptc_is_chain(chain
, h
->handle
))
410 for (num
= 0, e
= iptc_first_rule(chain
, h
->handle
);
412 num
++, e
= iptc_next_rule(e
, h
->handle
))
414 if (has_rule_tag(e
, sizeof(*e
), e
->target_offset
))
417 debug(h
, "-D %s %u\n", chain
, num
+ 1);
419 iptc_delete_num_entry(chain
, num
, h
->handle
);
429 fw3_ipt_create_chain(struct fw3_ipt_handle
*h
, const char *fmt
, ...)
435 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
439 debug(h
, "-N %s\n", buf
);
441 iptc_create_chain(buf
, h
->handle
);
445 fw3_ipt_flush(struct fw3_ipt_handle
*h
)
450 if (h
->family
== FW3_FAMILY_V6
)
452 for (chain
= ip6tc_first_chain(h
->handle
);
454 chain
= ip6tc_next_chain(h
->handle
))
456 ip6tc_flush_entries(chain
, h
->handle
);
459 for (chain
= ip6tc_first_chain(h
->handle
);
461 chain
= ip6tc_next_chain(h
->handle
))
463 ip6tc_delete_chain(chain
, h
->handle
);
469 for (chain
= iptc_first_chain(h
->handle
);
471 chain
= iptc_next_chain(h
->handle
))
473 iptc_flush_entries(chain
, h
->handle
);
476 for (chain
= iptc_first_chain(h
->handle
);
478 chain
= iptc_next_chain(h
->handle
))
480 iptc_delete_chain(chain
, h
->handle
);
486 chain_is_empty(struct fw3_ipt_handle
*h
, const char *chain
)
489 if (h
->family
== FW3_FAMILY_V6
)
490 return (!ip6tc_builtin(chain
, h
->handle
) &&
491 !ip6tc_first_rule(chain
, h
->handle
));
494 return (!iptc_builtin(chain
, h
->handle
) &&
495 !iptc_first_rule(chain
, h
->handle
));
499 fw3_ipt_gc(struct fw3_ipt_handle
*h
)
505 if (h
->family
== FW3_FAMILY_V6
)
510 for (chain
= ip6tc_first_chain(h
->handle
);
512 chain
= ip6tc_next_chain(h
->handle
))
514 if (!chain_is_empty(h
, chain
))
517 fw3_ipt_delete_chain(h
, chain
);
529 for (chain
= iptc_first_chain(h
->handle
);
531 chain
= iptc_next_chain(h
->handle
))
533 warn("C=%s\n", chain
);
535 if (!chain_is_empty(h
, chain
))
538 warn("D=%s\n", chain
);
540 fw3_ipt_delete_chain(h
, chain
);
549 fw3_ipt_commit(struct fw3_ipt_handle
*h
)
554 if (h
->family
== FW3_FAMILY_V6
)
556 rv
= ip6tc_commit(h
->handle
);
558 warn("ip6tc_commit(): %s", ip6tc_strerror(errno
));
563 rv
= iptc_commit(h
->handle
);
565 warn("iptc_commit(): %s", iptc_strerror(errno
));
570 fw3_ipt_close(struct fw3_ipt_handle
*h
)
572 fw3_unlock_path(&xt_lock_fd
, XT_LOCK_NAME
);
576 struct fw3_ipt_rule
*
577 fw3_ipt_rule_new(struct fw3_ipt_handle
*h
)
579 struct fw3_ipt_rule
*r
;
581 r
= fw3_alloc(sizeof(*r
));
584 r
->argv
= fw3_alloc(sizeof(char *));
585 r
->argv
[r
->argc
++] = "fw3";
592 is_chain(struct fw3_ipt_handle
*h
, const char *name
)
595 if (h
->family
== FW3_FAMILY_V6
)
596 return ip6tc_is_chain(name
, h
->handle
);
599 return iptc_is_chain(name
, h
->handle
);
603 get_protoname(struct fw3_ipt_rule
*r
)
605 const struct xtables_pprot
*pp
;
608 for (pp
= xtables_chain_protos
; pp
->name
; pp
++)
609 if (pp
->num
== r
->protocol
)
610 return (char *)pp
->name
;
615 static struct xtables_match
*
616 find_match(struct fw3_ipt_rule
*r
, const char *name
)
618 struct xtables_match
*m
;
621 m
= xtables_find_match(name
, XTF_TRY_LOAD
, &r
->matches
);
628 init_match(struct fw3_ipt_rule
*r
, struct xtables_match
*m
, bool no_clone
)
631 struct xtables_globals
*g
;
636 s
= XT_ALIGN(sizeof(struct xt_entry_match
)) + m
->size
;
640 fw3_xt_set_match_name(m
);
642 m
->m
->u
.user
.revision
= m
->revision
;
643 m
->m
->u
.match_size
= s
;
645 /* free previous userspace data */
646 fw3_xt_free_match_udata(m
);
651 /* don't merge options if no_clone is set and this match is a clone */
652 if (no_clone
&& (m
== m
->next
))
655 /* merge option table */
656 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
657 fw3_xt_merge_match_options(g
, m
);
661 need_protomatch(struct fw3_ipt_rule
*r
, const char *pname
)
666 if (!xtables_find_match(pname
, XTF_DONT_LOAD
, NULL
))
669 return !r
->protocol_loaded
;
672 static struct xtables_match
*
673 load_protomatch(struct fw3_ipt_rule
*r
)
675 const char *pname
= get_protoname(r
);
677 if (!need_protomatch(r
, pname
))
680 return find_match(r
, pname
);
683 static struct xtables_target
*
684 find_target(struct fw3_ipt_rule
*r
, const char *name
)
686 struct xtables_target
*t
;
690 if (is_chain(r
->h
, name
))
691 t
= xtables_find_target(XT_STANDARD_TARGET
, XTF_TRY_LOAD
);
693 t
= xtables_find_target(name
, XTF_TRY_LOAD
);
700 static struct xtables_target
*
701 get_target(struct fw3_ipt_rule
*r
, const char *name
)
704 struct xtables_target
*t
;
705 struct xtables_globals
*g
;
707 t
= find_target(r
, name
);
712 s
= XT_ALIGN(sizeof(struct xt_entry_target
)) + t
->size
;
715 fw3_xt_set_target_name(t
, name
);
717 t
->t
->u
.user
.revision
= t
->revision
;
718 t
->t
->u
.target_size
= s
;
720 /* free previous userspace data */
721 fw3_xt_free_target_udata(t
);
726 /* merge option table */
727 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
728 fw3_xt_merge_target_options(g
, t
);
736 fw3_ipt_rule_proto(struct fw3_ipt_rule
*r
, struct fw3_protocol
*proto
)
740 if (!proto
|| proto
->any
)
743 pr
= proto
->protocol
;
746 if (r
->h
->family
== FW3_FAMILY_V6
)
751 r
->e6
.ipv6
.proto
= pr
;
752 r
->e6
.ipv6
.flags
|= IP6T_F_PROTO
;
755 r
->e6
.ipv6
.invflags
|= XT_INV_PROTO
;
763 r
->e
.ip
.invflags
|= XT_INV_PROTO
;
770 fw3_ipt_rule_in_out(struct fw3_ipt_rule
*r
,
771 struct fw3_device
*in
, struct fw3_device
*out
)
774 if (r
->h
->family
== FW3_FAMILY_V6
)
778 xtables_parse_interface(in
->name
, r
->e6
.ipv6
.iniface
,
779 r
->e6
.ipv6
.iniface_mask
);
782 r
->e6
.ipv6
.invflags
|= IP6T_INV_VIA_IN
;
785 if (out
&& !out
->any
)
787 xtables_parse_interface(out
->name
, r
->e6
.ipv6
.outiface
,
788 r
->e6
.ipv6
.outiface_mask
);
791 r
->e6
.ipv6
.invflags
|= IP6T_INV_VIA_OUT
;
799 xtables_parse_interface(in
->name
, r
->e
.ip
.iniface
,
800 r
->e
.ip
.iniface_mask
);
803 r
->e
.ip
.invflags
|= IPT_INV_VIA_IN
;
806 if (out
&& !out
->any
)
808 xtables_parse_interface(out
->name
, r
->e
.ip
.outiface
,
809 r
->e
.ip
.outiface_mask
);
812 r
->e
.ip
.invflags
|= IPT_INV_VIA_OUT
;
819 fw3_ipt_rule_src_dest(struct fw3_ipt_rule
*r
,
820 struct fw3_address
*src
, struct fw3_address
*dest
)
822 if ((src
&& src
->range
) || (dest
&& dest
->range
))
824 fw3_ipt_rule_addarg(r
, false, "-m", "iprange");
831 fw3_ipt_rule_addarg(r
, src
->invert
, "--src-range",
832 fw3_address_to_string(src
, false, false));
835 else if (r
->h
->family
== FW3_FAMILY_V6
)
837 r
->e6
.ipv6
.src
= src
->address
.v6
;
838 r
->e6
.ipv6
.smsk
= src
->mask
.v6
;
841 for (i
= 0; i
< 4; i
++)
842 r
->e6
.ipv6
.src
.s6_addr32
[i
] &= r
->e6
.ipv6
.smsk
.s6_addr32
[i
];
845 r
->e6
.ipv6
.invflags
|= IP6T_INV_SRCIP
;
850 r
->e
.ip
.src
= src
->address
.v4
;
851 r
->e
.ip
.smsk
= src
->mask
.v4
;
853 r
->e
.ip
.src
.s_addr
&= r
->e
.ip
.smsk
.s_addr
;
856 r
->e
.ip
.invflags
|= IPT_INV_SRCIP
;
860 if (dest
&& dest
->set
)
864 fw3_ipt_rule_addarg(r
, dest
->invert
, "--dst-range",
865 fw3_address_to_string(dest
, false, false));
868 else if (r
->h
->family
== FW3_FAMILY_V6
)
870 r
->e6
.ipv6
.dst
= dest
->address
.v6
;
871 r
->e6
.ipv6
.dmsk
= dest
->mask
.v6
;
874 for (i
= 0; i
< 4; i
++)
875 r
->e6
.ipv6
.dst
.s6_addr32
[i
] &= r
->e6
.ipv6
.dmsk
.s6_addr32
[i
];
878 r
->e6
.ipv6
.invflags
|= IP6T_INV_DSTIP
;
883 r
->e
.ip
.dst
= dest
->address
.v4
;
884 r
->e
.ip
.dmsk
= dest
->mask
.v4
;
886 r
->e
.ip
.dst
.s_addr
&= r
->e
.ip
.dmsk
.s_addr
;
889 r
->e
.ip
.invflags
|= IPT_INV_DSTIP
;
895 fw3_ipt_rule_sport_dport(struct fw3_ipt_rule
*r
,
896 struct fw3_port
*sp
, struct fw3_port
*dp
)
898 char buf
[sizeof("65535:65535\0")];
900 if ((!sp
|| !sp
->set
) && (!dp
|| !dp
->set
))
903 if (!get_protoname(r
))
908 if (sp
->port_min
== sp
->port_max
)
909 sprintf(buf
, "%u", sp
->port_min
);
911 snprintf(buf
, sizeof(buf
), "%u:%u", sp
->port_min
, sp
->port_max
);
913 fw3_ipt_rule_addarg(r
, sp
->invert
, "--sport", buf
);
918 if (dp
->port_min
== dp
->port_max
)
919 sprintf(buf
, "%u", dp
->port_min
);
921 snprintf(buf
, sizeof(buf
), "%u:%u", dp
->port_min
, dp
->port_max
);
923 fw3_ipt_rule_addarg(r
, dp
->invert
, "--dport", buf
);
928 fw3_ipt_rule_device(struct fw3_ipt_rule
*r
, const char *device
, bool out
)
931 struct fw3_device dev
= { .any
= false };
932 strncpy(dev
.name
, device
, sizeof(dev
.name
) - 1);
933 fw3_ipt_rule_in_out(r
, (out
) ? NULL
: &dev
, (out
) ? &dev
: NULL
);
938 fw3_ipt_rule_mac(struct fw3_ipt_rule
*r
, struct fw3_mac
*mac
)
940 char buf
[sizeof("ff:ff:ff:ff:ff:ff\0")];
941 uint8_t *addr
= mac
->mac
.ether_addr_octet
;
946 sprintf(buf
, "%02x:%02x:%02x:%02x:%02x:%02x",
947 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
949 fw3_ipt_rule_addarg(r
, false, "-m", "mac");
950 fw3_ipt_rule_addarg(r
, mac
->invert
, "--mac-source", buf
);
954 fw3_ipt_rule_icmptype(struct fw3_ipt_rule
*r
, struct fw3_icmptype
*icmp
)
956 char buf
[sizeof("255/255\0")];
962 if (r
->h
->family
== FW3_FAMILY_V6
)
964 if (icmp
->code6_min
== 0 && icmp
->code6_max
== 0xFF)
965 sprintf(buf
, "%u", icmp
->type6
);
967 snprintf(buf
, sizeof(buf
), "%u/%u", icmp
->type6
, icmp
->code6_min
);
969 fw3_ipt_rule_addarg(r
, icmp
->invert
, "--icmpv6-type", buf
);
974 if (icmp
->code_min
== 0 && icmp
->code_max
== 0xFF)
975 sprintf(buf
, "%u", icmp
->type
);
977 snprintf(buf
, sizeof(buf
), "%u/%u", icmp
->type
, icmp
->code_min
);
979 fw3_ipt_rule_addarg(r
, icmp
->invert
, "--icmp-type", buf
);
984 fw3_ipt_rule_limit(struct fw3_ipt_rule
*r
, struct fw3_limit
*limit
)
986 char buf
[sizeof("-4294967296/second\0")];
988 if (!limit
|| limit
->rate
<= 0)
991 fw3_ipt_rule_addarg(r
, false, "-m", "limit");
993 sprintf(buf
, "%u/%s", limit
->rate
, fw3_limit_units
[limit
->unit
]);
994 fw3_ipt_rule_addarg(r
, limit
->invert
, "--limit", buf
);
996 if (limit
->burst
> 0)
998 sprintf(buf
, "%u", limit
->burst
);
999 fw3_ipt_rule_addarg(r
, limit
->invert
, "--limit-burst", buf
);
1004 fw3_ipt_rule_ipset(struct fw3_ipt_rule
*r
, struct fw3_setmatch
*match
)
1006 char buf
[sizeof("dst,dst,dst\0")];
1010 struct fw3_ipset
*set
;
1011 struct fw3_ipset_datatype
*type
;
1013 if (!match
|| !match
->set
|| !match
->ptr
)
1017 list_for_each_entry(type
, &set
->datatypes
, list
)
1025 p
+= sprintf(p
, "%s", match
->dir
[i
] ? match
->dir
[i
] : type
->dir
);
1029 fw3_ipt_rule_addarg(r
, false, "-m", "set");
1031 fw3_ipt_rule_addarg(r
, match
->invert
, "--match-set",
1032 set
->external
? set
->external
: set
->name
);
1034 fw3_ipt_rule_addarg(r
, false, buf
, NULL
);
1038 fw3_ipt_rule_helper(struct fw3_ipt_rule
*r
, struct fw3_cthelpermatch
*match
)
1040 if (!match
|| !match
->set
|| !match
->ptr
)
1043 fw3_ipt_rule_addarg(r
, false, "-m", "helper");
1044 fw3_ipt_rule_addarg(r
, match
->invert
, "--helper", match
->ptr
->name
);
1048 fw3_ipt_rule_time(struct fw3_ipt_rule
*r
, struct fw3_time
*time
)
1051 struct tm empty
= { 0 };
1053 char buf
[84]; /* sizeof("1,2,3,...,30,31\0") */
1056 bool d1
= memcmp(&time
->datestart
, &empty
, sizeof(empty
));
1057 bool d2
= memcmp(&time
->datestop
, &empty
, sizeof(empty
));
1059 if (!d1
&& !d2
&& !time
->timestart
&& !time
->timestop
&&
1060 !(time
->monthdays
& 0xFFFFFFFE) && !(time
->weekdays
& 0xFE))
1065 fw3_ipt_rule_addarg(r
, false, "-m", "time");
1068 fw3_ipt_rule_addarg(r
, false, "--kerneltz", NULL
);
1072 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestart
);
1073 fw3_ipt_rule_addarg(r
, false, "--datestart", buf
);
1078 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestop
);
1079 fw3_ipt_rule_addarg(r
, false, "--datestop", buf
);
1082 if (time
->timestart
)
1084 sprintf(buf
, "%02d:%02d:%02d",
1085 time
->timestart
/ 3600,
1086 time
->timestart
% 3600 / 60,
1087 time
->timestart
% 60);
1089 fw3_ipt_rule_addarg(r
, false, "--timestart", buf
);
1094 sprintf(buf
, "%02d:%02d:%02d",
1095 time
->timestop
/ 3600,
1096 time
->timestop
% 3600 / 60,
1097 time
->timestop
% 60);
1099 fw3_ipt_rule_addarg(r
, false, "--timestop", buf
);
1102 if (time
->monthdays
& 0xFFFFFFFE)
1104 for (i
= 1, p
= buf
; i
< 32; i
++)
1106 if (fw3_hasbit(time
->monthdays
, i
))
1111 p
+= sprintf(p
, "%u", i
);
1115 fw3_ipt_rule_addarg(r
, fw3_hasbit(time
->monthdays
, 0), "--monthdays", buf
);
1118 if (time
->weekdays
& 0xFE)
1120 for (i
= 1, p
= buf
; i
< 8; i
++)
1122 if (fw3_hasbit(time
->weekdays
, i
))
1127 p
+= sprintf(p
, "%u", i
);
1131 fw3_ipt_rule_addarg(r
, fw3_hasbit(time
->weekdays
, 0), "--weekdays", buf
);
1136 fw3_ipt_rule_mark(struct fw3_ipt_rule
*r
, struct fw3_mark
*mark
)
1138 char buf
[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
1140 if (!mark
|| !mark
->set
)
1143 if (mark
->mask
< 0xFFFFFFFF)
1144 sprintf(buf
, "0x%x/0x%x", mark
->mark
, mark
->mask
);
1146 sprintf(buf
, "0x%x", mark
->mark
);
1148 fw3_ipt_rule_addarg(r
, false, "-m", "mark");
1149 fw3_ipt_rule_addarg(r
, mark
->invert
, "--mark", buf
);
1153 fw3_ipt_rule_dscp(struct fw3_ipt_rule
*r
, struct fw3_dscp
*dscp
)
1155 char buf
[sizeof("0xFF\0")];
1157 if (!dscp
|| !dscp
->set
)
1160 sprintf(buf
, "0x%x", dscp
->dscp
);
1162 fw3_ipt_rule_addarg(r
, false, "-m", "dscp");
1163 fw3_ipt_rule_addarg(r
, dscp
->invert
, "--dscp", buf
);
1167 fw3_ipt_rule_comment(struct fw3_ipt_rule
*r
, const char *fmt
, ...)
1176 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
1179 fw3_ipt_rule_addarg(r
, false, "-m", "comment");
1180 fw3_ipt_rule_addarg(r
, false, "--comment", buf
);
1184 fw3_ipt_rule_extra(struct fw3_ipt_rule
*r
, const char *extra
)
1188 if (!extra
|| !*extra
)
1191 s
= fw3_strdup(extra
);
1193 for (p
= strtok(s
, " \t"); p
; p
= strtok(NULL
, " \t"))
1195 tmp
= realloc(r
->argv
, (r
->argc
+ 1) * sizeof(*r
->argv
));
1201 r
->argv
[r
->argc
++] = fw3_strdup(p
);
1207 #ifndef DISABLE_IPV6
1209 rule_print6(struct ip6t_entry
*e
)
1211 char buf1
[INET6_ADDRSTRLEN
], buf2
[INET6_ADDRSTRLEN
];
1214 if (e
->ipv6
.flags
& IP6T_F_PROTO
)
1216 if (e
->ipv6
.invflags
& XT_INV_PROTO
)
1219 pname
= get_protoname(container_of(e
, struct fw3_ipt_rule
, e6
));
1222 printf(" -p %s", pname
);
1224 printf(" -p %u", e
->ipv6
.proto
);
1227 if (e
->ipv6
.iniface
[0])
1229 if (e
->ipv6
.invflags
& IP6T_INV_VIA_IN
)
1232 printf(" -i %s", e
->ipv6
.iniface
);
1235 if (e
->ipv6
.outiface
[0])
1237 if (e
->ipv6
.invflags
& IP6T_INV_VIA_OUT
)
1240 printf(" -o %s", e
->ipv6
.outiface
);
1243 if (memcmp(&e
->ipv6
.src
, &in6addr_any
, sizeof(struct in6_addr
)))
1245 if (e
->ipv6
.invflags
& IP6T_INV_SRCIP
)
1249 inet_ntop(AF_INET6
, &e
->ipv6
.src
, buf1
, sizeof(buf1
)),
1250 inet_ntop(AF_INET6
, &e
->ipv6
.smsk
, buf2
, sizeof(buf2
)));
1253 if (memcmp(&e
->ipv6
.dst
, &in6addr_any
, sizeof(struct in6_addr
)))
1255 if (e
->ipv6
.invflags
& IP6T_INV_DSTIP
)
1259 inet_ntop(AF_INET6
, &e
->ipv6
.dst
, buf1
, sizeof(buf1
)),
1260 inet_ntop(AF_INET6
, &e
->ipv6
.dmsk
, buf2
, sizeof(buf2
)));
1266 rule_print4(struct ipt_entry
*e
)
1268 struct in_addr in_zero
= { 0 };
1269 char buf1
[sizeof("255.255.255.255\0")], buf2
[sizeof("255.255.255.255\0")];
1274 if (e
->ip
.invflags
& XT_INV_PROTO
)
1277 pname
= get_protoname(container_of(e
, struct fw3_ipt_rule
, e
));
1280 printf(" -p %s", pname
);
1282 printf(" -p %u", e
->ip
.proto
);
1285 if (e
->ip
.iniface
[0])
1287 if (e
->ip
.invflags
& IPT_INV_VIA_IN
)
1290 printf(" -i %s", e
->ip
.iniface
);
1293 if (e
->ip
.outiface
[0])
1295 if (e
->ip
.invflags
& IPT_INV_VIA_OUT
)
1298 printf(" -o %s", e
->ip
.outiface
);
1301 if (memcmp(&e
->ip
.src
, &in_zero
, sizeof(struct in_addr
)))
1303 if (e
->ip
.invflags
& IPT_INV_SRCIP
)
1307 inet_ntop(AF_INET
, &e
->ip
.src
, buf1
, sizeof(buf1
)),
1308 inet_ntop(AF_INET
, &e
->ip
.smsk
, buf2
, sizeof(buf2
)));
1311 if (memcmp(&e
->ip
.dst
, &in_zero
, sizeof(struct in_addr
)))
1313 if (e
->ip
.invflags
& IPT_INV_DSTIP
)
1317 inet_ntop(AF_INET
, &e
->ip
.dst
, buf1
, sizeof(buf1
)),
1318 inet_ntop(AF_INET
, &e
->ip
.dmsk
, buf2
, sizeof(buf2
)));
1323 rule_print(struct fw3_ipt_rule
*r
, const char *prefix
, const char *chain
)
1325 debug(r
->h
, "%s %s", prefix
, chain
);
1327 #ifndef DISABLE_IPV6
1328 if (r
->h
->family
== FW3_FAMILY_V6
)
1329 rule_print6(&r
->e6
);
1334 fw3_xt_print_matches(&r
->e
.ip
, r
->matches
);
1335 fw3_xt_print_target(&r
->e
.ip
, r
->target
);
1341 parse_option(struct fw3_ipt_rule
*r
, int optc
, bool inv
)
1343 struct xtables_rule_match
*m
;
1344 struct xtables_match
*em
;
1346 /* is a target option */
1347 if (r
->target
&& fw3_xt_has_target_parse(r
->target
) &&
1348 optc
>= r
->target
->option_offset
&&
1349 optc
< (r
->target
->option_offset
+ 256))
1351 xtables_option_tpcall(optc
, r
->argv
, inv
, r
->target
, &r
->e
);
1355 /* try to dispatch argument to one of the match parsers */
1356 for (m
= r
->matches
; m
; m
= m
->next
)
1360 if (m
->completed
|| !fw3_xt_has_match_parse(em
))
1363 if (optc
< em
->option_offset
||
1364 optc
>= (em
->option_offset
+ 256))
1367 xtables_option_mpcall(optc
, r
->argv
, inv
, em
, &r
->e
);
1371 /* unhandled option, might belong to a protocol match */
1372 if ((em
= load_protomatch(r
)) != NULL
)
1374 init_match(r
, em
, false);
1376 r
->protocol_loaded
= true;
1383 warn("parse_option(): option '%s' needs argument", r
->argv
[optind
-1]);
1386 warn("parse_option(): unknown option '%s'", r
->argv
[optind
-1]);
1392 fw3_ipt_rule_addarg(struct fw3_ipt_rule
*r
, bool inv
,
1393 const char *k
, const char *v
)
1401 n
= inv
+ !!k
+ !!v
;
1402 tmp
= realloc(r
->argv
, (r
->argc
+ n
) * sizeof(*tmp
));
1410 r
->argv
[r
->argc
++] = fw3_strdup("!");
1412 r
->argv
[r
->argc
++] = fw3_strdup(k
);
1415 r
->argv
[r
->argc
++] = fw3_strdup(v
);
1418 static unsigned char *
1419 rule_mask(struct fw3_ipt_rule
*r
)
1422 unsigned char *p
, *mask
= NULL
;
1423 struct xtables_rule_match
*m
;
1425 #define SZ(x) XT_ALIGN(sizeof(struct x))
1427 #ifndef DISABLE_IPV6
1428 if (r
->h
->family
== FW3_FAMILY_V6
)
1432 for (m
= r
->matches
; m
; m
= m
->next
)
1433 s
+= SZ(ip6t_entry_match
) + m
->match
->size
;
1435 s
+= SZ(ip6t_entry_target
);
1437 s
+= r
->target
->size
;
1439 mask
= fw3_alloc(s
);
1440 memset(mask
, 0xFF, SZ(ip6t_entry
));
1441 p
= mask
+ SZ(ip6t_entry
);
1443 for (m
= r
->matches
; m
; m
= m
->next
)
1445 memset(p
, 0xFF, SZ(ip6t_entry_match
) + m
->match
->userspacesize
);
1446 p
+= SZ(ip6t_entry_match
) + m
->match
->size
;
1449 memset(p
, 0xFF, SZ(ip6t_entry_target
) + (r
->target
? r
->target
->userspacesize
: 0));
1456 for (m
= r
->matches
; m
; m
= m
->next
)
1457 s
+= SZ(ipt_entry_match
) + m
->match
->size
;
1459 s
+= SZ(ipt_entry_target
);
1461 s
+= r
->target
->size
;
1463 mask
= fw3_alloc(s
);
1464 memset(mask
, 0xFF, SZ(ipt_entry
));
1465 p
= mask
+ SZ(ipt_entry
);
1467 for (m
= r
->matches
; m
; m
= m
->next
)
1469 memset(p
, 0xFF, SZ(ipt_entry_match
) + m
->match
->userspacesize
);
1470 p
+= SZ(ipt_entry_match
) + m
->match
->size
;
1473 memset(p
, 0xFF, SZ(ipt_entry_target
) + (r
->target
? r
->target
->userspacesize
: 0));
1480 rule_build(struct fw3_ipt_rule
*r
)
1482 size_t s
, target_size
= (r
->target
) ? r
->target
->t
->u
.target_size
: 0;
1483 struct xtables_rule_match
*m
;
1485 #ifndef DISABLE_IPV6
1486 if (r
->h
->family
== FW3_FAMILY_V6
)
1488 struct ip6t_entry
*e6
;
1490 s
= XT_ALIGN(sizeof(struct ip6t_entry
));
1492 for (m
= r
->matches
; m
; m
= m
->next
)
1493 s
+= m
->match
->m
->u
.match_size
;
1495 e6
= fw3_alloc(s
+ target_size
);
1497 memcpy(e6
, &r
->e6
, sizeof(struct ip6t_entry
));
1499 e6
->target_offset
= s
;
1500 e6
->next_offset
= s
+ target_size
;
1504 for (m
= r
->matches
; m
; m
= m
->next
)
1506 memcpy(e6
->elems
+ s
, m
->match
->m
, m
->match
->m
->u
.match_size
);
1507 s
+= m
->match
->m
->u
.match_size
;
1511 memcpy(e6
->elems
+ s
, r
->target
->t
, target_size
);
1518 struct ipt_entry
*e
;
1520 s
= XT_ALIGN(sizeof(struct ipt_entry
));
1522 for (m
= r
->matches
; m
; m
= m
->next
)
1523 s
+= m
->match
->m
->u
.match_size
;
1525 e
= fw3_alloc(s
+ target_size
);
1527 memcpy(e
, &r
->e
, sizeof(struct ipt_entry
));
1529 e
->target_offset
= s
;
1530 e
->next_offset
= s
+ target_size
;
1534 for (m
= r
->matches
; m
; m
= m
->next
)
1536 memcpy(e
->elems
+ s
, m
->match
->m
, m
->match
->m
->u
.match_size
);
1537 s
+= m
->match
->m
->u
.match_size
;
1541 memcpy(e
->elems
+ s
, r
->target
->t
, target_size
);
1548 set_rule_tag(struct fw3_ipt_rule
*r
)
1552 const char *tag
= "!fw3";
1554 for (i
= 0; i
< r
->argc
; i
++)
1555 if (!strcmp(r
->argv
[i
], "--comment") && (i
+ 1) < r
->argc
)
1556 if (asprintf(&p
, "%s: %s", tag
, r
->argv
[i
+ 1]) > 0)
1558 free(r
->argv
[i
+ 1]);
1563 tmp
= realloc(r
->argv
, (r
->argc
+ 4) * sizeof(*r
->argv
));
1568 r
->argv
[r
->argc
++] = fw3_strdup("-m");
1569 r
->argv
[r
->argc
++] = fw3_strdup("comment");
1570 r
->argv
[r
->argc
++] = fw3_strdup("--comment");
1571 r
->argv
[r
->argc
++] = fw3_strdup(tag
);
1576 __fw3_ipt_rule_append(struct fw3_ipt_rule
*r
, bool repl
, const char *fmt
, ...)
1579 unsigned char *mask
;
1581 struct xtables_rule_match
*m
;
1582 struct xtables_match
*em
;
1583 struct xtables_target
*et
;
1584 struct xtables_globals
*g
;
1586 struct fw3_device dev
;
1587 struct fw3_address addr
;
1589 enum xtables_exittype status
;
1597 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
1600 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
1601 g
->opts
= g
->orig_opts
;
1606 status
= setjmp(fw3_ipt_error_jmp
);
1610 info(" ! Skipping due to previous exception (code %u)", status
);
1616 while ((optc
= getopt_long(r
->argc
, r
->argv
, "-:m:j:i:o:s:d:", g
->opts
,
1622 em
= find_match(r
, optarg
);
1626 warn("fw3_ipt_rule_append(): Can't find match '%s'", optarg
);
1630 init_match(r
, em
, true);
1634 et
= get_target(r
, optarg
);
1638 warn("fw3_ipt_rule_append(): Can't find target '%s'", optarg
);
1646 if (!fw3_parse_device(&dev
, optarg
, false) ||
1647 dev
.any
|| dev
.invert
|| *dev
.network
)
1649 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg
);
1654 fw3_ipt_rule_in_out(r
, (optc
== 'i') ? &dev
: NULL
,
1655 (optc
== 'o') ? &dev
: NULL
);
1660 if (!fw3_parse_address(&addr
, optarg
, false) ||
1661 addr
.range
|| addr
.invert
)
1663 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg
);
1668 fw3_ipt_rule_src_dest(r
, (optc
== 's') ? &addr
: NULL
,
1669 (optc
== 'd') ? &addr
: NULL
);
1673 if ((optarg
[0] == '!') && (optarg
[1] == '\0'))
1680 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg
);
1684 if (parse_option(r
, optc
, inv
))
1692 for (m
= r
->matches
; m
; m
= m
->next
)
1693 xtables_option_mfcall(m
->match
);
1696 xtables_option_tfcall(r
->target
);
1698 rule
= rule_build(r
);
1700 #ifndef DISABLE_IPV6
1701 if (r
->h
->family
== FW3_FAMILY_V6
)
1705 mask
= rule_mask(r
);
1707 while (ip6tc_delete_entry(buf
, rule
, mask
, r
->h
->handle
))
1709 rule_print(r
, "-D", buf
);
1715 rule_print(r
, "-A", buf
);
1717 if (!ip6tc_append_entry(buf
, rule
, r
->h
->handle
))
1718 warn("ip6tc_append_entry(): %s", ip6tc_strerror(errno
));
1725 mask
= rule_mask(r
);
1727 while (iptc_delete_entry(buf
, rule
, mask
, r
->h
->handle
))
1729 rule_print(r
, "-D", buf
);
1735 rule_print(r
, "-A", buf
);
1737 if (!iptc_append_entry(buf
, rule
, r
->h
->handle
))
1738 warn("iptc_append_entry(): %s\n", iptc_strerror(errno
));
1744 for (i
= 1; i
< r
->argc
; i
++)
1749 xtables_rule_matches_free(&r
->matches
);
1756 /* reset all targets and matches */
1757 for (em
= xtables_matches
; em
; em
= em
->next
)
1760 for (et
= xtables_targets
; et
; et
= et
->next
)
1766 xtables_free_opts(1);
1769 struct fw3_ipt_rule
*
1770 fw3_ipt_rule_create(struct fw3_ipt_handle
*handle
, struct fw3_protocol
*proto
,
1771 struct fw3_device
*in
, struct fw3_device
*out
,
1772 struct fw3_address
*src
, struct fw3_address
*dest
)
1774 struct fw3_ipt_rule
*r
;
1776 r
= fw3_ipt_rule_new(handle
);
1778 fw3_ipt_rule_proto(r
, proto
);
1779 fw3_ipt_rule_in_out(r
, in
, out
);
1780 fw3_ipt_rule_src_dest(r
, src
, dest
);
1786 xtables_register_match(struct xtables_match
*me
)
1789 static struct xtables_match
**tmp
;
1791 if (!xext
.register_match
)
1792 xext
.register_match
= dlsym(RTLD_NEXT
, "xtables_register_match");
1794 if (!xext
.register_match
)
1797 xext
.register_match(me
);
1801 for (i
= 0; i
< xext
.mcount
; i
++)
1802 if (xext
.matches
[i
] == me
)
1805 tmp
= realloc(xext
.matches
, sizeof(me
) * (xext
.mcount
+ 1));
1811 xext
.matches
[xext
.mcount
++] = me
;
1816 xtables_register_target(struct xtables_target
*me
)
1819 static struct xtables_target
**tmp
;
1821 if (!xext
.register_target
)
1822 xext
.register_target
= dlsym(RTLD_NEXT
, "xtables_register_target");
1824 if (!xext
.register_target
)
1827 xext
.register_target(me
);
1831 for (i
= 0; i
< xext
.tcount
; i
++)
1832 if (xext
.targets
[i
] == me
)
1835 tmp
= realloc(xext
.targets
, sizeof(me
) * (xext
.tcount
+ 1));
1841 xext
.targets
[xext
.tcount
++] = me
;