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