2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
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.
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.
22 #include <sys/ioctl.h>
31 static int fw3_lock_fd
= -1;
32 static pid_t pipe_pid
= -1;
33 static FILE *pipe_fd
= NULL
;
35 bool fw3_pr_debug
= false;
39 warn_elem_section_name(struct uci_section
*s
, bool find_name
)
43 struct uci_element
*tmp
;
47 uci_foreach_element(&s
->package
->sections
, tmp
)
49 if (strcmp(uci_to_section(tmp
)->type
, s
->type
))
58 fprintf(stderr
, "@%s[%d]", s
->type
, i
);
62 uci_foreach_element(&s
->options
, tmp
)
64 o
= uci_to_option(tmp
);
66 if (!strcmp(tmp
->name
, "name") && (o
->type
== UCI_TYPE_STRING
))
68 fprintf(stderr
, " (%s)", o
->v
.string
);
76 fprintf(stderr
, "'%s'", s
->e
.name
);
84 warn_elem(struct uci_element
*e
, const char *format
, ...)
86 if (e
->type
== UCI_TYPE_SECTION
)
88 fprintf(stderr
, "Warning: Section ");
89 warn_elem_section_name(uci_to_section(e
), true);
91 else if (e
->type
== UCI_TYPE_OPTION
)
93 fprintf(stderr
, "Warning: Option ");
94 warn_elem_section_name(uci_to_option(e
)->section
, false);
95 fprintf(stderr
, ".%s ", e
->name
);
99 va_start(argptr
, format
);
100 vfprintf(stderr
, format
, argptr
);
103 fprintf(stderr
, "\n");
107 warn(const char* format
, ...)
109 fprintf(stderr
, "Warning: ");
111 va_start(argptr
, format
);
112 vfprintf(stderr
, format
, argptr
);
114 fprintf(stderr
, "\n");
118 error(const char* format
, ...)
120 fprintf(stderr
, "Error: ");
122 va_start(argptr
, format
);
123 vfprintf(stderr
, format
, argptr
);
125 fprintf(stderr
, "\n");
131 info(const char* format
, ...)
134 va_start(argptr
, format
);
135 vfprintf(stderr
, format
, argptr
);
137 fprintf(stderr
, "\n");
141 fw3_alloc(size_t size
)
145 mem
= calloc(1, size
);
148 error("Out of memory while allocating %zd bytes", size
);
154 fw3_strdup(const char *s
)
161 error("Out of memory while duplicating string '%s'", s
);
167 fw3_find_command(const char *cmd
)
170 int plen
= 0, clen
= strlen(cmd
) + 1;
172 static char path
[PATH_MAX
];
174 if (!stat(cmd
, &s
) && S_ISREG(s
.st_mode
))
177 search
= getenv("PATH");
180 search
= "/bin:/usr/bin:/sbin:/usr/sbin";
186 if (*p
!= ':' && *p
!= '\0')
191 if ((plen
+ clen
) >= sizeof(path
))
194 snprintf(path
, sizeof(path
), "%.*s/%s", plen
, search
, cmd
);
196 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
207 fw3_stdout_pipe(void)
214 __fw3_command_pipe(bool silent
, const char *command
, ...)
220 char *arg
, **args
, **tmp
;
222 command
= fw3_find_command(command
);
231 args
= calloc(argn
, sizeof(arg
));
236 args
[0] = (char *)command
;
239 va_start(argp
, command
);
241 while ((arg
= va_arg(argp
, char *)) != NULL
)
243 tmp
= realloc(args
, ++argn
* sizeof(arg
));
255 switch ((pid
= fork()))
272 execv(command
, args
);
275 signal(SIGPIPE
, SIG_IGN
);
278 fcntl(pfds
[1], F_SETFD
, fcntl(pfds
[1], F_GETFD
) | FD_CLOEXEC
);
281 pipe_fd
= fdopen(pfds
[1], "w");
287 fw3_pr(const char *fmt
, ...)
291 if (fw3_pr_debug
&& pipe_fd
!= stdout
)
294 vfprintf(stderr
, fmt
, args
);
299 vfprintf(pipe_fd
, fmt
, args
);
304 fw3_command_close(void)
306 if (pipe_fd
&& pipe_fd
!= stdout
)
310 waitpid(pipe_pid
, NULL
, 0);
312 signal(SIGPIPE
, SIG_DFL
);
319 file_contains(const char *path
, const char *str
)
325 if (!(f
= fopen(path
, "r")))
328 while (fgets(line
, sizeof(line
), f
))
330 if (!strncmp(line
, str
, strlen(str
)))
343 fw3_has_table(const bool ipv6
, const char *table
)
345 const char *path
= ipv6
346 ? "/proc/net/ip6_tables_names" : "/proc/net/ip_tables_names";
348 return file_contains(path
, table
);
352 fw3_has_target(const bool ipv6
, const char *target
)
354 const char *path
= ipv6
355 ? "/proc/net/ip6_tables_targets" : "/proc/net/ip_tables_targets";
357 return file_contains(path
, target
);
361 fw3_lock_path(int *fd
, const char *path
)
363 int lock_fd
= open(path
, O_CREAT
|O_WRONLY
, S_IRUSR
|S_IWUSR
);
367 warn("Cannot create lock file %s: %s", path
, strerror(errno
));
371 if (flock(lock_fd
, LOCK_EX
))
373 warn("Cannot acquire exclusive lock: %s", strerror(errno
));
386 return fw3_lock_path(&fw3_lock_fd
, FW3_LOCKFILE
);
391 fw3_unlock_path(int *fd
, const char *lockpath
)
396 if (flock(*fd
, LOCK_UN
))
397 warn("Cannot release exclusive lock: %s", strerror(errno
));
400 unlink(FW3_LOCKFILE
);
409 fw3_unlock_path(&fw3_lock_fd
, FW3_LOCKFILE
);
414 write_defaults_uci(struct uci_context
*ctx
, struct fw3_defaults
*d
,
415 struct uci_package
*dest
)
417 char buf
[sizeof("0xffffffff")];
418 struct uci_ptr ptr
= { .p
= dest
};
420 uci_add_section(ctx
, dest
, "defaults", &ptr
.s
);
423 ptr
.option
= "input";
424 ptr
.value
= fw3_flag_names
[d
->policy_input
];
428 ptr
.option
= "output";
429 ptr
.value
= fw3_flag_names
[d
->policy_output
];
433 ptr
.option
= "forward";
434 ptr
.value
= fw3_flag_names
[d
->policy_forward
];
437 snprintf(buf
, sizeof(buf
), "0x%x", d
->flags
[0]);
439 ptr
.option
= "__flags_v4";
443 snprintf(buf
, sizeof(buf
), "0x%x", d
->flags
[1]);
445 ptr
.option
= "__flags_v6";
451 write_zone_uci(struct uci_context
*ctx
, struct fw3_zone
*z
,
452 struct uci_package
*dest
, struct ifaddrs
*ifaddr
)
454 struct fw3_device
*dev
;
455 struct fw3_address
*sub
;
457 enum fw3_family fam
= FW3_FAMILY_ANY
;
459 char *p
, buf
[INET6_ADDRSTRLEN
];
461 struct uci_ptr ptr
= { .p
= dest
};
466 if (fw3_no_table(z
->flags
[0]) && !fw3_no_table(z
->flags
[1]))
468 else if (!fw3_no_table(z
->flags
[0]) && fw3_no_table(z
->flags
[1]))
470 else if (fw3_no_table(z
->flags
[0]) && fw3_no_table(z
->flags
[1]))
473 uci_add_section(ctx
, dest
, "zone", &ptr
.s
);
481 ptr
.option
= "input";
482 ptr
.value
= fw3_flag_names
[z
->policy_input
];
486 ptr
.option
= "output";
487 ptr
.value
= fw3_flag_names
[z
->policy_output
];
491 ptr
.option
= "forward";
492 ptr
.value
= fw3_flag_names
[z
->policy_forward
];
497 ptr
.value
= z
->masq
? "1" : "0";
501 ptr
.option
= "mtu_fix";
502 ptr
.value
= z
->mtu_fix
? "1" : "0";
506 ptr
.option
= "custom_chains";
507 ptr
.value
= z
->custom_chains
? "1" : "0";
510 if (fam
!= FW3_FAMILY_ANY
)
513 ptr
.option
= "family";
514 ptr
.value
= fw3_flag_names
[fam
];
519 ptr
.option
= "device";
521 fw3_foreach(dev
, &z
->devices
)
529 ep
= buf
+ sizeof(buf
);
532 p
+= snprintf(p
, ep
- p
, "!");
535 p
+= snprintf(p
, ep
- p
, "%s@%s", dev
->name
, dev
->network
);
537 p
+= snprintf(p
, ep
- p
, "%s", dev
->name
);
540 uci_add_list(ctx
, &ptr
);
544 ptr
.option
= "subnet";
546 fw3_foreach(sub
, &z
->subnets
)
551 ptr
.value
= fw3_address_to_string(sub
, true, false);
552 uci_add_list(ctx
, &ptr
);
556 ptr
.option
= "__addrs";
558 fw3_foreach(dev
, &z
->devices
)
563 for (ifa
= ifaddr
; ifa
; ifa
= ifa
->ifa_next
)
565 if (!ifa
->ifa_addr
|| strcmp(dev
->name
, ifa
->ifa_name
))
568 if (ifa
->ifa_addr
->sa_family
== AF_INET
)
570 &((struct sockaddr_in
*)ifa
->ifa_addr
)->sin_addr
,
572 else if (ifa
->ifa_addr
->sa_family
== AF_INET6
)
574 &((struct sockaddr_in6
*)ifa
->ifa_addr
)->sin6_addr
,
580 uci_add_list(ctx
, &ptr
);
587 ptr
.option
= "extra_src";
588 ptr
.value
= z
->extra_src
;
595 ptr
.option
= "extra_dest";
596 ptr
.value
= z
->extra_dest
;
600 snprintf(buf
, sizeof(buf
), "0x%x", z
->flags
[0]);
602 ptr
.option
= "__flags_v4";
606 snprintf(buf
, sizeof(buf
), "0x%x", z
->flags
[1]);
608 ptr
.option
= "__flags_v6";
614 write_ipset_uci(struct uci_context
*ctx
, struct fw3_ipset
*s
,
615 struct uci_package
*dest
)
617 struct fw3_ipset_datatype
*type
;
619 char buf
[sizeof("65535-65535")];
621 struct uci_ptr ptr
= { .p
= dest
};
623 if (!s
->enabled
|| s
->external
)
626 uci_add_section(ctx
, dest
, "ipset", &ptr
.s
);
634 ptr
.option
= "family";
635 if (s
->family
== FW3_FAMILY_V4
)
642 ptr
.option
= "storage";
643 ptr
.value
= fw3_ipset_method_names
[s
->method
];
646 list_for_each_entry(type
, &s
->datatypes
, list
)
648 snprintf(buf
, sizeof(buf
), "%s_%s", type
->dir
, fw3_ipset_type_names
[type
->type
]);
650 ptr
.option
= "match";
652 uci_add_list(ctx
, &ptr
);
658 ptr
.option
= "iprange";
659 ptr
.value
= fw3_address_to_string(&s
->iprange
, false, false);
663 if (s
->portrange
.set
)
665 snprintf(buf
, sizeof(buf
), "%u-%u", s
->portrange
.port_min
, s
->portrange
.port_max
);
667 ptr
.option
= "portrange";
674 fw3_write_statefile(void *state
)
677 struct fw3_state
*s
= state
;
680 struct ifaddrs
*ifaddr
;
682 struct uci_package
*p
;
684 if (fw3_no_family(s
->defaults
.flags
[0]) &&
685 fw3_no_family(s
->defaults
.flags
[1]))
687 unlink(FW3_STATEFILE
);
691 sf
= fopen(FW3_STATEFILE
, "w+");
695 warn("Cannot create state %s: %s", FW3_STATEFILE
, strerror(errno
));
699 if (getifaddrs(&ifaddr
))
701 warn("Cannot get interface addresses: %s", strerror(errno
));
705 if ((p
= uci_lookup_package(s
->uci
, "fw3_state")) != NULL
)
706 uci_unload(s
->uci
, p
);
708 uci_import(s
->uci
, sf
, "fw3_state", NULL
, true);
710 if ((p
= uci_lookup_package(s
->uci
, "fw3_state")) != NULL
)
712 write_defaults_uci(s
->uci
, &s
->defaults
, p
);
714 list_for_each_entry(z
, &s
->zones
, list
)
715 write_zone_uci(s
->uci
, z
, p
, ifaddr
);
717 list_for_each_entry(i
, &s
->ipsets
, list
)
718 write_ipset_uci(s
->uci
, i
, p
);
720 uci_export(s
->uci
, sf
, p
, true);
721 uci_unload(s
->uci
, p
);
734 fw3_free_object(void *obj
, const void *opts
)
736 const struct fw3_option
*ol
;
737 struct list_head
*list
, *cur
, *tmp
;
739 for (ol
= opts
; ol
->name
; ol
++)
744 list
= (struct list_head
*)((char *)obj
+ ol
->offset
);
745 list_for_each_safe(cur
, tmp
, list
)
756 fw3_free_list(struct list_head
*head
)
758 struct list_head
*entry
, *tmp
;
763 list_for_each_safe(entry
, tmp
, head
)
773 fw3_hotplug(bool add
, void *zone
, void *device
)
775 struct fw3_zone
*z
= zone
;
776 struct fw3_device
*d
= device
;
784 warn("Unable to fork(): %s\n", strerror(errno
));
800 setenv("ACTION", add
? "add" : "remove", 1);
801 setenv("ZONE", z
->name
, 1);
802 setenv("INTERFACE", d
->network
, 1);
803 setenv("DEVICE", d
->name
, 1);
805 execl(FW3_HOTPLUG
, FW3_HOTPLUG
, "firewall", NULL
);
812 fw3_netmask2bitlen(int family
, void *mask
)
818 if (family
== FW3_FAMILY_V6
)
819 for (bits
= 0, v6
= mask
;
820 bits
< 128 && (v6
->s6_addr
[bits
/ 8] << (bits
% 8)) & 128;
823 for (bits
= 0, v4
= mask
;
824 bits
< 32 && (ntohl(v4
->s_addr
) << bits
) & 0x80000000;
831 fw3_bitlen2netmask(int family
, int bits
, void *mask
)
838 if (family
== FW3_FAMILY_V6
)
840 if (bits
< -128 || bits
> 128)
846 for (i
= 0; i
< sizeof(v6
->s6_addr
); i
++)
848 b
= (rem
> 8) ? 8 : rem
;
849 v6
->s6_addr
[i
] = (uint8_t)(0xFF << (8 - b
));
854 for (i
= 0; i
< sizeof(v6
->s6_addr
); i
++)
855 v6
->s6_addr
[i
] = ~v6
->s6_addr
[i
];
859 if (bits
< -32 || bits
> 32)
863 v4
->s_addr
= bits
? htonl(~((1 << (32 - abs(bits
))) - 1)) : 0;
866 v4
->s_addr
= ~v4
->s_addr
;
873 fw3_flush_conntrack(void *state
)
876 struct fw3_state
*s
= state
;
877 struct fw3_address
*addr
;
878 struct fw3_device
*dev
;
879 struct fw3_zone
*zone
;
880 struct ifaddrs
*ifaddr
, *ifa
;
881 struct sockaddr_in
*sin
;
882 struct sockaddr_in6
*sin6
;
883 char buf
[INET6_ADDRSTRLEN
];
888 if ((ct
= fopen("/proc/net/nf_conntrack", "w")) != NULL
)
890 info(" * Flushing conntrack table ...");
892 fwrite("f\n", 1, 2, ct
);
899 if (getifaddrs(&ifaddr
))
901 warn("Cannot get interface addresses: %s", strerror(errno
));
905 if ((ct
= fopen("/proc/net/nf_conntrack", "w")) != NULL
)
907 list_for_each_entry(zone
, &s
->zones
, list
)
908 list_for_each_entry(addr
, &zone
->old_addrs
, list
)
912 list_for_each_entry(dev
, &zone
->devices
, list
)
914 for (ifa
= ifaddr
; ifa
&& !found
; ifa
= ifa
->ifa_next
)
916 if (!ifa
->ifa_addr
|| strcmp(dev
->name
, ifa
->ifa_name
))
919 sin
= (struct sockaddr_in
*)ifa
->ifa_addr
;
920 sin6
= (struct sockaddr_in6
*)ifa
->ifa_addr
;
922 if (addr
->family
== FW3_FAMILY_V4
&&
923 sin
->sin_family
== AF_INET
)
925 found
= !memcmp(&addr
->address
.v4
, &sin
->sin_addr
,
926 sizeof(sin
->sin_addr
));
928 else if (addr
->family
== FW3_FAMILY_V6
&&
929 sin6
->sin6_family
== AF_INET6
)
931 found
= !memcmp(&addr
->address
.v6
, &sin6
->sin6_addr
,
932 sizeof(sin6
->sin6_addr
));
942 inet_ntop(addr
->family
== FW3_FAMILY_V4
? AF_INET
: AF_INET6
,
943 &addr
->address
.v4
, buf
, sizeof(buf
));
945 info(" * Flushing conntrack: %s", buf
);
946 fprintf(ct
, "%s\n", buf
);
956 bool fw3_attr_parse_name_type(struct blob_attr
*entry
, const char **name
, const char **type
)
958 struct blob_attr
*opt
;
966 blobmsg_for_each_attr(opt
, entry
, orem
)
967 if (!strcmp(blobmsg_name(opt
), "type"))
968 *type
= blobmsg_get_string(opt
);
969 else if (!strcmp(blobmsg_name(opt
), "name"))
970 *name
= blobmsg_get_string(opt
);
972 return *type
!= NULL
? true : false;
976 fw3_protoname(void *proto
)
978 static char buf
[sizeof("4294967295")];
979 struct fw3_protocol
*p
= proto
;
985 pe
= getprotobynumber(p
->protocol
);
989 snprintf(buf
, sizeof(buf
), "%u", p
->protocol
);
997 fw3_check_loopback_dev(const char *name
)
1003 s
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
1008 memset(&ifr
, 0, sizeof(ifr
));
1009 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s", name
);
1011 if (ioctl(s
, SIOCGIFFLAGS
, &ifr
) >= 0) {
1012 if (ifr
.ifr_flags
& IFF_LOOPBACK
)
1022 fw3_check_loopback_addr(struct fw3_address
*addr
)
1024 if (addr
->family
== FW3_FAMILY_V4
&&
1025 (ntohl(addr
->address
.v4
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
)
1028 if (addr
->family
== FW3_FAMILY_V6
&& !addr
->range
&&
1029 fw3_netmask2bitlen(FW3_FAMILY_V6
, &addr
->mask
.v6
) == 128 &&
1030 IN6_IS_ADDR_LOOPBACK(&addr
->address
.v6
))