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;
28 struct device_user new_dep;
33 static const struct device_type alias_device_type;
35 static void alias_set_device(struct alias_device *alias, struct device *dev)
37 if (dev == alias->dep.dev)
40 device_set_present(&alias->dev, false);
41 device_remove_user(&alias->new_dep);
42 if (alias->dev.active) {
44 device_add_user(&alias->new_dep, dev);
50 alias->update = false;
51 device_remove_user(&alias->dep);
52 alias->dev.hidden = !dev;
54 device_set_ifindex(&alias->dev, dev->ifindex);
55 strcpy(alias->dev.ifname, dev->ifname);
56 device_broadcast_event(&alias->dev, DEV_EVENT_UPDATE_IFNAME);
57 device_add_user(&alias->dep, dev);
59 alias->dev.ifname[0] = 0;
60 device_broadcast_event(&alias->dev, DEV_EVENT_UPDATE_IFNAME);
65 alias_device_set_state(struct device *dev, bool state)
67 struct alias_device *alias;
69 alias = container_of(dev, struct alias_device, dev);
74 return device_claim(&alias->dep);
76 device_release(&alias->dep);
78 alias_set_device(alias, alias->new_dep.dev);
83 static void alias_device_cb(struct device_user *dep, enum device_event ev)
85 struct alias_device *alias;
86 bool new_state = false;
88 alias = container_of(dep, struct alias_device, dep);
92 case DEV_EVENT_REMOVE:
93 device_set_present(&alias->dev, new_state);
95 case DEV_EVENT_LINK_UP:
97 case DEV_EVENT_LINK_DOWN:
98 device_set_link(&alias->dev, new_state);
100 case DEV_EVENT_UPDATE_IFINDEX:
101 device_set_ifindex(&alias->dev, dep->dev->ifindex);
104 device_broadcast_event(&alias->dev, ev);
109 static struct device *
110 alias_device_create(const char *name, struct blob_attr *attr)
112 struct alias_device *alias;
114 alias = calloc(1, sizeof(*alias) + strlen(name) + 1);
115 strcpy(alias->name, name);
116 alias->dev.set_state = alias_device_set_state;
117 alias->dev.hidden = true;
118 device_init_virtual(&alias->dev, &alias_device_type, NULL);
119 alias->avl.key = alias->name;
120 avl_insert(&aliases, &alias->avl);
121 alias->dep.alias = true;
122 alias->dep.cb = alias_device_cb;
123 device_check_state(&alias->dev);
128 static void alias_device_free(struct device *dev)
130 struct alias_device *alias;
132 alias = container_of(dev, struct alias_device, dev);
133 avl_delete(&aliases, &alias->avl);
137 static int alias_check_state(struct device *dev)
139 struct alias_device *alias;
140 struct interface *iface;
141 struct device *ndev = NULL;
143 alias = container_of(dev, struct alias_device, dev);
145 iface = vlist_find(&interfaces, alias->name, iface, node);
146 if (iface && iface->state == IFS_UP)
147 ndev = iface->l3_dev.dev;
149 alias_set_device(alias, ndev);
154 static const struct device_type alias_device_type = {
155 .name = "Network alias",
156 .create = alias_device_create,
157 .free = alias_device_free,
158 .check_state = alias_check_state,
162 alias_notify_device(const char *name, struct device *dev)
164 struct alias_device *alias;
168 alias = avl_find_element(&aliases, name, alias, avl);
170 alias_set_device(alias, dev);
176 device_alias_get(const char *name)
178 struct alias_device *alias;
180 alias = avl_find_element(&aliases, name, alias, avl);
184 return alias_device_create(name, NULL);
187 static void __init alias_init(void)
189 avl_init(&aliases, avl_strcmp, false, NULL);