blob: c05aa7b04c6688d145297e432d2d436cfbbfa8ab [file] [log] [blame]
San Mehat9ff78fb2010-01-19 12:59:15 -08001/*
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>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080024#include <string.h>
John Michelauac208602011-05-27 22:07:20 -050025#include <cutils/properties.h>
San Mehat9ff78fb2010-01-19 12:59:15 -080026
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() {
Robert Greenwalt1caafe62010-03-24 15:43:00 -070037 natCount = 0;
San Mehat9ff78fb2010-01-19 12:59:15 -080038}
39
40NatController::~NatController() {
41}
42
43int NatController::runIptablesCmd(const char *cmd) {
JP Abgrall11b4e9b2011-08-11 15:34:49 -070044 char *buffer;
45 size_t len = strnlen(cmd, 255);
46 int res;
San Mehat9ff78fb2010-01-19 12:59:15 -080047
JP Abgrall11b4e9b2011-08-11 15:34:49 -070048 if (len == 255) {
49 LOGE("iptables command too long");
50 errno = E2BIG;
51 return -1;
San Mehat9ff78fb2010-01-19 12:59:15 -080052 }
San Mehat9ff78fb2010-01-19 12:59:15 -080053
JP Abgrall11b4e9b2011-08-11 15:34:49 -070054 asprintf(&buffer, "%s %s", IPTABLES_PATH, cmd);
55 res = system(buffer);
56 free(buffer);
57 return res;
San Mehat9ff78fb2010-01-19 12:59:15 -080058}
59
60int NatController::setDefaults() {
61
62 if (runIptablesCmd("-P INPUT ACCEPT"))
63 return -1;
San Mehat9ff78fb2010-01-19 12:59:15 -080064 if (runIptablesCmd("-P OUTPUT ACCEPT"))
65 return -1;
San Mehat9ff78fb2010-01-19 12:59:15 -080066 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) {
Paul Easthamb5ff9b22010-10-13 09:42:51 -070076 // XXX: Implement this
San Mehat9ff78fb2010-01-19 12:59:15 -080077 return true;
78}
79
Robert Greenwalt1caafe62010-03-24 15:43:00 -070080int NatController::doNatCommands(const char *intIface, const char *extIface, bool add) {
San Mehat9ff78fb2010-01-19 12:59:15 -080081 char cmd[255];
82
John Michelauac208602011-05-27 22:07:20 -050083 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;
Robert Greenwalt210b9772010-03-25 14:54:45 -070094 }
Robert Greenwalt210b9772010-03-25 14:54:45 -070095 }
96 }
97
San Mehat9ff78fb2010-01-19 12:59:15 -080098 if (!interfaceExists(intIface) || !interfaceExists (extIface)) {
99 LOGE("Invalid interface specified");
100 errno = ENODEV;
101 return -1;
102 }
103
104 snprintf(cmd, sizeof(cmd),
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700105 "-%s FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
106 (add ? "A" : "D"),
San Mehat9ff78fb2010-01-19 12:59:15 -0800107 extIface, intIface);
108 if (runIptablesCmd(cmd)) {
109 return -1;
110 }
111
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700112 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);
Robert Greenwalt6e4d5db2011-08-03 16:51:30 -0700121 runIptablesCmd(cmd);
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700122 return -1;
123 }
124
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700125 snprintf(cmd, sizeof(cmd), "-%s FORWARD -i %s -o %s -j ACCEPT", (add ? "A" : "D"),
126 intIface, extIface);
San Mehat9ff78fb2010-01-19 12:59:15 -0800127 if (runIptablesCmd(cmd)) {
Robert Greenwalt210b9772010-03-25 14:54:45 -0700128 // unwind what's been done, but don't care about success - what more could we do?
129 snprintf(cmd, sizeof(cmd),
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700130 "-%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),
Robert Greenwalt210b9772010-03-25 14:54:45 -0700136 "-%s FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
137 (!add ? "A" : "D"),
138 extIface, intIface);
Robert Greenwalt6e4d5db2011-08-03 16:51:30 -0700139 runIptablesCmd(cmd);
San Mehat9ff78fb2010-01-19 12:59:15 -0800140 return -1;
141 }
142
Robert Greenwalt210b9772010-03-25 14:54:45 -0700143 // add this if we are the first added nat
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700144 if (add && natCount == 0) {
145 snprintf(cmd, sizeof(cmd), "-t nat -A POSTROUTING -o %s -j MASQUERADE", extIface);
146 if (runIptablesCmd(cmd)) {
John Michelauac208602011-05-27 22:07:20 -0500147 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 }
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700151 return -1;
152 }
San Mehat9ff78fb2010-01-19 12:59:15 -0800153 }
154
Robert Greenwalt210b9772010-03-25 14:54:45 -0700155 if (add) {
156 natCount++;
157 } else {
158 natCount--;
159 }
San Mehat9ff78fb2010-01-19 12:59:15 -0800160 return 0;
161}
162
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700163int NatController::enableNat(const char *intIface, const char *extIface) {
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700164 return doNatCommands(intIface, extIface, true);
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700165}
166
San Mehat9ff78fb2010-01-19 12:59:15 -0800167int NatController::disableNat(const char *intIface, const char *extIface) {
Robert Greenwalt210b9772010-03-25 14:54:45 -0700168 return doNatCommands(intIface, extIface, false);
San Mehat9ff78fb2010-01-19 12:59:15 -0800169}