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