blob: 8d14a146eb948e33378a44f85cfabf24ae6a0f52 [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
Robert Greenwalt3208ea02010-03-24 16:32:55 -070099int TetherController::startTethering(int num_addrs, struct in_addr* addrs) {
San Mehat9d10b342010-01-18 09:51:02 -0800100 if (mDaemonPid != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000101 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -0800102 errno = EBUSY;
103 return -1;
104 }
105
Steve Block7b984e32011-12-20 16:22:42 +0000106 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800107
108 pid_t pid;
109 int pipefd[2];
110
111 if (pipe(pipefd) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000112 ALOGE("pipe failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800113 return -1;
114 }
115
116 /*
117 * TODO: Create a monitoring thread to handle and restart
118 * the daemon if it exits prematurely
119 */
120 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000121 ALOGE("fork failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800122 close(pipefd[0]);
123 close(pipefd[1]);
124 return -1;
125 }
126
127 if (!pid) {
128 close(pipefd[1]);
129 if (pipefd[0] != STDIN_FILENO) {
130 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
Steve Block5ea0c052012-01-06 19:18:11 +0000131 ALOGE("dup2 failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800132 return -1;
133 }
134 close(pipefd[0]);
135 }
San Mehat9d10b342010-01-18 09:51:02 -0800136
Jean-Baptiste Queru9997f9a2012-06-14 15:34:31 -0700137 int num_processed_args = 7 + (num_addrs/2) + 1; // 1 null for termination
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700138 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
139 args[num_processed_args - 1] = NULL;
140 args[0] = (char *)"/system/bin/dnsmasq";
Peter Nilssonb756f692011-09-08 09:48:31 -0700141 args[1] = (char *)"--keep-in-foreground";
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700142 args[2] = (char *)"--no-resolv";
143 args[3] = (char *)"--no-poll";
Jeff Sharkey6df79da2012-04-18 21:53:35 -0700144 // TODO: pipe through metered status from ConnService
145 args[4] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
Jean-Baptiste Queru9997f9a2012-06-14 15:34:31 -0700146 args[5] = (char *)"--pid-file";
147 args[6] = (char *)"";
San Mehat9d10b342010-01-18 09:51:02 -0800148
Jean-Baptiste Queru9997f9a2012-06-14 15:34:31 -0700149 int nextArg = 7;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700150 for (int addrIndex=0; addrIndex < num_addrs;) {
151 char *start = strdup(inet_ntoa(addrs[addrIndex++]));
152 char *end = strdup(inet_ntoa(addrs[addrIndex++]));
153 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end);
154 }
155
156 if (execv(args[0], args)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000157 ALOGE("execl failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800158 }
Steve Block5ea0c052012-01-06 19:18:11 +0000159 ALOGE("Should never get here!");
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700160 free(args);
San Mehat9d10b342010-01-18 09:51:02 -0800161 return 0;
162 } else {
163 close(pipefd[0]);
164 mDaemonPid = pid;
165 mDaemonFd = pipefd[1];
Steve Block7b984e32011-12-20 16:22:42 +0000166 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800167 }
168
169 return 0;
170}
171
172int TetherController::stopTethering() {
173
174 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000175 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800176 return 0;
177 }
178
Steve Block7b984e32011-12-20 16:22:42 +0000179 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800180
181 kill(mDaemonPid, SIGTERM);
San Mehat18737842010-01-21 09:22:43 -0800182 waitpid(mDaemonPid, NULL, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800183 mDaemonPid = 0;
184 close(mDaemonFd);
185 mDaemonFd = -1;
Steve Block7b984e32011-12-20 16:22:42 +0000186 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800187 return 0;
188}
189
190bool TetherController::isTetheringStarted() {
191 return (mDaemonPid == 0 ? false : true);
192}
193
Kenny Rootcf52faf2010-02-18 09:59:55 -0800194#define MAX_CMD_SIZE 1024
195
San Mehat9d10b342010-01-18 09:51:02 -0800196int TetherController::setDnsForwarders(char **servers, int numServers) {
197 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800198 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800199
200 strcpy(daemonCmd, "update_dns");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800201 int cmdLen = strlen(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800202
203 mDnsForwarders->clear();
204 for (i = 0; i < numServers; i++) {
Steve Block7b984e32011-12-20 16:22:42 +0000205 ALOGD("setDnsForwarders(%d = '%s')", i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800206
207 struct in_addr a;
208
209 if (!inet_aton(servers[i], &a)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000210 ALOGE("Failed to parse DNS server '%s'", servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800211 mDnsForwarders->clear();
212 return -1;
213 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800214
215 cmdLen += strlen(servers[i]);
216 if (cmdLen + 2 >= MAX_CMD_SIZE) {
Steve Block7b984e32011-12-20 16:22:42 +0000217 ALOGD("Too many DNS servers listed");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800218 break;
219 }
220
San Mehat9d10b342010-01-18 09:51:02 -0800221 strcat(daemonCmd, ":");
222 strcat(daemonCmd, servers[i]);
223 mDnsForwarders->push_back(a);
224 }
225
226 if (mDaemonFd != -1) {
Steve Block7b984e32011-12-20 16:22:42 +0000227 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800228 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000229 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800230 mDnsForwarders->clear();
231 return -1;
232 }
233 }
234 return 0;
235}
236
237NetAddressCollection *TetherController::getDnsForwarders() {
238 return mDnsForwarders;
239}
240
241int TetherController::tetherInterface(const char *interface) {
242 mInterfaces->push_back(strdup(interface));
243 return 0;
244}
245
246int TetherController::untetherInterface(const char *interface) {
247 InterfaceCollection::iterator it;
248
249 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
250 if (!strcmp(interface, *it)) {
251 free(*it);
252 mInterfaces->erase(it);
253 return 0;
254 }
255 }
256 errno = ENOENT;
257 return -1;
258}
259
260InterfaceCollection *TetherController::getTetheredInterfaceList() {
261 return mInterfaces;
262}