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