]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/netdev/veth.c
log: minimize includes in log.h
[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 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
21 #include <errno.h>
22 #include <linux/veth.h>
23 #include <net/if.h>
24
25 #include "sd-netlink.h"
26
27 #include "netdev/veth.h"
28
29 static int netdev_veth_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
30 Veth *v;
31 int r;
32
33 assert(netdev);
34 assert(!link);
35 assert(m);
36
37 v = VETH(netdev);
38
39 assert(v);
40
41 r = sd_netlink_message_open_container(m, VETH_INFO_PEER);
42 if (r < 0)
43 return log_netdev_error_errno(netdev, r, "Could not append VETH_INFO_PEER attribute: %m");
44
45 if (v->ifname_peer) {
46 r = sd_netlink_message_append_string(m, IFLA_IFNAME, v->ifname_peer);
47 if (r < 0)
48 return log_error_errno(r, "Failed to add netlink interface name: %m");
49 }
50
51 if (v->mac_peer) {
52 r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, v->mac_peer);
53 if (r < 0)
54 return log_netdev_error_errno(netdev, r, "Could not append IFLA_ADDRESS attribute: %m");
55 }
56
57 r = sd_netlink_message_close_container(m);
58 if (r < 0)
59 return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m");
60
61 return r;
62 }
63
64 static int netdev_veth_verify(NetDev *netdev, const char *filename) {
65 Veth *v;
66 int r;
67
68 assert(netdev);
69 assert(filename);
70
71 v = VETH(netdev);
72
73 assert(v);
74
75 if (!v->ifname_peer) {
76 log_warning("Veth NetDev without peer name configured in %s. Ignoring",
77 filename);
78 return -EINVAL;
79 }
80
81 if (!v->mac_peer) {
82 r = netdev_get_mac(v->ifname_peer, &v->mac_peer);
83 if (r < 0) {
84 log_warning("Failed to generate predictable MAC address for %s. Ignoring",
85 v->ifname_peer);
86 return -EINVAL;
87 }
88 }
89
90 return 0;
91 }
92
93 static void veth_done(NetDev *n) {
94 Veth *v;
95
96 assert(n);
97
98 v = VETH(n);
99
100 assert(v);
101
102 free(v->ifname_peer);
103 free(v->mac_peer);
104 }
105
106 const NetDevVTable veth_vtable = {
107 .object_size = sizeof(Veth),
108 .sections = "Match\0NetDev\0Peer\0",
109 .done = veth_done,
110 .fill_message_create = netdev_veth_fill_message_create,
111 .create_type = NETDEV_CREATE_INDEPENDENT,
112 .config_verify = netdev_veth_verify,
113 };