wireless: add retry_setup_failed to status notification
[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 #define _GNU_SOURCE
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18
19 #include <uci.h>
20
21 #include "netifd.h"
22 #include "interface.h"
23 #include "interface-ip.h"
24 #include "iprule.h"
25 #include "proto.h"
26 #include "wireless.h"
27 #include "config.h"
28
29 bool config_init = false;
30
31 static struct uci_context *uci_ctx;
32 static struct uci_package *uci_network;
33 static struct uci_package *uci_wireless;
34 static struct blob_buf b;
35
36 static int
37 config_section_idx(struct uci_section *s)
38 {
39         struct uci_element *e;
40         int idx = 0;
41
42         uci_foreach_element(&uci_wireless->sections, e) {
43                 struct uci_section *cur = uci_to_section(e);
44
45                 if (s == cur)
46                         return idx;
47
48                 if (!strcmp(cur->type, s->type))
49                         idx++;
50         }
51
52         return -1;
53 }
54
55 static int
56 config_parse_bridge_interface(struct uci_section *s)
57 {
58         char *name;
59
60         name = alloca(strlen(s->e.name) + 4);
61         sprintf(name, "br-%s", s->e.name);
62         blobmsg_add_string(&b, "name", name);
63
64         uci_to_blob(&b, s, bridge_device_type.config_params);
65         if (!device_create(name, &bridge_device_type, b.head)) {
66                 D(INTERFACE, "Failed to create bridge for interface '%s'\n", s->e.name);
67                 return -EINVAL;
68         }
69
70         blob_buf_init(&b, 0);
71         blobmsg_add_string(&b, "ifname", name);
72         return 0;
73 }
74
75 static void
76 config_parse_interface(struct uci_section *s, bool alias)
77 {
78         struct interface *iface;
79         const char *type = NULL, *disabled;
80         struct blob_attr *config;
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         return;
121
122 error_free_config:
123         free(config);
124 error:
125         free(iface);
126 }
127
128 static void
129 config_parse_route(struct uci_section *s, bool v6)
130 {
131         void *route;
132
133         blob_buf_init(&b, 0);
134         route = blobmsg_open_array(&b, "route");
135         uci_to_blob(&b, s, &route_attr_list);
136         blobmsg_close_array(&b, route);
137         interface_ip_add_route(NULL, blob_data(b.head), v6);
138 }
139
140 static void
141 config_parse_rule(struct uci_section *s, bool v6)
142 {
143         void *rule;
144
145         blob_buf_init(&b, 0);
146         rule = blobmsg_open_array(&b, "rule");
147         uci_to_blob(&b, s, &rule_attr_list);
148         blobmsg_close_array(&b, rule);
149         iprule_add(blob_data(b.head), v6);
150 }
151
152 static void
153 config_init_devices(void)
154 {
155         struct uci_element *e;
156
157         uci_foreach_element(&uci_network->sections, e) {
158                 const struct uci_blob_param_list *params = NULL;
159                 struct uci_section *s = uci_to_section(e);
160                 const struct device_type *devtype = NULL;
161                 struct device *dev;
162                 const char *type, *name;
163
164                 if (strcmp(s->type, "device") != 0)
165                         continue;
166
167                 name = uci_lookup_option_string(uci_ctx, s, "name");
168                 if (!name)
169                         continue;
170
171                 type = uci_lookup_option_string(uci_ctx, s, "type");
172                 if (type) {
173                         if (!strcmp(type, "8021ad"))
174                                 devtype = &vlandev_device_type;
175                         else if (!strcmp(type, "8021q"))
176                                 devtype = &vlandev_device_type;
177                         else if (!strcmp(type, "bridge"))
178                                 devtype = &bridge_device_type;
179                         else if (!strcmp(type, "macvlan"))
180                                 devtype = &macvlan_device_type;
181                         else if (!strcmp(type, "tunnel"))
182                                 devtype = &tunnel_device_type;
183                 }
184
185                 if (devtype)
186                         params = devtype->config_params;
187                 if (!params)
188                         params = simple_device_type.config_params;
189
190                 blob_buf_init(&b, 0);
191                 uci_to_blob(&b, s, params);
192                 if (devtype) {
193                         dev = device_create(name, devtype, b.head);
194                         if (!dev)
195                                 continue;
196                 } else {
197                         dev = device_get(name, 1);
198                         if (!dev)
199                                 continue;
200
201                         dev->current_config = true;
202                         device_apply_config(dev, dev->type, b.head);
203                 }
204                 dev->default_config = false;
205         }
206 }
207
208 static struct uci_package *
209 config_init_package(const char *config)
210 {
211         struct uci_context *ctx = uci_ctx;
212         struct uci_package *p = NULL;
213
214         if (!ctx) {
215                 ctx = uci_alloc_context();
216                 uci_ctx = ctx;
217
218                 ctx->flags &= ~UCI_FLAG_STRICT;
219                 if (config_path)
220                         uci_set_confdir(ctx, config_path);
221
222 #ifdef DUMMY_MODE
223                 uci_set_savedir(ctx, "./tmp");
224 #endif
225         } else {
226                 p = uci_lookup_package(ctx, config);
227                 if (p)
228                         uci_unload(ctx, p);
229         }
230
231         if (uci_load(ctx, config, &p))
232                 return NULL;
233
234         return p;
235 }
236
237 static void
238 config_init_interfaces(void)
239 {
240         struct uci_element *e;
241
242         uci_foreach_element(&uci_network->sections, e) {
243                 struct uci_section *s = uci_to_section(e);
244
245                 if (!strcmp(s->type, "interface"))
246                         config_parse_interface(s, false);
247         }
248
249         uci_foreach_element(&uci_network->sections, e) {
250                 struct uci_section *s = uci_to_section(e);
251
252                 if (!strcmp(s->type, "alias"))
253                         config_parse_interface(s, true);
254         }
255 }
256
257 static void
258 config_init_routes(void)
259 {
260         struct interface *iface;
261         struct uci_element *e;
262
263         vlist_for_each_element(&interfaces, iface, node)
264                 interface_ip_update_start(&iface->config_ip);
265
266         uci_foreach_element(&uci_network->sections, e) {
267                 struct uci_section *s = uci_to_section(e);
268
269                 if (!strcmp(s->type, "route"))
270                         config_parse_route(s, false);
271                 else if (!strcmp(s->type, "route6"))
272                         config_parse_route(s, true);
273         }
274
275         vlist_for_each_element(&interfaces, iface, node)
276                 interface_ip_update_complete(&iface->config_ip);
277 }
278
279 static void
280 config_init_rules(void)
281 {
282         struct uci_element *e;
283
284         iprule_update_start();
285
286         uci_foreach_element(&uci_network->sections, e) {
287                 struct uci_section *s = uci_to_section(e);
288
289                 if (!strcmp(s->type, "rule"))
290                         config_parse_rule(s, false);
291                 else if (!strcmp(s->type, "rule6"))
292                         config_parse_rule(s, true);
293         }
294
295         iprule_update_complete();
296 }
297
298 static void
299 config_init_globals(void)
300 {
301         struct uci_section *globals = uci_lookup_section(
302                         uci_ctx, uci_network, "globals");
303         if (!globals)
304                 return;
305
306         const char *ula_prefix = uci_lookup_option_string(
307                         uci_ctx, globals, "ula_prefix");
308         interface_ip_set_ula_prefix(ula_prefix);
309
310         const char *default_ps = uci_lookup_option_string(
311                         uci_ctx, globals, "default_ps");
312
313         if (default_ps)
314                 device_set_default_ps(strcmp(default_ps, "1") ? false : true);
315 }
316
317 static void
318 config_parse_wireless_device(struct uci_section *s)
319 {
320         struct wireless_driver *drv;
321         const char *driver_name;
322
323         driver_name = uci_lookup_option_string(uci_ctx, s, "type");
324         if (!driver_name)
325                 return;
326
327         drv = avl_find_element(&wireless_drivers, driver_name, drv, node);
328         if (!drv)
329                 return;
330
331         blob_buf_init(&b, 0);
332         uci_to_blob(&b, s, drv->device.config);
333         wireless_device_create(drv, s->e.name, b.head);
334 }
335
336 static void
337 config_parse_wireless_interface(struct wireless_device *wdev, struct uci_section *s)
338 {
339         char *name;
340
341         name = alloca(strlen(s->type) + 16);
342         sprintf(name, "@%s[%d]", s->type, config_section_idx(s));
343
344         blob_buf_init(&b, 0);
345         uci_to_blob(&b, s, wdev->drv->interface.config);
346         wireless_interface_create(wdev, b.head, s->anonymous ? name : s->e.name);
347 }
348
349 static void
350 config_init_wireless(void)
351 {
352         struct wireless_device *wdev;
353         struct uci_element *e;
354         const char *dev_name;
355
356         if (!uci_wireless) {
357                 DPRINTF("No wireless configuration found\n");
358                 return;
359         }
360
361         vlist_update(&wireless_devices);
362
363         uci_foreach_element(&uci_wireless->sections, e) {
364                 struct uci_section *s = uci_to_section(e);
365                 if (strcmp(s->type, "wifi-device") != 0)
366                         continue;
367
368                 config_parse_wireless_device(s);
369         }
370
371         vlist_flush(&wireless_devices);
372
373         vlist_for_each_element(&wireless_devices, wdev, node) {
374                 wdev->vif_idx = 0;
375                 vlist_update(&wdev->interfaces);
376         }
377
378         uci_foreach_element(&uci_wireless->sections, e) {
379                 struct uci_section *s = uci_to_section(e);
380
381                 if (strcmp(s->type, "wifi-iface") != 0)
382                         continue;
383
384                 dev_name = uci_lookup_option_string(uci_ctx, s, "device");
385                 if (!dev_name)
386                         continue;
387
388                 wdev = vlist_find(&wireless_devices, dev_name, wdev, node);
389                 if (!wdev) {
390                         DPRINTF("device %s not found!\n", dev_name);
391                         continue;
392                 }
393
394                 config_parse_wireless_interface(wdev, s);
395         }
396
397         vlist_for_each_element(&wireless_devices, wdev, node)
398                 vlist_flush(&wdev->interfaces);
399 }
400
401 void
402 config_init_all(void)
403 {
404         uci_network = config_init_package("network");
405         if (!uci_network) {
406                 fprintf(stderr, "Failed to load network config\n");
407                 return;
408         }
409
410         uci_wireless = config_init_package("wireless");
411
412         vlist_update(&interfaces);
413         config_init = true;
414         device_lock();
415
416         device_reset_config();
417         config_init_devices();
418         config_init_interfaces();
419         config_init_routes();
420         config_init_rules();
421         config_init_globals();
422         config_init_wireless();
423
424         config_init = false;
425         device_unlock();
426
427         device_reset_old();
428         device_init_pending();
429         vlist_flush(&interfaces);
430         device_free_unused(NULL);
431         interface_refresh_assignments(false);
432         interface_start_pending();
433         wireless_start_pending();
434 }