]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/netdev/veth.c
e0f5b4ebb1a7c61a2046a981c4cbdb5d42a5ee07
[thirdparty/systemd.git] / src / network / netdev / veth.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <net/if.h>
5 #include <netinet/in.h>
6 #include <linux/if_arp.h>
7 #include <linux/veth.h>
8
9 #include "netlink-util.h"
10 #include "veth.h"
11
12 static int netdev_veth_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
13 assert(!link);
14 assert(m);
15
16 struct hw_addr_data hw_addr;
17 Veth *v = VETH(netdev);
18 int r;
19
20 r = sd_netlink_message_open_container(m, VETH_INFO_PEER);
21 if (r < 0)
22 return r;
23
24 if (v->ifname_peer) {
25 r = sd_netlink_message_append_string(m, IFLA_IFNAME, v->ifname_peer);
26 if (r < 0)
27 return r;
28 }
29
30 r = netdev_generate_hw_addr(netdev, NULL, v->ifname_peer, &v->hw_addr_peer, &hw_addr);
31 if (r < 0)
32 return r;
33
34 if (hw_addr.length > 0) {
35 log_netdev_debug(netdev, "Using MAC address for peer: %s", HW_ADDR_TO_STR(&hw_addr));
36 r = netlink_message_append_hw_addr(m, IFLA_ADDRESS, &hw_addr);
37 if (r < 0)
38 return r;
39 }
40
41 if (netdev->mtu != 0) {
42 r = sd_netlink_message_append_u32(m, IFLA_MTU, netdev->mtu);
43 if (r < 0)
44 return r;
45 }
46
47 r = sd_netlink_message_close_container(m);
48 if (r < 0)
49 return r;
50
51 return 0;
52 }
53
54 static int netdev_veth_verify(NetDev *netdev, const char *filename) {
55 assert(filename);
56
57 Veth *v = VETH(netdev);
58
59 if (!v->ifname_peer)
60 return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
61 "Veth NetDev without peer name configured in %s. Ignoring",
62 filename);
63
64 return 0;
65 }
66
67 static void veth_done(NetDev *netdev) {
68 Veth *v = VETH(netdev);
69
70 free(v->ifname_peer);
71 }
72
73 const NetDevVTable veth_vtable = {
74 .object_size = sizeof(Veth),
75 .sections = NETDEV_COMMON_SECTIONS "Peer\0",
76 .done = veth_done,
77 .fill_message_create = netdev_veth_fill_message_create,
78 .create_type = NETDEV_CREATE_INDEPENDENT,
79 .config_verify = netdev_veth_verify,
80 .iftype = ARPHRD_ETHER,
81 .generate_mac = true,
82 };