]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/netdev/veth.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / network / netdev / veth.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
10142d75
SS
2/***
3 This file is part of systemd.
4
5 Copyright 2014 Susant Sahani <susant@redhat.com>
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
10142d75
SS
21#include <net/if.h>
22#include <linux/veth.h>
23
1c4baffc 24#include "sd-netlink.h"
cf0fbc49 25
441e9ae4 26#include "netdev/veth.h"
10142d75 27
1c4baffc 28static int netdev_veth_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
cd946b9c 29 Veth *v;
10142d75
SS
30 int r;
31
32 assert(netdev);
aa9f1140 33 assert(!link);
10142d75
SS
34 assert(m);
35
cd946b9c
SS
36 v = VETH(netdev);
37
38 assert(v);
39
1c4baffc 40 r = sd_netlink_message_open_container(m, VETH_INFO_PEER);
7676666c
SS
41 if (r < 0)
42 return log_netdev_error_errno(netdev, r, "Could not append VETH_INFO_PEER attribute: %m");
10142d75 43
aa9f1140 44 if (v->ifname_peer) {
1c4baffc 45 r = sd_netlink_message_append_string(m, IFLA_IFNAME, v->ifname_peer);
f647962d
MS
46 if (r < 0)
47 return log_error_errno(r, "Failed to add netlink interface name: %m");
10142d75
SS
48 }
49
aa9f1140 50 if (v->mac_peer) {
1c4baffc 51 r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, v->mac_peer);
7676666c
SS
52 if (r < 0)
53 return log_netdev_error_errno(netdev, r, "Could not append IFLA_ADDRESS attribute: %m");
96c90742
TG
54 }
55
1c4baffc 56 r = sd_netlink_message_close_container(m);
7676666c
SS
57 if (r < 0)
58 return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m");
10142d75 59
10142d75
SS
60 return r;
61}
62
3be1d7e0 63static int netdev_veth_verify(NetDev *netdev, const char *filename) {
cd946b9c 64 Veth *v;
10142d75
SS
65 int r;
66
67 assert(netdev);
3be1d7e0 68 assert(filename);
10142d75 69
cd946b9c
SS
70 v = VETH(netdev);
71
72 assert(v);
73
aa9f1140 74 if (!v->ifname_peer) {
3be1d7e0
TG
75 log_warning("Veth NetDev without peer name configured in %s. Ignoring",
76 filename);
77 return -EINVAL;
10142d75
SS
78 }
79
aa9f1140
TG
80 if (!v->mac_peer) {
81 r = netdev_get_mac(v->ifname_peer, &v->mac_peer);
3be1d7e0
TG
82 if (r < 0) {
83 log_warning("Failed to generate predictable MAC address for %s. Ignoring",
aa9f1140 84 v->ifname_peer);
3be1d7e0
TG
85 return -EINVAL;
86 }
10142d75
SS
87 }
88
10142d75
SS
89 return 0;
90}
3be1d7e0 91
aa9f1140 92static void veth_done(NetDev *n) {
cd946b9c 93 Veth *v;
aa9f1140
TG
94
95 assert(n);
cd946b9c
SS
96
97 v = VETH(n);
98
aa9f1140
TG
99 assert(v);
100
101 free(v->ifname_peer);
102 free(v->mac_peer);
103}
104
3be1d7e0 105const NetDevVTable veth_vtable = {
aa9f1140
TG
106 .object_size = sizeof(Veth),
107 .sections = "Match\0NetDev\0Peer\0",
108 .done = veth_done,
3be1d7e0 109 .fill_message_create = netdev_veth_fill_message_create,
aa9f1140 110 .create_type = NETDEV_CREATE_INDEPENDENT,
3be1d7e0
TG
111 .config_verify = netdev_veth_verify,
112};