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