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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt