Rework option parsing to support emitting multiple values from within a parse handler
[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",               device,    redirect,     ipset),
32
33         FW3_LIST("proto",              protocol,  redirect,     proto),
34
35         FW3_OPT("src_ip",              address,   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",             address,   redirect,     ip_dest),
40         FW3_OPT("src_dport",           port,      redirect,     port_dest),
41
42         FW3_OPT("dest_ip",             address,   redirect,     ip_redir),
43         FW3_OPT("dest_port",           port,      redirect,     port_redir),
44
45         FW3_OPT("extra",               string,    redirect,     extra),
46
47         FW3_OPT("utc_time",            bool,      redirect,     time.utc),
48         FW3_OPT("start_date",          date,      redirect,     time.datestart),
49         FW3_OPT("stop_date",           date,      redirect,     time.datestop),
50         FW3_OPT("start_time",          time,      redirect,     time.timestart),
51         FW3_OPT("stop_time",           time,      redirect,     time.timestop),
52         FW3_OPT("weekdays",            weekdays,  redirect,     time.weekdays),
53         FW3_OPT("monthdays",           monthdays, redirect,     time.monthdays),
54
55         FW3_OPT("reflection",          bool,      redirect,     reflection),
56         FW3_OPT("reflection_src",      reflection_source,
57                                                   redirect,     reflection_src),
58
59         FW3_OPT("target",              target,    redirect,     target),
60
61         { }
62 };
63
64
65 static bool
66 check_families(struct uci_element *e, struct fw3_redirect *r)
67 {
68         if (r->family == FW3_FAMILY_ANY)
69                 return true;
70
71         if (r->_src && r->_src->family && r->_src->family != r->family)
72         {
73                 warn_elem(e, "refers to source zone with different family");
74                 return false;
75         }
76
77         if (r->_dest && r->_dest->family && r->_dest->family != r->family)
78         {
79                 warn_elem(e, "refers to destination zone with different family");
80                 return false;
81         }
82
83         if (r->_ipset && r->_ipset->family && r->_ipset->family != r->family)
84         {
85                 warn_elem(e, "refers to ipset with different family");
86                 return false;
87         }
88
89         if (r->ip_src.family && r->ip_src.family != r->family)
90         {
91                 warn_elem(e, "uses source ip with different family");
92                 return false;
93         }
94
95         if (r->ip_dest.family && r->ip_dest.family != r->family)
96         {
97                 warn_elem(e, "uses destination ip with different family");
98                 return false;
99         }
100
101         if (r->ip_redir.family && r->ip_redir.family != r->family)
102         {
103                 warn_elem(e, "uses redirect ip with different family");
104                 return false;
105         }
106
107         return true;
108 }
109
110 void
111 fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
112 {
113         struct uci_section *s;
114         struct uci_element *e;
115         struct fw3_redirect *redir;
116
117         bool valid;
118
119         INIT_LIST_HEAD(&state->redirects);
120
121         uci_foreach_element(&p->sections, e)
122         {
123                 s = uci_to_section(e);
124
125                 if (strcmp(s->type, "redirect"))
126                         continue;
127
128                 redir = malloc(sizeof(*redir));
129
130                 if (!redir)
131                         continue;
132
133                 memset(redir, 0, sizeof(*redir));
134
135                 INIT_LIST_HEAD(&redir->proto);
136                 INIT_LIST_HEAD(&redir->mac_src);
137
138                 redir->enabled = true;
139                 redir->reflection = true;
140
141                 valid = false;
142
143                 fw3_parse_options(redir, fw3_redirect_opts, s);
144
145                 if (!redir->enabled)
146                 {
147                         fw3_free_redirect(redir);
148                         continue;
149                 }
150
151                 if (redir->src.invert)
152                 {
153                         warn_elem(e, "must not have an inverted source");
154                         fw3_free_redirect(redir);
155                         continue;
156                 }
157                 else if (redir->src.set && !redir->src.any &&
158                          !(redir->_src = fw3_lookup_zone(state, redir->src.name, false)))
159                 {
160                         warn_elem(e, "refers to not existing zone '%s'", redir->src.name);
161                         fw3_free_redirect(redir);
162                         continue;
163                 }
164                 else if (redir->dest.set && !redir->dest.any &&
165                          !(redir->_dest = fw3_lookup_zone(state, redir->dest.name, false)))
166                 {
167                         warn_elem(e, "refers to not existing zone '%s'", redir->dest.name);
168                         fw3_free_redirect(redir);
169                         continue;
170                 }
171                 else if (redir->ipset.set && state->disable_ipsets)
172                 {
173                         warn_elem(e, "skipped due to disabled ipset support");
174                         fw3_free_redirect(redir);
175                         continue;
176                 }
177                 else if (redir->ipset.set && !redir->ipset.any &&
178                          !(redir->_ipset = fw3_lookup_ipset(state, redir->ipset.name, false)))
179                 {
180                         warn_elem(e, "refers to unknown ipset '%s'", redir->ipset.name);
181                         fw3_free_redirect(redir);
182                         continue;
183                 }
184
185                 if (!check_families(e, redir))
186                 {
187                         fw3_free_redirect(redir);
188                         continue;
189                 }
190
191                 if (redir->target == FW3_FLAG_UNSPEC)
192                 {
193                         warn_elem(e, "has no target specified, defaulting to DNAT");
194                         redir->target = FW3_FLAG_DNAT;
195                 }
196                 else if (redir->target < FW3_FLAG_DNAT)
197                 {
198                         warn_elem(e, "has invalid target specified, defaulting to DNAT");
199                         redir->target = FW3_FLAG_DNAT;
200                 }
201
202                 if (redir->target == FW3_FLAG_DNAT)
203                 {
204                         if (redir->src.any)
205                                 warn_elem(e, "must not have source '*' for DNAT target");
206                         else if (!redir->_src)
207                                 warn_elem(e, "has no source specified");
208                         else
209                         {
210                                 set(redir->_src->flags, FW3_FAMILY_V4, redir->target);
211                                 redir->_src->conntrack = true;
212                                 valid = true;
213                         }
214
215                         if (redir->reflection && redir->_dest && redir->_src->masq)
216                         {
217                                 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_ACCEPT);
218                                 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_DNAT);
219                                 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
220                         }
221                 }
222                 else
223                 {
224                         if (redir->dest.any)
225                                 warn_elem(e, "must not have destination '*' for SNAT target");
226                         else if (!redir->_dest)
227                                 warn_elem(e, "has no destination specified");
228                         else if (!redir->ip_dest.set)
229                                 warn_elem(e, "has no src_dip option specified");
230                         else if (!list_empty(&redir->mac_src))
231                                 warn_elem(e, "must not use 'src_mac' option for SNAT target");
232                         else
233                         {
234                                 set(redir->_dest->flags, FW3_FAMILY_V4, redir->target);
235                                 redir->_dest->conntrack = true;
236                                 valid = true;
237                         }
238                 }
239
240                 if (!valid)
241                 {
242                         fw3_free_redirect(redir);
243                         continue;
244                 }
245
246                 if (!redir->port_redir.set)
247                         redir->port_redir = redir->port_dest;
248
249                 list_add_tail(&redir->list, &state->redirects);
250         }
251 }
252
253 static void
254 print_chain_nat(struct fw3_redirect *redir)
255 {
256         if (redir->target == FW3_FLAG_DNAT)
257                 fw3_pr("-A zone_%s_prerouting", redir->src.name);
258         else
259                 fw3_pr("-A zone_%s_postrouting", redir->dest.name);
260 }
261
262 static void
263 print_snat_dnat(enum fw3_flag target,
264                 struct fw3_address *addr, struct fw3_port *port)
265 {
266         const char *t;
267         char s[sizeof("255.255.255.255 ")];
268
269         if (target == FW3_FLAG_DNAT)
270                 t = "DNAT --to-destination";
271         else
272                 t = "SNAT --to-source";
273
274         inet_ntop(AF_INET, &addr->address.v4, s, sizeof(s));
275
276         fw3_pr(" -j %s %s", t, s);
277
278         if (port && port->set)
279         {
280                 if (port->port_min == port->port_max)
281                         fw3_pr(":%u", port->port_min);
282                 else
283                         fw3_pr(":%u-%u", port->port_min, port->port_max);
284         }
285
286         fw3_pr("\n");
287 }
288
289 static void
290 print_target_nat(struct fw3_redirect *redir)
291 {
292         if (redir->target == FW3_FLAG_DNAT)
293                 print_snat_dnat(redir->target, &redir->ip_redir, &redir->port_redir);
294         else
295                 print_snat_dnat(redir->target, &redir->ip_dest, &redir->port_dest);
296 }
297
298 static void
299 print_chain_filter(struct fw3_redirect *redir)
300 {
301         if (redir->target == FW3_FLAG_DNAT)
302         {
303                 /* XXX: check for local ip */
304                 if (!redir->ip_redir.set)
305                         fw3_pr("-A zone_%s_input", redir->src.name);
306                 else
307                         fw3_pr("-A zone_%s_forward", redir->src.name);
308         }
309         else
310         {
311                 if (redir->src.set && !redir->src.any)
312                         fw3_pr("-A zone_%s_forward", redir->src.name);
313                 else
314                         fw3_pr("-A delegate_forward");
315         }
316 }
317
318 static void
319 print_target_filter(struct fw3_redirect *redir)
320 {
321         /* XXX: check for local ip */
322         if (redir->target == FW3_FLAG_DNAT && !redir->ip_redir.set)
323                 fw3_pr(" -m conntrack --ctstate DNAT -j ACCEPT\n");
324         else
325                 fw3_pr(" -j ACCEPT\n");
326 }
327
328 static void
329 print_redirect(struct fw3_state *state, enum fw3_family family,
330                enum fw3_table table, struct fw3_redirect *redir, int num)
331 {
332         struct list_head *ext_addrs, *int_addrs;
333         struct fw3_address *ext_addr, *int_addr, ref_addr;
334         struct fw3_device *ext_net, *int_net;
335         struct fw3_protocol *proto;
336         struct fw3_mac *mac;
337
338         if (redir->name)
339                 info("   * Redirect '%s'", redir->name);
340         else
341                 info("   * Redirect #%u", num);
342
343         if (!fw3_is_family(redir->_src, family) ||
344                 !fw3_is_family(redir->_dest, family))
345         {
346                 info("     ! Skipping due to different family of zone");
347                 return;
348         }
349
350         if (!fw3_is_family(&redir->ip_src, family) ||
351             !fw3_is_family(&redir->ip_dest, family) ||
352                 !fw3_is_family(&redir->ip_redir, family))
353         {
354                 info("     ! Skipping due to different family of ip address");
355                 return;
356         }
357
358         if (redir->_ipset)
359         {
360                 if (!fw3_is_family(redir->_ipset, family))
361                 {
362                         info("     ! Skipping due to different family in ipset");
363                         return;
364                 }
365
366                 set(redir->_ipset->flags, family, family);
367         }
368
369         fw3_foreach(proto, &redir->proto)
370         fw3_foreach(mac, &redir->mac_src)
371         {
372                 if (table == FW3_TABLE_NAT)
373                 {
374                         print_chain_nat(redir);
375                         fw3_format_ipset(redir->_ipset, redir->ipset.invert);
376                         fw3_format_protocol(proto, family);
377
378                         if (redir->target == FW3_FLAG_DNAT)
379                         {
380                                 fw3_format_src_dest(&redir->ip_src, &redir->ip_dest);
381                                 fw3_format_sport_dport(&redir->port_src, &redir->port_dest);
382                         }
383                         else
384                         {
385                                 fw3_format_src_dest(&redir->ip_src, &redir->ip_redir);
386                                 fw3_format_sport_dport(&redir->port_src, &redir->port_redir);
387                         }
388
389                         fw3_format_mac(mac);
390                         fw3_format_time(&redir->time);
391                         fw3_format_extra(redir->extra);
392                         fw3_format_comment(redir->name);
393                         print_target_nat(redir);
394                 }
395                 else if (table == FW3_TABLE_FILTER)
396                 {
397                         print_chain_filter(redir);
398                         fw3_format_ipset(redir->_ipset, redir->ipset.invert);
399                         fw3_format_protocol(proto, family);
400                         fw3_format_src_dest(&redir->ip_src, &redir->ip_redir);
401                         fw3_format_sport_dport(&redir->port_src, &redir->port_redir);
402                         fw3_format_mac(mac);
403                         fw3_format_time(&redir->time);
404                         fw3_format_extra(redir->extra);
405                         fw3_format_comment(redir->name);
406                         print_target_filter(redir);
407                 }
408         }
409
410         /* reflection rules */
411         if (redir->target != FW3_FLAG_DNAT || !redir->reflection)
412                 return;
413
414         if (!redir->_dest || !redir->_src->masq)
415                 return;
416
417         list_for_each_entry(ext_net, &redir->_src->networks, list)
418         {
419                 ext_addrs = fw3_ubus_address(ext_net->name);
420
421                 if (!ext_addrs || list_empty(ext_addrs))
422                         continue;
423
424                 list_for_each_entry(int_net, &redir->_dest->networks, list)
425                 {
426                         int_addrs = fw3_ubus_address(int_net->name);
427
428                         if (!int_addrs || list_empty(int_addrs))
429                                 continue;
430
431                         fw3_foreach(ext_addr, ext_addrs)
432                         fw3_foreach(int_addr, int_addrs)
433                         fw3_foreach(proto, &redir->proto)
434                         {
435                                 if (!fw3_is_family(int_addr, family) ||
436                                     !fw3_is_family(ext_addr, family))
437                                         continue;
438
439                                 if (!proto || (proto->protocol != 6 && proto->protocol != 17))
440                                         continue;
441
442                                 if (redir->reflection_src == FW3_REFLECTION_INTERNAL)
443                                         ref_addr = *int_addr;
444                                 else
445                                         ref_addr = *ext_addr;
446
447                                 ref_addr.mask = 32;
448                                 ext_addr->mask = 32;
449
450                                 if (table == FW3_TABLE_NAT)
451                                 {
452                                         fw3_pr("-A zone_%s_prerouting", redir->dest.name);
453                                         fw3_format_protocol(proto, family);
454                                         fw3_format_src_dest(int_addr, ext_addr);
455                                         fw3_format_sport_dport(NULL, &redir->port_dest);
456                                         fw3_format_time(&redir->time);
457                                         fw3_format_comment(redir->name, " (reflection)");
458                                         print_snat_dnat(FW3_FLAG_DNAT,
459                                                         &redir->ip_redir, &redir->port_redir);
460
461                                         fw3_pr("-A zone_%s_postrouting", redir->dest.name);
462                                         fw3_format_protocol(proto, family);
463                                         fw3_format_src_dest(int_addr, &redir->ip_redir);
464                                         fw3_format_sport_dport(NULL, &redir->port_redir);
465                                         fw3_format_time(&redir->time);
466                                         fw3_format_comment(redir->name, " (reflection)");
467                                         print_snat_dnat(FW3_FLAG_SNAT, &ref_addr, NULL);
468                                 }
469                                 else if (table == FW3_TABLE_FILTER)
470                                 {
471                                         fw3_pr("-A zone_%s_forward", redir->dest.name);
472                                         fw3_format_protocol(proto, family);
473                                         fw3_format_src_dest(int_addr, &redir->ip_redir);
474                                         fw3_format_sport_dport(NULL, &redir->port_redir);
475                                         fw3_format_time(&redir->time);
476                                         fw3_format_comment(redir->name, " (reflection)");
477                                         fw3_pr(" -j zone_%s_dest_ACCEPT\n", redir->dest.name);
478                                 }
479                         }
480
481                         fw3_ubus_address_free(int_addrs);
482                 }
483
484                 fw3_ubus_address_free(ext_addrs);
485         }
486 }
487
488 void
489 fw3_print_redirects(struct fw3_state *state, enum fw3_family family,
490                     enum fw3_table table)
491 {
492         int num = 0;
493         struct fw3_redirect *redir;
494
495         if (family == FW3_FAMILY_V6)
496                 return;
497
498         if (table != FW3_TABLE_FILTER && table != FW3_TABLE_NAT)
499                 return;
500
501         list_for_each_entry(redir, &state->redirects, list)
502                 print_redirect(state, family, table, redir, num++);
503 }