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_TETHER_COUNTERS_CHAIN = "natctrl_tether_counters";
43 NatController::NatController() {
46 NatController::~NatController() {
49 struct CommandsAndArgs {
50 /* The array size doesn't really matter as the compiler will barf if too many initializers are specified. */
55 int NatController::runCmd(int argc, const char **argv) {
58 res = android_fork_execvp(argc, (char **)argv, NULL, false, false);
61 std::string full_cmd = argv[0];
64 * HACK: Sometimes runCmd() is called with a ridcously large value (32)
65 * and it works because the argv[] contains a NULL after the last
66 * true argv. So here we use the NULL argv[] to terminate when the argc
67 * is horribly wrong, and argc for the normal cases.
69 for (; argc && argv[0]; argc--, argv++) {
73 ALOGV("runCmd(%s) res=%d", full_cmd.c_str(), res);
78 int NatController::setupIptablesHooks() {
85 struct CommandsAndArgs defaultCommands[] = {
87 * First chain is for tethering counters.
88 * This chain is reached via --goto, and then RETURNS.
90 * Second chain is used to limit downstream mss to the upstream pmtu
91 * so we don't end up fragmenting every large packet tethered devices
92 * send. Note this feature requires kernel support with flag
93 * CONFIG_NETFILTER_XT_TARGET_TCPMSS=y, which not all builds will have,
94 * so the final rule is allowed to fail.
95 * Bug 17629786 asks to make the failure more obvious, or even fatal
96 * so that all builds eventually gain the performance improvement.
98 {{IPTABLES_PATH, "-w", "-F", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
99 {{IPTABLES_PATH, "-w", "-X", LOCAL_TETHER_COUNTERS_CHAIN,}, 0},
100 {{IPTABLES_PATH, "-w", "-N", LOCAL_TETHER_COUNTERS_CHAIN,}, 1},
101 {{IPTABLES_PATH, "-w", "-t", "mangle", "-A", LOCAL_MANGLE_FORWARD, "-p", "tcp", "--tcp-flags",
102 "SYN", "SYN", "-j", "TCPMSS", "--clamp-mss-to-pmtu"}, 0},
104 for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) {
105 if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) &&
106 defaultCommands[cmdNum].checkRes) {
110 ifacePairList.clear();
115 int NatController::setDefaults() {
117 * The following only works because:
118 * - the defaultsCommands[].cmd array is padded with NULL, and
119 * - the 1st argc of runCmd() will just be the max for the CommandsAndArgs[].cmd, and
120 * - internally it will be memcopied to an array and terminated with a NULL.
122 struct CommandsAndArgs defaultCommands[] = {
123 {{IPTABLES_PATH, "-w", "-F", LOCAL_FORWARD,}, 1},
124 {{IPTABLES_PATH, "-w", "-A", LOCAL_FORWARD, "-j", "DROP"}, 1},
125 {{IPTABLES_PATH, "-w", "-t", "nat", "-F", LOCAL_NAT_POSTROUTING}, 1},
127 for (unsigned int cmdNum = 0; cmdNum < ARRAY_SIZE(defaultCommands); cmdNum++) {
128 if (runCmd(ARRAY_SIZE(defaultCommands[cmdNum].cmd), defaultCommands[cmdNum].cmd) &&
129 defaultCommands[cmdNum].checkRes) {
139 int NatController::enableNat(const char* intIface, const char* extIface) {
140 ALOGV("enableNat(intIface=<%s>, extIface=<%s>)",intIface, extIface);
142 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
147 /* Bug: b/9565268. "enableNat wlan0 wlan0". For now we fail until java-land is fixed */
148 if (!strcmp(intIface, extIface)) {
149 ALOGE("Duplicate interface specified: %s %s", intIface, extIface);
154 // add this if we are the first added nat
156 const char *cmd[] = {
162 LOCAL_NAT_POSTROUTING,
168 if (runCmd(ARRAY_SIZE(cmd), cmd)) {
169 ALOGE("Error setting postroute rule: iface=%s", extIface);
170 // unwind what's been done, but don't care about success - what more could we do?
176 if (setForwardRules(true, intIface, extIface) != 0) {
177 ALOGE("Error setting forward rules");
185 /* Always make sure the drop rule is at the end */
186 const char *cmd1[] = {
194 runCmd(ARRAY_SIZE(cmd1), cmd1);
195 const char *cmd2[] = {
203 runCmd(ARRAY_SIZE(cmd2), cmd2);
209 bool NatController::checkTetherCountingRuleExist(const char *pair_name) {
210 std::list<std::string>::iterator it;
212 for (it = ifacePairList.begin(); it != ifacePairList.end(); it++) {
213 if (*it == pair_name) {
214 /* We already have this counter */
221 int NatController::setTetherCountingRules(bool add, const char *intIface, const char *extIface) {
223 /* We only ever add tethering quota rules so that they stick. */
228 asprintf(&pair_name, "%s_%s", intIface, extIface);
230 if (checkTetherCountingRuleExist(pair_name)) {
234 const char *cmd2b[] = {
238 LOCAL_TETHER_COUNTERS_CHAIN,
247 if (runCmd(ARRAY_SIZE(cmd2b), cmd2b) && add) {
251 ifacePairList.push_front(pair_name);
254 asprintf(&pair_name, "%s_%s", extIface, intIface);
255 if (checkTetherCountingRuleExist(pair_name)) {
260 const char *cmd3b[] = {
264 LOCAL_TETHER_COUNTERS_CHAIN,
273 if (runCmd(ARRAY_SIZE(cmd3b), cmd3b) && add) {
274 // unwind what's been done, but don't care about success - what more could we do?
278 ifacePairList.push_front(pair_name);
283 int NatController::setForwardRules(bool add, const char *intIface, const char *extIface) {
284 const char *cmd1[] = {
296 "ESTABLISHED,RELATED",
298 LOCAL_TETHER_COUNTERS_CHAIN
302 if (runCmd(ARRAY_SIZE(cmd1), cmd1) && add) {
306 const char *cmd2[] = {
323 const char *cmd3[] = {
333 LOCAL_TETHER_COUNTERS_CHAIN
336 if (runCmd(ARRAY_SIZE(cmd2), cmd2) && add) {
337 // bail on error, but only if adding
339 goto err_invalid_drop;
342 if (runCmd(ARRAY_SIZE(cmd3), cmd3) && add) {
343 // unwind what's been done, but don't care about success - what more could we do?
348 if (setTetherCountingRules(add, intIface, extIface) && add) {
357 runCmd(ARRAY_SIZE(cmd2), cmd2);
360 runCmd(ARRAY_SIZE(cmd1), cmd1);
364 int NatController::disableNat(const char* intIface, const char* extIface) {
365 if (!isIfaceName(intIface) || !isIfaceName(extIface)) {
370 setForwardRules(false, intIface, extIface);
371 if (--natCount <= 0) {
372 // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0