firewall3: add fw3_attr_parse_name_type() function
[project/firewall3.git] / snats.c
1 /*
2  * firewall3 - 3rd OpenWrt UCI firewall implementation
3  *
4  *   Copyright (C) 2014 Jo-Philipp Wich <jo@mein.io>
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
108 static struct fw3_snat*
109 alloc_snat(struct fw3_state *state)
110 {
111         struct fw3_snat *snat = calloc(1, sizeof(*snat));
112
113         if (snat) {
114                 INIT_LIST_HEAD(&snat->proto);
115                 list_add_tail(&snat->list, &state->snats);
116                 snat->enabled = true;
117         }
118
119         return snat;
120 }
121
122
123 void
124 fw3_load_snats(struct fw3_state *state, struct uci_package *p, struct blob_attr *a)
125 {
126         struct uci_section *s;
127         struct uci_element *e;
128         struct fw3_snat *snat, *n;
129         struct blob_attr *entry, *opt;
130         unsigned rem, orem;
131
132         INIT_LIST_HEAD(&state->snats);
133
134         blob_for_each_attr(entry, a, rem) {
135                 const char *type = NULL;
136                 const char *name = "ubus rule";
137
138                 if (!fw3_attr_parse_name_type(entry, &name, &type))
139                         continue;
140
141                 if (strcmp(type, "nat"))
142                         continue;
143
144                 if (!(snat = alloc_snat(state)))
145                         continue;
146
147                 if (!fw3_parse_blob_options(snat, fw3_snat_opts, rule, name))
148                 {
149                         warn_section("nat", snat, NULL, "skipped due to invalid options");
150                         fw3_free_snat(snat);
151                         continue;
152                 }
153         }
154
155         uci_foreach_element(&p->sections, e)
156         {
157                 s = uci_to_section(e);
158
159                 if (strcmp(s->type, "nat"))
160                         continue;
161
162                 if (!(snat = alloc_snat(state)))
163                         continue;
164
165                 if (!fw3_parse_options(snat, fw3_snat_opts, s))
166                 {
167                         warn_elem(e, "skipped due to invalid options");
168                         fw3_free_snat(snat);
169                         continue;
170                 }
171         }
172
173         list_for_each_entry_safe(snat, n, &state->snats, list)
174         {
175                 if (!snat->enabled)
176                 {
177                         fw3_free_snat(snat);
178                         continue;
179                 }
180
181                 if (snat->src.invert)
182                 {
183                         warn_elem(e, "must not have an inverted source");
184                         fw3_free_snat(snat);
185                         continue;
186                 }
187                 else if (snat->src.set && !snat->src.any &&
188                          !(snat->_src = fw3_lookup_zone(state, snat->src.name)))
189                 {
190                         warn_elem(e, "refers to not existing zone '%s'", snat->src.name);
191                         fw3_free_snat(snat);
192                         continue;
193                 }
194                 else if (snat->ipset.set && state->disable_ipsets)
195                 {
196                         warn_elem(e, "skipped due to disabled ipset support");
197                         fw3_free_snat(snat);
198                         continue;
199                 }
200                 else if (snat->ipset.set &&
201                          !(snat->ipset.ptr = fw3_lookup_ipset(state, snat->ipset.name)))
202                 {
203                         warn_elem(e, "refers to unknown ipset '%s'", snat->ipset.name);
204                         fw3_free_snat(snat);
205                         continue;
206                 }
207
208                 if (!check_families(e, snat))
209                 {
210                         fw3_free_snat(snat);
211                         continue;
212                 }
213
214                 if (snat->target == FW3_FLAG_UNSPEC)
215                 {
216                         warn_elem(e, "has no target specified, defaulting to MASQUERADE");
217                         snat->target = FW3_FLAG_MASQUERADE;
218                 }
219                 else if (snat->target != FW3_FLAG_ACCEPT && snat->target != FW3_FLAG_SNAT &&
220                                 snat->target != FW3_FLAG_MASQUERADE)
221                 {
222                         warn_elem(e, "has invalid target specified, defaulting to MASQUERADE");
223                         snat->target = FW3_FLAG_MASQUERADE;
224                 }
225
226                 if (snat->target == FW3_FLAG_SNAT &&
227                     !snat->ip_snat.set && !snat->port_snat.set)
228                 {
229                         warn_elem(e, "needs either 'snat_ip' or 'snat_port' for SNAT");
230                         fw3_free_snat(snat);
231                         continue;
232                 }
233                 else if (snat->target != FW3_FLAG_SNAT && snat->ip_snat.set)
234                 {
235                         warn_elem(e, "must not use 'snat_ip' for non-SNAT");
236                         fw3_free_snat(snat);
237                         continue;
238                 }
239                 else if (snat->target != FW3_FLAG_SNAT && snat->port_snat.set)
240                 {
241                         warn_elem(e, "must not use 'snat_port' for non-SNAT");
242                         fw3_free_snat(snat);
243                         continue;
244                 }
245
246                 if (list_empty(&snat->proto))
247                 {
248                         warn_elem(e, "does not specify a protocol, assuming all");
249                         fw3_parse_protocol(&snat->proto, "all", true);
250                 }
251
252                 if (snat->_src)
253                         set(snat->_src->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
254         }
255 }
256
257 static void
258 append_chain(struct fw3_ipt_rule *r, struct fw3_snat *snat)
259 {
260         if (snat->_src)
261                 fw3_ipt_rule_append(r, "zone_%s_postrouting", snat->src.name);
262         else
263                 fw3_ipt_rule_append(r, "POSTROUTING");
264 }
265
266 static void
267 set_target(struct fw3_ipt_rule *r, struct fw3_snat *snat,
268            struct fw3_protocol *proto)
269 {
270         char buf[sizeof("255.255.255.255:65535-65535\0")];
271
272         if (snat->target == FW3_FLAG_SNAT)
273         {
274                 buf[0] = '\0';
275
276                 if (snat->ip_snat.set)
277                 {
278                         inet_ntop(AF_INET, &snat->ip_snat.address.v4, buf, sizeof(buf));
279                 }
280
281                 if (snat->port_snat.set && proto && !proto->any &&
282                     (proto->protocol == 6 || proto->protocol == 17 || proto->protocol == 1))
283                 {
284                         if (snat->port_snat.port_min == snat->port_snat.port_max)
285                                 sprintf(buf + strlen(buf), ":%u", snat->port_snat.port_min);
286                         else
287                                 sprintf(buf + strlen(buf), ":%u-%u",
288                                                 snat->port_snat.port_min, snat->port_snat.port_max);
289
290                         if (snat->connlimit_ports) {
291                                 char portcntbuf[6];
292                                 snprintf(portcntbuf, sizeof(portcntbuf), "%u",
293                                                 1 + snat->port_snat.port_max - snat->port_snat.port_min);
294
295                                 fw3_ipt_rule_addarg(r, false, "-m", "connlimit");
296                                 fw3_ipt_rule_addarg(r, false, "--connlimit-daddr", NULL);
297                                 fw3_ipt_rule_addarg(r, false, "--connlimit-upto", portcntbuf);
298                         }
299                 }
300
301                 fw3_ipt_rule_target(r, "SNAT");
302                 fw3_ipt_rule_addarg(r, false, "--to-source", buf);
303         }
304         else if (snat->target == FW3_FLAG_ACCEPT)
305         {
306                 fw3_ipt_rule_target(r, "ACCEPT");
307         }
308         else
309         {
310                 fw3_ipt_rule_target(r, "MASQUERADE");
311         }
312 }
313
314 static void
315 set_comment(struct fw3_ipt_rule *r, const char *name, int num)
316 {
317         if (name)
318                 fw3_ipt_rule_comment(r, name);
319         else
320                 fw3_ipt_rule_comment(r, "@nat[%u]", num);
321 }
322
323 static void
324 print_snat(struct fw3_ipt_handle *h, struct fw3_state *state,
325            struct fw3_snat *snat, int num, struct fw3_protocol *proto)
326 {
327         struct fw3_ipt_rule *r;
328         struct fw3_address *src, *dst;
329         struct fw3_port *spt, *dpt;
330
331         switch (h->table)
332         {
333         case FW3_TABLE_NAT:
334                 src = &snat->ip_src;
335                 dst = &snat->ip_dest;
336                 spt = &snat->port_src;
337                 dpt = &snat->port_dest;
338
339                 r = fw3_ipt_rule_create(h, proto, NULL, NULL, src, dst);
340                 fw3_ipt_rule_sport_dport(r, spt, dpt);
341                 fw3_ipt_rule_device(r, snat->device, true);
342                 fw3_ipt_rule_ipset(r, &snat->ipset);
343                 fw3_ipt_rule_limit(r, &snat->limit);
344                 fw3_ipt_rule_time(r, &snat->time);
345                 fw3_ipt_rule_mark(r, &snat->mark);
346                 set_target(r, snat, proto);
347                 fw3_ipt_rule_extra(r, snat->extra);
348                 set_comment(r, snat->name, num);
349                 append_chain(r, snat);
350                 break;
351
352         default:
353                 break;
354         }
355 }
356
357 static void
358 expand_snat(struct fw3_ipt_handle *handle, struct fw3_state *state,
359                 struct fw3_snat *snat, int num)
360 {
361         struct fw3_protocol *proto;
362
363         if (snat->name)
364                 info("   * NAT '%s'", snat->name);
365         else
366                 info("   * NAT #%u", num);
367
368         if (!fw3_is_family(snat->_src, handle->family))
369         {
370                 info("     ! Skipping due to different family of zone");
371                 return;
372         }
373
374         if (!fw3_is_family(&snat->ip_src, handle->family) ||
375             !fw3_is_family(&snat->ip_dest, handle->family) ||
376                 !fw3_is_family(&snat->ip_snat, handle->family))
377         {
378                 if (!snat->ip_src.resolved ||
379                     !snat->ip_dest.resolved ||
380                     !snat->ip_snat.resolved)
381                         info("     ! Skipping due to different family of ip address");
382
383                 return;
384         }
385
386         if (snat->ipset.ptr)
387         {
388                 if (!fw3_is_family(snat->ipset.ptr, handle->family))
389                 {
390                         info("     ! Skipping due to different family in ipset");
391                         return;
392                 }
393
394                 if (!fw3_check_ipset(snat->ipset.ptr))
395                 {
396                         info("     ! Skipping due to missing ipset '%s'",
397                              snat->ipset.ptr->external ?
398                                         snat->ipset.ptr->external : snat->ipset.ptr->name);
399                         return;
400                 }
401
402                 set(snat->ipset.ptr->flags, handle->family, handle->family);
403         }
404
405         fw3_foreach(proto, &snat->proto)
406                 print_snat(handle, state, snat, num, proto);
407 }
408
409 void
410 fw3_print_snats(struct fw3_ipt_handle *handle, struct fw3_state *state)
411 {
412         int num = 0;
413         struct fw3_snat *snat;
414
415         if (handle->family == FW3_FAMILY_V6)
416                 return;
417
418         if (handle->table != FW3_TABLE_NAT)
419                 return;
420
421         list_for_each_entry(snat, &state->snats, list)
422                 expand_snat(handle, state, snat, num++);
423 }