blob: 5939924ce888bc212cc76b46cc5f61ad843d664d [file] [log] [blame]
Robert Greenwaltfc97b822011-11-02 16:48:36 -07001/*
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 Abgrall9e5e0ce2011-12-14 15:20:59 -080034extern "C" int system_nosh(const char *command);
35
Robert Greenwaltfc97b822011-11-02 16:48:36 -070036#include "ResponseCode.h"
Robert Greenwaltc4621772012-01-31 12:46:45 -080037#include "NetdConstants.h"
Robert Greenwaltfc97b822011-11-02 16:48:36 -070038#include "SecondaryTableController.h"
39
Robert Greenwaltfc97b822011-11-02 16:48:36 -070040SecondaryTableController::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
49SecondaryTableController::~SecondaryTableController() {
50}
51
52int SecondaryTableController::findTableNumber(const char *iface) {
53 int i;
54 for (i = 0; i < INTERFACES_TRACKED; i++) {
55 if (strncmp(iface, mInterfaceTable[i], MAX_IFACE_LENGTH) == 0) {
56 return i;
57 }
58 }
59 return -1;
60}
61
62int SecondaryTableController::addRoute(SocketClient *cli, char *iface, char *dest, int prefix,
63 char *gateway) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -070064 int tableIndex = findTableNumber(iface);
65 if (tableIndex == -1) {
66 tableIndex = findTableNumber(""); // look for an empty slot
67 if (tableIndex == -1) {
Steve Block5ea0c052012-01-06 19:18:11 +000068 ALOGE("Max number of NATed interfaces reached");
Robert Greenwaltfc97b822011-11-02 16:48:36 -070069 errno = ENODEV;
70 cli->sendMsg(ResponseCode::OperationFailed, "Max number NATed", true);
71 return -1;
72 }
73 strncpy(mInterfaceTable[tableIndex], iface, MAX_IFACE_LENGTH);
74 }
75
Robert Greenwalt063af322011-11-18 15:32:13 -080076 return modifyRoute(cli, ADD, iface, dest, prefix, gateway, tableIndex);
77}
78
Robert Greenwaltc4621772012-01-31 12:46:45 -080079int SecondaryTableController::modifyRoute(SocketClient *cli, const char *action, char *iface,
80 char *dest, int prefix, char *gateway, int tableIndex) {
Robert Greenwalt063af322011-11-18 15:32:13 -080081 char *cmd;
82
83 if (strcmp("::", gateway) == 0) {
84 // IP tool doesn't like "::" - the equiv of 0.0.0.0 that it accepts for ipv4
85 asprintf(&cmd, "%s route %s %s/%d dev %s table %d",
86 IP_PATH, action, dest, prefix, iface, tableIndex+BASE_TABLE_NUMBER);
87 } else {
88 asprintf(&cmd, "%s route %s %s/%d via %s dev %s table %d",
89 IP_PATH, action, dest, prefix, gateway, iface, tableIndex+BASE_TABLE_NUMBER);
90 }
91
Robert Greenwaltfc97b822011-11-02 16:48:36 -070092 if (runAndFree(cli, cmd)) {
Steve Block5ea0c052012-01-06 19:18:11 +000093 ALOGE("ip route %s failed: %s route %s %s/%d via %s dev %s table %d", action,
Robert Greenwalt063af322011-11-18 15:32:13 -080094 IP_PATH, action, dest, prefix, gateway, iface, tableIndex+BASE_TABLE_NUMBER);
Robert Greenwaltfc97b822011-11-02 16:48:36 -070095 errno = ENODEV;
Robert Greenwalt063af322011-11-18 15:32:13 -080096 cli->sendMsg(ResponseCode::OperationFailed, "ip route modification failed", true);
Robert Greenwaltfc97b822011-11-02 16:48:36 -070097 return -1;
98 }
Robert Greenwalt063af322011-11-18 15:32:13 -080099
100 if (strcmp(action, ADD) == 0) {
101 mInterfaceRuleCount[tableIndex]++;
102 } else {
103 if (--mInterfaceRuleCount[tableIndex] < 1) {
104 mInterfaceRuleCount[tableIndex] = 0;
105 mInterfaceTable[tableIndex][0] = 0;
106 }
107 }
Robert Greenwaltc4621772012-01-31 12:46:45 -0800108 modifyRuleCount(tableIndex, action);
Robert Greenwalt063af322011-11-18 15:32:13 -0800109 cli->sendMsg(ResponseCode::CommandOkay, "Route modified", false);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700110 return 0;
111}
112
Robert Greenwaltc4621772012-01-31 12:46:45 -0800113void SecondaryTableController::modifyRuleCount(int tableIndex, const char *action) {
114 if (strcmp(action, ADD) == 0) {
115 mInterfaceRuleCount[tableIndex]++;
116 } else {
117 if (--mInterfaceRuleCount[tableIndex] < 1) {
118 mInterfaceRuleCount[tableIndex] = 0;
119 mInterfaceTable[tableIndex][0] = 0;
120 }
121 }
122}
123
124int SecondaryTableController::verifyTableIndex(int tableIndex) {
125 if ((tableIndex < 0) ||
126 (tableIndex >= INTERFACES_TRACKED) ||
127 (mInterfaceTable[tableIndex][0] == 0)) {
128 return -1;
129 } else {
130 return 0;
131 }
132}
133
134const char *SecondaryTableController::getVersion(const char *addr) {
135 if (strchr(addr, ':') != NULL) {
136 return "-6";
137 } else {
138 return "-4";
139 }
140}
141
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700142int SecondaryTableController::removeRoute(SocketClient *cli, char *iface, char *dest, int prefix,
143 char *gateway) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700144 int tableIndex = findTableNumber(iface);
145 if (tableIndex == -1) {
Steve Block5ea0c052012-01-06 19:18:11 +0000146 ALOGE("Interface not found");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700147 errno = ENODEV;
148 cli->sendMsg(ResponseCode::OperationFailed, "Interface not found", true);
149 return -1;
150 }
151
Robert Greenwalt063af322011-11-18 15:32:13 -0800152 return modifyRoute(cli, DEL, iface, dest, prefix, gateway, tableIndex);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700153}
154
Robert Greenwaltc4621772012-01-31 12:46:45 -0800155int SecondaryTableController::modifyFromRule(int tableIndex, const char *action,
156 const char *addr) {
157 char *cmd;
158
159 if (verifyTableIndex(tableIndex)) {
160 return -1;
161 }
162 asprintf(&cmd, "%s %s rule %s from %s table %d", IP_PATH, getVersion(addr),
163 action, addr, tableIndex + BASE_TABLE_NUMBER);
164 if (runAndFree(NULL, cmd)) {
165 return -1;
166 }
167
168 modifyRuleCount(tableIndex, action);
169 return 0;
170}
171
172int SecondaryTableController::modifyLocalRoute(int tableIndex, const char *action,
173 const char *iface, const char *addr) {
174 char *cmd;
175
176 if (verifyTableIndex(tableIndex)) {
177 return -1;
178 }
179
180 modifyRuleCount(tableIndex, action); // some del's will fail as the iface is already gone.
181
182 asprintf(&cmd, "%s route %s %s dev %s table %d", IP_PATH, action, addr, iface,
183 tableIndex+BASE_TABLE_NUMBER);
184 return runAndFree(NULL, cmd);
185}
186
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700187int SecondaryTableController::runAndFree(SocketClient *cli, char *cmd) {
188 int ret = 0;
189 if (strlen(cmd) >= 255) {
Robert Greenwaltc4621772012-01-31 12:46:45 -0800190 if (cli != NULL) {
191 ALOGE("ip command (%s) too long", cmd);
192 errno = E2BIG;
193 cli->sendMsg(ResponseCode::CommandSyntaxError, "Too long", true);
194 }
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700195 free(cmd);
196 return -1;
197 }
JP Abgrall9e5e0ce2011-12-14 15:20:59 -0800198 ret = system_nosh(cmd);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700199 free(cmd);
200 return ret;
201}