blob: 59e8e15d4dc516b6554cc44d71c978f336fe6b33 [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>
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090024#include <cutils/log.h>
Robin Lee2cf56172016-09-13 18:55:42 +090025#include <cutils/properties.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"
Robin Leeb8087362016-03-30 18:43:08 +010041#include "RouteController.h"
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090042#include "SockDiag.h"
Robin Leeb8087362016-03-30 18:43:08 +010043#include "UidRanges.h"
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090044
45using android::base::StringPrintf;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +090046using android::os::PersistableBundle;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090047
48namespace android {
49namespace net {
50
51namespace {
52
53const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL";
Joel Scherpelz08b84cd2017-05-22 13:11:54 +090054const char NETWORK_STACK[] = "android.permission.NETWORK_STACK";
Erik Kline2d3a1632016-03-15 16:33:48 +090055const char DUMP[] = "android.permission.DUMP";
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090056
57binder::Status checkPermission(const char *permission) {
58 pid_t pid;
59 uid_t uid;
60
61 if (checkCallingPermission(String16(permission), (int32_t *) &pid, (int32_t *) &uid)) {
62 return binder::Status::ok();
63 } else {
64 auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission);
65 return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str()));
66 }
67}
68
Robin Lee2cf56172016-09-13 18:55:42 +090069#define ENFORCE_DEBUGGABLE() { \
70 char value[PROPERTY_VALUE_MAX + 1]; \
71 if (property_get("ro.debuggable", value, NULL) != 1 \
72 || value[0] != '1') { \
73 return binder::Status::fromExceptionCode( \
74 binder::Status::EX_SECURITY, \
75 String8("Not available in production builds.") \
76 ); \
77 } \
78}
79
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090080#define ENFORCE_PERMISSION(permission) { \
81 binder::Status status = checkPermission((permission)); \
82 if (!status.isOk()) { \
83 return status; \
84 } \
85}
86
Lorenzo Colitti89faa342016-02-26 11:38:47 +090087#define NETD_LOCKING_RPC(permission, lock) \
88 ENFORCE_PERMISSION(permission); \
89 android::RWLock::AutoWLock _lock(lock);
90
91#define NETD_BIG_LOCK_RPC(permission) NETD_LOCKING_RPC((permission), gBigNetdLock)
Lorenzo Colittid33e96d2016-12-15 23:59:01 +090092
93inline binder::Status statusFromErrcode(int ret) {
94 if (ret) {
95 return binder::Status::fromServiceSpecificError(-ret, strerror(-ret));
96 }
97 return binder::Status::ok();
98}
99
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900100} // namespace
101
102
Lorenzo Colittie4851de2016-03-17 13:23:28 +0900103status_t NetdNativeService::start() {
104 IPCThreadState::self()->disableBackgroundScheduling(true);
105 status_t ret = BinderService<NetdNativeService>::publish();
106 if (ret != android::OK) {
107 return ret;
108 }
109 sp<ProcessState> ps(ProcessState::self());
110 ps->startThreadPool();
111 ps->giveThreadPoolName();
112 return android::OK;
113}
114
Hugo Benichi7b314e12018-01-15 21:54:00 +0900115status_t NetdNativeService::dump(int fd, const Vector<String16> &args) {
Erik Kline2d3a1632016-03-15 16:33:48 +0900116 const binder::Status dump_permission = checkPermission(DUMP);
117 if (!dump_permission.isOk()) {
118 const String8 msg(dump_permission.toString8());
119 write(fd, msg.string(), msg.size());
120 return PERMISSION_DENIED;
121 }
122
123 // This method does not grab any locks. If individual classes need locking
124 // their dump() methods MUST handle locking appropriately.
Hugo Benichi7b314e12018-01-15 21:54:00 +0900125
Erik Kline2d3a1632016-03-15 16:33:48 +0900126 DumpWriter dw(fd);
Hugo Benichi7b314e12018-01-15 21:54:00 +0900127
128 if (!args.isEmpty() && args[0] == TcpSocketMonitor::DUMP_KEYWORD) {
129 dw.blankline();
130 gCtls->tcpSocketMonitor.dump(dw);
131 dw.blankline();
132 return NO_ERROR;
133 }
134
Erik Kline2d3a1632016-03-15 16:33:48 +0900135 dw.blankline();
136 gCtls->netCtrl.dump(dw);
137 dw.blankline();
138
139 return NO_ERROR;
140}
141
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900142binder::Status NetdNativeService::isAlive(bool *alive) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900143 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900144
145 *alive = true;
146 return binder::Status::ok();
147}
148
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900149binder::Status NetdNativeService::firewallReplaceUidChain(const android::String16& chainName,
150 bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) {
151 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->firewallCtrl.lock);
152
153 android::String8 name = android::String8(chainName);
154 int err = gCtls->firewallCtrl.replaceUidChain(name.string(), isWhitelist, uids);
155 *ret = (err == 0);
156 return binder::Status::ok();
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900157}
Lorenzo Colittidedd2712016-03-22 12:36:29 +0900158
159binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
160 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->bandwidthCtrl.lock);
161
162 int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
163 *ret = (err == 0);
164 return binder::Status::ok();
165}
166
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900167binder::Status NetdNativeService::networkCreatePhysical(int32_t netId,
168 const std::string& permission) {
169 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
170 int ret = gCtls->netCtrl.createPhysicalNetwork(netId, stringToPermission(permission.c_str()));
171 return statusFromErrcode(ret);
172}
173
174binder::Status NetdNativeService::networkCreateVpn(int32_t netId, bool hasDns, bool secure) {
175 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
176 int ret = gCtls->netCtrl.createVirtualNetwork(netId, hasDns, secure);
177 return statusFromErrcode(ret);
178}
179
180binder::Status NetdNativeService::networkDestroy(int32_t netId) {
Erik Klinec8b6a9c2018-01-15 17:06:48 +0900181 ENFORCE_PERMISSION(NETWORK_STACK);
182 // Both of these functions manage their own locking internally.
183 const int ret = gCtls->netCtrl.destroyNetwork(netId);
184 gCtls->resolverCtrl.clearDnsServers(netId);
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900185 return statusFromErrcode(ret);
186}
187
188binder::Status NetdNativeService::networkAddInterface(int32_t netId, const std::string& iface) {
189 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
190 int ret = gCtls->netCtrl.addInterfaceToNetwork(netId, iface.c_str());
191 return statusFromErrcode(ret);
192}
193
194binder::Status NetdNativeService::networkRemoveInterface(int32_t netId, const std::string& iface) {
195 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
196 int ret = gCtls->netCtrl.removeInterfaceFromNetwork(netId, iface.c_str());
197 return statusFromErrcode(ret);
198}
199
200binder::Status NetdNativeService::networkAddUidRanges(int32_t netId,
201 const std::vector<UidRange>& uidRangeArray) {
202 // NetworkController::addUsersToNetwork is thread-safe.
203 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
204 int ret = gCtls->netCtrl.addUsersToNetwork(netId, UidRanges(uidRangeArray));
205 return statusFromErrcode(ret);
206}
207
208binder::Status NetdNativeService::networkRemoveUidRanges(int32_t netId,
209 const std::vector<UidRange>& uidRangeArray) {
210 // NetworkController::removeUsersFromNetwork is thread-safe.
211 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
212 int ret = gCtls->netCtrl.removeUsersFromNetwork(netId, UidRanges(uidRangeArray));
213 return statusFromErrcode(ret);
214}
215
Robin Leeb8087362016-03-30 18:43:08 +0100216binder::Status NetdNativeService::networkRejectNonSecureVpn(bool add,
217 const std::vector<UidRange>& uidRangeArray) {
218 // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
219 // it should be possible to use the same lock as NetworkController. However, every call through
220 // the CommandListener "network" command will need to hold this lock too, not just the ones that
221 // read/modify network internal state (that is sufficient for ::dump() because it doesn't
222 // look at routes, but it's not enough here).
223 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
224
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900225 UidRanges uidRanges(uidRangeArray);
Robin Leeb8087362016-03-30 18:43:08 +0100226
227 int err;
228 if (add) {
229 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
230 } else {
231 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
232 }
233
Lorenzo Colittid33e96d2016-12-15 23:59:01 +0900234 return statusFromErrcode(err);
Robin Leeb8087362016-03-30 18:43:08 +0100235}
236
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900237binder::Status NetdNativeService::socketDestroy(const std::vector<UidRange>& uids,
238 const std::vector<int32_t>& skipUids) {
239
240 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
241
242 SockDiag sd;
243 if (!sd.open()) {
244 return binder::Status::fromServiceSpecificError(EIO,
245 String8("Could not open SOCK_DIAG socket"));
246 }
247
248 UidRanges uidRanges(uids);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +0900249 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()),
250 true /* excludeLoopback */);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900251
252 if (err) {
253 return binder::Status::fromServiceSpecificError(-err,
254 String8::format("destroySockets: %s", strerror(-err)));
255 }
Pierre Imaibeedec32016-04-13 06:44:51 +0900256 return binder::Status::ok();
257}
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900258
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400259// Parse a base64 encoded string into a vector of bytes.
260// On failure, return an empty vector.
261static std::vector<uint8_t> parseBase64(const std::string& input) {
262 std::vector<uint8_t> decoded;
263 size_t out_len;
264 if (EVP_DecodedLength(&out_len, input.size()) != 1) {
265 return decoded;
266 }
267 // out_len is now an upper bound on the output length.
268 decoded.resize(out_len);
269 if (EVP_DecodeBase64(decoded.data(), &out_len, decoded.size(),
270 reinterpret_cast<const uint8_t*>(input.data()), input.size()) == 1) {
271 // Possibly shrink the vector if the actual output was smaller than the bound.
272 decoded.resize(out_len);
273 } else {
274 decoded.clear();
275 }
276 if (out_len != SHA256_SIZE) {
277 decoded.clear();
278 }
279 return decoded;
280}
281
Pierre Imaibeedec32016-04-13 06:44:51 +0900282binder::Status NetdNativeService::setResolverConfiguration(int32_t netId,
283 const std::vector<std::string>& servers, const std::vector<std::string>& domains,
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400284 const std::vector<int32_t>& params, bool useTls, const std::string& tlsName,
285 const std::vector<std::string>& tlsFingerprints) {
Pierre Imaibeedec32016-04-13 06:44:51 +0900286 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
287 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
288
Ben Schwartz4204ecf2017-10-02 12:35:48 -0400289 std::set<std::vector<uint8_t>> decoded_fingerprints;
290 for (const std::string& fingerprint : tlsFingerprints) {
291 std::vector<uint8_t> decoded = parseBase64(fingerprint);
292 if (decoded.empty()) {
293 return binder::Status::fromServiceSpecificError(EINVAL,
294 String8::format("ResolverController error: bad fingerprint"));
295 }
296 decoded_fingerprints.emplace(decoded);
297 }
298
299 int err = gCtls->resolverCtrl.setResolverConfiguration(netId, servers, domains, params,
300 useTls, tlsName, decoded_fingerprints);
Pierre Imaibeedec32016-04-13 06:44:51 +0900301 if (err != 0) {
302 return binder::Status::fromServiceSpecificError(-err,
303 String8::format("ResolverController error: %s", strerror(-err)));
304 }
305 return binder::Status::ok();
306}
307
308binder::Status NetdNativeService::getResolverInfo(int32_t netId,
309 std::vector<std::string>* servers, std::vector<std::string>* domains,
310 std::vector<int32_t>* params, std::vector<int32_t>* stats) {
311 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
312 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
313
314 int err = gCtls->resolverCtrl.getResolverInfo(netId, servers, domains, params, stats);
315 if (err != 0) {
316 return binder::Status::fromServiceSpecificError(-err,
317 String8::format("ResolverController error: %s", strerror(-err)));
318 }
Lorenzo Colitti563d98b2016-04-24 13:13:14 +0900319 return binder::Status::ok();
320}
321
Erik Klinef48e4dd2016-07-18 04:02:07 +0900322binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900323 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock)
Erik Klinef48e4dd2016-07-18 04:02:07 +0900324
325 *ret = gCtls->tetherCtrl.applyDnsInterfaces();
326 return binder::Status::ok();
327}
328
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900329namespace {
330
331void tetherAddStats(PersistableBundle *bundle, const TetherController::TetherStats& stats) {
332 String16 iface = String16(stats.extIface.c_str());
333 std::vector<int64_t> statsVector(INetd::TETHER_STATS_ARRAY_SIZE);
334
335 bundle->getLongVector(iface, &statsVector);
336 if (statsVector.size() == 0) {
337 for (int i = 0; i < INetd::TETHER_STATS_ARRAY_SIZE; i++) statsVector.push_back(0);
338 }
339
Lorenzo Colitti9a65ac62017-09-04 18:07:56 +0900340 statsVector[INetd::TETHER_STATS_RX_BYTES] += stats.rxBytes;
341 statsVector[INetd::TETHER_STATS_RX_PACKETS] += stats.rxPackets;
342 statsVector[INetd::TETHER_STATS_TX_BYTES] += stats.txBytes;
343 statsVector[INetd::TETHER_STATS_TX_PACKETS] += stats.txPackets;
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900344
345 bundle->putLongVector(iface, statsVector);
346}
347
348} // namespace
349
350binder::Status NetdNativeService::tetherGetStats(PersistableBundle *bundle) {
351 NETD_LOCKING_RPC(NETWORK_STACK, gCtls->tetherCtrl.lock)
352
Lorenzo Colitti5192bf72017-09-04 13:30:59 +0900353 const auto& statsList = gCtls->tetherCtrl.getTetherStats();
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900354 if (!isOk(statsList)) {
Nathan Harold28ccfef2018-03-16 19:02:47 -0700355 return asBinderStatus(statsList);
Lorenzo Colitti9a8a9ff2017-01-31 19:06:59 +0900356 }
357
358 for (const auto& stats : statsList.value()) {
359 tetherAddStats(bundle, stats);
360 }
361
362 return binder::Status::ok();
363}
364
Erik Kline53c20882016-08-02 15:22:53 +0900365binder::Status NetdNativeService::interfaceAddAddress(const std::string &ifName,
366 const std::string &addrString, int prefixLength) {
367 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
368
369 const int err = InterfaceController::addAddress(
370 ifName.c_str(), addrString.c_str(), prefixLength);
371 if (err != 0) {
372 return binder::Status::fromServiceSpecificError(-err,
373 String8::format("InterfaceController error: %s", strerror(-err)));
374 }
375 return binder::Status::ok();
376}
377
378binder::Status NetdNativeService::interfaceDelAddress(const std::string &ifName,
379 const std::string &addrString, int prefixLength) {
380 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
381
382 const int err = InterfaceController::delAddress(
383 ifName.c_str(), addrString.c_str(), prefixLength);
384 if (err != 0) {
385 return binder::Status::fromServiceSpecificError(-err,
386 String8::format("InterfaceController error: %s", strerror(-err)));
387 }
388 return binder::Status::ok();
389}
390
Erik Kline55b06f82016-07-04 09:57:18 +0900391binder::Status NetdNativeService::setProcSysNet(
392 int32_t family, int32_t which, const std::string &ifname, const std::string &parameter,
393 const std::string &value) {
394 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
395
396 const char *familyStr;
397 switch (family) {
398 case INetd::IPV4:
399 familyStr = "ipv4";
400 break;
401 case INetd::IPV6:
402 familyStr = "ipv6";
403 break;
404 default:
405 return binder::Status::fromServiceSpecificError(EAFNOSUPPORT, String8("Bad family"));
406 }
407
408 const char *whichStr;
409 switch (which) {
410 case INetd::CONF:
411 whichStr = "conf";
412 break;
413 case INetd::NEIGH:
414 whichStr = "neigh";
415 break;
416 default:
417 return binder::Status::fromServiceSpecificError(EINVAL, String8("Bad category"));
418 }
419
420 const int err = InterfaceController::setParameter(
421 familyStr, whichStr, ifname.c_str(), parameter.c_str(),
422 value.c_str());
423 if (err != 0) {
424 return binder::Status::fromServiceSpecificError(-err,
425 String8::format("ResolverController error: %s", strerror(-err)));
426 }
427 return binder::Status::ok();
428}
429
Robin Lee2cf56172016-09-13 18:55:42 +0900430binder::Status NetdNativeService::getMetricsReportingLevel(int *reportingLevel) {
431 // This function intentionally does not lock, since the only thing it does is one read from an
432 // atomic_int.
433 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
434 ENFORCE_DEBUGGABLE();
435
Michal Karpinskid5440112016-10-06 16:56:04 +0100436 *reportingLevel = gCtls->eventReporter.getMetricsReportingLevel();
Robin Lee2cf56172016-09-13 18:55:42 +0900437 return binder::Status::ok();
438}
439
440binder::Status NetdNativeService::setMetricsReportingLevel(const int reportingLevel) {
441 // This function intentionally does not lock, since the only thing it does is one write to an
442 // atomic_int.
443 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
444 ENFORCE_DEBUGGABLE();
445
Michal Karpinskid5440112016-10-06 16:56:04 +0100446 return (gCtls->eventReporter.setMetricsReportingLevel(reportingLevel) == 0)
447 ? binder::Status::ok()
448 : binder::Status::fromExceptionCode(binder::Status::EX_ILLEGAL_ARGUMENT);
Robin Lee2cf56172016-09-13 18:55:42 +0900449}
450
Benedict Wongb2daefb2017-12-06 22:05:46 -0800451binder::Status NetdNativeService::ipSecSetEncapSocketOwner(const android::base::unique_fd& socket,
452 int newUid) {
453 ENFORCE_PERMISSION(NETWORK_STACK)
454 ALOGD("ipSecSetEncapSocketOwner()");
455
456 uid_t callerUid = IPCThreadState::self()->getCallingUid();
457 return asBinderStatus(gCtls->xfrmCtrl.ipSecSetEncapSocketOwner(socket, newUid, callerUid));
458}
459
460
Nathan Harold1a371532017-01-30 12:30:48 -0800461binder::Status NetdNativeService::ipSecAllocateSpi(
462 int32_t transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800463 const std::string& sourceAddress,
464 const std::string& destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800465 int32_t inSpi,
466 int32_t* outSpi) {
467 // Necessary locking done in IpSecService and kernel
468 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
469 ALOGD("ipSecAllocateSpi()");
ludi6e8eccd2017-08-14 14:40:37 -0700470 return asBinderStatus(gCtls->xfrmCtrl.ipSecAllocateSpi(
Nathan Harold1a371532017-01-30 12:30:48 -0800471 transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800472 sourceAddress,
473 destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800474 inSpi,
475 outSpi));
476}
477
478binder::Status NetdNativeService::ipSecAddSecurityAssociation(
479 int32_t transformId,
480 int32_t mode,
Nathan Haroldda54f122018-01-09 16:42:57 -0800481 const std::string& sourceAddress,
482 const std::string& destinationAddress,
Benedict Wong96abf482018-01-22 13:56:41 -0800483 int32_t underlyingNetId,
Nathan Harold1a371532017-01-30 12:30:48 -0800484 int32_t spi,
Di Lu2ccb3e52018-01-03 16:19:20 -0800485 int32_t markValue,
486 int32_t markMask,
Nathan Harold1a371532017-01-30 12:30:48 -0800487 const std::string& authAlgo, const std::vector<uint8_t>& authKey, int32_t authTruncBits,
488 const std::string& cryptAlgo, const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits,
Benedict Wongbe65b432017-08-22 21:43:14 -0700489 const std::string& aeadAlgo, const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits,
Nathan Harold1a371532017-01-30 12:30:48 -0800490 int32_t encapType,
491 int32_t encapLocalPort,
ludiec836052017-05-20 14:17:05 -0700492 int32_t encapRemotePort) {
Nathan Harold1a371532017-01-30 12:30:48 -0800493 // Necessary locking done in IpSecService and kernel
494 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
495 ALOGD("ipSecAddSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700496 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityAssociation(
Nathan Haroldda54f122018-01-09 16:42:57 -0800497 transformId, mode, sourceAddress, destinationAddress,
Benedict Wong96abf482018-01-22 13:56:41 -0800498 underlyingNetId,
Di Lu2ccb3e52018-01-03 16:19:20 -0800499 spi, markValue, markMask,
Nathan Harold1a371532017-01-30 12:30:48 -0800500 authAlgo, authKey, authTruncBits,
501 cryptAlgo, cryptKey, cryptTruncBits,
Benedict Wongbe65b432017-08-22 21:43:14 -0700502 aeadAlgo, aeadKey, aeadIcvBits,
ludiec836052017-05-20 14:17:05 -0700503 encapType, encapLocalPort, encapRemotePort));
Nathan Harold1a371532017-01-30 12:30:48 -0800504}
505
506binder::Status NetdNativeService::ipSecDeleteSecurityAssociation(
507 int32_t transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800508 const std::string& sourceAddress,
509 const std::string& destinationAddress,
Di Lu2ccb3e52018-01-03 16:19:20 -0800510 int32_t spi,
511 int32_t markValue,
512 int32_t markMask) {
Nathan Harold1a371532017-01-30 12:30:48 -0800513 // Necessary locking done in IpSecService and kernel
514 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
515 ALOGD("ipSecDeleteSecurityAssociation()");
ludi6e8eccd2017-08-14 14:40:37 -0700516 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityAssociation(
Nathan Harold1a371532017-01-30 12:30:48 -0800517 transformId,
Nathan Haroldda54f122018-01-09 16:42:57 -0800518 sourceAddress,
519 destinationAddress,
Di Lu2ccb3e52018-01-03 16:19:20 -0800520 spi,
521 markValue,
522 markMask));
Nathan Harold1a371532017-01-30 12:30:48 -0800523}
524
525binder::Status NetdNativeService::ipSecApplyTransportModeTransform(
526 const android::base::unique_fd& socket,
527 int32_t transformId,
528 int32_t direction,
Nathan Haroldda54f122018-01-09 16:42:57 -0800529 const std::string& sourceAddress,
530 const std::string& destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800531 int32_t spi) {
532 // Necessary locking done in IpSecService and kernel
533 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
534 ALOGD("ipSecApplyTransportModeTransform()");
ludi6e8eccd2017-08-14 14:40:37 -0700535 return asBinderStatus(gCtls->xfrmCtrl.ipSecApplyTransportModeTransform(
Nathan Harold1a371532017-01-30 12:30:48 -0800536 socket,
537 transformId,
538 direction,
Nathan Haroldda54f122018-01-09 16:42:57 -0800539 sourceAddress,
540 destinationAddress,
Nathan Harold1a371532017-01-30 12:30:48 -0800541 spi));
542}
543
544binder::Status NetdNativeService::ipSecRemoveTransportModeTransform(
545 const android::base::unique_fd& socket) {
546 // Necessary locking done in IpSecService and kernel
547 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
548 ALOGD("ipSecRemoveTransportModeTransform()");
ludi6e8eccd2017-08-14 14:40:37 -0700549 return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTransportModeTransform(
Nathan Harold1a371532017-01-30 12:30:48 -0800550 socket));
551}
552
Benedict Wong84a8dca2018-01-19 12:12:17 -0800553binder::Status NetdNativeService::ipSecAddSecurityPolicy(
554 int32_t transformId,
555 int32_t direction,
556 const std::string& sourceAddress,
557 const std::string& destinationAddress,
558 int32_t spi,
559 int32_t markValue,
560 int32_t markMask){
561 // Necessary locking done in IpSecService and kernel
562 ENFORCE_PERMISSION(NETWORK_STACK);
563 ALOGD("ipSecAddSecurityPolicy()");
564 return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityPolicy(
565 transformId,
566 direction,
567 sourceAddress,
568 destinationAddress,
569 spi,
570 markValue,
571 markMask));
572}
573
574binder::Status NetdNativeService::ipSecUpdateSecurityPolicy(
575 int32_t transformId,
576 int32_t direction,
577 const std::string& sourceAddress,
578 const std::string& destinationAddress,
579 int32_t spi,
580 int32_t markValue,
581 int32_t markMask){
582 // Necessary locking done in IpSecService and kernel
583 ENFORCE_PERMISSION(NETWORK_STACK);
584 ALOGD("ipSecAddSecurityPolicy()");
585 return asBinderStatus(gCtls->xfrmCtrl.ipSecUpdateSecurityPolicy(
586 transformId,
587 direction,
588 sourceAddress,
589 destinationAddress,
590 spi,
591 markValue,
592 markMask));
593}
594
595binder::Status NetdNativeService::ipSecDeleteSecurityPolicy(
596 int32_t transformId,
597 int32_t direction,
598 const std::string& sourceAddress,
599 const std::string& destinationAddress,
600 int32_t markValue,
601 int32_t markMask){
602 // Necessary locking done in IpSecService and kernel
603 ENFORCE_PERMISSION(NETWORK_STACK);
604 ALOGD("ipSecAddSecurityPolicy()");
605 return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityPolicy(
606 transformId,
607 direction,
608 sourceAddress,
609 destinationAddress,
610 markValue,
611 markMask));
612}
613
manojboopathi8707f232018-01-02 14:45:47 -0800614binder::Status NetdNativeService::addVirtualTunnelInterface(
615 const std::string& deviceName,
616 const std::string& localAddress,
617 const std::string& remoteAddress,
618 int32_t iKey,
619 int32_t oKey) {
620 // Necessary locking done in IpSecService and kernel
621 ENFORCE_PERMISSION(NETWORK_STACK);
622 ALOGD("addVirtualTunnelInterface()");
623 int ret = gCtls->xfrmCtrl.addVirtualTunnelInterface(
624 deviceName,
625 localAddress,
626 remoteAddress,
627 iKey,
628 oKey,
629 false);
630
631 return (ret == 0) ? binder::Status::ok() :
632 asBinderStatus(netdutils::statusFromErrno(
633 ret, "Error in creating virtual tunnel interface."));
634}
635
636binder::Status NetdNativeService::updateVirtualTunnelInterface(
637 const std::string& deviceName,
638 const std::string& localAddress,
639 const std::string& remoteAddress,
640 int32_t iKey,
641 int32_t oKey) {
642 // Necessary locking done in IpSecService and kernel
643 ENFORCE_PERMISSION(NETWORK_STACK);
644 ALOGD("updateVirtualTunnelInterface()");
645 int ret = gCtls->xfrmCtrl.addVirtualTunnelInterface(
646 deviceName,
647 localAddress,
648 remoteAddress,
649 iKey,
650 oKey,
651 true);
652
653 return (ret == 0) ? binder::Status::ok() :
654 asBinderStatus(netdutils::statusFromErrno(
655 ret, "Error in updating virtual tunnel interface."));
656}
657
658binder::Status NetdNativeService::removeVirtualTunnelInterface(const std::string& deviceName) {
659 // Necessary locking done in IpSecService and kernel
660 ENFORCE_PERMISSION(NETWORK_STACK);
661 ALOGD("removeVirtualTunnelInterface()");
662 int ret = gCtls->xfrmCtrl.removeVirtualTunnelInterface(deviceName);
663
664 return (ret == 0) ? binder::Status::ok() :
665 asBinderStatus(netdutils::statusFromErrno(
666 ret, "Error in removing virtual tunnel interface."));
667}
668
Joel Scherpelzde937962017-06-01 13:20:21 +0900669binder::Status NetdNativeService::setIPv6AddrGenMode(const std::string& ifName,
670 int32_t mode) {
671 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700672 return asBinderStatus(InterfaceController::setIPv6AddrGenMode(ifName, mode));
Joel Scherpelzde937962017-06-01 13:20:21 +0900673}
674
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900675binder::Status NetdNativeService::wakeupAddInterface(const std::string& ifName,
676 const std::string& prefix, int32_t mark,
677 int32_t mask) {
678 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700679 return asBinderStatus(gCtls->wakeupCtrl.addInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900680}
681
682binder::Status NetdNativeService::wakeupDelInterface(const std::string& ifName,
683 const std::string& prefix, int32_t mark,
684 int32_t mask) {
685 ENFORCE_PERMISSION(NETWORK_STACK);
Nathan Harold28ccfef2018-03-16 19:02:47 -0700686 return asBinderStatus(gCtls->wakeupCtrl.delInterface(ifName, prefix, mark, mask));
Joel Scherpelz08b84cd2017-05-22 13:11:54 +0900687}
688
Chenbo Feng07d43fe2017-12-21 14:38:51 -0800689binder::Status NetdNativeService::trafficCheckBpfStatsEnable(bool* ret) {
690 ENFORCE_PERMISSION(NETWORK_STACK);
691 *ret = gCtls->trafficCtrl.checkBpfStatsEnable();
692 return binder::Status::ok();
693}
694
Lorenzo Colittie4d626e2016-02-02 17:19:04 +0900695} // namespace net
696} // namespace android