bridge: only overwrite implicit vlan assignment if vlans are configured
[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)
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);
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, int id, 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
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)
179 continue;
180
181 vldev = container_of(dep, struct vlan_device, dep);
182 if (vldev->id != id)
183 continue;
184
185 return &vldev->dev;
186 }
187
188 if (!create)
189 return NULL;
190
191 if (snprintf(name, sizeof(name), "%s.%d", dev->ifname, id) >= sizeof(name) - 1)
192 return NULL;
193
194 D(DEVICE, "Create vlan device '%s'\n", name);
195
196 vldev = calloc(1, sizeof(*vldev));
197 if (!vldev)
198 return NULL;
199
200 vldev->id = id;
201 vldev->dev.hidden = dev->hidden;
202 strcpy(vldev->dev.ifname, name);
203
204 if (device_init(&vldev->dev, &vlan_type, NULL) < 0)
205 goto error;
206
207 vldev->dev.default_config = true;
208 vldev->dev.config_pending = true;
209
210 vldev->set_state = vldev->dev.set_state;
211 vldev->dev.set_state = vlan_set_device_state;
212
213 vldev->dep.cb = vlan_dev_cb;
214 vlan_hotplug_check(vldev, vldev->dep.dev);
215 device_add_user(&vldev->dep, dev);
216
217 return &vldev->dev;
218
219 error:
220 device_cleanup(&vldev->dev);
221 free(vldev);
222 return NULL;
223 }
224
225 static char *split_vlan(char *s)
226 {
227 s = strchr(s, '.');
228 if (!s)
229 goto out;
230
231 *s = 0;
232 s++;
233
234 out:
235 return s;
236 }
237
238 struct device *get_vlan_device_chain(const char *ifname, bool create)
239 {
240 struct device *dev = NULL;
241 char *buf, *s, *next, *err = NULL;
242 int id;
243
244 buf = strdup(ifname);
245 if (!buf)
246 return NULL;
247
248 s = split_vlan(buf);
249 dev = device_get(buf, create);
250 if (!dev)
251 goto error;
252
253 do {
254 next = split_vlan(s);
255 id = strtoul(s, &err, 10);
256 if (err && *err)
257 goto error;
258
259 dev = get_vlan_device(dev, id, create);
260 if (!dev)
261 goto error;
262
263 s = next;
264 if (!s)
265 goto out;
266 } while (1);
267
268 error:
269 dev = NULL;
270 out:
271 free(buf);
272 return dev;
273 }