blob: 3594a5d896d211591007171410c51e91376b014f [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
JP Abgrallbaeccc42013-06-25 09:44:10 -070017#define LOG_NDEBUG 0
JP Abgrall0031cea2012-04-17 16:38:23 -070018
San Mehat9ff78fb2010-01-19 12:59:15 -080019#include <stdlib.h>
20#include <errno.h>
21#include <sys/socket.h>
22#include <sys/stat.h>
Rom Lemarchand001f0a42013-01-31 12:41:03 -080023#include <sys/wait.h>
San Mehat9ff78fb2010-01-19 12:59:15 -080024#include <fcntl.h>
25#include <netinet/in.h>
26#include <arpa/inet.h>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080027#include <string.h>
John Michelauac208602011-05-27 22:07:20 -050028#include <cutils/properties.h>
San Mehat9ff78fb2010-01-19 12:59:15 -080029
30#define LOG_TAG "NatController"
31#include <cutils/log.h>
Rom Lemarchand001f0a42013-01-31 12:41:03 -080032#include <logwrap/logwrap.h>
San Mehat9ff78fb2010-01-19 12:59:15 -080033
34#include "NatController.h"
Robert Greenwaltc4621772012-01-31 12:46:45 -080035#include "NetdConstants.h"
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070036#include "RouteController.h"
San Mehat9ff78fb2010-01-19 12:59:15 -080037
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070038const char* NatController::LOCAL_FORWARD = "natctrl_FORWARD";
39const char* NatController::LOCAL_NAT_POSTROUTING = "natctrl_nat_POSTROUTING";
JP Abgrallbaeccc42013-06-25 09:44:10 -070040const char* NatController::LOCAL_TETHER_COUNTERS_CHAIN = "natctrl_tether_counters";
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070041
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070042NatController::NatController() {
San Mehat9ff78fb2010-01-19 12:59:15 -080043}
44
45NatController::~NatController() {
46}
47
JP Abgrall4ae80de2013-03-14 20:06:20 -070048struct CommandsAndArgs {
49 /* The array size doesn't really matter as the compiler will barf if too many initializers are specified. */
50 const char *cmd[32];
51 bool checkRes;
52};
53
Rom Lemarchand001f0a42013-01-31 12:41:03 -080054int NatController::runCmd(int argc, const char **argv) {
JP Abgrall11b4e9b2011-08-11 15:34:49 -070055 int res;
San Mehat9ff78fb2010-01-19 12:59:15 -080056
Rom Lemarchand001f0a42013-01-31 12:41:03 -080057 res = android_fork_execvp(argc, (char **)argv, NULL, false, false);
JP Abgrallbaeccc42013-06-25 09:44:10 -070058
59#if !LOG_NDEBUG
60 std::string full_cmd = argv[0];
61 argc--; argv++;
62 /*
63 * HACK: Sometimes runCmd() is called with a ridcously large value (32)
64 * and it works because the argv[] contains a NULL after the last
65 * true argv. So here we use the NULL argv[] to terminate when the argc
66 * is horribly wrong, and argc for the normal cases.
67 */
68 for (; argc && argv[0]; argc--, argv++) {
69 full_cmd += " ";
70 full_cmd += argv[0];
71 }
72 ALOGV("runCmd(%s) res=%d", full_cmd.c_str(), res);
73#endif
JP Abgrall11b4e9b2011-08-11 15:34:49 -070074 return res;
San Mehat9ff78fb2010-01-19 12:59:15 -080075}
76
JP Abgrall0031cea2012-04-17 16:38:23 -070077int NatController::setupIptablesHooks() {
JP Abgrallbaeccc42013-06-25 09:44:10 -070078 int res;
79 res = setDefaults();
80 if (res < 0) {
81 return res;
82 }
83
84 struct CommandsAndArgs defaultCommands[] = {
85 /*
86 * Chain for tethering counters.
87 * This chain is reached via --goto, and then RETURNS.
88 */
89 {{IPTABLES_PATH, "-F", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
90 {{IPTABLES_PATH, "-X", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
91 {{IPTABLES_PATH, "-N", LOCAL_TETHER_COUNTERS_CHAIN,}, 1},
92 };
93 for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) {
94 if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) &&
95 defaultCommands[cmdNum].checkRes) {
96 return -1;
97 }
98 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -070099 ifacePairList.clear();
JP Abgrallbaeccc42013-06-25 09:44:10 -0700100
JP Abgrall0031cea2012-04-17 16:38:23 -0700101 return 0;
102}
103
104int NatController::setDefaults() {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700105 /*
106 * The following only works because:
107 * - the defaultsCommands[].cmd array is padded with NULL, and
108 * - the 1st argc of runCmd() will just be the max for the CommandsAndArgs[].cmd, and
109 * - internally it will be memcopied to an array and terminated with a NULL.
110 */
JP Abgrall4ae80de2013-03-14 20:06:20 -0700111 struct CommandsAndArgs defaultCommands[] = {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700112 {{IPTABLES_PATH, "-F", LOCAL_FORWARD,}, 1},
113 {{IPTABLES_PATH, "-A", LOCAL_FORWARD, "-j", "DROP"}, 1},
114 {{IPTABLES_PATH, "-t", "nat", "-F", LOCAL_NAT_POSTROUTING}, 1},
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800115 };
JP Abgrall4ae80de2013-03-14 20:06:20 -0700116 for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) {
117 if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) &&
118 defaultCommands[cmdNum].checkRes) {
119 return -1;
120 }
121 }
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700122
123 natCount = 0;
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600124
San Mehat9ff78fb2010-01-19 12:59:15 -0800125 return 0;
126}
127
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700128int NatController::enableNat(const char* intIface, const char* extIface) {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700129 ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface);
130
JP Abgrall69261cb2014-06-19 18:35:24 -0700131 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800132 errno = ENODEV;
133 return -1;
134 }
135
JP Abgrallbaeccc42013-06-25 09:44:10 -0700136 /* Bug: b/9565268. "enableNat wlan0 wlan0". For now we fail until java-land is fixed */
137 if (!strcmp(intIface, extIface)) {
138 ALOGE("Duplicate interface specified: %s %s", intIface, extIface);
139 errno = EINVAL;
140 return -1;
141 }
142
JP Abgrall659692a2013-03-14 20:07:17 -0700143 // add this if we are the first added nat
144 if (natCount == 0) {
145 const char *cmd[] = {
146 IPTABLES_PATH,
147 "-t",
148 "nat",
149 "-A",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700150 LOCAL_NAT_POSTROUTING,
JP Abgrall659692a2013-03-14 20:07:17 -0700151 "-o",
152 extIface,
153 "-j",
154 "MASQUERADE"
155 };
156 if (runCmd(ARRAY_SIZE(cmd), cmd)) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700157 ALOGE("Error setting postroute rule: iface=%s", extIface);
JP Abgrall659692a2013-03-14 20:07:17 -0700158 // unwind what's been done, but don't care about success - what more could we do?
JP Abgrall659692a2013-03-14 20:07:17 -0700159 setDefaults();
160 return -1;
161 }
162 }
163
JP Abgrall659692a2013-03-14 20:07:17 -0700164 if (setForwardRules(true, intIface, extIface) != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000165 ALOGE("Error setting forward rules");
JP Abgrall659692a2013-03-14 20:07:17 -0700166 if (natCount == 0) {
167 setDefaults();
168 }
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700169 errno = ENODEV;
170 return -1;
171 }
172
JP Abgrall0031cea2012-04-17 16:38:23 -0700173 /* Always make sure the drop rule is at the end */
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800174 const char *cmd1[] = {
175 IPTABLES_PATH,
176 "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700177 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800178 "-j",
179 "DROP"
180 };
181 runCmd(ARRAY_SIZE(cmd1), cmd1);
182 const char *cmd2[] = {
183 IPTABLES_PATH,
184 "-A",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700185 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800186 "-j",
187 "DROP"
188 };
189 runCmd(ARRAY_SIZE(cmd2), cmd2);
JP Abgrall0031cea2012-04-17 16:38:23 -0700190
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700191 if (int ret = RouteController::enableTethering(intIface, extIface)) {
192 ALOGE("failed to add tethering rule for iif=%s oif=%s", intIface, extIface);
193 errno = -ret;
194 return -1;
195 }
196
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700197 natCount++;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700198 return 0;
199}
200
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700201bool NatController::checkTetherCountingRuleExist(const char *pair_name) {
202 std::list<std::string>::iterator it;
203
204 for (it = ifacePairList.begin(); it != ifacePairList.end(); it++) {
205 if (*it == pair_name) {
206 /* We already have this counter */
207 return true;
208 }
209 }
210 return false;
211}
212
JP Abgrallbaeccc42013-06-25 09:44:10 -0700213int NatController::setTetherCountingRules(bool add, const char *intIface, const char *extIface) {
214
215 /* We only ever add tethering quota rules so that they stick. */
216 if (!add) {
217 return 0;
218 }
Sreeram Ramachandran56afacf2014-05-28 15:07:00 -0700219 char *pair_name;
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700220 asprintf(&pair_name, "%s_%s", intIface, extIface);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700221
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700222 if (checkTetherCountingRuleExist(pair_name)) {
223 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700224 return 0;
225 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700226 const char *cmd2b[] = {
227 IPTABLES_PATH,
228 "-A",
229 LOCAL_TETHER_COUNTERS_CHAIN,
230 "-i",
231 intIface,
232 "-o",
233 extIface,
JP Abgrallbaeccc42013-06-25 09:44:10 -0700234 "-j",
235 "RETURN"
236 };
237
238 if (runCmd(ARRAY_SIZE(cmd2b), cmd2b) && add) {
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700239 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700240 return -1;
241 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700242 ifacePairList.push_front(pair_name);
243 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700244
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700245 asprintf(&pair_name, "%s_%s", extIface, intIface);
246 if (checkTetherCountingRuleExist(pair_name)) {
247 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700248 return 0;
249 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700250
251 const char *cmd3b[] = {
252 IPTABLES_PATH,
253 "-A",
254 LOCAL_TETHER_COUNTERS_CHAIN,
255 "-i",
256 extIface,
257 "-o",
258 intIface,
JP Abgrallbaeccc42013-06-25 09:44:10 -0700259 "-j",
260 "RETURN"
261 };
262
263 if (runCmd(ARRAY_SIZE(cmd3b), cmd3b) && add) {
264 // unwind what's been done, but don't care about success - what more could we do?
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700265 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700266 return -1;
267 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700268 ifacePairList.push_front(pair_name);
269 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700270 return 0;
271}
272
273int NatController::setForwardRules(bool add, const char *intIface, const char *extIface) {
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800274 const char *cmd1[] = {
275 IPTABLES_PATH,
276 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700277 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800278 "-i",
279 extIface,
280 "-o",
281 intIface,
282 "-m",
283 "state",
284 "--state",
285 "ESTABLISHED,RELATED",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700286 "-g",
287 LOCAL_TETHER_COUNTERS_CHAIN
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800288 };
289 int rc = 0;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700290
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800291 if (runCmd(ARRAY_SIZE(cmd1), cmd1) && add) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800292 return -1;
293 }
294
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800295 const char *cmd2[] = {
296 IPTABLES_PATH,
297 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700298 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800299 "-i",
300 intIface,
301 "-o",
302 extIface,
303 "-m",
304 "state",
305 "--state",
306 "INVALID",
307 "-j",
308 "DROP"
309 };
310
311 const char *cmd3[] = {
312 IPTABLES_PATH,
313 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700314 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800315 "-i",
316 intIface,
317 "-o",
318 extIface,
JP Abgrallbaeccc42013-06-25 09:44:10 -0700319 "-g",
320 LOCAL_TETHER_COUNTERS_CHAIN
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800321 };
322
323 if (runCmd(ARRAY_SIZE(cmd2), cmd2) && add) {
Robert Greenwaltf7bf29c2011-11-01 22:07:28 -0700324 // bail on error, but only if adding
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800325 rc = -1;
326 goto err_invalid_drop;
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700327 }
328
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800329 if (runCmd(ARRAY_SIZE(cmd3), cmd3) && add) {
Robert Greenwalt210b9772010-03-25 14:54:45 -0700330 // unwind what's been done, but don't care about success - what more could we do?
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800331 rc = -1;
332 goto err_return;
San Mehat9ff78fb2010-01-19 12:59:15 -0800333 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700334
JP Abgrallbaeccc42013-06-25 09:44:10 -0700335 if (setTetherCountingRules(add, intIface, extIface) && add) {
336 rc = -1;
337 goto err_return;
338 }
339
San Mehat9ff78fb2010-01-19 12:59:15 -0800340 return 0;
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800341
342err_return:
343 cmd2[1] = "-D";
344 runCmd(ARRAY_SIZE(cmd2), cmd2);
345err_invalid_drop:
346 cmd1[1] = "-D";
347 runCmd(ARRAY_SIZE(cmd1), cmd1);
348 return rc;
San Mehat9ff78fb2010-01-19 12:59:15 -0800349}
350
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700351int NatController::disableNat(const char* intIface, const char* extIface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700352 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700353 errno = ENODEV;
354 return -1;
355 }
356
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700357 if (int ret = RouteController::disableTethering(intIface, extIface)) {
358 ALOGE("failed to remove tethering rule for iif=%s oif=%s", intIface, extIface);
359 errno = -ret;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700360 return -1;
361 }
362
363 setForwardRules(false, intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700364 if (--natCount <= 0) {
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600365 // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0
366 setDefaults();
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700367 }
368 return 0;
San Mehat9ff78fb2010-01-19 12:59:15 -0800369}