contrib/fwd: rewrite rule generate to use xtables api
[project/luci.git] / contrib / fwd / src / fwd.h
1 /*
2  * fwd - OpenWrt firewall daemon - data structures
3  *
4  *   Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
5  *
6  * The fwd program is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation.
9  *
10  * The fwd program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with the fwd program. If not, see http://www.gnu.org/licenses/.
17  */
18
19 #ifndef __FWD_H__
20 #define __FWD_H__
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <getopt.h>
28 #include <netinet/in.h>
29
30 #if 0
31 #include "fwd_addr.h"
32 #include "fwd_rules.h"
33 #include "fwd_config.h"
34 #endif
35
36 enum fwd_policy {
37         FWD_P_UNSPEC = 0,
38         FWD_P_DROP   = 1,
39         FWD_P_REJECT = 2,
40         FWD_P_ACCEPT = 3
41 };
42
43 enum fwd_stype {
44         FWD_S_DEFAULTS = 0,
45         FWD_S_ZONE     = 1,
46         FWD_S_FORWARD  = 2,
47         FWD_S_REDIRECT = 3,
48         FWD_S_RULE     = 4,
49         FWD_S_INCLUDE  = 5
50 };
51
52 enum fwd_ptype {
53         FWD_PR_CUSTOM  = 0,
54         FWD_PR_TCP     = 1,
55         FWD_PR_UDP     = 2,
56         FWD_PR_TCPUDP  = 3,
57         FWD_PR_ICMP    = 4,
58         FWD_PR_ALL     = 5
59 };
60
61 struct fwd_portrange {
62         unsigned short min;
63         unsigned short max;
64 };
65
66 struct fwd_cidr {
67         struct in_addr addr;
68         int prefix;
69 };
70
71 struct fwd_mac {
72         unsigned char mac[6];
73 };
74
75 struct fwd_proto {
76         enum fwd_ptype type;
77         int proto;
78 };
79
80 struct fwd_icmptype {
81         char name[32];
82         int type;
83         int code;
84 };
85
86 struct fwd_network_list {
87         char *name;
88         char *ifname;
89         int isalias;
90         struct fwd_cidr *addr;
91         struct fwd_network_list *next;
92 };
93
94 struct fwd_defaults {
95         enum fwd_policy input;
96         enum fwd_policy forward;
97         enum fwd_policy output;
98         int syn_flood;
99         int syn_rate;
100         int syn_burst;
101         int drop_invalid;       
102 };
103
104 struct fwd_zone {
105         char *name;
106         struct fwd_network_list *networks;
107         struct fwd_data *forwardings;
108         struct fwd_data *redirects;
109         struct fwd_data *rules;
110         enum fwd_policy input;
111         enum fwd_policy forward;
112         enum fwd_policy output;
113         int masq;
114         int mtu_fix;
115         int conntrack;
116 };
117
118 struct fwd_forwarding {
119         struct fwd_zone *src;
120         struct fwd_zone *dest;
121         int mtu_fix;  /* legacy */
122         int masq;     /* new */
123 };
124
125 struct fwd_redirect {
126         struct fwd_zone      *src;
127         struct fwd_cidr      *src_ip;
128         struct fwd_mac       *src_mac;
129         struct fwd_portrange *src_port;
130         struct fwd_portrange *src_dport;
131         struct fwd_cidr      *dest_ip;
132         struct fwd_portrange *dest_port;
133         struct fwd_proto     *proto;
134         int clone; /* true if rule is cloned (tcpudp -> tcp + udp) */
135 };
136
137 struct fwd_rule {
138         struct fwd_zone      *src;
139         struct fwd_zone      *dest;
140         struct fwd_cidr      *src_ip;
141         struct fwd_mac       *src_mac;
142         struct fwd_portrange *src_port;
143         struct fwd_cidr      *dest_ip;
144         struct fwd_portrange *dest_port;
145         struct fwd_proto     *proto;
146         struct fwd_icmptype  *icmp_type;
147         enum fwd_policy target;
148         int clone; /* true if rule is cloned (tcpudp -> tcp + udp) */
149 };
150
151 struct fwd_include {
152         char *path;
153 };
154
155 struct fwd_data {
156         enum fwd_stype type;
157         struct fwd_data *next;
158         union {
159                 struct fwd_defaults   defaults;
160                 struct fwd_zone       zone;
161                 struct fwd_forwarding forwarding;
162                 struct fwd_redirect   redirect;
163                 struct fwd_rule       rule;
164                 struct fwd_include    include;
165         } section;
166 };
167
168
169 struct fwd_handle {
170         int rtnl_socket;
171         struct fwd_data *conf;
172         struct fwd_addr_list *addrs;
173 };
174
175
176 /* fwd_zmalloc(size_t)
177  * Allocates a zeroed buffer of the given size. */
178 static void * fwd_zmalloc(size_t s)
179 {
180         void *b = malloc(s);
181
182         if( b != NULL )
183                 memset(b, 0, s);
184
185         return b;
186 }
187
188 /* fwd_fatal(fmt, ...)
189  * Prints message to stderr and termintes program. */
190 #define fwd_fatal(...) do {       \
191         fprintf(stderr, "ERROR: ");   \
192         fprintf(stderr, __VA_ARGS__); \
193         fprintf(stderr, "\n");        \
194         exit(1);                      \
195 } while(0)
196
197 /* fwd_alloc_ptr(type)
198  * Allocates a buffer with the size of the given datatype
199  * and returns a pointer to it. */
200 #define fwd_alloc_ptr(t) (t *) fwd_zmalloc(sizeof(t))
201
202 /* fwd_free_ptr(void *)
203  * Frees the given pointer and sets it to NULL.
204  * Safe for NULL values. */
205 #define fwd_free_ptr(x) do { if(x != NULL) free(x); x = NULL; } while(0)
206
207 #endif