blob: 34b4f0756b8cc521aaf726263ef7739055423d16 [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) {
Henri Chataing9007cca2023-11-06 17:43:05 +000062 return Errorf("Interface match {} must have nonzero interface index",
Maciej Żenczykowskid7ade392024-02-16 00:29:57 +000063 static_cast<uint32_t>(match));
Ken Chenc52cbe02022-07-29 03:30:25 +080064 } else if (match != IIF_MATCH && iif != 0) {
Henri Chataing9007cca2023-11-06 17:43:05 +000065 return Errorf("Non-interface match {} must have zero interface index",
Maciej Żenczykowskid7ade392024-02-16 00:29:57 +000066 static_cast<uint32_t>(match));
Ken Chenc52cbe02022-07-29 03:30:25 +080067 }
68
69 std::lock_guard guard(mMutex);
70 auto oldMatch = mUidOwnerMap.readValue(uid);
71 if (oldMatch.ok()) {
72 UidOwnerValue newMatch = {
73 .iif = iif ? iif : oldMatch.value().iif,
Maciej Żenczykowskid7ade392024-02-16 00:29:57 +000074 .rule = oldMatch.value().rule | match,
Ken Chenc52cbe02022-07-29 03:30:25 +080075 };
76 auto res = mUidOwnerMap.writeValue(uid, newMatch, BPF_ANY);
77 if (!res.ok()) return Errorf("Failed to update rule: {}", res.error().message());
78 } else {
79 UidOwnerValue newMatch = {
80 .iif = iif,
Maciej Żenczykowskid7ade392024-02-16 00:29:57 +000081 .rule = match,
Ken Chenc52cbe02022-07-29 03:30:25 +080082 };
83 auto res = mUidOwnerMap.writeValue(uid, newMatch, BPF_ANY);
84 if (!res.ok()) return Errorf("Failed to add rule: {}", res.error().message());
85 }
86 return {};
87}
88
89Result<void> Firewall::removeRule(uint32_t uid, UidOwnerMatchType match) {
90 std::lock_guard guard(mMutex);
91 auto oldMatch = mUidOwnerMap.readValue(uid);
92 if (!oldMatch.ok()) return Errorf("uid: %u does not exist in map", uid);
93
94 UidOwnerValue newMatch = {
95 .iif = (match == IIF_MATCH) ? 0 : oldMatch.value().iif,
Maciej Żenczykowskid7ade392024-02-16 00:29:57 +000096 .rule = oldMatch.value().rule & ~match,
Ken Chenc52cbe02022-07-29 03:30:25 +080097 };
98 if (newMatch.rule == 0) {
99 auto res = mUidOwnerMap.deleteValue(uid);
100 if (!res.ok()) return Errorf("Failed to remove rule: {}", res.error().message());
101 } else {
102 auto res = mUidOwnerMap.writeValue(uid, newMatch, BPF_ANY);
103 if (!res.ok()) return Errorf("Failed to update rule: {}", res.error().message());
104 }
105 return {};
106}
107
108Result<void> Firewall::addUidInterfaceRules(const std::string& ifName,
109 const std::vector<int32_t>& uids) {
110 unsigned int iif = if_nametoindex(ifName.c_str());
111 if (!iif) return Errorf("Failed to get interface index: {}", ifName);
112
113 for (auto uid : uids) {
114 auto res = addRule(uid, IIF_MATCH, iif);
115 if (!res.ok()) return res;
116 }
117 return {};
118}
119
120Result<void> Firewall::removeUidInterfaceRules(const std::vector<int32_t>& uids) {
121 for (auto uid : uids) {
122 auto res = removeRule(uid, IIF_MATCH);
123 if (!res.ok()) return res;
124 }
125 return {};
126}
Ken Chen5b144a12023-10-26 14:00:23 +0800127
128Result<bool> Firewall::getDataSaverSetting() {
129 std::lock_guard guard(mMutex);
Ken Chen90e009b2023-11-04 16:10:34 +0800130 if (!mDataSaverEnabledMap.isValid()) {
131 return Errorf("init mDataSaverEnabledMap failed");
132 }
133
Ken Chen5b144a12023-10-26 14:00:23 +0800134 auto dataSaverSetting = mDataSaverEnabledMap.readValue(DATA_SAVER_ENABLED_KEY);
135 if (!dataSaverSetting.ok()) {
136 return Errorf("Cannot read the data saver setting: {}", dataSaverSetting.error().message());
137 }
138 return dataSaverSetting;
139}
140
141Result<void> Firewall::setDataSaver(bool enabled) {
142 std::lock_guard guard(mMutex);
Ken Chen90e009b2023-11-04 16:10:34 +0800143 if (!mDataSaverEnabledMap.isValid()) {
144 return Errorf("init mDataSaverEnabledMap failed");
145 }
146
Ken Chen5b144a12023-10-26 14:00:23 +0800147 auto res = mDataSaverEnabledMap.writeValue(DATA_SAVER_ENABLED_KEY, enabled, BPF_EXIST);
148 if (!res.ok()) return Errorf("Failed to set data saver: {}", res.error().message());
149
150 return {};
151}