Allow devices for src_ip, src_dip and dest_ip options
[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", 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("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 append_chain_nat(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
263 {
264 if (redir->target == FW3_FLAG_DNAT)
265 fw3_ipt_rule_append(r, "zone_%s_prerouting", redir->src.name);
266 else
267 fw3_ipt_rule_append(r, "zone_%s_postrouting", redir->dest.name);
268 }
269
270 static void
271 set_snat_dnat(struct fw3_ipt_rule *r, enum fw3_flag target,
272 struct fw3_address *addr, struct fw3_port *port)
273 {
274 char buf[sizeof("255.255.255.255:65535-65535\0")];
275
276 buf[0] = '\0';
277
278 if (addr && addr->set)
279 {
280 inet_ntop(AF_INET, &addr->address.v4, buf, sizeof(buf));
281 }
282
283 if (port && port->set)
284 {
285 if (port->port_min == port->port_max)
286 sprintf(buf + strlen(buf), ":%u", port->port_min);
287 else
288 sprintf(buf + strlen(buf), ":%u-%u",
289 port->port_min, port->port_max);
290 }
291
292 if (target == FW3_FLAG_DNAT)
293 {
294 fw3_ipt_rule_target(r, "DNAT");
295 fw3_ipt_rule_addarg(r, false, "--to-destination", buf);
296 }
297 else
298 {
299 fw3_ipt_rule_target(r, "SNAT");
300 fw3_ipt_rule_addarg(r, false, "--to-source", buf);
301 }
302 }
303
304 static void
305 set_target_nat(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
306 {
307 if (redir->target == FW3_FLAG_DNAT)
308 set_snat_dnat(r, redir->target, &redir->ip_redir, &redir->port_redir);
309 else
310 set_snat_dnat(r, redir->target, &redir->ip_dest, &redir->port_dest);
311 }
312
313 static void
314 append_chain_filter(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
315 {
316 if (redir->target == FW3_FLAG_DNAT)
317 {
318 /* XXX: check for local ip */
319 if (!redir->ip_redir.set)
320 fw3_ipt_rule_append(r, "zone_%s_input", redir->src.name);
321 else
322 fw3_ipt_rule_append(r, "zone_%s_forward", redir->src.name);
323 }
324 else
325 {
326 if (redir->src.set && !redir->src.any)
327 fw3_ipt_rule_append(r, "zone_%s_forward", redir->src.name);
328 else
329 fw3_ipt_rule_append(r, "delegate_forward");
330 }
331 }
332
333 static void
334 set_target_filter(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
335 {
336 /* XXX: check for local ip */
337 if (redir->target == FW3_FLAG_DNAT && !redir->ip_redir.set)
338 fw3_ipt_rule_extra(r, "-m conntrack --ctstate DNAT");
339
340 fw3_ipt_rule_target(r, "ACCEPT");
341 }
342
343 static void
344 set_comment(struct fw3_ipt_rule *r, const char *name, int num, bool ref)
345 {
346 if (name)
347 {
348 if (ref)
349 fw3_ipt_rule_comment(r, "%s (reflection)", name);
350 else
351 fw3_ipt_rule_comment(r, name);
352 }
353 else
354 {
355 if (ref)
356 fw3_ipt_rule_comment(r, "@redirect[%u] (reflection)", num);
357 else
358 fw3_ipt_rule_comment(r, "@redirect[%u]", num);
359 }
360 }
361
362 static void
363 print_redirect(struct fw3_ipt_handle *h, struct fw3_state *state,
364 struct fw3_redirect *redir, int num,
365 struct fw3_protocol *proto, struct fw3_mac *mac)
366 {
367 struct fw3_ipt_rule *r;
368 struct fw3_address *src, *dst;
369 struct fw3_port *spt, *dpt;
370
371 switch (h->table)
372 {
373 case FW3_TABLE_NAT:
374 src = &redir->ip_src;
375 dst = &redir->ip_dest;
376 spt = &redir->port_src;
377 dpt = &redir->port_dest;
378
379 if (redir->target == FW3_FLAG_SNAT)
380 {
381 dst = &redir->ip_redir;
382 dpt = &redir->port_redir;
383 }
384
385 r = fw3_ipt_rule_create(h, proto, NULL, NULL, src, dst);
386 fw3_ipt_rule_sport_dport(r, spt, dpt);
387 fw3_ipt_rule_mac(r, mac);
388 fw3_ipt_rule_ipset(r, redir->_ipset, redir->ipset.invert);
389 fw3_ipt_rule_time(r, &redir->time);
390 fw3_ipt_rule_mark(r, &redir->mark);
391 set_target_nat(r, redir);
392 fw3_ipt_rule_extra(r, redir->extra);
393 set_comment(r, redir->name, num, false);
394 append_chain_nat(r, redir);
395 break;
396
397 case FW3_TABLE_FILTER:
398 src = &redir->ip_src;
399 dst = &redir->ip_redir;
400 spt = &redir->port_src;
401 dpt = &redir->port_redir;
402
403 r = fw3_ipt_rule_create(h, proto, NULL, NULL, src, dst);
404 fw3_ipt_rule_sport_dport(r, spt, dpt);
405 fw3_ipt_rule_mac(r, mac);
406 fw3_ipt_rule_ipset(r, redir->_ipset, redir->ipset.invert);
407 fw3_ipt_rule_time(r, &redir->time);
408 fw3_ipt_rule_mark(r, &redir->mark);
409 set_target_filter(r, redir);
410 fw3_ipt_rule_extra(r, redir->extra);
411 set_comment(r, redir->name, num, false);
412 append_chain_filter(r, redir);
413 break;
414
415 default:
416 break;
417 }
418 }
419
420 static void
421 print_reflection(struct fw3_ipt_handle *h, struct fw3_state *state,
422 struct fw3_redirect *redir, int num,
423 struct fw3_protocol *proto, struct fw3_address *ra,
424 struct fw3_address *ia, struct fw3_address *ea)
425 {
426 struct fw3_ipt_rule *r;
427
428 switch (h->table)
429 {
430 case FW3_TABLE_NAT:
431 r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, ea);
432 fw3_ipt_rule_sport_dport(r, NULL, &redir->port_dest);
433 fw3_ipt_rule_time(r, &redir->time);
434 set_comment(r, redir->name, num, true);
435 set_snat_dnat(r, FW3_FLAG_DNAT, &redir->ip_redir, &redir->port_redir);
436 fw3_ipt_rule_append(r, "zone_%s_prerouting", redir->dest.name);
437
438 r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, &redir->ip_redir);
439 fw3_ipt_rule_sport_dport(r, NULL, &redir->port_redir);
440 fw3_ipt_rule_time(r, &redir->time);
441 set_comment(r, redir->name, num, true);
442 set_snat_dnat(r, FW3_FLAG_SNAT, ra, NULL);
443 fw3_ipt_rule_append(r, "zone_%s_postrouting", redir->dest.name);
444 break;
445
446 case FW3_TABLE_FILTER:
447 r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, &redir->ip_redir);
448 fw3_ipt_rule_sport_dport(r, NULL, &redir->port_redir);
449 fw3_ipt_rule_time(r, &redir->time);
450 set_comment(r, redir->name, num, true);
451 fw3_ipt_rule_target(r, "zone_%s_dest_ACCEPT", redir->dest.name);
452 fw3_ipt_rule_append(r, "zone_%s_forward", redir->dest.name);
453 break;
454
455 default:
456 break;
457 }
458 }
459
460 static void
461 expand_redirect(struct fw3_ipt_handle *handle, struct fw3_state *state,
462 struct fw3_redirect *redir, int num)
463 {
464 struct list_head *ext_addrs, *int_addrs;
465 struct fw3_address *ext_addr, *int_addr, ref_addr;
466 struct fw3_device *ext_net, *int_net;
467 struct fw3_protocol *proto;
468 struct fw3_mac *mac;
469
470 if (redir->name)
471 info(" * Redirect '%s'", redir->name);
472 else
473 info(" * Redirect #%u", num);
474
475 if (!fw3_is_family(redir->_src, handle->family) ||
476 !fw3_is_family(redir->_dest, handle->family))
477 {
478 info(" ! Skipping due to different family of zone");
479 return;
480 }
481
482 if (!fw3_is_family(&redir->ip_src, handle->family) ||
483 !fw3_is_family(&redir->ip_dest, handle->family) ||
484 !fw3_is_family(&redir->ip_redir, handle->family))
485 {
486 info(" ! Skipping due to different family of ip address");
487 return;
488 }
489
490 if (redir->_ipset)
491 {
492 if (!fw3_is_family(redir->_ipset, handle->family))
493 {
494 info(" ! Skipping due to different family in ipset");
495 return;
496 }
497
498 if (!fw3_check_ipset(redir->_ipset))
499 {
500 info(" ! Skipping due to missing ipset '%s'",
501 redir->_ipset->external ?
502 redir->_ipset->external : redir->_ipset->name);
503 return;
504 }
505
506 set(redir->_ipset->flags, handle->family, handle->family);
507 }
508
509 fw3_foreach(proto, &redir->proto)
510 fw3_foreach(mac, &redir->mac_src)
511 print_redirect(handle, state, redir, num, proto, mac);
512
513 /* reflection rules */
514 if (redir->target != FW3_FLAG_DNAT || !redir->reflection)
515 return;
516
517 if (!redir->_dest || !redir->_src->masq)
518 return;
519
520 list_for_each_entry(ext_net, &redir->_src->networks, list)
521 {
522 ext_addrs = fw3_ubus_address(ext_net->name);
523
524 if (!ext_addrs || list_empty(ext_addrs))
525 continue;
526
527 list_for_each_entry(int_net, &redir->_dest->networks, list)
528 {
529 int_addrs = fw3_ubus_address(int_net->name);
530
531 if (!int_addrs || list_empty(int_addrs))
532 continue;
533
534 fw3_foreach(ext_addr, ext_addrs)
535 fw3_foreach(int_addr, int_addrs)
536 fw3_foreach(proto, &redir->proto)
537 {
538 if (!fw3_is_family(int_addr, handle->family) ||
539 !fw3_is_family(ext_addr, handle->family))
540 continue;
541
542 if (!proto || (proto->protocol != 6 && proto->protocol != 17))
543 continue;
544
545 if (redir->reflection_src == FW3_REFLECTION_INTERNAL)
546 ref_addr = *int_addr;
547 else
548 ref_addr = *ext_addr;
549
550 ref_addr.mask = 32;
551 ext_addr->mask = 32;
552
553 print_reflection(handle, state, redir, num, proto,
554 &ref_addr, int_addr, ext_addr);
555 }
556
557 fw3_ubus_address_free(int_addrs);
558 }
559
560 fw3_ubus_address_free(ext_addrs);
561 }
562 }
563
564 void
565 fw3_print_redirects(struct fw3_ipt_handle *handle, struct fw3_state *state)
566 {
567 int num = 0;
568 struct fw3_redirect *redir;
569
570 if (handle->family == FW3_FAMILY_V6)
571 return;
572
573 if (handle->table != FW3_TABLE_FILTER && handle->table != FW3_TABLE_NAT)
574 return;
575
576 list_for_each_entry(redir, &state->redirects, list)
577 expand_redirect(handle, state, redir, num++);
578 }