blob: 971104f1add1ccf63bc4aa8d32f6bbd212c3a2cc [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"
Erik Kline1d065ba2016-06-08 13:24:45 +090038#include "InterfaceController.h"
San Mehat9d10b342010-01-18 09:51:02 -080039#include "TetherController.h"
40
Lorenzo Colitti799625c2015-02-25 12:52:00 +090041namespace {
42
Erik Kline1d065ba2016-06-08 13:24:45 +090043const char BP_TOOLS_MODE[] = "bp-tools";
44const char IPV4_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv4/ip_forward";
45const char IPV6_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv6/conf/all/forwarding";
46const char SEPARATOR[] = "|";
Lorenzo Colitti799625c2015-02-25 12:52:00 +090047
48bool writeToFile(const char* filename, const char* value) {
Nicolas Geoffrayafd40372015-03-16 11:58:06 +000049 int fd = open(filename, O_WRONLY);
50 if (fd < 0) {
51 ALOGE("Failed to open %s: %s", filename, strerror(errno));
52 return false;
53 }
54
55 const ssize_t len = strlen(value);
56 if (write(fd, value, len) != len) {
57 ALOGE("Failed to write %s to %s: %s", value, filename, strerror(errno));
58 close(fd);
59 return false;
60 }
61 close(fd);
62 return true;
Lorenzo Colitti799625c2015-02-25 12:52:00 +090063}
64
Erik Kline1d065ba2016-06-08 13:24:45 +090065bool configureForIPv6Router(const char *interface) {
66 return (InterfaceController::setEnableIPv6(interface, 0) == 0)
67 && (InterfaceController::setAcceptIPv6Ra(interface, 0) == 0)
68 && (InterfaceController::setEnableIPv6(interface, 1) == 0);
69}
70
71void configureForIPv6Client(const char *interface) {
72 InterfaceController::setAcceptIPv6Ra(interface, 1);
73 InterfaceController::setEnableIPv6(interface, 0);
74}
75
Lorenzo Colitti799625c2015-02-25 12:52:00 +090076bool inBpToolsMode() {
77 // In BP tools mode, do not disable IP forwarding
78 char bootmode[PROPERTY_VALUE_MAX] = {0};
79 property_get("ro.bootmode", bootmode, "unknown");
80 return !strcmp(BP_TOOLS_MODE, bootmode);
81}
82
83} // namespace
84
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070085TetherController::TetherController() {
Lorenzo Colitti667c4772014-08-26 14:13:07 -070086 mDnsNetId = 0;
San Mehat9d10b342010-01-18 09:51:02 -080087 mDaemonFd = -1;
88 mDaemonPid = 0;
Lorenzo Colitti799625c2015-02-25 12:52:00 +090089 if (inBpToolsMode()) {
90 enableForwarding(BP_TOOLS_MODE);
91 } else {
92 setIpFwdEnabled();
93 }
San Mehat9d10b342010-01-18 09:51:02 -080094}
95
96TetherController::~TetherController() {
Erik Kline1d065ba2016-06-08 13:24:45 +090097 mInterfaces.clear();
98 mDnsForwarders.clear();
Lorenzo Colitti799625c2015-02-25 12:52:00 +090099 mForwardingRequests.clear();
San Mehat9d10b342010-01-18 09:51:02 -0800100}
101
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900102bool TetherController::setIpFwdEnabled() {
103 bool success = true;
104 const char* value = mForwardingRequests.empty() ? "0" : "1";
105 ALOGD("Setting IP forward enable = %s", value);
106 success &= writeToFile(IPV4_FORWARDING_PROC_FILE, value);
107 success &= writeToFile(IPV6_FORWARDING_PROC_FILE, value);
108 return success;
San Mehat9d10b342010-01-18 09:51:02 -0800109}
110
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900111bool TetherController::enableForwarding(const char* requester) {
112 // Don't return an error if this requester already requested forwarding. Only return errors for
113 // things that the caller caller needs to care about, such as "couldn't write to the file to
114 // enable forwarding".
115 mForwardingRequests.insert(requester);
116 return setIpFwdEnabled();
117}
San Mehat9d10b342010-01-18 09:51:02 -0800118
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900119bool TetherController::disableForwarding(const char* requester) {
120 mForwardingRequests.erase(requester);
121 return setIpFwdEnabled();
122}
San Mehat9d10b342010-01-18 09:51:02 -0800123
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900124size_t TetherController::forwardingRequestCount() {
125 return mForwardingRequests.size();
San Mehat9d10b342010-01-18 09:51:02 -0800126}
127
Erik Klinea3ec3702016-01-05 03:52:07 +0000128#define TETHER_START_CONST_ARG 8
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800129
Erik Kline13fa01f2015-11-12 17:49:23 +0900130int TetherController::startTethering(int num_addrs, char **dhcp_ranges) {
San Mehat9d10b342010-01-18 09:51:02 -0800131 if (mDaemonPid != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000132 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -0800133 errno = EBUSY;
134 return -1;
135 }
136
Steve Block7b984e32011-12-20 16:22:42 +0000137 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800138
139 pid_t pid;
140 int pipefd[2];
141
142 if (pipe(pipefd) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000143 ALOGE("pipe failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800144 return -1;
145 }
146
147 /*
148 * TODO: Create a monitoring thread to handle and restart
149 * the daemon if it exits prematurely
150 */
151 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000152 ALOGE("fork failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800153 close(pipefd[0]);
154 close(pipefd[1]);
155 return -1;
156 }
157
158 if (!pid) {
159 close(pipefd[1]);
160 if (pipefd[0] != STDIN_FILENO) {
161 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
Steve Block5ea0c052012-01-06 19:18:11 +0000162 ALOGE("dup2 failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800163 return -1;
164 }
165 close(pipefd[0]);
166 }
San Mehat9d10b342010-01-18 09:51:02 -0800167
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800168 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700169 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
170 args[num_processed_args - 1] = NULL;
171 args[0] = (char *)"/system/bin/dnsmasq";
Peter Nilssonb756f692011-09-08 09:48:31 -0700172 args[1] = (char *)"--keep-in-foreground";
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700173 args[2] = (char *)"--no-resolv";
174 args[3] = (char *)"--no-poll";
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800175 args[4] = (char *)"--dhcp-authoritative";
Jeff Sharkey6df79da2012-04-18 21:53:35 -0700176 // TODO: pipe through metered status from ConnService
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800177 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
178 args[6] = (char *)"--pid-file";
179 args[7] = (char *)"";
San Mehat9d10b342010-01-18 09:51:02 -0800180
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800181 int nextArg = TETHER_START_CONST_ARG;
Erik Kline13fa01f2015-11-12 17:49:23 +0900182 for (int addrIndex = 0; addrIndex < num_addrs; addrIndex += 2) {
183 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h",
184 dhcp_ranges[addrIndex], dhcp_ranges[addrIndex+1]);
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700185 }
186
187 if (execv(args[0], args)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000188 ALOGE("execl failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800189 }
Steve Block5ea0c052012-01-06 19:18:11 +0000190 ALOGE("Should never get here!");
JP Abgrallce4f3792012-08-06 13:44:44 -0700191 _exit(-1);
San Mehat9d10b342010-01-18 09:51:02 -0800192 } else {
193 close(pipefd[0]);
194 mDaemonPid = pid;
195 mDaemonFd = pipefd[1];
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800196 applyDnsInterfaces();
Steve Block7b984e32011-12-20 16:22:42 +0000197 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800198 }
199
200 return 0;
201}
202
203int TetherController::stopTethering() {
204
205 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000206 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800207 return 0;
208 }
209
Steve Block7b984e32011-12-20 16:22:42 +0000210 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800211
212 kill(mDaemonPid, SIGTERM);
San Mehat18737842010-01-21 09:22:43 -0800213 waitpid(mDaemonPid, NULL, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800214 mDaemonPid = 0;
215 close(mDaemonFd);
216 mDaemonFd = -1;
Steve Block7b984e32011-12-20 16:22:42 +0000217 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800218 return 0;
219}
Matthew Xie19944102012-07-12 16:42:07 -0700220
San Mehat9d10b342010-01-18 09:51:02 -0800221bool TetherController::isTetheringStarted() {
222 return (mDaemonPid == 0 ? false : true);
223}
224
Kenny Rootcf52faf2010-02-18 09:59:55 -0800225#define MAX_CMD_SIZE 1024
226
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700227int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
San Mehat9d10b342010-01-18 09:51:02 -0800228 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800229 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800230
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700231 Fwmark fwmark;
232 fwmark.netId = netId;
233 fwmark.explicitlySelected = true;
234 fwmark.protectedFromVpn = true;
235 fwmark.permission = PERMISSION_SYSTEM;
236
Erik Kline13fa01f2015-11-12 17:49:23 +0900237 snprintf(daemonCmd, sizeof(daemonCmd), "update_dns%s0x%x", SEPARATOR, fwmark.intValue);
Kenny Rootcf52faf2010-02-18 09:59:55 -0800238 int cmdLen = strlen(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800239
Erik Kline1d065ba2016-06-08 13:24:45 +0900240 mDnsForwarders.clear();
San Mehat9d10b342010-01-18 09:51:02 -0800241 for (i = 0; i < numServers; i++) {
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700242 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800243
Lorenzo Colittic2841282015-11-25 22:13:57 +0900244 addrinfo *res, hints = { .ai_flags = AI_NUMERICHOST };
245 int ret = getaddrinfo(servers[i], NULL, &hints, &res);
246 freeaddrinfo(res);
247 if (ret) {
Steve Block5ea0c052012-01-06 19:18:11 +0000248 ALOGE("Failed to parse DNS server '%s'", servers[i]);
Erik Kline1d065ba2016-06-08 13:24:45 +0900249 mDnsForwarders.clear();
Lorenzo Colittic2841282015-11-25 22:13:57 +0900250 errno = EINVAL;
San Mehat9d10b342010-01-18 09:51:02 -0800251 return -1;
252 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800253
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700254 cmdLen += (strlen(servers[i]) + 1);
255 if (cmdLen + 1 >= MAX_CMD_SIZE) {
Steve Block7b984e32011-12-20 16:22:42 +0000256 ALOGD("Too many DNS servers listed");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800257 break;
258 }
259
Erik Kline13fa01f2015-11-12 17:49:23 +0900260 strcat(daemonCmd, SEPARATOR);
San Mehat9d10b342010-01-18 09:51:02 -0800261 strcat(daemonCmd, servers[i]);
Erik Kline1d065ba2016-06-08 13:24:45 +0900262 mDnsForwarders.push_back(servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800263 }
264
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700265 mDnsNetId = netId;
San Mehat9d10b342010-01-18 09:51:02 -0800266 if (mDaemonFd != -1) {
Steve Block7b984e32011-12-20 16:22:42 +0000267 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800268 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000269 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
Erik Kline1d065ba2016-06-08 13:24:45 +0900270 mDnsForwarders.clear();
Lorenzo Colittic2841282015-11-25 22:13:57 +0900271 errno = EREMOTEIO;
San Mehat9d10b342010-01-18 09:51:02 -0800272 return -1;
273 }
274 }
275 return 0;
276}
277
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700278unsigned TetherController::getDnsNetId() {
279 return mDnsNetId;
280}
281
Erik Kline1d065ba2016-06-08 13:24:45 +0900282const std::list<std::string> &TetherController::getDnsForwarders() const {
San Mehat9d10b342010-01-18 09:51:02 -0800283 return mDnsForwarders;
284}
285
Erik Kline1d065ba2016-06-08 13:24:45 +0900286bool TetherController::applyDnsInterfaces() {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800287 char daemonCmd[MAX_CMD_SIZE];
288
289 strcpy(daemonCmd, "update_ifaces");
290 int cmdLen = strlen(daemonCmd);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800291 bool haveInterfaces = false;
292
Erik Kline1d065ba2016-06-08 13:24:45 +0900293 for (const auto &ifname : mInterfaces) {
294 cmdLen += (ifname.size() + 1);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800295 if (cmdLen + 1 >= MAX_CMD_SIZE) {
296 ALOGD("Too many DNS ifaces listed");
297 break;
298 }
299
Erik Kline13fa01f2015-11-12 17:49:23 +0900300 strcat(daemonCmd, SEPARATOR);
Erik Kline1d065ba2016-06-08 13:24:45 +0900301 strcat(daemonCmd, ifname.c_str());
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800302 haveInterfaces = true;
303 }
304
305 if ((mDaemonFd != -1) && haveInterfaces) {
306 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
307 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
308 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
Erik Kline1d065ba2016-06-08 13:24:45 +0900309 return false;
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800310 }
311 }
Erik Kline1d065ba2016-06-08 13:24:45 +0900312 return true;
San Mehat9d10b342010-01-18 09:51:02 -0800313}
314
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800315int TetherController::tetherInterface(const char *interface) {
316 ALOGD("tetherInterface(%s)", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -0700317 if (!isIfaceName(interface)) {
318 errno = ENOENT;
319 return -1;
320 }
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800321
Erik Kline1d065ba2016-06-08 13:24:45 +0900322 if (!configureForIPv6Router(interface)) {
323 configureForIPv6Client(interface);
324 return -1;
325 }
326 mInterfaces.push_back(interface);
327
328 if (!applyDnsInterfaces()) {
329 mInterfaces.pop_back();
330 configureForIPv6Client(interface);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800331 return -1;
332 } else {
333 return 0;
334 }
335}
336
San Mehat9d10b342010-01-18 09:51:02 -0800337int TetherController::untetherInterface(const char *interface) {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800338 ALOGD("untetherInterface(%s)", interface);
339
Erik Kline1d065ba2016-06-08 13:24:45 +0900340 for (auto it = mInterfaces.cbegin(); it != mInterfaces.cend(); ++it) {
341 if (!strcmp(interface, it->c_str())) {
342 mInterfaces.erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800343
Erik Kline1d065ba2016-06-08 13:24:45 +0900344 configureForIPv6Client(interface);
345 return applyDnsInterfaces() ? 0 : -1;
San Mehat9d10b342010-01-18 09:51:02 -0800346 }
347 }
348 errno = ENOENT;
349 return -1;
350}
351
Erik Kline1d065ba2016-06-08 13:24:45 +0900352const std::list<std::string> &TetherController::getTetheredInterfaceList() const {
San Mehat9d10b342010-01-18 09:51:02 -0800353 return mInterfaces;
354}