Jason Jeremy Iman | 54c046f | 2020-06-23 23:12:00 +0900 | [diff] [blame] | 1 | // Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef PATCHPANEL_FIREWALL_H_ |
| 6 | #define PATCHPANEL_FIREWALL_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | |
Hugo Benichi | 283a781 | 2021-06-08 00:47:54 +0900 | [diff] [blame^] | 10 | #include <memory> |
Jason Jeremy Iman | 54c046f | 2020-06-23 23:12:00 +0900 | [diff] [blame] | 11 | #include <set> |
| 12 | #include <string> |
| 13 | #include <utility> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include <base/macros.h> |
| 17 | #include <brillo/errors/error.h> |
| 18 | #include <gtest/gtest_prod.h> |
| 19 | #include <patchpanel/proto_bindings/patchpanel_service.pb.h> |
| 20 | |
Hugo Benichi | 283a781 | 2021-06-08 00:47:54 +0900 | [diff] [blame^] | 21 | #include "patchpanel/minijailed_process_runner.h" |
| 22 | |
Jason Jeremy Iman | 54c046f | 2020-06-23 23:12:00 +0900 | [diff] [blame] | 23 | namespace patchpanel { |
| 24 | |
| 25 | using Operation = patchpanel::ModifyPortRuleRequest::Operation; |
| 26 | using Protocol = patchpanel::ModifyPortRuleRequest::Protocol; |
| 27 | using RuleType = patchpanel::ModifyPortRuleRequest::RuleType; |
| 28 | |
Jason Jeremy Iman | 54c046f | 2020-06-23 23:12:00 +0900 | [diff] [blame] | 29 | const std::string ProtocolName(Protocol proto); |
| 30 | |
| 31 | class Firewall { |
| 32 | public: |
| 33 | typedef std::pair<uint16_t, std::string> Hole; |
| 34 | |
Hugo Benichi | 283a781 | 2021-06-08 00:47:54 +0900 | [diff] [blame^] | 35 | Firewall(); |
| 36 | Firewall(MinijailedProcessRunner* process_runner); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 37 | Firewall(const Firewall&) = delete; |
| 38 | Firewall& operator=(const Firewall&) = delete; |
| 39 | |
Hugo Benichi | 283a781 | 2021-06-08 00:47:54 +0900 | [diff] [blame^] | 40 | virtual ~Firewall() = default; |
Jason Jeremy Iman | 54c046f | 2020-06-23 23:12:00 +0900 | [diff] [blame] | 41 | |
Hugo Benichi | 283a781 | 2021-06-08 00:47:54 +0900 | [diff] [blame^] | 42 | virtual bool AddAcceptRules(Protocol protocol, |
| 43 | uint16_t port, |
| 44 | const std::string& interface); |
| 45 | virtual bool DeleteAcceptRules(Protocol protocol, |
| 46 | uint16_t port, |
| 47 | const std::string& interface); |
| 48 | virtual bool AddLoopbackLockdownRules(Protocol protocol, uint16_t port); |
| 49 | virtual bool DeleteLoopbackLockdownRules(Protocol protocol, uint16_t port); |
| 50 | virtual bool AddIpv4ForwardRule(Protocol protocol, |
Jason Jeremy Iman | 54c046f | 2020-06-23 23:12:00 +0900 | [diff] [blame] | 51 | const std::string& input_ip, |
| 52 | uint16_t port, |
| 53 | const std::string& interface, |
| 54 | const std::string& dst_ip, |
Hugo Benichi | 283a781 | 2021-06-08 00:47:54 +0900 | [diff] [blame^] | 55 | uint16_t dst_port); |
| 56 | virtual bool DeleteIpv4ForwardRule(Protocol protocol, |
| 57 | const std::string& input_ip, |
| 58 | uint16_t port, |
| 59 | const std::string& interface, |
| 60 | const std::string& dst_ip, |
| 61 | uint16_t dst_port); |
Jason Jeremy Iman | 54c046f | 2020-06-23 23:12:00 +0900 | [diff] [blame] | 62 | |
Hugo Benichi | 283a781 | 2021-06-08 00:47:54 +0900 | [diff] [blame^] | 63 | private: |
| 64 | enum IpFamily { |
| 65 | IPv4, |
| 66 | IPv6, |
| 67 | }; |
| 68 | |
| 69 | // Adds ACCEPT chain rules to the filter INPUT chain. |
| 70 | bool AddAcceptRule(IpFamily ip_family, |
| 71 | Protocol protocol, |
| 72 | uint16_t port, |
| 73 | const std::string& interface); |
| 74 | // Removes ACCEPT chain rules from the filter INPUT chain. |
| 75 | bool DeleteAcceptRule(IpFamily ip_family, |
| 76 | Protocol protocol, |
| 77 | uint16_t port, |
| 78 | const std::string& interface); |
| 79 | // Adds or removes MASQUERADE chain rules to/from the nat PREROUTING chain. |
| 80 | bool ModifyIpv4DNATRule(Protocol protocol, |
| 81 | const std::string& input_ip, |
| 82 | uint16_t port, |
| 83 | const std::string& interface, |
| 84 | const std::string& dst_ip, |
| 85 | uint16_t dst_port, |
| 86 | const std::string& operation); |
| 87 | // Adds or removes ACCEPT chain rules to/from the filter FORWARD chain. |
| 88 | bool ModifyIpv4ForwardChain(Protocol protocol, |
| 89 | const std::string& interface, |
| 90 | const std::string& dst_ip, |
| 91 | uint16_t dst_port, |
| 92 | const std::string& operation); |
| 93 | bool AddLoopbackLockdownRule(IpFamily ip_family, |
| 94 | Protocol protocol, |
| 95 | uint16_t port); |
| 96 | bool DeleteLoopbackLockdownRule(IpFamily ip_family, |
| 97 | Protocol protocol, |
| 98 | uint16_t port); |
| 99 | bool RunIptables(IpFamily ip_family, |
| 100 | const std::string& table, |
| 101 | const std::vector<std::string>& argv); |
| 102 | |
| 103 | std::unique_ptr<MinijailedProcessRunner> process_runner_; |
Jason Jeremy Iman | 54c046f | 2020-06-23 23:12:00 +0900 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | } // namespace patchpanel |
| 107 | |
| 108 | #endif // PATCHPANEL_FIREWALL_H_ |