2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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
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.
20 #include "interface.h"
22 static struct avl_tree aliases;
27 struct device_user dep;
32 static const struct device_type alias_device_type;
35 alias_device_set_state(struct device *dev, bool state)
37 struct alias_device *alias;
39 alias = container_of(dev, struct alias_device, dev);
44 return device_claim(&alias->dep);
46 device_release(&alias->dep);
48 device_remove_user(&alias->dep);
52 static void alias_device_cb(struct device_user *dep, enum device_event ev)
54 struct alias_device *alias;
57 alias = container_of(dep, struct alias_device, dep);
61 case DEV_EVENT_REMOVE:
62 device_set_present(&alias->dev, present);
65 device_broadcast_event(&alias->dev, ev);
70 static struct device *
71 alias_device_create(const char *name, struct blob_attr *attr)
73 struct alias_device *alias;
75 alias = calloc(1, sizeof(*alias) + strlen(name) + 1);
76 strcpy(alias->name, name);
77 alias->dev.set_state = alias_device_set_state;
78 alias->dev.hidden = true;
79 device_init_virtual(&alias->dev, &alias_device_type, NULL);
80 alias->avl.key = alias->name;
81 avl_insert(&aliases, &alias->avl);
82 alias->dep.alias = true;
83 alias->dep.cb = alias_device_cb;
84 device_check_state(&alias->dev);
89 static void alias_device_free(struct device *dev)
91 struct alias_device *alias;
93 alias = container_of(dev, struct alias_device, dev);
94 avl_delete(&aliases, &alias->avl);
98 static void __alias_notify_device(struct alias_device *alias, struct device *dev)
100 alias->cleanup = !dev;
102 if (dev != alias->dep.dev) {
103 device_remove_user(&alias->dep);
104 strcpy(alias->dev.ifname, dev->ifname);
105 device_add_user(&alias->dep, dev);
106 alias->dev.hidden = false;
107 device_broadcast_event(&alias->dev, DEV_EVENT_UPDATE_IFNAME);
111 if (!dev && alias->dep.dev && !alias->dep.dev->active) {
112 device_remove_user(&alias->dep);
113 alias->dev.hidden = true;
114 alias->dev.ifname[0] = 0;
115 device_broadcast_event(&alias->dev, DEV_EVENT_UPDATE_IFNAME);
119 static int alias_check_state(struct device *dev)
121 struct alias_device *alias;
122 struct interface *iface;
123 struct device *ndev = NULL;
125 alias = container_of(dev, struct alias_device, dev);
127 iface = vlist_find(&interfaces, alias->name, iface, node);
128 if (iface && iface->state == IFS_UP)
129 ndev = iface->main_dev.dev;
131 __alias_notify_device(alias, ndev);
136 static const struct device_type alias_device_type = {
137 .name = "Network alias",
138 .create = alias_device_create,
139 .free = alias_device_free,
140 .check_state = alias_check_state,
144 alias_notify_device(const char *name, struct device *dev)
146 struct alias_device *alias;
150 alias = avl_find_element(&aliases, name, alias, avl);
152 __alias_notify_device(alias, dev);
158 device_alias_get(const char *name)
160 struct alias_device *alias;
162 alias = avl_find_element(&aliases, name, alias, avl);
166 return alias_device_create(name, NULL);
169 static void __init alias_init(void)
171 avl_init(&aliases, avl_strcmp, false, NULL);