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