473110403c813b40b9d4b306c5752d0a6e478caa
[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
33 extern struct xtables_match *xtables_pending_matches;
34 extern struct xtables_target *xtables_pending_targets;
35
36 /* libext.a interface */
37 void init_extensions(void);
38 void init_extensions4(void);
39 void init_extensions6(void);
40
41 /* Required by certain extensions like SNAT and DNAT */
42 extern int kernel_version;
43 void get_kernel_version(void);
44
45 struct fw3_ipt_handle {
46         enum fw3_family family;
47         enum fw3_table table;
48         struct xtc_handle *handle;
49 };
50
51 struct fw3_ipt_rule {
52         struct fw3_ipt_handle *h;
53
54         union {
55                 struct ipt_entry e;
56                 struct ip6t_entry e6;
57         };
58
59         struct xtables_rule_match *matches;
60         struct xtables_target *target;
61
62         int argc;
63         char **argv;
64
65         uint32_t protocol;
66         bool protocol_loaded;
67 };
68
69 struct fw3_ipt_handle *fw3_ipt_open(enum fw3_family family,
70                                     enum fw3_table table);
71
72 void fw3_ipt_set_policy(struct fw3_ipt_handle *h, const char *chain,
73                         enum fw3_flag policy);
74
75 void fw3_ipt_delete_chain(struct fw3_ipt_handle *h, const char *chain);
76 void fw3_ipt_delete_rules(struct fw3_ipt_handle *h, const char *target);
77
78 void fw3_ipt_create_chain(struct fw3_ipt_handle *h, const char *fmt, ...);
79
80 void fw3_ipt_flush(struct fw3_ipt_handle *h);
81
82 void fw3_ipt_commit(struct fw3_ipt_handle *h);
83
84 struct fw3_ipt_rule *fw3_ipt_rule_new(struct fw3_ipt_handle *h);
85
86 void fw3_ipt_rule_proto(struct fw3_ipt_rule *r, struct fw3_protocol *proto);
87
88 void fw3_ipt_rule_in_out(struct fw3_ipt_rule *r,
89                          struct fw3_device *in, struct fw3_device *out);
90
91 void fw3_ipt_rule_src_dest(struct fw3_ipt_rule *r,
92                            struct fw3_address *src, struct fw3_address *dest);
93
94 void fw3_ipt_rule_sport_dport(struct fw3_ipt_rule *r,
95                               struct fw3_port *sp, struct fw3_port *dp);
96
97 void fw3_ipt_rule_mac(struct fw3_ipt_rule *r, struct fw3_mac *mac);
98
99 void fw3_ipt_rule_icmptype(struct fw3_ipt_rule *r, struct fw3_icmptype *icmp);
100
101 void fw3_ipt_rule_limit(struct fw3_ipt_rule *r, struct fw3_limit *limit);
102
103 void fw3_ipt_rule_ipset(struct fw3_ipt_rule *r, struct fw3_ipset *ipset,
104                         bool invert);
105
106 void fw3_ipt_rule_time(struct fw3_ipt_rule *r, struct fw3_time *time);
107
108 void fw3_ipt_rule_mark(struct fw3_ipt_rule *r, struct fw3_mark *mark);
109
110 void fw3_ipt_rule_comment(struct fw3_ipt_rule *r, const char *fmt, ...);
111
112 void fw3_ipt_rule_extra(struct fw3_ipt_rule *r, const char *extra);
113
114 void fw3_ipt_rule_addarg(struct fw3_ipt_rule *r, bool inv,
115                          const char *k, const char *v);
116
117 struct fw3_ipt_rule * fw3_ipt_rule_create(struct fw3_ipt_handle *handle,
118                                           struct fw3_protocol *proto,
119                                           struct fw3_device *in,
120                                           struct fw3_device *out,
121                                           struct fw3_address *src,
122                                           struct fw3_address *dest);
123
124 void fw3_ipt_rule_append(struct fw3_ipt_rule *r, const char *fmt, ...);
125
126 static inline void
127 fw3_ipt_rule_target(struct fw3_ipt_rule *r, const char *fmt, ...)
128 {
129         va_list ap;
130         char buf[32];
131
132         va_start(ap, fmt);
133         vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
134         va_end(ap);
135
136         fw3_ipt_rule_addarg(r, false, "-j", buf);
137 }
138
139 #endif