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