• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt