Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | * RouteControllerTest.cpp - unit tests for RouteController.cpp |
| 17 | */ |
| 18 | |
| 19 | #include <gtest/gtest.h> |
Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 20 | #include <fstream> |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 21 | |
Benedict Wong | b9baf26 | 2017-12-03 15:43:08 -0800 | [diff] [blame] | 22 | #include "Fwmark.h" |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 23 | #include "IptablesBaseTest.h" |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 24 | #include "NetlinkCommands.h" |
| 25 | #include "RouteController.h" |
| 26 | |
Benedict Wong | b9baf26 | 2017-12-03 15:43:08 -0800 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> |
| 28 | |
| 29 | using android::base::StringPrintf; |
| 30 | |
Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 31 | static const char* TEST_IFACE1 = "netdtest1"; |
| 32 | static const char* TEST_IFACE2 = "netdtest2"; |
| 33 | static const uint32_t TEST_IFACE1_INDEX = 901; |
| 34 | static const uint32_t TEST_IFACE2_INDEX = 902; |
| 35 | // See Linux kernel source in include/net/flow.h |
| 36 | #define LOOPBACK_IFINDEX 1 |
| 37 | |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 38 | namespace android { |
| 39 | namespace net { |
| 40 | |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 41 | class RouteControllerTest : public IptablesBaseTest { |
| 42 | public: |
| 43 | RouteControllerTest() { |
| 44 | RouteController::iptablesRestoreCommandFunction = fakeExecIptablesRestoreCommand; |
Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 45 | RouteController::ifNameToIndexFunction = fakeIfaceNameToIndexFunction; |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 46 | } |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 47 | |
| 48 | int flushRoutes(uint32_t a) { |
| 49 | return RouteController::flushRoutes(a); |
| 50 | } |
Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 51 | |
| 52 | uint32_t static fakeIfaceNameToIndexFunction(const char* iface) { |
| 53 | // "lo" is the same as the real one |
| 54 | if (!strcmp(iface, "lo")) return LOOPBACK_IFINDEX; |
| 55 | if (!strcmp(iface, TEST_IFACE1)) return TEST_IFACE1_INDEX; |
| 56 | if (!strcmp(iface, TEST_IFACE2)) return TEST_IFACE2_INDEX; |
| 57 | return 0; |
| 58 | } |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | TEST_F(RouteControllerTest, TestGetRulePriority) { |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 62 | // Expect a rule dump for these two families to contain at least the following priorities. |
| 63 | for (int family : {AF_INET, AF_INET6 }) { |
| 64 | std::set<uint32_t> expectedPriorities = { |
Ken Chen | 69839af | 2021-01-05 22:48:32 +0800 | [diff] [blame] | 65 | 0, |
| 66 | RULE_PRIORITY_LEGACY_SYSTEM, |
| 67 | RULE_PRIORITY_LEGACY_NETWORK, |
| 68 | RULE_PRIORITY_UNREACHABLE, |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | NetlinkDumpCallback callback = [&expectedPriorities] (const nlmsghdr *nlh) { |
| 72 | expectedPriorities.erase(getRulePriority(nlh)); |
| 73 | }; |
| 74 | |
| 75 | rtmsg rtm = { .rtm_family = static_cast<uint8_t>(family) }; |
| 76 | iovec iov[] = { |
| 77 | { nullptr, 0 }, |
| 78 | { &rtm, sizeof(rtm) }, |
| 79 | }; |
| 80 | |
| 81 | ASSERT_EQ(0, sendNetlinkRequest(RTM_GETRULE, NETLINK_DUMP_FLAGS, |
| 82 | iov, ARRAY_SIZE(iov), &callback)); |
| 83 | |
| 84 | EXPECT_TRUE(expectedPriorities.empty()) << |
| 85 | "Did not see rule with priority " << *expectedPriorities.begin() << |
| 86 | " in dump for address family " << family; |
| 87 | } |
| 88 | } |
| 89 | |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 90 | TEST_F(RouteControllerTest, TestRouteFlush) { |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 91 | // Pick a table number that's not used by the system. |
| 92 | const uint32_t table1 = 500; |
| 93 | const uint32_t table2 = 600; |
| 94 | static_assert(table1 < RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX, |
| 95 | "Test table1 number too large"); |
| 96 | static_assert(table2 < RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX, |
| 97 | "Test table2 number too large"); |
| 98 | |
Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 99 | EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table1, "lo", |
Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 100 | "192.0.2.2/32", nullptr, 0 /* mtu */, 0 /* priority */)); |
Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 101 | EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table1, "lo", |
Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 102 | "192.0.2.3/32", nullptr, 0 /* mtu */, 0 /* priority */)); |
Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 103 | EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table2, "lo", |
Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 104 | "192.0.2.4/32", nullptr, 0 /* mtu */, 0 /* priority */)); |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 105 | |
| 106 | EXPECT_EQ(0, flushRoutes(table1)); |
| 107 | |
Maciej Żenczykowski | 78879f5 | 2023-04-14 22:47:48 +0000 | [diff] [blame] | 108 | EXPECT_EQ(-ESRCH, modifyIpRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, table1, "lo", |
Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 109 | "192.0.2.2/32", nullptr, 0 /* mtu */, 0 /* priority */)); |
Maciej Żenczykowski | 78879f5 | 2023-04-14 22:47:48 +0000 | [diff] [blame] | 110 | EXPECT_EQ(-ESRCH, modifyIpRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, table1, "lo", |
Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 111 | "192.0.2.3/32", nullptr, 0 /* mtu */, 0 /* priority */)); |
Maciej Żenczykowski | 78879f5 | 2023-04-14 22:47:48 +0000 | [diff] [blame] | 112 | EXPECT_EQ(0, modifyIpRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, table2, "lo", |
Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 113 | "192.0.2.4/32", nullptr, 0 /* mtu */, 0 /* priority */)); |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 114 | } |
| 115 | |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 116 | TEST_F(RouteControllerTest, TestModifyIncomingPacketMark) { |
Maciej Żenczykowski | 0a47ca4 | 2023-11-15 08:00:03 +0000 | [diff] [blame] | 117 | uint32_t mask = Fwmark::getUidBillingMask() | Fwmark::getIngressCpuWakeupMask(); |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 118 | |
Benedict Wong | b9baf26 | 2017-12-03 15:43:08 -0800 | [diff] [blame] | 119 | static constexpr int TEST_NETID = 30; |
| 120 | EXPECT_EQ(0, modifyIncomingPacketMark(TEST_NETID, "netdtest0", |
| 121 | PERMISSION_NONE, true)); |
| 122 | expectIptablesRestoreCommands({StringPrintf( |
| 123 | "-t mangle -A routectrl_mangle_INPUT -i netdtest0 -j MARK --set-mark " |
| 124 | "0x3001e/0x%x", |
Maciej Żenczykowski | 0a47ca4 | 2023-11-15 08:00:03 +0000 | [diff] [blame] | 125 | ~mask)}); |
Benedict Wong | b9baf26 | 2017-12-03 15:43:08 -0800 | [diff] [blame] | 126 | |
| 127 | EXPECT_EQ(0, modifyIncomingPacketMark(TEST_NETID, "netdtest0", |
| 128 | PERMISSION_NONE, false)); |
| 129 | expectIptablesRestoreCommands({StringPrintf( |
| 130 | "-t mangle -D routectrl_mangle_INPUT -i netdtest0 -j MARK --set-mark " |
| 131 | "0x3001e/0x%x", |
Maciej Żenczykowski | 0a47ca4 | 2023-11-15 08:00:03 +0000 | [diff] [blame] | 132 | ~mask)}); |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 133 | } |
| 134 | |
Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 135 | bool hasLocalInterfaceInRouteTable(const char* iface) { |
| 136 | // Calculate the table index from interface index |
| 137 | std::string index = std::to_string(RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL + |
| 138 | RouteController::ifNameToIndexFunction(iface)); |
| 139 | std::string localIface = |
| 140 | index + " " + std::string(iface) + std::string(RouteController::INTERFACE_LOCAL_SUFFIX); |
| 141 | std::string line; |
| 142 | |
| 143 | std::ifstream input(RouteController::RT_TABLES_PATH); |
| 144 | while (std::getline(input, line)) { |
| 145 | if (line.find(localIface) != std::string::npos) { |
| 146 | return true; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | TEST_F(RouteControllerTest, TestCreateVirtualLocalInterfaceTable) { |
| 154 | static constexpr int TEST_NETID = 65500; |
| 155 | std::map<int32_t, UidRanges> uidRangeMap; |
| 156 | EXPECT_EQ(0, RouteController::addInterfaceToVirtualNetwork(TEST_NETID, TEST_IFACE1, false, |
| 157 | uidRangeMap, false)); |
| 158 | // Expect to create <iface>_local in the routing table |
| 159 | EXPECT_TRUE(hasLocalInterfaceInRouteTable(TEST_IFACE1)); |
| 160 | // Add another interface, <TEST_IFACE2>_local should also be created |
| 161 | EXPECT_EQ(0, RouteController::addInterfaceToVirtualNetwork(TEST_NETID, TEST_IFACE2, false, |
| 162 | uidRangeMap, false)); |
| 163 | EXPECT_TRUE(hasLocalInterfaceInRouteTable(TEST_IFACE2)); |
| 164 | // Remove TEST_IFACE1 |
| 165 | EXPECT_EQ(0, RouteController::removeInterfaceFromVirtualNetwork(TEST_NETID, TEST_IFACE1, false, |
| 166 | uidRangeMap, false)); |
| 167 | // Interface remove should also remove the virtual local interface for routing table |
| 168 | EXPECT_FALSE(hasLocalInterfaceInRouteTable(TEST_IFACE1)); |
| 169 | // <TEST_IFACE2> should still in the routing table |
| 170 | EXPECT_TRUE(hasLocalInterfaceInRouteTable(TEST_IFACE2)); |
| 171 | EXPECT_EQ(0, RouteController::removeInterfaceFromVirtualNetwork(TEST_NETID, TEST_IFACE2, false, |
| 172 | uidRangeMap, false)); |
| 173 | EXPECT_FALSE(hasLocalInterfaceInRouteTable(TEST_IFACE2)); |
| 174 | } |
| 175 | |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 176 | } // namespace net |
| 177 | } // namespace android |