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