netifd: wireless: add support for tracking wifi-station sections
[oweals/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, struct device_type *devtype)
57 {
58         char *name;
59
60         name = alloca(strlen(s->e.name) + strlen(devtype->name_prefix) + 2);
61         sprintf(name, "%s-%s", devtype->name_prefix, s->e.name);
62         blobmsg_add_string(&b, "name", name);
63
64         uci_to_blob(&b, s, devtype->config_params);
65         if (!device_create(name, devtype, b.head)) {
66                 D(INTERFACE, "Failed to create '%s' device for interface '%s'\n",
67                         devtype->name, s->e.name);
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         struct device_type *devtype = NULL;
83
84         disabled = uci_lookup_option_string(uci_ctx, s, "disabled");
85         if (disabled && !strcmp(disabled, "1"))
86                 return;
87
88         blob_buf_init(&b, 0);
89
90         if (!alias)
91                 type = uci_lookup_option_string(uci_ctx, s, "type");
92
93         if (type)
94                 devtype = device_type_get(type);
95
96         if (devtype && devtype->bridge_capability) {
97                 if (config_parse_bridge_interface(s, devtype))
98                         return;
99
100                 bridge = true;
101         }
102
103         uci_to_blob(&b, s, &interface_attr_list);
104
105         iface = interface_alloc(s->e.name, b.head, false);
106         if (!iface)
107                 return;
108
109         if (iface->proto_handler && iface->proto_handler->config_params)
110                 uci_to_blob(&b, s, iface->proto_handler->config_params);
111
112         if (!bridge && uci_to_blob(&b, s, simple_device_type.config_params))
113                 iface->device_config = true;
114
115         config = blob_memdup(b.head);
116         if (!config)
117                 goto error;
118
119         if (alias) {
120                 if (!interface_add_alias(iface, config))
121                         goto error_free_config;
122         } else {
123                 if (!interface_add(iface, config))
124                         goto error_free_config;
125         }
126         return;
127
128 error_free_config:
129         free(config);
130 error:
131         free(iface);
132 }
133
134 static void
135 config_parse_route(struct uci_section *s, bool v6)
136 {
137         void *route;
138
139         blob_buf_init(&b, 0);
140         route = blobmsg_open_array(&b, "route");
141         uci_to_blob(&b, s, &route_attr_list);
142         blobmsg_close_array(&b, route);
143         interface_ip_add_route(NULL, blob_data(b.head), v6);
144 }
145
146 static void
147 config_parse_neighbor(struct uci_section *s, bool v6)
148 {
149         void *neighbor;
150         blob_buf_init(&b,0);
151         neighbor = blobmsg_open_array(&b, "neighbor");
152         uci_to_blob(&b,s, &neighbor_attr_list);
153         blobmsg_close_array(&b, neighbor);
154         interface_ip_add_neighbor(NULL, blob_data(b.head), v6);
155 }
156
157 static void
158 config_parse_rule(struct uci_section *s, bool v6)
159 {
160         void *rule;
161
162         blob_buf_init(&b, 0);
163         rule = blobmsg_open_array(&b, "rule");
164         uci_to_blob(&b, s, &rule_attr_list);
165         blobmsg_close_array(&b, rule);
166         iprule_add(blob_data(b.head), v6);
167 }
168
169 static void
170 config_init_devices(void)
171 {
172         struct uci_element *e;
173
174         uci_foreach_element(&uci_network->sections, e) {
175                 const struct uci_blob_param_list *params = NULL;
176                 struct uci_section *s = uci_to_section(e);
177                 struct device_type *devtype = NULL;
178                 struct device *dev;
179                 const char *type, *name;
180
181                 if (strcmp(s->type, "device") != 0)
182                         continue;
183
184                 name = uci_lookup_option_string(uci_ctx, s, "name");
185                 if (!name)
186                         continue;
187
188                 type = uci_lookup_option_string(uci_ctx, s, "type");
189                 if (type)
190                         devtype = device_type_get(type);
191
192                 if (devtype)
193                         params = devtype->config_params;
194                 if (!params)
195                         params = simple_device_type.config_params;
196
197                 blob_buf_init(&b, 0);
198                 uci_to_blob(&b, s, params);
199                 if (devtype) {
200                         dev = device_create(name, devtype, b.head);
201                         if (!dev)
202                                 continue;
203                 } else {
204                         dev = device_get(name, 1);
205                         if (!dev)
206                                 continue;
207
208                         dev->current_config = true;
209                         device_apply_config(dev, dev->type, b.head);
210                 }
211                 dev->default_config = false;
212         }
213 }
214
215 static struct uci_package *
216 config_init_package(const char *config)
217 {
218         struct uci_context *ctx = uci_ctx;
219         struct uci_package *p = NULL;
220
221         if (!ctx) {
222                 ctx = uci_alloc_context();
223                 uci_ctx = ctx;
224
225                 ctx->flags &= ~UCI_FLAG_STRICT;
226                 if (config_path)
227                         uci_set_confdir(ctx, config_path);
228
229 #ifdef DUMMY_MODE
230                 uci_set_savedir(ctx, "./tmp");
231 #endif
232         } else {
233                 p = uci_lookup_package(ctx, config);
234                 if (p)
235                         uci_unload(ctx, p);
236         }
237
238         if (uci_load(ctx, config, &p))
239                 return NULL;
240
241         return p;
242 }
243
244 static void
245 config_init_interfaces(void)
246 {
247         struct uci_element *e;
248
249         uci_foreach_element(&uci_network->sections, e) {
250                 struct uci_section *s = uci_to_section(e);
251
252                 if (!strcmp(s->type, "interface"))
253                         config_parse_interface(s, false);
254         }
255
256         uci_foreach_element(&uci_network->sections, e) {
257                 struct uci_section *s = uci_to_section(e);
258
259                 if (!strcmp(s->type, "alias"))
260                         config_parse_interface(s, true);
261         }
262 }
263
264 static void
265 config_init_ip(void)
266 {
267         struct interface *iface;
268         struct uci_element *e;
269
270         vlist_for_each_element(&interfaces, iface, node)
271                 interface_ip_update_start(&iface->config_ip);
272
273         uci_foreach_element(&uci_network->sections, e) {
274                 struct uci_section *s = uci_to_section(e);
275
276                 if (!strcmp(s->type, "route"))
277                         config_parse_route(s, false);
278                 else if (!strcmp(s->type, "route6"))
279                         config_parse_route(s, true);
280                 if (!strcmp(s->type, "neighbor"))
281                         config_parse_neighbor(s, false);
282                 else if (!strcmp(s->type, "neighbor6"))
283                         config_parse_neighbor(s, true);
284         }
285
286         vlist_for_each_element(&interfaces, iface, node)
287                 interface_ip_update_complete(&iface->config_ip);
288 }
289
290 static void
291 config_init_rules(void)
292 {
293         struct uci_element *e;
294
295         iprule_update_start();
296
297         uci_foreach_element(&uci_network->sections, e) {
298                 struct uci_section *s = uci_to_section(e);
299
300                 if (!strcmp(s->type, "rule"))
301                         config_parse_rule(s, false);
302                 else if (!strcmp(s->type, "rule6"))
303                         config_parse_rule(s, true);
304         }
305
306         iprule_update_complete();
307 }
308
309 static void
310 config_init_globals(void)
311 {
312         struct uci_section *globals = uci_lookup_section(
313                         uci_ctx, uci_network, "globals");
314         if (!globals)
315                 return;
316
317         const char *ula_prefix = uci_lookup_option_string(
318                         uci_ctx, globals, "ula_prefix");
319         interface_ip_set_ula_prefix(ula_prefix);
320 }
321
322 static void
323 config_parse_wireless_device(struct uci_section *s)
324 {
325         struct wireless_driver *drv;
326         const char *driver_name;
327
328         driver_name = uci_lookup_option_string(uci_ctx, s, "type");
329         if (!driver_name)
330                 return;
331
332         drv = avl_find_element(&wireless_drivers, driver_name, drv, node);
333         if (!drv)
334                 return;
335
336         blob_buf_init(&b, 0);
337         uci_to_blob(&b, s, drv->device.config);
338         wireless_device_create(drv, s->e.name, b.head);
339 }
340
341 static struct wireless_interface*
342 config_parse_wireless_interface(struct wireless_device *wdev, struct uci_section *s)
343 {
344         char *name;
345
346         name = alloca(strlen(s->type) + 16);
347         sprintf(name, "@%s[%d]", s->type, config_section_idx(s));
348
349         blob_buf_init(&b, 0);
350         uci_to_blob(&b, s, wdev->drv->interface.config);
351         return wireless_interface_create(wdev, b.head, s->anonymous ? name : s->e.name);
352 }
353
354 static void
355 config_parse_wireless_vlan(struct wireless_device *wdev, char *vif, struct uci_section *s)
356 {
357         char *name;
358
359         name = alloca(strlen(s->type) + 16);
360         sprintf(name, "@%s[%d]", s->type, config_section_idx(s));
361
362         blob_buf_init(&b, 0);
363         uci_to_blob(&b, s, wdev->drv->vlan.config);
364         wireless_vlan_create(wdev, vif, b.head, s->anonymous ? name : s->e.name);
365 }
366
367 static void
368 config_parse_wireless_station(struct wireless_device *wdev, char *vif, struct uci_section *s)
369 {
370         char *name;
371
372         name = alloca(strlen(s->type) + 16);
373         sprintf(name, "@%s[%d]", s->type, config_section_idx(s));
374
375         blob_buf_init(&b, 0);
376         uci_to_blob(&b, s, wdev->drv->station.config);
377         wireless_station_create(wdev, vif, b.head, s->anonymous ? name : s->e.name);
378 }
379
380 static void
381 config_init_wireless(void)
382 {
383         struct wireless_device *wdev;
384         struct uci_element *e;
385         const char *dev_name;
386
387         if (!uci_wireless) {
388                 DPRINTF("No wireless configuration found\n");
389                 return;
390         }
391
392         vlist_update(&wireless_devices);
393
394         uci_foreach_element(&uci_wireless->sections, e) {
395                 struct uci_section *s = uci_to_section(e);
396                 if (strcmp(s->type, "wifi-device") != 0)
397                         continue;
398
399                 config_parse_wireless_device(s);
400         }
401
402         vlist_flush(&wireless_devices);
403
404         vlist_for_each_element(&wireless_devices, wdev, node) {
405                 wdev->vif_idx = 0;
406                 vlist_update(&wdev->interfaces);
407                 wdev->vlan_idx = 0;
408                 vlist_update(&wdev->vlans);
409                 wdev->sta_idx = 0;
410                 vlist_update(&wdev->stations);
411         }
412
413         uci_foreach_element(&uci_wireless->sections, e) {
414                 struct uci_section *s = uci_to_section(e);
415                 struct wireless_interface *vif;
416                 struct uci_element *f;
417
418                 if (strcmp(s->type, "wifi-iface") != 0)
419                         continue;
420
421                 dev_name = uci_lookup_option_string(uci_ctx, s, "device");
422                 if (!dev_name)
423                         continue;
424
425                 wdev = vlist_find(&wireless_devices, dev_name, wdev, node);
426                 if (!wdev) {
427                         DPRINTF("device %s not found!\n", dev_name);
428                         continue;
429                 }
430
431                 vif = config_parse_wireless_interface(wdev, s);
432
433                 if (!vif || s->anonymous)
434                         continue;
435                 uci_foreach_element(&uci_wireless->sections, f) {
436                         struct uci_section *s = uci_to_section(f);
437                         const char *vif_name;
438
439                         if (strcmp(s->type, "wifi-vlan") != 0)
440                                 continue;
441
442                         vif_name = uci_lookup_option_string(uci_ctx, s, "iface");
443                         if (vif_name && strcmp(e->name, vif_name))
444                                 continue;
445                         config_parse_wireless_vlan(wdev, vif->name, s);
446                 }
447
448                 uci_foreach_element(&uci_wireless->sections, f) {
449                         struct uci_section *s = uci_to_section(f);
450                         const char *vif_name;
451
452                         if (strcmp(s->type, "wifi-station") != 0)
453                                 continue;
454
455                         vif_name = uci_lookup_option_string(uci_ctx, s, "iface");
456                         if (vif_name && strcmp(e->name, vif_name))
457                                 continue;
458                         config_parse_wireless_station(wdev, vif->name, s);
459                 }
460         }
461
462         vlist_for_each_element(&wireless_devices, wdev, node) {
463                 vlist_flush(&wdev->interfaces);
464                 vlist_flush(&wdev->vlans);
465                 vlist_flush(&wdev->stations);
466         }
467 }
468
469 int
470 config_init_all(void)
471 {
472         int ret = 0;
473         char *err;
474
475         uci_network = config_init_package("network");
476         if (!uci_network) {
477                 uci_get_errorstr(uci_ctx, &err, NULL);
478                 netifd_log_message(L_CRIT, "Failed to load network config (%s)\n", err);
479                 free(err);
480                 return -1;
481         }
482
483         uci_wireless = config_init_package("wireless");
484         if (!uci_wireless && uci_ctx->err != UCI_ERR_NOTFOUND) {
485                 uci_get_errorstr(uci_ctx, &err, NULL);
486                 netifd_log_message(L_CRIT, "Failed to load wireless config (%s)\n", err);
487                 free(err);
488                 ret = -1;
489         }
490
491         vlist_update(&interfaces);
492         config_init = true;
493         device_lock();
494
495         device_reset_config();
496         config_init_devices();
497         config_init_interfaces();
498         config_init_ip();
499         config_init_rules();
500         config_init_globals();
501         config_init_wireless();
502
503         config_init = false;
504         device_unlock();
505
506         device_reset_old();
507         device_init_pending();
508         vlist_flush(&interfaces);
509         device_free_unused(NULL);
510         interface_refresh_assignments(false);
511         interface_start_pending();
512         wireless_start_pending();
513
514         return ret;
515 }