2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
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.
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.
19 #ifndef __FW3_IPTABLES_H
20 #define __FW3_IPTABLES_H
22 #include <libiptc/libiptc.h>
23 #include <libiptc/libip6tc.h>
29 #include <sys/utsname.h>
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"
39 # error "Unsupported xtables version"
42 /* libext.a interface */
43 #define FW3_IPT_MODULES \
44 __ipt_module(comment) \
45 __ipt_module(conntrack) \
49 __ipt_module(connlimit) \
52 __ipt_module(multiport) \
54 __ipt_module(standard) \
62 __ipt_module(MASQUERADE) \
63 __ipt_module(REDIRECT) \
64 __ipt_module(REJECT) \
71 #define __ipt_module(x) \
72 extern void libxt_##x##_init(void) __attribute__((weak)); \
73 extern void libipt_##x##_init(void) __attribute__((weak));
76 #define __ipt_module(x) \
77 extern void libxt_##x##_init(void) __attribute__((weak)); \
78 extern void libipt_##x##_init(void) __attribute__((weak)); \
79 extern void libip6t_##x##_init(void) __attribute__((weak));
85 /* Required by certain extensions like SNAT and DNAT */
86 extern int kernel_version;
87 void get_kernel_version(void);
89 struct fw3_ipt_handle {
90 enum fw3_family family;
99 struct fw3_ipt_handle *h;
103 struct ip6t_entry e6;
106 struct xtables_rule_match *matches;
107 struct xtables_target *target;
113 bool protocol_loaded;
116 struct fw3_ipt_handle *fw3_ipt_open(enum fw3_family family,
117 enum fw3_table table);
119 void fw3_ipt_set_policy(struct fw3_ipt_handle *h, const char *chain,
120 enum fw3_flag policy);
123 void fw3_ipt_flush_chain(struct fw3_ipt_handle *h, const char *chain);
124 void fw3_ipt_delete_chain(struct fw3_ipt_handle *h, const char *chain);
126 void fw3_ipt_create_chain(struct fw3_ipt_handle *h, const char *fmt, ...);
128 void fw3_ipt_flush(struct fw3_ipt_handle *h);
130 void fw3_ipt_commit(struct fw3_ipt_handle *h);
132 void fw3_ipt_close(struct fw3_ipt_handle *h);
134 struct fw3_ipt_rule *fw3_ipt_rule_new(struct fw3_ipt_handle *h);
136 void fw3_ipt_rule_proto(struct fw3_ipt_rule *r, struct fw3_protocol *proto);
138 void fw3_ipt_rule_in_out(struct fw3_ipt_rule *r,
139 struct fw3_device *in, struct fw3_device *out);
141 void fw3_ipt_rule_src_dest(struct fw3_ipt_rule *r,
142 struct fw3_address *src, struct fw3_address *dest);
144 void fw3_ipt_rule_sport_dport(struct fw3_ipt_rule *r,
145 struct fw3_port *sp, struct fw3_port *dp);
147 void fw3_ipt_rule_device(struct fw3_ipt_rule *r, const char *device, bool out);
149 void fw3_ipt_rule_mac(struct fw3_ipt_rule *r, struct fw3_mac *mac);
151 void fw3_ipt_rule_icmptype(struct fw3_ipt_rule *r, struct fw3_icmptype *icmp);
153 void fw3_ipt_rule_limit(struct fw3_ipt_rule *r, struct fw3_limit *limit);
155 void fw3_ipt_rule_ipset(struct fw3_ipt_rule *r, struct fw3_setmatch *match);
157 void fw3_ipt_rule_time(struct fw3_ipt_rule *r, struct fw3_time *time);
159 void fw3_ipt_rule_mark(struct fw3_ipt_rule *r, struct fw3_mark *mark);
161 void fw3_ipt_rule_comment(struct fw3_ipt_rule *r, const char *fmt, ...);
163 void fw3_ipt_rule_extra(struct fw3_ipt_rule *r, const char *extra);
165 void fw3_ipt_rule_addarg(struct fw3_ipt_rule *r, bool inv,
166 const char *k, const char *v);
168 struct fw3_ipt_rule * fw3_ipt_rule_create(struct fw3_ipt_handle *handle,
169 struct fw3_protocol *proto,
170 struct fw3_device *in,
171 struct fw3_device *out,
172 struct fw3_address *src,
173 struct fw3_address *dest);
175 void __fw3_ipt_rule_append(struct fw3_ipt_rule *r, bool repl,
176 const char *fmt, ...);
178 #define fw3_ipt_rule_append(rule, ...) \
179 __fw3_ipt_rule_append(rule, false, __VA_ARGS__)
181 #define fw3_ipt_rule_replace(rule, ...) \
182 __fw3_ipt_rule_append(rule, true, __VA_ARGS__)
185 fw3_ipt_rule_target(struct fw3_ipt_rule *r, const char *fmt, ...)
191 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
194 fw3_ipt_rule_addarg(r, false, "-j", buf);