blob: ed1b095d9e78ac85a4efee597d79f32273e972f4 [file] [log] [blame]
San Mehat9ff78fb2010-01-19 12:59:15 -08001/*
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 <sys/socket.h>
20#include <sys/stat.h>
21#include <fcntl.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080024#include <string.h>
John Michelauac208602011-05-27 22:07:20 -050025#include <cutils/properties.h>
San Mehat9ff78fb2010-01-19 12:59:15 -080026
27#define LOG_TAG "NatController"
28#include <cutils/log.h>
29
30#include "NatController.h"
Robert Greenwaltfc97b822011-11-02 16:48:36 -070031#include "SecondaryTableController.h"
San Mehat9ff78fb2010-01-19 12:59:15 -080032
JP Abgrall9e5e0ce2011-12-14 15:20:59 -080033extern "C" int system_nosh(const char *command);
San Mehat9ff78fb2010-01-19 12:59:15 -080034
35static char IPTABLES_PATH[] = "/system/bin/iptables";
Robert Greenwaltfc97b822011-11-02 16:48:36 -070036static char IP_PATH[] = "/system/bin/ip";
San Mehat9ff78fb2010-01-19 12:59:15 -080037
Robert Greenwaltfc97b822011-11-02 16:48:36 -070038NatController::NatController(SecondaryTableController *ctrl) {
39 secondaryTableCtrl = ctrl;
40 setDefaults();
San Mehat9ff78fb2010-01-19 12:59:15 -080041}
42
43NatController::~NatController() {
44}
45
Robert Greenwaltfc97b822011-11-02 16:48:36 -070046int NatController::runCmd(const char *path, const char *cmd) {
JP Abgrall11b4e9b2011-08-11 15:34:49 -070047 char *buffer;
48 size_t len = strnlen(cmd, 255);
49 int res;
San Mehat9ff78fb2010-01-19 12:59:15 -080050
JP Abgrall11b4e9b2011-08-11 15:34:49 -070051 if (len == 255) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -070052 LOGE("command too long");
JP Abgrall11b4e9b2011-08-11 15:34:49 -070053 errno = E2BIG;
54 return -1;
San Mehat9ff78fb2010-01-19 12:59:15 -080055 }
San Mehat9ff78fb2010-01-19 12:59:15 -080056
Robert Greenwaltfc97b822011-11-02 16:48:36 -070057 asprintf(&buffer, "%s %s", path, cmd);
JP Abgrall9e5e0ce2011-12-14 15:20:59 -080058 res = system_nosh(buffer);
JP Abgrall11b4e9b2011-08-11 15:34:49 -070059 free(buffer);
60 return res;
San Mehat9ff78fb2010-01-19 12:59:15 -080061}
62
63int NatController::setDefaults() {
64
Robert Greenwaltfc97b822011-11-02 16:48:36 -070065 if (runCmd(IPTABLES_PATH, "-P INPUT ACCEPT"))
San Mehat9ff78fb2010-01-19 12:59:15 -080066 return -1;
Robert Greenwaltfc97b822011-11-02 16:48:36 -070067 if (runCmd(IPTABLES_PATH, "-P OUTPUT ACCEPT"))
San Mehat9ff78fb2010-01-19 12:59:15 -080068 return -1;
Robert Greenwaltfc97b822011-11-02 16:48:36 -070069 if (runCmd(IPTABLES_PATH, "-P FORWARD DROP"))
San Mehat9ff78fb2010-01-19 12:59:15 -080070 return -1;
Robert Greenwaltfc97b822011-11-02 16:48:36 -070071 if (runCmd(IPTABLES_PATH, "-F FORWARD"))
San Mehat9ff78fb2010-01-19 12:59:15 -080072 return -1;
Robert Greenwaltfc97b822011-11-02 16:48:36 -070073 if (runCmd(IPTABLES_PATH, "-t nat -F"))
San Mehat9ff78fb2010-01-19 12:59:15 -080074 return -1;
Robert Greenwaltfc97b822011-11-02 16:48:36 -070075
76 runCmd(IP_PATH, "rule flush");
Robert Greenwalt063af322011-11-18 15:32:13 -080077 runCmd(IP_PATH, "-6 rule flush");
Robert Greenwaltfc97b822011-11-02 16:48:36 -070078 runCmd(IP_PATH, "rule add from all lookup default prio 32767");
79 runCmd(IP_PATH, "rule add from all lookup main prio 32766");
Robert Greenwalt063af322011-11-18 15:32:13 -080080 runCmd(IP_PATH, "-6 rule add from all lookup default prio 32767");
81 runCmd(IP_PATH, "-6 rule add from all lookup main prio 32766");
82 runCmd(IP_PATH, "route flush cache");
Robert Greenwaltfc97b822011-11-02 16:48:36 -070083
84 natCount = 0;
San Mehat9ff78fb2010-01-19 12:59:15 -080085 return 0;
86}
87
Robert Greenwaltfc97b822011-11-02 16:48:36 -070088bool NatController::checkInterface(const char *iface) {
89 if (strlen(iface) > MAX_IFACE_LENGTH) return false;
San Mehat9ff78fb2010-01-19 12:59:15 -080090 return true;
91}
92
Robert Greenwalt063af322011-11-18 15:32:13 -080093const char *NatController::getVersion(const char *addr) {
94 if (strchr(addr, ':') != NULL) {
95 return "-6";
96 } else {
97 return "-4";
98 }
99}
100
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700101// 0 1 2 3 4 5
102// nat enable intface extface addrcnt nated-ipaddr/prelength
103int NatController::enableNat(const int argc, char **argv) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800104 char cmd[255];
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700105 int i;
106 int addrCount = atoi(argv[4]);
107 int ret = 0;
108 const char *intIface = argv[2];
109 const char *extIface = argv[3];
110 int tableNumber;
San Mehat9ff78fb2010-01-19 12:59:15 -0800111
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700112 if (!checkInterface(intIface) || !checkInterface(extIface)) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800113 LOGE("Invalid interface specified");
114 errno = ENODEV;
115 return -1;
116 }
117
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700118 if (argc < 5 + addrCount) {
119 LOGE("Missing Argument");
120 errno = EINVAL;
121 return -1;
122 }
123
124 tableNumber = secondaryTableCtrl->findTableNumber(extIface);
125 if (tableNumber != -1) {
126 for(i = 0; i < addrCount && ret == 0; i++) {
Robert Greenwalt063af322011-11-18 15:32:13 -0800127 snprintf(cmd, sizeof(cmd), "%s rule add from %s table %d", getVersion(argv[5+i]),
128 argv[5+i], tableNumber + BASE_TABLE_NUMBER);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700129 ret |= runCmd(IP_PATH, cmd);
130 if (ret) LOGE("IP rule %s got %d", cmd, ret);
131
132 snprintf(cmd, sizeof(cmd), "route add %s dev %s table %d", argv[5+i], intIface,
133 tableNumber + BASE_TABLE_NUMBER);
134 ret |= runCmd(IP_PATH, cmd);
135 if (ret) LOGE("IP route %s got %d", cmd, ret);
136 }
Robert Greenwalt063af322011-11-18 15:32:13 -0800137 runCmd(IP_PATH, "route flush cache");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700138 }
139
140 if (ret != 0 || setForwardRules(true, intIface, extIface) != 0) {
141 if (tableNumber != -1) {
142 for (i = 0; i < addrCount; i++) {
143 snprintf(cmd, sizeof(cmd), "route del %s dev %s table %d", argv[5+i], intIface,
144 tableNumber + BASE_TABLE_NUMBER);
145 runCmd(IP_PATH, cmd);
146
Robert Greenwalt063af322011-11-18 15:32:13 -0800147 snprintf(cmd, sizeof(cmd), "%s rule del from %s table %d", getVersion(argv[5+i]),
148 argv[5+i], tableNumber + BASE_TABLE_NUMBER);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700149 runCmd(IP_PATH, cmd);
150 }
Robert Greenwalt063af322011-11-18 15:32:13 -0800151 runCmd(IP_PATH, "route flush cache");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700152 }
153 LOGE("Error setting forward rules");
154 errno = ENODEV;
155 return -1;
156 }
157
158 natCount++;
159 // add this if we are the first added nat
160 if (natCount == 1) {
161 snprintf(cmd, sizeof(cmd), "-t nat -A POSTROUTING -o %s -j MASQUERADE", extIface);
162 if (runCmd(IPTABLES_PATH, cmd)) {
163 LOGE("Error seting postroute rule: %s", cmd);
164 // unwind what's been done, but don't care about success - what more could we do?
165 for (i = 0; i < addrCount; i++) {
166 snprintf(cmd, sizeof(cmd), "route del %s dev %s table %d", argv[5+i], intIface,
167 tableNumber + BASE_TABLE_NUMBER);
168 runCmd(IP_PATH, cmd);
169 }
170 setDefaults();
171 return -1;
172 }
173 }
174
175 return 0;
176}
177
178int NatController::setForwardRules(bool add, const char *intIface, const char * extIface) {
179 char cmd[255];
180
San Mehat9ff78fb2010-01-19 12:59:15 -0800181 snprintf(cmd, sizeof(cmd),
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700182 "-%s FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
183 (add ? "A" : "D"),
San Mehat9ff78fb2010-01-19 12:59:15 -0800184 extIface, intIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700185 if (runCmd(IPTABLES_PATH, cmd) && add) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800186 return -1;
187 }
188
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700189 snprintf(cmd, sizeof(cmd),
190 "-%s FORWARD -i %s -o %s -m state --state INVALID -j DROP",
191 (add ? "A" : "D"),
192 intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700193 if (runCmd(IPTABLES_PATH, cmd) && add) {
Robert Greenwaltf7bf29c2011-11-01 22:07:28 -0700194 // bail on error, but only if adding
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700195 snprintf(cmd, sizeof(cmd),
196 "-%s FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
197 (!add ? "A" : "D"),
198 extIface, intIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700199 runCmd(IPTABLES_PATH, cmd);
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700200 return -1;
201 }
202
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700203 snprintf(cmd, sizeof(cmd), "-%s FORWARD -i %s -o %s -j ACCEPT", (add ? "A" : "D"),
204 intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700205 if (runCmd(IPTABLES_PATH, cmd) && add) {
Robert Greenwalt210b9772010-03-25 14:54:45 -0700206 // unwind what's been done, but don't care about success - what more could we do?
207 snprintf(cmd, sizeof(cmd),
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700208 "-%s FORWARD -i %s -o %s -m state --state INVALID -j DROP",
209 (!add ? "A" : "D"),
210 intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700211 runCmd(IPTABLES_PATH, cmd);
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700212
213 snprintf(cmd, sizeof(cmd),
Robert Greenwalt210b9772010-03-25 14:54:45 -0700214 "-%s FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
215 (!add ? "A" : "D"),
216 extIface, intIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700217 runCmd(IPTABLES_PATH, cmd);
San Mehat9ff78fb2010-01-19 12:59:15 -0800218 return -1;
219 }
San Mehat9ff78fb2010-01-19 12:59:15 -0800220 return 0;
221}
222
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700223// nat disable intface extface
224// 0 1 2 3 4 5
225// nat enable intface extface addrcnt nated-ipaddr/prelength
226int NatController::disableNat(const int argc, char **argv) {
227 char cmd[255];
228 int i;
229 int addrCount = atoi(argv[4]);
230 const char *intIface = argv[2];
231 const char *extIface = argv[3];
232 int tableNumber;
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700233
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700234 if (!checkInterface(intIface) || !checkInterface(extIface)) {
235 LOGE("Invalid interface specified");
236 errno = ENODEV;
237 return -1;
238 }
239
240 if (argc < 5 + addrCount) {
241 LOGE("Missing Argument");
242 errno = EINVAL;
243 return -1;
244 }
245
246 setForwardRules(false, intIface, extIface);
247
248 tableNumber = secondaryTableCtrl->findTableNumber(extIface);
249 if (tableNumber != -1) {
250 for (i = 0; i < addrCount; i++) {
251 snprintf(cmd, sizeof(cmd), "route del %s dev %s table %d", argv[5+i], intIface,
252 tableNumber + BASE_TABLE_NUMBER);
253 // if the interface has gone down these will be gone already and give errors
254 // ignore them.
255 runCmd(IP_PATH, cmd);
Robert Greenwalt063af322011-11-18 15:32:13 -0800256
257 snprintf(cmd, sizeof(cmd), "%s rule del from %s table %d", getVersion(argv[5+i]),
258 argv[5+i], tableNumber + BASE_TABLE_NUMBER);
259 runCmd(IP_PATH, cmd);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700260 }
Robert Greenwalt063af322011-11-18 15:32:13 -0800261
262 runCmd(IP_PATH, "route flush cache");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700263 }
264
265 if (--natCount <= 0) {
266 char bootmode[PROPERTY_VALUE_MAX] = {0};
267 property_get("ro.bootmode", bootmode, "unknown");
268 if (0 != strcmp("bp-tools", bootmode)) {
269 // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0
270 setDefaults();
271 }
272 natCount = 0;
273 }
274 return 0;
San Mehat9ff78fb2010-01-19 12:59:15 -0800275}