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