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