blob: 81c39928a7a8f227c155ae6112b8ac1f39e9d0f4 [file] [log] [blame]
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +09001// 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 Benichi283a7812021-06-08 00:47:54 +090010#include <memory>
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090011#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 Benichi283a7812021-06-08 00:47:54 +090021#include "patchpanel/minijailed_process_runner.h"
22
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090023namespace patchpanel {
24
25using Operation = patchpanel::ModifyPortRuleRequest::Operation;
26using Protocol = patchpanel::ModifyPortRuleRequest::Protocol;
27using RuleType = patchpanel::ModifyPortRuleRequest::RuleType;
28
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090029const std::string ProtocolName(Protocol proto);
30
31class Firewall {
32 public:
33 typedef std::pair<uint16_t, std::string> Hole;
34
Hugo Benichi283a7812021-06-08 00:47:54 +090035 Firewall();
36 Firewall(MinijailedProcessRunner* process_runner);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090037 Firewall(const Firewall&) = delete;
38 Firewall& operator=(const Firewall&) = delete;
39
Hugo Benichi283a7812021-06-08 00:47:54 +090040 virtual ~Firewall() = default;
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090041
Hugo Benichi283a7812021-06-08 00:47:54 +090042 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 Iman54c046f2020-06-23 23:12:00 +090051 const std::string& input_ip,
52 uint16_t port,
53 const std::string& interface,
54 const std::string& dst_ip,
Hugo Benichi283a7812021-06-08 00:47:54 +090055 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 Iman54c046f2020-06-23 23:12:00 +090062
Hugo Benichi283a7812021-06-08 00:47:54 +090063 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 Iman54c046f2020-06-23 23:12:00 +0900104};
105
106} // namespace patchpanel
107
108#endif // PATCHPANEL_FIREWALL_H_