print a notification if forwards are skipped due to zone family mismatch
[project/firewall3.git] / zones.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 "zones.h"
20 #include "ubus.h"
21
22
23 #define C(f, tbl, tgt, name) \
24         { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_TARGET_##tgt, name }
25
26 struct chain {
27         enum fw3_family family;
28         enum fw3_table table;
29         enum fw3_target target;
30         const char *name;
31 };
32
33 static const struct chain src_chains[] = {
34         C(ANY, FILTER, UNSPEC,  "zone_%s_input"),
35         C(ANY, FILTER, UNSPEC,  "zone_%s_output"),
36         C(ANY, FILTER, UNSPEC,  "zone_%s_forward"),
37
38         C(ANY, FILTER, ACCEPT,  "zone_%s_src_ACCEPT"),
39         C(ANY, FILTER, REJECT,  "zone_%s_src_REJECT"),
40         C(ANY, FILTER, DROP,    "zone_%s_src_DROP"),
41 };
42
43 static const struct chain dst_chains[] = {
44         C(ANY, FILTER, ACCEPT,  "zone_%s_dest_ACCEPT"),
45         C(ANY, FILTER, REJECT,  "zone_%s_dest_REJECT"),
46         C(ANY, FILTER, DROP,    "zone_%s_dest_DROP"),
47
48         C(V4,  NAT,    SNAT,    "zone_%s_postrouting"),
49         C(V4,  NAT,    DNAT,    "zone_%s_prerouting"),
50
51         C(ANY, RAW,    NOTRACK, "zone_%s_notrack"),
52 };
53
54 static struct fw3_option zone_opts[] = {
55         FW3_OPT("name",                string,   zone,     name),
56         FW3_OPT("family",              family,   zone,     family),
57
58         FW3_LIST("network",            device,   zone,     networks),
59         FW3_LIST("device",             device,   zone,     devices),
60         FW3_LIST("subnet",             address,  zone,     subnets),
61
62         FW3_OPT("input",               target,   zone,     policy_input),
63         FW3_OPT("forward",             target,   zone,     policy_forward),
64         FW3_OPT("output",              target,   zone,     policy_output),
65
66         FW3_OPT("masq",                bool,     zone,     masq),
67         FW3_LIST("masq_src",           address,  zone,     masq_src),
68         FW3_LIST("masq_dest",          address,  zone,     masq_dest),
69
70         FW3_OPT("extra",               string,   zone,     extra_src),
71         FW3_OPT("extra_src",           string,   zone,     extra_src),
72         FW3_OPT("extra_dest",          string,   zone,     extra_dest),
73
74         FW3_OPT("conntrack",           bool,     zone,     conntrack),
75         FW3_OPT("mtu_fix",             bool,     zone,     mtu_fix),
76         FW3_OPT("custom_chains",       bool,     zone,     custom_chains),
77
78         FW3_OPT("log",                 bool,     zone,     log),
79         FW3_OPT("log_limit",           limit,    zone,     log_limit),
80 };
81
82
83 static bool
84 print_chains(enum fw3_table table, enum fw3_family family,
85              const char *fmt, const char *name, uint16_t targets,
86              const struct chain *chains, int n)
87 {
88         bool rv = false;
89         char cn[128] = { 0 };
90         const struct chain *c;
91
92         for (c = chains; n > 0; c++, n--)
93         {
94                 if (!fw3_is_family(c, family))
95                         continue;
96
97                 if (c->table != table)
98                         continue;
99
100                 if ((c->target != FW3_TARGET_UNSPEC) && !hasbit(targets, c->target))
101                         continue;
102
103                 snprintf(cn, sizeof(cn), c->name, name);
104                 fw3_pr(fmt, cn);
105
106                 rv = true;
107         }
108
109         return rv;
110 }
111
112 static void
113 check_policy(struct uci_element *e, enum fw3_target *pol, enum fw3_target def,
114              const char *name)
115 {
116         if (*pol == FW3_TARGET_UNSPEC)
117         {
118                 warn_elem(e, "has no %s policy specified, using default", name);
119                 *pol = def;
120         }
121         else if (*pol > FW3_TARGET_DROP)
122         {
123                 warn_elem(e, "has invalid %s policy, using default", name);
124                 *pol = def;
125         }
126 }
127
128 static void
129 resolve_networks(struct uci_element *e, struct fw3_zone *zone)
130 {
131         struct fw3_device *net, *tmp;
132
133         list_for_each_entry(net, &zone->networks, list)
134         {
135                 tmp = fw3_ubus_device(net->name);
136
137                 if (!tmp)
138                 {
139                         warn_elem(e, "cannot resolve device of network '%s'", net->name);
140                         continue;
141                 }
142
143                 list_add_tail(&tmp->list, &zone->devices);
144         }
145 }
146
147 void
148 fw3_load_zones(struct fw3_state *state, struct uci_package *p)
149 {
150         struct uci_section *s;
151         struct uci_element *e;
152         struct fw3_zone *zone;
153         struct fw3_defaults *defs = &state->defaults;
154
155         INIT_LIST_HEAD(&state->zones);
156
157         uci_foreach_element(&p->sections, e)
158         {
159                 s = uci_to_section(e);
160
161                 if (strcmp(s->type, "zone"))
162                         continue;
163
164                 zone = malloc(sizeof(*zone));
165
166                 if (!zone)
167                         continue;
168
169                 memset(zone, 0, sizeof(*zone));
170
171                 INIT_LIST_HEAD(&zone->networks);
172                 INIT_LIST_HEAD(&zone->devices);
173                 INIT_LIST_HEAD(&zone->subnets);
174                 INIT_LIST_HEAD(&zone->masq_src);
175                 INIT_LIST_HEAD(&zone->masq_dest);
176
177                 zone->log_limit.rate = 10;
178
179                 fw3_parse_options(zone, zone_opts, ARRAY_SIZE(zone_opts), s);
180
181                 if (!zone->extra_dest)
182                         zone->extra_dest = zone->extra_src;
183
184                 if (!zone->name || !*zone->name)
185                 {
186                         warn_elem(e, "has no name - ignoring");
187                         fw3_free_zone(zone);
188                         continue;
189                 }
190
191                 if (list_empty(&zone->networks) && list_empty(&zone->devices) &&
192                     list_empty(&zone->subnets) && !zone->extra_src)
193                 {
194                         warn_elem(e, "has no device, network, subnet or extra options");
195                 }
196
197                 check_policy(e, &zone->policy_input, defs->policy_input, "input");
198                 check_policy(e, &zone->policy_output, defs->policy_output, "output");
199                 check_policy(e, &zone->policy_forward, defs->policy_forward, "forward");
200
201                 resolve_networks(e, zone);
202
203                 if (zone->masq)
204                 {
205                         setbit(zone->dst_flags, FW3_TARGET_SNAT);
206                         zone->conntrack = true;
207                 }
208
209                 setbit(zone->src_flags, zone->policy_input);
210                 setbit(zone->dst_flags, zone->policy_output);
211                 setbit(zone->dst_flags, zone->policy_forward);
212
213                 list_add_tail(&zone->list, &state->zones);
214         }
215 }
216
217
218 static void
219 print_zone_chain(enum fw3_table table, enum fw3_family family,
220                  struct fw3_zone *zone, bool disable_notrack)
221 {
222         bool s, d;
223
224         if (!fw3_is_family(zone, family))
225                 return;
226
227         if (!zone->conntrack && !disable_notrack)
228                 setbit(zone->dst_flags, FW3_TARGET_NOTRACK);
229
230         s = print_chains(table, family, ":%s - [0:0]\n", zone->name,
231                          zone->src_flags, src_chains, ARRAY_SIZE(src_chains));
232
233         d = print_chains(table, family, ":%s - [0:0]\n", zone->name,
234                          zone->dst_flags, dst_chains, ARRAY_SIZE(dst_chains));
235
236         if (s || d)
237                 info("   * Zone '%s'", zone->name);
238 }
239
240 static void
241 print_interface_rule(enum fw3_table table, enum fw3_family family,
242                      struct fw3_zone *zone, struct fw3_device *dev,
243                      struct fw3_address *sub, bool disable_notrack)
244 {
245         enum fw3_target t;
246
247 #define jump_target(t) \
248         ((t == FW3_TARGET_REJECT) ? "reject" : fw3_flag_names[t])
249
250         if (table == FW3_TABLE_FILTER)
251         {
252                 for (t = FW3_TARGET_ACCEPT; t <= FW3_TARGET_DROP; t++)
253                 {
254                         if (hasbit(zone->src_flags, t))
255                         {
256                                 fw3_pr("-A zone_%s_src_%s", zone->name, fw3_flag_names[t]);
257                                 fw3_format_in_out(dev, NULL);
258                                 fw3_format_src_dest(sub, NULL);
259                                 fw3_format_extra(zone->extra_src);
260                                 fw3_pr(" -j %s\n", jump_target(t));
261                         }
262
263                         if (hasbit(zone->dst_flags, t))
264                         {
265                                 fw3_pr("-A zone_%s_dest_%s", zone->name, fw3_flag_names[t]);
266                                 fw3_format_in_out(NULL, dev);
267                                 fw3_format_src_dest(NULL, sub);
268                                 fw3_format_extra(zone->extra_dest);
269                                 fw3_pr(" -j %s\n", jump_target(t));
270                         }
271                 }
272
273                 fw3_pr("-A delegate_input");
274                 fw3_format_in_out(dev, NULL);
275                 fw3_format_src_dest(sub, NULL);
276                 fw3_format_extra(zone->extra_src);
277                 fw3_pr(" -j zone_%s_input\n", zone->name);
278
279                 fw3_pr("-A delegate_forward");
280                 fw3_format_in_out(dev, NULL);
281                 fw3_format_src_dest(sub, NULL);
282                 fw3_format_extra(zone->extra_src);
283                 fw3_pr(" -j zone_%s_forward\n", zone->name);
284
285                 fw3_pr("-A delegate_output");
286                 fw3_format_in_out(NULL, dev);
287                 fw3_format_src_dest(NULL, sub);
288                 fw3_format_extra(zone->extra_dest);
289                 fw3_pr(" -j zone_%s_output\n", zone->name);
290         }
291         else if (table == FW3_TABLE_NAT)
292         {
293                 if (hasbit(zone->dst_flags, FW3_TARGET_DNAT))
294                 {
295                         fw3_pr("-A delegate_prerouting");
296                         fw3_format_in_out(dev, NULL);
297                         fw3_format_src_dest(sub, NULL);
298                         fw3_format_extra(zone->extra_src);
299                         fw3_pr(" -j zone_%s_prerouting\n", zone->name);
300                 }
301
302                 if (hasbit(zone->dst_flags, FW3_TARGET_SNAT))
303                 {
304                         fw3_pr("-A delegate_postrouting");
305                         fw3_format_in_out(NULL, dev);
306                         fw3_format_src_dest(NULL, sub);
307                         fw3_format_extra(zone->extra_dest);
308                         fw3_pr(" -j zone_%s_postrouting\n", zone->name);
309                 }
310         }
311         else if (table == FW3_TABLE_MANGLE)
312         {
313                 if (zone->mtu_fix)
314                 {
315                         if (zone->log)
316                         {
317                                 fw3_pr("-A mssfix");
318                                 fw3_format_in_out(NULL, dev);
319                                 fw3_format_src_dest(NULL, sub);
320                                 fw3_pr(" -p tcp --tcp-flags SYN,RST SYN");
321                                 fw3_format_limit(&zone->log_limit);
322                                 fw3_format_comment(zone->name, " (mtu_fix logging)");
323                                 fw3_pr(" -j LOG --log-prefix \"MSSFIX(%s): \"\n", zone->name);
324                         }
325
326                         fw3_pr("-A mssfix");
327                         fw3_format_in_out(NULL, dev);
328                         fw3_format_src_dest(NULL, sub);
329                         fw3_pr(" -p tcp --tcp-flags SYN,RST SYN");
330                         fw3_format_comment(zone->name, " (mtu_fix)");
331                         fw3_pr(" -j TCPMSS --clamp-mss-to-pmtu\n");
332                 }
333         }
334         else if (table == FW3_TABLE_RAW)
335         {
336                 if (!zone->conntrack && !disable_notrack)
337                 {
338                         fw3_pr("-A notrack");
339                         fw3_format_in_out(dev, NULL);
340                         fw3_format_src_dest(sub, NULL);
341                         fw3_format_extra(zone->extra_src);
342                         fw3_format_comment(zone->name, " (notrack)");
343                         fw3_pr(" -j CT --notrack\n", zone->name);
344                 }
345         }
346 }
347
348 static void
349 print_interface_rules(enum fw3_table table, enum fw3_family family,
350                       struct fw3_zone *zone, bool disable_notrack)
351 {
352         struct fw3_device *dev;
353         struct fw3_address *sub;
354
355         fw3_foreach(dev, &zone->devices)
356         fw3_foreach(sub, &zone->subnets)
357         {
358                 if (!fw3_is_family(sub, family))
359                         continue;
360
361                 if (!dev && !sub)
362                         continue;
363
364                 print_interface_rule(table, family, zone, dev, sub, disable_notrack);
365         }
366 }
367
368 static void
369 print_zone_rule(enum fw3_table table, enum fw3_family family,
370                 struct fw3_zone *zone, bool disable_notrack)
371 {
372         struct fw3_address *msrc;
373         struct fw3_address *mdest;
374
375         enum fw3_target t;
376
377         if (!fw3_is_family(zone, family))
378                 return;
379
380         switch (table)
381         {
382         case FW3_TABLE_FILTER:
383                 fw3_pr("-A zone_%s_input -j zone_%s_src_%s\n",
384                            zone->name, zone->name, fw3_flag_names[zone->policy_input]);
385
386                 fw3_pr("-A zone_%s_forward -j zone_%s_dest_%s\n",
387                            zone->name, zone->name, fw3_flag_names[zone->policy_forward]);
388
389                 fw3_pr("-A zone_%s_output -j zone_%s_dest_%s\n",
390                            zone->name, zone->name, fw3_flag_names[zone->policy_output]);
391
392                 if (zone->log)
393                 {
394                         for (t = FW3_TARGET_REJECT; t <= FW3_TARGET_DROP; t++)
395                         {
396                                 if (hasbit(zone->src_flags, t))
397                                 {
398                                         fw3_pr("-A zone_%s_src_%s", zone->name, fw3_flag_names[t]);
399                                         fw3_format_limit(&zone->log_limit);
400                                         fw3_pr(" -j LOG --log-prefix \"%s(src %s)\"\n",
401                                                    fw3_flag_names[t], zone->name);
402                                 }
403
404                                 if (hasbit(zone->dst_flags, t))
405                                 {
406                                         fw3_pr("-A zone_%s_dest_%s", zone->name, fw3_flag_names[t]);
407                                         fw3_format_limit(&zone->log_limit);
408                                         fw3_pr(" -j LOG --log-prefix \"%s(dest %s)\"\n",
409                                                    fw3_flag_names[t], zone->name);
410                                 }
411                         }
412                 }
413                 break;
414
415         case FW3_TABLE_NAT:
416                 if (zone->masq && family == FW3_FAMILY_V4)
417                 {
418                         fw3_foreach(msrc, &zone->masq_src)
419                         fw3_foreach(mdest, &zone->masq_dest)
420                         {
421                                 fw3_pr("-A zone_%s_postrouting ", zone->name);
422                                 fw3_format_src_dest(msrc, mdest);
423                                 fw3_pr("-j MASQUERADE\n");
424                         }
425                 }
426                 break;
427
428         case FW3_TABLE_RAW:
429         case FW3_TABLE_MANGLE:
430                 break;
431         }
432
433         print_interface_rules(table, family, zone, disable_notrack);
434 }
435
436 void
437 fw3_print_zone_chains(enum fw3_table table, enum fw3_family family,
438                       struct fw3_state *state)
439 {
440         struct fw3_zone *zone;
441
442         list_for_each_entry(zone, &state->zones, list)
443                 print_zone_chain(table, family, zone, state->defaults.drop_invalid);
444 }
445
446 void
447 fw3_print_zone_rules(enum fw3_table table, enum fw3_family family,
448                      struct fw3_state *state)
449 {
450         struct fw3_zone *zone;
451
452         list_for_each_entry(zone, &state->zones, list)
453                 print_zone_rule(table, family, zone, state->defaults.drop_invalid);
454 }
455
456 void
457 fw3_flush_zones(enum fw3_table table, enum fw3_family family,
458                             bool pass2, struct list_head *statefile)
459 {
460         struct fw3_statefile_entry *e;
461
462         list_for_each_entry(e, statefile, list)
463         {
464                 if (e->type != FW3_TYPE_ZONE)
465                         continue;
466
467                 print_chains(table, family, pass2 ? "-X %s\n" : "-F %s\n",
468                              e->name, e->flags[0], src_chains, ARRAY_SIZE(src_chains));
469
470                 print_chains(table, family, pass2 ? "-X %s\n" : "-F %s\n",
471                              e->name, e->flags[1], dst_chains, ARRAY_SIZE(dst_chains));
472         }
473 }
474
475 struct fw3_zone *
476 fw3_lookup_zone(struct fw3_state *state, const char *name)
477 {
478         struct fw3_zone *z;
479
480         if (list_empty(&state->zones))
481                 return NULL;
482
483         list_for_each_entry(z, &state->zones, list)
484                 if (!strcmp(z->name, name))
485                         return z;
486
487         return NULL;
488 }
489
490 void
491 fw3_free_zone(struct fw3_zone *zone)
492 {
493         fw3_free_list(&zone->networks);
494         fw3_free_list(&zone->devices);
495         fw3_free_list(&zone->subnets);
496
497         fw3_free_list(&zone->masq_src);
498         fw3_free_list(&zone->masq_dest);
499
500         free(zone);
501 }