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