blob: fbee5a2f01485425680d2bd179f2fd46facc449b [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
JP Abgrall69261cb2014-06-19 18:35:24 -070034#include "NetdConstants.h"
San Mehat9d10b342010-01-18 09:51:02 -080035#include "TetherController.h"
36
37TetherController::TetherController() {
38 mInterfaces = new InterfaceCollection();
39 mDnsForwarders = new NetAddressCollection();
40 mDaemonFd = -1;
41 mDaemonPid = 0;
42}
43
44TetherController::~TetherController() {
45 InterfaceCollection::iterator it;
46
47 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
48 free(*it);
49 }
50 mInterfaces->clear();
51
52 mDnsForwarders->clear();
53}
54
55int TetherController::setIpFwdEnabled(bool enable) {
56
Steve Block7b984e32011-12-20 16:22:42 +000057 ALOGD("Setting IP forward enable = %d", enable);
Kazuhiro Ondo6b858eb2011-06-24 20:31:03 -050058
59 // In BP tools mode, do not disable IP forwarding
60 char bootmode[PROPERTY_VALUE_MAX] = {0};
61 property_get("ro.bootmode", bootmode, "unknown");
62 if ((enable == false) && (0 == strcmp("bp-tools", bootmode))) {
63 return 0;
64 }
65
San Mehat9d10b342010-01-18 09:51:02 -080066 int fd = open("/proc/sys/net/ipv4/ip_forward", O_WRONLY);
67 if (fd < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000068 ALOGE("Failed to open ip_forward (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -080069 return -1;
70 }
71
72 if (write(fd, (enable ? "1" : "0"), 1) != 1) {
Steve Block5ea0c052012-01-06 19:18:11 +000073 ALOGE("Failed to write ip_forward (%s)", strerror(errno));
Robert Greenwalt37dc4a52010-04-28 16:05:04 -070074 close(fd);
San Mehat9d10b342010-01-18 09:51:02 -080075 return -1;
76 }
77 close(fd);
78 return 0;
79}
80
81bool TetherController::getIpFwdEnabled() {
82 int fd = open("/proc/sys/net/ipv4/ip_forward", O_RDONLY);
83
84 if (fd < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000085 ALOGE("Failed to open ip_forward (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -080086 return false;
87 }
88
89 char enabled;
90 if (read(fd, &enabled, 1) != 1) {
Steve Block5ea0c052012-01-06 19:18:11 +000091 ALOGE("Failed to read ip_forward (%s)", strerror(errno));
Robert Greenwalt37dc4a52010-04-28 16:05:04 -070092 close(fd);
San Mehat9d10b342010-01-18 09:51:02 -080093 return -1;
94 }
95
96 close(fd);
San Mehat9d10b342010-01-18 09:51:02 -080097 return (enabled == '1' ? true : false);
98}
99
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800100#define TETHER_START_CONST_ARG 8
101
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700102int TetherController::startTethering(int num_addrs, struct in_addr* addrs) {
San Mehat9d10b342010-01-18 09:51:02 -0800103 if (mDaemonPid != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000104 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -0800105 errno = EBUSY;
106 return -1;
107 }
108
Steve Block7b984e32011-12-20 16:22:42 +0000109 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800110
111 pid_t pid;
112 int pipefd[2];
113
114 if (pipe(pipefd) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000115 ALOGE("pipe failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800116 return -1;
117 }
118
119 /*
120 * TODO: Create a monitoring thread to handle and restart
121 * the daemon if it exits prematurely
122 */
123 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000124 ALOGE("fork failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800125 close(pipefd[0]);
126 close(pipefd[1]);
127 return -1;
128 }
129
130 if (!pid) {
131 close(pipefd[1]);
132 if (pipefd[0] != STDIN_FILENO) {
133 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
Steve Block5ea0c052012-01-06 19:18:11 +0000134 ALOGE("dup2 failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800135 return -1;
136 }
137 close(pipefd[0]);
138 }
San Mehat9d10b342010-01-18 09:51:02 -0800139
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800140 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700141 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
142 args[num_processed_args - 1] = NULL;
143 args[0] = (char *)"/system/bin/dnsmasq";
Peter Nilssonb756f692011-09-08 09:48:31 -0700144 args[1] = (char *)"--keep-in-foreground";
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700145 args[2] = (char *)"--no-resolv";
146 args[3] = (char *)"--no-poll";
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800147 args[4] = (char *)"--dhcp-authoritative";
Jeff Sharkey6df79da2012-04-18 21:53:35 -0700148 // TODO: pipe through metered status from ConnService
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800149 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
150 args[6] = (char *)"--pid-file";
151 args[7] = (char *)"";
San Mehat9d10b342010-01-18 09:51:02 -0800152
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800153 int nextArg = TETHER_START_CONST_ARG;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700154 for (int addrIndex=0; addrIndex < num_addrs;) {
155 char *start = strdup(inet_ntoa(addrs[addrIndex++]));
156 char *end = strdup(inet_ntoa(addrs[addrIndex++]));
157 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end);
158 }
159
160 if (execv(args[0], args)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000161 ALOGE("execl failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800162 }
Steve Block5ea0c052012-01-06 19:18:11 +0000163 ALOGE("Should never get here!");
JP Abgrallce4f3792012-08-06 13:44:44 -0700164 _exit(-1);
San Mehat9d10b342010-01-18 09:51:02 -0800165 } else {
166 close(pipefd[0]);
167 mDaemonPid = pid;
168 mDaemonFd = pipefd[1];
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800169 applyDnsInterfaces();
Steve Block7b984e32011-12-20 16:22:42 +0000170 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800171 }
172
173 return 0;
174}
175
176int TetherController::stopTethering() {
177
178 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000179 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800180 return 0;
181 }
182
Steve Block7b984e32011-12-20 16:22:42 +0000183 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800184
185 kill(mDaemonPid, SIGTERM);
San Mehat18737842010-01-21 09:22:43 -0800186 waitpid(mDaemonPid, NULL, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800187 mDaemonPid = 0;
188 close(mDaemonFd);
189 mDaemonFd = -1;
Steve Block7b984e32011-12-20 16:22:42 +0000190 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800191 return 0;
192}
Matthew Xie19944102012-07-12 16:42:07 -0700193
San Mehat9d10b342010-01-18 09:51:02 -0800194bool TetherController::isTetheringStarted() {
195 return (mDaemonPid == 0 ? false : true);
196}
197
Kenny Rootcf52faf2010-02-18 09:59:55 -0800198#define MAX_CMD_SIZE 1024
199
San Mehat9d10b342010-01-18 09:51:02 -0800200int TetherController::setDnsForwarders(char **servers, int numServers) {
201 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800202 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800203
204 strcpy(daemonCmd, "update_dns");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800205 int cmdLen = strlen(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800206
207 mDnsForwarders->clear();
208 for (i = 0; i < numServers; i++) {
Steve Block7b984e32011-12-20 16:22:42 +0000209 ALOGD("setDnsForwarders(%d = '%s')", i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800210
211 struct in_addr a;
212
213 if (!inet_aton(servers[i], &a)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000214 ALOGE("Failed to parse DNS server '%s'", servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800215 mDnsForwarders->clear();
216 return -1;
217 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800218
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700219 cmdLen += (strlen(servers[i]) + 1);
220 if (cmdLen + 1 >= MAX_CMD_SIZE) {
Steve Block7b984e32011-12-20 16:22:42 +0000221 ALOGD("Too many DNS servers listed");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800222 break;
223 }
224
San Mehat9d10b342010-01-18 09:51:02 -0800225 strcat(daemonCmd, ":");
226 strcat(daemonCmd, servers[i]);
227 mDnsForwarders->push_back(a);
228 }
229
230 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
241NetAddressCollection *TetherController::getDnsForwarders() {
242 return mDnsForwarders;
243}
244
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800245int TetherController::applyDnsInterfaces() {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800246 char daemonCmd[MAX_CMD_SIZE];
247
248 strcpy(daemonCmd, "update_ifaces");
249 int cmdLen = strlen(daemonCmd);
250 InterfaceCollection::iterator it;
251 bool haveInterfaces = false;
252
253 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
254 cmdLen += (strlen(*it) + 1);
255 if (cmdLen + 1 >= MAX_CMD_SIZE) {
256 ALOGD("Too many DNS ifaces listed");
257 break;
258 }
259
260 strcat(daemonCmd, ":");
261 strcat(daemonCmd, *it);
262 haveInterfaces = true;
263 }
264
265 if ((mDaemonFd != -1) && haveInterfaces) {
266 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
267 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
268 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
269 return -1;
270 }
271 }
San Mehat9d10b342010-01-18 09:51:02 -0800272 return 0;
273}
274
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800275int TetherController::tetherInterface(const char *interface) {
276 ALOGD("tetherInterface(%s)", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -0700277 if (!isIfaceName(interface)) {
278 errno = ENOENT;
279 return -1;
280 }
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800281 mInterfaces->push_back(strdup(interface));
282
283 if (applyDnsInterfaces()) {
284 InterfaceCollection::iterator it;
285 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
286 if (!strcmp(interface, *it)) {
287 free(*it);
288 mInterfaces->erase(it);
289 break;
290 }
291 }
292 return -1;
293 } else {
294 return 0;
295 }
296}
297
San Mehat9d10b342010-01-18 09:51:02 -0800298int TetherController::untetherInterface(const char *interface) {
299 InterfaceCollection::iterator it;
300
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800301 ALOGD("untetherInterface(%s)", interface);
302
San Mehat9d10b342010-01-18 09:51:02 -0800303 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
304 if (!strcmp(interface, *it)) {
305 free(*it);
306 mInterfaces->erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800307
308 return applyDnsInterfaces();
San Mehat9d10b342010-01-18 09:51:02 -0800309 }
310 }
311 errno = ENOENT;
312 return -1;
313}
314
315InterfaceCollection *TetherController::getTetheredInterfaceList() {
316 return mInterfaces;
317}