Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 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 | */ |
Bernie Innocenti | 51a0e0f | 2018-10-05 20:24:06 +0900 | [diff] [blame^] | 16 | |
| 17 | #include "ClatdController.h" |
| 18 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 19 | #include <map> |
| 20 | #include <string> |
| 21 | |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | #include <errno.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
| 26 | |
| 27 | #define LOG_TAG "ClatdController" |
Logan Chien | 3f46148 | 2018-04-23 14:31:32 +0800 | [diff] [blame] | 28 | #include <log/log.h> |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 29 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 30 | #include <resolv_netid.h> |
| 31 | |
Lorenzo Colitti | 45d3dd0 | 2014-06-09 14:09:20 +0900 | [diff] [blame] | 32 | #include "Fwmark.h" |
Paul Jensen | 84c1d03 | 2014-05-30 13:29:41 -0400 | [diff] [blame] | 33 | #include "NetdConstants.h" |
| 34 | #include "NetworkController.h" |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 35 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 36 | static const char* kClatdPath = "/system/bin/clatd"; |
| 37 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 38 | namespace android { |
| 39 | namespace net { |
| 40 | |
Paul Jensen | 84c1d03 | 2014-05-30 13:29:41 -0400 | [diff] [blame] | 41 | ClatdController::ClatdController(NetworkController* controller) |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 42 | : mNetCtrl(controller) { |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | ClatdController::~ClatdController() { |
| 46 | } |
| 47 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 48 | // Returns the PID of the clatd running on interface |interface|, or 0 if clatd is not running on |
| 49 | // |interface|. |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 50 | pid_t ClatdController::getClatdPid(const char* interface) { |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 51 | auto it = mClatdPids.find(interface); |
| 52 | return (it == mClatdPids.end() ? 0 : it->second); |
| 53 | } |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 54 | |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 55 | int ClatdController::startClatd(const char* interface) { |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 56 | pid_t pid = getClatdPid(interface); |
| 57 | |
| 58 | if (pid != 0) { |
| 59 | ALOGE("clatd pid=%d already started on %s", pid, interface); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 60 | errno = EBUSY; |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 61 | return -errno; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 62 | } |
| 63 | |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 64 | // 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 Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 69 | return -errno; |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 70 | } |
| 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 Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 84 | char* interfaceName = const_cast<char*>(interface); |
| 85 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 86 | ALOGD("starting clatd on %s", interface); |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 87 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 88 | std::string progname("clatd-"); |
| 89 | progname += interface; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 90 | |
| 91 | if ((pid = fork()) < 0) { |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 92 | int res = errno; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 93 | ALOGE("fork failed (%s)", strerror(errno)); |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 94 | return -res; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | if (!pid) { |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 98 | char* args[] = {(char*) progname.c_str(), |
| 99 | (char*) "-i", |
| 100 | interfaceName, |
| 101 | (char*) "-n", |
| 102 | netIdString, |
| 103 | (char*) "-m", |
| 104 | fwmarkString, |
| 105 | nullptr}; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 106 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 107 | if (execv(kClatdPath, args)) { |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 108 | ALOGE("execv failed (%s)", strerror(errno)); |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 109 | _exit(1); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 110 | } |
| 111 | ALOGE("Should never get here!"); |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 112 | _exit(1); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 113 | } else { |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 114 | mClatdPids[interface] = pid; |
| 115 | ALOGD("clatd started on %s", interface); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 121 | int ClatdController::stopClatd(const char* interface) { |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 122 | pid_t pid = getClatdPid(interface); |
| 123 | |
| 124 | if (pid == 0) { |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 125 | ALOGE("clatd already stopped"); |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 126 | return -EREMOTEIO; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 129 | ALOGD("Stopping clatd pid=%d on %s", pid, interface); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 130 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 131 | kill(pid, SIGTERM); |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 132 | waitpid(pid, nullptr, 0); |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 133 | mClatdPids.erase(interface); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 134 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 135 | ALOGD("clatd on %s stopped", interface); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 140 | } // namespace net |
| 141 | } // namespace android |