OSDN Git Service

Merge "bundle init.rc contents with its service"
[android-x86/system-netd.git] / server / TetherController.cpp
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 #include <base/file.h>
31 #define LOG_TAG "TetherController"
32 #include <cutils/log.h>
33 #include <cutils/properties.h>
34
35 #include "Fwmark.h"
36 #include "NetdConstants.h"
37 #include "Permission.h"
38 #include "TetherController.h"
39
40 using android::base::ReadFileToString;
41 using android::base::WriteStringToFile;
42
43 TetherController::TetherController() {
44     mInterfaces = new InterfaceCollection();
45     mDnsNetId = 0;
46     mDnsForwarders = new NetAddressCollection();
47     mDaemonFd = -1;
48     mDaemonPid = 0;
49 }
50
51 TetherController::~TetherController() {
52     InterfaceCollection::iterator it;
53
54     for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
55         free(*it);
56     }
57     mInterfaces->clear();
58
59     mDnsForwarders->clear();
60 }
61
62 int TetherController::setIpFwdEnabled(bool enable) {
63
64     ALOGD("Setting IP forward enable = %d", enable);
65
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))) {
70         return 0;
71     }
72
73     if (!WriteStringToFile(enable ? "1" : "0", "/proc/sys/net/ipv4/ip_forward")) {
74         ALOGE("Failed to write ip_forward (%s)", strerror(errno));
75         return -1;
76     }
77
78     return 0;
79 }
80
81 bool TetherController::getIpFwdEnabled() {
82     std::string enabled;
83     if (!ReadFileToString("/proc/sys/net/ipv4/ip_forward", &enabled)) {
84         ALOGE("Failed to read ip_forward (%s)", strerror(errno));
85         return -1;
86     }
87
88     return (enabled == "1" ? true : false);
89 }
90
91 #define TETHER_START_CONST_ARG        8
92
93 int TetherController::startTethering(int num_addrs, struct in_addr* addrs) {
94     if (mDaemonPid != 0) {
95         ALOGE("Tethering already started");
96         errno = EBUSY;
97         return -1;
98     }
99
100     ALOGD("Starting tethering services");
101
102     pid_t pid;
103     int pipefd[2];
104
105     if (pipe(pipefd) < 0) {
106         ALOGE("pipe failed (%s)", strerror(errno));
107         return -1;
108     }
109
110     /*
111      * TODO: Create a monitoring thread to handle and restart
112      * the daemon if it exits prematurely
113      */
114     if ((pid = fork()) < 0) {
115         ALOGE("fork failed (%s)", strerror(errno));
116         close(pipefd[0]);
117         close(pipefd[1]);
118         return -1;
119     }
120
121     if (!pid) {
122         close(pipefd[1]);
123         if (pipefd[0] != STDIN_FILENO) {
124             if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
125                 ALOGE("dup2 failed (%s)", strerror(errno));
126                 return -1;
127             }
128             close(pipefd[0]);
129         }
130
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 *)"";
143
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);
149             free(start);
150             free(end);
151         }
152
153         if (execv(args[0], args)) {
154             ALOGE("execl failed (%s)", strerror(errno));
155         }
156         ALOGE("Should never get here!");
157         _exit(-1);
158     } else {
159         close(pipefd[0]);
160         mDaemonPid = pid;
161         mDaemonFd = pipefd[1];
162         applyDnsInterfaces();
163         ALOGD("Tethering services running");
164     }
165
166     return 0;
167 }
168
169 int TetherController::stopTethering() {
170
171     if (mDaemonPid == 0) {
172         ALOGE("Tethering already stopped");
173         return 0;
174     }
175
176     ALOGD("Stopping tethering services");
177
178     kill(mDaemonPid, SIGTERM);
179     waitpid(mDaemonPid, NULL, 0);
180     mDaemonPid = 0;
181     close(mDaemonFd);
182     mDaemonFd = -1;
183     ALOGD("Tethering services stopped");
184     return 0;
185 }
186
187 bool TetherController::isTetheringStarted() {
188     return (mDaemonPid == 0 ? false : true);
189 }
190
191 #define MAX_CMD_SIZE 1024
192
193 int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) {
194     int i;
195     char daemonCmd[MAX_CMD_SIZE];
196
197     Fwmark fwmark;
198     fwmark.netId = netId;
199     fwmark.explicitlySelected = true;
200     fwmark.protectedFromVpn = true;
201     fwmark.permission = PERMISSION_SYSTEM;
202
203     snprintf(daemonCmd, sizeof(daemonCmd), "update_dns:0x%x", fwmark.intValue);
204     int cmdLen = strlen(daemonCmd);
205
206     mDnsForwarders->clear();
207     for (i = 0; i < numServers; i++) {
208         ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]);
209
210         struct in_addr a;
211
212         if (!inet_aton(servers[i], &a)) {
213             ALOGE("Failed to parse DNS server '%s'", servers[i]);
214             mDnsForwarders->clear();
215             return -1;
216         }
217
218         cmdLen += (strlen(servers[i]) + 1);
219         if (cmdLen + 1 >= MAX_CMD_SIZE) {
220             ALOGD("Too many DNS servers listed");
221             break;
222         }
223
224         strcat(daemonCmd, ":");
225         strcat(daemonCmd, servers[i]);
226         mDnsForwarders->push_back(a);
227     }
228
229     mDnsNetId = netId;
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
241 unsigned TetherController::getDnsNetId() {
242     return mDnsNetId;
243 }
244
245 NetAddressCollection *TetherController::getDnsForwarders() {
246     return mDnsForwarders;
247 }
248
249 int TetherController::applyDnsInterfaces() {
250     char daemonCmd[MAX_CMD_SIZE];
251
252     strcpy(daemonCmd, "update_ifaces");
253     int cmdLen = strlen(daemonCmd);
254     InterfaceCollection::iterator it;
255     bool haveInterfaces = false;
256
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");
261             break;
262         }
263
264         strcat(daemonCmd, ":");
265         strcat(daemonCmd, *it);
266         haveInterfaces = true;
267     }
268
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));
273             return -1;
274         }
275     }
276     return 0;
277 }
278
279 int TetherController::tetherInterface(const char *interface) {
280     ALOGD("tetherInterface(%s)", interface);
281     if (!isIfaceName(interface)) {
282         errno = ENOENT;
283         return -1;
284     }
285     mInterfaces->push_back(strdup(interface));
286
287     if (applyDnsInterfaces()) {
288         InterfaceCollection::iterator it;
289         for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
290             if (!strcmp(interface, *it)) {
291                 free(*it);
292                 mInterfaces->erase(it);
293                 break;
294             }
295         }
296         return -1;
297     } else {
298         return 0;
299     }
300 }
301
302 int TetherController::untetherInterface(const char *interface) {
303     InterfaceCollection::iterator it;
304
305     ALOGD("untetherInterface(%s)", interface);
306
307     for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) {
308         if (!strcmp(interface, *it)) {
309             free(*it);
310             mInterfaces->erase(it);
311
312             return applyDnsInterfaces();
313         }
314     }
315     errno = ENOENT;
316     return -1;
317 }
318
319 InterfaceCollection *TetherController::getTetheredInterfaceList() {
320     return mInterfaces;
321 }