• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
20 #include <fstream>
21 
22 #include "Fwmark.h"
23 #include "IptablesBaseTest.h"
24 #include "NetlinkCommands.h"
25 #include "RouteController.h"
26 
27 #include <android-base/stringprintf.h>
28 
29 using android::base::StringPrintf;
30 
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 
38 namespace android {
39 namespace net {
40 
41 class RouteControllerTest : public IptablesBaseTest {
42 public:
RouteControllerTest()43     RouteControllerTest() {
44         RouteController::iptablesRestoreCommandFunction = fakeExecIptablesRestoreCommand;
45         RouteController::ifNameToIndexFunction = fakeIfaceNameToIndexFunction;
46     }
47 
flushRoutes(uint32_t a)48     int flushRoutes(uint32_t a) {
49         return RouteController::flushRoutes(a);
50     }
51 
fakeIfaceNameToIndexFunction(const char * iface)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     }
59 };
60 
TEST_F(RouteControllerTest,TestGetRulePriority)61 TEST_F(RouteControllerTest, TestGetRulePriority) {
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 = {
65                 0,
66                 RULE_PRIORITY_LEGACY_SYSTEM,
67                 RULE_PRIORITY_LEGACY_NETWORK,
68                 RULE_PRIORITY_UNREACHABLE,
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 
TEST_F(RouteControllerTest,TestRouteFlush)90 TEST_F(RouteControllerTest, TestRouteFlush) {
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 
99     EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table1, "lo",
100                                "192.0.2.2/32", nullptr, 0 /* mtu */, 0 /* priority */));
101     EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table1, "lo",
102                                "192.0.2.3/32", nullptr, 0 /* mtu */, 0 /* priority */));
103     EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table2, "lo",
104                                "192.0.2.4/32", nullptr, 0 /* mtu */, 0 /* priority */));
105 
106     EXPECT_EQ(0, flushRoutes(table1));
107 
108     EXPECT_EQ(-ESRCH, modifyIpRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, table1, "lo",
109                                     "192.0.2.2/32", nullptr, 0 /* mtu */, 0 /* priority */));
110     EXPECT_EQ(-ESRCH, modifyIpRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, table1, "lo",
111                                     "192.0.2.3/32", nullptr, 0 /* mtu */, 0 /* priority */));
112     EXPECT_EQ(0, modifyIpRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, table2, "lo",
113                                "192.0.2.4/32", nullptr, 0 /* mtu */, 0 /* priority */));
114 }
115 
TEST_F(RouteControllerTest,TestModifyIncomingPacketMark)116 TEST_F(RouteControllerTest, TestModifyIncomingPacketMark) {
117   uint32_t mask = ~Fwmark::getUidBillingMask();
118 
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",
125       mask)});
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",
132       mask)});
133 }
134 
hasLocalInterfaceInRouteTable(const char * iface)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 
TEST_F(RouteControllerTest,TestCreateVirtualLocalInterfaceTable)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 
176 }  // namespace net
177 }  // namespace android
178