[kernel] move lots of kernel related packages to the new system/ folder
[openwrt.git] / package / network / config / swconfig / src / uci.c
1 /*
2  * uci.c: UCI binding for the switch configuration utility
3  *
4  * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundatio.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <inttypes.h>
20 #include <errno.h>
21 #include <stdint.h>
22 #include <getopt.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <uci.h>
26
27 #include <linux/types.h>
28 #include <linux/netlink.h>
29 #include <linux/genetlink.h>
30 #include <netlink/netlink.h>
31 #include <netlink/genl/genl.h>
32 #include <netlink/genl/ctrl.h>
33 #include <linux/switch.h>
34 #include "swlib.h"
35
36 #ifndef ARRAY_SIZE
37 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
38 #endif
39
40 struct swlib_setting {
41         struct switch_attr *attr;
42         const char *name;
43         int port_vlan;
44         const char *val;
45         struct swlib_setting *next;
46 };
47
48 struct swlib_setting early_settings[] = {
49         { .name = "reset", .val = "1" },
50         { .name = "enable_vlan", .val = "1" },
51 };
52
53 static struct swlib_setting *settings;
54 static struct swlib_setting **head;
55
56 static bool swlib_match_name(struct switch_dev *dev, const char *name)
57 {
58         return (strcmp(name, dev->dev_name) == 0 ||
59                 strcmp(name, dev->alias) == 0);
60 }
61
62 static int
63 swlib_map_settings(struct switch_dev *dev, int type, int port_vlan, struct uci_section *s)
64 {
65         struct swlib_setting *setting;
66         struct switch_attr *attr;
67         struct uci_element *e;
68         struct uci_option *o;
69         int i;
70
71         uci_foreach_element(&s->options, e) {
72                 o = uci_to_option(e);
73
74                 if (o->type != UCI_TYPE_STRING)
75                         continue;
76
77                 if (!strcmp(e->name, "device"))
78                         continue;
79
80                 /* map early settings */
81                 if (type == SWLIB_ATTR_GROUP_GLOBAL) {
82                         int i;
83
84                         for (i = 0; i < ARRAY_SIZE(early_settings); i++) {
85                                 if (strcmp(e->name, early_settings[i].name) != 0)
86                                         continue;
87
88                                 early_settings[i].val = o->v.string;
89                                 goto skip;
90                         }
91                 }
92
93                 attr = swlib_lookup_attr(dev, type, e->name);
94                 if (!attr)
95                         continue;
96
97                 setting = malloc(sizeof(struct swlib_setting));
98                 memset(setting, 0, sizeof(struct swlib_setting));
99                 setting->attr = attr;
100                 setting->port_vlan = port_vlan;
101                 setting->val = o->v.string;
102                 *head = setting;
103                 head = &setting->next;
104 skip:
105                 continue;
106         }
107 }
108
109 int swlib_apply_from_uci(struct switch_dev *dev, struct uci_package *p)
110 {
111         struct switch_attr *attr;
112         struct uci_context *ctx = p->ctx;
113         struct uci_element *e;
114         struct uci_section *s;
115         struct uci_option *o;
116         struct uci_ptr ptr;
117         struct switch_val val;
118         int i;
119
120         settings = NULL;
121         head = &settings;
122
123         uci_foreach_element(&p->sections, e) {
124                 struct uci_element *n;
125
126                 s = uci_to_section(e);
127
128                 if (strcmp(s->type, "switch") != 0)
129                         continue;
130
131                 uci_foreach_element(&s->options, n) {
132                         struct uci_option *o = uci_to_option(n);
133
134                         if (strcmp(n->name, "name") != 0)
135                                 continue;
136
137                         if (o->type != UCI_TYPE_STRING)
138                                 continue;
139
140                         if (swlib_match_name(dev, o->v.string))
141                                 goto found;
142
143                         break;
144                 }
145
146                 if (!swlib_match_name(dev, e->name))
147                         continue;
148
149                 goto found;
150         }
151
152         /* not found */
153         return -1;
154
155 found:
156         /* look up available early options, which need to be taken care
157          * of in the correct order */
158         for (i = 0; i < ARRAY_SIZE(early_settings); i++) {
159                 early_settings[i].attr = swlib_lookup_attr(dev,
160                         SWLIB_ATTR_GROUP_GLOBAL, early_settings[i].name);
161         }
162         swlib_map_settings(dev, SWLIB_ATTR_GROUP_GLOBAL, 0, s);
163
164         /* look for port or vlan sections */
165         uci_foreach_element(&p->sections, e) {
166                 struct uci_element *os;
167                 s = uci_to_section(e);
168
169                 if (!strcmp(s->type, "switch_port")) {
170                         char *devn, *port, *port_err = NULL;
171                         int port_n;
172
173                         uci_foreach_element(&s->options, os) {
174                                 o = uci_to_option(os);
175                                 if (o->type != UCI_TYPE_STRING)
176                                         continue;
177
178                                 if (!strcmp(os->name, "device")) {
179                                         devn = o->v.string;
180                                         if (!swlib_match_name(dev, devn))
181                                                 devn = NULL;
182                                 } else if (!strcmp(os->name, "port")) {
183                                         port = o->v.string;
184                                 }
185                         }
186                         if (!devn || !port || !port[0])
187                                 continue;
188
189                         port_n = strtoul(port, &port_err, 0);
190                         if (port_err && port_err[0])
191                                 continue;
192
193                         swlib_map_settings(dev, SWLIB_ATTR_GROUP_PORT, port_n, s);
194                 } else if (!strcmp(s->type, "switch_vlan")) {
195                         char *devn, *vlan, *vlan_err = NULL;
196                         int vlan_n;
197
198                         uci_foreach_element(&s->options, os) {
199                                 o = uci_to_option(os);
200                                 if (o->type != UCI_TYPE_STRING)
201                                         continue;
202
203                                 if (!strcmp(os->name, "device")) {
204                                         devn = o->v.string;
205                                         if (!swlib_match_name(dev, devn))
206                                                 devn = NULL;
207                                 } else if (!strcmp(os->name, "vlan")) {
208                                         vlan = o->v.string;
209                                 }
210                         }
211                         if (!devn || !vlan || !vlan[0])
212                                 continue;
213
214                         vlan_n = strtoul(vlan, &vlan_err, 0);
215                         if (vlan_err && vlan_err[0])
216                                 continue;
217
218                         swlib_map_settings(dev, SWLIB_ATTR_GROUP_VLAN, vlan_n, s);
219                 }
220         }
221
222         for (i = 0; i < ARRAY_SIZE(early_settings); i++) {
223                 struct swlib_setting *st = &early_settings[i];
224                 if (!st->attr || !st->val)
225                         continue;
226                 swlib_set_attr_string(dev, st->attr, st->port_vlan, st->val);
227
228         }
229
230         while (settings) {
231                 struct swlib_setting *st = settings;
232
233                 swlib_set_attr_string(dev, st->attr, st->port_vlan, st->val);
234                 st = st->next;
235                 free(settings);
236                 settings = st;
237         }
238
239         /* Apply the config */
240         attr = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, "apply");
241         if (!attr)
242                 return 0;
243
244         memset(&val, 0, sizeof(val));
245         swlib_set_attr(dev, attr, &val);
246
247         return 0;
248 }