blob: 99c82b8241af2005ea7af43a5372ab513e337ca0 [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 */
Bernie Innocenti51a0e0f2018-10-05 20:24:06 +090016
17#include "ClatdController.h"
18
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090019#include <map>
20#include <string>
21
Daniel Drown0da73fc2012-06-20 16:51:39 -050022#include <errno.h>
Luke Huang94c43a12019-02-14 19:51:38 +080023#include <spawn.h>
Daniel Drown0da73fc2012-06-20 16:51:39 -050024#include <sys/types.h>
25#include <sys/wait.h>
Luke Huang94c43a12019-02-14 19:51:38 +080026#include <unistd.h>
Daniel Drown0da73fc2012-06-20 16:51:39 -050027
28#define LOG_TAG "ClatdController"
Logan Chien3f461482018-04-23 14:31:32 +080029#include <log/log.h>
Daniel Drown0da73fc2012-06-20 16:51:39 -050030
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"
Bernie Innocenti189eb502018-10-01 23:10:18 +090034#include "netid_client.h"
Daniel Drown0da73fc2012-06-20 16:51:39 -050035
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090036static const char* kClatdPath = "/system/bin/clatd";
37
Lorenzo Colitti7035f222017-02-13 18:29:00 +090038namespace android {
39namespace net {
40
Paul Jensen84c1d032014-05-30 13:29:41 -040041ClatdController::ClatdController(NetworkController* controller)
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090042 : mNetCtrl(controller) {
Daniel Drown0da73fc2012-06-20 16:51:39 -050043}
44
45ClatdController::~ClatdController() {
46}
47
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090048// Returns the PID of the clatd running on interface |interface|, or 0 if clatd is not running on
49// |interface|.
Luke Huang6d301232018-08-01 14:05:18 +080050pid_t ClatdController::getClatdPid(const char* interface) {
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090051 auto it = mClatdPids.find(interface);
52 return (it == mClatdPids.end() ? 0 : it->second);
53}
Daniel Drown0da73fc2012-06-20 16:51:39 -050054
Luke Huang6d301232018-08-01 14:05:18 +080055int ClatdController::startClatd(const char* interface) {
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090056 pid_t pid = getClatdPid(interface);
57
58 if (pid != 0) {
59 ALOGE("clatd pid=%d already started on %s", pid, interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -050060 errno = EBUSY;
Luke Huang6d301232018-08-01 14:05:18 +080061 return -errno;
Daniel Drown0da73fc2012-06-20 16:51:39 -050062 }
63
Lorenzo Colitti32b2e792015-01-07 15:11:30 +090064 // Pass in the interface, a netid to use for DNS lookups, and a fwmark for outgoing packets.
65 unsigned netId = mNetCtrl->getNetworkForInterface(interface);
66 if (netId == NETID_UNSET) {
67 ALOGE("interface %s not assigned to any netId", interface);
68 errno = ENODEV;
Luke Huang6d301232018-08-01 14:05:18 +080069 return -errno;
Lorenzo Colitti32b2e792015-01-07 15:11:30 +090070 }
71
72 char netIdString[UINT32_STRLEN];
73 snprintf(netIdString, sizeof(netIdString), "%u", netId);
74
75 Fwmark fwmark;
76 fwmark.netId = netId;
77 fwmark.explicitlySelected = true;
78 fwmark.protectedFromVpn = true;
79 fwmark.permission = PERMISSION_SYSTEM;
80
81 char fwmarkString[UINT32_HEX_STRLEN];
82 snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue);
83
Luke Huang6d301232018-08-01 14:05:18 +080084 char* interfaceName = const_cast<char*>(interface);
85
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090086 ALOGD("starting clatd on %s", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -070087
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090088 std::string progname("clatd-");
89 progname += interface;
Daniel Drown0da73fc2012-06-20 16:51:39 -050090
Luke Huang94c43a12019-02-14 19:51:38 +080091 char* args[] = {(char*)progname.c_str(),
92 (char*)"-i",
93 interfaceName,
94 (char*)"-n",
95 netIdString,
96 (char*)"-m",
97 fwmarkString,
98 nullptr};
99 // Specify no flags and no actions, posix_spawn will use vfork and is
100 // guaranteed to return only once exec has been called.
101 if (posix_spawn(&pid, kClatdPath, nullptr, nullptr, args, nullptr)) {
Luke Huang6d301232018-08-01 14:05:18 +0800102 int res = errno;
Luke Huang94c43a12019-02-14 19:51:38 +0800103 ALOGE("posix_spawn failed (%s)", strerror(errno));
Luke Huang6d301232018-08-01 14:05:18 +0800104 return -res;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500105 }
106
Luke Huang94c43a12019-02-14 19:51:38 +0800107 mClatdPids[interface] = pid;
108 ALOGD("clatd started on %s", interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500109
110 return 0;
111}
112
Luke Huang6d301232018-08-01 14:05:18 +0800113int ClatdController::stopClatd(const char* interface) {
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900114 pid_t pid = getClatdPid(interface);
115
116 if (pid == 0) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500117 ALOGE("clatd already stopped");
Luke Huang6d301232018-08-01 14:05:18 +0800118 return -EREMOTEIO;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500119 }
120
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900121 ALOGD("Stopping clatd pid=%d on %s", pid, interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500122
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900123 kill(pid, SIGTERM);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700124 waitpid(pid, nullptr, 0);
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900125 mClatdPids.erase(interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500126
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900127 ALOGD("clatd on %s stopped", interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500128
129 return 0;
130}
131
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900132} // namespace net
133} // namespace android