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