zones: disable masq when resolving of all masq_src or masq_dest items failed
[project/firewall3.git] / rules.c
1 /*
2  * firewall3 - 3rd OpenWrt UCI firewall implementation
3  *
4  *   Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
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("device",              string,    rule,     device),
32         FW3_OPT("direction",           direction, rule,     direction_out),
33
34         FW3_OPT("ipset",               setmatch,  rule,     ipset),
35
36         FW3_LIST("proto",              protocol,  rule,     proto),
37
38         FW3_LIST("src_ip",             network,   rule,     ip_src),
39         FW3_LIST("src_mac",            mac,       rule,     mac_src),
40         FW3_LIST("src_port",           port,      rule,     port_src),
41
42         FW3_LIST("dest_ip",            network,   rule,     ip_dest),
43         FW3_LIST("dest_port",          port,      rule,     port_dest),
44
45         FW3_LIST("icmp_type",          icmptype,  rule,     icmp_type),
46         FW3_OPT("extra",               string,    rule,     extra),
47
48         FW3_OPT("limit",               limit,     rule,     limit),
49         FW3_OPT("limit_burst",         int,       rule,     limit.burst),
50
51         FW3_OPT("utc_time",            bool,      rule,     time.utc),
52         FW3_OPT("start_date",          date,      rule,     time.datestart),
53         FW3_OPT("stop_date",           date,      rule,     time.datestop),
54         FW3_OPT("start_time",          time,      rule,     time.timestart),
55         FW3_OPT("stop_time",           time,      rule,     time.timestop),
56         FW3_OPT("weekdays",            weekdays,  rule,     time.weekdays),
57         FW3_OPT("monthdays",           monthdays, rule,     time.monthdays),
58
59         FW3_OPT("mark",                mark,      rule,     mark),
60         FW3_OPT("set_mark",            mark,      rule,     set_mark),
61         FW3_OPT("set_xmark",           mark,      rule,     set_xmark),
62
63         FW3_OPT("target",              target,    rule,     target),
64
65         { }
66 };
67
68
69 static bool
70 need_src_action_chain(struct fw3_rule *r)
71 {
72         return (r->_src && r->_src->log && (r->target > FW3_FLAG_ACCEPT));
73 }
74
75 static struct fw3_rule*
76 alloc_rule(struct fw3_state *state)
77 {
78         struct fw3_rule *rule = calloc(1, sizeof(*rule));
79
80         if (rule) {
81                 INIT_LIST_HEAD(&rule->proto);
82
83                 INIT_LIST_HEAD(&rule->ip_src);
84                 INIT_LIST_HEAD(&rule->mac_src);
85                 INIT_LIST_HEAD(&rule->port_src);
86
87                 INIT_LIST_HEAD(&rule->ip_dest);
88                 INIT_LIST_HEAD(&rule->port_dest);
89
90                 INIT_LIST_HEAD(&rule->icmp_type);
91
92                 list_add_tail(&rule->list, &state->rules);
93                 rule->enabled = true;
94         }
95
96         return rule;
97 }
98
99 static bool
100 check_rule(struct fw3_state *state, struct fw3_rule *r, struct uci_element *e)
101 {
102         if (!r->enabled)
103                 return false;
104
105         if (r->src.invert || r->dest.invert)
106         {
107                 warn_section("rule", r, e, "must not have inverted 'src' or 'dest' options");
108                 return false;
109         }
110         else if (r->src.set && !r->src.any &&
111                  !(r->_src = fw3_lookup_zone(state, r->src.name)))
112         {
113                 warn_section("rule", r, e, "refers to not existing zone '%s'", r->src.name);
114                 return false;
115         }
116         else if (r->dest.set && !r->dest.any &&
117                  !(r->_dest = fw3_lookup_zone(state, r->dest.name)))
118         {
119                 warn_section("rule", r, e, "refers to not existing zone '%s'", r->dest.name);
120                 return false;
121         }
122         else if (r->ipset.set && state->disable_ipsets)
123         {
124                 warn_section("rule", r, e, "skipped due to disabled ipset support");
125                 return false;
126         }
127         else if (r->ipset.set &&
128                  !(r->ipset.ptr = fw3_lookup_ipset(state, r->ipset.name)))
129         {
130                 warn_section("rule", r, e, "refers to unknown ipset '%s'", r->ipset.name);
131                 return false;
132         }
133
134         if (!r->_src && r->target == FW3_FLAG_NOTRACK)
135         {
136                 warn_section("rule", r, e, "is set to target NOTRACK but has no source assigned");
137                 return false;
138         }
139
140         if (!r->set_mark.set && !r->set_xmark.set &&
141             r->target == FW3_FLAG_MARK)
142         {
143                 warn_section("rule", r, e, "is set to target MARK but specifies neither "
144                                 "'set_mark' nor 'set_xmark' option");
145                 return false;
146         }
147
148         if (r->_dest && r->target == FW3_FLAG_MARK)
149         {
150                 warn_section("rule", r, e, "must not specify 'dest' for MARK target");
151                 return false;
152         }
153
154         if (r->set_mark.invert || r->set_xmark.invert)
155         {
156                 warn_section("rule", r, e, "must not have inverted 'set_mark' or 'set_xmark'");
157                 return false;
158         }
159
160         if (!r->_src && !r->_dest && !r->src.any && !r->dest.any)
161         {
162                 warn_section("rule", r, e, "has neither a source nor a destination zone assigned "
163                                 "- assuming an output r");
164         }
165
166         if (list_empty(&r->proto))
167         {
168                 warn_section("rule", r, e, "does not specify a protocol, assuming TCP+UDP");
169                 fw3_parse_protocol(&r->proto, "tcpudp", true);
170         }
171
172         if (r->target == FW3_FLAG_UNSPEC)
173         {
174                 warn_section("rule", r, e, "has no target specified, defaulting to REJECT");
175                 r->target = FW3_FLAG_REJECT;
176         }
177         else if (r->target > FW3_FLAG_MARK)
178         {
179                 warn_section("rule", r, e, "has invalid target specified, defaulting to REJECT");
180                 r->target = FW3_FLAG_REJECT;
181         }
182
183         /* NB: r family... */
184         if (r->_dest)
185         {
186                 fw3_setbit(r->_dest->flags[0], r->target);
187                 fw3_setbit(r->_dest->flags[1], r->target);
188         }
189         else if (need_src_action_chain(r))
190         {
191                 fw3_setbit(r->_src->flags[0], fw3_to_src_target(r->target));
192                 fw3_setbit(r->_src->flags[1], fw3_to_src_target(r->target));
193         }
194
195         return true;
196 }
197
198 void
199 fw3_load_rules(struct fw3_state *state, struct uci_package *p,
200                 struct blob_attr *a)
201 {
202         struct uci_section *s;
203         struct uci_element *e;
204         struct fw3_rule *rule;
205         struct blob_attr *entry;
206         unsigned rem;
207
208         INIT_LIST_HEAD(&state->rules);
209
210         blob_for_each_attr(entry, a, rem) {
211                 const char *type;
212                 const char *name = "ubus rule";
213
214                 if (!fw3_attr_parse_name_type(entry, &name, &type))
215                         continue;
216
217                 if (strcmp(type, "rule"))
218                         continue;
219
220                 if (!(rule = alloc_rule(state)))
221                         continue;
222
223                 if (!fw3_parse_blob_options(rule, fw3_rule_opts, entry, name))
224                 {
225                         warn_section("rule", rule, NULL, "skipped due to invalid options");
226                         fw3_free_rule(rule);
227                         continue;
228                 }
229
230                 if (!check_rule(state, rule, NULL))
231                         fw3_free_rule(rule);
232         }
233
234         uci_foreach_element(&p->sections, e)
235         {
236                 s = uci_to_section(e);
237
238                 if (strcmp(s->type, "rule"))
239                         continue;
240
241                 if (!(rule = alloc_rule(state)))
242                         continue;
243
244                 if (!fw3_parse_options(rule, fw3_rule_opts, s))
245                 {
246                         warn_elem(e, "skipped due to invalid options");
247                         fw3_free_rule(rule);
248                         continue;
249                 }
250
251                 if (!check_rule(state, rule, e))
252                         fw3_free_rule(rule);
253         }
254 }
255
256
257 static void
258 append_chain(struct fw3_ipt_rule *r, struct fw3_rule *rule)
259 {
260         char chain[32];
261
262         snprintf(chain, sizeof(chain), "OUTPUT");
263
264         if (rule->target == FW3_FLAG_NOTRACK)
265         {
266                 snprintf(chain, sizeof(chain), "zone_%s_notrack", rule->src.name);
267         }
268         else if (rule->target == FW3_FLAG_MARK && (rule->_src || rule->src.any))
269         {
270                 snprintf(chain, sizeof(chain), "PREROUTING");
271         }
272         else
273         {
274                 if (rule->src.set)
275                 {
276                         if (!rule->src.any)
277                         {
278                                 if (rule->dest.set)
279                                         snprintf(chain, sizeof(chain), "zone_%s_forward",
280                                                  rule->src.name);
281                                 else
282                                         snprintf(chain, sizeof(chain), "zone_%s_input",
283                                                  rule->src.name);
284                         }
285                         else
286                         {
287                                 if (rule->dest.set)
288                                         snprintf(chain, sizeof(chain), "FORWARD");
289                                 else
290                                         snprintf(chain, sizeof(chain), "INPUT");
291                         }
292                 }
293
294                 if (rule->dest.set && !rule->src.set)
295                 {
296                         if (rule->dest.any)
297                                 snprintf(chain, sizeof(chain), "OUTPUT");
298                         else
299                                 snprintf(chain, sizeof(chain), "zone_%s_output",
300                                          rule->dest.name);
301                 }
302         }
303
304         fw3_ipt_rule_append(r, chain);
305 }
306
307 static void set_target(struct fw3_ipt_rule *r, struct fw3_rule *rule)
308 {
309         const char *name;
310         struct fw3_mark *mark;
311         char buf[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
312
313         switch(rule->target)
314         {
315         case FW3_FLAG_MARK:
316                 name = rule->set_mark.set ? "--set-mark" : "--set-xmark";
317                 mark = rule->set_mark.set ? &rule->set_mark : &rule->set_xmark;
318                 sprintf(buf, "0x%x/0x%x", mark->mark, mark->mask);
319
320                 fw3_ipt_rule_target(r, "MARK");
321                 fw3_ipt_rule_addarg(r, false, name, buf);
322                 return;
323
324         case FW3_FLAG_NOTRACK:
325                 fw3_ipt_rule_target(r, "CT");
326                 fw3_ipt_rule_addarg(r, false, "--notrack", NULL);
327                 return;
328
329         case FW3_FLAG_ACCEPT:
330         case FW3_FLAG_DROP:
331                 name = fw3_flag_names[rule->target];
332                 break;
333
334         default:
335                 name = fw3_flag_names[FW3_FLAG_REJECT];
336                 break;
337         }
338
339         if (rule->dest.set && !rule->dest.any)
340                 fw3_ipt_rule_target(r, "zone_%s_dest_%s", rule->dest.name, name);
341         else if (need_src_action_chain(rule))
342                 fw3_ipt_rule_target(r, "zone_%s_src_%s", rule->src.name, name);
343         else if (strcmp(name, "REJECT"))
344                 fw3_ipt_rule_target(r, name);
345         else
346                 fw3_ipt_rule_target(r, "reject");
347 }
348
349 static void
350 set_comment(struct fw3_ipt_rule *r, const char *name, int num)
351 {
352         if (name)
353                 fw3_ipt_rule_comment(r, name);
354         else
355                 fw3_ipt_rule_comment(r, "@rule[%u]", num);
356 }
357
358 static void
359 print_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
360            struct fw3_rule *rule, int num, struct fw3_protocol *proto,
361            struct fw3_address *sip, struct fw3_address *dip,
362            struct fw3_port *sport, struct fw3_port *dport,
363            struct fw3_mac *mac, struct fw3_icmptype *icmptype)
364 {
365         struct fw3_ipt_rule *r;
366
367         if (!fw3_is_family(sip, handle->family) ||
368             !fw3_is_family(dip, handle->family))
369         {
370                 if ((sip && !sip->resolved) || (dip && !dip->resolved))
371                         info("     ! Skipping due to different family of ip address");
372
373                 return;
374         }
375
376         if (proto->protocol == 58 && handle->family == FW3_FAMILY_V4)
377         {
378                 info("     ! Skipping due to different family of protocol");
379                 return;
380         }
381
382         r = fw3_ipt_rule_create(handle, proto, NULL, NULL, sip, dip);
383         fw3_ipt_rule_sport_dport(r, sport, dport);
384         fw3_ipt_rule_device(r, rule->device, rule->direction_out);
385         fw3_ipt_rule_icmptype(r, icmptype);
386         fw3_ipt_rule_mac(r, mac);
387         fw3_ipt_rule_ipset(r, &rule->ipset);
388         fw3_ipt_rule_limit(r, &rule->limit);
389         fw3_ipt_rule_time(r, &rule->time);
390         fw3_ipt_rule_mark(r, &rule->mark);
391         set_target(r, rule);
392         fw3_ipt_rule_extra(r, rule->extra);
393         set_comment(r, rule->name, num);
394         append_chain(r, rule);
395 }
396
397 static void
398 expand_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
399             struct fw3_rule *rule, int num)
400 {
401         struct fw3_protocol *proto;
402         struct fw3_address *sip;
403         struct fw3_address *dip;
404         struct fw3_port *sport;
405         struct fw3_port *dport;
406         struct fw3_mac *mac;
407         struct fw3_icmptype *icmptype;
408
409         struct list_head *sports = NULL;
410         struct list_head *dports = NULL;
411         struct list_head *icmptypes = NULL;
412
413         struct list_head empty;
414         INIT_LIST_HEAD(&empty);
415
416         if (!fw3_is_family(rule, handle->family))
417                 return;
418
419         if ((rule->target == FW3_FLAG_NOTRACK && handle->table != FW3_TABLE_RAW) ||
420             (rule->target == FW3_FLAG_MARK && handle->table != FW3_TABLE_MANGLE) ||
421                 (rule->target < FW3_FLAG_NOTRACK && handle->table != FW3_TABLE_FILTER))
422                 return;
423
424         if (rule->name)
425                 info("   * Rule '%s'", rule->name);
426         else
427                 info("   * Rule #%u", num);
428
429         if (!fw3_is_family(rule->_src, handle->family) ||
430             !fw3_is_family(rule->_dest, handle->family))
431         {
432                 info("     ! Skipping due to different family of zone");
433                 return;
434         }
435
436         if (rule->ipset.ptr)
437         {
438                 if (!fw3_is_family(rule->ipset.ptr, handle->family))
439                 {
440                         info("     ! Skipping due to different family in ipset");
441                         return;
442                 }
443
444                 if (!fw3_check_ipset(rule->ipset.ptr))
445                 {
446                         info("     ! Skipping due to missing ipset '%s'",
447                              rule->ipset.ptr->external
448                                         ? rule->ipset.ptr->external : rule->ipset.ptr->name);
449                         return;
450                 }
451
452                 set(rule->ipset.ptr->flags, handle->family, handle->family);
453         }
454
455         list_for_each_entry(proto, &rule->proto, list)
456         {
457                 /* icmp / ipv6-icmp */
458                 if (proto->protocol == 1 || proto->protocol == 58)
459                 {
460                         sports = &empty;
461                         dports = &empty;
462                         icmptypes = &rule->icmp_type;
463                 }
464                 else
465                 {
466                         sports = &rule->port_src;
467                         dports = &rule->port_dest;
468                         icmptypes = &empty;
469                 }
470
471                 fw3_foreach(sip, &rule->ip_src)
472                 fw3_foreach(dip, &rule->ip_dest)
473                 fw3_foreach(sport, sports)
474                 fw3_foreach(dport, dports)
475                 fw3_foreach(mac, &rule->mac_src)
476                 fw3_foreach(icmptype, icmptypes)
477                         print_rule(handle, state, rule, num, proto, sip, dip,
478                                    sport, dport, mac, icmptype);
479         }
480 }
481
482 void
483 fw3_print_rules(struct fw3_ipt_handle *handle, struct fw3_state *state)
484 {
485         int num = 0;
486         struct fw3_rule *rule;
487
488         list_for_each_entry(rule, &state->rules, list)
489                 expand_rule(handle, state, rule, num++);
490 }