]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/netdev/veth.c
network: fix memleaks
[thirdparty/systemd.git] / src / network / netdev / veth.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <net/if.h>
5 #include <linux/veth.h>
6
7 #include "veth.h"
8
9 static int netdev_veth_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
10 Veth *v;
11 int r;
12
13 assert(netdev);
14 assert(!link);
15 assert(m);
16
17 v = VETH(netdev);
18
19 assert(v);
20
21 r = sd_netlink_message_open_container(m, VETH_INFO_PEER);
22 if (r < 0)
23 return log_netdev_error_errno(netdev, r, "Could not append VETH_INFO_PEER attribute: %m");
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 log_netdev_error_errno(netdev, r, "Failed to add netlink interface name: %m");
29 }
30
31 if (v->mac_peer) {
32 r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, v->mac_peer);
33 if (r < 0)
34 return log_netdev_error_errno(netdev, r, "Could not append IFLA_ADDRESS attribute: %m");
35 }
36
37 r = sd_netlink_message_close_container(m);
38 if (r < 0)
39 return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m");
40
41 return r;
42 }
43
44 static int netdev_veth_verify(NetDev *netdev, const char *filename) {
45 Veth *v;
46 int r;
47
48 assert(netdev);
49 assert(filename);
50
51 v = VETH(netdev);
52
53 assert(v);
54
55 if (!v->ifname_peer) {
56 log_netdev_warning(netdev, "Veth NetDev without peer name configured in %s. Ignoring",
57 filename);
58 return -EINVAL;
59 }
60
61 if (!v->mac_peer) {
62 r = netdev_get_mac(v->ifname_peer, &v->mac_peer);
63 if (r < 0) {
64 log_netdev_warning(netdev,
65 "Failed to generate predictable MAC address for %s. Ignoring",
66 v->ifname_peer);
67 return -EINVAL;
68 }
69 }
70
71 return 0;
72 }
73
74 static void veth_done(NetDev *n) {
75 Veth *v;
76
77 assert(n);
78
79 v = VETH(n);
80
81 assert(v);
82
83 free(v->ifname_peer);
84 free(v->mac_peer);
85 }
86
87 const NetDevVTable veth_vtable = {
88 .object_size = sizeof(Veth),
89 .sections = NETDEV_COMMON_SECTIONS "Peer\0",
90 .done = veth_done,
91 .fill_message_create = netdev_veth_fill_message_create,
92 .create_type = NETDEV_CREATE_INDEPENDENT,
93 .config_verify = netdev_veth_verify,
94 .generate_mac = true,
95 };