blob: 5af59f0e91e82ca8108d42294b84908abd442291 [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
Wink Saville4309f872011-07-29 15:25:24 -070056 // Give OEMs ability to leave IP Fwd enabled all the time for test modes.
57 char allowIpFwd[PROPERTY_VALUE_MAX] = {0};
58 property_get("ro.allow.ip.fwd", allowIpFwd, "0");
59 if ((enable == false) && ('1' == allowIpFwd[0])) {
60 LOGW("Ignoring IP forward disable due to OEM setting");
61 return 0;
62 }
63
San Mehat9d10b342010-01-18 09:51:02 -080064 LOGD("Setting IP forward enable = %d", enable);
Kazuhiro Ondo6b858eb2011-06-24 20:31:03 -050065
Wink Saville4309f872011-07-29 15:25:24 -070066 if (enable == false) {
Kazuhiro Ondo6b858eb2011-06-24 20:31:03 -050067 return 0;
68 }
69
San Mehat9d10b342010-01-18 09:51:02 -080070 int fd = open("/proc/sys/net/ipv4/ip_forward", O_WRONLY);
71 if (fd < 0) {
72 LOGE("Failed to open ip_forward (%s)", strerror(errno));
73 return -1;
74 }
75
76 if (write(fd, (enable ? "1" : "0"), 1) != 1) {
77 LOGE("Failed to write ip_forward (%s)", strerror(errno));
Robert Greenwalt37dc4a52010-04-28 16:05:04 -070078 close(fd);
San Mehat9d10b342010-01-18 09:51:02 -080079 return -1;
80 }
81 close(fd);
82 return 0;
83}
84
85bool TetherController::getIpFwdEnabled() {
86 int fd = open("/proc/sys/net/ipv4/ip_forward", O_RDONLY);
87
88 if (fd < 0) {
89 LOGE("Failed to open ip_forward (%s)", strerror(errno));
90 return false;
91 }
92
93 char enabled;
94 if (read(fd, &enabled, 1) != 1) {
95 LOGE("Failed to read ip_forward (%s)", strerror(errno));
Robert Greenwalt37dc4a52010-04-28 16:05:04 -070096 close(fd);
San Mehat9d10b342010-01-18 09:51:02 -080097 return -1;
98 }
99
100 close(fd);
San Mehat9d10b342010-01-18 09:51:02 -0800101 return (enabled == '1' ? true : false);
102}
103
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700104int TetherController::startTethering(int num_addrs, struct in_addr* addrs) {
San Mehat9d10b342010-01-18 09:51:02 -0800105 if (mDaemonPid != 0) {
106 LOGE("Tethering already started");
107 errno = EBUSY;
108 return -1;
109 }
110
111 LOGD("Starting tethering services");
112
113 pid_t pid;
114 int pipefd[2];
115
116 if (pipe(pipefd) < 0) {
117 LOGE("pipe failed (%s)", strerror(errno));
118 return -1;
119 }
120
121 /*
122 * TODO: Create a monitoring thread to handle and restart
123 * the daemon if it exits prematurely
124 */
125 if ((pid = fork()) < 0) {
126 LOGE("fork failed (%s)", strerror(errno));
127 close(pipefd[0]);
128 close(pipefd[1]);
129 return -1;
130 }
131
132 if (!pid) {
133 close(pipefd[1]);
134 if (pipefd[0] != STDIN_FILENO) {
135 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
136 LOGE("dup2 failed (%s)", strerror(errno));
137 return -1;
138 }
139 close(pipefd[0]);
140 }
San Mehat9d10b342010-01-18 09:51:02 -0800141
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700142 int num_processed_args = 4 + (num_addrs/2) + 1; // 1 null for termination
143 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
144 args[num_processed_args - 1] = NULL;
145 args[0] = (char *)"/system/bin/dnsmasq";
146 args[1] = (char *)"--no-daemon";
147 args[2] = (char *)"--no-resolv";
148 args[3] = (char *)"--no-poll";
San Mehat9d10b342010-01-18 09:51:02 -0800149
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700150 int nextArg = 4;
151 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)) {
San Mehat9d10b342010-01-18 09:51:02 -0800158 LOGE("execl failed (%s)", strerror(errno));
159 }
160 LOGE("Should never get here!");
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700161 free(args);
San Mehat9d10b342010-01-18 09:51:02 -0800162 return 0;
163 } else {
164 close(pipefd[0]);
165 mDaemonPid = pid;
166 mDaemonFd = pipefd[1];
167 LOGD("Tethering services running");
168 }
169
170 return 0;
171}
172
173int TetherController::stopTethering() {
174
175 if (mDaemonPid == 0) {
176 LOGE("Tethering already stopped");
177 return 0;
178 }
179
180 LOGD("Stopping tethering services");
181
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;
San Mehat18737842010-01-21 09:22:43 -0800187 LOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800188 return 0;
189}
190
191bool TetherController::isTetheringStarted() {
192 return (mDaemonPid == 0 ? false : true);
193}
194
Kenny Rootcf52faf2010-02-18 09:59:55 -0800195#define MAX_CMD_SIZE 1024
196
San Mehat9d10b342010-01-18 09:51:02 -0800197int TetherController::setDnsForwarders(char **servers, int numServers) {
198 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800199 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800200
201 strcpy(daemonCmd, "update_dns");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800202 int cmdLen = strlen(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800203
204 mDnsForwarders->clear();
205 for (i = 0; i < numServers; i++) {
206 LOGD("setDnsForwarders(%d = '%s')", i, servers[i]);
207
208 struct in_addr a;
209
210 if (!inet_aton(servers[i], &a)) {
211 LOGE("Failed to parse DNS server '%s'", servers[i]);
212 mDnsForwarders->clear();
213 return -1;
214 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800215
216 cmdLen += strlen(servers[i]);
217 if (cmdLen + 2 >= MAX_CMD_SIZE) {
218 LOGD("Too many DNS servers listed");
219 break;
220 }
221
San Mehat9d10b342010-01-18 09:51:02 -0800222 strcat(daemonCmd, ":");
223 strcat(daemonCmd, servers[i]);
224 mDnsForwarders->push_back(a);
225 }
226
227 if (mDaemonFd != -1) {
228 LOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
229 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
230 LOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
231 mDnsForwarders->clear();
232 return -1;
233 }
234 }
235 return 0;
236}
237
238NetAddressCollection *TetherController::getDnsForwarders() {
239 return mDnsForwarders;
240}
241
242int TetherController::tetherInterface(const char *interface) {
243 mInterfaces->push_back(strdup(interface));
244 return 0;
245}
246
247int TetherController::untetherInterface(const char *interface) {
248 InterfaceCollection::iterator it;
249
250 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
251 if (!strcmp(interface, *it)) {
252 free(*it);
253 mInterfaces->erase(it);
254 return 0;
255 }
256 }
257 errno = ENOENT;
258 return -1;
259}
260
261InterfaceCollection *TetherController::getTetheredInterfaceList() {
262 return mInterfaces;
263}