• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt