2 * netifd - network interface daemon
3 * Copyright (C) 2014 Gioacchino Mazzurco <gio@eigenlab.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.
19 #include "interface.h"
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 },
35 static const struct uci_blob_param_list vlandev_attr_list = {
36 .n_params = __VLANDEV_ATTR_MAX,
37 .params = vlandev_attrs,
40 .next = { &device_attr_list },
43 struct vlandev_device {
45 struct device_user parent;
47 device_state_cb set_state;
49 struct blob_attr *config_data;
50 struct blob_attr *ifname;
51 struct vlandev_config config;
55 vlandev_base_cb(struct device_user *dev, enum device_event ev)
57 struct vlandev_device *mvdev = container_of(dev, struct vlandev_device, parent);
61 device_set_present(&mvdev->dev, true);
63 case DEV_EVENT_REMOVE:
64 device_set_present(&mvdev->dev, false);
66 case DEV_EVENT_LINK_UP:
67 device_set_link(&mvdev->dev, true);
69 case DEV_EVENT_LINK_DOWN:
70 device_set_link(&mvdev->dev, false);
78 vlandev_set_down(struct vlandev_device *mvdev)
80 mvdev->set_state(&mvdev->dev, false);
81 system_vlandev_del(&mvdev->dev);
82 device_release(&mvdev->parent);
88 vlandev_set_up(struct vlandev_device *mvdev)
92 ret = device_claim(&mvdev->parent);
96 ret = system_vlandev_add(&mvdev->dev, mvdev->parent.dev, &mvdev->config);
100 ret = mvdev->set_state(&mvdev->dev, true);
107 system_vlandev_del(&mvdev->dev);
109 device_release(&mvdev->parent);
114 vlandev_set_state(struct device *dev, bool up)
116 struct vlandev_device *mvdev;
118 D(SYSTEM, "vlandev_set_state(%s, %u)\n", dev->ifname, up);
120 mvdev = container_of(dev, struct vlandev_device, dev);
122 return vlandev_set_up(mvdev);
124 return vlandev_set_down(mvdev);
128 vlandev_free(struct device *dev)
130 struct vlandev_device *mvdev;
132 mvdev = container_of(dev, struct vlandev_device, dev);
133 device_remove_user(&mvdev->parent);
138 vlandev_dump_info(struct device *dev, struct blob_buf *b)
140 struct vlandev_device *mvdev;
142 mvdev = container_of(dev, struct vlandev_device, dev);
143 blobmsg_add_string(b, "parent", mvdev->parent.dev->ifname);
144 system_if_dump_info(dev, b);
148 vlandev_config_init(struct device *dev)
150 struct vlandev_device *mvdev;
151 struct device *basedev = NULL;
153 mvdev = container_of(dev, struct vlandev_device, dev);
155 basedev = device_get(blobmsg_data(mvdev->ifname), true);
157 device_add_user(&mvdev->parent, basedev);
161 vlandev_apply_settings(struct vlandev_device *mvdev, struct blob_attr **tb)
163 struct vlandev_config *cfg = &mvdev->config;
164 struct blob_attr *cur;
166 cfg->proto = VLAN_PROTO_8021Q;
169 if ((cur = tb[VLANDEV_ATTR_TYPE]))
171 if(!strcmp(blobmsg_data(cur), "8021ad"))
172 cfg->proto = VLAN_PROTO_8021AD;
175 if ((cur = tb[VLANDEV_ATTR_VID]))
176 cfg->vid = (uint16_t) blobmsg_get_u32(cur);
179 static enum dev_change_type
180 vlandev_reload(struct device *dev, struct blob_attr *attr)
182 struct blob_attr *tb_dev[__DEV_ATTR_MAX];
183 struct blob_attr *tb_mv[__VLANDEV_ATTR_MAX];
184 enum dev_change_type ret = DEV_CONFIG_APPLIED;
185 struct vlandev_device *mvdev;
187 mvdev = container_of(dev, struct vlandev_device, dev);
189 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
190 blob_data(attr), blob_len(attr));
191 blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, tb_mv,
192 blob_data(attr), blob_len(attr));
194 device_init_settings(dev, tb_dev);
195 vlandev_apply_settings(mvdev, tb_mv);
196 mvdev->ifname = tb_mv[VLANDEV_ATTR_IFNAME];
198 if (mvdev->config_data) {
199 struct blob_attr *otb_dev[__DEV_ATTR_MAX];
200 struct blob_attr *otb_mv[__VLANDEV_ATTR_MAX];
202 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
203 blob_data(mvdev->config_data), blob_len(mvdev->config_data));
205 if (uci_blob_diff(tb_dev, otb_dev, &device_attr_list, NULL))
206 ret = DEV_CONFIG_RESTART;
208 blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, otb_mv,
209 blob_data(mvdev->config_data), blob_len(mvdev->config_data));
211 if (uci_blob_diff(tb_mv, otb_mv, &vlandev_attr_list, NULL))
212 ret = DEV_CONFIG_RESTART;
214 vlandev_config_init(dev);
217 mvdev->config_data = attr;
221 static struct device *
222 vlandev_create(const char *name, struct blob_attr *attr)
224 struct vlandev_device *mvdev;
225 struct device *dev = NULL;
227 mvdev = calloc(1, sizeof(*mvdev));
232 device_init(dev, &vlandev_device_type, name);
233 dev->config_pending = true;
235 mvdev->set_state = dev->set_state;
236 dev->set_state = vlandev_set_state;
238 dev->hotplug_ops = NULL;
239 mvdev->parent.cb = vlandev_base_cb;
241 vlandev_reload(dev, attr);
246 const struct device_type vlandev_device_type = {
248 .config_params = &vlandev_attr_list,
250 .create = vlandev_create,
251 .config_init = vlandev_config_init,
252 .reload = vlandev_reload,
253 .free = vlandev_free,
254 .dump_info = vlandev_dump_info,