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;
58 #define __ipt_module(x) libxt_##x##_init, libipt_##x##_init,
61 #define __ipt_module(x) libxt_##x##_init, libipt_##x##_init, libip6t_##x##_init,
64 static void fw3_init_extensions(void)
67 void (*initfuncs[])(void) = { FW3_IPT_MODULES };
69 for (i = 0; i < sizeof(initfuncs)/sizeof(initfuncs[0]); i++)
74 struct fw3_ipt_handle *
75 fw3_ipt_open(enum fw3_family family, enum fw3_table table)
77 struct fw3_ipt_handle *h;
79 h = fw3_alloc(sizeof(*h));
83 if (family == FW3_FAMILY_V6)
86 h->family = FW3_FAMILY_V6;
88 h->handle = ip6tc_init(fw3_flag_names[table]);
90 xtables_set_params(&xtg6);
91 xtables_set_nfproto(NFPROTO_IPV6);
96 h->family = FW3_FAMILY_V4;
98 h->handle = iptc_init(fw3_flag_names[table]);
100 xtables_set_params(&xtg);
101 xtables_set_nfproto(NFPROTO_IPV4);
111 fw3_init_extensions();
117 debug(struct fw3_ipt_handle *h, const char *fmt, ...)
121 printf("%s -t %s ", (h->family == FW3_FAMILY_V6) ? "ip6tables" : "iptables",
122 fw3_flag_names[h->table]);
130 fw3_ipt_set_policy(struct fw3_ipt_handle *h, const char *chain,
131 enum fw3_flag policy)
134 debug(h, "-P %s %s\n", chain, fw3_flag_names[policy]);
137 if (h->family == FW3_FAMILY_V6)
138 ip6tc_set_policy(chain, fw3_flag_names[policy], NULL, h->handle);
141 iptc_set_policy(chain, fw3_flag_names[policy], NULL, h->handle);
145 fw3_ipt_flush_chain(struct fw3_ipt_handle *h, const char *chain)
148 debug(h, "-F %s\n", chain);
151 if (h->family == FW3_FAMILY_V6)
152 ip6tc_flush_entries(chain, h->handle);
155 iptc_flush_entries(chain, h->handle);
159 delete_rules(struct fw3_ipt_handle *h, const char *target)
162 const struct ipt_entry *e;
168 if (h->family == FW3_FAMILY_V6)
170 for (chain = ip6tc_first_chain(h->handle);
172 chain = ip6tc_next_chain(h->handle))
177 const struct ip6t_entry *e6;
178 for (num = 0, e6 = ip6tc_first_rule(chain, h->handle);
180 num++, e6 = ip6tc_next_rule(e6, h->handle))
182 t = ip6tc_get_target(e6, h->handle);
184 if (*t && !strcmp(t, target))
187 debug(h, "-D %s %u\n", chain, num + 1);
189 ip6tc_delete_num_entry(chain, num, h->handle);
200 for (chain = iptc_first_chain(h->handle);
202 chain = iptc_next_chain(h->handle))
207 for (num = 0, e = iptc_first_rule(chain, h->handle);
209 num++, e = iptc_next_rule(e, h->handle))
211 t = iptc_get_target(e, h->handle);
213 if (*t && !strcmp(t, target))
216 debug(h, "-D %s %u\n", chain, num + 1);
218 iptc_delete_num_entry(chain, num, h->handle);
229 fw3_ipt_delete_chain(struct fw3_ipt_handle *h, const char *chain)
231 delete_rules(h, chain);
234 debug(h, "-X %s\n", chain);
237 if (h->family == FW3_FAMILY_V6)
238 ip6tc_delete_chain(chain, h->handle);
241 iptc_delete_chain(chain, h->handle);
245 fw3_ipt_create_chain(struct fw3_ipt_handle *h, const char *fmt, ...)
251 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
255 debug(h, "-N %s\n", buf);
257 iptc_create_chain(buf, h->handle);
261 fw3_ipt_flush(struct fw3_ipt_handle *h)
266 if (h->family == FW3_FAMILY_V6)
268 for (chain = ip6tc_first_chain(h->handle);
270 chain = ip6tc_next_chain(h->handle))
272 ip6tc_flush_entries(chain, h->handle);
275 for (chain = ip6tc_first_chain(h->handle);
277 chain = ip6tc_next_chain(h->handle))
279 ip6tc_delete_chain(chain, h->handle);
285 for (chain = iptc_first_chain(h->handle);
287 chain = iptc_next_chain(h->handle))
289 iptc_flush_entries(chain, h->handle);
292 for (chain = iptc_first_chain(h->handle);
294 chain = iptc_next_chain(h->handle))
296 iptc_delete_chain(chain, h->handle);
302 fw3_ipt_commit(struct fw3_ipt_handle *h)
307 if (h->family == FW3_FAMILY_V6)
309 rv = ip6tc_commit(h->handle);
311 warn("ip6tc_commit(): %s", ip6tc_strerror(errno));
316 rv = iptc_commit(h->handle);
318 warn("iptc_commit(): %s", iptc_strerror(errno));
323 fw3_ipt_close(struct fw3_ipt_handle *h)
330 dlclose(h->libv[h->libc]);
339 struct fw3_ipt_rule *
340 fw3_ipt_rule_new(struct fw3_ipt_handle *h)
342 struct fw3_ipt_rule *r;
344 r = fw3_alloc(sizeof(*r));
347 r->argv = fw3_alloc(sizeof(char *));
348 r->argv[r->argc++] = "fw3";
355 is_chain(struct fw3_ipt_handle *h, const char *name)
358 if (h->family == FW3_FAMILY_V6)
359 return ip6tc_is_chain(name, h->handle);
362 return iptc_is_chain(name, h->handle);
366 get_protoname(struct fw3_ipt_rule *r)
368 const struct xtables_pprot *pp;
371 for (pp = xtables_chain_protos; pp->name; pp++)
372 if (pp->num == r->protocol)
373 return (char *)pp->name;
379 load_extension(struct fw3_ipt_handle *h, const char *name)
383 const char *pfx = (h->family == FW3_FAMILY_V6) ? "libip6t" : "libipt";
385 snprintf(path, sizeof(path), "/usr/lib/iptables/libxt_%s.so", name);
386 if (!(lib = dlopen(path, RTLD_NOW)))
388 snprintf(path, sizeof(path), "/usr/lib/iptables/%s_%s.so", pfx, name);
389 lib = dlopen(path, RTLD_NOW);
395 tmp = realloc(h->libv, sizeof(lib) * (h->libc + 1));
401 h->libv[h->libc++] = lib;
406 static struct xtables_match *
407 find_match(struct fw3_ipt_rule *r, const char *name)
409 struct xtables_match *m;
411 m = xtables_find_match(name, XTF_DONT_LOAD, &r->matches);
413 if (!m && load_extension(r->h, name))
414 m = xtables_find_match(name, XTF_DONT_LOAD, &r->matches);
420 init_match(struct fw3_ipt_rule *r, struct xtables_match *m, bool no_clone)
423 struct xtables_globals *g;
428 s = XT_ALIGN(sizeof(struct xt_entry_match)) + m->size;
432 fw3_xt_set_match_name(m);
434 m->m->u.user.revision = m->revision;
435 m->m->u.match_size = s;
437 /* free previous userspace data */
438 fw3_xt_free_match_udata(m);
443 /* don't merge options if no_clone is set and this match is a clone */
444 if (no_clone && (m == m->next))
447 /* merge option table */
448 g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
449 fw3_xt_merge_match_options(g, m);
453 need_protomatch(struct fw3_ipt_rule *r, const char *pname)
458 if (!xtables_find_match(pname, XTF_DONT_LOAD, NULL))
461 return !r->protocol_loaded;
464 static struct xtables_match *
465 load_protomatch(struct fw3_ipt_rule *r)
467 const char *pname = get_protoname(r);
469 if (!need_protomatch(r, pname))
472 return find_match(r, pname);
475 static struct xtables_target *
476 find_target(struct fw3_ipt_rule *r, const char *name)
478 struct xtables_target *t;
480 if (is_chain(r->h, name))
481 return xtables_find_target(XT_STANDARD_TARGET, XTF_LOAD_MUST_SUCCEED);
483 t = xtables_find_target(name, XTF_DONT_LOAD);
485 if (!t && load_extension(r->h, name))
486 t = xtables_find_target(name, XTF_DONT_LOAD);
491 static struct xtables_target *
492 get_target(struct fw3_ipt_rule *r, const char *name)
495 struct xtables_target *t;
496 struct xtables_globals *g;
498 t = find_target(r, name);
503 s = XT_ALIGN(sizeof(struct xt_entry_target)) + t->size;
506 fw3_xt_set_target_name(t, name);
508 t->t->u.user.revision = t->revision;
509 t->t->u.target_size = s;
511 /* free previous userspace data */
512 fw3_xt_free_target_udata(t);
517 /* merge option table */
518 g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
519 fw3_xt_merge_target_options(g, t);
527 fw3_ipt_rule_proto(struct fw3_ipt_rule *r, struct fw3_protocol *proto)
531 if (!proto || proto->any)
534 pr = proto->protocol;
537 if (r->h->family == FW3_FAMILY_V6)
542 r->e6.ipv6.proto = pr;
543 r->e6.ipv6.flags |= IP6T_F_PROTO;
546 r->e6.ipv6.invflags |= XT_INV_PROTO;
554 r->e.ip.invflags |= XT_INV_PROTO;
561 fw3_ipt_rule_in_out(struct fw3_ipt_rule *r,
562 struct fw3_device *in, struct fw3_device *out)
565 if (r->h->family == FW3_FAMILY_V6)
569 xtables_parse_interface(in->name, r->e6.ipv6.iniface,
570 r->e6.ipv6.iniface_mask);
573 r->e6.ipv6.invflags |= IP6T_INV_VIA_IN;
576 if (out && !out->any)
578 xtables_parse_interface(out->name, r->e6.ipv6.outiface,
579 r->e6.ipv6.outiface_mask);
582 r->e6.ipv6.invflags |= IP6T_INV_VIA_OUT;
590 xtables_parse_interface(in->name, r->e.ip.iniface,
591 r->e.ip.iniface_mask);
594 r->e.ip.invflags |= IPT_INV_VIA_IN;
597 if (out && !out->any)
599 xtables_parse_interface(out->name, r->e.ip.outiface,
600 r->e.ip.outiface_mask);
603 r->e.ip.invflags |= IPT_INV_VIA_OUT;
610 fw3_ipt_rule_src_dest(struct fw3_ipt_rule *r,
611 struct fw3_address *src, struct fw3_address *dest)
613 if ((src && src->range) || (dest && dest->range))
615 fw3_ipt_rule_addarg(r, false, "-m", "iprange");
622 fw3_ipt_rule_addarg(r, src->invert, "--src-range",
623 fw3_address_to_string(src, false, false));
626 else if (r->h->family == FW3_FAMILY_V6)
628 r->e6.ipv6.src = src->address.v6;
629 r->e6.ipv6.smsk = src->mask.v6;
632 for (i = 0; i < 4; i++)
633 r->e6.ipv6.src.s6_addr32[i] &= r->e6.ipv6.smsk.s6_addr32[i];
636 r->e6.ipv6.invflags |= IP6T_INV_SRCIP;
641 r->e.ip.src = src->address.v4;
642 r->e.ip.smsk = src->mask.v4;
644 r->e.ip.src.s_addr &= r->e.ip.smsk.s_addr;
647 r->e.ip.invflags |= IPT_INV_SRCIP;
651 if (dest && dest->set)
655 fw3_ipt_rule_addarg(r, dest->invert, "--dst-range",
656 fw3_address_to_string(dest, false, false));
659 else if (r->h->family == FW3_FAMILY_V6)
661 r->e6.ipv6.dst = dest->address.v6;
662 r->e6.ipv6.dmsk = dest->mask.v6;
665 for (i = 0; i < 4; i++)
666 r->e6.ipv6.dst.s6_addr32[i] &= r->e6.ipv6.dmsk.s6_addr32[i];
669 r->e6.ipv6.invflags |= IP6T_INV_DSTIP;
674 r->e.ip.dst = dest->address.v4;
675 r->e.ip.dmsk = dest->mask.v4;
677 r->e.ip.dst.s_addr &= r->e.ip.dmsk.s_addr;
680 r->e.ip.invflags |= IPT_INV_DSTIP;
686 fw3_ipt_rule_sport_dport(struct fw3_ipt_rule *r,
687 struct fw3_port *sp, struct fw3_port *dp)
689 char buf[sizeof("65535:65535\0")];
691 if ((!sp || !sp->set) && (!dp || !dp->set))
694 if (!get_protoname(r))
699 if (sp->port_min == sp->port_max)
700 sprintf(buf, "%u", sp->port_min);
702 sprintf(buf, "%u:%u", sp->port_min, sp->port_max);
704 fw3_ipt_rule_addarg(r, sp->invert, "--sport", buf);
709 if (dp->port_min == dp->port_max)
710 sprintf(buf, "%u", dp->port_min);
712 sprintf(buf, "%u:%u", dp->port_min, dp->port_max);
714 fw3_ipt_rule_addarg(r, dp->invert, "--dport", buf);
719 fw3_ipt_rule_device(struct fw3_ipt_rule *r, const char *device, bool out)
722 struct fw3_device dev = { .any = false };
723 strncpy(dev.name, device, sizeof(dev.name) - 1);
724 fw3_ipt_rule_in_out(r, (out) ? NULL : &dev, (out) ? &dev : NULL);
729 fw3_ipt_rule_mac(struct fw3_ipt_rule *r, struct fw3_mac *mac)
731 char buf[sizeof("ff:ff:ff:ff:ff:ff\0")];
732 uint8_t *addr = mac->mac.ether_addr_octet;
737 sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
738 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
740 fw3_ipt_rule_addarg(r, false, "-m", "mac");
741 fw3_ipt_rule_addarg(r, mac->invert, "--mac-source", buf);
745 fw3_ipt_rule_icmptype(struct fw3_ipt_rule *r, struct fw3_icmptype *icmp)
747 char buf[sizeof("255/255\0")];
753 if (r->h->family == FW3_FAMILY_V6)
755 if (icmp->code6_min == 0 && icmp->code6_max == 0xFF)
756 sprintf(buf, "%u", icmp->type6);
758 sprintf(buf, "%u/%u", icmp->type6, icmp->code6_min);
760 fw3_ipt_rule_addarg(r, icmp->invert, "--icmpv6-type", buf);
765 if (icmp->code_min == 0 && icmp->code_max == 0xFF)
766 sprintf(buf, "%u", icmp->type);
768 sprintf(buf, "%u/%u", icmp->type, icmp->code_min);
770 fw3_ipt_rule_addarg(r, icmp->invert, "--icmp-type", buf);
775 fw3_ipt_rule_limit(struct fw3_ipt_rule *r, struct fw3_limit *limit)
777 char buf[sizeof("-4294967296/second\0")];
779 if (!limit || limit->rate <= 0)
782 fw3_ipt_rule_addarg(r, false, "-m", "limit");
784 sprintf(buf, "%u/%s", limit->rate, fw3_limit_units[limit->unit]);
785 fw3_ipt_rule_addarg(r, limit->invert, "--limit", buf);
787 if (limit->burst > 0)
789 sprintf(buf, "%u", limit->burst);
790 fw3_ipt_rule_addarg(r, limit->invert, "--limit-burst", buf);
795 fw3_ipt_rule_ipset(struct fw3_ipt_rule *r, struct fw3_setmatch *match)
797 char buf[sizeof("dst,dst,dst\0")];
801 struct fw3_ipset *set;
802 struct fw3_ipset_datatype *type;
804 if (!match || !match->set || !match->ptr)
808 list_for_each_entry(type, &set->datatypes, list)
816 p += sprintf(p, "%s", match->dir[i] ? match->dir[i] : type->dir);
820 fw3_ipt_rule_addarg(r, false, "-m", "set");
822 fw3_ipt_rule_addarg(r, match->invert, "--match-set",
823 set->external ? set->external : set->name);
825 fw3_ipt_rule_addarg(r, false, buf, NULL);
829 fw3_ipt_rule_time(struct fw3_ipt_rule *r, struct fw3_time *time)
832 struct tm empty = { 0 };
834 char buf[84]; /* sizeof("1,2,3,...,30,31\0") */
837 bool d1 = memcmp(&time->datestart, &empty, sizeof(empty));
838 bool d2 = memcmp(&time->datestop, &empty, sizeof(empty));
840 if (!d1 && !d2 && !time->timestart && !time->timestop &&
841 !(time->monthdays & 0xFFFFFFFE) && !(time->weekdays & 0xFE))
846 fw3_ipt_rule_addarg(r, false, "-m", "time");
849 fw3_ipt_rule_addarg(r, false, "--utc", NULL);
853 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestart);
854 fw3_ipt_rule_addarg(r, false, "--datestart", buf);
859 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestop);
860 fw3_ipt_rule_addarg(r, false, "--datestop", buf);
865 sprintf(buf, "%02d:%02d:%02d",
866 time->timestart / 3600,
867 time->timestart % 3600 / 60,
868 time->timestart % 60);
870 fw3_ipt_rule_addarg(r, false, "--timestart", buf);
875 sprintf(buf, "%02d:%02d:%02d",
876 time->timestop / 3600,
877 time->timestop % 3600 / 60,
878 time->timestop % 60);
880 fw3_ipt_rule_addarg(r, false, "--timestop", buf);
883 if (time->monthdays & 0xFFFFFFFE)
885 for (i = 1, p = buf; i < 32; i++)
887 if (hasbit(time->monthdays, i))
892 p += sprintf(p, "%u", i);
896 fw3_ipt_rule_addarg(r, hasbit(time->monthdays, 0), "--monthdays", buf);
899 if (time->weekdays & 0xFE)
901 for (i = 1, p = buf; i < 8; i++)
903 if (hasbit(time->weekdays, i))
908 p += sprintf(p, "%u", i);
912 fw3_ipt_rule_addarg(r, hasbit(time->weekdays, 0), "--weekdays", buf);
917 fw3_ipt_rule_mark(struct fw3_ipt_rule *r, struct fw3_mark *mark)
919 char buf[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
921 if (!mark || !mark->set)
924 if (mark->mask < 0xFFFFFFFF)
925 sprintf(buf, "0x%x/0x%x", mark->mark, mark->mask);
927 sprintf(buf, "0x%x", mark->mark);
929 fw3_ipt_rule_addarg(r, false, "-m", "mark");
930 fw3_ipt_rule_addarg(r, mark->invert, "--mark", buf);
934 fw3_ipt_rule_comment(struct fw3_ipt_rule *r, const char *fmt, ...)
943 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
946 fw3_ipt_rule_addarg(r, false, "-m", "comment");
947 fw3_ipt_rule_addarg(r, false, "--comment", buf);
951 fw3_ipt_rule_extra(struct fw3_ipt_rule *r, const char *extra)
955 if (!extra || !*extra)
958 s = fw3_strdup(extra);
960 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
962 tmp = realloc(r->argv, (r->argc + 1) * sizeof(*r->argv));
968 r->argv[r->argc++] = fw3_strdup(p);
976 rule_print6(struct ip6t_entry *e)
978 char buf1[INET6_ADDRSTRLEN], buf2[INET6_ADDRSTRLEN];
981 if (e->ipv6.flags & IP6T_F_PROTO)
983 if (e->ipv6.flags & XT_INV_PROTO)
986 pname = get_protoname(container_of(e, struct fw3_ipt_rule, e6));
989 printf(" -p %s", pname);
991 printf(" -p %u", e->ipv6.proto);
994 if (e->ipv6.iniface[0])
996 if (e->ipv6.flags & IP6T_INV_VIA_IN)
999 printf(" -i %s", e->ipv6.iniface);
1002 if (e->ipv6.outiface[0])
1004 if (e->ipv6.flags & IP6T_INV_VIA_OUT)
1007 printf(" -o %s", e->ipv6.outiface);
1010 if (memcmp(&e->ipv6.src, &in6addr_any, sizeof(struct in6_addr)))
1012 if (e->ipv6.flags & IP6T_INV_SRCIP)
1016 inet_ntop(AF_INET6, &e->ipv6.src, buf1, sizeof(buf1)),
1017 inet_ntop(AF_INET6, &e->ipv6.smsk, buf2, sizeof(buf2)));
1020 if (memcmp(&e->ipv6.dst, &in6addr_any, sizeof(struct in6_addr)))
1022 if (e->ipv6.flags & IP6T_INV_DSTIP)
1026 inet_ntop(AF_INET6, &e->ipv6.dst, buf1, sizeof(buf1)),
1027 inet_ntop(AF_INET6, &e->ipv6.dmsk, buf2, sizeof(buf2)));
1033 rule_print4(struct ipt_entry *e)
1035 struct in_addr in_zero = { 0 };
1036 char buf1[sizeof("255.255.255.255\0")], buf2[sizeof("255.255.255.255\0")];
1041 if (e->ip.flags & XT_INV_PROTO)
1044 pname = get_protoname(container_of(e, struct fw3_ipt_rule, e));
1047 printf(" -p %s", pname);
1049 printf(" -p %u", e->ip.proto);
1052 if (e->ip.iniface[0])
1054 if (e->ip.flags & IPT_INV_VIA_IN)
1057 printf(" -i %s", e->ip.iniface);
1060 if (e->ip.outiface[0])
1062 if (e->ip.flags & IPT_INV_VIA_OUT)
1065 printf(" -o %s", e->ip.outiface);
1068 if (memcmp(&e->ip.src, &in_zero, sizeof(struct in_addr)))
1070 if (e->ip.flags & IPT_INV_SRCIP)
1074 inet_ntop(AF_INET, &e->ip.src, buf1, sizeof(buf1)),
1075 inet_ntop(AF_INET, &e->ip.smsk, buf2, sizeof(buf2)));
1078 if (memcmp(&e->ip.dst, &in_zero, sizeof(struct in_addr)))
1080 if (e->ip.flags & IPT_INV_DSTIP)
1084 inet_ntop(AF_INET, &e->ip.dst, buf1, sizeof(buf1)),
1085 inet_ntop(AF_INET, &e->ip.dmsk, buf2, sizeof(buf2)));
1090 rule_print(struct fw3_ipt_rule *r, const char *prefix, const char *chain)
1092 debug(r->h, "%s %s", prefix, chain);
1094 #ifndef DISABLE_IPV6
1095 if (r->h->family == FW3_FAMILY_V6)
1096 rule_print6(&r->e6);
1101 fw3_xt_print_matches(&r->e.ip, r->matches);
1102 fw3_xt_print_target(&r->e.ip, r->target);
1108 parse_option(struct fw3_ipt_rule *r, int optc, bool inv)
1110 struct xtables_rule_match *m;
1111 struct xtables_match *em;
1113 /* is a target option */
1114 if (r->target && fw3_xt_has_target_parse(r->target) &&
1115 optc >= r->target->option_offset &&
1116 optc < (r->target->option_offset + 256))
1118 xtables_option_tpcall(optc, r->argv, inv, r->target, &r->e);
1122 /* try to dispatch argument to one of the match parsers */
1123 for (m = r->matches; m; m = m->next)
1127 if (m->completed || !fw3_xt_has_match_parse(em))
1130 if (optc < em->option_offset ||
1131 optc >= (em->option_offset + 256))
1134 xtables_option_mpcall(optc, r->argv, inv, em, &r->e);
1138 /* unhandled option, might belong to a protocol match */
1139 if ((em = load_protomatch(r)) != NULL)
1141 init_match(r, em, false);
1143 r->protocol_loaded = true;
1150 warn("parse_option(): option '%s' needs argument", r->argv[optind-1]);
1153 warn("parse_option(): unknown option '%s'", r->argv[optind-1]);
1159 fw3_ipt_rule_addarg(struct fw3_ipt_rule *r, bool inv,
1160 const char *k, const char *v)
1168 n = inv + !!k + !!v;
1169 tmp = realloc(r->argv, (r->argc + n) * sizeof(*tmp));
1177 r->argv[r->argc++] = fw3_strdup("!");
1179 r->argv[r->argc++] = fw3_strdup(k);
1182 r->argv[r->argc++] = fw3_strdup(v);
1185 static unsigned char *
1186 rule_mask(struct fw3_ipt_rule *r)
1189 unsigned char *p, *mask = NULL;
1190 struct xtables_rule_match *m;
1192 #define SZ(x) XT_ALIGN(sizeof(struct x))
1194 #ifndef DISABLE_IPV6
1195 if (r->h->family == FW3_FAMILY_V6)
1199 for (m = r->matches; m; m = m->next)
1200 s += SZ(ip6t_entry_match) + m->match->size;
1202 s += SZ(ip6t_entry_target);
1204 s += r->target->size;
1206 mask = fw3_alloc(s);
1207 memset(mask, 0xFF, SZ(ip6t_entry));
1208 p = mask + SZ(ip6t_entry);
1210 for (m = r->matches; m; m = m->next)
1212 memset(p, 0xFF, SZ(ip6t_entry_match) + m->match->userspacesize);
1213 p += SZ(ip6t_entry_match) + m->match->size;
1216 memset(p, 0xFF, SZ(ip6t_entry_target) + (r->target) ? r->target->userspacesize : 0);
1223 for (m = r->matches; m; m = m->next)
1224 s += SZ(ipt_entry_match) + m->match->size;
1226 s += SZ(ipt_entry_target);
1228 s += r->target->size;
1230 mask = fw3_alloc(s);
1231 memset(mask, 0xFF, SZ(ipt_entry));
1232 p = mask + SZ(ipt_entry);
1234 for (m = r->matches; m; m = m->next)
1236 memset(p, 0xFF, SZ(ipt_entry_match) + m->match->userspacesize);
1237 p += SZ(ipt_entry_match) + m->match->size;
1240 memset(p, 0xFF, SZ(ipt_entry_target) + (r->target) ? r->target->userspacesize : 0);
1247 rule_build(struct fw3_ipt_rule *r)
1249 size_t s, target_size = (r->target) ? r->target->t->u.target_size : 0;
1250 struct xtables_rule_match *m;
1252 #ifndef DISABLE_IPV6
1253 if (r->h->family == FW3_FAMILY_V6)
1255 struct ip6t_entry *e6;
1257 s = XT_ALIGN(sizeof(struct ip6t_entry));
1259 for (m = r->matches; m; m = m->next)
1260 s += m->match->m->u.match_size;
1262 e6 = fw3_alloc(s + target_size);
1264 memcpy(e6, &r->e6, sizeof(struct ip6t_entry));
1266 e6->target_offset = s;
1267 e6->next_offset = s + target_size;
1271 for (m = r->matches; m; m = m->next)
1273 memcpy(e6->elems + s, m->match->m, m->match->m->u.match_size);
1274 s += m->match->m->u.match_size;
1278 memcpy(e6->elems + s, r->target->t, target_size);
1285 struct ipt_entry *e;
1287 s = XT_ALIGN(sizeof(struct ipt_entry));
1289 for (m = r->matches; m; m = m->next)
1290 s += m->match->m->u.match_size;
1292 e = fw3_alloc(s + target_size);
1294 memcpy(e, &r->e, sizeof(struct ipt_entry));
1296 e->target_offset = s;
1297 e->next_offset = s + target_size;
1301 for (m = r->matches; m; m = m->next)
1303 memcpy(e->elems + s, m->match->m, m->match->m->u.match_size);
1304 s += m->match->m->u.match_size;
1308 memcpy(e->elems + s, r->target->t, target_size);
1315 __fw3_ipt_rule_append(struct fw3_ipt_rule *r, bool repl, const char *fmt, ...)
1318 unsigned char *mask;
1320 struct xtables_rule_match *m;
1321 struct xtables_match *em;
1322 struct xtables_target *et;
1323 struct xtables_globals *g;
1331 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1334 g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
1335 g->opts = g->orig_opts;
1340 while ((optc = getopt_long(r->argc, r->argv, "-:m:j:", g->opts,
1346 em = find_match(r, optarg);
1350 warn("fw3_ipt_rule_append(): Can't find match '%s'", optarg);
1354 init_match(r, em, true);
1358 et = get_target(r, optarg);
1362 warn("fw3_ipt_rule_append(): Can't find target '%s'", optarg);
1369 if ((optarg[0] == '!') && (optarg[1] == '\0'))
1376 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg);
1380 if (parse_option(r, optc, inv))
1388 for (m = r->matches; m; m = m->next)
1389 xtables_option_mfcall(m->match);
1392 xtables_option_tfcall(r->target);
1394 rule = rule_build(r);
1396 #ifndef DISABLE_IPV6
1397 if (r->h->family == FW3_FAMILY_V6)
1401 mask = rule_mask(r);
1403 while (ip6tc_delete_entry(buf, rule, mask, r->h->handle))
1405 rule_print(r, "-D", buf);
1411 rule_print(r, "-A", buf);
1413 if (!ip6tc_append_entry(buf, rule, r->h->handle))
1414 warn("ip6tc_append_entry(): %s", ip6tc_strerror(errno));
1421 mask = rule_mask(r);
1423 while (iptc_delete_entry(buf, rule, mask, r->h->handle))
1425 rule_print(r, "-D", buf);
1431 rule_print(r, "-A", buf);
1433 if (!iptc_append_entry(buf, rule, r->h->handle))
1434 warn("iptc_append_entry(): %s\n", iptc_strerror(errno));
1440 for (i = 1; i < r->argc; i++)
1445 xtables_rule_matches_free(&r->matches);
1452 /* reset all targets and matches */
1453 for (em = xtables_matches; em; em = em->next)
1456 for (et = xtables_targets; et; et = et->next)
1462 xtables_free_opts(1);
1465 struct fw3_ipt_rule *
1466 fw3_ipt_rule_create(struct fw3_ipt_handle *handle, struct fw3_protocol *proto,
1467 struct fw3_device *in, struct fw3_device *out,
1468 struct fw3_address *src, struct fw3_address *dest)
1470 struct fw3_ipt_rule *r;
1472 r = fw3_ipt_rule_new(handle);
1474 fw3_ipt_rule_proto(r, proto);
1475 fw3_ipt_rule_in_out(r, in, out);
1476 fw3_ipt_rule_src_dest(r, src, dest);