1 /*
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 #define LOG_TAG "NetdUpdatable"
18
19 #include "NetdUpdatable.h"
20
21 #include <android-base/logging.h>
22 #include <netdutils/Status.h>
23
24 #include "NetdUpdatablePublic.h"
25
libnetd_updatable_init(const char * cg2_path)26 int libnetd_updatable_init(const char* cg2_path) {
27 android::base::InitLogging(/*argv=*/nullptr);
28 LOG(INFO) << __func__ << ": Initializing";
29
30 android::net::gNetdUpdatable = android::net::NetdUpdatable::getInstance();
31 android::netdutils::Status ret = android::net::gNetdUpdatable->mBpfHandler.init(cg2_path);
32 if (!android::netdutils::isOk(ret)) {
33 LOG(ERROR) << __func__ << ": BPF handler init failed";
34 return -ret.code();
35 }
36 return 0;
37 }
38
libnetd_updatable_tagSocket(int sockFd,uint32_t tag,uid_t chargeUid,uid_t realUid)39 int libnetd_updatable_tagSocket(int sockFd, uint32_t tag, uid_t chargeUid, uid_t realUid) {
40 if (android::net::gNetdUpdatable == nullptr) return -EPERM;
41 return android::net::gNetdUpdatable->mBpfHandler.tagSocket(sockFd, tag, chargeUid, realUid);
42 }
43
libnetd_updatable_untagSocket(int sockFd)44 int libnetd_updatable_untagSocket(int sockFd) {
45 if (android::net::gNetdUpdatable == nullptr) return -EPERM;
46 return android::net::gNetdUpdatable->mBpfHandler.untagSocket(sockFd);
47 }
48
49 namespace android {
50 namespace net {
51
52 NetdUpdatable* gNetdUpdatable = nullptr;
53
getInstance()54 NetdUpdatable* NetdUpdatable::getInstance() {
55 // Instantiated on first use.
56 static NetdUpdatable instance;
57 return &instance;
58 }
59
60 } // namespace net
61 } // namespace android
62