2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
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.
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.
19 #include "redirects.h"
22 const struct fw3_option fw3_redirect_opts[] = {
23 FW3_OPT("enabled", bool, redirect, enabled),
25 FW3_OPT("name", string, redirect, name),
26 FW3_OPT("family", family, redirect, family),
28 FW3_OPT("src", device, redirect, src),
29 FW3_OPT("dest", device, redirect, dest),
31 FW3_OPT("ipset", device, redirect, ipset),
33 FW3_LIST("proto", protocol, redirect, proto),
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),
39 FW3_OPT("src_dip", address, redirect, ip_dest),
40 FW3_OPT("src_dport", port, redirect, port_dest),
42 FW3_OPT("dest_ip", address, redirect, ip_redir),
43 FW3_OPT("dest_port", port, redirect, port_redir),
45 FW3_OPT("extra", string, redirect, extra),
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),
55 FW3_OPT("reflection", bool, redirect, reflection),
56 FW3_OPT("reflection_src", reflection_source,
57 redirect, reflection_src),
59 FW3_OPT("target", target, redirect, target),
66 check_families(struct uci_element *e, struct fw3_redirect *r)
68 if (r->family == FW3_FAMILY_ANY)
71 if (r->_src && r->_src->family && r->_src->family != r->family)
73 warn_elem(e, "refers to source zone with different family");
77 if (r->_dest && r->_dest->family && r->_dest->family != r->family)
79 warn_elem(e, "refers to destination zone with different family");
83 if (r->_ipset && r->_ipset->family && r->_ipset->family != r->family)
85 warn_elem(e, "refers to ipset with different family");
89 if (r->ip_src.family && r->ip_src.family != r->family)
91 warn_elem(e, "uses source ip with different family");
95 if (r->ip_dest.family && r->ip_dest.family != r->family)
97 warn_elem(e, "uses destination ip with different family");
101 if (r->ip_redir.family && r->ip_redir.family != r->family)
103 warn_elem(e, "uses redirect ip with different family");
111 fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
113 struct uci_section *s;
114 struct uci_element *e;
115 struct fw3_redirect *redir;
119 INIT_LIST_HEAD(&state->redirects);
121 uci_foreach_element(&p->sections, e)
123 s = uci_to_section(e);
125 if (strcmp(s->type, "redirect"))
128 redir = malloc(sizeof(*redir));
133 memset(redir, 0, sizeof(*redir));
135 INIT_LIST_HEAD(&redir->proto);
136 INIT_LIST_HEAD(&redir->mac_src);
138 redir->enabled = true;
139 redir->reflection = true;
141 fw3_parse_options(redir, fw3_redirect_opts, s);
145 fw3_free_redirect(redir);
149 if (redir->src.invert)
151 warn_elem(e, "must not have an inverted source");
152 fw3_free_redirect(redir);
155 else if (redir->src.set && !redir->src.any &&
156 !(redir->_src = fw3_lookup_zone(state, redir->src.name, false)))
158 warn_elem(e, "refers to not existing zone '%s'", redir->src.name);
159 fw3_free_redirect(redir);
162 else if (redir->dest.set && !redir->dest.any &&
163 !(redir->_dest = fw3_lookup_zone(state, redir->dest.name, false)))
165 warn_elem(e, "refers to not existing zone '%s'", redir->dest.name);
166 fw3_free_redirect(redir);
169 else if (redir->ipset.set && state->disable_ipsets)
171 warn_elem(e, "skipped due to disabled ipset support");
172 fw3_free_redirect(redir);
175 else if (redir->ipset.set && !redir->ipset.any &&
176 !(redir->_ipset = fw3_lookup_ipset(state, redir->ipset.name, false)))
178 warn_elem(e, "refers to unknown ipset '%s'", redir->ipset.name);
179 fw3_free_redirect(redir);
183 if (!check_families(e, redir))
185 fw3_free_redirect(redir);
189 if (redir->target == FW3_FLAG_UNSPEC)
191 warn_elem(e, "has no target specified, defaulting to DNAT");
192 redir->target = FW3_FLAG_DNAT;
194 else if (redir->target < FW3_FLAG_DNAT)
196 warn_elem(e, "has invalid target specified, defaulting to DNAT");
197 redir->target = FW3_FLAG_DNAT;
200 if (redir->target == FW3_FLAG_DNAT)
203 warn_elem(e, "must not have source '*' for DNAT target");
204 else if (!redir->_src)
205 warn_elem(e, "has no source specified");
208 set(redir->_src->flags, FW3_FAMILY_V4, redir->target);
209 redir->_src->conntrack = true;
213 if (redir->reflection && redir->_dest && redir->_src->masq)
215 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_ACCEPT);
216 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_DNAT);
217 set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
223 warn_elem(e, "must not have destination '*' for SNAT target");
224 else if (!redir->_dest)
225 warn_elem(e, "has no destination specified");
226 else if (!redir->ip_dest.set)
227 warn_elem(e, "has no src_dip option specified");
230 set(redir->_dest->flags, FW3_FAMILY_V4, redir->target);
231 redir->_dest->conntrack = true;
238 fw3_free_redirect(redir);
242 if (!redir->port_redir.set)
243 redir->port_redir = redir->port_dest;
245 list_add_tail(&redir->list, &state->redirects);
250 print_chain_nat(struct fw3_redirect *redir)
252 if (redir->target == FW3_FLAG_DNAT)
253 fw3_pr("-A zone_%s_prerouting", redir->src.name);
255 fw3_pr("-A zone_%s_postrouting", redir->dest.name);
259 print_snat_dnat(enum fw3_flag target,
260 struct fw3_address *addr, struct fw3_port *port)
263 char s[sizeof("255.255.255.255 ")];
265 if (target == FW3_FLAG_DNAT)
266 t = "DNAT --to-destination";
268 t = "SNAT --to-source";
270 inet_ntop(AF_INET, &addr->address.v4, s, sizeof(s));
272 fw3_pr(" -j %s %s", t, s);
274 if (port && port->set)
276 if (port->port_min == port->port_max)
277 fw3_pr(":%u", port->port_min);
279 fw3_pr(":%u-%u", port->port_min, port->port_max);
286 print_target_nat(struct fw3_redirect *redir)
288 if (redir->target == FW3_FLAG_DNAT)
289 print_snat_dnat(redir->target, &redir->ip_redir, &redir->port_redir);
291 print_snat_dnat(redir->target, &redir->ip_dest, &redir->port_dest);
295 print_chain_filter(struct fw3_redirect *redir)
297 if (redir->target == FW3_FLAG_DNAT)
299 /* XXX: check for local ip */
300 if (!redir->ip_redir.set)
301 fw3_pr("-A zone_%s_input", redir->src.name);
303 fw3_pr("-A zone_%s_forward", redir->src.name);
307 if (redir->src.set && !redir->src.any)
308 fw3_pr("-A zone_%s_forward", redir->src.name);
310 fw3_pr("-A delegate_forward");
315 print_target_filter(struct fw3_redirect *redir)
317 /* XXX: check for local ip */
318 if (redir->target == FW3_FLAG_DNAT && !redir->ip_redir.set)
319 fw3_pr(" -m conntrack --ctstate DNAT -j ACCEPT\n");
321 fw3_pr(" -j ACCEPT\n");
325 print_redirect(enum fw3_table table, enum fw3_family family,
326 struct fw3_redirect *redir, int num)
328 struct list_head *ext_addrs, *int_addrs;
329 struct fw3_address *ext_addr, *int_addr, ref_addr;
330 struct fw3_device *ext_net, *int_net;
331 struct fw3_protocol *proto;
335 info(" * Redirect '%s'", redir->name);
337 info(" * Redirect #%u", num);
339 if (!fw3_is_family(redir->_src, family) ||
340 !fw3_is_family(redir->_dest, family))
342 info(" ! Skipping due to different family of zone");
346 if (!fw3_is_family(&redir->ip_src, family) ||
347 !fw3_is_family(&redir->ip_dest, family) ||
348 !fw3_is_family(&redir->ip_redir, family))
350 info(" ! Skipping due to different family of ip address");
356 if (!fw3_is_family(redir->_ipset, family))
358 info(" ! Skipping due to different family in ipset");
362 set(redir->_ipset->flags, family, family);
365 fw3_foreach(proto, &redir->proto)
366 fw3_foreach(mac, &redir->mac_src)
368 if (table == FW3_TABLE_NAT)
370 print_chain_nat(redir);
371 fw3_format_ipset(redir->_ipset, redir->ipset.invert);
372 fw3_format_protocol(proto, family);
374 if (redir->target == FW3_FLAG_DNAT)
376 fw3_format_src_dest(&redir->ip_src, &redir->ip_dest);
377 fw3_format_sport_dport(&redir->port_src, &redir->port_dest);
381 fw3_format_src_dest(&redir->ip_src, &redir->ip_redir);
382 fw3_format_sport_dport(&redir->port_src, &redir->port_redir);
386 fw3_format_time(&redir->time);
387 fw3_format_extra(redir->extra);
388 fw3_format_comment(redir->name);
389 print_target_nat(redir);
391 else if (table == FW3_TABLE_FILTER)
393 print_chain_filter(redir);
394 fw3_format_ipset(redir->_ipset, redir->ipset.invert);
395 fw3_format_protocol(proto, family);
396 fw3_format_src_dest(&redir->ip_src, &redir->ip_redir);
397 fw3_format_sport_dport(&redir->port_src, &redir->port_redir);
399 fw3_format_time(&redir->time);
400 fw3_format_extra(redir->extra);
401 fw3_format_comment(redir->name);
402 print_target_filter(redir);
406 /* reflection rules */
407 if (redir->target != FW3_FLAG_DNAT || !redir->reflection)
410 if (!redir->_dest || !redir->_src->masq)
413 list_for_each_entry(ext_net, &redir->_src->networks, list)
415 ext_addrs = fw3_ubus_address(ext_net->name);
417 if (!ext_addrs || list_empty(ext_addrs))
420 list_for_each_entry(int_net, &redir->_dest->networks, list)
422 int_addrs = fw3_ubus_address(int_net->name);
424 if (!int_addrs || list_empty(int_addrs))
427 fw3_foreach(ext_addr, ext_addrs)
428 fw3_foreach(int_addr, int_addrs)
429 fw3_foreach(proto, &redir->proto)
431 if (!fw3_is_family(int_addr, family) ||
432 !fw3_is_family(ext_addr, family))
435 if (!proto || (proto->protocol != 6 && proto->protocol != 17))
438 if (redir->reflection_src == FW3_REFLECTION_INTERNAL)
439 ref_addr = *int_addr;
441 ref_addr = *ext_addr;
446 if (table == FW3_TABLE_NAT)
448 fw3_pr("-A zone_%s_prerouting", redir->dest.name);
449 fw3_format_protocol(proto, family);
450 fw3_format_src_dest(int_addr, ext_addr);
451 fw3_format_sport_dport(NULL, &redir->port_dest);
452 fw3_format_time(&redir->time);
453 fw3_format_comment(redir->name, " (reflection)");
454 print_snat_dnat(FW3_FLAG_DNAT,
455 &redir->ip_redir, &redir->port_redir);
457 fw3_pr("-A zone_%s_postrouting", redir->dest.name);
458 fw3_format_protocol(proto, family);
459 fw3_format_src_dest(int_addr, &redir->ip_redir);
460 fw3_format_sport_dport(NULL, &redir->port_redir);
461 fw3_format_time(&redir->time);
462 fw3_format_comment(redir->name, " (reflection)");
463 print_snat_dnat(FW3_FLAG_SNAT, &ref_addr, NULL);
465 else if (table == FW3_TABLE_FILTER)
467 fw3_pr("-A zone_%s_forward", redir->dest.name);
468 fw3_format_protocol(proto, family);
469 fw3_format_src_dest(int_addr, &redir->ip_redir);
470 fw3_format_sport_dport(NULL, &redir->port_redir);
471 fw3_format_time(&redir->time);
472 fw3_format_comment(redir->name, " (reflection)");
473 fw3_pr(" -j zone_%s_dest_ACCEPT\n", redir->dest.name);
477 fw3_ubus_address_free(int_addrs);
480 fw3_ubus_address_free(ext_addrs);
485 fw3_print_redirects(enum fw3_table table, enum fw3_family family,
486 struct fw3_state *state)
489 struct fw3_redirect *redir;
491 if (family == FW3_FAMILY_V6)
494 if (table != FW3_TABLE_FILTER && table != FW3_TABLE_NAT)
497 list_for_each_entry(redir, &state->redirects, list)
498 print_redirect(table, family, redir, num++);