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