You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
252 lines
6.9 KiB
Go
252 lines
6.9 KiB
Go
//go:build linux
|
|
|
|
package networkobserve
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/mdlayher/netlink"
|
|
conntrack "github.com/ti-mo/conntrack"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
const conntrackAccountingPath = "/proc/sys/net/netfilter/nf_conntrack_acct"
|
|
const conntrackCountPath = "/proc/sys/net/netfilter/nf_conntrack_count"
|
|
const defaultConntrackDumpTimeout = 10 * time.Second
|
|
|
|
type conntrackDumper interface {
|
|
Dump(*conntrack.DumpOptions) ([]conntrack.Flow, error)
|
|
Close() error
|
|
}
|
|
|
|
type activeConntrackDump struct {
|
|
conn conntrackDumper
|
|
closeOnce sync.Once
|
|
closeErr error
|
|
}
|
|
|
|
func (dump *activeConntrackDump) Close() error {
|
|
dump.closeOnce.Do(func() { dump.closeErr = dump.conn.Close() })
|
|
return dump.closeErr
|
|
}
|
|
|
|
// NetlinkSource is intentionally read-only. The interface exposed here has no
|
|
// Flush, Delete, Update, Create or socket-option mutation method.
|
|
type NetlinkSource struct {
|
|
mu sync.Mutex
|
|
dial func() (conntrackDumper, error)
|
|
currentEntries func() (uint64, error)
|
|
dumpTimeout time.Duration
|
|
closed bool
|
|
nextDumpID uint64
|
|
active map[uint64]*activeConntrackDump
|
|
inflight sync.WaitGroup
|
|
}
|
|
|
|
func OpenNetlinkSource() (*NetlinkSource, error) {
|
|
return newNetlinkSource(func() (conntrackDumper, error) {
|
|
connection, err := conntrack.Dial(&netlink.Config{Strict: true})
|
|
if err != nil {
|
|
return nil, classifyNetlinkError(err)
|
|
}
|
|
return connection, nil
|
|
}, readConntrackTableCount, defaultConntrackDumpTimeout), nil
|
|
}
|
|
|
|
func newNetlinkSource(dial func() (conntrackDumper, error), currentEntries func() (uint64, error),
|
|
dumpTimeout time.Duration) *NetlinkSource {
|
|
return &NetlinkSource{
|
|
dial: dial, currentEntries: currentEntries, dumpTimeout: dumpTimeout,
|
|
active: make(map[uint64]*activeConntrackDump),
|
|
}
|
|
}
|
|
|
|
func (source *NetlinkSource) Probe(ctx context.Context) (ProbeEvidence, error) {
|
|
snapshot, err := source.Snapshot(ctx)
|
|
if err != nil {
|
|
return ProbeEvidence{}, err
|
|
}
|
|
return ProbeEvidence{
|
|
Conntrack: true, IPv4: true, IPv6: true,
|
|
PacketAccounting: snapshot.PacketAccounting,
|
|
}, nil
|
|
}
|
|
|
|
func (source *NetlinkSource) Snapshot(ctx context.Context) (KernelSnapshot, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return KernelSnapshot{}, err
|
|
}
|
|
if source.currentEntries == nil {
|
|
return KernelSnapshot{}, ErrConntrackUnavailable
|
|
}
|
|
currentEntries, err := source.currentEntries()
|
|
if err != nil || currentEntries > HardMaxKernelFlows {
|
|
return KernelSnapshot{}, ErrConntrackUnavailable
|
|
}
|
|
if source.dial == nil {
|
|
return KernelSnapshot{}, ErrConntrackUnavailable
|
|
}
|
|
connection, err := source.dial()
|
|
if err != nil {
|
|
return KernelSnapshot{}, classifyNetlinkError(err)
|
|
}
|
|
active, dumpID, err := source.register(connection)
|
|
if err != nil {
|
|
_ = connection.Close()
|
|
return KernelSnapshot{}, err
|
|
}
|
|
defer source.release(dumpID, active)
|
|
|
|
timeout := source.dumpTimeout
|
|
if timeout <= 0 {
|
|
timeout = defaultConntrackDumpTimeout
|
|
}
|
|
dumpContext, cancel := context.WithTimeout(ctx, timeout)
|
|
defer cancel()
|
|
dumpFinished := make(chan struct{})
|
|
go func() {
|
|
select {
|
|
case <-dumpContext.Done():
|
|
// mdlayher/netlink supports a concurrent Close specifically to
|
|
// interrupt a blocked receive. Each Snapshot owns its socket, so
|
|
// cancellation cannot poison a later collection.
|
|
_ = active.Close()
|
|
case <-dumpFinished:
|
|
}
|
|
}()
|
|
flows, err := connection.Dump(nil)
|
|
close(dumpFinished)
|
|
if contextErr := dumpContext.Err(); contextErr != nil {
|
|
return KernelSnapshot{}, contextErr
|
|
}
|
|
if err != nil {
|
|
return KernelSnapshot{}, classifyNetlinkError(err)
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return KernelSnapshot{}, err
|
|
}
|
|
result := KernelSnapshot{
|
|
Flows: make([]KernelFlow, 0, min(len(flows), HardMaxKernelFlows)),
|
|
PacketAccounting: packetAccountingEnabled(flows),
|
|
Overflow: len(flows) > HardMaxKernelFlows,
|
|
}
|
|
for index := range flows {
|
|
if len(result.Flows) >= HardMaxKernelFlows {
|
|
break
|
|
}
|
|
flow := &flows[index]
|
|
if flow.Status.Template() || flow.Status.Dying() {
|
|
continue
|
|
}
|
|
observed := KernelFlow{
|
|
SourceAddress: flow.TupleOrig.IP.SourceAddress,
|
|
DestinationAddress: flow.TupleOrig.IP.DestinationAddress,
|
|
SourcePort: flow.TupleOrig.Proto.SourcePort,
|
|
DestinationPort: flow.TupleOrig.Proto.DestinationPort,
|
|
Protocol: flow.TupleOrig.Proto.Protocol,
|
|
Zone: flow.Zone,
|
|
SeenReply: flow.Status.SeenReply(),
|
|
}
|
|
if flow.ProtoInfo.TCP != nil {
|
|
observed.TCPState = flow.ProtoInfo.TCP.State
|
|
}
|
|
result.Flows = append(result.Flows, observed)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (source *NetlinkSource) register(connection conntrackDumper) (*activeConntrackDump, uint64, error) {
|
|
source.mu.Lock()
|
|
defer source.mu.Unlock()
|
|
if source.closed {
|
|
return nil, 0, ErrConntrackUnavailable
|
|
}
|
|
source.nextDumpID++
|
|
if source.nextDumpID == 0 {
|
|
return nil, 0, ErrConntrackUnavailable
|
|
}
|
|
active := &activeConntrackDump{conn: connection}
|
|
source.active[source.nextDumpID] = active
|
|
source.inflight.Add(1)
|
|
return active, source.nextDumpID, nil
|
|
}
|
|
|
|
func (source *NetlinkSource) release(dumpID uint64, active *activeConntrackDump) {
|
|
_ = active.Close()
|
|
source.mu.Lock()
|
|
delete(source.active, dumpID)
|
|
source.mu.Unlock()
|
|
source.inflight.Done()
|
|
}
|
|
|
|
func readConntrackTableCount() (uint64, error) {
|
|
value, err := os.ReadFile(conntrackCountPath)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return strconv.ParseUint(strings.TrimSpace(string(value)), 10, 64)
|
|
}
|
|
|
|
func (source *NetlinkSource) Close() error {
|
|
source.mu.Lock()
|
|
if source.closed {
|
|
source.mu.Unlock()
|
|
return nil
|
|
}
|
|
source.closed = true
|
|
active := make([]*activeConntrackDump, 0, len(source.active))
|
|
for _, dump := range source.active {
|
|
active = append(active, dump)
|
|
}
|
|
source.mu.Unlock()
|
|
|
|
var result error
|
|
for _, dump := range active {
|
|
result = errors.Join(result, dump.Close())
|
|
}
|
|
source.inflight.Wait()
|
|
return result
|
|
}
|
|
|
|
func packetAccountingEnabled(flows []conntrack.Flow) bool {
|
|
if value, err := os.ReadFile(conntrackAccountingPath); err == nil {
|
|
switch strings.TrimSpace(string(value)) {
|
|
case "1":
|
|
return true
|
|
case "0":
|
|
return false
|
|
}
|
|
}
|
|
for index := range flows {
|
|
flow := &flows[index]
|
|
if flow.CountersOrig.Packets != 0 || flow.CountersOrig.Bytes != 0 ||
|
|
flow.CountersReply.Packets != 0 || flow.CountersReply.Bytes != 0 {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func classifyNetlinkError(err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
switch {
|
|
case errors.Is(err, os.ErrPermission), errors.Is(err, unix.EPERM), errors.Is(err, unix.EACCES):
|
|
return errors.Join(ErrPermissionDenied, err)
|
|
case errors.Is(err, unix.EAFNOSUPPORT), errors.Is(err, unix.EPROTONOSUPPORT),
|
|
errors.Is(err, unix.ENOPROTOOPT):
|
|
return errors.Join(ErrNetlinkUnavailable, err)
|
|
case errors.Is(err, unix.ENOENT), errors.Is(err, unix.EOPNOTSUPP), errors.Is(err, unix.ENODEV):
|
|
return errors.Join(ErrConntrackUnavailable, err)
|
|
default:
|
|
return errors.Join(ErrConntrackUnavailable, err)
|
|
}
|
|
}
|