Documentation ¶
Overview ¶
Package netfilter provides encoding and decoding of Netlink messages into Netfilter attributes. It handles Netfilter-specific nesting of attributes, endianness, and is written around a native Netlink implementation (https://github.com/mdlayher/netlink). It is purely written in Go, without any dependency on Cgo or any C library, kernel headers or userspace tools.
The goal of this package is to be used for implementing the Netfilter family of Netlink protocols. For an example implementation, see https://github.com/ti-mo/conntrack.
Index ¶
- Variables
- func EncodeNetlink(h Header, ae *netlink.AttributeEncoder) (netlink.Message, error)
- func MarshalAttributes(attrs []Attribute) ([]byte, error)
- func MarshalNetlink(h Header, attrs []Attribute) (netlink.Message, error)
- func NewAttributeDecoder(b []byte) (*netlink.AttributeDecoder, error)
- func NewAttributeEncoder() *netlink.AttributeEncoder
- func Uint16Bytes(u uint16) []byte
- func Uint32Bytes(u uint32) []byte
- func Uint64Bytes(u uint64) []byte
- func UnmarshalNetlink(msg netlink.Message) (Header, []Attribute, error)
- type Attribute
- func (a Attribute) Int32() int32
- func (a Attribute) Int64() int64
- func (a *Attribute) PutUint16(v uint16)
- func (a *Attribute) PutUint32(v uint32)
- func (a *Attribute) PutUint64(v uint64)
- func (a Attribute) String() string
- func (a Attribute) Uint16() uint16
- func (a Attribute) Uint32() uint32
- func (a Attribute) Uint64() uint64
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) IsMulticast() bool
- func (c *Conn) JoinGroups(groups []NetlinkGroup) error
- func (c *Conn) LeaveGroups(groups []NetlinkGroup) error
- func (c *Conn) Query(nlm netlink.Message) ([]netlink.Message, error)
- func (c *Conn) Receive() ([]netlink.Message, error)
- func (c *Conn) SetDeadline(t time.Time) error
- func (c *Conn) SetOption(option netlink.ConnOption, enable bool) error
- func (c *Conn) SetReadBuffer(bytes int) error
- func (c *Conn) SetReadDeadline(t time.Time) error
- func (c *Conn) SetWriteBuffer(bytes int) error
- func (c *Conn) SetWriteDeadline(t time.Time) error
- type Header
- type MessageType
- type NetlinkGroup
- type ProtoFamily
- type SubsystemID
Constants ¶
This section is empty.
Variables ¶
var ( // GroupsCT is a list of all Conntrack multicast groups. GroupsCT = []NetlinkGroup{GroupCTNew, GroupCTUpdate, GroupCTDestroy} // GroupsCTExp is a list of all Conntrack-expect multicast groups. GroupsCTExp = []NetlinkGroup{GroupCTExpNew, GroupCTExpUpdate, GroupCTExpDestroy} )
Functions ¶
func EncodeNetlink ¶ added in v0.3.0
EncodeNetlink generates a netlink.Message based on a given netfilter header h and a pre-filled netlink.AttributeEncoder ae.
func MarshalAttributes ¶ added in v0.3.0
MarshalAttributes marshals a nested attribute structure into a byte slice. This byte slice can then be copied into a netlink.Message's Data field after the nfHeaderLen offset.
func MarshalNetlink ¶
MarshalNetlink takes a Netfilter Header and Attributes and returns a netlink.Message.
func NewAttributeDecoder ¶ added in v0.3.0
func NewAttributeDecoder(b []byte) (*netlink.AttributeDecoder, error)
NewAttributeDecoder instantiates a new netlink.AttributeDecoder configured with a Big Endian byte order.
func NewAttributeEncoder ¶ added in v0.3.0
func NewAttributeEncoder() *netlink.AttributeEncoder
NewAttributeEncoder instantiates a new netlink.AttributeEncoder configured with a Big Endian byte order.
func Uint16Bytes ¶
Uint16Bytes gets the big-endian 2-byte representation of a uint16.
func Uint32Bytes ¶
Uint32Bytes gets the big-endian 4-byte representation of a uint32.
func Uint64Bytes ¶
Uint64Bytes gets the big-endian 8-byte representation of a uint64.
Types ¶
type Attribute ¶
type Attribute struct { // The type of this Attribute, typically matched to a constant. Type uint16 // An arbitrary payload which is specified by Type. Data []byte // Whether the attribute's data contains nested attributes. Nested bool Children []Attribute // Whether the attribute's data is in network (true) or native (false) byte order. NetByteOrder bool }
An Attribute is a copy of a netlink.Attribute that can be nested.
func UnmarshalAttributes ¶ added in v0.3.0
UnmarshalAttributes unmarshals a byte slice into a list of Attributes.
func (*Attribute) PutUint16 ¶
PutUint16 sets the Attribute's data field to a Uint16 encoded in net byte order.
func (*Attribute) PutUint32 ¶
PutUint32 sets the Attribute's data field to a Uint32 encoded in net byte order.
func (*Attribute) PutUint64 ¶
PutUint64 sets the Attribute's data field to a Uint64 encoded in net byte order.
func (Attribute) Uint16 ¶
Uint16 interprets a non-nested Netfilter attribute in network byte order as a uint16.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn represents a Netlink connection to the Netfilter subsystem.
func Dial ¶
Dial opens a new Netlink connection to the Netfilter subsystem and returns it wrapped in a Conn structure.
func (*Conn) IsMulticast ¶
IsMulticast returns the Conn's Multicast flag. It is set by calling Listen().
func (*Conn) JoinGroups ¶
func (c *Conn) JoinGroups(groups []NetlinkGroup) error
JoinGroups attaches the Netlink socket to one or more Netfilter multicast groups. Marks the Conn as Multicast, meaning it can no longer be used for any queries.
func (*Conn) LeaveGroups ¶
func (c *Conn) LeaveGroups(groups []NetlinkGroup) error
LeaveGroups detaches the Netlink socket from one or more Netfilter multicast groups. Does not remove the Multicast flag, open a separate Conn for making queries instead.
func (*Conn) Query ¶
Query sends a Netfilter message over Netlink and validates the response. The call will fail if the Conn is marked as Multicast. Any errors returned from the underlying Netlink layer are wrapped using pkg/errors.Wrap(). Use errors.Cause() to unwrap to compare to Errno.
func (*Conn) Receive ¶
Receive executes a blocking read on the underlying Netlink socket and returns a Message.
func (*Conn) SetDeadline ¶ added in v0.3.1
SetDeadline sets the read and write deadlines associated with the connection.
Deadline functionality is only supported on Go 1.12+. Calling this function on older versions of Go will result in an error.
func (*Conn) SetOption ¶
func (c *Conn) SetOption(option netlink.ConnOption, enable bool) error
SetOption enables or disables a netlink socket option for the Conn.
func (*Conn) SetReadBuffer ¶ added in v0.3.1
SetReadBuffer sets the size of the operating system's receive buffer associated with the Conn.
func (*Conn) SetReadDeadline ¶ added in v0.3.1
SetReadDeadline sets the read deadline associated with the connection.
Deadline functionality is only supported on Go 1.12+. Calling this function on older versions of Go will result in an error.
func (*Conn) SetWriteBuffer ¶ added in v0.3.1
SetWriteBuffer sets the size of the operating system's transmit buffer associated with the Conn.
type Header ¶
type Header struct { // Netlink header flags, to (un)marshal to a netlink Message in a single operation Flags netlink.HeaderFlags // netlink Header Type SubsystemID SubsystemID MessageType MessageType // nfgenmsg Family ProtoFamily Version uint8 // Usually NFNETLINK_V0 (Go: NFNLv0) ResourceID uint16 }
Header is an abstraction over the Netlink header's Type field and the Netfilter message header, also known as 'nfgenmsg'.
The Netlink header's Type field is divided into two bytes by netfilter: the most significant byte is the subsystem ID and the least significant is the message type. The significance of the MessageType field fully depends on the subsystem the message is for (eg. conntrack). This package is only responsible for splitting the field and providing a list of known SubsystemIDs. Subpackages use the MessageType field to implement subsystem-specific behaviour.
nfgenmsg holds the protocol family, version and resource ID of the Netfilter message. Family describes a protocol family that can be managed using Netfilter (eg. IPv4/6, ARP, Bridge) Version is a protocol version descriptor, and always set to 0 (NFNETLINK_V0) ResourceID is a generic field specific to the upper layer protocol (eg. CPU ID of Conntrack stats)
func DecodeNetlink ¶ added in v0.3.0
DecodeNetlink returns msg's Netfilter header and an AttributeDecoder that can be used to iteratively decode all Netlink attributes contained in the message.
type MessageType ¶
type MessageType uint8
MessageType denotes the message type specific to the subsystem. Its meaning can only be determined after decoding the Netfilter Subsystem type, because it only has meaning in that context. Possible values and string representations need to be implemented in a subsystem-specific package.
type NetlinkGroup ¶
type NetlinkGroup uint8
NetlinkGroup represents the multicast groups that can be joined with a Netlink socket.
const ( GroupNone NetlinkGroup = iota // NFNLGRP_NONE GroupCTNew // NFNLGRP_CONNTRACK_NEW GroupCTUpdate // NFNLGRP_CONNTRACK_UPDATE GroupCTDestroy // NFNLGRP_CONNTRACK_DESTROY GroupCTExpNew // NFNLGRP_CONNTRACK_EXP_NEW GroupCTExpUpdate // NFNLGRP_CONNTRACK_EXP_UPDATE GroupCTExpDestroy // NFNLGRP_CONNTRACK_EXP_DESTROY GroupNFTables // NFNLGRP_NFTABLES GroupAcctQuota // NFNLGRP_ACCT_QUOTA GroupNFTrace // NFNLGRP_NFTRACE )
enum nfnetlink_groups
type ProtoFamily ¶
type ProtoFamily uint8
ProtoFamily represents a protocol family in the Netfilter header (nfgenmsg).
const ( ProtoUnspec ProtoFamily = 0 // NFPROTO_UNSPEC ProtoInet ProtoFamily = 1 // NFPROTO_INET ProtoIPv4 ProtoFamily = 2 // NFPROTO_IPV4 ProtoARP ProtoFamily = 3 // NFPROTO_ARP ProtoNetDev ProtoFamily = 5 // NFPROTO_NETDEV ProtoBridge ProtoFamily = 7 // NFPROTO_BRIDGE ProtoIPv6 ProtoFamily = 10 // NFPROTO_IPV6 ProtoDECNet ProtoFamily = 12 // NFPROTO_DECNET )
anonymous enum in uapi/linux/netfilter.h
func (ProtoFamily) String ¶
func (i ProtoFamily) String() string
type SubsystemID ¶
type SubsystemID uint8
SubsystemID denotes the Netfilter Subsystem ID the message is for. It is a const that is defined in the kernel at uapi/linux/netfilter/nfnetlink.h.
const ( NFSubsysNone SubsystemID = iota // NFNL_SUBSYS_NONE NFSubsysCTNetlink // NFNL_SUBSYS_CTNETLINK NFSubsysCTNetlinkExp // NFNL_SUBSYS_CTNETLINK_EXP NFSubsysQueue // NFNL_SUBSYS_QUEUE NFSubsysULOG // NFNL_SUBSYS_ULOG NFSubsysOSF // NFNL_SUBSYS_OSF NFSubsysIPSet // NFNL_SUBSYS_IPSET NFSubsysAcct // NFNL_SUBSYS_ACCT NFSubsysCTNetlinkTimeout // NFNL_SUBSYS_CTNETLINK_TIMEOUT NFSubsysCTHelper // NFNL_SUBSYS_CTHELPER NFSubsysNFTables // NFNL_SUBSYS_NFTABLES NFSubsysNFTCompat // NFNL_SUBSYS_NFT_COMPAT NFSubsysCount // NFNL_SUBSYS_COUNT )
Subsystem specifiers for Netfilter Netlink messages
func (SubsystemID) String ¶
func (i SubsystemID) String() string