]>
Commit | Line | Data |
---|---|---|
10142d75 SS |
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 | r = sd_rtnl_message_open_container(m, IFLA_LINKINFO); | |
46 | if (r < 0) { | |
47 | log_error_netdev(netdev, | |
48 | "Could not append IFLA_LINKINFO attribute: %s", | |
49 | strerror(-r)); | |
50 | return r; | |
51 | } | |
52 | ||
53 | r = sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA, | |
54 | netdev_kind_to_string(netdev->kind)); | |
55 | if (r < 0) { | |
56 | log_error_netdev(netdev, | |
57 | "Could not append IFLA_INFO_DATA attribute: %s", | |
58 | strerror(-r)); | |
59 | return r; | |
60 | } | |
61 | ||
62 | r = sd_rtnl_message_open_container(m, VETH_INFO_PEER); | |
63 | if (r < 0) { | |
64 | log_error_netdev(netdev, | |
65 | "Could not append IFLA_IPTUN_LINK attribute: %s", | |
66 | strerror(-r)); | |
67 | return r; | |
68 | } | |
69 | ||
70 | if(netdev->ifname_peer) { | |
71 | r = sd_rtnl_message_append_string(m, IFLA_IFNAME, netdev->ifname_peer); | |
72 | if (r < 0) { | |
73 | log_error("Failed to add netlink interface name: %s", strerror(-r)); | |
74 | return r; | |
75 | } | |
76 | } | |
77 | ||
78 | r = sd_rtnl_message_close_container(m); | |
79 | if (r < 0) { | |
80 | log_error_netdev(netdev, | |
81 | "Could not append IFLA_INFO_DATA attribute: %s", | |
82 | strerror(-r)); | |
83 | return r; | |
84 | } | |
85 | ||
86 | r = sd_rtnl_message_close_container(m); | |
87 | if (r < 0) { | |
88 | log_error_netdev(netdev, | |
89 | "Could not append IFLA_LINKINFO attribute: %s", | |
90 | strerror(-r)); | |
91 | return r; | |
92 | } | |
93 | ||
94 | return r; | |
95 | } | |
96 | ||
97 | int netdev_create_veth(NetDev *netdev, sd_rtnl_message_handler_t callback) { | |
98 | _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL; | |
99 | int r; | |
100 | ||
101 | assert(netdev); | |
102 | assert(netdev->ifname); | |
103 | assert(netdev->manager); | |
104 | assert(netdev->manager->rtnl); | |
105 | ||
106 | r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, 0); | |
107 | if (r < 0) { | |
108 | log_error_netdev(netdev, | |
109 | "Could not allocate RTM_NEWLINK message: %s", | |
110 | strerror(-r)); | |
111 | return r; | |
112 | } | |
113 | ||
114 | if(netdev->kind != NETDEV_KIND_VETH) | |
115 | return -ENOTSUP; | |
116 | ||
117 | r = netdev_fill_veth_rtnl_message(netdev, m); | |
118 | if(r < 0) | |
119 | return r; | |
120 | ||
121 | r = sd_rtnl_call_async(netdev->manager->rtnl, m, callback, netdev, 0, NULL); | |
122 | if (r < 0) { | |
123 | log_error_netdev(netdev, | |
124 | "Could not send rtnetlink message: %s", strerror(-r)); | |
125 | return r; | |
126 | } | |
127 | ||
128 | log_debug_netdev(netdev, "Creating veth netdev: %s", | |
129 | netdev_kind_to_string(netdev->kind)); | |
130 | ||
131 | netdev->state = NETDEV_STATE_CREATING; | |
132 | ||
133 | return 0; | |
134 | } |