Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef WAKEUP_CONTROLLER_H |
| 18 | #define WAKEUP_CONTROLLER_H |
| 19 | |
| 20 | #include <functional> |
| 21 | |
| 22 | #include <netdutils/Status.h> |
| 23 | |
| 24 | #include "IptablesRestoreController.h" |
| 25 | #include "NFLogListener.h" |
| 26 | |
| 27 | namespace android { |
| 28 | namespace net { |
| 29 | |
| 30 | class WakeupController { |
| 31 | public: |
Hugo Benichi | e874b7f | 2017-10-12 21:34:46 +0900 | [diff] [blame] | 32 | |
| 33 | // Simple data struct for passing back packet wakeup event information to the ReportFn callback. |
| 34 | struct ReportArgs { |
| 35 | std::string prefix; |
| 36 | uint64_t timestampNs; |
| 37 | int uid; |
| 38 | int gid; |
| 39 | int ethertype; |
| 40 | int ipNextHeader; |
| 41 | std::vector<uint8_t> dstHw; |
| 42 | std::string srcIp; |
| 43 | std::string dstIp; |
| 44 | int srcPort; |
| 45 | int dstPort; |
| 46 | }; |
| 47 | |
| 48 | // Callback that is triggered for every wakeup event. |
| 49 | using ReportFn = std::function<void(const struct ReportArgs&)>; |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 50 | |
| 51 | // iptables chain where wakeup packets are matched |
| 52 | static const char LOCAL_MANGLE_INPUT[]; |
| 53 | |
Hugo Benichi | e874b7f | 2017-10-12 21:34:46 +0900 | [diff] [blame] | 54 | static const uint32_t kDefaultPacketCopyRange; |
| 55 | |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 56 | WakeupController(ReportFn report, IptablesRestoreInterface* iptables) |
| 57 | : mReport(report), mIptables(iptables) {} |
| 58 | |
| 59 | ~WakeupController(); |
| 60 | |
| 61 | // Subscribe this controller to a NFLOG events arriving at |listener|. |
| 62 | netdutils::Status init(NFLogListenerInterface* listener); |
| 63 | |
| 64 | // Install iptables rules to match packets arriving on |ifName| |
| 65 | // which match |mark|/|mask|. Metadata from matching packets will |
| 66 | // be delivered along with the arbitrary string |prefix| to |
| 67 | // INetdEventListener::onWakeupEvent. |
| 68 | netdutils::Status addInterface(const std::string& ifName, const std::string& prefix, |
| 69 | uint32_t mark, uint32_t mask); |
| 70 | |
| 71 | // Remove iptables rules previously installed by addInterface(). |
| 72 | // |ifName|, |prefix|, |mark| and |mask| must match precisely. |
| 73 | netdutils::Status delInterface(const std::string& ifName, const std::string& prefix, |
| 74 | uint32_t mark, uint32_t mask); |
| 75 | |
| 76 | private: |
| 77 | netdutils::Status execIptables(const std::string& action, const std::string& ifName, |
| 78 | const std::string& prefix, uint32_t mark, uint32_t mask); |
| 79 | |
| 80 | ReportFn const mReport; |
| 81 | IptablesRestoreInterface* const mIptables; |
| 82 | NFLogListenerInterface* mListener; |
| 83 | }; |
| 84 | |
| 85 | } // namespace net |
| 86 | } // namespace android |
| 87 | |
| 88 | #endif /* WAKEUP_CONTROLLER_H */ |