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