blob: b97ea320578b6efd0fb06446294c962acafe3282 [file] [log] [blame]
Daniel Drown0da73fc2012-06-20 16:51:39 -05001/*
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 */
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090016#include <map>
17#include <string>
18
Daniel Drown0da73fc2012-06-20 16:51:39 -050019#include <unistd.h>
20#include <errno.h>
21#include <sys/types.h>
22#include <sys/wait.h>
23
24#define LOG_TAG "ClatdController"
25#include <cutils/log.h>
26
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090027#include <resolv_netid.h>
28
JP Abgrall69261cb2014-06-19 18:35:24 -070029#include "NetdConstants.h"
Daniel Drown0da73fc2012-06-20 16:51:39 -050030#include "ClatdController.h"
Lorenzo Colitti45d3dd02014-06-09 14:09:20 +090031#include "Fwmark.h"
Paul Jensen84c1d032014-05-30 13:29:41 -040032#include "NetdConstants.h"
33#include "NetworkController.h"
Daniel Drown0da73fc2012-06-20 16:51:39 -050034
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090035static const char* kClatdPath = "/system/bin/clatd";
36
Paul Jensen84c1d032014-05-30 13:29:41 -040037ClatdController::ClatdController(NetworkController* controller)
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090038 : mNetCtrl(controller) {
Daniel Drown0da73fc2012-06-20 16:51:39 -050039}
40
41ClatdController::~ClatdController() {
42}
43
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090044// Returns the PID of the clatd running on interface |interface|, or 0 if clatd is not running on
45// |interface|.
46pid_t ClatdController::getClatdPid(char* interface) {
47 auto it = mClatdPids.find(interface);
48 return (it == mClatdPids.end() ? 0 : it->second);
49}
Daniel Drown0da73fc2012-06-20 16:51:39 -050050
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090051int ClatdController::startClatd(char* interface) {
52 pid_t pid = getClatdPid(interface);
53
54 if (pid != 0) {
55 ALOGE("clatd pid=%d already started on %s", pid, interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -050056 errno = EBUSY;
57 return -1;
58 }
59
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090060 ALOGD("starting clatd on %s", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -070061
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090062 std::string progname("clatd-");
63 progname += interface;
Daniel Drown0da73fc2012-06-20 16:51:39 -050064
65 if ((pid = fork()) < 0) {
66 ALOGE("fork failed (%s)", strerror(errno));
67 return -1;
68 }
69
70 if (!pid) {
Lorenzo Colitti45d3dd02014-06-09 14:09:20 +090071 // Pass in the interface, a netid to use for DNS lookups, and a fwmark for outgoing packets.
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070072 unsigned netId = mNetCtrl->getNetworkForInterface(interface);
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090073 if (netId == NETID_UNSET) {
74 ALOGE("interface %s not assigned to any netId", interface);
75 errno = ENODEV;
76 return -1;
77 }
78
Lorenzo Colitti45d3dd02014-06-09 14:09:20 +090079 char netIdString[UINT32_STRLEN];
80 snprintf(netIdString, sizeof(netIdString), "%u", netId);
81
Sreeram Ramachandran335f2932014-07-11 16:01:33 -070082 Fwmark fwmark;
Sreeram Ramachandran335f2932014-07-11 16:01:33 -070083 fwmark.netId = netId;
84 fwmark.explicitlySelected = true;
85 fwmark.protectedFromVpn = true;
86 fwmark.permission = PERMISSION_SYSTEM;
87
Lorenzo Colitti45d3dd02014-06-09 14:09:20 +090088 char fwmarkString[UINT32_HEX_STRLEN];
89 snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue);
90
Paul Jensen84c1d032014-05-30 13:29:41 -040091 char *args[] = {
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090092 (char *) progname.c_str(),
Lorenzo Colitti45d3dd02014-06-09 14:09:20 +090093 (char *) "-i",
Paul Jensen84c1d032014-05-30 13:29:41 -040094 interface,
Lorenzo Colitti45d3dd02014-06-09 14:09:20 +090095 (char *) "-n",
96 netIdString,
97 (char *) "-m",
98 fwmarkString,
Paul Jensen84c1d032014-05-30 13:29:41 -040099 NULL
100 };
Daniel Drown0da73fc2012-06-20 16:51:39 -0500101
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900102 if (execv(kClatdPath, args)) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500103 ALOGE("execv failed (%s)", strerror(errno));
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900104 return -1;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500105 }
106 ALOGE("Should never get here!");
Daniel Drown0da73fc2012-06-20 16:51:39 -0500107 _exit(0);
108 } else {
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900109 mClatdPids[interface] = pid;
110 ALOGD("clatd started on %s", interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500111 }
112
113 return 0;
114}
115
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900116int ClatdController::stopClatd(char* interface) {
117 pid_t pid = getClatdPid(interface);
118
119 if (pid == 0) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500120 ALOGE("clatd already stopped");
121 return -1;
122 }
123
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900124 ALOGD("Stopping clatd pid=%d on %s", pid, interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500125
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900126 kill(pid, SIGTERM);
127 waitpid(pid, NULL, 0);
128 mClatdPids.erase(interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500129
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900130 ALOGD("clatd on %s stopped", interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500131
132 return 0;
133}
134
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900135bool ClatdController::isClatdStarted(char* interface) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500136 pid_t waitpid_status;
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900137 pid_t pid = getClatdPid(interface);
138 if (pid == 0) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500139 return false;
140 }
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900141 waitpid_status = waitpid(pid, NULL, WNOHANG);
142 if (waitpid_status != 0) {
143 mClatdPids.erase(interface); // child exited, don't call waitpid on it again
Daniel Drown0da73fc2012-06-20 16:51:39 -0500144 }
145 return waitpid_status == 0; // 0 while child is running
146}