treewide: replace jow@openwrt.org with jo@mein.io
[project/firewall3.git] / iptables.c
1 /*
2  * firewall3 - 3rd OpenWrt UCI firewall implementation
3  *
4  *   Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
5  *
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.
9  *
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.
17  */
18
19 #include "iptables.h"
20
21
22 static struct option base_opts[] = {
23         { .name = "match",  .has_arg = 1, .val = 'm' },
24         { .name = "jump",   .has_arg = 1, .val = 'j' },
25         { NULL }
26 };
27
28 static struct xtables_globals xtg = {
29         .option_offset = 0,
30         .program_version = "4",
31         .orig_opts = base_opts,
32 };
33
34 static struct xtables_globals xtg6 = {
35         .option_offset = 0,
36         .program_version = "6",
37         .orig_opts = base_opts,
38 };
39
40 /* Required by certain extensions like SNAT and DNAT */
41 int kernel_version = 0;
42
43 void
44 get_kernel_version(void)
45 {
46         static struct utsname uts;
47         int x = 0, y = 0, z = 0;
48
49         if (uname(&uts) == -1)
50                 sprintf(uts.release, "3.0.0");
51
52         sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
53         kernel_version = 0x10000 * x + 0x100 * y + z;
54 }
55
56 static void fw3_init_extensions(void)
57 {
58         init_extensions();
59         init_extensions4();
60
61 #ifndef DISABLE_IPV6
62         init_extensions6();
63 #endif
64 }
65
66 struct fw3_ipt_handle *
67 fw3_ipt_open(enum fw3_family family, enum fw3_table table)
68 {
69         struct fw3_ipt_handle *h;
70
71         h = fw3_alloc(sizeof(*h));
72
73         xtables_init();
74
75         if (family == FW3_FAMILY_V6)
76         {
77 #ifndef DISABLE_IPV6
78                 h->family = FW3_FAMILY_V6;
79                 h->table  = table;
80                 h->handle = ip6tc_init(fw3_flag_names[table]);
81
82                 xtables_set_params(&xtg6);
83                 xtables_set_nfproto(NFPROTO_IPV6);
84 #endif
85         }
86         else
87         {
88                 h->family = FW3_FAMILY_V4;
89                 h->table  = table;
90                 h->handle = iptc_init(fw3_flag_names[table]);
91
92                 xtables_set_params(&xtg);
93                 xtables_set_nfproto(NFPROTO_IPV4);
94         }
95
96         if (!h->handle)
97         {
98                 free(h);
99                 return NULL;
100         }
101
102         fw3_xt_reset();
103         fw3_init_extensions();
104
105         return h;
106 }
107
108 static void
109 debug(struct fw3_ipt_handle *h, const char *fmt, ...)
110 {
111         va_list ap;
112
113         printf("%s -t %s ", (h->family == FW3_FAMILY_V6) ? "ip6tables" : "iptables",
114                             fw3_flag_names[h->table]);
115
116         va_start(ap, fmt);
117         vprintf(fmt, ap);
118         va_end(ap);
119 }
120
121 void
122 fw3_ipt_set_policy(struct fw3_ipt_handle *h, const char *chain,
123                    enum fw3_flag policy)
124 {
125         if (fw3_pr_debug)
126                 debug(h, "-P %s %s\n", chain, fw3_flag_names[policy]);
127
128 #ifndef DISABLE_IPV6
129         if (h->family == FW3_FAMILY_V6)
130                 ip6tc_set_policy(chain, fw3_flag_names[policy], NULL, h->handle);
131         else
132 #endif
133                 iptc_set_policy(chain, fw3_flag_names[policy], NULL, h->handle);
134 }
135
136 void
137 fw3_ipt_flush_chain(struct fw3_ipt_handle *h, const char *chain)
138 {
139         if (fw3_pr_debug)
140                 debug(h, "-F %s\n", chain);
141
142 #ifndef DISABLE_IPV6
143         if (h->family == FW3_FAMILY_V6)
144                 ip6tc_flush_entries(chain, h->handle);
145         else
146 #endif
147                 iptc_flush_entries(chain, h->handle);
148 }
149
150 static void
151 delete_rules(struct fw3_ipt_handle *h, const char *target)
152 {
153         unsigned int num;
154         const struct ipt_entry *e;
155         const char *chain;
156         const char *t;
157         bool found;
158
159 #ifndef DISABLE_IPV6
160         if (h->family == FW3_FAMILY_V6)
161         {
162                 for (chain = ip6tc_first_chain(h->handle);
163                      chain != NULL;
164                      chain = ip6tc_next_chain(h->handle))
165                 {
166                         do {
167                                 found = false;
168
169                                 const struct ip6t_entry *e6;
170                                 for (num = 0, e6 = ip6tc_first_rule(chain, h->handle);
171                                          e6 != NULL;
172                                          num++, e6 = ip6tc_next_rule(e6, h->handle))
173                                 {
174                                         t = ip6tc_get_target(e6, h->handle);
175
176                                         if (*t && !strcmp(t, target))
177                                         {
178                                                 if (fw3_pr_debug)
179                                                         debug(h, "-D %s %u\n", chain, num + 1);
180
181                                                 ip6tc_delete_num_entry(chain, num, h->handle);
182                                                 found = true;
183                                                 break;
184                                         }
185                                 }
186                         } while (found);
187                 }
188         }
189         else
190 #endif
191         {
192                 for (chain = iptc_first_chain(h->handle);
193                      chain != NULL;
194                      chain = iptc_next_chain(h->handle))
195                 {
196                         do {
197                                 found = false;
198
199                                 for (num = 0, e = iptc_first_rule(chain, h->handle);
200                                      e != NULL;
201                                          num++, e = iptc_next_rule(e, h->handle))
202                                 {
203                                         t = iptc_get_target(e, h->handle);
204
205                                         if (*t && !strcmp(t, target))
206                                         {
207                                                 if (fw3_pr_debug)
208                                                         debug(h, "-D %s %u\n", chain, num + 1);
209
210                                                 iptc_delete_num_entry(chain, num, h->handle);
211                                                 found = true;
212                                                 break;
213                                         }
214                                 }
215                         } while (found);
216                 }
217         }
218 }
219
220 void
221 fw3_ipt_delete_chain(struct fw3_ipt_handle *h, const char *chain)
222 {
223         delete_rules(h, chain);
224
225         if (fw3_pr_debug)
226                 debug(h, "-X %s\n", chain);
227
228 #ifndef DISABLE_IPV6
229         if (h->family == FW3_FAMILY_V6)
230                 ip6tc_delete_chain(chain, h->handle);
231         else
232 #endif
233                 iptc_delete_chain(chain, h->handle);
234 }
235
236 static int
237 get_rule_id(const void *base, unsigned int start, unsigned int end)
238 {
239         uint32_t id;
240         unsigned int i;
241         const struct xt_entry_match *em;
242
243         for (i = start; i < end; i += em->u.match_size)
244         {
245                 em = base + i;
246
247                 if (strcmp(em->u.user.name, "id"))
248                         continue;
249
250                 memcpy(&id, em->data, sizeof(id));
251
252                 if ((id & FW3_ID_MASK) != FW3_ID_MAGIC)
253                         continue;
254
255                 return (id & ~FW3_ID_MASK);
256         }
257
258         return -1;
259 }
260
261 void
262 fw3_ipt_delete_id_rules(struct fw3_ipt_handle *h, const char *chain)
263 {
264         unsigned int num;
265         const struct ipt_entry *e;
266         bool found;
267         int id;
268
269 #ifndef DISABLE_IPV6
270         if (h->family == FW3_FAMILY_V6)
271         {
272                 if (!ip6tc_is_chain(chain, h->handle))
273                         return;
274
275                 do {
276                         found = false;
277
278                         const struct ip6t_entry *e6;
279                         for (num = 0, e6 = ip6tc_first_rule(chain, h->handle);
280                                  e6 != NULL;
281                                  num++, e6 = ip6tc_next_rule(e6, h->handle))
282                         {
283                                 id = get_rule_id(e6, sizeof(*e6), e6->target_offset);
284
285                                 if (id >= 0)
286                                 {
287                                         if (fw3_pr_debug)
288                                                 debug(h, "-D %s %u\n", chain, num + 1);
289
290                                         ip6tc_delete_num_entry(chain, num, h->handle);
291                                         found = true;
292                                         break;
293                                 }
294                         }
295                 } while (found);
296         }
297         else
298 #endif
299         {
300                 if (!iptc_is_chain(chain, h->handle))
301                         return;
302
303                 do {
304                         found = false;
305
306                         for (num = 0, e = iptc_first_rule(chain, h->handle);
307                                  e != NULL;
308                                  num++, e = iptc_next_rule(e, h->handle))
309                         {
310                                 id = get_rule_id(e, sizeof(*e), e->target_offset);
311
312                                 if (id >= 0)
313                                 {
314                                         if (fw3_pr_debug)
315                                                 debug(h, "-D %s %u\n", chain, num + 1);
316
317                                         iptc_delete_num_entry(chain, num, h->handle);
318                                         found = true;
319                                         break;
320                                 }
321                         }
322                 } while (found);
323         }
324 }
325
326 void
327 fw3_ipt_create_chain(struct fw3_ipt_handle *h, const char *fmt, ...)
328 {
329         char buf[32];
330         va_list ap;
331
332         va_start(ap, fmt);
333         vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
334         va_end(ap);
335
336         if (fw3_pr_debug)
337                 debug(h, "-N %s\n", buf);
338
339         iptc_create_chain(buf, h->handle);
340 }
341
342 void
343 fw3_ipt_flush(struct fw3_ipt_handle *h)
344 {
345         const char *chain;
346
347 #ifndef DISABLE_IPV6
348         if (h->family == FW3_FAMILY_V6)
349         {
350                 for (chain = ip6tc_first_chain(h->handle);
351                      chain != NULL;
352                      chain = ip6tc_next_chain(h->handle))
353                 {
354                         ip6tc_flush_entries(chain, h->handle);
355                 }
356
357                 for (chain = ip6tc_first_chain(h->handle);
358                      chain != NULL;
359                      chain = ip6tc_next_chain(h->handle))
360                 {
361                         ip6tc_delete_chain(chain, h->handle);
362                 }
363         }
364         else
365 #endif
366         {
367                 for (chain = iptc_first_chain(h->handle);
368                      chain != NULL;
369                      chain = iptc_next_chain(h->handle))
370                 {
371                         iptc_flush_entries(chain, h->handle);
372                 }
373
374                 for (chain = iptc_first_chain(h->handle);
375                      chain != NULL;
376                      chain = iptc_next_chain(h->handle))
377                 {
378                         iptc_delete_chain(chain, h->handle);
379                 }
380         }
381 }
382
383 static bool
384 chain_is_empty(struct fw3_ipt_handle *h, const char *chain)
385 {
386 #ifndef DISABLE_IPV6
387         if (h->family == FW3_FAMILY_V6)
388                 return (!ip6tc_builtin(chain, h->handle) &&
389                         !ip6tc_first_rule(chain, h->handle));
390 #endif
391
392         return (!iptc_builtin(chain, h->handle) &&
393                 !iptc_first_rule(chain, h->handle));
394 }
395
396 void
397 fw3_ipt_gc(struct fw3_ipt_handle *h)
398 {
399         const char *chain;
400         bool found;
401
402 #ifndef DISABLE_IPV6
403         if (h->family == FW3_FAMILY_V6)
404         {
405                 do {
406                         found = false;
407
408                         for (chain = ip6tc_first_chain(h->handle);
409                                  chain != NULL;
410                                  chain = ip6tc_next_chain(h->handle))
411                         {
412                                 if (!chain_is_empty(h, chain))
413                                         continue;
414
415                                 fw3_ipt_delete_chain(h, chain);
416                                 found = true;
417                                 break;
418                         }
419                 } while(found);
420         }
421         else
422 #endif
423         {
424                 do {
425                         found = false;
426
427                         for (chain = iptc_first_chain(h->handle);
428                                  chain != NULL;
429                                  chain = iptc_next_chain(h->handle))
430                         {
431                                 warn("C=%s\n", chain);
432
433                                 if (!chain_is_empty(h, chain))
434                                         continue;
435
436                                 warn("D=%s\n", chain);
437
438                                 fw3_ipt_delete_chain(h, chain);
439                                 found = true;
440                                 break;
441                         }
442                 } while (found);
443         }
444 }
445
446 void
447 fw3_ipt_commit(struct fw3_ipt_handle *h)
448 {
449         int rv;
450
451 #ifndef DISABLE_IPV6
452         if (h->family == FW3_FAMILY_V6)
453         {
454                 rv = ip6tc_commit(h->handle);
455                 if (!rv)
456                         warn("ip6tc_commit(): %s", ip6tc_strerror(errno));
457         }
458         else
459 #endif
460         {
461                 rv = iptc_commit(h->handle);
462                 if (!rv)
463                         warn("iptc_commit(): %s", iptc_strerror(errno));
464         }
465 }
466
467 void
468 fw3_ipt_close(struct fw3_ipt_handle *h)
469 {
470         if (h->libv)
471         {
472                 while (h->libc > 0)
473                 {
474                         h->libc--;
475                         dlclose(h->libv[h->libc]);
476                 }
477
478                 free(h->libv);
479         }
480
481         free(h);
482 }
483
484 struct fw3_ipt_rule *
485 fw3_ipt_rule_new(struct fw3_ipt_handle *h)
486 {
487         struct fw3_ipt_rule *r;
488
489         r = fw3_alloc(sizeof(*r));
490
491         r->h = h;
492         r->id = 0;
493         r->argv = fw3_alloc(sizeof(char *));
494         r->argv[r->argc++] = "fw3";
495
496         return r;
497 }
498
499
500 static bool
501 is_chain(struct fw3_ipt_handle *h, const char *name)
502 {
503 #ifndef DISABLE_IPV6
504         if (h->family == FW3_FAMILY_V6)
505                 return ip6tc_is_chain(name, h->handle);
506         else
507 #endif
508                 return iptc_is_chain(name, h->handle);
509 }
510
511 static char *
512 get_protoname(struct fw3_ipt_rule *r)
513 {
514         const struct xtables_pprot *pp;
515
516         if (r->protocol)
517                 for (pp = xtables_chain_protos; pp->name; pp++)
518                         if (pp->num == r->protocol)
519                                 return (char *)pp->name;
520
521         return NULL;
522 }
523
524 static bool
525 load_extension(struct fw3_ipt_handle *h, const char *name)
526 {
527         char path[256];
528         void *lib, **tmp;
529         const char *pfx = (h->family == FW3_FAMILY_V6) ? "libip6t" : "libipt";
530
531         snprintf(path, sizeof(path), "/usr/lib/iptables/libxt_%s.so", name);
532         if (!(lib = dlopen(path, RTLD_NOW)))
533         {
534                 snprintf(path, sizeof(path), "/usr/lib/iptables/%s_%s.so", pfx, name);
535                 lib = dlopen(path, RTLD_NOW);
536         }
537
538         if (!lib)
539                 return false;
540
541         tmp = realloc(h->libv, sizeof(lib) * (h->libc + 1));
542
543         if (!tmp)
544                 return false;
545
546         h->libv = tmp;
547         h->libv[h->libc++] = lib;
548
549         return true;
550 }
551
552 static struct xtables_match *
553 find_match(struct fw3_ipt_rule *r, const char *name)
554 {
555         struct xtables_match *m;
556
557         m = xtables_find_match(name, XTF_DONT_LOAD, &r->matches);
558
559         if (!m && load_extension(r->h, name))
560                 m = xtables_find_match(name, XTF_DONT_LOAD, &r->matches);
561
562         return m;
563 }
564
565 static void
566 init_match(struct fw3_ipt_rule *r, struct xtables_match *m, bool no_clone)
567 {
568         size_t s;
569         struct xtables_globals *g;
570
571         if (!m)
572                 return;
573
574         s = XT_ALIGN(sizeof(struct xt_entry_match)) + m->size;
575
576         m->m = fw3_alloc(s);
577
578         fw3_xt_set_match_name(m);
579
580         m->m->u.user.revision = m->revision;
581         m->m->u.match_size = s;
582
583         /* free previous userspace data */
584         fw3_xt_free_match_udata(m);
585
586         if (m->init)
587                 m->init(m->m);
588
589         /* don't merge options if no_clone is set and this match is a clone */
590         if (no_clone && (m == m->next))
591                 return;
592
593         /* merge option table */
594         g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
595         fw3_xt_merge_match_options(g, m);
596 }
597
598 static bool
599 need_protomatch(struct fw3_ipt_rule *r, const char *pname)
600 {
601         if (!pname)
602                 return false;
603
604         if (!xtables_find_match(pname, XTF_DONT_LOAD, NULL))
605                 return true;
606
607         return !r->protocol_loaded;
608 }
609
610 static struct xtables_match *
611 load_protomatch(struct fw3_ipt_rule *r)
612 {
613         const char *pname = get_protoname(r);
614
615         if (!need_protomatch(r, pname))
616                 return NULL;
617
618         return find_match(r, pname);
619 }
620
621 static struct xtables_target *
622 find_target(struct fw3_ipt_rule *r, const char *name)
623 {
624         struct xtables_target *t;
625
626         if (is_chain(r->h, name))
627                 return xtables_find_target(XT_STANDARD_TARGET, XTF_LOAD_MUST_SUCCEED);
628
629         t = xtables_find_target(name, XTF_DONT_LOAD);
630
631         if (!t && load_extension(r->h, name))
632                 t = xtables_find_target(name, XTF_DONT_LOAD);
633
634         return t;
635 }
636
637 static struct xtables_target *
638 get_target(struct fw3_ipt_rule *r, const char *name)
639 {
640         size_t s;
641         struct xtables_target *t;
642         struct xtables_globals *g;
643
644         t = find_target(r, name);
645
646         if (!t)
647                 return NULL;
648
649         s = XT_ALIGN(sizeof(struct xt_entry_target)) + t->size;
650         t->t = fw3_alloc(s);
651
652         fw3_xt_set_target_name(t, name);
653
654         t->t->u.user.revision = t->revision;
655         t->t->u.target_size = s;
656
657         /* free previous userspace data */
658         fw3_xt_free_target_udata(t);
659
660         if (t->init)
661                 t->init(t->t);
662
663         /* merge option table */
664         g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
665         fw3_xt_merge_target_options(g, t);
666
667         r->target = t;
668
669         return t;
670 }
671
672 void
673 fw3_ipt_rule_proto(struct fw3_ipt_rule *r, struct fw3_protocol *proto)
674 {
675         uint32_t pr;
676
677         if (!proto || proto->any)
678                 return;
679
680         pr = proto->protocol;
681
682 #ifndef DISABLE_IPV6
683         if (r->h->family == FW3_FAMILY_V6)
684         {
685                 if (pr == 1)
686                         pr = 58;
687
688                 r->e6.ipv6.proto = pr;
689                 r->e6.ipv6.flags |= IP6T_F_PROTO;
690
691                 if (proto->invert)
692                         r->e6.ipv6.invflags |= XT_INV_PROTO;
693         }
694         else
695 #endif
696         {
697                 r->e.ip.proto = pr;
698
699                 if (proto->invert)
700                         r->e.ip.invflags |= XT_INV_PROTO;
701         }
702
703         r->protocol = pr;
704 }
705
706 void
707 fw3_ipt_rule_in_out(struct fw3_ipt_rule *r,
708                     struct fw3_device *in, struct fw3_device *out)
709 {
710 #ifndef DISABLE_IPV6
711         if (r->h->family == FW3_FAMILY_V6)
712         {
713                 if (in && !in->any)
714                 {
715                         xtables_parse_interface(in->name, r->e6.ipv6.iniface,
716                                                           r->e6.ipv6.iniface_mask);
717
718                         if (in->invert)
719                                 r->e6.ipv6.invflags |= IP6T_INV_VIA_IN;
720                 }
721
722                 if (out && !out->any)
723                 {
724                         xtables_parse_interface(out->name, r->e6.ipv6.outiface,
725                                                            r->e6.ipv6.outiface_mask);
726
727                         if (out->invert)
728                                 r->e6.ipv6.invflags |= IP6T_INV_VIA_OUT;
729                 }
730         }
731         else
732 #endif
733         {
734                 if (in && !in->any)
735                 {
736                         xtables_parse_interface(in->name, r->e.ip.iniface,
737                                                           r->e.ip.iniface_mask);
738
739                         if (in->invert)
740                                 r->e.ip.invflags |= IPT_INV_VIA_IN;
741                 }
742
743                 if (out && !out->any)
744                 {
745                         xtables_parse_interface(out->name, r->e.ip.outiface,
746                                                            r->e.ip.outiface_mask);
747
748                         if (out->invert)
749                                 r->e.ip.invflags |= IPT_INV_VIA_OUT;
750                 }
751         }
752 }
753
754
755 void
756 fw3_ipt_rule_src_dest(struct fw3_ipt_rule *r,
757                       struct fw3_address *src, struct fw3_address *dest)
758 {
759         if ((src && src->range) || (dest && dest->range))
760         {
761                 fw3_ipt_rule_addarg(r, false, "-m", "iprange");
762         }
763
764         if (src && src->set)
765         {
766                 if (src->range)
767                 {
768                         fw3_ipt_rule_addarg(r, src->invert, "--src-range",
769                                             fw3_address_to_string(src, false, false));
770                 }
771 #ifndef DISABLE_IPV6
772                 else if (r->h->family == FW3_FAMILY_V6)
773                 {
774                         r->e6.ipv6.src = src->address.v6;
775                         r->e6.ipv6.smsk = src->mask.v6;
776
777                         int i;
778                         for (i = 0; i < 4; i++)
779                                 r->e6.ipv6.src.s6_addr32[i] &= r->e6.ipv6.smsk.s6_addr32[i];
780
781                         if (src->invert)
782                                 r->e6.ipv6.invflags |= IP6T_INV_SRCIP;
783                 }
784 #endif
785                 else
786                 {
787                         r->e.ip.src = src->address.v4;
788                         r->e.ip.smsk = src->mask.v4;
789
790                         r->e.ip.src.s_addr &= r->e.ip.smsk.s_addr;
791
792                         if (src->invert)
793                                 r->e.ip.invflags |= IPT_INV_SRCIP;
794                 }
795         }
796
797         if (dest && dest->set)
798         {
799                 if (dest->range)
800                 {
801                         fw3_ipt_rule_addarg(r, dest->invert, "--dst-range",
802                                             fw3_address_to_string(dest, false, false));
803                 }
804 #ifndef DISABLE_IPV6
805                 else if (r->h->family == FW3_FAMILY_V6)
806                 {
807                         r->e6.ipv6.dst = dest->address.v6;
808                         r->e6.ipv6.dmsk = dest->mask.v6;
809
810                         int i;
811                         for (i = 0; i < 4; i++)
812                                 r->e6.ipv6.dst.s6_addr32[i] &= r->e6.ipv6.dmsk.s6_addr32[i];
813
814                         if (dest->invert)
815                                 r->e6.ipv6.invflags |= IP6T_INV_DSTIP;
816                 }
817 #endif
818                 else
819                 {
820                         r->e.ip.dst = dest->address.v4;
821                         r->e.ip.dmsk = dest->mask.v4;
822
823                         r->e.ip.dst.s_addr &= r->e.ip.dmsk.s_addr;
824
825                         if (dest->invert)
826                                 r->e.ip.invflags |= IPT_INV_DSTIP;
827                 }
828         }
829 }
830
831 void
832 fw3_ipt_rule_sport_dport(struct fw3_ipt_rule *r,
833                          struct fw3_port *sp, struct fw3_port *dp)
834 {
835         char buf[sizeof("65535:65535\0")];
836
837         if ((!sp || !sp->set) && (!dp || !dp->set))
838                 return;
839
840         if (!get_protoname(r))
841                 return;
842
843         if (sp && sp->set)
844         {
845                 if (sp->port_min == sp->port_max)
846                         sprintf(buf, "%u", sp->port_min);
847                 else
848                         sprintf(buf, "%u:%u", sp->port_min, sp->port_max);
849
850                 fw3_ipt_rule_addarg(r, sp->invert, "--sport", buf);
851         }
852
853         if (dp && dp->set)
854         {
855                 if (dp->port_min == dp->port_max)
856                         sprintf(buf, "%u", dp->port_min);
857                 else
858                         sprintf(buf, "%u:%u", dp->port_min, dp->port_max);
859
860                 fw3_ipt_rule_addarg(r, dp->invert, "--dport", buf);
861         }
862 }
863
864 void
865 fw3_ipt_rule_device(struct fw3_ipt_rule *r, const char *device, bool out)
866 {
867         if (device) {
868                 struct fw3_device dev = { .any = false };
869                 strncpy(dev.name, device, sizeof(dev.name) - 1);
870                 fw3_ipt_rule_in_out(r, (out) ? NULL : &dev, (out) ? &dev : NULL);
871         }
872 }
873
874 void
875 fw3_ipt_rule_mac(struct fw3_ipt_rule *r, struct fw3_mac *mac)
876 {
877         char buf[sizeof("ff:ff:ff:ff:ff:ff\0")];
878         uint8_t *addr = mac->mac.ether_addr_octet;
879
880         if (!mac)
881                 return;
882
883         sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
884                 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
885
886         fw3_ipt_rule_addarg(r, false, "-m", "mac");
887         fw3_ipt_rule_addarg(r, mac->invert, "--mac-source", buf);
888 }
889
890 void
891 fw3_ipt_rule_icmptype(struct fw3_ipt_rule *r, struct fw3_icmptype *icmp)
892 {
893         char buf[sizeof("255/255\0")];
894
895         if (!icmp)
896                 return;
897
898 #ifndef DISABLE_IPV6
899         if (r->h->family == FW3_FAMILY_V6)
900         {
901                 if (icmp->code6_min == 0 && icmp->code6_max == 0xFF)
902                         sprintf(buf, "%u", icmp->type6);
903                 else
904                         sprintf(buf, "%u/%u", icmp->type6, icmp->code6_min);
905
906                 fw3_ipt_rule_addarg(r, icmp->invert, "--icmpv6-type", buf);
907         }
908         else
909 #endif
910         {
911                 if (icmp->code_min == 0 && icmp->code_max == 0xFF)
912                         sprintf(buf, "%u", icmp->type);
913                 else
914                         sprintf(buf, "%u/%u", icmp->type, icmp->code_min);
915
916                 fw3_ipt_rule_addarg(r, icmp->invert, "--icmp-type", buf);
917         }
918 }
919
920 void
921 fw3_ipt_rule_limit(struct fw3_ipt_rule *r, struct fw3_limit *limit)
922 {
923         char buf[sizeof("-4294967296/second\0")];
924
925         if (!limit || limit->rate <= 0)
926                 return;
927
928         fw3_ipt_rule_addarg(r, false, "-m", "limit");
929
930         sprintf(buf, "%u/%s", limit->rate, fw3_limit_units[limit->unit]);
931         fw3_ipt_rule_addarg(r, limit->invert, "--limit", buf);
932
933         if (limit->burst > 0)
934         {
935                 sprintf(buf, "%u", limit->burst);
936                 fw3_ipt_rule_addarg(r, limit->invert, "--limit-burst", buf);
937         }
938 }
939
940 void
941 fw3_ipt_rule_ipset(struct fw3_ipt_rule *r, struct fw3_setmatch *match)
942 {
943         char buf[sizeof("dst,dst,dst\0")];
944         char *p = buf;
945         int i = 0;
946
947         struct fw3_ipset *set;
948         struct fw3_ipset_datatype *type;
949
950         if (!match || !match->set || !match->ptr)
951                 return;
952
953         set = match->ptr;
954         list_for_each_entry(type, &set->datatypes, list)
955         {
956                 if (i >= 3)
957                         break;
958
959                 if (p > buf)
960                         *p++ = ',';
961
962                 p += sprintf(p, "%s", match->dir[i] ? match->dir[i] : type->dir);
963                 i++;
964         }
965
966         fw3_ipt_rule_addarg(r, false, "-m", "set");
967
968         fw3_ipt_rule_addarg(r, match->invert, "--match-set",
969                             set->external ? set->external : set->name);
970
971         fw3_ipt_rule_addarg(r, false, buf, NULL);
972 }
973
974 void
975 fw3_ipt_rule_time(struct fw3_ipt_rule *r, struct fw3_time *time)
976 {
977         int i;
978         struct tm empty = { 0 };
979
980         char buf[84]; /* sizeof("1,2,3,...,30,31\0") */
981         char *p;
982
983         bool d1 = memcmp(&time->datestart, &empty, sizeof(empty));
984         bool d2 = memcmp(&time->datestop, &empty, sizeof(empty));
985
986         if (!d1 && !d2 && !time->timestart && !time->timestop &&
987             !(time->monthdays & 0xFFFFFFFE) && !(time->weekdays & 0xFE))
988         {
989                 return;
990         }
991
992         fw3_ipt_rule_addarg(r, false, "-m", "time");
993
994         if (time->utc)
995                 fw3_ipt_rule_addarg(r, false, "--utc", NULL);
996
997         if (d1)
998         {
999                 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestart);
1000                 fw3_ipt_rule_addarg(r, false, "--datestart", buf);
1001         }
1002
1003         if (d2)
1004         {
1005                 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestop);
1006                 fw3_ipt_rule_addarg(r, false, "--datestop", buf);
1007         }
1008
1009         if (time->timestart)
1010         {
1011                 sprintf(buf, "%02d:%02d:%02d",
1012                         time->timestart / 3600,
1013                         time->timestart % 3600 / 60,
1014                         time->timestart % 60);
1015
1016                 fw3_ipt_rule_addarg(r, false, "--timestart", buf);
1017         }
1018
1019         if (time->timestop)
1020         {
1021                 sprintf(buf, "%02d:%02d:%02d",
1022                         time->timestop / 3600,
1023                         time->timestop % 3600 / 60,
1024                         time->timestop % 60);
1025
1026                 fw3_ipt_rule_addarg(r, false, "--timestop", buf);
1027         }
1028
1029         if (time->monthdays & 0xFFFFFFFE)
1030         {
1031                 for (i = 1, p = buf; i < 32; i++)
1032                 {
1033                         if (hasbit(time->monthdays, i))
1034                         {
1035                                 if (p > buf)
1036                                         *p++ = ',';
1037
1038                                 p += sprintf(p, "%u", i);
1039                         }
1040                 }
1041
1042                 fw3_ipt_rule_addarg(r, hasbit(time->monthdays, 0), "--monthdays", buf);
1043         }
1044
1045         if (time->weekdays & 0xFE)
1046         {
1047                 for (i = 1, p = buf; i < 8; i++)
1048                 {
1049                         if (hasbit(time->weekdays, i))
1050                         {
1051                                 if (p > buf)
1052                                         *p++ = ',';
1053
1054                                 p += sprintf(p, "%u", i);
1055                         }
1056                 }
1057
1058                 fw3_ipt_rule_addarg(r, hasbit(time->weekdays, 0), "--weekdays", buf);
1059         }
1060 }
1061
1062 void
1063 fw3_ipt_rule_mark(struct fw3_ipt_rule *r, struct fw3_mark *mark)
1064 {
1065         char buf[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
1066
1067         if (!mark || !mark->set)
1068                 return;
1069
1070         if (mark->mask < 0xFFFFFFFF)
1071                 sprintf(buf, "0x%x/0x%x", mark->mark, mark->mask);
1072         else
1073                 sprintf(buf, "0x%x", mark->mark);
1074
1075         fw3_ipt_rule_addarg(r, false, "-m", "mark");
1076         fw3_ipt_rule_addarg(r, mark->invert, "--mark", buf);
1077 }
1078
1079 void
1080 fw3_ipt_rule_comment(struct fw3_ipt_rule *r, const char *fmt, ...)
1081 {
1082         va_list ap;
1083         char buf[256];
1084
1085         if (!fmt || !*fmt)
1086                 return;
1087
1088         va_start(ap, fmt);
1089         vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1090         va_end(ap);
1091
1092         fw3_ipt_rule_addarg(r, false, "-m", "comment");
1093         fw3_ipt_rule_addarg(r, false, "--comment", buf);
1094 }
1095
1096 void
1097 fw3_ipt_rule_extra(struct fw3_ipt_rule *r, const char *extra)
1098 {
1099         char *p, **tmp, *s;
1100
1101         if (!extra || !*extra)
1102                 return;
1103
1104         s = fw3_strdup(extra);
1105
1106         for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
1107         {
1108                 tmp = realloc(r->argv, (r->argc + 1) * sizeof(*r->argv));
1109
1110                 if (!tmp)
1111                         break;
1112
1113                 r->argv = tmp;
1114                 r->argv[r->argc++] = fw3_strdup(p);
1115         }
1116
1117         free(s);
1118 }
1119
1120 #ifndef DISABLE_IPV6
1121 static void
1122 rule_print6(struct ip6t_entry *e)
1123 {
1124         char buf1[INET6_ADDRSTRLEN], buf2[INET6_ADDRSTRLEN];
1125         char *pname;
1126
1127         if (e->ipv6.flags & IP6T_F_PROTO)
1128         {
1129                 if (e->ipv6.invflags & XT_INV_PROTO)
1130                         printf(" !");
1131
1132                 pname = get_protoname(container_of(e, struct fw3_ipt_rule, e6));
1133
1134                 if (pname)
1135                         printf(" -p %s", pname);
1136                 else
1137                         printf(" -p %u", e->ipv6.proto);
1138         }
1139
1140         if (e->ipv6.iniface[0])
1141         {
1142                 if (e->ipv6.invflags & IP6T_INV_VIA_IN)
1143                         printf(" !");
1144
1145                 printf(" -i %s", e->ipv6.iniface);
1146         }
1147
1148         if (e->ipv6.outiface[0])
1149         {
1150                 if (e->ipv6.invflags & IP6T_INV_VIA_OUT)
1151                         printf(" !");
1152
1153                 printf(" -o %s", e->ipv6.outiface);
1154         }
1155
1156         if (memcmp(&e->ipv6.src, &in6addr_any, sizeof(struct in6_addr)))
1157         {
1158                 if (e->ipv6.invflags & IP6T_INV_SRCIP)
1159                         printf(" !");
1160
1161                 printf(" -s %s/%s",
1162                        inet_ntop(AF_INET6, &e->ipv6.src, buf1, sizeof(buf1)),
1163                        inet_ntop(AF_INET6, &e->ipv6.smsk, buf2, sizeof(buf2)));
1164         }
1165
1166         if (memcmp(&e->ipv6.dst, &in6addr_any, sizeof(struct in6_addr)))
1167         {
1168                 if (e->ipv6.invflags & IP6T_INV_DSTIP)
1169                         printf(" !");
1170
1171                 printf(" -d %s/%s",
1172                        inet_ntop(AF_INET6, &e->ipv6.dst, buf1, sizeof(buf1)),
1173                        inet_ntop(AF_INET6, &e->ipv6.dmsk, buf2, sizeof(buf2)));
1174         }
1175 }
1176 #endif
1177
1178 static void
1179 rule_print4(struct ipt_entry *e)
1180 {
1181         struct in_addr in_zero = { 0 };
1182         char buf1[sizeof("255.255.255.255\0")], buf2[sizeof("255.255.255.255\0")];
1183         char *pname;
1184
1185         if (e->ip.proto)
1186         {
1187                 if (e->ip.invflags & XT_INV_PROTO)
1188                         printf(" !");
1189
1190                 pname = get_protoname(container_of(e, struct fw3_ipt_rule, e));
1191
1192                 if (pname)
1193                         printf(" -p %s", pname);
1194                 else
1195                         printf(" -p %u", e->ip.proto);
1196         }
1197
1198         if (e->ip.iniface[0])
1199         {
1200                 if (e->ip.invflags & IPT_INV_VIA_IN)
1201                         printf(" !");
1202
1203                 printf(" -i %s", e->ip.iniface);
1204         }
1205
1206         if (e->ip.outiface[0])
1207         {
1208                 if (e->ip.invflags & IPT_INV_VIA_OUT)
1209                         printf(" !");
1210
1211                 printf(" -o %s", e->ip.outiface);
1212         }
1213
1214         if (memcmp(&e->ip.src, &in_zero, sizeof(struct in_addr)))
1215         {
1216                 if (e->ip.invflags & IPT_INV_SRCIP)
1217                         printf(" !");
1218
1219                 printf(" -s %s/%s",
1220                        inet_ntop(AF_INET, &e->ip.src, buf1, sizeof(buf1)),
1221                        inet_ntop(AF_INET, &e->ip.smsk, buf2, sizeof(buf2)));
1222         }
1223
1224         if (memcmp(&e->ip.dst, &in_zero, sizeof(struct in_addr)))
1225         {
1226                 if (e->ip.invflags & IPT_INV_DSTIP)
1227                         printf(" !");
1228
1229                 printf(" -d %s/%s",
1230                        inet_ntop(AF_INET, &e->ip.dst, buf1, sizeof(buf1)),
1231                        inet_ntop(AF_INET, &e->ip.dmsk, buf2, sizeof(buf2)));
1232         }
1233 }
1234
1235 static void
1236 rule_print(struct fw3_ipt_rule *r, const char *prefix, const char *chain)
1237 {
1238         debug(r->h, "%s %s", prefix, chain);
1239
1240 #ifndef DISABLE_IPV6
1241         if (r->h->family == FW3_FAMILY_V6)
1242                 rule_print6(&r->e6);
1243         else
1244 #endif
1245                 rule_print4(&r->e);
1246
1247         fw3_xt_print_matches(&r->e.ip, r->matches);
1248         fw3_xt_print_target(&r->e.ip, r->target);
1249
1250         printf("\n");
1251 }
1252
1253 static bool
1254 parse_option(struct fw3_ipt_rule *r, int optc, bool inv)
1255 {
1256         struct xtables_rule_match *m;
1257         struct xtables_match *em;
1258
1259         /* is a target option */
1260         if (r->target && fw3_xt_has_target_parse(r->target) &&
1261                 optc >= r->target->option_offset &&
1262                 optc < (r->target->option_offset + 256))
1263         {
1264                 xtables_option_tpcall(optc, r->argv, inv, r->target, &r->e);
1265                 return false;
1266         }
1267
1268         /* try to dispatch argument to one of the match parsers */
1269         for (m = r->matches; m; m = m->next)
1270         {
1271                 em = m->match;
1272
1273                 if (m->completed || !fw3_xt_has_match_parse(em))
1274                         continue;
1275
1276                 if (optc < em->option_offset ||
1277                         optc >= (em->option_offset + 256))
1278                         continue;
1279
1280                 xtables_option_mpcall(optc, r->argv, inv, em, &r->e);
1281                 return false;
1282         }
1283
1284         /* unhandled option, might belong to a protocol match */
1285         if ((em = load_protomatch(r)) != NULL)
1286         {
1287                 init_match(r, em, false);
1288
1289                 r->protocol_loaded = true;
1290                 optind--;
1291
1292                 return true;
1293         }
1294
1295         if (optc == ':')
1296                 warn("parse_option(): option '%s' needs argument", r->argv[optind-1]);
1297
1298         if (optc == '?')
1299                 warn("parse_option(): unknown option '%s'", r->argv[optind-1]);
1300
1301         return false;
1302 }
1303
1304 void
1305 fw3_ipt_rule_addarg(struct fw3_ipt_rule *r, bool inv,
1306                     const char *k, const char *v)
1307 {
1308         int n;
1309         char **tmp;
1310
1311         if (!k)
1312                 return;
1313
1314         n = inv + !!k + !!v;
1315         tmp = realloc(r->argv, (r->argc + n) * sizeof(*tmp));
1316
1317         if (!tmp)
1318                 return;
1319
1320         r->argv = tmp;
1321
1322         if (inv)
1323                 r->argv[r->argc++] = fw3_strdup("!");
1324
1325         r->argv[r->argc++] = fw3_strdup(k);
1326
1327         if (v)
1328                 r->argv[r->argc++] = fw3_strdup(v);
1329 }
1330
1331 static unsigned char *
1332 rule_mask(struct fw3_ipt_rule *r)
1333 {
1334         size_t s;
1335         unsigned char *p, *mask = NULL;
1336         struct xtables_rule_match *m;
1337
1338 #define SZ(x) XT_ALIGN(sizeof(struct x))
1339
1340 #ifndef DISABLE_IPV6
1341         if (r->h->family == FW3_FAMILY_V6)
1342         {
1343                 s = SZ(ip6t_entry);
1344
1345                 for (m = r->matches; m; m = m->next)
1346                         s += SZ(ip6t_entry_match) + m->match->size;
1347
1348                 s += SZ(ip6t_entry_target);
1349                 if (r->target)
1350                         s += r->target->size;
1351
1352                 mask = fw3_alloc(s);
1353                 memset(mask, 0xFF, SZ(ip6t_entry));
1354                 p = mask + SZ(ip6t_entry);
1355
1356                 for (m = r->matches; m; m = m->next)
1357                 {
1358                         memset(p, 0xFF, SZ(ip6t_entry_match) + m->match->userspacesize);
1359                         p += SZ(ip6t_entry_match) + m->match->size;
1360                 }
1361
1362                 memset(p, 0xFF, SZ(ip6t_entry_target) + (r->target) ? r->target->userspacesize : 0);
1363         }
1364         else
1365 #endif
1366         {
1367                 s = SZ(ipt_entry);
1368
1369                 for (m = r->matches; m; m = m->next)
1370                         s += SZ(ipt_entry_match) + m->match->size;
1371
1372                 s += SZ(ipt_entry_target);
1373                 if (r->target)
1374                         s += r->target->size;
1375
1376                 mask = fw3_alloc(s);
1377                 memset(mask, 0xFF, SZ(ipt_entry));
1378                 p = mask + SZ(ipt_entry);
1379
1380                 for (m = r->matches; m; m = m->next)
1381                 {
1382                         memset(p, 0xFF, SZ(ipt_entry_match) + m->match->userspacesize);
1383                         p += SZ(ipt_entry_match) + m->match->size;
1384                 }
1385
1386                 memset(p, 0xFF, SZ(ipt_entry_target) + (r->target) ? r->target->userspacesize : 0);
1387         }
1388
1389         return mask;
1390 }
1391
1392 static void *
1393 rule_build(struct fw3_ipt_rule *r)
1394 {
1395         size_t s, target_size = (r->target) ? r->target->t->u.target_size : 0;
1396         struct xtables_rule_match *m;
1397
1398 #ifndef DISABLE_IPV6
1399         if (r->h->family == FW3_FAMILY_V6)
1400         {
1401                 struct ip6t_entry *e6;
1402
1403                 s = XT_ALIGN(sizeof(struct ip6t_entry));
1404
1405                 for (m = r->matches; m; m = m->next)
1406                         s += m->match->m->u.match_size;
1407
1408                 e6 = fw3_alloc(s + target_size);
1409
1410                 memcpy(e6, &r->e6, sizeof(struct ip6t_entry));
1411
1412                 e6->target_offset = s;
1413                 e6->next_offset = s + target_size;
1414
1415                 s = 0;
1416
1417                 for (m = r->matches; m; m = m->next)
1418                 {
1419                         memcpy(e6->elems + s, m->match->m, m->match->m->u.match_size);
1420                         s += m->match->m->u.match_size;
1421                 }
1422
1423                 if (target_size)
1424                         memcpy(e6->elems + s, r->target->t, target_size);
1425
1426                 return e6;
1427         }
1428         else
1429 #endif
1430         {
1431                 struct ipt_entry *e;
1432
1433                 s = XT_ALIGN(sizeof(struct ipt_entry));
1434
1435                 for (m = r->matches; m; m = m->next)
1436                         s += m->match->m->u.match_size;
1437
1438                 e = fw3_alloc(s + target_size);
1439
1440                 memcpy(e, &r->e, sizeof(struct ipt_entry));
1441
1442                 e->target_offset = s;
1443                 e->next_offset = s + target_size;
1444
1445                 s = 0;
1446
1447                 for (m = r->matches; m; m = m->next)
1448                 {
1449                         memcpy(e->elems + s, m->match->m, m->match->m->u.match_size);
1450                         s += m->match->m->u.match_size;
1451                 }
1452
1453                 if (target_size)
1454                         memcpy(e->elems + s, r->target->t, target_size);
1455
1456                 return e;
1457         }
1458 }
1459
1460 void
1461 __fw3_ipt_rule_append(struct fw3_ipt_rule *r, bool repl, const char *fmt, ...)
1462 {
1463         void *rule;
1464         unsigned char *mask;
1465
1466         struct xtables_rule_match *m;
1467         struct xtables_match *em;
1468         struct xtables_target *et;
1469         struct xtables_globals *g;
1470
1471         int i, optc;
1472         uint32_t id;
1473         bool inv = false;
1474         char buf[32];
1475         va_list ap;
1476
1477         va_start(ap, fmt);
1478         vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1479         va_end(ap);
1480
1481         g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
1482         g->opts = g->orig_opts;
1483
1484         optind = 0;
1485         opterr = 0;
1486
1487         if (r->id >= 0)
1488         {
1489                 em = find_match(r, "id");
1490
1491                 if (!em)
1492                 {
1493                         warn("fw3_ipt_rule_append(): Can't find match '%s'", "id");
1494                         goto free;
1495                 }
1496
1497                 init_match(r, em, true);
1498
1499                 id = FW3_ID_MAGIC | (r->id & ~FW3_ID_MASK);
1500                 memcpy(em->m->data, &id, sizeof(id));
1501
1502                 em->mflags = 1;
1503         }
1504
1505         while ((optc = getopt_long(r->argc, r->argv, "-:m:j:", g->opts,
1506                                    NULL)) != -1)
1507         {
1508                 switch (optc)
1509                 {
1510                 case 'm':
1511                         em = find_match(r, optarg);
1512
1513                         if (!em)
1514                         {
1515                                 warn("fw3_ipt_rule_append(): Can't find match '%s'", optarg);
1516                                 goto free;
1517                         }
1518
1519                         init_match(r, em, true);
1520                         break;
1521
1522                 case 'j':
1523                         et = get_target(r, optarg);
1524
1525                         if (!et)
1526                         {
1527                                 warn("fw3_ipt_rule_append(): Can't find target '%s'", optarg);
1528                                 goto free;
1529                         }
1530
1531                         break;
1532
1533                 case 1:
1534                         if ((optarg[0] == '!') && (optarg[1] == '\0'))
1535                         {
1536                                 optarg[0] = '\0';
1537                                 inv = true;
1538                                 continue;
1539                         }
1540
1541                         warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg);
1542                         goto free;
1543
1544                 default:
1545                         if (parse_option(r, optc, inv))
1546                                 continue;
1547                         break;
1548                 }
1549
1550                 inv = false;
1551         }
1552
1553         for (m = r->matches; m; m = m->next)
1554                 xtables_option_mfcall(m->match);
1555
1556         if (r->target)
1557                 xtables_option_tfcall(r->target);
1558
1559         rule = rule_build(r);
1560
1561 #ifndef DISABLE_IPV6
1562         if (r->h->family == FW3_FAMILY_V6)
1563         {
1564                 if (repl)
1565                 {
1566                         mask = rule_mask(r);
1567
1568                         while (ip6tc_delete_entry(buf, rule, mask, r->h->handle))
1569                                 if (fw3_pr_debug)
1570                                         rule_print(r, "-D", buf);
1571
1572                         free(mask);
1573                 }
1574
1575                 if (fw3_pr_debug)
1576                         rule_print(r, "-A", buf);
1577
1578                 if (!ip6tc_append_entry(buf, rule, r->h->handle))
1579                         warn("ip6tc_append_entry(): %s", ip6tc_strerror(errno));
1580         }
1581         else
1582 #endif
1583         {
1584                 if (repl)
1585                 {
1586                         mask = rule_mask(r);
1587
1588                         while (iptc_delete_entry(buf, rule, mask, r->h->handle))
1589                                 if (fw3_pr_debug)
1590                                         rule_print(r, "-D", buf);
1591
1592                         free(mask);
1593                 }
1594
1595                 if (fw3_pr_debug)
1596                         rule_print(r, "-A", buf);
1597
1598                 if (!iptc_append_entry(buf, rule, r->h->handle))
1599                         warn("iptc_append_entry(): %s\n", iptc_strerror(errno));
1600         }
1601
1602         free(rule);
1603
1604 free:
1605         for (i = 1; i < r->argc; i++)
1606                 free(r->argv[i]);
1607
1608         free(r->argv);
1609
1610         xtables_rule_matches_free(&r->matches);
1611
1612         if (r->target)
1613                 free(r->target->t);
1614
1615         free(r);
1616
1617         /* reset all targets and matches */
1618         for (em = xtables_matches; em; em = em->next)
1619                 em->mflags = 0;
1620
1621         for (et = xtables_targets; et; et = et->next)
1622         {
1623                 et->tflags = 0;
1624                 et->used = 0;
1625         }
1626
1627         xtables_free_opts(1);
1628 }
1629
1630 struct fw3_ipt_rule *
1631 fw3_ipt_rule_create(struct fw3_ipt_handle *handle, struct fw3_protocol *proto,
1632                     struct fw3_device *in, struct fw3_device *out,
1633                     struct fw3_address *src, struct fw3_address *dest)
1634 {
1635         struct fw3_ipt_rule *r;
1636
1637         r = fw3_ipt_rule_new(handle);
1638
1639         fw3_ipt_rule_proto(r, proto);
1640         fw3_ipt_rule_in_out(r, in, out);
1641         fw3_ipt_rule_src_dest(r, src, dest);
1642
1643         return r;
1644 }