Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 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 <binder/IPCThreadState.h> |
| 18 | #include <hidl/HidlTransportSupport.h> |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 19 | #include "Controllers.h" |
| 20 | #include "Fwmark.h" |
| 21 | #include "NetdHwService.h" |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 22 | #include "RouteController.h" |
| 23 | #include "TetherController.h" |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 24 | |
| 25 | using android::hardware::configureRpcThreadpool; |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 26 | using android::hardware::Void; |
| 27 | |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 28 | // 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. |
| 31 | namespace { |
| 32 | constexpr const char* FORWARDING_REQUESTER = "NetdHwService"; |
| 33 | } |
| 34 | |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace net { |
| 37 | |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 38 | static StatusCode toHalStatus(int ret) { |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 39 | switch(ret) { |
| 40 | case 0: |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 41 | return StatusCode::OK; |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 42 | case -EINVAL: |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 43 | return StatusCode::INVALID_ARGUMENTS; |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 44 | case -EEXIST: |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 45 | return StatusCode::ALREADY_EXISTS; |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 46 | case -ENONET: |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 47 | return StatusCode::NO_NETWORK; |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 48 | case -EPERM: |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 49 | return StatusCode::PERMISSION_DENIED; |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 50 | default: |
| 51 | ALOGE("HAL service error=%d", ret); |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 52 | return StatusCode::UNKNOWN_ERROR; |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | // Minimal service start. |
| 57 | status_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 | |
| 65 | Return<void> NetdHwService::createOemNetwork(createOemNetwork_cb _hidl_cb) { |
| 66 | unsigned netId; |
| 67 | Permission permission = PERMISSION_SYSTEM; |
| 68 | |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 69 | 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 Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 81 | // 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 Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 88 | Return<StatusCode> NetdHwService::destroyOemNetwork(uint64_t netHandle) { |
| 89 | unsigned netId = netHandleToNetId(netHandle); |
| 90 | RETURN_IF_NOT_OEM_NETWORK(netId); |
| 91 | |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 92 | return toHalStatus(gCtls->netCtrl.destroyNetwork(netId)); |
| 93 | } |
| 94 | |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 95 | const 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 | |
| 104 | Return <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 Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame^] | 111 | maybeNullString(nexthop), false, INVALID_UID, |
| 112 | 0 /* mtu */)); |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | Return <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 | |
| 125 | Return <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 | |
| 133 | Return <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 | |
| 141 | Return <StatusCode> NetdHwService::setIpForwardEnable(bool enable) { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 142 | std::lock_guard _lock(gCtls->tetherCtrl.lock); |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 143 | |
| 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 | |
| 150 | Return <StatusCode> NetdHwService::setForwardingBetweenInterfaces( |
| 151 | const hidl_string& inputIfName, const hidl_string& outputIfName, bool enable) { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 152 | std::lock_guard _lock(gCtls->tetherCtrl.lock); |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 153 | |
| 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 Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 161 | } // namespace net |
| 162 | } // namespace android |