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.
23 #include <sys/socket.h>
25 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
31 #define LOG_TAG "TetherController"
32 #include <cutils/log.h>
33 #include <cutils/properties.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";
45 static const char SEPARATOR[] = "|";
47 bool writeToFile(const char* filename, const char* value) {
48 int fd = open(filename, O_WRONLY);
50 ALOGE("Failed to open %s: %s", filename, strerror(errno));
54 const ssize_t len = strlen(value);
55 if (write(fd, value, len) != len) {
56 ALOGE("Failed to write %s to %s: %s", value, filename, strerror(errno));
64 bool inBpToolsMode() {
65 // In BP tools mode, do not disable IP forwarding
66 char bootmode[PROPERTY_VALUE_MAX] = {0};
67 property_get("ro.bootmode", bootmode, "unknown");
68 return !strcmp(BP_TOOLS_MODE, bootmode);
73 TetherController::TetherController() {
74 mInterfaces = new InterfaceCollection();
76 mDnsForwarders = new NetAddressCollection();
79 if (inBpToolsMode()) {
80 enableForwarding(BP_TOOLS_MODE);
86 TetherController::~TetherController() {
87 InterfaceCollection::iterator it;
89 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
94 mDnsForwarders->clear();
95 mForwardingRequests.clear();
98 bool TetherController::setIpFwdEnabled() {
100 const char* value = mForwardingRequests.empty() ? "0" : "1";
101 ALOGD("Setting IP forward enable = %s", value);
102 success &= writeToFile(IPV4_FORWARDING_PROC_FILE, value);
103 success &= writeToFile(IPV6_FORWARDING_PROC_FILE, value);
107 bool TetherController::enableForwarding(const char* requester) {
108 // Don't return an error if this requester already requested forwarding. Only return errors for
109 // things that the caller caller needs to care about, such as "couldn't write to the file to
110 // enable forwarding".
111 mForwardingRequests.insert(requester);
112 return setIpFwdEnabled();
115 bool TetherController::disableForwarding(const char* requester) {
116 mForwardingRequests.erase(requester);
117 return setIpFwdEnabled();
120 size_t TetherController::forwardingRequestCount() {
121 return mForwardingRequests.size();
124 #define TETHER_START_CONST_ARG 8
126 int TetherController::startTethering(int num_addrs, char **dhcp_ranges) {
127 if (mDaemonPid != 0) {
128 ALOGE("Tethering already started");
133 ALOGD("Starting tethering services");
138 if (pipe(pipefd) < 0) {
139 ALOGE("pipe failed (%s)", strerror(errno));
144 * TODO: Create a monitoring thread to handle and restart
145 * the daemon if it exits prematurely
147 if ((pid = fork()) < 0) {
148 ALOGE("fork failed (%s)", strerror(errno));
156 if (pipefd[0] != STDIN_FILENO) {
157 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
158 ALOGE("dup2 failed (%s)", strerror(errno));
164 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1;
165 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
166 args[num_processed_args - 1] = NULL;
167 args[0] = (char *)"/system/bin/dnsmasq";
168 args[1] = (char *)"--keep-in-foreground";
169 args[2] = (char *)"--no-resolv";
170 args[3] = (char *)"--no-poll";
171 args[4] = (char *)"--dhcp-authoritative";
172 // TODO: pipe through metered status from ConnService
173 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
174 args[6] = (char *)"--pid-file";
175 args[7] = (char *)"";
177 int nextArg = TETHER_START_CONST_ARG;
178 for (int addrIndex = 0; addrIndex < num_addrs; addrIndex += 2) {
179 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h",
180 dhcp_ranges[addrIndex], dhcp_ranges[addrIndex+1]);
183 if (execv(args[0], args)) {
184 ALOGE("execl failed (%s)", strerror(errno));
186 ALOGE("Should never get here!");
191 mDaemonFd = pipefd[1];
192 applyDnsInterfaces();
193 ALOGD("Tethering services running");
199 int TetherController::stopTethering() {
201 if (mDaemonPid == 0) {
202 ALOGE("Tethering already stopped");
206 ALOGD("Stopping tethering services");
208 kill(mDaemonPid, SIGTERM);
209 waitpid(mDaemonPid, NULL, 0);
213 ALOGD("Tethering services stopped");
217 bool TetherController::isTetheringStarted() {
218 return (mDaemonPid == 0 ? false : true);
221 #define MAX_CMD_SIZE 1024
223 int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
225 char daemonCmd[MAX_CMD_SIZE];
228 fwmark.netId = netId;
229 fwmark.explicitlySelected = true;
230 fwmark.protectedFromVpn = true;
231 fwmark.permission = PERMISSION_SYSTEM;
233 snprintf(daemonCmd, sizeof(daemonCmd), "update_dns%s0x%x", SEPARATOR, fwmark.intValue);
234 int cmdLen = strlen(daemonCmd);
236 mDnsForwarders->clear();
237 for (i = 0; i < numServers; i++) {
238 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
240 addrinfo *res, hints = { .ai_flags = AI_NUMERICHOST };
241 int ret = getaddrinfo(servers[i], NULL, &hints, &res);
244 ALOGE("Failed to parse DNS server '%s'", servers[i]);
245 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, SEPARATOR);
257 strcat(daemonCmd, servers[i]);
258 mDnsForwarders->push_back(servers[i]);
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();
274 unsigned TetherController::getDnsNetId() {
278 NetAddressCollection *TetherController::getDnsForwarders() {
279 return mDnsForwarders;
282 int TetherController::applyDnsInterfaces() {
283 char daemonCmd[MAX_CMD_SIZE];
285 strcpy(daemonCmd, "update_ifaces");
286 int cmdLen = strlen(daemonCmd);
287 InterfaceCollection::iterator it;
288 bool haveInterfaces = false;
290 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
291 cmdLen += (strlen(*it) + 1);
292 if (cmdLen + 1 >= MAX_CMD_SIZE) {
293 ALOGD("Too many DNS ifaces listed");
297 strcat(daemonCmd, SEPARATOR);
298 strcat(daemonCmd, *it);
299 haveInterfaces = true;
302 if ((mDaemonFd != -1) && haveInterfaces) {
303 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
304 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
305 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
312 int TetherController::tetherInterface(const char *interface) {
313 ALOGD("tetherInterface(%s)", interface);
314 if (!isIfaceName(interface)) {
318 mInterfaces->push_back(strdup(interface));
320 if (applyDnsInterfaces()) {
321 InterfaceCollection::iterator it;
322 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
323 if (!strcmp(interface, *it)) {
325 mInterfaces->erase(it);
335 int TetherController::untetherInterface(const char *interface) {
336 InterfaceCollection::iterator it;
338 ALOGD("untetherInterface(%s)", interface);
340 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
341 if (!strcmp(interface, *it)) {
343 mInterfaces->erase(it);
345 return applyDnsInterfaces();
352 InterfaceCollection *TetherController::getTetheredInterfaceList() {