RADIUS

Remote Authentication Dial In User Service, RFC 2865, with accounting (RFC 2866) and dynamic authorization, Disconnect and CoA (RFC 5176): who connects, through which equipment, with what result. Decoded since 11.6.0 (#101, #156). The module is packet_parser::parse::application::protocols::radius: the header, then the list of attributes, each one borrowed from the datagram and, when the crate knows its type, named after the IANA registry, with accessors for the integers, IPv4 addresses and texts of the RFC and for the vendor id of a Vendor-Specific. Passwords stay encrypted: nothing in the crate knows the shared secret.

Detection: five ports, then any UDP port

Two rules of the dispatch table, 26 and 44, share one probe, ProbeId::Radius:

#![allow(unused)]
fn main() {
// among the port rules, after DIS
port_rule(ApplicationProtocol::Radius, Guard::Udp, is_radius_udp_port, ProbeId::Radius), // 1812, 1813, 1645, 1646, 3799
// in the blind cascade, after DNP3, before BACnet/IP
rule(ApplicationProtocol::Radius, Guard::Udp, ProbeId::Radius),
}

is_radius_udp_port matches, on either side, 1812 and 1813, the IANA ports, 1645 and 1646, their historical numbers (the switch of radius.pcap still sends from 1645), and 3799, the port of RFC 5176. On UDP 1700, which tshark also dissects as RADIUS, the crate relies on the blind rule. The probe is RadiusPacket::try_from(payload).is_ok(), run on the whole payload, as every probe but the four text ones since 11.7.0 (#185): the declared length is compared with the real datagram. It accepts a datagram only if:

  • the code is one of the fourteen of RFC 2865, 2866 and 5176;
  • the declared length is between 20 and 4096 and equals the datagram;
  • the attributes tile the rest exactly: none shorter than its 2-byte header, none past the end, no byte left over.

RFC 2865 §3 tells a receiver to treat the bytes after Length as padding and ignore them. The crate requires the length to be the datagram's instead, its reason being that a decoder tolerating trailing bytes would make a poor blind probe. With that, the same probe runs blind on any UDP datagram that no earlier rule has claimed, and there is no DecodeAsProtocol variant for RADIUS: a server on another port is recognized anyway. Both rules are Guard::Udp; RADIUS over TCP or TLS is not probed.

Since the two rules share the probe, a datagram on 1812 that fails it is not probed again by the blind rule: it goes on down the table, usually to "Unknown". A refusal never fills corrupted: on UDP a port is a hint, not an announcement, as for PTP. Every packet of the corpus has 1812 on one side; the blind route is checked by moving frame 1 of radius.pcap to other ports, and the test shows what the probe looks at and what it does not (tested):

#![allow(unused)]
fn main() {
    #[test]
    fn radius_is_recognized_off_its_ports_and_refused_when_altered() {
        // Frame 1 moved to UDP 40000 -> 40001: the blind rule recognizes it.
        let mut raw = hex::decode(RADIUS_ACCESS_REQUEST_HEX).unwrap();
        raw[34..38].copy_from_slice(&[0x9c, 0x40, 0x9c, 0x41]);
        let flow = parse(LinkType::ETHERNET, &raw).unwrap();
        assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "RADIUS");

        // The authenticator and the values are not checked: nothing here knows
        // the shared secret.
        let start = raw.len() - 139; // where the RADIUS header starts
        let mut values = raw.clone();
        values[start + 4] ^= 0xff; // first byte of the authenticator
        values[start + 22] ^= 0xff; // first byte of the NAS-IP-Address value
        let flow = parse(LinkType::ETHERNET, &values).unwrap();
        assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "RADIUS");

        // The structure is checked: an unknown code, a length that is not the
        // datagram's, attributes that no longer tile it (a NAS-IP-Address one
        // byte longer makes the next attribute start on a length byte).
        for (offset, value, error) in [
            (0, 6, RadiusParseError::UnknownCode(6)),
            (3, 0x8a, RadiusParseError::LengthMismatch { declared: 138, actual: 139 }),
            (21, 7, RadiusParseError::AttributeTooShort { attribute_type: 6, length: 0 }),
        ] {
            let mut altered = raw.clone();
            altered[start + offset] = value;
            assert_eq!(RadiusPacket::try_from(&altered[start..]), Err(error));
            let flow = parse(LinkType::ETHERNET, &altered).unwrap();
            assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "Unknown");
            assert!(flow.corrupted.is_none());
        }
    }
}

The packet

The Access-Request of frame 1 of radius.pcap, 32 bits per row: code 1, identifier 5, length 139, the 16-byte request authenticator, then the attributes as type-length-value triplets, NAS-IP-Address 10.0.0.1, NAS-Port 50012, NAS-Port-Type 15 and User-Name John.McGuirk shown, six more summarised; RadiusPacket keeps the header fields and the list of RadiusAttribute, each with its raw type, its named kind, its length and its value bytes.

#![allow(unused)]
fn main() {
pub struct RadiusPacket<'a> {
    pub code: u8,
    pub kind: RadiusCode,                      // always known: the decoder refuses other codes
    pub identifier: u8,                        // pairs a request and its response
    pub length: u16,                           // equal to the datagram
    pub authenticator: [u8; 16],               // as on the wire
    pub attributes: Vec<RadiusAttribute<'a>>,
}
}
BytesFieldNote
0code, kindone of the fourteen codes below
1identifier
2–3length20 to 4096 (RFC 2865 §3), header included
4–19authenticatorthe Request Authenticator, or a Response Authenticator computed with the shared secret: copied, not verified
20–attributestype, length, value, up to length
CodeRadiusCodeRFC
1AccessRequest2865
2AccessAccept2865
3AccessReject2865
4AccountingRequest2866
5AccountingResponse2866
11AccessChallenge2865
12StatusServer2865
13StatusClient2865
40DisconnectRequest5176
41DisconnectAck5176
42DisconnectNak5176
43CoaRequest5176
44CoaAck5176
45CoaNak5176

The structs and enums of the module are #[non_exhaustive], and RadiusCode::from_code is public. The attribute list is the only part that allocates: a Vec whose initial capacity is at most 16, and never more than the bytes left could hold; past 16 attributes it grows like any Vec. An Access-Request from a switch, decoded attribute by attribute (tested):

#![allow(unused)]
fn main() {
    #[test]
    fn radius_access_request_of_a_switch() {
        let raw = hex::decode(RADIUS_ACCESS_REQUEST_HEX).unwrap();
        let flow = parse(LinkType::ETHERNET, &raw).unwrap();
        assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "RADIUS");
        let transport = flow.transport.as_ref().unwrap();
        assert_eq!((transport.source_port, transport.destination_port), (Some(1645), Some(1812)));

        let packet = RadiusPacket::try_from(transport.payload.unwrap()).unwrap();
        assert_eq!(packet.kind, RadiusCode::AccessRequest);
        assert_eq!((packet.code, packet.identifier, packet.length), (1, 5, 139));
        assert_eq!(packet.authenticator[..4], [0xec, 0xfe, 0x3d, 0x2f]);

        // Ten attributes in wire order, each value borrowed from the datagram.
        let types: Vec<u8> = packet.attributes.iter().map(|a| a.attribute_type).collect();
        assert_eq!(types, [4, 5, 61, 1, 30, 31, 6, 12, 79, 80]);
        assert_eq!(packet.attributes[0].kind, Some(AttributeType::NasIpAddress));

        // The first attribute of a type, read through the accessor that fits.
        let nas = packet.attribute(AttributeType::NasIpAddress).and_then(RadiusAttribute::as_ipv4);
        assert_eq!(nas, Some(Ipv4Addr::new(10, 0, 0, 1)));
        let port = packet.attribute(AttributeType::NasPort).and_then(RadiusAttribute::as_u32);
        assert_eq!(port, Some(50012));
        let user = packet.attribute(AttributeType::UserName).and_then(RadiusAttribute::as_text);
        assert_eq!(user, Some("John.McGuirk"));
        let station = packet
            .attribute(AttributeType::CallingStationId)
            .and_then(RadiusAttribute::as_text);
        assert_eq!(station, Some("00-14-22-E9-54-5E"));
        let mtu = packet.attribute(AttributeType::FramedMtu).and_then(RadiusAttribute::as_u32);
        assert_eq!(mtu, Some(1500));

        // EAP and the Message-Authenticator stay bytes; an accessor that does
        // not fit the value answers None.
        let eap = packet.attribute(AttributeType::EapMessage).unwrap();
        assert_eq!((eap.length, eap.value.len()), (19, 17));
        assert_eq!(eap.as_u32(), None);
        let authenticator = packet.attribute(AttributeType::MessageAuthenticator).unwrap();
        assert_eq!(authenticator.value.len(), 16);
        assert_eq!(packet.attribute(AttributeType::VendorSpecific), None);
    }
}

The header alone is a valid packet: the Access-Reject of a capture taken on the loopback interface, read from a BSD loopback frame (tested):

#![allow(unused)]
fn main() {
    #[test]
    fn radius_access_reject_without_attributes_on_loopback() {
        // Captured on the loopback interface: LINKTYPE_NULL, then IPv4 and UDP.
        let raw = hex::decode(LOCALHOST_ACCESS_REJECT_HEX).unwrap();
        let flow = parse(LinkType::NULL, &raw).unwrap();
        assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "RADIUS");

        let payload = flow.transport.as_ref().unwrap().payload.unwrap();
        let packet = RadiusPacket::try_from(payload).unwrap();
        assert_eq!(packet.kind, RadiusCode::AccessReject);
        assert_eq!((packet.identifier, packet.length), (43, 20));
        assert!(packet.attributes.is_empty()); // the header alone is a packet
    }
}

Attributes

#![allow(unused)]
fn main() {
pub struct RadiusAttribute<'a> {
    pub attribute_type: u8,
    pub kind: Option<AttributeType>,   // None for a type the crate does not name
    pub length: u8,                    // declared, header included
    pub value: &'a [u8],               // length - 2 bytes, borrowed
}
}

AttributeType names every assigned type of the IANA registry RADIUS Attribute Types, 192 of them: 1 to 190 except 17, 21, 54 and 93, which are unassigned, and the extended types 241 to 246. Types 126 to 190 have been named since 11.7.0 (#192): Operator-Name and location (RFC 5580), management (RFC 5607), PKM (RFC 5904), DS-Lite (RFC 6519), PMIPv6 (RFC 6572), EAP-Lower-Layer (RFC 6677), GSS-EAP (RFC 7055), IPv6 (RFC 6911, RFC 6930) and IEEE 802 (RFC 7268); 11.6.0 named 127 types and returned None for them. Code 191, the experimental 192 to 223, the implementation-specific 224 to 240 and the reserved 247 to 255 are None: any such type is kept, with kind: None and its value. AttributeType::from_code is public.

The variants carry the registry's names, in CamelCase (173, IPv6-6rd-Configuration, is Ipv6SixRdConfiguration), with eight exceptions. 89, 101 and 241 to 246 carry the names of their RFC: ChargeableUserIdentity (CUI in the registry), ErrorCause ("Error-Cause Attribute"), ExtendedType1 to ExtendedType4, LongExtendedType1 and LongExtendedType2 (Extended-Attribute-1 to -6). 124 and 125 carried until 12.0.0 the names of two Diameter AVPs, MipHomeAgentAddress and MipHomeAgentHost, which do not exist in RADIUS: 124 is MIP6-Feature-Vector, a field of 64 flags, not an address, and 125 MIP6-Home-Link-Prefix, a prefix length and an IPv6 prefix, not a host name (RFC 5447). The registry's name of 89, 124, 125, 173 and 241 to 246 is a search alias in the rustdoc.

11.7.0 deprecated those two variants (#191) and gave the registry's names to two associated constants equal to them. 12.0.0 renames the variants in place, at the same position: they are AttributeType::Mip6FeatureVector and AttributeType::Mip6HomeLinkPrefix, the constants are gone, and code written with them compiles unchanged. from_code(124) and from_code(125) return the same values as before, and {:?} now prints the registry's names. The old names no longer compile.

No capture of the corpus carries a type above 95. A packet built by hand, with the two types of RFC 5447 and an Operator-Name (tested):

#![allow(unused)]
fn main() {
    #[test]
    fn radius_types_124_and_125_by_their_registry_names() {
        // An Access-Request built by hand: no capture of the corpus carries a
        // type above 95. The header, then four attributes.
        let mut bytes = vec![1, 7, 0, 0]; // code, identifier, length (set below)
        bytes.extend_from_slice(&[0; 16]); // request authenticator, not checked
        // 124, MIP6-Feature-Vector: 64 flags, here the lowest one.
        bytes.extend_from_slice(&[124, 10, 0, 0, 0, 0, 0, 0, 0, 1]);
        // 125, MIP6-Home-Link-Prefix: a prefix length, then 2001:db8:1::.
        bytes.extend_from_slice(&[125, 19, 64, 0x20, 0x01, 0x0d, 0xb8, 0, 1]);
        bytes.extend_from_slice(&[0; 10]);
        // 126, Operator-Name, named since 11.7.0.
        bytes.extend_from_slice(&[126, 14]);
        bytes.extend_from_slice(b"1example.net");
        // 192, the first of the experimental types.
        bytes.extend_from_slice(&[192, 3, 0xff]);
        let length = u16::try_from(bytes.len()).unwrap();
        bytes[2..4].copy_from_slice(&length.to_be_bytes());

        let packet = RadiusPacket::try_from(bytes.as_slice()).unwrap();
        assert_eq!((packet.kind, packet.length), (RadiusCode::AccessRequest, 66));

        // The constants are patterns, like the variants they stand for.
        let names: Vec<&str> = packet
            .attributes
            .iter()
            .map(|attribute| match attribute.kind {
                Some(AttributeType::Mip6FeatureVector) => "MIP6-Feature-Vector",
                Some(AttributeType::Mip6HomeLinkPrefix) => "MIP6-Home-Link-Prefix",
                Some(AttributeType::OperatorName) => "Operator-Name",
                Some(_) => "another named type",
                None => "not named",
            })
            .collect();
        assert_eq!(
            names,
            ["MIP6-Feature-Vector", "MIP6-Home-Link-Prefix", "Operator-Name", "not named"]
        );

        // They are arguments too. The values stay bytes, read by the caller.
        let features = packet.attribute(AttributeType::Mip6FeatureVector).unwrap();
        assert_eq!(u64::from_be_bytes(features.value.try_into().unwrap()), 1);
        let home = packet.attribute(AttributeType::Mip6HomeLinkPrefix).unwrap();
        let prefix: [u8; 16] = home.value[1..].try_into().unwrap();
        assert_eq!((home.value[0], Ipv6Addr::from(prefix)), (64, "2001:db8:1::".parse().unwrap()));
        let operator = packet.attribute(AttributeType::OperatorName);
        assert_eq!(operator.and_then(RadiusAttribute::as_text), Some("1example.net"));

        // from_code(124) is the same value as in 11.6.0; since 12.0.0 the
        // variant carries the registry's name, and {:?} prints it.
        assert_eq!(AttributeType::from_code(124), Some(AttributeType::Mip6FeatureVector));
        assert_eq!(format!("{:?}", AttributeType::Mip6FeatureVector), "Mip6FeatureVector");
        assert_eq!(format!("{:?}", AttributeType::Mip6HomeLinkPrefix), "Mip6HomeLinkPrefix");

        // Every assigned type is named: 1 to 190 but four, and 241 to 246.
        let named = (0..=255).filter(|&code| AttributeType::from_code(code).is_some());
        assert_eq!(named.count(), 192);
        for code in [17, 21, 54, 93, 191, 192, 223, 224, 240, 247, 255] {
            assert_eq!(AttributeType::from_code(code), None);
        }
        assert_eq!(AttributeType::from_code(190), Some(AttributeType::WlanRfBand));
    }
}

Four accessors read a value:

MethodReturnsSome when
as_u32()Option<u32>the value is 4 bytes: NAS-Port, Service-Type, Acct-Status-Type…
as_ipv4()Option<Ipv4Addr>the value is 4 bytes: NAS-IP-Address, Framed-IP-Address…
as_text()Option<&'a str>the value is UTF-8: User-Name, NAS-Identifier, Reply-Message…
vendor()Option<(u32, &'a [u8])>the attribute is a Vendor-Specific (26) whose value has at least 4 bytes: the vendor id, an IANA Private Enterprise Number, then the bytes the vendor defines

The first three do not look at the type: as_u32 reads any 4-byte value, and choosing the accessor the RFC gives an attribute is the caller's job. RadiusPacket::attribute(kind) returns the first attribute of a type. An attribute holds at most 253 bytes of value, its length being one byte, so a longer EAP packet is spread over several EAP-Message attributes, which the caller reads by walking attributes: frame 36722 of the Ultimate PCAP, an Access-Challenge of 1 068 bytes, carries four of them, 255, 255, 255 and 247 bytes long with their 2-byte headers. An Access-Accept over IPv6 with three Microsoft attributes (tested):

#![allow(unused)]
fn main() {
    #[test]
    fn radius_access_accept_with_microsoft_vendor_attributes() {
        // Over IPv6 this time: the rule only looks at UDP.
        let raw = hex::decode(ULTIMATE_ACCESS_ACCEPT_HEX).unwrap();
        let flow = parse(LinkType::ETHERNET, &raw).unwrap();
        assert_eq!(flow.internet.as_ref().unwrap().protocol_name, "IPv6");
        assert_eq!(flow.application.as_ref().unwrap().protocol.label(), "RADIUS");

        let payload = flow.transport.as_ref().unwrap().payload.unwrap();
        let packet = RadiusPacket::try_from(payload).unwrap();
        assert_eq!(packet.kind, RadiusCode::AccessAccept);
        assert_eq!((packet.identifier, packet.length), (22, 96)); // answers request 22
        let reply = packet.attribute(AttributeType::ReplyMessage);
        assert_eq!(reply.and_then(RadiusAttribute::as_text), Some("Hello, bob"));

        // Three Vendor-Specific attributes: the vendor id, then the bytes the
        // vendor defines, which the crate does not decode.
        let vendors: Vec<(u32, &[u8])> =
            packet.attributes.iter().filter_map(RadiusAttribute::vendor).collect();
        assert_eq!(vendors.len(), 3);
        assert!(vendors.iter().all(|(id, _)| *id == 311)); // Microsoft
        // Vendor type 7, length 6, value 1: MS-MPPE-Encryption-Policy, for tshark.
        assert_eq!(vendors[1].1, &[7, 6, 0, 0, 0, 1]);
        // vendor() answers only on a Vendor-Specific attribute.
        assert_eq!(packet.attributes[0].kind, Some(AttributeType::ReplyMessage));
        assert_eq!(packet.attributes[0].vendor(), None);
    }
}

Validations

All of them are RadiusParseError, in packet_parser::errors::application::radius:

ErrorWhen
Truncated { expected, actual }fewer than the 20 bytes of the header
UnknownCode(u8)a code outside the fourteen above
InvalidLength(u16)a declared length below 20 or above 4096
LengthMismatch { declared, actual }a declared length other than the datagram's
AttributeTooShort { attribute_type, length }an attribute length of 0 or 1, shorter than its own header
AttributeOverflow { attribute_type, needed, available }an attribute that runs past the end, or a single byte left after the last attribute

In the pipeline, every one of them means the same thing: the label is not given, as the detection section shows.

Oracle: 95 packets, attribute by attribute

The golden test tests/radius_golden.rs runs the two captures of pcaps_exemple/protocols/radius/ and The-Ultimate-PCAP.pcapng, read in place, and compares every packet with tshark 4.6.6, frozen in tests/data/radius_tshark_oracle.tsv by tools/radius_oracle.sh: ports, code, identifier, length, authenticator, the types and lengths of the attributes, the vendor ids of the Vendor-Specific.

CapturePacketsContentOrigin
protocols/radius/radius.pcap4a Cisco switch, from port 1645, authenticates John.McGuirk in EAP: Access-Request, Access-Challenge, Access-Request, Access-AcceptZeek test corpus, BSD 3-clause
protocols/radius/radius_localhost.pcap19loopback traffic, 127.0.0.1 to 127.0.0.1, LINKTYPE_NULL (a classic pcap does not record the interface): a client that tries EAP, PAP and CHAP from seven ephemeral ports, one per authentication; eleven Access-Requests to port 1812 carrying EAP-Message, User-Password or CHAP-Password, and their two Access-Challenge, four Access-Accept and two Access-RejectZeek test corpus, BSD 3-clause
The-Ultimate-PCAP.pcapng72 of 51 328 framesDecember 2022, a switch and VMware machines over IPv4 and IPv6: EAP-PEAP and EAP-TTLS exchanges, Microsoft Vendor-Specific attributesJohannes Weber, The Ultimate PCAP

Over the 95: 49 Access-Request, 26 Access-Challenge, 18 Access-Accept and 2 Access-Reject, carrying 28 Vendor-Specific attributes, all of Microsoft (311). None of the other 51 256 frames of the Ultimate PCAP is labelled RADIUS. A second test moves frame 1 of radius.pcap to ports 40000 and 40001, then alters the code (to 6, outside the fourteen), the length and the length of the first attribute (twice): each is refused and loses the label, while an altered authenticator and an altered attribute value keep it.

A run of the 11.6.0 pipeline over the 386 403 frames that the crate's test reader gets from the captures tracked under pcaps_exemple/ gives the label "RADIUS" to exactly the 265 packets tshark dissects as RADIUS: these 95, and 170 more on port 1812 that the golden test does not cover, in vlan0--packet-capture CAPWAP…cap, a 2016 test-network capture at the root of pcaps_exemple/ whose origin is not documented. The 170 and the 72 of the Ultimate PCAP are the 242 RADIUS packets that, according to the CHANGELOG, leave "Unknown" on the 11.5.0 corpus. The fuzz target parse_radius runs nightly, on its input and on mutations of three real packets: whatever it decodes must have the input's length, between 20 and 4096, and attributes that tile it exactly, each value length - 2 bytes inside the input, vendor() answering only on a Vendor-Specific.

What is left out

The limits every protocol shares, reassembly and checksums among them, are on the Limits page. The ones below are this protocol's own.

  • The shared secret: neither authenticator nor the Message-Authenticator is verified, and User-Password, Tunnel-Password or the keys a vendor attribute carries stay the bytes of the wire.
  • EAP is not decoded: an EAP-Message value is raw bytes, and an EAP packet spread over several attributes is not reassembled.
  • Vendor attributes: vendor() gives the vendor id and the vendor's bytes; their sub-attributes are not split.
  • Other value types: no accessor for IPv6 addresses (NAS-IPv6-Address, in the Ultimate PCAP), and the extended types 241 to 246 are named but not unpacked.
  • No state: a response is not matched with its request, and the three identical Access-Requests of radius_localhost.pcap, a retransmission, are three packets. One datagram at a time, as in the rest of the crate.
  • Codes no capture exercises: none of the 265 RADIUS packets of the repository's captures is an accounting, Status-Server, Status-Client, Disconnect or CoA packet; codes 4, 5, 12, 13 and 40 to 45 follow the RFCs.