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