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 */
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 = LINUX_VERSION(x, y, z);
57 struct fw3_ipt_handle *
58 fw3_ipt_open(enum fw3_family family, enum fw3_table table)
60 struct fw3_ipt_handle *h;
62 h = fw3_alloc(sizeof(*h));
66 if (family == FW3_FAMILY_V6)
68 h->family = FW3_FAMILY_V6;
70 h->handle = ip6tc_init(fw3_flag_names[table]);
72 xtables_set_params(&xtg6);
73 xtables_set_nfproto(NFPROTO_IPV6);
77 h->family = FW3_FAMILY_V4;
79 h->handle = iptc_init(fw3_flag_names[table]);
81 xtables_set_params(&xtg);
82 xtables_set_nfproto(NFPROTO_IPV4);
91 xtables_pending_matches = NULL;
92 xtables_pending_targets = NULL;
94 xtables_matches = NULL;
95 xtables_targets = NULL;
105 fw3_ipt_set_policy(struct fw3_ipt_handle *h, const char *chain,
106 enum fw3_flag policy)
108 if (h->family == FW3_FAMILY_V6)
109 ip6tc_set_policy(chain, fw3_flag_names[policy], NULL, h->handle);
111 iptc_set_policy(chain, fw3_flag_names[policy], NULL, h->handle);
115 fw3_ipt_delete_chain(struct fw3_ipt_handle *h, const char *chain)
119 printf("-F %s\n", chain);
120 printf("-X %s\n", chain);
123 if (h->family == FW3_FAMILY_V6)
125 if (ip6tc_flush_entries(chain, h->handle))
126 ip6tc_delete_chain(chain, h->handle);
130 if (iptc_flush_entries(chain, h->handle))
131 iptc_delete_chain(chain, h->handle);
136 fw3_ipt_delete_rules(struct fw3_ipt_handle *h, const char *target)
139 const struct ipt_entry *e;
140 const struct ip6t_entry *e6;
145 if (h->family == FW3_FAMILY_V6)
147 for (chain = ip6tc_first_chain(h->handle);
149 chain = ip6tc_next_chain(h->handle))
154 for (num = 0, e6 = ip6tc_first_rule(chain, h->handle);
156 num++, e6 = ip6tc_next_rule(e6, h->handle))
158 t = ip6tc_get_target(e6, h->handle);
160 if (*t && !strcmp(t, target))
163 printf("-D %s %u\n", chain, num + 1);
165 ip6tc_delete_num_entry(chain, num, h->handle);
175 for (chain = iptc_first_chain(h->handle);
177 chain = iptc_next_chain(h->handle))
182 for (num = 0, e = iptc_first_rule(chain, h->handle);
184 num++, e = iptc_next_rule(e, h->handle))
186 t = iptc_get_target(e, h->handle);
188 if (*t && !strcmp(t, target))
191 printf("-D %s %u\n", chain, num + 1);
193 iptc_delete_num_entry(chain, num, h->handle);
204 fw3_ipt_flush(struct fw3_ipt_handle *h)
208 if (h->family == FW3_FAMILY_V6)
210 for (chain = ip6tc_first_chain(h->handle);
212 chain = ip6tc_next_chain(h->handle))
214 ip6tc_flush_entries(chain, h->handle);
217 for (chain = ip6tc_first_chain(h->handle);
219 chain = ip6tc_next_chain(h->handle))
221 ip6tc_delete_chain(chain, h->handle);
226 for (chain = iptc_first_chain(h->handle);
228 chain = iptc_next_chain(h->handle))
230 iptc_flush_entries(chain, h->handle);
233 for (chain = iptc_first_chain(h->handle);
235 chain = iptc_next_chain(h->handle))
237 iptc_delete_chain(chain, h->handle);
243 fw3_ipt_commit(struct fw3_ipt_handle *h)
247 if (h->family == FW3_FAMILY_V6)
249 rv = ip6tc_commit(h->handle);
251 fprintf(stderr, "ip6tc_commit(): %s\n", ip6tc_strerror(errno));
255 rv = iptc_commit(h->handle);
257 fprintf(stderr, "iptc_commit(): %s\n", iptc_strerror(errno));
263 struct fw3_ipt_rule *
264 fw3_ipt_rule_new(struct fw3_ipt_handle *h)
266 struct fw3_ipt_rule *r;
268 r = fw3_alloc(sizeof(*r));
271 r->argv = fw3_alloc(sizeof(char *));
272 r->argv[r->argc++] = "fw3";
279 is_chain(struct fw3_ipt_handle *h, const char *name)
281 if (h->family == FW3_FAMILY_V6)
282 return ip6tc_is_chain(name, h->handle);
284 return iptc_is_chain(name, h->handle);
288 get_protoname(struct fw3_ipt_rule *r)
290 const struct xtables_pprot *pp;
293 for (pp = xtables_chain_protos; pp->name; pp++)
294 if (pp->num == r->protocol)
295 return (char *)pp->name;
300 static struct xtables_match *
301 find_match(struct fw3_ipt_rule *r, const char *name)
303 return xtables_find_match(name, XTF_TRY_LOAD, &r->matches);
307 init_match(struct fw3_ipt_rule *r, struct xtables_match *m, bool no_clone)
310 struct xtables_globals *g;
315 s = XT_ALIGN(sizeof(struct xt_entry_match)) + m->size;
318 strcpy(m->m->u.user.name, m->real_name ? m->real_name : m->name);
319 m->m->u.user.revision = m->revision;
320 m->m->u.match_size = s;
322 /* free previous userspace data */
326 m->udata = fw3_alloc(m->udata_size);
332 /* don't merge options if no_clone is set and this match is a clone */
333 if (no_clone && (m == m->next))
336 /* merge option table */
337 g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
340 g->opts = xtables_options_xfrm(g->orig_opts, g->opts,
341 m->x6_options, &m->option_offset);
344 g->opts = xtables_merge_options(g->orig_opts, g->opts,
345 m->extra_opts, &m->option_offset);
349 need_protomatch(struct fw3_ipt_rule *r, const char *pname)
354 if (!xtables_find_match(pname, XTF_DONT_LOAD, NULL))
357 return !r->protocol_loaded;
360 static struct xtables_match *
361 load_protomatch(struct fw3_ipt_rule *r)
363 const char *pname = get_protoname(r);
365 if (!need_protomatch(r, pname))
368 return find_match(r, pname);
371 static struct xtables_target *
372 get_target(struct fw3_ipt_rule *r, const char *name)
375 struct xtables_target *t;
376 struct xtables_globals *g;
378 bool chain = is_chain(r->h, name);
381 t = xtables_find_target(XT_STANDARD_TARGET, XTF_LOAD_MUST_SUCCEED);
383 t = xtables_find_target(name, XTF_TRY_LOAD);
388 s = XT_ALIGN(sizeof(struct xt_entry_target)) + t->size;
392 strcpy(t->t->u.user.name, name);
394 strcpy(t->t->u.user.name, t->real_name);
396 t->t->u.user.revision = t->revision;
397 t->t->u.target_size = s;
402 t->udata = fw3_alloc(t->udata_size);
408 /* merge option table */
409 g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
412 g->opts = xtables_options_xfrm(g->orig_opts, g->opts,
413 t->x6_options, &t->option_offset);
415 g->opts = xtables_merge_options(g->orig_opts, g->opts,
416 t->extra_opts, &t->option_offset);
424 fw3_ipt_rule_proto(struct fw3_ipt_rule *r, struct fw3_protocol *proto)
428 if (!proto || proto->any)
431 pr = proto->protocol;
433 if (r->h->family == FW3_FAMILY_V6)
438 r->e6.ipv6.proto = pr;
439 r->e6.ipv6.flags |= IP6T_F_PROTO;
442 r->e6.ipv6.invflags |= XT_INV_PROTO;
449 r->e.ip.invflags |= XT_INV_PROTO;
456 fw3_ipt_rule_in_out(struct fw3_ipt_rule *r,
457 struct fw3_device *in, struct fw3_device *out)
459 if (r->h->family == FW3_FAMILY_V6)
463 xtables_parse_interface(in->name, r->e6.ipv6.iniface,
464 r->e6.ipv6.iniface_mask);
467 r->e6.ipv6.invflags |= IP6T_INV_VIA_IN;
470 if (out && !out->any)
472 xtables_parse_interface(out->name, r->e6.ipv6.outiface,
473 r->e6.ipv6.outiface_mask);
476 r->e6.ipv6.invflags |= IP6T_INV_VIA_OUT;
483 xtables_parse_interface(in->name, r->e.ip.iniface,
484 r->e.ip.iniface_mask);
487 r->e.ip.invflags |= IPT_INV_VIA_IN;
490 if (out && !out->any)
492 xtables_parse_interface(out->name, r->e.ip.outiface,
493 r->e.ip.outiface_mask);
496 r->e.ip.invflags |= IPT_INV_VIA_OUT;
503 ip4prefix2mask(int prefix, struct in_addr *mask)
505 mask->s_addr = htonl(~((1 << (32 - prefix)) - 1));
509 ip6prefix2mask(int prefix, struct in6_addr *mask)
511 char *p = (char *)mask;
515 memset(p, 0xff, prefix / 8);
516 memset(p + (prefix / 8) + 1, 0, (128 - prefix) / 8);
517 p[prefix / 8] = 0xff << (8 - (prefix & 7));
521 memset(mask, 0, sizeof(*mask));
526 fw3_ipt_rule_src_dest(struct fw3_ipt_rule *r,
527 struct fw3_address *src, struct fw3_address *dest)
531 if ((src && src->range) || (dest && dest->range))
533 fw3_ipt_rule_addarg(r, false, "-m", "iprange");
540 fw3_ipt_rule_addarg(r, src->invert, "--src-range",
541 fw3_address_to_string(src, false));
543 else if (r->h->family == FW3_FAMILY_V6)
545 r->e6.ipv6.src = src->address.v6;
546 ip6prefix2mask(src->mask, &r->e6.ipv6.smsk);
548 for (i = 0; i < 4; i++)
549 r->e6.ipv6.src.s6_addr32[i] &= r->e6.ipv6.smsk.s6_addr32[i];
552 r->e6.ipv6.invflags |= IP6T_INV_SRCIP;
556 r->e.ip.src = src->address.v4;
557 ip4prefix2mask(src->mask, &r->e.ip.smsk);
559 r->e.ip.src.s_addr &= r->e.ip.smsk.s_addr;
562 r->e.ip.invflags |= IPT_INV_SRCIP;
566 if (dest && dest->set)
570 fw3_ipt_rule_addarg(r, dest->invert, "--dst-range",
571 fw3_address_to_string(dest, false));
573 else if (r->h->family == FW3_FAMILY_V6)
575 r->e6.ipv6.dst = dest->address.v6;
576 ip6prefix2mask(dest->mask, &r->e6.ipv6.dmsk);
578 for (i = 0; i < 4; i++)
579 r->e6.ipv6.dst.s6_addr32[i] &= r->e6.ipv6.dmsk.s6_addr32[i];
582 r->e6.ipv6.invflags |= IP6T_INV_DSTIP;
586 r->e.ip.dst = dest->address.v4;
587 ip4prefix2mask(dest->mask, &r->e.ip.dmsk);
589 r->e.ip.dst.s_addr &= r->e.ip.dmsk.s_addr;
592 r->e.ip.invflags |= IPT_INV_DSTIP;
598 fw3_ipt_rule_sport_dport(struct fw3_ipt_rule *r,
599 struct fw3_port *sp, struct fw3_port *dp)
601 char buf[sizeof("65535:65535\0")];
603 if ((!sp || !sp->set) && (!dp || !dp->set))
606 if (!get_protoname(r))
611 if (sp->port_min == sp->port_max)
612 sprintf(buf, "%u", sp->port_min);
614 sprintf(buf, "%u:%u", sp->port_min, sp->port_max);
616 fw3_ipt_rule_addarg(r, sp->invert, "--sport", buf);
621 if (dp->port_min == dp->port_max)
622 sprintf(buf, "%u", dp->port_min);
624 sprintf(buf, "%u:%u", dp->port_min, dp->port_max);
626 fw3_ipt_rule_addarg(r, dp->invert, "--dport", buf);
631 fw3_ipt_rule_mac(struct fw3_ipt_rule *r, struct fw3_mac *mac)
636 fw3_ipt_rule_addarg(r, false, "-m", "mac");
637 fw3_ipt_rule_addarg(r, mac->invert, "--mac-source", ether_ntoa(&mac->mac));
641 fw3_ipt_rule_icmptype(struct fw3_ipt_rule *r, struct fw3_icmptype *icmp)
643 char buf[sizeof("255/255\0")];
648 if (r->h->family == FW3_FAMILY_V6)
650 if (icmp->code6_min == 0 && icmp->code6_max == 0xFF)
651 sprintf(buf, "%u", icmp->type6);
653 sprintf(buf, "%u/%u", icmp->type6, icmp->code6_min);
655 fw3_ipt_rule_addarg(r, icmp->invert, "--icmpv6-type", buf);
659 if (icmp->code_min == 0 && icmp->code_max == 0xFF)
660 sprintf(buf, "%u", icmp->type);
662 sprintf(buf, "%u/%u", icmp->type, icmp->code_min);
664 fw3_ipt_rule_addarg(r, icmp->invert, "--icmp-type", buf);
669 fw3_ipt_rule_limit(struct fw3_ipt_rule *r, struct fw3_limit *limit)
671 char buf[sizeof("-4294967296/second\0")];
673 if (!limit || limit->rate <= 0)
676 fw3_ipt_rule_addarg(r, false, "-m", "limit");
678 sprintf(buf, "%u/%s", limit->rate, fw3_limit_units[limit->unit]);
679 fw3_ipt_rule_addarg(r, limit->invert, "--limit", buf);
681 if (limit->burst > 0)
683 sprintf(buf, "%u", limit->burst);
684 fw3_ipt_rule_addarg(r, limit->invert, "--limit-burst", buf);
689 fw3_ipt_rule_ipset(struct fw3_ipt_rule *r, struct fw3_ipset *ipset,
692 char buf[sizeof("dst,dst,dst\0")];
695 struct fw3_ipset_datatype *type;
700 list_for_each_entry(type, &ipset->datatypes, list)
705 p += sprintf(p, "%s", type->dest ? "dst" : "src");
708 fw3_ipt_rule_addarg(r, false, "-m", "set");
710 fw3_ipt_rule_addarg(r, invert, "--match-set",
711 ipset->external ? ipset->external : ipset->name);
713 fw3_ipt_rule_addarg(r, false, buf, NULL);
717 fw3_ipt_rule_time(struct fw3_ipt_rule *r, struct fw3_time *time)
720 struct tm empty = { 0 };
722 char buf[84]; /* sizeof("1,2,3,...,30,31\0") */
725 bool d1 = memcmp(&time->datestart, &empty, sizeof(empty));
726 bool d2 = memcmp(&time->datestop, &empty, sizeof(empty));
728 if (!d1 && !d2 && !time->timestart && !time->timestop &&
729 !(time->monthdays & 0xFFFFFFFE) && !(time->weekdays & 0xFE))
734 fw3_ipt_rule_addarg(r, false, "-m", "time");
737 fw3_ipt_rule_addarg(r, false, "--utc", NULL);
741 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestart);
742 fw3_ipt_rule_addarg(r, false, "--datestart", buf);
747 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestop);
748 fw3_ipt_rule_addarg(r, false, "--datestop", buf);
753 sprintf(buf, "%02d:%02d:%02d",
754 time->timestart / 3600,
755 time->timestart % 3600 / 60,
756 time->timestart % 60);
758 fw3_ipt_rule_addarg(r, false, "--timestart", buf);
763 sprintf(buf, "%02d:%02d:%02d",
764 time->timestop / 3600,
765 time->timestop % 3600 / 60,
766 time->timestop % 60);
768 fw3_ipt_rule_addarg(r, false, "--timestop", buf);
771 if (time->monthdays & 0xFFFFFFFE)
773 for (i = 1, p = buf; i < 32; i++)
775 if (hasbit(time->monthdays, i))
780 p += sprintf(p, "%u", i);
784 fw3_ipt_rule_addarg(r, hasbit(time->monthdays, 0), "--monthdays", buf);
787 if (time->weekdays & 0xFE)
789 for (i = 1, p = buf; i < 8; i++)
791 if (hasbit(time->weekdays, i))
796 p += sprintf(p, "%u", i);
800 fw3_ipt_rule_addarg(r, hasbit(time->weekdays, 0), "--weekdays", buf);
805 fw3_ipt_rule_mark(struct fw3_ipt_rule *r, struct fw3_mark *mark)
807 char buf[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
809 if (!mark || !mark->set)
812 if (mark->mask < 0xFFFFFFFF)
813 sprintf(buf, "0x%x/0x%x", mark->mark, mark->mask);
815 sprintf(buf, "0x%x", mark->mark);
817 fw3_ipt_rule_addarg(r, false, "-m", "mark");
818 fw3_ipt_rule_addarg(r, mark->invert, "--mark", buf);
822 fw3_ipt_rule_comment(struct fw3_ipt_rule *r, const char *fmt, ...)
831 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
834 fw3_ipt_rule_addarg(r, false, "-m", "comment");
835 fw3_ipt_rule_addarg(r, false, "--comment", buf);
839 fw3_ipt_rule_extra(struct fw3_ipt_rule *r, const char *extra)
843 if (!extra || !*extra)
846 s = fw3_strdup(extra);
848 for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
850 tmp = realloc(r->argv, (r->argc + 1) * sizeof(*r->argv));
856 r->argv[r->argc++] = fw3_strdup(p);
863 rule_print6(struct ip6t_entry *e)
865 char buf[INET6_ADDRSTRLEN];
868 if (e->ipv6.flags & IP6T_F_PROTO)
870 if (e->ipv6.flags & XT_INV_PROTO)
873 pname = get_protoname(container_of(e, struct fw3_ipt_rule, e6));
876 printf(" -p %s", pname);
878 printf(" -p %u", e->ipv6.proto);
881 if (e->ipv6.iniface[0])
883 if (e->ipv6.flags & IP6T_INV_VIA_IN)
886 printf(" -i %s", e->ipv6.iniface);
889 if (e->ipv6.outiface[0])
891 if (e->ipv6.flags & IP6T_INV_VIA_OUT)
894 printf(" -o %s", e->ipv6.outiface);
897 if (memcmp(&e->ipv6.src, &in6addr_any, sizeof(struct in6_addr)))
899 if (e->ipv6.flags & IP6T_INV_SRCIP)
902 printf(" -s %s/%u", inet_ntop(AF_INET6, &e->ipv6.src, buf, sizeof(buf)),
903 xtables_ip6mask_to_cidr(&e->ipv6.smsk));
906 if (memcmp(&e->ipv6.dst, &in6addr_any, sizeof(struct in6_addr)))
908 if (e->ipv6.flags & IP6T_INV_DSTIP)
911 printf(" -d %s/%u", inet_ntop(AF_INET6, &e->ipv6.dst, buf, sizeof(buf)),
912 xtables_ip6mask_to_cidr(&e->ipv6.dmsk));
917 rule_print4(struct ipt_entry *e)
919 struct in_addr in_zero = { 0 };
920 char buf[sizeof("255.255.255.255\0")];
925 if (e->ip.flags & XT_INV_PROTO)
928 pname = get_protoname(container_of(e, struct fw3_ipt_rule, e));
931 printf(" -p %s", pname);
933 printf(" -p %u", e->ip.proto);
936 if (e->ip.iniface[0])
938 if (e->ip.flags & IPT_INV_VIA_IN)
941 printf(" -i %s", e->ip.iniface);
944 if (e->ip.outiface[0])
946 if (e->ip.flags & IPT_INV_VIA_OUT)
949 printf(" -o %s", e->ip.outiface);
952 if (memcmp(&e->ip.src, &in_zero, sizeof(struct in_addr)))
954 if (e->ip.flags & IPT_INV_SRCIP)
957 printf(" -s %s/%u", inet_ntop(AF_INET, &e->ip.src, buf, sizeof(buf)),
958 xtables_ipmask_to_cidr(&e->ip.smsk));
961 if (memcmp(&e->ip.dst, &in_zero, sizeof(struct in_addr)))
963 if (e->ip.flags & IPT_INV_DSTIP)
966 printf(" -d %s/%u", inet_ntop(AF_INET, &e->ip.dst, buf, sizeof(buf)),
967 xtables_ipmask_to_cidr(&e->ip.dmsk));
972 rule_print(struct fw3_ipt_rule *r, const char *chain)
974 struct xtables_rule_match *rm;
975 struct xtables_match *m;
976 struct xtables_target *t;
978 printf("-A %s", chain);
980 if (r->h->family == FW3_FAMILY_V6)
985 for (rm = r->matches; rm; rm = rm->next)
988 printf(" -m %s", m->alias ? m->alias(m->m) : m->m->u.user.name);
991 m->save(&r->e.ip, m->m);
997 printf(" -j %s", t->alias ? t->alias(t->t) : t->t->u.user.name);
1000 t->save(&r->e.ip, t->t);
1007 parse_option(struct fw3_ipt_rule *r, int optc, bool inv)
1009 struct xtables_rule_match *m;
1010 struct xtables_match *em;
1012 /* is a target option */
1013 if (r->target && (r->target->parse || r->target->x6_parse) &&
1014 optc >= r->target->option_offset &&
1015 optc < (r->target->option_offset + 256))
1017 xtables_option_tpcall(optc, r->argv, inv, r->target, &r->e);
1021 /* try to dispatch argument to one of the match parsers */
1022 for (m = r->matches; m; m = m->next)
1026 if (m->completed || (!em->parse && !em->x6_parse))
1029 if (optc < em->option_offset ||
1030 optc >= (em->option_offset + 256))
1033 xtables_option_mpcall(optc, r->argv, inv, em, &r->e);
1037 /* unhandled option, might belong to a protocol match */
1038 if ((em = load_protomatch(r)) != NULL)
1040 init_match(r, em, false);
1042 r->protocol_loaded = true;
1049 fprintf(stderr, "parse_option(): option '%s' needs argument\n",
1053 fprintf(stderr, "parse_option(): unknown option '%s'\n",
1060 fw3_ipt_rule_addarg(struct fw3_ipt_rule *r, bool inv,
1061 const char *k, const char *v)
1069 n = inv + !!k + !!v;
1070 tmp = realloc(r->argv, (r->argc + n) * sizeof(*tmp));
1078 r->argv[r->argc++] = fw3_strdup("!");
1080 r->argv[r->argc++] = fw3_strdup(k);
1083 r->argv[r->argc++] = fw3_strdup(v);
1087 fw3_ipt_rule_append(struct fw3_ipt_rule *r, const char *fmt, ...)
1090 struct xtables_rule_match *m;
1091 struct xtables_match *em;
1092 struct xtables_target *et;
1093 struct xtables_globals *g;
1094 struct ipt_entry *e;
1095 struct ip6t_entry *e6;
1103 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1106 g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
1107 g->opts = g->orig_opts;
1112 while ((optc = getopt_long(r->argc, r->argv, "m:j:", g->opts, NULL)) != -1)
1117 em = find_match(r, optarg);
1121 fprintf(stderr, "fw3_ipt_rule_append(): Can't find match '%s'\n", optarg);
1125 init_match(r, em, true);
1129 et = get_target(r, optarg);
1133 fprintf(stderr, "fw3_ipt_rule_append(): Can't find target '%s'\n", optarg);
1140 if ((optarg[0] == '!') && (optarg[1] == '\0'))
1146 fprintf(stderr, "fw3_ipt_rule_append(): Bad argument '%s'\n", optarg);
1150 if (parse_option(r, optc, inv))
1158 for (m = r->matches; m; m = m->next)
1159 xtables_option_mfcall(m->match);
1162 xtables_option_tfcall(r->target);
1167 if (r->h->family == FW3_FAMILY_V6)
1169 s = XT_ALIGN(sizeof(struct ip6t_entry));
1171 for (m = r->matches; m; m = m->next)
1172 s += m->match->m->u.match_size;
1174 e6 = fw3_alloc(s + r->target->t->u.target_size);
1176 memcpy(e6, &r->e6, sizeof(struct ip6t_entry));
1178 e6->target_offset = s;
1179 e6->next_offset = s + r->target->t->u.target_size;
1183 for (m = r->matches; m; m = m->next)
1185 memcpy(e6->elems + s, m->match->m, m->match->m->u.match_size);
1186 s += m->match->m->u.match_size;
1189 memcpy(e6->elems + s, r->target->t, r->target->t->u.target_size);
1190 ip6tc_append_entry(buf, e6, r->h->handle);
1195 s = XT_ALIGN(sizeof(struct ipt_entry));
1197 for (m = r->matches; m; m = m->next)
1198 s += m->match->m->u.match_size;
1200 e = fw3_alloc(s + r->target->t->u.target_size);
1202 memcpy(e, &r->e, sizeof(struct ipt_entry));
1204 e->target_offset = s;
1205 e->next_offset = s + r->target->t->u.target_size;
1209 for (m = r->matches; m; m = m->next)
1211 memcpy(e->elems + s, m->match->m, m->match->m->u.match_size);
1212 s += m->match->m->u.match_size;
1215 memcpy(e->elems + s, r->target->t, r->target->t->u.target_size);
1217 if (!iptc_append_entry(buf, e, r->h->handle))
1218 fprintf(stderr, "iptc_append_entry(): %s\n", iptc_strerror(errno));
1223 for (i = 1; i < r->argc; i++)
1228 xtables_rule_matches_free(&r->matches);
1233 /* reset all targets and matches */
1234 for (em = xtables_matches; em; em = em->next)
1237 for (et = xtables_targets; et; et = et->next)
1243 xtables_free_opts(1);
1246 struct fw3_ipt_rule *
1247 fw3_ipt_rule_create(struct fw3_ipt_handle *handle, struct fw3_protocol *proto,
1248 struct fw3_device *in, struct fw3_device *out,
1249 struct fw3_address *src, struct fw3_address *dest)
1251 struct fw3_ipt_rule *r;
1253 r = fw3_ipt_rule_new(handle);
1255 fw3_ipt_rule_proto(r, proto);
1256 fw3_ipt_rule_in_out(r, in, out);
1257 fw3_ipt_rule_src_dest(r, src, dest);