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" |
| 37 | #include "SecondaryTableController.h" |
| 38 | |
| 39 | static char IP_PATH[] = "/system/bin/ip"; |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 40 | static char ADD[] = "add"; |
| 41 | static char DEL[] = "del"; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 42 | |
| 43 | SecondaryTableController::SecondaryTableController() { |
| 44 | int i; |
| 45 | for (i=0; i < INTERFACES_TRACKED; i++) { |
| 46 | mInterfaceTable[i][0] = 0; |
| 47 | // TODO - use a hashtable or other prebuilt container class |
| 48 | mInterfaceRuleCount[i] = 0; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | SecondaryTableController::~SecondaryTableController() { |
| 53 | } |
| 54 | |
| 55 | int SecondaryTableController::findTableNumber(const char *iface) { |
| 56 | int i; |
| 57 | for (i = 0; i < INTERFACES_TRACKED; i++) { |
| 58 | if (strncmp(iface, mInterfaceTable[i], MAX_IFACE_LENGTH) == 0) { |
| 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) { |
| 71 | LOGE("Max number of NATed interfaces reached"); |
| 72 | errno = ENODEV; |
| 73 | cli->sendMsg(ResponseCode::OperationFailed, "Max number NATed", true); |
| 74 | return -1; |
| 75 | } |
| 76 | strncpy(mInterfaceTable[tableIndex], iface, MAX_IFACE_LENGTH); |
| 77 | } |
| 78 | |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 79 | return modifyRoute(cli, ADD, iface, dest, prefix, gateway, tableIndex); |
| 80 | } |
| 81 | |
| 82 | int SecondaryTableController::modifyRoute(SocketClient *cli, char *action, char *iface, char *dest, |
| 83 | int prefix, char *gateway, int tableIndex) { |
| 84 | char *cmd; |
| 85 | |
| 86 | if (strcmp("::", gateway) == 0) { |
| 87 | // IP tool doesn't like "::" - the equiv of 0.0.0.0 that it accepts for ipv4 |
| 88 | asprintf(&cmd, "%s route %s %s/%d dev %s table %d", |
| 89 | IP_PATH, action, dest, prefix, iface, tableIndex+BASE_TABLE_NUMBER); |
| 90 | } else { |
| 91 | asprintf(&cmd, "%s route %s %s/%d via %s dev %s table %d", |
| 92 | IP_PATH, action, dest, prefix, gateway, iface, tableIndex+BASE_TABLE_NUMBER); |
| 93 | } |
| 94 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 95 | if (runAndFree(cli, cmd)) { |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 96 | LOGE("ip route %s failed: %s route %s %s/%d via %s dev %s table %d", action, |
| 97 | IP_PATH, action, dest, prefix, gateway, iface, tableIndex+BASE_TABLE_NUMBER); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 98 | errno = ENODEV; |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 99 | cli->sendMsg(ResponseCode::OperationFailed, "ip route modification failed", true); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 100 | return -1; |
| 101 | } |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 102 | |
| 103 | if (strcmp(action, ADD) == 0) { |
| 104 | mInterfaceRuleCount[tableIndex]++; |
| 105 | } else { |
| 106 | if (--mInterfaceRuleCount[tableIndex] < 1) { |
| 107 | mInterfaceRuleCount[tableIndex] = 0; |
| 108 | mInterfaceTable[tableIndex][0] = 0; |
| 109 | } |
| 110 | } |
| 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 | |
| 115 | int SecondaryTableController::removeRoute(SocketClient *cli, char *iface, char *dest, int prefix, |
| 116 | char *gateway) { |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 117 | int tableIndex = findTableNumber(iface); |
| 118 | if (tableIndex == -1) { |
| 119 | LOGE("Interface not found"); |
| 120 | errno = ENODEV; |
| 121 | cli->sendMsg(ResponseCode::OperationFailed, "Interface not found", true); |
| 122 | return -1; |
| 123 | } |
| 124 | |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 125 | return modifyRoute(cli, DEL, iface, dest, prefix, gateway, tableIndex); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | int SecondaryTableController::runAndFree(SocketClient *cli, char *cmd) { |
| 129 | int ret = 0; |
| 130 | if (strlen(cmd) >= 255) { |
| 131 | LOGE("ip command (%s) too long", cmd); |
| 132 | errno = E2BIG; |
| 133 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Too long", true); |
| 134 | free(cmd); |
| 135 | return -1; |
| 136 | } |
JP Abgrall | 9e5e0ce | 2011-12-14 15:20:59 -0800 | [diff] [blame] | 137 | ret = system_nosh(cmd); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 138 | free(cmd); |
| 139 | return ret; |
| 140 | } |