2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
28 #include <cutils/properties.h>
30 #define LOG_TAG "NatController"
31 #include <cutils/log.h>
32 #include <logwrap/logwrap.h>
34 #include "NatController.h"
35 #include "NetdConstants.h"
36 #include "RouteController.h"
38 const char* NatController::LOCAL_FORWARD = "natctrl_FORWARD";
39 const char* NatController::LOCAL_MANGLE_FORWARD = "natctrl_mangle_FORWARD";
40 const char* NatController::LOCAL_NAT_POSTROUTING = "natctrl_nat_POSTROUTING";
41 const char* NatController::LOCAL_RAW_PREROUTING = "natctrl_raw_PREROUTING";
42 const char* NatController::LOCAL_TETHER_COUNTERS_CHAIN = "natctrl_tether_counters";
44 auto NatController::execFunction = android_fork_execvp;
46 NatController::NatController() {
49 NatController::~NatController() {
52 struct CommandsAndArgs {
53 /* The array size doesn't really matter as the compiler will barf if too many initializers are specified. */
58 int NatController::runCmd(int argc, const char **argv) {
61 res = execFunction(argc, (char **)argv, NULL, false, false);
64 std::string full_cmd = argv[0];
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.
72 for (; argc && argv[0]; argc--, argv++) {
76 ALOGV("runCmd(%s) res=%d", full_cmd.c_str(), res);
81 int NatController::setupIptablesHooks() {
88 struct CommandsAndArgs defaultCommands[] = {
90 * This is for tethering counters.
91 * This chain is reached via --goto, and then RETURNS.
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},
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.
109 {{IPTABLES_PATH, "-w", "-t", "mangle", "-A", LOCAL_MANGLE_FORWARD, "-p", "tcp",
110 "--tcp-flags", "SYN", "SYN", "-j", "TCPMSS", "--clamp-mss-to-pmtu"}, 0},
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) {
118 ifacePairList.clear();
123 int NatController::setDefaults() {
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.
130 struct CommandsAndArgs defaultCommands[] = {
131 {{IPTABLES_PATH, "-w", "-F", LOCAL_FORWARD,}, 1},
132 {{IP6TABLES_PATH, "-w", "-F", LOCAL_FORWARD,}, 1},
133 {{IPTABLES_PATH, "-w", "-A", LOCAL_FORWARD, "-j", "DROP"}, 1},
134 {{IPTABLES_PATH, "-w", "-t", "nat", "-F", LOCAL_NAT_POSTROUTING}, 1},
135 {{IP6TABLES_PATH, "-w", "-t", "raw", "-F", LOCAL_RAW_PREROUTING}, 1},
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) {
149 int NatController::enableNat(const char* intIface, const char* extIface) {
150 ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface);
152 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
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);
164 // add this if we are the first added nat
166 const char *v4Cmd[] = {
172 LOCAL_NAT_POSTROUTING,
180 * IPv6 tethering doesn't need the state-based conntrack rules, so
181 * it unconditionally jumps to the tether counters chain all the time.
183 const char *v6Cmd[] = {IP6TABLES_PATH, "-w", "-A", LOCAL_FORWARD,
184 "-g", LOCAL_TETHER_COUNTERS_CHAIN};
186 if (runCmd(ARRAY_SIZE(v4Cmd), v4Cmd) || runCmd(ARRAY_SIZE(v6Cmd), v6Cmd)) {
187 ALOGE("Error setting postroute rule: iface=%s", extIface);
188 // unwind what's been done, but don't care about success - what more could we do?
194 if (setForwardRules(true, intIface, extIface) != 0) {
195 ALOGE("Error setting forward rules");
203 /* Always make sure the drop rule is at the end */
204 const char *cmd1[] = {
212 runCmd(ARRAY_SIZE(cmd1), cmd1);
213 const char *cmd2[] = {
221 runCmd(ARRAY_SIZE(cmd2), cmd2);
227 bool NatController::checkTetherCountingRuleExist(const char *pair_name) {
228 std::list<std::string>::iterator it;
230 for (it = ifacePairList.begin(); it != ifacePairList.end(); it++) {
231 if (*it == pair_name) {
232 /* We already have this counter */
239 int NatController::setTetherCountingRules(bool add, const char *intIface, const char *extIface) {
241 /* We only ever add tethering quota rules so that they stick. */
246 asprintf(&pair_name, "%s_%s", intIface, extIface);
248 if (checkTetherCountingRuleExist(pair_name)) {
252 const char *cmd2b[] = {
254 "-w", "-A", LOCAL_TETHER_COUNTERS_CHAIN, "-i", intIface, "-o", extIface, "-j", "RETURN"
257 const char *cmd2c[] = {
259 "-w", "-A", LOCAL_TETHER_COUNTERS_CHAIN, "-i", intIface, "-o", extIface, "-j", "RETURN"
262 if (runCmd(ARRAY_SIZE(cmd2b), cmd2b) || runCmd(ARRAY_SIZE(cmd2c), cmd2c)) {
266 ifacePairList.push_front(pair_name);
269 asprintf(&pair_name, "%s_%s", extIface, intIface);
270 if (checkTetherCountingRuleExist(pair_name)) {
275 const char *cmd3b[] = {
277 "-w", "-A", LOCAL_TETHER_COUNTERS_CHAIN, "-i", extIface, "-o", intIface, "-j", "RETURN"
280 const char *cmd3c[] = {
282 "-w", "-A", LOCAL_TETHER_COUNTERS_CHAIN, "-i", extIface, "-o", intIface, "-j", "RETURN"
285 if (runCmd(ARRAY_SIZE(cmd3b), cmd3b) || runCmd(ARRAY_SIZE(cmd3c), cmd3c)) {
286 // unwind what's been done, but don't care about success - what more could we do?
290 ifacePairList.push_front(pair_name);
295 int NatController::setForwardRules(bool add, const char *intIface, const char *extIface) {
296 const char *cmd1[] = {
308 "ESTABLISHED,RELATED",
310 LOCAL_TETHER_COUNTERS_CHAIN
314 if (runCmd(ARRAY_SIZE(cmd1), cmd1) && add) {
318 const char *cmd2[] = {
335 const char *cmd3[] = {
345 LOCAL_TETHER_COUNTERS_CHAIN
348 const char *cmd4[] = {
354 LOCAL_RAW_PREROUTING,
367 if (runCmd(ARRAY_SIZE(cmd2), cmd2) && add) {
368 // bail on error, but only if adding
370 goto err_invalid_drop;
373 if (runCmd(ARRAY_SIZE(cmd3), cmd3) && add) {
374 // unwind what's been done, but don't care about success - what more could we do?
379 // STOPSHIP: Make this an error.
380 if (runCmd(ARRAY_SIZE(cmd4), cmd4) && add && false /* STOPSHIP */) {
385 if (setTetherCountingRules(add, intIface, extIface) && add) {
394 runCmd(ARRAY_SIZE(cmd3), cmd3);
397 runCmd(ARRAY_SIZE(cmd2), cmd2);
400 runCmd(ARRAY_SIZE(cmd1), cmd1);
404 int NatController::disableNat(const char* intIface, const char* extIface) {
405 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
410 setForwardRules(false, intIface, extIface);
411 if (--natCount <= 0) {
412 // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0