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>
45 /* xtables interface */
46 #if (XTABLES_VERSION_CODE >= 10)
47 # include "xtables-10.h"
48 #elif (XTABLES_VERSION_CODE == 5)
49 # include "xtables-5.h"
51 # error "Unsupported xtables version"
58 struct fw3_ipt_handle *h;
65 struct xtables_rule_match *matches;
66 struct xtables_target *target;
75 static struct option base_opts[] = {
76 { .name = "match", .has_arg = 1, .val = 'm' },
77 { .name = "jump", .has_arg = 1, .val = 'j' },
81 static struct xtables_globals xtg = {
83 .program_version = "4",
84 .orig_opts = base_opts,
85 #if XTABLES_VERSION_CODE > 10
86 .compat_rev = xtables_compatible_revision,
90 static struct xtables_globals xtg6 = {
92 .program_version = "6",
93 .orig_opts = base_opts,
94 #if XTABLES_VERSION_CODE > 10
95 .compat_rev = xtables_compatible_revision,
102 struct xtables_match **matches;
103 struct xtables_target **targets;
104 void (*register_match)(struct xtables_match *);
105 void (*register_target)(struct xtables_target *);
109 /* Required by certain extensions like SNAT and DNAT */
110 int kernel_version = 0;
113 get_kernel_version(void)
115 static struct utsname uts;
116 int x = 0, y = 0, z = 0;
118 if (uname(&uts) == -1)
119 sprintf(uts.release, "3.0.0");
121 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
122 kernel_version = 0x10000 * x + 0x100 * y + z;
125 static void fw3_init_extensions(void)
135 struct fw3_ipt_handle *
136 fw3_ipt_open(enum fw3_family family, enum fw3_table table)
139 struct fw3_ipt_handle *h;
141 h = fw3_alloc(sizeof(*h));
145 if (family == FW3_FAMILY_V6)
148 h->family = FW3_FAMILY_V6;
150 h->handle = ip6tc_init(fw3_flag_names[table]);
152 xtables_set_params(&xtg6);
153 xtables_set_nfproto(NFPROTO_IPV6);
158 h->family = FW3_FAMILY_V4;
160 h->handle = iptc_init(fw3_flag_names[table]);
162 xtables_set_params(&xtg);
163 xtables_set_nfproto(NFPROTO_IPV4);
173 fw3_init_extensions();
175 if (xext.register_match)
176 for (i = 0; i < xext.mcount; i++)
177 xext.register_match(xext.matches[i]);
179 if (xext.register_target)
180 for (i = 0; i < xext.tcount; i++)
181 xext.register_target(xext.targets[i]);
187 debug(struct fw3_ipt_handle *h, const char *fmt, ...)
191 printf("%s -t %s ", (h->family == FW3_FAMILY_V6) ? "ip6tables" : "iptables",
192 fw3_flag_names[h->table]);
200 fw3_ipt_set_policy(struct fw3_ipt_handle *h, const char *chain,
201 enum fw3_flag policy)
204 debug(h, "-P %s %s\n", chain, fw3_flag_names[policy]);
207 if (h->family == FW3_FAMILY_V6)
208 ip6tc_set_policy(chain, fw3_flag_names[policy], NULL, h->handle);
211 iptc_set_policy(chain, fw3_flag_names[policy], NULL, h->handle);
215 fw3_ipt_flush_chain(struct fw3_ipt_handle *h, const char *chain)
218 debug(h, "-F %s\n", chain);
221 if (h->family == FW3_FAMILY_V6)
222 ip6tc_flush_entries(chain, h->handle);
225 iptc_flush_entries(chain, h->handle);
229 delete_rules(struct fw3_ipt_handle *h, const char *target)
232 const struct ipt_entry *e;
238 if (h->family == FW3_FAMILY_V6)
240 for (chain = ip6tc_first_chain(h->handle);
242 chain = ip6tc_next_chain(h->handle))
247 const struct ip6t_entry *e6;
248 for (num = 0, e6 = ip6tc_first_rule(chain, h->handle);
250 num++, e6 = ip6tc_next_rule(e6, h->handle))
252 t = ip6tc_get_target(e6, h->handle);
254 if (*t && !strcmp(t, target))
257 debug(h, "-D %s %u\n", chain, num + 1);
259 ip6tc_delete_num_entry(chain, num, h->handle);
270 for (chain = iptc_first_chain(h->handle);
272 chain = iptc_next_chain(h->handle))
277 for (num = 0, e = iptc_first_rule(chain, h->handle);
279 num++, e = iptc_next_rule(e, h->handle))
281 t = iptc_get_target(e, h->handle);
283 if (*t && !strcmp(t, target))
286 debug(h, "-D %s %u\n", chain, num + 1);
288 iptc_delete_num_entry(chain, num, h->handle);
299 fw3_ipt_delete_chain(struct fw3_ipt_handle *h, const char *chain)
301 delete_rules(h, chain);
304 debug(h, "-X %s\n", chain);
307 if (h->family == FW3_FAMILY_V6)
308 ip6tc_delete_chain(chain, h->handle);
311 iptc_delete_chain(chain, h->handle);
315 has_rule_tag(const void *base, unsigned int start, unsigned int end)
318 const struct xt_entry_match *em;
320 for (i = start; i < end; i += em->u.match_size)
324 if (strcmp(em->u.user.name, "comment"))
327 if (!memcmp(em->data, "!fw3", 4))
335 fw3_ipt_delete_id_rules(struct fw3_ipt_handle *h, const char *chain)
338 const struct ipt_entry *e;
342 if (h->family == FW3_FAMILY_V6)
344 if (!ip6tc_is_chain(chain, h->handle))
350 const struct ip6t_entry *e6;
351 for (num = 0, e6 = ip6tc_first_rule(chain, h->handle);
353 num++, e6 = ip6tc_next_rule(e6, h->handle))
355 if (has_rule_tag(e6, sizeof(*e6), e6->target_offset))
358 debug(h, "-D %s %u\n", chain, num + 1);
360 ip6tc_delete_num_entry(chain, num, h->handle);
370 if (!iptc_is_chain(chain, h->handle))
376 for (num = 0, e = iptc_first_rule(chain, h->handle);
378 num++, e = iptc_next_rule(e, h->handle))
380 if (has_rule_tag(e, sizeof(*e), e->target_offset))
383 debug(h, "-D %s %u\n", chain, num + 1);
385 iptc_delete_num_entry(chain, num, h->handle);
395 fw3_ipt_create_chain(struct fw3_ipt_handle *h, const char *fmt, ...)
401 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
405 debug(h, "-N %s\n", buf);
407 iptc_create_chain(buf, h->handle);
411 fw3_ipt_flush(struct fw3_ipt_handle *h)
416 if (h->family == FW3_FAMILY_V6)
418 for (chain = ip6tc_first_chain(h->handle);
420 chain = ip6tc_next_chain(h->handle))
422 ip6tc_flush_entries(chain, h->handle);
425 for (chain = ip6tc_first_chain(h->handle);
427 chain = ip6tc_next_chain(h->handle))
429 ip6tc_delete_chain(chain, h->handle);
435 for (chain = iptc_first_chain(h->handle);
437 chain = iptc_next_chain(h->handle))
439 iptc_flush_entries(chain, h->handle);
442 for (chain = iptc_first_chain(h->handle);
444 chain = iptc_next_chain(h->handle))
446 iptc_delete_chain(chain, h->handle);
452 chain_is_empty(struct fw3_ipt_handle *h, const char *chain)
455 if (h->family == FW3_FAMILY_V6)
456 return (!ip6tc_builtin(chain, h->handle) &&
457 !ip6tc_first_rule(chain, h->handle));
460 return (!iptc_builtin(chain, h->handle) &&
461 !iptc_first_rule(chain, h->handle));
465 fw3_ipt_gc(struct fw3_ipt_handle *h)
471 if (h->family == FW3_FAMILY_V6)
476 for (chain = ip6tc_first_chain(h->handle);
478 chain = ip6tc_next_chain(h->handle))
480 if (!chain_is_empty(h, chain))
483 fw3_ipt_delete_chain(h, chain);
495 for (chain = iptc_first_chain(h->handle);
497 chain = iptc_next_chain(h->handle))
499 warn("C=%s\n", chain);
501 if (!chain_is_empty(h, chain))
504 warn("D=%s\n", chain);
506 fw3_ipt_delete_chain(h, chain);
515 fw3_ipt_commit(struct fw3_ipt_handle *h)
520 if (h->family == FW3_FAMILY_V6)
522 rv = ip6tc_commit(h->handle);
524 warn("ip6tc_commit(): %s", ip6tc_strerror(errno));
529 rv = iptc_commit(h->handle);
531 warn("iptc_commit(): %s", iptc_strerror(errno));
536 fw3_ipt_close(struct fw3_ipt_handle *h)
541 struct fw3_ipt_rule *
542 fw3_ipt_rule_new(struct fw3_ipt_handle *h)
544 struct fw3_ipt_rule *r;
546 r = fw3_alloc(sizeof(*r));
549 r->argv = fw3_alloc(sizeof(char *));
550 r->argv[r->argc++] = "fw3";
557 is_chain(struct fw3_ipt_handle *h, const char *name)
560 if (h->family == FW3_FAMILY_V6)
561 return ip6tc_is_chain(name, h->handle);
564 return iptc_is_chain(name, h->handle);
568 get_protoname(struct fw3_ipt_rule *r)
570 const struct xtables_pprot *pp;
573 for (pp = xtables_chain_protos; pp->name; pp++)
574 if (pp->num == r->protocol)
575 return (char *)pp->name;
580 static struct xtables_match *
581 find_match(struct fw3_ipt_rule *r, const char *name)
583 struct xtables_match *m;
586 m = xtables_find_match(name, XTF_TRY_LOAD, &r->matches);
593 init_match(struct fw3_ipt_rule *r, struct xtables_match *m, bool no_clone)
596 struct xtables_globals *g;
601 s = XT_ALIGN(sizeof(struct xt_entry_match)) + m->size;
605 fw3_xt_set_match_name(m);
607 m->m->u.user.revision = m->revision;
608 m->m->u.match_size = s;
610 /* free previous userspace data */
611 fw3_xt_free_match_udata(m);
616 /* don't merge options if no_clone is set and this match is a clone */
617 if (no_clone && (m == m->next))
620 /* merge option table */
621 g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
622 fw3_xt_merge_match_options(g, m);
626 need_protomatch(struct fw3_ipt_rule *r, const char *pname)
631 if (!xtables_find_match(pname, XTF_DONT_LOAD, NULL))
634 return !r->protocol_loaded;
637 static struct xtables_match *
638 load_protomatch(struct fw3_ipt_rule *r)
640 const char *pname = get_protoname(r);
642 if (!need_protomatch(r, pname))
645 return find_match(r, pname);
648 static struct xtables_target *
649 find_target(struct fw3_ipt_rule *r, const char *name)
651 struct xtables_target *t;
655 if (is_chain(r->h, name))
656 t = xtables_find_target(XT_STANDARD_TARGET, XTF_TRY_LOAD);
658 t = xtables_find_target(name, XTF_TRY_LOAD);
665 static struct xtables_target *
666 get_target(struct fw3_ipt_rule *r, const char *name)
669 struct xtables_target *t;
670 struct xtables_globals *g;
672 t = find_target(r, name);
677 s = XT_ALIGN(sizeof(struct xt_entry_target)) + t->size;
680 fw3_xt_set_target_name(t, name);
682 t->t->u.user.revision = t->revision;
683 t->t->u.target_size = s;
685 /* free previous userspace data */
686 fw3_xt_free_target_udata(t);
691 /* merge option table */
692 g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
693 fw3_xt_merge_target_options(g, t);
701 fw3_ipt_rule_proto(struct fw3_ipt_rule *r, struct fw3_protocol *proto)
705 if (!proto || proto->any)
708 pr = proto->protocol;
711 if (r->h->family == FW3_FAMILY_V6)
716 r->e6.ipv6.proto = pr;
717 r->e6.ipv6.flags |= IP6T_F_PROTO;
720 r->e6.ipv6.invflags |= XT_INV_PROTO;
728 r->e.ip.invflags |= XT_INV_PROTO;
735 fw3_ipt_rule_in_out(struct fw3_ipt_rule *r,
736 struct fw3_device *in, struct fw3_device *out)
739 if (r->h->family == FW3_FAMILY_V6)
743 xtables_parse_interface(in->name, r->e6.ipv6.iniface,
744 r->e6.ipv6.iniface_mask);
747 r->e6.ipv6.invflags |= IP6T_INV_VIA_IN;
750 if (out && !out->any)
752 xtables_parse_interface(out->name, r->e6.ipv6.outiface,
753 r->e6.ipv6.outiface_mask);
756 r->e6.ipv6.invflags |= IP6T_INV_VIA_OUT;
764 xtables_parse_interface(in->name, r->e.ip.iniface,
765 r->e.ip.iniface_mask);
768 r->e.ip.invflags |= IPT_INV_VIA_IN;
771 if (out && !out->any)
773 xtables_parse_interface(out->name, r->e.ip.outiface,
774 r->e.ip.outiface_mask);
777 r->e.ip.invflags |= IPT_INV_VIA_OUT;
784 fw3_ipt_rule_src_dest(struct fw3_ipt_rule *r,
785 struct fw3_address *src, struct fw3_address *dest)
787 if ((src && src->range) || (dest && dest->range))
789 fw3_ipt_rule_addarg(r, false, "-m", "iprange");
796 fw3_ipt_rule_addarg(r, src->invert, "--src-range",
797 fw3_address_to_string(src, false, false));
800 else if (r->h->family == FW3_FAMILY_V6)
802 r->e6.ipv6.src = src->address.v6;
803 r->e6.ipv6.smsk = src->mask.v6;
806 for (i = 0; i < 4; i++)
807 r->e6.ipv6.src.s6_addr32[i] &= r->e6.ipv6.smsk.s6_addr32[i];
810 r->e6.ipv6.invflags |= IP6T_INV_SRCIP;
815 r->e.ip.src = src->address.v4;
816 r->e.ip.smsk = src->mask.v4;
818 r->e.ip.src.s_addr &= r->e.ip.smsk.s_addr;
821 r->e.ip.invflags |= IPT_INV_SRCIP;
825 if (dest && dest->set)
829 fw3_ipt_rule_addarg(r, dest->invert, "--dst-range",
830 fw3_address_to_string(dest, false, false));
833 else if (r->h->family == FW3_FAMILY_V6)
835 r->e6.ipv6.dst = dest->address.v6;
836 r->e6.ipv6.dmsk = dest->mask.v6;
839 for (i = 0; i < 4; i++)
840 r->e6.ipv6.dst.s6_addr32[i] &= r->e6.ipv6.dmsk.s6_addr32[i];
843 r->e6.ipv6.invflags |= IP6T_INV_DSTIP;
848 r->e.ip.dst = dest->address.v4;
849 r->e.ip.dmsk = dest->mask.v4;
851 r->e.ip.dst.s_addr &= r->e.ip.dmsk.s_addr;
854 r->e.ip.invflags |= IPT_INV_DSTIP;
860 fw3_ipt_rule_sport_dport(struct fw3_ipt_rule *r,
861 struct fw3_port *sp, struct fw3_port *dp)
863 char buf[sizeof("65535:65535\0")];
865 if ((!sp || !sp->set) && (!dp || !dp->set))
868 if (!get_protoname(r))
873 if (sp->port_min == sp->port_max)
874 sprintf(buf, "%u", sp->port_min);
876 sprintf(buf, "%u:%u", sp->port_min, sp->port_max);
878 fw3_ipt_rule_addarg(r, sp->invert, "--sport", buf);
883 if (dp->port_min == dp->port_max)
884 sprintf(buf, "%u", dp->port_min);
886 sprintf(buf, "%u:%u", dp->port_min, dp->port_max);
888 fw3_ipt_rule_addarg(r, dp->invert, "--dport", buf);
893 fw3_ipt_rule_device(struct fw3_ipt_rule *r, const char *device, bool out)
896 struct fw3_device dev = { .any = false };
897 strncpy(dev.name, device, sizeof(dev.name) - 1);
898 fw3_ipt_rule_in_out(r, (out) ? NULL : &dev, (out) ? &dev : NULL);
903 fw3_ipt_rule_mac(struct fw3_ipt_rule *r, struct fw3_mac *mac)
905 char buf[sizeof("ff:ff:ff:ff:ff:ff\0")];
906 uint8_t *addr = mac->mac.ether_addr_octet;
911 sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
912 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
914 fw3_ipt_rule_addarg(r, false, "-m", "mac");
915 fw3_ipt_rule_addarg(r, mac->invert, "--mac-source", buf);
919 fw3_ipt_rule_icmptype(struct fw3_ipt_rule *r, struct fw3_icmptype *icmp)
921 char buf[sizeof("255/255\0")];
927 if (r->h->family == FW3_FAMILY_V6)
929 if (icmp->code6_min == 0 && icmp->code6_max == 0xFF)
930 sprintf(buf, "%u", icmp->type6);
932 sprintf(buf, "%u/%u", icmp->type6, icmp->code6_min);
934 fw3_ipt_rule_addarg(r, icmp->invert, "--icmpv6-type", buf);
939 if (icmp->code_min == 0 && icmp->code_max == 0xFF)
940 sprintf(buf, "%u", icmp->type);
942 sprintf(buf, "%u/%u", icmp->type, icmp->code_min);
944 fw3_ipt_rule_addarg(r, icmp->invert, "--icmp-type", buf);
949 fw3_ipt_rule_limit(struct fw3_ipt_rule *r, struct fw3_limit *limit)
951 char buf[sizeof("-4294967296/second\0")];
953 if (!limit || limit->rate <= 0)
956 fw3_ipt_rule_addarg(r, false, "-m", "limit");
958 sprintf(buf, "%u/%s", limit->rate, fw3_limit_units[limit->unit]);
959 fw3_ipt_rule_addarg(r, limit->invert, "--limit", buf);
961 if (limit->burst > 0)
963 sprintf(buf, "%u", limit->burst);
964 fw3_ipt_rule_addarg(r, limit->invert, "--limit-burst", buf);
969 fw3_ipt_rule_ipset(struct fw3_ipt_rule *r, struct fw3_setmatch *match)
971 char buf[sizeof("dst,dst,dst\0")];
975 struct fw3_ipset *set;
976 struct fw3_ipset_datatype *type;
978 if (!match || !match->set || !match->ptr)
982 list_for_each_entry(type, &set->datatypes, list)
990 p += sprintf(p, "%s", match->dir[i] ? match->dir[i] : type->dir);
994 fw3_ipt_rule_addarg(r, false, "-m", "set");
996 fw3_ipt_rule_addarg(r, match->invert, "--match-set",
997 set->external ? set->external : set->name);
999 fw3_ipt_rule_addarg(r, false, buf, NULL);
1003 fw3_ipt_rule_time(struct fw3_ipt_rule *r, struct fw3_time *time)
1006 struct tm empty = { 0 };
1008 char buf[84]; /* sizeof("1,2,3,...,30,31\0") */
1011 bool d1 = memcmp(&time->datestart, &empty, sizeof(empty));
1012 bool d2 = memcmp(&time->datestop, &empty, sizeof(empty));
1014 if (!d1 && !d2 && !time->timestart && !time->timestop &&
1015 !(time->monthdays & 0xFFFFFFFE) && !(time->weekdays & 0xFE))
1020 fw3_ipt_rule_addarg(r, false, "-m", "time");
1023 fw3_ipt_rule_addarg(r, false, "--kerneltz", NULL);
1027 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestart);
1028 fw3_ipt_rule_addarg(r, false, "--datestart", buf);
1033 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestop);
1034 fw3_ipt_rule_addarg(r, false, "--datestop", buf);
1037 if (time->timestart)
1039 sprintf(buf, "%02d:%02d:%02d",
1040 time->timestart / 3600,
1041 time->timestart % 3600 / 60,
1042 time->timestart % 60);
1044 fw3_ipt_rule_addarg(r, false, "--timestart", buf);
1049 sprintf(buf, "%02d:%02d:%02d",
1050 time->timestop / 3600,
1051 time->timestop % 3600 / 60,
1052 time->timestop % 60);
1054 fw3_ipt_rule_addarg(r, false, "--timestop", buf);
1057 if (time->monthdays & 0xFFFFFFFE)
1059 for (i = 1, p = buf; i < 32; i++)
1061 if (fw3_hasbit(time->monthdays, i))
1066 p += sprintf(p, "%u", i);
1070 fw3_ipt_rule_addarg(r, fw3_hasbit(time->monthdays, 0), "--monthdays", buf);
1073 if (time->weekdays & 0xFE)
1075 for (i = 1, p = buf; i < 8; i++)
1077 if (fw3_hasbit(time->weekdays, i))
1082 p += sprintf(p, "%u", i);
1086 fw3_ipt_rule_addarg(r, fw3_hasbit(time->weekdays, 0), "--weekdays", buf);
1091 fw3_ipt_rule_mark(struct fw3_ipt_rule *r, struct fw3_mark *mark)
1093 char buf[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
1095 if (!mark || !mark->set)
1098 if (mark->mask < 0xFFFFFFFF)
1099 sprintf(buf, "0x%x/0x%x", mark->mark, mark->mask);
1101 sprintf(buf, "0x%x", mark->mark);
1103 fw3_ipt_rule_addarg(r, false, "-m", "mark");
1104 fw3_ipt_rule_addarg(r, mark->invert, "--mark", buf);
1108 fw3_ipt_rule_comment(struct fw3_ipt_rule *r, const char *fmt, ...)
1117 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1120 fw3_ipt_rule_addarg(r, false, "-m", "comment");
1121 fw3_ipt_rule_addarg(r, false, "--comment", buf);
1125 fw3_ipt_rule_extra(struct fw3_ipt_rule *r, const char *extra)
1129 if (!extra || !*extra)
1132 s = fw3_strdup(extra);
1134 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
1136 tmp = realloc(r->argv, (r->argc + 1) * sizeof(*r->argv));
1142 r->argv[r->argc++] = fw3_strdup(p);
1148 #ifndef DISABLE_IPV6
1150 rule_print6(struct ip6t_entry *e)
1152 char buf1[INET6_ADDRSTRLEN], buf2[INET6_ADDRSTRLEN];
1155 if (e->ipv6.flags & IP6T_F_PROTO)
1157 if (e->ipv6.invflags & XT_INV_PROTO)
1160 pname = get_protoname(container_of(e, struct fw3_ipt_rule, e6));
1163 printf(" -p %s", pname);
1165 printf(" -p %u", e->ipv6.proto);
1168 if (e->ipv6.iniface[0])
1170 if (e->ipv6.invflags & IP6T_INV_VIA_IN)
1173 printf(" -i %s", e->ipv6.iniface);
1176 if (e->ipv6.outiface[0])
1178 if (e->ipv6.invflags & IP6T_INV_VIA_OUT)
1181 printf(" -o %s", e->ipv6.outiface);
1184 if (memcmp(&e->ipv6.src, &in6addr_any, sizeof(struct in6_addr)))
1186 if (e->ipv6.invflags & IP6T_INV_SRCIP)
1190 inet_ntop(AF_INET6, &e->ipv6.src, buf1, sizeof(buf1)),
1191 inet_ntop(AF_INET6, &e->ipv6.smsk, buf2, sizeof(buf2)));
1194 if (memcmp(&e->ipv6.dst, &in6addr_any, sizeof(struct in6_addr)))
1196 if (e->ipv6.invflags & IP6T_INV_DSTIP)
1200 inet_ntop(AF_INET6, &e->ipv6.dst, buf1, sizeof(buf1)),
1201 inet_ntop(AF_INET6, &e->ipv6.dmsk, buf2, sizeof(buf2)));
1207 rule_print4(struct ipt_entry *e)
1209 struct in_addr in_zero = { 0 };
1210 char buf1[sizeof("255.255.255.255\0")], buf2[sizeof("255.255.255.255\0")];
1215 if (e->ip.invflags & XT_INV_PROTO)
1218 pname = get_protoname(container_of(e, struct fw3_ipt_rule, e));
1221 printf(" -p %s", pname);
1223 printf(" -p %u", e->ip.proto);
1226 if (e->ip.iniface[0])
1228 if (e->ip.invflags & IPT_INV_VIA_IN)
1231 printf(" -i %s", e->ip.iniface);
1234 if (e->ip.outiface[0])
1236 if (e->ip.invflags & IPT_INV_VIA_OUT)
1239 printf(" -o %s", e->ip.outiface);
1242 if (memcmp(&e->ip.src, &in_zero, sizeof(struct in_addr)))
1244 if (e->ip.invflags & IPT_INV_SRCIP)
1248 inet_ntop(AF_INET, &e->ip.src, buf1, sizeof(buf1)),
1249 inet_ntop(AF_INET, &e->ip.smsk, buf2, sizeof(buf2)));
1252 if (memcmp(&e->ip.dst, &in_zero, sizeof(struct in_addr)))
1254 if (e->ip.invflags & IPT_INV_DSTIP)
1258 inet_ntop(AF_INET, &e->ip.dst, buf1, sizeof(buf1)),
1259 inet_ntop(AF_INET, &e->ip.dmsk, buf2, sizeof(buf2)));
1264 rule_print(struct fw3_ipt_rule *r, const char *prefix, const char *chain)
1266 debug(r->h, "%s %s", prefix, chain);
1268 #ifndef DISABLE_IPV6
1269 if (r->h->family == FW3_FAMILY_V6)
1270 rule_print6(&r->e6);
1275 fw3_xt_print_matches(&r->e.ip, r->matches);
1276 fw3_xt_print_target(&r->e.ip, r->target);
1282 parse_option(struct fw3_ipt_rule *r, int optc, bool inv)
1284 struct xtables_rule_match *m;
1285 struct xtables_match *em;
1287 /* is a target option */
1288 if (r->target && fw3_xt_has_target_parse(r->target) &&
1289 optc >= r->target->option_offset &&
1290 optc < (r->target->option_offset + 256))
1292 xtables_option_tpcall(optc, r->argv, inv, r->target, &r->e);
1296 /* try to dispatch argument to one of the match parsers */
1297 for (m = r->matches; m; m = m->next)
1301 if (m->completed || !fw3_xt_has_match_parse(em))
1304 if (optc < em->option_offset ||
1305 optc >= (em->option_offset + 256))
1308 xtables_option_mpcall(optc, r->argv, inv, em, &r->e);
1312 /* unhandled option, might belong to a protocol match */
1313 if ((em = load_protomatch(r)) != NULL)
1315 init_match(r, em, false);
1317 r->protocol_loaded = true;
1324 warn("parse_option(): option '%s' needs argument", r->argv[optind-1]);
1327 warn("parse_option(): unknown option '%s'", r->argv[optind-1]);
1333 fw3_ipt_rule_addarg(struct fw3_ipt_rule *r, bool inv,
1334 const char *k, const char *v)
1342 n = inv + !!k + !!v;
1343 tmp = realloc(r->argv, (r->argc + n) * sizeof(*tmp));
1351 r->argv[r->argc++] = fw3_strdup("!");
1353 r->argv[r->argc++] = fw3_strdup(k);
1356 r->argv[r->argc++] = fw3_strdup(v);
1359 static unsigned char *
1360 rule_mask(struct fw3_ipt_rule *r)
1363 unsigned char *p, *mask = NULL;
1364 struct xtables_rule_match *m;
1366 #define SZ(x) XT_ALIGN(sizeof(struct x))
1368 #ifndef DISABLE_IPV6
1369 if (r->h->family == FW3_FAMILY_V6)
1373 for (m = r->matches; m; m = m->next)
1374 s += SZ(ip6t_entry_match) + m->match->size;
1376 s += SZ(ip6t_entry_target);
1378 s += r->target->size;
1380 mask = fw3_alloc(s);
1381 memset(mask, 0xFF, SZ(ip6t_entry));
1382 p = mask + SZ(ip6t_entry);
1384 for (m = r->matches; m; m = m->next)
1386 memset(p, 0xFF, SZ(ip6t_entry_match) + m->match->userspacesize);
1387 p += SZ(ip6t_entry_match) + m->match->size;
1390 memset(p, 0xFF, SZ(ip6t_entry_target) + (r->target) ? r->target->userspacesize : 0);
1397 for (m = r->matches; m; m = m->next)
1398 s += SZ(ipt_entry_match) + m->match->size;
1400 s += SZ(ipt_entry_target);
1402 s += r->target->size;
1404 mask = fw3_alloc(s);
1405 memset(mask, 0xFF, SZ(ipt_entry));
1406 p = mask + SZ(ipt_entry);
1408 for (m = r->matches; m; m = m->next)
1410 memset(p, 0xFF, SZ(ipt_entry_match) + m->match->userspacesize);
1411 p += SZ(ipt_entry_match) + m->match->size;
1414 memset(p, 0xFF, SZ(ipt_entry_target) + (r->target) ? r->target->userspacesize : 0);
1421 rule_build(struct fw3_ipt_rule *r)
1423 size_t s, target_size = (r->target) ? r->target->t->u.target_size : 0;
1424 struct xtables_rule_match *m;
1426 #ifndef DISABLE_IPV6
1427 if (r->h->family == FW3_FAMILY_V6)
1429 struct ip6t_entry *e6;
1431 s = XT_ALIGN(sizeof(struct ip6t_entry));
1433 for (m = r->matches; m; m = m->next)
1434 s += m->match->m->u.match_size;
1436 e6 = fw3_alloc(s + target_size);
1438 memcpy(e6, &r->e6, sizeof(struct ip6t_entry));
1440 e6->target_offset = s;
1441 e6->next_offset = s + target_size;
1445 for (m = r->matches; m; m = m->next)
1447 memcpy(e6->elems + s, m->match->m, m->match->m->u.match_size);
1448 s += m->match->m->u.match_size;
1452 memcpy(e6->elems + s, r->target->t, target_size);
1459 struct ipt_entry *e;
1461 s = XT_ALIGN(sizeof(struct ipt_entry));
1463 for (m = r->matches; m; m = m->next)
1464 s += m->match->m->u.match_size;
1466 e = fw3_alloc(s + target_size);
1468 memcpy(e, &r->e, sizeof(struct ipt_entry));
1470 e->target_offset = s;
1471 e->next_offset = s + target_size;
1475 for (m = r->matches; m; m = m->next)
1477 memcpy(e->elems + s, m->match->m, m->match->m->u.match_size);
1478 s += m->match->m->u.match_size;
1482 memcpy(e->elems + s, r->target->t, target_size);
1489 set_rule_tag(struct fw3_ipt_rule *r)
1493 const char *tag = "!fw3";
1495 for (i = 0; i < r->argc; i++)
1496 if (!strcmp(r->argv[i], "--comment") && (i + 1) < r->argc)
1497 if (asprintf(&p, "%s: %s", tag, r->argv[i + 1]) > 0)
1499 free(r->argv[i + 1]);
1504 tmp = realloc(r->argv, (r->argc + 4) * sizeof(*r->argv));
1509 r->argv[r->argc++] = fw3_strdup("-m");
1510 r->argv[r->argc++] = fw3_strdup("comment");
1511 r->argv[r->argc++] = fw3_strdup("--comment");
1512 r->argv[r->argc++] = fw3_strdup(tag);
1517 __fw3_ipt_rule_append(struct fw3_ipt_rule *r, bool repl, const char *fmt, ...)
1520 unsigned char *mask;
1522 struct xtables_rule_match *m;
1523 struct xtables_match *em;
1524 struct xtables_target *et;
1525 struct xtables_globals *g;
1533 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1536 g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
1537 g->opts = g->orig_opts;
1544 while ((optc = getopt_long(r->argc, r->argv, "-:m:j:", g->opts,
1550 em = find_match(r, optarg);
1554 warn("fw3_ipt_rule_append(): Can't find match '%s'", optarg);
1558 init_match(r, em, true);
1562 et = get_target(r, optarg);
1566 warn("fw3_ipt_rule_append(): Can't find target '%s'", optarg);
1573 if ((optarg[0] == '!') && (optarg[1] == '\0'))
1580 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg);
1584 if (parse_option(r, optc, inv))
1592 for (m = r->matches; m; m = m->next)
1593 xtables_option_mfcall(m->match);
1596 xtables_option_tfcall(r->target);
1598 rule = rule_build(r);
1600 #ifndef DISABLE_IPV6
1601 if (r->h->family == FW3_FAMILY_V6)
1605 mask = rule_mask(r);
1607 while (ip6tc_delete_entry(buf, rule, mask, r->h->handle))
1609 rule_print(r, "-D", buf);
1615 rule_print(r, "-A", buf);
1617 if (!ip6tc_append_entry(buf, rule, r->h->handle))
1618 warn("ip6tc_append_entry(): %s", ip6tc_strerror(errno));
1625 mask = rule_mask(r);
1627 while (iptc_delete_entry(buf, rule, mask, r->h->handle))
1629 rule_print(r, "-D", buf);
1635 rule_print(r, "-A", buf);
1637 if (!iptc_append_entry(buf, rule, r->h->handle))
1638 warn("iptc_append_entry(): %s\n", iptc_strerror(errno));
1644 for (i = 1; i < r->argc; i++)
1649 xtables_rule_matches_free(&r->matches);
1656 /* reset all targets and matches */
1657 for (em = xtables_matches; em; em = em->next)
1660 for (et = xtables_targets; et; et = et->next)
1666 xtables_free_opts(1);
1669 struct fw3_ipt_rule *
1670 fw3_ipt_rule_create(struct fw3_ipt_handle *handle, struct fw3_protocol *proto,
1671 struct fw3_device *in, struct fw3_device *out,
1672 struct fw3_address *src, struct fw3_address *dest)
1674 struct fw3_ipt_rule *r;
1676 r = fw3_ipt_rule_new(handle);
1678 fw3_ipt_rule_proto(r, proto);
1679 fw3_ipt_rule_in_out(r, in, out);
1680 fw3_ipt_rule_src_dest(r, src, dest);
1686 xtables_register_match(struct xtables_match *me)
1689 static struct xtables_match **tmp;
1691 if (!xext.register_match)
1692 xext.register_match = dlsym(RTLD_NEXT, "xtables_register_match");
1694 if (!xext.register_match)
1697 xext.register_match(me);
1701 for (i = 0; i < xext.mcount; i++)
1702 if (xext.matches[i] == me)
1705 tmp = realloc(xext.matches, sizeof(me) * (xext.mcount + 1));
1711 xext.matches[xext.mcount++] = me;
1716 xtables_register_target(struct xtables_target *me)
1719 static struct xtables_target **tmp;
1721 if (!xext.register_target)
1722 xext.register_target = dlsym(RTLD_NEXT, "xtables_register_target");
1724 if (!xext.register_target)
1727 xext.register_target(me);
1731 for (i = 0; i < xext.tcount; i++)
1732 if (xext.targets[i] == me)
1735 tmp = realloc(xext.targets, sizeof(me) * (xext.tcount + 1));
1741 xext.targets[xext.tcount++] = me;