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
),
44 FW3_OPT("tcp_reject_code", reject_code
, defaults
, tcp_reject_code
),
45 FW3_OPT("any_reject_code", reject_code
, defaults
, any_reject_code
),
47 FW3_OPT("syn_flood", bool, defaults
, syn_flood
),
48 FW3_OPT("synflood_protect", bool, defaults
, syn_flood
),
49 FW3_OPT("synflood_rate", limit
, defaults
, syn_flood_rate
),
50 FW3_OPT("synflood_burst", int, defaults
, syn_flood_rate
.burst
),
52 FW3_OPT("tcp_syncookies", bool, defaults
, tcp_syncookies
),
53 FW3_OPT("tcp_ecn", int, defaults
, tcp_ecn
),
54 FW3_OPT("tcp_window_scaling", bool, defaults
, tcp_window_scaling
),
56 FW3_OPT("accept_redirects", bool, defaults
, accept_redirects
),
57 FW3_OPT("accept_source_route", bool, defaults
, accept_source_route
),
59 FW3_OPT("auto_helper", bool, defaults
, auto_helper
),
60 FW3_OPT("custom_chains", bool, defaults
, custom_chains
),
61 FW3_OPT("disable_ipv6", bool, defaults
, disable_ipv6
),
62 FW3_OPT("flow_offloading", bool, defaults
, flow_offloading
),
63 FW3_OPT("flow_offloading_hw", bool, defaults
, flow_offloading_hw
),
65 FW3_OPT("__flags_v4", int, defaults
, flags
[0]),
66 FW3_OPT("__flags_v6", int, defaults
, flags
[1]),
73 check_policy(struct uci_element
*e
, enum fw3_flag
*pol
, const char *name
)
75 if (*pol
== FW3_FLAG_UNSPEC
)
77 warn_elem(e
, "has no %s policy specified, defaulting to DROP", name
);
80 else if (*pol
> FW3_FLAG_DROP
)
82 warn_elem(e
, "has invalid %s policy, defaulting to DROP", name
);
88 check_target(struct uci_element
*e
, bool *available
, const char *target
, const bool ipv6
)
90 const bool b
= fw3_has_target(ipv6
, target
);
93 warn_elem(e
, "requires unavailable target extension %s, disabling", target
);
99 check_any_reject_code(struct uci_element
*e
, enum fw3_reject_code
*any_reject_code
)
101 if (*any_reject_code
== FW3_REJECT_CODE_TCP_RESET
) {
102 warn_elem(e
, "tcp-reset not valid for any_reject_code, defaulting to port-unreach");
103 *any_reject_code
= FW3_REJECT_CODE_PORT_UNREACH
;
108 get_reject_code(enum fw3_family family
, enum fw3_reject_code reject_code
)
110 switch (reject_code
) {
111 case FW3_REJECT_CODE_TCP_RESET
:
113 case FW3_REJECT_CODE_PORT_UNREACH
:
114 return "port-unreach";
115 case FW3_REJECT_CODE_ADM_PROHIBITED
:
116 return family
== FW3_FAMILY_V6
? "adm-prohibited": "admin-prohib";
123 fw3_load_defaults(struct fw3_state
*state
, struct uci_package
*p
)
125 struct uci_section
*s
;
126 struct uci_element
*e
;
127 struct fw3_defaults
*defs
= &state
->defaults
;
129 bool flow_offload_avaliable
= false;
132 defs
->tcp_reject_code
= FW3_REJECT_CODE_TCP_RESET
;
133 defs
->any_reject_code
= FW3_REJECT_CODE_PORT_UNREACH
;
134 defs
->syn_flood_rate
.rate
= 25;
135 defs
->syn_flood_rate
.burst
= 50;
136 defs
->tcp_syncookies
= true;
137 defs
->tcp_window_scaling
= true;
138 defs
->custom_chains
= true;
139 defs
->auto_helper
= true;
141 uci_foreach_element(&p
->sections
, e
)
143 s
= uci_to_section(e
);
145 if (strcmp(s
->type
, "defaults"))
150 warn_elem(e
, "ignoring duplicate section");
154 if(!fw3_parse_options(&state
->defaults
, fw3_flag_opts
, s
))
155 warn_elem(e
, "has invalid options");
157 check_policy(e
, &defs
->policy_input
, "input");
158 check_policy(e
, &defs
->policy_output
, "output");
159 check_policy(e
, &defs
->policy_forward
, "forward");
161 check_any_reject_code(e
, &defs
->any_reject_code
);
163 /* exists in both ipv4 and ipv6, if at all, so only check ipv4 */
164 check_target(e
, &flow_offload_avaliable
, "FLOWOFFLOAD", false);
166 if (!flow_offload_avaliable
)
167 defs
->flow_offloading
= false;
172 fw3_print_default_chains(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
175 struct fw3_defaults
*defs
= &state
->defaults
;
176 const struct fw3_chain_spec
*c
;
179 ((t == FW3_FLAG_REJECT) ? FW3_FLAG_DROP : t)
181 if (handle
->family
== FW3_FAMILY_V6
&& defs
->disable_ipv6
)
184 if (handle
->table
== FW3_TABLE_FILTER
)
186 fw3_ipt_set_policy(handle
, "INPUT", policy(defs
->policy_input
));
187 fw3_ipt_set_policy(handle
, "OUTPUT", policy(defs
->policy_output
));
188 fw3_ipt_set_policy(handle
, "FORWARD", policy(defs
->policy_forward
));
191 if (defs
->custom_chains
)
192 set(defs
->flags
, handle
->family
, FW3_FLAG_CUSTOM_CHAINS
);
195 set(defs
->flags
, handle
->family
, FW3_FLAG_SYN_FLOOD
);
197 for (c
= default_chains
; c
->format
; c
++)
199 /* don't touch user chains on selective stop */
200 if (reload
&& c
->flag
== FW3_FLAG_CUSTOM_CHAINS
)
203 if (!fw3_is_family(c
, handle
->family
))
206 if (c
->table
!= handle
->table
)
210 !fw3_hasbit(defs
->flags
[handle
->family
== FW3_FAMILY_V6
], c
->flag
))
213 fw3_ipt_create_chain(handle
, c
->format
);
216 set(defs
->flags
, handle
->family
, handle
->table
);
220 fw3_print_default_head_rules(struct fw3_ipt_handle
*handle
,
221 struct fw3_state
*state
, bool reload
)
224 struct fw3_defaults
*defs
= &state
->defaults
;
225 struct fw3_device lodev
= { .set
= true };
226 struct fw3_protocol tcp
= { .protocol
= 6 };
227 struct fw3_ipt_rule
*r
;
229 const char *chains
[] = {
232 "FORWARD", "forwarding",
235 switch (handle
->table
)
237 case FW3_TABLE_FILTER
:
239 sprintf(lodev
.name
, "lo");
241 r
= fw3_ipt_rule_create(handle
, NULL
, &lodev
, NULL
, NULL
, NULL
);
242 fw3_ipt_rule_target(r
, "ACCEPT");
243 fw3_ipt_rule_append(r
, "INPUT");
245 r
= fw3_ipt_rule_create(handle
, NULL
, NULL
, &lodev
, NULL
, NULL
);
246 fw3_ipt_rule_target(r
, "ACCEPT");
247 fw3_ipt_rule_append(r
, "OUTPUT");
249 if (defs
->custom_chains
)
251 for (i
= 0; i
< ARRAY_SIZE(chains
); i
+= 2)
253 r
= fw3_ipt_rule_new(handle
);
254 fw3_ipt_rule_comment(r
, "Custom %s rule chain", chains
[i
+1]);
255 fw3_ipt_rule_target(r
, "%s_rule", chains
[i
+1]);
256 fw3_ipt_rule_append(r
, chains
[i
]);
260 if (defs
->flow_offloading
)
262 r
= fw3_ipt_rule_new(handle
);
263 fw3_ipt_rule_comment(r
, "Traffic offloading");
264 fw3_ipt_rule_extra(r
, "-m conntrack --ctstate RELATED,ESTABLISHED");
265 fw3_ipt_rule_target(r
, "FLOWOFFLOAD");
266 if (defs
->flow_offloading_hw
)
267 fw3_ipt_rule_addarg(r
, false, "--hw", NULL
);
268 fw3_ipt_rule_append(r
, "FORWARD");
271 for (i
= 0; i
< ARRAY_SIZE(chains
); i
+= 2)
273 r
= fw3_ipt_rule_new(handle
);
274 fw3_ipt_rule_extra(r
, "-m conntrack --ctstate RELATED,ESTABLISHED");
275 fw3_ipt_rule_target(r
, "ACCEPT");
276 fw3_ipt_rule_append(r
, chains
[i
]);
278 if (defs
->drop_invalid
)
280 r
= fw3_ipt_rule_new(handle
);
281 fw3_ipt_rule_extra(r
, "-m conntrack --ctstate INVALID");
282 fw3_ipt_rule_target(r
, "DROP");
283 fw3_ipt_rule_append(r
, chains
[i
]);
289 r
= fw3_ipt_rule_create(handle
, &tcp
, NULL
, NULL
, NULL
, NULL
);
290 fw3_ipt_rule_extra(r
, "--syn");
291 fw3_ipt_rule_limit(r
, &defs
->syn_flood_rate
);
292 fw3_ipt_rule_target(r
, "RETURN");
293 fw3_ipt_rule_append(r
, "syn_flood");
295 r
= fw3_ipt_rule_new(handle
);
296 fw3_ipt_rule_target(r
, "DROP");
297 fw3_ipt_rule_append(r
, "syn_flood");
299 r
= fw3_ipt_rule_create(handle
, &tcp
, NULL
, NULL
, NULL
, NULL
);
300 fw3_ipt_rule_extra(r
, "--syn");
301 fw3_ipt_rule_target(r
, "syn_flood");
302 fw3_ipt_rule_append(r
, "INPUT");
305 r
= fw3_ipt_rule_create(handle
, &tcp
, NULL
, NULL
, NULL
, NULL
);
306 fw3_ipt_rule_target(r
, "REJECT");
307 fw3_ipt_rule_addarg(r
, false, "--reject-with", get_reject_code(handle
->family
, defs
->tcp_reject_code
));
308 fw3_ipt_rule_append(r
, "reject");
310 r
= fw3_ipt_rule_new(handle
);
311 fw3_ipt_rule_target(r
, "REJECT");
312 fw3_ipt_rule_addarg(r
, false, "--reject-with", get_reject_code(handle
->family
, defs
->any_reject_code
));
313 fw3_ipt_rule_append(r
, "reject");
318 if (defs
->custom_chains
)
320 r
= fw3_ipt_rule_new(handle
);
321 fw3_ipt_rule_comment(r
, "Custom prerouting rule chain");
322 fw3_ipt_rule_target(r
, "prerouting_rule");
323 fw3_ipt_rule_append(r
, "PREROUTING");
325 r
= fw3_ipt_rule_new(handle
);
326 fw3_ipt_rule_comment(r
, "Custom postrouting rule chain");
327 fw3_ipt_rule_target(r
, "postrouting_rule");
328 fw3_ipt_rule_append(r
, "POSTROUTING");
338 fw3_print_default_tail_rules(struct fw3_ipt_handle
*handle
,
339 struct fw3_state
*state
, bool reload
)
341 struct fw3_defaults
*defs
= &state
->defaults
;
342 struct fw3_ipt_rule
*r
;
344 if (handle
->table
!= FW3_TABLE_FILTER
)
347 if (defs
->policy_input
== FW3_FLAG_REJECT
)
349 r
= fw3_ipt_rule_new(handle
);
354 fw3_ipt_rule_target(r
, "reject");
355 fw3_ipt_rule_append(r
, "INPUT");
358 if (defs
->policy_output
== FW3_FLAG_REJECT
)
360 r
= fw3_ipt_rule_new(handle
);
365 fw3_ipt_rule_target(r
, "reject");
366 fw3_ipt_rule_append(r
, "OUTPUT");
369 if (defs
->policy_forward
== FW3_FLAG_REJECT
)
371 r
= fw3_ipt_rule_new(handle
);
376 fw3_ipt_rule_target(r
, "reject");
377 fw3_ipt_rule_append(r
, "FORWARD");
382 set_default(const char *name
, int set
)
385 char path
[sizeof("/proc/sys/net/ipv4/tcp_window_scaling\0")];
387 snprintf(path
, sizeof(path
), "/proc/sys/net/ipv4/tcp_%s", name
);
389 info(" * Set tcp_%s to %s", name
, set
? "on" : "off");
391 if (!(f
= fopen(path
, "w")))
393 info(" ! Unable to write value: %s", strerror(errno
));
397 fprintf(f
, "%u\n", set
);
402 fw3_set_defaults(struct fw3_state
*state
)
404 set_default("ecn", state
->defaults
.tcp_ecn
);
405 set_default("syncookies", state
->defaults
.tcp_syncookies
);
406 set_default("window_scaling", state
->defaults
.tcp_window_scaling
);
410 fw3_flush_rules(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
413 enum fw3_flag policy
= reload
? FW3_FLAG_DROP
: FW3_FLAG_ACCEPT
;
414 struct fw3_defaults
*defs
= &state
->defaults
;
415 const struct fw3_chain_spec
*c
;
417 if (!has(defs
->flags
, handle
->family
, handle
->table
))
420 if (handle
->table
== FW3_TABLE_FILTER
)
422 fw3_ipt_set_policy(handle
, "INPUT", policy
);
423 fw3_ipt_set_policy(handle
, "OUTPUT", policy
);
424 fw3_ipt_set_policy(handle
, "FORWARD", policy
);
427 fw3_ipt_delete_id_rules(handle
, "INPUT");
428 fw3_ipt_delete_id_rules(handle
, "OUTPUT");
429 fw3_ipt_delete_id_rules(handle
, "FORWARD");
430 fw3_ipt_delete_id_rules(handle
, "PREROUTING");
431 fw3_ipt_delete_id_rules(handle
, "POSTROUTING");
433 for (c
= default_chains
; c
->format
; c
++)
435 /* don't touch user chains on selective stop */
436 if (reload
&& c
->flag
== FW3_FLAG_CUSTOM_CHAINS
)
439 if (!fw3_is_family(c
, handle
->family
))
442 if (c
->table
!= handle
->table
)
445 if (c
->flag
&& !has(defs
->flags
, handle
->family
, c
->flag
))
448 fw3_ipt_flush_chain(handle
, c
->format
);
450 /* keep certain basic chains that do not depend on any settings to
451 avoid purging unrelated user rules pointing to them */
452 if (reload
&& !c
->flag
)
455 fw3_ipt_delete_chain(handle
, c
->format
);
458 del(defs
->flags
, handle
->family
, handle
->table
);
462 fw3_flush_all(struct fw3_ipt_handle
*handle
)
464 if (handle
->table
== FW3_TABLE_FILTER
)
466 fw3_ipt_set_policy(handle
, "INPUT", FW3_FLAG_ACCEPT
);
467 fw3_ipt_set_policy(handle
, "OUTPUT", FW3_FLAG_ACCEPT
);
468 fw3_ipt_set_policy(handle
, "FORWARD", FW3_FLAG_ACCEPT
);
471 fw3_ipt_flush(handle
);