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
),
61 FW3_OPT("__flags_v4", int, defaults
, flags
[0]),
62 FW3_OPT("__flags_v6", int, defaults
, flags
[1]),
69 check_policy(struct uci_element
*e
, enum fw3_flag
*pol
, const char *name
)
71 if (*pol
== FW3_FLAG_UNSPEC
)
73 warn_elem(e
, "has no %s policy specified, defaulting to DROP", name
);
76 else if (*pol
> FW3_FLAG_DROP
)
78 warn_elem(e
, "has invalid %s policy, defaulting to DROP", name
);
84 fw3_load_defaults(struct fw3_state
*state
, struct uci_package
*p
)
86 struct uci_section
*s
;
87 struct uci_element
*e
;
88 struct fw3_defaults
*defs
= &state
->defaults
;
92 defs
->syn_flood_rate
.rate
= 25;
93 defs
->syn_flood_rate
.burst
= 50;
94 defs
->tcp_syncookies
= true;
95 defs
->tcp_window_scaling
= true;
96 defs
->custom_chains
= true;
97 defs
->auto_helper
= true;
99 uci_foreach_element(&p
->sections
, e
)
101 s
= uci_to_section(e
);
103 if (strcmp(s
->type
, "defaults"))
108 warn_elem(e
, "ignoring duplicate section");
112 if(!fw3_parse_options(&state
->defaults
, fw3_flag_opts
, s
))
113 warn_elem(e
, "has invalid options");
115 check_policy(e
, &defs
->policy_input
, "input");
116 check_policy(e
, &defs
->policy_output
, "output");
117 check_policy(e
, &defs
->policy_forward
, "forward");
122 fw3_print_default_chains(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
125 struct fw3_defaults
*defs
= &state
->defaults
;
126 const struct fw3_chain_spec
*c
;
129 ((t == FW3_FLAG_REJECT) ? FW3_FLAG_DROP : t)
131 if (handle
->family
== FW3_FAMILY_V6
&& defs
->disable_ipv6
)
134 if (handle
->table
== FW3_TABLE_FILTER
)
136 fw3_ipt_set_policy(handle
, "INPUT", policy(defs
->policy_input
));
137 fw3_ipt_set_policy(handle
, "OUTPUT", policy(defs
->policy_output
));
138 fw3_ipt_set_policy(handle
, "FORWARD", policy(defs
->policy_forward
));
141 if (defs
->custom_chains
)
142 set(defs
->flags
, handle
->family
, FW3_FLAG_CUSTOM_CHAINS
);
145 set(defs
->flags
, handle
->family
, FW3_FLAG_SYN_FLOOD
);
147 for (c
= default_chains
; c
->format
; c
++)
149 /* don't touch user chains on selective stop */
150 if (reload
&& c
->flag
== FW3_FLAG_CUSTOM_CHAINS
)
153 if (!fw3_is_family(c
, handle
->family
))
156 if (c
->table
!= handle
->table
)
160 !fw3_hasbit(defs
->flags
[handle
->family
== FW3_FAMILY_V6
], c
->flag
))
163 fw3_ipt_create_chain(handle
, c
->format
);
166 set(defs
->flags
, handle
->family
, handle
->table
);
170 fw3_print_default_head_rules(struct fw3_ipt_handle
*handle
,
171 struct fw3_state
*state
, bool reload
)
174 struct fw3_defaults
*defs
= &state
->defaults
;
175 struct fw3_device lodev
= { .set
= true };
176 struct fw3_protocol tcp
= { .protocol
= 6 };
177 struct fw3_ipt_rule
*r
;
179 const char *chains
[] = {
182 "FORWARD", "forwarding",
185 switch (handle
->table
)
187 case FW3_TABLE_FILTER
:
189 sprintf(lodev
.name
, "lo");
191 r
= fw3_ipt_rule_create(handle
, NULL
, &lodev
, NULL
, NULL
, NULL
);
192 fw3_ipt_rule_target(r
, "ACCEPT");
193 fw3_ipt_rule_append(r
, "INPUT");
195 r
= fw3_ipt_rule_create(handle
, NULL
, NULL
, &lodev
, NULL
, NULL
);
196 fw3_ipt_rule_target(r
, "ACCEPT");
197 fw3_ipt_rule_append(r
, "OUTPUT");
199 if (defs
->custom_chains
)
201 for (i
= 0; i
< ARRAY_SIZE(chains
); i
+= 2)
203 r
= fw3_ipt_rule_new(handle
);
204 fw3_ipt_rule_comment(r
, "user chain for %s", chains
[i
+1]);
205 fw3_ipt_rule_target(r
, "%s_rule", chains
[i
+1]);
206 fw3_ipt_rule_append(r
, chains
[i
]);
210 for (i
= 0; i
< ARRAY_SIZE(chains
); i
+= 2)
212 r
= fw3_ipt_rule_new(handle
);
213 fw3_ipt_rule_extra(r
, "-m conntrack --ctstate RELATED,ESTABLISHED");
214 fw3_ipt_rule_target(r
, "ACCEPT");
215 fw3_ipt_rule_append(r
, chains
[i
]);
217 if (defs
->drop_invalid
)
219 r
= fw3_ipt_rule_new(handle
);
220 fw3_ipt_rule_extra(r
, "-m conntrack --ctstate INVALID");
221 fw3_ipt_rule_target(r
, "DROP");
222 fw3_ipt_rule_append(r
, chains
[i
]);
228 r
= fw3_ipt_rule_create(handle
, &tcp
, NULL
, NULL
, NULL
, NULL
);
229 fw3_ipt_rule_extra(r
, "--syn");
230 fw3_ipt_rule_limit(r
, &defs
->syn_flood_rate
);
231 fw3_ipt_rule_target(r
, "RETURN");
232 fw3_ipt_rule_append(r
, "syn_flood");
234 r
= fw3_ipt_rule_new(handle
);
235 fw3_ipt_rule_target(r
, "DROP");
236 fw3_ipt_rule_append(r
, "syn_flood");
238 r
= fw3_ipt_rule_create(handle
, &tcp
, NULL
, NULL
, NULL
, NULL
);
239 fw3_ipt_rule_extra(r
, "--syn");
240 fw3_ipt_rule_target(r
, "syn_flood");
241 fw3_ipt_rule_append(r
, "INPUT");
244 r
= fw3_ipt_rule_create(handle
, &tcp
, NULL
, NULL
, NULL
, NULL
);
245 fw3_ipt_rule_target(r
, "REJECT");
246 fw3_ipt_rule_addarg(r
, false, "--reject-with", "tcp-reset");
247 fw3_ipt_rule_append(r
, "reject");
249 r
= fw3_ipt_rule_new(handle
);
250 fw3_ipt_rule_target(r
, "REJECT");
251 fw3_ipt_rule_addarg(r
, false, "--reject-with", "port-unreach");
252 fw3_ipt_rule_append(r
, "reject");
257 if (defs
->custom_chains
)
259 r
= fw3_ipt_rule_new(handle
);
260 fw3_ipt_rule_comment(r
, "user chain for prerouting");
261 fw3_ipt_rule_target(r
, "prerouting_rule");
262 fw3_ipt_rule_append(r
, "PREROUTING");
264 r
= fw3_ipt_rule_new(handle
);
265 fw3_ipt_rule_comment(r
, "user chain for postrouting");
266 fw3_ipt_rule_target(r
, "postrouting_rule");
267 fw3_ipt_rule_append(r
, "POSTROUTING");
277 fw3_print_default_tail_rules(struct fw3_ipt_handle
*handle
,
278 struct fw3_state
*state
, bool reload
)
280 struct fw3_defaults
*defs
= &state
->defaults
;
281 struct fw3_ipt_rule
*r
;
283 if (handle
->table
!= FW3_TABLE_FILTER
)
286 if (defs
->policy_input
== FW3_FLAG_REJECT
)
288 r
= fw3_ipt_rule_new(handle
);
293 fw3_ipt_rule_target(r
, "reject");
294 fw3_ipt_rule_append(r
, "INPUT");
297 if (defs
->policy_output
== FW3_FLAG_REJECT
)
299 r
= fw3_ipt_rule_new(handle
);
304 fw3_ipt_rule_target(r
, "reject");
305 fw3_ipt_rule_append(r
, "OUTPUT");
308 if (defs
->policy_forward
== FW3_FLAG_REJECT
)
310 r
= fw3_ipt_rule_new(handle
);
315 fw3_ipt_rule_target(r
, "reject");
316 fw3_ipt_rule_append(r
, "FORWARD");
321 set_default(const char *name
, int set
)
324 char path
[sizeof("/proc/sys/net/ipv4/tcp_window_scaling\0")];
326 snprintf(path
, sizeof(path
), "/proc/sys/net/ipv4/tcp_%s", name
);
328 info(" * Set tcp_%s to %s", name
, set
? "on" : "off", name
);
330 if (!(f
= fopen(path
, "w")))
332 info(" ! Unable to write value: %s", strerror(errno
));
336 fprintf(f
, "%u\n", set
);
341 fw3_set_defaults(struct fw3_state
*state
)
343 set_default("ecn", state
->defaults
.tcp_ecn
);
344 set_default("syncookies", state
->defaults
.tcp_syncookies
);
345 set_default("window_scaling", state
->defaults
.tcp_window_scaling
);
349 fw3_flush_rules(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
352 enum fw3_flag policy
= reload
? FW3_FLAG_DROP
: FW3_FLAG_ACCEPT
;
353 struct fw3_defaults
*defs
= &state
->defaults
;
354 const struct fw3_chain_spec
*c
;
356 if (!has(defs
->flags
, handle
->family
, handle
->table
))
359 if (handle
->table
== FW3_TABLE_FILTER
)
361 fw3_ipt_set_policy(handle
, "INPUT", policy
);
362 fw3_ipt_set_policy(handle
, "OUTPUT", policy
);
363 fw3_ipt_set_policy(handle
, "FORWARD", policy
);
366 fw3_ipt_delete_id_rules(handle
, "INPUT");
367 fw3_ipt_delete_id_rules(handle
, "OUTPUT");
368 fw3_ipt_delete_id_rules(handle
, "FORWARD");
369 fw3_ipt_delete_id_rules(handle
, "PREROUTING");
370 fw3_ipt_delete_id_rules(handle
, "POSTROUTING");
372 for (c
= default_chains
; c
->format
; c
++)
374 /* don't touch user chains on selective stop */
375 if (reload
&& c
->flag
== FW3_FLAG_CUSTOM_CHAINS
)
378 if (!fw3_is_family(c
, handle
->family
))
381 if (c
->table
!= handle
->table
)
384 if (c
->flag
&& !has(defs
->flags
, handle
->family
, c
->flag
))
387 fw3_ipt_flush_chain(handle
, c
->format
);
389 /* keep certain basic chains that do not depend on any settings to
390 avoid purging unrelated user rules pointing to them */
391 if (reload
&& !c
->flag
)
394 fw3_ipt_delete_chain(handle
, c
->format
);
397 del(defs
->flags
, handle
->family
, handle
->table
);
401 fw3_flush_all(struct fw3_ipt_handle
*handle
)
403 if (handle
->table
== FW3_TABLE_FILTER
)
405 fw3_ipt_set_policy(handle
, "INPUT", FW3_FLAG_ACCEPT
);
406 fw3_ipt_set_policy(handle
, "OUTPUT", FW3_FLAG_ACCEPT
);
407 fw3_ipt_set_policy(handle
, "FORWARD", FW3_FLAG_ACCEPT
);
410 fw3_ipt_flush(handle
);