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