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 | */ |
| 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 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 | */ |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 109 | {{IPTABLES_PATH, "-w", "-t", "mangle", "-A", LOCAL_MANGLE_FORWARD, "-p", "tcp", |
| 110 | "--tcp-flags", "SYN", "SYN", "-j", "TCPMSS", "--clamp-mss-to-pmtu"}, 0}, |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 111 | }; |
| 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 Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 118 | ifacePairList.clear(); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 119 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | int NatController::setDefaults() { |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 124 | /* |
| 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 Abgrall | 4ae80de | 2013-03-14 20:06:20 -0700 | [diff] [blame] | 130 | struct CommandsAndArgs defaultCommands[] = { |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 131 | {{IPTABLES_PATH, "-w", "-F", LOCAL_FORWARD,}, 1}, |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 132 | {{IP6TABLES_PATH, "-w", "-F", LOCAL_FORWARD,}, 1}, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 133 | {{IPTABLES_PATH, "-w", "-A", LOCAL_FORWARD, "-j", "DROP"}, 1}, |
| 134 | {{IPTABLES_PATH, "-w", "-t", "nat", "-F", LOCAL_NAT_POSTROUTING}, 1}, |
Lorenzo Colitti | 8917e45 | 2016-08-01 16:47:50 +0900 | [diff] [blame] | 135 | {{IP6TABLES_PATH, "-w", "-t", "raw", "-F", LOCAL_RAW_PREROUTING}, 1}, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 136 | }; |
JP Abgrall | 4ae80de | 2013-03-14 20:06:20 -0700 | [diff] [blame] | 137 | 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 Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 143 | |
| 144 | natCount = 0; |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 145 | |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 149 | int NatController::enableNat(const char* intIface, const char* extIface) { |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 150 | ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface); |
| 151 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 152 | if (!isIfaceName(intIface) || !isIfaceName(extIface)) { |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 153 | errno = ENODEV; |
| 154 | return -1; |
| 155 | } |
| 156 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 157 | /* 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 Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 164 | // add this if we are the first added nat |
| 165 | if (natCount == 0) { |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 166 | const char *v4Cmd[] = { |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 167 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 168 | "-w", |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 169 | "-t", |
| 170 | "nat", |
| 171 | "-A", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 172 | LOCAL_NAT_POSTROUTING, |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 173 | "-o", |
| 174 | extIface, |
| 175 | "-j", |
| 176 | "MASQUERADE" |
| 177 | }; |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 178 | |
| 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 Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 187 | ALOGE("Error setting postroute rule: iface=%s", extIface); |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 188 | // 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] | 189 | setDefaults(); |
| 190 | return -1; |
| 191 | } |
| 192 | } |
| 193 | |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 194 | if (setForwardRules(true, intIface, extIface) != 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 195 | ALOGE("Error setting forward rules"); |
JP Abgrall | 659692a | 2013-03-14 20:07:17 -0700 | [diff] [blame] | 196 | if (natCount == 0) { |
| 197 | setDefaults(); |
| 198 | } |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 199 | errno = ENODEV; |
| 200 | return -1; |
| 201 | } |
| 202 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 203 | /* Always make sure the drop rule is at the end */ |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 204 | const char *cmd1[] = { |
| 205 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 206 | "-w", |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 207 | "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 208 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 209 | "-j", |
| 210 | "DROP" |
| 211 | }; |
| 212 | runCmd(ARRAY_SIZE(cmd1), cmd1); |
| 213 | const char *cmd2[] = { |
| 214 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 215 | "-w", |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 216 | "-A", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 217 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 218 | "-j", |
| 219 | "DROP" |
| 220 | }; |
| 221 | runCmd(ARRAY_SIZE(cmd2), cmd2); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 222 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 223 | natCount++; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 227 | bool 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 Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 239 | int 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 Ramachandran | 56afacf | 2014-05-28 15:07:00 -0700 | [diff] [blame] | 245 | char *pair_name; |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 246 | asprintf(&pair_name, "%s_%s", intIface, extIface); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 247 | |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 248 | if (checkTetherCountingRuleExist(pair_name)) { |
| 249 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 250 | return 0; |
| 251 | } |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 252 | const char *cmd2b[] = { |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 253 | IPTABLES_PATH, |
| 254 | "-w", "-A", LOCAL_TETHER_COUNTERS_CHAIN, "-i", intIface, "-o", extIface, "-j", "RETURN" |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 255 | }; |
| 256 | |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 257 | 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 Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 263 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 264 | return -1; |
| 265 | } |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 266 | ifacePairList.push_front(pair_name); |
| 267 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 268 | |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 269 | asprintf(&pair_name, "%s_%s", extIface, intIface); |
| 270 | if (checkTetherCountingRuleExist(pair_name)) { |
| 271 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 272 | return 0; |
| 273 | } |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 274 | |
| 275 | const char *cmd3b[] = { |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 276 | IPTABLES_PATH, |
| 277 | "-w", "-A", LOCAL_TETHER_COUNTERS_CHAIN, "-i", extIface, "-o", intIface, "-j", "RETURN" |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 278 | }; |
| 279 | |
Lorenzo Colitti | 05cfd25 | 2016-07-10 23:15:46 +0900 | [diff] [blame] | 280 | 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 Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 286 | // 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] | 287 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 288 | return -1; |
| 289 | } |
JP Abgrall | f3cc83f | 2013-09-11 20:01:59 -0700 | [diff] [blame] | 290 | ifacePairList.push_front(pair_name); |
| 291 | free(pair_name); |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | int NatController::setForwardRules(bool add, const char *intIface, const char *extIface) { |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 296 | const char *cmd1[] = { |
| 297 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 298 | "-w", |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 299 | add ? "-A" : "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 300 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 301 | "-i", |
| 302 | extIface, |
| 303 | "-o", |
| 304 | intIface, |
| 305 | "-m", |
| 306 | "state", |
| 307 | "--state", |
| 308 | "ESTABLISHED,RELATED", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 309 | "-g", |
| 310 | LOCAL_TETHER_COUNTERS_CHAIN |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 311 | }; |
| 312 | int rc = 0; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 313 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 314 | if (runCmd(ARRAY_SIZE(cmd1), cmd1) && add) { |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 315 | return -1; |
| 316 | } |
| 317 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 318 | const char *cmd2[] = { |
| 319 | IPTABLES_PATH, |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 320 | "-w", |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 321 | add ? "-A" : "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 322 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 323 | "-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 Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 337 | "-w", |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 338 | add ? "-A" : "-D", |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 339 | LOCAL_FORWARD, |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 340 | "-i", |
| 341 | intIface, |
| 342 | "-o", |
| 343 | extIface, |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 344 | "-g", |
| 345 | LOCAL_TETHER_COUNTERS_CHAIN |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 346 | }; |
| 347 | |
Lorenzo Colitti | 8917e45 | 2016-08-01 16:47:50 +0900 | [diff] [blame] | 348 | 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 Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 367 | if (runCmd(ARRAY_SIZE(cmd2), cmd2) && add) { |
Robert Greenwalt | f7bf29c | 2011-11-01 22:07:28 -0700 | [diff] [blame] | 368 | // bail on error, but only if adding |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 369 | rc = -1; |
| 370 | goto err_invalid_drop; |
Robert Greenwalt | ddb9f6e | 2011-08-02 13:00:11 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 373 | if (runCmd(ARRAY_SIZE(cmd3), cmd3) && add) { |
Robert Greenwalt | 210b977 | 2010-03-25 14:54:45 -0700 | [diff] [blame] | 374 | // 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] | 375 | rc = -1; |
| 376 | goto err_return; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 377 | } |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 378 | |
Lorenzo Colitti | bd96d95 | 2016-08-01 18:14:31 +0900 | [diff] [blame^] | 379 | if (runCmd(ARRAY_SIZE(cmd4), cmd4) && add) { |
Lorenzo Colitti | 8917e45 | 2016-08-01 16:47:50 +0900 | [diff] [blame] | 380 | rc = -1; |
| 381 | goto err_rpfilter; |
| 382 | } |
| 383 | |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 384 | if (setTetherCountingRules(add, intIface, extIface) && add) { |
| 385 | rc = -1; |
| 386 | goto err_return; |
| 387 | } |
| 388 | |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 389 | return 0; |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 390 | |
Lorenzo Colitti | 8917e45 | 2016-08-01 16:47:50 +0900 | [diff] [blame] | 391 | err_rpfilter: |
| 392 | cmd3[2] = "-D"; |
| 393 | runCmd(ARRAY_SIZE(cmd3), cmd3); |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 394 | err_return: |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 395 | cmd2[2] = "-D"; |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 396 | runCmd(ARRAY_SIZE(cmd2), cmd2); |
| 397 | err_invalid_drop: |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 398 | cmd1[2] = "-D"; |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 399 | runCmd(ARRAY_SIZE(cmd1), cmd1); |
| 400 | return rc; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 403 | int NatController::disableNat(const char* intIface, const char* extIface) { |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 404 | if (!isIfaceName(intIface) || !isIfaceName(extIface)) { |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 405 | errno = ENODEV; |
| 406 | return -1; |
| 407 | } |
| 408 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 409 | setForwardRules(false, intIface, extIface); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 410 | if (--natCount <= 0) { |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 411 | // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0 |
| 412 | setDefaults(); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 413 | } |
| 414 | return 0; |
San Mehat | 9ff78fb | 2010-01-19 12:59:15 -0800 | [diff] [blame] | 415 | } |