blob: 77c4874d1dfec540d9cafb312c10af8bd6c6448d [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
JP Abgrall0031cea2012-04-17 16:38:23 -070017// #define LOG_NDEBUG 0
18
San Mehat9ff78fb2010-01-19 12:59:15 -080019#include <stdlib.h>
20#include <errno.h>
21#include <sys/socket.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <netinet/in.h>
25#include <arpa/inet.h>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080026#include <string.h>
John Michelauac208602011-05-27 22:07:20 -050027#include <cutils/properties.h>
San Mehat9ff78fb2010-01-19 12:59:15 -080028
29#define LOG_TAG "NatController"
30#include <cutils/log.h>
31
32#include "NatController.h"
Robert Greenwaltfc97b822011-11-02 16:48:36 -070033#include "SecondaryTableController.h"
Robert Greenwaltc4621772012-01-31 12:46:45 -080034#include "NetdConstants.h"
San Mehat9ff78fb2010-01-19 12:59:15 -080035
JP Abgrall9e5e0ce2011-12-14 15:20:59 -080036extern "C" int system_nosh(const char *command);
San Mehat9ff78fb2010-01-19 12:59:15 -080037
Robert Greenwaltfc97b822011-11-02 16:48:36 -070038NatController::NatController(SecondaryTableController *ctrl) {
39 secondaryTableCtrl = ctrl;
San Mehat9ff78fb2010-01-19 12:59:15 -080040}
41
42NatController::~NatController() {
43}
44
Robert Greenwaltfc97b822011-11-02 16:48:36 -070045int NatController::runCmd(const char *path, const char *cmd) {
JP Abgrall11b4e9b2011-08-11 15:34:49 -070046 char *buffer;
47 size_t len = strnlen(cmd, 255);
48 int res;
San Mehat9ff78fb2010-01-19 12:59:15 -080049
JP Abgrall11b4e9b2011-08-11 15:34:49 -070050 if (len == 255) {
Steve Block5ea0c052012-01-06 19:18:11 +000051 ALOGE("command too long");
JP Abgrall11b4e9b2011-08-11 15:34:49 -070052 errno = E2BIG;
53 return -1;
San Mehat9ff78fb2010-01-19 12:59:15 -080054 }
San Mehat9ff78fb2010-01-19 12:59:15 -080055
Robert Greenwaltfc97b822011-11-02 16:48:36 -070056 asprintf(&buffer, "%s %s", path, cmd);
JP Abgrall9e5e0ce2011-12-14 15:20:59 -080057 res = system_nosh(buffer);
JP Abgrall0031cea2012-04-17 16:38:23 -070058 ALOGV("runCmd() buffer='%s' res=%d", buffer, res);
JP Abgrall11b4e9b2011-08-11 15:34:49 -070059 free(buffer);
60 return res;
San Mehat9ff78fb2010-01-19 12:59:15 -080061}
62
JP Abgrall0031cea2012-04-17 16:38:23 -070063int NatController::setupIptablesHooks() {
Robert Greenwaltfc97b822011-11-02 16:48:36 -070064 if (runCmd(IPTABLES_PATH, "-P INPUT ACCEPT"))
San Mehat9ff78fb2010-01-19 12:59:15 -080065 return -1;
Robert Greenwaltfc97b822011-11-02 16:48:36 -070066 if (runCmd(IPTABLES_PATH, "-P OUTPUT ACCEPT"))
San Mehat9ff78fb2010-01-19 12:59:15 -080067 return -1;
JP Abgrall0031cea2012-04-17 16:38:23 -070068 if (runCmd(IPTABLES_PATH, "-P FORWARD ACCEPT"))
San Mehat9ff78fb2010-01-19 12:59:15 -080069 return -1;
JP Abgrall0031cea2012-04-17 16:38:23 -070070
71 // Order is important!
72 // -D to delete any pre-existing jump rule, to prevent dupes (no-op if doesn't exist)
73 // -F to flush the chain (no-op if doesn't exist).
74 // -N to create the chain (no-op if already exist).
75
76 runCmd(IPTABLES_PATH, "-D FORWARD -j natctrl_FORWARD");
77 runCmd(IPTABLES_PATH, "-F natctrl_FORWARD");
78 runCmd(IPTABLES_PATH, "-N natctrl_FORWARD");
79 if (runCmd(IPTABLES_PATH, "-A FORWARD -j natctrl_FORWARD"))
San Mehat9ff78fb2010-01-19 12:59:15 -080080 return -1;
JP Abgrall0031cea2012-04-17 16:38:23 -070081
82 runCmd(IPTABLES_PATH, "-t nat -D POSTROUTING -j natctrl_nat_POSTROUTING");
83 runCmd(IPTABLES_PATH, "-t nat -F natctrl_nat_POSTROUTING");
84 runCmd(IPTABLES_PATH, "-t nat -N natctrl_nat_POSTROUTING");
85 if (runCmd(IPTABLES_PATH, "-t nat -A POSTROUTING -j natctrl_nat_POSTROUTING"))
86 return -1;
87
JP Abgrall458f3182012-04-24 21:30:43 -070088 setDefaults();
JP Abgrall0031cea2012-04-17 16:38:23 -070089 return 0;
90}
91
92int NatController::setDefaults() {
93 if (runCmd(IPTABLES_PATH, "-F natctrl_FORWARD"))
94 return -1;
95 if (runCmd(IPTABLES_PATH, "-t nat -F natctrl_nat_POSTROUTING"))
San Mehat9ff78fb2010-01-19 12:59:15 -080096 return -1;
Robert Greenwaltfc97b822011-11-02 16:48:36 -070097
98 runCmd(IP_PATH, "rule flush");
Robert Greenwalt063af322011-11-18 15:32:13 -080099 runCmd(IP_PATH, "-6 rule flush");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700100 runCmd(IP_PATH, "rule add from all lookup default prio 32767");
101 runCmd(IP_PATH, "rule add from all lookup main prio 32766");
Robert Greenwalt063af322011-11-18 15:32:13 -0800102 runCmd(IP_PATH, "-6 rule add from all lookup default prio 32767");
103 runCmd(IP_PATH, "-6 rule add from all lookup main prio 32766");
104 runCmd(IP_PATH, "route flush cache");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700105
106 natCount = 0;
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600107
San Mehat9ff78fb2010-01-19 12:59:15 -0800108 return 0;
109}
110
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700111bool NatController::checkInterface(const char *iface) {
Jaime A Lopez-Sollanod14fd4f2012-01-11 16:29:28 -0800112 if (strlen(iface) > IFNAMSIZ) return false;
San Mehat9ff78fb2010-01-19 12:59:15 -0800113 return true;
114}
115
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700116// 0 1 2 3 4 5
117// nat enable intface extface addrcnt nated-ipaddr/prelength
118int NatController::enableNat(const int argc, char **argv) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800119 char cmd[255];
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700120 int i;
121 int addrCount = atoi(argv[4]);
122 int ret = 0;
123 const char *intIface = argv[2];
124 const char *extIface = argv[3];
125 int tableNumber;
San Mehat9ff78fb2010-01-19 12:59:15 -0800126
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700127 if (!checkInterface(intIface) || !checkInterface(extIface)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000128 ALOGE("Invalid interface specified");
San Mehat9ff78fb2010-01-19 12:59:15 -0800129 errno = ENODEV;
130 return -1;
131 }
132
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700133 if (argc < 5 + addrCount) {
Steve Block5ea0c052012-01-06 19:18:11 +0000134 ALOGE("Missing Argument");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700135 errno = EINVAL;
136 return -1;
137 }
138
139 tableNumber = secondaryTableCtrl->findTableNumber(extIface);
140 if (tableNumber != -1) {
Robert Greenwaltc4621772012-01-31 12:46:45 -0800141 for(i = 0; i < addrCount; i++) {
142 ret |= secondaryTableCtrl->modifyFromRule(tableNumber, ADD, argv[5+i]);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700143
Robert Greenwaltc4621772012-01-31 12:46:45 -0800144 ret |= secondaryTableCtrl->modifyLocalRoute(tableNumber, ADD, intIface, argv[5+i]);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700145 }
Robert Greenwalt063af322011-11-18 15:32:13 -0800146 runCmd(IP_PATH, "route flush cache");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700147 }
148
149 if (ret != 0 || setForwardRules(true, intIface, extIface) != 0) {
150 if (tableNumber != -1) {
151 for (i = 0; i < addrCount; i++) {
Robert Greenwaltc4621772012-01-31 12:46:45 -0800152 secondaryTableCtrl->modifyLocalRoute(tableNumber, DEL, intIface, argv[5+i]);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700153
Robert Greenwaltc4621772012-01-31 12:46:45 -0800154 secondaryTableCtrl->modifyFromRule(tableNumber, DEL, argv[5+i]);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700155 }
Robert Greenwalt063af322011-11-18 15:32:13 -0800156 runCmd(IP_PATH, "route flush cache");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700157 }
Steve Block5ea0c052012-01-06 19:18:11 +0000158 ALOGE("Error setting forward rules");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700159 errno = ENODEV;
160 return -1;
161 }
162
JP Abgrall0031cea2012-04-17 16:38:23 -0700163 /* Always make sure the drop rule is at the end */
164 snprintf(cmd, sizeof(cmd), "-D natctrl_FORWARD -j DROP");
165 runCmd(IPTABLES_PATH, cmd);
166 snprintf(cmd, sizeof(cmd), "-A natctrl_FORWARD -j DROP");
167 runCmd(IPTABLES_PATH, cmd);
168
169
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700170 natCount++;
171 // add this if we are the first added nat
172 if (natCount == 1) {
JP Abgrall0031cea2012-04-17 16:38:23 -0700173 snprintf(cmd, sizeof(cmd), "-t nat -A natctrl_nat_POSTROUTING -o %s -j MASQUERADE", extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700174 if (runCmd(IPTABLES_PATH, cmd)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000175 ALOGE("Error seting postroute rule: %s", cmd);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700176 // unwind what's been done, but don't care about success - what more could we do?
177 for (i = 0; i < addrCount; i++) {
Robert Greenwaltc4621772012-01-31 12:46:45 -0800178 secondaryTableCtrl->modifyLocalRoute(tableNumber, DEL, intIface, argv[5+i]);
179
180 secondaryTableCtrl->modifyFromRule(tableNumber, DEL, argv[5+i]);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700181 }
182 setDefaults();
183 return -1;
184 }
185 }
186
187 return 0;
188}
189
190int NatController::setForwardRules(bool add, const char *intIface, const char * extIface) {
191 char cmd[255];
192
San Mehat9ff78fb2010-01-19 12:59:15 -0800193 snprintf(cmd, sizeof(cmd),
JP Abgrall0031cea2012-04-17 16:38:23 -0700194 "-%s natctrl_FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j RETURN",
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700195 (add ? "A" : "D"),
San Mehat9ff78fb2010-01-19 12:59:15 -0800196 extIface, intIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700197 if (runCmd(IPTABLES_PATH, cmd) && add) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800198 return -1;
199 }
200
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700201 snprintf(cmd, sizeof(cmd),
JP Abgrall0031cea2012-04-17 16:38:23 -0700202 "-%s natctrl_FORWARD -i %s -o %s -m state --state INVALID -j DROP",
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700203 (add ? "A" : "D"),
204 intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700205 if (runCmd(IPTABLES_PATH, cmd) && add) {
Robert Greenwaltf7bf29c2011-11-01 22:07:28 -0700206 // bail on error, but only if adding
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700207 snprintf(cmd, sizeof(cmd),
JP Abgrall0031cea2012-04-17 16:38:23 -0700208 "-%s natctrl_FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j RETURN",
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700209 (!add ? "A" : "D"),
210 extIface, intIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700211 runCmd(IPTABLES_PATH, cmd);
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700212 return -1;
213 }
214
JP Abgrall0031cea2012-04-17 16:38:23 -0700215 snprintf(cmd, sizeof(cmd), "-%s natctrl_FORWARD -i %s -o %s -j RETURN", (add ? "A" : "D"),
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700216 intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700217 if (runCmd(IPTABLES_PATH, cmd) && add) {
Robert Greenwalt210b9772010-03-25 14:54:45 -0700218 // unwind what's been done, but don't care about success - what more could we do?
219 snprintf(cmd, sizeof(cmd),
JP Abgrall0031cea2012-04-17 16:38:23 -0700220 "-%s natctrl_FORWARD -i %s -o %s -m state --state INVALID -j DROP",
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700221 (!add ? "A" : "D"),
222 intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700223 runCmd(IPTABLES_PATH, cmd);
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700224
225 snprintf(cmd, sizeof(cmd),
JP Abgrall0031cea2012-04-17 16:38:23 -0700226 "-%s natctrl_FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j RETURN",
Robert Greenwalt210b9772010-03-25 14:54:45 -0700227 (!add ? "A" : "D"),
228 extIface, intIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700229 runCmd(IPTABLES_PATH, cmd);
San Mehat9ff78fb2010-01-19 12:59:15 -0800230 return -1;
231 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700232
San Mehat9ff78fb2010-01-19 12:59:15 -0800233 return 0;
234}
235
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700236// nat disable intface extface
237// 0 1 2 3 4 5
238// nat enable intface extface addrcnt nated-ipaddr/prelength
239int NatController::disableNat(const int argc, char **argv) {
240 char cmd[255];
241 int i;
242 int addrCount = atoi(argv[4]);
243 const char *intIface = argv[2];
244 const char *extIface = argv[3];
245 int tableNumber;
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700246
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700247 if (!checkInterface(intIface) || !checkInterface(extIface)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000248 ALOGE("Invalid interface specified");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700249 errno = ENODEV;
250 return -1;
251 }
252
253 if (argc < 5 + addrCount) {
Steve Block5ea0c052012-01-06 19:18:11 +0000254 ALOGE("Missing Argument");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700255 errno = EINVAL;
256 return -1;
257 }
258
259 setForwardRules(false, intIface, extIface);
260
261 tableNumber = secondaryTableCtrl->findTableNumber(extIface);
262 if (tableNumber != -1) {
263 for (i = 0; i < addrCount; i++) {
Robert Greenwaltc4621772012-01-31 12:46:45 -0800264 secondaryTableCtrl->modifyLocalRoute(tableNumber, DEL, intIface, argv[5+i]);
Robert Greenwalt063af322011-11-18 15:32:13 -0800265
Robert Greenwaltc4621772012-01-31 12:46:45 -0800266 secondaryTableCtrl->modifyFromRule(tableNumber, DEL, argv[5+i]);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700267 }
Robert Greenwalt063af322011-11-18 15:32:13 -0800268
269 runCmd(IP_PATH, "route flush cache");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700270 }
271
272 if (--natCount <= 0) {
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600273 // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0
274 setDefaults();
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700275 }
276 return 0;
San Mehat9ff78fb2010-01-19 12:59:15 -0800277}