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