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"; |
Lorenzo Colitti | 8917e45 | 2016-08-01 16:47:50 +0900 | [diff] [blame] | 41 | const char* NatController::LOCAL_RAW_PREROUTING = "natctrl_raw_PREROUTING"; |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 42 | const char* NatController::LOCAL_TETHER_COUNTERS_CHAIN = "natctrl_tether_counters"; |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 43 | |
Lorenzo Colitti | 8e1cee9 | 2016-07-09 14:24:08 +0900 | [diff] [blame] | 44 | auto NatController::execFunction = android_fork_execvp; |
| 45 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 46 | NatController::NatController() { |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | NatController::~NatController() { |
| 50 | } |
| 51 | |
JP Abgrall | 4ae80de | 2013-03-14 20:06:20 -0700 | [diff] [blame] | 52 | struct 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 Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 58 | int NatController::runCmd(int argc, const char **argv) { |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 59 | int res; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 60 | |
Lorenzo Colitti | 8e1cee9 | 2016-07-09 14:24:08 +0900 | [diff] [blame] | 61 | res = execFunction(argc, (char **)argv, NULL, false, false); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 62 | |
| 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 Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 78 | return res; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 79 | } |
| 80 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 81 | int NatController::setupIptablesHooks() { |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 82 | int res; |
| 83 | res = setDefaults(); |
| 84 | if (res < 0) { |
| 85 | return res; |
| 86 | } |
| 87 | |
| 88 | struct CommandsAndArgs defaultCommands[] = { |
| 89 | /* |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 90 | * This is for tethering counters. |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 91 | * This chain is reached via --goto, and then RETURNS. |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 92 | */ |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 93 | {{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 Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 99 | |
| 100 | /* |
Gordon Gao | 6b6f22f | 2014-09-18 11:50:09 -0700 | [diff] [blame] | 101 | * 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 Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 108 | */ |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 109 | {{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 Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 112 | }; |
| 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 Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 119 | ifacePairList.clear(); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 120 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | int NatController::setDefaults() { |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 125 | /* |
| 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 Abgrall | 4ae80de | 2013-03-14 20:06:20 -0700 | [diff] [blame] | 131 | struct CommandsAndArgs defaultCommands[] = { |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 132 | {{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 Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 137 | }; |
JP Abgrall | 4ae80de | 2013-03-14 20:06:20 -0700 | [diff] [blame] | 138 | 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 Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 144 | |
| 145 | natCount = 0; |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 146 | |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 150 | int NatController::enableNat(const char* intIface, const char* extIface) { |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 151 | ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface); |
| 152 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 153 | if (!isIfaceName(intIface) || !isIfaceName(extIface)) { |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 154 | errno = ENODEV; |
| 155 | return -1; |
| 156 | } |
| 157 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 158 | /* 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 Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 165 | // add this if we are the first added nat |
| 166 | if (natCount == 0) { |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 167 | const char *v4Cmd[] = { |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 168 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 169 | "-w", |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 170 | "-W", |
| 171 | IPTABLES_RETRY_INTERVAL, |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 172 | "-t", |
| 173 | "nat", |
| 174 | "-A", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 175 | LOCAL_NAT_POSTROUTING, |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 176 | "-o", |
| 177 | extIface, |
| 178 | "-j", |
| 179 | "MASQUERADE" |
| 180 | }; |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 181 | |
| 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 V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 186 | const char *v6Cmd[] = {IP6TABLES_PATH, "-w", "-W", IPTABLES_RETRY_INTERVAL, |
| 187 | "-A", LOCAL_FORWARD, "-g", LOCAL_TETHER_COUNTERS_CHAIN}; |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 188 | |
| 189 | if (runCmd(ARRAY_SIZE(v4Cmd), v4Cmd) || runCmd(ARRAY_SIZE(v6Cmd), v6Cmd)) { |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 190 | ALOGE("Error setting postroute rule: iface=%s", extIface); |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 191 | // 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] | 192 | setDefaults(); |
| 193 | return -1; |
| 194 | } |
| 195 | } |
| 196 | |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 197 | if (setForwardRules(true, intIface, extIface) != 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 198 | ALOGE("Error setting forward rules"); |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 199 | if (natCount == 0) { |
| 200 | setDefaults(); |
| 201 | } |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 202 | errno = ENODEV; |
| 203 | return -1; |
| 204 | } |
| 205 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 206 | /* Always make sure the drop rule is at the end */ |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 207 | const char *cmd1[] = { |
| 208 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 209 | "-w", |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 210 | "-W", |
| 211 | IPTABLES_RETRY_INTERVAL, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 212 | "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 213 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 214 | "-j", |
| 215 | "DROP" |
| 216 | }; |
| 217 | runCmd(ARRAY_SIZE(cmd1), cmd1); |
| 218 | const char *cmd2[] = { |
| 219 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 220 | "-w", |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 221 | "-W", |
| 222 | IPTABLES_RETRY_INTERVAL, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 223 | "-A", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 224 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 225 | "-j", |
| 226 | "DROP" |
| 227 | }; |
| 228 | runCmd(ARRAY_SIZE(cmd2), cmd2); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 229 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 230 | natCount++; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 234 | bool 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 Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 246 | int 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 Ramachandran | 56afacf | 2014-05-28 15:07:00 -0700 | [diff] [blame] | 252 | char *pair_name; |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 253 | asprintf(&pair_name, "%s_%s", intIface, extIface); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 254 | |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 255 | if (checkTetherCountingRuleExist(pair_name)) { |
| 256 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 257 | return 0; |
| 258 | } |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 259 | const char *cmd2b[] = { |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 260 | IPTABLES_PATH, |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 261 | "-w", "-W", IPTABLES_RETRY_INTERVAL, "-A", LOCAL_TETHER_COUNTERS_CHAIN, |
| 262 | "-i", intIface, "-o", extIface, "-j", "RETURN" |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 263 | }; |
| 264 | |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 265 | const char *cmd2c[] = { |
| 266 | IP6TABLES_PATH, |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 267 | "-w", "-W", IPTABLES_RETRY_INTERVAL, "-A", LOCAL_TETHER_COUNTERS_CHAIN, |
| 268 | "-i", intIface, "-o", extIface, "-j", "RETURN" |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | if (runCmd(ARRAY_SIZE(cmd2b), cmd2b) || runCmd(ARRAY_SIZE(cmd2c), cmd2c)) { |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 272 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 273 | return -1; |
| 274 | } |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 275 | ifacePairList.push_front(pair_name); |
| 276 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 277 | |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 278 | asprintf(&pair_name, "%s_%s", extIface, intIface); |
| 279 | if (checkTetherCountingRuleExist(pair_name)) { |
| 280 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 281 | return 0; |
| 282 | } |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 283 | |
| 284 | const char *cmd3b[] = { |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 285 | IPTABLES_PATH, |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 286 | "-w", "-W", IPTABLES_RETRY_INTERVAL, "-A", LOCAL_TETHER_COUNTERS_CHAIN, |
| 287 | "-i", extIface, "-o", intIface, "-j", "RETURN" |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 288 | }; |
| 289 | |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 290 | const char *cmd3c[] = { |
| 291 | IP6TABLES_PATH, |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 292 | "-w", "-W", IPTABLES_RETRY_INTERVAL, "-A", LOCAL_TETHER_COUNTERS_CHAIN, |
| 293 | "-i", extIface, "-o", intIface, "-j", "RETURN" |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | if (runCmd(ARRAY_SIZE(cmd3b), cmd3b) || runCmd(ARRAY_SIZE(cmd3c), cmd3c)) { |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 297 | // 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] | 298 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 299 | return -1; |
| 300 | } |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 301 | ifacePairList.push_front(pair_name); |
| 302 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | int NatController::setForwardRules(bool add, const char *intIface, const char *extIface) { |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 307 | const char *cmd1[] = { |
| 308 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 309 | "-w", |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 310 | "-W", |
| 311 | IPTABLES_RETRY_INTERVAL, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 312 | add ? "-A" : "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 313 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 314 | "-i", |
| 315 | extIface, |
| 316 | "-o", |
| 317 | intIface, |
| 318 | "-m", |
| 319 | "state", |
| 320 | "--state", |
| 321 | "ESTABLISHED,RELATED", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 322 | "-g", |
| 323 | LOCAL_TETHER_COUNTERS_CHAIN |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 324 | }; |
| 325 | int rc = 0; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 326 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 327 | if (runCmd(ARRAY_SIZE(cmd1), cmd1) && add) { |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 328 | return -1; |
| 329 | } |
| 330 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 331 | const char *cmd2[] = { |
| 332 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 333 | "-w", |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 334 | "-W", |
| 335 | IPTABLES_RETRY_INTERVAL, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 336 | add ? "-A" : "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 337 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 338 | "-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 Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 352 | "-w", |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 353 | "-W", |
| 354 | IPTABLES_RETRY_INTERVAL, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 355 | add ? "-A" : "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 356 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 357 | "-i", |
| 358 | intIface, |
| 359 | "-o", |
| 360 | extIface, |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 361 | "-g", |
| 362 | LOCAL_TETHER_COUNTERS_CHAIN |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 363 | }; |
| 364 | |
Lorenzo Colitti | 8917e45 | 2016-08-01 16:47:50 +0900 | [diff] [blame] | 365 | const char *cmd4[] = { |
| 366 | IP6TABLES_PATH, |
| 367 | "-w", |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 368 | "-W", |
| 369 | IPTABLES_RETRY_INTERVAL, |
Lorenzo Colitti | 8917e45 | 2016-08-01 16:47:50 +0900 | [diff] [blame] | 370 | "-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 Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 386 | if (runCmd(ARRAY_SIZE(cmd2), cmd2) && add) { |
Robert Greenwalt | f7bf29c | 2011-11-01 22:07:28 -0700 | [diff] [blame] | 387 | // bail on error, but only if adding |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 388 | rc = -1; |
| 389 | goto err_invalid_drop; |
Robert Greenwalt | ddb9f6e | 2011-08-02 13:00:11 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 392 | if (runCmd(ARRAY_SIZE(cmd3), cmd3) && add) { |
Robert Greenwalt | 210b977 | 2010-03-25 14:54:45 -0700 | [diff] [blame] | 393 | // 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] | 394 | rc = -1; |
| 395 | goto err_return; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 396 | } |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 397 | |
Lorenzo Colitti | bd96d95 | 2016-08-01 18:14:31 +0900 | [diff] [blame] | 398 | if (runCmd(ARRAY_SIZE(cmd4), cmd4) && add) { |
Lorenzo Colitti | 8917e45 | 2016-08-01 16:47:50 +0900 | [diff] [blame] | 399 | rc = -1; |
| 400 | goto err_rpfilter; |
| 401 | } |
| 402 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 403 | if (setTetherCountingRules(add, intIface, extIface) && add) { |
| 404 | rc = -1; |
| 405 | goto err_return; |
| 406 | } |
| 407 | |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 408 | return 0; |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 409 | |
Lorenzo Colitti | 8917e45 | 2016-08-01 16:47:50 +0900 | [diff] [blame] | 410 | err_rpfilter: |
Devi Sandeep Endluri V V | 5619ec1 | 2017-02-13 13:40:01 +0530 | [diff] [blame] | 411 | cmd3[4] = "-D"; |
Lorenzo Colitti | 8917e45 | 2016-08-01 16:47:50 +0900 | [diff] [blame] | 412 | runCmd(ARRAY_SIZE(cmd3), cmd3); |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 413 | err_return: |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 414 | cmd2[4] = "-D"; |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 415 | runCmd(ARRAY_SIZE(cmd2), cmd2); |
| 416 | err_invalid_drop: |
Devi Sandeep Endluri V V | 9afc4f9 | 2016-10-13 13:15:17 +0530 | [diff] [blame] | 417 | cmd1[4] = "-D"; |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 418 | runCmd(ARRAY_SIZE(cmd1), cmd1); |
| 419 | return rc; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 422 | int NatController::disableNat(const char* intIface, const char* extIface) { |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 423 | if (!isIfaceName(intIface) || !isIfaceName(extIface)) { |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 424 | errno = ENODEV; |
| 425 | return -1; |
| 426 | } |
| 427 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 428 | setForwardRules(false, intIface, extIface); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 429 | if (--natCount <= 0) { |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 430 | // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0 |
| 431 | setDefaults(); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 432 | } |
| 433 | return 0; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 434 | } |