4936b3879616fa5f9b539d80428319c1f2e6dce1
[project/firewall3.git] / defaults.c
1 /*
2  * firewall3 - 3rd OpenWrt UCI firewall implementation
3  *
4  *   Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
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("custom_chains",       bool,     defaults, custom_chains),
58         FW3_OPT("disable_ipv6",        bool,     defaults, disable_ipv6),
59
60         FW3_OPT("__flags_v4",          int,      defaults, flags[0]),
61         FW3_OPT("__flags_v6",          int,      defaults, flags[1]),
62
63         { }
64 };
65
66
67 static void
68 check_policy(struct uci_element *e, enum fw3_flag *pol, const char *name)
69 {
70         if (*pol == FW3_FLAG_UNSPEC)
71         {
72                 warn_elem(e, "has no %s policy specified, defaulting to DROP", name);
73                 *pol = FW3_FLAG_DROP;
74         }
75         else if (*pol > FW3_FLAG_DROP)
76         {
77                 warn_elem(e, "has invalid %s policy, defaulting to DROP", name);
78                 *pol = FW3_FLAG_DROP;
79         }
80 }
81
82 void
83 fw3_load_defaults(struct fw3_state *state, struct uci_package *p)
84 {
85         struct uci_section *s;
86         struct uci_element *e;
87         struct fw3_defaults *defs = &state->defaults;
88
89         bool seen = false;
90
91         defs->syn_flood_rate.rate  = 25;
92         defs->syn_flood_rate.burst = 50;
93         defs->tcp_syncookies       = true;
94         defs->tcp_window_scaling   = true;
95         defs->custom_chains        = true;
96
97         uci_foreach_element(&p->sections, e)
98         {
99                 s = uci_to_section(e);
100
101                 if (strcmp(s->type, "defaults"))
102                         continue;
103
104                 if (seen)
105                 {
106                         warn_elem(e, "ignoring duplicate section");
107                         continue;
108                 }
109
110                 fw3_parse_options(&state->defaults, fw3_flag_opts, s);
111
112                 check_policy(e, &defs->policy_input, "input");
113                 check_policy(e, &defs->policy_output, "output");
114                 check_policy(e, &defs->policy_forward, "forward");
115         }
116 }
117
118 void
119 fw3_print_default_chains(struct fw3_ipt_handle *handle, struct fw3_state *state,
120                          bool reload)
121 {
122         struct fw3_defaults *defs = &state->defaults;
123         const struct fw3_chain_spec *c;
124
125 #define policy(t) \
126         ((t == FW3_FLAG_REJECT) ? FW3_FLAG_DROP : t)
127
128         if (handle->family == FW3_FAMILY_V6 && defs->disable_ipv6)
129                 return;
130
131         if (handle->table == FW3_TABLE_FILTER)
132         {
133                 fw3_ipt_set_policy(handle, "INPUT",   policy(defs->policy_input));
134                 fw3_ipt_set_policy(handle, "OUTPUT",  policy(defs->policy_output));
135                 fw3_ipt_set_policy(handle, "FORWARD", policy(defs->policy_forward));
136         }
137
138         if (defs->custom_chains)
139                 set(defs->flags, handle->family, FW3_FLAG_CUSTOM_CHAINS);
140
141         if (defs->syn_flood)
142                 set(defs->flags, handle->family, FW3_FLAG_SYN_FLOOD);
143
144         for (c = default_chains; c->format; c++)
145         {
146                 /* don't touch user chains on selective stop */
147                 if (reload && c->flag == FW3_FLAG_CUSTOM_CHAINS)
148                         continue;
149
150                 if (!fw3_is_family(c, handle->family))
151                         continue;
152
153                 if (c->table != handle->table)
154                         continue;
155
156                 if (c->flag &&
157                     !hasbit(defs->flags[handle->family == FW3_FAMILY_V6], c->flag))
158                         continue;
159
160                 fw3_ipt_create_chain(handle, c->format);
161         }
162
163         set(defs->flags, handle->family, handle->table);
164 }
165
166
167 struct toplevel_rule {
168         enum fw3_table table;
169         const char *chain;
170         const char *target;
171 };
172
173 void
174 fw3_print_default_head_rules(struct fw3_ipt_handle *handle,
175                              struct fw3_state *state, bool reload)
176 {
177         int i;
178         struct fw3_defaults *defs = &state->defaults;
179         struct fw3_device lodev = { .set = true };
180         struct fw3_protocol tcp = { .protocol = 6 };
181         struct fw3_ipt_rule *r;
182
183         const char *chains[] = {
184                 "INPUT", "input",
185                 "OUTPUT", "output",
186                 "FORWARD", "forwarding",
187         };
188
189         switch (handle->table)
190         {
191         case FW3_TABLE_FILTER:
192
193                 sprintf(lodev.name, "lo");
194
195                 r = fw3_ipt_rule_create(handle, NULL, &lodev, NULL, NULL, NULL);
196                 fw3_ipt_rule_target(r, "ACCEPT");
197                 fw3_ipt_rule_append(r, "INPUT");
198
199                 r = fw3_ipt_rule_create(handle, NULL, NULL, &lodev, NULL, NULL);
200                 fw3_ipt_rule_target(r, "ACCEPT");
201                 fw3_ipt_rule_append(r, "OUTPUT");
202
203                 if (defs->custom_chains)
204                 {
205                         for (i = 0; i < ARRAY_SIZE(chains); i += 2)
206                         {
207                                 r = fw3_ipt_rule_new(handle);
208                                 fw3_ipt_rule_comment(r, "user chain for %s", chains[i+1]);
209                                 fw3_ipt_rule_target(r, "%s_rule", chains[i+1]);
210                                 fw3_ipt_rule_append(r, chains[i]);
211                         }
212                 }
213
214                 for (i = 0; i < ARRAY_SIZE(chains); i += 2)
215                 {
216                         r = fw3_ipt_rule_new(handle);
217                         fw3_ipt_rule_extra(r, "-m conntrack --ctstate RELATED,ESTABLISHED");
218                         fw3_ipt_rule_target(r, "ACCEPT");
219                         fw3_ipt_rule_append(r, chains[i]);
220
221                         if (defs->drop_invalid)
222                         {
223                                 r = fw3_ipt_rule_new(handle);
224                                 fw3_ipt_rule_extra(r, "-m conntrack --ctstate INVALID");
225                                 fw3_ipt_rule_target(r, "DROP");
226                                 fw3_ipt_rule_append(r, chains[i]);
227                         }
228                 }
229
230                 if (defs->syn_flood)
231                 {
232                         r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
233                         fw3_ipt_rule_extra(r, "--syn");
234                         fw3_ipt_rule_limit(r, &defs->syn_flood_rate);
235                         fw3_ipt_rule_target(r, "RETURN");
236                         fw3_ipt_rule_append(r, "syn_flood");
237
238                         r = fw3_ipt_rule_new(handle);
239                         fw3_ipt_rule_target(r, "DROP");
240                         fw3_ipt_rule_append(r, "syn_flood");
241
242                         r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
243                         fw3_ipt_rule_extra(r, "--syn");
244                         fw3_ipt_rule_target(r, "syn_flood");
245                         fw3_ipt_rule_append(r, "INPUT");
246                 }
247
248                 r = fw3_ipt_rule_create(handle, &tcp, NULL, NULL, NULL, NULL);
249                 fw3_ipt_rule_target(r, "REJECT");
250                 fw3_ipt_rule_addarg(r, false, "--reject-with", "tcp-reset");
251                 fw3_ipt_rule_append(r, "reject");
252
253                 r = fw3_ipt_rule_new(handle);
254                 fw3_ipt_rule_target(r, "REJECT");
255                 fw3_ipt_rule_addarg(r, false, "--reject-with", "port-unreach");
256                 fw3_ipt_rule_append(r, "reject");
257
258                 break;
259
260         case FW3_TABLE_NAT:
261                 if (defs->custom_chains)
262                 {
263                         r = fw3_ipt_rule_new(handle);
264                         fw3_ipt_rule_comment(r, "user chain for prerouting");
265                         fw3_ipt_rule_target(r, "prerouting_rule");
266                         fw3_ipt_rule_append(r, "PREROUTING");
267
268                         r = fw3_ipt_rule_new(handle);
269                         fw3_ipt_rule_comment(r, "user chain for postrouting");
270                         fw3_ipt_rule_target(r, "postrouting_rule");
271                         fw3_ipt_rule_append(r, "POSTROUTING");
272                 }
273                 break;
274
275         default:
276                 break;
277         }
278 }
279
280 void
281 fw3_print_default_tail_rules(struct fw3_ipt_handle *handle,
282                              struct fw3_state *state, bool reload)
283 {
284         struct fw3_defaults *defs = &state->defaults;
285         struct fw3_ipt_rule *r;
286
287         if (handle->table != FW3_TABLE_FILTER)
288                 return;
289
290         if (defs->policy_input == FW3_FLAG_REJECT)
291         {
292                 r = fw3_ipt_rule_new(handle);
293
294                 if (!r)
295                         return;
296
297                 fw3_ipt_rule_target(r, "reject");
298                 fw3_ipt_rule_append(r, "INPUT");
299         }
300
301         if (defs->policy_output == FW3_FLAG_REJECT)
302         {
303                 r = fw3_ipt_rule_new(handle);
304
305                 if (!r)
306                         return;
307
308                 fw3_ipt_rule_target(r, "reject");
309                 fw3_ipt_rule_append(r, "OUTPUT");
310         }
311
312         if (defs->policy_forward == FW3_FLAG_REJECT)
313         {
314                 r = fw3_ipt_rule_new(handle);
315
316                 if (!r)
317                         return;
318
319                 fw3_ipt_rule_target(r, "reject");
320                 fw3_ipt_rule_append(r, "FORWARD");
321         }
322 }
323
324 static void
325 set_default(const char *name, int set)
326 {
327         FILE *f;
328         char path[sizeof("/proc/sys/net/ipv4/tcp_window_scaling\0")];
329
330         snprintf(path, sizeof(path), "/proc/sys/net/ipv4/tcp_%s", name);
331
332         info(" * Set tcp_%s to %s", name, set ? "on" : "off", name);
333
334         if (!(f = fopen(path, "w")))
335         {
336                 info("   ! Unable to write value: %s", strerror(errno));
337                 return;
338         }
339
340         fprintf(f, "%u\n", set);
341         fclose(f);
342 }
343
344 void
345 fw3_set_defaults(struct fw3_state *state)
346 {
347         set_default("ecn",            state->defaults.tcp_ecn);
348         set_default("syncookies",     state->defaults.tcp_syncookies);
349         set_default("window_scaling", state->defaults.tcp_window_scaling);
350 }
351
352 void
353 fw3_flush_rules(struct fw3_ipt_handle *handle, struct fw3_state *state,
354                 bool reload)
355 {
356         enum fw3_flag policy = reload ? FW3_FLAG_DROP : FW3_FLAG_ACCEPT;
357         struct fw3_defaults *defs = &state->defaults;
358         const struct fw3_chain_spec *c;
359
360         if (!has(defs->flags, handle->family, handle->table))
361                 return;
362
363         if (handle->table == FW3_TABLE_FILTER)
364         {
365                 fw3_ipt_set_policy(handle, "INPUT",   policy);
366                 fw3_ipt_set_policy(handle, "OUTPUT",  policy);
367                 fw3_ipt_set_policy(handle, "FORWARD", policy);
368         }
369
370         fw3_ipt_delete_id_rules(handle, "INPUT");
371         fw3_ipt_delete_id_rules(handle, "OUTPUT");
372         fw3_ipt_delete_id_rules(handle, "FORWARD");
373         fw3_ipt_delete_id_rules(handle, "PREROUTING");
374         fw3_ipt_delete_id_rules(handle, "POSTROUTING");
375
376         for (c = default_chains; c->format; c++)
377         {
378                 /* don't touch user chains on selective stop */
379                 if (reload && c->flag == FW3_FLAG_CUSTOM_CHAINS)
380                         continue;
381
382                 if (!fw3_is_family(c, handle->family))
383                         continue;
384
385                 if (c->table != handle->table)
386                         continue;
387
388                 if (c->flag && !has(defs->flags, handle->family, c->flag))
389                         continue;
390
391                 fw3_ipt_flush_chain(handle, c->format);
392
393                 /* keep certain basic chains that do not depend on any settings to
394                    avoid purging unrelated user rules pointing to them */
395                 if (reload && !c->flag)
396                         continue;
397
398                 fw3_ipt_delete_chain(handle, c->format);
399         }
400
401         del(defs->flags, handle->family, handle->table);
402 }
403
404 void
405 fw3_flush_all(struct fw3_ipt_handle *handle)
406 {
407         if (handle->table == FW3_TABLE_FILTER)
408         {
409                 fw3_ipt_set_policy(handle, "INPUT",   FW3_FLAG_ACCEPT);
410                 fw3_ipt_set_policy(handle, "OUTPUT",  FW3_FLAG_ACCEPT);
411                 fw3_ipt_set_policy(handle, "FORWARD", FW3_FLAG_ACCEPT);
412         }
413
414         fw3_ipt_flush(handle);
415 }