github.com/gopacket/gopacket@v1.1.0/layers/dot11_test.go (about) 1 // Copyright 2014, 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 "bytes" 11 "net" 12 "reflect" 13 "testing" 14 15 "github.com/gopacket/gopacket" 16 ) 17 18 // Generator: python layers/test_creator.py --layerType=LayerTypeRadioTap --linkType=LinkTypeIEEE80211Radio --name=Dot11%s ~/Downloads/mesh.pcap 19 // http://wiki.wireshark.org/SampleCaptures#Sample_Captures 20 21 // testPacketDot11CtrlCTS is the packet: 22 // 09:28:41.830560 20604983us tsft short preamble 24.0 Mb/s 5240 MHz 11a -79dB signal -92dB noise antenna 1 Clear-To-Send RA:d8:a2:5e:97:61:c1 23 // 0x0000: 0000 1900 6f08 0000 3768 3a01 0000 0000 ....o...7h:..... 24 // 0x0010: 1230 7814 4001 b1a4 01c4 0094 00d8 a25e .0x.@..........^ 25 // 0x0020: 9761 c136 5095 8e .a.6P.. 26 27 var testPacketDot11CtrlCTS = []byte{ 28 0x00, 0x00, 0x19, 0x00, 0x6f, 0x08, 0x00, 0x00, 0x37, 0x68, 0x3a, 0x01, 0x00, 0x00, 0x00, 0x00, 29 0x12, 0x30, 0x78, 0x14, 0x40, 0x01, 0xb1, 0xa4, 0x01, 0xc4, 0x00, 0x94, 0x00, 0xd8, 0xa2, 0x5e, 30 0x97, 0x61, 0xc1, 0x36, 0x50, 0x95, 0x8e, 31 } 32 33 func TestPacketDot11CtrlCTS(t *testing.T) { 34 p := gopacket.NewPacket(testPacketDot11CtrlCTS, LinkTypeIEEE80211Radio, gopacket.Default) 35 if p.ErrorLayer() != nil { 36 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 37 } 38 checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11}, t) 39 40 if got, ok := p.Layer(LayerTypeRadioTap).(*RadioTap); ok { 41 want := &RadioTap{ 42 BaseLayer: BaseLayer{ 43 Contents: []uint8{0x0, 0x0, 0x19, 0x0, 0x6f, 0x8, 0x0, 0x0, 0x37, 0x68, 0x3a, 0x1, 0x0, 0x0, 0x0, 0x0, 0x12, 0x30, 0x78, 0x14, 0x40, 0x1, 0xb1, 0xa4, 0x1}, 44 Payload: []uint8{0xc4, 0x0, 0x94, 0x0, 0xd8, 0xa2, 0x5e, 0x97, 0x61, 0xc1, 0x36, 0x50, 0x95, 0x8e}, 45 }, 46 Version: 0x0, 47 Length: 0x19, 48 Present: 0x86f, 49 TSFT: 0x13a6837, 50 Flags: 0x12, 51 Rate: 0x30, 52 ChannelFrequency: 0x1478, 53 ChannelFlags: 0x140, 54 FHSS: 0x0, 55 DBMAntennaSignal: -79, 56 DBMAntennaNoise: -92, 57 LockQuality: 0x0, 58 TxAttenuation: 0x0, 59 DBTxAttenuation: 0x0, 60 DBMTxPower: 0, 61 Antenna: 1, 62 DBAntennaSignal: 0x0, 63 DBAntennaNoise: 0x0, 64 } 65 66 if !reflect.DeepEqual(got, want) { 67 t.Errorf("RadioTap packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 68 } 69 } 70 71 if got, ok := p.Layer(LayerTypeDot11).(*Dot11); ok { 72 if !got.ChecksumValid() { 73 t.Errorf("Dot11 packet processing failed:\nchecksum failed. got :\n%#v\n\n", got) 74 } 75 76 want := &Dot11{ 77 BaseLayer: BaseLayer{ 78 Contents: []uint8{0xc4, 0x0, 0x94, 0x0, 0xd8, 0xa2, 0x5e, 0x97, 0x61, 0xc1}, 79 Payload: []uint8{}, 80 }, 81 Type: Dot11TypeCtrlCTS, 82 Proto: 0x0, 83 Flags: 0x0, 84 DurationID: 0x94, 85 Address1: net.HardwareAddr{0xd8, 0xa2, 0x5e, 0x97, 0x61, 0xc1}, // check 86 Address2: net.HardwareAddr(nil), 87 Address3: net.HardwareAddr(nil), 88 Address4: net.HardwareAddr(nil), 89 Checksum: 0x8e955036, 90 } 91 92 if !reflect.DeepEqual(got, want) { 93 t.Errorf("Dot11 packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 94 } 95 } 96 } 97 98 func BenchmarkDecodePacketDot11CtrlCTS(b *testing.B) { 99 for i := 0; i < b.N; i++ { 100 gopacket.NewPacket(testPacketDot11CtrlCTS, LinkTypeIEEE80211Radio, gopacket.NoCopy) 101 } 102 } 103 104 // testPacketDot11MgmtBeacon is the packet: 105 // 106 // 15:44:56.531833 6.0 Mb/s 2412 MHz 11g -81dB signal antenna 5 Beacon (Wi2) [6.0* 9.0 12.0* 18.0 24.0* 36.0 48.0 54.0 Mbit] ESS CH: 1 107 // 0x0000: 0000 1200 2e48 0000 100c 6c09 c000 af05 .....H....l..... 108 // 0x0010: 0000 8000 0000 ffff ffff ffff c08a de01 ................ 109 // 0x0020: 11b8 c08a de01 11b8 f097 80f1 30bc 1300 ............0... 110 // 0x0030: 0000 6400 2104 0003 5769 3201 088c 1298 ..d.!...Wi2..... 111 // 0x0040: 24b0 4860 6c03 0101 0504 0001 0000 2a01 $.H`l.........*. 112 // 0x0050: 00dd 1800 50f2 0201 0181 0007 a400 0023 ....P..........# 113 // 0x0060: a400 0042 435e 0062 322f 00dd 1e00 904c ...BC^.b2/.....L 114 // 0x0070: 338c 011b ffff 0000 0000 0000 0000 0000 3............... 115 // 0x0080: 1000 0000 0000 0000 0000 002d 1a8c 011b ...........-.... 116 // 0x0090: ffff 0000 0000 0000 0000 0000 1000 0000 ................ 117 // 0x00a0: 0000 0000 0000 00dd 1a00 904c 3401 0000 ...........L4... 118 // 0x00b0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 119 // 0x00c0: 0000 003d 1601 0000 0000 0000 0000 0000 ...=............ 120 // 0x00d0: 0000 0000 0000 0000 0000 007f 0400 0000 ................ 121 // 0x00e0: 00dd 0800 1392 0100 0185 0094 0b90 15 ............... 122 var testPacketDot11MgmtBeacon = []byte{ 123 0x00, 0x00, 0x12, 0x00, 0x2e, 0x48, 0x00, 0x00, 0x10, 0x0c, 0x6c, 0x09, 0xc0, 0x00, 0xaf, 0x05, 124 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x8a, 0xde, 0x01, 125 0x11, 0xb8, 0xc0, 0x8a, 0xde, 0x01, 0x11, 0xb8, 0xf0, 0x97, 0x80, 0xf1, 0x30, 0xbc, 0x13, 0x00, 126 0x00, 0x00, 0x64, 0x00, 0x21, 0x04, 0x00, 0x03, 0x57, 0x69, 0x32, 0x01, 0x08, 0x8c, 0x12, 0x98, 127 0x24, 0xb0, 0x48, 0x60, 0x6c, 0x03, 0x01, 0x01, 0x05, 0x04, 0x00, 0x01, 0x00, 0x00, 0x2a, 0x01, 128 0x00, 0xdd, 0x18, 0x00, 0x50, 0xf2, 0x02, 0x01, 0x01, 0x81, 0x00, 0x07, 0xa4, 0x00, 0x00, 0x23, 129 0xa4, 0x00, 0x00, 0x42, 0x43, 0x5e, 0x00, 0x62, 0x32, 0x2f, 0x00, 0xdd, 0x1e, 0x00, 0x90, 0x4c, 130 0x33, 0x8c, 0x01, 0x1b, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 131 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d, 0x1a, 0x8c, 0x01, 0x1b, 132 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 133 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xdd, 0x1a, 0x00, 0x90, 0x4c, 0x34, 0x01, 0x00, 0x00, 134 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 135 0x00, 0x00, 0x00, 0x3d, 0x16, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 136 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x04, 0x00, 0x00, 0x00, 137 0x00, 0xdd, 0x08, 0x00, 0x13, 0x92, 0x01, 0x00, 0x01, 0x85, 0x00, 0x94, 0x0b, 0x90, 0x15, 138 } 139 140 func TestPacketDot11MgmtBeacon(t *testing.T) { 141 p := gopacket.NewPacket(testPacketDot11MgmtBeacon, LinkTypeIEEE80211Radio, gopacket.Default) 142 if p.ErrorLayer() != nil { 143 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 144 } 145 expectedLayers := []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11MgmtBeacon} 146 for i := 0; i < 12; i++ { 147 expectedLayers = append(expectedLayers, LayerTypeDot11InformationElement) 148 } 149 checkLayers(p, expectedLayers, t) 150 151 if p.Layer(LayerTypeDot11).(*Dot11).SequenceNumber != 2431 { 152 t.Error("dot11 invalid sequence number") 153 } 154 if p.Layer(LayerTypeDot11).(*Dot11).FragmentNumber != 0 { 155 t.Error("dot11 invalid fragment number") 156 } 157 if _, ok := p.Layer(LayerTypeDot11MgmtBeacon).(*Dot11MgmtBeacon); !ok { 158 t.Errorf("dot11 management beacon frame was expected") 159 } 160 } 161 162 func BenchmarkDecodePacketDot11MgmtBeacon(b *testing.B) { 163 for i := 0; i < b.N; i++ { 164 gopacket.NewPacket(testPacketDot11MgmtBeacon, LinkTypeIEEE80211Radio, gopacket.NoCopy) 165 } 166 } 167 168 // testPacketDot11DataQOSData is the packet: 169 // 170 // 06:14:27.838634 638790765us tsft short preamble 54.0 Mb/s -51dB signal -96dB noise antenna 2 5180 MHz 11a CF +QoS ARP, Request who-has 140.180.51.68 tell 169.254.247.0, length 28 171 // 0x0000: 0000 2000 6708 0400 6d2c 1326 0000 0000 ....g...m,.&.... 172 // 0x0010: 226c cda0 0200 0000 4001 0000 3c14 2411 "l......@...<.$. 173 // 0x0020: 8801 2c00 0603 7f07 a016 0019 e3d3 5352 ..,...........SR 174 // 0x0030: ffff ffff ffff 5064 0000 50aa aaaa 0300 ......Pd..P..... 175 // 0x0040: 0000 0806 0001 0800 0604 0001 0019 e3d3 ................ 176 // 0x0050: 5352 a9fe f700 0000 0000 0000 8cb4 3344 SR............3D 177 var testPacketDot11DataQOSData = []byte{ 178 0x00, 0x00, 0x20, 0x00, 0x67, 0x08, 0x04, 0x00, 0x6d, 0x2c, 0x13, 0x26, 0x00, 0x00, 0x00, 0x00, 179 0x22, 0x6c, 0xcd, 0xa0, 0x02, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x3c, 0x14, 0x24, 0x11, 180 0x88, 0x01, 0x2c, 0x00, 0x06, 0x03, 0x7f, 0x07, 0xa0, 0x16, 0x00, 0x19, 0xe3, 0xd3, 0x53, 0x52, 181 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x50, 0x64, 0x00, 0x00, 0x50, 0xaa, 0xaa, 0xaa, 0x03, 0x00, 182 0x00, 0x00, 0x08, 0x06, 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x00, 0x19, 0xe3, 0xd3, 183 0x53, 0x52, 0xa9, 0xfe, 0xf7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8c, 0xb4, 0x33, 0x44, 184 } 185 186 func TestPacketDot11DataQOSData(t *testing.T) { 187 p := gopacket.NewPacket(testPacketDot11DataQOSData, LinkTypeIEEE80211Radio, gopacket.Default) 188 if p.ErrorLayer() != nil { 189 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 190 } 191 checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11DataQOSData, LayerTypeDot11Data, LayerTypeLLC, LayerTypeSNAP, LayerTypeARP}, t) 192 193 if got, ok := p.Layer(LayerTypeARP).(*ARP); ok { 194 want := &ARP{BaseLayer: BaseLayer{ 195 Contents: []uint8{0x0, 0x1, 0x8, 0x0, 0x6, 0x4, 0x0, 0x1, 0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0xa9, 0xfe, 0xf7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8c, 0xb4, 0x33, 0x44}, 196 Payload: []uint8{}, 197 }, 198 AddrType: 0x1, 199 Protocol: 0x800, 200 HwAddressSize: 0x6, 201 ProtAddressSize: 0x4, 202 Operation: 0x1, 203 SourceHwAddress: []uint8{0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52}, 204 SourceProtAddress: []uint8{0xa9, 0xfe, 0xf7, 0x0}, 205 DstHwAddress: []uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, 206 DstProtAddress: []uint8{0x8c, 0xb4, 0x33, 0x44}, 207 } 208 209 if !reflect.DeepEqual(got, want) { 210 t.Errorf("ARP packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 211 } 212 } 213 } 214 func BenchmarkDecodePacketDot11DataQOSData(b *testing.B) { 215 for i := 0; i < b.N; i++ { 216 gopacket.NewPacket(testPacketDot11DataQOSData, LinkTypeIEEE80211Radio, gopacket.NoCopy) 217 } 218 } 219 220 // testPacketDot11MgmtAction is the packet: 221 // 222 // 15:54:43.236460 1.0 Mb/s 2412 MHz 11b -67dB signal antenna 5 Action (8e:3a:e3:44:ac:c6): Spectrum Management Act#4 223 // 0x0000: 0000 1200 2e48 0000 1002 6c09 a000 bd05 .....H....l..... 224 // 0x0010: 0000 d000 0000 ffff ffff ffff 8e3a e344 .............:.D 225 // 0x0020: acc6 8e3a e344 acc6 001b 0004 2503 0001 ...:.D......%... 226 // 0x0030: 0055 39f0 33 .U9.3 227 var testPacketDot11MgmtAction = []byte{ 228 0x00, 0x00, 0x12, 0x00, 0x2e, 0x48, 0x00, 0x00, 0x10, 0x02, 0x6c, 0x09, 0xa0, 0x00, 0xbd, 0x05, 229 0x00, 0x00, 0xd0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8e, 0x3a, 0xe3, 0x44, 230 0xac, 0xc6, 0x8e, 0x3a, 0xe3, 0x44, 0xac, 0xc6, 0x00, 0x1b, 0x00, 0x04, 0x25, 0x03, 0x00, 0x01, 231 0x00, 0x55, 0x39, 0xf0, 0x33, 232 } 233 234 func TestPacketDot11MgmtAction(t *testing.T) { 235 p := gopacket.NewPacket(testPacketDot11MgmtAction, LinkTypeIEEE80211Radio, gopacket.Default) 236 if p.ErrorLayer() != nil { 237 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 238 } 239 checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11MgmtAction}, t) 240 if got, ok := p.Layer(LayerTypeDot11).(*Dot11); !ok { 241 t.Errorf("dot11 frame was not parsed") 242 } else if !got.ChecksumValid() { 243 t.Errorf("Dot11 packet processing failed: checksum failed") 244 } 245 if got, ok := p.Layer(LayerTypeDot11MgmtAction).(*Dot11MgmtAction); !ok { 246 t.Errorf("management action frame was not parsed") 247 } else if got.Contents[0] != 0 { 248 t.Errorf("action category was not spectrum management") 249 } 250 } 251 252 func BenchmarkDecodePacketDot11MgmtAction(b *testing.B) { 253 for i := 0; i < b.N; i++ { 254 gopacket.NewPacket(testPacketDot11MgmtAction, LinkTypeIEEE80211Radio, gopacket.NoCopy) 255 } 256 } 257 258 // testPacketDot11CtrlAck is the packet: 259 // 260 // 06:14:27.838669 638758038us tsft short preamble 24.0 Mb/s -39dB signal -96dB noise antenna 2 5180 MHz 11a Acknowledgment RA:00:19:e3:d3:53:52 261 // 0x0000: 0000 2000 6708 0400 96ac 1226 0000 0000 ....g......&.... 262 // 0x0010: 2230 d9a0 0200 0000 4001 0000 3c14 2411 "0......@...<.$. 263 // 0x0020: d400 0000 0019 e3d3 5352 46e9 7687 ........SRF.v. 264 var testPacketDot11CtrlAck = []byte{ 265 0x00, 0x00, 0x20, 0x00, 0x67, 0x08, 0x04, 0x00, 0x96, 0xac, 0x12, 0x26, 0x00, 0x00, 0x00, 0x00, 266 0x32, 0x30, 0xd9, 0xa0, 0x02, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x3c, 0x14, 0x24, 0x11, 267 0xd4, 0x00, 0x00, 0x00, 0x00, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0x46, 0xe9, 0x76, 0x87, 268 } 269 270 func TestPacketDot11CtrlAck(t *testing.T) { 271 p := gopacket.NewPacket(testPacketDot11CtrlAck, LinkTypeIEEE80211Radio, gopacket.Default) 272 if p.ErrorLayer() != nil { 273 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 274 } 275 checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11}, t) 276 277 if got, ok := p.Layer(LayerTypeDot11).(*Dot11); ok { 278 if !got.ChecksumValid() { 279 t.Errorf("Dot11 packet processing failed:\nchecksum failed. got :\n%#v\n\n", got) 280 } 281 } 282 283 if got, ok := p.Layer(LayerTypeDot11).(*Dot11); ok { 284 if !got.ChecksumValid() { 285 t.Errorf("Dot11 packet processing failed:\nchecksum failed. got :\n%#v\n\n", got) 286 } 287 want := &Dot11{ 288 BaseLayer: BaseLayer{ 289 Contents: []uint8{0xd4, 0x0, 0x0, 0x0, 0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52}, 290 Payload: []uint8{}, 291 }, 292 Type: Dot11TypeCtrlAck, 293 Proto: 0x0, 294 Flags: 0x0, 295 DurationID: 0x0, 296 Address1: net.HardwareAddr{0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52}, 297 Address2: net.HardwareAddr(nil), 298 Address3: net.HardwareAddr(nil), 299 Address4: net.HardwareAddr(nil), 300 Checksum: 0x8776e946, 301 } 302 if !reflect.DeepEqual(got, want) { 303 t.Errorf("Dot11 packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 304 } 305 } 306 } 307 func BenchmarkDecodePacketDot11CtrlAck(b *testing.B) { 308 for i := 0; i < b.N; i++ { 309 gopacket.NewPacket(testPacketDot11CtrlAck, LinkTypeIEEE80211Radio, gopacket.NoCopy) 310 } 311 } 312 313 // testPacketDot11DataARP is the packet: 314 // 315 // 06:14:11.512316 622463798us tsft short preamble 6.0 Mb/s -39dB signal -96dB noise antenna 2 5180 MHz 11a ARP, Request who-has 67.8.14.54 tell 169.254.247.0, length 28 316 // 0x0000: 0000 2000 6708 0400 360b 1a25 0000 0000 ....g...6..%.... 317 // 0x0010: 220c d9a0 0200 0000 4001 0000 3c14 2411 ".......@...<.$. 318 // 0x0020: 0802 0000 ffff ffff ffff 0603 7f07 a016 ................ 319 // 0x0030: 0019 e3d3 5352 e07f aaaa 0300 0000 0806 ....SR.......... 320 // 0x0040: 0001 0800 0604 0001 0019 e3d3 5352 a9fe ............SR.. 321 // 0x0050: f700 0000 0000 0000 4308 0e36 ........C..6 322 var testPacketDot11DataARP = []byte{ 323 0x00, 0x00, 0x20, 0x00, 0x67, 0x08, 0x04, 0x00, 0x36, 0x0b, 0x1a, 0x25, 0x00, 0x00, 0x00, 0x00, 324 0x22, 0x0c, 0xd9, 0xa0, 0x02, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x3c, 0x14, 0x24, 0x11, 325 0x08, 0x02, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x06, 0x03, 0x7f, 0x07, 0xa0, 0x16, 326 0x00, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0xe0, 0x7f, 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x08, 0x06, 327 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x00, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0xa9, 0xfe, 328 0xf7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43, 0x08, 0x0e, 0x36, 329 } 330 331 func TestPacketDot11DataARP(t *testing.T) { 332 p := gopacket.NewPacket(testPacketDot11DataARP, LinkTypeIEEE80211Radio, gopacket.Default) 333 if p.ErrorLayer() != nil { 334 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 335 } 336 checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11Data, LayerTypeLLC, LayerTypeSNAP, LayerTypeARP}, t) 337 338 if got, ok := p.Layer(LayerTypeARP).(*ARP); ok { 339 want := &ARP{ 340 BaseLayer: BaseLayer{ 341 Contents: []uint8{0x0, 0x1, 0x8, 0x0, 0x6, 0x4, 0x0, 0x1, 0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0xa9, 0xfe, 0xf7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x43, 0x8, 0xe, 0x36}, 342 Payload: []uint8{}, 343 }, 344 AddrType: 0x1, 345 Protocol: 0x800, 346 HwAddressSize: 0x6, 347 ProtAddressSize: 0x4, 348 Operation: 0x1, 349 SourceHwAddress: []uint8{0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52}, 350 SourceProtAddress: []uint8{0xa9, 0xfe, 0xf7, 0x0}, 351 DstHwAddress: []uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, 352 DstProtAddress: []uint8{0x43, 0x8, 0xe, 0x36}, 353 } 354 355 if !reflect.DeepEqual(got, want) { 356 t.Errorf("ARP packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want) 357 } 358 } 359 } 360 361 func BenchmarkDecodePacketDot11DataARP(b *testing.B) { 362 for i := 0; i < b.N; i++ { 363 gopacket.NewPacket(testPacketDot11DataARP, LinkTypeIEEE80211Radio, gopacket.NoCopy) 364 } 365 } 366 367 // testPacketDot11DataIP is the packet: 368 // 369 // 06:14:21.388622 632340487us tsft short preamble 6.0 Mb/s -40dB signal -96dB noise antenna 1 5180 MHz 11a IP 0.0.0.0.68 > 255.255.255.255.67: BOOTP/DHCP, Request from 00:19:e3:d3:53:52, length 300 370 // 0x0000: 0000 2000 6708 0400 07c0 b025 0000 0000 ....g......%.... 371 // 0x0010: 220c d8a0 0100 0000 4001 0000 3c14 2411 ".......@...<.$. 372 // 0x0020: 0802 0000 ffff ffff ffff 0603 7f07 a016 ................ 373 // 0x0030: 0019 e3d3 5352 4095 aaaa 0300 0000 0800 ....SR@......... 374 // 0x0040: 4500 0148 c514 0000 ff11 f590 0000 0000 E..H............ 375 // 0x0050: ffff ffff 0044 0043 0134 2b39 0101 0600 .....D.C.4+9.... 376 // 0x0060: 131f 8c43 003c 0000 0000 0000 0000 0000 ...C.<.......... 377 // 0x0070: 0000 0000 0000 0000 0019 e3d3 5352 0000 ............SR.. 378 // 0x0080: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 379 // 0x0090: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 380 // 0x00a0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 381 // 0x00b0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 382 // 0x00c0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 383 // 0x00d0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 384 // 0x00e0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 385 // 0x00f0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 386 // 0x0100: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 387 // 0x0110: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 388 // 0x0120: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 389 // 0x0130: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 390 // 0x0140: 0000 0000 0000 0000 6382 5363 3501 0137 ........c.Sc5..7 391 // 0x0150: 0a01 0306 0f77 5ffc 2c2e 2f39 0205 dc3d .....w_.,./9...= 392 // 0x0160: 0701 0019 e3d3 5352 3304 0076 a700 0c0b ......SR3..v.... 393 // 0x0170: 4d61 6369 6e74 6f73 682d 34ff 0000 0000 Macintosh-4..... 394 // 0x0180: 0000 0000 0000 0000 ........ 395 var testPacketDot11DataIP = []byte{ 396 0x00, 0x00, 0x20, 0x00, 0x67, 0x08, 0x04, 0x00, 0x07, 0xc0, 0xb0, 0x25, 0x00, 0x00, 0x00, 0x00, 397 0x22, 0x0c, 0xd8, 0xa0, 0x01, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x3c, 0x14, 0x24, 0x11, 398 0x08, 0x02, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x06, 0x03, 0x7f, 0x07, 0xa0, 0x16, 399 0x00, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0x40, 0x95, 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x08, 0x00, 400 0x45, 0x00, 0x01, 0x48, 0xc5, 0x14, 0x00, 0x00, 0xff, 0x11, 0xf5, 0x90, 0x00, 0x00, 0x00, 0x00, 401 0xff, 0xff, 0xff, 0xff, 0x00, 0x44, 0x00, 0x43, 0x01, 0x34, 0x2b, 0x39, 0x01, 0x01, 0x06, 0x00, 402 0x13, 0x1f, 0x8c, 0x43, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 403 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0x00, 0x00, 404 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 405 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 406 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 407 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 408 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 409 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 410 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 411 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 412 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 413 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 414 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 415 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 416 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x01, 0x37, 417 0x0a, 0x01, 0x03, 0x06, 0x0f, 0x77, 0x5f, 0xfc, 0x2c, 0x2e, 0x2f, 0x39, 0x02, 0x05, 0xdc, 0x3d, 418 0x07, 0x01, 0x00, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0x33, 0x04, 0x00, 0x76, 0xa7, 0x00, 0x0c, 0x0b, 419 0x4d, 0x61, 0x63, 0x69, 0x6e, 0x74, 0x6f, 0x73, 0x68, 0x2d, 0x34, 0xff, 0x00, 0x00, 0x00, 0x00, 420 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 421 } 422 423 func TestPacketDot11DataIP(t *testing.T) { 424 p := gopacket.NewPacket(testPacketDot11DataIP, LinkTypeIEEE80211Radio, gopacket.Default) 425 if p.ErrorLayer() != nil { 426 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 427 } 428 checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11Data, LayerTypeLLC, LayerTypeSNAP, LayerTypeIPv4, LayerTypeUDP, LayerTypeDHCPv4}, t) 429 } 430 func BenchmarkDecodePacketDot11DataIP(b *testing.B) { 431 for i := 0; i < b.N; i++ { 432 gopacket.NewPacket(testPacketDot11DataIP, LinkTypeIEEE80211Radio, gopacket.NoCopy) 433 } 434 } 435 436 // Encrypted 437 438 // / testPacketP6196 is the packet: 439 // 440 // 09:28:41.830631 20605036us tsft wep -69dB signal -92dB noise antenna 1 5240 MHz 11a ht/40- 162.0 Mb/s MCS 12 40 MHz lon GI mixed BCC FEC [bit 20] CF +QoS Data IV:50a9 Pad 20 KeyID 0 441 // 0x0000: 0000 3000 6b08 1c00 6c68 3a01 0000 0000 ..0.k...lh:..... 442 // 0x0010: 1400 7814 4001 bba4 0160 0e1a 4001 0400 ..x.@....`..@... 443 // 0x0020: 7814 3022 1f01 0cff b10d 0000 0400 0000 x.0"............ 444 // 0x0030: 8841 2c00 0025 9c42 c262 d8a2 5e97 61c1 .A,..%.B.b..^.a. 445 // 0x0040: 0025 9c42 c25f 10db 0000 a950 0020 0000 .%.B._.....P.... 446 // 0x0050: 0000 f8ab a97e 3fbd d6e1 785b 0040 5f15 .....~?...x[.@_. 447 // 0x0060: 7123 8711 bd1f ffb9 e5b3 84bb ec2a 0a90 q#...........*.. 448 // 0x0070: d0a0 1a6f 9033 1083 5179 a0da f833 3a00 ...o.3..Qy...3:. 449 // 0x0080: 5471 f596 539b 1823 a33c 4908 545c 266a Tq..S..#.<I.T\&j 450 // 0x0090: 8540 515a 1da9 c49e a85a fbf7 de09 7f9c .@QZ.....Z...... 451 // 0x00a0: 6f35 0b8b 6831 2c10 43dc 8983 b1d9 dd29 o5..h1,.C......) 452 // 0x00b0: 7395 65b9 4b43 b391 16ec 4201 86c9 ca s.e.KC....B.... 453 var testPacketP6196 = []byte{ 454 0x00, 0x00, 0x30, 0x00, 0x6b, 0x08, 0x1c, 0x00, 0x6c, 0x68, 0x3a, 0x01, 0x00, 0x00, 0x00, 0x00, 455 0x14, 0x00, 0x78, 0x14, 0x40, 0x01, 0xbb, 0xa4, 0x01, 0x60, 0x0e, 0x1a, 0x40, 0x01, 0x04, 0x00, 456 0x78, 0x14, 0x30, 0x22, 0x1f, 0x01, 0x0c, 0xff, 0xb1, 0x0d, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 457 0x88, 0x41, 0x2c, 0x00, 0x00, 0x25, 0x9c, 0x42, 0xc2, 0x62, 0xd8, 0xa2, 0x5e, 0x97, 0x61, 0xc1, 458 0x00, 0x25, 0x9c, 0x42, 0xc2, 0x5f, 0x10, 0xdb, 0x00, 0x00, 0xa9, 0x50, 0x00, 0x20, 0x00, 0x00, 459 0x00, 0x00, 0xf8, 0xab, 0xa9, 0x7e, 0x3f, 0xbd, 0xd6, 0xe1, 0x78, 0x5b, 0x00, 0x40, 0x5f, 0x15, 460 0x71, 0x23, 0x87, 0x11, 0xbd, 0x1f, 0xff, 0xb9, 0xe5, 0xb3, 0x84, 0xbb, 0xec, 0x2a, 0x0a, 0x90, 461 0xd0, 0xa0, 0x1a, 0x6f, 0x90, 0x33, 0x10, 0x83, 0x51, 0x79, 0xa0, 0xda, 0xf8, 0x33, 0x3a, 0x00, 462 0x54, 0x71, 0xf5, 0x96, 0x53, 0x9b, 0x18, 0x23, 0xa3, 0x3c, 0x49, 0x08, 0x54, 0x5c, 0x26, 0x6a, 463 0x85, 0x40, 0x51, 0x5a, 0x1d, 0xa9, 0xc4, 0x9e, 0xa8, 0x5a, 0xfb, 0xf7, 0xde, 0x09, 0x7f, 0x9c, 464 0x6f, 0x35, 0x0b, 0x8b, 0x68, 0x31, 0x2c, 0x10, 0x43, 0xdc, 0x89, 0x83, 0xb1, 0xd9, 0xdd, 0x29, 465 0x73, 0x95, 0x65, 0xb9, 0x4b, 0x43, 0xb3, 0x91, 0x16, 0xec, 0x42, 0x01, 0x86, 0xc9, 0xca, 466 } 467 468 func TestPacketP6196(t *testing.T) { 469 p := gopacket.NewPacket(testPacketP6196, LinkTypeIEEE80211Radio, gopacket.Default) 470 if p.ErrorLayer() != nil { 471 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 472 } 473 474 checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11DataQOSData, LayerTypeDot11WEP}, t) 475 } 476 477 func BenchmarkDecodePacketP6196(b *testing.B) { 478 for i := 0; i < b.N; i++ { 479 gopacket.NewPacket(testPacketP6196, LinkTypeIEEE80211Radio, gopacket.NoCopy) 480 } 481 } 482 483 // testPacketDot11HTControl is the packet: 484 // 0000 00 00 26 00 2b 48 20 00 bf 70 06 02 00 00 00 00 ..&.+H .¿p...... 485 // 0010 40 00 78 14 40 01 b8 00 00 00 44 00 00 01 73 00 @.x.@.¸...D...s. 486 // 0020 00 00 00 00 00 00 88 c9 30 14 01 02 03 04 05 06 .......É0.ò.Jòs} 487 // 0030 11 12 13 14 15 16 21 22 23 24 25 26 c0 bd 00 14 .öP.6:M 2.Á7À½.. 488 // 0040 0e 28 00 a8 06 01 00 04 e6 73 b3 4a 24 3e 19 ea .(.¨....æs³J$>.ê 489 // 0050 2a b7 1f 3c c7 89 2b 22 e2 2b 28 6c 69 aa 0a ee *·.<Ç.+"â+(liª.î 490 // 0060 1e bc 2d 2a 00 35 68 39 ad 6f 29 52 38 07 ae cf .¼-*.5h9.o)R8.®Ï 491 // 0070 03 e7 0d 53 8b 3c 12 28 52 05 cc 70 be c7 68 5e .ç.S.<.(R.Ìp¾Çh^ 492 // 0080 5f b1 06 f4 73 22 63 ef 77 41 7b 86 _±.ôs"cïwA{. 493 var testPacketDot11HTControl = []byte{ 494 0x00, 0x00, 0x26, 0x00, 0x2b, 0x48, 0x20, 0x00, 0xbf, 0x70, 0x06, 0x02, 0x00, 0x00, 0x00, 0x00, 495 0x40, 0x00, 0x78, 0x14, 0x40, 0x01, 0xb8, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x01, 0x73, 0x00, 496 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0xc9, 0x30, 0x14, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 497 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0xc0, 0xbd, 0x00, 0x14, 498 0x0e, 0x28, 0x00, 0xa8, 0x06, 0x01, 0x00, 0x04, 0xe6, 0x73, 0xb3, 0x4a, 0x24, 0x3e, 0x19, 0xea, 499 0x2a, 0xb7, 0x1f, 0x3c, 0xc7, 0x89, 0x2b, 0x22, 0xe2, 0x2b, 0x28, 0x6c, 0x69, 0xaa, 0x0a, 0xee, 500 0x1e, 0xbc, 0x2d, 0x2a, 0x00, 0x35, 0x68, 0x39, 0xad, 0x6f, 0x29, 0x52, 0x38, 0x07, 0xae, 0xcf, 501 0x03, 0xe7, 0x0d, 0x53, 0x8b, 0x3c, 0x12, 0x28, 0x52, 0x05, 0xcc, 0x70, 0xbe, 0xc7, 0x68, 0x5e, 502 0x5f, 0xb1, 0x06, 0xf4, 0x73, 0x22, 0x63, 0xef, 0x77, 0x41, 0x7b, 0x86, 503 } 504 505 var mfb = uint8(20) 506 507 var wantHTControl = Dot11HTControl{ 508 ACConstraint: false, 509 RDGMorePPDU: true, 510 HT: &Dot11HTControlHT{ 511 LinkAdapationControl: &Dot11LinkAdapationControl{ 512 TRQ: true, 513 MRQ: true, 514 MSI: 1, 515 MFSI: 0, 516 ASEL: nil, 517 MFB: &mfb, 518 }, 519 CalibrationPosition: 0, 520 CalibrationSequence: 0, 521 CSISteering: 0, 522 NDPAnnouncement: false, 523 DEI: true, 524 }, 525 } 526 527 func TestPacketDot11HTControl(t *testing.T) { 528 p := gopacket.NewPacket(testPacketDot11HTControl, LinkTypeIEEE80211Radio, gopacket.Default) 529 if p.ErrorLayer() != nil { 530 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 531 } 532 533 checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11DataQOSData, LayerTypeDot11WEP}, t) 534 535 ld11 := p.Layer(LayerTypeDot11) 536 if dot11, ok := ld11.(*Dot11); ok { 537 if dot11.HTControl == nil { 538 t.Fatal("Packet didn't contain HTControl") 539 } 540 if !reflect.DeepEqual(*dot11.HTControl, wantHTControl) { 541 t.Errorf("Dot11 packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", dot11.HTControl, wantHTControl) 542 } 543 } 544 } 545 546 func TestInformationElement(t *testing.T) { 547 bin := []byte{ 548 0, 0, 549 0, 2, 1, 3, 550 221, 5, 1, 2, 3, 4, 5, 551 } 552 pkt := gopacket.NewPacket(bin, LayerTypeDot11InformationElement, gopacket.NoCopy) 553 554 buf := gopacket.NewSerializeBuffer() 555 var sLayers []gopacket.SerializableLayer 556 for _, l := range pkt.Layers() { 557 sLayers = append(sLayers, l.(*Dot11InformationElement)) 558 } 559 if err := gopacket.SerializeLayers(buf, gopacket.SerializeOptions{}, sLayers...); err != nil { 560 t.Error(err.Error()) 561 } 562 if !bytes.Equal(bin, buf.Bytes()) { 563 t.Error("build failed") 564 } 565 }