xref: /aosp_15_r20/system/netd/server/WakeupController.h (revision 8542734a0dd1db395a4d42aae09c37f3c3c3e7a1)
1*8542734aSAndroid Build Coastguard Worker /*
2*8542734aSAndroid Build Coastguard Worker  * Copyright (C) 2017 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 #ifndef WAKEUP_CONTROLLER_H
18*8542734aSAndroid Build Coastguard Worker #define WAKEUP_CONTROLLER_H
19*8542734aSAndroid Build Coastguard Worker 
20*8542734aSAndroid Build Coastguard Worker #include <functional>
21*8542734aSAndroid Build Coastguard Worker 
22*8542734aSAndroid Build Coastguard Worker #include <netdutils/Status.h>
23*8542734aSAndroid Build Coastguard Worker 
24*8542734aSAndroid Build Coastguard Worker #include "IptablesRestoreController.h"
25*8542734aSAndroid Build Coastguard Worker #include "NFLogListener.h"
26*8542734aSAndroid Build Coastguard Worker 
27*8542734aSAndroid Build Coastguard Worker namespace android {
28*8542734aSAndroid Build Coastguard Worker namespace net {
29*8542734aSAndroid Build Coastguard Worker 
30*8542734aSAndroid Build Coastguard Worker class WakeupController {
31*8542734aSAndroid Build Coastguard Worker   public:
32*8542734aSAndroid Build Coastguard Worker 
33*8542734aSAndroid Build Coastguard Worker     // Simple data struct for passing back packet wakeup event information to the ReportFn callback.
34*8542734aSAndroid Build Coastguard Worker     struct ReportArgs {
35*8542734aSAndroid Build Coastguard Worker         std::string prefix;
36*8542734aSAndroid Build Coastguard Worker         uint64_t timestampNs;
37*8542734aSAndroid Build Coastguard Worker         int uid;
38*8542734aSAndroid Build Coastguard Worker         int gid;
39*8542734aSAndroid Build Coastguard Worker         int ethertype;
40*8542734aSAndroid Build Coastguard Worker         int ipNextHeader;
41*8542734aSAndroid Build Coastguard Worker         std::vector<uint8_t> dstHw;
42*8542734aSAndroid Build Coastguard Worker         std::string srcIp;
43*8542734aSAndroid Build Coastguard Worker         std::string dstIp;
44*8542734aSAndroid Build Coastguard Worker         int srcPort;
45*8542734aSAndroid Build Coastguard Worker         int dstPort;
46*8542734aSAndroid Build Coastguard Worker     };
47*8542734aSAndroid Build Coastguard Worker 
48*8542734aSAndroid Build Coastguard Worker     // Callback that is triggered for every wakeup event.
49*8542734aSAndroid Build Coastguard Worker     using ReportFn = std::function<void(const struct ReportArgs&)>;
50*8542734aSAndroid Build Coastguard Worker 
51*8542734aSAndroid Build Coastguard Worker     // iptables chain where wakeup packets are matched
52*8542734aSAndroid Build Coastguard Worker     static const char LOCAL_MANGLE_INPUT[];
53*8542734aSAndroid Build Coastguard Worker 
54*8542734aSAndroid Build Coastguard Worker     static const uint32_t kDefaultPacketCopyRange;
55*8542734aSAndroid Build Coastguard Worker 
WakeupController(ReportFn report,IptablesRestoreInterface * iptables)56*8542734aSAndroid Build Coastguard Worker     WakeupController(ReportFn report, IptablesRestoreInterface* iptables)
57*8542734aSAndroid Build Coastguard Worker         : mReport(report), mIptables(iptables) {}
58*8542734aSAndroid Build Coastguard Worker 
59*8542734aSAndroid Build Coastguard Worker     ~WakeupController();
60*8542734aSAndroid Build Coastguard Worker 
61*8542734aSAndroid Build Coastguard Worker     // Subscribe this controller to a NFLOG events arriving at |listener|.
62*8542734aSAndroid Build Coastguard Worker     netdutils::Status init(NFLogListenerInterface* listener);
63*8542734aSAndroid Build Coastguard Worker 
64*8542734aSAndroid Build Coastguard Worker     // Install iptables rules to match packets arriving on |ifName|
65*8542734aSAndroid Build Coastguard Worker     // which match |mark|/|mask|. Metadata from matching packets will
66*8542734aSAndroid Build Coastguard Worker     // be delivered along with the arbitrary string |prefix| to
67*8542734aSAndroid Build Coastguard Worker     // INetdEventListener::onWakeupEvent.
68*8542734aSAndroid Build Coastguard Worker     netdutils::Status addInterface(const std::string& ifName, const std::string& prefix,
69*8542734aSAndroid Build Coastguard Worker                                    uint32_t mark, uint32_t mask);
70*8542734aSAndroid Build Coastguard Worker 
71*8542734aSAndroid Build Coastguard Worker     // Remove iptables rules previously installed by addInterface().
72*8542734aSAndroid Build Coastguard Worker     // |ifName|, |prefix|, |mark| and |mask| must match precisely.
73*8542734aSAndroid Build Coastguard Worker     netdutils::Status delInterface(const std::string& ifName, const std::string& prefix,
74*8542734aSAndroid Build Coastguard Worker                                    uint32_t mark, uint32_t mask);
75*8542734aSAndroid Build Coastguard Worker 
76*8542734aSAndroid Build Coastguard Worker   private:
77*8542734aSAndroid Build Coastguard Worker     netdutils::Status execIptables(const std::string& action, const std::string& ifName,
78*8542734aSAndroid Build Coastguard Worker                                    const std::string& prefix, uint32_t mark, uint32_t mask);
79*8542734aSAndroid Build Coastguard Worker 
80*8542734aSAndroid Build Coastguard Worker     ReportFn const mReport;
81*8542734aSAndroid Build Coastguard Worker     IptablesRestoreInterface* const mIptables;
82*8542734aSAndroid Build Coastguard Worker     NFLogListenerInterface* mListener;
83*8542734aSAndroid Build Coastguard Worker };
84*8542734aSAndroid Build Coastguard Worker 
85*8542734aSAndroid Build Coastguard Worker }  // namespace net
86*8542734aSAndroid Build Coastguard Worker }  // namespace android
87*8542734aSAndroid Build Coastguard Worker 
88*8542734aSAndroid Build Coastguard Worker #endif /* WAKEUP_CONTROLLER_H */
89