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