Dobby 3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Loading...
Searching...
No Matches
Netfilter.h
1/*
2* If not stated otherwise in this file or this component's LICENSE file the
3* following copyright and licenses apply:
4*
5* Copyright 2019 Sky UK
6*
7* Licensed under the Apache License, Version 2.0 (the "License");
8* you may not use this file except in compliance with the License.
9* You may obtain a copy of the License at
10*
11* http://www.apache.org/licenses/LICENSE-2.0
12*
13* Unless required by applicable law or agreed to in writing, software
14* distributed under the License is distributed on an "AS IS" BASIS,
15* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16* See the License for the specific language governing permissions and
17* limitations under the License.
18*/
19/*
20 * File: Netfilter.h
21 *
22 */
23#ifndef NETFILTER_H
24#define NETFILTER_H
25
26#include <map>
27#include <list>
28#include <string>
29#include <mutex>
30
31
32// -----------------------------------------------------------------------------
45{
46public:
47 Netfilter();
48 ~Netfilter() = default;
49
50public:
51 enum class TableType { Invalid, Raw, Nat, Mangle, Filter, Security };
52 typedef std::map<TableType, std::list<std::string>> RuleSet;
53
54 RuleSet rules(const int ipVersion) const;
55 bool setRules(const RuleSet &ruleSet, const int ipVersion);
56
57 enum class Operation { Append, Insert, Delete, Unchanged };
58
59 bool addRules(RuleSet &ruleSet, const int ipVersion, Operation operation);
60
61 bool createNewChain(TableType table, const std::string &name,
62 const int ipVersion);
63
64 bool applyRules(const int ipVersion);
65
66private:
67 bool forkExec(const std::string &file,
68 const std::list<std::string> &args,
69 int stdinFd, int stdoutFd, int stderrFd) const;
70
71 bool writeString(int fd, const std::string &str) const;
72
73 RuleSet getRuleSet(const int ipVersion) const;
74
75 bool ruleInList(const std::string &rule,
76 const std::list<std::string> &rulesList) const;
77
78 typedef struct RuleSets
79 {
80 RuleSet appendRuleSet;
81 RuleSet insertRuleSet;
82 RuleSet deleteRuleSet;
83 RuleSet unchangedRuleSet;
84 } RuleSets;
85
86 RuleSets mIpv4RuleCache;
87 RuleSets mIpv6RuleCache;
88
89 void trimDuplicates(RuleSet &existing, RuleSet &newRuleSet,
90 Operation operation) const;
91 bool checkDuplicates(RuleSets ruleCache, const int ipVersion) const;
92
93 void dump(const RuleSet &ruleSet, const char *title = nullptr) const;
94
95 typedef struct IptablesVersion
96 {
97 int major;
98 int minor;
99 int patch;
101
103
104private:
105 mutable std::mutex mLock;
106 IptablesVersion mIptablesVersion;
107};
108
109#endif // !defined(NETFILTER_H)
Class that can read / write iptables rule sets.
Definition Netfilter.h:45
bool writeString(int fd, const std::string &str) const
Writes the string into the supplied file descriptor.
Definition Netfilter.cpp:681
bool ruleInList(const std::string &rule, const std::list< std::string > &rulesList) const
Returns true if the rule is in the rulesList.
Definition Netfilter.cpp:728
bool addRules(RuleSet &ruleSet, const int ipVersion, Operation operation)
Adds rules to the internal rule caches.
Definition Netfilter.cpp:763
bool forkExec(const std::string &file, const std::list< std::string > &args, int stdinFd, int stdoutFd, int stderrFd) const
Performs a fork/exec operation and waits for the child to terminate.
Definition Netfilter.cpp:105
RuleSet getRuleSet(const int ipVersion) const
Uses the iptables-save tool to get the current rules.
Definition Netfilter.cpp:240
bool createNewChain(TableType table, const std::string &name, const int ipVersion)
Creates a new IPTables chain with the given name and put it in the rule cache to write later.
Definition Netfilter.cpp:828
bool checkDuplicates(RuleSets ruleCache, const int ipVersion) const
Checks all rulesets in a rule cache for duplicates to check which rules need to be applied.
Definition Netfilter.cpp:434
bool applyRules(const int ipVersion)
Uses the iptables-restore tool to apply the rules stored in mRulesets.
Definition Netfilter.cpp:484
void dump(const RuleSet &ruleSet, const char *title=nullptr) const
Debugging function to print out the supplied ruleset.
Definition Netfilter.cpp:863
void trimDuplicates(RuleSet &existing, RuleSet &newRuleSet, Operation operation) const
Trims duplicates from mRuleSets based on the operation.
Definition Netfilter.cpp:381
IptablesVersion getIptablesVersion() const
Definition Netfilter.cpp:892
RuleSet rules(const int ipVersion) const
Returns the current iptables ruleset.
Definition Netfilter.cpp:714
Definition Netfilter.h:96
Definition Netfilter.h:79