Reword rule comments
[project/firewall3.git] / defaults.c
1 /*
2 * firewall3 - 3rd OpenWrt UCI firewall implementation
3 *
4 * Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
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 "defaults.h"
20
21
22 #define C(f, tbl, def, fmt) \
23 { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_FLAG_##def, fmt }
24
25 static const struct fw3_chain_spec default_chains[] = {
26 C(ANY, FILTER, UNSPEC, "reject"),
27 C(ANY, FILTER, CUSTOM_CHAINS, "input_rule"),
28 C(ANY, FILTER, CUSTOM_CHAINS, "output_rule"),
29 C(ANY, FILTER, CUSTOM_CHAINS, "forwarding_rule"),
30 C(ANY, FILTER, SYN_FLOOD, "syn_flood"),
31
32 C(V4, NAT, CUSTOM_CHAINS, "prerouting_rule"),
33 C(V4, NAT, CUSTOM_CHAINS, "postrouting_rule"),
34
35 { }
36 };
37
38 const struct fw3_option fw3_flag_opts[] = {
39 FW3_OPT("input", target, defaults, policy_input),
40 FW3_OPT("forward", target, defaults, policy_forward),
41 FW3_OPT("output", target, defaults, policy_output),
42
43 FW3_OPT("drop_invalid", bool, defaults, drop_invalid),
44
45 FW3_OPT("syn_flood", bool, defaults, syn_flood),
46 FW3_OPT("synflood_protect", bool, defaults, syn_flood),
47 FW3_OPT("synflood_rate", limit, defaults, syn_flood_rate),
48 FW3_OPT("synflood_burst", int, defaults, syn_flood_rate.burst),
49
50 FW3_OPT("tcp_syncookies", bool, defaults, tcp_syncookies),
51 FW3_OPT("tcp_ecn", int, defaults, tcp_ecn),
52 FW3_OPT("tcp_window_scaling", bool, defaults, tcp_window_scaling),
53
54 FW3_OPT("accept_redirects", bool, defaults, accept_redirects),
55 FW3_OPT("accept_source_route", bool, defaults, accept_source_route),
56
57 FW3_OPT("auto_helper", bool, defaults, auto_helper),
58 FW3_OPT("custom_chains", bool, defaults, custom_chains),
59 FW3_OPT("disable_ipv6", bool, defaults, disable_ipv6),
60 FW3_OPT("flow_offloading", bool, defaults, flow_offloading),
61
62 FW3_OPT("__flags_v4", int, defaults, flags[0]),
63 FW3_OPT("__flags_v6", int, defaults, flags[1]),
64
65 { }
66 };
67
68
69 static void
70 check_policy(struct uci_element *e, enum fw3_flag *pol, const char *name)
71 {
72 if (*pol == FW3_FLAG_UNSPEC)
73 {
74 warn_elem(e, "has no %s policy specified, defaulting to DROP", name);
75 *pol = FW3_FLAG_DROP;
76 }
77 else if (*pol > FW3_FLAG_DROP)
78 {
79 warn_elem(e, "has invalid %s policy, defaulting to DROP", name);
80 *pol = FW3_FLAG_DROP;
81 }
82 }
83
84 static void
85 check_offloading(struct uci_element *e, bool *offloading)
86 {
87 FILE *f;
88
89 if (!*offloading)
90 return;
91
92 f = fopen("/sys/module/xt_FLOWOFFLOAD/refcnt", "r");
93
94 if (f)
95 {
96 fclose(f);
97 return;
98 }
99
100 warn_elem(e, "enables offloading but missing kernel support, disabling");
101 *offloading = false;
102 }
103
104 void
105 fw3_load_defaults(struct fw3_state *state, struct uci_package *p)
106 {
107 struct uci_section *s;
108 struct uci_element *e;
109 struct fw3_defaults *defs = &state->defaults;
110
111 bool seen = false;
112
113 defs->syn_flood_rate.rate = 25;
114 defs->syn_flood_rate.burst = 50;
115 defs->tcp_syncookies = true;
116 defs->tcp_window_scaling = true;
117 defs->custom_chains = true;
118 defs->auto_helper = true;
119
120 uci_foreach_element(&p->sections, e)
121 {
122 s = uci_to_section(e);
123
124 if (strcmp(s->type, "defaults"))
125 continue;
126
127 if (seen)
128 {
129 warn_elem(e, "ignoring duplicate section");
130 continue;
131 }
132
133 if(!fw3_parse_options(&state->defaults, fw3_flag_opts, s))
134 warn_elem(e, "has invalid options");
135
136 check_policy(e, &defs->policy_input, "input");
137 check_policy(e, &defs->policy_output, "output");
138 check_policy(e, &defs->policy_forward, "forward");
139
140 check_offloading(e, &defs->flow_offloading);
141 }
142 }
143
144 void
145 fw3_print_default_chains(struct fw3_ipt_handle *handle, struct fw3_state *state,
146 bool reload)
147 {
148 struct fw3_defaults *defs = &state->defaults;
149 const struct fw3_chain_spec *c;
150
151 #define policy(t) \
152 ((t == FW3_FLAG_REJECT) ? FW3_FLAG_DROP : t)
153
154 if (handle->family == FW3_FAMILY_V6 && defs->disable_ipv6)
155 return;
156
157 if (handle->table == FW3_TABLE_FILTER)
158 {
159 fw3_ipt_set_policy(handle, "INPUT", policy(defs->policy_input));
160 fw3_ipt_set_policy(handle, "OUTPUT", policy(defs->policy_output));
161 fw3_ipt_set_policy(handle, "FORWARD", policy(defs->policy_forward));
162 }
163
164 if (defs->custom_chains)
165 set(defs->flags, handle->family, FW3_FLAG_CUSTOM_CHAINS);
166
167 if (defs->syn_flood)
168 set(defs->flags, handle->family, FW3_FLAG_SYN_FLOOD);
169
170 for (c = default_chains; c->format; c++)
171 {
172 /* don't touch user chains on selective stop */
173 if (reload && c->flag == FW3_FLAG_CUSTOM_CHAINS)
174 continue;
175
176 if (!fw3_is_family(c, handle->family))
177 continue;
178
179 if (c->table != handle->table)
180 continue;
181
182 if (c->flag &&
183 !fw3_hasbit(defs->flags[handle->family == FW3_FAMILY_V6], c->flag))
184 continue;
185
186 fw3_ipt_create_chain(handle, c->format);
187 }
188
189 set(defs->flags, handle->family, handle->table);
190 }
191
192 void
193 fw3_print_default_head_rules(struct fw3_ipt_handle *handle,
194 struct fw3_state *state, bool reload)
195 {
196 int i;
197 struct fw3_defaults *defs = &state->defaults;
198 struct fw3_device lodev = { .set = true };
199 struct fw3_protocol tcp = { .protocol = 6 };
200 struct fw3_ipt_rule *r;
201
202 const char *chains[] = {
203 "INPUT", "input",
204 "OUTPUT", "output",
205 "FORWARD", "forwarding",
206 };
207
208 switch (handle->table)
209 {
210 case FW3_TABLE_FILTER:
211
212 sprintf(lodev.name, "lo");
213
214 r = fw3_ipt_rule_create(handle, NULL, &lodev, NULL, NULL, NULL);
215 fw3_ipt_rule_target(r, "ACCEPT");
216 fw3_ipt_rule_append(r, "INPUT");
217
218 r = fw3_ipt_rule_create(handle, NULL, NULL, &lodev, NULL, NULL);
219 fw3_ipt_rule_target(r, "ACCEPT");
220 fw3_ipt_rule_append(r, "OUTPUT");
221
222 if (defs->custom_chains)
223 {
224 for (i = 0; i < ARRAY_SIZE(chains); i += 2)
225 {
226 r = fw3_ipt_rule_new(handle);
227 fw3_ipt_rule_comment(r, "Custom %s rule chain", chains[i+1]);
228 fw3_ipt_rule_target(r, "%s_rule", chains[i+1]);
229 fw3_ipt_rule_append(r, chains[i]);
230 }
231 }
232
233 if (defs->flow_offloading)
234 {
235 r = fw3_ipt_rule_new(handle);
236 fw3_ipt_rule_comment(r, "Traffic offloading");
237 fw3_ipt_rule_extra(r, "-m conntrack --ctstate RELATED,ESTABLISHED");
238 fw3_ipt_rule_target(r, "FLOWOFFLOAD");
239 fw3_ipt_rule_append(r, "FORWARD");
240 }
241
242 for (i = 0; i < ARRAY_SIZE(chains); i += 2)
243 {
244 r = fw3_ipt_rule_new(handle);
245 fw3_ipt_rule_extra(r, "-m conntrack --ctstate RELATED,ESTABLISHED");
246 fw3_ipt_rule_target(r, "ACCEPT");
247 fw3_ipt_rule_append(r, chains[i]);
248
249 if (defs->drop_invalid)
250 {
251 r = fw3_ipt_rule_new(handle);
252 fw3_ipt_rule_extra(r, "-m conntrack --ctstate INVALID");
253 fw3_ipt_rule_target(r, "DROP");
254 fw3_ipt_rule_append(r, chains[i]);
255 }
256 }
257
258 if (defs->syn_flood)
259 {
260 r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
261 fw3_ipt_rule_extra(r, "--syn");
262 fw3_ipt_rule_limit(r, &defs->syn_flood_rate);
263 fw3_ipt_rule_target(r, "RETURN");
264 fw3_ipt_rule_append(r, "syn_flood");
265
266 r = fw3_ipt_rule_new(handle);
267 fw3_ipt_rule_target(r, "DROP");
268 fw3_ipt_rule_append(r, "syn_flood");
269
270 r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
271 fw3_ipt_rule_extra(r, "--syn");
272 fw3_ipt_rule_target(r, "syn_flood");
273 fw3_ipt_rule_append(r, "INPUT");
274 }
275
276 r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
277 fw3_ipt_rule_target(r, "REJECT");
278 fw3_ipt_rule_addarg(r, false, "--reject-with", "tcp-reset");
279 fw3_ipt_rule_append(r, "reject");
280
281 r = fw3_ipt_rule_new(handle);
282 fw3_ipt_rule_target(r, "REJECT");
283 fw3_ipt_rule_addarg(r, false, "--reject-with", "port-unreach");
284 fw3_ipt_rule_append(r, "reject");
285
286 break;
287
288 case FW3_TABLE_NAT:
289 if (defs->custom_chains)
290 {
291 r = fw3_ipt_rule_new(handle);
292 fw3_ipt_rule_comment(r, "Custom prerouting rule chain");
293 fw3_ipt_rule_target(r, "prerouting_rule");
294 fw3_ipt_rule_append(r, "PREROUTING");
295
296 r = fw3_ipt_rule_new(handle);
297 fw3_ipt_rule_comment(r, "Custom postrouting rule chain");
298 fw3_ipt_rule_target(r, "postrouting_rule");
299 fw3_ipt_rule_append(r, "POSTROUTING");
300 }
301 break;
302
303 default:
304 break;
305 }
306 }
307
308 void
309 fw3_print_default_tail_rules(struct fw3_ipt_handle *handle,
310 struct fw3_state *state, bool reload)
311 {
312 struct fw3_defaults *defs = &state->defaults;
313 struct fw3_ipt_rule *r;
314
315 if (handle->table != FW3_TABLE_FILTER)
316 return;
317
318 if (defs->policy_input == FW3_FLAG_REJECT)
319 {
320 r = fw3_ipt_rule_new(handle);
321
322 if (!r)
323 return;
324
325 fw3_ipt_rule_target(r, "reject");
326 fw3_ipt_rule_append(r, "INPUT");
327 }
328
329 if (defs->policy_output == FW3_FLAG_REJECT)
330 {
331 r = fw3_ipt_rule_new(handle);
332
333 if (!r)
334 return;
335
336 fw3_ipt_rule_target(r, "reject");
337 fw3_ipt_rule_append(r, "OUTPUT");
338 }
339
340 if (defs->policy_forward == FW3_FLAG_REJECT)
341 {
342 r = fw3_ipt_rule_new(handle);
343
344 if (!r)
345 return;
346
347 fw3_ipt_rule_target(r, "reject");
348 fw3_ipt_rule_append(r, "FORWARD");
349 }
350 }
351
352 static void
353 set_default(const char *name, int set)
354 {
355 FILE *f;
356 char path[sizeof("/proc/sys/net/ipv4/tcp_window_scaling\0")];
357
358 snprintf(path, sizeof(path), "/proc/sys/net/ipv4/tcp_%s", name);
359
360 info(" * Set tcp_%s to %s", name, set ? "on" : "off", name);
361
362 if (!(f = fopen(path, "w")))
363 {
364 info(" ! Unable to write value: %s", strerror(errno));
365 return;
366 }
367
368 fprintf(f, "%u\n", set);
369 fclose(f);
370 }
371
372 void
373 fw3_set_defaults(struct fw3_state *state)
374 {
375 set_default("ecn", state->defaults.tcp_ecn);
376 set_default("syncookies", state->defaults.tcp_syncookies);
377 set_default("window_scaling", state->defaults.tcp_window_scaling);
378 }
379
380 void
381 fw3_flush_rules(struct fw3_ipt_handle *handle, struct fw3_state *state,
382 bool reload)
383 {
384 enum fw3_flag policy = reload ? FW3_FLAG_DROP : FW3_FLAG_ACCEPT;
385 struct fw3_defaults *defs = &state->defaults;
386 const struct fw3_chain_spec *c;
387
388 if (!has(defs->flags, handle->family, handle->table))
389 return;
390
391 if (handle->table == FW3_TABLE_FILTER)
392 {
393 fw3_ipt_set_policy(handle, "INPUT", policy);
394 fw3_ipt_set_policy(handle, "OUTPUT", policy);
395 fw3_ipt_set_policy(handle, "FORWARD", policy);
396 }
397
398 fw3_ipt_delete_id_rules(handle, "INPUT");
399 fw3_ipt_delete_id_rules(handle, "OUTPUT");
400 fw3_ipt_delete_id_rules(handle, "FORWARD");
401 fw3_ipt_delete_id_rules(handle, "PREROUTING");
402 fw3_ipt_delete_id_rules(handle, "POSTROUTING");
403
404 for (c = default_chains; c->format; c++)
405 {
406 /* don't touch user chains on selective stop */
407 if (reload && c->flag == FW3_FLAG_CUSTOM_CHAINS)
408 continue;
409
410 if (!fw3_is_family(c, handle->family))
411 continue;
412
413 if (c->table != handle->table)
414 continue;
415
416 if (c->flag && !has(defs->flags, handle->family, c->flag))
417 continue;
418
419 fw3_ipt_flush_chain(handle, c->format);
420
421 /* keep certain basic chains that do not depend on any settings to
422 avoid purging unrelated user rules pointing to them */
423 if (reload && !c->flag)
424 continue;
425
426 fw3_ipt_delete_chain(handle, c->format);
427 }
428
429 del(defs->flags, handle->family, handle->table);
430 }
431
432 void
433 fw3_flush_all(struct fw3_ipt_handle *handle)
434 {
435 if (handle->table == FW3_TABLE_FILTER)
436 {
437 fw3_ipt_set_policy(handle, "INPUT", FW3_FLAG_ACCEPT);
438 fw3_ipt_set_policy(handle, "OUTPUT", FW3_FLAG_ACCEPT);
439 fw3_ipt_set_policy(handle, "FORWARD", FW3_FLAG_ACCEPT);
440 }
441
442 fw3_ipt_flush(handle);
443 }