Record default policies in state file
[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, bool reload)
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                 if (!reload)
190                         warn("The firewall appears to be stopped. "
191                                  "Use the 'flush' command to forcefully purge all rules.");
192
193                 return rv;
194         }
195
196         if (!print_rules && run_state)
197                 fw3_hotplug_zones(run_state, false);
198
199         for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
200         {
201                 if (!complete && !family_running(family))
202                         continue;
203
204                 if (!restore_pipe(family, true))
205                         continue;
206
207                 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
208                 {
209                         if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
210                                 continue;
211
212                         info(" * %sing %s %s table", complete ? "Flush" : "Clear",
213                              fw3_flag_names[family], fw3_flag_names[table]);
214
215                         fw3_pr("*%s\n", fw3_flag_names[table]);
216
217                         if (complete)
218                         {
219                                 fw3_flush_all(table);
220                         }
221                         else if (run_state)
222                         {
223                                 /* pass 1 */
224                                 fw3_flush_rules(run_state, family, table, reload, false);
225                                 fw3_flush_zones(run_state, family, table, reload, false);
226
227                                 /* pass 2 */
228                                 fw3_flush_rules(run_state, family, table, reload, true);
229                                 fw3_flush_zones(run_state, family, table, reload, true);
230                         }
231
232                         fw3_pr("COMMIT\n");
233                 }
234
235                 fw3_command_close();
236                 family_set(run_state, family, false);
237                 family_set(cfg_state, family, false);
238
239                 rv = 0;
240         }
241
242         if (!reload && run_state)
243         {
244                 if (fw3_command_pipe(false, "ipset", "-exist", "-"))
245                 {
246                         fw3_destroy_ipsets(run_state);
247                         fw3_command_close();
248                 }
249         }
250
251         if (complete && (ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
252         {
253                 info(" * Flushing conntrack table ...");
254
255                 fwrite("f\n", 2, 1, ct);
256                 fclose(ct);
257         }
258
259         if (!rv && run_state)
260                 fw3_write_statefile(run_state);
261
262         return rv;
263 }
264
265 static int
266 start(bool reload)
267 {
268         int rv = 1;
269         enum fw3_family family;
270         enum fw3_table table;
271
272         if (!print_rules && !reload)
273         {
274                 if (fw3_command_pipe(false, "ipset", "-exist", "-"))
275                 {
276                         fw3_create_ipsets(cfg_state);
277                         fw3_command_close();
278                 }
279         }
280
281         for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
282         {
283                 if (family == FW3_FAMILY_V6 && cfg_state->defaults.disable_ipv6)
284                         continue;
285
286                 if (!print_rules && !reload && family_running(family))
287                 {
288                         warn("The %s firewall appears to be started already. "
289                              "If it is indeed empty, remove the %s file and retry.",
290                              fw3_flag_names[family], FW3_STATEFILE);
291
292                         continue;
293                 }
294
295                 if (!restore_pipe(family, false))
296                         continue;
297
298                 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
299                 {
300                         if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
301                                 continue;
302
303                         info(" * Populating %s %s table",
304                              fw3_flag_names[family], fw3_flag_names[table]);
305
306                         fw3_pr("*%s\n", fw3_flag_names[table]);
307                         fw3_print_default_chains(cfg_state, family, table, reload);
308                         fw3_print_zone_chains(cfg_state, family, table, reload);
309                         fw3_print_default_head_rules(cfg_state, family, table, reload);
310                         fw3_print_rules(cfg_state, family, table);
311                         fw3_print_redirects(cfg_state, family, table);
312                         fw3_print_forwards(cfg_state, family, table);
313                         fw3_print_zone_rules(cfg_state, family, table, reload);
314                         fw3_print_default_tail_rules(cfg_state, family, table, reload);
315                         fw3_pr("COMMIT\n");
316                 }
317
318                 fw3_print_includes(cfg_state, family, reload);
319
320                 fw3_command_close();
321                 family_set(run_state, family, true);
322                 family_set(cfg_state, family, true);
323
324                 rv = 0;
325         }
326
327         if (!rv)
328         {
329                 fw3_set_defaults(cfg_state);
330
331                 if (!print_rules)
332                 {
333                         fw3_run_includes(cfg_state, reload);
334                         fw3_hotplug_zones(cfg_state, true);
335                         fw3_write_statefile(cfg_state);
336                 }
337         }
338
339         return rv;
340 }
341
342 static int
343 lookup_network(const char *net)
344 {
345         struct fw3_zone *z;
346         struct fw3_device *d;
347
348         list_for_each_entry(z, &cfg_state->zones, list)
349         {
350                 list_for_each_entry(d, &z->networks, list)
351                 {
352                         if (!strcmp(d->name, net))
353                         {
354                                 printf("%s\n", z->name);
355                                 return 0;
356                         }
357                 }
358         }
359
360         return 1;
361 }
362
363 static int
364 lookup_device(const char *dev)
365 {
366         struct fw3_zone *z;
367         struct fw3_device *d;
368
369         list_for_each_entry(z, &cfg_state->zones, list)
370         {
371                 list_for_each_entry(d, &z->devices, list)
372                 {
373                         if (!strcmp(d->name, dev))
374                         {
375                                 printf("%s\n", z->name);
376                                 return 0;
377                         }
378                 }
379         }
380
381         return 1;
382 }
383
384 static int
385 usage(void)
386 {
387         fprintf(stderr, "fw3 [-4] [-6] [-q] print\n");
388         fprintf(stderr, "fw3 [-q] {start|stop|flush|reload|restart}\n");
389         fprintf(stderr, "fw3 [-q] network {net}\n");
390         fprintf(stderr, "fw3 [-q] device {dev}\n");
391
392         return 1;
393 }
394
395
396 int main(int argc, char **argv)
397 {
398         int ch, rv = 1;
399         struct fw3_defaults *defs = NULL;
400         enum fw3_family use_family = FW3_FAMILY_ANY;
401
402         while ((ch = getopt(argc, argv, "46dqh")) != -1)
403         {
404                 switch (ch)
405                 {
406                 case '4':
407                         use_family = FW3_FAMILY_V4;
408                         break;
409
410                 case '6':
411                         use_family = FW3_FAMILY_V6;
412                         break;
413
414                 case 'd':
415                         fw3_pr_debug = true;
416                         break;
417
418                 case 'q':
419                         freopen("/dev/null", "w", stderr);
420                         break;
421
422                 case 'h':
423                         rv = usage();
424                         goto out;
425                 }
426         }
427
428         build_state(false);
429         build_state(true);
430         defs = &cfg_state->defaults;
431
432         if (optind >= argc)
433         {
434                 rv = usage();
435                 goto out;
436         }
437
438         if (!strcmp(argv[optind], "print"))
439         {
440                 if (use_family == FW3_FAMILY_ANY)
441                         use_family = FW3_FAMILY_V4;
442                 else if (use_family == FW3_FAMILY_V6 && defs->disable_ipv6)
443                         warn("IPv6 rules globally disabled in configuration");
444
445                 freopen("/dev/null", "w", stderr);
446
447                 cfg_state->disable_ipsets = true;
448                 print_rules = true;
449
450                 rv = start(false);
451         }
452         else if (!strcmp(argv[optind], "start"))
453         {
454                 if (fw3_lock())
455                 {
456                         rv = start(false);
457                         fw3_unlock();
458                 }
459         }
460         else if (!strcmp(argv[optind], "stop"))
461         {
462                 if (fw3_lock())
463                 {
464                         rv = stop(false, false);
465                         fw3_unlock();
466                 }
467         }
468         else if (!strcmp(argv[optind], "flush"))
469         {
470                 if (fw3_lock())
471                 {
472                         rv = stop(true, false);
473                         fw3_unlock();
474                 }
475         }
476         else if (!strcmp(argv[optind], "restart"))
477         {
478                 if (fw3_lock())
479                 {
480                         stop(true, false);
481                         rv = start(false);
482
483                         fw3_unlock();
484                 }
485         }
486         else if (!strcmp(argv[optind], "reload"))
487         {
488                 if (fw3_lock())
489                 {
490                         rv = stop(false, true);
491                         rv = start(!rv);
492
493                         fw3_unlock();
494                 }
495         }
496         else if (!strcmp(argv[optind], "network") && (optind + 1) < argc)
497         {
498                 rv = lookup_network(argv[optind + 1]);
499         }
500         else if (!strcmp(argv[optind], "device") && (optind + 1) < argc)
501         {
502                 rv = lookup_device(argv[optind + 1]);
503         }
504         else
505         {
506                 rv = usage();
507         }
508
509 out:
510         if (cfg_state)
511                 free_state(cfg_state);
512
513         if (run_state)
514                 free_state(run_state);
515
516         return rv;
517 }