tests: Fix a typo in check_p2p_response error path
[mech_eap.git] / tests / hwsim / test_p2p_messages.py
1 # P2P protocol tests for various messages
2 # Copyright (c) 2014, Jouni Malinen <j@w1.fi>
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 import binascii
8 import struct
9 import time
10 import logging
11 logger = logging.getLogger()
12
13 import hostapd
14
15 MGMT_SUBTYPE_PROBE_REQ = 4
16 MGMT_SUBTYPE_ACTION = 13
17 ACTION_CATEG_PUBLIC = 4
18
19 P2P_GO_NEG_REQ = 0
20 P2P_GO_NEG_RESP = 1
21 P2P_GO_NEG_CONF = 2
22 P2P_INVITATION_REQ = 3
23 P2P_INVITATION_RESP = 4
24 P2P_DEV_DISC_REQ = 5
25 P2P_DEV_DISC_RESP = 6
26 P2P_PROV_DISC_REQ = 7
27 P2P_PROV_DISC_RESP = 8
28
29 P2P_ATTR_STATUS = 0
30 P2P_ATTR_MINOR_REASON_CODE = 1
31 P2P_ATTR_CAPABILITY = 2
32 P2P_ATTR_DEVICE_ID = 3
33 P2P_ATTR_GROUP_OWNER_INTENT = 4
34 P2P_ATTR_CONFIGURATION_TIMEOUT = 5
35 P2P_ATTR_LISTEN_CHANNEL = 6
36 P2P_ATTR_GROUP_BSSID = 7
37 P2P_ATTR_EXT_LISTEN_TIMING = 8
38 P2P_ATTR_INTENDED_INTERFACE_ADDR = 9
39 P2P_ATTR_MANAGEABILITY = 10
40 P2P_ATTR_CHANNEL_LIST = 11
41 P2P_ATTR_NOTICE_OF_ABSENCE = 12
42 P2P_ATTR_DEVICE_INFO = 13
43 P2P_ATTR_GROUP_INFO = 14
44 P2P_ATTR_GROUP_ID = 15
45 P2P_ATTR_INTERFACE = 16
46 P2P_ATTR_OPERATING_CHANNEL = 17
47 P2P_ATTR_INVITATION_FLAGS = 18
48 P2P_ATTR_OOB_GO_NEG_CHANNEL = 19
49 P2P_ATTR_VENDOR_SPECIFIC = 221
50
51 P2P_SC_SUCCESS = 0
52 P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE = 1
53 P2P_SC_FAIL_INCOMPATIBLE_PARAMS = 2
54 P2P_SC_FAIL_LIMIT_REACHED = 3
55 P2P_SC_FAIL_INVALID_PARAMS = 4
56 P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE = 5
57 P2P_SC_FAIL_PREV_PROTOCOL_ERROR = 6
58 P2P_SC_FAIL_NO_COMMON_CHANNELS = 7
59 P2P_SC_FAIL_UNKNOWN_GROUP = 8
60 P2P_SC_FAIL_BOTH_GO_INTENT_15 = 9
61 P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD = 10
62 P2P_SC_FAIL_REJECTED_BY_USER = 11
63
64 WSC_ATTR_CONFIG_METHODS = 0x1008
65
66 WLAN_EID_SSID = 0
67 WLAN_EID_SUPP_RATES = 1
68 WLAN_EID_VENDOR_SPECIFIC = 221
69
70 def ie_ssid(ssid):
71     return struct.pack("<BB", WLAN_EID_SSID, len(ssid)) + ssid
72
73 def ie_supp_rates():
74     return struct.pack("<BBBBBBBBBB", WLAN_EID_SUPP_RATES, 8,
75                        2*6, 2*9, 2*12, 2*18, 2*24, 2*36, 2*48, 2*54)
76
77 def ie_p2p(attrs):
78     return struct.pack("<BBBBBB", WLAN_EID_VENDOR_SPECIFIC, 4 + len(attrs),
79                        0x50, 0x6f, 0x9a, 9) + attrs
80
81 def ie_wsc(attrs):
82     return struct.pack("<BBBBBB", WLAN_EID_VENDOR_SPECIFIC, 4 + len(attrs),
83                        0x00, 0x50, 0xf2, 4) + attrs
84
85 def wsc_attr_config_methods(methods=0):
86     return struct.pack(">HHH", WSC_ATTR_CONFIG_METHODS, 2, methods)
87
88 def p2p_attr_status(status=P2P_SC_SUCCESS):
89     return struct.pack("<BHB", P2P_ATTR_STATUS, 1, status)
90
91 def p2p_attr_minor_reason_code(code=0):
92     return struct.pack("<BHB", P2P_ATTR_MINOR_REASON_CODE, 1, code)
93
94 def p2p_attr_capability(dev_capab=0, group_capab=0):
95     return struct.pack("<BHBB", P2P_ATTR_CAPABILITY, 2, dev_capab, group_capab)
96
97 def p2p_attr_device_id(addr):
98     val = struct.unpack('6B', binascii.unhexlify(addr.replace(':','')))
99     t = (P2P_ATTR_DEVICE_ID, 6) + val
100     return struct.pack('<BH6B', *t)
101
102 def p2p_attr_go_intent(go_intent=0, tie_breaker=0):
103     return struct.pack("<BHB", P2P_ATTR_GROUP_OWNER_INTENT, 1,
104                        (go_intent << 1) | (tie_breaker & 0x01))
105
106 def p2p_attr_config_timeout(go_config_timeout=0, client_config_timeout=0):
107     return struct.pack("<BHBB", P2P_ATTR_CONFIGURATION_TIMEOUT, 2,
108                        go_config_timeout, client_config_timeout)
109
110 def p2p_attr_listen_channel(op_class=81, chan=1):
111     return struct.pack("<BHBBBBB", P2P_ATTR_LISTEN_CHANNEL, 5,
112                        0x58, 0x58, 0x04, op_class, chan)
113
114 def p2p_attr_group_bssid(addr):
115     val = struct.unpack('6B', binascii.unhexlify(addr.replace(':','')))
116     t = (P2P_ATTR_GROUP_BSSID, 6) + val
117     return struct.pack('<BH6B', *t)
118
119 def p2p_attr_ext_listen_timing(period=0, interval=0):
120     return struct.pack("<BHHH", P2P_ATTR_EXT_LISTEN_TIMING, 4, period, interval)
121
122 def p2p_attr_intended_interface_addr(addr):
123     val = struct.unpack('6B', binascii.unhexlify(addr.replace(':','')))
124     t = (P2P_ATTR_INTENDED_INTERFACE_ADDR, 6) + val
125     return struct.pack('<BH6B', *t)
126
127 def p2p_attr_manageability(bitmap=0):
128     return struct.pack("<BHB", P2P_ATTR_MANAGEABILITY, 1, bitmap)
129
130 def p2p_attr_channel_list():
131     return struct.pack("<BH3BBB11B", P2P_ATTR_CHANNEL_LIST, 16,
132                        0x58, 0x58, 0x04,
133                        81, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
134
135 def p2p_attr_device_info(addr, name="Test", config_methods=0, dev_type="00010050F2040001"):
136     val = struct.unpack('6B', binascii.unhexlify(addr.replace(':','')))
137     val2 = struct.unpack('8B', binascii.unhexlify(dev_type))
138     t = (P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 4 + len(name)) + val + (config_methods,) + val2 + (0,)
139     return struct.pack("<BH6BH8BB", *t) + struct.pack('>HH', 0x1011, len(name)) +name
140
141 def p2p_attr_group_id(addr, ssid):
142     val = struct.unpack('6B', binascii.unhexlify(addr.replace(':','')))
143     t = (P2P_ATTR_GROUP_ID, 6 + len(ssid)) + val
144     return struct.pack('<BH6B', *t) + ssid
145
146 def p2p_attr_operating_channel(op_class=81, chan=1):
147     return struct.pack("<BHBBBBB", P2P_ATTR_OPERATING_CHANNEL, 5,
148                        0x58, 0x58, 0x04, op_class, chan)
149
150 def p2p_attr_invitation_flags(bitmap=0):
151     return struct.pack("<BHB", P2P_ATTR_INVITATION_FLAGS, 1, bitmap)
152
153 def p2p_hdr_helper(dst, src, type=None, dialog_token=1, req=True):
154     msg = {}
155     msg['fc'] = MGMT_SUBTYPE_ACTION << 4
156     msg['da'] = dst
157     msg['sa'] = src
158     if req:
159         msg['bssid'] = dst
160     else:
161         msg['bssid'] = src
162     msg['payload'] = struct.pack("<BBBBBB",
163                                  ACTION_CATEG_PUBLIC, 9, 0x50, 0x6f, 0x9a, 9)
164     if type is not None:
165         msg['payload'] += struct.pack("<B", type)
166         if dialog_token:
167             msg['payload'] += struct.pack("<B", dialog_token)
168     return msg
169
170 def p2p_hdr(dst, src, type=None, dialog_token=1):
171     return p2p_hdr_helper(dst, src, type, dialog_token, True)
172
173 def p2p_hdr_resp(dst, src, type=None, dialog_token=1):
174     return p2p_hdr_helper(dst, src, type, dialog_token, False)
175
176 def start_p2p(dev, apdev):
177     addr0 = dev[0].p2p_dev_addr()
178     dev[0].p2p_listen()
179     dev[1].p2p_find(social=True)
180     ev = dev[1].wait_event(["P2P-DEVICE-FOUND"], timeout=5)
181     if ev is None:
182         raise Exception("Device discovery timed out")
183     dev[1].p2p_stop_find()
184     peer = dev[1].get_peer(addr0)
185
186     bssid = apdev[0]['bssid']
187     params = { 'ssid': "test", 'beacon_int': "2000" }
188     if peer['listen_freq'] == "2412":
189         params['channel'] = '1'
190     elif peer['listen_freq'] == "2437":
191         params['channel'] = '6'
192     elif peer['listen_freq'] == "2462":
193         params['channel'] = '11'
194     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
195     hapd.set("ext_mgmt_frame_handling", "1")
196     return addr0, bssid, hapd, int(params['channel'])
197
198 def p2p_probe(hapd, src, chan=1):
199     msg = {}
200     msg['fc'] = MGMT_SUBTYPE_PROBE_REQ << 4
201     msg['da'] = "ff:ff:ff:ff:ff:ff"
202     msg['sa'] = src
203     msg['bssid'] = "ff:ff:ff:ff:ff:ff"
204     attrs = p2p_attr_listen_channel(chan=chan)
205     msg['payload'] = ie_ssid("DIRECT-") + ie_supp_rates() + ie_p2p(attrs)
206     hapd.mgmt_tx(msg)
207
208 def parse_p2p_public_action(payload):
209     pos = payload
210     (category, action) = struct.unpack('BB', pos[0:2])
211     if category != ACTION_CATEG_PUBLIC:
212         return None
213     if action != 9:
214         return None
215     pos = pos[2:]
216     (oui1,oui2,oui3,subtype) = struct.unpack('BBBB', pos[0:4])
217     if oui1 != 0x50 or oui2 != 0x6f or oui3 != 0x9a or subtype != 9:
218         return None
219     pos = pos[4:]
220     (subtype,dialog_token) = struct.unpack('BB', pos[0:2])
221     p2p = {}
222     p2p['subtype'] = subtype
223     p2p['dialog_token'] = dialog_token
224     pos = pos[2:]
225     p2p['elements'] = pos
226     while len(pos) > 2:
227         (id,elen) = struct.unpack('BB', pos[0:2])
228         pos = pos[2:]
229         if elen > len(pos):
230             raise Exception("Truncated IE in P2P Public Action frame (elen=%d left=%d)" % (elen, len(pos)))
231         if id == WLAN_EID_VENDOR_SPECIFIC:
232             if elen < 4:
233                 raise Exception("Too short vendor specific IE in P2P Public Action frame (elen=%d)" % elen)
234             (oui1,oui2,oui3,subtype) = struct.unpack('BBBB', pos[0:4])
235             if oui1 == 0x50 and oui2 == 0x6f and oui3 == 0x9a and subtype == 9:
236                 if 'p2p' in p2p:
237                     p2p['p2p'] += pos[4:elen]
238                 else:
239                     p2p['p2p'] = pos[4:elen]
240             if oui1 == 0x00 and oui2 == 0x50 and oui3 == 0xf2 and subtype == 4:
241                 p2p['wsc'] = pos[4:elen]
242         pos = pos[elen:]
243     if len(pos) > 0:
244         raise Exception("Invalid element in P2P Public Action frame")
245
246     if 'p2p' in p2p:
247         p2p['p2p_attrs'] = {}
248         pos = p2p['p2p']
249         while len(pos) >= 3:
250             (id,alen) = struct.unpack('<BH', pos[0:3])
251             pos = pos[3:]
252             if alen > len(pos):
253                 logger.info("P2P payload: " + binascii.hexlify(p2p['p2p']))
254                 raise Exception("Truncated P2P attribute in P2P Public Action frame (alen=%d left=%d p2p-payload=%d)" % (alen, len(pos), len(p2p['p2p'])))
255             p2p['p2p_attrs'][id] = pos[0:alen]
256             pos = pos[alen:]
257         if P2P_ATTR_STATUS in p2p['p2p_attrs']:
258             p2p['p2p_status'] = struct.unpack('B', p2p['p2p_attrs'][P2P_ATTR_STATUS])[0]
259
260     if 'wsc' in p2p:
261         p2p['wsc_attrs'] = {}
262         pos = p2p['wsc']
263         while len(pos) >= 4:
264             (id,alen) = struct.unpack('>HH', pos[0:4])
265             pos = pos[4:]
266             if alen > len(pos):
267                 logger.info("WSC payload: " + binascii.hexlify(p2p['wsc']))
268                 raise Exception("Truncated WSC attribute in P2P Public Action frame (alen=%d left=%d wsc-payload=%d)" % (alen, len(pos), len(p2p['wsc'])))
269             p2p['wsc_attrs'][id] = pos[0:alen]
270             pos = pos[alen:]
271
272     return p2p
273
274 def test_p2p_msg_empty(dev, apdev):
275     """P2P protocol test: empty P2P Public Action frame"""
276     dst, src, hapd, channel = start_p2p(dev, apdev)
277     msg = p2p_hdr(dst, src)
278     hapd.mgmt_tx(msg)
279
280 def test_p2p_msg_invitation_req(dev, apdev):
281     """P2P protocol tests for invitation request processing"""
282     dst, src, hapd, channel = start_p2p(dev, apdev)
283
284     # Empty P2P Invitation Request (missing dialog token)
285     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=None)
286     hapd.mgmt_tx(msg)
287     dialog_token = 0
288
289     # Various p2p_parse() failure cases due to invalid attributes
290
291     # Too short attribute header
292     dialog_token += 1
293     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
294     attrs = struct.pack("<BB", P2P_ATTR_CAPABILITY, 0)
295     msg['payload'] += ie_p2p(attrs)
296     hapd.mgmt_tx(msg)
297
298     # Minimal attribute underflow
299     dialog_token += 1
300     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
301     attrs = struct.pack("<BH", P2P_ATTR_CAPABILITY, 1)
302     msg['payload'] += ie_p2p(attrs)
303     hapd.mgmt_tx(msg)
304
305     # Large attribute underflow
306     dialog_token += 1
307     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
308     attrs = struct.pack("<BHB", P2P_ATTR_CAPABILITY, 0xffff, 1)
309     msg['payload'] += ie_p2p(attrs)
310     hapd.mgmt_tx(msg)
311
312     # Too short Capability attribute
313     dialog_token += 1
314     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
315     attrs = struct.pack("<BHB", P2P_ATTR_CAPABILITY, 1, 0)
316     msg['payload'] += ie_p2p(attrs)
317     hapd.mgmt_tx(msg)
318
319     # Too short Device ID attribute
320     dialog_token += 1
321     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
322     val = struct.unpack('5B', binascii.unhexlify("1122334455"))
323     t = (P2P_ATTR_DEVICE_ID, 5) + val
324     attrs = struct.pack('<BH5B', *t)
325     msg['payload'] += ie_p2p(attrs)
326     hapd.mgmt_tx(msg)
327
328     # Too short GO Intent attribute
329     dialog_token += 1
330     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
331     attrs = struct.pack("<BH", P2P_ATTR_GROUP_OWNER_INTENT, 0)
332     msg['payload'] += ie_p2p(attrs)
333     hapd.mgmt_tx(msg)
334
335     # Too short Status attribute
336     dialog_token += 1
337     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
338     attrs = struct.pack("<BH", P2P_ATTR_STATUS, 0)
339     msg['payload'] += ie_p2p(attrs)
340     hapd.mgmt_tx(msg)
341
342     # null Listen channel and too short Listen Channel attribute
343     dialog_token += 1
344     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
345     attrs = struct.pack("<BH", P2P_ATTR_LISTEN_CHANNEL, 0)
346     attrs += struct.pack("<BHB", P2P_ATTR_LISTEN_CHANNEL, 1, 0)
347     msg['payload'] += ie_p2p(attrs)
348     hapd.mgmt_tx(msg)
349
350     # null Operating channel and too short Operating Channel attribute
351     dialog_token += 1
352     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
353     attrs = struct.pack("<BH", P2P_ATTR_OPERATING_CHANNEL, 0)
354     attrs += struct.pack("<BHB", P2P_ATTR_OPERATING_CHANNEL, 1, 0)
355     msg['payload'] += ie_p2p(attrs)
356     hapd.mgmt_tx(msg)
357
358     # Too short Channel List attribute
359     dialog_token += 1
360     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
361     attrs = struct.pack("<BHBB", P2P_ATTR_CHANNEL_LIST, 2, 1, 2)
362     msg['payload'] += ie_p2p(attrs)
363     hapd.mgmt_tx(msg)
364
365     # Too short Device Info attribute
366     dialog_token += 1
367     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
368     attrs = struct.pack("<BHBB", P2P_ATTR_DEVICE_INFO, 2, 1, 2)
369     msg['payload'] += ie_p2p(attrs)
370     hapd.mgmt_tx(msg)
371
372     # Truncated Secondary Device Types in Device Info attribute
373     dialog_token += 1
374     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
375     attrs = struct.pack("<BH6BH8BB", P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1,
376                         0, 0, 0, 0, 0, 0,
377                         0,
378                         0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22,
379                         255)
380     msg['payload'] += ie_p2p(attrs)
381     hapd.mgmt_tx(msg)
382
383     # Missing Device Name in Device Info attribute
384     dialog_token += 1
385     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
386     attrs = struct.pack("<BH6BH8BB8B", P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 8,
387                         0, 0, 0, 0, 0, 0,
388                         0,
389                         0, 0, 0, 0, 0, 0, 0, 0,
390                         1,
391                         1, 2, 3, 4, 5, 6, 7, 8)
392     msg['payload'] += ie_p2p(attrs)
393     hapd.mgmt_tx(msg)
394
395     # Invalid Device Name header in Device Info attribute
396     dialog_token += 1
397     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
398     attrs = struct.pack("<BH6BH8BB8B4B", P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 8 + 4,
399                         0, 0, 0, 0, 0, 0,
400                         0,
401                         0, 0, 0, 0, 0, 0, 0, 0,
402                         1,
403                         1, 2, 3, 4, 5, 6, 7, 8,
404                         0x11, 0x12, 0, 0)
405     msg['payload'] += ie_p2p(attrs)
406     hapd.mgmt_tx(msg)
407
408     # Invalid Device Name header length in Device Info attribute
409     dialog_token += 1
410     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
411     attrs = struct.pack("<BH6BH8BB8B4B", P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 8 + 4,
412                         0, 0, 0, 0, 0, 0,
413                         0,
414                         0, 0, 0, 0, 0, 0, 0, 0,
415                         1,
416                         1, 2, 3, 4, 5, 6, 7, 8,
417                         0x10, 0x11, 0xff, 0xff)
418     msg['payload'] += ie_p2p(attrs)
419     hapd.mgmt_tx(msg)
420
421     # Invalid Device Name header length in Device Info attribute
422     dialog_token += 1
423     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
424     devname = 'A'
425     attrs = struct.pack("<BH6BH8BB8B4B", P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 8 + 4 + len(devname),
426                         0, 0, 0, 0, 0, 0,
427                         0,
428                         0, 0, 0, 0, 0, 0, 0, 0,
429                         1,
430                         1, 2, 3, 4, 5, 6, 7, 8,
431                         0x10, 0x11, 0, len(devname) + 1) + devname
432     msg['payload'] += ie_p2p(attrs)
433     hapd.mgmt_tx(msg)
434
435     # Device Name filtering and too long Device Name in Device Info attribute
436     dialog_token += 1
437     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
438     attrs = struct.pack("<BH6BH8BB8B4B4B", P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 8 + 4 + 4,
439                         0, 0, 0, 0, 0, 0,
440                         0,
441                         0, 0, 0, 0, 0, 0, 0, 0,
442                         1,
443                         1, 2, 3, 4, 5, 6, 7, 8,
444                         0x10, 0x11, 0, 4,
445                         64, 9, 0, 64)
446     devname = '123456789012345678901234567890123'
447     attrs += struct.pack("<BH6BH8BB8B4B", P2P_ATTR_DEVICE_INFO, 6 + 2 + 8 + 1 + 8 + 4 + len(devname),
448                          0, 0, 0, 0, 0, 0,
449                          0,
450                          0, 0, 0, 0, 0, 0, 0, 0,
451                          1,
452                          1, 2, 3, 4, 5, 6, 7, 8,
453                          0x10, 0x11, 0, len(devname)) + devname
454     msg['payload'] += ie_p2p(attrs)
455     hapd.mgmt_tx(msg)
456
457     # Too short Configuration Timeout attribute
458     dialog_token += 1
459     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
460     attrs = struct.pack("<BHB", P2P_ATTR_CONFIGURATION_TIMEOUT, 1, 1)
461     msg['payload'] += ie_p2p(attrs)
462     hapd.mgmt_tx(msg)
463
464     # Too short Intended P2P Interface Address attribute
465     dialog_token += 1
466     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
467     attrs = struct.pack("<BHB", P2P_ATTR_INTENDED_INTERFACE_ADDR, 1, 1)
468     msg['payload'] += ie_p2p(attrs)
469     hapd.mgmt_tx(msg)
470
471     # Too short P2P Group BSSID attribute
472     dialog_token += 1
473     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
474     attrs = struct.pack("<BHB", P2P_ATTR_GROUP_BSSID, 1, 1)
475     msg['payload'] += ie_p2p(attrs)
476     hapd.mgmt_tx(msg)
477
478     # Too short P2P Group ID attribute
479     dialog_token += 1
480     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
481     attrs = struct.pack("<BHB", P2P_ATTR_GROUP_ID, 1, 1)
482     msg['payload'] += ie_p2p(attrs)
483     hapd.mgmt_tx(msg)
484
485     # Too long P2P Group ID attribute
486     dialog_token += 1
487     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
488     attrs = struct.pack("<BH6B", P2P_ATTR_GROUP_ID, 6 + 33, 0, 0, 0, 0, 0, 0) + "123456789012345678901234567890123"
489     msg['payload'] += ie_p2p(attrs)
490     hapd.mgmt_tx(msg)
491
492     # Too short Invitation Flags attribute
493     dialog_token += 1
494     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
495     attrs = struct.pack("<BH", P2P_ATTR_INVITATION_FLAGS, 0)
496     msg['payload'] += ie_p2p(attrs)
497     hapd.mgmt_tx(msg)
498
499     # Valid and too short Manageability attribute
500     dialog_token += 1
501     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
502     attrs = p2p_attr_manageability()
503     attrs += struct.pack("<BH", P2P_ATTR_MANAGEABILITY, 0)
504     msg['payload'] += ie_p2p(attrs)
505     hapd.mgmt_tx(msg)
506
507     # Too short NoA attribute
508     dialog_token += 1
509     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
510     attrs = struct.pack("<BHB", P2P_ATTR_NOTICE_OF_ABSENCE, 1, 1)
511     msg['payload'] += ie_p2p(attrs)
512     hapd.mgmt_tx(msg)
513
514     # Valid and too short Extended Listen Timing attributes
515     dialog_token += 1
516     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
517     attrs = p2p_attr_ext_listen_timing(period=100, interval=50)
518     attrs += struct.pack("<BHBBB", P2P_ATTR_EXT_LISTEN_TIMING, 3, 0, 0, 0)
519     msg['payload'] += ie_p2p(attrs)
520     hapd.mgmt_tx(msg)
521
522     # Valid and too short Minor Reason Code attributes
523     dialog_token += 1
524     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
525     attrs = p2p_attr_minor_reason_code(code=2)
526     attrs += struct.pack("<BH", P2P_ATTR_MINOR_REASON_CODE, 0)
527     msg['payload'] += ie_p2p(attrs)
528     hapd.mgmt_tx(msg)
529
530     # Unknown attribute and too short OOB GO Negotiation Channel attribute
531     dialog_token += 1
532     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
533     attrs = struct.pack("<BHB", 99, 1, 1)
534     attrs += struct.pack("<BHB", P2P_ATTR_OOB_GO_NEG_CHANNEL, 1, 1)
535     msg['payload'] += ie_p2p(attrs)
536     hapd.mgmt_tx(msg)
537
538     if hapd.mgmt_rx(timeout=0.5) is not None:
539         raise Exception("Unexpected management frame received")
540
541     dev[0].dump_monitor()
542     dialog_token += 1
543     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
544     attrs = p2p_attr_config_timeout()
545     attrs += p2p_attr_invitation_flags()
546     attrs += p2p_attr_operating_channel()
547     attrs += p2p_attr_group_bssid(src)
548     attrs += p2p_attr_channel_list()
549     attrs += p2p_attr_group_id(src, "DIRECT-foo")
550     attrs += p2p_attr_device_info(src, config_methods=0x0108)
551     msg['payload'] += ie_p2p(attrs)
552     hapd.mgmt_tx(msg)
553     ev = dev[0].wait_event(["P2P-DEVICE-FOUND"], timeout=5)
554     if ev is None:
555         raise Exception("Timeout on device found event")
556     ev = dev[0].wait_event(["P2P-INVITATION-RECEIVED"], timeout=5)
557     if ev is None:
558         raise Exception("Timeout on invitation event " + str(dialog_token))
559     if hapd.mgmt_rx(timeout=1) is None:
560         raise Exception("No invitation response " + str(dialog_token))
561
562     time.sleep(0.1)
563     dev[0].dump_monitor()
564     dialog_token += 1
565     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
566     attrs = p2p_attr_config_timeout()
567     attrs += p2p_attr_invitation_flags()
568     attrs += p2p_attr_operating_channel()
569     attrs += p2p_attr_group_bssid(src)
570     attrs += p2p_attr_channel_list()
571     attrs += p2p_attr_group_id(src, "DIRECT-foo")
572     attrs += p2p_attr_device_info(src, config_methods=0x0108)
573     msg['payload'] += ie_p2p(attrs)
574     hapd.mgmt_tx(msg)
575     ev = dev[0].wait_event(["P2P-INVITATION-RECEIVED"], timeout=5)
576     if ev is None:
577         raise Exception("Timeout on invitation event " + str(dialog_token))
578     if hapd.mgmt_rx(timeout=1) is None:
579         raise Exception("No invitation response " + str(dialog_token))
580
581     time.sleep(0.1)
582     dev[0].dump_monitor()
583     dialog_token += 1
584     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
585     #attrs = p2p_attr_config_timeout()
586     attrs = p2p_attr_invitation_flags()
587     attrs += p2p_attr_operating_channel()
588     attrs += p2p_attr_group_bssid(src)
589     attrs += p2p_attr_channel_list()
590     attrs += p2p_attr_group_id(src, "DIRECT-foo")
591     attrs += p2p_attr_device_info(src, config_methods=0x0108)
592     msg['payload'] += ie_p2p(attrs)
593     hapd.mgmt_tx(msg)
594     if hapd.mgmt_rx(timeout=1) is None:
595         raise Exception("No invitation response " + str(dialog_token))
596
597     time.sleep(0.1)
598     dev[0].dump_monitor()
599     dialog_token += 1
600     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
601     attrs = p2p_attr_config_timeout()
602     #attrs = p2p_attr_invitation_flags()
603     attrs += p2p_attr_operating_channel()
604     attrs += p2p_attr_group_bssid(src)
605     attrs += p2p_attr_channel_list()
606     attrs += p2p_attr_group_id(src, "DIRECT-foo")
607     attrs += p2p_attr_device_info(src, config_methods=0x0108)
608     msg['payload'] += ie_p2p(attrs)
609     hapd.mgmt_tx(msg)
610     if hapd.mgmt_rx(timeout=1) is None:
611         raise Exception("No invitation response " + str(dialog_token))
612
613     time.sleep(0.1)
614     dev[0].dump_monitor()
615     dialog_token += 1
616     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
617     attrs = p2p_attr_config_timeout()
618     attrs = p2p_attr_invitation_flags()
619     #attrs += p2p_attr_operating_channel()
620     attrs += p2p_attr_group_bssid(src)
621     attrs += p2p_attr_channel_list()
622     attrs += p2p_attr_group_id(src, "DIRECT-foo")
623     attrs += p2p_attr_device_info(src, config_methods=0x0108)
624     msg['payload'] += ie_p2p(attrs)
625     hapd.mgmt_tx(msg)
626     if hapd.mgmt_rx(timeout=1) is None:
627         raise Exception("No invitation response " + str(dialog_token))
628
629     time.sleep(0.1)
630     dev[0].dump_monitor()
631     dialog_token += 1
632     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
633     attrs = p2p_attr_config_timeout()
634     attrs = p2p_attr_invitation_flags()
635     attrs += p2p_attr_operating_channel()
636     #attrs += p2p_attr_group_bssid(src)
637     attrs += p2p_attr_channel_list()
638     attrs += p2p_attr_group_id(src, "DIRECT-foo")
639     attrs += p2p_attr_device_info(src, config_methods=0x0108)
640     msg['payload'] += ie_p2p(attrs)
641     hapd.mgmt_tx(msg)
642     if hapd.mgmt_rx(timeout=1) is None:
643         raise Exception("No invitation response " + str(dialog_token))
644
645     time.sleep(0.1)
646     dev[0].dump_monitor()
647     dialog_token += 1
648     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
649     attrs = p2p_attr_config_timeout()
650     attrs = p2p_attr_invitation_flags()
651     attrs += p2p_attr_operating_channel()
652     attrs += p2p_attr_group_bssid(src)
653     #attrs += p2p_attr_channel_list()
654     attrs += p2p_attr_group_id(src, "DIRECT-foo")
655     attrs += p2p_attr_device_info(src, config_methods=0x0108)
656     msg['payload'] += ie_p2p(attrs)
657     hapd.mgmt_tx(msg)
658     if hapd.mgmt_rx(timeout=1) is None:
659         raise Exception("No invitation response " + str(dialog_token))
660
661     time.sleep(0.1)
662     dev[0].dump_monitor()
663     dialog_token += 1
664     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
665     attrs = p2p_attr_config_timeout()
666     attrs = p2p_attr_invitation_flags()
667     attrs += p2p_attr_operating_channel()
668     attrs += p2p_attr_group_bssid(src)
669     attrs += p2p_attr_channel_list()
670     #attrs += p2p_attr_group_id(src, "DIRECT-foo")
671     attrs += p2p_attr_device_info(src, config_methods=0x0108)
672     msg['payload'] += ie_p2p(attrs)
673     hapd.mgmt_tx(msg)
674     if hapd.mgmt_rx(timeout=1) is None:
675         raise Exception("No invitation response " + str(dialog_token))
676
677     time.sleep(0.1)
678     dev[0].dump_monitor()
679     dialog_token += 1
680     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
681     attrs = p2p_attr_config_timeout()
682     attrs = p2p_attr_invitation_flags()
683     attrs += p2p_attr_operating_channel()
684     attrs += p2p_attr_group_bssid(src)
685     attrs += p2p_attr_channel_list()
686     attrs += p2p_attr_group_id(src, "DIRECT-foo")
687     #attrs += p2p_attr_device_info(src, config_methods=0x0108)
688     msg['payload'] += ie_p2p(attrs)
689     hapd.mgmt_tx(msg)
690     if hapd.mgmt_rx(timeout=1) is None:
691         raise Exception("No invitation response " + str(dialog_token))
692
693     time.sleep(0.1)
694     dev[0].dump_monitor()
695     dialog_token += 1
696     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
697     hapd.mgmt_tx(msg)
698     if hapd.mgmt_rx(timeout=1) is None:
699         raise Exception("No invitation response " + str(dialog_token))
700
701 def test_p2p_msg_invitation_req_unknown(dev, apdev):
702     """P2P protocol tests for invitation request from unknown peer"""
703     dst, src, hapd, channel = start_p2p(dev, apdev)
704     dialog_token = 0
705
706     dialog_token += 1
707     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
708     attrs = p2p_attr_config_timeout()
709     attrs += p2p_attr_invitation_flags()
710     attrs += p2p_attr_operating_channel()
711     attrs += p2p_attr_group_bssid(src)
712     attrs += p2p_attr_channel_list()
713     #attrs += p2p_attr_group_id(src, "DIRECT-foo")
714     #attrs += p2p_attr_device_info(src, config_methods=0x0108)
715     msg['payload'] += ie_p2p(attrs)
716     hapd.mgmt_tx(msg)
717     ev = dev[0].wait_event(["P2P-INVITATION-RECEIVED"], timeout=5)
718     if ev is None:
719         raise Exception("Timeout on invitation event " + str(dialog_token))
720     if hapd.mgmt_rx(timeout=1) is None:
721         raise Exception("No invitation response " + str(dialog_token))
722
723 def test_p2p_msg_invitation_no_common_channels(dev, apdev):
724     """P2P protocol tests for invitation request without common channels"""
725     dst, src, hapd, channel = start_p2p(dev, apdev)
726     dialog_token = 0
727
728     dialog_token += 1
729     msg = p2p_hdr(dst, src, type=P2P_INVITATION_REQ, dialog_token=dialog_token)
730     attrs = p2p_attr_config_timeout()
731     attrs += p2p_attr_invitation_flags()
732     attrs += p2p_attr_operating_channel()
733     attrs += p2p_attr_group_bssid(src)
734     attrs += struct.pack("<BH3BBB", P2P_ATTR_CHANNEL_LIST, 5,
735                          0x58, 0x58, 0x04,
736                          81, 0)
737     attrs += p2p_attr_group_id(src, "DIRECT-foo")
738     attrs += p2p_attr_device_info(src, config_methods=0x0108)
739     msg['payload'] += ie_p2p(attrs)
740     hapd.mgmt_tx(msg)
741     if hapd.mgmt_rx(timeout=1) is None:
742         raise Exception("No invitation response " + str(dialog_token))
743     ev = dev[0].wait_event(["P2P-INVITATION-RECEIVED"], timeout=0.1)
744     if ev is not None:
745         raise Exception("Unexpected invitation event")
746
747 def test_p2p_msg_pd_req(dev, apdev):
748     """P2P protocol tests for provision discovery request processing"""
749     dst, src, hapd, channel = start_p2p(dev, apdev)
750     dialog_token = 0
751
752     # Too short attribute header
753     dialog_token += 1
754     msg = p2p_hdr(dst, src, type=P2P_PROV_DISC_REQ, dialog_token=dialog_token)
755     attrs = struct.pack("<BB", P2P_ATTR_CAPABILITY, 0)
756     msg['payload'] += ie_p2p(attrs)
757     hapd.mgmt_tx(msg)
758
759     if hapd.mgmt_rx(timeout=0.5) is not None:
760         raise Exception("Unexpected management frame received")
761
762     # No attributes
763     dialog_token += 1
764     msg = p2p_hdr(dst, src, type=P2P_PROV_DISC_REQ, dialog_token=dialog_token)
765     attrs = ""
766     msg['payload'] += ie_p2p(attrs)
767     hapd.mgmt_tx(msg)
768     if hapd.mgmt_rx(timeout=1) is None:
769         raise Exception("No PD response " + str(dialog_token))
770
771     # Valid request
772     time.sleep(0.1)
773     dialog_token += 1
774     msg = p2p_hdr(dst, src, type=P2P_PROV_DISC_REQ, dialog_token=dialog_token)
775     attrs = wsc_attr_config_methods(methods=0x1008)
776     msg['payload'] += ie_wsc(attrs)
777     attrs = p2p_attr_capability()
778     attrs += p2p_attr_device_info(src, config_methods=0x0108)
779     msg['payload'] += ie_p2p(attrs)
780     hapd.mgmt_tx(msg)
781     ev = dev[0].wait_event(["P2P-DEVICE-FOUND"], timeout=5)
782     if ev is None:
783         raise Exception("Timeout on device found event")
784     ev = dev[0].wait_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=5)
785     if ev is None:
786         raise Exception("Timeout on PD event")
787     if hapd.mgmt_rx(timeout=1) is None:
788         raise Exception("No PD response " + str(dialog_token))
789
790     # Unknown group
791     time.sleep(0.1)
792     dialog_token += 1
793     msg = p2p_hdr(dst, src, type=P2P_PROV_DISC_REQ, dialog_token=dialog_token)
794     attrs = wsc_attr_config_methods(methods=0x1008)
795     msg['payload'] += ie_wsc(attrs)
796     attrs = p2p_attr_capability()
797     attrs += p2p_attr_device_info(src, config_methods=0x0108)
798     attrs += p2p_attr_group_id("02:02:02:02:02:02", "DIRECT-foo")
799     msg['payload'] += ie_p2p(attrs)
800     hapd.mgmt_tx(msg)
801     if hapd.mgmt_rx(timeout=1) is None:
802         raise Exception("No PD response " + str(dialog_token))
803     ev = dev[0].wait_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=1)
804     if ev is not None:
805         raise Exception("Unexpected PD event")
806
807     # Listen channel is not yet known
808     if "FAIL" not in dev[0].global_request("P2P_PROV_DISC " + src + " display"):
809         raise Exception("Unexpected P2P_PROV_DISC success")
810
811     # Unknown peer
812     if "FAIL" not in dev[0].global_request("P2P_PROV_DISC 02:03:04:05:06:07 display"):
813         raise Exception("Unexpected P2P_PROV_DISC success (2)")
814
815 def test_p2p_msg_pd(dev, apdev):
816     """P2P protocol tests for provision discovery request processing (known)"""
817     dst, src, hapd, channel = start_p2p(dev, apdev)
818     dialog_token = 0
819
820     p2p_probe(hapd, src, chan=channel)
821     time.sleep(0.1)
822
823     # Valid request
824     dialog_token += 1
825     msg = p2p_hdr(dst, src, type=P2P_PROV_DISC_REQ, dialog_token=dialog_token)
826     attrs = wsc_attr_config_methods(methods=0x1008)
827     msg['payload'] += ie_wsc(attrs)
828     attrs = p2p_attr_capability()
829     attrs += p2p_attr_device_info(src, config_methods=0x0108)
830     msg['payload'] += ie_p2p(attrs)
831     hapd.mgmt_tx(msg)
832     ev = dev[0].wait_event(["P2P-DEVICE-FOUND"], timeout=5)
833     if ev is None:
834         raise Exception("Timeout on device found event")
835     ev = dev[0].wait_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=5)
836     if ev is None:
837         raise Exception("Timeout on PD event")
838     if hapd.mgmt_rx(timeout=1) is None:
839         raise Exception("No PD response " + str(dialog_token))
840
841     if "FAIL" in dev[0].global_request("P2P_PROV_DISC " + src + " display"):
842         raise Exception("Unexpected P2P_PROV_DISC failure")
843     frame = hapd.mgmt_rx(timeout=1)
844     if frame is None:
845         raise Exception("No PD request " + str(dialog_token))
846     p2p = parse_p2p_public_action(frame['payload'])
847     if p2p is None:
848         raise Exception("Failed to parse PD request")
849
850     # invalid dialog token
851     msg = p2p_hdr_resp(dst, src, type=P2P_PROV_DISC_RESP,
852                        dialog_token=p2p['dialog_token'] + 1)
853     hapd.mgmt_tx(msg)
854     ev = dev[0].wait_event(["P2P-PROV-DISC-FAILURE"], timeout=0.1)
855     if ev is not None:
856         raise Exception("Unexpected PD result event")
857
858     # valid dialog token
859     msg = p2p_hdr_resp(dst, src, type=P2P_PROV_DISC_RESP,
860                        dialog_token=p2p['dialog_token'])
861     hapd.mgmt_tx(msg)
862     ev = dev[0].wait_event(["P2P-PROV-DISC-FAILURE"], timeout=5)
863     if ev is None:
864         raise Exception("Timeout on PD result event")
865
866     # valid dialog token
867     msg = p2p_hdr_resp(dst, src, type=P2P_PROV_DISC_RESP,
868                        dialog_token=p2p['dialog_token'])
869     hapd.mgmt_tx(msg)
870     ev = dev[0].wait_event(["P2P-PROV-DISC-FAILURE"], timeout=0.1)
871     if ev is not None:
872         raise Exception("Unexpected PD result event")
873
874 def check_p2p_response(hapd, dialog_token, status):
875     resp = hapd.mgmt_rx(timeout=1)
876     if resp is None:
877         raise Exception("No GO Neg Response " + str(dialog_token))
878     p2p = parse_p2p_public_action(resp['payload'])
879     if p2p is None:
880         raise Exception("Not a P2P Public Action frame " + str(dialog_token))
881     if dialog_token != p2p['dialog_token']:
882         raise Exception("Unexpected dialog token in response")
883     if p2p['p2p_status'] != status:
884         raise Exception("Unexpected status code %s in response (expected %d)" % (p2p['p2p_status'], status))
885
886 def test_p2p_msg_go_neg_both_start(dev, apdev):
887     """P2P protocol test for simultaneous GO Neg initiation"""
888     addr0 = dev[0].p2p_dev_addr()
889     addr1 = dev[1].p2p_dev_addr()
890     dev[0].p2p_listen()
891     dev[1].discover_peer(addr0)
892     dev[1].p2p_listen()
893     dev[0].discover_peer(addr1)
894     dev[0].p2p_listen()
895     if "FAIL" in dev[0].request("SET ext_mgmt_frame_handling 1"):
896         raise Exception("Failed to enable external management frame handling")
897     if "FAIL" in dev[1].request("SET ext_mgmt_frame_handling 1"):
898         raise Exception("Failed to enable external management frame handling")
899     dev[0].request("P2P_CONNECT {} pbc".format(addr1))
900     dev[1].request("P2P_CONNECT {} pbc".format(addr0))
901     msg = dev[0].mgmt_rx()
902     if msg is None:
903         raise Exception("MGMT-RX timeout")
904     msg = dev[1].mgmt_rx()
905     if msg is None:
906         raise Exception("MGMT-RX timeout(2)")
907     if "FAIL" in dev[0].request("SET ext_mgmt_frame_handling 0"):
908         raise Exception("Failed to disable external management frame handling")
909     ev = dev[0].wait_global_event(["P2P-GO-NEG-SUCCESS"], timeout=2)
910     if ev is not None:
911         raise Exception("Unexpected GO Neg success")
912     if "FAIL" in dev[1].request("SET ext_mgmt_frame_handling 0"):
913         raise Exception("Failed to disable external management frame handling")
914     ev = dev[0].wait_global_event(["P2P-GO-NEG-SUCCESS"], timeout=10)
915     if ev is None:
916         raise Exception("GO Neg did not succeed")
917     ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=5);
918     if ev is None:
919         raise Exception("Group formation not succeed")
920     ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=5);
921     if ev is None:
922         raise Exception("Group formation not succeed")
923
924 def test_p2p_msg_go_neg_req(dev, apdev):
925     """P2P protocol tests for invitation request from unknown peer"""
926     dst, src, hapd, channel = start_p2p(dev, apdev)
927     dialog_token = 0
928
929     # invalid attribute
930     dialog_token += 1
931     msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
932     attrs = struct.pack("<BB", P2P_ATTR_CAPABILITY, 0)
933     msg['payload'] += ie_p2p(attrs)
934     hapd.mgmt_tx(msg)
935     frame = hapd.mgmt_rx(timeout=0.1)
936     if frame is not None:
937         print frame
938         raise Exception("Unexpected GO Neg Response")
939
940     # missing atributes
941     dialog_token += 1
942     msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
943     attrs = p2p_attr_capability()
944     attrs += p2p_attr_go_intent()
945     attrs += p2p_attr_config_timeout()
946     #attrs += p2p_attr_listen_channel()
947     attrs += p2p_attr_ext_listen_timing()
948     attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
949     attrs += p2p_attr_channel_list()
950     attrs += p2p_attr_device_info(src, config_methods=0x0108)
951     attrs += p2p_attr_operating_channel()
952     msg['payload'] += ie_p2p(attrs)
953     hapd.mgmt_tx(msg)
954     if hapd.mgmt_rx(timeout=1) is None:
955         raise Exception("No GO Neg Response " + str(dialog_token))
956     time.sleep(0.1)
957
958     dialog_token += 1
959     msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
960     attrs = p2p_attr_capability()
961     attrs += p2p_attr_go_intent()
962     attrs += p2p_attr_config_timeout()
963     attrs += p2p_attr_listen_channel()
964     attrs += p2p_attr_ext_listen_timing()
965     attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
966     attrs += p2p_attr_channel_list()
967     attrs += p2p_attr_device_info(src, config_methods=0x0108)
968     #attrs += p2p_attr_operating_channel()
969     msg['payload'] += ie_p2p(attrs)
970     hapd.mgmt_tx(msg)
971     if hapd.mgmt_rx(timeout=1) is None:
972         raise Exception("No GO Neg Response " + str(dialog_token))
973     time.sleep(0.1)
974
975     dialog_token += 1
976     msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
977     attrs = p2p_attr_capability()
978     attrs += p2p_attr_go_intent()
979     attrs += p2p_attr_config_timeout()
980     attrs += p2p_attr_listen_channel()
981     attrs += p2p_attr_ext_listen_timing()
982     attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
983     #attrs += p2p_attr_channel_list()
984     attrs += p2p_attr_device_info(src, config_methods=0x0108)
985     attrs += p2p_attr_operating_channel()
986     msg['payload'] += ie_p2p(attrs)
987     hapd.mgmt_tx(msg)
988     if hapd.mgmt_rx(timeout=1) is None:
989         raise Exception("No GO Neg Response " + str(dialog_token))
990     time.sleep(0.1)
991
992     dialog_token += 1
993     msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
994     attrs = p2p_attr_capability()
995     attrs += p2p_attr_go_intent()
996     attrs += p2p_attr_config_timeout()
997     attrs += p2p_attr_listen_channel()
998     attrs += p2p_attr_ext_listen_timing()
999     #attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
1000     attrs += p2p_attr_channel_list()
1001     attrs += p2p_attr_device_info(src, config_methods=0x0108)
1002     attrs += p2p_attr_operating_channel()
1003     msg['payload'] += ie_p2p(attrs)
1004     hapd.mgmt_tx(msg)
1005     if hapd.mgmt_rx(timeout=1) is None:
1006         raise Exception("No GO Neg Response " + str(dialog_token))
1007     time.sleep(0.1)
1008
1009     dialog_token += 1
1010     msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
1011     attrs = p2p_attr_capability()
1012     attrs += p2p_attr_go_intent()
1013     attrs += p2p_attr_config_timeout()
1014     attrs += p2p_attr_listen_channel()
1015     attrs += p2p_attr_ext_listen_timing()
1016     attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
1017     attrs += p2p_attr_channel_list()
1018     #attrs += p2p_attr_device_info(src, config_methods=0x0108)
1019     attrs += p2p_attr_operating_channel()
1020     msg['payload'] += ie_p2p(attrs)
1021     hapd.mgmt_tx(msg)
1022     if hapd.mgmt_rx(timeout=1) is None:
1023         raise Exception("No GO Neg Response " + str(dialog_token))
1024     time.sleep(0.1)
1025
1026     # SA != P2P Device address
1027     dialog_token += 1
1028     msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
1029     attrs = p2p_attr_capability()
1030     attrs += p2p_attr_go_intent()
1031     attrs += p2p_attr_config_timeout()
1032     attrs += p2p_attr_listen_channel()
1033     attrs += p2p_attr_ext_listen_timing()
1034     attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
1035     attrs += p2p_attr_channel_list()
1036     attrs += p2p_attr_device_info("02:02:02:02:02:02", config_methods=0x0108)
1037     attrs += p2p_attr_operating_channel()
1038     msg['payload'] += ie_p2p(attrs)
1039     hapd.mgmt_tx(msg)
1040     if hapd.mgmt_rx(timeout=1) is None:
1041         raise Exception("No GO Neg Response " + str(dialog_token))
1042     time.sleep(0.1)
1043
1044     # unexpected Status attribute
1045     dialog_token += 1
1046     msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
1047     attrs = p2p_attr_capability()
1048     attrs += p2p_attr_go_intent()
1049     attrs += p2p_attr_config_timeout()
1050     attrs += p2p_attr_listen_channel()
1051     attrs += p2p_attr_ext_listen_timing()
1052     attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
1053     attrs += p2p_attr_channel_list()
1054     attrs += p2p_attr_device_info(src, config_methods=0x0108)
1055     attrs += p2p_attr_operating_channel()
1056     attrs += p2p_attr_status(status=P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
1057     msg['payload'] += ie_p2p(attrs)
1058     hapd.mgmt_tx(msg)
1059     if hapd.mgmt_rx(timeout=1) is None:
1060         raise Exception("No GO Neg Response(1) " + str(dialog_token))
1061     time.sleep(0.1)
1062
1063     # valid (with workarounds) GO Neg Req
1064     dialog_token += 1
1065     msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
1066     #attrs = p2p_attr_capability()
1067     #attrs += p2p_attr_go_intent()
1068     #attrs += p2p_attr_config_timeout()
1069     attrs = p2p_attr_listen_channel()
1070     attrs += p2p_attr_ext_listen_timing()
1071     attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
1072     attrs += p2p_attr_channel_list()
1073     attrs += p2p_attr_device_info(src, config_methods=0x0108)
1074     attrs += p2p_attr_operating_channel()
1075     msg['payload'] += ie_p2p(attrs)
1076     hapd.mgmt_tx(msg)
1077     check_p2p_response(hapd, dialog_token,
1078                        P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
1079     ev = dev[0].wait_event(["P2P-GO-NEG-REQUEST"], timeout=1)
1080     if ev is None:
1081         raise Exception("Timeout on GO Neg event " + str(dialog_token))
1082
1083     dev[0].request("P2P_CONNECT " + src + " 12345670 display auth")
1084
1085     # ready - missing attributes (with workarounds) GO Neg Req
1086     time.sleep(0.1)
1087     dialog_token += 1
1088     msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
1089     #attrs = p2p_attr_capability()
1090     #attrs += p2p_attr_go_intent()
1091     #attrs += p2p_attr_config_timeout()
1092     attrs = p2p_attr_listen_channel()
1093     attrs += p2p_attr_ext_listen_timing()
1094     attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
1095     attrs += p2p_attr_channel_list()
1096     attrs += p2p_attr_device_info(src, config_methods=0x0108)
1097     attrs += p2p_attr_operating_channel()
1098     msg['payload'] += ie_p2p(attrs)
1099     hapd.mgmt_tx(msg)
1100     if hapd.mgmt_rx(timeout=1) is None:
1101         raise Exception("No GO Neg Response " + str(dialog_token))
1102
1103     # ready - invalid GO Intent GO Neg Req
1104     time.sleep(0.1)
1105     dialog_token += 1
1106     msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
1107     #attrs = p2p_attr_capability()
1108     attrs = p2p_attr_go_intent(go_intent=16)
1109     #attrs += p2p_attr_config_timeout()
1110     attrs += p2p_attr_listen_channel()
1111     attrs += p2p_attr_ext_listen_timing()
1112     attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
1113     attrs += p2p_attr_channel_list()
1114     attrs += p2p_attr_device_info(src, config_methods=0x0108)
1115     attrs += p2p_attr_operating_channel()
1116     msg['payload'] += ie_p2p(attrs)
1117     hapd.mgmt_tx(msg)
1118     check_p2p_response(hapd, dialog_token, P2P_SC_FAIL_INVALID_PARAMS)
1119
1120     # ready - invalid Channel List
1121     time.sleep(0.1)
1122     dialog_token += 1
1123     msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
1124     attrs = p2p_attr_capability()
1125     attrs += p2p_attr_go_intent()
1126     attrs += p2p_attr_config_timeout()
1127     attrs += p2p_attr_listen_channel()
1128     attrs += p2p_attr_ext_listen_timing()
1129     attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
1130     attrs += struct.pack("<BH3BBB11B", P2P_ATTR_CHANNEL_LIST, 16,
1131                          0x58, 0x58, 0x04,
1132                          81, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
1133     attrs += p2p_attr_device_info(src, config_methods=0x0108)
1134     attrs += p2p_attr_operating_channel()
1135     msg['payload'] += ie_p2p(attrs)
1136     hapd.mgmt_tx(msg)
1137     check_p2p_response(hapd, dialog_token, P2P_SC_FAIL_NO_COMMON_CHANNELS)
1138
1139     # ready - invalid GO Neg Req (unsupported Device Password ID)
1140     time.sleep(0.1)
1141     dialog_token += 1
1142     msg = p2p_hdr(dst, src, type=P2P_GO_NEG_REQ, dialog_token=dialog_token)
1143     attrs = p2p_attr_capability()
1144     attrs += p2p_attr_go_intent()
1145     attrs += p2p_attr_config_timeout()
1146     attrs += p2p_attr_listen_channel()
1147     attrs += p2p_attr_ext_listen_timing()
1148     attrs += p2p_attr_intended_interface_addr("02:02:02:02:02:02")
1149     # very long channel list
1150     attrs += struct.pack("<BH3BBB11B30B", P2P_ATTR_CHANNEL_LIST, 46,
1151                          0x58, 0x58, 0x04,
1152                          81, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
1153                          1, 1, 1, 2, 1, 2, 3, 1, 3, 4, 1, 4, 5, 1, 5,
1154                          6, 1, 6, 7, 1, 7, 8, 1, 8, 9, 1, 9, 10, 1, 10)
1155     attrs += p2p_attr_device_info(src, config_methods=0x0108)
1156     attrs += p2p_attr_operating_channel()
1157     msg['payload'] += ie_p2p(attrs)
1158     hapd.mgmt_tx(msg)
1159     check_p2p_response(hapd, dialog_token, P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD)
1160
1161 def test_p2p_msg_go_neg_req_reject(dev, apdev):
1162     """P2P protocol tests for user reject incorrectly in GO Neg Req"""
1163     addr0 = dev[0].p2p_dev_addr()
1164     addr1 = dev[1].p2p_dev_addr()
1165     dev[0].p2p_listen()
1166     dev[1].discover_peer(addr0)
1167     dev[1].group_request("P2P_CONNECT " + addr0 + " pbc")
1168     ev = dev[0].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=10)
1169     if ev is None:
1170         raise Exception("Timeout on GO Neg Req")
1171
1172     peer = dev[0].get_peer(addr1)
1173     dev[0].p2p_stop_find()
1174
1175     msg = p2p_hdr(addr1, addr0, type=P2P_GO_NEG_REQ, dialog_token=123)
1176     attrs = p2p_attr_capability()
1177     attrs += p2p_attr_status(status=P2P_SC_FAIL_REJECTED_BY_USER)
1178     attrs += p2p_attr_go_intent()
1179     attrs += p2p_attr_config_timeout()
1180     attrs += p2p_attr_listen_channel()
1181     attrs += p2p_attr_ext_listen_timing()
1182     attrs += p2p_attr_intended_interface_addr(addr0)
1183     attrs += p2p_attr_channel_list()
1184     attrs += p2p_attr_device_info(addr0, config_methods=0x0108)
1185     attrs += p2p_attr_operating_channel()
1186     msg['payload'] += ie_p2p(attrs)
1187
1188     for i in range(0, 20):
1189         if "FAIL" in dev[0].request("MGMT_TX {} {} freq={} wait_time=10 no_cck=1 action={}".format(addr1, addr1, peer['listen_freq'], binascii.hexlify(msg['payload']))):
1190             raise Exception("Failed to send Action frame")
1191         ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
1192         if ev is None:
1193             raise Exception("Timeout on MGMT-TX-STATUS")
1194         if "result=SUCCESS" in ev:
1195             break
1196         time.sleep(0.01)
1197     if "result=SUCCESS" not in ev:
1198         raise Exception("Peer did not ack GO Neg Req")
1199
1200     ev = dev[1].wait_global_event(["P2P-GO-NEG-FAILURE"], timeout=5)
1201     if ev is None:
1202         raise Exception("GO Negotiation failure not reported")
1203     if "status=%d" % P2P_SC_FAIL_REJECTED_BY_USER not in ev:
1204         raise Exception("Unexpected failure reason: " + ev)