chiark / gitweb /
networkd: split out vlan and macvlan handling
[elogind.git] / src / network / networkd-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.h"
29
30
31 static int netdev_fill_veth_rtnl_message(NetDev *netdev, sd_rtnl_message *m) {
32         int r;
33
34         assert(netdev);
35         assert(m);
36
37         r = sd_rtnl_message_append_string(m, IFLA_IFNAME, netdev->ifname);
38         if (r < 0) {
39                 log_error_netdev(netdev,
40                                  "Could not append IFLA_IFNAME, attribute: %s",
41                                  strerror(-r));
42                 return r;
43         }
44
45         if (netdev->mac) {
46                 r = sd_rtnl_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac);
47                 if (r < 0) {
48                         log_error_netdev(netdev,
49                                          "Colud not append IFLA_ADDRESS attribute: %s",
50                                          strerror(-r));
51                     return r;
52                 }
53         }
54
55         r = sd_rtnl_message_open_container(m, IFLA_LINKINFO);
56         if (r < 0) {
57                 log_error_netdev(netdev,
58                                  "Could not append IFLA_LINKINFO attribute: %s",
59                                  strerror(-r));
60                 return r;
61         }
62
63         r = sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA,
64                                                  netdev_kind_to_string(netdev->kind));
65         if (r < 0) {
66                 log_error_netdev(netdev,
67                                  "Could not append IFLA_INFO_DATA attribute: %s",
68                                  strerror(-r));
69                 return r;
70         }
71
72         r = sd_rtnl_message_open_container(m, VETH_INFO_PEER);
73         if (r < 0) {
74                 log_error_netdev(netdev,
75                                  "Could not append IFLA_IPTUN_LINK attribute: %s",
76                                  strerror(-r));
77                 return r;
78         }
79
80         if (netdev->ifname_peer) {
81                 r = sd_rtnl_message_append_string(m, IFLA_IFNAME, netdev->ifname_peer);
82                 if (r < 0) {
83                         log_error("Failed to add netlink interface name: %s", strerror(-r));
84                         return r;
85                 }
86         }
87
88         if (netdev->mac_peer) {
89                 r = sd_rtnl_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac_peer);
90                 if (r < 0) {
91                         log_error_netdev(netdev,
92                                          "Colud not append IFLA_ADDRESS attribute: %s",
93                                          strerror(-r));
94                     return r;
95                 }
96         }
97
98         r = sd_rtnl_message_close_container(m);
99         if (r < 0) {
100                 log_error_netdev(netdev,
101                                  "Could not append IFLA_INFO_DATA attribute: %s",
102                                  strerror(-r));
103                 return r;
104         }
105
106         r = sd_rtnl_message_close_container(m);
107         if (r < 0) {
108                 log_error_netdev(netdev,
109                                  "Could not append IFLA_LINKINFO attribute: %s",
110                                  strerror(-r));
111                 return r;
112         }
113
114         return r;
115 }
116
117 int netdev_create_veth(NetDev *netdev, sd_rtnl_message_handler_t callback) {
118         _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
119         int r;
120
121         assert(netdev);
122         assert(netdev->ifname);
123         assert(netdev->manager);
124         assert(netdev->manager->rtnl);
125
126         r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, 0);
127         if (r < 0) {
128                 log_error_netdev(netdev,
129                                  "Could not allocate RTM_NEWLINK message: %s",
130                                  strerror(-r));
131                 return r;
132         }
133
134         if(netdev->kind != NETDEV_KIND_VETH)
135                 return -ENOTSUP;
136
137         r = netdev_fill_veth_rtnl_message(netdev, m);
138         if(r < 0)
139                 return r;
140
141         r = sd_rtnl_call_async(netdev->manager->rtnl, m, callback, netdev, 0, NULL);
142         if (r < 0) {
143                 log_error_netdev(netdev,
144                                  "Could not send rtnetlink message: %s", strerror(-r));
145                 return r;
146         }
147
148         netdev_ref(netdev);
149
150         log_debug_netdev(netdev, "Creating veth netdev: %s",
151                          netdev_kind_to_string(netdev->kind));
152
153         netdev->state = NETDEV_STATE_CREATING;
154
155         return 0;
156 }