Android-cuttlefish cvd tool
alloc_utils.h
Go to the documentation of this file.
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
17#pragma once
18
19#include <pwd.h>
20#include <sys/wait.h>
21#include <unistd.h>
22
23#include <atomic>
24#include <optional>
25#include <sstream>
26
28
29namespace cuttlefish {
30
31constexpr char kEbtablesName[] = "ebtables";
32constexpr char kEbtablesLegacyName[] = "ebtables-legacy";
33
34// Wireless network prefix
35constexpr char kWirelessIp[] = "192.168.96";
36// Mobile network prefix
37constexpr char kMobileIp[] = "192.168.97";
38// Ethernet network prefix
39constexpr char kEthernetIp[] = "192.168.98";
40// permission bits for socket
41constexpr int kSocketMode = 0666;
42
43// Max ID an interface can have
44// Note: Interface names only have 2 digits in addition to the username prefix
45// Additionally limited by available netmask values in MobileNetworkName
46// Exceeding 63 would result in an overflow when calculating the netmask
47constexpr uint32_t kMaxIfaceNameId = 63;
48
49// struct for managing configuration state
51 bool has_broute_ipv4 = false;
52 bool has_broute_ipv6 = false;
53 bool has_tap = false;
54 bool use_ebtables_legacy = false;
55};
56
57// struct for managing configuration state
59 bool has_gateway = false;
60 bool has_dnsmasq = false;
61 bool has_iptable = false;
62};
63
64int RunExternalCommand(const std::string& command);
65std::optional<std::string> GetUserName(uid_t uid);
66
67bool AddTapIface(const std::string& name);
68bool CreateTap(const std::string& name);
69
70bool BringUpIface(const std::string& name);
71bool ShutdownIface(const std::string& name);
72
73bool DestroyIface(const std::string& name);
74bool DeleteIface(const std::string& name);
75
76bool CreateBridge(const std::string& name);
77bool DestroyBridge(const std::string& name);
78
79bool CreateEbtables(const std::string& name, bool use_ipv,
80 bool use_ebtables_legacy);
81bool DestroyEbtables(const std::string& name, bool use_ipv4,
82 bool use_ebtables_legacy);
83bool EbtablesBroute(const std::string& name, bool use_ipv4, bool add,
84 bool use_ebtables_legacy);
85bool EbtablesFilter(const std::string& name, bool use_ipv4, bool add,
86 bool use_ebtables_legacy);
87
88bool CreateMobileIface(const std::string& name, uint16_t id,
89 const std::string& ipaddr);
90bool DestroyMobileIface(const std::string& name, uint16_t id,
91 const std::string& ipaddr);
92
93bool CreateEthernetIface(const std::string& name, const std::string& bridge_name,
94 bool has_ipv4_bridge, bool has_ipv6_bridge,
95 bool use_ebtables_legacy);
96bool DestroyEthernetIface(const std::string& name,
97 bool has_ipv4_bridge, bool use_ipv6,
98 bool use_ebtables_legacy);
99void CleanupEthernetIface(const std::string& name,
100 const EthernetNetworkConfig& config);
101
102bool IptableConfig(const std::string& network, bool add);
103
104bool LinkTapToBridge(const std::string& tap_name,
105 const std::string& bridge_name);
106
107bool SetupBridgeGateway(const std::string& name, const std::string& ipaddr);
108void CleanupBridgeGateway(const std::string& name, const std::string& ipaddr,
109 const GatewayConfig& config);
110
111bool CreateEthernetBridgeIface(const std::string& name,
112 const std::string &ipaddr);
113bool DestroyEthernetBridgeIface(const std::string& name,
114 const std::string &ipaddr);
115
116bool AddGateway(const std::string& name, const std::string& gateway,
117 const std::string& netmask);
118bool DestroyGateway(const std::string& name, const std::string& gateway,
119 const std::string& netmask);
120
121bool StartDnsmasq(const std::string& bridge_name, const std::string& gateway,
122 const std::string& dhcp_range);
123bool StopDnsmasq(const std::string& name);
124
125} // namespace cuttlefish
Definition: alloc_utils.cpp:23
std::optional< std::string > GetUserName(uid_t uid)
Definition: alloc_utils.cpp:326
bool CreateBridge(const std::string &name)
Definition: alloc_utils.cpp:335
bool CreateEbtables(const std::string &name, bool use_ipv4, bool use_ebtables_legacy)
Definition: alloc_utils.cpp:225
constexpr char kWirelessIp[]
Definition: alloc_utils.h:35
bool DeleteIface(const std::string &name)
Definition: alloc_utils.cpp:300
bool DestroyIface(const std::string &name)
Definition: alloc_utils.cpp:310
bool LinkTapToBridge(const std::string &tap_name, const std::string &bridge_name)
Definition: alloc_utils.cpp:274
constexpr int kSocketMode
Definition: alloc_utils.h:41
bool CreateTap(const std::string &name)
Definition: alloc_utils.cpp:284
constexpr char kEbtablesName[]
Definition: alloc_utils.h:31
bool DestroyGateway(const std::string &name, const std::string &gateway, const std::string &netmask)
Definition: alloc_utils.cpp:186
bool DestroyBridge(const std::string &name)
Definition: alloc_utils.cpp:351
bool EbtablesFilter(const std::string &name, bool use_ipv4, bool add, bool use_ebtables_legacy)
Definition: alloc_utils.cpp:257
bool CreateMobileIface(const std::string &name, uint16_t id, const std::string &ipaddr)
Definition: alloc_utils.cpp:131
bool DestroyEbtables(const std::string &name, bool use_ipv4, bool use_ebtables_legacy)
Definition: alloc_utils.cpp:231
bool StopDnsmasq(const std::string &name)
Definition: alloc_utils.cpp:434
void CleanupBridgeGateway(const std::string &name, const std::string &ipaddr, const GatewayConfig &config)
Definition: alloc_utils.cpp:383
bool SetupBridgeGateway(const std::string &bridge_name, const std::string &ipaddr)
Definition: alloc_utils.cpp:353
bool EbtablesBroute(const std::string &name, bool use_ipv4, bool add, bool use_ebtables_legacy)
Definition: alloc_utils.cpp:237
void CleanupEthernetIface(const std::string &name, const EthernetNetworkConfig &config)
Definition: alloc_utils.cpp:210
bool DestroyEthernetIface(const std::string &name, bool has_ipv4_bridge, bool has_ipv6_bridge, bool use_ebtables_legacy)
Definition: alloc_utils.cpp:197
int RunExternalCommand(const std::string &command)
Definition: alloc_utils.cpp:25
constexpr char kMobileIp[]
Definition: alloc_utils.h:37
bool AddGateway(const std::string &name, const std::string &gateway, const std::string &netmask)
Definition: alloc_utils.cpp:175
constexpr char kEbtablesLegacyName[]
Definition: alloc_utils.h:32
constexpr char kEthernetIp[]
Definition: alloc_utils.h:39
bool CreateEthernetIface(const std::string &name, const std::string &bridge_name, bool has_ipv4_bridge, bool has_ipv6_bridge, bool use_ebtables_legacy)
Definition: alloc_utils.cpp:81
bool ShutdownIface(const std::string &name)
Definition: alloc_utils.cpp:61
bool AddTapIface(const std::string &name)
Definition: alloc_utils.cpp:52
bool DestroyMobileIface(const std::string &name, uint16_t id, const std::string &ipaddr)
Definition: alloc_utils.cpp:159
bool DestroyEthernetBridgeIface(const std::string &name, const std::string &ipaddr)
Definition: alloc_utils.cpp:486
bool BringUpIface(const std::string &name)
Definition: alloc_utils.cpp:71
constexpr uint32_t kMaxIfaceNameId
Definition: alloc_utils.h:47
bool StartDnsmasq(const std::string &bridge_name, const std::string &gateway, const std::string &dhcp_range)
Definition: alloc_utils.cpp:403
bool IptableConfig(const std::string &network, bool add)
Definition: alloc_utils.cpp:460
bool CreateEthernetBridgeIface(const std::string &name, const std::string &ipaddr)
Definition: alloc_utils.cpp:472
BinOp< T1, T2, Numeric, Add > add
Definition: utils.h:264
Definition: alloc_utils.h:50
bool has_broute_ipv4
Definition: alloc_utils.h:51
bool use_ebtables_legacy
Definition: alloc_utils.h:54
bool has_broute_ipv6
Definition: alloc_utils.h:52
bool has_tap
Definition: alloc_utils.h:53
Definition: alloc_utils.h:58
bool has_gateway
Definition: alloc_utils.h:59
bool has_dnsmasq
Definition: alloc_utils.h:60
bool has_iptable
Definition: alloc_utils.h:61