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