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