rework ipset removal logic to only purge sets that are not in use by any family
[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 && fw3_command_pipe(false, "ipset", "-exist", "-"))
224 {
225 fw3_destroy_ipsets(state, statefile);
226 fw3_command_close();
227 }
228
229 fw3_free_statefile(statefile);
230
231 if (!rv)
232 fw3_write_statefile(state);
233
234 return rv;
235 }
236
237 static int
238 start(struct fw3_state *state, bool restart)
239 {
240 int rv = 1;
241 enum fw3_family family;
242 enum fw3_table table;
243
244 struct list_head *statefile = fw3_read_statefile();
245
246 if (!print_rules && !restart &&
247 fw3_command_pipe(false, "ipset", "-exist", "-"))
248 {
249 fw3_create_ipsets(state, statefile);
250 fw3_command_close();
251 }
252
253 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
254 {
255 if (!family_used(family))
256 continue;
257
258 if (!family_loaded(state, family) || !restore_pipe(family, false))
259 continue;
260
261 if (!print_rules && !restart && family_running(statefile, family))
262 {
263 warn("The %s firewall appears to be started already. "
264 "If it is indeed empty, remove the %s file and retry.",
265 fw3_flag_names[family], FW3_STATEFILE);
266
267 continue;
268 }
269
270 info("Constructing %s rules ...", fw3_flag_names[family]);
271
272 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
273 {
274 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
275 continue;
276
277 info(" * Populating %s table", fw3_flag_names[table]);
278
279 fw3_pr("*%s\n", fw3_flag_names[table]);
280 fw3_print_default_chains(table, family, state);
281 fw3_print_zone_chains(table, family, state);
282 fw3_print_default_head_rules(table, family, state);
283 fw3_print_rules(table, family, state);
284 fw3_print_redirects(table, family, state);
285 fw3_print_forwards(table, family, state);
286 fw3_print_zone_rules(table, family, state);
287 fw3_print_default_tail_rules(table, family, state);
288 fw3_pr("COMMIT\n");
289 }
290
291 fw3_command_close();
292 family_set(state, family, true);
293
294 rv = 0;
295 }
296
297 fw3_free_statefile(statefile);
298
299 if (!rv && !print_rules)
300 fw3_write_statefile(state);
301
302 return rv;
303 }
304
305 static int
306 lookup_network(struct fw3_state *state, const char *net)
307 {
308 struct fw3_zone *z;
309 struct fw3_device *d;
310
311 list_for_each_entry(z, &state->zones, list)
312 {
313 list_for_each_entry(d, &z->networks, list)
314 {
315 if (!strcmp(d->name, net))
316 {
317 printf("%s\n", z->name);
318 return 0;
319 }
320 }
321 }
322
323 return 1;
324 }
325
326 static int
327 lookup_device(struct fw3_state *state, const char *dev)
328 {
329 struct fw3_zone *z;
330 struct fw3_device *d;
331
332 list_for_each_entry(z, &state->zones, list)
333 {
334 list_for_each_entry(d, &z->devices, list)
335 {
336 if (!strcmp(d->name, dev))
337 {
338 printf("%s\n", z->name);
339 return 0;
340 }
341 }
342 }
343
344 return 1;
345 }
346
347 static int
348 usage(void)
349 {
350 fprintf(stderr, "fw3 [-4] [-6] [-q] {start|stop|flush|restart|print}\n");
351 fprintf(stderr, "fw3 [-q] network {net}\n");
352 fprintf(stderr, "fw3 [-q] device {dev}\n");
353
354 return 1;
355 }
356
357
358 int main(int argc, char **argv)
359 {
360 int ch, rv = 1;
361 struct fw3_state *state = NULL;
362 struct fw3_defaults *defs = NULL;
363
364 while ((ch = getopt(argc, argv, "46qh")) != -1)
365 {
366 switch (ch)
367 {
368 case '4':
369 use_family = FW3_FAMILY_V4;
370 break;
371
372 case '6':
373 use_family = FW3_FAMILY_V6;
374 break;
375
376 case 'q':
377 freopen("/dev/null", "w", stderr);
378 break;
379
380 case 'h':
381 rv = usage();
382 goto out;
383 }
384 }
385
386 if (!fw3_ubus_connect())
387 error("Failed to connect to ubus");
388
389 state = build_state();
390 defs = &state->defaults;
391
392 if (!fw3_lock())
393 goto out;
394
395 if (optind >= argc)
396 {
397 rv = usage();
398 goto out;
399 }
400
401 if (use_family == FW3_FAMILY_V6 && defs->disable_ipv6)
402 warn("IPv6 rules globally disabled in configuration");
403
404 if (!strcmp(argv[optind], "print"))
405 {
406 if (use_family == FW3_FAMILY_ANY)
407 use_family = FW3_FAMILY_V4;
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 }