blob: 5e45eed7d413644637c631f5665df5c468727070 [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
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090016#include <brillo/errors/error.h>
17#include <gtest/gtest_prod.h>
18#include <patchpanel/proto_bindings/patchpanel_service.pb.h>
19
Hugo Benichi283a7812021-06-08 00:47:54 +090020#include "patchpanel/minijailed_process_runner.h"
21
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090022namespace patchpanel {
23
24using Operation = patchpanel::ModifyPortRuleRequest::Operation;
25using Protocol = patchpanel::ModifyPortRuleRequest::Protocol;
26using RuleType = patchpanel::ModifyPortRuleRequest::RuleType;
27
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090028const std::string ProtocolName(Protocol proto);
29
30class Firewall {
31 public:
32 typedef std::pair<uint16_t, std::string> Hole;
33
Hugo Benichi283a7812021-06-08 00:47:54 +090034 Firewall();
35 Firewall(MinijailedProcessRunner* process_runner);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090036 Firewall(const Firewall&) = delete;
37 Firewall& operator=(const Firewall&) = delete;
38
Hugo Benichi283a7812021-06-08 00:47:54 +090039 virtual ~Firewall() = default;
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090040
Hugo Benichi283a7812021-06-08 00:47:54 +090041 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 Iman54c046f2020-06-23 23:12:00 +090050 const std::string& input_ip,
51 uint16_t port,
52 const std::string& interface,
53 const std::string& dst_ip,
Hugo Benichi283a7812021-06-08 00:47:54 +090054 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 Iman54c046f2020-06-23 23:12:00 +090061
Hugo Benichi283a7812021-06-08 00:47:54 +090062 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 Iman54c046f2020-06-23 23:12:00 +0900103};
104
105} // namespace patchpanel
106
107#endif // PATCHPANEL_FIREWALL_H_