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