a21bf54d6f5dbfe20bf2686f3e9f72445bdac5c3
[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
15 /* The wireless configuration is projected on the following objects
16 *
17 * 1. wireless device
18 * 2. wireless interface
19 * 3. wireless vlan
20 * 4. wireless station
21 *
22 * A wireless device is a phy or simplified a wireless card.
23 * A wireless interface is a SSID on a phy.
24 * A wireless vlan can be assigned to a wireless interface. A wireless interface can
25 * have multiple vlans.
26 * A wireless station is a client connected to an wireless interface.
27 */
28
29 #include <signal.h>
30 #include "netifd.h"
31 #include "wireless.h"
32 #include "handler.h"
33 #include "ubus.h"
34
35 #define WIRELESS_SETUP_RETRY 3
36
37 struct vlist_tree wireless_devices;
38 struct avl_tree wireless_drivers;
39 static struct blob_buf b;
40 static int drv_fd;
41 static LIST_HEAD(handlers);
42 static bool handler_pending;
43
44 enum {
45 WDEV_ATTR_DISABLED,
46 WDEV_ATTR_RECONF,
47 WDEV_ATTR_SERIALIZE,
48 __WDEV_ATTR_MAX,
49 };
50
51 static const struct blobmsg_policy wdev_policy[__WDEV_ATTR_MAX] = {
52 [WDEV_ATTR_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL },
53 [WDEV_ATTR_RECONF] = { .name = "reconf", .type = BLOBMSG_TYPE_BOOL },
54 [WDEV_ATTR_SERIALIZE] = { .name = "serialize", .type = BLOBMSG_TYPE_BOOL },
55 };
56
57 static const struct uci_blob_param_list wdev_param = {
58 .n_params = ARRAY_SIZE(wdev_policy),
59 .params = wdev_policy,
60 };
61
62 enum {
63 VIF_ATTR_DISABLED,
64 VIF_ATTR_NETWORK,
65 VIF_ATTR_NETWORK_VLAN,
66 VIF_ATTR_ISOLATE,
67 VIF_ATTR_MODE,
68 VIF_ATTR_PROXYARP,
69 VIF_ATTR_MCAST_TO_UCAST,
70 __VIF_ATTR_MAX,
71 };
72
73 static const struct blobmsg_policy vif_policy[__VIF_ATTR_MAX] = {
74 [VIF_ATTR_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL },
75 [VIF_ATTR_NETWORK] = { .name = "network", .type = BLOBMSG_TYPE_ARRAY },
76 [VIF_ATTR_NETWORK_VLAN] = { .name = "network_vlan", .type = BLOBMSG_TYPE_ARRAY },
77 [VIF_ATTR_ISOLATE] = { .name = "isolate", .type = BLOBMSG_TYPE_BOOL },
78 [VIF_ATTR_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_STRING },
79 [VIF_ATTR_PROXYARP] = { .name = "proxy_arp", .type = BLOBMSG_TYPE_BOOL },
80 [VIF_ATTR_MCAST_TO_UCAST] = { .name = "multicast_to_unicast", .type = BLOBMSG_TYPE_BOOL },
81 };
82
83 static const struct uci_blob_param_list vif_param = {
84 .n_params = ARRAY_SIZE(vif_policy),
85 .params = vif_policy,
86 };
87
88 enum {
89 VLAN_ATTR_DISABLED,
90 VLAN_ATTR_NETWORK,
91 VLAN_ATTR_NETWORK_VLAN,
92 VLAN_ATTR_ISOLATE,
93 VLAN_ATTR_MCAST_TO_UCAST,
94 __VLAN_ATTR_MAX,
95 };
96
97 static const struct blobmsg_policy vlan_policy[__VLAN_ATTR_MAX] = {
98 [VLAN_ATTR_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL },
99 [VLAN_ATTR_NETWORK] = { .name = "network", .type = BLOBMSG_TYPE_ARRAY },
100 [VLAN_ATTR_NETWORK_VLAN] = { .name = "network_vlan", .type = BLOBMSG_TYPE_ARRAY },
101 [VLAN_ATTR_ISOLATE] = { .name = "isolate", .type = BLOBMSG_TYPE_BOOL },
102 [VLAN_ATTR_MCAST_TO_UCAST] = { .name = "multicast_to_unicast", .type = BLOBMSG_TYPE_BOOL },
103 };
104
105 static const struct uci_blob_param_list vlan_param = {
106 .n_params = ARRAY_SIZE(vlan_policy),
107 .params = vlan_policy,
108 };
109
110 enum {
111 STA_ATTR_DISABLED,
112 __STA_ATTR_MAX,
113 };
114
115 static const struct blobmsg_policy sta_policy[__STA_ATTR_MAX] = {
116 [STA_ATTR_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL },
117 };
118
119 static const struct uci_blob_param_list station_param = {
120 .n_params = ARRAY_SIZE(sta_policy),
121 .params = sta_policy,
122 };
123
124 static void
125 wireless_handler_stop(struct wireless_device *wdev)
126 {
127 if (wdev->handler_pending) {
128 wdev->handler_pending = false;
129 list_del(&wdev->handler);
130 }
131 }
132
133 static void
134 put_container(struct blob_buf *buf, struct blob_attr *attr, const char *name)
135 {
136 void *c = blobmsg_open_table(buf, name);
137 blob_put_raw(buf, blob_data(attr), blob_len(attr));
138 blobmsg_close_table(buf, c);
139 }
140
141 static void
142 vif_config_add_bridge(struct blob_buf *buf, struct blob_attr *networks, bool prepare)
143 {
144 struct interface *iface;
145 struct device *dev = NULL, *orig_dev;
146 struct blob_attr *cur;
147 const char *network;
148 size_t rem;
149
150 if (!networks)
151 return;
152
153 blobmsg_for_each_attr(cur, networks, rem) {
154 network = blobmsg_data(cur);
155
156 iface = vlist_find(&interfaces, network, iface, node);
157 if (!iface)
158 continue;
159
160 dev = iface->main_dev.dev;
161 if (!dev)
162 return;
163
164 if (!dev->hotplug_ops)
165 return;
166 }
167
168 if (!dev)
169 return;
170
171 orig_dev = dev;
172 if (dev->hotplug_ops && dev->hotplug_ops->prepare)
173 dev->hotplug_ops->prepare(dev, &dev);
174
175 if (!dev || !dev->type->bridge_capability)
176 return;
177
178 blobmsg_add_string(buf, "bridge", dev->ifname);
179 blobmsg_add_string(buf, "bridge-ifname", orig_dev->ifname);
180
181 if (dev->settings.flags & DEV_OPT_MULTICAST_TO_UNICAST)
182 blobmsg_add_u8(buf, "multicast_to_unicast",
183 dev->settings.multicast_to_unicast);
184 }
185
186 static void
187 prepare_config(struct wireless_device *wdev, struct blob_buf *buf, bool up)
188 {
189 struct wireless_interface *vif;
190 struct wireless_vlan *vlan;
191 struct wireless_station *sta;
192 void *l, *i, *j, *k;
193
194 blob_buf_init(&b, 0);
195 put_container(&b, wdev->config, "config");
196 if (wdev->data)
197 blobmsg_add_blob(&b, wdev->data);
198
199 l = blobmsg_open_table(&b, "interfaces");
200 vlist_for_each_element(&wdev->interfaces, vif, node) {
201 if (vif->disabled)
202 continue;
203
204 i = blobmsg_open_table(&b, vif->name);
205 vif_config_add_bridge(&b, vif->network, up);
206 put_container(&b, vif->config, "config");
207 if (vif->data)
208 blobmsg_add_blob(&b, vif->data);
209
210 j = blobmsg_open_table(&b, "vlans");
211 vlist_for_each_element(&vif->vlans, vlan, node) {
212 k = blobmsg_open_table(&b, vlan->name);
213 vif_config_add_bridge(&b, vlan->network, up);
214 put_container(&b, vlan->config, "config");
215 if (vlan->data)
216 blobmsg_add_blob(&b, vlan->data);
217 blobmsg_close_table(&b, k);
218 }
219 blobmsg_close_table(&b, j);
220
221 j = blobmsg_open_table(&b, "stas");
222 vlist_for_each_element(&vif->stations, sta, node) {
223 k = blobmsg_open_table(&b, sta->name);
224 put_container(&b, sta->config, "config");
225 if (sta->data)
226 blobmsg_add_blob(&b, sta->data);
227 blobmsg_close_table(&b, k);
228 }
229 blobmsg_close_table(&b, j);
230 blobmsg_close_table(&b, i);
231 }
232 blobmsg_close_table(&b, l);
233
234 }
235
236 static bool
237 wireless_process_check(struct wireless_process *proc)
238 {
239 return check_pid_path(proc->pid, proc->exe);
240 }
241
242 static void
243 wireless_complete_kill_request(struct wireless_device *wdev)
244 {
245 if (!wdev->kill_request)
246 return;
247
248 ubus_complete_deferred_request(ubus_ctx, wdev->kill_request, 0);
249 free(wdev->kill_request);
250 wdev->kill_request = NULL;
251 }
252
253 static void
254 wireless_process_free(struct wireless_device *wdev, struct wireless_process *proc)
255 {
256 D(WIRELESS, "Wireless device '%s' free pid %d\n", wdev->name, proc->pid);
257 list_del(&proc->list);
258 free(proc);
259
260 if (list_empty(&wdev->script_proc))
261 wireless_complete_kill_request(wdev);
262 }
263
264 static void
265 wireless_close_script_proc_fd(struct wireless_device *wdev)
266 {
267 if (wdev->script_proc_fd.fd < 0)
268 return;
269
270 uloop_fd_delete(&wdev->script_proc_fd);
271 close(wdev->script_proc_fd.fd);
272 wdev->script_proc_fd.fd = -1;
273 }
274
275 static void
276 wireless_process_kill_all(struct wireless_device *wdev, int signal, bool free)
277 {
278 struct wireless_process *proc, *tmp;
279
280 list_for_each_entry_safe(proc, tmp, &wdev->script_proc, list) {
281 bool check = wireless_process_check(proc);
282
283 if (check && !proc->keep) {
284 D(WIRELESS, "Wireless device '%s' kill pid %d\n", wdev->name, proc->pid);
285 kill(proc->pid, signal);
286 }
287
288 if (free || !check)
289 wireless_process_free(wdev, proc);
290 }
291
292 if (free)
293 wireless_close_script_proc_fd(wdev);
294 }
295
296 static void
297 wireless_device_free_state(struct wireless_device *wdev)
298 {
299 struct wireless_interface *vif;
300 struct wireless_vlan *vlan;
301 struct wireless_station *sta;
302
303 wireless_handler_stop(wdev);
304 uloop_timeout_cancel(&wdev->script_check);
305 uloop_timeout_cancel(&wdev->timeout);
306 wireless_complete_kill_request(wdev);
307 free(wdev->data);
308 wdev->data = NULL;
309 vlist_for_each_element(&wdev->interfaces, vif, node) {
310 free(vif->data);
311 vif->data = NULL;
312 vif->ifname = NULL;
313 vlist_for_each_element(&vif->vlans, vlan, node) {
314 free(vlan->data);
315 vlan->data = NULL;
316 vlan->ifname = NULL;
317 }
318 vlist_for_each_element(&vif->stations, sta, node) {
319 free(sta->data);
320 sta->data = NULL;
321 }
322 }
323 }
324
325 static void wireless_device_set_mcast_to_unicast(struct device *dev, int val)
326 {
327 if (val < 0) {
328 dev->settings.flags &= ~DEV_OPT_MULTICAST_TO_UNICAST;
329 return;
330 }
331
332 dev->settings.multicast_to_unicast = !!val;
333 dev->settings.flags |= DEV_OPT_MULTICAST_TO_UNICAST;
334 }
335
336 static void wireless_interface_handle_link(struct wireless_interface *vif, const char *ifname, bool up)
337 {
338 struct interface *iface;
339 struct blob_attr *cur;
340 const char *network;
341 size_t rem;
342
343 if (!vif->network || !vif->ifname)
344 return;
345
346 if (!ifname)
347 ifname = vif->ifname;
348
349 if (up) {
350 struct device *dev = __device_get(ifname, 2, false);
351
352 if (dev && !strcmp(ifname, vif->ifname)) {
353 dev->wireless_isolate = vif->isolate;
354 dev->wireless_proxyarp = vif->proxyarp;
355 dev->wireless = true;
356 dev->wireless_ap = vif->ap_mode;
357 wireless_device_set_mcast_to_unicast(dev, vif->multicast_to_unicast);
358 dev->bpdu_filter = dev->wireless_ap;
359 }
360 }
361
362 blobmsg_for_each_attr(cur, vif->network, rem) {
363 network = blobmsg_data(cur);
364
365 iface = vlist_find(&interfaces, network, iface, node);
366 if (!iface)
367 continue;
368
369 interface_handle_link(iface, ifname, vif->network_vlan, up, true);
370 }
371 }
372
373 static void wireless_vlan_handle_link(struct wireless_vlan *vlan, bool up)
374 {
375 struct interface *iface;
376 struct blob_attr *cur;
377 const char *network;
378 size_t rem;
379
380 if (!vlan->network || !vlan->ifname)
381 return;
382
383 if (up) {
384 struct device *dev = device_get(vlan->ifname, 2);
385 if (dev) {
386 dev->wireless_isolate = vlan->isolate;
387 dev->wireless = true;
388 dev->wireless_ap = true;
389 dev->bpdu_filter = true;
390 wireless_device_set_mcast_to_unicast(dev, vlan->multicast_to_unicast);
391 }
392 }
393
394 blobmsg_for_each_attr(cur, vlan->network, rem) {
395 network = blobmsg_data(cur);
396
397 iface = vlist_find(&interfaces, network, iface, node);
398 if (!iface)
399 continue;
400
401 interface_handle_link(iface, vlan->ifname, vlan->network_vlan, up, true);
402 }
403 }
404
405 static void
406 wireless_device_setup_cancel(struct wireless_device *wdev)
407 {
408 if (wdev->cancel)
409 return;
410
411 wireless_handler_stop(wdev);
412 D(WIRELESS, "Cancel wireless device '%s' setup\n", wdev->name);
413 wdev->cancel = true;
414 uloop_timeout_set(&wdev->timeout, 10 * 1000);
415 }
416
417 static void
418 wireless_device_run_handler(struct wireless_device *wdev, bool up)
419 {
420 const char *action = up ? "setup" : "teardown";
421 const char *argv[6];
422 char *config;
423 int i = 0;
424 int fds[2] = { -1, -1 };
425
426 wireless_handler_stop(wdev);
427
428 if (handler_pending && wdev->serialize) {
429 wdev->handler_action = up;
430 wdev->handler_pending = true;
431 list_add_tail(&wdev->handler, &handlers);
432 return;
433 }
434 if (wdev->serialize)
435 handler_pending = true;
436
437 D(WIRELESS, "Wireless device '%s' run %s handler\n", wdev->name, action);
438 if (!up && wdev->prev_config) {
439 config = blobmsg_format_json(wdev->prev_config, true);
440 free(wdev->prev_config);
441 wdev->prev_config = NULL;
442 } else {
443 prepare_config(wdev, &b, up);
444 free(wdev->prev_config);
445 wdev->prev_config = up ? blob_memdup(b.head) : NULL;
446 config = blobmsg_format_json(b.head, true);
447 }
448
449 argv[i++] = wdev->drv->script;
450 argv[i++] = wdev->drv->name;
451 argv[i++] = action;
452 argv[i++] = wdev->name;
453 argv[i++] = config;
454 argv[i] = NULL;
455
456 if (up && pipe(fds) == 0) {
457 if (wdev->script_proc_fd.fd >= 0)
458 wireless_close_script_proc_fd(wdev);
459
460 wdev->script_proc_fd.fd = fds[0];
461 uloop_fd_add(&wdev->script_proc_fd,
462 ULOOP_READ | ULOOP_EDGE_TRIGGER);
463 }
464
465 netifd_start_process(argv, NULL, &wdev->script_task);
466
467 if (fds[1] >= 0)
468 close(fds[1]);
469
470 free(config);
471 }
472
473 static void
474 wireless_handler_next(void)
475 {
476 struct wireless_device *wdev;
477
478 if (handler_pending)
479 return;
480 if (list_empty(&handlers))
481 return;
482 wdev = list_first_entry(&handlers, struct wireless_device, handler);
483 list_del(&wdev->handler);
484 wdev->handler_pending = false;
485 wireless_device_run_handler(wdev, wdev->handler_action);
486 }
487
488 static void
489 __wireless_device_set_up(struct wireless_device *wdev, int force)
490 {
491 if (wdev->disabled)
492 return;
493
494 if (wdev->retry_setup_failed)
495 return;
496
497 if (!wdev->autostart)
498 return;
499
500 if ((!force && wdev->state != IFS_DOWN) || config_init)
501 return;
502
503 wdev->state = IFS_SETUP;
504 wireless_device_run_handler(wdev, true);
505 }
506
507 static void
508 wireless_device_free(struct wireless_device *wdev)
509 {
510 wireless_handler_stop(wdev);
511 vlist_flush_all(&wdev->interfaces);
512 avl_delete(&wireless_devices.avl, &wdev->node.avl);
513 free(wdev->config);
514 free(wdev->prev_config);
515 free(wdev);
516 }
517
518 static void
519 wdev_handle_config_change(struct wireless_device *wdev)
520 {
521 enum interface_config_state state = wdev->config_state;
522
523 switch(state) {
524 case IFC_RELOAD:
525 wdev->retry = WIRELESS_SETUP_RETRY;
526 wdev->retry_setup_failed = false;
527 fallthrough;
528 case IFC_NORMAL:
529 __wireless_device_set_up(wdev, 0);
530
531 wdev->config_state = IFC_NORMAL;
532 break;
533 case IFC_REMOVE:
534 wireless_device_free(wdev);
535 break;
536 }
537 }
538
539 static void
540 wireless_device_mark_down(struct wireless_device *wdev)
541 {
542 struct wireless_interface *vif;
543 struct wireless_vlan *vlan;
544
545 netifd_log_message(L_NOTICE, "Wireless device '%s' is now down\n", wdev->name);
546
547
548 vlist_for_each_element(&wdev->interfaces, vif, node) {
549 wireless_interface_handle_link(vif, NULL, false);
550 vlist_for_each_element(&vif->vlans, vlan, node)
551 wireless_vlan_handle_link(vlan, false);
552 }
553
554 wireless_process_kill_all(wdev, SIGTERM, true);
555
556 wdev->cancel = false;
557 wdev->state = IFS_DOWN;
558 wireless_device_free_state(wdev);
559 wdev_handle_config_change(wdev);
560 }
561
562 /* timeout callback to protect the tear down */
563 static void
564 wireless_device_setup_timeout(struct uloop_timeout *timeout)
565 {
566 struct wireless_device *wdev = container_of(timeout, struct wireless_device, timeout);
567
568 if (wdev->handler_pending) {
569 wdev->handler_pending = false;
570 list_del(&wdev->handler);
571 }
572 netifd_kill_process(&wdev->script_task);
573 wdev->script_task.cb(&wdev->script_task, -1);
574 wireless_device_mark_down(wdev);
575 }
576
577 void
578 wireless_device_set_up(struct wireless_device *wdev)
579 {
580 wdev->retry = WIRELESS_SETUP_RETRY;
581 wdev->autostart = true;
582 __wireless_device_set_up(wdev, 0);
583 }
584
585 void
586 wireless_device_reconf(struct wireless_device *wdev)
587 {
588 wdev->retry = WIRELESS_SETUP_RETRY;
589 wdev->autostart = true;
590 __wireless_device_set_up(wdev, wdev->reconf && (wdev->state == IFS_UP));
591 }
592
593 static void
594 __wireless_device_set_down(struct wireless_device *wdev)
595 {
596 if (wdev->state == IFS_TEARDOWN || wdev->state == IFS_DOWN)
597 return;
598
599 if (wdev->script_task.uloop.pending) {
600 wireless_device_setup_cancel(wdev);
601 return;
602 }
603
604 wdev->state = IFS_TEARDOWN;
605 wireless_device_run_handler(wdev, false);
606 }
607
608 /* ubus callback network.wireless.notify, command = up */
609 static void
610 wireless_device_mark_up(struct wireless_device *wdev)
611 {
612 struct wireless_interface *vif;
613 struct wireless_vlan *vlan;
614
615 if (wdev->cancel) {
616 wdev->cancel = false;
617 __wireless_device_set_down(wdev);
618 return;
619 }
620
621 netifd_log_message(L_NOTICE, "Wireless device '%s' is now up\n", wdev->name);
622 wdev->retry = WIRELESS_SETUP_RETRY;
623 wdev->state = IFS_UP;
624 vlist_for_each_element(&wdev->interfaces, vif, node) {
625 wireless_interface_handle_link(vif, NULL, true);
626 vlist_for_each_element(&vif->vlans, vlan, node)
627 wireless_vlan_handle_link(vlan, true);
628 }
629 }
630
631 static void
632 wireless_device_retry_setup(struct wireless_device *wdev)
633 {
634 if (wdev->state == IFS_TEARDOWN || wdev->state == IFS_DOWN || wdev->cancel)
635 return;
636
637 netifd_log_message(wdev->retry ? L_WARNING : L_CRIT,
638 "Wireless device '%s' setup failed, retry=%d\n",
639 wdev->name, wdev->retry);
640 if (--wdev->retry < 0)
641 wdev->retry_setup_failed = true;
642
643 __wireless_device_set_down(wdev);
644 }
645
646 static void
647 wireless_device_script_task_cb(struct netifd_process *proc, int ret)
648 {
649 struct wireless_device *wdev = container_of(proc, struct wireless_device, script_task);
650
651 switch (wdev->state) {
652 case IFS_SETUP:
653 wireless_device_retry_setup(wdev);
654 break;
655 case IFS_TEARDOWN:
656 wireless_device_mark_down(wdev);
657 break;
658 default:
659 break;
660 }
661
662 if (wdev->serialize) {
663 handler_pending = false;
664 wireless_handler_next();
665 }
666 }
667
668 void
669 wireless_device_set_down(struct wireless_device *wdev)
670 {
671 wdev->retry_setup_failed = false;
672 wdev->autostart = false;
673 __wireless_device_set_down(wdev);
674 }
675
676 static void
677 wdev_set_config_state(struct wireless_device *wdev, enum interface_config_state s)
678 {
679 if (wdev->config_state != IFC_NORMAL)
680 return;
681
682 wdev->config_update = false;
683 if (!wdev->disabled && s == IFC_RELOAD && wdev->reconf && wdev->state == IFS_UP) {
684 wireless_device_reconf(wdev);
685 return;
686 }
687
688 wdev->config_state = s;
689 if (wdev->state == IFS_DOWN)
690 wdev_handle_config_change(wdev);
691 else
692 __wireless_device_set_down(wdev);
693 }
694
695 static void
696 wdev_change_config(struct wireless_device *wdev, struct wireless_device *wd_new)
697 {
698 struct blob_attr *new_config = wd_new->config;
699 bool disabled = wd_new->disabled;
700
701 wdev->reconf = wd_new->reconf;
702 wdev->serialize = wd_new->serialize;
703 free(wd_new);
704
705 if (blob_attr_equal(wdev->config, new_config) && wdev->disabled == disabled)
706 return;
707
708 D(WIRELESS, "Update configuration of wireless device '%s'\n", wdev->name);
709 free(wdev->config);
710 wdev->config = blob_memdup(new_config);
711 wdev->disabled = disabled;
712 wdev->config_update = true;
713 }
714
715 static void
716 wdev_create(struct wireless_device *wdev)
717 {
718 wdev->retry = WIRELESS_SETUP_RETRY;
719 wdev->config = blob_memdup(wdev->config);
720 }
721
722 /* vlist update call for wireless device list */
723 static void
724 wdev_update(struct vlist_tree *tree, struct vlist_node *node_new,
725 struct vlist_node *node_old)
726 {
727 struct wireless_device *wd_old = container_of(node_old, struct wireless_device, node);
728 struct wireless_device *wd_new = container_of(node_new, struct wireless_device, node);
729
730 if (wd_old && wd_new) {
731 D(WIRELESS, "Update wireless device '%s'\n", wd_old->name);
732 wdev_change_config(wd_old, wd_new);
733 } else if (wd_old) {
734 D(WIRELESS, "Delete wireless device '%s'\n", wd_old->name);
735 wdev_set_config_state(wd_old, IFC_REMOVE);
736 } else if (wd_new) {
737 D(WIRELESS, "Create wireless device '%s'\n", wd_new->name);
738 wdev_create(wd_new);
739 }
740 }
741
742 /* wireless netifd script handler */
743 static void
744 wireless_add_handler(const char *script, const char *name, json_object *obj)
745 {
746 struct wireless_driver *drv;
747 char *name_str, *script_str;
748 json_object *dev_config_obj, *iface_config_obj, *vlan_config_obj, *station_config_obj;
749 struct uci_blob_param_list *dev_config, *iface_config, *vlan_config, *station_config;
750
751 dev_config_obj = json_get_field(obj, "device", json_type_array);
752 iface_config_obj = json_get_field(obj, "iface", json_type_array);
753 vlan_config_obj = json_get_field(obj, "vlan", json_type_array);
754 station_config_obj = json_get_field(obj, "station", json_type_array);
755
756 if (!dev_config_obj || !iface_config_obj || !vlan_config_obj || !station_config_obj)
757 return;
758
759 drv = calloc_a(sizeof(*drv),
760 &dev_config, sizeof(*dev_config) + sizeof(void *),
761 &iface_config, sizeof(*iface_config) + sizeof(void *),
762 &vlan_config, sizeof(*vlan_config) + sizeof(void *),
763 &station_config, sizeof(*station_config) + sizeof(void *),
764 &name_str, strlen(name) + 1,
765 &script_str, strlen(script) + 1);
766
767 drv->name = strcpy(name_str, name);
768 drv->script = strcpy(script_str, script);
769
770 dev_config->n_next = 1;
771 dev_config->next[0] = &wdev_param;
772 drv->device.config = dev_config;
773
774 iface_config->n_next = 1;
775 iface_config->next[0] = &vif_param;
776 drv->interface.config = iface_config;
777
778 vlan_config->n_next = 1;
779 vlan_config->next[0] = &vlan_param;
780 drv->vlan.config = vlan_config;
781
782 station_config->n_next = 1;
783 station_config->next[0] = &station_param;
784 drv->station.config = station_config;
785
786 drv->device.buf = netifd_handler_parse_config(drv->device.config, dev_config_obj);
787 drv->interface.buf = netifd_handler_parse_config(drv->interface.config, iface_config_obj);
788 drv->vlan.buf = netifd_handler_parse_config(drv->vlan.config, vlan_config_obj);
789 drv->station.buf = netifd_handler_parse_config(drv->station.config, station_config_obj);
790
791 drv->node.key = drv->name;
792 avl_insert(&wireless_drivers, &drv->node);
793 D(WIRELESS, "Add handler for script %s: %s\n", script, name);
794 }
795
796 void wireless_init(void)
797 {
798 vlist_init(&wireless_devices, avl_strcmp, wdev_update);
799 wireless_devices.keep_old = true;
800 wireless_devices.no_delete = true;
801
802 avl_init(&wireless_drivers, avl_strcmp, false, NULL);
803 drv_fd = netifd_open_subdir("wireless");
804 if (drv_fd < 0)
805 return;
806
807 netifd_init_script_handlers(drv_fd, wireless_add_handler);
808 }
809
810 /* parse blob config into the wireless interface object */
811 static void
812 wireless_interface_init_config(struct wireless_interface *vif)
813 {
814 struct blob_attr *tb[__VIF_ATTR_MAX];
815 struct blob_attr *cur;
816
817 vif->network = NULL;
818 blobmsg_parse(vif_policy, __VIF_ATTR_MAX, tb, blob_data(vif->config), blob_len(vif->config));
819
820 if ((cur = tb[VIF_ATTR_NETWORK]))
821 vif->network = cur;
822
823 if ((cur = tb[VIF_ATTR_NETWORK_VLAN]))
824 vif->network_vlan = cur;
825
826 cur = tb[VIF_ATTR_MODE];
827 vif->ap_mode = cur && !strcmp(blobmsg_get_string(cur), "ap");
828
829 cur = tb[VIF_ATTR_ISOLATE];
830 vif->isolate = vif->ap_mode && cur && blobmsg_get_bool(cur);
831
832 cur = tb[VIF_ATTR_PROXYARP];
833 vif->proxyarp = vif->ap_mode && cur && blobmsg_get_bool(cur);
834
835 cur = tb[VIF_ATTR_MCAST_TO_UCAST];
836 vif->multicast_to_unicast = cur ? blobmsg_get_bool(cur) : -1;
837 }
838
839 static void
840 vif_free(struct wireless_interface *vif)
841 {
842 vlist_flush_all(&vif->vlans);
843 vlist_flush_all(&vif->stations);
844 free((void *) vif->section);
845 free(vif->config);
846 free(vif);
847 }
848
849 /* vlist update call for wireless interface list */
850 static void
851 vif_update(struct vlist_tree *tree, struct vlist_node *node_new,
852 struct vlist_node *node_old)
853 {
854 struct wireless_interface *vif_old = container_of(node_old, struct wireless_interface, node);
855 struct wireless_interface *vif_new = container_of(node_new, struct wireless_interface, node);
856 struct wireless_device *wdev;
857
858 if (vif_old)
859 wdev = vif_old->wdev;
860 else
861 wdev = vif_new->wdev;
862
863 if (vif_old && vif_new) {
864 free((void *) vif_old->section);
865 vif_old->section = strdup(vif_new->section);
866 if (blob_attr_equal(vif_old->config, vif_new->config)) {
867 free(vif_new);
868 return;
869 }
870
871 D(WIRELESS, "Update wireless interface %s on device %s\n", vif_new->name, wdev->name);
872 wireless_interface_handle_link(vif_old, NULL, false);
873 free(vif_old->config);
874 vif_old->config = blob_memdup(vif_new->config);
875 wireless_interface_init_config(vif_old);
876 free(vif_new);
877 } else if (vif_new) {
878 D(WIRELESS, "Create new wireless interface %s on device %s\n", vif_new->name, wdev->name);
879 vif_new->section = strdup(vif_new->section);
880 vif_new->config = blob_memdup(vif_new->config);
881 wireless_interface_init_config(vif_new);
882 } else if (vif_old) {
883 D(WIRELESS, "Delete wireless interface %s on device %s\n", vif_old->name, wdev->name);
884 wireless_interface_handle_link(vif_old, NULL, false);
885 vif_free(vif_old);
886 }
887
888 wdev->config_update = true;
889 }
890
891 /* parse blob config into the vlan object */
892 static void
893 wireless_vlan_init_config(struct wireless_vlan *vlan)
894 {
895 struct blob_attr *tb[__VLAN_ATTR_MAX];
896 struct blob_attr *cur;
897
898 vlan->network = NULL;
899 blobmsg_parse(vlan_policy, __VLAN_ATTR_MAX, tb, blob_data(vlan->config), blob_len(vlan->config));
900
901 if ((cur = tb[VLAN_ATTR_NETWORK]))
902 vlan->network = cur;
903
904 if ((cur = tb[VLAN_ATTR_NETWORK_VLAN]))
905 vlan->network_vlan = cur;
906
907 cur = tb[VLAN_ATTR_ISOLATE];
908 if (cur)
909 vlan->isolate = blobmsg_get_bool(cur);
910
911 cur = tb[VLAN_ATTR_MCAST_TO_UCAST];
912 vlan->multicast_to_unicast = cur ? blobmsg_get_bool(cur) : -1;
913 }
914
915 /* vlist update call for vlan list */
916 static void
917 vlan_update(struct vlist_tree *tree, struct vlist_node *node_new,
918 struct vlist_node *node_old)
919 {
920 struct wireless_vlan *vlan_old = container_of_safe(node_old, struct wireless_vlan, node);
921 struct wireless_vlan *vlan_new = container_of_safe(node_new, struct wireless_vlan, node);
922 struct wireless_interface *vif = container_of(tree, struct wireless_interface, vlans);
923 struct wireless_device *wdev = vif->wdev;
924
925 if (vlan_old && vlan_new) {
926 free((void *) vlan_old->section);
927 vlan_old->section = strdup(vlan_new->section);
928 if (blob_attr_equal(vlan_old->config, vlan_new->config)) {
929 free(vlan_new);
930 return;
931 }
932
933 D(WIRELESS, "Update wireless vlan %s on device %s\n", vlan_new->name, wdev->name);
934 wireless_vlan_handle_link(vlan_old, false);
935 free(vlan_old->config);
936 vlan_old->config = blob_memdup(vlan_new->config);
937 vlan_old->isolate = vlan_new->isolate;
938 wireless_vlan_init_config(vlan_old);
939 free(vlan_new);
940 } else if (vlan_new) {
941 D(WIRELESS, "Create new wireless vlan %s on device %s\n", vlan_new->name, wdev->name);
942 vlan_new->section = strdup(vlan_new->section);
943 vlan_new->config = blob_memdup(vlan_new->config);
944 wireless_vlan_init_config(vlan_new);
945 } else if (vlan_old) {
946 D(WIRELESS, "Delete wireless vlan %s on device %s\n", vlan_old->name, wdev->name);
947 wireless_vlan_handle_link(vlan_old, false);
948 free((void *) vlan_old->section);
949 free(vlan_old->config);
950 free(vlan_old);
951 }
952
953 wdev->config_update = true;
954 }
955
956 /* vlist update call for station list */
957 static void
958 station_update(struct vlist_tree *tree, struct vlist_node *node_new,
959 struct vlist_node *node_old)
960 {
961 struct wireless_station *sta_old = container_of_safe(node_old, struct wireless_station, node);
962 struct wireless_station *sta_new = container_of_safe(node_new, struct wireless_station, node);
963 struct wireless_interface *vif = container_of(tree, struct wireless_interface, stations);
964 struct wireless_device *wdev = vif->wdev;
965
966 if (sta_old && sta_new) {
967 free((void *) sta_old->section);
968 sta_old->section = strdup(sta_new->section);
969 if (blob_attr_equal(sta_old->config, sta_new->config)) {
970 free(sta_new);
971 return;
972 }
973
974 D(WIRELESS, "Update wireless station %s on device %s\n", sta_new->name, wdev->name);
975 free(sta_old->config);
976 sta_old->config = blob_memdup(sta_new->config);
977 free(sta_new);
978 } else if (sta_new) {
979 D(WIRELESS, "Create new wireless station %s on device %s\n", sta_new->name, wdev->name);
980 sta_new->section = strdup(sta_new->section);
981 sta_new->config = blob_memdup(sta_new->config);
982 } else if (sta_old) {
983 D(WIRELESS, "Delete wireless station %s on device %s\n", sta_old->name, wdev->name);
984 free((void *) sta_old->section);
985 free(sta_old->config);
986 free(sta_old);
987 }
988
989 wdev->config_update = true;
990 }
991
992 static void
993 wireless_proc_poll_fd(struct uloop_fd *fd, unsigned int events)
994 {
995 struct wireless_device *wdev = container_of(fd, struct wireless_device, script_proc_fd);
996 char buf[128];
997
998 while (1) {
999 int b = read(fd->fd, buf, sizeof(buf));
1000 if (b < 0) {
1001 if (errno == EINTR)
1002 continue;
1003
1004 if (errno == EAGAIN)
1005 return;
1006
1007 goto done;
1008 }
1009
1010 if (!b)
1011 goto done;
1012 }
1013
1014 done:
1015 uloop_timeout_set(&wdev->script_check, 0);
1016 wireless_close_script_proc_fd(wdev);
1017 }
1018
1019 /* watchdog and garbage collector for wireless processes.
1020 * It cleans up terminated processes. If a process is a requirement for the wireless device, it retries the setup */
1021 static void
1022 wireless_device_check_script_tasks(struct uloop_timeout *timeout)
1023 {
1024 struct wireless_device *wdev = container_of(timeout, struct wireless_device, script_check);
1025 struct wireless_process *proc, *tmp;
1026 bool restart = false;
1027
1028 list_for_each_entry_safe(proc, tmp, &wdev->script_proc, list) {
1029 if (wireless_process_check(proc))
1030 continue;
1031
1032 D(WIRELESS, "Wireless device '%s' pid %d has terminated\n", wdev->name, proc->pid);
1033 if (proc->required)
1034 restart = true;
1035
1036 wireless_process_free(wdev, proc);
1037 }
1038
1039 if (restart)
1040 wireless_device_retry_setup(wdev);
1041 else
1042 uloop_timeout_set(&wdev->script_check, 1000);
1043 }
1044
1045 /* creates a wireless device object. Called by config */
1046 void
1047 wireless_device_create(struct wireless_driver *drv, const char *name, struct blob_attr *data)
1048 {
1049 struct wireless_device *wdev;
1050 char *name_buf;
1051 struct blob_attr *tb[__WDEV_ATTR_MAX];
1052 struct blob_attr *cur;
1053
1054 blobmsg_parse(wdev_policy, __WDEV_ATTR_MAX, tb, blob_data(data), blob_len(data));
1055
1056 wdev = calloc_a(sizeof(*wdev), &name_buf, strlen(name) + 1);
1057
1058 cur = tb[WDEV_ATTR_DISABLED];
1059 wdev->disabled = cur && blobmsg_get_bool(cur);
1060
1061 wdev->drv = drv;
1062 wdev->state = IFS_DOWN;
1063 wdev->config_state = IFC_NORMAL;
1064 wdev->name = strcpy(name_buf, name);
1065 wdev->config = data;
1066 wdev->handler_pending = false;
1067
1068 cur = tb[WDEV_ATTR_SERIALIZE];
1069 wdev->serialize = cur && blobmsg_get_bool(cur);
1070
1071 cur = tb[WDEV_ATTR_RECONF];
1072 wdev->reconf = !cur || blobmsg_get_bool(cur);
1073
1074 wdev->retry_setup_failed = false;
1075 wdev->autostart = true;
1076 INIT_LIST_HEAD(&wdev->script_proc);
1077 vlist_init(&wdev->interfaces, avl_strcmp, vif_update);
1078 wdev->interfaces.keep_old = true;
1079
1080 wdev->timeout.cb = wireless_device_setup_timeout;
1081 wdev->script_task.cb = wireless_device_script_task_cb;
1082 wdev->script_task.dir_fd = drv_fd;
1083 wdev->script_task.log_prefix = wdev->name;
1084
1085 wdev->script_proc_fd.fd = -1;
1086 wdev->script_proc_fd.cb = wireless_proc_poll_fd;
1087
1088 wdev->script_check.cb = wireless_device_check_script_tasks;
1089
1090 vlist_add(&wireless_devices, &wdev->node, wdev->name);
1091 }
1092
1093 /* creates a wireless station object. Called by config */
1094 void
1095 wireless_station_create(struct wireless_interface *vif, struct blob_attr *data, const char *section)
1096 {
1097 struct wireless_station *sta;
1098 struct blob_attr *tb[__STA_ATTR_MAX];
1099 struct blob_attr *cur;
1100 char *name_buf;
1101 char name[8];
1102
1103 blobmsg_parse(sta_policy, __STA_ATTR_MAX, tb, blob_data(data), blob_len(data));
1104
1105 cur = tb[STA_ATTR_DISABLED];
1106 if (cur && blobmsg_get_bool(cur))
1107 return;
1108
1109 sprintf(name, "%d", vif->sta_idx++);
1110
1111 sta = calloc_a(sizeof(*sta),
1112 &name_buf, strlen(name) + 1);
1113 sta->name = strcpy(name_buf, name);
1114 sta->config = data;
1115 sta->section = section;
1116
1117 vlist_add(&vif->stations, &sta->node, sta->name);
1118 }
1119
1120 /* ubus callback network.wireless.status, runs for every interface, encode the station */
1121 static void
1122 wireless_station_status(struct wireless_station *sta, struct blob_buf *b)
1123 {
1124 void *i;
1125
1126 i = blobmsg_open_table(b, NULL);
1127 if (sta->section)
1128 blobmsg_add_string(b, "section", sta->section);
1129 put_container(b, sta->config, "config");
1130 blobmsg_close_table(b, i);
1131 }
1132
1133 /* create a vlan object. Called by config */
1134 void
1135 wireless_vlan_create(struct wireless_interface *vif, struct blob_attr *data, const char *section)
1136 {
1137 struct wireless_vlan *vlan;
1138 struct blob_attr *tb[__VLAN_ATTR_MAX];
1139 struct blob_attr *cur;
1140 char *name_buf;
1141 char name[8];
1142
1143 blobmsg_parse(vlan_policy, __VLAN_ATTR_MAX, tb, blob_data(data), blob_len(data));
1144
1145 cur = tb[VLAN_ATTR_DISABLED];
1146 if (cur && blobmsg_get_bool(cur))
1147 return;
1148
1149 sprintf(name, "%d", vif->vlan_idx++);
1150
1151 vlan = calloc_a(sizeof(*vlan), &name_buf, strlen(name) + 1);
1152 vlan->name = strcpy(name_buf, name);
1153 vlan->config = data;
1154 vlan->section = section;
1155
1156 vlist_add(&vif->vlans, &vlan->node, vlan->name);
1157 }
1158
1159 /* ubus callback network.wireless.status, runs for every interface, encode the vlan informations */
1160 static void
1161 wireless_vlan_status(struct wireless_vlan *vlan, struct blob_buf *b)
1162 {
1163 void *i;
1164
1165 i = blobmsg_open_table(b, NULL);
1166 if (vlan->section)
1167 blobmsg_add_string(b, "section", vlan->section);
1168 if (vlan->ifname)
1169 blobmsg_add_string(b, "ifname", vlan->ifname);
1170 put_container(b, vlan->config, "config");
1171 blobmsg_close_table(b, i);
1172 }
1173
1174 /* create a wireless interface object. Called by config */
1175 struct wireless_interface* wireless_interface_create(struct wireless_device *wdev, struct blob_attr *data, const char *section)
1176 {
1177 struct wireless_interface *vif;
1178 struct blob_attr *tb[__VIF_ATTR_MAX];
1179 struct blob_attr *cur;
1180 char *name_buf;
1181 char name[8];
1182
1183 blobmsg_parse(vif_policy, __VIF_ATTR_MAX, tb, blob_data(data), blob_len(data));
1184
1185 cur = tb[VIF_ATTR_DISABLED];
1186 if (cur && blobmsg_get_bool(cur))
1187 return NULL;
1188
1189 sprintf(name, "%d", wdev->vif_idx++);
1190
1191 vif = calloc_a(sizeof(*vif),
1192 &name_buf, strlen(name) + 1);
1193 vif->name = strcpy(name_buf, name);
1194 vif->wdev = wdev;
1195 vif->config = data;
1196 vif->section = section;
1197 vif->isolate = false;
1198
1199 vlist_init(&vif->vlans, avl_strcmp, vlan_update);
1200 vif->vlans.keep_old = true;
1201
1202 vlist_init(&vif->stations, avl_strcmp, station_update);
1203 vif->stations.keep_old = true;
1204
1205 vlist_add(&wdev->interfaces, &vif->node, vif->name);
1206
1207 return vlist_find(&wdev->interfaces, name, vif, node);
1208 }
1209
1210 /* ubus callback network.wireless.status, runs for every interface */
1211 static void
1212 wireless_interface_status(struct wireless_interface *iface, struct blob_buf *b)
1213 {
1214 struct wireless_vlan *vlan;
1215 struct wireless_station *sta;
1216 void *i, *j;
1217
1218 i = blobmsg_open_table(b, NULL);
1219 if (iface->section)
1220 blobmsg_add_string(b, "section", iface->section);
1221 if (iface->ifname)
1222 blobmsg_add_string(b, "ifname", iface->ifname);
1223 put_container(b, iface->config, "config");
1224 j = blobmsg_open_array(b, "vlans");
1225 vlist_for_each_element(&iface->vlans, vlan, node)
1226 wireless_vlan_status(vlan, b);
1227 blobmsg_close_array(b, j);
1228 j = blobmsg_open_array(b, "stations");
1229 vlist_for_each_element(&iface->stations, sta, node)
1230 wireless_station_status(sta, b);
1231 blobmsg_close_array(b, j);
1232 blobmsg_close_table(b, i);
1233 }
1234
1235 /* ubus callback network.wireless.status */
1236 void
1237 wireless_device_status(struct wireless_device *wdev, struct blob_buf *b)
1238 {
1239 struct wireless_interface *iface;
1240 void *c, *i;
1241
1242 c = blobmsg_open_table(b, wdev->name);
1243 blobmsg_add_u8(b, "up", wdev->state == IFS_UP);
1244 blobmsg_add_u8(b, "pending", wdev->state == IFS_SETUP || wdev->state == IFS_TEARDOWN);
1245 blobmsg_add_u8(b, "autostart", wdev->autostart);
1246 blobmsg_add_u8(b, "disabled", wdev->disabled);
1247 blobmsg_add_u8(b, "retry_setup_failed", wdev->retry_setup_failed);
1248 put_container(b, wdev->config, "config");
1249
1250 i = blobmsg_open_array(b, "interfaces");
1251 vlist_for_each_element(&wdev->interfaces, iface, node)
1252 wireless_interface_status(iface, b);
1253 blobmsg_close_array(b, i);
1254 blobmsg_close_table(b, c);
1255 }
1256
1257 /* ubus callback network.wireless.get_validate */
1258 void
1259 wireless_device_get_validate(struct wireless_device *wdev, struct blob_buf *b)
1260 {
1261 struct uci_blob_param_list *p;
1262 void *c, *d;
1263 int i;
1264
1265 c = blobmsg_open_table(b, wdev->name);
1266
1267 d = blobmsg_open_table(b, "device");
1268 p = wdev->drv->device.config;
1269 for (i = 0; i < p->n_params; i++)
1270 blobmsg_add_string(b, p->params[i].name, uci_get_validate_string(p, i));
1271 blobmsg_close_table(b, d);
1272
1273 d = blobmsg_open_table(b, "interface");
1274 p = wdev->drv->interface.config;
1275 for (i = 0; i < p->n_params; i++)
1276 blobmsg_add_string(b, p->params[i].name, uci_get_validate_string(p, i));
1277 blobmsg_close_table(b, d);
1278
1279 blobmsg_close_table(b, c);
1280 }
1281
1282 /* ubus callback network.wireless.notify, command = set data, for vif */
1283 static void
1284 wireless_interface_set_data(struct wireless_interface *vif)
1285 {
1286 enum {
1287 VIF_DATA_IFNAME,
1288 __VIF_DATA_MAX,
1289 };
1290 static const struct blobmsg_policy data_policy[__VIF_DATA_MAX] = {
1291 [VIF_DATA_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
1292 };
1293 struct blob_attr *tb[__VIF_DATA_MAX];
1294 struct blob_attr *cur;
1295
1296 blobmsg_parse(data_policy, __VIF_DATA_MAX, tb,
1297 blobmsg_data(vif->data), blobmsg_data_len(vif->data));
1298
1299 if ((cur = tb[VIF_DATA_IFNAME]))
1300 vif->ifname = blobmsg_data(cur);
1301 }
1302
1303 /* ubus callback network.wireless.notify, command = set data, for vlan */
1304 static void
1305 wireless_vlan_set_data(struct wireless_vlan *vlan)
1306 {
1307 enum {
1308 VLAN_DATA_IFNAME,
1309 __VLAN_DATA_MAX,
1310 };
1311 static const struct blobmsg_policy data_policy[__VLAN_DATA_MAX] = {
1312 [VLAN_DATA_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
1313 };
1314 struct blob_attr *tb[__VLAN_DATA_MAX];
1315 struct blob_attr *cur;
1316
1317 blobmsg_parse(data_policy, __VLAN_DATA_MAX, tb,
1318 blobmsg_data(vlan->data), blobmsg_data_len(vlan->data));
1319
1320 if ((cur = tb[VLAN_DATA_IFNAME]))
1321 vlan->ifname = blobmsg_data(cur);
1322 }
1323
1324 /* ubus callback network.wireless.notify, command = process add */
1325 static int
1326 wireless_device_add_process(struct wireless_device *wdev, struct blob_attr *data)
1327 {
1328 enum {
1329 PROC_ATTR_PID,
1330 PROC_ATTR_EXE,
1331 PROC_ATTR_REQUIRED,
1332 PROC_ATTR_KEEP,
1333 __PROC_ATTR_MAX
1334 };
1335 static const struct blobmsg_policy proc_policy[__PROC_ATTR_MAX] = {
1336 [PROC_ATTR_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
1337 [PROC_ATTR_EXE] = { .name = "exe", .type = BLOBMSG_TYPE_STRING },
1338 [PROC_ATTR_REQUIRED] = { .name = "required", .type = BLOBMSG_TYPE_BOOL },
1339 [PROC_ATTR_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
1340 };
1341 struct blob_attr *tb[__PROC_ATTR_MAX];
1342 struct wireless_process *proc;
1343 char *name;
1344 int pid;
1345
1346 if (!data)
1347 return UBUS_STATUS_INVALID_ARGUMENT;
1348
1349 blobmsg_parse(proc_policy, __PROC_ATTR_MAX, tb, blobmsg_data(data), blobmsg_data_len(data));
1350 if (!tb[PROC_ATTR_PID] || !tb[PROC_ATTR_EXE])
1351 return UBUS_STATUS_INVALID_ARGUMENT;
1352
1353 pid = blobmsg_get_u32(tb[PROC_ATTR_PID]);
1354 if (pid < 2)
1355 return UBUS_STATUS_INVALID_ARGUMENT;
1356
1357 proc = calloc_a(sizeof(*proc),
1358 &name, strlen(blobmsg_data(tb[PROC_ATTR_EXE])) + 1);
1359
1360 proc->pid = pid;
1361 proc->exe = strcpy(name, blobmsg_data(tb[PROC_ATTR_EXE]));
1362
1363 if (tb[PROC_ATTR_REQUIRED])
1364 proc->required = blobmsg_get_bool(tb[PROC_ATTR_REQUIRED]);
1365
1366 if (tb[PROC_ATTR_KEEP])
1367 proc->keep = blobmsg_get_bool(tb[PROC_ATTR_KEEP]);
1368
1369 D(WIRELESS, "Wireless device '%s' add pid %d\n", wdev->name, proc->pid);
1370 list_add(&proc->list, &wdev->script_proc);
1371 uloop_timeout_set(&wdev->script_check, 0);
1372
1373 return 0;
1374 }
1375
1376 /* ubus callback network.wireless.notify, command = process kill all */
1377 static int
1378 wireless_device_process_kill_all(struct wireless_device *wdev, struct blob_attr *data,
1379 struct ubus_request_data *req)
1380 {
1381 enum {
1382 KILL_ATTR_SIGNAL,
1383 KILL_ATTR_IMMEDIATE,
1384 __KILL_ATTR_MAX
1385 };
1386 static const struct blobmsg_policy kill_policy[__KILL_ATTR_MAX] = {
1387 [KILL_ATTR_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
1388 [KILL_ATTR_IMMEDIATE] = { .name = "immediate", .type = BLOBMSG_TYPE_BOOL },
1389 };
1390 struct blob_attr *tb[__KILL_ATTR_MAX];
1391 struct blob_attr *cur;
1392 bool immediate = false;
1393 int signal = SIGTERM;
1394
1395 blobmsg_parse(kill_policy, __KILL_ATTR_MAX, tb, blobmsg_data(data), blobmsg_data_len(data));
1396
1397 if ((cur = tb[KILL_ATTR_SIGNAL]))
1398 signal = blobmsg_get_u32(cur);
1399
1400 if ((cur = tb[KILL_ATTR_IMMEDIATE]))
1401 immediate = blobmsg_get_bool(cur);
1402
1403 if (wdev->state != IFS_TEARDOWN || wdev->kill_request)
1404 return UBUS_STATUS_PERMISSION_DENIED;
1405
1406 wireless_process_kill_all(wdev, signal, immediate);
1407
1408 if (list_empty(&wdev->script_proc))
1409 return 0;
1410
1411 wdev->kill_request = calloc(1, sizeof(*wdev->kill_request));
1412 ubus_defer_request(ubus_ctx, req, wdev->kill_request);
1413
1414 return 0;
1415 }
1416
1417 /* ubus callback network.wireless.notify, command = set_retry */
1418 static int
1419 wireless_device_set_retry(struct wireless_device *wdev, struct blob_attr *data)
1420 {
1421 static const struct blobmsg_policy retry_policy = {
1422 .name = "retry", .type = BLOBMSG_TYPE_INT32
1423 };
1424 struct blob_attr *val;
1425
1426 blobmsg_parse(&retry_policy, 1, &val, blobmsg_data(data), blobmsg_data_len(data));
1427 if (val)
1428 wdev->retry = blobmsg_get_u32(val);
1429 else
1430 wdev->retry = WIRELESS_SETUP_RETRY;
1431 __wireless_device_set_up(wdev, 0);
1432 netifd_log_message(L_NOTICE, "Wireless device '%s' set retry=%d\n", wdev->name, wdev->retry);
1433 return 0;
1434 }
1435
1436 enum {
1437 NOTIFY_CMD_UP = 0,
1438 NOTIFY_CMD_SET_DATA = 1,
1439 NOTIFY_CMD_PROCESS_ADD = 2,
1440 NOTIFY_CMD_PROCESS_KILL_ALL = 3,
1441 NOTIFY_CMD_SET_RETRY = 4,
1442 };
1443
1444 /* ubus callback network.wireless.notify */
1445 int
1446 wireless_device_notify(struct wireless_device *wdev, struct blob_attr *data,
1447 struct ubus_request_data *req)
1448 {
1449 enum {
1450 NOTIFY_ATTR_COMMAND,
1451 NOTIFY_ATTR_VIF,
1452 NOTIFY_ATTR_VLAN,
1453 NOTIFY_ATTR_DATA,
1454 __NOTIFY_MAX,
1455 };
1456 static const struct blobmsg_policy notify_policy[__NOTIFY_MAX] = {
1457 [NOTIFY_ATTR_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_INT32 },
1458 [NOTIFY_ATTR_VIF] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
1459 [NOTIFY_ATTR_VLAN] = { .name = "vlan", .type = BLOBMSG_TYPE_STRING },
1460 [NOTIFY_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
1461 };
1462 struct wireless_interface *vif = NULL;
1463 struct wireless_vlan *vlan = NULL;
1464 struct blob_attr *tb[__NOTIFY_MAX];
1465 struct blob_attr *cur, **pdata;
1466
1467 blobmsg_parse(notify_policy, __NOTIFY_MAX, tb, blob_data(data), blob_len(data));
1468
1469 if (!tb[NOTIFY_ATTR_COMMAND])
1470 return UBUS_STATUS_INVALID_ARGUMENT;
1471
1472 if ((cur = tb[NOTIFY_ATTR_VIF]) != NULL) {
1473 vif = vlist_find(&wdev->interfaces, blobmsg_data(cur), vif, node);
1474 if (!vif)
1475 return UBUS_STATUS_NOT_FOUND;
1476 }
1477
1478 if ((cur = tb[NOTIFY_ATTR_VLAN]) != NULL) {
1479 if (!vif)
1480 return UBUS_STATUS_NOT_FOUND;
1481 vlan = vlist_find(&vif->vlans, blobmsg_data(cur), vlan, node);
1482 if (!vlan)
1483 return UBUS_STATUS_NOT_FOUND;
1484 }
1485
1486 cur = tb[NOTIFY_ATTR_DATA];
1487 if (!cur)
1488 return UBUS_STATUS_INVALID_ARGUMENT;
1489
1490 switch (blobmsg_get_u32(tb[NOTIFY_ATTR_COMMAND])) {
1491 case NOTIFY_CMD_UP:
1492 if (vif || vlan)
1493 return UBUS_STATUS_INVALID_ARGUMENT;
1494
1495 if (wdev->state != IFS_SETUP)
1496 return UBUS_STATUS_PERMISSION_DENIED;
1497
1498 wireless_device_mark_up(wdev);
1499 break;
1500 case NOTIFY_CMD_SET_DATA:
1501 if (vlan)
1502 pdata = &vlan->data;
1503 else if (vif)
1504 pdata = &vif->data;
1505 else
1506 pdata = &wdev->data;
1507
1508 free(*pdata);
1509 *pdata = blob_memdup(cur);
1510 if (vlan)
1511 wireless_vlan_set_data(vlan);
1512 else if (vif)
1513 wireless_interface_set_data(vif);
1514 break;
1515 case NOTIFY_CMD_PROCESS_ADD:
1516 return wireless_device_add_process(wdev, cur);
1517 case NOTIFY_CMD_PROCESS_KILL_ALL:
1518 return wireless_device_process_kill_all(wdev, cur, req);
1519 case NOTIFY_CMD_SET_RETRY:
1520 return wireless_device_set_retry(wdev, cur);
1521 default:
1522 return UBUS_STATUS_INVALID_ARGUMENT;
1523 }
1524
1525 return 0;
1526 }
1527
1528 static void
1529 wdev_check_network_enabled(struct wireless_device *wdev)
1530 {
1531 struct wireless_interface *vif;
1532 struct interface *iface;
1533 struct blob_attr *cur;
1534 int rem;
1535
1536 vlist_for_each_element(&wdev->interfaces, vif, node) {
1537 int enabled = -1;
1538
1539 blobmsg_for_each_attr(cur, vif->network, rem) {
1540 iface = vlist_find(&interfaces, blobmsg_get_string(cur), iface, node);
1541 if (!iface)
1542 continue;
1543
1544 if (iface->autostart) {
1545 enabled = 1;
1546 break;
1547 }
1548 if (enabled != 1)
1549 enabled = 0;
1550 }
1551
1552 if (vif->disabled == !enabled)
1553 continue;
1554
1555 vif->disabled = !enabled;
1556 wdev->config_update = true;
1557 }
1558 }
1559
1560 static void
1561 __wireless_start_pending(struct uloop_timeout *t)
1562 {
1563 struct wireless_device *wdev;
1564
1565 vlist_for_each_element(&wireless_devices, wdev, node) {
1566 wdev_check_network_enabled(wdev);
1567 if (wdev->config_update)
1568 wdev_set_config_state(wdev, IFC_RELOAD);
1569 __wireless_device_set_up(wdev, 0);
1570 }
1571 }
1572
1573 void wireless_start_pending(int timeout)
1574 {
1575 static struct uloop_timeout timer = {
1576 .cb = __wireless_start_pending
1577 };
1578
1579 if (timeout) {
1580 uloop_timeout_set(&timer, timeout);
1581 return;
1582 }
1583
1584 uloop_timeout_cancel(&timer);
1585 timer.cb(&timer);
1586 }
1587
1588 void wireless_check_network_enabled(void)
1589 {
1590 struct wireless_device *wdev;
1591
1592 vlist_for_each_element(&wireless_devices, wdev, node) {
1593 wdev_check_network_enabled(wdev);
1594
1595 if (wdev->config_update)
1596 wireless_start_pending(1000);
1597 }
1598 }
1599
1600 void wireless_device_hotplug_event(const char *name, bool add)
1601 {
1602 struct wireless_interface *vif;
1603 struct wireless_device *wdev;
1604 const char *s;
1605 size_t len;
1606
1607 s = strstr(name, ".sta");
1608 if (s) {
1609 if (strchr(s + 4, '.'))
1610 return;
1611
1612 len = s - name;
1613 } else if (!device_find(name)) {
1614 len = strlen(name);
1615 } else {
1616 return;
1617 }
1618
1619 vlist_for_each_element(&wireless_devices, wdev, node) {
1620 vlist_for_each_element(&wdev->interfaces, vif, node) {
1621 if (!vif->ifname)
1622 continue;
1623
1624 if (strlen(vif->ifname) != len ||
1625 strncmp(vif->ifname, name, len) != 0)
1626 continue;
1627
1628 wireless_interface_handle_link(vif, name, add);
1629 }
1630 }
1631 }