Implement reload option for includes to decide whether includes should get reloaded...
[project/firewall3.git] / redirects.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 "redirects.h"
20
21
22 const struct fw3_option fw3_redirect_opts[] = {
23         FW3_OPT("enabled",             bool,      redirect,     enabled),
24
25         FW3_OPT("name",                string,    redirect,     name),
26         FW3_OPT("family",              family,    redirect,     family),
27
28         FW3_OPT("src",                 device,    redirect,     src),
29         FW3_OPT("dest",                device,    redirect,     dest),
30
31         FW3_OPT("ipset",               device,    redirect,     ipset),
32
33         FW3_LIST("proto",              protocol,  redirect,     proto),
34
35         FW3_OPT("src_ip",              address,   redirect,     ip_src),
36         FW3_LIST("src_mac",            mac,       redirect,     mac_src),
37         FW3_OPT("src_port",            port,      redirect,     port_src),
38
39         FW3_OPT("src_dip",             address,   redirect,     ip_dest),
40         FW3_OPT("src_dport",           port,      redirect,     port_dest),
41
42         FW3_OPT("dest_ip",             address,   redirect,     ip_redir),
43         FW3_OPT("dest_port",           port,      redirect,     port_redir),
44
45         FW3_OPT("extra",               string,    redirect,     extra),
46
47         FW3_OPT("utc_time",            bool,      redirect,     time.utc),
48         FW3_OPT("start_date",          date,      redirect,     time.datestart),
49         FW3_OPT("stop_date",           date,      redirect,     time.datestop),
50         FW3_OPT("start_time",          time,      redirect,     time.timestart),
51         FW3_OPT("stop_time",           time,      redirect,     time.timestop),
52         FW3_OPT("weekdays",            weekdays,  redirect,     time.weekdays),
53         FW3_OPT("monthdays",           monthdays, redirect,     time.monthdays),
54
55         FW3_OPT("reflection",          bool,      redirect,     reflection),
56         FW3_OPT("reflection_src",      reflection_source,
57                                                   redirect,     reflection_src),
58
59         FW3_OPT("target",              target,    redirect,     target),
60
61         { }
62 };
63
64
65 static bool
66 check_families(struct uci_element *e, struct fw3_redirect *r)
67 {
68         if (r->family == FW3_FAMILY_ANY)
69                 return true;
70
71         if (r->_src && r->_src->family && r->_src->family != r->family)
72         {
73                 warn_elem(e, "refers to source zone with different family");
74                 return false;
75         }
76
77         if (r->_dest && r->_dest->family && r->_dest->family != r->family)
78         {
79                 warn_elem(e, "refers to destination zone with different family");
80                 return false;
81         }
82
83         if (r->_ipset && r->_ipset->family && r->_ipset->family != r->family)
84         {
85                 warn_elem(e, "refers to ipset with different family");
86                 return false;
87         }
88
89         if (r->ip_src.family && r->ip_src.family != r->family)
90         {
91                 warn_elem(e, "uses source ip with different family");
92                 return false;
93         }
94
95         if (r->ip_dest.family && r->ip_dest.family != r->family)
96         {
97                 warn_elem(e, "uses destination ip with different family");
98                 return false;
99         }
100
101         if (r->ip_redir.family && r->ip_redir.family != r->family)
102         {
103                 warn_elem(e, "uses redirect ip with different family");
104                 return false;
105         }
106
107         return true;
108 }
109
110 void
111 fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
112 {
113         struct uci_section *s;
114         struct uci_element *e;
115         struct fw3_redirect *redir;
116
117         bool valid = false;
118
119         INIT_LIST_HEAD(&state->redirects);
120
121         uci_foreach_element(&p->sections, e)
122         {
123                 s = uci_to_section(e);
124
125                 if (strcmp(s->type, "redirect"))
126                         continue;
127
128                 redir = malloc(sizeof(*redir));
129
130                 if (!redir)
131                         continue;
132
133                 memset(redir, 0, sizeof(*redir));
134
135                 INIT_LIST_HEAD(&redir->proto);
136                 INIT_LIST_HEAD(&redir->mac_src);
137
138                 redir->enabled = true;
139                 redir->reflection = true;
140
141                 fw3_parse_options(redir, fw3_redirect_opts, s);
142
143                 if (!redir->enabled)
144                 {
145                         fw3_free_redirect(redir);
146                         continue;
147                 }
148
149                 if (redir->src.invert)
150                 {
151                         warn_elem(e, "must not have an inverted source");
152                         fw3_free_redirect(redir);
153                         continue;
154                 }
155                 else if (redir->src.set && !redir->src.any &&
156                          !(redir->_src = fw3_lookup_zone(state, redir->src.name, false)))
157                 {
158                         warn_elem(e, "refers to not existing zone '%s'", redir->src.name);
159                         fw3_free_redirect(redir);
160                         continue;
161                 }
162                 else if (redir->dest.set && !redir->dest.any &&
163                          !(redir->_dest = fw3_lookup_zone(state, redir->dest.name, false)))
164                 {
165                         warn_elem(e, "refers to not existing zone '%s'", redir->dest.name);
166                         fw3_free_redirect(redir);
167                         continue;
168                 }
169                 else if (redir->ipset.set && state->disable_ipsets)
170                 {
171                         warn_elem(e, "skipped due to disabled ipset support");
172                         fw3_free_redirect(redir);
173                         continue;
174                 }
175                 else if (redir->ipset.set && !redir->ipset.any &&
176                          !(redir->_ipset = fw3_lookup_ipset(state, redir->ipset.name, false)))
177                 {
178                         warn_elem(e, "refers to unknown ipset '%s'", redir->ipset.name);
179                         fw3_free_redirect(redir);
180                         continue;
181                 }
182
183                 if (!check_families(e, redir))
184                 {
185                         fw3_free_redirect(redir);
186                         continue;
187                 }
188
189                 if (redir->target == FW3_FLAG_UNSPEC)
190                 {
191                         warn_elem(e, "has no target specified, defaulting to DNAT");
192                         redir->target = FW3_FLAG_DNAT;
193                 }
194                 else if (redir->target < FW3_FLAG_DNAT)
195                 {
196                         warn_elem(e, "has invalid target specified, defaulting to DNAT");
197                         redir->target = FW3_FLAG_DNAT;
198                 }
199
200                 if (redir->target == FW3_FLAG_DNAT)
201                 {
202                         if (redir->src.any)
203                                 warn_elem(e, "must not have source '*' for DNAT target");
204                         else if (!redir->_src)
205                                 warn_elem(e, "has no source specified");
206                         else
207                         {
208                                 set(redir->_src->flags, FW3_FAMILY_V4, redir->target);
209                                 redir->_src->conntrack = true;
210                                 valid = true;
211                         }
212
213                         if (redir->reflection && redir->_dest && redir->_src->masq)
214                         {
215                                 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_ACCEPT);
216                                 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_DNAT);
217                                 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
218                         }
219                 }
220                 else
221                 {
222                         if (redir->dest.any)
223                                 warn_elem(e, "must not have destination '*' for SNAT target");
224                         else if (!redir->_dest)
225                                 warn_elem(e, "has no destination specified");
226                         else if (!redir->ip_dest.set)
227                                 warn_elem(e, "has no src_dip option specified");
228                         else
229                         {
230                                 set(redir->_dest->flags, FW3_FAMILY_V4, redir->target);
231                                 redir->_dest->conntrack = true;
232                                 valid = true;
233                         }
234                 }
235
236                 if (!valid)
237                 {
238                         fw3_free_redirect(redir);
239                         continue;
240                 }
241
242                 if (!redir->port_redir.set)
243                         redir->port_redir = redir->port_dest;
244
245                 list_add_tail(&redir->list, &state->redirects);
246         }
247 }
248
249 static void
250 print_chain_nat(struct fw3_redirect *redir)
251 {
252         if (redir->target == FW3_FLAG_DNAT)
253                 fw3_pr("-A zone_%s_prerouting", redir->src.name);
254         else
255                 fw3_pr("-A zone_%s_postrouting", redir->dest.name);
256 }
257
258 static void
259 print_snat_dnat(enum fw3_flag target,
260                 struct fw3_address *addr, struct fw3_port *port)
261 {
262         const char *t;
263         char s[sizeof("255.255.255.255 ")];
264
265         if (target == FW3_FLAG_DNAT)
266                 t = "DNAT --to-destination";
267         else
268                 t = "SNAT --to-source";
269
270         inet_ntop(AF_INET, &addr->address.v4, s, sizeof(s));
271
272         fw3_pr(" -j %s %s", t, s);
273
274         if (port && port->set)
275         {
276                 if (port->port_min == port->port_max)
277                         fw3_pr(":%u", port->port_min);
278                 else
279                         fw3_pr(":%u-%u", port->port_min, port->port_max);
280         }
281
282         fw3_pr("\n");
283 }
284
285 static void
286 print_target_nat(struct fw3_redirect *redir)
287 {
288         if (redir->target == FW3_FLAG_DNAT)
289                 print_snat_dnat(redir->target, &redir->ip_redir, &redir->port_redir);
290         else
291                 print_snat_dnat(redir->target, &redir->ip_dest, &redir->port_dest);
292 }
293
294 static void
295 print_chain_filter(struct fw3_redirect *redir)
296 {
297         if (redir->target == FW3_FLAG_DNAT)
298         {
299                 /* XXX: check for local ip */
300                 if (!redir->ip_redir.set)
301                         fw3_pr("-A zone_%s_input", redir->src.name);
302                 else
303                         fw3_pr("-A zone_%s_forward", redir->src.name);
304         }
305         else
306         {
307                 if (redir->src.set && !redir->src.any)
308                         fw3_pr("-A zone_%s_forward", redir->src.name);
309                 else
310                         fw3_pr("-A delegate_forward");
311         }
312 }
313
314 static void
315 print_target_filter(struct fw3_redirect *redir)
316 {
317         /* XXX: check for local ip */
318         if (redir->target == FW3_FLAG_DNAT && !redir->ip_redir.set)
319                 fw3_pr(" -m conntrack --ctstate DNAT -j ACCEPT\n");
320         else
321                 fw3_pr(" -j ACCEPT\n");
322 }
323
324 static void
325 print_redirect(enum fw3_table table, enum fw3_family family,
326                struct fw3_redirect *redir, int num)
327 {
328         struct list_head *ext_addrs, *int_addrs;
329         struct fw3_address *ext_addr, *int_addr, ref_addr;
330         struct fw3_device *ext_net, *int_net;
331         struct fw3_protocol *proto;
332         struct fw3_mac *mac;
333
334         if (redir->name)
335                 info("   * Redirect '%s'", redir->name);
336         else
337                 info("   * Redirect #%u", num);
338
339         if (!fw3_is_family(redir->_src, family) ||
340                 !fw3_is_family(redir->_dest, family))
341         {
342                 info("     ! Skipping due to different family of zone");
343                 return;
344         }
345
346         if (!fw3_is_family(&redir->ip_src, family) ||
347             !fw3_is_family(&redir->ip_dest, family) ||
348                 !fw3_is_family(&redir->ip_redir, family))
349         {
350                 info("     ! Skipping due to different family of ip address");
351                 return;
352         }
353
354         if (redir->_ipset)
355         {
356                 if (!fw3_is_family(redir->_ipset, family))
357                 {
358                         info("     ! Skipping due to different family in ipset");
359                         return;
360                 }
361
362                 set(redir->_ipset->flags, family, family);
363         }
364
365         fw3_foreach(proto, &redir->proto)
366         fw3_foreach(mac, &redir->mac_src)
367         {
368                 if (table == FW3_TABLE_NAT)
369                 {
370                         print_chain_nat(redir);
371                         fw3_format_ipset(redir->_ipset, redir->ipset.invert);
372                         fw3_format_protocol(proto, family);
373
374                         if (redir->target == FW3_FLAG_DNAT)
375                         {
376                                 fw3_format_src_dest(&redir->ip_src, &redir->ip_dest);
377                                 fw3_format_sport_dport(&redir->port_src, &redir->port_dest);
378                         }
379                         else
380                         {
381                                 fw3_format_src_dest(&redir->ip_src, &redir->ip_redir);
382                                 fw3_format_sport_dport(&redir->port_src, &redir->port_redir);
383                         }
384
385                         fw3_format_mac(mac);
386                         fw3_format_time(&redir->time);
387                         fw3_format_extra(redir->extra);
388                         fw3_format_comment(redir->name);
389                         print_target_nat(redir);
390                 }
391                 else if (table == FW3_TABLE_FILTER)
392                 {
393                         print_chain_filter(redir);
394                         fw3_format_ipset(redir->_ipset, redir->ipset.invert);
395                         fw3_format_protocol(proto, family);
396                         fw3_format_src_dest(&redir->ip_src, &redir->ip_redir);
397                         fw3_format_sport_dport(&redir->port_src, &redir->port_redir);
398                         fw3_format_mac(mac);
399                         fw3_format_time(&redir->time);
400                         fw3_format_extra(redir->extra);
401                         fw3_format_comment(redir->name);
402                         print_target_filter(redir);
403                 }
404         }
405
406         /* reflection rules */
407         if (redir->target != FW3_FLAG_DNAT || !redir->reflection)
408                 return;
409
410         if (!redir->_dest || !redir->_src->masq)
411                 return;
412
413         list_for_each_entry(ext_net, &redir->_src->networks, list)
414         {
415                 ext_addrs = fw3_ubus_address(ext_net->name);
416
417                 if (!ext_addrs || list_empty(ext_addrs))
418                         continue;
419
420                 list_for_each_entry(int_net, &redir->_dest->networks, list)
421                 {
422                         int_addrs = fw3_ubus_address(int_net->name);
423
424                         if (!int_addrs || list_empty(int_addrs))
425                                 continue;
426
427                         fw3_foreach(ext_addr, ext_addrs)
428                         fw3_foreach(int_addr, int_addrs)
429                         fw3_foreach(proto, &redir->proto)
430                         {
431                                 if (!fw3_is_family(int_addr, family) ||
432                                     !fw3_is_family(ext_addr, family))
433                                         continue;
434
435                                 if (!proto || (proto->protocol != 6 && proto->protocol != 17))
436                                         continue;
437
438                                 if (redir->reflection_src == FW3_REFLECTION_INTERNAL)
439                                         ref_addr = *int_addr;
440                                 else
441                                         ref_addr = *ext_addr;
442
443                                 ref_addr.mask = 32;
444                                 ext_addr->mask = 32;
445
446                                 if (table == FW3_TABLE_NAT)
447                                 {
448                                         fw3_pr("-A zone_%s_prerouting", redir->dest.name);
449                                         fw3_format_protocol(proto, family);
450                                         fw3_format_src_dest(int_addr, ext_addr);
451                                         fw3_format_sport_dport(NULL, &redir->port_dest);
452                                         fw3_format_time(&redir->time);
453                                         fw3_format_comment(redir->name, " (reflection)");
454                                         print_snat_dnat(FW3_FLAG_DNAT,
455                                                         &redir->ip_redir, &redir->port_redir);
456
457                                         fw3_pr("-A zone_%s_postrouting", redir->dest.name);
458                                         fw3_format_protocol(proto, family);
459                                         fw3_format_src_dest(int_addr, &redir->ip_redir);
460                                         fw3_format_sport_dport(NULL, &redir->port_redir);
461                                         fw3_format_time(&redir->time);
462                                         fw3_format_comment(redir->name, " (reflection)");
463                                         print_snat_dnat(FW3_FLAG_SNAT, &ref_addr, NULL);
464                                 }
465                                 else if (table == FW3_TABLE_FILTER)
466                                 {
467                                         fw3_pr("-A zone_%s_forward", redir->dest.name);
468                                         fw3_format_protocol(proto, family);
469                                         fw3_format_src_dest(int_addr, &redir->ip_redir);
470                                         fw3_format_sport_dport(NULL, &redir->port_redir);
471                                         fw3_format_time(&redir->time);
472                                         fw3_format_comment(redir->name, " (reflection)");
473                                         fw3_pr(" -j zone_%s_dest_ACCEPT\n", redir->dest.name);
474                                 }
475                         }
476
477                         fw3_ubus_address_free(int_addrs);
478                 }
479
480                 fw3_ubus_address_free(ext_addrs);
481         }
482 }
483
484 void
485 fw3_print_redirects(enum fw3_table table, enum fw3_family family,
486                     struct fw3_state *state)
487 {
488         int num = 0;
489         struct fw3_redirect *redir;
490
491         if (family == FW3_FAMILY_V6)
492                 return;
493
494         if (table != FW3_TABLE_FILTER && table != FW3_TABLE_NAT)
495                 return;
496
497         list_for_each_entry(redir, &state->redirects, list)
498                 print_redirect(table, family, redir, num++);
499 }