2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/socket.h>
24 #include <sys/types.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
30 #define LOG_TAG "TetherController"
31 #include <cutils/log.h>
32 #include <cutils/properties.h>
33 #include <utils/file.h>
36 #include "NetdConstants.h"
37 #include "Permission.h"
38 #include "TetherController.h"
42 static const char BP_TOOLS_MODE[] = "bp-tools";
43 static const char IPV4_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv4/ip_forward";
44 static const char IPV6_FORWARDING_PROC_FILE[] = "/proc/sys/net/ipv6/conf/all/forwarding";
46 bool writeToFile(const char* filename, const char* value) {
47 int fd = open(filename, O_WRONLY);
49 ALOGE("Failed to open %s: %s", filename, strerror(errno));
53 const ssize_t len = strlen(value);
54 if (write(fd, value, len) != len) {
55 ALOGE("Failed to write %s to %s: %s", value, filename, strerror(errno));
63 bool inBpToolsMode() {
64 // In BP tools mode, do not disable IP forwarding
65 char bootmode[PROPERTY_VALUE_MAX] = {0};
66 property_get("ro.bootmode", bootmode, "unknown");
67 return !strcmp(BP_TOOLS_MODE, bootmode);
72 TetherController::TetherController() {
73 mInterfaces = new InterfaceCollection();
75 mDnsForwarders = new NetAddressCollection();
78 if (inBpToolsMode()) {
79 enableForwarding(BP_TOOLS_MODE);
85 TetherController::~TetherController() {
86 InterfaceCollection::iterator it;
88 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
93 mDnsForwarders->clear();
94 mForwardingRequests.clear();
97 bool TetherController::setIpFwdEnabled() {
99 const char* value = mForwardingRequests.empty() ? "0" : "1";
100 ALOGD("Setting IP forward enable = %s", value);
101 success &= writeToFile(IPV4_FORWARDING_PROC_FILE, value);
102 success &= writeToFile(IPV6_FORWARDING_PROC_FILE, value);
106 bool TetherController::enableForwarding(const char* requester) {
107 // Don't return an error if this requester already requested forwarding. Only return errors for
108 // things that the caller caller needs to care about, such as "couldn't write to the file to
109 // enable forwarding".
110 mForwardingRequests.insert(requester);
111 return setIpFwdEnabled();
114 bool TetherController::disableForwarding(const char* requester) {
115 mForwardingRequests.erase(requester);
116 return setIpFwdEnabled();
119 size_t TetherController::forwardingRequestCount() {
120 return mForwardingRequests.size();
123 #define TETHER_START_CONST_ARG 8
125 int TetherController::startTethering(int num_addrs, struct in_addr* addrs) {
126 if (mDaemonPid != 0) {
127 ALOGE("Tethering already started");
132 ALOGD("Starting tethering services");
137 if (pipe(pipefd) < 0) {
138 ALOGE("pipe failed (%s)", strerror(errno));
143 * TODO: Create a monitoring thread to handle and restart
144 * the daemon if it exits prematurely
146 if ((pid = fork()) < 0) {
147 ALOGE("fork failed (%s)", strerror(errno));
155 if (pipefd[0] != STDIN_FILENO) {
156 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
157 ALOGE("dup2 failed (%s)", strerror(errno));
163 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1;
164 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
165 args[num_processed_args - 1] = NULL;
166 args[0] = (char *)"/system/bin/dnsmasq";
167 args[1] = (char *)"--keep-in-foreground";
168 args[2] = (char *)"--no-resolv";
169 args[3] = (char *)"--no-poll";
170 args[4] = (char *)"--dhcp-authoritative";
171 // TODO: pipe through metered status from ConnService
172 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
173 args[6] = (char *)"--pid-file";
174 args[7] = (char *)"";
176 int nextArg = TETHER_START_CONST_ARG;
177 for (int addrIndex=0; addrIndex < num_addrs;) {
178 char *start = strdup(inet_ntoa(addrs[addrIndex++]));
179 char *end = strdup(inet_ntoa(addrs[addrIndex++]));
180 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end);
185 if (execv(args[0], args)) {
186 ALOGE("execl failed (%s)", strerror(errno));
188 ALOGE("Should never get here!");
193 mDaemonFd = pipefd[1];
194 applyDnsInterfaces();
195 ALOGD("Tethering services running");
201 int TetherController::stopTethering() {
203 if (mDaemonPid == 0) {
204 ALOGE("Tethering already stopped");
208 ALOGD("Stopping tethering services");
210 kill(mDaemonPid, SIGTERM);
211 waitpid(mDaemonPid, NULL, 0);
215 ALOGD("Tethering services stopped");
219 bool TetherController::isTetheringStarted() {
220 return (mDaemonPid == 0 ? false : true);
223 #define MAX_CMD_SIZE 1024
225 int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
227 char daemonCmd[MAX_CMD_SIZE];
230 fwmark.netId = netId;
231 fwmark.explicitlySelected = true;
232 fwmark.protectedFromVpn = true;
233 fwmark.permission = PERMISSION_SYSTEM;
235 snprintf(daemonCmd, sizeof(daemonCmd), "update_dns:0x%x", fwmark.intValue);
236 int cmdLen = strlen(daemonCmd);
238 mDnsForwarders->clear();
239 for (i = 0; i < numServers; i++) {
240 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
244 if (!inet_aton(servers[i], &a)) {
245 ALOGE("Failed to parse DNS server '%s'", servers[i]);
246 mDnsForwarders->clear();
250 cmdLen += (strlen(servers[i]) + 1);
251 if (cmdLen + 1 >= MAX_CMD_SIZE) {
252 ALOGD("Too many DNS servers listed");
256 strcat(daemonCmd, ":");
257 strcat(daemonCmd, servers[i]);
258 mDnsForwarders->push_back(a);
262 if (mDaemonFd != -1) {
263 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
264 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
265 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
266 mDnsForwarders->clear();
273 unsigned TetherController::getDnsNetId() {
277 NetAddressCollection *TetherController::getDnsForwarders() {
278 return mDnsForwarders;
281 int TetherController::applyDnsInterfaces() {
282 char daemonCmd[MAX_CMD_SIZE];
284 strcpy(daemonCmd, "update_ifaces");
285 int cmdLen = strlen(daemonCmd);
286 InterfaceCollection::iterator it;
287 bool haveInterfaces = false;
289 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
290 cmdLen += (strlen(*it) + 1);
291 if (cmdLen + 1 >= MAX_CMD_SIZE) {
292 ALOGD("Too many DNS ifaces listed");
296 strcat(daemonCmd, ":");
297 strcat(daemonCmd, *it);
298 haveInterfaces = true;
301 if ((mDaemonFd != -1) && haveInterfaces) {
302 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
303 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
304 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
311 int TetherController::tetherInterface(const char *interface) {
312 ALOGD("tetherInterface(%s)", interface);
313 if (!isIfaceName(interface)) {
317 mInterfaces->push_back(strdup(interface));
319 if (applyDnsInterfaces()) {
320 InterfaceCollection::iterator it;
321 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
322 if (!strcmp(interface, *it)) {
324 mInterfaces->erase(it);
334 int TetherController::untetherInterface(const char *interface) {
335 InterfaceCollection::iterator it;
337 ALOGD("untetherInterface(%s)", interface);
339 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
340 if (!strcmp(interface, *it)) {
342 mInterfaces->erase(it);
344 return applyDnsInterfaces();
351 InterfaceCollection *TetherController::getTetheredInterfaceList() {