1 /*
2  * Copyright (C) 2018 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 #include <unistd.h>
18 
19 //#define LOG_NDEBUG 0
20 #define LOG_TAG "NetworkUtils"
21 #include <utils/Log.h>
22 
23 #include "NetworkUtils.h"
24 #include <cutils/qtaguid.h>
25 #include <NetdClient.h>
26 
27 namespace android {
28 
29 // static
RegisterSocketUserTag(int sockfd,uid_t uid,uint32_t kTag)30 void NetworkUtils::RegisterSocketUserTag(int sockfd, uid_t uid, uint32_t kTag) {
31     int res = qtaguid_tagSocket(sockfd, kTag, uid);
32     if (res != 0) {
33         ALOGE("Failed tagging socket %d for uid %d (My UID=%d)", sockfd, uid, geteuid());
34     }
35 }
36 
37 // static
UnRegisterSocketUserTag(int sockfd)38 void NetworkUtils::UnRegisterSocketUserTag(int sockfd) {
39     int res = qtaguid_untagSocket(sockfd);
40     if (res != 0) {
41         ALOGE("Failed untagging socket %d (My UID=%d)", sockfd, geteuid());
42     }
43 }
44 
45 // static
RegisterSocketUserMark(int sockfd,uid_t uid)46 void NetworkUtils::RegisterSocketUserMark(int sockfd, uid_t uid) {
47     setNetworkForUser(uid, sockfd);
48 }
49 
50 // static
UnRegisterSocketUserMark(int sockfd)51 void NetworkUtils::UnRegisterSocketUserMark(int sockfd) {
52     RegisterSocketUserMark(sockfd, geteuid());
53 }
54 
55 }  // namespace android
56