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 void alias_set_device(struct alias_device
*alias
, struct device
*dev
)
35 if (dev
== alias
->dep
.dev
) {
37 device_remove_user(&alias
->new_dep
);
38 alias
->update
= false;
40 device_set_present(&alias
->dev
, true);
45 device_set_present(&alias
->dev
, false);
46 device_remove_user(&alias
->new_dep
);
47 if (alias
->dev
.active
) {
49 device_add_user(&alias
->new_dep
, dev
);
55 alias
->update
= false;
56 device_remove_user(&alias
->dep
);
57 alias
->dev
.hidden
= !dev
;
59 device_set_ifindex(&alias
->dev
, dev
->ifindex
);
60 device_set_ifname(&alias
->dev
, dev
->ifname
);
61 device_add_user(&alias
->dep
, dev
);
63 device_set_ifname(&alias
->dev
, "");
64 device_set_link(&alias
->dev
, false);
69 alias_device_set_state(struct device
*dev
, bool state
)
71 struct alias_device
*alias
;
73 alias
= container_of(dev
, struct alias_device
, dev
);
78 return device_claim(&alias
->dep
);
80 device_release(&alias
->dep
);
82 alias_set_device(alias
, alias
->new_dep
.dev
);
87 static void alias_device_cb(struct device_user
*dep
, enum device_event ev
)
89 struct alias_device
*alias
;
91 alias
= container_of(dep
, struct alias_device
, dep
);
94 device_set_present(&alias
->dev
, true);
96 case DEV_EVENT_REMOVE
:
97 device_set_present(&alias
->dev
, false);
99 case DEV_EVENT_LINK_UP
:
100 device_set_link(&alias
->dev
, true);
102 case DEV_EVENT_LINK_DOWN
:
103 device_set_link(&alias
->dev
, false);
105 case DEV_EVENT_UPDATE_IFINDEX
:
106 device_set_ifindex(&alias
->dev
, dep
->dev
->ifindex
);
109 device_broadcast_event(&alias
->dev
, ev
);
114 static struct device
*
115 alias_device_create(const char *name
, struct device_type
*devtype
,
116 struct blob_attr
*attr
)
118 struct alias_device
*alias
;
120 alias
= calloc(1, sizeof(*alias
) + strlen(name
) + 1);
124 strcpy(alias
->name
, name
);
125 alias
->dev
.set_state
= alias_device_set_state
;
126 alias
->dev
.hidden
= true;
127 if (device_init_virtual(&alias
->dev
, devtype
, NULL
) < 0) {
132 alias
->avl
.key
= alias
->name
;
133 avl_insert(&aliases
, &alias
->avl
);
134 alias
->dep
.alias
= true;
135 alias
->dep
.cb
= alias_device_cb
;
136 device_check_state(&alias
->dev
);
141 static void alias_device_free(struct device
*dev
)
143 struct alias_device
*alias
;
145 alias
= container_of(dev
, struct alias_device
, dev
);
146 device_remove_user(&alias
->new_dep
);
147 device_remove_user(&alias
->dep
);
148 avl_delete(&aliases
, &alias
->avl
);
152 static int alias_check_state(struct device
*dev
)
154 struct alias_device
*alias
;
155 struct interface
*iface
;
156 struct device
*ndev
= NULL
;
158 alias
= container_of(dev
, struct alias_device
, dev
);
160 iface
= vlist_find(&interfaces
, alias
->name
, iface
, node
);
161 if (iface
&& iface
->state
== IFS_UP
)
162 ndev
= iface
->l3_dev
.dev
;
164 alias_set_device(alias
, ndev
);
169 static struct device_type alias_device_type
= {
170 .name
= "Network alias",
171 .create
= alias_device_create
,
172 .free
= alias_device_free
,
173 .check_state
= alias_check_state
,
177 alias_notify_device(const char *name
, struct device
*dev
)
179 struct alias_device
*alias
;
181 alias
= avl_find_element(&aliases
, name
, alias
, avl
);
183 alias_set_device(alias
, dev
);
187 device_alias_get(const char *name
)
189 struct alias_device
*alias
;
191 alias
= avl_find_element(&aliases
, name
, alias
, avl
);
195 return alias_device_create(name
, &alias_device_type
, NULL
);
198 static void __init
alias_init(void)
200 avl_init(&aliases
, avl_strcmp
, false, NULL
);