|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include "alloc_driver.h" |
| 17 | + |
| 18 | +#include <arpa/inet.h> |
| 19 | +#include <fcntl.h> |
| 20 | +#include <grp.h> |
| 21 | +#include <linux/if_tun.h> |
| 22 | +#include <linux/netlink.h> |
| 23 | +#include <linux/rtnetlink.h> |
| 24 | +#include <net/if.h> |
| 25 | +#include <net/if_arp.h> |
| 26 | +#include <netinet/in.h> |
| 27 | +#include <string.h> |
| 28 | +#include <sys/ioctl.h> |
| 29 | +#include <sys/socket.h> |
| 30 | +#include <sys/types.h> |
| 31 | + |
| 32 | +#include <cstdint> |
| 33 | +#include <fstream> |
| 34 | +#include <string_view> |
| 35 | + |
| 36 | +#include "absl/log/check.h" |
| 37 | +#include "absl/log/log.h" |
| 38 | +#include "absl/strings/numbers.h" |
| 39 | +#include "absl/strings/str_format.h" |
| 40 | + |
| 41 | +#include "allocd/net/netlink_client.h" |
| 42 | + |
| 43 | +namespace cuttlefish { |
| 44 | + |
| 45 | +namespace { |
| 46 | + |
| 47 | +unsigned int Index(std::string_view ifname) { |
| 48 | + std::string name = std::string(ifname); |
| 49 | + return if_nametoindex(name.c_str()); |
| 50 | +} |
| 51 | + |
| 52 | +int Prefix(std::string_view textual_netmask) { |
| 53 | + // Currently, the netmask argument is provided as, e.g., "/24". |
| 54 | + // TODO: Consider passing the number of prefix bits numerically, which |
| 55 | + // would require API changes across all drivers. |
| 56 | + int prefix; |
| 57 | + textual_netmask.remove_prefix(1); |
| 58 | + CHECK(absl::SimpleAtoi(textual_netmask, &prefix)) |
| 59 | + << "Prefix: couldn't get prefix from netmask: " << textual_netmask; |
| 60 | + return prefix; |
| 61 | +} |
| 62 | + |
| 63 | +} // namespace |
| 64 | + |
| 65 | +bool AddTapIface(std::string_view name) { |
| 66 | + int tunfd = open("/dev/net/tun", O_RDWR | O_CLOEXEC); |
| 67 | + if (tunfd == -1) { |
| 68 | + LOG(ERROR) << "AddTapIface: open: " << strerror(errno); |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + std::string ifname = std::string(name); |
| 73 | + struct ifreq ifr; |
| 74 | + strlcpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ); |
| 75 | + ifr.ifr_flags = IFF_TAP | IFF_VNET_HDR | IFF_TUN_EXCL; |
| 76 | + int r = ioctl(tunfd, TUNSETIFF, (void*)&ifr); |
| 77 | + if (r == -1) { |
| 78 | + LOG(ERROR) << "AddTapIface: TUNSETIFF: " << strerror(errno); |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + struct group* g = getgrnam(kCvdNetworkGroupName); |
| 83 | + if (g == NULL) { |
| 84 | + LOG(ERROR) << "AddTapIface: getgrnam: " << strerror(errno); |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + r = ioctl(tunfd, TUNSETGROUP, g->gr_gid); |
| 89 | + if (r == -1) { |
| 90 | + LOG(ERROR) << "AddTapIface: TUNSETGROUP: " << strerror(errno); |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + r = ioctl(tunfd, TUNSETPERSIST, 1); |
| 95 | + if (r == -1) { |
| 96 | + LOG(ERROR) << "AddTapIface: TUNSETPERSIST: " << strerror(errno); |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + r = close(tunfd); |
| 101 | + if (r == -1) { |
| 102 | + LOG(ERROR) << "AddTapIface: close: " << strerror(errno); |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + return true; |
| 107 | +} |
| 108 | + |
| 109 | +bool ShutdownIface(std::string_view name) { |
| 110 | + LOG(INFO) << "ShutdownIface: " << name; |
| 111 | + auto factory = cuttlefish::NetlinkClientFactory::Default(); |
| 112 | + std::unique_ptr<cuttlefish::NetlinkClient> nl(factory->New(NETLINK_ROUTE)); |
| 113 | + |
| 114 | + std::string n = std::string(name); |
| 115 | + cuttlefish::NetlinkRequest req(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK); |
| 116 | + req.AddIfInfo(0, false); |
| 117 | + req.AddString(IFLA_IFNAME, n); |
| 118 | + bool res = nl->Send(req); |
| 119 | + if (!res) { |
| 120 | + LOG(ERROR) << "ShutdownIface: failed"; |
| 121 | + } |
| 122 | + return res; |
| 123 | +} |
| 124 | + |
| 125 | +bool BringUpIface(std::string_view name) { |
| 126 | + LOG(INFO) << "BringUpIface: " << name; |
| 127 | + auto factory = cuttlefish::NetlinkClientFactory::Default(); |
| 128 | + std::unique_ptr<cuttlefish::NetlinkClient> nl(factory->New(NETLINK_ROUTE)); |
| 129 | + |
| 130 | + std::string n = std::string(name); |
| 131 | + cuttlefish::NetlinkRequest req(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK); |
| 132 | + req.AddIfInfo(0, true); |
| 133 | + req.AddString(IFLA_IFNAME, n); |
| 134 | + bool res = nl->Send(req); |
| 135 | + if (!res) { |
| 136 | + LOG(ERROR) << "BringUpIface: failed"; |
| 137 | + } |
| 138 | + return res; |
| 139 | +} |
| 140 | + |
| 141 | +bool AddGateway(std::string_view name, std::string_view gateway, |
| 142 | + std::string_view netmask) { |
| 143 | + LOG(INFO) << "AddGateway: " << name << ", " << gateway << netmask; |
| 144 | + auto factory = cuttlefish::NetlinkClientFactory::Default(); |
| 145 | + std::unique_ptr<cuttlefish::NetlinkClient> nl(factory->New(NETLINK_ROUTE)); |
| 146 | + |
| 147 | + cuttlefish::NetlinkRequest req(RTM_NEWADDR, NLM_F_REQUEST | NLM_F_ACK); |
| 148 | + req.AddAddrInfo(Index(name), Prefix(netmask)); |
| 149 | + std::string addr = std::string(gateway); |
| 150 | + in_addr_t inaddr = inet_addr(addr.c_str()); |
| 151 | + req.AddInAddr(IFA_LOCAL, &inaddr); |
| 152 | + req.AddInAddr(IFA_ADDRESS, &inaddr); |
| 153 | + |
| 154 | + bool res = nl->Send(req); |
| 155 | + if (!res) { |
| 156 | + LOG(ERROR) << "AddGateway: failed"; |
| 157 | + } |
| 158 | + return res; |
| 159 | +} |
| 160 | + |
| 161 | +bool DestroyGateway(std::string_view name, std::string_view gateway, |
| 162 | + std::string_view netmask) { |
| 163 | + LOG(INFO) << "DestroyGateway: " << name << ", " << gateway << netmask; |
| 164 | + auto factory = cuttlefish::NetlinkClientFactory::Default(); |
| 165 | + std::unique_ptr<cuttlefish::NetlinkClient> nl(factory->New(NETLINK_ROUTE)); |
| 166 | + |
| 167 | + std::string addr = std::string(gateway); |
| 168 | + cuttlefish::NetlinkRequest req(RTM_DELADDR, NLM_F_REQUEST | NLM_F_ACK); |
| 169 | + req.AddAddrInfo(Index(name), Prefix(netmask)); |
| 170 | + in_addr_t inaddr = inet_addr(addr.c_str()); |
| 171 | + req.AddInAddr(IFA_LOCAL, &inaddr); |
| 172 | + req.AddInAddr(IFA_ADDRESS, &inaddr); |
| 173 | + |
| 174 | + bool res = nl->Send(req); |
| 175 | + if (!res) { |
| 176 | + LOG(ERROR) << "DestroyGateway: failed"; |
| 177 | + } |
| 178 | + return res; |
| 179 | +} |
| 180 | + |
| 181 | +bool LinkTapToBridge(std::string_view tap_name, std::string_view bridge_name) { |
| 182 | + LOG(INFO) << "LinkTapToBridge: " << tap_name << ", " << bridge_name; |
| 183 | + auto factory = cuttlefish::NetlinkClientFactory::Default(); |
| 184 | + std::unique_ptr<cuttlefish::NetlinkClient> nl(factory->New(NETLINK_ROUTE)); |
| 185 | + |
| 186 | + cuttlefish::NetlinkRequest req(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK); |
| 187 | + req.AddIfInfo(Index(tap_name), true); |
| 188 | + req.AddInt(IFLA_MASTER, Index(bridge_name)); |
| 189 | + |
| 190 | + bool res = nl->Send(req); |
| 191 | + if (!res) { |
| 192 | + LOG(ERROR) << "LinkTapToBridge: failed"; |
| 193 | + } |
| 194 | + return res; |
| 195 | +} |
| 196 | + |
| 197 | +bool DeleteIface(std::string_view name) { |
| 198 | + LOG(INFO) << "DeleteIface: " << name; |
| 199 | + auto factory = cuttlefish::NetlinkClientFactory::Default(); |
| 200 | + std::unique_ptr<cuttlefish::NetlinkClient> nl(factory->New(NETLINK_ROUTE)); |
| 201 | + |
| 202 | + cuttlefish::NetlinkRequest req(RTM_DELLINK, NLM_F_REQUEST | NLM_F_ACK); |
| 203 | + req.AddIfInfo(Index(name), false); |
| 204 | + |
| 205 | + bool res = nl->Send(req); |
| 206 | + if (!res) { |
| 207 | + LOG(ERROR) << "DeleteIface: failed"; |
| 208 | + } |
| 209 | + return res; |
| 210 | +} |
| 211 | + |
| 212 | +bool BridgeExists(std::string_view name) { |
| 213 | + LOG(INFO) << "BridgeExists: " << name; |
| 214 | + auto factory = cuttlefish::NetlinkClientFactory::Default(); |
| 215 | + std::unique_ptr<cuttlefish::NetlinkClient> nl(factory->New(NETLINK_ROUTE)); |
| 216 | + |
| 217 | + std::string ifname = std::string(name); |
| 218 | + cuttlefish::NetlinkRequest req(RTM_GETLINK, NLM_F_REQUEST | NLM_F_ACK); |
| 219 | + req.AddIfInfo(Index(name), 0); |
| 220 | + |
| 221 | + /* |
| 222 | + * TODO: Check for ENODEV specifically. Doing this requires changing |
| 223 | + * this API, since we will then have three states: bridge exists, |
| 224 | + * bridge does not exist, and some other error. This is good enough |
| 225 | + * for now; if there is some other error, we'll likely crash out |
| 226 | + * soon after. |
| 227 | + */ |
| 228 | + return nl->Send(req); |
| 229 | +} |
| 230 | + |
| 231 | +bool CreateBridge(std::string_view name) { |
| 232 | + LOG(INFO) << "CreateBridge: " << name; |
| 233 | + auto factory = cuttlefish::NetlinkClientFactory::Default(); |
| 234 | + std::unique_ptr<cuttlefish::NetlinkClient> nl(factory->New(NETLINK_ROUTE)); |
| 235 | + |
| 236 | + std::string ifname = std::string(name); |
| 237 | + cuttlefish::NetlinkRequest req(RTM_NEWLINK, |
| 238 | + NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE); |
| 239 | + req.Append(ifinfomsg{ |
| 240 | + .ifi_type = ARPHRD_NETROM, |
| 241 | + }); |
| 242 | + req.AddString(IFLA_IFNAME, ifname); |
| 243 | + req.PushList(IFLA_LINKINFO); |
| 244 | + req.AddString(IFLA_INFO_KIND, "bridge"); |
| 245 | + req.PushList(IFLA_INFO_DATA); |
| 246 | + req.AddInt(IFLA_BR_FORWARD_DELAY, 0); |
| 247 | + req.AddInt(IFLA_BR_STP_STATE, 0); |
| 248 | + req.PopList(); |
| 249 | + req.PopList(); |
| 250 | + |
| 251 | + bool res = nl->Send(req); |
| 252 | + if (!res) { |
| 253 | + LOG(ERROR) << "CreateBridge: failed"; |
| 254 | + } |
| 255 | + return res; |
| 256 | +} |
| 257 | + |
| 258 | +int RunExternalCommand(const std::string& command) { |
| 259 | + FILE* fp; |
| 260 | + LOG(INFO) << "Running external command: " << command; |
| 261 | + fp = popen(command.c_str(), "r"); |
| 262 | + |
| 263 | + if (fp == nullptr) { |
| 264 | + LOG(WARNING) << "Error running external command"; |
| 265 | + return -1; |
| 266 | + } |
| 267 | + |
| 268 | + int status = pclose(fp); |
| 269 | + int ret = -1; |
| 270 | + if (status == -1) { |
| 271 | + LOG(WARNING) << "pclose error"; |
| 272 | + } else { |
| 273 | + if (WIFEXITED(status)) { |
| 274 | + LOG(INFO) << "child process exited normally"; |
| 275 | + ret = WEXITSTATUS(status); |
| 276 | + } else if (WIFSIGNALED(status)) { |
| 277 | + int sig = WTERMSIG(status); |
| 278 | + LOG(WARNING) << "child process was terminated by signal " |
| 279 | + << strsignal(sig) << " (" << sig << ")"; |
| 280 | + } else { |
| 281 | + LOG(WARNING) << "child process did not terminate normally"; |
| 282 | + } |
| 283 | + } |
| 284 | + return ret; |
| 285 | +} |
| 286 | + |
| 287 | +bool IptableConfig(std::string_view network, bool add) { |
| 288 | + // TODO: Use NETLINK_NETFILTER. |
| 289 | + std::stringstream ss; |
| 290 | + ss << "iptables -t nat " << (add ? "-A" : "-D") << " POSTROUTING -s " |
| 291 | + << network << " -j MASQUERADE"; |
| 292 | + |
| 293 | + auto command = ss.str(); |
| 294 | + LOG(INFO) << "iptable_config: " << command; |
| 295 | + int status = RunExternalCommand(command); |
| 296 | + |
| 297 | + return status == 0; |
| 298 | +} |
| 299 | + |
| 300 | +} // namespace cuttlefish |
0 commit comments