blob: 15855da8319be25e46dacd589a8e46dc8a82942b [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(),
Tyler Wearfa94a272019-12-05 15:01:48 -0800111 maybeNullString(nexthop), false, INVALID_UID,
112 0 /* mtu */));
Lorenzo Colitti15589f72018-02-07 17:43:31 +0900113}
114
115Return <StatusCode> NetdHwService::removeRouteFromOemNetwork(
116 uint64_t networkHandle, const hidl_string& ifname, const hidl_string& destination,
117 const hidl_string& nexthop) {
118 unsigned netId = netHandleToNetId(networkHandle);
119 RETURN_IF_NOT_OEM_NETWORK(netId);
120
121 return toHalStatus(gCtls->netCtrl.removeRoute(netId, ifname.c_str(), destination.c_str(),
122 maybeNullString(nexthop), false, INVALID_UID));
123}
124
125Return <StatusCode> NetdHwService::addInterfaceToOemNetwork(uint64_t networkHandle,
126 const hidl_string& ifname) {
127 unsigned netId = netHandleToNetId(networkHandle);
128 RETURN_IF_NOT_OEM_NETWORK(netId);
129
130 return toHalStatus(gCtls->netCtrl.addInterfaceToNetwork(netId, ifname.c_str()));
131}
132
133Return <StatusCode> NetdHwService::removeInterfaceFromOemNetwork(uint64_t networkHandle,
134 const hidl_string& ifname) {
135 unsigned netId = netHandleToNetId(networkHandle);
136 RETURN_IF_NOT_OEM_NETWORK(netId);
137
138 return toHalStatus(gCtls->netCtrl.removeInterfaceFromNetwork(netId, ifname.c_str()));
139}
140
141Return <StatusCode> NetdHwService::setIpForwardEnable(bool enable) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900142 std::lock_guard _lock(gCtls->tetherCtrl.lock);
Lorenzo Colitti15589f72018-02-07 17:43:31 +0900143
144 bool success = enable ? gCtls->tetherCtrl.enableForwarding(FORWARDING_REQUESTER) :
145 gCtls->tetherCtrl.disableForwarding(FORWARDING_REQUESTER);
146
147 return success ? StatusCode::OK : StatusCode::UNKNOWN_ERROR;
148}
149
150Return <StatusCode> NetdHwService::setForwardingBetweenInterfaces(
151 const hidl_string& inputIfName, const hidl_string& outputIfName, bool enable) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900152 std::lock_guard _lock(gCtls->tetherCtrl.lock);
Lorenzo Colitti15589f72018-02-07 17:43:31 +0900153
154 // TODO: check that one interface is an OEM interface and the other is another OEM interface, an
155 // IPsec interface or a dummy interface.
156 int ret = enable ? RouteController::enableTethering(inputIfName.c_str(), outputIfName.c_str()) :
157 RouteController::disableTethering(inputIfName.c_str(), outputIfName.c_str());
158 return toHalStatus(ret);
159}
160
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700161} // namespace net
162} // namespace android