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