a2c733dce7d0f51230444d238d6343a36fcd85b0
[project/firewall3.git] / iptables.h
1 /*
2  * firewall3 - 3rd OpenWrt UCI firewall implementation
3  *
4  *   Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
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 #define _GNU_SOURCE /* RTLD_NEXT */
23
24 #include <libiptc/libiptc.h>
25 #include <libiptc/libip6tc.h>
26 #include <xtables.h>
27
28 #include <dlfcn.h>
29 #include <unistd.h>
30 #include <getopt.h>
31 #include <sys/utsname.h>
32
33 #include "options.h"
34
35 /* xtables interface */
36 #if (XTABLES_VERSION_CODE == 10 || XTABLES_VERSION_CODE == 11)
37 # include "xtables-10.h"
38 #elif (XTABLES_VERSION_CODE == 5)
39 # include "xtables-5.h"
40 #else
41 # error "Unsupported xtables version"
42 #endif
43
44 #ifndef DISABLE_STATIC_EXTENSIONS
45 /* libipt*ext.so interfaces */
46 extern void init_extensions(void);
47 extern void init_extensions4(void);
48 extern void init_extensions6(void);
49 #else
50 static inline void init_extensions(void) { }
51 static inline void init_extensions4(void) { }
52 static inline void init_extensions6(void) { }
53 #endif
54
55 /* Required by certain extensions like SNAT and DNAT */
56 extern int kernel_version;
57 void get_kernel_version(void);
58
59 struct fw3_ipt_handle {
60         enum fw3_family family;
61         enum fw3_table table;
62         void *handle;
63 };
64
65 struct fw3_ipt_rule {
66         struct fw3_ipt_handle *h;
67
68         union {
69                 struct ipt_entry e;
70                 struct ip6t_entry e6;
71         };
72
73         struct xtables_rule_match *matches;
74         struct xtables_target *target;
75
76         int argc;
77         char **argv;
78
79         uint32_t protocol;
80         bool protocol_loaded;
81 };
82
83 struct fw3_ipt_handle *fw3_ipt_open(enum fw3_family family,
84                                     enum fw3_table table);
85
86 void fw3_ipt_set_policy(struct fw3_ipt_handle *h, const char *chain,
87                         enum fw3_flag policy);
88
89
90 void fw3_ipt_flush_chain(struct fw3_ipt_handle *h, const char *chain);
91 void fw3_ipt_delete_chain(struct fw3_ipt_handle *h, const char *chain);
92
93 void fw3_ipt_delete_id_rules(struct fw3_ipt_handle *h, const char *chain);
94
95 void fw3_ipt_create_chain(struct fw3_ipt_handle *h, const char *fmt, ...);
96
97 void fw3_ipt_flush(struct fw3_ipt_handle *h);
98
99 void fw3_ipt_gc(struct fw3_ipt_handle *h);
100
101 void fw3_ipt_commit(struct fw3_ipt_handle *h);
102
103 void fw3_ipt_close(struct fw3_ipt_handle *h);
104
105 struct fw3_ipt_rule *fw3_ipt_rule_new(struct fw3_ipt_handle *h);
106
107 void fw3_ipt_rule_proto(struct fw3_ipt_rule *r, struct fw3_protocol *proto);
108
109 void fw3_ipt_rule_in_out(struct fw3_ipt_rule *r,
110                          struct fw3_device *in, struct fw3_device *out);
111
112 void fw3_ipt_rule_src_dest(struct fw3_ipt_rule *r,
113                            struct fw3_address *src, struct fw3_address *dest);
114
115 void fw3_ipt_rule_sport_dport(struct fw3_ipt_rule *r,
116                               struct fw3_port *sp, struct fw3_port *dp);
117
118 void fw3_ipt_rule_device(struct fw3_ipt_rule *r, const char *device, bool out);
119
120 void fw3_ipt_rule_mac(struct fw3_ipt_rule *r, struct fw3_mac *mac);
121
122 void fw3_ipt_rule_icmptype(struct fw3_ipt_rule *r, struct fw3_icmptype *icmp);
123
124 void fw3_ipt_rule_limit(struct fw3_ipt_rule *r, struct fw3_limit *limit);
125
126 void fw3_ipt_rule_ipset(struct fw3_ipt_rule *r, struct fw3_setmatch *match);
127
128 void fw3_ipt_rule_time(struct fw3_ipt_rule *r, struct fw3_time *time);
129
130 void fw3_ipt_rule_mark(struct fw3_ipt_rule *r, struct fw3_mark *mark);
131
132 void fw3_ipt_rule_comment(struct fw3_ipt_rule *r, const char *fmt, ...);
133
134 void fw3_ipt_rule_extra(struct fw3_ipt_rule *r, const char *extra);
135
136 void fw3_ipt_rule_addarg(struct fw3_ipt_rule *r, bool inv,
137                          const char *k, const char *v);
138
139 struct fw3_ipt_rule * fw3_ipt_rule_create(struct fw3_ipt_handle *handle,
140                                           struct fw3_protocol *proto,
141                                           struct fw3_device *in,
142                                           struct fw3_device *out,
143                                           struct fw3_address *src,
144                                           struct fw3_address *dest);
145
146 void __fw3_ipt_rule_append(struct fw3_ipt_rule *r, bool repl,
147                            const char *fmt, ...);
148
149 #define fw3_ipt_rule_append(rule, ...) \
150         __fw3_ipt_rule_append(rule, false, __VA_ARGS__)
151
152 #define fw3_ipt_rule_replace(rule, ...) \
153         __fw3_ipt_rule_append(rule, true, __VA_ARGS__)
154
155 static inline void
156 fw3_ipt_rule_target(struct fw3_ipt_rule *r, const char *fmt, ...)
157 {
158         va_list ap;
159         char buf[32];
160
161         va_start(ap, fmt);
162         vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
163         va_end(ap);
164
165         fw3_ipt_rule_addarg(r, false, "-j", buf);
166 }
167
168 void xtables_register_match(struct xtables_match *me);
169 void xtables_register_target(struct xtables_target *me);
170
171 #endif