NatController.cpp revision 11b4e9b26fe7b878992162afb39f5a8acfd143ed
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
17#include <stdlib.h>
18#include <errno.h>
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <fcntl.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24#include <string.h>
25#include <cutils/properties.h>
26
27#define LOG_TAG "NatController"
28#include <cutils/log.h>
29
30#include "NatController.h"
31
32extern "C" int logwrap(int argc, const char **argv, int background);
33
34static char IPTABLES_PATH[] = "/system/bin/iptables";
35
36NatController::NatController() {
37    natCount = 0;
38}
39
40NatController::~NatController() {
41}
42
43int NatController::runIptablesCmd(const char *cmd) {
44    char *buffer;
45    size_t len = strnlen(cmd, 255);
46    int res;
47
48    if (len == 255) {
49        LOGE("iptables command too long");
50        errno = E2BIG;
51        return -1;
52    }
53
54    asprintf(&buffer, "%s %s", IPTABLES_PATH, cmd);
55    res = system(buffer);
56    free(buffer);
57    return res;
58}
59
60int NatController::setDefaults() {
61
62    if (runIptablesCmd("-P INPUT ACCEPT"))
63        return -1;
64    if (runIptablesCmd("-P OUTPUT ACCEPT"))
65        return -1;
66    if (runIptablesCmd("-P FORWARD DROP"))
67        return -1;
68    if (runIptablesCmd("-F FORWARD"))
69        return -1;
70    if (runIptablesCmd("-t nat -F"))
71        return -1;
72    return 0;
73}
74
75bool NatController::interfaceExists(const char *iface) {
76    // XXX: Implement this
77    return true;
78}
79
80int NatController::doNatCommands(const char *intIface, const char *extIface, bool add) {
81    char cmd[255];
82
83    char bootmode[PROPERTY_VALUE_MAX] = {0};
84    property_get("ro.bootmode", bootmode, "unknown");
85    if (0 != strcmp("bp-tools", bootmode)) {
86        // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0
87        if (add == false) {
88            if (natCount <= 1) {
89                int ret = setDefaults();
90                if (ret == 0) {
91                    natCount=0;
92                }
93                return ret;
94            }
95        }
96    }
97
98    if (!interfaceExists(intIface) || !interfaceExists (extIface)) {
99        LOGE("Invalid interface specified");
100        errno = ENODEV;
101        return -1;
102    }
103
104    snprintf(cmd, sizeof(cmd),
105             "-%s FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
106             (add ? "A" : "D"),
107             extIface, intIface);
108    if (runIptablesCmd(cmd)) {
109        return -1;
110    }
111
112    snprintf(cmd, sizeof(cmd),
113            "-%s FORWARD -i %s -o %s -m state --state INVALID -j DROP",
114            (add ? "A" : "D"),
115            intIface, extIface);
116    if (runIptablesCmd(cmd)) {
117        snprintf(cmd, sizeof(cmd),
118                "-%s FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
119                (!add ? "A" : "D"),
120                extIface, intIface);
121        runIptablesCmd(cmd);
122        return -1;
123    }
124
125    snprintf(cmd, sizeof(cmd), "-%s FORWARD -i %s -o %s -j ACCEPT", (add ? "A" : "D"),
126            intIface, extIface);
127    if (runIptablesCmd(cmd)) {
128        // unwind what's been done, but don't care about success - what more could we do?
129        snprintf(cmd, sizeof(cmd),
130                "-%s FORWARD -i %s -o %s -m state --state INVALID -j DROP",
131                (!add ? "A" : "D"),
132                intIface, extIface);
133        runIptablesCmd(cmd);
134
135        snprintf(cmd, sizeof(cmd),
136                 "-%s FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
137                 (!add ? "A" : "D"),
138                 extIface, intIface);
139        runIptablesCmd(cmd);
140        return -1;
141    }
142
143    // add this if we are the first added nat
144    if (add && natCount == 0) {
145        snprintf(cmd, sizeof(cmd), "-t nat -A POSTROUTING -o %s -j MASQUERADE", extIface);
146        if (runIptablesCmd(cmd)) {
147            if (0 != strcmp("bp-tools", bootmode)) {
148                // unwind what's been done, but don't care about success - what more could we do?
149                setDefaults();;
150            }
151            return -1;
152        }
153    }
154
155    if (add) {
156        natCount++;
157    } else {
158        natCount--;
159    }
160    return 0;
161}
162
163int NatController::enableNat(const char *intIface, const char *extIface) {
164    return doNatCommands(intIface, extIface, true);
165}
166
167int NatController::disableNat(const char *intIface, const char *extIface) {
168    return doNatCommands(intIface, extIface, false);
169}
170