blob: 222a0e04e8053deb0c26e56c3415abde9a8bba42 [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>
Rom Lemarchand001f0a42013-01-31 12:41:03 -080033#include <logwrap/logwrap.h>
JP Abgrall9e5e0ce2011-12-14 15:20:59 -080034
Robert Greenwaltfc97b822011-11-02 16:48:36 -070035#include "ResponseCode.h"
Robert Greenwaltc4621772012-01-31 12:46:45 -080036#include "NetdConstants.h"
Robert Greenwaltfc97b822011-11-02 16:48:36 -070037#include "SecondaryTableController.h"
38
Chad Brubaker9a508892013-05-31 20:51:46 -070039const char* SecondaryTableController::LOCAL_MANGLE_OUTPUT = "st_mangle_OUTPUT";
Chad Brubaker7a6ce4b2013-06-06 21:42:53 -070040const char* SecondaryTableController::LOCAL_NAT_POSTROUTING = "st_nat_POSTROUTING";
Chad Brubaker9a508892013-05-31 20:51:46 -070041
Robert Greenwaltfc97b822011-11-02 16:48:36 -070042SecondaryTableController::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
51SecondaryTableController::~SecondaryTableController() {
52}
53
54int SecondaryTableController::findTableNumber(const char *iface) {
55 int i;
56 for (i = 0; i < INTERFACES_TRACKED; i++) {
Jaime A Lopez-Sollano3c207872012-01-11 16:29:28 -080057 // compare through the final null, hence +1
58 if (strncmp(iface, mInterfaceTable[i], IFNAMSIZ + 1) == 0) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -070059 return i;
60 }
61 }
62 return -1;
63}
64
65int SecondaryTableController::addRoute(SocketClient *cli, char *iface, char *dest, int prefix,
66 char *gateway) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -070067 int tableIndex = findTableNumber(iface);
68 if (tableIndex == -1) {
69 tableIndex = findTableNumber(""); // look for an empty slot
70 if (tableIndex == -1) {
Steve Block5ea0c052012-01-06 19:18:11 +000071 ALOGE("Max number of NATed interfaces reached");
Robert Greenwaltfc97b822011-11-02 16:48:36 -070072 errno = ENODEV;
73 cli->sendMsg(ResponseCode::OperationFailed, "Max number NATed", true);
74 return -1;
75 }
Jaime A Lopez-Sollanod14fd4f2012-01-11 16:29:28 -080076 strncpy(mInterfaceTable[tableIndex], iface, IFNAMSIZ);
77 // Ensure null termination even if truncation happened
78 mInterfaceTable[tableIndex][IFNAMSIZ] = 0;
Robert Greenwaltfc97b822011-11-02 16:48:36 -070079 }
80
Robert Greenwalt063af322011-11-18 15:32:13 -080081 return modifyRoute(cli, ADD, iface, dest, prefix, gateway, tableIndex);
82}
83
Robert Greenwaltc4621772012-01-31 12:46:45 -080084int SecondaryTableController::modifyRoute(SocketClient *cli, const char *action, char *iface,
85 char *dest, int prefix, char *gateway, int tableIndex) {
Rom Lemarchand001f0a42013-01-31 12:41:03 -080086 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 Greenwalt063af322011-11-18 15:32:13 -080093
94 if (strcmp("::", gateway) == 0) {
Rom Lemarchand001f0a42013-01-31 12:41:03 -080095 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 Greenwalt063af322011-11-18 15:32:13 -0800106 } else {
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800107 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 Greenwalt063af322011-11-18 15:32:13 -0800120 }
121
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800122 if (ret) {
Steve Block5ea0c052012-01-06 19:18:11 +0000123 ALOGE("ip route %s failed: %s route %s %s/%d via %s dev %s table %d", action,
Robert Greenwalt063af322011-11-18 15:32:13 -0800124 IP_PATH, action, dest, prefix, gateway, iface, tableIndex+BASE_TABLE_NUMBER);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700125 errno = ENODEV;
Robert Greenwalt063af322011-11-18 15:32:13 -0800126 cli->sendMsg(ResponseCode::OperationFailed, "ip route modification failed", true);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700127 return -1;
128 }
Robert Greenwalt063af322011-11-18 15:32:13 -0800129
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 Greenwaltc4621772012-01-31 12:46:45 -0800138 modifyRuleCount(tableIndex, action);
Robert Greenwalt063af322011-11-18 15:32:13 -0800139 cli->sendMsg(ResponseCode::CommandOkay, "Route modified", false);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700140 return 0;
141}
142
Robert Greenwaltc4621772012-01-31 12:46:45 -0800143void 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
154int 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
164const char *SecondaryTableController::getVersion(const char *addr) {
165 if (strchr(addr, ':') != NULL) {
166 return "-6";
167 } else {
168 return "-4";
169 }
170}
171
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700172int SecondaryTableController::removeRoute(SocketClient *cli, char *iface, char *dest, int prefix,
173 char *gateway) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700174 int tableIndex = findTableNumber(iface);
175 if (tableIndex == -1) {
Steve Block5ea0c052012-01-06 19:18:11 +0000176 ALOGE("Interface not found");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700177 errno = ENODEV;
178 cli->sendMsg(ResponseCode::OperationFailed, "Interface not found", true);
179 return -1;
180 }
181
Robert Greenwalt063af322011-11-18 15:32:13 -0800182 return modifyRoute(cli, DEL, iface, dest, prefix, gateway, tableIndex);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700183}
184
Robert Greenwaltc4621772012-01-31 12:46:45 -0800185int SecondaryTableController::modifyFromRule(int tableIndex, const char *action,
186 const char *addr) {
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800187 char tableIndex_str[11];
Robert Greenwaltc4621772012-01-31 12:46:45 -0800188
189 if (verifyTableIndex(tableIndex)) {
190 return -1;
191 }
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800192
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 Greenwaltc4621772012-01-31 12:46:45 -0800206 return -1;
207 }
208
209 modifyRuleCount(tableIndex, action);
210 return 0;
211}
212
213int SecondaryTableController::modifyLocalRoute(int tableIndex, const char *action,
214 const char *iface, const char *addr) {
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800215 char tableIndex_str[11];
Robert Greenwaltc4621772012-01-31 12:46:45 -0800216
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 Lemarchand001f0a42013-01-31 12:41:03 -0800223 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 Greenwaltc4621772012-01-31 12:46:45 -0800237}
Chad Brubaker7a6ce4b2013-06-06 21:42:53 -0700238int SecondaryTableController::addFwmarkRule(const char *iface) {
239 return setFwmarkRule(iface, true);
240}
241
242int SecondaryTableController::removeFwmarkRule(const char *iface) {
243 return setFwmarkRule(iface, false);
244}
245
246int 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 Greenwaltc4621772012-01-31 12:46:45 -0800292
Chad Brubaker8830b942013-06-12 10:51:55 -0700293int SecondaryTableController::addUidRule(const char *iface, int uid_start, int uid_end) {
294 return setUidRule(iface, uid_start, uid_end, true);
Chad Brubaker9a508892013-05-31 20:51:46 -0700295}
296
Chad Brubaker8830b942013-06-12 10:51:55 -0700297int SecondaryTableController::removeUidRule(const char *iface, int uid_start, int uid_end) {
298 return setUidRule(iface, uid_start, uid_end, false);
Chad Brubaker9a508892013-05-31 20:51:46 -0700299}
300
Chad Brubaker8830b942013-06-12 10:51:55 -0700301int SecondaryTableController::setUidRule(const char *iface, int uid_start, int uid_end, bool add) {
Chad Brubaker9a508892013-05-31 20:51:46 -0700302 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 Brubaker8830b942013-06-12 10:51:55 -0700308 char uid_str[24] = {0};
309 snprintf(uid_str, sizeof(uid_str), "%d-%d", uid_start, uid_end);
Chad Brubaker9a508892013-05-31 20:51:46 -0700310 return execIptables(V4V6,
311 "-t",
312 "mangle",
313 add ? "-A" : "-D",
314 LOCAL_MANGLE_OUTPUT,
315 "-m",
316 "owner",
317 "--uid-owner",
Chad Brubaker8830b942013-06-12 10:51:55 -0700318 uid_str,
Chad Brubaker9a508892013-05-31 20:51:46 -0700319 "-j",
320 "MARK",
321 "--set-mark",
322 tableIndex_str,
323 NULL);
324}
325
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800326int SecondaryTableController::runCmd(int argc, const char **argv) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700327 int ret = 0;
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800328
329 ret = android_fork_execvp(argc, (char **)argv, NULL, false, false);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700330 return ret;
331}