c93c0aff9033dd0a31e8278007523d38a7c9e42b
[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 <netinet/in.h>
28
29 #if 0
30 #include "fwd_addr.h"
31 #include "fwd_rules.h"
32 #include "fwd_config.h"
33 #endif
34
35 enum fwd_policy {
36         FWD_P_UNSPEC = 0,
37         FWD_P_DROP   = 1,
38         FWD_P_REJECT = 2,
39         FWD_P_ACCEPT = 3        
40 };
41
42 enum fwd_stype {
43         FWD_S_DEFAULTS = 0,
44         FWD_S_ZONE     = 1,
45         FWD_S_FORWARD  = 2,
46         FWD_S_REDIRECT = 3,
47         FWD_S_RULE     = 4,
48         FWD_S_INCLUDE  = 5
49 };
50
51 enum fwd_ptype {
52         FWD_PR_CUSTOM  = 0,
53         FWD_PR_TCP     = 1,
54         FWD_PR_UDP     = 2,
55         FWD_PR_TCPUDP  = 3,
56         FWD_PR_ICMP    = 4,
57         FWD_PR_ALL     = 5
58 };
59
60 struct fwd_portrange {
61         unsigned short min;
62         unsigned short max;
63 };
64
65 struct fwd_cidr {
66         struct in_addr addr;
67         int prefix;
68 };
69
70 struct fwd_mac {
71         unsigned char mac[6];
72 };
73
74 struct fwd_proto {
75         enum fwd_ptype type;
76         int proto;
77 };
78
79 struct fwd_icmptype {
80         char name[32];
81         int type;
82         int code;
83 };
84
85 struct fwd_network_list {
86         char *name;
87         char *ifname;
88         int isalias;
89         struct fwd_cidr *addr;
90         struct fwd_network_list *next;
91 };
92
93 struct fwd_defaults {
94         enum fwd_policy input;
95         enum fwd_policy forward;
96         enum fwd_policy output;
97         int syn_flood;
98         int syn_rate;
99         int syn_burst;
100         int drop_invalid;       
101 };
102
103 struct fwd_zone {
104         char *name;
105         struct fwd_network_list *networks;
106         enum fwd_policy input;
107         enum fwd_policy forward;
108         enum fwd_policy output;
109         int masq;
110         int mtu_fix;
111         int conntrack;
112 };
113
114 struct fwd_forwarding {
115         struct fwd_zone *src;
116         struct fwd_zone *dest;
117         int mtu_fix;  /* legacy */
118         int masq;     /* new */
119 };
120
121 struct fwd_redirect {
122         struct fwd_zone      *src;
123         struct fwd_cidr      *src_ip;
124         struct fwd_mac       *src_mac;
125         struct fwd_portrange *src_port;
126         struct fwd_portrange *src_dport;
127         struct fwd_cidr      *dest_ip;
128         struct fwd_portrange *dest_port;
129         struct fwd_proto     *proto;
130 };
131
132 struct fwd_rule {
133         struct fwd_zone      *src;
134         struct fwd_zone      *dest;
135         struct fwd_cidr      *src_ip;
136         struct fwd_mac       *src_mac;
137         struct fwd_portrange *src_port;
138         struct fwd_cidr      *dest_ip;
139         struct fwd_portrange *dest_port;
140         struct fwd_proto     *proto;
141         struct fwd_icmptype  *icmp_type;
142         enum fwd_policy target;
143 };
144
145 struct fwd_include {
146         char *path;
147 };
148
149 struct fwd_data {
150         enum fwd_stype type;
151         struct fwd_data *next;
152         union {
153                 struct fwd_defaults   defaults;
154                 struct fwd_zone       zone;
155                 struct fwd_forwarding forwarding;
156                 struct fwd_redirect   redirect;
157                 struct fwd_rule       rule;
158                 struct fwd_include    include;
159         } section;
160 };
161
162
163 struct fwd_handle {
164         int rtnl_socket;
165         struct fwd_data *conf;
166         struct fwd_addr_list *addrs;
167 };
168
169
170 /* fwd_zmalloc(size_t)
171  * Allocates a zeroed buffer of the given size. */
172 static void * fwd_zmalloc(size_t s)
173 {
174         void *b = malloc(s);
175
176         if( b != NULL )
177                 memset(b, 0, s);
178
179         return b;
180 }
181
182 /* fwd_fatal(fmt, ...)
183  * Prints message to stderr and termintes program. */
184 #define fwd_fatal(...) do {       \
185         fprintf(stderr, "ERROR: ");   \
186         fprintf(stderr, __VA_ARGS__); \
187         fprintf(stderr, "\n");        \
188         exit(1);                      \
189 } while(0)
190
191 /* fwd_alloc_ptr(type)
192  * Allocates a buffer with the size of the given datatype
193  * and returns a pointer to it. */
194 #define fwd_alloc_ptr(t) (t *) fwd_zmalloc(sizeof(t))
195
196 /* fwd_free_ptr(void *)
197  * Frees the given pointer and sets it to NULL.
198  * Safe for NULL values. */
199 #define fwd_free_ptr(x) do { if(x != NULL) free(x); x = NULL; } while(0)
200
201 #endif