2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2014 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_snat_opts
[] = {
23 FW3_OPT("enabled", bool, snat
, enabled
),
25 FW3_OPT("name", string
, snat
, name
),
26 FW3_OPT("family", family
, snat
, family
),
28 FW3_OPT("src", device
, snat
, src
),
30 FW3_OPT("ipset", setmatch
, snat
, ipset
),
32 FW3_LIST("proto", protocol
, snat
, proto
),
34 FW3_OPT("src_ip", network
, snat
, ip_src
),
35 FW3_OPT("src_port", port
, snat
, port_src
),
37 FW3_OPT("snat_ip", network
, snat
, ip_snat
),
38 FW3_OPT("snat_port", port
, snat
, port_snat
),
40 FW3_OPT("dest_ip", network
, snat
, ip_dest
),
41 FW3_OPT("dest_port", port
, snat
, port_dest
),
43 FW3_OPT("extra", string
, snat
, extra
),
45 FW3_OPT("limit", limit
, snat
, limit
),
46 FW3_OPT("limit_burst", int, snat
, limit
.burst
),
48 FW3_OPT("utc_time", bool, snat
, time
.utc
),
49 FW3_OPT("start_date", date
, snat
, time
.datestart
),
50 FW3_OPT("stop_date", date
, snat
, time
.datestop
),
51 FW3_OPT("start_time", time
, snat
, time
.timestart
),
52 FW3_OPT("stop_time", time
, snat
, time
.timestop
),
53 FW3_OPT("weekdays", weekdays
, snat
, time
.weekdays
),
54 FW3_OPT("monthdays", monthdays
, snat
, time
.monthdays
),
56 FW3_OPT("mark", mark
, snat
, mark
),
58 FW3_OPT("target", target
, snat
, target
),
65 check_families(struct uci_element
*e
, struct fw3_snat
*r
)
67 if (r
->family
== FW3_FAMILY_ANY
)
70 if (r
->_src
&& r
->_src
->family
&& r
->_src
->family
!= r
->family
)
72 warn_elem(e
, "refers to source zone with different family");
76 if (r
->ipset
.ptr
&& r
->ipset
.ptr
->family
&&
77 r
->ipset
.ptr
->family
!= r
->family
)
79 warn_elem(e
, "refers to ipset with different family");
83 if (r
->ip_src
.family
&& r
->ip_src
.family
!= r
->family
)
85 warn_elem(e
, "uses source ip with different family");
89 if (r
->ip_dest
.family
&& r
->ip_dest
.family
!= r
->family
)
91 warn_elem(e
, "uses destination ip with different family");
95 if (r
->ip_snat
.family
&& r
->ip_snat
.family
!= r
->family
)
97 warn_elem(e
, "uses snat ip with different family");
105 fw3_load_snats(struct fw3_state
*state
, struct uci_package
*p
)
107 struct uci_section
*s
;
108 struct uci_element
*e
;
109 struct fw3_snat
*snat
;
111 INIT_LIST_HEAD(&state
->snats
);
113 uci_foreach_element(&p
->sections
, e
)
115 s
= uci_to_section(e
);
117 if (strcmp(s
->type
, "nat"))
120 snat
= malloc(sizeof(*snat
));
125 memset(snat
, 0, sizeof(*snat
));
127 INIT_LIST_HEAD(&snat
->proto
);
129 snat
->enabled
= true;
131 if (!fw3_parse_options(snat
, fw3_snat_opts
, s
))
133 warn_elem(e
, "skipped due to invalid options");
144 if (snat
->src
.invert
)
146 warn_elem(e
, "must not have an inverted source");
150 else if (snat
->src
.set
&& !snat
->src
.any
&&
151 !(snat
->_src
= fw3_lookup_zone(state
, snat
->src
.name
)))
153 warn_elem(e
, "refers to not existing zone '%s'", snat
->src
.name
);
157 else if (snat
->ipset
.set
&& state
->disable_ipsets
)
159 warn_elem(e
, "skipped due to disabled ipset support");
163 else if (snat
->ipset
.set
&&
164 !(snat
->ipset
.ptr
= fw3_lookup_ipset(state
, snat
->ipset
.name
)))
166 warn_elem(e
, "refers to unknown ipset '%s'", snat
->ipset
.name
);
171 if (!check_families(e
, snat
))
177 if (snat
->target
== FW3_FLAG_UNSPEC
)
179 warn_elem(e
, "has no target specified, defaulting to MASQUERADE");
180 snat
->target
= FW3_FLAG_MASQUERADE
;
182 else if (snat
->target
< FW3_FLAG_SNAT
|| snat
->target
> FW3_FLAG_MASQUERADE
)
184 warn_elem(e
, "has invalid target specified, defaulting to MASQUERADE");
185 snat
->target
= FW3_FLAG_MASQUERADE
;
188 if (snat
->target
== FW3_FLAG_SNAT
&&
189 !snat
->ip_snat
.set
&& !snat
->port_snat
.set
)
191 warn_elem(e
, "needs either 'snat_ip' or 'snat_port' for SNAT");
195 else if (snat
->target
== FW3_FLAG_MASQUERADE
&& snat
->ip_snat
.set
)
197 warn_elem(e
, "must not use 'snat_ip' for MASQUERADE");
201 else if (snat
->target
== FW3_FLAG_MASQUERADE
&& snat
->port_snat
.set
)
203 warn_elem(e
, "must not use 'snat_port' for MASQUERADE");
208 if (list_empty(&snat
->proto
))
210 warn_elem(e
, "does not specify a protocol, assuming all");
211 fw3_parse_protocol(&snat
->proto
, "all", true);
216 set(snat
->_src
->flags
, FW3_FAMILY_V4
, FW3_FLAG_SNAT
);
217 snat
->_src
->conntrack
= true;
220 list_add_tail(&snat
->list
, &state
->snats
);
225 append_chain(struct fw3_ipt_rule
*r
, struct fw3_snat
*snat
)
228 fw3_ipt_rule_append(r
, "zone_%s_postrouting", snat
->src
.name
);
230 fw3_ipt_rule_append(r
, "delegate_postrouting");
234 set_target(struct fw3_ipt_rule
*r
, struct fw3_snat
*snat
,
235 struct fw3_protocol
*proto
)
237 char buf
[sizeof("255.255.255.255:65535-65535\0")];
239 if (snat
->target
== FW3_FLAG_SNAT
)
243 if (snat
->ip_snat
.set
)
245 inet_ntop(AF_INET
, &snat
->ip_snat
.address
.v4
, buf
, sizeof(buf
));
248 if (snat
->port_snat
.set
&& proto
&& !proto
->any
&&
249 (proto
->protocol
== 6 || proto
->protocol
== 17))
251 if (snat
->port_snat
.port_min
== snat
->port_snat
.port_max
)
252 sprintf(buf
+ strlen(buf
), ":%u", snat
->port_snat
.port_min
);
254 sprintf(buf
+ strlen(buf
), ":%u-%u",
255 snat
->port_snat
.port_min
, snat
->port_snat
.port_max
);
258 fw3_ipt_rule_target(r
, "SNAT");
259 fw3_ipt_rule_addarg(r
, false, "--to-source", buf
);
263 fw3_ipt_rule_target(r
, "MASQUERADE");
268 set_comment(struct fw3_ipt_rule
*r
, const char *name
, int num
)
271 fw3_ipt_rule_comment(r
, name
);
273 fw3_ipt_rule_comment(r
, "@nat[%u]", num
);
277 print_snat(struct fw3_ipt_handle
*h
, struct fw3_state
*state
,
278 struct fw3_snat
*snat
, int num
, struct fw3_protocol
*proto
)
280 struct fw3_ipt_rule
*r
;
281 struct fw3_address
*src
, *dst
;
282 struct fw3_port
*spt
, *dpt
;
288 dst
= &snat
->ip_dest
;
289 spt
= &snat
->port_src
;
290 dpt
= &snat
->port_dest
;
292 r
= fw3_ipt_rule_create(h
, proto
, NULL
, NULL
, src
, dst
);
293 fw3_ipt_rule_sport_dport(r
, spt
, dpt
);
294 fw3_ipt_rule_ipset(r
, &snat
->ipset
);
295 fw3_ipt_rule_limit(r
, &snat
->limit
);
296 fw3_ipt_rule_time(r
, &snat
->time
);
297 fw3_ipt_rule_mark(r
, &snat
->mark
);
298 set_target(r
, snat
, proto
);
299 fw3_ipt_rule_extra(r
, snat
->extra
);
300 set_comment(r
, snat
->name
, num
);
301 append_chain(r
, snat
);
310 expand_snat(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
311 struct fw3_snat
*snat
, int num
)
313 struct fw3_protocol
*proto
;
316 info(" * NAT '%s'", snat
->name
);
318 info(" * NAT #%u", num
);
320 if (!fw3_is_family(snat
->_src
, handle
->family
))
322 info(" ! Skipping due to different family of zone");
326 if (!fw3_is_family(&snat
->ip_src
, handle
->family
) ||
327 !fw3_is_family(&snat
->ip_dest
, handle
->family
) ||
328 !fw3_is_family(&snat
->ip_snat
, handle
->family
))
330 if (!snat
->ip_src
.resolved
||
331 !snat
->ip_dest
.resolved
||
332 !snat
->ip_snat
.resolved
)
333 info(" ! Skipping due to different family of ip address");
340 if (!fw3_is_family(snat
->ipset
.ptr
, handle
->family
))
342 info(" ! Skipping due to different family in ipset");
346 if (!fw3_check_ipset(snat
->ipset
.ptr
))
348 info(" ! Skipping due to missing ipset '%s'",
349 snat
->ipset
.ptr
->external
?
350 snat
->ipset
.ptr
->external
: snat
->ipset
.ptr
->name
);
354 set(snat
->ipset
.ptr
->flags
, handle
->family
, handle
->family
);
357 fw3_foreach(proto
, &snat
->proto
)
358 print_snat(handle
, state
, snat
, num
, proto
);
362 fw3_print_snats(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
)
365 struct fw3_snat
*snat
;
367 if (handle
->family
== FW3_FAMILY_V6
)
370 if (handle
->table
!= FW3_TABLE_NAT
)
373 list_for_each_entry(snat
, &state
->snats
, list
)
374 expand_snat(handle
, state
, snat
, num
++);