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