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