iptables: add exception handling
[project/firewall3.git] / redirects.c
1 /*
2  * firewall3 - 3rd OpenWrt UCI firewall implementation
3  *
4  *   Copyright (C) 2013-2014 Jo-Philipp Wich <jo@mein.io>
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",               setmatch,  redirect,     ipset),
32
33         FW3_LIST("proto",              protocol,  redirect,     proto),
34
35         FW3_OPT("src_ip",              network,   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",             network,   redirect,     ip_dest),
40         FW3_OPT("src_dport",           port,      redirect,     port_dest),
41
42         FW3_OPT("dest_ip",             network,   redirect,     ip_redir),
43         FW3_OPT("dest_port",           port,      redirect,     port_redir),
44
45         FW3_OPT("extra",               string,    redirect,     extra),
46
47         FW3_OPT("limit",               limit,     redirect,     limit),
48         FW3_OPT("limit_burst",         int,       redirect,     limit.burst),
49
50         FW3_OPT("utc_time",            bool,      redirect,     time.utc),
51         FW3_OPT("start_date",          date,      redirect,     time.datestart),
52         FW3_OPT("stop_date",           date,      redirect,     time.datestop),
53         FW3_OPT("start_time",          time,      redirect,     time.timestart),
54         FW3_OPT("stop_time",           time,      redirect,     time.timestop),
55         FW3_OPT("weekdays",            weekdays,  redirect,     time.weekdays),
56         FW3_OPT("monthdays",           monthdays, redirect,     time.monthdays),
57
58         FW3_OPT("mark",                mark,      redirect,     mark),
59
60         FW3_OPT("reflection",          bool,      redirect,     reflection),
61         FW3_OPT("reflection_src",      reflection_source,
62                                                   redirect,     reflection_src),
63
64         FW3_OPT("target",              target,    redirect,     target),
65
66         { }
67 };
68
69
70 static bool
71 check_families(struct uci_element *e, struct fw3_redirect *r)
72 {
73         if (r->family == FW3_FAMILY_ANY)
74                 return true;
75
76         if (r->_src && r->_src->family && r->_src->family != r->family)
77         {
78                 warn_elem(e, "refers to source zone with different family");
79                 return false;
80         }
81
82         if (r->_dest && r->_dest->family && r->_dest->family != r->family)
83         {
84                 warn_elem(e, "refers to destination zone with different family");
85                 return false;
86         }
87
88         if (r->ipset.ptr && r->ipset.ptr->family &&
89             r->ipset.ptr->family != r->family)
90         {
91                 warn_elem(e, "refers to ipset with different family");
92                 return false;
93         }
94
95         if (r->ip_src.family && r->ip_src.family != r->family)
96         {
97                 warn_elem(e, "uses source ip with different family");
98                 return false;
99         }
100
101         if (r->ip_dest.family && r->ip_dest.family != r->family)
102         {
103                 warn_elem(e, "uses destination ip with different family");
104                 return false;
105         }
106
107         if (r->ip_redir.family && r->ip_redir.family != r->family)
108         {
109                 warn_elem(e, "uses redirect ip with different family");
110                 return false;
111         }
112
113         return true;
114 }
115
116 static bool
117 compare_addr(struct fw3_address *a, struct fw3_address *b)
118 {
119         if (a->family != FW3_FAMILY_V4 || b->family != FW3_FAMILY_V4)
120                 return false;
121
122         return ((a->address.v4.s_addr & a->mask.v4.s_addr) ==
123                 (b->address.v4.s_addr & a->mask.v4.s_addr));
124 }
125
126 static bool
127 resolve_dest(struct uci_element *e, struct fw3_redirect *redir,
128              struct fw3_state *state)
129 {
130         struct fw3_zone *zone;
131         struct fw3_address *addr;
132         struct list_head *addrs;
133
134         if (!redir->ip_redir.set)
135                 return false;
136
137         list_for_each_entry(zone, &state->zones, list)
138         {
139                 addrs = fw3_resolve_zone_addresses(zone, NULL);
140
141                 if (!addrs)
142                         continue;
143
144                 list_for_each_entry(addr, addrs, list)
145                 {
146                         if (!compare_addr(addr, &redir->ip_redir))
147                                 continue;
148
149                         strncpy(redir->dest.name, zone->name, sizeof(redir->dest.name));
150                         redir->dest.set = true;
151                         redir->_dest = zone;
152
153                         break;
154                 }
155
156                 fw3_free_list(addrs);
157
158                 if (redir->_dest)
159                         return true;
160         }
161
162         return false;
163 }
164
165 static bool
166 check_local(struct uci_element *e, struct fw3_redirect *redir,
167             struct fw3_state *state)
168 {
169         if (redir->target != FW3_FLAG_DNAT)
170                 return false;
171
172         if (!redir->ip_redir.set)
173                 redir->local = true;
174
175         return redir->local;
176 }
177
178 static bool
179 check_redirect(struct fw3_state *state, struct fw3_redirect *redir, struct uci_element *e)
180 {
181         bool valid;
182
183         if (!redir->enabled)
184                 return false;
185
186         if (redir->src.invert)
187         {
188                 warn_section("redirect", redir, e, "must not have an inverted source");
189                 return false;
190         }
191         else if (redir->src.set && !redir->src.any &&
192                         !(redir->_src = fw3_lookup_zone(state, redir->src.name)))
193         {
194                 warn_section("redirect", redir, e, "refers to not existing zone '%s'",
195                                 redir->src.name);
196                 return false;
197         }
198         else if (redir->dest.set && !redir->dest.any &&
199                         !(redir->_dest = fw3_lookup_zone(state, redir->dest.name)))
200         {
201                 warn_section("redirect", redir, e, "refers to not existing zone '%s'",
202                                 redir->dest.name);
203                 return false;
204         }
205         else if (redir->ipset.set && state->disable_ipsets)
206         {
207                 warn_section("redirect", redir, e, "skipped due to disabled ipset support",
208                                 redir->name);
209                 return false;
210         }
211         else if (redir->ipset.set &&
212                         !(redir->ipset.ptr = fw3_lookup_ipset(state, redir->ipset.name)))
213         {
214                 warn_section("redirect", redir, e, "refers to unknown ipset '%s'", redir->name,
215                                 redir->ipset.name);
216                 return false;
217         }
218
219         if (!check_families(e, redir))
220                 return false;
221
222         if (redir->target == FW3_FLAG_UNSPEC)
223         {
224                 warn_section("redirect", redir, e, "has no target specified, defaulting to DNAT");
225                 redir->target = FW3_FLAG_DNAT;
226         }
227         else if (redir->target < FW3_FLAG_DNAT || redir->target > FW3_FLAG_SNAT)
228         {
229                 warn_section("redirect", redir, e, "has invalid target specified, defaulting to DNAT");
230                 redir->target = FW3_FLAG_DNAT;
231         }
232
233         valid = false;
234
235         if (redir->target == FW3_FLAG_DNAT)
236         {
237                 if (redir->src.any)
238                         warn_section("redirect", redir, e, "must not have source '*' for DNAT target");
239                 else if (!redir->_src)
240                         warn_section("redirect", redir, e, "has no source specified");
241                 else
242                 {
243                         set(redir->_src->flags, FW3_FAMILY_V4, redir->target);
244                         valid = true;
245
246                         if (!check_local(e, redir, state) && !redir->dest.set &&
247                                         resolve_dest(e, redir, state))
248                         {
249                                 warn_section("redirect", redir, e,
250                                                 "does not specify a destination, assuming '%s'",
251                                                 redir->dest.name);
252                         }
253
254                         if (redir->reflection && redir->_dest && redir->_src->masq)
255                         {
256                                 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_ACCEPT);
257                                 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_DNAT);
258                                 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
259                         }
260                 }
261         }
262         else
263         {
264                 if (redir->dest.any)
265                         warn_section("redirect", redir, e,
266                                         "must not have destination '*' for SNAT target");
267                 else if (!redir->_dest)
268                         warn_section("redirect", redir, e, "has no destination specified");
269                 else if (!redir->ip_dest.set)
270                         warn_section("redirect", redir, e, "has no src_dip option specified");
271                 else if (!list_empty(&redir->mac_src))
272                         warn_section("redirect", redir, e, "must not use 'src_mac' option for SNAT target");
273                 else
274                 {
275                         set(redir->_dest->flags, FW3_FAMILY_V4, redir->target);
276                         valid = true;
277                 }
278         }
279
280         if (list_empty(&redir->proto))
281         {
282                 warn_section("redirect", redir, e, "does not specify a protocol, assuming TCP+UDP");
283                 fw3_parse_protocol(&redir->proto, "tcpudp", true);
284         }
285
286         if (!valid)
287                 return false;
288
289         if (!redir->port_redir.set)
290                 redir->port_redir = redir->port_dest;
291
292         return true;
293 }
294
295 static struct fw3_redirect *
296 fw3_alloc_redirect(struct fw3_state *state)
297 {
298         struct fw3_redirect *redir;
299
300         redir = calloc(1, sizeof(*redir));
301         if (!redir)
302                 return NULL;
303
304         INIT_LIST_HEAD(&redir->proto);
305         INIT_LIST_HEAD(&redir->mac_src);
306
307         redir->enabled = true;
308         redir->reflection = true;
309
310         list_add_tail(&redir->list, &state->redirects);
311
312         return redir;
313 }
314
315 void
316 fw3_load_redirects(struct fw3_state *state, struct uci_package *p,
317                 struct blob_attr *a)
318 {
319         struct uci_section *s;
320         struct uci_element *e;
321         struct fw3_redirect *redir;
322         struct blob_attr *entry;
323         unsigned rem;
324
325         INIT_LIST_HEAD(&state->redirects);
326
327         blob_for_each_attr(entry, a, rem)
328         {
329                 const char *type;
330                 const char *name = "ubus redirect";
331
332                 if (!fw3_attr_parse_name_type(entry, &name, &type))
333                         continue;
334
335                 if (strcmp(type, "redirect"))
336                         continue;
337
338                 redir = fw3_alloc_redirect(state);
339                 if (!redir)
340                         continue;
341
342                 if (!fw3_parse_blob_options(redir, fw3_redirect_opts, entry, name))
343                 {
344                         warn_section("redirect", redir, NULL, "skipped due to invalid options");
345                         fw3_free_redirect(redir);
346                         continue;
347                 }
348
349                 if (!check_redirect(state, redir, NULL))
350                         fw3_free_redirect(redir);
351         }
352
353         uci_foreach_element(&p->sections, e)
354         {
355                 s = uci_to_section(e);
356
357                 if (strcmp(s->type, "redirect"))
358                         continue;
359
360                 redir = fw3_alloc_redirect(state);
361                 if (!redir)
362                         continue;
363
364                 if (!fw3_parse_options(redir, fw3_redirect_opts, s))
365                 {
366                         warn_elem(e, "skipped due to invalid options");
367                         fw3_free_redirect(redir);
368                         continue;
369                 }
370
371                 if (!check_redirect(state, redir, e))
372                         fw3_free_redirect(redir);
373         }
374 }
375
376 static void
377 append_chain_nat(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
378 {
379         if (redir->target == FW3_FLAG_DNAT)
380                 fw3_ipt_rule_append(r, "zone_%s_prerouting", redir->src.name);
381         else
382                 fw3_ipt_rule_append(r, "zone_%s_postrouting", redir->dest.name);
383 }
384
385 static void
386 set_redirect(struct fw3_ipt_rule *r, struct fw3_port *port)
387 {
388         char buf[sizeof("65535-65535\0")];
389
390         fw3_ipt_rule_target(r, "REDIRECT");
391
392         if (port && port->set)
393         {
394                 if (port->port_min == port->port_max)
395                         sprintf(buf, "%u", port->port_min);
396                 else
397                         sprintf(buf, "%u-%u", port->port_min, port->port_max);
398
399                 fw3_ipt_rule_addarg(r, false, "--to-ports", buf);
400         }
401 }
402
403 static void
404 set_snat_dnat(struct fw3_ipt_rule *r, enum fw3_flag target,
405               struct fw3_address *addr, struct fw3_port *port)
406 {
407         char buf[sizeof("255.255.255.255:65535-65535\0")];
408
409         buf[0] = '\0';
410
411         if (addr && addr->set)
412         {
413                 inet_ntop(AF_INET, &addr->address.v4, buf, sizeof(buf));
414         }
415
416         if (port && port->set)
417         {
418                 if (port->port_min == port->port_max)
419                         sprintf(buf + strlen(buf), ":%u", port->port_min);
420                 else
421                         sprintf(buf + strlen(buf), ":%u-%u",
422                                 port->port_min, port->port_max);
423         }
424
425         if (target == FW3_FLAG_DNAT)
426         {
427                 fw3_ipt_rule_target(r, "DNAT");
428                 fw3_ipt_rule_addarg(r, false, "--to-destination", buf);
429         }
430         else
431         {
432                 fw3_ipt_rule_target(r, "SNAT");
433                 fw3_ipt_rule_addarg(r, false, "--to-source", buf);
434         }
435 }
436
437 static void
438 set_target_nat(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
439 {
440         if (redir->local)
441                 set_redirect(r, &redir->port_redir);
442         else if (redir->target == FW3_FLAG_DNAT)
443                 set_snat_dnat(r, redir->target, &redir->ip_redir, &redir->port_redir);
444         else
445                 set_snat_dnat(r, redir->target, &redir->ip_dest, &redir->port_dest);
446 }
447
448 static void
449 set_comment(struct fw3_ipt_rule *r, const char *name, int num, bool ref)
450 {
451         if (name)
452         {
453                 if (ref)
454                         fw3_ipt_rule_comment(r, "%s (reflection)", name);
455                 else
456                         fw3_ipt_rule_comment(r, name);
457         }
458         else
459         {
460                 if (ref)
461                         fw3_ipt_rule_comment(r, "@redirect[%u] (reflection)", num);
462                 else
463                         fw3_ipt_rule_comment(r, "@redirect[%u]", num);
464         }
465 }
466
467 static void
468 print_redirect(struct fw3_ipt_handle *h, struct fw3_state *state,
469                struct fw3_redirect *redir, int num,
470                struct fw3_protocol *proto, struct fw3_mac *mac)
471 {
472         struct fw3_ipt_rule *r;
473         struct fw3_address *src, *dst;
474         struct fw3_port *spt, *dpt;
475
476         switch (h->table)
477         {
478         case FW3_TABLE_NAT:
479                 src = &redir->ip_src;
480                 dst = &redir->ip_dest;
481                 spt = &redir->port_src;
482                 dpt = &redir->port_dest;
483
484                 if (redir->target == FW3_FLAG_SNAT)
485                 {
486                         dst = &redir->ip_redir;
487                         dpt = &redir->port_redir;
488                 }
489
490                 r = fw3_ipt_rule_create(h, proto, NULL, NULL, src, dst);
491                 fw3_ipt_rule_sport_dport(r, spt, dpt);
492                 fw3_ipt_rule_mac(r, mac);
493                 fw3_ipt_rule_ipset(r, &redir->ipset);
494                 fw3_ipt_rule_limit(r, &redir->limit);
495                 fw3_ipt_rule_time(r, &redir->time);
496                 fw3_ipt_rule_mark(r, &redir->mark);
497                 set_target_nat(r, redir);
498                 fw3_ipt_rule_extra(r, redir->extra);
499                 set_comment(r, redir->name, num, false);
500                 append_chain_nat(r, redir);
501                 break;
502
503         default:
504                 break;
505         }
506 }
507
508 static void
509 print_reflection(struct fw3_ipt_handle *h, struct fw3_state *state,
510                  struct fw3_redirect *redir, int num,
511                  struct fw3_protocol *proto, struct fw3_address *ra,
512                  struct fw3_address *ia, struct fw3_address *ea)
513 {
514         struct fw3_ipt_rule *r;
515
516         switch (h->table)
517         {
518         case FW3_TABLE_NAT:
519                 r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, ea);
520                 fw3_ipt_rule_sport_dport(r, NULL, &redir->port_dest);
521                 fw3_ipt_rule_limit(r, &redir->limit);
522                 fw3_ipt_rule_time(r, &redir->time);
523                 set_comment(r, redir->name, num, true);
524                 set_snat_dnat(r, FW3_FLAG_DNAT, &redir->ip_redir, &redir->port_redir);
525                 fw3_ipt_rule_replace(r, "zone_%s_prerouting", redir->dest.name);
526
527                 r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, &redir->ip_redir);
528                 fw3_ipt_rule_sport_dport(r, NULL, &redir->port_redir);
529                 fw3_ipt_rule_limit(r, &redir->limit);
530                 fw3_ipt_rule_time(r, &redir->time);
531                 set_comment(r, redir->name, num, true);
532                 set_snat_dnat(r, FW3_FLAG_SNAT, ra, NULL);
533                 fw3_ipt_rule_replace(r, "zone_%s_postrouting", redir->dest.name);
534                 break;
535
536         default:
537                 break;
538         }
539 }
540
541 static void
542 expand_redirect(struct fw3_ipt_handle *handle, struct fw3_state *state,
543                 struct fw3_redirect *redir, int num)
544 {
545         struct list_head *ext_addrs, *int_addrs;
546         struct fw3_address *ext_addr, *int_addr, ref_addr;
547         struct fw3_protocol *proto;
548         struct fw3_mac *mac;
549
550         if (redir->name)
551                 info("   * Redirect '%s'", redir->name);
552         else
553                 info("   * Redirect #%u", num);
554
555         if (!fw3_is_family(redir->_src, handle->family) ||
556                 !fw3_is_family(redir->_dest, handle->family))
557         {
558                 info("     ! Skipping due to different family of zone");
559                 return;
560         }
561
562         if (!fw3_is_family(&redir->ip_src, handle->family) ||
563             !fw3_is_family(&redir->ip_dest, handle->family) ||
564                 !fw3_is_family(&redir->ip_redir, handle->family))
565         {
566                 if (!redir->ip_src.resolved ||
567                     !redir->ip_dest.resolved ||
568                     !redir->ip_redir.resolved)
569                         info("     ! Skipping due to different family of ip address");
570
571                 return;
572         }
573
574         if (redir->ipset.ptr)
575         {
576                 if (!fw3_is_family(redir->ipset.ptr, handle->family))
577                 {
578                         info("     ! Skipping due to different family in ipset");
579                         return;
580                 }
581
582                 if (!fw3_check_ipset(redir->ipset.ptr))
583                 {
584                         info("     ! Skipping due to missing ipset '%s'",
585                              redir->ipset.ptr->external ?
586                                         redir->ipset.ptr->external : redir->ipset.ptr->name);
587                         return;
588                 }
589
590                 set(redir->ipset.ptr->flags, handle->family, handle->family);
591         }
592
593         fw3_foreach(proto, &redir->proto)
594         fw3_foreach(mac, &redir->mac_src)
595                 print_redirect(handle, state, redir, num, proto, mac);
596
597         /* reflection rules */
598         if (redir->target != FW3_FLAG_DNAT || !redir->reflection || redir->local)
599                 return;
600
601         if (!redir->_dest || !redir->_src->masq)
602                 return;
603
604         ext_addrs = fw3_resolve_zone_addresses(redir->_src, &redir->ip_dest);
605         int_addrs = fw3_resolve_zone_addresses(redir->_dest, NULL);
606
607         if (!ext_addrs || !int_addrs)
608                 goto out;
609
610         list_for_each_entry(ext_addr, ext_addrs, list)
611         {
612                 if (!fw3_is_family(ext_addr, handle->family))
613                         continue;
614
615                 list_for_each_entry(int_addr, int_addrs, list)
616                 {
617                         if (!fw3_is_family(int_addr, handle->family))
618                                 continue;
619
620                         fw3_foreach(proto, &redir->proto)
621                         {
622                                 if (!proto)
623                                         continue;
624
625                                 if (redir->reflection_src == FW3_REFLECTION_INTERNAL)
626                                         ref_addr = *int_addr;
627                                 else
628                                         ref_addr = *ext_addr;
629
630                                 ref_addr.mask.v4.s_addr = 0xFFFFFFFF;
631                                 ext_addr->mask.v4.s_addr = 0xFFFFFFFF;
632
633                                 print_reflection(handle, state, redir, num, proto,
634                                                                  &ref_addr, int_addr, ext_addr);
635                         }
636                 }
637         }
638
639 out:
640         fw3_free_list(ext_addrs);
641         fw3_free_list(int_addrs);
642 }
643
644 void
645 fw3_print_redirects(struct fw3_ipt_handle *handle, struct fw3_state *state)
646 {
647         int num = 0;
648         struct fw3_redirect *redir;
649
650         if (handle->family == FW3_FAMILY_V6)
651                 return;
652
653         if (handle->table != FW3_TABLE_FILTER && handle->table != FW3_TABLE_NAT)
654                 return;
655
656         list_for_each_entry(redir, &state->redirects, list)
657                 expand_redirect(handle, state, redir, num++);
658 }