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

netflow v9 some templates not being parsed - Multiple errors: can not read the data #42

Closed
jgc234 opened this issue Dec 3, 2017 · 8 comments

Comments

@jgc234
Copy link

jgc234 commented Dec 3, 2017

I'm having trouble with some v9 templates not being parsed from a Juniper SRX.. Some are, and some aren't.. As an example below - template id# 261 seems to fail to be defined, even though its definition gets transmitted 60 seconds by the sending router. First some background:

  • sending device - SRX210HE JUNOS Software Release [12.1X46-D67]
  • receiving devices (similar results from both)
  • built from git (today), VERSION=0.4.1 in Makefile (although interestingly that version doesn't populate into the version [vflow] 2017/12/03 14:31:45 Welcome to vFlow v.unknown Apache License 2.0)

Router config:

set services flow-monitoring version9 template template_1 ipv4-template
set services flow-monitoring version9 template template_2 ipv6-template
set forwarding-options sampling input rate 1
set forwarding-options sampling family inet output flow-inactive-timeout 30
set forwarding-options sampling family inet output flow-active-timeout 60
set forwarding-options sampling family inet output flow-server 10.232.6.89 port 4729
set forwarding-options sampling family inet output flow-server 10.232.6.89 version9 template template_1
set forwarding-options sampling family inet output inline-jflow source-address 10.232.4.5
set forwarding-options sampling family inet6 output flow-inactive-timeout 30
set forwarding-options sampling family inet6 output flow-active-timeout 60
set forwarding-options sampling family inet6 output flow-server 10.232.6.89 port 4729
set forwarding-options sampling family inet6 output flow-server 10.232.6.89 version9 template template_2
set forwarding-options sampling family inet6 output inline-jflow source-address 10.232.4.5

From the vflow.log file.. The "can not read data" appears every minute.

[vflow] 2017/11/28 11:10:16 Multiple errors:
- 10.232.4.5 unknown netflow template id# 261
- can not read the data
- can not read the data
- can not read the data
[vflow] 2017/11/28 11:10:16 rcvd netflow v9 data from: 10.232.4.5:63651, size: 144 bytes
[vflow] 2017/11/28 11:10:16 Multiple errors:
- can not read the data
- can not read the data
- can not read the data
- can not read the data
- can not read the data
- can not read the data
- can not read the data
- can not read the data
- can not read the data
- can not read the data
- can not read the data
- can not read the data
[vflow] 2017/11/28 11:10:18 rcvd netflow v9 data from: 10.232.4.5:58938, size: 144 bytes
[vflow] 2017/11/28 11:10:18 10.232.4.5 unknown netflow template id# 261

(After a few days it also crashes with a backtrace that I haven't really had a look at yet)

To me it looks like the definition of template 261 is being sent every minute, along with a few flows using the same template id.. Here's a packet.

Frame 16: 618 bytes on wire (4944 bits), 618 bytes captured (4944 bits) on interface 0
    Interface id: 0 (net0)
    Encapsulation type: Ethernet (1)
    Arrival Time: Dec  3, 2017 15:02:37.876916000 AEDT
    [Time shift for this packet: 0.000000000 seconds]
    Epoch Time: 1512273757.876916000 seconds
    [Time delta from previous captured frame: 2.002214000 seconds]
    [Time delta from previous displayed frame: 2.002214000 seconds]
    [Time since reference or first frame: 19.046828000 seconds]
    Frame Number: 16
    Frame Length: 618 bytes (4944 bits)
    Capture Length: 618 bytes (4944 bits)
    [Frame is marked: False]
    [Frame is ignored: False]
    [Protocols in frame: eth:ethertype:ip:udp:cflow]
Ethernet II, Src: JuniperN_cb:2f:01 (80:71:1f:cb:2f:01), Dst: 82:c5:5d:98:41:4a (82:c5:5d:98:41:4a)
    Destination: 82:c5:5d:98:41:4a (82:c5:5d:98:41:4a)
        Address: 82:c5:5d:98:41:4a (82:c5:5d:98:41:4a)
        .... ..1. .... .... .... .... = LG bit: Locally administered address (this is NOT the factory default)
        .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
    Source: JuniperN_cb:2f:01 (80:71:1f:cb:2f:01)
        Address: JuniperN_cb:2f:01 (80:71:1f:cb:2f:01)
        .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
        .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
    Type: IPv4 (0x0800)
Internet Protocol Version 4, Src: 10.232.4.5, Dst: 10.232.6.89
    0100 .... = Version: 4
    .... 0101 = Header Length: 20 bytes (5)
    Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
        0000 00.. = Differentiated Services Codepoint: Default (0)
        .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
    Total Length: 604
    Identification: 0x7864 (30820)
    Flags: 0x00
        0... .... = Reserved bit: Not set
        .0.. .... = Don't fragment: Not set
        ..0. .... = More fragments: Not set
    Fragment offset: 0
    Time to live: 63
    Protocol: UDP (17)
    Header checksum: 0xe0ff [validation disabled]
    [Header checksum status: Unverified]
    Source: 10.232.4.5
    Destination: 10.232.6.89
User Datagram Protocol, Src Port: 54060, Dst Port: 4729
    Source Port: 54060
    Destination Port: 4729
    Length: 584
    Checksum: 0xfb15 [unverified]
    [Checksum Status: Unverified]
    [Stream index: 0]
Cisco NetFlow/IPFIX
    Version: 9
    Count: 10
    SysUptime: 2613.569000000 seconds
    Timestamp: Dec  3, 2017 15:02:37.000000000 AEDT
        CurrentSecs: 1512273757
    FlowSequence: 372
    SourceId: 142
    FlowSet 1 [id=261]
        FlowSet Id: (Data) (261)
        FlowSet Length: 184
        Data (180 bytes), no template found
            [Expert Info (Warning/Malformed): Data (180 bytes), no template found]
                [Data (180 bytes), no template found]
                [Severity level: Warning]
                [Group: Malformed]
    FlowSet 2 [id=1] (Options Template): 256
        FlowSet Id: Options Template(V9) (1)
        FlowSet Length: 24
        Options Template (Id = 256) (Scope Count = 1; Data Count = 2)
            Template Id: 256
            Option Scope Length: 4
            Option Length: 8
            Field (1/1) [Scope]: System
                Scope Type: System (1)
                Length: 0
            Field (1/2): SAMPLING_ALGORITHM
                Type: SAMPLING_ALGORITHM (35)
                Length: 1
            Field (2/2): SAMPLING_INTERVAL
                Type: SAMPLING_INTERVAL (34)
                Length: 4
        Padding: 0000
    FlowSet 3 [id=256] (1 flows)
        FlowSet Id: (Data) (256)
        FlowSet Length: 12
        [Template Frame: 16]
        Flow 1
            Sampling algorithm: Random sampling (2)
            Sampling interval: 1
        Padding: 000000
    FlowSet 4 [id=0] (Data Template): 261
        FlowSet Id: Data Template (V9) (0)
        FlowSet Length: 92
        Template (Id = 261, Count = 21)
            Template Id: 261
            Field Count: 21
            Field (1/21): IP_SRC_ADDR
                Type: IP_SRC_ADDR (8)
                Length: 4
            Field (2/21): IP_DST_ADDR
                Type: IP_DST_ADDR (12)
                Length: 4
            Field (3/21): IP_TOS
                Type: IP_TOS (5)
                Length: 1
            Field (4/21): PROTOCOL
                Type: PROTOCOL (4)
                Length: 1
            Field (5/21): L4_SRC_PORT
                Type: L4_SRC_PORT (7)
                Length: 2
            Field (6/21): L4_DST_PORT
                Type: L4_DST_PORT (11)
                Length: 2
            Field (7/21): ICMP_TYPE
                Type: ICMP_TYPE (32)
                Length: 2
            Field (8/21): INPUT_SNMP
                Type: INPUT_SNMP (10)
                Length: 4
            Field (9/21): SRC_MASK
                Type: SRC_MASK (9)
                Length: 1
            Field (10/21): DST_MASK
                Type: DST_MASK (13)
                Length: 1
            Field (11/21): SRC_AS
                Type: SRC_AS (16)
                Length: 4
            Field (12/21): DST_AS
                Type: DST_AS (17)
                Length: 4
            Field (13/21): BGP_NEXT_HOP
                Type: BGP_NEXT_HOP (18)
                Length: 4
            Field (14/21): TCP_FLAGS
                Type: TCP_FLAGS (6)
                Length: 1
            Field (15/21): OUTPUT_SNMP
                Type: OUTPUT_SNMP (14)
                Length: 4
            Field (16/21): IP_NEXT_HOP
                Type: IP_NEXT_HOP (15)
                Length: 4
            Field (17/21): BYTES
                Type: BYTES (1)
                Length: 4
            Field (18/21): PKTS
                Type: PKTS (2)
                Length: 4
            Field (19/21): FIRST_SWITCHED
                Type: FIRST_SWITCHED (22)
                Length: 4
            Field (20/21): LAST_SWITCHED
                Type: LAST_SWITCHED (21)
                Length: 4
            Field (21/21): IP_PROTOCOL_VERSION
                Type: IP_PROTOCOL_VERSION (60)
                Length: 1
    FlowSet 5 [id=261] (4 flows)
        FlowSet Id: (Data) (261)
        FlowSet Length: 244
        [Template Frame: 16]
        Flow 1
            SrcAddr: xxx.xxx.xxx.xxx
            DstAddr: xxx.xxx.xxx.xxx
            IP ToS: 0x00
            Protocol: TCP (6)
            SrcPort: 443 (443)
            DstPort: 17776 (17776)
            ICMP Type: 0x0000
            InputInt: 539
            SrcMask: 32
            DstMask: 32
            SrcAS: 0
            DstAS: 0
            BGPNextHop: 0.0.0.0
            TCP Flags: 0x1b, ACK, PSH, SYN, FIN
                00.. .... = Reserved: 0x0
                ..0. .... = URG: Not used
                ...1 .... = ACK: Used
                .... 1... = PSH: Used
                .... .0.. = RST: Not used
                .... ..1. = SYN: Used
                .... ...1 = FIN: Used
            OutputInt: 0
            NextHop: 0.0.0.0
            Octets: 6166
            Packets: 9
            [Duration: 1.320000000 seconds (switched)]
                StartTime: 2609.845000000 seconds
                EndTime: 2611.165000000 seconds
            IPVersion: 4
        Flow 2
            SrcAddr: xxx.xxx.xxx.xxx
            DstAddr: xxx.xxx.xxx.xxx
            IP ToS: 0x00
            Protocol: TCP (6)
            SrcPort: 17776 (17776)
            DstPort: 443 (443)
            ICMP Type: 0x0000
            InputInt: 536
            SrcMask: 32
            DstMask: 32
            SrcAS: 0
            DstAS: 0
            BGPNextHop: 0.0.0.0
            TCP Flags: 0x1b, ACK, PSH, SYN, FIN
                00.. .... = Reserved: 0x0
                ..0. .... = URG: Not used
                ...1 .... = ACK: Used
                .... 1... = PSH: Used
                .... .0.. = RST: Not used
                .... ..1. = SYN: Used
                .... ...1 = FIN: Used
            OutputInt: 539
            NextHop: 0.0.0.0
            Octets: 1441
            Packets: 13
            [Duration: 1.319000000 seconds (switched)]
                StartTime: 2609.816000000 seconds
                EndTime: 2611.135000000 seconds
            IPVersion: 4
        Flow 3
            SrcAddr: xxx.xxx.xxx.xxx
            DstAddr: xxx.xxx.xxx.xxx
            IP ToS: 0x00
            Protocol: UDP (17)
            SrcPort: 53 (53)
            DstPort: 12494 (12494)
            ICMP Type: 0x0000
            InputInt: 539
            SrcMask: 32
            DstMask: 29
            SrcAS: 0
            DstAS: 0
            BGPNextHop: 0.0.0.0
            TCP Flags: 0x00
                00.. .... = Reserved: 0x0
                ..0. .... = URG: Not used
                ...0 .... = ACK: Not used
                .... 0... = PSH: Not used
                .... .0.. = RST: Not used
                .... ..0. = SYN: Not used
                .... ...0 = FIN: Not used
            OutputInt: 536
            NextHop: 10.232.4.3
            Octets: 76
            Packets: 1
            [Duration: 0.000000000 seconds (switched)]
                StartTime: 2552.968000000 seconds
                EndTime: 2552.968000000 seconds
            IPVersion: 4
        Flow 4
            SrcAddr: xxx.xxx.xxx.xxx
            DstAddr: xxx.xxx.xxx.xxx
            IP ToS: 0x00
            Protocol: UDP (17)
            SrcPort: 12494 (12494)
            DstPort: 53 (53)
            ICMP Type: 0x0000
            InputInt: 536
            SrcMask: 29
            DstMask: 32
            SrcAS: 0
            DstAS: 0
            BGPNextHop: 0.0.0.0
            TCP Flags: 0x00
                00.. .... = Reserved: 0x0
                ..0. .... = URG: Not used
                ...0 .... = ACK: Not used
                .... 0... = PSH: Not used
                .... .0.. = RST: Not used
                .... ..0. = SYN: Not used
                .... ...0 = FIN: Not used
            OutputInt: 539
            NextHop: 0.0.0.0
            Octets: 60
            Packets: 1
            [Duration: 0.000000000 seconds (switched)]
                StartTime: 2552.811000000 seconds
                EndTime: 2552.811000000 seconds
            IPVersion: 4

Some other templates are working fine.. I haven't worked out what the relationship is between the successful and failing ones yet.

[vflow] 2017/12/03 14:52:29 {"AgentID":"10.232.4.5","Header":{"Version":9,"Count":7,"SysUpTime":1708483,"UNIXSecs":1512272852,"SeqNum":427,"SrcID":142},"DataSets":[[{"I":35,"V":2},{"I":34,"V":1},{"I":1,"V":"0x"}],[{"I":35,"V":0},{"I":34,"V":0},{"I":1,"V":"0x"}],[{"I":35,"V":1},.......
Any thoughts on how to debug this futher?

@jgc234
Copy link
Author

jgc234 commented Dec 6, 2017

I think I've worked this one out.. The detector for padding between flowsets broken in vflow/netflow/v9/decoder.go:decodeSet(). It failed to detect the padding, got out-of-sync and started parsing the padding as the next record. .. Once it started eating rubbish, the promotion to an unsigned was wrapping and trying to parse 65K of data (uint16(-1)) .. Currently:

// the next set should be greater than 4 bytes otherwise that's padding                                                                                                                                                                                                                                                                                                                       
        for err == nil && setHeader.Length > uint16(d.reader.ReadCount()-startCount) && d.reader.Len() > 4 {         

The "d.reader.Len() > 4" would detect the total bytes remaining in the packet, however we also need to test for bytes remaining in the current flowset (against the header length vs the current offset).

// the next set should be greater than 4 bytes otherwise that's padding                                                                                                                                         
        for err == nil && (int(setHeader.Length) - (d.reader.ReadCount()-startCount) > 4) && d.reader.Len() > 4 {                                                                                                       

Someone will need to check that logic - I don't know Go or Netflow well.

@jgc234
Copy link
Author

jgc234 commented Dec 6, 2017

Actuallly.. there is a bit of code further down that detects the padding, but it does so by entering the loop again and peeking at the data - if there's zeros there, it bails out.. (rather than checking the current offset position in the FlowSet).. and it only does this for FlowSetID of 0 or 1 (ie templates, not data).

In the packet above, FlowSetID 256 (data) has padding, so it won't get caught by this test..

The IPFIX code uses the same algorithm to peak past the end and hope for zeros.

Is there some magic reason why it's done this way?

@jgc234
Copy link
Author

jgc234 commented Dec 6, 2017

diff --git a/netflow/v9/decoder.go b/netflow/v9/decoder.go
index 8b17136..a9de9e2 100644
--- a/netflow/v9/decoder.go
+++ b/netflow/v9/decoder.go
@@ -435,16 +435,10 @@ func (d *Decoder) decodeSet(mem MemCache, msg *Message) error {
        }
 
        // the next set should be greater than 4 bytes otherwise that's padding
-       for err == nil && setHeader.Length > uint16(d.reader.ReadCount()-startCount) && d.reader.Len() > 4 {
+       for err == nil && (int(setHeader.Length) - (d.reader.ReadCount()-startCount) > 4) && d.reader.Len() > 4 {
                if setId := setHeader.FlowSetID; setId == 0 || setId == 1 {
                        // Template record or template option record
 
-                       // Check if only padding is left in this set. A template id of zero indicates padding bytes, which MUST be zero.
-                       templateId, err := d.reader.PeekUint16()
-                       if err == nil && templateId == 0 {
-                               break
-                       }
-
                        tr := TemplateRecord{}
                        if setId == 0 {
                                err = tr.unmarshal(d.reader)

@mehrdadrad
Copy link
Collaborator

@jgc234 thank you for looking at this issue. I tried netflow during development by nprobe as I don't have any netflow device. I can work on this issue next week.

@jgc234
Copy link
Author

jgc234 commented Dec 8, 2017

@mehrdadrad happy to help.. you could spin up a router in a VM (Juniper vSRX or Cisco CSR100v). or replay some captured packets from someone else. I haven't read the IPFIX to see how it'd different from V9.. I'm unsure if the same padding problem could occur there to.

@mehrdadrad
Copy link
Collaborator

@jgc234 can you send me a pcap including templates and samples to my email address: arshad.rad@gmail.com ?

@mehrdadrad
Copy link
Collaborator

@jgc234 pls try and see how it works w/ your SRX. it changed same as you mentioned.

@jgc234
Copy link
Author

jgc234 commented Dec 12, 2017

@mehrdadrad - tested and works fine for the last few hours using an SRX with netflow v9 IPv4 only.. removed netflowv9.tempaltes cache file to force correct parsing of transmitted template definitions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants