TetherController.cpp revision 69261cb65186e27dfbdc1e3eec796437f9968ff9
1/* 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> 19#include <fcntl.h> 20#include <string.h> 21 22#include <sys/socket.h> 23#include <sys/stat.h> 24#include <sys/types.h> 25#include <sys/wait.h> 26 27#include <netinet/in.h> 28#include <arpa/inet.h> 29 30#define LOG_TAG "TetherController" 31#include <cutils/log.h> 32#include <cutils/properties.h> 33 34#include "NetdConstants.h" 35#include "TetherController.h" 36 37TetherController::TetherController() { 38 mInterfaces = new InterfaceCollection(); 39 mDnsForwarders = new NetAddressCollection(); 40 mDaemonFd = -1; 41 mDaemonPid = 0; 42} 43 44TetherController::~TetherController() { 45 InterfaceCollection::iterator it; 46 47 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) { 48 free(*it); 49 } 50 mInterfaces->clear(); 51 52 mDnsForwarders->clear(); 53} 54 55int TetherController::setIpFwdEnabled(bool enable) { 56 57 ALOGD("Setting IP forward enable = %d", enable); 58 59 // In BP tools mode, do not disable IP forwarding 60 char bootmode[PROPERTY_VALUE_MAX] = {0}; 61 property_get("ro.bootmode", bootmode, "unknown"); 62 if ((enable == false) && (0 == strcmp("bp-tools", bootmode))) { 63 return 0; 64 } 65 66 int fd = open("/proc/sys/net/ipv4/ip_forward", O_WRONLY); 67 if (fd < 0) { 68 ALOGE("Failed to open ip_forward (%s)", strerror(errno)); 69 return -1; 70 } 71 72 if (write(fd, (enable ? "1" : "0"), 1) != 1) { 73 ALOGE("Failed to write ip_forward (%s)", strerror(errno)); 74 close(fd); 75 return -1; 76 } 77 close(fd); 78 return 0; 79} 80 81bool TetherController::getIpFwdEnabled() { 82 int fd = open("/proc/sys/net/ipv4/ip_forward", O_RDONLY); 83 84 if (fd < 0) { 85 ALOGE("Failed to open ip_forward (%s)", strerror(errno)); 86 return false; 87 } 88 89 char enabled; 90 if (read(fd, &enabled, 1) != 1) { 91 ALOGE("Failed to read ip_forward (%s)", strerror(errno)); 92 close(fd); 93 return -1; 94 } 95 96 close(fd); 97 return (enabled == '1' ? true : false); 98} 99 100#define TETHER_START_CONST_ARG 8 101 102int TetherController::startTethering(int num_addrs, struct in_addr* addrs) { 103 if (mDaemonPid != 0) { 104 ALOGE("Tethering already started"); 105 errno = EBUSY; 106 return -1; 107 } 108 109 ALOGD("Starting tethering services"); 110 111 pid_t pid; 112 int pipefd[2]; 113 114 if (pipe(pipefd) < 0) { 115 ALOGE("pipe failed (%s)", strerror(errno)); 116 return -1; 117 } 118 119 /* 120 * TODO: Create a monitoring thread to handle and restart 121 * the daemon if it exits prematurely 122 */ 123 if ((pid = fork()) < 0) { 124 ALOGE("fork failed (%s)", strerror(errno)); 125 close(pipefd[0]); 126 close(pipefd[1]); 127 return -1; 128 } 129 130 if (!pid) { 131 close(pipefd[1]); 132 if (pipefd[0] != STDIN_FILENO) { 133 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) { 134 ALOGE("dup2 failed (%s)", strerror(errno)); 135 return -1; 136 } 137 close(pipefd[0]); 138 } 139 140 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1; 141 char **args = (char **)malloc(sizeof(char *) * num_processed_args); 142 args[num_processed_args - 1] = NULL; 143 args[0] = (char *)"/system/bin/dnsmasq"; 144 args[1] = (char *)"--keep-in-foreground"; 145 args[2] = (char *)"--no-resolv"; 146 args[3] = (char *)"--no-poll"; 147 args[4] = (char *)"--dhcp-authoritative"; 148 // TODO: pipe through metered status from ConnService 149 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED"; 150 args[6] = (char *)"--pid-file"; 151 args[7] = (char *)""; 152 153 int nextArg = TETHER_START_CONST_ARG; 154 for (int addrIndex=0; addrIndex < num_addrs;) { 155 char *start = strdup(inet_ntoa(addrs[addrIndex++])); 156 char *end = strdup(inet_ntoa(addrs[addrIndex++])); 157 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end); 158 } 159 160 if (execv(args[0], args)) { 161 ALOGE("execl failed (%s)", strerror(errno)); 162 } 163 ALOGE("Should never get here!"); 164 _exit(-1); 165 } else { 166 close(pipefd[0]); 167 mDaemonPid = pid; 168 mDaemonFd = pipefd[1]; 169 applyDnsInterfaces(); 170 ALOGD("Tethering services running"); 171 } 172 173 return 0; 174} 175 176int TetherController::stopTethering() { 177 178 if (mDaemonPid == 0) { 179 ALOGE("Tethering already stopped"); 180 return 0; 181 } 182 183 ALOGD("Stopping tethering services"); 184 185 kill(mDaemonPid, SIGTERM); 186 waitpid(mDaemonPid, NULL, 0); 187 mDaemonPid = 0; 188 close(mDaemonFd); 189 mDaemonFd = -1; 190 ALOGD("Tethering services stopped"); 191 return 0; 192} 193 194bool TetherController::isTetheringStarted() { 195 return (mDaemonPid == 0 ? false : true); 196} 197 198#define MAX_CMD_SIZE 1024 199 200int TetherController::setDnsForwarders(char **servers, int numServers) { 201 int i; 202 char daemonCmd[MAX_CMD_SIZE]; 203 204 strcpy(daemonCmd, "update_dns"); 205 int cmdLen = strlen(daemonCmd); 206 207 mDnsForwarders->clear(); 208 for (i = 0; i < numServers; i++) { 209 ALOGD("setDnsForwarders(%d = '%s')", i, servers[i]); 210 211 struct in_addr a; 212 213 if (!inet_aton(servers[i], &a)) { 214 ALOGE("Failed to parse DNS server '%s'", servers[i]); 215 mDnsForwarders->clear(); 216 return -1; 217 } 218 219 cmdLen += (strlen(servers[i]) + 1); 220 if (cmdLen + 1 >= MAX_CMD_SIZE) { 221 ALOGD("Too many DNS servers listed"); 222 break; 223 } 224 225 strcat(daemonCmd, ":"); 226 strcat(daemonCmd, servers[i]); 227 mDnsForwarders->push_back(a); 228 } 229 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(); 235 return -1; 236 } 237 } 238 return 0; 239} 240 241NetAddressCollection *TetherController::getDnsForwarders() { 242 return mDnsForwarders; 243} 244 245int TetherController::applyDnsInterfaces() { 246 char daemonCmd[MAX_CMD_SIZE]; 247 248 strcpy(daemonCmd, "update_ifaces"); 249 int cmdLen = strlen(daemonCmd); 250 InterfaceCollection::iterator it; 251 bool haveInterfaces = false; 252 253 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) { 254 cmdLen += (strlen(*it) + 1); 255 if (cmdLen + 1 >= MAX_CMD_SIZE) { 256 ALOGD("Too many DNS ifaces listed"); 257 break; 258 } 259 260 strcat(daemonCmd, ":"); 261 strcat(daemonCmd, *it); 262 haveInterfaces = true; 263 } 264 265 if ((mDaemonFd != -1) && haveInterfaces) { 266 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd); 267 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) { 268 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno)); 269 return -1; 270 } 271 } 272 return 0; 273} 274 275int TetherController::tetherInterface(const char *interface) { 276 ALOGD("tetherInterface(%s)", interface); 277 if (!isIfaceName(interface)) { 278 errno = ENOENT; 279 return -1; 280 } 281 mInterfaces->push_back(strdup(interface)); 282 283 if (applyDnsInterfaces()) { 284 InterfaceCollection::iterator it; 285 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) { 286 if (!strcmp(interface, *it)) { 287 free(*it); 288 mInterfaces->erase(it); 289 break; 290 } 291 } 292 return -1; 293 } else { 294 return 0; 295 } 296} 297 298int TetherController::untetherInterface(const char *interface) { 299 InterfaceCollection::iterator it; 300 301 ALOGD("untetherInterface(%s)", interface); 302 303 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) { 304 if (!strcmp(interface, *it)) { 305 free(*it); 306 mInterfaces->erase(it); 307 308 return applyDnsInterfaces(); 309 } 310 } 311 errno = ENOENT; 312 return -1; 313} 314 315InterfaceCollection *TetherController::getTetheredInterfaceList() { 316 return mInterfaces; 317} 318