introduce support for enabled option in zones, forwards, rules, redirects, ipsets...
[project/firewall3.git] / rules.c
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 #include "rules.h"
20
21
22 const struct fw3_option fw3_rule_opts[] = {
23 FW3_OPT("enabled", bool, rule, enabled),
24
25 FW3_OPT("name", string, rule, name),
26 FW3_OPT("family", family, rule, family),
27
28 FW3_OPT("src", device, rule, src),
29 FW3_OPT("dest", device, rule, dest),
30
31 FW3_OPT("ipset", device, rule, ipset),
32
33 FW3_LIST("proto", protocol, rule, proto),
34
35 FW3_LIST("src_ip", address, rule, ip_src),
36 FW3_LIST("src_mac", mac, rule, mac_src),
37 FW3_LIST("src_port", port, rule, port_src),
38
39 FW3_LIST("dest_ip", address, rule, ip_dest),
40 FW3_LIST("dest_port", port, rule, port_dest),
41
42 FW3_LIST("icmp_type", icmptype, rule, icmp_type),
43 FW3_OPT("extra", string, rule, extra),
44
45 FW3_OPT("limit", limit, rule, limit),
46 FW3_OPT("limit_burst", int, rule, limit.burst),
47
48 FW3_OPT("utc_time", bool, rule, time.utc),
49 FW3_OPT("start_date", date, rule, time.datestart),
50 FW3_OPT("stop_date", date, rule, time.datestop),
51 FW3_OPT("start_time", time, rule, time.timestart),
52 FW3_OPT("stop_time", time, rule, time.timestop),
53 FW3_OPT("weekdays", weekdays, rule, time.weekdays),
54 FW3_OPT("monthdays", monthdays, rule, time.monthdays),
55
56 FW3_OPT("target", target, rule, target),
57
58 { }
59 };
60
61
62 void
63 fw3_load_rules(struct fw3_state *state, struct uci_package *p)
64 {
65 struct uci_section *s;
66 struct uci_element *e;
67 struct fw3_rule *rule;
68
69 INIT_LIST_HEAD(&state->rules);
70
71 uci_foreach_element(&p->sections, e)
72 {
73 s = uci_to_section(e);
74
75 if (strcmp(s->type, "rule"))
76 continue;
77
78 rule = malloc(sizeof(*rule));
79
80 if (!rule)
81 continue;
82
83 memset(rule, 0, sizeof(*rule));
84
85 INIT_LIST_HEAD(&rule->proto);
86
87 INIT_LIST_HEAD(&rule->ip_src);
88 INIT_LIST_HEAD(&rule->mac_src);
89 INIT_LIST_HEAD(&rule->port_src);
90
91 INIT_LIST_HEAD(&rule->ip_dest);
92 INIT_LIST_HEAD(&rule->port_dest);
93
94 INIT_LIST_HEAD(&rule->icmp_type);
95
96 rule->enabled = true;
97
98 fw3_parse_options(rule, fw3_rule_opts, s);
99
100 if (!rule->enabled)
101 {
102 fw3_free_rule(rule);
103 continue;
104 }
105
106 if (rule->src.invert || rule->dest.invert)
107 {
108 warn_elem(e, "must not have inverted 'src' or 'dest' options");
109 fw3_free_rule(rule);
110 continue;
111 }
112 else if (rule->src.set && !rule->src.any &&
113 !(rule->_src = fw3_lookup_zone(state, rule->src.name, false)))
114 {
115 warn_elem(e, "refers to not existing zone '%s'", rule->src.name);
116 fw3_free_rule(rule);
117 continue;
118 }
119 else if (rule->dest.set && !rule->dest.any &&
120 !(rule->_dest = fw3_lookup_zone(state, rule->dest.name, false)))
121 {
122 warn_elem(e, "refers to not existing zone '%s'", rule->dest.name);
123 fw3_free_rule(rule);
124 continue;
125 }
126 else if (rule->ipset.set && state->disable_ipsets)
127 {
128 warn_elem(e, "skipped due to disabled ipset support");
129 fw3_free_rule(rule);
130 continue;
131 }
132 else if (rule->ipset.set && !rule->ipset.any &&
133 !(rule->_ipset = fw3_lookup_ipset(state, rule->ipset.name, false)))
134 {
135 warn_elem(e, "refers to unknown ipset '%s'", rule->ipset.name);
136 fw3_free_rule(rule);
137 continue;
138 }
139
140 if (!rule->_src && rule->target == FW3_TARGET_NOTRACK)
141 {
142 warn_elem(e, "is set to target NOTRACK but has no source assigned");
143 fw3_free_rule(rule);
144 continue;
145 }
146
147 if (!rule->_src && !rule->_dest && !rule->src.any && !rule->dest.any)
148 {
149 warn_elem(e, "has neither a source nor a destination zone assigned "
150 "- assuming an output rule");
151 }
152
153 if (rule->target == FW3_TARGET_UNSPEC)
154 {
155 warn_elem(e, "has no target specified, defaulting to REJECT");
156 rule->target = FW3_TARGET_REJECT;
157 }
158 else if (rule->target > FW3_TARGET_NOTRACK)
159 {
160 warn_elem(e, "has invalid target specified, defaulting to REJECT");
161 rule->target = FW3_TARGET_REJECT;
162 }
163
164 if (rule->_dest)
165 setbit(rule->_dest->dst_flags, rule->target);
166
167 list_add_tail(&rule->list, &state->rules);
168 continue;
169 }
170 }
171
172
173 static void
174 print_chain(struct fw3_rule *rule)
175 {
176 char chain[256];
177
178 sprintf(chain, "delegate_output");
179
180 if (rule->target == FW3_TARGET_NOTRACK)
181 {
182 sprintf(chain, "zone_%s_notrack", rule->src.name);
183 }
184 else
185 {
186 if (rule->src.set)
187 {
188 if (!rule->src.any)
189 {
190 if (rule->dest.set)
191 sprintf(chain, "zone_%s_forward", rule->src.name);
192 else
193 sprintf(chain, "zone_%s_input", rule->src.name);
194 }
195 else
196 {
197 if (rule->dest.set)
198 sprintf(chain, "delegate_forward");
199 else
200 sprintf(chain, "delegate_input");
201 }
202 }
203
204 if (rule->dest.set && !rule->src.set)
205 sprintf(chain, "zone_%s_output", rule->dest.name);
206 }
207
208 fw3_pr("-A %s", chain);
209 }
210
211 static void print_target(struct fw3_rule *rule)
212 {
213 const char *target;
214
215 switch(rule->target)
216 {
217 case FW3_TARGET_ACCEPT:
218 case FW3_TARGET_DROP:
219 case FW3_TARGET_NOTRACK:
220 target = fw3_flag_names[rule->target];
221 break;
222
223 default:
224 target = fw3_flag_names[FW3_TARGET_REJECT];
225 break;
226 }
227
228 if (rule->dest.set && !rule->dest.any)
229 fw3_pr(" -j zone_%s_dest_%s\n", rule->dest.name, target);
230 else if (rule->target == FW3_TARGET_REJECT)
231 fw3_pr(" -j reject\n");
232 else
233 fw3_pr(" -j %s\n", target);
234 }
235
236 static void
237 print_rule(enum fw3_table table, enum fw3_family family,
238 struct fw3_rule *rule, struct fw3_protocol *proto,
239 struct fw3_address *sip, struct fw3_address *dip,
240 struct fw3_port *sport, struct fw3_port *dport,
241 struct fw3_mac *mac, struct fw3_icmptype *icmptype)
242 {
243 if (!fw3_is_family(sip, family) || !fw3_is_family(dip, family))
244 {
245 info(" ! Skipping due to different family of ip address");
246 return;
247 }
248
249 if (proto->protocol == 58 && family == FW3_FAMILY_V4)
250 {
251 info(" ! Skipping due to different family of protocol");
252 return;
253 }
254
255 print_chain(rule);
256 fw3_format_ipset(rule->_ipset, rule->ipset.invert);
257 fw3_format_protocol(proto, family);
258 fw3_format_src_dest(sip, dip);
259 fw3_format_sport_dport(sport, dport);
260 fw3_format_icmptype(icmptype, family);
261 fw3_format_mac(mac);
262 fw3_format_limit(&rule->limit);
263 fw3_format_time(&rule->time);
264 fw3_format_extra(rule->extra);
265 fw3_format_comment(rule->name);
266 print_target(rule);
267 }
268
269 static void
270 expand_rule(enum fw3_table table, enum fw3_family family,
271 struct fw3_rule *rule, int num)
272 {
273 struct fw3_protocol *proto;
274 struct fw3_address *sip;
275 struct fw3_address *dip;
276 struct fw3_port *sport;
277 struct fw3_port *dport;
278 struct fw3_mac *mac;
279 struct fw3_icmptype *icmptype;
280
281 struct list_head *sports = NULL;
282 struct list_head *dports = NULL;
283 struct list_head *icmptypes = NULL;
284
285 struct list_head empty;
286 INIT_LIST_HEAD(&empty);
287
288 if (!fw3_is_family(rule, family))
289 return;
290
291 if ((table == FW3_TABLE_RAW && rule->target != FW3_TARGET_NOTRACK) ||
292 (table != FW3_TABLE_FILTER))
293 return;
294
295 if (rule->name)
296 info(" * Rule '%s'", rule->name);
297 else
298 info(" * Rule #%u", num);
299
300 if (!fw3_is_family(rule->_src, family) ||
301 !fw3_is_family(rule->_dest, family))
302 {
303 info(" ! Skipping due to different family of zone");
304 return;
305 }
306
307 if (rule->_ipset)
308 {
309 if (!fw3_is_family(rule->_ipset, family))
310 {
311 info(" ! Skipping due to different family in ipset");
312 return;
313 }
314
315 setbit(rule->_ipset->flags, family);
316 }
317
318 list_for_each_entry(proto, &rule->proto, list)
319 {
320 /* icmp / ipv6-icmp */
321 if (proto->protocol == 1 || proto->protocol == 58)
322 {
323 sports = &empty;
324 dports = &empty;
325 icmptypes = &rule->icmp_type;
326 }
327 else
328 {
329 sports = &rule->port_src;
330 dports = &rule->port_dest;
331 icmptypes = &empty;
332 }
333
334 fw3_foreach(sip, &rule->ip_src)
335 fw3_foreach(dip, &rule->ip_dest)
336 fw3_foreach(sport, sports)
337 fw3_foreach(dport, dports)
338 fw3_foreach(mac, &rule->mac_src)
339 fw3_foreach(icmptype, icmptypes)
340 print_rule(table, family, rule, proto, sip, dip, sport, dport,
341 mac, icmptype);
342 }
343 }
344
345 void
346 fw3_print_rules(enum fw3_table table, enum fw3_family family,
347 struct fw3_state *state)
348 {
349 int num = 0;
350 struct fw3_rule *rule;
351
352 list_for_each_entry(rule, &state->rules, list)
353 expand_rule(table, family, rule, num++);
354 }