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