82b2d66821a2c9800c2a7718bec5dee483ca047b
[project/netifd.git] / vlan.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
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
8 *
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.
13 */
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17
18 #include "netifd.h"
19 #include "system.h"
20
21 static struct blob_buf b;
22
23 struct vlan_device {
24 struct device dev;
25 struct device_user dep;
26
27 device_state_cb set_state;
28 int id;
29 };
30
31 static void free_vlan_if(struct device *iface)
32 {
33 struct vlan_device *vldev;
34
35 vldev = container_of(iface, struct vlan_device, dev);
36 device_remove_user(&vldev->dep);
37 device_cleanup(&vldev->dev);
38 free(vldev);
39 }
40
41 static int
42 vlan_hotplug_add(struct device *dev, struct device *member, struct blob_attr *vlan)
43 {
44 struct vlan_device *vldev = container_of(dev, struct vlan_device, dev);
45 void *a;
46
47 dev = vldev->dep.dev;
48 if (!dev || !dev->hotplug_ops)
49 return UBUS_STATUS_NOT_SUPPORTED;
50
51 blob_buf_init(&b, 0);
52 a = blobmsg_open_array(&b, "vlans");
53 blobmsg_printf(&b, NULL, "%d", vldev->id);
54 blobmsg_close_array(&b, a);
55
56 return dev->hotplug_ops->add(dev, member, blobmsg_data(b.head));
57 }
58
59 static int
60 vlan_hotplug_del(struct device *dev, struct device *member)
61 {
62 struct vlan_device *vldev = container_of(dev, struct vlan_device, dev);
63
64 dev = vldev->dep.dev;
65 if (!dev || !dev->hotplug_ops)
66 return UBUS_STATUS_NOT_SUPPORTED;
67
68 return dev->hotplug_ops->del(dev, member);
69 }
70
71 static int
72 vlan_hotplug_prepare(struct device *dev, struct device **bridge_dev)
73 {
74 struct vlan_device *vldev = container_of(dev, struct vlan_device, dev);
75
76 dev = vldev->dep.dev;
77 if (!dev || !dev->hotplug_ops)
78 return UBUS_STATUS_NOT_SUPPORTED;
79
80 return dev->hotplug_ops->prepare(dev, bridge_dev);
81 }
82
83 static void vlan_hotplug_check(struct vlan_device *vldev, struct device *dev)
84 {
85 static const struct device_hotplug_ops hotplug_ops = {
86 .prepare = vlan_hotplug_prepare,
87 .add = vlan_hotplug_add,
88 .del = vlan_hotplug_del
89 };
90
91 if (!dev || !dev->hotplug_ops || avl_is_empty(&dev->vlans.avl)) {
92 vldev->dev.hotplug_ops = NULL;
93 return;
94 }
95
96 vldev->dev.hotplug_ops = &hotplug_ops;
97 }
98
99
100 static int vlan_set_device_state(struct device *dev, bool up)
101 {
102 struct vlan_device *vldev;
103 int ret = 0;
104
105 vldev = container_of(dev, struct vlan_device, dev);
106 if (!up) {
107 vldev->set_state(dev, false);
108 system_vlan_del(dev);
109 device_release(&vldev->dep);
110 return 0;
111 }
112
113 ret = device_claim(&vldev->dep);
114 if (ret < 0)
115 return ret;
116
117 system_vlan_add(vldev->dep.dev, vldev->id);
118 ret = vldev->set_state(dev, true);
119 if (ret)
120 device_release(&vldev->dep);
121
122 return ret;
123 }
124
125 static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
126 {
127 char name[IFNAMSIZ + 1];
128 struct vlan_device *vldev;
129
130 vldev = container_of(dep, struct vlan_device, dep);
131 switch(ev) {
132 case DEV_EVENT_ADD:
133 device_set_present(&vldev->dev, true);
134 break;
135 case DEV_EVENT_REMOVE:
136 device_set_present(&vldev->dev, false);
137 break;
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);
145 break;
146 case DEV_EVENT_TOPO_CHANGE:
147 /* Propagate topo changes */
148 device_broadcast_event(&vldev->dev, DEV_EVENT_TOPO_CHANGE);
149 break;
150 default:
151 break;
152 }
153 }
154
155 static void
156 vlan_config_init(struct device *dev)
157 {
158 struct vlan_device *vldev;
159
160 vldev = container_of(dev, struct vlan_device, dev);
161 vlan_hotplug_check(vldev, vldev->dep.dev);
162 }
163
164 static struct device *get_vlan_device(struct device *dev, char *id_str, bool create)
165 {
166 static struct device_type vlan_type = {
167 .name = "VLAN",
168 .config_params = &device_attr_list,
169 .config_init = vlan_config_init,
170 .free = free_vlan_if,
171 };
172 struct vlan_device *vldev;
173 struct device_user *dep;
174 char name[IFNAMSIZ + 1];
175 char *err = NULL;
176 int id, *alias_id;
177
178 id = strtoul(id_str, &err, 10);
179 if (err && *err) {
180 alias_id = kvlist_get(&dev->vlan_aliases, id_str);
181 if (!alias_id)
182 return NULL;
183
184 id = *alias_id;
185 }
186
187 /* look for an existing interface before creating a new one */
188 list_for_each_entry(dep, &dev->users.list, list.list) {
189 if (dep->cb != vlan_dev_cb)
190 continue;
191
192 vldev = container_of(dep, struct vlan_device, dep);
193 if (vldev->id != id)
194 continue;
195
196 return &vldev->dev;
197 }
198
199 if (!create)
200 return NULL;
201
202 if (snprintf(name, sizeof(name), "%s.%d", dev->ifname, id) >= sizeof(name) - 1)
203 return NULL;
204
205 D(DEVICE, "Create vlan device '%s'\n", name);
206
207 vldev = calloc(1, sizeof(*vldev));
208 if (!vldev)
209 return NULL;
210
211 vldev->id = id;
212 vldev->dev.hidden = dev->hidden;
213 strcpy(vldev->dev.ifname, name);
214
215 if (device_init(&vldev->dev, &vlan_type, NULL) < 0)
216 goto error;
217
218 vldev->dev.default_config = true;
219 vldev->dev.config_pending = true;
220
221 vldev->set_state = vldev->dev.set_state;
222 vldev->dev.set_state = vlan_set_device_state;
223
224 vldev->dep.cb = vlan_dev_cb;
225 vlan_hotplug_check(vldev, vldev->dep.dev);
226 device_add_user(&vldev->dep, dev);
227
228 return &vldev->dev;
229
230 error:
231 device_cleanup(&vldev->dev);
232 free(vldev);
233 return NULL;
234 }
235
236 static char *split_vlan(char *s)
237 {
238 s = strchr(s, '.');
239 if (!s)
240 goto out;
241
242 *s = 0;
243 s++;
244
245 out:
246 return s;
247 }
248
249 struct device *get_vlan_device_chain(const char *ifname, bool create)
250 {
251 struct device *dev = NULL;
252 char *buf, *s, *next;
253
254 buf = strdup(ifname);
255 if (!buf)
256 return NULL;
257
258 s = split_vlan(buf);
259 dev = device_get(buf, create);
260 if (!dev)
261 goto error;
262
263 do {
264 next = split_vlan(s);
265 dev = get_vlan_device(dev, s, create);
266 if (!dev)
267 goto error;
268
269 s = next;
270 if (!s)
271 goto out;
272 } while (1);
273
274 error:
275 dev = NULL;
276 out:
277 free(buf);
278 return dev;
279 }