blob: 44c7e35aa148c323a3d54ed6f4fa497b09df762c [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
Dan Albertb67219a2015-03-13 22:35:27 -070030#include <base/file.h>
San Mehat9d10b342010-01-18 09:51:02 -080031#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"
San Mehat9d10b342010-01-18 09:51:02 -080038#include "TetherController.h"
39
Dan Albertb67219a2015-03-13 22:35:27 -070040using android::base::ReadFileToString;
41using android::base::WriteStringToFile;
42
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070043TetherController::TetherController() {
San Mehat9d10b342010-01-18 09:51:02 -080044 mInterfaces = new InterfaceCollection();
Lorenzo Colitti667c4772014-08-26 14:13:07 -070045 mDnsNetId = 0;
San Mehat9d10b342010-01-18 09:51:02 -080046 mDnsForwarders = new NetAddressCollection();
47 mDaemonFd = -1;
48 mDaemonPid = 0;
49}
50
51TetherController::~TetherController() {
52 InterfaceCollection::iterator it;
53
54 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
55 free(*it);
56 }
57 mInterfaces->clear();
58
59 mDnsForwarders->clear();
60}
61
62int TetherController::setIpFwdEnabled(bool enable) {
63
Steve Block7b984e32011-12-20 16:22:42 +000064 ALOGD("Setting IP forward enable = %d", enable);
Kazuhiro Ondo6b858eb2011-06-24 20:31:03 -050065
66 // In BP tools mode, do not disable IP forwarding
67 char bootmode[PROPERTY_VALUE_MAX] = {0};
68 property_get("ro.bootmode", bootmode, "unknown");
69 if ((enable == false) && (0 == strcmp("bp-tools", bootmode))) {
70 return 0;
71 }
72
Dan Albertb67219a2015-03-13 22:35:27 -070073 if (!WriteStringToFile(enable ? "1" : "0", "/proc/sys/net/ipv4/ip_forward")) {
Elliott Hughesd1614062015-02-02 18:08:59 -080074 ALOGE("Failed to write ip_forward (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -080075 return -1;
76 }
77
San Mehat9d10b342010-01-18 09:51:02 -080078 return 0;
79}
80
81bool TetherController::getIpFwdEnabled() {
Elliott Hughesd1614062015-02-02 18:08:59 -080082 std::string enabled;
Dan Albertb67219a2015-03-13 22:35:27 -070083 if (!ReadFileToString("/proc/sys/net/ipv4/ip_forward", &enabled)) {
Steve Block5ea0c052012-01-06 19:18:11 +000084 ALOGE("Failed to read ip_forward (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -080085 return -1;
86 }
87
Elliott Hughesd1614062015-02-02 18:08:59 -080088 return (enabled == "1" ? true : false);
San Mehat9d10b342010-01-18 09:51:02 -080089}
90
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -080091#define TETHER_START_CONST_ARG 8
92
Robert Greenwalt3208ea02010-03-24 16:32:55 -070093int TetherController::startTethering(int num_addrs, struct in_addr* addrs) {
San Mehat9d10b342010-01-18 09:51:02 -080094 if (mDaemonPid != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000095 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -080096 errno = EBUSY;
97 return -1;
98 }
99
Steve Block7b984e32011-12-20 16:22:42 +0000100 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800101
102 pid_t pid;
103 int pipefd[2];
104
105 if (pipe(pipefd) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000106 ALOGE("pipe failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800107 return -1;
108 }
109
110 /*
111 * TODO: Create a monitoring thread to handle and restart
112 * the daemon if it exits prematurely
113 */
114 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000115 ALOGE("fork failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800116 close(pipefd[0]);
117 close(pipefd[1]);
118 return -1;
119 }
120
121 if (!pid) {
122 close(pipefd[1]);
123 if (pipefd[0] != STDIN_FILENO) {
124 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
Steve Block5ea0c052012-01-06 19:18:11 +0000125 ALOGE("dup2 failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800126 return -1;
127 }
128 close(pipefd[0]);
129 }
San Mehat9d10b342010-01-18 09:51:02 -0800130
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800131 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700132 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
133 args[num_processed_args - 1] = NULL;
134 args[0] = (char *)"/system/bin/dnsmasq";
Peter Nilssonb756f692011-09-08 09:48:31 -0700135 args[1] = (char *)"--keep-in-foreground";
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700136 args[2] = (char *)"--no-resolv";
137 args[3] = (char *)"--no-poll";
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800138 args[4] = (char *)"--dhcp-authoritative";
Jeff Sharkey6df79da2012-04-18 21:53:35 -0700139 // TODO: pipe through metered status from ConnService
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800140 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
141 args[6] = (char *)"--pid-file";
142 args[7] = (char *)"";
San Mehat9d10b342010-01-18 09:51:02 -0800143
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800144 int nextArg = TETHER_START_CONST_ARG;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700145 for (int addrIndex=0; addrIndex < num_addrs;) {
146 char *start = strdup(inet_ntoa(addrs[addrIndex++]));
147 char *end = strdup(inet_ntoa(addrs[addrIndex++]));
148 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end);
Jesper Hanssona9d791f2012-04-27 13:54:27 +0200149 free(start);
150 free(end);
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700151 }
152
153 if (execv(args[0], args)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000154 ALOGE("execl failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800155 }
Steve Block5ea0c052012-01-06 19:18:11 +0000156 ALOGE("Should never get here!");
JP Abgrallce4f3792012-08-06 13:44:44 -0700157 _exit(-1);
San Mehat9d10b342010-01-18 09:51:02 -0800158 } else {
159 close(pipefd[0]);
160 mDaemonPid = pid;
161 mDaemonFd = pipefd[1];
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800162 applyDnsInterfaces();
Steve Block7b984e32011-12-20 16:22:42 +0000163 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800164 }
165
166 return 0;
167}
168
169int TetherController::stopTethering() {
170
171 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000172 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800173 return 0;
174 }
175
Steve Block7b984e32011-12-20 16:22:42 +0000176 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800177
178 kill(mDaemonPid, SIGTERM);
San Mehat18737842010-01-21 09:22:43 -0800179 waitpid(mDaemonPid, NULL, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800180 mDaemonPid = 0;
181 close(mDaemonFd);
182 mDaemonFd = -1;
Steve Block7b984e32011-12-20 16:22:42 +0000183 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800184 return 0;
185}
Matthew Xie19944102012-07-12 16:42:07 -0700186
San Mehat9d10b342010-01-18 09:51:02 -0800187bool TetherController::isTetheringStarted() {
188 return (mDaemonPid == 0 ? false : true);
189}
190
Kenny Rootcf52faf2010-02-18 09:59:55 -0800191#define MAX_CMD_SIZE 1024
192
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700193int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
San Mehat9d10b342010-01-18 09:51:02 -0800194 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800195 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800196
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700197 Fwmark fwmark;
198 fwmark.netId = netId;
199 fwmark.explicitlySelected = true;
200 fwmark.protectedFromVpn = true;
201 fwmark.permission = PERMISSION_SYSTEM;
202
203 snprintf(daemonCmd, sizeof(daemonCmd), "update_dns:0x%x", fwmark.intValue);
Kenny Rootcf52faf2010-02-18 09:59:55 -0800204 int cmdLen = strlen(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800205
206 mDnsForwarders->clear();
207 for (i = 0; i < numServers; i++) {
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700208 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800209
210 struct in_addr a;
211
212 if (!inet_aton(servers[i], &a)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000213 ALOGE("Failed to parse DNS server '%s'", servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800214 mDnsForwarders->clear();
215 return -1;
216 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800217
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700218 cmdLen += (strlen(servers[i]) + 1);
219 if (cmdLen + 1 >= MAX_CMD_SIZE) {
Steve Block7b984e32011-12-20 16:22:42 +0000220 ALOGD("Too many DNS servers listed");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800221 break;
222 }
223
San Mehat9d10b342010-01-18 09:51:02 -0800224 strcat(daemonCmd, ":");
225 strcat(daemonCmd, servers[i]);
226 mDnsForwarders->push_back(a);
227 }
228
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700229 mDnsNetId = netId;
San Mehat9d10b342010-01-18 09:51:02 -0800230 if (mDaemonFd != -1) {
Steve Block7b984e32011-12-20 16:22:42 +0000231 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800232 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000233 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800234 mDnsForwarders->clear();
235 return -1;
236 }
237 }
238 return 0;
239}
240
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700241unsigned TetherController::getDnsNetId() {
242 return mDnsNetId;
243}
244
San Mehat9d10b342010-01-18 09:51:02 -0800245NetAddressCollection *TetherController::getDnsForwarders() {
246 return mDnsForwarders;
247}
248
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800249int TetherController::applyDnsInterfaces() {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800250 char daemonCmd[MAX_CMD_SIZE];
251
252 strcpy(daemonCmd, "update_ifaces");
253 int cmdLen = strlen(daemonCmd);
254 InterfaceCollection::iterator it;
255 bool haveInterfaces = false;
256
257 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
258 cmdLen += (strlen(*it) + 1);
259 if (cmdLen + 1 >= MAX_CMD_SIZE) {
260 ALOGD("Too many DNS ifaces listed");
261 break;
262 }
263
264 strcat(daemonCmd, ":");
265 strcat(daemonCmd, *it);
266 haveInterfaces = true;
267 }
268
269 if ((mDaemonFd != -1) && haveInterfaces) {
270 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
271 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
272 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
273 return -1;
274 }
275 }
San Mehat9d10b342010-01-18 09:51:02 -0800276 return 0;
277}
278
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800279int TetherController::tetherInterface(const char *interface) {
280 ALOGD("tetherInterface(%s)", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -0700281 if (!isIfaceName(interface)) {
282 errno = ENOENT;
283 return -1;
284 }
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800285 mInterfaces->push_back(strdup(interface));
286
287 if (applyDnsInterfaces()) {
288 InterfaceCollection::iterator it;
289 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
290 if (!strcmp(interface, *it)) {
291 free(*it);
292 mInterfaces->erase(it);
293 break;
294 }
295 }
296 return -1;
297 } else {
298 return 0;
299 }
300}
301
San Mehat9d10b342010-01-18 09:51:02 -0800302int TetherController::untetherInterface(const char *interface) {
303 InterfaceCollection::iterator it;
304
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800305 ALOGD("untetherInterface(%s)", interface);
306
San Mehat9d10b342010-01-18 09:51:02 -0800307 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
308 if (!strcmp(interface, *it)) {
309 free(*it);
310 mInterfaces->erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800311
312 return applyDnsInterfaces();
San Mehat9d10b342010-01-18 09:51:02 -0800313 }
314 }
315 errno = ENOENT;
316 return -1;
317}
318
319InterfaceCollection *TetherController::getTetheredInterfaceList() {
320 return mInterfaces;
321}