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