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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt