utils: fix resource leak
[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 sprintf(buf, "0x%x", z->flags[0]);
573 ptr.o = NULL;
574 ptr.option = "__flags_v4";
575 ptr.value = buf;
576 uci_set(ctx, &ptr);
577
578 sprintf(buf, "0x%x", z->flags[1]);
579 ptr.o = NULL;
580 ptr.option = "__flags_v6";
581 ptr.value = buf;
582 uci_set(ctx, &ptr);
583 }
584
585 static void
586 write_ipset_uci(struct uci_context *ctx, struct fw3_ipset *s,
587 struct uci_package *dest)
588 {
589 struct fw3_ipset_datatype *type;
590
591 char buf[sizeof("65535-65535\0")];
592
593 struct uci_ptr ptr = { .p = dest };
594
595 if (!s->enabled || s->external)
596 return;
597
598 uci_add_section(ctx, dest, "ipset", &ptr.s);
599
600 ptr.o = NULL;
601 ptr.option = "name";
602 ptr.value = s->name;
603 uci_set(ctx, &ptr);
604
605 ptr.o = NULL;
606 ptr.option = "family";
607 if (s->family == FW3_FAMILY_V4)
608 ptr.value = "ipv4";
609 else
610 ptr.value = "ipv6";
611 uci_set(ctx, &ptr);
612
613 ptr.o = NULL;
614 ptr.option = "storage";
615 ptr.value = fw3_ipset_method_names[s->method];
616 uci_set(ctx, &ptr);
617
618 list_for_each_entry(type, &s->datatypes, list)
619 {
620 sprintf(buf, "%s_%s", type->dir, fw3_ipset_type_names[type->type]);
621 ptr.o = NULL;
622 ptr.option = "match";
623 ptr.value = buf;
624 uci_add_list(ctx, &ptr);
625 }
626
627 if (s->iprange.set)
628 {
629 ptr.o = NULL;
630 ptr.option = "iprange";
631 ptr.value = fw3_address_to_string(&s->iprange, false, false);
632 uci_set(ctx, &ptr);
633 }
634
635 if (s->portrange.set)
636 {
637 sprintf(buf, "%u-%u", s->portrange.port_min, s->portrange.port_max);
638 ptr.o = NULL;
639 ptr.option = "portrange";
640 ptr.value = buf;
641 uci_set(ctx, &ptr);
642 }
643 }
644
645 void
646 fw3_write_statefile(void *state)
647 {
648 FILE *sf;
649 struct fw3_state *s = state;
650 struct fw3_zone *z;
651 struct fw3_ipset *i;
652 struct ifaddrs *ifaddr;
653
654 struct uci_package *p;
655
656 if (fw3_no_family(s->defaults.flags[0]) &&
657 fw3_no_family(s->defaults.flags[1]))
658 {
659 unlink(FW3_STATEFILE);
660 }
661 else
662 {
663 sf = fopen(FW3_STATEFILE, "w+");
664
665 if (!sf)
666 {
667 warn("Cannot create state %s: %s", FW3_STATEFILE, strerror(errno));
668 return;
669 }
670
671 if (getifaddrs(&ifaddr))
672 {
673 warn("Cannot get interface addresses: %s", strerror(errno));
674 ifaddr = NULL;
675 }
676
677 if ((p = uci_lookup_package(s->uci, "fw3_state")) != NULL)
678 uci_unload(s->uci, p);
679
680 uci_import(s->uci, sf, "fw3_state", NULL, true);
681
682 if ((p = uci_lookup_package(s->uci, "fw3_state")) != NULL)
683 {
684 write_defaults_uci(s->uci, &s->defaults, p);
685
686 list_for_each_entry(z, &s->zones, list)
687 write_zone_uci(s->uci, z, p, ifaddr);
688
689 list_for_each_entry(i, &s->ipsets, list)
690 write_ipset_uci(s->uci, i, p);
691
692 uci_export(s->uci, sf, p, true);
693 uci_unload(s->uci, p);
694 }
695
696 fsync(fileno(sf));
697 fclose(sf);
698
699 if (ifaddr)
700 freeifaddrs(ifaddr);
701 }
702 }
703
704
705 void
706 fw3_free_object(void *obj, const void *opts)
707 {
708 const struct fw3_option *ol;
709 struct list_head *list, *cur, *tmp;
710
711 for (ol = opts; ol->name; ol++)
712 {
713 if (!ol->elem_size)
714 continue;
715
716 list = (struct list_head *)((char *)obj + ol->offset);
717 list_for_each_safe(cur, tmp, list)
718 {
719 list_del(cur);
720 free(cur);
721 }
722 }
723
724 free(obj);
725 }
726
727 void
728 fw3_free_list(struct list_head *head)
729 {
730 struct list_head *entry, *tmp;
731
732 if (!head)
733 return;
734
735 list_for_each_safe(entry, tmp, head)
736 {
737 list_del(entry);
738 free(entry);
739 }
740
741 free(head);
742 }
743
744 bool
745 fw3_hotplug(bool add, void *zone, void *device)
746 {
747 struct fw3_zone *z = zone;
748 struct fw3_device *d = device;
749
750 if (!*d->network)
751 return false;
752
753 switch (fork())
754 {
755 case -1:
756 warn("Unable to fork(): %s\n", strerror(errno));
757 return false;
758
759 case 0:
760 break;
761
762 default:
763 return true;
764 }
765
766 close(0);
767 close(1);
768 close(2);
769 if (chdir("/")) {};
770
771 clearenv();
772 setenv("ACTION", add ? "add" : "remove", 1);
773 setenv("ZONE", z->name, 1);
774 setenv("INTERFACE", d->network, 1);
775 setenv("DEVICE", d->name, 1);
776
777 execl(FW3_HOTPLUG, FW3_HOTPLUG, "firewall", NULL);
778
779 /* unreached */
780 return false;
781 }
782
783 int
784 fw3_netmask2bitlen(int family, void *mask)
785 {
786 int bits;
787 struct in_addr *v4;
788 struct in6_addr *v6;
789
790 if (family == FW3_FAMILY_V6)
791 for (bits = 0, v6 = mask;
792 bits < 128 && (v6->s6_addr[bits / 8] << (bits % 8)) & 128;
793 bits++);
794 else
795 for (bits = 0, v4 = mask;
796 bits < 32 && (ntohl(v4->s_addr) << bits) & 0x80000000;
797 bits++);
798
799 return bits;
800 }
801
802 bool
803 fw3_bitlen2netmask(int family, int bits, void *mask)
804 {
805 int i;
806 uint8_t rem, b;
807 struct in_addr *v4;
808 struct in6_addr *v6;
809
810 if (family == FW3_FAMILY_V6)
811 {
812 if (bits < -128 || bits > 128)
813 return false;
814
815 v6 = mask;
816 rem = abs(bits);
817
818 for (i = 0; i < sizeof(v6->s6_addr); i++)
819 {
820 b = (rem > 8) ? 8 : rem;
821 v6->s6_addr[i] = (uint8_t)(0xFF << (8 - b));
822 rem -= b;
823 }
824
825 if (bits < 0)
826 for (i = 0; i < sizeof(v6->s6_addr); i++)
827 v6->s6_addr[i] = ~v6->s6_addr[i];
828 }
829 else
830 {
831 if (bits < -32 || bits > 32)
832 return false;
833
834 v4 = mask;
835 v4->s_addr = bits ? htonl(~((1 << (32 - abs(bits))) - 1)) : 0;
836
837 if (bits < 0)
838 v4->s_addr = ~v4->s_addr;
839 }
840
841 return true;
842 }
843
844 void
845 fw3_flush_conntrack(void *state)
846 {
847 bool found;
848 struct fw3_state *s = state;
849 struct fw3_address *addr;
850 struct fw3_device *dev;
851 struct fw3_zone *zone;
852 struct ifaddrs *ifaddr, *ifa;
853 struct sockaddr_in *sin;
854 struct sockaddr_in6 *sin6;
855 char buf[INET6_ADDRSTRLEN];
856 FILE *ct;
857
858 if (!state)
859 {
860 if ((ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
861 {
862 info(" * Flushing conntrack table ...");
863
864 fwrite("f\n", 1, 2, ct);
865 fclose(ct);
866 }
867
868 return;
869 }
870
871 if (getifaddrs(&ifaddr))
872 {
873 warn("Cannot get interface addresses: %s", strerror(errno));
874 return;
875 }
876
877 if ((ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
878 {
879 list_for_each_entry(zone, &s->zones, list)
880 list_for_each_entry(addr, &zone->old_addrs, list)
881 {
882 found = false;
883
884 list_for_each_entry(dev, &zone->devices, list)
885 {
886 for (ifa = ifaddr; ifa && !found; ifa = ifa->ifa_next)
887 {
888 if (!ifa->ifa_addr || strcmp(dev->name, ifa->ifa_name))
889 continue;
890
891 sin = (struct sockaddr_in *)ifa->ifa_addr;
892 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
893
894 if (addr->family == FW3_FAMILY_V4 &&
895 sin->sin_family == AF_INET)
896 {
897 found = !memcmp(&addr->address.v4, &sin->sin_addr,
898 sizeof(sin->sin_addr));
899 }
900 else if (addr->family == FW3_FAMILY_V6 &&
901 sin6->sin6_family == AF_INET6)
902 {
903 found = !memcmp(&addr->address.v6, &sin6->sin6_addr,
904 sizeof(sin6->sin6_addr));
905 }
906 }
907
908 if (found)
909 break;
910 }
911
912 if (!found)
913 {
914 inet_ntop(addr->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
915 &addr->address.v4, buf, sizeof(buf));
916
917 info(" * Flushing conntrack: %s", buf);
918 fprintf(ct, "%s\n", buf);
919 }
920 }
921
922 fclose(ct);
923 }
924
925 freeifaddrs(ifaddr);
926 }
927
928 bool fw3_attr_parse_name_type(struct blob_attr *entry, const char **name, const char **type)
929 {
930 struct blob_attr *opt;
931 unsigned orem;
932
933 if (!type || !name)
934 return false;
935
936 *type = NULL;
937
938 blobmsg_for_each_attr(opt, entry, orem)
939 if (!strcmp(blobmsg_name(opt), "type"))
940 *type = blobmsg_get_string(opt);
941 else if (!strcmp(blobmsg_name(opt), "name"))
942 *name = blobmsg_get_string(opt);
943
944 return *type != NULL ? true : false;
945 }
946
947 const char *
948 fw3_protoname(void *proto)
949 {
950 static char buf[sizeof("4294967295")];
951 struct fw3_protocol *p = proto;
952 struct protoent *pe;
953
954 if (!p)
955 return "?";
956
957 pe = getprotobynumber(p->protocol);
958
959 if (!pe)
960 {
961 snprintf(buf, sizeof(buf), "%u", p->protocol);
962 return buf;
963 }
964
965 return pe->p_name;
966 }
967
968 bool
969 fw3_check_loopback_dev(const char *name)
970 {
971 struct ifreq ifr;
972 int s;
973 bool rv = false;
974
975 s = socket(AF_LOCAL, SOCK_DGRAM, 0);
976
977 if (s < 0)
978 return false;
979
980 memset(&ifr, 0, sizeof(ifr));
981 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name) - 1);
982
983 if (ioctl(s, SIOCGIFFLAGS, &ifr) >= 0) {
984 if (ifr.ifr_flags & IFF_LOOPBACK)
985 rv = true;
986 }
987
988 close(s);
989
990 return rv;
991 }
992
993 bool
994 fw3_check_loopback_addr(struct fw3_address *addr)
995 {
996 if (addr->family == FW3_FAMILY_V4 &&
997 (ntohl(addr->address.v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
998 return true;
999
1000 if (addr->family == FW3_FAMILY_V6 && !addr->range &&
1001 fw3_netmask2bitlen(FW3_FAMILY_V6, &addr->mask.v6) == 128 &&
1002 IN6_IS_ADDR_LOOPBACK(&addr->address.v6))
1003 return true;
1004
1005 return false;
1006 }