blob: 5a08ed21ddcb66923eb60d3adb5861a094e490f0 [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) {
Vinit Deshpandea4614fe2015-03-16 01:28:03 -070047 return WriteStringToFile(value, file);
Lorenzo Colitti799625c2015-02-25 12:52:00 +090048}
49
50bool inBpToolsMode() {
51 // In BP tools mode, do not disable IP forwarding
52 char bootmode[PROPERTY_VALUE_MAX] = {0};
53 property_get("ro.bootmode", bootmode, "unknown");
54 return !strcmp(BP_TOOLS_MODE, bootmode);
55}
56
57} // namespace
58
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070059TetherController::TetherController() {
San Mehat9d10b342010-01-18 09:51:02 -080060 mInterfaces = new InterfaceCollection();
Lorenzo Colitti667c4772014-08-26 14:13:07 -070061 mDnsNetId = 0;
San Mehat9d10b342010-01-18 09:51:02 -080062 mDnsForwarders = new NetAddressCollection();
63 mDaemonFd = -1;
64 mDaemonPid = 0;
Lorenzo Colitti799625c2015-02-25 12:52:00 +090065 if (inBpToolsMode()) {
66 enableForwarding(BP_TOOLS_MODE);
67 } else {
68 setIpFwdEnabled();
69 }
San Mehat9d10b342010-01-18 09:51:02 -080070}
71
72TetherController::~TetherController() {
73 InterfaceCollection::iterator it;
74
75 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
76 free(*it);
77 }
78 mInterfaces->clear();
79
80 mDnsForwarders->clear();
Lorenzo Colitti799625c2015-02-25 12:52:00 +090081 mForwardingRequests.clear();
San Mehat9d10b342010-01-18 09:51:02 -080082}
83
Lorenzo Colitti799625c2015-02-25 12:52:00 +090084bool TetherController::setIpFwdEnabled() {
85 bool success = true;
86 const char* value = mForwardingRequests.empty() ? "0" : "1";
87 ALOGD("Setting IP forward enable = %s", value);
88 success &= writeToFile(IPV4_FORWARDING_PROC_FILE, value);
89 success &= writeToFile(IPV6_FORWARDING_PROC_FILE, value);
90 return success;
San Mehat9d10b342010-01-18 09:51:02 -080091}
92
Lorenzo Colitti799625c2015-02-25 12:52:00 +090093bool TetherController::enableForwarding(const char* requester) {
94 // Don't return an error if this requester already requested forwarding. Only return errors for
95 // things that the caller caller needs to care about, such as "couldn't write to the file to
96 // enable forwarding".
97 mForwardingRequests.insert(requester);
98 return setIpFwdEnabled();
99}
San Mehat9d10b342010-01-18 09:51:02 -0800100
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900101bool TetherController::disableForwarding(const char* requester) {
102 mForwardingRequests.erase(requester);
103 return setIpFwdEnabled();
104}
San Mehat9d10b342010-01-18 09:51:02 -0800105
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900106size_t TetherController::forwardingRequestCount() {
107 return mForwardingRequests.size();
San Mehat9d10b342010-01-18 09:51:02 -0800108}
109
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800110#define TETHER_START_CONST_ARG 8
111
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700112int TetherController::startTethering(int num_addrs, struct in_addr* addrs) {
San Mehat9d10b342010-01-18 09:51:02 -0800113 if (mDaemonPid != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000114 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -0800115 errno = EBUSY;
116 return -1;
117 }
118
Steve Block7b984e32011-12-20 16:22:42 +0000119 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800120
121 pid_t pid;
122 int pipefd[2];
123
124 if (pipe(pipefd) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000125 ALOGE("pipe failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800126 return -1;
127 }
128
129 /*
130 * TODO: Create a monitoring thread to handle and restart
131 * the daemon if it exits prematurely
132 */
133 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000134 ALOGE("fork failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800135 close(pipefd[0]);
136 close(pipefd[1]);
137 return -1;
138 }
139
140 if (!pid) {
141 close(pipefd[1]);
142 if (pipefd[0] != STDIN_FILENO) {
143 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
Steve Block5ea0c052012-01-06 19:18:11 +0000144 ALOGE("dup2 failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800145 return -1;
146 }
147 close(pipefd[0]);
148 }
San Mehat9d10b342010-01-18 09:51:02 -0800149
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800150 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700151 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
152 args[num_processed_args - 1] = NULL;
153 args[0] = (char *)"/system/bin/dnsmasq";
Peter Nilssonb756f692011-09-08 09:48:31 -0700154 args[1] = (char *)"--keep-in-foreground";
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700155 args[2] = (char *)"--no-resolv";
156 args[3] = (char *)"--no-poll";
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800157 args[4] = (char *)"--dhcp-authoritative";
Jeff Sharkey6df79da2012-04-18 21:53:35 -0700158 // TODO: pipe through metered status from ConnService
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800159 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
160 args[6] = (char *)"--pid-file";
161 args[7] = (char *)"";
San Mehat9d10b342010-01-18 09:51:02 -0800162
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800163 int nextArg = TETHER_START_CONST_ARG;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700164 for (int addrIndex=0; addrIndex < num_addrs;) {
165 char *start = strdup(inet_ntoa(addrs[addrIndex++]));
166 char *end = strdup(inet_ntoa(addrs[addrIndex++]));
167 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end);
Jesper Hanssona9d791f2012-04-27 13:54:27 +0200168 free(start);
169 free(end);
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700170 }
171
172 if (execv(args[0], args)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000173 ALOGE("execl failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800174 }
Steve Block5ea0c052012-01-06 19:18:11 +0000175 ALOGE("Should never get here!");
JP Abgrallce4f3792012-08-06 13:44:44 -0700176 _exit(-1);
San Mehat9d10b342010-01-18 09:51:02 -0800177 } else {
178 close(pipefd[0]);
179 mDaemonPid = pid;
180 mDaemonFd = pipefd[1];
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800181 applyDnsInterfaces();
Steve Block7b984e32011-12-20 16:22:42 +0000182 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800183 }
184
185 return 0;
186}
187
188int TetherController::stopTethering() {
189
190 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000191 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800192 return 0;
193 }
194
Steve Block7b984e32011-12-20 16:22:42 +0000195 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800196
197 kill(mDaemonPid, SIGTERM);
San Mehat18737842010-01-21 09:22:43 -0800198 waitpid(mDaemonPid, NULL, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800199 mDaemonPid = 0;
200 close(mDaemonFd);
201 mDaemonFd = -1;
Steve Block7b984e32011-12-20 16:22:42 +0000202 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800203 return 0;
204}
Matthew Xie19944102012-07-12 16:42:07 -0700205
San Mehat9d10b342010-01-18 09:51:02 -0800206bool TetherController::isTetheringStarted() {
207 return (mDaemonPid == 0 ? false : true);
208}
209
Kenny Rootcf52faf2010-02-18 09:59:55 -0800210#define MAX_CMD_SIZE 1024
211
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700212int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
San Mehat9d10b342010-01-18 09:51:02 -0800213 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800214 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800215
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700216 Fwmark fwmark;
217 fwmark.netId = netId;
218 fwmark.explicitlySelected = true;
219 fwmark.protectedFromVpn = true;
220 fwmark.permission = PERMISSION_SYSTEM;
221
222 snprintf(daemonCmd, sizeof(daemonCmd), "update_dns:0x%x", fwmark.intValue);
Kenny Rootcf52faf2010-02-18 09:59:55 -0800223 int cmdLen = strlen(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800224
225 mDnsForwarders->clear();
226 for (i = 0; i < numServers; i++) {
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700227 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800228
229 struct in_addr a;
230
231 if (!inet_aton(servers[i], &a)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000232 ALOGE("Failed to parse DNS server '%s'", servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800233 mDnsForwarders->clear();
234 return -1;
235 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800236
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700237 cmdLen += (strlen(servers[i]) + 1);
238 if (cmdLen + 1 >= MAX_CMD_SIZE) {
Steve Block7b984e32011-12-20 16:22:42 +0000239 ALOGD("Too many DNS servers listed");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800240 break;
241 }
242
San Mehat9d10b342010-01-18 09:51:02 -0800243 strcat(daemonCmd, ":");
244 strcat(daemonCmd, servers[i]);
245 mDnsForwarders->push_back(a);
246 }
247
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700248 mDnsNetId = netId;
San Mehat9d10b342010-01-18 09:51:02 -0800249 if (mDaemonFd != -1) {
Steve Block7b984e32011-12-20 16:22:42 +0000250 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800251 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000252 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800253 mDnsForwarders->clear();
254 return -1;
255 }
256 }
257 return 0;
258}
259
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700260unsigned TetherController::getDnsNetId() {
261 return mDnsNetId;
262}
263
San Mehat9d10b342010-01-18 09:51:02 -0800264NetAddressCollection *TetherController::getDnsForwarders() {
265 return mDnsForwarders;
266}
267
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800268int TetherController::applyDnsInterfaces() {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800269 char daemonCmd[MAX_CMD_SIZE];
270
271 strcpy(daemonCmd, "update_ifaces");
272 int cmdLen = strlen(daemonCmd);
273 InterfaceCollection::iterator it;
274 bool haveInterfaces = false;
275
276 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
277 cmdLen += (strlen(*it) + 1);
278 if (cmdLen + 1 >= MAX_CMD_SIZE) {
279 ALOGD("Too many DNS ifaces listed");
280 break;
281 }
282
283 strcat(daemonCmd, ":");
284 strcat(daemonCmd, *it);
285 haveInterfaces = true;
286 }
287
288 if ((mDaemonFd != -1) && haveInterfaces) {
289 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
290 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
291 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
292 return -1;
293 }
294 }
San Mehat9d10b342010-01-18 09:51:02 -0800295 return 0;
296}
297
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800298int TetherController::tetherInterface(const char *interface) {
299 ALOGD("tetherInterface(%s)", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -0700300 if (!isIfaceName(interface)) {
301 errno = ENOENT;
302 return -1;
303 }
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800304 mInterfaces->push_back(strdup(interface));
305
306 if (applyDnsInterfaces()) {
307 InterfaceCollection::iterator it;
308 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
309 if (!strcmp(interface, *it)) {
310 free(*it);
311 mInterfaces->erase(it);
312 break;
313 }
314 }
315 return -1;
316 } else {
317 return 0;
318 }
319}
320
San Mehat9d10b342010-01-18 09:51:02 -0800321int TetherController::untetherInterface(const char *interface) {
322 InterfaceCollection::iterator it;
323
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800324 ALOGD("untetherInterface(%s)", interface);
325
San Mehat9d10b342010-01-18 09:51:02 -0800326 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
327 if (!strcmp(interface, *it)) {
328 free(*it);
329 mInterfaces->erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800330
331 return applyDnsInterfaces();
San Mehat9d10b342010-01-18 09:51:02 -0800332 }
333 }
334 errno = ENOENT;
335 return -1;
336}
337
338InterfaceCollection *TetherController::getTetheredInterfaceList() {
339 return mInterfaces;
340}