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' },
25 { .name
= "append", .has_arg
= 1, .val
= 'A' },
29 static struct xtables_globals xtg
= {
31 .program_version
= "4",
32 .orig_opts
= base_opts
,
35 static struct xtables_globals xtg6
= {
37 .program_version
= "6",
38 .orig_opts
= base_opts
,
41 /* Required by certain extensions like SNAT and DNAT */
42 int kernel_version
= 0;
45 get_kernel_version(void)
47 static struct utsname uts
;
48 int x
= 0, y
= 0, z
= 0;
50 if (uname(&uts
) == -1)
51 sprintf(uts
.release
, "3.0.0");
53 sscanf(uts
.release
, "%d.%d.%d", &x
, &y
, &z
);
54 kernel_version
= 0x10000 * x
+ 0x100 * y
+ z
;
58 #define __ipt_module(x) libxt_##x##_init, libipt_##x##_init, libip6t_##x##_init,
60 static void fw3_init_extensions(void)
63 void (*initfuncs
[])(void) = { FW3_IPT_MODULES
};
65 for (i
= 0; i
< sizeof(initfuncs
)/sizeof(initfuncs
[0]); i
++)
70 struct fw3_ipt_handle
*
71 fw3_ipt_open(enum fw3_family family
, enum fw3_table table
)
73 struct fw3_ipt_handle
*h
;
75 h
= fw3_alloc(sizeof(*h
));
79 if (family
== FW3_FAMILY_V6
)
81 h
->family
= FW3_FAMILY_V6
;
83 h
->handle
= ip6tc_init(fw3_flag_names
[table
]);
85 xtables_set_params(&xtg6
);
86 xtables_set_nfproto(NFPROTO_IPV6
);
90 h
->family
= FW3_FAMILY_V4
;
92 h
->handle
= iptc_init(fw3_flag_names
[table
]);
94 xtables_set_params(&xtg
);
95 xtables_set_nfproto(NFPROTO_IPV4
);
104 xtables_pending_matches
= NULL
;
105 xtables_pending_targets
= NULL
;
107 xtables_matches
= NULL
;
108 xtables_targets
= NULL
;
110 fw3_init_extensions();
116 debug(struct fw3_ipt_handle
*h
, const char *fmt
, ...)
120 printf("%s -t %s ", (h
->family
== FW3_FAMILY_V6
) ? "ip6tables" : "iptables",
121 fw3_flag_names
[h
->table
]);
129 fw3_ipt_set_policy(struct fw3_ipt_handle
*h
, const char *chain
,
130 enum fw3_flag policy
)
133 debug(h
, "-P %s %s\n", chain
, fw3_flag_names
[policy
]);
135 if (h
->family
== FW3_FAMILY_V6
)
136 ip6tc_set_policy(chain
, fw3_flag_names
[policy
], NULL
, h
->handle
);
138 iptc_set_policy(chain
, fw3_flag_names
[policy
], NULL
, h
->handle
);
142 fw3_ipt_delete_chain(struct fw3_ipt_handle
*h
, const char *chain
)
146 debug(h
, "-F %s\n", chain
);
147 debug(h
, "-X %s\n", chain
);
150 if (h
->family
== FW3_FAMILY_V6
)
152 if (ip6tc_flush_entries(chain
, h
->handle
))
153 ip6tc_delete_chain(chain
, h
->handle
);
157 if (iptc_flush_entries(chain
, h
->handle
))
158 iptc_delete_chain(chain
, h
->handle
);
163 fw3_ipt_delete_rules(struct fw3_ipt_handle
*h
, const char *target
)
166 const struct ipt_entry
*e
;
167 const struct ip6t_entry
*e6
;
172 if (h
->family
== FW3_FAMILY_V6
)
174 for (chain
= ip6tc_first_chain(h
->handle
);
176 chain
= ip6tc_next_chain(h
->handle
))
181 for (num
= 0, e6
= ip6tc_first_rule(chain
, h
->handle
);
183 num
++, e6
= ip6tc_next_rule(e6
, h
->handle
))
185 t
= ip6tc_get_target(e6
, h
->handle
);
187 if (*t
&& !strcmp(t
, target
))
190 debug(h
, "-D %s %u\n", chain
, num
+ 1);
192 ip6tc_delete_num_entry(chain
, num
, h
->handle
);
202 for (chain
= iptc_first_chain(h
->handle
);
204 chain
= iptc_next_chain(h
->handle
))
209 for (num
= 0, e
= iptc_first_rule(chain
, h
->handle
);
211 num
++, e
= iptc_next_rule(e
, h
->handle
))
213 t
= iptc_get_target(e
, h
->handle
);
215 if (*t
&& !strcmp(t
, target
))
218 debug(h
, "-D %s %u\n", chain
, num
+ 1);
220 iptc_delete_num_entry(chain
, num
, h
->handle
);
231 fw3_ipt_create_chain(struct fw3_ipt_handle
*h
, const char *fmt
, ...)
237 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
241 debug(h
, "-N %s\n", buf
);
243 iptc_create_chain(buf
, h
->handle
);
247 fw3_ipt_flush(struct fw3_ipt_handle
*h
)
251 if (h
->family
== FW3_FAMILY_V6
)
253 for (chain
= ip6tc_first_chain(h
->handle
);
255 chain
= ip6tc_next_chain(h
->handle
))
257 ip6tc_flush_entries(chain
, h
->handle
);
260 for (chain
= ip6tc_first_chain(h
->handle
);
262 chain
= ip6tc_next_chain(h
->handle
))
264 ip6tc_delete_chain(chain
, h
->handle
);
269 for (chain
= iptc_first_chain(h
->handle
);
271 chain
= iptc_next_chain(h
->handle
))
273 iptc_flush_entries(chain
, h
->handle
);
276 for (chain
= iptc_first_chain(h
->handle
);
278 chain
= iptc_next_chain(h
->handle
))
280 iptc_delete_chain(chain
, h
->handle
);
286 fw3_ipt_commit(struct fw3_ipt_handle
*h
)
290 if (h
->family
== FW3_FAMILY_V6
)
292 rv
= ip6tc_commit(h
->handle
);
294 fprintf(stderr
, "ip6tc_commit(): %s\n", ip6tc_strerror(errno
));
298 rv
= iptc_commit(h
->handle
);
300 fprintf(stderr
, "iptc_commit(): %s\n", iptc_strerror(errno
));
306 struct fw3_ipt_rule
*
307 fw3_ipt_rule_new(struct fw3_ipt_handle
*h
)
309 struct fw3_ipt_rule
*r
;
311 r
= fw3_alloc(sizeof(*r
));
314 r
->argv
= fw3_alloc(sizeof(char *));
315 r
->argv
[r
->argc
++] = "fw3";
322 is_chain(struct fw3_ipt_handle
*h
, const char *name
)
324 if (h
->family
== FW3_FAMILY_V6
)
325 return ip6tc_is_chain(name
, h
->handle
);
327 return iptc_is_chain(name
, h
->handle
);
331 get_protoname(struct fw3_ipt_rule
*r
)
333 const struct xtables_pprot
*pp
;
336 for (pp
= xtables_chain_protos
; pp
->name
; pp
++)
337 if (pp
->num
== r
->protocol
)
338 return (char *)pp
->name
;
343 static struct xtables_match
*
344 find_match(struct fw3_ipt_rule
*r
, const char *name
)
346 return xtables_find_match(name
, XTF_TRY_LOAD
, &r
->matches
);
350 init_match(struct fw3_ipt_rule
*r
, struct xtables_match
*m
, bool no_clone
)
353 struct xtables_globals
*g
;
358 s
= XT_ALIGN(sizeof(struct xt_entry_match
)) + m
->size
;
362 fw3_xt_set_match_name(m
);
364 m
->m
->u
.user
.revision
= m
->revision
;
365 m
->m
->u
.match_size
= s
;
367 /* free previous userspace data */
368 fw3_xt_free_match_udata(m
);
373 /* don't merge options if no_clone is set and this match is a clone */
374 if (no_clone
&& (m
== m
->next
))
377 /* merge option table */
378 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
379 fw3_xt_merge_match_options(g
, m
);
383 need_protomatch(struct fw3_ipt_rule
*r
, const char *pname
)
388 if (!xtables_find_match(pname
, XTF_DONT_LOAD
, NULL
))
391 return !r
->protocol_loaded
;
394 static struct xtables_match
*
395 load_protomatch(struct fw3_ipt_rule
*r
)
397 const char *pname
= get_protoname(r
);
399 if (!need_protomatch(r
, pname
))
402 return find_match(r
, pname
);
405 static struct xtables_target
*
406 get_target(struct fw3_ipt_rule
*r
, const char *name
)
409 struct xtables_target
*t
;
410 struct xtables_globals
*g
;
412 bool chain
= is_chain(r
->h
, name
);
415 t
= xtables_find_target(XT_STANDARD_TARGET
, XTF_LOAD_MUST_SUCCEED
);
417 t
= xtables_find_target(name
, XTF_TRY_LOAD
);
422 s
= XT_ALIGN(sizeof(struct xt_entry_target
)) + t
->size
;
425 fw3_xt_set_target_name(t
, name
);
427 t
->t
->u
.user
.revision
= t
->revision
;
428 t
->t
->u
.target_size
= s
;
430 /* free previous userspace data */
431 fw3_xt_free_target_udata(t
);
436 /* merge option table */
437 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
438 fw3_xt_merge_target_options(g
, t
);
446 fw3_ipt_rule_proto(struct fw3_ipt_rule
*r
, struct fw3_protocol
*proto
)
450 if (!proto
|| proto
->any
)
453 pr
= proto
->protocol
;
455 if (r
->h
->family
== FW3_FAMILY_V6
)
460 r
->e6
.ipv6
.proto
= pr
;
461 r
->e6
.ipv6
.flags
|= IP6T_F_PROTO
;
464 r
->e6
.ipv6
.invflags
|= XT_INV_PROTO
;
471 r
->e
.ip
.invflags
|= XT_INV_PROTO
;
478 fw3_ipt_rule_in_out(struct fw3_ipt_rule
*r
,
479 struct fw3_device
*in
, struct fw3_device
*out
)
481 if (r
->h
->family
== FW3_FAMILY_V6
)
485 xtables_parse_interface(in
->name
, r
->e6
.ipv6
.iniface
,
486 r
->e6
.ipv6
.iniface_mask
);
489 r
->e6
.ipv6
.invflags
|= IP6T_INV_VIA_IN
;
492 if (out
&& !out
->any
)
494 xtables_parse_interface(out
->name
, r
->e6
.ipv6
.outiface
,
495 r
->e6
.ipv6
.outiface_mask
);
498 r
->e6
.ipv6
.invflags
|= IP6T_INV_VIA_OUT
;
505 xtables_parse_interface(in
->name
, r
->e
.ip
.iniface
,
506 r
->e
.ip
.iniface_mask
);
509 r
->e
.ip
.invflags
|= IPT_INV_VIA_IN
;
512 if (out
&& !out
->any
)
514 xtables_parse_interface(out
->name
, r
->e
.ip
.outiface
,
515 r
->e
.ip
.outiface_mask
);
518 r
->e
.ip
.invflags
|= IPT_INV_VIA_OUT
;
525 ip4prefix2mask(int prefix
, struct in_addr
*mask
)
527 mask
->s_addr
= htonl(~((1 << (32 - prefix
)) - 1));
531 ip6prefix2mask(int prefix
, struct in6_addr
*mask
)
533 char *p
= (char *)mask
;
537 memset(p
, 0xff, prefix
/ 8);
538 memset(p
+ (prefix
/ 8) + 1, 0, (128 - prefix
) / 8);
539 p
[prefix
/ 8] = 0xff << (8 - (prefix
& 7));
543 memset(mask
, 0, sizeof(*mask
));
548 fw3_ipt_rule_src_dest(struct fw3_ipt_rule
*r
,
549 struct fw3_address
*src
, struct fw3_address
*dest
)
553 if ((src
&& src
->range
) || (dest
&& dest
->range
))
555 fw3_ipt_rule_addarg(r
, false, "-m", "iprange");
562 fw3_ipt_rule_addarg(r
, src
->invert
, "--src-range",
563 fw3_address_to_string(src
, false));
565 else if (r
->h
->family
== FW3_FAMILY_V6
)
567 r
->e6
.ipv6
.src
= src
->address
.v6
;
568 ip6prefix2mask(src
->mask
, &r
->e6
.ipv6
.smsk
);
570 for (i
= 0; i
< 4; i
++)
571 r
->e6
.ipv6
.src
.s6_addr32
[i
] &= r
->e6
.ipv6
.smsk
.s6_addr32
[i
];
574 r
->e6
.ipv6
.invflags
|= IP6T_INV_SRCIP
;
578 r
->e
.ip
.src
= src
->address
.v4
;
579 ip4prefix2mask(src
->mask
, &r
->e
.ip
.smsk
);
581 r
->e
.ip
.src
.s_addr
&= r
->e
.ip
.smsk
.s_addr
;
584 r
->e
.ip
.invflags
|= IPT_INV_SRCIP
;
588 if (dest
&& dest
->set
)
592 fw3_ipt_rule_addarg(r
, dest
->invert
, "--dst-range",
593 fw3_address_to_string(dest
, false));
595 else if (r
->h
->family
== FW3_FAMILY_V6
)
597 r
->e6
.ipv6
.dst
= dest
->address
.v6
;
598 ip6prefix2mask(dest
->mask
, &r
->e6
.ipv6
.dmsk
);
600 for (i
= 0; i
< 4; i
++)
601 r
->e6
.ipv6
.dst
.s6_addr32
[i
] &= r
->e6
.ipv6
.dmsk
.s6_addr32
[i
];
604 r
->e6
.ipv6
.invflags
|= IP6T_INV_DSTIP
;
608 r
->e
.ip
.dst
= dest
->address
.v4
;
609 ip4prefix2mask(dest
->mask
, &r
->e
.ip
.dmsk
);
611 r
->e
.ip
.dst
.s_addr
&= r
->e
.ip
.dmsk
.s_addr
;
614 r
->e
.ip
.invflags
|= IPT_INV_DSTIP
;
620 fw3_ipt_rule_sport_dport(struct fw3_ipt_rule
*r
,
621 struct fw3_port
*sp
, struct fw3_port
*dp
)
623 char buf
[sizeof("65535:65535\0")];
625 if ((!sp
|| !sp
->set
) && (!dp
|| !dp
->set
))
628 if (!get_protoname(r
))
633 if (sp
->port_min
== sp
->port_max
)
634 sprintf(buf
, "%u", sp
->port_min
);
636 sprintf(buf
, "%u:%u", sp
->port_min
, sp
->port_max
);
638 fw3_ipt_rule_addarg(r
, sp
->invert
, "--sport", buf
);
643 if (dp
->port_min
== dp
->port_max
)
644 sprintf(buf
, "%u", dp
->port_min
);
646 sprintf(buf
, "%u:%u", dp
->port_min
, dp
->port_max
);
648 fw3_ipt_rule_addarg(r
, dp
->invert
, "--dport", buf
);
653 fw3_ipt_rule_mac(struct fw3_ipt_rule
*r
, struct fw3_mac
*mac
)
658 fw3_ipt_rule_addarg(r
, false, "-m", "mac");
659 fw3_ipt_rule_addarg(r
, mac
->invert
, "--mac-source", ether_ntoa(&mac
->mac
));
663 fw3_ipt_rule_icmptype(struct fw3_ipt_rule
*r
, struct fw3_icmptype
*icmp
)
665 char buf
[sizeof("255/255\0")];
670 if (r
->h
->family
== FW3_FAMILY_V6
)
672 if (icmp
->code6_min
== 0 && icmp
->code6_max
== 0xFF)
673 sprintf(buf
, "%u", icmp
->type6
);
675 sprintf(buf
, "%u/%u", icmp
->type6
, icmp
->code6_min
);
677 fw3_ipt_rule_addarg(r
, icmp
->invert
, "--icmpv6-type", buf
);
681 if (icmp
->code_min
== 0 && icmp
->code_max
== 0xFF)
682 sprintf(buf
, "%u", icmp
->type
);
684 sprintf(buf
, "%u/%u", icmp
->type
, icmp
->code_min
);
686 fw3_ipt_rule_addarg(r
, icmp
->invert
, "--icmp-type", buf
);
691 fw3_ipt_rule_limit(struct fw3_ipt_rule
*r
, struct fw3_limit
*limit
)
693 char buf
[sizeof("-4294967296/second\0")];
695 if (!limit
|| limit
->rate
<= 0)
698 fw3_ipt_rule_addarg(r
, false, "-m", "limit");
700 sprintf(buf
, "%u/%s", limit
->rate
, fw3_limit_units
[limit
->unit
]);
701 fw3_ipt_rule_addarg(r
, limit
->invert
, "--limit", buf
);
703 if (limit
->burst
> 0)
705 sprintf(buf
, "%u", limit
->burst
);
706 fw3_ipt_rule_addarg(r
, limit
->invert
, "--limit-burst", buf
);
711 fw3_ipt_rule_ipset(struct fw3_ipt_rule
*r
, struct fw3_ipset
*ipset
,
714 char buf
[sizeof("dst,dst,dst\0")];
717 struct fw3_ipset_datatype
*type
;
722 list_for_each_entry(type
, &ipset
->datatypes
, list
)
727 p
+= sprintf(p
, "%s", type
->dest
? "dst" : "src");
730 fw3_ipt_rule_addarg(r
, false, "-m", "set");
732 fw3_ipt_rule_addarg(r
, invert
, "--match-set",
733 ipset
->external
? ipset
->external
: ipset
->name
);
735 fw3_ipt_rule_addarg(r
, false, buf
, NULL
);
739 fw3_ipt_rule_time(struct fw3_ipt_rule
*r
, struct fw3_time
*time
)
742 struct tm empty
= { 0 };
744 char buf
[84]; /* sizeof("1,2,3,...,30,31\0") */
747 bool d1
= memcmp(&time
->datestart
, &empty
, sizeof(empty
));
748 bool d2
= memcmp(&time
->datestop
, &empty
, sizeof(empty
));
750 if (!d1
&& !d2
&& !time
->timestart
&& !time
->timestop
&&
751 !(time
->monthdays
& 0xFFFFFFFE) && !(time
->weekdays
& 0xFE))
756 fw3_ipt_rule_addarg(r
, false, "-m", "time");
759 fw3_ipt_rule_addarg(r
, false, "--utc", NULL
);
763 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestart
);
764 fw3_ipt_rule_addarg(r
, false, "--datestart", buf
);
769 strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S", &time
->datestop
);
770 fw3_ipt_rule_addarg(r
, false, "--datestop", buf
);
775 sprintf(buf
, "%02d:%02d:%02d",
776 time
->timestart
/ 3600,
777 time
->timestart
% 3600 / 60,
778 time
->timestart
% 60);
780 fw3_ipt_rule_addarg(r
, false, "--timestart", buf
);
785 sprintf(buf
, "%02d:%02d:%02d",
786 time
->timestop
/ 3600,
787 time
->timestop
% 3600 / 60,
788 time
->timestop
% 60);
790 fw3_ipt_rule_addarg(r
, false, "--timestop", buf
);
793 if (time
->monthdays
& 0xFFFFFFFE)
795 for (i
= 1, p
= buf
; i
< 32; i
++)
797 if (hasbit(time
->monthdays
, i
))
802 p
+= sprintf(p
, "%u", i
);
806 fw3_ipt_rule_addarg(r
, hasbit(time
->monthdays
, 0), "--monthdays", buf
);
809 if (time
->weekdays
& 0xFE)
811 for (i
= 1, p
= buf
; i
< 8; i
++)
813 if (hasbit(time
->weekdays
, i
))
818 p
+= sprintf(p
, "%u", i
);
822 fw3_ipt_rule_addarg(r
, hasbit(time
->weekdays
, 0), "--weekdays", buf
);
827 fw3_ipt_rule_mark(struct fw3_ipt_rule
*r
, struct fw3_mark
*mark
)
829 char buf
[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
831 if (!mark
|| !mark
->set
)
834 if (mark
->mask
< 0xFFFFFFFF)
835 sprintf(buf
, "0x%x/0x%x", mark
->mark
, mark
->mask
);
837 sprintf(buf
, "0x%x", mark
->mark
);
839 fw3_ipt_rule_addarg(r
, false, "-m", "mark");
840 fw3_ipt_rule_addarg(r
, mark
->invert
, "--mark", buf
);
844 fw3_ipt_rule_comment(struct fw3_ipt_rule
*r
, const char *fmt
, ...)
853 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
856 fw3_ipt_rule_addarg(r
, false, "-m", "comment");
857 fw3_ipt_rule_addarg(r
, false, "--comment", buf
);
861 fw3_ipt_rule_extra(struct fw3_ipt_rule
*r
, const char *extra
)
865 if (!extra
|| !*extra
)
868 s
= fw3_strdup(extra
);
870 for (p
= strtok(s
, " \t"); p
; p
= strtok(NULL
, " \t"))
872 tmp
= realloc(r
->argv
, (r
->argc
+ 1) * sizeof(*r
->argv
));
878 r
->argv
[r
->argc
++] = fw3_strdup(p
);
885 rule_print6(struct ip6t_entry
*e
)
887 char buf
[INET6_ADDRSTRLEN
];
890 if (e
->ipv6
.flags
& IP6T_F_PROTO
)
892 if (e
->ipv6
.flags
& XT_INV_PROTO
)
895 pname
= get_protoname(container_of(e
, struct fw3_ipt_rule
, e6
));
898 printf(" -p %s", pname
);
900 printf(" -p %u", e
->ipv6
.proto
);
903 if (e
->ipv6
.iniface
[0])
905 if (e
->ipv6
.flags
& IP6T_INV_VIA_IN
)
908 printf(" -i %s", e
->ipv6
.iniface
);
911 if (e
->ipv6
.outiface
[0])
913 if (e
->ipv6
.flags
& IP6T_INV_VIA_OUT
)
916 printf(" -o %s", e
->ipv6
.outiface
);
919 if (memcmp(&e
->ipv6
.src
, &in6addr_any
, sizeof(struct in6_addr
)))
921 if (e
->ipv6
.flags
& IP6T_INV_SRCIP
)
924 printf(" -s %s/%u", inet_ntop(AF_INET6
, &e
->ipv6
.src
, buf
, sizeof(buf
)),
925 xtables_ip6mask_to_cidr(&e
->ipv6
.smsk
));
928 if (memcmp(&e
->ipv6
.dst
, &in6addr_any
, sizeof(struct in6_addr
)))
930 if (e
->ipv6
.flags
& IP6T_INV_DSTIP
)
933 printf(" -d %s/%u", inet_ntop(AF_INET6
, &e
->ipv6
.dst
, buf
, sizeof(buf
)),
934 xtables_ip6mask_to_cidr(&e
->ipv6
.dmsk
));
939 rule_print4(struct ipt_entry
*e
)
941 struct in_addr in_zero
= { 0 };
942 char buf
[sizeof("255.255.255.255\0")];
947 if (e
->ip
.flags
& XT_INV_PROTO
)
950 pname
= get_protoname(container_of(e
, struct fw3_ipt_rule
, e
));
953 printf(" -p %s", pname
);
955 printf(" -p %u", e
->ip
.proto
);
958 if (e
->ip
.iniface
[0])
960 if (e
->ip
.flags
& IPT_INV_VIA_IN
)
963 printf(" -i %s", e
->ip
.iniface
);
966 if (e
->ip
.outiface
[0])
968 if (e
->ip
.flags
& IPT_INV_VIA_OUT
)
971 printf(" -o %s", e
->ip
.outiface
);
974 if (memcmp(&e
->ip
.src
, &in_zero
, sizeof(struct in_addr
)))
976 if (e
->ip
.flags
& IPT_INV_SRCIP
)
979 printf(" -s %s/%u", inet_ntop(AF_INET
, &e
->ip
.src
, buf
, sizeof(buf
)),
980 xtables_ipmask_to_cidr(&e
->ip
.smsk
));
983 if (memcmp(&e
->ip
.dst
, &in_zero
, sizeof(struct in_addr
)))
985 if (e
->ip
.flags
& IPT_INV_DSTIP
)
988 printf(" -d %s/%u", inet_ntop(AF_INET
, &e
->ip
.dst
, buf
, sizeof(buf
)),
989 xtables_ipmask_to_cidr(&e
->ip
.dmsk
));
994 rule_print(struct fw3_ipt_rule
*r
, const char *chain
)
996 struct xtables_rule_match
*rm
;
997 struct xtables_match
*m
;
998 struct xtables_target
*t
;
1000 debug(r
->h
, "-A %s", chain
);
1002 if (r
->h
->family
== FW3_FAMILY_V6
)
1003 rule_print6(&r
->e6
);
1007 for (rm
= r
->matches
; rm
; rm
= rm
->next
)
1010 printf(" -m %s", fw3_xt_get_match_name(m
));
1013 m
->save(&r
->e
.ip
, m
->m
);
1019 printf(" -j %s", fw3_xt_get_target_name(t
));
1022 t
->save(&r
->e
.ip
, t
->t
);
1029 parse_option(struct fw3_ipt_rule
*r
, int optc
, bool inv
)
1031 struct xtables_rule_match
*m
;
1032 struct xtables_match
*em
;
1034 /* is a target option */
1035 if (r
->target
&& fw3_xt_has_target_parse(r
->target
) &&
1036 optc
>= r
->target
->option_offset
&&
1037 optc
< (r
->target
->option_offset
+ 256))
1039 xtables_option_tpcall(optc
, r
->argv
, inv
, r
->target
, &r
->e
);
1043 /* try to dispatch argument to one of the match parsers */
1044 for (m
= r
->matches
; m
; m
= m
->next
)
1048 if (m
->completed
|| !fw3_xt_has_match_parse(em
))
1051 if (optc
< em
->option_offset
||
1052 optc
>= (em
->option_offset
+ 256))
1055 xtables_option_mpcall(optc
, r
->argv
, inv
, em
, &r
->e
);
1059 /* unhandled option, might belong to a protocol match */
1060 if ((em
= load_protomatch(r
)) != NULL
)
1062 init_match(r
, em
, false);
1064 r
->protocol_loaded
= true;
1071 fprintf(stderr
, "parse_option(): option '%s' needs argument\n",
1075 fprintf(stderr
, "parse_option(): unknown option '%s'\n",
1082 fw3_ipt_rule_addarg(struct fw3_ipt_rule
*r
, bool inv
,
1083 const char *k
, const char *v
)
1091 n
= inv
+ !!k
+ !!v
;
1092 tmp
= realloc(r
->argv
, (r
->argc
+ n
) * sizeof(*tmp
));
1100 r
->argv
[r
->argc
++] = fw3_strdup("!");
1102 r
->argv
[r
->argc
++] = fw3_strdup(k
);
1105 r
->argv
[r
->argc
++] = fw3_strdup(v
);
1109 fw3_ipt_rule_append(struct fw3_ipt_rule
*r
, const char *fmt
, ...)
1112 struct xtables_rule_match
*m
;
1113 struct xtables_match
*em
;
1114 struct xtables_target
*et
;
1115 struct xtables_globals
*g
;
1116 struct ipt_entry
*e
;
1117 struct ip6t_entry
*e6
;
1125 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
1128 g
= (r
->h
->family
== FW3_FAMILY_V6
) ? &xtg6
: &xtg
;
1129 g
->opts
= g
->orig_opts
;
1134 while ((optc
= getopt_long(r
->argc
, r
->argv
, "m:j:", g
->opts
, NULL
)) != -1)
1139 em
= find_match(r
, optarg
);
1143 fprintf(stderr
, "fw3_ipt_rule_append(): Can't find match '%s'\n", optarg
);
1147 init_match(r
, em
, true);
1151 et
= get_target(r
, optarg
);
1155 fprintf(stderr
, "fw3_ipt_rule_append(): Can't find target '%s'\n", optarg
);
1162 if ((optarg
[0] == '!') && (optarg
[1] == '\0'))
1168 fprintf(stderr
, "fw3_ipt_rule_append(): Bad argument '%s'\n", optarg
);
1172 if (parse_option(r
, optc
, inv
))
1180 for (m
= r
->matches
; m
; m
= m
->next
)
1181 xtables_option_mfcall(m
->match
);
1184 xtables_option_tfcall(r
->target
);
1189 if (r
->h
->family
== FW3_FAMILY_V6
)
1191 s
= XT_ALIGN(sizeof(struct ip6t_entry
));
1193 for (m
= r
->matches
; m
; m
= m
->next
)
1194 s
+= m
->match
->m
->u
.match_size
;
1196 e6
= fw3_alloc(s
+ r
->target
->t
->u
.target_size
);
1198 memcpy(e6
, &r
->e6
, sizeof(struct ip6t_entry
));
1200 e6
->target_offset
= s
;
1201 e6
->next_offset
= s
+ r
->target
->t
->u
.target_size
;
1205 for (m
= r
->matches
; m
; m
= m
->next
)
1207 memcpy(e6
->elems
+ s
, m
->match
->m
, m
->match
->m
->u
.match_size
);
1208 s
+= m
->match
->m
->u
.match_size
;
1211 memcpy(e6
->elems
+ s
, r
->target
->t
, r
->target
->t
->u
.target_size
);
1212 ip6tc_append_entry(buf
, e6
, r
->h
->handle
);
1217 s
= XT_ALIGN(sizeof(struct ipt_entry
));
1219 for (m
= r
->matches
; m
; m
= m
->next
)
1220 s
+= m
->match
->m
->u
.match_size
;
1222 e
= fw3_alloc(s
+ r
->target
->t
->u
.target_size
);
1224 memcpy(e
, &r
->e
, sizeof(struct ipt_entry
));
1226 e
->target_offset
= s
;
1227 e
->next_offset
= s
+ r
->target
->t
->u
.target_size
;
1231 for (m
= r
->matches
; m
; m
= m
->next
)
1233 memcpy(e
->elems
+ s
, m
->match
->m
, m
->match
->m
->u
.match_size
);
1234 s
+= m
->match
->m
->u
.match_size
;
1237 memcpy(e
->elems
+ s
, r
->target
->t
, r
->target
->t
->u
.target_size
);
1239 if (!iptc_append_entry(buf
, e
, r
->h
->handle
))
1240 fprintf(stderr
, "iptc_append_entry(): %s\n", iptc_strerror(errno
));
1246 for (i
= 1; i
< r
->argc
; i
++)
1251 xtables_rule_matches_free(&r
->matches
);
1258 /* reset all targets and matches */
1259 for (em
= xtables_matches
; em
; em
= em
->next
)
1262 for (et
= xtables_targets
; et
; et
= et
->next
)
1268 xtables_free_opts(1);
1271 struct fw3_ipt_rule
*
1272 fw3_ipt_rule_create(struct fw3_ipt_handle
*handle
, struct fw3_protocol
*proto
,
1273 struct fw3_device
*in
, struct fw3_device
*out
,
1274 struct fw3_address
*src
, struct fw3_address
*dest
)
1276 struct fw3_ipt_rule
*r
;
1278 r
= fw3_ipt_rule_new(handle
);
1280 fw3_ipt_rule_proto(r
, proto
);
1281 fw3_ipt_rule_in_out(r
, in
, out
);
1282 fw3_ipt_rule_src_dest(r
, src
, dest
);