• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "NetdHwService.h"
18 #include <binder/IPCThreadState.h>
19 #include "Controllers.h"
20 #include "Fwmark.h"
21 #include "RouteController.h"
22 #include "TetherController.h"
23 
24 using android::hardware::Void;
25 
26 // 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.
29 namespace {
30 constexpr const char* FORWARDING_REQUESTER = "NetdHwService";
31 }
32 
33 namespace android {
34 namespace net {
35 
toHalStatus(int ret)36 static StatusCode toHalStatus(int ret) {
37     switch(ret) {
38         case 0:
39             return StatusCode::OK;
40         case -EINVAL:
41             return StatusCode::INVALID_ARGUMENTS;
42         case -EEXIST:
43             return StatusCode::ALREADY_EXISTS;
44         case -ENONET:
45             return StatusCode::NO_NETWORK;
46         case -EPERM:
47             return StatusCode::PERMISSION_DENIED;
48         default:
49             ALOGE("HAL service error=%d", ret);
50             return StatusCode::UNKNOWN_ERROR;
51     }
52 }
53 
54 // Minimal service start.
start()55 status_t NetdHwService::start() {
56     // Register hardware service with ServiceManager.
57     return INetd::registerAsService();
58 }
59 
createOemNetwork(createOemNetwork_cb _hidl_cb)60 Return<void> NetdHwService::createOemNetwork(createOemNetwork_cb _hidl_cb) {
61     unsigned netId;
62     Permission permission = PERMISSION_SYSTEM;
63 
64     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 
76 // 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;  \
81     }
82 
destroyOemNetwork(uint64_t netHandle)83 Return<StatusCode> NetdHwService::destroyOemNetwork(uint64_t netHandle) {
84     unsigned netId = netHandleToNetId(netHandle);
85     RETURN_IF_NOT_OEM_NETWORK(netId);
86 
87     return toHalStatus(gCtls->netCtrl.destroyNetwork(netId));
88 }
89 
maybeNullString(const hidl_string & nexthop)90 const 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 
addRouteToOemNetwork(uint64_t networkHandle,const hidl_string & ifname,const hidl_string & destination,const hidl_string & nexthop)99 Return <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(),
106                                                maybeNullString(nexthop), false, INVALID_UID,
107                                                0 /* mtu */));
108 }
109 
removeRouteFromOemNetwork(uint64_t networkHandle,const hidl_string & ifname,const hidl_string & destination,const hidl_string & nexthop)110 Return <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 
addInterfaceToOemNetwork(uint64_t networkHandle,const hidl_string & ifname)120 Return <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 
removeInterfaceFromOemNetwork(uint64_t networkHandle,const hidl_string & ifname)128 Return <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 
setIpForwardEnable(bool enable)136 Return <StatusCode> NetdHwService::setIpForwardEnable(bool enable) {
137     std::lock_guard _lock(gCtls->tetherCtrl.lock);
138 
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 
setForwardingBetweenInterfaces(const hidl_string & inputIfName,const hidl_string & outputIfName,bool enable)145 Return <StatusCode> NetdHwService::setForwardingBetweenInterfaces(
146         const hidl_string& inputIfName, const hidl_string& outputIfName, bool enable) {
147     std::lock_guard _lock(gCtls->tetherCtrl.lock);
148 
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 
156 }  // namespace net
157 }  // namespace android
158