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