blob: ddb049975fd5b958f62365c2c6ccf8182c1645c2 [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) {
44 char buffer[255];
45
46 strncpy(buffer, cmd, sizeof(buffer)-1);
47
48 const char *args[16];
49 char *next = buffer;
50 char *tmp;
51
52 args[0] = IPTABLES_PATH;
53 args[1] = "--verbose";
54 int i = 2;
55
56 while ((tmp = strsep(&next, " "))) {
57 args[i++] = tmp;
58 if (i == 16) {
59 LOGE("iptables argument overflow");
60 errno = E2BIG;
61 return -1;
62 }
63 }
64 args[i] = NULL;
65
66 return logwrap(i, args, 0);
67}
68
69int NatController::setDefaults() {
70
71 if (runIptablesCmd("-P INPUT ACCEPT"))
72 return -1;
73 if (runIptablesCmd("-F INPUT"))
74 return -1;
75 if (runIptablesCmd("-P OUTPUT ACCEPT"))
76 return -1;
77 if (runIptablesCmd("-F OUTPUT"))
78 return -1;
79 if (runIptablesCmd("-P FORWARD DROP"))
80 return -1;
81 if (runIptablesCmd("-F FORWARD"))
82 return -1;
83 if (runIptablesCmd("-t nat -F"))
84 return -1;
85 return 0;
86}
87
88bool NatController::interfaceExists(const char *iface) {
Paul Easthamb5ff9b22010-10-13 09:42:51 -070089 // XXX: Implement this
San Mehat9ff78fb2010-01-19 12:59:15 -080090 return true;
91}
92
Robert Greenwalt1caafe62010-03-24 15:43:00 -070093int NatController::doNatCommands(const char *intIface, const char *extIface, bool add) {
San Mehat9ff78fb2010-01-19 12:59:15 -080094 char cmd[255];
95
John Michelauac208602011-05-27 22:07:20 -050096 char bootmode[PROPERTY_VALUE_MAX] = {0};
97 property_get("ro.bootmode", bootmode, "unknown");
98 if (0 != strcmp("bp-tools", bootmode)) {
99 // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0
100 if (add == false) {
101 if (natCount <= 1) {
102 int ret = setDefaults();
103 if (ret == 0) {
104 natCount=0;
105 }
106 return ret;
Robert Greenwalt210b9772010-03-25 14:54:45 -0700107 }
Robert Greenwalt210b9772010-03-25 14:54:45 -0700108 }
109 }
110
San Mehat9ff78fb2010-01-19 12:59:15 -0800111 if (!interfaceExists(intIface) || !interfaceExists (extIface)) {
112 LOGE("Invalid interface specified");
113 errno = ENODEV;
114 return -1;
115 }
116
117 snprintf(cmd, sizeof(cmd),
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700118 "-%s FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
119 (add ? "A" : "D"),
San Mehat9ff78fb2010-01-19 12:59:15 -0800120 extIface, intIface);
121 if (runIptablesCmd(cmd)) {
122 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),
130 "-%s FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
131 (!add ? "A" : "D"),
132 extIface, intIface);
San Mehat9ff78fb2010-01-19 12:59:15 -0800133 return -1;
134 }
135
Robert Greenwalt210b9772010-03-25 14:54:45 -0700136 // add this if we are the first added nat
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700137 if (add && natCount == 0) {
138 snprintf(cmd, sizeof(cmd), "-t nat -A POSTROUTING -o %s -j MASQUERADE", extIface);
139 if (runIptablesCmd(cmd)) {
John Michelauac208602011-05-27 22:07:20 -0500140 if (0 != strcmp("bp-tools", bootmode)) {
141 // unwind what's been done, but don't care about success - what more could we do?
142 setDefaults();;
143 }
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700144 return -1;
145 }
San Mehat9ff78fb2010-01-19 12:59:15 -0800146 }
147
Robert Greenwalt210b9772010-03-25 14:54:45 -0700148 if (add) {
149 natCount++;
150 } else {
151 natCount--;
152 }
San Mehat9ff78fb2010-01-19 12:59:15 -0800153 return 0;
154}
155
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700156int NatController::enableNat(const char *intIface, const char *extIface) {
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700157 return doNatCommands(intIface, extIface, true);
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700158}
159
San Mehat9ff78fb2010-01-19 12:59:15 -0800160int NatController::disableNat(const char *intIface, const char *extIface) {
Robert Greenwalt210b9772010-03-25 14:54:45 -0700161 return doNatCommands(intIface, extIface, false);
San Mehat9ff78fb2010-01-19 12:59:15 -0800162}