blob: 1785ec71e0450dd75a737ef63e0fee19aab501e4 [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"
Lorenzo Colittie20a5262017-05-09 18:30:44 +090039#include "NetworkController.h"
San Mehat9d10b342010-01-18 09:51:02 -080040#include "TetherController.h"
41
Lorenzo Colitti799625c2015-02-25 12:52:00 +090042namespace {
43
Erik Kline1d065ba2016-06-08 13:24:45 +090044const char BP_TOOLS_MODE[] = "bp-tools";
45const char IPV4_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv4/ip_forward";
46const char IPV6_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv6/conf/all/forwarding";
47const char SEPARATOR[] = "|";
Lorenzo Colitti799625c2015-02-25 12:52:00 +090048
49bool writeToFile(const char* filename, const char* value) {
Nick Kralevichb95c60c2016-11-19 09:09:16 -080050 int fd = open(filename, O_WRONLY | O_CLOEXEC);
Nicolas Geoffrayafd40372015-03-16 11:58:06 +000051 if (fd < 0) {
52 ALOGE("Failed to open %s: %s", filename, strerror(errno));
53 return false;
54 }
55
56 const ssize_t len = strlen(value);
57 if (write(fd, value, len) != len) {
58 ALOGE("Failed to write %s to %s: %s", value, filename, strerror(errno));
59 close(fd);
60 return false;
61 }
62 close(fd);
63 return true;
Lorenzo Colitti799625c2015-02-25 12:52:00 +090064}
65
Erik Kline1d065ba2016-06-08 13:24:45 +090066bool configureForIPv6Router(const char *interface) {
67 return (InterfaceController::setEnableIPv6(interface, 0) == 0)
68 && (InterfaceController::setAcceptIPv6Ra(interface, 0) == 0)
Erik Kline6cddf512016-08-09 15:28:42 +090069 && (InterfaceController::setAcceptIPv6Dad(interface, 0) == 0)
70 && (InterfaceController::setIPv6DadTransmits(interface, "0") == 0)
Erik Kline1d065ba2016-06-08 13:24:45 +090071 && (InterfaceController::setEnableIPv6(interface, 1) == 0);
72}
73
74void configureForIPv6Client(const char *interface) {
75 InterfaceController::setAcceptIPv6Ra(interface, 1);
Erik Kline6cddf512016-08-09 15:28:42 +090076 InterfaceController::setAcceptIPv6Dad(interface, 1);
77 InterfaceController::setIPv6DadTransmits(interface, "1");
Erik Kline1d065ba2016-06-08 13:24:45 +090078 InterfaceController::setEnableIPv6(interface, 0);
79}
80
Lorenzo Colitti799625c2015-02-25 12:52:00 +090081bool inBpToolsMode() {
82 // In BP tools mode, do not disable IP forwarding
83 char bootmode[PROPERTY_VALUE_MAX] = {0};
84 property_get("ro.bootmode", bootmode, "unknown");
85 return !strcmp(BP_TOOLS_MODE, bootmode);
86}
87
88} // namespace
89
Lorenzo Colittie20a5262017-05-09 18:30:44 +090090namespace android {
91namespace net {
92
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070093TetherController::TetherController() {
Lorenzo Colitti667c4772014-08-26 14:13:07 -070094 mDnsNetId = 0;
San Mehat9d10b342010-01-18 09:51:02 -080095 mDaemonFd = -1;
96 mDaemonPid = 0;
Lorenzo Colitti799625c2015-02-25 12:52:00 +090097 if (inBpToolsMode()) {
98 enableForwarding(BP_TOOLS_MODE);
99 } else {
100 setIpFwdEnabled();
101 }
San Mehat9d10b342010-01-18 09:51:02 -0800102}
103
104TetherController::~TetherController() {
Erik Kline1d065ba2016-06-08 13:24:45 +0900105 mInterfaces.clear();
106 mDnsForwarders.clear();
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900107 mForwardingRequests.clear();
San Mehat9d10b342010-01-18 09:51:02 -0800108}
109
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900110bool TetherController::setIpFwdEnabled() {
111 bool success = true;
112 const char* value = mForwardingRequests.empty() ? "0" : "1";
113 ALOGD("Setting IP forward enable = %s", value);
114 success &= writeToFile(IPV4_FORWARDING_PROC_FILE, value);
115 success &= writeToFile(IPV6_FORWARDING_PROC_FILE, value);
116 return success;
San Mehat9d10b342010-01-18 09:51:02 -0800117}
118
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900119bool TetherController::enableForwarding(const char* requester) {
120 // Don't return an error if this requester already requested forwarding. Only return errors for
121 // things that the caller caller needs to care about, such as "couldn't write to the file to
122 // enable forwarding".
123 mForwardingRequests.insert(requester);
124 return setIpFwdEnabled();
125}
San Mehat9d10b342010-01-18 09:51:02 -0800126
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900127bool TetherController::disableForwarding(const char* requester) {
128 mForwardingRequests.erase(requester);
129 return setIpFwdEnabled();
130}
San Mehat9d10b342010-01-18 09:51:02 -0800131
Lorenzo Colitti799625c2015-02-25 12:52:00 +0900132size_t TetherController::forwardingRequestCount() {
133 return mForwardingRequests.size();
San Mehat9d10b342010-01-18 09:51:02 -0800134}
135
Lorenzo Colittie20a5262017-05-09 18:30:44 +0900136#define TETHER_START_CONST_ARG 10
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800137
Erik Kline13fa01f2015-11-12 17:49:23 +0900138int TetherController::startTethering(int num_addrs, char **dhcp_ranges) {
San Mehat9d10b342010-01-18 09:51:02 -0800139 if (mDaemonPid != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000140 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -0800141 errno = EBUSY;
142 return -1;
143 }
144
Steve Block7b984e32011-12-20 16:22:42 +0000145 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800146
147 pid_t pid;
148 int pipefd[2];
149
150 if (pipe(pipefd) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000151 ALOGE("pipe failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800152 return -1;
153 }
154
155 /*
156 * TODO: Create a monitoring thread to handle and restart
157 * the daemon if it exits prematurely
158 */
159 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000160 ALOGE("fork failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800161 close(pipefd[0]);
162 close(pipefd[1]);
163 return -1;
164 }
165
166 if (!pid) {
167 close(pipefd[1]);
168 if (pipefd[0] != STDIN_FILENO) {
169 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
Steve Block5ea0c052012-01-06 19:18:11 +0000170 ALOGE("dup2 failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800171 return -1;
172 }
173 close(pipefd[0]);
174 }
San Mehat9d10b342010-01-18 09:51:02 -0800175
Lorenzo Colittie20a5262017-05-09 18:30:44 +0900176 Fwmark fwmark;
177 fwmark.netId = NetworkController::LOCAL_NET_ID;
178 fwmark.explicitlySelected = true;
179 fwmark.protectedFromVpn = true;
180 fwmark.permission = PERMISSION_SYSTEM;
181 char markStr[UINT32_HEX_STRLEN];
182 snprintf(markStr, sizeof(markStr), "0x%x", fwmark.intValue);
183
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800184 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700185 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
186 args[num_processed_args - 1] = NULL;
187 args[0] = (char *)"/system/bin/dnsmasq";
Peter Nilssonb756f692011-09-08 09:48:31 -0700188 args[1] = (char *)"--keep-in-foreground";
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700189 args[2] = (char *)"--no-resolv";
190 args[3] = (char *)"--no-poll";
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800191 args[4] = (char *)"--dhcp-authoritative";
Jeff Sharkey6df79da2012-04-18 21:53:35 -0700192 // TODO: pipe through metered status from ConnService
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800193 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
194 args[6] = (char *)"--pid-file";
Lorenzo Colittie20a5262017-05-09 18:30:44 +0900195 args[7] = (char *)"--listen-mark";
196 args[8] = (char *)markStr;
197 args[9] = (char *)"";
San Mehat9d10b342010-01-18 09:51:02 -0800198
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800199 int nextArg = TETHER_START_CONST_ARG;
Erik Kline13fa01f2015-11-12 17:49:23 +0900200 for (int addrIndex = 0; addrIndex < num_addrs; addrIndex += 2) {
201 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h",
202 dhcp_ranges[addrIndex], dhcp_ranges[addrIndex+1]);
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700203 }
204
205 if (execv(args[0], args)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000206 ALOGE("execl failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800207 }
Steve Block5ea0c052012-01-06 19:18:11 +0000208 ALOGE("Should never get here!");
JP Abgrallce4f3792012-08-06 13:44:44 -0700209 _exit(-1);
San Mehat9d10b342010-01-18 09:51:02 -0800210 } else {
211 close(pipefd[0]);
212 mDaemonPid = pid;
213 mDaemonFd = pipefd[1];
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800214 applyDnsInterfaces();
Steve Block7b984e32011-12-20 16:22:42 +0000215 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800216 }
217
218 return 0;
219}
220
221int TetherController::stopTethering() {
222
223 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000224 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800225 return 0;
226 }
227
Steve Block7b984e32011-12-20 16:22:42 +0000228 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800229
230 kill(mDaemonPid, SIGTERM);
San Mehat18737842010-01-21 09:22:43 -0800231 waitpid(mDaemonPid, NULL, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800232 mDaemonPid = 0;
233 close(mDaemonFd);
234 mDaemonFd = -1;
Steve Block7b984e32011-12-20 16:22:42 +0000235 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800236 return 0;
237}
Matthew Xie19944102012-07-12 16:42:07 -0700238
San Mehat9d10b342010-01-18 09:51:02 -0800239bool TetherController::isTetheringStarted() {
240 return (mDaemonPid == 0 ? false : true);
241}
242
Kenny Rootcf52faf2010-02-18 09:59:55 -0800243#define MAX_CMD_SIZE 1024
244
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700245int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
San Mehat9d10b342010-01-18 09:51:02 -0800246 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800247 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800248
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700249 Fwmark fwmark;
250 fwmark.netId = netId;
251 fwmark.explicitlySelected = true;
252 fwmark.protectedFromVpn = true;
253 fwmark.permission = PERMISSION_SYSTEM;
254
Erik Kline13fa01f2015-11-12 17:49:23 +0900255 snprintf(daemonCmd, sizeof(daemonCmd), "update_dns%s0x%x", SEPARATOR, fwmark.intValue);
Kenny Rootcf52faf2010-02-18 09:59:55 -0800256 int cmdLen = strlen(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800257
Erik Kline1d065ba2016-06-08 13:24:45 +0900258 mDnsForwarders.clear();
San Mehat9d10b342010-01-18 09:51:02 -0800259 for (i = 0; i < numServers; i++) {
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700260 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800261
Lorenzo Colittic2841282015-11-25 22:13:57 +0900262 addrinfo *res, hints = { .ai_flags = AI_NUMERICHOST };
263 int ret = getaddrinfo(servers[i], NULL, &hints, &res);
264 freeaddrinfo(res);
265 if (ret) {
Steve Block5ea0c052012-01-06 19:18:11 +0000266 ALOGE("Failed to parse DNS server '%s'", servers[i]);
Erik Kline1d065ba2016-06-08 13:24:45 +0900267 mDnsForwarders.clear();
Lorenzo Colittic2841282015-11-25 22:13:57 +0900268 errno = EINVAL;
San Mehat9d10b342010-01-18 09:51:02 -0800269 return -1;
270 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800271
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700272 cmdLen += (strlen(servers[i]) + 1);
273 if (cmdLen + 1 >= MAX_CMD_SIZE) {
Steve Block7b984e32011-12-20 16:22:42 +0000274 ALOGD("Too many DNS servers listed");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800275 break;
276 }
277
Erik Kline13fa01f2015-11-12 17:49:23 +0900278 strcat(daemonCmd, SEPARATOR);
San Mehat9d10b342010-01-18 09:51:02 -0800279 strcat(daemonCmd, servers[i]);
Erik Kline1d065ba2016-06-08 13:24:45 +0900280 mDnsForwarders.push_back(servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800281 }
282
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700283 mDnsNetId = netId;
San Mehat9d10b342010-01-18 09:51:02 -0800284 if (mDaemonFd != -1) {
Steve Block7b984e32011-12-20 16:22:42 +0000285 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800286 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000287 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
Erik Kline1d065ba2016-06-08 13:24:45 +0900288 mDnsForwarders.clear();
Lorenzo Colittic2841282015-11-25 22:13:57 +0900289 errno = EREMOTEIO;
San Mehat9d10b342010-01-18 09:51:02 -0800290 return -1;
291 }
292 }
293 return 0;
294}
295
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700296unsigned TetherController::getDnsNetId() {
297 return mDnsNetId;
298}
299
Erik Kline1d065ba2016-06-08 13:24:45 +0900300const std::list<std::string> &TetherController::getDnsForwarders() const {
San Mehat9d10b342010-01-18 09:51:02 -0800301 return mDnsForwarders;
302}
303
Erik Kline1d065ba2016-06-08 13:24:45 +0900304bool TetherController::applyDnsInterfaces() {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800305 char daemonCmd[MAX_CMD_SIZE];
306
307 strcpy(daemonCmd, "update_ifaces");
308 int cmdLen = strlen(daemonCmd);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800309 bool haveInterfaces = false;
310
Erik Kline1d065ba2016-06-08 13:24:45 +0900311 for (const auto &ifname : mInterfaces) {
312 cmdLen += (ifname.size() + 1);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800313 if (cmdLen + 1 >= MAX_CMD_SIZE) {
314 ALOGD("Too many DNS ifaces listed");
315 break;
316 }
317
Erik Kline13fa01f2015-11-12 17:49:23 +0900318 strcat(daemonCmd, SEPARATOR);
Erik Kline1d065ba2016-06-08 13:24:45 +0900319 strcat(daemonCmd, ifname.c_str());
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800320 haveInterfaces = true;
321 }
322
323 if ((mDaemonFd != -1) && haveInterfaces) {
324 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
325 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
326 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
Erik Kline1d065ba2016-06-08 13:24:45 +0900327 return false;
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800328 }
329 }
Erik Kline1d065ba2016-06-08 13:24:45 +0900330 return true;
San Mehat9d10b342010-01-18 09:51:02 -0800331}
332
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800333int TetherController::tetherInterface(const char *interface) {
334 ALOGD("tetherInterface(%s)", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -0700335 if (!isIfaceName(interface)) {
336 errno = ENOENT;
337 return -1;
338 }
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800339
Erik Kline1d065ba2016-06-08 13:24:45 +0900340 if (!configureForIPv6Router(interface)) {
341 configureForIPv6Client(interface);
342 return -1;
343 }
344 mInterfaces.push_back(interface);
345
346 if (!applyDnsInterfaces()) {
347 mInterfaces.pop_back();
348 configureForIPv6Client(interface);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800349 return -1;
350 } else {
351 return 0;
352 }
353}
354
San Mehat9d10b342010-01-18 09:51:02 -0800355int TetherController::untetherInterface(const char *interface) {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800356 ALOGD("untetherInterface(%s)", interface);
357
Erik Kline1d065ba2016-06-08 13:24:45 +0900358 for (auto it = mInterfaces.cbegin(); it != mInterfaces.cend(); ++it) {
359 if (!strcmp(interface, it->c_str())) {
360 mInterfaces.erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800361
Erik Kline1d065ba2016-06-08 13:24:45 +0900362 configureForIPv6Client(interface);
363 return applyDnsInterfaces() ? 0 : -1;
San Mehat9d10b342010-01-18 09:51:02 -0800364 }
365 }
366 errno = ENOENT;
367 return -1;
368}
369
Erik Kline1d065ba2016-06-08 13:24:45 +0900370const std::list<std::string> &TetherController::getTetheredInterfaceList() const {
San Mehat9d10b342010-01-18 09:51:02 -0800371 return mInterfaces;
372}
Lorenzo Colittie20a5262017-05-09 18:30:44 +0900373
374} // namespace net
375} // namespace android