Documentation
¶
Overview ¶
Package conntrack provides an API to interact with the conntrack subsystem of the netfilter family from the linux kernel.
Example:
package main
import (
"fmt"
ct "github.com/florianl/go-conntrack"
)
func main() {
// Opens the socket for the communication with the subsystem
nfct, err := ct.Open()
if err != nil {
fmt.Println("Could not open socket:", err)
return
}
defer nfct.Close()
// Get all IPv4 sessions
sessions, err := nfct.Dump(ct.Ct, ct.CtIPv4)
if err != nil {
fmt.Println("Could not dump sessions:", err)
return
}
for _, x := range sessions {
oSrcIP, _ := x.OrigSrcIP()
oDstIP, _ := x.OrigDstIP()
// Print source and destination for each IPv4 session
fmt.Printf("src: %s\tdst: %s \n", oSrcIP, oDstIP)
}
}
Index ¶
- Constants
- Variables
- type Conn
- func (c Conn) OrigDstIP() (net.IP, error)
- func (c Conn) OrigSrcIP() (net.IP, error)
- func (c Conn) Uint8(attr ConnAttrType) (uint8, error)
- func (c Conn) Uint16(attr ConnAttrType) (uint16, error)
- func (c Conn) Uint32(attr ConnAttrType) (uint32, error)
- func (c Conn) Uint64(attr ConnAttrType) (uint64, error)
- type ConnAttr
- type ConnAttrType
- type CtFamily
- type CtTable
- type ErrMsg
- type FilterAttr
- type HookFunc
- type NetlinkGroup
- type Nfct
- func (nfct *Nfct) Close() error
- func (nfct *Nfct) Counters(t CtTable) ([]Conn, error)
- func (nfct *Nfct) Create(t CtTable, f CtFamily, attributes []ConnAttr) error
- func (nfct *Nfct) Delete(t CtTable, f CtFamily, filters []ConnAttr) error
- func (nfct *Nfct) Dump(t CtTable, f CtFamily) ([]Conn, error)
- func (nfct *Nfct) DumpCPUStats(t CtTable) ([]Conn, error)
- func (nfct *Nfct) DumpDying(f CtFamily) ([]Conn, error)
- func (nfct *Nfct) DumpUnconfirmed(f CtFamily) ([]Conn, error)
- func (nfct *Nfct) Flush(t CtTable, f CtFamily) error
- func (nfct *Nfct) Get(t CtTable, f CtFamily, attributes []ConnAttr) ([]Conn, error)
- func (nfct *Nfct) Query(t CtTable, f CtFamily, filter FilterAttr) ([]Conn, error)
- func (nfct *Nfct) Register(ctx context.Context, t CtTable, group NetlinkGroup, fn HookFunc) (<-chan error, error)
- func (nfct *Nfct) RegisterFiltered(ctx context.Context, t CtTable, group NetlinkGroup, filter []ConnAttr, ...) (<-chan error, error)
- func (nfct *Nfct) Update(t CtTable, f CtFamily, attributes []ConnAttr) error
Examples ¶
Constants ¶
const ( CPUID = ConnAttrType(0) CPUStatsSearched = ConnAttrType(1) /* u64 bits */ CPUStatsFound = ConnAttrType(2) /* u64 bits */ CPUStatsNew = ConnAttrType(3) /* u64 bits */ CPUStatsInvalid = ConnAttrType(4) /* u64 bits */ CPUStatsIgnore = ConnAttrType(5) /* u64 bits */ CPUStatsDelete = ConnAttrType(6) /* u64 bits */ CPUStatsDeleteList = ConnAttrType(7) /* u64 bits */ CPUStatsInsert = ConnAttrType(8) /* u64 bits */ CPUStatsInsertFailed = ConnAttrType(9) /* u64 bits */ CPUStatsDrop = ConnAttrType(10) /* u64 bits */ CPUStatsEarlyDrop = ConnAttrType(11) /* u64 bits */ CPUStatsError = ConnAttrType(12) /* u64 bits */ CPUStatsSearchRestart = ConnAttrType(13) /* u64 bits */ )
Various CPU related statistics based on libnetfilter_conntrack.h
const ( StatsGlobalEntries = 1 StatsGlobalMaxEntries = 2 )
Various types of global statistics
Variables ¶
var ( ErrFilterLength = errors.New("Number of filtering instructions are too high") ErrFilterAttributeLength = errors.New("Incorrect length of filter attribute") ErrFilterAttributeMaskLength = errors.New("Incorrect length of filter mask") ErrFilterAttributeNotImplemented = errors.New("Filter attribute not implemented") ErrFilterAttributeNegateMix = errors.New("Can not mix negation for attribute of the same type") )
Various errors which may occur when processing filters
var ( ErrConnNoSrcIP = errors.New("Conn has no source IP") ErrConnNoDstIP = errors.New("Conn has no destination IP") ErrConnNoAttr = errors.New("Conn has not this attribute") )
Various errors which may occur when procressing a connection
var ( ErrAttrLength = errors.New("Incorrect length of attribute") ErrAttrNotImplemented = errors.New("Attribute not implemented") ErrAttrNotExist = errors.New("Type of attribute does not exist") ErrDataLength = errors.New("Incorrect length of provided data") )
Various errors which may occur when processing attributes
var (
ErrFilterAttrLength = errors.New("Incorrect length of filter attribute")
)
Error which may occur when processing the filter attribute
var ErrUnknownCtTable = errors.New("Not supported for this subsystem")
ErrUnknownCtTable will be return, if the function can not be performed on this subsystem
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn map[ConnAttrType][]byte
Conn contains all the information of a connection
func ParseAttributes ¶
ParseAttributes extracts all the attributes from the given data
func (Conn) Uint8 ¶
func (c Conn) Uint8(attr ConnAttrType) (uint8, error)
Uint8 returns the uint8 representation of the given attribute's data.
func (Conn) Uint16 ¶
func (c Conn) Uint16(attr ConnAttrType) (uint16, error)
Uint16 returns the uint16 representation of the given attribute's data.
type ConnAttr ¶
type ConnAttr struct {
Type ConnAttrType
Data []byte
// Mask is required for specific attributes, if you want to filter for them
Mask []byte
// Negates this attribute for filtering
Negate bool
}
ConnAttr represents the type and value of a attribute of a connection
type ConnAttrType ¶
type ConnAttrType uint16
ConnAttrType specifies the attribute of a connection
const ( AttrOrigIPv4Src ConnAttrType = iota /* u32 bits, requires a mask if applied as filter */ AttrOrigIPv4Dst ConnAttrType = iota /* u32 bits, requires a mask if applied as filter */ AttrReplIPv4Src ConnAttrType = iota /* u32 bits, requires a mask if applied as filter */ AttrReplIPv4Dst ConnAttrType = iota /* u32 bits, requires a mask if applied as filter */ AttrOrigIPv6Src ConnAttrType = iota /* u128 bits */ AttrOrigIPv6Dst ConnAttrType = iota /* u128 bits */ AttrReplIPv6Src ConnAttrType = iota /* u128 bits */ AttrReplIPv6Dst ConnAttrType = iota /* u128 bits */ AttrOrigPortSrc ConnAttrType = iota /* u16 bits */ AttrOrigPortDst ConnAttrType = iota /* u16 bits */ AttrReplPortSrc ConnAttrType = iota /* u16 bits */ AttrReplPortDst ConnAttrType = iota /* u16 bits */ AttrIcmpType ConnAttrType = iota /* u8 bits */ AttrIcmpCode ConnAttrType = iota /* u8 bits */ AttrIcmpID ConnAttrType = iota /* u16 bits */ AttrOrigL3Proto ConnAttrType = iota /* u8 bits */ AttrReplL3Proto ConnAttrType = iota /* u8 bits */ AttrOrigL4Proto ConnAttrType = iota /* u8 bits */ AttrReplL4Proto ConnAttrType = iota /* u8 bits */ AttrTCPState ConnAttrType = iota /* u8 bits */ AttrSNatIPv4 ConnAttrType = iota /* u32 bits */ AttrDNatIPv4 ConnAttrType = iota /* u32 bits */ AttrSNatPort ConnAttrType = iota /* u16 bits */ AttrDNatPort ConnAttrType = iota /* u16 bits */ AttrTimeout ConnAttrType = iota /* u32 bits */ AttrMark ConnAttrType = iota /* u32 bits, requires a mask if applied as filter */ AttrOrigCounterPackets ConnAttrType = iota /* u64 bits */ AttrReplCounterPackets ConnAttrType = iota /* u64 bits */ AttrOrigCounterBytes ConnAttrType = iota /* u64 bits */ AttrReplCounterBytes ConnAttrType = iota /* u64 bits */ AttrUse ConnAttrType = iota /* u32 bits */ AttrID ConnAttrType = iota /* u32 bits */ AttrStatus ConnAttrType = iota /* u32 bits */ AttrTCPFlagsOrig ConnAttrType = iota /* u8 bits */ AttrTCPFlagsRepl ConnAttrType = iota /* u8 bits */ AttrTCPMaskOrig ConnAttrType = iota /* u8 bits */ AttrTCPMaskRepl ConnAttrType = iota /* u8 bits */ AttrMasterIPv4Src ConnAttrType = iota /* u32 bits */ AttrMasterIPv4Dst ConnAttrType = iota /* u32 bits */ AttrMasterIPv6Src ConnAttrType = iota /* u128 bits */ AttrMasterIPv6Dst ConnAttrType = iota /* u128 bits */ AttrMasterPortSrc ConnAttrType = iota /* u16 bits */ AttrMasterPortDst ConnAttrType = iota /* u16 bits */ AttrMasterL3Proto ConnAttrType = iota /* u8 bits */ AttrMasterL4Proto ConnAttrType = iota /* u8 bits */ AttrSecmark ConnAttrType = iota /* u32 bits */ AttrOrigNatSeqCorrectionPos ConnAttrType = iota /* u32 bits */ AttrOrigNatSeqOffsetBefore ConnAttrType = iota /* u32 bits */ AttrOrigNatSeqOffsetAfter ConnAttrType = iota /* u32 bits */ AttrReplNatSeqCorrectionPos ConnAttrType = iota /* u32 bits */ AttrReplNatSeqOffsetBefore ConnAttrType = iota /* u32 bits */ AttrReplNatSeqOffsetAfter ConnAttrType = iota /* u32 bits */ AttrSctpState ConnAttrType = iota /* u8 bits */ AttrSctpVtagOrig ConnAttrType = iota /* u32 bits */ AttrSctpVtagRepl ConnAttrType = iota /* u32 bits */ AttrHelperName ConnAttrType = iota /* string (30 bytes max) */ AttrDccpState ConnAttrType = iota /* u8 bits */ AttrDccpRole ConnAttrType = iota /* u8 bits */ AttrDccpHandshakeSeq ConnAttrType = iota /* u64 bits */ AttrTCPWScaleOrig ConnAttrType = iota /* u8 bits */ AttrTCPWScaleRepl ConnAttrType = iota /* u8 bits */ AttrZone ConnAttrType = iota /* u16 bits */ AttrSecCtx ConnAttrType = iota /* string */ AttrTimestampStart ConnAttrType = iota /* u64 bits linux >= 2.6.38 */ AttrTimestampStop ConnAttrType = iota /* u64 bits linux >= 2.6.38 */ AttrHelperInfo ConnAttrType = iota /* variable length */ AttrConnlabels ConnAttrType = iota /* variable length */ AttrConnlabelsMask ConnAttrType = iota /* variable length */ AttrOrigzone ConnAttrType = iota /* u16 bits */ AttrReplzone ConnAttrType = iota /* u16 bits */ AttrSNatIPv6 ConnAttrType = iota /* u128 bits */ AttrDNatIPv6 ConnAttrType = iota /* u128 bits */ AttrExpID ConnAttrType = iota /* u32 bits */ AttrExpFlags ConnAttrType = iota /* u32 bits */ AttrExpClass ConnAttrType = iota /* u32 bits */ AttrExpNATDir ConnAttrType = iota /* u32 bits */ )
Attributes of a connection based on libnetfilter_conntrack.h
type CtTable ¶
type CtTable int
CtTable specifies the subsystem of conntrack
const ( // Conntrack table Ct CtTable = unix.NFNL_SUBSYS_CTNETLINK // Conntrack expect table CtExpected CtTable = unix.NFNL_SUBSYS_CTNETLINK_EXP )
Supported conntrack subsystems
type FilterAttr ¶
type FilterAttr struct {
Mark, MarkMask []byte
}
FilterAttr represents a very basic filter
type HookFunc ¶
HookFunc is a function, that receives events from a Netlinkgroup. Return something different than 0, to stop receiving messages.
type NetlinkGroup ¶
type NetlinkGroup uint32
NetlinkGroup represents a Netlink multicast group
const ( NetlinkCtNew NetlinkGroup = 1 << iota NetlinkCtUpdate NetlinkGroup = 1 << iota NetlinkCtDestroy NetlinkGroup = 1 << iota NetlinkCtExpectedNew NetlinkGroup = 1 << iota NetlinkCtExpectedUpdate NetlinkGroup = 1 << iota NetlinkCtExpectedDestroy NetlinkGroup = 1 << iota )
Supported Netlink groups
type Nfct ¶
Nfct represents a conntrack handler
func (*Nfct) Create ¶
Create a new entry in the conntrack subsystem with certain attributes
Example ¶
package main
import (
"fmt"
"net"
ct "github.com/florianl/go-conntrack"
)
func main() {
nfct, err := ct.Open()
if err != nil {
fmt.Println("Could not create nfct:", err)
return
}
defer nfct.Close()
ipA := net.ParseIP("1.1.1.1")
ipB := net.ParseIP("8.8.8.8")
err = nfct.Create(ct.Ct, ct.CtIPv4, []ct.ConnAttr{
{Type: ct.AttrOrigIPv4Src, Data: []byte{ipA[12], ipA[13], ipA[14], ipA[15]}},
{Type: ct.AttrOrigIPv4Dst, Data: []byte{ipB[12], ipB[13], ipB[14], ipB[15]}},
{Type: ct.AttrReplIPv4Src, Data: []byte{ipB[12], ipB[13], ipB[14], ipB[15]}},
{Type: ct.AttrReplIPv4Dst, Data: []byte{ipA[12], ipA[13], ipA[14], ipA[15]}},
{Type: ct.AttrOrigL4Proto, Data: []byte{0x02}},
{Type: ct.AttrReplL4Proto, Data: []byte{0x02}},
{Type: ct.AttrStatus, Data: []byte{0x0, 0x0, 0x0, 0x08}},
{Type: ct.AttrTimeout, Data: []byte{0x0, 0x0, 0x0, 0x42}}})
if err != nil {
fmt.Println("Could not create new session:", err)
return
}
}
Output:
func (*Nfct) Dump ¶
Dump a conntrack subsystem
Example ¶
package main
import (
"fmt"
ct "github.com/florianl/go-conntrack"
)
func main() {
nfct, err := ct.Open()
if err != nil {
fmt.Println("Could not create nfct:", err)
return
}
defer nfct.Close()
sessions, err := nfct.Dump(ct.Ct, ct.CtIPv4)
if err != nil {
fmt.Println("Could not dump sessions:", err)
return
}
for _, x := range sessions {
oSrcIP, _ := x.OrigSrcIP()
oDstIP, _ := x.OrigDstIP()
fmt.Printf("src: %s\tdst: %s \n", oSrcIP, oDstIP)
}
}
Output:
func (*Nfct) DumpCPUStats ¶
DumpCPUStats gets statistics about the conntrack subsystem
func (*Nfct) DumpUnconfirmed ¶
DumpUnconfirmed returns connections marked as unconfirmed
func (*Nfct) Flush ¶
Flush a conntrack subsystem
Example ¶
package main
import (
"fmt"
ct "github.com/florianl/go-conntrack"
)
func main() {
nfct, err := ct.Open()
if err != nil {
fmt.Println("Could not create nfct:", err)
return
}
defer nfct.Close()
err = nfct.Flush(ct.Ct, ct.CtIPv4)
if err != nil {
fmt.Println("Could not flush conntrack subsystem:", err)
return
}
}
Output:
func (*Nfct) Query ¶
Query conntrack subsystem with certain attributes
Example ¶
package main
import (
"fmt"
ct "github.com/florianl/go-conntrack"
)
func main() {
nfct, err := ct.Open()
if err != nil {
fmt.Println("Could not create nfct:", err)
return
}
defer nfct.Close()
sessions, err := nfct.Query(ct.Ct, ct.CtIPv6,
ct.FilterAttr{Mark: []byte{0x00, 0x0, 0x00, 0x01},
MarkMask: []byte{0x00, 0x0, 0x0, 0xFF}})
if err != nil {
fmt.Println("Could not query sessions:", err)
return
}
for _, x := range sessions {
oSrcIP, _ := x.OrigSrcIP()
oDstIP, _ := x.OrigDstIP()
fmt.Printf("src: %s\tdst: %s \n", oSrcIP, oDstIP)
}
}
Output:
func (*Nfct) Register ¶
func (nfct *Nfct) Register(ctx context.Context, t CtTable, group NetlinkGroup, fn HookFunc) (<-chan error, error)
Register your function to receive events from a Netlinkgroup. If your function returns something different than 0, it will stop.
func (*Nfct) RegisterFiltered ¶
func (nfct *Nfct) RegisterFiltered(ctx context.Context, t CtTable, group NetlinkGroup, filter []ConnAttr, fn HookFunc) (<-chan error, error)
RegisterFiltered registers your function to receive events from a Netlinkgroup and applies a filter. If your function returns something different than 0, it will stop. ConnAttr of the same ConnAttrType will be linked by an OR operation. Otherwise, ConnAttr of different ConnAttrType will be connected by an AND operation for the filter.
Example ¶
package main
import (
"context"
"fmt"
"time"
ct "github.com/florianl/go-conntrack"
)
func main() {
nfct, err := ct.Open()
if err != nil {
fmt.Println("Could not create nfct:", err)
return
}
defer nfct.Close()
errChan, err := nfct.RegisterFiltered(
context.Background(), ct.Ct, ct.NetlinkCtUpdate,
[]ct.ConnAttr{
{Type: ct.AttrOrigL4Proto, Data: []byte{0x11}}, // TCP
{Type: ct.AttrOrigL4Proto, Data: []byte{0x06}}, // UDP
},
func(c ct.Conn) int { fmt.Println(c); return 0 },
)
if err != nil {
fmt.Println("Could not register with filter:", err)
return
}
for {
select {
case errMsg := <-errChan:
fmt.Println(errMsg)
case <-time.After(10 * time.Second):
break
}
}
}
Output: