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.
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
,
32 #if XTABLES_VERSION_CODE > 10
33 .compat_rev
= xtables_compatible_revision
,
37 static struct xtables_globals xtg6
= {
39 .program_version
= "6",
40 .orig_opts
= base_opts
,
41 #if XTABLES_VERSION_CODE > 10
42 .compat_rev
= xtables_compatible_revision
,
49 struct xtables_match
**matches
;
50 struct xtables_target
**targets
;
51 void (*register_match
)(struct xtables_match
*);
52 void (*register_target
)(struct xtables_target
*);
56 /* Required by certain extensions like SNAT and DNAT */
57 int kernel_version
= 0;
60 get_kernel_version(void)
62 static struct utsname uts
;
63 int x
= 0, y
= 0, z
= 0;
65 if (uname(&uts
) == -1)
66 sprintf(uts
.release
, "3.0.0");
68 sscanf(uts
.release
, "%d.%d.%d", &x
, &y
, &z
);
69 kernel_version
= 0x10000 * x
+ 0x100 * y
+ z
;
72 static void fw3_init_extensions(void)
82 struct fw3_ipt_handle
*
83 fw3_ipt_open(enum fw3_family family
, enum fw3_table table
)
86 struct fw3_ipt_handle
*h
;
88 h
= fw3_alloc(sizeof(*h
));
92 if (family
== FW3_FAMILY_V6
)
95 h
->family
= FW3_FAMILY_V6
;
97 h
->handle
= ip6tc_init(fw3_flag_names
[table
]);
99 xtables_set_params(&xtg6
);
100 xtables_set_nfproto(NFPROTO_IPV6
);
105 h
->family
= FW3_FAMILY_V4
;
107 h
->handle
= iptc_init(fw3_flag_names
[table
]);
109 xtables_set_params(&xtg
);
110 xtables_set_nfproto(NFPROTO_IPV4
);
120 fw3_init_extensions();
122 if (xext
.register_match
)
123 for (i
= 0; i
< xext
.mcount
; i
++)
124 xext
.register_match(xext
.matches
[i
]);
126 if (xext
.register_target
)
127 for (i
= 0; i
< xext
.tcount
; i
++)
128 xext
.register_target(xext
.targets
[i
]);
134 debug(struct fw3_ipt_handle
*h
, const char *fmt
, ...)
138 printf("%s -t %s ", (h
->family
== FW3_FAMILY_V6
) ? "ip6tables" : "iptables",
139 fw3_flag_names
[h
->table
]);
147 fw3_ipt_set_policy(struct fw3_ipt_handle
*h
, const char *chain
,
148 enum fw3_flag policy
)
151 debug(h
, "-P %s %s\n", chain
, fw3_flag_names
[policy
]);
154 if (h
->family
== FW3_FAMILY_V6
)
155 ip6tc_set_policy(chain
, fw3_flag_names
[policy
], NULL
, h
->handle
);
158 iptc_set_policy(chain
, fw3_flag_names
[policy
], NULL
, h
->handle
);
162 fw3_ipt_flush_chain(struct fw3_ipt_handle
*h
, const char *chain
)
165 debug(h
, "-F %s\n", chain
);
168 if (h
->family
== FW3_FAMILY_V6
)
169 ip6tc_flush_entries(chain
, h
->handle
);
172 iptc_flush_entries(chain
, h
->handle
);
176 delete_rules(struct fw3_ipt_handle
*h
, const char *target
)
179 const struct ipt_entry
*e
;
185 if (h
->family
== FW3_FAMILY_V6
)
187 for (chain
= ip6tc_first_chain(h
->handle
);
189 chain
= ip6tc_next_chain(h
->handle
))
194 const struct ip6t_entry
*e6
;
195 for (num
= 0, e6
= ip6tc_first_rule(chain
, h
->handle
);
197 num
++, e6
= ip6tc_next_rule(e6
, h
->handle
))
199 t
= ip6tc_get_target(e6
, h
->handle
);
201 if (*t
&& !strcmp(t
, target
))
204 debug(h
, "-D %s %u\n", chain
, num
+ 1);
206 ip6tc_delete_num_entry(chain
, num
, h
->handle
);
217 for (chain
= iptc_first_chain(h
->handle
);
219 chain
= iptc_next_chain(h
->handle
))
224 for (num
= 0, e
= iptc_first_rule(chain
, h
->handle
);
226 num
++, e
= iptc_next_rule(e
, h
->handle
))
228 t
= iptc_get_target(e
, h
->handle
);
230 if (*t
&& !strcmp(t
, target
))
233 debug(h
, "-D %s %u\n", chain
, num
+ 1);
235 iptc_delete_num_entry(chain
, num
, h
->handle
);
246 fw3_ipt_delete_chain(struct fw3_ipt_handle
*h
, const char *chain
)
248 delete_rules(h
, chain
);
251 debug(h
, "-X %s\n", chain
);
254 if (h
->family
== FW3_FAMILY_V6
)
255 ip6tc_delete_chain(chain
, h
->handle
);
258 iptc_delete_chain(chain
, h
->handle
);
262 get_rule_id(const void *base
, unsigned int start
, unsigned int end
)
266 const struct xt_entry_match
*em
;
268 for (i
= start
; i
< end
; i
+= em
->u
.match_size
)
272 if (strcmp(em
->u
.user
.name
, "id"))
275 memcpy(&id
, em
->data
, sizeof(id
));
277 if ((id
& FW3_ID_MASK
) != FW3_ID_MAGIC
)
280 return (id
& ~FW3_ID_MASK
);
287 fw3_ipt_delete_id_rules(struct fw3_ipt_handle
*h
, const char *chain
)
290 const struct ipt_entry
*e
;
295 if (h
->family
== FW3_FAMILY_V6
)
297 if (!ip6tc_is_chain(chain
, h
->handle
))
303 const struct ip6t_entry
*e6
;
304 for (num
= 0, e6
= ip6tc_first_rule(chain
, h
->handle
);
306 num
++, e6
= ip6tc_next_rule(e6
, h
->handle
))
308 id
= get_rule_id(e6
, sizeof(*e6
), e6
->target_offset
);
313 debug(h
, "-D %s %u\n", chain
, num
+ 1);
315 ip6tc_delete_num_entry(chain
, num
, h
->handle
);
325 if (!iptc_is_chain(chain
, h
->handle
))
331 for (num
= 0, e
= iptc_first_rule(chain
, h
->handle
);
333 num
++, e
= iptc_next_rule(e
, h
->handle
))
335 id
= get_rule_id(e
, sizeof(*e
), e
->target_offset
);
340 debug(h
, "-D %s %u\n", chain
, num
+ 1);
342 iptc_delete_num_entry(chain
, num
, h
->handle
);
352 fw3_ipt_create_chain(struct fw3_ipt_handle
*h
, const char *fmt
, ...)
358 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
362 debug(h
, "-N %s\n", buf
);
364 iptc_create_chain(buf
, h
->handle
);
368 fw3_ipt_flush(struct fw3_ipt_handle
*h
)
373 if (h
->family
== FW3_FAMILY_V6
)
375 for (chain
= ip6tc_first_chain(h
->handle
);
377 chain
= ip6tc_next_chain(h
->handle
))
379 ip6tc_flush_entries(chain
, h
->handle
);
382 for (chain
= ip6tc_first_chain(h
->handle
);
384 chain
= ip6tc_next_chain(h
->handle
))
386 ip6tc_delete_chain(chain
, h
->handle
);
392 for (chain
= iptc_first_chain(h
->handle
);
394 chain
= iptc_next_chain(h
->handle
))
396 iptc_flush_entries(chain
, h
->handle
);
399 for (chain
= iptc_first_chain(h
->handle
);
401 chain
= iptc_next_chain(h
->handle
))
403 iptc_delete_chain(chain
, h
->handle
);
409 chain_is_empty(struct fw3_ipt_handle
*h
, const char *chain
)
412 if (h
->family
== FW3_FAMILY_V6
)
413 return (!ip6tc_builtin(chain
, h
->handle
) &&
414 !ip6tc_first_rule(chain
, h
->handle
));
417 return (!iptc_builtin(chain
, h
->handle
) &&
418 !iptc_first_rule(chain
, h
->handle
));
422 fw3_ipt_gc(struct fw3_ipt_handle
*h
)
428 if (h
->family
== FW3_FAMILY_V6
)
433 for (chain
= ip6tc_first_chain(h
->handle
);
435 chain
= ip6tc_next_chain(h
->handle
))
437 if (!chain_is_empty(h
, chain
))
440 fw3_ipt_delete_chain(h
, chain
);
452 for (chain
= iptc_first_chain(h
->handle
);
454 chain
= iptc_next_chain(h
->handle
))
456 warn("C=%s\n", chain
);
458 if (!chain_is_empty(h
, chain
))
461 warn("D=%s\n", chain
);
463 fw3_ipt_delete_chain(h
, chain
);
472 fw3_ipt_commit(struct fw3_ipt_handle
*h
)
477 if (h
->family
== FW3_FAMILY_V6
)
479 rv
= ip6tc_commit(h
->handle
);
481 warn("ip6tc_commit(): %s", ip6tc_strerror(errno
));
486 rv
= iptc_commit(h
->handle
);
488 warn("iptc_commit(): %s", iptc_strerror(errno
));
493 fw3_ipt_close(struct fw3_ipt_handle
*h
)
498 struct fw3_ipt_rule
*
499 fw3_ipt_rule_new(struct fw3_ipt_handle
*h
)
501 struct fw3_ipt_rule
*r
;
503 r
= fw3_alloc(sizeof(*r
));
507 r
->argv
= fw3_alloc(sizeof(char *));
508 r
->argv
[r
->argc
++] = "fw3";
515 is_chain(struct fw3_ipt_handle
*h
, const char *name
)
518 if (h
->family
== FW3_FAMILY_V6
)
519 return ip6tc_is_chain(name
, h
->handle
);
522 return iptc_is_chain(name
, h
->handle
);
526 get_protoname(struct fw3_ipt_rule
*r
)
528 const struct xtables_pprot
*pp
;
531 for (pp
= xtables_chain_protos
; pp
->name
; pp
++)
532 if (pp
->num
== r
->protocol
)
533 return (char *)pp
->name
;
538 static struct xtables_match
*
539 find_match(struct fw3_ipt_rule
*r
, const char *name
)
541 struct xtables_match
*m
;
544 m
= xtables_find_match(name
, XTF_TRY_LOAD
, &r
->matches
);
551 init_match(struct fw3_ipt_rule
*r
, struct xtables_match
*m
, bool no_clone
)
554 struct xtables_globals
*g
;
559 s
= XT_ALIGN(sizeof(struct xt_entry_match
)) + m
->size
;
563 fw3_xt_set_match_name(m
);
565 m
->m
->u
.user
.revision
= m
->revision
;
566 m
->m
->u
.match_size
= s
;
568 /* free previous userspace data */
569 fw3_xt_free_match_udata(m
);
574 /* don't merge options if no_clone is set and this match is a clone */
575 if (no_clone
&& (m
== m
->next
))
578 /* merge option table */
579 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
580 fw3_xt_merge_match_options(g
, m
);
584 need_protomatch(struct fw3_ipt_rule
*r
, const char *pname
)
589 if (!xtables_find_match(pname
, XTF_DONT_LOAD
, NULL
))
592 return !r
->protocol_loaded
;
595 static struct xtables_match
*
596 load_protomatch(struct fw3_ipt_rule
*r
)
598 const char *pname
= get_protoname(r
);
600 if (!need_protomatch(r
, pname
))
603 return find_match(r
, pname
);
606 static struct xtables_target
*
607 find_target(struct fw3_ipt_rule
*r
, const char *name
)
609 struct xtables_target
*t
;
613 if (is_chain(r
->h
, name
))
614 t
= xtables_find_target(XT_STANDARD_TARGET
, XTF_TRY_LOAD
);
616 t
= xtables_find_target(name
, XTF_TRY_LOAD
);
623 static struct xtables_target
*
624 get_target(struct fw3_ipt_rule
*r
, const char *name
)
627 struct xtables_target
*t
;
628 struct xtables_globals
*g
;
630 t
= find_target(r
, name
);
635 s
= XT_ALIGN(sizeof(struct xt_entry_target
)) + t
->size
;
638 fw3_xt_set_target_name(t
, name
);
640 t
->t
->u
.user
.revision
= t
->revision
;
641 t
->t
->u
.target_size
= s
;
643 /* free previous userspace data */
644 fw3_xt_free_target_udata(t
);
649 /* merge option table */
650 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
651 fw3_xt_merge_target_options(g
, t
);
659 fw3_ipt_rule_proto(struct fw3_ipt_rule
*r
, struct fw3_protocol
*proto
)
663 if (!proto
|| proto
->any
)
666 pr
= proto
->protocol
;
669 if (r
->h
->family
== FW3_FAMILY_V6
)
674 r
->e6
.ipv6
.proto
= pr
;
675 r
->e6
.ipv6
.flags
|= IP6T_F_PROTO
;
678 r
->e6
.ipv6
.invflags
|= XT_INV_PROTO
;
686 r
->e
.ip
.invflags
|= XT_INV_PROTO
;
693 fw3_ipt_rule_in_out(struct fw3_ipt_rule
*r
,
694 struct fw3_device
*in
, struct fw3_device
*out
)
697 if (r
->h
->family
== FW3_FAMILY_V6
)
701 xtables_parse_interface(in
->name
, r
->e6
.ipv6
.iniface
,
702 r
->e6
.ipv6
.iniface_mask
);
705 r
->e6
.ipv6
.invflags
|= IP6T_INV_VIA_IN
;
708 if (out
&& !out
->any
)
710 xtables_parse_interface(out
->name
, r
->e6
.ipv6
.outiface
,
711 r
->e6
.ipv6
.outiface_mask
);
714 r
->e6
.ipv6
.invflags
|= IP6T_INV_VIA_OUT
;
722 xtables_parse_interface(in
->name
, r
->e
.ip
.iniface
,
723 r
->e
.ip
.iniface_mask
);
726 r
->e
.ip
.invflags
|= IPT_INV_VIA_IN
;
729 if (out
&& !out
->any
)
731 xtables_parse_interface(out
->name
, r
->e
.ip
.outiface
,
732 r
->e
.ip
.outiface_mask
);
735 r
->e
.ip
.invflags
|= IPT_INV_VIA_OUT
;
742 fw3_ipt_rule_src_dest(struct fw3_ipt_rule
*r
,
743 struct fw3_address
*src
, struct fw3_address
*dest
)
745 if ((src
&& src
->range
) || (dest
&& dest
->range
))
747 fw3_ipt_rule_addarg(r
, false, "-m", "iprange");
754 fw3_ipt_rule_addarg(r
, src
->invert
, "--src-range",
755 fw3_address_to_string(src
, false, false));
758 else if (r
->h
->family
== FW3_FAMILY_V6
)
760 r
->e6
.ipv6
.src
= src
->address
.v6
;
761 r
->e6
.ipv6
.smsk
= src
->mask
.v6
;
764 for (i
= 0; i
< 4; i
++)
765 r
->e6
.ipv6
.src
.s6_addr32
[i
] &= r
->e6
.ipv6
.smsk
.s6_addr32
[i
];
768 r
->e6
.ipv6
.invflags
|= IP6T_INV_SRCIP
;
773 r
->e
.ip
.src
= src
->address
.v4
;
774 r
->e
.ip
.smsk
= src
->mask
.v4
;
776 r
->e
.ip
.src
.s_addr
&= r
->e
.ip
.smsk
.s_addr
;
779 r
->e
.ip
.invflags
|= IPT_INV_SRCIP
;
783 if (dest
&& dest
->set
)
787 fw3_ipt_rule_addarg(r
, dest
->invert
, "--dst-range",
788 fw3_address_to_string(dest
, false, false));
791 else if (r
->h
->family
== FW3_FAMILY_V6
)
793 r
->e6
.ipv6
.dst
= dest
->address
.v6
;
794 r
->e6
.ipv6
.dmsk
= dest
->mask
.v6
;
797 for (i
= 0; i
< 4; i
++)
798 r
->e6
.ipv6
.dst
.s6_addr32
[i
] &= r
->e6
.ipv6
.dmsk
.s6_addr32
[i
];
801 r
->e6
.ipv6
.invflags
|= IP6T_INV_DSTIP
;
806 r
->e
.ip
.dst
= dest
->address
.v4
;
807 r
->e
.ip
.dmsk
= dest
->mask
.v4
;
809 r
->e
.ip
.dst
.s_addr
&= r
->e
.ip
.dmsk
.s_addr
;
812 r
->e
.ip
.invflags
|= IPT_INV_DSTIP
;
818 fw3_ipt_rule_sport_dport(struct fw3_ipt_rule
*r
,
819 struct fw3_port
*sp
, struct fw3_port
*dp
)
821 char buf
[sizeof("65535:65535\0")];
823 if ((!sp
|| !sp
->set
) && (!dp
|| !dp
->set
))
826 if (!get_protoname(r
))
831 if (sp
->port_min
== sp
->port_max
)
832 sprintf(buf
, "%u", sp
->port_min
);
834 sprintf(buf
, "%u:%u", sp
->port_min
, sp
->port_max
);
836 fw3_ipt_rule_addarg(r
, sp
->invert
, "--sport", buf
);
841 if (dp
->port_min
== dp
->port_max
)
842 sprintf(buf
, "%u", dp
->port_min
);
844 sprintf(buf
, "%u:%u", dp
->port_min
, dp
->port_max
);
846 fw3_ipt_rule_addarg(r
, dp
->invert
, "--dport", buf
);
851 fw3_ipt_rule_device(struct fw3_ipt_rule
*r
, const char *device
, bool out
)
854 struct fw3_device dev
= { .any
= false };
855 strncpy(dev
.name
, device
, sizeof(dev
.name
) - 1);
856 fw3_ipt_rule_in_out(r
, (out
) ? NULL
: &dev
, (out
) ? &dev
: NULL
);
861 fw3_ipt_rule_mac(struct fw3_ipt_rule
*r
, struct fw3_mac
*mac
)
863 char buf
[sizeof("ff:ff:ff:ff:ff:ff\0")];
864 uint8_t *addr
= mac
->mac
.ether_addr_octet
;
869 sprintf(buf
, "%02x:%02x:%02x:%02x:%02x:%02x",
870 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
872 fw3_ipt_rule_addarg(r
, false, "-m", "mac");
873 fw3_ipt_rule_addarg(r
, mac
->invert
, "--mac-source", buf
);
877 fw3_ipt_rule_icmptype(struct fw3_ipt_rule
*r
, struct fw3_icmptype
*icmp
)
879 char buf
[sizeof("255/255\0")];
885 if (r
->h
->family
== FW3_FAMILY_V6
)
887 if (icmp
->code6_min
== 0 && icmp
->code6_max
== 0xFF)
888 sprintf(buf
, "%u", icmp
->type6
);
890 sprintf(buf
, "%u/%u", icmp
->type6
, icmp
->code6_min
);
892 fw3_ipt_rule_addarg(r
, icmp
->invert
, "--icmpv6-type", buf
);
897 if (icmp
->code_min
== 0 && icmp
->code_max
== 0xFF)
898 sprintf(buf
, "%u", icmp
->type
);
900 sprintf(buf
, "%u/%u", icmp
->type
, icmp
->code_min
);
902 fw3_ipt_rule_addarg(r
, icmp
->invert
, "--icmp-type", buf
);
907 fw3_ipt_rule_limit(struct fw3_ipt_rule
*r
, struct fw3_limit
*limit
)
909 char buf
[sizeof("-4294967296/second\0")];
911 if (!limit
|| limit
->rate
<= 0)
914 fw3_ipt_rule_addarg(r
, false, "-m", "limit");
916 sprintf(buf
, "%u/%s", limit
->rate
, fw3_limit_units
[limit
->unit
]);
917 fw3_ipt_rule_addarg(r
, limit
->invert
, "--limit", buf
);
919 if (limit
->burst
> 0)
921 sprintf(buf
, "%u", limit
->burst
);
922 fw3_ipt_rule_addarg(r
, limit
->invert
, "--limit-burst", buf
);
927 fw3_ipt_rule_ipset(struct fw3_ipt_rule
*r
, struct fw3_setmatch
*match
)
929 char buf
[sizeof("dst,dst,dst\0")];
933 struct fw3_ipset
*set
;
934 struct fw3_ipset_datatype
*type
;
936 if (!match
|| !match
->set
|| !match
->ptr
)
940 list_for_each_entry(type
, &set
->datatypes
, list
)
948 p
+= sprintf(p
, "%s", match
->dir
[i
] ? match
->dir
[i
] : type
->dir
);
952 fw3_ipt_rule_addarg(r
, false, "-m", "set");
954 fw3_ipt_rule_addarg(r
, match
->invert
, "--match-set",
955 set
->external
? set
->external
: set
->name
);
957 fw3_ipt_rule_addarg(r
, false, buf
, NULL
);
961 fw3_ipt_rule_time(struct fw3_ipt_rule
*r
, struct fw3_time
*time
)
964 struct tm empty
= { 0 };
966 char buf
[84]; /* sizeof("1,2,3,...,30,31\0") */
969 bool d1
= memcmp(&time
->datestart
, &empty
, sizeof(empty
));
970 bool d2
= memcmp(&time
->datestop
, &empty
, sizeof(empty
));
972 if (!d1
&& !d2
&& !time
->timestart
&& !time
->timestop
&&
973 !(time
->monthdays
& 0xFFFFFFFE) && !(time
->weekdays
& 0xFE))
978 fw3_ipt_rule_addarg(r
, false, "-m", "time");
981 fw3_ipt_rule_addarg(r
, false, "--utc", NULL
);
985 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestart
);
986 fw3_ipt_rule_addarg(r
, false, "--datestart", buf
);
991 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestop
);
992 fw3_ipt_rule_addarg(r
, false, "--datestop", buf
);
997 sprintf(buf
, "%02d:%02d:%02d",
998 time
->timestart
/ 3600,
999 time
->timestart
% 3600 / 60,
1000 time
->timestart
% 60);
1002 fw3_ipt_rule_addarg(r
, false, "--timestart", buf
);
1007 sprintf(buf
, "%02d:%02d:%02d",
1008 time
->timestop
/ 3600,
1009 time
->timestop
% 3600 / 60,
1010 time
->timestop
% 60);
1012 fw3_ipt_rule_addarg(r
, false, "--timestop", buf
);
1015 if (time
->monthdays
& 0xFFFFFFFE)
1017 for (i
= 1, p
= buf
; i
< 32; i
++)
1019 if (fw3_hasbit(time
->monthdays
, i
))
1024 p
+= sprintf(p
, "%u", i
);
1028 fw3_ipt_rule_addarg(r
, fw3_hasbit(time
->monthdays
, 0), "--monthdays", buf
);
1031 if (time
->weekdays
& 0xFE)
1033 for (i
= 1, p
= buf
; i
< 8; i
++)
1035 if (fw3_hasbit(time
->weekdays
, i
))
1040 p
+= sprintf(p
, "%u", i
);
1044 fw3_ipt_rule_addarg(r
, fw3_hasbit(time
->weekdays
, 0), "--weekdays", buf
);
1049 fw3_ipt_rule_mark(struct fw3_ipt_rule
*r
, struct fw3_mark
*mark
)
1051 char buf
[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
1053 if (!mark
|| !mark
->set
)
1056 if (mark
->mask
< 0xFFFFFFFF)
1057 sprintf(buf
, "0x%x/0x%x", mark
->mark
, mark
->mask
);
1059 sprintf(buf
, "0x%x", mark
->mark
);
1061 fw3_ipt_rule_addarg(r
, false, "-m", "mark");
1062 fw3_ipt_rule_addarg(r
, mark
->invert
, "--mark", buf
);
1066 fw3_ipt_rule_comment(struct fw3_ipt_rule
*r
, const char *fmt
, ...)
1075 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
1078 fw3_ipt_rule_addarg(r
, false, "-m", "comment");
1079 fw3_ipt_rule_addarg(r
, false, "--comment", buf
);
1083 fw3_ipt_rule_extra(struct fw3_ipt_rule
*r
, const char *extra
)
1087 if (!extra
|| !*extra
)
1090 s
= fw3_strdup(extra
);
1092 for (p
= strtok(s
, " \t"); p
; p
= strtok(NULL
, " \t"))
1094 tmp
= realloc(r
->argv
, (r
->argc
+ 1) * sizeof(*r
->argv
));
1100 r
->argv
[r
->argc
++] = fw3_strdup(p
);
1106 #ifndef DISABLE_IPV6
1108 rule_print6(struct ip6t_entry
*e
)
1110 char buf1
[INET6_ADDRSTRLEN
], buf2
[INET6_ADDRSTRLEN
];
1113 if (e
->ipv6
.flags
& IP6T_F_PROTO
)
1115 if (e
->ipv6
.invflags
& XT_INV_PROTO
)
1118 pname
= get_protoname(container_of(e
, struct fw3_ipt_rule
, e6
));
1121 printf(" -p %s", pname
);
1123 printf(" -p %u", e
->ipv6
.proto
);
1126 if (e
->ipv6
.iniface
[0])
1128 if (e
->ipv6
.invflags
& IP6T_INV_VIA_IN
)
1131 printf(" -i %s", e
->ipv6
.iniface
);
1134 if (e
->ipv6
.outiface
[0])
1136 if (e
->ipv6
.invflags
& IP6T_INV_VIA_OUT
)
1139 printf(" -o %s", e
->ipv6
.outiface
);
1142 if (memcmp(&e
->ipv6
.src
, &in6addr_any
, sizeof(struct in6_addr
)))
1144 if (e
->ipv6
.invflags
& IP6T_INV_SRCIP
)
1148 inet_ntop(AF_INET6
, &e
->ipv6
.src
, buf1
, sizeof(buf1
)),
1149 inet_ntop(AF_INET6
, &e
->ipv6
.smsk
, buf2
, sizeof(buf2
)));
1152 if (memcmp(&e
->ipv6
.dst
, &in6addr_any
, sizeof(struct in6_addr
)))
1154 if (e
->ipv6
.invflags
& IP6T_INV_DSTIP
)
1158 inet_ntop(AF_INET6
, &e
->ipv6
.dst
, buf1
, sizeof(buf1
)),
1159 inet_ntop(AF_INET6
, &e
->ipv6
.dmsk
, buf2
, sizeof(buf2
)));
1165 rule_print4(struct ipt_entry
*e
)
1167 struct in_addr in_zero
= { 0 };
1168 char buf1
[sizeof("255.255.255.255\0")], buf2
[sizeof("255.255.255.255\0")];
1173 if (e
->ip
.invflags
& XT_INV_PROTO
)
1176 pname
= get_protoname(container_of(e
, struct fw3_ipt_rule
, e
));
1179 printf(" -p %s", pname
);
1181 printf(" -p %u", e
->ip
.proto
);
1184 if (e
->ip
.iniface
[0])
1186 if (e
->ip
.invflags
& IPT_INV_VIA_IN
)
1189 printf(" -i %s", e
->ip
.iniface
);
1192 if (e
->ip
.outiface
[0])
1194 if (e
->ip
.invflags
& IPT_INV_VIA_OUT
)
1197 printf(" -o %s", e
->ip
.outiface
);
1200 if (memcmp(&e
->ip
.src
, &in_zero
, sizeof(struct in_addr
)))
1202 if (e
->ip
.invflags
& IPT_INV_SRCIP
)
1206 inet_ntop(AF_INET
, &e
->ip
.src
, buf1
, sizeof(buf1
)),
1207 inet_ntop(AF_INET
, &e
->ip
.smsk
, buf2
, sizeof(buf2
)));
1210 if (memcmp(&e
->ip
.dst
, &in_zero
, sizeof(struct in_addr
)))
1212 if (e
->ip
.invflags
& IPT_INV_DSTIP
)
1216 inet_ntop(AF_INET
, &e
->ip
.dst
, buf1
, sizeof(buf1
)),
1217 inet_ntop(AF_INET
, &e
->ip
.dmsk
, buf2
, sizeof(buf2
)));
1222 rule_print(struct fw3_ipt_rule
*r
, const char *prefix
, const char *chain
)
1224 debug(r
->h
, "%s %s", prefix
, chain
);
1226 #ifndef DISABLE_IPV6
1227 if (r
->h
->family
== FW3_FAMILY_V6
)
1228 rule_print6(&r
->e6
);
1233 fw3_xt_print_matches(&r
->e
.ip
, r
->matches
);
1234 fw3_xt_print_target(&r
->e
.ip
, r
->target
);
1240 parse_option(struct fw3_ipt_rule
*r
, int optc
, bool inv
)
1242 struct xtables_rule_match
*m
;
1243 struct xtables_match
*em
;
1245 /* is a target option */
1246 if (r
->target
&& fw3_xt_has_target_parse(r
->target
) &&
1247 optc
>= r
->target
->option_offset
&&
1248 optc
< (r
->target
->option_offset
+ 256))
1250 xtables_option_tpcall(optc
, r
->argv
, inv
, r
->target
, &r
->e
);
1254 /* try to dispatch argument to one of the match parsers */
1255 for (m
= r
->matches
; m
; m
= m
->next
)
1259 if (m
->completed
|| !fw3_xt_has_match_parse(em
))
1262 if (optc
< em
->option_offset
||
1263 optc
>= (em
->option_offset
+ 256))
1266 xtables_option_mpcall(optc
, r
->argv
, inv
, em
, &r
->e
);
1270 /* unhandled option, might belong to a protocol match */
1271 if ((em
= load_protomatch(r
)) != NULL
)
1273 init_match(r
, em
, false);
1275 r
->protocol_loaded
= true;
1282 warn("parse_option(): option '%s' needs argument", r
->argv
[optind
-1]);
1285 warn("parse_option(): unknown option '%s'", r
->argv
[optind
-1]);
1291 fw3_ipt_rule_addarg(struct fw3_ipt_rule
*r
, bool inv
,
1292 const char *k
, const char *v
)
1300 n
= inv
+ !!k
+ !!v
;
1301 tmp
= realloc(r
->argv
, (r
->argc
+ n
) * sizeof(*tmp
));
1309 r
->argv
[r
->argc
++] = fw3_strdup("!");
1311 r
->argv
[r
->argc
++] = fw3_strdup(k
);
1314 r
->argv
[r
->argc
++] = fw3_strdup(v
);
1317 static unsigned char *
1318 rule_mask(struct fw3_ipt_rule
*r
)
1321 unsigned char *p
, *mask
= NULL
;
1322 struct xtables_rule_match
*m
;
1324 #define SZ(x) XT_ALIGN(sizeof(struct x))
1326 #ifndef DISABLE_IPV6
1327 if (r
->h
->family
== FW3_FAMILY_V6
)
1331 for (m
= r
->matches
; m
; m
= m
->next
)
1332 s
+= SZ(ip6t_entry_match
) + m
->match
->size
;
1334 s
+= SZ(ip6t_entry_target
);
1336 s
+= r
->target
->size
;
1338 mask
= fw3_alloc(s
);
1339 memset(mask
, 0xFF, SZ(ip6t_entry
));
1340 p
= mask
+ SZ(ip6t_entry
);
1342 for (m
= r
->matches
; m
; m
= m
->next
)
1344 memset(p
, 0xFF, SZ(ip6t_entry_match
) + m
->match
->userspacesize
);
1345 p
+= SZ(ip6t_entry_match
) + m
->match
->size
;
1348 memset(p
, 0xFF, SZ(ip6t_entry_target
) + (r
->target
) ? r
->target
->userspacesize
: 0);
1355 for (m
= r
->matches
; m
; m
= m
->next
)
1356 s
+= SZ(ipt_entry_match
) + m
->match
->size
;
1358 s
+= SZ(ipt_entry_target
);
1360 s
+= r
->target
->size
;
1362 mask
= fw3_alloc(s
);
1363 memset(mask
, 0xFF, SZ(ipt_entry
));
1364 p
= mask
+ SZ(ipt_entry
);
1366 for (m
= r
->matches
; m
; m
= m
->next
)
1368 memset(p
, 0xFF, SZ(ipt_entry_match
) + m
->match
->userspacesize
);
1369 p
+= SZ(ipt_entry_match
) + m
->match
->size
;
1372 memset(p
, 0xFF, SZ(ipt_entry_target
) + (r
->target
) ? r
->target
->userspacesize
: 0);
1379 rule_build(struct fw3_ipt_rule
*r
)
1381 size_t s
, target_size
= (r
->target
) ? r
->target
->t
->u
.target_size
: 0;
1382 struct xtables_rule_match
*m
;
1384 #ifndef DISABLE_IPV6
1385 if (r
->h
->family
== FW3_FAMILY_V6
)
1387 struct ip6t_entry
*e6
;
1389 s
= XT_ALIGN(sizeof(struct ip6t_entry
));
1391 for (m
= r
->matches
; m
; m
= m
->next
)
1392 s
+= m
->match
->m
->u
.match_size
;
1394 e6
= fw3_alloc(s
+ target_size
);
1396 memcpy(e6
, &r
->e6
, sizeof(struct ip6t_entry
));
1398 e6
->target_offset
= s
;
1399 e6
->next_offset
= s
+ target_size
;
1403 for (m
= r
->matches
; m
; m
= m
->next
)
1405 memcpy(e6
->elems
+ s
, m
->match
->m
, m
->match
->m
->u
.match_size
);
1406 s
+= m
->match
->m
->u
.match_size
;
1410 memcpy(e6
->elems
+ s
, r
->target
->t
, target_size
);
1417 struct ipt_entry
*e
;
1419 s
= XT_ALIGN(sizeof(struct ipt_entry
));
1421 for (m
= r
->matches
; m
; m
= m
->next
)
1422 s
+= m
->match
->m
->u
.match_size
;
1424 e
= fw3_alloc(s
+ target_size
);
1426 memcpy(e
, &r
->e
, sizeof(struct ipt_entry
));
1428 e
->target_offset
= s
;
1429 e
->next_offset
= s
+ target_size
;
1433 for (m
= r
->matches
; m
; m
= m
->next
)
1435 memcpy(e
->elems
+ s
, m
->match
->m
, m
->match
->m
->u
.match_size
);
1436 s
+= m
->match
->m
->u
.match_size
;
1440 memcpy(e
->elems
+ s
, r
->target
->t
, target_size
);
1447 __fw3_ipt_rule_append(struct fw3_ipt_rule
*r
, bool repl
, const char *fmt
, ...)
1450 unsigned char *mask
;
1452 struct xtables_rule_match
*m
;
1453 struct xtables_match
*em
;
1454 struct xtables_target
*et
;
1455 struct xtables_globals
*g
;
1464 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
1467 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
1468 g
->opts
= g
->orig_opts
;
1475 em
= find_match(r
, "id");
1479 warn("fw3_ipt_rule_append(): Can't find match '%s'", "id");
1483 init_match(r
, em
, true);
1485 id
= FW3_ID_MAGIC
| (r
->id
& ~FW3_ID_MASK
);
1486 memcpy(em
->m
->data
, &id
, sizeof(id
));
1491 while ((optc
= getopt_long(r
->argc
, r
->argv
, "-:m:j:", g
->opts
,
1497 em
= find_match(r
, optarg
);
1501 warn("fw3_ipt_rule_append(): Can't find match '%s'", optarg
);
1505 init_match(r
, em
, true);
1509 et
= get_target(r
, optarg
);
1513 warn("fw3_ipt_rule_append(): Can't find target '%s'", optarg
);
1520 if ((optarg
[0] == '!') && (optarg
[1] == '\0'))
1527 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg
);
1531 if (parse_option(r
, optc
, inv
))
1539 for (m
= r
->matches
; m
; m
= m
->next
)
1540 xtables_option_mfcall(m
->match
);
1543 xtables_option_tfcall(r
->target
);
1545 rule
= rule_build(r
);
1547 #ifndef DISABLE_IPV6
1548 if (r
->h
->family
== FW3_FAMILY_V6
)
1552 mask
= rule_mask(r
);
1554 while (ip6tc_delete_entry(buf
, rule
, mask
, r
->h
->handle
))
1556 rule_print(r
, "-D", buf
);
1562 rule_print(r
, "-A", buf
);
1564 if (!ip6tc_append_entry(buf
, rule
, r
->h
->handle
))
1565 warn("ip6tc_append_entry(): %s", ip6tc_strerror(errno
));
1572 mask
= rule_mask(r
);
1574 while (iptc_delete_entry(buf
, rule
, mask
, r
->h
->handle
))
1576 rule_print(r
, "-D", buf
);
1582 rule_print(r
, "-A", buf
);
1584 if (!iptc_append_entry(buf
, rule
, r
->h
->handle
))
1585 warn("iptc_append_entry(): %s\n", iptc_strerror(errno
));
1591 for (i
= 1; i
< r
->argc
; i
++)
1596 xtables_rule_matches_free(&r
->matches
);
1603 /* reset all targets and matches */
1604 for (em
= xtables_matches
; em
; em
= em
->next
)
1607 for (et
= xtables_targets
; et
; et
= et
->next
)
1613 xtables_free_opts(1);
1616 struct fw3_ipt_rule
*
1617 fw3_ipt_rule_create(struct fw3_ipt_handle
*handle
, struct fw3_protocol
*proto
,
1618 struct fw3_device
*in
, struct fw3_device
*out
,
1619 struct fw3_address
*src
, struct fw3_address
*dest
)
1621 struct fw3_ipt_rule
*r
;
1623 r
= fw3_ipt_rule_new(handle
);
1625 fw3_ipt_rule_proto(r
, proto
);
1626 fw3_ipt_rule_in_out(r
, in
, out
);
1627 fw3_ipt_rule_src_dest(r
, src
, dest
);
1633 xtables_register_match(struct xtables_match
*me
)
1636 static struct xtables_match
**tmp
;
1638 if (!xext
.register_match
)
1639 xext
.register_match
= dlsym(RTLD_NEXT
, "xtables_register_match");
1641 if (!xext
.register_match
)
1644 xext
.register_match(me
);
1648 for (i
= 0; i
< xext
.mcount
; i
++)
1649 if (xext
.matches
[i
] == me
)
1652 tmp
= realloc(xext
.matches
, sizeof(me
) * (xext
.mcount
+ 1));
1658 xext
.matches
[xext
.mcount
++] = me
;
1663 xtables_register_target(struct xtables_target
*me
)
1666 static struct xtables_target
**tmp
;
1668 if (!xext
.register_target
)
1669 xext
.register_target
= dlsym(RTLD_NEXT
, "xtables_register_target");
1671 if (!xext
.register_target
)
1674 xext
.register_target(me
);
1678 for (i
= 0; i
< xext
.tcount
; i
++)
1679 if (xext
.targets
[i
] == me
)
1682 tmp
= realloc(xext
.targets
, sizeof(me
) * (xext
.tcount
+ 1));
1688 xext
.targets
[xext
.tcount
++] = me
;