snat: ICMP can be port-natted as well
[project/firewall3.git] / snats.c
1 /*
2  * firewall3 - 3rd OpenWrt UCI firewall implementation
3  *
4  *   Copyright (C) 2014 Jo-Philipp Wich <jow@openwrt.org>
5  *
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.
9  *
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.
17  */
18
19 #include "snats.h"
20
21
22 const struct fw3_option fw3_snat_opts[] = {
23         FW3_OPT("enabled",             bool,      snat,     enabled),
24
25         FW3_OPT("name",                string,    snat,     name),
26         FW3_OPT("family",              family,    snat,     family),
27
28         FW3_OPT("src",                 device,    snat,     src),
29
30         FW3_OPT("ipset",               setmatch,  snat,     ipset),
31
32         FW3_LIST("proto",              protocol,  snat,     proto),
33
34         FW3_OPT("src_ip",              network,   snat,     ip_src),
35         FW3_OPT("src_port",            port,      snat,     port_src),
36
37         FW3_OPT("snat_ip",             network,   snat,     ip_snat),
38         FW3_OPT("snat_port",           port,      snat,     port_snat),
39
40         FW3_OPT("dest_ip",             network,   snat,     ip_dest),
41         FW3_OPT("dest_port",           port,      snat,     port_dest),
42
43         FW3_OPT("extra",               string,    snat,     extra),
44
45         FW3_OPT("limit",               limit,     snat,     limit),
46         FW3_OPT("limit_burst",         int,       snat,     limit.burst),
47
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),
55
56         FW3_OPT("mark",                mark,      snat,     mark),
57
58         FW3_OPT("target",              target,    snat,     target),
59
60         { }
61 };
62
63
64 static bool
65 check_families(struct uci_element *e, struct fw3_snat *r)
66 {
67         if (r->family == FW3_FAMILY_ANY)
68                 return true;
69
70         if (r->_src && r->_src->family && r->_src->family != r->family)
71         {
72                 warn_elem(e, "refers to source zone with different family");
73                 return false;
74         }
75
76         if (r->ipset.ptr && r->ipset.ptr->family &&
77             r->ipset.ptr->family != r->family)
78         {
79                 warn_elem(e, "refers to ipset with different family");
80                 return false;
81         }
82
83         if (r->ip_src.family && r->ip_src.family != r->family)
84         {
85                 warn_elem(e, "uses source ip with different family");
86                 return false;
87         }
88
89         if (r->ip_dest.family && r->ip_dest.family != r->family)
90         {
91                 warn_elem(e, "uses destination ip with different family");
92                 return false;
93         }
94
95         if (r->ip_snat.family && r->ip_snat.family != r->family)
96         {
97                 warn_elem(e, "uses snat ip with different family");
98                 return false;
99         }
100
101         return true;
102 }
103
104 void
105 fw3_load_snats(struct fw3_state *state, struct uci_package *p)
106 {
107         struct uci_section *s;
108         struct uci_element *e;
109         struct fw3_snat *snat;
110
111         INIT_LIST_HEAD(&state->snats);
112
113         uci_foreach_element(&p->sections, e)
114         {
115                 s = uci_to_section(e);
116
117                 if (strcmp(s->type, "nat"))
118                         continue;
119
120                 snat = malloc(sizeof(*snat));
121
122                 if (!snat)
123                         continue;
124
125                 memset(snat, 0, sizeof(*snat));
126
127                 INIT_LIST_HEAD(&snat->proto);
128
129                 snat->enabled = true;
130
131                 if (!fw3_parse_options(snat, fw3_snat_opts, s))
132                 {
133                         warn_elem(e, "skipped due to invalid options");
134                         fw3_free_snat(snat);
135                         continue;
136                 }
137
138                 if (!snat->enabled)
139                 {
140                         fw3_free_snat(snat);
141                         continue;
142                 }
143
144                 if (snat->src.invert)
145                 {
146                         warn_elem(e, "must not have an inverted source");
147                         fw3_free_snat(snat);
148                         continue;
149                 }
150                 else if (snat->src.set && !snat->src.any &&
151                          !(snat->_src = fw3_lookup_zone(state, snat->src.name)))
152                 {
153                         warn_elem(e, "refers to not existing zone '%s'", snat->src.name);
154                         fw3_free_snat(snat);
155                         continue;
156                 }
157                 else if (snat->ipset.set && state->disable_ipsets)
158                 {
159                         warn_elem(e, "skipped due to disabled ipset support");
160                         fw3_free_snat(snat);
161                         continue;
162                 }
163                 else if (snat->ipset.set &&
164                          !(snat->ipset.ptr = fw3_lookup_ipset(state, snat->ipset.name)))
165                 {
166                         warn_elem(e, "refers to unknown ipset '%s'", snat->ipset.name);
167                         fw3_free_snat(snat);
168                         continue;
169                 }
170
171                 if (!check_families(e, snat))
172                 {
173                         fw3_free_snat(snat);
174                         continue;
175                 }
176
177                 if (snat->target == FW3_FLAG_UNSPEC)
178                 {
179                         warn_elem(e, "has no target specified, defaulting to MASQUERADE");
180                         snat->target = FW3_FLAG_MASQUERADE;
181                 }
182                 else if (snat->target != FW3_FLAG_ACCEPT && snat->target != FW3_FLAG_SNAT &&
183                                 snat->target != FW3_FLAG_MASQUERADE)
184                 {
185                         warn_elem(e, "has invalid target specified, defaulting to MASQUERADE");
186                         snat->target = FW3_FLAG_MASQUERADE;
187                 }
188
189                 if (snat->target == FW3_FLAG_SNAT &&
190                     !snat->ip_snat.set && !snat->port_snat.set)
191                 {
192                         warn_elem(e, "needs either 'snat_ip' or 'snat_port' for SNAT");
193                         fw3_free_snat(snat);
194                         continue;
195                 }
196                 else if (snat->target != FW3_FLAG_SNAT && snat->ip_snat.set)
197                 {
198                         warn_elem(e, "must not use 'snat_ip' for non-SNAT");
199                         fw3_free_snat(snat);
200                         continue;
201                 }
202                 else if (snat->target != FW3_FLAG_SNAT && snat->port_snat.set)
203                 {
204                         warn_elem(e, "must not use 'snat_port' for non-SNAT");
205                         fw3_free_snat(snat);
206                         continue;
207                 }
208
209                 if (list_empty(&snat->proto))
210                 {
211                         warn_elem(e, "does not specify a protocol, assuming all");
212                         fw3_parse_protocol(&snat->proto, "all", true);
213                 }
214
215                 if (snat->_src)
216                 {
217                         set(snat->_src->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
218                         snat->_src->conntrack = true;
219                 }
220
221                 list_add_tail(&snat->list, &state->snats);
222         }
223 }
224
225 static void
226 append_chain(struct fw3_ipt_rule *r, struct fw3_snat *snat)
227 {
228         if (snat->_src)
229                 fw3_ipt_rule_append(r, "zone_%s_postrouting", snat->src.name);
230         else
231                 fw3_ipt_rule_append(r, "delegate_postrouting");
232 }
233
234 static void
235 set_target(struct fw3_ipt_rule *r, struct fw3_snat *snat,
236            struct fw3_protocol *proto)
237 {
238         char buf[sizeof("255.255.255.255:65535-65535\0")];
239
240         if (snat->target == FW3_FLAG_SNAT)
241         {
242                 buf[0] = '\0';
243
244                 if (snat->ip_snat.set)
245                 {
246                         inet_ntop(AF_INET, &snat->ip_snat.address.v4, buf, sizeof(buf));
247                 }
248
249                 if (snat->port_snat.set && proto && !proto->any &&
250                     (proto->protocol == 6 || proto->protocol == 17 || proto->protocol == 1))
251                 {
252                         if (snat->port_snat.port_min == snat->port_snat.port_max)
253                                 sprintf(buf + strlen(buf), ":%u", snat->port_snat.port_min);
254                         else
255                                 sprintf(buf + strlen(buf), ":%u-%u",
256                                                 snat->port_snat.port_min, snat->port_snat.port_max);
257                 }
258
259                 fw3_ipt_rule_target(r, "SNAT");
260                 fw3_ipt_rule_addarg(r, false, "--to-source", buf);
261         }
262         else if (snat->target == FW3_FLAG_ACCEPT)
263         {
264                 fw3_ipt_rule_target(r, "ACCEPT");
265         }
266         else
267         {
268                 fw3_ipt_rule_target(r, "MASQUERADE");
269         }
270 }
271
272 static void
273 set_comment(struct fw3_ipt_rule *r, const char *name, int num)
274 {
275         if (name)
276                 fw3_ipt_rule_comment(r, name);
277         else
278                 fw3_ipt_rule_comment(r, "@nat[%u]", num);
279 }
280
281 static void
282 print_snat(struct fw3_ipt_handle *h, struct fw3_state *state,
283            struct fw3_snat *snat, int num, struct fw3_protocol *proto)
284 {
285         struct fw3_ipt_rule *r;
286         struct fw3_address *src, *dst;
287         struct fw3_port *spt, *dpt;
288
289         switch (h->table)
290         {
291         case FW3_TABLE_NAT:
292                 src = &snat->ip_src;
293                 dst = &snat->ip_dest;
294                 spt = &snat->port_src;
295                 dpt = &snat->port_dest;
296
297                 r = fw3_ipt_rule_create(h, proto, NULL, NULL, src, dst);
298                 fw3_ipt_rule_sport_dport(r, spt, dpt);
299                 fw3_ipt_rule_ipset(r, &snat->ipset);
300                 fw3_ipt_rule_limit(r, &snat->limit);
301                 fw3_ipt_rule_time(r, &snat->time);
302                 fw3_ipt_rule_mark(r, &snat->mark);
303                 set_target(r, snat, proto);
304                 fw3_ipt_rule_extra(r, snat->extra);
305                 set_comment(r, snat->name, num);
306                 append_chain(r, snat);
307                 break;
308
309         default:
310                 break;
311         }
312 }
313
314 static void
315 expand_snat(struct fw3_ipt_handle *handle, struct fw3_state *state,
316                 struct fw3_snat *snat, int num)
317 {
318         struct fw3_protocol *proto;
319
320         if (snat->name)
321                 info("   * NAT '%s'", snat->name);
322         else
323                 info("   * NAT #%u", num);
324
325         if (!fw3_is_family(snat->_src, handle->family))
326         {
327                 info("     ! Skipping due to different family of zone");
328                 return;
329         }
330
331         if (!fw3_is_family(&snat->ip_src, handle->family) ||
332             !fw3_is_family(&snat->ip_dest, handle->family) ||
333                 !fw3_is_family(&snat->ip_snat, handle->family))
334         {
335                 if (!snat->ip_src.resolved ||
336                     !snat->ip_dest.resolved ||
337                     !snat->ip_snat.resolved)
338                         info("     ! Skipping due to different family of ip address");
339
340                 return;
341         }
342
343         if (snat->ipset.ptr)
344         {
345                 if (!fw3_is_family(snat->ipset.ptr, handle->family))
346                 {
347                         info("     ! Skipping due to different family in ipset");
348                         return;
349                 }
350
351                 if (!fw3_check_ipset(snat->ipset.ptr))
352                 {
353                         info("     ! Skipping due to missing ipset '%s'",
354                              snat->ipset.ptr->external ?
355                                         snat->ipset.ptr->external : snat->ipset.ptr->name);
356                         return;
357                 }
358
359                 set(snat->ipset.ptr->flags, handle->family, handle->family);
360         }
361
362         fw3_foreach(proto, &snat->proto)
363                 print_snat(handle, state, snat, num, proto);
364 }
365
366 void
367 fw3_print_snats(struct fw3_ipt_handle *handle, struct fw3_state *state)
368 {
369         int num = 0;
370         struct fw3_snat *snat;
371
372         if (handle->family == FW3_FAMILY_V6)
373                 return;
374
375         if (handle->table != FW3_TABLE_NAT)
376                 return;
377
378         list_for_each_entry(snat, &state->snats, list)
379                 expand_snat(handle, state, snat, num++);
380 }