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.
19 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
25 #include <cutils/properties.h>
27 #define LOG_TAG "NatController"
28 #include <cutils/log.h>
30 #include "NatController.h"
32 extern "C" int logwrap(int argc, const char **argv, int background);
34 static char IPTABLES_PATH[] = "/system/bin/iptables";
36 NatController::NatController() {
40 NatController::~NatController() {
43 int NatController::runIptablesCmd(const char *cmd) {
46 strncpy(buffer, cmd, sizeof(buffer)-1);
52 args[0] = IPTABLES_PATH;
53 args[1] = "--verbose";
56 while ((tmp = strsep(&next, " "))) {
59 LOGE("iptables argument overflow");
66 return logwrap(i, args, 0);
69 int NatController::setDefaults() {
71 if (runIptablesCmd("-P INPUT ACCEPT"))
73 if (runIptablesCmd("-P OUTPUT ACCEPT"))
75 if (runIptablesCmd("-P FORWARD DROP"))
77 if (runIptablesCmd("-F FORWARD"))
79 if (runIptablesCmd("-t nat -F"))
84 bool NatController::interfaceExists(const char *iface) {
85 // XXX: Implement this
89 int NatController::doNatCommands(const char *intIface, const char *extIface, bool add) {
92 char bootmode[PROPERTY_VALUE_MAX] = {0};
93 property_get("ro.bootmode", bootmode, "unknown");
94 if (0 != strcmp("bp-tools", bootmode)) {
95 // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0
98 int ret = setDefaults();
107 if (!interfaceExists(intIface) || !interfaceExists (extIface)) {
108 LOGE("Invalid interface specified");
113 snprintf(cmd, sizeof(cmd),
114 "-%s FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
117 if (runIptablesCmd(cmd)) {
121 snprintf(cmd, sizeof(cmd), "-%s FORWARD -i %s -o %s -j ACCEPT", (add ? "A" : "D"),
123 if (runIptablesCmd(cmd)) {
124 // unwind what's been done, but don't care about success - what more could we do?
125 snprintf(cmd, sizeof(cmd),
126 "-%s FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
132 // add this if we are the first added nat
133 if (add && natCount == 0) {
134 snprintf(cmd, sizeof(cmd), "-t nat -A POSTROUTING -o %s -j MASQUERADE", extIface);
135 if (runIptablesCmd(cmd)) {
136 if (0 != strcmp("bp-tools", bootmode)) {
137 // unwind what's been done, but don't care about success - what more could we do?
152 int NatController::enableNat(const char *intIface, const char *extIface) {
153 return doNatCommands(intIface, extIface, true);
156 int NatController::disableNat(const char *intIface, const char *extIface) {
157 return doNatCommands(intIface, extIface, false);