ubus: do not overwrite ipset name attribute
[oweals/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                 return false;
363         }
364
365         *fd = lock_fd;
366
367         return true;
368 }
369
370 bool
371 fw3_lock()
372 {
373         return fw3_lock_path(&fw3_lock_fd, FW3_LOCKFILE);
374 }
375
376
377 void
378 fw3_unlock_path(int *fd, const char *lockpath)
379 {
380         if (*fd < 0)
381                 return;
382
383         if (flock(*fd, LOCK_UN))
384                 warn("Cannot release exclusive lock: %s", strerror(errno));
385
386         close(*fd);
387         unlink(FW3_LOCKFILE);
388
389         *fd = -1;
390 }
391
392
393 void
394 fw3_unlock(void)
395 {
396         fw3_unlock_path(&fw3_lock_fd, FW3_LOCKFILE);
397 }
398
399
400 static void
401 write_defaults_uci(struct uci_context *ctx, struct fw3_defaults *d,
402                    struct uci_package *dest)
403 {
404         char buf[sizeof("0xffffffff\0")];
405         struct uci_ptr ptr = { .p = dest };
406
407         uci_add_section(ctx, dest, "defaults", &ptr.s);
408
409         ptr.o      = NULL;
410         ptr.option = "input";
411         ptr.value  = fw3_flag_names[d->policy_input];
412         uci_set(ctx, &ptr);
413
414         ptr.o      = NULL;
415         ptr.option = "output";
416         ptr.value  = fw3_flag_names[d->policy_output];
417         uci_set(ctx, &ptr);
418
419         ptr.o      = NULL;
420         ptr.option = "forward";
421         ptr.value  = fw3_flag_names[d->policy_forward];
422         uci_set(ctx, &ptr);
423
424         sprintf(buf, "0x%x", d->flags[0]);
425         ptr.o      = NULL;
426         ptr.option = "__flags_v4";
427         ptr.value  = buf;
428         uci_set(ctx, &ptr);
429
430         sprintf(buf, "0x%x", d->flags[1]);
431         ptr.o      = NULL;
432         ptr.option = "__flags_v6";
433         ptr.value  = buf;
434         uci_set(ctx, &ptr);
435 }
436
437 static void
438 write_zone_uci(struct uci_context *ctx, struct fw3_zone *z,
439                struct uci_package *dest, struct ifaddrs *ifaddr)
440 {
441         struct fw3_device *dev;
442         struct fw3_address *sub;
443         struct ifaddrs *ifa;
444         enum fw3_family fam = FW3_FAMILY_ANY;
445
446         char *p, buf[INET6_ADDRSTRLEN];
447
448         struct uci_ptr ptr = { .p = dest };
449
450         if (!z->enabled)
451                 return;
452
453         if (fw3_no_table(z->flags[0]) && !fw3_no_table(z->flags[1]))
454                 fam = FW3_FAMILY_V6;
455         else if (!fw3_no_table(z->flags[0]) && fw3_no_table(z->flags[1]))
456                 fam = FW3_FAMILY_V4;
457         else if (fw3_no_table(z->flags[0]) && fw3_no_table(z->flags[1]))
458                 return;
459
460         uci_add_section(ctx, dest, "zone", &ptr.s);
461
462         ptr.o      = NULL;
463         ptr.option = "name";
464         ptr.value  = z->name;
465         uci_set(ctx, &ptr);
466
467         ptr.o      = NULL;
468         ptr.option = "input";
469         ptr.value  = fw3_flag_names[z->policy_input];
470         uci_set(ctx, &ptr);
471
472         ptr.o      = NULL;
473         ptr.option = "output";
474         ptr.value  = fw3_flag_names[z->policy_output];
475         uci_set(ctx, &ptr);
476
477         ptr.o      = NULL;
478         ptr.option = "forward";
479         ptr.value  = fw3_flag_names[z->policy_forward];
480         uci_set(ctx, &ptr);
481
482         ptr.o      = NULL;
483         ptr.option = "masq";
484         ptr.value  = z->masq ? "1" : "0";
485         uci_set(ctx, &ptr);
486
487         ptr.o      = NULL;
488         ptr.option = "mtu_fix";
489         ptr.value  = z->mtu_fix ? "1" : "0";
490         uci_set(ctx, &ptr);
491
492         ptr.o      = NULL;
493         ptr.option = "custom_chains";
494         ptr.value  = z->custom_chains ? "1" : "0";
495         uci_set(ctx, &ptr);
496
497         if (fam != FW3_FAMILY_ANY)
498         {
499                 ptr.o      = NULL;
500                 ptr.option = "family";
501                 ptr.value  = fw3_flag_names[fam];
502                 uci_set(ctx, &ptr);
503         }
504
505         ptr.o      = NULL;
506         ptr.option = "device";
507
508         fw3_foreach(dev, &z->devices)
509         {
510                 char *ep;
511
512                 if (!dev)
513                         continue;
514
515                 p = buf;
516                 ep = buf + sizeof(buf);
517
518                 if (dev->invert)
519                         p += snprintf(p, ep - p, "!");
520
521                 if (*dev->network)
522                         p += snprintf(p, ep - p, "%s@%s", dev->name, dev->network);
523                 else
524                         p += snprintf(p, ep - p, "%s", dev->name);
525
526                 ptr.value = buf;
527                 uci_add_list(ctx, &ptr);
528         }
529
530         ptr.o      = NULL;
531         ptr.option = "subnet";
532
533         fw3_foreach(sub, &z->subnets)
534         {
535                 if (!sub)
536                         continue;
537
538                 ptr.value = fw3_address_to_string(sub, true, false);
539                 uci_add_list(ctx, &ptr);
540         }
541
542         ptr.o      = NULL;
543         ptr.option = "__addrs";
544
545         fw3_foreach(dev, &z->devices)
546         {
547                 if (!dev)
548                         continue;
549
550                 for (ifa = ifaddr; ifa; ifa = ifa->ifa_next)
551                 {
552                         if (!ifa->ifa_addr || strcmp(dev->name, ifa->ifa_name))
553                                 continue;
554
555                         if (ifa->ifa_addr->sa_family == AF_INET)
556                                 inet_ntop(AF_INET,
557                                           &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr,
558                                           buf, sizeof(buf));
559                         else if (ifa->ifa_addr->sa_family == AF_INET6)
560                                 inet_ntop(AF_INET6,
561                                           &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr,
562                                           buf, sizeof(buf));
563                         else
564                                 continue;
565
566                         ptr.value = buf;
567                         uci_add_list(ctx, &ptr);
568                 }
569         }
570
571         sprintf(buf, "0x%x", z->flags[0]);
572         ptr.o      = NULL;
573         ptr.option = "__flags_v4";
574         ptr.value  = buf;
575         uci_set(ctx, &ptr);
576
577         sprintf(buf, "0x%x", z->flags[1]);
578         ptr.o      = NULL;
579         ptr.option = "__flags_v6";
580         ptr.value  = buf;
581         uci_set(ctx, &ptr);
582 }
583
584 static void
585 write_ipset_uci(struct uci_context *ctx, struct fw3_ipset *s,
586                 struct uci_package *dest)
587 {
588         struct fw3_ipset_datatype *type;
589
590         char buf[sizeof("65535-65535\0")];
591
592         struct uci_ptr ptr = { .p = dest };
593
594         if (!s->enabled || s->external)
595                 return;
596
597         uci_add_section(ctx, dest, "ipset", &ptr.s);
598
599         ptr.o      = NULL;
600         ptr.option = "name";
601         ptr.value  = s->name;
602         uci_set(ctx, &ptr);
603
604         ptr.o      = NULL;
605         ptr.option = "family";
606         if (s->family == FW3_FAMILY_V4)
607                 ptr.value = "ipv4";
608         else
609                 ptr.value = "ipv6";
610         uci_set(ctx, &ptr);
611
612         ptr.o      = NULL;
613         ptr.option = "storage";
614         ptr.value  = fw3_ipset_method_names[s->method];
615         uci_set(ctx, &ptr);
616
617         list_for_each_entry(type, &s->datatypes, list)
618         {
619                 sprintf(buf, "%s_%s", type->dir, fw3_ipset_type_names[type->type]);
620                 ptr.o      = NULL;
621                 ptr.option = "match";
622                 ptr.value  = buf;
623                 uci_add_list(ctx, &ptr);
624         }
625
626         if (s->iprange.set)
627         {
628                 ptr.o      = NULL;
629                 ptr.option = "iprange";
630                 ptr.value  = fw3_address_to_string(&s->iprange, false, false);
631                 uci_set(ctx, &ptr);
632         }
633
634         if (s->portrange.set)
635         {
636                 sprintf(buf, "%u-%u", s->portrange.port_min, s->portrange.port_max);
637                 ptr.o      = NULL;
638                 ptr.option = "portrange";
639                 ptr.value  = buf;
640                 uci_set(ctx, &ptr);
641         }
642 }
643
644 void
645 fw3_write_statefile(void *state)
646 {
647         FILE *sf;
648         struct fw3_state *s = state;
649         struct fw3_zone *z;
650         struct fw3_ipset *i;
651         struct ifaddrs *ifaddr;
652
653         struct uci_package *p;
654
655         if (fw3_no_family(s->defaults.flags[0]) &&
656             fw3_no_family(s->defaults.flags[1]))
657         {
658                 unlink(FW3_STATEFILE);
659         }
660         else
661         {
662                 sf = fopen(FW3_STATEFILE, "w+");
663
664                 if (!sf)
665                 {
666                         warn("Cannot create state %s: %s", FW3_STATEFILE, strerror(errno));
667                         return;
668                 }
669
670                 if (getifaddrs(&ifaddr))
671                 {
672                         warn("Cannot get interface addresses: %s", strerror(errno));
673                         ifaddr = NULL;
674                 }
675
676                 if ((p = uci_lookup_package(s->uci, "fw3_state")) != NULL)
677                         uci_unload(s->uci, p);
678
679                 uci_import(s->uci, sf, "fw3_state", NULL, true);
680
681                 if ((p = uci_lookup_package(s->uci, "fw3_state")) != NULL)
682                 {
683                         write_defaults_uci(s->uci, &s->defaults, p);
684
685                         list_for_each_entry(z, &s->zones, list)
686                                 write_zone_uci(s->uci, z, p, ifaddr);
687
688                         list_for_each_entry(i, &s->ipsets, list)
689                                 write_ipset_uci(s->uci, i, p);
690
691                         uci_export(s->uci, sf, p, true);
692                         uci_unload(s->uci, p);
693                 }
694
695                 fsync(fileno(sf));
696                 fclose(sf);
697
698                 if (ifaddr)
699                         freeifaddrs(ifaddr);
700         }
701 }
702
703
704 void
705 fw3_free_object(void *obj, const void *opts)
706 {
707         const struct fw3_option *ol;
708         struct list_head *list, *cur, *tmp;
709
710         for (ol = opts; ol->name; ol++)
711         {
712                 if (!ol->elem_size)
713                         continue;
714
715                 list = (struct list_head *)((char *)obj + ol->offset);
716                 list_for_each_safe(cur, tmp, list)
717                 {
718                         list_del(cur);
719                         free(cur);
720                 }
721         }
722
723         free(obj);
724 }
725
726 void
727 fw3_free_list(struct list_head *head)
728 {
729         struct list_head *entry, *tmp;
730
731         if (!head)
732                 return;
733
734         list_for_each_safe(entry, tmp, head)
735         {
736                 list_del(entry);
737                 free(entry);
738         }
739
740         free(head);
741 }
742
743 bool
744 fw3_hotplug(bool add, void *zone, void *device)
745 {
746         struct fw3_zone *z = zone;
747         struct fw3_device *d = device;
748
749         if (!*d->network)
750                 return false;
751
752         switch (fork())
753         {
754         case -1:
755                 warn("Unable to fork(): %s\n", strerror(errno));
756                 return false;
757
758         case 0:
759                 break;
760
761         default:
762                 return true;
763         }
764
765         close(0);
766         close(1);
767         close(2);
768         if (chdir("/")) {};
769
770         clearenv();
771         setenv("ACTION",    add ? "add" : "remove", 1);
772         setenv("ZONE",      z->name,                1);
773         setenv("INTERFACE", d->network,             1);
774         setenv("DEVICE",    d->name,                1);
775
776         execl(FW3_HOTPLUG, FW3_HOTPLUG, "firewall", NULL);
777
778         /* unreached */
779         return false;
780 }
781
782 int
783 fw3_netmask2bitlen(int family, void *mask)
784 {
785         int bits;
786         struct in_addr *v4;
787         struct in6_addr *v6;
788
789         if (family == FW3_FAMILY_V6)
790                 for (bits = 0, v6 = mask;
791                      bits < 128 && (v6->s6_addr[bits / 8] << (bits % 8)) & 128;
792                      bits++);
793         else
794                 for (bits = 0, v4 = mask;
795                      bits < 32 && (ntohl(v4->s_addr) << bits) & 0x80000000;
796                      bits++);
797
798         return bits;
799 }
800
801 bool
802 fw3_bitlen2netmask(int family, int bits, void *mask)
803 {
804         int i;
805         uint8_t rem, b;
806         struct in_addr *v4;
807         struct in6_addr *v6;
808
809         if (family == FW3_FAMILY_V6)
810         {
811                 if (bits < -128 || bits > 128)
812                         return false;
813
814                 v6 = mask;
815                 rem = abs(bits);
816
817                 for (i = 0; i < sizeof(v6->s6_addr); i++)
818                 {
819                         b = (rem > 8) ? 8 : rem;
820                         v6->s6_addr[i] = (uint8_t)(0xFF << (8 - b));
821                         rem -= b;
822                 }
823
824                 if (bits < 0)
825                         for (i = 0; i < sizeof(v6->s6_addr); i++)
826                                 v6->s6_addr[i] = ~v6->s6_addr[i];
827         }
828         else
829         {
830                 if (bits < -32 || bits > 32)
831                         return false;
832
833                 v4 = mask;
834                 v4->s_addr = bits ? htonl(~((1 << (32 - abs(bits))) - 1)) : 0;
835
836                 if (bits < 0)
837                         v4->s_addr = ~v4->s_addr;
838         }
839
840         return true;
841 }
842
843 void
844 fw3_flush_conntrack(void *state)
845 {
846         bool found;
847         struct fw3_state *s = state;
848         struct fw3_address *addr;
849         struct fw3_device *dev;
850         struct fw3_zone *zone;
851         struct ifaddrs *ifaddr, *ifa;
852         struct sockaddr_in *sin;
853         struct sockaddr_in6 *sin6;
854         char buf[INET6_ADDRSTRLEN];
855         FILE *ct;
856
857         if (!state)
858         {
859                 if ((ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
860                 {
861                         info(" * Flushing conntrack table ...");
862
863                         fwrite("f\n", 1, 2, ct);
864                         fclose(ct);
865                 }
866
867                 return;
868         }
869
870         if (getifaddrs(&ifaddr))
871         {
872                 warn("Cannot get interface addresses: %s", strerror(errno));
873                 return;
874         }
875
876         if ((ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
877         {
878                 list_for_each_entry(zone, &s->zones, list)
879                 list_for_each_entry(addr, &zone->old_addrs, list)
880                 {
881                         found = false;
882
883                         list_for_each_entry(dev, &zone->devices, list)
884                         {
885                                 for (ifa = ifaddr; ifa && !found; ifa = ifa->ifa_next)
886                                 {
887                                         if (!ifa->ifa_addr || strcmp(dev->name, ifa->ifa_name))
888                                                 continue;
889
890                                         sin = (struct sockaddr_in *)ifa->ifa_addr;
891                                         sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
892
893                                         if (addr->family == FW3_FAMILY_V4 &&
894                                                 sin->sin_family == AF_INET)
895                                         {
896                                                 found = !memcmp(&addr->address.v4, &sin->sin_addr,
897                                                                                 sizeof(sin->sin_addr));
898                                         }
899                                         else if (addr->family == FW3_FAMILY_V6 &&
900                                                          sin6->sin6_family == AF_INET6)
901                                         {
902                                                 found = !memcmp(&addr->address.v6, &sin6->sin6_addr,
903                                                                                 sizeof(sin6->sin6_addr));
904                                         }
905                                 }
906
907                                 if (found)
908                                         break;
909                         }
910
911                         if (!found)
912                         {
913                                 inet_ntop(addr->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
914                                                   &addr->address.v4, buf, sizeof(buf));
915
916                                 info(" * Flushing conntrack: %s", buf);
917                                 fprintf(ct, "%s\n", buf);
918                         }
919                 }
920
921                 fclose(ct);
922         }
923
924         freeifaddrs(ifaddr);
925 }
926
927 bool fw3_attr_parse_name_type(struct blob_attr *entry, const char **name, const char **type)
928 {
929         struct blob_attr *opt;
930         unsigned orem;
931
932         if (!type || !name)
933                 return false;
934
935         *type = NULL;
936
937         blobmsg_for_each_attr(opt, entry, orem)
938                 if (!strcmp(blobmsg_name(opt), "type"))
939                         *type = blobmsg_get_string(opt);
940                 else if (!strcmp(blobmsg_name(opt), "name"))
941                         *name = blobmsg_get_string(opt);
942
943         return *type != NULL ? true : false;
944 }
945
946 const char *
947 fw3_protoname(void *proto)
948 {
949         static char buf[sizeof("4294967295")];
950         struct fw3_protocol *p = proto;
951         struct protoent *pe;
952
953         if (!p)
954                 return "?";
955
956         pe = getprotobynumber(p->protocol);
957
958         if (!pe)
959         {
960                 snprintf(buf, sizeof(buf), "%u", p->protocol);
961                 return buf;
962         }
963
964         return pe->p_name;
965 }
966
967 bool
968 fw3_check_loopback_dev(const char *name)
969 {
970         struct ifreq ifr;
971         int s;
972         bool rv = false;
973
974         s = socket(AF_LOCAL, SOCK_DGRAM, 0);
975
976         if (s < 0)
977                 return false;
978
979         memset(&ifr, 0, sizeof(ifr));
980         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name) - 1);
981
982         if (ioctl(s, SIOCGIFFLAGS, &ifr) >= 0) {
983                 if (ifr.ifr_flags & IFF_LOOPBACK)
984                         rv = true;
985         }
986
987         close(s);
988
989         return rv;
990 }
991
992 bool
993 fw3_check_loopback_addr(struct fw3_address *addr)
994 {
995         if (addr->family == FW3_FAMILY_V4 &&
996             (ntohl(addr->address.v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
997                 return true;
998
999         if (addr->family == FW3_FAMILY_V6 && !addr->range &&
1000             fw3_netmask2bitlen(FW3_FAMILY_V6, &addr->mask.v6) == 128 &&
1001             IN6_IS_ADDR_LOOPBACK(&addr->address.v6))
1002                 return true;
1003
1004         return false;
1005 }