Documentation ¶
Index ¶
- Constants
- Variables
- func GenSetKeyType(types ...nftables.SetDatatype) nftables.SetDatatype
- func InitConn(netns ...int) *nftables.Conn
- func IsNFTablesOn() bool
- func L3Protocol(proto int) *uint32
- func MakeConcatElement(keys []nftables.SetDatatype, vals []ElementValue, ra *RuleAction) (*nftables.SetElement, error)
- func MakeElement(input *ElementValue) ([]nftables.SetElement, error)
- func MakeRuleComment(s string) []byte
- func SetPortList(ports []int) []*uint16
- func SetPortRange(ports [2]int) [2]*uint16
- type ChainAttributes
- type ChainFuncs
- type ChainPolicy
- type ChainsInterface
- type Concat
- type ConcatElement
- type Conntrack
- type Counter
- type Dynamic
- type ElementValue
- type Fib
- type IPAddr
- type IPAddrSpec
- type L3Rule
- type L4Rule
- type Log
- type MatchAct
- type MatchType
- type Meta
- type MetaExpr
- type MetaMark
- type NATAttributes
- type NetNS
- type Operator
- type Port
- type Rule
- type RuleAction
- func SetDNAT(natAttrs *NATAttributes) (*RuleAction, error)
- func SetLoadbalance(chains []string, action int, mode int) (*RuleAction, error)
- func SetMasq(random, fullyRandom, persistent bool) (*RuleAction, error)
- func SetMasqToPort(port ...int) (*RuleAction, error)
- func SetRedirect(port int, tproxy bool) (*RuleAction, error)
- func SetReject(rt int, rc int) (*RuleAction, error)
- func SetSNAT(natAttrs *NATAttributes) (*RuleAction, error)
- func SetVerdict(key int, chain ...string) (*RuleAction, error)
- type RuleFuncs
- type RulesInterface
- type SetAttributes
- type SetFuncs
- type SetRef
- type SetsInterface
- type TableFuncs
- type TablesInterface
Constants ¶
const ( // ChainPolicyAccept defines "accept" chain policy ChainPolicyAccept ChainPolicy = 1 // ChainPolicyDrop defines "drop" chain policy ChainPolicyDrop ChainPolicy = 0 // ChainReadyTimeout defines maximum time to wait for a chain to be ready ChainReadyTimeout = time.Millisecond * 100 // ChainDeleteTimeout defines maximum time to wait for a chain to be ready ChainDeleteTimeout = time.Second * 60 )
const ( // NFT_DROP defines Drop action for a verdict NFT_DROP = 0x0 // NFT_ACCEPT defines Accept action for a verdict NFT_ACCEPT = 0x1 )
By some reason github.com/golang/unix does not define these constants but they can by used in a Verdict.
const (
// MaxCommentLength defines Maximum Length of Rule's Comment field
MaxCommentLength = 127
)
Variables ¶
var ( CTStateNew uint32 = 0x08000000 CTStateRelated uint32 = 0x04000000 CTStateEstablished uint32 = 0x02000000 CTStateInvalid uint32 = 0x01000000 )
Define States of Connection tracking State key
Functions ¶
func GenSetKeyType ¶
func GenSetKeyType(types ...nftables.SetDatatype) nftables.SetDatatype
GenSetKeyType generates a composite key type, combining all types
func IsNFTablesOn ¶
func IsNFTablesOn() bool
IsNFTablesOn detects whether nf_tables module is loaded or not, it return true is ListChains call succeeds, otherwise it return false.
func L3Protocol ¶
L3Protocol is a helper function to convert a value of L3 protocol to the type required by L3Rule *uint32
func MakeConcatElement ¶
func MakeConcatElement(keys []nftables.SetDatatype, vals []ElementValue, ra *RuleAction) (*nftables.SetElement, error)
MakeConcatElement creates an element of a set/map as a concatination of standard SetDatatypes example: nftables.TypeIPAddr and nftables.TypeInetService
func MakeElement ¶
func MakeElement(input *ElementValue) ([]nftables.SetElement, error)
MakeElement creates a list of Elements for IPv4 or IPv6 address, slice of IPAddrElement carries IP address which will be used as a key in the element, and 3 possible values depending on the type of a set. Value could be IP address as a string, Port as uint16 and a nftables.Verdict For IPv4 addresses ipv4 bool should be set to true, otherwise IPv6 addresses are expected.
func MakeRuleComment ¶
MakeRuleComment makes NFTNL_UDATA_RULE_COMMENT TLV. Length of TLV is 1 bytes as a result, the maximum comment length is 254 bytes.
func SetPortList ¶
SetPortList is a helper function which transforms a slice of int into a format required by Port struct
func SetPortRange ¶
SetPortRange is a helper function which transforms an 2 element array of int into a format required by Port struct
Types ¶
type ChainAttributes ¶
type ChainAttributes struct { Type nftables.ChainType Hook *nftables.ChainHook Priority *nftables.ChainPriority Device string Policy *ChainPolicy }
ChainAttributes defines attributes which can be apply to a chain of BASE type
func (*ChainAttributes) Validate ¶
func (cha *ChainAttributes) Validate() error
Validate validate attributes passed for a base chain creation
type ChainFuncs ¶
type ChainFuncs interface { Chain(name string) (RulesInterface, error) Create(name string, attributes *ChainAttributes) error CreateImm(name string, attributes *ChainAttributes) error Delete(name string) error DeleteImm(name string) error Exist(name string) bool Sync() error Dump() ([]byte, error) Get() ([]string, error) }
ChainFuncs defines funcations to operate with chains
type ChainsInterface ¶
type ChainsInterface interface {
Chains() ChainFuncs
}
ChainsInterface defines third level interface operating with nf chains
type Concat ¶
type Concat struct { Elements []*ConcatElement // VMap defines if concatination is used with verdict map, if set to true // Rule's Action will be ignored as the action is stored in the verdict of the map. VMap bool // SetRef defines name and id of map for SetRef *SetRef }
Concat defines parameters of Concatination rule
type ConcatElement ¶
type ConcatElement struct { // Etype defines an element type as defined in github.com/google/nftables // example nftables.InetService or nftables.IPAddr EType nftables.SetDatatype // EProto defines a protocol as defined in golang.org/x/sys/unix EProto byte // ESource defines a direction, if true then element is saddr or sport, // if false then daddr or dport ESource bool // EMask defines mask of the element, mostly used along with IPAddr EMask []byte }
ConcatElement defines 1 element of Concatination rule
type Dynamic ¶
type Dynamic struct { Match MatchType // Op defines an operation, supported operations are Add and Update. Op uint32 // Key defines a key to use for a new entry added to a Set or Map. Key uint32 // SetRef defines a reference to the Set or Map that gets updated. SetRef *SetRef // Timeout defines an aging timeout for a new entry. Timeout time.Duration Invert bool }
Dynamic defines a rule which dynamically add or update a Set or Map based on an incoming packet.
type ElementValue ¶
type ElementValue struct { Addr string Port *uint16 AddrIP *string Action *RuleAction // New members Integer *uint32 IPAddr []byte EtherAddr []byte InetProto *byte InetService *uint16 Mark *uint32 }
ElementValue defines key:value of the element of the type nftables.TypeIPAddr if IPAddrElement is element of a basic set, then only Addr will be specified, if it is element of a map then either Port or AddrIP and if it is element of a vmap, then Verdict.
type Fib ¶
type Fib struct { ResultOIF bool ResultOIFNAME bool ResultADDRTYPE bool FlagSADDR bool FlagDADDR bool FlagMARK bool FlagIIF bool FlagOIF bool FlagPRESENT bool RelOp Operator Data []byte }
Fib defines nftables Fib expression. Results and Flags can have multiple selections. Data is a slice of bytes, its content depends up on Result and Flags combination. Example: if fib expression specifies a particular address type, then Data would carry one of constants defined in golang.org/x/sys/unix RTN_UNICAST = 0x1 RTN_LOCAL = 0x2 RTN_BROADCAST = 0x3 RTN_ANYCAST = 0x4 RTN_MULTICAST = 0x5 RTN_BLACKHOLE = 0x6 RTN_UNREACHABLE = 0x7 RTN_PROHIBIT = 0x8 RTN_THROW = 0x9 RTN_NAT = 0xa RTN_XRESOLVE = 0xb
type IPAddr ¶
IPAddr defines a type of ip address, if it is host address with mask of 32 for ipv4 and mask of 128 for ipv6 then CIDR should be false, if it is a network address, then CIDR should be true and Mask set to a number of bits in the address' mask. Mask value is from 0 to 32 for ipv4 and from 0 to 128 for ipv6 addresses.
func NewIPAddr ¶
NewIPAddr is a helper function which converts ip address into IPAddr format required by IPAddrSpec. If CIDR format is specified, Mask will be set to address' subnet mask and CIDR will e set to true
type IPAddrSpec ¶
IPAddrSpec lists possible flavours if specifying ip address, either List or Range can be specified
func (*IPAddrSpec) Validate ¶
func (ip *IPAddrSpec) Validate() error
Validate checks IPAddrSpec struct
type L3Rule ¶
type L3Rule struct { Src *IPAddrSpec Dst *IPAddrSpec Version *byte Protocol *uint32 RelOp Operator Counter *Counter }
L3Rule contains parameters for L3 based rule, either Source or Destination can be specified
type MatchAct ¶
type MatchAct struct { Match MatchType // MatchRef defines a reference to the named map { match criteria : integer } MatchRef *SetRef // ActElements defines a slice elements of type { integer : action }, these will be placed into // the anonymous action map. ActElement map[int]*RuleAction }
MatchAct rule defines a special type of rules (no support yet by nft cli tool), where matching is done by referring to a named map { match criteria : integer } and action is defined in an anonymous vmap { integer : action }. The match returns a key which is used as input for action lookup in the anonymous vmap.
type MatchType ¶
type MatchType uint32
MatchType defines a matching criteria for an incoming packet. Only one of the criterias can be specified.
type MetaExpr ¶
MetaExpr allows specifing Meta expressions by meta key and its value, example Key: unix.NFT_META_SKGID and Value: 1024
type MetaMark ¶
MetaMark defines Mark keyword of Meta key Mark can be used either to Set or Match a mark. If Set is true, then the Value will be used to mark a packet, and if Set is false, then the Value will be used to match packet's mark against it. Mask can be used to test for or to set only particular bits in mark. If mask is 0, than it is not used at all.
type NATAttributes ¶
type NATAttributes struct { L3Addr [2]*IPAddr Port [2]uint16 FullyRandom bool Random bool Persistent bool }
NATAttributes defines parameters used to generate nftables nat rule it is used as input parameter to two helper functions SetSNAT and SetDNAT Either L3Addr or Port must be defined. When 2 elements of array are specified, then the range of either ip addresses or ports will be specified in NAT rule.
type NetNS ¶
type NetNS interface { Flush() error FlushRuleset() AddTable(*nftables.Table) *nftables.Table DelTable(*nftables.Table) ListTables() ([]*nftables.Table, error) AddChain(*nftables.Chain) *nftables.Chain DelChain(*nftables.Chain) ListChains() ([]*nftables.Chain, error) AddRule(*nftables.Rule) *nftables.Rule InsertRule(*nftables.Rule) *nftables.Rule ReplaceRule(*nftables.Rule) *nftables.Rule DelRule(*nftables.Rule) error GetRule(*nftables.Table, *nftables.Chain) ([]*nftables.Rule, error) AddSet(*nftables.Set, []nftables.SetElement) error DelSet(*nftables.Set) GetSets(*nftables.Table) ([]*nftables.Set, error) GetSetByName(*nftables.Table, string) (*nftables.Set, error) GetSetElements(*nftables.Set) ([]nftables.SetElement, error) SetAddElements(*nftables.Set, []nftables.SetElement) error SetDeleteElements(*nftables.Set, []nftables.SetElement) error }
NetNS defines interface needed to nf tables
type Rule ¶
type Rule struct { Concat *Concat Dynamic *Dynamic MatchAct *MatchAct Fib *Fib L3 *L3Rule L4 *L4Rule Conntracks []*Conntrack Meta *Meta Log *Log RelOp Operator Counter *Counter Action *RuleAction UserData []byte // Position identifies the desired position of the rule, depending on the operation // Add, Insert or Replace, the resulting position may vary. // AddRule with position 0, will add a rule to the end of the chain // AddRule with position != 0, will add a rule right after the rule with specified position // InsertRule with position 0 will insert a rule at the beginning of the chain // InsertRule with position != 0 will insert a rule right before the rule with specified position // Replace operation with position 0 will fail. Position int }
Rule contains parameters for a rule to configure, only L3 OR L4 parameters can be specified
type RuleAction ¶
type RuleAction struct {
// contains filtered or unexported fields
}
RuleAction defines what action needs to be executed on the rule match
func SetDNAT ¶
func SetDNAT(natAttrs *NATAttributes) (*RuleAction, error)
SetDNAT builds RuleAction struct for DNAT action
func SetLoadbalance ¶
func SetLoadbalance(chains []string, action int, mode int) (*RuleAction, error)
SetLoadbalance builds RuleAction struct for Verdict based actions, action parameter defines whether unix.NFT_JUMP (default) or unix.NFT_GOTO will be used to reach one of load balanced chains mode parameters defines the mode of load balancing between the chains; unix.NFT_NG_RANDOM (default) or unix.NFT_NG_INCREMENTAL.
func SetMasq ¶
func SetMasq(random, fullyRandom, persistent bool) (*RuleAction, error)
SetMasq builds RuleAction struct for Masquerade action
func SetMasqToPort ¶
func SetMasqToPort(port ...int) (*RuleAction, error)
SetMasqToPort builds RuleAction struct for Masquerade action
func SetRedirect ¶
func SetRedirect(port int, tproxy bool) (*RuleAction, error)
SetRedirect builds RuleAction struct for Redirect action
func SetReject ¶
func SetReject(rt int, rc int) (*RuleAction, error)
SetReject builds RuleAction struct for Reject action, rt defines Reject type ICMP or TCP rc defines ICMP Reject Code
func SetSNAT ¶
func SetSNAT(natAttrs *NATAttributes) (*RuleAction, error)
SetSNAT builds RuleAction struct for SNAT action
func SetVerdict ¶
func SetVerdict(key int, chain ...string) (*RuleAction, error)
SetVerdict builds RuleAction struct for Verdict based actions
func (*RuleAction) Validate ¶
func (ra *RuleAction) Validate() error
Validate method validates RuleAction parameters and returns error if inconsistency if found
type RuleFuncs ¶
type RuleFuncs interface { Create(*Rule) (uint32, error) CreateImm(*Rule) (uint64, error) Delete(uint32) error DeleteImm(uint64) error Insert(*Rule) (uint32, error) InsertImm(*Rule) (uint64, error) Update(*Rule, uint64) error Dump() ([]byte, error) Sync() error UpdateRulesHandle() error GetRuleHandle(id uint32) (uint64, error) GetRulesUserData() (map[uint64][]byte, error) GetRulesExpr() (map[uint64][]expr.Any, error) }
RuleFuncs defines funcations to operate with Rules
type RulesInterface ¶
type RulesInterface interface {
Rules() RuleFuncs
}
RulesInterface defines third level interface operating with nf Rules
type SetAttributes ¶
type SetAttributes struct { Name string Constant bool IsMap bool HasTimeout bool Timeout time.Duration // Interval flag must be set only when the set elements are ranges, address ranges or port ranges Interval bool KeyType nftables.SetDatatype DataType nftables.SetDatatype }
SetAttributes defines parameters of a nftables Set
type SetFuncs ¶
type SetFuncs interface { CreateSet(*SetAttributes, []nftables.SetElement) (*nftables.Set, error) DelSet(string) error GetSets() ([]*nftables.Set, error) GetSetByName(string) (*nftables.Set, error) GetSetElements(string) ([]nftables.SetElement, error) SetAddElements(string, []nftables.SetElement) error SetDelElements(string, []nftables.SetElement) error }
SetFuncs defines funcations to operate with nftables Sets
type SetsInterface ¶
type SetsInterface interface {
Sets() SetFuncs
}
SetsInterface defines third level interface operating with nf maps
type TableFuncs ¶
type TableFuncs interface { Table(name string, familyType nftables.TableFamily) (ChainsInterface, error) TableChains(name string, familyType nftables.TableFamily) (ChainsInterface, error) TableSets(name string, familyType nftables.TableFamily) (SetsInterface, error) Create(name string, familyType nftables.TableFamily) error Delete(name string, familyType nftables.TableFamily) error CreateImm(name string, familyType nftables.TableFamily) error DeleteImm(name string, familyType nftables.TableFamily) error Exist(name string, familyType nftables.TableFamily) bool Get(familyType nftables.TableFamily) ([]string, error) Sync(familyType nftables.TableFamily) error Dump() ([]byte, error) }
TableFuncs defines second level interface operating with nf tables
type TablesInterface ¶
type TablesInterface interface {
Tables() TableFuncs
}
TablesInterface defines a top level interface
func InitNFTables ¶
func InitNFTables(conn NetNS) TablesInterface
InitNFTables initializes netlink connection of the nftables family