blob: 555ffd1cf16d7d8e053e463f6fc1435621177131 [file] [log] [blame]
Lorenzo Colitti60367db2017-02-13 16:31:45 +09001/*
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 Wangf79e3562022-01-15 13:31:04 +080020#include <fstream>
Lorenzo Colitti60367db2017-02-13 16:31:45 +090021
Benedict Wongb9baf262017-12-03 15:43:08 -080022#include "Fwmark.h"
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090023#include "IptablesBaseTest.h"
Lorenzo Colitti60367db2017-02-13 16:31:45 +090024#include "NetlinkCommands.h"
25#include "RouteController.h"
26
Benedict Wongb9baf262017-12-03 15:43:08 -080027#include <android-base/stringprintf.h>
28
29using android::base::StringPrintf;
30
Chiachang Wangf79e3562022-01-15 13:31:04 +080031static const char* TEST_IFACE1 = "netdtest1";
32static const char* TEST_IFACE2 = "netdtest2";
33static const uint32_t TEST_IFACE1_INDEX = 901;
34static const uint32_t TEST_IFACE2_INDEX = 902;
35// See Linux kernel source in include/net/flow.h
36#define LOOPBACK_IFINDEX 1
37
Lorenzo Colitti60367db2017-02-13 16:31:45 +090038namespace android {
39namespace net {
40
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090041class RouteControllerTest : public IptablesBaseTest {
42public:
43 RouteControllerTest() {
44 RouteController::iptablesRestoreCommandFunction = fakeExecIptablesRestoreCommand;
Chiachang Wangf79e3562022-01-15 13:31:04 +080045 RouteController::ifNameToIndexFunction = fakeIfaceNameToIndexFunction;
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090046 }
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +090047
48 int flushRoutes(uint32_t a) {
49 return RouteController::flushRoutes(a);
50 }
Chiachang Wangf79e3562022-01-15 13:31:04 +080051
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 Colittic1306ea2017-03-27 05:52:31 +090059};
60
61TEST_F(RouteControllerTest, TestGetRulePriority) {
Lorenzo Colitti60367db2017-02-13 16:31:45 +090062 // 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 Chen69839af2021-01-05 22:48:32 +080065 0,
66 RULE_PRIORITY_LEGACY_SYSTEM,
67 RULE_PRIORITY_LEGACY_NETWORK,
68 RULE_PRIORITY_UNREACHABLE,
Lorenzo Colitti60367db2017-02-13 16:31:45 +090069 };
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 Colittic1306ea2017-03-27 05:52:31 +090090TEST_F(RouteControllerTest, TestRouteFlush) {
Lorenzo Colitti60367db2017-02-13 16:31:45 +090091 // 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 Wearfa94a272019-12-05 15:01:48 -080099 EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table1, "lo",
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200100 "192.0.2.2/32", nullptr, 0 /* mtu */, 0 /* priority */));
Tyler Wearfa94a272019-12-05 15:01:48 -0800101 EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table1, "lo",
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200102 "192.0.2.3/32", nullptr, 0 /* mtu */, 0 /* priority */));
Tyler Wearfa94a272019-12-05 15:01:48 -0800103 EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table2, "lo",
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200104 "192.0.2.4/32", nullptr, 0 /* mtu */, 0 /* priority */));
Lorenzo Colitti60367db2017-02-13 16:31:45 +0900105
106 EXPECT_EQ(0, flushRoutes(table1));
107
Maciej Żenczykowski78879f52023-04-14 22:47:48 +0000108 EXPECT_EQ(-ESRCH, modifyIpRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, table1, "lo",
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200109 "192.0.2.2/32", nullptr, 0 /* mtu */, 0 /* priority */));
Maciej Żenczykowski78879f52023-04-14 22:47:48 +0000110 EXPECT_EQ(-ESRCH, modifyIpRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, table1, "lo",
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200111 "192.0.2.3/32", nullptr, 0 /* mtu */, 0 /* priority */));
Maciej Żenczykowski78879f52023-04-14 22:47:48 +0000112 EXPECT_EQ(0, modifyIpRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, table2, "lo",
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200113 "192.0.2.4/32", nullptr, 0 /* mtu */, 0 /* priority */));
Lorenzo Colitti60367db2017-02-13 16:31:45 +0900114}
115
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900116TEST_F(RouteControllerTest, TestModifyIncomingPacketMark) {
Maciej Żenczykowski0a47ca42023-11-15 08:00:03 +0000117 uint32_t mask = Fwmark::getUidBillingMask() | Fwmark::getIngressCpuWakeupMask();
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900118
Benedict Wongb9baf262017-12-03 15:43:08 -0800119 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 Żenczykowski0a47ca42023-11-15 08:00:03 +0000125 ~mask)});
Benedict Wongb9baf262017-12-03 15:43:08 -0800126
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 Żenczykowski0a47ca42023-11-15 08:00:03 +0000132 ~mask)});
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900133}
134
Chiachang Wangf79e3562022-01-15 13:31:04 +0800135bool 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
153TEST_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 Colitti60367db2017-02-13 16:31:45 +0900176} // namespace net
177} // namespace android