JFLib

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 str the name of a packet type as capture tools show it

  • fn lsa_name(t: u8) -> &'static str the name of an LSA type

  • fn options_text(o: u8) -> String the set option bits: "E, MC" ("none" without any)

  • fn dd_flags_text(f: u8) -> String the flags of a Database Description packet: "I, M, MS" ("none")

  • fn area_text(a: u32) -> String an area ID as dissectors show it: "0.0.0.0 (Backbone)", "0.0.0.1"

  • fn lsa_checksum(lsa: &[u8]) -> u16 the 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 255

  • fn lsa_checksum_ok(lsa: &[u8]) -> bool an 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 correct

  • fn set_age(lsa: &mut [u8], age: u16) writes a new LS age into an LSA (the checksum does not cover it)

  • struct LsaKey what identifies an LSA: its type, Link State ID and advertising router (12.1)

    • kind: u8
    • id: Ipv4
    • adv: Ipv4
  • struct LsaHeader the 20-byte header of an LSA

    • age: u16 -- seconds since it was originated
    • options: u8
    • kind: u8
    • id: Ipv4
    • adv: Ipv4
    • seq: u32 -- signed: INITIAL_SEQ (0x80000001) is the smallest one used
    • checksum: u16
    • len: u16 -- the whole LSA with this header

impl LsaHeader

  • fn read(b: &[u8]) -> LsaHeader a 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 bytes

  • fn key(&self) -> LsaKey

  • fn compare(a: &LsaHeader, b: &LsaHeader) -> Ordering which of two instances of the same LSA is more recent (RFC 2328 13.1): Greater when a is, Equal when they are the same instance

  • struct RouterLsa the body of a router-LSA

    • flags: u8 -- RTR_V, RTR_E, RTR_B
    • links: Vec<RouterLink>
  • struct NetworkLsa the body of a network-LSA (A.4.3)

    • mask: Ipv4
    • routers: Vec<Ipv4> -- the routers fully adjacent to the DR, the DR itself included
  • struct SummaryLsa the body of a summary-LSA of type 3 or 4 (A.4.4; type 4 has the mask 0)

    • mask: Ipv4
    • metric: u32 -- 24 bits; LS_INFINITY: unreachable
    • tos: Vec<(u8, u32)>
  • struct ExternalLsa the TOS 0 part of an AS-external-LSA and its further TOS entries (A.4.5)

    • mask: Ipv4
    • e2: bool -- the E bit: a type 2 metric (larger than any path inside the AS)
    • metric: u32 -- 24 bits
    • fwd: Ipv4 -- where traffic for the destination should go (0.0.0.0: to the AS boundary router)
    • tag: u32
    • tos: Vec<(u8, u32, Ipv4, u32)> -- further TOS entries: (E bit and TOS, metric, forwarding address, tag)
  • enum LsaBody what an LSA says

    • Router(RouterLsa)
    • Network(NetworkLsa)
    • Summary(SummaryLsa)
    • External(ExternalLsa)
    • Other(Vec<u8>) -- an LSA of another type: its bytes after the header
  • enum LsaErr why an LSA cannot be read

    • Short -- fewer bytes than its header or its length field
    • Length -- the length field does not fit what the body needs

impl LsaErr

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

  • struct Lsa an LSA as a record

    • hdr: LsaHeader
    • body: LsaBody

impl Lsa

  • fn new(kind: u8, id: Ipv4, adv: Ipv4, seq: u32, options: u8, body: LsaBody) -> Lsa a new instance: age 0; to_bytes fills in length and checksum

  • fn key(&self) -> LsaKey

  • fn 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 Header the common header (A.3.1)

    • kind: u8
    • router: Ipv4 -- the sender's router ID
    • area: u32
    • autype: u16
    • auth: [u8; 8] -- the authentication field: zeros (AU_NULL), the password (AU_SIMPLE), key ID, length and sequence number (AU_CRYPTO)
  • struct Hello a Hello packet (A.3.2)

    • mask: Ipv4
    • interval: u16 -- HelloInterval in seconds
    • options: u8
    • priority: u8
    • dead: u32 -- RouterDeadInterval in seconds
    • dr: Ipv4 -- the interface addresses of the DR and the BDR as the sender sees them (0.0.0.0: none)
    • bdr: Ipv4
    • neighbors: Vec<Ipv4> -- the router IDs it heard Hellos from within RouterDeadInterval
  • struct Dd a Database Description packet (A.3.3)

    • mtu: u16 -- the largest IP datagram the interface sends without fragmentation
    • options: u8
    • flags: u8 -- DD_I, DD_M, DD_MS
    • seq: u32
    • lsas: Vec<LsaHeader>
  • enum Body the body of a packet

    • Hello(Hello)
    • Dd(Dd)
    • Lsr(Vec<LsaKey>)
    • Lsu(Vec<Vec<u8>>) -- whole LSAs (each as long as its length field)
    • Ack(Vec<LsaHeader>)
  • enum OspfErr why a packet cannot be read

    • Version(u8) -- not version 2
    • Kind(u8) -- not one of the five packet types
    • Body -- the body is shorter than its fixed part, or its entries do not fill it
    • Lsa -- an LSA of an update is cut or longer than the packet

impl OspfErr

  • fn text(self) -> String

  • struct Packet an OSPF packet as a record

    • hdr: Header
    • body: 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) -> u8
  • fn kind(&self) -> u8
  • fn len(&self) -> usize
  • fn is_empty(&self) -> bool
  • fn router(&self) -> Ipv4
  • fn area(&self) -> u32
  • fn checksum(&self) -> u16
  • fn autype(&self) -> u16
  • fn auth(&self) -> [u8; 8]
  • fn bytes(&self) -> &'a [u8]
  • fn body(&self) -> &'a [u8]
  • fn computed_checksum(&self) -> u16 the checksum the packet should carry
  • fn checksum_ok(&self) -> bool the checksum is right (with cryptographic authentication there is none: always true)
  • fn header(&self) -> Header
  • fn 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) -> u8
  • fn read(v: &OspfView) -> Result<Packet, OspfErr> reads a packet; the checksum and the authentication are for the receiver to judge
  • fn to_bytes(&self) -> Vec<u8> the packet as bytes: version 2, the type of the body, the length, the checksum (none with AU_CRYPTO)

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