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