Handle nat status from conntrack via netlink

Replacement for the --any-nat command-line parameter
This commit is contained in:
Bryan Boreham
2018-08-05 07:12:24 +00:00
parent ed6a010330
commit 73f35fd6d9
3 changed files with 13 additions and 6 deletions

View File

@@ -79,7 +79,7 @@ func (t *connectionTracker) useProcfs() {
t.conf.Scanner = procspy.NewConnectionScanner(t.conf.ProcessCache, t.conf.SpyProcs)
}
if t.flowWalker == nil {
t.flowWalker = newConntrackFlowWalker(t.conf.UseConntrack, t.conf.ProcRoot, t.conf.BufferSize)
t.flowWalker = newConntrackFlowWalker(t.conf.UseConntrack, t.conf.ProcRoot, t.conf.BufferSize, false /* natOnly */)
}
}
@@ -136,10 +136,13 @@ func (t *connectionTracker) existingFlows() map[string]fourTuple {
// log.Warnf("Not using conntrack: disabled")
} else if err := IsConntrackSupported(t.conf.ProcRoot); err != nil {
log.Warnf("Not using conntrack: not supported by the kernel: %s", err)
} else if existingFlows, err := conntrack.Established(t.conf.BufferSize); err != nil { // TODO: worry about --any-nat
} else if existingFlows, err := conntrack.Established(t.conf.BufferSize); err != nil {
log.Errorf("conntrack existingConnections error: %v", err)
} else {
for _, f := range existingFlows {
if (f.Status & conntrack.IPS_NAT_MASK) == 0 {
continue
}
tuple := flowToTuple(f)
seenTuples[tuple.key()] = tuple
}

View File

@@ -38,12 +38,12 @@ type conntrackWalker struct {
activeFlows map[uint32]conntrack.Flow // active flows in state != TIME_WAIT
bufferedFlows []conntrack.Flow // flows coming out of activeFlows spend 1 walk cycle here
bufferSize int
args []string
natOnly bool
quit chan struct{}
}
// newConntracker creates and starts a new conntracker.
func newConntrackFlowWalker(useConntrack bool, procRoot string, bufferSize int, args ...string) flowWalker {
func newConntrackFlowWalker(useConntrack bool, procRoot string, bufferSize int, natOnly bool) flowWalker {
if !useConntrack {
return nilFlowWalker{}
} else if err := IsConntrackSupported(procRoot); err != nil {
@@ -53,7 +53,7 @@ func newConntrackFlowWalker(useConntrack bool, procRoot string, bufferSize int,
result := &conntrackWalker{
activeFlows: map[uint32]conntrack.Flow{},
bufferSize: bufferSize,
args: args,
natOnly: natOnly,
quit: make(chan struct{}),
}
go result.loop()
@@ -175,6 +175,10 @@ func (c *conntrackWalker) handleFlow(f conntrack.Flow, forceAdd bool) {
c.Lock()
defer c.Unlock()
if c.natOnly && (f.Status&conntrack.IPS_NAT_MASK) == 0 {
return
}
// Ignore flows for which we never saw an update; they are likely
// incomplete or wrong. See #1462.
switch {

View File

@@ -71,7 +71,7 @@ func NewReporter(conf ReporterConfig) *Reporter {
Scanner: conf.Scanner,
DNSSnooper: conf.DNSSnooper,
}),
natMapper: makeNATMapper(newConntrackFlowWalker(conf.UseConntrack, conf.ProcRoot, conf.BufferSize, "--any-nat")),
natMapper: makeNATMapper(newConntrackFlowWalker(conf.UseConntrack, conf.ProcRoot, conf.BufferSize, true /* natOnly */)),
}
}