7f748ebb043fb96028d3598fc5f93c94ace3d137
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("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 fw3_load_rules(struct fw3_state
*state
, struct uci_package
*p
)
69 struct uci_section
*s
;
70 struct uci_element
*e
;
71 struct fw3_rule
*rule
;
73 INIT_LIST_HEAD(&state
->rules
);
75 uci_foreach_element(&p
->sections
, e
)
77 s
= uci_to_section(e
);
79 if (strcmp(s
->type
, "rule"))
82 rule
= malloc(sizeof(*rule
));
87 memset(rule
, 0, sizeof(*rule
));
89 INIT_LIST_HEAD(&rule
->proto
);
91 INIT_LIST_HEAD(&rule
->ip_src
);
92 INIT_LIST_HEAD(&rule
->mac_src
);
93 INIT_LIST_HEAD(&rule
->port_src
);
95 INIT_LIST_HEAD(&rule
->ip_dest
);
96 INIT_LIST_HEAD(&rule
->port_dest
);
98 INIT_LIST_HEAD(&rule
->icmp_type
);
100 rule
->enabled
= true;
102 fw3_parse_options(rule
, fw3_rule_opts
, s
);
110 if (rule
->src
.invert
|| rule
->dest
.invert
)
112 warn_elem(e
, "must not have inverted 'src' or 'dest' options");
116 else if (rule
->src
.set
&& !rule
->src
.any
&&
117 !(rule
->_src
= fw3_lookup_zone(state
, rule
->src
.name
)))
119 warn_elem(e
, "refers to not existing zone '%s'", rule
->src
.name
);
123 else if (rule
->dest
.set
&& !rule
->dest
.any
&&
124 !(rule
->_dest
= fw3_lookup_zone(state
, rule
->dest
.name
)))
126 warn_elem(e
, "refers to not existing zone '%s'", rule
->dest
.name
);
130 else if (rule
->ipset
.set
&& state
->disable_ipsets
)
132 warn_elem(e
, "skipped due to disabled ipset support");
136 else if (rule
->ipset
.set
&& !rule
->ipset
.any
&&
137 !(rule
->_ipset
= fw3_lookup_ipset(state
, rule
->ipset
.name
)))
139 warn_elem(e
, "refers to unknown ipset '%s'", rule
->ipset
.name
);
144 if (!rule
->_src
&& rule
->target
== FW3_FLAG_NOTRACK
)
146 warn_elem(e
, "is set to target NOTRACK but has no source assigned");
151 if (!rule
->set_mark
.set
&& !rule
->set_xmark
.set
&&
152 rule
->target
== FW3_FLAG_MARK
)
154 warn_elem(e
, "is set to target MARK but specifies neither "
155 "'set_mark' nor 'set_xmark' option");
160 if (rule
->_dest
&& rule
->target
== FW3_FLAG_MARK
)
162 warn_elem(e
, "must not specify 'dest' for MARK target");
167 if (rule
->set_mark
.invert
|| rule
->set_xmark
.invert
)
169 warn_elem(e
, "must not have inverted 'set_mark' or 'set_xmark'");
174 if (!rule
->_src
&& !rule
->_dest
&& !rule
->src
.any
&& !rule
->dest
.any
)
176 warn_elem(e
, "has neither a source nor a destination zone assigned "
177 "- assuming an output rule");
180 if (list_empty(&rule
->proto
))
182 warn_elem(e
, "does not specify a protocol, assuming TCP+UDP");
183 fw3_parse_protocol(&rule
->proto
, "tcpudp", true);
186 if (rule
->target
== FW3_FLAG_UNSPEC
)
188 warn_elem(e
, "has no target specified, defaulting to REJECT");
189 rule
->target
= FW3_FLAG_REJECT
;
191 else if (rule
->target
> FW3_FLAG_MARK
)
193 warn_elem(e
, "has invalid target specified, defaulting to REJECT");
194 rule
->target
= FW3_FLAG_REJECT
;
197 /* NB: rule family... */
200 setbit(rule
->_dest
->flags
[0], rule
->target
);
201 setbit(rule
->_dest
->flags
[1], rule
->target
);
204 list_add_tail(&rule
->list
, &state
->rules
);
211 print_chain(struct fw3_rule
*rule
)
215 sprintf(chain
, "delegate_output");
217 if (rule
->target
== FW3_FLAG_NOTRACK
)
219 sprintf(chain
, "zone_%s_notrack", rule
->src
.name
);
221 else if (rule
->target
== FW3_FLAG_MARK
)
223 sprintf(chain
, "fwmark");
232 sprintf(chain
, "zone_%s_forward", rule
->src
.name
);
234 sprintf(chain
, "zone_%s_input", rule
->src
.name
);
239 sprintf(chain
, "delegate_forward");
241 sprintf(chain
, "delegate_input");
245 if (rule
->dest
.set
&& !rule
->src
.set
)
246 sprintf(chain
, "zone_%s_output", rule
->dest
.name
);
249 fw3_pr("-A %s", chain
);
252 static void print_target(struct fw3_rule
*rule
)
259 if (rule
->set_mark
.set
)
260 fw3_pr(" -j MARK --set-mark 0x%x/0x%x\n",
261 rule
->set_mark
.mark
, rule
->set_mark
.mask
);
263 fw3_pr(" -j MARK --set-xmark 0x%x/0x%x\n",
264 rule
->set_xmark
.mark
, rule
->set_xmark
.mask
);
267 case FW3_FLAG_ACCEPT
:
269 case FW3_FLAG_NOTRACK
:
270 target
= fw3_flag_names
[rule
->target
];
274 target
= fw3_flag_names
[FW3_FLAG_REJECT
];
278 if (rule
->dest
.set
&& !rule
->dest
.any
)
279 fw3_pr(" -j zone_%s_dest_%s\n", rule
->dest
.name
, target
);
280 else if (rule
->target
== FW3_FLAG_REJECT
)
281 fw3_pr(" -j reject\n");
283 fw3_pr(" -j %s\n", target
);
287 print_rule(struct fw3_state
*state
, enum fw3_family family
,
288 enum fw3_table table
, struct fw3_rule
*rule
,
289 struct fw3_protocol
*proto
,
290 struct fw3_address
*sip
, struct fw3_address
*dip
,
291 struct fw3_port
*sport
, struct fw3_port
*dport
,
292 struct fw3_mac
*mac
, struct fw3_icmptype
*icmptype
)
294 if (!fw3_is_family(sip
, family
) || !fw3_is_family(dip
, family
))
296 info(" ! Skipping due to different family of ip address");
300 if (proto
->protocol
== 58 && family
== FW3_FAMILY_V4
)
302 info(" ! Skipping due to different family of protocol");
307 fw3_format_ipset(rule
->_ipset
, rule
->ipset
.invert
);
308 fw3_format_protocol(proto
, family
);
309 fw3_format_src_dest(sip
, dip
);
310 fw3_format_sport_dport(sport
, dport
);
311 fw3_format_icmptype(icmptype
, family
);
313 fw3_format_limit(&rule
->limit
);
314 fw3_format_time(&rule
->time
);
315 fw3_format_mark(&rule
->mark
);
316 fw3_format_extra(rule
->extra
);
317 fw3_format_comment(rule
->name
);
322 expand_rule(struct fw3_state
*state
, enum fw3_family family
,
323 enum fw3_table table
, struct fw3_rule
*rule
, int num
)
325 struct fw3_protocol
*proto
;
326 struct fw3_address
*sip
;
327 struct fw3_address
*dip
;
328 struct fw3_port
*sport
;
329 struct fw3_port
*dport
;
331 struct fw3_icmptype
*icmptype
;
333 struct list_head
*sports
= NULL
;
334 struct list_head
*dports
= NULL
;
335 struct list_head
*icmptypes
= NULL
;
337 struct list_head empty
;
338 INIT_LIST_HEAD(&empty
);
340 if (!fw3_is_family(rule
, family
))
343 if ((rule
->target
== FW3_FLAG_NOTRACK
&& table
!= FW3_TABLE_RAW
) ||
344 (rule
->target
== FW3_FLAG_MARK
&& table
!= FW3_TABLE_MANGLE
) ||
345 (rule
->target
< FW3_FLAG_NOTRACK
&& table
!= FW3_TABLE_FILTER
))
349 info(" * Rule '%s'", rule
->name
);
351 info(" * Rule #%u", num
);
353 if (!fw3_is_family(rule
->_src
, family
) ||
354 !fw3_is_family(rule
->_dest
, family
))
356 info(" ! Skipping due to different family of zone");
362 if (!fw3_is_family(rule
->_ipset
, family
))
364 info(" ! Skipping due to different family in ipset");
368 if (!fw3_check_ipset(rule
->_ipset
))
370 info(" ! Skipping due to missing ipset '%s'",
371 rule
->_ipset
->external
372 ? rule
->_ipset
->external
: rule
->_ipset
->name
);
376 set(rule
->_ipset
->flags
, family
, family
);
379 list_for_each_entry(proto
, &rule
->proto
, list
)
381 /* icmp / ipv6-icmp */
382 if (proto
->protocol
== 1 || proto
->protocol
== 58)
386 icmptypes
= &rule
->icmp_type
;
390 sports
= &rule
->port_src
;
391 dports
= &rule
->port_dest
;
395 fw3_foreach(sip
, &rule
->ip_src
)
396 fw3_foreach(dip
, &rule
->ip_dest
)
397 fw3_foreach(sport
, sports
)
398 fw3_foreach(dport
, dports
)
399 fw3_foreach(mac
, &rule
->mac_src
)
400 fw3_foreach(icmptype
, icmptypes
)
401 print_rule(state
, family
, table
, rule
, proto
, sip
, dip
,
402 sport
, dport
, mac
, icmptype
);
407 fw3_print_rules(struct fw3_state
*state
, enum fw3_family family
,
408 enum fw3_table table
)
411 struct fw3_rule
*rule
;
413 list_for_each_entry(rule
, &state
->rules
, list
)
414 expand_rule(state
, family
, table
, rule
, num
++);