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> |
| 33 | |
JP Abgrall | 9e5e0ce | 2011-12-14 15:20:59 -0800 | [diff] [blame] | 34 | extern "C" int system_nosh(const char *command); |
| 35 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 36 | #include "ResponseCode.h" |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 37 | #include "NetdConstants.h" |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 38 | #include "SecondaryTableController.h" |
| 39 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 40 | SecondaryTableController::SecondaryTableController() { |
| 41 | int i; |
| 42 | for (i=0; i < INTERFACES_TRACKED; i++) { |
| 43 | mInterfaceTable[i][0] = 0; |
| 44 | // TODO - use a hashtable or other prebuilt container class |
| 45 | mInterfaceRuleCount[i] = 0; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | SecondaryTableController::~SecondaryTableController() { |
| 50 | } |
| 51 | |
| 52 | int SecondaryTableController::findTableNumber(const char *iface) { |
| 53 | int i; |
| 54 | for (i = 0; i < INTERFACES_TRACKED; i++) { |
Jaime A Lopez-Sollano | d14fd4f | 2012-01-11 16:29:28 -0800 | [diff] [blame^] | 55 | if (strncmp(iface, mInterfaceTable[i], IFNAMSIZ) == 0) { |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 56 | return i; |
| 57 | } |
| 58 | } |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | int SecondaryTableController::addRoute(SocketClient *cli, char *iface, char *dest, int prefix, |
| 63 | char *gateway) { |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 64 | int tableIndex = findTableNumber(iface); |
| 65 | if (tableIndex == -1) { |
| 66 | tableIndex = findTableNumber(""); // look for an empty slot |
| 67 | if (tableIndex == -1) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 68 | ALOGE("Max number of NATed interfaces reached"); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 69 | errno = ENODEV; |
| 70 | cli->sendMsg(ResponseCode::OperationFailed, "Max number NATed", true); |
| 71 | return -1; |
| 72 | } |
Jaime A Lopez-Sollano | d14fd4f | 2012-01-11 16:29:28 -0800 | [diff] [blame^] | 73 | strncpy(mInterfaceTable[tableIndex], iface, IFNAMSIZ); |
| 74 | // Ensure null termination even if truncation happened |
| 75 | mInterfaceTable[tableIndex][IFNAMSIZ] = 0; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 78 | return modifyRoute(cli, ADD, iface, dest, prefix, gateway, tableIndex); |
| 79 | } |
| 80 | |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 81 | int SecondaryTableController::modifyRoute(SocketClient *cli, const char *action, char *iface, |
| 82 | char *dest, int prefix, char *gateway, int tableIndex) { |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 83 | char *cmd; |
| 84 | |
| 85 | if (strcmp("::", gateway) == 0) { |
| 86 | // IP tool doesn't like "::" - the equiv of 0.0.0.0 that it accepts for ipv4 |
| 87 | asprintf(&cmd, "%s route %s %s/%d dev %s table %d", |
| 88 | IP_PATH, action, dest, prefix, iface, tableIndex+BASE_TABLE_NUMBER); |
| 89 | } else { |
| 90 | asprintf(&cmd, "%s route %s %s/%d via %s dev %s table %d", |
| 91 | IP_PATH, action, dest, prefix, gateway, iface, tableIndex+BASE_TABLE_NUMBER); |
| 92 | } |
| 93 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 94 | if (runAndFree(cli, cmd)) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 95 | 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] | 96 | IP_PATH, action, dest, prefix, gateway, iface, tableIndex+BASE_TABLE_NUMBER); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 97 | errno = ENODEV; |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 98 | cli->sendMsg(ResponseCode::OperationFailed, "ip route modification failed", true); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 99 | return -1; |
| 100 | } |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 101 | |
| 102 | if (strcmp(action, ADD) == 0) { |
| 103 | mInterfaceRuleCount[tableIndex]++; |
| 104 | } else { |
| 105 | if (--mInterfaceRuleCount[tableIndex] < 1) { |
| 106 | mInterfaceRuleCount[tableIndex] = 0; |
| 107 | mInterfaceTable[tableIndex][0] = 0; |
| 108 | } |
| 109 | } |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 110 | modifyRuleCount(tableIndex, action); |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 111 | cli->sendMsg(ResponseCode::CommandOkay, "Route modified", false); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 115 | void SecondaryTableController::modifyRuleCount(int tableIndex, const char *action) { |
| 116 | if (strcmp(action, ADD) == 0) { |
| 117 | mInterfaceRuleCount[tableIndex]++; |
| 118 | } else { |
| 119 | if (--mInterfaceRuleCount[tableIndex] < 1) { |
| 120 | mInterfaceRuleCount[tableIndex] = 0; |
| 121 | mInterfaceTable[tableIndex][0] = 0; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | int SecondaryTableController::verifyTableIndex(int tableIndex) { |
| 127 | if ((tableIndex < 0) || |
| 128 | (tableIndex >= INTERFACES_TRACKED) || |
| 129 | (mInterfaceTable[tableIndex][0] == 0)) { |
| 130 | return -1; |
| 131 | } else { |
| 132 | return 0; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | const char *SecondaryTableController::getVersion(const char *addr) { |
| 137 | if (strchr(addr, ':') != NULL) { |
| 138 | return "-6"; |
| 139 | } else { |
| 140 | return "-4"; |
| 141 | } |
| 142 | } |
| 143 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 144 | int SecondaryTableController::removeRoute(SocketClient *cli, char *iface, char *dest, int prefix, |
| 145 | char *gateway) { |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 146 | int tableIndex = findTableNumber(iface); |
| 147 | if (tableIndex == -1) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 148 | ALOGE("Interface not found"); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 149 | errno = ENODEV; |
| 150 | cli->sendMsg(ResponseCode::OperationFailed, "Interface not found", true); |
| 151 | return -1; |
| 152 | } |
| 153 | |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 154 | return modifyRoute(cli, DEL, iface, dest, prefix, gateway, tableIndex); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 157 | int SecondaryTableController::modifyFromRule(int tableIndex, const char *action, |
| 158 | const char *addr) { |
| 159 | char *cmd; |
| 160 | |
| 161 | if (verifyTableIndex(tableIndex)) { |
| 162 | return -1; |
| 163 | } |
| 164 | asprintf(&cmd, "%s %s rule %s from %s table %d", IP_PATH, getVersion(addr), |
| 165 | action, addr, tableIndex + BASE_TABLE_NUMBER); |
| 166 | if (runAndFree(NULL, cmd)) { |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | modifyRuleCount(tableIndex, action); |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | int SecondaryTableController::modifyLocalRoute(int tableIndex, const char *action, |
| 175 | const char *iface, const char *addr) { |
| 176 | char *cmd; |
| 177 | |
| 178 | if (verifyTableIndex(tableIndex)) { |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | modifyRuleCount(tableIndex, action); // some del's will fail as the iface is already gone. |
| 183 | |
| 184 | asprintf(&cmd, "%s route %s %s dev %s table %d", IP_PATH, action, addr, iface, |
| 185 | tableIndex+BASE_TABLE_NUMBER); |
| 186 | return runAndFree(NULL, cmd); |
| 187 | } |
| 188 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 189 | int SecondaryTableController::runAndFree(SocketClient *cli, char *cmd) { |
| 190 | int ret = 0; |
| 191 | if (strlen(cmd) >= 255) { |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 192 | if (cli != NULL) { |
| 193 | ALOGE("ip command (%s) too long", cmd); |
| 194 | errno = E2BIG; |
| 195 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Too long", true); |
| 196 | } |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 197 | free(cmd); |
| 198 | return -1; |
| 199 | } |
JP Abgrall | 9e5e0ce | 2011-12-14 15:20:59 -0800 | [diff] [blame] | 200 | ret = system_nosh(cmd); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 201 | free(cmd); |
| 202 | return ret; |
| 203 | } |