blob: d4a0e58ec2668a4b18446c27369df1362371452c [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
Devin Moorec304b472022-04-08 21:42:50 +000017#include "NetdHwService.h"
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070018#include <binder/IPCThreadState.h>
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070019#include "Controllers.h"
20#include "Fwmark.h"
Lorenzo Colitti15589f72018-02-07 17:43:31 +090021#include "RouteController.h"
22#include "TetherController.h"
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070023
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070024using android::hardware::Void;
25
Lorenzo Colitti15589f72018-02-07 17:43:31 +090026// Tells TetherController::enableForwarding who is requesting forwarding, so that TetherController
27// can manage/refcount requests to enable forwarding by multiple parties such as the framework, this
28// hwbinder interface, and the legacy "ndc ipfwd enable <requester>" commands.
29namespace {
30constexpr const char* FORWARDING_REQUESTER = "NetdHwService";
31}
32
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070033namespace android {
34namespace net {
35
Lorenzo Colitti15589f72018-02-07 17:43:31 +090036static StatusCode toHalStatus(int ret) {
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070037 switch(ret) {
38 case 0:
Lorenzo Colitti15589f72018-02-07 17:43:31 +090039 return StatusCode::OK;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070040 case -EINVAL:
Lorenzo Colitti15589f72018-02-07 17:43:31 +090041 return StatusCode::INVALID_ARGUMENTS;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070042 case -EEXIST:
Lorenzo Colitti15589f72018-02-07 17:43:31 +090043 return StatusCode::ALREADY_EXISTS;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070044 case -ENONET:
Lorenzo Colitti15589f72018-02-07 17:43:31 +090045 return StatusCode::NO_NETWORK;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070046 case -EPERM:
Lorenzo Colitti15589f72018-02-07 17:43:31 +090047 return StatusCode::PERMISSION_DENIED;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070048 default:
49 ALOGE("HAL service error=%d", ret);
Lorenzo Colitti15589f72018-02-07 17:43:31 +090050 return StatusCode::UNKNOWN_ERROR;
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070051 }
52}
53
54// Minimal service start.
55status_t NetdHwService::start() {
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070056 // Register hardware service with ServiceManager.
57 return INetd::registerAsService();
58}
59
60Return<void> NetdHwService::createOemNetwork(createOemNetwork_cb _hidl_cb) {
61 unsigned netId;
62 Permission permission = PERMISSION_SYSTEM;
63
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070064 int ret = gCtls->netCtrl.createPhysicalOemNetwork(permission, &netId);
65
66 Fwmark fwmark;
67 fwmark.netId = netId;
68 fwmark.explicitlySelected = true;
69 fwmark.protectedFromVpn = true;
70 fwmark.permission = PERMISSION_SYSTEM;
71 _hidl_cb(netIdToNetHandle(netId), fwmark.intValue, toHalStatus(ret));
72
73 return Void();
74}
75
Lorenzo Colitti15589f72018-02-07 17:43:31 +090076// Vendor code can only modify OEM networks. All other networks are managed by ConnectivityService.
77#define RETURN_IF_NOT_OEM_NETWORK(netId) \
78 if (((netId) < NetworkController::MIN_OEM_ID) || \
79 ((netId) > NetworkController::MAX_OEM_ID)) { \
80 return StatusCode::INVALID_ARGUMENTS; \
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070081 }
82
Lorenzo Colitti15589f72018-02-07 17:43:31 +090083Return<StatusCode> NetdHwService::destroyOemNetwork(uint64_t netHandle) {
84 unsigned netId = netHandleToNetId(netHandle);
85 RETURN_IF_NOT_OEM_NETWORK(netId);
86
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -070087 return toHalStatus(gCtls->netCtrl.destroyNetwork(netId));
88}
89
Lorenzo Colitti15589f72018-02-07 17:43:31 +090090const char* maybeNullString(const hidl_string& nexthop) {
91 // HIDL strings can't be null, but RouteController wants null instead of an empty string.
92 const char* nh = nexthop.c_str();
93 if (nh && !*nh) {
94 nh = nullptr;
95 }
96 return nh;
97}
98
99Return <StatusCode> NetdHwService::addRouteToOemNetwork(
100 uint64_t networkHandle, const hidl_string& ifname, const hidl_string& destination,
101 const hidl_string& nexthop) {
102 unsigned netId = netHandleToNetId(networkHandle);
103 RETURN_IF_NOT_OEM_NETWORK(netId);
104
105 return toHalStatus(gCtls->netCtrl.addRoute(netId, ifname.c_str(), destination.c_str(),
Tyler Wearfa94a272019-12-05 15:01:48 -0800106 maybeNullString(nexthop), false, INVALID_UID,
107 0 /* mtu */));
Lorenzo Colitti15589f72018-02-07 17:43:31 +0900108}
109
110Return <StatusCode> NetdHwService::removeRouteFromOemNetwork(
111 uint64_t networkHandle, const hidl_string& ifname, const hidl_string& destination,
112 const hidl_string& nexthop) {
113 unsigned netId = netHandleToNetId(networkHandle);
114 RETURN_IF_NOT_OEM_NETWORK(netId);
115
116 return toHalStatus(gCtls->netCtrl.removeRoute(netId, ifname.c_str(), destination.c_str(),
117 maybeNullString(nexthop), false, INVALID_UID));
118}
119
120Return <StatusCode> NetdHwService::addInterfaceToOemNetwork(uint64_t networkHandle,
121 const hidl_string& ifname) {
122 unsigned netId = netHandleToNetId(networkHandle);
123 RETURN_IF_NOT_OEM_NETWORK(netId);
124
125 return toHalStatus(gCtls->netCtrl.addInterfaceToNetwork(netId, ifname.c_str()));
126}
127
128Return <StatusCode> NetdHwService::removeInterfaceFromOemNetwork(uint64_t networkHandle,
129 const hidl_string& ifname) {
130 unsigned netId = netHandleToNetId(networkHandle);
131 RETURN_IF_NOT_OEM_NETWORK(netId);
132
133 return toHalStatus(gCtls->netCtrl.removeInterfaceFromNetwork(netId, ifname.c_str()));
134}
135
136Return <StatusCode> NetdHwService::setIpForwardEnable(bool enable) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900137 std::lock_guard _lock(gCtls->tetherCtrl.lock);
Lorenzo Colitti15589f72018-02-07 17:43:31 +0900138
139 bool success = enable ? gCtls->tetherCtrl.enableForwarding(FORWARDING_REQUESTER) :
140 gCtls->tetherCtrl.disableForwarding(FORWARDING_REQUESTER);
141
142 return success ? StatusCode::OK : StatusCode::UNKNOWN_ERROR;
143}
144
145Return <StatusCode> NetdHwService::setForwardingBetweenInterfaces(
146 const hidl_string& inputIfName, const hidl_string& outputIfName, bool enable) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900147 std::lock_guard _lock(gCtls->tetherCtrl.lock);
Lorenzo Colitti15589f72018-02-07 17:43:31 +0900148
149 // TODO: check that one interface is an OEM interface and the other is another OEM interface, an
150 // IPsec interface or a dummy interface.
151 int ret = enable ? RouteController::enableTethering(inputIfName.c_str(), outputIfName.c_str()) :
152 RouteController::disableTethering(inputIfName.c_str(), outputIfName.c_str());
153 return toHalStatus(ret);
154}
155
Niranjan Pendharkar7e08f852017-07-24 11:40:05 -0700156} // namespace net
157} // namespace android