Skip to main content

Module codec

Module codec 

Source
Expand description

Pure ICMPv6 Neighbour Discovery (RFC 4861) codec.

Everything in this file is a pure function over plain values: it opens no socket, reads no configuration and touches no global state. That is the whole design. NDP’s real transport is a raw ICMPv6 socket, which needs root or CAP_NET_RAW, and nothing in this repository has that — so the transport can never be executed here, but the packet format can be, and is, checked against literal bytes in tests/server/ndp/codec_test.rs.

This is the bluetooth_ble_beacon split the root CLAUDE.md describes: message construction is pure and exhaustively tested; the platform transport is a thin layer over it that has never run. metadata().notes says both.

§Message layouts (RFC 4861 §4)

Every one starts with the ICMPv6 header of RFC 4443 §2.1 — type, code, checksum — and every one of the five NDP types uses code 0. Options, when present, always follow.

Router Solicitation  (133)  type code cksum | reserved(4)                          | options
Router Advertisement (134)  type code cksum | hop(1) flags(1) lifetime(2)
                                            | reachable(4) | retrans(4)            | options
Neighbour Solicit.   (135)  type code cksum | reserved(4) | target(16)             | options
Neighbour Advert.    (136)  type code cksum | flags(4)    | target(16)             | options
Redirect             (137)  type code cksum | reserved(4) | target(16) | dest(16)  | options

§Two things implementations get wrong, both pinned by literal-byte tests

  1. Option length is in units of 8 octets, not octets (RFC 4861 §4.6). A Prefix Information option is 32 octets on the wire and its length field reads 4. Writing 32 there produces an option a real stack walks straight past the end of, and it is the classic NDP bug.
  2. The ICMPv6 checksum covers an IPv6 pseudo-header (RFC 4443 §2.3, RFC 8200 §8.1) — the source address, the destination address, the upper-layer length and next header 58. It is therefore not computable from the ICMPv6 bytes alone, which is why every encode entry point on this type demands both addresses. A codec that computes it over the message only produces packets every conforming receiver silently drops.

§No bytes cross the LLM boundary

Addresses go in and out as IPv6 strings, link-layer addresses as "00:11:22:33:44:55", flags as booleans, lifetimes as seconds. There is no way to hand this codec a hex blob and there is deliberately no action parameter that would accept one.

Structs§

PrefixInformation
A Prefix Information option’s contents (RFC 4861 §4.6.2).
RouterAdvertisement
The body of a Router Advertisement (RFC 4861 §4.2).
SendRequest
A validated request to transmit one NDP message.

Enums§

NdpMessage
One of the five Neighbour Discovery messages, decoded.
NdpOption
One Neighbour Discovery option.

Constants§

ADDRESSED_PREFIX_LEN
How many octets of addressing precede the ICMPv6 message on the UDP test transport.
ALL_NODES_MULTICAST
ff02::1, the all-nodes link-local multicast group: where an unsolicited Router Advertisement goes.
ALL_ROUTERS_MULTICAST
ff02::2, the all-routers link-local multicast group: where a Router Solicitation goes.
LIFETIME_INFINITE
A lifetime of 0xffffffff means “for ever” in a Prefix Information option and in RDNSS.
NA_FLAG_OVERRIDE
NA_FLAG_ROUTER
Neighbour Advertisement flags (RFC 4861 §4.4) — the top three bits of a 32-bit field.
NA_FLAG_SOLICITED
NDP_CODE
RFC 4861 §4: all five NDP messages use ICMPv6 code 0, and a receiver discards anything else.
NDP_HOP_LIMIT
RFC 4861 §11.2: every NDP message is sent with an IPv6 Hop Limit of 255 and a receiver discards one that arrives with anything less. That is the protocol’s entire defence against an off-link attacker — it cannot be forged, because a router decrements.
NEXT_HEADER_ICMPV6
IPv6 “Next Header” value for ICMPv6 (RFC 4443 §1). The pseudo-header carries this, not 0.
OPT_MTU
OPT_PREFIX_INFORMATION
OPT_RDNSS
OPT_SOURCE_LINK_LAYER_ADDRESS
Option types (RFC 4861 §4.6, RFC 8106 §5.1 for RDNSS).
OPT_TARGET_LINK_LAYER_ADDRESS
PREFIX_FLAG_AUTONOMOUS
PREFIX_FLAG_ON_LINK
Prefix Information flags (RFC 4861 §4.6.2).
RA_FLAG_MANAGED
Router Advertisement flags (RFC 4861 §4.2).
RA_FLAG_OTHER
TYPE_NEIGHBOR_ADVERTISEMENT
TYPE_NEIGHBOR_SOLICITATION
TYPE_REDIRECT
TYPE_ROUTER_ADVERTISEMENT
TYPE_ROUTER_SOLICITATION

Functions§

decode_addressed
The inverse of encode_addressed.
decode_options
Decode a run of options.
encode_addressed
Wrap an ICMPv6 message with the two addresses its checksum was computed over.
format_mac
Render six octets as lowercase colon-separated hex.
icmpv6_checksum
The ICMPv6 checksum over the IPv6 pseudo-header of RFC 8200 §8.1 plus message.
parse_mac
Parse aa:bb:cc:dd:ee:ff (also accepting -, . and space separators) into six octets.
solicited_node_multicast
The solicited-node multicast address for a target (RFC 4291 §2.7.1): ff02::1:ff00:0/104 with the target’s low 24 bits appended.
verify_checksum
Verify the checksum a message carries against the addresses it was sent between.
write_checksum
Compute the checksum for message and write it into its header.