Rework match initialization
[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 /* libipt*ext.so interfaces */
43 extern void init_extensions(void);
44 extern void init_extensions4(void);
45 extern void init_extensions6(void);
46
47 /* Required by certain extensions like SNAT and DNAT */
48 extern int kernel_version;
49 void get_kernel_version(void);
50
51 struct fw3_ipt_handle {
52         enum fw3_family family;
53         enum fw3_table table;
54         void *handle;
55
56         int libc;
57         void **libv;
58 };
59
60 struct fw3_ipt_rule {
61         struct fw3_ipt_handle *h;
62
63         union {
64                 struct ipt_entry e;
65                 struct ip6t_entry e6;
66         };
67
68         struct xtables_rule_match *matches;
69         struct xtables_target *target;
70
71         int argc;
72         char **argv;
73
74         uint32_t protocol;
75         bool protocol_loaded;
76 };
77
78 struct fw3_ipt_handle *fw3_ipt_open(enum fw3_family family,
79                                     enum fw3_table table);
80
81 void fw3_ipt_set_policy(struct fw3_ipt_handle *h, const char *chain,
82                         enum fw3_flag policy);
83
84
85 void fw3_ipt_flush_chain(struct fw3_ipt_handle *h, const char *chain);
86 void fw3_ipt_delete_chain(struct fw3_ipt_handle *h, const char *chain);
87
88 void fw3_ipt_create_chain(struct fw3_ipt_handle *h, const char *fmt, ...);
89
90 void fw3_ipt_flush(struct fw3_ipt_handle *h);
91
92 void fw3_ipt_commit(struct fw3_ipt_handle *h);
93
94 void fw3_ipt_close(struct fw3_ipt_handle *h);
95
96 struct fw3_ipt_rule *fw3_ipt_rule_new(struct fw3_ipt_handle *h);
97
98 void fw3_ipt_rule_proto(struct fw3_ipt_rule *r, struct fw3_protocol *proto);
99
100 void fw3_ipt_rule_in_out(struct fw3_ipt_rule *r,
101                          struct fw3_device *in, struct fw3_device *out);
102
103 void fw3_ipt_rule_src_dest(struct fw3_ipt_rule *r,
104                            struct fw3_address *src, struct fw3_address *dest);
105
106 void fw3_ipt_rule_sport_dport(struct fw3_ipt_rule *r,
107                               struct fw3_port *sp, struct fw3_port *dp);
108
109 void fw3_ipt_rule_device(struct fw3_ipt_rule *r, const char *device, bool out);
110
111 void fw3_ipt_rule_mac(struct fw3_ipt_rule *r, struct fw3_mac *mac);
112
113 void fw3_ipt_rule_icmptype(struct fw3_ipt_rule *r, struct fw3_icmptype *icmp);
114
115 void fw3_ipt_rule_limit(struct fw3_ipt_rule *r, struct fw3_limit *limit);
116
117 void fw3_ipt_rule_ipset(struct fw3_ipt_rule *r, struct fw3_setmatch *match);
118
119 void fw3_ipt_rule_time(struct fw3_ipt_rule *r, struct fw3_time *time);
120
121 void fw3_ipt_rule_mark(struct fw3_ipt_rule *r, struct fw3_mark *mark);
122
123 void fw3_ipt_rule_comment(struct fw3_ipt_rule *r, const char *fmt, ...);
124
125 void fw3_ipt_rule_extra(struct fw3_ipt_rule *r, const char *extra);
126
127 void fw3_ipt_rule_addarg(struct fw3_ipt_rule *r, bool inv,
128                          const char *k, const char *v);
129
130 struct fw3_ipt_rule * fw3_ipt_rule_create(struct fw3_ipt_handle *handle,
131                                           struct fw3_protocol *proto,
132                                           struct fw3_device *in,
133                                           struct fw3_device *out,
134                                           struct fw3_address *src,
135                                           struct fw3_address *dest);
136
137 void __fw3_ipt_rule_append(struct fw3_ipt_rule *r, bool repl,
138                            const char *fmt, ...);
139
140 #define fw3_ipt_rule_append(rule, ...) \
141         __fw3_ipt_rule_append(rule, false, __VA_ARGS__)
142
143 #define fw3_ipt_rule_replace(rule, ...) \
144         __fw3_ipt_rule_append(rule, true, __VA_ARGS__)
145
146 static inline void
147 fw3_ipt_rule_target(struct fw3_ipt_rule *r, const char *fmt, ...)
148 {
149         va_list ap;
150         char buf[32];
151
152         va_start(ap, fmt);
153         vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
154         va_end(ap);
155
156         fw3_ipt_rule_addarg(r, false, "-j", buf);
157 }
158
159 #endif