2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
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("device", string, rule, device),
32 FW3_OPT("direction", direction, rule, direction_out),
34 FW3_OPT("ipset", setmatch, rule, ipset),
36 FW3_LIST("proto", protocol, rule, proto),
38 FW3_LIST("src_ip", network, rule, ip_src),
39 FW3_LIST("src_mac", mac, rule, mac_src),
40 FW3_LIST("src_port", port, rule, port_src),
42 FW3_LIST("dest_ip", network, rule, ip_dest),
43 FW3_LIST("dest_port", port, rule, port_dest),
45 FW3_LIST("icmp_type", icmptype, rule, icmp_type),
46 FW3_OPT("extra", string, rule, extra),
48 FW3_OPT("limit", limit, rule, limit),
49 FW3_OPT("limit_burst", int, rule, limit.burst),
51 FW3_OPT("utc_time", bool, rule, time.utc),
52 FW3_OPT("start_date", date, rule, time.datestart),
53 FW3_OPT("stop_date", date, rule, time.datestop),
54 FW3_OPT("start_time", time, rule, time.timestart),
55 FW3_OPT("stop_time", time, rule, time.timestop),
56 FW3_OPT("weekdays", weekdays, rule, time.weekdays),
57 FW3_OPT("monthdays", monthdays, rule, time.monthdays),
59 FW3_OPT("mark", mark, rule, mark),
60 FW3_OPT("set_mark", mark, rule, set_mark),
61 FW3_OPT("set_xmark", mark, rule, set_xmark),
63 FW3_OPT("target", target, rule, target),
70 need_src_action_chain(struct fw3_rule *r)
72 return (r->_src && r->_src->log && (r->target > FW3_FLAG_ACCEPT));
75 static struct fw3_rule*
76 alloc_rule(struct fw3_state *state)
78 struct fw3_rule *rule = calloc(1, sizeof(*rule));
81 INIT_LIST_HEAD(&rule->proto);
83 INIT_LIST_HEAD(&rule->ip_src);
84 INIT_LIST_HEAD(&rule->mac_src);
85 INIT_LIST_HEAD(&rule->port_src);
87 INIT_LIST_HEAD(&rule->ip_dest);
88 INIT_LIST_HEAD(&rule->port_dest);
90 INIT_LIST_HEAD(&rule->icmp_type);
92 list_add_tail(&rule->list, &state->rules);
100 check_rule(struct fw3_state *state, struct fw3_rule *r, struct uci_element *e)
105 if (r->src.invert || r->dest.invert)
107 warn_section("rule", r, e, "must not have inverted 'src' or 'dest' options");
110 else if (r->src.set && !r->src.any &&
111 !(r->_src = fw3_lookup_zone(state, r->src.name)))
113 warn_section("rule", r, e, "refers to not existing zone '%s'", r->src.name);
116 else if (r->dest.set && !r->dest.any &&
117 !(r->_dest = fw3_lookup_zone(state, r->dest.name)))
119 warn_section("rule", r, e, "refers to not existing zone '%s'", r->dest.name);
122 else if (r->ipset.set && state->disable_ipsets)
124 warn_section("rule", r, e, "skipped due to disabled ipset support");
127 else if (r->ipset.set &&
128 !(r->ipset.ptr = fw3_lookup_ipset(state, r->ipset.name)))
130 warn_section("rule", r, e, "refers to unknown ipset '%s'", r->ipset.name);
134 if (!r->_src && r->target == FW3_FLAG_NOTRACK)
136 warn_section("rule", r, e, "is set to target NOTRACK but has no source assigned");
140 if (!r->set_mark.set && !r->set_xmark.set &&
141 r->target == FW3_FLAG_MARK)
143 warn_section("rule", r, e, "is set to target MARK but specifies neither "
144 "'set_mark' nor 'set_xmark' option");
148 if (r->_dest && r->target == FW3_FLAG_MARK)
150 warn_section("rule", r, e, "must not specify 'dest' for MARK target");
154 if (r->set_mark.invert || r->set_xmark.invert)
156 warn_section("rule", r, e, "must not have inverted 'set_mark' or 'set_xmark'");
160 if (!r->_src && !r->_dest && !r->src.any && !r->dest.any)
162 warn_section("rule", r, e, "has neither a source nor a destination zone assigned "
163 "- assuming an output r");
166 if (list_empty(&r->proto))
168 warn_section("rule", r, e, "does not specify a protocol, assuming TCP+UDP");
169 fw3_parse_protocol(&r->proto, "tcpudp", true);
172 if (r->target == FW3_FLAG_UNSPEC)
174 warn_section("rule", r, e, "has no target specified, defaulting to REJECT");
175 r->target = FW3_FLAG_REJECT;
177 else if (r->target > FW3_FLAG_MARK)
179 warn_section("rule", r, e, "has invalid target specified, defaulting to REJECT");
180 r->target = FW3_FLAG_REJECT;
183 /* NB: r family... */
186 fw3_setbit(r->_dest->flags[0], r->target);
187 fw3_setbit(r->_dest->flags[1], r->target);
189 else if (need_src_action_chain(r))
191 fw3_setbit(r->_src->flags[0], fw3_to_src_target(r->target));
192 fw3_setbit(r->_src->flags[1], fw3_to_src_target(r->target));
199 fw3_load_rules(struct fw3_state *state, struct uci_package *p,
202 struct uci_section *s;
203 struct uci_element *e;
204 struct fw3_rule *rule;
205 struct blob_attr *entry, *opt;
208 INIT_LIST_HEAD(&state->rules);
210 blob_for_each_attr(entry, a, rem) {
211 const char *type = NULL;
212 const char *name = "ubus rule";
213 blobmsg_for_each_attr(opt, entry, orem)
214 if (!strcmp(blobmsg_name(opt), "type"))
215 type = blobmsg_get_string(opt);
216 else if (!strcmp(blobmsg_name(opt), "name"))
217 name = blobmsg_get_string(opt);
219 if (!type || strcmp(type, "rule"))
222 if (!(rule = alloc_rule(state)))
225 if (!fw3_parse_blob_options(rule, fw3_rule_opts, entry, name))
227 warn_section("rule", rule, NULL, "skipped due to invalid options");
232 if (!check_rule(state, rule, NULL))
236 uci_foreach_element(&p->sections, e)
238 s = uci_to_section(e);
240 if (strcmp(s->type, "rule"))
243 if (!(rule = alloc_rule(state)))
246 if (!fw3_parse_options(rule, fw3_rule_opts, s))
248 warn_elem(e, "skipped due to invalid options");
253 if (!check_rule(state, rule, e))
260 append_chain(struct fw3_ipt_rule *r, struct fw3_rule *rule)
264 snprintf(chain, sizeof(chain), "OUTPUT");
266 if (rule->target == FW3_FLAG_NOTRACK)
268 snprintf(chain, sizeof(chain), "zone_%s_notrack", rule->src.name);
270 else if (rule->target == FW3_FLAG_MARK && (rule->_src || rule->src.any))
272 snprintf(chain, sizeof(chain), "PREROUTING");
281 snprintf(chain, sizeof(chain), "zone_%s_forward",
284 snprintf(chain, sizeof(chain), "zone_%s_input",
290 snprintf(chain, sizeof(chain), "FORWARD");
292 snprintf(chain, sizeof(chain), "INPUT");
296 if (rule->dest.set && !rule->src.set)
299 snprintf(chain, sizeof(chain), "OUTPUT");
301 snprintf(chain, sizeof(chain), "zone_%s_output",
306 fw3_ipt_rule_append(r, chain);
309 static void set_target(struct fw3_ipt_rule *r, struct fw3_rule *rule)
312 struct fw3_mark *mark;
313 char buf[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
318 name = rule->set_mark.set ? "--set-mark" : "--set-xmark";
319 mark = rule->set_mark.set ? &rule->set_mark : &rule->set_xmark;
320 sprintf(buf, "0x%x/0x%x", mark->mark, mark->mask);
322 fw3_ipt_rule_target(r, "MARK");
323 fw3_ipt_rule_addarg(r, false, name, buf);
326 case FW3_FLAG_NOTRACK:
327 fw3_ipt_rule_target(r, "CT");
328 fw3_ipt_rule_addarg(r, false, "--notrack", NULL);
331 case FW3_FLAG_ACCEPT:
333 name = fw3_flag_names[rule->target];
337 name = fw3_flag_names[FW3_FLAG_REJECT];
341 if (rule->dest.set && !rule->dest.any)
342 fw3_ipt_rule_target(r, "zone_%s_dest_%s", rule->dest.name, name);
343 else if (need_src_action_chain(rule))
344 fw3_ipt_rule_target(r, "zone_%s_src_%s", rule->src.name, name);
345 else if (strcmp(name, "REJECT"))
346 fw3_ipt_rule_target(r, name);
348 fw3_ipt_rule_target(r, "reject");
352 set_comment(struct fw3_ipt_rule *r, const char *name, int num)
355 fw3_ipt_rule_comment(r, name);
357 fw3_ipt_rule_comment(r, "@rule[%u]", num);
361 print_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
362 struct fw3_rule *rule, int num, struct fw3_protocol *proto,
363 struct fw3_address *sip, struct fw3_address *dip,
364 struct fw3_port *sport, struct fw3_port *dport,
365 struct fw3_mac *mac, struct fw3_icmptype *icmptype)
367 struct fw3_ipt_rule *r;
369 if (!fw3_is_family(sip, handle->family) ||
370 !fw3_is_family(dip, handle->family))
372 if ((sip && !sip->resolved) || (dip && !dip->resolved))
373 info(" ! Skipping due to different family of ip address");
378 if (proto->protocol == 58 && handle->family == FW3_FAMILY_V4)
380 info(" ! Skipping due to different family of protocol");
384 r = fw3_ipt_rule_create(handle, proto, NULL, NULL, sip, dip);
385 fw3_ipt_rule_sport_dport(r, sport, dport);
386 fw3_ipt_rule_device(r, rule->device, rule->direction_out);
387 fw3_ipt_rule_icmptype(r, icmptype);
388 fw3_ipt_rule_mac(r, mac);
389 fw3_ipt_rule_ipset(r, &rule->ipset);
390 fw3_ipt_rule_limit(r, &rule->limit);
391 fw3_ipt_rule_time(r, &rule->time);
392 fw3_ipt_rule_mark(r, &rule->mark);
394 fw3_ipt_rule_extra(r, rule->extra);
395 set_comment(r, rule->name, num);
396 append_chain(r, rule);
400 expand_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
401 struct fw3_rule *rule, int num)
403 struct fw3_protocol *proto;
404 struct fw3_address *sip;
405 struct fw3_address *dip;
406 struct fw3_port *sport;
407 struct fw3_port *dport;
409 struct fw3_icmptype *icmptype;
411 struct list_head *sports = NULL;
412 struct list_head *dports = NULL;
413 struct list_head *icmptypes = NULL;
415 struct list_head empty;
416 INIT_LIST_HEAD(&empty);
418 if (!fw3_is_family(rule, handle->family))
421 if ((rule->target == FW3_FLAG_NOTRACK && handle->table != FW3_TABLE_RAW) ||
422 (rule->target == FW3_FLAG_MARK && handle->table != FW3_TABLE_MANGLE) ||
423 (rule->target < FW3_FLAG_NOTRACK && handle->table != FW3_TABLE_FILTER))
427 info(" * Rule '%s'", rule->name);
429 info(" * Rule #%u", num);
431 if (!fw3_is_family(rule->_src, handle->family) ||
432 !fw3_is_family(rule->_dest, handle->family))
434 info(" ! Skipping due to different family of zone");
440 if (!fw3_is_family(rule->ipset.ptr, handle->family))
442 info(" ! Skipping due to different family in ipset");
446 if (!fw3_check_ipset(rule->ipset.ptr))
448 info(" ! Skipping due to missing ipset '%s'",
449 rule->ipset.ptr->external
450 ? rule->ipset.ptr->external : rule->ipset.ptr->name);
454 set(rule->ipset.ptr->flags, handle->family, handle->family);
457 list_for_each_entry(proto, &rule->proto, list)
459 /* icmp / ipv6-icmp */
460 if (proto->protocol == 1 || proto->protocol == 58)
464 icmptypes = &rule->icmp_type;
468 sports = &rule->port_src;
469 dports = &rule->port_dest;
473 fw3_foreach(sip, &rule->ip_src)
474 fw3_foreach(dip, &rule->ip_dest)
475 fw3_foreach(sport, sports)
476 fw3_foreach(dport, dports)
477 fw3_foreach(mac, &rule->mac_src)
478 fw3_foreach(icmptype, icmptypes)
479 print_rule(handle, state, rule, num, proto, sip, dip,
480 sport, dport, mac, icmptype);
485 fw3_print_rules(struct fw3_ipt_handle *handle, struct fw3_state *state)
488 struct fw3_rule *rule;
490 list_for_each_entry(rule, &state->rules, list)
491 expand_rule(handle, state, rule, num++);