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 | /* |
Gordon Gao | 6b6f22f | 2014-09-18 11:50:09 -0700 | [diff] [blame^] | 86 | * First chain is for tethering counters. |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 87 | * This chain is reached via --goto, and then RETURNS. |
Gordon Gao | 6b6f22f | 2014-09-18 11:50:09 -0700 | [diff] [blame^] | 88 | * |
| 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 Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 96 | */ |
| 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 Gao | 6b6f22f | 2014-09-18 11:50:09 -0700 | [diff] [blame^] | 100 | {{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 Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 105 | }; |
| 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 Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 112 | ifacePairList.clear(); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 113 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int NatController::setDefaults() { |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 118 | /* |
| 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 Abgrall | 4ae80de | 2013-03-14 20:06:20 -0700 | [diff] [blame] | 124 | struct CommandsAndArgs defaultCommands[] = { |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 125 | {{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 Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 128 | }; |
JP Abgrall | 4ae80de | 2013-03-14 20:06:20 -0700 | [diff] [blame] | 129 | 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 Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 135 | |
| 136 | natCount = 0; |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 137 | |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 141 | int NatController::enableNat(const char* intIface, const char* extIface) { |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 142 | ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface); |
| 143 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 144 | if (!isIfaceName(intIface) || !isIfaceName(extIface)) { |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 145 | errno = ENODEV; |
| 146 | return -1; |
| 147 | } |
| 148 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 149 | /* 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 Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 156 | // 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 Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 163 | LOCAL_NAT_POSTROUTING, |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 164 | "-o", |
| 165 | extIface, |
| 166 | "-j", |
| 167 | "MASQUERADE" |
| 168 | }; |
| 169 | if (runCmd(ARRAY_SIZE(cmd), cmd)) { |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 170 | ALOGE("Error setting postroute rule: iface=%s", extIface); |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 171 | // 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] | 172 | setDefaults(); |
| 173 | return -1; |
| 174 | } |
| 175 | } |
| 176 | |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 177 | if (setForwardRules(true, intIface, extIface) != 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 178 | ALOGE("Error setting forward rules"); |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 179 | if (natCount == 0) { |
| 180 | setDefaults(); |
| 181 | } |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 182 | errno = ENODEV; |
| 183 | return -1; |
| 184 | } |
| 185 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 186 | /* Always make sure the drop rule is at the end */ |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 187 | const char *cmd1[] = { |
| 188 | IPTABLES_PATH, |
| 189 | "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 190 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 191 | "-j", |
| 192 | "DROP" |
| 193 | }; |
| 194 | runCmd(ARRAY_SIZE(cmd1), cmd1); |
| 195 | const char *cmd2[] = { |
| 196 | IPTABLES_PATH, |
| 197 | "-A", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 198 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 199 | "-j", |
| 200 | "DROP" |
| 201 | }; |
| 202 | runCmd(ARRAY_SIZE(cmd2), cmd2); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 203 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 204 | if (int ret = RouteController::enableTethering(intIface, extIface)) { |
| 205 | ALOGE("failed to add tethering rule for iif=%s oif=%s", intIface, extIface); |
Sreeram Ramachandran | 8b3b91c | 2014-07-22 12:40:36 -0700 | [diff] [blame] | 206 | if (natCount == 0) { |
| 207 | setDefaults(); |
| 208 | } |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 209 | errno = -ret; |
| 210 | return -1; |
| 211 | } |
| 212 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 213 | natCount++; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 217 | bool 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 Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 229 | int 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 Ramachandran | 56afacf | 2014-05-28 15:07:00 -0700 | [diff] [blame] | 235 | char *pair_name; |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 236 | asprintf(&pair_name, "%s_%s", intIface, extIface); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 237 | |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 238 | if (checkTetherCountingRuleExist(pair_name)) { |
| 239 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 240 | return 0; |
| 241 | } |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 242 | const char *cmd2b[] = { |
| 243 | IPTABLES_PATH, |
| 244 | "-A", |
| 245 | LOCAL_TETHER_COUNTERS_CHAIN, |
| 246 | "-i", |
| 247 | intIface, |
| 248 | "-o", |
| 249 | extIface, |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 250 | "-j", |
| 251 | "RETURN" |
| 252 | }; |
| 253 | |
| 254 | if (runCmd(ARRAY_SIZE(cmd2b), cmd2b) && add) { |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 255 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 256 | return -1; |
| 257 | } |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 258 | ifacePairList.push_front(pair_name); |
| 259 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 260 | |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 261 | asprintf(&pair_name, "%s_%s", extIface, intIface); |
| 262 | if (checkTetherCountingRuleExist(pair_name)) { |
| 263 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 264 | return 0; |
| 265 | } |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 266 | |
| 267 | const char *cmd3b[] = { |
| 268 | IPTABLES_PATH, |
| 269 | "-A", |
| 270 | LOCAL_TETHER_COUNTERS_CHAIN, |
| 271 | "-i", |
| 272 | extIface, |
| 273 | "-o", |
| 274 | intIface, |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 275 | "-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 Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 281 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 282 | return -1; |
| 283 | } |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 284 | ifacePairList.push_front(pair_name); |
| 285 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | int NatController::setForwardRules(bool add, const char *intIface, const char *extIface) { |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 290 | const char *cmd1[] = { |
| 291 | IPTABLES_PATH, |
| 292 | add ? "-A" : "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 293 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 294 | "-i", |
| 295 | extIface, |
| 296 | "-o", |
| 297 | intIface, |
| 298 | "-m", |
| 299 | "state", |
| 300 | "--state", |
| 301 | "ESTABLISHED,RELATED", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 302 | "-g", |
| 303 | LOCAL_TETHER_COUNTERS_CHAIN |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 304 | }; |
| 305 | int rc = 0; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 306 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 307 | if (runCmd(ARRAY_SIZE(cmd1), cmd1) && add) { |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 308 | return -1; |
| 309 | } |
| 310 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 311 | const char *cmd2[] = { |
| 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, |
| 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 Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 330 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 331 | "-i", |
| 332 | intIface, |
| 333 | "-o", |
| 334 | extIface, |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 335 | "-g", |
| 336 | LOCAL_TETHER_COUNTERS_CHAIN |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | if (runCmd(ARRAY_SIZE(cmd2), cmd2) && add) { |
Robert Greenwalt | f7bf29c | 2011-11-01 22:07:28 -0700 | [diff] [blame] | 340 | // bail on error, but only if adding |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 341 | rc = -1; |
| 342 | goto err_invalid_drop; |
Robert Greenwalt | ddb9f6e | 2011-08-02 13:00:11 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 345 | if (runCmd(ARRAY_SIZE(cmd3), cmd3) && add) { |
Robert Greenwalt | 210b977 | 2010-03-25 14:54:45 -0700 | [diff] [blame] | 346 | // 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] | 347 | rc = -1; |
| 348 | goto err_return; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 349 | } |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 350 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 351 | if (setTetherCountingRules(add, intIface, extIface) && add) { |
| 352 | rc = -1; |
| 353 | goto err_return; |
| 354 | } |
| 355 | |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 356 | return 0; |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 357 | |
| 358 | err_return: |
| 359 | cmd2[1] = "-D"; |
| 360 | runCmd(ARRAY_SIZE(cmd2), cmd2); |
| 361 | err_invalid_drop: |
| 362 | cmd1[1] = "-D"; |
| 363 | runCmd(ARRAY_SIZE(cmd1), cmd1); |
| 364 | return rc; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 367 | int NatController::disableNat(const char* intIface, const char* extIface) { |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 368 | if (!isIfaceName(intIface) || !isIfaceName(extIface)) { |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 369 | errno = ENODEV; |
| 370 | return -1; |
| 371 | } |
| 372 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 373 | 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 Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 376 | return -1; |
| 377 | } |
| 378 | |
| 379 | setForwardRules(false, intIface, extIface); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 380 | if (--natCount <= 0) { |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 381 | // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0 |
| 382 | setDefaults(); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 383 | } |
| 384 | return 0; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 385 | } |