blob: d067f110a35c93696aea007aa0909ed4ae8828f8 [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
34#include "TetherController.h"
35
36TetherController::TetherController() {
37 mInterfaces = new InterfaceCollection();
38 mDnsForwarders = new NetAddressCollection();
39 mDaemonFd = -1;
40 mDaemonPid = 0;
zzy9589a4c2012-04-14 17:24:24 -070041 mDhcpcdPid = 0;
San Mehat9d10b342010-01-18 09:51:02 -080042}
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
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700100int TetherController::startTethering(int num_addrs, struct in_addr* addrs) {
San Mehat9d10b342010-01-18 09:51:02 -0800101 if (mDaemonPid != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000102 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -0800103 errno = EBUSY;
104 return -1;
105 }
106
Steve Block7b984e32011-12-20 16:22:42 +0000107 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800108
109 pid_t pid;
110 int pipefd[2];
111
112 if (pipe(pipefd) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000113 ALOGE("pipe failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800114 return -1;
115 }
116
117 /*
118 * TODO: Create a monitoring thread to handle and restart
119 * the daemon if it exits prematurely
120 */
121 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000122 ALOGE("fork failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800123 close(pipefd[0]);
124 close(pipefd[1]);
125 return -1;
126 }
127
128 if (!pid) {
129 close(pipefd[1]);
130 if (pipefd[0] != STDIN_FILENO) {
131 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
Steve Block5ea0c052012-01-06 19:18:11 +0000132 ALOGE("dup2 failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800133 return -1;
134 }
135 close(pipefd[0]);
136 }
San Mehat9d10b342010-01-18 09:51:02 -0800137
Jean-Baptiste Queru9997f9a2012-06-14 15:34:31 -0700138 int num_processed_args = 7 + (num_addrs/2) + 1; // 1 null for termination
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700139 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
140 args[num_processed_args - 1] = NULL;
141 args[0] = (char *)"/system/bin/dnsmasq";
Peter Nilssonb756f692011-09-08 09:48:31 -0700142 args[1] = (char *)"--keep-in-foreground";
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700143 args[2] = (char *)"--no-resolv";
144 args[3] = (char *)"--no-poll";
Jeff Sharkey6df79da2012-04-18 21:53:35 -0700145 // TODO: pipe through metered status from ConnService
146 args[4] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
Jean-Baptiste Queru9997f9a2012-06-14 15:34:31 -0700147 args[5] = (char *)"--pid-file";
148 args[6] = (char *)"";
San Mehat9d10b342010-01-18 09:51:02 -0800149
Jean-Baptiste Queru9997f9a2012-06-14 15:34:31 -0700150 int nextArg = 7;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700151 for (int addrIndex=0; addrIndex < num_addrs;) {
152 char *start = strdup(inet_ntoa(addrs[addrIndex++]));
153 char *end = strdup(inet_ntoa(addrs[addrIndex++]));
154 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end);
155 }
156
157 if (execv(args[0], args)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000158 ALOGE("execl failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800159 }
Steve Block5ea0c052012-01-06 19:18:11 +0000160 ALOGE("Should never get here!");
JP Abgrallce4f3792012-08-06 13:44:44 -0700161 _exit(-1);
San Mehat9d10b342010-01-18 09:51:02 -0800162 } else {
163 close(pipefd[0]);
164 mDaemonPid = pid;
165 mDaemonFd = pipefd[1];
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800166 applyDnsInterfaces();
Steve Block7b984e32011-12-20 16:22:42 +0000167 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800168 }
169
170 return 0;
171}
172
173int TetherController::stopTethering() {
174
175 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000176 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800177 return 0;
178 }
179
Steve Block7b984e32011-12-20 16:22:42 +0000180 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800181
182 kill(mDaemonPid, SIGTERM);
San Mehat18737842010-01-21 09:22:43 -0800183 waitpid(mDaemonPid, NULL, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800184 mDaemonPid = 0;
185 close(mDaemonFd);
186 mDaemonFd = -1;
Steve Block7b984e32011-12-20 16:22:42 +0000187 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800188 return 0;
189}
Matthew Xie19944102012-07-12 16:42:07 -0700190
191// TODO(BT) remove
zzy9589a4c2012-04-14 17:24:24 -0700192int TetherController::startReverseTethering(const char* iface) {
193 if (mDhcpcdPid != 0) {
Matthew Xie9caaa442012-06-21 13:40:02 -0700194 ALOGE("Reverse tethering already started");
zzy9589a4c2012-04-14 17:24:24 -0700195 errno = EBUSY;
196 return -1;
197 }
San Mehat9d10b342010-01-18 09:51:02 -0800198
Matthew Xie9caaa442012-06-21 13:40:02 -0700199 ALOGD("TetherController::startReverseTethering, Starting reverse tethering");
zzy9589a4c2012-04-14 17:24:24 -0700200
201 /*
202 * TODO: Create a monitoring thread to handle and restart
203 * the daemon if it exits prematurely
204 */
205 //cleanup the dhcp result
206 char dhcp_result_name[64];
207 snprintf(dhcp_result_name, sizeof(dhcp_result_name) - 1, "dhcp.%s.result", iface);
208 property_set(dhcp_result_name, "");
209
210 pid_t pid;
211 if ((pid = fork()) < 0) {
Matthew Xie9caaa442012-06-21 13:40:02 -0700212 ALOGE("fork failed (%s)", strerror(errno));
zzy9589a4c2012-04-14 17:24:24 -0700213 return -1;
214 }
215
216 if (!pid) {
217
218 char *args[10];
219 int argc = 0;
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800220 args[argc++] = (char *)"/system/bin/dhcpcd";
zzy9589a4c2012-04-14 17:24:24 -0700221 char host_name[128];
222 if (property_get("net.hostname", host_name, NULL) && (host_name[0] != '\0'))
223 {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800224 args[argc++] = (char *)"-h";
zzy9589a4c2012-04-14 17:24:24 -0700225 args[argc++] = host_name;
226 }
227 args[argc++] = (char*)iface;
228 args[argc] = NULL;
229 if (execv(args[0], args)) {
Matthew Xie9caaa442012-06-21 13:40:02 -0700230 ALOGE("startReverseTethering, execv failed (%s)", strerror(errno));
zzy9589a4c2012-04-14 17:24:24 -0700231 }
Matthew Xie9caaa442012-06-21 13:40:02 -0700232 ALOGE("startReverseTethering, Should never get here!");
Matthew Xie19944102012-07-12 16:42:07 -0700233 // TODO(BT) inform parent of the failure.
234 // Parent process need wait for child to report error status
235 // before it set mDhcpcdPid and return 0.
JP Abgrallce4f3792012-08-06 13:44:44 -0700236 _exit(-1);
zzy9589a4c2012-04-14 17:24:24 -0700237 } else {
238 mDhcpcdPid = pid;
Matthew Xie9caaa442012-06-21 13:40:02 -0700239 ALOGD("Reverse Tethering running, pid:%d", pid);
zzy9589a4c2012-04-14 17:24:24 -0700240 }
241 return 0;
242}
Matthew Xie19944102012-07-12 16:42:07 -0700243
244// TODO(BT) remove
zzy9589a4c2012-04-14 17:24:24 -0700245int TetherController::stopReverseTethering() {
246
247 if (mDhcpcdPid == 0) {
Matthew Xie9caaa442012-06-21 13:40:02 -0700248 ALOGE("Tethering already stopped");
zzy9589a4c2012-04-14 17:24:24 -0700249 return 0;
250 }
251
Matthew Xie9caaa442012-06-21 13:40:02 -0700252 ALOGD("Stopping tethering services");
zzy9589a4c2012-04-14 17:24:24 -0700253
254 kill(mDhcpcdPid, SIGTERM);
255 waitpid(mDhcpcdPid, NULL, 0);
256 mDhcpcdPid = 0;
Matthew Xie9caaa442012-06-21 13:40:02 -0700257 ALOGD("Tethering services stopped");
zzy9589a4c2012-04-14 17:24:24 -0700258 return 0;
259}
San Mehat9d10b342010-01-18 09:51:02 -0800260bool TetherController::isTetheringStarted() {
261 return (mDaemonPid == 0 ? false : true);
262}
263
Kenny Rootcf52faf2010-02-18 09:59:55 -0800264#define MAX_CMD_SIZE 1024
265
San Mehat9d10b342010-01-18 09:51:02 -0800266int TetherController::setDnsForwarders(char **servers, int numServers) {
267 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800268 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800269
270 strcpy(daemonCmd, "update_dns");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800271 int cmdLen = strlen(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800272
273 mDnsForwarders->clear();
274 for (i = 0; i < numServers; i++) {
Steve Block7b984e32011-12-20 16:22:42 +0000275 ALOGD("setDnsForwarders(%d = '%s')", i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800276
277 struct in_addr a;
278
279 if (!inet_aton(servers[i], &a)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000280 ALOGE("Failed to parse DNS server '%s'", servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800281 mDnsForwarders->clear();
282 return -1;
283 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800284
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700285 cmdLen += (strlen(servers[i]) + 1);
286 if (cmdLen + 1 >= MAX_CMD_SIZE) {
Steve Block7b984e32011-12-20 16:22:42 +0000287 ALOGD("Too many DNS servers listed");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800288 break;
289 }
290
San Mehat9d10b342010-01-18 09:51:02 -0800291 strcat(daemonCmd, ":");
292 strcat(daemonCmd, servers[i]);
293 mDnsForwarders->push_back(a);
294 }
295
296 if (mDaemonFd != -1) {
Steve Block7b984e32011-12-20 16:22:42 +0000297 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800298 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000299 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800300 mDnsForwarders->clear();
301 return -1;
302 }
303 }
304 return 0;
305}
306
307NetAddressCollection *TetherController::getDnsForwarders() {
308 return mDnsForwarders;
309}
310
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800311int TetherController::applyDnsInterfaces() {
312 int i;
313 char daemonCmd[MAX_CMD_SIZE];
314
315 strcpy(daemonCmd, "update_ifaces");
316 int cmdLen = strlen(daemonCmd);
317 InterfaceCollection::iterator it;
318 bool haveInterfaces = false;
319
320 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
321 cmdLen += (strlen(*it) + 1);
322 if (cmdLen + 1 >= MAX_CMD_SIZE) {
323 ALOGD("Too many DNS ifaces listed");
324 break;
325 }
326
327 strcat(daemonCmd, ":");
328 strcat(daemonCmd, *it);
329 haveInterfaces = true;
330 }
331
332 if ((mDaemonFd != -1) && haveInterfaces) {
333 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
334 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
335 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
336 return -1;
337 }
338 }
San Mehat9d10b342010-01-18 09:51:02 -0800339 return 0;
340}
341
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800342int TetherController::tetherInterface(const char *interface) {
343 ALOGD("tetherInterface(%s)", interface);
344 mInterfaces->push_back(strdup(interface));
345
346 if (applyDnsInterfaces()) {
347 InterfaceCollection::iterator it;
348 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
349 if (!strcmp(interface, *it)) {
350 free(*it);
351 mInterfaces->erase(it);
352 break;
353 }
354 }
355 return -1;
356 } else {
357 return 0;
358 }
359}
360
San Mehat9d10b342010-01-18 09:51:02 -0800361int TetherController::untetherInterface(const char *interface) {
362 InterfaceCollection::iterator it;
363
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800364 ALOGD("untetherInterface(%s)", interface);
365
San Mehat9d10b342010-01-18 09:51:02 -0800366 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
367 if (!strcmp(interface, *it)) {
368 free(*it);
369 mInterfaces->erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800370
371 return applyDnsInterfaces();
San Mehat9d10b342010-01-18 09:51:02 -0800372 }
373 }
374 errno = ENOENT;
375 return -1;
376}
377
378InterfaceCollection *TetherController::getTetheredInterfaceList() {
379 return mInterfaces;
380}