Coverage Report

Created: 2026-08-14 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zeek/src/packet_analysis/protocol/ethernet/Ethernet.cc
Line
Count
Source
1
// See the file "COPYING" in the main distribution directory for copyright.
2
3
#include "zeek/packet_analysis/protocol/ethernet/Ethernet.h"
4
5
#include "zeek/packet_analysis/Manager.h"
6
7
using namespace zeek::packet_analysis::Ethernet;
8
9
66
EthernetAnalyzer::EthernetAnalyzer() : zeek::packet_analysis::Analyzer("Ethernet") {
10
66
    snap_forwarding_key = id::find_val("PacketAnalyzer::ETHERNET::SNAP_FORWARDING_KEY")->AsCount();
11
66
    novell_forwarding_key = id::find_val("PacketAnalyzer::ETHERNET::NOVELL_FORWARDING_KEY")->AsCount();
12
66
    llc_forwarding_key = id::find_val("PacketAnalyzer::ETHERNET::LLC_FORWARDING_KEY")->AsCount();
13
66
}
14
15
7.33k
bool EthernetAnalyzer::AnalyzePacket(size_t len, const uint8_t* data, Packet* packet) {
16
    // Make sure that we actually got an entire ethernet header before trying
17
    // to pull bytes out of it.
18
7.33k
    if ( 16 >= len ) {
19
169
        Weird("truncated_ethernet_frame", packet);
20
169
        return false;
21
169
    }
22
23
    // Skip past Cisco FabricPath to encapsulated ethernet frame.
24
7.16k
    if ( data[12] == 0x89 && data[13] == 0x03 ) {
25
104
        auto constexpr cfplen = 16;
26
27
104
        if ( cfplen + 14 >= len ) {
28
20
            Weird("truncated_link_header_cfp", packet);
29
20
            return false;
30
20
        }
31
32
84
        data += cfplen;
33
84
        len -= cfplen;
34
84
    }
35
36
    // Get protocol being carried from the ethernet frame.
37
7.14k
    uint32_t protocol = (data[12] << 8) + data[13];
38
39
7.14k
    packet->eth_type = protocol;
40
7.14k
    packet->l2_dst = data;
41
7.14k
    packet->l2_src = data + 6;
42
43
    // Ethernet II frames
44
7.14k
    if ( protocol >= 1536 )
45
6.31k
        return ForwardPacket(len - 14, data + 14, packet, protocol);
46
47
    // Other ethernet frame types
48
832
    if ( protocol <= 1500 ) {
49
771
        len -= 14;
50
771
        data += 14;
51
52
        // Need at least two bytes to check the packet types below.
53
771
        if ( len < 2 ) {
54
18
            Weird("truncated_ethernet_frame", packet);
55
18
            return false;
56
18
        }
57
753
        if ( len > protocol )
58
369
            len = protocol; // use 802.3/802.2 length field and remove trailing bytes
59
60
        // Let specialized analyzers take over for non Ethernet II frames.
61
753
        if ( data[0] == 0xAA && data[1] == 0xAA )
62
            // IEEE 802.2 SNAP
63
74
            return ForwardPacket(len, data, packet, snap_forwarding_key);
64
679
        else if ( data[0] == 0xFF && data[1] == 0xFF )
65
            // Novell raw IEEE 802.3
66
34
            return ForwardPacket(len, data, packet, novell_forwarding_key);
67
645
        else
68
            // IEEE 802.2 LLC
69
645
            return ForwardPacket(len, data, packet, llc_forwarding_key);
70
753
    }
71
72
    // Undefined (1500 < EtherType < 1536)
73
61
    Weird("undefined_ether_type", packet);
74
61
    return false;
75
832
}