blob: 1b54902837af70d2fd60746ad113b106b8881e69 [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 <unistd.h>
23#include <errno.h>
24#include <sys/types.h>
25#include <sys/wait.h>
26
27#define LOG_TAG "ClatdController"
Logan Chien3f461482018-04-23 14:31:32 +080028#include <log/log.h>
Daniel Drown0da73fc2012-06-20 16:51:39 -050029
Lorenzo Colitti45d3dd02014-06-09 14:09:20 +090030#include "Fwmark.h"
Paul Jensen84c1d032014-05-30 13:29:41 -040031#include "NetdConstants.h"
32#include "NetworkController.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090033#include "netid_client.h"
Daniel Drown0da73fc2012-06-20 16:51:39 -050034
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090035static const char* kClatdPath = "/system/bin/clatd";
36
Lorenzo Colitti7035f222017-02-13 18:29:00 +090037namespace android {
38namespace net {
39
Paul Jensen84c1d032014-05-30 13:29:41 -040040ClatdController::ClatdController(NetworkController* controller)
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090041 : mNetCtrl(controller) {
Daniel Drown0da73fc2012-06-20 16:51:39 -050042}
43
44ClatdController::~ClatdController() {
45}
46
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090047// Returns the PID of the clatd running on interface |interface|, or 0 if clatd is not running on
48// |interface|.
Luke Huang6d301232018-08-01 14:05:18 +080049pid_t ClatdController::getClatdPid(const char* interface) {
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090050 auto it = mClatdPids.find(interface);
51 return (it == mClatdPids.end() ? 0 : it->second);
52}
Daniel Drown0da73fc2012-06-20 16:51:39 -050053
Luke Huang6d301232018-08-01 14:05:18 +080054int ClatdController::startClatd(const char* interface) {
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090055 pid_t pid = getClatdPid(interface);
56
57 if (pid != 0) {
58 ALOGE("clatd pid=%d already started on %s", pid, interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -050059 errno = EBUSY;
Luke Huang6d301232018-08-01 14:05:18 +080060 return -errno;
Daniel Drown0da73fc2012-06-20 16:51:39 -050061 }
62
Lorenzo Colitti32b2e792015-01-07 15:11:30 +090063 // Pass in the interface, a netid to use for DNS lookups, and a fwmark for outgoing packets.
64 unsigned netId = mNetCtrl->getNetworkForInterface(interface);
65 if (netId == NETID_UNSET) {
66 ALOGE("interface %s not assigned to any netId", interface);
67 errno = ENODEV;
Luke Huang6d301232018-08-01 14:05:18 +080068 return -errno;
Lorenzo Colitti32b2e792015-01-07 15:11:30 +090069 }
70
71 char netIdString[UINT32_STRLEN];
72 snprintf(netIdString, sizeof(netIdString), "%u", netId);
73
74 Fwmark fwmark;
75 fwmark.netId = netId;
76 fwmark.explicitlySelected = true;
77 fwmark.protectedFromVpn = true;
78 fwmark.permission = PERMISSION_SYSTEM;
79
80 char fwmarkString[UINT32_HEX_STRLEN];
81 snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue);
82
Luke Huang6d301232018-08-01 14:05:18 +080083 char* interfaceName = const_cast<char*>(interface);
84
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090085 ALOGD("starting clatd on %s", interface);
JP Abgrall69261cb2014-06-19 18:35:24 -070086
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090087 std::string progname("clatd-");
88 progname += interface;
Daniel Drown0da73fc2012-06-20 16:51:39 -050089
90 if ((pid = fork()) < 0) {
Luke Huang6d301232018-08-01 14:05:18 +080091 int res = errno;
Daniel Drown0da73fc2012-06-20 16:51:39 -050092 ALOGE("fork failed (%s)", strerror(errno));
Luke Huang6d301232018-08-01 14:05:18 +080093 return -res;
Daniel Drown0da73fc2012-06-20 16:51:39 -050094 }
95
96 if (!pid) {
Luke Huang6d301232018-08-01 14:05:18 +080097 char* args[] = {(char*) progname.c_str(),
98 (char*) "-i",
99 interfaceName,
100 (char*) "-n",
101 netIdString,
102 (char*) "-m",
103 fwmarkString,
104 nullptr};
Daniel Drown0da73fc2012-06-20 16:51:39 -0500105
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900106 if (execv(kClatdPath, args)) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500107 ALOGE("execv failed (%s)", strerror(errno));
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900108 _exit(1);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500109 }
110 ALOGE("Should never get here!");
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900111 _exit(1);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500112 } else {
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900113 mClatdPids[interface] = pid;
114 ALOGD("clatd started on %s", interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500115 }
116
117 return 0;
118}
119
Luke Huang6d301232018-08-01 14:05:18 +0800120int ClatdController::stopClatd(const char* interface) {
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900121 pid_t pid = getClatdPid(interface);
122
123 if (pid == 0) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500124 ALOGE("clatd already stopped");
Luke Huang6d301232018-08-01 14:05:18 +0800125 return -EREMOTEIO;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500126 }
127
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900128 ALOGD("Stopping clatd pid=%d on %s", pid, interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500129
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900130 kill(pid, SIGTERM);
Yi Kongbdfd57e2018-07-25 13:26:10 -0700131 waitpid(pid, nullptr, 0);
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900132 mClatdPids.erase(interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500133
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900134 ALOGD("clatd on %s stopped", interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500135
136 return 0;
137}
138
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900139} // namespace net
140} // namespace android