blob: 19d9aa6e0ae0421639d3010a69a3d42d8ec43eae [file] [log] [blame]
San Mehat9d10b342010-01-18 09:51:02 -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>
San Mehat18737842010-01-21 09:22:43 -080019#include <fcntl.h>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080020#include <string.h>
San Mehat18737842010-01-21 09:22:43 -080021
San Mehat9d10b342010-01-18 09:51:02 -080022#include <sys/socket.h>
23#include <sys/stat.h>
San Mehat18737842010-01-21 09:22:43 -080024#include <sys/types.h>
25#include <sys/wait.h>
26
San Mehat9d10b342010-01-18 09:51:02 -080027#include <netinet/in.h>
28#include <arpa/inet.h>
29
30#define LOG_TAG "TetherController"
31#include <cutils/log.h>
Kazuhiro Ondo6b858eb2011-06-24 20:31:03 -050032#include <cutils/properties.h>
San Mehat9d10b342010-01-18 09:51:02 -080033
Lorenzo Colitti667c4772014-08-26 14:13:07 -070034#include "Fwmark.h"
JP Abgrall69261cb2014-06-19 18:35:24 -070035#include "NetdConstants.h"
Lorenzo Colitti667c4772014-08-26 14:13:07 -070036#include "Permission.h"
San Mehat9d10b342010-01-18 09:51:02 -080037#include "TetherController.h"
38
Lorenzo Colitti799625c2015-02-25 12:52:00 +090039namespace {
40
41static const char BP_TOOLS_MODE[] = "bp-tools";
42static const char IPV4_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv4/ip_forward";
43static const char IPV6_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv6/conf/all/forwarding";
Erik Kline13fa01f2015-11-12 17:49:23 +090044static const char SEPARATOR[] = "|";
Lorenzo Colitti799625c2015-02-25 12:52:00 +090045
46bool writeToFile(const char* filename, const char* value) {
Nicolas Geoffrayafd40372015-03-16 11:58:06 +000047 int fd = open(filename, O_WRONLY);
48 if (fd < 0) {
49 ALOGE("Failed to open %s: %s", filename, strerror(errno));
50 return false;
51 }
52
53 const ssize_t len = strlen(value);
54 if (write(fd, value, len) != len) {
55 ALOGE("Failed to write %s to %s: %s", value, filename, strerror(errno));
56 close(fd);
57 return false;
58 }
59 close(fd);
60 return true;
Lorenzo Colitti799625c2015-02-25 12:52:00 +090061}
62
63bool inBpToolsMode() {
64 // In BP tools mode, do not disable IP forwarding
65 char bootmode[PROPERTY_VALUE_MAX] = {0};
66 property_get("ro.bootmode", bootmode, "unknown");
67 return !strcmp(BP_TOOLS_MODE, bootmode);
68}
69
70} // namespace
71
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070072TetherController::TetherController() {
San Mehat9d10b342010-01-18 09:51:02 -080073 mInterfaces = new InterfaceCollection();
Lorenzo Colitti667c4772014-08-26 14:13:07 -070074 mDnsNetId = 0;
San Mehat9d10b342010-01-18 09:51:02 -080075 mDnsForwarders = new NetAddressCollection();
76 mDaemonFd = -1;
77 mDaemonPid = 0;
Lorenzo Colitti799625c2015-02-25 12:52:00 +090078 if (inBpToolsMode()) {
79 enableForwarding(BP_TOOLS_MODE);
80 } else {
81 setIpFwdEnabled();
82 }
San Mehat9d10b342010-01-18 09:51:02 -080083}
84
85TetherController::~TetherController() {
86 InterfaceCollection::iterator it;
87
88 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
89 free(*it);
90 }
91 mInterfaces->clear();
92
93 mDnsForwarders->clear();
Lorenzo Colitti799625c2015-02-25 12:52:00 +090094 mForwardingRequests.clear();
San Mehat9d10b342010-01-18 09:51:02 -080095}
96
Lorenzo Colitti799625c2015-02-25 12:52:00 +090097bool TetherController::setIpFwdEnabled() {
98 bool success = true;
99 const char* value = mForwardingRequests.empty() ? "0" : "1";
100 ALOGD("Setting IP forward enable = %s", value);
101 success &= writeToFile(IPV4_FORWARDING_PROC_FILE, value);
102 success &= writeToFile(IPV6_FORWARDING_PROC_FILE, value);
103 return success;
San Mehat9d10b342010-01-18 09:51:02 -0800104}
105
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900106bool TetherController::enableForwarding(const char* requester) {
107 // Don't return an error if this requester already requested forwarding. Only return errors for
108 // things that the caller caller needs to care about, such as "couldn't write to the file to
109 // enable forwarding".
110 mForwardingRequests.insert(requester);
111 return setIpFwdEnabled();
112}
San Mehat9d10b342010-01-18 09:51:02 -0800113
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900114bool TetherController::disableForwarding(const char* requester) {
115 mForwardingRequests.erase(requester);
116 return setIpFwdEnabled();
117}
San Mehat9d10b342010-01-18 09:51:02 -0800118
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900119size_t TetherController::forwardingRequestCount() {
120 return mForwardingRequests.size();
San Mehat9d10b342010-01-18 09:51:02 -0800121}
122
Erik Klinea3ec3702016-01-05 03:52:07 +0000123#define TETHER_START_CONST_ARG 8
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800124
Erik Kline13fa01f2015-11-12 17:49:23 +0900125int TetherController::startTethering(int num_addrs, char **dhcp_ranges) {
San Mehat9d10b342010-01-18 09:51:02 -0800126 if (mDaemonPid != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000127 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -0800128 errno = EBUSY;
129 return -1;
130 }
131
Steve Block7b984e32011-12-20 16:22:42 +0000132 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800133
134 pid_t pid;
135 int pipefd[2];
136
137 if (pipe(pipefd) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000138 ALOGE("pipe failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800139 return -1;
140 }
141
142 /*
143 * TODO: Create a monitoring thread to handle and restart
144 * the daemon if it exits prematurely
145 */
146 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000147 ALOGE("fork failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800148 close(pipefd[0]);
149 close(pipefd[1]);
150 return -1;
151 }
152
153 if (!pid) {
154 close(pipefd[1]);
155 if (pipefd[0] != STDIN_FILENO) {
156 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
Steve Block5ea0c052012-01-06 19:18:11 +0000157 ALOGE("dup2 failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800158 return -1;
159 }
160 close(pipefd[0]);
161 }
San Mehat9d10b342010-01-18 09:51:02 -0800162
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800163 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700164 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
165 args[num_processed_args - 1] = NULL;
166 args[0] = (char *)"/system/bin/dnsmasq";
Peter Nilssonb756f692011-09-08 09:48:31 -0700167 args[1] = (char *)"--keep-in-foreground";
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700168 args[2] = (char *)"--no-resolv";
169 args[3] = (char *)"--no-poll";
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800170 args[4] = (char *)"--dhcp-authoritative";
Jeff Sharkey6df79da2012-04-18 21:53:35 -0700171 // TODO: pipe through metered status from ConnService
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800172 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
173 args[6] = (char *)"--pid-file";
174 args[7] = (char *)"";
San Mehat9d10b342010-01-18 09:51:02 -0800175
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800176 int nextArg = TETHER_START_CONST_ARG;
Erik Kline13fa01f2015-11-12 17:49:23 +0900177 for (int addrIndex = 0; addrIndex < num_addrs; addrIndex += 2) {
178 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h",
179 dhcp_ranges[addrIndex], dhcp_ranges[addrIndex+1]);
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700180 }
181
182 if (execv(args[0], args)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000183 ALOGE("execl failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800184 }
Steve Block5ea0c052012-01-06 19:18:11 +0000185 ALOGE("Should never get here!");
JP Abgrallce4f3792012-08-06 13:44:44 -0700186 _exit(-1);
San Mehat9d10b342010-01-18 09:51:02 -0800187 } else {
188 close(pipefd[0]);
189 mDaemonPid = pid;
190 mDaemonFd = pipefd[1];
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800191 applyDnsInterfaces();
Steve Block7b984e32011-12-20 16:22:42 +0000192 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800193 }
194
195 return 0;
196}
197
198int TetherController::stopTethering() {
199
200 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000201 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800202 return 0;
203 }
204
Steve Block7b984e32011-12-20 16:22:42 +0000205 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800206
207 kill(mDaemonPid, SIGTERM);
San Mehat18737842010-01-21 09:22:43 -0800208 waitpid(mDaemonPid, NULL, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800209 mDaemonPid = 0;
210 close(mDaemonFd);
211 mDaemonFd = -1;
Steve Block7b984e32011-12-20 16:22:42 +0000212 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800213 return 0;
214}
Matthew Xie19944102012-07-12 16:42:07 -0700215
San Mehat9d10b342010-01-18 09:51:02 -0800216bool TetherController::isTetheringStarted() {
217 return (mDaemonPid == 0 ? false : true);
218}
219
Kenny Rootcf52faf2010-02-18 09:59:55 -0800220#define MAX_CMD_SIZE 1024
221
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700222int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
San Mehat9d10b342010-01-18 09:51:02 -0800223 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800224 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800225
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700226 Fwmark fwmark;
227 fwmark.netId = netId;
228 fwmark.explicitlySelected = true;
229 fwmark.protectedFromVpn = true;
230 fwmark.permission = PERMISSION_SYSTEM;
231
Erik Kline13fa01f2015-11-12 17:49:23 +0900232 snprintf(daemonCmd, sizeof(daemonCmd), "update_dns%s0x%x", SEPARATOR, fwmark.intValue);
Kenny Rootcf52faf2010-02-18 09:59:55 -0800233 int cmdLen = strlen(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800234
235 mDnsForwarders->clear();
236 for (i = 0; i < numServers; i++) {
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700237 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800238
Erik Klinef90a8b92015-11-13 16:26:27 +0900239 struct in_addr v4dns;
240 struct in6_addr v6dns;
San Mehat9d10b342010-01-18 09:51:02 -0800241
Erik Klinef90a8b92015-11-13 16:26:27 +0900242 if (!inet_aton(servers[i], &v4dns) &&
243 (inet_pton(AF_INET6, servers[i], &v6dns) != 1)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000244 ALOGE("Failed to parse DNS server '%s'", servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800245 mDnsForwarders->clear();
246 return -1;
247 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800248
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700249 cmdLen += (strlen(servers[i]) + 1);
250 if (cmdLen + 1 >= MAX_CMD_SIZE) {
Steve Block7b984e32011-12-20 16:22:42 +0000251 ALOGD("Too many DNS servers listed");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800252 break;
253 }
254
Erik Kline13fa01f2015-11-12 17:49:23 +0900255 strcat(daemonCmd, SEPARATOR);
San Mehat9d10b342010-01-18 09:51:02 -0800256 strcat(daemonCmd, servers[i]);
Erik Klinef90a8b92015-11-13 16:26:27 +0900257 mDnsForwarders->push_back(servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800258 }
259
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700260 mDnsNetId = netId;
San Mehat9d10b342010-01-18 09:51:02 -0800261 if (mDaemonFd != -1) {
Steve Block7b984e32011-12-20 16:22:42 +0000262 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800263 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000264 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800265 mDnsForwarders->clear();
266 return -1;
267 }
268 }
269 return 0;
270}
271
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700272unsigned TetherController::getDnsNetId() {
273 return mDnsNetId;
274}
275
San Mehat9d10b342010-01-18 09:51:02 -0800276NetAddressCollection *TetherController::getDnsForwarders() {
277 return mDnsForwarders;
278}
279
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800280int TetherController::applyDnsInterfaces() {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800281 char daemonCmd[MAX_CMD_SIZE];
282
283 strcpy(daemonCmd, "update_ifaces");
284 int cmdLen = strlen(daemonCmd);
285 InterfaceCollection::iterator it;
286 bool haveInterfaces = false;
287
288 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
289 cmdLen += (strlen(*it) + 1);
290 if (cmdLen + 1 >= MAX_CMD_SIZE) {
291 ALOGD("Too many DNS ifaces listed");
292 break;
293 }
294
Erik Kline13fa01f2015-11-12 17:49:23 +0900295 strcat(daemonCmd, SEPARATOR);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800296 strcat(daemonCmd, *it);
297 haveInterfaces = true;
298 }
299
300 if ((mDaemonFd != -1) && haveInterfaces) {
301 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
302 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
303 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
304 return -1;
305 }
306 }
San Mehat9d10b342010-01-18 09:51:02 -0800307 return 0;
308}
309
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800310int TetherController::tetherInterface(const char *interface) {
311 ALOGD("tetherInterface(%s)", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -0700312 if (!isIfaceName(interface)) {
313 errno = ENOENT;
314 return -1;
315 }
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800316 mInterfaces->push_back(strdup(interface));
317
318 if (applyDnsInterfaces()) {
319 InterfaceCollection::iterator it;
320 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
321 if (!strcmp(interface, *it)) {
322 free(*it);
323 mInterfaces->erase(it);
324 break;
325 }
326 }
327 return -1;
328 } else {
329 return 0;
330 }
331}
332
San Mehat9d10b342010-01-18 09:51:02 -0800333int TetherController::untetherInterface(const char *interface) {
334 InterfaceCollection::iterator it;
335
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800336 ALOGD("untetherInterface(%s)", interface);
337
San Mehat9d10b342010-01-18 09:51:02 -0800338 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
339 if (!strcmp(interface, *it)) {
340 free(*it);
341 mInterfaces->erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800342
343 return applyDnsInterfaces();
San Mehat9d10b342010-01-18 09:51:02 -0800344 }
345 }
346 errno = ENOENT;
347 return -1;
348}
349
350InterfaceCollection *TetherController::getTetheredInterfaceList() {
351 return mInterfaces;
352}