Increase compatibility to old firewall by initializing protocol of rules and redirect...
[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_FLAG_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 (list_empty(&rule->proto))
154 {
155 warn_elem(e, "does not specify a protocol, assuming TCP+UDP");
156 fw3_parse_protocol(&rule->proto, "tcpudp", true);
157 }
158
159 if (rule->target == FW3_FLAG_UNSPEC)
160 {
161 warn_elem(e, "has no target specified, defaulting to REJECT");
162 rule->target = FW3_FLAG_REJECT;
163 }
164 else if (rule->target > FW3_FLAG_NOTRACK)
165 {
166 warn_elem(e, "has invalid target specified, defaulting to REJECT");
167 rule->target = FW3_FLAG_REJECT;
168 }
169
170 /* NB: rule family... */
171 if (rule->_dest)
172 {
173 setbit(rule->_dest->flags[0], rule->target);
174 setbit(rule->_dest->flags[1], rule->target);
175 }
176
177 list_add_tail(&rule->list, &state->rules);
178 continue;
179 }
180 }
181
182
183 static void
184 print_chain(struct fw3_rule *rule)
185 {
186 char chain[256];
187
188 sprintf(chain, "delegate_output");
189
190 if (rule->target == FW3_FLAG_NOTRACK)
191 {
192 sprintf(chain, "zone_%s_notrack", rule->src.name);
193 }
194 else
195 {
196 if (rule->src.set)
197 {
198 if (!rule->src.any)
199 {
200 if (rule->dest.set)
201 sprintf(chain, "zone_%s_forward", rule->src.name);
202 else
203 sprintf(chain, "zone_%s_input", rule->src.name);
204 }
205 else
206 {
207 if (rule->dest.set)
208 sprintf(chain, "delegate_forward");
209 else
210 sprintf(chain, "delegate_input");
211 }
212 }
213
214 if (rule->dest.set && !rule->src.set)
215 sprintf(chain, "zone_%s_output", rule->dest.name);
216 }
217
218 fw3_pr("-A %s", chain);
219 }
220
221 static void print_target(struct fw3_rule *rule)
222 {
223 const char *target;
224
225 switch(rule->target)
226 {
227 case FW3_FLAG_ACCEPT:
228 case FW3_FLAG_DROP:
229 case FW3_FLAG_NOTRACK:
230 target = fw3_flag_names[rule->target];
231 break;
232
233 default:
234 target = fw3_flag_names[FW3_FLAG_REJECT];
235 break;
236 }
237
238 if (rule->dest.set && !rule->dest.any)
239 fw3_pr(" -j zone_%s_dest_%s\n", rule->dest.name, target);
240 else if (rule->target == FW3_FLAG_REJECT)
241 fw3_pr(" -j reject\n");
242 else
243 fw3_pr(" -j %s\n", target);
244 }
245
246 static void
247 print_rule(struct fw3_state *state, enum fw3_family family,
248 enum fw3_table table, struct fw3_rule *rule,
249 struct fw3_protocol *proto,
250 struct fw3_address *sip, struct fw3_address *dip,
251 struct fw3_port *sport, struct fw3_port *dport,
252 struct fw3_mac *mac, struct fw3_icmptype *icmptype)
253 {
254 if (!fw3_is_family(sip, family) || !fw3_is_family(dip, family))
255 {
256 info(" ! Skipping due to different family of ip address");
257 return;
258 }
259
260 if (proto->protocol == 58 && family == FW3_FAMILY_V4)
261 {
262 info(" ! Skipping due to different family of protocol");
263 return;
264 }
265
266 print_chain(rule);
267 fw3_format_ipset(rule->_ipset, rule->ipset.invert);
268 fw3_format_protocol(proto, family);
269 fw3_format_src_dest(sip, dip);
270 fw3_format_sport_dport(sport, dport);
271 fw3_format_icmptype(icmptype, family);
272 fw3_format_mac(mac);
273 fw3_format_limit(&rule->limit);
274 fw3_format_time(&rule->time);
275 fw3_format_extra(rule->extra);
276 fw3_format_comment(rule->name);
277 print_target(rule);
278 }
279
280 static void
281 expand_rule(struct fw3_state *state, enum fw3_family family,
282 enum fw3_table table, struct fw3_rule *rule, int num)
283 {
284 struct fw3_protocol *proto;
285 struct fw3_address *sip;
286 struct fw3_address *dip;
287 struct fw3_port *sport;
288 struct fw3_port *dport;
289 struct fw3_mac *mac;
290 struct fw3_icmptype *icmptype;
291
292 struct list_head *sports = NULL;
293 struct list_head *dports = NULL;
294 struct list_head *icmptypes = NULL;
295
296 struct list_head empty;
297 INIT_LIST_HEAD(&empty);
298
299 if (!fw3_is_family(rule, family))
300 return;
301
302 if ((table == FW3_TABLE_RAW && rule->target != FW3_FLAG_NOTRACK) ||
303 (table != FW3_TABLE_FILTER))
304 return;
305
306 if (rule->name)
307 info(" * Rule '%s'", rule->name);
308 else
309 info(" * Rule #%u", num);
310
311 if (!fw3_is_family(rule->_src, family) ||
312 !fw3_is_family(rule->_dest, family))
313 {
314 info(" ! Skipping due to different family of zone");
315 return;
316 }
317
318 if (rule->_ipset)
319 {
320 if (!fw3_is_family(rule->_ipset, family))
321 {
322 info(" ! Skipping due to different family in ipset");
323 return;
324 }
325
326 set(rule->_ipset->flags, family, family);
327 }
328
329 list_for_each_entry(proto, &rule->proto, list)
330 {
331 /* icmp / ipv6-icmp */
332 if (proto->protocol == 1 || proto->protocol == 58)
333 {
334 sports = &empty;
335 dports = &empty;
336 icmptypes = &rule->icmp_type;
337 }
338 else
339 {
340 sports = &rule->port_src;
341 dports = &rule->port_dest;
342 icmptypes = &empty;
343 }
344
345 fw3_foreach(sip, &rule->ip_src)
346 fw3_foreach(dip, &rule->ip_dest)
347 fw3_foreach(sport, sports)
348 fw3_foreach(dport, dports)
349 fw3_foreach(mac, &rule->mac_src)
350 fw3_foreach(icmptype, icmptypes)
351 print_rule(state, family, table, rule, proto, sip, dip,
352 sport, dport, mac, icmptype);
353 }
354 }
355
356 void
357 fw3_print_rules(struct fw3_state *state, enum fw3_family family,
358 enum fw3_table table)
359 {
360 int num = 0;
361 struct fw3_rule *rule;
362
363 list_for_each_entry(rule, &state->rules, list)
364 expand_rule(state, family, table, rule, num++);
365 }