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