github.com/gopacket/gopacket@v1.1.0/layers/gre_test.go (about) 1 // Copyright 2012 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 8 import ( 9 "fmt" 10 "net" 11 "reflect" 12 "testing" 13 14 "github.com/gopacket/gopacket" 15 ) 16 17 // testPacketGRE is the packet: 18 // 19 // 15:08:08.003196 IP 192.168.1.1 > 192.168.1.2: GREv0, length 88: IP 172.16.1.1 > 172.16.2.1: ICMP echo request, id 4724, seq 1, length 64 20 // 0x0000: 3a56 6b69 595e 8e7a 12c3 a971 0800 4500 :VkiY^.z...q..E. 21 // 0x0010: 006c 843c 4000 402f 32d3 c0a8 0101 c0a8 .l.<@.@/2....... 22 // 0x0020: 0102 0000 0800 4500 0054 0488 4000 4001 ......E..T..@.@. 23 // 0x0030: dafe ac10 0101 ac10 0201 0800 82c4 1274 ...............t 24 // 0x0040: 0001 c892 a354 0000 0000 380c 0000 0000 .....T....8..... 25 // 0x0050: 0000 1011 1213 1415 1617 1819 1a1b 1c1d ................ 26 // 0x0060: 1e1f 2021 2223 2425 2627 2829 2a2b 2c2d ...!"#$%&'()*+,- 27 // 0x0070: 2e2f 3031 3233 3435 3637 ./01234567 28 var testPacketGRE = []byte{ 29 0x3a, 0x56, 0x6b, 0x69, 0x59, 0x5e, 0x8e, 0x7a, 0x12, 0xc3, 0xa9, 0x71, 0x08, 0x00, 0x45, 0x00, 30 0x00, 0x6c, 0x84, 0x3c, 0x40, 0x00, 0x40, 0x2f, 0x32, 0xd3, 0xc0, 0xa8, 0x01, 0x01, 0xc0, 0xa8, 31 0x01, 0x02, 0x00, 0x00, 0x08, 0x00, 0x45, 0x00, 0x00, 0x54, 0x04, 0x88, 0x40, 0x00, 0x40, 0x01, 32 0xda, 0xfe, 0xac, 0x10, 0x01, 0x01, 0xac, 0x10, 0x02, 0x01, 0x08, 0x00, 0x82, 0xc4, 0x12, 0x74, 33 0x00, 0x01, 0xc8, 0x92, 0xa3, 0x54, 0x00, 0x00, 0x00, 0x00, 0x38, 0x0c, 0x00, 0x00, 0x00, 0x00, 34 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 35 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 36 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 37 } 38 39 func TestPacketGRE(t *testing.T) { 40 p := gopacket.NewPacket(testPacketGRE, LinkTypeEthernet, gopacket.Default) 41 if p.ErrorLayer() != nil { 42 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 43 } 44 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeGRE, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t) 45 if got, ok := p.Layer(LayerTypeGRE).(*GRE); ok { 46 want := &GRE{ 47 BaseLayer: BaseLayer{testPacketGRE[34:38], testPacketGRE[38:]}, 48 Protocol: EthernetTypeIPv4, 49 } 50 if !reflect.DeepEqual(want, got) { 51 t.Errorf("GRE layer mismatch, \nwant %#v\ngot %#v\n", want, got) 52 } 53 } 54 } 55 56 func BenchmarkDecodePacketGRE(b *testing.B) { 57 for i := 0; i < b.N; i++ { 58 gopacket.NewPacket(testPacketGRE, LinkTypeEthernet, gopacket.NoCopy) 59 } 60 } 61 62 var testIPv4OverGRE = []gopacket.SerializableLayer{ 63 &Ethernet{ 64 SrcMAC: net.HardwareAddr{142, 122, 18, 195, 169, 113}, 65 DstMAC: net.HardwareAddr{58, 86, 107, 105, 89, 94}, 66 EthernetType: EthernetTypeIPv4, 67 }, 68 &IPv4{ 69 Version: 4, 70 SrcIP: net.IP{192, 168, 1, 1}, 71 DstIP: net.IP{192, 168, 1, 2}, 72 Protocol: IPProtocolGRE, 73 Flags: IPv4DontFragment, 74 TTL: 64, 75 Id: 33852, 76 IHL: 5, 77 }, 78 &GRE{ 79 Protocol: EthernetTypeIPv4, 80 }, 81 &IPv4{ 82 Version: 4, 83 SrcIP: net.IP{172, 16, 1, 1}, 84 DstIP: net.IP{172, 16, 2, 1}, 85 Protocol: IPProtocolICMPv4, 86 Flags: IPv4DontFragment, 87 TTL: 64, 88 IHL: 5, 89 Id: 1160, 90 }, 91 &ICMPv4{ 92 TypeCode: CreateICMPv4TypeCode(ICMPv4TypeEchoRequest, 0), 93 Id: 4724, 94 Seq: 1, 95 }, 96 gopacket.Payload{ 97 0xc8, 0x92, 0xa3, 0x54, 0x00, 0x00, 0x00, 0x00, 0x38, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 98 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 99 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 100 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 101 }, 102 } 103 104 func TestIPv4OverGREEncode(t *testing.T) { 105 b := gopacket.NewSerializeBuffer() 106 opts := gopacket.SerializeOptions{ 107 ComputeChecksums: true, 108 FixLengths: true, 109 } 110 if err := gopacket.SerializeLayers(b, opts, testIPv4OverGRE...); err != nil { 111 t.Errorf("Unable to serialize: %v", err) 112 } 113 p := gopacket.NewPacket(b.Bytes(), LinkTypeEthernet, gopacket.Default) 114 if p.ErrorLayer() != nil { 115 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 116 } 117 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeGRE, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t) 118 if got, want := b.Bytes(), testPacketGRE; !reflect.DeepEqual(want, got) { 119 t.Errorf("Encoding mismatch, \nwant: %v\ngot %v\n", want, got) 120 } 121 } 122 123 func BenchmarkEncodePacketGRE(b *testing.B) { 124 buf := gopacket.NewSerializeBuffer() 125 opts := gopacket.SerializeOptions{ 126 ComputeChecksums: true, 127 FixLengths: true, 128 } 129 for i := 0; i < b.N; i++ { 130 gopacket.SerializeLayers(buf, opts, testIPv4OverGRE...) 131 buf.Clear() 132 } 133 } 134 135 // testPacketEthernetOverGRE is the packet: 136 // 137 // 11:01:38.124768 IP 192.168.1.1 > 192.168.1.2: GREv0, length 102: IP 172.16.1.1 > 172.16.1.2: ICMP echo request, id 3842, seq 1, length 64 138 // 0x0000: ea6b 4cd3 5513 d6b9 d880 56ef 0800 4500 .kL.U.....V...E. 139 // 0x0010: 007a 0acd 4000 402f ac34 c0a8 0101 c0a8 .z..@.@/.4...... 140 // 0x0020: 0102 0000 6558 aa6a 36e6 c630 6e32 3ec7 ....eX.j6..0n2>. 141 // 0x0030: 9def 0800 4500 0054 d970 4000 4001 0715 ....E..T.p@.@... 142 // 0x0040: ac10 0101 ac10 0102 0800 3f15 0f02 0001 ..........?..... 143 // 0x0050: 82d9 b154 0000 0000 b5e6 0100 0000 0000 ...T............ 144 // 0x0060: 1011 1213 1415 1617 1819 1a1b 1c1d 1e1f ................ 145 // 0x0070: 2021 2223 2425 2627 2829 2a2b 2c2d 2e2f .!"#$%&'()*+,-./ 146 // 0x0080: 3031 3233 3435 3637 01234567 147 var testPacketEthernetOverGRE = []byte{ 148 0xea, 0x6b, 0x4c, 0xd3, 0x55, 0x13, 0xd6, 0xb9, 0xd8, 0x80, 0x56, 0xef, 0x08, 0x00, 0x45, 0x00, 149 0x00, 0x7a, 0x0a, 0xcd, 0x40, 0x00, 0x40, 0x2f, 0xac, 0x34, 0xc0, 0xa8, 0x01, 0x01, 0xc0, 0xa8, 150 0x01, 0x02, 0x00, 0x00, 0x65, 0x58, 0xaa, 0x6a, 0x36, 0xe6, 0xc6, 0x30, 0x6e, 0x32, 0x3e, 0xc7, 151 0x9d, 0xef, 0x08, 0x00, 0x45, 0x00, 0x00, 0x54, 0xd9, 0x70, 0x40, 0x00, 0x40, 0x01, 0x07, 0x15, 152 0xac, 0x10, 0x01, 0x01, 0xac, 0x10, 0x01, 0x02, 0x08, 0x00, 0x3f, 0x15, 0x0f, 0x02, 0x00, 0x01, 153 0x82, 0xd9, 0xb1, 0x54, 0x00, 0x00, 0x00, 0x00, 0xb5, 0xe6, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 154 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 155 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 156 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 157 } 158 159 func TestPacketEthernetOverGRE(t *testing.T) { 160 p := gopacket.NewPacket(testPacketEthernetOverGRE, LinkTypeEthernet, gopacket.Default) 161 if p.ErrorLayer() != nil { 162 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 163 } 164 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeGRE, LayerTypeEthernet, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t) 165 if got, ok := p.Layer(LayerTypeGRE).(*GRE); ok { 166 want := &GRE{ 167 BaseLayer: BaseLayer{testPacketEthernetOverGRE[34:38], testPacketEthernetOverGRE[38:]}, 168 Protocol: EthernetTypeTransparentEthernetBridging, 169 } 170 if !reflect.DeepEqual(want, got) { 171 t.Errorf("GRE layer mismatch, \nwant %#v\ngot %#v\n", want, got) 172 } 173 } 174 } 175 176 func BenchmarkDecodePacketEthernetOverGRE(b *testing.B) { 177 for i := 0; i < b.N; i++ { 178 gopacket.NewPacket(testPacketEthernetOverGRE, LinkTypeEthernet, gopacket.NoCopy) 179 } 180 } 181 182 var testEthernetOverGRE = []gopacket.SerializableLayer{ 183 &Ethernet{ 184 SrcMAC: net.HardwareAddr{0xd6, 0xb9, 0xd8, 0x80, 0x56, 0xef}, 185 DstMAC: net.HardwareAddr{0xea, 0x6b, 0x4c, 0xd3, 0x55, 0x13}, 186 EthernetType: EthernetTypeIPv4, 187 }, 188 &IPv4{ 189 Version: 4, 190 SrcIP: net.IP{192, 168, 1, 1}, 191 DstIP: net.IP{192, 168, 1, 2}, 192 Protocol: IPProtocolGRE, 193 Flags: IPv4DontFragment, 194 TTL: 64, 195 Id: 2765, 196 IHL: 5, 197 }, 198 &GRE{ 199 Protocol: EthernetTypeTransparentEthernetBridging, 200 }, 201 &Ethernet{ 202 SrcMAC: net.HardwareAddr{0x6e, 0x32, 0x3e, 0xc7, 0x9d, 0xef}, 203 DstMAC: net.HardwareAddr{0xaa, 0x6a, 0x36, 0xe6, 0xc6, 0x30}, 204 EthernetType: EthernetTypeIPv4, 205 }, 206 &IPv4{ 207 Version: 4, 208 SrcIP: net.IP{172, 16, 1, 1}, 209 DstIP: net.IP{172, 16, 1, 2}, 210 Protocol: IPProtocolICMPv4, 211 Flags: IPv4DontFragment, 212 TTL: 64, 213 IHL: 5, 214 Id: 55664, 215 }, 216 &ICMPv4{ 217 TypeCode: CreateICMPv4TypeCode(ICMPv4TypeEchoRequest, 0), 218 Id: 3842, 219 Seq: 1, 220 }, 221 gopacket.Payload{ 222 0x82, 0xd9, 0xb1, 0x54, 0x00, 0x00, 0x00, 0x00, 0xb5, 0xe6, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 223 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 224 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 225 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 226 }, 227 } 228 229 func TestEthernetOverGREEncode(t *testing.T) { 230 b := gopacket.NewSerializeBuffer() 231 opts := gopacket.SerializeOptions{ 232 ComputeChecksums: true, 233 FixLengths: true, 234 } 235 if err := gopacket.SerializeLayers(b, opts, testEthernetOverGRE...); err != nil { 236 t.Errorf("Unable to serialize: %v", err) 237 } 238 p := gopacket.NewPacket(b.Bytes(), LinkTypeEthernet, gopacket.Default) 239 if p.ErrorLayer() != nil { 240 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 241 } 242 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeGRE, LayerTypeEthernet, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t) 243 if got, want := b.Bytes(), testPacketEthernetOverGRE; !reflect.DeepEqual(want, got) { 244 t.Errorf("Encoding mismatch, \nwant: %v\ngot %v\n", want, got) 245 } 246 } 247 248 func BenchmarkEncodePacketEthernetOverGRE(b *testing.B) { 249 buf := gopacket.NewSerializeBuffer() 250 opts := gopacket.SerializeOptions{ 251 ComputeChecksums: true, 252 FixLengths: true, 253 } 254 for i := 0; i < b.N; i++ { 255 gopacket.SerializeLayers(buf, opts, testEthernetOverGRE...) 256 buf.Clear() 257 } 258 } 259 260 var testGREChecksum = map[uint16][]gopacket.SerializableLayer{ 261 0x77ff: { 262 &Ethernet{ 263 SrcMAC: net.HardwareAddr{0xc2, 0x00, 0x57, 0x75, 0x00, 0x00}, 264 DstMAC: net.HardwareAddr{0xc2, 0x01, 0x57, 0x75, 0x00, 0x00}, 265 EthernetType: EthernetTypeIPv4, 266 }, 267 &IPv4{ 268 Version: 4, 269 SrcIP: net.IP{10, 0, 0, 1}, 270 DstIP: net.IP{10, 0, 0, 2}, 271 Protocol: IPProtocolGRE, 272 TTL: 255, 273 Id: 10, 274 IHL: 5, 275 }, 276 &GRE{ 277 Protocol: EthernetTypeIPv4, 278 ChecksumPresent: true, 279 }, 280 &IPv4{ 281 Version: 4, 282 SrcIP: net.IP{1, 1, 1, 1}, 283 DstIP: net.IP{2, 2, 2, 2}, 284 Protocol: IPProtocolICMPv4, 285 TTL: 255, 286 IHL: 5, 287 Id: 10, 288 }, 289 &ICMPv4{ 290 TypeCode: CreateICMPv4TypeCode(ICMPv4TypeEchoRequest, 0), 291 Id: 2, 292 Seq: 0, 293 }, 294 gopacket.Payload{ 295 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xbe, 0x70, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 296 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 297 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 298 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 299 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 300 }, 301 }, 302 0x8475: { 303 &Ethernet{ 304 SrcMAC: net.HardwareAddr{0xc2, 0x00, 0x57, 0x75, 0x00, 0x00}, 305 DstMAC: net.HardwareAddr{0xc2, 0x01, 0x57, 0x75, 0x00, 0x00}, 306 EthernetType: EthernetTypeIPv4, 307 }, 308 &IPv4{ 309 Version: 4, 310 SrcIP: net.IP{10, 0, 0, 1}, 311 DstIP: net.IP{10, 0, 0, 2}, 312 Protocol: IPProtocolGRE, 313 TTL: 255, 314 Id: 10, 315 IHL: 5, 316 }, 317 &GRE{ 318 Protocol: EthernetTypeIPv4, 319 ChecksumPresent: true, 320 }, 321 &IPv4{ 322 Version: 4, 323 SrcIP: net.IP{2, 3, 4, 5}, 324 DstIP: net.IP{2, 3, 4, 50}, 325 Protocol: IPProtocolUDP, 326 TTL: 1, 327 IHL: 5, 328 Flags: IPv4DontFragment, 329 Id: 964, 330 }, 331 &UDP{ 332 SrcPort: 41781, 333 DstPort: 33434, 334 }, 335 gopacket.Payload{ 336 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 337 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 338 }, 339 }, 340 } 341 342 func TestGREChecksum(t *testing.T) { 343 buf := gopacket.NewSerializeBuffer() 344 opts := gopacket.SerializeOptions{ 345 ComputeChecksums: true, 346 FixLengths: true, 347 } 348 for cksum, packet := range testGREChecksum { 349 buf.Clear() 350 if err := setNetworkLayer(packet); err != nil { 351 t.Errorf("Failed to set network layer: %v", err) 352 continue 353 } 354 if err := gopacket.SerializeLayers(buf, opts, packet...); err != nil { 355 t.Errorf("Failed to serialize packet: %v", err) 356 continue 357 } 358 p := gopacket.NewPacket(buf.Bytes(), LinkTypeEthernet, gopacket.Default) 359 t.Log(p) 360 if p.ErrorLayer() != nil { 361 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 362 continue 363 } 364 if got, ok := p.Layer(LayerTypeGRE).(*GRE); ok { 365 if got.Checksum != cksum { 366 t.Errorf("Incorrect checksum calculated for GRE packet: want %v, got %v", cksum, got.Checksum) 367 } 368 } 369 } 370 } 371 372 func setNetworkLayer(layers []gopacket.SerializableLayer) error { 373 type setNetworkLayerForChecksum interface { 374 SetNetworkLayerForChecksum(gopacket.NetworkLayer) error 375 } 376 var l gopacket.NetworkLayer 377 for _, layer := range layers { 378 if n, ok := layer.(gopacket.NetworkLayer); ok { 379 l = n 380 } 381 if s, ok := layer.(setNetworkLayerForChecksum); ok { 382 if l == nil { 383 return fmt.Errorf("no enclosing network layer found before: %v", s) 384 } 385 if err := s.SetNetworkLayerForChecksum(l); err != nil { 386 return fmt.Errorf("failed to set network layer(%v) on layer(%v): %v", l, s, err) 387 } 388 } 389 } 390 return nil 391 }