Skip redirects with invalid options
[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",               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         uint32_t mask;
120
121         if (a->family != FW3_FAMILY_V4 || b->family != FW3_FAMILY_V4)
122                 return false;
123
124         mask = htonl(~((1 << (32 - a->mask)) - 1));
125
126         return ((a->address.v4.s_addr & mask) == (b->address.v4.s_addr & mask));
127 }
128
129 static bool
130 resolve_dest(struct uci_element *e, struct fw3_redirect *redir,
131              struct fw3_state *state)
132 {
133         struct fw3_zone *zone;
134         struct fw3_address *addr;
135         struct list_head *addrs;
136
137         if (!redir->ip_redir.set)
138                 return false;
139
140         list_for_each_entry(zone, &state->zones, list)
141         {
142                 addrs = fw3_resolve_zone_addresses(zone);
143
144                 if (!addrs)
145                         continue;
146
147                 list_for_each_entry(addr, addrs, list)
148                 {
149                         if (!compare_addr(addr, &redir->ip_redir))
150                                 continue;
151
152                         strncpy(redir->dest.name, zone->name, sizeof(redir->dest.name));
153                         redir->dest.set = true;
154                         redir->_dest = zone;
155
156                         break;
157                 }
158
159                 fw3_free_list(addrs);
160
161                 if (redir->_dest)
162                         return true;
163         }
164
165         return false;
166 }
167
168 static bool
169 check_local(struct uci_element *e, struct fw3_redirect *redir,
170             struct fw3_state *state)
171 {
172         struct fw3_zone *zone;
173         struct fw3_device *net;
174         struct fw3_address *addr;
175         struct list_head *addrs;
176
177         if (redir->target != FW3_FLAG_DNAT)
178                 return false;
179
180         if (!redir->ip_redir.set)
181                 redir->local = true;
182
183         if (redir->local)
184                 return true;
185
186         list_for_each_entry(zone, &state->zones, list)
187         {
188                 list_for_each_entry(net, &zone->networks, list)
189                 {
190                         addrs = fw3_ubus_address(net->name);
191
192                         if (!addrs)
193                                 continue;
194
195                         list_for_each_entry(addr, addrs, list)
196                         {
197                                 if (!compare_addr(&redir->ip_redir, addr))
198                                         continue;
199
200                                 warn_elem(e, "refers to a destination address on this router, "
201                                              "assuming port redirection");
202
203                                 redir->local = true;
204                                 break;
205                         }
206
207                         fw3_free_list(addrs);
208
209                         if (redir->local)
210                                 return true;
211                 }
212         }
213
214         return false;
215 }
216
217 void
218 fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
219 {
220         struct uci_section *s;
221         struct uci_element *e;
222         struct fw3_redirect *redir;
223
224         bool valid;
225
226         INIT_LIST_HEAD(&state->redirects);
227
228         uci_foreach_element(&p->sections, e)
229         {
230                 s = uci_to_section(e);
231
232                 if (strcmp(s->type, "redirect"))
233                         continue;
234
235                 redir = malloc(sizeof(*redir));
236
237                 if (!redir)
238                         continue;
239
240                 memset(redir, 0, sizeof(*redir));
241
242                 INIT_LIST_HEAD(&redir->proto);
243                 INIT_LIST_HEAD(&redir->mac_src);
244
245                 redir->enabled = true;
246                 redir->reflection = true;
247
248                 valid = false;
249
250                 if (!fw3_parse_options(redir, fw3_redirect_opts, s))
251                 {
252                         warn_elem(e, "skipped due to invalid options");
253                         fw3_free_redirect(redir);
254                         continue;
255                 }
256
257                 if (!redir->enabled)
258                 {
259                         fw3_free_redirect(redir);
260                         continue;
261                 }
262
263                 if (redir->src.invert)
264                 {
265                         warn_elem(e, "must not have an inverted source");
266                         fw3_free_redirect(redir);
267                         continue;
268                 }
269                 else if (redir->src.set && !redir->src.any &&
270                          !(redir->_src = fw3_lookup_zone(state, redir->src.name)))
271                 {
272                         warn_elem(e, "refers to not existing zone '%s'", redir->src.name);
273                         fw3_free_redirect(redir);
274                         continue;
275                 }
276                 else if (redir->dest.set && !redir->dest.any &&
277                          !(redir->_dest = fw3_lookup_zone(state, redir->dest.name)))
278                 {
279                         warn_elem(e, "refers to not existing zone '%s'", redir->dest.name);
280                         fw3_free_redirect(redir);
281                         continue;
282                 }
283                 else if (redir->ipset.set && state->disable_ipsets)
284                 {
285                         warn_elem(e, "skipped due to disabled ipset support");
286                         fw3_free_redirect(redir);
287                         continue;
288                 }
289                 else if (redir->ipset.set &&
290                          !(redir->ipset.ptr = fw3_lookup_ipset(state, redir->ipset.name)))
291                 {
292                         warn_elem(e, "refers to unknown ipset '%s'", redir->ipset.name);
293                         fw3_free_redirect(redir);
294                         continue;
295                 }
296
297                 if (!check_families(e, redir))
298                 {
299                         fw3_free_redirect(redir);
300                         continue;
301                 }
302
303                 if (redir->target == FW3_FLAG_UNSPEC)
304                 {
305                         warn_elem(e, "has no target specified, defaulting to DNAT");
306                         redir->target = FW3_FLAG_DNAT;
307                 }
308                 else if (redir->target < FW3_FLAG_DNAT)
309                 {
310                         warn_elem(e, "has invalid target specified, defaulting to DNAT");
311                         redir->target = FW3_FLAG_DNAT;
312                 }
313
314                 if (redir->target == FW3_FLAG_DNAT)
315                 {
316                         if (redir->src.any)
317                                 warn_elem(e, "must not have source '*' for DNAT target");
318                         else if (!redir->_src)
319                                 warn_elem(e, "has no source specified");
320                         else
321                         {
322                                 set(redir->_src->flags, FW3_FAMILY_V4, redir->target);
323                                 redir->_src->conntrack = true;
324                                 valid = true;
325                         }
326
327                         if (!check_local(e, redir, state) && !redir->dest.set &&
328                             resolve_dest(e, redir, state))
329                         {
330                                 warn_elem(e, "does not specify a destination, assuming '%s'",
331                                           redir->dest.name);
332                         }
333
334                         if (redir->reflection && redir->_dest && redir->_src->masq)
335                         {
336                                 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_ACCEPT);
337                                 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_DNAT);
338                                 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
339                         }
340                 }
341                 else
342                 {
343                         if (redir->dest.any)
344                                 warn_elem(e, "must not have destination '*' for SNAT target");
345                         else if (!redir->_dest)
346                                 warn_elem(e, "has no destination specified");
347                         else if (!redir->ip_dest.set)
348                                 warn_elem(e, "has no src_dip option specified");
349                         else if (!list_empty(&redir->mac_src))
350                                 warn_elem(e, "must not use 'src_mac' option for SNAT target");
351                         else
352                         {
353                                 set(redir->_dest->flags, FW3_FAMILY_V4, redir->target);
354                                 redir->_dest->conntrack = true;
355                                 valid = true;
356                         }
357                 }
358
359                 if (list_empty(&redir->proto))
360                 {
361                         warn_elem(e, "does not specify a protocol, assuming TCP+UDP");
362                         fw3_parse_protocol(&redir->proto, "tcpudp", true);
363                 }
364
365                 if (!valid)
366                 {
367                         fw3_free_redirect(redir);
368                         continue;
369                 }
370
371                 if (!redir->port_redir.set)
372                         redir->port_redir = redir->port_dest;
373
374                 list_add_tail(&redir->list, &state->redirects);
375         }
376 }
377
378 static void
379 append_chain_nat(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
380 {
381         if (redir->target == FW3_FLAG_DNAT)
382                 fw3_ipt_rule_append(r, "zone_%s_prerouting", redir->src.name);
383         else
384                 fw3_ipt_rule_append(r, "zone_%s_postrouting", redir->dest.name);
385 }
386
387 static void
388 set_snat_dnat(struct fw3_ipt_rule *r, enum fw3_flag target,
389               struct fw3_address *addr, struct fw3_port *port)
390 {
391         char buf[sizeof("255.255.255.255:65535-65535\0")];
392
393         buf[0] = '\0';
394
395         if (addr && addr->set)
396         {
397                 inet_ntop(AF_INET, &addr->address.v4, buf, sizeof(buf));
398         }
399
400         if (port && port->set)
401         {
402                 if (port->port_min == port->port_max)
403                         sprintf(buf + strlen(buf), ":%u", port->port_min);
404                 else
405                         sprintf(buf + strlen(buf), ":%u-%u",
406                                 port->port_min, port->port_max);
407         }
408
409         if (target == FW3_FLAG_DNAT)
410         {
411                 fw3_ipt_rule_target(r, "DNAT");
412                 fw3_ipt_rule_addarg(r, false, "--to-destination", buf);
413         }
414         else
415         {
416                 fw3_ipt_rule_target(r, "SNAT");
417                 fw3_ipt_rule_addarg(r, false, "--to-source", buf);
418         }
419 }
420
421 static void
422 set_target_nat(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
423 {
424         if (redir->target == FW3_FLAG_DNAT)
425                 set_snat_dnat(r, redir->target, &redir->ip_redir, &redir->port_redir);
426         else
427                 set_snat_dnat(r, redir->target, &redir->ip_dest, &redir->port_dest);
428 }
429
430 //static void
431 //append_chain_filter(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
432 //{
433 //      if (redir->target == FW3_FLAG_DNAT)
434 //      {
435 //              if (redir->local)
436 //                      fw3_ipt_rule_append(r, "zone_%s_input", redir->src.name);
437 //              else
438 //                      fw3_ipt_rule_append(r, "zone_%s_forward", redir->src.name);
439 //      }
440 //      else
441 //      {
442 //              if (redir->src.set && !redir->src.any)
443 //                      fw3_ipt_rule_append(r, "zone_%s_forward", redir->src.name);
444 //              else
445 //                      fw3_ipt_rule_append(r, "delegate_forward");
446 //      }
447 //}
448 //
449 //static void
450 //set_target_filter(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
451 //{
452 //      if (redir->local)
453 //              fw3_ipt_rule_extra(r, "-m conntrack --ctstate DNAT");
454 //
455 //      fw3_ipt_rule_target(r, "ACCEPT");
456 //}
457
458 static void
459 set_comment(struct fw3_ipt_rule *r, const char *name, int num, bool ref)
460 {
461         if (name)
462         {
463                 if (ref)
464                         fw3_ipt_rule_comment(r, "%s (reflection)", name);
465                 else
466                         fw3_ipt_rule_comment(r, name);
467         }
468         else
469         {
470                 if (ref)
471                         fw3_ipt_rule_comment(r, "@redirect[%u] (reflection)", num);
472                 else
473                         fw3_ipt_rule_comment(r, "@redirect[%u]", num);
474         }
475 }
476
477 static void
478 print_redirect(struct fw3_ipt_handle *h, struct fw3_state *state,
479                struct fw3_redirect *redir, int num,
480                struct fw3_protocol *proto, struct fw3_mac *mac)
481 {
482         struct fw3_ipt_rule *r;
483         struct fw3_address *src, *dst;
484         struct fw3_port *spt, *dpt;
485
486         switch (h->table)
487         {
488         case FW3_TABLE_NAT:
489                 src = &redir->ip_src;
490                 dst = &redir->ip_dest;
491                 spt = &redir->port_src;
492                 dpt = &redir->port_dest;
493
494                 if (redir->target == FW3_FLAG_SNAT)
495                 {
496                         dst = &redir->ip_redir;
497                         dpt = &redir->port_redir;
498                 }
499
500                 r = fw3_ipt_rule_create(h, proto, NULL, NULL, src, dst);
501                 fw3_ipt_rule_sport_dport(r, spt, dpt);
502                 fw3_ipt_rule_mac(r, mac);
503                 fw3_ipt_rule_ipset(r, &redir->ipset);
504                 fw3_ipt_rule_limit(r, &redir->limit);
505                 fw3_ipt_rule_time(r, &redir->time);
506                 fw3_ipt_rule_mark(r, &redir->mark);
507                 set_target_nat(r, redir);
508                 fw3_ipt_rule_extra(r, redir->extra);
509                 set_comment(r, redir->name, num, false);
510                 append_chain_nat(r, redir);
511                 break;
512
513         case FW3_TABLE_FILTER:
514                 //src = &redir->ip_src;
515                 //dst = &redir->ip_redir;
516                 //spt = &redir->port_src;
517                 //dpt = &redir->port_redir;
518                 //
519                 //r = fw3_ipt_rule_create(h, proto, NULL, NULL, src, dst);
520                 //fw3_ipt_rule_sport_dport(r, spt, dpt);
521                 //fw3_ipt_rule_mac(r, mac);
522                 //fw3_ipt_rule_ipset(r, &redir->ipset);
523                 //fw3_ipt_rule_limit(r, &redir->limit);
524                 //fw3_ipt_rule_time(r, &redir->time);
525                 //fw3_ipt_rule_mark(r, &redir->mark);
526                 //set_target_filter(r, redir);
527                 //fw3_ipt_rule_extra(r, redir->extra);
528                 //set_comment(r, redir->name, num, false);
529                 //append_chain_filter(r, redir);
530                 break;
531
532         default:
533                 break;
534         }
535 }
536
537 static void
538 print_reflection(struct fw3_ipt_handle *h, struct fw3_state *state,
539                  struct fw3_redirect *redir, int num,
540                  struct fw3_protocol *proto, struct fw3_address *ra,
541                  struct fw3_address *ia, struct fw3_address *ea)
542 {
543         struct fw3_ipt_rule *r;
544
545         switch (h->table)
546         {
547         case FW3_TABLE_NAT:
548                 r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, ea);
549                 fw3_ipt_rule_sport_dport(r, NULL, &redir->port_dest);
550                 fw3_ipt_rule_limit(r, &redir->limit);
551                 fw3_ipt_rule_time(r, &redir->time);
552                 set_comment(r, redir->name, num, true);
553                 set_snat_dnat(r, FW3_FLAG_DNAT, &redir->ip_redir, &redir->port_redir);
554                 fw3_ipt_rule_replace(r, "zone_%s_prerouting", redir->dest.name);
555
556                 r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, &redir->ip_redir);
557                 fw3_ipt_rule_sport_dport(r, NULL, &redir->port_redir);
558                 fw3_ipt_rule_limit(r, &redir->limit);
559                 fw3_ipt_rule_time(r, &redir->time);
560                 set_comment(r, redir->name, num, true);
561                 set_snat_dnat(r, FW3_FLAG_SNAT, ra, NULL);
562                 fw3_ipt_rule_replace(r, "zone_%s_postrouting", redir->dest.name);
563                 break;
564
565         //case FW3_TABLE_FILTER:
566         //      r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, &redir->ip_redir);
567         //      fw3_ipt_rule_sport_dport(r, NULL, &redir->port_redir);
568         //      fw3_ipt_rule_limit(r, &redir->limit);
569         //      fw3_ipt_rule_time(r, &redir->time);
570         //      set_comment(r, redir->name, num, true);
571         //      fw3_ipt_rule_target(r, "zone_%s_dest_ACCEPT", redir->dest.name);
572         //      fw3_ipt_rule_replace(r, "zone_%s_forward", redir->dest.name);
573         //      break;
574
575         default:
576                 break;
577         }
578 }
579
580 static void
581 expand_redirect(struct fw3_ipt_handle *handle, struct fw3_state *state,
582                 struct fw3_redirect *redir, int num)
583 {
584         struct list_head *ext_addrs, *int_addrs;
585         struct fw3_address *ext_addr, *int_addr, ref_addr;
586         struct fw3_protocol *proto;
587         struct fw3_mac *mac;
588
589         if (redir->name)
590                 info("   * Redirect '%s'", redir->name);
591         else
592                 info("   * Redirect #%u", num);
593
594         if (!fw3_is_family(redir->_src, handle->family) ||
595                 !fw3_is_family(redir->_dest, handle->family))
596         {
597                 info("     ! Skipping due to different family of zone");
598                 return;
599         }
600
601         if (!fw3_is_family(&redir->ip_src, handle->family) ||
602             !fw3_is_family(&redir->ip_dest, handle->family) ||
603                 !fw3_is_family(&redir->ip_redir, handle->family))
604         {
605                 if (!redir->ip_src.resolved ||
606                     !redir->ip_dest.resolved ||
607                     !redir->ip_redir.resolved)
608                         info("     ! Skipping due to different family of ip address");
609
610                 return;
611         }
612
613         if (redir->ipset.ptr)
614         {
615                 if (!fw3_is_family(redir->ipset.ptr, handle->family))
616                 {
617                         info("     ! Skipping due to different family in ipset");
618                         return;
619                 }
620
621                 if (!fw3_check_ipset(redir->ipset.ptr))
622                 {
623                         info("     ! Skipping due to missing ipset '%s'",
624                              redir->ipset.ptr->external ?
625                                         redir->ipset.ptr->external : redir->ipset.ptr->name);
626                         return;
627                 }
628
629                 set(redir->ipset.ptr->flags, handle->family, handle->family);
630         }
631
632         fw3_foreach(proto, &redir->proto)
633         fw3_foreach(mac, &redir->mac_src)
634                 print_redirect(handle, state, redir, num, proto, mac);
635
636         /* reflection rules */
637         if (redir->target != FW3_FLAG_DNAT || !redir->reflection)
638                 return;
639
640         if (!redir->_dest || !redir->_src->masq)
641                 return;
642
643         ext_addrs = fw3_resolve_zone_addresses(redir->_src);
644         int_addrs = fw3_resolve_zone_addresses(redir->_dest);
645
646         if (!ext_addrs || !int_addrs)
647                 goto out;
648
649         list_for_each_entry(ext_addr, ext_addrs, list)
650         {
651                 if (!fw3_is_family(ext_addr, handle->family))
652                         continue;
653
654                 list_for_each_entry(int_addr, int_addrs, list)
655                 {
656                         if (!fw3_is_family(int_addr, handle->family))
657                                 continue;
658
659                         fw3_foreach(proto, &redir->proto)
660                         {
661                                 if (!proto)
662                                         continue;
663
664                                 if (redir->reflection_src == FW3_REFLECTION_INTERNAL)
665                                         ref_addr = *int_addr;
666                                 else
667                                         ref_addr = *ext_addr;
668
669                                 ref_addr.mask = 32;
670                                 ext_addr->mask = 32;
671
672                                 print_reflection(handle, state, redir, num, proto,
673                                                                  &ref_addr, int_addr, ext_addr);
674                         }
675                 }
676         }
677
678 out:
679         fw3_free_list(ext_addrs);
680         fw3_free_list(int_addrs);
681 }
682
683 void
684 fw3_print_redirects(struct fw3_ipt_handle *handle, struct fw3_state *state)
685 {
686         int num = 0;
687         struct fw3_redirect *redir;
688
689         if (handle->family == FW3_FAMILY_V6)
690                 return;
691
692         if (handle->table != FW3_TABLE_FILTER && handle->table != FW3_TABLE_NAT)
693                 return;
694
695         list_for_each_entry(redir, &state->redirects, list)
696                 expand_redirect(handle, state, redir, num++);
697 }