JFLib

netpkt/decode.rs#

Source: lib/netpkt/decode.rs

decode -- dissects a frame into layers for packet inspectors and event filters

  stack(frame) -> Stack          the protocol layers and their header ranges, no allocation
  proto_mask(frame) -> u32       P_* bits of every layer present (for event filters)
  stack_ip / ip_mask(packet)     the same for a bare IP packet (serial lines, tunnels)
  layers(frame) -> Vec<Layer>    per layer: title, fields with byte ranges (for a hex view),
                                 problems (bad checksum, truncated header, ...)
  info(frame) -> (Proto, String) the top protocol and a one-line summary like a capture tool
                                 ("Echo (ping) request id=0x0001, seq=1, ttl=64")

Application protocols are recognised by their well-known ports.  DHCP (fixed fields and
every option), DNS (header, questions and records, also over TCP), NTP and syslog have
dissectors; the payload of the others shows as one field.
BPDUs of STP and RSTP (LLC SAP 0x42) show every field and flag (stp.rs).
Field names and values follow the usual capture-tool wording (English protocol terms).
SNMPv3 (RFC 3412, 3414) shows msgID, msgMaxSize, msgFlags with their names, the security model,
the engine ID, boots and time, the user, the authentication and privacy parameters, and the scoped
PDU as in 2c, or the field "Encrypted scopedPDU" with the value "verschlüsselt" (snmpv3.rs).
SSH (TCP port 22) shows the identification line and, while the connection is in the clear, each
packet with its length, padding and message name, the lists of a KEXINIT and the values of the
key exchange; everything after NEWKEYS is one field with the value "verschlüsselt" (a German
word on purpose: it is what the inspector's reader needs to see, no protocol term) (ssh.rs).
TLS (TCP ports 443 and 8443) shows every record a segment holds with its type, version and length;
what goes in the clear field by field (ClientHello and ServerHello with their random, session id,
suites and extensions, an alert in the clear); everything protected is one field with the value
"verschlüsselt" (tls.rs).  Only what a capture tool can see without keys is here.
HTTP (ports 80, 8080, 8000, and any TCP segment that starts like a request or a response)
shows the start line, the header lines and the body bytes of every message the segment
holds; a segment in the middle of a message is a "Continuation".
RIP (port 520) shows its header and one field per entry (route, authentication, the request
for the whole table) with the problems a receiver would refuse (RFC 2453, RFC 1058).
IPv6 shows its extension headers as fields of its layer (a later fragment carries data only);
ICMPv6 every field of echo, the errors (with the quoted packet) and Neighbor Discovery (RS, RA,
NS, NA, redirect and one field per option) with what a receiver refuses: a checksum over the
pseudo header that does not fit, an ND message whose hop limit is not 255 (RFC 4443, 4861).
OSPF (IP protocol 89) shows the common header, the fields of the five packet types, every LSA
header and the links, routers and metrics of the LSAs an update carries, with what a receiver
refuses: another version, a wrong packet or LSA checksum, bodies that do not fit (RFC 2328).
EAPOL (ethertype 0x888E) shows its header and an EAPOL-Key body field by field: the Key
Information bits, replay counter, nonce, MIC and the key data (the RSN element and, when it is
not encrypted, the KDEs); the summary names the message of the 4-way handshake (eapol.rs).
BGP (TCP port 179) shows every message a segment holds: the header, the fields of OPEN with each
capability, UPDATE with each withdrawn route, path attribute (its flags and value) and NLRI,
NOTIFICATION with the names of its code and subcode, ROUTE-REFRESH; what a receiver would answer
with a NOTIFICATION is a problem with that code; a segment that does not start with the marker
is the rest of a message of an earlier one (bgp.rs; AS numbers of 2 or 4 octets as
bgp::as_path_4 guesses).
TFTP (port 69, and the DATA, ACK, ERROR and OACK packets of a transfer between two ports
above 1023 by their shape: RFC 1350 moves a transfer to new ports) shows its opcode, file,
mode, options, block, data and error (tftp.rs).  FTP (TCP port 21) shows each command line
with its verb and argument and each reply line with its code; FTP-DATA (port 20, the active
data connections) its bytes; passive data connections run between two free ports and show
as TCP data (ftp.rs).
SMTP (TCP ports 25 and 587) shows each command line with its verb and parameter, each reply
line with its code and enhanced status code, and the lines of a message after DATA as such
(the line with the dot ends it); POP3 (port 110) each command with its parameters, the status
indicator and description of a response, the lines of a multi-line one.  Both share the bit
P_MAIL (smtp.rs, pop3.rs).
SNMP (UDP ports 161 and 162) shows version, community, the PDU with request id, error status and
index (non-repeaters and max-repetitions of a GetBulk; enterprise, agent address, generic and
specific trap and time stamp of a v1 Trap) and each variable binding with the name of a known
object and its value as net-snmp prints it; BER that does not hold is a problem at its place
(snmp.rs, ber.rs, mib.rs).
RADIUS (UDP ports 1812 and 1813, the old 1645 and 1646) shows its code, identifier, length and
authenticator and every attribute by name with its value (named values of the integers, a
Vendor-Specific attribute's vendor, a hidden User-Password as hidden), with what a receiver
discards: a Length that does not fit, attributes that do not end at it (radius.rs).  Telnet
(TCP port 23) shows each command and negotiation ("Will Echo", "Interrupt Process") and the
data as text; a command cut off at the end of a segment is a problem (telnet.rs).
EAP in an EAPOL frame is a layer of its own: code, identifier, length, type and the data of
Identity, Nak, EAP-MD5 and EAP-PWD field by field (eap.rs); as an EAP-Message of RADIUS one
line with the same words.
IMAP (TCP port 143) shows each line: a command with its tag, verb and arguments, a response with
its tag or "*", status, response code and text; the bytes of a literal as a line of data. It
shares the bit P_MAIL (imap.rs).
P_ETH: u32 = 1 << 0
P_VLAN: u32 = 1 << 1
P_LLC: u32 = 1 << 2
P_ARP: u32 = 1 << 3
P_IPV4: u32 = 1 << 4
P_IPV6: u32 = 1 << 5
P_ICMP: u32 = 1 << 6
P_ICMPV6: u32 = 1 << 7
P_IGMP: u32 = 1 << 8
P_UDP: u32 = 1 << 9
P_TCP: u32 = 1 << 10
P_DHCP: u32 = 1 << 11
P_DNS: u32 = 1 << 12
P_HTTP: u32 = 1 << 13
P_TLS: u32 = 1 << 14
P_SSH: u32 = 1 << 15
P_STP: u32 = 1 << 16
P_RIP: u32 = 1 << 17
P_OSPF: u32 = 1 << 18
P_NTP: u32 = 1 << 19
P_SYSLOG: u32 = 1 << 20
P_TFTP: u32 = 1 << 21
P_SNMP: u32 = 1 << 22
P_LLDP: u32 = 1 << 23
P_DHCPV6: u32 = 1 << 24
P_EAPOL: u32 = 1 << 25
/// BGP (TCP port 179); a bit far from the next free ones, which other protocols take in order
P_BGP: u32 = 1 << 30
/// FTP's control connection and its active data connections (FTP-DATA)
P_FTP: u32 = 1 << 26
P_DATA: u32 = 1 << 31
/// mail: SMTP (TCP ports 25 and 587) and POP3 (port 110)
P_MAIL: u32 = 1 << 28
/// RADIUS (UDP ports 1812, 1813, 1645, 1646)
P_RADIUS: u32 = 1 << 27
/// Telnet (TCP port 23)
P_TELNET: u32 = 1 << 29
  • enum Proto a protocol layer
    • Eth
    • Vlan
    • Llc
    • Arp
    • Ipv4
    • Ipv6
    • Icmp
    • Icmpv6
    • Igmp
    • Udp
    • Tcp
    • Dhcp
    • Dhcpv6
    • Dns
    • Http
    • Tls
    • Ssh
    • Stp
    • Rip
    • Ospf
    • Ntp
    • Syslog
    • Tftp
    • Snmp
    • Lldp
    • Data
    • Eapol
    • Bgp
    • Ftp -- FTP's control connection (port 21)
    • FtpData -- an active FTP data connection (port 20)
    • Smtp -- SMTP (ports 25 and 587)
    • Pop3 -- POP3 (port 110)
    • Radius -- RADIUS authentication and accounting
    • Telnet -- Telnet (port 23)
    • Eap -- EAP inside EAPOL
    • Imap -- IMAP (port 143)

impl Proto

  • fn name(self) -> &'static str

  • fn bit(self) -> u32

  • struct Stack the layers of a frame: protocol, header offset and header length (the application layer and Data cover their payload)

    • n: usize
    • l: [(Proto, u32, u32); 10]
    • trailer: u32 -- bytes after the network packet (Ethernet padding)

impl Stack

  • fn layers(&self) -> &[(Proto, u32, u32)]

  • fn top(&self) -> Proto

  • fn mask(&self) -> u32

  • fn find(&self, p: Proto) -> Option<(usize, usize)>

  • fn stack(frame: &[u8]) -> Stack the layers of a frame (header ranges); stops at the first malformed header

  • fn proto_mask(frame: &[u8]) -> u32 the P_* bits of every layer of a frame

  • fn stack_ip(packet: &[u8]) -> Stack the layers of a bare IP packet (no link header: serial lines, tunnels); offsets are relative to the packet

  • fn ip_mask(packet: &[u8]) -> u32 the P_* bits of a bare IP packet

  • struct Field one header field: name, shown value, byte range in the frame

    • name: &'static str
    • value: String
    • off: u32
    • len: u32
  • struct Layer one layer of a dissected frame

    • proto: Proto
    • off: u32 -- header range in the frame
    • len: u32
    • title: String -- "Internet Protocol Version 4, Src: 10.0.0.1, Dst: 10.0.0.2"
    • fields: Vec<Field>
    • problems: Vec<String> -- "bad header checksum (should be 0x1c46)", ...
  • fn layers(frame: &[u8]) -> Vec<Layer> every layer of a frame with its fields

  • fn info(frame: &[u8]) -> (Proto, String) the top protocol of a frame and a one-line summary

Source: docs/API.md in the JFLib repository.