blob: fba96ca6ec8bda5089e1ad7630b7e2d75b7d3003 [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 /*
Gordon Gao6b6f22f2014-09-18 11:50:09 -070089 * First chain is for tethering counters.
JP Abgrallbaeccc42013-06-25 09:44:10 -070090 * This chain is reached via --goto, and then RETURNS.
Gordon Gao6b6f22f2014-09-18 11:50:09 -070091 *
92 * Second chain is used to limit downstream mss to the upstream pmtu
93 * so we don't end up fragmenting every large packet tethered devices
94 * send. Note this feature requires kernel support with flag
95 * CONFIG_NETFILTER_XT_TARGET_TCPMSS=y, which not all builds will have,
96 * so the final rule is allowed to fail.
97 * Bug 17629786 asks to make the failure more obvious, or even fatal
98 * so that all builds eventually gain the performance improvement.
JP Abgrallbaeccc42013-06-25 09:44:10 -070099 */
Paul Jensen94b2ab92015-08-04 10:35:05 -0400100 {{IPTABLES_PATH, "-w", "-F", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
101 {{IPTABLES_PATH, "-w", "-X", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
102 {{IPTABLES_PATH, "-w", "-N", LOCAL_TETHER_COUNTERS_CHAIN,}, 1},
103 {{IPTABLES_PATH, "-w", "-t", "mangle", "-A", LOCAL_MANGLE_FORWARD, "-p", "tcp", "--tcp-flags",
Gordon Gao6b6f22f2014-09-18 11:50:09 -0700104 "SYN", "SYN", "-j", "TCPMSS", "--clamp-mss-to-pmtu"}, 0},
JP Abgrallbaeccc42013-06-25 09:44:10 -0700105 };
106 for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) {
107 if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) &&
108 defaultCommands[cmdNum].checkRes) {
109 return -1;
110 }
111 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700112 ifacePairList.clear();
JP Abgrallbaeccc42013-06-25 09:44:10 -0700113
JP Abgrall0031cea2012-04-17 16:38:23 -0700114 return 0;
115}
116
117int NatController::setDefaults() {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700118 /*
119 * The following only works because:
120 * - the defaultsCommands[].cmd array is padded with NULL, and
121 * - the 1st argc of runCmd() will just be the max for the CommandsAndArgs[].cmd, and
122 * - internally it will be memcopied to an array and terminated with a NULL.
123 */
JP Abgrall4ae80de2013-03-14 20:06:20 -0700124 struct CommandsAndArgs defaultCommands[] = {
Paul Jensen94b2ab92015-08-04 10:35:05 -0400125 {{IPTABLES_PATH, "-w", "-F", LOCAL_FORWARD,}, 1},
126 {{IPTABLES_PATH, "-w", "-A", LOCAL_FORWARD, "-j", "DROP"}, 1},
127 {{IPTABLES_PATH, "-w", "-t", "nat", "-F", LOCAL_NAT_POSTROUTING}, 1},
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800128 };
JP Abgrall4ae80de2013-03-14 20:06:20 -0700129 for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) {
130 if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) &&
131 defaultCommands[cmdNum].checkRes) {
132 return -1;
133 }
134 }
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700135
136 natCount = 0;
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600137
San Mehat9ff78fb2010-01-19 12:59:15 -0800138 return 0;
139}
140
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700141int NatController::enableNat(const char* intIface, const char* extIface) {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700142 ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface);
143
JP Abgrall69261cb2014-06-19 18:35:24 -0700144 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800145 errno = ENODEV;
146 return -1;
147 }
148
JP Abgrallbaeccc42013-06-25 09:44:10 -0700149 /* Bug: b/9565268. "enableNat wlan0 wlan0". For now we fail until java-land is fixed */
150 if (!strcmp(intIface, extIface)) {
151 ALOGE("Duplicate interface specified: %s %s", intIface, extIface);
152 errno = EINVAL;
153 return -1;
154 }
155
JP Abgrall659692a2013-03-14 20:07:17 -0700156 // add this if we are the first added nat
157 if (natCount == 0) {
158 const char *cmd[] = {
159 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400160 "-w",
JP Abgrall659692a2013-03-14 20:07:17 -0700161 "-t",
162 "nat",
163 "-A",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700164 LOCAL_NAT_POSTROUTING,
JP Abgrall659692a2013-03-14 20:07:17 -0700165 "-o",
166 extIface,
167 "-j",
168 "MASQUERADE"
169 };
170 if (runCmd(ARRAY_SIZE(cmd), cmd)) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700171 ALOGE("Error setting postroute rule: iface=%s", extIface);
JP Abgrall659692a2013-03-14 20:07:17 -0700172 // unwind what's been done, but don't care about success - what more could we do?
JP Abgrall659692a2013-03-14 20:07:17 -0700173 setDefaults();
174 return -1;
175 }
176 }
177
JP Abgrall659692a2013-03-14 20:07:17 -0700178 if (setForwardRules(true, intIface, extIface) != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000179 ALOGE("Error setting forward rules");
JP Abgrall659692a2013-03-14 20:07:17 -0700180 if (natCount == 0) {
181 setDefaults();
182 }
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700183 errno = ENODEV;
184 return -1;
185 }
186
JP Abgrall0031cea2012-04-17 16:38:23 -0700187 /* Always make sure the drop rule is at the end */
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800188 const char *cmd1[] = {
189 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400190 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800191 "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700192 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800193 "-j",
194 "DROP"
195 };
196 runCmd(ARRAY_SIZE(cmd1), cmd1);
197 const char *cmd2[] = {
198 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400199 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800200 "-A",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700201 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800202 "-j",
203 "DROP"
204 };
205 runCmd(ARRAY_SIZE(cmd2), cmd2);
JP Abgrall0031cea2012-04-17 16:38:23 -0700206
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700207 natCount++;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700208 return 0;
209}
210
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700211bool NatController::checkTetherCountingRuleExist(const char *pair_name) {
212 std::list<std::string>::iterator it;
213
214 for (it = ifacePairList.begin(); it != ifacePairList.end(); it++) {
215 if (*it == pair_name) {
216 /* We already have this counter */
217 return true;
218 }
219 }
220 return false;
221}
222
JP Abgrallbaeccc42013-06-25 09:44:10 -0700223int NatController::setTetherCountingRules(bool add, const char *intIface, const char *extIface) {
224
225 /* We only ever add tethering quota rules so that they stick. */
226 if (!add) {
227 return 0;
228 }
Sreeram Ramachandran56afacf2014-05-28 15:07:00 -0700229 char *pair_name;
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700230 asprintf(&pair_name, "%s_%s", intIface, extIface);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700231
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700232 if (checkTetherCountingRuleExist(pair_name)) {
233 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700234 return 0;
235 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700236 const char *cmd2b[] = {
237 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400238 "-w",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700239 "-A",
240 LOCAL_TETHER_COUNTERS_CHAIN,
241 "-i",
242 intIface,
243 "-o",
244 extIface,
JP Abgrallbaeccc42013-06-25 09:44:10 -0700245 "-j",
246 "RETURN"
247 };
248
249 if (runCmd(ARRAY_SIZE(cmd2b), cmd2b) && add) {
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700250 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700251 return -1;
252 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700253 ifacePairList.push_front(pair_name);
254 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700255
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700256 asprintf(&pair_name, "%s_%s", extIface, intIface);
257 if (checkTetherCountingRuleExist(pair_name)) {
258 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700259 return 0;
260 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700261
262 const char *cmd3b[] = {
263 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400264 "-w",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700265 "-A",
266 LOCAL_TETHER_COUNTERS_CHAIN,
267 "-i",
268 extIface,
269 "-o",
270 intIface,
JP Abgrallbaeccc42013-06-25 09:44:10 -0700271 "-j",
272 "RETURN"
273 };
274
275 if (runCmd(ARRAY_SIZE(cmd3b), cmd3b) && add) {
276 // unwind what's been done, but don't care about success - what more could we do?
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700277 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700278 return -1;
279 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700280 ifacePairList.push_front(pair_name);
281 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700282 return 0;
283}
284
285int NatController::setForwardRules(bool add, const char *intIface, const char *extIface) {
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800286 const char *cmd1[] = {
287 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400288 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800289 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700290 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800291 "-i",
292 extIface,
293 "-o",
294 intIface,
295 "-m",
296 "state",
297 "--state",
298 "ESTABLISHED,RELATED",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700299 "-g",
300 LOCAL_TETHER_COUNTERS_CHAIN
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800301 };
302 int rc = 0;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700303
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800304 if (runCmd(ARRAY_SIZE(cmd1), cmd1) && add) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800305 return -1;
306 }
307
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800308 const char *cmd2[] = {
309 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400310 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800311 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700312 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800313 "-i",
314 intIface,
315 "-o",
316 extIface,
317 "-m",
318 "state",
319 "--state",
320 "INVALID",
321 "-j",
322 "DROP"
323 };
324
325 const char *cmd3[] = {
326 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400327 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800328 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700329 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800330 "-i",
331 intIface,
332 "-o",
333 extIface,
JP Abgrallbaeccc42013-06-25 09:44:10 -0700334 "-g",
335 LOCAL_TETHER_COUNTERS_CHAIN
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800336 };
337
338 if (runCmd(ARRAY_SIZE(cmd2), cmd2) && add) {
Robert Greenwaltf7bf29c2011-11-01 22:07:28 -0700339 // bail on error, but only if adding
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800340 rc = -1;
341 goto err_invalid_drop;
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700342 }
343
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800344 if (runCmd(ARRAY_SIZE(cmd3), cmd3) && add) {
Robert Greenwalt210b9772010-03-25 14:54:45 -0700345 // unwind what's been done, but don't care about success - what more could we do?
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800346 rc = -1;
347 goto err_return;
San Mehat9ff78fb2010-01-19 12:59:15 -0800348 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700349
JP Abgrallbaeccc42013-06-25 09:44:10 -0700350 if (setTetherCountingRules(add, intIface, extIface) && add) {
351 rc = -1;
352 goto err_return;
353 }
354
San Mehat9ff78fb2010-01-19 12:59:15 -0800355 return 0;
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800356
357err_return:
Paul Jensen94b2ab92015-08-04 10:35:05 -0400358 cmd2[2] = "-D";
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800359 runCmd(ARRAY_SIZE(cmd2), cmd2);
360err_invalid_drop:
Paul Jensen94b2ab92015-08-04 10:35:05 -0400361 cmd1[2] = "-D";
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800362 runCmd(ARRAY_SIZE(cmd1), cmd1);
363 return rc;
San Mehat9ff78fb2010-01-19 12:59:15 -0800364}
365
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700366int NatController::disableNat(const char* intIface, const char* extIface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700367 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700368 errno = ENODEV;
369 return -1;
370 }
371
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700372 setForwardRules(false, intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700373 if (--natCount <= 0) {
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600374 // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0
375 setDefaults();
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700376 }
377 return 0;
San Mehat9ff78fb2010-01-19 12:59:15 -0800378}