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.
22 const struct fw3_option fw3_rule_opts[] = {
23 FW3_OPT("enabled", bool, rule, enabled),
25 FW3_OPT("name", string, rule, name),
26 FW3_OPT("family", family, rule, family),
28 FW3_OPT("src", device, rule, src),
29 FW3_OPT("dest", device, rule, dest),
31 FW3_OPT("ipset", device, rule, ipset),
33 FW3_LIST("proto", protocol, rule, proto),
35 FW3_LIST("src_ip", address, rule, ip_src),
36 FW3_LIST("src_mac", mac, rule, mac_src),
37 FW3_LIST("src_port", port, rule, port_src),
39 FW3_LIST("dest_ip", address, rule, ip_dest),
40 FW3_LIST("dest_port", port, rule, port_dest),
42 FW3_LIST("icmp_type", icmptype, rule, icmp_type),
43 FW3_OPT("extra", string, rule, extra),
45 FW3_OPT("limit", limit, rule, limit),
46 FW3_OPT("limit_burst", int, rule, limit.burst),
48 FW3_OPT("utc_time", bool, rule, time.utc),
49 FW3_OPT("start_date", date, rule, time.datestart),
50 FW3_OPT("stop_date", date, rule, time.datestop),
51 FW3_OPT("start_time", time, rule, time.timestart),
52 FW3_OPT("stop_time", time, rule, time.timestop),
53 FW3_OPT("weekdays", weekdays, rule, time.weekdays),
54 FW3_OPT("monthdays", monthdays, rule, time.monthdays),
56 FW3_OPT("target", target, rule, target),
63 fw3_load_rules(struct fw3_state *state, struct uci_package *p)
65 struct uci_section *s;
66 struct uci_element *e;
67 struct fw3_rule *rule;
69 INIT_LIST_HEAD(&state->rules);
71 uci_foreach_element(&p->sections, e)
73 s = uci_to_section(e);
75 if (strcmp(s->type, "rule"))
78 rule = malloc(sizeof(*rule));
83 memset(rule, 0, sizeof(*rule));
85 INIT_LIST_HEAD(&rule->proto);
87 INIT_LIST_HEAD(&rule->ip_src);
88 INIT_LIST_HEAD(&rule->mac_src);
89 INIT_LIST_HEAD(&rule->port_src);
91 INIT_LIST_HEAD(&rule->ip_dest);
92 INIT_LIST_HEAD(&rule->port_dest);
94 INIT_LIST_HEAD(&rule->icmp_type);
98 fw3_parse_options(rule, fw3_rule_opts, s);
106 if (rule->src.invert || rule->dest.invert)
108 warn_elem(e, "must not have inverted 'src' or 'dest' options");
112 else if (rule->src.set && !rule->src.any &&
113 !(rule->_src = fw3_lookup_zone(state, rule->src.name, false)))
115 warn_elem(e, "refers to not existing zone '%s'", rule->src.name);
119 else if (rule->dest.set && !rule->dest.any &&
120 !(rule->_dest = fw3_lookup_zone(state, rule->dest.name, false)))
122 warn_elem(e, "refers to not existing zone '%s'", rule->dest.name);
126 else if (rule->ipset.set && state->disable_ipsets)
128 warn_elem(e, "skipped due to disabled ipset support");
132 else if (rule->ipset.set && !rule->ipset.any &&
133 !(rule->_ipset = fw3_lookup_ipset(state, rule->ipset.name, false)))
135 warn_elem(e, "refers to unknown ipset '%s'", rule->ipset.name);
140 if (!rule->_src && rule->target == FW3_FLAG_NOTRACK)
142 warn_elem(e, "is set to target NOTRACK but has no source assigned");
147 if (!rule->_src && !rule->_dest && !rule->src.any && !rule->dest.any)
149 warn_elem(e, "has neither a source nor a destination zone assigned "
150 "- assuming an output rule");
153 if (rule->target == FW3_FLAG_UNSPEC)
155 warn_elem(e, "has no target specified, defaulting to REJECT");
156 rule->target = FW3_FLAG_REJECT;
158 else if (rule->target > FW3_FLAG_NOTRACK)
160 warn_elem(e, "has invalid target specified, defaulting to REJECT");
161 rule->target = FW3_FLAG_REJECT;
164 /* NB: rule family... */
167 setbit(rule->_dest->flags[0], rule->target);
168 setbit(rule->_dest->flags[1], rule->target);
171 list_add_tail(&rule->list, &state->rules);
178 print_chain(struct fw3_rule *rule)
182 sprintf(chain, "delegate_output");
184 if (rule->target == FW3_FLAG_NOTRACK)
186 sprintf(chain, "zone_%s_notrack", rule->src.name);
195 sprintf(chain, "zone_%s_forward", rule->src.name);
197 sprintf(chain, "zone_%s_input", rule->src.name);
202 sprintf(chain, "delegate_forward");
204 sprintf(chain, "delegate_input");
208 if (rule->dest.set && !rule->src.set)
209 sprintf(chain, "zone_%s_output", rule->dest.name);
212 fw3_pr("-A %s", chain);
215 static void print_target(struct fw3_rule *rule)
221 case FW3_FLAG_ACCEPT:
223 case FW3_FLAG_NOTRACK:
224 target = fw3_flag_names[rule->target];
228 target = fw3_flag_names[FW3_FLAG_REJECT];
232 if (rule->dest.set && !rule->dest.any)
233 fw3_pr(" -j zone_%s_dest_%s\n", rule->dest.name, target);
234 else if (rule->target == FW3_FLAG_REJECT)
235 fw3_pr(" -j reject\n");
237 fw3_pr(" -j %s\n", target);
241 print_rule(struct fw3_state *state, enum fw3_family family,
242 enum fw3_table table, struct fw3_rule *rule,
243 struct fw3_protocol *proto,
244 struct fw3_address *sip, struct fw3_address *dip,
245 struct fw3_port *sport, struct fw3_port *dport,
246 struct fw3_mac *mac, struct fw3_icmptype *icmptype)
248 if (!fw3_is_family(sip, family) || !fw3_is_family(dip, family))
250 info(" ! Skipping due to different family of ip address");
254 if (proto->protocol == 58 && family == FW3_FAMILY_V4)
256 info(" ! Skipping due to different family of protocol");
261 fw3_format_ipset(rule->_ipset, rule->ipset.invert);
262 fw3_format_protocol(proto, family);
263 fw3_format_src_dest(sip, dip);
264 fw3_format_sport_dport(sport, dport);
265 fw3_format_icmptype(icmptype, family);
267 fw3_format_limit(&rule->limit);
268 fw3_format_time(&rule->time);
269 fw3_format_extra(rule->extra);
270 fw3_format_comment(rule->name);
275 expand_rule(struct fw3_state *state, enum fw3_family family,
276 enum fw3_table table, struct fw3_rule *rule, int num)
278 struct fw3_protocol *proto;
279 struct fw3_address *sip;
280 struct fw3_address *dip;
281 struct fw3_port *sport;
282 struct fw3_port *dport;
284 struct fw3_icmptype *icmptype;
286 struct list_head *sports = NULL;
287 struct list_head *dports = NULL;
288 struct list_head *icmptypes = NULL;
290 struct list_head empty;
291 INIT_LIST_HEAD(&empty);
293 if (!fw3_is_family(rule, family))
296 if ((table == FW3_TABLE_RAW && rule->target != FW3_FLAG_NOTRACK) ||
297 (table != FW3_TABLE_FILTER))
301 info(" * Rule '%s'", rule->name);
303 info(" * Rule #%u", num);
305 if (!fw3_is_family(rule->_src, family) ||
306 !fw3_is_family(rule->_dest, family))
308 info(" ! Skipping due to different family of zone");
314 if (!fw3_is_family(rule->_ipset, family))
316 info(" ! Skipping due to different family in ipset");
320 set(rule->_ipset->flags, family, family);
323 list_for_each_entry(proto, &rule->proto, list)
325 /* icmp / ipv6-icmp */
326 if (proto->protocol == 1 || proto->protocol == 58)
330 icmptypes = &rule->icmp_type;
334 sports = &rule->port_src;
335 dports = &rule->port_dest;
339 fw3_foreach(sip, &rule->ip_src)
340 fw3_foreach(dip, &rule->ip_dest)
341 fw3_foreach(sport, sports)
342 fw3_foreach(dport, dports)
343 fw3_foreach(mac, &rule->mac_src)
344 fw3_foreach(icmptype, icmptypes)
345 print_rule(state, family, table, rule, proto, sip, dip,
346 sport, dport, mac, icmptype);
351 fw3_print_rules(struct fw3_state *state, enum fw3_family family,
352 enum fw3_table table)
355 struct fw3_rule *rule;
357 list_for_each_entry(rule, &state->rules, list)
358 expand_rule(state, family, table, rule, num++);