blob: d5e58006a1aed899ebbfe64d4b0ce8f338115bfe [file] [log] [blame]
Lorenzo Colittie4d626e2016-02-02 17:19:04 +09001/**
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 Schwartz4204ecf2017-10-02 12:35:48 -040019#include <set>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090020#include <vector>
21
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090022#include <android-base/stringprintf.h>
Ben Schwartz4204ecf2017-10-02 12:35:48 -040023#include <android-base/strings.h>
Robin Lee2cf56172016-09-13 18:55:42 +090024#include <cutils/properties.h>
Logan Chien3f461482018-04-23 14:31:32 +080025#include <log/log.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090026#include <utils/Errors.h>
Pierre Imaibeedec32016-04-13 06:44:51 +090027#include <utils/String16.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090028
29#include <binder/IPCThreadState.h>
30#include <binder/IServiceManager.h>
31#include "android/net/BnNetd.h"
32
Ben Schwartze7601812017-04-28 16:38:29 -040033#include <openssl/base64.h>
34
Lorenzo Colitti89faa342016-02-26 11:38:47 +090035#include "Controllers.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090036#include "DumpWriter.h"
Michal Karpinskid5440112016-10-06 16:56:04 +010037#include "EventReporter.h"
Erik Kline55b06f82016-07-04 09:57:18 +090038#include "InterfaceController.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090039#include "NetdConstants.h"
40#include "NetdNativeService.h"
Erik Kline85890042018-05-25 19:19:11 +090041#include "Process.h"
Robin Leeb8087362016-03-30 18:43:08 +010042#include "RouteController.h"
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090043#include "SockDiag.h"
Robin Leeb8087362016-03-30 18:43:08 +010044#include "UidRanges.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090045
46using android::base::StringPrintf;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +090047using android::os::PersistableBundle;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090048
49namespace android {
50namespace net {
51
52namespace {
53
54const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL";
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090055const char NETWORK_STACK[] = "android.permission.NETWORK_STACK";
Erik Kline2d3a1632016-03-15 16:33:48 +090056const char DUMP[] = "android.permission.DUMP";
Erik Klineb31fd692018-06-06 20:50:11 +090057const char OPT_SHORT[] = "--short";
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090058
59binder::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 Lee2cf56172016-09-13 18:55:42 +090071#define ENFORCE_DEBUGGABLE() { \
72 char value[PROPERTY_VALUE_MAX + 1]; \
Yi Kongbdfd57e2018-07-25 13:26:10 -070073 if (property_get("ro.debuggable", value, nullptr) != 1 \
Robin Lee2cf56172016-09-13 18:55:42 +090074 || 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 Colittie4d626e2016-02-02 17:19:04 +090082#define ENFORCE_PERMISSION(permission) { \
83 binder::Status status = checkPermission((permission)); \
84 if (!status.isOk()) { \
85 return status; \
86 } \
87}
88
Lorenzo Colitti89faa342016-02-26 11:38:47 +090089#define NETD_LOCKING_RPC(permission, lock) \
90 ENFORCE_PERMISSION(permission); \
Luke Huangd1ee4622018-06-29 13:49:58 +080091 std::lock_guard<std::mutex> _lock(lock);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090092
93#define NETD_BIG_LOCK_RPC(permission) NETD_LOCKING_RPC((permission), gBigNetdLock)
Lorenzo Colittid33e96d2016-12-15 23:59:01 +090094
95inline 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 Klineb31fd692018-06-06 20:50:11 +0900102bool contains(const Vector<String16>& words, const String16& word) {
103 for (const auto& w : words) {
104 if (w == word) return true;
105 }
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900106
Erik Klineb31fd692018-06-06 20:50:11 +0900107 return false;
108}
109
110} // namespace
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900111
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900112status_t NetdNativeService::start() {
113 IPCThreadState::self()->disableBackgroundScheduling(true);
Erik Klineb31fd692018-06-06 20:50:11 +0900114 const status_t ret = BinderService<NetdNativeService>::publish();
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900115 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 Benichi7b314e12018-01-15 21:54:00 +0900124status_t NetdNativeService::dump(int fd, const Vector<String16> &args) {
Erik Kline2d3a1632016-03-15 16:33:48 +0900125 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 Benichi7b314e12018-01-15 21:54:00 +0900134
Erik Kline2d3a1632016-03-15 16:33:48 +0900135 DumpWriter dw(fd);
Hugo Benichi7b314e12018-01-15 21:54:00 +0900136
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 Fengef297172018-03-26 10:53:33 -0700144 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 Kline85890042018-05-25 19:19:11 +0900151 process::dump(dw);
Erik Kline2d3a1632016-03-15 16:33:48 +0900152 dw.blankline();
153 gCtls->netCtrl.dump(dw);
154 dw.blankline();
155
Chenbo Fengef297172018-03-26 10:53:33 -0700156 gCtls->trafficCtrl.dump(dw, false);
157 dw.blankline();
158
Erik Klineb31fd692018-06-06 20:50:11 +0900159 {
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 Kline2d3a1632016-03-15 16:33:48 +0900171 return NO_ERROR;
172}
173
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900174binder::Status NetdNativeService::isAlive(bool *alive) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900175 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900176 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900177
178 *alive = true;
Erik Klineb31fd692018-06-06 20:50:11 +0900179
180 gLog.log(entry.returns(*alive));
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900181 return binder::Status::ok();
182}
183
Erik Klinef52d4522018-03-14 15:01:46 +0900184binder::Status NetdNativeService::firewallReplaceUidChain(const std::string& chainName,
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900185 bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) {
186 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->firewallCtrl.lock);
Erik Klineb31fd692018-06-06 20:50:11 +0900187 auto entry = gLog.newEntry()
188 .prettyFunction(__PRETTY_FUNCTION__)
189 .arg(chainName)
190 .arg(isWhitelist)
191 .arg(uids);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900192
Erik Klinef52d4522018-03-14 15:01:46 +0900193 int err = gCtls->firewallCtrl.replaceUidChain(chainName, isWhitelist, uids);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900194 *ret = (err == 0);
Erik Klineb31fd692018-06-06 20:50:11 +0900195
196 gLog.log(entry.returns(*ret).withAutomaticDuration());
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900197 return binder::Status::ok();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900198}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900199
200binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
201 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->bandwidthCtrl.lock);
Erik Klineb31fd692018-06-06 20:50:11 +0900202 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(enable);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900203
204 int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
205 *ret = (err == 0);
Erik Klineb31fd692018-06-06 20:50:11 +0900206 gLog.log(entry.returns(*ret).withAutomaticDuration());
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900207 return binder::Status::ok();
208}
209
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900210binder::Status NetdNativeService::networkCreatePhysical(int32_t netId,
211 const std::string& permission) {
212 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900213 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId).arg(permission);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900214 int ret = gCtls->netCtrl.createPhysicalNetwork(netId, stringToPermission(permission.c_str()));
Erik Klineb31fd692018-06-06 20:50:11 +0900215 gLog.log(entry.returns(ret).withAutomaticDuration());
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900216 return statusFromErrcode(ret);
217}
218
219binder::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
225binder::Status NetdNativeService::networkDestroy(int32_t netId) {
Erik Klinec8b6a9c2018-01-15 17:06:48 +0900226 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 Colittid33e96d2016-12-15 23:59:01 +0900230 return statusFromErrcode(ret);
231}
232
233binder::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
239binder::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
245binder::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
253binder::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 Leeb8087362016-03-30 18:43:08 +0100261binder::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 Colitti563d98b2016-04-24 13:13:14 +0900270 UidRanges uidRanges(uidRangeArray);
Robin Leeb8087362016-03-30 18:43:08 +0100271
272 int err;
273 if (add) {
274 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
275 } else {
276 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
277 }
278
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900279 return statusFromErrcode(err);
Robin Leeb8087362016-03-30 18:43:08 +0100280}
281
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900282binder::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 Colittie5c3c992016-07-26 17:53:50 +0900294 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()),
295 true /* excludeLoopback */);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900296
297 if (err) {
298 return binder::Status::fromServiceSpecificError(-err,
299 String8::format("destroySockets: %s", strerror(-err)));
300 }
Pierre Imaibeedec32016-04-13 06:44:51 +0900301 return binder::Status::ok();
302}
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900303
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400304// Parse a base64 encoded string into a vector of bytes.
305// On failure, return an empty vector.
306static 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 Imaibeedec32016-04-13 06:44:51 +0900327binder::Status NetdNativeService::setResolverConfiguration(int32_t netId,
328 const std::vector<std::string>& servers, const std::vector<std::string>& domains,
Erik Klinea1476fb2018-03-04 21:01:56 +0900329 const std::vector<int32_t>& params, const std::string& tlsName,
330 const std::vector<std::string>& tlsServers,
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400331 const std::vector<std::string>& tlsFingerprints) {
Pierre Imaibeedec32016-04-13 06:44:51 +0900332 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
333 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900334 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 Imaibeedec32016-04-13 06:44:51 +0900343
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400344 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 Klinea1476fb2018-03-04 21:01:56 +0900355 tlsName, tlsServers, decoded_fingerprints);
Erik Klineb31fd692018-06-06 20:50:11 +0900356 gLog.log(entry.returns(err).withAutomaticDuration());
Pierre Imaibeedec32016-04-13 06:44:51 +0900357 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
364binder::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 Colitti563d98b2016-04-24 13:13:14 +0900375 return binder::Status::ok();
376}
377
Erik Klinef48e4dd2016-07-18 04:02:07 +0900378binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800379 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
Erik Klinef48e4dd2016-07-18 04:02:07 +0900380
381 *ret = gCtls->tetherCtrl.applyDnsInterfaces();
382 return binder::Status::ok();
383}
384
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900385namespace {
386
387void 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 Colitti9a65ac62017-09-04 18:07:56 +0900396 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 Colitti9a8a9ff2017-01-31 19:06:59 +0900400
401 bundle->putLongVector(iface, statsVector);
402}
403
404} // namespace
405
406binder::Status NetdNativeService::tetherGetStats(PersistableBundle *bundle) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800407 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900408
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900409 const auto& statsList = gCtls->tetherCtrl.getTetherStats();
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900410 if (!isOk(statsList)) {
Nathan Harold28ccfef2018-03-16 19:02:47 -0700411 return asBinderStatus(statsList);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900412 }
413
414 for (const auto& stats : statsList.value()) {
415 tetherAddStats(bundle, stats);
416 }
417
418 return binder::Status::ok();
419}
420
Erik Kline53c20882016-08-02 15:22:53 +0900421binder::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
434binder::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 Kline55b06f82016-07-04 09:57:18 +0900447binder::Status NetdNativeService::setProcSysNet(
448 int32_t family, int32_t which, const std::string &ifname, const std::string &parameter,
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 Lee2cf56172016-09-13 18:55:42 +0900486binder::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 Karpinskid5440112016-10-06 16:56:04 +0100492 *reportingLevel = gCtls->eventReporter.getMetricsReportingLevel();
Robin Lee2cf56172016-09-13 18:55:42 +0900493 return binder::Status::ok();
494}
495
496binder::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 Karpinskid5440112016-10-06 16:56:04 +0100502 return (gCtls->eventReporter.setMetricsReportingLevel(reportingLevel) == 0)
503 ? binder::Status::ok()
504 : binder::Status::fromExceptionCode(binder::Status::EX_ILLEGAL_ARGUMENT);
Robin Lee2cf56172016-09-13 18:55:42 +0900505}
506
Benedict Wongb2daefb2017-12-06 22:05:46 -0800507binder::Status NetdNativeService::ipSecSetEncapSocketOwner(const android::base::unique_fd& socket,
508 int newUid) {
509 ENFORCE_PERMISSION(NETWORK_STACK)
Erik Klineb31fd692018-06-06 20:50:11 +0900510 gLog.log("ipSecSetEncapSocketOwner()");
Benedict Wongb2daefb2017-12-06 22:05:46 -0800511
512 uid_t callerUid = IPCThreadState::self()->getCallingUid();
513 return asBinderStatus(gCtls->xfrmCtrl.ipSecSetEncapSocketOwner(socket, newUid, callerUid));
514}
515
Nathan Harold1a371532017-01-30 12:30:48 -0800516binder::Status NetdNativeService::ipSecAllocateSpi(
517 int32_t transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800518 const std::string& sourceAddress,
519 const std::string& destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800520 int32_t inSpi,
521 int32_t* outSpi) {
522 // Necessary locking done in IpSecService and kernel
523 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900524 gLog.log("ipSecAllocateSpi()");
ludi6e8eccd2017-08-14 14:40:37 -0700525 return asBinderStatus(gCtls->xfrmCtrl.ipSecAllocateSpi(
Nathan Harold1a371532017-01-30 12:30:48 -0800526 transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800527 sourceAddress,
528 destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800529 inSpi,
530 outSpi));
531}
532
533binder::Status NetdNativeService::ipSecAddSecurityAssociation(
534 int32_t transformId,
535 int32_t mode,
Nathan Haroldda54f122018-01-09 16:42:57 -0800536 const std::string& sourceAddress,
537 const std::string& destinationAddress,
Benedict Wong96abf482018-01-22 13:56:41 -0800538 int32_t underlyingNetId,
Nathan Harold1a371532017-01-30 12:30:48 -0800539 int32_t spi,
Di Lu2ccb3e52018-01-03 16:19:20 -0800540 int32_t markValue,
541 int32_t markMask,
Nathan Harold1a371532017-01-30 12:30:48 -0800542 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 Wongbe65b432017-08-22 21:43:14 -0700544 const std::string& aeadAlgo, const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits,
Nathan Harold1a371532017-01-30 12:30:48 -0800545 int32_t encapType,
546 int32_t encapLocalPort,
ludiec836052017-05-20 14:17:05 -0700547 int32_t encapRemotePort) {
Nathan Harold1a371532017-01-30 12:30:48 -0800548 // Necessary locking done in IpSecService and kernel
549 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900550 gLog.log("ipSecAddSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700551 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityAssociation(
Nathan Haroldda54f122018-01-09 16:42:57 -0800552 transformId, mode, sourceAddress, destinationAddress,
Benedict Wong96abf482018-01-22 13:56:41 -0800553 underlyingNetId,
Di Lu2ccb3e52018-01-03 16:19:20 -0800554 spi, markValue, markMask,
Nathan Harold1a371532017-01-30 12:30:48 -0800555 authAlgo, authKey, authTruncBits,
556 cryptAlgo, cryptKey, cryptTruncBits,
Benedict Wongbe65b432017-08-22 21:43:14 -0700557 aeadAlgo, aeadKey, aeadIcvBits,
ludiec836052017-05-20 14:17:05 -0700558 encapType, encapLocalPort, encapRemotePort));
Nathan Harold1a371532017-01-30 12:30:48 -0800559}
560
561binder::Status NetdNativeService::ipSecDeleteSecurityAssociation(
562 int32_t transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800563 const std::string& sourceAddress,
564 const std::string& destinationAddress,
Di Lu2ccb3e52018-01-03 16:19:20 -0800565 int32_t spi,
566 int32_t markValue,
567 int32_t markMask) {
Nathan Harold1a371532017-01-30 12:30:48 -0800568 // Necessary locking done in IpSecService and kernel
569 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900570 gLog.log("ipSecDeleteSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700571 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityAssociation(
Nathan Harold1a371532017-01-30 12:30:48 -0800572 transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800573 sourceAddress,
574 destinationAddress,
Di Lu2ccb3e52018-01-03 16:19:20 -0800575 spi,
576 markValue,
577 markMask));
Nathan Harold1a371532017-01-30 12:30:48 -0800578}
579
580binder::Status NetdNativeService::ipSecApplyTransportModeTransform(
581 const android::base::unique_fd& socket,
582 int32_t transformId,
583 int32_t direction,
Nathan Haroldda54f122018-01-09 16:42:57 -0800584 const std::string& sourceAddress,
585 const std::string& destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800586 int32_t spi) {
587 // Necessary locking done in IpSecService and kernel
588 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900589 gLog.log("ipSecApplyTransportModeTransform()");
ludi6e8eccd2017-08-14 14:40:37 -0700590 return asBinderStatus(gCtls->xfrmCtrl.ipSecApplyTransportModeTransform(
Nathan Harold1a371532017-01-30 12:30:48 -0800591 socket,
592 transformId,
593 direction,
Nathan Haroldda54f122018-01-09 16:42:57 -0800594 sourceAddress,
595 destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800596 spi));
597}
598
599binder::Status NetdNativeService::ipSecRemoveTransportModeTransform(
600 const android::base::unique_fd& socket) {
601 // Necessary locking done in IpSecService and kernel
602 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900603 gLog.log("ipSecRemoveTransportModeTransform()");
ludi6e8eccd2017-08-14 14:40:37 -0700604 return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTransportModeTransform(
Nathan Harold1a371532017-01-30 12:30:48 -0800605 socket));
606}
607
Benedict Wong84a8dca2018-01-19 12:12:17 -0800608binder::Status NetdNativeService::ipSecAddSecurityPolicy(
609 int32_t transformId,
610 int32_t direction,
611 const std::string& sourceAddress,
612 const std::string& destinationAddress,
613 int32_t spi,
614 int32_t markValue,
615 int32_t markMask){
616 // Necessary locking done in IpSecService and kernel
617 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900618 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800619 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityPolicy(
620 transformId,
621 direction,
622 sourceAddress,
623 destinationAddress,
624 spi,
625 markValue,
626 markMask));
627}
628
629binder::Status NetdNativeService::ipSecUpdateSecurityPolicy(
630 int32_t transformId,
631 int32_t direction,
632 const std::string& sourceAddress,
633 const std::string& destinationAddress,
634 int32_t spi,
635 int32_t markValue,
636 int32_t markMask){
637 // Necessary locking done in IpSecService and kernel
638 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900639 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800640 return asBinderStatus(gCtls->xfrmCtrl.ipSecUpdateSecurityPolicy(
641 transformId,
642 direction,
643 sourceAddress,
644 destinationAddress,
645 spi,
646 markValue,
647 markMask));
648}
649
650binder::Status NetdNativeService::ipSecDeleteSecurityPolicy(
651 int32_t transformId,
652 int32_t direction,
653 const std::string& sourceAddress,
654 const std::string& destinationAddress,
655 int32_t markValue,
656 int32_t markMask){
657 // Necessary locking done in IpSecService and kernel
658 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900659 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800660 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityPolicy(
661 transformId,
662 direction,
663 sourceAddress,
664 destinationAddress,
665 markValue,
666 markMask));
667}
668
manojboopathi8707f232018-01-02 14:45:47 -0800669binder::Status NetdNativeService::addVirtualTunnelInterface(
670 const std::string& deviceName,
671 const std::string& localAddress,
672 const std::string& remoteAddress,
673 int32_t iKey,
674 int32_t oKey) {
675 // Necessary locking done in IpSecService and kernel
676 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900677 gLog.log("addVirtualTunnelInterface()");
manojboopathi8707f232018-01-02 14:45:47 -0800678 int ret = gCtls->xfrmCtrl.addVirtualTunnelInterface(
679 deviceName,
680 localAddress,
681 remoteAddress,
682 iKey,
683 oKey,
684 false);
685
686 return (ret == 0) ? binder::Status::ok() :
687 asBinderStatus(netdutils::statusFromErrno(
688 ret, "Error in creating virtual tunnel interface."));
689}
690
691binder::Status NetdNativeService::updateVirtualTunnelInterface(
692 const std::string& deviceName,
693 const std::string& localAddress,
694 const std::string& remoteAddress,
695 int32_t iKey,
696 int32_t oKey) {
697 // Necessary locking done in IpSecService and kernel
698 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900699 gLog.log("updateVirtualTunnelInterface()");
manojboopathi8707f232018-01-02 14:45:47 -0800700 int ret = gCtls->xfrmCtrl.addVirtualTunnelInterface(
701 deviceName,
702 localAddress,
703 remoteAddress,
704 iKey,
705 oKey,
706 true);
707
708 return (ret == 0) ? binder::Status::ok() :
709 asBinderStatus(netdutils::statusFromErrno(
710 ret, "Error in updating virtual tunnel interface."));
711}
712
713binder::Status NetdNativeService::removeVirtualTunnelInterface(const std::string& deviceName) {
714 // Necessary locking done in IpSecService and kernel
715 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900716 gLog.log("removeVirtualTunnelInterface()");
manojboopathi8707f232018-01-02 14:45:47 -0800717 int ret = gCtls->xfrmCtrl.removeVirtualTunnelInterface(deviceName);
718
719 return (ret == 0) ? binder::Status::ok() :
720 asBinderStatus(netdutils::statusFromErrno(
721 ret, "Error in removing virtual tunnel interface."));
722}
723
Joel Scherpelzde937962017-06-01 13:20:21 +0900724binder::Status NetdNativeService::setIPv6AddrGenMode(const std::string& ifName,
725 int32_t mode) {
726 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700727 return asBinderStatus(InterfaceController::setIPv6AddrGenMode(ifName, mode));
Joel Scherpelzde937962017-06-01 13:20:21 +0900728}
729
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900730binder::Status NetdNativeService::wakeupAddInterface(const std::string& ifName,
731 const std::string& prefix, int32_t mark,
732 int32_t mask) {
733 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700734 return asBinderStatus(gCtls->wakeupCtrl.addInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900735}
736
737binder::Status NetdNativeService::wakeupDelInterface(const std::string& ifName,
738 const std::string& prefix, int32_t mark,
739 int32_t mask) {
740 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700741 return asBinderStatus(gCtls->wakeupCtrl.delInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900742}
743
Chenbo Feng07d43fe2017-12-21 14:38:51 -0800744binder::Status NetdNativeService::trafficCheckBpfStatsEnable(bool* ret) {
745 ENFORCE_PERMISSION(NETWORK_STACK);
746 *ret = gCtls->trafficCtrl.checkBpfStatsEnable();
747 return binder::Status::ok();
748}
749
Luke Huang0051a622018-07-23 20:30:16 +0800750binder::Status NetdNativeService::idletimerAddInterface(const std::string& ifName, int32_t timeout,
751 const std::string& classLabel) {
752 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->idletimerCtrl.lock);
753 auto entry = gLog.newEntry()
754 .prettyFunction(__PRETTY_FUNCTION__)
755 .arg(ifName)
756 .arg(timeout)
757 .arg(classLabel);
758 int res =
759 gCtls->idletimerCtrl.addInterfaceIdletimer(ifName.c_str(), timeout, classLabel.c_str());
760 gLog.log(entry.returns(res).withAutomaticDuration());
761 return statusFromErrcode(res);
762}
763
764binder::Status NetdNativeService::idletimerRemoveInterface(const std::string& ifName,
765 int32_t timeout,
766 const std::string& classLabel) {
767 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->idletimerCtrl.lock);
768 auto entry = gLog.newEntry()
769 .prettyFunction(__PRETTY_FUNCTION__)
770 .arg(ifName)
771 .arg(timeout)
772 .arg(classLabel);
773 int res = gCtls->idletimerCtrl.removeInterfaceIdletimer(ifName.c_str(), timeout,
774 classLabel.c_str());
775 gLog.log(entry.returns(res).withAutomaticDuration());
776 return statusFromErrcode(res);
777}
Luke Huanga67dd562018-07-17 19:58:25 +0800778
779binder::Status NetdNativeService::strictUidCleartextPenalty(int32_t uid, int32_t policyPenalty) {
780 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->strictCtrl.lock);
781 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid).arg(policyPenalty);
782 StrictPenalty penalty;
783 switch (policyPenalty) {
784 case INetd::PENALTY_POLICY_REJECT:
785 penalty = REJECT;
786 break;
787 case INetd::PENALTY_POLICY_LOG:
788 penalty = LOG;
789 break;
790 case INetd::PENALTY_POLICY_ACCEPT:
791 penalty = ACCEPT;
792 break;
793 default:
794 return statusFromErrcode(-EINVAL);
795 break;
796 }
797 int res = gCtls->strictCtrl.setUidCleartextPenalty((uid_t) uid, penalty);
798 gLog.log(entry.returns(res).withAutomaticDuration());
799 return statusFromErrcode(res);
800}
801
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900802} // namespace net
803} // namespace android