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