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