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_forward_opts
[] = {
23 FW3_OPT("enabled", bool, forward
, enabled
),
25 FW3_OPT("name", string
, forward
, name
),
26 FW3_OPT("family", family
, forward
, family
),
28 FW3_OPT("src", device
, forward
, src
),
29 FW3_OPT("dest", device
, forward
, dest
),
36 fw3_load_forwards(struct fw3_state
*state
, struct uci_package
*p
)
38 struct uci_section
*s
;
39 struct uci_element
*e
;
40 struct fw3_forward
*forward
;
42 INIT_LIST_HEAD(&state
->forwards
);
44 uci_foreach_element(&p
->sections
, e
)
46 s
= uci_to_section(e
);
48 if (strcmp(s
->type
, "forwarding"))
51 forward
= malloc(sizeof(*forward
));
56 memset(forward
, 0, sizeof(*forward
));
58 forward
->enabled
= true;
60 fw3_parse_options(forward
, fw3_forward_opts
, s
);
62 if (!forward
->enabled
)
64 fw3_free_forward(forward
);
68 if (forward
->src
.invert
|| forward
->dest
.invert
)
70 warn_elem(e
, "must not have inverted 'src' or 'dest' options");
71 fw3_free_forward(forward
);
74 else if (forward
->src
.set
&& !forward
->src
.any
&&
75 !(forward
->_src
= fw3_lookup_zone(state
, forward
->src
.name
)))
77 warn_elem(e
, "refers to not existing zone '%s'", forward
->src
.name
);
78 fw3_free_forward(forward
);
81 else if (forward
->dest
.set
&& !forward
->dest
.any
&&
82 !(forward
->_dest
= fw3_lookup_zone(state
, forward
->dest
.name
)))
84 warn_elem(e
, "refers to not existing zone '%s'", forward
->dest
.name
);
85 fw3_free_forward(forward
);
89 /* NB: forward family... */
92 setbit(forward
->_dest
->flags
[0], FW3_FLAG_ACCEPT
);
93 setbit(forward
->_dest
->flags
[1], FW3_FLAG_ACCEPT
);
96 (forward
->_src
->conntrack
|| forward
->_dest
->conntrack
))
98 forward
->_src
->conntrack
= forward
->_dest
->conntrack
= true;
102 list_add_tail(&forward
->list
, &state
->forwards
);
109 append_chain(struct fw3_ipt_rule
*r
, struct fw3_forward
*forward
)
111 if (forward
->src
.any
|| !forward
->src
.set
)
112 fw3_ipt_rule_append(r
, "delegate_forward");
114 fw3_ipt_rule_append(r
, "zone_%s_forward", forward
->src
.name
);
117 static void set_target(struct fw3_ipt_rule
*r
, struct fw3_forward
*forward
)
119 if (forward
->dest
.any
|| !forward
->dest
.set
)
120 fw3_ipt_rule_target(r
, "ACCEPT");
122 fw3_ipt_rule_target(r
, "zone_%s_dest_ACCEPT", forward
->dest
.name
);
126 print_forward(struct fw3_ipt_handle
*handle
, struct fw3_forward
*forward
)
129 struct fw3_ipt_rule
*r
;
131 if (handle
->table
!= FW3_TABLE_FILTER
)
134 if (!fw3_is_family(forward
, handle
->family
))
137 s
= forward
->_src
? forward
->_src
->name
: "*";
138 d
= forward
->_dest
? forward
->_dest
->name
: "*";
140 info(" * Forward '%s' -> '%s'", s
, d
);
142 if (!fw3_is_family(forward
->_src
, handle
->family
) ||
143 !fw3_is_family(forward
->_dest
, handle
->family
))
145 info(" ! Skipping due to different family of zone");
149 r
= fw3_ipt_rule_new(handle
);
150 fw3_ipt_rule_comment(r
, "forwarding %s -> %s", s
, d
);
151 set_target(r
, forward
);
152 append_chain(r
, forward
);
156 fw3_print_forwards(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
)
158 struct fw3_forward
*forward
;
160 list_for_each_entry(forward
, &state
->forwards
, list
)
161 print_forward(handle
, forward
);