San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 1 | /* |
| 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 Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 17 | #define LOG_NDEBUG 0 |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 18 | |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <errno.h> |
| 21 | #include <sys/socket.h> |
| 22 | #include <sys/stat.h> |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 23 | #include <sys/wait.h> |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 24 | #include <fcntl.h> |
| 25 | #include <netinet/in.h> |
| 26 | #include <arpa/inet.h> |
Olivier Bailly | ff2c0d8 | 2010-11-17 11:45:07 -0800 | [diff] [blame] | 27 | #include <string.h> |
John Michelau | ac20860 | 2011-05-27 22:07:20 -0500 | [diff] [blame] | 28 | #include <cutils/properties.h> |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 29 | |
| 30 | #define LOG_TAG "NatController" |
| 31 | #include <cutils/log.h> |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 32 | #include <logwrap/logwrap.h> |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 33 | |
| 34 | #include "NatController.h" |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 35 | #include "NetdConstants.h" |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame^] | 36 | #include "RouteController.h" |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 37 | |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 38 | const char* NatController::LOCAL_FORWARD = "natctrl_FORWARD"; |
| 39 | const char* NatController::LOCAL_NAT_POSTROUTING = "natctrl_nat_POSTROUTING"; |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 40 | const char* NatController::LOCAL_TETHER_COUNTERS_CHAIN = "natctrl_tether_counters"; |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 41 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame^] | 42 | NatController::NatController() { |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | NatController::~NatController() { |
| 46 | } |
| 47 | |
JP Abgrall | 4ae80de | 2013-03-14 20:06:20 -0700 | [diff] [blame] | 48 | struct 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 Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 54 | int NatController::runCmd(int argc, const char **argv) { |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 55 | int res; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 56 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 57 | res = android_fork_execvp(argc, (char **)argv, NULL, false, false); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 58 | |
| 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 Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 74 | return res; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 75 | } |
| 76 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 77 | int NatController::setupIptablesHooks() { |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 78 | int res; |
| 79 | res = setDefaults(); |
| 80 | if (res < 0) { |
| 81 | return res; |
| 82 | } |
| 83 | |
| 84 | struct CommandsAndArgs defaultCommands[] = { |
| 85 | /* |
| 86 | * Chain for tethering counters. |
| 87 | * This chain is reached via --goto, and then RETURNS. |
| 88 | */ |
| 89 | {{IPTABLES_PATH, "-F", LOCAL_TETHER_COUNTERS_CHAIN,}, 0}, |
| 90 | {{IPTABLES_PATH, "-X", LOCAL_TETHER_COUNTERS_CHAIN,}, 0}, |
| 91 | {{IPTABLES_PATH, "-N", LOCAL_TETHER_COUNTERS_CHAIN,}, 1}, |
| 92 | }; |
| 93 | for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) { |
| 94 | if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) && |
| 95 | defaultCommands[cmdNum].checkRes) { |
| 96 | return -1; |
| 97 | } |
| 98 | } |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 99 | ifacePairList.clear(); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 100 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | int NatController::setDefaults() { |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 105 | /* |
| 106 | * The following only works because: |
| 107 | * - the defaultsCommands[].cmd array is padded with NULL, and |
| 108 | * - the 1st argc of runCmd() will just be the max for the CommandsAndArgs[].cmd, and |
| 109 | * - internally it will be memcopied to an array and terminated with a NULL. |
| 110 | */ |
JP Abgrall | 4ae80de | 2013-03-14 20:06:20 -0700 | [diff] [blame] | 111 | struct CommandsAndArgs defaultCommands[] = { |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 112 | {{IPTABLES_PATH, "-F", LOCAL_FORWARD,}, 1}, |
| 113 | {{IPTABLES_PATH, "-A", LOCAL_FORWARD, "-j", "DROP"}, 1}, |
| 114 | {{IPTABLES_PATH, "-t", "nat", "-F", LOCAL_NAT_POSTROUTING}, 1}, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 115 | }; |
JP Abgrall | 4ae80de | 2013-03-14 20:06:20 -0700 | [diff] [blame] | 116 | for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) { |
| 117 | if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) && |
| 118 | defaultCommands[cmdNum].checkRes) { |
| 119 | return -1; |
| 120 | } |
| 121 | } |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 122 | |
| 123 | natCount = 0; |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 124 | |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame^] | 128 | int NatController::enableNat(const char* intIface, const char* extIface) { |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 129 | ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface); |
| 130 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 131 | if (!isIfaceName(intIface) || !isIfaceName(extIface)) { |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 132 | errno = ENODEV; |
| 133 | return -1; |
| 134 | } |
| 135 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 136 | /* Bug: b/9565268. "enableNat wlan0 wlan0". For now we fail until java-land is fixed */ |
| 137 | if (!strcmp(intIface, extIface)) { |
| 138 | ALOGE("Duplicate interface specified: %s %s", intIface, extIface); |
| 139 | errno = EINVAL; |
| 140 | return -1; |
| 141 | } |
| 142 | |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 143 | // add this if we are the first added nat |
| 144 | if (natCount == 0) { |
| 145 | const char *cmd[] = { |
| 146 | IPTABLES_PATH, |
| 147 | "-t", |
| 148 | "nat", |
| 149 | "-A", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 150 | LOCAL_NAT_POSTROUTING, |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 151 | "-o", |
| 152 | extIface, |
| 153 | "-j", |
| 154 | "MASQUERADE" |
| 155 | }; |
| 156 | if (runCmd(ARRAY_SIZE(cmd), cmd)) { |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame^] | 157 | ALOGE("Error setting postroute rule: iface=%s", extIface); |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 158 | // unwind what's been done, but don't care about success - what more could we do? |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 159 | setDefaults(); |
| 160 | return -1; |
| 161 | } |
| 162 | } |
| 163 | |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 164 | if (setForwardRules(true, intIface, extIface) != 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 165 | ALOGE("Error setting forward rules"); |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 166 | if (natCount == 0) { |
| 167 | setDefaults(); |
| 168 | } |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 169 | errno = ENODEV; |
| 170 | return -1; |
| 171 | } |
| 172 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 173 | /* Always make sure the drop rule is at the end */ |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 174 | const char *cmd1[] = { |
| 175 | IPTABLES_PATH, |
| 176 | "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 177 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 178 | "-j", |
| 179 | "DROP" |
| 180 | }; |
| 181 | runCmd(ARRAY_SIZE(cmd1), cmd1); |
| 182 | const char *cmd2[] = { |
| 183 | IPTABLES_PATH, |
| 184 | "-A", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 185 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 186 | "-j", |
| 187 | "DROP" |
| 188 | }; |
| 189 | runCmd(ARRAY_SIZE(cmd2), cmd2); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 190 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame^] | 191 | if (int ret = RouteController::enableTethering(intIface, extIface)) { |
| 192 | ALOGE("failed to add tethering rule for iif=%s oif=%s", intIface, extIface); |
| 193 | errno = -ret; |
| 194 | return -1; |
| 195 | } |
| 196 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 197 | natCount++; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 201 | bool NatController::checkTetherCountingRuleExist(const char *pair_name) { |
| 202 | std::list<std::string>::iterator it; |
| 203 | |
| 204 | for (it = ifacePairList.begin(); it != ifacePairList.end(); it++) { |
| 205 | if (*it == pair_name) { |
| 206 | /* We already have this counter */ |
| 207 | return true; |
| 208 | } |
| 209 | } |
| 210 | return false; |
| 211 | } |
| 212 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 213 | int NatController::setTetherCountingRules(bool add, const char *intIface, const char *extIface) { |
| 214 | |
| 215 | /* We only ever add tethering quota rules so that they stick. */ |
| 216 | if (!add) { |
| 217 | return 0; |
| 218 | } |
Sreeram Ramachandran | 56afacf | 2014-05-28 15:07:00 -0700 | [diff] [blame] | 219 | char *pair_name; |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 220 | asprintf(&pair_name, "%s_%s", intIface, extIface); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 221 | |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 222 | if (checkTetherCountingRuleExist(pair_name)) { |
| 223 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 224 | return 0; |
| 225 | } |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 226 | const char *cmd2b[] = { |
| 227 | IPTABLES_PATH, |
| 228 | "-A", |
| 229 | LOCAL_TETHER_COUNTERS_CHAIN, |
| 230 | "-i", |
| 231 | intIface, |
| 232 | "-o", |
| 233 | extIface, |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 234 | "-j", |
| 235 | "RETURN" |
| 236 | }; |
| 237 | |
| 238 | if (runCmd(ARRAY_SIZE(cmd2b), cmd2b) && add) { |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 239 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 240 | return -1; |
| 241 | } |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 242 | ifacePairList.push_front(pair_name); |
| 243 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 244 | |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 245 | asprintf(&pair_name, "%s_%s", extIface, intIface); |
| 246 | if (checkTetherCountingRuleExist(pair_name)) { |
| 247 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 248 | return 0; |
| 249 | } |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 250 | |
| 251 | const char *cmd3b[] = { |
| 252 | IPTABLES_PATH, |
| 253 | "-A", |
| 254 | LOCAL_TETHER_COUNTERS_CHAIN, |
| 255 | "-i", |
| 256 | extIface, |
| 257 | "-o", |
| 258 | intIface, |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 259 | "-j", |
| 260 | "RETURN" |
| 261 | }; |
| 262 | |
| 263 | if (runCmd(ARRAY_SIZE(cmd3b), cmd3b) && add) { |
| 264 | // unwind what's been done, but don't care about success - what more could we do? |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 265 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 266 | return -1; |
| 267 | } |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 268 | ifacePairList.push_front(pair_name); |
| 269 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | int NatController::setForwardRules(bool add, const char *intIface, const char *extIface) { |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 274 | const char *cmd1[] = { |
| 275 | IPTABLES_PATH, |
| 276 | add ? "-A" : "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 277 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 278 | "-i", |
| 279 | extIface, |
| 280 | "-o", |
| 281 | intIface, |
| 282 | "-m", |
| 283 | "state", |
| 284 | "--state", |
| 285 | "ESTABLISHED,RELATED", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 286 | "-g", |
| 287 | LOCAL_TETHER_COUNTERS_CHAIN |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 288 | }; |
| 289 | int rc = 0; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 290 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 291 | if (runCmd(ARRAY_SIZE(cmd1), cmd1) && add) { |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 292 | return -1; |
| 293 | } |
| 294 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 295 | const char *cmd2[] = { |
| 296 | IPTABLES_PATH, |
| 297 | add ? "-A" : "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 298 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 299 | "-i", |
| 300 | intIface, |
| 301 | "-o", |
| 302 | extIface, |
| 303 | "-m", |
| 304 | "state", |
| 305 | "--state", |
| 306 | "INVALID", |
| 307 | "-j", |
| 308 | "DROP" |
| 309 | }; |
| 310 | |
| 311 | const char *cmd3[] = { |
| 312 | IPTABLES_PATH, |
| 313 | add ? "-A" : "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 314 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 315 | "-i", |
| 316 | intIface, |
| 317 | "-o", |
| 318 | extIface, |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 319 | "-g", |
| 320 | LOCAL_TETHER_COUNTERS_CHAIN |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 321 | }; |
| 322 | |
| 323 | if (runCmd(ARRAY_SIZE(cmd2), cmd2) && add) { |
Robert Greenwalt | f7bf29c | 2011-11-01 22:07:28 -0700 | [diff] [blame] | 324 | // bail on error, but only if adding |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 325 | rc = -1; |
| 326 | goto err_invalid_drop; |
Robert Greenwalt | ddb9f6e | 2011-08-02 13:00:11 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 329 | if (runCmd(ARRAY_SIZE(cmd3), cmd3) && add) { |
Robert Greenwalt | 210b977 | 2010-03-25 14:54:45 -0700 | [diff] [blame] | 330 | // unwind what's been done, but don't care about success - what more could we do? |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 331 | rc = -1; |
| 332 | goto err_return; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 333 | } |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 334 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 335 | if (setTetherCountingRules(add, intIface, extIface) && add) { |
| 336 | rc = -1; |
| 337 | goto err_return; |
| 338 | } |
| 339 | |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 340 | return 0; |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 341 | |
| 342 | err_return: |
| 343 | cmd2[1] = "-D"; |
| 344 | runCmd(ARRAY_SIZE(cmd2), cmd2); |
| 345 | err_invalid_drop: |
| 346 | cmd1[1] = "-D"; |
| 347 | runCmd(ARRAY_SIZE(cmd1), cmd1); |
| 348 | return rc; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame^] | 351 | int NatController::disableNat(const char* intIface, const char* extIface) { |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 352 | if (!isIfaceName(intIface) || !isIfaceName(extIface)) { |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 353 | errno = ENODEV; |
| 354 | return -1; |
| 355 | } |
| 356 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame^] | 357 | if (int ret = RouteController::disableTethering(intIface, extIface)) { |
| 358 | ALOGE("failed to remove tethering rule for iif=%s oif=%s", intIface, extIface); |
| 359 | errno = -ret; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 360 | return -1; |
| 361 | } |
| 362 | |
| 363 | setForwardRules(false, intIface, extIface); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 364 | if (--natCount <= 0) { |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 365 | // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0 |
| 366 | setDefaults(); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 367 | } |
| 368 | return 0; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 369 | } |