• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt