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 <cutils/properties.h>
24 #include <utils/Errors.h>
25 #include <utils/String16.h>
27 #include <binder/IPCThreadState.h>
28 #include <binder/IServiceManager.h>
29 #include "android/net/BnNetd.h"
31 #include "Controllers.h"
32 #include "DumpWriter.h"
33 #include "InterfaceController.h"
34 #include "NetdConstants.h"
35 #include "NetdNativeService.h"
36 #include "RouteController.h"
38 #include "UidRanges.h"
40 using android::base::StringPrintf;
47 const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL";
48 const char DUMP[] = "android.permission.DUMP";
50 binder::Status checkPermission(const char *permission) {
54 if (checkCallingPermission(String16(permission), (int32_t *) &pid, (int32_t *) &uid)) {
55 return binder::Status::ok();
57 auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission);
58 return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str()));
62 #define ENFORCE_DEBUGGABLE() { \
63 char value[PROPERTY_VALUE_MAX + 1]; \
64 if (property_get("ro.debuggable", value, NULL) != 1 \
65 || value[0] != '1') { \
66 return binder::Status::fromExceptionCode( \
67 binder::Status::EX_SECURITY, \
68 String8("Not available in production builds.") \
73 #define ENFORCE_PERMISSION(permission) { \
74 binder::Status status = checkPermission((permission)); \
75 if (!status.isOk()) { \
80 #define NETD_LOCKING_RPC(permission, lock) \
81 ENFORCE_PERMISSION(permission); \
82 android::RWLock::AutoWLock _lock(lock);
84 #define NETD_BIG_LOCK_RPC(permission) NETD_LOCKING_RPC((permission), gBigNetdLock)
88 status_t NetdNativeService::start() {
89 IPCThreadState::self()->disableBackgroundScheduling(true);
90 status_t ret = BinderService<NetdNativeService>::publish();
91 if (ret != android::OK) {
94 sp<ProcessState> ps(ProcessState::self());
95 ps->startThreadPool();
96 ps->giveThreadPoolName();
100 status_t NetdNativeService::dump(int fd, const Vector<String16> & /* args */) {
101 const binder::Status dump_permission = checkPermission(DUMP);
102 if (!dump_permission.isOk()) {
103 const String8 msg(dump_permission.toString8());
104 write(fd, msg.string(), msg.size());
105 return PERMISSION_DENIED;
108 // This method does not grab any locks. If individual classes need locking
109 // their dump() methods MUST handle locking appropriately.
112 gCtls->netCtrl.dump(dw);
118 binder::Status NetdNativeService::isAlive(bool *alive) {
119 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
122 return binder::Status::ok();
125 binder::Status NetdNativeService::firewallReplaceUidChain(const android::String16& chainName,
126 bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) {
127 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->firewallCtrl.lock);
129 android::String8 name = android::String8(chainName);
130 int err = gCtls->firewallCtrl.replaceUidChain(name.string(), isWhitelist, uids);
132 return binder::Status::ok();
135 binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
136 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->bandwidthCtrl.lock);
138 int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
140 return binder::Status::ok();
143 binder::Status NetdNativeService::networkRejectNonSecureVpn(bool add,
144 const std::vector<UidRange>& uidRangeArray) {
145 // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
146 // it should be possible to use the same lock as NetworkController. However, every call through
147 // the CommandListener "network" command will need to hold this lock too, not just the ones that
148 // read/modify network internal state (that is sufficient for ::dump() because it doesn't
149 // look at routes, but it's not enough here).
150 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
152 UidRanges uidRanges(uidRangeArray);
156 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
158 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
162 return binder::Status::fromServiceSpecificError(-err,
163 String8::format("RouteController error: %s", strerror(-err)));
165 return binder::Status::ok();
168 binder::Status NetdNativeService::socketDestroy(const std::vector<UidRange>& uids,
169 const std::vector<int32_t>& skipUids) {
171 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
175 return binder::Status::fromServiceSpecificError(EIO,
176 String8("Could not open SOCK_DIAG socket"));
179 UidRanges uidRanges(uids);
180 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()),
181 true /* excludeLoopback */);
184 return binder::Status::fromServiceSpecificError(-err,
185 String8::format("destroySockets: %s", strerror(-err)));
187 return binder::Status::ok();
190 binder::Status NetdNativeService::setResolverConfiguration(int32_t netId,
191 const std::vector<std::string>& servers, const std::vector<std::string>& domains,
192 const std::vector<int32_t>& params) {
193 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
194 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
196 int err = gCtls->resolverCtrl.setResolverConfiguration(netId, servers, domains, params);
198 return binder::Status::fromServiceSpecificError(-err,
199 String8::format("ResolverController error: %s", strerror(-err)));
201 return binder::Status::ok();
204 binder::Status NetdNativeService::getResolverInfo(int32_t netId,
205 std::vector<std::string>* servers, std::vector<std::string>* domains,
206 std::vector<int32_t>* params, std::vector<int32_t>* stats) {
207 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
208 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
210 int err = gCtls->resolverCtrl.getResolverInfo(netId, servers, domains, params, stats);
212 return binder::Status::fromServiceSpecificError(-err,
213 String8::format("ResolverController error: %s", strerror(-err)));
215 return binder::Status::ok();
218 binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
219 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
221 *ret = gCtls->tetherCtrl.applyDnsInterfaces();
222 return binder::Status::ok();
225 binder::Status NetdNativeService::interfaceAddAddress(const std::string &ifName,
226 const std::string &addrString, int prefixLength) {
227 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
229 const int err = InterfaceController::addAddress(
230 ifName.c_str(), addrString.c_str(), prefixLength);
232 return binder::Status::fromServiceSpecificError(-err,
233 String8::format("InterfaceController error: %s", strerror(-err)));
235 return binder::Status::ok();
238 binder::Status NetdNativeService::interfaceDelAddress(const std::string &ifName,
239 const std::string &addrString, int prefixLength) {
240 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
242 const int err = InterfaceController::delAddress(
243 ifName.c_str(), addrString.c_str(), prefixLength);
245 return binder::Status::fromServiceSpecificError(-err,
246 String8::format("InterfaceController error: %s", strerror(-err)));
248 return binder::Status::ok();
251 binder::Status NetdNativeService::setProcSysNet(
252 int32_t family, int32_t which, const std::string &ifname, const std::string ¶meter,
253 const std::string &value) {
254 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
256 const char *familyStr;
265 return binder::Status::fromServiceSpecificError(EAFNOSUPPORT, String8("Bad family"));
268 const char *whichStr;
277 return binder::Status::fromServiceSpecificError(EINVAL, String8("Bad category"));
280 const int err = InterfaceController::setParameter(
281 familyStr, whichStr, ifname.c_str(), parameter.c_str(),
284 return binder::Status::fromServiceSpecificError(-err,
285 String8::format("ResolverController error: %s", strerror(-err)));
287 return binder::Status::ok();
290 binder::Status NetdNativeService::getMetricsReportingLevel(int *reportingLevel) {
291 // This function intentionally does not lock, since the only thing it does is one read from an
293 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
294 ENFORCE_DEBUGGABLE();
296 *reportingLevel = gCtls->netCtrl.getMetricsReportingLevel();
297 return binder::Status::ok();
300 binder::Status NetdNativeService::setMetricsReportingLevel(const int reportingLevel) {
301 // This function intentionally does not lock, since the only thing it does is one write to an
303 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
304 ENFORCE_DEBUGGABLE();
306 if (int err = gCtls->netCtrl.setMetricsReportingLevel(reportingLevel)) {
307 return binder::Status::fromExceptionCode(binder::Status::EX_ILLEGAL_ARGUMENT);
309 return binder::Status::ok();
313 } // namespace android