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"; |
Lorenzo Colitti | e8164dd | 2014-10-02 20:46:23 +0900 | [diff] [blame] | 39 | const char* NatController::LOCAL_MANGLE_FORWARD = "natctrl_mangle_FORWARD"; |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 40 | const char* NatController::LOCAL_NAT_POSTROUTING = "natctrl_nat_POSTROUTING"; |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 41 | const char* NatController::LOCAL_TETHER_COUNTERS_CHAIN = "natctrl_tether_counters"; |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 42 | |
Lorenzo Colitti | 8e1cee9 | 2016-07-09 14:24:08 +0900 | [diff] [blame^] | 43 | auto NatController::execFunction = android_fork_execvp; |
| 44 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 45 | NatController::NatController() { |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | NatController::~NatController() { |
| 49 | } |
| 50 | |
JP Abgrall | 4ae80de | 2013-03-14 20:06:20 -0700 | [diff] [blame] | 51 | struct CommandsAndArgs { |
| 52 | /* The array size doesn't really matter as the compiler will barf if too many initializers are specified. */ |
| 53 | const char *cmd[32]; |
| 54 | bool checkRes; |
| 55 | }; |
| 56 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 57 | int NatController::runCmd(int argc, const char **argv) { |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 58 | int res; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 59 | |
Lorenzo Colitti | 8e1cee9 | 2016-07-09 14:24:08 +0900 | [diff] [blame^] | 60 | res = execFunction(argc, (char **)argv, NULL, false, false); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 61 | |
| 62 | #if !LOG_NDEBUG |
| 63 | std::string full_cmd = argv[0]; |
| 64 | argc--; argv++; |
| 65 | /* |
| 66 | * HACK: Sometimes runCmd() is called with a ridcously large value (32) |
| 67 | * and it works because the argv[] contains a NULL after the last |
| 68 | * true argv. So here we use the NULL argv[] to terminate when the argc |
| 69 | * is horribly wrong, and argc for the normal cases. |
| 70 | */ |
| 71 | for (; argc && argv[0]; argc--, argv++) { |
| 72 | full_cmd += " "; |
| 73 | full_cmd += argv[0]; |
| 74 | } |
| 75 | ALOGV("runCmd(%s) res=%d", full_cmd.c_str(), res); |
| 76 | #endif |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 77 | return res; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 78 | } |
| 79 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 80 | int NatController::setupIptablesHooks() { |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 81 | int res; |
| 82 | res = setDefaults(); |
| 83 | if (res < 0) { |
| 84 | return res; |
| 85 | } |
| 86 | |
| 87 | struct CommandsAndArgs defaultCommands[] = { |
| 88 | /* |
Gordon Gao | 6b6f22f | 2014-09-18 11:50:09 -0700 | [diff] [blame] | 89 | * First chain is for tethering counters. |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 90 | * This chain is reached via --goto, and then RETURNS. |
Gordon Gao | 6b6f22f | 2014-09-18 11:50:09 -0700 | [diff] [blame] | 91 | * |
| 92 | * Second chain is used to limit downstream mss to the upstream pmtu |
| 93 | * so we don't end up fragmenting every large packet tethered devices |
| 94 | * send. Note this feature requires kernel support with flag |
| 95 | * CONFIG_NETFILTER_XT_TARGET_TCPMSS=y, which not all builds will have, |
| 96 | * so the final rule is allowed to fail. |
| 97 | * Bug 17629786 asks to make the failure more obvious, or even fatal |
| 98 | * so that all builds eventually gain the performance improvement. |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 99 | */ |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 100 | {{IPTABLES_PATH, "-w", "-F", LOCAL_TETHER_COUNTERS_CHAIN,}, 0}, |
| 101 | {{IPTABLES_PATH, "-w", "-X", LOCAL_TETHER_COUNTERS_CHAIN,}, 0}, |
| 102 | {{IPTABLES_PATH, "-w", "-N", LOCAL_TETHER_COUNTERS_CHAIN,}, 1}, |
| 103 | {{IPTABLES_PATH, "-w", "-t", "mangle", "-A", LOCAL_MANGLE_FORWARD, "-p", "tcp", "--tcp-flags", |
Gordon Gao | 6b6f22f | 2014-09-18 11:50:09 -0700 | [diff] [blame] | 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[] = { |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 125 | {{IPTABLES_PATH, "-w", "-F", LOCAL_FORWARD,}, 1}, |
| 126 | {{IPTABLES_PATH, "-w", "-A", LOCAL_FORWARD, "-j", "DROP"}, 1}, |
| 127 | {{IPTABLES_PATH, "-w", "-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, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 160 | "-w", |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 161 | "-t", |
| 162 | "nat", |
| 163 | "-A", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 164 | LOCAL_NAT_POSTROUTING, |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 165 | "-o", |
| 166 | extIface, |
| 167 | "-j", |
| 168 | "MASQUERADE" |
| 169 | }; |
| 170 | if (runCmd(ARRAY_SIZE(cmd), cmd)) { |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 171 | ALOGE("Error setting postroute rule: iface=%s", extIface); |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 172 | // 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] | 173 | setDefaults(); |
| 174 | return -1; |
| 175 | } |
| 176 | } |
| 177 | |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 178 | if (setForwardRules(true, intIface, extIface) != 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 179 | ALOGE("Error setting forward rules"); |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 180 | if (natCount == 0) { |
| 181 | setDefaults(); |
| 182 | } |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 183 | errno = ENODEV; |
| 184 | return -1; |
| 185 | } |
| 186 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 187 | /* Always make sure the drop rule is at the end */ |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 188 | const char *cmd1[] = { |
| 189 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 190 | "-w", |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 191 | "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 192 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 193 | "-j", |
| 194 | "DROP" |
| 195 | }; |
| 196 | runCmd(ARRAY_SIZE(cmd1), cmd1); |
| 197 | const char *cmd2[] = { |
| 198 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 199 | "-w", |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 200 | "-A", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 201 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 202 | "-j", |
| 203 | "DROP" |
| 204 | }; |
| 205 | runCmd(ARRAY_SIZE(cmd2), cmd2); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 206 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 207 | natCount++; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 211 | bool NatController::checkTetherCountingRuleExist(const char *pair_name) { |
| 212 | std::list<std::string>::iterator it; |
| 213 | |
| 214 | for (it = ifacePairList.begin(); it != ifacePairList.end(); it++) { |
| 215 | if (*it == pair_name) { |
| 216 | /* We already have this counter */ |
| 217 | return true; |
| 218 | } |
| 219 | } |
| 220 | return false; |
| 221 | } |
| 222 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 223 | int NatController::setTetherCountingRules(bool add, const char *intIface, const char *extIface) { |
| 224 | |
| 225 | /* We only ever add tethering quota rules so that they stick. */ |
| 226 | if (!add) { |
| 227 | return 0; |
| 228 | } |
Sreeram Ramachandran | 56afacf | 2014-05-28 15:07:00 -0700 | [diff] [blame] | 229 | char *pair_name; |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 230 | asprintf(&pair_name, "%s_%s", intIface, extIface); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 231 | |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 232 | if (checkTetherCountingRuleExist(pair_name)) { |
| 233 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 234 | return 0; |
| 235 | } |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 236 | const char *cmd2b[] = { |
| 237 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 238 | "-w", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 239 | "-A", |
| 240 | LOCAL_TETHER_COUNTERS_CHAIN, |
| 241 | "-i", |
| 242 | intIface, |
| 243 | "-o", |
| 244 | extIface, |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 245 | "-j", |
| 246 | "RETURN" |
| 247 | }; |
| 248 | |
| 249 | if (runCmd(ARRAY_SIZE(cmd2b), cmd2b) && add) { |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 250 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 251 | return -1; |
| 252 | } |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 253 | ifacePairList.push_front(pair_name); |
| 254 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 255 | |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 256 | asprintf(&pair_name, "%s_%s", extIface, intIface); |
| 257 | if (checkTetherCountingRuleExist(pair_name)) { |
| 258 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 259 | return 0; |
| 260 | } |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 261 | |
| 262 | const char *cmd3b[] = { |
| 263 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 264 | "-w", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 265 | "-A", |
| 266 | LOCAL_TETHER_COUNTERS_CHAIN, |
| 267 | "-i", |
| 268 | extIface, |
| 269 | "-o", |
| 270 | intIface, |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 271 | "-j", |
| 272 | "RETURN" |
| 273 | }; |
| 274 | |
| 275 | if (runCmd(ARRAY_SIZE(cmd3b), cmd3b) && add) { |
| 276 | // 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] | 277 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 278 | return -1; |
| 279 | } |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 280 | ifacePairList.push_front(pair_name); |
| 281 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | int NatController::setForwardRules(bool add, const char *intIface, const char *extIface) { |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 286 | const char *cmd1[] = { |
| 287 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 288 | "-w", |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 289 | add ? "-A" : "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 290 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 291 | "-i", |
| 292 | extIface, |
| 293 | "-o", |
| 294 | intIface, |
| 295 | "-m", |
| 296 | "state", |
| 297 | "--state", |
| 298 | "ESTABLISHED,RELATED", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 299 | "-g", |
| 300 | LOCAL_TETHER_COUNTERS_CHAIN |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 301 | }; |
| 302 | int rc = 0; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 303 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 304 | if (runCmd(ARRAY_SIZE(cmd1), cmd1) && add) { |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 305 | return -1; |
| 306 | } |
| 307 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 308 | const char *cmd2[] = { |
| 309 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 310 | "-w", |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 311 | add ? "-A" : "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 312 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 313 | "-i", |
| 314 | intIface, |
| 315 | "-o", |
| 316 | extIface, |
| 317 | "-m", |
| 318 | "state", |
| 319 | "--state", |
| 320 | "INVALID", |
| 321 | "-j", |
| 322 | "DROP" |
| 323 | }; |
| 324 | |
| 325 | const char *cmd3[] = { |
| 326 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 327 | "-w", |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 328 | add ? "-A" : "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 329 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 330 | "-i", |
| 331 | intIface, |
| 332 | "-o", |
| 333 | extIface, |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 334 | "-g", |
| 335 | LOCAL_TETHER_COUNTERS_CHAIN |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | if (runCmd(ARRAY_SIZE(cmd2), cmd2) && add) { |
Robert Greenwalt | f7bf29c | 2011-11-01 22:07:28 -0700 | [diff] [blame] | 339 | // bail on error, but only if adding |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 340 | rc = -1; |
| 341 | goto err_invalid_drop; |
Robert Greenwalt | ddb9f6e | 2011-08-02 13:00:11 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 344 | if (runCmd(ARRAY_SIZE(cmd3), cmd3) && add) { |
Robert Greenwalt | 210b977 | 2010-03-25 14:54:45 -0700 | [diff] [blame] | 345 | // 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] | 346 | rc = -1; |
| 347 | goto err_return; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 348 | } |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 349 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 350 | if (setTetherCountingRules(add, intIface, extIface) && add) { |
| 351 | rc = -1; |
| 352 | goto err_return; |
| 353 | } |
| 354 | |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 355 | return 0; |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 356 | |
| 357 | err_return: |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 358 | cmd2[2] = "-D"; |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 359 | runCmd(ARRAY_SIZE(cmd2), cmd2); |
| 360 | err_invalid_drop: |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 361 | cmd1[2] = "-D"; |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 362 | runCmd(ARRAY_SIZE(cmd1), cmd1); |
| 363 | return rc; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 366 | int NatController::disableNat(const char* intIface, const char* extIface) { |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 367 | if (!isIfaceName(intIface) || !isIfaceName(extIface)) { |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 368 | errno = ENODEV; |
| 369 | return -1; |
| 370 | } |
| 371 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 372 | setForwardRules(false, intIface, extIface); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 373 | if (--natCount <= 0) { |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 374 | // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0 |
| 375 | setDefaults(); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 376 | } |
| 377 | return 0; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 378 | } |