a27da142acaa11640ebc5b7111628c1640681d83
2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
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.
22 static struct option base_opts
[] = {
23 { .name
= "match", .has_arg
= 1, .val
= 'm' },
24 { .name
= "jump", .has_arg
= 1, .val
= 'j' },
28 static struct xtables_globals xtg
= {
30 .program_version
= "4",
31 .orig_opts
= base_opts
,
34 static struct xtables_globals xtg6
= {
36 .program_version
= "6",
37 .orig_opts
= base_opts
,
40 /* Required by certain extensions like SNAT and DNAT */
41 int kernel_version
= 0;
44 get_kernel_version(void)
46 static struct utsname uts
;
47 int x
= 0, y
= 0, z
= 0;
49 if (uname(&uts
) == -1)
50 sprintf(uts
.release
, "3.0.0");
52 sscanf(uts
.release
, "%d.%d.%d", &x
, &y
, &z
);
53 kernel_version
= 0x10000 * x
+ 0x100 * y
+ z
;
56 static void fw3_init_extensions(void)
66 struct fw3_ipt_handle
*
67 fw3_ipt_open(enum fw3_family family
, enum fw3_table table
)
69 struct fw3_ipt_handle
*h
;
71 h
= fw3_alloc(sizeof(*h
));
75 if (family
== FW3_FAMILY_V6
)
78 h
->family
= FW3_FAMILY_V6
;
80 h
->handle
= ip6tc_init(fw3_flag_names
[table
]);
82 xtables_set_params(&xtg6
);
83 xtables_set_nfproto(NFPROTO_IPV6
);
88 h
->family
= FW3_FAMILY_V4
;
90 h
->handle
= iptc_init(fw3_flag_names
[table
]);
92 xtables_set_params(&xtg
);
93 xtables_set_nfproto(NFPROTO_IPV4
);
103 fw3_init_extensions();
109 debug(struct fw3_ipt_handle
*h
, const char *fmt
, ...)
113 printf("%s -t %s ", (h
->family
== FW3_FAMILY_V6
) ? "ip6tables" : "iptables",
114 fw3_flag_names
[h
->table
]);
122 fw3_ipt_set_policy(struct fw3_ipt_handle
*h
, const char *chain
,
123 enum fw3_flag policy
)
126 debug(h
, "-P %s %s\n", chain
, fw3_flag_names
[policy
]);
129 if (h
->family
== FW3_FAMILY_V6
)
130 ip6tc_set_policy(chain
, fw3_flag_names
[policy
], NULL
, h
->handle
);
133 iptc_set_policy(chain
, fw3_flag_names
[policy
], NULL
, h
->handle
);
137 fw3_ipt_flush_chain(struct fw3_ipt_handle
*h
, const char *chain
)
140 debug(h
, "-F %s\n", chain
);
143 if (h
->family
== FW3_FAMILY_V6
)
144 ip6tc_flush_entries(chain
, h
->handle
);
147 iptc_flush_entries(chain
, h
->handle
);
151 delete_rules(struct fw3_ipt_handle
*h
, const char *target
)
154 const struct ipt_entry
*e
;
160 if (h
->family
== FW3_FAMILY_V6
)
162 for (chain
= ip6tc_first_chain(h
->handle
);
164 chain
= ip6tc_next_chain(h
->handle
))
169 const struct ip6t_entry
*e6
;
170 for (num
= 0, e6
= ip6tc_first_rule(chain
, h
->handle
);
172 num
++, e6
= ip6tc_next_rule(e6
, h
->handle
))
174 t
= ip6tc_get_target(e6
, h
->handle
);
176 if (*t
&& !strcmp(t
, target
))
179 debug(h
, "-D %s %u\n", chain
, num
+ 1);
181 ip6tc_delete_num_entry(chain
, num
, h
->handle
);
192 for (chain
= iptc_first_chain(h
->handle
);
194 chain
= iptc_next_chain(h
->handle
))
199 for (num
= 0, e
= iptc_first_rule(chain
, h
->handle
);
201 num
++, e
= iptc_next_rule(e
, h
->handle
))
203 t
= iptc_get_target(e
, h
->handle
);
205 if (*t
&& !strcmp(t
, target
))
208 debug(h
, "-D %s %u\n", chain
, num
+ 1);
210 iptc_delete_num_entry(chain
, num
, h
->handle
);
221 fw3_ipt_delete_chain(struct fw3_ipt_handle
*h
, const char *chain
)
223 delete_rules(h
, chain
);
226 debug(h
, "-X %s\n", chain
);
229 if (h
->family
== FW3_FAMILY_V6
)
230 ip6tc_delete_chain(chain
, h
->handle
);
233 iptc_delete_chain(chain
, h
->handle
);
237 get_rule_id(const void *base
, unsigned int start
, unsigned int end
)
241 const struct xt_entry_match
*em
;
243 for (i
= start
; i
< end
; i
+= em
->u
.match_size
)
247 if (strcmp(em
->u
.user
.name
, "id"))
250 memcpy(&id
, em
->data
, sizeof(id
));
252 if ((id
& FW3_ID_MASK
) != FW3_ID_MAGIC
)
255 return (id
& ~FW3_ID_MASK
);
262 fw3_ipt_delete_id_rules(struct fw3_ipt_handle
*h
, const char *chain
)
265 const struct ipt_entry
*e
;
270 if (h
->family
== FW3_FAMILY_V6
)
272 if (!ip6tc_is_chain(chain
, h
->handle
))
278 const struct ip6t_entry
*e6
;
279 for (num
= 0, e6
= ip6tc_first_rule(chain
, h
->handle
);
281 num
++, e6
= ip6tc_next_rule(e6
, h
->handle
))
283 id
= get_rule_id(e6
, sizeof(*e6
), e6
->target_offset
);
288 debug(h
, "-D %s %u\n", chain
, num
+ 1);
290 ip6tc_delete_num_entry(chain
, num
, h
->handle
);
300 if (!iptc_is_chain(chain
, h
->handle
))
306 for (num
= 0, e
= iptc_first_rule(chain
, h
->handle
);
308 num
++, e
= iptc_next_rule(e
, h
->handle
))
310 id
= get_rule_id(e
, sizeof(*e
), e
->target_offset
);
315 debug(h
, "-D %s %u\n", chain
, num
+ 1);
317 iptc_delete_num_entry(chain
, num
, h
->handle
);
327 fw3_ipt_create_chain(struct fw3_ipt_handle
*h
, const char *fmt
, ...)
333 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
337 debug(h
, "-N %s\n", buf
);
339 iptc_create_chain(buf
, h
->handle
);
343 fw3_ipt_flush(struct fw3_ipt_handle
*h
)
348 if (h
->family
== FW3_FAMILY_V6
)
350 for (chain
= ip6tc_first_chain(h
->handle
);
352 chain
= ip6tc_next_chain(h
->handle
))
354 ip6tc_flush_entries(chain
, h
->handle
);
357 for (chain
= ip6tc_first_chain(h
->handle
);
359 chain
= ip6tc_next_chain(h
->handle
))
361 ip6tc_delete_chain(chain
, h
->handle
);
367 for (chain
= iptc_first_chain(h
->handle
);
369 chain
= iptc_next_chain(h
->handle
))
371 iptc_flush_entries(chain
, h
->handle
);
374 for (chain
= iptc_first_chain(h
->handle
);
376 chain
= iptc_next_chain(h
->handle
))
378 iptc_delete_chain(chain
, h
->handle
);
384 chain_is_empty(struct fw3_ipt_handle
*h
, const char *chain
)
387 if (h
->family
== FW3_FAMILY_V6
)
388 return (!ip6tc_builtin(chain
, h
->handle
) &&
389 !ip6tc_first_rule(chain
, h
->handle
));
392 return (!iptc_builtin(chain
, h
->handle
) &&
393 !iptc_first_rule(chain
, h
->handle
));
397 fw3_ipt_gc(struct fw3_ipt_handle
*h
)
403 if (h
->family
== FW3_FAMILY_V6
)
408 for (chain
= ip6tc_first_chain(h
->handle
);
410 chain
= ip6tc_next_chain(h
->handle
))
412 if (!chain_is_empty(h
, chain
))
415 fw3_ipt_delete_chain(h
, chain
);
427 for (chain
= iptc_first_chain(h
->handle
);
429 chain
= iptc_next_chain(h
->handle
))
431 warn("C=%s\n", chain
);
433 if (!chain_is_empty(h
, chain
))
436 warn("D=%s\n", chain
);
438 fw3_ipt_delete_chain(h
, chain
);
447 fw3_ipt_commit(struct fw3_ipt_handle
*h
)
452 if (h
->family
== FW3_FAMILY_V6
)
454 rv
= ip6tc_commit(h
->handle
);
456 warn("ip6tc_commit(): %s", ip6tc_strerror(errno
));
461 rv
= iptc_commit(h
->handle
);
463 warn("iptc_commit(): %s", iptc_strerror(errno
));
468 fw3_ipt_close(struct fw3_ipt_handle
*h
)
475 dlclose(h
->libv
[h
->libc
]);
484 struct fw3_ipt_rule
*
485 fw3_ipt_rule_new(struct fw3_ipt_handle
*h
)
487 struct fw3_ipt_rule
*r
;
489 r
= fw3_alloc(sizeof(*r
));
493 r
->argv
= fw3_alloc(sizeof(char *));
494 r
->argv
[r
->argc
++] = "fw3";
501 is_chain(struct fw3_ipt_handle
*h
, const char *name
)
504 if (h
->family
== FW3_FAMILY_V6
)
505 return ip6tc_is_chain(name
, h
->handle
);
508 return iptc_is_chain(name
, h
->handle
);
512 get_protoname(struct fw3_ipt_rule
*r
)
514 const struct xtables_pprot
*pp
;
517 for (pp
= xtables_chain_protos
; pp
->name
; pp
++)
518 if (pp
->num
== r
->protocol
)
519 return (char *)pp
->name
;
525 load_extension(struct fw3_ipt_handle
*h
, const char *name
)
529 const char *pfx
= (h
->family
== FW3_FAMILY_V6
) ? "libip6t" : "libipt";
531 snprintf(path
, sizeof(path
), "/usr/lib/iptables/libxt_%s.so", name
);
532 if (!(lib
= dlopen(path
, RTLD_NOW
)))
534 snprintf(path
, sizeof(path
), "/usr/lib/iptables/%s_%s.so", pfx
, name
);
535 lib
= dlopen(path
, RTLD_NOW
);
541 tmp
= realloc(h
->libv
, sizeof(lib
) * (h
->libc
+ 1));
547 h
->libv
[h
->libc
++] = lib
;
552 static struct xtables_match
*
553 find_match(struct fw3_ipt_rule
*r
, const char *name
)
555 struct xtables_match
*m
;
557 m
= xtables_find_match(name
, XTF_DONT_LOAD
, &r
->matches
);
559 if (!m
&& load_extension(r
->h
, name
))
560 m
= xtables_find_match(name
, XTF_DONT_LOAD
, &r
->matches
);
566 init_match(struct fw3_ipt_rule
*r
, struct xtables_match
*m
, bool no_clone
)
569 struct xtables_globals
*g
;
574 s
= XT_ALIGN(sizeof(struct xt_entry_match
)) + m
->size
;
578 fw3_xt_set_match_name(m
);
580 m
->m
->u
.user
.revision
= m
->revision
;
581 m
->m
->u
.match_size
= s
;
583 /* free previous userspace data */
584 fw3_xt_free_match_udata(m
);
589 /* don't merge options if no_clone is set and this match is a clone */
590 if (no_clone
&& (m
== m
->next
))
593 /* merge option table */
594 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
595 fw3_xt_merge_match_options(g
, m
);
599 need_protomatch(struct fw3_ipt_rule
*r
, const char *pname
)
604 if (!xtables_find_match(pname
, XTF_DONT_LOAD
, NULL
))
607 return !r
->protocol_loaded
;
610 static struct xtables_match
*
611 load_protomatch(struct fw3_ipt_rule
*r
)
613 const char *pname
= get_protoname(r
);
615 if (!need_protomatch(r
, pname
))
618 return find_match(r
, pname
);
621 static struct xtables_target
*
622 find_target(struct fw3_ipt_rule
*r
, const char *name
)
624 struct xtables_target
*t
;
626 if (is_chain(r
->h
, name
))
627 return xtables_find_target(XT_STANDARD_TARGET
, XTF_LOAD_MUST_SUCCEED
);
629 t
= xtables_find_target(name
, XTF_DONT_LOAD
);
631 if (!t
&& load_extension(r
->h
, name
))
632 t
= xtables_find_target(name
, XTF_DONT_LOAD
);
637 static struct xtables_target
*
638 get_target(struct fw3_ipt_rule
*r
, const char *name
)
641 struct xtables_target
*t
;
642 struct xtables_globals
*g
;
644 t
= find_target(r
, name
);
649 s
= XT_ALIGN(sizeof(struct xt_entry_target
)) + t
->size
;
652 fw3_xt_set_target_name(t
, name
);
654 t
->t
->u
.user
.revision
= t
->revision
;
655 t
->t
->u
.target_size
= s
;
657 /* free previous userspace data */
658 fw3_xt_free_target_udata(t
);
663 /* merge option table */
664 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
665 fw3_xt_merge_target_options(g
, t
);
673 fw3_ipt_rule_proto(struct fw3_ipt_rule
*r
, struct fw3_protocol
*proto
)
677 if (!proto
|| proto
->any
)
680 pr
= proto
->protocol
;
683 if (r
->h
->family
== FW3_FAMILY_V6
)
688 r
->e6
.ipv6
.proto
= pr
;
689 r
->e6
.ipv6
.flags
|= IP6T_F_PROTO
;
692 r
->e6
.ipv6
.invflags
|= XT_INV_PROTO
;
700 r
->e
.ip
.invflags
|= XT_INV_PROTO
;
707 fw3_ipt_rule_in_out(struct fw3_ipt_rule
*r
,
708 struct fw3_device
*in
, struct fw3_device
*out
)
711 if (r
->h
->family
== FW3_FAMILY_V6
)
715 xtables_parse_interface(in
->name
, r
->e6
.ipv6
.iniface
,
716 r
->e6
.ipv6
.iniface_mask
);
719 r
->e6
.ipv6
.invflags
|= IP6T_INV_VIA_IN
;
722 if (out
&& !out
->any
)
724 xtables_parse_interface(out
->name
, r
->e6
.ipv6
.outiface
,
725 r
->e6
.ipv6
.outiface_mask
);
728 r
->e6
.ipv6
.invflags
|= IP6T_INV_VIA_OUT
;
736 xtables_parse_interface(in
->name
, r
->e
.ip
.iniface
,
737 r
->e
.ip
.iniface_mask
);
740 r
->e
.ip
.invflags
|= IPT_INV_VIA_IN
;
743 if (out
&& !out
->any
)
745 xtables_parse_interface(out
->name
, r
->e
.ip
.outiface
,
746 r
->e
.ip
.outiface_mask
);
749 r
->e
.ip
.invflags
|= IPT_INV_VIA_OUT
;
756 fw3_ipt_rule_src_dest(struct fw3_ipt_rule
*r
,
757 struct fw3_address
*src
, struct fw3_address
*dest
)
759 if ((src
&& src
->range
) || (dest
&& dest
->range
))
761 fw3_ipt_rule_addarg(r
, false, "-m", "iprange");
768 fw3_ipt_rule_addarg(r
, src
->invert
, "--src-range",
769 fw3_address_to_string(src
, false, false));
772 else if (r
->h
->family
== FW3_FAMILY_V6
)
774 r
->e6
.ipv6
.src
= src
->address
.v6
;
775 r
->e6
.ipv6
.smsk
= src
->mask
.v6
;
778 for (i
= 0; i
< 4; i
++)
779 r
->e6
.ipv6
.src
.s6_addr32
[i
] &= r
->e6
.ipv6
.smsk
.s6_addr32
[i
];
782 r
->e6
.ipv6
.invflags
|= IP6T_INV_SRCIP
;
787 r
->e
.ip
.src
= src
->address
.v4
;
788 r
->e
.ip
.smsk
= src
->mask
.v4
;
790 r
->e
.ip
.src
.s_addr
&= r
->e
.ip
.smsk
.s_addr
;
793 r
->e
.ip
.invflags
|= IPT_INV_SRCIP
;
797 if (dest
&& dest
->set
)
801 fw3_ipt_rule_addarg(r
, dest
->invert
, "--dst-range",
802 fw3_address_to_string(dest
, false, false));
805 else if (r
->h
->family
== FW3_FAMILY_V6
)
807 r
->e6
.ipv6
.dst
= dest
->address
.v6
;
808 r
->e6
.ipv6
.dmsk
= dest
->mask
.v6
;
811 for (i
= 0; i
< 4; i
++)
812 r
->e6
.ipv6
.dst
.s6_addr32
[i
] &= r
->e6
.ipv6
.dmsk
.s6_addr32
[i
];
815 r
->e6
.ipv6
.invflags
|= IP6T_INV_DSTIP
;
820 r
->e
.ip
.dst
= dest
->address
.v4
;
821 r
->e
.ip
.dmsk
= dest
->mask
.v4
;
823 r
->e
.ip
.dst
.s_addr
&= r
->e
.ip
.dmsk
.s_addr
;
826 r
->e
.ip
.invflags
|= IPT_INV_DSTIP
;
832 fw3_ipt_rule_sport_dport(struct fw3_ipt_rule
*r
,
833 struct fw3_port
*sp
, struct fw3_port
*dp
)
835 char buf
[sizeof("65535:65535\0")];
837 if ((!sp
|| !sp
->set
) && (!dp
|| !dp
->set
))
840 if (!get_protoname(r
))
845 if (sp
->port_min
== sp
->port_max
)
846 sprintf(buf
, "%u", sp
->port_min
);
848 sprintf(buf
, "%u:%u", sp
->port_min
, sp
->port_max
);
850 fw3_ipt_rule_addarg(r
, sp
->invert
, "--sport", buf
);
855 if (dp
->port_min
== dp
->port_max
)
856 sprintf(buf
, "%u", dp
->port_min
);
858 sprintf(buf
, "%u:%u", dp
->port_min
, dp
->port_max
);
860 fw3_ipt_rule_addarg(r
, dp
->invert
, "--dport", buf
);
865 fw3_ipt_rule_device(struct fw3_ipt_rule
*r
, const char *device
, bool out
)
868 struct fw3_device dev
= { .any
= false };
869 strncpy(dev
.name
, device
, sizeof(dev
.name
) - 1);
870 fw3_ipt_rule_in_out(r
, (out
) ? NULL
: &dev
, (out
) ? &dev
: NULL
);
875 fw3_ipt_rule_mac(struct fw3_ipt_rule
*r
, struct fw3_mac
*mac
)
877 char buf
[sizeof("ff:ff:ff:ff:ff:ff\0")];
878 uint8_t *addr
= mac
->mac
.ether_addr_octet
;
883 sprintf(buf
, "%02x:%02x:%02x:%02x:%02x:%02x",
884 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
886 fw3_ipt_rule_addarg(r
, false, "-m", "mac");
887 fw3_ipt_rule_addarg(r
, mac
->invert
, "--mac-source", buf
);
891 fw3_ipt_rule_icmptype(struct fw3_ipt_rule
*r
, struct fw3_icmptype
*icmp
)
893 char buf
[sizeof("255/255\0")];
899 if (r
->h
->family
== FW3_FAMILY_V6
)
901 if (icmp
->code6_min
== 0 && icmp
->code6_max
== 0xFF)
902 sprintf(buf
, "%u", icmp
->type6
);
904 sprintf(buf
, "%u/%u", icmp
->type6
, icmp
->code6_min
);
906 fw3_ipt_rule_addarg(r
, icmp
->invert
, "--icmpv6-type", buf
);
911 if (icmp
->code_min
== 0 && icmp
->code_max
== 0xFF)
912 sprintf(buf
, "%u", icmp
->type
);
914 sprintf(buf
, "%u/%u", icmp
->type
, icmp
->code_min
);
916 fw3_ipt_rule_addarg(r
, icmp
->invert
, "--icmp-type", buf
);
921 fw3_ipt_rule_limit(struct fw3_ipt_rule
*r
, struct fw3_limit
*limit
)
923 char buf
[sizeof("-4294967296/second\0")];
925 if (!limit
|| limit
->rate
<= 0)
928 fw3_ipt_rule_addarg(r
, false, "-m", "limit");
930 sprintf(buf
, "%u/%s", limit
->rate
, fw3_limit_units
[limit
->unit
]);
931 fw3_ipt_rule_addarg(r
, limit
->invert
, "--limit", buf
);
933 if (limit
->burst
> 0)
935 sprintf(buf
, "%u", limit
->burst
);
936 fw3_ipt_rule_addarg(r
, limit
->invert
, "--limit-burst", buf
);
941 fw3_ipt_rule_ipset(struct fw3_ipt_rule
*r
, struct fw3_setmatch
*match
)
943 char buf
[sizeof("dst,dst,dst\0")];
947 struct fw3_ipset
*set
;
948 struct fw3_ipset_datatype
*type
;
950 if (!match
|| !match
->set
|| !match
->ptr
)
954 list_for_each_entry(type
, &set
->datatypes
, list
)
962 p
+= sprintf(p
, "%s", match
->dir
[i
] ? match
->dir
[i
] : type
->dir
);
966 fw3_ipt_rule_addarg(r
, false, "-m", "set");
968 fw3_ipt_rule_addarg(r
, match
->invert
, "--match-set",
969 set
->external
? set
->external
: set
->name
);
971 fw3_ipt_rule_addarg(r
, false, buf
, NULL
);
975 fw3_ipt_rule_time(struct fw3_ipt_rule
*r
, struct fw3_time
*time
)
978 struct tm empty
= { 0 };
980 char buf
[84]; /* sizeof("1,2,3,...,30,31\0") */
983 bool d1
= memcmp(&time
->datestart
, &empty
, sizeof(empty
));
984 bool d2
= memcmp(&time
->datestop
, &empty
, sizeof(empty
));
986 if (!d1
&& !d2
&& !time
->timestart
&& !time
->timestop
&&
987 !(time
->monthdays
& 0xFFFFFFFE) && !(time
->weekdays
& 0xFE))
992 fw3_ipt_rule_addarg(r
, false, "-m", "time");
995 fw3_ipt_rule_addarg(r
, false, "--utc", NULL
);
999 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestart
);
1000 fw3_ipt_rule_addarg(r
, false, "--datestart", buf
);
1005 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestop
);
1006 fw3_ipt_rule_addarg(r
, false, "--datestop", buf
);
1009 if (time
->timestart
)
1011 sprintf(buf
, "%02d:%02d:%02d",
1012 time
->timestart
/ 3600,
1013 time
->timestart
% 3600 / 60,
1014 time
->timestart
% 60);
1016 fw3_ipt_rule_addarg(r
, false, "--timestart", buf
);
1021 sprintf(buf
, "%02d:%02d:%02d",
1022 time
->timestop
/ 3600,
1023 time
->timestop
% 3600 / 60,
1024 time
->timestop
% 60);
1026 fw3_ipt_rule_addarg(r
, false, "--timestop", buf
);
1029 if (time
->monthdays
& 0xFFFFFFFE)
1031 for (i
= 1, p
= buf
; i
< 32; i
++)
1033 if (hasbit(time
->monthdays
, i
))
1038 p
+= sprintf(p
, "%u", i
);
1042 fw3_ipt_rule_addarg(r
, hasbit(time
->monthdays
, 0), "--monthdays", buf
);
1045 if (time
->weekdays
& 0xFE)
1047 for (i
= 1, p
= buf
; i
< 8; i
++)
1049 if (hasbit(time
->weekdays
, i
))
1054 p
+= sprintf(p
, "%u", i
);
1058 fw3_ipt_rule_addarg(r
, hasbit(time
->weekdays
, 0), "--weekdays", buf
);
1063 fw3_ipt_rule_mark(struct fw3_ipt_rule
*r
, struct fw3_mark
*mark
)
1065 char buf
[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
1067 if (!mark
|| !mark
->set
)
1070 if (mark
->mask
< 0xFFFFFFFF)
1071 sprintf(buf
, "0x%x/0x%x", mark
->mark
, mark
->mask
);
1073 sprintf(buf
, "0x%x", mark
->mark
);
1075 fw3_ipt_rule_addarg(r
, false, "-m", "mark");
1076 fw3_ipt_rule_addarg(r
, mark
->invert
, "--mark", buf
);
1080 fw3_ipt_rule_comment(struct fw3_ipt_rule
*r
, const char *fmt
, ...)
1089 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
1092 fw3_ipt_rule_addarg(r
, false, "-m", "comment");
1093 fw3_ipt_rule_addarg(r
, false, "--comment", buf
);
1097 fw3_ipt_rule_extra(struct fw3_ipt_rule
*r
, const char *extra
)
1101 if (!extra
|| !*extra
)
1104 s
= fw3_strdup(extra
);
1106 for (p
= strtok(s
, " \t"); p
; p
= strtok(NULL
, " \t"))
1108 tmp
= realloc(r
->argv
, (r
->argc
+ 1) * sizeof(*r
->argv
));
1114 r
->argv
[r
->argc
++] = fw3_strdup(p
);
1120 #ifndef DISABLE_IPV6
1122 rule_print6(struct ip6t_entry
*e
)
1124 char buf1
[INET6_ADDRSTRLEN
], buf2
[INET6_ADDRSTRLEN
];
1127 if (e
->ipv6
.flags
& IP6T_F_PROTO
)
1129 if (e
->ipv6
.flags
& XT_INV_PROTO
)
1132 pname
= get_protoname(container_of(e
, struct fw3_ipt_rule
, e6
));
1135 printf(" -p %s", pname
);
1137 printf(" -p %u", e
->ipv6
.proto
);
1140 if (e
->ipv6
.iniface
[0])
1142 if (e
->ipv6
.flags
& IP6T_INV_VIA_IN
)
1145 printf(" -i %s", e
->ipv6
.iniface
);
1148 if (e
->ipv6
.outiface
[0])
1150 if (e
->ipv6
.flags
& IP6T_INV_VIA_OUT
)
1153 printf(" -o %s", e
->ipv6
.outiface
);
1156 if (memcmp(&e
->ipv6
.src
, &in6addr_any
, sizeof(struct in6_addr
)))
1158 if (e
->ipv6
.flags
& IP6T_INV_SRCIP
)
1162 inet_ntop(AF_INET6
, &e
->ipv6
.src
, buf1
, sizeof(buf1
)),
1163 inet_ntop(AF_INET6
, &e
->ipv6
.smsk
, buf2
, sizeof(buf2
)));
1166 if (memcmp(&e
->ipv6
.dst
, &in6addr_any
, sizeof(struct in6_addr
)))
1168 if (e
->ipv6
.flags
& IP6T_INV_DSTIP
)
1172 inet_ntop(AF_INET6
, &e
->ipv6
.dst
, buf1
, sizeof(buf1
)),
1173 inet_ntop(AF_INET6
, &e
->ipv6
.dmsk
, buf2
, sizeof(buf2
)));
1179 rule_print4(struct ipt_entry
*e
)
1181 struct in_addr in_zero
= { 0 };
1182 char buf1
[sizeof("255.255.255.255\0")], buf2
[sizeof("255.255.255.255\0")];
1187 if (e
->ip
.flags
& XT_INV_PROTO
)
1190 pname
= get_protoname(container_of(e
, struct fw3_ipt_rule
, e
));
1193 printf(" -p %s", pname
);
1195 printf(" -p %u", e
->ip
.proto
);
1198 if (e
->ip
.iniface
[0])
1200 if (e
->ip
.flags
& IPT_INV_VIA_IN
)
1203 printf(" -i %s", e
->ip
.iniface
);
1206 if (e
->ip
.outiface
[0])
1208 if (e
->ip
.flags
& IPT_INV_VIA_OUT
)
1211 printf(" -o %s", e
->ip
.outiface
);
1214 if (memcmp(&e
->ip
.src
, &in_zero
, sizeof(struct in_addr
)))
1216 if (e
->ip
.flags
& IPT_INV_SRCIP
)
1220 inet_ntop(AF_INET
, &e
->ip
.src
, buf1
, sizeof(buf1
)),
1221 inet_ntop(AF_INET
, &e
->ip
.smsk
, buf2
, sizeof(buf2
)));
1224 if (memcmp(&e
->ip
.dst
, &in_zero
, sizeof(struct in_addr
)))
1226 if (e
->ip
.flags
& IPT_INV_DSTIP
)
1230 inet_ntop(AF_INET
, &e
->ip
.dst
, buf1
, sizeof(buf1
)),
1231 inet_ntop(AF_INET
, &e
->ip
.dmsk
, buf2
, sizeof(buf2
)));
1236 rule_print(struct fw3_ipt_rule
*r
, const char *prefix
, const char *chain
)
1238 debug(r
->h
, "%s %s", prefix
, chain
);
1240 #ifndef DISABLE_IPV6
1241 if (r
->h
->family
== FW3_FAMILY_V6
)
1242 rule_print6(&r
->e6
);
1247 fw3_xt_print_matches(&r
->e
.ip
, r
->matches
);
1248 fw3_xt_print_target(&r
->e
.ip
, r
->target
);
1254 parse_option(struct fw3_ipt_rule
*r
, int optc
, bool inv
)
1256 struct xtables_rule_match
*m
;
1257 struct xtables_match
*em
;
1259 /* is a target option */
1260 if (r
->target
&& fw3_xt_has_target_parse(r
->target
) &&
1261 optc
>= r
->target
->option_offset
&&
1262 optc
< (r
->target
->option_offset
+ 256))
1264 xtables_option_tpcall(optc
, r
->argv
, inv
, r
->target
, &r
->e
);
1268 /* try to dispatch argument to one of the match parsers */
1269 for (m
= r
->matches
; m
; m
= m
->next
)
1273 if (m
->completed
|| !fw3_xt_has_match_parse(em
))
1276 if (optc
< em
->option_offset
||
1277 optc
>= (em
->option_offset
+ 256))
1280 xtables_option_mpcall(optc
, r
->argv
, inv
, em
, &r
->e
);
1284 /* unhandled option, might belong to a protocol match */
1285 if ((em
= load_protomatch(r
)) != NULL
)
1287 init_match(r
, em
, false);
1289 r
->protocol_loaded
= true;
1296 warn("parse_option(): option '%s' needs argument", r
->argv
[optind
-1]);
1299 warn("parse_option(): unknown option '%s'", r
->argv
[optind
-1]);
1305 fw3_ipt_rule_addarg(struct fw3_ipt_rule
*r
, bool inv
,
1306 const char *k
, const char *v
)
1314 n
= inv
+ !!k
+ !!v
;
1315 tmp
= realloc(r
->argv
, (r
->argc
+ n
) * sizeof(*tmp
));
1323 r
->argv
[r
->argc
++] = fw3_strdup("!");
1325 r
->argv
[r
->argc
++] = fw3_strdup(k
);
1328 r
->argv
[r
->argc
++] = fw3_strdup(v
);
1331 static unsigned char *
1332 rule_mask(struct fw3_ipt_rule
*r
)
1335 unsigned char *p
, *mask
= NULL
;
1336 struct xtables_rule_match
*m
;
1338 #define SZ(x) XT_ALIGN(sizeof(struct x))
1340 #ifndef DISABLE_IPV6
1341 if (r
->h
->family
== FW3_FAMILY_V6
)
1345 for (m
= r
->matches
; m
; m
= m
->next
)
1346 s
+= SZ(ip6t_entry_match
) + m
->match
->size
;
1348 s
+= SZ(ip6t_entry_target
);
1350 s
+= r
->target
->size
;
1352 mask
= fw3_alloc(s
);
1353 memset(mask
, 0xFF, SZ(ip6t_entry
));
1354 p
= mask
+ SZ(ip6t_entry
);
1356 for (m
= r
->matches
; m
; m
= m
->next
)
1358 memset(p
, 0xFF, SZ(ip6t_entry_match
) + m
->match
->userspacesize
);
1359 p
+= SZ(ip6t_entry_match
) + m
->match
->size
;
1362 memset(p
, 0xFF, SZ(ip6t_entry_target
) + (r
->target
) ? r
->target
->userspacesize
: 0);
1369 for (m
= r
->matches
; m
; m
= m
->next
)
1370 s
+= SZ(ipt_entry_match
) + m
->match
->size
;
1372 s
+= SZ(ipt_entry_target
);
1374 s
+= r
->target
->size
;
1376 mask
= fw3_alloc(s
);
1377 memset(mask
, 0xFF, SZ(ipt_entry
));
1378 p
= mask
+ SZ(ipt_entry
);
1380 for (m
= r
->matches
; m
; m
= m
->next
)
1382 memset(p
, 0xFF, SZ(ipt_entry_match
) + m
->match
->userspacesize
);
1383 p
+= SZ(ipt_entry_match
) + m
->match
->size
;
1386 memset(p
, 0xFF, SZ(ipt_entry_target
) + (r
->target
) ? r
->target
->userspacesize
: 0);
1393 rule_build(struct fw3_ipt_rule
*r
)
1395 size_t s
, target_size
= (r
->target
) ? r
->target
->t
->u
.target_size
: 0;
1396 struct xtables_rule_match
*m
;
1398 #ifndef DISABLE_IPV6
1399 if (r
->h
->family
== FW3_FAMILY_V6
)
1401 struct ip6t_entry
*e6
;
1403 s
= XT_ALIGN(sizeof(struct ip6t_entry
));
1405 for (m
= r
->matches
; m
; m
= m
->next
)
1406 s
+= m
->match
->m
->u
.match_size
;
1408 e6
= fw3_alloc(s
+ target_size
);
1410 memcpy(e6
, &r
->e6
, sizeof(struct ip6t_entry
));
1412 e6
->target_offset
= s
;
1413 e6
->next_offset
= s
+ target_size
;
1417 for (m
= r
->matches
; m
; m
= m
->next
)
1419 memcpy(e6
->elems
+ s
, m
->match
->m
, m
->match
->m
->u
.match_size
);
1420 s
+= m
->match
->m
->u
.match_size
;
1424 memcpy(e6
->elems
+ s
, r
->target
->t
, target_size
);
1431 struct ipt_entry
*e
;
1433 s
= XT_ALIGN(sizeof(struct ipt_entry
));
1435 for (m
= r
->matches
; m
; m
= m
->next
)
1436 s
+= m
->match
->m
->u
.match_size
;
1438 e
= fw3_alloc(s
+ target_size
);
1440 memcpy(e
, &r
->e
, sizeof(struct ipt_entry
));
1442 e
->target_offset
= s
;
1443 e
->next_offset
= s
+ target_size
;
1447 for (m
= r
->matches
; m
; m
= m
->next
)
1449 memcpy(e
->elems
+ s
, m
->match
->m
, m
->match
->m
->u
.match_size
);
1450 s
+= m
->match
->m
->u
.match_size
;
1454 memcpy(e
->elems
+ s
, r
->target
->t
, target_size
);
1461 __fw3_ipt_rule_append(struct fw3_ipt_rule
*r
, bool repl
, const char *fmt
, ...)
1464 unsigned char *mask
;
1466 struct xtables_rule_match
*m
;
1467 struct xtables_match
*em
;
1468 struct xtables_target
*et
;
1469 struct xtables_globals
*g
;
1478 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
1481 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
1482 g
->opts
= g
->orig_opts
;
1489 em
= find_match(r
, "id");
1493 warn("fw3_ipt_rule_append(): Can't find match '%s'", "id");
1497 init_match(r
, em
, true);
1499 id
= FW3_ID_MAGIC
| (r
->id
& ~FW3_ID_MASK
);
1500 memcpy(em
->m
->data
, &id
, sizeof(id
));
1505 while ((optc
= getopt_long(r
->argc
, r
->argv
, "-:m:j:", g
->opts
,
1511 em
= find_match(r
, optarg
);
1515 warn("fw3_ipt_rule_append(): Can't find match '%s'", optarg
);
1519 init_match(r
, em
, true);
1523 et
= get_target(r
, optarg
);
1527 warn("fw3_ipt_rule_append(): Can't find target '%s'", optarg
);
1534 if ((optarg
[0] == '!') && (optarg
[1] == '\0'))
1541 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg
);
1545 if (parse_option(r
, optc
, inv
))
1553 for (m
= r
->matches
; m
; m
= m
->next
)
1554 xtables_option_mfcall(m
->match
);
1557 xtables_option_tfcall(r
->target
);
1559 rule
= rule_build(r
);
1561 #ifndef DISABLE_IPV6
1562 if (r
->h
->family
== FW3_FAMILY_V6
)
1566 mask
= rule_mask(r
);
1568 while (ip6tc_delete_entry(buf
, rule
, mask
, r
->h
->handle
))
1570 rule_print(r
, "-D", buf
);
1576 rule_print(r
, "-A", buf
);
1578 if (!ip6tc_append_entry(buf
, rule
, r
->h
->handle
))
1579 warn("ip6tc_append_entry(): %s", ip6tc_strerror(errno
));
1586 mask
= rule_mask(r
);
1588 while (iptc_delete_entry(buf
, rule
, mask
, r
->h
->handle
))
1590 rule_print(r
, "-D", buf
);
1596 rule_print(r
, "-A", buf
);
1598 if (!iptc_append_entry(buf
, rule
, r
->h
->handle
))
1599 warn("iptc_append_entry(): %s\n", iptc_strerror(errno
));
1605 for (i
= 1; i
< r
->argc
; i
++)
1610 xtables_rule_matches_free(&r
->matches
);
1617 /* reset all targets and matches */
1618 for (em
= xtables_matches
; em
; em
= em
->next
)
1621 for (et
= xtables_targets
; et
; et
= et
->next
)
1627 xtables_free_opts(1);
1630 struct fw3_ipt_rule
*
1631 fw3_ipt_rule_create(struct fw3_ipt_handle
*handle
, struct fw3_protocol
*proto
,
1632 struct fw3_device
*in
, struct fw3_device
*out
,
1633 struct fw3_address
*src
, struct fw3_address
*dest
)
1635 struct fw3_ipt_rule
*r
;
1637 r
= fw3_ipt_rule_new(handle
);
1639 fw3_ipt_rule_proto(r
, proto
);
1640 fw3_ipt_rule_in_out(r
, in
, out
);
1641 fw3_ipt_rule_src_dest(r
, src
, dest
);