ubus: handle attribute access after NULL check in parse_subnets()
[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, *s, *e;
248         int i, m = -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 ((p = strchr(s, '/')) != NULL)
262         {
263                 *p++ = 0;
264                 m = strtoul(p, &e, 10);
265
266                 if ((e == p) || (*e != 0))
267                 {
268                         if (strchr(s, ':') || !inet_pton(AF_INET, p, &v4))
269                         {
270                                 free(s);
271                                 return false;
272                         }
273
274                         for (i = 0, m = 32; !(v4.s_addr & 1) && (i < 32); i++)
275                         {
276                                 m--;
277                                 v4.s_addr >>= 1;
278                         }
279                 }
280         }
281         else if ((p = strchr(s, '-')) != NULL)
282         {
283                 *p++ = 0;
284
285                 if (inet_pton(AF_INET6, p, &v6))
286                 {
287                         addr.family = FW3_FAMILY_V6;
288                         addr.address2.v6 = v6;
289                         addr.range = true;
290                 }
291                 else if (inet_pton(AF_INET, p, &v4))
292                 {
293                         addr.family = FW3_FAMILY_V4;
294                         addr.address2.v4 = v4;
295                         addr.range = true;
296                 }
297                 else
298                 {
299                         free(s);
300                         return false;
301                 }
302         }
303
304         if (inet_pton(AF_INET6, s, &v6))
305         {
306                 addr.family = FW3_FAMILY_V6;
307                 addr.address.v6 = v6;
308                 addr.mask = (m >= 0) ? m : 128;
309         }
310         else if (inet_pton(AF_INET, s, &v4))
311         {
312                 addr.family = FW3_FAMILY_V4;
313                 addr.address.v4 = v4;
314                 addr.mask = (m >= 0) ? m : 32;
315         }
316         else
317         {
318                 free(s);
319                 return false;
320         }
321
322         free(s);
323         addr.set = true;
324         put_value(ptr, &addr, sizeof(addr), is_list);
325         return true;
326 }
327
328 bool
329 fw3_parse_network(void *ptr, const char *val, bool is_list)
330 {
331         struct fw3_device dev = { };
332         struct fw3_address *addr, *tmp;
333         LIST_HEAD(addr_list);
334
335         if (!fw3_parse_address(ptr, val, is_list))
336         {
337                 if (!fw3_parse_device(&dev, val, false))
338                         return false;
339
340                 fw3_ubus_address(&addr_list, dev.name);
341                 list_for_each_entry(addr, &addr_list, list)
342                 {
343                         addr->invert = dev.invert;
344                         addr->resolved = true;
345                 }
346
347                 if (is_list)
348                 {
349                         list_splice_tail(&addr_list, ptr);
350                 }
351                 else if (!list_empty(&addr_list))
352                 {
353                         memcpy(ptr, list_first_entry(&addr_list, typeof(*addr), list),
354                                sizeof(*addr));
355
356                         list_for_each_entry_safe(addr, tmp, &addr_list, list)
357                                 free(addr);
358                 }
359         }
360
361         return true;
362 }
363
364 bool
365 fw3_parse_mac(void *ptr, const char *val, bool is_list)
366 {
367         struct fw3_mac addr = { };
368         struct ether_addr *mac;
369
370         if (*val == '!')
371         {
372                 addr.invert = true;
373                 while (isspace(*++val));
374         }
375
376         if ((mac = ether_aton(val)) != NULL)
377         {
378                 addr.mac = *mac;
379                 addr.set = true;
380
381                 put_value(ptr, &addr, sizeof(addr), is_list);
382                 return true;
383         }
384
385         return false;
386 }
387
388 bool
389 fw3_parse_port(void *ptr, const char *val, bool is_list)
390 {
391         struct fw3_port range = { };
392         uint16_t n;
393         uint16_t m;
394         char *p;
395
396         if (*val == '!')
397         {
398                 range.invert = true;
399                 while (isspace(*++val));
400         }
401
402         n = strtoul(val, &p, 10);
403
404         if (errno == ERANGE || errno == EINVAL)
405                 return false;
406
407         if (*p && *p != '-' && *p != ':')
408                 return false;
409
410         if (*p)
411         {
412                 m = strtoul(++p, NULL, 10);
413
414                 if (errno == ERANGE || errno == EINVAL || m < n)
415                         return false;
416
417                 range.port_min = n;
418                 range.port_max = m;
419         }
420         else
421         {
422                 range.port_min = n;
423                 range.port_max = n;
424         }
425
426         range.set = true;
427         put_value(ptr, &range, sizeof(range), is_list);
428         return true;
429 }
430
431 bool
432 fw3_parse_family(void *ptr, const char *val, bool is_list)
433 {
434         if (!strcmp(val, "any"))
435                 *((enum fw3_family *)ptr) = FW3_FAMILY_ANY;
436         else if (!strcmp(val, "inet") || strrchr(val, '4'))
437                 *((enum fw3_family *)ptr) = FW3_FAMILY_V4;
438         else if (!strcmp(val, "inet6") || strrchr(val, '6'))
439                 *((enum fw3_family *)ptr) = FW3_FAMILY_V6;
440         else
441                 return false;
442
443         return true;
444 }
445
446 bool
447 fw3_parse_icmptype(void *ptr, const char *val, bool is_list)
448 {
449         struct fw3_icmptype icmp = { };
450         bool v4 = false;
451         bool v6 = false;
452         char *p;
453         int i;
454
455         for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v4); i++)
456         {
457                 if (!strcmp(val, fw3_icmptype_list_v4[i].name))
458                 {
459                         icmp.type     = fw3_icmptype_list_v4[i].type;
460                         icmp.code_min = fw3_icmptype_list_v4[i].code_min;
461                         icmp.code_max = fw3_icmptype_list_v4[i].code_max;
462
463                         v4 = true;
464                         break;
465                 }
466         }
467
468         for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v6); i++)
469         {
470                 if (!strcmp(val, fw3_icmptype_list_v6[i].name))
471                 {
472                         icmp.type6     = fw3_icmptype_list_v6[i].type;
473                         icmp.code6_min = fw3_icmptype_list_v6[i].code_min;
474                         icmp.code6_max = fw3_icmptype_list_v6[i].code_max;
475
476                         v6 = true;
477                         break;
478                 }
479         }
480
481         if (!v4 && !v6)
482         {
483                 i = strtoul(val, &p, 10);
484
485                 if ((p == val) || (*p != '/' && *p != 0) || (i > 0xFF))
486                         return false;
487
488                 icmp.type = i;
489
490                 if (*p == '/')
491                 {
492                         val = ++p;
493                         i = strtoul(val, &p, 10);
494
495                         if ((p == val) || (*p != 0) || (i > 0xFF))
496                                 return false;
497
498                         icmp.code_min = i;
499                         icmp.code_max = i;
500                 }
501                 else
502                 {
503                         icmp.code_min = 0;
504                         icmp.code_max = 0xFF;
505                 }
506
507                 icmp.type6     = icmp.type;
508                 icmp.code6_min = icmp.code_max;
509                 icmp.code6_max = icmp.code_max;
510
511                 v4 = true;
512                 v6 = true;
513         }
514
515         icmp.family = (v4 && v6) ? FW3_FAMILY_ANY
516                                  : (v6 ? FW3_FAMILY_V6 : FW3_FAMILY_V4);
517
518         put_value(ptr, &icmp, sizeof(icmp), is_list);
519         return true;
520 }
521
522 bool
523 fw3_parse_protocol(void *ptr, const char *val, bool is_list)
524 {
525         struct fw3_protocol proto = { };
526         struct protoent *ent;
527         char *e;
528
529         if (*val == '!')
530         {
531                 proto.invert = true;
532                 while (isspace(*++val));
533         }
534
535         if (!strcmp(val, "all"))
536         {
537                 proto.any = true;
538                 put_value(ptr, &proto, sizeof(proto), is_list);
539                 return true;
540         }
541         else if (!strcmp(val, "icmpv6"))
542         {
543                 val = "ipv6-icmp";
544         }
545         else if (!strcmp(val, "tcpudp"))
546         {
547                 proto.protocol = 6;
548                 if (put_value(ptr, &proto, sizeof(proto), is_list))
549                 {
550                         proto.protocol = 17;
551                         put_value(ptr, &proto, sizeof(proto), is_list);
552                 }
553
554                 return true;
555         }
556
557         ent = getprotobyname(val);
558
559         if (ent)
560         {
561                 proto.protocol = ent->p_proto;
562                 put_value(ptr, &proto, sizeof(proto), is_list);
563                 return true;
564         }
565
566         proto.protocol = strtoul(val, &e, 10);
567
568         if ((e == val) || (*e != 0))
569                 return false;
570
571         put_value(ptr, &proto, sizeof(proto), is_list);
572         return true;
573 }
574
575 bool
576 fw3_parse_ipset_method(void *ptr, const char *val, bool is_list)
577 {
578         return parse_enum(ptr, val, &fw3_ipset_method_names[FW3_IPSET_METHOD_BITMAP],
579                           FW3_IPSET_METHOD_BITMAP, FW3_IPSET_METHOD_LIST);
580 }
581
582 bool
583 fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list)
584 {
585         struct fw3_ipset_datatype type = { };
586
587         type.dir = "src";
588
589         if (!strncmp(val, "dest_", 5))
590         {
591                 val += 5;
592                 type.dir = "dst";
593         }
594         else if (!strncmp(val, "dst_", 4))
595         {
596                 val += 4;
597                 type.dir = "dst";
598         }
599         else if (!strncmp(val, "src_", 4))
600         {
601                 val += 4;
602                 type.dir = "src";
603         }
604
605         if (parse_enum(&type.type, val, &fw3_ipset_type_names[FW3_IPSET_TYPE_IP],
606                        FW3_IPSET_TYPE_IP, FW3_IPSET_TYPE_SET))
607         {
608                 put_value(ptr, &type, sizeof(type), is_list);
609                 return true;
610         }
611
612         return false;
613 }
614
615 bool
616 fw3_parse_date(void *ptr, const char *val, bool is_list)
617 {
618         unsigned int year = 1970, mon = 1, day = 1, hour = 0, min = 0, sec = 0;
619         struct tm tm = { 0 };
620         char *p;
621
622         year = strtoul(val, &p, 10);
623         if ((*p != '-' && *p) || year < 1970 || year > 2038)
624                 goto fail;
625         else if (!*p)
626                 goto ret;
627
628         mon = strtoul(++p, &p, 10);
629         if ((*p != '-' && *p) || mon > 12)
630                 goto fail;
631         else if (!*p)
632                 goto ret;
633
634         day = strtoul(++p, &p, 10);
635         if ((*p != 'T' && *p) || day > 31)
636                 goto fail;
637         else if (!*p)
638                 goto ret;
639
640         hour = strtoul(++p, &p, 10);
641         if ((*p != ':' && *p) || hour > 23)
642                 goto fail;
643         else if (!*p)
644                 goto ret;
645
646         min = strtoul(++p, &p, 10);
647         if ((*p != ':' && *p) || min > 59)
648                 goto fail;
649         else if (!*p)
650                 goto ret;
651
652         sec = strtoul(++p, &p, 10);
653         if (*p || sec > 59)
654                 goto fail;
655
656 ret:
657         tm.tm_year = year - 1900;
658         tm.tm_mon  = mon - 1;
659         tm.tm_mday = day;
660         tm.tm_hour = hour;
661         tm.tm_min  = min;
662         tm.tm_sec  = sec;
663
664         if (mktime(&tm) >= 0)
665         {
666                 *((struct tm *)ptr) = tm;
667                 return true;
668         }
669
670 fail:
671         return false;
672 }
673
674 bool
675 fw3_parse_time(void *ptr, const char *val, bool is_list)
676 {
677         unsigned int hour = 0, min = 0, sec = 0;
678         char *p;
679
680         hour = strtoul(val, &p, 10);
681         if (*p != ':' || hour > 23)
682                 goto fail;
683
684         min = strtoul(++p, &p, 10);
685         if ((*p != ':' && *p) || min > 59)
686                 goto fail;
687         else if (!*p)
688                 goto ret;
689
690         sec = strtoul(++p, &p, 10);
691         if (*p || sec > 59)
692                 goto fail;
693
694 ret:
695         *((int *)ptr) = 60 * 60 * hour + 60 * min + sec;
696         return true;
697
698 fail:
699         return false;
700 }
701
702 bool
703 fw3_parse_weekdays(void *ptr, const char *val, bool is_list)
704 {
705         unsigned int w = 0;
706         char *p, *s;
707
708         if (*val == '!')
709         {
710                 setbit(*(uint8_t *)ptr, 0);
711                 while (isspace(*++val));
712         }
713
714         if (!(s = strdup(val)))
715                 return false;
716
717         for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
718         {
719                 if (!parse_enum(&w, p, weekdays, 1, 7))
720                 {
721                         w = strtoul(p, &p, 10);
722
723                         if (*p || w < 1 || w > 7)
724                         {
725                                 free(s);
726                                 return false;
727                         }
728                 }
729
730                 setbit(*(uint8_t *)ptr, w);
731         }
732
733         free(s);
734         return true;
735 }
736
737 bool
738 fw3_parse_monthdays(void *ptr, const char *val, bool is_list)
739 {
740         unsigned int d;
741         char *p, *s;
742
743         if (*val == '!')
744         {
745                 setbit(*(uint32_t *)ptr, 0);
746                 while (isspace(*++val));
747         }
748
749         if (!(s = strdup(val)))
750                 return false;
751
752         for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
753         {
754                 d = strtoul(p, &p, 10);
755
756                 if (*p || d < 1 || d > 31)
757                 {
758                         free(s);
759                         return false;
760                 }
761
762                 setbit(*(uint32_t *)ptr, d);
763         }
764
765         free(s);
766         return true;
767 }
768
769 bool
770 fw3_parse_include_type(void *ptr, const char *val, bool is_list)
771 {
772         return parse_enum(ptr, val, include_types,
773                           FW3_INC_TYPE_SCRIPT, FW3_INC_TYPE_RESTORE);
774 }
775
776 bool
777 fw3_parse_reflection_source(void *ptr, const char *val, bool is_list)
778 {
779         return parse_enum(ptr, val, reflection_sources,
780                           FW3_REFLECTION_INTERNAL, FW3_REFLECTION_EXTERNAL);
781 }
782
783 bool
784 fw3_parse_mark(void *ptr, const char *val, bool is_list)
785 {
786         uint32_t n;
787         char *s, *e;
788         struct fw3_mark *m = ptr;
789
790         if (*val == '!')
791         {
792                 m->invert = true;
793                 while (isspace(*++val));
794         }
795
796         if ((s = strchr(val, '/')) != NULL)
797                 *s++ = 0;
798
799         n = strtoul(val, &e, 0);
800
801         if (e == val || *e)
802                 return false;
803
804         m->mark = n;
805         m->mask = 0xFFFFFFFF;
806
807         if (s)
808         {
809                 n = strtoul(s, &e, 0);
810
811                 if (e == s || *e)
812                         return false;
813
814                 m->mask = n;
815         }
816
817         m->set = true;
818         return true;
819 }
820
821 bool
822 fw3_parse_setmatch(void *ptr, const char *val, bool is_list)
823 {
824         struct fw3_setmatch *m = ptr;
825         char *p, *s;
826         int i;
827
828         if (*val == '!')
829         {
830                 m->invert = true;
831                 while (isspace(*++val));
832         }
833
834         if (!(s = strdup(val)))
835                 return false;
836
837         if (!(p = strtok(s, " \t")))
838         {
839                 free(s);
840                 return false;
841         }
842
843         strncpy(m->name, p, sizeof(m->name));
844
845         for (i = 0, p = strtok(NULL, " \t,");
846              i < 3 && p != NULL;
847              i++, p = strtok(NULL, " \t,"))
848         {
849                 if (!strncmp(p, "dest", 4) || !strncmp(p, "dst", 3))
850                         m->dir[i] = "dst";
851                 else if (!strncmp(p, "src", 3))
852                         m->dir[i] = "src";
853         }
854
855         free(s);
856
857         m->set = true;
858         return true;
859 }
860
861 bool
862 fw3_parse_direction(void *ptr, const char *val, bool is_list)
863 {
864         bool *is_out = ptr;
865         bool valid = true;
866
867         if (!strcmp(val, "in") || !strcmp(val, "ingress"))
868                 *is_out = false;
869         else if (!strcmp(val, "out") || !strcmp(val, "egress"))
870                 *is_out = true;
871         else
872                 valid = false;
873
874         return valid;
875 }
876
877
878 bool
879 fw3_parse_options(void *s, const struct fw3_option *opts,
880                   struct uci_section *section)
881 {
882         char *p, *v;
883         bool known;
884         struct uci_element *e, *l;
885         struct uci_option *o;
886         const struct fw3_option *opt;
887         struct list_head *dest;
888         bool valid = true;
889
890         uci_foreach_element(&section->options, e)
891         {
892                 o = uci_to_option(e);
893                 known = false;
894
895                 for (opt = opts; opt->name; opt++)
896                 {
897                         if (!opt->parse)
898                                 continue;
899
900                         if (strcmp(opt->name, e->name))
901                                 continue;
902
903                         if (o->type == UCI_TYPE_LIST)
904                         {
905                                 if (!opt->elem_size)
906                                 {
907                                         warn_elem(e, "must not be a list");
908                                         valid = false;
909                                 }
910                                 else
911                                 {
912                                         dest = (struct list_head *)((char *)s + opt->offset);
913
914                                         uci_foreach_element(&o->v.list, l)
915                                         {
916                                                 if (!l->name)
917                                                         continue;
918
919                                                 if (!opt->parse(dest, l->name, true))
920                                                 {
921                                                         warn_elem(e, "has invalid value '%s'", l->name);
922                                                         valid = false;
923                                                         continue;
924                                                 }
925                                         }
926                                 }
927                         }
928                         else
929                         {
930                                 v = o->v.string;
931
932                                 if (!v)
933                                         continue;
934
935                                 if (!opt->elem_size)
936                                 {
937                                         if (!opt->parse((char *)s + opt->offset, o->v.string, false))
938                                         {
939                                                 warn_elem(e, "has invalid value '%s'", o->v.string);
940                                                 valid = false;
941                                         }
942                                 }
943                                 else
944                                 {
945                                         dest = (struct list_head *)((char *)s + opt->offset);
946
947                                         for (p = strtok(v, " \t"); p != NULL; p = strtok(NULL, " \t"))
948                                         {
949                                                 if (!opt->parse(dest, p, true))
950                                                 {
951                                                         warn_elem(e, "has invalid value '%s'", p);
952                                                         valid = false;
953                                                         continue;
954                                                 }
955                                         }
956                                 }
957                         }
958
959                         known = true;
960                         break;
961                 }
962
963                 if (!known)
964                         warn_elem(e, "is unknown");
965         }
966
967         return valid;
968 }
969
970
971 bool
972 fw3_parse_blob_options(void *s, const struct fw3_option *opts,
973                   struct blob_attr *a)
974 {
975         char *p, *v, buf[16];
976         bool known;
977         unsigned rem, erem;
978         struct blob_attr *o, *e;
979         const struct fw3_option *opt;
980         struct list_head *dest;
981         bool valid = true;
982
983         blobmsg_for_each_attr(o, a, rem)
984         {
985                 known = false;
986
987                 for (opt = opts; opt->name; opt++)
988                 {
989                         if (!opt->parse)
990                                 continue;
991
992                         if (strcmp(opt->name, blobmsg_name(o)))
993                                 continue;
994
995                         if (blobmsg_type(o) == BLOBMSG_TYPE_ARRAY)
996                         {
997                                 if (!opt->elem_size)
998                                 {
999                                         fprintf(stderr, "%s must not be a list\n", opt->name);
1000                                         valid = false;
1001                                 }
1002                                 else
1003                                 {
1004                                         dest = (struct list_head *)((char *)s + opt->offset);
1005
1006                                         blobmsg_for_each_attr(e, o, erem)
1007                                         {
1008                                                 if (blobmsg_type(e) == BLOBMSG_TYPE_INT32) {
1009                                                         snprintf(buf, sizeof(buf), "%d", blobmsg_get_u32(e));
1010                                                         v = buf;
1011                                                 } else {
1012                                                         v = blobmsg_get_string(e);
1013                                                 }
1014
1015                                                 if (!opt->parse(dest, v, true))
1016                                                 {
1017                                                         fprintf(stderr, "%s has invalid value '%s'\n", opt->name, v);
1018                                                         valid = false;
1019                                                         continue;
1020                                                 }
1021                                         }
1022                                 }
1023                         }
1024                         else
1025                         {
1026                                 if (blobmsg_type(o) == BLOBMSG_TYPE_INT32) {
1027                                         snprintf(buf, sizeof(buf), "%d", blobmsg_get_u32(o));
1028                                         v = buf;
1029                                 } else {
1030                                         v = blobmsg_get_string(o);
1031                                 }
1032
1033                                 if (!v)
1034                                         continue;
1035
1036                                 if (!opt->elem_size)
1037                                 {
1038                                         if (!opt->parse((char *)s + opt->offset, v, false))
1039                                         {
1040                                                 fprintf(stderr, "%s has invalid value '%s'\n", opt->name, v);
1041                                                 valid = false;
1042                                         }
1043                                 }
1044                                 else
1045                                 {
1046                                         dest = (struct list_head *)((char *)s + opt->offset);
1047
1048                                         for (p = strtok(v, " \t"); p != NULL; p = strtok(NULL, " \t"))
1049                                         {
1050                                                 if (!opt->parse(dest, p, true))
1051                                                 {
1052                                                         fprintf(stderr, "%s has invalid value '%s'\n", opt->name, p);
1053                                                         valid = false;
1054                                                         continue;
1055                                                 }
1056                                         }
1057                                 }
1058                         }
1059
1060                         known = true;
1061                         break;
1062                 }
1063
1064                 if (!known)
1065                         fprintf(stderr, "%s is unknown\n", blobmsg_name(o));
1066         }
1067
1068         return valid;
1069 }
1070
1071
1072 const char *
1073 fw3_address_to_string(struct fw3_address *address, bool allow_invert)
1074 {
1075         char *p, ip[INET6_ADDRSTRLEN];
1076         static char buf[INET6_ADDRSTRLEN * 2 + 2];
1077
1078         p = buf;
1079
1080         if (address->invert && allow_invert)
1081                 p += sprintf(p, "!");
1082
1083         inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
1084                   &address->address.v4, ip, sizeof(ip));
1085
1086         p += sprintf(p, "%s", ip);
1087
1088         if (address->range)
1089         {
1090                 inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
1091                           &address->address2.v4, ip, sizeof(ip));
1092
1093                 p += sprintf(p, "-%s", ip);
1094         }
1095         else
1096         {
1097                 p += sprintf(p, "/%u", address->mask);
1098         }
1099
1100         return buf;
1101 }