2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
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.
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.
22 #define C(f, tbl, def, fmt) \
23 { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_FLAG_##def, fmt }
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"),
32 C(V4, NAT, CUSTOM_CHAINS, "prerouting_rule"),
33 C(V4, NAT, CUSTOM_CHAINS, "postrouting_rule"),
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),
43 FW3_OPT("drop_invalid", bool, defaults, drop_invalid),
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),
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),
54 FW3_OPT("accept_redirects", bool, defaults, accept_redirects),
55 FW3_OPT("accept_source_route", bool, defaults, accept_source_route),
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),
62 FW3_OPT("__flags_v4", int, defaults, flags[0]),
63 FW3_OPT("__flags_v6", int, defaults, flags[1]),
70 check_policy(struct uci_element *e, enum fw3_flag *pol, const char *name)
72 if (*pol == FW3_FLAG_UNSPEC)
74 warn_elem(e, "has no %s policy specified, defaulting to DROP", name);
77 else if (*pol > FW3_FLAG_DROP)
79 warn_elem(e, "has invalid %s policy, defaulting to DROP", name);
85 check_offloading(struct uci_element *e, bool *offloading)
92 f = fopen("/sys/module/xt_FLOWOFFLOAD/refcnt", "r");
100 warn_elem(e, "enables offloading but missing kernel support, disabling");
105 fw3_load_defaults(struct fw3_state *state, struct uci_package *p)
107 struct uci_section *s;
108 struct uci_element *e;
109 struct fw3_defaults *defs = &state->defaults;
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;
120 uci_foreach_element(&p->sections, e)
122 s = uci_to_section(e);
124 if (strcmp(s->type, "defaults"))
129 warn_elem(e, "ignoring duplicate section");
133 if(!fw3_parse_options(&state->defaults, fw3_flag_opts, s))
134 warn_elem(e, "has invalid options");
136 check_policy(e, &defs->policy_input, "input");
137 check_policy(e, &defs->policy_output, "output");
138 check_policy(e, &defs->policy_forward, "forward");
140 check_offloading(e, &defs->flow_offloading);
145 fw3_print_default_chains(struct fw3_ipt_handle *handle, struct fw3_state *state,
148 struct fw3_defaults *defs = &state->defaults;
149 const struct fw3_chain_spec *c;
152 ((t == FW3_FLAG_REJECT) ? FW3_FLAG_DROP : t)
154 if (handle->family == FW3_FAMILY_V6 && defs->disable_ipv6)
157 if (handle->table == FW3_TABLE_FILTER)
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));
164 if (defs->custom_chains)
165 set(defs->flags, handle->family, FW3_FLAG_CUSTOM_CHAINS);
168 set(defs->flags, handle->family, FW3_FLAG_SYN_FLOOD);
170 for (c = default_chains; c->format; c++)
172 /* don't touch user chains on selective stop */
173 if (reload && c->flag == FW3_FLAG_CUSTOM_CHAINS)
176 if (!fw3_is_family(c, handle->family))
179 if (c->table != handle->table)
183 !fw3_hasbit(defs->flags[handle->family == FW3_FAMILY_V6], c->flag))
186 fw3_ipt_create_chain(handle, c->format);
189 set(defs->flags, handle->family, handle->table);
193 fw3_print_default_head_rules(struct fw3_ipt_handle *handle,
194 struct fw3_state *state, bool reload)
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;
202 const char *chains[] = {
205 "FORWARD", "forwarding",
208 switch (handle->table)
210 case FW3_TABLE_FILTER:
212 sprintf(lodev.name, "lo");
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");
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");
222 if (defs->custom_chains)
224 for (i = 0; i < ARRAY_SIZE(chains); i += 2)
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]);
233 if (defs->flow_offloading)
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");
242 for (i = 0; i < ARRAY_SIZE(chains); i += 2)
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]);
249 if (defs->drop_invalid)
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]);
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");
266 r = fw3_ipt_rule_new(handle);
267 fw3_ipt_rule_target(r, "DROP");
268 fw3_ipt_rule_append(r, "syn_flood");
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");
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");
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");
289 if (defs->custom_chains)
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");
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");
309 fw3_print_default_tail_rules(struct fw3_ipt_handle *handle,
310 struct fw3_state *state, bool reload)
312 struct fw3_defaults *defs = &state->defaults;
313 struct fw3_ipt_rule *r;
315 if (handle->table != FW3_TABLE_FILTER)
318 if (defs->policy_input == FW3_FLAG_REJECT)
320 r = fw3_ipt_rule_new(handle);
325 fw3_ipt_rule_target(r, "reject");
326 fw3_ipt_rule_append(r, "INPUT");
329 if (defs->policy_output == FW3_FLAG_REJECT)
331 r = fw3_ipt_rule_new(handle);
336 fw3_ipt_rule_target(r, "reject");
337 fw3_ipt_rule_append(r, "OUTPUT");
340 if (defs->policy_forward == FW3_FLAG_REJECT)
342 r = fw3_ipt_rule_new(handle);
347 fw3_ipt_rule_target(r, "reject");
348 fw3_ipt_rule_append(r, "FORWARD");
353 set_default(const char *name, int set)
356 char path[sizeof("/proc/sys/net/ipv4/tcp_window_scaling\0")];
358 snprintf(path, sizeof(path), "/proc/sys/net/ipv4/tcp_%s", name);
360 info(" * Set tcp_%s to %s", name, set ? "on" : "off", name);
362 if (!(f = fopen(path, "w")))
364 info(" ! Unable to write value: %s", strerror(errno));
368 fprintf(f, "%u\n", set);
373 fw3_set_defaults(struct fw3_state *state)
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);
381 fw3_flush_rules(struct fw3_ipt_handle *handle, struct fw3_state *state,
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;
388 if (!has(defs->flags, handle->family, handle->table))
391 if (handle->table == FW3_TABLE_FILTER)
393 fw3_ipt_set_policy(handle, "INPUT", policy);
394 fw3_ipt_set_policy(handle, "OUTPUT", policy);
395 fw3_ipt_set_policy(handle, "FORWARD", policy);
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");
404 for (c = default_chains; c->format; c++)
406 /* don't touch user chains on selective stop */
407 if (reload && c->flag == FW3_FLAG_CUSTOM_CHAINS)
410 if (!fw3_is_family(c, handle->family))
413 if (c->table != handle->table)
416 if (c->flag && !has(defs->flags, handle->family, c->flag))
419 fw3_ipt_flush_chain(handle, c->format);
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)
426 fw3_ipt_delete_chain(handle, c->format);
429 del(defs->flags, handle->family, handle->table);
433 fw3_flush_all(struct fw3_ipt_handle *handle)
435 if (handle->table == FW3_TABLE_FILTER)
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);
442 fw3_ipt_flush(handle);