chiark / gitweb /
networkd: link - don't enter LINK_CONFIGURED more than once
[elogind.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_error_netdev(netdev,
42                                  "Could not append IFLA_IPTUN_LINK 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                         log_error("Failed to add netlink interface name: %s", strerror(-r));
51                         return r;
52                 }
53         }
54
55         if (v->mac_peer) {
56                 r = sd_rtnl_message_append_ether_addr(m, IFLA_ADDRESS, v->mac_peer);
57                 if (r < 0) {
58                         log_error_netdev(netdev,
59                                          "Could not append IFLA_ADDRESS attribute: %s",
60                                          strerror(-r));
61                     return r;
62                 }
63         }
64
65         r = sd_rtnl_message_close_container(m);
66         if (r < 0) {
67                 log_error_netdev(netdev,
68                                  "Could not append IFLA_INFO_DATA attribute: %s",
69                                  strerror(-r));
70                 return r;
71         }
72
73         return r;
74 }
75
76 static int netdev_veth_verify(NetDev *netdev, const char *filename) {
77         Veth *v = VETH(netdev);
78         int r;
79
80         assert(netdev);
81         assert(v);
82         assert(filename);
83
84         if (!v->ifname_peer) {
85                 log_warning("Veth NetDev without peer name configured in %s. Ignoring",
86                             filename);
87                 return -EINVAL;
88         }
89
90         if (!v->mac_peer) {
91                 r = netdev_get_mac(v->ifname_peer, &v->mac_peer);
92                 if (r < 0) {
93                         log_warning("Failed to generate predictable MAC address for %s. Ignoring",
94                                   v->ifname_peer);
95                         return -EINVAL;
96                 }
97         }
98
99         return 0;
100 }
101
102 static void veth_done(NetDev *n) {
103         Veth *v = VETH(n);
104
105         assert(n);
106         assert(v);
107
108         free(v->ifname_peer);
109         free(v->mac_peer);
110 }
111
112 const NetDevVTable veth_vtable = {
113         .object_size = sizeof(Veth),
114         .sections = "Match\0NetDev\0Peer\0",
115         .done = veth_done,
116         .fill_message_create = netdev_veth_fill_message_create,
117         .create_type = NETDEV_CREATE_INDEPENDENT,
118         .config_verify = netdev_veth_verify,
119 };