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 | |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 19 | #include <set> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 23 | #include <android-base/strings.h> |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 24 | #include <cutils/properties.h> |
Logan Chien | 3f46148 | 2018-04-23 14:31:32 +0800 | [diff] [blame] | 25 | #include <log/log.h> |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 26 | #include <utils/Errors.h> |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 27 | #include <utils/String16.h> |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 28 | |
| 29 | #include <binder/IPCThreadState.h> |
| 30 | #include <binder/IServiceManager.h> |
| 31 | #include "android/net/BnNetd.h" |
| 32 | |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 33 | #include <openssl/base64.h> |
| 34 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 35 | #include "Controllers.h" |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 36 | #include "DumpWriter.h" |
Michal Karpinski | d544011 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 37 | #include "EventReporter.h" |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 38 | #include "InterfaceController.h" |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 39 | #include "NetdConstants.h" |
| 40 | #include "NetdNativeService.h" |
Erik Kline | 8589004 | 2018-05-25 19:19:11 +0900 | [diff] [blame] | 41 | #include "Process.h" |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 42 | #include "RouteController.h" |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 43 | #include "SockDiag.h" |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 44 | #include "UidRanges.h" |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 45 | |
| 46 | using android::base::StringPrintf; |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 47 | using android::os::PersistableBundle; |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 48 | |
| 49 | namespace android { |
| 50 | namespace net { |
| 51 | |
| 52 | namespace { |
| 53 | |
| 54 | const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL"; |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 55 | const char NETWORK_STACK[] = "android.permission.NETWORK_STACK"; |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 56 | const char DUMP[] = "android.permission.DUMP"; |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 57 | const char OPT_SHORT[] = "--short"; |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 58 | |
| 59 | binder::Status checkPermission(const char *permission) { |
| 60 | pid_t pid; |
| 61 | uid_t uid; |
| 62 | |
| 63 | if (checkCallingPermission(String16(permission), (int32_t *) &pid, (int32_t *) &uid)) { |
| 64 | return binder::Status::ok(); |
| 65 | } else { |
| 66 | auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission); |
| 67 | return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str())); |
| 68 | } |
| 69 | } |
| 70 | |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 71 | #define ENFORCE_DEBUGGABLE() { \ |
| 72 | char value[PROPERTY_VALUE_MAX + 1]; \ |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 73 | if (property_get("ro.debuggable", value, nullptr) != 1 \ |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 74 | || value[0] != '1') { \ |
| 75 | return binder::Status::fromExceptionCode( \ |
| 76 | binder::Status::EX_SECURITY, \ |
| 77 | String8("Not available in production builds.") \ |
| 78 | ); \ |
| 79 | } \ |
| 80 | } |
| 81 | |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 82 | #define ENFORCE_PERMISSION(permission) { \ |
| 83 | binder::Status status = checkPermission((permission)); \ |
| 84 | if (!status.isOk()) { \ |
| 85 | return status; \ |
| 86 | } \ |
| 87 | } |
| 88 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 89 | #define NETD_LOCKING_RPC(permission, lock) \ |
| 90 | ENFORCE_PERMISSION(permission); \ |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 91 | std::lock_guard _lock(lock); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 92 | |
| 93 | #define NETD_BIG_LOCK_RPC(permission) NETD_LOCKING_RPC((permission), gBigNetdLock) |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 94 | |
| 95 | inline binder::Status statusFromErrcode(int ret) { |
| 96 | if (ret) { |
| 97 | return binder::Status::fromServiceSpecificError(-ret, strerror(-ret)); |
| 98 | } |
| 99 | return binder::Status::ok(); |
| 100 | } |
| 101 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 102 | bool contains(const Vector<String16>& words, const String16& word) { |
| 103 | for (const auto& w : words) { |
| 104 | if (w == word) return true; |
| 105 | } |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 106 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 107 | return false; |
| 108 | } |
| 109 | |
| 110 | } // namespace |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 111 | |
Lorenzo Colitti | e4851de | 2016-03-17 13:23:28 +0900 | [diff] [blame] | 112 | status_t NetdNativeService::start() { |
| 113 | IPCThreadState::self()->disableBackgroundScheduling(true); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 114 | const status_t ret = BinderService<NetdNativeService>::publish(); |
Lorenzo Colitti | e4851de | 2016-03-17 13:23:28 +0900 | [diff] [blame] | 115 | if (ret != android::OK) { |
| 116 | return ret; |
| 117 | } |
| 118 | sp<ProcessState> ps(ProcessState::self()); |
| 119 | ps->startThreadPool(); |
| 120 | ps->giveThreadPoolName(); |
| 121 | return android::OK; |
| 122 | } |
| 123 | |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 124 | status_t NetdNativeService::dump(int fd, const Vector<String16> &args) { |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 125 | const binder::Status dump_permission = checkPermission(DUMP); |
| 126 | if (!dump_permission.isOk()) { |
| 127 | const String8 msg(dump_permission.toString8()); |
| 128 | write(fd, msg.string(), msg.size()); |
| 129 | return PERMISSION_DENIED; |
| 130 | } |
| 131 | |
| 132 | // This method does not grab any locks. If individual classes need locking |
| 133 | // their dump() methods MUST handle locking appropriately. |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 134 | |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 135 | DumpWriter dw(fd); |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 136 | |
| 137 | if (!args.isEmpty() && args[0] == TcpSocketMonitor::DUMP_KEYWORD) { |
| 138 | dw.blankline(); |
| 139 | gCtls->tcpSocketMonitor.dump(dw); |
| 140 | dw.blankline(); |
| 141 | return NO_ERROR; |
| 142 | } |
| 143 | |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 144 | if (!args.isEmpty() && args[0] == TrafficController::DUMP_KEYWORD) { |
| 145 | dw.blankline(); |
| 146 | gCtls->trafficCtrl.dump(dw, true); |
| 147 | dw.blankline(); |
| 148 | return NO_ERROR; |
| 149 | } |
| 150 | |
Erik Kline | 8589004 | 2018-05-25 19:19:11 +0900 | [diff] [blame] | 151 | process::dump(dw); |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 152 | dw.blankline(); |
| 153 | gCtls->netCtrl.dump(dw); |
| 154 | dw.blankline(); |
| 155 | |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 156 | gCtls->trafficCtrl.dump(dw, false); |
| 157 | dw.blankline(); |
| 158 | |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 159 | { |
| 160 | ScopedIndent indentLog(dw); |
| 161 | if (contains(args, String16(OPT_SHORT))) { |
| 162 | dw.println("Log: <omitted>"); |
| 163 | } else { |
| 164 | dw.println("Log:"); |
| 165 | ScopedIndent indentLogEntries(dw); |
| 166 | gLog.forEachEntry([&dw](const std::string& entry) mutable { dw.println(entry); }); |
| 167 | } |
| 168 | dw.blankline(); |
| 169 | } |
| 170 | |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 171 | return NO_ERROR; |
| 172 | } |
| 173 | |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 174 | binder::Status NetdNativeService::isAlive(bool *alive) { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 175 | NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 176 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 177 | |
| 178 | *alive = true; |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 179 | |
| 180 | gLog.log(entry.returns(*alive)); |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 181 | return binder::Status::ok(); |
| 182 | } |
| 183 | |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 184 | binder::Status NetdNativeService::firewallReplaceUidChain(const std::string& chainName, |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 185 | bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) { |
| 186 | NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->firewallCtrl.lock); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 187 | auto entry = gLog.newEntry() |
| 188 | .prettyFunction(__PRETTY_FUNCTION__) |
| 189 | .arg(chainName) |
| 190 | .arg(isWhitelist) |
| 191 | .arg(uids); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 192 | |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 193 | int err = gCtls->firewallCtrl.replaceUidChain(chainName, isWhitelist, uids); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 194 | *ret = (err == 0); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 195 | |
| 196 | gLog.log(entry.returns(*ret).withAutomaticDuration()); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 197 | return binder::Status::ok(); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 198 | } |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 199 | |
| 200 | binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) { |
| 201 | NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->bandwidthCtrl.lock); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 202 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(enable); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 203 | |
| 204 | int err = gCtls->bandwidthCtrl.enableDataSaver(enable); |
| 205 | *ret = (err == 0); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 206 | gLog.log(entry.returns(*ret).withAutomaticDuration()); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 207 | return binder::Status::ok(); |
| 208 | } |
| 209 | |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 210 | binder::Status NetdNativeService::networkCreatePhysical(int32_t netId, |
| 211 | const std::string& permission) { |
| 212 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 213 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId).arg(permission); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 214 | int ret = gCtls->netCtrl.createPhysicalNetwork(netId, stringToPermission(permission.c_str())); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 215 | gLog.log(entry.returns(ret).withAutomaticDuration()); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 216 | return statusFromErrcode(ret); |
| 217 | } |
| 218 | |
| 219 | binder::Status NetdNativeService::networkCreateVpn(int32_t netId, bool hasDns, bool secure) { |
| 220 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 221 | int ret = gCtls->netCtrl.createVirtualNetwork(netId, hasDns, secure); |
| 222 | return statusFromErrcode(ret); |
| 223 | } |
| 224 | |
| 225 | binder::Status NetdNativeService::networkDestroy(int32_t netId) { |
Erik Kline | c8b6a9c | 2018-01-15 17:06:48 +0900 | [diff] [blame] | 226 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 227 | // Both of these functions manage their own locking internally. |
| 228 | const int ret = gCtls->netCtrl.destroyNetwork(netId); |
| 229 | gCtls->resolverCtrl.clearDnsServers(netId); |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 230 | return statusFromErrcode(ret); |
| 231 | } |
| 232 | |
| 233 | binder::Status NetdNativeService::networkAddInterface(int32_t netId, const std::string& iface) { |
| 234 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 235 | int ret = gCtls->netCtrl.addInterfaceToNetwork(netId, iface.c_str()); |
| 236 | return statusFromErrcode(ret); |
| 237 | } |
| 238 | |
| 239 | binder::Status NetdNativeService::networkRemoveInterface(int32_t netId, const std::string& iface) { |
| 240 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 241 | int ret = gCtls->netCtrl.removeInterfaceFromNetwork(netId, iface.c_str()); |
| 242 | return statusFromErrcode(ret); |
| 243 | } |
| 244 | |
| 245 | binder::Status NetdNativeService::networkAddUidRanges(int32_t netId, |
| 246 | const std::vector<UidRange>& uidRangeArray) { |
| 247 | // NetworkController::addUsersToNetwork is thread-safe. |
| 248 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 249 | int ret = gCtls->netCtrl.addUsersToNetwork(netId, UidRanges(uidRangeArray)); |
| 250 | return statusFromErrcode(ret); |
| 251 | } |
| 252 | |
| 253 | binder::Status NetdNativeService::networkRemoveUidRanges(int32_t netId, |
| 254 | const std::vector<UidRange>& uidRangeArray) { |
| 255 | // NetworkController::removeUsersFromNetwork is thread-safe. |
| 256 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 257 | int ret = gCtls->netCtrl.removeUsersFromNetwork(netId, UidRanges(uidRangeArray)); |
| 258 | return statusFromErrcode(ret); |
| 259 | } |
| 260 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 261 | binder::Status NetdNativeService::networkRejectNonSecureVpn(bool add, |
| 262 | const std::vector<UidRange>& uidRangeArray) { |
| 263 | // TODO: elsewhere RouteController is only used from the tethering and network controllers, so |
| 264 | // it should be possible to use the same lock as NetworkController. However, every call through |
| 265 | // the CommandListener "network" command will need to hold this lock too, not just the ones that |
| 266 | // read/modify network internal state (that is sufficient for ::dump() because it doesn't |
| 267 | // look at routes, but it's not enough here). |
| 268 | NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL); |
| 269 | |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 270 | UidRanges uidRanges(uidRangeArray); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 271 | |
| 272 | int err; |
| 273 | if (add) { |
| 274 | err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges); |
| 275 | } else { |
| 276 | err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges); |
| 277 | } |
| 278 | |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 279 | return statusFromErrcode(err); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 282 | binder::Status NetdNativeService::socketDestroy(const std::vector<UidRange>& uids, |
| 283 | const std::vector<int32_t>& skipUids) { |
| 284 | |
| 285 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 286 | |
| 287 | SockDiag sd; |
| 288 | if (!sd.open()) { |
| 289 | return binder::Status::fromServiceSpecificError(EIO, |
| 290 | String8("Could not open SOCK_DIAG socket")); |
| 291 | } |
| 292 | |
| 293 | UidRanges uidRanges(uids); |
Lorenzo Colitti | e5c3c99 | 2016-07-26 17:53:50 +0900 | [diff] [blame] | 294 | int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()), |
| 295 | true /* excludeLoopback */); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 296 | |
| 297 | if (err) { |
| 298 | return binder::Status::fromServiceSpecificError(-err, |
| 299 | String8::format("destroySockets: %s", strerror(-err))); |
| 300 | } |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 301 | return binder::Status::ok(); |
| 302 | } |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 303 | |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 304 | // Parse a base64 encoded string into a vector of bytes. |
| 305 | // On failure, return an empty vector. |
| 306 | static std::vector<uint8_t> parseBase64(const std::string& input) { |
| 307 | std::vector<uint8_t> decoded; |
| 308 | size_t out_len; |
| 309 | if (EVP_DecodedLength(&out_len, input.size()) != 1) { |
| 310 | return decoded; |
| 311 | } |
| 312 | // out_len is now an upper bound on the output length. |
| 313 | decoded.resize(out_len); |
| 314 | if (EVP_DecodeBase64(decoded.data(), &out_len, decoded.size(), |
| 315 | reinterpret_cast<const uint8_t*>(input.data()), input.size()) == 1) { |
| 316 | // Possibly shrink the vector if the actual output was smaller than the bound. |
| 317 | decoded.resize(out_len); |
| 318 | } else { |
| 319 | decoded.clear(); |
| 320 | } |
| 321 | if (out_len != SHA256_SIZE) { |
| 322 | decoded.clear(); |
| 323 | } |
| 324 | return decoded; |
| 325 | } |
| 326 | |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 327 | binder::Status NetdNativeService::setResolverConfiguration(int32_t netId, |
| 328 | const std::vector<std::string>& servers, const std::vector<std::string>& domains, |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 329 | const std::vector<int32_t>& params, const std::string& tlsName, |
| 330 | const std::vector<std::string>& tlsServers, |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 331 | const std::vector<std::string>& tlsFingerprints) { |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 332 | // This function intentionally does not lock within Netd, as Bionic is thread-safe. |
| 333 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 334 | auto entry = gLog.newEntry() |
| 335 | .prettyFunction(__PRETTY_FUNCTION__) |
| 336 | .arg(netId) |
| 337 | .arg(servers) |
| 338 | .arg(domains) |
| 339 | .arg(params) |
| 340 | .arg(tlsName) |
| 341 | .arg(tlsServers) |
| 342 | .arg(tlsFingerprints); |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 343 | |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 344 | std::set<std::vector<uint8_t>> decoded_fingerprints; |
| 345 | for (const std::string& fingerprint : tlsFingerprints) { |
| 346 | std::vector<uint8_t> decoded = parseBase64(fingerprint); |
| 347 | if (decoded.empty()) { |
| 348 | return binder::Status::fromServiceSpecificError(EINVAL, |
| 349 | String8::format("ResolverController error: bad fingerprint")); |
| 350 | } |
| 351 | decoded_fingerprints.emplace(decoded); |
| 352 | } |
| 353 | |
| 354 | int err = gCtls->resolverCtrl.setResolverConfiguration(netId, servers, domains, params, |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 355 | tlsName, tlsServers, decoded_fingerprints); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 356 | gLog.log(entry.returns(err).withAutomaticDuration()); |
Pierre Imai | beedec3 | 2016-04-13 06:44:51 +0900 | [diff] [blame] | 357 | if (err != 0) { |
| 358 | return binder::Status::fromServiceSpecificError(-err, |
| 359 | String8::format("ResolverController error: %s", strerror(-err))); |
| 360 | } |
| 361 | return binder::Status::ok(); |
| 362 | } |
| 363 | |
| 364 | binder::Status NetdNativeService::getResolverInfo(int32_t netId, |
| 365 | std::vector<std::string>* servers, std::vector<std::string>* domains, |
| 366 | std::vector<int32_t>* params, std::vector<int32_t>* stats) { |
| 367 | // This function intentionally does not lock within Netd, as Bionic is thread-safe. |
| 368 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 369 | |
| 370 | int err = gCtls->resolverCtrl.getResolverInfo(netId, servers, domains, params, stats); |
| 371 | if (err != 0) { |
| 372 | return binder::Status::fromServiceSpecificError(-err, |
| 373 | String8::format("ResolverController error: %s", strerror(-err))); |
| 374 | } |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 375 | return binder::Status::ok(); |
| 376 | } |
| 377 | |
Erik Kline | f48e4dd | 2016-07-18 04:02:07 +0900 | [diff] [blame] | 378 | binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) { |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 379 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
Erik Kline | f48e4dd | 2016-07-18 04:02:07 +0900 | [diff] [blame] | 380 | |
| 381 | *ret = gCtls->tetherCtrl.applyDnsInterfaces(); |
| 382 | return binder::Status::ok(); |
| 383 | } |
| 384 | |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 385 | namespace { |
| 386 | |
| 387 | void tetherAddStats(PersistableBundle *bundle, const TetherController::TetherStats& stats) { |
| 388 | String16 iface = String16(stats.extIface.c_str()); |
| 389 | std::vector<int64_t> statsVector(INetd::TETHER_STATS_ARRAY_SIZE); |
| 390 | |
| 391 | bundle->getLongVector(iface, &statsVector); |
| 392 | if (statsVector.size() == 0) { |
| 393 | for (int i = 0; i < INetd::TETHER_STATS_ARRAY_SIZE; i++) statsVector.push_back(0); |
| 394 | } |
| 395 | |
Lorenzo Colitti | 9a65ac6 | 2017-09-04 18:07:56 +0900 | [diff] [blame] | 396 | statsVector[INetd::TETHER_STATS_RX_BYTES] += stats.rxBytes; |
| 397 | statsVector[INetd::TETHER_STATS_RX_PACKETS] += stats.rxPackets; |
| 398 | statsVector[INetd::TETHER_STATS_TX_BYTES] += stats.txBytes; |
| 399 | statsVector[INetd::TETHER_STATS_TX_PACKETS] += stats.txPackets; |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 400 | |
| 401 | bundle->putLongVector(iface, statsVector); |
| 402 | } |
| 403 | |
| 404 | } // namespace |
| 405 | |
| 406 | binder::Status NetdNativeService::tetherGetStats(PersistableBundle *bundle) { |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 407 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 408 | |
Lorenzo Colitti | 5192bf7 | 2017-09-04 13:30:59 +0900 | [diff] [blame] | 409 | const auto& statsList = gCtls->tetherCtrl.getTetherStats(); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 410 | if (!isOk(statsList)) { |
Nathan Harold | 28ccfef | 2018-03-16 19:02:47 -0700 | [diff] [blame] | 411 | return asBinderStatus(statsList); |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | for (const auto& stats : statsList.value()) { |
| 415 | tetherAddStats(bundle, stats); |
| 416 | } |
| 417 | |
| 418 | return binder::Status::ok(); |
| 419 | } |
| 420 | |
Erik Kline | 53c2088 | 2016-08-02 15:22:53 +0900 | [diff] [blame] | 421 | binder::Status NetdNativeService::interfaceAddAddress(const std::string &ifName, |
| 422 | const std::string &addrString, int prefixLength) { |
| 423 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 424 | |
| 425 | const int err = InterfaceController::addAddress( |
| 426 | ifName.c_str(), addrString.c_str(), prefixLength); |
| 427 | if (err != 0) { |
| 428 | return binder::Status::fromServiceSpecificError(-err, |
| 429 | String8::format("InterfaceController error: %s", strerror(-err))); |
| 430 | } |
| 431 | return binder::Status::ok(); |
| 432 | } |
| 433 | |
| 434 | binder::Status NetdNativeService::interfaceDelAddress(const std::string &ifName, |
| 435 | const std::string &addrString, int prefixLength) { |
| 436 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 437 | |
| 438 | const int err = InterfaceController::delAddress( |
| 439 | ifName.c_str(), addrString.c_str(), prefixLength); |
| 440 | if (err != 0) { |
| 441 | return binder::Status::fromServiceSpecificError(-err, |
| 442 | String8::format("InterfaceController error: %s", strerror(-err))); |
| 443 | } |
| 444 | return binder::Status::ok(); |
| 445 | } |
| 446 | |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 447 | binder::Status NetdNativeService::setProcSysNet( |
| 448 | int32_t family, int32_t which, const std::string &ifname, const std::string ¶meter, |
| 449 | const std::string &value) { |
| 450 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 451 | |
| 452 | const char *familyStr; |
| 453 | switch (family) { |
| 454 | case INetd::IPV4: |
| 455 | familyStr = "ipv4"; |
| 456 | break; |
| 457 | case INetd::IPV6: |
| 458 | familyStr = "ipv6"; |
| 459 | break; |
| 460 | default: |
| 461 | return binder::Status::fromServiceSpecificError(EAFNOSUPPORT, String8("Bad family")); |
| 462 | } |
| 463 | |
| 464 | const char *whichStr; |
| 465 | switch (which) { |
| 466 | case INetd::CONF: |
| 467 | whichStr = "conf"; |
| 468 | break; |
| 469 | case INetd::NEIGH: |
| 470 | whichStr = "neigh"; |
| 471 | break; |
| 472 | default: |
| 473 | return binder::Status::fromServiceSpecificError(EINVAL, String8("Bad category")); |
| 474 | } |
| 475 | |
| 476 | const int err = InterfaceController::setParameter( |
| 477 | familyStr, whichStr, ifname.c_str(), parameter.c_str(), |
| 478 | value.c_str()); |
| 479 | if (err != 0) { |
| 480 | return binder::Status::fromServiceSpecificError(-err, |
| 481 | String8::format("ResolverController error: %s", strerror(-err))); |
| 482 | } |
| 483 | return binder::Status::ok(); |
| 484 | } |
| 485 | |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 486 | binder::Status NetdNativeService::getMetricsReportingLevel(int *reportingLevel) { |
| 487 | // This function intentionally does not lock, since the only thing it does is one read from an |
| 488 | // atomic_int. |
| 489 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 490 | ENFORCE_DEBUGGABLE(); |
| 491 | |
Michal Karpinski | d544011 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 492 | *reportingLevel = gCtls->eventReporter.getMetricsReportingLevel(); |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 493 | return binder::Status::ok(); |
| 494 | } |
| 495 | |
| 496 | binder::Status NetdNativeService::setMetricsReportingLevel(const int reportingLevel) { |
| 497 | // This function intentionally does not lock, since the only thing it does is one write to an |
| 498 | // atomic_int. |
| 499 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
| 500 | ENFORCE_DEBUGGABLE(); |
| 501 | |
Michal Karpinski | d544011 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 502 | return (gCtls->eventReporter.setMetricsReportingLevel(reportingLevel) == 0) |
| 503 | ? binder::Status::ok() |
| 504 | : binder::Status::fromExceptionCode(binder::Status::EX_ILLEGAL_ARGUMENT); |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 505 | } |
| 506 | |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 507 | binder::Status NetdNativeService::ipSecSetEncapSocketOwner(const android::base::unique_fd& socket, |
| 508 | int newUid) { |
| 509 | ENFORCE_PERMISSION(NETWORK_STACK) |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 510 | gLog.log("ipSecSetEncapSocketOwner()"); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 511 | |
| 512 | uid_t callerUid = IPCThreadState::self()->getCallingUid(); |
| 513 | return asBinderStatus(gCtls->xfrmCtrl.ipSecSetEncapSocketOwner(socket, newUid, callerUid)); |
| 514 | } |
| 515 | |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 516 | binder::Status NetdNativeService::ipSecAllocateSpi( |
| 517 | int32_t transformId, |
Nathan Harold | da54f12 | 2018-01-09 16:42:57 -0800 | [diff] [blame] | 518 | const std::string& sourceAddress, |
| 519 | const std::string& destinationAddress, |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 520 | int32_t inSpi, |
| 521 | int32_t* outSpi) { |
| 522 | // Necessary locking done in IpSecService and kernel |
| 523 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 524 | gLog.log("ipSecAllocateSpi()"); |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 525 | return asBinderStatus(gCtls->xfrmCtrl.ipSecAllocateSpi( |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 526 | transformId, |
Nathan Harold | da54f12 | 2018-01-09 16:42:57 -0800 | [diff] [blame] | 527 | sourceAddress, |
| 528 | destinationAddress, |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 529 | inSpi, |
| 530 | outSpi)); |
| 531 | } |
| 532 | |
| 533 | binder::Status NetdNativeService::ipSecAddSecurityAssociation( |
| 534 | int32_t transformId, |
| 535 | int32_t mode, |
Nathan Harold | da54f12 | 2018-01-09 16:42:57 -0800 | [diff] [blame] | 536 | const std::string& sourceAddress, |
| 537 | const std::string& destinationAddress, |
Benedict Wong | 96abf48 | 2018-01-22 13:56:41 -0800 | [diff] [blame] | 538 | int32_t underlyingNetId, |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 539 | int32_t spi, |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 540 | int32_t markValue, |
| 541 | int32_t markMask, |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 542 | const std::string& authAlgo, const std::vector<uint8_t>& authKey, int32_t authTruncBits, |
| 543 | const std::string& cryptAlgo, const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits, |
Benedict Wong | be65b43 | 2017-08-22 21:43:14 -0700 | [diff] [blame] | 544 | const std::string& aeadAlgo, const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits, |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 545 | int32_t encapType, |
| 546 | int32_t encapLocalPort, |
ludi | ec83605 | 2017-05-20 14:17:05 -0700 | [diff] [blame] | 547 | int32_t encapRemotePort) { |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 548 | // Necessary locking done in IpSecService and kernel |
| 549 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 550 | gLog.log("ipSecAddSecurityAssociation()"); |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 551 | return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityAssociation( |
Benedict Wong | ad600cb | 2018-05-14 17:22:35 -0700 | [diff] [blame^] | 552 | transformId, mode, sourceAddress, destinationAddress, underlyingNetId, spi, markValue, |
| 553 | markMask, authAlgo, authKey, authTruncBits, cryptAlgo, cryptKey, cryptTruncBits, |
| 554 | aeadAlgo, aeadKey, aeadIcvBits, encapType, encapLocalPort, encapRemotePort)); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | binder::Status NetdNativeService::ipSecDeleteSecurityAssociation( |
| 558 | int32_t transformId, |
Nathan Harold | da54f12 | 2018-01-09 16:42:57 -0800 | [diff] [blame] | 559 | const std::string& sourceAddress, |
| 560 | const std::string& destinationAddress, |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 561 | int32_t spi, |
| 562 | int32_t markValue, |
| 563 | int32_t markMask) { |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 564 | // Necessary locking done in IpSecService and kernel |
| 565 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 566 | gLog.log("ipSecDeleteSecurityAssociation()"); |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 567 | return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityAssociation( |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 568 | transformId, |
Nathan Harold | da54f12 | 2018-01-09 16:42:57 -0800 | [diff] [blame] | 569 | sourceAddress, |
| 570 | destinationAddress, |
Di Lu | 2ccb3e5 | 2018-01-03 16:19:20 -0800 | [diff] [blame] | 571 | spi, |
| 572 | markValue, |
| 573 | markMask)); |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | binder::Status NetdNativeService::ipSecApplyTransportModeTransform( |
| 577 | const android::base::unique_fd& socket, |
| 578 | int32_t transformId, |
| 579 | int32_t direction, |
Nathan Harold | da54f12 | 2018-01-09 16:42:57 -0800 | [diff] [blame] | 580 | const std::string& sourceAddress, |
| 581 | const std::string& destinationAddress, |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 582 | int32_t spi) { |
| 583 | // Necessary locking done in IpSecService and kernel |
| 584 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 585 | gLog.log("ipSecApplyTransportModeTransform()"); |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 586 | return asBinderStatus(gCtls->xfrmCtrl.ipSecApplyTransportModeTransform( |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 587 | socket, |
| 588 | transformId, |
| 589 | direction, |
Nathan Harold | da54f12 | 2018-01-09 16:42:57 -0800 | [diff] [blame] | 590 | sourceAddress, |
| 591 | destinationAddress, |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 592 | spi)); |
| 593 | } |
| 594 | |
| 595 | binder::Status NetdNativeService::ipSecRemoveTransportModeTransform( |
| 596 | const android::base::unique_fd& socket) { |
| 597 | // Necessary locking done in IpSecService and kernel |
| 598 | ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 599 | gLog.log("ipSecRemoveTransportModeTransform()"); |
ludi | 6e8eccd | 2017-08-14 14:40:37 -0700 | [diff] [blame] | 600 | return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTransportModeTransform( |
Nathan Harold | 1a37153 | 2017-01-30 12:30:48 -0800 | [diff] [blame] | 601 | socket)); |
| 602 | } |
| 603 | |
Benedict Wong | ad600cb | 2018-05-14 17:22:35 -0700 | [diff] [blame^] | 604 | binder::Status NetdNativeService::ipSecAddSecurityPolicy(int32_t transformId, int32_t direction, |
| 605 | const std::string& tmplSrcAddress, |
| 606 | const std::string& tmplDstAddress, |
| 607 | int32_t spi, int32_t markValue, |
| 608 | int32_t markMask) { |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 609 | // Necessary locking done in IpSecService and kernel |
| 610 | ENFORCE_PERMISSION(NETWORK_STACK); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 611 | gLog.log("ipSecAddSecurityPolicy()"); |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 612 | return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityPolicy( |
Benedict Wong | ad600cb | 2018-05-14 17:22:35 -0700 | [diff] [blame^] | 613 | transformId, direction, tmplSrcAddress, tmplDstAddress, spi, markValue, markMask)); |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Benedict Wong | ad600cb | 2018-05-14 17:22:35 -0700 | [diff] [blame^] | 616 | binder::Status NetdNativeService::ipSecUpdateSecurityPolicy(int32_t transformId, int32_t direction, |
| 617 | const std::string& tmplSrcAddress, |
| 618 | const std::string& tmplDstAddress, |
| 619 | int32_t spi, int32_t markValue, |
| 620 | int32_t markMask) { |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 621 | // Necessary locking done in IpSecService and kernel |
| 622 | ENFORCE_PERMISSION(NETWORK_STACK); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 623 | gLog.log("ipSecAddSecurityPolicy()"); |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 624 | return asBinderStatus(gCtls->xfrmCtrl.ipSecUpdateSecurityPolicy( |
Benedict Wong | ad600cb | 2018-05-14 17:22:35 -0700 | [diff] [blame^] | 625 | transformId, direction, tmplSrcAddress, tmplDstAddress, spi, markValue, markMask)); |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 626 | } |
| 627 | |
Benedict Wong | ad600cb | 2018-05-14 17:22:35 -0700 | [diff] [blame^] | 628 | binder::Status NetdNativeService::ipSecDeleteSecurityPolicy(int32_t transformId, int32_t direction, |
| 629 | const std::string& tmplSrcAddress, |
| 630 | const std::string& tmplDstAddress, |
| 631 | int32_t markValue, int32_t markMask) { |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 632 | // Necessary locking done in IpSecService and kernel |
| 633 | ENFORCE_PERMISSION(NETWORK_STACK); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 634 | gLog.log("ipSecAddSecurityPolicy()"); |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 635 | return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityPolicy( |
Benedict Wong | ad600cb | 2018-05-14 17:22:35 -0700 | [diff] [blame^] | 636 | transformId, direction, tmplSrcAddress, tmplDstAddress, markValue, markMask)); |
Benedict Wong | 84a8dca | 2018-01-19 12:12:17 -0800 | [diff] [blame] | 637 | } |
| 638 | |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 639 | binder::Status NetdNativeService::addVirtualTunnelInterface( |
| 640 | const std::string& deviceName, |
| 641 | const std::string& localAddress, |
| 642 | const std::string& remoteAddress, |
| 643 | int32_t iKey, |
| 644 | int32_t oKey) { |
| 645 | // Necessary locking done in IpSecService and kernel |
| 646 | ENFORCE_PERMISSION(NETWORK_STACK); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 647 | gLog.log("addVirtualTunnelInterface()"); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 648 | int ret = gCtls->xfrmCtrl.addVirtualTunnelInterface( |
| 649 | deviceName, |
| 650 | localAddress, |
| 651 | remoteAddress, |
| 652 | iKey, |
| 653 | oKey, |
| 654 | false); |
| 655 | |
| 656 | return (ret == 0) ? binder::Status::ok() : |
| 657 | asBinderStatus(netdutils::statusFromErrno( |
| 658 | ret, "Error in creating virtual tunnel interface.")); |
| 659 | } |
| 660 | |
| 661 | binder::Status NetdNativeService::updateVirtualTunnelInterface( |
| 662 | const std::string& deviceName, |
| 663 | const std::string& localAddress, |
| 664 | const std::string& remoteAddress, |
| 665 | int32_t iKey, |
| 666 | int32_t oKey) { |
| 667 | // Necessary locking done in IpSecService and kernel |
| 668 | ENFORCE_PERMISSION(NETWORK_STACK); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 669 | gLog.log("updateVirtualTunnelInterface()"); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 670 | int ret = gCtls->xfrmCtrl.addVirtualTunnelInterface( |
| 671 | deviceName, |
| 672 | localAddress, |
| 673 | remoteAddress, |
| 674 | iKey, |
| 675 | oKey, |
| 676 | true); |
| 677 | |
| 678 | return (ret == 0) ? binder::Status::ok() : |
| 679 | asBinderStatus(netdutils::statusFromErrno( |
| 680 | ret, "Error in updating virtual tunnel interface.")); |
| 681 | } |
| 682 | |
| 683 | binder::Status NetdNativeService::removeVirtualTunnelInterface(const std::string& deviceName) { |
| 684 | // Necessary locking done in IpSecService and kernel |
| 685 | ENFORCE_PERMISSION(NETWORK_STACK); |
Erik Kline | b31fd69 | 2018-06-06 20:50:11 +0900 | [diff] [blame] | 686 | gLog.log("removeVirtualTunnelInterface()"); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 687 | int ret = gCtls->xfrmCtrl.removeVirtualTunnelInterface(deviceName); |
| 688 | |
| 689 | return (ret == 0) ? binder::Status::ok() : |
| 690 | asBinderStatus(netdutils::statusFromErrno( |
| 691 | ret, "Error in removing virtual tunnel interface.")); |
| 692 | } |
| 693 | |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 694 | binder::Status NetdNativeService::setIPv6AddrGenMode(const std::string& ifName, |
| 695 | int32_t mode) { |
| 696 | ENFORCE_PERMISSION(NETWORK_STACK); |
Nathan Harold | 28ccfef | 2018-03-16 19:02:47 -0700 | [diff] [blame] | 697 | return asBinderStatus(InterfaceController::setIPv6AddrGenMode(ifName, mode)); |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 698 | } |
| 699 | |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 700 | binder::Status NetdNativeService::wakeupAddInterface(const std::string& ifName, |
| 701 | const std::string& prefix, int32_t mark, |
| 702 | int32_t mask) { |
| 703 | ENFORCE_PERMISSION(NETWORK_STACK); |
Nathan Harold | 28ccfef | 2018-03-16 19:02:47 -0700 | [diff] [blame] | 704 | return asBinderStatus(gCtls->wakeupCtrl.addInterface(ifName, prefix, mark, mask)); |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | binder::Status NetdNativeService::wakeupDelInterface(const std::string& ifName, |
| 708 | const std::string& prefix, int32_t mark, |
| 709 | int32_t mask) { |
| 710 | ENFORCE_PERMISSION(NETWORK_STACK); |
Nathan Harold | 28ccfef | 2018-03-16 19:02:47 -0700 | [diff] [blame] | 711 | return asBinderStatus(gCtls->wakeupCtrl.delInterface(ifName, prefix, mark, mask)); |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 712 | } |
| 713 | |
Chenbo Feng | 07d43fe | 2017-12-21 14:38:51 -0800 | [diff] [blame] | 714 | binder::Status NetdNativeService::trafficCheckBpfStatsEnable(bool* ret) { |
| 715 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 716 | *ret = gCtls->trafficCtrl.checkBpfStatsEnable(); |
| 717 | return binder::Status::ok(); |
| 718 | } |
| 719 | |
Luke Huang | 0051a62 | 2018-07-23 20:30:16 +0800 | [diff] [blame] | 720 | binder::Status NetdNativeService::idletimerAddInterface(const std::string& ifName, int32_t timeout, |
| 721 | const std::string& classLabel) { |
| 722 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->idletimerCtrl.lock); |
| 723 | auto entry = gLog.newEntry() |
| 724 | .prettyFunction(__PRETTY_FUNCTION__) |
| 725 | .arg(ifName) |
| 726 | .arg(timeout) |
| 727 | .arg(classLabel); |
| 728 | int res = |
| 729 | gCtls->idletimerCtrl.addInterfaceIdletimer(ifName.c_str(), timeout, classLabel.c_str()); |
| 730 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 731 | return statusFromErrcode(res); |
| 732 | } |
| 733 | |
| 734 | binder::Status NetdNativeService::idletimerRemoveInterface(const std::string& ifName, |
| 735 | int32_t timeout, |
| 736 | const std::string& classLabel) { |
| 737 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->idletimerCtrl.lock); |
| 738 | auto entry = gLog.newEntry() |
| 739 | .prettyFunction(__PRETTY_FUNCTION__) |
| 740 | .arg(ifName) |
| 741 | .arg(timeout) |
| 742 | .arg(classLabel); |
| 743 | int res = gCtls->idletimerCtrl.removeInterfaceIdletimer(ifName.c_str(), timeout, |
| 744 | classLabel.c_str()); |
| 745 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 746 | return statusFromErrcode(res); |
| 747 | } |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 748 | |
| 749 | binder::Status NetdNativeService::strictUidCleartextPenalty(int32_t uid, int32_t policyPenalty) { |
| 750 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->strictCtrl.lock); |
| 751 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid).arg(policyPenalty); |
| 752 | StrictPenalty penalty; |
| 753 | switch (policyPenalty) { |
| 754 | case INetd::PENALTY_POLICY_REJECT: |
| 755 | penalty = REJECT; |
| 756 | break; |
| 757 | case INetd::PENALTY_POLICY_LOG: |
| 758 | penalty = LOG; |
| 759 | break; |
| 760 | case INetd::PENALTY_POLICY_ACCEPT: |
| 761 | penalty = ACCEPT; |
| 762 | break; |
| 763 | default: |
| 764 | return statusFromErrcode(-EINVAL); |
| 765 | break; |
| 766 | } |
| 767 | int res = gCtls->strictCtrl.setUidCleartextPenalty((uid_t) uid, penalty); |
| 768 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 769 | return statusFromErrcode(res); |
| 770 | } |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 771 | binder::Status NetdNativeService::clatdStart(const std::string& ifName) { |
| 772 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->clatdCtrl.mutex); |
| 773 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName); |
| 774 | int res = gCtls->clatdCtrl.startClatd(ifName.c_str()); |
| 775 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 776 | return statusFromErrcode(res); |
| 777 | } |
| 778 | |
| 779 | binder::Status NetdNativeService::clatdStop(const std::string& ifName) { |
| 780 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->clatdCtrl.mutex); |
| 781 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName); |
| 782 | int res = gCtls->clatdCtrl.stopClatd(ifName.c_str()); |
| 783 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 784 | return statusFromErrcode(res); |
| 785 | } |
Luke Huang | a67dd56 | 2018-07-17 19:58:25 +0800 | [diff] [blame] | 786 | |
Luke Huang | 457d470 | 2018-08-16 15:39:15 +0800 | [diff] [blame] | 787 | binder::Status NetdNativeService::ipfwdEnabled(bool* status) { |
| 788 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
| 789 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__); |
| 790 | *status = (gCtls->tetherCtrl.forwardingRequestCount() > 0) ? true : false; |
| 791 | gLog.log(entry.returns(*status).withAutomaticDuration()); |
| 792 | return binder::Status::ok(); |
| 793 | } |
| 794 | |
| 795 | binder::Status NetdNativeService::ipfwdEnableForwarding(const std::string& requester) { |
| 796 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
| 797 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(requester); |
| 798 | int res = (gCtls->tetherCtrl.enableForwarding(requester.c_str())) ? 0 : -EREMOTEIO; |
| 799 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 800 | return statusFromErrcode(res); |
| 801 | } |
| 802 | |
| 803 | binder::Status NetdNativeService::ipfwdDisableForwarding(const std::string& requester) { |
| 804 | NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock); |
| 805 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(requester); |
| 806 | int res = (gCtls->tetherCtrl.disableForwarding(requester.c_str())) ? 0 : -EREMOTEIO; |
| 807 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 808 | return statusFromErrcode(res); |
| 809 | } |
| 810 | |
| 811 | binder::Status NetdNativeService::ipfwdAddInterfaceForward(const std::string& fromIface, |
| 812 | const std::string& toIface) { |
| 813 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 814 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(fromIface).arg(toIface); |
| 815 | int res = RouteController::enableTethering(fromIface.c_str(), toIface.c_str()); |
| 816 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 817 | return statusFromErrcode(res); |
| 818 | } |
| 819 | |
| 820 | binder::Status NetdNativeService::ipfwdRemoveInterfaceForward(const std::string& fromIface, |
| 821 | const std::string& toIface) { |
| 822 | ENFORCE_PERMISSION(NETWORK_STACK); |
| 823 | auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(fromIface).arg(toIface); |
| 824 | int res = RouteController::disableTethering(fromIface.c_str(), toIface.c_str()); |
| 825 | gLog.log(entry.returns(res).withAutomaticDuration()); |
| 826 | return statusFromErrcode(res); |
| 827 | } |
| 828 | |
Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 829 | } // namespace net |
| 830 | } // namespace android |