blob: 8b4ee119e33edf60f4454c85a289f7ad6fcd5c33 [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 */
93 {{IPTABLES_PATH, "-w", "-F", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
94 {{IP6TABLES_PATH, "-w", "-F", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
95 {{IPTABLES_PATH, "-w", "-X", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
96 {{IP6TABLES_PATH, "-w", "-X", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
97 {{IPTABLES_PATH, "-w", "-N", LOCAL_TETHER_COUNTERS_CHAIN,}, 1},
98 {{IP6TABLES_PATH, "-w", "-N", LOCAL_TETHER_COUNTERS_CHAIN,}, 1},
99
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 */
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900109 {{IPTABLES_PATH, "-w", "-t", "mangle", "-A", LOCAL_MANGLE_FORWARD, "-p", "tcp",
110 "--tcp-flags", "SYN", "SYN", "-j", "TCPMSS", "--clamp-mss-to-pmtu"}, 0},
JP Abgrallbaeccc42013-06-25 09:44:10 -0700111 };
112 for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) {
113 if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) &&
114 defaultCommands[cmdNum].checkRes) {
115 return -1;
116 }
117 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700118 ifacePairList.clear();
JP Abgrallbaeccc42013-06-25 09:44:10 -0700119
JP Abgrall0031cea2012-04-17 16:38:23 -0700120 return 0;
121}
122
123int NatController::setDefaults() {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700124 /*
125 * The following only works because:
126 * - the defaultsCommands[].cmd array is padded with NULL, and
127 * - the 1st argc of runCmd() will just be the max for the CommandsAndArgs[].cmd, and
128 * - internally it will be memcopied to an array and terminated with a NULL.
129 */
JP Abgrall4ae80de2013-03-14 20:06:20 -0700130 struct CommandsAndArgs defaultCommands[] = {
Paul Jensen94b2ab92015-08-04 10:35:05 -0400131 {{IPTABLES_PATH, "-w", "-F", LOCAL_FORWARD,}, 1},
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900132 {{IP6TABLES_PATH, "-w", "-F", LOCAL_FORWARD,}, 1},
Paul Jensen94b2ab92015-08-04 10:35:05 -0400133 {{IPTABLES_PATH, "-w", "-A", LOCAL_FORWARD, "-j", "DROP"}, 1},
134 {{IPTABLES_PATH, "-w", "-t", "nat", "-F", LOCAL_NAT_POSTROUTING}, 1},
Lorenzo Colitti8917e452016-08-01 16:47:50 +0900135 {{IP6TABLES_PATH, "-w", "-t", "raw", "-F", LOCAL_RAW_PREROUTING}, 1},
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800136 };
JP Abgrall4ae80de2013-03-14 20:06:20 -0700137 for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) {
138 if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) &&
139 defaultCommands[cmdNum].checkRes) {
140 return -1;
141 }
142 }
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700143
144 natCount = 0;
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600145
San Mehat9ff78fb2010-01-19 12:59:15 -0800146 return 0;
147}
148
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700149int NatController::enableNat(const char* intIface, const char* extIface) {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700150 ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface);
151
JP Abgrall69261cb2014-06-19 18:35:24 -0700152 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800153 errno = ENODEV;
154 return -1;
155 }
156
JP Abgrallbaeccc42013-06-25 09:44:10 -0700157 /* Bug: b/9565268. "enableNat wlan0 wlan0". For now we fail until java-land is fixed */
158 if (!strcmp(intIface, extIface)) {
159 ALOGE("Duplicate interface specified: %s %s", intIface, extIface);
160 errno = EINVAL;
161 return -1;
162 }
163
JP Abgrall659692a2013-03-14 20:07:17 -0700164 // add this if we are the first added nat
165 if (natCount == 0) {
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900166 const char *v4Cmd[] = {
JP Abgrall659692a2013-03-14 20:07:17 -0700167 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400168 "-w",
JP Abgrall659692a2013-03-14 20:07:17 -0700169 "-t",
170 "nat",
171 "-A",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700172 LOCAL_NAT_POSTROUTING,
JP Abgrall659692a2013-03-14 20:07:17 -0700173 "-o",
174 extIface,
175 "-j",
176 "MASQUERADE"
177 };
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900178
179 /*
180 * IPv6 tethering doesn't need the state-based conntrack rules, so
181 * it unconditionally jumps to the tether counters chain all the time.
182 */
183 const char *v6Cmd[] = {IP6TABLES_PATH, "-w", "-A", LOCAL_FORWARD,
184 "-g", LOCAL_TETHER_COUNTERS_CHAIN};
185
186 if (runCmd(ARRAY_SIZE(v4Cmd), v4Cmd) || runCmd(ARRAY_SIZE(v6Cmd), v6Cmd)) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700187 ALOGE("Error setting postroute rule: iface=%s", extIface);
JP Abgrall659692a2013-03-14 20:07:17 -0700188 // unwind what's been done, but don't care about success - what more could we do?
JP Abgrall659692a2013-03-14 20:07:17 -0700189 setDefaults();
190 return -1;
191 }
192 }
193
JP Abgrall659692a2013-03-14 20:07:17 -0700194 if (setForwardRules(true, intIface, extIface) != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000195 ALOGE("Error setting forward rules");
JP Abgrall659692a2013-03-14 20:07:17 -0700196 if (natCount == 0) {
197 setDefaults();
198 }
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700199 errno = ENODEV;
200 return -1;
201 }
202
JP Abgrall0031cea2012-04-17 16:38:23 -0700203 /* Always make sure the drop rule is at the end */
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800204 const char *cmd1[] = {
205 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400206 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800207 "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700208 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800209 "-j",
210 "DROP"
211 };
212 runCmd(ARRAY_SIZE(cmd1), cmd1);
213 const char *cmd2[] = {
214 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400215 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800216 "-A",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700217 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800218 "-j",
219 "DROP"
220 };
221 runCmd(ARRAY_SIZE(cmd2), cmd2);
JP Abgrall0031cea2012-04-17 16:38:23 -0700222
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700223 natCount++;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700224 return 0;
225}
226
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700227bool NatController::checkTetherCountingRuleExist(const char *pair_name) {
228 std::list<std::string>::iterator it;
229
230 for (it = ifacePairList.begin(); it != ifacePairList.end(); it++) {
231 if (*it == pair_name) {
232 /* We already have this counter */
233 return true;
234 }
235 }
236 return false;
237}
238
JP Abgrallbaeccc42013-06-25 09:44:10 -0700239int NatController::setTetherCountingRules(bool add, const char *intIface, const char *extIface) {
240
241 /* We only ever add tethering quota rules so that they stick. */
242 if (!add) {
243 return 0;
244 }
Sreeram Ramachandran56afacf2014-05-28 15:07:00 -0700245 char *pair_name;
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700246 asprintf(&pair_name, "%s_%s", intIface, extIface);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700247
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700248 if (checkTetherCountingRuleExist(pair_name)) {
249 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700250 return 0;
251 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700252 const char *cmd2b[] = {
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900253 IPTABLES_PATH,
254 "-w", "-A", LOCAL_TETHER_COUNTERS_CHAIN, "-i", intIface, "-o", extIface, "-j", "RETURN"
JP Abgrallbaeccc42013-06-25 09:44:10 -0700255 };
256
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900257 const char *cmd2c[] = {
258 IP6TABLES_PATH,
259 "-w", "-A", LOCAL_TETHER_COUNTERS_CHAIN, "-i", intIface, "-o", extIface, "-j", "RETURN"
260 };
261
262 if (runCmd(ARRAY_SIZE(cmd2b), cmd2b) || runCmd(ARRAY_SIZE(cmd2c), cmd2c)) {
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700263 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700264 return -1;
265 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700266 ifacePairList.push_front(pair_name);
267 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700268
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700269 asprintf(&pair_name, "%s_%s", extIface, intIface);
270 if (checkTetherCountingRuleExist(pair_name)) {
271 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700272 return 0;
273 }
JP Abgrallbaeccc42013-06-25 09:44:10 -0700274
275 const char *cmd3b[] = {
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900276 IPTABLES_PATH,
277 "-w", "-A", LOCAL_TETHER_COUNTERS_CHAIN, "-i", extIface, "-o", intIface, "-j", "RETURN"
JP Abgrallbaeccc42013-06-25 09:44:10 -0700278 };
279
Lorenzo Colitti05cfd252016-07-10 23:15:46 +0900280 const char *cmd3c[] = {
281 IP6TABLES_PATH,
282 "-w", "-A", LOCAL_TETHER_COUNTERS_CHAIN, "-i", extIface, "-o", intIface, "-j", "RETURN"
283 };
284
285 if (runCmd(ARRAY_SIZE(cmd3b), cmd3b) || runCmd(ARRAY_SIZE(cmd3c), cmd3c)) {
JP Abgrallbaeccc42013-06-25 09:44:10 -0700286 // unwind what's been done, but don't care about success - what more could we do?
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700287 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700288 return -1;
289 }
JP Abgrallf3cc83f2013-09-11 20:01:59 -0700290 ifacePairList.push_front(pair_name);
291 free(pair_name);
JP Abgrallbaeccc42013-06-25 09:44:10 -0700292 return 0;
293}
294
295int NatController::setForwardRules(bool add, const char *intIface, const char *extIface) {
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800296 const char *cmd1[] = {
297 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400298 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800299 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700300 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800301 "-i",
302 extIface,
303 "-o",
304 intIface,
305 "-m",
306 "state",
307 "--state",
308 "ESTABLISHED,RELATED",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700309 "-g",
310 LOCAL_TETHER_COUNTERS_CHAIN
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800311 };
312 int rc = 0;
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700313
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800314 if (runCmd(ARRAY_SIZE(cmd1), cmd1) && add) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800315 return -1;
316 }
317
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800318 const char *cmd2[] = {
319 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400320 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800321 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700322 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800323 "-i",
324 intIface,
325 "-o",
326 extIface,
327 "-m",
328 "state",
329 "--state",
330 "INVALID",
331 "-j",
332 "DROP"
333 };
334
335 const char *cmd3[] = {
336 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -0400337 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800338 add ? "-A" : "-D",
JP Abgrallbaeccc42013-06-25 09:44:10 -0700339 LOCAL_FORWARD,
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800340 "-i",
341 intIface,
342 "-o",
343 extIface,
JP Abgrallbaeccc42013-06-25 09:44:10 -0700344 "-g",
345 LOCAL_TETHER_COUNTERS_CHAIN
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800346 };
347
Lorenzo Colitti8917e452016-08-01 16:47:50 +0900348 const char *cmd4[] = {
349 IP6TABLES_PATH,
350 "-w",
351 "-t",
352 "raw",
353 add ? "-A" : "-D",
354 LOCAL_RAW_PREROUTING,
355 "-i",
356 intIface,
357 "-m",
358 "rpfilter",
359 "--invert",
360 "!",
361 "-s",
362 "fe80::/64",
363 "-j",
364 "DROP"
365 };
366
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800367 if (runCmd(ARRAY_SIZE(cmd2), cmd2) && add) {
Robert Greenwaltf7bf29c2011-11-01 22:07:28 -0700368 // bail on error, but only if adding
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800369 rc = -1;
370 goto err_invalid_drop;
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700371 }
372
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800373 if (runCmd(ARRAY_SIZE(cmd3), cmd3) && add) {
Robert Greenwalt210b9772010-03-25 14:54:45 -0700374 // unwind what's been done, but don't care about success - what more could we do?
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800375 rc = -1;
376 goto err_return;
San Mehat9ff78fb2010-01-19 12:59:15 -0800377 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700378
Lorenzo Colitti8917e452016-08-01 16:47:50 +0900379 // STOPSHIP: Make this an error.
380 if (runCmd(ARRAY_SIZE(cmd4), cmd4) && add && false /* STOPSHIP */) {
381 rc = -1;
382 goto err_rpfilter;
383 }
384
JP Abgrallbaeccc42013-06-25 09:44:10 -0700385 if (setTetherCountingRules(add, intIface, extIface) && add) {
386 rc = -1;
387 goto err_return;
388 }
389
San Mehat9ff78fb2010-01-19 12:59:15 -0800390 return 0;
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800391
Lorenzo Colitti8917e452016-08-01 16:47:50 +0900392err_rpfilter:
393 cmd3[2] = "-D";
394 runCmd(ARRAY_SIZE(cmd3), cmd3);
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800395err_return:
Paul Jensen94b2ab92015-08-04 10:35:05 -0400396 cmd2[2] = "-D";
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800397 runCmd(ARRAY_SIZE(cmd2), cmd2);
398err_invalid_drop:
Paul Jensen94b2ab92015-08-04 10:35:05 -0400399 cmd1[2] = "-D";
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800400 runCmd(ARRAY_SIZE(cmd1), cmd1);
401 return rc;
San Mehat9ff78fb2010-01-19 12:59:15 -0800402}
403
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700404int NatController::disableNat(const char* intIface, const char* extIface) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700405 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700406 errno = ENODEV;
407 return -1;
408 }
409
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700410 setForwardRules(false, intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700411 if (--natCount <= 0) {
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600412 // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0
413 setDefaults();
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700414 }
415 return 0;
San Mehat9ff78fb2010-01-19 12:59:15 -0800416}