884e6efefaf439397843902cb2b855576da70835
[project/netifd.git] / vlandev.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2014 Gioacchino Mazzurco <gio@eigenlab.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
15 #include <string.h>
16
17 #include "netifd.h"
18 #include "device.h"
19 #include "interface.h"
20 #include "system.h"
21
22 enum {
23 VLANDEV_ATTR_TYPE,
24 VLANDEV_ATTR_IFNAME,
25 VLANDEV_ATTR_VID,
26 __VLANDEV_ATTR_MAX
27 };
28
29 static const struct blobmsg_policy vlandev_attrs[__VLANDEV_ATTR_MAX] = {
30 [VLANDEV_ATTR_TYPE] = { "type", BLOBMSG_TYPE_STRING },
31 [VLANDEV_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_STRING },
32 [VLANDEV_ATTR_VID] = { "vid", BLOBMSG_TYPE_INT32 },
33 };
34
35 static const struct uci_blob_param_list vlandev_attr_list = {
36 .n_params = __VLANDEV_ATTR_MAX,
37 .params = vlandev_attrs,
38
39 .n_next = 1,
40 .next = { &device_attr_list },
41 };
42
43 struct vlandev_device {
44 struct device dev;
45 struct device_user parent;
46
47 device_state_cb set_state;
48
49 struct blob_attr *config_data;
50 struct blob_attr *ifname;
51 struct vlandev_config config;
52 };
53
54 static void
55 vlandev_base_cb(struct device_user *dev, enum device_event ev)
56 {
57 struct vlandev_device *mvdev = container_of(dev, struct vlandev_device, parent);
58
59 switch (ev) {
60 case DEV_EVENT_ADD:
61 device_set_present(&mvdev->dev, true);
62 break;
63 case DEV_EVENT_REMOVE:
64 device_set_present(&mvdev->dev, false);
65 break;
66 case DEV_EVENT_LINK_UP:
67 device_set_link(&mvdev->dev, true);
68 break;
69 case DEV_EVENT_LINK_DOWN:
70 device_set_link(&mvdev->dev, false);
71 break;
72 default:
73 return;
74 }
75 }
76
77 static int
78 vlandev_set_down(struct vlandev_device *mvdev)
79 {
80 mvdev->set_state(&mvdev->dev, false);
81 system_vlandev_del(&mvdev->dev);
82 device_release(&mvdev->parent);
83
84 return 0;
85 }
86
87 static int
88 vlandev_set_up(struct vlandev_device *mvdev)
89 {
90 int ret;
91
92 ret = device_claim(&mvdev->parent);
93 if (ret < 0)
94 return ret;
95
96 ret = system_vlandev_add(&mvdev->dev, mvdev->parent.dev, &mvdev->config);
97 if (ret < 0)
98 goto release;
99
100 ret = mvdev->set_state(&mvdev->dev, true);
101 if (ret)
102 goto delete;
103
104 return 0;
105
106 delete:
107 system_vlandev_del(&mvdev->dev);
108 release:
109 device_release(&mvdev->parent);
110 return ret;
111 }
112
113 static int
114 vlandev_set_state(struct device *dev, bool up)
115 {
116 struct vlandev_device *mvdev;
117
118 D(SYSTEM, "vlandev_set_state(%s, %u)\n", dev->ifname, up);
119
120 mvdev = container_of(dev, struct vlandev_device, dev);
121 if (up)
122 return vlandev_set_up(mvdev);
123 else
124 return vlandev_set_down(mvdev);
125 }
126
127 static void
128 vlandev_free(struct device *dev)
129 {
130 struct vlandev_device *mvdev;
131
132 mvdev = container_of(dev, struct vlandev_device, dev);
133 device_remove_user(&mvdev->parent);
134 free(mvdev->config_data);
135 free(mvdev);
136 }
137
138 static void
139 vlandev_dump_info(struct device *dev, struct blob_buf *b)
140 {
141 struct vlandev_device *mvdev;
142
143 mvdev = container_of(dev, struct vlandev_device, dev);
144 blobmsg_add_string(b, "parent", mvdev->parent.dev->ifname);
145 system_if_dump_info(dev, b);
146 }
147
148 static void
149 vlandev_config_init(struct device *dev)
150 {
151 struct vlandev_device *mvdev;
152 struct device *basedev = NULL;
153
154 mvdev = container_of(dev, struct vlandev_device, dev);
155 if (mvdev->ifname)
156 basedev = device_get(blobmsg_data(mvdev->ifname), true);
157
158 device_add_user(&mvdev->parent, basedev);
159 }
160
161 static void
162 vlandev_apply_settings(struct vlandev_device *mvdev, struct blob_attr **tb)
163 {
164 struct vlandev_config *cfg = &mvdev->config;
165 struct blob_attr *cur;
166
167 cfg->proto = VLAN_PROTO_8021Q;
168 cfg->vid = 1;
169
170 if ((cur = tb[VLANDEV_ATTR_TYPE]))
171 {
172 if(!strcmp(blobmsg_data(cur), "8021ad"))
173 cfg->proto = VLAN_PROTO_8021AD;
174 }
175
176 if ((cur = tb[VLANDEV_ATTR_VID]))
177 cfg->vid = (uint16_t) blobmsg_get_u32(cur);
178 }
179
180 static enum dev_change_type
181 vlandev_reload(struct device *dev, struct blob_attr *attr)
182 {
183 struct blob_attr *tb_dev[__DEV_ATTR_MAX];
184 struct blob_attr *tb_mv[__VLANDEV_ATTR_MAX];
185 enum dev_change_type ret = DEV_CONFIG_APPLIED;
186 struct vlandev_device *mvdev;
187
188 mvdev = container_of(dev, struct vlandev_device, dev);
189 attr = blob_memdup(attr);
190
191 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
192 blob_data(attr), blob_len(attr));
193 blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, tb_mv,
194 blob_data(attr), blob_len(attr));
195
196 device_init_settings(dev, tb_dev);
197 vlandev_apply_settings(mvdev, tb_mv);
198 mvdev->ifname = tb_mv[VLANDEV_ATTR_IFNAME];
199
200 if (mvdev->config_data) {
201 struct blob_attr *otb_dev[__DEV_ATTR_MAX];
202 struct blob_attr *otb_mv[__VLANDEV_ATTR_MAX];
203
204 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
205 blob_data(mvdev->config_data), blob_len(mvdev->config_data));
206
207 if (uci_blob_diff(tb_dev, otb_dev, &device_attr_list, NULL))
208 ret = DEV_CONFIG_RESTART;
209
210 blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, otb_mv,
211 blob_data(mvdev->config_data), blob_len(mvdev->config_data));
212
213 if (uci_blob_diff(tb_mv, otb_mv, &vlandev_attr_list, NULL))
214 ret = DEV_CONFIG_RESTART;
215
216 vlandev_config_init(dev);
217 }
218
219 free(mvdev->config_data);
220 mvdev->config_data = attr;
221 return ret;
222 }
223
224 static struct device *
225 vlandev_create(const char *name, struct blob_attr *attr)
226 {
227 struct vlandev_device *mvdev;
228 struct device *dev = NULL;
229
230 mvdev = calloc(1, sizeof(*mvdev));
231 if (!mvdev)
232 return NULL;
233
234 dev = &mvdev->dev;
235 device_init(dev, &vlandev_device_type, name);
236 dev->config_pending = true;
237
238 mvdev->set_state = dev->set_state;
239 dev->set_state = vlandev_set_state;
240
241 dev->hotplug_ops = NULL;
242 mvdev->parent.cb = vlandev_base_cb;
243
244 vlandev_reload(dev, attr);
245
246 return dev;
247 }
248
249 const struct device_type vlandev_device_type = {
250 .name = "VLANDEV",
251 .config_params = &vlandev_attr_list,
252 .keep_link_status = true,
253
254 .create = vlandev_create,
255 .config_init = vlandev_config_init,
256 .reload = vlandev_reload,
257 .free = vlandev_free,
258 .dump_info = vlandev_dump_info,
259 };