2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 static struct option base_opts[] = {
23 { .name = "match", .has_arg = 1, .val = 'm' },
24 { .name = "jump", .has_arg = 1, .val = 'j' },
28 static struct xtables_globals xtg = {
30 .program_version = "4",
31 .orig_opts = base_opts,
34 static struct xtables_globals xtg6 = {
36 .program_version = "6",
37 .orig_opts = base_opts,
40 /* Required by certain extensions like SNAT and DNAT */
41 int kernel_version = 0;
44 get_kernel_version(void)
46 static struct utsname uts;
47 int x = 0, y = 0, z = 0;
49 if (uname(&uts) == -1)
50 sprintf(uts.release, "3.0.0");
52 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
53 kernel_version = 0x10000 * x + 0x100 * y + z;
56 static void fw3_init_extensions(void)
66 struct fw3_ipt_handle *
67 fw3_ipt_open(enum fw3_family family, enum fw3_table table)
69 struct fw3_ipt_handle *h;
71 h = fw3_alloc(sizeof(*h));
75 if (family == FW3_FAMILY_V6)
78 h->family = FW3_FAMILY_V6;
80 h->handle = ip6tc_init(fw3_flag_names[table]);
82 xtables_set_params(&xtg6);
83 xtables_set_nfproto(NFPROTO_IPV6);
88 h->family = FW3_FAMILY_V4;
90 h->handle = iptc_init(fw3_flag_names[table]);
92 xtables_set_params(&xtg);
93 xtables_set_nfproto(NFPROTO_IPV4);
103 fw3_init_extensions();
109 debug(struct fw3_ipt_handle *h, const char *fmt, ...)
113 printf("%s -t %s ", (h->family == FW3_FAMILY_V6) ? "ip6tables" : "iptables",
114 fw3_flag_names[h->table]);
122 fw3_ipt_set_policy(struct fw3_ipt_handle *h, const char *chain,
123 enum fw3_flag policy)
126 debug(h, "-P %s %s\n", chain, fw3_flag_names[policy]);
129 if (h->family == FW3_FAMILY_V6)
130 ip6tc_set_policy(chain, fw3_flag_names[policy], NULL, h->handle);
133 iptc_set_policy(chain, fw3_flag_names[policy], NULL, h->handle);
137 fw3_ipt_flush_chain(struct fw3_ipt_handle *h, const char *chain)
140 debug(h, "-F %s\n", chain);
143 if (h->family == FW3_FAMILY_V6)
144 ip6tc_flush_entries(chain, h->handle);
147 iptc_flush_entries(chain, h->handle);
151 delete_rules(struct fw3_ipt_handle *h, const char *target)
154 const struct ipt_entry *e;
160 if (h->family == FW3_FAMILY_V6)
162 for (chain = ip6tc_first_chain(h->handle);
164 chain = ip6tc_next_chain(h->handle))
169 const struct ip6t_entry *e6;
170 for (num = 0, e6 = ip6tc_first_rule(chain, h->handle);
172 num++, e6 = ip6tc_next_rule(e6, h->handle))
174 t = ip6tc_get_target(e6, h->handle);
176 if (*t && !strcmp(t, target))
179 debug(h, "-D %s %u\n", chain, num + 1);
181 ip6tc_delete_num_entry(chain, num, h->handle);
192 for (chain = iptc_first_chain(h->handle);
194 chain = iptc_next_chain(h->handle))
199 for (num = 0, e = iptc_first_rule(chain, h->handle);
201 num++, e = iptc_next_rule(e, h->handle))
203 t = iptc_get_target(e, h->handle);
205 if (*t && !strcmp(t, target))
208 debug(h, "-D %s %u\n", chain, num + 1);
210 iptc_delete_num_entry(chain, num, h->handle);
221 fw3_ipt_delete_chain(struct fw3_ipt_handle *h, const char *chain)
223 delete_rules(h, chain);
226 debug(h, "-X %s\n", chain);
229 if (h->family == FW3_FAMILY_V6)
230 ip6tc_delete_chain(chain, h->handle);
233 iptc_delete_chain(chain, h->handle);
237 fw3_ipt_create_chain(struct fw3_ipt_handle *h, const char *fmt, ...)
243 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
247 debug(h, "-N %s\n", buf);
249 iptc_create_chain(buf, h->handle);
253 fw3_ipt_flush(struct fw3_ipt_handle *h)
258 if (h->family == FW3_FAMILY_V6)
260 for (chain = ip6tc_first_chain(h->handle);
262 chain = ip6tc_next_chain(h->handle))
264 ip6tc_flush_entries(chain, h->handle);
267 for (chain = ip6tc_first_chain(h->handle);
269 chain = ip6tc_next_chain(h->handle))
271 ip6tc_delete_chain(chain, h->handle);
277 for (chain = iptc_first_chain(h->handle);
279 chain = iptc_next_chain(h->handle))
281 iptc_flush_entries(chain, h->handle);
284 for (chain = iptc_first_chain(h->handle);
286 chain = iptc_next_chain(h->handle))
288 iptc_delete_chain(chain, h->handle);
294 fw3_ipt_commit(struct fw3_ipt_handle *h)
299 if (h->family == FW3_FAMILY_V6)
301 rv = ip6tc_commit(h->handle);
303 warn("ip6tc_commit(): %s", ip6tc_strerror(errno));
308 rv = iptc_commit(h->handle);
310 warn("iptc_commit(): %s", iptc_strerror(errno));
315 fw3_ipt_close(struct fw3_ipt_handle *h)
322 dlclose(h->libv[h->libc]);
331 struct fw3_ipt_rule *
332 fw3_ipt_rule_new(struct fw3_ipt_handle *h)
334 struct fw3_ipt_rule *r;
336 r = fw3_alloc(sizeof(*r));
339 r->argv = fw3_alloc(sizeof(char *));
340 r->argv[r->argc++] = "fw3";
347 is_chain(struct fw3_ipt_handle *h, const char *name)
350 if (h->family == FW3_FAMILY_V6)
351 return ip6tc_is_chain(name, h->handle);
354 return iptc_is_chain(name, h->handle);
358 get_protoname(struct fw3_ipt_rule *r)
360 const struct xtables_pprot *pp;
363 for (pp = xtables_chain_protos; pp->name; pp++)
364 if (pp->num == r->protocol)
365 return (char *)pp->name;
371 load_extension(struct fw3_ipt_handle *h, const char *name)
375 const char *pfx = (h->family == FW3_FAMILY_V6) ? "libip6t" : "libipt";
377 snprintf(path, sizeof(path), "/usr/lib/iptables/libxt_%s.so", name);
378 if (!(lib = dlopen(path, RTLD_NOW)))
380 snprintf(path, sizeof(path), "/usr/lib/iptables/%s_%s.so", pfx, name);
381 lib = dlopen(path, RTLD_NOW);
387 tmp = realloc(h->libv, sizeof(lib) * (h->libc + 1));
393 h->libv[h->libc++] = lib;
398 static struct xtables_match *
399 find_match(struct fw3_ipt_rule *r, const char *name)
401 struct xtables_match *m;
403 m = xtables_find_match(name, XTF_DONT_LOAD, &r->matches);
405 if (!m && load_extension(r->h, name))
406 m = xtables_find_match(name, XTF_DONT_LOAD, &r->matches);
412 init_match(struct fw3_ipt_rule *r, struct xtables_match *m, bool no_clone)
415 struct xtables_globals *g;
420 s = XT_ALIGN(sizeof(struct xt_entry_match)) + m->size;
424 fw3_xt_set_match_name(m);
426 m->m->u.user.revision = m->revision;
427 m->m->u.match_size = s;
429 /* free previous userspace data */
430 fw3_xt_free_match_udata(m);
435 /* don't merge options if no_clone is set and this match is a clone */
436 if (no_clone && (m == m->next))
439 /* merge option table */
440 g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
441 fw3_xt_merge_match_options(g, m);
445 need_protomatch(struct fw3_ipt_rule *r, const char *pname)
450 if (!xtables_find_match(pname, XTF_DONT_LOAD, NULL))
453 return !r->protocol_loaded;
456 static struct xtables_match *
457 load_protomatch(struct fw3_ipt_rule *r)
459 const char *pname = get_protoname(r);
461 if (!need_protomatch(r, pname))
464 return find_match(r, pname);
467 static struct xtables_target *
468 find_target(struct fw3_ipt_rule *r, const char *name)
470 struct xtables_target *t;
472 if (is_chain(r->h, name))
473 return xtables_find_target(XT_STANDARD_TARGET, XTF_LOAD_MUST_SUCCEED);
475 t = xtables_find_target(name, XTF_DONT_LOAD);
477 if (!t && load_extension(r->h, name))
478 t = xtables_find_target(name, XTF_DONT_LOAD);
483 static struct xtables_target *
484 get_target(struct fw3_ipt_rule *r, const char *name)
487 struct xtables_target *t;
488 struct xtables_globals *g;
490 t = find_target(r, name);
495 s = XT_ALIGN(sizeof(struct xt_entry_target)) + t->size;
498 fw3_xt_set_target_name(t, name);
500 t->t->u.user.revision = t->revision;
501 t->t->u.target_size = s;
503 /* free previous userspace data */
504 fw3_xt_free_target_udata(t);
509 /* merge option table */
510 g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
511 fw3_xt_merge_target_options(g, t);
519 fw3_ipt_rule_proto(struct fw3_ipt_rule *r, struct fw3_protocol *proto)
523 if (!proto || proto->any)
526 pr = proto->protocol;
529 if (r->h->family == FW3_FAMILY_V6)
534 r->e6.ipv6.proto = pr;
535 r->e6.ipv6.flags |= IP6T_F_PROTO;
538 r->e6.ipv6.invflags |= XT_INV_PROTO;
546 r->e.ip.invflags |= XT_INV_PROTO;
553 fw3_ipt_rule_in_out(struct fw3_ipt_rule *r,
554 struct fw3_device *in, struct fw3_device *out)
557 if (r->h->family == FW3_FAMILY_V6)
561 xtables_parse_interface(in->name, r->e6.ipv6.iniface,
562 r->e6.ipv6.iniface_mask);
565 r->e6.ipv6.invflags |= IP6T_INV_VIA_IN;
568 if (out && !out->any)
570 xtables_parse_interface(out->name, r->e6.ipv6.outiface,
571 r->e6.ipv6.outiface_mask);
574 r->e6.ipv6.invflags |= IP6T_INV_VIA_OUT;
582 xtables_parse_interface(in->name, r->e.ip.iniface,
583 r->e.ip.iniface_mask);
586 r->e.ip.invflags |= IPT_INV_VIA_IN;
589 if (out && !out->any)
591 xtables_parse_interface(out->name, r->e.ip.outiface,
592 r->e.ip.outiface_mask);
595 r->e.ip.invflags |= IPT_INV_VIA_OUT;
602 fw3_ipt_rule_src_dest(struct fw3_ipt_rule *r,
603 struct fw3_address *src, struct fw3_address *dest)
605 if ((src && src->range) || (dest && dest->range))
607 fw3_ipt_rule_addarg(r, false, "-m", "iprange");
614 fw3_ipt_rule_addarg(r, src->invert, "--src-range",
615 fw3_address_to_string(src, false, false));
618 else if (r->h->family == FW3_FAMILY_V6)
620 r->e6.ipv6.src = src->address.v6;
621 r->e6.ipv6.smsk = src->mask.v6;
624 for (i = 0; i < 4; i++)
625 r->e6.ipv6.src.s6_addr32[i] &= r->e6.ipv6.smsk.s6_addr32[i];
628 r->e6.ipv6.invflags |= IP6T_INV_SRCIP;
633 r->e.ip.src = src->address.v4;
634 r->e.ip.smsk = src->mask.v4;
636 r->e.ip.src.s_addr &= r->e.ip.smsk.s_addr;
639 r->e.ip.invflags |= IPT_INV_SRCIP;
643 if (dest && dest->set)
647 fw3_ipt_rule_addarg(r, dest->invert, "--dst-range",
648 fw3_address_to_string(dest, false, false));
651 else if (r->h->family == FW3_FAMILY_V6)
653 r->e6.ipv6.dst = dest->address.v6;
654 r->e6.ipv6.dmsk = dest->mask.v6;
657 for (i = 0; i < 4; i++)
658 r->e6.ipv6.dst.s6_addr32[i] &= r->e6.ipv6.dmsk.s6_addr32[i];
661 r->e6.ipv6.invflags |= IP6T_INV_DSTIP;
666 r->e.ip.dst = dest->address.v4;
667 r->e.ip.dmsk = dest->mask.v4;
669 r->e.ip.dst.s_addr &= r->e.ip.dmsk.s_addr;
672 r->e.ip.invflags |= IPT_INV_DSTIP;
678 fw3_ipt_rule_sport_dport(struct fw3_ipt_rule *r,
679 struct fw3_port *sp, struct fw3_port *dp)
681 char buf[sizeof("65535:65535\0")];
683 if ((!sp || !sp->set) && (!dp || !dp->set))
686 if (!get_protoname(r))
691 if (sp->port_min == sp->port_max)
692 sprintf(buf, "%u", sp->port_min);
694 sprintf(buf, "%u:%u", sp->port_min, sp->port_max);
696 fw3_ipt_rule_addarg(r, sp->invert, "--sport", buf);
701 if (dp->port_min == dp->port_max)
702 sprintf(buf, "%u", dp->port_min);
704 sprintf(buf, "%u:%u", dp->port_min, dp->port_max);
706 fw3_ipt_rule_addarg(r, dp->invert, "--dport", buf);
711 fw3_ipt_rule_device(struct fw3_ipt_rule *r, const char *device, bool out)
714 struct fw3_device dev = { .any = false };
715 strncpy(dev.name, device, sizeof(dev.name) - 1);
716 fw3_ipt_rule_in_out(r, (out) ? NULL : &dev, (out) ? &dev : NULL);
721 fw3_ipt_rule_mac(struct fw3_ipt_rule *r, struct fw3_mac *mac)
723 char buf[sizeof("ff:ff:ff:ff:ff:ff\0")];
724 uint8_t *addr = mac->mac.ether_addr_octet;
729 sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
730 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
732 fw3_ipt_rule_addarg(r, false, "-m", "mac");
733 fw3_ipt_rule_addarg(r, mac->invert, "--mac-source", buf);
737 fw3_ipt_rule_icmptype(struct fw3_ipt_rule *r, struct fw3_icmptype *icmp)
739 char buf[sizeof("255/255\0")];
745 if (r->h->family == FW3_FAMILY_V6)
747 if (icmp->code6_min == 0 && icmp->code6_max == 0xFF)
748 sprintf(buf, "%u", icmp->type6);
750 sprintf(buf, "%u/%u", icmp->type6, icmp->code6_min);
752 fw3_ipt_rule_addarg(r, icmp->invert, "--icmpv6-type", buf);
757 if (icmp->code_min == 0 && icmp->code_max == 0xFF)
758 sprintf(buf, "%u", icmp->type);
760 sprintf(buf, "%u/%u", icmp->type, icmp->code_min);
762 fw3_ipt_rule_addarg(r, icmp->invert, "--icmp-type", buf);
767 fw3_ipt_rule_limit(struct fw3_ipt_rule *r, struct fw3_limit *limit)
769 char buf[sizeof("-4294967296/second\0")];
771 if (!limit || limit->rate <= 0)
774 fw3_ipt_rule_addarg(r, false, "-m", "limit");
776 sprintf(buf, "%u/%s", limit->rate, fw3_limit_units[limit->unit]);
777 fw3_ipt_rule_addarg(r, limit->invert, "--limit", buf);
779 if (limit->burst > 0)
781 sprintf(buf, "%u", limit->burst);
782 fw3_ipt_rule_addarg(r, limit->invert, "--limit-burst", buf);
787 fw3_ipt_rule_ipset(struct fw3_ipt_rule *r, struct fw3_setmatch *match)
789 char buf[sizeof("dst,dst,dst\0")];
793 struct fw3_ipset *set;
794 struct fw3_ipset_datatype *type;
796 if (!match || !match->set || !match->ptr)
800 list_for_each_entry(type, &set->datatypes, list)
808 p += sprintf(p, "%s", match->dir[i] ? match->dir[i] : type->dir);
812 fw3_ipt_rule_addarg(r, false, "-m", "set");
814 fw3_ipt_rule_addarg(r, match->invert, "--match-set",
815 set->external ? set->external : set->name);
817 fw3_ipt_rule_addarg(r, false, buf, NULL);
821 fw3_ipt_rule_time(struct fw3_ipt_rule *r, struct fw3_time *time)
824 struct tm empty = { 0 };
826 char buf[84]; /* sizeof("1,2,3,...,30,31\0") */
829 bool d1 = memcmp(&time->datestart, &empty, sizeof(empty));
830 bool d2 = memcmp(&time->datestop, &empty, sizeof(empty));
832 if (!d1 && !d2 && !time->timestart && !time->timestop &&
833 !(time->monthdays & 0xFFFFFFFE) && !(time->weekdays & 0xFE))
838 fw3_ipt_rule_addarg(r, false, "-m", "time");
841 fw3_ipt_rule_addarg(r, false, "--utc", NULL);
845 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestart);
846 fw3_ipt_rule_addarg(r, false, "--datestart", buf);
851 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestop);
852 fw3_ipt_rule_addarg(r, false, "--datestop", buf);
857 sprintf(buf, "%02d:%02d:%02d",
858 time->timestart / 3600,
859 time->timestart % 3600 / 60,
860 time->timestart % 60);
862 fw3_ipt_rule_addarg(r, false, "--timestart", buf);
867 sprintf(buf, "%02d:%02d:%02d",
868 time->timestop / 3600,
869 time->timestop % 3600 / 60,
870 time->timestop % 60);
872 fw3_ipt_rule_addarg(r, false, "--timestop", buf);
875 if (time->monthdays & 0xFFFFFFFE)
877 for (i = 1, p = buf; i < 32; i++)
879 if (hasbit(time->monthdays, i))
884 p += sprintf(p, "%u", i);
888 fw3_ipt_rule_addarg(r, hasbit(time->monthdays, 0), "--monthdays", buf);
891 if (time->weekdays & 0xFE)
893 for (i = 1, p = buf; i < 8; i++)
895 if (hasbit(time->weekdays, i))
900 p += sprintf(p, "%u", i);
904 fw3_ipt_rule_addarg(r, hasbit(time->weekdays, 0), "--weekdays", buf);
909 fw3_ipt_rule_mark(struct fw3_ipt_rule *r, struct fw3_mark *mark)
911 char buf[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
913 if (!mark || !mark->set)
916 if (mark->mask < 0xFFFFFFFF)
917 sprintf(buf, "0x%x/0x%x", mark->mark, mark->mask);
919 sprintf(buf, "0x%x", mark->mark);
921 fw3_ipt_rule_addarg(r, false, "-m", "mark");
922 fw3_ipt_rule_addarg(r, mark->invert, "--mark", buf);
926 fw3_ipt_rule_comment(struct fw3_ipt_rule *r, const char *fmt, ...)
935 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
938 fw3_ipt_rule_addarg(r, false, "-m", "comment");
939 fw3_ipt_rule_addarg(r, false, "--comment", buf);
943 fw3_ipt_rule_extra(struct fw3_ipt_rule *r, const char *extra)
947 if (!extra || !*extra)
950 s = fw3_strdup(extra);
952 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
954 tmp = realloc(r->argv, (r->argc + 1) * sizeof(*r->argv));
960 r->argv[r->argc++] = fw3_strdup(p);
968 rule_print6(struct ip6t_entry *e)
970 char buf1[INET6_ADDRSTRLEN], buf2[INET6_ADDRSTRLEN];
973 if (e->ipv6.flags & IP6T_F_PROTO)
975 if (e->ipv6.flags & XT_INV_PROTO)
978 pname = get_protoname(container_of(e, struct fw3_ipt_rule, e6));
981 printf(" -p %s", pname);
983 printf(" -p %u", e->ipv6.proto);
986 if (e->ipv6.iniface[0])
988 if (e->ipv6.flags & IP6T_INV_VIA_IN)
991 printf(" -i %s", e->ipv6.iniface);
994 if (e->ipv6.outiface[0])
996 if (e->ipv6.flags & IP6T_INV_VIA_OUT)
999 printf(" -o %s", e->ipv6.outiface);
1002 if (memcmp(&e->ipv6.src, &in6addr_any, sizeof(struct in6_addr)))
1004 if (e->ipv6.flags & IP6T_INV_SRCIP)
1008 inet_ntop(AF_INET6, &e->ipv6.src, buf1, sizeof(buf1)),
1009 inet_ntop(AF_INET6, &e->ipv6.smsk, buf2, sizeof(buf2)));
1012 if (memcmp(&e->ipv6.dst, &in6addr_any, sizeof(struct in6_addr)))
1014 if (e->ipv6.flags & IP6T_INV_DSTIP)
1018 inet_ntop(AF_INET6, &e->ipv6.dst, buf1, sizeof(buf1)),
1019 inet_ntop(AF_INET6, &e->ipv6.dmsk, buf2, sizeof(buf2)));
1025 rule_print4(struct ipt_entry *e)
1027 struct in_addr in_zero = { 0 };
1028 char buf1[sizeof("255.255.255.255\0")], buf2[sizeof("255.255.255.255\0")];
1033 if (e->ip.flags & XT_INV_PROTO)
1036 pname = get_protoname(container_of(e, struct fw3_ipt_rule, e));
1039 printf(" -p %s", pname);
1041 printf(" -p %u", e->ip.proto);
1044 if (e->ip.iniface[0])
1046 if (e->ip.flags & IPT_INV_VIA_IN)
1049 printf(" -i %s", e->ip.iniface);
1052 if (e->ip.outiface[0])
1054 if (e->ip.flags & IPT_INV_VIA_OUT)
1057 printf(" -o %s", e->ip.outiface);
1060 if (memcmp(&e->ip.src, &in_zero, sizeof(struct in_addr)))
1062 if (e->ip.flags & IPT_INV_SRCIP)
1066 inet_ntop(AF_INET, &e->ip.src, buf1, sizeof(buf1)),
1067 inet_ntop(AF_INET, &e->ip.smsk, buf2, sizeof(buf2)));
1070 if (memcmp(&e->ip.dst, &in_zero, sizeof(struct in_addr)))
1072 if (e->ip.flags & IPT_INV_DSTIP)
1076 inet_ntop(AF_INET, &e->ip.dst, buf1, sizeof(buf1)),
1077 inet_ntop(AF_INET, &e->ip.dmsk, buf2, sizeof(buf2)));
1082 rule_print(struct fw3_ipt_rule *r, const char *prefix, const char *chain)
1084 debug(r->h, "%s %s", prefix, chain);
1086 #ifndef DISABLE_IPV6
1087 if (r->h->family == FW3_FAMILY_V6)
1088 rule_print6(&r->e6);
1093 fw3_xt_print_matches(&r->e.ip, r->matches);
1094 fw3_xt_print_target(&r->e.ip, r->target);
1100 parse_option(struct fw3_ipt_rule *r, int optc, bool inv)
1102 struct xtables_rule_match *m;
1103 struct xtables_match *em;
1105 /* is a target option */
1106 if (r->target && fw3_xt_has_target_parse(r->target) &&
1107 optc >= r->target->option_offset &&
1108 optc < (r->target->option_offset + 256))
1110 xtables_option_tpcall(optc, r->argv, inv, r->target, &r->e);
1114 /* try to dispatch argument to one of the match parsers */
1115 for (m = r->matches; m; m = m->next)
1119 if (m->completed || !fw3_xt_has_match_parse(em))
1122 if (optc < em->option_offset ||
1123 optc >= (em->option_offset + 256))
1126 xtables_option_mpcall(optc, r->argv, inv, em, &r->e);
1130 /* unhandled option, might belong to a protocol match */
1131 if ((em = load_protomatch(r)) != NULL)
1133 init_match(r, em, false);
1135 r->protocol_loaded = true;
1142 warn("parse_option(): option '%s' needs argument", r->argv[optind-1]);
1145 warn("parse_option(): unknown option '%s'", r->argv[optind-1]);
1151 fw3_ipt_rule_addarg(struct fw3_ipt_rule *r, bool inv,
1152 const char *k, const char *v)
1160 n = inv + !!k + !!v;
1161 tmp = realloc(r->argv, (r->argc + n) * sizeof(*tmp));
1169 r->argv[r->argc++] = fw3_strdup("!");
1171 r->argv[r->argc++] = fw3_strdup(k);
1174 r->argv[r->argc++] = fw3_strdup(v);
1177 static unsigned char *
1178 rule_mask(struct fw3_ipt_rule *r)
1181 unsigned char *p, *mask = NULL;
1182 struct xtables_rule_match *m;
1184 #define SZ(x) XT_ALIGN(sizeof(struct x))
1186 #ifndef DISABLE_IPV6
1187 if (r->h->family == FW3_FAMILY_V6)
1191 for (m = r->matches; m; m = m->next)
1192 s += SZ(ip6t_entry_match) + m->match->size;
1194 s += SZ(ip6t_entry_target);
1196 s += r->target->size;
1198 mask = fw3_alloc(s);
1199 memset(mask, 0xFF, SZ(ip6t_entry));
1200 p = mask + SZ(ip6t_entry);
1202 for (m = r->matches; m; m = m->next)
1204 memset(p, 0xFF, SZ(ip6t_entry_match) + m->match->userspacesize);
1205 p += SZ(ip6t_entry_match) + m->match->size;
1208 memset(p, 0xFF, SZ(ip6t_entry_target) + (r->target) ? r->target->userspacesize : 0);
1215 for (m = r->matches; m; m = m->next)
1216 s += SZ(ipt_entry_match) + m->match->size;
1218 s += SZ(ipt_entry_target);
1220 s += r->target->size;
1222 mask = fw3_alloc(s);
1223 memset(mask, 0xFF, SZ(ipt_entry));
1224 p = mask + SZ(ipt_entry);
1226 for (m = r->matches; m; m = m->next)
1228 memset(p, 0xFF, SZ(ipt_entry_match) + m->match->userspacesize);
1229 p += SZ(ipt_entry_match) + m->match->size;
1232 memset(p, 0xFF, SZ(ipt_entry_target) + (r->target) ? r->target->userspacesize : 0);
1239 rule_build(struct fw3_ipt_rule *r)
1241 size_t s, target_size = (r->target) ? r->target->t->u.target_size : 0;
1242 struct xtables_rule_match *m;
1244 #ifndef DISABLE_IPV6
1245 if (r->h->family == FW3_FAMILY_V6)
1247 struct ip6t_entry *e6;
1249 s = XT_ALIGN(sizeof(struct ip6t_entry));
1251 for (m = r->matches; m; m = m->next)
1252 s += m->match->m->u.match_size;
1254 e6 = fw3_alloc(s + target_size);
1256 memcpy(e6, &r->e6, sizeof(struct ip6t_entry));
1258 e6->target_offset = s;
1259 e6->next_offset = s + target_size;
1263 for (m = r->matches; m; m = m->next)
1265 memcpy(e6->elems + s, m->match->m, m->match->m->u.match_size);
1266 s += m->match->m->u.match_size;
1270 memcpy(e6->elems + s, r->target->t, target_size);
1277 struct ipt_entry *e;
1279 s = XT_ALIGN(sizeof(struct ipt_entry));
1281 for (m = r->matches; m; m = m->next)
1282 s += m->match->m->u.match_size;
1284 e = fw3_alloc(s + target_size);
1286 memcpy(e, &r->e, sizeof(struct ipt_entry));
1288 e->target_offset = s;
1289 e->next_offset = s + target_size;
1293 for (m = r->matches; m; m = m->next)
1295 memcpy(e->elems + s, m->match->m, m->match->m->u.match_size);
1296 s += m->match->m->u.match_size;
1300 memcpy(e->elems + s, r->target->t, target_size);
1307 __fw3_ipt_rule_append(struct fw3_ipt_rule *r, bool repl, const char *fmt, ...)
1310 unsigned char *mask;
1312 struct xtables_rule_match *m;
1313 struct xtables_match *em;
1314 struct xtables_target *et;
1315 struct xtables_globals *g;
1323 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1326 g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
1327 g->opts = g->orig_opts;
1332 while ((optc = getopt_long(r->argc, r->argv, "-:m:j:", g->opts,
1338 em = find_match(r, optarg);
1342 warn("fw3_ipt_rule_append(): Can't find match '%s'", optarg);
1346 init_match(r, em, true);
1350 et = get_target(r, optarg);
1354 warn("fw3_ipt_rule_append(): Can't find target '%s'", optarg);
1361 if ((optarg[0] == '!') && (optarg[1] == '\0'))
1368 warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg);
1372 if (parse_option(r, optc, inv))
1380 for (m = r->matches; m; m = m->next)
1381 xtables_option_mfcall(m->match);
1384 xtables_option_tfcall(r->target);
1386 rule = rule_build(r);
1388 #ifndef DISABLE_IPV6
1389 if (r->h->family == FW3_FAMILY_V6)
1393 mask = rule_mask(r);
1395 while (ip6tc_delete_entry(buf, rule, mask, r->h->handle))
1397 rule_print(r, "-D", buf);
1403 rule_print(r, "-A", buf);
1405 if (!ip6tc_append_entry(buf, rule, r->h->handle))
1406 warn("ip6tc_append_entry(): %s", ip6tc_strerror(errno));
1413 mask = rule_mask(r);
1415 while (iptc_delete_entry(buf, rule, mask, r->h->handle))
1417 rule_print(r, "-D", buf);
1423 rule_print(r, "-A", buf);
1425 if (!iptc_append_entry(buf, rule, r->h->handle))
1426 warn("iptc_append_entry(): %s\n", iptc_strerror(errno));
1432 for (i = 1; i < r->argc; i++)
1437 xtables_rule_matches_free(&r->matches);
1444 /* reset all targets and matches */
1445 for (em = xtables_matches; em; em = em->next)
1448 for (et = xtables_targets; et; et = et->next)
1454 xtables_free_opts(1);
1457 struct fw3_ipt_rule *
1458 fw3_ipt_rule_create(struct fw3_ipt_handle *handle, struct fw3_protocol *proto,
1459 struct fw3_device *in, struct fw3_device *out,
1460 struct fw3_address *src, struct fw3_address *dest)
1462 struct fw3_ipt_rule *r;
1464 r = fw3_ipt_rule_new(handle);
1466 fw3_ipt_rule_proto(r, proto);
1467 fw3_ipt_rule_in_out(r, in, out);
1468 fw3_ipt_rule_src_dest(r, src, dest);