system-linux: fix memory leak on error in system_if_check
[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 case DEV_EVENT_LINK_UP:
76 device_set_link(&mvdev->dev, true);
77 break;
78 case DEV_EVENT_LINK_DOWN:
79 device_set_link(&mvdev->dev, false);
80 break;
81 default:
82 return;
83 }
84 }
85
86 static int
87 macvlan_set_down(struct macvlan_device *mvdev)
88 {
89 mvdev->set_state(&mvdev->dev, false);
90 system_macvlan_del(&mvdev->dev);
91 device_release(&mvdev->parent);
92
93 return 0;
94 }
95
96 static int
97 macvlan_set_up(struct macvlan_device *mvdev)
98 {
99 int ret;
100
101 ret = device_claim(&mvdev->parent);
102 if (ret < 0)
103 return ret;
104
105 ret = system_macvlan_add(&mvdev->dev, mvdev->parent.dev, &mvdev->config);
106 if (ret < 0)
107 goto release;
108
109 ret = mvdev->set_state(&mvdev->dev, true);
110 if (ret)
111 goto delete;
112
113 return 0;
114
115 delete:
116 system_macvlan_del(&mvdev->dev);
117 release:
118 device_release(&mvdev->parent);
119 return ret;
120 }
121
122 static int
123 macvlan_set_state(struct device *dev, bool up)
124 {
125 struct macvlan_device *mvdev;
126
127 D(SYSTEM, "macvlan_set_state(%s, %u)\n", dev->ifname, up);
128
129 mvdev = container_of(dev, struct macvlan_device, dev);
130 if (up)
131 return macvlan_set_up(mvdev);
132 else
133 return macvlan_set_down(mvdev);
134 }
135
136 static void
137 macvlan_free(struct device *dev)
138 {
139 struct macvlan_device *mvdev;
140
141 mvdev = container_of(dev, struct macvlan_device, dev);
142 device_remove_user(&mvdev->parent);
143 free(mvdev->config_data);
144 free(mvdev);
145 }
146
147 static void
148 macvlan_dump_info(struct device *dev, struct blob_buf *b)
149 {
150 struct macvlan_device *mvdev;
151
152 mvdev = container_of(dev, struct macvlan_device, dev);
153 blobmsg_add_string(b, "parent", mvdev->parent.dev->ifname);
154 system_if_dump_info(dev, b);
155 }
156
157 static void
158 macvlan_config_init(struct device *dev)
159 {
160 struct macvlan_device *mvdev;
161 struct device *basedev = NULL;
162
163 mvdev = container_of(dev, struct macvlan_device, dev);
164 if (mvdev->ifname)
165 basedev = device_get(blobmsg_data(mvdev->ifname), true);
166
167 device_add_user(&mvdev->parent, basedev);
168 }
169
170 static void
171 macvlan_apply_settings(struct macvlan_device *mvdev, struct blob_attr **tb)
172 {
173 struct macvlan_config *cfg = &mvdev->config;
174 struct blob_attr *cur;
175 struct ether_addr *ea;
176
177 cfg->flags = 0;
178 cfg->mode = NULL;
179
180 if ((cur = tb[MACVLAN_ATTR_MACADDR])) {
181 ea = ether_aton(blobmsg_data(cur));
182 if (ea) {
183 memcpy(cfg->macaddr, ea, 6);
184 cfg->flags |= MACVLAN_OPT_MACADDR;
185 }
186 }
187
188 if ((cur = tb[MACVLAN_ATTR_MODE]))
189 cfg->mode = blobmsg_data(cur);
190 }
191
192 static enum dev_change_type
193 macvlan_reload(struct device *dev, struct blob_attr *attr)
194 {
195 struct blob_attr *tb_dev[__DEV_ATTR_MAX];
196 struct blob_attr *tb_mv[__MACVLAN_ATTR_MAX];
197 enum dev_change_type ret = DEV_CONFIG_APPLIED;
198 struct macvlan_device *mvdev;
199
200 mvdev = container_of(dev, struct macvlan_device, dev);
201 attr = blob_memdup(attr);
202
203 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
204 blob_data(attr), blob_len(attr));
205 blobmsg_parse(macvlan_attrs, __MACVLAN_ATTR_MAX, tb_mv,
206 blob_data(attr), blob_len(attr));
207
208 device_init_settings(dev, tb_dev);
209 macvlan_apply_settings(mvdev, tb_mv);
210 mvdev->ifname = tb_mv[MACVLAN_ATTR_IFNAME];
211
212 if (mvdev->config_data) {
213 struct blob_attr *otb_dev[__DEV_ATTR_MAX];
214 struct blob_attr *otb_mv[__MACVLAN_ATTR_MAX];
215
216 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
217 blob_data(mvdev->config_data), blob_len(mvdev->config_data));
218
219 if (uci_blob_diff(tb_dev, otb_dev, &device_attr_list, NULL))
220 ret = DEV_CONFIG_RESTART;
221
222 blobmsg_parse(macvlan_attrs, __MACVLAN_ATTR_MAX, otb_mv,
223 blob_data(mvdev->config_data), blob_len(mvdev->config_data));
224
225 if (uci_blob_diff(tb_mv, otb_mv, &macvlan_attr_list, NULL))
226 ret = DEV_CONFIG_RESTART;
227
228 macvlan_config_init(dev);
229 }
230
231 free(mvdev->config_data);
232 mvdev->config_data = attr;
233 return ret;
234 }
235
236 static struct device *
237 macvlan_create(const char *name, struct blob_attr *attr)
238 {
239 struct macvlan_device *mvdev;
240 struct device *dev = NULL;
241
242 mvdev = calloc(1, sizeof(*mvdev));
243 if (!mvdev)
244 return NULL;
245
246 dev = &mvdev->dev;
247 device_init(dev, &macvlan_device_type, name);
248 dev->config_pending = true;
249
250 mvdev->set_state = dev->set_state;
251 dev->set_state = macvlan_set_state;
252
253 dev->hotplug_ops = NULL;
254 mvdev->parent.cb = macvlan_base_cb;
255
256 macvlan_reload(dev, attr);
257
258 return dev;
259 }
260
261 const struct device_type macvlan_device_type = {
262 .name = "MAC VLAN",
263 .config_params = &macvlan_attr_list,
264 .keep_link_status = true,
265
266 .create = macvlan_create,
267 .config_init = macvlan_config_init,
268 .reload = macvlan_reload,
269 .free = macvlan_free,
270 .dump_info = macvlan_dump_info,
271 };