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
= 3, y
= 0, z
= 0;
146 if (uname(&uts
) != -1)
147 sscanf(uts
.release
, "%d.%d.%d", &x
, &y
, &z
);
149 kernel_version
= 0x10000 * x
+ 0x100 * y
+ z
;
152 static void fw3_init_extensions(void)
162 struct fw3_ipt_handle
*
163 fw3_ipt_open(enum fw3_family family
, enum fw3_table table
)
166 struct fw3_ipt_handle
*h
;
168 h
= fw3_alloc(sizeof(*h
));
172 while (!fw3_lock_path(&xt_lock_fd
, XT_LOCK_NAME
)) {
173 warn("Currently busy xtables.lock - wait 1 second");
177 if (family
== FW3_FAMILY_V6
)
180 h
->family
= FW3_FAMILY_V6
;
182 h
->handle
= ip6tc_init(fw3_flag_names
[table
]);
184 xtables_set_params(&xtg6
);
185 xtables_set_nfproto(NFPROTO_IPV6
);
190 h
->family
= FW3_FAMILY_V4
;
192 h
->handle
= iptc_init(fw3_flag_names
[table
]);
194 xtables_set_params(&xtg
);
195 xtables_set_nfproto(NFPROTO_IPV4
);
201 fw3_unlock_path(&xt_lock_fd
, XT_LOCK_NAME
);
206 fw3_init_extensions();
208 if (xext
.register_match
)
209 for (i
= 0; i
< xext
.mcount
; i
++)
210 xext
.register_match(xext
.matches
[i
]);
212 if (xext
.register_target
)
213 for (i
= 0; i
< xext
.tcount
; i
++)
214 xext
.register_target(xext
.targets
[i
]);
220 debug(struct fw3_ipt_handle
*h
, const char *fmt
, ...)
224 printf("%s -t %s ", (h
->family
== FW3_FAMILY_V6
) ? "ip6tables" : "iptables",
225 fw3_flag_names
[h
->table
]);
233 fw3_ipt_set_policy(struct fw3_ipt_handle
*h
, const char *chain
,
234 enum fw3_flag policy
)
237 debug(h
, "-P %s %s\n", chain
, fw3_flag_names
[policy
]);
240 if (h
->family
== FW3_FAMILY_V6
)
241 ip6tc_set_policy(chain
, fw3_flag_names
[policy
], NULL
, h
->handle
);
244 iptc_set_policy(chain
, fw3_flag_names
[policy
], NULL
, h
->handle
);
248 fw3_ipt_flush_chain(struct fw3_ipt_handle
*h
, const char *chain
)
251 debug(h
, "-F %s\n", chain
);
254 if (h
->family
== FW3_FAMILY_V6
)
255 ip6tc_flush_entries(chain
, h
->handle
);
258 iptc_flush_entries(chain
, h
->handle
);
262 delete_rules(struct fw3_ipt_handle
*h
, const char *target
)
265 const struct ipt_entry
*e
;
271 if (h
->family
== FW3_FAMILY_V6
)
273 for (chain
= ip6tc_first_chain(h
->handle
);
275 chain
= ip6tc_next_chain(h
->handle
))
280 const struct ip6t_entry
*e6
;
281 for (num
= 0, e6
= ip6tc_first_rule(chain
, h
->handle
);
283 num
++, e6
= ip6tc_next_rule(e6
, h
->handle
))
285 t
= ip6tc_get_target(e6
, h
->handle
);
287 if (*t
&& !strcmp(t
, target
))
290 debug(h
, "-D %s %u\n", chain
, num
+ 1);
292 ip6tc_delete_num_entry(chain
, num
, h
->handle
);
303 for (chain
= iptc_first_chain(h
->handle
);
305 chain
= iptc_next_chain(h
->handle
))
310 for (num
= 0, e
= iptc_first_rule(chain
, h
->handle
);
312 num
++, e
= iptc_next_rule(e
, h
->handle
))
314 t
= iptc_get_target(e
, h
->handle
);
316 if (*t
&& !strcmp(t
, target
))
319 debug(h
, "-D %s %u\n", chain
, num
+ 1);
321 iptc_delete_num_entry(chain
, num
, h
->handle
);
332 is_referenced(struct fw3_ipt_handle
*h
, const char *target
)
334 const struct ipt_entry
*e
;
339 if (h
->family
== FW3_FAMILY_V6
)
341 for (chain
= ip6tc_first_chain(h
->handle
);
343 chain
= ip6tc_next_chain(h
->handle
))
345 const struct ip6t_entry
*e6
;
346 for (e6
= ip6tc_first_rule(chain
, h
->handle
);
348 e6
= ip6tc_next_rule(e6
, h
->handle
))
350 t
= ip6tc_get_target(e6
, h
->handle
);
352 if (*t
&& !strcmp(t
, target
))
360 for (chain
= iptc_first_chain(h
->handle
);
362 chain
= iptc_next_chain(h
->handle
))
364 for (e
= iptc_first_rule(chain
, h
->handle
);
366 e
= iptc_next_rule(e
, h
->handle
))
368 t
= iptc_get_target(e
, h
->handle
);
370 if (*t
&& !strcmp(t
, target
))
380 fw3_ipt_delete_chain(struct fw3_ipt_handle
*h
, bool if_unused
,
383 if (if_unused
&& is_referenced(h
, chain
))
386 delete_rules(h
, chain
);
389 debug(h
, "-X %s\n", chain
);
392 if (h
->family
== FW3_FAMILY_V6
)
393 ip6tc_delete_chain(chain
, h
->handle
);
396 iptc_delete_chain(chain
, h
->handle
);
400 has_rule_tag(const void *base
, unsigned int start
, unsigned int end
)
403 const struct xt_entry_match
*em
;
405 for (i
= start
; i
< end
; i
+= em
->u
.match_size
)
409 if (strcmp(em
->u
.user
.name
, "comment"))
412 if (!memcmp(em
->data
, "!fw3", 4))
420 fw3_ipt_delete_id_rules(struct fw3_ipt_handle
*h
, const char *chain
)
423 const struct ipt_entry
*e
;
427 if (h
->family
== FW3_FAMILY_V6
)
429 if (!ip6tc_is_chain(chain
, h
->handle
))
435 const struct ip6t_entry
*e6
;
436 for (num
= 0, e6
= ip6tc_first_rule(chain
, h
->handle
);
438 num
++, e6
= ip6tc_next_rule(e6
, h
->handle
))
440 if (has_rule_tag(e6
, sizeof(*e6
), e6
->target_offset
))
443 debug(h
, "-D %s %u\n", chain
, num
+ 1);
445 ip6tc_delete_num_entry(chain
, num
, h
->handle
);
455 if (!iptc_is_chain(chain
, h
->handle
))
461 for (num
= 0, e
= iptc_first_rule(chain
, h
->handle
);
463 num
++, e
= iptc_next_rule(e
, h
->handle
))
465 if (has_rule_tag(e
, sizeof(*e
), e
->target_offset
))
468 debug(h
, "-D %s %u\n", chain
, num
+ 1);
470 iptc_delete_num_entry(chain
, num
, h
->handle
);
481 is_chain(struct fw3_ipt_handle
*h
, const char *name
)
484 if (h
->family
== FW3_FAMILY_V6
)
485 return ip6tc_is_chain(name
, h
->handle
);
488 return iptc_is_chain(name
, h
->handle
);
492 fw3_ipt_create_chain(struct fw3_ipt_handle
*h
, bool ignore_existing
,
495 if (ignore_existing
&& is_chain(h
, chain
))
499 debug(h
, "-N %s\n", chain
);
501 iptc_create_chain(chain
, h
->handle
);
505 fw3_ipt_flush(struct fw3_ipt_handle
*h
)
510 if (h
->family
== FW3_FAMILY_V6
)
512 for (chain
= ip6tc_first_chain(h
->handle
);
514 chain
= ip6tc_next_chain(h
->handle
))
516 ip6tc_flush_entries(chain
, h
->handle
);
519 for (chain
= ip6tc_first_chain(h
->handle
);
521 chain
= ip6tc_next_chain(h
->handle
))
523 ip6tc_delete_chain(chain
, h
->handle
);
529 for (chain
= iptc_first_chain(h
->handle
);
531 chain
= iptc_next_chain(h
->handle
))
533 iptc_flush_entries(chain
, h
->handle
);
536 for (chain
= iptc_first_chain(h
->handle
);
538 chain
= iptc_next_chain(h
->handle
))
540 iptc_delete_chain(chain
, h
->handle
);
546 chain_is_empty(struct fw3_ipt_handle
*h
, const char *chain
)
549 if (h
->family
== FW3_FAMILY_V6
)
550 return (!ip6tc_builtin(chain
, h
->handle
) &&
551 !ip6tc_first_rule(chain
, h
->handle
));
554 return (!iptc_builtin(chain
, h
->handle
) &&
555 !iptc_first_rule(chain
, h
->handle
));
559 fw3_ipt_gc(struct fw3_ipt_handle
*h
)
565 if (h
->family
== FW3_FAMILY_V6
)
570 for (chain
= ip6tc_first_chain(h
->handle
);
572 chain
= ip6tc_next_chain(h
->handle
))
574 if (!chain_is_empty(h
, chain
))
577 fw3_ipt_delete_chain(h
, false, chain
);
589 for (chain
= iptc_first_chain(h
->handle
);
591 chain
= iptc_next_chain(h
->handle
))
593 warn("C=%s\n", chain
);
595 if (!chain_is_empty(h
, chain
))
598 warn("D=%s\n", chain
);
600 fw3_ipt_delete_chain(h
, false, chain
);
609 fw3_ipt_commit(struct fw3_ipt_handle
*h
)
614 if (h
->family
== FW3_FAMILY_V6
)
616 rv
= ip6tc_commit(h
->handle
);
618 warn("ip6tc_commit(): %s", ip6tc_strerror(errno
));
623 rv
= iptc_commit(h
->handle
);
625 warn("iptc_commit(): %s", iptc_strerror(errno
));
630 fw3_ipt_close(struct fw3_ipt_handle
*h
)
632 fw3_unlock_path(&xt_lock_fd
, XT_LOCK_NAME
);
636 struct fw3_ipt_rule
*
637 fw3_ipt_rule_new(struct fw3_ipt_handle
*h
)
639 struct fw3_ipt_rule
*r
;
641 r
= fw3_alloc(sizeof(*r
));
644 r
->argv
= fw3_alloc(sizeof(char *));
645 r
->argv
[r
->argc
++] = "fw3";
652 get_protoname(struct fw3_ipt_rule
*r
)
654 const struct xtables_pprot
*pp
;
657 for (pp
= xtables_chain_protos
; pp
->name
; pp
++)
658 if (pp
->num
== r
->protocol
)
659 return (char *)pp
->name
;
664 static struct xtables_match
*
665 find_match(struct fw3_ipt_rule
*r
, const char *name
)
667 struct xtables_match
*m
;
670 m
= xtables_find_match(name
, XTF_TRY_LOAD
, &r
->matches
);
677 init_match(struct fw3_ipt_rule
*r
, struct xtables_match
*m
, bool no_clone
)
680 struct xtables_globals
*g
;
685 s
= XT_ALIGN(sizeof(struct xt_entry_match
)) + m
->size
;
689 fw3_xt_set_match_name(m
);
691 m
->m
->u
.user
.revision
= m
->revision
;
692 m
->m
->u
.match_size
= s
;
694 /* free previous userspace data */
695 fw3_xt_free_match_udata(m
);
700 /* don't merge options if no_clone is set and this match is a clone */
701 if (no_clone
&& (m
== m
->next
))
704 /* merge option table */
705 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
706 fw3_xt_merge_match_options(g
, m
);
710 need_protomatch(struct fw3_ipt_rule
*r
, const char *pname
)
712 struct xtables_match
*match
;
717 match
= xtables_find_match(pname
, XTF_DONT_LOAD
, NULL
);
721 /* Free any kind of clone from xtables_find_match */
722 if (match
== match
->next
)
724 return !r
->protocol_loaded
;
727 static struct xtables_match
*
728 load_protomatch(struct fw3_ipt_rule
*r
)
730 const char *pname
= get_protoname(r
);
732 if (!need_protomatch(r
, pname
))
735 return find_match(r
, pname
);
738 static struct xtables_target
*
739 find_target(struct fw3_ipt_rule
*r
, const char *name
)
741 struct xtables_target
*t
;
745 if (is_chain(r
->h
, name
))
746 t
= xtables_find_target(XT_STANDARD_TARGET
, XTF_TRY_LOAD
);
748 t
= xtables_find_target(name
, XTF_TRY_LOAD
);
755 static struct xtables_target
*
756 get_target(struct fw3_ipt_rule
*r
, const char *name
)
759 struct xtables_target
*t
;
760 struct xtables_globals
*g
;
762 t
= find_target(r
, name
);
767 s
= XT_ALIGN(sizeof(struct xt_entry_target
)) + t
->size
;
770 fw3_xt_set_target_name(t
, name
);
772 t
->t
->u
.user
.revision
= t
->revision
;
773 t
->t
->u
.target_size
= s
;
775 /* free previous userspace data */
776 fw3_xt_free_target_udata(t
);
781 /* merge option table */
782 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
783 fw3_xt_merge_target_options(g
, t
);
791 fw3_ipt_rule_proto(struct fw3_ipt_rule
*r
, struct fw3_protocol
*proto
)
795 if (!proto
|| proto
->any
)
798 pr
= proto
->protocol
;
801 if (r
->h
->family
== FW3_FAMILY_V6
)
806 r
->e6
.ipv6
.proto
= pr
;
807 r
->e6
.ipv6
.flags
|= IP6T_F_PROTO
;
810 r
->e6
.ipv6
.invflags
|= XT_INV_PROTO
;
818 r
->e
.ip
.invflags
|= XT_INV_PROTO
;
825 fw3_ipt_rule_in_out(struct fw3_ipt_rule
*r
,
826 struct fw3_device
*in
, struct fw3_device
*out
)
829 if (r
->h
->family
== FW3_FAMILY_V6
)
833 xtables_parse_interface(in
->name
, r
->e6
.ipv6
.iniface
,
834 r
->e6
.ipv6
.iniface_mask
);
837 r
->e6
.ipv6
.invflags
|= IP6T_INV_VIA_IN
;
840 if (out
&& !out
->any
)
842 xtables_parse_interface(out
->name
, r
->e6
.ipv6
.outiface
,
843 r
->e6
.ipv6
.outiface_mask
);
846 r
->e6
.ipv6
.invflags
|= IP6T_INV_VIA_OUT
;
854 xtables_parse_interface(in
->name
, r
->e
.ip
.iniface
,
855 r
->e
.ip
.iniface_mask
);
858 r
->e
.ip
.invflags
|= IPT_INV_VIA_IN
;
861 if (out
&& !out
->any
)
863 xtables_parse_interface(out
->name
, r
->e
.ip
.outiface
,
864 r
->e
.ip
.outiface_mask
);
867 r
->e
.ip
.invflags
|= IPT_INV_VIA_OUT
;
874 fw3_ipt_rule_src_dest(struct fw3_ipt_rule
*r
,
875 struct fw3_address
*src
, struct fw3_address
*dest
)
877 if ((src
&& src
->range
) || (dest
&& dest
->range
))
879 fw3_ipt_rule_addarg(r
, false, "-m", "iprange");
886 fw3_ipt_rule_addarg(r
, src
->invert
, "--src-range",
887 fw3_address_to_string(src
, false, false));
890 else if (r
->h
->family
== FW3_FAMILY_V6
)
892 r
->e6
.ipv6
.src
= src
->address
.v6
;
893 r
->e6
.ipv6
.smsk
= src
->mask
.v6
;
896 for (i
= 0; i
< 4; i
++)
897 r
->e6
.ipv6
.src
.s6_addr32
[i
] &= r
->e6
.ipv6
.smsk
.s6_addr32
[i
];
900 r
->e6
.ipv6
.invflags
|= IP6T_INV_SRCIP
;
905 r
->e
.ip
.src
= src
->address
.v4
;
906 r
->e
.ip
.smsk
= src
->mask
.v4
;
908 r
->e
.ip
.src
.s_addr
&= r
->e
.ip
.smsk
.s_addr
;
911 r
->e
.ip
.invflags
|= IPT_INV_SRCIP
;
915 if (dest
&& dest
->set
)
919 fw3_ipt_rule_addarg(r
, dest
->invert
, "--dst-range",
920 fw3_address_to_string(dest
, false, false));
923 else if (r
->h
->family
== FW3_FAMILY_V6
)
925 r
->e6
.ipv6
.dst
= dest
->address
.v6
;
926 r
->e6
.ipv6
.dmsk
= dest
->mask
.v6
;
929 for (i
= 0; i
< 4; i
++)
930 r
->e6
.ipv6
.dst
.s6_addr32
[i
] &= r
->e6
.ipv6
.dmsk
.s6_addr32
[i
];
933 r
->e6
.ipv6
.invflags
|= IP6T_INV_DSTIP
;
938 r
->e
.ip
.dst
= dest
->address
.v4
;
939 r
->e
.ip
.dmsk
= dest
->mask
.v4
;
941 r
->e
.ip
.dst
.s_addr
&= r
->e
.ip
.dmsk
.s_addr
;
944 r
->e
.ip
.invflags
|= IPT_INV_DSTIP
;
950 fw3_ipt_rule_sport_dport(struct fw3_ipt_rule
*r
,
951 struct fw3_port
*sp
, struct fw3_port
*dp
)
953 char buf
[sizeof("65535:65535")];
955 if ((!sp
|| !sp
->set
) && (!dp
|| !dp
->set
))
958 if (!get_protoname(r
))
963 if (sp
->port_min
== sp
->port_max
)
964 snprintf(buf
, sizeof(buf
), "%u", sp
->port_min
);
966 snprintf(buf
, sizeof(buf
), "%u:%u", sp
->port_min
, sp
->port_max
);
968 fw3_ipt_rule_addarg(r
, sp
->invert
, "--sport", buf
);
973 if (dp
->port_min
== dp
->port_max
)
974 snprintf(buf
, sizeof(buf
), "%u", dp
->port_min
);
976 snprintf(buf
, sizeof(buf
), "%u:%u", dp
->port_min
, dp
->port_max
);
978 fw3_ipt_rule_addarg(r
, dp
->invert
, "--dport", buf
);
983 fw3_ipt_rule_device(struct fw3_ipt_rule
*r
, const char *device
, bool out
)
985 struct fw3_device dev
= { .any
= false };
988 snprintf(dev
.name
, sizeof(dev
.name
), "%s", device
);
989 fw3_ipt_rule_in_out(r
, (out
) ? NULL
: &dev
, (out
) ? &dev
: NULL
);
994 fw3_ipt_rule_mac(struct fw3_ipt_rule
*r
, struct fw3_mac
*mac
)
996 char buf
[sizeof("ff:ff:ff:ff:ff:ff")];
997 uint8_t *addr
= mac
->mac
.ether_addr_octet
;
1002 snprintf(buf
, sizeof(buf
), "%02x:%02x:%02x:%02x:%02x:%02x",
1003 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
1005 fw3_ipt_rule_addarg(r
, false, "-m", "mac");
1006 fw3_ipt_rule_addarg(r
, mac
->invert
, "--mac-source", buf
);
1010 fw3_ipt_rule_icmptype(struct fw3_ipt_rule
*r
, struct fw3_icmptype
*icmp
)
1012 char buf
[sizeof("255/255")];
1017 #ifndef DISABLE_IPV6
1018 if (r
->h
->family
== FW3_FAMILY_V6
)
1020 if (icmp
->code6_min
== 0 && icmp
->code6_max
== 0xFF)
1021 snprintf(buf
, sizeof(buf
), "%u", icmp
->type6
);
1023 snprintf(buf
, sizeof(buf
), "%u/%u", icmp
->type6
, icmp
->code6_min
);
1025 fw3_ipt_rule_addarg(r
, icmp
->invert
, "--icmpv6-type", buf
);
1030 if (icmp
->code_min
== 0 && icmp
->code_max
== 0xFF)
1031 snprintf(buf
, sizeof(buf
), "%u", icmp
->type
);
1033 snprintf(buf
, sizeof(buf
), "%u/%u", icmp
->type
, icmp
->code_min
);
1035 fw3_ipt_rule_addarg(r
, icmp
->invert
, "--icmp-type", buf
);
1040 fw3_ipt_rule_limit(struct fw3_ipt_rule
*r
, struct fw3_limit
*limit
)
1042 char buf
[sizeof("-4294967296/second")];
1044 if (!limit
|| limit
->rate
<= 0)
1047 fw3_ipt_rule_addarg(r
, false, "-m", "limit");
1049 snprintf(buf
, sizeof(buf
), "%u/%s", limit
->rate
, fw3_limit_units
[limit
->unit
]);
1050 fw3_ipt_rule_addarg(r
, limit
->invert
, "--limit", buf
);
1052 if (limit
->burst
> 0)
1054 snprintf(buf
, sizeof(buf
), "%u", limit
->burst
);
1055 fw3_ipt_rule_addarg(r
, limit
->invert
, "--limit-burst", buf
);
1060 fw3_ipt_rule_ipset(struct fw3_ipt_rule
*r
, struct fw3_setmatch
*match
)
1062 char buf
[sizeof("dst,dst,dst")];
1065 size_t rem
= sizeof(buf
);
1067 struct fw3_ipset
*set
;
1068 struct fw3_ipset_datatype
*type
;
1070 if (!match
|| !match
->set
|| !match
->ptr
)
1074 list_for_each_entry(type
, &set
->datatypes
, list
)
1088 len
= snprintf(p
, rem
, "%s", match
->dir
[i
] ? match
->dir
[i
] : type
->dir
);
1090 if (len
< 0 || len
>= rem
)
1099 fw3_ipt_rule_addarg(r
, false, "-m", "set");
1101 fw3_ipt_rule_addarg(r
, match
->invert
, "--match-set",
1102 set
->external
? set
->external
: set
->name
);
1104 fw3_ipt_rule_addarg(r
, false, buf
, NULL
);
1108 fw3_ipt_rule_helper(struct fw3_ipt_rule
*r
, struct fw3_cthelpermatch
*match
)
1110 if (!match
|| !match
->set
|| !match
->ptr
)
1113 fw3_ipt_rule_addarg(r
, false, "-m", "helper");
1114 fw3_ipt_rule_addarg(r
, match
->invert
, "--helper", match
->ptr
->name
);
1118 fw3_ipt_rule_time(struct fw3_ipt_rule
*r
, struct fw3_time
*time
)
1121 struct tm empty
= { 0 };
1123 char buf
[84]; /* sizeof("1,2,3,...,30,31") */
1126 bool d1
= memcmp(&time
->datestart
, &empty
, sizeof(empty
));
1127 bool d2
= memcmp(&time
->datestop
, &empty
, sizeof(empty
));
1131 if (!d1
&& !d2
&& !time
->timestart
&& !time
->timestop
&&
1132 !(time
->monthdays
& 0xFFFFFFFE) && !(time
->weekdays
& 0xFE))
1137 fw3_ipt_rule_addarg(r
, false, "-m", "time");
1140 fw3_ipt_rule_addarg(r
, false, "--kerneltz", NULL
);
1144 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestart
);
1145 fw3_ipt_rule_addarg(r
, false, "--datestart", buf
);
1150 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestop
);
1151 fw3_ipt_rule_addarg(r
, false, "--datestop", buf
);
1154 if (time
->timestart
)
1156 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
1157 time
->timestart
/ 3600,
1158 time
->timestart
% 3600 / 60,
1159 time
->timestart
% 60);
1161 fw3_ipt_rule_addarg(r
, false, "--timestart", buf
);
1166 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
1167 time
->timestop
/ 3600,
1168 time
->timestop
% 3600 / 60,
1169 time
->timestop
% 60);
1171 fw3_ipt_rule_addarg(r
, false, "--timestop", buf
);
1174 if (time
->monthdays
& 0xFFFFFFFE)
1176 for (i
= 1, p
= buf
, rem
= sizeof(buf
); i
< 32; i
++)
1178 if (fw3_hasbit(time
->monthdays
, i
))
1189 len
= snprintf(p
, rem
, "%u", i
);
1191 if (len
< 0 || len
>= rem
)
1199 fw3_ipt_rule_addarg(r
, fw3_hasbit(time
->monthdays
, 0), "--monthdays", buf
);
1202 if (time
->weekdays
& 0xFE)
1204 for (i
= 1, p
= buf
, rem
= sizeof(buf
); i
< 8; i
++)
1206 if (fw3_hasbit(time
->weekdays
, i
))
1217 len
= snprintf(p
, rem
, "%u", i
);
1219 if (len
< 0 || len
>= rem
)
1227 fw3_ipt_rule_addarg(r
, fw3_hasbit(time
->weekdays
, 0), "--weekdays", buf
);
1232 fw3_ipt_rule_mark(struct fw3_ipt_rule
*r
, struct fw3_mark
*mark
)
1234 char buf
[sizeof("0xFFFFFFFF/0xFFFFFFFF")];
1236 if (!mark
|| !mark
->set
)
1239 if (mark
->mask
< 0xFFFFFFFF)
1240 snprintf(buf
, sizeof(buf
), "0x%x/0x%x", mark
->mark
, mark
->mask
);
1242 snprintf(buf
, sizeof(buf
), "0x%x", mark
->mark
);
1244 fw3_ipt_rule_addarg(r
, false, "-m", "mark");
1245 fw3_ipt_rule_addarg(r
, mark
->invert
, "--mark", buf
);
1249 fw3_ipt_rule_dscp(struct fw3_ipt_rule
*r
, struct fw3_dscp
*dscp
)
1251 char buf
[sizeof("0xFF")];
1253 if (!dscp
|| !dscp
->set
)
1256 snprintf(buf
, sizeof(buf
), "0x%x", dscp
->dscp
);
1258 fw3_ipt_rule_addarg(r
, false, "-m", "dscp");
1259 fw3_ipt_rule_addarg(r
, dscp
->invert
, "--dscp", buf
);
1263 fw3_ipt_rule_comment(struct fw3_ipt_rule
*r
, const char *fmt
, ...)
1272 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1275 fw3_ipt_rule_addarg(r
, false, "-m", "comment");
1276 fw3_ipt_rule_addarg(r
, false, "--comment", buf
);
1280 fw3_ipt_rule_extra(struct fw3_ipt_rule
*r
, const char *extra
)
1284 if (!extra
|| !*extra
)
1287 s
= fw3_strdup(extra
);
1289 for (p
= strtok(s
, " \t"); p
; p
= strtok(NULL
, " \t"))
1291 tmp
= realloc(r
->argv
, (r
->argc
+ 1) * sizeof(*r
->argv
));
1297 r
->argv
[r
->argc
++] = fw3_strdup(p
);
1303 #ifndef DISABLE_IPV6
1305 rule_print6(struct ip6t_entry
*e
)
1307 char buf1
[INET6_ADDRSTRLEN
], buf2
[INET6_ADDRSTRLEN
];
1310 if (e
->ipv6
.flags
& IP6T_F_PROTO
)
1312 if (e
->ipv6
.invflags
& XT_INV_PROTO
)
1315 pname
= get_protoname(container_of(e
, struct fw3_ipt_rule
, e6
));
1318 printf(" -p %s", pname
);
1320 printf(" -p %u", e
->ipv6
.proto
);
1323 if (e
->ipv6
.iniface
[0])
1325 if (e
->ipv6
.invflags
& IP6T_INV_VIA_IN
)
1328 printf(" -i %s", e
->ipv6
.iniface
);
1331 if (e
->ipv6
.outiface
[0])
1333 if (e
->ipv6
.invflags
& IP6T_INV_VIA_OUT
)
1336 printf(" -o %s", e
->ipv6
.outiface
);
1339 if (memcmp(&e
->ipv6
.src
, &in6addr_any
, sizeof(struct in6_addr
)))
1341 if (e
->ipv6
.invflags
& IP6T_INV_SRCIP
)
1345 inet_ntop(AF_INET6
, &e
->ipv6
.src
, buf1
, sizeof(buf1
)),
1346 inet_ntop(AF_INET6
, &e
->ipv6
.smsk
, buf2
, sizeof(buf2
)));
1349 if (memcmp(&e
->ipv6
.dst
, &in6addr_any
, sizeof(struct in6_addr
)))
1351 if (e
->ipv6
.invflags
& IP6T_INV_DSTIP
)
1355 inet_ntop(AF_INET6
, &e
->ipv6
.dst
, buf1
, sizeof(buf1
)),
1356 inet_ntop(AF_INET6
, &e
->ipv6
.dmsk
, buf2
, sizeof(buf2
)));
1362 rule_print4(struct ipt_entry
*e
)
1364 struct in_addr in_zero
= { 0 };
1365 char buf1
[sizeof("255.255.255.255")], buf2
[sizeof("255.255.255.255")];
1370 if (e
->ip
.invflags
& XT_INV_PROTO
)
1373 pname
= get_protoname(container_of(e
, struct fw3_ipt_rule
, e
));
1376 printf(" -p %s", pname
);
1378 printf(" -p %u", e
->ip
.proto
);
1381 if (e
->ip
.iniface
[0])
1383 if (e
->ip
.invflags
& IPT_INV_VIA_IN
)
1386 printf(" -i %s", e
->ip
.iniface
);
1389 if (e
->ip
.outiface
[0])
1391 if (e
->ip
.invflags
& IPT_INV_VIA_OUT
)
1394 printf(" -o %s", e
->ip
.outiface
);
1397 if (memcmp(&e
->ip
.src
, &in_zero
, sizeof(struct in_addr
)))
1399 if (e
->ip
.invflags
& IPT_INV_SRCIP
)
1403 inet_ntop(AF_INET
, &e
->ip
.src
, buf1
, sizeof(buf1
)),
1404 inet_ntop(AF_INET
, &e
->ip
.smsk
, buf2
, sizeof(buf2
)));
1407 if (memcmp(&e
->ip
.dst
, &in_zero
, sizeof(struct in_addr
)))
1409 if (e
->ip
.invflags
& IPT_INV_DSTIP
)
1413 inet_ntop(AF_INET
, &e
->ip
.dst
, buf1
, sizeof(buf1
)),
1414 inet_ntop(AF_INET
, &e
->ip
.dmsk
, buf2
, sizeof(buf2
)));
1419 rule_print(struct fw3_ipt_rule
*r
, const char *prefix
, const char *chain
)
1421 debug(r
->h
, "%s %s", prefix
, chain
);
1423 #ifndef DISABLE_IPV6
1424 if (r
->h
->family
== FW3_FAMILY_V6
)
1425 rule_print6(&r
->e6
);
1430 fw3_xt_print_matches(&r
->e
.ip
, r
->matches
);
1431 fw3_xt_print_target(&r
->e
.ip
, r
->target
);
1437 parse_option(struct fw3_ipt_rule
*r
, int optc
, bool inv
)
1439 struct xtables_rule_match
*m
;
1440 struct xtables_match
*em
;
1442 /* is a target option */
1443 if (r
->target
&& fw3_xt_has_target_parse(r
->target
) &&
1444 optc
>= r
->target
->option_offset
&&
1445 optc
< (r
->target
->option_offset
+ 256))
1447 xtables_option_tpcall(optc
, r
->argv
, inv
, r
->target
, &r
->e
);
1451 /* try to dispatch argument to one of the match parsers */
1452 for (m
= r
->matches
; m
; m
= m
->next
)
1456 if (m
->completed
|| !fw3_xt_has_match_parse(em
))
1459 if (optc
< em
->option_offset
||
1460 optc
>= (em
->option_offset
+ 256))
1463 xtables_option_mpcall(optc
, r
->argv
, inv
, em
, &r
->e
);
1467 /* unhandled option, might belong to a protocol match */
1468 if ((em
= load_protomatch(r
)) != NULL
)
1470 init_match(r
, em
, false);
1472 r
->protocol_loaded
= true;
1479 warn("parse_option(): option '%s' needs argument", r
->argv
[optind
-1]);
1482 warn("parse_option(): unknown option '%s'", r
->argv
[optind
-1]);
1488 fw3_ipt_rule_addarg(struct fw3_ipt_rule
*r
, bool inv
,
1489 const char *k
, const char *v
)
1497 n
= inv
+ !!k
+ !!v
;
1498 tmp
= realloc(r
->argv
, (r
->argc
+ n
) * sizeof(*tmp
));
1506 r
->argv
[r
->argc
++] = fw3_strdup("!");
1508 r
->argv
[r
->argc
++] = fw3_strdup(k
);
1511 r
->argv
[r
->argc
++] = fw3_strdup(v
);
1514 static unsigned char *
1515 rule_mask(struct fw3_ipt_rule
*r
)
1518 unsigned char *p
, *mask
= NULL
;
1519 struct xtables_rule_match
*m
;
1521 #define SZ(x) XT_ALIGN(sizeof(struct x))
1523 #ifndef DISABLE_IPV6
1524 if (r
->h
->family
== FW3_FAMILY_V6
)
1528 for (m
= r
->matches
; m
; m
= m
->next
)
1529 s
+= SZ(ip6t_entry_match
) + m
->match
->size
;
1531 s
+= SZ(ip6t_entry_target
);
1533 s
+= r
->target
->size
;
1535 mask
= fw3_alloc(s
);
1536 memset(mask
, 0xFF, SZ(ip6t_entry
));
1537 p
= mask
+ SZ(ip6t_entry
);
1539 for (m
= r
->matches
; m
; m
= m
->next
)
1541 memset(p
, 0xFF, SZ(ip6t_entry_match
) + m
->match
->userspacesize
);
1542 p
+= SZ(ip6t_entry_match
) + m
->match
->size
;
1545 memset(p
, 0xFF, SZ(ip6t_entry_target
) + (r
->target
? r
->target
->userspacesize
: 0));
1552 for (m
= r
->matches
; m
; m
= m
->next
)
1553 s
+= SZ(ipt_entry_match
) + m
->match
->size
;
1555 s
+= SZ(ipt_entry_target
);
1557 s
+= r
->target
->size
;
1559 mask
= fw3_alloc(s
);
1560 memset(mask
, 0xFF, SZ(ipt_entry
));
1561 p
= mask
+ SZ(ipt_entry
);
1563 for (m
= r
->matches
; m
; m
= m
->next
)
1565 memset(p
, 0xFF, SZ(ipt_entry_match
) + m
->match
->userspacesize
);
1566 p
+= SZ(ipt_entry_match
) + m
->match
->size
;
1569 memset(p
, 0xFF, SZ(ipt_entry_target
) + (r
->target
? r
->target
->userspacesize
: 0));
1576 rule_build(struct fw3_ipt_rule
*r
)
1578 size_t s
, target_size
= (r
->target
) ? r
->target
->t
->u
.target_size
: 0;
1579 struct xtables_rule_match
*m
;
1581 #ifndef DISABLE_IPV6
1582 if (r
->h
->family
== FW3_FAMILY_V6
)
1584 struct ip6t_entry
*e6
;
1586 s
= XT_ALIGN(sizeof(struct ip6t_entry
));
1588 for (m
= r
->matches
; m
; m
= m
->next
)
1589 s
+= m
->match
->m
->u
.match_size
;
1591 e6
= fw3_alloc(s
+ target_size
);
1593 memcpy(e6
, &r
->e6
, sizeof(struct ip6t_entry
));
1595 e6
->target_offset
= s
;
1596 e6
->next_offset
= s
+ target_size
;
1600 for (m
= r
->matches
; m
; m
= m
->next
)
1602 memcpy(e6
->elems
+ s
, m
->match
->m
, m
->match
->m
->u
.match_size
);
1603 s
+= m
->match
->m
->u
.match_size
;
1607 memcpy(e6
->elems
+ s
, r
->target
->t
, target_size
);
1614 struct ipt_entry
*e
;
1616 s
= XT_ALIGN(sizeof(struct ipt_entry
));
1618 for (m
= r
->matches
; m
; m
= m
->next
)
1619 s
+= m
->match
->m
->u
.match_size
;
1621 e
= fw3_alloc(s
+ target_size
);
1623 memcpy(e
, &r
->e
, sizeof(struct ipt_entry
));
1625 e
->target_offset
= s
;
1626 e
->next_offset
= s
+ target_size
;
1630 for (m
= r
->matches
; m
; m
= m
->next
)
1632 memcpy(e
->elems
+ s
, m
->match
->m
, m
->match
->m
->u
.match_size
);
1633 s
+= m
->match
->m
->u
.match_size
;
1637 memcpy(e
->elems
+ s
, r
->target
->t
, target_size
);
1644 set_rule_tag(struct fw3_ipt_rule
*r
)
1648 const char *tag
= "!fw3";
1650 for (i
= 0; i
< r
->argc
; i
++)
1651 if (!strcmp(r
->argv
[i
], "--comment") && (i
+ 1) < r
->argc
)
1652 if (asprintf(&p
, "%s: %s", tag
, r
->argv
[i
+ 1]) > 0)
1654 free(r
->argv
[i
+ 1]);
1659 tmp
= realloc(r
->argv
, (r
->argc
+ 4) * sizeof(*r
->argv
));
1664 r
->argv
[r
->argc
++] = fw3_strdup("-m");
1665 r
->argv
[r
->argc
++] = fw3_strdup("comment");
1666 r
->argv
[r
->argc
++] = fw3_strdup("--comment");
1667 r
->argv
[r
->argc
++] = fw3_strdup(tag
);
1672 __fw3_ipt_rule_append(struct fw3_ipt_rule
*r
, bool repl
, const char *fmt
, ...)
1675 unsigned char *mask
;
1677 struct xtables_rule_match
*m
;
1678 struct xtables_match
*em
;
1679 struct xtables_target
*et
;
1680 struct xtables_globals
*g
;
1682 struct fw3_device dev
;
1683 struct fw3_address addr
;
1685 enum xtables_exittype status
;
1693 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
1696 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
1697 g
->opts
= g
->orig_opts
;
1702 status
= setjmp(fw3_ipt_error_jmp
);
1706 info(" ! Skipping due to previous exception (code %u)", status
);
1712 while ((optc
= getopt_long(r
->argc
, r
->argv
, "-:m:j:i:o:s:d:", g
->opts
,
1718 em
= find_match(r
, optarg
);
1722 warn("fw3_ipt_rule_append(): Can't find match '%s'", optarg
);
1726 init_match(r
, em
, true);
1730 et
= get_target(r
, optarg
);
1734 warn("fw3_ipt_rule_append(): Can't find target '%s'", optarg
);
1742 if (!fw3_parse_device(&dev
, optarg
, false) ||
1743 dev
.any
|| dev
.invert
|| *dev
.network
)
1745 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg
);
1750 fw3_ipt_rule_in_out(r
, (optc
== 'i') ? &dev
: NULL
,
1751 (optc
== 'o') ? &dev
: NULL
);
1756 if (!fw3_parse_address(&addr
, optarg
, false) ||
1757 addr
.range
|| addr
.invert
)
1759 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg
);
1764 fw3_ipt_rule_src_dest(r
, (optc
== 's') ? &addr
: NULL
,
1765 (optc
== 'd') ? &addr
: NULL
);
1769 if ((optarg
[0] == '!') && (optarg
[1] == '\0'))
1776 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg
);
1780 if (parse_option(r
, optc
, inv
))
1788 for (m
= r
->matches
; m
; m
= m
->next
)
1789 xtables_option_mfcall(m
->match
);
1792 xtables_option_tfcall(r
->target
);
1794 rule
= rule_build(r
);
1796 #ifndef DISABLE_IPV6
1797 if (r
->h
->family
== FW3_FAMILY_V6
)
1801 mask
= rule_mask(r
);
1803 while (ip6tc_delete_entry(buf
, rule
, mask
, r
->h
->handle
))
1805 rule_print(r
, "-D", buf
);
1811 rule_print(r
, "-A", buf
);
1813 if (!ip6tc_append_entry(buf
, rule
, r
->h
->handle
))
1814 warn("ip6tc_append_entry(): %s", ip6tc_strerror(errno
));
1821 mask
= rule_mask(r
);
1823 while (iptc_delete_entry(buf
, rule
, mask
, r
->h
->handle
))
1825 rule_print(r
, "-D", buf
);
1831 rule_print(r
, "-A", buf
);
1833 if (!iptc_append_entry(buf
, rule
, r
->h
->handle
))
1834 warn("iptc_append_entry(): %s\n", iptc_strerror(errno
));
1840 for (i
= 1; i
< r
->argc
; i
++)
1845 xtables_rule_matches_free(&r
->matches
);
1852 /* reset all targets and matches */
1853 for (em
= xtables_matches
; em
; em
= em
->next
)
1856 for (et
= xtables_targets
; et
; et
= et
->next
)
1862 xtables_free_opts(1);
1865 struct fw3_ipt_rule
*
1866 fw3_ipt_rule_create(struct fw3_ipt_handle
*handle
, struct fw3_protocol
*proto
,
1867 struct fw3_device
*in
, struct fw3_device
*out
,
1868 struct fw3_address
*src
, struct fw3_address
*dest
)
1870 struct fw3_ipt_rule
*r
;
1872 r
= fw3_ipt_rule_new(handle
);
1874 fw3_ipt_rule_proto(r
, proto
);
1875 fw3_ipt_rule_in_out(r
, in
, out
);
1876 fw3_ipt_rule_src_dest(r
, src
, dest
);
1882 xtables_register_match(struct xtables_match
*me
)
1885 static struct xtables_match
**tmp
;
1887 if (!xext
.register_match
)
1888 xext
.register_match
= dlsym(RTLD_NEXT
, "xtables_register_match");
1890 if (!xext
.register_match
)
1893 xext
.register_match(me
);
1897 for (i
= 0; i
< xext
.mcount
; i
++)
1898 if (xext
.matches
[i
] == me
)
1901 tmp
= realloc(xext
.matches
, sizeof(me
) * (xext
.mcount
+ 1));
1907 xext
.matches
[xext
.mcount
++] = me
;
1912 xtables_register_target(struct xtables_target
*me
)
1915 static struct xtables_target
**tmp
;
1917 if (!xext
.register_target
)
1918 xext
.register_target
= dlsym(RTLD_NEXT
, "xtables_register_target");
1920 if (!xext
.register_target
)
1923 xext
.register_target(me
);
1927 for (i
= 0; i
< xext
.tcount
; i
++)
1928 if (xext
.targets
[i
] == me
)
1931 tmp
= realloc(xext
.targets
, sizeof(me
) * (xext
.tcount
+ 1));
1937 xext
.targets
[xext
.tcount
++] = me
;