blob: e44a29b2786f36e850199f3ea0613aaa13fa6bf8 [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
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070038const char* NatController::LOCAL_FORWARD = "natctrl_FORWARD";
39const char* NatController::LOCAL_NAT_POSTROUTING = "natctrl_nat_POSTROUTING";
40
Robert Greenwaltfc97b822011-11-02 16:48:36 -070041NatController::NatController(SecondaryTableController *ctrl) {
42 secondaryTableCtrl = ctrl;
San Mehat9ff78fb2010-01-19 12:59:15 -080043}
44
45NatController::~NatController() {
46}
47
Robert Greenwaltfc97b822011-11-02 16:48:36 -070048int NatController::runCmd(const char *path, const char *cmd) {
JP Abgrall11b4e9b2011-08-11 15:34:49 -070049 char *buffer;
50 size_t len = strnlen(cmd, 255);
51 int res;
San Mehat9ff78fb2010-01-19 12:59:15 -080052
JP Abgrall11b4e9b2011-08-11 15:34:49 -070053 if (len == 255) {
Steve Block5ea0c052012-01-06 19:18:11 +000054 ALOGE("command too long");
JP Abgrall11b4e9b2011-08-11 15:34:49 -070055 errno = E2BIG;
56 return -1;
San Mehat9ff78fb2010-01-19 12:59:15 -080057 }
San Mehat9ff78fb2010-01-19 12:59:15 -080058
Robert Greenwaltfc97b822011-11-02 16:48:36 -070059 asprintf(&buffer, "%s %s", path, cmd);
JP Abgrall9e5e0ce2011-12-14 15:20:59 -080060 res = system_nosh(buffer);
JP Abgrall0031cea2012-04-17 16:38:23 -070061 ALOGV("runCmd() buffer='%s' res=%d", buffer, res);
JP Abgrall11b4e9b2011-08-11 15:34:49 -070062 free(buffer);
63 return res;
San Mehat9ff78fb2010-01-19 12:59:15 -080064}
65
JP Abgrall0031cea2012-04-17 16:38:23 -070066int NatController::setupIptablesHooks() {
JP Abgrall458f3182012-04-24 21:30:43 -070067 setDefaults();
JP Abgrall0031cea2012-04-17 16:38:23 -070068 return 0;
69}
70
71int NatController::setDefaults() {
72 if (runCmd(IPTABLES_PATH, "-F natctrl_FORWARD"))
73 return -1;
74 if (runCmd(IPTABLES_PATH, "-t nat -F natctrl_nat_POSTROUTING"))
San Mehat9ff78fb2010-01-19 12:59:15 -080075 return -1;
Robert Greenwaltfc97b822011-11-02 16:48:36 -070076
77 runCmd(IP_PATH, "rule flush");
Robert Greenwalt063af322011-11-18 15:32:13 -080078 runCmd(IP_PATH, "-6 rule flush");
Robert Greenwaltfc97b822011-11-02 16:48:36 -070079 runCmd(IP_PATH, "rule add from all lookup default prio 32767");
80 runCmd(IP_PATH, "rule add from all lookup main prio 32766");
Robert Greenwalt063af322011-11-18 15:32:13 -080081 runCmd(IP_PATH, "-6 rule add from all lookup default prio 32767");
82 runCmd(IP_PATH, "-6 rule add from all lookup main prio 32766");
83 runCmd(IP_PATH, "route flush cache");
Robert Greenwaltfc97b822011-11-02 16:48:36 -070084
85 natCount = 0;
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -060086
San Mehat9ff78fb2010-01-19 12:59:15 -080087 return 0;
88}
89
Robert Greenwaltfc97b822011-11-02 16:48:36 -070090bool NatController::checkInterface(const char *iface) {
Jaime A Lopez-Sollanod14fd4f2012-01-11 16:29:28 -080091 if (strlen(iface) > IFNAMSIZ) return false;
San Mehat9ff78fb2010-01-19 12:59:15 -080092 return true;
93}
94
Robert Greenwaltfc97b822011-11-02 16:48:36 -070095// 0 1 2 3 4 5
96// nat enable intface extface addrcnt nated-ipaddr/prelength
97int NatController::enableNat(const int argc, char **argv) {
San Mehat9ff78fb2010-01-19 12:59:15 -080098 char cmd[255];
Robert Greenwaltfc97b822011-11-02 16:48:36 -070099 int i;
100 int addrCount = atoi(argv[4]);
101 int ret = 0;
102 const char *intIface = argv[2];
103 const char *extIface = argv[3];
104 int tableNumber;
San Mehat9ff78fb2010-01-19 12:59:15 -0800105
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700106 if (!checkInterface(intIface) || !checkInterface(extIface)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000107 ALOGE("Invalid interface specified");
San Mehat9ff78fb2010-01-19 12:59:15 -0800108 errno = ENODEV;
109 return -1;
110 }
111
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700112 if (argc < 5 + addrCount) {
Steve Block5ea0c052012-01-06 19:18:11 +0000113 ALOGE("Missing Argument");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700114 errno = EINVAL;
115 return -1;
116 }
117
118 tableNumber = secondaryTableCtrl->findTableNumber(extIface);
119 if (tableNumber != -1) {
Robert Greenwaltc4621772012-01-31 12:46:45 -0800120 for(i = 0; i < addrCount; i++) {
121 ret |= secondaryTableCtrl->modifyFromRule(tableNumber, ADD, argv[5+i]);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700122
Robert Greenwaltc4621772012-01-31 12:46:45 -0800123 ret |= secondaryTableCtrl->modifyLocalRoute(tableNumber, ADD, intIface, argv[5+i]);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700124 }
Robert Greenwalt063af322011-11-18 15:32:13 -0800125 runCmd(IP_PATH, "route flush cache");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700126 }
127
128 if (ret != 0 || setForwardRules(true, intIface, extIface) != 0) {
129 if (tableNumber != -1) {
130 for (i = 0; i < addrCount; i++) {
Robert Greenwaltc4621772012-01-31 12:46:45 -0800131 secondaryTableCtrl->modifyLocalRoute(tableNumber, DEL, intIface, argv[5+i]);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700132
Robert Greenwaltc4621772012-01-31 12:46:45 -0800133 secondaryTableCtrl->modifyFromRule(tableNumber, DEL, argv[5+i]);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700134 }
Robert Greenwalt063af322011-11-18 15:32:13 -0800135 runCmd(IP_PATH, "route flush cache");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700136 }
Steve Block5ea0c052012-01-06 19:18:11 +0000137 ALOGE("Error setting forward rules");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700138 errno = ENODEV;
139 return -1;
140 }
141
JP Abgrall0031cea2012-04-17 16:38:23 -0700142 /* Always make sure the drop rule is at the end */
143 snprintf(cmd, sizeof(cmd), "-D natctrl_FORWARD -j DROP");
144 runCmd(IPTABLES_PATH, cmd);
145 snprintf(cmd, sizeof(cmd), "-A natctrl_FORWARD -j DROP");
146 runCmd(IPTABLES_PATH, cmd);
147
148
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700149 natCount++;
150 // add this if we are the first added nat
151 if (natCount == 1) {
JP Abgrall0031cea2012-04-17 16:38:23 -0700152 snprintf(cmd, sizeof(cmd), "-t nat -A natctrl_nat_POSTROUTING -o %s -j MASQUERADE", extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700153 if (runCmd(IPTABLES_PATH, cmd)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000154 ALOGE("Error seting postroute rule: %s", cmd);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700155 // unwind what's been done, but don't care about success - what more could we do?
156 for (i = 0; i < addrCount; i++) {
Robert Greenwaltc4621772012-01-31 12:46:45 -0800157 secondaryTableCtrl->modifyLocalRoute(tableNumber, DEL, intIface, argv[5+i]);
158
159 secondaryTableCtrl->modifyFromRule(tableNumber, DEL, argv[5+i]);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700160 }
161 setDefaults();
162 return -1;
163 }
164 }
165
166 return 0;
167}
168
169int NatController::setForwardRules(bool add, const char *intIface, const char * extIface) {
170 char cmd[255];
171
San Mehat9ff78fb2010-01-19 12:59:15 -0800172 snprintf(cmd, sizeof(cmd),
JP Abgrall0031cea2012-04-17 16:38:23 -0700173 "-%s natctrl_FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j RETURN",
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700174 (add ? "A" : "D"),
San Mehat9ff78fb2010-01-19 12:59:15 -0800175 extIface, intIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700176 if (runCmd(IPTABLES_PATH, cmd) && add) {
San Mehat9ff78fb2010-01-19 12:59:15 -0800177 return -1;
178 }
179
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700180 snprintf(cmd, sizeof(cmd),
JP Abgrall0031cea2012-04-17 16:38:23 -0700181 "-%s natctrl_FORWARD -i %s -o %s -m state --state INVALID -j DROP",
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700182 (add ? "A" : "D"),
183 intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700184 if (runCmd(IPTABLES_PATH, cmd) && add) {
Robert Greenwaltf7bf29c2011-11-01 22:07:28 -0700185 // bail on error, but only if adding
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700186 snprintf(cmd, sizeof(cmd),
JP Abgrall0031cea2012-04-17 16:38:23 -0700187 "-%s natctrl_FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j RETURN",
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700188 (!add ? "A" : "D"),
189 extIface, intIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700190 runCmd(IPTABLES_PATH, cmd);
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700191 return -1;
192 }
193
JP Abgrall0031cea2012-04-17 16:38:23 -0700194 snprintf(cmd, sizeof(cmd), "-%s natctrl_FORWARD -i %s -o %s -j RETURN", (add ? "A" : "D"),
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700195 intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700196 if (runCmd(IPTABLES_PATH, cmd) && add) {
Robert Greenwalt210b9772010-03-25 14:54:45 -0700197 // unwind what's been done, but don't care about success - what more could we do?
198 snprintf(cmd, sizeof(cmd),
JP Abgrall0031cea2012-04-17 16:38:23 -0700199 "-%s natctrl_FORWARD -i %s -o %s -m state --state INVALID -j DROP",
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700200 (!add ? "A" : "D"),
201 intIface, extIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700202 runCmd(IPTABLES_PATH, cmd);
Robert Greenwaltddb9f6e2011-08-02 13:00:11 -0700203
204 snprintf(cmd, sizeof(cmd),
JP Abgrall0031cea2012-04-17 16:38:23 -0700205 "-%s natctrl_FORWARD -i %s -o %s -m state --state ESTABLISHED,RELATED -j RETURN",
Robert Greenwalt210b9772010-03-25 14:54:45 -0700206 (!add ? "A" : "D"),
207 extIface, intIface);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700208 runCmd(IPTABLES_PATH, cmd);
San Mehat9ff78fb2010-01-19 12:59:15 -0800209 return -1;
210 }
JP Abgrall0031cea2012-04-17 16:38:23 -0700211
San Mehat9ff78fb2010-01-19 12:59:15 -0800212 return 0;
213}
214
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700215// nat disable intface extface
216// 0 1 2 3 4 5
217// nat enable intface extface addrcnt nated-ipaddr/prelength
218int NatController::disableNat(const int argc, char **argv) {
219 char cmd[255];
220 int i;
221 int addrCount = atoi(argv[4]);
222 const char *intIface = argv[2];
223 const char *extIface = argv[3];
224 int tableNumber;
Robert Greenwalt1caafe62010-03-24 15:43:00 -0700225
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700226 if (!checkInterface(intIface) || !checkInterface(extIface)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000227 ALOGE("Invalid interface specified");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700228 errno = ENODEV;
229 return -1;
230 }
231
232 if (argc < 5 + addrCount) {
Steve Block5ea0c052012-01-06 19:18:11 +0000233 ALOGE("Missing Argument");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700234 errno = EINVAL;
235 return -1;
236 }
237
238 setForwardRules(false, intIface, extIface);
239
240 tableNumber = secondaryTableCtrl->findTableNumber(extIface);
241 if (tableNumber != -1) {
242 for (i = 0; i < addrCount; i++) {
Robert Greenwaltc4621772012-01-31 12:46:45 -0800243 secondaryTableCtrl->modifyLocalRoute(tableNumber, DEL, intIface, argv[5+i]);
Robert Greenwalt063af322011-11-18 15:32:13 -0800244
Robert Greenwaltc4621772012-01-31 12:46:45 -0800245 secondaryTableCtrl->modifyFromRule(tableNumber, DEL, argv[5+i]);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700246 }
Robert Greenwalt063af322011-11-18 15:32:13 -0800247
248 runCmd(IP_PATH, "route flush cache");
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700249 }
250
251 if (--natCount <= 0) {
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -0600252 // handle decrement to 0 case (do reset to defaults) and erroneous dec below 0
253 setDefaults();
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700254 }
255 return 0;
San Mehat9ff78fb2010-01-19 12:59:15 -0800256}