interface: report link up events for force_link interfaces
[project/netifd.git] / device.c
1 /*
2  * netifd - network interface daemon
3  * Copyright (C) 2012 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 <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <assert.h>
18
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <net/ethernet.h>
22
23 #ifdef linux
24 #include <netinet/ether.h>
25 #endif
26
27 #include "netifd.h"
28 #include "system.h"
29 #include "config.h"
30
31 static struct avl_tree devices;
32 static bool default_ps = true;
33
34 static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
35         [DEV_ATTR_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
36         [DEV_ATTR_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
37         [DEV_ATTR_MTU6] = { .name = "mtu6", .type = BLOBMSG_TYPE_INT32 },
38         [DEV_ATTR_MACADDR] = { .name = "macaddr", .type = BLOBMSG_TYPE_STRING },
39         [DEV_ATTR_TXQUEUELEN] = { .name = "txqueuelen", .type = BLOBMSG_TYPE_INT32 },
40         [DEV_ATTR_ENABLED] = { .name = "enabled", .type = BLOBMSG_TYPE_BOOL },
41         [DEV_ATTR_IPV6] = { .name = "ipv6", .type = BLOBMSG_TYPE_BOOL },
42         [DEV_ATTR_PROMISC] = { .name = "promisc", .type = BLOBMSG_TYPE_BOOL },
43         [DEV_ATTR_RPFILTER] = { .name = "rpfilter", .type = BLOBMSG_TYPE_STRING },
44         [DEV_ATTR_ACCEPTLOCAL] = { .name = "acceptlocal", .type = BLOBMSG_TYPE_BOOL },
45         [DEV_ATTR_IGMPVERSION] = { .name = "igmpversion", .type = BLOBMSG_TYPE_INT32 },
46         [DEV_ATTR_MLDVERSION] = { .name = "mldversion", .type = BLOBMSG_TYPE_INT32 },
47         [DEV_ATTR_NEIGHREACHABLETIME] = { .name = "neighreachabletime", .type = BLOBMSG_TYPE_INT32 },
48         [DEV_ATTR_NEIGHGCSTALETIME] = { .name = "neighgcstaletime", .type = BLOBMSG_TYPE_INT32 },
49         [DEV_ATTR_RPS] = { .name = "rps", .type = BLOBMSG_TYPE_BOOL },
50         [DEV_ATTR_XPS] = { .name = "xps", .type = BLOBMSG_TYPE_BOOL },
51         [DEV_ATTR_DADTRANSMITS] = { .name = "dadtransmits", .type = BLOBMSG_TYPE_INT32 },
52         [DEV_ATTR_MULTICAST_TO_UNICAST] = { .name = "multicast_to_unicast", .type = BLOBMSG_TYPE_BOOL },
53         [DEV_ATTR_MULTICAST_ROUTER] = { .name = "multicast_router", .type = BLOBMSG_TYPE_INT32 },
54         [DEV_ATTR_MULTICAST] = { .name ="multicast", .type = BLOBMSG_TYPE_BOOL },
55         [DEV_ATTR_LEARNING] = { .name ="learning", .type = BLOBMSG_TYPE_BOOL },
56         [DEV_ATTR_UNICAST_FLOOD] = { .name ="unicast_flood", .type = BLOBMSG_TYPE_BOOL },
57 };
58
59 const struct uci_blob_param_list device_attr_list = {
60         .n_params = __DEV_ATTR_MAX,
61         .params = dev_attrs,
62 };
63
64 static int __devlock = 0;
65
66 void device_lock(void)
67 {
68         __devlock++;
69 }
70
71 void device_unlock(void)
72 {
73         __devlock--;
74         if (!__devlock)
75                 device_free_unused(NULL);
76 }
77
78 static int set_device_state(struct device *dev, bool state)
79 {
80         if (state) {
81                 /* Get ifindex for all devices being enabled so a valid  */
82                 /* ifindex is in place avoiding possible race conditions */
83                 device_set_ifindex(dev, system_if_resolve(dev));
84                 if (!dev->ifindex)
85                         return -1;
86
87                 system_if_up(dev);
88         }
89         else
90                 system_if_down(dev);
91
92         return 0;
93 }
94
95 static int
96 simple_device_set_state(struct device *dev, bool state)
97 {
98         struct device *pdev;
99         int ret = 0;
100
101         pdev = dev->parent.dev;
102         if (state && !pdev) {
103                 pdev = system_if_get_parent(dev);
104                 if (pdev)
105                         device_add_user(&dev->parent, pdev);
106         }
107
108         if (pdev) {
109                 if (state)
110                         ret = device_claim(&dev->parent);
111                 else
112                         device_release(&dev->parent);
113
114                 if (ret < 0)
115                         return ret;
116         }
117         return set_device_state(dev, state);
118 }
119
120 static struct device *
121 simple_device_create(const char *name, struct blob_attr *attr)
122 {
123         struct blob_attr *tb[__DEV_ATTR_MAX];
124         struct device *dev = NULL;
125
126         blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
127         dev = device_get(name, true);
128         if (!dev)
129                 return NULL;
130
131         dev->set_state = simple_device_set_state;
132         device_init_settings(dev, tb);
133
134         return dev;
135 }
136
137 static void simple_device_free(struct device *dev)
138 {
139         if (dev->parent.dev)
140                 device_remove_user(&dev->parent);
141         free(dev);
142 }
143
144 const struct device_type simple_device_type = {
145         .name = "Network device",
146         .config_params = &device_attr_list,
147
148         .create = simple_device_create,
149         .check_state = system_if_check,
150         .free = simple_device_free,
151 };
152
153 static void
154 device_merge_settings(struct device *dev, struct device_settings *n)
155 {
156         struct device_settings *os = &dev->orig_settings;
157         struct device_settings *s = &dev->settings;
158
159         memset(n, 0, sizeof(*n));
160         n->mtu = s->flags & DEV_OPT_MTU ? s->mtu : os->mtu;
161         n->mtu6 = s->flags & DEV_OPT_MTU6 ? s->mtu6 : os->mtu6;
162         n->txqueuelen = s->flags & DEV_OPT_TXQUEUELEN ?
163                 s->txqueuelen : os->txqueuelen;
164         memcpy(n->macaddr,
165                 (s->flags & DEV_OPT_MACADDR ? s->macaddr : os->macaddr),
166                 sizeof(n->macaddr));
167         n->ipv6 = s->flags & DEV_OPT_IPV6 ? s->ipv6 : os->ipv6;
168         n->promisc = s->flags & DEV_OPT_PROMISC ? s->promisc : os->promisc;
169         n->rpfilter = s->flags & DEV_OPT_RPFILTER ? s->rpfilter : os->rpfilter;
170         n->acceptlocal = s->flags & DEV_OPT_ACCEPTLOCAL ? s->acceptlocal : os->acceptlocal;
171         n->igmpversion = s->flags & DEV_OPT_IGMPVERSION ? s->igmpversion : os->igmpversion;
172         n->mldversion = s->flags & DEV_OPT_MLDVERSION ? s->mldversion : os->mldversion;
173         n->neigh4reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ?
174                 s->neigh4reachabletime : os->neigh4reachabletime;
175         n->neigh6reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ?
176                 s->neigh6reachabletime : os->neigh6reachabletime;
177         n->neigh4gcstaletime = s->flags & DEV_OPT_NEIGHGCSTALETIME ?
178                 s->neigh4gcstaletime : os->neigh4gcstaletime;
179         n->neigh6gcstaletime = s->flags & DEV_OPT_NEIGHGCSTALETIME ?
180                 s->neigh6gcstaletime : os->neigh6gcstaletime;
181         n->dadtransmits = s->flags & DEV_OPT_DADTRANSMITS ?
182                 s->dadtransmits : os->dadtransmits;
183         n->multicast = s->flags & DEV_OPT_MULTICAST ?
184                 s->multicast : os->multicast;
185         n->multicast_to_unicast = s->multicast_to_unicast;
186         n->multicast_router = s->multicast_router;
187         n->learning = s->learning;
188         n->unicast_flood = s->unicast_flood;
189         n->flags = s->flags | os->flags | os->valid_flags;
190 }
191
192 void
193 device_init_settings(struct device *dev, struct blob_attr **tb)
194 {
195         struct device_settings *s = &dev->settings;
196         struct blob_attr *cur;
197         struct ether_addr *ea;
198         bool disabled = false;
199
200         s->flags = 0;
201         if ((cur = tb[DEV_ATTR_ENABLED]))
202                 disabled = !blobmsg_get_bool(cur);
203
204         if ((cur = tb[DEV_ATTR_MTU])) {
205                 s->mtu = blobmsg_get_u32(cur);
206                 s->flags |= DEV_OPT_MTU;
207         }
208
209         if ((cur = tb[DEV_ATTR_MTU6])) {
210                 s->mtu6 = blobmsg_get_u32(cur);
211                 s->flags |= DEV_OPT_MTU6;
212         }
213
214         if ((cur = tb[DEV_ATTR_TXQUEUELEN])) {
215                 s->txqueuelen = blobmsg_get_u32(cur);
216                 s->flags |= DEV_OPT_TXQUEUELEN;
217         }
218
219         if ((cur = tb[DEV_ATTR_MACADDR])) {
220                 ea = ether_aton(blobmsg_data(cur));
221                 if (ea) {
222                         memcpy(s->macaddr, ea, 6);
223                         s->flags |= DEV_OPT_MACADDR;
224                 }
225         }
226
227         if ((cur = tb[DEV_ATTR_IPV6])) {
228                 s->ipv6 = blobmsg_get_bool(cur);
229                 s->flags |= DEV_OPT_IPV6;
230         }
231
232         if ((cur = tb[DEV_ATTR_PROMISC])) {
233                 s->promisc = blobmsg_get_bool(cur);
234                 s->flags |= DEV_OPT_PROMISC;
235         }
236
237         if ((cur = tb[DEV_ATTR_RPFILTER])) {
238                 if (system_resolve_rpfilter(blobmsg_data(cur), &s->rpfilter))
239                         s->flags |= DEV_OPT_RPFILTER;
240                 else
241                         DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur));
242         }
243
244         if ((cur = tb[DEV_ATTR_ACCEPTLOCAL])) {
245                 s->acceptlocal = blobmsg_get_bool(cur);
246                 s->flags |= DEV_OPT_ACCEPTLOCAL;
247         }
248
249         if ((cur = tb[DEV_ATTR_IGMPVERSION])) {
250                 s->igmpversion = blobmsg_get_u32(cur);
251                 if (s->igmpversion >= 1 && s->igmpversion <= 3)
252                         s->flags |= DEV_OPT_IGMPVERSION;
253                 else
254                         DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur));
255         }
256
257         if ((cur = tb[DEV_ATTR_MLDVERSION])) {
258                 s->mldversion = blobmsg_get_u32(cur);
259                 if (s->mldversion >= 1 && s->mldversion <= 2)
260                         s->flags |= DEV_OPT_MLDVERSION;
261                 else
262                         DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur));
263         }
264
265         if ((cur = tb[DEV_ATTR_NEIGHREACHABLETIME])) {
266                 s->neigh6reachabletime = s->neigh4reachabletime = blobmsg_get_u32(cur);
267                 s->flags |= DEV_OPT_NEIGHREACHABLETIME;
268         }
269
270         if ((cur = tb[DEV_ATTR_NEIGHGCSTALETIME])) {
271                 s->neigh6gcstaletime = s->neigh4gcstaletime = blobmsg_get_u32(cur);
272                 s->flags |= DEV_OPT_NEIGHGCSTALETIME;
273         }
274
275         if ((cur = tb[DEV_ATTR_RPS])) {
276                 s->rps = blobmsg_get_bool(cur);
277                 s->flags |= DEV_OPT_RPS;
278         }
279         else
280                 s->rps = default_ps;
281
282         if ((cur = tb[DEV_ATTR_XPS])) {
283                 s->xps = blobmsg_get_bool(cur);
284                 s->flags |= DEV_OPT_XPS;
285         }
286         else
287                 s->xps = default_ps;
288
289         if ((cur = tb[DEV_ATTR_DADTRANSMITS])) {
290                 s->dadtransmits = blobmsg_get_u32(cur);
291                 s->flags |= DEV_OPT_DADTRANSMITS;
292         }
293
294         if ((cur = tb[DEV_ATTR_MULTICAST_TO_UNICAST])) {
295                 s->multicast_to_unicast = blobmsg_get_bool(cur);
296                 s->flags |= DEV_OPT_MULTICAST_TO_UNICAST;
297         }
298
299         if ((cur = tb[DEV_ATTR_MULTICAST_ROUTER])) {
300                 s->multicast_router = blobmsg_get_u32(cur);
301                 if (s->multicast_router <= 2)
302                         s->flags |= DEV_OPT_MULTICAST_ROUTER;
303                 else
304                         DPRINTF("Invalid value: %d - (Use 0: never, 1: learn, 2: always)\n", blobmsg_get_u32(cur));
305         }
306
307         if ((cur = tb[DEV_ATTR_MULTICAST])) {
308                 s->multicast = blobmsg_get_bool(cur);
309                 s->flags |= DEV_OPT_MULTICAST;
310         }
311
312         if ((cur = tb[DEV_ATTR_LEARNING])) {
313                 s->learning = blobmsg_get_bool(cur);
314                 s->flags |= DEV_OPT_LEARNING;
315         }
316
317         if ((cur = tb[DEV_ATTR_UNICAST_FLOOD])) {
318                 s->unicast_flood = blobmsg_get_bool(cur);
319                 s->flags |= DEV_OPT_UNICAST_FLOOD;
320         }
321
322         device_set_disabled(dev, disabled);
323 }
324
325 static void __init dev_init(void)
326 {
327         avl_init(&devices, avl_strcmp, true, NULL);
328 }
329
330 static int device_broadcast_cb(void *ctx, struct safe_list *list)
331 {
332         struct device_user *dep = container_of(list, struct device_user, list);
333         int *ev = ctx;
334
335         /* device might have been removed by an earlier callback */
336         if (!dep->dev)
337                 return 0;
338
339         if (dep->cb)
340                 dep->cb(dep, *ev);
341         return 0;
342 }
343
344 void device_broadcast_event(struct device *dev, enum device_event ev)
345 {
346         int dev_ev = ev;
347
348         safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev);
349         safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev);
350 }
351
352 int device_claim(struct device_user *dep)
353 {
354         struct device *dev = dep->dev;
355         int ret = 0;
356
357         if (dep->claimed)
358                 return 0;
359
360         if (!dev)
361                 return -1;
362
363         dep->claimed = true;
364         D(DEVICE, "Claim %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active + 1);
365         if (++dev->active != 1)
366                 return 0;
367
368         device_broadcast_event(dev, DEV_EVENT_SETUP);
369         if (dev->external) {
370                 /* Get ifindex for external claimed devices so a valid   */
371                 /* ifindex is in place avoiding possible race conditions */
372                 device_set_ifindex(dev, system_if_resolve(dev));
373                 if (!dev->ifindex)
374                         ret = -1;
375
376                 system_if_get_settings(dev, &dev->orig_settings);
377         } else
378                 ret = dev->set_state(dev, true);
379
380         if (ret == 0)
381                 device_broadcast_event(dev, DEV_EVENT_UP);
382         else {
383                 D(DEVICE, "claim %s %s failed: %d\n", dev->type->name, dev->ifname, ret);
384                 dev->active = 0;
385                 dep->claimed = false;
386         }
387
388         return ret;
389 }
390
391 void device_release(struct device_user *dep)
392 {
393         struct device *dev = dep->dev;
394
395         if (!dep->claimed)
396                 return;
397
398         dep->claimed = false;
399         dev->active--;
400         D(DEVICE, "Release %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active);
401         assert(dev->active >= 0);
402
403         if (dev->active)
404                 return;
405
406         device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
407         if (!dev->external)
408                 dev->set_state(dev, false);
409         device_broadcast_event(dev, DEV_EVENT_DOWN);
410 }
411
412 int device_check_state(struct device *dev)
413 {
414         if (!dev->type->check_state)
415                 return simple_device_type.check_state(dev);
416
417         return dev->type->check_state(dev);
418 }
419
420 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name)
421 {
422         assert(dev);
423         assert(type);
424
425         D(DEVICE, "Initialize device '%s'\n", name ? name : "");
426         INIT_SAFE_LIST(&dev->users);
427         INIT_SAFE_LIST(&dev->aliases);
428         dev->type = type;
429
430         if (name)
431                 device_set_ifname(dev, name);
432
433         if (!dev->set_state)
434                 dev->set_state = set_device_state;
435 }
436
437 int device_init(struct device *dev, const struct device_type *type, const char *ifname)
438 {
439         int ret;
440
441         device_init_virtual(dev, type, ifname);
442
443         dev->avl.key = dev->ifname;
444
445         ret = avl_insert(&devices, &dev->avl);
446         if (ret < 0)
447                 return ret;
448
449         system_if_clear_state(dev);
450         device_check_state(dev);
451         dev->settings.rps = default_ps;
452         dev->settings.xps = default_ps;
453
454         return 0;
455 }
456
457 static struct device *
458 device_create_default(const char *name, bool external)
459 {
460         struct device *dev;
461
462         if (!external && system_if_force_external(name))
463                 return NULL;
464
465         D(DEVICE, "Create simple device '%s'\n", name);
466         dev = calloc(1, sizeof(*dev));
467         if (!dev)
468                 return NULL;
469
470         dev->external = external;
471         dev->set_state = simple_device_set_state;
472         device_init(dev, &simple_device_type, name);
473         dev->default_config = true;
474         if (external)
475                 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
476         return dev;
477 }
478
479 struct device *
480 device_find(const char *name)
481 {
482         struct device *dev;
483
484         return avl_find_element(&devices, name, dev, avl);
485 }
486
487 struct device *
488 device_get(const char *name, int create)
489 {
490         struct device *dev;
491
492         if (strchr(name, '.'))
493                 return get_vlan_device_chain(name, create);
494
495         if (name[0] == '@')
496                 return device_alias_get(name + 1);
497
498         dev = avl_find_element(&devices, name, dev, avl);
499         if (dev) {
500                 if (create > 1 && !dev->external) {
501                         system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
502                         dev->external = true;
503                         device_set_present(dev, true);
504                 }
505                 return dev;
506         }
507
508         if (!create)
509                 return NULL;
510
511         return device_create_default(name, create > 1);
512 }
513
514 static void
515 device_delete(struct device *dev)
516 {
517         if (!dev->avl.key)
518                 return;
519
520         D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
521         avl_delete(&devices, &dev->avl);
522         dev->avl.key = NULL;
523 }
524
525 static int device_cleanup_cb(void *ctx, struct safe_list *list)
526 {
527         struct device_user *dep = container_of(list, struct device_user, list);
528         if (dep->cb)
529                 dep->cb(dep, DEV_EVENT_REMOVE);
530
531         device_release(dep);
532         return 0;
533 }
534
535 void device_cleanup(struct device *dev)
536 {
537         D(DEVICE, "Clean up device '%s'\n", dev->ifname);
538         safe_list_for_each(&dev->users, device_cleanup_cb, NULL);
539         safe_list_for_each(&dev->aliases, device_cleanup_cb, NULL);
540         device_delete(dev);
541 }
542
543 static void __device_set_present(struct device *dev, bool state)
544 {
545         if (dev->present == state)
546                 return;
547
548         dev->present = state;
549         device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
550 }
551
552 void
553 device_refresh_present(struct device *dev)
554 {
555         bool state = dev->sys_present;
556
557         if (dev->disabled || dev->deferred)
558                 state = false;
559
560         __device_set_present(dev, state);
561 }
562
563 void device_set_present(struct device *dev, bool state)
564 {
565         if (dev->sys_present == state)
566                 return;
567
568         D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
569         dev->sys_present = state;
570         device_refresh_present(dev);
571 }
572
573 void device_set_link(struct device *dev, bool state)
574 {
575         if (dev->link_active == state)
576                 return;
577
578         netifd_log_message(L_NOTICE, "%s '%s' link is %s\n", dev->type->name, dev->ifname, state ? "up" : "down" );
579
580         dev->link_active = state;
581         device_broadcast_event(dev, state ? DEV_EVENT_LINK_UP : DEV_EVENT_LINK_DOWN);
582 }
583
584 void device_set_ifindex(struct device *dev, int ifindex)
585 {
586         if (dev->ifindex == ifindex)
587                 return;
588
589         dev->ifindex = ifindex;
590         device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX);
591 }
592
593 int device_set_ifname(struct device *dev, const char *name)
594 {
595         int ret = 0;
596
597         if (!strcmp(dev->ifname, name))
598                 return 0;
599
600         if (dev->avl.key)
601                 avl_delete(&devices, &dev->avl);
602
603         strncpy(dev->ifname, name, IFNAMSIZ);
604
605         if (dev->avl.key)
606                 ret = avl_insert(&devices, &dev->avl);
607
608         if (ret == 0)
609                 device_broadcast_event(dev, DEV_EVENT_UPDATE_IFNAME);
610
611         return ret;
612 }
613
614 static int device_refcount(struct device *dev)
615 {
616         struct list_head *list;
617         int count = 0;
618
619         list_for_each(list, &dev->users.list)
620                 count++;
621
622         list_for_each(list, &dev->aliases.list)
623                 count++;
624
625         return count;
626 }
627
628 static void
629 __device_add_user(struct device_user *dep, struct device *dev)
630 {
631         struct safe_list *head;
632
633         dep->dev = dev;
634
635         if (dep->alias)
636                 head = &dev->aliases;
637         else
638                 head = &dev->users;
639
640         safe_list_add(&dep->list, head);
641         D(DEVICE, "Add user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
642
643         if (dep->cb && dev->present) {
644                 dep->cb(dep, DEV_EVENT_ADD);
645                 if (dev->active)
646                         dep->cb(dep, DEV_EVENT_UP);
647
648                 if (dev->link_active)
649                         dep->cb(dep, DEV_EVENT_LINK_UP);
650         }
651 }
652
653 void device_add_user(struct device_user *dep, struct device *dev)
654 {
655         if (dep->dev == dev)
656                 return;
657
658         if (dep->dev)
659                 device_remove_user(dep);
660
661         if (!dev)
662                 return;
663
664         __device_add_user(dep, dev);
665 }
666
667 void
668 device_free(struct device *dev)
669 {
670         __devlock++;
671         free(dev->config);
672         device_cleanup(dev);
673         dev->type->free(dev);
674         __devlock--;
675 }
676
677 static void
678 __device_free_unused(struct device *dev)
679 {
680         if (!safe_list_empty(&dev->users) ||
681                 !safe_list_empty(&dev->aliases) ||
682             dev->current_config || __devlock)
683                 return;
684
685         device_free(dev);
686 }
687
688 void device_remove_user(struct device_user *dep)
689 {
690         struct device *dev = dep->dev;
691
692         if (!dep->dev)
693                 return;
694
695         dep->hotplug = false;
696         if (dep->claimed)
697                 device_release(dep);
698
699         safe_list_del(&dep->list);
700         dep->dev = NULL;
701         D(DEVICE, "Remove user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
702         __device_free_unused(dev);
703 }
704
705 void
706 device_free_unused(struct device *dev)
707 {
708         struct device *tmp;
709
710         if (dev)
711                 return __device_free_unused(dev);
712
713         avl_for_each_element_safe(&devices, dev, avl, tmp)
714                 __device_free_unused(dev);
715 }
716
717 void
718 device_init_pending(void)
719 {
720         struct device *dev, *tmp;
721
722         avl_for_each_element_safe(&devices, dev, avl, tmp) {
723                 if (!dev->config_pending)
724                         continue;
725
726                 dev->type->config_init(dev);
727                 dev->config_pending = false;
728         }
729 }
730
731 static enum dev_change_type
732 device_set_config(struct device *dev, const struct device_type *type,
733                   struct blob_attr *attr)
734 {
735         struct blob_attr *tb[__DEV_ATTR_MAX];
736         const struct uci_blob_param_list *cfg = type->config_params;
737
738         if (type != dev->type)
739                 return DEV_CONFIG_RECREATE;
740
741         if (dev->type->reload)
742                 return dev->type->reload(dev, attr);
743
744         if (uci_blob_check_equal(dev->config, attr, cfg))
745                 return DEV_CONFIG_NO_CHANGE;
746
747         if (cfg == &device_attr_list) {
748                 memset(tb, 0, sizeof(tb));
749
750                 if (attr)
751                         blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
752                                 blob_data(attr), blob_len(attr));
753
754                 device_init_settings(dev, tb);
755                 return DEV_CONFIG_RESTART;
756         } else
757                 return DEV_CONFIG_RECREATE;
758 }
759
760 enum dev_change_type
761 device_apply_config(struct device *dev, const struct device_type *type,
762                     struct blob_attr *config)
763 {
764         enum dev_change_type change;
765
766         change = device_set_config(dev, type, config);
767         if (dev->external) {
768                 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
769                 change = DEV_CONFIG_APPLIED;
770         }
771
772         switch (change) {
773                 case DEV_CONFIG_RESTART:
774                 case DEV_CONFIG_APPLIED:
775                         D(DEVICE, "Device '%s': config applied\n", dev->ifname);
776                         config = blob_memdup(config);
777                         free(dev->config);
778                         dev->config = config;
779                         if (change == DEV_CONFIG_RESTART && dev->present) {
780                                 int ret = 0;
781
782                                 device_set_present(dev, false);
783                                 if (dev->active && !dev->external) {
784                                         ret = dev->set_state(dev, false);
785                                         if (!ret)
786                                                 ret = dev->set_state(dev, true);
787                                 }
788                                 if (!ret)
789                                         device_set_present(dev, true);
790                         }
791                         break;
792                 case DEV_CONFIG_NO_CHANGE:
793                         D(DEVICE, "Device '%s': no configuration change\n", dev->ifname);
794                         break;
795                 case DEV_CONFIG_RECREATE:
796                         break;
797         }
798
799         return change;
800 }
801
802 static void
803 device_replace(struct device *dev, struct device *odev)
804 {
805         struct device_user *dep, *tmp;
806
807         __devlock++;
808         if (odev->present)
809                 device_set_present(odev, false);
810
811         list_for_each_entry_safe(dep, tmp, &odev->users.list, list.list) {
812                 device_release(dep);
813                 safe_list_del(&dep->list);
814                 __device_add_user(dep, dev);
815         }
816         __devlock--;
817
818         device_free(odev);
819 }
820
821 void
822 device_reset_config(void)
823 {
824         struct device *dev;
825
826         avl_for_each_element(&devices, dev, avl)
827                 dev->current_config = false;
828 }
829
830 void
831 device_reset_old(void)
832 {
833         struct device *dev, *tmp, *ndev;
834
835         avl_for_each_element_safe(&devices, dev, avl, tmp) {
836                 if (dev->current_config || dev->default_config)
837                         continue;
838
839                 if (dev->type != &simple_device_type)
840                         continue;
841
842                 ndev = device_create_default(dev->ifname, dev->external);
843                 if (!ndev)
844                         continue;
845
846                 device_replace(ndev, dev);
847         }
848 }
849
850 void
851 device_set_default_ps(bool state)
852 {
853         struct device *dev;
854
855         if (state == default_ps)
856                 return;
857
858         default_ps = state;
859
860         avl_for_each_element(&devices, dev, avl) {
861                 struct device_settings *s = &dev->settings;
862                 unsigned int apply_mask = 0;
863
864                 if (!(s->flags & DEV_OPT_RPS)) {
865                         s->rps = default_ps;
866                         apply_mask |= DEV_OPT_RPS;
867                 }
868
869                 if (!(s->flags & DEV_OPT_XPS)) {
870                         s->xps = default_ps;
871                         apply_mask |= DEV_OPT_XPS;
872                 }
873
874                 if (!apply_mask)
875                         continue;
876
877                 if (!(dev->external || (dev->present && dev->active)) ||
878                                 dev->config_pending)
879                         continue;
880
881                 system_if_apply_settings(dev, s, apply_mask);
882         }
883 }
884
885 struct device *
886 device_create(const char *name, const struct device_type *type,
887               struct blob_attr *config)
888 {
889         struct device *odev = NULL, *dev;
890         enum dev_change_type change;
891
892         odev = device_get(name, false);
893         if (odev) {
894                 odev->current_config = true;
895                 change = device_apply_config(odev, type, config);
896                 switch (change) {
897                 case DEV_CONFIG_RECREATE:
898                         D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
899                         device_delete(odev);
900                         break;
901                 default:
902                         return odev;
903                 }
904         } else
905                 D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
906
907         config = blob_memdup(config);
908         if (!config)
909                 return NULL;
910
911         dev = type->create(name, config);
912         if (!dev)
913                 return NULL;
914
915         dev->current_config = true;
916         dev->config = config;
917         if (odev)
918                 device_replace(dev, odev);
919
920         if (!config_init && dev->config_pending) {
921                 type->config_init(dev);
922                 dev->config_pending = false;
923         }
924
925         return dev;
926 }
927
928 void
929 device_dump_status(struct blob_buf *b, struct device *dev)
930 {
931         struct device_settings st;
932         void *c, *s;
933
934         if (!dev) {
935                 avl_for_each_element(&devices, dev, avl) {
936                         if (!dev->present)
937                                 continue;
938                         c = blobmsg_open_table(b, dev->ifname);
939                         device_dump_status(b, dev);
940                         blobmsg_close_table(b, c);
941                 }
942
943                 return;
944         }
945
946         blobmsg_add_u8(b, "external", dev->external);
947         blobmsg_add_u8(b, "present", dev->present);
948         blobmsg_add_string(b, "type", dev->type->name);
949
950         if (!dev->present)
951                 return;
952
953         blobmsg_add_u8(b, "up", !!dev->active);
954         blobmsg_add_u8(b, "carrier", !!dev->link_active);
955
956         if (dev->type->dump_info)
957                 dev->type->dump_info(dev, b);
958         else
959                 system_if_dump_info(dev, b);
960
961         if (dev->active) {
962                 device_merge_settings(dev, &st);
963                 if (st.flags & DEV_OPT_MTU)
964                         blobmsg_add_u32(b, "mtu", st.mtu);
965                 if (st.flags & DEV_OPT_MTU6)
966                         blobmsg_add_u32(b, "mtu6", st.mtu6);
967                 if (st.flags & DEV_OPT_MACADDR)
968                         blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr));
969                 if (st.flags & DEV_OPT_TXQUEUELEN)
970                         blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
971                 if (st.flags & DEV_OPT_IPV6)
972                         blobmsg_add_u8(b, "ipv6", st.ipv6);
973                 if (st.flags & DEV_OPT_PROMISC)
974                         blobmsg_add_u8(b, "promisc", st.promisc);
975                 if (st.flags & DEV_OPT_RPFILTER)
976                         blobmsg_add_u32(b, "rpfilter", st.rpfilter);
977                 if (st.flags & DEV_OPT_ACCEPTLOCAL)
978                         blobmsg_add_u8(b, "acceptlocal", st.acceptlocal);
979                 if (st.flags & DEV_OPT_IGMPVERSION)
980                         blobmsg_add_u32(b, "igmpversion", st.igmpversion);
981                 if (st.flags & DEV_OPT_MLDVERSION)
982                         blobmsg_add_u32(b, "mldversion", st.mldversion);
983                 if (st.flags & DEV_OPT_NEIGHREACHABLETIME) {
984                         blobmsg_add_u32(b, "neigh4reachabletime", st.neigh4reachabletime);
985                         blobmsg_add_u32(b, "neigh6reachabletime", st.neigh6reachabletime);
986                 }
987                 if (st.flags & DEV_OPT_NEIGHGCSTALETIME) {
988                         blobmsg_add_u32(b, "neigh4gcstaletime", st.neigh4gcstaletime);
989                         blobmsg_add_u32(b, "neigh6gcstaletime", st.neigh6gcstaletime);
990                 }
991                 if (st.flags & DEV_OPT_DADTRANSMITS)
992                         blobmsg_add_u32(b, "dadtransmits", st.dadtransmits);
993                 if (st.flags & DEV_OPT_MULTICAST_TO_UNICAST)
994                         blobmsg_add_u8(b, "multicast_to_unicast", st.multicast_to_unicast);
995                 if (st.flags & DEV_OPT_MULTICAST_ROUTER)
996                         blobmsg_add_u32(b, "multicast_router", st.multicast_router);
997                 if (st.flags & DEV_OPT_MULTICAST)
998                         blobmsg_add_u8(b, "multicast", st.multicast);
999                 if (st.flags & DEV_OPT_LEARNING)
1000                         blobmsg_add_u8(b, "learning", st.learning);
1001                 if (st.flags & DEV_OPT_UNICAST_FLOOD)
1002                         blobmsg_add_u8(b, "unicast_flood", st.unicast_flood);
1003         }
1004
1005         s = blobmsg_open_table(b, "statistics");
1006         if (dev->type->dump_stats)
1007                 dev->type->dump_stats(dev, b);
1008         else
1009                 system_if_dump_stats(dev, b);
1010         blobmsg_close_table(b, s);
1011 }