device: add macvlan support
[project/netifd.git] / macvlan.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
5 *
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
9 *
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.
14 */
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <assert.h>
19 #include <errno.h>
20 #include <netinet/ether.h>
21
22 #include "netifd.h"
23 #include "device.h"
24 #include "interface.h"
25 #include "system.h"
26
27 enum {
28 MACVLAN_ATTR_IFNAME,
29 MACVLAN_ATTR_MACADDR,
30 MACVLAN_ATTR_MODE,
31 __MACVLAN_ATTR_MAX
32 };
33
34 static const struct blobmsg_policy macvlan_attrs[__MACVLAN_ATTR_MAX] = {
35 [MACVLAN_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_STRING },
36 [MACVLAN_ATTR_MACADDR] = { "macaddr", BLOBMSG_TYPE_STRING },
37 [MACVLAN_ATTR_MODE] = { "mode", BLOBMSG_TYPE_STRING },
38 };
39
40 static const struct uci_blob_param_list macvlan_attr_list = {
41 .n_params = __MACVLAN_ATTR_MAX,
42 .params = macvlan_attrs,
43 };
44
45 struct macvlan_device {
46 struct device dev;
47 struct device_user parent;
48
49 device_state_cb set_state;
50
51 struct blob_attr *config_data;
52 struct blob_attr *ifname;
53 struct macvlan_config config;
54 };
55
56 static void
57 macvlan_base_cb(struct device_user *dev, enum device_event ev)
58 {
59 struct macvlan_device *mvdev = container_of(dev, struct macvlan_device, parent);
60
61 switch (ev) {
62 case DEV_EVENT_ADD:
63 device_set_present(&mvdev->dev, true);
64 break;
65 case DEV_EVENT_REMOVE:
66 device_set_present(&mvdev->dev, false);
67 break;
68 default:
69 return;
70 }
71 }
72
73 static int
74 macvlan_set_down(struct macvlan_device *mvdev)
75 {
76 mvdev->set_state(&mvdev->dev, false);
77 system_macvlan_del(&mvdev->dev);
78 device_release(&mvdev->parent);
79
80 return 0;
81 }
82
83 static int
84 macvlan_set_up(struct macvlan_device *mvdev)
85 {
86 int ret;
87
88 ret = device_claim(&mvdev->parent);
89 if (ret < 0)
90 return ret;
91
92 ret = system_macvlan_add(&mvdev->dev, mvdev->parent.dev, &mvdev->config);
93 if (ret < 0)
94 goto release;
95
96 ret = mvdev->set_state(&mvdev->dev, true);
97 if (ret)
98 goto delete;
99
100 return 0;
101
102 delete:
103 system_macvlan_del(&mvdev->dev);
104 release:
105 device_release(&mvdev->parent);
106 return ret;
107 }
108
109 static int
110 macvlan_set_state(struct device *dev, bool up)
111 {
112 struct macvlan_device *mvdev;
113
114 D(SYSTEM, "macvlan_set_state(%s, %u)\n", dev->ifname, up);
115
116 mvdev = container_of(dev, struct macvlan_device, dev);
117 if (up)
118 return macvlan_set_up(mvdev);
119 else
120 return macvlan_set_down(mvdev);
121 }
122
123 static void
124 macvlan_free(struct device *dev)
125 {
126 struct macvlan_device *mvdev;
127
128 mvdev = container_of(dev, struct macvlan_device, dev);
129 device_remove_user(&mvdev->parent);
130 free(mvdev);
131 }
132
133 static void
134 macvlan_dump_info(struct device *dev, struct blob_buf *b)
135 {
136 struct macvlan_device *mvdev;
137
138 mvdev = container_of(dev, struct macvlan_device, dev);
139 blobmsg_add_string(b, "parent", mvdev->parent.dev->ifname);
140 system_if_dump_info(dev, b);
141 }
142
143 static void
144 macvlan_config_init(struct device *dev)
145 {
146 struct macvlan_device *mvdev;
147 struct device *basedev = NULL;
148
149 mvdev = container_of(dev, struct macvlan_device, dev);
150 if (mvdev->ifname)
151 basedev = device_get(blobmsg_data(mvdev->ifname), true);
152
153 device_add_user(&mvdev->parent, basedev);
154 }
155
156 static void
157 macvlan_apply_settings(struct macvlan_device *mvdev, struct blob_attr **tb)
158 {
159 struct macvlan_config *cfg = &mvdev->config;
160 struct blob_attr *cur;
161 struct ether_addr *ea;
162
163 cfg->flags = 0;
164 cfg->mode = NULL;
165
166 if ((cur = tb[MACVLAN_ATTR_MACADDR])) {
167 ea = ether_aton(blobmsg_data(cur));
168 if (ea) {
169 memcpy(cfg->macaddr, ea, 6);
170 cfg->flags |= MACVLAN_OPT_MACADDR;
171 }
172 }
173
174 if ((cur = tb[MACVLAN_ATTR_MODE]))
175 cfg->mode = blobmsg_data(cur);
176 }
177
178 static enum dev_change_type
179 macvlan_reload(struct device *dev, struct blob_attr *attr)
180 {
181 struct blob_attr *tb_dev[__DEV_ATTR_MAX];
182 struct blob_attr *tb_mv[__MACVLAN_ATTR_MAX];
183 enum dev_change_type ret = DEV_CONFIG_APPLIED;
184 struct macvlan_device *mvdev;
185
186 mvdev = container_of(dev, struct macvlan_device, dev);
187
188 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
189 blob_data(attr), blob_len(attr));
190 blobmsg_parse(macvlan_attrs, __MACVLAN_ATTR_MAX, tb_mv,
191 blob_data(attr), blob_len(attr));
192
193 device_init_settings(dev, tb_dev);
194 macvlan_apply_settings(mvdev, tb_mv);
195 mvdev->ifname = tb_mv[MACVLAN_ATTR_IFNAME];
196
197 if (mvdev->config_data) {
198 struct blob_attr *otb_dev[__DEV_ATTR_MAX];
199 struct blob_attr *otb_mv[__MACVLAN_ATTR_MAX];
200
201 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
202 blob_data(mvdev->config_data), blob_len(mvdev->config_data));
203
204 if (uci_blob_diff(tb_dev, otb_dev, &device_attr_list, NULL))
205 ret = DEV_CONFIG_RESTART;
206
207 blobmsg_parse(macvlan_attrs, __MACVLAN_ATTR_MAX, otb_mv,
208 blob_data(mvdev->config_data), blob_len(mvdev->config_data));
209
210 if (uci_blob_diff(tb_mv, otb_mv, &macvlan_attr_list, NULL))
211 ret = DEV_CONFIG_RESTART;
212
213 macvlan_config_init(dev);
214 }
215
216 mvdev->config_data = attr;
217 return ret;
218 }
219
220 static struct device *
221 macvlan_create(const char *name, struct blob_attr *attr)
222 {
223 struct macvlan_device *mvdev;
224 struct device *dev = NULL;
225
226 mvdev = calloc(1, sizeof(*mvdev));
227 if (!mvdev)
228 return NULL;
229
230 dev = &mvdev->dev;
231 device_init(dev, &macvlan_device_type, name);
232 dev->config_pending = true;
233
234 mvdev->set_state = dev->set_state;
235 dev->set_state = macvlan_set_state;
236
237 dev->hotplug_ops = NULL;
238 mvdev->parent.cb = macvlan_base_cb;
239
240 macvlan_reload(dev, attr);
241
242 return dev;
243 }
244
245 const struct device_type macvlan_device_type = {
246 .name = "MAC VLAN",
247 .config_params = &macvlan_attr_list,
248
249 .create = macvlan_create,
250 .config_init = macvlan_config_init,
251 .reload = macvlan_reload,
252 .free = macvlan_free,
253 .dump_info = macvlan_dump_info,
254 };