Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 <stdlib.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
| 26 | |
| 27 | #include <netinet/in.h> |
| 28 | #include <arpa/inet.h> |
| 29 | |
| 30 | #define LOG_TAG "SecondaryTablController" |
| 31 | #include <cutils/log.h> |
| 32 | #include <cutils/properties.h> |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 33 | #include <logwrap/logwrap.h> |
JP Abgrall | 9e5e0ce | 2011-12-14 15:20:59 -0800 | [diff] [blame] | 34 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 35 | #include "ResponseCode.h" |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 36 | #include "NetdConstants.h" |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 37 | #include "SecondaryTableController.h" |
| 38 | |
Chad Brubaker | 9a50889 | 2013-05-31 20:51:46 -0700 | [diff] [blame] | 39 | const char* SecondaryTableController::LOCAL_MANGLE_OUTPUT = "st_mangle_OUTPUT"; |
Chad Brubaker | 7a6ce4b | 2013-06-06 21:42:53 -0700 | [diff] [blame] | 40 | const char* SecondaryTableController::LOCAL_NAT_POSTROUTING = "st_nat_POSTROUTING"; |
Chad Brubaker | 9a50889 | 2013-05-31 20:51:46 -0700 | [diff] [blame] | 41 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 42 | SecondaryTableController::SecondaryTableController() { |
| 43 | int i; |
| 44 | for (i=0; i < INTERFACES_TRACKED; i++) { |
| 45 | mInterfaceTable[i][0] = 0; |
| 46 | // TODO - use a hashtable or other prebuilt container class |
| 47 | mInterfaceRuleCount[i] = 0; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | SecondaryTableController::~SecondaryTableController() { |
| 52 | } |
| 53 | |
| 54 | int SecondaryTableController::findTableNumber(const char *iface) { |
| 55 | int i; |
| 56 | for (i = 0; i < INTERFACES_TRACKED; i++) { |
Jaime A Lopez-Sollano | 3c20787 | 2012-01-11 16:29:28 -0800 | [diff] [blame] | 57 | // compare through the final null, hence +1 |
| 58 | if (strncmp(iface, mInterfaceTable[i], IFNAMSIZ + 1) == 0) { |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 59 | return i; |
| 60 | } |
| 61 | } |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | int SecondaryTableController::addRoute(SocketClient *cli, char *iface, char *dest, int prefix, |
| 66 | char *gateway) { |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 67 | int tableIndex = findTableNumber(iface); |
| 68 | if (tableIndex == -1) { |
| 69 | tableIndex = findTableNumber(""); // look for an empty slot |
| 70 | if (tableIndex == -1) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 71 | ALOGE("Max number of NATed interfaces reached"); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 72 | errno = ENODEV; |
| 73 | cli->sendMsg(ResponseCode::OperationFailed, "Max number NATed", true); |
| 74 | return -1; |
| 75 | } |
Jaime A Lopez-Sollano | d14fd4f | 2012-01-11 16:29:28 -0800 | [diff] [blame] | 76 | strncpy(mInterfaceTable[tableIndex], iface, IFNAMSIZ); |
| 77 | // Ensure null termination even if truncation happened |
| 78 | mInterfaceTable[tableIndex][IFNAMSIZ] = 0; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 81 | return modifyRoute(cli, ADD, iface, dest, prefix, gateway, tableIndex); |
| 82 | } |
| 83 | |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 84 | int SecondaryTableController::modifyRoute(SocketClient *cli, const char *action, char *iface, |
| 85 | char *dest, int prefix, char *gateway, int tableIndex) { |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 86 | char dest_str[44]; // enough to store an IPv6 address + 3 character bitmask |
| 87 | char tableIndex_str[11]; |
| 88 | int ret; |
| 89 | |
| 90 | // IP tool doesn't like "::" - the equiv of 0.0.0.0 that it accepts for ipv4 |
| 91 | snprintf(dest_str, sizeof(dest_str), "%s/%d", dest, prefix); |
| 92 | snprintf(tableIndex_str, sizeof(tableIndex_str), "%d", tableIndex + BASE_TABLE_NUMBER); |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 93 | |
| 94 | if (strcmp("::", gateway) == 0) { |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 95 | const char *cmd[] = { |
| 96 | IP_PATH, |
| 97 | "route", |
| 98 | action, |
| 99 | dest_str, |
| 100 | "dev", |
| 101 | iface, |
| 102 | "table", |
| 103 | tableIndex_str |
| 104 | }; |
| 105 | ret = runCmd(ARRAY_SIZE(cmd), cmd); |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 106 | } else { |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 107 | const char *cmd[] = { |
| 108 | IP_PATH, |
| 109 | "route", |
| 110 | action, |
| 111 | dest_str, |
| 112 | "via", |
| 113 | gateway, |
| 114 | "dev", |
| 115 | iface, |
| 116 | "table", |
| 117 | tableIndex_str |
| 118 | }; |
| 119 | ret = runCmd(ARRAY_SIZE(cmd), cmd); |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 122 | if (ret) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 123 | ALOGE("ip route %s failed: %s route %s %s/%d via %s dev %s table %d", action, |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 124 | IP_PATH, action, dest, prefix, gateway, iface, tableIndex+BASE_TABLE_NUMBER); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 125 | errno = ENODEV; |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 126 | cli->sendMsg(ResponseCode::OperationFailed, "ip route modification failed", true); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 127 | return -1; |
| 128 | } |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 129 | |
| 130 | if (strcmp(action, ADD) == 0) { |
| 131 | mInterfaceRuleCount[tableIndex]++; |
| 132 | } else { |
| 133 | if (--mInterfaceRuleCount[tableIndex] < 1) { |
| 134 | mInterfaceRuleCount[tableIndex] = 0; |
| 135 | mInterfaceTable[tableIndex][0] = 0; |
| 136 | } |
| 137 | } |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 138 | modifyRuleCount(tableIndex, action); |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 139 | cli->sendMsg(ResponseCode::CommandOkay, "Route modified", false); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 143 | void SecondaryTableController::modifyRuleCount(int tableIndex, const char *action) { |
| 144 | if (strcmp(action, ADD) == 0) { |
| 145 | mInterfaceRuleCount[tableIndex]++; |
| 146 | } else { |
| 147 | if (--mInterfaceRuleCount[tableIndex] < 1) { |
| 148 | mInterfaceRuleCount[tableIndex] = 0; |
| 149 | mInterfaceTable[tableIndex][0] = 0; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | int SecondaryTableController::verifyTableIndex(int tableIndex) { |
| 155 | if ((tableIndex < 0) || |
| 156 | (tableIndex >= INTERFACES_TRACKED) || |
| 157 | (mInterfaceTable[tableIndex][0] == 0)) { |
| 158 | return -1; |
| 159 | } else { |
| 160 | return 0; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | const char *SecondaryTableController::getVersion(const char *addr) { |
| 165 | if (strchr(addr, ':') != NULL) { |
| 166 | return "-6"; |
| 167 | } else { |
| 168 | return "-4"; |
| 169 | } |
| 170 | } |
| 171 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 172 | int SecondaryTableController::removeRoute(SocketClient *cli, char *iface, char *dest, int prefix, |
| 173 | char *gateway) { |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 174 | int tableIndex = findTableNumber(iface); |
| 175 | if (tableIndex == -1) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 176 | ALOGE("Interface not found"); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 177 | errno = ENODEV; |
| 178 | cli->sendMsg(ResponseCode::OperationFailed, "Interface not found", true); |
| 179 | return -1; |
| 180 | } |
| 181 | |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 182 | return modifyRoute(cli, DEL, iface, dest, prefix, gateway, tableIndex); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 185 | int SecondaryTableController::modifyFromRule(int tableIndex, const char *action, |
| 186 | const char *addr) { |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 187 | char tableIndex_str[11]; |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 188 | |
| 189 | if (verifyTableIndex(tableIndex)) { |
| 190 | return -1; |
| 191 | } |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 192 | |
| 193 | snprintf(tableIndex_str, sizeof(tableIndex_str), "%d", tableIndex + |
| 194 | BASE_TABLE_NUMBER); |
| 195 | const char *cmd[] = { |
| 196 | IP_PATH, |
| 197 | getVersion(addr), |
| 198 | "rule", |
| 199 | action, |
| 200 | "from", |
| 201 | addr, |
| 202 | "table", |
| 203 | tableIndex_str |
| 204 | }; |
| 205 | if (runCmd(ARRAY_SIZE(cmd), cmd)) { |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | modifyRuleCount(tableIndex, action); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | int SecondaryTableController::modifyLocalRoute(int tableIndex, const char *action, |
| 214 | const char *iface, const char *addr) { |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 215 | char tableIndex_str[11]; |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 216 | |
| 217 | if (verifyTableIndex(tableIndex)) { |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | modifyRuleCount(tableIndex, action); // some del's will fail as the iface is already gone. |
| 222 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 223 | snprintf(tableIndex_str, sizeof(tableIndex_str), "%d", tableIndex + |
| 224 | BASE_TABLE_NUMBER); |
| 225 | const char *cmd[] = { |
| 226 | IP_PATH, |
| 227 | "route", |
| 228 | action, |
| 229 | addr, |
| 230 | "dev", |
| 231 | iface, |
| 232 | "table", |
| 233 | tableIndex_str |
| 234 | }; |
| 235 | |
| 236 | return runCmd(ARRAY_SIZE(cmd), cmd); |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 237 | } |
Chad Brubaker | 7a6ce4b | 2013-06-06 21:42:53 -0700 | [diff] [blame] | 238 | int SecondaryTableController::addFwmarkRule(const char *iface) { |
| 239 | return setFwmarkRule(iface, true); |
| 240 | } |
| 241 | |
| 242 | int SecondaryTableController::removeFwmarkRule(const char *iface) { |
| 243 | return setFwmarkRule(iface, false); |
| 244 | } |
| 245 | |
| 246 | int SecondaryTableController::setFwmarkRule(const char *iface, bool add) { |
| 247 | char tableIndex_str[11]; |
| 248 | int tableIndex = findTableNumber(iface); |
| 249 | if (tableIndex == -1) { |
| 250 | tableIndex = findTableNumber(""); // look for an empty slot |
| 251 | if (tableIndex == -1) { |
| 252 | ALOGE("Max number of NATed interfaces reached"); |
| 253 | errno = ENODEV; |
| 254 | return -1; |
| 255 | } |
| 256 | strncpy(mInterfaceTable[tableIndex], iface, IFNAMSIZ); |
| 257 | // Ensure null termination even if truncation happened |
| 258 | mInterfaceTable[tableIndex][IFNAMSIZ] = 0; |
| 259 | } |
| 260 | snprintf(tableIndex_str, sizeof(tableIndex_str), "%d", tableIndex + |
| 261 | BASE_TABLE_NUMBER); |
| 262 | const char *cmd[] = { |
| 263 | IP_PATH, |
| 264 | "rule", |
| 265 | add ? "add" : "del", |
| 266 | "fwmark", |
| 267 | tableIndex_str, |
| 268 | "table", |
| 269 | tableIndex_str |
| 270 | }; |
| 271 | int ret = runCmd(ARRAY_SIZE(cmd), cmd); |
| 272 | if (ret) return ret; |
| 273 | |
| 274 | //set up the needed source IP rewriting |
| 275 | //NOTE: Without ipv6 NAT in the kernel <3.7 only support V4 NAT |
| 276 | return execIptables(V4, |
| 277 | "-t", |
| 278 | "nat", |
| 279 | add ? "-A" : "-D", |
| 280 | LOCAL_NAT_POSTROUTING, |
| 281 | "-o", |
| 282 | iface, |
| 283 | "-m", |
| 284 | "mark", |
| 285 | "--mark", |
| 286 | tableIndex_str, |
| 287 | "-j", |
| 288 | "MASQUERADE", |
| 289 | NULL); |
| 290 | |
| 291 | } |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 292 | |
Chad Brubaker | 8830b94 | 2013-06-12 10:51:55 -0700 | [diff] [blame^] | 293 | int SecondaryTableController::addUidRule(const char *iface, int uid_start, int uid_end) { |
| 294 | return setUidRule(iface, uid_start, uid_end, true); |
Chad Brubaker | 9a50889 | 2013-05-31 20:51:46 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Chad Brubaker | 8830b94 | 2013-06-12 10:51:55 -0700 | [diff] [blame^] | 297 | int SecondaryTableController::removeUidRule(const char *iface, int uid_start, int uid_end) { |
| 298 | return setUidRule(iface, uid_start, uid_end, false); |
Chad Brubaker | 9a50889 | 2013-05-31 20:51:46 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Chad Brubaker | 8830b94 | 2013-06-12 10:51:55 -0700 | [diff] [blame^] | 301 | int SecondaryTableController::setUidRule(const char *iface, int uid_start, int uid_end, bool add) { |
Chad Brubaker | 9a50889 | 2013-05-31 20:51:46 -0700 | [diff] [blame] | 302 | int tableIndex = findTableNumber(iface); |
| 303 | if (tableIndex == -1) { |
| 304 | return -1; |
| 305 | } |
| 306 | char tableIndex_str[11] = {0}; |
| 307 | snprintf(tableIndex_str, sizeof(tableIndex_str), "%d", tableIndex + BASE_TABLE_NUMBER); |
Chad Brubaker | 8830b94 | 2013-06-12 10:51:55 -0700 | [diff] [blame^] | 308 | char uid_str[24] = {0}; |
| 309 | snprintf(uid_str, sizeof(uid_str), "%d-%d", uid_start, uid_end); |
Chad Brubaker | 9a50889 | 2013-05-31 20:51:46 -0700 | [diff] [blame] | 310 | return execIptables(V4V6, |
| 311 | "-t", |
| 312 | "mangle", |
| 313 | add ? "-A" : "-D", |
| 314 | LOCAL_MANGLE_OUTPUT, |
| 315 | "-m", |
| 316 | "owner", |
| 317 | "--uid-owner", |
Chad Brubaker | 8830b94 | 2013-06-12 10:51:55 -0700 | [diff] [blame^] | 318 | uid_str, |
Chad Brubaker | 9a50889 | 2013-05-31 20:51:46 -0700 | [diff] [blame] | 319 | "-j", |
| 320 | "MARK", |
| 321 | "--set-mark", |
| 322 | tableIndex_str, |
| 323 | NULL); |
| 324 | } |
| 325 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 326 | int SecondaryTableController::runCmd(int argc, const char **argv) { |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 327 | int ret = 0; |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 328 | |
| 329 | ret = android_fork_execvp(argc, (char **)argv, NULL, false, false); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 330 | return ret; |
| 331 | } |