blob: d9a779cac5dbfe4e20e9cc9c0317e48b95183cda [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 /*
Gordon Gao6b6f22f2014-09-18 11:50:09 -070086 * First chain is for tethering counters.
JP Abgrallbaeccc42013-06-25 09:44:10 -070087 * This chain is reached via --goto, and then RETURNS.
Gordon Gao6b6f22f2014-09-18 11:50:09 -070088 *
89 * Second chain is used to limit downstream mss to the upstream pmtu
90 * so we don't end up fragmenting every large packet tethered devices
91 * send. Note this feature requires kernel support with flag
92 * CONFIG_NETFILTER_XT_TARGET_TCPMSS=y, which not all builds will have,
93 * so the final rule is allowed to fail.
94 * Bug 17629786 asks to make the failure more obvious, or even fatal
95 * so that all builds eventually gain the performance improvement.
JP Abgrallbaeccc42013-06-25 09:44:10 -070096 */
97 {{IPTABLES_PATH, "-F", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
98 {{IPTABLES_PATH, "-X", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
99 {{IPTABLES_PATH, "-N", LOCAL_TETHER_COUNTERS_CHAIN,}, 1},
Gordon Gao6b6f22f2014-09-18 11:50:09 -0700100 {{IPTABLES_PATH, "-t", "mangle", "-F", LOCAL_FORWARD,}, 0},
101 {{IPTABLES_PATH, "-t", "mangle", "-X", LOCAL_FORWARD,}, 0},
102 {{IPTABLES_PATH, "-t", "mangle", "-N", LOCAL_FORWARD,}, 1},
103 {{IPTABLES_PATH, "-t", "mangle", "-A", LOCAL_FORWARD, "-p", "tcp", "--tcp-flags",
104 "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[] = {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700125 {{IPTABLES_PATH, "-F", LOCAL_FORWARD,}, 1},
126 {{IPTABLES_PATH, "-A", LOCAL_FORWARD, "-j", "DROP"}, 1},
127 {{IPTABLES_PATH, "-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,
160 "-t",
161 "nat",
162 "-A",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700163 LOCAL_NAT_POSTROUTING,
JP Abgrall659692a2013-03-14 20:07:17 -0700164 "-o",
165 extIface,
166 "-j",
167 "MASQUERADE"
168 };
169 if (runCmd(ARRAY_SIZE(cmd), cmd)) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700170 ALOGE("Error setting postroute rule: iface=%s", extIface);
JP Abgrall659692a2013-03-14 20:07:17 -0700171 // unwind what's been done, but don't care about success - what more could we do?
JP Abgrall659692a2013-03-14 20:07:17 -0700172 setDefaults();
173 return -1;
174 }
175 }
176
JP Abgrall659692a2013-03-14 20:07:17 -0700177 if (setForwardRules(true, intIface, extIface) != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000178 ALOGE("Error setting forward rules");
JP Abgrall659692a2013-03-14 20:07:17 -0700179 if (natCount == 0) {
180 setDefaults();
181 }
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700182 errno = ENODEV;
183 return -1;
184 }
185
JP Abgrall0031cea2012-04-17 16:38:23 -0700186 /* Always make sure the drop rule is at the end */
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800187 const char *cmd1[] = {
188 IPTABLES_PATH,
189 "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700190 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800191 "-j",
192 "DROP"
193 };
194 runCmd(ARRAY_SIZE(cmd1), cmd1);
195 const char *cmd2[] = {
196 IPTABLES_PATH,
197 "-A",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700198 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800199 "-j",
200 "DROP"
201 };
202 runCmd(ARRAY_SIZE(cmd2), cmd2);
JP Abgrall0031cea2012-04-17 16:38:23 -0700203
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700204 if (int ret = RouteController::enableTethering(intIface, extIface)) {
205 ALOGE("failed to add tethering rule for iif=%s oif=%s", intIface, extIface);
Sreeram Ramachandran8b3b91c2014-07-22 12:40:36 -0700206 if (natCount == 0) {
207 setDefaults();
208 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700209 errno = -ret;
210 return -1;
211 }
212
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700213 natCount++;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700214 return 0;
215}
216
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700217bool NatController::checkTetherCountingRuleExist(const char *pair_name) {
218 std::list<std::string>::iterator it;
219
220 for (it = ifacePairList.begin(); it != ifacePairList.end(); it++) {
221 if (*it == pair_name) {
222 /* We already have this counter */
223 return true;
224 }
225 }
226 return false;
227}
228
JP Abgrallbaeccc42013-06-25 09:44:10 -0700229int NatController::setTetherCountingRules(bool add, const char *intIface, const char *extIface) {
230
231 /* We only ever add tethering quota rules so that they stick. */
232 if (!add) {
233 return 0;
234 }
Sreeram Ramachandran56afacf2014-05-28 15:07:00 -0700235 char *pair_name;
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700236 asprintf(&pair_name, "%s_%s", intIface, extIface);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700237
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700238 if (checkTetherCountingRuleExist(pair_name)) {
239 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700240 return 0;
241 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700242 const char *cmd2b[] = {
243 IPTABLES_PATH,
244 "-A",
245 LOCAL_TETHER_COUNTERS_CHAIN,
246 "-i",
247 intIface,
248 "-o",
249 extIface,
JP Abgrallbaeccc42013-06-25 09:44:10 -0700250 "-j",
251 "RETURN"
252 };
253
254 if (runCmd(ARRAY_SIZE(cmd2b), cmd2b) && add) {
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700255 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700256 return -1;
257 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700258 ifacePairList.push_front(pair_name);
259 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700260
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700261 asprintf(&pair_name, "%s_%s", extIface, intIface);
262 if (checkTetherCountingRuleExist(pair_name)) {
263 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700264 return 0;
265 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700266
267 const char *cmd3b[] = {
268 IPTABLES_PATH,
269 "-A",
270 LOCAL_TETHER_COUNTERS_CHAIN,
271 "-i",
272 extIface,
273 "-o",
274 intIface,
JP Abgrallbaeccc42013-06-25 09:44:10 -0700275 "-j",
276 "RETURN"
277 };
278
279 if (runCmd(ARRAY_SIZE(cmd3b), cmd3b) && add) {
280 // unwind what's been done, but don't care about success - what more could we do?
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700281 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700282 return -1;
283 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700284 ifacePairList.push_front(pair_name);
285 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700286 return 0;
287}
288
289int NatController::setForwardRules(bool add, const char *intIface, const char *extIface) {
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800290 const char *cmd1[] = {
291 IPTABLES_PATH,
292 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700293 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800294 "-i",
295 extIface,
296 "-o",
297 intIface,
298 "-m",
299 "state",
300 "--state",
301 "ESTABLISHED,RELATED",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700302 "-g",
303 LOCAL_TETHER_COUNTERS_CHAIN
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800304 };
305 int rc = 0;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700306
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800307 if (runCmd(ARRAY_SIZE(cmd1), cmd1) && add) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800308 return -1;
309 }
310
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800311 const char *cmd2[] = {
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,
319 "-m",
320 "state",
321 "--state",
322 "INVALID",
323 "-j",
324 "DROP"
325 };
326
327 const char *cmd3[] = {
328 IPTABLES_PATH,
329 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700330 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800331 "-i",
332 intIface,
333 "-o",
334 extIface,
JP Abgrallbaeccc42013-06-25 09:44:10 -0700335 "-g",
336 LOCAL_TETHER_COUNTERS_CHAIN
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800337 };
338
339 if (runCmd(ARRAY_SIZE(cmd2), cmd2) && add) {
Robert Greenwaltf7bf29c2011-11-01 22:07:28 -0700340 // bail on error, but only if adding
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800341 rc = -1;
342 goto err_invalid_drop;
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700343 }
344
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800345 if (runCmd(ARRAY_SIZE(cmd3), cmd3) && add) {
Robert Greenwalt210b9772010-03-25 14:54:45 -0700346 // unwind what's been done, but don't care about success - what more could we do?
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800347 rc = -1;
348 goto err_return;
San Mehat9ff78fb2010-01-19 12:59:15 -0800349 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700350
JP Abgrallbaeccc42013-06-25 09:44:10 -0700351 if (setTetherCountingRules(add, intIface, extIface) && add) {
352 rc = -1;
353 goto err_return;
354 }
355
San Mehat9ff78fb2010-01-19 12:59:15 -0800356 return 0;
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800357
358err_return:
359 cmd2[1] = "-D";
360 runCmd(ARRAY_SIZE(cmd2), cmd2);
361err_invalid_drop:
362 cmd1[1] = "-D";
363 runCmd(ARRAY_SIZE(cmd1), cmd1);
364 return rc;
San Mehat9ff78fb2010-01-19 12:59:15 -0800365}
366
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700367int NatController::disableNat(const char* intIface, const char* extIface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700368 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700369 errno = ENODEV;
370 return -1;
371 }
372
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700373 if (int ret = RouteController::disableTethering(intIface, extIface)) {
374 ALOGE("failed to remove tethering rule for iif=%s oif=%s", intIface, extIface);
375 errno = -ret;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700376 return -1;
377 }
378
379 setForwardRules(false, intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700380 if (--natCount <= 0) {
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600381 // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0
382 setDefaults();
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700383 }
384 return 0;
San Mehat9ff78fb2010-01-19 12:59:15 -0800385}