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