-
Notifications
You must be signed in to change notification settings - Fork 51
/
autoport.go
149 lines (123 loc) · 3.85 KB
/
autoport.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package nfqdatapath
import (
"path/filepath"
"sync"
"time"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/ipsetmanager"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/pucontext"
"go.aporeto.io/enforcerd/trireme-lib/utils/portspec"
"go.uber.org/zap"
)
type readSystemFiles interface {
readOpenSockFD(pid string) []string
readProcNetTCP() (inodeMap map[string]string, userMap map[string]map[string]bool, err error)
getCgroupList() []string
listCgroupProcesses(cgroupname string) ([]string, error)
}
type defaultRead struct{}
var readFiles readSystemFiles
var d *defaultRead
var lock sync.RWMutex
func init() {
lock.Lock()
readFiles = d
lock.Unlock()
}
func (d *Datapath) autoPortDiscovery() {
for {
d.findPorts()
time.Sleep(2 * time.Second)
}
}
// resync adds new port for the PU and removes the stale ports
func (d *Datapath) resync(newPortMap map[string]map[string]bool) {
for k, vs := range d.puToPortsMap {
m := newPortMap[k]
for v := range vs {
if m == nil || !m[v] {
err := ipsetmanager.V4().DeletePortFromServerPortSet(k, v)
if err != nil {
zap.L().Debug("autoPortDiscovery: Delete port set returned error", zap.Error(err))
}
err = ipsetmanager.V6().DeletePortFromServerPortSet(k, v)
if err != nil {
zap.L().Debug("autoPortDiscovery: Delete port set returned error", zap.Error(err))
}
// delete the port from contextIDFromTCPPort cache
err = d.contextIDFromTCPPort.RemoveStringPorts(v)
if err != nil {
zap.L().Debug("autoPortDiscovery: can not remove port from cache", zap.Error(err))
}
}
}
}
for k, vs := range newPortMap {
m := d.puToPortsMap[k]
for v := range vs {
if m == nil || !m[v] {
portSpec, err := portspec.NewPortSpecFromString(v, k)
if err != nil {
continue
}
d.contextIDFromTCPPort.AddPortSpec(portSpec)
err = ipsetmanager.V4().AddPortToServerPortSet(k, v)
if err != nil {
zap.L().Error("autoPortDiscovery: Failed to add port to portset", zap.String("context", k), zap.String("port", v))
}
err = ipsetmanager.V6().AddPortToServerPortSet(k, v)
if err != nil {
zap.L().Error("autoPortDiscovery: Failed to add port to portset", zap.String("context", k), zap.String("port", v))
}
}
}
}
d.puToPortsMap = newPortMap
}
var lastRun time.Time
func (d *Datapath) findPorts() {
lock.Lock()
defer lock.Unlock()
// Rate limit this function to run every 5 milliseconds
if time.Since(lastRun) <= 5*time.Millisecond {
return
}
lastRun = time.Now()
cgroupList := readFiles.getCgroupList()
newPUToPortsMap := map[string]map[string]bool{}
inodeMap, _, err := readFiles.readProcNetTCP()
if err != nil {
zap.L().Error("autoPortDiscovery: /proc/net/tcp read failed with error", zap.Error(err))
return
}
for _, cgroupPath := range cgroupList {
/* cgroup is also the contextID */
newMap := map[string]bool{}
cgroup := filepath.Base(cgroupPath)
// check if a PU exists with that contextID and is marked with auto port
pu, err := d.puFromContextID.Get(cgroup)
if err != nil {
continue
}
p := pu.(*pucontext.PUContext)
// we skip AutoPort discovery if it is not enabled
if !p.Autoport() {
continue
}
procs, err := readFiles.listCgroupProcesses(cgroupPath)
if err != nil {
zap.L().Warn("autoPortDiscovery: Cgroup processes could not be retrieved", zap.String("cgroupPath", cgroupPath), zap.String("cgroup", cgroup), zap.Error(err))
continue
}
zap.L().Debug("autoPortDiscovery: processes for cgroup detected", zap.String("cgroupPath", cgroupPath), zap.String("cgroup", cgroup), zap.String("id", p.ID()), zap.Strings("procs", procs))
for _, proc := range procs {
openSockFDs := readFiles.readOpenSockFD(proc)
for _, sock := range openSockFDs {
if inodeMap[sock] != "" {
newMap[inodeMap[sock]] = true
}
}
}
newPUToPortsMap[cgroup] = newMap
}
d.resync(newPUToPortsMap)
}