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