ospf.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. // Copyright 2017 Google, Inc. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the LICENSE file in the root of the source
  5. // tree.
  6. package layers
  7. import (
  8. "encoding/binary"
  9. "fmt"
  10. "github.com/google/gopacket"
  11. )
  12. // OSPFType denotes what kind of OSPF type it is
  13. type OSPFType uint8
  14. // Potential values for OSPF.Type.
  15. const (
  16. OSPFHello OSPFType = 1
  17. OSPFDatabaseDescription OSPFType = 2
  18. OSPFLinkStateRequest OSPFType = 3
  19. OSPFLinkStateUpdate OSPFType = 4
  20. OSPFLinkStateAcknowledgment OSPFType = 5
  21. )
  22. // LSA Function Codes for LSAheader.LSType
  23. const (
  24. RouterLSAtypeV2 = 0x1
  25. RouterLSAtype = 0x2001
  26. NetworkLSAtypeV2 = 0x2
  27. NetworkLSAtype = 0x2002
  28. SummaryLSANetworktypeV2 = 0x3
  29. InterAreaPrefixLSAtype = 0x2003
  30. SummaryLSAASBRtypeV2 = 0x4
  31. InterAreaRouterLSAtype = 0x2004
  32. ASExternalLSAtypeV2 = 0x5
  33. ASExternalLSAtype = 0x4005
  34. NSSALSAtype = 0x2007
  35. LinkLSAtype = 0x0008
  36. IntraAreaPrefixLSAtype = 0x2009
  37. )
  38. // String conversions for OSPFType
  39. func (i OSPFType) String() string {
  40. switch i {
  41. case OSPFHello:
  42. return "Hello"
  43. case OSPFDatabaseDescription:
  44. return "Database Description"
  45. case OSPFLinkStateRequest:
  46. return "Link State Request"
  47. case OSPFLinkStateUpdate:
  48. return "Link State Update"
  49. case OSPFLinkStateAcknowledgment:
  50. return "Link State Acknowledgment"
  51. default:
  52. return ""
  53. }
  54. }
  55. // Prefix extends IntraAreaPrefixLSA
  56. type Prefix struct {
  57. PrefixLength uint8
  58. PrefixOptions uint8
  59. Metric uint16
  60. AddressPrefix []byte
  61. }
  62. // IntraAreaPrefixLSA is the struct from RFC 5340 A.4.10.
  63. type IntraAreaPrefixLSA struct {
  64. NumOfPrefixes uint16
  65. RefLSType uint16
  66. RefLinkStateID uint32
  67. RefAdvRouter uint32
  68. Prefixes []Prefix
  69. }
  70. // LinkLSA is the struct from RFC 5340 A.4.9.
  71. type LinkLSA struct {
  72. RtrPriority uint8
  73. Options uint32
  74. LinkLocalAddress []byte
  75. NumOfPrefixes uint32
  76. Prefixes []Prefix
  77. }
  78. // ASExternalLSAV2 is the struct from RFC 2328 A.4.5.
  79. type ASExternalLSAV2 struct {
  80. NetworkMask uint32
  81. ExternalBit uint8
  82. Metric uint32
  83. ForwardingAddress uint32
  84. ExternalRouteTag uint32
  85. }
  86. // ASExternalLSA is the struct from RFC 5340 A.4.7.
  87. type ASExternalLSA struct {
  88. Flags uint8
  89. Metric uint32
  90. PrefixLength uint8
  91. PrefixOptions uint8
  92. RefLSType uint16
  93. AddressPrefix []byte
  94. ForwardingAddress []byte
  95. ExternalRouteTag uint32
  96. RefLinkStateID uint32
  97. }
  98. // InterAreaRouterLSA is the struct from RFC 5340 A.4.6.
  99. type InterAreaRouterLSA struct {
  100. Options uint32
  101. Metric uint32
  102. DestinationRouterID uint32
  103. }
  104. // InterAreaPrefixLSA is the struct from RFC 5340 A.4.5.
  105. type InterAreaPrefixLSA struct {
  106. Metric uint32
  107. PrefixLength uint8
  108. PrefixOptions uint8
  109. AddressPrefix []byte
  110. }
  111. // NetworkLSA is the struct from RFC 5340 A.4.4.
  112. type NetworkLSA struct {
  113. Options uint32
  114. AttachedRouter []uint32
  115. }
  116. // RouterV2 extends RouterLSAV2
  117. type RouterV2 struct {
  118. Type uint8
  119. LinkID uint32
  120. LinkData uint32
  121. Metric uint16
  122. }
  123. // RouterLSAV2 is the struct from RFC 2328 A.4.2.
  124. type RouterLSAV2 struct {
  125. Flags uint8
  126. Links uint16
  127. Routers []RouterV2
  128. }
  129. // Router extends RouterLSA
  130. type Router struct {
  131. Type uint8
  132. Metric uint16
  133. InterfaceID uint32
  134. NeighborInterfaceID uint32
  135. NeighborRouterID uint32
  136. }
  137. // RouterLSA is the struct from RFC 5340 A.4.3.
  138. type RouterLSA struct {
  139. Flags uint8
  140. Options uint32
  141. Routers []Router
  142. }
  143. // LSAheader is the struct from RFC 5340 A.4.2 and RFC 2328 A.4.1.
  144. type LSAheader struct {
  145. LSAge uint16
  146. LSType uint16
  147. LinkStateID uint32
  148. AdvRouter uint32
  149. LSSeqNumber uint32
  150. LSChecksum uint16
  151. Length uint16
  152. LSOptions uint8
  153. }
  154. // LSA links LSAheader with the structs from RFC 5340 A.4.
  155. type LSA struct {
  156. LSAheader
  157. Content interface{}
  158. }
  159. // LSUpdate is the struct from RFC 5340 A.3.5.
  160. type LSUpdate struct {
  161. NumOfLSAs uint32
  162. LSAs []LSA
  163. }
  164. // LSReq is the struct from RFC 5340 A.3.4.
  165. type LSReq struct {
  166. LSType uint16
  167. LSID uint32
  168. AdvRouter uint32
  169. }
  170. // DbDescPkg is the struct from RFC 5340 A.3.3.
  171. type DbDescPkg struct {
  172. Options uint32
  173. InterfaceMTU uint16
  174. Flags uint16
  175. DDSeqNumber uint32
  176. LSAinfo []LSAheader
  177. }
  178. // HelloPkg is the struct from RFC 5340 A.3.2.
  179. type HelloPkg struct {
  180. InterfaceID uint32
  181. RtrPriority uint8
  182. Options uint32
  183. HelloInterval uint16
  184. RouterDeadInterval uint32
  185. DesignatedRouterID uint32
  186. BackupDesignatedRouterID uint32
  187. NeighborID []uint32
  188. }
  189. // HelloPkgV2 extends the HelloPkg struct with OSPFv2 information
  190. type HelloPkgV2 struct {
  191. HelloPkg
  192. NetworkMask uint32
  193. }
  194. // OSPF is a basic OSPF packet header with common fields of Version 2 and Version 3.
  195. type OSPF struct {
  196. Version uint8
  197. Type OSPFType
  198. PacketLength uint16
  199. RouterID uint32
  200. AreaID uint32
  201. Checksum uint16
  202. Content interface{}
  203. }
  204. //OSPFv2 extend the OSPF head with version 2 specific fields
  205. type OSPFv2 struct {
  206. BaseLayer
  207. OSPF
  208. AuType uint16
  209. Authentication uint64
  210. }
  211. // OSPFv3 extend the OSPF head with version 3 specific fields
  212. type OSPFv3 struct {
  213. BaseLayer
  214. OSPF
  215. Instance uint8
  216. Reserved uint8
  217. }
  218. // getLSAsv2 parses the LSA information from the packet for OSPFv2
  219. func getLSAsv2(num uint32, data []byte) ([]LSA, error) {
  220. var lsas []LSA
  221. var i uint32 = 0
  222. var offset uint32 = 0
  223. for ; i < num; i++ {
  224. lstype := uint16(data[offset+3])
  225. lsalength := binary.BigEndian.Uint16(data[offset+18 : offset+20])
  226. content, err := extractLSAInformation(lstype, lsalength, data[offset:])
  227. if err != nil {
  228. return nil, fmt.Errorf("Could not extract Link State type.")
  229. }
  230. lsa := LSA{
  231. LSAheader: LSAheader{
  232. LSAge: binary.BigEndian.Uint16(data[offset : offset+2]),
  233. LSOptions: data[offset+2],
  234. LSType: lstype,
  235. LinkStateID: binary.BigEndian.Uint32(data[offset+4 : offset+8]),
  236. AdvRouter: binary.BigEndian.Uint32(data[offset+8 : offset+12]),
  237. LSSeqNumber: binary.BigEndian.Uint32(data[offset+12 : offset+16]),
  238. LSChecksum: binary.BigEndian.Uint16(data[offset+16 : offset+18]),
  239. Length: lsalength,
  240. },
  241. Content: content,
  242. }
  243. lsas = append(lsas, lsa)
  244. offset += uint32(lsalength)
  245. }
  246. return lsas, nil
  247. }
  248. // extractLSAInformation extracts all the LSA information
  249. func extractLSAInformation(lstype, lsalength uint16, data []byte) (interface{}, error) {
  250. var content interface{}
  251. switch lstype {
  252. case RouterLSAtypeV2:
  253. var routers []RouterV2
  254. links := binary.BigEndian.Uint16(data[22:24])
  255. content = RouterLSAV2{
  256. Flags: data[20],
  257. Links: links,
  258. Routers: routers,
  259. }
  260. case ASExternalLSAtypeV2:
  261. content = ASExternalLSAV2{
  262. NetworkMask: binary.BigEndian.Uint32(data[20:24]),
  263. ExternalBit: data[24] & 0x80,
  264. Metric: binary.BigEndian.Uint32(data[24:28]) & 0x00FFFFFF,
  265. ForwardingAddress: binary.BigEndian.Uint32(data[28:32]),
  266. ExternalRouteTag: binary.BigEndian.Uint32(data[32:36]),
  267. }
  268. case RouterLSAtype:
  269. var routers []Router
  270. var j uint32
  271. for j = 24; j < uint32(lsalength); j += 16 {
  272. router := Router{
  273. Type: uint8(data[j]),
  274. Metric: binary.BigEndian.Uint16(data[j+2 : j+4]),
  275. InterfaceID: binary.BigEndian.Uint32(data[j+4 : j+8]),
  276. NeighborInterfaceID: binary.BigEndian.Uint32(data[j+8 : j+12]),
  277. NeighborRouterID: binary.BigEndian.Uint32(data[j+12 : j+16]),
  278. }
  279. routers = append(routers, router)
  280. }
  281. content = RouterLSA{
  282. Flags: uint8(data[20]),
  283. Options: binary.BigEndian.Uint32(data[20:24]) & 0x00FFFFFF,
  284. Routers: routers,
  285. }
  286. case NetworkLSAtype:
  287. var routers []uint32
  288. var j uint32
  289. for j = 24; j < uint32(lsalength); j += 4 {
  290. routers = append(routers, binary.BigEndian.Uint32(data[j:j+4]))
  291. }
  292. content = NetworkLSA{
  293. Options: binary.BigEndian.Uint32(data[20:24]) & 0x00FFFFFF,
  294. AttachedRouter: routers,
  295. }
  296. case InterAreaPrefixLSAtype:
  297. content = InterAreaPrefixLSA{
  298. Metric: binary.BigEndian.Uint32(data[20:24]) & 0x00FFFFFF,
  299. PrefixLength: uint8(data[24]),
  300. PrefixOptions: uint8(data[25]),
  301. AddressPrefix: data[28:uint32(lsalength)],
  302. }
  303. case InterAreaRouterLSAtype:
  304. content = InterAreaRouterLSA{
  305. Options: binary.BigEndian.Uint32(data[20:24]) & 0x00FFFFFF,
  306. Metric: binary.BigEndian.Uint32(data[24:28]) & 0x00FFFFFF,
  307. DestinationRouterID: binary.BigEndian.Uint32(data[28:32]),
  308. }
  309. case ASExternalLSAtype:
  310. fallthrough
  311. case NSSALSAtype:
  312. flags := uint8(data[20])
  313. prefixLen := uint8(data[24]) / 8
  314. var forwardingAddress []byte
  315. if (flags & 0x02) == 0x02 {
  316. forwardingAddress = data[28+uint32(prefixLen) : 28+uint32(prefixLen)+16]
  317. }
  318. content = ASExternalLSA{
  319. Flags: flags,
  320. Metric: binary.BigEndian.Uint32(data[20:24]) & 0x00FFFFFF,
  321. PrefixLength: prefixLen,
  322. PrefixOptions: uint8(data[25]),
  323. RefLSType: binary.BigEndian.Uint16(data[26:28]),
  324. AddressPrefix: data[28 : 28+uint32(prefixLen)],
  325. ForwardingAddress: forwardingAddress,
  326. }
  327. case LinkLSAtype:
  328. var prefixes []Prefix
  329. var prefixOffset uint32 = 44
  330. var j uint32
  331. numOfPrefixes := binary.BigEndian.Uint32(data[40:44])
  332. for j = 0; j < numOfPrefixes; j++ {
  333. prefixLen := uint8(data[prefixOffset])
  334. prefix := Prefix{
  335. PrefixLength: prefixLen,
  336. PrefixOptions: uint8(data[prefixOffset+1]),
  337. AddressPrefix: data[prefixOffset+4 : prefixOffset+4+uint32(prefixLen)/8],
  338. }
  339. prefixes = append(prefixes, prefix)
  340. prefixOffset = prefixOffset + 4 + uint32(prefixLen)/8
  341. }
  342. content = LinkLSA{
  343. RtrPriority: uint8(data[20]),
  344. Options: binary.BigEndian.Uint32(data[20:24]) & 0x00FFFFFF,
  345. LinkLocalAddress: data[24:40],
  346. NumOfPrefixes: numOfPrefixes,
  347. Prefixes: prefixes,
  348. }
  349. case IntraAreaPrefixLSAtype:
  350. var prefixes []Prefix
  351. var prefixOffset uint32 = 32
  352. var j uint16
  353. numOfPrefixes := binary.BigEndian.Uint16(data[20:22])
  354. for j = 0; j < numOfPrefixes; j++ {
  355. prefixLen := uint8(data[prefixOffset])
  356. prefix := Prefix{
  357. PrefixLength: prefixLen,
  358. PrefixOptions: uint8(data[prefixOffset+1]),
  359. Metric: binary.BigEndian.Uint16(data[prefixOffset+2 : prefixOffset+4]),
  360. AddressPrefix: data[prefixOffset+4 : prefixOffset+4+uint32(prefixLen)/8],
  361. }
  362. prefixes = append(prefixes, prefix)
  363. prefixOffset = prefixOffset + 4 + uint32(prefixLen)
  364. }
  365. content = IntraAreaPrefixLSA{
  366. NumOfPrefixes: numOfPrefixes,
  367. RefLSType: binary.BigEndian.Uint16(data[22:24]),
  368. RefLinkStateID: binary.BigEndian.Uint32(data[24:28]),
  369. RefAdvRouter: binary.BigEndian.Uint32(data[28:32]),
  370. Prefixes: prefixes,
  371. }
  372. default:
  373. return nil, fmt.Errorf("Unknown Link State type.")
  374. }
  375. return content, nil
  376. }
  377. // getLSAs parses the LSA information from the packet for OSPFv3
  378. func getLSAs(num uint32, data []byte) ([]LSA, error) {
  379. var lsas []LSA
  380. var i uint32 = 0
  381. var offset uint32 = 0
  382. for ; i < num; i++ {
  383. var content interface{}
  384. lstype := binary.BigEndian.Uint16(data[offset+2 : offset+4])
  385. lsalength := binary.BigEndian.Uint16(data[offset+18 : offset+20])
  386. content, err := extractLSAInformation(lstype, lsalength, data[offset:])
  387. if err != nil {
  388. return nil, fmt.Errorf("Could not extract Link State type.")
  389. }
  390. lsa := LSA{
  391. LSAheader: LSAheader{
  392. LSAge: binary.BigEndian.Uint16(data[offset : offset+2]),
  393. LSType: lstype,
  394. LinkStateID: binary.BigEndian.Uint32(data[offset+4 : offset+8]),
  395. AdvRouter: binary.BigEndian.Uint32(data[offset+8 : offset+12]),
  396. LSSeqNumber: binary.BigEndian.Uint32(data[offset+12 : offset+16]),
  397. LSChecksum: binary.BigEndian.Uint16(data[offset+16 : offset+18]),
  398. Length: lsalength,
  399. },
  400. Content: content,
  401. }
  402. lsas = append(lsas, lsa)
  403. offset += uint32(lsalength)
  404. }
  405. return lsas, nil
  406. }
  407. // DecodeFromBytes decodes the given bytes into the OSPF layer.
  408. func (ospf *OSPFv2) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  409. if len(data) < 24 {
  410. return fmt.Errorf("Packet too smal for OSPF Version 2")
  411. }
  412. ospf.Version = uint8(data[0])
  413. ospf.Type = OSPFType(data[1])
  414. ospf.PacketLength = binary.BigEndian.Uint16(data[2:4])
  415. ospf.RouterID = binary.BigEndian.Uint32(data[4:8])
  416. ospf.AreaID = binary.BigEndian.Uint32(data[8:12])
  417. ospf.Checksum = binary.BigEndian.Uint16(data[12:14])
  418. ospf.AuType = binary.BigEndian.Uint16(data[14:16])
  419. ospf.Authentication = binary.BigEndian.Uint64(data[16:24])
  420. switch ospf.Type {
  421. case OSPFHello:
  422. var neighbors []uint32
  423. for i := 44; uint16(i+4) <= ospf.PacketLength; i += 4 {
  424. neighbors = append(neighbors, binary.BigEndian.Uint32(data[i:i+4]))
  425. }
  426. ospf.Content = HelloPkgV2{
  427. NetworkMask: binary.BigEndian.Uint32(data[24:28]),
  428. HelloPkg: HelloPkg{
  429. HelloInterval: binary.BigEndian.Uint16(data[28:30]),
  430. Options: uint32(data[30]),
  431. RtrPriority: uint8(data[31]),
  432. RouterDeadInterval: binary.BigEndian.Uint32(data[32:36]),
  433. DesignatedRouterID: binary.BigEndian.Uint32(data[36:40]),
  434. BackupDesignatedRouterID: binary.BigEndian.Uint32(data[40:44]),
  435. NeighborID: neighbors,
  436. },
  437. }
  438. case OSPFDatabaseDescription:
  439. var lsas []LSAheader
  440. for i := 32; uint16(i+20) <= ospf.PacketLength; i += 20 {
  441. lsa := LSAheader{
  442. LSAge: binary.BigEndian.Uint16(data[i : i+2]),
  443. LSType: binary.BigEndian.Uint16(data[i+2 : i+4]),
  444. LinkStateID: binary.BigEndian.Uint32(data[i+4 : i+8]),
  445. AdvRouter: binary.BigEndian.Uint32(data[i+8 : i+12]),
  446. LSSeqNumber: binary.BigEndian.Uint32(data[i+12 : i+16]),
  447. LSChecksum: binary.BigEndian.Uint16(data[i+16 : i+18]),
  448. Length: binary.BigEndian.Uint16(data[i+18 : i+20]),
  449. }
  450. lsas = append(lsas, lsa)
  451. }
  452. ospf.Content = DbDescPkg{
  453. InterfaceMTU: binary.BigEndian.Uint16(data[24:26]),
  454. Options: uint32(data[26]),
  455. Flags: uint16(data[27]),
  456. DDSeqNumber: binary.BigEndian.Uint32(data[28:32]),
  457. LSAinfo: lsas,
  458. }
  459. case OSPFLinkStateRequest:
  460. var lsrs []LSReq
  461. for i := 24; uint16(i+12) <= ospf.PacketLength; i += 12 {
  462. lsr := LSReq{
  463. LSType: binary.BigEndian.Uint16(data[i+2 : i+4]),
  464. LSID: binary.BigEndian.Uint32(data[i+4 : i+8]),
  465. AdvRouter: binary.BigEndian.Uint32(data[i+8 : i+12]),
  466. }
  467. lsrs = append(lsrs, lsr)
  468. }
  469. ospf.Content = lsrs
  470. case OSPFLinkStateUpdate:
  471. num := binary.BigEndian.Uint32(data[24:28])
  472. lsas, err := getLSAsv2(num, data[28:])
  473. if err != nil {
  474. return fmt.Errorf("Cannot parse Link State Update packet: %v", err)
  475. }
  476. ospf.Content = LSUpdate{
  477. NumOfLSAs: num,
  478. LSAs: lsas,
  479. }
  480. case OSPFLinkStateAcknowledgment:
  481. var lsas []LSAheader
  482. for i := 24; uint16(i+20) <= ospf.PacketLength; i += 20 {
  483. lsa := LSAheader{
  484. LSAge: binary.BigEndian.Uint16(data[i : i+2]),
  485. LSOptions: data[i+2],
  486. LSType: uint16(data[i+3]),
  487. LinkStateID: binary.BigEndian.Uint32(data[i+4 : i+8]),
  488. AdvRouter: binary.BigEndian.Uint32(data[i+8 : i+12]),
  489. LSSeqNumber: binary.BigEndian.Uint32(data[i+12 : i+16]),
  490. LSChecksum: binary.BigEndian.Uint16(data[i+16 : i+18]),
  491. Length: binary.BigEndian.Uint16(data[i+18 : i+20]),
  492. }
  493. lsas = append(lsas, lsa)
  494. }
  495. ospf.Content = lsas
  496. }
  497. return nil
  498. }
  499. // DecodeFromBytes decodes the given bytes into the OSPF layer.
  500. func (ospf *OSPFv3) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  501. if len(data) < 16 {
  502. return fmt.Errorf("Packet too smal for OSPF Version 3")
  503. }
  504. ospf.Version = uint8(data[0])
  505. ospf.Type = OSPFType(data[1])
  506. ospf.PacketLength = binary.BigEndian.Uint16(data[2:4])
  507. ospf.RouterID = binary.BigEndian.Uint32(data[4:8])
  508. ospf.AreaID = binary.BigEndian.Uint32(data[8:12])
  509. ospf.Checksum = binary.BigEndian.Uint16(data[12:14])
  510. ospf.Instance = uint8(data[14])
  511. ospf.Reserved = uint8(data[15])
  512. switch ospf.Type {
  513. case OSPFHello:
  514. var neighbors []uint32
  515. for i := 36; uint16(i+4) <= ospf.PacketLength; i += 4 {
  516. neighbors = append(neighbors, binary.BigEndian.Uint32(data[i:i+4]))
  517. }
  518. ospf.Content = HelloPkg{
  519. InterfaceID: binary.BigEndian.Uint32(data[16:20]),
  520. RtrPriority: uint8(data[20]),
  521. Options: binary.BigEndian.Uint32(data[21:25]) >> 8,
  522. HelloInterval: binary.BigEndian.Uint16(data[24:26]),
  523. RouterDeadInterval: uint32(binary.BigEndian.Uint16(data[26:28])),
  524. DesignatedRouterID: binary.BigEndian.Uint32(data[28:32]),
  525. BackupDesignatedRouterID: binary.BigEndian.Uint32(data[32:36]),
  526. NeighborID: neighbors,
  527. }
  528. case OSPFDatabaseDescription:
  529. var lsas []LSAheader
  530. for i := 28; uint16(i+20) <= ospf.PacketLength; i += 20 {
  531. lsa := LSAheader{
  532. LSAge: binary.BigEndian.Uint16(data[i : i+2]),
  533. LSType: binary.BigEndian.Uint16(data[i+2 : i+4]),
  534. LinkStateID: binary.BigEndian.Uint32(data[i+4 : i+8]),
  535. AdvRouter: binary.BigEndian.Uint32(data[i+8 : i+12]),
  536. LSSeqNumber: binary.BigEndian.Uint32(data[i+12 : i+16]),
  537. LSChecksum: binary.BigEndian.Uint16(data[i+16 : i+18]),
  538. Length: binary.BigEndian.Uint16(data[i+18 : i+20]),
  539. }
  540. lsas = append(lsas, lsa)
  541. }
  542. ospf.Content = DbDescPkg{
  543. Options: binary.BigEndian.Uint32(data[16:20]) & 0x00FFFFFF,
  544. InterfaceMTU: binary.BigEndian.Uint16(data[20:22]),
  545. Flags: binary.BigEndian.Uint16(data[22:24]),
  546. DDSeqNumber: binary.BigEndian.Uint32(data[24:28]),
  547. LSAinfo: lsas,
  548. }
  549. case OSPFLinkStateRequest:
  550. var lsrs []LSReq
  551. for i := 16; uint16(i+12) <= ospf.PacketLength; i += 12 {
  552. lsr := LSReq{
  553. LSType: binary.BigEndian.Uint16(data[i+2 : i+4]),
  554. LSID: binary.BigEndian.Uint32(data[i+4 : i+8]),
  555. AdvRouter: binary.BigEndian.Uint32(data[i+8 : i+12]),
  556. }
  557. lsrs = append(lsrs, lsr)
  558. }
  559. ospf.Content = lsrs
  560. case OSPFLinkStateUpdate:
  561. num := binary.BigEndian.Uint32(data[16:20])
  562. lsas, err := getLSAs(num, data[20:])
  563. if err != nil {
  564. return fmt.Errorf("Cannot parse Link State Update packet: %v", err)
  565. }
  566. ospf.Content = LSUpdate{
  567. NumOfLSAs: num,
  568. LSAs: lsas,
  569. }
  570. case OSPFLinkStateAcknowledgment:
  571. var lsas []LSAheader
  572. for i := 16; uint16(i+20) <= ospf.PacketLength; i += 20 {
  573. lsa := LSAheader{
  574. LSAge: binary.BigEndian.Uint16(data[i : i+2]),
  575. LSType: binary.BigEndian.Uint16(data[i+2 : i+4]),
  576. LinkStateID: binary.BigEndian.Uint32(data[i+4 : i+8]),
  577. AdvRouter: binary.BigEndian.Uint32(data[i+8 : i+12]),
  578. LSSeqNumber: binary.BigEndian.Uint32(data[i+12 : i+16]),
  579. LSChecksum: binary.BigEndian.Uint16(data[i+16 : i+18]),
  580. Length: binary.BigEndian.Uint16(data[i+18 : i+20]),
  581. }
  582. lsas = append(lsas, lsa)
  583. }
  584. ospf.Content = lsas
  585. default:
  586. }
  587. return nil
  588. }
  589. // LayerType returns LayerTypeOSPF
  590. func (ospf *OSPFv2) LayerType() gopacket.LayerType {
  591. return LayerTypeOSPF
  592. }
  593. func (ospf *OSPFv3) LayerType() gopacket.LayerType {
  594. return LayerTypeOSPF
  595. }
  596. // NextLayerType returns the layer type contained by this DecodingLayer.
  597. func (ospf *OSPFv2) NextLayerType() gopacket.LayerType {
  598. return gopacket.LayerTypeZero
  599. }
  600. func (ospf *OSPFv3) NextLayerType() gopacket.LayerType {
  601. return gopacket.LayerTypeZero
  602. }
  603. // CanDecode returns the set of layer types that this DecodingLayer can decode.
  604. func (ospf *OSPFv2) CanDecode() gopacket.LayerClass {
  605. return LayerTypeOSPF
  606. }
  607. func (ospf *OSPFv3) CanDecode() gopacket.LayerClass {
  608. return LayerTypeOSPF
  609. }
  610. func decodeOSPF(data []byte, p gopacket.PacketBuilder) error {
  611. if len(data) < 14 {
  612. return fmt.Errorf("Packet too smal for OSPF")
  613. }
  614. switch uint8(data[0]) {
  615. case 2:
  616. ospf := &OSPFv2{}
  617. return decodingLayerDecoder(ospf, data, p)
  618. case 3:
  619. ospf := &OSPFv3{}
  620. return decodingLayerDecoder(ospf, data, p)
  621. default:
  622. }
  623. return fmt.Errorf("Unable to determine OSPF type.")
  624. }