Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | * binder_test.cpp - unit tests for netd binder RPCs. |
| 17 | */ |
| 18 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 19 | #include <cerrno> |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 20 | #include <chrono> |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 21 | #include <cinttypes> |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 22 | #include <condition_variable> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 23 | #include <cstdint> |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 24 | #include <cstdlib> |
Bernie Innocenti | 196f1b8 | 2019-05-20 16:34:16 +0900 | [diff] [blame] | 25 | #include <iostream> |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 26 | #include <mutex> |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 27 | #include <set> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 30 | #include <dirent.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 31 | #include <fcntl.h> |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 32 | #include <ifaddrs.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 33 | #include <linux/if.h> |
| 34 | #include <linux/if_tun.h> |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 35 | #include <net/if.h> |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 36 | #include <netdb.h> |
| 37 | #include <netinet/in.h> |
Lorenzo Colitti | 5c53ae5 | 2020-04-15 17:26:21 +0000 | [diff] [blame^] | 38 | #include <netinet/tcp.h> |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 39 | #include <openssl/base64.h> |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 40 | #include <sys/socket.h> |
| 41 | #include <sys/types.h> |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 42 | |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 43 | #include <android-base/file.h> |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 44 | #include <android-base/macros.h> |
Lorenzo Colitti | 5c53ae5 | 2020-04-15 17:26:21 +0000 | [diff] [blame^] | 45 | #include <android-base/scopeguard.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 46 | #include <android-base/stringprintf.h> |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 47 | #include <android-base/strings.h> |
Chiachang Wang | 00fc62f | 2019-12-04 20:38:26 +0800 | [diff] [blame] | 48 | #include <android/multinetwork.h> |
Luke Huang | 2ff8b34 | 2019-04-30 15:33:33 +0800 | [diff] [blame] | 49 | #include <binder/IPCThreadState.h> |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 50 | #include <bpf/BpfMap.h> |
Chenbo Feng | 837ddfc | 2018-05-08 13:45:08 -0700 | [diff] [blame] | 51 | #include <bpf/BpfUtils.h> |
Luke Huang | 2ff8b34 | 2019-04-30 15:33:33 +0800 | [diff] [blame] | 52 | #include <com/android/internal/net/BnOemNetdUnsolicitedEventListener.h> |
| 53 | #include <com/android/internal/net/IOemNetd.h> |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 54 | #include <cutils/multiuser.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 55 | #include <gtest/gtest.h> |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 56 | #include <netdbpf/bpf_shared.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 57 | #include <netutils/ifc.h> |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 58 | #include "Fwmark.h" |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 59 | #include "InterfaceController.h" |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 60 | #include "NetdClient.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 61 | #include "NetdConstants.h" |
Chiachang Wang | 00fc62f | 2019-12-04 20:38:26 +0800 | [diff] [blame] | 62 | #include "NetworkController.h" |
Lorenzo Colitti | 5c53ae5 | 2020-04-15 17:26:21 +0000 | [diff] [blame^] | 63 | #include "SockDiag.h" |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 64 | #include "TestUnsolService.h" |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 65 | #include "XfrmController.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 66 | #include "android/net/INetd.h" |
| 67 | #include "binder/IServiceManager.h" |
Luke Huang | 2559932 | 2019-06-14 00:34:05 +0800 | [diff] [blame] | 68 | #include "netdutils/InternetAddresses.h" |
Mike Yu | e7e332f | 2019-03-13 17:15:48 +0800 | [diff] [blame] | 69 | #include "netdutils/Stopwatch.h" |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 70 | #include "netdutils/Syscalls.h" |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 71 | #include "netid_client.h" // NETID_UNSET |
Lorenzo Colitti | 758bccc | 2019-06-26 22:12:35 +0900 | [diff] [blame] | 72 | #include "test_utils.h" |
Mike Yu | 5ae6154 | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 73 | #include "tun_interface.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 74 | |
Lorenzo Colitti | 5c68b9c | 2017-08-10 18:50:10 +0900 | [diff] [blame] | 75 | #define IP6TABLES_PATH "/system/bin/ip6tables" |
| 76 | #define IPTABLES_PATH "/system/bin/iptables" |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 77 | #define RAW_TABLE "raw" |
| 78 | #define MANGLE_TABLE "mangle" |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 79 | #define FILTER_TABLE "filter" |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 80 | #define NAT_TABLE "nat" |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 81 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 82 | namespace binder = android::binder; |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 83 | |
| 84 | using android::IBinder; |
| 85 | using android::IServiceManager; |
| 86 | using android::sp; |
| 87 | using android::String16; |
| 88 | using android::String8; |
| 89 | using android::base::Join; |
Lorenzo Colitti | 5c53ae5 | 2020-04-15 17:26:21 +0000 | [diff] [blame^] | 90 | using android::base::make_scope_guard; |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 91 | using android::base::ReadFileToString; |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 92 | using android::base::StartsWith; |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 93 | using android::base::StringPrintf; |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 94 | using android::base::Trim; |
Bernie Innocenti | d26a04a | 2019-10-30 17:15:15 +0900 | [diff] [blame] | 95 | using android::base::unique_fd; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 96 | using android::net::INetd; |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 97 | using android::net::InterfaceConfigurationParcel; |
| 98 | using android::net::InterfaceController; |
Chiachang Wang | 00fc62f | 2019-12-04 20:38:26 +0800 | [diff] [blame] | 99 | using android::net::MarkMaskParcel; |
Lorenzo Colitti | 5c53ae5 | 2020-04-15 17:26:21 +0000 | [diff] [blame^] | 100 | using android::net::SockDiag; |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 101 | using android::net::TetherOffloadRuleParcel; |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 102 | using android::net::TetherStatsParcel; |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 103 | using android::net::TunInterface; |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 104 | using android::net::UidRangeParcel; |
Lorenzo Colitti | 5c53ae5 | 2020-04-15 17:26:21 +0000 | [diff] [blame^] | 105 | using android::netdutils::IPAddress; |
Luke Huang | 2559932 | 2019-06-14 00:34:05 +0800 | [diff] [blame] | 106 | using android::netdutils::ScopedAddrinfo; |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 107 | using android::netdutils::sSyscalls; |
Mike Yu | e7e332f | 2019-03-13 17:15:48 +0800 | [diff] [blame] | 108 | using android::netdutils::Stopwatch; |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 109 | |
| 110 | static const char* IP_RULE_V4 = "-4"; |
| 111 | static const char* IP_RULE_V6 = "-6"; |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 112 | static const int TEST_NETID1 = 65501; |
| 113 | static const int TEST_NETID2 = 65502; |
Lorenzo Colitti | 5c53ae5 | 2020-04-15 17:26:21 +0000 | [diff] [blame^] | 114 | static const char* DNSMASQ = "dnsmasq"; |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 115 | |
| 116 | // Use maximum reserved appId for applications to avoid conflict with existing |
| 117 | // uids. |
| 118 | static const int TEST_UID1 = 99999; |
| 119 | static const int TEST_UID2 = 99998; |
| 120 | |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 121 | constexpr int BASE_UID = AID_USER_OFFSET * 5; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 122 | |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 123 | static const std::string NO_SOCKET_ALLOW_RULE("! owner UID match 0-4294967294"); |
| 124 | static const std::string ESP_ALLOW_RULE("esp"); |
| 125 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 126 | class BinderTest : public ::testing::Test { |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 127 | public: |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 128 | BinderTest() { |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 129 | sp<IServiceManager> sm = android::defaultServiceManager(); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 130 | sp<IBinder> binder = sm->getService(String16("netd")); |
| 131 | if (binder != nullptr) { |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 132 | mNetd = android::interface_cast<INetd>(binder); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 136 | void SetUp() override { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 137 | ASSERT_NE(nullptr, mNetd.get()); |
| 138 | } |
| 139 | |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 140 | void TearDown() override { |
| 141 | mNetd->networkDestroy(TEST_NETID1); |
| 142 | mNetd->networkDestroy(TEST_NETID2); |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 143 | setNetworkForProcess(NETID_UNSET); |
| 144 | // Restore default network |
| 145 | if (mStoredDefaultNetwork >= 0) mNetd->networkSetDefault(mStoredDefaultNetwork); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 146 | } |
| 147 | |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 148 | bool allocateIpSecResources(bool expectOk, int32_t* spi); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 149 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 150 | // Static because setting up the tun interface takes about 40ms. |
| 151 | static void SetUpTestCase() { |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 152 | ASSERT_EQ(0, sTun.init()); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 153 | ASSERT_EQ(0, sTun2.init()); |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 154 | ASSERT_LE(sTun.name().size(), static_cast<size_t>(IFNAMSIZ)); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 155 | ASSERT_LE(sTun2.name().size(), static_cast<size_t>(IFNAMSIZ)); |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void TearDownTestCase() { |
| 159 | // Closing the socket removes the interface and IP addresses. |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 160 | sTun.destroy(); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 161 | sTun2.destroy(); |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 162 | } |
| 163 | |
Bernie Innocenti | d26a04a | 2019-10-30 17:15:15 +0900 | [diff] [blame] | 164 | static void fakeRemoteSocketPair(unique_fd* clientSocket, unique_fd* serverSocket, |
| 165 | unique_fd* acceptedSocket); |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 166 | |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 167 | void createVpnNetworkWithUid(bool secure, uid_t uid, int vpnNetId = TEST_NETID2, |
| 168 | int fallthroughNetId = TEST_NETID1); |
| 169 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 170 | protected: |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 171 | // Use -1 to represent that default network was not modified because |
| 172 | // real netId must be an unsigned value. |
| 173 | int mStoredDefaultNetwork = -1; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 174 | sp<INetd> mNetd; |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 175 | static TunInterface sTun; |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 176 | static TunInterface sTun2; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 177 | }; |
| 178 | |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 179 | TunInterface BinderTest::sTun; |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 180 | TunInterface BinderTest::sTun2; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 181 | |
Lorenzo Colitti | 699aa99 | 2016-04-15 10:22:37 +0900 | [diff] [blame] | 182 | class TimedOperation : public Stopwatch { |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 183 | public: |
Chih-Hung Hsieh | 1805105 | 2016-05-06 10:36:13 -0700 | [diff] [blame] | 184 | explicit TimedOperation(const std::string &name): mName(name) {} |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 185 | virtual ~TimedOperation() { |
Bernie Innocenti | 196f1b8 | 2019-05-20 16:34:16 +0900 | [diff] [blame] | 186 | std::cerr << " " << mName << ": " << timeTakenUs() << "us" << std::endl; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 187 | } |
| 188 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 189 | private: |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 190 | std::string mName; |
| 191 | }; |
| 192 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 193 | TEST_F(BinderTest, IsAlive) { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 194 | TimedOperation t("isAlive RPC"); |
| 195 | bool isAlive = false; |
| 196 | mNetd->isAlive(&isAlive); |
| 197 | ASSERT_TRUE(isAlive); |
| 198 | } |
| 199 | |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 200 | static bool iptablesNoSocketAllowRuleExists(const char *chainName){ |
| 201 | return iptablesRuleExists(IPTABLES_PATH, chainName, NO_SOCKET_ALLOW_RULE) && |
| 202 | iptablesRuleExists(IP6TABLES_PATH, chainName, NO_SOCKET_ALLOW_RULE); |
| 203 | } |
| 204 | |
| 205 | static bool iptablesEspAllowRuleExists(const char *chainName){ |
| 206 | return iptablesRuleExists(IPTABLES_PATH, chainName, ESP_ALLOW_RULE) && |
| 207 | iptablesRuleExists(IP6TABLES_PATH, chainName, ESP_ALLOW_RULE); |
| 208 | } |
| 209 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 210 | TEST_F(BinderTest, FirewallReplaceUidChain) { |
Chenbo Feng | 837ddfc | 2018-05-08 13:45:08 -0700 | [diff] [blame] | 211 | SKIP_IF_BPF_SUPPORTED; |
| 212 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 213 | std::string chainName = StringPrintf("netd_binder_test_%u", arc4random_uniform(10000)); |
| 214 | const int kNumUids = 500; |
| 215 | std::vector<int32_t> noUids(0); |
| 216 | std::vector<int32_t> uids(kNumUids); |
| 217 | for (int i = 0; i < kNumUids; i++) { |
| 218 | uids[i] = randomUid(); |
| 219 | } |
| 220 | |
| 221 | bool ret; |
| 222 | { |
| 223 | TimedOperation op(StringPrintf("Programming %d-UID whitelist chain", kNumUids)); |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 224 | mNetd->firewallReplaceUidChain(chainName, true, uids, &ret); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 225 | } |
| 226 | EXPECT_EQ(true, ret); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 227 | EXPECT_EQ((int) uids.size() + 9, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 228 | EXPECT_EQ((int) uids.size() + 15, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
| 229 | EXPECT_EQ(true, iptablesNoSocketAllowRuleExists(chainName.c_str())); |
| 230 | EXPECT_EQ(true, iptablesEspAllowRuleExists(chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 231 | { |
| 232 | TimedOperation op("Clearing whitelist chain"); |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 233 | mNetd->firewallReplaceUidChain(chainName, false, noUids, &ret); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 234 | } |
| 235 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 236 | EXPECT_EQ(5, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 237 | EXPECT_EQ(5, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 238 | |
| 239 | { |
| 240 | TimedOperation op(StringPrintf("Programming %d-UID blacklist chain", kNumUids)); |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 241 | mNetd->firewallReplaceUidChain(chainName, false, uids, &ret); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 242 | } |
| 243 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 244 | EXPECT_EQ((int) uids.size() + 5, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 245 | EXPECT_EQ((int) uids.size() + 5, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 246 | EXPECT_EQ(false, iptablesNoSocketAllowRuleExists(chainName.c_str())); |
| 247 | EXPECT_EQ(false, iptablesEspAllowRuleExists(chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 248 | |
| 249 | { |
| 250 | TimedOperation op("Clearing blacklist chain"); |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 251 | mNetd->firewallReplaceUidChain(chainName, false, noUids, &ret); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 252 | } |
| 253 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 254 | EXPECT_EQ(5, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 255 | EXPECT_EQ(5, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 256 | |
| 257 | // Check that the call fails if iptables returns an error. |
| 258 | std::string veryLongStringName = "netd_binder_test_UnacceptablyLongIptablesChainName"; |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 259 | mNetd->firewallReplaceUidChain(veryLongStringName, true, noUids, &ret); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 260 | EXPECT_EQ(false, ret); |
| 261 | } |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 262 | |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 263 | TEST_F(BinderTest, IpSecTunnelInterface) { |
George Burgess IV | c4a6d27 | 2018-05-21 14:41:57 -0700 | [diff] [blame] | 264 | const struct TestData { |
| 265 | const std::string family; |
| 266 | const std::string deviceName; |
| 267 | const std::string localAddress; |
| 268 | const std::string remoteAddress; |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 269 | int32_t iKey; |
| 270 | int32_t oKey; |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 271 | int32_t ifId; |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 272 | } kTestData[] = { |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 273 | {"IPV4", "ipsec_test", "127.0.0.1", "8.8.8.8", 0x1234 + 53, 0x1234 + 53, 0xFFFE}, |
| 274 | {"IPV6", "ipsec_test6", "::1", "2001:4860:4860::8888", 0x1234 + 50, 0x1234 + 50, |
| 275 | 0xFFFE}, |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 276 | }; |
| 277 | |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 278 | for (size_t i = 0; i < std::size(kTestData); i++) { |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 279 | const auto& td = kTestData[i]; |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 280 | |
| 281 | binder::Status status; |
| 282 | |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 283 | // Create Tunnel Interface. |
| 284 | status = mNetd->ipSecAddTunnelInterface(td.deviceName, td.localAddress, td.remoteAddress, |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 285 | td.iKey, td.oKey, td.ifId); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 286 | EXPECT_TRUE(status.isOk()) << td.family << status.exceptionMessage(); |
| 287 | |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 288 | // Check that the interface exists |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 289 | EXPECT_NE(0U, if_nametoindex(td.deviceName.c_str())); |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 290 | |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 291 | // Update Tunnel Interface. |
| 292 | status = mNetd->ipSecUpdateTunnelInterface(td.deviceName, td.localAddress, td.remoteAddress, |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 293 | td.iKey, td.oKey, td.ifId); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 294 | EXPECT_TRUE(status.isOk()) << td.family << status.exceptionMessage(); |
| 295 | |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 296 | // Remove Tunnel Interface. |
| 297 | status = mNetd->ipSecRemoveTunnelInterface(td.deviceName); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 298 | EXPECT_TRUE(status.isOk()) << td.family << status.exceptionMessage(); |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 299 | |
| 300 | // Check that the interface no longer exists |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 301 | EXPECT_EQ(0U, if_nametoindex(td.deviceName.c_str())); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
Benedict Wong | 1cb73df | 2018-12-03 00:54:14 -0800 | [diff] [blame] | 305 | TEST_F(BinderTest, IpSecSetEncapSocketOwner) { |
Bernie Innocenti | d26a04a | 2019-10-30 17:15:15 +0900 | [diff] [blame] | 306 | unique_fd uniqueFd(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
Benedict Wong | 1cb73df | 2018-12-03 00:54:14 -0800 | [diff] [blame] | 307 | android::os::ParcelFileDescriptor sockFd(std::move(uniqueFd)); |
| 308 | |
| 309 | int sockOptVal = UDP_ENCAP_ESPINUDP; |
| 310 | setsockopt(sockFd.get(), IPPROTO_UDP, UDP_ENCAP, &sockOptVal, sizeof(sockOptVal)); |
| 311 | |
| 312 | binder::Status res = mNetd->ipSecSetEncapSocketOwner(sockFd, 1001); |
| 313 | EXPECT_TRUE(res.isOk()); |
| 314 | |
| 315 | struct stat info; |
| 316 | EXPECT_EQ(0, fstat(sockFd.get(), &info)); |
| 317 | EXPECT_EQ(1001, (int) info.st_uid); |
| 318 | } |
| 319 | |
Nathan Harold | 2deff32 | 2018-05-10 14:03:48 -0700 | [diff] [blame] | 320 | // IPsec tests are not run in 32 bit mode; both 32-bit kernels and |
| 321 | // mismatched ABIs (64-bit kernel with 32-bit userspace) are unsupported. |
| 322 | #if INTPTR_MAX != INT32_MAX |
Bernie Innocenti | a5161a0 | 2019-01-30 22:40:53 +0900 | [diff] [blame] | 323 | |
| 324 | using android::net::XfrmController; |
| 325 | |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 326 | static const int XFRM_DIRECTIONS[] = {static_cast<int>(android::net::XfrmDirection::IN), |
| 327 | static_cast<int>(android::net::XfrmDirection::OUT)}; |
| 328 | static const int ADDRESS_FAMILIES[] = {AF_INET, AF_INET6}; |
| 329 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 330 | #define RETURN_FALSE_IF_NEQ(_expect_, _ret_) \ |
| 331 | do { if ((_expect_) != (_ret_)) return false; } while(false) |
Bernie Innocenti | 6f9fd90 | 2018-10-11 20:50:23 +0900 | [diff] [blame] | 332 | bool BinderTest::allocateIpSecResources(bool expectOk, int32_t* spi) { |
Bernie Innocenti | a5161a0 | 2019-01-30 22:40:53 +0900 | [diff] [blame] | 333 | android::netdutils::Status status = XfrmController::ipSecAllocateSpi(0, "::", "::1", 123, spi); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 334 | SCOPED_TRACE(status); |
| 335 | RETURN_FALSE_IF_NEQ(status.ok(), expectOk); |
| 336 | |
| 337 | // Add a policy |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 338 | status = XfrmController::ipSecAddSecurityPolicy(0, AF_INET6, 0, "::", "::1", 123, 0, 0, 0); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 339 | SCOPED_TRACE(status); |
| 340 | RETURN_FALSE_IF_NEQ(status.ok(), expectOk); |
| 341 | |
| 342 | // Add an ipsec interface |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 343 | return expectOk == XfrmController::ipSecAddTunnelInterface("ipsec_test", "::", "::1", 0xF00D, |
| 344 | 0xD00D, 0xE00D, false) |
| 345 | .ok(); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 348 | TEST_F(BinderTest, XfrmDualSelectorTunnelModePoliciesV4) { |
Bernie Innocenti | a5161a0 | 2019-01-30 22:40:53 +0900 | [diff] [blame] | 349 | android::binder::Status status; |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 350 | |
| 351 | // Repeat to ensure cleanup and recreation works correctly |
| 352 | for (int i = 0; i < 2; i++) { |
| 353 | for (int direction : XFRM_DIRECTIONS) { |
| 354 | for (int addrFamily : ADDRESS_FAMILIES) { |
| 355 | status = mNetd->ipSecAddSecurityPolicy(0, addrFamily, direction, "127.0.0.5", |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 356 | "127.0.0.6", 123, 0, 0, 0); |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 357 | EXPECT_TRUE(status.isOk()) |
| 358 | << " family: " << addrFamily << " direction: " << direction; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // Cleanup |
| 363 | for (int direction : XFRM_DIRECTIONS) { |
| 364 | for (int addrFamily : ADDRESS_FAMILIES) { |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 365 | status = mNetd->ipSecDeleteSecurityPolicy(0, addrFamily, direction, 0, 0, 0); |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 366 | EXPECT_TRUE(status.isOk()); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | TEST_F(BinderTest, XfrmDualSelectorTunnelModePoliciesV6) { |
| 373 | binder::Status status; |
| 374 | |
| 375 | // Repeat to ensure cleanup and recreation works correctly |
| 376 | for (int i = 0; i < 2; i++) { |
| 377 | for (int direction : XFRM_DIRECTIONS) { |
| 378 | for (int addrFamily : ADDRESS_FAMILIES) { |
| 379 | status = mNetd->ipSecAddSecurityPolicy(0, addrFamily, direction, "2001:db8::f00d", |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 380 | "2001:db8::d00d", 123, 0, 0, 0); |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 381 | EXPECT_TRUE(status.isOk()) |
| 382 | << " family: " << addrFamily << " direction: " << direction; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | // Cleanup |
| 387 | for (int direction : XFRM_DIRECTIONS) { |
| 388 | for (int addrFamily : ADDRESS_FAMILIES) { |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 389 | status = mNetd->ipSecDeleteSecurityPolicy(0, addrFamily, direction, 0, 0, 0); |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 390 | EXPECT_TRUE(status.isOk()); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 396 | TEST_F(BinderTest, XfrmControllerInit) { |
Bernie Innocenti | a5161a0 | 2019-01-30 22:40:53 +0900 | [diff] [blame] | 397 | android::netdutils::Status status; |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 398 | status = XfrmController::Init(); |
| 399 | SCOPED_TRACE(status); |
Nathan Harold | 2deff32 | 2018-05-10 14:03:48 -0700 | [diff] [blame] | 400 | |
| 401 | // Older devices or devices with mismatched Kernel/User ABI cannot support the IPsec |
| 402 | // feature. |
| 403 | if (status.code() == EOPNOTSUPP) return; |
| 404 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 405 | ASSERT_TRUE(status.ok()); |
| 406 | |
| 407 | int32_t spi = 0; |
| 408 | |
| 409 | ASSERT_TRUE(allocateIpSecResources(true, &spi)); |
| 410 | ASSERT_TRUE(allocateIpSecResources(false, &spi)); |
| 411 | |
| 412 | status = XfrmController::Init(); |
Nathan Harold | 39ad662 | 2018-04-25 12:56:56 -0700 | [diff] [blame] | 413 | ASSERT_TRUE(status.ok()); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 414 | ASSERT_TRUE(allocateIpSecResources(true, &spi)); |
| 415 | |
| 416 | // Clean up |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 417 | status = XfrmController::ipSecDeleteSecurityAssociation(0, "::", "::1", 123, spi, 0, 0); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 418 | SCOPED_TRACE(status); |
| 419 | ASSERT_TRUE(status.ok()); |
| 420 | |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 421 | status = XfrmController::ipSecDeleteSecurityPolicy(0, AF_INET6, 0, 0, 0, 0); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 422 | SCOPED_TRACE(status); |
| 423 | ASSERT_TRUE(status.ok()); |
| 424 | |
| 425 | // Remove Virtual Tunnel Interface. |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 426 | ASSERT_TRUE(XfrmController::ipSecRemoveTunnelInterface("ipsec_test").ok()); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 427 | } |
Bernie Innocenti | a5161a0 | 2019-01-30 22:40:53 +0900 | [diff] [blame] | 428 | |
| 429 | #endif // INTPTR_MAX != INT32_MAX |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 430 | |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 431 | static int bandwidthDataSaverEnabled(const char *binary) { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 432 | std::vector<std::string> lines = listIptablesRule(binary, "bw_data_saver"); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 433 | |
| 434 | // Output looks like this: |
| 435 | // |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 436 | // Chain bw_data_saver (1 references) |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 437 | // target prot opt source destination |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 438 | // RETURN all -- 0.0.0.0/0 0.0.0.0/0 |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 439 | // |
| 440 | // or: |
| 441 | // |
| 442 | // Chain bw_data_saver (1 references) |
| 443 | // target prot opt source destination |
| 444 | // ... possibly connectivity critical packet rules here ... |
| 445 | // REJECT all -- ::/0 ::/0 |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 446 | |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 447 | EXPECT_GE(lines.size(), 3U); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 448 | |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 449 | if (lines.size() == 3 && StartsWith(lines[2], "RETURN ")) { |
| 450 | // Data saver disabled. |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | size_t minSize = (std::string(binary) == IPTABLES_PATH) ? 3 : 9; |
| 455 | |
| 456 | if (lines.size() >= minSize && StartsWith(lines[lines.size() -1], "REJECT ")) { |
| 457 | // Data saver enabled. |
| 458 | return 1; |
| 459 | } |
| 460 | |
| 461 | return -1; |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | bool enableDataSaver(sp<INetd>& netd, bool enable) { |
| 465 | TimedOperation op(enable ? " Enabling data saver" : "Disabling data saver"); |
| 466 | bool ret; |
| 467 | netd->bandwidthEnableDataSaver(enable, &ret); |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | int getDataSaverState() { |
| 472 | const int enabled4 = bandwidthDataSaverEnabled(IPTABLES_PATH); |
| 473 | const int enabled6 = bandwidthDataSaverEnabled(IP6TABLES_PATH); |
| 474 | EXPECT_EQ(enabled4, enabled6); |
| 475 | EXPECT_NE(-1, enabled4); |
| 476 | EXPECT_NE(-1, enabled6); |
| 477 | if (enabled4 != enabled6 || (enabled6 != 0 && enabled6 != 1)) { |
| 478 | return -1; |
| 479 | } |
| 480 | return enabled6; |
| 481 | } |
| 482 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 483 | TEST_F(BinderTest, BandwidthEnableDataSaver) { |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 484 | const int wasEnabled = getDataSaverState(); |
| 485 | ASSERT_NE(-1, wasEnabled); |
| 486 | |
| 487 | if (wasEnabled) { |
| 488 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 489 | EXPECT_EQ(0, getDataSaverState()); |
| 490 | } |
| 491 | |
| 492 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 493 | EXPECT_EQ(0, getDataSaverState()); |
| 494 | |
| 495 | ASSERT_TRUE(enableDataSaver(mNetd, true)); |
| 496 | EXPECT_EQ(1, getDataSaverState()); |
| 497 | |
| 498 | ASSERT_TRUE(enableDataSaver(mNetd, true)); |
| 499 | EXPECT_EQ(1, getDataSaverState()); |
| 500 | |
| 501 | if (!wasEnabled) { |
| 502 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 503 | EXPECT_EQ(0, getDataSaverState()); |
| 504 | } |
| 505 | } |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 506 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 507 | static bool ipRuleExistsForRange(const uint32_t priority, const UidRangeParcel& range, |
| 508 | const std::string& action, const char* ipVersion) { |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 509 | // Output looks like this: |
Robin Lee | 6c84ef6 | 2016-05-03 13:17:58 +0100 | [diff] [blame] | 510 | // "12500:\tfrom all fwmark 0x0/0x20000 iif lo uidrange 1000-2000 prohibit" |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 511 | std::vector<std::string> rules = listIpRules(ipVersion); |
| 512 | |
| 513 | std::string prefix = StringPrintf("%" PRIu32 ":", priority); |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 514 | std::string suffix = |
| 515 | StringPrintf(" iif lo uidrange %d-%d %s\n", range.start, range.stop, action.c_str()); |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 516 | for (const auto& line : rules) { |
Elliott Hughes | 2f44508 | 2017-12-20 12:39:35 -0800 | [diff] [blame] | 517 | if (android::base::StartsWith(line, prefix) && android::base::EndsWith(line, suffix)) { |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 518 | return true; |
| 519 | } |
| 520 | } |
| 521 | return false; |
| 522 | } |
| 523 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 524 | static bool ipRuleExistsForRange(const uint32_t priority, const UidRangeParcel& range, |
| 525 | const std::string& action) { |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 526 | bool existsIp4 = ipRuleExistsForRange(priority, range, action, IP_RULE_V4); |
| 527 | bool existsIp6 = ipRuleExistsForRange(priority, range, action, IP_RULE_V6); |
| 528 | EXPECT_EQ(existsIp4, existsIp6); |
| 529 | return existsIp4; |
| 530 | } |
| 531 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 532 | namespace { |
| 533 | |
| 534 | UidRangeParcel makeUidRangeParcel(int start, int stop) { |
| 535 | UidRangeParcel res; |
| 536 | res.start = start; |
| 537 | res.stop = stop; |
| 538 | |
| 539 | return res; |
| 540 | } |
| 541 | |
| 542 | } // namespace |
| 543 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 544 | TEST_F(BinderTest, NetworkInterfaces) { |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 545 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, INetd::PERMISSION_NONE).isOk()); |
| 546 | EXPECT_EQ(EEXIST, mNetd->networkCreatePhysical(TEST_NETID1, INetd::PERMISSION_NONE) |
| 547 | .serviceSpecificErrorCode()); |
cken | 67cd14c | 2018-12-05 17:26:59 +0900 | [diff] [blame] | 548 | EXPECT_EQ(EEXIST, mNetd->networkCreateVpn(TEST_NETID1, true).serviceSpecificErrorCode()); |
| 549 | EXPECT_TRUE(mNetd->networkCreateVpn(TEST_NETID2, true).isOk()); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 550 | |
| 551 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 552 | EXPECT_EQ(EBUSY, |
| 553 | mNetd->networkAddInterface(TEST_NETID2, sTun.name()).serviceSpecificErrorCode()); |
| 554 | |
| 555 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 556 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID2, sTun.name()).isOk()); |
| 557 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID2).isOk()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 558 | EXPECT_EQ(ENONET, mNetd->networkDestroy(TEST_NETID1).serviceSpecificErrorCode()); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 559 | } |
| 560 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 561 | TEST_F(BinderTest, NetworkUidRules) { |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 562 | const uint32_t RULE_PRIORITY_SECURE_VPN = 12000; |
| 563 | |
cken | 67cd14c | 2018-12-05 17:26:59 +0900 | [diff] [blame] | 564 | EXPECT_TRUE(mNetd->networkCreateVpn(TEST_NETID1, true).isOk()); |
| 565 | EXPECT_EQ(EEXIST, mNetd->networkCreateVpn(TEST_NETID1, true).serviceSpecificErrorCode()); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 566 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 567 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 568 | std::vector<UidRangeParcel> uidRanges = {makeUidRangeParcel(BASE_UID + 8005, BASE_UID + 8012), |
| 569 | makeUidRangeParcel(BASE_UID + 8090, BASE_UID + 8099)}; |
| 570 | UidRangeParcel otherRange = makeUidRangeParcel(BASE_UID + 8190, BASE_UID + 8299); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 571 | std::string suffix = StringPrintf("lookup %s ", sTun.name().c_str()); |
| 572 | |
| 573 | EXPECT_TRUE(mNetd->networkAddUidRanges(TEST_NETID1, uidRanges).isOk()); |
| 574 | |
| 575 | EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[0], suffix)); |
| 576 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, otherRange, suffix)); |
| 577 | EXPECT_TRUE(mNetd->networkRemoveUidRanges(TEST_NETID1, uidRanges).isOk()); |
| 578 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[0], suffix)); |
| 579 | |
| 580 | EXPECT_TRUE(mNetd->networkAddUidRanges(TEST_NETID1, uidRanges).isOk()); |
| 581 | EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[1], suffix)); |
| 582 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 583 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[1], suffix)); |
| 584 | |
| 585 | EXPECT_EQ(ENONET, mNetd->networkDestroy(TEST_NETID1).serviceSpecificErrorCode()); |
| 586 | } |
| 587 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 588 | TEST_F(BinderTest, NetworkRejectNonSecureVpn) { |
Robin Lee | 6c84ef6 | 2016-05-03 13:17:58 +0100 | [diff] [blame] | 589 | constexpr uint32_t RULE_PRIORITY = 12500; |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 590 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 591 | std::vector<UidRangeParcel> uidRanges = {makeUidRangeParcel(BASE_UID + 150, BASE_UID + 224), |
| 592 | makeUidRangeParcel(BASE_UID + 226, BASE_UID + 300)}; |
Luke Huang | 841f3ef | 2019-05-30 15:09:19 +0800 | [diff] [blame] | 593 | // Make sure no rules existed before calling commands. |
| 594 | for (auto const& range : uidRanges) { |
| 595 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY, range, "prohibit")); |
| 596 | } |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 597 | // Create two valid rules. |
| 598 | ASSERT_TRUE(mNetd->networkRejectNonSecureVpn(true, uidRanges).isOk()); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 599 | for (auto const& range : uidRanges) { |
| 600 | EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY, range, "prohibit")); |
| 601 | } |
| 602 | |
| 603 | // Remove the rules. |
| 604 | ASSERT_TRUE(mNetd->networkRejectNonSecureVpn(false, uidRanges).isOk()); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 605 | for (auto const& range : uidRanges) { |
| 606 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY, range, "prohibit")); |
| 607 | } |
| 608 | |
| 609 | // Fail to remove the rules a second time after they are already deleted. |
| 610 | binder::Status status = mNetd->networkRejectNonSecureVpn(false, uidRanges); |
| 611 | ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 612 | EXPECT_EQ(ENOENT, status.serviceSpecificErrorCode()); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 613 | } |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 614 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 615 | // Create a socket pair that isLoopbackSocket won't think is local. |
Bernie Innocenti | d26a04a | 2019-10-30 17:15:15 +0900 | [diff] [blame] | 616 | void BinderTest::fakeRemoteSocketPair(unique_fd* clientSocket, unique_fd* serverSocket, |
| 617 | unique_fd* acceptedSocket) { |
| 618 | serverSocket->reset(socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0)); |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 619 | struct sockaddr_in6 server6 = { .sin6_family = AF_INET6, .sin6_addr = sTun.dstAddr() }; |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 620 | ASSERT_EQ(0, bind(*serverSocket, (struct sockaddr *) &server6, sizeof(server6))); |
| 621 | |
| 622 | socklen_t addrlen = sizeof(server6); |
| 623 | ASSERT_EQ(0, getsockname(*serverSocket, (struct sockaddr *) &server6, &addrlen)); |
| 624 | ASSERT_EQ(0, listen(*serverSocket, 10)); |
| 625 | |
Bernie Innocenti | d26a04a | 2019-10-30 17:15:15 +0900 | [diff] [blame] | 626 | clientSocket->reset(socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0)); |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 627 | struct sockaddr_in6 client6 = { .sin6_family = AF_INET6, .sin6_addr = sTun.srcAddr() }; |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 628 | ASSERT_EQ(0, bind(*clientSocket, (struct sockaddr *) &client6, sizeof(client6))); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 629 | ASSERT_EQ(0, connect(*clientSocket, (struct sockaddr *) &server6, sizeof(server6))); |
| 630 | ASSERT_EQ(0, getsockname(*clientSocket, (struct sockaddr *) &client6, &addrlen)); |
| 631 | |
Bernie Innocenti | d26a04a | 2019-10-30 17:15:15 +0900 | [diff] [blame] | 632 | acceptedSocket->reset( |
| 633 | accept4(*serverSocket, (struct sockaddr*)&server6, &addrlen, SOCK_CLOEXEC)); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 634 | ASSERT_NE(-1, *acceptedSocket); |
| 635 | |
| 636 | ASSERT_EQ(0, memcmp(&client6, &server6, sizeof(client6))); |
| 637 | } |
| 638 | |
| 639 | void checkSocketpairOpen(int clientSocket, int acceptedSocket) { |
| 640 | char buf[4096]; |
| 641 | EXPECT_EQ(4, write(clientSocket, "foo", sizeof("foo"))); |
| 642 | EXPECT_EQ(4, read(acceptedSocket, buf, sizeof(buf))); |
| 643 | EXPECT_EQ(0, memcmp(buf, "foo", sizeof("foo"))); |
| 644 | } |
| 645 | |
| 646 | void checkSocketpairClosed(int clientSocket, int acceptedSocket) { |
| 647 | // Check that the client socket was closed with ECONNABORTED. |
| 648 | int ret = write(clientSocket, "foo", sizeof("foo")); |
| 649 | int err = errno; |
| 650 | EXPECT_EQ(-1, ret); |
| 651 | EXPECT_EQ(ECONNABORTED, err); |
| 652 | |
| 653 | // Check that it sent a RST to the server. |
| 654 | ret = write(acceptedSocket, "foo", sizeof("foo")); |
| 655 | err = errno; |
| 656 | EXPECT_EQ(-1, ret); |
| 657 | EXPECT_EQ(ECONNRESET, err); |
| 658 | } |
| 659 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 660 | TEST_F(BinderTest, SocketDestroy) { |
Bernie Innocenti | d26a04a | 2019-10-30 17:15:15 +0900 | [diff] [blame] | 661 | unique_fd clientSocket, serverSocket, acceptedSocket; |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 662 | ASSERT_NO_FATAL_FAILURE(fakeRemoteSocketPair(&clientSocket, &serverSocket, &acceptedSocket)); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 663 | |
| 664 | // Pick a random UID in the system UID range. |
| 665 | constexpr int baseUid = AID_APP - 2000; |
| 666 | static_assert(baseUid > 0, "Not enough UIDs? Please fix this test."); |
| 667 | int uid = baseUid + 500 + arc4random_uniform(1000); |
| 668 | EXPECT_EQ(0, fchown(clientSocket, uid, -1)); |
| 669 | |
| 670 | // UID ranges that don't contain uid. |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 671 | std::vector<UidRangeParcel> uidRanges = { |
| 672 | makeUidRangeParcel(baseUid + 42, baseUid + 449), |
| 673 | makeUidRangeParcel(baseUid + 1536, AID_APP - 4), |
| 674 | makeUidRangeParcel(baseUid + 498, uid - 1), |
| 675 | makeUidRangeParcel(uid + 1, baseUid + 1520), |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 676 | }; |
| 677 | // A skip list that doesn't contain UID. |
| 678 | std::vector<int32_t> skipUids { baseUid + 123, baseUid + 1600 }; |
| 679 | |
| 680 | // Close sockets. Our test socket should be intact. |
| 681 | EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk()); |
| 682 | checkSocketpairOpen(clientSocket, acceptedSocket); |
| 683 | |
| 684 | // UID ranges that do contain uid. |
| 685 | uidRanges = { |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 686 | makeUidRangeParcel(baseUid + 42, baseUid + 449), |
| 687 | makeUidRangeParcel(baseUid + 1536, AID_APP - 4), |
| 688 | makeUidRangeParcel(baseUid + 498, baseUid + 1520), |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 689 | }; |
| 690 | // Add uid to the skip list. |
| 691 | skipUids.push_back(uid); |
| 692 | |
| 693 | // Close sockets. Our test socket should still be intact because it's in the skip list. |
| 694 | EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk()); |
| 695 | checkSocketpairOpen(clientSocket, acceptedSocket); |
| 696 | |
| 697 | // Now remove uid from skipUids, and close sockets. Our test socket should have been closed. |
| 698 | skipUids.resize(skipUids.size() - 1); |
| 699 | EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk()); |
| 700 | checkSocketpairClosed(clientSocket, acceptedSocket); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 701 | } |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 702 | |
| 703 | namespace { |
| 704 | |
| 705 | int netmaskToPrefixLength(const uint8_t *buf, size_t buflen) { |
| 706 | if (buf == nullptr) return -1; |
| 707 | |
| 708 | int prefixLength = 0; |
| 709 | bool endOfContiguousBits = false; |
| 710 | for (unsigned int i = 0; i < buflen; i++) { |
| 711 | const uint8_t value = buf[i]; |
| 712 | |
| 713 | // Bad bit sequence: check for a contiguous set of bits from the high |
| 714 | // end by verifying that the inverted value + 1 is a power of 2 |
| 715 | // (power of 2 iff. (v & (v - 1)) == 0). |
| 716 | const uint8_t inverse = ~value + 1; |
| 717 | if ((inverse & (inverse - 1)) != 0) return -1; |
| 718 | |
| 719 | prefixLength += (value == 0) ? 0 : CHAR_BIT - ffs(value) + 1; |
| 720 | |
| 721 | // Bogus netmask. |
| 722 | if (endOfContiguousBits && value != 0) return -1; |
| 723 | |
| 724 | if (value != 0xff) endOfContiguousBits = true; |
| 725 | } |
| 726 | |
| 727 | return prefixLength; |
| 728 | } |
| 729 | |
| 730 | template<typename T> |
| 731 | int netmaskToPrefixLength(const T *p) { |
| 732 | return netmaskToPrefixLength(reinterpret_cast<const uint8_t*>(p), sizeof(T)); |
| 733 | } |
| 734 | |
| 735 | |
| 736 | static bool interfaceHasAddress( |
| 737 | const std::string &ifname, const char *addrString, int prefixLength) { |
| 738 | struct addrinfo *addrinfoList = nullptr; |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 739 | |
| 740 | const struct addrinfo hints = { |
| 741 | .ai_flags = AI_NUMERICHOST, |
| 742 | .ai_family = AF_UNSPEC, |
| 743 | .ai_socktype = SOCK_DGRAM, |
| 744 | }; |
| 745 | if (getaddrinfo(addrString, nullptr, &hints, &addrinfoList) != 0 || |
| 746 | addrinfoList == nullptr || addrinfoList->ai_addr == nullptr) { |
| 747 | return false; |
| 748 | } |
Bernie Innocenti | 9bf749f | 2018-08-30 08:37:22 +0900 | [diff] [blame] | 749 | ScopedAddrinfo addrinfoCleanup(addrinfoList); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 750 | |
| 751 | struct ifaddrs *ifaddrsList = nullptr; |
| 752 | ScopedIfaddrs ifaddrsCleanup(ifaddrsList); |
| 753 | |
| 754 | if (getifaddrs(&ifaddrsList) != 0) { |
| 755 | return false; |
| 756 | } |
| 757 | |
| 758 | for (struct ifaddrs *addr = ifaddrsList; addr != nullptr; addr = addr->ifa_next) { |
| 759 | if (std::string(addr->ifa_name) != ifname || |
| 760 | addr->ifa_addr == nullptr || |
| 761 | addr->ifa_addr->sa_family != addrinfoList->ai_addr->sa_family) { |
| 762 | continue; |
| 763 | } |
| 764 | |
| 765 | switch (addr->ifa_addr->sa_family) { |
| 766 | case AF_INET: { |
| 767 | auto *addr4 = reinterpret_cast<const struct sockaddr_in*>(addr->ifa_addr); |
| 768 | auto *want = reinterpret_cast<const struct sockaddr_in*>(addrinfoList->ai_addr); |
| 769 | if (memcmp(&addr4->sin_addr, &want->sin_addr, sizeof(want->sin_addr)) != 0) { |
| 770 | continue; |
| 771 | } |
| 772 | |
| 773 | if (prefixLength < 0) return true; // not checking prefix lengths |
| 774 | |
| 775 | if (addr->ifa_netmask == nullptr) return false; |
| 776 | auto *nm = reinterpret_cast<const struct sockaddr_in*>(addr->ifa_netmask); |
| 777 | EXPECT_EQ(prefixLength, netmaskToPrefixLength(&nm->sin_addr)); |
| 778 | return (prefixLength == netmaskToPrefixLength(&nm->sin_addr)); |
| 779 | } |
| 780 | case AF_INET6: { |
| 781 | auto *addr6 = reinterpret_cast<const struct sockaddr_in6*>(addr->ifa_addr); |
| 782 | auto *want = reinterpret_cast<const struct sockaddr_in6*>(addrinfoList->ai_addr); |
| 783 | if (memcmp(&addr6->sin6_addr, &want->sin6_addr, sizeof(want->sin6_addr)) != 0) { |
| 784 | continue; |
| 785 | } |
| 786 | |
| 787 | if (prefixLength < 0) return true; // not checking prefix lengths |
| 788 | |
| 789 | if (addr->ifa_netmask == nullptr) return false; |
| 790 | auto *nm = reinterpret_cast<const struct sockaddr_in6*>(addr->ifa_netmask); |
| 791 | EXPECT_EQ(prefixLength, netmaskToPrefixLength(&nm->sin6_addr)); |
| 792 | return (prefixLength == netmaskToPrefixLength(&nm->sin6_addr)); |
| 793 | } |
| 794 | default: |
| 795 | // Cannot happen because we have already screened for matching |
| 796 | // address families at the top of each iteration. |
| 797 | continue; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | return false; |
| 802 | } |
| 803 | |
| 804 | } // namespace |
| 805 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 806 | TEST_F(BinderTest, InterfaceAddRemoveAddress) { |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 807 | static const struct TestData { |
| 808 | const char *addrString; |
| 809 | const int prefixLength; |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 810 | const int expectAddResult; |
| 811 | const int expectRemoveResult; |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 812 | } kTestData[] = { |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 813 | {"192.0.2.1", 24, 0, 0}, |
| 814 | {"192.0.2.2", 25, 0, 0}, |
| 815 | {"192.0.2.3", 32, 0, 0}, |
| 816 | {"192.0.2.4", 33, EINVAL, EADDRNOTAVAIL}, |
| 817 | {"192.not.an.ip", 24, EINVAL, EINVAL}, |
| 818 | {"2001:db8::1", 64, 0, 0}, |
| 819 | {"2001:db8::2", 65, 0, 0}, |
| 820 | {"2001:db8::3", 128, 0, 0}, |
| 821 | {"2001:db8::4", 129, EINVAL, EINVAL}, |
| 822 | {"foo:bar::bad", 64, EINVAL, EINVAL}, |
| 823 | {"2001:db8::1/64", 64, EINVAL, EINVAL}, |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 824 | }; |
| 825 | |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 826 | for (size_t i = 0; i < std::size(kTestData); i++) { |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 827 | const auto &td = kTestData[i]; |
| 828 | |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 829 | SCOPED_TRACE(String8::format("Offending IP address %s/%d", td.addrString, td.prefixLength)); |
| 830 | |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 831 | // [1.a] Add the address. |
| 832 | binder::Status status = mNetd->interfaceAddAddress( |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 833 | sTun.name(), td.addrString, td.prefixLength); |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 834 | if (td.expectAddResult == 0) { |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 835 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 836 | } else { |
| 837 | ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 838 | ASSERT_EQ(td.expectAddResult, status.serviceSpecificErrorCode()); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | // [1.b] Verify the addition meets the expectation. |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 842 | if (td.expectAddResult == 0) { |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 843 | EXPECT_TRUE(interfaceHasAddress(sTun.name(), td.addrString, td.prefixLength)); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 844 | } else { |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 845 | EXPECT_FALSE(interfaceHasAddress(sTun.name(), td.addrString, -1)); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | // [2.a] Try to remove the address. If it was not previously added, removing it fails. |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 849 | status = mNetd->interfaceDelAddress(sTun.name(), td.addrString, td.prefixLength); |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 850 | if (td.expectRemoveResult == 0) { |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 851 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 852 | } else { |
| 853 | ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
Lorenzo Colitti | 0111898 | 2019-12-02 13:19:44 +0900 | [diff] [blame] | 854 | ASSERT_EQ(td.expectRemoveResult, status.serviceSpecificErrorCode()); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | // [2.b] No matter what, the address should not be present. |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 858 | EXPECT_FALSE(interfaceHasAddress(sTun.name(), td.addrString, -1)); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 859 | } |
Lorenzo Colitti | 9ae1771 | 2019-12-02 17:28:49 +0900 | [diff] [blame] | 860 | |
| 861 | // Check that netlink errors are returned correctly. |
| 862 | // We do this by attempting to create an IPv6 address on an interface that has IPv6 disabled, |
| 863 | // which returns EACCES. |
| 864 | TunInterface tun; |
| 865 | ASSERT_EQ(0, tun.init()); |
| 866 | binder::Status status = |
| 867 | mNetd->setProcSysNet(INetd::IPV6, INetd::CONF, tun.name(), "disable_ipv6", "1"); |
| 868 | ASSERT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 869 | status = mNetd->interfaceAddAddress(tun.name(), "2001:db8::1", 64); |
| 870 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 871 | EXPECT_EQ(EACCES, status.serviceSpecificErrorCode()); |
| 872 | tun.destroy(); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 873 | } |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 874 | |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 875 | TEST_F(BinderTest, GetProcSysNet) { |
| 876 | const char LOOPBACK[] = "lo"; |
| 877 | static const struct { |
| 878 | const int ipversion; |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 879 | const int which; |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 880 | const char* ifname; |
| 881 | const char* parameter; |
| 882 | const char* expectedValue; |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 883 | const int expectedReturnCode; |
| 884 | } kTestData[] = { |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 885 | {INetd::IPV4, INetd::CONF, LOOPBACK, "arp_ignore", "0", 0}, |
| 886 | {-1, INetd::CONF, sTun.name().c_str(), "arp_ignore", nullptr, EAFNOSUPPORT}, |
| 887 | {INetd::IPV4, -1, sTun.name().c_str(), "arp_ignore", nullptr, EINVAL}, |
| 888 | {INetd::IPV4, INetd::CONF, "..", "conf/lo/arp_ignore", nullptr, EINVAL}, |
| 889 | {INetd::IPV4, INetd::CONF, ".", "lo/arp_ignore", nullptr, EINVAL}, |
| 890 | {INetd::IPV4, INetd::CONF, sTun.name().c_str(), "../all/arp_ignore", nullptr, EINVAL}, |
| 891 | {INetd::IPV6, INetd::NEIGH, LOOPBACK, "ucast_solicit", "3", 0}, |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 892 | }; |
| 893 | |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 894 | for (size_t i = 0; i < std::size(kTestData); i++) { |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 895 | const auto& td = kTestData[i]; |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 896 | |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 897 | std::string value; |
| 898 | const binder::Status status = |
| 899 | mNetd->getProcSysNet(td.ipversion, td.which, td.ifname, td.parameter, &value); |
| 900 | |
| 901 | if (td.expectedReturnCode == 0) { |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 902 | SCOPED_TRACE(String8::format("test case %zu should have passed", i)); |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 903 | EXPECT_EQ(0, status.exceptionCode()); |
| 904 | EXPECT_EQ(0, status.serviceSpecificErrorCode()); |
| 905 | EXPECT_EQ(td.expectedValue, value); |
| 906 | } else { |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 907 | SCOPED_TRACE(String8::format("test case %zu should have failed", i)); |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 908 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 909 | EXPECT_EQ(td.expectedReturnCode, status.serviceSpecificErrorCode()); |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | TEST_F(BinderTest, SetProcSysNet) { |
| 915 | static const struct { |
| 916 | const int ipversion; |
| 917 | const int which; |
| 918 | const char* ifname; |
| 919 | const char* parameter; |
| 920 | const char* value; |
| 921 | const int expectedReturnCode; |
| 922 | } kTestData[] = { |
| 923 | {INetd::IPV4, INetd::CONF, sTun.name().c_str(), "arp_ignore", "1", 0}, |
| 924 | {-1, INetd::CONF, sTun.name().c_str(), "arp_ignore", "1", EAFNOSUPPORT}, |
| 925 | {INetd::IPV4, -1, sTun.name().c_str(), "arp_ignore", "1", EINVAL}, |
| 926 | {INetd::IPV4, INetd::CONF, "..", "conf/lo/arp_ignore", "1", EINVAL}, |
| 927 | {INetd::IPV4, INetd::CONF, ".", "lo/arp_ignore", "1", EINVAL}, |
| 928 | {INetd::IPV4, INetd::CONF, sTun.name().c_str(), "../all/arp_ignore", "1", EINVAL}, |
| 929 | {INetd::IPV6, INetd::NEIGH, sTun.name().c_str(), "ucast_solicit", "7", 0}, |
| 930 | }; |
| 931 | |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 932 | for (size_t i = 0; i < std::size(kTestData); i++) { |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 933 | const auto& td = kTestData[i]; |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 934 | const binder::Status status = |
| 935 | mNetd->setProcSysNet(td.ipversion, td.which, td.ifname, td.parameter, td.value); |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 936 | |
| 937 | if (td.expectedReturnCode == 0) { |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 938 | SCOPED_TRACE(String8::format("test case %zu should have passed", i)); |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 939 | EXPECT_EQ(0, status.exceptionCode()); |
| 940 | EXPECT_EQ(0, status.serviceSpecificErrorCode()); |
| 941 | } else { |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 942 | SCOPED_TRACE(String8::format("test case %zu should have failed", i)); |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 943 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 944 | EXPECT_EQ(td.expectedReturnCode, status.serviceSpecificErrorCode()); |
| 945 | } |
| 946 | } |
| 947 | } |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 948 | |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 949 | TEST_F(BinderTest, GetSetProcSysNet) { |
| 950 | const int ipversion = INetd::IPV6; |
| 951 | const int category = INetd::NEIGH; |
| 952 | const std::string& tun = sTun.name(); |
| 953 | const std::string parameter("ucast_solicit"); |
| 954 | |
| 955 | std::string value{}; |
| 956 | EXPECT_TRUE(mNetd->getProcSysNet(ipversion, category, tun, parameter, &value).isOk()); |
Maciej Żenczykowski | 7b789b9 | 2019-04-09 15:55:06 -0700 | [diff] [blame] | 957 | ASSERT_FALSE(value.empty()); |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 958 | const int ival = std::stoi(value); |
| 959 | EXPECT_GT(ival, 0); |
| 960 | // Try doubling the parameter value (always best!). |
| 961 | EXPECT_TRUE(mNetd->setProcSysNet(ipversion, category, tun, parameter, std::to_string(2 * ival)) |
| 962 | .isOk()); |
| 963 | EXPECT_TRUE(mNetd->getProcSysNet(ipversion, category, tun, parameter, &value).isOk()); |
| 964 | EXPECT_EQ(2 * ival, std::stoi(value)); |
| 965 | // Try resetting the parameter. |
| 966 | EXPECT_TRUE(mNetd->setProcSysNet(ipversion, category, tun, parameter, std::to_string(ival)) |
| 967 | .isOk()); |
| 968 | EXPECT_TRUE(mNetd->getProcSysNet(ipversion, category, tun, parameter, &value).isOk()); |
| 969 | EXPECT_EQ(ival, std::stoi(value)); |
| 970 | } |
| 971 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 972 | namespace { |
| 973 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 974 | void expectNoTestCounterRules() { |
| 975 | for (const auto& binary : { IPTABLES_PATH, IP6TABLES_PATH }) { |
| 976 | std::string command = StringPrintf("%s -w -nvL tetherctrl_counters", binary); |
| 977 | std::string allRules = Join(runCommand(command), "\n"); |
| 978 | EXPECT_EQ(std::string::npos, allRules.find("netdtest_")); |
| 979 | } |
| 980 | } |
| 981 | |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 982 | void addTetherCounterValues(const char* path, const std::string& if1, const std::string& if2, |
| 983 | int byte, int pkt) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 984 | runCommand(StringPrintf("%s -w -A tetherctrl_counters -i %s -o %s -j RETURN -c %d %d", |
| 985 | path, if1.c_str(), if2.c_str(), pkt, byte)); |
| 986 | } |
| 987 | |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 988 | void delTetherCounterValues(const char* path, const std::string& if1, const std::string& if2) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 989 | runCommand(StringPrintf("%s -w -D tetherctrl_counters -i %s -o %s -j RETURN", |
| 990 | path, if1.c_str(), if2.c_str())); |
| 991 | runCommand(StringPrintf("%s -w -D tetherctrl_counters -i %s -o %s -j RETURN", |
| 992 | path, if2.c_str(), if1.c_str())); |
| 993 | } |
| 994 | |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 995 | std::vector<int64_t> getStatsVectorByIf(const std::vector<TetherStatsParcel>& statsVec, |
| 996 | const std::string& iface) { |
| 997 | for (auto& stats : statsVec) { |
| 998 | if (stats.iface == iface) { |
| 999 | return {stats.rxBytes, stats.rxPackets, stats.txBytes, stats.txPackets}; |
| 1000 | } |
| 1001 | } |
| 1002 | return {}; |
| 1003 | } |
| 1004 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 1005 | } // namespace |
| 1006 | |
| 1007 | TEST_F(BinderTest, TetherGetStats) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1008 | expectNoTestCounterRules(); |
| 1009 | |
| 1010 | // TODO: fold this into more comprehensive tests once we have binder RPCs for enabling and |
| 1011 | // disabling tethering. We don't check the return value because these commands will fail if |
| 1012 | // tethering is already enabled. |
| 1013 | runCommand(StringPrintf("%s -w -N tetherctrl_counters", IPTABLES_PATH)); |
| 1014 | runCommand(StringPrintf("%s -w -N tetherctrl_counters", IP6TABLES_PATH)); |
| 1015 | |
| 1016 | std::string intIface1 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
| 1017 | std::string intIface2 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
| 1018 | std::string intIface3 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
Luke Huang | 0f91cdc | 2019-05-29 17:55:51 +0800 | [diff] [blame] | 1019 | |
| 1020 | // Ensure we won't use the same interface name, otherwise the test will fail. |
| 1021 | u_int32_t rNumber = arc4random_uniform(10000); |
| 1022 | std::string extIface1 = StringPrintf("netdtest_%u", rNumber); |
| 1023 | std::string extIface2 = StringPrintf("netdtest_%u", rNumber + 1); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1024 | |
| 1025 | addTetherCounterValues(IPTABLES_PATH, intIface1, extIface1, 123, 111); |
| 1026 | addTetherCounterValues(IP6TABLES_PATH, intIface1, extIface1, 456, 10); |
| 1027 | addTetherCounterValues(IPTABLES_PATH, extIface1, intIface1, 321, 222); |
| 1028 | addTetherCounterValues(IP6TABLES_PATH, extIface1, intIface1, 654, 20); |
| 1029 | // RX is from external to internal, and TX is from internal to external. |
| 1030 | // So rxBytes is 321 + 654 = 975, txBytes is 123 + 456 = 579, etc. |
| 1031 | std::vector<int64_t> expected1 = { 975, 242, 579, 121 }; |
| 1032 | |
| 1033 | addTetherCounterValues(IPTABLES_PATH, intIface2, extIface2, 1000, 333); |
| 1034 | addTetherCounterValues(IP6TABLES_PATH, intIface2, extIface2, 3000, 30); |
| 1035 | |
| 1036 | addTetherCounterValues(IPTABLES_PATH, extIface2, intIface2, 2000, 444); |
| 1037 | addTetherCounterValues(IP6TABLES_PATH, extIface2, intIface2, 4000, 40); |
| 1038 | |
| 1039 | addTetherCounterValues(IP6TABLES_PATH, intIface3, extIface2, 1000, 25); |
| 1040 | addTetherCounterValues(IP6TABLES_PATH, extIface2, intIface3, 2000, 35); |
| 1041 | std::vector<int64_t> expected2 = { 8000, 519, 5000, 388 }; |
| 1042 | |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 1043 | std::vector<TetherStatsParcel> statsVec; |
| 1044 | binder::Status status = mNetd->tetherGetStats(&statsVec); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1045 | EXPECT_TRUE(status.isOk()) << "Getting tethering stats failed: " << status; |
| 1046 | |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 1047 | EXPECT_EQ(expected1, getStatsVectorByIf(statsVec, extIface1)); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1048 | |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 1049 | EXPECT_EQ(expected2, getStatsVectorByIf(statsVec, extIface2)); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 1050 | |
| 1051 | for (const auto& path : { IPTABLES_PATH, IP6TABLES_PATH }) { |
| 1052 | delTetherCounterValues(path, intIface1, extIface1); |
| 1053 | delTetherCounterValues(path, intIface2, extIface2); |
| 1054 | if (path == IP6TABLES_PATH) { |
| 1055 | delTetherCounterValues(path, intIface3, extIface2); |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | expectNoTestCounterRules(); |
| 1060 | } |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 1061 | |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1062 | namespace { |
| 1063 | |
Luke Huang | a521107 | 2018-08-01 23:36:29 +0800 | [diff] [blame] | 1064 | constexpr char IDLETIMER_RAW_PREROUTING[] = "idletimer_raw_PREROUTING"; |
| 1065 | constexpr char IDLETIMER_MANGLE_POSTROUTING[] = "idletimer_mangle_POSTROUTING"; |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1066 | |
| 1067 | static std::vector<std::string> listIptablesRuleByTable(const char* binary, const char* table, |
| 1068 | const char* chainName) { |
| 1069 | std::string command = StringPrintf("%s -t %s -w -n -v -L %s", binary, table, chainName); |
| 1070 | return runCommand(command); |
| 1071 | } |
| 1072 | |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 1073 | // TODO: It is a duplicate function, need to remove it |
Luke Huang | a521107 | 2018-08-01 23:36:29 +0800 | [diff] [blame] | 1074 | bool iptablesIdleTimerInterfaceRuleExists(const char* binary, const char* chainName, |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1075 | const std::string& expectedInterface, |
| 1076 | const std::string& expectedRule, const char* table) { |
| 1077 | std::vector<std::string> rules = listIptablesRuleByTable(binary, table, chainName); |
| 1078 | for (const auto& rule : rules) { |
| 1079 | if (rule.find(expectedInterface) != std::string::npos) { |
| 1080 | if (rule.find(expectedRule) != std::string::npos) { |
| 1081 | return true; |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | return false; |
| 1086 | } |
| 1087 | |
| 1088 | void expectIdletimerInterfaceRuleExists(const std::string& ifname, int timeout, |
Luke Huang | a521107 | 2018-08-01 23:36:29 +0800 | [diff] [blame] | 1089 | const std::string& classLabel) { |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1090 | std::string IdletimerRule = |
Luke Huang | a521107 | 2018-08-01 23:36:29 +0800 | [diff] [blame] | 1091 | StringPrintf("timeout:%u label:%s send_nl_msg:1", timeout, classLabel.c_str()); |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1092 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
Luke Huang | a521107 | 2018-08-01 23:36:29 +0800 | [diff] [blame] | 1093 | EXPECT_TRUE(iptablesIdleTimerInterfaceRuleExists(binary, IDLETIMER_RAW_PREROUTING, ifname, |
| 1094 | IdletimerRule, RAW_TABLE)); |
| 1095 | EXPECT_TRUE(iptablesIdleTimerInterfaceRuleExists(binary, IDLETIMER_MANGLE_POSTROUTING, |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1096 | ifname, IdletimerRule, MANGLE_TABLE)); |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | void expectIdletimerInterfaceRuleNotExists(const std::string& ifname, int timeout, |
Luke Huang | a521107 | 2018-08-01 23:36:29 +0800 | [diff] [blame] | 1101 | const std::string& classLabel) { |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1102 | std::string IdletimerRule = |
Luke Huang | a521107 | 2018-08-01 23:36:29 +0800 | [diff] [blame] | 1103 | StringPrintf("timeout:%u label:%s send_nl_msg:1", timeout, classLabel.c_str()); |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1104 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
Luke Huang | a521107 | 2018-08-01 23:36:29 +0800 | [diff] [blame] | 1105 | EXPECT_FALSE(iptablesIdleTimerInterfaceRuleExists(binary, IDLETIMER_RAW_PREROUTING, ifname, |
| 1106 | IdletimerRule, RAW_TABLE)); |
| 1107 | EXPECT_FALSE(iptablesIdleTimerInterfaceRuleExists(binary, IDLETIMER_MANGLE_POSTROUTING, |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1108 | ifname, IdletimerRule, MANGLE_TABLE)); |
| 1109 | } |
| 1110 | } |
| 1111 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 1112 | } // namespace |
| 1113 | |
| 1114 | TEST_F(BinderTest, IdletimerAddRemoveInterface) { |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 1115 | // TODO: We will get error in if expectIdletimerInterfaceRuleNotExists if there are the same |
| 1116 | // rule in the table. Because we only check the result after calling remove function. We might |
| 1117 | // check the actual rule which is removed by our function (maybe compare the results between |
| 1118 | // calling function before and after) |
| 1119 | binder::Status status; |
| 1120 | const struct TestData { |
| 1121 | const std::string ifname; |
| 1122 | int32_t timeout; |
| 1123 | const std::string classLabel; |
| 1124 | } idleTestData[] = { |
| 1125 | {"wlan0", 1234, "happyday"}, |
| 1126 | {"rmnet_data0", 4567, "friday"}, |
| 1127 | }; |
| 1128 | for (const auto& td : idleTestData) { |
| 1129 | status = mNetd->idletimerAddInterface(td.ifname, td.timeout, td.classLabel); |
| 1130 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1131 | expectIdletimerInterfaceRuleExists(td.ifname, td.timeout, td.classLabel); |
| 1132 | |
| 1133 | status = mNetd->idletimerRemoveInterface(td.ifname, td.timeout, td.classLabel); |
| 1134 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1135 | expectIdletimerInterfaceRuleNotExists(td.ifname, td.timeout, td.classLabel); |
| 1136 | } |
| 1137 | } |
| 1138 | |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 1139 | namespace { |
| 1140 | |
| 1141 | constexpr char STRICT_OUTPUT[] = "st_OUTPUT"; |
| 1142 | constexpr char STRICT_CLEAR_CAUGHT[] = "st_clear_caught"; |
| 1143 | |
| 1144 | void expectStrictSetUidAccept(const int uid) { |
| 1145 | std::string uidRule = StringPrintf("owner UID match %u", uid); |
| 1146 | std::string perUidChain = StringPrintf("st_clear_caught_%u", uid); |
| 1147 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
Greg Kaiser | 46a1d53 | 2019-03-26 12:10:58 -0700 | [diff] [blame] | 1148 | EXPECT_FALSE(iptablesRuleExists(binary, STRICT_OUTPUT, uidRule)); |
| 1149 | EXPECT_FALSE(iptablesRuleExists(binary, STRICT_CLEAR_CAUGHT, uidRule)); |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 1150 | EXPECT_EQ(0, iptablesRuleLineLength(binary, perUidChain.c_str())); |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | void expectStrictSetUidLog(const int uid) { |
| 1155 | static const char logRule[] = "st_penalty_log all"; |
| 1156 | std::string uidRule = StringPrintf("owner UID match %u", uid); |
| 1157 | std::string perUidChain = StringPrintf("st_clear_caught_%u", uid); |
| 1158 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
Greg Kaiser | 46a1d53 | 2019-03-26 12:10:58 -0700 | [diff] [blame] | 1159 | EXPECT_TRUE(iptablesRuleExists(binary, STRICT_OUTPUT, uidRule)); |
| 1160 | EXPECT_TRUE(iptablesRuleExists(binary, STRICT_CLEAR_CAUGHT, uidRule)); |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 1161 | EXPECT_TRUE(iptablesRuleExists(binary, perUidChain.c_str(), logRule)); |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | void expectStrictSetUidReject(const int uid) { |
| 1166 | static const char rejectRule[] = "st_penalty_reject all"; |
| 1167 | std::string uidRule = StringPrintf("owner UID match %u", uid); |
| 1168 | std::string perUidChain = StringPrintf("st_clear_caught_%u", uid); |
| 1169 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
Greg Kaiser | 46a1d53 | 2019-03-26 12:10:58 -0700 | [diff] [blame] | 1170 | EXPECT_TRUE(iptablesRuleExists(binary, STRICT_OUTPUT, uidRule)); |
| 1171 | EXPECT_TRUE(iptablesRuleExists(binary, STRICT_CLEAR_CAUGHT, uidRule)); |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 1172 | EXPECT_TRUE(iptablesRuleExists(binary, perUidChain.c_str(), rejectRule)); |
| 1173 | } |
| 1174 | } |
| 1175 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 1176 | } // namespace |
| 1177 | |
| 1178 | TEST_F(BinderTest, StrictSetUidCleartextPenalty) { |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 1179 | binder::Status status; |
| 1180 | int32_t uid = randomUid(); |
| 1181 | |
| 1182 | // setUidCleartextPenalty Policy:Log with randomUid |
| 1183 | status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_LOG); |
| 1184 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1185 | expectStrictSetUidLog(uid); |
| 1186 | |
| 1187 | // setUidCleartextPenalty Policy:Accept with randomUid |
| 1188 | status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_ACCEPT); |
| 1189 | expectStrictSetUidAccept(uid); |
| 1190 | |
| 1191 | // setUidCleartextPenalty Policy:Reject with randomUid |
| 1192 | status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_REJECT); |
| 1193 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1194 | expectStrictSetUidReject(uid); |
| 1195 | |
| 1196 | // setUidCleartextPenalty Policy:Accept with randomUid |
| 1197 | status = mNetd->strictUidCleartextPenalty(uid, INetd::PENALTY_POLICY_ACCEPT); |
| 1198 | expectStrictSetUidAccept(uid); |
| 1199 | |
| 1200 | // test wrong policy |
| 1201 | int32_t wrongPolicy = -123; |
| 1202 | status = mNetd->strictUidCleartextPenalty(uid, wrongPolicy); |
| 1203 | EXPECT_EQ(EINVAL, status.serviceSpecificErrorCode()); |
| 1204 | } |
| 1205 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 1206 | namespace { |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 1207 | |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1208 | std::vector<std::string> tryToFindProcesses(const std::string& processName, uint32_t maxTries = 1, |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 1209 | uint32_t intervalMs = 50) { |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1210 | // Output looks like:(clatd) |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 1211 | // clat 4963 850 1 12:16:51 ? 00:00:00 clatd-netd10a88 -i netd10a88 ... |
| 1212 | // ... |
| 1213 | // root 5221 5219 0 12:18:12 ? 00:00:00 sh -c ps -Af | grep ' clatd-netdcc1a0' |
| 1214 | |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1215 | // (dnsmasq) |
| 1216 | // dns_tether 4620 792 0 16:51:28 ? 00:00:00 dnsmasq --keep-in-foreground ... |
| 1217 | |
| 1218 | if (maxTries == 0) return {}; |
| 1219 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 1220 | std::string cmd = StringPrintf("ps -Af | grep '[0-9] %s'", processName.c_str()); |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1221 | std::vector<std::string> result; |
| 1222 | for (uint32_t run = 1;;) { |
Greg Kaiser | 46a1d53 | 2019-03-26 12:10:58 -0700 | [diff] [blame] | 1223 | result = runCommand(cmd); |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1224 | if (result.size() || ++run > maxTries) { |
| 1225 | break; |
| 1226 | } |
| 1227 | |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 1228 | usleep(intervalMs * 1000); |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1229 | } |
| 1230 | return result; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 1231 | } |
| 1232 | |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1233 | void expectProcessExists(const std::string& processName) { |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 1234 | EXPECT_EQ(1U, tryToFindProcesses(processName, 5 /*maxTries*/).size()); |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 1237 | void expectProcessDoesNotExist(const std::string& processName) { |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1238 | EXPECT_FALSE(tryToFindProcesses(processName).size()); |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 1239 | } |
| 1240 | |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 1241 | } // namespace |
| 1242 | |
| 1243 | TEST_F(BinderTest, ClatdStartStop) { |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 1244 | binder::Status status; |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 1245 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 1246 | const std::string clatdName = StringPrintf("clatd-%s", sTun.name().c_str()); |
| 1247 | std::string clatAddress; |
| 1248 | std::string nat64Prefix = "2001:db8:cafe:f00d:1:2::/96"; |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 1249 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 1250 | // Can't start clatd on an interface that's not part of any network... |
| 1251 | status = mNetd->clatdStart(sTun.name(), nat64Prefix, &clatAddress); |
| 1252 | EXPECT_FALSE(status.isOk()); |
| 1253 | EXPECT_EQ(ENODEV, status.serviceSpecificErrorCode()); |
| 1254 | |
| 1255 | // ... so create a test physical network and add our tun to it. |
| 1256 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, INetd::PERMISSION_NONE).isOk()); |
| 1257 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 1258 | |
| 1259 | // Prefix must be 96 bits long. |
| 1260 | status = mNetd->clatdStart(sTun.name(), "2001:db8:cafe:f00d::/64", &clatAddress); |
| 1261 | EXPECT_FALSE(status.isOk()); |
| 1262 | EXPECT_EQ(EINVAL, status.serviceSpecificErrorCode()); |
| 1263 | |
| 1264 | // Can't start clatd unless there's a default route... |
| 1265 | status = mNetd->clatdStart(sTun.name(), nat64Prefix, &clatAddress); |
| 1266 | EXPECT_FALSE(status.isOk()); |
| 1267 | EXPECT_EQ(EADDRNOTAVAIL, status.serviceSpecificErrorCode()); |
| 1268 | |
| 1269 | // so add a default route. |
| 1270 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID1, sTun.name(), "::/0", "").isOk()); |
| 1271 | |
| 1272 | // Can't start clatd unless there's a global address... |
| 1273 | status = mNetd->clatdStart(sTun.name(), nat64Prefix, &clatAddress); |
| 1274 | EXPECT_FALSE(status.isOk()); |
| 1275 | EXPECT_EQ(EADDRNOTAVAIL, status.serviceSpecificErrorCode()); |
| 1276 | |
| 1277 | // ... so add a global address. |
| 1278 | const std::string v6 = "2001:db8:1:2:f076:ae99:124e:aa99"; |
Lorenzo Colitti | 8a9f1ad | 2019-02-26 00:30:18 +0900 | [diff] [blame] | 1279 | EXPECT_EQ(0, sTun.addAddress(v6.c_str(), 64)); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 1280 | |
| 1281 | // Now expect clatd to start successfully. |
| 1282 | status = mNetd->clatdStart(sTun.name(), nat64Prefix, &clatAddress); |
| 1283 | EXPECT_TRUE(status.isOk()); |
| 1284 | EXPECT_EQ(0, status.serviceSpecificErrorCode()); |
| 1285 | |
| 1286 | // Starting it again returns EBUSY. |
| 1287 | status = mNetd->clatdStart(sTun.name(), nat64Prefix, &clatAddress); |
| 1288 | EXPECT_FALSE(status.isOk()); |
| 1289 | EXPECT_EQ(EBUSY, status.serviceSpecificErrorCode()); |
| 1290 | |
Luke Huang | d167592 | 2019-03-11 17:29:27 +0800 | [diff] [blame] | 1291 | expectProcessExists(clatdName); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 1292 | |
| 1293 | // Expect clatd to stop successfully. |
| 1294 | status = mNetd->clatdStop(sTun.name()); |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 1295 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 1296 | expectProcessDoesNotExist(clatdName); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 1297 | |
| 1298 | // Stopping a clatd that doesn't exist returns ENODEV. |
| 1299 | status = mNetd->clatdStop(sTun.name()); |
| 1300 | EXPECT_FALSE(status.isOk()); |
| 1301 | EXPECT_EQ(ENODEV, status.serviceSpecificErrorCode()); |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 1302 | expectProcessDoesNotExist(clatdName); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 1303 | |
| 1304 | // Clean up. |
| 1305 | EXPECT_TRUE(mNetd->networkRemoveRoute(TEST_NETID1, sTun.name(), "::/0", "").isOk()); |
| 1306 | EXPECT_EQ(0, ifc_del_address(sTun.name().c_str(), v6.c_str(), 64)); |
| 1307 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 1308 | } |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 1309 | |
| 1310 | namespace { |
| 1311 | |
| 1312 | bool getIpfwdV4Enable() { |
| 1313 | static const char ipv4IpfwdCmd[] = "cat /proc/sys/net/ipv4/ip_forward"; |
| 1314 | std::vector<std::string> result = runCommand(ipv4IpfwdCmd); |
| 1315 | EXPECT_TRUE(!result.empty()); |
| 1316 | int v4Enable = std::stoi(result[0]); |
| 1317 | return v4Enable; |
| 1318 | } |
| 1319 | |
| 1320 | bool getIpfwdV6Enable() { |
Lorenzo Colitti | 76edb4b | 2019-05-09 11:46:45 +0900 | [diff] [blame] | 1321 | static const char ipv6IpfwdCmd[] = "cat /proc/sys/net/ipv6/conf/all/forwarding"; |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 1322 | std::vector<std::string> result = runCommand(ipv6IpfwdCmd); |
| 1323 | EXPECT_TRUE(!result.empty()); |
| 1324 | int v6Enable = std::stoi(result[0]); |
| 1325 | return v6Enable; |
| 1326 | } |
| 1327 | |
| 1328 | void expectIpfwdEnable(bool enable) { |
| 1329 | int enableIPv4 = getIpfwdV4Enable(); |
| 1330 | int enableIPv6 = getIpfwdV6Enable(); |
| 1331 | EXPECT_EQ(enable, enableIPv4); |
| 1332 | EXPECT_EQ(enable, enableIPv6); |
| 1333 | } |
| 1334 | |
Bernie Innocenti | 1bdf10d | 2018-09-10 18:46:07 +0900 | [diff] [blame] | 1335 | bool ipRuleIpfwdExists(const char* ipVersion, const std::string& ipfwdRule) { |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 1336 | std::vector<std::string> rules = listIpRules(ipVersion); |
| 1337 | for (const auto& rule : rules) { |
| 1338 | if (rule.find(ipfwdRule) != std::string::npos) { |
| 1339 | return true; |
| 1340 | } |
| 1341 | } |
| 1342 | return false; |
| 1343 | } |
| 1344 | |
| 1345 | void expectIpfwdRuleExists(const char* fromIf, const char* toIf) { |
| 1346 | std::string ipfwdRule = StringPrintf("18000:\tfrom all iif %s lookup %s ", fromIf, toIf); |
| 1347 | |
| 1348 | for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { |
| 1349 | EXPECT_TRUE(ipRuleIpfwdExists(ipVersion, ipfwdRule)); |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | void expectIpfwdRuleNotExists(const char* fromIf, const char* toIf) { |
| 1354 | std::string ipfwdRule = StringPrintf("18000:\tfrom all iif %s lookup %s ", fromIf, toIf); |
| 1355 | |
| 1356 | for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { |
| 1357 | EXPECT_FALSE(ipRuleIpfwdExists(ipVersion, ipfwdRule)); |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | } // namespace |
| 1362 | |
| 1363 | TEST_F(BinderTest, TestIpfwdEnableDisableStatusForwarding) { |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 1364 | // Get ipfwd requester list from Netd |
| 1365 | std::vector<std::string> requesterList; |
| 1366 | binder::Status status = mNetd->ipfwdGetRequesterList(&requesterList); |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 1367 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 1368 | |
| 1369 | bool ipfwdEnabled; |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 1370 | if (requesterList.size() == 0) { |
| 1371 | // No requester in Netd, ipfwd should be disabled |
| 1372 | // So add one test requester and verify |
| 1373 | status = mNetd->ipfwdEnableForwarding("TestRequester"); |
| 1374 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 1375 | |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 1376 | expectIpfwdEnable(true); |
| 1377 | status = mNetd->ipfwdEnabled(&ipfwdEnabled); |
| 1378 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1379 | EXPECT_TRUE(ipfwdEnabled); |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 1380 | |
Luke Huang | 728cf4c | 2019-03-14 19:43:02 +0800 | [diff] [blame] | 1381 | // Remove test one, verify again |
| 1382 | status = mNetd->ipfwdDisableForwarding("TestRequester"); |
| 1383 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1384 | |
| 1385 | expectIpfwdEnable(false); |
| 1386 | status = mNetd->ipfwdEnabled(&ipfwdEnabled); |
| 1387 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1388 | EXPECT_FALSE(ipfwdEnabled); |
| 1389 | } else { |
| 1390 | // Disable all requesters |
| 1391 | for (const auto& requester : requesterList) { |
| 1392 | status = mNetd->ipfwdDisableForwarding(requester); |
| 1393 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1394 | } |
| 1395 | |
| 1396 | // After disable all requester, ipfwd should be disabled |
| 1397 | expectIpfwdEnable(false); |
| 1398 | status = mNetd->ipfwdEnabled(&ipfwdEnabled); |
| 1399 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1400 | EXPECT_FALSE(ipfwdEnabled); |
| 1401 | |
| 1402 | // Enable them back |
| 1403 | for (const auto& requester : requesterList) { |
| 1404 | status = mNetd->ipfwdEnableForwarding(requester); |
| 1405 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1406 | } |
| 1407 | |
| 1408 | // ipfwd should be enabled |
| 1409 | expectIpfwdEnable(true); |
| 1410 | status = mNetd->ipfwdEnabled(&ipfwdEnabled); |
| 1411 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1412 | EXPECT_TRUE(ipfwdEnabled); |
| 1413 | } |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | TEST_F(BinderTest, TestIpfwdAddRemoveInterfaceForward) { |
Luke Huang | d1827b8 | 2019-02-15 15:03:27 +0800 | [diff] [blame] | 1417 | // Add test physical network |
| 1418 | EXPECT_TRUE( |
| 1419 | mNetd->networkCreatePhysical(TEST_NETID1, INetd::PERMISSION_NONE).isOk()); |
| 1420 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 1421 | EXPECT_TRUE( |
| 1422 | mNetd->networkCreatePhysical(TEST_NETID2, INetd::PERMISSION_NONE).isOk()); |
| 1423 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID2, sTun2.name()).isOk()); |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 1424 | |
Luke Huang | d1827b8 | 2019-02-15 15:03:27 +0800 | [diff] [blame] | 1425 | binder::Status status = |
| 1426 | mNetd->ipfwdAddInterfaceForward(sTun.name(), sTun2.name()); |
| 1427 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1428 | expectIpfwdRuleExists(sTun.name().c_str(), sTun2.name().c_str()); |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 1429 | |
Luke Huang | d1827b8 | 2019-02-15 15:03:27 +0800 | [diff] [blame] | 1430 | status = mNetd->ipfwdRemoveInterfaceForward(sTun.name(), sTun2.name()); |
| 1431 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1432 | expectIpfwdRuleNotExists(sTun.name().c_str(), sTun2.name().c_str()); |
Bernie Innocenti | 1bcc25a | 2018-08-10 17:15:00 +0900 | [diff] [blame] | 1433 | } |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 1434 | |
| 1435 | namespace { |
| 1436 | |
| 1437 | constexpr char BANDWIDTH_INPUT[] = "bw_INPUT"; |
| 1438 | constexpr char BANDWIDTH_OUTPUT[] = "bw_OUTPUT"; |
| 1439 | constexpr char BANDWIDTH_FORWARD[] = "bw_FORWARD"; |
| 1440 | constexpr char BANDWIDTH_NAUGHTY[] = "bw_penalty_box"; |
| 1441 | constexpr char BANDWIDTH_NICE[] = "bw_happy_box"; |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 1442 | constexpr char BANDWIDTH_ALERT[] = "bw_global_alert"; |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 1443 | |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 1444 | // TODO: Move iptablesTargetsExists and listIptablesRuleByTable to the top. |
| 1445 | // Use either a std::vector<std::string> of things to match, or a variadic function. |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 1446 | bool iptablesTargetsExists(const char* binary, int expectedCount, const char* table, |
| 1447 | const char* chainName, const std::string& expectedTargetA, |
| 1448 | const std::string& expectedTargetB) { |
| 1449 | std::vector<std::string> rules = listIptablesRuleByTable(binary, table, chainName); |
| 1450 | int matchCount = 0; |
| 1451 | |
| 1452 | for (const auto& rule : rules) { |
| 1453 | if (rule.find(expectedTargetA) != std::string::npos) { |
| 1454 | if (rule.find(expectedTargetB) != std::string::npos) { |
| 1455 | matchCount++; |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | return matchCount == expectedCount; |
| 1460 | } |
| 1461 | |
| 1462 | void expectXtQuotaValueEqual(const char* ifname, long quotaBytes) { |
| 1463 | std::string path = StringPrintf("/proc/net/xt_quota/%s", ifname); |
| 1464 | std::string result = ""; |
| 1465 | |
| 1466 | EXPECT_TRUE(ReadFileToString(path, &result)); |
Luke Huang | 4953ca2 | 2018-09-14 14:08:50 +0800 | [diff] [blame] | 1467 | // Quota value might be decreased while matching packets |
| 1468 | EXPECT_GE(quotaBytes, std::stol(Trim(result))); |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | void expectBandwidthInterfaceQuotaRuleExists(const char* ifname, long quotaBytes) { |
| 1472 | std::string BANDWIDTH_COSTLY_IF = StringPrintf("bw_costly_%s", ifname); |
| 1473 | std::string quotaRule = StringPrintf("quota %s", ifname); |
| 1474 | |
| 1475 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 1476 | EXPECT_TRUE(iptablesTargetsExists(binary, 1, FILTER_TABLE, BANDWIDTH_INPUT, ifname, |
| 1477 | BANDWIDTH_COSTLY_IF)); |
| 1478 | EXPECT_TRUE(iptablesTargetsExists(binary, 1, FILTER_TABLE, BANDWIDTH_OUTPUT, ifname, |
| 1479 | BANDWIDTH_COSTLY_IF)); |
| 1480 | EXPECT_TRUE(iptablesTargetsExists(binary, 2, FILTER_TABLE, BANDWIDTH_FORWARD, ifname, |
| 1481 | BANDWIDTH_COSTLY_IF)); |
| 1482 | EXPECT_TRUE(iptablesRuleExists(binary, BANDWIDTH_COSTLY_IF.c_str(), BANDWIDTH_NAUGHTY)); |
| 1483 | EXPECT_TRUE(iptablesRuleExists(binary, BANDWIDTH_COSTLY_IF.c_str(), quotaRule)); |
| 1484 | } |
| 1485 | expectXtQuotaValueEqual(ifname, quotaBytes); |
| 1486 | } |
| 1487 | |
| 1488 | void expectBandwidthInterfaceQuotaRuleDoesNotExist(const char* ifname) { |
| 1489 | std::string BANDWIDTH_COSTLY_IF = StringPrintf("bw_costly_%s", ifname); |
| 1490 | std::string quotaRule = StringPrintf("quota %s", ifname); |
| 1491 | |
| 1492 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 1493 | EXPECT_FALSE(iptablesTargetsExists(binary, 1, FILTER_TABLE, BANDWIDTH_INPUT, ifname, |
| 1494 | BANDWIDTH_COSTLY_IF)); |
| 1495 | EXPECT_FALSE(iptablesTargetsExists(binary, 1, FILTER_TABLE, BANDWIDTH_OUTPUT, ifname, |
| 1496 | BANDWIDTH_COSTLY_IF)); |
| 1497 | EXPECT_FALSE(iptablesTargetsExists(binary, 2, FILTER_TABLE, BANDWIDTH_FORWARD, ifname, |
| 1498 | BANDWIDTH_COSTLY_IF)); |
| 1499 | EXPECT_FALSE(iptablesRuleExists(binary, BANDWIDTH_COSTLY_IF.c_str(), BANDWIDTH_NAUGHTY)); |
| 1500 | EXPECT_FALSE(iptablesRuleExists(binary, BANDWIDTH_COSTLY_IF.c_str(), quotaRule)); |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | void expectBandwidthInterfaceAlertRuleExists(const char* ifname, long alertBytes) { |
| 1505 | std::string BANDWIDTH_COSTLY_IF = StringPrintf("bw_costly_%s", ifname); |
| 1506 | std::string alertRule = StringPrintf("quota %sAlert", ifname); |
| 1507 | std::string alertName = StringPrintf("%sAlert", ifname); |
| 1508 | |
| 1509 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 1510 | EXPECT_TRUE(iptablesRuleExists(binary, BANDWIDTH_COSTLY_IF.c_str(), alertRule)); |
| 1511 | } |
| 1512 | expectXtQuotaValueEqual(alertName.c_str(), alertBytes); |
| 1513 | } |
| 1514 | |
| 1515 | void expectBandwidthInterfaceAlertRuleDoesNotExist(const char* ifname) { |
| 1516 | std::string BANDWIDTH_COSTLY_IF = StringPrintf("bw_costly_%s", ifname); |
| 1517 | std::string alertRule = StringPrintf("quota %sAlert", ifname); |
| 1518 | |
| 1519 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 1520 | EXPECT_FALSE(iptablesRuleExists(binary, BANDWIDTH_COSTLY_IF.c_str(), alertRule)); |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | void expectBandwidthGlobalAlertRuleExists(long alertBytes) { |
| 1525 | static const char globalAlertRule[] = "quota globalAlert"; |
| 1526 | static const char globalAlertName[] = "globalAlert"; |
| 1527 | |
| 1528 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 1529 | EXPECT_TRUE(iptablesRuleExists(binary, BANDWIDTH_ALERT, globalAlertRule)); |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 1530 | } |
| 1531 | expectXtQuotaValueEqual(globalAlertName, alertBytes); |
| 1532 | } |
| 1533 | |
| 1534 | void expectBandwidthManipulateSpecialAppRuleExists(const char* chain, const char* target, int uid) { |
| 1535 | std::string uidRule = StringPrintf("owner UID match %u", uid); |
| 1536 | |
| 1537 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 1538 | EXPECT_TRUE(iptablesTargetsExists(binary, 1, FILTER_TABLE, chain, target, uidRule)); |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | void expectBandwidthManipulateSpecialAppRuleDoesNotExist(const char* chain, int uid) { |
| 1543 | std::string uidRule = StringPrintf("owner UID match %u", uid); |
| 1544 | |
| 1545 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 1546 | EXPECT_FALSE(iptablesRuleExists(binary, chain, uidRule)); |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | } // namespace |
| 1551 | |
| 1552 | TEST_F(BinderTest, BandwidthSetRemoveInterfaceQuota) { |
| 1553 | long testQuotaBytes = 5550; |
| 1554 | |
| 1555 | // Add test physical network |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1556 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, INetd::PERMISSION_NONE).isOk()); |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 1557 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 1558 | |
| 1559 | binder::Status status = mNetd->bandwidthSetInterfaceQuota(sTun.name(), testQuotaBytes); |
| 1560 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1561 | expectBandwidthInterfaceQuotaRuleExists(sTun.name().c_str(), testQuotaBytes); |
| 1562 | |
| 1563 | status = mNetd->bandwidthRemoveInterfaceQuota(sTun.name()); |
| 1564 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1565 | expectBandwidthInterfaceQuotaRuleDoesNotExist(sTun.name().c_str()); |
| 1566 | |
| 1567 | // Remove test physical network |
| 1568 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 1569 | } |
| 1570 | |
| 1571 | TEST_F(BinderTest, BandwidthSetRemoveInterfaceAlert) { |
| 1572 | long testAlertBytes = 373; |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 1573 | // Add test physical network |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1574 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, INetd::PERMISSION_NONE).isOk()); |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 1575 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 1576 | // Need to have a prior interface quota set to set an alert |
| 1577 | binder::Status status = mNetd->bandwidthSetInterfaceQuota(sTun.name(), testAlertBytes); |
| 1578 | status = mNetd->bandwidthSetInterfaceAlert(sTun.name(), testAlertBytes); |
| 1579 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1580 | expectBandwidthInterfaceAlertRuleExists(sTun.name().c_str(), testAlertBytes); |
| 1581 | |
| 1582 | status = mNetd->bandwidthRemoveInterfaceAlert(sTun.name()); |
| 1583 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1584 | expectBandwidthInterfaceAlertRuleDoesNotExist(sTun.name().c_str()); |
| 1585 | |
| 1586 | // Remove interface quota |
| 1587 | status = mNetd->bandwidthRemoveInterfaceQuota(sTun.name()); |
| 1588 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1589 | expectBandwidthInterfaceQuotaRuleDoesNotExist(sTun.name().c_str()); |
| 1590 | |
| 1591 | // Remove test physical network |
| 1592 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 1593 | } |
| 1594 | |
| 1595 | TEST_F(BinderTest, BandwidthSetGlobalAlert) { |
Luke Huang | 8ca0f1c | 2019-05-29 15:56:42 +0800 | [diff] [blame] | 1596 | int64_t testAlertBytes = 2097200; |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 1597 | |
| 1598 | binder::Status status = mNetd->bandwidthSetGlobalAlert(testAlertBytes); |
| 1599 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1600 | expectBandwidthGlobalAlertRuleExists(testAlertBytes); |
| 1601 | |
Luke Huang | 8ca0f1c | 2019-05-29 15:56:42 +0800 | [diff] [blame] | 1602 | testAlertBytes = 2098230; |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 1603 | status = mNetd->bandwidthSetGlobalAlert(testAlertBytes); |
| 1604 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1605 | expectBandwidthGlobalAlertRuleExists(testAlertBytes); |
| 1606 | } |
| 1607 | |
| 1608 | TEST_F(BinderTest, BandwidthManipulateSpecialApp) { |
| 1609 | SKIP_IF_BPF_SUPPORTED; |
| 1610 | |
| 1611 | int32_t uid = randomUid(); |
| 1612 | static const char targetReject[] = "REJECT"; |
| 1613 | static const char targetReturn[] = "RETURN"; |
| 1614 | |
| 1615 | // add NaughtyApp |
| 1616 | binder::Status status = mNetd->bandwidthAddNaughtyApp(uid); |
| 1617 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1618 | expectBandwidthManipulateSpecialAppRuleExists(BANDWIDTH_NAUGHTY, targetReject, uid); |
| 1619 | |
| 1620 | // remove NaughtyApp |
| 1621 | status = mNetd->bandwidthRemoveNaughtyApp(uid); |
| 1622 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1623 | expectBandwidthManipulateSpecialAppRuleDoesNotExist(BANDWIDTH_NAUGHTY, uid); |
| 1624 | |
| 1625 | // add NiceApp |
| 1626 | status = mNetd->bandwidthAddNiceApp(uid); |
| 1627 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1628 | expectBandwidthManipulateSpecialAppRuleExists(BANDWIDTH_NICE, targetReturn, uid); |
| 1629 | |
| 1630 | // remove NiceApp |
| 1631 | status = mNetd->bandwidthRemoveNiceApp(uid); |
| 1632 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1633 | expectBandwidthManipulateSpecialAppRuleDoesNotExist(BANDWIDTH_NICE, uid); |
| 1634 | } |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 1635 | |
| 1636 | namespace { |
| 1637 | |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1638 | std::string ipRouteString(const std::string& ifName, const std::string& dst, |
Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 1639 | const std::string& nextHop, const std::string& mtu) { |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1640 | std::string dstString = (dst == "0.0.0.0/0" || dst == "::/0") ? "default" : dst; |
| 1641 | |
| 1642 | if (!nextHop.empty()) { |
| 1643 | dstString += " via " + nextHop; |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1644 | } |
| 1645 | |
Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 1646 | dstString += " dev " + ifName; |
| 1647 | |
| 1648 | if (!mtu.empty()) { |
| 1649 | dstString += " proto static"; |
| 1650 | // IPv6 routes report the metric, IPv4 routes report the scope. |
| 1651 | // TODO: move away from specifying the entire string and use a regexp instead. |
| 1652 | if (dst.find(':') != std::string::npos) { |
| 1653 | dstString += " metric 1024"; |
| 1654 | } else { |
| 1655 | if (nextHop.empty()) { |
| 1656 | dstString += " scope link"; |
| 1657 | } |
| 1658 | } |
| 1659 | dstString += " mtu " + mtu; |
| 1660 | } |
| 1661 | |
| 1662 | return dstString; |
| 1663 | } |
| 1664 | |
| 1665 | void expectNetworkRouteExistsWithMtu(const char* ipVersion, const std::string& ifName, |
| 1666 | const std::string& dst, const std::string& nextHop, |
| 1667 | const std::string& mtu, const char* table) { |
| 1668 | std::string routeString = ipRouteString(ifName, dst, nextHop, mtu); |
| 1669 | EXPECT_TRUE(ipRouteExists(ipVersion, table, ipRouteString(ifName, dst, nextHop, mtu))) |
| 1670 | << "Couldn't find route to " << dst << ": '" << routeString << "' in table " << table; |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1671 | } |
| 1672 | |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1673 | void expectNetworkRouteExists(const char* ipVersion, const std::string& ifName, |
| 1674 | const std::string& dst, const std::string& nextHop, |
| 1675 | const char* table) { |
Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 1676 | expectNetworkRouteExistsWithMtu(ipVersion, ifName, dst, nextHop, "", table); |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | void expectNetworkRouteDoesNotExist(const char* ipVersion, const std::string& ifName, |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1680 | const std::string& dst, const std::string& nextHop, |
| 1681 | const char* table) { |
Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 1682 | std::string routeString = ipRouteString(ifName, dst, nextHop, ""); |
| 1683 | EXPECT_FALSE(ipRouteExists(ipVersion, table, ipRouteString(ifName, dst, nextHop, ""))) |
| 1684 | << "Found unexpected route " << routeString << " in table " << table; |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | bool ipRuleExists(const char* ipVersion, const std::string& ipRule) { |
| 1688 | std::vector<std::string> rules = listIpRules(ipVersion); |
| 1689 | for (const auto& rule : rules) { |
| 1690 | if (rule.find(ipRule) != std::string::npos) { |
| 1691 | return true; |
| 1692 | } |
| 1693 | } |
| 1694 | return false; |
| 1695 | } |
| 1696 | |
| 1697 | void expectNetworkDefaultIpRuleExists(const char* ifName) { |
| 1698 | std::string networkDefaultRule = |
| 1699 | StringPrintf("22000:\tfrom all fwmark 0x0/0xffff iif lo lookup %s", ifName); |
| 1700 | |
| 1701 | for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { |
| 1702 | EXPECT_TRUE(ipRuleExists(ipVersion, networkDefaultRule)); |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | void expectNetworkDefaultIpRuleDoesNotExist() { |
| 1707 | static const char networkDefaultRule[] = "22000:\tfrom all fwmark 0x0/0xffff iif lo"; |
| 1708 | |
| 1709 | for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { |
| 1710 | EXPECT_FALSE(ipRuleExists(ipVersion, networkDefaultRule)); |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | void expectNetworkPermissionIpRuleExists(const char* ifName, int permission) { |
| 1715 | std::string networkPermissionRule = ""; |
| 1716 | switch (permission) { |
| 1717 | case INetd::PERMISSION_NONE: |
| 1718 | networkPermissionRule = StringPrintf( |
| 1719 | "13000:\tfrom all fwmark 0x1ffdd/0x1ffff iif lo lookup %s", ifName); |
| 1720 | break; |
| 1721 | case INetd::PERMISSION_NETWORK: |
| 1722 | networkPermissionRule = StringPrintf( |
| 1723 | "13000:\tfrom all fwmark 0x5ffdd/0x5ffff iif lo lookup %s", ifName); |
| 1724 | break; |
| 1725 | case INetd::PERMISSION_SYSTEM: |
| 1726 | networkPermissionRule = StringPrintf( |
| 1727 | "13000:\tfrom all fwmark 0xdffdd/0xdffff iif lo lookup %s", ifName); |
| 1728 | break; |
| 1729 | } |
| 1730 | |
| 1731 | for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { |
| 1732 | EXPECT_TRUE(ipRuleExists(ipVersion, networkPermissionRule)); |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | // TODO: It is a duplicate function, need to remove it |
| 1737 | bool iptablesNetworkPermissionIptablesRuleExists(const char* binary, const char* chainName, |
| 1738 | const std::string& expectedInterface, |
| 1739 | const std::string& expectedRule, |
| 1740 | const char* table) { |
| 1741 | std::vector<std::string> rules = listIptablesRuleByTable(binary, table, chainName); |
| 1742 | for (const auto& rule : rules) { |
| 1743 | if (rule.find(expectedInterface) != std::string::npos) { |
| 1744 | if (rule.find(expectedRule) != std::string::npos) { |
| 1745 | return true; |
| 1746 | } |
| 1747 | } |
| 1748 | } |
| 1749 | return false; |
| 1750 | } |
| 1751 | |
| 1752 | void expectNetworkPermissionIptablesRuleExists(const char* ifName, int permission) { |
| 1753 | static const char ROUTECTRL_INPUT[] = "routectrl_mangle_INPUT"; |
| 1754 | std::string networkIncomingPacketMarkRule = ""; |
| 1755 | switch (permission) { |
| 1756 | case INetd::PERMISSION_NONE: |
| 1757 | networkIncomingPacketMarkRule = "MARK xset 0x3ffdd/0xffefffff"; |
| 1758 | break; |
| 1759 | case INetd::PERMISSION_NETWORK: |
| 1760 | networkIncomingPacketMarkRule = "MARK xset 0x7ffdd/0xffefffff"; |
| 1761 | break; |
| 1762 | case INetd::PERMISSION_SYSTEM: |
| 1763 | networkIncomingPacketMarkRule = "MARK xset 0xfffdd/0xffefffff"; |
| 1764 | break; |
| 1765 | } |
| 1766 | |
| 1767 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 1768 | EXPECT_TRUE(iptablesNetworkPermissionIptablesRuleExists( |
| 1769 | binary, ROUTECTRL_INPUT, ifName, networkIncomingPacketMarkRule, MANGLE_TABLE)); |
| 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | } // namespace |
| 1774 | |
| 1775 | TEST_F(BinderTest, NetworkAddRemoveRouteUserPermission) { |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1776 | static const struct { |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1777 | const char* ipVersion; |
| 1778 | const char* testDest; |
| 1779 | const char* testNextHop; |
| 1780 | const bool expectSuccess; |
| 1781 | } kTestData[] = { |
| 1782 | {IP_RULE_V4, "0.0.0.0/0", "", true}, |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1783 | {IP_RULE_V4, "0.0.0.0/0", "10.251.10.0", true}, |
| 1784 | {IP_RULE_V4, "10.251.0.0/16", "", true}, |
| 1785 | {IP_RULE_V4, "10.251.0.0/16", "10.251.10.0", true}, |
| 1786 | {IP_RULE_V4, "10.251.0.0/16", "fe80::/64", false}, |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1787 | {IP_RULE_V6, "::/0", "", true}, |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1788 | {IP_RULE_V6, "::/0", "2001:db8::", true}, |
| 1789 | {IP_RULE_V6, "2001:db8:cafe::/64", "2001:db8::", true}, |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1790 | {IP_RULE_V4, "fe80::/64", "0.0.0.0", false}, |
| 1791 | }; |
| 1792 | |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1793 | static const struct { |
| 1794 | const char* ipVersion; |
| 1795 | const char* testDest; |
| 1796 | const char* testNextHop; |
| 1797 | } kTestDataWithNextHop[] = { |
| 1798 | {IP_RULE_V4, "10.251.10.0/30", ""}, |
| 1799 | {IP_RULE_V6, "2001:db8::/32", ""}, |
| 1800 | }; |
| 1801 | |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1802 | static const char testTableLegacySystem[] = "legacy_system"; |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1803 | static const char testTableLegacyNetwork[] = "legacy_network"; |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1804 | const int testUid = randomUid(); |
| 1805 | const std::vector<int32_t> testUids = {testUid}; |
| 1806 | |
| 1807 | // Add test physical network |
| 1808 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, INetd::PERMISSION_NONE).isOk()); |
| 1809 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 1810 | |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1811 | // Setup route for testing nextHop |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 1812 | for (size_t i = 0; i < std::size(kTestDataWithNextHop); i++) { |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1813 | const auto& td = kTestDataWithNextHop[i]; |
| 1814 | |
| 1815 | // All route for test tun will disappear once the tun interface is deleted. |
| 1816 | binder::Status status = |
| 1817 | mNetd->networkAddRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop); |
| 1818 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1819 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 1820 | sTun.name().c_str()); |
| 1821 | |
| 1822 | // Add system permission for test uid, setup route in legacy system table. |
| 1823 | EXPECT_TRUE(mNetd->networkSetPermissionForUser(INetd::PERMISSION_SYSTEM, testUids).isOk()); |
| 1824 | |
| 1825 | status = mNetd->networkAddLegacyRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop, |
| 1826 | testUid); |
| 1827 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1828 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 1829 | testTableLegacySystem); |
| 1830 | |
| 1831 | // Remove system permission for test uid, setup route in legacy network table. |
| 1832 | EXPECT_TRUE(mNetd->networkClearPermissionForUser(testUids).isOk()); |
| 1833 | |
| 1834 | status = mNetd->networkAddLegacyRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop, |
| 1835 | testUid); |
| 1836 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1837 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 1838 | testTableLegacyNetwork); |
| 1839 | } |
| 1840 | |
Sehee Park | 8659b8d | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 1841 | for (size_t i = 0; i < std::size(kTestData); i++) { |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1842 | const auto& td = kTestData[i]; |
| 1843 | |
| 1844 | binder::Status status = |
| 1845 | mNetd->networkAddRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop); |
| 1846 | if (td.expectSuccess) { |
| 1847 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1848 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1849 | sTun.name().c_str()); |
| 1850 | } else { |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1851 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 1852 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | status = mNetd->networkRemoveRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop); |
| 1856 | if (td.expectSuccess) { |
| 1857 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1858 | expectNetworkRouteDoesNotExist(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 1859 | sTun.name().c_str()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1860 | } else { |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1861 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 1862 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1863 | } |
| 1864 | |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1865 | // Add system permission for test uid, route will be added into legacy system table. |
| 1866 | EXPECT_TRUE(mNetd->networkSetPermissionForUser(INetd::PERMISSION_SYSTEM, testUids).isOk()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1867 | |
| 1868 | status = mNetd->networkAddLegacyRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop, |
| 1869 | testUid); |
| 1870 | if (td.expectSuccess) { |
| 1871 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1872 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1873 | testTableLegacySystem); |
| 1874 | } else { |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1875 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 1876 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | status = mNetd->networkRemoveLegacyRoute(TEST_NETID1, sTun.name(), td.testDest, |
| 1880 | td.testNextHop, testUid); |
| 1881 | if (td.expectSuccess) { |
| 1882 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1883 | expectNetworkRouteDoesNotExist(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 1884 | testTableLegacySystem); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1885 | } else { |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1886 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 1887 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1888 | } |
| 1889 | |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1890 | // Remove system permission for test uid, route will be added into legacy network table. |
| 1891 | EXPECT_TRUE(mNetd->networkClearPermissionForUser(testUids).isOk()); |
| 1892 | |
| 1893 | status = mNetd->networkAddLegacyRoute(TEST_NETID1, sTun.name(), td.testDest, td.testNextHop, |
| 1894 | testUid); |
| 1895 | if (td.expectSuccess) { |
| 1896 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1897 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 1898 | testTableLegacyNetwork); |
| 1899 | } else { |
| 1900 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 1901 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
| 1902 | } |
| 1903 | |
| 1904 | status = mNetd->networkRemoveLegacyRoute(TEST_NETID1, sTun.name(), td.testDest, |
| 1905 | td.testNextHop, testUid); |
| 1906 | if (td.expectSuccess) { |
| 1907 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1908 | expectNetworkRouteDoesNotExist(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 1909 | testTableLegacyNetwork); |
| 1910 | } else { |
| 1911 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 1912 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
| 1913 | } |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1914 | } |
| 1915 | |
Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 1916 | for (size_t i = 0; i < std::size(kTestData); i++) { |
| 1917 | const auto& td = kTestData[i]; |
| 1918 | int mtu = (i % 2) ? 1480 : 1280; |
| 1919 | |
| 1920 | android::net::RouteInfoParcel parcel; |
| 1921 | parcel.ifName = sTun.name(); |
| 1922 | parcel.destination = td.testDest; |
| 1923 | parcel.nextHop = td.testNextHop; |
| 1924 | parcel.mtu = mtu; |
| 1925 | binder::Status status = mNetd->networkAddRouteParcel(TEST_NETID1, parcel); |
| 1926 | if (td.expectSuccess) { |
| 1927 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1928 | expectNetworkRouteExistsWithMtu(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 1929 | std::to_string(parcel.mtu), sTun.name().c_str()); |
| 1930 | } else { |
| 1931 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 1932 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
| 1933 | } |
| 1934 | |
| 1935 | parcel.mtu = 1337; |
| 1936 | status = mNetd->networkUpdateRouteParcel(TEST_NETID1, parcel); |
| 1937 | if (td.expectSuccess) { |
| 1938 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1939 | expectNetworkRouteExistsWithMtu(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 1940 | std::to_string(parcel.mtu), sTun.name().c_str()); |
| 1941 | } else { |
| 1942 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 1943 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
| 1944 | } |
| 1945 | |
| 1946 | status = mNetd->networkRemoveRouteParcel(TEST_NETID1, parcel); |
| 1947 | if (td.expectSuccess) { |
| 1948 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1949 | expectNetworkRouteDoesNotExist(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 1950 | sTun.name().c_str()); |
| 1951 | } else { |
| 1952 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 1953 | EXPECT_NE(0, status.serviceSpecificErrorCode()); |
| 1954 | } |
| 1955 | } |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1956 | // Remove test physical network |
| 1957 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 1958 | } |
| 1959 | |
| 1960 | TEST_F(BinderTest, NetworkPermissionDefault) { |
| 1961 | // Add test physical network |
| 1962 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, INetd::PERMISSION_NONE).isOk()); |
| 1963 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 1964 | |
Luke Huang | c3252cc | 2018-10-16 15:43:23 +0800 | [diff] [blame] | 1965 | // Get current default network NetId |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 1966 | binder::Status status = mNetd->networkGetDefault(&mStoredDefaultNetwork); |
| 1967 | ASSERT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1968 | |
| 1969 | // Test SetDefault |
| 1970 | status = mNetd->networkSetDefault(TEST_NETID1); |
| 1971 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1972 | expectNetworkDefaultIpRuleExists(sTun.name().c_str()); |
| 1973 | |
| 1974 | status = mNetd->networkClearDefault(); |
| 1975 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1976 | expectNetworkDefaultIpRuleDoesNotExist(); |
| 1977 | |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 1978 | // Set default network back |
| 1979 | status = mNetd->networkSetDefault(mStoredDefaultNetwork); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1980 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1981 | |
| 1982 | // Test SetPermission |
| 1983 | status = mNetd->networkSetPermissionForNetwork(TEST_NETID1, INetd::PERMISSION_SYSTEM); |
| 1984 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1985 | expectNetworkPermissionIpRuleExists(sTun.name().c_str(), INetd::PERMISSION_SYSTEM); |
| 1986 | expectNetworkPermissionIptablesRuleExists(sTun.name().c_str(), INetd::PERMISSION_SYSTEM); |
| 1987 | |
| 1988 | status = mNetd->networkSetPermissionForNetwork(TEST_NETID1, INetd::PERMISSION_NONE); |
| 1989 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 1990 | expectNetworkPermissionIpRuleExists(sTun.name().c_str(), INetd::PERMISSION_NONE); |
| 1991 | expectNetworkPermissionIptablesRuleExists(sTun.name().c_str(), INetd::PERMISSION_NONE); |
| 1992 | |
| 1993 | // Remove test physical network |
| 1994 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 1995 | } |
| 1996 | |
| 1997 | TEST_F(BinderTest, NetworkSetProtectAllowDeny) { |
Luke Huang | 854e299 | 2019-05-29 16:42:48 +0800 | [diff] [blame] | 1998 | binder::Status status = mNetd->networkSetProtectAllow(TEST_UID1); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1999 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2000 | bool ret = false; |
Luke Huang | 854e299 | 2019-05-29 16:42:48 +0800 | [diff] [blame] | 2001 | status = mNetd->networkCanProtect(TEST_UID1, &ret); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2002 | EXPECT_TRUE(ret); |
| 2003 | |
Luke Huang | 854e299 | 2019-05-29 16:42:48 +0800 | [diff] [blame] | 2004 | status = mNetd->networkSetProtectDeny(TEST_UID1); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2005 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Luke Huang | 854e299 | 2019-05-29 16:42:48 +0800 | [diff] [blame] | 2006 | |
| 2007 | // Clear uid permission before calling networkCanProtect to ensure |
| 2008 | // the call won't be affected by uid permission. |
| 2009 | EXPECT_TRUE(mNetd->networkClearPermissionForUser({TEST_UID1}).isOk()); |
| 2010 | |
| 2011 | status = mNetd->networkCanProtect(TEST_UID1, &ret); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 2012 | EXPECT_FALSE(ret); |
| 2013 | } |
| 2014 | |
| 2015 | namespace { |
| 2016 | |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2017 | int readIntFromPath(const std::string& path) { |
| 2018 | std::string result = ""; |
| 2019 | EXPECT_TRUE(ReadFileToString(path, &result)); |
| 2020 | return std::stoi(result); |
| 2021 | } |
| 2022 | |
| 2023 | int getTetherAcceptIPv6Ra(const std::string& ifName) { |
| 2024 | std::string path = StringPrintf("/proc/sys/net/ipv6/conf/%s/accept_ra", ifName.c_str()); |
| 2025 | return readIntFromPath(path); |
| 2026 | } |
| 2027 | |
| 2028 | bool getTetherAcceptIPv6Dad(const std::string& ifName) { |
| 2029 | std::string path = StringPrintf("/proc/sys/net/ipv6/conf/%s/accept_dad", ifName.c_str()); |
| 2030 | return readIntFromPath(path); |
| 2031 | } |
| 2032 | |
| 2033 | int getTetherIPv6DadTransmits(const std::string& ifName) { |
| 2034 | std::string path = StringPrintf("/proc/sys/net/ipv6/conf/%s/dad_transmits", ifName.c_str()); |
| 2035 | return readIntFromPath(path); |
| 2036 | } |
| 2037 | |
| 2038 | bool getTetherEnableIPv6(const std::string& ifName) { |
| 2039 | std::string path = StringPrintf("/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifName.c_str()); |
| 2040 | int disableIPv6 = readIntFromPath(path); |
| 2041 | return !disableIPv6; |
| 2042 | } |
| 2043 | |
| 2044 | bool interfaceListContains(const std::vector<std::string>& ifList, const std::string& ifName) { |
| 2045 | for (const auto& iface : ifList) { |
| 2046 | if (iface == ifName) { |
| 2047 | return true; |
| 2048 | } |
| 2049 | } |
| 2050 | return false; |
| 2051 | } |
| 2052 | |
| 2053 | void expectTetherInterfaceConfigureForIPv6Router(const std::string& ifName) { |
| 2054 | EXPECT_EQ(getTetherAcceptIPv6Ra(ifName), 0); |
| 2055 | EXPECT_FALSE(getTetherAcceptIPv6Dad(ifName)); |
| 2056 | EXPECT_EQ(getTetherIPv6DadTransmits(ifName), 0); |
| 2057 | EXPECT_TRUE(getTetherEnableIPv6(ifName)); |
| 2058 | } |
| 2059 | |
| 2060 | void expectTetherInterfaceConfigureForIPv6Client(const std::string& ifName) { |
| 2061 | EXPECT_EQ(getTetherAcceptIPv6Ra(ifName), 2); |
| 2062 | EXPECT_TRUE(getTetherAcceptIPv6Dad(ifName)); |
| 2063 | EXPECT_EQ(getTetherIPv6DadTransmits(ifName), 1); |
| 2064 | EXPECT_FALSE(getTetherEnableIPv6(ifName)); |
| 2065 | } |
| 2066 | |
| 2067 | void expectTetherInterfaceExists(const std::vector<std::string>& ifList, |
| 2068 | const std::string& ifName) { |
| 2069 | EXPECT_TRUE(interfaceListContains(ifList, ifName)); |
| 2070 | } |
| 2071 | |
| 2072 | void expectTetherInterfaceNotExists(const std::vector<std::string>& ifList, |
| 2073 | const std::string& ifName) { |
| 2074 | EXPECT_FALSE(interfaceListContains(ifList, ifName)); |
| 2075 | } |
| 2076 | |
| 2077 | void expectTetherDnsListEquals(const std::vector<std::string>& dnsList, |
| 2078 | const std::vector<std::string>& testDnsAddrs) { |
| 2079 | EXPECT_TRUE(dnsList == testDnsAddrs); |
| 2080 | } |
| 2081 | |
| 2082 | } // namespace |
| 2083 | |
| 2084 | TEST_F(BinderTest, TetherStartStopStatus) { |
| 2085 | std::vector<std::string> noDhcpRange = {}; |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 2086 | for (bool usingLegacyDnsProxy : {true, false}) { |
Chiachang Wang | 08cb211 | 2019-12-10 09:53:24 +0800 | [diff] [blame] | 2087 | android::net::TetherConfigParcel config; |
| 2088 | config.usingLegacyDnsProxy = usingLegacyDnsProxy; |
| 2089 | config.dhcpRanges = noDhcpRange; |
| 2090 | binder::Status status = mNetd->tetherStartWithConfiguration(config); |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 2091 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2092 | SCOPED_TRACE(StringPrintf("usingLegacyDnsProxy: %d", usingLegacyDnsProxy)); |
| 2093 | if (usingLegacyDnsProxy == true) { |
Lorenzo Colitti | 5c53ae5 | 2020-04-15 17:26:21 +0000 | [diff] [blame^] | 2094 | expectProcessExists(DNSMASQ); |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 2095 | } else { |
Lorenzo Colitti | 5c53ae5 | 2020-04-15 17:26:21 +0000 | [diff] [blame^] | 2096 | expectProcessDoesNotExist(DNSMASQ); |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 2097 | } |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2098 | |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 2099 | bool tetherEnabled; |
| 2100 | status = mNetd->tetherIsEnabled(&tetherEnabled); |
| 2101 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2102 | EXPECT_TRUE(tetherEnabled); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2103 | |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 2104 | status = mNetd->tetherStop(); |
| 2105 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
Lorenzo Colitti | 5c53ae5 | 2020-04-15 17:26:21 +0000 | [diff] [blame^] | 2106 | expectProcessDoesNotExist(DNSMASQ); |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2107 | |
Luke Huang | 91bd3e1 | 2019-08-20 11:33:52 +0800 | [diff] [blame] | 2108 | status = mNetd->tetherIsEnabled(&tetherEnabled); |
| 2109 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2110 | EXPECT_FALSE(tetherEnabled); |
| 2111 | } |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | TEST_F(BinderTest, TetherInterfaceAddRemoveList) { |
| 2115 | // TODO: verify if dnsmasq update interface successfully |
| 2116 | |
| 2117 | binder::Status status = mNetd->tetherInterfaceAdd(sTun.name()); |
| 2118 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2119 | expectTetherInterfaceConfigureForIPv6Router(sTun.name()); |
| 2120 | |
| 2121 | std::vector<std::string> ifList; |
| 2122 | status = mNetd->tetherInterfaceList(&ifList); |
| 2123 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2124 | expectTetherInterfaceExists(ifList, sTun.name()); |
| 2125 | |
| 2126 | status = mNetd->tetherInterfaceRemove(sTun.name()); |
| 2127 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2128 | expectTetherInterfaceConfigureForIPv6Client(sTun.name()); |
| 2129 | |
| 2130 | status = mNetd->tetherInterfaceList(&ifList); |
| 2131 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2132 | expectTetherInterfaceNotExists(ifList, sTun.name()); |
| 2133 | } |
| 2134 | |
| 2135 | TEST_F(BinderTest, TetherDnsSetList) { |
| 2136 | // TODO: verify if dnsmasq update dns successfully |
Luke Huang | 8dc1cac | 2019-03-30 16:12:31 +0800 | [diff] [blame] | 2137 | std::vector<std::string> testDnsAddrs = {"192.168.1.37", "213.137.100.3", |
| 2138 | "fe80::1%" + sTun.name()}; |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 2139 | |
| 2140 | binder::Status status = mNetd->tetherDnsSet(TEST_NETID1, testDnsAddrs); |
| 2141 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2142 | |
| 2143 | std::vector<std::string> dnsList; |
| 2144 | status = mNetd->tetherDnsList(&dnsList); |
| 2145 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2146 | expectTetherDnsListEquals(dnsList, testDnsAddrs); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | namespace { |
| 2150 | |
Lorenzo Colitti | 5c53ae5 | 2020-04-15 17:26:21 +0000 | [diff] [blame^] | 2151 | std::vector<IPAddress> findDnsSockets(SockDiag* sd, unsigned numExpected) { |
| 2152 | std::vector<IPAddress> listenAddrs; |
| 2153 | |
| 2154 | // Callback lambda that finds all IPv4 sockets with source port 53. |
| 2155 | auto findDnsSockets = [&](uint8_t /* proto */, const inet_diag_msg* msg) { |
| 2156 | // Always return false, which means do not destroy this socket. |
| 2157 | if (msg->id.idiag_sport != htons(53)) return false; |
| 2158 | IPAddress addr(*(in_addr*)msg->id.idiag_src); |
| 2159 | listenAddrs.push_back(addr); |
| 2160 | return false; |
| 2161 | }; |
| 2162 | |
| 2163 | // There is no way to know if dnsmasq has finished processing the update_interfaces command and |
| 2164 | // opened listening sockets. So, just spin a few times and return the first list of sockets |
| 2165 | // that is at least numExpected long. |
| 2166 | // Pick a relatively large timeout to avoid flaky tests. Testing suggests that 5 attempts are |
| 2167 | // sufficient for the test to pass 500 times in a row on crosshatch-eng. Pick 10 to be safe. |
| 2168 | constexpr int kMaxAttempts = 10; |
| 2169 | constexpr int kSleepMs = 100; |
| 2170 | for (int i = 0; i < kMaxAttempts; i++) { |
| 2171 | listenAddrs.clear(); |
| 2172 | EXPECT_EQ(0, sd->sendDumpRequest(IPPROTO_TCP, AF_INET, 1 << TCP_LISTEN)) |
| 2173 | << "Failed to dump sockets, attempt " << i << " of " << kMaxAttempts; |
| 2174 | sd->readDiagMsg(IPPROTO_TCP, findDnsSockets); |
| 2175 | if (listenAddrs.size() >= numExpected) { |
| 2176 | break; |
| 2177 | } |
| 2178 | usleep(kSleepMs * 1000); |
| 2179 | } |
| 2180 | |
| 2181 | return listenAddrs; |
| 2182 | } |
| 2183 | |
| 2184 | } // namespace |
| 2185 | |
| 2186 | // Checks that when starting dnsmasq on an interface that no longer exists, it doesn't attempt to |
| 2187 | // start on other interfaces instead. |
| 2188 | TEST_F(BinderTest, TetherDeletedInterface) { |
| 2189 | // Do this first so we don't need to clean up anything else if it fails. |
| 2190 | SockDiag sd; |
| 2191 | ASSERT_TRUE(sd.open()) << "Failed to open SOCK_DIAG socket"; |
| 2192 | |
| 2193 | // Create our own TunInterfaces (so we can delete them without affecting other tests), and add |
| 2194 | // IP addresses to them. They must be IPv4 because tethering an interface disables and |
| 2195 | // re-enables IPv6 on the interface, which clears all addresses. |
| 2196 | TunInterface tun1, tun2; |
| 2197 | ASSERT_EQ(0, tun1.init()); |
| 2198 | ASSERT_EQ(0, tun2.init()); |
| 2199 | |
| 2200 | // Clean up. It is safe to call TunInterface::destroy multiple times. |
| 2201 | auto guard = android::base::make_scope_guard([&] { |
| 2202 | tun1.destroy(); |
| 2203 | tun2.destroy(); |
| 2204 | mNetd->tetherStop(); |
| 2205 | mNetd->tetherInterfaceRemove(tun1.name()); |
| 2206 | mNetd->tetherInterfaceRemove(tun2.name()); |
| 2207 | }); |
| 2208 | |
| 2209 | IPAddress addr1, addr2; |
| 2210 | ASSERT_TRUE(IPAddress::forString("192.0.2.1", &addr1)); |
| 2211 | ASSERT_TRUE(IPAddress::forString("192.0.2.2", &addr2)); |
| 2212 | EXPECT_EQ(0, tun1.addAddress(addr1.toString(), 32)); |
| 2213 | EXPECT_EQ(0, tun2.addAddress(addr2.toString(), 32)); |
| 2214 | |
| 2215 | // Stop tethering. |
| 2216 | binder::Status status = mNetd->tetherStop(); |
| 2217 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2218 | |
| 2219 | // Start dnsmasq on an interface that doesn't exist. |
| 2220 | // First, tether our tun interface... |
| 2221 | status = mNetd->tetherInterfaceAdd(tun1.name()); |
| 2222 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2223 | expectTetherInterfaceConfigureForIPv6Router(tun1.name()); |
| 2224 | |
| 2225 | // ... then delete it... |
| 2226 | tun1.destroy(); |
| 2227 | |
| 2228 | // ... then start dnsmasq. |
| 2229 | android::net::TetherConfigParcel config; |
| 2230 | config.usingLegacyDnsProxy = true; |
| 2231 | config.dhcpRanges = {}; |
| 2232 | status = mNetd->tetherStartWithConfiguration(config); |
| 2233 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2234 | |
| 2235 | // Wait for dnsmasq to start. |
| 2236 | expectProcessExists(DNSMASQ); |
| 2237 | |
| 2238 | // Make sure that netd thinks the interface is tethered (even though it doesn't exist). |
| 2239 | std::vector<std::string> ifList; |
| 2240 | status = mNetd->tetherInterfaceList(&ifList); |
| 2241 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2242 | ASSERT_EQ(1U, ifList.size()); |
| 2243 | EXPECT_EQ(tun1.name(), ifList[0]); |
| 2244 | |
| 2245 | // Give dnsmasq some time to start up. |
| 2246 | usleep(200 * 1000); |
| 2247 | |
| 2248 | // Check that dnsmasq is not listening on any IP addresses. It shouldn't, because it was only |
| 2249 | // told to run on tun1, and tun1 does not exist. Ensure it stays running and doesn't listen on |
| 2250 | // any IP addresses. |
| 2251 | std::vector<IPAddress> listenAddrs = findDnsSockets(&sd, 0); |
| 2252 | EXPECT_EQ(0U, listenAddrs.size()) << "Unexpectedly found IPv4 socket(s) listening on port 53"; |
| 2253 | |
| 2254 | // Now add an interface to dnsmasq and check that we can see the sockets. This confirms that |
| 2255 | // findDnsSockets is actually able to see sockets when they exist. |
| 2256 | status = mNetd->tetherInterfaceAdd(tun2.name()); |
| 2257 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2258 | |
| 2259 | in_addr loopback = {htonl(INADDR_LOOPBACK)}; |
| 2260 | listenAddrs = findDnsSockets(&sd, 2); |
| 2261 | EXPECT_EQ(2U, listenAddrs.size()) << "Expected exactly 2 IPv4 sockets listening on port 53"; |
| 2262 | EXPECT_EQ(1, std::count(listenAddrs.begin(), listenAddrs.end(), addr2)); |
| 2263 | EXPECT_EQ(1, std::count(listenAddrs.begin(), listenAddrs.end(), IPAddress(loopback))); |
| 2264 | |
| 2265 | // Clean up. |
| 2266 | status = mNetd->tetherStop(); |
| 2267 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2268 | |
| 2269 | expectProcessDoesNotExist(DNSMASQ); |
| 2270 | |
| 2271 | status = mNetd->tetherInterfaceRemove(tun1.name()); |
| 2272 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2273 | |
| 2274 | status = mNetd->tetherInterfaceRemove(tun2.name()); |
| 2275 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2276 | } |
| 2277 | |
| 2278 | namespace { |
| 2279 | |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 2280 | constexpr char FIREWALL_INPUT[] = "fw_INPUT"; |
| 2281 | constexpr char FIREWALL_OUTPUT[] = "fw_OUTPUT"; |
| 2282 | constexpr char FIREWALL_FORWARD[] = "fw_FORWARD"; |
| 2283 | constexpr char FIREWALL_DOZABLE[] = "fw_dozable"; |
| 2284 | constexpr char FIREWALL_POWERSAVE[] = "fw_powersave"; |
| 2285 | constexpr char FIREWALL_STANDBY[] = "fw_standby"; |
| 2286 | constexpr char targetReturn[] = "RETURN"; |
| 2287 | constexpr char targetDrop[] = "DROP"; |
| 2288 | |
| 2289 | void expectFirewallWhitelistMode() { |
| 2290 | static const char dropRule[] = "DROP all"; |
| 2291 | static const char rejectRule[] = "REJECT all"; |
| 2292 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 2293 | EXPECT_TRUE(iptablesRuleExists(binary, FIREWALL_INPUT, dropRule)); |
| 2294 | EXPECT_TRUE(iptablesRuleExists(binary, FIREWALL_OUTPUT, rejectRule)); |
| 2295 | EXPECT_TRUE(iptablesRuleExists(binary, FIREWALL_FORWARD, rejectRule)); |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | void expectFirewallBlacklistMode() { |
| 2300 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 2301 | EXPECT_EQ(2, iptablesRuleLineLength(binary, FIREWALL_INPUT)); |
| 2302 | EXPECT_EQ(2, iptablesRuleLineLength(binary, FIREWALL_OUTPUT)); |
| 2303 | EXPECT_EQ(2, iptablesRuleLineLength(binary, FIREWALL_FORWARD)); |
| 2304 | } |
| 2305 | } |
| 2306 | |
| 2307 | bool iptablesFirewallInterfaceFirstRuleExists(const char* binary, const char* chainName, |
| 2308 | const std::string& expectedInterface, |
| 2309 | const std::string& expectedRule) { |
| 2310 | std::vector<std::string> rules = listIptablesRuleByTable(binary, FILTER_TABLE, chainName); |
| 2311 | // Expected rule: |
| 2312 | // Chain fw_INPUT (1 references) |
| 2313 | // pkts bytes target prot opt in out source destination |
| 2314 | // 0 0 RETURN all -- expectedInterface * 0.0.0.0/0 0.0.0.0/0 |
| 2315 | // 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 |
| 2316 | int firstRuleIndex = 2; |
| 2317 | if (rules.size() < 4) return false; |
| 2318 | if (rules[firstRuleIndex].find(expectedInterface) != std::string::npos) { |
| 2319 | if (rules[firstRuleIndex].find(expectedRule) != std::string::npos) { |
| 2320 | return true; |
| 2321 | } |
| 2322 | } |
| 2323 | return false; |
| 2324 | } |
| 2325 | |
| 2326 | // TODO: It is a duplicate function, need to remove it |
| 2327 | bool iptablesFirewallInterfaceRuleExists(const char* binary, const char* chainName, |
| 2328 | const std::string& expectedInterface, |
| 2329 | const std::string& expectedRule) { |
| 2330 | std::vector<std::string> rules = listIptablesRuleByTable(binary, FILTER_TABLE, chainName); |
| 2331 | for (const auto& rule : rules) { |
| 2332 | if (rule.find(expectedInterface) != std::string::npos) { |
| 2333 | if (rule.find(expectedRule) != std::string::npos) { |
| 2334 | return true; |
| 2335 | } |
| 2336 | } |
| 2337 | } |
| 2338 | return false; |
| 2339 | } |
| 2340 | |
| 2341 | void expectFirewallInterfaceRuleAllowExists(const std::string& ifname) { |
| 2342 | static const char returnRule[] = "RETURN all"; |
| 2343 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 2344 | EXPECT_TRUE(iptablesFirewallInterfaceFirstRuleExists(binary, FIREWALL_INPUT, ifname, |
| 2345 | returnRule)); |
| 2346 | EXPECT_TRUE(iptablesFirewallInterfaceFirstRuleExists(binary, FIREWALL_OUTPUT, ifname, |
| 2347 | returnRule)); |
| 2348 | } |
| 2349 | } |
| 2350 | |
| 2351 | void expectFireWallInterfaceRuleAllowDoesNotExist(const std::string& ifname) { |
| 2352 | static const char returnRule[] = "RETURN all"; |
| 2353 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 2354 | EXPECT_FALSE( |
| 2355 | iptablesFirewallInterfaceRuleExists(binary, FIREWALL_INPUT, ifname, returnRule)); |
| 2356 | EXPECT_FALSE( |
| 2357 | iptablesFirewallInterfaceRuleExists(binary, FIREWALL_OUTPUT, ifname, returnRule)); |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | bool iptablesFirewallUidFirstRuleExists(const char* binary, const char* chainName, |
| 2362 | const std::string& expectedTarget, |
| 2363 | const std::string& expectedRule) { |
| 2364 | std::vector<std::string> rules = listIptablesRuleByTable(binary, FILTER_TABLE, chainName); |
| 2365 | int firstRuleIndex = 2; |
| 2366 | if (rules.size() < 4) return false; |
| 2367 | if (rules[firstRuleIndex].find(expectedTarget) != std::string::npos) { |
| 2368 | if (rules[firstRuleIndex].find(expectedRule) != std::string::npos) { |
| 2369 | return true; |
| 2370 | } |
| 2371 | } |
| 2372 | return false; |
| 2373 | } |
| 2374 | |
| 2375 | bool iptablesFirewallUidLastRuleExists(const char* binary, const char* chainName, |
| 2376 | const std::string& expectedTarget, |
| 2377 | const std::string& expectedRule) { |
| 2378 | std::vector<std::string> rules = listIptablesRuleByTable(binary, FILTER_TABLE, chainName); |
| 2379 | int lastRuleIndex = rules.size() - 1; |
| 2380 | if (lastRuleIndex < 0) return false; |
| 2381 | if (rules[lastRuleIndex].find(expectedTarget) != std::string::npos) { |
| 2382 | if (rules[lastRuleIndex].find(expectedRule) != std::string::npos) { |
| 2383 | return true; |
| 2384 | } |
| 2385 | } |
| 2386 | return false; |
| 2387 | } |
| 2388 | |
| 2389 | void expectFirewallUidFirstRuleExists(const char* chainName, int32_t uid) { |
| 2390 | std::string uidRule = StringPrintf("owner UID match %u", uid); |
| 2391 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) |
| 2392 | EXPECT_TRUE(iptablesFirewallUidFirstRuleExists(binary, chainName, targetReturn, uidRule)); |
| 2393 | } |
| 2394 | |
| 2395 | void expectFirewallUidFirstRuleDoesNotExist(const char* chainName, int32_t uid) { |
| 2396 | std::string uidRule = StringPrintf("owner UID match %u", uid); |
| 2397 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) |
| 2398 | EXPECT_FALSE(iptablesFirewallUidFirstRuleExists(binary, chainName, targetReturn, uidRule)); |
| 2399 | } |
| 2400 | |
| 2401 | void expectFirewallUidLastRuleExists(const char* chainName, int32_t uid) { |
| 2402 | std::string uidRule = StringPrintf("owner UID match %u", uid); |
| 2403 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) |
| 2404 | EXPECT_TRUE(iptablesFirewallUidLastRuleExists(binary, chainName, targetDrop, uidRule)); |
| 2405 | } |
| 2406 | |
| 2407 | void expectFirewallUidLastRuleDoesNotExist(const char* chainName, int32_t uid) { |
| 2408 | std::string uidRule = StringPrintf("owner UID match %u", uid); |
| 2409 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) |
| 2410 | EXPECT_FALSE(iptablesFirewallUidLastRuleExists(binary, chainName, targetDrop, uidRule)); |
| 2411 | } |
| 2412 | |
| 2413 | bool iptablesFirewallChildChainsLastRuleExists(const char* binary, const char* chainName) { |
| 2414 | std::vector<std::string> inputRules = |
| 2415 | listIptablesRuleByTable(binary, FILTER_TABLE, FIREWALL_INPUT); |
| 2416 | std::vector<std::string> outputRules = |
| 2417 | listIptablesRuleByTable(binary, FILTER_TABLE, FIREWALL_OUTPUT); |
| 2418 | int inputLastRuleIndex = inputRules.size() - 1; |
| 2419 | int outputLastRuleIndex = outputRules.size() - 1; |
| 2420 | |
| 2421 | if (inputLastRuleIndex < 0 || outputLastRuleIndex < 0) return false; |
| 2422 | if (inputRules[inputLastRuleIndex].find(chainName) != std::string::npos) { |
| 2423 | if (outputRules[outputLastRuleIndex].find(chainName) != std::string::npos) { |
| 2424 | return true; |
| 2425 | } |
| 2426 | } |
| 2427 | return false; |
| 2428 | } |
| 2429 | |
| 2430 | void expectFirewallChildChainsLastRuleExists(const char* chainRule) { |
| 2431 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) |
| 2432 | EXPECT_TRUE(iptablesFirewallChildChainsLastRuleExists(binary, chainRule)); |
| 2433 | } |
| 2434 | |
| 2435 | void expectFirewallChildChainsLastRuleDoesNotExist(const char* chainRule) { |
| 2436 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 2437 | EXPECT_FALSE(iptablesRuleExists(binary, FIREWALL_INPUT, chainRule)); |
| 2438 | EXPECT_FALSE(iptablesRuleExists(binary, FIREWALL_OUTPUT, chainRule)); |
| 2439 | } |
| 2440 | } |
| 2441 | |
| 2442 | } // namespace |
| 2443 | |
| 2444 | TEST_F(BinderTest, FirewallSetFirewallType) { |
| 2445 | binder::Status status = mNetd->firewallSetFirewallType(INetd::FIREWALL_WHITELIST); |
| 2446 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2447 | expectFirewallWhitelistMode(); |
| 2448 | |
| 2449 | status = mNetd->firewallSetFirewallType(INetd::FIREWALL_BLACKLIST); |
| 2450 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2451 | expectFirewallBlacklistMode(); |
| 2452 | |
| 2453 | // set firewall type blacklist twice |
| 2454 | mNetd->firewallSetFirewallType(INetd::FIREWALL_BLACKLIST); |
| 2455 | status = mNetd->firewallSetFirewallType(INetd::FIREWALL_BLACKLIST); |
| 2456 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2457 | expectFirewallBlacklistMode(); |
| 2458 | |
| 2459 | // set firewall type whitelist twice |
| 2460 | mNetd->firewallSetFirewallType(INetd::FIREWALL_WHITELIST); |
| 2461 | status = mNetd->firewallSetFirewallType(INetd::FIREWALL_WHITELIST); |
| 2462 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2463 | expectFirewallWhitelistMode(); |
| 2464 | |
| 2465 | // reset firewall type to default |
| 2466 | status = mNetd->firewallSetFirewallType(INetd::FIREWALL_BLACKLIST); |
| 2467 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2468 | expectFirewallBlacklistMode(); |
| 2469 | } |
| 2470 | |
| 2471 | TEST_F(BinderTest, FirewallSetInterfaceRule) { |
| 2472 | // setinterfaceRule is not supported in BLACKLIST MODE |
| 2473 | binder::Status status = mNetd->firewallSetFirewallType(INetd::FIREWALL_BLACKLIST); |
| 2474 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2475 | |
| 2476 | status = mNetd->firewallSetInterfaceRule(sTun.name(), INetd::FIREWALL_RULE_ALLOW); |
| 2477 | EXPECT_FALSE(status.isOk()) << status.exceptionMessage(); |
| 2478 | |
| 2479 | // set WHITELIST mode first |
| 2480 | status = mNetd->firewallSetFirewallType(INetd::FIREWALL_WHITELIST); |
| 2481 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2482 | |
| 2483 | status = mNetd->firewallSetInterfaceRule(sTun.name(), INetd::FIREWALL_RULE_ALLOW); |
| 2484 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2485 | expectFirewallInterfaceRuleAllowExists(sTun.name()); |
| 2486 | |
| 2487 | status = mNetd->firewallSetInterfaceRule(sTun.name(), INetd::FIREWALL_RULE_DENY); |
| 2488 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2489 | expectFireWallInterfaceRuleAllowDoesNotExist(sTun.name()); |
| 2490 | |
| 2491 | // reset firewall mode to default |
| 2492 | status = mNetd->firewallSetFirewallType(INetd::FIREWALL_BLACKLIST); |
| 2493 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2494 | expectFirewallBlacklistMode(); |
| 2495 | } |
| 2496 | |
| 2497 | TEST_F(BinderTest, FirewallSetUidRule) { |
| 2498 | SKIP_IF_BPF_SUPPORTED; |
| 2499 | |
| 2500 | int32_t uid = randomUid(); |
| 2501 | |
| 2502 | // Doze allow |
| 2503 | binder::Status status = mNetd->firewallSetUidRule(INetd::FIREWALL_CHAIN_DOZABLE, uid, |
| 2504 | INetd::FIREWALL_RULE_ALLOW); |
| 2505 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2506 | expectFirewallUidFirstRuleExists(FIREWALL_DOZABLE, uid); |
| 2507 | |
| 2508 | // Doze deny |
| 2509 | status = mNetd->firewallSetUidRule(INetd::FIREWALL_CHAIN_DOZABLE, uid, |
| 2510 | INetd::FIREWALL_RULE_DENY); |
| 2511 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2512 | expectFirewallUidFirstRuleDoesNotExist(FIREWALL_DOZABLE, uid); |
| 2513 | |
| 2514 | // Powersave allow |
| 2515 | status = mNetd->firewallSetUidRule(INetd::FIREWALL_CHAIN_POWERSAVE, uid, |
| 2516 | INetd::FIREWALL_RULE_ALLOW); |
| 2517 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2518 | expectFirewallUidFirstRuleExists(FIREWALL_POWERSAVE, uid); |
| 2519 | |
| 2520 | // Powersave deny |
| 2521 | status = mNetd->firewallSetUidRule(INetd::FIREWALL_CHAIN_POWERSAVE, uid, |
| 2522 | INetd::FIREWALL_RULE_DENY); |
| 2523 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2524 | expectFirewallUidFirstRuleDoesNotExist(FIREWALL_POWERSAVE, uid); |
| 2525 | |
| 2526 | // Standby deny |
| 2527 | status = mNetd->firewallSetUidRule(INetd::FIREWALL_CHAIN_STANDBY, uid, |
| 2528 | INetd::FIREWALL_RULE_DENY); |
| 2529 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2530 | expectFirewallUidLastRuleExists(FIREWALL_STANDBY, uid); |
| 2531 | |
| 2532 | // Standby allow |
| 2533 | status = mNetd->firewallSetUidRule(INetd::FIREWALL_CHAIN_STANDBY, uid, |
| 2534 | INetd::FIREWALL_RULE_ALLOW); |
| 2535 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2536 | expectFirewallUidLastRuleDoesNotExist(FIREWALL_STANDBY, uid); |
| 2537 | |
| 2538 | // None deny in BLACKLIST |
| 2539 | status = mNetd->firewallSetUidRule(INetd::FIREWALL_CHAIN_NONE, uid, INetd::FIREWALL_RULE_DENY); |
| 2540 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2541 | expectFirewallUidLastRuleExists(FIREWALL_INPUT, uid); |
| 2542 | expectFirewallUidLastRuleExists(FIREWALL_OUTPUT, uid); |
| 2543 | |
| 2544 | // None allow in BLACKLIST |
| 2545 | status = mNetd->firewallSetUidRule(INetd::FIREWALL_CHAIN_NONE, uid, INetd::FIREWALL_RULE_ALLOW); |
| 2546 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2547 | expectFirewallUidLastRuleDoesNotExist(FIREWALL_INPUT, uid); |
| 2548 | expectFirewallUidLastRuleDoesNotExist(FIREWALL_OUTPUT, uid); |
| 2549 | |
| 2550 | // set firewall type whitelist twice |
| 2551 | status = mNetd->firewallSetFirewallType(INetd::FIREWALL_WHITELIST); |
| 2552 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2553 | expectFirewallWhitelistMode(); |
| 2554 | |
| 2555 | // None allow in WHITELIST |
| 2556 | status = mNetd->firewallSetUidRule(INetd::FIREWALL_CHAIN_NONE, uid, INetd::FIREWALL_RULE_ALLOW); |
| 2557 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2558 | expectFirewallUidFirstRuleExists(FIREWALL_INPUT, uid); |
| 2559 | expectFirewallUidFirstRuleExists(FIREWALL_OUTPUT, uid); |
| 2560 | |
| 2561 | // None deny in WHITELIST |
| 2562 | status = mNetd->firewallSetUidRule(INetd::FIREWALL_CHAIN_NONE, uid, INetd::FIREWALL_RULE_DENY); |
| 2563 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2564 | expectFirewallUidFirstRuleDoesNotExist(FIREWALL_INPUT, uid); |
| 2565 | expectFirewallUidFirstRuleDoesNotExist(FIREWALL_OUTPUT, uid); |
| 2566 | |
| 2567 | // reset firewall mode to default |
| 2568 | status = mNetd->firewallSetFirewallType(INetd::FIREWALL_BLACKLIST); |
| 2569 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2570 | expectFirewallBlacklistMode(); |
| 2571 | } |
| 2572 | |
| 2573 | TEST_F(BinderTest, FirewallEnableDisableChildChains) { |
| 2574 | SKIP_IF_BPF_SUPPORTED; |
| 2575 | |
| 2576 | binder::Status status = mNetd->firewallEnableChildChain(INetd::FIREWALL_CHAIN_DOZABLE, true); |
| 2577 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2578 | expectFirewallChildChainsLastRuleExists(FIREWALL_DOZABLE); |
| 2579 | |
| 2580 | status = mNetd->firewallEnableChildChain(INetd::FIREWALL_CHAIN_STANDBY, true); |
| 2581 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2582 | expectFirewallChildChainsLastRuleExists(FIREWALL_STANDBY); |
| 2583 | |
| 2584 | status = mNetd->firewallEnableChildChain(INetd::FIREWALL_CHAIN_POWERSAVE, true); |
| 2585 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2586 | expectFirewallChildChainsLastRuleExists(FIREWALL_POWERSAVE); |
| 2587 | |
| 2588 | status = mNetd->firewallEnableChildChain(INetd::FIREWALL_CHAIN_DOZABLE, false); |
| 2589 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2590 | expectFirewallChildChainsLastRuleDoesNotExist(FIREWALL_DOZABLE); |
| 2591 | |
| 2592 | status = mNetd->firewallEnableChildChain(INetd::FIREWALL_CHAIN_STANDBY, false); |
| 2593 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2594 | expectFirewallChildChainsLastRuleDoesNotExist(FIREWALL_STANDBY); |
| 2595 | |
| 2596 | status = mNetd->firewallEnableChildChain(INetd::FIREWALL_CHAIN_POWERSAVE, false); |
| 2597 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2598 | expectFirewallChildChainsLastRuleDoesNotExist(FIREWALL_POWERSAVE); |
| 2599 | } |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 2600 | |
| 2601 | namespace { |
| 2602 | |
| 2603 | std::string hwAddrToStr(unsigned char* hwaddr) { |
| 2604 | return StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x", hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3], |
| 2605 | hwaddr[4], hwaddr[5]); |
| 2606 | } |
| 2607 | |
| 2608 | int ipv4NetmaskToPrefixLength(in_addr_t mask) { |
| 2609 | int prefixLength = 0; |
| 2610 | uint32_t m = ntohl(mask); |
| 2611 | while (m & (1 << 31)) { |
| 2612 | prefixLength++; |
| 2613 | m = m << 1; |
| 2614 | } |
| 2615 | return prefixLength; |
| 2616 | } |
| 2617 | |
| 2618 | std::string toStdString(const String16& s) { |
| 2619 | return std::string(String8(s.string())); |
| 2620 | } |
| 2621 | |
| 2622 | android::netdutils::StatusOr<ifreq> ioctlByIfName(const std::string& ifName, unsigned long flag) { |
| 2623 | const auto& sys = sSyscalls.get(); |
| 2624 | auto fd = sys.socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 2625 | EXPECT_TRUE(isOk(fd.status())); |
| 2626 | |
| 2627 | struct ifreq ifr = {}; |
| 2628 | strlcpy(ifr.ifr_name, ifName.c_str(), IFNAMSIZ); |
| 2629 | |
| 2630 | return sys.ioctl(fd.value(), flag, &ifr); |
| 2631 | } |
| 2632 | |
| 2633 | std::string getInterfaceHwAddr(const std::string& ifName) { |
| 2634 | auto res = ioctlByIfName(ifName, SIOCGIFHWADDR); |
| 2635 | |
| 2636 | unsigned char hwaddr[ETH_ALEN] = {}; |
| 2637 | if (isOk(res.status())) { |
| 2638 | memcpy((void*) hwaddr, &res.value().ifr_hwaddr.sa_data, ETH_ALEN); |
| 2639 | } |
| 2640 | |
| 2641 | return hwAddrToStr(hwaddr); |
| 2642 | } |
| 2643 | |
| 2644 | int getInterfaceIPv4Prefix(const std::string& ifName) { |
| 2645 | auto res = ioctlByIfName(ifName, SIOCGIFNETMASK); |
| 2646 | |
| 2647 | int prefixLength = 0; |
| 2648 | if (isOk(res.status())) { |
| 2649 | prefixLength = ipv4NetmaskToPrefixLength( |
| 2650 | ((struct sockaddr_in*) &res.value().ifr_addr)->sin_addr.s_addr); |
| 2651 | } |
| 2652 | |
| 2653 | return prefixLength; |
| 2654 | } |
| 2655 | |
| 2656 | std::string getInterfaceIPv4Addr(const std::string& ifName) { |
| 2657 | auto res = ioctlByIfName(ifName, SIOCGIFADDR); |
| 2658 | |
| 2659 | struct in_addr addr = {}; |
| 2660 | if (isOk(res.status())) { |
| 2661 | addr.s_addr = ((struct sockaddr_in*) &res.value().ifr_addr)->sin_addr.s_addr; |
| 2662 | } |
| 2663 | |
| 2664 | return std::string(inet_ntoa(addr)); |
| 2665 | } |
| 2666 | |
| 2667 | std::vector<std::string> getInterfaceFlags(const std::string& ifName) { |
| 2668 | auto res = ioctlByIfName(ifName, SIOCGIFFLAGS); |
| 2669 | |
| 2670 | unsigned flags = 0; |
| 2671 | if (isOk(res.status())) { |
| 2672 | flags = res.value().ifr_flags; |
| 2673 | } |
| 2674 | |
| 2675 | std::vector<std::string> ifFlags; |
| 2676 | ifFlags.push_back(flags & IFF_UP ? toStdString(INetd::IF_STATE_UP()) |
| 2677 | : toStdString(INetd::IF_STATE_DOWN())); |
| 2678 | |
| 2679 | if (flags & IFF_BROADCAST) ifFlags.push_back(toStdString(INetd::IF_FLAG_BROADCAST())); |
| 2680 | if (flags & IFF_LOOPBACK) ifFlags.push_back(toStdString(INetd::IF_FLAG_LOOPBACK())); |
| 2681 | if (flags & IFF_POINTOPOINT) ifFlags.push_back(toStdString(INetd::IF_FLAG_POINTOPOINT())); |
| 2682 | if (flags & IFF_RUNNING) ifFlags.push_back(toStdString(INetd::IF_FLAG_RUNNING())); |
| 2683 | if (flags & IFF_MULTICAST) ifFlags.push_back(toStdString(INetd::IF_FLAG_MULTICAST())); |
| 2684 | |
| 2685 | return ifFlags; |
| 2686 | } |
| 2687 | |
| 2688 | bool compareListInterface(const std::vector<std::string>& interfaceList) { |
| 2689 | const auto& res = InterfaceController::getIfaceNames(); |
| 2690 | EXPECT_TRUE(isOk(res)); |
| 2691 | |
| 2692 | std::vector<std::string> resIfList; |
| 2693 | resIfList.reserve(res.value().size()); |
| 2694 | resIfList.insert(end(resIfList), begin(res.value()), end(res.value())); |
| 2695 | |
| 2696 | return resIfList == interfaceList; |
| 2697 | } |
| 2698 | |
| 2699 | int getInterfaceIPv6PrivacyExtensions(const std::string& ifName) { |
| 2700 | std::string path = StringPrintf("/proc/sys/net/ipv6/conf/%s/use_tempaddr", ifName.c_str()); |
| 2701 | return readIntFromPath(path); |
| 2702 | } |
| 2703 | |
| 2704 | bool getInterfaceEnableIPv6(const std::string& ifName) { |
| 2705 | std::string path = StringPrintf("/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifName.c_str()); |
| 2706 | |
| 2707 | int disableIPv6 = readIntFromPath(path); |
| 2708 | return !disableIPv6; |
| 2709 | } |
| 2710 | |
| 2711 | int getInterfaceMtu(const std::string& ifName) { |
| 2712 | std::string path = StringPrintf("/sys/class/net/%s/mtu", ifName.c_str()); |
| 2713 | return readIntFromPath(path); |
| 2714 | } |
| 2715 | |
| 2716 | void expectInterfaceList(const std::vector<std::string>& interfaceList) { |
| 2717 | EXPECT_TRUE(compareListInterface(interfaceList)); |
| 2718 | } |
| 2719 | |
| 2720 | void expectCurrentInterfaceConfigurationEquals(const std::string& ifName, |
| 2721 | const InterfaceConfigurationParcel& interfaceCfg) { |
| 2722 | EXPECT_EQ(getInterfaceIPv4Addr(ifName), interfaceCfg.ipv4Addr); |
| 2723 | EXPECT_EQ(getInterfaceIPv4Prefix(ifName), interfaceCfg.prefixLength); |
| 2724 | EXPECT_EQ(getInterfaceHwAddr(ifName), interfaceCfg.hwAddr); |
| 2725 | EXPECT_EQ(getInterfaceFlags(ifName), interfaceCfg.flags); |
| 2726 | } |
| 2727 | |
| 2728 | void expectCurrentInterfaceConfigurationAlmostEqual(const InterfaceConfigurationParcel& setCfg) { |
| 2729 | EXPECT_EQ(getInterfaceIPv4Addr(setCfg.ifName), setCfg.ipv4Addr); |
| 2730 | EXPECT_EQ(getInterfaceIPv4Prefix(setCfg.ifName), setCfg.prefixLength); |
| 2731 | |
| 2732 | const auto& ifFlags = getInterfaceFlags(setCfg.ifName); |
| 2733 | for (const auto& flag : setCfg.flags) { |
| 2734 | EXPECT_TRUE(std::find(ifFlags.begin(), ifFlags.end(), flag) != ifFlags.end()); |
| 2735 | } |
| 2736 | } |
| 2737 | |
| 2738 | void expectInterfaceIPv6PrivacyExtensions(const std::string& ifName, bool enable) { |
| 2739 | int v6PrivacyExtensions = getInterfaceIPv6PrivacyExtensions(ifName); |
| 2740 | EXPECT_EQ(v6PrivacyExtensions, enable ? 2 : 0); |
| 2741 | } |
| 2742 | |
| 2743 | void expectInterfaceNoAddr(const std::string& ifName) { |
| 2744 | // noAddr |
| 2745 | EXPECT_EQ(getInterfaceIPv4Addr(ifName), "0.0.0.0"); |
| 2746 | // noPrefix |
| 2747 | EXPECT_EQ(getInterfaceIPv4Prefix(ifName), 0); |
| 2748 | } |
| 2749 | |
| 2750 | void expectInterfaceEnableIPv6(const std::string& ifName, bool enable) { |
| 2751 | int enableIPv6 = getInterfaceEnableIPv6(ifName); |
| 2752 | EXPECT_EQ(enableIPv6, enable); |
| 2753 | } |
| 2754 | |
| 2755 | void expectInterfaceMtu(const std::string& ifName, const int mtu) { |
| 2756 | int mtuSize = getInterfaceMtu(ifName); |
| 2757 | EXPECT_EQ(mtu, mtuSize); |
| 2758 | } |
| 2759 | |
| 2760 | InterfaceConfigurationParcel makeInterfaceCfgParcel(const std::string& ifName, |
| 2761 | const std::string& addr, int prefixLength, |
| 2762 | const std::vector<std::string>& flags) { |
| 2763 | InterfaceConfigurationParcel cfg; |
| 2764 | cfg.ifName = ifName; |
| 2765 | cfg.hwAddr = ""; |
| 2766 | cfg.ipv4Addr = addr; |
| 2767 | cfg.prefixLength = prefixLength; |
| 2768 | cfg.flags = flags; |
| 2769 | return cfg; |
| 2770 | } |
| 2771 | |
| 2772 | void expectTunFlags(const InterfaceConfigurationParcel& interfaceCfg) { |
| 2773 | std::vector<std::string> expectedFlags = {"up", "point-to-point", "running", "multicast"}; |
| 2774 | std::vector<std::string> unexpectedFlags = {"down", "broadcast"}; |
| 2775 | |
| 2776 | for (const auto& flag : expectedFlags) { |
| 2777 | EXPECT_TRUE(std::find(interfaceCfg.flags.begin(), interfaceCfg.flags.end(), flag) != |
| 2778 | interfaceCfg.flags.end()); |
| 2779 | } |
| 2780 | |
| 2781 | for (const auto& flag : unexpectedFlags) { |
| 2782 | EXPECT_TRUE(std::find(interfaceCfg.flags.begin(), interfaceCfg.flags.end(), flag) == |
| 2783 | interfaceCfg.flags.end()); |
| 2784 | } |
| 2785 | } |
| 2786 | |
| 2787 | } // namespace |
| 2788 | |
| 2789 | TEST_F(BinderTest, InterfaceList) { |
| 2790 | std::vector<std::string> interfaceListResult; |
| 2791 | |
| 2792 | binder::Status status = mNetd->interfaceGetList(&interfaceListResult); |
| 2793 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2794 | expectInterfaceList(interfaceListResult); |
| 2795 | } |
| 2796 | |
| 2797 | TEST_F(BinderTest, InterfaceGetCfg) { |
| 2798 | InterfaceConfigurationParcel interfaceCfgResult; |
| 2799 | |
| 2800 | // Add test physical network |
| 2801 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, INetd::PERMISSION_NONE).isOk()); |
| 2802 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 2803 | |
| 2804 | binder::Status status = mNetd->interfaceGetCfg(sTun.name(), &interfaceCfgResult); |
| 2805 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2806 | expectCurrentInterfaceConfigurationEquals(sTun.name(), interfaceCfgResult); |
| 2807 | expectTunFlags(interfaceCfgResult); |
| 2808 | |
| 2809 | // Remove test physical network |
| 2810 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 2811 | } |
| 2812 | |
| 2813 | TEST_F(BinderTest, InterfaceSetCfg) { |
| 2814 | const std::string testAddr = "192.0.2.3"; |
| 2815 | const int testPrefixLength = 24; |
| 2816 | std::vector<std::string> upFlags = {"up"}; |
| 2817 | std::vector<std::string> downFlags = {"down"}; |
| 2818 | |
| 2819 | // Add test physical network |
| 2820 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, INetd::PERMISSION_NONE).isOk()); |
| 2821 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 2822 | |
| 2823 | // Set tun interface down. |
| 2824 | auto interfaceCfg = makeInterfaceCfgParcel(sTun.name(), testAddr, testPrefixLength, downFlags); |
| 2825 | binder::Status status = mNetd->interfaceSetCfg(interfaceCfg); |
| 2826 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2827 | expectCurrentInterfaceConfigurationAlmostEqual(interfaceCfg); |
| 2828 | |
| 2829 | // Set tun interface up again. |
| 2830 | interfaceCfg = makeInterfaceCfgParcel(sTun.name(), testAddr, testPrefixLength, upFlags); |
| 2831 | status = mNetd->interfaceSetCfg(interfaceCfg); |
| 2832 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2833 | status = mNetd->interfaceClearAddrs(sTun.name()); |
| 2834 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2835 | |
| 2836 | // Remove test physical network |
| 2837 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 2838 | } |
| 2839 | |
| 2840 | TEST_F(BinderTest, InterfaceSetIPv6PrivacyExtensions) { |
| 2841 | // enable |
| 2842 | binder::Status status = mNetd->interfaceSetIPv6PrivacyExtensions(sTun.name(), true); |
| 2843 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2844 | expectInterfaceIPv6PrivacyExtensions(sTun.name(), true); |
| 2845 | |
| 2846 | // disable |
| 2847 | status = mNetd->interfaceSetIPv6PrivacyExtensions(sTun.name(), false); |
| 2848 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2849 | expectInterfaceIPv6PrivacyExtensions(sTun.name(), false); |
| 2850 | } |
| 2851 | |
| 2852 | TEST_F(BinderTest, InterfaceClearAddr) { |
| 2853 | const std::string testAddr = "192.0.2.3"; |
| 2854 | const int testPrefixLength = 24; |
| 2855 | std::vector<std::string> noFlags{}; |
| 2856 | |
| 2857 | // Add test physical network |
| 2858 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, INetd::PERMISSION_NONE).isOk()); |
| 2859 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 2860 | |
| 2861 | auto interfaceCfg = makeInterfaceCfgParcel(sTun.name(), testAddr, testPrefixLength, noFlags); |
| 2862 | binder::Status status = mNetd->interfaceSetCfg(interfaceCfg); |
| 2863 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2864 | expectCurrentInterfaceConfigurationAlmostEqual(interfaceCfg); |
| 2865 | |
| 2866 | status = mNetd->interfaceClearAddrs(sTun.name()); |
| 2867 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2868 | expectInterfaceNoAddr(sTun.name()); |
| 2869 | |
| 2870 | // Remove test physical network |
| 2871 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 2872 | } |
| 2873 | |
| 2874 | TEST_F(BinderTest, InterfaceSetEnableIPv6) { |
| 2875 | // Add test physical network |
| 2876 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, INetd::PERMISSION_NONE).isOk()); |
| 2877 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 2878 | |
| 2879 | // disable |
| 2880 | binder::Status status = mNetd->interfaceSetEnableIPv6(sTun.name(), false); |
| 2881 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2882 | expectInterfaceEnableIPv6(sTun.name(), false); |
| 2883 | |
| 2884 | // enable |
| 2885 | status = mNetd->interfaceSetEnableIPv6(sTun.name(), true); |
| 2886 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2887 | expectInterfaceEnableIPv6(sTun.name(), true); |
| 2888 | |
| 2889 | // Remove test physical network |
| 2890 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 2891 | } |
| 2892 | |
| 2893 | TEST_F(BinderTest, InterfaceSetMtu) { |
| 2894 | const int testMtu = 1200; |
| 2895 | |
| 2896 | // Add test physical network |
| 2897 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, INetd::PERMISSION_NONE).isOk()); |
| 2898 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 2899 | |
| 2900 | binder::Status status = mNetd->interfaceSetMtu(sTun.name(), testMtu); |
| 2901 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2902 | expectInterfaceMtu(sTun.name(), testMtu); |
| 2903 | |
| 2904 | // Remove test physical network |
| 2905 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 2906 | } |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 2907 | |
| 2908 | namespace { |
| 2909 | |
| 2910 | constexpr const char TETHER_FORWARD[] = "tetherctrl_FORWARD"; |
| 2911 | constexpr const char TETHER_NAT_POSTROUTING[] = "tetherctrl_nat_POSTROUTING"; |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 2912 | constexpr const char TETHER_RAW_PREROUTING[] = "tetherctrl_raw_PREROUTING"; |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 2913 | constexpr const char TETHER_COUNTERS_CHAIN[] = "tetherctrl_counters"; |
| 2914 | |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 2915 | int iptablesCountRules(const char* binary, const char* table, const char* chainName) { |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 2916 | return listIptablesRuleByTable(binary, table, chainName).size(); |
| 2917 | } |
| 2918 | |
| 2919 | bool iptablesChainMatch(const char* binary, const char* table, const char* chainName, |
| 2920 | const std::vector<std::string>& targetVec) { |
| 2921 | std::vector<std::string> rules = listIptablesRuleByTable(binary, table, chainName); |
| 2922 | if (targetVec.size() != rules.size() - 2) { |
| 2923 | return false; |
| 2924 | } |
| 2925 | |
| 2926 | /* |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 2927 | * Check that the rules match. Note that this function matches substrings, not entire rules, |
| 2928 | * because otherwise rules where "pkts" or "bytes" are nonzero would not match. |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 2929 | * Skip first two lines since rules start from third line. |
| 2930 | * Chain chainName (x references) |
| 2931 | * pkts bytes target prot opt in out source destination |
| 2932 | * ... |
| 2933 | */ |
| 2934 | int rIndex = 2; |
| 2935 | for (const auto& target : targetVec) { |
| 2936 | if (rules[rIndex].find(target) == std::string::npos) { |
| 2937 | return false; |
| 2938 | } |
| 2939 | rIndex++; |
| 2940 | } |
| 2941 | return true; |
| 2942 | } |
| 2943 | |
| 2944 | void expectNatEnable(const std::string& intIf, const std::string& extIf) { |
| 2945 | std::vector<std::string> postroutingV4Match = {"MASQUERADE"}; |
| 2946 | std::vector<std::string> preroutingV4Match = {"CT helper ftp", "CT helper pptp"}; |
| 2947 | std::vector<std::string> forwardV4Match = { |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 2948 | "bw_global_alert", "state RELATED", "state INVALID", |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 2949 | StringPrintf("tetherctrl_counters all -- %s %s", intIf.c_str(), extIf.c_str()), |
| 2950 | "DROP"}; |
| 2951 | |
| 2952 | // V4 |
| 2953 | EXPECT_TRUE(iptablesChainMatch(IPTABLES_PATH, NAT_TABLE, TETHER_NAT_POSTROUTING, |
| 2954 | postroutingV4Match)); |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 2955 | EXPECT_TRUE( |
| 2956 | iptablesChainMatch(IPTABLES_PATH, RAW_TABLE, TETHER_RAW_PREROUTING, preroutingV4Match)); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 2957 | EXPECT_TRUE(iptablesChainMatch(IPTABLES_PATH, FILTER_TABLE, TETHER_FORWARD, forwardV4Match)); |
| 2958 | |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 2959 | std::vector<std::string> forwardV6Match = {"bw_global_alert", "tetherctrl_counters"}; |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 2960 | std::vector<std::string> preroutingV6Match = {"rpfilter invert"}; |
| 2961 | |
| 2962 | // V6 |
| 2963 | EXPECT_TRUE(iptablesChainMatch(IP6TABLES_PATH, FILTER_TABLE, TETHER_FORWARD, forwardV6Match)); |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 2964 | EXPECT_TRUE(iptablesChainMatch(IP6TABLES_PATH, RAW_TABLE, TETHER_RAW_PREROUTING, |
| 2965 | preroutingV6Match)); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 2966 | |
| 2967 | for (const auto& binary : {IPTABLES_PATH, IP6TABLES_PATH}) { |
| 2968 | EXPECT_TRUE(iptablesTargetsExists(binary, 2, FILTER_TABLE, TETHER_COUNTERS_CHAIN, intIf, |
| 2969 | extIf)); |
| 2970 | } |
| 2971 | } |
| 2972 | |
| 2973 | void expectNatDisable() { |
| 2974 | // It is the default DROP rule with tethering disable. |
| 2975 | // Chain tetherctrl_FORWARD (1 references) |
| 2976 | // pkts bytes target prot opt in out source destination |
| 2977 | // 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 |
| 2978 | std::vector<std::string> forwardV4Match = {"DROP"}; |
| 2979 | EXPECT_TRUE(iptablesChainMatch(IPTABLES_PATH, FILTER_TABLE, TETHER_FORWARD, forwardV4Match)); |
| 2980 | |
| 2981 | // We expect that these chains should be empty. |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 2982 | EXPECT_EQ(2, iptablesCountRules(IPTABLES_PATH, NAT_TABLE, TETHER_NAT_POSTROUTING)); |
| 2983 | EXPECT_EQ(2, iptablesCountRules(IPTABLES_PATH, RAW_TABLE, TETHER_RAW_PREROUTING)); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 2984 | |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 2985 | EXPECT_EQ(2, iptablesCountRules(IP6TABLES_PATH, FILTER_TABLE, TETHER_FORWARD)); |
| 2986 | EXPECT_EQ(2, iptablesCountRules(IP6TABLES_PATH, RAW_TABLE, TETHER_RAW_PREROUTING)); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 2987 | |
| 2988 | // Netd won't clear tether quota rule, we don't care rule in tetherctrl_counters. |
| 2989 | } |
| 2990 | |
| 2991 | } // namespace |
| 2992 | |
| 2993 | TEST_F(BinderTest, TetherForwardAddRemove) { |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 2994 | binder::Status status = mNetd->tetherAddForward(sTun.name(), sTun2.name()); |
| 2995 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 2996 | expectNatEnable(sTun.name(), sTun2.name()); |
| 2997 | |
| 2998 | status = mNetd->tetherRemoveForward(sTun.name(), sTun2.name()); |
| 2999 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3000 | expectNatDisable(); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 3001 | } |
Chenbo Feng | f5663d8 | 2018-11-08 16:10:48 -0800 | [diff] [blame] | 3002 | |
| 3003 | namespace { |
| 3004 | |
| 3005 | using TripleInt = std::array<int, 3>; |
| 3006 | |
| 3007 | TripleInt readProcFileToTripleInt(const std::string& path) { |
| 3008 | std::string valueString; |
| 3009 | int min, def, max; |
| 3010 | EXPECT_TRUE(ReadFileToString(path, &valueString)); |
| 3011 | EXPECT_EQ(3, sscanf(valueString.c_str(), "%d %d %d", &min, &def, &max)); |
| 3012 | return {min, def, max}; |
| 3013 | } |
| 3014 | |
| 3015 | void updateAndCheckTcpBuffer(sp<INetd>& netd, TripleInt& rmemValues, TripleInt& wmemValues) { |
| 3016 | std::string testRmemValues = |
| 3017 | StringPrintf("%u %u %u", rmemValues[0], rmemValues[1], rmemValues[2]); |
| 3018 | std::string testWmemValues = |
| 3019 | StringPrintf("%u %u %u", wmemValues[0], wmemValues[1], wmemValues[2]); |
| 3020 | EXPECT_TRUE(netd->setTcpRWmemorySize(testRmemValues, testWmemValues).isOk()); |
| 3021 | |
| 3022 | TripleInt newRmemValues = readProcFileToTripleInt(TCP_RMEM_PROC_FILE); |
| 3023 | TripleInt newWmemValues = readProcFileToTripleInt(TCP_WMEM_PROC_FILE); |
| 3024 | |
| 3025 | for (int i = 0; i < 3; i++) { |
| 3026 | SCOPED_TRACE(StringPrintf("tcp_mem value %d should be equal", i)); |
| 3027 | EXPECT_EQ(rmemValues[i], newRmemValues[i]); |
| 3028 | EXPECT_EQ(wmemValues[i], newWmemValues[i]); |
| 3029 | } |
| 3030 | } |
| 3031 | |
| 3032 | } // namespace |
| 3033 | |
| 3034 | TEST_F(BinderTest, TcpBufferSet) { |
| 3035 | TripleInt rmemValue = readProcFileToTripleInt(TCP_RMEM_PROC_FILE); |
| 3036 | TripleInt testRmemValue{rmemValue[0] + 42, rmemValue[1] + 42, rmemValue[2] + 42}; |
| 3037 | TripleInt wmemValue = readProcFileToTripleInt(TCP_WMEM_PROC_FILE); |
| 3038 | TripleInt testWmemValue{wmemValue[0] + 42, wmemValue[1] + 42, wmemValue[2] + 42}; |
| 3039 | |
| 3040 | updateAndCheckTcpBuffer(mNetd, testRmemValue, testWmemValue); |
| 3041 | updateAndCheckTcpBuffer(mNetd, rmemValue, wmemValue); |
| 3042 | } |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 3043 | |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 3044 | namespace { |
| 3045 | |
Chenbo Feng | bf660aa | 2019-02-26 16:12:27 -0800 | [diff] [blame] | 3046 | void checkUidsInPermissionMap(std::vector<int32_t>& uids, bool exist) { |
Maciej Żenczykowski | f879a7f | 2020-01-20 03:18:06 -0800 | [diff] [blame] | 3047 | android::bpf::BpfMap<uint32_t, uint8_t> uidPermissionMap(UID_PERMISSION_MAP_PATH); |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 3048 | for (int32_t uid : uids) { |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 3049 | android::base::Result<uint8_t> permission = uidPermissionMap.readValue(uid); |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 3050 | if (exist) { |
Bernie Innocenti | dfd5a43 | 2020-02-12 09:21:56 +0900 | [diff] [blame] | 3051 | ASSERT_RESULT_OK(permission); |
Chenbo Feng | 84f48cd | 2019-04-22 15:34:40 -0700 | [diff] [blame] | 3052 | EXPECT_EQ(INetd::PERMISSION_NONE, permission.value()); |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 3053 | } else { |
Bernie Innocenti | dfd5a43 | 2020-02-12 09:21:56 +0900 | [diff] [blame] | 3054 | ASSERT_FALSE(permission.ok()); |
Steven Moreland | a307454 | 2020-01-13 14:13:44 -0800 | [diff] [blame] | 3055 | EXPECT_EQ(ENOENT, permission.error().code()); |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 3056 | } |
| 3057 | } |
| 3058 | } |
| 3059 | |
| 3060 | } // namespace |
| 3061 | |
| 3062 | TEST_F(BinderTest, TestInternetPermission) { |
| 3063 | SKIP_IF_BPF_NOT_SUPPORTED; |
| 3064 | |
| 3065 | std::vector<int32_t> appUids = {TEST_UID1, TEST_UID2}; |
| 3066 | |
| 3067 | mNetd->trafficSetNetPermForUids(INetd::PERMISSION_INTERNET, appUids); |
Chenbo Feng | bf660aa | 2019-02-26 16:12:27 -0800 | [diff] [blame] | 3068 | checkUidsInPermissionMap(appUids, false); |
Chenbo Feng | 84f48cd | 2019-04-22 15:34:40 -0700 | [diff] [blame] | 3069 | mNetd->trafficSetNetPermForUids(INetd::PERMISSION_NONE, appUids); |
Chenbo Feng | bf660aa | 2019-02-26 16:12:27 -0800 | [diff] [blame] | 3070 | checkUidsInPermissionMap(appUids, true); |
| 3071 | mNetd->trafficSetNetPermForUids(INetd::PERMISSION_UNINSTALLED, appUids); |
| 3072 | checkUidsInPermissionMap(appUids, false); |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 3073 | } |
| 3074 | |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 3075 | TEST_F(BinderTest, UnsolEvents) { |
| 3076 | auto testUnsolService = android::net::TestUnsolService::start(); |
| 3077 | std::string oldTunName = sTun.name(); |
| 3078 | std::string newTunName = "unsolTest"; |
| 3079 | testUnsolService->tarVec.push_back(oldTunName); |
| 3080 | testUnsolService->tarVec.push_back(newTunName); |
| 3081 | auto& cv = testUnsolService->getCv(); |
| 3082 | auto& cvMutex = testUnsolService->getCvMutex(); |
| 3083 | binder::Status status = mNetd->registerUnsolicitedEventListener( |
| 3084 | android::interface_cast<android::net::INetdUnsolicitedEventListener>(testUnsolService)); |
| 3085 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3086 | |
| 3087 | // TODO: Add test for below events |
| 3088 | // StrictCleartextDetected / InterfaceDnsServersAdded |
| 3089 | // InterfaceClassActivity / QuotaLimitReached / InterfaceAddressRemoved |
| 3090 | |
| 3091 | { |
| 3092 | std::unique_lock lock(cvMutex); |
| 3093 | |
| 3094 | // Re-init test Tun, and we expect that we will get some unsol events. |
| 3095 | // Use the test Tun device name to verify if we receive its unsol events. |
| 3096 | sTun.destroy(); |
| 3097 | // Use predefined name |
| 3098 | sTun.init(newTunName); |
| 3099 | |
| 3100 | EXPECT_EQ(std::cv_status::no_timeout, cv.wait_for(lock, std::chrono::seconds(2))); |
| 3101 | } |
| 3102 | |
| 3103 | // bit mask 1101101000 |
| 3104 | // Test only covers below events currently |
| 3105 | const uint32_t kExpectedEvents = InterfaceAddressUpdated | InterfaceAdded | InterfaceRemoved | |
| 3106 | InterfaceLinkStatusChanged | RouteChanged; |
| 3107 | EXPECT_EQ(kExpectedEvents, testUnsolService->getReceived()); |
Luke Huang | cfd04b2 | 2019-03-18 15:53:21 +0800 | [diff] [blame] | 3108 | |
| 3109 | // Re-init sTun to clear predefined name |
| 3110 | sTun.destroy(); |
| 3111 | sTun.init(); |
| 3112 | } |
| 3113 | |
| 3114 | TEST_F(BinderTest, NDC) { |
| 3115 | struct Command { |
| 3116 | const std::string cmdString; |
| 3117 | const std::string expectedResult; |
| 3118 | }; |
| 3119 | |
| 3120 | // clang-format off |
| 3121 | // Do not change the commands order |
| 3122 | const Command networkCmds[] = { |
| 3123 | {StringPrintf("ndc network create %d", TEST_NETID1), |
| 3124 | "200 0 success"}, |
| 3125 | {StringPrintf("ndc network interface add %d %s", TEST_NETID1, sTun.name().c_str()), |
| 3126 | "200 0 success"}, |
| 3127 | {StringPrintf("ndc network interface remove %d %s", TEST_NETID1, sTun.name().c_str()), |
| 3128 | "200 0 success"}, |
| 3129 | {StringPrintf("ndc network interface add %d %s", TEST_NETID2, sTun.name().c_str()), |
| 3130 | "400 0 addInterfaceToNetwork() failed (Machine is not on the network)"}, |
| 3131 | {StringPrintf("ndc network destroy %d", TEST_NETID1), |
| 3132 | "200 0 success"}, |
| 3133 | }; |
| 3134 | |
| 3135 | const std::vector<Command> ipfwdCmds = { |
| 3136 | {"ndc ipfwd enable " + sTun.name(), |
| 3137 | "200 0 ipfwd operation succeeded"}, |
| 3138 | {"ndc ipfwd disable " + sTun.name(), |
| 3139 | "200 0 ipfwd operation succeeded"}, |
| 3140 | {"ndc ipfwd add lo2 lo3", |
| 3141 | "400 0 ipfwd operation failed (No such process)"}, |
| 3142 | {"ndc ipfwd add " + sTun.name() + " " + sTun2.name(), |
| 3143 | "200 0 ipfwd operation succeeded"}, |
| 3144 | {"ndc ipfwd remove " + sTun.name() + " " + sTun2.name(), |
| 3145 | "200 0 ipfwd operation succeeded"}, |
| 3146 | }; |
| 3147 | |
| 3148 | static const struct { |
| 3149 | const char* ipVersion; |
| 3150 | const char* testDest; |
| 3151 | const char* testNextHop; |
| 3152 | const bool expectSuccess; |
| 3153 | const std::string expectedResult; |
| 3154 | } kTestData[] = { |
| 3155 | {IP_RULE_V4, "0.0.0.0/0", "", true, |
| 3156 | "200 0 success"}, |
| 3157 | {IP_RULE_V4, "10.251.0.0/16", "", true, |
| 3158 | "200 0 success"}, |
| 3159 | {IP_RULE_V4, "10.251.0.0/16", "fe80::/64", false, |
| 3160 | "400 0 addRoute() failed (Invalid argument)",}, |
| 3161 | {IP_RULE_V6, "::/0", "", true, |
| 3162 | "200 0 success"}, |
| 3163 | {IP_RULE_V6, "2001:db8:cafe::/64", "", true, |
| 3164 | "200 0 success"}, |
| 3165 | {IP_RULE_V6, "fe80::/64", "0.0.0.0", false, |
| 3166 | "400 0 addRoute() failed (Invalid argument)"}, |
| 3167 | }; |
| 3168 | // clang-format on |
| 3169 | |
| 3170 | for (const auto& cmd : networkCmds) { |
| 3171 | const std::vector<std::string> result = runCommand(cmd.cmdString); |
| 3172 | SCOPED_TRACE(cmd.cmdString); |
| 3173 | EXPECT_EQ(result.size(), 1U); |
| 3174 | EXPECT_EQ(cmd.expectedResult, Trim(result[0])); |
| 3175 | } |
| 3176 | |
| 3177 | for (const auto& cmd : ipfwdCmds) { |
| 3178 | const std::vector<std::string> result = runCommand(cmd.cmdString); |
| 3179 | SCOPED_TRACE(cmd.cmdString); |
| 3180 | EXPECT_EQ(result.size(), 1U); |
| 3181 | EXPECT_EQ(cmd.expectedResult, Trim(result[0])); |
| 3182 | } |
| 3183 | |
| 3184 | // Add test physical network |
| 3185 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, INetd::PERMISSION_NONE).isOk()); |
| 3186 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 3187 | |
| 3188 | for (const auto& td : kTestData) { |
| 3189 | const std::string routeAddCmd = |
| 3190 | StringPrintf("ndc network route add %d %s %s %s", TEST_NETID1, sTun.name().c_str(), |
| 3191 | td.testDest, td.testNextHop); |
| 3192 | const std::string routeRemoveCmd = |
| 3193 | StringPrintf("ndc network route remove %d %s %s %s", TEST_NETID1, |
| 3194 | sTun.name().c_str(), td.testDest, td.testNextHop); |
| 3195 | std::vector<std::string> result = runCommand(routeAddCmd); |
| 3196 | SCOPED_TRACE(routeAddCmd); |
| 3197 | EXPECT_EQ(result.size(), 1U); |
| 3198 | EXPECT_EQ(td.expectedResult, Trim(result[0])); |
| 3199 | if (td.expectSuccess) { |
| 3200 | expectNetworkRouteExists(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 3201 | sTun.name().c_str()); |
| 3202 | result = runCommand(routeRemoveCmd); |
| 3203 | EXPECT_EQ(result.size(), 1U); |
| 3204 | EXPECT_EQ(td.expectedResult, Trim(result[0])); |
| 3205 | expectNetworkRouteDoesNotExist(td.ipVersion, sTun.name(), td.testDest, td.testNextHop, |
| 3206 | sTun.name().c_str()); |
| 3207 | } |
| 3208 | } |
| 3209 | // Remove test physical network |
| 3210 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
Bernie Innocenti | a5161a0 | 2019-01-30 22:40:53 +0900 | [diff] [blame] | 3211 | } |
Luke Huang | 0e5e69d | 2019-03-06 15:42:38 +0800 | [diff] [blame] | 3212 | |
Luke Huang | 2ff8b34 | 2019-04-30 15:33:33 +0800 | [diff] [blame] | 3213 | TEST_F(BinderTest, OemNetdRelated) { |
Luke Huang | 0e5e69d | 2019-03-06 15:42:38 +0800 | [diff] [blame] | 3214 | sp<IBinder> binder; |
| 3215 | binder::Status status = mNetd->getOemNetd(&binder); |
| 3216 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3217 | sp<com::android::internal::net::IOemNetd> oemNetd; |
| 3218 | if (binder != nullptr) { |
| 3219 | oemNetd = android::interface_cast<com::android::internal::net::IOemNetd>(binder); |
| 3220 | } |
Luke Huang | 2ff8b34 | 2019-04-30 15:33:33 +0800 | [diff] [blame] | 3221 | ASSERT_NE(nullptr, oemNetd.get()); |
Luke Huang | 0e5e69d | 2019-03-06 15:42:38 +0800 | [diff] [blame] | 3222 | |
| 3223 | TimedOperation t("OemNetd isAlive RPC"); |
| 3224 | bool isAlive = false; |
| 3225 | oemNetd->isAlive(&isAlive); |
| 3226 | ASSERT_TRUE(isAlive); |
Luke Huang | 2ff8b34 | 2019-04-30 15:33:33 +0800 | [diff] [blame] | 3227 | |
| 3228 | class TestOemUnsolListener |
| 3229 | : public com::android::internal::net::BnOemNetdUnsolicitedEventListener { |
| 3230 | public: |
| 3231 | android::binder::Status onRegistered() override { |
| 3232 | std::lock_guard lock(mCvMutex); |
| 3233 | mCv.notify_one(); |
| 3234 | return android::binder::Status::ok(); |
| 3235 | } |
| 3236 | std::condition_variable& getCv() { return mCv; } |
| 3237 | std::mutex& getCvMutex() { return mCvMutex; } |
| 3238 | |
| 3239 | private: |
| 3240 | std::mutex mCvMutex; |
| 3241 | std::condition_variable mCv; |
| 3242 | }; |
| 3243 | |
| 3244 | // Start the Binder thread pool. |
| 3245 | android::ProcessState::self()->startThreadPool(); |
| 3246 | |
| 3247 | android::sp<TestOemUnsolListener> testListener = new TestOemUnsolListener(); |
| 3248 | |
| 3249 | auto& cv = testListener->getCv(); |
| 3250 | auto& cvMutex = testListener->getCvMutex(); |
| 3251 | |
| 3252 | { |
| 3253 | std::unique_lock lock(cvMutex); |
| 3254 | |
| 3255 | status = oemNetd->registerOemUnsolicitedEventListener( |
| 3256 | ::android::interface_cast< |
| 3257 | com::android::internal::net::IOemNetdUnsolicitedEventListener>( |
| 3258 | testListener)); |
| 3259 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 3260 | |
| 3261 | // Wait for receiving expected events. |
| 3262 | EXPECT_EQ(std::cv_status::no_timeout, cv.wait_for(lock, std::chrono::seconds(2))); |
| 3263 | } |
Luke Huang | 0e5e69d | 2019-03-06 15:42:38 +0800 | [diff] [blame] | 3264 | } |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3265 | |
| 3266 | void BinderTest::createVpnNetworkWithUid(bool secure, uid_t uid, int vpnNetId, |
| 3267 | int fallthroughNetId) { |
| 3268 | // Re-init sTun* to ensure route rule exists. |
| 3269 | sTun.destroy(); |
| 3270 | sTun.init(); |
| 3271 | sTun2.destroy(); |
| 3272 | sTun2.init(); |
| 3273 | |
| 3274 | // Create physical network with fallthroughNetId but not set it as default network |
| 3275 | EXPECT_TRUE(mNetd->networkCreatePhysical(fallthroughNetId, INetd::PERMISSION_NONE).isOk()); |
| 3276 | EXPECT_TRUE(mNetd->networkAddInterface(fallthroughNetId, sTun.name()).isOk()); |
| 3277 | |
| 3278 | // Create VPN with vpnNetId |
| 3279 | EXPECT_TRUE(mNetd->networkCreateVpn(vpnNetId, secure).isOk()); |
| 3280 | |
| 3281 | // Add uid to VPN |
| 3282 | EXPECT_TRUE(mNetd->networkAddUidRanges(vpnNetId, {makeUidRangeParcel(uid, uid)}).isOk()); |
| 3283 | EXPECT_TRUE(mNetd->networkAddInterface(vpnNetId, sTun2.name()).isOk()); |
| 3284 | |
| 3285 | // Add default route to fallthroughNetwork |
| 3286 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID1, sTun.name(), "::/0", "").isOk()); |
| 3287 | // Add limited route |
| 3288 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID2, sTun2.name(), "2001:db8::/32", "").isOk()); |
| 3289 | } |
| 3290 | |
| 3291 | namespace { |
| 3292 | |
| 3293 | class ScopedUidChange { |
| 3294 | public: |
| 3295 | explicit ScopedUidChange(uid_t uid) : mInputUid(uid) { |
| 3296 | mStoredUid = getuid(); |
| 3297 | if (mInputUid == mStoredUid) return; |
| 3298 | EXPECT_TRUE(seteuid(uid) == 0); |
| 3299 | } |
| 3300 | ~ScopedUidChange() { |
| 3301 | if (mInputUid == mStoredUid) return; |
| 3302 | EXPECT_TRUE(seteuid(mStoredUid) == 0); |
| 3303 | } |
| 3304 | |
| 3305 | private: |
| 3306 | uid_t mInputUid; |
| 3307 | uid_t mStoredUid; |
| 3308 | }; |
| 3309 | |
| 3310 | constexpr uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000; |
| 3311 | |
| 3312 | void clearQueue(int tunFd) { |
| 3313 | char buf[4096]; |
| 3314 | int ret; |
| 3315 | do { |
| 3316 | ret = read(tunFd, buf, sizeof(buf)); |
| 3317 | } while (ret > 0); |
| 3318 | } |
| 3319 | |
| 3320 | void checkDataReceived(int udpSocket, int tunFd) { |
| 3321 | char buf[4096] = {}; |
| 3322 | // Clear tunFd's queue before write something because there might be some |
| 3323 | // arbitrary packets in the queue. (e.g. ICMPv6 packet) |
| 3324 | clearQueue(tunFd); |
| 3325 | EXPECT_EQ(4, write(udpSocket, "foo", sizeof("foo"))); |
| 3326 | // TODO: extract header and verify data |
| 3327 | EXPECT_GT(read(tunFd, buf, sizeof(buf)), 0); |
| 3328 | } |
| 3329 | |
| 3330 | bool sendIPv6PacketFromUid(uid_t uid, const in6_addr& dstAddr, Fwmark* fwmark, int tunFd) { |
| 3331 | ScopedUidChange scopedUidChange(uid); |
Bernie Innocenti | d26a04a | 2019-10-30 17:15:15 +0900 | [diff] [blame] | 3332 | unique_fd testSocket(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3333 | if (testSocket < 0) return false; |
| 3334 | |
Nick Desaulniers | 6b35750 | 2019-10-11 09:26:44 -0700 | [diff] [blame] | 3335 | const sockaddr_in6 dst6 = { |
| 3336 | .sin6_family = AF_INET6, |
| 3337 | .sin6_port = 42, |
| 3338 | .sin6_addr = dstAddr, |
| 3339 | }; |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 3340 | int res = connect(testSocket, (sockaddr*)&dst6, sizeof(dst6)); |
| 3341 | socklen_t fwmarkLen = sizeof(fwmark->intValue); |
| 3342 | EXPECT_NE(-1, getsockopt(testSocket, SOL_SOCKET, SO_MARK, &(fwmark->intValue), &fwmarkLen)); |
| 3343 | if (res == -1) return false; |
| 3344 | |
| 3345 | char addr[INET6_ADDRSTRLEN]; |
| 3346 | inet_ntop(AF_INET6, &dstAddr, addr, INET6_ADDRSTRLEN); |
| 3347 | SCOPED_TRACE(StringPrintf("sendIPv6PacketFromUid, addr: %s, uid: %u", addr, uid)); |
| 3348 | checkDataReceived(testSocket, tunFd); |
| 3349 | return true; |
| 3350 | } |
| 3351 | |
| 3352 | void expectVpnFallthroughRuleExists(const std::string& ifName, int vpnNetId) { |
| 3353 | std::string vpnFallthroughRule = |
| 3354 | StringPrintf("%d:\tfrom all fwmark 0x%x/0xffff lookup %s", |
| 3355 | RULE_PRIORITY_VPN_FALLTHROUGH, vpnNetId, ifName.c_str()); |
| 3356 | for (const auto& ipVersion : {IP_RULE_V4, IP_RULE_V6}) { |
| 3357 | EXPECT_TRUE(ipRuleExists(ipVersion, vpnFallthroughRule)); |
| 3358 | } |
| 3359 | } |
| 3360 | |
| 3361 | void expectVpnFallthroughWorks(android::net::INetd* netdService, bool bypassable, uid_t uid, |
| 3362 | const TunInterface& fallthroughNetwork, |
| 3363 | const TunInterface& vpnNetwork, int vpnNetId = TEST_NETID2, |
| 3364 | int fallthroughNetId = TEST_NETID1) { |
| 3365 | // Set default network to NETID_UNSET |
| 3366 | EXPECT_TRUE(netdService->networkSetDefault(NETID_UNSET).isOk()); |
| 3367 | |
| 3368 | // insideVpnAddr based on the route we added in createVpnNetworkWithUid |
| 3369 | in6_addr insideVpnAddr = { |
| 3370 | {// 2001:db8:cafe::1 |
| 3371 | .u6_addr8 = {0x20, 0x01, 0x0d, 0xb8, 0xca, 0xfe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}}; |
| 3372 | // outsideVpnAddr will hit the route in the fallthrough network route table |
| 3373 | // because we added default route in createVpnNetworkWithUid |
| 3374 | in6_addr outsideVpnAddr = { |
| 3375 | {// 2607:f0d0:1002::4 |
| 3376 | .u6_addr8 = {0x26, 0x07, 0xf0, 0xd0, 0x10, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}}}; |
| 3377 | |
| 3378 | int fallthroughFd = fallthroughNetwork.getFdForTesting(); |
| 3379 | int vpnFd = vpnNetwork.getFdForTesting(); |
| 3380 | // Expect all connections to fail because UID 0 is not routed to the VPN and there is no |
| 3381 | // default network. |
| 3382 | Fwmark fwmark; |
| 3383 | EXPECT_FALSE(sendIPv6PacketFromUid(0, outsideVpnAddr, &fwmark, fallthroughFd)); |
| 3384 | EXPECT_FALSE(sendIPv6PacketFromUid(0, insideVpnAddr, &fwmark, fallthroughFd)); |
| 3385 | |
| 3386 | // Set default network |
| 3387 | EXPECT_TRUE(netdService->networkSetDefault(fallthroughNetId).isOk()); |
| 3388 | |
| 3389 | // Connections go on the default network because UID 0 is not subject to the VPN. |
| 3390 | EXPECT_TRUE(sendIPv6PacketFromUid(0, outsideVpnAddr, &fwmark, fallthroughFd)); |
| 3391 | EXPECT_EQ(fallthroughNetId | 0xC0000, static_cast<int>(fwmark.intValue)); |
| 3392 | EXPECT_TRUE(sendIPv6PacketFromUid(0, insideVpnAddr, &fwmark, fallthroughFd)); |
| 3393 | EXPECT_EQ(fallthroughNetId | 0xC0000, static_cast<int>(fwmark.intValue)); |
| 3394 | |
| 3395 | // Check if fallthrough rule exists |
| 3396 | expectVpnFallthroughRuleExists(fallthroughNetwork.name(), vpnNetId); |
| 3397 | |
| 3398 | // Expect fallthrough to default network |
| 3399 | // The fwmark differs depending on whether the VPN is bypassable or not. |
| 3400 | EXPECT_TRUE(sendIPv6PacketFromUid(uid, outsideVpnAddr, &fwmark, fallthroughFd)); |
| 3401 | EXPECT_EQ(bypassable ? vpnNetId : fallthroughNetId, static_cast<int>(fwmark.intValue)); |
| 3402 | |
| 3403 | // Expect connect success, packet will be sent to vpnFd. |
| 3404 | EXPECT_TRUE(sendIPv6PacketFromUid(uid, insideVpnAddr, &fwmark, vpnFd)); |
| 3405 | EXPECT_EQ(bypassable ? vpnNetId : fallthroughNetId, static_cast<int>(fwmark.intValue)); |
| 3406 | |
| 3407 | // Explicitly select vpn network |
| 3408 | setNetworkForProcess(vpnNetId); |
| 3409 | |
| 3410 | // Expect fallthrough to default network |
| 3411 | EXPECT_TRUE(sendIPv6PacketFromUid(0, outsideVpnAddr, &fwmark, fallthroughFd)); |
| 3412 | // Expect the mark contains all the bit because we've selected network. |
| 3413 | EXPECT_EQ(vpnNetId | 0xF0000, static_cast<int>(fwmark.intValue)); |
| 3414 | |
| 3415 | // Expect connect success, packet will be sent to vpnFd. |
| 3416 | EXPECT_TRUE(sendIPv6PacketFromUid(0, insideVpnAddr, &fwmark, vpnFd)); |
| 3417 | // Expect the mark contains all the bit because we've selected network. |
| 3418 | EXPECT_EQ(vpnNetId | 0xF0000, static_cast<int>(fwmark.intValue)); |
| 3419 | |
| 3420 | // Explicitly select fallthrough network |
| 3421 | setNetworkForProcess(fallthroughNetId); |
| 3422 | |
| 3423 | // The mark is set to fallthrough network because we've selected it. |
| 3424 | EXPECT_TRUE(sendIPv6PacketFromUid(0, outsideVpnAddr, &fwmark, fallthroughFd)); |
| 3425 | EXPECT_TRUE(sendIPv6PacketFromUid(0, insideVpnAddr, &fwmark, fallthroughFd)); |
| 3426 | |
| 3427 | // If vpn is BypassableVPN, connections can also go on the fallthrough network under vpn uid. |
| 3428 | if (bypassable) { |
| 3429 | EXPECT_TRUE(sendIPv6PacketFromUid(uid, outsideVpnAddr, &fwmark, fallthroughFd)); |
| 3430 | EXPECT_TRUE(sendIPv6PacketFromUid(uid, insideVpnAddr, &fwmark, fallthroughFd)); |
| 3431 | } else { |
| 3432 | // If not, no permission to bypass vpn. |
| 3433 | EXPECT_FALSE(sendIPv6PacketFromUid(uid, outsideVpnAddr, &fwmark, fallthroughFd)); |
| 3434 | EXPECT_FALSE(sendIPv6PacketFromUid(uid, insideVpnAddr, &fwmark, fallthroughFd)); |
| 3435 | } |
| 3436 | } |
| 3437 | |
| 3438 | } // namespace |
| 3439 | |
| 3440 | TEST_F(BinderTest, SecureVPNFallthrough) { |
| 3441 | createVpnNetworkWithUid(true /* secure */, TEST_UID1); |
| 3442 | // Get current default network NetId |
| 3443 | ASSERT_TRUE(mNetd->networkGetDefault(&mStoredDefaultNetwork).isOk()); |
| 3444 | expectVpnFallthroughWorks(mNetd.get(), false /* bypassable */, TEST_UID1, sTun, sTun2); |
| 3445 | } |
| 3446 | |
| 3447 | TEST_F(BinderTest, BypassableVPNFallthrough) { |
| 3448 | createVpnNetworkWithUid(false /* secure */, TEST_UID1); |
| 3449 | // Get current default network NetId |
| 3450 | ASSERT_TRUE(mNetd->networkGetDefault(&mStoredDefaultNetwork).isOk()); |
| 3451 | expectVpnFallthroughWorks(mNetd.get(), true /* bypassable */, TEST_UID1, sTun, sTun2); |
Bernie Innocenti | 80ffd0f | 2019-06-05 15:27:46 +0900 | [diff] [blame] | 3452 | } |
Chiachang Wang | 00fc62f | 2019-12-04 20:38:26 +0800 | [diff] [blame] | 3453 | |
| 3454 | namespace { |
| 3455 | |
| 3456 | int32_t createIpv6SocketAndCheckMark(int type, const in6_addr& dstAddr) { |
| 3457 | const sockaddr_in6 dst6 = { |
| 3458 | .sin6_family = AF_INET6, |
| 3459 | .sin6_port = 1234, |
| 3460 | .sin6_addr = dstAddr, |
| 3461 | }; |
| 3462 | // create non-blocking socket. |
| 3463 | int sockFd = socket(AF_INET6, type | SOCK_NONBLOCK, 0); |
| 3464 | EXPECT_NE(-1, sockFd); |
| 3465 | EXPECT_EQ((type == SOCK_STREAM) ? -1 : 0, connect(sockFd, (sockaddr*)&dst6, sizeof(dst6))); |
| 3466 | |
| 3467 | // Get socket fwmark. |
| 3468 | Fwmark fwmark; |
| 3469 | socklen_t fwmarkLen = sizeof(fwmark.intValue); |
| 3470 | EXPECT_EQ(0, getsockopt(sockFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen)); |
| 3471 | EXPECT_EQ(0, close(sockFd)); |
| 3472 | return fwmark.intValue; |
| 3473 | } |
| 3474 | |
| 3475 | } // namespace |
| 3476 | |
| 3477 | TEST_F(BinderTest, GetFwmarkForNetwork) { |
| 3478 | in6_addr v6Addr = { |
| 3479 | {// 2001:db8:cafe::8888 |
| 3480 | .u6_addr8 = {0x20, 0x01, 0x0d, 0xb8, 0xca, 0xfe, 0, 0, 0, 0, 0, 0, 0, 0, 0x88, 0x88}}}; |
| 3481 | // Add test physical network 1 and set as default network. |
| 3482 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, INetd::PERMISSION_NONE).isOk()); |
| 3483 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 3484 | EXPECT_TRUE(mNetd->networkAddRoute(TEST_NETID1, sTun.name(), "2001:db8::/32", "").isOk()); |
| 3485 | EXPECT_TRUE(mNetd->networkSetDefault(TEST_NETID1).isOk()); |
| 3486 | // Add test physical network 2 |
| 3487 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID2, INetd::PERMISSION_NONE).isOk()); |
| 3488 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID2, sTun2.name()).isOk()); |
| 3489 | |
| 3490 | // Get fwmark for network 1. |
| 3491 | MarkMaskParcel maskMarkNet1; |
| 3492 | ASSERT_TRUE(mNetd->getFwmarkForNetwork(TEST_NETID1, &maskMarkNet1).isOk()); |
| 3493 | |
| 3494 | uint32_t fwmarkTcp = createIpv6SocketAndCheckMark(SOCK_STREAM, v6Addr); |
| 3495 | uint32_t fwmarkUdp = createIpv6SocketAndCheckMark(SOCK_DGRAM, v6Addr); |
| 3496 | EXPECT_EQ(maskMarkNet1.mark, static_cast<int>(fwmarkTcp & maskMarkNet1.mask)); |
| 3497 | EXPECT_EQ(maskMarkNet1.mark, static_cast<int>(fwmarkUdp & maskMarkNet1.mask)); |
| 3498 | |
| 3499 | // Get fwmark for network 2. |
| 3500 | MarkMaskParcel maskMarkNet2; |
| 3501 | ASSERT_TRUE(mNetd->getFwmarkForNetwork(TEST_NETID2, &maskMarkNet2).isOk()); |
| 3502 | EXPECT_NE(maskMarkNet2.mark, static_cast<int>(fwmarkTcp & maskMarkNet2.mask)); |
| 3503 | EXPECT_NE(maskMarkNet2.mark, static_cast<int>(fwmarkUdp & maskMarkNet2.mask)); |
| 3504 | |
| 3505 | // Remove test physical network. |
| 3506 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID2).isOk()); |
| 3507 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 3508 | } |
Hungming Chen | 9257104 | 2020-02-20 17:01:31 +0800 | [diff] [blame] | 3509 | |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 3510 | namespace { |
| 3511 | |
| 3512 | TetherOffloadRuleParcel makeTetherOffloadRule(int inputInterfaceIndex, int outputInterfaceIndex, |
| 3513 | const std::vector<uint8_t>& destination, |
| 3514 | int prefixLength, |
| 3515 | const std::vector<uint8_t>& srcL2Address, |
| 3516 | const std::vector<uint8_t>& dstL2Address) { |
| 3517 | android::net::TetherOffloadRuleParcel parcel; |
| 3518 | parcel.inputInterfaceIndex = inputInterfaceIndex; |
| 3519 | parcel.outputInterfaceIndex = outputInterfaceIndex; |
| 3520 | parcel.destination = destination; |
| 3521 | parcel.prefixLength = prefixLength; |
| 3522 | parcel.srcL2Address = srcL2Address; |
| 3523 | parcel.dstL2Address = dstL2Address; |
| 3524 | return parcel; |
| 3525 | } |
| 3526 | |
| 3527 | } // namespace |
| 3528 | |
| 3529 | TEST_F(BinderTest, TetherOffloadRule) { |
Hungming Chen | 9257104 | 2020-02-20 17:01:31 +0800 | [diff] [blame] | 3530 | SKIP_IF_BPF_NOT_SUPPORTED; |
| 3531 | |
| 3532 | // TODO: Perhaps verify invalid interface index once the netd handle the error in methods. |
| 3533 | constexpr uint32_t kIfaceInt = 101; |
| 3534 | constexpr uint32_t kIfaceExt = 102; |
| 3535 | constexpr uint32_t kIfaceNonExistent = 103; |
| 3536 | |
| 3537 | const std::vector<uint8_t> kAddr6 = {0x20, 0x01, 0x0d, 0xb8, 0xca, 0xfe, 0x00, 0x00, |
| 3538 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}; |
| 3539 | const std::vector<uint8_t> kSrcMac = {0x00, 0x00, 0x00, 0x00, 0x00, 0x0a}; |
| 3540 | const std::vector<uint8_t> kDstMac = {0x00, 0x00, 0x00, 0x00, 0x00, 0x0b}; |
| 3541 | |
| 3542 | const std::vector<uint8_t> kInvalidAddr4 = {0xac, 0x0a, 0x0d, 0xb8}; // should be IPv6 address |
| 3543 | const std::vector<uint8_t> kInvalidMac = {0xde, 0xad, 0xbe, 0xef}; // should be 6-byte length |
| 3544 | |
| 3545 | // Invalid IP address, add rule |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 3546 | TetherOffloadRuleParcel rule = makeTetherOffloadRule( |
| 3547 | kIfaceExt, kIfaceInt, kInvalidAddr4 /*bad*/, 128, kSrcMac, kDstMac); |
| 3548 | auto status = mNetd->tetherOffloadRuleAdd(rule); |
Hungming Chen | 9257104 | 2020-02-20 17:01:31 +0800 | [diff] [blame] | 3549 | EXPECT_FALSE(status.isOk()); |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 3550 | EXPECT_EQ(EAFNOSUPPORT, status.serviceSpecificErrorCode()); |
Hungming Chen | 9257104 | 2020-02-20 17:01:31 +0800 | [diff] [blame] | 3551 | |
| 3552 | // Invalid source L2 address, add rule |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 3553 | rule = makeTetherOffloadRule(kIfaceExt, kIfaceInt, kAddr6, 128, kInvalidMac /*bad*/, kDstMac); |
| 3554 | status = mNetd->tetherOffloadRuleAdd(rule); |
Hungming Chen | 9257104 | 2020-02-20 17:01:31 +0800 | [diff] [blame] | 3555 | EXPECT_FALSE(status.isOk()); |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 3556 | EXPECT_EQ(ENXIO, status.serviceSpecificErrorCode()); |
Hungming Chen | 9257104 | 2020-02-20 17:01:31 +0800 | [diff] [blame] | 3557 | |
| 3558 | // Invalid destination L2 address, add rule |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 3559 | rule = makeTetherOffloadRule(kIfaceExt, kIfaceInt, kAddr6, 128, kSrcMac, kInvalidMac /*bad*/); |
| 3560 | status = mNetd->tetherOffloadRuleAdd(rule); |
Hungming Chen | 9257104 | 2020-02-20 17:01:31 +0800 | [diff] [blame] | 3561 | EXPECT_FALSE(status.isOk()); |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 3562 | EXPECT_EQ(ENXIO, status.serviceSpecificErrorCode()); |
Hungming Chen | 9257104 | 2020-02-20 17:01:31 +0800 | [diff] [blame] | 3563 | |
| 3564 | // Invalid IP address, remove rule |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 3565 | rule = makeTetherOffloadRule(kIfaceExt, kIfaceInt, kInvalidAddr4 /*bad*/, 128, kSrcMac, |
| 3566 | kDstMac); |
| 3567 | status = mNetd->tetherOffloadRuleRemove(rule); |
| 3568 | EXPECT_FALSE(status.isOk()); |
| 3569 | EXPECT_EQ(EAFNOSUPPORT, status.serviceSpecificErrorCode()); |
| 3570 | |
| 3571 | // Invalid prefix length |
| 3572 | rule = makeTetherOffloadRule(kIfaceExt, kIfaceInt, kAddr6, 64 /*bad*/, kSrcMac, kDstMac); |
| 3573 | status = mNetd->tetherOffloadRuleAdd(rule); |
| 3574 | EXPECT_FALSE(status.isOk()); |
| 3575 | EXPECT_EQ(EINVAL, status.serviceSpecificErrorCode()); |
| 3576 | status = mNetd->tetherOffloadRuleRemove(rule); |
Hungming Chen | 9257104 | 2020-02-20 17:01:31 +0800 | [diff] [blame] | 3577 | EXPECT_FALSE(status.isOk()); |
| 3578 | EXPECT_EQ(EINVAL, status.serviceSpecificErrorCode()); |
| 3579 | |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 3580 | // Invalid interface index |
| 3581 | rule = makeTetherOffloadRule(kIfaceExt, 0, kAddr6, 128, kSrcMac, kDstMac); |
| 3582 | status = mNetd->tetherOffloadRuleAdd(rule); |
| 3583 | EXPECT_FALSE(status.isOk()); |
| 3584 | EXPECT_EQ(ENODEV, status.serviceSpecificErrorCode()); |
| 3585 | rule = makeTetherOffloadRule(0, kIfaceInt, kAddr6, 64, kSrcMac, kDstMac); |
| 3586 | status = mNetd->tetherOffloadRuleRemove(rule); |
| 3587 | EXPECT_FALSE(status.isOk()); |
| 3588 | EXPECT_EQ(ENODEV, status.serviceSpecificErrorCode()); |
| 3589 | |
Hungming Chen | 9257104 | 2020-02-20 17:01:31 +0800 | [diff] [blame] | 3590 | // Remove non existent rule. Expect that silently return success if the rule did not exist. |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 3591 | rule = makeTetherOffloadRule(kIfaceNonExistent, kIfaceInt, kAddr6, 128, kSrcMac, kDstMac); |
| 3592 | EXPECT_TRUE(mNetd->tetherOffloadRuleRemove(rule).isOk()); |
Hungming Chen | 9257104 | 2020-02-20 17:01:31 +0800 | [diff] [blame] | 3593 | |
| 3594 | // Add and remove rule normally. |
Lorenzo Colitti | f0e051c | 2020-04-06 09:19:57 +0000 | [diff] [blame] | 3595 | rule = makeTetherOffloadRule(kIfaceExt, kIfaceInt, kAddr6, 128, kSrcMac, kDstMac); |
| 3596 | EXPECT_TRUE(mNetd->tetherOffloadRuleAdd(rule).isOk()); |
| 3597 | EXPECT_TRUE(mNetd->tetherOffloadRuleRemove(rule).isOk()); |
Hungming Chen | 9257104 | 2020-02-20 17:01:31 +0800 | [diff] [blame] | 3598 | } |