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