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.
21 #include <net/ethernet.h>
24 #include <netinet/ether.h>
29 #include "interface.h"
35 VETH_ATTR_PEER_MACADDR
,
39 static const struct blobmsg_policy veth_attrs
[__VETH_ATTR_MAX
] = {
40 [VETH_ATTR_MACADDR
] = { "macaddr", BLOBMSG_TYPE_STRING
},
41 [VETH_ATTR_PEER_NAME
] = { "peer_name", BLOBMSG_TYPE_STRING
},
42 [VETH_ATTR_PEER_MACADDR
] = { "peer_macaddr", BLOBMSG_TYPE_STRING
},
45 static const struct uci_blob_param_list veth_attr_list
= {
46 .n_params
= __VETH_ATTR_MAX
,
50 .next
= { &device_attr_list
},
56 device_state_cb set_state
;
58 struct blob_attr
*config_data
;
59 struct veth_config config
;
63 veth_set_down(struct veth
*veth
)
65 veth
->set_state(&veth
->dev
, false);
66 system_veth_del(&veth
->dev
);
72 veth_set_up(struct veth
*veth
)
76 ret
= system_veth_add(&veth
->dev
, &veth
->config
);
80 ret
= veth
->set_state(&veth
->dev
, true);
87 system_veth_del(&veth
->dev
);
92 veth_set_state(struct device
*dev
, bool up
)
96 D(SYSTEM
, "veth_set_state(%s, %u)\n", dev
->ifname
, up
);
98 veth
= container_of(dev
, struct veth
, dev
);
100 return veth_set_up(veth
);
102 return veth_set_down(veth
);
106 veth_free(struct device
*dev
)
110 veth
= container_of(dev
, struct veth
, dev
);
111 free(veth
->config_data
);
116 veth_dump_info(struct device
*dev
, struct blob_buf
*b
)
120 veth
= container_of(dev
, struct veth
, dev
);
121 if (veth
->config
.flags
& VETH_OPT_PEER_NAME
)
122 blobmsg_add_string(b
, "peer", veth
->config
.peer_name
);
123 system_if_dump_info(dev
, b
);
127 veth_config_init(struct device
*dev
)
129 device_set_present(dev
, true);
133 veth_apply_settings(struct veth
*veth
, struct blob_attr
**tb
)
135 struct veth_config
*cfg
= &veth
->config
;
136 struct blob_attr
*cur
;
137 struct ether_addr
*ea
;
141 if ((cur
= tb
[VETH_ATTR_MACADDR
]))
143 ea
= ether_aton(blobmsg_data(cur
));
145 memcpy(cfg
->macaddr
, ea
, 6);
146 cfg
->flags
|= VETH_OPT_MACADDR
;
150 if ((cur
= tb
[VETH_ATTR_PEER_NAME
]))
152 strncpy(cfg
->peer_name
, blobmsg_get_string(cur
), sizeof(cfg
->peer_name
)-1);
153 cfg
->flags
|= VETH_OPT_PEER_NAME
;
156 if ((cur
= tb
[VETH_ATTR_PEER_MACADDR
]))
158 ea
= ether_aton(blobmsg_data(cur
));
160 memcpy(cfg
->peer_macaddr
, ea
, 6);
161 cfg
->flags
|= VETH_OPT_PEER_MACADDR
;
166 static enum dev_change_type
167 veth_reload(struct device
*dev
, struct blob_attr
*attr
)
169 struct blob_attr
*tb_dev
[__DEV_ATTR_MAX
];
170 struct blob_attr
*tb_mv
[__VETH_ATTR_MAX
];
171 enum dev_change_type ret
= DEV_CONFIG_APPLIED
;
174 veth
= container_of(dev
, struct veth
, dev
);
175 attr
= blob_memdup(attr
);
177 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, tb_dev
,
178 blob_data(attr
), blob_len(attr
));
179 blobmsg_parse(veth_attrs
, __VETH_ATTR_MAX
, tb_mv
,
180 blob_data(attr
), blob_len(attr
));
182 device_init_settings(dev
, tb_dev
);
183 veth_apply_settings(veth
, tb_mv
);
185 if (veth
->config_data
) {
186 struct blob_attr
*otb_dev
[__DEV_ATTR_MAX
];
187 struct blob_attr
*otb_mv
[__VETH_ATTR_MAX
];
189 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb_dev
,
190 blob_data(veth
->config_data
), blob_len(veth
->config_data
));
192 if (uci_blob_diff(tb_dev
, otb_dev
, &device_attr_list
, NULL
))
193 ret
= DEV_CONFIG_RESTART
;
195 blobmsg_parse(veth_attrs
, __VETH_ATTR_MAX
, otb_mv
,
196 blob_data(veth
->config_data
), blob_len(veth
->config_data
));
198 if (uci_blob_diff(tb_mv
, otb_mv
, &veth_attr_list
, NULL
))
199 ret
= DEV_CONFIG_RESTART
;
201 veth_config_init(dev
);
204 free(veth
->config_data
);
205 veth
->config_data
= attr
;
209 static struct device
*
210 veth_create(const char *name
, struct device_type
*devtype
,
211 struct blob_attr
*attr
)
214 struct device
*dev
= NULL
;
216 veth
= calloc(1, sizeof(*veth
));
221 if (device_init(dev
, devtype
, name
) < 0) {
227 dev
->config_pending
= true;
229 veth
->set_state
= dev
->set_state
;
230 dev
->set_state
= veth_set_state
;
232 dev
->hotplug_ops
= NULL
;
234 veth_reload(dev
, attr
);
239 static struct device_type veth_device_type
= {
241 .config_params
= &veth_attr_list
,
242 .create
= veth_create
,
243 .config_init
= veth_config_init
,
244 .reload
= veth_reload
,
246 .dump_info
= veth_dump_info
,
249 static void __init
veth_device_type_init(void)
251 device_type_add(&veth_device_type
);