| Ken Chen | ec0f7ac | 2023-09-08 14:14:55 +0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2023 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 | #define LOG_TAG "DnsBpfHelper" | 
|  | 18 |  | 
|  | 19 | #include "DnsBpfHelper.h" | 
|  | 20 |  | 
|  | 21 | #include <android-base/logging.h> | 
|  | 22 | #include <android-modules-utils/sdk_level.h> | 
|  | 23 |  | 
|  | 24 | namespace android { | 
|  | 25 | namespace net { | 
|  | 26 |  | 
| Ken Chen | 006a3f1 | 2023-10-26 09:25:55 +0800 | [diff] [blame] | 27 | #define RETURN_IF_RESULT_NOT_OK(result)                                                            \ | 
|  | 28 | do {                                                                                             \ | 
|  | 29 | if (!result.ok()) {                                                                            \ | 
|  | 30 | LOG(ERROR) << "L" << __LINE__ << " " << __func__ << ": " << strerror(result.error().code()); \ | 
|  | 31 | return result.error();                                                                       \ | 
|  | 32 | }                                                                                              \ | 
|  | 33 | } while (0) | 
|  | 34 |  | 
| Ken Chen | ec0f7ac | 2023-09-08 14:14:55 +0800 | [diff] [blame] | 35 | base::Result<void> DnsBpfHelper::init() { | 
|  | 36 | if (!android::modules::sdklevel::IsAtLeastT()) { | 
|  | 37 | LOG(ERROR) << __func__ << ": Unsupported before Android T."; | 
|  | 38 | return base::Error(EOPNOTSUPP); | 
|  | 39 | } | 
|  | 40 |  | 
| Ken Chen | 006a3f1 | 2023-10-26 09:25:55 +0800 | [diff] [blame] | 41 | RETURN_IF_RESULT_NOT_OK(mConfigurationMap.init(CONFIGURATION_MAP_PATH)); | 
|  | 42 | RETURN_IF_RESULT_NOT_OK(mUidOwnerMap.init(UID_OWNER_MAP_PATH)); | 
|  | 43 | RETURN_IF_RESULT_NOT_OK(mDataSaverEnabledMap.init(DATA_SAVER_ENABLED_MAP_PATH)); | 
|  | 44 | return {}; | 
| Ken Chen | ec0f7ac | 2023-09-08 14:14:55 +0800 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
| Ken Chen | 006a3f1 | 2023-10-26 09:25:55 +0800 | [diff] [blame] | 47 | base::Result<bool> DnsBpfHelper::isUidNetworkingBlocked(uid_t uid, bool metered) { | 
| Ken Chen | ec0f7ac | 2023-09-08 14:14:55 +0800 | [diff] [blame] | 48 | if (is_system_uid(uid)) return false; | 
|  | 49 | if (!mConfigurationMap.isValid() || !mUidOwnerMap.isValid()) { | 
|  | 50 | LOG(ERROR) << __func__ | 
|  | 51 | << ": BPF maps are not ready. Forgot to call ADnsHelper_init?"; | 
|  | 52 | return base::Error(EUNATCH); | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | auto enabledRules = mConfigurationMap.readValue(UID_RULES_CONFIGURATION_KEY); | 
| Ken Chen | 006a3f1 | 2023-10-26 09:25:55 +0800 | [diff] [blame] | 56 | RETURN_IF_RESULT_NOT_OK(enabledRules); | 
| Ken Chen | ec0f7ac | 2023-09-08 14:14:55 +0800 | [diff] [blame] | 57 |  | 
|  | 58 | auto value = mUidOwnerMap.readValue(uid); | 
|  | 59 | uint32_t uidRules = value.ok() ? value.value().rule : 0; | 
|  | 60 |  | 
| Ken Chen | 006a3f1 | 2023-10-26 09:25:55 +0800 | [diff] [blame] | 61 | // For doze mode, battery saver, low power standby. | 
| Ken Chen | ec0f7ac | 2023-09-08 14:14:55 +0800 | [diff] [blame] | 62 | if (isBlockedByUidRules(enabledRules.value(), uidRules)) return true; | 
|  | 63 |  | 
| Ken Chen | 006a3f1 | 2023-10-26 09:25:55 +0800 | [diff] [blame] | 64 | // For data saver. | 
|  | 65 | if (!metered) return false; | 
| Ken Chen | ec0f7ac | 2023-09-08 14:14:55 +0800 | [diff] [blame] | 66 |  | 
| Ken Chen | 006a3f1 | 2023-10-26 09:25:55 +0800 | [diff] [blame] | 67 | // The background data setting (PENALTY_BOX_MATCH) and unrestricted data usage setting | 
|  | 68 | // (HAPPY_BOX_MATCH) for individual apps override the system wide Data Saver setting. | 
|  | 69 | if (uidRules & PENALTY_BOX_MATCH) return true; | 
|  | 70 | if (uidRules & HAPPY_BOX_MATCH) return false; | 
|  | 71 |  | 
|  | 72 | auto dataSaverSetting = mDataSaverEnabledMap.readValue(DATA_SAVER_ENABLED_KEY); | 
|  | 73 | RETURN_IF_RESULT_NOT_OK(dataSaverSetting); | 
|  | 74 | return dataSaverSetting.value(); | 
| Ken Chen | ec0f7ac | 2023-09-08 14:14:55 +0800 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
|  | 77 | }  // namespace net | 
|  | 78 | }  // namespace android |