1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
use crate::parser::{parse_ospf_external_tos_routes, parse_ospf_tos_routes, parse_ospf_vec_u32};
use nom::number::streaming::be_u24;
use nom_derive::*;
use rusticata_macros::newtype_enum;
use std::net::Ipv4Addr;

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, NomBE)]
pub struct OspfPacketType(pub u8);

newtype_enum! {
impl display OspfPacketType {
    Hello = 1,
    DatabaseDescription = 2,
    LinkStateRequest = 3,
    LinkStateUpdate = 4,
    LinkStateAcknowledgment = 5,
}
}

/// An OSPF version 2 packet
#[derive(Debug)]
pub enum Ospfv2Packet {
    Hello(OspfHelloPacket),
    DatabaseDescription(OspfDatabaseDescriptionPacket),
    LinkStateRequest(OspfLinkStateRequestPacket),
    LinkStateUpdate(OspfLinkStateUpdatePacket),
    LinkStateAcknowledgment(OspfLinkStateAcknowledgmentPacket),
}

/// The OSPF packet header
///
/// Every OSPF packet starts with a common 24 byte header.  This header
/// contains all the necessary information to determine whether the
/// packet should be accepted for further processing.  This
/// determination is described in Section 8.2 of the specification.
#[derive(Debug, NomBE)]
pub struct Ospfv2PacketHeader {
    #[nom(Verify = "*version == 2")]
    pub version: u8,
    pub packet_type: OspfPacketType,
    pub packet_length: u16,
    pub router_id: u32,
    pub area_id: u32,
    pub checksum: u16,
    pub au_type: u16,
    pub authentication: u64,
}

impl Ospfv2PacketHeader {
    pub fn source_router(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.router_id)
    }
}

/// The Hello packet
///
/// Hello packets are OSPF packet type 1.  These packets are sent
/// periodically on all interfaces (including virtual links) in order to
/// establish and maintain neighbor relationships.  In addition, Hello
/// Packets are multicast on those physical networks having a multicast
/// or broadcast capability, enabling dynamic discovery of neighboring
/// routers.
///
/// All routers connected to a common network must agree on certain
/// parameters (Network mask, HelloInterval and RouterDeadInterval).
/// These parameters are included in Hello packets, so that differences
/// can inhibit the forming of neighbor relationships. A detailed
/// explanation of the receive processing for Hello packets is presented
/// in Section 10.5.  The sending of Hello packets is covered in Section
/// 9.5.
#[derive(Debug, NomBE)]
pub struct OspfHelloPacket {
    #[nom(Verify = "header.packet_type == OspfPacketType::Hello")]
    pub header: Ospfv2PacketHeader,
    pub network_mask: u32,
    pub hello_interval: u16,
    pub options: u8,
    pub router_priority: u8,
    pub router_dead_interval: u32,
    pub designated_router: u32,
    pub backup_designated_router: u32,
    // limit parsing to (length-xxx) bytes
    #[nom(Parse = "parse_ospf_vec_u32(header.packet_length, 44)")]
    pub neighbor_list: Vec<u32>,
}

impl OspfHelloPacket {
    pub fn network_mask(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.network_mask)
    }

    pub fn designated_router(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.designated_router)
    }

    pub fn backup_designated_router(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.backup_designated_router)
    }
}

/// The Database Description packet
///
/// Database Description packets are OSPF packet type 2.  These packets
/// are exchanged when an adjacency is being initialized.  They describe
/// the contents of the topological database.  Multiple packets may be
/// used to describe the database.  For this purpose a poll-response
/// procedure is used.  One of the routers is designated to be master,
/// the other a slave.  The master sends Database Description packets
/// (polls) which are acknowledged by Database Description packets sent
/// by the slave (responses).  The responses are linked to the polls via
/// the packets' DD sequence numbers.
#[derive(Debug, NomBE)]
pub struct OspfDatabaseDescriptionPacket {
    #[nom(Verify = "header.packet_type == OspfPacketType::DatabaseDescription")]
    pub header: Ospfv2PacketHeader,
    pub if_mtu: u16,
    pub options: u8,
    pub flags: u8,
    pub dd_sequence_number: u32,
    pub lsa_headers: Vec<OspfLinkStateAdvertisementHeader>,
}

/// The Link State Request packet
///
/// Link State Request packets are OSPF packet type 3.  After exchanging
/// Database Description packets with a neighboring router, a router may
/// find that parts of its topological database are out of date.  The
/// Link State Request packet is used to request the pieces of the
/// neighbor's database that are more up to date.  Multiple Link State
/// Request packets may need to be used.  The sending of Link State
/// Request packets is the last step in bringing up an adjacency.
///
/// A router that sends a Link State Request packet has in mind the
/// precise instance of the database pieces it is requesting, defined by
/// LS sequence number, LS checksum, and LS age, although these fields
/// are not specified in the Link State Request Packet itself.  The
/// router may receive even more recent instances in response.
///
/// The sending of Link State Request packets is documented in Section
/// 10.9.  The reception of Link State Request packets is documented in
/// Section 10.7.
#[derive(Debug, NomBE)]
pub struct OspfLinkStateRequestPacket {
    #[nom(Verify = "header.packet_type == OspfPacketType::LinkStateRequest")]
    pub header: Ospfv2PacketHeader,
    pub requests: Vec<OspfLinkStateRequest>,
}

#[derive(Debug, NomBE)]
pub struct OspfLinkStateRequest {
    // XXX should be a OspfLinkStateType, but it is only an u8
    pub link_state_type: u32,
    pub link_state_id: u32,
    pub advertising_router: u32,
}

impl OspfLinkStateRequest {
    pub fn link_state_id(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.link_state_id)
    }

    pub fn advertising_router(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.advertising_router)
    }
}

/// The Link State Update packet
///
/// Link State Update packets are OSPF packet type 4.  These packets
/// implement the flooding of link state advertisements.  Each Link
/// State Update packet carries a collection of link state
/// advertisements one hop further from its origin.  Several link state
/// advertisements may be included in a single packet.
///
/// Link State Update packets are multicast on those physical networks
/// that support multicast/broadcast.  In order to make the flooding
/// procedure reliable, flooded advertisements are acknowledged in Link
/// State Acknowledgment packets.  If retransmission of certain
/// advertisements is necessary, the retransmitted advertisements are
/// always carried by unicast Link State Update packets.  For more
/// information on the reliable flooding of link state advertisements,
/// consult Section 13.
#[derive(Debug, NomBE)]
pub struct OspfLinkStateUpdatePacket {
    #[nom(Verify = "header.packet_type == OspfPacketType::LinkStateUpdate")]
    pub header: Ospfv2PacketHeader,
    pub num_advertisements: u32,
    #[nom(Count = "num_advertisements")]
    pub lsa: Vec<OspfLinkStateAdvertisement>,
}

/// The Link State Acknowledgment packet
///
/// Link State Acknowledgment Packets are OSPF packet type 5.  To make
/// the flooding of link state advertisements reliable, flooded
/// advertisements are explicitly acknowledged.  This acknowledgment is
/// accomplished through the sending and receiving of Link State
/// Acknowledgment packets.  Multiple link state advertisements can be
/// acknowledged in a single Link State Acknowledgment packet.
///
/// Depending on the state of the sending interface and the source of
/// the advertisements being acknowledged, a Link State Acknowledgment
/// packet is sent either to the multicast address AllSPFRouters, to the
/// multicast address AllDRouters, or as a unicast.  The sending of Link
/// State Acknowledgement packets is documented in Section 13.5.  The
/// reception of Link State Acknowledgement packets is documented in
/// Section 13.7.
///
/// The format of this packet is similar to that of the Data Description
/// packet.  The body of both packets is simply a list of link state
/// advertisement headers.
#[derive(Debug, NomBE)]
pub struct OspfLinkStateAcknowledgmentPacket {
    #[nom(Verify = "header.packet_type == OspfPacketType::LinkStateAcknowledgment")]
    pub header: Ospfv2PacketHeader,
    pub lsa_headers: Vec<OspfLinkStateAdvertisementHeader>,
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, NomBE)]
pub struct OspfLinkStateType(pub u8);

newtype_enum! {
impl display OspfLinkStateType {
    RouterLinks = 1,
    NetworkLinks = 2,
    SummaryLinkIpNetwork = 3,
    SummaryLinkAsbr = 4,
    ASExternalLink = 5,
    NSSAASExternal = 7,
    OpaqueLinkLocalScope = 9,
    OpaqueAreaLocalScope = 10,
    OpaqueASWideScope = 11,
}
}

/// The Link State Advertisement header
///
/// All link state advertisements begin with a common 20 byte header.
/// This header contains enough information to uniquely identify the
/// advertisement (LS type, Link State ID, and Advertising Router).
/// Multiple instances of the link state advertisement may exist in the
/// routing domain at the same time.  It is then necessary to determine
/// which instance is more recent.  This is accomplished by examining
/// the LS age, LS sequence number and LS checksum fields that are also
/// contained in the link state advertisement header.
#[derive(Debug, NomBE)]
pub struct OspfLinkStateAdvertisementHeader {
    pub ls_age: u16,
    pub options: u8,
    pub link_state_type: OspfLinkStateType,
    pub link_state_id: u32,
    pub advertising_router: u32,
    pub ls_seq_number: u32,
    pub ls_checksum: u16,
    pub length: u16,
}

impl OspfLinkStateAdvertisementHeader {
    pub fn link_state_id(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.link_state_id)
    }

    pub fn advertising_router(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.advertising_router)
    }
}

/// Link state advertisements
#[derive(Debug)]
pub enum OspfLinkStateAdvertisement {
    RouterLinks(OspfRouterLinksAdvertisement),
    NetworkLinks(OspfNetworkLinksAdvertisement),
    SummaryLinkIpNetwork(OspfSummaryLinkAdvertisement),
    SummaryLinkAsbr(OspfSummaryLinkAdvertisement),
    ASExternalLink(OspfASExternalLinkAdvertisement),
    NSSAASExternal(OspfNSSAExternalLinkAdvertisement),
    OpaqueLinkLocalScope(OspfOpaqueLinkAdvertisement),
    OpaqueAreaLocalScope(OspfOpaqueLinkAdvertisement),
    OpaqueASWideScope(OspfOpaqueLinkAdvertisement),
}

/// Router links advertisements
///
/// Router links advertisements are the Type 1 link state
/// advertisements.  Each router in an area originates a router links
/// advertisement.  The advertisement describes the state and cost of
/// the router's links (i.e., interfaces) to the area.  All of the
/// router's links to the area must be described in a single router
/// links advertisement.  For details concerning the construction of
/// router links advertisements, see Section 12.4.1.
#[derive(Debug, NomBE)]
pub struct OspfRouterLinksAdvertisement {
    #[nom(Verify = "header.link_state_type == OspfLinkStateType::RouterLinks")]
    pub header: OspfLinkStateAdvertisementHeader,
    pub flags: u16,
    pub num_links: u16,
    #[nom(Count = "num_links")]
    pub links: Vec<OspfRouterLink>,
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, NomBE)]
pub struct OspfRouterLinkType(pub u8);

newtype_enum! {
impl display OspfRouterLinkType {
    PointToPoint = 1,
    Transit = 2,
    Stub = 3,
    Virtual = 4,
}
}

/// OSPF router link (i.e., interface)
#[derive(Debug, NomBE)]
pub struct OspfRouterLink {
    pub link_id: u32,
    pub link_data: u32,
    pub link_type: OspfRouterLinkType,
    pub num_tos: u8,
    pub tos_0_metric: u16,
    #[nom(Count = "num_tos")]
    pub tos_list: Vec<OspfRouterTOS>,
}

impl OspfRouterLink {
    pub fn link_id(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.link_id)
    }

    pub fn link_data(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.link_data)
    }
}

/// OSPF Router Type Of Service (TOS)
#[derive(Debug, NomBE)]
pub struct OspfRouterTOS {
    pub tos: u8,
    pub reserved: u8,
    pub metric: u16,
}

/// Network links advertisements
///
/// Network links advertisements are the Type 2 link state
/// advertisements.  A network links advertisement is originated for
/// each transit network in the area.  A transit network is a multi-
/// access network that has more than one attached router.  The network
/// links advertisement is originated by the network's Designated
/// Router.  The advertisement describes all routers attached to the
/// network, including the Designated Router itself.  The
/// advertisement's Link State ID field lists the IP interface address
/// of the Designated Router.
///
/// The distance from the network to all attached routers is zero, for
/// all Types of Service.  This is why the TOS and metric fields need
/// not be specified in the network links advertisement.  For details
/// concerning the construction of network links advertisements, see
/// Section 12.4.2.
#[derive(Debug, NomBE)]
pub struct OspfNetworkLinksAdvertisement {
    #[nom(Verify = "header.link_state_type == OspfLinkStateType::NetworkLinks")]
    pub header: OspfLinkStateAdvertisementHeader,
    pub network_mask: u32,
    // limit parsing to (length-xxx) bytes
    #[nom(Parse = "parse_ospf_vec_u32(header.length, 24)")]
    pub attached_routers: Vec<u32>,
}

impl OspfNetworkLinksAdvertisement {
    pub fn network_mask(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.network_mask)
    }

    pub fn iter_attached_routers(&self) -> impl Iterator<Item = Ipv4Addr> + '_ {
        self.attached_routers.iter().map(|&u| Ipv4Addr::from(u))
    }
}

/// Summary link advertisements
///
/// Summary link advertisements are the Type 3 and 4 link state
/// advertisements.  These advertisements are originated by area border
/// routers.  A separate summary link advertisement is made for each
/// destination (known to the router) which belongs to the AS, yet is
/// outside the area.  For details concerning the construction of
/// summary link advertisements, see Section 12.4.3.
///
/// Type 3 link state advertisements are used when the destination is an
/// IP network.  In this case the advertisement's Link State ID field is
/// an IP network number (if necessary, the Link State ID can also have
/// one or more of the network's "host" bits set; see Appendix F for
/// details). When the destination is an AS boundary router, a Type 4
/// advertisement is used, and the Link State ID field is the AS
/// boundary router's OSPF Router ID.  (To see why it is necessary to
/// advertise the location of each ASBR, consult Section 16.4.)  Other
/// than the difference in the Link State ID field, the format of Type 3
/// and 4 link state advertisements is identical.
#[derive(Debug, NomBE)]
pub struct OspfSummaryLinkAdvertisement {
    #[nom(
        Verify = "header.link_state_type == OspfLinkStateType::SummaryLinkIpNetwork ||
        header.link_state_type == OspfLinkStateType::SummaryLinkAsbr"
    )]
    pub header: OspfLinkStateAdvertisementHeader,
    pub network_mask: u32,
    pub tos: u8,
    #[nom(Parse = "be_u24")]
    pub metric: u32,
    // limit parsing to (length-xxx) bytes
    #[nom(Parse = "parse_ospf_tos_routes(header.length)")]
    pub tos_routes: Vec<OspfTosRoute>,
}

impl OspfSummaryLinkAdvertisement {
    pub fn network_mask(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.network_mask)
    }
}

#[derive(Debug, NomBE)]
pub struct OspfTosRoute {
    pub tos: u8,
    #[nom(Parse = "be_u24")]
    pub metric: u32,
}

/// AS external link advertisements
///
/// AS external link advertisements are the Type 5 link state
/// advertisements.  These advertisements are originated by AS boundary
/// routers.  A separate advertisement is made for each destination
/// (known to the router) which is external to the AS.  For details
/// concerning the construction of AS external link advertisements, see
/// Section 12.4.3.
///
/// AS external link advertisements usually describe a particular
/// external destination.  For these advertisements the Link State ID
/// field specifies an IP network number (if necessary, the Link State
/// ID can also have one or more of the network's "host" bits set; see
/// Appendix F for details).  AS external link advertisements are also
/// used to describe a default route.  Default routes are used when no
/// specific route exists to the destination.  When describing a default
/// route, the Link State ID is always set to DefaultDestination
/// (0.0.0.0) and the Network Mask is set to 0.0.0.0.
#[derive(Debug, NomBE)]
pub struct OspfASExternalLinkAdvertisement {
    #[nom(Verify = "header.link_state_type == OspfLinkStateType::ASExternalLink")]
    pub header: OspfLinkStateAdvertisementHeader,
    pub network_mask: u32,
    pub external_and_reserved: u8,
    #[nom(Parse = "be_u24")]
    pub metric: u32,
    pub forwarding_address: u32,
    pub external_route_tag: u32,
    // limit parsing to (length-xxx) bytes
    #[nom(Parse = "parse_ospf_external_tos_routes(header.length)")]
    pub tos_list: Vec<OspfExternalTosRoute>,
}

impl OspfASExternalLinkAdvertisement {
    pub fn forwarding_address(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.forwarding_address)
    }
    pub fn network_mask(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.network_mask)
    }
}

#[derive(Debug, NomBE)]
pub struct OspfExternalTosRoute {
    pub tos: u8,
    #[nom(Parse = "be_u24")]
    pub metric: u32,
    pub forwarding_address: u32,
    pub external_route_tag: u32,
}

impl OspfExternalTosRoute {
    pub fn forwarding_address(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.forwarding_address)
    }
}

/// NSSA AS-External LSA (type 7, rfc1587, rfc3101)
#[derive(Debug, NomBE)]
pub struct OspfNSSAExternalLinkAdvertisement {
    #[nom(Verify = "header.link_state_type == OspfLinkStateType::NSSAASExternal")]
    pub header: OspfLinkStateAdvertisementHeader,
    pub network_mask: u32,
    pub external_and_tos: u8,
    #[nom(Parse = "be_u24")]
    pub metric: u32,
    pub forwarding_address: u32,
    pub external_route_tag: u32,
    // limit parsing to (length-xxx) bytes
    #[nom(Parse = "parse_ospf_external_tos_routes(header.length)")]
    pub tos_list: Vec<OspfExternalTosRoute>,
}

impl OspfNSSAExternalLinkAdvertisement {
    pub fn forwarding_address(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.forwarding_address)
    }
    pub fn network_mask(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.network_mask)
    }
}

/// The Opaque LSA (RFC5250)
///
/// Opaque LSAs are Type 9, 10, and 11 link state advertisements.  These
/// advertisements MAY be used directly by OSPF or indirectly by some
/// application wishing to distribute information throughout the OSPF
/// domain.  The function of the Opaque LSA option is to provide for
/// future OSPF extensibility.
///
/// Opaque LSAs contain some number of octets (of application-specific
/// data) padded to 32-bit alignment.  Like any other LSA, the Opaque LSA
/// uses the link-state database distribution mechanism for flooding this
/// information throughout the topology.  However, the Opaque LSA has a
/// flooding scope associated with it so that the scope of flooding may
/// be link-local (type-9), area-local (type-10), or the entire OSPF
/// routing domain (type-11).  Section 3 of this document describes the
/// flooding procedures for the Opaque LSA.
#[derive(Debug, NomBE)]
pub struct OspfOpaqueLinkAdvertisement {
    #[nom(
        Verify = "header.link_state_type == OspfLinkStateType::OpaqueLinkLocalScope ||
        header.link_state_type == OspfLinkStateType::OpaqueAreaLocalScope ||
        header.link_state_type == OspfLinkStateType::OpaqueASWideScope"
    )]
    pub header: OspfLinkStateAdvertisementHeader,
    pub data: Vec<u8>,
}

impl OspfOpaqueLinkAdvertisement {
    pub fn opaque_type(&self) -> u8 {
        (self.header.link_state_id >> 24) as u8
    }

    pub fn opaque_id(&self) -> u32 {
        self.header.link_state_id & 0xff_ffff
    }
}