7a8969fae7a5683d4b3a75011dc7f3464bb6891c
[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 enum fw3_family print_family = FW3_FAMILY_ANY;
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 family_running(enum fw3_family family)
144 {
145         return (run_state && has(run_state->defaults.flags, family, family));
146 }
147
148 static void
149 family_set(struct fw3_state *state, enum fw3_family family, bool set)
150 {
151         if (!state)
152                 return;
153
154         if (set)
155                 set(state->defaults.flags, family, family);
156         else
157                 del(state->defaults.flags, family, family);
158 }
159
160 static int
161 stop(bool complete)
162 {
163         FILE *ct;
164
165         int rv = 1;
166         enum fw3_family family;
167         enum fw3_table table;
168         struct fw3_ipt_handle *handle;
169
170         if (!complete && !run_state)
171         {
172                 warn("The firewall appears to be stopped. "
173                          "Use the 'flush' command to forcefully purge all rules.");
174
175                 return rv;
176         }
177
178         if (!print_family && run_state)
179                 fw3_hotplug_zones(run_state, false);
180
181         for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
182         {
183                 if (!complete && !family_running(family))
184                         continue;
185
186                 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
187                 {
188                         if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
189                                 continue;
190
191                         if (!(handle = fw3_ipt_open(family, table)))
192                                 continue;
193
194                         info(" * %sing %s %s table", complete ? "Flush" : "Clear",
195                              fw3_flag_names[family], fw3_flag_names[table]);
196
197                         if (complete)
198                         {
199                                 fw3_flush_all(handle);
200                         }
201                         else if (run_state)
202                         {
203                                 fw3_flush_rules(handle, run_state, false);
204                                 fw3_flush_zones(handle, run_state, false);
205                         }
206
207                         fw3_ipt_commit(handle);
208                 }
209
210                 family_set(run_state, family, false);
211                 family_set(cfg_state, family, false);
212
213                 rv = 0;
214         }
215
216         if (run_state)
217         {
218                 if (fw3_command_pipe(false, "ipset", "-exist", "-"))
219                 {
220                         fw3_destroy_ipsets(run_state);
221                         fw3_command_close();
222                 }
223         }
224
225         if (complete && (ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
226         {
227                 info(" * Flushing conntrack table ...");
228
229                 fwrite("f\n", 2, 1, ct);
230                 fclose(ct);
231         }
232
233         if (!rv && run_state)
234                 fw3_write_statefile(run_state);
235
236         return rv;
237 }
238
239 static int
240 start(void)
241 {
242         int rv = 1;
243         enum fw3_family family;
244         enum fw3_table table;
245         struct fw3_ipt_handle *handle;
246
247         if (!print_family)
248         {
249                 if (fw3_command_pipe(false, "ipset", "-exist", "-"))
250                 {
251                         fw3_create_ipsets(cfg_state);
252                         fw3_command_close();
253                 }
254         }
255
256         for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
257         {
258                 if (family == FW3_FAMILY_V6 && cfg_state->defaults.disable_ipv6)
259                         continue;
260
261                 if (print_family && family != print_family)
262                         continue;
263
264                 if (!print_family && family_running(family))
265                 {
266                         warn("The %s firewall appears to be started already. "
267                              "If it is indeed empty, remove the %s file and retry.",
268                              fw3_flag_names[family], FW3_STATEFILE);
269
270                         continue;
271                 }
272
273                 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
274                 {
275                         if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
276                                 continue;
277
278                         if (!(handle = fw3_ipt_open(family, table)))
279                                 continue;
280
281                         info(" * Populating %s %s table",
282                              fw3_flag_names[family], fw3_flag_names[table]);
283
284                         fw3_print_default_chains(handle, cfg_state, false);
285                         fw3_print_zone_chains(handle, cfg_state, false);
286                         fw3_print_default_head_rules(handle, cfg_state, false);
287                         fw3_print_rules(handle, cfg_state);
288                         fw3_print_redirects(handle, cfg_state);
289                         fw3_print_forwards(handle, cfg_state);
290                         fw3_print_zone_rules(handle, cfg_state, false);
291                         fw3_print_default_tail_rules(handle, cfg_state, false);
292
293                         if (!print_family)
294                                 fw3_ipt_commit(handle);
295                 }
296
297                 if (!print_family)
298                         fw3_print_includes(cfg_state, family, false);
299
300                 family_set(run_state, family, true);
301                 family_set(cfg_state, family, true);
302
303                 rv = 0;
304         }
305
306         if (!rv)
307         {
308                 fw3_set_defaults(cfg_state);
309
310                 if (!print_family)
311                 {
312                         fw3_run_includes(cfg_state, false);
313                         fw3_hotplug_zones(cfg_state, true);
314                         fw3_write_statefile(cfg_state);
315                 }
316         }
317
318         return rv;
319 }
320
321
322 static int
323 reload(void)
324 {
325         int rv = 1;
326         enum fw3_family family;
327         enum fw3_table table;
328         struct fw3_ipt_handle *handle;
329
330         if (!print_family && run_state)
331                 fw3_hotplug_zones(run_state, false);
332
333         for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
334         {
335                 if (!family_running(family))
336                         goto start;
337
338                 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
339                 {
340                         if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
341                                 continue;
342
343                         if (!(handle = fw3_ipt_open(family, table)))
344                                 continue;
345
346                         info(" * Clearing %s %s table",
347                              fw3_flag_names[family], fw3_flag_names[table]);
348
349                         if (run_state)
350                         {
351                                 fw3_flush_rules(handle, run_state, true);
352                                 fw3_flush_zones(handle, run_state, true);
353                         }
354
355                         fw3_ipt_commit(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_forwards(handle, cfg_state);
382                         fw3_print_zone_rules(handle, cfg_state, true);
383                         fw3_print_default_tail_rules(handle, cfg_state, true);
384
385                         fw3_ipt_commit(handle);
386                 }
387
388                 fw3_print_includes(cfg_state, family, true);
389
390                 family_set(run_state, family, true);
391                 family_set(cfg_state, family, true);
392
393                 rv = 0;
394         }
395
396         if (!rv)
397         {
398                 fw3_set_defaults(cfg_state);
399
400                 if (!print_family)
401                 {
402                         fw3_run_includes(cfg_state, true);
403                         fw3_hotplug_zones(cfg_state, true);
404                         fw3_write_statefile(cfg_state);
405                 }
406         }
407
408         return rv;
409 }
410
411 static int
412 lookup_network(const char *net)
413 {
414         struct fw3_zone *z;
415         struct fw3_device *d;
416
417         list_for_each_entry(z, &cfg_state->zones, list)
418         {
419                 list_for_each_entry(d, &z->networks, list)
420                 {
421                         if (!strcmp(d->name, net))
422                         {
423                                 printf("%s\n", z->name);
424                                 return 0;
425                         }
426                 }
427         }
428
429         return 1;
430 }
431
432 static int
433 lookup_device(const char *dev)
434 {
435         struct fw3_zone *z;
436         struct fw3_device *d;
437
438         list_for_each_entry(z, &cfg_state->zones, list)
439         {
440                 list_for_each_entry(d, &z->devices, list)
441                 {
442                         if (!strcmp(d->name, dev))
443                         {
444                                 printf("%s\n", z->name);
445                                 return 0;
446                         }
447                 }
448         }
449
450         return 1;
451 }
452
453 static int
454 usage(void)
455 {
456         fprintf(stderr, "fw3 [-4] [-6] [-q] print\n");
457         fprintf(stderr, "fw3 [-q] {start|stop|flush|reload|restart}\n");
458         fprintf(stderr, "fw3 [-q] network {net}\n");
459         fprintf(stderr, "fw3 [-q] device {dev}\n");
460
461         return 1;
462 }
463
464
465 int main(int argc, char **argv)
466 {
467         int ch, rv = 1;
468         struct fw3_defaults *defs = NULL;
469
470         while ((ch = getopt(argc, argv, "46dqh")) != -1)
471         {
472                 switch (ch)
473                 {
474                 case '4':
475                         print_family = FW3_FAMILY_V4;
476                         break;
477
478                 case '6':
479                         print_family = FW3_FAMILY_V6;
480                         break;
481
482                 case 'd':
483                         fw3_pr_debug = true;
484                         break;
485
486                 case 'q':
487                         freopen("/dev/null", "w", stderr);
488                         break;
489
490                 case 'h':
491                         rv = usage();
492                         goto out;
493                 }
494         }
495
496         build_state(false);
497         build_state(true);
498         defs = &cfg_state->defaults;
499
500         if (optind >= argc)
501         {
502                 rv = usage();
503                 goto out;
504         }
505
506         if (!strcmp(argv[optind], "print"))
507         {
508                 if (print_family == FW3_FAMILY_ANY)
509                         print_family = FW3_FAMILY_V4;
510                 else if (print_family == FW3_FAMILY_V6 && defs->disable_ipv6)
511                         warn("IPv6 rules globally disabled in configuration");
512
513                 freopen("/dev/null", "w", stderr);
514
515                 cfg_state->disable_ipsets = true;
516                 fw3_pr_debug = true;
517
518                 rv = start();
519         }
520         else if (!strcmp(argv[optind], "start"))
521         {
522                 if (fw3_lock())
523                 {
524                         rv = start();
525                         fw3_unlock();
526                 }
527         }
528         else if (!strcmp(argv[optind], "stop"))
529         {
530                 if (fw3_lock())
531                 {
532                         rv = stop(false);
533                         fw3_unlock();
534                 }
535         }
536         else if (!strcmp(argv[optind], "flush"))
537         {
538                 if (fw3_lock())
539                 {
540                         rv = stop(true);
541                         fw3_unlock();
542                 }
543         }
544         else if (!strcmp(argv[optind], "restart"))
545         {
546                 if (fw3_lock())
547                 {
548                         stop(true);
549                         rv = start();
550                         fw3_unlock();
551                 }
552         }
553         else if (!strcmp(argv[optind], "reload"))
554         {
555                 if (fw3_lock())
556                 {
557                         rv = reload();
558                         fw3_unlock();
559                 }
560         }
561         else if (!strcmp(argv[optind], "network") && (optind + 1) < argc)
562         {
563                 rv = lookup_network(argv[optind + 1]);
564         }
565         else if (!strcmp(argv[optind], "device") && (optind + 1) < argc)
566         {
567                 rv = lookup_device(argv[optind + 1]);
568         }
569         else
570         {
571                 rv = usage();
572         }
573
574 out:
575         if (cfg_state)
576                 free_state(cfg_state);
577
578         if (run_state)
579                 free_state(run_state);
580
581         return rv;
582 }