blob: 6bb9fde7203156215cb0f76f9f48907e5914e263 [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>
Nicolas Geoffray4a0ab5f2015-03-16 10:28:37 +000033#include <utils/file.h>
San Mehat9d10b342010-01-18 09:51:02 -080034
Lorenzo Colitti667c4772014-08-26 14:13:07 -070035#include "Fwmark.h"
JP Abgrall69261cb2014-06-19 18:35:24 -070036#include "NetdConstants.h"
Lorenzo Colitti667c4772014-08-26 14:13:07 -070037#include "Permission.h"
San Mehat9d10b342010-01-18 09:51:02 -080038#include "TetherController.h"
39
Lorenzo Colitti799625c2015-02-25 12:52:00 +090040namespace {
41
42static const char BP_TOOLS_MODE[] = "bp-tools";
43static const char IPV4_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv4/ip_forward";
44static const char IPV6_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv6/conf/all/forwarding";
45
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
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800123#define TETHER_START_CONST_ARG 8
124
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700125int TetherController::startTethering(int num_addrs, struct in_addr* addrs) {
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;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700177 for (int addrIndex=0; addrIndex < num_addrs;) {
178 char *start = strdup(inet_ntoa(addrs[addrIndex++]));
179 char *end = strdup(inet_ntoa(addrs[addrIndex++]));
180 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end);
Jesper Hanssona9d791f2012-04-27 13:54:27 +0200181 free(start);
182 free(end);
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700183 }
184
185 if (execv(args[0], args)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000186 ALOGE("execl failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800187 }
Steve Block5ea0c052012-01-06 19:18:11 +0000188 ALOGE("Should never get here!");
JP Abgrallce4f3792012-08-06 13:44:44 -0700189 _exit(-1);
San Mehat9d10b342010-01-18 09:51:02 -0800190 } else {
191 close(pipefd[0]);
192 mDaemonPid = pid;
193 mDaemonFd = pipefd[1];
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800194 applyDnsInterfaces();
Steve Block7b984e32011-12-20 16:22:42 +0000195 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800196 }
197
198 return 0;
199}
200
201int TetherController::stopTethering() {
202
203 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000204 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800205 return 0;
206 }
207
Steve Block7b984e32011-12-20 16:22:42 +0000208 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800209
210 kill(mDaemonPid, SIGTERM);
San Mehat18737842010-01-21 09:22:43 -0800211 waitpid(mDaemonPid, NULL, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800212 mDaemonPid = 0;
213 close(mDaemonFd);
214 mDaemonFd = -1;
Steve Block7b984e32011-12-20 16:22:42 +0000215 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800216 return 0;
217}
Matthew Xie19944102012-07-12 16:42:07 -0700218
San Mehat9d10b342010-01-18 09:51:02 -0800219bool TetherController::isTetheringStarted() {
220 return (mDaemonPid == 0 ? false : true);
221}
222
Kenny Rootcf52faf2010-02-18 09:59:55 -0800223#define MAX_CMD_SIZE 1024
224
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700225int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
San Mehat9d10b342010-01-18 09:51:02 -0800226 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800227 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800228
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700229 Fwmark fwmark;
230 fwmark.netId = netId;
231 fwmark.explicitlySelected = true;
232 fwmark.protectedFromVpn = true;
233 fwmark.permission = PERMISSION_SYSTEM;
234
235 snprintf(daemonCmd, sizeof(daemonCmd), "update_dns:0x%x", fwmark.intValue);
Kenny Rootcf52faf2010-02-18 09:59:55 -0800236 int cmdLen = strlen(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800237
238 mDnsForwarders->clear();
239 for (i = 0; i < numServers; i++) {
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700240 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800241
242 struct in_addr a;
243
244 if (!inet_aton(servers[i], &a)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000245 ALOGE("Failed to parse DNS server '%s'", servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800246 mDnsForwarders->clear();
247 return -1;
248 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800249
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700250 cmdLen += (strlen(servers[i]) + 1);
251 if (cmdLen + 1 >= MAX_CMD_SIZE) {
Steve Block7b984e32011-12-20 16:22:42 +0000252 ALOGD("Too many DNS servers listed");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800253 break;
254 }
255
San Mehat9d10b342010-01-18 09:51:02 -0800256 strcat(daemonCmd, ":");
257 strcat(daemonCmd, servers[i]);
258 mDnsForwarders->push_back(a);
259 }
260
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700261 mDnsNetId = netId;
San Mehat9d10b342010-01-18 09:51:02 -0800262 if (mDaemonFd != -1) {
Steve Block7b984e32011-12-20 16:22:42 +0000263 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800264 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000265 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800266 mDnsForwarders->clear();
267 return -1;
268 }
269 }
270 return 0;
271}
272
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700273unsigned TetherController::getDnsNetId() {
274 return mDnsNetId;
275}
276
San Mehat9d10b342010-01-18 09:51:02 -0800277NetAddressCollection *TetherController::getDnsForwarders() {
278 return mDnsForwarders;
279}
280
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800281int TetherController::applyDnsInterfaces() {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800282 char daemonCmd[MAX_CMD_SIZE];
283
284 strcpy(daemonCmd, "update_ifaces");
285 int cmdLen = strlen(daemonCmd);
286 InterfaceCollection::iterator it;
287 bool haveInterfaces = false;
288
289 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
290 cmdLen += (strlen(*it) + 1);
291 if (cmdLen + 1 >= MAX_CMD_SIZE) {
292 ALOGD("Too many DNS ifaces listed");
293 break;
294 }
295
296 strcat(daemonCmd, ":");
297 strcat(daemonCmd, *it);
298 haveInterfaces = true;
299 }
300
301 if ((mDaemonFd != -1) && haveInterfaces) {
302 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
303 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
304 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
305 return -1;
306 }
307 }
San Mehat9d10b342010-01-18 09:51:02 -0800308 return 0;
309}
310
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800311int TetherController::tetherInterface(const char *interface) {
312 ALOGD("tetherInterface(%s)", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -0700313 if (!isIfaceName(interface)) {
314 errno = ENOENT;
315 return -1;
316 }
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800317 mInterfaces->push_back(strdup(interface));
318
319 if (applyDnsInterfaces()) {
320 InterfaceCollection::iterator it;
321 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
322 if (!strcmp(interface, *it)) {
323 free(*it);
324 mInterfaces->erase(it);
325 break;
326 }
327 }
328 return -1;
329 } else {
330 return 0;
331 }
332}
333
San Mehat9d10b342010-01-18 09:51:02 -0800334int TetherController::untetherInterface(const char *interface) {
335 InterfaceCollection::iterator it;
336
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800337 ALOGD("untetherInterface(%s)", interface);
338
San Mehat9d10b342010-01-18 09:51:02 -0800339 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
340 if (!strcmp(interface, *it)) {
341 free(*it);
342 mInterfaces->erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800343
344 return applyDnsInterfaces();
San Mehat9d10b342010-01-18 09:51:02 -0800345 }
346 }
347 errno = ENOENT;
348 return -1;
349}
350
351InterfaceCollection *TetherController::getTetheredInterfaceList() {
352 return mInterfaces;
353}