blob: 81909c2f6e590e899713cc6cbbe444b16cb79305 [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";
Lorenzo Colitti8917e452016-08-01 16:47:50 +090041const char* NatController::LOCAL_RAW_PREROUTING = "natctrl_raw_PREROUTING";
JP Abgrallbaeccc42013-06-25 09:44:10 -070042const char* NatController::LOCAL_TETHER_COUNTERS_CHAIN = "natctrl_tether_counters";
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070043
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +090044auto NatController::execFunction = android_fork_execvp;
45
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070046NatController::NatController() {
San Mehat9ff78fb2010-01-19 12:59:15 -080047}
48
49NatController::~NatController() {
50}
51
JP Abgrall4ae80de2013-03-14 20:06:20 -070052struct CommandsAndArgs {
53 /* The array size doesn't really matter as the compiler will barf if too many initializers are specified. */
54 const char *cmd[32];
55 bool checkRes;
56};
57
Rom Lemarchand001f0a42013-01-31 12:41:03 -080058int NatController::runCmd(int argc, const char **argv) {
JP Abgrall11b4e9b2011-08-11 15:34:49 -070059 int res;
San Mehat9ff78fb2010-01-19 12:59:15 -080060
Lorenzo Colitti8e1cee92016-07-09 14:24:08 +090061 res = execFunction(argc, (char **)argv, NULL, false, false);
JP Abgrallbaeccc42013-06-25 09:44:10 -070062
63#if !LOG_NDEBUG
64 std::string full_cmd = argv[0];
65 argc--; argv++;
66 /*
67 * HACK: Sometimes runCmd() is called with a ridcously large value (32)
68 * and it works because the argv[] contains a NULL after the last
69 * true argv. So here we use the NULL argv[] to terminate when the argc
70 * is horribly wrong, and argc for the normal cases.
71 */
72 for (; argc && argv[0]; argc--, argv++) {
73 full_cmd += " ";
74 full_cmd += argv[0];
75 }
76 ALOGV("runCmd(%s) res=%d", full_cmd.c_str(), res);
77#endif
JP Abgrall11b4e9b2011-08-11 15:34:49 -070078 return res;
San Mehat9ff78fb2010-01-19 12:59:15 -080079}
80
JP Abgrall0031cea2012-04-17 16:38:23 -070081int NatController::setupIptablesHooks() {
JP Abgrallbaeccc42013-06-25 09:44:10 -070082 int res;
83 res = setDefaults();
84 if (res < 0) {
85 return res;
86 }
87
88 struct CommandsAndArgs defaultCommands[] = {
89 /*
Lorenzo Colitti05cfd252016-07-10 23:15:46 +090090 * This is for tethering counters.
JP Abgrallbaeccc42013-06-25 09:44:10 -070091 * This chain is reached via --goto, and then RETURNS.
Lorenzo Colitti05cfd252016-07-10 23:15:46 +090092 */
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +053093 {{IPTABLES_PATH, "-w", "-W", IPTABLES_RETRY_INTERVAL, "-F", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
94 {{IP6TABLES_PATH, "-w", "-W", IPTABLES_RETRY_INTERVAL, "-F", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
95 {{IPTABLES_PATH, "-w", "-W", IPTABLES_RETRY_INTERVAL, "-X", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
96 {{IP6TABLES_PATH, "-w", "-W", IPTABLES_RETRY_INTERVAL, "-X", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
97 {{IPTABLES_PATH, "-w", "-W", IPTABLES_RETRY_INTERVAL, "-N", LOCAL_TETHER_COUNTERS_CHAIN,}, 1},
98 {{IP6TABLES_PATH, "-w", "-W", IPTABLES_RETRY_INTERVAL, "-N", LOCAL_TETHER_COUNTERS_CHAIN,}, 1},
Lorenzo Colitti05cfd252016-07-10 23:15:46 +090099
100 /*
Gordon Gao6b6f22f2014-09-18 11:50:09 -0700101 * Second chain is used to limit downstream mss to the upstream pmtu
102 * so we don't end up fragmenting every large packet tethered devices
103 * send. Note this feature requires kernel support with flag
104 * CONFIG_NETFILTER_XT_TARGET_TCPMSS=y, which not all builds will have,
105 * so the final rule is allowed to fail.
106 * Bug 17629786 asks to make the failure more obvious, or even fatal
107 * so that all builds eventually gain the performance improvement.
JP Abgrallbaeccc42013-06-25 09:44:10 -0700108 */
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530109 {{IPTABLES_PATH, "-w", "-W", IPTABLES_RETRY_INTERVAL, "-t", "mangle", "-A",
110 LOCAL_MANGLE_FORWARD, "-p", "tcp", "--tcp-flags",
111 "SYN", "SYN", "-j", "TCPMSS", "--clamp-mss-to-pmtu"}, 0},
JP Abgrallbaeccc42013-06-25 09:44:10 -0700112 };
113 for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) {
114 if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) &&
115 defaultCommands[cmdNum].checkRes) {
116 return -1;
117 }
118 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700119 ifacePairList.clear();
JP Abgrallbaeccc42013-06-25 09:44:10 -0700120
JP Abgrall0031cea2012-04-17 16:38:23 -0700121 return 0;
122}
123
124int NatController::setDefaults() {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700125 /*
126 * The following only works because:
127 * - the defaultsCommands[].cmd array is padded with NULL, and
128 * - the 1st argc of runCmd() will just be the max for the CommandsAndArgs[].cmd, and
129 * - internally it will be memcopied to an array and terminated with a NULL.
130 */
JP Abgrall4ae80de2013-03-14 20:06:20 -0700131 struct CommandsAndArgs defaultCommands[] = {
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530132 {{IPTABLES_PATH, "-w", "-W", IPTABLES_RETRY_INTERVAL, "-F", LOCAL_FORWARD,}, 1},
133 {{IP6TABLES_PATH, "-w", "-W", IPTABLES_RETRY_INTERVAL, "-F", LOCAL_FORWARD,}, 1},
134 {{IPTABLES_PATH, "-w", "-W", IPTABLES_RETRY_INTERVAL, "-A", LOCAL_FORWARD, "-j", "DROP"}, 1},
135 {{IPTABLES_PATH, "-w", "-W", IPTABLES_RETRY_INTERVAL, "-t", "nat", "-F", LOCAL_NAT_POSTROUTING}, 1},
136 {{IP6TABLES_PATH, "-w", "-W", IPTABLES_RETRY_INTERVAL, "-t", "raw", "-F", LOCAL_RAW_PREROUTING}, 1},
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800137 };
JP Abgrall4ae80de2013-03-14 20:06:20 -0700138 for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) {
139 if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) &&
140 defaultCommands[cmdNum].checkRes) {
141 return -1;
142 }
143 }
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700144
145 natCount = 0;
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600146
San Mehat9ff78fb2010-01-19 12:59:15 -0800147 return 0;
148}
149
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700150int NatController::enableNat(const char* intIface, const char* extIface) {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700151 ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface);
152
JP Abgrall69261cb2014-06-19 18:35:24 -0700153 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800154 errno = ENODEV;
155 return -1;
156 }
157
JP Abgrallbaeccc42013-06-25 09:44:10 -0700158 /* Bug: b/9565268. "enableNat wlan0 wlan0". For now we fail until java-land is fixed */
159 if (!strcmp(intIface, extIface)) {
160 ALOGE("Duplicate interface specified: %s %s", intIface, extIface);
161 errno = EINVAL;
162 return -1;
163 }
164
JP Abgrall659692a2013-03-14 20:07:17 -0700165 // add this if we are the first added nat
166 if (natCount == 0) {
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900167 const char *v4Cmd[] = {
JP Abgrall659692a2013-03-14 20:07:17 -0700168 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400169 "-w",
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530170 "-W",
171 IPTABLES_RETRY_INTERVAL,
JP Abgrall659692a2013-03-14 20:07:17 -0700172 "-t",
173 "nat",
174 "-A",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700175 LOCAL_NAT_POSTROUTING,
JP Abgrall659692a2013-03-14 20:07:17 -0700176 "-o",
177 extIface,
178 "-j",
179 "MASQUERADE"
180 };
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900181
182 /*
183 * IPv6 tethering doesn't need the state-based conntrack rules, so
184 * it unconditionally jumps to the tether counters chain all the time.
185 */
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530186 const char *v6Cmd[] = {IP6TABLES_PATH, "-w", "-W", IPTABLES_RETRY_INTERVAL,
187 "-A", LOCAL_FORWARD, "-g", LOCAL_TETHER_COUNTERS_CHAIN};
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900188
189 if (runCmd(ARRAY_SIZE(v4Cmd), v4Cmd) || runCmd(ARRAY_SIZE(v6Cmd), v6Cmd)) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700190 ALOGE("Error setting postroute rule: iface=%s", extIface);
JP Abgrall659692a2013-03-14 20:07:17 -0700191 // unwind what's been done, but don't care about success - what more could we do?
JP Abgrall659692a2013-03-14 20:07:17 -0700192 setDefaults();
193 return -1;
194 }
195 }
196
JP Abgrall659692a2013-03-14 20:07:17 -0700197 if (setForwardRules(true, intIface, extIface) != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000198 ALOGE("Error setting forward rules");
JP Abgrall659692a2013-03-14 20:07:17 -0700199 if (natCount == 0) {
200 setDefaults();
201 }
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700202 errno = ENODEV;
203 return -1;
204 }
205
JP Abgrall0031cea2012-04-17 16:38:23 -0700206 /* Always make sure the drop rule is at the end */
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800207 const char *cmd1[] = {
208 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400209 "-w",
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530210 "-W",
211 IPTABLES_RETRY_INTERVAL,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800212 "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700213 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800214 "-j",
215 "DROP"
216 };
217 runCmd(ARRAY_SIZE(cmd1), cmd1);
218 const char *cmd2[] = {
219 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400220 "-w",
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530221 "-W",
222 IPTABLES_RETRY_INTERVAL,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800223 "-A",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700224 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800225 "-j",
226 "DROP"
227 };
228 runCmd(ARRAY_SIZE(cmd2), cmd2);
JP Abgrall0031cea2012-04-17 16:38:23 -0700229
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700230 natCount++;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700231 return 0;
232}
233
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700234bool NatController::checkTetherCountingRuleExist(const char *pair_name) {
235 std::list<std::string>::iterator it;
236
237 for (it = ifacePairList.begin(); it != ifacePairList.end(); it++) {
238 if (*it == pair_name) {
239 /* We already have this counter */
240 return true;
241 }
242 }
243 return false;
244}
245
JP Abgrallbaeccc42013-06-25 09:44:10 -0700246int NatController::setTetherCountingRules(bool add, const char *intIface, const char *extIface) {
247
248 /* We only ever add tethering quota rules so that they stick. */
249 if (!add) {
250 return 0;
251 }
Sreeram Ramachandran56afacf2014-05-28 15:07:00 -0700252 char *pair_name;
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700253 asprintf(&pair_name, "%s_%s", intIface, extIface);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700254
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700255 if (checkTetherCountingRuleExist(pair_name)) {
256 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700257 return 0;
258 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700259 const char *cmd2b[] = {
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900260 IPTABLES_PATH,
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530261 "-w", "-W", IPTABLES_RETRY_INTERVAL, "-A", LOCAL_TETHER_COUNTERS_CHAIN,
262 "-i", intIface, "-o", extIface, "-j", "RETURN"
JP Abgrallbaeccc42013-06-25 09:44:10 -0700263 };
264
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900265 const char *cmd2c[] = {
266 IP6TABLES_PATH,
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530267 "-w", "-W", IPTABLES_RETRY_INTERVAL, "-A", LOCAL_TETHER_COUNTERS_CHAIN,
268 "-i", intIface, "-o", extIface, "-j", "RETURN"
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900269 };
270
271 if (runCmd(ARRAY_SIZE(cmd2b), cmd2b) || runCmd(ARRAY_SIZE(cmd2c), cmd2c)) {
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700272 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700273 return -1;
274 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700275 ifacePairList.push_front(pair_name);
276 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700277
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700278 asprintf(&pair_name, "%s_%s", extIface, intIface);
279 if (checkTetherCountingRuleExist(pair_name)) {
280 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700281 return 0;
282 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700283
284 const char *cmd3b[] = {
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900285 IPTABLES_PATH,
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530286 "-w", "-W", IPTABLES_RETRY_INTERVAL, "-A", LOCAL_TETHER_COUNTERS_CHAIN,
287 "-i", extIface, "-o", intIface, "-j", "RETURN"
JP Abgrallbaeccc42013-06-25 09:44:10 -0700288 };
289
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900290 const char *cmd3c[] = {
291 IP6TABLES_PATH,
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530292 "-w", "-W", IPTABLES_RETRY_INTERVAL, "-A", LOCAL_TETHER_COUNTERS_CHAIN,
293 "-i", extIface, "-o", intIface, "-j", "RETURN"
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900294 };
295
296 if (runCmd(ARRAY_SIZE(cmd3b), cmd3b) || runCmd(ARRAY_SIZE(cmd3c), cmd3c)) {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700297 // unwind what's been done, but don't care about success - what more could we do?
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700298 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700299 return -1;
300 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700301 ifacePairList.push_front(pair_name);
302 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700303 return 0;
304}
305
306int NatController::setForwardRules(bool add, const char *intIface, const char *extIface) {
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800307 const char *cmd1[] = {
308 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400309 "-w",
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530310 "-W",
311 IPTABLES_RETRY_INTERVAL,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800312 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700313 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800314 "-i",
315 extIface,
316 "-o",
317 intIface,
318 "-m",
319 "state",
320 "--state",
321 "ESTABLISHED,RELATED",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700322 "-g",
323 LOCAL_TETHER_COUNTERS_CHAIN
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800324 };
325 int rc = 0;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700326
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800327 if (runCmd(ARRAY_SIZE(cmd1), cmd1) && add) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800328 return -1;
329 }
330
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800331 const char *cmd2[] = {
332 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400333 "-w",
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530334 "-W",
335 IPTABLES_RETRY_INTERVAL,
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,
342 "-m",
343 "state",
344 "--state",
345 "INVALID",
346 "-j",
347 "DROP"
348 };
349
350 const char *cmd3[] = {
351 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400352 "-w",
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530353 "-W",
354 IPTABLES_RETRY_INTERVAL,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800355 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700356 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800357 "-i",
358 intIface,
359 "-o",
360 extIface,
JP Abgrallbaeccc42013-06-25 09:44:10 -0700361 "-g",
362 LOCAL_TETHER_COUNTERS_CHAIN
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800363 };
364
Lorenzo Colitti8917e452016-08-01 16:47:50 +0900365 const char *cmd4[] = {
366 IP6TABLES_PATH,
367 "-w",
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530368 "-W",
369 IPTABLES_RETRY_INTERVAL,
Lorenzo Colitti8917e452016-08-01 16:47:50 +0900370 "-t",
371 "raw",
372 add ? "-A" : "-D",
373 LOCAL_RAW_PREROUTING,
374 "-i",
375 intIface,
376 "-m",
377 "rpfilter",
378 "--invert",
379 "!",
380 "-s",
381 "fe80::/64",
382 "-j",
383 "DROP"
384 };
385
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800386 if (runCmd(ARRAY_SIZE(cmd2), cmd2) && add) {
Robert Greenwaltf7bf29c2011-11-01 22:07:28 -0700387 // bail on error, but only if adding
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800388 rc = -1;
389 goto err_invalid_drop;
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700390 }
391
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800392 if (runCmd(ARRAY_SIZE(cmd3), cmd3) && add) {
Robert Greenwalt210b9772010-03-25 14:54:45 -0700393 // unwind what's been done, but don't care about success - what more could we do?
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800394 rc = -1;
395 goto err_return;
San Mehat9ff78fb2010-01-19 12:59:15 -0800396 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700397
Lorenzo Colittibd96d952016-08-01 18:14:31 +0900398 if (runCmd(ARRAY_SIZE(cmd4), cmd4) && add) {
Lorenzo Colitti8917e452016-08-01 16:47:50 +0900399 rc = -1;
400 goto err_rpfilter;
401 }
402
JP Abgrallbaeccc42013-06-25 09:44:10 -0700403 if (setTetherCountingRules(add, intIface, extIface) && add) {
404 rc = -1;
405 goto err_return;
406 }
407
San Mehat9ff78fb2010-01-19 12:59:15 -0800408 return 0;
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800409
Lorenzo Colitti8917e452016-08-01 16:47:50 +0900410err_rpfilter:
Devi Sandeep Endluri V V5619ec12017-02-13 13:40:01 +0530411 cmd3[4] = "-D";
Lorenzo Colitti8917e452016-08-01 16:47:50 +0900412 runCmd(ARRAY_SIZE(cmd3), cmd3);
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800413err_return:
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530414 cmd2[4] = "-D";
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800415 runCmd(ARRAY_SIZE(cmd2), cmd2);
416err_invalid_drop:
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +0530417 cmd1[4] = "-D";
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800418 runCmd(ARRAY_SIZE(cmd1), cmd1);
419 return rc;
San Mehat9ff78fb2010-01-19 12:59:15 -0800420}
421
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700422int NatController::disableNat(const char* intIface, const char* extIface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700423 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700424 errno = ENODEV;
425 return -1;
426 }
427
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700428 setForwardRules(false, intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700429 if (--natCount <= 0) {
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600430 // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0
431 setDefaults();
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700432 }
433 return 0;
San Mehat9ff78fb2010-01-19 12:59:15 -0800434}