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