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