blob: 5a15afa914543188be6d0259ad5b1f0575eb6e2b [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
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070043NatController::NatController() {
San Mehat9ff78fb2010-01-19 12:59:15 -080044}
45
46NatController::~NatController() {
47}
48
JP Abgrall4ae80de2013-03-14 20:06:20 -070049struct CommandsAndArgs {
50 /* The array size doesn't really matter as the compiler will barf if too many initializers are specified. */
51 const char *cmd[32];
52 bool checkRes;
53};
54
Rom Lemarchand001f0a42013-01-31 12:41:03 -080055int NatController::runCmd(int argc, const char **argv) {
JP Abgrall11b4e9b2011-08-11 15:34:49 -070056 int res;
San Mehat9ff78fb2010-01-19 12:59:15 -080057
Rom Lemarchand001f0a42013-01-31 12:41:03 -080058 res = android_fork_execvp(argc, (char **)argv, NULL, false, false);
JP Abgrallbaeccc42013-06-25 09:44:10 -070059
60#if !LOG_NDEBUG
61 std::string full_cmd = argv[0];
62 argc--; argv++;
63 /*
64 * HACK: Sometimes runCmd() is called with a ridcously large value (32)
65 * and it works because the argv[] contains a NULL after the last
66 * true argv. So here we use the NULL argv[] to terminate when the argc
67 * is horribly wrong, and argc for the normal cases.
68 */
69 for (; argc && argv[0]; argc--, argv++) {
70 full_cmd += " ";
71 full_cmd += argv[0];
72 }
73 ALOGV("runCmd(%s) res=%d", full_cmd.c_str(), res);
74#endif
JP Abgrall11b4e9b2011-08-11 15:34:49 -070075 return res;
San Mehat9ff78fb2010-01-19 12:59:15 -080076}
77
JP Abgrall0031cea2012-04-17 16:38:23 -070078int NatController::setupIptablesHooks() {
JP Abgrallbaeccc42013-06-25 09:44:10 -070079 int res;
80 res = setDefaults();
81 if (res < 0) {
82 return res;
83 }
84
85 struct CommandsAndArgs defaultCommands[] = {
86 /*
Gordon Gao6b6f22f2014-09-18 11:50:09 -070087 * First chain is for tethering counters.
JP Abgrallbaeccc42013-06-25 09:44:10 -070088 * This chain is reached via --goto, and then RETURNS.
Gordon Gao6b6f22f2014-09-18 11:50:09 -070089 *
90 * Second chain is used to limit downstream mss to the upstream pmtu
91 * so we don't end up fragmenting every large packet tethered devices
92 * send. Note this feature requires kernel support with flag
93 * CONFIG_NETFILTER_XT_TARGET_TCPMSS=y, which not all builds will have,
94 * so the final rule is allowed to fail.
95 * Bug 17629786 asks to make the failure more obvious, or even fatal
96 * so that all builds eventually gain the performance improvement.
JP Abgrallbaeccc42013-06-25 09:44:10 -070097 */
98 {{IPTABLES_PATH, "-F", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
99 {{IPTABLES_PATH, "-X", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
100 {{IPTABLES_PATH, "-N", LOCAL_TETHER_COUNTERS_CHAIN,}, 1},
Lorenzo Colittie8164dd2014-10-02 20:46:23 +0900101 {{IPTABLES_PATH, "-t", "mangle", "-A", LOCAL_MANGLE_FORWARD, "-p", "tcp", "--tcp-flags",
Gordon Gao6b6f22f2014-09-18 11:50:09 -0700102 "SYN", "SYN", "-j", "TCPMSS", "--clamp-mss-to-pmtu"}, 0},
JP Abgrallbaeccc42013-06-25 09:44:10 -0700103 };
104 for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) {
105 if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) &&
106 defaultCommands[cmdNum].checkRes) {
107 return -1;
108 }
109 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700110 ifacePairList.clear();
JP Abgrallbaeccc42013-06-25 09:44:10 -0700111
JP Abgrall0031cea2012-04-17 16:38:23 -0700112 return 0;
113}
114
115int NatController::setDefaults() {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700116 /*
117 * The following only works because:
118 * - the defaultsCommands[].cmd array is padded with NULL, and
119 * - the 1st argc of runCmd() will just be the max for the CommandsAndArgs[].cmd, and
120 * - internally it will be memcopied to an array and terminated with a NULL.
121 */
JP Abgrall4ae80de2013-03-14 20:06:20 -0700122 struct CommandsAndArgs defaultCommands[] = {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700123 {{IPTABLES_PATH, "-F", LOCAL_FORWARD,}, 1},
124 {{IPTABLES_PATH, "-A", LOCAL_FORWARD, "-j", "DROP"}, 1},
125 {{IPTABLES_PATH, "-t", "nat", "-F", LOCAL_NAT_POSTROUTING}, 1},
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800126 };
JP Abgrall4ae80de2013-03-14 20:06:20 -0700127 for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) {
128 if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) &&
129 defaultCommands[cmdNum].checkRes) {
130 return -1;
131 }
132 }
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700133
134 natCount = 0;
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600135
San Mehat9ff78fb2010-01-19 12:59:15 -0800136 return 0;
137}
138
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700139int NatController::enableNat(const char* intIface, const char* extIface) {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700140 ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface);
141
JP Abgrall69261cb2014-06-19 18:35:24 -0700142 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800143 errno = ENODEV;
144 return -1;
145 }
146
JP Abgrallbaeccc42013-06-25 09:44:10 -0700147 /* Bug: b/9565268. "enableNat wlan0 wlan0". For now we fail until java-land is fixed */
148 if (!strcmp(intIface, extIface)) {
149 ALOGE("Duplicate interface specified: %s %s", intIface, extIface);
150 errno = EINVAL;
151 return -1;
152 }
153
JP Abgrall659692a2013-03-14 20:07:17 -0700154 // add this if we are the first added nat
155 if (natCount == 0) {
156 const char *cmd[] = {
157 IPTABLES_PATH,
158 "-t",
159 "nat",
160 "-A",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700161 LOCAL_NAT_POSTROUTING,
JP Abgrall659692a2013-03-14 20:07:17 -0700162 "-o",
163 extIface,
164 "-j",
165 "MASQUERADE"
166 };
167 if (runCmd(ARRAY_SIZE(cmd), cmd)) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700168 ALOGE("Error setting postroute rule: iface=%s", extIface);
JP Abgrall659692a2013-03-14 20:07:17 -0700169 // unwind what's been done, but don't care about success - what more could we do?
JP Abgrall659692a2013-03-14 20:07:17 -0700170 setDefaults();
171 return -1;
172 }
173 }
174
JP Abgrall659692a2013-03-14 20:07:17 -0700175 if (setForwardRules(true, intIface, extIface) != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000176 ALOGE("Error setting forward rules");
JP Abgrall659692a2013-03-14 20:07:17 -0700177 if (natCount == 0) {
178 setDefaults();
179 }
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700180 errno = ENODEV;
181 return -1;
182 }
183
JP Abgrall0031cea2012-04-17 16:38:23 -0700184 /* Always make sure the drop rule is at the end */
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800185 const char *cmd1[] = {
186 IPTABLES_PATH,
187 "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700188 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800189 "-j",
190 "DROP"
191 };
192 runCmd(ARRAY_SIZE(cmd1), cmd1);
193 const char *cmd2[] = {
194 IPTABLES_PATH,
195 "-A",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700196 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800197 "-j",
198 "DROP"
199 };
200 runCmd(ARRAY_SIZE(cmd2), cmd2);
JP Abgrall0031cea2012-04-17 16:38:23 -0700201
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700202 natCount++;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700203 return 0;
204}
205
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700206bool NatController::checkTetherCountingRuleExist(const char *pair_name) {
207 std::list<std::string>::iterator it;
208
209 for (it = ifacePairList.begin(); it != ifacePairList.end(); it++) {
210 if (*it == pair_name) {
211 /* We already have this counter */
212 return true;
213 }
214 }
215 return false;
216}
217
JP Abgrallbaeccc42013-06-25 09:44:10 -0700218int NatController::setTetherCountingRules(bool add, const char *intIface, const char *extIface) {
219
220 /* We only ever add tethering quota rules so that they stick. */
221 if (!add) {
222 return 0;
223 }
Sreeram Ramachandran56afacf2014-05-28 15:07:00 -0700224 char *pair_name;
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700225 asprintf(&pair_name, "%s_%s", intIface, extIface);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700226
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700227 if (checkTetherCountingRuleExist(pair_name)) {
228 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700229 return 0;
230 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700231 const char *cmd2b[] = {
232 IPTABLES_PATH,
233 "-A",
234 LOCAL_TETHER_COUNTERS_CHAIN,
235 "-i",
236 intIface,
237 "-o",
238 extIface,
JP Abgrallbaeccc42013-06-25 09:44:10 -0700239 "-j",
240 "RETURN"
241 };
242
243 if (runCmd(ARRAY_SIZE(cmd2b), cmd2b) && add) {
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700244 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700245 return -1;
246 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700247 ifacePairList.push_front(pair_name);
248 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700249
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700250 asprintf(&pair_name, "%s_%s", extIface, intIface);
251 if (checkTetherCountingRuleExist(pair_name)) {
252 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700253 return 0;
254 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700255
256 const char *cmd3b[] = {
257 IPTABLES_PATH,
258 "-A",
259 LOCAL_TETHER_COUNTERS_CHAIN,
260 "-i",
261 extIface,
262 "-o",
263 intIface,
JP Abgrallbaeccc42013-06-25 09:44:10 -0700264 "-j",
265 "RETURN"
266 };
267
268 if (runCmd(ARRAY_SIZE(cmd3b), cmd3b) && add) {
269 // unwind what's been done, but don't care about success - what more could we do?
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700270 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700271 return -1;
272 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700273 ifacePairList.push_front(pair_name);
274 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700275 return 0;
276}
277
278int NatController::setForwardRules(bool add, const char *intIface, const char *extIface) {
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800279 const char *cmd1[] = {
280 IPTABLES_PATH,
281 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700282 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800283 "-i",
284 extIface,
285 "-o",
286 intIface,
287 "-m",
288 "state",
289 "--state",
290 "ESTABLISHED,RELATED",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700291 "-g",
292 LOCAL_TETHER_COUNTERS_CHAIN
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800293 };
294 int rc = 0;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700295
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800296 if (runCmd(ARRAY_SIZE(cmd1), cmd1) && add) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800297 return -1;
298 }
299
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800300 const char *cmd2[] = {
301 IPTABLES_PATH,
302 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700303 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800304 "-i",
305 intIface,
306 "-o",
307 extIface,
308 "-m",
309 "state",
310 "--state",
311 "INVALID",
312 "-j",
313 "DROP"
314 };
315
316 const char *cmd3[] = {
317 IPTABLES_PATH,
318 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700319 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800320 "-i",
321 intIface,
322 "-o",
323 extIface,
JP Abgrallbaeccc42013-06-25 09:44:10 -0700324 "-g",
325 LOCAL_TETHER_COUNTERS_CHAIN
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800326 };
327
328 if (runCmd(ARRAY_SIZE(cmd2), cmd2) && add) {
Robert Greenwaltf7bf29c2011-11-01 22:07:28 -0700329 // bail on error, but only if adding
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800330 rc = -1;
331 goto err_invalid_drop;
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700332 }
333
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800334 if (runCmd(ARRAY_SIZE(cmd3), cmd3) && add) {
Robert Greenwalt210b9772010-03-25 14:54:45 -0700335 // unwind what's been done, but don't care about success - what more could we do?
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800336 rc = -1;
337 goto err_return;
San Mehat9ff78fb2010-01-19 12:59:15 -0800338 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700339
JP Abgrallbaeccc42013-06-25 09:44:10 -0700340 if (setTetherCountingRules(add, intIface, extIface) && add) {
341 rc = -1;
342 goto err_return;
343 }
344
San Mehat9ff78fb2010-01-19 12:59:15 -0800345 return 0;
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800346
347err_return:
348 cmd2[1] = "-D";
349 runCmd(ARRAY_SIZE(cmd2), cmd2);
350err_invalid_drop:
351 cmd1[1] = "-D";
352 runCmd(ARRAY_SIZE(cmd1), cmd1);
353 return rc;
San Mehat9ff78fb2010-01-19 12:59:15 -0800354}
355
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700356int NatController::disableNat(const char* intIface, const char* extIface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700357 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700358 errno = ENODEV;
359 return -1;
360 }
361
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700362 setForwardRules(false, intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700363 if (--natCount <= 0) {
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600364 // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0
365 setDefaults();
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700366 }
367 return 0;
San Mehat9ff78fb2010-01-19 12:59:15 -0800368}