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