Add fw3 zone call to list devices in a zone
[project/firewall3.git] / main.c
1 /*
2 * firewall3 - 3rd OpenWrt UCI firewall implementation
3 *
4 * Copyright (C) 2013-2014 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 <stdio.h>
20 #include <unistd.h>
21
22 #include "options.h"
23 #include "defaults.h"
24 #include "zones.h"
25 #include "rules.h"
26 #include "redirects.h"
27 #include "snats.h"
28 #include "forwards.h"
29 #include "ipsets.h"
30 #include "includes.h"
31 #include "ubus.h"
32 #include "iptables.h"
33
34
35 static enum fw3_family print_family = FW3_FAMILY_ANY;
36
37 static struct fw3_state *run_state = NULL;
38 static struct fw3_state *cfg_state = NULL;
39
40
41 static bool
42 build_state(bool runtime)
43 {
44 struct fw3_state *state = NULL;
45 struct uci_package *p = NULL;
46 FILE *sf;
47
48 state = malloc(sizeof(*state));
49
50 if (!state)
51 error("Out of memory");
52
53 memset(state, 0, sizeof(*state));
54 state->uci = uci_alloc_context();
55
56 if (!state->uci)
57 error("Out of memory");
58
59 if (runtime)
60 {
61 sf = fopen(FW3_STATEFILE, "r");
62
63 if (sf)
64 {
65 uci_import(state->uci, sf, "fw3_state", &p, true);
66 fclose(sf);
67 }
68
69 if (!p)
70 {
71 uci_free_context(state->uci);
72 free(state);
73
74 return false;
75 }
76
77 state->statefile = true;
78
79 run_state = state;
80 }
81 else
82 {
83 if (!fw3_ubus_connect())
84 error("Failed to connect to ubus");
85
86 if (uci_load(state->uci, "firewall", &p))
87 {
88 uci_perror(state->uci, NULL);
89 error("Failed to load /etc/config/firewall");
90 }
91
92 if (!fw3_find_command("ipset"))
93 {
94 warn("Unable to locate ipset utility, disabling ipset support");
95 state->disable_ipsets = true;
96 }
97
98 cfg_state = state;
99 }
100
101
102 struct blob_buf b = {NULL, NULL, 0, NULL};
103 fw3_ubus_rules(&b);
104
105 fw3_load_defaults(state, p);
106 fw3_load_ipsets(state, p);
107 fw3_load_zones(state, p);
108 fw3_load_rules(state, p, b.head);
109 fw3_load_redirects(state, p);
110 fw3_load_snats(state, p, b.head);
111 fw3_load_forwards(state, p);
112 fw3_load_includes(state, p);
113
114 return true;
115 }
116
117 static void
118 free_state(struct fw3_state *state)
119 {
120 struct list_head *cur, *tmp;
121
122 list_for_each_safe(cur, tmp, &state->zones)
123 fw3_free_zone((struct fw3_zone *)cur);
124
125 list_for_each_safe(cur, tmp, &state->rules)
126 fw3_free_rule((struct fw3_rule *)cur);
127
128 list_for_each_safe(cur, tmp, &state->redirects)
129 fw3_free_redirect((struct fw3_redirect *)cur);
130
131 list_for_each_safe(cur, tmp, &state->snats)
132 fw3_free_snat((struct fw3_snat *)cur);
133
134 list_for_each_safe(cur, tmp, &state->forwards)
135 fw3_free_forward((struct fw3_forward *)cur);
136
137 list_for_each_safe(cur, tmp, &state->ipsets)
138 fw3_free_ipset((struct fw3_ipset *)cur);
139
140 list_for_each_safe(cur, tmp, &state->includes)
141 fw3_free_include((struct fw3_include *)cur);
142
143 uci_free_context(state->uci);
144
145 free(state);
146
147 fw3_ubus_disconnect();
148 }
149
150
151 static bool
152 family_running(enum fw3_family family)
153 {
154 return (run_state && has(run_state->defaults.flags, family, family));
155 }
156
157 static void
158 family_set(struct fw3_state *state, enum fw3_family family, bool set)
159 {
160 if (!state)
161 return;
162
163 if (set)
164 set(state->defaults.flags, family, family);
165 else
166 del(state->defaults.flags, family, family);
167 }
168
169 static int
170 stop(bool complete)
171 {
172 FILE *ct;
173
174 int rv = 1;
175 enum fw3_family family;
176 enum fw3_table table;
177 struct fw3_ipt_handle *handle;
178
179 if (!complete && !run_state)
180 {
181 warn("The firewall appears to be stopped. "
182 "Use the 'flush' command to forcefully purge all rules.");
183
184 return rv;
185 }
186
187 if (!print_family && run_state)
188 fw3_hotplug_zones(run_state, false);
189
190 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
191 {
192 if (!complete && !family_running(family))
193 continue;
194
195 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
196 {
197 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
198 continue;
199
200 if (!(handle = fw3_ipt_open(family, table)))
201 continue;
202
203 info(" * %sing %s %s table", complete ? "Flush" : "Clear",
204 fw3_flag_names[family], fw3_flag_names[table]);
205
206 if (complete)
207 {
208 fw3_flush_all(handle);
209 }
210 else if (run_state)
211 {
212 fw3_flush_rules(handle, run_state, false);
213 fw3_flush_zones(handle, run_state, false);
214 }
215
216 fw3_ipt_commit(handle);
217 fw3_ipt_close(handle);
218 }
219
220 family_set(run_state, family, false);
221 family_set(cfg_state, family, false);
222
223 rv = 0;
224 }
225
226 if (run_state)
227 fw3_destroy_ipsets(run_state);
228
229 if (complete && (ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
230 {
231 info(" * Flushing conntrack table ...");
232
233 fwrite("f\n", 2, 1, ct);
234 fclose(ct);
235 }
236
237 if (!rv && run_state)
238 fw3_write_statefile(run_state);
239
240 return rv;
241 }
242
243 static int
244 start(void)
245 {
246 int rv = 1;
247 enum fw3_family family;
248 enum fw3_table table;
249 struct fw3_ipt_handle *handle;
250
251 if (!print_family)
252 fw3_create_ipsets(cfg_state);
253
254 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
255 {
256 if (family == FW3_FAMILY_V6 && cfg_state->defaults.disable_ipv6)
257 continue;
258
259 if (print_family && family != print_family)
260 continue;
261
262 if (!print_family && family_running(family))
263 {
264 warn("The %s firewall appears to be started already. "
265 "If it is indeed empty, remove the %s file and retry.",
266 fw3_flag_names[family], FW3_STATEFILE);
267
268 continue;
269 }
270
271 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
272 {
273 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
274 continue;
275
276 if (!(handle = fw3_ipt_open(family, table)))
277 continue;
278
279 info(" * Populating %s %s table",
280 fw3_flag_names[family], fw3_flag_names[table]);
281
282 fw3_print_default_chains(handle, cfg_state, false);
283 fw3_print_zone_chains(handle, cfg_state, false);
284 fw3_print_default_head_rules(handle, cfg_state, false);
285 fw3_print_rules(handle, cfg_state);
286 fw3_print_redirects(handle, cfg_state);
287 fw3_print_snats(handle, cfg_state);
288 fw3_print_forwards(handle, cfg_state);
289 fw3_print_zone_rules(handle, cfg_state, false);
290 fw3_print_default_tail_rules(handle, cfg_state, false);
291
292 if (!print_family)
293 fw3_ipt_commit(handle);
294
295 fw3_ipt_close(handle);
296 }
297
298 if (!print_family)
299 fw3_print_includes(cfg_state, family, false);
300
301 family_set(run_state, family, true);
302 family_set(cfg_state, family, true);
303
304 rv = 0;
305 }
306
307 if (!rv)
308 {
309 fw3_set_defaults(cfg_state);
310
311 if (!print_family)
312 {
313 fw3_run_includes(cfg_state, false);
314 fw3_hotplug_zones(cfg_state, true);
315 fw3_write_statefile(cfg_state);
316 }
317 }
318
319 return rv;
320 }
321
322
323 static int
324 reload(void)
325 {
326 int rv = 1;
327 enum fw3_family family;
328 enum fw3_table table;
329 struct fw3_ipt_handle *handle;
330
331 if (!run_state)
332 return start();
333
334 fw3_hotplug_zones(run_state, false);
335
336 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
337 {
338 if (!family_running(family))
339 goto start;
340
341 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
342 {
343 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
344 continue;
345
346 if (!(handle = fw3_ipt_open(family, table)))
347 continue;
348
349 info(" * Clearing %s %s table",
350 fw3_flag_names[family], fw3_flag_names[table]);
351
352 fw3_flush_rules(handle, run_state, true);
353 fw3_flush_zones(handle, run_state, true);
354 fw3_ipt_commit(handle);
355 fw3_ipt_close(handle);
356 }
357
358 family_set(run_state, family, false);
359 family_set(cfg_state, family, false);
360
361 start:
362 if (family == FW3_FAMILY_V6 && cfg_state->defaults.disable_ipv6)
363 continue;
364
365 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
366 {
367 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
368 continue;
369
370 if (!(handle = fw3_ipt_open(family, table)))
371 continue;
372
373 info(" * Populating %s %s table",
374 fw3_flag_names[family], fw3_flag_names[table]);
375
376 fw3_print_default_chains(handle, cfg_state, true);
377 fw3_print_zone_chains(handle, cfg_state, true);
378 fw3_print_default_head_rules(handle, cfg_state, true);
379 fw3_print_rules(handle, cfg_state);
380 fw3_print_redirects(handle, cfg_state);
381 fw3_print_snats(handle, cfg_state);
382 fw3_print_forwards(handle, cfg_state);
383 fw3_print_zone_rules(handle, cfg_state, true);
384 fw3_print_default_tail_rules(handle, cfg_state, true);
385
386 fw3_ipt_commit(handle);
387 fw3_ipt_close(handle);
388 }
389
390 fw3_print_includes(cfg_state, family, true);
391
392 family_set(run_state, family, true);
393 family_set(cfg_state, family, true);
394
395 rv = 0;
396 }
397
398 if (!rv)
399 {
400 fw3_set_defaults(cfg_state);
401 fw3_run_includes(cfg_state, true);
402 fw3_hotplug_zones(cfg_state, true);
403 fw3_write_statefile(cfg_state);
404 }
405
406 return rv;
407 }
408
409 static int
410 lookup_network(const char *net)
411 {
412 struct fw3_zone *z;
413 struct fw3_device *d;
414
415 list_for_each_entry(z, &cfg_state->zones, list)
416 {
417 list_for_each_entry(d, &z->networks, list)
418 {
419 if (!strcmp(d->name, net))
420 {
421 printf("%s\n", z->name);
422 return 0;
423 }
424 }
425 }
426
427 return 1;
428 }
429
430 static int
431 lookup_device(const char *dev)
432 {
433 struct fw3_zone *z;
434 struct fw3_device *d;
435
436 list_for_each_entry(z, &cfg_state->zones, list)
437 {
438 list_for_each_entry(d, &z->devices, list)
439 {
440 if (!strcmp(d->name, dev))
441 {
442 printf("%s\n", z->name);
443 return 0;
444 }
445 }
446 }
447
448 return 1;
449 }
450
451 static int
452 lookup_zone(const char *zone, const char *device)
453 {
454 struct fw3_zone *z;
455 struct fw3_device *d;
456
457 list_for_each_entry(z, &cfg_state->zones, list)
458 {
459 if (strcmp(z->name, zone))
460 continue;
461
462 list_for_each_entry(d, &z->devices, list)
463 {
464 if (device && strcmp(device, d->name))
465 continue;
466
467 printf("%s\n", d->name);
468
469 if (device)
470 return 0;
471 }
472
473 if (!device)
474 return 0;
475 }
476
477 return 1;
478 }
479
480 static int
481 usage(void)
482 {
483 fprintf(stderr, "fw3 [-4] [-6] [-q] print\n");
484 fprintf(stderr, "fw3 [-q] {start|stop|flush|reload|restart}\n");
485 fprintf(stderr, "fw3 [-q] network {net}\n");
486 fprintf(stderr, "fw3 [-q] device {dev}\n");
487 fprintf(stderr, "fw3 [-q] zone {zone} [dev]\n");
488
489 return 1;
490 }
491
492
493 int main(int argc, char **argv)
494 {
495 int ch, rv = 1;
496 enum fw3_family family = FW3_FAMILY_ANY;
497 struct fw3_defaults *defs = NULL;
498
499 while ((ch = getopt(argc, argv, "46dqh")) != -1)
500 {
501 switch (ch)
502 {
503 case '4':
504 family = FW3_FAMILY_V4;
505 break;
506
507 case '6':
508 family = FW3_FAMILY_V6;
509 break;
510
511 case 'd':
512 fw3_pr_debug = true;
513 break;
514
515 case 'q':
516 if (freopen("/dev/null", "w", stderr)) {}
517 break;
518
519 case 'h':
520 rv = usage();
521 goto out;
522 }
523 }
524
525 build_state(false);
526 build_state(true);
527 defs = &cfg_state->defaults;
528
529 if (optind >= argc)
530 {
531 rv = usage();
532 goto out;
533 }
534
535 if (!strcmp(argv[optind], "print"))
536 {
537 if (family == FW3_FAMILY_ANY)
538 {
539 family = FW3_FAMILY_V4;
540 }
541 else if (family == FW3_FAMILY_V6)
542 {
543 if (defs->disable_ipv6)
544 warn("IPv6 rules globally disabled in configuration");
545 #ifdef DISABLE_IPV6
546 else
547 warn("IPv6 support is not compiled in");
548 #endif
549 }
550
551 if (freopen("/dev/null", "w", stderr)) {};
552
553 cfg_state->disable_ipsets = true;
554 print_family = family;
555 fw3_pr_debug = true;
556
557 rv = start();
558 }
559 else if (!strcmp(argv[optind], "start"))
560 {
561 if (fw3_lock())
562 {
563 rv = start();
564 fw3_unlock();
565 }
566 }
567 else if (!strcmp(argv[optind], "stop"))
568 {
569 if (fw3_lock())
570 {
571 rv = stop(false);
572 fw3_unlock();
573 }
574 }
575 else if (!strcmp(argv[optind], "flush"))
576 {
577 if (fw3_lock())
578 {
579 rv = stop(true);
580 fw3_unlock();
581 }
582 }
583 else if (!strcmp(argv[optind], "restart"))
584 {
585 if (fw3_lock())
586 {
587 stop(true);
588 rv = start();
589 fw3_unlock();
590 }
591 }
592 else if (!strcmp(argv[optind], "reload"))
593 {
594 if (fw3_lock())
595 {
596 rv = reload();
597 fw3_unlock();
598 }
599 }
600 else if (!strcmp(argv[optind], "network") && (optind + 1) < argc)
601 {
602 rv = lookup_network(argv[optind + 1]);
603 }
604 else if (!strcmp(argv[optind], "device") && (optind + 1) < argc)
605 {
606 rv = lookup_device(argv[optind + 1]);
607 }
608 else if (!strcmp(argv[optind], "zone") && (optind + 1) < argc)
609 {
610 rv = lookup_zone(argv[optind + 1], argv[optind + 2]);
611 }
612 else
613 {
614 rv = usage();
615 }
616
617 out:
618 if (cfg_state)
619 free_state(cfg_state);
620
621 if (run_state)
622 free_state(run_state);
623
624 return rv;
625 }