blob: 22f83e8ed4419033d91733b6c1dbc43ecb367e42 [file] [log] [blame]
Ken Chenc52cbe02022-07-29 03:30:25 +08001/*
2 * Copyright (C) 2022 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
18#include "firewall.h"
19
20#include <android-base/result.h>
21#include <gtest/gtest.h>
22
23Firewall::Firewall() {
24 std::lock_guard guard(mMutex);
25 auto result = mConfigurationMap.init(CONFIGURATION_MAP_PATH);
26 EXPECT_RESULT_OK(result) << "init mConfigurationMap failed";
27
28 result = mUidOwnerMap.init(UID_OWNER_MAP_PATH);
29 EXPECT_RESULT_OK(result) << "init mUidOwnerMap failed";
Ken Chen5b144a12023-10-26 14:00:23 +080030
Ken Chen90e009b2023-11-04 16:10:34 +080031 // Do not check whether DATA_SAVER_ENABLED_MAP_PATH init succeeded or failed since the map is
32 // defined in tethering module, but the user of this class may be in other modules. For example,
33 // DNS resolver tests statically link to this class. But when running MTS, the test infra
34 // installs only DNS resolver module without installing tethering module together.
35 mDataSaverEnabledMap.init(DATA_SAVER_ENABLED_MAP_PATH);
Ken Chenc52cbe02022-07-29 03:30:25 +080036}
37
38Firewall* Firewall::getInstance() {
39 static Firewall instance;
40 return &instance;
41}
42
43Result<void> Firewall::toggleStandbyMatch(bool enable) {
44 std::lock_guard guard(mMutex);
45 uint32_t key = UID_RULES_CONFIGURATION_KEY;
46 auto oldConfiguration = mConfigurationMap.readValue(key);
47 if (!oldConfiguration.ok()) {
48 return Errorf("Cannot read the old configuration: {}", oldConfiguration.error().message());
49 }
50
51 BpfConfig newConfiguration = enable ? (oldConfiguration.value() | STANDBY_MATCH)
52 : (oldConfiguration.value() & (~STANDBY_MATCH));
53 auto res = mConfigurationMap.writeValue(key, newConfiguration, BPF_EXIST);
54 if (!res.ok()) return Errorf("Failed to toggle STANDBY_MATCH: {}", res.error().message());
55
56 return {};
57}
58
59Result<void> Firewall::addRule(uint32_t uid, UidOwnerMatchType match, uint32_t iif) {
60 // iif should be non-zero if and only if match == MATCH_IIF
61 if (match == IIF_MATCH && iif == 0) {
62 return Errorf("Interface match {} must have nonzero interface index", match);
63 } else if (match != IIF_MATCH && iif != 0) {
64 return Errorf("Non-interface match {} must have zero interface index", match);
65 }
66
67 std::lock_guard guard(mMutex);
68 auto oldMatch = mUidOwnerMap.readValue(uid);
69 if (oldMatch.ok()) {
70 UidOwnerValue newMatch = {
71 .iif = iif ? iif : oldMatch.value().iif,
72 .rule = static_cast<uint8_t>(oldMatch.value().rule | match),
73 };
74 auto res = mUidOwnerMap.writeValue(uid, newMatch, BPF_ANY);
75 if (!res.ok()) return Errorf("Failed to update rule: {}", res.error().message());
76 } else {
77 UidOwnerValue newMatch = {
78 .iif = iif,
79 .rule = static_cast<uint8_t>(match),
80 };
81 auto res = mUidOwnerMap.writeValue(uid, newMatch, BPF_ANY);
82 if (!res.ok()) return Errorf("Failed to add rule: {}", res.error().message());
83 }
84 return {};
85}
86
87Result<void> Firewall::removeRule(uint32_t uid, UidOwnerMatchType match) {
88 std::lock_guard guard(mMutex);
89 auto oldMatch = mUidOwnerMap.readValue(uid);
90 if (!oldMatch.ok()) return Errorf("uid: %u does not exist in map", uid);
91
92 UidOwnerValue newMatch = {
93 .iif = (match == IIF_MATCH) ? 0 : oldMatch.value().iif,
94 .rule = static_cast<uint8_t>(oldMatch.value().rule & ~match),
95 };
96 if (newMatch.rule == 0) {
97 auto res = mUidOwnerMap.deleteValue(uid);
98 if (!res.ok()) return Errorf("Failed to remove rule: {}", res.error().message());
99 } else {
100 auto res = mUidOwnerMap.writeValue(uid, newMatch, BPF_ANY);
101 if (!res.ok()) return Errorf("Failed to update rule: {}", res.error().message());
102 }
103 return {};
104}
105
106Result<void> Firewall::addUidInterfaceRules(const std::string& ifName,
107 const std::vector<int32_t>& uids) {
108 unsigned int iif = if_nametoindex(ifName.c_str());
109 if (!iif) return Errorf("Failed to get interface index: {}", ifName);
110
111 for (auto uid : uids) {
112 auto res = addRule(uid, IIF_MATCH, iif);
113 if (!res.ok()) return res;
114 }
115 return {};
116}
117
118Result<void> Firewall::removeUidInterfaceRules(const std::vector<int32_t>& uids) {
119 for (auto uid : uids) {
120 auto res = removeRule(uid, IIF_MATCH);
121 if (!res.ok()) return res;
122 }
123 return {};
124}
Ken Chen5b144a12023-10-26 14:00:23 +0800125
126Result<bool> Firewall::getDataSaverSetting() {
127 std::lock_guard guard(mMutex);
Ken Chen90e009b2023-11-04 16:10:34 +0800128 if (!mDataSaverEnabledMap.isValid()) {
129 return Errorf("init mDataSaverEnabledMap failed");
130 }
131
Ken Chen5b144a12023-10-26 14:00:23 +0800132 auto dataSaverSetting = mDataSaverEnabledMap.readValue(DATA_SAVER_ENABLED_KEY);
133 if (!dataSaverSetting.ok()) {
134 return Errorf("Cannot read the data saver setting: {}", dataSaverSetting.error().message());
135 }
136 return dataSaverSetting;
137}
138
139Result<void> Firewall::setDataSaver(bool enabled) {
140 std::lock_guard guard(mMutex);
Ken Chen90e009b2023-11-04 16:10:34 +0800141 if (!mDataSaverEnabledMap.isValid()) {
142 return Errorf("init mDataSaverEnabledMap failed");
143 }
144
Ken Chen5b144a12023-10-26 14:00:23 +0800145 auto res = mDataSaverEnabledMap.writeValue(DATA_SAVER_ENABLED_KEY, enabled, BPF_EXIST);
146 if (!res.ok()) return Errorf("Failed to set data saver: {}", res.error().message());
147
148 return {};
149}