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