Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2016, 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 | #define LOG_TAG "Netd" |
| 18 | |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 19 | #include <cinttypes> |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 20 | #include <set> |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 21 | #include <tuple> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Chenbo Feng | f5663d8 | 2018-11-08 16:10:48 -0800 | [diff] [blame] | 24 | #include <android-base/file.h> |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 26 | #include <android-base/strings.h> |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 27 | #include <cutils/properties.h> |
Logan Chien | 3f46148 | 2018-04-23 14:31:32 +0800 | [diff] [blame] | 28 | #include <log/log.h> |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 29 | #include <utils/Errors.h> |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 30 | #include <utils/String16.h> |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 31 | |
| 32 | #include <binder/IPCThreadState.h> |
| 33 | #include <binder/IServiceManager.h> |
| 34 | #include "android/net/BnNetd.h" |
| 35 | |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 36 | #include <openssl/base64.h> |
| 37 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 38 | #include "Controllers.h" |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 39 | #include "DumpWriter.h" |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 40 | #include "InterfaceController.h" |
Mike Yu | 5ae6154 | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 41 | #include "NetdConstants.h" // SHA256_SIZE |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 42 | #include "NetdNativeService.h" |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 43 | #include "Permission.h" |
Erik Kline | 8589004 | 2018-05-25 19:19:11 +0900 | [diff] [blame] | 44 | #include "Process.h" |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 45 | #include "RouteController.h" |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 46 | #include "SockDiag.h" |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 47 | #include "UidRanges.h" |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 48 | #include "netid_client.h" // NETID_UNSET |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 49 | |
| 50 | using android::base::StringPrintf; |
Chenbo Feng | f5663d8 | 2018-11-08 16:10:48 -0800 | [diff] [blame] | 51 | using android::base::WriteStringToFile; |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 52 | using android::net::TetherStatsParcel; |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 53 | using android::net::UidRangeParcel; |
Luke Huang | e203a15 | 2018-11-23 11:47:28 +0800 | [diff] [blame] | 54 | using android::os::ParcelFileDescriptor; |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 55 | |
| 56 | namespace android { |
| 57 | namespace net { |
| 58 | |
| 59 | namespace { |
| 60 | |
| 61 | const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL"; |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 62 | const char NETWORK_STACK[] = "android.permission.NETWORK_STACK"; |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 63 | const char DUMP[] = "android.permission.DUMP"; |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 64 | const char OPT_SHORT[] = "--short"; |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 65 | |
| 66 | binder::Status checkPermission(const char *permission) { |
Luke Huang | a38b65c | 2018-09-26 16:31:03 +0800 | [diff] [blame] | 67 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 68 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 69 | |
Luke Huang | a38b65c | 2018-09-26 16:31:03 +0800 | [diff] [blame] | 70 | // If the caller is the system UID, don't check permissions. |
| 71 | // Otherwise, if the system server's binder thread pool is full, and all the threads are |
| 72 | // blocked on a thread that's waiting for us to complete, we deadlock. http://b/69389492 |
| 73 | // |
| 74 | // From a security perspective, there is currently no difference, because: |
| 75 | // 1. The only permissions we check in netd's binder interface are CONNECTIVITY_INTERNAL |
| 76 | // and NETWORK_STACK, which the system server will always need to have. |
| 77 | // 2. AID_SYSTEM always has all permissions. See ActivityManager#checkComponentPermission. |
| 78 | if (uid == AID_SYSTEM || checkPermission(String16(permission), pid, uid)) { |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 79 | return binder::Status::ok(); |
| 80 | } else { |
| 81 | auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission); |
| 82 | return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str())); |
| 83 | } |
| 84 | } |
| 85 | |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 86 | #define ENFORCE_DEBUGGABLE() { \ |
| 87 | char value[PROPERTY_VALUE_MAX + 1]; \ |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 88 | if (property_get("ro.debuggable", value, nullptr) != 1 \ |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 89 | || value[0] != '1') { \ |
| 90 | return binder::Status::fromExceptionCode( \ |
| 91 | binder::Status::EX_SECURITY, \ |
| 92 | String8("Not available in production builds.") \ |
| 93 | ); \ |
| 94 | } \ |
| 95 | } |
| 96 | |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 97 | #define ENFORCE_PERMISSION(permission) { \ |
| 98 | binder::Status status = checkPermission((permission)); \ |
| 99 | if (!status.isOk()) { \ |
| 100 | return status; \ |
| 101 | } \ |
| 102 | } |
| 103 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 104 | #define NETD_LOCKING_RPC(permission, lock) \ |
| 105 | ENFORCE_PERMISSION(permission); \ |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 106 | std::lock_guard _lock(lock); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 107 | |
| 108 | #define NETD_BIG_LOCK_RPC(permission) NETD_LOCKING_RPC((permission), gBigNetdLock) |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 109 | |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 110 | #define RETURN_BINDER_STATUS_IF_NOT_OK(logEntry, res) \ |
| 111 | do { \ |
| 112 | if (!isOk((res))) { \ |
| 113 | logErrorStatus((logEntry), (res)); \ |
| 114 | return asBinderStatus((res)); \ |
| 115 | } \ |
| 116 | } while (0) |
| 117 | |
| 118 | void logErrorStatus(netdutils::LogEntry& logEntry, const netdutils::Status& status) { |
| 119 | gLog.log(logEntry.returns(status.code()).withAutomaticDuration()); |
| 120 | } |
| 121 | |
Bernie Innocenti | 97f388f | 2018-10-16 19:17:08 +0900 | [diff] [blame] | 122 | binder::Status asBinderStatus(const netdutils::Status& status) { |
| 123 | if (isOk(status)) { |
| 124 | return binder::Status::ok(); |
| 125 | } |
| 126 | return binder::Status::fromServiceSpecificError(status.code(), status.msg().c_str()); |
| 127 | } |
| 128 | |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 129 | inline binder::Status statusFromErrcode(int ret) { |
| 130 | if (ret) { |
| 131 | return binder::Status::fromServiceSpecificError(-ret, strerror(-ret)); |
| 132 | } |
| 133 | return binder::Status::ok(); |
| 134 | } |
| 135 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 136 | bool contains(const Vector<String16>& words, const String16& word) { |
| 137 | for (const auto& w : words) { |
| 138 | if (w == word) return true; |
| 139 | } |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 140 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 141 | return false; |
| 142 | } |
| 143 | |
| 144 | } // namespace |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 145 | |
Lorenzo Colitti | e4851de | 2016-03-17 13:23:28 +0900 | [diff] [blame] | 146 | status_t NetdNativeService::start() { |
| 147 | IPCThreadState::self()->disableBackgroundScheduling(true); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 148 | const status_t ret = BinderService<NetdNativeService>::publish(); |
Lorenzo Colitti | e4851de | 2016-03-17 13:23:28 +0900 | [diff] [blame] | 149 | if (ret != android::OK) { |
| 150 | return ret; |
| 151 | } |
| 152 | sp<ProcessState> ps(ProcessState::self()); |
| 153 | ps->startThreadPool(); |
| 154 | ps->giveThreadPoolName(); |
| 155 | return android::OK; |
| 156 | } |
| 157 | |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 158 | status_t NetdNativeService::dump(int fd, const Vector<String16> &args) { |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 159 | const binder::Status dump_permission = checkPermission(DUMP); |
| 160 | if (!dump_permission.isOk()) { |
| 161 | const String8 msg(dump_permission.toString8()); |
| 162 | write(fd, msg.string(), msg.size()); |
| 163 | return PERMISSION_DENIED; |
| 164 | } |
| 165 | |
| 166 | // This method does not grab any locks. If individual classes need locking |
| 167 | // their dump() methods MUST handle locking appropriately. |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 168 | |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 169 | DumpWriter dw(fd); |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 170 | |
| 171 | if (!args.isEmpty() && args[0] == TcpSocketMonitor::DUMP_KEYWORD) { |
| 172 | dw.blankline(); |
| 173 | gCtls->tcpSocketMonitor.dump(dw); |
| 174 | dw.blankline(); |
| 175 | return NO_ERROR; |
| 176 | } |
| 177 | |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 178 | if (!args.isEmpty() && args[0] == TrafficController::DUMP_KEYWORD) { |
| 179 | dw.blankline(); |
| 180 | gCtls->trafficCtrl.dump(dw, true); |
| 181 | dw.blankline(); |
| 182 | return NO_ERROR; |
| 183 | } |
| 184 | |
Erik Kline | 8589004 | 2018-05-25 19:19:11 +0900 | [diff] [blame] | 185 | process::dump(dw); |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 186 | dw.blankline(); |
| 187 | gCtls->netCtrl.dump(dw); |
| 188 | dw.blankline(); |
| 189 | |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 190 | gCtls->trafficCtrl.dump(dw, false); |
| 191 | dw.blankline(); |
| 192 | |
Benedict Wong | af85543 | 2018-05-10 17:07:37 -0700 | [diff] [blame] | 193 | gCtls->xfrmCtrl.dump(dw); |
| 194 | dw.blankline(); |
| 195 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 196 | { |
| 197 | ScopedIndent indentLog(dw); |
| 198 | if (contains(args, String16(OPT_SHORT))) { |
| 199 | dw.println("Log: <omitted>"); |
| 200 | } else { |
| 201 | dw.println("Log:"); |
| 202 | ScopedIndent indentLogEntries(dw); |
| 203 | gLog.forEachEntry([&dw](const std::string& entry) mutable { dw.println(entry); }); |
| 204 | } |
| 205 | dw.blankline(); |
| 206 | } |
| 207 | |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 208 | { |
| 209 | ScopedIndent indentLog(dw); |
| 210 | if (contains(args, String16(OPT_SHORT))) { |
| 211 | dw.println("UnsolicitedLog: <omitted>"); |
| 212 | } else { |
| 213 | dw.println("UnsolicitedLog:"); |
| 214 | ScopedIndent indentLogEntries(dw); |
| 215 | gUnsolicitedLog.forEachEntry( |
| 216 | [&dw](const std::string& entry) mutable { dw.println(entry); }); |
| 217 | } |
| 218 | dw.blankline(); |
| 219 | } |
| 220 | |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 221 | return NO_ERROR; |
| 222 | } |
| 223 | |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 224 | binder::Status NetdNativeService::isAlive(bool *alive) { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 225 | NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 226 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 227 | |
| 228 | *alive = true; |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 229 | |
| 230 | gLog.log(entry.returns(*alive)); |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 231 | return binder::Status::ok(); |
| 232 | } |
| 233 | |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 234 | binder::Status NetdNativeService::firewallReplaceUidChain(const std::string& chainName, |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 235 | bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) { |
| 236 | NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->firewallCtrl.lock); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 237 | auto entry = gLog.newEntry() |
| 238 | .prettyFunction(__PRETTY_FUNCTION__) |
| 239 | .arg(chainName) |
| 240 | .arg(isWhitelist) |
| 241 | .arg(uids); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 242 | |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 243 | int err = gCtls->firewallCtrl.replaceUidChain(chainName, isWhitelist, uids); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 244 | *ret = (err == 0); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 245 | |
| 246 | gLog.log(entry.returns(*ret).withAutomaticDuration()); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 247 | return binder::Status::ok(); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 248 | } |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 249 | |
| 250 | binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) { |
| 251 | NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->bandwidthCtrl.lock); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 252 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(enable); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 253 | |
| 254 | int err = gCtls->bandwidthCtrl.enableDataSaver(enable); |
| 255 | *ret = (err == 0); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 256 | gLog.log(entry.returns(*ret).withAutomaticDuration()); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 257 | return binder::Status::ok(); |
| 258 | } |
| 259 | |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 260 | binder::Status NetdNativeService::bandwidthSetInterfaceQuota(const std::string& ifName, |
| 261 | int64_t bytes) { |
| 262 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock); |
| 263 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName).arg(bytes); |
| 264 | |
| 265 | int res = gCtls->bandwidthCtrl.setInterfaceQuota(ifName, bytes); |
| 266 | |
| 267 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 268 | return statusFromErrcode(res); |
| 269 | } |
| 270 | |
| 271 | binder::Status NetdNativeService::bandwidthRemoveInterfaceQuota(const std::string& ifName) { |
| 272 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock); |
| 273 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName); |
| 274 | |
| 275 | int res = gCtls->bandwidthCtrl.removeInterfaceQuota(ifName); |
| 276 | |
| 277 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 278 | return statusFromErrcode(res); |
| 279 | } |
| 280 | |
| 281 | binder::Status NetdNativeService::bandwidthSetInterfaceAlert(const std::string& ifName, |
| 282 | int64_t bytes) { |
| 283 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock); |
| 284 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName).arg(bytes); |
| 285 | |
| 286 | int res = gCtls->bandwidthCtrl.setInterfaceAlert(ifName, bytes); |
| 287 | |
| 288 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 289 | return statusFromErrcode(res); |
| 290 | } |
| 291 | |
| 292 | binder::Status NetdNativeService::bandwidthRemoveInterfaceAlert(const std::string& ifName) { |
| 293 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock); |
| 294 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName); |
| 295 | |
| 296 | int res = gCtls->bandwidthCtrl.removeInterfaceAlert(ifName); |
| 297 | |
| 298 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 299 | return statusFromErrcode(res); |
| 300 | } |
| 301 | |
| 302 | binder::Status NetdNativeService::bandwidthSetGlobalAlert(int64_t bytes) { |
| 303 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock); |
| 304 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(bytes); |
| 305 | |
| 306 | int res = gCtls->bandwidthCtrl.setGlobalAlert(bytes); |
| 307 | |
| 308 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 309 | return statusFromErrcode(res); |
| 310 | } |
| 311 | |
| 312 | binder::Status NetdNativeService::bandwidthAddNaughtyApp(int32_t uid) { |
| 313 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock); |
| 314 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid); |
| 315 | |
| 316 | std::vector<std::string> appStrUids = {std::to_string(abs(uid))}; |
| 317 | int res = gCtls->bandwidthCtrl.addNaughtyApps(appStrUids); |
| 318 | |
| 319 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 320 | return statusFromErrcode(res); |
| 321 | } |
| 322 | |
| 323 | binder::Status NetdNativeService::bandwidthRemoveNaughtyApp(int32_t uid) { |
| 324 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock); |
| 325 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid); |
| 326 | |
| 327 | std::vector<std::string> appStrUids = {std::to_string(abs(uid))}; |
| 328 | int res = gCtls->bandwidthCtrl.removeNaughtyApps(appStrUids); |
| 329 | |
| 330 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 331 | return statusFromErrcode(res); |
| 332 | } |
| 333 | |
| 334 | binder::Status NetdNativeService::bandwidthAddNiceApp(int32_t uid) { |
| 335 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock); |
| 336 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid); |
| 337 | |
| 338 | std::vector<std::string> appStrUids = {std::to_string(abs(uid))}; |
| 339 | int res = gCtls->bandwidthCtrl.addNiceApps(appStrUids); |
| 340 | |
| 341 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 342 | return statusFromErrcode(res); |
| 343 | } |
| 344 | |
| 345 | binder::Status NetdNativeService::bandwidthRemoveNiceApp(int32_t uid) { |
| 346 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock); |
| 347 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid); |
| 348 | |
| 349 | std::vector<std::string> appStrUids = {std::to_string(abs(uid))}; |
| 350 | int res = gCtls->bandwidthCtrl.removeNiceApps(appStrUids); |
| 351 | |
| 352 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 353 | return statusFromErrcode(res); |
| 354 | } |
| 355 | |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 356 | binder::Status NetdNativeService::networkCreatePhysical(int32_t netId, int32_t permission) { |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 357 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 358 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId).arg(permission); |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 359 | int ret = gCtls->netCtrl.createPhysicalNetwork(netId, convertPermission(permission)); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 360 | gLog.log(entry.returns(ret).withAutomaticDuration()); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 361 | return statusFromErrcode(ret); |
| 362 | } |
| 363 | |
cken | 67cd14c | 2018-12-05 17:26:59 +0900 | [diff] [blame] | 364 | binder::Status NetdNativeService::networkCreateVpn(int32_t netId, bool secure) { |
Ken Chen | 87f1ec9 | 2018-12-18 20:15:28 +0800 | [diff] [blame] | 365 | ENFORCE_PERMISSION(NETWORK_STACK); |
cken | 67cd14c | 2018-12-05 17:26:59 +0900 | [diff] [blame] | 366 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(netId, secure); |
| 367 | int ret = gCtls->netCtrl.createVirtualNetwork(netId, secure); |
| 368 | gLog.log(entry.returns(ret).withAutomaticDuration()); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 369 | return statusFromErrcode(ret); |
| 370 | } |
| 371 | |
| 372 | binder::Status NetdNativeService::networkDestroy(int32_t netId) { |
Erik Kline | c8b6a9c | 2018-01-15 17:06:48 +0900 | [diff] [blame] | 373 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 374 | // Both of these functions manage their own locking internally. |
| 375 | const int ret = gCtls->netCtrl.destroyNetwork(netId); |
| 376 | gCtls->resolverCtrl.clearDnsServers(netId); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 377 | return statusFromErrcode(ret); |
| 378 | } |
| 379 | |
| 380 | binder::Status NetdNativeService::networkAddInterface(int32_t netId, const std::string& iface) { |
| 381 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 382 | int ret = gCtls->netCtrl.addInterfaceToNetwork(netId, iface.c_str()); |
| 383 | return statusFromErrcode(ret); |
| 384 | } |
| 385 | |
| 386 | binder::Status NetdNativeService::networkRemoveInterface(int32_t netId, const std::string& iface) { |
| 387 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 388 | int ret = gCtls->netCtrl.removeInterfaceFromNetwork(netId, iface.c_str()); |
| 389 | return statusFromErrcode(ret); |
| 390 | } |
| 391 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 392 | namespace { |
| 393 | |
| 394 | std::string uidRangeParcelVecToString(const std::vector<UidRangeParcel>& uidRangeArray) { |
| 395 | std::vector<std::string> result; |
| 396 | result.reserve(uidRangeArray.size()); |
| 397 | for (const auto& uidRange : uidRangeArray) { |
| 398 | result.push_back(StringPrintf("%d-%d", uidRange.start, uidRange.stop)); |
| 399 | } |
| 400 | |
| 401 | return base::Join(result, ", "); |
| 402 | } |
| 403 | |
| 404 | } // namespace |
| 405 | |
| 406 | binder::Status NetdNativeService::networkAddUidRanges( |
| 407 | int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) { |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 408 | // NetworkController::addUsersToNetwork is thread-safe. |
| 409 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 410 | auto entry = gLog.newEntry() |
| 411 | .prettyFunction(__PRETTY_FUNCTION__) |
| 412 | .args(netId, uidRangeParcelVecToString(uidRangeArray)); |
| 413 | |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 414 | int ret = gCtls->netCtrl.addUsersToNetwork(netId, UidRanges(uidRangeArray)); |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 415 | gLog.log(entry.returns(ret).withAutomaticDuration()); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 416 | return statusFromErrcode(ret); |
| 417 | } |
| 418 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 419 | binder::Status NetdNativeService::networkRemoveUidRanges( |
| 420 | int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) { |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 421 | // NetworkController::removeUsersFromNetwork is thread-safe. |
| 422 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 423 | auto entry = gLog.newEntry() |
| 424 | .prettyFunction(__PRETTY_FUNCTION__) |
| 425 | .args(netId, uidRangeParcelVecToString(uidRangeArray)); |
| 426 | |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 427 | int ret = gCtls->netCtrl.removeUsersFromNetwork(netId, UidRanges(uidRangeArray)); |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 428 | gLog.log(entry.returns(ret).withAutomaticDuration()); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 429 | return statusFromErrcode(ret); |
| 430 | } |
| 431 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 432 | binder::Status NetdNativeService::networkRejectNonSecureVpn( |
| 433 | bool add, const std::vector<UidRangeParcel>& uidRangeArray) { |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 434 | // TODO: elsewhere RouteController is only used from the tethering and network controllers, so |
| 435 | // it should be possible to use the same lock as NetworkController. However, every call through |
| 436 | // the CommandListener "network" command will need to hold this lock too, not just the ones that |
| 437 | // read/modify network internal state (that is sufficient for ::dump() because it doesn't |
| 438 | // look at routes, but it's not enough here). |
| 439 | NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL); |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 440 | auto entry = gLog.newEntry() |
| 441 | .prettyFunction(__PRETTY_FUNCTION__) |
| 442 | .args(add, uidRangeParcelVecToString(uidRangeArray)); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 443 | UidRanges uidRanges(uidRangeArray); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 444 | |
| 445 | int err; |
| 446 | if (add) { |
| 447 | err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges); |
| 448 | } else { |
| 449 | err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges); |
| 450 | } |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 451 | gLog.log(entry.returns(err).withAutomaticDuration()); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 452 | return statusFromErrcode(err); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 453 | } |
| 454 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 455 | binder::Status NetdNativeService::socketDestroy(const std::vector<UidRangeParcel>& uids, |
| 456 | const std::vector<int32_t>& skipUids) { |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 457 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 458 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 459 | auto entry = gLog.newEntry() |
| 460 | .prettyFunction(__PRETTY_FUNCTION__) |
| 461 | .arg(uidRangeParcelVecToString(uids)); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 462 | SockDiag sd; |
| 463 | if (!sd.open()) { |
| 464 | return binder::Status::fromServiceSpecificError(EIO, |
| 465 | String8("Could not open SOCK_DIAG socket")); |
| 466 | } |
| 467 | |
| 468 | UidRanges uidRanges(uids); |
Lorenzo Colitti | e5c3c99 | 2016-07-26 17:53:50 +0900 | [diff] [blame] | 469 | int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()), |
| 470 | true /* excludeLoopback */); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 471 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 472 | gLog.log(entry.returns(err).withAutomaticDuration()); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 473 | if (err) { |
| 474 | return binder::Status::fromServiceSpecificError(-err, |
| 475 | String8::format("destroySockets: %s", strerror(-err))); |
| 476 | } |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 477 | return binder::Status::ok(); |
| 478 | } |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 479 | |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 480 | // Parse a base64 encoded string into a vector of bytes. |
| 481 | // On failure, return an empty vector. |
| 482 | static std::vector<uint8_t> parseBase64(const std::string& input) { |
| 483 | std::vector<uint8_t> decoded; |
| 484 | size_t out_len; |
| 485 | if (EVP_DecodedLength(&out_len, input.size()) != 1) { |
| 486 | return decoded; |
| 487 | } |
| 488 | // out_len is now an upper bound on the output length. |
| 489 | decoded.resize(out_len); |
| 490 | if (EVP_DecodeBase64(decoded.data(), &out_len, decoded.size(), |
| 491 | reinterpret_cast<const uint8_t*>(input.data()), input.size()) == 1) { |
| 492 | // Possibly shrink the vector if the actual output was smaller than the bound. |
| 493 | decoded.resize(out_len); |
| 494 | } else { |
| 495 | decoded.clear(); |
| 496 | } |
| 497 | if (out_len != SHA256_SIZE) { |
| 498 | decoded.clear(); |
| 499 | } |
| 500 | return decoded; |
| 501 | } |
| 502 | |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 503 | binder::Status NetdNativeService::setResolverConfiguration(int32_t netId, |
| 504 | const std::vector<std::string>& servers, const std::vector<std::string>& domains, |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 505 | const std::vector<int32_t>& params, const std::string& tlsName, |
| 506 | const std::vector<std::string>& tlsServers, |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 507 | const std::vector<std::string>& tlsFingerprints) { |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 508 | // This function intentionally does not lock within Netd, as Bionic is thread-safe. |
| 509 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 510 | auto entry = gLog.newEntry() |
| 511 | .prettyFunction(__PRETTY_FUNCTION__) |
| 512 | .arg(netId) |
| 513 | .arg(servers) |
| 514 | .arg(domains) |
| 515 | .arg(params) |
| 516 | .arg(tlsName) |
| 517 | .arg(tlsServers) |
| 518 | .arg(tlsFingerprints); |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 519 | |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 520 | std::set<std::vector<uint8_t>> decoded_fingerprints; |
| 521 | for (const std::string& fingerprint : tlsFingerprints) { |
| 522 | std::vector<uint8_t> decoded = parseBase64(fingerprint); |
| 523 | if (decoded.empty()) { |
| 524 | return binder::Status::fromServiceSpecificError(EINVAL, |
| 525 | String8::format("ResolverController error: bad fingerprint")); |
| 526 | } |
| 527 | decoded_fingerprints.emplace(decoded); |
| 528 | } |
| 529 | |
| 530 | int err = gCtls->resolverCtrl.setResolverConfiguration(netId, servers, domains, params, |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 531 | tlsName, tlsServers, decoded_fingerprints); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 532 | gLog.log(entry.returns(err).withAutomaticDuration()); |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 533 | if (err != 0) { |
| 534 | return binder::Status::fromServiceSpecificError(-err, |
| 535 | String8::format("ResolverController error: %s", strerror(-err))); |
| 536 | } |
| 537 | return binder::Status::ok(); |
| 538 | } |
| 539 | |
Mike Yu | da77e8e | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 540 | binder::Status NetdNativeService::getResolverInfo(int32_t netId, std::vector<std::string>* servers, |
| 541 | std::vector<std::string>* domains, |
| 542 | std::vector<std::string>* tlsServers, |
| 543 | std::vector<int32_t>* params, |
| 544 | std::vector<int32_t>* stats) { |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 545 | // This function intentionally does not lock within Netd, as Bionic is thread-safe. |
Mike Yu | da77e8e | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 546 | ENFORCE_PERMISSION(NETWORK_STACK); |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 547 | |
Mike Yu | da77e8e | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 548 | int err = |
| 549 | gCtls->resolverCtrl.getResolverInfo(netId, servers, domains, tlsServers, params, stats); |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 550 | if (err != 0) { |
| 551 | return binder::Status::fromServiceSpecificError(-err, |
| 552 | String8::format("ResolverController error: %s", strerror(-err))); |
| 553 | } |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 554 | return binder::Status::ok(); |
| 555 | } |
| 556 | |
Erik Kline | f48e4dd | 2016-07-18 04:02:07 +0900 | [diff] [blame] | 557 | binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) { |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 558 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
Erik Kline | f48e4dd | 2016-07-18 04:02:07 +0900 | [diff] [blame] | 559 | |
| 560 | *ret = gCtls->tetherCtrl.applyDnsInterfaces(); |
| 561 | return binder::Status::ok(); |
| 562 | } |
| 563 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 564 | namespace { |
| 565 | |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 566 | void tetherAddStatsByInterface(TetherController::TetherStats* tetherStatsParcel, |
| 567 | const TetherController::TetherStats& tetherStats) { |
| 568 | if (tetherStatsParcel->extIface == tetherStats.extIface) { |
| 569 | tetherStatsParcel->rxBytes += tetherStats.rxBytes; |
| 570 | tetherStatsParcel->rxPackets += tetherStats.rxPackets; |
| 571 | tetherStatsParcel->txBytes += tetherStats.txBytes; |
| 572 | tetherStatsParcel->txPackets += tetherStats.txPackets; |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 573 | } |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 574 | } |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 575 | |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 576 | TetherStatsParcel toTetherStatsParcel(const TetherController::TetherStats& stats) { |
| 577 | TetherStatsParcel result; |
| 578 | result.iface = stats.extIface; |
| 579 | result.rxBytes = stats.rxBytes; |
| 580 | result.rxPackets = stats.rxPackets; |
| 581 | result.txBytes = stats.txBytes; |
| 582 | result.txPackets = stats.txPackets; |
| 583 | return result; |
| 584 | } |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 585 | |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 586 | void setTetherStatsParcelVecByInterface(std::vector<TetherStatsParcel>* tetherStatsVec, |
| 587 | const TetherController::TetherStatsList& statsList) { |
| 588 | std::map<std::string, TetherController::TetherStats> statsMap; |
| 589 | for (const auto& stats : statsList) { |
| 590 | auto iter = statsMap.find(stats.extIface); |
| 591 | if (iter != statsMap.end()) { |
| 592 | tetherAddStatsByInterface(&(iter->second), stats); |
| 593 | } else { |
| 594 | statsMap.insert( |
| 595 | std::pair<std::string, TetherController::TetherStats>(stats.extIface, stats)); |
| 596 | } |
| 597 | } |
| 598 | for (auto iter = statsMap.begin(); iter != statsMap.end(); iter++) { |
| 599 | tetherStatsVec->push_back(toTetherStatsParcel(iter->second)); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | std::vector<std::string> tetherStatsParcelVecToStringVec(std::vector<TetherStatsParcel>* tVec) { |
| 604 | std::vector<std::string> result; |
| 605 | for (const auto& t : *tVec) { |
| 606 | result.push_back(StringPrintf("%s:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64, |
| 607 | t.iface.c_str(), t.rxBytes, t.rxPackets, t.txBytes, |
| 608 | t.txPackets)); |
| 609 | } |
| 610 | return result; |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | } // namespace |
| 614 | |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 615 | binder::Status NetdNativeService::tetherGetStats( |
| 616 | std::vector<TetherStatsParcel>* tetherStatsParcelVec) { |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 617 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 618 | |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 619 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
| 620 | |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 621 | const auto& statsList = gCtls->tetherCtrl.getTetherStats(); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 622 | if (!isOk(statsList)) { |
Nathan Harold | 28ccfef | 2018-03-16 19:02:47 -0700 | [diff] [blame] | 623 | return asBinderStatus(statsList); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 624 | } |
Luke Huang | caebcbb | 2018-09-27 20:37:14 +0800 | [diff] [blame] | 625 | setTetherStatsParcelVecByInterface(tetherStatsParcelVec, statsList.value()); |
| 626 | auto statsResults = tetherStatsParcelVecToStringVec(tetherStatsParcelVec); |
| 627 | gLog.log(entry.returns(base::Join(statsResults, ";")).withAutomaticDuration()); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 628 | return binder::Status::ok(); |
| 629 | } |
| 630 | |
Erik Kline | 53c2088 | 2016-08-02 15:22:53 +0900 | [diff] [blame] | 631 | binder::Status NetdNativeService::interfaceAddAddress(const std::string &ifName, |
| 632 | const std::string &addrString, int prefixLength) { |
| 633 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 634 | |
| 635 | const int err = InterfaceController::addAddress( |
| 636 | ifName.c_str(), addrString.c_str(), prefixLength); |
| 637 | if (err != 0) { |
| 638 | return binder::Status::fromServiceSpecificError(-err, |
| 639 | String8::format("InterfaceController error: %s", strerror(-err))); |
| 640 | } |
| 641 | return binder::Status::ok(); |
| 642 | } |
| 643 | |
| 644 | binder::Status NetdNativeService::interfaceDelAddress(const std::string &ifName, |
| 645 | const std::string &addrString, int prefixLength) { |
| 646 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 647 | |
| 648 | const int err = InterfaceController::delAddress( |
| 649 | ifName.c_str(), addrString.c_str(), prefixLength); |
| 650 | if (err != 0) { |
| 651 | return binder::Status::fromServiceSpecificError(-err, |
| 652 | String8::format("InterfaceController error: %s", strerror(-err))); |
| 653 | } |
| 654 | return binder::Status::ok(); |
| 655 | } |
| 656 | |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 657 | namespace { |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 658 | |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 659 | std::tuple<binder::Status, const char*, const char*> getPathComponents(int32_t ipversion, |
| 660 | int32_t category) { |
| 661 | const char* ipversionStr = nullptr; |
| 662 | switch (ipversion) { |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 663 | case INetd::IPV4: |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 664 | ipversionStr = "ipv4"; |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 665 | break; |
| 666 | case INetd::IPV6: |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 667 | ipversionStr = "ipv6"; |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 668 | break; |
| 669 | default: |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 670 | return {binder::Status::fromServiceSpecificError(EAFNOSUPPORT, "Bad IP version"), |
| 671 | nullptr, nullptr}; |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 672 | } |
| 673 | |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 674 | const char* whichStr = nullptr; |
| 675 | switch (category) { |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 676 | case INetd::CONF: |
| 677 | whichStr = "conf"; |
| 678 | break; |
| 679 | case INetd::NEIGH: |
| 680 | whichStr = "neigh"; |
| 681 | break; |
| 682 | default: |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 683 | return {binder::Status::fromServiceSpecificError(EINVAL, "Bad category"), nullptr, |
| 684 | nullptr}; |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 685 | } |
| 686 | |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 687 | return {binder::Status::ok(), ipversionStr, whichStr}; |
| 688 | } |
| 689 | |
| 690 | } // namespace |
| 691 | |
| 692 | binder::Status NetdNativeService::getProcSysNet(int32_t ipversion, int32_t which, |
| 693 | const std::string& ifname, |
| 694 | const std::string& parameter, std::string* value) { |
| 695 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 696 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__) |
| 697 | .args(ipversion, which, ifname, parameter); |
| 698 | |
| 699 | const auto pathParts = getPathComponents(ipversion, which); |
| 700 | const auto& pathStatus = std::get<0>(pathParts); |
| 701 | if (!pathStatus.isOk()) { |
| 702 | gLog.log(entry.returns(pathStatus.exceptionCode()).withAutomaticDuration()); |
| 703 | return pathStatus; |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 704 | } |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 705 | |
| 706 | const int err = InterfaceController::getParameter(std::get<1>(pathParts), |
| 707 | std::get<2>(pathParts), ifname.c_str(), |
| 708 | parameter.c_str(), value); |
| 709 | entry.returns(err); |
| 710 | if (err == 0) entry.returns(*value); |
| 711 | gLog.log(entry.withAutomaticDuration()); |
| 712 | return statusFromErrcode(err); |
| 713 | } |
| 714 | |
| 715 | binder::Status NetdNativeService::setProcSysNet(int32_t ipversion, int32_t which, |
| 716 | const std::string& ifname, |
| 717 | const std::string& parameter, |
| 718 | const std::string& value) { |
| 719 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 720 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__) |
| 721 | .args(ipversion, which, ifname, parameter, value); |
| 722 | |
| 723 | const auto pathParts = getPathComponents(ipversion, which); |
| 724 | const auto& pathStatus = std::get<0>(pathParts); |
| 725 | if (!pathStatus.isOk()) { |
| 726 | gLog.log(entry.returns(pathStatus.exceptionCode()).withAutomaticDuration()); |
| 727 | return pathStatus; |
| 728 | } |
| 729 | |
| 730 | const int err = InterfaceController::setParameter(std::get<1>(pathParts), |
| 731 | std::get<2>(pathParts), ifname.c_str(), |
| 732 | parameter.c_str(), value.c_str()); |
| 733 | gLog.log(entry.returns(err).withAutomaticDuration()); |
| 734 | return statusFromErrcode(err); |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 735 | } |
| 736 | |
Luke Huang | e203a15 | 2018-11-23 11:47:28 +0800 | [diff] [blame] | 737 | binder::Status NetdNativeService::ipSecSetEncapSocketOwner(const ParcelFileDescriptor& socket, |
| 738 | int newUid) { |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 739 | ENFORCE_PERMISSION(NETWORK_STACK) |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 740 | gLog.log("ipSecSetEncapSocketOwner()"); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 741 | |
| 742 | uid_t callerUid = IPCThreadState::self()->getCallingUid(); |
Luke Huang | e203a15 | 2018-11-23 11:47:28 +0800 | [diff] [blame] | 743 | return asBinderStatus( |
| 744 | gCtls->xfrmCtrl.ipSecSetEncapSocketOwner(socket.get(), newUid, callerUid)); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 745 | } |
| 746 | |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 747 | binder::Status NetdNativeService::ipSecAllocateSpi( |
| 748 | int32_t transformId, |
Nathan Harold | da54f12 | 2018-01-09 16:42:57 -0800 | [diff] [blame] | 749 | const std::string& sourceAddress, |
| 750 | const std::string& destinationAddress, |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 751 | int32_t inSpi, |
| 752 | int32_t* outSpi) { |
| 753 | // Necessary locking done in IpSecService and kernel |
| 754 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 755 | gLog.log("ipSecAllocateSpi()"); |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 756 | return asBinderStatus(gCtls->xfrmCtrl.ipSecAllocateSpi( |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 757 | transformId, |
Nathan Harold | da54f12 | 2018-01-09 16:42:57 -0800 | [diff] [blame] | 758 | sourceAddress, |
| 759 | destinationAddress, |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 760 | inSpi, |
| 761 | outSpi)); |
| 762 | } |
| 763 | |
| 764 | binder::Status NetdNativeService::ipSecAddSecurityAssociation( |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 765 | int32_t transformId, int32_t mode, const std::string& sourceAddress, |
| 766 | const std::string& destinationAddress, int32_t underlyingNetId, int32_t spi, |
| 767 | int32_t markValue, int32_t markMask, const std::string& authAlgo, |
| 768 | const std::vector<uint8_t>& authKey, int32_t authTruncBits, const std::string& cryptAlgo, |
| 769 | const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits, const std::string& aeadAlgo, |
| 770 | const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits, int32_t encapType, |
| 771 | int32_t encapLocalPort, int32_t encapRemotePort, int32_t interfaceId) { |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 772 | // Necessary locking done in IpSecService and kernel |
| 773 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 774 | gLog.log("ipSecAddSecurityAssociation()"); |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 775 | return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityAssociation( |
Benedict Wong | ad600cb | 2018-05-14 17:22:35 -0700 | [diff] [blame] | 776 | transformId, mode, sourceAddress, destinationAddress, underlyingNetId, spi, markValue, |
| 777 | markMask, authAlgo, authKey, authTruncBits, cryptAlgo, cryptKey, cryptTruncBits, |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 778 | aeadAlgo, aeadKey, aeadIcvBits, encapType, encapLocalPort, encapRemotePort, |
| 779 | interfaceId)); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | binder::Status NetdNativeService::ipSecDeleteSecurityAssociation( |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 783 | int32_t transformId, const std::string& sourceAddress, |
| 784 | const std::string& destinationAddress, int32_t spi, int32_t markValue, int32_t markMask, |
| 785 | int32_t interfaceId) { |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 786 | // Necessary locking done in IpSecService and kernel |
| 787 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 788 | gLog.log("ipSecDeleteSecurityAssociation()"); |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 789 | return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityAssociation( |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 790 | transformId, sourceAddress, destinationAddress, spi, markValue, markMask, interfaceId)); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | binder::Status NetdNativeService::ipSecApplyTransportModeTransform( |
Luke Huang | e203a15 | 2018-11-23 11:47:28 +0800 | [diff] [blame] | 794 | const ParcelFileDescriptor& socket, int32_t transformId, int32_t direction, |
| 795 | const std::string& sourceAddress, const std::string& destinationAddress, int32_t spi) { |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 796 | // Necessary locking done in IpSecService and kernel |
| 797 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 798 | gLog.log("ipSecApplyTransportModeTransform()"); |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 799 | return asBinderStatus(gCtls->xfrmCtrl.ipSecApplyTransportModeTransform( |
Luke Huang | e203a15 | 2018-11-23 11:47:28 +0800 | [diff] [blame] | 800 | socket.get(), transformId, direction, sourceAddress, destinationAddress, spi)); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | binder::Status NetdNativeService::ipSecRemoveTransportModeTransform( |
Luke Huang | e203a15 | 2018-11-23 11:47:28 +0800 | [diff] [blame] | 804 | const ParcelFileDescriptor& socket) { |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 805 | // Necessary locking done in IpSecService and kernel |
| 806 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 807 | gLog.log("ipSecRemoveTransportModeTransform()"); |
Luke Huang | e203a15 | 2018-11-23 11:47:28 +0800 | [diff] [blame] | 808 | return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTransportModeTransform(socket.get())); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 809 | } |
| 810 | |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 811 | binder::Status NetdNativeService::ipSecAddSecurityPolicy(int32_t transformId, int32_t selAddrFamily, |
| 812 | int32_t direction, |
Benedict Wong | ad600cb | 2018-05-14 17:22:35 -0700 | [diff] [blame] | 813 | const std::string& tmplSrcAddress, |
| 814 | const std::string& tmplDstAddress, |
| 815 | int32_t spi, int32_t markValue, |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 816 | int32_t markMask, int32_t interfaceId) { |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 817 | // Necessary locking done in IpSecService and kernel |
| 818 | ENFORCE_PERMISSION(NETWORK_STACK); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 819 | gLog.log("ipSecAddSecurityPolicy()"); |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 820 | return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityPolicy( |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 821 | transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue, |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 822 | markMask, interfaceId)); |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 823 | } |
| 824 | |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 825 | binder::Status NetdNativeService::ipSecUpdateSecurityPolicy( |
| 826 | int32_t transformId, int32_t selAddrFamily, int32_t direction, |
| 827 | const std::string& tmplSrcAddress, const std::string& tmplDstAddress, int32_t spi, |
| 828 | int32_t markValue, int32_t markMask, int32_t interfaceId) { |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 829 | // Necessary locking done in IpSecService and kernel |
| 830 | ENFORCE_PERMISSION(NETWORK_STACK); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 831 | gLog.log("ipSecAddSecurityPolicy()"); |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 832 | return asBinderStatus(gCtls->xfrmCtrl.ipSecUpdateSecurityPolicy( |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 833 | transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue, |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 834 | markMask, interfaceId)); |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 835 | } |
| 836 | |
Benedict Wong | a04ffa7 | 2018-05-09 21:42:42 -0700 | [diff] [blame] | 837 | binder::Status NetdNativeService::ipSecDeleteSecurityPolicy(int32_t transformId, |
| 838 | int32_t selAddrFamily, |
| 839 | int32_t direction, int32_t markValue, |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 840 | int32_t markMask, int32_t interfaceId) { |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 841 | // Necessary locking done in IpSecService and kernel |
| 842 | ENFORCE_PERMISSION(NETWORK_STACK); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 843 | gLog.log("ipSecAddSecurityPolicy()"); |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 844 | return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityPolicy( |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 845 | transformId, selAddrFamily, direction, markValue, markMask, interfaceId)); |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 846 | } |
| 847 | |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 848 | binder::Status NetdNativeService::ipSecAddTunnelInterface(const std::string& deviceName, |
| 849 | const std::string& localAddress, |
| 850 | const std::string& remoteAddress, |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 851 | int32_t iKey, int32_t oKey, |
| 852 | int32_t interfaceId) { |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 853 | // Necessary locking done in IpSecService and kernel |
| 854 | ENFORCE_PERMISSION(NETWORK_STACK); |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 855 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 856 | |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 857 | netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface( |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 858 | deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, false); |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 859 | RETURN_BINDER_STATUS_IF_NOT_OK(entry, result); |
| 860 | |
| 861 | gLog.log(entry.returns(result).withAutomaticDuration()); |
| 862 | return binder::Status::ok(); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 863 | } |
| 864 | |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 865 | binder::Status NetdNativeService::ipSecUpdateTunnelInterface(const std::string& deviceName, |
| 866 | const std::string& localAddress, |
| 867 | const std::string& remoteAddress, |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 868 | int32_t iKey, int32_t oKey, |
| 869 | int32_t interfaceId) { |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 870 | // Necessary locking done in IpSecService and kernel |
| 871 | ENFORCE_PERMISSION(NETWORK_STACK); |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 872 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 873 | |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 874 | netdutils::Status result = gCtls->xfrmCtrl.ipSecAddTunnelInterface( |
Benedict Wong | a450e72 | 2018-05-07 10:29:02 -0700 | [diff] [blame] | 875 | deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, true); |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 876 | RETURN_BINDER_STATUS_IF_NOT_OK(entry, result); |
| 877 | |
| 878 | gLog.log(entry.returns(result).withAutomaticDuration()); |
| 879 | return binder::Status::ok(); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 880 | } |
| 881 | |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 882 | binder::Status NetdNativeService::ipSecRemoveTunnelInterface(const std::string& deviceName) { |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 883 | // Necessary locking done in IpSecService and kernel |
| 884 | ENFORCE_PERMISSION(NETWORK_STACK); |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 885 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 886 | |
Benedict Wong | 319f17e | 2018-05-15 17:06:44 -0700 | [diff] [blame] | 887 | netdutils::Status result = gCtls->xfrmCtrl.ipSecRemoveTunnelInterface(deviceName); |
| 888 | RETURN_BINDER_STATUS_IF_NOT_OK(entry, result); |
| 889 | |
| 890 | gLog.log(entry.returns(result).withAutomaticDuration()); |
| 891 | return binder::Status::ok(); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 892 | } |
| 893 | |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 894 | binder::Status NetdNativeService::setIPv6AddrGenMode(const std::string& ifName, |
| 895 | int32_t mode) { |
| 896 | ENFORCE_PERMISSION(NETWORK_STACK); |
Nathan Harold | 28ccfef | 2018-03-16 19:02:47 -0700 | [diff] [blame] | 897 | return asBinderStatus(InterfaceController::setIPv6AddrGenMode(ifName, mode)); |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 898 | } |
| 899 | |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 900 | binder::Status NetdNativeService::wakeupAddInterface(const std::string& ifName, |
| 901 | const std::string& prefix, int32_t mark, |
| 902 | int32_t mask) { |
| 903 | ENFORCE_PERMISSION(NETWORK_STACK); |
Nathan Harold | 28ccfef | 2018-03-16 19:02:47 -0700 | [diff] [blame] | 904 | return asBinderStatus(gCtls->wakeupCtrl.addInterface(ifName, prefix, mark, mask)); |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | binder::Status NetdNativeService::wakeupDelInterface(const std::string& ifName, |
| 908 | const std::string& prefix, int32_t mark, |
| 909 | int32_t mask) { |
| 910 | ENFORCE_PERMISSION(NETWORK_STACK); |
Nathan Harold | 28ccfef | 2018-03-16 19:02:47 -0700 | [diff] [blame] | 911 | return asBinderStatus(gCtls->wakeupCtrl.delInterface(ifName, prefix, mark, mask)); |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 912 | } |
| 913 | |
Chenbo Feng | 07d43fe | 2017-12-21 14:38:51 -0800 | [diff] [blame] | 914 | binder::Status NetdNativeService::trafficCheckBpfStatsEnable(bool* ret) { |
| 915 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 916 | *ret = gCtls->trafficCtrl.checkBpfStatsEnable(); |
| 917 | return binder::Status::ok(); |
| 918 | } |
| 919 | |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 920 | binder::Status NetdNativeService::idletimerAddInterface(const std::string& ifName, int32_t timeout, |
| 921 | const std::string& classLabel) { |
| 922 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->idletimerCtrl.lock); |
| 923 | auto entry = gLog.newEntry() |
| 924 | .prettyFunction(__PRETTY_FUNCTION__) |
| 925 | .arg(ifName) |
| 926 | .arg(timeout) |
| 927 | .arg(classLabel); |
| 928 | int res = |
| 929 | gCtls->idletimerCtrl.addInterfaceIdletimer(ifName.c_str(), timeout, classLabel.c_str()); |
| 930 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 931 | return statusFromErrcode(res); |
| 932 | } |
| 933 | |
| 934 | binder::Status NetdNativeService::idletimerRemoveInterface(const std::string& ifName, |
| 935 | int32_t timeout, |
| 936 | const std::string& classLabel) { |
| 937 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->idletimerCtrl.lock); |
| 938 | auto entry = gLog.newEntry() |
| 939 | .prettyFunction(__PRETTY_FUNCTION__) |
| 940 | .arg(ifName) |
| 941 | .arg(timeout) |
| 942 | .arg(classLabel); |
| 943 | int res = gCtls->idletimerCtrl.removeInterfaceIdletimer(ifName.c_str(), timeout, |
| 944 | classLabel.c_str()); |
| 945 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 946 | return statusFromErrcode(res); |
| 947 | } |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 948 | |
| 949 | binder::Status NetdNativeService::strictUidCleartextPenalty(int32_t uid, int32_t policyPenalty) { |
| 950 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->strictCtrl.lock); |
| 951 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid).arg(policyPenalty); |
| 952 | StrictPenalty penalty; |
| 953 | switch (policyPenalty) { |
| 954 | case INetd::PENALTY_POLICY_REJECT: |
| 955 | penalty = REJECT; |
| 956 | break; |
| 957 | case INetd::PENALTY_POLICY_LOG: |
| 958 | penalty = LOG; |
| 959 | break; |
| 960 | case INetd::PENALTY_POLICY_ACCEPT: |
| 961 | penalty = ACCEPT; |
| 962 | break; |
| 963 | default: |
| 964 | return statusFromErrcode(-EINVAL); |
| 965 | break; |
| 966 | } |
| 967 | int res = gCtls->strictCtrl.setUidCleartextPenalty((uid_t) uid, penalty); |
| 968 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 969 | return statusFromErrcode(res); |
| 970 | } |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 971 | |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 972 | binder::Status NetdNativeService::clatdStart(const std::string& ifName) { |
| 973 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->clatdCtrl.mutex); |
| 974 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName); |
| 975 | int res = gCtls->clatdCtrl.startClatd(ifName.c_str()); |
| 976 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 977 | return statusFromErrcode(res); |
| 978 | } |
| 979 | |
| 980 | binder::Status NetdNativeService::clatdStop(const std::string& ifName) { |
| 981 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->clatdCtrl.mutex); |
| 982 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName); |
| 983 | int res = gCtls->clatdCtrl.stopClatd(ifName.c_str()); |
| 984 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 985 | return statusFromErrcode(res); |
| 986 | } |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 987 | |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 988 | binder::Status NetdNativeService::ipfwdEnabled(bool* status) { |
| 989 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
| 990 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
| 991 | *status = (gCtls->tetherCtrl.forwardingRequestCount() > 0) ? true : false; |
| 992 | gLog.log(entry.returns(*status).withAutomaticDuration()); |
| 993 | return binder::Status::ok(); |
| 994 | } |
| 995 | |
| 996 | binder::Status NetdNativeService::ipfwdEnableForwarding(const std::string& requester) { |
| 997 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
| 998 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(requester); |
| 999 | int res = (gCtls->tetherCtrl.enableForwarding(requester.c_str())) ? 0 : -EREMOTEIO; |
| 1000 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1001 | return statusFromErrcode(res); |
| 1002 | } |
| 1003 | |
| 1004 | binder::Status NetdNativeService::ipfwdDisableForwarding(const std::string& requester) { |
| 1005 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
| 1006 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(requester); |
| 1007 | int res = (gCtls->tetherCtrl.disableForwarding(requester.c_str())) ? 0 : -EREMOTEIO; |
| 1008 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1009 | return statusFromErrcode(res); |
| 1010 | } |
| 1011 | |
| 1012 | binder::Status NetdNativeService::ipfwdAddInterfaceForward(const std::string& fromIface, |
| 1013 | const std::string& toIface) { |
| 1014 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1015 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(fromIface).arg(toIface); |
| 1016 | int res = RouteController::enableTethering(fromIface.c_str(), toIface.c_str()); |
| 1017 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1018 | return statusFromErrcode(res); |
| 1019 | } |
| 1020 | |
| 1021 | binder::Status NetdNativeService::ipfwdRemoveInterfaceForward(const std::string& fromIface, |
| 1022 | const std::string& toIface) { |
| 1023 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1024 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(fromIface).arg(toIface); |
| 1025 | int res = RouteController::disableTethering(fromIface.c_str(), toIface.c_str()); |
| 1026 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1027 | return statusFromErrcode(res); |
| 1028 | } |
| 1029 | |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 1030 | namespace { |
| 1031 | std::string addSquareBrackets(const std::string& s) { |
| 1032 | return "[" + s + "]"; |
| 1033 | } |
| 1034 | |
| 1035 | std::string addCurlyBrackets(const std::string& s) { |
| 1036 | return "{" + s + "}"; |
| 1037 | } |
| 1038 | |
| 1039 | } // namespace |
| 1040 | binder::Status NetdNativeService::interfaceGetList(std::vector<std::string>* interfaceListResult) { |
| 1041 | NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex); |
| 1042 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
| 1043 | |
| 1044 | const auto& ifaceList = InterfaceController::getIfaceNames(); |
| 1045 | RETURN_BINDER_STATUS_IF_NOT_OK(entry, ifaceList); |
| 1046 | |
| 1047 | interfaceListResult->clear(); |
| 1048 | interfaceListResult->reserve(ifaceList.value().size()); |
| 1049 | interfaceListResult->insert(end(*interfaceListResult), begin(ifaceList.value()), |
| 1050 | end(ifaceList.value())); |
| 1051 | |
| 1052 | gLog.log(entry.returns(addSquareBrackets(base::Join(*interfaceListResult, ", "))) |
| 1053 | .withAutomaticDuration()); |
| 1054 | return binder::Status::ok(); |
| 1055 | } |
| 1056 | |
| 1057 | std::string interfaceConfigurationParcelToString(const InterfaceConfigurationParcel& cfg) { |
| 1058 | std::vector<std::string> result{cfg.ifName, cfg.hwAddr, cfg.ipv4Addr, |
| 1059 | std::to_string(cfg.prefixLength)}; |
| 1060 | result.insert(end(result), begin(cfg.flags), end(cfg.flags)); |
| 1061 | return addCurlyBrackets(base::Join(result, ", ")); |
| 1062 | } |
| 1063 | |
| 1064 | binder::Status NetdNativeService::interfaceGetCfg( |
| 1065 | const std::string& ifName, InterfaceConfigurationParcel* interfaceGetCfgResult) { |
| 1066 | NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex); |
| 1067 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName); |
| 1068 | |
| 1069 | const auto& cfgRes = InterfaceController::getCfg(ifName); |
| 1070 | RETURN_BINDER_STATUS_IF_NOT_OK(entry, cfgRes); |
| 1071 | |
| 1072 | *interfaceGetCfgResult = cfgRes.value(); |
| 1073 | gLog.log(entry.returns(interfaceConfigurationParcelToString(*interfaceGetCfgResult)) |
| 1074 | .withAutomaticDuration()); |
| 1075 | return binder::Status::ok(); |
| 1076 | } |
| 1077 | |
| 1078 | binder::Status NetdNativeService::interfaceSetCfg(const InterfaceConfigurationParcel& cfg) { |
| 1079 | NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex); |
| 1080 | auto entry = gLog.newEntry() |
| 1081 | .prettyFunction(__PRETTY_FUNCTION__) |
| 1082 | .arg(interfaceConfigurationParcelToString(cfg)); |
| 1083 | |
| 1084 | const auto& res = InterfaceController::setCfg(cfg); |
| 1085 | RETURN_BINDER_STATUS_IF_NOT_OK(entry, res); |
| 1086 | |
| 1087 | gLog.log(entry.withAutomaticDuration()); |
| 1088 | return binder::Status::ok(); |
| 1089 | } |
| 1090 | |
| 1091 | binder::Status NetdNativeService::interfaceSetIPv6PrivacyExtensions(const std::string& ifName, |
| 1092 | bool enable) { |
| 1093 | NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex); |
| 1094 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(ifName, enable); |
| 1095 | int res = InterfaceController::setIPv6PrivacyExtensions(ifName.c_str(), enable); |
| 1096 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1097 | return statusFromErrcode(res); |
| 1098 | } |
| 1099 | |
| 1100 | binder::Status NetdNativeService::interfaceClearAddrs(const std::string& ifName) { |
| 1101 | NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex); |
| 1102 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName); |
| 1103 | int res = InterfaceController::clearAddrs(ifName.c_str()); |
| 1104 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1105 | return statusFromErrcode(res); |
| 1106 | } |
| 1107 | |
| 1108 | binder::Status NetdNativeService::interfaceSetEnableIPv6(const std::string& ifName, bool enable) { |
| 1109 | NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex); |
| 1110 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(ifName, enable); |
| 1111 | int res = InterfaceController::setEnableIPv6(ifName.c_str(), enable); |
| 1112 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1113 | return statusFromErrcode(res); |
| 1114 | } |
| 1115 | |
| 1116 | binder::Status NetdNativeService::interfaceSetMtu(const std::string& ifName, int32_t mtuValue) { |
| 1117 | NETD_LOCKING_RPC(NETWORK_STACK, InterfaceController::mutex); |
| 1118 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(ifName, mtuValue); |
| 1119 | std::string mtu = std::to_string(mtuValue); |
| 1120 | int res = InterfaceController::setMtu(ifName.c_str(), mtu.c_str()); |
| 1121 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1122 | return statusFromErrcode(res); |
| 1123 | } |
| 1124 | |
Luke Huang | b5733d7 | 2018-08-21 17:17:19 +0800 | [diff] [blame] | 1125 | binder::Status NetdNativeService::tetherStart(const std::vector<std::string>& dhcpRanges) { |
| 1126 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
| 1127 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(dhcpRanges); |
| 1128 | if (dhcpRanges.size() % 2 == 1) { |
| 1129 | return statusFromErrcode(-EINVAL); |
| 1130 | } |
| 1131 | int res = gCtls->tetherCtrl.startTethering(dhcpRanges); |
| 1132 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1133 | return statusFromErrcode(res); |
| 1134 | } |
| 1135 | |
| 1136 | binder::Status NetdNativeService::tetherStop() { |
| 1137 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
| 1138 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
| 1139 | int res = gCtls->tetherCtrl.stopTethering(); |
| 1140 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1141 | return statusFromErrcode(res); |
| 1142 | } |
| 1143 | |
| 1144 | binder::Status NetdNativeService::tetherIsEnabled(bool* enabled) { |
| 1145 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
| 1146 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
| 1147 | *enabled = gCtls->tetherCtrl.isTetheringStarted(); |
| 1148 | gLog.log(entry.returns(*enabled).withAutomaticDuration()); |
| 1149 | return binder::Status::ok(); |
| 1150 | } |
| 1151 | |
| 1152 | binder::Status NetdNativeService::tetherInterfaceAdd(const std::string& ifName) { |
| 1153 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
| 1154 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName); |
| 1155 | int res = gCtls->tetherCtrl.tetherInterface(ifName.c_str()); |
| 1156 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1157 | return statusFromErrcode(res); |
| 1158 | } |
| 1159 | |
| 1160 | binder::Status NetdNativeService::tetherInterfaceRemove(const std::string& ifName) { |
| 1161 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
| 1162 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName); |
| 1163 | int res = gCtls->tetherCtrl.untetherInterface(ifName.c_str()); |
| 1164 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1165 | return statusFromErrcode(res); |
| 1166 | } |
| 1167 | |
| 1168 | binder::Status NetdNativeService::tetherInterfaceList(std::vector<std::string>* ifList) { |
| 1169 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
| 1170 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
| 1171 | for (const auto& ifname : gCtls->tetherCtrl.getTetheredInterfaceList()) { |
| 1172 | ifList->push_back(ifname); |
| 1173 | } |
| 1174 | gLog.log(entry.returns(true).withAutomaticDuration()); |
| 1175 | return binder::Status::ok(); |
| 1176 | } |
| 1177 | |
| 1178 | binder::Status NetdNativeService::tetherDnsSet(int32_t netId, |
| 1179 | const std::vector<std::string>& dnsAddrs) { |
| 1180 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
| 1181 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId).arg(dnsAddrs); |
| 1182 | int res = gCtls->tetherCtrl.setDnsForwarders(netId, dnsAddrs); |
| 1183 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1184 | return statusFromErrcode(res); |
| 1185 | } |
| 1186 | |
| 1187 | binder::Status NetdNativeService::tetherDnsList(std::vector<std::string>* dnsList) { |
| 1188 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
| 1189 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
| 1190 | for (const auto& fwdr : gCtls->tetherCtrl.getDnsForwarders()) { |
| 1191 | dnsList->push_back(fwdr); |
| 1192 | } |
| 1193 | gLog.log(entry.returns(true).withAutomaticDuration()); |
| 1194 | return binder::Status::ok(); |
| 1195 | } |
| 1196 | |
Luke Huang | b670d16 | 2018-08-23 20:01:13 +0800 | [diff] [blame] | 1197 | binder::Status NetdNativeService::networkAddRoute(int32_t netId, const std::string& ifName, |
| 1198 | const std::string& destination, |
| 1199 | const std::string& nextHop) { |
| 1200 | // Public methods of NetworkController are thread-safe. |
| 1201 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1202 | auto entry = gLog.newEntry() |
| 1203 | .prettyFunction(__PRETTY_FUNCTION__) |
| 1204 | .arg(netId) |
| 1205 | .arg(ifName) |
| 1206 | .arg(destination) |
| 1207 | .arg(nextHop); |
| 1208 | bool legacy = false; |
| 1209 | uid_t uid = 0; // UID is only meaningful for legacy routes. |
| 1210 | int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(), |
| 1211 | nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid); |
| 1212 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1213 | return statusFromErrcode(res); |
| 1214 | } |
| 1215 | |
| 1216 | binder::Status NetdNativeService::networkRemoveRoute(int32_t netId, const std::string& ifName, |
| 1217 | const std::string& destination, |
| 1218 | const std::string& nextHop) { |
| 1219 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1220 | auto entry = gLog.newEntry() |
| 1221 | .prettyFunction(__PRETTY_FUNCTION__) |
| 1222 | .arg(netId) |
| 1223 | .arg(ifName) |
| 1224 | .arg(destination) |
| 1225 | .arg(nextHop); |
| 1226 | bool legacy = false; |
| 1227 | uid_t uid = 0; // UID is only meaningful for legacy routes. |
| 1228 | int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(), |
| 1229 | nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid); |
| 1230 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1231 | return statusFromErrcode(res); |
| 1232 | } |
| 1233 | |
| 1234 | binder::Status NetdNativeService::networkAddLegacyRoute(int32_t netId, const std::string& ifName, |
| 1235 | const std::string& destination, |
| 1236 | const std::string& nextHop, int32_t uid) { |
| 1237 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1238 | auto entry = gLog.newEntry() |
| 1239 | .prettyFunction(__PRETTY_FUNCTION__) |
| 1240 | .arg(netId) |
| 1241 | .arg(ifName) |
| 1242 | .arg(destination) |
| 1243 | .arg(nextHop) |
| 1244 | .arg(uid); |
| 1245 | bool legacy = true; |
| 1246 | int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(), |
| 1247 | nextHop.empty() ? nullptr : nextHop.c_str(), legacy, |
| 1248 | (uid_t) uid); |
| 1249 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1250 | return statusFromErrcode(res); |
| 1251 | } |
| 1252 | |
| 1253 | binder::Status NetdNativeService::networkRemoveLegacyRoute(int32_t netId, const std::string& ifName, |
| 1254 | const std::string& destination, |
| 1255 | const std::string& nextHop, |
| 1256 | int32_t uid) { |
| 1257 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1258 | auto entry = gLog.newEntry() |
| 1259 | .prettyFunction(__PRETTY_FUNCTION__) |
| 1260 | .arg(netId) |
| 1261 | .arg(ifName) |
| 1262 | .arg(destination) |
| 1263 | .arg(nextHop) |
| 1264 | .arg(uid); |
| 1265 | bool legacy = true; |
| 1266 | int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(), |
| 1267 | nextHop.empty() ? nullptr : nextHop.c_str(), legacy, |
| 1268 | (uid_t) uid); |
| 1269 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1270 | return statusFromErrcode(res); |
| 1271 | } |
| 1272 | |
| 1273 | binder::Status NetdNativeService::networkGetDefault(int32_t* netId) { |
| 1274 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1275 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
| 1276 | *netId = gCtls->netCtrl.getDefaultNetwork(); |
| 1277 | gLog.log(entry.returns(*netId).withAutomaticDuration()); |
| 1278 | return binder::Status::ok(); |
| 1279 | } |
| 1280 | |
| 1281 | binder::Status NetdNativeService::networkSetDefault(int32_t netId) { |
| 1282 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1283 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId); |
| 1284 | int res = gCtls->netCtrl.setDefaultNetwork(netId); |
| 1285 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1286 | return statusFromErrcode(res); |
| 1287 | } |
| 1288 | |
| 1289 | binder::Status NetdNativeService::networkClearDefault() { |
| 1290 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1291 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
| 1292 | unsigned netId = NETID_UNSET; |
| 1293 | int res = gCtls->netCtrl.setDefaultNetwork(netId); |
| 1294 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1295 | return statusFromErrcode(res); |
| 1296 | } |
| 1297 | |
| 1298 | std::vector<uid_t> NetdNativeService::intsToUids(const std::vector<int32_t>& intUids) { |
| 1299 | return {begin(intUids), end(intUids)}; |
| 1300 | } |
| 1301 | |
| 1302 | Permission NetdNativeService::convertPermission(int32_t permission) { |
| 1303 | switch (permission) { |
| 1304 | case INetd::PERMISSION_NETWORK: |
| 1305 | return Permission::PERMISSION_NETWORK; |
| 1306 | case INetd::PERMISSION_SYSTEM: |
| 1307 | return Permission::PERMISSION_SYSTEM; |
| 1308 | default: |
| 1309 | return Permission::PERMISSION_NONE; |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | binder::Status NetdNativeService::networkSetPermissionForNetwork(int32_t netId, |
| 1314 | int32_t permission) { |
| 1315 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1316 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId).arg(permission); |
| 1317 | std::vector<unsigned> netIds = {(unsigned) netId}; |
| 1318 | int res = gCtls->netCtrl.setPermissionForNetworks(convertPermission(permission), netIds); |
| 1319 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1320 | return statusFromErrcode(res); |
| 1321 | } |
| 1322 | |
| 1323 | binder::Status NetdNativeService::networkSetPermissionForUser(int32_t permission, |
| 1324 | const std::vector<int32_t>& uids) { |
| 1325 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1326 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(permission).arg(uids); |
| 1327 | gCtls->netCtrl.setPermissionForUsers(convertPermission(permission), intsToUids(uids)); |
| 1328 | gLog.log(entry.withAutomaticDuration()); |
| 1329 | return binder::Status::ok(); |
| 1330 | } |
| 1331 | |
| 1332 | binder::Status NetdNativeService::networkClearPermissionForUser(const std::vector<int32_t>& uids) { |
| 1333 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1334 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uids); |
| 1335 | Permission permission = Permission::PERMISSION_NONE; |
| 1336 | gCtls->netCtrl.setPermissionForUsers(permission, intsToUids(uids)); |
| 1337 | gLog.log(entry.withAutomaticDuration()); |
| 1338 | return binder::Status::ok(); |
| 1339 | } |
| 1340 | |
| 1341 | binder::Status NetdNativeService::NetdNativeService::networkSetProtectAllow(int32_t uid) { |
| 1342 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1343 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid); |
| 1344 | std::vector<uid_t> uids = {(uid_t) uid}; |
| 1345 | gCtls->netCtrl.allowProtect(uids); |
| 1346 | gLog.log(entry.withAutomaticDuration()); |
| 1347 | return binder::Status::ok(); |
| 1348 | } |
| 1349 | |
| 1350 | binder::Status NetdNativeService::networkSetProtectDeny(int32_t uid) { |
| 1351 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1352 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid); |
| 1353 | std::vector<uid_t> uids = {(uid_t) uid}; |
| 1354 | gCtls->netCtrl.denyProtect(uids); |
| 1355 | gLog.log(entry.withAutomaticDuration()); |
| 1356 | return binder::Status::ok(); |
| 1357 | } |
| 1358 | |
| 1359 | binder::Status NetdNativeService::networkCanProtect(int32_t uid, bool* ret) { |
| 1360 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1361 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid); |
| 1362 | *ret = gCtls->netCtrl.canProtect((uid_t) uid); |
| 1363 | gLog.log(entry.returns(*ret).withAutomaticDuration()); |
| 1364 | return binder::Status::ok(); |
| 1365 | } |
| 1366 | |
Chenbo Feng | 48eaed3 | 2018-12-26 17:40:21 -0800 | [diff] [blame] | 1367 | binder::Status NetdNativeService::trafficSetNetPermForUids(int32_t permission, |
| 1368 | const std::vector<int32_t>& uids) { |
| 1369 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1370 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(permission).arg(uids); |
| 1371 | gCtls->trafficCtrl.setPermissionForUids(permission, intsToUids(uids)); |
| 1372 | gLog.log(entry.withAutomaticDuration()); |
| 1373 | return binder::Status::ok(); |
| 1374 | } |
| 1375 | |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 1376 | namespace { |
| 1377 | std::string ruleToString(int32_t rule) { |
| 1378 | switch (rule) { |
| 1379 | case INetd::FIREWALL_RULE_DENY: |
| 1380 | return "DENY"; |
| 1381 | case INetd::FIREWALL_RULE_ALLOW: |
| 1382 | return "ALLOW"; |
| 1383 | default: |
| 1384 | return "INVALID"; |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | std::string typeToString(int32_t type) { |
| 1389 | switch (type) { |
| 1390 | case INetd::FIREWALL_WHITELIST: |
| 1391 | return "WHITELIST"; |
| 1392 | case INetd::FIREWALL_BLACKLIST: |
| 1393 | return "BLACKLIST"; |
| 1394 | default: |
| 1395 | return "INVALID"; |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | std::string chainToString(int32_t chain) { |
| 1400 | switch (chain) { |
| 1401 | case INetd::FIREWALL_CHAIN_NONE: |
| 1402 | return "NONE"; |
| 1403 | case INetd::FIREWALL_CHAIN_DOZABLE: |
| 1404 | return "DOZABLE"; |
| 1405 | case INetd::FIREWALL_CHAIN_STANDBY: |
| 1406 | return "STANDBY"; |
| 1407 | case INetd::FIREWALL_CHAIN_POWERSAVE: |
| 1408 | return "POWERSAVE"; |
| 1409 | default: |
| 1410 | return "INVALID"; |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | } // namespace |
| 1415 | |
| 1416 | binder::Status NetdNativeService::firewallSetFirewallType(int32_t firewallType) { |
| 1417 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock); |
| 1418 | auto entry = |
| 1419 | gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(typeToString(firewallType)); |
| 1420 | auto type = static_cast<FirewallType>(firewallType); |
| 1421 | |
| 1422 | int res = gCtls->firewallCtrl.setFirewallType(type); |
| 1423 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1424 | return statusFromErrcode(res); |
| 1425 | } |
| 1426 | |
| 1427 | binder::Status NetdNativeService::firewallSetInterfaceRule(const std::string& ifName, |
| 1428 | int32_t firewallRule) { |
| 1429 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock); |
| 1430 | auto entry = gLog.newEntry() |
| 1431 | .prettyFunction(__PRETTY_FUNCTION__) |
| 1432 | .args(ifName, ruleToString(firewallRule)); |
| 1433 | auto rule = static_cast<FirewallRule>(firewallRule); |
| 1434 | |
| 1435 | int res = gCtls->firewallCtrl.setInterfaceRule(ifName.c_str(), rule); |
| 1436 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1437 | return statusFromErrcode(res); |
| 1438 | } |
| 1439 | |
| 1440 | binder::Status NetdNativeService::firewallSetUidRule(int32_t childChain, int32_t uid, |
| 1441 | int32_t firewallRule) { |
| 1442 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock); |
| 1443 | auto entry = gLog.newEntry() |
| 1444 | .prettyFunction(__PRETTY_FUNCTION__) |
| 1445 | .args(chainToString(childChain), uid, ruleToString(firewallRule)); |
| 1446 | auto chain = static_cast<ChildChain>(childChain); |
| 1447 | auto rule = static_cast<FirewallRule>(firewallRule); |
| 1448 | |
| 1449 | int res = gCtls->firewallCtrl.setUidRule(chain, uid, rule); |
| 1450 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1451 | return statusFromErrcode(res); |
| 1452 | } |
| 1453 | |
| 1454 | binder::Status NetdNativeService::firewallEnableChildChain(int32_t childChain, bool enable) { |
| 1455 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->firewallCtrl.lock); |
| 1456 | auto entry = gLog.newEntry() |
| 1457 | .prettyFunction(__PRETTY_FUNCTION__) |
| 1458 | .args(chainToString(childChain), enable); |
| 1459 | auto chain = static_cast<ChildChain>(childChain); |
| 1460 | |
| 1461 | int res = gCtls->firewallCtrl.enableChildChains(chain, enable); |
| 1462 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1463 | return statusFromErrcode(res); |
| 1464 | } |
| 1465 | |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 1466 | binder::Status NetdNativeService::tetherAddForward(const std::string& intIface, |
| 1467 | const std::string& extIface) { |
| 1468 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
| 1469 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(intIface, extIface); |
| 1470 | |
| 1471 | int res = gCtls->tetherCtrl.enableNat(intIface.c_str(), extIface.c_str()); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 1472 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1473 | return statusFromErrcode(res); |
| 1474 | } |
| 1475 | |
| 1476 | binder::Status NetdNativeService::tetherRemoveForward(const std::string& intIface, |
| 1477 | const std::string& extIface) { |
| 1478 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 1479 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(intIface, extIface); |
| 1480 | |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 1481 | int res = gCtls->tetherCtrl.disableNat(intIface.c_str(), extIface.c_str()); |
Luke Huang | 19b49c5 | 2018-10-22 12:12:05 +0900 | [diff] [blame] | 1482 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 1483 | return statusFromErrcode(res); |
| 1484 | } |
| 1485 | |
Chenbo Feng | f5663d8 | 2018-11-08 16:10:48 -0800 | [diff] [blame] | 1486 | binder::Status NetdNativeService::setTcpRWmemorySize(const std::string& rmemValues, |
| 1487 | const std::string& wmemValues) { |
| 1488 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1489 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).args(rmemValues, wmemValues); |
| 1490 | if (!WriteStringToFile(rmemValues, TCP_RMEM_PROC_FILE)) { |
| 1491 | int ret = -errno; |
| 1492 | gLog.log(entry.returns(ret).withAutomaticDuration()); |
| 1493 | return statusFromErrcode(ret); |
| 1494 | } |
| 1495 | |
| 1496 | if (!WriteStringToFile(wmemValues, TCP_WMEM_PROC_FILE)) { |
| 1497 | int ret = -errno; |
| 1498 | gLog.log(entry.returns(ret).withAutomaticDuration()); |
| 1499 | return statusFromErrcode(ret); |
| 1500 | } |
| 1501 | gLog.log(entry.withAutomaticDuration()); |
| 1502 | return binder::Status::ok(); |
| 1503 | } |
| 1504 | |
nuccachen | f52f7a5 | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 1505 | binder::Status NetdNativeService::getPrefix64(int netId, std::string* _aidl_return) { |
| 1506 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 1507 | |
| 1508 | netdutils::IPPrefix prefix{}; |
| 1509 | int err = gCtls->resolverCtrl.getPrefix64(netId, &prefix); |
| 1510 | if (err != 0) { |
| 1511 | return binder::Status::fromServiceSpecificError( |
| 1512 | -err, String8::format("ResolverController error: %s", strerror(-err))); |
| 1513 | } |
| 1514 | *_aidl_return = prefix.toString(); |
| 1515 | return binder::Status::ok(); |
| 1516 | } |
| 1517 | |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 1518 | binder::Status NetdNativeService::registerUnsolicitedEventListener( |
| 1519 | const android::sp<android::net::INetdUnsolicitedEventListener>& listener) { |
| 1520 | ENFORCE_PERMISSION(NETWORK_STACK); |
Bernie Innocenti | 9ee088d | 2019-02-01 17:14:32 +0900 | [diff] [blame^] | 1521 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
| 1522 | gCtls->eventReporter.registerUnsolEventListener(listener); |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 1523 | gLog.log(entry.withAutomaticDuration()); |
| 1524 | return binder::Status::ok(); |
| 1525 | } |
| 1526 | |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 1527 | } // namespace net |
| 1528 | } // namespace android |