f41153c693f35e509a3c531e2a7afa437ae67ddf
[project/firewall3.git] / options.c
1 /*
2  * firewall3 - 3rd OpenWrt UCI firewall implementation
3  *
4  *   Copyright (C) 2013-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 "options.h"
20 #include "ubus.h"
21
22
23 static bool
24 put_value(void *ptr, void *val, int elem_size, bool is_list)
25 {
26         void *copy;
27
28         if (is_list)
29         {
30                 copy = malloc(elem_size);
31
32                 if (!copy)
33                         return false;
34
35                 memcpy(copy, val, elem_size);
36                 list_add_tail((struct list_head *)copy, (struct list_head *)ptr);
37                 return true;
38         }
39
40         memcpy(ptr, val, elem_size);
41         return false;
42 }
43
44 static bool
45 parse_enum(void *ptr, const char *val, const char **values, int min, int max)
46 {
47         int i, l = strlen(val);
48
49         if (l > 0)
50         {
51                 for (i = 0; i <= (max - min); i++)
52                 {
53                         if (!strncasecmp(val, values[i], l))
54                         {
55                                 *((int *)ptr) = min + i;
56                                 return true;
57                         }
58                 }
59         }
60
61         return false;
62 }
63
64
65 const char *fw3_flag_names[__FW3_FLAG_MAX] = {
66         "filter",
67         "nat",
68         "mangle",
69         "raw",
70
71         "IPv4",
72         "IPv6",
73
74         "ACCEPT",
75         "REJECT",
76         "DROP",
77         "NOTRACK",
78         "MARK",
79         "DNAT",
80         "SNAT",
81         "MASQUERADE",
82
83         "ACCEPT",
84         "REJECT",
85         "DROP",
86 };
87
88 const char *fw3_limit_units[__FW3_LIMIT_UNIT_MAX] = {
89         "second",
90         "minute",
91         "hour",
92         "day",
93 };
94
95 const char *fw3_ipset_method_names[__FW3_IPSET_METHOD_MAX] = {
96         "(bug)",
97         "bitmap",
98         "hash",
99         "list",
100 };
101
102 const char *fw3_ipset_type_names[__FW3_IPSET_TYPE_MAX] = {
103         "(bug)",
104         "ip",
105         "port",
106         "mac",
107         "net",
108         "set",
109 };
110
111 static const char *weekdays[] = {
112         "monday",
113         "tuesday",
114         "wednesday",
115         "thursday",
116         "friday",
117         "saturday",
118         "sunday",
119 };
120
121 static const char *include_types[] = {
122         "script",
123         "restore",
124 };
125
126 static const char *reflection_sources[] = {
127         "internal",
128         "external",
129 };
130
131
132 bool
133 fw3_parse_bool(void *ptr, const char *val, bool is_list)
134 {
135         if (!strcmp(val, "true") || !strcmp(val, "yes") || !strcmp(val, "1"))
136                 *((bool *)ptr) = true;
137         else
138                 *((bool *)ptr) = false;
139
140         return true;
141 }
142
143 bool
144 fw3_parse_int(void *ptr, const char *val, bool is_list)
145 {
146         char *e;
147         int n = strtol(val, &e, 0);
148
149         if (e == val || *e)
150                 return false;
151
152         *((int *)ptr) = n;
153
154         return true;
155 }
156
157 bool
158 fw3_parse_string(void *ptr, const char *val, bool is_list)
159 {
160         *((char **)ptr) = (char *)val;
161         return true;
162 }
163
164 bool
165 fw3_parse_target(void *ptr, const char *val, bool is_list)
166 {
167         return parse_enum(ptr, val, &fw3_flag_names[FW3_FLAG_ACCEPT],
168                           FW3_FLAG_ACCEPT, FW3_FLAG_MASQUERADE);
169 }
170
171 bool
172 fw3_parse_limit(void *ptr, const char *val, bool is_list)
173 {
174         struct fw3_limit *limit = ptr;
175         enum fw3_limit_unit u = FW3_LIMIT_UNIT_SECOND;
176         char *e;
177         int n;
178
179         if (*val == '!')
180         {
181                 limit->invert = true;
182                 while (isspace(*++val));
183         }
184
185         n = strtol(val, &e, 10);
186
187         if (errno == ERANGE || errno == EINVAL)
188                 return false;
189
190         if (*e && *e++ != '/')
191                 return false;
192
193         if (!strlen(e))
194                 return false;
195
196         if (!parse_enum(&u, e, fw3_limit_units, 0, FW3_LIMIT_UNIT_DAY))
197                 return false;
198
199         limit->rate = n;
200         limit->unit = u;
201
202         return true;
203 }
204
205 bool
206 fw3_parse_device(void *ptr, const char *val, bool is_list)
207 {
208         char *p;
209         struct fw3_device dev = { };
210
211         if (*val == '*')
212         {
213                 dev.set = true;
214                 dev.any = true;
215                 put_value(ptr, &dev, sizeof(dev), is_list);
216                 return true;
217         }
218
219         if (*val == '!')
220         {
221                 dev.invert = true;
222                 while (isspace(*++val));
223         }
224
225         if ((p = strchr(val, '@')) != NULL)
226         {
227                 *p++ = 0;
228                 snprintf(dev.network, sizeof(dev.network), "%s", p);
229         }
230
231         if (*val)
232                 snprintf(dev.name, sizeof(dev.name), "%s", val);
233         else
234                 return false;
235
236         dev.set = true;
237         put_value(ptr, &dev, sizeof(dev), is_list);
238         return true;
239 }
240
241 bool
242 fw3_parse_address(void *ptr, const char *val, bool is_list)
243 {
244         struct fw3_address addr = { };
245         struct in_addr v4;
246         struct in6_addr v6;
247         char *p = NULL, *m = NULL, *s, *e;
248         int bits = -1;
249
250         if (*val == '!')
251         {
252                 addr.invert = true;
253                 while (isspace(*++val));
254         }
255
256         s = strdup(val);
257
258         if (!s)
259                 return false;
260
261         if ((m = strchr(s, '/')) != NULL)
262                 *m++ = 0;
263         else if ((p = strchr(s, '-')) != NULL)
264                 *p++ = 0;
265
266         if (inet_pton(AF_INET6, s, &v6))
267         {
268                 addr.family = FW3_FAMILY_V6;
269                 addr.address.v6 = v6;
270
271                 if (m)
272                 {
273                         if (!inet_pton(AF_INET6, m, &v6))
274                         {
275                                 bits = strtol(m, &e, 10);
276
277                                 if ((*e != 0) || !fw3_bitlen2netmask(addr.family, bits, &v6))
278                                         goto fail;
279                         }
280
281                         addr.mask.v6 = v6;
282                 }
283                 else if (p)
284                 {
285                         if (!inet_pton(AF_INET6, p, &addr.mask.v6))
286                                 goto fail;
287
288                         addr.range = true;
289                 }
290                 else
291                 {
292                         memset(addr.mask.v6.s6_addr, 0xFF, 16);
293                 }
294         }
295         else if (inet_pton(AF_INET, s, &v4))
296         {
297                 addr.family = FW3_FAMILY_V4;
298                 addr.address.v4 = v4;
299
300                 if (m)
301                 {
302                         if (!inet_pton(AF_INET, m, &v4))
303                         {
304                                 bits = strtol(m, &e, 10);
305
306                                 if ((*e != 0) || !fw3_bitlen2netmask(addr.family, bits, &v4))
307                                         goto fail;
308                         }
309
310                         addr.mask.v4 = v4;
311                 }
312                 else if (p)
313                 {
314                         if (!inet_pton(AF_INET, p, &addr.mask.v4))
315                                 goto fail;
316
317                         addr.range = true;
318                 }
319                 else
320                 {
321                         addr.mask.v4.s_addr = 0xFFFFFFFF;
322                 }
323         }
324         else
325         {
326                 goto fail;
327         }
328
329         free(s);
330         addr.set = true;
331         put_value(ptr, &addr, sizeof(addr), is_list);
332         return true;
333
334 fail:
335         free(s);
336         return false;
337 }
338
339 bool
340 fw3_parse_network(void *ptr, const char *val, bool is_list)
341 {
342         struct fw3_device dev = { };
343         struct fw3_address *addr, *tmp;
344         LIST_HEAD(addr_list);
345
346         if (!fw3_parse_address(ptr, val, is_list))
347         {
348                 if (!fw3_parse_device(&dev, val, false))
349                         return false;
350
351                 fw3_ubus_address(&addr_list, dev.name);
352                 list_for_each_entry(addr, &addr_list, list)
353                 {
354                         addr->invert = dev.invert;
355                         addr->resolved = true;
356                 }
357
358                 if (is_list)
359                 {
360                         list_splice_tail(&addr_list, ptr);
361                 }
362                 else if (!list_empty(&addr_list))
363                 {
364                         memcpy(ptr, list_first_entry(&addr_list, typeof(*addr), list),
365                                sizeof(*addr));
366
367                         list_for_each_entry_safe(addr, tmp, &addr_list, list)
368                                 free(addr);
369                 }
370         }
371
372         return true;
373 }
374
375 bool
376 fw3_parse_mac(void *ptr, const char *val, bool is_list)
377 {
378         struct fw3_mac addr = { };
379         struct ether_addr *mac;
380
381         if (*val == '!')
382         {
383                 addr.invert = true;
384                 while (isspace(*++val));
385         }
386
387         if ((mac = ether_aton(val)) != NULL)
388         {
389                 addr.mac = *mac;
390                 addr.set = true;
391
392                 put_value(ptr, &addr, sizeof(addr), is_list);
393                 return true;
394         }
395
396         return false;
397 }
398
399 bool
400 fw3_parse_port(void *ptr, const char *val, bool is_list)
401 {
402         struct fw3_port range = { };
403         uint16_t n;
404         uint16_t m;
405         char *p;
406
407         if (*val == '!')
408         {
409                 range.invert = true;
410                 while (isspace(*++val));
411         }
412
413         n = strtoul(val, &p, 10);
414
415         if (errno == ERANGE || errno == EINVAL)
416                 return false;
417
418         if (*p && *p != '-' && *p != ':')
419                 return false;
420
421         if (*p)
422         {
423                 m = strtoul(++p, NULL, 10);
424
425                 if (errno == ERANGE || errno == EINVAL || m < n)
426                         return false;
427
428                 range.port_min = n;
429                 range.port_max = m;
430         }
431         else
432         {
433                 range.port_min = n;
434                 range.port_max = n;
435         }
436
437         range.set = true;
438         put_value(ptr, &range, sizeof(range), is_list);
439         return true;
440 }
441
442 bool
443 fw3_parse_family(void *ptr, const char *val, bool is_list)
444 {
445         if (!strcmp(val, "any") || !strcmp(val, "*"))
446                 *((enum fw3_family *)ptr) = FW3_FAMILY_ANY;
447         else if (!strcmp(val, "inet") || strrchr(val, '4'))
448                 *((enum fw3_family *)ptr) = FW3_FAMILY_V4;
449         else if (!strcmp(val, "inet6") || strrchr(val, '6'))
450                 *((enum fw3_family *)ptr) = FW3_FAMILY_V6;
451         else
452                 return false;
453
454         return true;
455 }
456
457 bool
458 fw3_parse_icmptype(void *ptr, const char *val, bool is_list)
459 {
460         struct fw3_icmptype icmp = { };
461         bool v4 = false;
462         bool v6 = false;
463         char *p;
464         int i;
465
466         for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v4); i++)
467         {
468                 if (!strcmp(val, fw3_icmptype_list_v4[i].name))
469                 {
470                         icmp.type     = fw3_icmptype_list_v4[i].type;
471                         icmp.code_min = fw3_icmptype_list_v4[i].code_min;
472                         icmp.code_max = fw3_icmptype_list_v4[i].code_max;
473
474                         v4 = true;
475                         break;
476                 }
477         }
478
479         for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v6); i++)
480         {
481                 if (!strcmp(val, fw3_icmptype_list_v6[i].name))
482                 {
483                         icmp.type6     = fw3_icmptype_list_v6[i].type;
484                         icmp.code6_min = fw3_icmptype_list_v6[i].code_min;
485                         icmp.code6_max = fw3_icmptype_list_v6[i].code_max;
486
487                         v6 = true;
488                         break;
489                 }
490         }
491
492         if (!v4 && !v6)
493         {
494                 i = strtoul(val, &p, 10);
495
496                 if ((p == val) || (*p != '/' && *p != 0) || (i > 0xFF))
497                         return false;
498
499                 icmp.type = i;
500
501                 if (*p == '/')
502                 {
503                         val = ++p;
504                         i = strtoul(val, &p, 10);
505
506                         if ((p == val) || (*p != 0) || (i > 0xFF))
507                                 return false;
508
509                         icmp.code_min = i;
510                         icmp.code_max = i;
511                 }
512                 else
513                 {
514                         icmp.code_min = 0;
515                         icmp.code_max = 0xFF;
516                 }
517
518                 icmp.type6     = icmp.type;
519                 icmp.code6_min = icmp.code_max;
520                 icmp.code6_max = icmp.code_max;
521
522                 v4 = true;
523                 v6 = true;
524         }
525
526         icmp.family = (v4 && v6) ? FW3_FAMILY_ANY
527                                  : (v6 ? FW3_FAMILY_V6 : FW3_FAMILY_V4);
528
529         put_value(ptr, &icmp, sizeof(icmp), is_list);
530         return true;
531 }
532
533 bool
534 fw3_parse_protocol(void *ptr, const char *val, bool is_list)
535 {
536         struct fw3_protocol proto = { };
537         struct protoent *ent;
538         char *e;
539
540         if (*val == '!')
541         {
542                 proto.invert = true;
543                 while (isspace(*++val));
544         }
545
546         if (!strcmp(val, "all") || !strcmp(val, "any") || !strcmp(val, "*"))
547         {
548                 proto.any = true;
549                 put_value(ptr, &proto, sizeof(proto), is_list);
550                 return true;
551         }
552         else if (!strcmp(val, "icmpv6"))
553         {
554                 val = "ipv6-icmp";
555         }
556         else if (!strcmp(val, "tcpudp"))
557         {
558                 proto.protocol = 6;
559                 if (put_value(ptr, &proto, sizeof(proto), is_list))
560                 {
561                         proto.protocol = 17;
562                         put_value(ptr, &proto, sizeof(proto), is_list);
563                 }
564
565                 return true;
566         }
567
568         ent = getprotobyname(val);
569
570         if (ent)
571         {
572                 proto.protocol = ent->p_proto;
573                 put_value(ptr, &proto, sizeof(proto), is_list);
574                 return true;
575         }
576
577         proto.protocol = strtoul(val, &e, 10);
578
579         if ((e == val) || (*e != 0))
580                 return false;
581
582         put_value(ptr, &proto, sizeof(proto), is_list);
583         return true;
584 }
585
586 bool
587 fw3_parse_ipset_method(void *ptr, const char *val, bool is_list)
588 {
589         return parse_enum(ptr, val, &fw3_ipset_method_names[FW3_IPSET_METHOD_BITMAP],
590                           FW3_IPSET_METHOD_BITMAP, FW3_IPSET_METHOD_LIST);
591 }
592
593 bool
594 fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list)
595 {
596         struct fw3_ipset_datatype type = { };
597
598         type.dir = "src";
599
600         if (!strncmp(val, "dest_", 5))
601         {
602                 val += 5;
603                 type.dir = "dst";
604         }
605         else if (!strncmp(val, "dst_", 4))
606         {
607                 val += 4;
608                 type.dir = "dst";
609         }
610         else if (!strncmp(val, "src_", 4))
611         {
612                 val += 4;
613                 type.dir = "src";
614         }
615
616         if (parse_enum(&type.type, val, &fw3_ipset_type_names[FW3_IPSET_TYPE_IP],
617                        FW3_IPSET_TYPE_IP, FW3_IPSET_TYPE_SET))
618         {
619                 put_value(ptr, &type, sizeof(type), is_list);
620                 return true;
621         }
622
623         return false;
624 }
625
626 bool
627 fw3_parse_date(void *ptr, const char *val, bool is_list)
628 {
629         unsigned int year = 1970, mon = 1, day = 1, hour = 0, min = 0, sec = 0;
630         struct tm tm = { 0 };
631         char *p;
632
633         year = strtoul(val, &p, 10);
634         if ((*p != '-' && *p) || year < 1970 || year > 2038)
635                 goto fail;
636         else if (!*p)
637                 goto ret;
638
639         mon = strtoul(++p, &p, 10);
640         if ((*p != '-' && *p) || mon > 12)
641                 goto fail;
642         else if (!*p)
643                 goto ret;
644
645         day = strtoul(++p, &p, 10);
646         if ((*p != 'T' && *p) || day > 31)
647                 goto fail;
648         else if (!*p)
649                 goto ret;
650
651         hour = strtoul(++p, &p, 10);
652         if ((*p != ':' && *p) || hour > 23)
653                 goto fail;
654         else if (!*p)
655                 goto ret;
656
657         min = strtoul(++p, &p, 10);
658         if ((*p != ':' && *p) || min > 59)
659                 goto fail;
660         else if (!*p)
661                 goto ret;
662
663         sec = strtoul(++p, &p, 10);
664         if (*p || sec > 59)
665                 goto fail;
666
667 ret:
668         tm.tm_year = year - 1900;
669         tm.tm_mon  = mon - 1;
670         tm.tm_mday = day;
671         tm.tm_hour = hour;
672         tm.tm_min  = min;
673         tm.tm_sec  = sec;
674
675         if (mktime(&tm) >= 0)
676         {
677                 *((struct tm *)ptr) = tm;
678                 return true;
679         }
680
681 fail:
682         return false;
683 }
684
685 bool
686 fw3_parse_time(void *ptr, const char *val, bool is_list)
687 {
688         unsigned int hour = 0, min = 0, sec = 0;
689         char *p;
690
691         hour = strtoul(val, &p, 10);
692         if (*p != ':' || hour > 23)
693                 goto fail;
694
695         min = strtoul(++p, &p, 10);
696         if ((*p != ':' && *p) || min > 59)
697                 goto fail;
698         else if (!*p)
699                 goto ret;
700
701         sec = strtoul(++p, &p, 10);
702         if (*p || sec > 59)
703                 goto fail;
704
705 ret:
706         *((int *)ptr) = 60 * 60 * hour + 60 * min + sec;
707         return true;
708
709 fail:
710         return false;
711 }
712
713 bool
714 fw3_parse_weekdays(void *ptr, const char *val, bool is_list)
715 {
716         unsigned int w = 0;
717         char *p, *s;
718
719         if (*val == '!')
720         {
721                 setbit(*(uint8_t *)ptr, 0);
722                 while (isspace(*++val));
723         }
724
725         if (!(s = strdup(val)))
726                 return false;
727
728         for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
729         {
730                 if (!parse_enum(&w, p, weekdays, 1, 7))
731                 {
732                         w = strtoul(p, &p, 10);
733
734                         if (*p || w < 1 || w > 7)
735                         {
736                                 free(s);
737                                 return false;
738                         }
739                 }
740
741                 setbit(*(uint8_t *)ptr, w);
742         }
743
744         free(s);
745         return true;
746 }
747
748 bool
749 fw3_parse_monthdays(void *ptr, const char *val, bool is_list)
750 {
751         unsigned int d;
752         char *p, *s;
753
754         if (*val == '!')
755         {
756                 setbit(*(uint32_t *)ptr, 0);
757                 while (isspace(*++val));
758         }
759
760         if (!(s = strdup(val)))
761                 return false;
762
763         for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
764         {
765                 d = strtoul(p, &p, 10);
766
767                 if (*p || d < 1 || d > 31)
768                 {
769                         free(s);
770                         return false;
771                 }
772
773                 setbit(*(uint32_t *)ptr, d);
774         }
775
776         free(s);
777         return true;
778 }
779
780 bool
781 fw3_parse_include_type(void *ptr, const char *val, bool is_list)
782 {
783         return parse_enum(ptr, val, include_types,
784                           FW3_INC_TYPE_SCRIPT, FW3_INC_TYPE_RESTORE);
785 }
786
787 bool
788 fw3_parse_reflection_source(void *ptr, const char *val, bool is_list)
789 {
790         return parse_enum(ptr, val, reflection_sources,
791                           FW3_REFLECTION_INTERNAL, FW3_REFLECTION_EXTERNAL);
792 }
793
794 bool
795 fw3_parse_mark(void *ptr, const char *val, bool is_list)
796 {
797         uint32_t n;
798         char *s, *e;
799         struct fw3_mark *m = ptr;
800
801         if (*val == '!')
802         {
803                 m->invert = true;
804                 while (isspace(*++val));
805         }
806
807         if ((s = strchr(val, '/')) != NULL)
808                 *s++ = 0;
809
810         n = strtoul(val, &e, 0);
811
812         if (e == val || *e)
813                 return false;
814
815         m->mark = n;
816         m->mask = 0xFFFFFFFF;
817
818         if (s)
819         {
820                 n = strtoul(s, &e, 0);
821
822                 if (e == s || *e)
823                         return false;
824
825                 m->mask = n;
826         }
827
828         m->set = true;
829         return true;
830 }
831
832 bool
833 fw3_parse_setmatch(void *ptr, const char *val, bool is_list)
834 {
835         struct fw3_setmatch *m = ptr;
836         char *p, *s;
837         int i;
838
839         if (*val == '!')
840         {
841                 m->invert = true;
842                 while (isspace(*++val));
843         }
844
845         if (!(s = strdup(val)))
846                 return false;
847
848         if (!(p = strtok(s, " \t")))
849         {
850                 free(s);
851                 return false;
852         }
853
854         strncpy(m->name, p, sizeof(m->name));
855
856         for (i = 0, p = strtok(NULL, " \t,");
857              i < 3 && p != NULL;
858              i++, p = strtok(NULL, " \t,"))
859         {
860                 if (!strncmp(p, "dest", 4) || !strncmp(p, "dst", 3))
861                         m->dir[i] = "dst";
862                 else if (!strncmp(p, "src", 3))
863                         m->dir[i] = "src";
864         }
865
866         free(s);
867
868         m->set = true;
869         return true;
870 }
871
872 bool
873 fw3_parse_direction(void *ptr, const char *val, bool is_list)
874 {
875         bool *is_out = ptr;
876         bool valid = true;
877
878         if (!strcmp(val, "in") || !strcmp(val, "ingress"))
879                 *is_out = false;
880         else if (!strcmp(val, "out") || !strcmp(val, "egress"))
881                 *is_out = true;
882         else
883                 valid = false;
884
885         return valid;
886 }
887
888
889 bool
890 fw3_parse_options(void *s, const struct fw3_option *opts,
891                   struct uci_section *section)
892 {
893         char *p, *v;
894         bool known;
895         struct uci_element *e, *l;
896         struct uci_option *o;
897         const struct fw3_option *opt;
898         struct list_head *dest;
899         bool valid = true;
900
901         uci_foreach_element(&section->options, e)
902         {
903                 o = uci_to_option(e);
904                 known = false;
905
906                 for (opt = opts; opt->name; opt++)
907                 {
908                         if (!opt->parse)
909                                 continue;
910
911                         if (strcmp(opt->name, e->name))
912                                 continue;
913
914                         if (o->type == UCI_TYPE_LIST)
915                         {
916                                 if (!opt->elem_size)
917                                 {
918                                         warn_elem(e, "must not be a list");
919                                         valid = false;
920                                 }
921                                 else
922                                 {
923                                         dest = (struct list_head *)((char *)s + opt->offset);
924
925                                         uci_foreach_element(&o->v.list, l)
926                                         {
927                                                 if (!l->name)
928                                                         continue;
929
930                                                 if (!opt->parse(dest, l->name, true))
931                                                 {
932                                                         warn_elem(e, "has invalid value '%s'", l->name);
933                                                         valid = false;
934                                                         continue;
935                                                 }
936                                         }
937                                 }
938                         }
939                         else
940                         {
941                                 v = o->v.string;
942
943                                 if (!v)
944                                         continue;
945
946                                 if (!opt->elem_size)
947                                 {
948                                         if (!opt->parse((char *)s + opt->offset, o->v.string, false))
949                                         {
950                                                 warn_elem(e, "has invalid value '%s'", o->v.string);
951                                                 valid = false;
952                                         }
953                                 }
954                                 else
955                                 {
956                                         dest = (struct list_head *)((char *)s + opt->offset);
957
958                                         for (p = strtok(v, " \t"); p != NULL; p = strtok(NULL, " \t"))
959                                         {
960                                                 if (!opt->parse(dest, p, true))
961                                                 {
962                                                         warn_elem(e, "has invalid value '%s'", p);
963                                                         valid = false;
964                                                         continue;
965                                                 }
966                                         }
967                                 }
968                         }
969
970                         known = true;
971                         break;
972                 }
973
974                 if (!known)
975                         warn_elem(e, "is unknown");
976         }
977
978         return valid;
979 }
980
981
982 bool
983 fw3_parse_blob_options(void *s, const struct fw3_option *opts,
984                   struct blob_attr *a)
985 {
986         char *p, *v, buf[16];
987         bool known;
988         unsigned rem, erem;
989         struct blob_attr *o, *e;
990         const struct fw3_option *opt;
991         struct list_head *dest;
992         bool valid = true;
993
994         blobmsg_for_each_attr(o, a, rem)
995         {
996                 known = false;
997
998                 for (opt = opts; opt->name; opt++)
999                 {
1000                         if (!opt->parse)
1001                                 continue;
1002
1003                         if (strcmp(opt->name, blobmsg_name(o)))
1004                                 continue;
1005
1006                         if (blobmsg_type(o) == BLOBMSG_TYPE_ARRAY)
1007                         {
1008                                 if (!opt->elem_size)
1009                                 {
1010                                         fprintf(stderr, "%s must not be a list\n", opt->name);
1011                                         valid = false;
1012                                 }
1013                                 else
1014                                 {
1015                                         dest = (struct list_head *)((char *)s + opt->offset);
1016
1017                                         blobmsg_for_each_attr(e, o, erem)
1018                                         {
1019                                                 if (blobmsg_type(e) == BLOBMSG_TYPE_INT32) {
1020                                                         snprintf(buf, sizeof(buf), "%d", blobmsg_get_u32(e));
1021                                                         v = buf;
1022                                                 } else {
1023                                                         v = blobmsg_get_string(e);
1024                                                 }
1025
1026                                                 if (!opt->parse(dest, v, true))
1027                                                 {
1028                                                         fprintf(stderr, "%s has invalid value '%s'\n", opt->name, v);
1029                                                         valid = false;
1030                                                         continue;
1031                                                 }
1032                                         }
1033                                 }
1034                         }
1035                         else
1036                         {
1037                                 if (blobmsg_type(o) == BLOBMSG_TYPE_INT32) {
1038                                         snprintf(buf, sizeof(buf), "%d", blobmsg_get_u32(o));
1039                                         v = buf;
1040                                 } else {
1041                                         v = blobmsg_get_string(o);
1042                                 }
1043
1044                                 if (!v)
1045                                         continue;
1046
1047                                 if (!opt->elem_size)
1048                                 {
1049                                         if (!opt->parse((char *)s + opt->offset, v, false))
1050                                         {
1051                                                 fprintf(stderr, "%s has invalid value '%s'\n", opt->name, v);
1052                                                 valid = false;
1053                                         }
1054                                 }
1055                                 else
1056                                 {
1057                                         dest = (struct list_head *)((char *)s + opt->offset);
1058
1059                                         for (p = strtok(v, " \t"); p != NULL; p = strtok(NULL, " \t"))
1060                                         {
1061                                                 if (!opt->parse(dest, p, true))
1062                                                 {
1063                                                         fprintf(stderr, "%s has invalid value '%s'\n", opt->name, p);
1064                                                         valid = false;
1065                                                         continue;
1066                                                 }
1067                                         }
1068                                 }
1069                         }
1070
1071                         known = true;
1072                         break;
1073                 }
1074
1075                 if (!known)
1076                         fprintf(stderr, "%s is unknown\n", blobmsg_name(o));
1077         }
1078
1079         return valid;
1080 }
1081
1082
1083 const char *
1084 fw3_address_to_string(struct fw3_address *address, bool allow_invert, bool as_cidr)
1085 {
1086         char *p, ip[INET6_ADDRSTRLEN];
1087         static char buf[INET6_ADDRSTRLEN * 2 + 2];
1088
1089         p = buf;
1090
1091         if (address->invert && allow_invert)
1092                 p += sprintf(p, "!");
1093
1094         inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
1095                   &address->address.v4, ip, sizeof(ip));
1096
1097         p += sprintf(p, "%s", ip);
1098
1099         if (address->range)
1100         {
1101                 inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
1102                           &address->mask.v4, ip, sizeof(ip));
1103
1104                 p += sprintf(p, "-%s", ip);
1105         }
1106         else if (!as_cidr)
1107         {
1108                 inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
1109                           &address->mask.v4, ip, sizeof(ip));
1110
1111                 p += sprintf(p, "/%s", ip);
1112         }
1113         else
1114         {
1115                 p += sprintf(p, "/%u", fw3_netmask2bitlen(address->family,
1116                                                           &address->mask.v6));
1117         }
1118
1119         return buf;
1120 }