blob: 4e1c52f4765326cdd3b4bf81aac79e8a163a2538 [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
Lorenzo Colitti667c4772014-08-26 14:13:07 -070034#include "Fwmark.h"
JP Abgrall69261cb2014-06-19 18:35:24 -070035#include "NetdConstants.h"
Lorenzo Colitti667c4772014-08-26 14:13:07 -070036#include "Permission.h"
San Mehat9d10b342010-01-18 09:51:02 -080037#include "TetherController.h"
38
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070039TetherController::TetherController() {
San Mehat9d10b342010-01-18 09:51:02 -080040 mInterfaces = new InterfaceCollection();
Lorenzo Colitti667c4772014-08-26 14:13:07 -070041 mDnsNetId = 0;
San Mehat9d10b342010-01-18 09:51:02 -080042 mDnsForwarders = new NetAddressCollection();
43 mDaemonFd = -1;
44 mDaemonPid = 0;
45}
46
47TetherController::~TetherController() {
48 InterfaceCollection::iterator it;
49
50 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
51 free(*it);
52 }
53 mInterfaces->clear();
54
55 mDnsForwarders->clear();
56}
57
58int TetherController::setIpFwdEnabled(bool enable) {
59
Steve Block7b984e32011-12-20 16:22:42 +000060 ALOGD("Setting IP forward enable = %d", enable);
Kazuhiro Ondo6b858eb2011-06-24 20:31:03 -050061
62 // In BP tools mode, do not disable IP forwarding
63 char bootmode[PROPERTY_VALUE_MAX] = {0};
64 property_get("ro.bootmode", bootmode, "unknown");
65 if ((enable == false) && (0 == strcmp("bp-tools", bootmode))) {
66 return 0;
67 }
68
San Mehat9d10b342010-01-18 09:51:02 -080069 int fd = open("/proc/sys/net/ipv4/ip_forward", O_WRONLY);
70 if (fd < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000071 ALOGE("Failed to open ip_forward (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -080072 return -1;
73 }
74
75 if (write(fd, (enable ? "1" : "0"), 1) != 1) {
Steve Block5ea0c052012-01-06 19:18:11 +000076 ALOGE("Failed to write ip_forward (%s)", strerror(errno));
Robert Greenwalt37dc4a52010-04-28 16:05:04 -070077 close(fd);
San Mehat9d10b342010-01-18 09:51:02 -080078 return -1;
79 }
80 close(fd);
81 return 0;
82}
83
84bool TetherController::getIpFwdEnabled() {
85 int fd = open("/proc/sys/net/ipv4/ip_forward", O_RDONLY);
86
87 if (fd < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000088 ALOGE("Failed to open ip_forward (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -080089 return false;
90 }
91
92 char enabled;
93 if (read(fd, &enabled, 1) != 1) {
Steve Block5ea0c052012-01-06 19:18:11 +000094 ALOGE("Failed to read ip_forward (%s)", strerror(errno));
Robert Greenwalt37dc4a52010-04-28 16:05:04 -070095 close(fd);
San Mehat9d10b342010-01-18 09:51:02 -080096 return -1;
97 }
98
99 close(fd);
San Mehat9d10b342010-01-18 09:51:02 -0800100 return (enabled == '1' ? true : false);
101}
102
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800103#define TETHER_START_CONST_ARG 8
104
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700105int TetherController::startTethering(int num_addrs, struct in_addr* addrs) {
San Mehat9d10b342010-01-18 09:51:02 -0800106 if (mDaemonPid != 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000107 ALOGE("Tethering already started");
San Mehat9d10b342010-01-18 09:51:02 -0800108 errno = EBUSY;
109 return -1;
110 }
111
Steve Block7b984e32011-12-20 16:22:42 +0000112 ALOGD("Starting tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800113
114 pid_t pid;
115 int pipefd[2];
116
117 if (pipe(pipefd) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000118 ALOGE("pipe failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800119 return -1;
120 }
121
122 /*
123 * TODO: Create a monitoring thread to handle and restart
124 * the daemon if it exits prematurely
125 */
126 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000127 ALOGE("fork failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800128 close(pipefd[0]);
129 close(pipefd[1]);
130 return -1;
131 }
132
133 if (!pid) {
134 close(pipefd[1]);
135 if (pipefd[0] != STDIN_FILENO) {
136 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
Steve Block5ea0c052012-01-06 19:18:11 +0000137 ALOGE("dup2 failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800138 return -1;
139 }
140 close(pipefd[0]);
141 }
San Mehat9d10b342010-01-18 09:51:02 -0800142
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800143 int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700144 char **args = (char **)malloc(sizeof(char *) * num_processed_args);
145 args[num_processed_args - 1] = NULL;
146 args[0] = (char *)"/system/bin/dnsmasq";
Peter Nilssonb756f692011-09-08 09:48:31 -0700147 args[1] = (char *)"--keep-in-foreground";
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700148 args[2] = (char *)"--no-resolv";
149 args[3] = (char *)"--no-poll";
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800150 args[4] = (char *)"--dhcp-authoritative";
Jeff Sharkey6df79da2012-04-18 21:53:35 -0700151 // TODO: pipe through metered status from ConnService
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800152 args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED";
153 args[6] = (char *)"--pid-file";
154 args[7] = (char *)"";
San Mehat9d10b342010-01-18 09:51:02 -0800155
Dmitry Shmidtbc775ed2013-12-12 16:41:16 -0800156 int nextArg = TETHER_START_CONST_ARG;
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700157 for (int addrIndex=0; addrIndex < num_addrs;) {
158 char *start = strdup(inet_ntoa(addrs[addrIndex++]));
159 char *end = strdup(inet_ntoa(addrs[addrIndex++]));
160 asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end);
Jesper Hanssona9d791f2012-04-27 13:54:27 +0200161 free(start);
162 free(end);
Robert Greenwalt3208ea02010-03-24 16:32:55 -0700163 }
164
165 if (execv(args[0], args)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000166 ALOGE("execl failed (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800167 }
Steve Block5ea0c052012-01-06 19:18:11 +0000168 ALOGE("Should never get here!");
JP Abgrallce4f3792012-08-06 13:44:44 -0700169 _exit(-1);
San Mehat9d10b342010-01-18 09:51:02 -0800170 } else {
171 close(pipefd[0]);
172 mDaemonPid = pid;
173 mDaemonFd = pipefd[1];
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800174 applyDnsInterfaces();
Steve Block7b984e32011-12-20 16:22:42 +0000175 ALOGD("Tethering services running");
San Mehat9d10b342010-01-18 09:51:02 -0800176 }
177
178 return 0;
179}
180
181int TetherController::stopTethering() {
182
183 if (mDaemonPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000184 ALOGE("Tethering already stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800185 return 0;
186 }
187
Steve Block7b984e32011-12-20 16:22:42 +0000188 ALOGD("Stopping tethering services");
San Mehat9d10b342010-01-18 09:51:02 -0800189
190 kill(mDaemonPid, SIGTERM);
San Mehat18737842010-01-21 09:22:43 -0800191 waitpid(mDaemonPid, NULL, 0);
San Mehat9d10b342010-01-18 09:51:02 -0800192 mDaemonPid = 0;
193 close(mDaemonFd);
194 mDaemonFd = -1;
Steve Block7b984e32011-12-20 16:22:42 +0000195 ALOGD("Tethering services stopped");
San Mehat9d10b342010-01-18 09:51:02 -0800196 return 0;
197}
Matthew Xie19944102012-07-12 16:42:07 -0700198
San Mehat9d10b342010-01-18 09:51:02 -0800199bool TetherController::isTetheringStarted() {
200 return (mDaemonPid == 0 ? false : true);
201}
202
Kenny Rootcf52faf2010-02-18 09:59:55 -0800203#define MAX_CMD_SIZE 1024
204
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700205int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
San Mehat9d10b342010-01-18 09:51:02 -0800206 int i;
Kenny Rootcf52faf2010-02-18 09:59:55 -0800207 char daemonCmd[MAX_CMD_SIZE];
San Mehat9d10b342010-01-18 09:51:02 -0800208
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700209 Fwmark fwmark;
210 fwmark.netId = netId;
211 fwmark.explicitlySelected = true;
212 fwmark.protectedFromVpn = true;
213 fwmark.permission = PERMISSION_SYSTEM;
214
215 snprintf(daemonCmd, sizeof(daemonCmd), "update_dns:0x%x", fwmark.intValue);
Kenny Rootcf52faf2010-02-18 09:59:55 -0800216 int cmdLen = strlen(daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800217
218 mDnsForwarders->clear();
219 for (i = 0; i < numServers; i++) {
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700220 ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800221
222 struct in_addr a;
223
224 if (!inet_aton(servers[i], &a)) {
Steve Block5ea0c052012-01-06 19:18:11 +0000225 ALOGE("Failed to parse DNS server '%s'", servers[i]);
San Mehat9d10b342010-01-18 09:51:02 -0800226 mDnsForwarders->clear();
227 return -1;
228 }
Kenny Rootcf52faf2010-02-18 09:59:55 -0800229
Nick Kralevichad5b41f2012-07-19 18:48:05 -0700230 cmdLen += (strlen(servers[i]) + 1);
231 if (cmdLen + 1 >= MAX_CMD_SIZE) {
Steve Block7b984e32011-12-20 16:22:42 +0000232 ALOGD("Too many DNS servers listed");
Kenny Rootcf52faf2010-02-18 09:59:55 -0800233 break;
234 }
235
San Mehat9d10b342010-01-18 09:51:02 -0800236 strcat(daemonCmd, ":");
237 strcat(daemonCmd, servers[i]);
238 mDnsForwarders->push_back(a);
239 }
240
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700241 mDnsNetId = netId;
San Mehat9d10b342010-01-18 09:51:02 -0800242 if (mDaemonFd != -1) {
Steve Block7b984e32011-12-20 16:22:42 +0000243 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
San Mehat9d10b342010-01-18 09:51:02 -0800244 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000245 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
San Mehat9d10b342010-01-18 09:51:02 -0800246 mDnsForwarders->clear();
247 return -1;
248 }
249 }
250 return 0;
251}
252
Lorenzo Colitti667c4772014-08-26 14:13:07 -0700253unsigned TetherController::getDnsNetId() {
254 return mDnsNetId;
255}
256
San Mehat9d10b342010-01-18 09:51:02 -0800257NetAddressCollection *TetherController::getDnsForwarders() {
258 return mDnsForwarders;
259}
260
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800261int TetherController::applyDnsInterfaces() {
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800262 char daemonCmd[MAX_CMD_SIZE];
263
264 strcpy(daemonCmd, "update_ifaces");
265 int cmdLen = strlen(daemonCmd);
266 InterfaceCollection::iterator it;
267 bool haveInterfaces = false;
268
269 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
270 cmdLen += (strlen(*it) + 1);
271 if (cmdLen + 1 >= MAX_CMD_SIZE) {
272 ALOGD("Too many DNS ifaces listed");
273 break;
274 }
275
276 strcat(daemonCmd, ":");
277 strcat(daemonCmd, *it);
278 haveInterfaces = true;
279 }
280
281 if ((mDaemonFd != -1) && haveInterfaces) {
282 ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd);
283 if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) {
284 ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno));
285 return -1;
286 }
287 }
San Mehat9d10b342010-01-18 09:51:02 -0800288 return 0;
289}
290
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800291int TetherController::tetherInterface(const char *interface) {
292 ALOGD("tetherInterface(%s)", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -0700293 if (!isIfaceName(interface)) {
294 errno = ENOENT;
295 return -1;
296 }
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800297 mInterfaces->push_back(strdup(interface));
298
299 if (applyDnsInterfaces()) {
300 InterfaceCollection::iterator it;
301 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
302 if (!strcmp(interface, *it)) {
303 free(*it);
304 mInterfaces->erase(it);
305 break;
306 }
307 }
308 return -1;
309 } else {
310 return 0;
311 }
312}
313
San Mehat9d10b342010-01-18 09:51:02 -0800314int TetherController::untetherInterface(const char *interface) {
315 InterfaceCollection::iterator it;
316
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800317 ALOGD("untetherInterface(%s)", interface);
318
San Mehat9d10b342010-01-18 09:51:02 -0800319 for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
320 if (!strcmp(interface, *it)) {
321 free(*it);
322 mInterfaces->erase(it);
Robert Greenwalt3d4c7582012-12-11 12:33:37 -0800323
324 return applyDnsInterfaces();
San Mehat9d10b342010-01-18 09:51:02 -0800325 }
326 }
327 errno = ENOENT;
328 return -1;
329}
330
331InterfaceCollection *TetherController::getTetheredInterfaceList() {
332 return mInterfaces;
333}