Track used networks and devices in state file
[project/firewall3.git] / utils.h
1 /*
2  * firewall3 - 3rd OpenWrt UCI firewall implementation
3  *
4  *   Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
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 #ifndef __FW3_UTILS_H
20 #define __FW3_UTILS_H
21
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
30 #include <sys/file.h>
31
32 #include <libubox/list.h>
33 #include <uci.h>
34
35
36 #define FW3_STATEFILE   "/var/run/fw3.state"
37 #define FW3_LOCKFILE    "/var/run/fw3.lock"
38
39 extern bool fw3_pr_debug;
40
41 void warn_elem(struct uci_element *e, const char *format, ...);
42 void warn(const char *format, ...);
43 void error(const char *format, ...);
44 void info(const char *format, ...);
45
46 #define setbit(field, flag) field |= (1 << (flag))
47 #define delbit(field, flag) field &= ~(1 << (flag))
48 #define hasbit(field, flag) (field & (1 << (flag)))
49
50 #define set(field, family, flag) setbit(field[family == FW3_FAMILY_V6], flag)
51 #define del(field, family, flag) delbit(field[family == FW3_FAMILY_V6], flag)
52 #define has(field, family, flag) hasbit(field[family == FW3_FAMILY_V6], flag)
53
54 #define fw3_foreach(p, h)                                                  \
55         for (p = list_empty(h) ? NULL : list_first_entry(h, typeof(*p), list); \
56          list_empty(h) ? (p == NULL) : (&p->list != (h));                  \
57              p = list_empty(h) ? list_first_entry(h, typeof(*p), list)         \
58                            : list_entry(p->list.next, typeof(*p), list))
59
60 #define fw3_is_family(p, f)                                                \
61         (!p || (p)->family == FW3_FAMILY_ANY || (p)->family == f)
62
63 #define fw3_no_family(flags)                                               \
64         (!(flags & ((1 << FW3_FAMILY_V4) | (1 << FW3_FAMILY_V6))))
65
66 #define fw3_no_table(flags)                                                \
67     (!(flags & ((1<<FW3_TABLE_FILTER)|(1<<FW3_TABLE_NAT)|                  \
68                 (1<<FW3_TABLE_MANGLE)|(1<<FW3_TABLE_RAW))))
69
70
71 const char * fw3_find_command(const char *cmd);
72
73 bool fw3_stdout_pipe(void);
74 bool __fw3_command_pipe(bool silent, const char *command, ...);
75 #define fw3_command_pipe(...) __fw3_command_pipe(__VA_ARGS__, NULL)
76
77 void fw3_command_close(void);
78 void fw3_pr(const char *fmt, ...);
79
80 bool fw3_has_table(bool ipv6, const char *table);
81
82 bool fw3_lock(void);
83 void fw3_unlock(void);
84
85
86 enum fw3_statefile_type
87 {
88         FW3_TYPE_DEFAULTS = 0,
89         FW3_TYPE_ZONE     = 1,
90         FW3_TYPE_NETWORK  = 2,
91         FW3_TYPE_IPSET    = 3,
92 };
93
94 bool fw3_read_statefile(void *state);
95 void fw3_write_statefile(void *state);
96
97 void fw3_set_running(void *object, struct list_head *dest);
98
99 void fw3_free_object(void *obj, const void *opts);
100
101
102 struct fw3_rule_spec {
103         int family;
104         int table;
105         int flag;
106         const char *format;
107 };
108
109 bool fw3_pr_rulespec(int table, int family, uint32_t *flags, uint32_t mask,
110                      const struct fw3_rule_spec *r, const char *fmt, ...);
111
112 #endif