blob: c8e0c7e56c001a2e3d2475cf70b889f5a37de8ed [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); \
Bernie Innocentiabf8a342018-08-10 15:17:16 +090091 std::lock_guard _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(
Benedict Wongad600cb2018-05-14 17:22:35 -0700552 transformId, mode, sourceAddress, destinationAddress, underlyingNetId, spi, markValue,
553 markMask, authAlgo, authKey, authTruncBits, cryptAlgo, cryptKey, cryptTruncBits,
554 aeadAlgo, aeadKey, aeadIcvBits, encapType, encapLocalPort, encapRemotePort));
Nathan Harold1a371532017-01-30 12:30:48 -0800555}
556
557binder::Status NetdNativeService::ipSecDeleteSecurityAssociation(
558 int32_t transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800559 const std::string& sourceAddress,
560 const std::string& destinationAddress,
Di Lu2ccb3e52018-01-03 16:19:20 -0800561 int32_t spi,
562 int32_t markValue,
563 int32_t markMask) {
Nathan Harold1a371532017-01-30 12:30:48 -0800564 // Necessary locking done in IpSecService and kernel
565 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900566 gLog.log("ipSecDeleteSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700567 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityAssociation(
Nathan Harold1a371532017-01-30 12:30:48 -0800568 transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800569 sourceAddress,
570 destinationAddress,
Di Lu2ccb3e52018-01-03 16:19:20 -0800571 spi,
572 markValue,
573 markMask));
Nathan Harold1a371532017-01-30 12:30:48 -0800574}
575
576binder::Status NetdNativeService::ipSecApplyTransportModeTransform(
577 const android::base::unique_fd& socket,
578 int32_t transformId,
579 int32_t direction,
Nathan Haroldda54f122018-01-09 16:42:57 -0800580 const std::string& sourceAddress,
581 const std::string& destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800582 int32_t spi) {
583 // Necessary locking done in IpSecService and kernel
584 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900585 gLog.log("ipSecApplyTransportModeTransform()");
ludi6e8eccd2017-08-14 14:40:37 -0700586 return asBinderStatus(gCtls->xfrmCtrl.ipSecApplyTransportModeTransform(
Nathan Harold1a371532017-01-30 12:30:48 -0800587 socket,
588 transformId,
589 direction,
Nathan Haroldda54f122018-01-09 16:42:57 -0800590 sourceAddress,
591 destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800592 spi));
593}
594
595binder::Status NetdNativeService::ipSecRemoveTransportModeTransform(
596 const android::base::unique_fd& socket) {
597 // Necessary locking done in IpSecService and kernel
598 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900599 gLog.log("ipSecRemoveTransportModeTransform()");
ludi6e8eccd2017-08-14 14:40:37 -0700600 return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTransportModeTransform(
Nathan Harold1a371532017-01-30 12:30:48 -0800601 socket));
602}
603
Benedict Wongad600cb2018-05-14 17:22:35 -0700604binder::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 Wong84a8dca2018-01-19 12:12:17 -0800609 // Necessary locking done in IpSecService and kernel
610 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900611 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800612 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityPolicy(
Benedict Wongad600cb2018-05-14 17:22:35 -0700613 transformId, direction, tmplSrcAddress, tmplDstAddress, spi, markValue, markMask));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800614}
615
Benedict Wongad600cb2018-05-14 17:22:35 -0700616binder::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 Wong84a8dca2018-01-19 12:12:17 -0800621 // Necessary locking done in IpSecService and kernel
622 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900623 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800624 return asBinderStatus(gCtls->xfrmCtrl.ipSecUpdateSecurityPolicy(
Benedict Wongad600cb2018-05-14 17:22:35 -0700625 transformId, direction, tmplSrcAddress, tmplDstAddress, spi, markValue, markMask));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800626}
627
Benedict Wongad600cb2018-05-14 17:22:35 -0700628binder::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 Wong84a8dca2018-01-19 12:12:17 -0800632 // Necessary locking done in IpSecService and kernel
633 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900634 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800635 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityPolicy(
Benedict Wongad600cb2018-05-14 17:22:35 -0700636 transformId, direction, tmplSrcAddress, tmplDstAddress, markValue, markMask));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800637}
638
manojboopathi8707f232018-01-02 14:45:47 -0800639binder::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 Klineb31fd692018-06-06 20:50:11 +0900647 gLog.log("addVirtualTunnelInterface()");
manojboopathi8707f232018-01-02 14:45:47 -0800648 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
661binder::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 Klineb31fd692018-06-06 20:50:11 +0900669 gLog.log("updateVirtualTunnelInterface()");
manojboopathi8707f232018-01-02 14:45:47 -0800670 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
683binder::Status NetdNativeService::removeVirtualTunnelInterface(const std::string& deviceName) {
684 // Necessary locking done in IpSecService and kernel
685 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900686 gLog.log("removeVirtualTunnelInterface()");
manojboopathi8707f232018-01-02 14:45:47 -0800687 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 Scherpelzde937962017-06-01 13:20:21 +0900694binder::Status NetdNativeService::setIPv6AddrGenMode(const std::string& ifName,
695 int32_t mode) {
696 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700697 return asBinderStatus(InterfaceController::setIPv6AddrGenMode(ifName, mode));
Joel Scherpelzde937962017-06-01 13:20:21 +0900698}
699
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900700binder::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 Harold28ccfef2018-03-16 19:02:47 -0700704 return asBinderStatus(gCtls->wakeupCtrl.addInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900705}
706
707binder::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 Harold28ccfef2018-03-16 19:02:47 -0700711 return asBinderStatus(gCtls->wakeupCtrl.delInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900712}
713
Chenbo Feng07d43fe2017-12-21 14:38:51 -0800714binder::Status NetdNativeService::trafficCheckBpfStatsEnable(bool* ret) {
715 ENFORCE_PERMISSION(NETWORK_STACK);
716 *ret = gCtls->trafficCtrl.checkBpfStatsEnable();
717 return binder::Status::ok();
718}
719
Luke Huang0051a622018-07-23 20:30:16 +0800720binder::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
734binder::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 Huanga67dd562018-07-17 19:58:25 +0800748
749binder::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 Huang6d301232018-08-01 14:05:18 +0800771binder::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
779binder::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 Huanga67dd562018-07-17 19:58:25 +0800786
Luke Huang457d4702018-08-16 15:39:15 +0800787binder::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
795binder::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
803binder::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
811binder::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
820binder::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 Colittie4d626e2016-02-02 17:19:04 +0900829} // namespace net
830} // namespace android