Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [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 NETD_SERVER_IPTABLES_RESTORE_CONTROLLER_H |
| 18 | #define NETD_SERVER_IPTABLES_RESTORE_CONTROLLER_H |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <mutex> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | #include "NetdConstants.h" |
| 25 | |
| 26 | class IptablesProcess; |
| 27 | |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 28 | class IptablesRestoreInterface { |
| 29 | public: |
| 30 | virtual ~IptablesRestoreInterface() = default; |
| 31 | |
| 32 | // Execute |commands| on the given |target|, and populate |output| with stdout. |
| 33 | virtual int execute(const IptablesTarget target, const std::string& commands, |
| 34 | std::string* output) = 0; |
| 35 | }; |
| 36 | |
| 37 | class IptablesRestoreController final : public IptablesRestoreInterface { |
| 38 | public: |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 39 | // Not for general use. Use gCtls->iptablesRestoreCtrl |
| 40 | // to get an instance of this class. |
| 41 | IptablesRestoreController(); |
| 42 | |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 43 | ~IptablesRestoreController() override; |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 44 | |
Joel Scherpelz | 08b84cd | 2017-05-22 13:11:54 +0900 | [diff] [blame] | 45 | int execute(const IptablesTarget target, const std::string& commands, |
| 46 | std::string* output) override; |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 47 | |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 48 | enum IptablesProcessType { |
| 49 | IPTABLES_PROCESS, |
| 50 | IP6TABLES_PROCESS, |
| 51 | INVALID_PROCESS = -1, |
| 52 | }; |
| 53 | |
| 54 | // Called by the SIGCHLD signal handler when it detects that one |
| 55 | // of the forked iptables[6]-restore process has died. |
| 56 | IptablesProcessType notifyChildTermination(pid_t pid); |
| 57 | |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 58 | protected: |
| 59 | friend class IptablesRestoreControllerTest; |
| 60 | pid_t getIpRestorePid(const IptablesProcessType type); |
| 61 | |
Lorenzo Colitti | a701afb | 2017-02-28 01:47:11 +0900 | [diff] [blame] | 62 | // The maximum number of times we poll(2) for a response on our set of polled |
| 63 | // fds. Chosen so that the overall timeout is 5s. The timeout is so high because |
| 64 | // our version of iptables still polls every second in xtables_lock. |
| 65 | static int MAX_RETRIES; |
| 66 | |
| 67 | // The timeout (in millis) for each call to poll. The maximum wait is |
| 68 | // |POLL_TIMEOUT_MS * MAX_RETRIES|. Chosen so that the overall timeout is 1s. |
| 69 | static int POLL_TIMEOUT_MS; |
| 70 | |
Lorenzo Colitti | 2103b6b | 2017-08-14 11:38:18 +0900 | [diff] [blame] | 71 | void Init(); |
| 72 | |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 73 | private: |
| 74 | static IptablesProcess* forkAndExec(const IptablesProcessType type); |
| 75 | |
Lorenzo Colitti | cd28377 | 2017-01-31 19:00:49 +0900 | [diff] [blame] | 76 | int sendCommand(const IptablesProcessType type, const std::string& command, |
| 77 | std::string *output); |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 78 | |
Lorenzo Colitti | 2509142 | 2017-02-03 16:05:28 +0900 | [diff] [blame] | 79 | static bool drainAndWaitForAck(const std::unique_ptr<IptablesProcess> &process, |
| 80 | const std::string& command, |
| 81 | std::string *output); |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 82 | |
| 83 | static void maybeLogStderr(const std::unique_ptr<IptablesProcess> &process, |
Lorenzo Colitti | 2509142 | 2017-02-03 16:05:28 +0900 | [diff] [blame] | 84 | const std::string& command); |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 85 | |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 86 | // Guards calls to execute(). |
| 87 | std::mutex mLock; |
Lorenzo Colitti | 173da32 | 2017-02-05 01:56:40 +0900 | [diff] [blame] | 88 | |
| 89 | std::unique_ptr<IptablesProcess> mIpRestore; |
| 90 | std::unique_ptr<IptablesProcess> mIp6Restore; |
Narayan Kamath | a5ace89 | 2017-01-06 15:10:02 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | #endif // NETD_SERVER_IPTABLES_RESTORE_CONTROLLER_H |