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