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