2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
20 #include <net/ethernet.h>
23 #include <netinet/ether.h>
28 #include "interface.h"
38 static const struct blobmsg_policy macvlan_attrs[__MACVLAN_ATTR_MAX] = {
39 [MACVLAN_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_STRING },
40 [MACVLAN_ATTR_MACADDR] = { "macaddr", BLOBMSG_TYPE_STRING },
41 [MACVLAN_ATTR_MODE] = { "mode", BLOBMSG_TYPE_STRING },
44 static const struct uci_blob_param_list macvlan_attr_list = {
45 .n_params = __MACVLAN_ATTR_MAX,
46 .params = macvlan_attrs,
49 .next = { &device_attr_list },
52 struct macvlan_device {
54 struct device_user parent;
56 device_state_cb set_state;
58 struct blob_attr *config_data;
59 struct blob_attr *ifname;
60 struct macvlan_config config;
64 macvlan_base_cb(struct device_user *dev, enum device_event ev)
66 struct macvlan_device *mvdev = container_of(dev, struct macvlan_device, parent);
70 device_set_present(&mvdev->dev, true);
72 case DEV_EVENT_REMOVE:
73 device_set_present(&mvdev->dev, false);
75 case DEV_EVENT_LINK_UP:
76 device_set_link(&mvdev->dev, true);
78 case DEV_EVENT_LINK_DOWN:
79 device_set_link(&mvdev->dev, false);
87 macvlan_set_down(struct macvlan_device *mvdev)
89 mvdev->set_state(&mvdev->dev, false);
90 system_macvlan_del(&mvdev->dev);
91 device_release(&mvdev->parent);
97 macvlan_set_up(struct macvlan_device *mvdev)
101 ret = device_claim(&mvdev->parent);
105 ret = system_macvlan_add(&mvdev->dev, mvdev->parent.dev, &mvdev->config);
109 ret = mvdev->set_state(&mvdev->dev, true);
116 system_macvlan_del(&mvdev->dev);
118 device_release(&mvdev->parent);
123 macvlan_set_state(struct device *dev, bool up)
125 struct macvlan_device *mvdev;
127 D(SYSTEM, "macvlan_set_state(%s, %u)\n", dev->ifname, up);
129 mvdev = container_of(dev, struct macvlan_device, dev);
131 return macvlan_set_up(mvdev);
133 return macvlan_set_down(mvdev);
137 macvlan_free(struct device *dev)
139 struct macvlan_device *mvdev;
141 mvdev = container_of(dev, struct macvlan_device, dev);
142 device_remove_user(&mvdev->parent);
147 macvlan_dump_info(struct device *dev, struct blob_buf *b)
149 struct macvlan_device *mvdev;
151 mvdev = container_of(dev, struct macvlan_device, dev);
152 blobmsg_add_string(b, "parent", mvdev->parent.dev->ifname);
153 system_if_dump_info(dev, b);
157 macvlan_config_init(struct device *dev)
159 struct macvlan_device *mvdev;
160 struct device *basedev = NULL;
162 mvdev = container_of(dev, struct macvlan_device, dev);
164 basedev = device_get(blobmsg_data(mvdev->ifname), true);
166 device_add_user(&mvdev->parent, basedev);
170 macvlan_apply_settings(struct macvlan_device *mvdev, struct blob_attr **tb)
172 struct macvlan_config *cfg = &mvdev->config;
173 struct blob_attr *cur;
174 struct ether_addr *ea;
179 if ((cur = tb[MACVLAN_ATTR_MACADDR])) {
180 ea = ether_aton(blobmsg_data(cur));
182 memcpy(cfg->macaddr, ea, 6);
183 cfg->flags |= MACVLAN_OPT_MACADDR;
187 if ((cur = tb[MACVLAN_ATTR_MODE]))
188 cfg->mode = blobmsg_data(cur);
191 static enum dev_change_type
192 macvlan_reload(struct device *dev, struct blob_attr *attr)
194 struct blob_attr *tb_dev[__DEV_ATTR_MAX];
195 struct blob_attr *tb_mv[__MACVLAN_ATTR_MAX];
196 enum dev_change_type ret = DEV_CONFIG_APPLIED;
197 struct macvlan_device *mvdev;
199 mvdev = container_of(dev, struct macvlan_device, dev);
201 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
202 blob_data(attr), blob_len(attr));
203 blobmsg_parse(macvlan_attrs, __MACVLAN_ATTR_MAX, tb_mv,
204 blob_data(attr), blob_len(attr));
206 device_init_settings(dev, tb_dev);
207 macvlan_apply_settings(mvdev, tb_mv);
208 mvdev->ifname = tb_mv[MACVLAN_ATTR_IFNAME];
210 if (mvdev->config_data) {
211 struct blob_attr *otb_dev[__DEV_ATTR_MAX];
212 struct blob_attr *otb_mv[__MACVLAN_ATTR_MAX];
214 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
215 blob_data(mvdev->config_data), blob_len(mvdev->config_data));
217 if (uci_blob_diff(tb_dev, otb_dev, &device_attr_list, NULL))
218 ret = DEV_CONFIG_RESTART;
220 blobmsg_parse(macvlan_attrs, __MACVLAN_ATTR_MAX, otb_mv,
221 blob_data(mvdev->config_data), blob_len(mvdev->config_data));
223 if (uci_blob_diff(tb_mv, otb_mv, &macvlan_attr_list, NULL))
224 ret = DEV_CONFIG_RESTART;
226 macvlan_config_init(dev);
229 mvdev->config_data = attr;
233 static struct device *
234 macvlan_create(const char *name, struct blob_attr *attr)
236 struct macvlan_device *mvdev;
237 struct device *dev = NULL;
239 mvdev = calloc(1, sizeof(*mvdev));
244 device_init(dev, &macvlan_device_type, name);
245 dev->config_pending = true;
247 mvdev->set_state = dev->set_state;
248 dev->set_state = macvlan_set_state;
250 dev->hotplug_ops = NULL;
251 mvdev->parent.cb = macvlan_base_cb;
253 macvlan_reload(dev, attr);
258 const struct device_type macvlan_device_type = {
260 .config_params = &macvlan_attr_list,
261 .keep_link_status = true,
263 .create = macvlan_create,
264 .config_init = macvlan_config_init,
265 .reload = macvlan_reload,
266 .free = macvlan_free,
267 .dump_info = macvlan_dump_info,