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"
60 struct fw3_ipt_handle
*h
;
67 struct xtables_rule_match
*matches
;
68 struct xtables_target
*target
;
77 static struct option base_opts
[] = {
78 { .name
= "match", .has_arg
= 1, .val
= 'm' },
79 { .name
= "jump", .has_arg
= 1, .val
= 'j' },
80 { .name
= "in-interface", .has_arg
= 1, .val
= 'i' },
81 { .name
= "out-interface", .has_arg
= 1, .val
= 'o' },
82 { .name
= "source", .has_arg
= 1, .val
= 's' },
83 { .name
= "destination", .has_arg
= 1, .val
= 'd' },
88 static jmp_buf fw3_ipt_error_jmp
;
90 static __attribute__((noreturn
))
91 void fw3_ipt_error_handler(enum xtables_exittype status
,
96 fprintf(stderr
, " ! Exception: ");
99 vfprintf(stderr
, fmt
, args
);
102 longjmp(fw3_ipt_error_jmp
, status
);
105 static struct xtables_globals xtg
= {
107 .program_version
= "4",
108 .orig_opts
= base_opts
,
109 .exit_err
= fw3_ipt_error_handler
,
110 #if XTABLES_VERSION_CODE > 10
111 .compat_rev
= xtables_compatible_revision
,
115 static struct xtables_globals xtg6
= {
117 .program_version
= "6",
118 .orig_opts
= base_opts
,
119 .exit_err
= fw3_ipt_error_handler
,
120 #if XTABLES_VERSION_CODE > 10
121 .compat_rev
= xtables_compatible_revision
,
128 struct xtables_match
**matches
;
129 struct xtables_target
**targets
;
130 void (*register_match
)(struct xtables_match
*);
131 void (*register_target
)(struct xtables_target
*);
135 /* Required by certain extensions like SNAT and DNAT */
136 int kernel_version
= 0;
139 get_kernel_version(void)
141 static struct utsname uts
;
142 int x
= 0, y
= 0, z
= 0;
144 if (uname(&uts
) == -1)
145 sprintf(uts
.release
, "3.0.0");
147 sscanf(uts
.release
, "%d.%d.%d", &x
, &y
, &z
);
148 kernel_version
= 0x10000 * x
+ 0x100 * y
+ z
;
151 static void fw3_init_extensions(void)
161 struct fw3_ipt_handle
*
162 fw3_ipt_open(enum fw3_family family
, enum fw3_table table
)
165 struct fw3_ipt_handle
*h
;
167 h
= fw3_alloc(sizeof(*h
));
171 if (family
== FW3_FAMILY_V6
)
174 h
->family
= FW3_FAMILY_V6
;
176 h
->handle
= ip6tc_init(fw3_flag_names
[table
]);
178 xtables_set_params(&xtg6
);
179 xtables_set_nfproto(NFPROTO_IPV6
);
184 h
->family
= FW3_FAMILY_V4
;
186 h
->handle
= iptc_init(fw3_flag_names
[table
]);
188 xtables_set_params(&xtg
);
189 xtables_set_nfproto(NFPROTO_IPV4
);
199 fw3_init_extensions();
201 if (xext
.register_match
)
202 for (i
= 0; i
< xext
.mcount
; i
++)
203 xext
.register_match(xext
.matches
[i
]);
205 if (xext
.register_target
)
206 for (i
= 0; i
< xext
.tcount
; i
++)
207 xext
.register_target(xext
.targets
[i
]);
213 debug(struct fw3_ipt_handle
*h
, const char *fmt
, ...)
217 printf("%s -t %s ", (h
->family
== FW3_FAMILY_V6
) ? "ip6tables" : "iptables",
218 fw3_flag_names
[h
->table
]);
226 fw3_ipt_set_policy(struct fw3_ipt_handle
*h
, const char *chain
,
227 enum fw3_flag policy
)
230 debug(h
, "-P %s %s\n", chain
, fw3_flag_names
[policy
]);
233 if (h
->family
== FW3_FAMILY_V6
)
234 ip6tc_set_policy(chain
, fw3_flag_names
[policy
], NULL
, h
->handle
);
237 iptc_set_policy(chain
, fw3_flag_names
[policy
], NULL
, h
->handle
);
241 fw3_ipt_flush_chain(struct fw3_ipt_handle
*h
, const char *chain
)
244 debug(h
, "-F %s\n", chain
);
247 if (h
->family
== FW3_FAMILY_V6
)
248 ip6tc_flush_entries(chain
, h
->handle
);
251 iptc_flush_entries(chain
, h
->handle
);
255 delete_rules(struct fw3_ipt_handle
*h
, const char *target
)
258 const struct ipt_entry
*e
;
264 if (h
->family
== FW3_FAMILY_V6
)
266 for (chain
= ip6tc_first_chain(h
->handle
);
268 chain
= ip6tc_next_chain(h
->handle
))
273 const struct ip6t_entry
*e6
;
274 for (num
= 0, e6
= ip6tc_first_rule(chain
, h
->handle
);
276 num
++, e6
= ip6tc_next_rule(e6
, h
->handle
))
278 t
= ip6tc_get_target(e6
, h
->handle
);
280 if (*t
&& !strcmp(t
, target
))
283 debug(h
, "-D %s %u\n", chain
, num
+ 1);
285 ip6tc_delete_num_entry(chain
, num
, h
->handle
);
296 for (chain
= iptc_first_chain(h
->handle
);
298 chain
= iptc_next_chain(h
->handle
))
303 for (num
= 0, e
= iptc_first_rule(chain
, h
->handle
);
305 num
++, e
= iptc_next_rule(e
, h
->handle
))
307 t
= iptc_get_target(e
, h
->handle
);
309 if (*t
&& !strcmp(t
, target
))
312 debug(h
, "-D %s %u\n", chain
, num
+ 1);
314 iptc_delete_num_entry(chain
, num
, h
->handle
);
325 fw3_ipt_delete_chain(struct fw3_ipt_handle
*h
, const char *chain
)
327 delete_rules(h
, chain
);
330 debug(h
, "-X %s\n", chain
);
333 if (h
->family
== FW3_FAMILY_V6
)
334 ip6tc_delete_chain(chain
, h
->handle
);
337 iptc_delete_chain(chain
, h
->handle
);
341 has_rule_tag(const void *base
, unsigned int start
, unsigned int end
)
344 const struct xt_entry_match
*em
;
346 for (i
= start
; i
< end
; i
+= em
->u
.match_size
)
350 if (strcmp(em
->u
.user
.name
, "comment"))
353 if (!memcmp(em
->data
, "!fw3", 4))
361 fw3_ipt_delete_id_rules(struct fw3_ipt_handle
*h
, const char *chain
)
364 const struct ipt_entry
*e
;
368 if (h
->family
== FW3_FAMILY_V6
)
370 if (!ip6tc_is_chain(chain
, h
->handle
))
376 const struct ip6t_entry
*e6
;
377 for (num
= 0, e6
= ip6tc_first_rule(chain
, h
->handle
);
379 num
++, e6
= ip6tc_next_rule(e6
, h
->handle
))
381 if (has_rule_tag(e6
, sizeof(*e6
), e6
->target_offset
))
384 debug(h
, "-D %s %u\n", chain
, num
+ 1);
386 ip6tc_delete_num_entry(chain
, num
, h
->handle
);
396 if (!iptc_is_chain(chain
, h
->handle
))
402 for (num
= 0, e
= iptc_first_rule(chain
, h
->handle
);
404 num
++, e
= iptc_next_rule(e
, h
->handle
))
406 if (has_rule_tag(e
, sizeof(*e
), e
->target_offset
))
409 debug(h
, "-D %s %u\n", chain
, num
+ 1);
411 iptc_delete_num_entry(chain
, num
, h
->handle
);
421 fw3_ipt_create_chain(struct fw3_ipt_handle
*h
, const char *fmt
, ...)
427 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
431 debug(h
, "-N %s\n", buf
);
433 iptc_create_chain(buf
, h
->handle
);
437 fw3_ipt_flush(struct fw3_ipt_handle
*h
)
442 if (h
->family
== FW3_FAMILY_V6
)
444 for (chain
= ip6tc_first_chain(h
->handle
);
446 chain
= ip6tc_next_chain(h
->handle
))
448 ip6tc_flush_entries(chain
, h
->handle
);
451 for (chain
= ip6tc_first_chain(h
->handle
);
453 chain
= ip6tc_next_chain(h
->handle
))
455 ip6tc_delete_chain(chain
, h
->handle
);
461 for (chain
= iptc_first_chain(h
->handle
);
463 chain
= iptc_next_chain(h
->handle
))
465 iptc_flush_entries(chain
, h
->handle
);
468 for (chain
= iptc_first_chain(h
->handle
);
470 chain
= iptc_next_chain(h
->handle
))
472 iptc_delete_chain(chain
, h
->handle
);
478 chain_is_empty(struct fw3_ipt_handle
*h
, const char *chain
)
481 if (h
->family
== FW3_FAMILY_V6
)
482 return (!ip6tc_builtin(chain
, h
->handle
) &&
483 !ip6tc_first_rule(chain
, h
->handle
));
486 return (!iptc_builtin(chain
, h
->handle
) &&
487 !iptc_first_rule(chain
, h
->handle
));
491 fw3_ipt_gc(struct fw3_ipt_handle
*h
)
497 if (h
->family
== FW3_FAMILY_V6
)
502 for (chain
= ip6tc_first_chain(h
->handle
);
504 chain
= ip6tc_next_chain(h
->handle
))
506 if (!chain_is_empty(h
, chain
))
509 fw3_ipt_delete_chain(h
, chain
);
521 for (chain
= iptc_first_chain(h
->handle
);
523 chain
= iptc_next_chain(h
->handle
))
525 warn("C=%s\n", chain
);
527 if (!chain_is_empty(h
, chain
))
530 warn("D=%s\n", chain
);
532 fw3_ipt_delete_chain(h
, chain
);
541 fw3_ipt_commit(struct fw3_ipt_handle
*h
)
546 if (h
->family
== FW3_FAMILY_V6
)
548 rv
= ip6tc_commit(h
->handle
);
550 warn("ip6tc_commit(): %s", ip6tc_strerror(errno
));
555 rv
= iptc_commit(h
->handle
);
557 warn("iptc_commit(): %s", iptc_strerror(errno
));
562 fw3_ipt_close(struct fw3_ipt_handle
*h
)
567 struct fw3_ipt_rule
*
568 fw3_ipt_rule_new(struct fw3_ipt_handle
*h
)
570 struct fw3_ipt_rule
*r
;
572 r
= fw3_alloc(sizeof(*r
));
575 r
->argv
= fw3_alloc(sizeof(char *));
576 r
->argv
[r
->argc
++] = "fw3";
583 is_chain(struct fw3_ipt_handle
*h
, const char *name
)
586 if (h
->family
== FW3_FAMILY_V6
)
587 return ip6tc_is_chain(name
, h
->handle
);
590 return iptc_is_chain(name
, h
->handle
);
594 get_protoname(struct fw3_ipt_rule
*r
)
596 const struct xtables_pprot
*pp
;
599 for (pp
= xtables_chain_protos
; pp
->name
; pp
++)
600 if (pp
->num
== r
->protocol
)
601 return (char *)pp
->name
;
606 static struct xtables_match
*
607 find_match(struct fw3_ipt_rule
*r
, const char *name
)
609 struct xtables_match
*m
;
612 m
= xtables_find_match(name
, XTF_TRY_LOAD
, &r
->matches
);
619 init_match(struct fw3_ipt_rule
*r
, struct xtables_match
*m
, bool no_clone
)
622 struct xtables_globals
*g
;
627 s
= XT_ALIGN(sizeof(struct xt_entry_match
)) + m
->size
;
631 fw3_xt_set_match_name(m
);
633 m
->m
->u
.user
.revision
= m
->revision
;
634 m
->m
->u
.match_size
= s
;
636 /* free previous userspace data */
637 fw3_xt_free_match_udata(m
);
642 /* don't merge options if no_clone is set and this match is a clone */
643 if (no_clone
&& (m
== m
->next
))
646 /* merge option table */
647 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
648 fw3_xt_merge_match_options(g
, m
);
652 need_protomatch(struct fw3_ipt_rule
*r
, const char *pname
)
657 if (!xtables_find_match(pname
, XTF_DONT_LOAD
, NULL
))
660 return !r
->protocol_loaded
;
663 static struct xtables_match
*
664 load_protomatch(struct fw3_ipt_rule
*r
)
666 const char *pname
= get_protoname(r
);
668 if (!need_protomatch(r
, pname
))
671 return find_match(r
, pname
);
674 static struct xtables_target
*
675 find_target(struct fw3_ipt_rule
*r
, const char *name
)
677 struct xtables_target
*t
;
681 if (is_chain(r
->h
, name
))
682 t
= xtables_find_target(XT_STANDARD_TARGET
, XTF_TRY_LOAD
);
684 t
= xtables_find_target(name
, XTF_TRY_LOAD
);
691 static struct xtables_target
*
692 get_target(struct fw3_ipt_rule
*r
, const char *name
)
695 struct xtables_target
*t
;
696 struct xtables_globals
*g
;
698 t
= find_target(r
, name
);
703 s
= XT_ALIGN(sizeof(struct xt_entry_target
)) + t
->size
;
706 fw3_xt_set_target_name(t
, name
);
708 t
->t
->u
.user
.revision
= t
->revision
;
709 t
->t
->u
.target_size
= s
;
711 /* free previous userspace data */
712 fw3_xt_free_target_udata(t
);
717 /* merge option table */
718 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
719 fw3_xt_merge_target_options(g
, t
);
727 fw3_ipt_rule_proto(struct fw3_ipt_rule
*r
, struct fw3_protocol
*proto
)
731 if (!proto
|| proto
->any
)
734 pr
= proto
->protocol
;
737 if (r
->h
->family
== FW3_FAMILY_V6
)
742 r
->e6
.ipv6
.proto
= pr
;
743 r
->e6
.ipv6
.flags
|= IP6T_F_PROTO
;
746 r
->e6
.ipv6
.invflags
|= XT_INV_PROTO
;
754 r
->e
.ip
.invflags
|= XT_INV_PROTO
;
761 fw3_ipt_rule_in_out(struct fw3_ipt_rule
*r
,
762 struct fw3_device
*in
, struct fw3_device
*out
)
765 if (r
->h
->family
== FW3_FAMILY_V6
)
769 xtables_parse_interface(in
->name
, r
->e6
.ipv6
.iniface
,
770 r
->e6
.ipv6
.iniface_mask
);
773 r
->e6
.ipv6
.invflags
|= IP6T_INV_VIA_IN
;
776 if (out
&& !out
->any
)
778 xtables_parse_interface(out
->name
, r
->e6
.ipv6
.outiface
,
779 r
->e6
.ipv6
.outiface_mask
);
782 r
->e6
.ipv6
.invflags
|= IP6T_INV_VIA_OUT
;
790 xtables_parse_interface(in
->name
, r
->e
.ip
.iniface
,
791 r
->e
.ip
.iniface_mask
);
794 r
->e
.ip
.invflags
|= IPT_INV_VIA_IN
;
797 if (out
&& !out
->any
)
799 xtables_parse_interface(out
->name
, r
->e
.ip
.outiface
,
800 r
->e
.ip
.outiface_mask
);
803 r
->e
.ip
.invflags
|= IPT_INV_VIA_OUT
;
810 fw3_ipt_rule_src_dest(struct fw3_ipt_rule
*r
,
811 struct fw3_address
*src
, struct fw3_address
*dest
)
813 if ((src
&& src
->range
) || (dest
&& dest
->range
))
815 fw3_ipt_rule_addarg(r
, false, "-m", "iprange");
822 fw3_ipt_rule_addarg(r
, src
->invert
, "--src-range",
823 fw3_address_to_string(src
, false, false));
826 else if (r
->h
->family
== FW3_FAMILY_V6
)
828 r
->e6
.ipv6
.src
= src
->address
.v6
;
829 r
->e6
.ipv6
.smsk
= src
->mask
.v6
;
832 for (i
= 0; i
< 4; i
++)
833 r
->e6
.ipv6
.src
.s6_addr32
[i
] &= r
->e6
.ipv6
.smsk
.s6_addr32
[i
];
836 r
->e6
.ipv6
.invflags
|= IP6T_INV_SRCIP
;
841 r
->e
.ip
.src
= src
->address
.v4
;
842 r
->e
.ip
.smsk
= src
->mask
.v4
;
844 r
->e
.ip
.src
.s_addr
&= r
->e
.ip
.smsk
.s_addr
;
847 r
->e
.ip
.invflags
|= IPT_INV_SRCIP
;
851 if (dest
&& dest
->set
)
855 fw3_ipt_rule_addarg(r
, dest
->invert
, "--dst-range",
856 fw3_address_to_string(dest
, false, false));
859 else if (r
->h
->family
== FW3_FAMILY_V6
)
861 r
->e6
.ipv6
.dst
= dest
->address
.v6
;
862 r
->e6
.ipv6
.dmsk
= dest
->mask
.v6
;
865 for (i
= 0; i
< 4; i
++)
866 r
->e6
.ipv6
.dst
.s6_addr32
[i
] &= r
->e6
.ipv6
.dmsk
.s6_addr32
[i
];
869 r
->e6
.ipv6
.invflags
|= IP6T_INV_DSTIP
;
874 r
->e
.ip
.dst
= dest
->address
.v4
;
875 r
->e
.ip
.dmsk
= dest
->mask
.v4
;
877 r
->e
.ip
.dst
.s_addr
&= r
->e
.ip
.dmsk
.s_addr
;
880 r
->e
.ip
.invflags
|= IPT_INV_DSTIP
;
886 fw3_ipt_rule_sport_dport(struct fw3_ipt_rule
*r
,
887 struct fw3_port
*sp
, struct fw3_port
*dp
)
889 char buf
[sizeof("65535:65535\0")];
891 if ((!sp
|| !sp
->set
) && (!dp
|| !dp
->set
))
894 if (!get_protoname(r
))
899 if (sp
->port_min
== sp
->port_max
)
900 sprintf(buf
, "%u", sp
->port_min
);
902 snprintf(buf
, sizeof(buf
), "%u:%u", sp
->port_min
, sp
->port_max
);
904 fw3_ipt_rule_addarg(r
, sp
->invert
, "--sport", buf
);
909 if (dp
->port_min
== dp
->port_max
)
910 sprintf(buf
, "%u", dp
->port_min
);
912 snprintf(buf
, sizeof(buf
), "%u:%u", dp
->port_min
, dp
->port_max
);
914 fw3_ipt_rule_addarg(r
, dp
->invert
, "--dport", buf
);
919 fw3_ipt_rule_device(struct fw3_ipt_rule
*r
, const char *device
, bool out
)
922 struct fw3_device dev
= { .any
= false };
923 strncpy(dev
.name
, device
, sizeof(dev
.name
) - 1);
924 fw3_ipt_rule_in_out(r
, (out
) ? NULL
: &dev
, (out
) ? &dev
: NULL
);
929 fw3_ipt_rule_mac(struct fw3_ipt_rule
*r
, struct fw3_mac
*mac
)
931 char buf
[sizeof("ff:ff:ff:ff:ff:ff\0")];
932 uint8_t *addr
= mac
->mac
.ether_addr_octet
;
937 sprintf(buf
, "%02x:%02x:%02x:%02x:%02x:%02x",
938 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
940 fw3_ipt_rule_addarg(r
, false, "-m", "mac");
941 fw3_ipt_rule_addarg(r
, mac
->invert
, "--mac-source", buf
);
945 fw3_ipt_rule_icmptype(struct fw3_ipt_rule
*r
, struct fw3_icmptype
*icmp
)
947 char buf
[sizeof("255/255\0")];
953 if (r
->h
->family
== FW3_FAMILY_V6
)
955 if (icmp
->code6_min
== 0 && icmp
->code6_max
== 0xFF)
956 sprintf(buf
, "%u", icmp
->type6
);
958 snprintf(buf
, sizeof(buf
), "%u/%u", icmp
->type6
, icmp
->code6_min
);
960 fw3_ipt_rule_addarg(r
, icmp
->invert
, "--icmpv6-type", buf
);
965 if (icmp
->code_min
== 0 && icmp
->code_max
== 0xFF)
966 sprintf(buf
, "%u", icmp
->type
);
968 snprintf(buf
, sizeof(buf
), "%u/%u", icmp
->type
, icmp
->code_min
);
970 fw3_ipt_rule_addarg(r
, icmp
->invert
, "--icmp-type", buf
);
975 fw3_ipt_rule_limit(struct fw3_ipt_rule
*r
, struct fw3_limit
*limit
)
977 char buf
[sizeof("-4294967296/second\0")];
979 if (!limit
|| limit
->rate
<= 0)
982 fw3_ipt_rule_addarg(r
, false, "-m", "limit");
984 sprintf(buf
, "%u/%s", limit
->rate
, fw3_limit_units
[limit
->unit
]);
985 fw3_ipt_rule_addarg(r
, limit
->invert
, "--limit", buf
);
987 if (limit
->burst
> 0)
989 sprintf(buf
, "%u", limit
->burst
);
990 fw3_ipt_rule_addarg(r
, limit
->invert
, "--limit-burst", buf
);
995 fw3_ipt_rule_ipset(struct fw3_ipt_rule
*r
, struct fw3_setmatch
*match
)
997 char buf
[sizeof("dst,dst,dst\0")];
1001 struct fw3_ipset
*set
;
1002 struct fw3_ipset_datatype
*type
;
1004 if (!match
|| !match
->set
|| !match
->ptr
)
1008 list_for_each_entry(type
, &set
->datatypes
, list
)
1016 p
+= sprintf(p
, "%s", match
->dir
[i
] ? match
->dir
[i
] : type
->dir
);
1020 fw3_ipt_rule_addarg(r
, false, "-m", "set");
1022 fw3_ipt_rule_addarg(r
, match
->invert
, "--match-set",
1023 set
->external
? set
->external
: set
->name
);
1025 fw3_ipt_rule_addarg(r
, false, buf
, NULL
);
1029 fw3_ipt_rule_helper(struct fw3_ipt_rule
*r
, struct fw3_cthelpermatch
*match
)
1031 if (!match
|| !match
->set
|| !match
->ptr
)
1034 fw3_ipt_rule_addarg(r
, false, "-m", "helper");
1035 fw3_ipt_rule_addarg(r
, match
->invert
, "--helper", match
->ptr
->name
);
1039 fw3_ipt_rule_time(struct fw3_ipt_rule
*r
, struct fw3_time
*time
)
1042 struct tm empty
= { 0 };
1044 char buf
[84]; /* sizeof("1,2,3,...,30,31\0") */
1047 bool d1
= memcmp(&time
->datestart
, &empty
, sizeof(empty
));
1048 bool d2
= memcmp(&time
->datestop
, &empty
, sizeof(empty
));
1050 if (!d1
&& !d2
&& !time
->timestart
&& !time
->timestop
&&
1051 !(time
->monthdays
& 0xFFFFFFFE) && !(time
->weekdays
& 0xFE))
1056 fw3_ipt_rule_addarg(r
, false, "-m", "time");
1059 fw3_ipt_rule_addarg(r
, false, "--kerneltz", NULL
);
1063 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestart
);
1064 fw3_ipt_rule_addarg(r
, false, "--datestart", buf
);
1069 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestop
);
1070 fw3_ipt_rule_addarg(r
, false, "--datestop", buf
);
1073 if (time
->timestart
)
1075 sprintf(buf
, "%02d:%02d:%02d",
1076 time
->timestart
/ 3600,
1077 time
->timestart
% 3600 / 60,
1078 time
->timestart
% 60);
1080 fw3_ipt_rule_addarg(r
, false, "--timestart", buf
);
1085 sprintf(buf
, "%02d:%02d:%02d",
1086 time
->timestop
/ 3600,
1087 time
->timestop
% 3600 / 60,
1088 time
->timestop
% 60);
1090 fw3_ipt_rule_addarg(r
, false, "--timestop", buf
);
1093 if (time
->monthdays
& 0xFFFFFFFE)
1095 for (i
= 1, p
= buf
; i
< 32; i
++)
1097 if (fw3_hasbit(time
->monthdays
, i
))
1102 p
+= sprintf(p
, "%u", i
);
1106 fw3_ipt_rule_addarg(r
, fw3_hasbit(time
->monthdays
, 0), "--monthdays", buf
);
1109 if (time
->weekdays
& 0xFE)
1111 for (i
= 1, p
= buf
; i
< 8; i
++)
1113 if (fw3_hasbit(time
->weekdays
, i
))
1118 p
+= sprintf(p
, "%u", i
);
1122 fw3_ipt_rule_addarg(r
, fw3_hasbit(time
->weekdays
, 0), "--weekdays", buf
);
1127 fw3_ipt_rule_mark(struct fw3_ipt_rule
*r
, struct fw3_mark
*mark
)
1129 char buf
[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
1131 if (!mark
|| !mark
->set
)
1134 if (mark
->mask
< 0xFFFFFFFF)
1135 sprintf(buf
, "0x%x/0x%x", mark
->mark
, mark
->mask
);
1137 sprintf(buf
, "0x%x", mark
->mark
);
1139 fw3_ipt_rule_addarg(r
, false, "-m", "mark");
1140 fw3_ipt_rule_addarg(r
, mark
->invert
, "--mark", buf
);
1144 fw3_ipt_rule_comment(struct fw3_ipt_rule
*r
, const char *fmt
, ...)
1153 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
1156 fw3_ipt_rule_addarg(r
, false, "-m", "comment");
1157 fw3_ipt_rule_addarg(r
, false, "--comment", buf
);
1161 fw3_ipt_rule_extra(struct fw3_ipt_rule
*r
, const char *extra
)
1165 if (!extra
|| !*extra
)
1168 s
= fw3_strdup(extra
);
1170 for (p
= strtok(s
, " \t"); p
; p
= strtok(NULL
, " \t"))
1172 tmp
= realloc(r
->argv
, (r
->argc
+ 1) * sizeof(*r
->argv
));
1178 r
->argv
[r
->argc
++] = fw3_strdup(p
);
1184 #ifndef DISABLE_IPV6
1186 rule_print6(struct ip6t_entry
*e
)
1188 char buf1
[INET6_ADDRSTRLEN
], buf2
[INET6_ADDRSTRLEN
];
1191 if (e
->ipv6
.flags
& IP6T_F_PROTO
)
1193 if (e
->ipv6
.invflags
& XT_INV_PROTO
)
1196 pname
= get_protoname(container_of(e
, struct fw3_ipt_rule
, e6
));
1199 printf(" -p %s", pname
);
1201 printf(" -p %u", e
->ipv6
.proto
);
1204 if (e
->ipv6
.iniface
[0])
1206 if (e
->ipv6
.invflags
& IP6T_INV_VIA_IN
)
1209 printf(" -i %s", e
->ipv6
.iniface
);
1212 if (e
->ipv6
.outiface
[0])
1214 if (e
->ipv6
.invflags
& IP6T_INV_VIA_OUT
)
1217 printf(" -o %s", e
->ipv6
.outiface
);
1220 if (memcmp(&e
->ipv6
.src
, &in6addr_any
, sizeof(struct in6_addr
)))
1222 if (e
->ipv6
.invflags
& IP6T_INV_SRCIP
)
1226 inet_ntop(AF_INET6
, &e
->ipv6
.src
, buf1
, sizeof(buf1
)),
1227 inet_ntop(AF_INET6
, &e
->ipv6
.smsk
, buf2
, sizeof(buf2
)));
1230 if (memcmp(&e
->ipv6
.dst
, &in6addr_any
, sizeof(struct in6_addr
)))
1232 if (e
->ipv6
.invflags
& IP6T_INV_DSTIP
)
1236 inet_ntop(AF_INET6
, &e
->ipv6
.dst
, buf1
, sizeof(buf1
)),
1237 inet_ntop(AF_INET6
, &e
->ipv6
.dmsk
, buf2
, sizeof(buf2
)));
1243 rule_print4(struct ipt_entry
*e
)
1245 struct in_addr in_zero
= { 0 };
1246 char buf1
[sizeof("255.255.255.255\0")], buf2
[sizeof("255.255.255.255\0")];
1251 if (e
->ip
.invflags
& XT_INV_PROTO
)
1254 pname
= get_protoname(container_of(e
, struct fw3_ipt_rule
, e
));
1257 printf(" -p %s", pname
);
1259 printf(" -p %u", e
->ip
.proto
);
1262 if (e
->ip
.iniface
[0])
1264 if (e
->ip
.invflags
& IPT_INV_VIA_IN
)
1267 printf(" -i %s", e
->ip
.iniface
);
1270 if (e
->ip
.outiface
[0])
1272 if (e
->ip
.invflags
& IPT_INV_VIA_OUT
)
1275 printf(" -o %s", e
->ip
.outiface
);
1278 if (memcmp(&e
->ip
.src
, &in_zero
, sizeof(struct in_addr
)))
1280 if (e
->ip
.invflags
& IPT_INV_SRCIP
)
1284 inet_ntop(AF_INET
, &e
->ip
.src
, buf1
, sizeof(buf1
)),
1285 inet_ntop(AF_INET
, &e
->ip
.smsk
, buf2
, sizeof(buf2
)));
1288 if (memcmp(&e
->ip
.dst
, &in_zero
, sizeof(struct in_addr
)))
1290 if (e
->ip
.invflags
& IPT_INV_DSTIP
)
1294 inet_ntop(AF_INET
, &e
->ip
.dst
, buf1
, sizeof(buf1
)),
1295 inet_ntop(AF_INET
, &e
->ip
.dmsk
, buf2
, sizeof(buf2
)));
1300 rule_print(struct fw3_ipt_rule
*r
, const char *prefix
, const char *chain
)
1302 debug(r
->h
, "%s %s", prefix
, chain
);
1304 #ifndef DISABLE_IPV6
1305 if (r
->h
->family
== FW3_FAMILY_V6
)
1306 rule_print6(&r
->e6
);
1311 fw3_xt_print_matches(&r
->e
.ip
, r
->matches
);
1312 fw3_xt_print_target(&r
->e
.ip
, r
->target
);
1318 parse_option(struct fw3_ipt_rule
*r
, int optc
, bool inv
)
1320 struct xtables_rule_match
*m
;
1321 struct xtables_match
*em
;
1323 /* is a target option */
1324 if (r
->target
&& fw3_xt_has_target_parse(r
->target
) &&
1325 optc
>= r
->target
->option_offset
&&
1326 optc
< (r
->target
->option_offset
+ 256))
1328 xtables_option_tpcall(optc
, r
->argv
, inv
, r
->target
, &r
->e
);
1332 /* try to dispatch argument to one of the match parsers */
1333 for (m
= r
->matches
; m
; m
= m
->next
)
1337 if (m
->completed
|| !fw3_xt_has_match_parse(em
))
1340 if (optc
< em
->option_offset
||
1341 optc
>= (em
->option_offset
+ 256))
1344 xtables_option_mpcall(optc
, r
->argv
, inv
, em
, &r
->e
);
1348 /* unhandled option, might belong to a protocol match */
1349 if ((em
= load_protomatch(r
)) != NULL
)
1351 init_match(r
, em
, false);
1353 r
->protocol_loaded
= true;
1360 warn("parse_option(): option '%s' needs argument", r
->argv
[optind
-1]);
1363 warn("parse_option(): unknown option '%s'", r
->argv
[optind
-1]);
1369 fw3_ipt_rule_addarg(struct fw3_ipt_rule
*r
, bool inv
,
1370 const char *k
, const char *v
)
1378 n
= inv
+ !!k
+ !!v
;
1379 tmp
= realloc(r
->argv
, (r
->argc
+ n
) * sizeof(*tmp
));
1387 r
->argv
[r
->argc
++] = fw3_strdup("!");
1389 r
->argv
[r
->argc
++] = fw3_strdup(k
);
1392 r
->argv
[r
->argc
++] = fw3_strdup(v
);
1395 static unsigned char *
1396 rule_mask(struct fw3_ipt_rule
*r
)
1399 unsigned char *p
, *mask
= NULL
;
1400 struct xtables_rule_match
*m
;
1402 #define SZ(x) XT_ALIGN(sizeof(struct x))
1404 #ifndef DISABLE_IPV6
1405 if (r
->h
->family
== FW3_FAMILY_V6
)
1409 for (m
= r
->matches
; m
; m
= m
->next
)
1410 s
+= SZ(ip6t_entry_match
) + m
->match
->size
;
1412 s
+= SZ(ip6t_entry_target
);
1414 s
+= r
->target
->size
;
1416 mask
= fw3_alloc(s
);
1417 memset(mask
, 0xFF, SZ(ip6t_entry
));
1418 p
= mask
+ SZ(ip6t_entry
);
1420 for (m
= r
->matches
; m
; m
= m
->next
)
1422 memset(p
, 0xFF, SZ(ip6t_entry_match
) + m
->match
->userspacesize
);
1423 p
+= SZ(ip6t_entry_match
) + m
->match
->size
;
1426 memset(p
, 0xFF, SZ(ip6t_entry_target
) + (r
->target
? r
->target
->userspacesize
: 0));
1433 for (m
= r
->matches
; m
; m
= m
->next
)
1434 s
+= SZ(ipt_entry_match
) + m
->match
->size
;
1436 s
+= SZ(ipt_entry_target
);
1438 s
+= r
->target
->size
;
1440 mask
= fw3_alloc(s
);
1441 memset(mask
, 0xFF, SZ(ipt_entry
));
1442 p
= mask
+ SZ(ipt_entry
);
1444 for (m
= r
->matches
; m
; m
= m
->next
)
1446 memset(p
, 0xFF, SZ(ipt_entry_match
) + m
->match
->userspacesize
);
1447 p
+= SZ(ipt_entry_match
) + m
->match
->size
;
1450 memset(p
, 0xFF, SZ(ipt_entry_target
) + (r
->target
? r
->target
->userspacesize
: 0));
1457 rule_build(struct fw3_ipt_rule
*r
)
1459 size_t s
, target_size
= (r
->target
) ? r
->target
->t
->u
.target_size
: 0;
1460 struct xtables_rule_match
*m
;
1462 #ifndef DISABLE_IPV6
1463 if (r
->h
->family
== FW3_FAMILY_V6
)
1465 struct ip6t_entry
*e6
;
1467 s
= XT_ALIGN(sizeof(struct ip6t_entry
));
1469 for (m
= r
->matches
; m
; m
= m
->next
)
1470 s
+= m
->match
->m
->u
.match_size
;
1472 e6
= fw3_alloc(s
+ target_size
);
1474 memcpy(e6
, &r
->e6
, sizeof(struct ip6t_entry
));
1476 e6
->target_offset
= s
;
1477 e6
->next_offset
= s
+ target_size
;
1481 for (m
= r
->matches
; m
; m
= m
->next
)
1483 memcpy(e6
->elems
+ s
, m
->match
->m
, m
->match
->m
->u
.match_size
);
1484 s
+= m
->match
->m
->u
.match_size
;
1488 memcpy(e6
->elems
+ s
, r
->target
->t
, target_size
);
1495 struct ipt_entry
*e
;
1497 s
= XT_ALIGN(sizeof(struct ipt_entry
));
1499 for (m
= r
->matches
; m
; m
= m
->next
)
1500 s
+= m
->match
->m
->u
.match_size
;
1502 e
= fw3_alloc(s
+ target_size
);
1504 memcpy(e
, &r
->e
, sizeof(struct ipt_entry
));
1506 e
->target_offset
= s
;
1507 e
->next_offset
= s
+ target_size
;
1511 for (m
= r
->matches
; m
; m
= m
->next
)
1513 memcpy(e
->elems
+ s
, m
->match
->m
, m
->match
->m
->u
.match_size
);
1514 s
+= m
->match
->m
->u
.match_size
;
1518 memcpy(e
->elems
+ s
, r
->target
->t
, target_size
);
1525 set_rule_tag(struct fw3_ipt_rule
*r
)
1529 const char *tag
= "!fw3";
1531 for (i
= 0; i
< r
->argc
; i
++)
1532 if (!strcmp(r
->argv
[i
], "--comment") && (i
+ 1) < r
->argc
)
1533 if (asprintf(&p
, "%s: %s", tag
, r
->argv
[i
+ 1]) > 0)
1535 free(r
->argv
[i
+ 1]);
1540 tmp
= realloc(r
->argv
, (r
->argc
+ 4) * sizeof(*r
->argv
));
1545 r
->argv
[r
->argc
++] = fw3_strdup("-m");
1546 r
->argv
[r
->argc
++] = fw3_strdup("comment");
1547 r
->argv
[r
->argc
++] = fw3_strdup("--comment");
1548 r
->argv
[r
->argc
++] = fw3_strdup(tag
);
1553 __fw3_ipt_rule_append(struct fw3_ipt_rule
*r
, bool repl
, const char *fmt
, ...)
1556 unsigned char *mask
;
1558 struct xtables_rule_match
*m
;
1559 struct xtables_match
*em
;
1560 struct xtables_target
*et
;
1561 struct xtables_globals
*g
;
1563 struct fw3_device dev
;
1564 struct fw3_address addr
;
1566 enum xtables_exittype status
;
1574 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
1577 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
1578 g
->opts
= g
->orig_opts
;
1583 status
= setjmp(fw3_ipt_error_jmp
);
1587 info(" ! Skipping due to previous exception (code %u)", status
);
1593 while ((optc
= getopt_long(r
->argc
, r
->argv
, "-:m:j:i:o:s:d:", g
->opts
,
1599 em
= find_match(r
, optarg
);
1603 warn("fw3_ipt_rule_append(): Can't find match '%s'", optarg
);
1607 init_match(r
, em
, true);
1611 et
= get_target(r
, optarg
);
1615 warn("fw3_ipt_rule_append(): Can't find target '%s'", optarg
);
1623 if (!fw3_parse_device(&dev
, optarg
, false) ||
1624 dev
.any
|| dev
.invert
|| *dev
.network
)
1626 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg
);
1631 fw3_ipt_rule_in_out(r
, (optc
== 'i') ? &dev
: NULL
,
1632 (optc
== 'o') ? &dev
: NULL
);
1637 if (!fw3_parse_address(&addr
, optarg
, false) ||
1638 addr
.range
|| addr
.invert
)
1640 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg
);
1645 fw3_ipt_rule_src_dest(r
, (optc
== 's') ? &addr
: NULL
,
1646 (optc
== 'd') ? &addr
: NULL
);
1650 if ((optarg
[0] == '!') && (optarg
[1] == '\0'))
1657 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg
);
1661 if (parse_option(r
, optc
, inv
))
1669 for (m
= r
->matches
; m
; m
= m
->next
)
1670 xtables_option_mfcall(m
->match
);
1673 xtables_option_tfcall(r
->target
);
1675 rule
= rule_build(r
);
1677 #ifndef DISABLE_IPV6
1678 if (r
->h
->family
== FW3_FAMILY_V6
)
1682 mask
= rule_mask(r
);
1684 while (ip6tc_delete_entry(buf
, rule
, mask
, r
->h
->handle
))
1686 rule_print(r
, "-D", buf
);
1692 rule_print(r
, "-A", buf
);
1694 if (!ip6tc_append_entry(buf
, rule
, r
->h
->handle
))
1695 warn("ip6tc_append_entry(): %s", ip6tc_strerror(errno
));
1702 mask
= rule_mask(r
);
1704 while (iptc_delete_entry(buf
, rule
, mask
, r
->h
->handle
))
1706 rule_print(r
, "-D", buf
);
1712 rule_print(r
, "-A", buf
);
1714 if (!iptc_append_entry(buf
, rule
, r
->h
->handle
))
1715 warn("iptc_append_entry(): %s\n", iptc_strerror(errno
));
1721 for (i
= 1; i
< r
->argc
; i
++)
1726 xtables_rule_matches_free(&r
->matches
);
1733 /* reset all targets and matches */
1734 for (em
= xtables_matches
; em
; em
= em
->next
)
1737 for (et
= xtables_targets
; et
; et
= et
->next
)
1743 xtables_free_opts(1);
1746 struct fw3_ipt_rule
*
1747 fw3_ipt_rule_create(struct fw3_ipt_handle
*handle
, struct fw3_protocol
*proto
,
1748 struct fw3_device
*in
, struct fw3_device
*out
,
1749 struct fw3_address
*src
, struct fw3_address
*dest
)
1751 struct fw3_ipt_rule
*r
;
1753 r
= fw3_ipt_rule_new(handle
);
1755 fw3_ipt_rule_proto(r
, proto
);
1756 fw3_ipt_rule_in_out(r
, in
, out
);
1757 fw3_ipt_rule_src_dest(r
, src
, dest
);
1763 xtables_register_match(struct xtables_match
*me
)
1766 static struct xtables_match
**tmp
;
1768 if (!xext
.register_match
)
1769 xext
.register_match
= dlsym(RTLD_NEXT
, "xtables_register_match");
1771 if (!xext
.register_match
)
1774 xext
.register_match(me
);
1778 for (i
= 0; i
< xext
.mcount
; i
++)
1779 if (xext
.matches
[i
] == me
)
1782 tmp
= realloc(xext
.matches
, sizeof(me
) * (xext
.mcount
+ 1));
1788 xext
.matches
[xext
.mcount
++] = me
;
1793 xtables_register_target(struct xtables_target
*me
)
1796 static struct xtables_target
**tmp
;
1798 if (!xext
.register_target
)
1799 xext
.register_target
= dlsym(RTLD_NEXT
, "xtables_register_target");
1801 if (!xext
.register_target
)
1804 xext
.register_target(me
);
1808 for (i
= 0; i
< xext
.tcount
; i
++)
1809 if (xext
.targets
[i
] == me
)
1812 tmp
= realloc(xext
.targets
, sizeof(me
) * (xext
.tcount
+ 1));
1818 xext
.targets
[xext
.tcount
++] = me
;