blob: 0563af412b27f25915c5cc8836d18d9c5252413e [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>
Erik Kline38e51f12018-09-06 20:14:44 +090020#include <tuple>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090021#include <vector>
22
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090023#include <android-base/stringprintf.h>
Ben Schwartz4204ecf2017-10-02 12:35:48 -040024#include <android-base/strings.h>
Robin Lee2cf56172016-09-13 18:55:42 +090025#include <cutils/properties.h>
Logan Chien3f461482018-04-23 14:31:32 +080026#include <log/log.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090027#include <utils/Errors.h>
Pierre Imaibeedec32016-04-13 06:44:51 +090028#include <utils/String16.h>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090029
30#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
32#include "android/net/BnNetd.h"
33
Ben Schwartze7601812017-04-28 16:38:29 -040034#include <openssl/base64.h>
35
Lorenzo Colitti89faa342016-02-26 11:38:47 +090036#include "Controllers.h"
Erik Kline2d3a1632016-03-15 16:33:48 +090037#include "DumpWriter.h"
Michal Karpinskid5440112016-10-06 16:56:04 +010038#include "EventReporter.h"
Erik Kline55b06f82016-07-04 09:57:18 +090039#include "InterfaceController.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090040#include "NetdConstants.h"
41#include "NetdNativeService.h"
Erik Kline85890042018-05-25 19:19:11 +090042#include "Process.h"
Robin Leeb8087362016-03-30 18:43:08 +010043#include "RouteController.h"
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090044#include "SockDiag.h"
Robin Leeb8087362016-03-30 18:43:08 +010045#include "UidRanges.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090046
47using android::base::StringPrintf;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +090048using android::os::PersistableBundle;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090049
50namespace android {
51namespace net {
52
53namespace {
54
55const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL";
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090056const char NETWORK_STACK[] = "android.permission.NETWORK_STACK";
Erik Kline2d3a1632016-03-15 16:33:48 +090057const char DUMP[] = "android.permission.DUMP";
Erik Klineb31fd692018-06-06 20:50:11 +090058const char OPT_SHORT[] = "--short";
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090059
60binder::Status checkPermission(const char *permission) {
61 pid_t pid;
62 uid_t uid;
63
64 if (checkCallingPermission(String16(permission), (int32_t *) &pid, (int32_t *) &uid)) {
65 return binder::Status::ok();
66 } else {
67 auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission);
68 return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str()));
69 }
70}
71
Robin Lee2cf56172016-09-13 18:55:42 +090072#define ENFORCE_DEBUGGABLE() { \
73 char value[PROPERTY_VALUE_MAX + 1]; \
Yi Kongbdfd57e2018-07-25 13:26:10 -070074 if (property_get("ro.debuggable", value, nullptr) != 1 \
Robin Lee2cf56172016-09-13 18:55:42 +090075 || value[0] != '1') { \
76 return binder::Status::fromExceptionCode( \
77 binder::Status::EX_SECURITY, \
78 String8("Not available in production builds.") \
79 ); \
80 } \
81}
82
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090083#define ENFORCE_PERMISSION(permission) { \
84 binder::Status status = checkPermission((permission)); \
85 if (!status.isOk()) { \
86 return status; \
87 } \
88}
89
Lorenzo Colitti89faa342016-02-26 11:38:47 +090090#define NETD_LOCKING_RPC(permission, lock) \
91 ENFORCE_PERMISSION(permission); \
Bernie Innocentiabf8a342018-08-10 15:17:16 +090092 std::lock_guard _lock(lock);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090093
94#define NETD_BIG_LOCK_RPC(permission) NETD_LOCKING_RPC((permission), gBigNetdLock)
Lorenzo Colittid33e96d2016-12-15 23:59:01 +090095
96inline binder::Status statusFromErrcode(int ret) {
97 if (ret) {
98 return binder::Status::fromServiceSpecificError(-ret, strerror(-ret));
99 }
100 return binder::Status::ok();
101}
102
Erik Klineb31fd692018-06-06 20:50:11 +0900103bool contains(const Vector<String16>& words, const String16& word) {
104 for (const auto& w : words) {
105 if (w == word) return true;
106 }
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900107
Erik Klineb31fd692018-06-06 20:50:11 +0900108 return false;
109}
110
111} // namespace
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900112
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900113status_t NetdNativeService::start() {
114 IPCThreadState::self()->disableBackgroundScheduling(true);
Erik Klineb31fd692018-06-06 20:50:11 +0900115 const status_t ret = BinderService<NetdNativeService>::publish();
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900116 if (ret != android::OK) {
117 return ret;
118 }
119 sp<ProcessState> ps(ProcessState::self());
120 ps->startThreadPool();
121 ps->giveThreadPoolName();
122 return android::OK;
123}
124
Hugo Benichi7b314e12018-01-15 21:54:00 +0900125status_t NetdNativeService::dump(int fd, const Vector<String16> &args) {
Erik Kline2d3a1632016-03-15 16:33:48 +0900126 const binder::Status dump_permission = checkPermission(DUMP);
127 if (!dump_permission.isOk()) {
128 const String8 msg(dump_permission.toString8());
129 write(fd, msg.string(), msg.size());
130 return PERMISSION_DENIED;
131 }
132
133 // This method does not grab any locks. If individual classes need locking
134 // their dump() methods MUST handle locking appropriately.
Hugo Benichi7b314e12018-01-15 21:54:00 +0900135
Erik Kline2d3a1632016-03-15 16:33:48 +0900136 DumpWriter dw(fd);
Hugo Benichi7b314e12018-01-15 21:54:00 +0900137
138 if (!args.isEmpty() && args[0] == TcpSocketMonitor::DUMP_KEYWORD) {
139 dw.blankline();
140 gCtls->tcpSocketMonitor.dump(dw);
141 dw.blankline();
142 return NO_ERROR;
143 }
144
Chenbo Fengef297172018-03-26 10:53:33 -0700145 if (!args.isEmpty() && args[0] == TrafficController::DUMP_KEYWORD) {
146 dw.blankline();
147 gCtls->trafficCtrl.dump(dw, true);
148 dw.blankline();
149 return NO_ERROR;
150 }
151
Erik Kline85890042018-05-25 19:19:11 +0900152 process::dump(dw);
Erik Kline2d3a1632016-03-15 16:33:48 +0900153 dw.blankline();
154 gCtls->netCtrl.dump(dw);
155 dw.blankline();
156
Chenbo Fengef297172018-03-26 10:53:33 -0700157 gCtls->trafficCtrl.dump(dw, false);
158 dw.blankline();
159
Erik Klineb31fd692018-06-06 20:50:11 +0900160 {
161 ScopedIndent indentLog(dw);
162 if (contains(args, String16(OPT_SHORT))) {
163 dw.println("Log: <omitted>");
164 } else {
165 dw.println("Log:");
166 ScopedIndent indentLogEntries(dw);
167 gLog.forEachEntry([&dw](const std::string& entry) mutable { dw.println(entry); });
168 }
169 dw.blankline();
170 }
171
Erik Kline2d3a1632016-03-15 16:33:48 +0900172 return NO_ERROR;
173}
174
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900175binder::Status NetdNativeService::isAlive(bool *alive) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900176 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900177 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900178
179 *alive = true;
Erik Klineb31fd692018-06-06 20:50:11 +0900180
181 gLog.log(entry.returns(*alive));
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900182 return binder::Status::ok();
183}
184
Erik Klinef52d4522018-03-14 15:01:46 +0900185binder::Status NetdNativeService::firewallReplaceUidChain(const std::string& chainName,
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900186 bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) {
187 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->firewallCtrl.lock);
Erik Klineb31fd692018-06-06 20:50:11 +0900188 auto entry = gLog.newEntry()
189 .prettyFunction(__PRETTY_FUNCTION__)
190 .arg(chainName)
191 .arg(isWhitelist)
192 .arg(uids);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900193
Erik Klinef52d4522018-03-14 15:01:46 +0900194 int err = gCtls->firewallCtrl.replaceUidChain(chainName, isWhitelist, uids);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900195 *ret = (err == 0);
Erik Klineb31fd692018-06-06 20:50:11 +0900196
197 gLog.log(entry.returns(*ret).withAutomaticDuration());
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900198 return binder::Status::ok();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900199}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900200
201binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
202 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->bandwidthCtrl.lock);
Erik Klineb31fd692018-06-06 20:50:11 +0900203 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(enable);
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900204
205 int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
206 *ret = (err == 0);
Erik Klineb31fd692018-06-06 20:50:11 +0900207 gLog.log(entry.returns(*ret).withAutomaticDuration());
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900208 return binder::Status::ok();
209}
210
Luke Huang531f5d32018-08-03 15:19:05 +0800211binder::Status NetdNativeService::bandwidthSetInterfaceQuota(const std::string& ifName,
212 int64_t bytes) {
213 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
214 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName).arg(bytes);
215
216 int res = gCtls->bandwidthCtrl.setInterfaceQuota(ifName, bytes);
217
218 gLog.log(entry.returns(res).withAutomaticDuration());
219 return statusFromErrcode(res);
220}
221
222binder::Status NetdNativeService::bandwidthRemoveInterfaceQuota(const std::string& ifName) {
223 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
224 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
225
226 int res = gCtls->bandwidthCtrl.removeInterfaceQuota(ifName);
227
228 gLog.log(entry.returns(res).withAutomaticDuration());
229 return statusFromErrcode(res);
230}
231
232binder::Status NetdNativeService::bandwidthSetInterfaceAlert(const std::string& ifName,
233 int64_t bytes) {
234 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
235 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName).arg(bytes);
236
237 int res = gCtls->bandwidthCtrl.setInterfaceAlert(ifName, bytes);
238
239 gLog.log(entry.returns(res).withAutomaticDuration());
240 return statusFromErrcode(res);
241}
242
243binder::Status NetdNativeService::bandwidthRemoveInterfaceAlert(const std::string& ifName) {
244 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
245 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
246
247 int res = gCtls->bandwidthCtrl.removeInterfaceAlert(ifName);
248
249 gLog.log(entry.returns(res).withAutomaticDuration());
250 return statusFromErrcode(res);
251}
252
253binder::Status NetdNativeService::bandwidthSetGlobalAlert(int64_t bytes) {
254 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
255 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(bytes);
256
257 int res = gCtls->bandwidthCtrl.setGlobalAlert(bytes);
258
259 gLog.log(entry.returns(res).withAutomaticDuration());
260 return statusFromErrcode(res);
261}
262
263binder::Status NetdNativeService::bandwidthAddNaughtyApp(int32_t uid) {
264 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
265 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
266
267 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
268 int res = gCtls->bandwidthCtrl.addNaughtyApps(appStrUids);
269
270 gLog.log(entry.returns(res).withAutomaticDuration());
271 return statusFromErrcode(res);
272}
273
274binder::Status NetdNativeService::bandwidthRemoveNaughtyApp(int32_t uid) {
275 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
276 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
277
278 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
279 int res = gCtls->bandwidthCtrl.removeNaughtyApps(appStrUids);
280
281 gLog.log(entry.returns(res).withAutomaticDuration());
282 return statusFromErrcode(res);
283}
284
285binder::Status NetdNativeService::bandwidthAddNiceApp(int32_t uid) {
286 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
287 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
288
289 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
290 int res = gCtls->bandwidthCtrl.addNiceApps(appStrUids);
291
292 gLog.log(entry.returns(res).withAutomaticDuration());
293 return statusFromErrcode(res);
294}
295
296binder::Status NetdNativeService::bandwidthRemoveNiceApp(int32_t uid) {
297 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->bandwidthCtrl.lock);
298 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid);
299
300 std::vector<std::string> appStrUids = {std::to_string(abs(uid))};
301 int res = gCtls->bandwidthCtrl.removeNiceApps(appStrUids);
302
303 gLog.log(entry.returns(res).withAutomaticDuration());
304 return statusFromErrcode(res);
305}
306
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900307binder::Status NetdNativeService::networkCreatePhysical(int32_t netId,
308 const std::string& permission) {
309 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900310 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(netId).arg(permission);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900311 int ret = gCtls->netCtrl.createPhysicalNetwork(netId, stringToPermission(permission.c_str()));
Erik Klineb31fd692018-06-06 20:50:11 +0900312 gLog.log(entry.returns(ret).withAutomaticDuration());
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900313 return statusFromErrcode(ret);
314}
315
316binder::Status NetdNativeService::networkCreateVpn(int32_t netId, bool hasDns, bool secure) {
317 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
318 int ret = gCtls->netCtrl.createVirtualNetwork(netId, hasDns, secure);
319 return statusFromErrcode(ret);
320}
321
322binder::Status NetdNativeService::networkDestroy(int32_t netId) {
Erik Klinec8b6a9c2018-01-15 17:06:48 +0900323 ENFORCE_PERMISSION(NETWORK_STACK);
324 // Both of these functions manage their own locking internally.
325 const int ret = gCtls->netCtrl.destroyNetwork(netId);
326 gCtls->resolverCtrl.clearDnsServers(netId);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900327 return statusFromErrcode(ret);
328}
329
330binder::Status NetdNativeService::networkAddInterface(int32_t netId, const std::string& iface) {
331 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
332 int ret = gCtls->netCtrl.addInterfaceToNetwork(netId, iface.c_str());
333 return statusFromErrcode(ret);
334}
335
336binder::Status NetdNativeService::networkRemoveInterface(int32_t netId, const std::string& iface) {
337 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
338 int ret = gCtls->netCtrl.removeInterfaceFromNetwork(netId, iface.c_str());
339 return statusFromErrcode(ret);
340}
341
342binder::Status NetdNativeService::networkAddUidRanges(int32_t netId,
343 const std::vector<UidRange>& uidRangeArray) {
344 // NetworkController::addUsersToNetwork is thread-safe.
345 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
346 int ret = gCtls->netCtrl.addUsersToNetwork(netId, UidRanges(uidRangeArray));
347 return statusFromErrcode(ret);
348}
349
350binder::Status NetdNativeService::networkRemoveUidRanges(int32_t netId,
351 const std::vector<UidRange>& uidRangeArray) {
352 // NetworkController::removeUsersFromNetwork is thread-safe.
353 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
354 int ret = gCtls->netCtrl.removeUsersFromNetwork(netId, UidRanges(uidRangeArray));
355 return statusFromErrcode(ret);
356}
357
Robin Leeb8087362016-03-30 18:43:08 +0100358binder::Status NetdNativeService::networkRejectNonSecureVpn(bool add,
359 const std::vector<UidRange>& uidRangeArray) {
360 // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
361 // it should be possible to use the same lock as NetworkController. However, every call through
362 // the CommandListener "network" command will need to hold this lock too, not just the ones that
363 // read/modify network internal state (that is sufficient for ::dump() because it doesn't
364 // look at routes, but it's not enough here).
365 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
366
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900367 UidRanges uidRanges(uidRangeArray);
Robin Leeb8087362016-03-30 18:43:08 +0100368
369 int err;
370 if (add) {
371 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
372 } else {
373 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
374 }
375
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900376 return statusFromErrcode(err);
Robin Leeb8087362016-03-30 18:43:08 +0100377}
378
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900379binder::Status NetdNativeService::socketDestroy(const std::vector<UidRange>& uids,
380 const std::vector<int32_t>& skipUids) {
381
382 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
383
384 SockDiag sd;
385 if (!sd.open()) {
386 return binder::Status::fromServiceSpecificError(EIO,
387 String8("Could not open SOCK_DIAG socket"));
388 }
389
390 UidRanges uidRanges(uids);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +0900391 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()),
392 true /* excludeLoopback */);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900393
394 if (err) {
395 return binder::Status::fromServiceSpecificError(-err,
396 String8::format("destroySockets: %s", strerror(-err)));
397 }
Pierre Imaibeedec32016-04-13 06:44:51 +0900398 return binder::Status::ok();
399}
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900400
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400401// Parse a base64 encoded string into a vector of bytes.
402// On failure, return an empty vector.
403static std::vector<uint8_t> parseBase64(const std::string& input) {
404 std::vector<uint8_t> decoded;
405 size_t out_len;
406 if (EVP_DecodedLength(&out_len, input.size()) != 1) {
407 return decoded;
408 }
409 // out_len is now an upper bound on the output length.
410 decoded.resize(out_len);
411 if (EVP_DecodeBase64(decoded.data(), &out_len, decoded.size(),
412 reinterpret_cast<const uint8_t*>(input.data()), input.size()) == 1) {
413 // Possibly shrink the vector if the actual output was smaller than the bound.
414 decoded.resize(out_len);
415 } else {
416 decoded.clear();
417 }
418 if (out_len != SHA256_SIZE) {
419 decoded.clear();
420 }
421 return decoded;
422}
423
Pierre Imaibeedec32016-04-13 06:44:51 +0900424binder::Status NetdNativeService::setResolverConfiguration(int32_t netId,
425 const std::vector<std::string>& servers, const std::vector<std::string>& domains,
Erik Klinea1476fb2018-03-04 21:01:56 +0900426 const std::vector<int32_t>& params, const std::string& tlsName,
427 const std::vector<std::string>& tlsServers,
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400428 const std::vector<std::string>& tlsFingerprints) {
Pierre Imaibeedec32016-04-13 06:44:51 +0900429 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
430 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900431 auto entry = gLog.newEntry()
432 .prettyFunction(__PRETTY_FUNCTION__)
433 .arg(netId)
434 .arg(servers)
435 .arg(domains)
436 .arg(params)
437 .arg(tlsName)
438 .arg(tlsServers)
439 .arg(tlsFingerprints);
Pierre Imaibeedec32016-04-13 06:44:51 +0900440
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400441 std::set<std::vector<uint8_t>> decoded_fingerprints;
442 for (const std::string& fingerprint : tlsFingerprints) {
443 std::vector<uint8_t> decoded = parseBase64(fingerprint);
444 if (decoded.empty()) {
445 return binder::Status::fromServiceSpecificError(EINVAL,
446 String8::format("ResolverController error: bad fingerprint"));
447 }
448 decoded_fingerprints.emplace(decoded);
449 }
450
451 int err = gCtls->resolverCtrl.setResolverConfiguration(netId, servers, domains, params,
Erik Klinea1476fb2018-03-04 21:01:56 +0900452 tlsName, tlsServers, decoded_fingerprints);
Erik Klineb31fd692018-06-06 20:50:11 +0900453 gLog.log(entry.returns(err).withAutomaticDuration());
Pierre Imaibeedec32016-04-13 06:44:51 +0900454 if (err != 0) {
455 return binder::Status::fromServiceSpecificError(-err,
456 String8::format("ResolverController error: %s", strerror(-err)));
457 }
458 return binder::Status::ok();
459}
460
461binder::Status NetdNativeService::getResolverInfo(int32_t netId,
462 std::vector<std::string>* servers, std::vector<std::string>* domains,
463 std::vector<int32_t>* params, std::vector<int32_t>* stats) {
464 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
465 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
466
467 int err = gCtls->resolverCtrl.getResolverInfo(netId, servers, domains, params, stats);
468 if (err != 0) {
469 return binder::Status::fromServiceSpecificError(-err,
470 String8::format("ResolverController error: %s", strerror(-err)));
471 }
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900472 return binder::Status::ok();
473}
474
Erik Klinef48e4dd2016-07-18 04:02:07 +0900475binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800476 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
Erik Klinef48e4dd2016-07-18 04:02:07 +0900477
478 *ret = gCtls->tetherCtrl.applyDnsInterfaces();
479 return binder::Status::ok();
480}
481
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900482namespace {
483
484void tetherAddStats(PersistableBundle *bundle, const TetherController::TetherStats& stats) {
485 String16 iface = String16(stats.extIface.c_str());
486 std::vector<int64_t> statsVector(INetd::TETHER_STATS_ARRAY_SIZE);
487
488 bundle->getLongVector(iface, &statsVector);
489 if (statsVector.size() == 0) {
490 for (int i = 0; i < INetd::TETHER_STATS_ARRAY_SIZE; i++) statsVector.push_back(0);
491 }
492
Lorenzo Colitti9a65ac62017-09-04 18:07:56 +0900493 statsVector[INetd::TETHER_STATS_RX_BYTES] += stats.rxBytes;
494 statsVector[INetd::TETHER_STATS_RX_PACKETS] += stats.rxPackets;
495 statsVector[INetd::TETHER_STATS_TX_BYTES] += stats.txBytes;
496 statsVector[INetd::TETHER_STATS_TX_PACKETS] += stats.txPackets;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900497
498 bundle->putLongVector(iface, statsVector);
499}
500
501} // namespace
502
503binder::Status NetdNativeService::tetherGetStats(PersistableBundle *bundle) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800504 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900505
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900506 const auto& statsList = gCtls->tetherCtrl.getTetherStats();
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900507 if (!isOk(statsList)) {
Nathan Harold28ccfef2018-03-16 19:02:47 -0700508 return asBinderStatus(statsList);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900509 }
510
511 for (const auto& stats : statsList.value()) {
512 tetherAddStats(bundle, stats);
513 }
514
515 return binder::Status::ok();
516}
517
Erik Kline53c20882016-08-02 15:22:53 +0900518binder::Status NetdNativeService::interfaceAddAddress(const std::string &ifName,
519 const std::string &addrString, int prefixLength) {
520 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
521
522 const int err = InterfaceController::addAddress(
523 ifName.c_str(), addrString.c_str(), prefixLength);
524 if (err != 0) {
525 return binder::Status::fromServiceSpecificError(-err,
526 String8::format("InterfaceController error: %s", strerror(-err)));
527 }
528 return binder::Status::ok();
529}
530
531binder::Status NetdNativeService::interfaceDelAddress(const std::string &ifName,
532 const std::string &addrString, int prefixLength) {
533 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
534
535 const int err = InterfaceController::delAddress(
536 ifName.c_str(), addrString.c_str(), prefixLength);
537 if (err != 0) {
538 return binder::Status::fromServiceSpecificError(-err,
539 String8::format("InterfaceController error: %s", strerror(-err)));
540 }
541 return binder::Status::ok();
542}
543
Erik Kline38e51f12018-09-06 20:14:44 +0900544namespace {
Erik Kline55b06f82016-07-04 09:57:18 +0900545
Erik Kline38e51f12018-09-06 20:14:44 +0900546std::tuple<binder::Status, const char*, const char*> getPathComponents(int32_t ipversion,
547 int32_t category) {
548 const char* ipversionStr = nullptr;
549 switch (ipversion) {
Erik Kline55b06f82016-07-04 09:57:18 +0900550 case INetd::IPV4:
Erik Kline38e51f12018-09-06 20:14:44 +0900551 ipversionStr = "ipv4";
Erik Kline55b06f82016-07-04 09:57:18 +0900552 break;
553 case INetd::IPV6:
Erik Kline38e51f12018-09-06 20:14:44 +0900554 ipversionStr = "ipv6";
Erik Kline55b06f82016-07-04 09:57:18 +0900555 break;
556 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900557 return {binder::Status::fromServiceSpecificError(EAFNOSUPPORT, "Bad IP version"),
558 nullptr, nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900559 }
560
Erik Kline38e51f12018-09-06 20:14:44 +0900561 const char* whichStr = nullptr;
562 switch (category) {
Erik Kline55b06f82016-07-04 09:57:18 +0900563 case INetd::CONF:
564 whichStr = "conf";
565 break;
566 case INetd::NEIGH:
567 whichStr = "neigh";
568 break;
569 default:
Erik Kline38e51f12018-09-06 20:14:44 +0900570 return {binder::Status::fromServiceSpecificError(EINVAL, "Bad category"), nullptr,
571 nullptr};
Erik Kline55b06f82016-07-04 09:57:18 +0900572 }
573
Erik Kline38e51f12018-09-06 20:14:44 +0900574 return {binder::Status::ok(), ipversionStr, whichStr};
575}
576
577} // namespace
578
579binder::Status NetdNativeService::getProcSysNet(int32_t ipversion, int32_t which,
580 const std::string& ifname,
581 const std::string& parameter, std::string* value) {
582 ENFORCE_PERMISSION(NETWORK_STACK);
583 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__)
584 .args(ipversion, which, ifname, parameter);
585
586 const auto pathParts = getPathComponents(ipversion, which);
587 const auto& pathStatus = std::get<0>(pathParts);
588 if (!pathStatus.isOk()) {
589 gLog.log(entry.returns(pathStatus.exceptionCode()).withAutomaticDuration());
590 return pathStatus;
Erik Kline55b06f82016-07-04 09:57:18 +0900591 }
Erik Kline38e51f12018-09-06 20:14:44 +0900592
593 const int err = InterfaceController::getParameter(std::get<1>(pathParts),
594 std::get<2>(pathParts), ifname.c_str(),
595 parameter.c_str(), value);
596 entry.returns(err);
597 if (err == 0) entry.returns(*value);
598 gLog.log(entry.withAutomaticDuration());
599 return statusFromErrcode(err);
600}
601
602binder::Status NetdNativeService::setProcSysNet(int32_t ipversion, int32_t which,
603 const std::string& ifname,
604 const std::string& parameter,
605 const std::string& value) {
606 ENFORCE_PERMISSION(NETWORK_STACK);
607 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__)
608 .args(ipversion, which, ifname, parameter, value);
609
610 const auto pathParts = getPathComponents(ipversion, which);
611 const auto& pathStatus = std::get<0>(pathParts);
612 if (!pathStatus.isOk()) {
613 gLog.log(entry.returns(pathStatus.exceptionCode()).withAutomaticDuration());
614 return pathStatus;
615 }
616
617 const int err = InterfaceController::setParameter(std::get<1>(pathParts),
618 std::get<2>(pathParts), ifname.c_str(),
619 parameter.c_str(), value.c_str());
620 gLog.log(entry.returns(err).withAutomaticDuration());
621 return statusFromErrcode(err);
Erik Kline55b06f82016-07-04 09:57:18 +0900622}
623
Robin Lee2cf56172016-09-13 18:55:42 +0900624binder::Status NetdNativeService::getMetricsReportingLevel(int *reportingLevel) {
625 // This function intentionally does not lock, since the only thing it does is one read from an
626 // atomic_int.
627 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
628 ENFORCE_DEBUGGABLE();
629
Michal Karpinskid5440112016-10-06 16:56:04 +0100630 *reportingLevel = gCtls->eventReporter.getMetricsReportingLevel();
Robin Lee2cf56172016-09-13 18:55:42 +0900631 return binder::Status::ok();
632}
633
634binder::Status NetdNativeService::setMetricsReportingLevel(const int reportingLevel) {
635 // This function intentionally does not lock, since the only thing it does is one write to an
636 // atomic_int.
637 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
638 ENFORCE_DEBUGGABLE();
639
Michal Karpinskid5440112016-10-06 16:56:04 +0100640 return (gCtls->eventReporter.setMetricsReportingLevel(reportingLevel) == 0)
641 ? binder::Status::ok()
642 : binder::Status::fromExceptionCode(binder::Status::EX_ILLEGAL_ARGUMENT);
Robin Lee2cf56172016-09-13 18:55:42 +0900643}
644
Benedict Wongb2daefb2017-12-06 22:05:46 -0800645binder::Status NetdNativeService::ipSecSetEncapSocketOwner(const android::base::unique_fd& socket,
646 int newUid) {
647 ENFORCE_PERMISSION(NETWORK_STACK)
Erik Klineb31fd692018-06-06 20:50:11 +0900648 gLog.log("ipSecSetEncapSocketOwner()");
Benedict Wongb2daefb2017-12-06 22:05:46 -0800649
650 uid_t callerUid = IPCThreadState::self()->getCallingUid();
651 return asBinderStatus(gCtls->xfrmCtrl.ipSecSetEncapSocketOwner(socket, newUid, callerUid));
652}
653
Nathan Harold1a371532017-01-30 12:30:48 -0800654binder::Status NetdNativeService::ipSecAllocateSpi(
655 int32_t transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800656 const std::string& sourceAddress,
657 const std::string& destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800658 int32_t inSpi,
659 int32_t* outSpi) {
660 // Necessary locking done in IpSecService and kernel
661 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900662 gLog.log("ipSecAllocateSpi()");
ludi6e8eccd2017-08-14 14:40:37 -0700663 return asBinderStatus(gCtls->xfrmCtrl.ipSecAllocateSpi(
Nathan Harold1a371532017-01-30 12:30:48 -0800664 transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800665 sourceAddress,
666 destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800667 inSpi,
668 outSpi));
669}
670
671binder::Status NetdNativeService::ipSecAddSecurityAssociation(
672 int32_t transformId,
673 int32_t mode,
Nathan Haroldda54f122018-01-09 16:42:57 -0800674 const std::string& sourceAddress,
675 const std::string& destinationAddress,
Benedict Wong96abf482018-01-22 13:56:41 -0800676 int32_t underlyingNetId,
Nathan Harold1a371532017-01-30 12:30:48 -0800677 int32_t spi,
Di Lu2ccb3e52018-01-03 16:19:20 -0800678 int32_t markValue,
679 int32_t markMask,
Nathan Harold1a371532017-01-30 12:30:48 -0800680 const std::string& authAlgo, const std::vector<uint8_t>& authKey, int32_t authTruncBits,
681 const std::string& cryptAlgo, const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits,
Benedict Wongbe65b432017-08-22 21:43:14 -0700682 const std::string& aeadAlgo, const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits,
Nathan Harold1a371532017-01-30 12:30:48 -0800683 int32_t encapType,
684 int32_t encapLocalPort,
ludiec836052017-05-20 14:17:05 -0700685 int32_t encapRemotePort) {
Nathan Harold1a371532017-01-30 12:30:48 -0800686 // Necessary locking done in IpSecService and kernel
687 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900688 gLog.log("ipSecAddSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700689 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityAssociation(
Benedict Wongad600cb2018-05-14 17:22:35 -0700690 transformId, mode, sourceAddress, destinationAddress, underlyingNetId, spi, markValue,
691 markMask, authAlgo, authKey, authTruncBits, cryptAlgo, cryptKey, cryptTruncBits,
692 aeadAlgo, aeadKey, aeadIcvBits, encapType, encapLocalPort, encapRemotePort));
Nathan Harold1a371532017-01-30 12:30:48 -0800693}
694
695binder::Status NetdNativeService::ipSecDeleteSecurityAssociation(
696 int32_t transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800697 const std::string& sourceAddress,
698 const std::string& destinationAddress,
Di Lu2ccb3e52018-01-03 16:19:20 -0800699 int32_t spi,
700 int32_t markValue,
701 int32_t markMask) {
Nathan Harold1a371532017-01-30 12:30:48 -0800702 // Necessary locking done in IpSecService and kernel
703 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900704 gLog.log("ipSecDeleteSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700705 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityAssociation(
Nathan Harold1a371532017-01-30 12:30:48 -0800706 transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800707 sourceAddress,
708 destinationAddress,
Di Lu2ccb3e52018-01-03 16:19:20 -0800709 spi,
710 markValue,
711 markMask));
Nathan Harold1a371532017-01-30 12:30:48 -0800712}
713
714binder::Status NetdNativeService::ipSecApplyTransportModeTransform(
715 const android::base::unique_fd& socket,
716 int32_t transformId,
717 int32_t direction,
Nathan Haroldda54f122018-01-09 16:42:57 -0800718 const std::string& sourceAddress,
719 const std::string& destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800720 int32_t spi) {
721 // Necessary locking done in IpSecService and kernel
722 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900723 gLog.log("ipSecApplyTransportModeTransform()");
ludi6e8eccd2017-08-14 14:40:37 -0700724 return asBinderStatus(gCtls->xfrmCtrl.ipSecApplyTransportModeTransform(
Nathan Harold1a371532017-01-30 12:30:48 -0800725 socket,
726 transformId,
727 direction,
Nathan Haroldda54f122018-01-09 16:42:57 -0800728 sourceAddress,
729 destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800730 spi));
731}
732
733binder::Status NetdNativeService::ipSecRemoveTransportModeTransform(
734 const android::base::unique_fd& socket) {
735 // Necessary locking done in IpSecService and kernel
736 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
Erik Klineb31fd692018-06-06 20:50:11 +0900737 gLog.log("ipSecRemoveTransportModeTransform()");
ludi6e8eccd2017-08-14 14:40:37 -0700738 return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTransportModeTransform(
Nathan Harold1a371532017-01-30 12:30:48 -0800739 socket));
740}
741
Benedict Wonga04ffa72018-05-09 21:42:42 -0700742binder::Status NetdNativeService::ipSecAddSecurityPolicy(int32_t transformId, int32_t selAddrFamily,
743 int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700744 const std::string& tmplSrcAddress,
745 const std::string& tmplDstAddress,
746 int32_t spi, int32_t markValue,
747 int32_t markMask) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800748 // Necessary locking done in IpSecService and kernel
749 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900750 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800751 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700752 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
753 markMask));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800754}
755
Benedict Wonga04ffa72018-05-09 21:42:42 -0700756binder::Status NetdNativeService::ipSecUpdateSecurityPolicy(int32_t transformId,
757 int32_t selAddrFamily,
758 int32_t direction,
Benedict Wongad600cb2018-05-14 17:22:35 -0700759 const std::string& tmplSrcAddress,
760 const std::string& tmplDstAddress,
761 int32_t spi, int32_t markValue,
762 int32_t markMask) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800763 // Necessary locking done in IpSecService and kernel
764 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900765 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800766 return asBinderStatus(gCtls->xfrmCtrl.ipSecUpdateSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700767 transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
768 markMask));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800769}
770
Benedict Wonga04ffa72018-05-09 21:42:42 -0700771binder::Status NetdNativeService::ipSecDeleteSecurityPolicy(int32_t transformId,
772 int32_t selAddrFamily,
773 int32_t direction, int32_t markValue,
774 int32_t markMask) {
Benedict Wong84a8dca2018-01-19 12:12:17 -0800775 // Necessary locking done in IpSecService and kernel
776 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900777 gLog.log("ipSecAddSecurityPolicy()");
Benedict Wong84a8dca2018-01-19 12:12:17 -0800778 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityPolicy(
Benedict Wonga04ffa72018-05-09 21:42:42 -0700779 transformId, selAddrFamily, direction, markValue, markMask));
Benedict Wong84a8dca2018-01-19 12:12:17 -0800780}
781
manojboopathi8707f232018-01-02 14:45:47 -0800782binder::Status NetdNativeService::addVirtualTunnelInterface(
783 const std::string& deviceName,
784 const std::string& localAddress,
785 const std::string& remoteAddress,
786 int32_t iKey,
787 int32_t oKey) {
788 // Necessary locking done in IpSecService and kernel
789 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900790 gLog.log("addVirtualTunnelInterface()");
manojboopathi8707f232018-01-02 14:45:47 -0800791 int ret = gCtls->xfrmCtrl.addVirtualTunnelInterface(
792 deviceName,
793 localAddress,
794 remoteAddress,
795 iKey,
796 oKey,
797 false);
798
799 return (ret == 0) ? binder::Status::ok() :
800 asBinderStatus(netdutils::statusFromErrno(
801 ret, "Error in creating virtual tunnel interface."));
802}
803
804binder::Status NetdNativeService::updateVirtualTunnelInterface(
805 const std::string& deviceName,
806 const std::string& localAddress,
807 const std::string& remoteAddress,
808 int32_t iKey,
809 int32_t oKey) {
810 // Necessary locking done in IpSecService and kernel
811 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900812 gLog.log("updateVirtualTunnelInterface()");
manojboopathi8707f232018-01-02 14:45:47 -0800813 int ret = gCtls->xfrmCtrl.addVirtualTunnelInterface(
814 deviceName,
815 localAddress,
816 remoteAddress,
817 iKey,
818 oKey,
819 true);
820
821 return (ret == 0) ? binder::Status::ok() :
822 asBinderStatus(netdutils::statusFromErrno(
823 ret, "Error in updating virtual tunnel interface."));
824}
825
826binder::Status NetdNativeService::removeVirtualTunnelInterface(const std::string& deviceName) {
827 // Necessary locking done in IpSecService and kernel
828 ENFORCE_PERMISSION(NETWORK_STACK);
Erik Klineb31fd692018-06-06 20:50:11 +0900829 gLog.log("removeVirtualTunnelInterface()");
manojboopathi8707f232018-01-02 14:45:47 -0800830 int ret = gCtls->xfrmCtrl.removeVirtualTunnelInterface(deviceName);
831
832 return (ret == 0) ? binder::Status::ok() :
833 asBinderStatus(netdutils::statusFromErrno(
834 ret, "Error in removing virtual tunnel interface."));
835}
836
Joel Scherpelzde937962017-06-01 13:20:21 +0900837binder::Status NetdNativeService::setIPv6AddrGenMode(const std::string& ifName,
838 int32_t mode) {
839 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700840 return asBinderStatus(InterfaceController::setIPv6AddrGenMode(ifName, mode));
Joel Scherpelzde937962017-06-01 13:20:21 +0900841}
842
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900843binder::Status NetdNativeService::wakeupAddInterface(const std::string& ifName,
844 const std::string& prefix, int32_t mark,
845 int32_t mask) {
846 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700847 return asBinderStatus(gCtls->wakeupCtrl.addInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900848}
849
850binder::Status NetdNativeService::wakeupDelInterface(const std::string& ifName,
851 const std::string& prefix, int32_t mark,
852 int32_t mask) {
853 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700854 return asBinderStatus(gCtls->wakeupCtrl.delInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900855}
856
Chenbo Feng07d43fe2017-12-21 14:38:51 -0800857binder::Status NetdNativeService::trafficCheckBpfStatsEnable(bool* ret) {
858 ENFORCE_PERMISSION(NETWORK_STACK);
859 *ret = gCtls->trafficCtrl.checkBpfStatsEnable();
860 return binder::Status::ok();
861}
862
Luke Huang0051a622018-07-23 20:30:16 +0800863binder::Status NetdNativeService::idletimerAddInterface(const std::string& ifName, int32_t timeout,
864 const std::string& classLabel) {
865 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->idletimerCtrl.lock);
866 auto entry = gLog.newEntry()
867 .prettyFunction(__PRETTY_FUNCTION__)
868 .arg(ifName)
869 .arg(timeout)
870 .arg(classLabel);
871 int res =
872 gCtls->idletimerCtrl.addInterfaceIdletimer(ifName.c_str(), timeout, classLabel.c_str());
873 gLog.log(entry.returns(res).withAutomaticDuration());
874 return statusFromErrcode(res);
875}
876
877binder::Status NetdNativeService::idletimerRemoveInterface(const std::string& ifName,
878 int32_t timeout,
879 const std::string& classLabel) {
880 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->idletimerCtrl.lock);
881 auto entry = gLog.newEntry()
882 .prettyFunction(__PRETTY_FUNCTION__)
883 .arg(ifName)
884 .arg(timeout)
885 .arg(classLabel);
886 int res = gCtls->idletimerCtrl.removeInterfaceIdletimer(ifName.c_str(), timeout,
887 classLabel.c_str());
888 gLog.log(entry.returns(res).withAutomaticDuration());
889 return statusFromErrcode(res);
890}
Luke Huanga67dd562018-07-17 19:58:25 +0800891
892binder::Status NetdNativeService::strictUidCleartextPenalty(int32_t uid, int32_t policyPenalty) {
893 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->strictCtrl.lock);
894 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(uid).arg(policyPenalty);
895 StrictPenalty penalty;
896 switch (policyPenalty) {
897 case INetd::PENALTY_POLICY_REJECT:
898 penalty = REJECT;
899 break;
900 case INetd::PENALTY_POLICY_LOG:
901 penalty = LOG;
902 break;
903 case INetd::PENALTY_POLICY_ACCEPT:
904 penalty = ACCEPT;
905 break;
906 default:
907 return statusFromErrcode(-EINVAL);
908 break;
909 }
910 int res = gCtls->strictCtrl.setUidCleartextPenalty((uid_t) uid, penalty);
911 gLog.log(entry.returns(res).withAutomaticDuration());
912 return statusFromErrcode(res);
913}
Luke Huang6d301232018-08-01 14:05:18 +0800914binder::Status NetdNativeService::clatdStart(const std::string& ifName) {
915 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->clatdCtrl.mutex);
916 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
917 int res = gCtls->clatdCtrl.startClatd(ifName.c_str());
918 gLog.log(entry.returns(res).withAutomaticDuration());
919 return statusFromErrcode(res);
920}
921
922binder::Status NetdNativeService::clatdStop(const std::string& ifName) {
923 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->clatdCtrl.mutex);
924 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
925 int res = gCtls->clatdCtrl.stopClatd(ifName.c_str());
926 gLog.log(entry.returns(res).withAutomaticDuration());
927 return statusFromErrcode(res);
928}
Luke Huanga67dd562018-07-17 19:58:25 +0800929
Luke Huang457d4702018-08-16 15:39:15 +0800930binder::Status NetdNativeService::ipfwdEnabled(bool* status) {
931 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
932 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__);
933 *status = (gCtls->tetherCtrl.forwardingRequestCount() > 0) ? true : false;
934 gLog.log(entry.returns(*status).withAutomaticDuration());
935 return binder::Status::ok();
936}
937
938binder::Status NetdNativeService::ipfwdEnableForwarding(const std::string& requester) {
939 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
940 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(requester);
941 int res = (gCtls->tetherCtrl.enableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
942 gLog.log(entry.returns(res).withAutomaticDuration());
943 return statusFromErrcode(res);
944}
945
946binder::Status NetdNativeService::ipfwdDisableForwarding(const std::string& requester) {
947 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock);
948 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(requester);
949 int res = (gCtls->tetherCtrl.disableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
950 gLog.log(entry.returns(res).withAutomaticDuration());
951 return statusFromErrcode(res);
952}
953
954binder::Status NetdNativeService::ipfwdAddInterfaceForward(const std::string& fromIface,
955 const std::string& toIface) {
956 ENFORCE_PERMISSION(NETWORK_STACK);
957 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(fromIface).arg(toIface);
958 int res = RouteController::enableTethering(fromIface.c_str(), toIface.c_str());
959 gLog.log(entry.returns(res).withAutomaticDuration());
960 return statusFromErrcode(res);
961}
962
963binder::Status NetdNativeService::ipfwdRemoveInterfaceForward(const std::string& fromIface,
964 const std::string& toIface) {
965 ENFORCE_PERMISSION(NETWORK_STACK);
966 auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(fromIface).arg(toIface);
967 int res = RouteController::disableTethering(fromIface.c_str(), toIface.c_str());
968 gLog.log(entry.returns(res).withAutomaticDuration());
969 return statusFromErrcode(res);
970}
971
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900972} // namespace net
973} // namespace android