blob: b978358cd3ba7e0aaddf57ff301d4140fc801616 [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";
Lorenzo Colittie8164dd2014-10-02 20:46:23 +090039const char* NatController::LOCAL_MANGLE_FORWARD = "natctrl_mangle_FORWARD";
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070040const char* NatController::LOCAL_NAT_POSTROUTING = "natctrl_nat_POSTROUTING";
JP Abgrallbaeccc42013-06-25 09:44:10 -070041const char* NatController::LOCAL_TETHER_COUNTERS_CHAIN = "natctrl_tether_counters";
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070042
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +090043auto NatController::execFunction = android_fork_execvp;
44
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070045NatController::NatController() {
San Mehat9ff78fb2010-01-19 12:59:15 -080046}
47
48NatController::~NatController() {
49}
50
JP Abgrall4ae80de2013-03-14 20:06:20 -070051struct CommandsAndArgs {
52 /* The array size doesn't really matter as the compiler will barf if too many initializers are specified. */
53 const char *cmd[32];
54 bool checkRes;
55};
56
Rom Lemarchand001f0a42013-01-31 12:41:03 -080057int NatController::runCmd(int argc, const char **argv) {
JP Abgrall11b4e9b2011-08-11 15:34:49 -070058 int res;
San Mehat9ff78fb2010-01-19 12:59:15 -080059
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +090060 res = execFunction(argc, (char **)argv, NULL, false, false);
JP Abgrallbaeccc42013-06-25 09:44:10 -070061
62#if !LOG_NDEBUG
63 std::string full_cmd = argv[0];
64 argc--; argv++;
65 /*
66 * HACK: Sometimes runCmd() is called with a ridcously large value (32)
67 * and it works because the argv[] contains a NULL after the last
68 * true argv. So here we use the NULL argv[] to terminate when the argc
69 * is horribly wrong, and argc for the normal cases.
70 */
71 for (; argc && argv[0]; argc--, argv++) {
72 full_cmd += " ";
73 full_cmd += argv[0];
74 }
75 ALOGV("runCmd(%s) res=%d", full_cmd.c_str(), res);
76#endif
JP Abgrall11b4e9b2011-08-11 15:34:49 -070077 return res;
San Mehat9ff78fb2010-01-19 12:59:15 -080078}
79
JP Abgrall0031cea2012-04-17 16:38:23 -070080int NatController::setupIptablesHooks() {
JP Abgrallbaeccc42013-06-25 09:44:10 -070081 int res;
82 res = setDefaults();
83 if (res < 0) {
84 return res;
85 }
86
87 struct CommandsAndArgs defaultCommands[] = {
88 /*
Lorenzo Colitti05cfd252016-07-10 23:15:46 +090089 * This is for tethering counters.
JP Abgrallbaeccc42013-06-25 09:44:10 -070090 * This chain is reached via --goto, and then RETURNS.
Lorenzo Colitti05cfd252016-07-10 23:15:46 +090091 */
92 {{IPTABLES_PATH, "-w", "-F", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
93 {{IP6TABLES_PATH, "-w", "-F", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
94 {{IPTABLES_PATH, "-w", "-X", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
95 {{IP6TABLES_PATH, "-w", "-X", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
96 {{IPTABLES_PATH, "-w", "-N", LOCAL_TETHER_COUNTERS_CHAIN,}, 1},
97 {{IP6TABLES_PATH, "-w", "-N", LOCAL_TETHER_COUNTERS_CHAIN,}, 1},
98
99 /*
Gordon Gao6b6f22f2014-09-18 11:50:09 -0700100 * Second chain is used to limit downstream mss to the upstream pmtu
101 * so we don't end up fragmenting every large packet tethered devices
102 * send. Note this feature requires kernel support with flag
103 * CONFIG_NETFILTER_XT_TARGET_TCPMSS=y, which not all builds will have,
104 * so the final rule is allowed to fail.
105 * Bug 17629786 asks to make the failure more obvious, or even fatal
106 * so that all builds eventually gain the performance improvement.
JP Abgrallbaeccc42013-06-25 09:44:10 -0700107 */
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900108 {{IPTABLES_PATH, "-w", "-t", "mangle", "-A", LOCAL_MANGLE_FORWARD, "-p", "tcp",
109 "--tcp-flags", "SYN", "SYN", "-j", "TCPMSS", "--clamp-mss-to-pmtu"}, 0},
JP Abgrallbaeccc42013-06-25 09:44:10 -0700110 };
111 for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) {
112 if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) &&
113 defaultCommands[cmdNum].checkRes) {
114 return -1;
115 }
116 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700117 ifacePairList.clear();
JP Abgrallbaeccc42013-06-25 09:44:10 -0700118
JP Abgrall0031cea2012-04-17 16:38:23 -0700119 return 0;
120}
121
122int NatController::setDefaults() {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700123 /*
124 * The following only works because:
125 * - the defaultsCommands[].cmd array is padded with NULL, and
126 * - the 1st argc of runCmd() will just be the max for the CommandsAndArgs[].cmd, and
127 * - internally it will be memcopied to an array and terminated with a NULL.
128 */
JP Abgrall4ae80de2013-03-14 20:06:20 -0700129 struct CommandsAndArgs defaultCommands[] = {
Paul Jensen94b2ab92015-08-04 10:35:05 -0400130 {{IPTABLES_PATH, "-w", "-F", LOCAL_FORWARD,}, 1},
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900131 {{IP6TABLES_PATH, "-w", "-F", LOCAL_FORWARD,}, 1},
Paul Jensen94b2ab92015-08-04 10:35:05 -0400132 {{IPTABLES_PATH, "-w", "-A", LOCAL_FORWARD, "-j", "DROP"}, 1},
133 {{IPTABLES_PATH, "-w", "-t", "nat", "-F", LOCAL_NAT_POSTROUTING}, 1},
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800134 };
JP Abgrall4ae80de2013-03-14 20:06:20 -0700135 for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) {
136 if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) &&
137 defaultCommands[cmdNum].checkRes) {
138 return -1;
139 }
140 }
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700141
142 natCount = 0;
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600143
San Mehat9ff78fb2010-01-19 12:59:15 -0800144 return 0;
145}
146
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700147int NatController::enableNat(const char* intIface, const char* extIface) {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700148 ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface);
149
JP Abgrall69261cb2014-06-19 18:35:24 -0700150 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800151 errno = ENODEV;
152 return -1;
153 }
154
JP Abgrallbaeccc42013-06-25 09:44:10 -0700155 /* Bug: b/9565268. "enableNat wlan0 wlan0". For now we fail until java-land is fixed */
156 if (!strcmp(intIface, extIface)) {
157 ALOGE("Duplicate interface specified: %s %s", intIface, extIface);
158 errno = EINVAL;
159 return -1;
160 }
161
JP Abgrall659692a2013-03-14 20:07:17 -0700162 // add this if we are the first added nat
163 if (natCount == 0) {
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900164 const char *v4Cmd[] = {
JP Abgrall659692a2013-03-14 20:07:17 -0700165 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400166 "-w",
JP Abgrall659692a2013-03-14 20:07:17 -0700167 "-t",
168 "nat",
169 "-A",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700170 LOCAL_NAT_POSTROUTING,
JP Abgrall659692a2013-03-14 20:07:17 -0700171 "-o",
172 extIface,
173 "-j",
174 "MASQUERADE"
175 };
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900176
177 /*
178 * IPv6 tethering doesn't need the state-based conntrack rules, so
179 * it unconditionally jumps to the tether counters chain all the time.
180 */
181 const char *v6Cmd[] = {IP6TABLES_PATH, "-w", "-A", LOCAL_FORWARD,
182 "-g", LOCAL_TETHER_COUNTERS_CHAIN};
183
184 if (runCmd(ARRAY_SIZE(v4Cmd), v4Cmd) || runCmd(ARRAY_SIZE(v6Cmd), v6Cmd)) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700185 ALOGE("Error setting postroute rule: iface=%s", extIface);
JP Abgrall659692a2013-03-14 20:07:17 -0700186 // unwind what's been done, but don't care about success - what more could we do?
JP Abgrall659692a2013-03-14 20:07:17 -0700187 setDefaults();
188 return -1;
189 }
190 }
191
JP Abgrall659692a2013-03-14 20:07:17 -0700192 if (setForwardRules(true, intIface, extIface) != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000193 ALOGE("Error setting forward rules");
JP Abgrall659692a2013-03-14 20:07:17 -0700194 if (natCount == 0) {
195 setDefaults();
196 }
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700197 errno = ENODEV;
198 return -1;
199 }
200
JP Abgrall0031cea2012-04-17 16:38:23 -0700201 /* Always make sure the drop rule is at the end */
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800202 const char *cmd1[] = {
203 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400204 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800205 "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700206 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800207 "-j",
208 "DROP"
209 };
210 runCmd(ARRAY_SIZE(cmd1), cmd1);
211 const char *cmd2[] = {
212 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400213 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800214 "-A",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700215 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800216 "-j",
217 "DROP"
218 };
219 runCmd(ARRAY_SIZE(cmd2), cmd2);
JP Abgrall0031cea2012-04-17 16:38:23 -0700220
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700221 natCount++;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700222 return 0;
223}
224
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700225bool NatController::checkTetherCountingRuleExist(const char *pair_name) {
226 std::list<std::string>::iterator it;
227
228 for (it = ifacePairList.begin(); it != ifacePairList.end(); it++) {
229 if (*it == pair_name) {
230 /* We already have this counter */
231 return true;
232 }
233 }
234 return false;
235}
236
JP Abgrallbaeccc42013-06-25 09:44:10 -0700237int NatController::setTetherCountingRules(bool add, const char *intIface, const char *extIface) {
238
239 /* We only ever add tethering quota rules so that they stick. */
240 if (!add) {
241 return 0;
242 }
Sreeram Ramachandran56afacf2014-05-28 15:07:00 -0700243 char *pair_name;
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700244 asprintf(&pair_name, "%s_%s", intIface, extIface);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700245
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700246 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 const char *cmd2b[] = {
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900251 IPTABLES_PATH,
252 "-w", "-A", LOCAL_TETHER_COUNTERS_CHAIN, "-i", intIface, "-o", extIface, "-j", "RETURN"
JP Abgrallbaeccc42013-06-25 09:44:10 -0700253 };
254
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900255 const char *cmd2c[] = {
256 IP6TABLES_PATH,
257 "-w", "-A", LOCAL_TETHER_COUNTERS_CHAIN, "-i", intIface, "-o", extIface, "-j", "RETURN"
258 };
259
260 if (runCmd(ARRAY_SIZE(cmd2b), cmd2b) || runCmd(ARRAY_SIZE(cmd2c), cmd2c)) {
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700261 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700262 return -1;
263 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700264 ifacePairList.push_front(pair_name);
265 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700266
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700267 asprintf(&pair_name, "%s_%s", extIface, intIface);
268 if (checkTetherCountingRuleExist(pair_name)) {
269 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700270 return 0;
271 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700272
273 const char *cmd3b[] = {
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900274 IPTABLES_PATH,
275 "-w", "-A", LOCAL_TETHER_COUNTERS_CHAIN, "-i", extIface, "-o", intIface, "-j", "RETURN"
JP Abgrallbaeccc42013-06-25 09:44:10 -0700276 };
277
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900278 const char *cmd3c[] = {
279 IP6TABLES_PATH,
280 "-w", "-A", LOCAL_TETHER_COUNTERS_CHAIN, "-i", extIface, "-o", intIface, "-j", "RETURN"
281 };
282
283 if (runCmd(ARRAY_SIZE(cmd3b), cmd3b) || runCmd(ARRAY_SIZE(cmd3c), cmd3c)) {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700284 // unwind what's been done, but don't care about success - what more could we do?
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700285 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700286 return -1;
287 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700288 ifacePairList.push_front(pair_name);
289 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700290 return 0;
291}
292
293int NatController::setForwardRules(bool add, const char *intIface, const char *extIface) {
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800294 const char *cmd1[] = {
295 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400296 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800297 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700298 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800299 "-i",
300 extIface,
301 "-o",
302 intIface,
303 "-m",
304 "state",
305 "--state",
306 "ESTABLISHED,RELATED",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700307 "-g",
308 LOCAL_TETHER_COUNTERS_CHAIN
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800309 };
310 int rc = 0;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700311
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800312 if (runCmd(ARRAY_SIZE(cmd1), cmd1) && add) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800313 return -1;
314 }
315
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800316 const char *cmd2[] = {
317 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400318 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800319 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700320 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800321 "-i",
322 intIface,
323 "-o",
324 extIface,
325 "-m",
326 "state",
327 "--state",
328 "INVALID",
329 "-j",
330 "DROP"
331 };
332
333 const char *cmd3[] = {
334 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400335 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800336 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700337 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800338 "-i",
339 intIface,
340 "-o",
341 extIface,
JP Abgrallbaeccc42013-06-25 09:44:10 -0700342 "-g",
343 LOCAL_TETHER_COUNTERS_CHAIN
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800344 };
345
346 if (runCmd(ARRAY_SIZE(cmd2), cmd2) && add) {
Robert Greenwaltf7bf29c2011-11-01 22:07:28 -0700347 // bail on error, but only if adding
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800348 rc = -1;
349 goto err_invalid_drop;
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700350 }
351
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800352 if (runCmd(ARRAY_SIZE(cmd3), cmd3) && add) {
Robert Greenwalt210b9772010-03-25 14:54:45 -0700353 // unwind what's been done, but don't care about success - what more could we do?
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800354 rc = -1;
355 goto err_return;
San Mehat9ff78fb2010-01-19 12:59:15 -0800356 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700357
JP Abgrallbaeccc42013-06-25 09:44:10 -0700358 if (setTetherCountingRules(add, intIface, extIface) && add) {
359 rc = -1;
360 goto err_return;
361 }
362
San Mehat9ff78fb2010-01-19 12:59:15 -0800363 return 0;
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800364
365err_return:
Paul Jensen94b2ab92015-08-04 10:35:05 -0400366 cmd2[2] = "-D";
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800367 runCmd(ARRAY_SIZE(cmd2), cmd2);
368err_invalid_drop:
Paul Jensen94b2ab92015-08-04 10:35:05 -0400369 cmd1[2] = "-D";
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800370 runCmd(ARRAY_SIZE(cmd1), cmd1);
371 return rc;
San Mehat9ff78fb2010-01-19 12:59:15 -0800372}
373
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700374int NatController::disableNat(const char* intIface, const char* extIface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700375 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700376 errno = ENODEV;
377 return -1;
378 }
379
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700380 setForwardRules(false, intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700381 if (--natCount <= 0) {
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600382 // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0
383 setDefaults();
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700384 }
385 return 0;
San Mehat9ff78fb2010-01-19 12:59:15 -0800386}