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.
21 static struct blob_buf b
;
25 struct device_user dep
;
27 device_state_cb set_state
;
31 static void free_vlan_if(struct device
*iface
)
33 struct vlan_device
*vldev
;
35 vldev
= container_of(iface
, struct vlan_device
, dev
);
36 device_remove_user(&vldev
->dep
);
37 device_cleanup(&vldev
->dev
);
42 __vlan_hotplug_op(struct device
*dev
, struct device
*member
, struct blob_attr
*vlan
, bool add
)
44 struct vlan_device
*vldev
= container_of(dev
, struct vlan_device
, dev
);
48 if (!dev
|| !dev
->hotplug_ops
)
49 return UBUS_STATUS_NOT_SUPPORTED
;
52 a
= blobmsg_open_array(&b
, "vlans");
53 blobmsg_printf(&b
, NULL
, "%d", vldev
->id
);
54 blobmsg_close_array(&b
, a
);
57 return dev
->hotplug_ops
->add(dev
, member
, blobmsg_data(b
.head
));
59 return dev
->hotplug_ops
->del(dev
, member
, blobmsg_data(b
.head
));
63 vlan_hotplug_add(struct device
*dev
, struct device
*member
, struct blob_attr
*vlan
)
65 return __vlan_hotplug_op(dev
, member
, vlan
, true);
69 vlan_hotplug_del(struct device
*dev
, struct device
*member
, struct blob_attr
*vlan
)
71 return __vlan_hotplug_op(dev
, member
, vlan
, false);
75 vlan_hotplug_prepare(struct device
*dev
, struct device
**bridge_dev
)
77 struct vlan_device
*vldev
= container_of(dev
, struct vlan_device
, dev
);
80 if (!dev
|| !dev
->hotplug_ops
)
81 return UBUS_STATUS_NOT_SUPPORTED
;
83 return dev
->hotplug_ops
->prepare(dev
, bridge_dev
);
86 static void vlan_hotplug_check(struct vlan_device
*vldev
, struct device
*dev
)
88 static const struct device_hotplug_ops hotplug_ops
= {
89 .prepare
= vlan_hotplug_prepare
,
90 .add
= vlan_hotplug_add
,
91 .del
= vlan_hotplug_del
94 if (!dev
|| !dev
->hotplug_ops
|| avl_is_empty(&dev
->vlans
.avl
)) {
95 vldev
->dev
.hotplug_ops
= NULL
;
99 vldev
->dev
.hotplug_ops
= &hotplug_ops
;
103 static int vlan_set_device_state(struct device
*dev
, bool up
)
105 struct vlan_device
*vldev
;
108 vldev
= container_of(dev
, struct vlan_device
, dev
);
110 vldev
->set_state(dev
, false);
111 system_vlan_del(dev
);
112 device_release(&vldev
->dep
);
116 ret
= device_claim(&vldev
->dep
);
120 system_vlan_add(vldev
->dep
.dev
, vldev
->id
);
121 ret
= vldev
->set_state(dev
, true);
123 device_release(&vldev
->dep
);
128 static void vlan_dev_cb(struct device_user
*dep
, enum device_event ev
)
130 char name
[IFNAMSIZ
+ 1];
131 struct vlan_device
*vldev
;
133 vldev
= container_of(dep
, struct vlan_device
, dep
);
136 device_set_present(&vldev
->dev
, true);
138 case DEV_EVENT_REMOVE
:
139 device_set_present(&vldev
->dev
, false);
141 case DEV_EVENT_UPDATE_IFNAME
:
142 vlan_hotplug_check(vldev
, dep
->dev
);
143 vldev
->dev
.hidden
= dep
->dev
->hidden
;
144 if (snprintf(name
, sizeof(name
), "%s.%d", dep
->dev
->ifname
,
145 vldev
->id
) >= sizeof(name
) - 1 ||
146 device_set_ifname(&vldev
->dev
, name
))
147 free_vlan_if(&vldev
->dev
);
149 case DEV_EVENT_TOPO_CHANGE
:
150 /* Propagate topo changes */
151 device_broadcast_event(&vldev
->dev
, DEV_EVENT_TOPO_CHANGE
);
159 vlan_config_init(struct device
*dev
)
161 struct vlan_device
*vldev
;
163 vldev
= container_of(dev
, struct vlan_device
, dev
);
164 vlan_hotplug_check(vldev
, vldev
->dep
.dev
);
167 static struct device
*get_vlan_device(struct device
*dev
, char *id_str
, bool create
)
169 static struct device_type vlan_type
= {
171 .config_params
= &device_attr_list
,
172 .config_init
= vlan_config_init
,
173 .free
= free_vlan_if
,
175 struct vlan_device
*vldev
;
176 struct device_user
*dep
;
177 char name
[IFNAMSIZ
+ 1];
181 id
= strtoul(id_str
, &err
, 10);
183 alias_id
= kvlist_get(&dev
->vlan_aliases
, id_str
);
190 /* look for an existing interface before creating a new one */
191 list_for_each_entry(dep
, &dev
->users
.list
, list
.list
) {
192 if (dep
->cb
!= vlan_dev_cb
)
195 vldev
= container_of(dep
, struct vlan_device
, dep
);
205 if (snprintf(name
, sizeof(name
), "%s.%d", dev
->ifname
, id
) >= sizeof(name
) - 1)
208 D(DEVICE
, "Create vlan device '%s'\n", name
);
210 vldev
= calloc(1, sizeof(*vldev
));
215 vldev
->dev
.hidden
= dev
->hidden
;
216 strcpy(vldev
->dev
.ifname
, name
);
218 if (device_init(&vldev
->dev
, &vlan_type
, NULL
) < 0)
221 vldev
->dev
.default_config
= true;
222 vldev
->dev
.config_pending
= true;
224 vldev
->set_state
= vldev
->dev
.set_state
;
225 vldev
->dev
.set_state
= vlan_set_device_state
;
227 vldev
->dep
.cb
= vlan_dev_cb
;
228 vlan_hotplug_check(vldev
, vldev
->dep
.dev
);
229 device_add_user(&vldev
->dep
, dev
);
234 device_cleanup(&vldev
->dev
);
239 static char *split_vlan(char *s
)
252 struct device
*get_vlan_device_chain(const char *ifname
, bool create
)
254 struct device
*dev
= NULL
;
255 char *buf
, *s
, *next
;
257 buf
= strdup(ifname
);
262 dev
= device_get(buf
, create
);
267 next
= split_vlan(s
);
268 dev
= get_vlan_device(dev
, s
, create
);