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