system-linux: fix a glob related memleak
[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(buf, name);
55         blob_put_raw(buf, blob_data(attr), blob_len(attr));
56         blobmsg_close_table(buf, 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, *section_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),
675                        &name_buf, strlen(name) + 1,
676                        &section_buf, strlen(section) + 1);
677         vif->name = strcpy(name_buf, name);
678         vif->wdev = wdev;
679         vif->config = data;
680         vif->section = strcpy(section_buf, section);
681         vlist_add(&wdev->interfaces, &vif->node, vif->name);
682 }
683
684 static void
685 wireless_interface_status(struct wireless_interface *iface, struct blob_buf *b)
686 {
687         void *i;
688
689         i = blobmsg_open_table(b, NULL);
690         if (iface->section)
691                 blobmsg_add_string(b, "section", iface->section);
692         if (iface->ifname)
693                 blobmsg_add_string(b, "ifname", iface->ifname);
694         put_container(b, iface->config, "config");
695         blobmsg_close_table(b, i);
696 }
697
698 void
699 wireless_device_status(struct wireless_device *wdev, struct blob_buf *b)
700 {
701         struct wireless_interface *iface;
702         void *c, *i;
703
704         c = blobmsg_open_table(b, wdev->name);
705         blobmsg_add_u8(b, "up", wdev->state == IFS_UP);
706         blobmsg_add_u8(b, "pending", wdev->state == IFS_SETUP || wdev->state == IFS_TEARDOWN);
707         blobmsg_add_u8(b, "autostart", wdev->autostart);
708         blobmsg_add_u8(b, "disabled", wdev->disabled);
709         put_container(b, wdev->config, "config");
710
711         i = blobmsg_open_array(b, "interfaces");
712         vlist_for_each_element(&wdev->interfaces, iface, node)
713                 wireless_interface_status(iface, b);
714         blobmsg_close_array(b, i);
715         blobmsg_close_table(b, c);
716 }
717
718 void
719 wireless_device_get_validate(struct wireless_device *wdev, struct blob_buf *b)
720 {
721         struct uci_blob_param_list *p;
722         void *c, *d;
723         int i;
724
725         c = blobmsg_open_table(b, wdev->name);
726
727         d = blobmsg_open_table(b, "device");
728         p = wdev->drv->device.config;
729         for (i = 0; i < p->n_params; i++)
730                 blobmsg_add_string(b, p->params[i].name, uci_get_validate_string(p, i));
731         blobmsg_close_table(b, d);
732
733         d = blobmsg_open_table(b, "interface");
734         p = wdev->drv->interface.config;
735         for (i = 0; i < p->n_params; i++)
736                 blobmsg_add_string(b, p->params[i].name, uci_get_validate_string(p, i));
737         blobmsg_close_table(b, d);
738
739         blobmsg_close_table(b, c);
740 }
741
742 static void
743 wireless_interface_set_data(struct wireless_interface *vif)
744 {
745         enum {
746                 VIF_DATA_IFNAME,
747                 __VIF_DATA_MAX,
748         };
749         static const struct blobmsg_policy data_policy[__VIF_DATA_MAX] = {
750                 [VIF_DATA_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
751         };
752         struct blob_attr *tb[__VIF_DATA_MAX];
753         struct blob_attr *cur;
754
755         blobmsg_parse(data_policy, __VIF_DATA_MAX, tb,
756                       blobmsg_data(vif->data), blobmsg_data_len(vif->data));
757
758         if ((cur = tb[VIF_DATA_IFNAME]))
759                 vif->ifname = blobmsg_data(cur);
760 }
761
762 static int
763 wireless_device_add_process(struct wireless_device *wdev, struct blob_attr *data)
764 {
765         enum {
766                 PROC_ATTR_PID,
767                 PROC_ATTR_EXE,
768                 PROC_ATTR_REQUIRED,
769                 __PROC_ATTR_MAX
770         };
771         static const struct blobmsg_policy proc_policy[__PROC_ATTR_MAX] = {
772                 [PROC_ATTR_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
773                 [PROC_ATTR_EXE] = { .name = "exe", .type = BLOBMSG_TYPE_STRING },
774                 [PROC_ATTR_REQUIRED] = { .name = "required", .type = BLOBMSG_TYPE_BOOL },
775         };
776         struct blob_attr *tb[__PROC_ATTR_MAX];
777         struct wireless_process *proc;
778         char *name;
779         int pid;
780
781         if (!data)
782                 return UBUS_STATUS_INVALID_ARGUMENT;
783
784         blobmsg_parse(proc_policy, __PROC_ATTR_MAX, tb, blobmsg_data(data), blobmsg_data_len(data));
785         if (!tb[PROC_ATTR_PID] || !tb[PROC_ATTR_EXE])
786                 return UBUS_STATUS_INVALID_ARGUMENT;
787
788         pid = blobmsg_get_u32(tb[PROC_ATTR_PID]);
789         if (pid < 2)
790                 return UBUS_STATUS_INVALID_ARGUMENT;
791
792         proc = calloc_a(sizeof(*proc),
793                 &name, strlen(blobmsg_data(tb[PROC_ATTR_EXE])) + 1);
794
795         proc->pid = pid;
796         proc->exe = strcpy(name, blobmsg_data(tb[PROC_ATTR_EXE]));
797
798         if (tb[PROC_ATTR_REQUIRED])
799                 proc->required = blobmsg_get_bool(tb[PROC_ATTR_REQUIRED]);
800
801         D(WIRELESS, "Wireless device '%s' add pid %d\n", wdev->name, proc->pid);
802         list_add(&proc->list, &wdev->script_proc);
803         uloop_timeout_set(&wdev->script_check, 0);
804
805         return 0;
806 }
807
808 static int
809 wireless_device_process_kill_all(struct wireless_device *wdev, struct blob_attr *data,
810                                  struct ubus_request_data *req)
811 {
812         enum {
813                 KILL_ATTR_SIGNAL,
814                 KILL_ATTR_IMMEDIATE,
815                 __KILL_ATTR_MAX
816         };
817         static const struct blobmsg_policy kill_policy[__KILL_ATTR_MAX] = {
818                 [KILL_ATTR_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
819                 [KILL_ATTR_IMMEDIATE] = { .name = "immediate", .type = BLOBMSG_TYPE_BOOL },
820         };
821         struct blob_attr *tb[__KILL_ATTR_MAX];
822         struct blob_attr *cur;
823         bool immediate = false;
824         int signal = SIGTERM;
825
826         blobmsg_parse(kill_policy, __KILL_ATTR_MAX, tb, blobmsg_data(data), blobmsg_data_len(data));
827
828         if ((cur = tb[KILL_ATTR_SIGNAL]))
829                 signal = blobmsg_get_u32(cur);
830
831         if ((cur = tb[KILL_ATTR_IMMEDIATE]))
832                 immediate = blobmsg_get_bool(cur);
833
834         if (wdev->state != IFS_TEARDOWN || wdev->kill_request)
835                 return UBUS_STATUS_PERMISSION_DENIED;
836
837         wireless_process_kill_all(wdev, signal, immediate);
838
839         if (list_empty(&wdev->script_proc))
840                 return 0;
841
842         wdev->kill_request = calloc(1, sizeof(*wdev->kill_request));
843         ubus_defer_request(ubus_ctx, req, wdev->kill_request);
844
845         return 0;
846 }
847
848 static int
849 wireless_device_set_retry(struct wireless_device *wdev, struct blob_attr *data)
850 {
851         static const struct blobmsg_policy retry_policy = {
852                 .name = "retry", .type = BLOBMSG_TYPE_INT32
853         };
854         struct blob_attr *val;
855
856         blobmsg_parse(&retry_policy, 1, &val, blobmsg_data(data), blobmsg_data_len(data));
857         if (!val)
858                 return UBUS_STATUS_INVALID_ARGUMENT;
859
860         wdev->retry = blobmsg_get_u32(val);
861         return 0;
862 }
863
864 enum {
865         NOTIFY_CMD_UP = 0,
866         NOTIFY_CMD_SET_DATA = 1,
867         NOTIFY_CMD_PROCESS_ADD = 2,
868         NOTIFY_CMD_PROCESS_KILL_ALL = 3,
869         NOTIFY_CMD_SET_RETRY = 4,
870 };
871
872 int
873 wireless_device_notify(struct wireless_device *wdev, struct blob_attr *data,
874                        struct ubus_request_data *req)
875 {
876         enum {
877                 NOTIFY_ATTR_COMMAND,
878                 NOTIFY_ATTR_VIF,
879                 NOTIFY_ATTR_DATA,
880                 __NOTIFY_MAX,
881         };
882         static const struct blobmsg_policy notify_policy[__NOTIFY_MAX] = {
883                 [NOTIFY_ATTR_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_INT32 },
884                 [NOTIFY_ATTR_VIF] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
885                 [NOTIFY_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
886         };
887         struct wireless_interface *vif = NULL;
888         struct blob_attr *tb[__NOTIFY_MAX];
889         struct blob_attr *cur, **pdata;
890
891         blobmsg_parse(notify_policy, __NOTIFY_MAX, tb, blob_data(data), blob_len(data));
892
893         if (!tb[NOTIFY_ATTR_COMMAND])
894                 return UBUS_STATUS_INVALID_ARGUMENT;
895
896         if ((cur = tb[NOTIFY_ATTR_VIF]) != NULL) {
897                 vif = vlist_find(&wdev->interfaces, blobmsg_data(cur), vif, node);
898                 if (!vif)
899                         return UBUS_STATUS_NOT_FOUND;
900         }
901
902         cur = tb[NOTIFY_ATTR_DATA];
903         if (!cur)
904                 return UBUS_STATUS_INVALID_ARGUMENT;
905
906         switch (blobmsg_get_u32(tb[NOTIFY_ATTR_COMMAND])) {
907         case NOTIFY_CMD_UP:
908                 if (vif)
909                         return UBUS_STATUS_INVALID_ARGUMENT;
910
911                 if (wdev->state != IFS_SETUP)
912                         return UBUS_STATUS_PERMISSION_DENIED;
913
914                 wireless_device_mark_up(wdev);
915                 break;
916         case NOTIFY_CMD_SET_DATA:
917                 if (vif)
918                         pdata = &vif->data;
919                 else
920                         pdata = &wdev->data;
921
922                 if (*pdata)
923                         return UBUS_STATUS_INVALID_ARGUMENT;
924
925                 *pdata = blob_memdup(cur);
926                 if (vif)
927                         wireless_interface_set_data(vif);
928                 break;
929         case NOTIFY_CMD_PROCESS_ADD:
930                 return wireless_device_add_process(wdev, cur);
931         case NOTIFY_CMD_PROCESS_KILL_ALL:
932                 return wireless_device_process_kill_all(wdev, cur, req);
933         case NOTIFY_CMD_SET_RETRY:
934                 return wireless_device_set_retry(wdev, cur);
935         default:
936                 return UBUS_STATUS_INVALID_ARGUMENT;
937         }
938
939         return 0;
940 }
941
942 void
943 wireless_start_pending(void)
944 {
945         struct wireless_device *wdev;
946
947         vlist_for_each_element(&wireless_devices, wdev, node)
948                 if (wdev->autostart)
949                         __wireless_device_set_up(wdev);
950 }