blob: 7d3de382892913042cd16b3e6d8d947fbf307053 [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++) {
Jaime A Lopez-Sollano3c207872012-01-11 16:29:28 -080055 // compare through the final null, hence +1
56 if (strncmp(iface, mInterfaceTable[i], IFNAMSIZ + 1) == 0) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -070057 return i;
58 }
59 }
60 return -1;
61}
62
63int SecondaryTableController::addRoute(SocketClient *cli, char *iface, char *dest, int prefix,
64 char *gateway) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -070065 int tableIndex = findTableNumber(iface);
66 if (tableIndex == -1) {
67 tableIndex = findTableNumber(""); // look for an empty slot
68 if (tableIndex == -1) {
Steve Block5ea0c052012-01-06 19:18:11 +000069 ALOGE("Max number of NATed interfaces reached");
Robert Greenwaltfc97b822011-11-02 16:48:36 -070070 errno = ENODEV;
71 cli->sendMsg(ResponseCode::OperationFailed, "Max number NATed", true);
72 return -1;
73 }
Jaime A Lopez-Sollanod14fd4f2012-01-11 16:29:28 -080074 strncpy(mInterfaceTable[tableIndex], iface, IFNAMSIZ);
75 // Ensure null termination even if truncation happened
76 mInterfaceTable[tableIndex][IFNAMSIZ] = 0;
Robert Greenwaltfc97b822011-11-02 16:48:36 -070077 }
78
Robert Greenwalt063af322011-11-18 15:32:13 -080079 return modifyRoute(cli, ADD, iface, dest, prefix, gateway, tableIndex);
80}
81
Robert Greenwaltc4621772012-01-31 12:46:45 -080082int SecondaryTableController::modifyRoute(SocketClient *cli, const char *action, char *iface,
83 char *dest, int prefix, char *gateway, int tableIndex) {
Robert Greenwalt063af322011-11-18 15:32:13 -080084 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 Greenwaltfc97b822011-11-02 16:48:36 -070095 if (runAndFree(cli, cmd)) {
Steve Block5ea0c052012-01-06 19:18:11 +000096 ALOGE("ip route %s failed: %s route %s %s/%d via %s dev %s table %d", action,
Robert Greenwalt063af322011-11-18 15:32:13 -080097 IP_PATH, action, dest, prefix, gateway, iface, tableIndex+BASE_TABLE_NUMBER);
Robert Greenwaltfc97b822011-11-02 16:48:36 -070098 errno = ENODEV;
Robert Greenwalt063af322011-11-18 15:32:13 -080099 cli->sendMsg(ResponseCode::OperationFailed, "ip route modification failed", true);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700100 return -1;
101 }
Robert Greenwalt063af322011-11-18 15:32:13 -0800102
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 }
Robert Greenwaltc4621772012-01-31 12:46:45 -0800111 modifyRuleCount(tableIndex, action);
Robert Greenwalt063af322011-11-18 15:32:13 -0800112 cli->sendMsg(ResponseCode::CommandOkay, "Route modified", false);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700113 return 0;
114}
115
Robert Greenwaltc4621772012-01-31 12:46:45 -0800116void SecondaryTableController::modifyRuleCount(int tableIndex, const char *action) {
117 if (strcmp(action, ADD) == 0) {
118 mInterfaceRuleCount[tableIndex]++;
119 } else {
120 if (--mInterfaceRuleCount[tableIndex] < 1) {
121 mInterfaceRuleCount[tableIndex] = 0;
122 mInterfaceTable[tableIndex][0] = 0;
123 }
124 }
125}
126
127int SecondaryTableController::verifyTableIndex(int tableIndex) {
128 if ((tableIndex < 0) ||
129 (tableIndex >= INTERFACES_TRACKED) ||
130 (mInterfaceTable[tableIndex][0] == 0)) {
131 return -1;
132 } else {
133 return 0;
134 }
135}
136
137const char *SecondaryTableController::getVersion(const char *addr) {
138 if (strchr(addr, ':') != NULL) {
139 return "-6";
140 } else {
141 return "-4";
142 }
143}
144
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700145int SecondaryTableController::removeRoute(SocketClient *cli, char *iface, char *dest, int prefix,
146 char *gateway) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700147 int tableIndex = findTableNumber(iface);
148 if (tableIndex == -1) {
Steve Block5ea0c052012-01-06 19:18:11 +0000149 ALOGE("Interface not found");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700150 errno = ENODEV;
151 cli->sendMsg(ResponseCode::OperationFailed, "Interface not found", true);
152 return -1;
153 }
154
Robert Greenwalt063af322011-11-18 15:32:13 -0800155 return modifyRoute(cli, DEL, iface, dest, prefix, gateway, tableIndex);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700156}
157
Robert Greenwaltc4621772012-01-31 12:46:45 -0800158int SecondaryTableController::modifyFromRule(int tableIndex, const char *action,
159 const char *addr) {
160 char *cmd;
161
162 if (verifyTableIndex(tableIndex)) {
163 return -1;
164 }
165 asprintf(&cmd, "%s %s rule %s from %s table %d", IP_PATH, getVersion(addr),
166 action, addr, tableIndex + BASE_TABLE_NUMBER);
167 if (runAndFree(NULL, cmd)) {
168 return -1;
169 }
170
171 modifyRuleCount(tableIndex, action);
172 return 0;
173}
174
175int SecondaryTableController::modifyLocalRoute(int tableIndex, const char *action,
176 const char *iface, const char *addr) {
177 char *cmd;
178
179 if (verifyTableIndex(tableIndex)) {
180 return -1;
181 }
182
183 modifyRuleCount(tableIndex, action); // some del's will fail as the iface is already gone.
184
185 asprintf(&cmd, "%s route %s %s dev %s table %d", IP_PATH, action, addr, iface,
186 tableIndex+BASE_TABLE_NUMBER);
187 return runAndFree(NULL, cmd);
188}
189
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700190int SecondaryTableController::runAndFree(SocketClient *cli, char *cmd) {
191 int ret = 0;
192 if (strlen(cmd) >= 255) {
Robert Greenwaltc4621772012-01-31 12:46:45 -0800193 if (cli != NULL) {
194 ALOGE("ip command (%s) too long", cmd);
195 errno = E2BIG;
196 cli->sendMsg(ResponseCode::CommandSyntaxError, "Too long", true);
197 }
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700198 free(cmd);
199 return -1;
200 }
JP Abgrall9e5e0ce2011-12-14 15:20:59 -0800201 ret = system_nosh(cmd);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700202 free(cmd);
203 return ret;
204}