Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | #include <logwrap/logwrap.h> |
| 23 | #include <net/if.h> |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 24 | |
| 25 | namespace { |
| 26 | |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 27 | const uint32_t RULE_PRIORITY_PER_NETWORK_EXPLICIT = 300; |
| 28 | const uint32_t RULE_PRIORITY_PER_NETWORK_INTERFACE = 400; |
| 29 | const uint32_t RULE_PRIORITY_PER_NETWORK_NORMAL = 700; |
| 30 | const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 900; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 31 | |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 32 | const bool FWMARK_USE_NET_ID = true; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 33 | const bool FWMARK_USE_EXPLICIT = true; |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 34 | const bool FWMARK_USE_PROTECT = true; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 35 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 36 | uint32_t getRouteTableForInterface(const char* interface) { |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 37 | uint32_t index = if_nametoindex(interface); |
| 38 | return index ? index + RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX : 0; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Lorenzo Colitti | a10ac32 | 2014-04-11 18:26:17 +0900 | [diff] [blame] | 41 | bool runIpRuleCommand(const char* action, uint32_t priority, uint32_t table, |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 42 | uint32_t fwmark, uint32_t mask, const char* interface) { |
Lorenzo Colitti | a10ac32 | 2014-04-11 18:26:17 +0900 | [diff] [blame] | 43 | char priorityString[UINT32_STRLEN]; |
Lorenzo Colitti | a10ac32 | 2014-04-11 18:26:17 +0900 | [diff] [blame] | 44 | snprintf(priorityString, sizeof(priorityString), "%u", priority); |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame^] | 45 | |
| 46 | char tableString[UINT32_STRLEN]; |
Lorenzo Colitti | a10ac32 | 2014-04-11 18:26:17 +0900 | [diff] [blame] | 47 | snprintf(tableString, sizeof(tableString), "%u", table); |
| 48 | |
| 49 | char fwmarkString[sizeof("0x12345678/0x12345678")]; |
| 50 | snprintf(fwmarkString, sizeof(fwmarkString), "0x%x/0x%x", fwmark, mask); |
| 51 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 52 | const char* version[] = {"-4", "-6"}; |
| 53 | for (size_t i = 0; i < ARRAY_SIZE(version); ++i) { |
| 54 | int argc = 0; |
| 55 | const char* argv[16]; |
| 56 | |
| 57 | argv[argc++] = IP_PATH; |
| 58 | argv[argc++] = version[i]; |
| 59 | argv[argc++] = "rule"; |
| 60 | argv[argc++] = action; |
| 61 | argv[argc++] = "priority"; |
Lorenzo Colitti | a10ac32 | 2014-04-11 18:26:17 +0900 | [diff] [blame] | 62 | argv[argc++] = priorityString; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 63 | argv[argc++] = "table"; |
Lorenzo Colitti | a10ac32 | 2014-04-11 18:26:17 +0900 | [diff] [blame] | 64 | argv[argc++] = tableString; |
| 65 | if (mask) { |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 66 | argv[argc++] = "fwmark"; |
Lorenzo Colitti | a10ac32 | 2014-04-11 18:26:17 +0900 | [diff] [blame] | 67 | argv[argc++] = fwmarkString; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 68 | } |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 69 | if (interface) { |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 70 | argv[argc++] = "oif"; |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 71 | argv[argc++] = interface; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 72 | } |
| 73 | if (android_fork_execvp(argc, const_cast<char**>(argv), NULL, false, false)) { |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame^] | 81 | bool runIpRouteCommand(const char* action, uint32_t table, const char* interface, |
| 82 | const char* destination, const char* nexthop) { |
| 83 | char tableString[UINT32_STRLEN]; |
| 84 | snprintf(tableString, sizeof(tableString), "%u", table); |
| 85 | |
| 86 | int argc = 0; |
| 87 | const char* argv[16]; |
| 88 | |
| 89 | argv[argc++] = IP_PATH; |
| 90 | argv[argc++] = "route"; |
| 91 | argv[argc++] = action; |
| 92 | argv[argc++] = "table"; |
| 93 | argv[argc++] = tableString; |
| 94 | if (destination) { |
| 95 | argv[argc++] = destination; |
| 96 | argv[argc++] = "dev"; |
| 97 | argv[argc++] = interface; |
| 98 | if (nexthop) { |
| 99 | argv[argc++] = "via"; |
| 100 | argv[argc++] = nexthop; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return android_fork_execvp(argc, const_cast<char**>(argv), NULL, false, false); |
| 105 | } |
| 106 | |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 107 | bool modifyPerNetworkRules(unsigned netId, const char* interface, Permission permission, bool add, |
| 108 | bool modifyIptables) { |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 109 | uint32_t table = getRouteTableForInterface(interface); |
| 110 | if (!table) { |
| 111 | return false; |
| 112 | } |
| 113 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 114 | const char* action = add ? ADD : DEL; |
| 115 | |
| 116 | // A rule to route traffic based on an explicitly chosen network. |
| 117 | // |
| 118 | // Supports apps that use the multinetwork APIs to restrict their traffic to a network. |
| 119 | // |
| 120 | // We don't really need to check the permission bits of the fwmark here, as they would've been |
| 121 | // checked at the time the netId was set into the fwmark, but we do so to be consistent. |
| 122 | uint32_t fwmark = getFwmark(netId, FWMARK_USE_EXPLICIT, !FWMARK_USE_PROTECT, permission); |
| 123 | uint32_t mask = getFwmarkMask(FWMARK_USE_NET_ID, FWMARK_USE_EXPLICIT, !FWMARK_USE_PROTECT, |
| 124 | permission); |
Lorenzo Colitti | a10ac32 | 2014-04-11 18:26:17 +0900 | [diff] [blame] | 125 | if (!runIpRuleCommand(action, RULE_PRIORITY_PER_NETWORK_EXPLICIT, table, fwmark, mask, NULL)) { |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | |
| 129 | // A rule to route traffic based on a chosen outgoing interface. |
| 130 | // |
| 131 | // Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already |
| 132 | // knows the outgoing interface (typically for link-local communications). |
| 133 | fwmark = getFwmark(0, !FWMARK_USE_EXPLICIT, !FWMARK_USE_PROTECT, permission); |
| 134 | mask = getFwmark(!FWMARK_USE_NET_ID, !FWMARK_USE_EXPLICIT, !FWMARK_USE_PROTECT, permission); |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 135 | if (!runIpRuleCommand(action, RULE_PRIORITY_PER_NETWORK_INTERFACE, table, fwmark, mask, |
| 136 | interface)) { |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
| 140 | // A rule to route traffic based on the chosen network. |
| 141 | // |
| 142 | // This is for sockets that have not explicitly requested a particular network, but have been |
| 143 | // bound to one when they called connect(). This ensures that sockets connected on a particular |
| 144 | // network stay on that network even if the default network changes. |
| 145 | fwmark = getFwmark(netId, !FWMARK_USE_EXPLICIT, !FWMARK_USE_PROTECT, permission); |
| 146 | mask = getFwmarkMask(FWMARK_USE_NET_ID, !FWMARK_USE_EXPLICIT, !FWMARK_USE_PROTECT, permission); |
Lorenzo Colitti | a10ac32 | 2014-04-11 18:26:17 +0900 | [diff] [blame] | 147 | if (!runIpRuleCommand(action, RULE_PRIORITY_PER_NETWORK_NORMAL, table, fwmark, mask, NULL)) { |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | |
| 151 | // An iptables rule to mark incoming packets on a network with the netId of the network. |
| 152 | // |
| 153 | // This is so that the kernel can: |
| 154 | // + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 155 | // ping replies). |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 156 | // + Mark sockets that accept connections from this interface so that the connection stays on |
| 157 | // the same interface. |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 158 | if (modifyIptables) { |
| 159 | action = add ? "-A" : "-D"; |
| 160 | char markString[UINT32_HEX_STRLEN]; |
| 161 | snprintf(markString, sizeof(markString), "0x%x", netId); |
| 162 | if (execIptables(V4V6, "-t", "mangle", action, "INPUT", "-i", interface, "-j", "MARK", |
| 163 | "--set-mark", markString, NULL)) { |
| 164 | return false; |
| 165 | } |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | return true; |
| 169 | } |
| 170 | |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 171 | bool modifyDefaultNetworkRules(const char* interface, Permission permission, const char* action) { |
| 172 | uint32_t table = getRouteTableForInterface(interface); |
| 173 | if (!table) { |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | uint32_t fwmark = getFwmark(0, !FWMARK_USE_EXPLICIT, !FWMARK_USE_PROTECT, permission); |
| 178 | uint32_t mask = getFwmarkMask(FWMARK_USE_NET_ID, !FWMARK_USE_EXPLICIT, !FWMARK_USE_PROTECT, |
| 179 | permission); |
| 180 | |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame^] | 181 | return runIpRuleCommand(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark, mask, NULL); |
| 182 | } |
| 183 | |
| 184 | bool modifyRoute(const char* interface, const char* destination, const char* nexthop, bool add) { |
| 185 | uint32_t table = getRouteTableForInterface(interface); |
| 186 | if (!table) { |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 187 | return false; |
| 188 | } |
| 189 | |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame^] | 190 | return runIpRouteCommand(add ? ADD : DEL, table, interface, destination, nexthop); |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 193 | } // namespace |
| 194 | |
| 195 | bool RouteController::createNetwork(unsigned netId, const char* interface, Permission permission) { |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 196 | return modifyPerNetworkRules(netId, interface, permission, true, true); |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | bool RouteController::destroyNetwork(unsigned netId, const char* interface, Permission permission) { |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 200 | return modifyPerNetworkRules(netId, interface, permission, false, true); |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 201 | // TODO: Flush the routing table. |
| 202 | } |
| 203 | |
| 204 | bool RouteController::modifyNetworkPermission(unsigned netId, const char* interface, |
| 205 | Permission oldPermission, Permission newPermission) { |
| 206 | // Add the new rules before deleting the old ones, to avoid race conditions. |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 207 | return modifyPerNetworkRules(netId, interface, newPermission, true, false) && |
| 208 | modifyPerNetworkRules(netId, interface, oldPermission, false, false); |
| 209 | } |
| 210 | |
| 211 | bool RouteController::addDefaultNetwork(const char* interface, Permission permission) { |
| 212 | return modifyDefaultNetworkRules(interface, permission, ADD); |
| 213 | } |
| 214 | |
| 215 | bool RouteController::removeDefaultNetwork(const char* interface, Permission permission) { |
| 216 | return modifyDefaultNetworkRules(interface, permission, DEL); |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 217 | } |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame^] | 218 | |
| 219 | bool RouteController::addRoute(const char* interface, const char* destination, |
| 220 | const char* nexthop) { |
| 221 | return modifyRoute(interface, destination, nexthop, true); |
| 222 | } |
| 223 | |
| 224 | bool RouteController::removeRoute(const char* interface, const char* destination, |
| 225 | const char* nexthop) { |
| 226 | return modifyRoute(interface, destination, nexthop, false); |
| 227 | } |