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