2 * Copyright (c) 2016, The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "Netd"
21 #include <android-base/stringprintf.h>
22 #include <cutils/log.h>
23 #include <utils/Errors.h>
25 #include <binder/IPCThreadState.h>
26 #include <binder/IServiceManager.h>
27 #include "android/net/BnNetd.h"
29 #include "Controllers.h"
30 #include "NetdConstants.h"
31 #include "NetdNativeService.h"
33 using android::base::StringPrintf;
40 const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL";
42 binder::Status checkPermission(const char *permission) {
46 if (checkCallingPermission(String16(permission), (int32_t *) &pid, (int32_t *) &uid)) {
47 return binder::Status::ok();
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()));
54 #define ENFORCE_PERMISSION(permission) { \
55 binder::Status status = checkPermission((permission)); \
56 if (!status.isOk()) { \
61 #define NETD_LOCKING_RPC(permission, lock) \
62 ENFORCE_PERMISSION(permission); \
63 android::RWLock::AutoWLock _lock(lock);
65 #define NETD_BIG_LOCK_RPC(permission) NETD_LOCKING_RPC((permission), gBigNetdLock)
70 binder::Status NetdNativeService::isAlive(bool *alive) {
71 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
74 return binder::Status::ok();
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);
81 android::String8 name = android::String8(chainName);
82 int err = gCtls->firewallCtrl.replaceUidChain(name.string(), isWhitelist, uids);
84 return binder::Status::ok();
88 } // namespace android