blob: dbbd933b17b9a1ab15e4ac517759a6e60ea7daf4 [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>
Lorenzo Colittic2841282015-11-25 22:13:57 +090020#include <netdb.h>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080021#include <string.h>
San Mehat18737842010-01-21 09:22:43 -080022
San Mehat9d10b342010-01-18 09:51:02 -080023#include <sys/socket.h>
24#include <sys/stat.h>
San Mehat18737842010-01-21 09:22:43 -080025#include <sys/types.h>
26#include <sys/wait.h>
27
San Mehat9d10b342010-01-18 09:51:02 -080028#include <netinet/in.h>
29#include <arpa/inet.h>
30
31#define LOG_TAG "TetherController"
32#include <cutils/log.h>
Kazuhiro Ondo6b858eb2011-06-24 20:31:03 -050033#include <cutils/properties.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";
Erik Kline13fa01f2015-11-12 17:49:23 +090045static const char SEPARATOR[] = "|";
Lorenzo Colitti799625c2015-02-25 12:52:00 +090046
47bool writeToFile(const char* filename, const char* value) {
Nicolas Geoffrayafd40372015-03-16 11:58:06 +000048 int fd = open(filename, O_WRONLY);
49 if (fd < 0) {
50 ALOGE("Failed to open %s: %s", filename, strerror(errno));
51 return false;
52 }
53
54 const ssize_t len = strlen(value);
55 if (write(fd, value, len) != len) {
56 ALOGE("Failed to write %s to %s: %s", value, filename, strerror(errno));
57 close(fd);
58 return false;
59 }
60 close(fd);
61 return true;
Lorenzo Colitti799625c2015-02-25 12:52:00 +090062}
63
64bool inBpToolsMode() {
65 // In BP tools mode, do not disable IP forwarding
66 char bootmode[PROPERTY_VALUE_MAX] = {0};
67 property_get("ro.bootmode", bootmode, "unknown");
68 return !strcmp(BP_TOOLS_MODE, bootmode);
69}
70
71} // namespace
72
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070073TetherController::TetherController() {
San Mehat9d10b342010-01-18 09:51:02 -080074 mInterfaces = new InterfaceCollection();
Lorenzo Colitti667c4772014-08-26 14:13:07 -070075 mDnsNetId = 0;
San Mehat9d10b342010-01-18 09:51:02 -080076 mDnsForwarders = new NetAddressCollection();
77 mDaemonFd = -1;
78 mDaemonPid = 0;
Lorenzo Colitti799625c2015-02-25 12:52:00 +090079 if (inBpToolsMode()) {
80 enableForwarding(BP_TOOLS_MODE);
81 } else {
82 setIpFwdEnabled();
83 }
San Mehat9d10b342010-01-18 09:51:02 -080084}
85
86TetherController::~TetherController() {
87 InterfaceCollection::iterator it;
88
89 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
90 free(*it);
91 }
92 mInterfaces->clear();
93
94 mDnsForwarders->clear();
Lorenzo Colitti799625c2015-02-25 12:52:00 +090095 mForwardingRequests.clear();
San Mehat9d10b342010-01-18 09:51:02 -080096}
97
Lorenzo Colitti799625c2015-02-25 12:52:00 +090098bool TetherController::setIpFwdEnabled() {
99 bool success = true;
100 const char* value = mForwardingRequests.empty() ? "0" : "1";
101 ALOGD("Setting IP forward enable = %s", value);
102 success &= writeToFile(IPV4_FORWARDING_PROC_FILE, value);
103 success &= writeToFile(IPV6_FORWARDING_PROC_FILE, value);
104 return success;
San Mehat9d10b342010-01-18 09:51:02 -0800105}
106
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900107bool TetherController::enableForwarding(const char* requester) {
108 // Don't return an error if this requester already requested forwarding. Only return errors for
109 // things that the caller caller needs to care about, such as "couldn't write to the file to
110 // enable forwarding".
111 mForwardingRequests.insert(requester);
112 return setIpFwdEnabled();
113}
San Mehat9d10b342010-01-18 09:51:02 -0800114
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900115bool TetherController::disableForwarding(const char* requester) {
116 mForwardingRequests.erase(requester);
117 return setIpFwdEnabled();
118}
San Mehat9d10b342010-01-18 09:51:02 -0800119
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900120size_t TetherController::forwardingRequestCount() {
121 return mForwardingRequests.size();
San Mehat9d10b342010-01-18 09:51:02 -0800122}
123
Erik Klinea3ec3702016-01-05 03:52:07 +0000124#define TETHER_START_CONST_ARG 8
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800125
Erik Kline13fa01f2015-11-12 17:49:23 +0900126int TetherController::startTethering(int num_addrs, char **dhcp_ranges) {
San Mehat9d10b342010-01-18 09:51:02 -0800127 if (mDaemonPid != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000128 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -0800129 errno = EBUSY;
130 return -1;
131 }
132
Steve Block7b984e32011-12-20 16:22:42 +0000133 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800134
135 pid_t pid;
136 int pipefd[2];
137
138 if (pipe(pipefd) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000139 ALOGE("pipe failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800140 return -1;
141 }
142
143 /*
144 * TODO: Create a monitoring thread to handle and restart
145 * the daemon if it exits prematurely
146 */
147 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000148 ALOGE("fork failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800149 close(pipefd[0]);
150 close(pipefd[1]);
151 return -1;
152 }
153
154 if (!pid) {
155 close(pipefd[1]);
156 if (pipefd[0] != STDIN_FILENO) {
157 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
Steve Block5ea0c052012-01-06 19:18:11 +0000158 ALOGE("dup2 failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800159 return -1;
160 }
161 close(pipefd[0]);
162 }
San Mehat9d10b342010-01-18 09:51:02 -0800163
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800164 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700165 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
166 args[num_processed_args - 1] = NULL;
167 args[0] = (char *)"/system/bin/dnsmasq";
Peter Nilssonb756f692011-09-08 09:48:31 -0700168 args[1] = (char *)"--keep-in-foreground";
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700169 args[2] = (char *)"--no-resolv";
170 args[3] = (char *)"--no-poll";
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800171 args[4] = (char *)"--dhcp-authoritative";
Jeff Sharkey6df79da2012-04-18 21:53:35 -0700172 // TODO: pipe through metered status from ConnService
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800173 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
174 args[6] = (char *)"--pid-file";
175 args[7] = (char *)"";
San Mehat9d10b342010-01-18 09:51:02 -0800176
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800177 int nextArg = TETHER_START_CONST_ARG;
Erik Kline13fa01f2015-11-12 17:49:23 +0900178 for (int addrIndex = 0; addrIndex < num_addrs; addrIndex += 2) {
179 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h",
180 dhcp_ranges[addrIndex], dhcp_ranges[addrIndex+1]);
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700181 }
182
183 if (execv(args[0], args)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000184 ALOGE("execl failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800185 }
Steve Block5ea0c052012-01-06 19:18:11 +0000186 ALOGE("Should never get here!");
JP Abgrallce4f3792012-08-06 13:44:44 -0700187 _exit(-1);
San Mehat9d10b342010-01-18 09:51:02 -0800188 } else {
189 close(pipefd[0]);
190 mDaemonPid = pid;
191 mDaemonFd = pipefd[1];
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800192 applyDnsInterfaces();
Steve Block7b984e32011-12-20 16:22:42 +0000193 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800194 }
195
196 return 0;
197}
198
199int TetherController::stopTethering() {
200
201 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000202 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800203 return 0;
204 }
205
Steve Block7b984e32011-12-20 16:22:42 +0000206 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800207
208 kill(mDaemonPid, SIGTERM);
San Mehat18737842010-01-21 09:22:43 -0800209 waitpid(mDaemonPid, NULL, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800210 mDaemonPid = 0;
211 close(mDaemonFd);
212 mDaemonFd = -1;
Steve Block7b984e32011-12-20 16:22:42 +0000213 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800214 return 0;
215}
Matthew Xie19944102012-07-12 16:42:07 -0700216
San Mehat9d10b342010-01-18 09:51:02 -0800217bool TetherController::isTetheringStarted() {
218 return (mDaemonPid == 0 ? false : true);
219}
220
Kenny Rootcf52faf2010-02-18 09:59:55 -0800221#define MAX_CMD_SIZE 1024
222
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700223int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
San Mehat9d10b342010-01-18 09:51:02 -0800224 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800225 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800226
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700227 Fwmark fwmark;
228 fwmark.netId = netId;
229 fwmark.explicitlySelected = true;
230 fwmark.protectedFromVpn = true;
231 fwmark.permission = PERMISSION_SYSTEM;
232
Erik Kline13fa01f2015-11-12 17:49:23 +0900233 snprintf(daemonCmd, sizeof(daemonCmd), "update_dns%s0x%x", SEPARATOR, fwmark.intValue);
Kenny Rootcf52faf2010-02-18 09:59:55 -0800234 int cmdLen = strlen(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800235
236 mDnsForwarders->clear();
237 for (i = 0; i < numServers; i++) {
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700238 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800239
Lorenzo Colittic2841282015-11-25 22:13:57 +0900240 addrinfo *res, hints = { .ai_flags = AI_NUMERICHOST };
241 int ret = getaddrinfo(servers[i], NULL, &hints, &res);
242 freeaddrinfo(res);
243 if (ret) {
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();
Lorenzo Colittic2841282015-11-25 22:13:57 +0900246 errno = EINVAL;
San Mehat9d10b342010-01-18 09:51:02 -0800247 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
Erik Kline13fa01f2015-11-12 17:49:23 +0900256 strcat(daemonCmd, SEPARATOR);
San Mehat9d10b342010-01-18 09:51:02 -0800257 strcat(daemonCmd, servers[i]);
Erik Klinef90a8b92015-11-13 16:26:27 +0900258 mDnsForwarders->push_back(servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800259 }
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();
Lorenzo Colittic2841282015-11-25 22:13:57 +0900267 errno = EREMOTEIO;
San Mehat9d10b342010-01-18 09:51:02 -0800268 return -1;
269 }
270 }
271 return 0;
272}
273
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700274unsigned TetherController::getDnsNetId() {
275 return mDnsNetId;
276}
277
San Mehat9d10b342010-01-18 09:51:02 -0800278NetAddressCollection *TetherController::getDnsForwarders() {
279 return mDnsForwarders;
280}
281
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800282int TetherController::applyDnsInterfaces() {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800283 char daemonCmd[MAX_CMD_SIZE];
284
285 strcpy(daemonCmd, "update_ifaces");
286 int cmdLen = strlen(daemonCmd);
287 InterfaceCollection::iterator it;
288 bool haveInterfaces = false;
289
290 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
291 cmdLen += (strlen(*it) + 1);
292 if (cmdLen + 1 >= MAX_CMD_SIZE) {
293 ALOGD("Too many DNS ifaces listed");
294 break;
295 }
296
Erik Kline13fa01f2015-11-12 17:49:23 +0900297 strcat(daemonCmd, SEPARATOR);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800298 strcat(daemonCmd, *it);
299 haveInterfaces = true;
300 }
301
302 if ((mDaemonFd != -1) && haveInterfaces) {
303 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
304 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
305 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
306 return -1;
307 }
308 }
San Mehat9d10b342010-01-18 09:51:02 -0800309 return 0;
310}
311
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800312int TetherController::tetherInterface(const char *interface) {
313 ALOGD("tetherInterface(%s)", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -0700314 if (!isIfaceName(interface)) {
315 errno = ENOENT;
316 return -1;
317 }
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800318 mInterfaces->push_back(strdup(interface));
319
320 if (applyDnsInterfaces()) {
321 InterfaceCollection::iterator it;
322 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
323 if (!strcmp(interface, *it)) {
324 free(*it);
325 mInterfaces->erase(it);
326 break;
327 }
328 }
329 return -1;
330 } else {
331 return 0;
332 }
333}
334
San Mehat9d10b342010-01-18 09:51:02 -0800335int TetherController::untetherInterface(const char *interface) {
336 InterfaceCollection::iterator it;
337
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800338 ALOGD("untetherInterface(%s)", interface);
339
San Mehat9d10b342010-01-18 09:51:02 -0800340 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
341 if (!strcmp(interface, *it)) {
342 free(*it);
343 mInterfaces->erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800344
345 return applyDnsInterfaces();
San Mehat9d10b342010-01-18 09:51:02 -0800346 }
347 }
348 errno = ENOENT;
349 return -1;
350}
351
352InterfaceCollection *TetherController::getTetheredInterfaceList() {
353 return mInterfaces;
354}