proto-shell: Model config parameter "no-proto-task" as a proto flag
[project/netifd.git] / wireless.c
1 /*
2  * netifd - network interface daemon
3  * Copyright (C) 2013 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 <signal.h>
15 #include "netifd.h"
16 #include "wireless.h"
17 #include "handler.h"
18 #include "ubus.h"
19
20 #define WIRELESS_SETUP_RETRY    3
21
22 struct vlist_tree wireless_devices;
23 struct avl_tree wireless_drivers;
24 static struct blob_buf b;
25 static int drv_fd;
26
27 static const struct blobmsg_policy wdev_policy =
28         { .name = "disabled", .type = BLOBMSG_TYPE_BOOL };
29
30 static const struct uci_blob_param_list wdev_param = {
31         .n_params = 1,
32         .params = &wdev_policy,
33 };
34
35 enum {
36         VIF_ATTR_DISABLED,
37         VIF_ATTR_NETWORK,
38         VIF_ATTR_ISOLATE,
39         VIF_ATTR_MODE,
40         __VIF_ATTR_MAX,
41 };
42
43 static const struct blobmsg_policy vif_policy[__VIF_ATTR_MAX] = {
44         [VIF_ATTR_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL },
45         [VIF_ATTR_NETWORK] = { .name = "network", .type = BLOBMSG_TYPE_ARRAY },
46         [VIF_ATTR_ISOLATE] = { .name = "isolate", .type = BLOBMSG_TYPE_BOOL },
47         [VIF_ATTR_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_STRING },
48 };
49
50 static const struct uci_blob_param_list vif_param = {
51         .n_params = ARRAY_SIZE(vif_policy),
52         .params = vif_policy,
53 };
54
55 static void
56 put_container(struct blob_buf *buf, struct blob_attr *attr, const char *name)
57 {
58         void *c = blobmsg_open_table(buf, name);
59         blob_put_raw(buf, blob_data(attr), blob_len(attr));
60         blobmsg_close_table(buf, c);
61 }
62
63 static void
64 vif_config_add_bridge(struct blob_buf *buf, struct blob_attr *networks, bool prepare)
65 {
66         struct interface *iface;
67         struct device *dev = NULL;
68         struct blob_attr *cur;
69         const char *network;
70         int rem;
71
72         if (!networks)
73                 return;
74
75         blobmsg_for_each_attr(cur, networks, rem) {
76                 network = blobmsg_data(cur);
77
78                 iface = vlist_find(&interfaces, network, iface, node);
79                 if (!iface)
80                         continue;
81
82                 dev = iface->main_dev.dev;
83                 if (!dev)
84                         return;
85
86                 if (dev->type != &bridge_device_type)
87                         return;
88         }
89
90         if (!dev)
91                 return;
92
93         if (dev->hotplug_ops && dev->hotplug_ops->prepare)
94                 dev->hotplug_ops->prepare(dev);
95
96         blobmsg_add_string(buf, "bridge", dev->ifname);
97
98         if (dev->settings.flags & DEV_OPT_MULTICAST_TO_UNICAST)
99                 blobmsg_add_u8(buf, "multicast_to_unicast",
100                                dev->settings.multicast_to_unicast);
101 }
102
103 static void
104 prepare_config(struct wireless_device *wdev, struct blob_buf *buf, bool up)
105 {
106         struct wireless_interface *vif;
107         void *l, *i;
108
109         blob_buf_init(&b, 0);
110         put_container(&b, wdev->config, "config");
111         if (wdev->data)
112                 blobmsg_add_blob(&b, wdev->data);
113
114         l = blobmsg_open_table(&b, "interfaces");
115         vlist_for_each_element(&wdev->interfaces, vif, node) {
116                 i = blobmsg_open_table(&b, vif->name);
117                 vif_config_add_bridge(&b, vif->network, up);
118                 put_container(&b, vif->config, "config");
119                 if (vif->data)
120                         blobmsg_add_blob(&b, vif->data);
121                 blobmsg_close_table(&b, i);
122         }
123         blobmsg_close_table(&b, l);
124 }
125
126 static bool
127 wireless_process_check(struct wireless_process *proc)
128 {
129         return check_pid_path(proc->pid, proc->exe);
130 }
131
132 static void
133 wireless_complete_kill_request(struct wireless_device *wdev)
134 {
135         if (!wdev->kill_request)
136                 return;
137
138         ubus_complete_deferred_request(ubus_ctx, wdev->kill_request, 0);
139         free(wdev->kill_request);
140         wdev->kill_request = NULL;
141 }
142
143 static void
144 wireless_process_free(struct wireless_device *wdev, struct wireless_process *proc)
145 {
146         D(WIRELESS, "Wireless device '%s' free pid %d\n", wdev->name, proc->pid);
147         list_del(&proc->list);
148         free(proc);
149
150         if (list_empty(&wdev->script_proc))
151                 wireless_complete_kill_request(wdev);
152 }
153
154 static void
155 wireless_close_script_proc_fd(struct wireless_device *wdev)
156 {
157         if (wdev->script_proc_fd.fd < 0)
158                 return;
159
160         uloop_fd_delete(&wdev->script_proc_fd);
161         close(wdev->script_proc_fd.fd);
162         wdev->script_proc_fd.fd = -1;
163 }
164
165 static void
166 wireless_process_kill_all(struct wireless_device *wdev, int signal, bool free)
167 {
168         struct wireless_process *proc, *tmp;
169
170         list_for_each_entry_safe(proc, tmp, &wdev->script_proc, list) {
171                 bool check = wireless_process_check(proc);
172
173                 if (check) {
174                         D(WIRELESS, "Wireless device '%s' kill pid %d\n", wdev->name, proc->pid);
175                         kill(proc->pid, signal);
176                 }
177
178                 if (free || !check)
179                         wireless_process_free(wdev, proc);
180         }
181
182         if (free)
183                 wireless_close_script_proc_fd(wdev);
184 }
185
186 static void
187 wireless_device_free_state(struct wireless_device *wdev)
188 {
189         struct wireless_interface *vif;
190
191         uloop_timeout_cancel(&wdev->script_check);
192         uloop_timeout_cancel(&wdev->timeout);
193         wireless_complete_kill_request(wdev);
194         free(wdev->data);
195         wdev->data = NULL;
196         vlist_for_each_element(&wdev->interfaces, vif, node) {
197                 free(vif->data);
198                 vif->data = NULL;
199                 vif->ifname = NULL;
200         }
201 }
202
203 static void wireless_interface_handle_link(struct wireless_interface *vif, bool up)
204 {
205         struct interface *iface;
206         struct blob_attr *cur;
207         const char *network;
208         int rem;
209
210         if (!vif->network || !vif->ifname)
211                 return;
212
213         if (up) {
214                 struct device *dev = device_get(vif->ifname, 2);
215                 if (dev) {
216                         dev->wireless_isolate = vif->isolate;
217                         dev->wireless = true;
218                         dev->wireless_ap = vif->ap_mode;
219                 }
220         }
221
222         blobmsg_for_each_attr(cur, vif->network, rem) {
223                 network = blobmsg_data(cur);
224
225                 iface = vlist_find(&interfaces, network, iface, node);
226                 if (!iface)
227                         continue;
228
229                 interface_handle_link(iface, vif->ifname, up, true);
230         }
231 }
232
233 static void
234 wireless_device_setup_cancel(struct wireless_device *wdev)
235 {
236         if (wdev->cancel)
237                 return;
238
239         D(WIRELESS, "Cancel wireless device '%s' setup\n", wdev->name);
240         wdev->cancel = true;
241         uloop_timeout_set(&wdev->timeout, 10 * 1000);
242 }
243
244 static void
245 wireless_device_run_handler(struct wireless_device *wdev, bool up)
246 {
247         const char *action = up ? "setup" : "teardown";
248         const char *argv[6];
249         char *config;
250         int i = 0;
251         int fds[2] = { -1, -1 };
252
253         D(WIRELESS, "Wireless device '%s' run %s handler\n", wdev->name, action);
254         if (!up && wdev->prev_config) {
255                 config = blobmsg_format_json(wdev->prev_config, true);
256                 free(wdev->prev_config);
257                 wdev->prev_config = NULL;
258         } else {
259                 prepare_config(wdev, &b, up);
260                 config = blobmsg_format_json(b.head, true);
261         }
262
263         argv[i++] = wdev->drv->script;
264         argv[i++] = wdev->drv->name;
265         argv[i++] = action;
266         argv[i++] = wdev->name;
267         argv[i++] = config;
268         argv[i] = NULL;
269
270         if (up && pipe(fds) == 0) {
271                 wdev->script_proc_fd.fd = fds[0];
272                 uloop_fd_add(&wdev->script_proc_fd,
273                              ULOOP_READ | ULOOP_EDGE_TRIGGER);
274         }
275
276         netifd_start_process(argv, NULL, &wdev->script_task);
277
278         if (fds[1] >= 0)
279                 close(fds[1]);
280
281         free(config);
282 }
283
284 static void
285 __wireless_device_set_up(struct wireless_device *wdev)
286 {
287         if (wdev->disabled)
288                 return;
289
290         if (wdev->state != IFS_DOWN || config_init)
291                 return;
292
293         free(wdev->prev_config);
294         wdev->prev_config = NULL;
295         wdev->state = IFS_SETUP;
296         wireless_device_run_handler(wdev, true);
297 }
298
299 static void
300 wireless_device_free(struct wireless_device *wdev)
301 {
302         vlist_flush_all(&wdev->interfaces);
303         avl_delete(&wireless_devices.avl, &wdev->node.avl);
304         free(wdev->config);
305         free(wdev->prev_config);
306         free(wdev);
307 }
308
309 static void
310 wdev_handle_config_change(struct wireless_device *wdev)
311 {
312         enum interface_config_state state = wdev->config_state;
313
314         switch(state) {
315         case IFC_NORMAL:
316         case IFC_RELOAD:
317                 wdev->config_state = IFC_NORMAL;
318                 if (wdev->autostart)
319                         __wireless_device_set_up(wdev);
320                 break;
321         case IFC_REMOVE:
322                 wireless_device_free(wdev);
323                 break;
324         }
325 }
326
327 static void
328 wireless_device_mark_down(struct wireless_device *wdev)
329 {
330         struct wireless_interface *vif;
331
332         D(WIRELESS, "Wireless device '%s' is now down\n", wdev->name);
333
334         vlist_for_each_element(&wdev->interfaces, vif, node)
335                 wireless_interface_handle_link(vif, false);
336
337         wireless_process_kill_all(wdev, SIGTERM, true);
338
339         wdev->cancel = false;
340         wdev->state = IFS_DOWN;
341         wireless_device_free_state(wdev);
342         wdev_handle_config_change(wdev);
343 }
344
345 static void
346 wireless_device_setup_timeout(struct uloop_timeout *timeout)
347 {
348         struct wireless_device *wdev = container_of(timeout, struct wireless_device, timeout);
349
350         netifd_kill_process(&wdev->script_task);
351         wdev->script_task.cb(&wdev->script_task, -1);
352         wireless_device_mark_down(wdev);
353 }
354
355 void
356 wireless_device_set_up(struct wireless_device *wdev)
357 {
358         wdev->retry = WIRELESS_SETUP_RETRY;
359         wdev->autostart = true;
360         __wireless_device_set_up(wdev);
361 }
362
363 static void
364 __wireless_device_set_down(struct wireless_device *wdev)
365 {
366         if (wdev->state == IFS_TEARDOWN || wdev->state == IFS_DOWN)
367                 return;
368
369         if (wdev->script_task.uloop.pending) {
370                 wireless_device_setup_cancel(wdev);
371                 return;
372         }
373
374         wdev->state = IFS_TEARDOWN;
375         wireless_device_run_handler(wdev, false);
376 }
377
378 static void
379 wireless_device_mark_up(struct wireless_device *wdev)
380 {
381         struct wireless_interface *vif;
382
383         if (wdev->cancel) {
384                 wdev->cancel = false;
385                 __wireless_device_set_down(wdev);
386                 return;
387         }
388
389         D(WIRELESS, "Wireless device '%s' is now up\n", wdev->name);
390         wdev->state = IFS_UP;
391         vlist_for_each_element(&wdev->interfaces, vif, node)
392                 wireless_interface_handle_link(vif, true);
393 }
394
395 static void
396 wireless_device_retry_setup(struct wireless_device *wdev)
397 {
398         if (wdev->state == IFS_TEARDOWN || wdev->state == IFS_DOWN || wdev->cancel)
399                 return;
400
401         if (--wdev->retry < 0)
402                 wdev->autostart = false;
403
404         __wireless_device_set_down(wdev);
405 }
406
407 static void
408 wireless_device_script_task_cb(struct netifd_process *proc, int ret)
409 {
410         struct wireless_device *wdev = container_of(proc, struct wireless_device, script_task);
411
412         switch (wdev->state) {
413         case IFS_SETUP:
414                 wireless_device_retry_setup(wdev);
415                 break;
416         case IFS_TEARDOWN:
417                 wireless_device_mark_down(wdev);
418                 break;
419         default:
420                 break;
421         }
422 }
423
424 void
425 wireless_device_set_down(struct wireless_device *wdev)
426 {
427         wdev->autostart = false;
428         __wireless_device_set_down(wdev);
429 }
430
431 static void
432 wdev_set_config_state(struct wireless_device *wdev, enum interface_config_state s)
433 {
434         if (wdev->config_state != IFC_NORMAL)
435                 return;
436
437         wdev->config_state = s;
438         if (wdev->state == IFS_DOWN)
439                 wdev_handle_config_change(wdev);
440         else
441                 __wireless_device_set_down(wdev);
442 }
443
444 static void
445 wdev_prepare_prev_config(struct wireless_device *wdev)
446 {
447         if (wdev->prev_config)
448                 return;
449
450         prepare_config(wdev, &b, false);
451         wdev->prev_config = blob_memdup(b.head);
452 }
453
454 static void
455 wdev_change_config(struct wireless_device *wdev, struct wireless_device *wd_new)
456 {
457         struct blob_attr *new_config = wd_new->config;
458         bool disabled = wd_new->disabled;
459
460         free(wd_new);
461
462         wdev_prepare_prev_config(wdev);
463         if (blob_attr_equal(wdev->config, new_config) && wdev->disabled == disabled)
464                 return;
465
466         D(WIRELESS, "Update configuration of wireless device '%s'\n", wdev->name);
467         free(wdev->config);
468         wdev->config = blob_memdup(new_config);
469         wdev->disabled = disabled;
470         wdev_set_config_state(wdev, IFC_RELOAD);
471 }
472
473 static void
474 wdev_create(struct wireless_device *wdev)
475 {
476         wdev->retry = WIRELESS_SETUP_RETRY;
477         wdev->config = blob_memdup(wdev->config);
478 }
479
480 static void
481 wdev_update(struct vlist_tree *tree, struct vlist_node *node_new,
482             struct vlist_node *node_old)
483 {
484         struct wireless_device *wd_old = container_of(node_old, struct wireless_device, node);
485         struct wireless_device *wd_new = container_of(node_new, struct wireless_device, node);
486
487         if (wd_old && wd_new) {
488                 wdev_change_config(wd_old, wd_new);
489         } else if (wd_old) {
490                 D(WIRELESS, "Delete wireless device '%s'\n", wd_old->name);
491                 wdev_set_config_state(wd_old, IFC_REMOVE);
492         } else if (wd_new) {
493                 D(WIRELESS, "Create wireless device '%s'\n", wd_new->name);
494                 wdev_create(wd_new);
495         }
496 }
497
498 static void
499 wireless_add_handler(const char *script, const char *name, json_object *obj)
500 {
501         struct wireless_driver *drv;
502         char *name_str, *script_str;
503         json_object *dev_config_obj, *iface_config_obj;
504         struct uci_blob_param_list *dev_config, *iface_config;
505
506         dev_config_obj = json_get_field(obj, "device", json_type_array);
507         iface_config_obj = json_get_field(obj, "iface", json_type_array);
508
509         if (!dev_config_obj || !iface_config_obj)
510                 return;
511
512         drv = calloc_a(sizeof(*drv),
513                 &dev_config, sizeof(*dev_config) + sizeof(void *),
514                 &iface_config, sizeof(*iface_config) + sizeof(void *),
515                 &name_str, strlen(name) + 1,
516                 &script_str, strlen(script) + 1);
517
518         drv->name = strcpy(name_str, name);
519         drv->script = strcpy(script_str, script);
520
521         dev_config->n_next = 1;
522         dev_config->next[0] = &wdev_param;
523         drv->device.config = dev_config;
524
525         iface_config->n_next = 1;
526         iface_config->next[0] = &vif_param;
527         drv->interface.config = iface_config;
528
529         drv->device.buf = netifd_handler_parse_config(drv->device.config, dev_config_obj);
530         drv->interface.buf = netifd_handler_parse_config(drv->interface.config, iface_config_obj);
531
532         drv->node.key = drv->name;
533         avl_insert(&wireless_drivers, &drv->node);
534         D(WIRELESS, "Add handler for script %s: %s\n", script, name);
535 }
536
537 void wireless_init(void)
538 {
539         vlist_init(&wireless_devices, avl_strcmp, wdev_update);
540         wireless_devices.keep_old = true;
541         wireless_devices.no_delete = true;
542
543         avl_init(&wireless_drivers, avl_strcmp, false, NULL);
544         drv_fd = netifd_open_subdir("wireless");
545         if (drv_fd < 0)
546                 return;
547
548         netifd_init_script_handlers(drv_fd, wireless_add_handler);
549 }
550
551 static void
552 wireless_interface_init_config(struct wireless_interface *vif)
553 {
554         struct blob_attr *tb[__VIF_ATTR_MAX];
555         struct blob_attr *cur;
556
557         vif->network = NULL;
558         blobmsg_parse(vif_policy, __VIF_ATTR_MAX, tb, blob_data(vif->config), blob_len(vif->config));
559
560         if ((cur = tb[VIF_ATTR_NETWORK]))
561                 vif->network = cur;
562
563         cur = tb[VIF_ATTR_ISOLATE];
564         if (cur)
565                 vif->isolate = blobmsg_get_bool(cur);
566
567         cur = tb[VIF_ATTR_MODE];
568         if (cur)
569                 vif->ap_mode = !strcmp(blobmsg_get_string(cur), "ap");
570 }
571
572 static void
573 vif_update(struct vlist_tree *tree, struct vlist_node *node_new,
574            struct vlist_node *node_old)
575 {
576         struct wireless_interface *vif_old = container_of(node_old, struct wireless_interface, node);
577         struct wireless_interface *vif_new = container_of(node_new, struct wireless_interface, node);
578         struct wireless_device *wdev;
579
580         if (vif_old)
581                 wdev = vif_old->wdev;
582         else
583                 wdev = vif_new->wdev;
584
585         if (vif_old && vif_new) {
586                 free((void *) vif_old->section);
587                 vif_old->section = strdup(vif_new->section);
588                 if (blob_attr_equal(vif_old->config, vif_new->config)) {
589                         free(vif_new);
590                         return;
591                 }
592
593                 D(WIRELESS, "Update wireless interface %s on device %s\n", vif_new->name, wdev->name);
594                 wireless_interface_handle_link(vif_old, false);
595                 free(vif_old->config);
596                 vif_old->config = blob_memdup(vif_new->config);
597                 vif_old->isolate = vif_new->isolate;
598                 vif_old->ap_mode = vif_new->ap_mode;
599                 wireless_interface_init_config(vif_old);
600                 free(vif_new);
601         } else if (vif_new) {
602                 D(WIRELESS, "Create new wireless interface %s on device %s\n", vif_new->name, wdev->name);
603                 vif_new->section = strdup(vif_new->section);
604                 vif_new->config = blob_memdup(vif_new->config);
605                 wireless_interface_init_config(vif_new);
606         } else if (vif_old) {
607                 D(WIRELESS, "Delete wireless interface %s on device %s\n", vif_old->name, wdev->name);
608                 free((void *) vif_old->section);
609                 free(vif_old->config);
610                 free(vif_old);
611         }
612
613         wdev_set_config_state(wdev, IFC_RELOAD);
614 }
615
616 static void
617 wireless_proc_poll_fd(struct uloop_fd *fd, unsigned int events)
618 {
619         struct wireless_device *wdev = container_of(fd, struct wireless_device, script_proc_fd);
620         char buf[128];
621
622         while (1) {
623                 int b = read(fd->fd, buf, sizeof(buf));
624                 if (b < 0) {
625                         if (errno == EINTR)
626                                 continue;
627
628                         if (errno == EAGAIN)
629                                 return;
630
631                         goto done;
632                 }
633
634                 if (!b)
635                         goto done;
636         }
637
638 done:
639         uloop_timeout_set(&wdev->script_check, 0);
640         wireless_close_script_proc_fd(wdev);
641 }
642
643 static void
644 wireless_device_check_script_tasks(struct uloop_timeout *timeout)
645 {
646         struct wireless_device *wdev = container_of(timeout, struct wireless_device, script_check);
647         struct wireless_process *proc, *tmp;
648         bool restart = false;
649
650         list_for_each_entry_safe(proc, tmp, &wdev->script_proc, list) {
651                 if (wireless_process_check(proc))
652                         continue;
653
654                 D(WIRELESS, "Wireless device '%s' pid %d has terminated\n", wdev->name, proc->pid);
655                 if (proc->required)
656                         restart = true;
657
658                 wireless_process_free(wdev, proc);
659         }
660
661         if (restart)
662                 wireless_device_retry_setup(wdev);
663         else
664                 uloop_timeout_set(&wdev->script_check, 1000);
665 }
666
667 void
668 wireless_device_create(struct wireless_driver *drv, const char *name, struct blob_attr *data)
669 {
670         struct wireless_device *wdev;
671         char *name_buf;
672         struct blob_attr *disabled;
673
674         blobmsg_parse(&wdev_policy, 1, &disabled, blob_data(data), blob_len(data));
675
676         wdev = calloc_a(sizeof(*wdev), &name_buf, strlen(name) + 1);
677         if (disabled && blobmsg_get_bool(disabled))
678                 wdev->disabled = true;
679         wdev->drv = drv;
680         wdev->state = IFS_DOWN;
681         wdev->config_state = IFC_NORMAL;
682         wdev->name = strcpy(name_buf, name);
683         wdev->config = data;
684         wdev->config_autostart = true;
685         wdev->autostart = wdev->config_autostart;
686         INIT_LIST_HEAD(&wdev->script_proc);
687         vlist_init(&wdev->interfaces, avl_strcmp, vif_update);
688         wdev->interfaces.keep_old = true;
689
690         wdev->timeout.cb = wireless_device_setup_timeout;
691         wdev->script_task.cb = wireless_device_script_task_cb;
692         wdev->script_task.dir_fd = drv_fd;
693         wdev->script_task.log_prefix = wdev->name;
694
695         wdev->script_proc_fd.fd = -1;
696         wdev->script_proc_fd.cb = wireless_proc_poll_fd;
697
698         wdev->script_check.cb = wireless_device_check_script_tasks;
699
700         vlist_add(&wireless_devices, &wdev->node, wdev->name);
701 }
702
703 void wireless_interface_create(struct wireless_device *wdev, struct blob_attr *data, const char *section)
704 {
705         struct wireless_interface *vif;
706         struct blob_attr *tb[__VIF_ATTR_MAX];
707         struct blob_attr *cur;
708         char *name_buf;
709         char name[8];
710
711         blobmsg_parse(vif_policy, __VIF_ATTR_MAX, tb, blob_data(data), blob_len(data));
712
713         cur = tb[VIF_ATTR_DISABLED];
714         if (cur && blobmsg_get_bool(cur))
715                 return;
716
717         sprintf(name, "%d", wdev->vif_idx++);
718
719         vif = calloc_a(sizeof(*vif),
720                        &name_buf, strlen(name) + 1);
721         vif->name = strcpy(name_buf, name);
722         vif->wdev = wdev;
723         vif->config = data;
724         vif->section = section;
725         vif->isolate = false;
726
727         vlist_add(&wdev->interfaces, &vif->node, vif->name);
728 }
729
730 static void
731 wireless_interface_status(struct wireless_interface *iface, struct blob_buf *b)
732 {
733         void *i;
734
735         i = blobmsg_open_table(b, NULL);
736         if (iface->section)
737                 blobmsg_add_string(b, "section", iface->section);
738         if (iface->ifname)
739                 blobmsg_add_string(b, "ifname", iface->ifname);
740         put_container(b, iface->config, "config");
741         blobmsg_close_table(b, i);
742 }
743
744 void
745 wireless_device_status(struct wireless_device *wdev, struct blob_buf *b)
746 {
747         struct wireless_interface *iface;
748         void *c, *i;
749
750         c = blobmsg_open_table(b, wdev->name);
751         blobmsg_add_u8(b, "up", wdev->state == IFS_UP);
752         blobmsg_add_u8(b, "pending", wdev->state == IFS_SETUP || wdev->state == IFS_TEARDOWN);
753         blobmsg_add_u8(b, "autostart", wdev->autostart);
754         blobmsg_add_u8(b, "disabled", wdev->disabled);
755         put_container(b, wdev->config, "config");
756
757         i = blobmsg_open_array(b, "interfaces");
758         vlist_for_each_element(&wdev->interfaces, iface, node)
759                 wireless_interface_status(iface, b);
760         blobmsg_close_array(b, i);
761         blobmsg_close_table(b, c);
762 }
763
764 void
765 wireless_device_get_validate(struct wireless_device *wdev, struct blob_buf *b)
766 {
767         struct uci_blob_param_list *p;
768         void *c, *d;
769         int i;
770
771         c = blobmsg_open_table(b, wdev->name);
772
773         d = blobmsg_open_table(b, "device");
774         p = wdev->drv->device.config;
775         for (i = 0; i < p->n_params; i++)
776                 blobmsg_add_string(b, p->params[i].name, uci_get_validate_string(p, i));
777         blobmsg_close_table(b, d);
778
779         d = blobmsg_open_table(b, "interface");
780         p = wdev->drv->interface.config;
781         for (i = 0; i < p->n_params; i++)
782                 blobmsg_add_string(b, p->params[i].name, uci_get_validate_string(p, i));
783         blobmsg_close_table(b, d);
784
785         blobmsg_close_table(b, c);
786 }
787
788 static void
789 wireless_interface_set_data(struct wireless_interface *vif)
790 {
791         enum {
792                 VIF_DATA_IFNAME,
793                 __VIF_DATA_MAX,
794         };
795         static const struct blobmsg_policy data_policy[__VIF_DATA_MAX] = {
796                 [VIF_DATA_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
797         };
798         struct blob_attr *tb[__VIF_DATA_MAX];
799         struct blob_attr *cur;
800
801         blobmsg_parse(data_policy, __VIF_DATA_MAX, tb,
802                       blobmsg_data(vif->data), blobmsg_data_len(vif->data));
803
804         if ((cur = tb[VIF_DATA_IFNAME]))
805                 vif->ifname = blobmsg_data(cur);
806 }
807
808 static int
809 wireless_device_add_process(struct wireless_device *wdev, struct blob_attr *data)
810 {
811         enum {
812                 PROC_ATTR_PID,
813                 PROC_ATTR_EXE,
814                 PROC_ATTR_REQUIRED,
815                 __PROC_ATTR_MAX
816         };
817         static const struct blobmsg_policy proc_policy[__PROC_ATTR_MAX] = {
818                 [PROC_ATTR_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
819                 [PROC_ATTR_EXE] = { .name = "exe", .type = BLOBMSG_TYPE_STRING },
820                 [PROC_ATTR_REQUIRED] = { .name = "required", .type = BLOBMSG_TYPE_BOOL },
821         };
822         struct blob_attr *tb[__PROC_ATTR_MAX];
823         struct wireless_process *proc;
824         char *name;
825         int pid;
826
827         if (!data)
828                 return UBUS_STATUS_INVALID_ARGUMENT;
829
830         blobmsg_parse(proc_policy, __PROC_ATTR_MAX, tb, blobmsg_data(data), blobmsg_data_len(data));
831         if (!tb[PROC_ATTR_PID] || !tb[PROC_ATTR_EXE])
832                 return UBUS_STATUS_INVALID_ARGUMENT;
833
834         pid = blobmsg_get_u32(tb[PROC_ATTR_PID]);
835         if (pid < 2)
836                 return UBUS_STATUS_INVALID_ARGUMENT;
837
838         proc = calloc_a(sizeof(*proc),
839                 &name, strlen(blobmsg_data(tb[PROC_ATTR_EXE])) + 1);
840
841         proc->pid = pid;
842         proc->exe = strcpy(name, blobmsg_data(tb[PROC_ATTR_EXE]));
843
844         if (tb[PROC_ATTR_REQUIRED])
845                 proc->required = blobmsg_get_bool(tb[PROC_ATTR_REQUIRED]);
846
847         D(WIRELESS, "Wireless device '%s' add pid %d\n", wdev->name, proc->pid);
848         list_add(&proc->list, &wdev->script_proc);
849         uloop_timeout_set(&wdev->script_check, 0);
850
851         return 0;
852 }
853
854 static int
855 wireless_device_process_kill_all(struct wireless_device *wdev, struct blob_attr *data,
856                                  struct ubus_request_data *req)
857 {
858         enum {
859                 KILL_ATTR_SIGNAL,
860                 KILL_ATTR_IMMEDIATE,
861                 __KILL_ATTR_MAX
862         };
863         static const struct blobmsg_policy kill_policy[__KILL_ATTR_MAX] = {
864                 [KILL_ATTR_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
865                 [KILL_ATTR_IMMEDIATE] = { .name = "immediate", .type = BLOBMSG_TYPE_BOOL },
866         };
867         struct blob_attr *tb[__KILL_ATTR_MAX];
868         struct blob_attr *cur;
869         bool immediate = false;
870         int signal = SIGTERM;
871
872         blobmsg_parse(kill_policy, __KILL_ATTR_MAX, tb, blobmsg_data(data), blobmsg_data_len(data));
873
874         if ((cur = tb[KILL_ATTR_SIGNAL]))
875                 signal = blobmsg_get_u32(cur);
876
877         if ((cur = tb[KILL_ATTR_IMMEDIATE]))
878                 immediate = blobmsg_get_bool(cur);
879
880         if (wdev->state != IFS_TEARDOWN || wdev->kill_request)
881                 return UBUS_STATUS_PERMISSION_DENIED;
882
883         wireless_process_kill_all(wdev, signal, immediate);
884
885         if (list_empty(&wdev->script_proc))
886                 return 0;
887
888         wdev->kill_request = calloc(1, sizeof(*wdev->kill_request));
889         ubus_defer_request(ubus_ctx, req, wdev->kill_request);
890
891         return 0;
892 }
893
894 static int
895 wireless_device_set_retry(struct wireless_device *wdev, struct blob_attr *data)
896 {
897         static const struct blobmsg_policy retry_policy = {
898                 .name = "retry", .type = BLOBMSG_TYPE_INT32
899         };
900         struct blob_attr *val;
901
902         blobmsg_parse(&retry_policy, 1, &val, blobmsg_data(data), blobmsg_data_len(data));
903         if (!val)
904                 return UBUS_STATUS_INVALID_ARGUMENT;
905
906         wdev->retry = blobmsg_get_u32(val);
907         return 0;
908 }
909
910 enum {
911         NOTIFY_CMD_UP = 0,
912         NOTIFY_CMD_SET_DATA = 1,
913         NOTIFY_CMD_PROCESS_ADD = 2,
914         NOTIFY_CMD_PROCESS_KILL_ALL = 3,
915         NOTIFY_CMD_SET_RETRY = 4,
916 };
917
918 int
919 wireless_device_notify(struct wireless_device *wdev, struct blob_attr *data,
920                        struct ubus_request_data *req)
921 {
922         enum {
923                 NOTIFY_ATTR_COMMAND,
924                 NOTIFY_ATTR_VIF,
925                 NOTIFY_ATTR_DATA,
926                 __NOTIFY_MAX,
927         };
928         static const struct blobmsg_policy notify_policy[__NOTIFY_MAX] = {
929                 [NOTIFY_ATTR_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_INT32 },
930                 [NOTIFY_ATTR_VIF] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
931                 [NOTIFY_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
932         };
933         struct wireless_interface *vif = NULL;
934         struct blob_attr *tb[__NOTIFY_MAX];
935         struct blob_attr *cur, **pdata;
936
937         blobmsg_parse(notify_policy, __NOTIFY_MAX, tb, blob_data(data), blob_len(data));
938
939         if (!tb[NOTIFY_ATTR_COMMAND])
940                 return UBUS_STATUS_INVALID_ARGUMENT;
941
942         if ((cur = tb[NOTIFY_ATTR_VIF]) != NULL) {
943                 vif = vlist_find(&wdev->interfaces, blobmsg_data(cur), vif, node);
944                 if (!vif)
945                         return UBUS_STATUS_NOT_FOUND;
946         }
947
948         cur = tb[NOTIFY_ATTR_DATA];
949         if (!cur)
950                 return UBUS_STATUS_INVALID_ARGUMENT;
951
952         switch (blobmsg_get_u32(tb[NOTIFY_ATTR_COMMAND])) {
953         case NOTIFY_CMD_UP:
954                 if (vif)
955                         return UBUS_STATUS_INVALID_ARGUMENT;
956
957                 if (wdev->state != IFS_SETUP)
958                         return UBUS_STATUS_PERMISSION_DENIED;
959
960                 wireless_device_mark_up(wdev);
961                 break;
962         case NOTIFY_CMD_SET_DATA:
963                 if (vif)
964                         pdata = &vif->data;
965                 else
966                         pdata = &wdev->data;
967
968                 if (*pdata)
969                         return UBUS_STATUS_INVALID_ARGUMENT;
970
971                 *pdata = blob_memdup(cur);
972                 if (vif)
973                         wireless_interface_set_data(vif);
974                 break;
975         case NOTIFY_CMD_PROCESS_ADD:
976                 return wireless_device_add_process(wdev, cur);
977         case NOTIFY_CMD_PROCESS_KILL_ALL:
978                 return wireless_device_process_kill_all(wdev, cur, req);
979         case NOTIFY_CMD_SET_RETRY:
980                 return wireless_device_set_retry(wdev, cur);
981         default:
982                 return UBUS_STATUS_INVALID_ARGUMENT;
983         }
984
985         return 0;
986 }
987
988 void
989 wireless_start_pending(void)
990 {
991         struct wireless_device *wdev;
992
993         vlist_for_each_element(&wireless_devices, wdev, node)
994                 if (wdev->autostart)
995                         __wireless_device_set_up(wdev);
996 }