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