9104a9c48cf2d3e77239babcf20a4e5965423a8f
[project/netifd.git] / config.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <net/ethernet.h>
8
9 #include "netifd.h"
10 #include "interface.h"
11
12 struct uci_context *uci_ctx;
13 static struct uci_package *uci_network;
14 bool config_init = false;
15
16 static void uci_attr_to_blob(struct blob_buf *b, const char *str,
17 const char *name, enum blobmsg_type type)
18 {
19 char *err;
20 int intval;
21
22 switch (type) {
23 case BLOBMSG_TYPE_STRING:
24 blobmsg_add_string(b, name, str);
25 break;
26 case BLOBMSG_TYPE_BOOL:
27 if (!strcmp(str, "true") || !strcmp(str, "1"))
28 intval = 1;
29 else if (!strcmp(str, "false") || !strcmp(str, "0"))
30 intval = 0;
31 else
32 return;
33
34 blobmsg_add_u8(b, name, intval);
35 break;
36 case BLOBMSG_TYPE_INT32:
37 intval = strtol(str, &err, 0);
38 if (*err)
39 return;
40
41 blobmsg_add_u32(b, name, intval);
42 break;
43 default:
44 break;
45 }
46 }
47
48 static void uci_array_to_blob(struct blob_buf *b, struct uci_option *o,
49 enum blobmsg_type type)
50 {
51 struct uci_element *e;
52 char *str, *next, *word;
53
54 if (o->type == UCI_TYPE_LIST) {
55 uci_foreach_element(&o->v.list, e) {
56 uci_attr_to_blob(b, e->name, NULL, type);
57 }
58 return;
59 }
60
61 str = strdup(o->v.string);
62 next = str;
63
64 while ((word = strsep(&next, " \t")) != NULL) {
65 if (!*word)
66 continue;
67
68 uci_attr_to_blob(b, word, NULL, type);
69 }
70
71 free(str);
72 }
73
74 static void uci_to_blob(struct blob_buf *b, struct uci_section *s,
75 const struct config_param_list *p)
76 {
77 const struct blobmsg_policy *attr;
78 struct uci_element *e;
79 struct uci_option *o;
80 void *array;
81 int i;
82
83 uci_foreach_element(&s->options, e) {
84 for (i = 0; i < p->n_params; i++) {
85 attr = &p->params[i];
86 if (!strcmp(attr->name, e->name))
87 break;
88 }
89
90 if (i == p->n_params)
91 continue;
92
93 o = uci_to_option(e);
94
95 if (attr->type == BLOBMSG_TYPE_ARRAY) {
96 if (!p->info)
97 continue;
98
99 array = blobmsg_open_array(b, attr->name);
100 uci_array_to_blob(b, o, p->info[i].type);
101 blobmsg_close_array(b, array);
102 continue;
103 }
104
105 if (o->type == UCI_TYPE_LIST)
106 continue;
107
108 uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
109 }
110 }
111
112 static void
113 config_parse_interface(struct uci_section *s)
114 {
115 struct blob_buf b = {};
116
117 DPRINTF("Create interface '%s'\n", s->e.name);
118
119 blob_buf_init(&b, 0);
120 uci_to_blob(&b, s, &interface_attr_list);
121 alloc_interface(s->e.name, s, b.head);
122 }
123
124 enum {
125 SDEV_NAME,
126 SDEV_TYPE,
127 SDEV_MTU,
128 SDEV_MACADDR,
129 SDEV_TXQUEUELEN,
130 __SDEV_MAX,
131 };
132
133 static const struct uci_parse_option dev_opts[__SDEV_MAX] = {
134 [SDEV_NAME] = { "name", UCI_TYPE_STRING },
135 [SDEV_TYPE] = { "type", UCI_TYPE_STRING },
136 [SDEV_MTU] = { "mtu", UCI_TYPE_STRING },
137 [SDEV_MACADDR] = { "macaddr", UCI_TYPE_STRING },
138 [SDEV_TXQUEUELEN] = { "txqueuelen", UCI_TYPE_STRING },
139 };
140
141 static bool
142 add_int_option(struct uci_option *o, unsigned int *dest)
143 {
144 char *error = NULL;
145 int val;
146
147 if (!o)
148 return false;
149
150 val = strtoul(o->v.string, &error, 0);
151 if (error && *error)
152 return false;
153
154 *dest = val;
155 return true;
156 }
157
158 static void
159 config_init_device_settings(struct device *dev, struct uci_option **opts)
160 {
161 struct ether_addr *ea;
162
163 dev->flags = 0;
164
165 if (add_int_option(opts[SDEV_MTU], &dev->mtu))
166 dev->flags |= DEV_OPT_MTU;
167
168 if (add_int_option(opts[SDEV_TXQUEUELEN], &dev->txqueuelen))
169 dev->flags |= DEV_OPT_TXQUEUELEN;
170
171 if (opts[SDEV_MACADDR]) {
172 ea = ether_aton(opts[SDEV_MACADDR]->v.string);
173 if (ea) {
174 memcpy(dev->macaddr, ea, sizeof(dev->macaddr));
175 dev->flags |= DEV_OPT_MACADDR;
176 }
177 }
178 }
179
180 void
181 config_init_devices(void)
182 {
183 struct uci_element *e;
184 struct device *dev;
185 struct uci_option *opts[__SDEV_MAX];
186
187 uci_foreach_element(&uci_network->sections, e) {
188 struct uci_section *s = uci_to_section(e);
189
190 if (strcmp(s->type, "device") != 0)
191 continue;
192
193 uci_parse_section(s, dev_opts, __SDEV_MAX, opts);
194 if (!opts[SDEV_NAME])
195 continue;
196
197 dev = NULL;
198 if (opts[SDEV_TYPE]) {
199 const char *type = opts[SDEV_TYPE]->v.string;
200
201 if (!strcmp(type, "bridge"))
202 dev = bridge_create(opts[SDEV_NAME]->v.string, s);
203 } else {
204 dev = get_device(opts[SDEV_NAME]->v.string, true);
205 }
206
207 if (!dev)
208 continue;
209
210 config_init_device_settings(dev, opts);
211 dev->config_hash = uci_hash_options(opts, __SDEV_MAX);
212 }
213 }
214
215 void
216 config_init_interfaces(const char *name)
217 {
218 struct uci_context *ctx;
219 struct uci_package *p = NULL;
220 struct uci_element *e;
221
222 ctx = uci_alloc_context();
223 uci_ctx = ctx;
224
225 uci_set_confdir(ctx, "./config");
226
227 if (uci_load(ctx, "network", &p)) {
228 fprintf(stderr, "Failed to load network config\n");
229 return;
230 }
231
232 uci_network = p;
233 config_init = true;
234
235 config_init_devices();
236
237 uci_foreach_element(&p->sections, e) {
238 struct uci_section *s = uci_to_section(e);
239
240 if (name && strcmp(s->e.name, name) != 0)
241 continue;
242
243 if (!strcmp(s->type, "interface"))
244 config_parse_interface(s);
245 }
246 cleanup_devices();
247 config_init = false;
248
249 start_pending_interfaces();
250 }