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