d4d7b7b6aa577539faaa8859158170ac38ac0884
[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 has(state->defaults.flags, family, 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 void
148 family_set(struct fw3_state *state, enum fw3_family family, bool set)
149 {
150         if (set)
151                 set(state->defaults.flags, family, family);
152         else
153                 del(state->defaults.flags, family, family);
154 }
155
156 static int
157 stop(struct fw3_state *state, bool complete, bool reload)
158 {
159         FILE *ct;
160
161         int rv = 1;
162         enum fw3_family family;
163         enum fw3_table table;
164
165         if (!complete && !state->statefile)
166         {
167                 if (!reload)
168                         warn("The firewall appears to be stopped. "
169                                  "Use the 'flush' command to forcefully purge all rules.");
170
171                 return rv;
172         }
173
174         if (!print_rules)
175                 fw3_hotplug_zones(state, false);
176
177         for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
178         {
179                 if (!complete && !family_running(state, family))
180                         continue;
181
182                 if (!family_used(family) || !restore_pipe(family, true))
183                         continue;
184
185                 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
186                 {
187                         if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
188                                 continue;
189
190                         info(" * %sing %s %s table", complete ? "Flush" : "Clear",
191                              fw3_flag_names[family], fw3_flag_names[table]);
192
193                         fw3_pr("*%s\n", fw3_flag_names[table]);
194
195                         if (complete)
196                         {
197                                 fw3_flush_all(table);
198                         }
199                         else
200                         {
201                                 /* pass 1 */
202                                 fw3_flush_rules(state, family, table, reload, false);
203                                 fw3_flush_zones(state, family, table, reload, false);
204
205                                 /* pass 2 */
206                                 fw3_flush_rules(state, family, table, reload, true);
207                                 fw3_flush_zones(state, family, table, reload, true);
208                         }
209
210                         fw3_pr("COMMIT\n");
211                 }
212
213                 fw3_command_close();
214
215                 if (!reload)
216                 {
217                         if (fw3_command_pipe(false, "ipset", "-exist", "-"))
218                         {
219                                 fw3_destroy_ipsets(state, family);
220                                 fw3_command_close();
221                         }
222
223                         family_set(state, family, false);
224                 }
225
226                 rv = 0;
227         }
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)
238                 fw3_write_statefile(state);
239
240         return rv;
241 }
242
243 static int
244 start(struct fw3_state *state, bool reload)
245 {
246         int rv = 1;
247         enum fw3_family family;
248         enum fw3_table table;
249
250         if (!print_rules && !reload)
251         {
252                 if (fw3_command_pipe(false, "ipset", "-exist", "-"))
253                 {
254                         fw3_create_ipsets(state);
255                         fw3_command_close();
256                 }
257         }
258
259         for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
260         {
261                 if (!family_used(family))
262                         continue;
263
264                 if (!print_rules && !reload && family_running(state, family))
265                 {
266                         warn("The %s firewall appears to be started already. "
267                              "If it is indeed empty, remove the %s file and retry.",
268                              fw3_flag_names[family], FW3_STATEFILE);
269
270                         continue;
271                 }
272
273                 if (!restore_pipe(family, false))
274                         continue;
275
276                 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
277                 {
278                         if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
279                                 continue;
280
281                         info(" * Populating %s %s table",
282                              fw3_flag_names[family], fw3_flag_names[table]);
283
284                         fw3_pr("*%s\n", fw3_flag_names[table]);
285                         fw3_print_default_chains(state, family, table, reload);
286                         fw3_print_zone_chains(state, family, table, reload);
287                         fw3_print_default_head_rules(state, family, table, reload);
288                         fw3_print_rules(state, family, table);
289                         fw3_print_redirects(state, family, table);
290                         fw3_print_forwards(state, family, table);
291                         fw3_print_zone_rules(state, family, table, reload);
292                         fw3_print_default_tail_rules(state, family, table, reload);
293                         fw3_pr("COMMIT\n");
294                 }
295
296                 fw3_print_includes(state, family, reload);
297
298                 fw3_command_close();
299                 family_set(state, family, true);
300
301                 rv = 0;
302         }
303
304         if (!rv)
305         {
306                 fw3_set_defaults(state);
307
308                 if (!print_rules)
309                 {
310                         fw3_run_includes(state, reload);
311                         fw3_hotplug_zones(state, true);
312                         fw3_write_statefile(state);
313                 }
314         }
315
316         return rv;
317 }
318
319 static int
320 lookup_network(struct fw3_state *state, const char *net)
321 {
322         struct fw3_zone *z;
323         struct fw3_device *d;
324
325         list_for_each_entry(z, &state->zones, list)
326         {
327                 list_for_each_entry(d, &z->networks, list)
328                 {
329                         if (!strcmp(d->name, net))
330                         {
331                                 printf("%s\n", z->name);
332                                 return 0;
333                         }
334                 }
335         }
336
337         return 1;
338 }
339
340 static int
341 lookup_device(struct fw3_state *state, const char *dev)
342 {
343         struct fw3_zone *z;
344         struct fw3_device *d;
345
346         list_for_each_entry(z, &state->zones, list)
347         {
348                 list_for_each_entry(d, &z->devices, list)
349                 {
350                         if (!strcmp(d->name, dev))
351                         {
352                                 printf("%s\n", z->name);
353                                 return 0;
354                         }
355                 }
356         }
357
358         return 1;
359 }
360
361 static int
362 usage(void)
363 {
364         fprintf(stderr, "fw3 [-4] [-6] [-q] {start|stop|flush|reload|restart|print}\n");
365         fprintf(stderr, "fw3 [-q] network {net}\n");
366         fprintf(stderr, "fw3 [-q] device {dev}\n");
367
368         return 1;
369 }
370
371
372 int main(int argc, char **argv)
373 {
374         int ch, rv = 1;
375         struct fw3_state *state = NULL;
376         struct fw3_defaults *defs = NULL;
377
378         while ((ch = getopt(argc, argv, "46dqh")) != -1)
379         {
380                 switch (ch)
381                 {
382                 case '4':
383                         use_family = FW3_FAMILY_V4;
384                         break;
385
386                 case '6':
387                         use_family = FW3_FAMILY_V6;
388                         break;
389
390                 case 'd':
391                         fw3_pr_debug = true;
392                         break;
393
394                 case 'q':
395                         freopen("/dev/null", "w", stderr);
396                         break;
397
398                 case 'h':
399                         rv = usage();
400                         goto out;
401                 }
402         }
403
404         state = build_state();
405         defs = &state->defaults;
406
407         if (optind >= argc)
408         {
409                 rv = usage();
410                 goto out;
411         }
412
413         if (use_family == FW3_FAMILY_V6 && defs->disable_ipv6)
414                 warn("IPv6 rules globally disabled in configuration");
415
416         if (!strcmp(argv[optind], "print"))
417         {
418                 if (use_family == FW3_FAMILY_ANY)
419                         use_family = FW3_FAMILY_V4;
420
421                 freopen("/dev/null", "w", stderr);
422
423                 state->disable_ipsets = true;
424                 print_rules = true;
425
426                 rv = start(state, false);
427         }
428         else if (!strcmp(argv[optind], "start"))
429         {
430                 if (fw3_lock())
431                 {
432                         rv = start(state, false);
433                         fw3_unlock();
434                 }
435         }
436         else if (!strcmp(argv[optind], "stop"))
437         {
438                 if (fw3_lock())
439                 {
440                         rv = stop(state, false, false);
441                         fw3_unlock();
442                 }
443         }
444         else if (!strcmp(argv[optind], "flush"))
445         {
446                 if (fw3_lock())
447                 {
448                         rv = stop(state, true, false);
449                         fw3_unlock();
450                 }
451         }
452         else if (!strcmp(argv[optind], "restart"))
453         {
454                 if (fw3_lock())
455                 {
456                         stop(state, true, false);
457                         free_state(state);
458
459                         state = build_state();
460                         rv = start(state, false);
461
462                         fw3_unlock();
463                 }
464         }
465         else if (!strcmp(argv[optind], "reload"))
466         {
467                 if (fw3_lock())
468                 {
469                         rv = stop(state, false, true);
470                         rv = start(state, !rv);
471
472                         fw3_unlock();
473                 }
474         }
475         else if (!strcmp(argv[optind], "network") && (optind + 1) < argc)
476         {
477                 rv = lookup_network(state, argv[optind + 1]);
478         }
479         else if (!strcmp(argv[optind], "device") && (optind + 1) < argc)
480         {
481                 rv = lookup_device(state, argv[optind + 1]);
482         }
483         else
484         {
485                 rv = usage();
486         }
487
488 out:
489         if (state)
490                 free_state(state);
491
492         return rv;
493 }