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;
59 #define __ipt_module(x) libxt_##x##_init, libipt_##x##_init,
62 #define __ipt_module(x) libxt_##x##_init, libipt_##x##_init, libip6t_##x##_init,
65 static void fw3_init_extensions(void)
68 void (*initfuncs[])(void) = { FW3_IPT_MODULES };
70 for (i = 0; i < sizeof(initfuncs)/sizeof(initfuncs[0]); i++)
75 struct fw3_ipt_handle *
76 fw3_ipt_open(enum fw3_family family, enum fw3_table table)
78 struct fw3_ipt_handle *h;
80 h = fw3_alloc(sizeof(*h));
84 if (family == FW3_FAMILY_V6)
87 h->family = FW3_FAMILY_V6;
89 h->handle = ip6tc_init(fw3_flag_names[table]);
91 xtables_set_params(&xtg6);
92 xtables_set_nfproto(NFPROTO_IPV6);
97 h->family = FW3_FAMILY_V4;
99 h->handle = iptc_init(fw3_flag_names[table]);
101 xtables_set_params(&xtg);
102 xtables_set_nfproto(NFPROTO_IPV4);
112 fw3_init_extensions();
118 debug(struct fw3_ipt_handle *h, const char *fmt, ...)
122 printf("%s -t %s ", (h->family == FW3_FAMILY_V6) ? "ip6tables" : "iptables",
123 fw3_flag_names[h->table]);
131 fw3_ipt_set_policy(struct fw3_ipt_handle *h, const char *chain,
132 enum fw3_flag policy)
135 debug(h, "-P %s %s\n", chain, fw3_flag_names[policy]);
138 if (h->family == FW3_FAMILY_V6)
139 ip6tc_set_policy(chain, fw3_flag_names[policy], NULL, h->handle);
142 iptc_set_policy(chain, fw3_flag_names[policy], NULL, h->handle);
146 fw3_ipt_delete_chain(struct fw3_ipt_handle *h, const char *chain)
150 debug(h, "-F %s\n", chain);
151 debug(h, "-X %s\n", chain);
155 if (h->family == FW3_FAMILY_V6)
157 if (ip6tc_flush_entries(chain, h->handle))
158 ip6tc_delete_chain(chain, h->handle);
163 if (iptc_flush_entries(chain, h->handle))
164 iptc_delete_chain(chain, h->handle);
169 fw3_ipt_delete_rules(struct fw3_ipt_handle *h, const char *target)
172 const struct ipt_entry *e;
178 if (h->family == FW3_FAMILY_V6)
180 for (chain = ip6tc_first_chain(h->handle);
182 chain = ip6tc_next_chain(h->handle))
187 const struct ip6t_entry *e6;
188 for (num = 0, e6 = ip6tc_first_rule(chain, h->handle);
190 num++, e6 = ip6tc_next_rule(e6, h->handle))
192 t = ip6tc_get_target(e6, h->handle);
194 if (*t && !strcmp(t, target))
197 debug(h, "-D %s %u\n", chain, num + 1);
199 ip6tc_delete_num_entry(chain, num, h->handle);
210 for (chain = iptc_first_chain(h->handle);
212 chain = iptc_next_chain(h->handle))
217 for (num = 0, e = iptc_first_rule(chain, h->handle);
219 num++, e = iptc_next_rule(e, h->handle))
221 t = iptc_get_target(e, h->handle);
223 if (*t && !strcmp(t, target))
226 debug(h, "-D %s %u\n", chain, num + 1);
228 iptc_delete_num_entry(chain, num, h->handle);
239 fw3_ipt_create_chain(struct fw3_ipt_handle *h, const char *fmt, ...)
245 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
249 debug(h, "-N %s\n", buf);
251 iptc_create_chain(buf, h->handle);
255 fw3_ipt_flush(struct fw3_ipt_handle *h)
260 if (h->family == FW3_FAMILY_V6)
262 for (chain = ip6tc_first_chain(h->handle);
264 chain = ip6tc_next_chain(h->handle))
266 ip6tc_flush_entries(chain, h->handle);
269 for (chain = ip6tc_first_chain(h->handle);
271 chain = ip6tc_next_chain(h->handle))
273 ip6tc_delete_chain(chain, h->handle);
279 for (chain = iptc_first_chain(h->handle);
281 chain = iptc_next_chain(h->handle))
283 iptc_flush_entries(chain, h->handle);
286 for (chain = iptc_first_chain(h->handle);
288 chain = iptc_next_chain(h->handle))
290 iptc_delete_chain(chain, h->handle);
296 fw3_ipt_commit(struct fw3_ipt_handle *h)
301 if (h->family == FW3_FAMILY_V6)
303 rv = ip6tc_commit(h->handle);
305 fprintf(stderr, "ip6tc_commit(): %s\n", ip6tc_strerror(errno));
310 rv = iptc_commit(h->handle);
312 fprintf(stderr, "iptc_commit(): %s\n", iptc_strerror(errno));
317 fw3_ipt_close(struct fw3_ipt_handle *h)
324 dlclose(h->libv[h->libc]);
333 struct fw3_ipt_rule *
334 fw3_ipt_rule_new(struct fw3_ipt_handle *h)
336 struct fw3_ipt_rule *r;
338 r = fw3_alloc(sizeof(*r));
341 r->argv = fw3_alloc(sizeof(char *));
342 r->argv[r->argc++] = "fw3";
349 is_chain(struct fw3_ipt_handle *h, const char *name)
352 if (h->family == FW3_FAMILY_V6)
353 return ip6tc_is_chain(name, h->handle);
356 return iptc_is_chain(name, h->handle);
360 get_protoname(struct fw3_ipt_rule *r)
362 const struct xtables_pprot *pp;
365 for (pp = xtables_chain_protos; pp->name; pp++)
366 if (pp->num == r->protocol)
367 return (char *)pp->name;
373 load_extension(struct fw3_ipt_handle *h, const char *name)
377 const char *pfx = (h->family == FW3_FAMILY_V6) ? "libip6t" : "libipt";
379 snprintf(path, sizeof(path), "/usr/lib/iptables/libxt_%s.so", name);
380 if (!(lib = dlopen(path, RTLD_NOW)))
382 snprintf(path, sizeof(path), "/usr/lib/iptables/%s_%s.so", pfx, name);
383 lib = dlopen(path, RTLD_NOW);
389 tmp = realloc(h->libv, sizeof(lib) * (h->libc + 1));
395 h->libv[h->libc++] = lib;
400 static struct xtables_match *
401 find_match(struct fw3_ipt_rule *r, const char *name)
403 struct xtables_match *m;
405 m = xtables_find_match(name, XTF_DONT_LOAD, &r->matches);
407 if (!m && load_extension(r->h, name))
408 m = xtables_find_match(name, XTF_DONT_LOAD, &r->matches);
414 init_match(struct fw3_ipt_rule *r, struct xtables_match *m, bool no_clone)
417 struct xtables_globals *g;
422 s = XT_ALIGN(sizeof(struct xt_entry_match)) + m->size;
426 fw3_xt_set_match_name(m);
428 m->m->u.user.revision = m->revision;
429 m->m->u.match_size = s;
431 /* free previous userspace data */
432 fw3_xt_free_match_udata(m);
437 /* don't merge options if no_clone is set and this match is a clone */
438 if (no_clone && (m == m->next))
441 /* merge option table */
442 g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
443 fw3_xt_merge_match_options(g, m);
447 need_protomatch(struct fw3_ipt_rule *r, const char *pname)
452 if (!xtables_find_match(pname, XTF_DONT_LOAD, NULL))
455 return !r->protocol_loaded;
458 static struct xtables_match *
459 load_protomatch(struct fw3_ipt_rule *r)
461 const char *pname = get_protoname(r);
463 if (!need_protomatch(r, pname))
466 return find_match(r, pname);
469 static struct xtables_target *
470 find_target(struct fw3_ipt_rule *r, const char *name)
472 struct xtables_target *t;
474 if (is_chain(r->h, name))
475 return xtables_find_target(XT_STANDARD_TARGET, XTF_LOAD_MUST_SUCCEED);
477 t = xtables_find_target(name, XTF_DONT_LOAD);
479 if (!t && load_extension(r->h, name))
480 t = xtables_find_target(name, XTF_DONT_LOAD);
485 static struct xtables_target *
486 get_target(struct fw3_ipt_rule *r, const char *name)
489 struct xtables_target *t;
490 struct xtables_globals *g;
492 t = find_target(r, name);
497 s = XT_ALIGN(sizeof(struct xt_entry_target)) + t->size;
500 fw3_xt_set_target_name(t, name);
502 t->t->u.user.revision = t->revision;
503 t->t->u.target_size = s;
505 /* free previous userspace data */
506 fw3_xt_free_target_udata(t);
511 /* merge option table */
512 g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
513 fw3_xt_merge_target_options(g, t);
521 fw3_ipt_rule_proto(struct fw3_ipt_rule *r, struct fw3_protocol *proto)
525 if (!proto || proto->any)
528 pr = proto->protocol;
531 if (r->h->family == FW3_FAMILY_V6)
536 r->e6.ipv6.proto = pr;
537 r->e6.ipv6.flags |= IP6T_F_PROTO;
540 r->e6.ipv6.invflags |= XT_INV_PROTO;
548 r->e.ip.invflags |= XT_INV_PROTO;
555 fw3_ipt_rule_in_out(struct fw3_ipt_rule *r,
556 struct fw3_device *in, struct fw3_device *out)
559 if (r->h->family == FW3_FAMILY_V6)
563 xtables_parse_interface(in->name, r->e6.ipv6.iniface,
564 r->e6.ipv6.iniface_mask);
567 r->e6.ipv6.invflags |= IP6T_INV_VIA_IN;
570 if (out && !out->any)
572 xtables_parse_interface(out->name, r->e6.ipv6.outiface,
573 r->e6.ipv6.outiface_mask);
576 r->e6.ipv6.invflags |= IP6T_INV_VIA_OUT;
584 xtables_parse_interface(in->name, r->e.ip.iniface,
585 r->e.ip.iniface_mask);
588 r->e.ip.invflags |= IPT_INV_VIA_IN;
591 if (out && !out->any)
593 xtables_parse_interface(out->name, r->e.ip.outiface,
594 r->e.ip.outiface_mask);
597 r->e.ip.invflags |= IPT_INV_VIA_OUT;
604 ip4prefix2mask(int prefix, struct in_addr *mask)
606 mask->s_addr = htonl(~((1 << (32 - prefix)) - 1));
611 ip6prefix2mask(int prefix, struct in6_addr *mask)
613 char *p = (char *)mask;
617 memset(p, 0xff, prefix / 8);
618 memset(p + (prefix / 8) + 1, 0, (128 - prefix) / 8);
619 p[prefix / 8] = 0xff << (8 - (prefix & 7));
623 memset(mask, 0, sizeof(*mask));
629 fw3_ipt_rule_src_dest(struct fw3_ipt_rule *r,
630 struct fw3_address *src, struct fw3_address *dest)
632 if ((src && src->range) || (dest && dest->range))
634 fw3_ipt_rule_addarg(r, false, "-m", "iprange");
641 fw3_ipt_rule_addarg(r, src->invert, "--src-range",
642 fw3_address_to_string(src, false));
645 else if (r->h->family == FW3_FAMILY_V6)
647 r->e6.ipv6.src = src->address.v6;
648 ip6prefix2mask(src->mask, &r->e6.ipv6.smsk);
651 for (i = 0; i < 4; i++)
652 r->e6.ipv6.src.s6_addr32[i] &= r->e6.ipv6.smsk.s6_addr32[i];
655 r->e6.ipv6.invflags |= IP6T_INV_SRCIP;
660 r->e.ip.src = src->address.v4;
661 ip4prefix2mask(src->mask, &r->e.ip.smsk);
663 r->e.ip.src.s_addr &= r->e.ip.smsk.s_addr;
666 r->e.ip.invflags |= IPT_INV_SRCIP;
670 if (dest && dest->set)
674 fw3_ipt_rule_addarg(r, dest->invert, "--dst-range",
675 fw3_address_to_string(dest, false));
678 else if (r->h->family == FW3_FAMILY_V6)
680 r->e6.ipv6.dst = dest->address.v6;
681 ip6prefix2mask(dest->mask, &r->e6.ipv6.dmsk);
684 for (i = 0; i < 4; i++)
685 r->e6.ipv6.dst.s6_addr32[i] &= r->e6.ipv6.dmsk.s6_addr32[i];
688 r->e6.ipv6.invflags |= IP6T_INV_DSTIP;
693 r->e.ip.dst = dest->address.v4;
694 ip4prefix2mask(dest->mask, &r->e.ip.dmsk);
696 r->e.ip.dst.s_addr &= r->e.ip.dmsk.s_addr;
699 r->e.ip.invflags |= IPT_INV_DSTIP;
705 fw3_ipt_rule_sport_dport(struct fw3_ipt_rule *r,
706 struct fw3_port *sp, struct fw3_port *dp)
708 char buf[sizeof("65535:65535\0")];
710 if ((!sp || !sp->set) && (!dp || !dp->set))
713 if (!get_protoname(r))
718 if (sp->port_min == sp->port_max)
719 sprintf(buf, "%u", sp->port_min);
721 sprintf(buf, "%u:%u", sp->port_min, sp->port_max);
723 fw3_ipt_rule_addarg(r, sp->invert, "--sport", buf);
728 if (dp->port_min == dp->port_max)
729 sprintf(buf, "%u", dp->port_min);
731 sprintf(buf, "%u:%u", dp->port_min, dp->port_max);
733 fw3_ipt_rule_addarg(r, dp->invert, "--dport", buf);
738 fw3_ipt_rule_mac(struct fw3_ipt_rule *r, struct fw3_mac *mac)
743 fw3_ipt_rule_addarg(r, false, "-m", "mac");
744 fw3_ipt_rule_addarg(r, mac->invert, "--mac-source", ether_ntoa(&mac->mac));
748 fw3_ipt_rule_icmptype(struct fw3_ipt_rule *r, struct fw3_icmptype *icmp)
750 char buf[sizeof("255/255\0")];
756 if (r->h->family == FW3_FAMILY_V6)
758 if (icmp->code6_min == 0 && icmp->code6_max == 0xFF)
759 sprintf(buf, "%u", icmp->type6);
761 sprintf(buf, "%u/%u", icmp->type6, icmp->code6_min);
763 fw3_ipt_rule_addarg(r, icmp->invert, "--icmpv6-type", buf);
768 if (icmp->code_min == 0 && icmp->code_max == 0xFF)
769 sprintf(buf, "%u", icmp->type);
771 sprintf(buf, "%u/%u", icmp->type, icmp->code_min);
773 fw3_ipt_rule_addarg(r, icmp->invert, "--icmp-type", buf);
778 fw3_ipt_rule_limit(struct fw3_ipt_rule *r, struct fw3_limit *limit)
780 char buf[sizeof("-4294967296/second\0")];
782 if (!limit || limit->rate <= 0)
785 fw3_ipt_rule_addarg(r, false, "-m", "limit");
787 sprintf(buf, "%u/%s", limit->rate, fw3_limit_units[limit->unit]);
788 fw3_ipt_rule_addarg(r, limit->invert, "--limit", buf);
790 if (limit->burst > 0)
792 sprintf(buf, "%u", limit->burst);
793 fw3_ipt_rule_addarg(r, limit->invert, "--limit-burst", buf);
798 fw3_ipt_rule_ipset(struct fw3_ipt_rule *r, struct fw3_ipset *ipset,
801 char buf[sizeof("dst,dst,dst\0")];
804 struct fw3_ipset_datatype *type;
809 list_for_each_entry(type, &ipset->datatypes, list)
814 p += sprintf(p, "%s", type->dest ? "dst" : "src");
817 fw3_ipt_rule_addarg(r, false, "-m", "set");
819 fw3_ipt_rule_addarg(r, invert, "--match-set",
820 ipset->external ? ipset->external : ipset->name);
822 fw3_ipt_rule_addarg(r, false, buf, NULL);
826 fw3_ipt_rule_time(struct fw3_ipt_rule *r, struct fw3_time *time)
829 struct tm empty = { 0 };
831 char buf[84]; /* sizeof("1,2,3,...,30,31\0") */
834 bool d1 = memcmp(&time->datestart, &empty, sizeof(empty));
835 bool d2 = memcmp(&time->datestop, &empty, sizeof(empty));
837 if (!d1 && !d2 && !time->timestart && !time->timestop &&
838 !(time->monthdays & 0xFFFFFFFE) && !(time->weekdays & 0xFE))
843 fw3_ipt_rule_addarg(r, false, "-m", "time");
846 fw3_ipt_rule_addarg(r, false, "--utc", NULL);
850 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestart);
851 fw3_ipt_rule_addarg(r, false, "--datestart", buf);
856 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestop);
857 fw3_ipt_rule_addarg(r, false, "--datestop", buf);
862 sprintf(buf, "%02d:%02d:%02d",
863 time->timestart / 3600,
864 time->timestart % 3600 / 60,
865 time->timestart % 60);
867 fw3_ipt_rule_addarg(r, false, "--timestart", buf);
872 sprintf(buf, "%02d:%02d:%02d",
873 time->timestop / 3600,
874 time->timestop % 3600 / 60,
875 time->timestop % 60);
877 fw3_ipt_rule_addarg(r, false, "--timestop", buf);
880 if (time->monthdays & 0xFFFFFFFE)
882 for (i = 1, p = buf; i < 32; i++)
884 if (hasbit(time->monthdays, i))
889 p += sprintf(p, "%u", i);
893 fw3_ipt_rule_addarg(r, hasbit(time->monthdays, 0), "--monthdays", buf);
896 if (time->weekdays & 0xFE)
898 for (i = 1, p = buf; i < 8; i++)
900 if (hasbit(time->weekdays, i))
905 p += sprintf(p, "%u", i);
909 fw3_ipt_rule_addarg(r, hasbit(time->weekdays, 0), "--weekdays", buf);
914 fw3_ipt_rule_mark(struct fw3_ipt_rule *r, struct fw3_mark *mark)
916 char buf[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
918 if (!mark || !mark->set)
921 if (mark->mask < 0xFFFFFFFF)
922 sprintf(buf, "0x%x/0x%x", mark->mark, mark->mask);
924 sprintf(buf, "0x%x", mark->mark);
926 fw3_ipt_rule_addarg(r, false, "-m", "mark");
927 fw3_ipt_rule_addarg(r, mark->invert, "--mark", buf);
931 fw3_ipt_rule_comment(struct fw3_ipt_rule *r, const char *fmt, ...)
940 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
943 fw3_ipt_rule_addarg(r, false, "-m", "comment");
944 fw3_ipt_rule_addarg(r, false, "--comment", buf);
948 fw3_ipt_rule_extra(struct fw3_ipt_rule *r, const char *extra)
952 if (!extra || !*extra)
955 s = fw3_strdup(extra);
957 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
959 tmp = realloc(r->argv, (r->argc + 1) * sizeof(*r->argv));
965 r->argv[r->argc++] = fw3_strdup(p);
973 rule_print6(struct ip6t_entry *e)
975 char buf[INET6_ADDRSTRLEN];
978 if (e->ipv6.flags & IP6T_F_PROTO)
980 if (e->ipv6.flags & XT_INV_PROTO)
983 pname = get_protoname(container_of(e, struct fw3_ipt_rule, e6));
986 printf(" -p %s", pname);
988 printf(" -p %u", e->ipv6.proto);
991 if (e->ipv6.iniface[0])
993 if (e->ipv6.flags & IP6T_INV_VIA_IN)
996 printf(" -i %s", e->ipv6.iniface);
999 if (e->ipv6.outiface[0])
1001 if (e->ipv6.flags & IP6T_INV_VIA_OUT)
1004 printf(" -o %s", e->ipv6.outiface);
1007 if (memcmp(&e->ipv6.src, &in6addr_any, sizeof(struct in6_addr)))
1009 if (e->ipv6.flags & IP6T_INV_SRCIP)
1012 printf(" -s %s/%u", inet_ntop(AF_INET6, &e->ipv6.src, buf, sizeof(buf)),
1013 xtables_ip6mask_to_cidr(&e->ipv6.smsk));
1016 if (memcmp(&e->ipv6.dst, &in6addr_any, sizeof(struct in6_addr)))
1018 if (e->ipv6.flags & IP6T_INV_DSTIP)
1021 printf(" -d %s/%u", inet_ntop(AF_INET6, &e->ipv6.dst, buf, sizeof(buf)),
1022 xtables_ip6mask_to_cidr(&e->ipv6.dmsk));
1028 rule_print4(struct ipt_entry *e)
1030 struct in_addr in_zero = { 0 };
1031 char buf[sizeof("255.255.255.255\0")];
1036 if (e->ip.flags & XT_INV_PROTO)
1039 pname = get_protoname(container_of(e, struct fw3_ipt_rule, e));
1042 printf(" -p %s", pname);
1044 printf(" -p %u", e->ip.proto);
1047 if (e->ip.iniface[0])
1049 if (e->ip.flags & IPT_INV_VIA_IN)
1052 printf(" -i %s", e->ip.iniface);
1055 if (e->ip.outiface[0])
1057 if (e->ip.flags & IPT_INV_VIA_OUT)
1060 printf(" -o %s", e->ip.outiface);
1063 if (memcmp(&e->ip.src, &in_zero, sizeof(struct in_addr)))
1065 if (e->ip.flags & IPT_INV_SRCIP)
1068 printf(" -s %s/%u", inet_ntop(AF_INET, &e->ip.src, buf, sizeof(buf)),
1069 xtables_ipmask_to_cidr(&e->ip.smsk));
1072 if (memcmp(&e->ip.dst, &in_zero, sizeof(struct in_addr)))
1074 if (e->ip.flags & IPT_INV_DSTIP)
1077 printf(" -d %s/%u", inet_ntop(AF_INET, &e->ip.dst, buf, sizeof(buf)),
1078 xtables_ipmask_to_cidr(&e->ip.dmsk));
1083 rule_print(struct fw3_ipt_rule *r, const char *chain)
1085 struct xtables_rule_match *rm;
1086 struct xtables_match *m;
1087 struct xtables_target *t;
1089 debug(r->h, "-A %s", chain);
1091 #ifndef DISABLE_IPV6
1092 if (r->h->family == FW3_FAMILY_V6)
1093 rule_print6(&r->e6);
1098 for (rm = r->matches; rm; rm = rm->next)
1101 printf(" -m %s", fw3_xt_get_match_name(m));
1104 m->save(&r->e.ip, m->m);
1110 printf(" -j %s", fw3_xt_get_target_name(t));
1113 t->save(&r->e.ip, t->t);
1120 parse_option(struct fw3_ipt_rule *r, int optc, bool inv)
1122 struct xtables_rule_match *m;
1123 struct xtables_match *em;
1125 /* is a target option */
1126 if (r->target && fw3_xt_has_target_parse(r->target) &&
1127 optc >= r->target->option_offset &&
1128 optc < (r->target->option_offset + 256))
1130 xtables_option_tpcall(optc, r->argv, inv, r->target, &r->e);
1134 /* try to dispatch argument to one of the match parsers */
1135 for (m = r->matches; m; m = m->next)
1139 if (m->completed || !fw3_xt_has_match_parse(em))
1142 if (optc < em->option_offset ||
1143 optc >= (em->option_offset + 256))
1146 xtables_option_mpcall(optc, r->argv, inv, em, &r->e);
1150 /* unhandled option, might belong to a protocol match */
1151 if ((em = load_protomatch(r)) != NULL)
1153 init_match(r, em, false);
1155 r->protocol_loaded = true;
1162 fprintf(stderr, "parse_option(): option '%s' needs argument\n",
1166 fprintf(stderr, "parse_option(): unknown option '%s'\n",
1173 fw3_ipt_rule_addarg(struct fw3_ipt_rule *r, bool inv,
1174 const char *k, const char *v)
1182 n = inv + !!k + !!v;
1183 tmp = realloc(r->argv, (r->argc + n) * sizeof(*tmp));
1191 r->argv[r->argc++] = fw3_strdup("!");
1193 r->argv[r->argc++] = fw3_strdup(k);
1196 r->argv[r->argc++] = fw3_strdup(v);
1200 fw3_ipt_rule_append(struct fw3_ipt_rule *r, const char *fmt, ...)
1203 struct xtables_rule_match *m;
1204 struct xtables_match *em;
1205 struct xtables_target *et;
1206 struct xtables_globals *g;
1207 struct ipt_entry *e;
1215 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1218 g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
1219 g->opts = g->orig_opts;
1224 while ((optc = getopt_long(r->argc, r->argv, "m:j:", g->opts, NULL)) != -1)
1229 em = find_match(r, optarg);
1233 fprintf(stderr, "fw3_ipt_rule_append(): Can't find match '%s'\n", optarg);
1237 init_match(r, em, true);
1241 et = get_target(r, optarg);
1245 fprintf(stderr, "fw3_ipt_rule_append(): Can't find target '%s'\n", optarg);
1252 if ((optarg[0] == '!') && (optarg[1] == '\0'))
1258 fprintf(stderr, "fw3_ipt_rule_append(): Bad argument '%s'\n", optarg);
1262 if (parse_option(r, optc, inv))
1270 for (m = r->matches; m; m = m->next)
1271 xtables_option_mfcall(m->match);
1274 xtables_option_tfcall(r->target);
1279 #ifndef DISABLE_IPV6
1280 if (r->h->family == FW3_FAMILY_V6)
1282 struct ip6t_entry *e6;
1284 s = XT_ALIGN(sizeof(struct ip6t_entry));
1286 for (m = r->matches; m; m = m->next)
1287 s += m->match->m->u.match_size;
1289 e6 = fw3_alloc(s + r->target->t->u.target_size);
1291 memcpy(e6, &r->e6, sizeof(struct ip6t_entry));
1293 e6->target_offset = s;
1294 e6->next_offset = s + r->target->t->u.target_size;
1298 for (m = r->matches; m; m = m->next)
1300 memcpy(e6->elems + s, m->match->m, m->match->m->u.match_size);
1301 s += m->match->m->u.match_size;
1304 memcpy(e6->elems + s, r->target->t, r->target->t->u.target_size);
1305 ip6tc_append_entry(buf, e6, r->h->handle);
1311 s = XT_ALIGN(sizeof(struct ipt_entry));
1313 for (m = r->matches; m; m = m->next)
1314 s += m->match->m->u.match_size;
1316 e = fw3_alloc(s + r->target->t->u.target_size);
1318 memcpy(e, &r->e, sizeof(struct ipt_entry));
1320 e->target_offset = s;
1321 e->next_offset = s + r->target->t->u.target_size;
1325 for (m = r->matches; m; m = m->next)
1327 memcpy(e->elems + s, m->match->m, m->match->m->u.match_size);
1328 s += m->match->m->u.match_size;
1331 memcpy(e->elems + s, r->target->t, r->target->t->u.target_size);
1333 if (!iptc_append_entry(buf, e, r->h->handle))
1334 fprintf(stderr, "iptc_append_entry(): %s\n", iptc_strerror(errno));
1340 for (i = 1; i < r->argc; i++)
1345 xtables_rule_matches_free(&r->matches);
1352 /* reset all targets and matches */
1353 for (em = xtables_matches; em; em = em->next)
1356 for (et = xtables_targets; et; et = et->next)
1362 xtables_free_opts(1);
1365 struct fw3_ipt_rule *
1366 fw3_ipt_rule_create(struct fw3_ipt_handle *handle, struct fw3_protocol *proto,
1367 struct fw3_device *in, struct fw3_device *out,
1368 struct fw3_address *src, struct fw3_address *dest)
1370 struct fw3_ipt_rule *r;
1372 r = fw3_ipt_rule_new(handle);
1374 fw3_ipt_rule_proto(r, proto);
1375 fw3_ipt_rule_in_out(r, in, out);
1376 fw3_ipt_rule_src_dest(r, src, dest);