blob: 0b8365a80f640bb83f3e79a37bd01f8013b7ffd1 [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;
36 ~Firewall() = default;
37
38 bool AddAcceptRules(Protocol protocol,
39 uint16_t port,
40 const std::string& interface);
41 bool DeleteAcceptRules(Protocol protocol,
42 uint16_t port,
43 const std::string& interface);
44 bool AddLoopbackLockdownRules(Protocol protocol, uint16_t port);
45 bool DeleteLoopbackLockdownRules(Protocol protocol, uint16_t port);
46 bool AddIpv4ForwardRule(Protocol protocol,
47 const std::string& input_ip,
48 uint16_t port,
49 const std::string& interface,
50 const std::string& dst_ip,
51 uint16_t dst_port);
52 bool DeleteIpv4ForwardRule(Protocol protocol,
53 const std::string& input_ip,
54 uint16_t port,
55 const std::string& interface,
56 const std::string& dst_ip,
57 uint16_t dst_port);
58
59 private:
60 friend class FirewallTest;
61 // Adds ACCEPT chain rules to the filter INPUT chain.
62 virtual bool AddAcceptRule(const std::string& executable_path,
63 Protocol protocol,
64 uint16_t port,
65 const std::string& interface);
66 // Removes ACCEPT chain rules from the filter INPUT chain.
67 virtual bool DeleteAcceptRule(const std::string& executable_path,
68 Protocol protocol,
69 uint16_t port,
70 const std::string& interface);
71 // Adds or removes MASQUERADE chain rules to/from the nat PREROUTING chain.
72 virtual bool ModifyIpv4DNATRule(Protocol protocol,
73 const std::string& input_ip,
74 uint16_t port,
75 const std::string& interface,
76 const std::string& dst_ip,
77 uint16_t dst_port,
78 const std::string& operation);
79 // Adds or removes ACCEPT chain rules to/from the filter FORWARD chain.
80 virtual bool ModifyIpv4ForwardChain(Protocol protocol,
81 const std::string& interface,
82 const std::string& dst_ip,
83 uint16_t dst_port,
84 const std::string& operation);
85 virtual bool AddLoopbackLockdownRule(const std::string& executable_path,
86 Protocol protocol,
87 uint16_t port);
88 virtual bool DeleteLoopbackLockdownRule(const std::string& executable_path,
89 Protocol protocol,
90 uint16_t port);
91
92 virtual int RunInMinijail(const std::vector<std::string>& argv);
93
94 DISALLOW_COPY_AND_ASSIGN(Firewall);
95};
96
97} // namespace patchpanel
98
99#endif // PATCHPANEL_FIREWALL_H_