blob: 88baa31e23c69d389357b4036c763bb8a2e381cf [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";
44
45bool writeToFile(const char* filename, const char* value) {
Nicolas Geoffrayafd40372015-03-16 11:58:06 +000046 int fd = open(filename, O_WRONLY);
47 if (fd < 0) {
48 ALOGE("Failed to open %s: %s", filename, strerror(errno));
49 return false;
50 }
51
52 const ssize_t len = strlen(value);
53 if (write(fd, value, len) != len) {
54 ALOGE("Failed to write %s to %s: %s", value, filename, strerror(errno));
55 close(fd);
56 return false;
57 }
58 close(fd);
59 return true;
Lorenzo Colitti799625c2015-02-25 12:52:00 +090060}
61
62bool inBpToolsMode() {
63 // In BP tools mode, do not disable IP forwarding
64 char bootmode[PROPERTY_VALUE_MAX] = {0};
65 property_get("ro.bootmode", bootmode, "unknown");
66 return !strcmp(BP_TOOLS_MODE, bootmode);
67}
68
69} // namespace
70
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070071TetherController::TetherController() {
San Mehat9d10b342010-01-18 09:51:02 -080072 mInterfaces = new InterfaceCollection();
Lorenzo Colitti667c4772014-08-26 14:13:07 -070073 mDnsNetId = 0;
San Mehat9d10b342010-01-18 09:51:02 -080074 mDnsForwarders = new NetAddressCollection();
75 mDaemonFd = -1;
76 mDaemonPid = 0;
Lorenzo Colitti799625c2015-02-25 12:52:00 +090077 if (inBpToolsMode()) {
78 enableForwarding(BP_TOOLS_MODE);
79 } else {
80 setIpFwdEnabled();
81 }
San Mehat9d10b342010-01-18 09:51:02 -080082}
83
84TetherController::~TetherController() {
85 InterfaceCollection::iterator it;
86
87 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
88 free(*it);
89 }
90 mInterfaces->clear();
91
92 mDnsForwarders->clear();
Lorenzo Colitti799625c2015-02-25 12:52:00 +090093 mForwardingRequests.clear();
San Mehat9d10b342010-01-18 09:51:02 -080094}
95
Lorenzo Colitti799625c2015-02-25 12:52:00 +090096bool TetherController::setIpFwdEnabled() {
97 bool success = true;
98 const char* value = mForwardingRequests.empty() ? "0" : "1";
99 ALOGD("Setting IP forward enable = %s", value);
100 success &= writeToFile(IPV4_FORWARDING_PROC_FILE, value);
101 success &= writeToFile(IPV6_FORWARDING_PROC_FILE, value);
102 return success;
San Mehat9d10b342010-01-18 09:51:02 -0800103}
104
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900105bool TetherController::enableForwarding(const char* requester) {
106 // Don't return an error if this requester already requested forwarding. Only return errors for
107 // things that the caller caller needs to care about, such as "couldn't write to the file to
108 // enable forwarding".
109 mForwardingRequests.insert(requester);
110 return setIpFwdEnabled();
111}
San Mehat9d10b342010-01-18 09:51:02 -0800112
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900113bool TetherController::disableForwarding(const char* requester) {
114 mForwardingRequests.erase(requester);
115 return setIpFwdEnabled();
116}
San Mehat9d10b342010-01-18 09:51:02 -0800117
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900118size_t TetherController::forwardingRequestCount() {
119 return mForwardingRequests.size();
San Mehat9d10b342010-01-18 09:51:02 -0800120}
121
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800122#define TETHER_START_CONST_ARG 8
123
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700124int TetherController::startTethering(int num_addrs, struct in_addr* addrs) {
San Mehat9d10b342010-01-18 09:51:02 -0800125 if (mDaemonPid != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000126 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -0800127 errno = EBUSY;
128 return -1;
129 }
130
Steve Block7b984e32011-12-20 16:22:42 +0000131 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800132
133 pid_t pid;
134 int pipefd[2];
135
136 if (pipe(pipefd) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000137 ALOGE("pipe failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800138 return -1;
139 }
140
141 /*
142 * TODO: Create a monitoring thread to handle and restart
143 * the daemon if it exits prematurely
144 */
145 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000146 ALOGE("fork failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800147 close(pipefd[0]);
148 close(pipefd[1]);
149 return -1;
150 }
151
152 if (!pid) {
153 close(pipefd[1]);
154 if (pipefd[0] != STDIN_FILENO) {
155 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
Steve Block5ea0c052012-01-06 19:18:11 +0000156 ALOGE("dup2 failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800157 return -1;
158 }
159 close(pipefd[0]);
160 }
San Mehat9d10b342010-01-18 09:51:02 -0800161
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800162 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700163 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
164 args[num_processed_args - 1] = NULL;
165 args[0] = (char *)"/system/bin/dnsmasq";
Peter Nilssonb756f692011-09-08 09:48:31 -0700166 args[1] = (char *)"--keep-in-foreground";
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700167 args[2] = (char *)"--no-resolv";
168 args[3] = (char *)"--no-poll";
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800169 args[4] = (char *)"--dhcp-authoritative";
Jeff Sharkey6df79da2012-04-18 21:53:35 -0700170 // TODO: pipe through metered status from ConnService
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800171 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
172 args[6] = (char *)"--pid-file";
173 args[7] = (char *)"";
San Mehat9d10b342010-01-18 09:51:02 -0800174
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800175 int nextArg = TETHER_START_CONST_ARG;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700176 for (int addrIndex=0; addrIndex < num_addrs;) {
177 char *start = strdup(inet_ntoa(addrs[addrIndex++]));
178 char *end = strdup(inet_ntoa(addrs[addrIndex++]));
179 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end);
Jesper Hanssona9d791f2012-04-27 13:54:27 +0200180 free(start);
181 free(end);
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700182 }
183
184 if (execv(args[0], args)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000185 ALOGE("execl failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800186 }
Steve Block5ea0c052012-01-06 19:18:11 +0000187 ALOGE("Should never get here!");
JP Abgrallce4f3792012-08-06 13:44:44 -0700188 _exit(-1);
San Mehat9d10b342010-01-18 09:51:02 -0800189 } else {
190 close(pipefd[0]);
191 mDaemonPid = pid;
192 mDaemonFd = pipefd[1];
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800193 applyDnsInterfaces();
Steve Block7b984e32011-12-20 16:22:42 +0000194 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800195 }
196
197 return 0;
198}
199
200int TetherController::stopTethering() {
201
202 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000203 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800204 return 0;
205 }
206
Steve Block7b984e32011-12-20 16:22:42 +0000207 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800208
209 kill(mDaemonPid, SIGTERM);
San Mehat18737842010-01-21 09:22:43 -0800210 waitpid(mDaemonPid, NULL, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800211 mDaemonPid = 0;
212 close(mDaemonFd);
213 mDaemonFd = -1;
Steve Block7b984e32011-12-20 16:22:42 +0000214 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800215 return 0;
216}
Matthew Xie19944102012-07-12 16:42:07 -0700217
San Mehat9d10b342010-01-18 09:51:02 -0800218bool TetherController::isTetheringStarted() {
219 return (mDaemonPid == 0 ? false : true);
220}
221
Kenny Rootcf52faf2010-02-18 09:59:55 -0800222#define MAX_CMD_SIZE 1024
223
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700224int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
San Mehat9d10b342010-01-18 09:51:02 -0800225 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800226 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800227
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700228 Fwmark fwmark;
229 fwmark.netId = netId;
230 fwmark.explicitlySelected = true;
231 fwmark.protectedFromVpn = true;
232 fwmark.permission = PERMISSION_SYSTEM;
233
234 snprintf(daemonCmd, sizeof(daemonCmd), "update_dns:0x%x", fwmark.intValue);
Kenny Rootcf52faf2010-02-18 09:59:55 -0800235 int cmdLen = strlen(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800236
237 mDnsForwarders->clear();
238 for (i = 0; i < numServers; i++) {
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700239 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800240
241 struct in_addr a;
242
243 if (!inet_aton(servers[i], &a)) {
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
San Mehat9d10b342010-01-18 09:51:02 -0800255 strcat(daemonCmd, ":");
256 strcat(daemonCmd, servers[i]);
257 mDnsForwarders->push_back(a);
258 }
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
295 strcat(daemonCmd, ":");
296 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}