blob: 3539844f471e999da3629e28da89f908cae8728f [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>
24
25#define LOG_TAG "NatController"
26#include <cutils/log.h>
27
28#include "NatController.h"
29
30extern "C" int logwrap(int argc, const char **argv, int background);
31
32static char IPTABLES_PATH[] = "/system/bin/iptables";
33
34NatController::NatController() {
35}
36
37NatController::~NatController() {
38}
39
40int NatController::runIptablesCmd(const char *cmd) {
41 char buffer[255];
42
43 strncpy(buffer, cmd, sizeof(buffer)-1);
44
45 const char *args[16];
46 char *next = buffer;
47 char *tmp;
48
49 args[0] = IPTABLES_PATH;
50 args[1] = "--verbose";
51 int i = 2;
52
53 while ((tmp = strsep(&next, " "))) {
54 args[i++] = tmp;
55 if (i == 16) {
56 LOGE("iptables argument overflow");
57 errno = E2BIG;
58 return -1;
59 }
60 }
61 args[i] = NULL;
62
63 return logwrap(i, args, 0);
64}
65
66int NatController::setDefaults() {
67
68 if (runIptablesCmd("-P INPUT ACCEPT"))
69 return -1;
70 if (runIptablesCmd("-F INPUT"))
71 return -1;
72 if (runIptablesCmd("-P OUTPUT ACCEPT"))
73 return -1;
74 if (runIptablesCmd("-F OUTPUT"))
75 return -1;
76 if (runIptablesCmd("-P FORWARD DROP"))
77 return -1;
78 if (runIptablesCmd("-F FORWARD"))
79 return -1;
80 if (runIptablesCmd("-t nat -F"))
81 return -1;
82 return 0;
83}
84
85bool NatController::interfaceExists(const char *iface) {
86 // XXX: STOPSHIP - Implement this
87 return true;
88}
89
90// TODO: Make this work for multiple masqueraded networks
91int NatController::enableNat(const char *intIface, const char *extIface) {
92 char cmd[255];
93
94 if (!interfaceExists(intIface) || !interfaceExists (extIface)) {
95 LOGE("Invalid interface specified");
96 errno = ENODEV;
97 return -1;
98 }
99
100 snprintf(cmd, sizeof(cmd),
101 "-A FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j ACCEPT",
102 extIface, intIface);
103 if (runIptablesCmd(cmd)) {
104 return -1;
105 }
106
107 snprintf(cmd, sizeof(cmd), "-A FORWARD -i %s -o %s -j ACCEPT", intIface, extIface);
108 if (runIptablesCmd(cmd)) {
109 return -1;
110 }
111
112 snprintf(cmd, sizeof(cmd), "-t nat -A POSTROUTING -o %s -j MASQUERADE", extIface);
113 if (runIptablesCmd(cmd)) {
114 return -1;
115 }
116
117 return 0;
118}
119
120// TODO: Make this work for multiple masqueraded networks
121int NatController::disableNat(const char *intIface, const char *extIface) {
122 return setDefaults();
123}