netpkt/ospf.rs#
Source: lib/netpkt/ospf.rs
ospf -- OSPF version 2 packets and LSAs byte for byte (RFC 2328 appendix A): the 24-byte
header with its checksum and authentication field, the five packet types, LSA headers with
the Fletcher checksum of RFC 2328 12.1.7 (RFC 905 annex B), router-, network-, summary- and
AS-external-LSAs, and how two instances of an LSA compare (13.1)
OspfView::parse(b) version, kind, length, router ID, area, checksum, authentication
type and field, body(); checksum_ok() (the IP checksum of the
packet without the authentication field, A.3.1); None when the
bytes are shorter than the header or than its length field
Header { kind, router, area, autype, auth } the common header
Packet { hdr, body } read(&view) (Err with the reason: OspfErr::text) / to_bytes()
(length and checksum filled in; with AU_CRYPTO the checksum stays
0 and no digest is appended)
Body::Hello(Hello) network mask, HelloInterval, options, priority, RouterDeadInterval,
DR, BDR, the neighbours seen
Dd(Dd) interface MTU, options, the I / M / MS flags, DD sequence number,
LSA headers
Lsr(Vec<LsaKey>) the LSAs asked for (type, Link State ID, advertising router)
Lsu(Vec<Vec<u8>>) whole LSAs as they travel (flooding forwards them byte for byte)
Ack(Vec<LsaHeader>) the LSA headers acknowledged
LsaHeader age, options, type, Link State ID, advertising router, sequence
number, checksum, length; read / write / of(lsa) / key()
Lsa { hdr, body } parse(lsa) (Err: LsaErr) / to_bytes() (length and Fletcher
checksum filled in); LsaBody::Router (flags V / E / B and links
with metrics), Network (mask, attached routers), Summary (types 3
and 4: mask and metric), External (mask, type 1 or 2, metric,
forwarding address, route tag), Other (bytes of an unknown type)
lsa_checksum(lsa) / lsa_checksum_ok(lsa) the Fletcher checksum over the LSA without its
age, C0 and C1 both zero for a correct one
set_age(lsa, age) a new LS age (outside the checksum)
compare(a, b) which instance is more recent: sequence number (signed), checksum,
MaxAge, an age difference beyond MaxAgeDiff (13.1)
type_name, lsa_name, options_text, dd_flags_text, area_text names for dissectors
Constants: VERSION, the packet types T_*, ALL_SPF_ROUTERS (224.0.0.5), ALL_D_ROUTERS
(224.0.0.6), authentication types AU_*, option bits OPT_*, the DD flags DD_I, DD_M, DD_MS,
LSA types LSA_*, router-LSA link types LINK_* and flags RTR_B / RTR_E / RTR_V, the
architectural constants of appendix B (MAX_AGE, LS_REFRESH_TIME, MIN_LS_INTERVAL, ...),
INITIAL_SEQ / MAX_SEQ and LS_INFINITY.
The codec reads and writes; what a receiver refuses because of its own state (mismatched
timers, the area, the neighbours) is the protocol's business. TOS metrics other than TOS 0
are read and written but carry no meaning (RFC 2328 dropped TOS routing).
/// OSPF version 2
VERSION: u8 = 2
/// the common header, an LSA header, an entry of a Link State Request
HDR_LEN: usize = 24
LSA_HDR_LEN: usize = 20
LSR_ENTRY_LEN: usize = 12
/// the fixed part of a Hello and of a Database Description packet
HELLO_LEN: usize = 20
DD_LEN: usize = 8
/// packet types
T_HELLO: u8 = 1
T_DD: u8 = 2
T_LSR: u8 = 3
T_LSU: u8 = 4
T_ACK: u8 = 5
/// every OSPF router listens to AllSPFRouters, the DR and the BDR to AllDRouters (A.1)
ALL_SPF_ROUTERS: Ipv4 = Ipv4([224, 0, 0, 5])
ALL_D_ROUTERS: Ipv4 = Ipv4([224, 0, 0, 6])
/// authentication types (appendix D)
AU_NULL: u16 = 0
AU_SIMPLE: u16 = 1
AU_CRYPTO: u16 = 2
/// option bits (A.2; DN and O came later: RFC 4576, RFC 5250)
OPT_E: u8 = 0x02
OPT_MC: u8 = 0x04
OPT_NP: u8 = 0x08
OPT_EA: u8 = 0x10
OPT_DC: u8 = 0x20
OPT_O: u8 = 0x40
OPT_DN: u8 = 0x80
/// the flags of a Database Description packet: Init, More, Master/Slave
DD_I: u8 = 0x04
DD_M: u8 = 0x02
DD_MS: u8 = 0x01
/// LSA types
LSA_ROUTER: u8 = 1
LSA_NETWORK: u8 = 2
LSA_SUMMARY: u8 = 3
LSA_ASBR: u8 = 4
LSA_EXTERNAL: u8 = 5
/// the links of a router-LSA
LINK_P2P: u8 = 1
LINK_TRANSIT: u8 = 2
LINK_STUB: u8 = 3
LINK_VIRTUAL: u8 = 4
/// the flags of a router-LSA: area border router, AS boundary router, virtual link endpoint
RTR_B: u8 = 0x01
RTR_E: u8 = 0x02
RTR_V: u8 = 0x04
/// architectural constants (appendix B), in seconds
LS_REFRESH_TIME: u16 = 1800
MIN_LS_INTERVAL: u16 = 5
MIN_LS_ARRIVAL: u16 = 1
MAX_AGE: u16 = 3600
CHECK_AGE: u16 = 300
MAX_AGE_DIFF: u16 = 900
/// the metric of an unreachable destination in summary- and AS-external-LSAs
LS_INFINITY: u32 = 0xFF_FFFF
/// LS sequence numbers are signed: the first one, the largest one (12.1.6)
INITIAL_SEQ: u32 = 0x8000_0001
MAX_SEQ: u32 = 0x7FFF_FFFF
fn type_name(t: u8) -> &'static strthe name of a packet type as capture tools show itfn lsa_name(t: u8) -> &'static strthe name of an LSA typefn options_text(o: u8) -> Stringthe set option bits: "E, MC" ("none" without any)fn dd_flags_text(f: u8) -> Stringthe flags of a Database Description packet: "I, M, MS" ("none")fn area_text(a: u32) -> Stringan area ID as dissectors show it: "0.0.0.0 (Backbone)", "0.0.0.1"fn lsa_checksum(lsa: &[u8]) -> u16the Fletcher checksum of an LSA (RFC 2328 12.1.7): over the bytes from the options field to the end of the LSA (the LS age left out), the checksum field taken as zero; the two check bytes X and Y of RFC 905 B.4 that make C0 and C1 zero, a zero byte written as 255fn lsa_checksum_ok(lsa: &[u8]) -> boolan LSA's checksum is correct: C0 and C1 over the bytes after the age are both zero (RFC 905 B.2); a checksum of 0 is never correctfn set_age(lsa: &mut [u8], age: u16)writes a new LS age into an LSA (the checksum does not cover it)struct LsaKeywhat identifies an LSA: its type, Link State ID and advertising router (12.1)kind: u8id: Ipv4adv: Ipv4
struct LsaHeaderthe 20-byte header of an LSAage: u16-- seconds since it was originatedoptions: u8kind: u8id: Ipv4adv: Ipv4seq: u32-- signed: INITIAL_SEQ (0x80000001) is the smallest one usedchecksum: u16len: u16-- the whole LSA with this header
impl LsaHeader
fn read(b: &[u8]) -> LsaHeadera header from its 20 bytes (the caller has checked the length)fn of(lsa: &[u8]) -> Option<LsaHeader>the header of an LSA (None when it is shorter than a header)fn write(&self, b: &mut [u8])writes the header into 20 bytesfn key(&self) -> LsaKeyfn compare(a: &LsaHeader, b: &LsaHeader) -> Orderingwhich of two instances of the same LSA is more recent (RFC 2328 13.1): Greater whenais, Equal when they are the same instancestruct RouterLinkone link of a router-LSA (A.4.2)id: Ipv4-- LINK_P2P: the neighbour's router ID; LINK_TRANSIT: the DR's interface address; LINK_STUB: the networkdata: Ipv4-- the router's interface address (LINK_P2P, LINK_TRANSIT; an interface index when unnumbered) or the network mask (LINK_STUB)kind: u8metric: u16-- the TOS 0 metrictos: Vec<(u8, u16)>-- further TOS metrics: (TOS, metric)
struct RouterLsathe body of a router-LSAflags: u8-- RTR_V, RTR_E, RTR_Blinks: Vec<RouterLink>
struct NetworkLsathe body of a network-LSA (A.4.3)mask: Ipv4routers: Vec<Ipv4>-- the routers fully adjacent to the DR, the DR itself included
struct SummaryLsathe body of a summary-LSA of type 3 or 4 (A.4.4; type 4 has the mask 0)mask: Ipv4metric: u32-- 24 bits; LS_INFINITY: unreachabletos: Vec<(u8, u32)>
struct ExternalLsathe TOS 0 part of an AS-external-LSA and its further TOS entries (A.4.5)mask: Ipv4e2: bool-- the E bit: a type 2 metric (larger than any path inside the AS)metric: u32-- 24 bitsfwd: Ipv4-- where traffic for the destination should go (0.0.0.0: to the AS boundary router)tag: u32tos: Vec<(u8, u32, Ipv4, u32)>-- further TOS entries: (E bit and TOS, metric, forwarding address, tag)
enum LsaBodywhat an LSA saysRouter(RouterLsa)Network(NetworkLsa)Summary(SummaryLsa)External(ExternalLsa)Other(Vec<u8>)-- an LSA of another type: its bytes after the header
enum LsaErrwhy an LSA cannot be readShort-- fewer bytes than its header or its length fieldLength-- the length field does not fit what the body needs
impl LsaErr
fn text(self) -> &'static strstruct Lsaan LSA as a recordhdr: LsaHeaderbody: LsaBody
impl Lsa
fn new(kind: u8, id: Ipv4, adv: Ipv4, seq: u32, options: u8, body: LsaBody) -> Lsaa new instance: age 0; to_bytes fills in length and checksumfn key(&self) -> LsaKeyfn parse(b: &[u8]) -> Result<Lsa, LsaErr>reads an LSA (the bytes may go on after it: its length field says where it ends)fn to_bytes(&self) -> Vec<u8>the LSA as bytes: the header with the length and the Fletcher checksum filled in (the record's age stays as it is)struct Headerthe common header (A.3.1)kind: u8router: Ipv4-- the sender's router IDarea: u32autype: u16auth: [u8; 8]-- the authentication field: zeros (AU_NULL), the password (AU_SIMPLE), key ID, length and sequence number (AU_CRYPTO)
struct Helloa Hello packet (A.3.2)mask: Ipv4interval: u16-- HelloInterval in secondsoptions: u8priority: u8dead: u32-- RouterDeadInterval in secondsdr: Ipv4-- the interface addresses of the DR and the BDR as the sender sees them (0.0.0.0: none)bdr: Ipv4neighbors: Vec<Ipv4>-- the router IDs it heard Hellos from within RouterDeadInterval
struct Dda Database Description packet (A.3.3)mtu: u16-- the largest IP datagram the interface sends without fragmentationoptions: u8flags: u8-- DD_I, DD_M, DD_MSseq: u32lsas: Vec<LsaHeader>
enum Bodythe body of a packetHello(Hello)Dd(Dd)Lsr(Vec<LsaKey>)Lsu(Vec<Vec<u8>>)-- whole LSAs (each as long as its length field)Ack(Vec<LsaHeader>)
enum OspfErrwhy a packet cannot be readVersion(u8)-- not version 2Kind(u8)-- not one of the five packet typesBody-- the body is shorter than its fixed part, or its entries do not fill itLsa-- an LSA of an update is cut or longer than the packet
impl OspfErr
fn text(self) -> Stringstruct Packetan OSPF packet as a recordhdr: Headerbody: Body
struct OspfView<'a>a received OSPF packet (the bytes are cut to its length field)
impl<'a> OspfView<'a>
fn parse(b: &'a [u8]) -> Option<OspfView<'a>>at least the header, and as many bytes as its length field says (more are ignored)fn version(&self) -> u8fn kind(&self) -> u8fn len(&self) -> usizefn is_empty(&self) -> boolfn router(&self) -> Ipv4fn area(&self) -> u32fn checksum(&self) -> u16fn autype(&self) -> u16fn auth(&self) -> [u8; 8]fn bytes(&self) -> &'a [u8]fn body(&self) -> &'a [u8]fn computed_checksum(&self) -> u16the checksum the packet should carryfn checksum_ok(&self) -> boolthe checksum is right (with cryptographic authentication there is none: always true)fn header(&self) -> Headerfn lsu_spans(&self) -> Option<Vec<(usize, usize)>>where the LSAs of an update start and end within the packet: (offset, length); None when one does not fit
impl Packet
fn kind(&self) -> u8fn read(v: &OspfView) -> Result<Packet, OspfErr>reads a packet; the checksum and the authentication are for the receiver to judgefn to_bytes(&self) -> Vec<u8>the packet as bytes: version 2, the type of the body, the length, the checksum (none with AU_CRYPTO)