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