|
| 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 "allocd/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 | +#include "absl/strings/strip.h" |
| 41 | + |
| 42 | +#include "allocd/net/netlink_client.h" |
| 43 | +#include "cuttlefish/common/libs/fs/shared_fd.h" |
| 44 | +#include "cuttlefish/result/result.h" |
| 45 | + |
| 46 | +namespace cuttlefish { |
| 47 | + |
| 48 | +namespace { |
| 49 | + |
| 50 | +Result<unsigned int> Index(std::string_view ifname) { |
| 51 | + unsigned int index = if_nametoindex(std::string(ifname).c_str()); |
| 52 | + CF_EXPECT(index != 0, "Index: " << ifname); |
| 53 | + return index; |
| 54 | +} |
| 55 | + |
| 56 | +int Prefix(std::string_view textual_netmask) { |
| 57 | + // Currently, the netmask argument is provided as, e.g., "/24". |
| 58 | + // TODO: Consider passing the number of prefix bits numerically, which |
| 59 | + // would require API changes across all drivers. |
| 60 | + std::string_view netmask = textual_netmask; |
| 61 | + CHECK(absl::ConsumePrefix(&netmask, "/")); |
| 62 | + |
| 63 | + int prefix; |
| 64 | + CHECK(absl::SimpleAtoi(netmask, &prefix)) |
| 65 | + << "Prefix: couldn't get prefix from netmask: " << textual_netmask; |
| 66 | + return prefix; |
| 67 | +} |
| 68 | + |
| 69 | +} // namespace |
| 70 | + |
| 71 | +extern int RunExternalCommand(const std::string& name); |
| 72 | + |
| 73 | +bool AddTapIface(std::string_view name) { |
| 74 | + SharedFD tunfd = SharedFD::Open("/dev/net/tun", O_RDWR | O_CLOEXEC); |
| 75 | + if (!tunfd->IsOpen()) { |
| 76 | + LOG(ERROR) << "AddTapIface: open: " << tunfd->StrError(); |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + struct ifreq ifr; |
| 81 | + strlcpy(ifr.ifr_name, std::string(name).c_str(), IFNAMSIZ); |
| 82 | + ifr.ifr_flags = IFF_TAP | IFF_VNET_HDR; |
| 83 | + ifr.ifr_flags |= IFF_TUN_EXCL; |
| 84 | + int r = tunfd->Ioctl(TUNSETIFF, (void*)&ifr); |
| 85 | + if (r == -1) { |
| 86 | + LOG(ERROR) << "AddTapIface: TUNSETIFF: " << tunfd->StrError(); |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + struct group* g = getgrnam(kCvdNetworkGroupName); |
| 91 | + if (g == NULL) { |
| 92 | + LOG(ERROR) << "AddTapIface: getgrnam: " << tunfd->StrError(); |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + r = tunfd->Ioctl(TUNSETGROUP, (void*)(intptr_t)g->gr_gid); |
| 97 | + if (r == -1) { |
| 98 | + LOG(ERROR) << "AddTapIface: TUNSETGROUP: " << tunfd->StrError(); |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + r = tunfd->Ioctl(TUNSETPERSIST, (void*)1); |
| 103 | + if (r == -1) { |
| 104 | + LOG(ERROR) << "AddTapIface: TUNSETPERSIST: " << tunfd->StrError(); |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + tunfd->Close(); |
| 109 | + |
| 110 | + return true; |
| 111 | +} |
| 112 | + |
| 113 | +bool ShutdownIface(std::string_view name) { |
| 114 | + VLOG(0) << "ShutdownIface: " << name; |
| 115 | + auto factory = NetlinkClientFactory::Default(); |
| 116 | + std::unique_ptr<NetlinkClient> nl = factory->New(NETLINK_ROUTE); |
| 117 | + |
| 118 | + NetlinkRequest req(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK); |
| 119 | + req.AddIfInfo(0, false); |
| 120 | + req.AddString(IFLA_IFNAME, std::string(name)); |
| 121 | + bool res = nl->Send(req); |
| 122 | + if (!res) { |
| 123 | + LOG(ERROR) << "ShutdownIface: failed"; |
| 124 | + } |
| 125 | + return res; |
| 126 | +} |
| 127 | + |
| 128 | +bool BringUpIface(std::string_view name) { |
| 129 | + VLOG(0) << "BringUpIface: " << name; |
| 130 | + auto factory = NetlinkClientFactory::Default(); |
| 131 | + std::unique_ptr<NetlinkClient> nl = factory->New(NETLINK_ROUTE); |
| 132 | + |
| 133 | + NetlinkRequest req(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK); |
| 134 | + req.AddIfInfo(0, true); |
| 135 | + req.AddString(IFLA_IFNAME, std::string(name)); |
| 136 | + bool res = nl->Send(req); |
| 137 | + if (!res) { |
| 138 | + LOG(ERROR) << "BringUpIface: failed"; |
| 139 | + } |
| 140 | + return res; |
| 141 | +} |
| 142 | + |
| 143 | +bool AddGateway(std::string_view name, std::string_view gateway, |
| 144 | + std::string_view netmask) { |
| 145 | + VLOG(0) << "AddGateway: " << name << ", " << gateway << netmask; |
| 146 | + auto index = Index(name); |
| 147 | + PCHECK(index.ok()) << "Index: " << name; |
| 148 | + |
| 149 | + auto factory = NetlinkClientFactory::Default(); |
| 150 | + std::unique_ptr<NetlinkClient> nl = factory->New(NETLINK_ROUTE); |
| 151 | + |
| 152 | + NetlinkRequest req(RTM_NEWADDR, NLM_F_REQUEST | NLM_F_ACK); |
| 153 | + req.AddAddrInfo(*index, Prefix(netmask)); |
| 154 | + in_addr_t inaddr = inet_addr(std::string(gateway).c_str()); |
| 155 | + req.AddInAddr(IFA_LOCAL, &inaddr); |
| 156 | + req.AddInAddr(IFA_ADDRESS, &inaddr); |
| 157 | + |
| 158 | + bool res = nl->Send(req); |
| 159 | + if (!res) { |
| 160 | + LOG(ERROR) << "AddGateway: failed"; |
| 161 | + } |
| 162 | + return res; |
| 163 | +} |
| 164 | + |
| 165 | +bool DestroyGateway(std::string_view name, std::string_view gateway, |
| 166 | + std::string_view netmask) { |
| 167 | + VLOG(0) << "DestroyGateway: " << name << ", " << gateway << netmask; |
| 168 | + auto index = Index(name); |
| 169 | + if (!index.ok()) { |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + auto factory = NetlinkClientFactory::Default(); |
| 174 | + std::unique_ptr<NetlinkClient> nl = factory->New(NETLINK_ROUTE); |
| 175 | + |
| 176 | + NetlinkRequest req(RTM_DELADDR, NLM_F_REQUEST | NLM_F_ACK); |
| 177 | + req.AddAddrInfo(*index, Prefix(netmask)); |
| 178 | + in_addr_t inaddr = inet_addr(std::string(gateway).c_str()); |
| 179 | + req.AddInAddr(IFA_LOCAL, &inaddr); |
| 180 | + req.AddInAddr(IFA_ADDRESS, &inaddr); |
| 181 | + |
| 182 | + bool res = nl->Send(req); |
| 183 | + if (!res) { |
| 184 | + LOG(ERROR) << "DestroyGateway: failed"; |
| 185 | + } |
| 186 | + return res; |
| 187 | +} |
| 188 | + |
| 189 | +bool LinkTapToBridge(std::string_view tap_name, std::string_view bridge_name) { |
| 190 | + VLOG(0) << "LinkTapToBridge: " << tap_name << ", " << bridge_name; |
| 191 | + auto tap_index = Index(tap_name); |
| 192 | + PCHECK(tap_index.ok()) << "Index: " << tap_name; |
| 193 | + auto bridge_index = Index(bridge_name); |
| 194 | + PCHECK(bridge_index.ok()) << "Index: " << bridge_name; |
| 195 | + |
| 196 | + auto factory = NetlinkClientFactory::Default(); |
| 197 | + std::unique_ptr<NetlinkClient> nl = factory->New(NETLINK_ROUTE); |
| 198 | + |
| 199 | + NetlinkRequest req(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK); |
| 200 | + req.AddIfInfo(*tap_index, true); |
| 201 | + req.AddInt(IFLA_MASTER, *bridge_index); |
| 202 | + |
| 203 | + bool res = nl->Send(req); |
| 204 | + if (!res) { |
| 205 | + LOG(ERROR) << "LinkTapToBridge: failed"; |
| 206 | + } |
| 207 | + return res; |
| 208 | +} |
| 209 | + |
| 210 | +bool DeleteIface(std::string_view name) { |
| 211 | + VLOG(0) << "DeleteIface: " << name; |
| 212 | + auto index = Index(name); |
| 213 | + if (!index.ok()) { |
| 214 | + return false; |
| 215 | + } |
| 216 | + |
| 217 | + auto factory = NetlinkClientFactory::Default(); |
| 218 | + std::unique_ptr<NetlinkClient> nl = factory->New(NETLINK_ROUTE); |
| 219 | + |
| 220 | + NetlinkRequest req(RTM_DELLINK, NLM_F_REQUEST | NLM_F_ACK); |
| 221 | + req.AddIfInfo(*index, false); |
| 222 | + |
| 223 | + bool res = nl->Send(req); |
| 224 | + if (!res) { |
| 225 | + LOG(ERROR) << "DeleteIface: failed"; |
| 226 | + } |
| 227 | + return res; |
| 228 | +} |
| 229 | + |
| 230 | +bool BridgeExists(std::string_view name) { |
| 231 | + VLOG(0) << "BridgeExists: " << name; |
| 232 | + auto index = Index(name); |
| 233 | + if (!index.ok() && errno == ENODEV) { |
| 234 | + return false; |
| 235 | + } |
| 236 | + |
| 237 | + return true; |
| 238 | +} |
| 239 | + |
| 240 | +bool CreateBridge(std::string_view name) { |
| 241 | + VLOG(0) << "CreateBridge: " << name; |
| 242 | + auto factory = NetlinkClientFactory::Default(); |
| 243 | + std::unique_ptr<NetlinkClient> nl = factory->New(NETLINK_ROUTE); |
| 244 | + |
| 245 | + NetlinkRequest req(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE); |
| 246 | + req.Append(ifinfomsg{ |
| 247 | + .ifi_type = ARPHRD_NETROM, |
| 248 | + }); |
| 249 | + req.AddString(IFLA_IFNAME, std::string(name)); |
| 250 | + req.PushList(IFLA_LINKINFO); |
| 251 | + req.AddString(IFLA_INFO_KIND, "bridge"); |
| 252 | + req.PushList(IFLA_INFO_DATA); |
| 253 | + req.AddInt(IFLA_BR_FORWARD_DELAY, 0); |
| 254 | + req.AddInt(IFLA_BR_STP_STATE, 0); |
| 255 | + req.PopList(); |
| 256 | + req.PopList(); |
| 257 | + |
| 258 | + bool res = nl->Send(req); |
| 259 | + if (!res) { |
| 260 | + LOG(ERROR) << "CreateBridge: failed"; |
| 261 | + } |
| 262 | + return res; |
| 263 | +} |
| 264 | + |
| 265 | +bool IptableConfig(std::string_view network, bool add) { |
| 266 | + // TODO: Use NETLINK_NETFILTER. |
| 267 | + std::stringstream ss; |
| 268 | + ss << "iptables -t nat " << (add ? "-A" : "-D") << " POSTROUTING -s " |
| 269 | + << network << " -j MASQUERADE"; |
| 270 | + |
| 271 | + auto command = ss.str(); |
| 272 | + LOG(INFO) << "iptable_config: " << command; |
| 273 | + int status = RunExternalCommand(command); |
| 274 | + |
| 275 | + return status == 0; |
| 276 | +} |
| 277 | + |
| 278 | +} // namespace cuttlefish |
0 commit comments