blob: 2ede1c1bc7ac772699062a60ce77e6965c7a05aa [file] [log] [blame]
Ken Chen1647f602021-10-05 21:55:22 +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#pragma once
18
19#include <mutex>
20
21#include <netdutils/Status.h>
22#include "bpf/BpfMap.h"
23#include "bpf_shared.h"
24
25using android::bpf::BpfMap;
26
27namespace android {
28namespace net {
29
30class BpfHandler {
31 public:
32 BpfHandler();
33 BpfHandler(const BpfHandler&) = delete;
34 BpfHandler& operator=(const BpfHandler&) = delete;
35 netdutils::Status init(const char* cg2_path);
36 /*
37 * Tag the socket with the specified tag and uid. In the qtaguid module, the
38 * first tag request that grab the spinlock of rb_tree can update the tag
39 * information first and other request need to wait until it finish. All the
40 * tag request will be addressed in the order of they obtaining the spinlock.
41 * In the eBPF implementation, the kernel will try to update the eBPF map
42 * entry with the tag request. And the hashmap update process is protected by
43 * the spinlock initialized with the map. So the behavior of two modules
44 * should be the same. No additional lock needed.
45 */
46 int tagSocket(int sockFd, uint32_t tag, uid_t chargeUid, uid_t realUid);
47
48 /*
49 * The untag process is similar to tag socket and both old qtaguid module and
50 * new eBPF module have spinlock inside the kernel for concurrent update. No
51 * external lock is required.
52 */
53 int untagSocket(int sockFd);
54
55 private:
56 // For testing
57 BpfHandler(uint32_t perUidLimit, uint32_t totalLimit);
58
59 netdutils::Status initMaps();
60 bool hasUpdateDeviceStatsPermission(uid_t uid);
61
62 BpfMap<uint64_t, UidTagValue> mCookieTagMap;
63 BpfMap<StatsKey, StatsValue> mStatsMapA;
64 BpfMap<StatsKey, StatsValue> mStatsMapB;
65 BpfMap<uint32_t, uint8_t> mConfigurationMap;
66 BpfMap<uint32_t, uint8_t> mUidPermissionMap;
67
68 std::mutex mMutex;
69
70 // The limit on the number of stats entries a uid can have in the per uid stats map. BpfHandler
71 // will block that specific uid from tagging new sockets after the limit is reached.
72 const uint32_t mPerUidStatsEntriesLimit;
73
74 // The limit on the total number of stats entries in the per uid stats map. BpfHandler will
75 // block all tagging requests after the limit is reached.
76 const uint32_t mTotalUidStatsEntriesLimit;
77
78 // For testing
79 friend class BpfHandlerTest;
80};
81
82} // namespace net
83} // namespace android