defaults: add support for xt_FLOWOFFLOAD rule
[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, "user chain for %s", 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_extra(r, "-m conntrack --ctstate RELATED,ESTABLISHED");
237 fw3_ipt_rule_target(r, "FLOWOFFLOAD");
238 fw3_ipt_rule_append(r, "FORWARD");
239 }
240
241 for (i = 0; i < ARRAY_SIZE(chains); i += 2)
242 {
243 r = fw3_ipt_rule_new(handle);
244 fw3_ipt_rule_extra(r, "-m conntrack --ctstate RELATED,ESTABLISHED");
245 fw3_ipt_rule_target(r, "ACCEPT");
246 fw3_ipt_rule_append(r, chains[i]);
247
248 if (defs->drop_invalid)
249 {
250 r = fw3_ipt_rule_new(handle);
251 fw3_ipt_rule_extra(r, "-m conntrack --ctstate INVALID");
252 fw3_ipt_rule_target(r, "DROP");
253 fw3_ipt_rule_append(r, chains[i]);
254 }
255 }
256
257 if (defs->syn_flood)
258 {
259 r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
260 fw3_ipt_rule_extra(r, "--syn");
261 fw3_ipt_rule_limit(r, &defs->syn_flood_rate);
262 fw3_ipt_rule_target(r, "RETURN");
263 fw3_ipt_rule_append(r, "syn_flood");
264
265 r = fw3_ipt_rule_new(handle);
266 fw3_ipt_rule_target(r, "DROP");
267 fw3_ipt_rule_append(r, "syn_flood");
268
269 r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
270 fw3_ipt_rule_extra(r, "--syn");
271 fw3_ipt_rule_target(r, "syn_flood");
272 fw3_ipt_rule_append(r, "INPUT");
273 }
274
275 r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
276 fw3_ipt_rule_target(r, "REJECT");
277 fw3_ipt_rule_addarg(r, false, "--reject-with", "tcp-reset");
278 fw3_ipt_rule_append(r, "reject");
279
280 r = fw3_ipt_rule_new(handle);
281 fw3_ipt_rule_target(r, "REJECT");
282 fw3_ipt_rule_addarg(r, false, "--reject-with", "port-unreach");
283 fw3_ipt_rule_append(r, "reject");
284
285 break;
286
287 case FW3_TABLE_NAT:
288 if (defs->custom_chains)
289 {
290 r = fw3_ipt_rule_new(handle);
291 fw3_ipt_rule_comment(r, "user chain for prerouting");
292 fw3_ipt_rule_target(r, "prerouting_rule");
293 fw3_ipt_rule_append(r, "PREROUTING");
294
295 r = fw3_ipt_rule_new(handle);
296 fw3_ipt_rule_comment(r, "user chain for postrouting");
297 fw3_ipt_rule_target(r, "postrouting_rule");
298 fw3_ipt_rule_append(r, "POSTROUTING");
299 }
300 break;
301
302 default:
303 break;
304 }
305 }
306
307 void
308 fw3_print_default_tail_rules(struct fw3_ipt_handle *handle,
309 struct fw3_state *state, bool reload)
310 {
311 struct fw3_defaults *defs = &state->defaults;
312 struct fw3_ipt_rule *r;
313
314 if (handle->table != FW3_TABLE_FILTER)
315 return;
316
317 if (defs->policy_input == FW3_FLAG_REJECT)
318 {
319 r = fw3_ipt_rule_new(handle);
320
321 if (!r)
322 return;
323
324 fw3_ipt_rule_target(r, "reject");
325 fw3_ipt_rule_append(r, "INPUT");
326 }
327
328 if (defs->policy_output == FW3_FLAG_REJECT)
329 {
330 r = fw3_ipt_rule_new(handle);
331
332 if (!r)
333 return;
334
335 fw3_ipt_rule_target(r, "reject");
336 fw3_ipt_rule_append(r, "OUTPUT");
337 }
338
339 if (defs->policy_forward == FW3_FLAG_REJECT)
340 {
341 r = fw3_ipt_rule_new(handle);
342
343 if (!r)
344 return;
345
346 fw3_ipt_rule_target(r, "reject");
347 fw3_ipt_rule_append(r, "FORWARD");
348 }
349 }
350
351 static void
352 set_default(const char *name, int set)
353 {
354 FILE *f;
355 char path[sizeof("/proc/sys/net/ipv4/tcp_window_scaling\0")];
356
357 snprintf(path, sizeof(path), "/proc/sys/net/ipv4/tcp_%s", name);
358
359 info(" * Set tcp_%s to %s", name, set ? "on" : "off", name);
360
361 if (!(f = fopen(path, "w")))
362 {
363 info(" ! Unable to write value: %s", strerror(errno));
364 return;
365 }
366
367 fprintf(f, "%u\n", set);
368 fclose(f);
369 }
370
371 void
372 fw3_set_defaults(struct fw3_state *state)
373 {
374 set_default("ecn", state->defaults.tcp_ecn);
375 set_default("syncookies", state->defaults.tcp_syncookies);
376 set_default("window_scaling", state->defaults.tcp_window_scaling);
377 }
378
379 void
380 fw3_flush_rules(struct fw3_ipt_handle *handle, struct fw3_state *state,
381 bool reload)
382 {
383 enum fw3_flag policy = reload ? FW3_FLAG_DROP : FW3_FLAG_ACCEPT;
384 struct fw3_defaults *defs = &state->defaults;
385 const struct fw3_chain_spec *c;
386
387 if (!has(defs->flags, handle->family, handle->table))
388 return;
389
390 if (handle->table == FW3_TABLE_FILTER)
391 {
392 fw3_ipt_set_policy(handle, "INPUT", policy);
393 fw3_ipt_set_policy(handle, "OUTPUT", policy);
394 fw3_ipt_set_policy(handle, "FORWARD", policy);
395 }
396
397 fw3_ipt_delete_id_rules(handle, "INPUT");
398 fw3_ipt_delete_id_rules(handle, "OUTPUT");
399 fw3_ipt_delete_id_rules(handle, "FORWARD");
400 fw3_ipt_delete_id_rules(handle, "PREROUTING");
401 fw3_ipt_delete_id_rules(handle, "POSTROUTING");
402
403 for (c = default_chains; c->format; c++)
404 {
405 /* don't touch user chains on selective stop */
406 if (reload && c->flag == FW3_FLAG_CUSTOM_CHAINS)
407 continue;
408
409 if (!fw3_is_family(c, handle->family))
410 continue;
411
412 if (c->table != handle->table)
413 continue;
414
415 if (c->flag && !has(defs->flags, handle->family, c->flag))
416 continue;
417
418 fw3_ipt_flush_chain(handle, c->format);
419
420 /* keep certain basic chains that do not depend on any settings to
421 avoid purging unrelated user rules pointing to them */
422 if (reload && !c->flag)
423 continue;
424
425 fw3_ipt_delete_chain(handle, c->format);
426 }
427
428 del(defs->flags, handle->family, handle->table);
429 }
430
431 void
432 fw3_flush_all(struct fw3_ipt_handle *handle)
433 {
434 if (handle->table == FW3_TABLE_FILTER)
435 {
436 fw3_ipt_set_policy(handle, "INPUT", FW3_FLAG_ACCEPT);
437 fw3_ipt_set_policy(handle, "OUTPUT", FW3_FLAG_ACCEPT);
438 fw3_ipt_set_policy(handle, "FORWARD", FW3_FLAG_ACCEPT);
439 }
440
441 fw3_ipt_flush(handle);
442 }