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", setmatch, rule, ipset),
33 FW3_LIST("proto", protocol, rule, proto),
35 FW3_LIST("src_ip", network, 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", network, 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("mark", mark, rule, mark),
57 FW3_OPT("set_mark", mark, rule, set_mark),
58 FW3_OPT("set_xmark", mark, rule, set_xmark),
60 FW3_OPT("target", target, rule, target),
67 need_src_action_chain(struct fw3_rule *r)
69 return (r->_src && r->_src->log && (r->target > FW3_FLAG_ACCEPT));
73 fw3_load_rules(struct fw3_state *state, struct uci_package *p)
75 struct uci_section *s;
76 struct uci_element *e;
77 struct fw3_rule *rule;
79 INIT_LIST_HEAD(&state->rules);
81 uci_foreach_element(&p->sections, e)
83 s = uci_to_section(e);
85 if (strcmp(s->type, "rule"))
88 rule = malloc(sizeof(*rule));
93 memset(rule, 0, sizeof(*rule));
95 INIT_LIST_HEAD(&rule->proto);
97 INIT_LIST_HEAD(&rule->ip_src);
98 INIT_LIST_HEAD(&rule->mac_src);
99 INIT_LIST_HEAD(&rule->port_src);
101 INIT_LIST_HEAD(&rule->ip_dest);
102 INIT_LIST_HEAD(&rule->port_dest);
104 INIT_LIST_HEAD(&rule->icmp_type);
106 rule->enabled = true;
108 if (!fw3_parse_options(rule, fw3_rule_opts, s))
110 warn_elem(e, "skipped due to invalid options");
121 if (rule->src.invert || rule->dest.invert)
123 warn_elem(e, "must not have inverted 'src' or 'dest' options");
127 else if (rule->src.set && !rule->src.any &&
128 !(rule->_src = fw3_lookup_zone(state, rule->src.name)))
130 warn_elem(e, "refers to not existing zone '%s'", rule->src.name);
134 else if (rule->dest.set && !rule->dest.any &&
135 !(rule->_dest = fw3_lookup_zone(state, rule->dest.name)))
137 warn_elem(e, "refers to not existing zone '%s'", rule->dest.name);
141 else if (rule->ipset.set && state->disable_ipsets)
143 warn_elem(e, "skipped due to disabled ipset support");
147 else if (rule->ipset.set &&
148 !(rule->ipset.ptr = fw3_lookup_ipset(state, rule->ipset.name)))
150 warn_elem(e, "refers to unknown ipset '%s'", rule->ipset.name);
155 if (!rule->_src && rule->target == FW3_FLAG_NOTRACK)
157 warn_elem(e, "is set to target NOTRACK but has no source assigned");
162 if (!rule->set_mark.set && !rule->set_xmark.set &&
163 rule->target == FW3_FLAG_MARK)
165 warn_elem(e, "is set to target MARK but specifies neither "
166 "'set_mark' nor 'set_xmark' option");
171 if (rule->_dest && rule->target == FW3_FLAG_MARK)
173 warn_elem(e, "must not specify 'dest' for MARK target");
178 if (rule->set_mark.invert || rule->set_xmark.invert)
180 warn_elem(e, "must not have inverted 'set_mark' or 'set_xmark'");
185 if (!rule->_src && !rule->_dest && !rule->src.any && !rule->dest.any)
187 warn_elem(e, "has neither a source nor a destination zone assigned "
188 "- assuming an output rule");
191 if (list_empty(&rule->proto))
193 warn_elem(e, "does not specify a protocol, assuming TCP+UDP");
194 fw3_parse_protocol(&rule->proto, "tcpudp", true);
197 if (rule->target == FW3_FLAG_UNSPEC)
199 warn_elem(e, "has no target specified, defaulting to REJECT");
200 rule->target = FW3_FLAG_REJECT;
202 else if (rule->target > FW3_FLAG_MARK)
204 warn_elem(e, "has invalid target specified, defaulting to REJECT");
205 rule->target = FW3_FLAG_REJECT;
208 /* NB: rule family... */
211 setbit(rule->_dest->flags[0], rule->target);
212 setbit(rule->_dest->flags[1], rule->target);
214 else if (need_src_action_chain(rule))
216 setbit(rule->_src->flags[0], fw3_to_src_target(rule->target));
217 setbit(rule->_src->flags[1], fw3_to_src_target(rule->target));
220 list_add_tail(&rule->list, &state->rules);
227 append_chain(struct fw3_ipt_rule *r, struct fw3_rule *rule)
231 snprintf(chain, sizeof(chain), "delegate_output");
233 if (rule->target == FW3_FLAG_NOTRACK)
235 snprintf(chain, sizeof(chain), "zone_%s_notrack", rule->src.name);
237 else if (rule->target == FW3_FLAG_MARK)
239 snprintf(chain, sizeof(chain), "fwmark");
248 snprintf(chain, sizeof(chain), "zone_%s_forward",
251 snprintf(chain, sizeof(chain), "zone_%s_input",
257 snprintf(chain, sizeof(chain), "delegate_forward");
259 snprintf(chain, sizeof(chain), "delegate_input");
263 if (rule->dest.set && !rule->src.set)
266 snprintf(chain, sizeof(chain), "delegate_output");
268 snprintf(chain, sizeof(chain), "zone_%s_output",
273 fw3_ipt_rule_append(r, chain);
276 static void set_target(struct fw3_ipt_rule *r, struct fw3_rule *rule)
279 struct fw3_mark *mark;
280 char buf[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
285 name = rule->set_mark.set ? "--set-mark" : "--set-xmark";
286 mark = rule->set_mark.set ? &rule->set_mark : &rule->set_xmark;
287 sprintf(buf, "0x%x/0x%x", mark->mark, mark->mask);
289 fw3_ipt_rule_target(r, "MARK");
290 fw3_ipt_rule_addarg(r, false, name, buf);
293 case FW3_FLAG_NOTRACK:
294 fw3_ipt_rule_target(r, "CT");
295 fw3_ipt_rule_addarg(r, false, "--notrack", NULL);
298 case FW3_FLAG_ACCEPT:
300 name = fw3_flag_names[rule->target];
304 name = fw3_flag_names[FW3_FLAG_REJECT];
308 if (rule->dest.set && !rule->dest.any)
309 fw3_ipt_rule_target(r, "zone_%s_dest_%s", rule->dest.name, name);
310 else if (need_src_action_chain(rule))
311 fw3_ipt_rule_target(r, "zone_%s_src_%s", rule->src.name, name);
312 else if (strcmp(name, "REJECT"))
313 fw3_ipt_rule_target(r, name);
315 fw3_ipt_rule_target(r, "reject");
319 set_comment(struct fw3_ipt_rule *r, const char *name, int num)
322 fw3_ipt_rule_comment(r, name);
324 fw3_ipt_rule_comment(r, "@rule[%u]", num);
328 print_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
329 struct fw3_rule *rule, int num, struct fw3_protocol *proto,
330 struct fw3_address *sip, struct fw3_address *dip,
331 struct fw3_port *sport, struct fw3_port *dport,
332 struct fw3_mac *mac, struct fw3_icmptype *icmptype)
334 struct fw3_ipt_rule *r;
336 if (!fw3_is_family(sip, handle->family) ||
337 !fw3_is_family(dip, handle->family))
339 if ((sip && !sip->resolved) || (dip && !dip->resolved))
340 info(" ! Skipping due to different family of ip address");
345 if (proto->protocol == 58 && handle->family == FW3_FAMILY_V4)
347 info(" ! Skipping due to different family of protocol");
351 r = fw3_ipt_rule_create(handle, proto, NULL, NULL, sip, dip);
352 fw3_ipt_rule_sport_dport(r, sport, dport);
353 fw3_ipt_rule_icmptype(r, icmptype);
354 fw3_ipt_rule_mac(r, mac);
355 fw3_ipt_rule_ipset(r, &rule->ipset);
356 fw3_ipt_rule_limit(r, &rule->limit);
357 fw3_ipt_rule_time(r, &rule->time);
358 fw3_ipt_rule_mark(r, &rule->mark);
360 fw3_ipt_rule_extra(r, rule->extra);
361 set_comment(r, rule->name, num);
362 append_chain(r, rule);
366 expand_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
367 struct fw3_rule *rule, int num)
369 struct fw3_protocol *proto;
370 struct fw3_address *sip;
371 struct fw3_address *dip;
372 struct fw3_port *sport;
373 struct fw3_port *dport;
375 struct fw3_icmptype *icmptype;
377 struct list_head *sports = NULL;
378 struct list_head *dports = NULL;
379 struct list_head *icmptypes = NULL;
381 struct list_head empty;
382 INIT_LIST_HEAD(&empty);
384 if (!fw3_is_family(rule, handle->family))
387 if ((rule->target == FW3_FLAG_NOTRACK && handle->table != FW3_TABLE_RAW) ||
388 (rule->target == FW3_FLAG_MARK && handle->table != FW3_TABLE_MANGLE) ||
389 (rule->target < FW3_FLAG_NOTRACK && handle->table != FW3_TABLE_FILTER))
393 info(" * Rule '%s'", rule->name);
395 info(" * Rule #%u", num);
397 if (!fw3_is_family(rule->_src, handle->family) ||
398 !fw3_is_family(rule->_dest, handle->family))
400 info(" ! Skipping due to different family of zone");
406 if (!fw3_is_family(rule->ipset.ptr, handle->family))
408 info(" ! Skipping due to different family in ipset");
412 if (!fw3_check_ipset(rule->ipset.ptr))
414 info(" ! Skipping due to missing ipset '%s'",
415 rule->ipset.ptr->external
416 ? rule->ipset.ptr->external : rule->ipset.ptr->name);
420 set(rule->ipset.ptr->flags, handle->family, handle->family);
423 list_for_each_entry(proto, &rule->proto, list)
425 /* icmp / ipv6-icmp */
426 if (proto->protocol == 1 || proto->protocol == 58)
430 icmptypes = &rule->icmp_type;
434 sports = &rule->port_src;
435 dports = &rule->port_dest;
439 fw3_foreach(sip, &rule->ip_src)
440 fw3_foreach(dip, &rule->ip_dest)
441 fw3_foreach(sport, sports)
442 fw3_foreach(dport, dports)
443 fw3_foreach(mac, &rule->mac_src)
444 fw3_foreach(icmptype, icmptypes)
445 print_rule(handle, state, rule, num, proto, sip, dip,
446 sport, dport, mac, icmptype);
451 fw3_print_rules(struct fw3_ipt_handle *handle, struct fw3_state *state)
454 struct fw3_rule *rule;
456 list_for_each_entry(rule, &state->rules, list)
457 expand_rule(handle, state, rule, num++);