Fix processing of negated options
[project/firewall3.git] / iptables.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_IPTABLES_H
20 #define __FW3_IPTABLES_H
21
22 #include <libiptc/libiptc.h>
23 #include <libiptc/libip6tc.h>
24 #include <xtables.h>
25
26 #include <dlfcn.h>
27 #include <unistd.h>
28 #include <getopt.h>
29 #include <sys/utsname.h>
30
31 #include "options.h"
32
33 /* xtables interface */
34 #if (XTABLES_VERSION_CODE == 10)
35 # include "xtables-10.h"
36 #elif (XTABLES_VERSION_CODE == 5)
37 # include "xtables-5.h"
38 #else
39 # error "Unsupported xtables version"
40 #endif
41
42 /* libext.a interface */
43 #define FW3_IPT_MODULES                 \
44         __ipt_module(comment)           \
45         __ipt_module(conntrack)         \
46         __ipt_module(icmp)                      \
47         __ipt_module(icmp6)                     \
48         __ipt_module(limit)                     \
49         __ipt_module(mac)                       \
50         __ipt_module(mark)                      \
51         __ipt_module(set)                       \
52         __ipt_module(standard)          \
53         __ipt_module(tcp)                       \
54         __ipt_module(time)                      \
55         __ipt_module(udp)                       \
56         __ipt_module(CT)                        \
57         __ipt_module(DNAT)                      \
58         __ipt_module(LOG)                       \
59         __ipt_module(MARK)                      \
60         __ipt_module(MASQUERADE)        \
61         __ipt_module(REDIRECT)          \
62         __ipt_module(REJECT)            \
63         __ipt_module(SET)                       \
64         __ipt_module(SNAT)                      \
65         __ipt_module(TCPMSS)
66
67 #ifdef DISABLE_IPV6
68 #undef __ipt_module
69 #define __ipt_module(x) \
70         extern void libxt_##x##_init(void) __attribute__((weak)); \
71         extern void libipt_##x##_init(void) __attribute__((weak));
72 #else
73 #undef __ipt_module
74 #define __ipt_module(x) \
75         extern void libxt_##x##_init(void) __attribute__((weak)); \
76         extern void libipt_##x##_init(void) __attribute__((weak)); \
77         extern void libip6t_##x##_init(void) __attribute__((weak));
78 #endif
79
80 FW3_IPT_MODULES
81
82
83 /* Required by certain extensions like SNAT and DNAT */
84 extern int kernel_version;
85 void get_kernel_version(void);
86
87 struct fw3_ipt_handle {
88         enum fw3_family family;
89         enum fw3_table table;
90         void *handle;
91
92         int libc;
93         void **libv;
94 };
95
96 struct fw3_ipt_rule {
97         struct fw3_ipt_handle *h;
98
99         union {
100                 struct ipt_entry e;
101                 struct ip6t_entry e6;
102         };
103
104         struct xtables_rule_match *matches;
105         struct xtables_target *target;
106
107         int argc;
108         char **argv;
109
110         uint32_t protocol;
111         bool protocol_loaded;
112 };
113
114 struct fw3_ipt_handle *fw3_ipt_open(enum fw3_family family,
115                                     enum fw3_table table);
116
117 void fw3_ipt_set_policy(struct fw3_ipt_handle *h, const char *chain,
118                         enum fw3_flag policy);
119
120
121 void fw3_ipt_flush_chain(struct fw3_ipt_handle *h, const char *chain);
122 void fw3_ipt_delete_chain(struct fw3_ipt_handle *h, const char *chain);
123
124 void fw3_ipt_create_chain(struct fw3_ipt_handle *h, const char *fmt, ...);
125
126 void fw3_ipt_flush(struct fw3_ipt_handle *h);
127
128 void fw3_ipt_commit(struct fw3_ipt_handle *h);
129
130 void fw3_ipt_close(struct fw3_ipt_handle *h);
131
132 struct fw3_ipt_rule *fw3_ipt_rule_new(struct fw3_ipt_handle *h);
133
134 void fw3_ipt_rule_proto(struct fw3_ipt_rule *r, struct fw3_protocol *proto);
135
136 void fw3_ipt_rule_in_out(struct fw3_ipt_rule *r,
137                          struct fw3_device *in, struct fw3_device *out);
138
139 void fw3_ipt_rule_src_dest(struct fw3_ipt_rule *r,
140                            struct fw3_address *src, struct fw3_address *dest);
141
142 void fw3_ipt_rule_sport_dport(struct fw3_ipt_rule *r,
143                               struct fw3_port *sp, struct fw3_port *dp);
144
145 void fw3_ipt_rule_mac(struct fw3_ipt_rule *r, struct fw3_mac *mac);
146
147 void fw3_ipt_rule_icmptype(struct fw3_ipt_rule *r, struct fw3_icmptype *icmp);
148
149 void fw3_ipt_rule_limit(struct fw3_ipt_rule *r, struct fw3_limit *limit);
150
151 void fw3_ipt_rule_ipset(struct fw3_ipt_rule *r, struct fw3_setmatch *match);
152
153 void fw3_ipt_rule_time(struct fw3_ipt_rule *r, struct fw3_time *time);
154
155 void fw3_ipt_rule_mark(struct fw3_ipt_rule *r, struct fw3_mark *mark);
156
157 void fw3_ipt_rule_comment(struct fw3_ipt_rule *r, const char *fmt, ...);
158
159 void fw3_ipt_rule_extra(struct fw3_ipt_rule *r, const char *extra);
160
161 void fw3_ipt_rule_addarg(struct fw3_ipt_rule *r, bool inv,
162                          const char *k, const char *v);
163
164 struct fw3_ipt_rule * fw3_ipt_rule_create(struct fw3_ipt_handle *handle,
165                                           struct fw3_protocol *proto,
166                                           struct fw3_device *in,
167                                           struct fw3_device *out,
168                                           struct fw3_address *src,
169                                           struct fw3_address *dest);
170
171 void __fw3_ipt_rule_append(struct fw3_ipt_rule *r, bool repl,
172                            const char *fmt, ...);
173
174 #define fw3_ipt_rule_append(rule, ...) \
175         __fw3_ipt_rule_append(rule, false, __VA_ARGS__)
176
177 #define fw3_ipt_rule_replace(rule, ...) \
178         __fw3_ipt_rule_append(rule, true, __VA_ARGS__)
179
180 static inline void
181 fw3_ipt_rule_target(struct fw3_ipt_rule *r, const char *fmt, ...)
182 {
183         va_list ap;
184         char buf[32];
185
186         va_start(ap, fmt);
187         vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
188         va_end(ap);
189
190         fw3_ipt_rule_addarg(r, false, "-j", buf);
191 }
192
193 #endif