iptables: fix loading standard target
[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                 t = xtables_find_target(XT_STANDARD_TARGET, XTF_DONT_LOAD);
635
636                 if (t)
637                         return t;
638
639                 load_extension(r->h, "standard");
640                 return xtables_find_target(XT_STANDARD_TARGET, XTF_LOAD_MUST_SUCCEED);
641         }
642
643         t = xtables_find_target(name, XTF_DONT_LOAD);
644
645         if (!t && load_extension(r->h, name))
646                 t = xtables_find_target(name, XTF_DONT_LOAD);
647
648         return t;
649 }
650
651 static struct xtables_target *
652 get_target(struct fw3_ipt_rule *r, const char *name)
653 {
654         size_t s;
655         struct xtables_target *t;
656         struct xtables_globals *g;
657
658         t = find_target(r, name);
659
660         if (!t)
661                 return NULL;
662
663         s = XT_ALIGN(sizeof(struct xt_entry_target)) + t->size;
664         t->t = fw3_alloc(s);
665
666         fw3_xt_set_target_name(t, name);
667
668         t->t->u.user.revision = t->revision;
669         t->t->u.target_size = s;
670
671         /* free previous userspace data */
672         fw3_xt_free_target_udata(t);
673
674         if (t->init)
675                 t->init(t->t);
676
677         /* merge option table */
678         g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
679         fw3_xt_merge_target_options(g, t);
680
681         r->target = t;
682
683         return t;
684 }
685
686 void
687 fw3_ipt_rule_proto(struct fw3_ipt_rule *r, struct fw3_protocol *proto)
688 {
689         uint32_t pr;
690
691         if (!proto || proto->any)
692                 return;
693
694         pr = proto->protocol;
695
696 #ifndef DISABLE_IPV6
697         if (r->h->family == FW3_FAMILY_V6)
698         {
699                 if (pr == 1)
700                         pr = 58;
701
702                 r->e6.ipv6.proto = pr;
703                 r->e6.ipv6.flags |= IP6T_F_PROTO;
704
705                 if (proto->invert)
706                         r->e6.ipv6.invflags |= XT_INV_PROTO;
707         }
708         else
709 #endif
710         {
711                 r->e.ip.proto = pr;
712
713                 if (proto->invert)
714                         r->e.ip.invflags |= XT_INV_PROTO;
715         }
716
717         r->protocol = pr;
718 }
719
720 void
721 fw3_ipt_rule_in_out(struct fw3_ipt_rule *r,
722                     struct fw3_device *in, struct fw3_device *out)
723 {
724 #ifndef DISABLE_IPV6
725         if (r->h->family == FW3_FAMILY_V6)
726         {
727                 if (in && !in->any)
728                 {
729                         xtables_parse_interface(in->name, r->e6.ipv6.iniface,
730                                                           r->e6.ipv6.iniface_mask);
731
732                         if (in->invert)
733                                 r->e6.ipv6.invflags |= IP6T_INV_VIA_IN;
734                 }
735
736                 if (out && !out->any)
737                 {
738                         xtables_parse_interface(out->name, r->e6.ipv6.outiface,
739                                                            r->e6.ipv6.outiface_mask);
740
741                         if (out->invert)
742                                 r->e6.ipv6.invflags |= IP6T_INV_VIA_OUT;
743                 }
744         }
745         else
746 #endif
747         {
748                 if (in && !in->any)
749                 {
750                         xtables_parse_interface(in->name, r->e.ip.iniface,
751                                                           r->e.ip.iniface_mask);
752
753                         if (in->invert)
754                                 r->e.ip.invflags |= IPT_INV_VIA_IN;
755                 }
756
757                 if (out && !out->any)
758                 {
759                         xtables_parse_interface(out->name, r->e.ip.outiface,
760                                                            r->e.ip.outiface_mask);
761
762                         if (out->invert)
763                                 r->e.ip.invflags |= IPT_INV_VIA_OUT;
764                 }
765         }
766 }
767
768
769 void
770 fw3_ipt_rule_src_dest(struct fw3_ipt_rule *r,
771                       struct fw3_address *src, struct fw3_address *dest)
772 {
773         if ((src && src->range) || (dest && dest->range))
774         {
775                 fw3_ipt_rule_addarg(r, false, "-m", "iprange");
776         }
777
778         if (src && src->set)
779         {
780                 if (src->range)
781                 {
782                         fw3_ipt_rule_addarg(r, src->invert, "--src-range",
783                                             fw3_address_to_string(src, false, false));
784                 }
785 #ifndef DISABLE_IPV6
786                 else if (r->h->family == FW3_FAMILY_V6)
787                 {
788                         r->e6.ipv6.src = src->address.v6;
789                         r->e6.ipv6.smsk = src->mask.v6;
790
791                         int i;
792                         for (i = 0; i < 4; i++)
793                                 r->e6.ipv6.src.s6_addr32[i] &= r->e6.ipv6.smsk.s6_addr32[i];
794
795                         if (src->invert)
796                                 r->e6.ipv6.invflags |= IP6T_INV_SRCIP;
797                 }
798 #endif
799                 else
800                 {
801                         r->e.ip.src = src->address.v4;
802                         r->e.ip.smsk = src->mask.v4;
803
804                         r->e.ip.src.s_addr &= r->e.ip.smsk.s_addr;
805
806                         if (src->invert)
807                                 r->e.ip.invflags |= IPT_INV_SRCIP;
808                 }
809         }
810
811         if (dest && dest->set)
812         {
813                 if (dest->range)
814                 {
815                         fw3_ipt_rule_addarg(r, dest->invert, "--dst-range",
816                                             fw3_address_to_string(dest, false, false));
817                 }
818 #ifndef DISABLE_IPV6
819                 else if (r->h->family == FW3_FAMILY_V6)
820                 {
821                         r->e6.ipv6.dst = dest->address.v6;
822                         r->e6.ipv6.dmsk = dest->mask.v6;
823
824                         int i;
825                         for (i = 0; i < 4; i++)
826                                 r->e6.ipv6.dst.s6_addr32[i] &= r->e6.ipv6.dmsk.s6_addr32[i];
827
828                         if (dest->invert)
829                                 r->e6.ipv6.invflags |= IP6T_INV_DSTIP;
830                 }
831 #endif
832                 else
833                 {
834                         r->e.ip.dst = dest->address.v4;
835                         r->e.ip.dmsk = dest->mask.v4;
836
837                         r->e.ip.dst.s_addr &= r->e.ip.dmsk.s_addr;
838
839                         if (dest->invert)
840                                 r->e.ip.invflags |= IPT_INV_DSTIP;
841                 }
842         }
843 }
844
845 void
846 fw3_ipt_rule_sport_dport(struct fw3_ipt_rule *r,
847                          struct fw3_port *sp, struct fw3_port *dp)
848 {
849         char buf[sizeof("65535:65535\0")];
850
851         if ((!sp || !sp->set) && (!dp || !dp->set))
852                 return;
853
854         if (!get_protoname(r))
855                 return;
856
857         if (sp && sp->set)
858         {
859                 if (sp->port_min == sp->port_max)
860                         sprintf(buf, "%u", sp->port_min);
861                 else
862                         sprintf(buf, "%u:%u", sp->port_min, sp->port_max);
863
864                 fw3_ipt_rule_addarg(r, sp->invert, "--sport", buf);
865         }
866
867         if (dp && dp->set)
868         {
869                 if (dp->port_min == dp->port_max)
870                         sprintf(buf, "%u", dp->port_min);
871                 else
872                         sprintf(buf, "%u:%u", dp->port_min, dp->port_max);
873
874                 fw3_ipt_rule_addarg(r, dp->invert, "--dport", buf);
875         }
876 }
877
878 void
879 fw3_ipt_rule_device(struct fw3_ipt_rule *r, const char *device, bool out)
880 {
881         if (device) {
882                 struct fw3_device dev = { .any = false };
883                 strncpy(dev.name, device, sizeof(dev.name) - 1);
884                 fw3_ipt_rule_in_out(r, (out) ? NULL : &dev, (out) ? &dev : NULL);
885         }
886 }
887
888 void
889 fw3_ipt_rule_mac(struct fw3_ipt_rule *r, struct fw3_mac *mac)
890 {
891         char buf[sizeof("ff:ff:ff:ff:ff:ff\0")];
892         uint8_t *addr = mac->mac.ether_addr_octet;
893
894         if (!mac)
895                 return;
896
897         sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
898                 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
899
900         fw3_ipt_rule_addarg(r, false, "-m", "mac");
901         fw3_ipt_rule_addarg(r, mac->invert, "--mac-source", buf);
902 }
903
904 void
905 fw3_ipt_rule_icmptype(struct fw3_ipt_rule *r, struct fw3_icmptype *icmp)
906 {
907         char buf[sizeof("255/255\0")];
908
909         if (!icmp)
910                 return;
911
912 #ifndef DISABLE_IPV6
913         if (r->h->family == FW3_FAMILY_V6)
914         {
915                 if (icmp->code6_min == 0 && icmp->code6_max == 0xFF)
916                         sprintf(buf, "%u", icmp->type6);
917                 else
918                         sprintf(buf, "%u/%u", icmp->type6, icmp->code6_min);
919
920                 fw3_ipt_rule_addarg(r, icmp->invert, "--icmpv6-type", buf);
921         }
922         else
923 #endif
924         {
925                 if (icmp->code_min == 0 && icmp->code_max == 0xFF)
926                         sprintf(buf, "%u", icmp->type);
927                 else
928                         sprintf(buf, "%u/%u", icmp->type, icmp->code_min);
929
930                 fw3_ipt_rule_addarg(r, icmp->invert, "--icmp-type", buf);
931         }
932 }
933
934 void
935 fw3_ipt_rule_limit(struct fw3_ipt_rule *r, struct fw3_limit *limit)
936 {
937         char buf[sizeof("-4294967296/second\0")];
938
939         if (!limit || limit->rate <= 0)
940                 return;
941
942         fw3_ipt_rule_addarg(r, false, "-m", "limit");
943
944         sprintf(buf, "%u/%s", limit->rate, fw3_limit_units[limit->unit]);
945         fw3_ipt_rule_addarg(r, limit->invert, "--limit", buf);
946
947         if (limit->burst > 0)
948         {
949                 sprintf(buf, "%u", limit->burst);
950                 fw3_ipt_rule_addarg(r, limit->invert, "--limit-burst", buf);
951         }
952 }
953
954 void
955 fw3_ipt_rule_ipset(struct fw3_ipt_rule *r, struct fw3_setmatch *match)
956 {
957         char buf[sizeof("dst,dst,dst\0")];
958         char *p = buf;
959         int i = 0;
960
961         struct fw3_ipset *set;
962         struct fw3_ipset_datatype *type;
963
964         if (!match || !match->set || !match->ptr)
965                 return;
966
967         set = match->ptr;
968         list_for_each_entry(type, &set->datatypes, list)
969         {
970                 if (i >= 3)
971                         break;
972
973                 if (p > buf)
974                         *p++ = ',';
975
976                 p += sprintf(p, "%s", match->dir[i] ? match->dir[i] : type->dir);
977                 i++;
978         }
979
980         fw3_ipt_rule_addarg(r, false, "-m", "set");
981
982         fw3_ipt_rule_addarg(r, match->invert, "--match-set",
983                             set->external ? set->external : set->name);
984
985         fw3_ipt_rule_addarg(r, false, buf, NULL);
986 }
987
988 void
989 fw3_ipt_rule_time(struct fw3_ipt_rule *r, struct fw3_time *time)
990 {
991         int i;
992         struct tm empty = { 0 };
993
994         char buf[84]; /* sizeof("1,2,3,...,30,31\0") */
995         char *p;
996
997         bool d1 = memcmp(&time->datestart, &empty, sizeof(empty));
998         bool d2 = memcmp(&time->datestop, &empty, sizeof(empty));
999
1000         if (!d1 && !d2 && !time->timestart && !time->timestop &&
1001             !(time->monthdays & 0xFFFFFFFE) && !(time->weekdays & 0xFE))
1002         {
1003                 return;
1004         }
1005
1006         fw3_ipt_rule_addarg(r, false, "-m", "time");
1007
1008         if (time->utc)
1009                 fw3_ipt_rule_addarg(r, false, "--utc", NULL);
1010
1011         if (d1)
1012         {
1013                 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestart);
1014                 fw3_ipt_rule_addarg(r, false, "--datestart", buf);
1015         }
1016
1017         if (d2)
1018         {
1019                 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &time->datestop);
1020                 fw3_ipt_rule_addarg(r, false, "--datestop", buf);
1021         }
1022
1023         if (time->timestart)
1024         {
1025                 sprintf(buf, "%02d:%02d:%02d",
1026                         time->timestart / 3600,
1027                         time->timestart % 3600 / 60,
1028                         time->timestart % 60);
1029
1030                 fw3_ipt_rule_addarg(r, false, "--timestart", buf);
1031         }
1032
1033         if (time->timestop)
1034         {
1035                 sprintf(buf, "%02d:%02d:%02d",
1036                         time->timestop / 3600,
1037                         time->timestop % 3600 / 60,
1038                         time->timestop % 60);
1039
1040                 fw3_ipt_rule_addarg(r, false, "--timestop", buf);
1041         }
1042
1043         if (time->monthdays & 0xFFFFFFFE)
1044         {
1045                 for (i = 1, p = buf; i < 32; i++)
1046                 {
1047                         if (fw3_hasbit(time->monthdays, i))
1048                         {
1049                                 if (p > buf)
1050                                         *p++ = ',';
1051
1052                                 p += sprintf(p, "%u", i);
1053                         }
1054                 }
1055
1056                 fw3_ipt_rule_addarg(r, fw3_hasbit(time->monthdays, 0), "--monthdays", buf);
1057         }
1058
1059         if (time->weekdays & 0xFE)
1060         {
1061                 for (i = 1, p = buf; i < 8; i++)
1062                 {
1063                         if (fw3_hasbit(time->weekdays, i))
1064                         {
1065                                 if (p > buf)
1066                                         *p++ = ',';
1067
1068                                 p += sprintf(p, "%u", i);
1069                         }
1070                 }
1071
1072                 fw3_ipt_rule_addarg(r, fw3_hasbit(time->weekdays, 0), "--weekdays", buf);
1073         }
1074 }
1075
1076 void
1077 fw3_ipt_rule_mark(struct fw3_ipt_rule *r, struct fw3_mark *mark)
1078 {
1079         char buf[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
1080
1081         if (!mark || !mark->set)
1082                 return;
1083
1084         if (mark->mask < 0xFFFFFFFF)
1085                 sprintf(buf, "0x%x/0x%x", mark->mark, mark->mask);
1086         else
1087                 sprintf(buf, "0x%x", mark->mark);
1088
1089         fw3_ipt_rule_addarg(r, false, "-m", "mark");
1090         fw3_ipt_rule_addarg(r, mark->invert, "--mark", buf);
1091 }
1092
1093 void
1094 fw3_ipt_rule_comment(struct fw3_ipt_rule *r, const char *fmt, ...)
1095 {
1096         va_list ap;
1097         char buf[256];
1098
1099         if (!fmt || !*fmt)
1100                 return;
1101
1102         va_start(ap, fmt);
1103         vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1104         va_end(ap);
1105
1106         fw3_ipt_rule_addarg(r, false, "-m", "comment");
1107         fw3_ipt_rule_addarg(r, false, "--comment", buf);
1108 }
1109
1110 void
1111 fw3_ipt_rule_extra(struct fw3_ipt_rule *r, const char *extra)
1112 {
1113         char *p, **tmp, *s;
1114
1115         if (!extra || !*extra)
1116                 return;
1117
1118         s = fw3_strdup(extra);
1119
1120         for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
1121         {
1122                 tmp = realloc(r->argv, (r->argc + 1) * sizeof(*r->argv));
1123
1124                 if (!tmp)
1125                         break;
1126
1127                 r->argv = tmp;
1128                 r->argv[r->argc++] = fw3_strdup(p);
1129         }
1130
1131         free(s);
1132 }
1133
1134 #ifndef DISABLE_IPV6
1135 static void
1136 rule_print6(struct ip6t_entry *e)
1137 {
1138         char buf1[INET6_ADDRSTRLEN], buf2[INET6_ADDRSTRLEN];
1139         char *pname;
1140
1141         if (e->ipv6.flags & IP6T_F_PROTO)
1142         {
1143                 if (e->ipv6.invflags & XT_INV_PROTO)
1144                         printf(" !");
1145
1146                 pname = get_protoname(container_of(e, struct fw3_ipt_rule, e6));
1147
1148                 if (pname)
1149                         printf(" -p %s", pname);
1150                 else
1151                         printf(" -p %u", e->ipv6.proto);
1152         }
1153
1154         if (e->ipv6.iniface[0])
1155         {
1156                 if (e->ipv6.invflags & IP6T_INV_VIA_IN)
1157                         printf(" !");
1158
1159                 printf(" -i %s", e->ipv6.iniface);
1160         }
1161
1162         if (e->ipv6.outiface[0])
1163         {
1164                 if (e->ipv6.invflags & IP6T_INV_VIA_OUT)
1165                         printf(" !");
1166
1167                 printf(" -o %s", e->ipv6.outiface);
1168         }
1169
1170         if (memcmp(&e->ipv6.src, &in6addr_any, sizeof(struct in6_addr)))
1171         {
1172                 if (e->ipv6.invflags & IP6T_INV_SRCIP)
1173                         printf(" !");
1174
1175                 printf(" -s %s/%s",
1176                        inet_ntop(AF_INET6, &e->ipv6.src, buf1, sizeof(buf1)),
1177                        inet_ntop(AF_INET6, &e->ipv6.smsk, buf2, sizeof(buf2)));
1178         }
1179
1180         if (memcmp(&e->ipv6.dst, &in6addr_any, sizeof(struct in6_addr)))
1181         {
1182                 if (e->ipv6.invflags & IP6T_INV_DSTIP)
1183                         printf(" !");
1184
1185                 printf(" -d %s/%s",
1186                        inet_ntop(AF_INET6, &e->ipv6.dst, buf1, sizeof(buf1)),
1187                        inet_ntop(AF_INET6, &e->ipv6.dmsk, buf2, sizeof(buf2)));
1188         }
1189 }
1190 #endif
1191
1192 static void
1193 rule_print4(struct ipt_entry *e)
1194 {
1195         struct in_addr in_zero = { 0 };
1196         char buf1[sizeof("255.255.255.255\0")], buf2[sizeof("255.255.255.255\0")];
1197         char *pname;
1198
1199         if (e->ip.proto)
1200         {
1201                 if (e->ip.invflags & XT_INV_PROTO)
1202                         printf(" !");
1203
1204                 pname = get_protoname(container_of(e, struct fw3_ipt_rule, e));
1205
1206                 if (pname)
1207                         printf(" -p %s", pname);
1208                 else
1209                         printf(" -p %u", e->ip.proto);
1210         }
1211
1212         if (e->ip.iniface[0])
1213         {
1214                 if (e->ip.invflags & IPT_INV_VIA_IN)
1215                         printf(" !");
1216
1217                 printf(" -i %s", e->ip.iniface);
1218         }
1219
1220         if (e->ip.outiface[0])
1221         {
1222                 if (e->ip.invflags & IPT_INV_VIA_OUT)
1223                         printf(" !");
1224
1225                 printf(" -o %s", e->ip.outiface);
1226         }
1227
1228         if (memcmp(&e->ip.src, &in_zero, sizeof(struct in_addr)))
1229         {
1230                 if (e->ip.invflags & IPT_INV_SRCIP)
1231                         printf(" !");
1232
1233                 printf(" -s %s/%s",
1234                        inet_ntop(AF_INET, &e->ip.src, buf1, sizeof(buf1)),
1235                        inet_ntop(AF_INET, &e->ip.smsk, buf2, sizeof(buf2)));
1236         }
1237
1238         if (memcmp(&e->ip.dst, &in_zero, sizeof(struct in_addr)))
1239         {
1240                 if (e->ip.invflags & IPT_INV_DSTIP)
1241                         printf(" !");
1242
1243                 printf(" -d %s/%s",
1244                        inet_ntop(AF_INET, &e->ip.dst, buf1, sizeof(buf1)),
1245                        inet_ntop(AF_INET, &e->ip.dmsk, buf2, sizeof(buf2)));
1246         }
1247 }
1248
1249 static void
1250 rule_print(struct fw3_ipt_rule *r, const char *prefix, const char *chain)
1251 {
1252         debug(r->h, "%s %s", prefix, chain);
1253
1254 #ifndef DISABLE_IPV6
1255         if (r->h->family == FW3_FAMILY_V6)
1256                 rule_print6(&r->e6);
1257         else
1258 #endif
1259                 rule_print4(&r->e);
1260
1261         fw3_xt_print_matches(&r->e.ip, r->matches);
1262         fw3_xt_print_target(&r->e.ip, r->target);
1263
1264         printf("\n");
1265 }
1266
1267 static bool
1268 parse_option(struct fw3_ipt_rule *r, int optc, bool inv)
1269 {
1270         struct xtables_rule_match *m;
1271         struct xtables_match *em;
1272
1273         /* is a target option */
1274         if (r->target && fw3_xt_has_target_parse(r->target) &&
1275                 optc >= r->target->option_offset &&
1276                 optc < (r->target->option_offset + 256))
1277         {
1278                 xtables_option_tpcall(optc, r->argv, inv, r->target, &r->e);
1279                 return false;
1280         }
1281
1282         /* try to dispatch argument to one of the match parsers */
1283         for (m = r->matches; m; m = m->next)
1284         {
1285                 em = m->match;
1286
1287                 if (m->completed || !fw3_xt_has_match_parse(em))
1288                         continue;
1289
1290                 if (optc < em->option_offset ||
1291                         optc >= (em->option_offset + 256))
1292                         continue;
1293
1294                 xtables_option_mpcall(optc, r->argv, inv, em, &r->e);
1295                 return false;
1296         }
1297
1298         /* unhandled option, might belong to a protocol match */
1299         if ((em = load_protomatch(r)) != NULL)
1300         {
1301                 init_match(r, em, false);
1302
1303                 r->protocol_loaded = true;
1304                 optind--;
1305
1306                 return true;
1307         }
1308
1309         if (optc == ':')
1310                 warn("parse_option(): option '%s' needs argument", r->argv[optind-1]);
1311
1312         if (optc == '?')
1313                 warn("parse_option(): unknown option '%s'", r->argv[optind-1]);
1314
1315         return false;
1316 }
1317
1318 void
1319 fw3_ipt_rule_addarg(struct fw3_ipt_rule *r, bool inv,
1320                     const char *k, const char *v)
1321 {
1322         int n;
1323         char **tmp;
1324
1325         if (!k)
1326                 return;
1327
1328         n = inv + !!k + !!v;
1329         tmp = realloc(r->argv, (r->argc + n) * sizeof(*tmp));
1330
1331         if (!tmp)
1332                 return;
1333
1334         r->argv = tmp;
1335
1336         if (inv)
1337                 r->argv[r->argc++] = fw3_strdup("!");
1338
1339         r->argv[r->argc++] = fw3_strdup(k);
1340
1341         if (v)
1342                 r->argv[r->argc++] = fw3_strdup(v);
1343 }
1344
1345 static unsigned char *
1346 rule_mask(struct fw3_ipt_rule *r)
1347 {
1348         size_t s;
1349         unsigned char *p, *mask = NULL;
1350         struct xtables_rule_match *m;
1351
1352 #define SZ(x) XT_ALIGN(sizeof(struct x))
1353
1354 #ifndef DISABLE_IPV6
1355         if (r->h->family == FW3_FAMILY_V6)
1356         {
1357                 s = SZ(ip6t_entry);
1358
1359                 for (m = r->matches; m; m = m->next)
1360                         s += SZ(ip6t_entry_match) + m->match->size;
1361
1362                 s += SZ(ip6t_entry_target);
1363                 if (r->target)
1364                         s += r->target->size;
1365
1366                 mask = fw3_alloc(s);
1367                 memset(mask, 0xFF, SZ(ip6t_entry));
1368                 p = mask + SZ(ip6t_entry);
1369
1370                 for (m = r->matches; m; m = m->next)
1371                 {
1372                         memset(p, 0xFF, SZ(ip6t_entry_match) + m->match->userspacesize);
1373                         p += SZ(ip6t_entry_match) + m->match->size;
1374                 }
1375
1376                 memset(p, 0xFF, SZ(ip6t_entry_target) + (r->target) ? r->target->userspacesize : 0);
1377         }
1378         else
1379 #endif
1380         {
1381                 s = SZ(ipt_entry);
1382
1383                 for (m = r->matches; m; m = m->next)
1384                         s += SZ(ipt_entry_match) + m->match->size;
1385
1386                 s += SZ(ipt_entry_target);
1387                 if (r->target)
1388                         s += r->target->size;
1389
1390                 mask = fw3_alloc(s);
1391                 memset(mask, 0xFF, SZ(ipt_entry));
1392                 p = mask + SZ(ipt_entry);
1393
1394                 for (m = r->matches; m; m = m->next)
1395                 {
1396                         memset(p, 0xFF, SZ(ipt_entry_match) + m->match->userspacesize);
1397                         p += SZ(ipt_entry_match) + m->match->size;
1398                 }
1399
1400                 memset(p, 0xFF, SZ(ipt_entry_target) + (r->target) ? r->target->userspacesize : 0);
1401         }
1402
1403         return mask;
1404 }
1405
1406 static void *
1407 rule_build(struct fw3_ipt_rule *r)
1408 {
1409         size_t s, target_size = (r->target) ? r->target->t->u.target_size : 0;
1410         struct xtables_rule_match *m;
1411
1412 #ifndef DISABLE_IPV6
1413         if (r->h->family == FW3_FAMILY_V6)
1414         {
1415                 struct ip6t_entry *e6;
1416
1417                 s = XT_ALIGN(sizeof(struct ip6t_entry));
1418
1419                 for (m = r->matches; m; m = m->next)
1420                         s += m->match->m->u.match_size;
1421
1422                 e6 = fw3_alloc(s + target_size);
1423
1424                 memcpy(e6, &r->e6, sizeof(struct ip6t_entry));
1425
1426                 e6->target_offset = s;
1427                 e6->next_offset = s + target_size;
1428
1429                 s = 0;
1430
1431                 for (m = r->matches; m; m = m->next)
1432                 {
1433                         memcpy(e6->elems + s, m->match->m, m->match->m->u.match_size);
1434                         s += m->match->m->u.match_size;
1435                 }
1436
1437                 if (target_size)
1438                         memcpy(e6->elems + s, r->target->t, target_size);
1439
1440                 return e6;
1441         }
1442         else
1443 #endif
1444         {
1445                 struct ipt_entry *e;
1446
1447                 s = XT_ALIGN(sizeof(struct ipt_entry));
1448
1449                 for (m = r->matches; m; m = m->next)
1450                         s += m->match->m->u.match_size;
1451
1452                 e = fw3_alloc(s + target_size);
1453
1454                 memcpy(e, &r->e, sizeof(struct ipt_entry));
1455
1456                 e->target_offset = s;
1457                 e->next_offset = s + target_size;
1458
1459                 s = 0;
1460
1461                 for (m = r->matches; m; m = m->next)
1462                 {
1463                         memcpy(e->elems + s, m->match->m, m->match->m->u.match_size);
1464                         s += m->match->m->u.match_size;
1465                 }
1466
1467                 if (target_size)
1468                         memcpy(e->elems + s, r->target->t, target_size);
1469
1470                 return e;
1471         }
1472 }
1473
1474 void
1475 __fw3_ipt_rule_append(struct fw3_ipt_rule *r, bool repl, const char *fmt, ...)
1476 {
1477         void *rule;
1478         unsigned char *mask;
1479
1480         struct xtables_rule_match *m;
1481         struct xtables_match *em;
1482         struct xtables_target *et;
1483         struct xtables_globals *g;
1484
1485         int i, optc;
1486         uint32_t id;
1487         bool inv = false;
1488         char buf[32];
1489         va_list ap;
1490
1491         va_start(ap, fmt);
1492         vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1493         va_end(ap);
1494
1495         g = (r->h->family == FW3_FAMILY_V6) ? &xtg6 : &xtg;
1496         g->opts = g->orig_opts;
1497
1498         optind = 0;
1499         opterr = 0;
1500
1501         if (r->id >= 0)
1502         {
1503                 em = find_match(r, "id");
1504
1505                 if (!em)
1506                 {
1507                         warn("fw3_ipt_rule_append(): Can't find match '%s'", "id");
1508                         goto free;
1509                 }
1510
1511                 init_match(r, em, true);
1512
1513                 id = FW3_ID_MAGIC | (r->id & ~FW3_ID_MASK);
1514                 memcpy(em->m->data, &id, sizeof(id));
1515
1516                 em->mflags = 1;
1517         }
1518
1519         while ((optc = getopt_long(r->argc, r->argv, "-:m:j:", g->opts,
1520                                    NULL)) != -1)
1521         {
1522                 switch (optc)
1523                 {
1524                 case 'm':
1525                         em = find_match(r, optarg);
1526
1527                         if (!em)
1528                         {
1529                                 warn("fw3_ipt_rule_append(): Can't find match '%s'", optarg);
1530                                 goto free;
1531                         }
1532
1533                         init_match(r, em, true);
1534                         break;
1535
1536                 case 'j':
1537                         et = get_target(r, optarg);
1538
1539                         if (!et)
1540                         {
1541                                 warn("fw3_ipt_rule_append(): Can't find target '%s'", optarg);
1542                                 goto free;
1543                         }
1544
1545                         break;
1546
1547                 case 1:
1548                         if ((optarg[0] == '!') && (optarg[1] == '\0'))
1549                         {
1550                                 optarg[0] = '\0';
1551                                 inv = true;
1552                                 continue;
1553                         }
1554
1555                         warn("fw3_ipt_rule_append(): Bad argument '%s'", optarg);
1556                         goto free;
1557
1558                 default:
1559                         if (parse_option(r, optc, inv))
1560                                 continue;
1561                         break;
1562                 }
1563
1564                 inv = false;
1565         }
1566
1567         for (m = r->matches; m; m = m->next)
1568                 xtables_option_mfcall(m->match);
1569
1570         if (r->target)
1571                 xtables_option_tfcall(r->target);
1572
1573         rule = rule_build(r);
1574
1575 #ifndef DISABLE_IPV6
1576         if (r->h->family == FW3_FAMILY_V6)
1577         {
1578                 if (repl)
1579                 {
1580                         mask = rule_mask(r);
1581
1582                         while (ip6tc_delete_entry(buf, rule, mask, r->h->handle))
1583                                 if (fw3_pr_debug)
1584                                         rule_print(r, "-D", buf);
1585
1586                         free(mask);
1587                 }
1588
1589                 if (fw3_pr_debug)
1590                         rule_print(r, "-A", buf);
1591
1592                 if (!ip6tc_append_entry(buf, rule, r->h->handle))
1593                         warn("ip6tc_append_entry(): %s", ip6tc_strerror(errno));
1594         }
1595         else
1596 #endif
1597         {
1598                 if (repl)
1599                 {
1600                         mask = rule_mask(r);
1601
1602                         while (iptc_delete_entry(buf, rule, mask, r->h->handle))
1603                                 if (fw3_pr_debug)
1604                                         rule_print(r, "-D", buf);
1605
1606                         free(mask);
1607                 }
1608
1609                 if (fw3_pr_debug)
1610                         rule_print(r, "-A", buf);
1611
1612                 if (!iptc_append_entry(buf, rule, r->h->handle))
1613                         warn("iptc_append_entry(): %s\n", iptc_strerror(errno));
1614         }
1615
1616         free(rule);
1617
1618 free:
1619         for (i = 1; i < r->argc; i++)
1620                 free(r->argv[i]);
1621
1622         free(r->argv);
1623
1624         xtables_rule_matches_free(&r->matches);
1625
1626         if (r->target)
1627                 free(r->target->t);
1628
1629         free(r);
1630
1631         /* reset all targets and matches */
1632         for (em = xtables_matches; em; em = em->next)
1633                 em->mflags = 0;
1634
1635         for (et = xtables_targets; et; et = et->next)
1636         {
1637                 et->tflags = 0;
1638                 et->used = 0;
1639         }
1640
1641         xtables_free_opts(1);
1642 }
1643
1644 struct fw3_ipt_rule *
1645 fw3_ipt_rule_create(struct fw3_ipt_handle *handle, struct fw3_protocol *proto,
1646                     struct fw3_device *in, struct fw3_device *out,
1647                     struct fw3_address *src, struct fw3_address *dest)
1648 {
1649         struct fw3_ipt_rule *r;
1650
1651         r = fw3_ipt_rule_new(handle);
1652
1653         fw3_ipt_rule_proto(r, proto);
1654         fw3_ipt_rule_in_out(r, in, out);
1655         fw3_ipt_rule_src_dest(r, src, dest);
1656
1657         return r;
1658 }
1659
1660 void
1661 xtables_register_match(struct xtables_match *me)
1662 {
1663         int i;
1664         static struct xtables_match **tmp;
1665
1666         if (!xext.register_match)
1667                 xext.register_match = dlsym(RTLD_NEXT, "xtables_register_match");
1668
1669         if (!xext.register_match)
1670                 return;
1671
1672         xext.register_match(me);
1673
1674         if (xext.retain)
1675         {
1676                 for (i = 0; i < xext.mcount; i++)
1677                         if (xext.matches[i] == me)
1678                                 return;
1679
1680                 tmp = realloc(xext.matches, sizeof(me) * (xext.mcount + 1));
1681
1682                 if (!tmp)
1683                         return;
1684
1685                 xext.matches = tmp;
1686                 xext.matches[xext.mcount++] = me;
1687         }
1688 }
1689
1690 void
1691 xtables_register_target(struct xtables_target *me)
1692 {
1693         int i;
1694         static struct xtables_target **tmp;
1695
1696         if (!xext.register_target)
1697                 xext.register_target = dlsym(RTLD_NEXT, "xtables_register_target");
1698
1699         if (!xext.register_target)
1700                 return;
1701
1702         xext.register_target(me);
1703
1704         if (xext.retain)
1705         {
1706                 for (i = 0; i < xext.tcount; i++)
1707                         if (xext.targets[i] == me)
1708                                 return;
1709
1710                 tmp = realloc(xext.targets, sizeof(me) * (xext.tcount + 1));
1711
1712                 if (!tmp)
1713                         return;
1714
1715                 xext.targets = tmp;
1716                 xext.targets[xext.tcount++] = me;
1717         }
1718 }