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 alias
->dev
.ifindex
= 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
;
88 alias
= container_of(dep
, struct alias_device
, dep
);
92 case DEV_EVENT_REMOVE
:
93 device_set_present(&alias
->dev
, present
);
96 device_broadcast_event(&alias
->dev
, ev
);
101 static struct device
*
102 alias_device_create(const char *name
, struct blob_attr
*attr
)
104 struct alias_device
*alias
;
106 alias
= calloc(1, sizeof(*alias
) + strlen(name
) + 1);
107 strcpy(alias
->name
, name
);
108 alias
->dev
.set_state
= alias_device_set_state
;
109 alias
->dev
.hidden
= true;
110 device_init_virtual(&alias
->dev
, &alias_device_type
, NULL
);
111 alias
->avl
.key
= alias
->name
;
112 avl_insert(&aliases
, &alias
->avl
);
113 alias
->dep
.alias
= true;
114 alias
->dep
.cb
= alias_device_cb
;
115 device_check_state(&alias
->dev
);
120 static void alias_device_free(struct device
*dev
)
122 struct alias_device
*alias
;
124 alias
= container_of(dev
, struct alias_device
, dev
);
125 avl_delete(&aliases
, &alias
->avl
);
129 static int alias_check_state(struct device
*dev
)
131 struct alias_device
*alias
;
132 struct interface
*iface
;
133 struct device
*ndev
= NULL
;
135 alias
= container_of(dev
, struct alias_device
, dev
);
137 iface
= vlist_find(&interfaces
, alias
->name
, iface
, node
);
138 if (iface
&& iface
->state
== IFS_UP
)
139 ndev
= iface
->l3_dev
.dev
;
141 alias_set_device(alias
, ndev
);
146 static const struct device_type alias_device_type
= {
147 .name
= "Network alias",
148 .create
= alias_device_create
,
149 .free
= alias_device_free
,
150 .check_state
= alias_check_state
,
154 alias_notify_device(const char *name
, struct device
*dev
)
156 struct alias_device
*alias
;
160 alias
= avl_find_element(&aliases
, name
, alias
, avl
);
162 alias_set_device(alias
, dev
);
168 device_alias_get(const char *name
)
170 struct alias_device
*alias
;
172 alias
= avl_find_element(&aliases
, name
, alias
, avl
);
176 return alias_device_create(name
, NULL
);
179 static void __init
alias_init(void)
181 avl_init(&aliases
, avl_strcmp
, false, NULL
);