blob: dad03b47578221c05f6e5ffacae177193f03b98c [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) {
46 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;
60}
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
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);
181 }
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
233 snprintf(daemonCmd, sizeof(daemonCmd), "update_dns:0x%x", 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
240 struct in_addr a;
241
242 if (!inet_aton(servers[i], &a)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000243 ALOGE("Failed to parse DNS server '%s'", servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800244 mDnsForwarders->clear();
245 return -1;
246 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800247
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700248 cmdLen += (strlen(servers[i]) + 1);
249 if (cmdLen + 1 >= MAX_CMD_SIZE) {
Steve Block7b984e32011-12-20 16:22:42 +0000250 ALOGD("Too many DNS servers listed");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800251 break;
252 }
253
San Mehat9d10b342010-01-18 09:51:02 -0800254 strcat(daemonCmd, ":");
255 strcat(daemonCmd, servers[i]);
256 mDnsForwarders->push_back(a);
257 }
258
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700259 mDnsNetId = netId;
San Mehat9d10b342010-01-18 09:51:02 -0800260 if (mDaemonFd != -1) {
Steve Block7b984e32011-12-20 16:22:42 +0000261 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800262 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000263 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800264 mDnsForwarders->clear();
265 return -1;
266 }
267 }
268 return 0;
269}
270
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700271unsigned TetherController::getDnsNetId() {
272 return mDnsNetId;
273}
274
San Mehat9d10b342010-01-18 09:51:02 -0800275NetAddressCollection *TetherController::getDnsForwarders() {
276 return mDnsForwarders;
277}
278
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800279int TetherController::applyDnsInterfaces() {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800280 char daemonCmd[MAX_CMD_SIZE];
281
282 strcpy(daemonCmd, "update_ifaces");
283 int cmdLen = strlen(daemonCmd);
284 InterfaceCollection::iterator it;
285 bool haveInterfaces = false;
286
287 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
288 cmdLen += (strlen(*it) + 1);
289 if (cmdLen + 1 >= MAX_CMD_SIZE) {
290 ALOGD("Too many DNS ifaces listed");
291 break;
292 }
293
294 strcat(daemonCmd, ":");
295 strcat(daemonCmd, *it);
296 haveInterfaces = true;
297 }
298
299 if ((mDaemonFd != -1) && haveInterfaces) {
300 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
301 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
302 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
303 return -1;
304 }
305 }
San Mehat9d10b342010-01-18 09:51:02 -0800306 return 0;
307}
308
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800309int TetherController::tetherInterface(const char *interface) {
310 ALOGD("tetherInterface(%s)", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -0700311 if (!isIfaceName(interface)) {
312 errno = ENOENT;
313 return -1;
314 }
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800315 mInterfaces->push_back(strdup(interface));
316
317 if (applyDnsInterfaces()) {
318 InterfaceCollection::iterator it;
319 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
320 if (!strcmp(interface, *it)) {
321 free(*it);
322 mInterfaces->erase(it);
323 break;
324 }
325 }
326 return -1;
327 } else {
328 return 0;
329 }
330}
331
San Mehat9d10b342010-01-18 09:51:02 -0800332int TetherController::untetherInterface(const char *interface) {
333 InterfaceCollection::iterator it;
334
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800335 ALOGD("untetherInterface(%s)", interface);
336
San Mehat9d10b342010-01-18 09:51:02 -0800337 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
338 if (!strcmp(interface, *it)) {
339 free(*it);
340 mInterfaces->erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800341
342 return applyDnsInterfaces();
San Mehat9d10b342010-01-18 09:51:02 -0800343 }
344 }
345 errno = ENOENT;
346 return -1;
347}
348
349InterfaceCollection *TetherController::getTetheredInterfaceList() {
350 return mInterfaces;
351}