blob: b209aca6cc206cfbe6ba74ea2b738b838ac7b343 [file] [log] [blame]
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -07001/*
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>
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070019#include "Controllers.h"
20#include "Fwmark.h"
21#include "NetdHwService.h"
Lorenzo Colitti15589f72018-02-07 17:43:31 +090022#include "RouteController.h"
23#include "TetherController.h"
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070024
25using android::hardware::configureRpcThreadpool;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070026using android::hardware::Void;
27
Lorenzo Colitti15589f72018-02-07 17:43:31 +090028// Tells TetherController::enableForwarding who is requesting forwarding, so that TetherController
29// can manage/refcount requests to enable forwarding by multiple parties such as the framework, this
30// hwbinder interface, and the legacy "ndc ipfwd enable <requester>" commands.
31namespace {
32constexpr const char* FORWARDING_REQUESTER = "NetdHwService";
33}
34
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070035namespace android {
36namespace net {
37
Lorenzo Colitti15589f72018-02-07 17:43:31 +090038static StatusCode toHalStatus(int ret) {
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070039 switch(ret) {
40 case 0:
Lorenzo Colitti15589f72018-02-07 17:43:31 +090041 return StatusCode::OK;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070042 case -EINVAL:
Lorenzo Colitti15589f72018-02-07 17:43:31 +090043 return StatusCode::INVALID_ARGUMENTS;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070044 case -EEXIST:
Lorenzo Colitti15589f72018-02-07 17:43:31 +090045 return StatusCode::ALREADY_EXISTS;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070046 case -ENONET:
Lorenzo Colitti15589f72018-02-07 17:43:31 +090047 return StatusCode::NO_NETWORK;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070048 case -EPERM:
Lorenzo Colitti15589f72018-02-07 17:43:31 +090049 return StatusCode::PERMISSION_DENIED;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070050 default:
51 ALOGE("HAL service error=%d", ret);
Lorenzo Colitti15589f72018-02-07 17:43:31 +090052 return StatusCode::UNKNOWN_ERROR;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070053 }
54}
55
56// Minimal service start.
57status_t NetdHwService::start() {
58 IPCThreadState::self()->disableBackgroundScheduling(true);
59 // Usage of this HAL is anticipated to be thin; one thread should suffice.
60 configureRpcThreadpool(1, false /* callerWillNotJoin */);
61 // Register hardware service with ServiceManager.
62 return INetd::registerAsService();
63}
64
65Return<void> NetdHwService::createOemNetwork(createOemNetwork_cb _hidl_cb) {
66 unsigned netId;
67 Permission permission = PERMISSION_SYSTEM;
68
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070069 int ret = gCtls->netCtrl.createPhysicalOemNetwork(permission, &netId);
70
71 Fwmark fwmark;
72 fwmark.netId = netId;
73 fwmark.explicitlySelected = true;
74 fwmark.protectedFromVpn = true;
75 fwmark.permission = PERMISSION_SYSTEM;
76 _hidl_cb(netIdToNetHandle(netId), fwmark.intValue, toHalStatus(ret));
77
78 return Void();
79}
80
Lorenzo Colitti15589f72018-02-07 17:43:31 +090081// Vendor code can only modify OEM networks. All other networks are managed by ConnectivityService.
82#define RETURN_IF_NOT_OEM_NETWORK(netId) \
83 if (((netId) < NetworkController::MIN_OEM_ID) || \
84 ((netId) > NetworkController::MAX_OEM_ID)) { \
85 return StatusCode::INVALID_ARGUMENTS; \
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070086 }
87
Lorenzo Colitti15589f72018-02-07 17:43:31 +090088Return<StatusCode> NetdHwService::destroyOemNetwork(uint64_t netHandle) {
89 unsigned netId = netHandleToNetId(netHandle);
90 RETURN_IF_NOT_OEM_NETWORK(netId);
91
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070092 return toHalStatus(gCtls->netCtrl.destroyNetwork(netId));
93}
94
Lorenzo Colitti15589f72018-02-07 17:43:31 +090095const char* maybeNullString(const hidl_string& nexthop) {
96 // HIDL strings can't be null, but RouteController wants null instead of an empty string.
97 const char* nh = nexthop.c_str();
98 if (nh && !*nh) {
99 nh = nullptr;
100 }
101 return nh;
102}
103
104Return <StatusCode> NetdHwService::addRouteToOemNetwork(
105 uint64_t networkHandle, const hidl_string& ifname, const hidl_string& destination,
106 const hidl_string& nexthop) {
107 unsigned netId = netHandleToNetId(networkHandle);
108 RETURN_IF_NOT_OEM_NETWORK(netId);
109
110 return toHalStatus(gCtls->netCtrl.addRoute(netId, ifname.c_str(), destination.c_str(),
111 maybeNullString(nexthop), false, INVALID_UID));
112}
113
114Return <StatusCode> NetdHwService::removeRouteFromOemNetwork(
115 uint64_t networkHandle, const hidl_string& ifname, const hidl_string& destination,
116 const hidl_string& nexthop) {
117 unsigned netId = netHandleToNetId(networkHandle);
118 RETURN_IF_NOT_OEM_NETWORK(netId);
119
120 return toHalStatus(gCtls->netCtrl.removeRoute(netId, ifname.c_str(), destination.c_str(),
121 maybeNullString(nexthop), false, INVALID_UID));
122}
123
124Return <StatusCode> NetdHwService::addInterfaceToOemNetwork(uint64_t networkHandle,
125 const hidl_string& ifname) {
126 unsigned netId = netHandleToNetId(networkHandle);
127 RETURN_IF_NOT_OEM_NETWORK(netId);
128
129 return toHalStatus(gCtls->netCtrl.addInterfaceToNetwork(netId, ifname.c_str()));
130}
131
132Return <StatusCode> NetdHwService::removeInterfaceFromOemNetwork(uint64_t networkHandle,
133 const hidl_string& ifname) {
134 unsigned netId = netHandleToNetId(networkHandle);
135 RETURN_IF_NOT_OEM_NETWORK(netId);
136
137 return toHalStatus(gCtls->netCtrl.removeInterfaceFromNetwork(netId, ifname.c_str()));
138}
139
140Return <StatusCode> NetdHwService::setIpForwardEnable(bool enable) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900141 std::lock_guard _lock(gCtls->tetherCtrl.lock);
Lorenzo Colitti15589f72018-02-07 17:43:31 +0900142
143 bool success = enable ? gCtls->tetherCtrl.enableForwarding(FORWARDING_REQUESTER) :
144 gCtls->tetherCtrl.disableForwarding(FORWARDING_REQUESTER);
145
146 return success ? StatusCode::OK : StatusCode::UNKNOWN_ERROR;
147}
148
149Return <StatusCode> NetdHwService::setForwardingBetweenInterfaces(
150 const hidl_string& inputIfName, const hidl_string& outputIfName, bool enable) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900151 std::lock_guard _lock(gCtls->tetherCtrl.lock);
Lorenzo Colitti15589f72018-02-07 17:43:31 +0900152
153 // TODO: check that one interface is an OEM interface and the other is another OEM interface, an
154 // IPsec interface or a dummy interface.
155 int ret = enable ? RouteController::enableTethering(inputIfName.c_str(), outputIfName.c_str()) :
156 RouteController::disableTethering(inputIfName.c_str(), outputIfName.c_str());
157 return toHalStatus(ret);
158}
159
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700160} // namespace net
161} // namespace android