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