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_add(struct device
*dev
, struct device
*member
, struct blob_attr
*vlan
)
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
);
56 return dev
->hotplug_ops
->add(dev
, member
, blobmsg_data(b
.head
));
60 vlan_hotplug_del(struct device
*dev
, struct device
*member
)
62 struct vlan_device
*vldev
= container_of(dev
, struct vlan_device
, dev
);
65 if (!dev
|| !dev
->hotplug_ops
)
66 return UBUS_STATUS_NOT_SUPPORTED
;
68 return dev
->hotplug_ops
->del(dev
, member
);
72 vlan_hotplug_prepare(struct device
*dev
)
74 struct vlan_device
*vldev
= container_of(dev
, struct vlan_device
, dev
);
77 if (!dev
|| !dev
->hotplug_ops
)
78 return UBUS_STATUS_NOT_SUPPORTED
;
80 return dev
->hotplug_ops
->prepare(dev
);
83 static void vlan_hotplug_check(struct vlan_device
*vldev
, struct device
*dev
)
85 static const struct device_hotplug_ops hotplug_ops
= {
86 .prepare
= vlan_hotplug_prepare
,
87 .add
= vlan_hotplug_add
,
88 .del
= vlan_hotplug_del
91 if (!dev
|| !dev
->hotplug_ops
|| avl_is_empty(&dev
->vlans
.avl
)) {
92 vldev
->dev
.hotplug_ops
= NULL
;
96 vldev
->dev
.hotplug_ops
= &hotplug_ops
;
100 static int vlan_set_device_state(struct device
*dev
, bool up
)
102 struct vlan_device
*vldev
;
105 vldev
= container_of(dev
, struct vlan_device
, dev
);
107 vldev
->set_state(dev
, false);
108 system_vlan_del(dev
);
109 device_release(&vldev
->dep
);
113 ret
= device_claim(&vldev
->dep
);
117 system_vlan_add(vldev
->dep
.dev
, vldev
->id
);
118 ret
= vldev
->set_state(dev
, true);
120 device_release(&vldev
->dep
);
125 static void vlan_dev_cb(struct device_user
*dep
, enum device_event ev
)
127 char name
[IFNAMSIZ
+ 1];
128 struct vlan_device
*vldev
;
130 vldev
= container_of(dep
, struct vlan_device
, dep
);
133 device_set_present(&vldev
->dev
, true);
135 case DEV_EVENT_REMOVE
:
136 device_set_present(&vldev
->dev
, false);
138 case DEV_EVENT_UPDATE_IFNAME
:
139 vlan_hotplug_check(vldev
, dep
->dev
);
140 vldev
->dev
.hidden
= dep
->dev
->hidden
;
141 if (snprintf(name
, sizeof(name
), "%s.%d", dep
->dev
->ifname
,
142 vldev
->id
) >= sizeof(name
) - 1 ||
143 device_set_ifname(&vldev
->dev
, name
))
144 free_vlan_if(&vldev
->dev
);
146 case DEV_EVENT_TOPO_CHANGE
:
147 /* Propagate topo changes */
148 device_broadcast_event(&vldev
->dev
, DEV_EVENT_TOPO_CHANGE
);
156 vlan_config_init(struct device
*dev
)
158 struct vlan_device
*vldev
;
160 vldev
= container_of(dev
, struct vlan_device
, dev
);
161 vlan_hotplug_check(vldev
, vldev
->dep
.dev
);
164 static struct device
*get_vlan_device(struct device
*dev
, int id
, bool create
)
166 static struct device_type vlan_type
= {
168 .config_params
= &device_attr_list
,
169 .config_init
= vlan_config_init
,
170 .free
= free_vlan_if
,
172 struct vlan_device
*vldev
;
173 struct device_user
*dep
;
174 char name
[IFNAMSIZ
+ 1];
176 /* look for an existing interface before creating a new one */
177 list_for_each_entry(dep
, &dev
->users
.list
, list
.list
) {
178 if (dep
->cb
!= vlan_dev_cb
)
181 vldev
= container_of(dep
, struct vlan_device
, dep
);
191 if (snprintf(name
, sizeof(name
), "%s.%d", dev
->ifname
, id
) >= sizeof(name
) - 1)
194 D(DEVICE
, "Create vlan device '%s'\n", name
);
196 vldev
= calloc(1, sizeof(*vldev
));
201 vldev
->dev
.hidden
= dev
->hidden
;
202 strcpy(vldev
->dev
.ifname
, name
);
204 if (device_init(&vldev
->dev
, &vlan_type
, NULL
) < 0)
207 vldev
->dev
.default_config
= true;
208 vldev
->dev
.config_pending
= true;
210 vldev
->set_state
= vldev
->dev
.set_state
;
211 vldev
->dev
.set_state
= vlan_set_device_state
;
213 vldev
->dep
.cb
= vlan_dev_cb
;
214 vlan_hotplug_check(vldev
, vldev
->dep
.dev
);
215 device_add_user(&vldev
->dep
, dev
);
220 device_cleanup(&vldev
->dev
);
225 static char *split_vlan(char *s
)
238 struct device
*get_vlan_device_chain(const char *ifname
, bool create
)
240 struct device
*dev
= NULL
;
241 char *buf
, *s
, *next
, *err
= NULL
;
244 buf
= strdup(ifname
);
249 dev
= device_get(buf
, create
);
254 next
= split_vlan(s
);
255 id
= strtoul(s
, &err
, 10);
259 dev
= get_vlan_device(dev
, id
, create
);