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