blob: 93110e0dc8c5fa422ccaf727e50e0646318c849b [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;
41}
42
43TetherController::~TetherController() {
44 InterfaceCollection::iterator it;
45
46 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
47 free(*it);
48 }
49 mInterfaces->clear();
50
51 mDnsForwarders->clear();
52}
53
54int TetherController::setIpFwdEnabled(bool enable) {
55
Steve Block7b984e32011-12-20 16:22:42 +000056 ALOGD("Setting IP forward enable = %d", enable);
Kazuhiro Ondo6b858eb2011-06-24 20:31:03 -050057
58 // In BP tools mode, do not disable IP forwarding
59 char bootmode[PROPERTY_VALUE_MAX] = {0};
60 property_get("ro.bootmode", bootmode, "unknown");
61 if ((enable == false) && (0 == strcmp("bp-tools", bootmode))) {
62 return 0;
63 }
64
San Mehat9d10b342010-01-18 09:51:02 -080065 int fd = open("/proc/sys/net/ipv4/ip_forward", O_WRONLY);
66 if (fd < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000067 ALOGE("Failed to open ip_forward (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -080068 return -1;
69 }
70
71 if (write(fd, (enable ? "1" : "0"), 1) != 1) {
Steve Block5ea0c052012-01-06 19:18:11 +000072 ALOGE("Failed to write ip_forward (%s)", strerror(errno));
Robert Greenwalt37dc4a52010-04-28 16:05:04 -070073 close(fd);
San Mehat9d10b342010-01-18 09:51:02 -080074 return -1;
75 }
76 close(fd);
77 return 0;
78}
79
80bool TetherController::getIpFwdEnabled() {
81 int fd = open("/proc/sys/net/ipv4/ip_forward", O_RDONLY);
82
83 if (fd < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000084 ALOGE("Failed to open ip_forward (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -080085 return false;
86 }
87
88 char enabled;
89 if (read(fd, &enabled, 1) != 1) {
Steve Block5ea0c052012-01-06 19:18:11 +000090 ALOGE("Failed to read ip_forward (%s)", strerror(errno));
Robert Greenwalt37dc4a52010-04-28 16:05:04 -070091 close(fd);
San Mehat9d10b342010-01-18 09:51:02 -080092 return -1;
93 }
94
95 close(fd);
San Mehat9d10b342010-01-18 09:51:02 -080096 return (enabled == '1' ? true : false);
97}
98
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -080099#define TETHER_START_CONST_ARG 8
100
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700101int TetherController::startTethering(int num_addrs, struct in_addr* addrs) {
San Mehat9d10b342010-01-18 09:51:02 -0800102 if (mDaemonPid != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000103 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -0800104 errno = EBUSY;
105 return -1;
106 }
107
Steve Block7b984e32011-12-20 16:22:42 +0000108 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800109
110 pid_t pid;
111 int pipefd[2];
112
113 if (pipe(pipefd) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000114 ALOGE("pipe failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800115 return -1;
116 }
117
118 /*
119 * TODO: Create a monitoring thread to handle and restart
120 * the daemon if it exits prematurely
121 */
122 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000123 ALOGE("fork failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800124 close(pipefd[0]);
125 close(pipefd[1]);
126 return -1;
127 }
128
129 if (!pid) {
130 close(pipefd[1]);
131 if (pipefd[0] != STDIN_FILENO) {
132 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
Steve Block5ea0c052012-01-06 19:18:11 +0000133 ALOGE("dup2 failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800134 return -1;
135 }
136 close(pipefd[0]);
137 }
San Mehat9d10b342010-01-18 09:51:02 -0800138
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800139 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700140 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
141 args[num_processed_args - 1] = NULL;
142 args[0] = (char *)"/system/bin/dnsmasq";
Peter Nilssonb756f692011-09-08 09:48:31 -0700143 args[1] = (char *)"--keep-in-foreground";
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700144 args[2] = (char *)"--no-resolv";
145 args[3] = (char *)"--no-poll";
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800146 args[4] = (char *)"--dhcp-authoritative";
Jeff Sharkey6df79da2012-04-18 21:53:35 -0700147 // TODO: pipe through metered status from ConnService
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800148 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
149 args[6] = (char *)"--pid-file";
150 args[7] = (char *)"";
San Mehat9d10b342010-01-18 09:51:02 -0800151
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800152 int nextArg = TETHER_START_CONST_ARG;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700153 for (int addrIndex=0; addrIndex < num_addrs;) {
154 char *start = strdup(inet_ntoa(addrs[addrIndex++]));
155 char *end = strdup(inet_ntoa(addrs[addrIndex++]));
156 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end);
157 }
158
159 if (execv(args[0], args)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000160 ALOGE("execl failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800161 }
Steve Block5ea0c052012-01-06 19:18:11 +0000162 ALOGE("Should never get here!");
JP Abgrallce4f3792012-08-06 13:44:44 -0700163 _exit(-1);
San Mehat9d10b342010-01-18 09:51:02 -0800164 } else {
165 close(pipefd[0]);
166 mDaemonPid = pid;
167 mDaemonFd = pipefd[1];
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800168 applyDnsInterfaces();
Steve Block7b984e32011-12-20 16:22:42 +0000169 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800170 }
171
172 return 0;
173}
174
175int TetherController::stopTethering() {
176
177 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000178 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800179 return 0;
180 }
181
Steve Block7b984e32011-12-20 16:22:42 +0000182 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800183
184 kill(mDaemonPid, SIGTERM);
San Mehat18737842010-01-21 09:22:43 -0800185 waitpid(mDaemonPid, NULL, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800186 mDaemonPid = 0;
187 close(mDaemonFd);
188 mDaemonFd = -1;
Steve Block7b984e32011-12-20 16:22:42 +0000189 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800190 return 0;
191}
Matthew Xie19944102012-07-12 16:42:07 -0700192
San Mehat9d10b342010-01-18 09:51:02 -0800193bool TetherController::isTetheringStarted() {
194 return (mDaemonPid == 0 ? false : true);
195}
196
Kenny Rootcf52faf2010-02-18 09:59:55 -0800197#define MAX_CMD_SIZE 1024
198
San Mehat9d10b342010-01-18 09:51:02 -0800199int TetherController::setDnsForwarders(char **servers, int numServers) {
200 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800201 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800202
203 strcpy(daemonCmd, "update_dns");
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++) {
Steve Block7b984e32011-12-20 16:22:42 +0000208 ALOGD("setDnsForwarders(%d = '%s')", 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
229 if (mDaemonFd != -1) {
Steve Block7b984e32011-12-20 16:22:42 +0000230 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800231 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000232 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800233 mDnsForwarders->clear();
234 return -1;
235 }
236 }
237 return 0;
238}
239
240NetAddressCollection *TetherController::getDnsForwarders() {
241 return mDnsForwarders;
242}
243
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800244int TetherController::applyDnsInterfaces() {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800245 char daemonCmd[MAX_CMD_SIZE];
246
247 strcpy(daemonCmd, "update_ifaces");
248 int cmdLen = strlen(daemonCmd);
249 InterfaceCollection::iterator it;
250 bool haveInterfaces = false;
251
252 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
253 cmdLen += (strlen(*it) + 1);
254 if (cmdLen + 1 >= MAX_CMD_SIZE) {
255 ALOGD("Too many DNS ifaces listed");
256 break;
257 }
258
259 strcat(daemonCmd, ":");
260 strcat(daemonCmd, *it);
261 haveInterfaces = true;
262 }
263
264 if ((mDaemonFd != -1) && haveInterfaces) {
265 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
266 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
267 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
268 return -1;
269 }
270 }
San Mehat9d10b342010-01-18 09:51:02 -0800271 return 0;
272}
273
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800274int TetherController::tetherInterface(const char *interface) {
275 ALOGD("tetherInterface(%s)", interface);
276 mInterfaces->push_back(strdup(interface));
277
278 if (applyDnsInterfaces()) {
279 InterfaceCollection::iterator it;
280 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
281 if (!strcmp(interface, *it)) {
282 free(*it);
283 mInterfaces->erase(it);
284 break;
285 }
286 }
287 return -1;
288 } else {
289 return 0;
290 }
291}
292
San Mehat9d10b342010-01-18 09:51:02 -0800293int TetherController::untetherInterface(const char *interface) {
294 InterfaceCollection::iterator it;
295
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800296 ALOGD("untetherInterface(%s)", interface);
297
San Mehat9d10b342010-01-18 09:51:02 -0800298 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
299 if (!strcmp(interface, *it)) {
300 free(*it);
301 mInterfaces->erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800302
303 return applyDnsInterfaces();
San Mehat9d10b342010-01-18 09:51:02 -0800304 }
305 }
306 errno = ENOENT;
307 return -1;
308}
309
310InterfaceCollection *TetherController::getTetheredInterfaceList() {
311 return mInterfaces;
312}