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 "EventReporter.h"
34 #include "InterfaceController.h"
35 #include "NetdConstants.h"
36 #include "NetdNativeService.h"
37 #include "RouteController.h"
39 #include "UidRanges.h"
41 using android::base::StringPrintf;
48 const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL";
49 const char DUMP[] = "android.permission.DUMP";
51 binder::Status checkPermission(const char *permission) {
55 if (checkCallingPermission(String16(permission), (int32_t *) &pid, (int32_t *) &uid)) {
56 return binder::Status::ok();
58 auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission);
59 return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str()));
63 #define ENFORCE_DEBUGGABLE() { \
64 char value[PROPERTY_VALUE_MAX + 1]; \
65 if (property_get("ro.debuggable", value, NULL) != 1 \
66 || value[0] != '1') { \
67 return binder::Status::fromExceptionCode( \
68 binder::Status::EX_SECURITY, \
69 String8("Not available in production builds.") \
74 #define ENFORCE_PERMISSION(permission) { \
75 binder::Status status = checkPermission((permission)); \
76 if (!status.isOk()) { \
81 #define NETD_LOCKING_RPC(permission, lock) \
82 ENFORCE_PERMISSION(permission); \
83 android::RWLock::AutoWLock _lock(lock);
85 #define NETD_BIG_LOCK_RPC(permission) NETD_LOCKING_RPC((permission), gBigNetdLock)
89 status_t NetdNativeService::start() {
90 IPCThreadState::self()->disableBackgroundScheduling(true);
91 status_t ret = BinderService<NetdNativeService>::publish();
92 if (ret != android::OK) {
95 sp<ProcessState> ps(ProcessState::self());
96 ps->startThreadPool();
97 ps->giveThreadPoolName();
101 status_t NetdNativeService::dump(int fd, const Vector<String16> & /* args */) {
102 const binder::Status dump_permission = checkPermission(DUMP);
103 if (!dump_permission.isOk()) {
104 const String8 msg(dump_permission.toString8());
105 write(fd, msg.string(), msg.size());
106 return PERMISSION_DENIED;
109 // This method does not grab any locks. If individual classes need locking
110 // their dump() methods MUST handle locking appropriately.
113 gCtls->netCtrl.dump(dw);
119 binder::Status NetdNativeService::isAlive(bool *alive) {
120 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
123 return binder::Status::ok();
126 binder::Status NetdNativeService::firewallReplaceUidChain(const android::String16& chainName,
127 bool isWhitelist, const std::vector<int32_t>& uids, bool *ret) {
128 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->firewallCtrl.lock);
130 android::String8 name = android::String8(chainName);
131 int err = gCtls->firewallCtrl.replaceUidChain(name.string(), isWhitelist, uids);
133 return binder::Status::ok();
136 binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
137 NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL, gCtls->bandwidthCtrl.lock);
139 int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
141 return binder::Status::ok();
144 binder::Status NetdNativeService::networkRejectNonSecureVpn(bool add,
145 const std::vector<UidRange>& uidRangeArray) {
146 // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
147 // it should be possible to use the same lock as NetworkController. However, every call through
148 // the CommandListener "network" command will need to hold this lock too, not just the ones that
149 // read/modify network internal state (that is sufficient for ::dump() because it doesn't
150 // look at routes, but it's not enough here).
151 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
153 UidRanges uidRanges(uidRangeArray);
157 err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
159 err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
163 return binder::Status::fromServiceSpecificError(-err,
164 String8::format("RouteController error: %s", strerror(-err)));
166 return binder::Status::ok();
169 binder::Status NetdNativeService::socketDestroy(const std::vector<UidRange>& uids,
170 const std::vector<int32_t>& skipUids) {
172 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
176 return binder::Status::fromServiceSpecificError(EIO,
177 String8("Could not open SOCK_DIAG socket"));
180 UidRanges uidRanges(uids);
181 int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()),
182 true /* excludeLoopback */);
185 return binder::Status::fromServiceSpecificError(-err,
186 String8::format("destroySockets: %s", strerror(-err)));
188 return binder::Status::ok();
191 binder::Status NetdNativeService::setResolverConfiguration(int32_t netId,
192 const std::vector<std::string>& servers, const std::vector<std::string>& domains,
193 const std::vector<int32_t>& params) {
194 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
195 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
197 int err = gCtls->resolverCtrl.setResolverConfiguration(netId, servers, domains, params);
199 return binder::Status::fromServiceSpecificError(-err,
200 String8::format("ResolverController error: %s", strerror(-err)));
202 return binder::Status::ok();
205 binder::Status NetdNativeService::getResolverInfo(int32_t netId,
206 std::vector<std::string>* servers, std::vector<std::string>* domains,
207 std::vector<int32_t>* params, std::vector<int32_t>* stats) {
208 // This function intentionally does not lock within Netd, as Bionic is thread-safe.
209 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
211 int err = gCtls->resolverCtrl.getResolverInfo(netId, servers, domains, params, stats);
213 return binder::Status::fromServiceSpecificError(-err,
214 String8::format("ResolverController error: %s", strerror(-err)));
216 return binder::Status::ok();
219 binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
220 NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
222 *ret = gCtls->tetherCtrl.applyDnsInterfaces();
223 return binder::Status::ok();
226 binder::Status NetdNativeService::interfaceAddAddress(const std::string &ifName,
227 const std::string &addrString, int prefixLength) {
228 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
230 const int err = InterfaceController::addAddress(
231 ifName.c_str(), addrString.c_str(), prefixLength);
233 return binder::Status::fromServiceSpecificError(-err,
234 String8::format("InterfaceController error: %s", strerror(-err)));
236 return binder::Status::ok();
239 binder::Status NetdNativeService::interfaceDelAddress(const std::string &ifName,
240 const std::string &addrString, int prefixLength) {
241 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
243 const int err = InterfaceController::delAddress(
244 ifName.c_str(), addrString.c_str(), prefixLength);
246 return binder::Status::fromServiceSpecificError(-err,
247 String8::format("InterfaceController error: %s", strerror(-err)));
249 return binder::Status::ok();
252 binder::Status NetdNativeService::setProcSysNet(
253 int32_t family, int32_t which, const std::string &ifname, const std::string ¶meter,
254 const std::string &value) {
255 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
257 const char *familyStr;
266 return binder::Status::fromServiceSpecificError(EAFNOSUPPORT, String8("Bad family"));
269 const char *whichStr;
278 return binder::Status::fromServiceSpecificError(EINVAL, String8("Bad category"));
281 const int err = InterfaceController::setParameter(
282 familyStr, whichStr, ifname.c_str(), parameter.c_str(),
285 return binder::Status::fromServiceSpecificError(-err,
286 String8::format("ResolverController error: %s", strerror(-err)));
288 return binder::Status::ok();
291 binder::Status NetdNativeService::getMetricsReportingLevel(int *reportingLevel) {
292 // This function intentionally does not lock, since the only thing it does is one read from an
294 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
295 ENFORCE_DEBUGGABLE();
297 *reportingLevel = gCtls->eventReporter.getMetricsReportingLevel();
298 return binder::Status::ok();
301 binder::Status NetdNativeService::setMetricsReportingLevel(const int reportingLevel) {
302 // This function intentionally does not lock, since the only thing it does is one write to an
304 ENFORCE_PERMISSION(CONNECTIVITY_INTERNAL);
305 ENFORCE_DEBUGGABLE();
307 return (gCtls->eventReporter.setMetricsReportingLevel(reportingLevel) == 0)
308 ? binder::Status::ok()
309 : binder::Status::fromExceptionCode(binder::Status::EX_ILLEGAL_ARGUMENT);
313 } // namespace android