47951e64349593071a2f3d6067c51cb1a3517a69
[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
32
33 static bool print_rules = false;
34 static enum fw3_family use_family = FW3_FAMILY_ANY;
35
36
37 static struct fw3_state *
38 build_state(void)
39 {
40         struct fw3_state *state = NULL;
41         struct uci_package *p = NULL;
42
43         if (!fw3_ubus_connect())
44                 error("Failed to connect to ubus");
45
46         state = malloc(sizeof(*state));
47
48         if (!state)
49                 error("Out of memory");
50
51         memset(state, 0, sizeof(*state));
52         state->uci = uci_alloc_context();
53
54         if (!state->uci)
55                 error("Out of memory");
56
57         if (uci_load(state->uci, "firewall", &p))
58         {
59                 uci_perror(state->uci, NULL);
60                 error("Failed to load /etc/config/firewall");
61         }
62
63         if (!fw3_find_command("ipset"))
64         {
65                 warn("Unable to locate ipset utility, disabling ipset support");
66                 state->disable_ipsets = true;
67         }
68
69         INIT_LIST_HEAD(&state->running_zones);
70         INIT_LIST_HEAD(&state->running_ipsets);
71
72         fw3_load_defaults(state, p);
73         fw3_load_ipsets(state, p);
74         fw3_load_zones(state, p);
75         fw3_load_rules(state, p);
76         fw3_load_redirects(state, p);
77         fw3_load_forwards(state, p);
78         fw3_load_includes(state, p);
79
80         state->statefile = fw3_read_statefile(state);
81
82         return state;
83 }
84
85 static void
86 free_state(struct fw3_state *state)
87 {
88         struct list_head *cur, *tmp;
89
90         list_for_each_safe(cur, tmp, &state->zones)
91                 fw3_free_zone((struct fw3_zone *)cur);
92
93         list_for_each_safe(cur, tmp, &state->rules)
94                 fw3_free_rule((struct fw3_rule *)cur);
95
96         list_for_each_safe(cur, tmp, &state->redirects)
97                 fw3_free_redirect((struct fw3_redirect *)cur);
98
99         list_for_each_safe(cur, tmp, &state->forwards)
100                 fw3_free_forward((struct fw3_forward *)cur);
101
102         list_for_each_safe(cur, tmp, &state->ipsets)
103                 fw3_free_ipset((struct fw3_ipset *)cur);
104
105         list_for_each_safe(cur, tmp, &state->includes)
106                 fw3_free_include((struct fw3_include *)cur);
107
108         uci_free_context(state->uci);
109
110         free(state);
111
112         fw3_ubus_disconnect();
113 }
114
115
116 static bool
117 restore_pipe(enum fw3_family family, bool silent)
118 {
119         const char *cmd;
120
121         cmd = (family == FW3_FAMILY_V4) ? "iptables-restore" : "ip6tables-restore";
122
123         if (print_rules)
124                 return fw3_stdout_pipe();
125
126         if (!fw3_command_pipe(silent, cmd, "--lenient", "--noflush"))
127         {
128                 warn("Unable to execute %s", cmd);
129                 return false;
130         }
131
132         return true;
133 }
134
135 static bool
136 family_running(struct fw3_state *state, enum fw3_family family)
137 {
138         return hasbit(state->defaults.running_flags, family);
139 }
140
141 static bool
142 family_used(enum fw3_family family)
143 {
144         return (use_family == FW3_FAMILY_ANY) || (use_family == family);
145 }
146
147 static bool
148 family_loaded(struct fw3_state *state, enum fw3_family family)
149 {
150         return hasbit(state->defaults.flags, family);
151 }
152
153 static void
154 family_set(struct fw3_state *state, enum fw3_family family, bool set)
155 {
156         if (set)
157                 setbit(state->defaults.flags, family);
158         else
159                 delbit(state->defaults.flags, family);
160 }
161
162 static int
163 stop(struct fw3_state *state, bool complete, bool reload)
164 {
165         FILE *ct;
166
167         int rv = 1;
168         enum fw3_family family;
169         enum fw3_table table;
170         enum fw3_target policy = reload ? FW3_TARGET_DROP : FW3_TARGET_ACCEPT;
171
172         if (!complete && !state->statefile)
173         {
174                 if (!reload)
175                         warn("The firewall appears to be stopped. "
176                                  "Use the 'flush' command to forcefully purge all rules.");
177
178                 return rv;
179         }
180
181         for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
182         {
183                 if (!complete && !family_running(state, family))
184                         continue;
185
186                 if (!family_used(family) || !restore_pipe(family, true))
187                         continue;
188
189                 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
190                 {
191                         if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
192                                 continue;
193
194                         info(" * %sing %s %s table", complete ? "Flush" : "Clear",
195                              fw3_flag_names[family], fw3_flag_names[table]);
196
197                         fw3_pr("*%s\n", fw3_flag_names[table]);
198
199                         if (complete)
200                         {
201                                 fw3_flush_all(table);
202                         }
203                         else
204                         {
205                                 /* pass 1 */
206                                 fw3_flush_rules(table, family, false, state, policy);
207                                 fw3_flush_zones(table, family, false, reload, state);
208
209                                 /* pass 2 */
210                                 fw3_flush_rules(table, family, true, state, policy);
211                                 fw3_flush_zones(table, family, true, reload, state);
212                         }
213
214                         fw3_pr("COMMIT\n");
215                 }
216
217                 fw3_command_close();
218
219                 if (!reload)
220                 {
221                         if (fw3_command_pipe(false, "ipset", "-exist", "-"))
222                         {
223                                 fw3_destroy_ipsets(state, family);
224                                 fw3_command_close();
225                         }
226
227                         family_set(state, family, false);
228                 }
229
230                 rv = 0;
231         }
232
233         if (complete && (ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
234         {
235                 info(" * Flushing conntrack table ...");
236
237                 fwrite("f\n", 2, 1, ct);
238                 fclose(ct);
239         }
240
241         if (!rv)
242                 fw3_write_statefile(state);
243
244         return rv;
245 }
246
247 static int
248 start(struct fw3_state *state, bool reload)
249 {
250         int rv = 1;
251         enum fw3_family family;
252         enum fw3_table table;
253
254         if (!print_rules && !reload)
255         {
256                 if (fw3_command_pipe(false, "ipset", "-exist", "-"))
257                 {
258                         fw3_create_ipsets(state);
259                         fw3_command_close();
260                 }
261         }
262
263         for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
264         {
265                 if (!family_used(family))
266                         continue;
267
268                 if (!print_rules && !reload && family_running(state, family))
269                 {
270                         warn("The %s firewall appears to be started already. "
271                              "If it is indeed empty, remove the %s file and retry.",
272                              fw3_flag_names[family], FW3_STATEFILE);
273
274                         continue;
275                 }
276
277                 if (!family_loaded(state, family) || !restore_pipe(family, false))
278                         continue;
279
280                 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
281                 {
282                         if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
283                                 continue;
284
285                         info(" * Populating %s %s table",
286                              fw3_flag_names[family], fw3_flag_names[table]);
287
288                         fw3_pr("*%s\n", fw3_flag_names[table]);
289                         fw3_print_default_chains(table, family, state);
290                         fw3_print_zone_chains(table, family, state);
291                         fw3_print_default_head_rules(table, family, state);
292                         fw3_print_rules(table, family, state);
293                         fw3_print_redirects(table, family, state);
294                         fw3_print_forwards(table, family, state);
295                         fw3_print_zone_rules(table, family, state);
296                         fw3_print_default_tail_rules(table, family, state);
297                         fw3_pr("COMMIT\n");
298                 }
299
300                 if (!reload)
301                         fw3_print_includes(family, state);
302
303                 fw3_command_close();
304                 family_set(state, family, true);
305
306                 rv = 0;
307         }
308
309         if (!rv)
310         {
311                 fw3_set_defaults(state);
312
313                 if (!reload && !print_rules)
314                         fw3_run_includes(state);
315
316                 if (!print_rules)
317                         fw3_write_statefile(state);
318         }
319
320         return rv;
321 }
322
323 static int
324 lookup_network(struct fw3_state *state, const char *net)
325 {
326         struct fw3_zone *z;
327         struct fw3_device *d;
328
329         list_for_each_entry(z, &state->zones, list)
330         {
331                 list_for_each_entry(d, &z->networks, list)
332                 {
333                         if (!strcmp(d->name, net))
334                         {
335                                 printf("%s\n", z->name);
336                                 return 0;
337                         }
338                 }
339         }
340
341         return 1;
342 }
343
344 static int
345 lookup_device(struct fw3_state *state, const char *dev)
346 {
347         struct fw3_zone *z;
348         struct fw3_device *d;
349
350         list_for_each_entry(z, &state->zones, list)
351         {
352                 list_for_each_entry(d, &z->devices, list)
353                 {
354                         if (!strcmp(d->name, dev))
355                         {
356                                 printf("%s\n", z->name);
357                                 return 0;
358                         }
359                 }
360         }
361
362         return 1;
363 }
364
365 static int
366 usage(void)
367 {
368         fprintf(stderr, "fw3 [-4] [-6] [-q] {start|stop|flush|reload|restart|print}\n");
369         fprintf(stderr, "fw3 [-q] network {net}\n");
370         fprintf(stderr, "fw3 [-q] device {dev}\n");
371
372         return 1;
373 }
374
375
376 int main(int argc, char **argv)
377 {
378         int ch, rv = 1;
379         struct fw3_state *state = NULL;
380         struct fw3_defaults *defs = NULL;
381
382         while ((ch = getopt(argc, argv, "46dqh")) != -1)
383         {
384                 switch (ch)
385                 {
386                 case '4':
387                         use_family = FW3_FAMILY_V4;
388                         break;
389
390                 case '6':
391                         use_family = FW3_FAMILY_V6;
392                         break;
393
394                 case 'd':
395                         fw3_pr_debug = true;
396                         break;
397
398                 case 'q':
399                         freopen("/dev/null", "w", stderr);
400                         break;
401
402                 case 'h':
403                         rv = usage();
404                         goto out;
405                 }
406         }
407
408         state = build_state();
409         defs = &state->defaults;
410
411         if (!fw3_lock())
412                 goto out;
413
414         if (optind >= argc)
415         {
416                 rv = usage();
417                 goto out;
418         }
419
420         if (use_family == FW3_FAMILY_V6 && defs->disable_ipv6)
421                 warn("IPv6 rules globally disabled in configuration");
422
423         if (!strcmp(argv[optind], "print"))
424         {
425                 if (use_family == FW3_FAMILY_ANY)
426                         use_family = FW3_FAMILY_V4;
427
428                 freopen("/dev/null", "w", stderr);
429
430                 state->disable_ipsets = true;
431                 print_rules = true;
432
433                 rv = start(state, false);
434         }
435         else if (!strcmp(argv[optind], "start"))
436         {
437                 rv = start(state, false);
438         }
439         else if (!strcmp(argv[optind], "stop"))
440         {
441                 rv = stop(state, false, false);
442         }
443         else if (!strcmp(argv[optind], "flush"))
444         {
445                 rv = stop(state, true, false);
446         }
447         else if (!strcmp(argv[optind], "restart"))
448         {
449                 stop(state, true, false);
450                 free_state(state);
451
452                 state = build_state();
453                 rv = start(state, false);
454         }
455         else if (!strcmp(argv[optind], "reload"))
456         {
457                 rv = stop(state, false, true);
458                 rv = start(state, !rv);
459         }
460         else if (!strcmp(argv[optind], "network") && (optind + 1) < argc)
461         {
462                 rv = lookup_network(state, argv[optind + 1]);
463         }
464         else if (!strcmp(argv[optind], "device") && (optind + 1) < argc)
465         {
466                 rv = lookup_device(state, argv[optind + 1]);
467         }
468         else
469         {
470                 rv = usage();
471         }
472
473 out:
474         if (state)
475                 free_state(state);
476
477         fw3_unlock();
478
479         return rv;
480 }