github.com/gopacket/gopacket@v1.1.0/layers/ospf_test.go (about) 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 7 package layers 8 9 import ( 10 "reflect" 11 "testing" 12 13 "github.com/gopacket/gopacket" 14 ) 15 16 // testPacketOSPF2Hello is the packet: 17 // 13:19:20.008765 IP 192.168.170.8 > 224.0.0.5: OSPFv2, Hello, length 44 18 // 19 // 0x0000: 0100 5e00 0005 00e0 18b1 0cad 0800 45c0 ..^...........E. 20 // 0x0010: 0040 0812 0000 0159 65dd c0a8 aa08 e000 .@.....Ye....... 21 // 0x0020: 0005 0201 002c c0a8 aa08 0000 0001 273b .....,........'; 22 // 0x0030: 0000 0000 0000 0000 0000 ffff ff00 000a ................ 23 // 0x0040: 0201 0000 0028 c0a8 aa08 0000 0000 .....(........ 24 var testPacketOSPF2Hello = []byte{ 25 0x01, 0x00, 0x5e, 0x00, 0x00, 0x05, 0x00, 0xe0, 0x18, 0xb1, 0x0c, 0xad, 0x08, 0x00, 0x45, 0xc0, 26 0x00, 0x40, 0x08, 0x12, 0x00, 0x00, 0x01, 0x59, 0x65, 0xdd, 0xc0, 0xa8, 0xaa, 0x08, 0xe0, 0x00, 27 0x00, 0x05, 0x02, 0x01, 0x00, 0x2c, 0xc0, 0xa8, 0xaa, 0x08, 0x00, 0x00, 0x00, 0x01, 0x27, 0x3b, 28 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x0a, 29 0x02, 0x01, 0x00, 0x00, 0x00, 0x28, 0xc0, 0xa8, 0xaa, 0x08, 0x00, 0x00, 0x00, 0x00, 30 } 31 32 func TestPacketOSPF2Hello(t *testing.T) { 33 p := gopacket.NewPacket(testPacketOSPF2Hello, LinkTypeEthernet, gopacket.Default) 34 if p.ErrorLayer() != nil { 35 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 36 } 37 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeOSPF}, t) 38 39 ospf := p.Layer(LayerTypeOSPF).(*OSPFv2) 40 if ospf.Version != 2 { 41 t.Fatal("Invalid OSPF version") 42 } 43 if got, ok := p.Layer(LayerTypeOSPF).(*OSPFv2); ok { 44 want := &OSPFv2{ 45 OSPF: OSPF{ 46 Version: 2, 47 Type: OSPFHello, 48 PacketLength: 44, 49 RouterID: 0xc0a8aa08, 50 AreaID: 1, 51 Checksum: 0x273b, 52 Content: HelloPkgV2{ 53 NetworkMask: 0xffffff00, 54 HelloPkg: HelloPkg{ 55 RtrPriority: 0x1, 56 Options: 0x2, 57 HelloInterval: 0xa, 58 RouterDeadInterval: 0x28, 59 DesignatedRouterID: 0xc0a8aa08, 60 BackupDesignatedRouterID: 0x0, 61 }, 62 }, 63 }, 64 } 65 if !reflect.DeepEqual(got, want) { 66 t.Errorf("OSPF packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 67 } 68 } else { 69 t.Error("No OSPF layer type found in packet") 70 } 71 } 72 func BenchmarkDecodePacketPacket5(b *testing.B) { 73 for i := 0; i < b.N; i++ { 74 gopacket.NewPacket(testPacketOSPF2Hello, LinkTypeEthernet, gopacket.NoCopy) 75 } 76 } 77 78 // testPacketOSPF3Hello is the packet: 79 // 80 // 14:43:11.663317 IP6 fe80::1 > ff02::5: OSPFv3, Hello, length 36 81 // 0x0000: 3333 0000 0005 c200 1ffa 0001 86dd 6e00 33............n. 82 // 0x0010: 0000 0024 5901 fe80 0000 0000 0000 0000 ...$Y........... 83 // 0x0020: 0000 0000 0001 ff02 0000 0000 0000 0000 ................ 84 // 0x0030: 0000 0000 0005 0301 0024 0101 0101 0000 .........$...... 85 // 0x0040: 0001 fb86 0000 0000 0005 0100 0013 000a ................ 86 // 0x0050: 0028 0000 0000 0000 0000 .(........ 87 var testPacketOSPF3Hello = []byte{ 88 0x33, 0x33, 0x00, 0x00, 0x00, 0x05, 0xc2, 0x00, 0x1f, 0xfa, 0x00, 0x01, 0x86, 0xdd, 0x6e, 0x00, 89 0x00, 0x00, 0x00, 0x24, 0x59, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 90 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 91 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x03, 0x01, 0x00, 0x24, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 92 0x00, 0x01, 0xfb, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x01, 0x00, 0x00, 0x13, 0x00, 0x0a, 93 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 94 } 95 96 func TestPacketOSPF3Hello(t *testing.T) { 97 p := gopacket.NewPacket(testPacketOSPF3Hello, LinkTypeEthernet, gopacket.Default) 98 if p.ErrorLayer() != nil { 99 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 100 } 101 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeOSPF}, t) 102 103 ospf := p.Layer(LayerTypeOSPF).(*OSPFv3) 104 if ospf.Version != 3 { 105 t.Fatal("Invalid OSPF version") 106 } 107 if got, ok := p.Layer(LayerTypeOSPF).(*OSPFv3); ok { 108 want := &OSPFv3{ 109 OSPF: OSPF{ 110 Version: 3, 111 Type: OSPFHello, 112 PacketLength: 36, 113 RouterID: 0x1010101, 114 AreaID: 1, 115 Checksum: 0xfb86, 116 Content: HelloPkg{ 117 InterfaceID: 5, 118 RtrPriority: 1, 119 Options: 0x000013, 120 HelloInterval: 10, 121 RouterDeadInterval: 40, 122 DesignatedRouterID: 0, 123 BackupDesignatedRouterID: 0, 124 }, 125 }, 126 Instance: 0, 127 Reserved: 0, 128 } 129 if !reflect.DeepEqual(got, want) { 130 t.Errorf("OSPF packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 131 } 132 } else { 133 t.Error("No OSPF layer type found in packet") 134 } 135 } 136 func BenchmarkDecodePacketPacket0(b *testing.B) { 137 for i := 0; i < b.N; i++ { 138 gopacket.NewPacket(testPacketOSPF3Hello, LinkTypeEthernet, gopacket.NoCopy) 139 } 140 } 141 142 // testPacketOSPF2DBDesc is the packet: 143 // 13:20:14.414477 IP 192.168.170.8 > 192.168.170.2: OSPFv2, Database Description, length 32 144 // 145 // 0x0000: 0060 0881 7a70 00e0 18b1 0cad 0800 45c0 .`..zp........E. 146 // 0x0010: 0034 2be5 0000 0159 b770 c0a8 aa08 c0a8 .4+....Y.p...... 147 // 0x0020: aa02 0202 0020 c0a8 aa08 0000 0001 a052 ...............R 148 // 0x0030: 0000 0000 0000 0000 0000 05dc 0207 4177 ..............Aw 149 // 0x0040: a97e .~ 150 var testPacketOSPF2DBDesc = []byte{ 151 0x00, 0x60, 0x08, 0x81, 0x7a, 0x70, 0x00, 0xe0, 0x18, 0xb1, 0x0c, 0xad, 0x08, 0x00, 0x45, 0xc0, 152 0x00, 0x34, 0x2b, 0xe5, 0x00, 0x00, 0x01, 0x59, 0xb7, 0x70, 0xc0, 0xa8, 0xaa, 0x08, 0xc0, 0xa8, 153 0xaa, 0x02, 0x02, 0x02, 0x00, 0x20, 0xc0, 0xa8, 0xaa, 0x08, 0x00, 0x00, 0x00, 0x01, 0xa0, 0x52, 154 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xdc, 0x02, 0x07, 0x41, 0x77, 155 0xa9, 0x7e, 156 } 157 158 func TestPacketOSPF2DBDesc(t *testing.T) { 159 p := gopacket.NewPacket(testPacketOSPF2DBDesc, LinkTypeEthernet, gopacket.Default) 160 if p.ErrorLayer() != nil { 161 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 162 } 163 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeOSPF}, t) 164 if got, ok := p.Layer(LayerTypeOSPF).(*OSPFv2); ok { 165 want := &OSPFv2{ 166 OSPF: OSPF{ 167 Version: 2, 168 Type: OSPFDatabaseDescription, 169 PacketLength: 32, 170 RouterID: 0xc0a8aa08, 171 AreaID: 1, 172 Checksum: 0xa052, 173 Content: DbDescPkg{ 174 Options: 0x02, 175 InterfaceMTU: 1500, 176 Flags: 0x7, 177 DDSeqNumber: 1098361214, 178 }, 179 }, 180 } 181 if !reflect.DeepEqual(got, want) { 182 t.Errorf("OSPF packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 183 } 184 } else { 185 t.Error("No OSPF layer type found in packet") 186 } 187 } 188 func BenchmarkDecodePacketPacket6(b *testing.B) { 189 for i := 0; i < b.N; i++ { 190 gopacket.NewPacket(testPacketOSPF2DBDesc, LinkTypeEthernet, gopacket.NoCopy) 191 } 192 } 193 194 // testPacketOSPF3DBDesc is the packet: 195 // 196 // 14:43:51.657571 IP6 fe80::2 > fe80::1: OSPFv3, Database Description, length 28 197 // 0x0000: c200 1ffa 0001 c201 1ffa 0001 86dd 6e00 ..............n. 198 // 0x0010: 0000 001c 5901 fe80 0000 0000 0000 0000 ....Y........... 199 // 0x0020: 0000 0000 0002 fe80 0000 0000 0000 0000 ................ 200 // 0x0030: 0000 0000 0001 0302 001c 0202 0202 0000 ................ 201 // 0x0040: 0001 d826 0000 0000 0013 05dc 0007 0000 ...&............ 202 // 0x0050: 1d46 .F 203 var testPacketOSPF3DBDesc = []byte{ 204 0xc2, 0x00, 0x1f, 0xfa, 0x00, 0x01, 0xc2, 0x01, 0x1f, 0xfa, 0x00, 0x01, 0x86, 0xdd, 0x6e, 0x00, 205 0x00, 0x00, 0x00, 0x1c, 0x59, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 206 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 207 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x02, 0x00, 0x1c, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 208 0x00, 0x01, 0xd8, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x05, 0xdc, 0x00, 0x07, 0x00, 0x00, 209 0x1d, 0x46, 210 } 211 212 func TestPacketOSPF3DBDesc(t *testing.T) { 213 p := gopacket.NewPacket(testPacketOSPF3DBDesc, LinkTypeEthernet, gopacket.Default) 214 if p.ErrorLayer() != nil { 215 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 216 } 217 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeOSPF}, t) 218 if got, ok := p.Layer(LayerTypeOSPF).(*OSPFv3); ok { 219 want := &OSPFv3{ 220 OSPF: OSPF{ 221 Version: 3, 222 Type: OSPFDatabaseDescription, 223 PacketLength: 28, 224 RouterID: 0x2020202, 225 AreaID: 1, 226 Checksum: 0xd826, 227 Content: DbDescPkg{ 228 Options: 0x000013, 229 InterfaceMTU: 1500, 230 Flags: 0x7, 231 DDSeqNumber: 7494, 232 }, 233 }, 234 Instance: 0, 235 Reserved: 0, 236 } 237 if !reflect.DeepEqual(got, want) { 238 t.Errorf("OSPF packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 239 } 240 } else { 241 t.Error("No OSPF layer type found in packet") 242 } 243 } 244 func BenchmarkDecodePacketPacket1(b *testing.B) { 245 for i := 0; i < b.N; i++ { 246 gopacket.NewPacket(testPacketOSPF3DBDesc, LinkTypeEthernet, gopacket.NoCopy) 247 } 248 } 249 250 // testPacketOSPF2LSRequest is the packet: 251 // 13:20:14.418003 IP 192.168.170.2 > 192.168.170.8: OSPFv2, LS-Request, length 36 252 // 253 // 0x0000: 00e0 18b1 0cad 0060 0881 7a70 0800 45c0 .......`..zp..E. 254 // 0x0010: 0038 88c6 0000 0159 5a8b c0a8 aa02 c0a8 .8.....YZ....... 255 // 0x0020: aa08 0203 0024 c0a8 aa03 0000 0001 bdc7 .....$.......... 256 // 0x0030: 0000 0000 0000 0000 0000 0000 0001 c0a8 ................ 257 // 0x0040: aa08 c0a8 aa08 ...... 258 var testPacketOSPF2LSRequest = []byte{ 259 0x00, 0xe0, 0x18, 0xb1, 0x0c, 0xad, 0x00, 0x60, 0x08, 0x81, 0x7a, 0x70, 0x08, 0x00, 0x45, 0xc0, 260 0x00, 0x38, 0x88, 0xc6, 0x00, 0x00, 0x01, 0x59, 0x5a, 0x8b, 0xc0, 0xa8, 0xaa, 0x02, 0xc0, 0xa8, 261 0xaa, 0x08, 0x02, 0x03, 0x00, 0x24, 0xc0, 0xa8, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x01, 0xbd, 0xc7, 262 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0xa8, 263 0xaa, 0x08, 0xc0, 0xa8, 0xaa, 0x08, 264 } 265 266 func TestPacketOSPF2LSRequest(t *testing.T) { 267 p := gopacket.NewPacket(testPacketOSPF2LSRequest, LinkTypeEthernet, gopacket.Default) 268 if p.ErrorLayer() != nil { 269 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 270 } 271 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeOSPF}, t) 272 if got, ok := p.Layer(LayerTypeOSPF).(*OSPFv2); ok { 273 want := &OSPFv2{ 274 OSPF: OSPF{ 275 Version: 2, 276 Type: OSPFLinkStateRequest, 277 PacketLength: 36, 278 RouterID: 0xc0a8aa03, 279 AreaID: 1, 280 Checksum: 0xbdc7, 281 Content: []LSReq{ 282 LSReq{ 283 LSType: 0x1, 284 LSID: 0xc0a8aa08, 285 AdvRouter: 0xc0a8aa08, 286 }, 287 }, 288 }, 289 } 290 if !reflect.DeepEqual(got, want) { 291 t.Errorf("OSPF packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 292 } 293 } else { 294 t.Error("No OSPF layer type found in packet") 295 } 296 } 297 func BenchmarkDecodePacketPacket7(b *testing.B) { 298 for i := 0; i < b.N; i++ { 299 gopacket.NewPacket(testPacketOSPF2LSRequest, LinkTypeEthernet, gopacket.NoCopy) 300 } 301 } 302 303 // testPacketOSPF3LSRequest is the packet: 304 // 305 // 14:43:51.673584 IP6 fe80::2 > fe80::1: OSPFv3, LS-Request, length 100 306 // 0x0000: c200 1ffa 0001 c201 1ffa 0001 86dd 6e00 ..............n. 307 // 0x0010: 0000 0064 5901 fe80 0000 0000 0000 0000 ...dY........... 308 // 0x0020: 0000 0000 0002 fe80 0000 0000 0000 0000 ................ 309 // 0x0030: 0000 0000 0001 0303 0064 0202 0202 0000 .........d...... 310 // 0x0040: 0001 2c9a 0000 0000 2001 0000 0000 0101 ..,............. 311 // 0x0050: 0101 0000 2003 0000 0003 0101 0101 0000 ................ 312 // 0x0060: 2003 0000 0002 0101 0101 0000 2003 0000 ................ 313 // 0x0070: 0001 0101 0101 0000 2003 0000 0000 0101 ................ 314 // 0x0080: 0101 0000 0008 0000 0005 0101 0101 0000 ................ 315 // 0x0090: 2009 0000 0000 0101 0101 .......... 316 var testPacketOSPF3LSRequest = []byte{ 317 0xc2, 0x00, 0x1f, 0xfa, 0x00, 0x01, 0xc2, 0x01, 0x1f, 0xfa, 0x00, 0x01, 0x86, 0xdd, 0x6e, 0x00, 318 0x00, 0x00, 0x00, 0x64, 0x59, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 319 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 320 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x00, 0x64, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 321 0x00, 0x01, 0x2c, 0x9a, 0x00, 0x00, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 322 0x01, 0x01, 0x00, 0x00, 0x20, 0x03, 0x00, 0x00, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 323 0x20, 0x03, 0x00, 0x00, 0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x20, 0x03, 0x00, 0x00, 324 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x20, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 325 0x01, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x05, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 326 0x20, 0x09, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 327 } 328 329 func TestPacketOSPF3LSRequest(t *testing.T) { 330 p := gopacket.NewPacket(testPacketOSPF3LSRequest, LinkTypeEthernet, gopacket.Default) 331 if p.ErrorLayer() != nil { 332 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 333 } 334 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeOSPF}, t) 335 if got, ok := p.Layer(LayerTypeOSPF).(*OSPFv3); ok { 336 want := &OSPFv3{ 337 OSPF: OSPF{ 338 Version: 3, 339 Type: OSPFLinkStateRequest, 340 PacketLength: 100, 341 RouterID: 0x2020202, 342 AreaID: 1, 343 Checksum: 0x2c9a, 344 Content: []LSReq{ 345 LSReq{ 346 LSType: 0x2001, 347 LSID: 0x00000000, 348 AdvRouter: 0x01010101, 349 }, 350 LSReq{ 351 LSType: 0x2003, 352 LSID: 0x00000003, 353 AdvRouter: 0x01010101, 354 }, 355 LSReq{ 356 LSType: 0x2003, 357 LSID: 0x00000002, 358 AdvRouter: 0x01010101, 359 }, 360 LSReq{ 361 LSType: 0x2003, 362 LSID: 0x00000001, 363 AdvRouter: 0x01010101, 364 }, 365 LSReq{ 366 LSType: 0x2003, 367 LSID: 0x00000000, 368 AdvRouter: 0x01010101, 369 }, 370 LSReq{ 371 LSType: 0x0008, 372 LSID: 0x00000005, 373 AdvRouter: 0x01010101, 374 }, 375 LSReq{ 376 LSType: 0x2009, 377 LSID: 0x00000000, 378 AdvRouter: 0x01010101, 379 }, 380 }, 381 }, 382 Instance: 0, 383 Reserved: 0, 384 } 385 if !reflect.DeepEqual(got, want) { 386 t.Errorf("OSPF packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 387 } 388 } else { 389 t.Error("No OSPF layer type found in packet") 390 } 391 } 392 func BenchmarkDecodePacketPacket2(b *testing.B) { 393 for i := 0; i < b.N; i++ { 394 gopacket.NewPacket(testPacketOSPF3LSRequest, LinkTypeEthernet, gopacket.NoCopy) 395 } 396 } 397 398 // testPacketOSPF2LSUpdate is the packet: 399 // 13:20:14.420459 IP 192.168.170.2 > 224.0.0.6: OSPFv2, LS-Update, length 292 400 // 401 // 0x0000: 0100 5e00 0006 0060 0881 7a70 0800 45c0 ..^....`..zp..E. 402 // 0x0010: 0138 3025 0000 0159 3cd7 c0a8 aa02 e000 .80%...Y<....... 403 // 0x0020: 0006 0204 0124 c0a8 aa03 0000 0001 366b .....$........6k 404 // 0x0030: 0000 0000 0000 0000 0000 0000 0007 0002 ................ 405 // 0x0040: 0201 c0a8 aa03 c0a8 aa03 8000 0001 3a9c ..............:. 406 // 0x0050: 0030 0200 0002 c0a8 aa00 ffff ff00 0300 .0.............. 407 // 0x0060: 000a c0a8 aa00 ffff ff00 0300 000a 0003 ................ 408 // 0x0070: 0205 50d4 1000 c0a8 aa02 8000 0001 2a49 ..P...........*I 409 // 0x0080: 0024 ffff ffff 8000 0014 0000 0000 0000 .$.............. 410 // 0x0090: 0000 0003 0205 9479 ab00 c0a8 aa02 8000 .......y........ 411 // 0x00a0: 0001 34a5 0024 ffff ff00 8000 0014 c0a8 ..4..$.......... 412 // 0x00b0: aa01 0000 0000 0003 0205 c082 7800 c0a8 ............x... 413 // 0x00c0: aa02 8000 0001 d319 0024 ffff ff00 8000 .........$...... 414 // 0x00d0: 0014 0000 0000 0000 0000 0003 0205 c0a8 ................ 415 // 0x00e0: 0000 c0a8 aa02 8000 0001 3708 0024 ffff ..........7..$.. 416 // 0x00f0: ff00 8000 0014 0000 0000 0000 0000 0003 ................ 417 // 0x0100: 0205 c0a8 0100 c0a8 aa02 8000 0001 2c12 ..............,. 418 // 0x0110: 0024 ffff ff00 8000 0014 0000 0000 0000 .$.............. 419 // 0x0120: 0000 0003 0205 c0a8 ac00 c0a8 aa02 8000 ................ 420 // 0x0130: 0001 3341 0024 ffff ff00 8000 0014 c0a8 ..3A.$.......... 421 // 0x0140: aa0a 0000 0000 ...... 422 var testPacketOSPF2LSUpdate = []byte{ 423 0x01, 0x00, 0x5e, 0x00, 0x00, 0x06, 0x00, 0x60, 0x08, 0x81, 0x7a, 0x70, 0x08, 0x00, 0x45, 0xc0, 424 0x01, 0x38, 0x30, 0x25, 0x00, 0x00, 0x01, 0x59, 0x3c, 0xd7, 0xc0, 0xa8, 0xaa, 0x02, 0xe0, 0x00, 425 0x00, 0x06, 0x02, 0x04, 0x01, 0x24, 0xc0, 0xa8, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x01, 0x36, 0x6b, 426 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x02, 427 0x02, 0x01, 0xc0, 0xa8, 0xaa, 0x03, 0xc0, 0xa8, 0xaa, 0x03, 0x80, 0x00, 0x00, 0x01, 0x3a, 0x9c, 428 0x00, 0x30, 0x02, 0x00, 0x00, 0x02, 0xc0, 0xa8, 0xaa, 0x00, 0xff, 0xff, 0xff, 0x00, 0x03, 0x00, 429 0x00, 0x0a, 0xc0, 0xa8, 0xaa, 0x00, 0xff, 0xff, 0xff, 0x00, 0x03, 0x00, 0x00, 0x0a, 0x00, 0x03, 430 0x02, 0x05, 0x50, 0xd4, 0x10, 0x00, 0xc0, 0xa8, 0xaa, 0x02, 0x80, 0x00, 0x00, 0x01, 0x2a, 0x49, 431 0x00, 0x24, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 432 0x00, 0x00, 0x00, 0x03, 0x02, 0x05, 0x94, 0x79, 0xab, 0x00, 0xc0, 0xa8, 0xaa, 0x02, 0x80, 0x00, 433 0x00, 0x01, 0x34, 0xa5, 0x00, 0x24, 0xff, 0xff, 0xff, 0x00, 0x80, 0x00, 0x00, 0x14, 0xc0, 0xa8, 434 0xaa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x02, 0x05, 0xc0, 0x82, 0x78, 0x00, 0xc0, 0xa8, 435 0xaa, 0x02, 0x80, 0x00, 0x00, 0x01, 0xd3, 0x19, 0x00, 0x24, 0xff, 0xff, 0xff, 0x00, 0x80, 0x00, 436 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x02, 0x05, 0xc0, 0xa8, 437 0x00, 0x00, 0xc0, 0xa8, 0xaa, 0x02, 0x80, 0x00, 0x00, 0x01, 0x37, 0x08, 0x00, 0x24, 0xff, 0xff, 438 0xff, 0x00, 0x80, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 439 0x02, 0x05, 0xc0, 0xa8, 0x01, 0x00, 0xc0, 0xa8, 0xaa, 0x02, 0x80, 0x00, 0x00, 0x01, 0x2c, 0x12, 440 0x00, 0x24, 0xff, 0xff, 0xff, 0x00, 0x80, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 441 0x00, 0x00, 0x00, 0x03, 0x02, 0x05, 0xc0, 0xa8, 0xac, 0x00, 0xc0, 0xa8, 0xaa, 0x02, 0x80, 0x00, 442 0x00, 0x01, 0x33, 0x41, 0x00, 0x24, 0xff, 0xff, 0xff, 0x00, 0x80, 0x00, 0x00, 0x14, 0xc0, 0xa8, 443 0xaa, 0x0a, 0x00, 0x00, 0x00, 0x00, 444 } 445 446 func TestPacketOSPF2LSUpdate(t *testing.T) { 447 p := gopacket.NewPacket(testPacketOSPF2LSUpdate, LinkTypeEthernet, gopacket.Default) 448 if p.ErrorLayer() != nil { 449 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 450 } 451 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeOSPF}, t) 452 if got, ok := p.Layer(LayerTypeOSPF).(*OSPFv2); ok { 453 want := &OSPFv2{ 454 OSPF: OSPF{ 455 Version: 2, 456 Type: OSPFLinkStateUpdate, 457 PacketLength: 292, 458 RouterID: 0xc0a8aa03, 459 AreaID: 1, 460 Checksum: 0x366b, 461 Content: LSUpdate{ 462 NumOfLSAs: 7, 463 LSAs: []LSA{ 464 LSA{ 465 LSAheader: LSAheader{ 466 LSAge: 0x2, 467 LSType: 0x1, 468 LinkStateID: 0xc0a8aa03, 469 AdvRouter: 0xc0a8aa03, 470 LSSeqNumber: 0x80000001, 471 LSChecksum: 0x3a9c, 472 Length: 0x30, 473 LSOptions: 0x2, 474 }, 475 Content: RouterLSAV2{ 476 Flags: 0x2, 477 Links: 0x2, 478 Routers: []RouterV2{ 479 RouterV2{ 480 LinkID: 0xc0a8aa00, 481 LinkData: 0xffffff00, 482 Type: 0x03, 483 Metric: 0x0a, 484 }, 485 RouterV2{ 486 LinkID: 0xc0a8aa00, 487 LinkData: 0xffffff00, 488 Type: 0x03, 489 Metric: 0x0a, 490 }, 491 }, 492 }, 493 }, 494 LSA{ 495 LSAheader: LSAheader{ 496 LSAge: 0x3, 497 LSType: 0x5, 498 LinkStateID: 0x50d41000, 499 AdvRouter: 0xc0a8aa02, 500 LSSeqNumber: 0x80000001, 501 LSChecksum: 0x2a49, 502 Length: 0x24, 503 LSOptions: 0x2, 504 }, 505 Content: ASExternalLSAV2{ 506 NetworkMask: 0xffffffff, 507 ExternalBit: 0x80, 508 Metric: 0x14, 509 ForwardingAddress: 0x0, 510 ExternalRouteTag: 0x0, 511 }, 512 }, 513 LSA{ 514 LSAheader: LSAheader{ 515 LSAge: 0x3, 516 LSType: 0x5, 517 LinkStateID: 0x9479ab00, 518 AdvRouter: 0xc0a8aa02, 519 LSSeqNumber: 0x80000001, 520 LSChecksum: 0x34a5, 521 Length: 0x24, 522 LSOptions: 0x2, 523 }, 524 Content: ASExternalLSAV2{ 525 NetworkMask: 0xffffff00, 526 ExternalBit: 0x80, 527 Metric: 0x14, 528 ForwardingAddress: 0xc0a8aa01, 529 ExternalRouteTag: 0x0, 530 }, 531 }, 532 LSA{ 533 LSAheader: LSAheader{ 534 LSAge: 0x3, 535 LSType: 0x5, 536 LinkStateID: 0xc0827800, 537 AdvRouter: 0xc0a8aa02, 538 LSSeqNumber: 0x80000001, 539 LSChecksum: 0xd319, 540 Length: 0x24, 541 LSOptions: 0x2, 542 }, 543 Content: ASExternalLSAV2{ 544 NetworkMask: 0xffffff00, 545 ExternalBit: 0x80, 546 Metric: 0x14, 547 ForwardingAddress: 0x0, 548 ExternalRouteTag: 0x0, 549 }, 550 }, 551 LSA{ 552 LSAheader: LSAheader{ 553 LSAge: 0x3, 554 LSType: 0x5, 555 LinkStateID: 0xc0a80000, 556 AdvRouter: 0xc0a8aa02, 557 LSSeqNumber: 0x80000001, 558 LSChecksum: 0x3708, 559 Length: 0x24, 560 LSOptions: 0x2, 561 }, 562 Content: ASExternalLSAV2{ 563 NetworkMask: 0xffffff00, 564 ExternalBit: 0x80, 565 Metric: 0x14, 566 ForwardingAddress: 0x0, 567 ExternalRouteTag: 0x0, 568 }, 569 }, 570 LSA{ 571 LSAheader: LSAheader{ 572 LSAge: 0x3, 573 LSType: 0x5, 574 LinkStateID: 0xc0a80100, 575 AdvRouter: 0xc0a8aa02, 576 LSSeqNumber: 0x80000001, 577 LSChecksum: 0x2c12, 578 Length: 0x24, 579 LSOptions: 0x2, 580 }, 581 Content: ASExternalLSAV2{ 582 NetworkMask: 0xffffff00, 583 ExternalBit: 0x80, 584 Metric: 0x14, 585 ForwardingAddress: 0x0, 586 ExternalRouteTag: 0x0, 587 }, 588 }, 589 LSA{ 590 LSAheader: LSAheader{ 591 LSAge: 0x3, 592 LSType: 0x5, 593 LinkStateID: 0xc0a8ac00, 594 AdvRouter: 0xc0a8aa02, 595 LSSeqNumber: 0x80000001, 596 LSChecksum: 0x3341, 597 Length: 0x24, 598 LSOptions: 0x2, 599 }, 600 Content: ASExternalLSAV2{ 601 NetworkMask: 0xffffff00, 602 ExternalBit: 0x80, 603 Metric: 0x14, 604 ForwardingAddress: 0xc0a8aa0a, 605 ExternalRouteTag: 0x0, 606 }, 607 }, 608 }, 609 }, 610 }, 611 } 612 if !reflect.DeepEqual(got, want) { 613 t.Errorf("OSPF packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 614 } 615 } else { 616 t.Error("No OSPF layer type found in packet") 617 } 618 } 619 func BenchmarkDecodePacketPacket8(b *testing.B) { 620 for i := 0; i < b.N; i++ { 621 gopacket.NewPacket(testPacketOSPF2LSUpdate, LinkTypeEthernet, gopacket.NoCopy) 622 } 623 } 624 625 // testPacketOSPF2LSUpdateLSA2 is the packet: 626 // 00:50:17.836469 IP 172.24.27.86 > ospf-all.mcast.net: OSPFv2, LS-Update, length 104 627 // 628 // 0x0000: 0100 5e00 0005 d4e8 80c2 b1c9 8100 017f 629 // 0x0010: 0800 45c0 008c f7e4 0000 0159 1901 ac18 630 // 0x0020: 1b56 e000 0005 0204 0058 ac18 0446 0000 631 // 0x0030: 000c 0000 0000 0000 0000 0000 0000 0000 632 // 0x0040: 0002 0001 2801 ac18 0446 ac18 0446 8000 633 // 0x0050: 041a 6025 0024 0000 0001 ac18 1b56 ac18 634 // 0x0060: 1b56 0200 0001 0001 2802 ac18 1b56 ac18 635 // 0x0070: 0446 8000 041f 390e 0028 ffff fff8 ac18 636 // 0x0080: 0446 ac18 0445 ac18 1b53 ac18 1b54 637 var testPacketOSPF2LSUpdateLSA2 = []byte{ 638 0x01, 0x00, 0x5e, 0x00, 0x00, 0x05, 0xd4, 0xe8, 0x80, 0xc2, 0xb1, 0xc9, 0x81, 0x00, 0x01, 0x7f, 639 0x08, 0x00, 0x45, 0xc0, 0x00, 0x8c, 0xf7, 0xe4, 0x00, 0x00, 0x01, 0x59, 0x19, 0x01, 0xac, 0x18, 640 0x1b, 0x56, 0xe0, 0x00, 0x00, 0x05, 0x02, 0x04, 0x00, 0x58, 0xac, 0x18, 0x04, 0x46, 0x00, 0x00, 641 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 642 0x00, 0x02, 0x00, 0x01, 0x28, 0x01, 0xac, 0x18, 0x04, 0x46, 0xac, 0x18, 0x04, 0x46, 0x80, 0x00, 643 0x04, 0x1a, 0x60, 0x25, 0x00, 0x24, 0x00, 0x00, 0x00, 0x01, 0xac, 0x18, 0x1b, 0x56, 0xac, 0x18, 644 0x1b, 0x56, 0x02, 0x00, 0x00, 0x01, 0x00, 0x01, 0x28, 0x02, 0xac, 0x18, 0x1b, 0x56, 0xac, 0x18, 645 0x04, 0x46, 0x80, 0x00, 0x04, 0x1f, 0x39, 0x0e, 0x00, 0x28, 0xff, 0xff, 0xff, 0xf8, 0xac, 0x18, 646 0x04, 0x46, 0xac, 0x18, 0x04, 0x45, 0xac, 0x18, 0x1b, 0x53, 0xac, 0x18, 0x1b, 0x54, 647 } 648 649 func TestPacketOSPF2LSUpdateLSA2(t *testing.T) { 650 p := gopacket.NewPacket(testPacketOSPF2LSUpdateLSA2, LinkTypeEthernet, gopacket.Default) 651 if p.ErrorLayer() != nil { 652 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 653 } 654 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeDot1Q, LayerTypeIPv4, LayerTypeOSPF}, t) 655 if got, ok := p.Layer(LayerTypeOSPF).(*OSPFv2); ok { 656 want := &OSPFv2{ 657 OSPF: OSPF{ 658 Version: 2, 659 Type: OSPFLinkStateUpdate, 660 PacketLength: 88, 661 RouterID: 0xac180446, 662 AreaID: 12, 663 Checksum: 0x0000, 664 Content: LSUpdate{ 665 NumOfLSAs: 2, 666 LSAs: []LSA{ 667 LSA{ 668 LSAheader: LSAheader{ 669 LSAge: 0x1, 670 LSType: 0x1, 671 LinkStateID: 0xac180446, 672 AdvRouter: 0xac180446, 673 LSSeqNumber: 0x8000041a, 674 LSChecksum: 0x6025, 675 Length: 0x24, 676 LSOptions: 0x28, 677 }, 678 Content: RouterLSAV2{ 679 Flags: 0x0, 680 Links: 0x1, 681 Routers: []RouterV2{ 682 RouterV2{ 683 LinkID: 0xac181b56, 684 LinkData: 0xac181b56, 685 Type: 0x02, 686 Metric: 0x01, 687 }, 688 }, 689 }, 690 }, 691 LSA{ 692 LSAheader: LSAheader{ 693 LSAge: 0x1, 694 LSType: 0x2, 695 LinkStateID: 0xac181b56, 696 AdvRouter: 0xac180446, 697 LSSeqNumber: 0x8000041f, 698 LSChecksum: 0x390e, 699 Length: 0x28, 700 LSOptions: 0x28, 701 }, 702 Content: NetworkLSAV2{ 703 NetworkMask: 0xfffffff8, 704 AttachedRouter: []uint32{ 705 0xac180446, 706 0xac180445, 707 0xac181b53, 708 0xac181b54, 709 }, 710 }, 711 }, 712 }, 713 }, 714 }, 715 } 716 if !reflect.DeepEqual(got, want) { 717 t.Errorf("OSPF packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 718 } 719 } else { 720 t.Error("No OSPF layer type found in packet") 721 } 722 } 723 724 // testPacketOSPF2LSUpdateLSA7 is the packet: 725 // 00:00:00.337386 IP 172.24.27.84 > ospf-dsig.mcast.net: OSPFv2, LS-Update, length 100 726 // 727 // 0x0000: 0100 5e00 0006 001a 1e02 81a8 8100 017f 728 // 0x0010: 0800 45c0 0088 0000 4000 0159 d0ea ac18 729 // 0x0020: 1b54 e000 0006 0204 0054 ac18 1b54 0000 730 // 0x0030: 000c 0000 0000 0000 0000 0000 0000 0000 731 // 0x0040: 0002 0001 0807 0ac8 4f60 ac18 1b54 8000 732 // 0x0050: 03a6 4d08 0024 ffff ffe0 8000 0100 ac18 733 // 0x0060: 1b54 0000 0000 0001 0807 0a80 9300 ac18 734 // 0x0070: 1b54 8000 03a6 43d6 0024 ffff ff80 8000 735 // 0x0080: 0100 ac18 1b54 0000 0000 736 var testPacketOSPF2LSUpdateLSA7 = []byte{ 737 0x01, 0x00, 0x5e, 0x00, 0x00, 0x06, 0x00, 0x1a, 0x1e, 0x02, 0x81, 0xa8, 0x81, 0x00, 0x01, 0x7f, 738 0x08, 0x00, 0x45, 0xc0, 0x00, 0x88, 0x00, 0x00, 0x40, 0x00, 0x01, 0x59, 0xd0, 0xea, 0xac, 0x18, 739 0x1b, 0x54, 0xe0, 0x00, 0x00, 0x06, 0x02, 0x04, 0x00, 0x54, 0xac, 0x18, 0x1b, 0x54, 0x00, 0x00, 740 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 741 0x00, 0x02, 0x00, 0x01, 0x08, 0x07, 0x0a, 0xc8, 0x4f, 0x60, 0xac, 0x18, 0x1b, 0x54, 0x80, 0x00, 742 0x03, 0xa6, 0x4d, 0x08, 0x00, 0x24, 0xff, 0xff, 0xff, 0xe0, 0x80, 0x00, 0x01, 0x00, 0xac, 0x18, 743 0x1b, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x07, 0x0a, 0x80, 0x93, 0x00, 0xac, 0x18, 744 0x1b, 0x54, 0x80, 0x00, 0x03, 0xa6, 0x43, 0xd6, 0x00, 0x24, 0xff, 0xff, 0xff, 0x80, 0x80, 0x00, 745 0x01, 0x00, 0xac, 0x18, 0x1b, 0x54, 0x00, 0x00, 0x00, 0x00, 746 } 747 748 func TestPacketOSPF2LSUpdateLSA7(t *testing.T) { 749 p := gopacket.NewPacket(testPacketOSPF2LSUpdateLSA7, LinkTypeEthernet, gopacket.Default) 750 if p.ErrorLayer() != nil { 751 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 752 } 753 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeDot1Q, LayerTypeIPv4, LayerTypeOSPF}, t) 754 if got, ok := p.Layer(LayerTypeOSPF).(*OSPFv2); ok { 755 want := &OSPFv2{ 756 OSPF: OSPF{ 757 Version: 2, 758 Type: OSPFLinkStateUpdate, 759 PacketLength: 84, 760 RouterID: 0xac181b54, 761 AreaID: 12, 762 Checksum: 0x0, 763 Content: LSUpdate{ 764 NumOfLSAs: 2, 765 LSAs: []LSA{ 766 LSA{ 767 LSAheader: LSAheader{ 768 LSAge: 0x1, 769 LSType: 0x7, 770 LinkStateID: 0x0ac84f60, 771 AdvRouter: 0xac181b54, 772 LSSeqNumber: 0x800003a6, 773 LSChecksum: 0x4d08, 774 Length: 0x24, 775 LSOptions: 0x08, 776 }, 777 Content: ASExternalLSAV2{ 778 NetworkMask: 0xffffffe0, 779 ExternalBit: 0x80, 780 Metric: 0x0100, 781 ForwardingAddress: 0xac181b54, 782 ExternalRouteTag: 0x0, 783 }, 784 }, 785 LSA{ 786 LSAheader: LSAheader{ 787 LSAge: 0x1, 788 LSType: 0x7, 789 LinkStateID: 0x0a809300, 790 AdvRouter: 0xac181b54, 791 LSSeqNumber: 0x800003a6, 792 LSChecksum: 0x43d6, 793 Length: 0x24, 794 LSOptions: 0x08, 795 }, 796 Content: ASExternalLSAV2{ 797 NetworkMask: 0xffffff80, 798 ExternalBit: 0x80, 799 Metric: 0x0100, 800 ForwardingAddress: 0xac181b54, 801 ExternalRouteTag: 0x0, 802 }, 803 }, 804 }, 805 }, 806 }, 807 } 808 if !reflect.DeepEqual(got, want) { 809 t.Errorf("OSPF packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 810 } 811 } else { 812 t.Error("No OSPF layer type found in packet") 813 } 814 } 815 816 // testPacketOSPF3LSUpdate is the packet: 817 // 818 // 14:43:51.681554 IP6 fe80::1 > fe80::2: OSPFv3, LS-Update, length 288 819 // 0x0000: c201 1ffa 0001 c200 1ffa 0001 86dd 6e00 ..............n. 820 // 0x0010: 0000 0120 5901 fe80 0000 0000 0000 0000 ....Y........... 821 // 0x0020: 0000 0000 0001 fe80 0000 0000 0000 0000 ................ 822 // 0x0030: 0000 0000 0002 0304 0120 0101 0101 0000 ................ 823 // 0x0040: 0001 e556 0000 0000 0007 0028 2001 0000 ...V.......(.... 824 // 0x0050: 0000 0101 0101 8000 0002 d13a 0018 0100 ...........:.... 825 // 0x0060: 0033 0029 2003 0000 0003 0101 0101 8000 .3.)............ 826 // 0x0070: 0001 6259 0024 0000 004a 4000 0000 2001 ..bY.$...J@..... 827 // 0x0080: 0db8 0000 0003 0029 2003 0000 0002 0101 .......)........ 828 // 0x0090: 0101 8000 0001 baf6 0024 0000 0054 4000 .........$...T@. 829 // 0x00a0: 0000 2001 0db8 0000 0004 0029 2003 0000 ...........).... 830 // 0x00b0: 0001 0101 0101 8000 0001 eba0 0024 0000 .............$.. 831 // 0x00c0: 004a 4000 0000 2001 0db8 0000 0034 0029 .J@..........4.) 832 // 0x00d0: 2003 0000 0000 0101 0101 8000 0001 0ebd ................ 833 // 0x00e0: 0024 0000 0040 4000 0000 2001 0db8 0000 .$...@@......... 834 // 0x00f0: 0000 0023 0008 0000 0005 0101 0101 8000 ...#............ 835 // 0x0100: 0002 3d08 0038 0100 0033 fe80 0000 0000 ..=..8...3...... 836 // 0x0110: 0000 0000 0000 0000 0001 0000 0001 4000 ..............@. 837 // 0x0120: 0000 2001 0db8 0000 0012 0023 2009 0000 ...........#.... 838 // 0x0130: 0000 0101 0101 8000 0001 e8d2 002c 0001 .............,.. 839 // 0x0140: 2001 0000 0000 0101 0101 4000 000a 2001 ..........@..... 840 // 0x0150: 0db8 0000 0012 ...... 841 var testPacketOSPF3LSUpdate = []byte{ 842 0xc2, 0x01, 0x1f, 0xfa, 0x00, 0x01, 0xc2, 0x00, 0x1f, 0xfa, 0x00, 0x01, 0x86, 0xdd, 0x6e, 0x00, 843 0x00, 0x00, 0x01, 0x20, 0x59, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 844 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 845 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x04, 0x01, 0x20, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 846 0x00, 0x01, 0xe5, 0x56, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x28, 0x20, 0x01, 0x00, 0x00, 847 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x80, 0x00, 0x00, 0x02, 0xd1, 0x3a, 0x00, 0x18, 0x01, 0x00, 848 0x00, 0x33, 0x00, 0x29, 0x20, 0x03, 0x00, 0x00, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x80, 0x00, 849 0x00, 0x01, 0x62, 0x59, 0x00, 0x24, 0x00, 0x00, 0x00, 0x4a, 0x40, 0x00, 0x00, 0x00, 0x20, 0x01, 850 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x03, 0x00, 0x29, 0x20, 0x03, 0x00, 0x00, 0x00, 0x02, 0x01, 0x01, 851 0x01, 0x01, 0x80, 0x00, 0x00, 0x01, 0xba, 0xf6, 0x00, 0x24, 0x00, 0x00, 0x00, 0x54, 0x40, 0x00, 852 0x00, 0x00, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x04, 0x00, 0x29, 0x20, 0x03, 0x00, 0x00, 853 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x80, 0x00, 0x00, 0x01, 0xeb, 0xa0, 0x00, 0x24, 0x00, 0x00, 854 0x00, 0x4a, 0x40, 0x00, 0x00, 0x00, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x34, 0x00, 0x29, 855 0x20, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x80, 0x00, 0x00, 0x01, 0x0e, 0xbd, 856 0x00, 0x24, 0x00, 0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x00, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 857 0x00, 0x00, 0x00, 0x23, 0x00, 0x08, 0x00, 0x00, 0x00, 0x05, 0x01, 0x01, 0x01, 0x01, 0x80, 0x00, 858 0x00, 0x02, 0x3d, 0x08, 0x00, 0x38, 0x01, 0x00, 0x00, 0x33, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 859 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x40, 0x00, 860 0x00, 0x00, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x12, 0x00, 0x23, 0x20, 0x09, 0x00, 0x00, 861 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x80, 0x00, 0x00, 0x01, 0xe8, 0xd2, 0x00, 0x2c, 0x00, 0x01, 862 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x40, 0x00, 0x00, 0x0a, 0x20, 0x01, 863 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x12, 864 } 865 866 func TestPacketOSPF3LSUpdate(t *testing.T) { 867 p := gopacket.NewPacket(testPacketOSPF3LSUpdate, LinkTypeEthernet, gopacket.Default) 868 if p.ErrorLayer() != nil { 869 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 870 } 871 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeOSPF}, t) 872 if got, ok := p.Layer(LayerTypeOSPF).(*OSPFv3); ok { 873 want := &OSPFv3{ 874 OSPF: OSPF{ 875 Version: 3, 876 Type: OSPFLinkStateUpdate, 877 PacketLength: 288, 878 RouterID: 0x1010101, 879 AreaID: 1, 880 Checksum: 0xe556, 881 Content: LSUpdate{ 882 NumOfLSAs: 7, 883 LSAs: []LSA{ 884 LSA{ 885 LSAheader: LSAheader{ 886 LSAge: 40, 887 LSType: 0x2001, 888 LinkStateID: 0x00000000, 889 AdvRouter: 0x01010101, 890 LSSeqNumber: 0x80000002, 891 LSChecksum: 0xd13a, 892 Length: 24, 893 }, 894 Content: RouterLSA{ 895 Flags: 0x1, 896 Options: 0x33, 897 }, 898 }, 899 LSA{ 900 LSAheader: LSAheader{ 901 LSAge: 41, 902 LSType: 0x2003, 903 LinkStateID: 0x00000003, 904 AdvRouter: 0x01010101, 905 LSSeqNumber: 0x80000001, 906 LSChecksum: 0x6259, 907 Length: 36, 908 }, 909 Content: InterAreaPrefixLSA{ 910 Metric: 74, 911 PrefixLength: 64, 912 PrefixOptions: 0, 913 AddressPrefix: []byte{0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x03}, 914 }, 915 }, 916 LSA{ 917 LSAheader: LSAheader{ 918 LSAge: 41, 919 LSType: 0x2003, 920 LinkStateID: 0x00000002, 921 AdvRouter: 0x01010101, 922 LSSeqNumber: 0x80000001, 923 LSChecksum: 0xbaf6, 924 Length: 36, 925 }, 926 Content: InterAreaPrefixLSA{ 927 Metric: 84, 928 PrefixLength: 64, 929 PrefixOptions: 0, 930 AddressPrefix: []byte{0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x4}, 931 }, 932 }, 933 LSA{ 934 LSAheader: LSAheader{ 935 LSAge: 41, 936 LSType: 0x2003, 937 LinkStateID: 0x00000001, 938 AdvRouter: 0x01010101, 939 LSSeqNumber: 0x80000001, 940 LSChecksum: 0xeba0, 941 Length: 36, 942 }, 943 Content: InterAreaPrefixLSA{ 944 Metric: 74, 945 PrefixLength: 64, 946 PrefixOptions: 0, 947 AddressPrefix: []byte{0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x34}, 948 }, 949 }, 950 LSA{ 951 LSAheader: LSAheader{ 952 LSAge: 41, 953 LSType: 0x2003, 954 LinkStateID: 0x00000000, 955 AdvRouter: 0x01010101, 956 LSSeqNumber: 0x80000001, 957 LSChecksum: 0xebd, 958 Length: 36, 959 }, 960 Content: InterAreaPrefixLSA{ 961 Metric: 64, 962 PrefixLength: 64, 963 PrefixOptions: 0, 964 AddressPrefix: []byte{0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0}, 965 }, 966 }, 967 LSA{ 968 LSAheader: LSAheader{ 969 LSAge: 35, 970 LSType: 0x8, 971 LinkStateID: 0x00000005, 972 AdvRouter: 0x01010101, 973 LSSeqNumber: 0x80000002, 974 LSChecksum: 0x3d08, 975 Length: 56, 976 }, 977 Content: LinkLSA{ 978 RtrPriority: 1, 979 Options: 0x33, 980 LinkLocalAddress: []byte{0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, 981 NumOfPrefixes: 1, 982 Prefixes: []Prefix{ 983 Prefix{ 984 PrefixLength: 64, 985 PrefixOptions: 0, 986 AddressPrefix: []byte{0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x12}, 987 }, 988 }, 989 }, 990 }, 991 LSA{ 992 LSAheader: LSAheader{ 993 LSAge: 35, 994 LSType: 0x2009, 995 LinkStateID: 0x00000000, 996 AdvRouter: 0x01010101, 997 LSSeqNumber: 0x80000001, 998 LSChecksum: 0xe8d2, 999 Length: 44, 1000 }, 1001 Content: IntraAreaPrefixLSA{ 1002 NumOfPrefixes: 1, 1003 RefLSType: 0x2001, 1004 RefAdvRouter: 0x01010101, 1005 Prefixes: []Prefix{ 1006 Prefix{ 1007 PrefixLength: 64, 1008 PrefixOptions: 0, 1009 Metric: 10, 1010 AddressPrefix: []byte{0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x12}, 1011 }, 1012 }, 1013 }, 1014 }, 1015 }, 1016 }, 1017 }, 1018 Instance: 0, 1019 Reserved: 0, 1020 } 1021 if !reflect.DeepEqual(got, want) { 1022 t.Errorf("OSPF packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 1023 } 1024 } else { 1025 t.Error("No OSPF layer type found in packet") 1026 } 1027 } 1028 func BenchmarkDecodePacketPacket3(b *testing.B) { 1029 for i := 0; i < b.N; i++ { 1030 gopacket.NewPacket(testPacketOSPF3LSUpdate, LinkTypeEthernet, gopacket.NoCopy) 1031 } 1032 } 1033 1034 // testPacketOSPF2LSAck is the packet: 1035 // 13:20:20.858322 IP 192.168.170.8 > 192.168.170.2: OSPFv2, LS-Ack, length 44 1036 // 1037 // 0x0000: 0060 0881 7a70 00e0 18b1 0cad 0800 45c0 .`..zp........E. 1038 // 0x0010: 0040 2bea 0000 0159 b75f c0a8 aa08 c0a8 .@+....Y._...... 1039 // 0x0020: aa02 0205 002c c0a8 aa08 0000 0001 e2f4 .....,.......... 1040 // 0x0030: 0000 0000 0000 0000 0000 0e10 0201 c0a8 ................ 1041 // 0x0040: aa02 c0a8 aa02 8000 0001 4a8e 0030 ..........J..0 1042 var testPacketOSPF2LSAck = []byte{ 1043 0x00, 0x60, 0x08, 0x81, 0x7a, 0x70, 0x00, 0xe0, 0x18, 0xb1, 0x0c, 0xad, 0x08, 0x00, 0x45, 0xc0, 1044 0x00, 0x40, 0x2b, 0xea, 0x00, 0x00, 0x01, 0x59, 0xb7, 0x5f, 0xc0, 0xa8, 0xaa, 0x08, 0xc0, 0xa8, 1045 0xaa, 0x02, 0x02, 0x05, 0x00, 0x2c, 0xc0, 0xa8, 0xaa, 0x08, 0x00, 0x00, 0x00, 0x01, 0xe2, 0xf4, 1046 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x10, 0x02, 0x01, 0xc0, 0xa8, 1047 0xaa, 0x02, 0xc0, 0xa8, 0xaa, 0x02, 0x80, 0x00, 0x00, 0x01, 0x4a, 0x8e, 0x00, 0x30, 1048 } 1049 1050 func TestPacketOSPF2LSAck(t *testing.T) { 1051 p := gopacket.NewPacket(testPacketOSPF2LSAck, LinkTypeEthernet, gopacket.Default) 1052 if p.ErrorLayer() != nil { 1053 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 1054 } 1055 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeOSPF}, t) 1056 if got, ok := p.Layer(LayerTypeOSPF).(*OSPFv2); ok { 1057 want := &OSPFv2{ 1058 OSPF: OSPF{ 1059 Version: 2, 1060 Type: OSPFLinkStateAcknowledgment, 1061 PacketLength: 44, 1062 RouterID: 0xc0a8aa08, 1063 AreaID: 1, 1064 Checksum: 0xe2f4, 1065 Content: []LSAheader{ 1066 LSAheader{ 1067 LSAge: 0xe10, 1068 LSType: 0x1, 1069 LinkStateID: 0xc0a8aa02, 1070 AdvRouter: 0xc0a8aa02, 1071 LSSeqNumber: 0x80000001, 1072 LSChecksum: 0x4a8e, 1073 Length: 0x30, 1074 LSOptions: 0x2, 1075 }, 1076 }, 1077 }, 1078 } 1079 if !reflect.DeepEqual(got, want) { 1080 t.Errorf("OSPF packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 1081 } 1082 } else { 1083 t.Error("No OSPF layer type found in packet") 1084 } 1085 } 1086 func BenchmarkDecodePacketPacket9(b *testing.B) { 1087 for i := 0; i < b.N; i++ { 1088 gopacket.NewPacket(testPacketOSPF3LSAck, LinkTypeEthernet, gopacket.NoCopy) 1089 } 1090 } 1091 1092 // testPacketOSPF3LSAck is the packet: 1093 // 1094 // 14:43:54.185384 IP6 fe80::1 > ff02::5: OSPFv3, LS-Ack, length 136 1095 // 0x0000: 3333 0000 0005 c200 1ffa 0001 86dd 6e00 33............n. 1096 // 0x0010: 0000 0088 5901 fe80 0000 0000 0000 0000 ....Y........... 1097 // 0x0020: 0000 0000 0001 ff02 0000 0000 0000 0000 ................ 1098 // 0x0030: 0000 0000 0005 0305 0088 0101 0101 0000 ................ 1099 // 0x0040: 0001 9d2c 0000 0005 2001 0000 0000 0202 ...,............ 1100 // 0x0050: 0202 8000 0002 b354 0018 0006 2003 0000 .......T........ 1101 // 0x0060: 0003 0202 0202 8000 0001 4473 0024 0006 ..........Ds.$.. 1102 // 0x0070: 2003 0000 0002 0202 0202 8000 0001 9c11 ................ 1103 // 0x0080: 0024 0006 2003 0000 0001 0202 0202 8000 .$.............. 1104 // 0x0090: 0001 cdba 0024 0006 2003 0000 0000 0202 .....$.......... 1105 // 0x00a0: 0202 8000 0001 efd7 0024 0005 0008 0000 .........$...... 1106 // 0x00b0: 0005 0202 0202 8000 0001 5433 002c ..........T3., 1107 var testPacketOSPF3LSAck = []byte{ 1108 0x33, 0x33, 0x00, 0x00, 0x00, 0x05, 0xc2, 0x00, 0x1f, 0xfa, 0x00, 0x01, 0x86, 0xdd, 0x6e, 0x00, 1109 0x00, 0x00, 0x00, 0x88, 0x59, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1110 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1111 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x03, 0x05, 0x00, 0x88, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 1112 0x00, 0x01, 0x9d, 0x2c, 0x00, 0x00, 0x00, 0x05, 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 1113 0x02, 0x02, 0x80, 0x00, 0x00, 0x02, 0xb3, 0x54, 0x00, 0x18, 0x00, 0x06, 0x20, 0x03, 0x00, 0x00, 1114 0x00, 0x03, 0x02, 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x01, 0x44, 0x73, 0x00, 0x24, 0x00, 0x06, 1115 0x20, 0x03, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x01, 0x9c, 0x11, 1116 0x00, 0x24, 0x00, 0x06, 0x20, 0x03, 0x00, 0x00, 0x00, 0x01, 0x02, 0x02, 0x02, 0x02, 0x80, 0x00, 1117 0x00, 0x01, 0xcd, 0xba, 0x00, 0x24, 0x00, 0x06, 0x20, 0x03, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 1118 0x02, 0x02, 0x80, 0x00, 0x00, 0x01, 0xef, 0xd7, 0x00, 0x24, 0x00, 0x05, 0x00, 0x08, 0x00, 0x00, 1119 0x00, 0x05, 0x02, 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x01, 0x54, 0x33, 0x00, 0x2c, 1120 } 1121 1122 func TestPacketOSPF3LSAck(t *testing.T) { 1123 p := gopacket.NewPacket(testPacketOSPF3LSAck, LinkTypeEthernet, gopacket.Default) 1124 if p.ErrorLayer() != nil { 1125 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 1126 } 1127 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeOSPF}, t) 1128 if got, ok := p.Layer(LayerTypeOSPF).(*OSPFv3); ok { 1129 want := &OSPFv3{ 1130 OSPF: OSPF{ 1131 Version: 3, 1132 Type: OSPFLinkStateAcknowledgment, 1133 PacketLength: 136, 1134 RouterID: 0x1010101, 1135 AreaID: 1, 1136 Checksum: 0x9d2c, 1137 Content: []LSAheader{ 1138 LSAheader{ 1139 LSAge: 5, 1140 LSType: 0x2001, 1141 LinkStateID: 0x00000000, 1142 AdvRouter: 0x02020202, 1143 LSSeqNumber: 0x80000002, 1144 LSChecksum: 0xb354, 1145 Length: 24, 1146 }, 1147 LSAheader{ 1148 LSAge: 6, 1149 LSType: 0x2003, 1150 LinkStateID: 0x00000003, 1151 AdvRouter: 0x02020202, 1152 LSSeqNumber: 0x80000001, 1153 LSChecksum: 0x4473, 1154 Length: 36, 1155 }, 1156 LSAheader{ 1157 LSAge: 6, 1158 LSType: 0x2003, 1159 LinkStateID: 0x00000002, 1160 AdvRouter: 0x02020202, 1161 LSSeqNumber: 0x80000001, 1162 LSChecksum: 0x9c11, 1163 Length: 36, 1164 }, 1165 LSAheader{ 1166 LSAge: 6, 1167 LSType: 0x2003, 1168 LinkStateID: 0x00000001, 1169 AdvRouter: 0x02020202, 1170 LSSeqNumber: 0x80000001, 1171 LSChecksum: 0xcdba, 1172 Length: 36, 1173 }, 1174 LSAheader{ 1175 LSAge: 6, 1176 LSType: 0x2003, 1177 LinkStateID: 0x00000000, 1178 AdvRouter: 0x02020202, 1179 LSSeqNumber: 0x80000001, 1180 LSChecksum: 0xefd7, 1181 Length: 36, 1182 }, 1183 LSAheader{ 1184 LSAge: 5, 1185 LSType: 0x0008, 1186 LinkStateID: 0x00000005, 1187 AdvRouter: 0x02020202, 1188 LSSeqNumber: 0x80000001, 1189 LSChecksum: 0x5433, 1190 Length: 44, 1191 }, 1192 }, 1193 }, 1194 Instance: 0, 1195 Reserved: 0, 1196 } 1197 if !reflect.DeepEqual(got, want) { 1198 t.Errorf("OSPF packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 1199 } 1200 } else { 1201 t.Error("No OSPF layer type found in packet") 1202 } 1203 } 1204 1205 var testPacketOSPFInvalidLSA = []byte{ 1206 0x5c, 0xfa, 0x00, 0x00, 0xfa, 0x6c, 0xbd, 0xe4, 0x19, 0x72, 0x1d, 0x1f, 1207 0x08, 0x00, 0x45, 0x00, 0x00, 0xad, 0x00, 0x00, 0x40, 0x00, 0x40, 0x59, 1208 0x6e, 0xef, 0x6b, 0x96, 0x61, 0x11, 0xb4, 0x96, 0xc0, 0xa8, 0x02, 0x04, 1209 0x00, 0xa2, 0x80, 0x00, 0x01, 0x1f, 0x08, 0x00, 0x45, 0x00, 0x6f, 0x76, 1210 0x65, 0x72, 0x66, 0x6c, 0x7f, 0xff, 0x12, 0x12, 0x00, 0x5a, 0xf6, 0x73, 1211 0xa2, 0xba, 0x81, 0x90, 0x00, 0x01, 0x40, 0x6f, 0x77, 0x20, 0x6f, 0x6e, 1212 0x20, 0x63, 0x68, 0x61, 0x72, 0x61, 0x63, 0x74, 0x00, 0x00, 0x00, 0x01, 1213 0x96, 0x20, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x20, 0x00, 0x6e, 0x00, 0x6f, 1214 0x77, 1215 } 1216 1217 func TestPacketOSPFInvalidLSA(t *testing.T) { 1218 p := gopacket.NewPacket(testPacketOSPFInvalidLSA, LinkTypeEthernet, gopacket.Default) 1219 if p.ErrorLayer() == nil { 1220 t.Error("Bad packet decoded successfully") 1221 } 1222 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, gopacket.LayerTypeDecodeFailure}, t) 1223 } 1224 1225 func BenchmarkDecodePacketPacket4(b *testing.B) { 1226 for i := 0; i < b.N; i++ { 1227 gopacket.NewPacket(testPacketOSPF3LSAck, LinkTypeEthernet, gopacket.NoCopy) 1228 } 1229 }