blob: 6695d6ef8d5eddbf05cac9c4fc2f9848d5227049 [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
10#include <set>
11#include <string>
12#include <utility>
13#include <vector>
14
15#include <base/macros.h>
16#include <brillo/errors/error.h>
17#include <gtest/gtest_prod.h>
18#include <patchpanel/proto_bindings/patchpanel_service.pb.h>
19
20namespace patchpanel {
21
22using Operation = patchpanel::ModifyPortRuleRequest::Operation;
23using Protocol = patchpanel::ModifyPortRuleRequest::Protocol;
24using RuleType = patchpanel::ModifyPortRuleRequest::RuleType;
25
26extern const char kIpTablesPath[];
27extern const char kIp6TablesPath[];
28
29const std::string ProtocolName(Protocol proto);
30
31class Firewall {
32 public:
33 typedef std::pair<uint16_t, std::string> Hole;
34
35 Firewall() = default;
Qijiang Fan6bc59e12020-11-11 02:51:06 +090036 Firewall(const Firewall&) = delete;
37 Firewall& operator=(const Firewall&) = delete;
38
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090039 ~Firewall() = default;
40
41 bool AddAcceptRules(Protocol protocol,
42 uint16_t port,
43 const std::string& interface);
44 bool DeleteAcceptRules(Protocol protocol,
45 uint16_t port,
46 const std::string& interface);
47 bool AddLoopbackLockdownRules(Protocol protocol, uint16_t port);
48 bool DeleteLoopbackLockdownRules(Protocol protocol, uint16_t port);
49 bool AddIpv4ForwardRule(Protocol protocol,
50 const std::string& input_ip,
51 uint16_t port,
52 const std::string& interface,
53 const std::string& dst_ip,
54 uint16_t dst_port);
55 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);
61
62 private:
63 friend class FirewallTest;
64 // Adds ACCEPT chain rules to the filter INPUT chain.
65 virtual bool AddAcceptRule(const std::string& executable_path,
66 Protocol protocol,
67 uint16_t port,
68 const std::string& interface);
69 // Removes ACCEPT chain rules from the filter INPUT chain.
70 virtual bool DeleteAcceptRule(const std::string& executable_path,
71 Protocol protocol,
72 uint16_t port,
73 const std::string& interface);
74 // Adds or removes MASQUERADE chain rules to/from the nat PREROUTING chain.
75 virtual bool ModifyIpv4DNATRule(Protocol protocol,
76 const std::string& input_ip,
77 uint16_t port,
78 const std::string& interface,
79 const std::string& dst_ip,
80 uint16_t dst_port,
81 const std::string& operation);
82 // Adds or removes ACCEPT chain rules to/from the filter FORWARD chain.
83 virtual bool ModifyIpv4ForwardChain(Protocol protocol,
84 const std::string& interface,
85 const std::string& dst_ip,
86 uint16_t dst_port,
87 const std::string& operation);
88 virtual bool AddLoopbackLockdownRule(const std::string& executable_path,
89 Protocol protocol,
90 uint16_t port);
91 virtual bool DeleteLoopbackLockdownRule(const std::string& executable_path,
92 Protocol protocol,
93 uint16_t port);
94
95 virtual int RunInMinijail(const std::vector<std::string>& argv);
Jason Jeremy Iman54c046f2020-06-23 23:12:00 +090096};
97
98} // namespace patchpanel
99
100#endif // PATCHPANEL_FIREWALL_H_