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