1 /*
2 * Copyright (C) 2017 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 #include <binder/IPCThreadState.h>
18 #include <hidl/HidlTransportSupport.h>
19 #include <hwbinder/IPCThreadState.h>
20 #include "Controllers.h"
21 #include "Fwmark.h"
22 #include "NetdHwService.h"
23
24 using android::hardware::configureRpcThreadpool;
25 using android::hardware::IPCThreadState;
26 using android::hardware::Void;
27
28 namespace android {
29 namespace net {
30
31 /**
32 * This lock exists to make NetdHwService RPCs (which come in on multiple HwBinder threads)
33 * coexist with the commands in CommandListener.cpp. These are presumed not thread-safe because
34 * CommandListener has only one user (NetworkManagementService), which is connected through a
35 * FrameworkListener that passes in commands one at a time.
36 */
37 extern android::RWLock gBigNetdLock;
38
toHalStatus(int ret)39 static INetd::StatusCode toHalStatus(int ret) {
40 switch(ret) {
41 case 0:
42 return INetd::StatusCode::OK;
43 case -EINVAL:
44 return INetd::StatusCode::INVALID_ARGUMENTS;
45 case -EEXIST:
46 return INetd::StatusCode::ALREADY_EXISTS;
47 case -ENONET:
48 return INetd::StatusCode::NO_NETWORK;
49 case -EPERM:
50 return INetd::StatusCode::PERMISSION_DENIED;
51 default:
52 ALOGE("HAL service error=%d", ret);
53 return INetd::StatusCode::UNKNOWN_ERROR;
54 }
55 }
56
57 // Minimal service start.
start()58 status_t NetdHwService::start() {
59 IPCThreadState::self()->disableBackgroundScheduling(true);
60 // Usage of this HAL is anticipated to be thin; one thread should suffice.
61 configureRpcThreadpool(1, false /* callerWillNotJoin */);
62 // Register hardware service with ServiceManager.
63 return INetd::registerAsService();
64 }
65
createOemNetwork(createOemNetwork_cb _hidl_cb)66 Return<void> NetdHwService::createOemNetwork(createOemNetwork_cb _hidl_cb) {
67 unsigned netId;
68 Permission permission = PERMISSION_SYSTEM;
69
70 android::RWLock::AutoWLock _lock(gBigNetdLock);
71 int ret = gCtls->netCtrl.createPhysicalOemNetwork(permission, &netId);
72
73 Fwmark fwmark;
74 fwmark.netId = netId;
75 fwmark.explicitlySelected = true;
76 fwmark.protectedFromVpn = true;
77 fwmark.permission = PERMISSION_SYSTEM;
78 _hidl_cb(netIdToNetHandle(netId), fwmark.intValue, toHalStatus(ret));
79
80 return Void();
81 }
82
destroyOemNetwork(uint64_t netHandle)83 Return<INetd::StatusCode> NetdHwService::destroyOemNetwork(uint64_t netHandle) {
84 unsigned netId = netHandleToNetId(netHandle);
85 if ((netId < NetworkController::MIN_OEM_ID) ||
86 (netId > NetworkController::MAX_OEM_ID)) {
87 return INetd::StatusCode::INVALID_ARGUMENTS;
88 }
89
90 android::RWLock::AutoWLock _lock(gBigNetdLock);
91
92 return toHalStatus(gCtls->netCtrl.destroyNetwork(netId));
93 }
94
95 } // namespace net
96 } // namespace android
97