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
, "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
]);
233 if (defs
->flow_offloading
)
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");
241 for (i
= 0; i
< ARRAY_SIZE(chains
); i
+= 2)
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
]);
248 if (defs
->drop_invalid
)
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
]);
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");
265 r
= fw3_ipt_rule_new(handle
);
266 fw3_ipt_rule_target(r
, "DROP");
267 fw3_ipt_rule_append(r
, "syn_flood");
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");
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");
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");
288 if (defs
->custom_chains
)
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");
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");
308 fw3_print_default_tail_rules(struct fw3_ipt_handle
*handle
,
309 struct fw3_state
*state
, bool reload
)
311 struct fw3_defaults
*defs
= &state
->defaults
;
312 struct fw3_ipt_rule
*r
;
314 if (handle
->table
!= FW3_TABLE_FILTER
)
317 if (defs
->policy_input
== FW3_FLAG_REJECT
)
319 r
= fw3_ipt_rule_new(handle
);
324 fw3_ipt_rule_target(r
, "reject");
325 fw3_ipt_rule_append(r
, "INPUT");
328 if (defs
->policy_output
== FW3_FLAG_REJECT
)
330 r
= fw3_ipt_rule_new(handle
);
335 fw3_ipt_rule_target(r
, "reject");
336 fw3_ipt_rule_append(r
, "OUTPUT");
339 if (defs
->policy_forward
== FW3_FLAG_REJECT
)
341 r
= fw3_ipt_rule_new(handle
);
346 fw3_ipt_rule_target(r
, "reject");
347 fw3_ipt_rule_append(r
, "FORWARD");
352 set_default(const char *name
, int set
)
355 char path
[sizeof("/proc/sys/net/ipv4/tcp_window_scaling\0")];
357 snprintf(path
, sizeof(path
), "/proc/sys/net/ipv4/tcp_%s", name
);
359 info(" * Set tcp_%s to %s", name
, set
? "on" : "off", name
);
361 if (!(f
= fopen(path
, "w")))
363 info(" ! Unable to write value: %s", strerror(errno
));
367 fprintf(f
, "%u\n", set
);
372 fw3_set_defaults(struct fw3_state
*state
)
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
);
380 fw3_flush_rules(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
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
;
387 if (!has(defs
->flags
, handle
->family
, handle
->table
))
390 if (handle
->table
== FW3_TABLE_FILTER
)
392 fw3_ipt_set_policy(handle
, "INPUT", policy
);
393 fw3_ipt_set_policy(handle
, "OUTPUT", policy
);
394 fw3_ipt_set_policy(handle
, "FORWARD", policy
);
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");
403 for (c
= default_chains
; c
->format
; c
++)
405 /* don't touch user chains on selective stop */
406 if (reload
&& c
->flag
== FW3_FLAG_CUSTOM_CHAINS
)
409 if (!fw3_is_family(c
, handle
->family
))
412 if (c
->table
!= handle
->table
)
415 if (c
->flag
&& !has(defs
->flags
, handle
->family
, c
->flag
))
418 fw3_ipt_flush_chain(handle
, c
->format
);
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
)
425 fw3_ipt_delete_chain(handle
, c
->format
);
428 del(defs
->flags
, handle
->family
, handle
->table
);
432 fw3_flush_all(struct fw3_ipt_handle
*handle
)
434 if (handle
->table
== FW3_TABLE_FILTER
)
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
);
441 fw3_ipt_flush(handle
);