blob: 592dc351392b913141d035bd47a3877d505c0523 [file] [log] [blame]
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "RouteController.h"
18
19#include "Fwmark.h"
20#include "NetdConstants.h"
21
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -070022#include <linux/rtnetlink.h>
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070023#include <logwrap/logwrap.h>
24#include <net/if.h>
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070025
26namespace {
27
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -070028const uint32_t RULE_PRIORITY_PRIVILEGED_LEGACY = 11000;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -070029const uint32_t RULE_PRIORITY_PER_NETWORK_EXPLICIT = 13000;
30const uint32_t RULE_PRIORITY_PER_NETWORK_INTERFACE = 14000;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -070031const uint32_t RULE_PRIORITY_LEGACY = 16000;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -070032const uint32_t RULE_PRIORITY_PER_NETWORK_NORMAL = 17000;
33const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 19000;
34const uint32_t RULE_PRIORITY_MAIN = 20000;
Sreeram Ramachandran56afacf2014-05-28 15:07:00 -070035// TODO: Uncomment once we are sure everything works.
36#if 0
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -070037const uint32_t RULE_PRIORITY_UNREACHABLE = 21000;
Sreeram Ramachandran56afacf2014-05-28 15:07:00 -070038#endif
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070039
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -070040// TODO: These should be turned into per-UID tables once the kernel supports UID-based routing.
41const int ROUTE_TABLE_PRIVILEGED_LEGACY = RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX - 901;
42const int ROUTE_TABLE_LEGACY = RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX - 902;
43
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070044uint32_t getRouteTableForInterface(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070045 uint32_t index = if_nametoindex(interface);
46 return index ? index + RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX : 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070047}
48
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -070049// Adds or removes a routing rule for IPv4 and IPv6.
50//
51// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the rule
52// returns ENETUNREACH.
53// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
54// ignored.
55// + If |interface| is non-NULL, the rule matches the specified outgoing interface.
56bool runIpRuleCommand(const char* action, uint32_t priority, uint32_t table, uint32_t fwmark,
57 uint32_t mask, const char* interface) {
Lorenzo Colittia10ac322014-04-11 18:26:17 +090058 char priorityString[UINT32_STRLEN];
Lorenzo Colittia10ac322014-04-11 18:26:17 +090059 snprintf(priorityString, sizeof(priorityString), "%u", priority);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -070060
61 char tableString[UINT32_STRLEN];
Lorenzo Colittia10ac322014-04-11 18:26:17 +090062 snprintf(tableString, sizeof(tableString), "%u", table);
63
64 char fwmarkString[sizeof("0x12345678/0x12345678")];
65 snprintf(fwmarkString, sizeof(fwmarkString), "0x%x/0x%x", fwmark, mask);
66
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070067 const char* version[] = {"-4", "-6"};
68 for (size_t i = 0; i < ARRAY_SIZE(version); ++i) {
69 int argc = 0;
70 const char* argv[16];
71
72 argv[argc++] = IP_PATH;
73 argv[argc++] = version[i];
74 argv[argc++] = "rule";
75 argv[argc++] = action;
76 argv[argc++] = "priority";
Lorenzo Colittia10ac322014-04-11 18:26:17 +090077 argv[argc++] = priorityString;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -070078 if (table) {
79 argv[argc++] = "table";
80 argv[argc++] = tableString;
81 } else {
82 argv[argc++] = "unreachable";
83 }
Lorenzo Colittia10ac322014-04-11 18:26:17 +090084 if (mask) {
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070085 argv[argc++] = "fwmark";
Lorenzo Colittia10ac322014-04-11 18:26:17 +090086 argv[argc++] = fwmarkString;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070087 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -070088 if (interface) {
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070089 argv[argc++] = "oif";
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -070090 argv[argc++] = interface;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070091 }
92 if (android_fork_execvp(argc, const_cast<char**>(argv), NULL, false, false)) {
93 return false;
94 }
95 }
96
97 return true;
98}
99
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700100bool runIpRouteCommand(const char* action, uint32_t table, const char* interface,
101 const char* destination, const char* nexthop) {
102 char tableString[UINT32_STRLEN];
103 snprintf(tableString, sizeof(tableString), "%u", table);
104
105 int argc = 0;
106 const char* argv[16];
107
108 argv[argc++] = IP_PATH;
109 argv[argc++] = "route";
110 argv[argc++] = action;
111 argv[argc++] = "table";
112 argv[argc++] = tableString;
113 if (destination) {
114 argv[argc++] = destination;
115 argv[argc++] = "dev";
116 argv[argc++] = interface;
117 if (nexthop) {
118 argv[argc++] = "via";
119 argv[argc++] = nexthop;
120 }
121 }
122
Sreeram Ramachandrana79f6182014-04-24 15:55:26 -0700123 return !android_fork_execvp(argc, const_cast<char**>(argv), NULL, false, false);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700124}
125
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700126bool modifyPerNetworkRules(unsigned netId, const char* interface, Permission permission, bool add,
127 bool modifyIptables) {
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700128 uint32_t table = getRouteTableForInterface(interface);
129 if (!table) {
130 return false;
131 }
132
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700133 const char* action = add ? ADD : DEL;
134
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700135 Fwmark fwmark;
136 fwmark.permission = permission;
137
138 Fwmark mask;
139 mask.permission = permission;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700140
141 // A rule to route traffic based on a chosen outgoing interface.
142 //
143 // Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already
144 // knows the outgoing interface (typically for link-local communications).
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700145 if (!runIpRuleCommand(action, RULE_PRIORITY_PER_NETWORK_INTERFACE, table, fwmark.intValue,
146 mask.intValue, interface)) {
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700147 return false;
148 }
149
150 // A rule to route traffic based on the chosen network.
151 //
152 // This is for sockets that have not explicitly requested a particular network, but have been
153 // bound to one when they called connect(). This ensures that sockets connected on a particular
154 // network stay on that network even if the default network changes.
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700155 fwmark.netId = netId;
156 mask.netId = FWMARK_NET_ID_MASK;
157 if (!runIpRuleCommand(action, RULE_PRIORITY_PER_NETWORK_NORMAL, table, fwmark.intValue,
158 mask.intValue, NULL)) {
159 return false;
160 }
161
162 // A rule to route traffic based on an explicitly chosen network.
163 //
164 // Supports apps that use the multinetwork APIs to restrict their traffic to a network.
165 //
166 // We don't really need to check the permission bits of the fwmark here, as they would've been
167 // checked at the time the netId was set into the fwmark, but we do so to be consistent.
168 fwmark.explicitlySelected = true;
169 mask.explicitlySelected = true;
170 if (!runIpRuleCommand(action, RULE_PRIORITY_PER_NETWORK_EXPLICIT, table, fwmark.intValue,
171 mask.intValue, NULL)) {
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700172 return false;
173 }
174
175 // An iptables rule to mark incoming packets on a network with the netId of the network.
176 //
177 // This is so that the kernel can:
178 // + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors,
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700179 // ping replies).
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700180 // + Mark sockets that accept connections from this interface so that the connection stays on
181 // the same interface.
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700182 if (modifyIptables) {
183 action = add ? "-A" : "-D";
184 char markString[UINT32_HEX_STRLEN];
185 snprintf(markString, sizeof(markString), "0x%x", netId);
186 if (execIptables(V4V6, "-t", "mangle", action, "INPUT", "-i", interface, "-j", "MARK",
187 "--set-mark", markString, NULL)) {
188 return false;
189 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700190 }
191
192 return true;
193}
194
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700195bool modifyDefaultNetworkRules(const char* interface, Permission permission, const char* action) {
196 uint32_t table = getRouteTableForInterface(interface);
197 if (!table) {
198 return false;
199 }
200
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700201 Fwmark fwmark;
202 fwmark.netId = 0;
203 fwmark.permission = permission;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700204
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700205 Fwmark mask;
206 mask.netId = FWMARK_NET_ID_MASK;
207 mask.permission = permission;
208
209 return runIpRuleCommand(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
210 mask.intValue, NULL);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700211}
212
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700213bool modifyRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700214 const char* action, RouteController::TableType tableType, unsigned /* uid */) {
215 uint32_t table = 0;
216 switch (tableType) {
217 case RouteController::INTERFACE: {
218 table = getRouteTableForInterface(interface);
219 break;
220 }
221 case RouteController::LEGACY: {
222 // TODO: Use the UID to assign a unique table per UID instead of this fixed table.
223 table = ROUTE_TABLE_LEGACY;
224 break;
225 }
226 case RouteController::PRIVILEGED_LEGACY: {
227 // TODO: Use the UID to assign a unique table per UID instead of this fixed table.
228 table = ROUTE_TABLE_PRIVILEGED_LEGACY;
229 break;
230 }
231 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700232 if (!table) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700233 return false;
234 }
235
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700236 if (!runIpRouteCommand(action, table, interface, destination, nexthop)) {
237 return false;
238 }
239
240 // If there's no nexthop, this is a directly connected route. Add it to the main table also, to
241 // let the kernel find it when validating nexthops when global routes are added. Don't do this
242 // for IPv6, since all directly-connected routes in v6 are link-local and should already be in
243 // the main table.
Sreeram Ramachandranfd6424f2014-04-24 16:23:35 -0700244 // TODO: A failure here typically means that the route already exists in the main table, so we
245 // ignore it. It's wrong to ignore other kinds of failures, but we have no way to distinguish
246 // them based on the return status of the 'ip' command. Fix this situation by ignoring errors
247 // only when action == ADD && error == EEXIST.
248 if (!nexthop && !strchr(destination, ':')) {
249 runIpRouteCommand(action, RT_TABLE_MAIN, interface, destination, NULL);
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700250 }
251
252 return true;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700253}
254
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700255bool flushRoutes(const char* interface) {
256 uint32_t table = getRouteTableForInterface(interface);
257 if (!table) {
258 return false;
259 }
260
261 return runIpRouteCommand("flush", table, NULL, NULL, NULL);
262}
263
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700264} // namespace
265
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700266void RouteController::Init() {
267 // Add a new rule to look up the 'main' table, with the same selectors as the "default network"
268 // rule, but with a lower priority. Since the default network rule points to a table with a
269 // default route, the rule we're adding will never be used for normal routing lookups. However,
270 // the kernel may fall-through to it to find directly-connected routes when it validates that a
271 // nexthop (in a route being added) is reachable.
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700272 Fwmark fwmark;
273 fwmark.netId = 0;
274
275 Fwmark mask;
276 mask.netId = FWMARK_NET_ID_MASK;
277
278 runIpRuleCommand(ADD, RULE_PRIORITY_MAIN, RT_TABLE_MAIN, fwmark.intValue, mask.intValue, NULL);
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700279
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700280 // Add rules to allow lookup of legacy routes.
281 //
282 // TODO: Remove these once the kernel supports UID-based routing. Instead, add them on demand
283 // when routes are added.
284 fwmark.netId = 0;
285 mask.netId = 0;
286
287 fwmark.explicitlySelected = false;
288 mask.explicitlySelected = true;
289
290 runIpRuleCommand(ADD, RULE_PRIORITY_LEGACY, ROUTE_TABLE_LEGACY, fwmark.intValue, mask.intValue,
291 NULL);
292
293 fwmark.permission = PERMISSION_CONNECTIVITY_INTERNAL;
294 mask.permission = PERMISSION_CONNECTIVITY_INTERNAL;
295
296 runIpRuleCommand(ADD, RULE_PRIORITY_PRIVILEGED_LEGACY, ROUTE_TABLE_PRIVILEGED_LEGACY,
297 fwmark.intValue, mask.intValue, NULL);
298
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700299// TODO: Uncomment once we are sure everything works.
300#if 0
301 // Add a rule to preempt the pre-defined "from all lookup main" rule. This ensures that packets
302 // that are already marked with a specific NetId don't fall-through to the main table.
303 runIpRuleCommand(ADD, RULE_PRIORITY_UNREACHABLE, 0, 0, 0, NULL);
304#endif
305}
306
Paul Jensenae37e8a2014-04-28 10:35:51 -0400307bool RouteController::addInterfaceToNetwork(unsigned netId, const char* interface,
308 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700309 return modifyPerNetworkRules(netId, interface, permission, true, true);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700310}
311
Paul Jensenae37e8a2014-04-28 10:35:51 -0400312bool RouteController::removeInterfaceFromNetwork(unsigned netId, const char* interface,
313 Permission permission) {
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700314 return modifyPerNetworkRules(netId, interface, permission, false, true) &&
315 flushRoutes(interface);
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700316}
317
318bool RouteController::modifyNetworkPermission(unsigned netId, const char* interface,
319 Permission oldPermission, Permission newPermission) {
320 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700321 return modifyPerNetworkRules(netId, interface, newPermission, true, false) &&
322 modifyPerNetworkRules(netId, interface, oldPermission, false, false);
323}
324
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700325bool RouteController::addToDefaultNetwork(const char* interface, Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700326 return modifyDefaultNetworkRules(interface, permission, ADD);
327}
328
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700329bool RouteController::removeFromDefaultNetwork(const char* interface, Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700330 return modifyDefaultNetworkRules(interface, permission, DEL);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700331}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700332
333bool RouteController::addRoute(const char* interface, const char* destination,
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700334 const char* nexthop, TableType tableType, unsigned uid) {
335 return modifyRoute(interface, destination, nexthop, ADD, tableType, uid);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700336}
337
338bool RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700339 const char* nexthop, TableType tableType, unsigned uid) {
340 return modifyRoute(interface, destination, nexthop, DEL, tableType, uid);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700341}