OSDN Git Service

Add an RPC to replace a UID firewall rule.
[android-x86/system-netd.git] / server / NetdNativeService.cpp
1 /**
2  * Copyright (c) 2016, 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 "Netd"
18
19 #include <vector>
20
21 #include <android-base/stringprintf.h>
22 #include <cutils/log.h>
23 #include <utils/Errors.h>
24
25 #include <binder/IPCThreadState.h>
26 #include <binder/IServiceManager.h>
27 #include "android/net/BnNetd.h"
28
29 #include "Controllers.h"
30 #include "NetdConstants.h"
31 #include "NetdNativeService.h"
32
33 using android::base::StringPrintf;
34
35 namespace android {
36 namespace net {
37
38 namespace {
39
40 const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL";
41
42 binder::Status checkPermission(const char *permission) {
43     pid_t pid;
44     uid_t uid;
45
46     if (checkCallingPermission(String16(permission), (int32_t *) &pid, (int32_t *) &uid)) {
47         return binder::Status::ok();
48     } else {
49         auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission);
50         return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str()));
51     }
52 }
53
54 #define ENFORCE_PERMISSION(permission) {                    \
55     binder::Status status = checkPermission((permission));  \
56     if (!status.isOk()) {                                   \
57         return status;                                      \
58     }                                                       \
59 }
60
61 #define NETD_LOCKING_RPC(permission, lock)                  \
62     ENFORCE_PERMISSION(permission);                         \
63     android::RWLock::AutoWLock _lock(lock);
64
65 #define NETD_BIG_LOCK_RPC(permission) NETD_LOCKING_RPC((permission), gBigNetdLock)
66
67 }  // namespace
68
69
70 binder::Status NetdNativeService::isAlive(bool *alive) {
71     NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
72
73     *alive = true;
74     return binder::Status::ok();
75 }
76
77 binder::Status NetdNativeService::firewallReplaceUidChain(const android::String16& chainName,
78         bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) {
79     NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->firewallCtrl.lock);
80
81     android::String8 name = android::String8(chainName);
82     int err = gCtls->firewallCtrl.replaceUidChain(name.string(), isWhitelist, uids);
83     *ret = (err == 0);
84     return binder::Status::ok();
85
86 }
87 }  // namespace net
88 }  // namespace android