-
Notifications
You must be signed in to change notification settings - Fork 7
/
nf_conntrack.go
135 lines (117 loc) · 3.4 KB
/
nf_conntrack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package conntrack
import (
"fmt"
"net"
"os"
"time"
ct "github.com/florianl/go-conntrack"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netns"
"inet.af/netaddr"
"github.com/castai/egressd/metrics"
)
type nfctDumper interface {
Dump(t ct.Table, f ct.Family) ([]ct.Con, error)
Close() error
}
var (
accounting = "/hostproc/sys/net/netfilter/nf_conntrack_acct"
)
func InitNetfilterAccounting() error {
return os.WriteFile(accounting, []byte{'1'}, 0600)
}
func NewNetfilterClient(log logrus.FieldLogger, hostPid bool) (Client, error) {
var hostNs netns.NsHandle
// If hostPid is used we can access host network namespace without the need to expose container to host network.
if hostPid {
var err error
hostNs, err = netns.GetFromPid(1)
if err != nil {
return nil, fmt.Errorf("getting host network namespace: %w", err)
}
}
nfct, err := ct.Open(&ct.Config{
NetNS: int(hostNs),
})
if err != nil {
return nil, fmt.Errorf("opening nfct: %w", err)
}
return &netfilterClient{
log: log,
nfctDumper: nfct,
}, nil
}
type netfilterClient struct {
log logrus.FieldLogger
nfctDumper nfctDumper
}
func (n *netfilterClient) ListEntries(filter EntriesFilter) ([]*Entry, error) {
sessions, err := n.nfctDumper.Dump(ct.Conntrack, ct.IPv4)
if err != nil {
return nil, fmt.Errorf("dumping nfct sessions: %w", err)
}
metrics.SetConntrackEntriesCount(float64(len(sessions)))
now := time.Now().UTC()
res := make([]*Entry, 0)
var skippedEntriesCount int
for _, sess := range sessions {
if sess.Origin == nil || sess.Origin.Src == nil || sess.Origin.Proto == nil || sess.Origin.Proto.SrcPort == nil || sess.Origin.Proto.DstPort == nil ||
sess.Reply == nil || sess.Reply.Dst == nil || sess.Reply.Proto == nil || sess.Reply.Proto.SrcPort == nil || sess.Reply.Proto.DstPort == nil ||
sess.CounterOrigin == nil || sess.CounterReply == nil {
skippedEntriesCount++
continue
}
origin := sess.Origin
originCounter := sess.CounterOrigin
reply := sess.Reply
replyCounter := sess.CounterReply
entry := &Entry{
Src: netaddr.IPPortFrom(ipFromStdIP(*origin.Src), *origin.Proto.SrcPort),
Dst: netaddr.IPPortFrom(ipFromStdIP(*origin.Dst), *origin.Proto.DstPort),
TxBytes: *originCounter.Bytes,
TxPackets: *originCounter.Packets,
RxBytes: *replyCounter.Bytes,
RxPackets: *replyCounter.Packets,
Proto: *origin.Proto.Number,
}
if sess.Timeout != nil {
entry.Lifetime = now.Add(time.Second * time.Duration(*sess.Timeout))
}
replySrc := netaddr.IPPortFrom(ipFromStdIP(*reply.Src), *reply.Proto.SrcPort)
// Probably ClusterIP service, remap destination to actual pod IP.
if entry.Dst != replySrc {
entry.Dst = replySrc
}
if filter(entry) {
res = append(res, entry)
}
// Cilium stores two records for single flow.
entry2 := &Entry{
Src: entry.Dst,
Dst: entry.Src,
TxBytes: entry.RxBytes,
TxPackets: entry.RxPackets,
RxBytes: entry.TxBytes,
RxPackets: entry.TxPackets,
Proto: entry.Proto,
Lifetime: entry.Lifetime,
}
if filter(entry2) {
res = append(res, entry2)
}
}
if skippedEntriesCount > 0 {
n.log.Debugf("skipped %d conntrack entries", skippedEntriesCount)
}
return res, nil
}
func (n *netfilterClient) Close() error {
if n.nfctDumper != nil {
return n.nfctDumper.Close()
}
return nil
}
func ipFromStdIP(ip net.IP) netaddr.IP {
res, _ := netaddr.FromStdIP(ip)
return res
}