1*8542734aSAndroid Build Coastguard Worker /*
2*8542734aSAndroid Build Coastguard Worker * Copyright (C) 2022 The Android Open Source Project
3*8542734aSAndroid Build Coastguard Worker *
4*8542734aSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8542734aSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8542734aSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8542734aSAndroid Build Coastguard Worker *
8*8542734aSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8542734aSAndroid Build Coastguard Worker *
10*8542734aSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8542734aSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8542734aSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8542734aSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8542734aSAndroid Build Coastguard Worker * limitations under the License.
15*8542734aSAndroid Build Coastguard Worker */
16*8542734aSAndroid Build Coastguard Worker
17*8542734aSAndroid Build Coastguard Worker #include "NetdHwAidlService.h"
18*8542734aSAndroid Build Coastguard Worker #include <android/binder_manager.h>
19*8542734aSAndroid Build Coastguard Worker #include <android/binder_process.h>
20*8542734aSAndroid Build Coastguard Worker #include "Controllers.h"
21*8542734aSAndroid Build Coastguard Worker #include "Fwmark.h"
22*8542734aSAndroid Build Coastguard Worker #include "RouteController.h"
23*8542734aSAndroid Build Coastguard Worker #include "TetherController.h"
24*8542734aSAndroid Build Coastguard Worker
25*8542734aSAndroid Build Coastguard Worker // Tells TetherController::enableForwarding who is requesting forwarding, so that TetherController
26*8542734aSAndroid Build Coastguard Worker // can manage/refcount requests to enable forwarding by multiple parties such as the framework, this
27*8542734aSAndroid Build Coastguard Worker // binder interface, and the legacy "ndc ipfwd enable <requester>" commands.
28*8542734aSAndroid Build Coastguard Worker namespace {
29*8542734aSAndroid Build Coastguard Worker constexpr const char* FORWARDING_REQUESTER = "NetdHwAidlService";
30*8542734aSAndroid Build Coastguard Worker }
31*8542734aSAndroid Build Coastguard Worker
32*8542734aSAndroid Build Coastguard Worker namespace android {
33*8542734aSAndroid Build Coastguard Worker namespace net {
34*8542734aSAndroid Build Coastguard Worker namespace aidl {
35*8542734aSAndroid Build Coastguard Worker
toHalStatus(int ret)36*8542734aSAndroid Build Coastguard Worker static int toHalStatus(int ret) {
37*8542734aSAndroid Build Coastguard Worker switch (ret) {
38*8542734aSAndroid Build Coastguard Worker case 0:
39*8542734aSAndroid Build Coastguard Worker return 0;
40*8542734aSAndroid Build Coastguard Worker case -EINVAL:
41*8542734aSAndroid Build Coastguard Worker return NetdHwAidlService::STATUS_INVALID_ARGUMENTS;
42*8542734aSAndroid Build Coastguard Worker case -EEXIST:
43*8542734aSAndroid Build Coastguard Worker return NetdHwAidlService::STATUS_ALREADY_EXISTS;
44*8542734aSAndroid Build Coastguard Worker case -ENONET:
45*8542734aSAndroid Build Coastguard Worker return NetdHwAidlService::STATUS_NO_NETWORK;
46*8542734aSAndroid Build Coastguard Worker case -EPERM:
47*8542734aSAndroid Build Coastguard Worker return NetdHwAidlService::STATUS_PERMISSION_DENIED;
48*8542734aSAndroid Build Coastguard Worker default:
49*8542734aSAndroid Build Coastguard Worker ALOGE("HAL service error=%d", ret);
50*8542734aSAndroid Build Coastguard Worker return NetdHwAidlService::STATUS_UNKNOWN_ERROR;
51*8542734aSAndroid Build Coastguard Worker }
52*8542734aSAndroid Build Coastguard Worker }
53*8542734aSAndroid Build Coastguard Worker
run()54*8542734aSAndroid Build Coastguard Worker void NetdHwAidlService::run() {
55*8542734aSAndroid Build Coastguard Worker std::shared_ptr<NetdHwAidlService> service = ndk::SharedRefBase::make<NetdHwAidlService>();
56*8542734aSAndroid Build Coastguard Worker
57*8542734aSAndroid Build Coastguard Worker const std::string instance = std::string() + NetdHwAidlService::descriptor + "/default";
58*8542734aSAndroid Build Coastguard Worker binder_status_t status =
59*8542734aSAndroid Build Coastguard Worker AServiceManager_addService(service->asBinder().get(), instance.c_str());
60*8542734aSAndroid Build Coastguard Worker if (status != STATUS_OK) {
61*8542734aSAndroid Build Coastguard Worker ALOGE("Failed to register AIDL INetd service. Status: %d.", status);
62*8542734aSAndroid Build Coastguard Worker return;
63*8542734aSAndroid Build Coastguard Worker }
64*8542734aSAndroid Build Coastguard Worker
65*8542734aSAndroid Build Coastguard Worker ABinderProcess_joinThreadPool();
66*8542734aSAndroid Build Coastguard Worker }
67*8542734aSAndroid Build Coastguard Worker
createOemNetwork(OemNetwork * network)68*8542734aSAndroid Build Coastguard Worker ScopedAStatus NetdHwAidlService::createOemNetwork(OemNetwork* network) {
69*8542734aSAndroid Build Coastguard Worker unsigned netId;
70*8542734aSAndroid Build Coastguard Worker Permission permission = PERMISSION_SYSTEM;
71*8542734aSAndroid Build Coastguard Worker
72*8542734aSAndroid Build Coastguard Worker int ret = gCtls->netCtrl.createPhysicalOemNetwork(permission, &netId);
73*8542734aSAndroid Build Coastguard Worker
74*8542734aSAndroid Build Coastguard Worker Fwmark fwmark;
75*8542734aSAndroid Build Coastguard Worker fwmark.netId = netId;
76*8542734aSAndroid Build Coastguard Worker fwmark.explicitlySelected = true;
77*8542734aSAndroid Build Coastguard Worker fwmark.protectedFromVpn = true;
78*8542734aSAndroid Build Coastguard Worker fwmark.permission = PERMISSION_SYSTEM;
79*8542734aSAndroid Build Coastguard Worker network->networkHandle = netIdToNetHandle(netId);
80*8542734aSAndroid Build Coastguard Worker network->packetMark = fwmark.intValue;
81*8542734aSAndroid Build Coastguard Worker if (ret != 0) {
82*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::fromServiceSpecificError(toHalStatus(ret));
83*8542734aSAndroid Build Coastguard Worker } else {
84*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::ok();
85*8542734aSAndroid Build Coastguard Worker }
86*8542734aSAndroid Build Coastguard Worker }
87*8542734aSAndroid Build Coastguard Worker
88*8542734aSAndroid Build Coastguard Worker // Vendor code can only modify OEM networks. All other networks are managed by ConnectivityService.
89*8542734aSAndroid Build Coastguard Worker #define RETURN_IF_NOT_OEM_NETWORK(netId) \
90*8542734aSAndroid Build Coastguard Worker if (((netId) < NetworkController::MIN_OEM_ID) || ((netId) > NetworkController::MAX_OEM_ID)) { \
91*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_ARGUMENTS); \
92*8542734aSAndroid Build Coastguard Worker }
93*8542734aSAndroid Build Coastguard Worker
destroyOemNetwork(int64_t netHandle)94*8542734aSAndroid Build Coastguard Worker ScopedAStatus NetdHwAidlService::destroyOemNetwork(int64_t netHandle) {
95*8542734aSAndroid Build Coastguard Worker unsigned netId = netHandleToNetId(netHandle);
96*8542734aSAndroid Build Coastguard Worker RETURN_IF_NOT_OEM_NETWORK(netId);
97*8542734aSAndroid Build Coastguard Worker
98*8542734aSAndroid Build Coastguard Worker auto ret = toHalStatus(gCtls->netCtrl.destroyNetwork(netId));
99*8542734aSAndroid Build Coastguard Worker if (ret != 0) {
100*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::fromServiceSpecificError(toHalStatus(ret));
101*8542734aSAndroid Build Coastguard Worker } else {
102*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::ok();
103*8542734aSAndroid Build Coastguard Worker }
104*8542734aSAndroid Build Coastguard Worker }
105*8542734aSAndroid Build Coastguard Worker
maybeNullString(const std::string & nexthop)106*8542734aSAndroid Build Coastguard Worker const char* maybeNullString(const std::string& nexthop) {
107*8542734aSAndroid Build Coastguard Worker // std::strings can't be null, but RouteController wants null instead of an empty string.
108*8542734aSAndroid Build Coastguard Worker const char* nh = nexthop.c_str();
109*8542734aSAndroid Build Coastguard Worker if (nh && !*nh) {
110*8542734aSAndroid Build Coastguard Worker nh = nullptr;
111*8542734aSAndroid Build Coastguard Worker }
112*8542734aSAndroid Build Coastguard Worker return nh;
113*8542734aSAndroid Build Coastguard Worker }
114*8542734aSAndroid Build Coastguard Worker
addRouteToOemNetwork(int64_t networkHandle,const std::string & ifname,const std::string & destination,const std::string & nexthop)115*8542734aSAndroid Build Coastguard Worker ScopedAStatus NetdHwAidlService::addRouteToOemNetwork(int64_t networkHandle,
116*8542734aSAndroid Build Coastguard Worker const std::string& ifname,
117*8542734aSAndroid Build Coastguard Worker const std::string& destination,
118*8542734aSAndroid Build Coastguard Worker const std::string& nexthop) {
119*8542734aSAndroid Build Coastguard Worker unsigned netId = netHandleToNetId(networkHandle);
120*8542734aSAndroid Build Coastguard Worker RETURN_IF_NOT_OEM_NETWORK(netId);
121*8542734aSAndroid Build Coastguard Worker
122*8542734aSAndroid Build Coastguard Worker auto ret = gCtls->netCtrl.addRoute(netId, ifname.c_str(), destination.c_str(),
123*8542734aSAndroid Build Coastguard Worker maybeNullString(nexthop), false, INVALID_UID, 0 /* mtu */);
124*8542734aSAndroid Build Coastguard Worker if (ret != 0) {
125*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::fromServiceSpecificError(toHalStatus(ret));
126*8542734aSAndroid Build Coastguard Worker } else {
127*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::ok();
128*8542734aSAndroid Build Coastguard Worker }
129*8542734aSAndroid Build Coastguard Worker }
130*8542734aSAndroid Build Coastguard Worker
removeRouteFromOemNetwork(int64_t networkHandle,const std::string & ifname,const std::string & destination,const std::string & nexthop)131*8542734aSAndroid Build Coastguard Worker ScopedAStatus NetdHwAidlService::removeRouteFromOemNetwork(int64_t networkHandle,
132*8542734aSAndroid Build Coastguard Worker const std::string& ifname,
133*8542734aSAndroid Build Coastguard Worker const std::string& destination,
134*8542734aSAndroid Build Coastguard Worker const std::string& nexthop) {
135*8542734aSAndroid Build Coastguard Worker unsigned netId = netHandleToNetId(networkHandle);
136*8542734aSAndroid Build Coastguard Worker RETURN_IF_NOT_OEM_NETWORK(netId);
137*8542734aSAndroid Build Coastguard Worker
138*8542734aSAndroid Build Coastguard Worker auto ret = gCtls->netCtrl.removeRoute(netId, ifname.c_str(), destination.c_str(),
139*8542734aSAndroid Build Coastguard Worker maybeNullString(nexthop), false, INVALID_UID);
140*8542734aSAndroid Build Coastguard Worker if (ret != 0) {
141*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::fromServiceSpecificError(toHalStatus(ret));
142*8542734aSAndroid Build Coastguard Worker } else {
143*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::ok();
144*8542734aSAndroid Build Coastguard Worker }
145*8542734aSAndroid Build Coastguard Worker }
146*8542734aSAndroid Build Coastguard Worker
addInterfaceToOemNetwork(int64_t networkHandle,const std::string & ifname)147*8542734aSAndroid Build Coastguard Worker ScopedAStatus NetdHwAidlService::addInterfaceToOemNetwork(int64_t networkHandle,
148*8542734aSAndroid Build Coastguard Worker const std::string& ifname) {
149*8542734aSAndroid Build Coastguard Worker unsigned netId = netHandleToNetId(networkHandle);
150*8542734aSAndroid Build Coastguard Worker RETURN_IF_NOT_OEM_NETWORK(netId);
151*8542734aSAndroid Build Coastguard Worker
152*8542734aSAndroid Build Coastguard Worker auto ret = gCtls->netCtrl.addInterfaceToNetwork(netId, ifname.c_str());
153*8542734aSAndroid Build Coastguard Worker if (ret != 0) {
154*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::fromServiceSpecificError(toHalStatus(ret));
155*8542734aSAndroid Build Coastguard Worker } else {
156*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::ok();
157*8542734aSAndroid Build Coastguard Worker }
158*8542734aSAndroid Build Coastguard Worker }
159*8542734aSAndroid Build Coastguard Worker
removeInterfaceFromOemNetwork(int64_t networkHandle,const std::string & ifname)160*8542734aSAndroid Build Coastguard Worker ScopedAStatus NetdHwAidlService::removeInterfaceFromOemNetwork(int64_t networkHandle,
161*8542734aSAndroid Build Coastguard Worker const std::string& ifname) {
162*8542734aSAndroid Build Coastguard Worker unsigned netId = netHandleToNetId(networkHandle);
163*8542734aSAndroid Build Coastguard Worker RETURN_IF_NOT_OEM_NETWORK(netId);
164*8542734aSAndroid Build Coastguard Worker
165*8542734aSAndroid Build Coastguard Worker auto ret = gCtls->netCtrl.removeInterfaceFromNetwork(netId, ifname.c_str());
166*8542734aSAndroid Build Coastguard Worker if (ret != 0) {
167*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::fromServiceSpecificError(toHalStatus(ret));
168*8542734aSAndroid Build Coastguard Worker } else {
169*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::ok();
170*8542734aSAndroid Build Coastguard Worker }
171*8542734aSAndroid Build Coastguard Worker }
172*8542734aSAndroid Build Coastguard Worker
setIpForwardEnable(bool enable)173*8542734aSAndroid Build Coastguard Worker ScopedAStatus NetdHwAidlService::setIpForwardEnable(bool enable) {
174*8542734aSAndroid Build Coastguard Worker std::lock_guard _lock(gCtls->tetherCtrl.lock);
175*8542734aSAndroid Build Coastguard Worker
176*8542734aSAndroid Build Coastguard Worker bool success = enable ? gCtls->tetherCtrl.enableForwarding(FORWARDING_REQUESTER)
177*8542734aSAndroid Build Coastguard Worker : gCtls->tetherCtrl.disableForwarding(FORWARDING_REQUESTER);
178*8542734aSAndroid Build Coastguard Worker
179*8542734aSAndroid Build Coastguard Worker if (!success) {
180*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::fromServiceSpecificError(STATUS_UNKNOWN_ERROR);
181*8542734aSAndroid Build Coastguard Worker } else {
182*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::ok();
183*8542734aSAndroid Build Coastguard Worker }
184*8542734aSAndroid Build Coastguard Worker }
185*8542734aSAndroid Build Coastguard Worker
setForwardingBetweenInterfaces(const std::string & inputIfName,const std::string & outputIfName,bool enable)186*8542734aSAndroid Build Coastguard Worker ScopedAStatus NetdHwAidlService::setForwardingBetweenInterfaces(const std::string& inputIfName,
187*8542734aSAndroid Build Coastguard Worker const std::string& outputIfName,
188*8542734aSAndroid Build Coastguard Worker bool enable) {
189*8542734aSAndroid Build Coastguard Worker std::lock_guard _lock(gCtls->tetherCtrl.lock);
190*8542734aSAndroid Build Coastguard Worker
191*8542734aSAndroid Build Coastguard Worker // TODO: check that one interface is an OEM interface and the other is another OEM interface, an
192*8542734aSAndroid Build Coastguard Worker // IPsec interface or a dummy interface.
193*8542734aSAndroid Build Coastguard Worker int ret = enable ? RouteController::enableTethering(inputIfName.c_str(), outputIfName.c_str())
194*8542734aSAndroid Build Coastguard Worker : RouteController::disableTethering(inputIfName.c_str(), outputIfName.c_str());
195*8542734aSAndroid Build Coastguard Worker if (ret != 0) {
196*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::fromServiceSpecificError(toHalStatus(ret));
197*8542734aSAndroid Build Coastguard Worker } else {
198*8542734aSAndroid Build Coastguard Worker return ScopedAStatus::ok();
199*8542734aSAndroid Build Coastguard Worker }
200*8542734aSAndroid Build Coastguard Worker }
201*8542734aSAndroid Build Coastguard Worker
202*8542734aSAndroid Build Coastguard Worker } // namespace aidl
203*8542734aSAndroid Build Coastguard Worker } // namespace net
204*8542734aSAndroid Build Coastguard Worker } // namespace android
205