wireless: display anonymous interface section names using index based extended syntax
[project/netifd.git] / config.c
1 /*
2  * netifd - network interface daemon
3  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17
18 #include <uci.h>
19
20 #include "netifd.h"
21 #include "interface.h"
22 #include "interface-ip.h"
23 #include "iprule.h"
24 #include "proto.h"
25 #include "wireless.h"
26 #include "config.h"
27
28 bool config_init = false;
29
30 static struct uci_context *uci_ctx;
31 static struct uci_package *uci_network;
32 static struct uci_package *uci_wireless;
33 static struct blob_buf b;
34
35 static int
36 config_section_idx(struct uci_section *s)
37 {
38         struct uci_element *e;
39         int idx = 0;
40
41         uci_foreach_element(&uci_wireless->sections, e) {
42                 struct uci_section *cur = uci_to_section(e);
43
44                 if (s == cur)
45                         return idx;
46
47                 if (!strcmp(cur->type, s->type))
48                         idx++;
49         }
50
51         return -1;
52 }
53
54 static int
55 config_parse_bridge_interface(struct uci_section *s)
56 {
57         char *name;
58
59         name = alloca(strlen(s->e.name) + 4);
60         sprintf(name, "br-%s", s->e.name);
61         blobmsg_add_string(&b, "name", name);
62
63         uci_to_blob(&b, s, bridge_device_type.config_params);
64         if (!device_create(name, &bridge_device_type, b.head)) {
65                 D(INTERFACE, "Failed to create bridge for interface '%s'\n", s->e.name);
66                 return -EINVAL;
67         }
68
69         blob_buf_init(&b, 0);
70         blobmsg_add_string(&b, "ifname", name);
71         return 0;
72 }
73
74 static void
75 config_parse_interface(struct uci_section *s, bool alias)
76 {
77         struct interface *iface;
78         const char *type = NULL, *disabled;
79         struct blob_attr *config;
80         struct device *dev;
81         bool bridge = false;
82
83         disabled = uci_lookup_option_string(uci_ctx, s, "disabled");
84         if (disabled && !strcmp(disabled, "1"))
85                 return;
86
87         blob_buf_init(&b, 0);
88
89         if (!alias)
90                 type = uci_lookup_option_string(uci_ctx, s, "type");
91         if (type && !strcmp(type, "bridge")) {
92                 if (config_parse_bridge_interface(s))
93                         return;
94
95                 bridge = true;
96         }
97
98         uci_to_blob(&b, s, &interface_attr_list);
99
100         iface = interface_alloc(s->e.name, b.head);
101         if (!iface)
102                 return;
103
104         if (iface->proto_handler && iface->proto_handler->config_params)
105                 uci_to_blob(&b, s, iface->proto_handler->config_params);
106
107         if (!bridge && uci_to_blob(&b, s, simple_device_type.config_params))
108                 iface->device_config = true;
109
110         config = blob_memdup(b.head);
111         if (!config)
112                 goto error;
113
114         if (alias) {
115                 if (!interface_add_alias(iface, config))
116                         goto error_free_config;
117         } else {
118                 interface_add(iface, config);
119         }
120
121         /*
122          * need to look up the interface name again, in case of config update,
123          * the pointer will have changed
124          */
125         iface = vlist_find(&interfaces, s->e.name, iface, node);
126         if (!iface)
127                 return;
128
129         dev = iface->main_dev.dev;
130         if (!dev || !dev->default_config)
131                 return;
132
133         blob_buf_init(&b, 0);
134         uci_to_blob(&b, s, dev->type->config_params);
135         if (blob_len(b.head) == 0)
136                 return;
137
138         device_set_config(dev, dev->type, b.head);
139         return;
140 error_free_config:
141         free(config);
142 error:
143         free(iface);
144 }
145
146 static void
147 config_parse_route(struct uci_section *s, bool v6)
148 {
149         void *route;
150
151         blob_buf_init(&b, 0);
152         route = blobmsg_open_array(&b, "route");
153         uci_to_blob(&b, s, &route_attr_list);
154         blobmsg_close_array(&b, route);
155         interface_ip_add_route(NULL, blob_data(b.head), v6);
156 }
157
158 static void
159 config_parse_rule(struct uci_section *s, bool v6)
160 {
161         void *rule;
162
163         blob_buf_init(&b, 0);
164         rule = blobmsg_open_array(&b, "rule");
165         uci_to_blob(&b, s, &rule_attr_list);
166         blobmsg_close_array(&b, rule);
167         iprule_add(blob_data(b.head), v6);
168 }
169
170 static void
171 config_init_devices(void)
172 {
173         struct uci_element *e;
174
175         uci_foreach_element(&uci_network->sections, e) {
176                 struct uci_section *s = uci_to_section(e);
177                 const struct device_type *devtype = NULL;
178                 const char *type, *name;
179
180                 if (strcmp(s->type, "device") != 0)
181                         continue;
182
183                 name = uci_lookup_option_string(uci_ctx, s, "name");
184                 if (!name)
185                         continue;
186
187                 type = uci_lookup_option_string(uci_ctx, s, "type");
188                 if (type) {
189                         if (!strcmp(type, "8021ad"))
190                                 devtype = &vlandev_device_type;
191                         else if (!strcmp(type, "8021q"))
192                                 devtype = &vlandev_device_type;
193                         else if (!strcmp(type, "bridge"))
194                                 devtype = &bridge_device_type;
195                         else if (!strcmp(type, "macvlan"))
196                                 devtype = &macvlan_device_type;
197                         else if (!strcmp(type, "tunnel"))
198                                 devtype = &tunnel_device_type;
199                 }
200
201                 if (!devtype)
202                         devtype = &simple_device_type;
203
204                 blob_buf_init(&b, 0);
205                 uci_to_blob(&b, s, devtype->config_params);
206                 device_create(name, devtype, b.head);
207         }
208 }
209
210 static struct uci_package *
211 config_init_package(const char *config)
212 {
213         struct uci_context *ctx = uci_ctx;
214         struct uci_package *p = NULL;
215
216         if (!ctx) {
217                 ctx = uci_alloc_context();
218                 uci_ctx = ctx;
219
220                 ctx->flags &= ~UCI_FLAG_STRICT;
221                 if (config_path)
222                         uci_set_confdir(ctx, config_path);
223
224 #ifdef DUMMY_MODE
225                 uci_set_savedir(ctx, "./tmp");
226 #endif
227         } else {
228                 p = uci_lookup_package(ctx, config);
229                 if (p)
230                         uci_unload(ctx, p);
231         }
232
233         if (uci_load(ctx, config, &p))
234                 return NULL;
235
236         return p;
237 }
238
239 static void
240 config_init_interfaces(void)
241 {
242         struct uci_element *e;
243
244         uci_foreach_element(&uci_network->sections, e) {
245                 struct uci_section *s = uci_to_section(e);
246
247                 if (!strcmp(s->type, "interface"))
248                         config_parse_interface(s, false);
249         }
250
251         uci_foreach_element(&uci_network->sections, e) {
252                 struct uci_section *s = uci_to_section(e);
253
254                 if (!strcmp(s->type, "alias"))
255                         config_parse_interface(s, true);
256         }
257 }
258
259 static void
260 config_init_routes(void)
261 {
262         struct interface *iface;
263         struct uci_element *e;
264
265         vlist_for_each_element(&interfaces, iface, node)
266                 interface_ip_update_start(&iface->config_ip);
267
268         uci_foreach_element(&uci_network->sections, e) {
269                 struct uci_section *s = uci_to_section(e);
270
271                 if (!strcmp(s->type, "route"))
272                         config_parse_route(s, false);
273                 else if (!strcmp(s->type, "route6"))
274                         config_parse_route(s, true);
275         }
276
277         vlist_for_each_element(&interfaces, iface, node)
278                 interface_ip_update_complete(&iface->config_ip);
279 }
280
281 static void
282 config_init_rules(void)
283 {
284         struct uci_element *e;
285
286         iprule_update_start();
287
288         uci_foreach_element(&uci_network->sections, e) {
289                 struct uci_section *s = uci_to_section(e);
290
291                 if (!strcmp(s->type, "rule"))
292                         config_parse_rule(s, false);
293                 else if (!strcmp(s->type, "rule6"))
294                         config_parse_rule(s, true);
295         }
296
297         iprule_update_complete();
298 }
299
300 static void
301 config_init_globals(void)
302 {
303         struct uci_section *globals = uci_lookup_section(
304                         uci_ctx, uci_network, "globals");
305         if (!globals)
306                 return;
307
308         const char *ula_prefix = uci_lookup_option_string(
309                         uci_ctx, globals, "ula_prefix");
310         interface_ip_set_ula_prefix(ula_prefix);
311 }
312
313 static void
314 config_parse_wireless_device(struct uci_section *s)
315 {
316         struct wireless_driver *drv;
317         const char *driver_name;
318
319         driver_name = uci_lookup_option_string(uci_ctx, s, "type");
320         if (!driver_name)
321                 return;
322
323         drv = avl_find_element(&wireless_drivers, driver_name, drv, node);
324         if (!drv)
325                 return;
326
327         blob_buf_init(&b, 0);
328         uci_to_blob(&b, s, drv->device.config);
329         wireless_device_create(drv, s->e.name, b.head);
330 }
331
332 static void
333 config_parse_wireless_interface(struct wireless_device *wdev, struct uci_section *s)
334 {
335         char *name = NULL;
336
337         if (s->anonymous) {
338                 name = alloca(strlen(s->type) + 16);
339                 asprintf(&name, "@%s[%d]", s->type, config_section_idx(s));
340         }
341
342         blob_buf_init(&b, 0);
343         uci_to_blob(&b, s, wdev->drv->interface.config);
344         wireless_interface_create(wdev, b.head, name ? name : s->e.name);
345 }
346
347 static void
348 config_init_wireless(void)
349 {
350         struct wireless_device *wdev;
351         struct uci_element *e;
352         const char *dev_name;
353
354         if (!uci_wireless) {
355                 DPRINTF("No wireless configuration found\n");
356                 return;
357         }
358
359         vlist_update(&wireless_devices);
360
361         uci_foreach_element(&uci_wireless->sections, e) {
362                 struct uci_section *s = uci_to_section(e);
363                 if (strcmp(s->type, "wifi-device") != 0)
364                         continue;
365
366                 config_parse_wireless_device(s);
367         }
368
369         vlist_flush(&wireless_devices);
370
371         vlist_for_each_element(&wireless_devices, wdev, node) {
372                 wdev->vif_idx = 0;
373                 vlist_update(&wdev->interfaces);
374         }
375
376         uci_foreach_element(&uci_wireless->sections, e) {
377                 struct uci_section *s = uci_to_section(e);
378
379                 if (strcmp(s->type, "wifi-iface") != 0)
380                         continue;
381
382                 dev_name = uci_lookup_option_string(uci_ctx, s, "device");
383                 if (!dev_name)
384                         continue;
385
386                 wdev = vlist_find(&wireless_devices, dev_name, wdev, node);
387                 if (!wdev) {
388                         DPRINTF("device %s not found!\n", dev_name);
389                         continue;
390                 }
391
392                 config_parse_wireless_interface(wdev, s);
393         }
394
395         vlist_for_each_element(&wireless_devices, wdev, node)
396                 vlist_flush(&wdev->interfaces);
397 }
398
399 void
400 config_init_all(void)
401 {
402         uci_network = config_init_package("network");
403         if (!uci_network) {
404                 fprintf(stderr, "Failed to load network config\n");
405                 return;
406         }
407
408         uci_wireless = config_init_package("wireless");
409
410         vlist_update(&interfaces);
411         config_init = true;
412         device_lock();
413
414         device_reset_config();
415         config_init_devices();
416         config_init_interfaces();
417         config_init_routes();
418         config_init_rules();
419         config_init_globals();
420         config_init_wireless();
421
422         config_init = false;
423         device_unlock();
424
425         device_reset_old();
426         device_init_pending();
427         vlist_flush(&interfaces);
428         device_free_unused(NULL);
429         interface_refresh_assignments(false);
430         interface_start_pending();
431         wireless_start_pending();
432 }