blob: 49984f8bf340bdd6c267f3244d3dee99ef1d02eb [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 Greenwaltf7bf29c2011-11-01 22:07:28 -070080// when un-doing NAT, we should report errors, but also try to do as much cleanup
81// as we can - don't short circuit on error.
Robert Greenwalt1caafe62010-03-24 15:43:00 -070082int NatController::doNatCommands(const char *intIface, const char *extIface, bool add) {
San Mehat9ff78fb2010-01-19 12:59:15 -080083 char cmd[255];
84
John Michelauac208602011-05-27 22:07:20 -050085 char bootmode[PROPERTY_VALUE_MAX] = {0};
86 property_get("ro.bootmode", bootmode, "unknown");
87 if (0 != strcmp("bp-tools", bootmode)) {
88 // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0
89 if (add == false) {
90 if (natCount <= 1) {
91 int ret = setDefaults();
92 if (ret == 0) {
93 natCount=0;
94 }
Robert Greenwaltf7bf29c2011-11-01 22:07:28 -070095 LOGE("setDefaults returned %d", ret);
John Michelauac208602011-05-27 22:07:20 -050096 return ret;
Robert Greenwalt210b9772010-03-25 14:54:45 -070097 }
Robert Greenwalt210b9772010-03-25 14:54:45 -070098 }
99 }
100
San Mehat9ff78fb2010-01-19 12:59:15 -0800101 if (!interfaceExists(intIface) || !interfaceExists (extIface)) {
102 LOGE("Invalid interface specified");
103 errno = ENODEV;
104 return -1;
105 }
106
107 snprintf(cmd, sizeof(cmd),
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700108 "-%s FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
109 (add ? "A" : "D"),
San Mehat9ff78fb2010-01-19 12:59:15 -0800110 extIface, intIface);
Robert Greenwaltf7bf29c2011-11-01 22:07:28 -0700111 if (runIptablesCmd(cmd) && add) {
112 // only bail out if we are adding, not removing nat rules
San Mehat9ff78fb2010-01-19 12:59:15 -0800113 return -1;
114 }
115
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700116 snprintf(cmd, sizeof(cmd),
117 "-%s FORWARD -i %s -o %s -m state --state INVALID -j DROP",
118 (add ? "A" : "D"),
119 intIface, extIface);
Robert Greenwaltf7bf29c2011-11-01 22:07:28 -0700120 if (runIptablesCmd(cmd) && add) {
121 // bail on error, but only if adding
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700122 snprintf(cmd, sizeof(cmd),
123 "-%s FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
124 (!add ? "A" : "D"),
125 extIface, intIface);
Robert Greenwalt6e4d5db2011-08-03 16:51:30 -0700126 runIptablesCmd(cmd);
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700127 return -1;
128 }
129
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700130 snprintf(cmd, sizeof(cmd), "-%s FORWARD -i %s -o %s -j ACCEPT", (add ? "A" : "D"),
131 intIface, extIface);
Robert Greenwaltf7bf29c2011-11-01 22:07:28 -0700132 if (runIptablesCmd(cmd) && add) {
Robert Greenwalt210b9772010-03-25 14:54:45 -0700133 // unwind what's been done, but don't care about success - what more could we do?
134 snprintf(cmd, sizeof(cmd),
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700135 "-%s FORWARD -i %s -o %s -m state --state INVALID -j DROP",
136 (!add ? "A" : "D"),
137 intIface, extIface);
138 runIptablesCmd(cmd);
139
140 snprintf(cmd, sizeof(cmd),
Robert Greenwalt210b9772010-03-25 14:54:45 -0700141 "-%s FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
142 (!add ? "A" : "D"),
143 extIface, intIface);
Robert Greenwalt6e4d5db2011-08-03 16:51:30 -0700144 runIptablesCmd(cmd);
San Mehat9ff78fb2010-01-19 12:59:15 -0800145 return -1;
146 }
147
Robert Greenwalt210b9772010-03-25 14:54:45 -0700148 // add this if we are the first added nat
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700149 if (add && natCount == 0) {
150 snprintf(cmd, sizeof(cmd), "-t nat -A POSTROUTING -o %s -j MASQUERADE", extIface);
151 if (runIptablesCmd(cmd)) {
John Michelauac208602011-05-27 22:07:20 -0500152 if (0 != strcmp("bp-tools", bootmode)) {
153 // unwind what's been done, but don't care about success - what more could we do?
154 setDefaults();;
155 }
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700156 return -1;
157 }
San Mehat9ff78fb2010-01-19 12:59:15 -0800158 }
159
Robert Greenwalt210b9772010-03-25 14:54:45 -0700160 if (add) {
161 natCount++;
162 } else {
163 natCount--;
164 }
San Mehat9ff78fb2010-01-19 12:59:15 -0800165 return 0;
166}
167
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700168int NatController::enableNat(const char *intIface, const char *extIface) {
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700169 return doNatCommands(intIface, extIface, true);
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700170}
171
San Mehat9ff78fb2010-01-19 12:59:15 -0800172int NatController::disableNat(const char *intIface, const char *extIface) {
Robert Greenwalt210b9772010-03-25 14:54:45 -0700173 return doNatCommands(intIface, extIface, false);
San Mehat9ff78fb2010-01-19 12:59:15 -0800174}