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