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 is_referenced(struct fw3_ipt_handle
*h
, const char *target
)
335 const struct ipt_entry
*e
;
340 if (h
->family
== FW3_FAMILY_V6
)
342 for (chain
= ip6tc_first_chain(h
->handle
);
344 chain
= ip6tc_next_chain(h
->handle
))
346 const struct ip6t_entry
*e6
;
347 for (e6
= ip6tc_first_rule(chain
, h
->handle
);
349 e6
= ip6tc_next_rule(e6
, h
->handle
))
351 t
= ip6tc_get_target(e6
, h
->handle
);
353 if (*t
&& !strcmp(t
, target
))
361 for (chain
= iptc_first_chain(h
->handle
);
363 chain
= iptc_next_chain(h
->handle
))
365 for (e
= iptc_first_rule(chain
, h
->handle
);
367 e
= iptc_next_rule(e
, h
->handle
))
369 t
= iptc_get_target(e
, h
->handle
);
371 if (*t
&& !strcmp(t
, target
))
381 fw3_ipt_delete_chain(struct fw3_ipt_handle
*h
, bool if_unused
,
384 if (if_unused
&& is_referenced(h
, chain
))
387 delete_rules(h
, chain
);
390 debug(h
, "-X %s\n", chain
);
393 if (h
->family
== FW3_FAMILY_V6
)
394 ip6tc_delete_chain(chain
, h
->handle
);
397 iptc_delete_chain(chain
, h
->handle
);
401 has_rule_tag(const void *base
, unsigned int start
, unsigned int end
)
404 const struct xt_entry_match
*em
;
406 for (i
= start
; i
< end
; i
+= em
->u
.match_size
)
410 if (strcmp(em
->u
.user
.name
, "comment"))
413 if (!memcmp(em
->data
, "!fw3", 4))
421 fw3_ipt_delete_id_rules(struct fw3_ipt_handle
*h
, const char *chain
)
424 const struct ipt_entry
*e
;
428 if (h
->family
== FW3_FAMILY_V6
)
430 if (!ip6tc_is_chain(chain
, h
->handle
))
436 const struct ip6t_entry
*e6
;
437 for (num
= 0, e6
= ip6tc_first_rule(chain
, h
->handle
);
439 num
++, e6
= ip6tc_next_rule(e6
, h
->handle
))
441 if (has_rule_tag(e6
, sizeof(*e6
), e6
->target_offset
))
444 debug(h
, "-D %s %u\n", chain
, num
+ 1);
446 ip6tc_delete_num_entry(chain
, num
, h
->handle
);
456 if (!iptc_is_chain(chain
, h
->handle
))
462 for (num
= 0, e
= iptc_first_rule(chain
, h
->handle
);
464 num
++, e
= iptc_next_rule(e
, h
->handle
))
466 if (has_rule_tag(e
, sizeof(*e
), e
->target_offset
))
469 debug(h
, "-D %s %u\n", chain
, num
+ 1);
471 iptc_delete_num_entry(chain
, num
, h
->handle
);
482 is_chain(struct fw3_ipt_handle
*h
, const char *name
)
485 if (h
->family
== FW3_FAMILY_V6
)
486 return ip6tc_is_chain(name
, h
->handle
);
489 return iptc_is_chain(name
, h
->handle
);
493 fw3_ipt_create_chain(struct fw3_ipt_handle
*h
, bool ignore_existing
,
494 const char *fmt
, ...)
500 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
503 if (ignore_existing
&& is_chain(h
, buf
))
507 debug(h
, "-N %s\n", buf
);
509 iptc_create_chain(buf
, h
->handle
);
513 fw3_ipt_flush(struct fw3_ipt_handle
*h
)
518 if (h
->family
== FW3_FAMILY_V6
)
520 for (chain
= ip6tc_first_chain(h
->handle
);
522 chain
= ip6tc_next_chain(h
->handle
))
524 ip6tc_flush_entries(chain
, h
->handle
);
527 for (chain
= ip6tc_first_chain(h
->handle
);
529 chain
= ip6tc_next_chain(h
->handle
))
531 ip6tc_delete_chain(chain
, h
->handle
);
537 for (chain
= iptc_first_chain(h
->handle
);
539 chain
= iptc_next_chain(h
->handle
))
541 iptc_flush_entries(chain
, h
->handle
);
544 for (chain
= iptc_first_chain(h
->handle
);
546 chain
= iptc_next_chain(h
->handle
))
548 iptc_delete_chain(chain
, h
->handle
);
554 chain_is_empty(struct fw3_ipt_handle
*h
, const char *chain
)
557 if (h
->family
== FW3_FAMILY_V6
)
558 return (!ip6tc_builtin(chain
, h
->handle
) &&
559 !ip6tc_first_rule(chain
, h
->handle
));
562 return (!iptc_builtin(chain
, h
->handle
) &&
563 !iptc_first_rule(chain
, h
->handle
));
567 fw3_ipt_gc(struct fw3_ipt_handle
*h
)
573 if (h
->family
== FW3_FAMILY_V6
)
578 for (chain
= ip6tc_first_chain(h
->handle
);
580 chain
= ip6tc_next_chain(h
->handle
))
582 if (!chain_is_empty(h
, chain
))
585 fw3_ipt_delete_chain(h
, false, chain
);
597 for (chain
= iptc_first_chain(h
->handle
);
599 chain
= iptc_next_chain(h
->handle
))
601 warn("C=%s\n", chain
);
603 if (!chain_is_empty(h
, chain
))
606 warn("D=%s\n", chain
);
608 fw3_ipt_delete_chain(h
, false, chain
);
617 fw3_ipt_commit(struct fw3_ipt_handle
*h
)
622 if (h
->family
== FW3_FAMILY_V6
)
624 rv
= ip6tc_commit(h
->handle
);
626 warn("ip6tc_commit(): %s", ip6tc_strerror(errno
));
631 rv
= iptc_commit(h
->handle
);
633 warn("iptc_commit(): %s", iptc_strerror(errno
));
638 fw3_ipt_close(struct fw3_ipt_handle
*h
)
640 fw3_unlock_path(&xt_lock_fd
, XT_LOCK_NAME
);
644 struct fw3_ipt_rule
*
645 fw3_ipt_rule_new(struct fw3_ipt_handle
*h
)
647 struct fw3_ipt_rule
*r
;
649 r
= fw3_alloc(sizeof(*r
));
652 r
->argv
= fw3_alloc(sizeof(char *));
653 r
->argv
[r
->argc
++] = "fw3";
660 get_protoname(struct fw3_ipt_rule
*r
)
662 const struct xtables_pprot
*pp
;
665 for (pp
= xtables_chain_protos
; pp
->name
; pp
++)
666 if (pp
->num
== r
->protocol
)
667 return (char *)pp
->name
;
672 static struct xtables_match
*
673 find_match(struct fw3_ipt_rule
*r
, const char *name
)
675 struct xtables_match
*m
;
678 m
= xtables_find_match(name
, XTF_TRY_LOAD
, &r
->matches
);
685 init_match(struct fw3_ipt_rule
*r
, struct xtables_match
*m
, bool no_clone
)
688 struct xtables_globals
*g
;
693 s
= XT_ALIGN(sizeof(struct xt_entry_match
)) + m
->size
;
697 fw3_xt_set_match_name(m
);
699 m
->m
->u
.user
.revision
= m
->revision
;
700 m
->m
->u
.match_size
= s
;
702 /* free previous userspace data */
703 fw3_xt_free_match_udata(m
);
708 /* don't merge options if no_clone is set and this match is a clone */
709 if (no_clone
&& (m
== m
->next
))
712 /* merge option table */
713 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
714 fw3_xt_merge_match_options(g
, m
);
718 need_protomatch(struct fw3_ipt_rule
*r
, const char *pname
)
723 if (!xtables_find_match(pname
, XTF_DONT_LOAD
, NULL
))
726 return !r
->protocol_loaded
;
729 static struct xtables_match
*
730 load_protomatch(struct fw3_ipt_rule
*r
)
732 const char *pname
= get_protoname(r
);
734 if (!need_protomatch(r
, pname
))
737 return find_match(r
, pname
);
740 static struct xtables_target
*
741 find_target(struct fw3_ipt_rule
*r
, const char *name
)
743 struct xtables_target
*t
;
747 if (is_chain(r
->h
, name
))
748 t
= xtables_find_target(XT_STANDARD_TARGET
, XTF_TRY_LOAD
);
750 t
= xtables_find_target(name
, XTF_TRY_LOAD
);
757 static struct xtables_target
*
758 get_target(struct fw3_ipt_rule
*r
, const char *name
)
761 struct xtables_target
*t
;
762 struct xtables_globals
*g
;
764 t
= find_target(r
, name
);
769 s
= XT_ALIGN(sizeof(struct xt_entry_target
)) + t
->size
;
772 fw3_xt_set_target_name(t
, name
);
774 t
->t
->u
.user
.revision
= t
->revision
;
775 t
->t
->u
.target_size
= s
;
777 /* free previous userspace data */
778 fw3_xt_free_target_udata(t
);
783 /* merge option table */
784 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
785 fw3_xt_merge_target_options(g
, t
);
793 fw3_ipt_rule_proto(struct fw3_ipt_rule
*r
, struct fw3_protocol
*proto
)
797 if (!proto
|| proto
->any
)
800 pr
= proto
->protocol
;
803 if (r
->h
->family
== FW3_FAMILY_V6
)
808 r
->e6
.ipv6
.proto
= pr
;
809 r
->e6
.ipv6
.flags
|= IP6T_F_PROTO
;
812 r
->e6
.ipv6
.invflags
|= XT_INV_PROTO
;
820 r
->e
.ip
.invflags
|= XT_INV_PROTO
;
827 fw3_ipt_rule_in_out(struct fw3_ipt_rule
*r
,
828 struct fw3_device
*in
, struct fw3_device
*out
)
831 if (r
->h
->family
== FW3_FAMILY_V6
)
835 xtables_parse_interface(in
->name
, r
->e6
.ipv6
.iniface
,
836 r
->e6
.ipv6
.iniface_mask
);
839 r
->e6
.ipv6
.invflags
|= IP6T_INV_VIA_IN
;
842 if (out
&& !out
->any
)
844 xtables_parse_interface(out
->name
, r
->e6
.ipv6
.outiface
,
845 r
->e6
.ipv6
.outiface_mask
);
848 r
->e6
.ipv6
.invflags
|= IP6T_INV_VIA_OUT
;
856 xtables_parse_interface(in
->name
, r
->e
.ip
.iniface
,
857 r
->e
.ip
.iniface_mask
);
860 r
->e
.ip
.invflags
|= IPT_INV_VIA_IN
;
863 if (out
&& !out
->any
)
865 xtables_parse_interface(out
->name
, r
->e
.ip
.outiface
,
866 r
->e
.ip
.outiface_mask
);
869 r
->e
.ip
.invflags
|= IPT_INV_VIA_OUT
;
876 fw3_ipt_rule_src_dest(struct fw3_ipt_rule
*r
,
877 struct fw3_address
*src
, struct fw3_address
*dest
)
879 if ((src
&& src
->range
) || (dest
&& dest
->range
))
881 fw3_ipt_rule_addarg(r
, false, "-m", "iprange");
888 fw3_ipt_rule_addarg(r
, src
->invert
, "--src-range",
889 fw3_address_to_string(src
, false, false));
892 else if (r
->h
->family
== FW3_FAMILY_V6
)
894 r
->e6
.ipv6
.src
= src
->address
.v6
;
895 r
->e6
.ipv6
.smsk
= src
->mask
.v6
;
898 for (i
= 0; i
< 4; i
++)
899 r
->e6
.ipv6
.src
.s6_addr32
[i
] &= r
->e6
.ipv6
.smsk
.s6_addr32
[i
];
902 r
->e6
.ipv6
.invflags
|= IP6T_INV_SRCIP
;
907 r
->e
.ip
.src
= src
->address
.v4
;
908 r
->e
.ip
.smsk
= src
->mask
.v4
;
910 r
->e
.ip
.src
.s_addr
&= r
->e
.ip
.smsk
.s_addr
;
913 r
->e
.ip
.invflags
|= IPT_INV_SRCIP
;
917 if (dest
&& dest
->set
)
921 fw3_ipt_rule_addarg(r
, dest
->invert
, "--dst-range",
922 fw3_address_to_string(dest
, false, false));
925 else if (r
->h
->family
== FW3_FAMILY_V6
)
927 r
->e6
.ipv6
.dst
= dest
->address
.v6
;
928 r
->e6
.ipv6
.dmsk
= dest
->mask
.v6
;
931 for (i
= 0; i
< 4; i
++)
932 r
->e6
.ipv6
.dst
.s6_addr32
[i
] &= r
->e6
.ipv6
.dmsk
.s6_addr32
[i
];
935 r
->e6
.ipv6
.invflags
|= IP6T_INV_DSTIP
;
940 r
->e
.ip
.dst
= dest
->address
.v4
;
941 r
->e
.ip
.dmsk
= dest
->mask
.v4
;
943 r
->e
.ip
.dst
.s_addr
&= r
->e
.ip
.dmsk
.s_addr
;
946 r
->e
.ip
.invflags
|= IPT_INV_DSTIP
;
952 fw3_ipt_rule_sport_dport(struct fw3_ipt_rule
*r
,
953 struct fw3_port
*sp
, struct fw3_port
*dp
)
955 char buf
[sizeof("65535:65535\0")];
957 if ((!sp
|| !sp
->set
) && (!dp
|| !dp
->set
))
960 if (!get_protoname(r
))
965 if (sp
->port_min
== sp
->port_max
)
966 sprintf(buf
, "%u", sp
->port_min
);
968 snprintf(buf
, sizeof(buf
), "%u:%u", sp
->port_min
, sp
->port_max
);
970 fw3_ipt_rule_addarg(r
, sp
->invert
, "--sport", buf
);
975 if (dp
->port_min
== dp
->port_max
)
976 sprintf(buf
, "%u", dp
->port_min
);
978 snprintf(buf
, sizeof(buf
), "%u:%u", dp
->port_min
, dp
->port_max
);
980 fw3_ipt_rule_addarg(r
, dp
->invert
, "--dport", buf
);
985 fw3_ipt_rule_device(struct fw3_ipt_rule
*r
, const char *device
, bool out
)
988 struct fw3_device dev
= { .any
= false };
989 strncpy(dev
.name
, device
, sizeof(dev
.name
) - 1);
990 fw3_ipt_rule_in_out(r
, (out
) ? NULL
: &dev
, (out
) ? &dev
: NULL
);
995 fw3_ipt_rule_mac(struct fw3_ipt_rule
*r
, struct fw3_mac
*mac
)
997 char buf
[sizeof("ff:ff:ff:ff:ff:ff\0")];
998 uint8_t *addr
= mac
->mac
.ether_addr_octet
;
1003 sprintf(buf
, "%02x:%02x:%02x:%02x:%02x:%02x",
1004 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
1006 fw3_ipt_rule_addarg(r
, false, "-m", "mac");
1007 fw3_ipt_rule_addarg(r
, mac
->invert
, "--mac-source", buf
);
1011 fw3_ipt_rule_icmptype(struct fw3_ipt_rule
*r
, struct fw3_icmptype
*icmp
)
1013 char buf
[sizeof("255/255\0")];
1018 #ifndef DISABLE_IPV6
1019 if (r
->h
->family
== FW3_FAMILY_V6
)
1021 if (icmp
->code6_min
== 0 && icmp
->code6_max
== 0xFF)
1022 sprintf(buf
, "%u", icmp
->type6
);
1024 snprintf(buf
, sizeof(buf
), "%u/%u", icmp
->type6
, icmp
->code6_min
);
1026 fw3_ipt_rule_addarg(r
, icmp
->invert
, "--icmpv6-type", buf
);
1031 if (icmp
->code_min
== 0 && icmp
->code_max
== 0xFF)
1032 sprintf(buf
, "%u", icmp
->type
);
1034 snprintf(buf
, sizeof(buf
), "%u/%u", icmp
->type
, icmp
->code_min
);
1036 fw3_ipt_rule_addarg(r
, icmp
->invert
, "--icmp-type", buf
);
1041 fw3_ipt_rule_limit(struct fw3_ipt_rule
*r
, struct fw3_limit
*limit
)
1043 char buf
[sizeof("-4294967296/second\0")];
1045 if (!limit
|| limit
->rate
<= 0)
1048 fw3_ipt_rule_addarg(r
, false, "-m", "limit");
1050 sprintf(buf
, "%u/%s", limit
->rate
, fw3_limit_units
[limit
->unit
]);
1051 fw3_ipt_rule_addarg(r
, limit
->invert
, "--limit", buf
);
1053 if (limit
->burst
> 0)
1055 sprintf(buf
, "%u", limit
->burst
);
1056 fw3_ipt_rule_addarg(r
, limit
->invert
, "--limit-burst", buf
);
1061 fw3_ipt_rule_ipset(struct fw3_ipt_rule
*r
, struct fw3_setmatch
*match
)
1063 char buf
[sizeof("dst,dst,dst\0")];
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
)
1082 p
+= sprintf(p
, "%s", match
->dir
[i
] ? match
->dir
[i
] : type
->dir
);
1086 fw3_ipt_rule_addarg(r
, false, "-m", "set");
1088 fw3_ipt_rule_addarg(r
, match
->invert
, "--match-set",
1089 set
->external
? set
->external
: set
->name
);
1091 fw3_ipt_rule_addarg(r
, false, buf
, NULL
);
1095 fw3_ipt_rule_helper(struct fw3_ipt_rule
*r
, struct fw3_cthelpermatch
*match
)
1097 if (!match
|| !match
->set
|| !match
->ptr
)
1100 fw3_ipt_rule_addarg(r
, false, "-m", "helper");
1101 fw3_ipt_rule_addarg(r
, match
->invert
, "--helper", match
->ptr
->name
);
1105 fw3_ipt_rule_time(struct fw3_ipt_rule
*r
, struct fw3_time
*time
)
1108 struct tm empty
= { 0 };
1110 char buf
[84]; /* sizeof("1,2,3,...,30,31\0") */
1113 bool d1
= memcmp(&time
->datestart
, &empty
, sizeof(empty
));
1114 bool d2
= memcmp(&time
->datestop
, &empty
, sizeof(empty
));
1116 if (!d1
&& !d2
&& !time
->timestart
&& !time
->timestop
&&
1117 !(time
->monthdays
& 0xFFFFFFFE) && !(time
->weekdays
& 0xFE))
1122 fw3_ipt_rule_addarg(r
, false, "-m", "time");
1125 fw3_ipt_rule_addarg(r
, false, "--kerneltz", NULL
);
1129 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestart
);
1130 fw3_ipt_rule_addarg(r
, false, "--datestart", buf
);
1135 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestop
);
1136 fw3_ipt_rule_addarg(r
, false, "--datestop", buf
);
1139 if (time
->timestart
)
1141 sprintf(buf
, "%02d:%02d:%02d",
1142 time
->timestart
/ 3600,
1143 time
->timestart
% 3600 / 60,
1144 time
->timestart
% 60);
1146 fw3_ipt_rule_addarg(r
, false, "--timestart", buf
);
1151 sprintf(buf
, "%02d:%02d:%02d",
1152 time
->timestop
/ 3600,
1153 time
->timestop
% 3600 / 60,
1154 time
->timestop
% 60);
1156 fw3_ipt_rule_addarg(r
, false, "--timestop", buf
);
1159 if (time
->monthdays
& 0xFFFFFFFE)
1161 for (i
= 1, p
= buf
; i
< 32; i
++)
1163 if (fw3_hasbit(time
->monthdays
, i
))
1168 p
+= sprintf(p
, "%u", i
);
1172 fw3_ipt_rule_addarg(r
, fw3_hasbit(time
->monthdays
, 0), "--monthdays", buf
);
1175 if (time
->weekdays
& 0xFE)
1177 for (i
= 1, p
= buf
; i
< 8; i
++)
1179 if (fw3_hasbit(time
->weekdays
, i
))
1184 p
+= sprintf(p
, "%u", i
);
1188 fw3_ipt_rule_addarg(r
, fw3_hasbit(time
->weekdays
, 0), "--weekdays", buf
);
1193 fw3_ipt_rule_mark(struct fw3_ipt_rule
*r
, struct fw3_mark
*mark
)
1195 char buf
[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
1197 if (!mark
|| !mark
->set
)
1200 if (mark
->mask
< 0xFFFFFFFF)
1201 sprintf(buf
, "0x%x/0x%x", mark
->mark
, mark
->mask
);
1203 sprintf(buf
, "0x%x", mark
->mark
);
1205 fw3_ipt_rule_addarg(r
, false, "-m", "mark");
1206 fw3_ipt_rule_addarg(r
, mark
->invert
, "--mark", buf
);
1210 fw3_ipt_rule_dscp(struct fw3_ipt_rule
*r
, struct fw3_dscp
*dscp
)
1212 char buf
[sizeof("0xFF\0")];
1214 if (!dscp
|| !dscp
->set
)
1217 sprintf(buf
, "0x%x", dscp
->dscp
);
1219 fw3_ipt_rule_addarg(r
, false, "-m", "dscp");
1220 fw3_ipt_rule_addarg(r
, dscp
->invert
, "--dscp", buf
);
1224 fw3_ipt_rule_comment(struct fw3_ipt_rule
*r
, const char *fmt
, ...)
1233 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
1236 fw3_ipt_rule_addarg(r
, false, "-m", "comment");
1237 fw3_ipt_rule_addarg(r
, false, "--comment", buf
);
1241 fw3_ipt_rule_extra(struct fw3_ipt_rule
*r
, const char *extra
)
1245 if (!extra
|| !*extra
)
1248 s
= fw3_strdup(extra
);
1250 for (p
= strtok(s
, " \t"); p
; p
= strtok(NULL
, " \t"))
1252 tmp
= realloc(r
->argv
, (r
->argc
+ 1) * sizeof(*r
->argv
));
1258 r
->argv
[r
->argc
++] = fw3_strdup(p
);
1264 #ifndef DISABLE_IPV6
1266 rule_print6(struct ip6t_entry
*e
)
1268 char buf1
[INET6_ADDRSTRLEN
], buf2
[INET6_ADDRSTRLEN
];
1271 if (e
->ipv6
.flags
& IP6T_F_PROTO
)
1273 if (e
->ipv6
.invflags
& XT_INV_PROTO
)
1276 pname
= get_protoname(container_of(e
, struct fw3_ipt_rule
, e6
));
1279 printf(" -p %s", pname
);
1281 printf(" -p %u", e
->ipv6
.proto
);
1284 if (e
->ipv6
.iniface
[0])
1286 if (e
->ipv6
.invflags
& IP6T_INV_VIA_IN
)
1289 printf(" -i %s", e
->ipv6
.iniface
);
1292 if (e
->ipv6
.outiface
[0])
1294 if (e
->ipv6
.invflags
& IP6T_INV_VIA_OUT
)
1297 printf(" -o %s", e
->ipv6
.outiface
);
1300 if (memcmp(&e
->ipv6
.src
, &in6addr_any
, sizeof(struct in6_addr
)))
1302 if (e
->ipv6
.invflags
& IP6T_INV_SRCIP
)
1306 inet_ntop(AF_INET6
, &e
->ipv6
.src
, buf1
, sizeof(buf1
)),
1307 inet_ntop(AF_INET6
, &e
->ipv6
.smsk
, buf2
, sizeof(buf2
)));
1310 if (memcmp(&e
->ipv6
.dst
, &in6addr_any
, sizeof(struct in6_addr
)))
1312 if (e
->ipv6
.invflags
& IP6T_INV_DSTIP
)
1316 inet_ntop(AF_INET6
, &e
->ipv6
.dst
, buf1
, sizeof(buf1
)),
1317 inet_ntop(AF_INET6
, &e
->ipv6
.dmsk
, buf2
, sizeof(buf2
)));
1323 rule_print4(struct ipt_entry
*e
)
1325 struct in_addr in_zero
= { 0 };
1326 char buf1
[sizeof("255.255.255.255\0")], buf2
[sizeof("255.255.255.255\0")];
1331 if (e
->ip
.invflags
& XT_INV_PROTO
)
1334 pname
= get_protoname(container_of(e
, struct fw3_ipt_rule
, e
));
1337 printf(" -p %s", pname
);
1339 printf(" -p %u", e
->ip
.proto
);
1342 if (e
->ip
.iniface
[0])
1344 if (e
->ip
.invflags
& IPT_INV_VIA_IN
)
1347 printf(" -i %s", e
->ip
.iniface
);
1350 if (e
->ip
.outiface
[0])
1352 if (e
->ip
.invflags
& IPT_INV_VIA_OUT
)
1355 printf(" -o %s", e
->ip
.outiface
);
1358 if (memcmp(&e
->ip
.src
, &in_zero
, sizeof(struct in_addr
)))
1360 if (e
->ip
.invflags
& IPT_INV_SRCIP
)
1364 inet_ntop(AF_INET
, &e
->ip
.src
, buf1
, sizeof(buf1
)),
1365 inet_ntop(AF_INET
, &e
->ip
.smsk
, buf2
, sizeof(buf2
)));
1368 if (memcmp(&e
->ip
.dst
, &in_zero
, sizeof(struct in_addr
)))
1370 if (e
->ip
.invflags
& IPT_INV_DSTIP
)
1374 inet_ntop(AF_INET
, &e
->ip
.dst
, buf1
, sizeof(buf1
)),
1375 inet_ntop(AF_INET
, &e
->ip
.dmsk
, buf2
, sizeof(buf2
)));
1380 rule_print(struct fw3_ipt_rule
*r
, const char *prefix
, const char *chain
)
1382 debug(r
->h
, "%s %s", prefix
, chain
);
1384 #ifndef DISABLE_IPV6
1385 if (r
->h
->family
== FW3_FAMILY_V6
)
1386 rule_print6(&r
->e6
);
1391 fw3_xt_print_matches(&r
->e
.ip
, r
->matches
);
1392 fw3_xt_print_target(&r
->e
.ip
, r
->target
);
1398 parse_option(struct fw3_ipt_rule
*r
, int optc
, bool inv
)
1400 struct xtables_rule_match
*m
;
1401 struct xtables_match
*em
;
1403 /* is a target option */
1404 if (r
->target
&& fw3_xt_has_target_parse(r
->target
) &&
1405 optc
>= r
->target
->option_offset
&&
1406 optc
< (r
->target
->option_offset
+ 256))
1408 xtables_option_tpcall(optc
, r
->argv
, inv
, r
->target
, &r
->e
);
1412 /* try to dispatch argument to one of the match parsers */
1413 for (m
= r
->matches
; m
; m
= m
->next
)
1417 if (m
->completed
|| !fw3_xt_has_match_parse(em
))
1420 if (optc
< em
->option_offset
||
1421 optc
>= (em
->option_offset
+ 256))
1424 xtables_option_mpcall(optc
, r
->argv
, inv
, em
, &r
->e
);
1428 /* unhandled option, might belong to a protocol match */
1429 if ((em
= load_protomatch(r
)) != NULL
)
1431 init_match(r
, em
, false);
1433 r
->protocol_loaded
= true;
1440 warn("parse_option(): option '%s' needs argument", r
->argv
[optind
-1]);
1443 warn("parse_option(): unknown option '%s'", r
->argv
[optind
-1]);
1449 fw3_ipt_rule_addarg(struct fw3_ipt_rule
*r
, bool inv
,
1450 const char *k
, const char *v
)
1458 n
= inv
+ !!k
+ !!v
;
1459 tmp
= realloc(r
->argv
, (r
->argc
+ n
) * sizeof(*tmp
));
1467 r
->argv
[r
->argc
++] = fw3_strdup("!");
1469 r
->argv
[r
->argc
++] = fw3_strdup(k
);
1472 r
->argv
[r
->argc
++] = fw3_strdup(v
);
1475 static unsigned char *
1476 rule_mask(struct fw3_ipt_rule
*r
)
1479 unsigned char *p
, *mask
= NULL
;
1480 struct xtables_rule_match
*m
;
1482 #define SZ(x) XT_ALIGN(sizeof(struct x))
1484 #ifndef DISABLE_IPV6
1485 if (r
->h
->family
== FW3_FAMILY_V6
)
1489 for (m
= r
->matches
; m
; m
= m
->next
)
1490 s
+= SZ(ip6t_entry_match
) + m
->match
->size
;
1492 s
+= SZ(ip6t_entry_target
);
1494 s
+= r
->target
->size
;
1496 mask
= fw3_alloc(s
);
1497 memset(mask
, 0xFF, SZ(ip6t_entry
));
1498 p
= mask
+ SZ(ip6t_entry
);
1500 for (m
= r
->matches
; m
; m
= m
->next
)
1502 memset(p
, 0xFF, SZ(ip6t_entry_match
) + m
->match
->userspacesize
);
1503 p
+= SZ(ip6t_entry_match
) + m
->match
->size
;
1506 memset(p
, 0xFF, SZ(ip6t_entry_target
) + (r
->target
? r
->target
->userspacesize
: 0));
1513 for (m
= r
->matches
; m
; m
= m
->next
)
1514 s
+= SZ(ipt_entry_match
) + m
->match
->size
;
1516 s
+= SZ(ipt_entry_target
);
1518 s
+= r
->target
->size
;
1520 mask
= fw3_alloc(s
);
1521 memset(mask
, 0xFF, SZ(ipt_entry
));
1522 p
= mask
+ SZ(ipt_entry
);
1524 for (m
= r
->matches
; m
; m
= m
->next
)
1526 memset(p
, 0xFF, SZ(ipt_entry_match
) + m
->match
->userspacesize
);
1527 p
+= SZ(ipt_entry_match
) + m
->match
->size
;
1530 memset(p
, 0xFF, SZ(ipt_entry_target
) + (r
->target
? r
->target
->userspacesize
: 0));
1537 rule_build(struct fw3_ipt_rule
*r
)
1539 size_t s
, target_size
= (r
->target
) ? r
->target
->t
->u
.target_size
: 0;
1540 struct xtables_rule_match
*m
;
1542 #ifndef DISABLE_IPV6
1543 if (r
->h
->family
== FW3_FAMILY_V6
)
1545 struct ip6t_entry
*e6
;
1547 s
= XT_ALIGN(sizeof(struct ip6t_entry
));
1549 for (m
= r
->matches
; m
; m
= m
->next
)
1550 s
+= m
->match
->m
->u
.match_size
;
1552 e6
= fw3_alloc(s
+ target_size
);
1554 memcpy(e6
, &r
->e6
, sizeof(struct ip6t_entry
));
1556 e6
->target_offset
= s
;
1557 e6
->next_offset
= s
+ target_size
;
1561 for (m
= r
->matches
; m
; m
= m
->next
)
1563 memcpy(e6
->elems
+ s
, m
->match
->m
, m
->match
->m
->u
.match_size
);
1564 s
+= m
->match
->m
->u
.match_size
;
1568 memcpy(e6
->elems
+ s
, r
->target
->t
, target_size
);
1575 struct ipt_entry
*e
;
1577 s
= XT_ALIGN(sizeof(struct ipt_entry
));
1579 for (m
= r
->matches
; m
; m
= m
->next
)
1580 s
+= m
->match
->m
->u
.match_size
;
1582 e
= fw3_alloc(s
+ target_size
);
1584 memcpy(e
, &r
->e
, sizeof(struct ipt_entry
));
1586 e
->target_offset
= s
;
1587 e
->next_offset
= s
+ target_size
;
1591 for (m
= r
->matches
; m
; m
= m
->next
)
1593 memcpy(e
->elems
+ s
, m
->match
->m
, m
->match
->m
->u
.match_size
);
1594 s
+= m
->match
->m
->u
.match_size
;
1598 memcpy(e
->elems
+ s
, r
->target
->t
, target_size
);
1605 set_rule_tag(struct fw3_ipt_rule
*r
)
1609 const char *tag
= "!fw3";
1611 for (i
= 0; i
< r
->argc
; i
++)
1612 if (!strcmp(r
->argv
[i
], "--comment") && (i
+ 1) < r
->argc
)
1613 if (asprintf(&p
, "%s: %s", tag
, r
->argv
[i
+ 1]) > 0)
1615 free(r
->argv
[i
+ 1]);
1620 tmp
= realloc(r
->argv
, (r
->argc
+ 4) * sizeof(*r
->argv
));
1625 r
->argv
[r
->argc
++] = fw3_strdup("-m");
1626 r
->argv
[r
->argc
++] = fw3_strdup("comment");
1627 r
->argv
[r
->argc
++] = fw3_strdup("--comment");
1628 r
->argv
[r
->argc
++] = fw3_strdup(tag
);
1633 __fw3_ipt_rule_append(struct fw3_ipt_rule
*r
, bool repl
, const char *fmt
, ...)
1636 unsigned char *mask
;
1638 struct xtables_rule_match
*m
;
1639 struct xtables_match
*em
;
1640 struct xtables_target
*et
;
1641 struct xtables_globals
*g
;
1643 struct fw3_device dev
;
1644 struct fw3_address addr
;
1646 enum xtables_exittype status
;
1654 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
1657 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
1658 g
->opts
= g
->orig_opts
;
1663 status
= setjmp(fw3_ipt_error_jmp
);
1667 info(" ! Skipping due to previous exception (code %u)", status
);
1673 while ((optc
= getopt_long(r
->argc
, r
->argv
, "-:m:j:i:o:s:d:", g
->opts
,
1679 em
= find_match(r
, optarg
);
1683 warn("fw3_ipt_rule_append(): Can't find match '%s'", optarg
);
1687 init_match(r
, em
, true);
1691 et
= get_target(r
, optarg
);
1695 warn("fw3_ipt_rule_append(): Can't find target '%s'", optarg
);
1703 if (!fw3_parse_device(&dev
, optarg
, false) ||
1704 dev
.any
|| dev
.invert
|| *dev
.network
)
1706 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg
);
1711 fw3_ipt_rule_in_out(r
, (optc
== 'i') ? &dev
: NULL
,
1712 (optc
== 'o') ? &dev
: NULL
);
1717 if (!fw3_parse_address(&addr
, optarg
, false) ||
1718 addr
.range
|| addr
.invert
)
1720 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg
);
1725 fw3_ipt_rule_src_dest(r
, (optc
== 's') ? &addr
: NULL
,
1726 (optc
== 'd') ? &addr
: NULL
);
1730 if ((optarg
[0] == '!') && (optarg
[1] == '\0'))
1737 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg
);
1741 if (parse_option(r
, optc
, inv
))
1749 for (m
= r
->matches
; m
; m
= m
->next
)
1750 xtables_option_mfcall(m
->match
);
1753 xtables_option_tfcall(r
->target
);
1755 rule
= rule_build(r
);
1757 #ifndef DISABLE_IPV6
1758 if (r
->h
->family
== FW3_FAMILY_V6
)
1762 mask
= rule_mask(r
);
1764 while (ip6tc_delete_entry(buf
, rule
, mask
, r
->h
->handle
))
1766 rule_print(r
, "-D", buf
);
1772 rule_print(r
, "-A", buf
);
1774 if (!ip6tc_append_entry(buf
, rule
, r
->h
->handle
))
1775 warn("ip6tc_append_entry(): %s", ip6tc_strerror(errno
));
1782 mask
= rule_mask(r
);
1784 while (iptc_delete_entry(buf
, rule
, mask
, r
->h
->handle
))
1786 rule_print(r
, "-D", buf
);
1792 rule_print(r
, "-A", buf
);
1794 if (!iptc_append_entry(buf
, rule
, r
->h
->handle
))
1795 warn("iptc_append_entry(): %s\n", iptc_strerror(errno
));
1801 for (i
= 1; i
< r
->argc
; i
++)
1806 xtables_rule_matches_free(&r
->matches
);
1813 /* reset all targets and matches */
1814 for (em
= xtables_matches
; em
; em
= em
->next
)
1817 for (et
= xtables_targets
; et
; et
= et
->next
)
1823 xtables_free_opts(1);
1826 struct fw3_ipt_rule
*
1827 fw3_ipt_rule_create(struct fw3_ipt_handle
*handle
, struct fw3_protocol
*proto
,
1828 struct fw3_device
*in
, struct fw3_device
*out
,
1829 struct fw3_address
*src
, struct fw3_address
*dest
)
1831 struct fw3_ipt_rule
*r
;
1833 r
= fw3_ipt_rule_new(handle
);
1835 fw3_ipt_rule_proto(r
, proto
);
1836 fw3_ipt_rule_in_out(r
, in
, out
);
1837 fw3_ipt_rule_src_dest(r
, src
, dest
);
1843 xtables_register_match(struct xtables_match
*me
)
1846 static struct xtables_match
**tmp
;
1848 if (!xext
.register_match
)
1849 xext
.register_match
= dlsym(RTLD_NEXT
, "xtables_register_match");
1851 if (!xext
.register_match
)
1854 xext
.register_match(me
);
1858 for (i
= 0; i
< xext
.mcount
; i
++)
1859 if (xext
.matches
[i
] == me
)
1862 tmp
= realloc(xext
.matches
, sizeof(me
) * (xext
.mcount
+ 1));
1868 xext
.matches
[xext
.mcount
++] = me
;
1873 xtables_register_target(struct xtables_target
*me
)
1876 static struct xtables_target
**tmp
;
1878 if (!xext
.register_target
)
1879 xext
.register_target
= dlsym(RTLD_NEXT
, "xtables_register_target");
1881 if (!xext
.register_target
)
1884 xext
.register_target(me
);
1888 for (i
= 0; i
< xext
.tcount
; i
++)
1889 if (xext
.targets
[i
] == me
)
1892 tmp
= realloc(xext
.targets
, sizeof(me
) * (xext
.tcount
+ 1));
1898 xext
.targets
[xext
.tcount
++] = me
;