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 * Copyright (C) 2017 Matthias Schiffer <mschiffer@universe-factory.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
24 #include "interface.h"
30 VETH_ATTR_PEER_MACADDR
,
34 static const struct blobmsg_policy veth_attrs
[__VETH_ATTR_MAX
] = {
35 [VETH_ATTR_MACADDR
] = { "macaddr", BLOBMSG_TYPE_STRING
},
36 [VETH_ATTR_PEER_NAME
] = { "peer_name", BLOBMSG_TYPE_STRING
},
37 [VETH_ATTR_PEER_MACADDR
] = { "peer_macaddr", BLOBMSG_TYPE_STRING
},
40 static const struct uci_blob_param_list veth_attr_list
= {
41 .n_params
= __VETH_ATTR_MAX
,
45 .next
= { &device_attr_list
},
51 device_state_cb set_state
;
53 struct blob_attr
*config_data
;
54 struct veth_config config
;
58 veth_set_down(struct veth
*veth
)
60 veth
->set_state(&veth
->dev
, false);
61 system_veth_del(&veth
->dev
);
67 veth_set_up(struct veth
*veth
)
71 ret
= system_veth_add(&veth
->dev
, &veth
->config
);
75 ret
= veth
->set_state(&veth
->dev
, true);
82 system_veth_del(&veth
->dev
);
87 veth_set_state(struct device
*dev
, bool up
)
91 D(SYSTEM
, "veth_set_state(%s, %u)\n", dev
->ifname
, up
);
93 veth
= container_of(dev
, struct veth
, dev
);
95 return veth_set_up(veth
);
97 return veth_set_down(veth
);
101 veth_free(struct device
*dev
)
105 veth
= container_of(dev
, struct veth
, dev
);
106 free(veth
->config_data
);
111 veth_dump_info(struct device
*dev
, struct blob_buf
*b
)
115 veth
= container_of(dev
, struct veth
, dev
);
116 if (veth
->config
.flags
& VETH_OPT_PEER_NAME
)
117 blobmsg_add_string(b
, "peer", veth
->config
.peer_name
);
118 system_if_dump_info(dev
, b
);
122 veth_config_init(struct device
*dev
)
124 device_set_present(dev
, true);
128 veth_apply_settings(struct veth
*veth
, struct blob_attr
**tb
)
130 struct veth_config
*cfg
= &veth
->config
;
131 struct blob_attr
*cur
;
132 struct ether_addr
*ea
;
136 if ((cur
= tb
[VETH_ATTR_MACADDR
]))
138 ea
= ether_aton(blobmsg_data(cur
));
140 memcpy(cfg
->macaddr
, ea
, 6);
141 cfg
->flags
|= VETH_OPT_MACADDR
;
145 if ((cur
= tb
[VETH_ATTR_PEER_NAME
]))
147 strncpy(cfg
->peer_name
, blobmsg_get_string(cur
), sizeof(cfg
->peer_name
)-1);
148 cfg
->flags
|= VETH_OPT_PEER_NAME
;
151 if ((cur
= tb
[VETH_ATTR_PEER_MACADDR
]))
153 ea
= ether_aton(blobmsg_data(cur
));
155 memcpy(cfg
->peer_macaddr
, ea
, 6);
156 cfg
->flags
|= VETH_OPT_PEER_MACADDR
;
161 static enum dev_change_type
162 veth_reload(struct device
*dev
, struct blob_attr
*attr
)
164 struct blob_attr
*tb_dev
[__DEV_ATTR_MAX
];
165 struct blob_attr
*tb_mv
[__VETH_ATTR_MAX
];
166 enum dev_change_type ret
= DEV_CONFIG_APPLIED
;
169 veth
= container_of(dev
, struct veth
, dev
);
170 attr
= blob_memdup(attr
);
172 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, tb_dev
,
173 blob_data(attr
), blob_len(attr
));
174 blobmsg_parse(veth_attrs
, __VETH_ATTR_MAX
, tb_mv
,
175 blob_data(attr
), blob_len(attr
));
177 device_init_settings(dev
, tb_dev
);
178 veth_apply_settings(veth
, tb_mv
);
180 if (veth
->config_data
) {
181 struct blob_attr
*otb_dev
[__DEV_ATTR_MAX
];
182 struct blob_attr
*otb_mv
[__VETH_ATTR_MAX
];
184 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb_dev
,
185 blob_data(veth
->config_data
), blob_len(veth
->config_data
));
187 if (uci_blob_diff(tb_dev
, otb_dev
, &device_attr_list
, NULL
))
188 ret
= DEV_CONFIG_RESTART
;
190 blobmsg_parse(veth_attrs
, __VETH_ATTR_MAX
, otb_mv
,
191 blob_data(veth
->config_data
), blob_len(veth
->config_data
));
193 if (uci_blob_diff(tb_mv
, otb_mv
, &veth_attr_list
, NULL
))
194 ret
= DEV_CONFIG_RESTART
;
196 veth_config_init(dev
);
199 free(veth
->config_data
);
200 veth
->config_data
= attr
;
204 static struct device
*
205 veth_create(const char *name
, struct device_type
*devtype
,
206 struct blob_attr
*attr
)
209 struct device
*dev
= NULL
;
211 veth
= calloc(1, sizeof(*veth
));
216 if (device_init(dev
, devtype
, name
) < 0) {
222 dev
->config_pending
= true;
224 veth
->set_state
= dev
->set_state
;
225 dev
->set_state
= veth_set_state
;
227 dev
->hotplug_ops
= NULL
;
229 veth_reload(dev
, attr
);
234 static struct device_type veth_device_type
= {
236 .config_params
= &veth_attr_list
,
237 .create
= veth_create
,
238 .config_init
= veth_config_init
,
239 .reload
= veth_reload
,
241 .dump_info
= veth_dump_info
,
244 static void __init
veth_device_type_init(void)
246 device_type_add(&veth_device_type
);