xref: /aosp_15_r20/system/netd/server/NetdNativeService.cpp (revision 8542734a0dd1db395a4d42aae09c37f3c3c3e7a1)
1*8542734aSAndroid Build Coastguard Worker /**
2*8542734aSAndroid Build Coastguard Worker  * Copyright (c) 2016, The Android Open Source Project
3*8542734aSAndroid Build Coastguard Worker  *
4*8542734aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8542734aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8542734aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8542734aSAndroid Build Coastguard Worker  *
8*8542734aSAndroid Build Coastguard Worker  *     http://www.apache.org/licenses/LICENSE-2.0
9*8542734aSAndroid Build Coastguard Worker  *
10*8542734aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8542734aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8542734aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8542734aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8542734aSAndroid Build Coastguard Worker  * limitations under the License.
15*8542734aSAndroid Build Coastguard Worker  */
16*8542734aSAndroid Build Coastguard Worker 
17*8542734aSAndroid Build Coastguard Worker #define LOG_TAG "Netd"
18*8542734aSAndroid Build Coastguard Worker 
19*8542734aSAndroid Build Coastguard Worker #include <cinttypes>
20*8542734aSAndroid Build Coastguard Worker #include <numeric>
21*8542734aSAndroid Build Coastguard Worker #include <set>
22*8542734aSAndroid Build Coastguard Worker #include <string>
23*8542734aSAndroid Build Coastguard Worker #include <tuple>
24*8542734aSAndroid Build Coastguard Worker #include <vector>
25*8542734aSAndroid Build Coastguard Worker 
26*8542734aSAndroid Build Coastguard Worker #include <android-base/file.h>
27*8542734aSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
28*8542734aSAndroid Build Coastguard Worker #include <android-base/strings.h>
29*8542734aSAndroid Build Coastguard Worker #include <binder/IPCThreadState.h>
30*8542734aSAndroid Build Coastguard Worker #include <binder/IServiceManager.h>
31*8542734aSAndroid Build Coastguard Worker #include <binder/Status.h>
32*8542734aSAndroid Build Coastguard Worker #include <cutils/properties.h>
33*8542734aSAndroid Build Coastguard Worker #include <log/log.h>
34*8542734aSAndroid Build Coastguard Worker #include <netdutils/DumpWriter.h>
35*8542734aSAndroid Build Coastguard Worker #include <netdutils/Utils.h>
36*8542734aSAndroid Build Coastguard Worker #include <utils/Errors.h>
37*8542734aSAndroid Build Coastguard Worker #include <utils/String16.h>
38*8542734aSAndroid Build Coastguard Worker 
39*8542734aSAndroid Build Coastguard Worker #include "Controllers.h"
40*8542734aSAndroid Build Coastguard Worker #include "Fwmark.h"
41*8542734aSAndroid Build Coastguard Worker #include "InterfaceController.h"
42*8542734aSAndroid Build Coastguard Worker #include "NetdNativeService.h"
43*8542734aSAndroid Build Coastguard Worker #include "OemNetdListener.h"
44*8542734aSAndroid Build Coastguard Worker #include "Permission.h"
45*8542734aSAndroid Build Coastguard Worker #include "Process.h"
46*8542734aSAndroid Build Coastguard Worker #include "RouteController.h"
47*8542734aSAndroid Build Coastguard Worker #include "SockDiag.h"
48*8542734aSAndroid Build Coastguard Worker #include "UidRanges.h"
49*8542734aSAndroid Build Coastguard Worker #include "android/net/BnNetd.h"
50*8542734aSAndroid Build Coastguard Worker #include "binder_utils/BinderUtil.h"
51*8542734aSAndroid Build Coastguard Worker #include "binder_utils/NetdPermissions.h"
52*8542734aSAndroid Build Coastguard Worker #include "netid_client.h"  // NETID_UNSET
53*8542734aSAndroid Build Coastguard Worker 
54*8542734aSAndroid Build Coastguard Worker using android::base::StringPrintf;
55*8542734aSAndroid Build Coastguard Worker using android::base::WriteStringToFile;
56*8542734aSAndroid Build Coastguard Worker using android::net::TetherStatsParcel;
57*8542734aSAndroid Build Coastguard Worker using android::net::UidRangeParcel;
58*8542734aSAndroid Build Coastguard Worker using android::net::netd::aidl::NativeUidRangeConfig;
59*8542734aSAndroid Build Coastguard Worker using android::netdutils::DumpWriter;
60*8542734aSAndroid Build Coastguard Worker using android::netdutils::getIfaceNames;
61*8542734aSAndroid Build Coastguard Worker using android::netdutils::ScopedIndent;
62*8542734aSAndroid Build Coastguard Worker using android::os::ParcelFileDescriptor;
63*8542734aSAndroid Build Coastguard Worker 
64*8542734aSAndroid Build Coastguard Worker namespace android {
65*8542734aSAndroid Build Coastguard Worker namespace net {
66*8542734aSAndroid Build Coastguard Worker 
67*8542734aSAndroid Build Coastguard Worker namespace {
68*8542734aSAndroid Build Coastguard Worker const char OPT_SHORT[] = "--short";
69*8542734aSAndroid Build Coastguard Worker 
70*8542734aSAndroid Build Coastguard Worker #define ENFORCE_ANY_PERMISSION(...)                                \
71*8542734aSAndroid Build Coastguard Worker     do {                                                           \
72*8542734aSAndroid Build Coastguard Worker         binder::Status status = checkAnyPermission({__VA_ARGS__}); \
73*8542734aSAndroid Build Coastguard Worker         if (!status.isOk()) {                                      \
74*8542734aSAndroid Build Coastguard Worker             return status;                                         \
75*8542734aSAndroid Build Coastguard Worker         }                                                          \
76*8542734aSAndroid Build Coastguard Worker     } while (0)
77*8542734aSAndroid Build Coastguard Worker 
78*8542734aSAndroid Build Coastguard Worker #define NETD_LOCKING_RPC(lock, ... /* permissions */) \
79*8542734aSAndroid Build Coastguard Worker     ENFORCE_ANY_PERMISSION(__VA_ARGS__);              \
80*8542734aSAndroid Build Coastguard Worker     std::lock_guard _lock(lock);
81*8542734aSAndroid Build Coastguard Worker 
82*8542734aSAndroid Build Coastguard Worker #define NETD_BIG_LOCK_RPC(... /* permissions */) NETD_LOCKING_RPC(gBigNetdLock, __VA_ARGS__)
83*8542734aSAndroid Build Coastguard Worker 
84*8542734aSAndroid Build Coastguard Worker #define RETURN_BINDER_STATUS_IF_NOT_OK(logEntry, res) \
85*8542734aSAndroid Build Coastguard Worker     do {                                              \
86*8542734aSAndroid Build Coastguard Worker         if (!isOk((res))) {                           \
87*8542734aSAndroid Build Coastguard Worker             logErrorStatus((logEntry), (res));        \
88*8542734aSAndroid Build Coastguard Worker             return asBinderStatus((res));             \
89*8542734aSAndroid Build Coastguard Worker         }                                             \
90*8542734aSAndroid Build Coastguard Worker     } while (0)
91*8542734aSAndroid Build Coastguard Worker 
92*8542734aSAndroid Build Coastguard Worker #define ENFORCE_NETWORK_STACK_PERMISSIONS() \
93*8542734aSAndroid Build Coastguard Worker     ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK)
94*8542734aSAndroid Build Coastguard Worker 
logErrorStatus(netdutils::LogEntry & logEntry,const netdutils::Status & status)95*8542734aSAndroid Build Coastguard Worker void logErrorStatus(netdutils::LogEntry& logEntry, const netdutils::Status& status) {
96*8542734aSAndroid Build Coastguard Worker     gLog.log(logEntry.returns(status.code()).withAutomaticDuration());
97*8542734aSAndroid Build Coastguard Worker }
98*8542734aSAndroid Build Coastguard Worker 
asBinderStatus(const netdutils::Status & status)99*8542734aSAndroid Build Coastguard Worker binder::Status asBinderStatus(const netdutils::Status& status) {
100*8542734aSAndroid Build Coastguard Worker     if (isOk(status)) {
101*8542734aSAndroid Build Coastguard Worker         return binder::Status::ok();
102*8542734aSAndroid Build Coastguard Worker     }
103*8542734aSAndroid Build Coastguard Worker     return binder::Status::fromServiceSpecificError(status.code(), status.msg().c_str());
104*8542734aSAndroid Build Coastguard Worker }
105*8542734aSAndroid Build Coastguard Worker 
106*8542734aSAndroid Build Coastguard Worker template <typename T>
asBinderStatus(const base::Result<T> result)107*8542734aSAndroid Build Coastguard Worker binder::Status asBinderStatus(const base::Result<T> result) {
108*8542734aSAndroid Build Coastguard Worker     if (result.ok()) return binder::Status::ok();
109*8542734aSAndroid Build Coastguard Worker 
110*8542734aSAndroid Build Coastguard Worker     return binder::Status::fromServiceSpecificError(result.error().code(),
111*8542734aSAndroid Build Coastguard Worker                                                     result.error().message().c_str());
112*8542734aSAndroid Build Coastguard Worker }
113*8542734aSAndroid Build Coastguard Worker 
contains(const Vector<String16> & words,const String16 & word)114*8542734aSAndroid Build Coastguard Worker bool contains(const Vector<String16>& words, const String16& word) {
115*8542734aSAndroid Build Coastguard Worker     for (const auto& w : words) {
116*8542734aSAndroid Build Coastguard Worker         if (w == word) return true;
117*8542734aSAndroid Build Coastguard Worker     }
118*8542734aSAndroid Build Coastguard Worker 
119*8542734aSAndroid Build Coastguard Worker     return false;
120*8542734aSAndroid Build Coastguard Worker }
121*8542734aSAndroid Build Coastguard Worker 
122*8542734aSAndroid Build Coastguard Worker }  // namespace
123*8542734aSAndroid Build Coastguard Worker 
NetdNativeService()124*8542734aSAndroid Build Coastguard Worker NetdNativeService::NetdNativeService() {
125*8542734aSAndroid Build Coastguard Worker     // register log callback to BnNetd::logFunc
126*8542734aSAndroid Build Coastguard Worker     BnNetd::logFunc = [](const auto& log) {
127*8542734aSAndroid Build Coastguard Worker         binderCallLogFn(log, [](const std::string& msg) { gLog.info("%s", msg.c_str()); });
128*8542734aSAndroid Build Coastguard Worker     };
129*8542734aSAndroid Build Coastguard Worker }
130*8542734aSAndroid Build Coastguard Worker 
start()131*8542734aSAndroid Build Coastguard Worker status_t NetdNativeService::start() {
132*8542734aSAndroid Build Coastguard Worker     IPCThreadState::self()->disableBackgroundScheduling(true);
133*8542734aSAndroid Build Coastguard Worker     const status_t ret = BinderService<NetdNativeService>::publish();
134*8542734aSAndroid Build Coastguard Worker     if (ret != android::OK) {
135*8542734aSAndroid Build Coastguard Worker         return ret;
136*8542734aSAndroid Build Coastguard Worker     }
137*8542734aSAndroid Build Coastguard Worker     sp<ProcessState> ps(ProcessState::self());
138*8542734aSAndroid Build Coastguard Worker     ps->startThreadPool();
139*8542734aSAndroid Build Coastguard Worker     ps->giveThreadPoolName();
140*8542734aSAndroid Build Coastguard Worker 
141*8542734aSAndroid Build Coastguard Worker     return android::OK;
142*8542734aSAndroid Build Coastguard Worker }
143*8542734aSAndroid Build Coastguard Worker 
dump(int fd,const Vector<String16> & args)144*8542734aSAndroid Build Coastguard Worker status_t NetdNativeService::dump(int fd, const Vector<String16> &args) {
145*8542734aSAndroid Build Coastguard Worker     const binder::Status dump_permission = checkAnyPermission({PERM_DUMP});
146*8542734aSAndroid Build Coastguard Worker     if (!dump_permission.isOk()) {
147*8542734aSAndroid Build Coastguard Worker         const String8 msg(dump_permission.toString8());
148*8542734aSAndroid Build Coastguard Worker         write(fd, msg.c_str(), msg.size());
149*8542734aSAndroid Build Coastguard Worker         return PERMISSION_DENIED;
150*8542734aSAndroid Build Coastguard Worker     }
151*8542734aSAndroid Build Coastguard Worker 
152*8542734aSAndroid Build Coastguard Worker     // This method does not grab any locks. If individual classes need locking
153*8542734aSAndroid Build Coastguard Worker     // their dump() methods MUST handle locking appropriately.
154*8542734aSAndroid Build Coastguard Worker 
155*8542734aSAndroid Build Coastguard Worker     DumpWriter dw(fd);
156*8542734aSAndroid Build Coastguard Worker 
157*8542734aSAndroid Build Coastguard Worker     if (!args.isEmpty() && args[0] == TcpSocketMonitor::DUMP_KEYWORD) {
158*8542734aSAndroid Build Coastguard Worker       dw.blankline();
159*8542734aSAndroid Build Coastguard Worker       gCtls->tcpSocketMonitor.dump(dw);
160*8542734aSAndroid Build Coastguard Worker       dw.blankline();
161*8542734aSAndroid Build Coastguard Worker       return NO_ERROR;
162*8542734aSAndroid Build Coastguard Worker     }
163*8542734aSAndroid Build Coastguard Worker 
164*8542734aSAndroid Build Coastguard Worker     process::dump(dw);
165*8542734aSAndroid Build Coastguard Worker     dw.blankline();
166*8542734aSAndroid Build Coastguard Worker     gCtls->netCtrl.dump(dw);
167*8542734aSAndroid Build Coastguard Worker     dw.blankline();
168*8542734aSAndroid Build Coastguard Worker 
169*8542734aSAndroid Build Coastguard Worker     gCtls->xfrmCtrl.dump(dw);
170*8542734aSAndroid Build Coastguard Worker     dw.blankline();
171*8542734aSAndroid Build Coastguard Worker 
172*8542734aSAndroid Build Coastguard Worker     gCtls->tetherCtrl.dump(dw);
173*8542734aSAndroid Build Coastguard Worker     dw.blankline();
174*8542734aSAndroid Build Coastguard Worker 
175*8542734aSAndroid Build Coastguard Worker     {
176*8542734aSAndroid Build Coastguard Worker         ScopedIndent indentLog(dw);
177*8542734aSAndroid Build Coastguard Worker         if (contains(args, String16(OPT_SHORT))) {
178*8542734aSAndroid Build Coastguard Worker             dw.println("Log: <omitted>");
179*8542734aSAndroid Build Coastguard Worker         } else {
180*8542734aSAndroid Build Coastguard Worker             dw.println("Log:");
181*8542734aSAndroid Build Coastguard Worker             ScopedIndent indentLogEntries(dw);
182*8542734aSAndroid Build Coastguard Worker             gLog.forEachEntry([&dw](const std::string& entry) mutable { dw.println(entry); });
183*8542734aSAndroid Build Coastguard Worker         }
184*8542734aSAndroid Build Coastguard Worker         dw.blankline();
185*8542734aSAndroid Build Coastguard Worker     }
186*8542734aSAndroid Build Coastguard Worker 
187*8542734aSAndroid Build Coastguard Worker     {
188*8542734aSAndroid Build Coastguard Worker         ScopedIndent indentLog(dw);
189*8542734aSAndroid Build Coastguard Worker         if (contains(args, String16(OPT_SHORT))) {
190*8542734aSAndroid Build Coastguard Worker             dw.println("UnsolicitedLog: <omitted>");
191*8542734aSAndroid Build Coastguard Worker         } else {
192*8542734aSAndroid Build Coastguard Worker             dw.println("UnsolicitedLog:");
193*8542734aSAndroid Build Coastguard Worker             ScopedIndent indentLogEntries(dw);
194*8542734aSAndroid Build Coastguard Worker             gUnsolicitedLog.forEachEntry(
195*8542734aSAndroid Build Coastguard Worker                     [&dw](const std::string& entry) mutable { dw.println(entry); });
196*8542734aSAndroid Build Coastguard Worker         }
197*8542734aSAndroid Build Coastguard Worker         dw.blankline();
198*8542734aSAndroid Build Coastguard Worker     }
199*8542734aSAndroid Build Coastguard Worker 
200*8542734aSAndroid Build Coastguard Worker     return NO_ERROR;
201*8542734aSAndroid Build Coastguard Worker }
202*8542734aSAndroid Build Coastguard Worker 
isAlive(bool * alive)203*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::isAlive(bool *alive) {
204*8542734aSAndroid Build Coastguard Worker     NETD_BIG_LOCK_RPC(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
205*8542734aSAndroid Build Coastguard Worker 
206*8542734aSAndroid Build Coastguard Worker     *alive = true;
207*8542734aSAndroid Build Coastguard Worker 
208*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
209*8542734aSAndroid Build Coastguard Worker }
210*8542734aSAndroid Build Coastguard Worker 
firewallReplaceUidChain(const std::string &,bool,const std::vector<int32_t> &,bool *)211*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::firewallReplaceUidChain(const std::string&, bool,
212*8542734aSAndroid Build Coastguard Worker                                                           const std::vector<int32_t>&, bool*) {
213*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
214*8542734aSAndroid Build Coastguard Worker }
215*8542734aSAndroid Build Coastguard Worker 
bandwidthEnableDataSaver(bool enable,bool * ret)216*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::bandwidthEnableDataSaver(bool enable, bool *ret) {
217*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
218*8542734aSAndroid Build Coastguard Worker     int err = gCtls->bandwidthCtrl.enableDataSaver(enable);
219*8542734aSAndroid Build Coastguard Worker     *ret = (err == 0);
220*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
221*8542734aSAndroid Build Coastguard Worker }
222*8542734aSAndroid Build Coastguard Worker 
bandwidthSetInterfaceQuota(const std::string & ifName,int64_t bytes)223*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::bandwidthSetInterfaceQuota(const std::string& ifName,
224*8542734aSAndroid Build Coastguard Worker                                                              int64_t bytes) {
225*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
226*8542734aSAndroid Build Coastguard Worker     int res = gCtls->bandwidthCtrl.setInterfaceQuota(ifName, bytes);
227*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
228*8542734aSAndroid Build Coastguard Worker }
229*8542734aSAndroid Build Coastguard Worker 
bandwidthRemoveInterfaceQuota(const std::string & ifName)230*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::bandwidthRemoveInterfaceQuota(const std::string& ifName) {
231*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
232*8542734aSAndroid Build Coastguard Worker     int res = gCtls->bandwidthCtrl.removeInterfaceQuota(ifName);
233*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
234*8542734aSAndroid Build Coastguard Worker }
235*8542734aSAndroid Build Coastguard Worker 
bandwidthSetInterfaceAlert(const std::string & ifName,int64_t bytes)236*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::bandwidthSetInterfaceAlert(const std::string& ifName,
237*8542734aSAndroid Build Coastguard Worker                                                              int64_t bytes) {
238*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
239*8542734aSAndroid Build Coastguard Worker     int res = gCtls->bandwidthCtrl.setInterfaceAlert(ifName, bytes);
240*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
241*8542734aSAndroid Build Coastguard Worker }
242*8542734aSAndroid Build Coastguard Worker 
bandwidthRemoveInterfaceAlert(const std::string & ifName)243*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::bandwidthRemoveInterfaceAlert(const std::string& ifName) {
244*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
245*8542734aSAndroid Build Coastguard Worker     int res = gCtls->bandwidthCtrl.removeInterfaceAlert(ifName);
246*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
247*8542734aSAndroid Build Coastguard Worker }
248*8542734aSAndroid Build Coastguard Worker 
bandwidthSetGlobalAlert(int64_t bytes)249*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::bandwidthSetGlobalAlert(int64_t bytes) {
250*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->bandwidthCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
251*8542734aSAndroid Build Coastguard Worker     int res = gCtls->bandwidthCtrl.setGlobalAlert(bytes);
252*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
253*8542734aSAndroid Build Coastguard Worker }
254*8542734aSAndroid Build Coastguard Worker 
bandwidthAddNaughtyApp(int32_t)255*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::bandwidthAddNaughtyApp(int32_t) {
256*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
257*8542734aSAndroid Build Coastguard Worker }
258*8542734aSAndroid Build Coastguard Worker 
bandwidthRemoveNaughtyApp(int32_t)259*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::bandwidthRemoveNaughtyApp(int32_t) {
260*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
261*8542734aSAndroid Build Coastguard Worker }
262*8542734aSAndroid Build Coastguard Worker 
bandwidthAddNiceApp(int32_t)263*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::bandwidthAddNiceApp(int32_t) {
264*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
265*8542734aSAndroid Build Coastguard Worker }
266*8542734aSAndroid Build Coastguard Worker 
bandwidthRemoveNiceApp(int32_t)267*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::bandwidthRemoveNiceApp(int32_t) {
268*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
269*8542734aSAndroid Build Coastguard Worker }
270*8542734aSAndroid Build Coastguard Worker 
271*8542734aSAndroid Build Coastguard Worker // TODO: Remove this function when there are no users. Currently, it is still used by DNS resolver
272*8542734aSAndroid Build Coastguard Worker // tests.
networkCreatePhysical(int32_t netId,int32_t permission)273*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkCreatePhysical(int32_t netId, int32_t permission) {
274*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
275*8542734aSAndroid Build Coastguard Worker     int ret = gCtls->netCtrl.createPhysicalNetwork(netId, convertPermission(permission),
276*8542734aSAndroid Build Coastguard Worker                                                    false /* local */);
277*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(ret);
278*8542734aSAndroid Build Coastguard Worker }
279*8542734aSAndroid Build Coastguard Worker 
280*8542734aSAndroid Build Coastguard Worker // TODO: Remove this function when there are no users. Currently, it is still used by DNS resolver
281*8542734aSAndroid Build Coastguard Worker // tests.
networkCreateVpn(int32_t netId,bool secure)282*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkCreateVpn(int32_t netId, bool secure) {
283*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
284*8542734aSAndroid Build Coastguard Worker     // The value of vpnType does not matter here, because it is not used in AOSP and is only
285*8542734aSAndroid Build Coastguard Worker     // implemented by OEMs. Also, the RPC is going to deprecate. Just pick a value defined in INetd
286*8542734aSAndroid Build Coastguard Worker     // as default.
287*8542734aSAndroid Build Coastguard Worker     int ret = gCtls->netCtrl.createVirtualNetwork(netId, secure, NativeVpnType::LEGACY,
288*8542734aSAndroid Build Coastguard Worker                                                   false /* excludeLocalRoutes */);
289*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(ret);
290*8542734aSAndroid Build Coastguard Worker }
291*8542734aSAndroid Build Coastguard Worker 
networkCreate(const NativeNetworkConfig & config)292*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkCreate(const NativeNetworkConfig& config) {
293*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
294*8542734aSAndroid Build Coastguard Worker     int ret = -EINVAL;
295*8542734aSAndroid Build Coastguard Worker     if (config.networkType == NativeNetworkType::PHYSICAL) {
296*8542734aSAndroid Build Coastguard Worker         ret = gCtls->netCtrl.createPhysicalNetwork(
297*8542734aSAndroid Build Coastguard Worker                 config.netId, convertPermission(config.permission), false /* isLocalNetwork */);
298*8542734aSAndroid Build Coastguard Worker     } else if (config.networkType == NativeNetworkType::PHYSICAL_LOCAL) {
299*8542734aSAndroid Build Coastguard Worker         ret = gCtls->netCtrl.createPhysicalNetwork(
300*8542734aSAndroid Build Coastguard Worker                 config.netId, convertPermission(config.permission), true /* isLocalNetwork */);
301*8542734aSAndroid Build Coastguard Worker     } else if (config.networkType == NativeNetworkType::VIRTUAL) {
302*8542734aSAndroid Build Coastguard Worker         ret = gCtls->netCtrl.createVirtualNetwork(config.netId, config.secure, config.vpnType,
303*8542734aSAndroid Build Coastguard Worker                                                   config.excludeLocalRoutes);
304*8542734aSAndroid Build Coastguard Worker     }
305*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(ret);
306*8542734aSAndroid Build Coastguard Worker }
307*8542734aSAndroid Build Coastguard Worker 
networkDestroy(int32_t netId)308*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkDestroy(int32_t netId) {
309*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
310*8542734aSAndroid Build Coastguard Worker     // NetworkController::destroyNetwork is thread-safe.
311*8542734aSAndroid Build Coastguard Worker     const int ret = gCtls->netCtrl.destroyNetwork(netId);
312*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(ret);
313*8542734aSAndroid Build Coastguard Worker }
314*8542734aSAndroid Build Coastguard Worker 
networkAddInterface(int32_t netId,const std::string & iface)315*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkAddInterface(int32_t netId, const std::string& iface) {
316*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
317*8542734aSAndroid Build Coastguard Worker     int ret = gCtls->netCtrl.addInterfaceToNetwork(netId, iface.c_str());
318*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(ret);
319*8542734aSAndroid Build Coastguard Worker }
320*8542734aSAndroid Build Coastguard Worker 
networkRemoveInterface(int32_t netId,const std::string & iface)321*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkRemoveInterface(int32_t netId, const std::string& iface) {
322*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
323*8542734aSAndroid Build Coastguard Worker     int ret = gCtls->netCtrl.removeInterfaceFromNetwork(netId, iface.c_str());
324*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(ret);
325*8542734aSAndroid Build Coastguard Worker }
326*8542734aSAndroid Build Coastguard Worker 
networkAddUidRanges(int32_t netId,const std::vector<UidRangeParcel> & uidRangeArray)327*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkAddUidRanges(
328*8542734aSAndroid Build Coastguard Worker         int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
329*8542734aSAndroid Build Coastguard Worker     // NetworkController::addUsersToNetwork is thread-safe.
330*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
331*8542734aSAndroid Build Coastguard Worker     int ret = gCtls->netCtrl.addUsersToNetwork(netId, UidRanges(uidRangeArray),
332*8542734aSAndroid Build Coastguard Worker                                                UidRanges::SUB_PRIORITY_HIGHEST);
333*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(ret);
334*8542734aSAndroid Build Coastguard Worker }
335*8542734aSAndroid Build Coastguard Worker 
networkRemoveUidRanges(int32_t netId,const std::vector<UidRangeParcel> & uidRangeArray)336*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkRemoveUidRanges(
337*8542734aSAndroid Build Coastguard Worker         int32_t netId, const std::vector<UidRangeParcel>& uidRangeArray) {
338*8542734aSAndroid Build Coastguard Worker     // NetworkController::removeUsersFromNetwork is thread-safe.
339*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
340*8542734aSAndroid Build Coastguard Worker     int ret = gCtls->netCtrl.removeUsersFromNetwork(netId, UidRanges(uidRangeArray),
341*8542734aSAndroid Build Coastguard Worker                                                     UidRanges::SUB_PRIORITY_HIGHEST);
342*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(ret);
343*8542734aSAndroid Build Coastguard Worker }
344*8542734aSAndroid Build Coastguard Worker 
networkAddUidRangesParcel(const NativeUidRangeConfig & config)345*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkAddUidRangesParcel(const NativeUidRangeConfig& config) {
346*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
347*8542734aSAndroid Build Coastguard Worker     int ret = gCtls->netCtrl.addUsersToNetwork(config.netId, UidRanges(config.uidRanges),
348*8542734aSAndroid Build Coastguard Worker                                                config.subPriority);
349*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(ret);
350*8542734aSAndroid Build Coastguard Worker }
351*8542734aSAndroid Build Coastguard Worker 
networkRemoveUidRangesParcel(const NativeUidRangeConfig & config)352*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkRemoveUidRangesParcel(const NativeUidRangeConfig& config) {
353*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
354*8542734aSAndroid Build Coastguard Worker     int ret = gCtls->netCtrl.removeUsersFromNetwork(config.netId, UidRanges(config.uidRanges),
355*8542734aSAndroid Build Coastguard Worker                                                     config.subPriority);
356*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(ret);
357*8542734aSAndroid Build Coastguard Worker }
358*8542734aSAndroid Build Coastguard Worker 
networkRejectNonSecureVpn(bool add,const std::vector<UidRangeParcel> & uidRangeArray)359*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkRejectNonSecureVpn(
360*8542734aSAndroid Build Coastguard Worker         bool add, const std::vector<UidRangeParcel>& uidRangeArray) {
361*8542734aSAndroid Build Coastguard Worker     // TODO: elsewhere RouteController is only used from the tethering and network controllers, so
362*8542734aSAndroid Build Coastguard Worker     // it should be possible to use the same lock as NetworkController. However, every call through
363*8542734aSAndroid Build Coastguard Worker     // the CommandListener "network" command will need to hold this lock too, not just the ones that
364*8542734aSAndroid Build Coastguard Worker     // read/modify network internal state (that is sufficient for ::dump() because it doesn't
365*8542734aSAndroid Build Coastguard Worker     // look at routes, but it's not enough here).
366*8542734aSAndroid Build Coastguard Worker     NETD_BIG_LOCK_RPC(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
367*8542734aSAndroid Build Coastguard Worker     UidRanges uidRanges(uidRangeArray);
368*8542734aSAndroid Build Coastguard Worker 
369*8542734aSAndroid Build Coastguard Worker     int err;
370*8542734aSAndroid Build Coastguard Worker     if (add) {
371*8542734aSAndroid Build Coastguard Worker         err = RouteController::addUsersToRejectNonSecureNetworkRule(uidRanges);
372*8542734aSAndroid Build Coastguard Worker     } else {
373*8542734aSAndroid Build Coastguard Worker         err = RouteController::removeUsersFromRejectNonSecureNetworkRule(uidRanges);
374*8542734aSAndroid Build Coastguard Worker     }
375*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(err);
376*8542734aSAndroid Build Coastguard Worker }
377*8542734aSAndroid Build Coastguard Worker 
socketDestroy(const std::vector<UidRangeParcel> & uids,const std::vector<int32_t> & skipUids)378*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::socketDestroy(const std::vector<UidRangeParcel>& uids,
379*8542734aSAndroid Build Coastguard Worker                                                 const std::vector<int32_t>& skipUids) {
380*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
381*8542734aSAndroid Build Coastguard Worker 
382*8542734aSAndroid Build Coastguard Worker     SockDiag sd;
383*8542734aSAndroid Build Coastguard Worker     if (!sd.open()) {
384*8542734aSAndroid Build Coastguard Worker         return binder::Status::fromServiceSpecificError(EIO,
385*8542734aSAndroid Build Coastguard Worker                 String8("Could not open SOCK_DIAG socket"));
386*8542734aSAndroid Build Coastguard Worker     }
387*8542734aSAndroid Build Coastguard Worker 
388*8542734aSAndroid Build Coastguard Worker     UidRanges uidRanges(uids);
389*8542734aSAndroid Build Coastguard Worker     int err = sd.destroySockets(uidRanges, std::set<uid_t>(skipUids.begin(), skipUids.end()),
390*8542734aSAndroid Build Coastguard Worker                                 true /* excludeLoopback */);
391*8542734aSAndroid Build Coastguard Worker     if (err) {
392*8542734aSAndroid Build Coastguard Worker         return binder::Status::fromServiceSpecificError(-err,
393*8542734aSAndroid Build Coastguard Worker                 String8::format("destroySockets: %s", strerror(-err)));
394*8542734aSAndroid Build Coastguard Worker     }
395*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
396*8542734aSAndroid Build Coastguard Worker }
397*8542734aSAndroid Build Coastguard Worker 
tetherApplyDnsInterfaces(bool * ret)398*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherApplyDnsInterfaces(bool *ret) {
399*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
400*8542734aSAndroid Build Coastguard Worker     *ret = gCtls->tetherCtrl.applyDnsInterfaces();
401*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
402*8542734aSAndroid Build Coastguard Worker }
403*8542734aSAndroid Build Coastguard Worker 
404*8542734aSAndroid Build Coastguard Worker namespace {
405*8542734aSAndroid Build Coastguard Worker 
406*8542734aSAndroid Build Coastguard Worker constexpr const int UNUSED_IFINDEX = 0;
407*8542734aSAndroid Build Coastguard Worker 
tetherAddStatsByInterface(TetherController::TetherStats * tetherStatsParcel,const TetherController::TetherStats & tetherStats)408*8542734aSAndroid Build Coastguard Worker void tetherAddStatsByInterface(TetherController::TetherStats* tetherStatsParcel,
409*8542734aSAndroid Build Coastguard Worker                                const TetherController::TetherStats& tetherStats) {
410*8542734aSAndroid Build Coastguard Worker     if (tetherStatsParcel->extIface == tetherStats.extIface) {
411*8542734aSAndroid Build Coastguard Worker         tetherStatsParcel->rxBytes += tetherStats.rxBytes;
412*8542734aSAndroid Build Coastguard Worker         tetherStatsParcel->rxPackets += tetherStats.rxPackets;
413*8542734aSAndroid Build Coastguard Worker         tetherStatsParcel->txBytes += tetherStats.txBytes;
414*8542734aSAndroid Build Coastguard Worker         tetherStatsParcel->txPackets += tetherStats.txPackets;
415*8542734aSAndroid Build Coastguard Worker     }
416*8542734aSAndroid Build Coastguard Worker }
417*8542734aSAndroid Build Coastguard Worker 
toTetherStatsParcel(const TetherController::TetherStats & stats)418*8542734aSAndroid Build Coastguard Worker TetherStatsParcel toTetherStatsParcel(const TetherController::TetherStats& stats) {
419*8542734aSAndroid Build Coastguard Worker     TetherStatsParcel result;
420*8542734aSAndroid Build Coastguard Worker     result.iface = stats.extIface;
421*8542734aSAndroid Build Coastguard Worker     result.rxBytes = stats.rxBytes;
422*8542734aSAndroid Build Coastguard Worker     result.rxPackets = stats.rxPackets;
423*8542734aSAndroid Build Coastguard Worker     result.txBytes = stats.txBytes;
424*8542734aSAndroid Build Coastguard Worker     result.txPackets = stats.txPackets;
425*8542734aSAndroid Build Coastguard Worker     result.ifIndex = UNUSED_IFINDEX;
426*8542734aSAndroid Build Coastguard Worker     return result;
427*8542734aSAndroid Build Coastguard Worker }
428*8542734aSAndroid Build Coastguard Worker 
setTetherStatsParcelVecByInterface(std::vector<TetherStatsParcel> * tetherStatsVec,const TetherController::TetherStatsList & statsList)429*8542734aSAndroid Build Coastguard Worker void setTetherStatsParcelVecByInterface(std::vector<TetherStatsParcel>* tetherStatsVec,
430*8542734aSAndroid Build Coastguard Worker                                         const TetherController::TetherStatsList& statsList) {
431*8542734aSAndroid Build Coastguard Worker     std::map<std::string, TetherController::TetherStats> statsMap;
432*8542734aSAndroid Build Coastguard Worker     for (const auto& stats : statsList) {
433*8542734aSAndroid Build Coastguard Worker         auto iter = statsMap.find(stats.extIface);
434*8542734aSAndroid Build Coastguard Worker         if (iter != statsMap.end()) {
435*8542734aSAndroid Build Coastguard Worker             tetherAddStatsByInterface(&(iter->second), stats);
436*8542734aSAndroid Build Coastguard Worker         } else {
437*8542734aSAndroid Build Coastguard Worker             statsMap.insert(
438*8542734aSAndroid Build Coastguard Worker                     std::pair<std::string, TetherController::TetherStats>(stats.extIface, stats));
439*8542734aSAndroid Build Coastguard Worker         }
440*8542734aSAndroid Build Coastguard Worker     }
441*8542734aSAndroid Build Coastguard Worker     for (auto iter = statsMap.begin(); iter != statsMap.end(); iter++) {
442*8542734aSAndroid Build Coastguard Worker         tetherStatsVec->push_back(toTetherStatsParcel(iter->second));
443*8542734aSAndroid Build Coastguard Worker     }
444*8542734aSAndroid Build Coastguard Worker }
445*8542734aSAndroid Build Coastguard Worker 
tetherStatsParcelVecToStringVec(std::vector<TetherStatsParcel> * tVec)446*8542734aSAndroid Build Coastguard Worker std::vector<std::string> tetherStatsParcelVecToStringVec(std::vector<TetherStatsParcel>* tVec) {
447*8542734aSAndroid Build Coastguard Worker     std::vector<std::string> result;
448*8542734aSAndroid Build Coastguard Worker     for (const auto& t : *tVec) {
449*8542734aSAndroid Build Coastguard Worker         result.push_back(StringPrintf("%s:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64,
450*8542734aSAndroid Build Coastguard Worker                                       t.iface.c_str(), t.rxBytes, t.rxPackets, t.txBytes,
451*8542734aSAndroid Build Coastguard Worker                                       t.txPackets));
452*8542734aSAndroid Build Coastguard Worker     }
453*8542734aSAndroid Build Coastguard Worker     return result;
454*8542734aSAndroid Build Coastguard Worker }
455*8542734aSAndroid Build Coastguard Worker 
456*8542734aSAndroid Build Coastguard Worker }  // namespace
457*8542734aSAndroid Build Coastguard Worker 
tetherGetStats(std::vector<TetherStatsParcel> * tetherStatsParcelVec)458*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherGetStats(
459*8542734aSAndroid Build Coastguard Worker         std::vector<TetherStatsParcel>* tetherStatsParcelVec) {
460*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
461*8542734aSAndroid Build Coastguard Worker     const auto& statsList = gCtls->tetherCtrl.getTetherStats();
462*8542734aSAndroid Build Coastguard Worker     if (!isOk(statsList)) {
463*8542734aSAndroid Build Coastguard Worker         return asBinderStatus(statsList);
464*8542734aSAndroid Build Coastguard Worker     }
465*8542734aSAndroid Build Coastguard Worker     setTetherStatsParcelVecByInterface(tetherStatsParcelVec, statsList.value());
466*8542734aSAndroid Build Coastguard Worker     auto statsResults = tetherStatsParcelVecToStringVec(tetherStatsParcelVec);
467*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
468*8542734aSAndroid Build Coastguard Worker }
469*8542734aSAndroid Build Coastguard Worker 
interfaceAddAddress(const std::string & ifName,const std::string & addrString,int prefixLength)470*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::interfaceAddAddress(const std::string &ifName,
471*8542734aSAndroid Build Coastguard Worker         const std::string &addrString, int prefixLength) {
472*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
473*8542734aSAndroid Build Coastguard Worker     const int err = InterfaceController::addAddress(
474*8542734aSAndroid Build Coastguard Worker             ifName.c_str(), addrString.c_str(), prefixLength);
475*8542734aSAndroid Build Coastguard Worker     if (err != 0) {
476*8542734aSAndroid Build Coastguard Worker         return binder::Status::fromServiceSpecificError(-err,
477*8542734aSAndroid Build Coastguard Worker                 String8::format("InterfaceController error: %s", strerror(-err)));
478*8542734aSAndroid Build Coastguard Worker     }
479*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
480*8542734aSAndroid Build Coastguard Worker }
481*8542734aSAndroid Build Coastguard Worker 
interfaceDelAddress(const std::string & ifName,const std::string & addrString,int prefixLength)482*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::interfaceDelAddress(const std::string &ifName,
483*8542734aSAndroid Build Coastguard Worker         const std::string &addrString, int prefixLength) {
484*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
485*8542734aSAndroid Build Coastguard Worker     const int err = InterfaceController::delAddress(
486*8542734aSAndroid Build Coastguard Worker             ifName.c_str(), addrString.c_str(), prefixLength);
487*8542734aSAndroid Build Coastguard Worker     if (err != 0) {
488*8542734aSAndroid Build Coastguard Worker         return binder::Status::fromServiceSpecificError(-err,
489*8542734aSAndroid Build Coastguard Worker                 String8::format("InterfaceController error: %s", strerror(-err)));
490*8542734aSAndroid Build Coastguard Worker     }
491*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
492*8542734aSAndroid Build Coastguard Worker }
493*8542734aSAndroid Build Coastguard Worker 
494*8542734aSAndroid Build Coastguard Worker namespace {
495*8542734aSAndroid Build Coastguard Worker 
getPathComponents(int32_t ipversion,int32_t category)496*8542734aSAndroid Build Coastguard Worker std::tuple<binder::Status, const char*, const char*> getPathComponents(int32_t ipversion,
497*8542734aSAndroid Build Coastguard Worker                                                                        int32_t category) {
498*8542734aSAndroid Build Coastguard Worker     const char* ipversionStr = nullptr;
499*8542734aSAndroid Build Coastguard Worker     switch (ipversion) {
500*8542734aSAndroid Build Coastguard Worker         case INetd::IPV4:
501*8542734aSAndroid Build Coastguard Worker             ipversionStr = "ipv4";
502*8542734aSAndroid Build Coastguard Worker             break;
503*8542734aSAndroid Build Coastguard Worker         case INetd::IPV6:
504*8542734aSAndroid Build Coastguard Worker             ipversionStr = "ipv6";
505*8542734aSAndroid Build Coastguard Worker             break;
506*8542734aSAndroid Build Coastguard Worker         default:
507*8542734aSAndroid Build Coastguard Worker             return {binder::Status::fromServiceSpecificError(EAFNOSUPPORT, "Bad IP version"),
508*8542734aSAndroid Build Coastguard Worker                     nullptr, nullptr};
509*8542734aSAndroid Build Coastguard Worker     }
510*8542734aSAndroid Build Coastguard Worker 
511*8542734aSAndroid Build Coastguard Worker     const char* whichStr = nullptr;
512*8542734aSAndroid Build Coastguard Worker     switch (category) {
513*8542734aSAndroid Build Coastguard Worker         case INetd::CONF:
514*8542734aSAndroid Build Coastguard Worker             whichStr = "conf";
515*8542734aSAndroid Build Coastguard Worker             break;
516*8542734aSAndroid Build Coastguard Worker         case INetd::NEIGH:
517*8542734aSAndroid Build Coastguard Worker             whichStr = "neigh";
518*8542734aSAndroid Build Coastguard Worker             break;
519*8542734aSAndroid Build Coastguard Worker         default:
520*8542734aSAndroid Build Coastguard Worker             return {binder::Status::fromServiceSpecificError(EINVAL, "Bad category"), nullptr,
521*8542734aSAndroid Build Coastguard Worker                     nullptr};
522*8542734aSAndroid Build Coastguard Worker     }
523*8542734aSAndroid Build Coastguard Worker 
524*8542734aSAndroid Build Coastguard Worker     return {binder::Status::ok(), ipversionStr, whichStr};
525*8542734aSAndroid Build Coastguard Worker }
526*8542734aSAndroid Build Coastguard Worker 
527*8542734aSAndroid Build Coastguard Worker }  // namespace
528*8542734aSAndroid Build Coastguard Worker 
getProcSysNet(int32_t ipversion,int32_t which,const std::string & ifname,const std::string & parameter,std::string * value)529*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::getProcSysNet(int32_t ipversion, int32_t which,
530*8542734aSAndroid Build Coastguard Worker                                                 const std::string& ifname,
531*8542734aSAndroid Build Coastguard Worker                                                 const std::string& parameter, std::string* value) {
532*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
533*8542734aSAndroid Build Coastguard Worker     const auto pathParts = getPathComponents(ipversion, which);
534*8542734aSAndroid Build Coastguard Worker     const auto& pathStatus = std::get<0>(pathParts);
535*8542734aSAndroid Build Coastguard Worker     if (!pathStatus.isOk()) {
536*8542734aSAndroid Build Coastguard Worker         return pathStatus;
537*8542734aSAndroid Build Coastguard Worker     }
538*8542734aSAndroid Build Coastguard Worker 
539*8542734aSAndroid Build Coastguard Worker     const int err = InterfaceController::getParameter(std::get<1>(pathParts),
540*8542734aSAndroid Build Coastguard Worker                                                       std::get<2>(pathParts), ifname.c_str(),
541*8542734aSAndroid Build Coastguard Worker                                                       parameter.c_str(), value);
542*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(err);
543*8542734aSAndroid Build Coastguard Worker }
544*8542734aSAndroid Build Coastguard Worker 
setProcSysNet(int32_t ipversion,int32_t which,const std::string & ifname,const std::string & parameter,const std::string & value)545*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::setProcSysNet(int32_t ipversion, int32_t which,
546*8542734aSAndroid Build Coastguard Worker                                                 const std::string& ifname,
547*8542734aSAndroid Build Coastguard Worker                                                 const std::string& parameter,
548*8542734aSAndroid Build Coastguard Worker                                                 const std::string& value) {
549*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
550*8542734aSAndroid Build Coastguard Worker     const auto pathParts = getPathComponents(ipversion, which);
551*8542734aSAndroid Build Coastguard Worker     const auto& pathStatus = std::get<0>(pathParts);
552*8542734aSAndroid Build Coastguard Worker     if (!pathStatus.isOk()) {
553*8542734aSAndroid Build Coastguard Worker         return pathStatus;
554*8542734aSAndroid Build Coastguard Worker     }
555*8542734aSAndroid Build Coastguard Worker 
556*8542734aSAndroid Build Coastguard Worker     const int err = InterfaceController::setParameter(std::get<1>(pathParts),
557*8542734aSAndroid Build Coastguard Worker                                                       std::get<2>(pathParts), ifname.c_str(),
558*8542734aSAndroid Build Coastguard Worker                                                       parameter.c_str(), value.c_str());
559*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(err);
560*8542734aSAndroid Build Coastguard Worker }
561*8542734aSAndroid Build Coastguard Worker 
ipSecSetEncapSocketOwner(const ParcelFileDescriptor & socket,int newUid)562*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipSecSetEncapSocketOwner(const ParcelFileDescriptor& socket,
563*8542734aSAndroid Build Coastguard Worker                                                            int newUid) {
564*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
565*8542734aSAndroid Build Coastguard Worker 
566*8542734aSAndroid Build Coastguard Worker     uid_t callerUid = IPCThreadState::self()->getCallingUid();
567*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(
568*8542734aSAndroid Build Coastguard Worker             gCtls->xfrmCtrl.ipSecSetEncapSocketOwner(socket.get(), newUid, callerUid));
569*8542734aSAndroid Build Coastguard Worker }
570*8542734aSAndroid Build Coastguard Worker 
ipSecAllocateSpi(int32_t transformId,const std::string & sourceAddress,const std::string & destinationAddress,int32_t inSpi,int32_t * outSpi)571*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipSecAllocateSpi(
572*8542734aSAndroid Build Coastguard Worker         int32_t transformId,
573*8542734aSAndroid Build Coastguard Worker         const std::string& sourceAddress,
574*8542734aSAndroid Build Coastguard Worker         const std::string& destinationAddress,
575*8542734aSAndroid Build Coastguard Worker         int32_t inSpi,
576*8542734aSAndroid Build Coastguard Worker         int32_t* outSpi) {
577*8542734aSAndroid Build Coastguard Worker     // Necessary locking done in IpSecService and kernel
578*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
579*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(gCtls->xfrmCtrl.ipSecAllocateSpi(
580*8542734aSAndroid Build Coastguard Worker                     transformId,
581*8542734aSAndroid Build Coastguard Worker                     sourceAddress,
582*8542734aSAndroid Build Coastguard Worker                     destinationAddress,
583*8542734aSAndroid Build Coastguard Worker                     inSpi,
584*8542734aSAndroid Build Coastguard Worker                     outSpi));
585*8542734aSAndroid Build Coastguard Worker }
586*8542734aSAndroid Build Coastguard Worker 
ipSecAddSecurityAssociation(int32_t transformId,int32_t mode,const std::string & sourceAddress,const std::string & destinationAddress,int32_t underlyingNetId,int32_t spi,int32_t markValue,int32_t markMask,const std::string & authAlgo,const std::vector<uint8_t> & authKey,int32_t authTruncBits,const std::string & cryptAlgo,const std::vector<uint8_t> & cryptKey,int32_t cryptTruncBits,const std::string & aeadAlgo,const std::vector<uint8_t> & aeadKey,int32_t aeadIcvBits,int32_t encapType,int32_t encapLocalPort,int32_t encapRemotePort,int32_t interfaceId)587*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipSecAddSecurityAssociation(
588*8542734aSAndroid Build Coastguard Worker         int32_t transformId, int32_t mode, const std::string& sourceAddress,
589*8542734aSAndroid Build Coastguard Worker         const std::string& destinationAddress, int32_t underlyingNetId, int32_t spi,
590*8542734aSAndroid Build Coastguard Worker         int32_t markValue, int32_t markMask, const std::string& authAlgo,
591*8542734aSAndroid Build Coastguard Worker         const std::vector<uint8_t>& authKey, int32_t authTruncBits, const std::string& cryptAlgo,
592*8542734aSAndroid Build Coastguard Worker         const std::vector<uint8_t>& cryptKey, int32_t cryptTruncBits, const std::string& aeadAlgo,
593*8542734aSAndroid Build Coastguard Worker         const std::vector<uint8_t>& aeadKey, int32_t aeadIcvBits, int32_t encapType,
594*8542734aSAndroid Build Coastguard Worker         int32_t encapLocalPort, int32_t encapRemotePort, int32_t interfaceId) {
595*8542734aSAndroid Build Coastguard Worker     // Necessary locking done in IpSecService and kernel
596*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
597*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityAssociation(
598*8542734aSAndroid Build Coastguard Worker             transformId, mode, sourceAddress, destinationAddress, underlyingNetId, spi, markValue,
599*8542734aSAndroid Build Coastguard Worker             markMask, authAlgo, authKey, authTruncBits, cryptAlgo, cryptKey, cryptTruncBits,
600*8542734aSAndroid Build Coastguard Worker             aeadAlgo, aeadKey, aeadIcvBits, encapType, encapLocalPort, encapRemotePort,
601*8542734aSAndroid Build Coastguard Worker             interfaceId));
602*8542734aSAndroid Build Coastguard Worker }
603*8542734aSAndroid Build Coastguard Worker 
ipSecDeleteSecurityAssociation(int32_t transformId,const std::string & sourceAddress,const std::string & destinationAddress,int32_t spi,int32_t markValue,int32_t markMask,int32_t interfaceId)604*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipSecDeleteSecurityAssociation(
605*8542734aSAndroid Build Coastguard Worker         int32_t transformId, const std::string& sourceAddress,
606*8542734aSAndroid Build Coastguard Worker         const std::string& destinationAddress, int32_t spi, int32_t markValue, int32_t markMask,
607*8542734aSAndroid Build Coastguard Worker         int32_t interfaceId) {
608*8542734aSAndroid Build Coastguard Worker     // Necessary locking done in IpSecService and kernel
609*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
610*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityAssociation(
611*8542734aSAndroid Build Coastguard Worker             transformId, sourceAddress, destinationAddress, spi, markValue, markMask, interfaceId));
612*8542734aSAndroid Build Coastguard Worker }
613*8542734aSAndroid Build Coastguard Worker 
ipSecApplyTransportModeTransform(const ParcelFileDescriptor & socket,int32_t transformId,int32_t direction,const std::string & sourceAddress,const std::string & destinationAddress,int32_t spi)614*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipSecApplyTransportModeTransform(
615*8542734aSAndroid Build Coastguard Worker         const ParcelFileDescriptor& socket, int32_t transformId, int32_t direction,
616*8542734aSAndroid Build Coastguard Worker         const std::string& sourceAddress, const std::string& destinationAddress, int32_t spi) {
617*8542734aSAndroid Build Coastguard Worker     // Necessary locking done in IpSecService and kernel
618*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
619*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(gCtls->xfrmCtrl.ipSecApplyTransportModeTransform(
620*8542734aSAndroid Build Coastguard Worker             socket.get(), transformId, direction, sourceAddress, destinationAddress, spi));
621*8542734aSAndroid Build Coastguard Worker }
622*8542734aSAndroid Build Coastguard Worker 
ipSecRemoveTransportModeTransform(const ParcelFileDescriptor & socket)623*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipSecRemoveTransportModeTransform(
624*8542734aSAndroid Build Coastguard Worker         const ParcelFileDescriptor& socket) {
625*8542734aSAndroid Build Coastguard Worker     // Necessary locking done in IpSecService and kernel
626*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
627*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTransportModeTransform(socket.get()));
628*8542734aSAndroid Build Coastguard Worker }
629*8542734aSAndroid Build Coastguard Worker 
ipSecAddSecurityPolicy(int32_t transformId,int32_t selAddrFamily,int32_t direction,const std::string & tmplSrcAddress,const std::string & tmplDstAddress,int32_t spi,int32_t markValue,int32_t markMask,int32_t interfaceId)630*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipSecAddSecurityPolicy(int32_t transformId, int32_t selAddrFamily,
631*8542734aSAndroid Build Coastguard Worker                                                          int32_t direction,
632*8542734aSAndroid Build Coastguard Worker                                                          const std::string& tmplSrcAddress,
633*8542734aSAndroid Build Coastguard Worker                                                          const std::string& tmplDstAddress,
634*8542734aSAndroid Build Coastguard Worker                                                          int32_t spi, int32_t markValue,
635*8542734aSAndroid Build Coastguard Worker                                                          int32_t markMask, int32_t interfaceId) {
636*8542734aSAndroid Build Coastguard Worker     // Necessary locking done in IpSecService and kernel
637*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
638*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(gCtls->xfrmCtrl.ipSecAddSecurityPolicy(
639*8542734aSAndroid Build Coastguard Worker             transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
640*8542734aSAndroid Build Coastguard Worker             markMask, interfaceId));
641*8542734aSAndroid Build Coastguard Worker }
642*8542734aSAndroid Build Coastguard Worker 
ipSecUpdateSecurityPolicy(int32_t transformId,int32_t selAddrFamily,int32_t direction,const std::string & tmplSrcAddress,const std::string & tmplDstAddress,int32_t spi,int32_t markValue,int32_t markMask,int32_t interfaceId)643*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipSecUpdateSecurityPolicy(
644*8542734aSAndroid Build Coastguard Worker         int32_t transformId, int32_t selAddrFamily, int32_t direction,
645*8542734aSAndroid Build Coastguard Worker         const std::string& tmplSrcAddress, const std::string& tmplDstAddress, int32_t spi,
646*8542734aSAndroid Build Coastguard Worker         int32_t markValue, int32_t markMask, int32_t interfaceId) {
647*8542734aSAndroid Build Coastguard Worker     // Necessary locking done in IpSecService and kernel
648*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
649*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(gCtls->xfrmCtrl.ipSecUpdateSecurityPolicy(
650*8542734aSAndroid Build Coastguard Worker             transformId, selAddrFamily, direction, tmplSrcAddress, tmplDstAddress, spi, markValue,
651*8542734aSAndroid Build Coastguard Worker             markMask, interfaceId));
652*8542734aSAndroid Build Coastguard Worker }
653*8542734aSAndroid Build Coastguard Worker 
ipSecDeleteSecurityPolicy(int32_t transformId,int32_t selAddrFamily,int32_t direction,int32_t markValue,int32_t markMask,int32_t interfaceId)654*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipSecDeleteSecurityPolicy(int32_t transformId,
655*8542734aSAndroid Build Coastguard Worker                                                             int32_t selAddrFamily,
656*8542734aSAndroid Build Coastguard Worker                                                             int32_t direction, int32_t markValue,
657*8542734aSAndroid Build Coastguard Worker                                                             int32_t markMask, int32_t interfaceId) {
658*8542734aSAndroid Build Coastguard Worker     // Necessary locking done in IpSecService and kernel
659*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
660*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(gCtls->xfrmCtrl.ipSecDeleteSecurityPolicy(
661*8542734aSAndroid Build Coastguard Worker             transformId, selAddrFamily, direction, markValue, markMask, interfaceId));
662*8542734aSAndroid Build Coastguard Worker }
663*8542734aSAndroid Build Coastguard Worker 
ipSecAddTunnelInterface(const std::string & deviceName,const std::string & localAddress,const std::string & remoteAddress,int32_t iKey,int32_t oKey,int32_t interfaceId)664*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipSecAddTunnelInterface(const std::string& deviceName,
665*8542734aSAndroid Build Coastguard Worker                                                           const std::string& localAddress,
666*8542734aSAndroid Build Coastguard Worker                                                           const std::string& remoteAddress,
667*8542734aSAndroid Build Coastguard Worker                                                           int32_t iKey, int32_t oKey,
668*8542734aSAndroid Build Coastguard Worker                                                           int32_t interfaceId) {
669*8542734aSAndroid Build Coastguard Worker     // Necessary locking done in IpSecService and kernel
670*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
671*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(gCtls->xfrmCtrl.ipSecAddTunnelInterface(
672*8542734aSAndroid Build Coastguard Worker             deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, false));
673*8542734aSAndroid Build Coastguard Worker }
674*8542734aSAndroid Build Coastguard Worker 
ipSecUpdateTunnelInterface(const std::string & deviceName,const std::string & localAddress,const std::string & remoteAddress,int32_t iKey,int32_t oKey,int32_t interfaceId)675*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipSecUpdateTunnelInterface(const std::string& deviceName,
676*8542734aSAndroid Build Coastguard Worker                                                              const std::string& localAddress,
677*8542734aSAndroid Build Coastguard Worker                                                              const std::string& remoteAddress,
678*8542734aSAndroid Build Coastguard Worker                                                              int32_t iKey, int32_t oKey,
679*8542734aSAndroid Build Coastguard Worker                                                              int32_t interfaceId) {
680*8542734aSAndroid Build Coastguard Worker     // Necessary locking done in IpSecService and kernel
681*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
682*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(gCtls->xfrmCtrl.ipSecAddTunnelInterface(
683*8542734aSAndroid Build Coastguard Worker             deviceName, localAddress, remoteAddress, iKey, oKey, interfaceId, true));
684*8542734aSAndroid Build Coastguard Worker }
685*8542734aSAndroid Build Coastguard Worker 
ipSecRemoveTunnelInterface(const std::string & deviceName)686*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipSecRemoveTunnelInterface(const std::string& deviceName) {
687*8542734aSAndroid Build Coastguard Worker     // Necessary locking done in IpSecService and kernel
688*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
689*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(gCtls->xfrmCtrl.ipSecRemoveTunnelInterface(deviceName));
690*8542734aSAndroid Build Coastguard Worker }
691*8542734aSAndroid Build Coastguard Worker 
ipSecMigrate(const IpSecMigrateInfoParcel & migrateInfo)692*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipSecMigrate(const IpSecMigrateInfoParcel& migrateInfo) {
693*8542734aSAndroid Build Coastguard Worker     // Necessary locking done in IpSecService and kernel
694*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
695*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(gCtls->xfrmCtrl.ipSecMigrate(
696*8542734aSAndroid Build Coastguard Worker             migrateInfo.requestId, migrateInfo.selAddrFamily, migrateInfo.direction,
697*8542734aSAndroid Build Coastguard Worker             migrateInfo.oldSourceAddress, migrateInfo.oldDestinationAddress,
698*8542734aSAndroid Build Coastguard Worker             migrateInfo.newSourceAddress, migrateInfo.newDestinationAddress,
699*8542734aSAndroid Build Coastguard Worker             migrateInfo.interfaceId));
700*8542734aSAndroid Build Coastguard Worker }
701*8542734aSAndroid Build Coastguard Worker 
setIPv6AddrGenMode(const std::string & ifName,int32_t mode)702*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::setIPv6AddrGenMode(const std::string& ifName,
703*8542734aSAndroid Build Coastguard Worker                                                      int32_t mode) {
704*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
705*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(InterfaceController::setIPv6AddrGenMode(ifName, mode));
706*8542734aSAndroid Build Coastguard Worker }
707*8542734aSAndroid Build Coastguard Worker 
wakeupAddInterface(const std::string & ifName,const std::string & prefix,int32_t mark,int32_t mask)708*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::wakeupAddInterface(const std::string& ifName,
709*8542734aSAndroid Build Coastguard Worker                                                      const std::string& prefix, int32_t mark,
710*8542734aSAndroid Build Coastguard Worker                                                      int32_t mask) {
711*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
712*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(gCtls->wakeupCtrl.addInterface(ifName, prefix, mark, mask));
713*8542734aSAndroid Build Coastguard Worker }
714*8542734aSAndroid Build Coastguard Worker 
wakeupDelInterface(const std::string & ifName,const std::string & prefix,int32_t mark,int32_t mask)715*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::wakeupDelInterface(const std::string& ifName,
716*8542734aSAndroid Build Coastguard Worker                                                      const std::string& prefix, int32_t mark,
717*8542734aSAndroid Build Coastguard Worker                                                      int32_t mask) {
718*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
719*8542734aSAndroid Build Coastguard Worker     return asBinderStatus(gCtls->wakeupCtrl.delInterface(ifName, prefix, mark, mask));
720*8542734aSAndroid Build Coastguard Worker }
721*8542734aSAndroid Build Coastguard Worker 
trafficSwapActiveStatsMap()722*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::trafficSwapActiveStatsMap() {
723*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
724*8542734aSAndroid Build Coastguard Worker }
725*8542734aSAndroid Build Coastguard Worker 
idletimerAddInterface(const std::string & ifName,int32_t timeout,const std::string & classLabel)726*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::idletimerAddInterface(const std::string& ifName, int32_t timeout,
727*8542734aSAndroid Build Coastguard Worker                                                         const std::string& classLabel) {
728*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->idletimerCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
729*8542734aSAndroid Build Coastguard Worker     int res =
730*8542734aSAndroid Build Coastguard Worker             gCtls->idletimerCtrl.addInterfaceIdletimer(ifName.c_str(), timeout, classLabel.c_str());
731*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
732*8542734aSAndroid Build Coastguard Worker }
733*8542734aSAndroid Build Coastguard Worker 
idletimerRemoveInterface(const std::string & ifName,int32_t timeout,const std::string & classLabel)734*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::idletimerRemoveInterface(const std::string& ifName,
735*8542734aSAndroid Build Coastguard Worker                                                            int32_t timeout,
736*8542734aSAndroid Build Coastguard Worker                                                            const std::string& classLabel) {
737*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->idletimerCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
738*8542734aSAndroid Build Coastguard Worker     int res = gCtls->idletimerCtrl.removeInterfaceIdletimer(ifName.c_str(), timeout,
739*8542734aSAndroid Build Coastguard Worker                                                             classLabel.c_str());
740*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
741*8542734aSAndroid Build Coastguard Worker }
742*8542734aSAndroid Build Coastguard Worker 
strictUidCleartextPenalty(int32_t uid,int32_t policyPenalty)743*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::strictUidCleartextPenalty(int32_t uid, int32_t policyPenalty) {
744*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->strictCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
745*8542734aSAndroid Build Coastguard Worker     StrictPenalty penalty;
746*8542734aSAndroid Build Coastguard Worker     switch (policyPenalty) {
747*8542734aSAndroid Build Coastguard Worker         case INetd::PENALTY_POLICY_REJECT:
748*8542734aSAndroid Build Coastguard Worker             penalty = REJECT;
749*8542734aSAndroid Build Coastguard Worker             break;
750*8542734aSAndroid Build Coastguard Worker         case INetd::PENALTY_POLICY_LOG:
751*8542734aSAndroid Build Coastguard Worker             penalty = LOG;
752*8542734aSAndroid Build Coastguard Worker             break;
753*8542734aSAndroid Build Coastguard Worker         case INetd::PENALTY_POLICY_ACCEPT:
754*8542734aSAndroid Build Coastguard Worker             penalty = ACCEPT;
755*8542734aSAndroid Build Coastguard Worker             break;
756*8542734aSAndroid Build Coastguard Worker         default:
757*8542734aSAndroid Build Coastguard Worker             return statusFromErrcode(-EINVAL);
758*8542734aSAndroid Build Coastguard Worker             break;
759*8542734aSAndroid Build Coastguard Worker     }
760*8542734aSAndroid Build Coastguard Worker     int res = gCtls->strictCtrl.setUidCleartextPenalty((uid_t) uid, penalty);
761*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
762*8542734aSAndroid Build Coastguard Worker }
763*8542734aSAndroid Build Coastguard Worker 
clatdStart(const std::string &,const std::string &,std::string *)764*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::clatdStart(const std::string& /* ifName */,
765*8542734aSAndroid Build Coastguard Worker                                              const std::string& /* nat64Prefix */,
766*8542734aSAndroid Build Coastguard Worker                                              std::string* /* v6Addr */) {
767*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
768*8542734aSAndroid Build Coastguard Worker }
769*8542734aSAndroid Build Coastguard Worker 
clatdStop(const std::string &)770*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::clatdStop(const std::string& /* ifName */) {
771*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
772*8542734aSAndroid Build Coastguard Worker }
773*8542734aSAndroid Build Coastguard Worker 
ipfwdEnabled(bool * status)774*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipfwdEnabled(bool* status) {
775*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
776*8542734aSAndroid Build Coastguard Worker     *status = (gCtls->tetherCtrl.getIpfwdRequesterList().size() > 0) ? true : false;
777*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
778*8542734aSAndroid Build Coastguard Worker }
779*8542734aSAndroid Build Coastguard Worker 
ipfwdGetRequesterList(std::vector<std::string> * requesterList)780*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipfwdGetRequesterList(std::vector<std::string>* requesterList) {
781*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
782*8542734aSAndroid Build Coastguard Worker     for (const auto& requester : gCtls->tetherCtrl.getIpfwdRequesterList()) {
783*8542734aSAndroid Build Coastguard Worker         requesterList->push_back(requester);
784*8542734aSAndroid Build Coastguard Worker     }
785*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
786*8542734aSAndroid Build Coastguard Worker }
787*8542734aSAndroid Build Coastguard Worker 
ipfwdEnableForwarding(const std::string & requester)788*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipfwdEnableForwarding(const std::string& requester) {
789*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
790*8542734aSAndroid Build Coastguard Worker     int res = (gCtls->tetherCtrl.enableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
791*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
792*8542734aSAndroid Build Coastguard Worker }
793*8542734aSAndroid Build Coastguard Worker 
ipfwdDisableForwarding(const std::string & requester)794*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipfwdDisableForwarding(const std::string& requester) {
795*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
796*8542734aSAndroid Build Coastguard Worker     int res = (gCtls->tetherCtrl.disableForwarding(requester.c_str())) ? 0 : -EREMOTEIO;
797*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
798*8542734aSAndroid Build Coastguard Worker }
799*8542734aSAndroid Build Coastguard Worker 
ipfwdAddInterfaceForward(const std::string & fromIface,const std::string & toIface)800*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipfwdAddInterfaceForward(const std::string& fromIface,
801*8542734aSAndroid Build Coastguard Worker                                                            const std::string& toIface) {
802*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
803*8542734aSAndroid Build Coastguard Worker     int res = RouteController::enableTethering(fromIface.c_str(), toIface.c_str());
804*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
805*8542734aSAndroid Build Coastguard Worker }
806*8542734aSAndroid Build Coastguard Worker 
ipfwdRemoveInterfaceForward(const std::string & fromIface,const std::string & toIface)807*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::ipfwdRemoveInterfaceForward(const std::string& fromIface,
808*8542734aSAndroid Build Coastguard Worker                                                               const std::string& toIface) {
809*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
810*8542734aSAndroid Build Coastguard Worker     int res = RouteController::disableTethering(fromIface.c_str(), toIface.c_str());
811*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
812*8542734aSAndroid Build Coastguard Worker }
813*8542734aSAndroid Build Coastguard Worker 
814*8542734aSAndroid Build Coastguard Worker namespace {
815*8542734aSAndroid Build Coastguard Worker 
addCurlyBrackets(const std::string & s)816*8542734aSAndroid Build Coastguard Worker std::string addCurlyBrackets(const std::string& s) {
817*8542734aSAndroid Build Coastguard Worker     return "{" + s + "}";
818*8542734aSAndroid Build Coastguard Worker }
819*8542734aSAndroid Build Coastguard Worker 
820*8542734aSAndroid Build Coastguard Worker }  // namespace
821*8542734aSAndroid Build Coastguard Worker 
interfaceGetList(std::vector<std::string> * interfaceListResult)822*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::interfaceGetList(std::vector<std::string>* interfaceListResult) {
823*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
824*8542734aSAndroid Build Coastguard Worker     const auto& ifaceList = getIfaceNames();
825*8542734aSAndroid Build Coastguard Worker 
826*8542734aSAndroid Build Coastguard Worker     interfaceListResult->clear();
827*8542734aSAndroid Build Coastguard Worker     interfaceListResult->reserve(ifaceList.value().size());
828*8542734aSAndroid Build Coastguard Worker     interfaceListResult->insert(end(*interfaceListResult), begin(ifaceList.value()),
829*8542734aSAndroid Build Coastguard Worker                                 end(ifaceList.value()));
830*8542734aSAndroid Build Coastguard Worker 
831*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
832*8542734aSAndroid Build Coastguard Worker }
833*8542734aSAndroid Build Coastguard Worker 
interfaceConfigurationParcelToString(const InterfaceConfigurationParcel & cfg)834*8542734aSAndroid Build Coastguard Worker std::string interfaceConfigurationParcelToString(const InterfaceConfigurationParcel& cfg) {
835*8542734aSAndroid Build Coastguard Worker     std::vector<std::string> result{cfg.ifName, cfg.hwAddr, cfg.ipv4Addr,
836*8542734aSAndroid Build Coastguard Worker                                     std::to_string(cfg.prefixLength)};
837*8542734aSAndroid Build Coastguard Worker     result.insert(end(result), begin(cfg.flags), end(cfg.flags));
838*8542734aSAndroid Build Coastguard Worker     return addCurlyBrackets(base::Join(result, ", "));
839*8542734aSAndroid Build Coastguard Worker }
840*8542734aSAndroid Build Coastguard Worker 
interfaceGetCfg(const std::string & ifName,InterfaceConfigurationParcel * interfaceGetCfgResult)841*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::interfaceGetCfg(
842*8542734aSAndroid Build Coastguard Worker         const std::string& ifName, InterfaceConfigurationParcel* interfaceGetCfgResult) {
843*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
844*8542734aSAndroid Build Coastguard Worker     auto entry = gLog.newEntry().prettyFunction(__PRETTY_FUNCTION__).arg(ifName);
845*8542734aSAndroid Build Coastguard Worker 
846*8542734aSAndroid Build Coastguard Worker     const auto& cfgRes = InterfaceController::getCfg(ifName);
847*8542734aSAndroid Build Coastguard Worker     RETURN_BINDER_STATUS_IF_NOT_OK(entry, cfgRes);
848*8542734aSAndroid Build Coastguard Worker 
849*8542734aSAndroid Build Coastguard Worker     *interfaceGetCfgResult = cfgRes.value();
850*8542734aSAndroid Build Coastguard Worker     gLog.log(entry.returns(interfaceConfigurationParcelToString(*interfaceGetCfgResult))
851*8542734aSAndroid Build Coastguard Worker                      .withAutomaticDuration());
852*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
853*8542734aSAndroid Build Coastguard Worker }
854*8542734aSAndroid Build Coastguard Worker 
interfaceSetCfg(const InterfaceConfigurationParcel & cfg)855*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::interfaceSetCfg(const InterfaceConfigurationParcel& cfg) {
856*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
857*8542734aSAndroid Build Coastguard Worker     auto entry = gLog.newEntry()
858*8542734aSAndroid Build Coastguard Worker                          .prettyFunction(__PRETTY_FUNCTION__)
859*8542734aSAndroid Build Coastguard Worker                          .arg(interfaceConfigurationParcelToString(cfg));
860*8542734aSAndroid Build Coastguard Worker 
861*8542734aSAndroid Build Coastguard Worker     const auto& res = InterfaceController::setCfg(cfg);
862*8542734aSAndroid Build Coastguard Worker     RETURN_BINDER_STATUS_IF_NOT_OK(entry, res);
863*8542734aSAndroid Build Coastguard Worker 
864*8542734aSAndroid Build Coastguard Worker     gLog.log(entry.withAutomaticDuration());
865*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
866*8542734aSAndroid Build Coastguard Worker }
867*8542734aSAndroid Build Coastguard Worker 
interfaceSetIPv6PrivacyExtensions(const std::string & ifName,bool enable)868*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::interfaceSetIPv6PrivacyExtensions(const std::string& ifName,
869*8542734aSAndroid Build Coastguard Worker                                                                     bool enable) {
870*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
871*8542734aSAndroid Build Coastguard Worker     int res = InterfaceController::setIPv6PrivacyExtensions(ifName.c_str(), enable);
872*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
873*8542734aSAndroid Build Coastguard Worker }
874*8542734aSAndroid Build Coastguard Worker 
interfaceClearAddrs(const std::string & ifName)875*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::interfaceClearAddrs(const std::string& ifName) {
876*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
877*8542734aSAndroid Build Coastguard Worker     int res = InterfaceController::clearAddrs(ifName.c_str());
878*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
879*8542734aSAndroid Build Coastguard Worker }
880*8542734aSAndroid Build Coastguard Worker 
interfaceSetEnableIPv6(const std::string & ifName,bool enable)881*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::interfaceSetEnableIPv6(const std::string& ifName, bool enable) {
882*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
883*8542734aSAndroid Build Coastguard Worker     int res = InterfaceController::setEnableIPv6(ifName.c_str(), enable);
884*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
885*8542734aSAndroid Build Coastguard Worker }
886*8542734aSAndroid Build Coastguard Worker 
interfaceSetMtu(const std::string & ifName,int32_t mtuValue)887*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::interfaceSetMtu(const std::string& ifName, int32_t mtuValue) {
888*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(InterfaceController::mutex, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
889*8542734aSAndroid Build Coastguard Worker     std::string mtu = std::to_string(mtuValue);
890*8542734aSAndroid Build Coastguard Worker     int res = InterfaceController::setMtu(ifName.c_str(), mtu.c_str());
891*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
892*8542734aSAndroid Build Coastguard Worker }
893*8542734aSAndroid Build Coastguard Worker 
tetherStart(const std::vector<std::string> & dhcpRanges)894*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherStart(const std::vector<std::string>& dhcpRanges) {
895*8542734aSAndroid Build Coastguard Worker     TetherConfigParcel config;
896*8542734aSAndroid Build Coastguard Worker     config.usingLegacyDnsProxy = true;
897*8542734aSAndroid Build Coastguard Worker     config.dhcpRanges = dhcpRanges;
898*8542734aSAndroid Build Coastguard Worker     return tetherStartWithConfiguration(config);
899*8542734aSAndroid Build Coastguard Worker }
900*8542734aSAndroid Build Coastguard Worker 
tetherStartWithConfiguration(const TetherConfigParcel & config)901*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherStartWithConfiguration(const TetherConfigParcel& config) {
902*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
903*8542734aSAndroid Build Coastguard Worker     if (config.dhcpRanges.size() % 2 == 1) {
904*8542734aSAndroid Build Coastguard Worker         return statusFromErrcode(-EINVAL);
905*8542734aSAndroid Build Coastguard Worker     }
906*8542734aSAndroid Build Coastguard Worker     // TODO: Pass TetherConfigParcel directly.
907*8542734aSAndroid Build Coastguard Worker     int res = gCtls->tetherCtrl.startTethering(config.usingLegacyDnsProxy, config.dhcpRanges);
908*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
909*8542734aSAndroid Build Coastguard Worker }
910*8542734aSAndroid Build Coastguard Worker 
tetherStop()911*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherStop() {
912*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
913*8542734aSAndroid Build Coastguard Worker     int res = gCtls->tetherCtrl.stopTethering();
914*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
915*8542734aSAndroid Build Coastguard Worker }
916*8542734aSAndroid Build Coastguard Worker 
tetherIsEnabled(bool * enabled)917*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherIsEnabled(bool* enabled) {
918*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
919*8542734aSAndroid Build Coastguard Worker     *enabled = gCtls->tetherCtrl.isTetheringStarted();
920*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
921*8542734aSAndroid Build Coastguard Worker }
922*8542734aSAndroid Build Coastguard Worker 
tetherInterfaceAdd(const std::string & ifName)923*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherInterfaceAdd(const std::string& ifName) {
924*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
925*8542734aSAndroid Build Coastguard Worker     int res = gCtls->tetherCtrl.tetherInterface(ifName.c_str());
926*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
927*8542734aSAndroid Build Coastguard Worker }
928*8542734aSAndroid Build Coastguard Worker 
tetherInterfaceRemove(const std::string & ifName)929*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherInterfaceRemove(const std::string& ifName) {
930*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
931*8542734aSAndroid Build Coastguard Worker     int res = gCtls->tetherCtrl.untetherInterface(ifName.c_str());
932*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
933*8542734aSAndroid Build Coastguard Worker }
934*8542734aSAndroid Build Coastguard Worker 
tetherInterfaceList(std::vector<std::string> * ifList)935*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherInterfaceList(std::vector<std::string>* ifList) {
936*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
937*8542734aSAndroid Build Coastguard Worker     for (const auto& ifname : gCtls->tetherCtrl.getTetheredInterfaceList()) {
938*8542734aSAndroid Build Coastguard Worker         ifList->push_back(ifname);
939*8542734aSAndroid Build Coastguard Worker     }
940*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
941*8542734aSAndroid Build Coastguard Worker }
942*8542734aSAndroid Build Coastguard Worker 
tetherDnsSet(int32_t netId,const std::vector<std::string> & dnsAddrs)943*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherDnsSet(int32_t netId,
944*8542734aSAndroid Build Coastguard Worker                                                const std::vector<std::string>& dnsAddrs) {
945*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
946*8542734aSAndroid Build Coastguard Worker     int res = gCtls->tetherCtrl.setDnsForwarders(netId, dnsAddrs);
947*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
948*8542734aSAndroid Build Coastguard Worker }
949*8542734aSAndroid Build Coastguard Worker 
tetherDnsList(std::vector<std::string> * dnsList)950*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherDnsList(std::vector<std::string>* dnsList) {
951*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
952*8542734aSAndroid Build Coastguard Worker     for (const auto& fwdr : gCtls->tetherCtrl.getDnsForwarders()) {
953*8542734aSAndroid Build Coastguard Worker         dnsList->push_back(fwdr);
954*8542734aSAndroid Build Coastguard Worker     }
955*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
956*8542734aSAndroid Build Coastguard Worker }
957*8542734aSAndroid Build Coastguard Worker 
networkAddRouteParcel(int32_t netId,const RouteInfoParcel & route)958*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkAddRouteParcel(int32_t netId,
959*8542734aSAndroid Build Coastguard Worker                                                         const RouteInfoParcel& route) {
960*8542734aSAndroid Build Coastguard Worker     // Public methods of NetworkController are thread-safe.
961*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
962*8542734aSAndroid Build Coastguard Worker     bool legacy = false;
963*8542734aSAndroid Build Coastguard Worker     uid_t uid = 0;  // UID is only meaningful for legacy routes.
964*8542734aSAndroid Build Coastguard Worker 
965*8542734aSAndroid Build Coastguard Worker     // convert Parcel to parameters
966*8542734aSAndroid Build Coastguard Worker     int res = gCtls->netCtrl.addRoute(netId, route.ifName.c_str(), route.destination.c_str(),
967*8542734aSAndroid Build Coastguard Worker                                       route.nextHop.empty() ? nullptr : route.nextHop.c_str(),
968*8542734aSAndroid Build Coastguard Worker                                       legacy, uid, route.mtu);
969*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
970*8542734aSAndroid Build Coastguard Worker }
971*8542734aSAndroid Build Coastguard Worker 
networkUpdateRouteParcel(int32_t netId,const RouteInfoParcel & route)972*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkUpdateRouteParcel(int32_t netId,
973*8542734aSAndroid Build Coastguard Worker                                                            const RouteInfoParcel& route) {
974*8542734aSAndroid Build Coastguard Worker     // Public methods of NetworkController are thread-safe.
975*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
976*8542734aSAndroid Build Coastguard Worker     bool legacy = false;
977*8542734aSAndroid Build Coastguard Worker     uid_t uid = 0;  // UID is only meaningful for legacy routes.
978*8542734aSAndroid Build Coastguard Worker 
979*8542734aSAndroid Build Coastguard Worker     // convert Parcel to parameters
980*8542734aSAndroid Build Coastguard Worker     int res = gCtls->netCtrl.updateRoute(netId, route.ifName.c_str(), route.destination.c_str(),
981*8542734aSAndroid Build Coastguard Worker                                          route.nextHop.empty() ? nullptr : route.nextHop.c_str(),
982*8542734aSAndroid Build Coastguard Worker                                          legacy, uid, route.mtu);
983*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
984*8542734aSAndroid Build Coastguard Worker }
985*8542734aSAndroid Build Coastguard Worker 
networkRemoveRouteParcel(int32_t netId,const RouteInfoParcel & route)986*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkRemoveRouteParcel(int32_t netId,
987*8542734aSAndroid Build Coastguard Worker                                                            const RouteInfoParcel& route) {
988*8542734aSAndroid Build Coastguard Worker     return networkRemoveRoute(netId, route.ifName, route.destination, route.nextHop);
989*8542734aSAndroid Build Coastguard Worker }
990*8542734aSAndroid Build Coastguard Worker 
networkAddRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)991*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkAddRoute(int32_t netId, const std::string& ifName,
992*8542734aSAndroid Build Coastguard Worker                                                   const std::string& destination,
993*8542734aSAndroid Build Coastguard Worker                                                   const std::string& nextHop) {
994*8542734aSAndroid Build Coastguard Worker     // Public methods of NetworkController are thread-safe.
995*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
996*8542734aSAndroid Build Coastguard Worker     bool legacy = false;
997*8542734aSAndroid Build Coastguard Worker     uid_t uid = 0;  // UID is only meaningful for legacy routes.
998*8542734aSAndroid Build Coastguard Worker     int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
999*8542734aSAndroid Build Coastguard Worker                                       nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid, 0);
1000*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
1001*8542734aSAndroid Build Coastguard Worker }
1002*8542734aSAndroid Build Coastguard Worker 
networkRemoveRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)1003*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkRemoveRoute(int32_t netId, const std::string& ifName,
1004*8542734aSAndroid Build Coastguard Worker                                                      const std::string& destination,
1005*8542734aSAndroid Build Coastguard Worker                                                      const std::string& nextHop) {
1006*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1007*8542734aSAndroid Build Coastguard Worker     bool legacy = false;
1008*8542734aSAndroid Build Coastguard Worker     uid_t uid = 0;  // UID is only meaningful for legacy routes.
1009*8542734aSAndroid Build Coastguard Worker     int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1010*8542734aSAndroid Build Coastguard Worker                                          nextHop.empty() ? nullptr : nextHop.c_str(), legacy, uid);
1011*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
1012*8542734aSAndroid Build Coastguard Worker }
1013*8542734aSAndroid Build Coastguard Worker 
networkAddLegacyRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop,int32_t uid)1014*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkAddLegacyRoute(int32_t netId, const std::string& ifName,
1015*8542734aSAndroid Build Coastguard Worker                                                         const std::string& destination,
1016*8542734aSAndroid Build Coastguard Worker                                                         const std::string& nextHop, int32_t uid) {
1017*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1018*8542734aSAndroid Build Coastguard Worker     bool legacy = true;
1019*8542734aSAndroid Build Coastguard Worker     int res = gCtls->netCtrl.addRoute(netId, ifName.c_str(), destination.c_str(),
1020*8542734aSAndroid Build Coastguard Worker                                       nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1021*8542734aSAndroid Build Coastguard Worker                                       (uid_t)uid, 0);
1022*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
1023*8542734aSAndroid Build Coastguard Worker }
1024*8542734aSAndroid Build Coastguard Worker 
networkRemoveLegacyRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop,int32_t uid)1025*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkRemoveLegacyRoute(int32_t netId, const std::string& ifName,
1026*8542734aSAndroid Build Coastguard Worker                                                            const std::string& destination,
1027*8542734aSAndroid Build Coastguard Worker                                                            const std::string& nextHop,
1028*8542734aSAndroid Build Coastguard Worker                                                            int32_t uid) {
1029*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1030*8542734aSAndroid Build Coastguard Worker     bool legacy = true;
1031*8542734aSAndroid Build Coastguard Worker     int res = gCtls->netCtrl.removeRoute(netId, ifName.c_str(), destination.c_str(),
1032*8542734aSAndroid Build Coastguard Worker                                          nextHop.empty() ? nullptr : nextHop.c_str(), legacy,
1033*8542734aSAndroid Build Coastguard Worker                                          (uid_t) uid);
1034*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
1035*8542734aSAndroid Build Coastguard Worker }
1036*8542734aSAndroid Build Coastguard Worker 
networkGetDefault(int32_t * netId)1037*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkGetDefault(int32_t* netId) {
1038*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1039*8542734aSAndroid Build Coastguard Worker     *netId = gCtls->netCtrl.getDefaultNetwork();
1040*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
1041*8542734aSAndroid Build Coastguard Worker }
1042*8542734aSAndroid Build Coastguard Worker 
networkSetDefault(int32_t netId)1043*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkSetDefault(int32_t netId) {
1044*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1045*8542734aSAndroid Build Coastguard Worker     int res = gCtls->netCtrl.setDefaultNetwork(netId);
1046*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
1047*8542734aSAndroid Build Coastguard Worker }
1048*8542734aSAndroid Build Coastguard Worker 
networkClearDefault()1049*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkClearDefault() {
1050*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1051*8542734aSAndroid Build Coastguard Worker     unsigned netId = NETID_UNSET;
1052*8542734aSAndroid Build Coastguard Worker     int res = gCtls->netCtrl.setDefaultNetwork(netId);
1053*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
1054*8542734aSAndroid Build Coastguard Worker }
1055*8542734aSAndroid Build Coastguard Worker 
intsToUids(const std::vector<int32_t> & intUids)1056*8542734aSAndroid Build Coastguard Worker std::vector<uid_t> NetdNativeService::intsToUids(const std::vector<int32_t>& intUids) {
1057*8542734aSAndroid Build Coastguard Worker     return {begin(intUids), end(intUids)};
1058*8542734aSAndroid Build Coastguard Worker }
1059*8542734aSAndroid Build Coastguard Worker 
convertPermission(int32_t permission)1060*8542734aSAndroid Build Coastguard Worker Permission NetdNativeService::convertPermission(int32_t permission) {
1061*8542734aSAndroid Build Coastguard Worker     switch (permission) {
1062*8542734aSAndroid Build Coastguard Worker         case INetd::PERMISSION_NETWORK:
1063*8542734aSAndroid Build Coastguard Worker             return Permission::PERMISSION_NETWORK;
1064*8542734aSAndroid Build Coastguard Worker         case INetd::PERMISSION_SYSTEM:
1065*8542734aSAndroid Build Coastguard Worker             return Permission::PERMISSION_SYSTEM;
1066*8542734aSAndroid Build Coastguard Worker         default:
1067*8542734aSAndroid Build Coastguard Worker             return Permission::PERMISSION_NONE;
1068*8542734aSAndroid Build Coastguard Worker     }
1069*8542734aSAndroid Build Coastguard Worker }
1070*8542734aSAndroid Build Coastguard Worker 
networkSetPermissionForNetwork(int32_t netId,int32_t permission)1071*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkSetPermissionForNetwork(int32_t netId,
1072*8542734aSAndroid Build Coastguard Worker                                                                  int32_t permission) {
1073*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1074*8542734aSAndroid Build Coastguard Worker     std::vector<unsigned> netIds = {(unsigned) netId};
1075*8542734aSAndroid Build Coastguard Worker     int res = gCtls->netCtrl.setPermissionForNetworks(convertPermission(permission), netIds);
1076*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
1077*8542734aSAndroid Build Coastguard Worker }
1078*8542734aSAndroid Build Coastguard Worker 
networkSetPermissionForUser(int32_t permission,const std::vector<int32_t> & uids)1079*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkSetPermissionForUser(int32_t permission,
1080*8542734aSAndroid Build Coastguard Worker                                                               const std::vector<int32_t>& uids) {
1081*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1082*8542734aSAndroid Build Coastguard Worker     gCtls->netCtrl.setPermissionForUsers(convertPermission(permission), intsToUids(uids));
1083*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
1084*8542734aSAndroid Build Coastguard Worker }
1085*8542734aSAndroid Build Coastguard Worker 
networkClearPermissionForUser(const std::vector<int32_t> & uids)1086*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkClearPermissionForUser(const std::vector<int32_t>& uids) {
1087*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1088*8542734aSAndroid Build Coastguard Worker     Permission permission = Permission::PERMISSION_NONE;
1089*8542734aSAndroid Build Coastguard Worker     gCtls->netCtrl.setPermissionForUsers(permission, intsToUids(uids));
1090*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
1091*8542734aSAndroid Build Coastguard Worker }
1092*8542734aSAndroid Build Coastguard Worker 
networkSetProtectAllow(int32_t uid)1093*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkSetProtectAllow(int32_t uid) {
1094*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1095*8542734aSAndroid Build Coastguard Worker     gCtls->netCtrl.allowProtect((uid_t)uid, NETID_UNSET);
1096*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
1097*8542734aSAndroid Build Coastguard Worker }
1098*8542734aSAndroid Build Coastguard Worker 
networkSetProtectDeny(int32_t uid)1099*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkSetProtectDeny(int32_t uid) {
1100*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1101*8542734aSAndroid Build Coastguard Worker     gCtls->netCtrl.denyProtect((uid_t)uid, NETID_UNSET);
1102*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
1103*8542734aSAndroid Build Coastguard Worker }
1104*8542734aSAndroid Build Coastguard Worker 
networkCanProtect(int32_t uid,bool * ret)1105*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkCanProtect(int32_t uid, bool* ret) {
1106*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1107*8542734aSAndroid Build Coastguard Worker     *ret = gCtls->netCtrl.canProtect((uid_t)uid, NETID_UNSET);
1108*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
1109*8542734aSAndroid Build Coastguard Worker }
1110*8542734aSAndroid Build Coastguard Worker 
networkAllowBypassVpnOnNetwork(bool allow,int32_t uid,int32_t netId)1111*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::networkAllowBypassVpnOnNetwork(bool allow, int32_t uid,
1112*8542734aSAndroid Build Coastguard Worker                                                                  int32_t netId) {
1113*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1114*8542734aSAndroid Build Coastguard Worker     int err;
1115*8542734aSAndroid Build Coastguard Worker     if (allow) {
1116*8542734aSAndroid Build Coastguard Worker         err = gCtls->netCtrl.allowProtect((uid_t)uid, netId);
1117*8542734aSAndroid Build Coastguard Worker     } else {
1118*8542734aSAndroid Build Coastguard Worker         err = gCtls->netCtrl.denyProtect((uid_t)uid, netId);
1119*8542734aSAndroid Build Coastguard Worker     }
1120*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(err);
1121*8542734aSAndroid Build Coastguard Worker }
1122*8542734aSAndroid Build Coastguard Worker 
trafficSetNetPermForUids(int32_t,const std::vector<int32_t> &)1123*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::trafficSetNetPermForUids(int32_t, const std::vector<int32_t>&) {
1124*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
1125*8542734aSAndroid Build Coastguard Worker }
1126*8542734aSAndroid Build Coastguard Worker 
firewallSetFirewallType(int32_t firewallType)1127*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::firewallSetFirewallType(int32_t firewallType) {
1128*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
1129*8542734aSAndroid Build Coastguard Worker     auto type = static_cast<FirewallType>(firewallType);
1130*8542734aSAndroid Build Coastguard Worker 
1131*8542734aSAndroid Build Coastguard Worker     int res = gCtls->firewallCtrl.setFirewallType(type);
1132*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
1133*8542734aSAndroid Build Coastguard Worker }
1134*8542734aSAndroid Build Coastguard Worker 
firewallSetInterfaceRule(const std::string & ifName,int32_t firewallRule)1135*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::firewallSetInterfaceRule(const std::string& ifName,
1136*8542734aSAndroid Build Coastguard Worker                                                            int32_t firewallRule) {
1137*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->firewallCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
1138*8542734aSAndroid Build Coastguard Worker     auto rule = static_cast<FirewallRule>(firewallRule);
1139*8542734aSAndroid Build Coastguard Worker 
1140*8542734aSAndroid Build Coastguard Worker     int res = gCtls->firewallCtrl.setInterfaceRule(ifName.c_str(), rule);
1141*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
1142*8542734aSAndroid Build Coastguard Worker }
1143*8542734aSAndroid Build Coastguard Worker 
firewallSetUidRule(int32_t,int32_t,int32_t)1144*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::firewallSetUidRule(int32_t, int32_t, int32_t) {
1145*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
1146*8542734aSAndroid Build Coastguard Worker }
1147*8542734aSAndroid Build Coastguard Worker 
firewallEnableChildChain(int32_t,bool)1148*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::firewallEnableChildChain(int32_t, bool) {
1149*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
1150*8542734aSAndroid Build Coastguard Worker }
1151*8542734aSAndroid Build Coastguard Worker 
firewallAddUidInterfaceRules(const std::string &,const std::vector<int32_t> &)1152*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::firewallAddUidInterfaceRules(const std::string&,
1153*8542734aSAndroid Build Coastguard Worker                                                                const std::vector<int32_t>&) {
1154*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
1155*8542734aSAndroid Build Coastguard Worker }
1156*8542734aSAndroid Build Coastguard Worker 
firewallRemoveUidInterfaceRules(const std::vector<int32_t> &)1157*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::firewallRemoveUidInterfaceRules(const std::vector<int32_t>&) {
1158*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
1159*8542734aSAndroid Build Coastguard Worker }
1160*8542734aSAndroid Build Coastguard Worker 
tetherAddForward(const std::string & intIface,const std::string & extIface)1161*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherAddForward(const std::string& intIface,
1162*8542734aSAndroid Build Coastguard Worker                                                    const std::string& extIface) {
1163*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
1164*8542734aSAndroid Build Coastguard Worker 
1165*8542734aSAndroid Build Coastguard Worker     int res = gCtls->tetherCtrl.enableNat(intIface.c_str(), extIface.c_str());
1166*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
1167*8542734aSAndroid Build Coastguard Worker }
1168*8542734aSAndroid Build Coastguard Worker 
tetherRemoveForward(const std::string & intIface,const std::string & extIface)1169*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherRemoveForward(const std::string& intIface,
1170*8542734aSAndroid Build Coastguard Worker                                                       const std::string& extIface) {
1171*8542734aSAndroid Build Coastguard Worker     NETD_LOCKING_RPC(gCtls->tetherCtrl.lock, PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK);
1172*8542734aSAndroid Build Coastguard Worker     int res = gCtls->tetherCtrl.disableNat(intIface.c_str(), extIface.c_str());
1173*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(res);
1174*8542734aSAndroid Build Coastguard Worker }
1175*8542734aSAndroid Build Coastguard Worker 
setTcpRWmemorySize(const std::string & rmemValues,const std::string & wmemValues)1176*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::setTcpRWmemorySize(const std::string& rmemValues,
1177*8542734aSAndroid Build Coastguard Worker                                                      const std::string& wmemValues) {
1178*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1179*8542734aSAndroid Build Coastguard Worker     if (!WriteStringToFile(rmemValues, TCP_RMEM_PROC_FILE)) {
1180*8542734aSAndroid Build Coastguard Worker         int ret = -errno;
1181*8542734aSAndroid Build Coastguard Worker         return statusFromErrcode(ret);
1182*8542734aSAndroid Build Coastguard Worker     }
1183*8542734aSAndroid Build Coastguard Worker 
1184*8542734aSAndroid Build Coastguard Worker     if (!WriteStringToFile(wmemValues, TCP_WMEM_PROC_FILE)) {
1185*8542734aSAndroid Build Coastguard Worker         int ret = -errno;
1186*8542734aSAndroid Build Coastguard Worker         return statusFromErrcode(ret);
1187*8542734aSAndroid Build Coastguard Worker     }
1188*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
1189*8542734aSAndroid Build Coastguard Worker }
1190*8542734aSAndroid Build Coastguard Worker 
registerUnsolicitedEventListener(const android::sp<android::net::INetdUnsolicitedEventListener> & listener)1191*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::registerUnsolicitedEventListener(
1192*8542734aSAndroid Build Coastguard Worker         const android::sp<android::net::INetdUnsolicitedEventListener>& listener) {
1193*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1194*8542734aSAndroid Build Coastguard Worker     gCtls->eventReporter.registerUnsolEventListener(listener);
1195*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
1196*8542734aSAndroid Build Coastguard Worker }
1197*8542734aSAndroid Build Coastguard Worker 
getOemNetd(android::sp<android::IBinder> * listener)1198*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::getOemNetd(android::sp<android::IBinder>* listener) {
1199*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1200*8542734aSAndroid Build Coastguard Worker     *listener = com::android::internal::net::OemNetdListener::getListener();
1201*8542734aSAndroid Build Coastguard Worker 
1202*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
1203*8542734aSAndroid Build Coastguard Worker }
1204*8542734aSAndroid Build Coastguard Worker 
getFwmarkForNetwork(int32_t netId,MarkMaskParcel * markMask)1205*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::getFwmarkForNetwork(int32_t netId, MarkMaskParcel* markMask) {
1206*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1207*8542734aSAndroid Build Coastguard Worker 
1208*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
1209*8542734aSAndroid Build Coastguard Worker     fwmark.netId = netId;
1210*8542734aSAndroid Build Coastguard Worker     markMask->mask = FWMARK_NET_ID_MASK;
1211*8542734aSAndroid Build Coastguard Worker     markMask->mark = fwmark.intValue;
1212*8542734aSAndroid Build Coastguard Worker     return binder::Status::ok();
1213*8542734aSAndroid Build Coastguard Worker }
1214*8542734aSAndroid Build Coastguard Worker 
tetherOffloadRuleAdd(const TetherOffloadRuleParcel &)1215*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherOffloadRuleAdd(const TetherOffloadRuleParcel& /* rule */) {
1216*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
1217*8542734aSAndroid Build Coastguard Worker }
1218*8542734aSAndroid Build Coastguard Worker 
tetherOffloadRuleRemove(const TetherOffloadRuleParcel &)1219*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherOffloadRuleRemove(
1220*8542734aSAndroid Build Coastguard Worker         const TetherOffloadRuleParcel& /* rule */) {
1221*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
1222*8542734aSAndroid Build Coastguard Worker }
1223*8542734aSAndroid Build Coastguard Worker 
tetherOffloadGetStats(std::vector<TetherStatsParcel> *)1224*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherOffloadGetStats(
1225*8542734aSAndroid Build Coastguard Worker         std::vector<TetherStatsParcel>* /* tetherStatsParcelVec */) {
1226*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
1227*8542734aSAndroid Build Coastguard Worker }
1228*8542734aSAndroid Build Coastguard Worker 
tetherOffloadSetInterfaceQuota(int,int64_t)1229*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherOffloadSetInterfaceQuota(int /* ifIndex */,
1230*8542734aSAndroid Build Coastguard Worker                                                                  int64_t /* quotaBytes */) {
1231*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
1232*8542734aSAndroid Build Coastguard Worker }
1233*8542734aSAndroid Build Coastguard Worker 
tetherOffloadGetAndClearStats(int,android::net::TetherStatsParcel *)1234*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::tetherOffloadGetAndClearStats(
1235*8542734aSAndroid Build Coastguard Worker         int /* ifIndex */, android::net::TetherStatsParcel* /* tetherStats */) {
1236*8542734aSAndroid Build Coastguard Worker     DEPRECATED;
1237*8542734aSAndroid Build Coastguard Worker }
1238*8542734aSAndroid Build Coastguard Worker 
setNetworkAllowlist(const std::vector<NativeUidRangeConfig> & rangeConfigs)1239*8542734aSAndroid Build Coastguard Worker binder::Status NetdNativeService::setNetworkAllowlist(
1240*8542734aSAndroid Build Coastguard Worker         const std::vector<NativeUidRangeConfig>& rangeConfigs) {
1241*8542734aSAndroid Build Coastguard Worker     ENFORCE_NETWORK_STACK_PERMISSIONS();
1242*8542734aSAndroid Build Coastguard Worker     return statusFromErrcode(gCtls->netCtrl.setNetworkAllowlist(rangeConfigs));
1243*8542734aSAndroid Build Coastguard Worker }
1244*8542734aSAndroid Build Coastguard Worker 
1245*8542734aSAndroid Build Coastguard Worker }  // namespace net
1246*8542734aSAndroid Build Coastguard Worker }  // namespace android
1247