Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NETOBSERV-1186: extract DNS flags rcode field and stringify it #451

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 96 additions & 30 deletions pkg/pipeline/decode/decode_protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package decode
import (
"fmt"
"net"
"syscall"
"time"

"github.com/netobserv/flowlogs-pipeline/pkg/config"
"github.com/netobserv/netobserv-ebpf-agent/pkg/pbflow"

log "github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -37,36 +39,58 @@ func PBFlowToMap(flow *pbflow.Record) config.GenericMap {
return config.GenericMap{}
}
out := config.GenericMap{
"FlowDirection": int(flow.Direction.Number()),
"Bytes": flow.Bytes,
"SrcAddr": ipToStr(flow.Network.GetSrcAddr()),
"DstAddr": ipToStr(flow.Network.GetDstAddr()),
"SrcMac": macToStr(flow.DataLink.GetSrcMac()),
"DstMac": macToStr(flow.DataLink.GetDstMac()),
"SrcPort": flow.Transport.GetSrcPort(),
"DstPort": flow.Transport.GetDstPort(),
"Etype": flow.EthProtocol,
"Packets": flow.Packets,
"Duplicate": flow.Duplicate,
"Proto": flow.Transport.GetProtocol(),
"TimeFlowStartMs": flow.TimeFlowStart.AsTime().UnixMilli(),
"TimeFlowEndMs": flow.TimeFlowEnd.AsTime().UnixMilli(),
"TimeReceived": time.Now().Unix(),
"Interface": flow.Interface,
"AgentIP": ipToStr(flow.AgentIp),
"Flags": flow.Flags,
"IcmpType": flow.GetIcmpType(),
"IcmpCode": flow.GetIcmpCode(),
"TcpDropBytes": flow.TcpDropBytes,
"TcpDropPackets": flow.TcpDropPackets,
"TcpDropLatestFlags": flow.GetTcpDropLatestFlags(),
"TcpDropLatestState": tcpStateToStr(flow.GetTcpDropLatestState()),
"TcpDropLatestDropCause": tcpDropCauseToStr(flow.GetTcpDropLatestDropCause()),
"DnsRequestTimeMs": flow.TimeDnsReq.AsTime().UnixMilli(),
"DnsResponseTimeMs": flow.TimeDnsRsp.AsTime().UnixMilli(),
"DnsId": flow.GetDnsId(),
"DnsFlags": flow.GetDnsFlags(),
"TimeFlowRttMs": flow.TimeFlowRtt.AsDuration().Milliseconds(),
"FlowDirection": int(flow.Direction.Number()),
"Bytes": flow.Bytes,
"SrcAddr": ipToStr(flow.Network.GetSrcAddr()),
"DstAddr": ipToStr(flow.Network.GetDstAddr()),
"SrcMac": macToStr(flow.DataLink.GetSrcMac()),
"DstMac": macToStr(flow.DataLink.GetDstMac()),
"Etype": flow.EthProtocol,
"Packets": flow.Packets,
"Duplicate": flow.Duplicate,
"Proto": flow.Transport.GetProtocol(),
"TimeFlowStartMs": flow.TimeFlowStart.AsTime().UnixMilli(),
"TimeFlowEndMs": flow.TimeFlowEnd.AsTime().UnixMilli(),
"TimeReceived": time.Now().Unix(),
"Interface": flow.Interface,
"AgentIP": ipToStr(flow.AgentIp),
}

proto := flow.Transport.GetProtocol()
if proto == syscall.IPPROTO_ICMP || proto == syscall.IPPROTO_ICMPV6 {
out["IcmpType"] = flow.GetIcmpType()
out["IcmpCode"] = flow.GetIcmpCode()
}

if proto == syscall.IPPROTO_TCP || proto == syscall.IPPROTO_UDP || proto == syscall.IPPROTO_SCTP {
if proto == syscall.IPPROTO_TCP {
out["SrcPort"] = flow.Transport.GetSrcPort()
out["DstPort"] = flow.Transport.GetDstPort()
out["Flags"] = flow.Flags
} else {
out["SrcPort"] = flow.Transport.GetSrcPort()
out["DstPort"] = flow.Transport.GetDstPort()
}
}

if flow.GetDnsId() != 0 {
out["DnsRequestTimeMs"] = flow.TimeDnsReq.AsTime().UnixMilli()
out["DnsResponseTimeMs"] = flow.TimeDnsRsp.AsTime().UnixMilli()
out["DnsId"] = flow.GetDnsId()
out["DnsFlags"] = flow.GetDnsFlags()
out["DnsFlagsResponseCode"] = dnsRcodeToStr(flow.GetDnsFlags() & 0xF)
}

if flow.GetTcpDropLatestDropCause() != 0 {
out["TcpDropBytes"] = flow.TcpDropBytes
out["TcpDropPackets"] = flow.TcpDropPackets
out["TcpDropLatestFlags"] = flow.GetTcpDropLatestFlags()
out["TcpDropLatestState"] = tcpStateToStr(flow.GetTcpDropLatestState())
out["TcpDropLatestDropCause"] = tcpDropCauseToStr(flow.GetTcpDropLatestDropCause())
}

if flow.TimeFlowRtt.AsDuration().Milliseconds() != 0 {
out["TimeFlowRttMs"] = flow.TimeFlowRtt.AsDuration().Milliseconds()
}
return out
}
Expand Down Expand Up @@ -276,3 +300,45 @@ func tcpDropCauseToStr(dropCause uint32) string {
}
return "SKB_DROP_UNKNOWN_CAUSE"
}

// dnsRcodeToStr decode DNS flags response code bits and return a string
// https://datatracker.ietf.org/doc/html/rfc2929#section-2.3
func dnsRcodeToStr(rcode uint32) string {
switch rcode {
case 0:
return "NoError"
case 1:
return "FormErr"
case 2:
return "ServFail"
case 3:
return "NXDomain"
case 4:
return "NotImp"
case 5:
return "Refused"
case 6:
return "YXDomain"
case 7:
return "YXRRSet"
case 8:
return "NXRRSet"
case 9:
return "NotAuth"
case 10:
return "NotZone"
case 16:
return "BADVERS"
case 17:
return "BADKEY"
case 18:
return "BADTIME"
case 19:
return "BADMODE"
case 20:
return "BADNAME"
case 21:
return "BADALG"
}
return "UnDefined"
}
17 changes: 6 additions & 11 deletions pkg/pipeline/decode/decode_protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestDecodeProtobuf(t *testing.T) {
TimeDnsReq: timestamppb.New(someTime),
TimeDnsRsp: timestamppb.New(someTime),
DnsId: 1,
DnsFlags: 0x80,
DnsFlags: 0x8001,
TimeFlowRtt: durationpb.New(someDuration),
}
rawPB, err := proto.Marshal(&flow)
Expand All @@ -75,8 +75,6 @@ func TestDecodeProtobuf(t *testing.T) {
"DstAddr": "5.6.7.8",
"DstMac": "11:22:33:44:55:66",
"SrcMac": "01:02:03:04:05:06",
"SrcPort": uint32(23000),
"DstPort": uint32(443),
"Duplicate": false,
"Etype": uint32(2048),
"Packets": uint64(123),
Expand All @@ -85,7 +83,6 @@ func TestDecodeProtobuf(t *testing.T) {
"TimeFlowEndMs": someTime.UnixMilli(),
"Interface": "eth0",
"AgentIP": "10.9.8.7",
"Flags": uint32(0x100),
"IcmpType": uint32(8),
"IcmpCode": uint32(0),
"TcpDropBytes": uint64(100),
Expand All @@ -96,7 +93,8 @@ func TestDecodeProtobuf(t *testing.T) {
"DnsRequestTimeMs": someTime.UnixMilli(),
"DnsResponseTimeMs": someTime.UnixMilli(),
"DnsId": uint32(1),
"DnsFlags": uint32(0x80),
"DnsFlags": uint32(0x8001),
"DnsFlagsResponseCode": "FormErr",
"TimeFlowRttMs": someDuration.Milliseconds(),
}, out)
}
Expand Down Expand Up @@ -126,16 +124,14 @@ func TestPBFlowToMap(t *testing.T) {
SrcMac: 0x010203040506,
},
Transport: &pbflow.Transport{
Protocol: 1,
Protocol: 6,
SrcPort: 23000,
DstPort: 443,
},
AgentIp: &pbflow.IP{
IpFamily: &pbflow.IP_Ipv4{Ipv4: 0x0a090807},
},
Flags: 0x100,
IcmpType: 10,
IcmpCode: 11,
TcpDropBytes: 200,
TcpDropPackets: 20,
TcpDropLatestFlags: 0x100,
Expand Down Expand Up @@ -163,14 +159,12 @@ func TestPBFlowToMap(t *testing.T) {
"Duplicate": true,
"Etype": uint32(2048),
"Packets": uint64(123),
"Proto": uint32(1),
"Proto": uint32(6),
"TimeFlowStartMs": someTime.UnixMilli(),
"TimeFlowEndMs": someTime.UnixMilli(),
"Interface": "eth0",
"AgentIP": "10.9.8.7",
"Flags": uint32(0x100),
"IcmpType": uint32(10),
"IcmpCode": uint32(11),
"TcpDropBytes": uint64(200),
"TcpDropPackets": uint64(20),
"TcpDropLatestFlags": uint32(0x100),
Expand All @@ -180,6 +174,7 @@ func TestPBFlowToMap(t *testing.T) {
"DnsResponseTimeMs": someTime.UnixMilli(),
"DnsId": uint32(1),
"DnsFlags": uint32(0x80),
"DnsFlagsResponseCode": "NoError",
"TimeFlowRttMs": someDuration.Milliseconds(),
}, out)

Expand Down
11 changes: 3 additions & 8 deletions pkg/pipeline/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,13 @@ parameters:
SrcMac: 0x010203040506,
},
Transport: &pbflow.Transport{
Protocol: 1,
Protocol: 17,
SrcPort: 23000,
DstPort: 443,
},
AgentIp: &pbflow.IP{
IpFamily: &pbflow.IP_Ipv4{Ipv4: 0x0a0b0c0d},
},
Flags: 0x100,
IcmpType: 10,
IcmpCode: 11,
TcpDropBytes: 100,
TcpDropPackets: 10,
TcpDropLatestFlags: 1,
Expand Down Expand Up @@ -241,14 +238,11 @@ parameters:
"Duplicate": false,
"Etype": float64(2048),
"Packets": float64(123),
"Proto": float64(1),
"Proto": float64(17),
"TimeFlowStartMs": float64(startTime.UnixMilli()),
"TimeFlowEndMs": float64(endTime.UnixMilli()),
"Interface": "eth0",
"AgentIP": "10.11.12.13",
"Flags": float64(0x100),
"IcmpType": float64(10),
"IcmpCode": float64(11),
"TcpDropBytes": float64(100),
"TcpDropPackets": float64(10),
"TcpDropLatestFlags": float64(1),
Expand All @@ -258,6 +252,7 @@ parameters:
"DnsResponseTimeMs": float64(endTime.UnixMilli()),
"DnsId": float64(1),
"DnsFlags": float64(0x80),
"DnsFlagsResponseCode": "NoError",
"TimeFlowRttMs": float64(someDuration.Milliseconds()),
}, capturedRecord)
}
Expand Down
Loading