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