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