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 27 #include "common/libs/fs/shared_fd.h" 28 #include "host/libs/allocd/request.h" 29 30 namespace cuttlefish { 31 32 constexpr char kEbtablesName[] = "ebtables"; 33 constexpr char kEbtablesLegacyName[] = "ebtables-legacy"; 34 35 // Wireless network prefix 36 constexpr char kWirelessIp[] = "192.168.96"; 37 // Mobile network prefix 38 constexpr char kMobileIp[] = "192.168.97"; 39 // Ethernet network prefix 40 constexpr char kEthernetIp[] = "192.168.98"; 41 // permission bits for socket 42 constexpr int kSocketMode = 0666; 43 44 // Max ID an interface can have 45 // Note: Interface names only have 2 digits in addition to the username prefix 46 // Additionally limited by available netmask values in MobileNetworkName 47 // Exceeding 63 would result in an overflow when calculating the netmask 48 constexpr uint32_t kMaxIfaceNameId = 63; 49 50 // struct for managing configuration state 51 struct EthernetNetworkConfig { 52 bool has_broute_ipv4 = false; 53 bool has_broute_ipv6 = false; 54 bool has_tap = false; 55 bool use_ebtables_legacy = false; 56 }; 57 58 // struct for managing configuration state 59 struct GatewayConfig { 60 bool has_gateway = false; 61 bool has_dnsmasq = false; 62 bool has_iptable = false; 63 }; 64 65 int RunExternalCommand(const std::string& command); 66 std::optional<std::string> GetUserName(uid_t uid); 67 68 bool AddTapIface(const std::string& name); 69 bool CreateTap(const std::string& name); 70 71 bool BringUpIface(const std::string& name); 72 bool ShutdownIface(const std::string& name); 73 74 bool DestroyIface(const std::string& name); 75 bool DeleteIface(const std::string& name); 76 77 bool CreateBridge(const std::string& name); 78 bool DestroyBridge(const std::string& name); 79 80 bool CreateEbtables(const std::string& name, bool use_ipv, 81 bool use_ebtables_legacy); 82 bool DestroyEbtables(const std::string& name, bool use_ipv4, 83 bool use_ebtables_legacy); 84 bool EbtablesBroute(const std::string& name, bool use_ipv4, bool add, 85 bool use_ebtables_legacy); 86 bool EbtablesFilter(const std::string& name, bool use_ipv4, bool add, 87 bool use_ebtables_legacy); 88 89 bool CreateMobileIface(const std::string& name, uint16_t id, 90 const std::string& ipaddr); 91 bool DestroyMobileIface(const std::string& name, uint16_t id, 92 const std::string& ipaddr); 93 94 bool CreateEthernetIface(const std::string& name, const std::string& bridge_name, 95 bool has_ipv4_bridge, bool has_ipv6_bridge, 96 bool use_ebtables_legacy); 97 bool DestroyEthernetIface(const std::string& name, 98 bool has_ipv4_bridge, bool use_ipv6, 99 bool use_ebtables_legacy); 100 void CleanupEthernetIface(const std::string& name, 101 const EthernetNetworkConfig& config); 102 103 bool IptableConfig(const std::string& network, bool add); 104 105 bool LinkTapToBridge(const std::string& tap_name, 106 const std::string& bridge_name); 107 108 bool SetupBridgeGateway(const std::string& name, const std::string& ipaddr); 109 void CleanupBridgeGateway(const std::string& name, const std::string& ipaddr, 110 const GatewayConfig& config); 111 112 bool CreateEthernetBridgeIface(const std::string& name, 113 const std::string &ipaddr); 114 bool DestroyEthernetBridgeIface(const std::string& name, 115 const std::string &ipaddr); 116 117 bool AddGateway(const std::string& name, const std::string& gateway, 118 const std::string& netmask); 119 bool DestroyGateway(const std::string& name, const std::string& gateway, 120 const std::string& netmask); 121 122 bool StartDnsmasq(const std::string& bridge_name, const std::string& gateway, 123 const std::string& dhcp_range); 124 bool StopDnsmasq(const std::string& name); 125 126 } // namespace cuttlefish 127