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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt