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.
23 #include "interface.h"
33 static const struct blobmsg_policy macvlan_attrs
[__MACVLAN_ATTR_MAX
] = {
34 [MACVLAN_ATTR_IFNAME
] = { "ifname", BLOBMSG_TYPE_STRING
},
35 [MACVLAN_ATTR_MACADDR
] = { "macaddr", BLOBMSG_TYPE_STRING
},
36 [MACVLAN_ATTR_MODE
] = { "mode", BLOBMSG_TYPE_STRING
},
39 static const struct uci_blob_param_list macvlan_attr_list
= {
40 .n_params
= __MACVLAN_ATTR_MAX
,
41 .params
= macvlan_attrs
,
44 .next
= { &device_attr_list
},
47 struct macvlan_device
{
49 struct device_user parent
;
51 device_state_cb set_state
;
53 struct blob_attr
*config_data
;
54 struct blob_attr
*ifname
;
55 struct macvlan_config config
;
59 macvlan_base_cb(struct device_user
*dev
, enum device_event ev
)
61 struct macvlan_device
*mvdev
= container_of(dev
, struct macvlan_device
, parent
);
65 device_set_present(&mvdev
->dev
, true);
67 case DEV_EVENT_REMOVE
:
68 device_set_present(&mvdev
->dev
, false);
76 macvlan_set_down(struct macvlan_device
*mvdev
)
78 mvdev
->set_state(&mvdev
->dev
, false);
79 system_macvlan_del(&mvdev
->dev
);
80 device_release(&mvdev
->parent
);
86 macvlan_set_up(struct macvlan_device
*mvdev
)
90 ret
= device_claim(&mvdev
->parent
);
94 ret
= system_macvlan_add(&mvdev
->dev
, mvdev
->parent
.dev
, &mvdev
->config
);
98 ret
= mvdev
->set_state(&mvdev
->dev
, true);
105 system_macvlan_del(&mvdev
->dev
);
107 device_release(&mvdev
->parent
);
112 macvlan_set_state(struct device
*dev
, bool up
)
114 struct macvlan_device
*mvdev
;
116 D(SYSTEM
, "macvlan_set_state(%s, %u)\n", dev
->ifname
, up
);
118 mvdev
= container_of(dev
, struct macvlan_device
, dev
);
120 return macvlan_set_up(mvdev
);
122 return macvlan_set_down(mvdev
);
126 macvlan_free(struct device
*dev
)
128 struct macvlan_device
*mvdev
;
130 mvdev
= container_of(dev
, struct macvlan_device
, dev
);
131 device_remove_user(&mvdev
->parent
);
132 free(mvdev
->config_data
);
137 macvlan_dump_info(struct device
*dev
, struct blob_buf
*b
)
139 struct macvlan_device
*mvdev
;
141 mvdev
= container_of(dev
, struct macvlan_device
, dev
);
142 blobmsg_add_string(b
, "parent", mvdev
->parent
.dev
->ifname
);
143 system_if_dump_info(dev
, b
);
147 macvlan_config_init(struct device
*dev
)
149 struct macvlan_device
*mvdev
;
150 struct device
*basedev
= NULL
;
152 mvdev
= container_of(dev
, struct macvlan_device
, dev
);
154 basedev
= device_get(blobmsg_data(mvdev
->ifname
), true);
156 device_add_user(&mvdev
->parent
, basedev
);
160 macvlan_apply_settings(struct macvlan_device
*mvdev
, struct blob_attr
**tb
)
162 struct macvlan_config
*cfg
= &mvdev
->config
;
163 struct blob_attr
*cur
;
164 struct ether_addr
*ea
;
169 if ((cur
= tb
[MACVLAN_ATTR_MACADDR
])) {
170 ea
= ether_aton(blobmsg_data(cur
));
172 memcpy(cfg
->macaddr
, ea
, 6);
173 cfg
->flags
|= MACVLAN_OPT_MACADDR
;
177 if ((cur
= tb
[MACVLAN_ATTR_MODE
]))
178 cfg
->mode
= blobmsg_data(cur
);
181 static enum dev_change_type
182 macvlan_reload(struct device
*dev
, struct blob_attr
*attr
)
184 struct blob_attr
*tb_dev
[__DEV_ATTR_MAX
];
185 struct blob_attr
*tb_mv
[__MACVLAN_ATTR_MAX
];
186 enum dev_change_type ret
= DEV_CONFIG_APPLIED
;
187 struct macvlan_device
*mvdev
;
189 mvdev
= container_of(dev
, struct macvlan_device
, dev
);
190 attr
= blob_memdup(attr
);
192 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, tb_dev
,
193 blob_data(attr
), blob_len(attr
));
194 blobmsg_parse(macvlan_attrs
, __MACVLAN_ATTR_MAX
, tb_mv
,
195 blob_data(attr
), blob_len(attr
));
197 device_init_settings(dev
, tb_dev
);
198 macvlan_apply_settings(mvdev
, tb_mv
);
199 mvdev
->ifname
= tb_mv
[MACVLAN_ATTR_IFNAME
];
201 if (mvdev
->config_data
) {
202 struct blob_attr
*otb_dev
[__DEV_ATTR_MAX
];
203 struct blob_attr
*otb_mv
[__MACVLAN_ATTR_MAX
];
205 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb_dev
,
206 blob_data(mvdev
->config_data
), blob_len(mvdev
->config_data
));
208 if (uci_blob_diff(tb_dev
, otb_dev
, &device_attr_list
, NULL
))
209 ret
= DEV_CONFIG_RESTART
;
211 blobmsg_parse(macvlan_attrs
, __MACVLAN_ATTR_MAX
, otb_mv
,
212 blob_data(mvdev
->config_data
), blob_len(mvdev
->config_data
));
214 if (uci_blob_diff(tb_mv
, otb_mv
, &macvlan_attr_list
, NULL
))
215 ret
= DEV_CONFIG_RESTART
;
217 macvlan_config_init(dev
);
220 free(mvdev
->config_data
);
221 mvdev
->config_data
= attr
;
225 static struct device
*
226 macvlan_create(const char *name
, struct device_type
*devtype
,
227 struct blob_attr
*attr
)
229 struct macvlan_device
*mvdev
;
230 struct device
*dev
= NULL
;
232 mvdev
= calloc(1, sizeof(*mvdev
));
237 if (device_init(dev
, devtype
, name
) < 0) {
243 dev
->config_pending
= true;
245 mvdev
->set_state
= dev
->set_state
;
246 dev
->set_state
= macvlan_set_state
;
248 dev
->hotplug_ops
= NULL
;
249 mvdev
->parent
.cb
= macvlan_base_cb
;
251 macvlan_reload(dev
, attr
);
256 static struct device_type macvlan_device_type
= {
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
,
266 static void __init
macvlan_device_type_init(void)
268 device_type_add(&macvlan_device_type
);