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 | |
Devin Moore | c304b47 | 2022-04-08 21:42:50 +0000 | [diff] [blame] | 17 | #include "NetdHwService.h" |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 18 | #include <binder/IPCThreadState.h> |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 19 | #include "Controllers.h" |
| 20 | #include "Fwmark.h" |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 21 | #include "RouteController.h" |
| 22 | #include "TetherController.h" |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 23 | |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 24 | using android::hardware::Void; |
| 25 | |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 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 | |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | namespace net { |
| 35 | |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 36 | static StatusCode toHalStatus(int ret) { |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 37 | switch(ret) { |
| 38 | case 0: |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 39 | return StatusCode::OK; |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 40 | case -EINVAL: |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 41 | return StatusCode::INVALID_ARGUMENTS; |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 42 | case -EEXIST: |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 43 | return StatusCode::ALREADY_EXISTS; |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 44 | case -ENONET: |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 45 | return StatusCode::NO_NETWORK; |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 46 | case -EPERM: |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 47 | return StatusCode::PERMISSION_DENIED; |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 48 | default: |
| 49 | ALOGE("HAL service error=%d", ret); |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 50 | return StatusCode::UNKNOWN_ERROR; |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | // Minimal service start. |
| 55 | status_t NetdHwService::start() { |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 56 | // Register hardware service with ServiceManager. |
| 57 | return INetd::registerAsService(); |
| 58 | } |
| 59 | |
| 60 | Return<void> NetdHwService::createOemNetwork(createOemNetwork_cb _hidl_cb) { |
| 61 | unsigned netId; |
| 62 | Permission permission = PERMISSION_SYSTEM; |
| 63 | |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 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 | |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 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; \ |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 83 | Return<StatusCode> NetdHwService::destroyOemNetwork(uint64_t netHandle) { |
| 84 | unsigned netId = netHandleToNetId(netHandle); |
| 85 | RETURN_IF_NOT_OEM_NETWORK(netId); |
| 86 | |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 87 | return toHalStatus(gCtls->netCtrl.destroyNetwork(netId)); |
| 88 | } |
| 89 | |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 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 | |
| 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(), |
Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 106 | maybeNullString(nexthop), false, INVALID_UID, |
| 107 | 0 /* mtu */)); |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 108 | } |
| 109 | |
| 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 | |
| 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 | |
| 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 | |
| 136 | Return <StatusCode> NetdHwService::setIpForwardEnable(bool enable) { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 137 | std::lock_guard _lock(gCtls->tetherCtrl.lock); |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 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 | |
| 145 | Return <StatusCode> NetdHwService::setForwardingBetweenInterfaces( |
| 146 | const hidl_string& inputIfName, const hidl_string& outputIfName, bool enable) { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 147 | std::lock_guard _lock(gCtls->tetherCtrl.lock); |
Lorenzo Colitti | 15589f7 | 2018-02-07 17:43:31 +0900 | [diff] [blame] | 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 | |
Niranjan Pendharkar | 7e08f85 | 2017-07-24 11:40:05 -0700 | [diff] [blame] | 156 | } // namespace net |
| 157 | } // namespace android |