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 #include <base/file.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"
40 using android::base::ReadFileToString;
41 using android::base::WriteStringToFile;
43 TetherController::TetherController() {
44 mInterfaces = new InterfaceCollection();
46 mDnsForwarders = new NetAddressCollection();
51 TetherController::~TetherController() {
52 InterfaceCollection::iterator it;
54 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
59 mDnsForwarders->clear();
62 int TetherController::setIpFwdEnabled(bool enable) {
64 ALOGD("Setting IP forward enable = %d", enable);
66 // In BP tools mode, do not disable IP forwarding
67 char bootmode[PROPERTY_VALUE_MAX] = {0};
68 property_get("ro.bootmode", bootmode, "unknown");
69 if ((enable == false) && (0 == strcmp("bp-tools", bootmode))) {
73 if (!WriteStringToFile(enable ? "1" : "0", "/proc/sys/net/ipv4/ip_forward")) {
74 ALOGE("Failed to write ip_forward (%s)", strerror(errno));
81 bool TetherController::getIpFwdEnabled() {
83 if (!ReadFileToString("/proc/sys/net/ipv4/ip_forward", &enabled)) {
84 ALOGE("Failed to read ip_forward (%s)", strerror(errno));
88 return (enabled == "1" ? true : false);
91 #define TETHER_START_CONST_ARG 8
93 int TetherController::startTethering(int num_addrs, struct in_addr* addrs) {
94 if (mDaemonPid != 0) {
95 ALOGE("Tethering already started");
100 ALOGD("Starting tethering services");
105 if (pipe(pipefd) < 0) {
106 ALOGE("pipe failed (%s)", strerror(errno));
111 * TODO: Create a monitoring thread to handle and restart
112 * the daemon if it exits prematurely
114 if ((pid = fork()) < 0) {
115 ALOGE("fork failed (%s)", strerror(errno));
123 if (pipefd[0] != STDIN_FILENO) {
124 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
125 ALOGE("dup2 failed (%s)", strerror(errno));
131 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1;
132 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
133 args[num_processed_args - 1] = NULL;
134 args[0] = (char *)"/system/bin/dnsmasq";
135 args[1] = (char *)"--keep-in-foreground";
136 args[2] = (char *)"--no-resolv";
137 args[3] = (char *)"--no-poll";
138 args[4] = (char *)"--dhcp-authoritative";
139 // TODO: pipe through metered status from ConnService
140 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
141 args[6] = (char *)"--pid-file";
142 args[7] = (char *)"";
144 int nextArg = TETHER_START_CONST_ARG;
145 for (int addrIndex=0; addrIndex < num_addrs;) {
146 char *start = strdup(inet_ntoa(addrs[addrIndex++]));
147 char *end = strdup(inet_ntoa(addrs[addrIndex++]));
148 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end);
153 if (execv(args[0], args)) {
154 ALOGE("execl failed (%s)", strerror(errno));
156 ALOGE("Should never get here!");
161 mDaemonFd = pipefd[1];
162 applyDnsInterfaces();
163 ALOGD("Tethering services running");
169 int TetherController::stopTethering() {
171 if (mDaemonPid == 0) {
172 ALOGE("Tethering already stopped");
176 ALOGD("Stopping tethering services");
178 kill(mDaemonPid, SIGTERM);
179 waitpid(mDaemonPid, NULL, 0);
183 ALOGD("Tethering services stopped");
187 bool TetherController::isTetheringStarted() {
188 return (mDaemonPid == 0 ? false : true);
191 #define MAX_CMD_SIZE 1024
193 int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
195 char daemonCmd[MAX_CMD_SIZE];
198 fwmark.netId = netId;
199 fwmark.explicitlySelected = true;
200 fwmark.protectedFromVpn = true;
201 fwmark.permission = PERMISSION_SYSTEM;
203 snprintf(daemonCmd, sizeof(daemonCmd), "update_dns:0x%x", fwmark.intValue);
204 int cmdLen = strlen(daemonCmd);
206 mDnsForwarders->clear();
207 for (i = 0; i < numServers; i++) {
208 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
212 if (!inet_aton(servers[i], &a)) {
213 ALOGE("Failed to parse DNS server '%s'", servers[i]);
214 mDnsForwarders->clear();
218 cmdLen += (strlen(servers[i]) + 1);
219 if (cmdLen + 1 >= MAX_CMD_SIZE) {
220 ALOGD("Too many DNS servers listed");
224 strcat(daemonCmd, ":");
225 strcat(daemonCmd, servers[i]);
226 mDnsForwarders->push_back(a);
230 if (mDaemonFd != -1) {
231 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
232 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
233 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
234 mDnsForwarders->clear();
241 unsigned TetherController::getDnsNetId() {
245 NetAddressCollection *TetherController::getDnsForwarders() {
246 return mDnsForwarders;
249 int TetherController::applyDnsInterfaces() {
250 char daemonCmd[MAX_CMD_SIZE];
252 strcpy(daemonCmd, "update_ifaces");
253 int cmdLen = strlen(daemonCmd);
254 InterfaceCollection::iterator it;
255 bool haveInterfaces = false;
257 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
258 cmdLen += (strlen(*it) + 1);
259 if (cmdLen + 1 >= MAX_CMD_SIZE) {
260 ALOGD("Too many DNS ifaces listed");
264 strcat(daemonCmd, ":");
265 strcat(daemonCmd, *it);
266 haveInterfaces = true;
269 if ((mDaemonFd != -1) && haveInterfaces) {
270 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
271 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
272 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
279 int TetherController::tetherInterface(const char *interface) {
280 ALOGD("tetherInterface(%s)", interface);
281 if (!isIfaceName(interface)) {
285 mInterfaces->push_back(strdup(interface));
287 if (applyDnsInterfaces()) {
288 InterfaceCollection::iterator it;
289 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
290 if (!strcmp(interface, *it)) {
292 mInterfaces->erase(it);
302 int TetherController::untetherInterface(const char *interface) {
303 InterfaceCollection::iterator it;
305 ALOGD("untetherInterface(%s)", interface);
307 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
308 if (!strcmp(interface, *it)) {
310 mInterfaces->erase(it);
312 return applyDnsInterfaces();
319 InterfaceCollection *TetherController::getTetheredInterfaceList() {