utils: persist effective extra_src and extra_dest options in state file
[project/firewall3.git] / utils.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
20
21 #include <net/if.h>
22 #include <sys/ioctl.h>
23
24 #include "utils.h"
25 #include "options.h"
26
27 #include "zones.h"
28 #include "ipsets.h"
29
30
31 static int fw3_lock_fd = -1;
32 static pid_t pipe_pid = -1;
33 static FILE *pipe_fd = NULL;
34
35 bool fw3_pr_debug = false;
36
37
38 static void
39 warn_elem_section_name(struct uci_section *s, bool find_name)
40 {
41 int i = 0;
42 struct uci_option *o;
43 struct uci_element *tmp;
44
45 if (s->anonymous)
46 {
47 uci_foreach_element(&s->package->sections, tmp)
48 {
49 if (strcmp(uci_to_section(tmp)->type, s->type))
50 continue;
51
52 if (&s->e == tmp)
53 break;
54
55 i++;
56 }
57
58 fprintf(stderr, "@%s[%d]", s->type, i);
59
60 if (find_name)
61 {
62 uci_foreach_element(&s->options, tmp)
63 {
64 o = uci_to_option(tmp);
65
66 if (!strcmp(tmp->name, "name") && (o->type == UCI_TYPE_STRING))
67 {
68 fprintf(stderr, " (%s)", o->v.string);
69 break;
70 }
71 }
72 }
73 }
74 else
75 {
76 fprintf(stderr, "'%s'", s->e.name);
77 }
78
79 if (find_name)
80 fprintf(stderr, " ");
81 }
82
83 void
84 warn_elem(struct uci_element *e, const char *format, ...)
85 {
86 if (e->type == UCI_TYPE_SECTION)
87 {
88 fprintf(stderr, "Warning: Section ");
89 warn_elem_section_name(uci_to_section(e), true);
90 }
91 else if (e->type == UCI_TYPE_OPTION)
92 {
93 fprintf(stderr, "Warning: Option ");
94 warn_elem_section_name(uci_to_option(e)->section, false);
95 fprintf(stderr, ".%s ", e->name);
96 }
97
98 va_list argptr;
99 va_start(argptr, format);
100 vfprintf(stderr, format, argptr);
101 va_end(argptr);
102
103 fprintf(stderr, "\n");
104 }
105
106 void
107 warn(const char* format, ...)
108 {
109 fprintf(stderr, "Warning: ");
110 va_list argptr;
111 va_start(argptr, format);
112 vfprintf(stderr, format, argptr);
113 va_end(argptr);
114 fprintf(stderr, "\n");
115 }
116
117 void
118 error(const char* format, ...)
119 {
120 fprintf(stderr, "Error: ");
121 va_list argptr;
122 va_start(argptr, format);
123 vfprintf(stderr, format, argptr);
124 va_end(argptr);
125 fprintf(stderr, "\n");
126
127 exit(1);
128 }
129
130 void
131 info(const char* format, ...)
132 {
133 va_list argptr;
134 va_start(argptr, format);
135 vfprintf(stderr, format, argptr);
136 va_end(argptr);
137 fprintf(stderr, "\n");
138 }
139
140 void *
141 fw3_alloc(size_t size)
142 {
143 void *mem;
144
145 mem = calloc(1, size);
146
147 if (!mem)
148 error("Out of memory while allocating %zd bytes", size);
149
150 return mem;
151 }
152
153 char *
154 fw3_strdup(const char *s)
155 {
156 char *ns;
157
158 ns = strdup(s);
159
160 if (!ns)
161 error("Out of memory while duplicating string '%s'", s);
162
163 return ns;
164 }
165
166 const char *
167 fw3_find_command(const char *cmd)
168 {
169 struct stat s;
170 int plen = 0, clen = strlen(cmd) + 1;
171 char *search, *p;
172 static char path[PATH_MAX];
173
174 if (!stat(cmd, &s) && S_ISREG(s.st_mode))
175 return cmd;
176
177 search = getenv("PATH");
178
179 if (!search)
180 search = "/bin:/usr/bin:/sbin:/usr/sbin";
181
182 p = search;
183
184 do
185 {
186 if (*p != ':' && *p != '\0')
187 continue;
188
189 plen = p - search;
190
191 if ((plen + clen) >= sizeof(path))
192 continue;
193
194 strncpy(path, search, plen);
195 sprintf(path + plen, "/%s", cmd);
196
197 if (!stat(path, &s) && S_ISREG(s.st_mode))
198 return path;
199
200 search = p + 1;
201 }
202 while (*p++);
203
204 return NULL;
205 }
206
207 bool
208 fw3_stdout_pipe(void)
209 {
210 pipe_fd = stdout;
211 return true;
212 }
213
214 bool
215 __fw3_command_pipe(bool silent, const char *command, ...)
216 {
217 pid_t pid;
218 va_list argp;
219 int pfds[2];
220 int argn;
221 char *arg, **args, **tmp;
222
223 command = fw3_find_command(command);
224
225 if (!command)
226 return false;
227
228 if (pipe(pfds))
229 return false;
230
231 argn = 2;
232 args = calloc(argn, sizeof(arg));
233
234 if (!args)
235 return false;
236
237 args[0] = (char *)command;
238 args[1] = NULL;
239
240 va_start(argp, command);
241
242 while ((arg = va_arg(argp, char *)) != NULL)
243 {
244 tmp = realloc(args, ++argn * sizeof(arg));
245
246 if (!tmp)
247 break;
248
249 args = tmp;
250 args[argn-2] = arg;
251 args[argn-1] = NULL;
252 }
253
254 va_end(argp);
255
256 switch ((pid = fork()))
257 {
258 case -1:
259 free(args);
260 return false;
261
262 case 0:
263 dup2(pfds[0], 0);
264
265 close(pfds[0]);
266 close(pfds[1]);
267
268 close(1);
269
270 if (silent)
271 close(2);
272
273 execv(command, args);
274
275 default:
276 signal(SIGPIPE, SIG_IGN);
277 pipe_pid = pid;
278 close(pfds[0]);
279 fcntl(pfds[1], F_SETFD, fcntl(pfds[1], F_GETFD) | FD_CLOEXEC);
280 }
281
282 pipe_fd = fdopen(pfds[1], "w");
283 free(args);
284 return true;
285 }
286
287 void
288 fw3_pr(const char *fmt, ...)
289 {
290 va_list args;
291
292 if (fw3_pr_debug && pipe_fd != stdout)
293 {
294 va_start(args, fmt);
295 vfprintf(stderr, fmt, args);
296 va_end(args);
297 }
298
299 va_start(args, fmt);
300 vfprintf(pipe_fd, fmt, args);
301 va_end(args);
302 }
303
304 void
305 fw3_command_close(void)
306 {
307 if (pipe_fd && pipe_fd != stdout)
308 fclose(pipe_fd);
309
310 if (pipe_pid > -1)
311 waitpid(pipe_pid, NULL, 0);
312
313 signal(SIGPIPE, SIG_DFL);
314
315 pipe_fd = NULL;
316 pipe_pid = -1;
317 }
318
319 bool
320 fw3_has_table(bool ipv6, const char *table)
321 {
322 FILE *f;
323
324 char line[12];
325 bool seen = false;
326
327 const char *path = ipv6
328 ? "/proc/net/ip6_tables_names" : "/proc/net/ip_tables_names";
329
330 if (!(f = fopen(path, "r")))
331 return false;
332
333 while (fgets(line, sizeof(line), f))
334 {
335 if (!strncmp(line, table, strlen(table)))
336 {
337 seen = true;
338 break;
339 }
340 }
341
342 fclose(f);
343
344 return seen;
345 }
346
347
348 bool
349 fw3_lock_path(int *fd, const char *path)
350 {
351 int lock_fd = open(path, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
352
353 if (lock_fd < 0)
354 {
355 warn("Cannot create lock file %s: %s", path, strerror(errno));
356 return false;
357 }
358
359 if (flock(lock_fd, LOCK_EX))
360 {
361 warn("Cannot acquire exclusive lock: %s", strerror(errno));
362 close(lock_fd);
363 return false;
364 }
365
366 *fd = lock_fd;
367
368 return true;
369 }
370
371 bool
372 fw3_lock()
373 {
374 return fw3_lock_path(&fw3_lock_fd, FW3_LOCKFILE);
375 }
376
377
378 void
379 fw3_unlock_path(int *fd, const char *lockpath)
380 {
381 if (*fd < 0)
382 return;
383
384 if (flock(*fd, LOCK_UN))
385 warn("Cannot release exclusive lock: %s", strerror(errno));
386
387 close(*fd);
388 unlink(FW3_LOCKFILE);
389
390 *fd = -1;
391 }
392
393
394 void
395 fw3_unlock(void)
396 {
397 fw3_unlock_path(&fw3_lock_fd, FW3_LOCKFILE);
398 }
399
400
401 static void
402 write_defaults_uci(struct uci_context *ctx, struct fw3_defaults *d,
403 struct uci_package *dest)
404 {
405 char buf[sizeof("0xffffffff\0")];
406 struct uci_ptr ptr = { .p = dest };
407
408 uci_add_section(ctx, dest, "defaults", &ptr.s);
409
410 ptr.o = NULL;
411 ptr.option = "input";
412 ptr.value = fw3_flag_names[d->policy_input];
413 uci_set(ctx, &ptr);
414
415 ptr.o = NULL;
416 ptr.option = "output";
417 ptr.value = fw3_flag_names[d->policy_output];
418 uci_set(ctx, &ptr);
419
420 ptr.o = NULL;
421 ptr.option = "forward";
422 ptr.value = fw3_flag_names[d->policy_forward];
423 uci_set(ctx, &ptr);
424
425 sprintf(buf, "0x%x", d->flags[0]);
426 ptr.o = NULL;
427 ptr.option = "__flags_v4";
428 ptr.value = buf;
429 uci_set(ctx, &ptr);
430
431 sprintf(buf, "0x%x", d->flags[1]);
432 ptr.o = NULL;
433 ptr.option = "__flags_v6";
434 ptr.value = buf;
435 uci_set(ctx, &ptr);
436 }
437
438 static void
439 write_zone_uci(struct uci_context *ctx, struct fw3_zone *z,
440 struct uci_package *dest, struct ifaddrs *ifaddr)
441 {
442 struct fw3_device *dev;
443 struct fw3_address *sub;
444 struct ifaddrs *ifa;
445 enum fw3_family fam = FW3_FAMILY_ANY;
446
447 char *p, buf[INET6_ADDRSTRLEN];
448
449 struct uci_ptr ptr = { .p = dest };
450
451 if (!z->enabled)
452 return;
453
454 if (fw3_no_table(z->flags[0]) && !fw3_no_table(z->flags[1]))
455 fam = FW3_FAMILY_V6;
456 else if (!fw3_no_table(z->flags[0]) && fw3_no_table(z->flags[1]))
457 fam = FW3_FAMILY_V4;
458 else if (fw3_no_table(z->flags[0]) && fw3_no_table(z->flags[1]))
459 return;
460
461 uci_add_section(ctx, dest, "zone", &ptr.s);
462
463 ptr.o = NULL;
464 ptr.option = "name";
465 ptr.value = z->name;
466 uci_set(ctx, &ptr);
467
468 ptr.o = NULL;
469 ptr.option = "input";
470 ptr.value = fw3_flag_names[z->policy_input];
471 uci_set(ctx, &ptr);
472
473 ptr.o = NULL;
474 ptr.option = "output";
475 ptr.value = fw3_flag_names[z->policy_output];
476 uci_set(ctx, &ptr);
477
478 ptr.o = NULL;
479 ptr.option = "forward";
480 ptr.value = fw3_flag_names[z->policy_forward];
481 uci_set(ctx, &ptr);
482
483 ptr.o = NULL;
484 ptr.option = "masq";
485 ptr.value = z->masq ? "1" : "0";
486 uci_set(ctx, &ptr);
487
488 ptr.o = NULL;
489 ptr.option = "mtu_fix";
490 ptr.value = z->mtu_fix ? "1" : "0";
491 uci_set(ctx, &ptr);
492
493 ptr.o = NULL;
494 ptr.option = "custom_chains";
495 ptr.value = z->custom_chains ? "1" : "0";
496 uci_set(ctx, &ptr);
497
498 if (fam != FW3_FAMILY_ANY)
499 {
500 ptr.o = NULL;
501 ptr.option = "family";
502 ptr.value = fw3_flag_names[fam];
503 uci_set(ctx, &ptr);
504 }
505
506 ptr.o = NULL;
507 ptr.option = "device";
508
509 fw3_foreach(dev, &z->devices)
510 {
511 char *ep;
512
513 if (!dev)
514 continue;
515
516 p = buf;
517 ep = buf + sizeof(buf);
518
519 if (dev->invert)
520 p += snprintf(p, ep - p, "!");
521
522 if (*dev->network)
523 p += snprintf(p, ep - p, "%s@%s", dev->name, dev->network);
524 else
525 p += snprintf(p, ep - p, "%s", dev->name);
526
527 ptr.value = buf;
528 uci_add_list(ctx, &ptr);
529 }
530
531 ptr.o = NULL;
532 ptr.option = "subnet";
533
534 fw3_foreach(sub, &z->subnets)
535 {
536 if (!sub)
537 continue;
538
539 ptr.value = fw3_address_to_string(sub, true, false);
540 uci_add_list(ctx, &ptr);
541 }
542
543 ptr.o = NULL;
544 ptr.option = "__addrs";
545
546 fw3_foreach(dev, &z->devices)
547 {
548 if (!dev)
549 continue;
550
551 for (ifa = ifaddr; ifa; ifa = ifa->ifa_next)
552 {
553 if (!ifa->ifa_addr || strcmp(dev->name, ifa->ifa_name))
554 continue;
555
556 if (ifa->ifa_addr->sa_family == AF_INET)
557 inet_ntop(AF_INET,
558 &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr,
559 buf, sizeof(buf));
560 else if (ifa->ifa_addr->sa_family == AF_INET6)
561 inet_ntop(AF_INET6,
562 &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr,
563 buf, sizeof(buf));
564 else
565 continue;
566
567 ptr.value = buf;
568 uci_add_list(ctx, &ptr);
569 }
570 }
571
572 if (z->extra_src)
573 {
574 ptr.o = NULL;
575 ptr.option = "extra_src";
576 ptr.value = z->extra_src;
577 uci_set(ctx, &ptr);
578 }
579
580 if (z->extra_dest)
581 {
582 ptr.o = NULL;
583 ptr.option = "extra_dest";
584 ptr.value = z->extra_dest;
585 uci_set(ctx, &ptr);
586 }
587
588 sprintf(buf, "0x%x", z->flags[0]);
589 ptr.o = NULL;
590 ptr.option = "__flags_v4";
591 ptr.value = buf;
592 uci_set(ctx, &ptr);
593
594 sprintf(buf, "0x%x", z->flags[1]);
595 ptr.o = NULL;
596 ptr.option = "__flags_v6";
597 ptr.value = buf;
598 uci_set(ctx, &ptr);
599 }
600
601 static void
602 write_ipset_uci(struct uci_context *ctx, struct fw3_ipset *s,
603 struct uci_package *dest)
604 {
605 struct fw3_ipset_datatype *type;
606
607 char buf[sizeof("65535-65535\0")];
608
609 struct uci_ptr ptr = { .p = dest };
610
611 if (!s->enabled || s->external)
612 return;
613
614 uci_add_section(ctx, dest, "ipset", &ptr.s);
615
616 ptr.o = NULL;
617 ptr.option = "name";
618 ptr.value = s->name;
619 uci_set(ctx, &ptr);
620
621 ptr.o = NULL;
622 ptr.option = "family";
623 if (s->family == FW3_FAMILY_V4)
624 ptr.value = "ipv4";
625 else
626 ptr.value = "ipv6";
627 uci_set(ctx, &ptr);
628
629 ptr.o = NULL;
630 ptr.option = "storage";
631 ptr.value = fw3_ipset_method_names[s->method];
632 uci_set(ctx, &ptr);
633
634 list_for_each_entry(type, &s->datatypes, list)
635 {
636 sprintf(buf, "%s_%s", type->dir, fw3_ipset_type_names[type->type]);
637 ptr.o = NULL;
638 ptr.option = "match";
639 ptr.value = buf;
640 uci_add_list(ctx, &ptr);
641 }
642
643 if (s->iprange.set)
644 {
645 ptr.o = NULL;
646 ptr.option = "iprange";
647 ptr.value = fw3_address_to_string(&s->iprange, false, false);
648 uci_set(ctx, &ptr);
649 }
650
651 if (s->portrange.set)
652 {
653 sprintf(buf, "%u-%u", s->portrange.port_min, s->portrange.port_max);
654 ptr.o = NULL;
655 ptr.option = "portrange";
656 ptr.value = buf;
657 uci_set(ctx, &ptr);
658 }
659 }
660
661 void
662 fw3_write_statefile(void *state)
663 {
664 FILE *sf;
665 struct fw3_state *s = state;
666 struct fw3_zone *z;
667 struct fw3_ipset *i;
668 struct ifaddrs *ifaddr;
669
670 struct uci_package *p;
671
672 if (fw3_no_family(s->defaults.flags[0]) &&
673 fw3_no_family(s->defaults.flags[1]))
674 {
675 unlink(FW3_STATEFILE);
676 }
677 else
678 {
679 sf = fopen(FW3_STATEFILE, "w+");
680
681 if (!sf)
682 {
683 warn("Cannot create state %s: %s", FW3_STATEFILE, strerror(errno));
684 return;
685 }
686
687 if (getifaddrs(&ifaddr))
688 {
689 warn("Cannot get interface addresses: %s", strerror(errno));
690 ifaddr = NULL;
691 }
692
693 if ((p = uci_lookup_package(s->uci, "fw3_state")) != NULL)
694 uci_unload(s->uci, p);
695
696 uci_import(s->uci, sf, "fw3_state", NULL, true);
697
698 if ((p = uci_lookup_package(s->uci, "fw3_state")) != NULL)
699 {
700 write_defaults_uci(s->uci, &s->defaults, p);
701
702 list_for_each_entry(z, &s->zones, list)
703 write_zone_uci(s->uci, z, p, ifaddr);
704
705 list_for_each_entry(i, &s->ipsets, list)
706 write_ipset_uci(s->uci, i, p);
707
708 uci_export(s->uci, sf, p, true);
709 uci_unload(s->uci, p);
710 }
711
712 fsync(fileno(sf));
713 fclose(sf);
714
715 if (ifaddr)
716 freeifaddrs(ifaddr);
717 }
718 }
719
720
721 void
722 fw3_free_object(void *obj, const void *opts)
723 {
724 const struct fw3_option *ol;
725 struct list_head *list, *cur, *tmp;
726
727 for (ol = opts; ol->name; ol++)
728 {
729 if (!ol->elem_size)
730 continue;
731
732 list = (struct list_head *)((char *)obj + ol->offset);
733 list_for_each_safe(cur, tmp, list)
734 {
735 list_del(cur);
736 free(cur);
737 }
738 }
739
740 free(obj);
741 }
742
743 void
744 fw3_free_list(struct list_head *head)
745 {
746 struct list_head *entry, *tmp;
747
748 if (!head)
749 return;
750
751 list_for_each_safe(entry, tmp, head)
752 {
753 list_del(entry);
754 free(entry);
755 }
756
757 free(head);
758 }
759
760 bool
761 fw3_hotplug(bool add, void *zone, void *device)
762 {
763 struct fw3_zone *z = zone;
764 struct fw3_device *d = device;
765
766 if (!*d->network)
767 return false;
768
769 switch (fork())
770 {
771 case -1:
772 warn("Unable to fork(): %s\n", strerror(errno));
773 return false;
774
775 case 0:
776 break;
777
778 default:
779 return true;
780 }
781
782 close(0);
783 close(1);
784 close(2);
785 if (chdir("/")) {};
786
787 clearenv();
788 setenv("ACTION", add ? "add" : "remove", 1);
789 setenv("ZONE", z->name, 1);
790 setenv("INTERFACE", d->network, 1);
791 setenv("DEVICE", d->name, 1);
792
793 execl(FW3_HOTPLUG, FW3_HOTPLUG, "firewall", NULL);
794
795 /* unreached */
796 return false;
797 }
798
799 int
800 fw3_netmask2bitlen(int family, void *mask)
801 {
802 int bits;
803 struct in_addr *v4;
804 struct in6_addr *v6;
805
806 if (family == FW3_FAMILY_V6)
807 for (bits = 0, v6 = mask;
808 bits < 128 && (v6->s6_addr[bits / 8] << (bits % 8)) & 128;
809 bits++);
810 else
811 for (bits = 0, v4 = mask;
812 bits < 32 && (ntohl(v4->s_addr) << bits) & 0x80000000;
813 bits++);
814
815 return bits;
816 }
817
818 bool
819 fw3_bitlen2netmask(int family, int bits, void *mask)
820 {
821 int i;
822 uint8_t rem, b;
823 struct in_addr *v4;
824 struct in6_addr *v6;
825
826 if (family == FW3_FAMILY_V6)
827 {
828 if (bits < -128 || bits > 128)
829 return false;
830
831 v6 = mask;
832 rem = abs(bits);
833
834 for (i = 0; i < sizeof(v6->s6_addr); i++)
835 {
836 b = (rem > 8) ? 8 : rem;
837 v6->s6_addr[i] = (uint8_t)(0xFF << (8 - b));
838 rem -= b;
839 }
840
841 if (bits < 0)
842 for (i = 0; i < sizeof(v6->s6_addr); i++)
843 v6->s6_addr[i] = ~v6->s6_addr[i];
844 }
845 else
846 {
847 if (bits < -32 || bits > 32)
848 return false;
849
850 v4 = mask;
851 v4->s_addr = bits ? htonl(~((1 << (32 - abs(bits))) - 1)) : 0;
852
853 if (bits < 0)
854 v4->s_addr = ~v4->s_addr;
855 }
856
857 return true;
858 }
859
860 void
861 fw3_flush_conntrack(void *state)
862 {
863 bool found;
864 struct fw3_state *s = state;
865 struct fw3_address *addr;
866 struct fw3_device *dev;
867 struct fw3_zone *zone;
868 struct ifaddrs *ifaddr, *ifa;
869 struct sockaddr_in *sin;
870 struct sockaddr_in6 *sin6;
871 char buf[INET6_ADDRSTRLEN];
872 FILE *ct;
873
874 if (!state)
875 {
876 if ((ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
877 {
878 info(" * Flushing conntrack table ...");
879
880 fwrite("f\n", 1, 2, ct);
881 fclose(ct);
882 }
883
884 return;
885 }
886
887 if (getifaddrs(&ifaddr))
888 {
889 warn("Cannot get interface addresses: %s", strerror(errno));
890 return;
891 }
892
893 if ((ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
894 {
895 list_for_each_entry(zone, &s->zones, list)
896 list_for_each_entry(addr, &zone->old_addrs, list)
897 {
898 found = false;
899
900 list_for_each_entry(dev, &zone->devices, list)
901 {
902 for (ifa = ifaddr; ifa && !found; ifa = ifa->ifa_next)
903 {
904 if (!ifa->ifa_addr || strcmp(dev->name, ifa->ifa_name))
905 continue;
906
907 sin = (struct sockaddr_in *)ifa->ifa_addr;
908 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
909
910 if (addr->family == FW3_FAMILY_V4 &&
911 sin->sin_family == AF_INET)
912 {
913 found = !memcmp(&addr->address.v4, &sin->sin_addr,
914 sizeof(sin->sin_addr));
915 }
916 else if (addr->family == FW3_FAMILY_V6 &&
917 sin6->sin6_family == AF_INET6)
918 {
919 found = !memcmp(&addr->address.v6, &sin6->sin6_addr,
920 sizeof(sin6->sin6_addr));
921 }
922 }
923
924 if (found)
925 break;
926 }
927
928 if (!found)
929 {
930 inet_ntop(addr->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
931 &addr->address.v4, buf, sizeof(buf));
932
933 info(" * Flushing conntrack: %s", buf);
934 fprintf(ct, "%s\n", buf);
935 }
936 }
937
938 fclose(ct);
939 }
940
941 freeifaddrs(ifaddr);
942 }
943
944 bool fw3_attr_parse_name_type(struct blob_attr *entry, const char **name, const char **type)
945 {
946 struct blob_attr *opt;
947 unsigned orem;
948
949 if (!type || !name)
950 return false;
951
952 *type = NULL;
953
954 blobmsg_for_each_attr(opt, entry, orem)
955 if (!strcmp(blobmsg_name(opt), "type"))
956 *type = blobmsg_get_string(opt);
957 else if (!strcmp(blobmsg_name(opt), "name"))
958 *name = blobmsg_get_string(opt);
959
960 return *type != NULL ? true : false;
961 }
962
963 const char *
964 fw3_protoname(void *proto)
965 {
966 static char buf[sizeof("4294967295")];
967 struct fw3_protocol *p = proto;
968 struct protoent *pe;
969
970 if (!p)
971 return "?";
972
973 pe = getprotobynumber(p->protocol);
974
975 if (!pe)
976 {
977 snprintf(buf, sizeof(buf), "%u", p->protocol);
978 return buf;
979 }
980
981 return pe->p_name;
982 }
983
984 bool
985 fw3_check_loopback_dev(const char *name)
986 {
987 struct ifreq ifr;
988 int s;
989 bool rv = false;
990
991 s = socket(AF_LOCAL, SOCK_DGRAM, 0);
992
993 if (s < 0)
994 return false;
995
996 memset(&ifr, 0, sizeof(ifr));
997 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name) - 1);
998
999 if (ioctl(s, SIOCGIFFLAGS, &ifr) >= 0) {
1000 if (ifr.ifr_flags & IFF_LOOPBACK)
1001 rv = true;
1002 }
1003
1004 close(s);
1005
1006 return rv;
1007 }
1008
1009 bool
1010 fw3_check_loopback_addr(struct fw3_address *addr)
1011 {
1012 if (addr->family == FW3_FAMILY_V4 &&
1013 (ntohl(addr->address.v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
1014 return true;
1015
1016 if (addr->family == FW3_FAMILY_V6 && !addr->range &&
1017 fw3_netmask2bitlen(FW3_FAMILY_V6, &addr->mask.v6) == 128 &&
1018 IN6_IS_ADDR_LOOPBACK(&addr->address.v6))
1019 return true;
1020
1021 return false;
1022 }