tests: Clear p2p_go_intent explicitly in test cases where it is used
[mech_eap.git] / tests / hwsim / wpasupplicant.py
1 # Python class for controlling wpa_supplicant
2 # Copyright (c) 2013-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 os
8 import time
9 import logging
10 import binascii
11 import re
12 import struct
13 import subprocess
14 import wpaspy
15
16 logger = logging.getLogger()
17 wpas_ctrl = '/var/run/wpa_supplicant'
18
19 class WpaSupplicant:
20     def __init__(self, ifname=None, global_iface=None):
21         self.group_ifname = None
22         self.gctrl_mon = None
23         if ifname:
24             self.set_ifname(ifname)
25         else:
26             self.ifname = None
27
28         self.global_iface = global_iface
29         if global_iface:
30             self.global_ctrl = wpaspy.Ctrl(global_iface)
31             self.global_mon = wpaspy.Ctrl(global_iface)
32             self.global_mon.attach()
33         else:
34             self.global_mon = None
35
36     def close_ctrl(self):
37         if self.global_mon:
38             self.global_mon.detach()
39             self.global_mon = None
40             self.global_ctrl = None
41         self.remove_ifname()
42
43     def set_ifname(self, ifname):
44         self.ifname = ifname
45         self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
46         self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
47         self.mon.attach()
48
49     def remove_ifname(self):
50         if self.ifname:
51             self.mon.detach()
52             self.mon = None
53             self.ctrl = None
54             self.ifname = None
55
56     def interface_add(self, ifname, config="", driver="nl80211",
57                       drv_params=None, br_ifname=None, create=False,
58                       set_ifname=True, all_params=False, if_type=None):
59         try:
60             groups = subprocess.check_output(["id"])
61             group = "admin" if "(admin)" in groups else "adm"
62         except Exception, e:
63             group = "admin"
64         cmd = "INTERFACE_ADD " + ifname + "\t" + config + "\t" + driver + "\tDIR=/var/run/wpa_supplicant GROUP=" + group
65         if drv_params:
66             cmd = cmd + '\t' + drv_params
67         if br_ifname:
68             if not drv_params:
69                 cmd += '\t'
70             cmd += '\t' + br_ifname
71         if create:
72             if not br_ifname:
73                 cmd += '\t'
74                 if not drv_params:
75                     cmd += '\t'
76             cmd += '\tcreate'
77             if if_type:
78                 cmd += '\t' + if_type
79         if all_params and not create:
80             if not br_ifname:
81                 cmd += '\t'
82                 if not drv_params:
83                     cmd += '\t'
84             cmd += '\t'
85         if "FAIL" in self.global_request(cmd):
86             raise Exception("Failed to add a dynamic wpa_supplicant interface")
87         if not create and set_ifname:
88             self.set_ifname(ifname)
89
90     def interface_remove(self, ifname):
91         self.remove_ifname()
92         self.global_request("INTERFACE_REMOVE " + ifname)
93
94     def request(self, cmd, timeout=10):
95         logger.debug(self.ifname + ": CTRL: " + cmd)
96         return self.ctrl.request(cmd, timeout=timeout)
97
98     def global_request(self, cmd):
99         if self.global_iface is None:
100             self.request(cmd)
101         else:
102             ifname = self.ifname or self.global_iface
103             logger.debug(ifname + ": CTRL(global): " + cmd)
104             return self.global_ctrl.request(cmd)
105
106     def group_request(self, cmd):
107         if self.group_ifname and self.group_ifname != self.ifname:
108             logger.debug(self.group_ifname + ": CTRL: " + cmd)
109             gctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, self.group_ifname))
110             return gctrl.request(cmd)
111         return self.request(cmd)
112
113     def ping(self):
114         return "PONG" in self.request("PING")
115
116     def global_ping(self):
117         return "PONG" in self.global_request("PING")
118
119     def reset(self):
120         self.dump_monitor()
121         res = self.request("FLUSH")
122         if not "OK" in res:
123             logger.info("FLUSH to " + self.ifname + " failed: " + res)
124         self.global_request("REMOVE_NETWORK all")
125         self.global_request("SET p2p_pref_chan ")
126         self.global_request("SET p2p_no_group_iface 1")
127         self.global_request("P2P_FLUSH")
128         self.request("SET ignore_old_scan_res 0")
129         if self.gctrl_mon:
130             try:
131                 self.gctrl_mon.detach()
132             except:
133                 pass
134             self.gctrl_mon = None
135         self.group_ifname = None
136         self.dump_monitor()
137
138         iter = 0
139         while iter < 60:
140             state1 = self.get_driver_status_field("scan_state")
141             p2pdev = "p2p-dev-" + self.ifname
142             state2 = self.get_driver_status_field("scan_state", ifname=p2pdev)
143             states = str(state1) + " " + str(state2)
144             if "SCAN_STARTED" in states or "SCAN_REQUESTED" in states:
145                 logger.info(self.ifname + ": Waiting for scan operation to complete before continuing")
146                 time.sleep(1)
147             else:
148                 break
149             iter = iter + 1
150         if iter == 60:
151             logger.error(self.ifname + ": Driver scan state did not clear")
152             print "Trying to clear cfg80211/mac80211 scan state"
153             try:
154                 cmd = ["ifconfig", self.ifname, "down"]
155                 subprocess.call(cmd)
156             except subprocess.CalledProcessError, e:
157                 logger.info("ifconfig failed: " + str(e.returncode))
158                 logger.info(e.output)
159             try:
160                 cmd = ["ifconfig", self.ifname, "up"]
161                 subprocess.call(cmd)
162             except subprocess.CalledProcessError, e:
163                 logger.info("ifconfig failed: " + str(e.returncode))
164                 logger.info(e.output)
165         if iter > 0:
166             # The ongoing scan could have discovered BSSes or P2P peers
167             logger.info("Run FLUSH again since scan was in progress")
168             self.request("FLUSH")
169             self.dump_monitor()
170
171         if not self.ping():
172             logger.info("No PING response from " + self.ifname + " after reset")
173
174     def add_network(self):
175         id = self.request("ADD_NETWORK")
176         if "FAIL" in id:
177             raise Exception("ADD_NETWORK failed")
178         return int(id)
179
180     def remove_network(self, id):
181         id = self.request("REMOVE_NETWORK " + str(id))
182         if "FAIL" in id:
183             raise Exception("REMOVE_NETWORK failed")
184         return None
185
186     def get_network(self, id, field):
187         res = self.request("GET_NETWORK " + str(id) + " " + field)
188         if res == "FAIL\n":
189             return None
190         return res
191
192     def set_network(self, id, field, value):
193         res = self.request("SET_NETWORK " + str(id) + " " + field + " " + value)
194         if "FAIL" in res:
195             raise Exception("SET_NETWORK failed")
196         return None
197
198     def set_network_quoted(self, id, field, value):
199         res = self.request("SET_NETWORK " + str(id) + " " + field + ' "' + value + '"')
200         if "FAIL" in res:
201             raise Exception("SET_NETWORK failed")
202         return None
203
204     def list_networks(self, p2p=False):
205         if p2p:
206             res = self.global_request("LIST_NETWORKS")
207         else:
208             res = self.request("LIST_NETWORKS")
209         lines = res.splitlines()
210         networks = []
211         for l in lines:
212             if "network id" in l:
213                 continue
214             [id,ssid,bssid,flags] = l.split('\t')
215             network = {}
216             network['id'] = id
217             network['ssid'] = ssid
218             network['bssid'] = bssid
219             network['flags'] = flags
220             networks.append(network)
221         return networks
222
223     def hs20_enable(self, auto_interworking=False):
224         self.request("SET interworking 1")
225         self.request("SET hs20 1")
226         if auto_interworking:
227             self.request("SET auto_interworking 1")
228         else:
229             self.request("SET auto_interworking 0")
230
231     def interworking_add_network(self, bssid):
232         id = self.request("INTERWORKING_ADD_NETWORK " + bssid)
233         if "FAIL" in id or "OK" in id:
234             raise Exception("INTERWORKING_ADD_NETWORK failed")
235         return int(id)
236
237     def add_cred(self):
238         id = self.request("ADD_CRED")
239         if "FAIL" in id:
240             raise Exception("ADD_CRED failed")
241         return int(id)
242
243     def remove_cred(self, id):
244         id = self.request("REMOVE_CRED " + str(id))
245         if "FAIL" in id:
246             raise Exception("REMOVE_CRED failed")
247         return None
248
249     def set_cred(self, id, field, value):
250         res = self.request("SET_CRED " + str(id) + " " + field + " " + value)
251         if "FAIL" in res:
252             raise Exception("SET_CRED failed")
253         return None
254
255     def set_cred_quoted(self, id, field, value):
256         res = self.request("SET_CRED " + str(id) + " " + field + ' "' + value + '"')
257         if "FAIL" in res:
258             raise Exception("SET_CRED failed")
259         return None
260
261     def get_cred(self, id, field):
262         return self.request("GET_CRED " + str(id) + " " + field)
263
264     def add_cred_values(self, params):
265         id = self.add_cred()
266
267         quoted = [ "realm", "username", "password", "domain", "imsi",
268                    "excluded_ssid", "milenage", "ca_cert", "client_cert",
269                    "private_key", "domain_suffix_match", "provisioning_sp",
270                    "roaming_partner", "phase1", "phase2" ]
271         for field in quoted:
272             if field in params:
273                 self.set_cred_quoted(id, field, params[field])
274
275         not_quoted = [ "eap", "roaming_consortium", "priority",
276                        "required_roaming_consortium", "sp_priority",
277                        "max_bss_load", "update_identifier", "req_conn_capab",
278                        "min_dl_bandwidth_home", "min_ul_bandwidth_home",
279                        "min_dl_bandwidth_roaming", "min_ul_bandwidth_roaming" ]
280         for field in not_quoted:
281             if field in params:
282                 self.set_cred(id, field, params[field])
283
284         return id;
285
286     def select_network(self, id, freq=None):
287         if freq:
288             extra = " freq=" + str(freq)
289         else:
290             extra = ""
291         id = self.request("SELECT_NETWORK " + str(id) + extra)
292         if "FAIL" in id:
293             raise Exception("SELECT_NETWORK failed")
294         return None
295
296     def mesh_group_add(self, id):
297         id = self.request("MESH_GROUP_ADD " + str(id))
298         if "FAIL" in id:
299             raise Exception("MESH_GROUP_ADD failed")
300         return None
301
302     def mesh_group_remove(self):
303         id = self.request("MESH_GROUP_REMOVE " + str(self.ifname))
304         if "FAIL" in id:
305             raise Exception("MESH_GROUP_REMOVE failed")
306         return None
307
308     def connect_network(self, id, timeout=10):
309         self.dump_monitor()
310         self.select_network(id)
311         self.wait_connected(timeout=timeout)
312         self.dump_monitor()
313
314     def get_status(self, extra=None):
315         if extra:
316             extra = "-" + extra
317         else:
318             extra = ""
319         res = self.request("STATUS" + extra)
320         lines = res.splitlines()
321         vals = dict()
322         for l in lines:
323             try:
324                 [name,value] = l.split('=', 1)
325                 vals[name] = value
326             except ValueError, e:
327                 logger.info(self.ifname + ": Ignore unexpected STATUS line: " + l)
328         return vals
329
330     def get_status_field(self, field, extra=None):
331         vals = self.get_status(extra)
332         if field in vals:
333             return vals[field]
334         return None
335
336     def get_group_status(self, extra=None):
337         if extra:
338             extra = "-" + extra
339         else:
340             extra = ""
341         res = self.group_request("STATUS" + extra)
342         lines = res.splitlines()
343         vals = dict()
344         for l in lines:
345             try:
346                 [name,value] = l.split('=', 1)
347             except ValueError:
348                 logger.info(self.ifname + ": Ignore unexpected status line: " + l)
349                 continue
350             vals[name] = value
351         return vals
352
353     def get_group_status_field(self, field, extra=None):
354         vals = self.get_group_status(extra)
355         if field in vals:
356             return vals[field]
357         return None
358
359     def get_driver_status(self, ifname=None):
360         if ifname is None:
361             res = self.request("STATUS-DRIVER")
362         else:
363             res = self.global_request("IFNAME=%s STATUS-DRIVER" % ifname)
364             if res.startswith("FAIL"):
365                 return dict()
366         lines = res.splitlines()
367         vals = dict()
368         for l in lines:
369             try:
370                 [name,value] = l.split('=', 1)
371             except ValueError:
372                 logger.info(self.ifname + ": Ignore unexpected status-driver line: " + l)
373                 continue
374             vals[name] = value
375         return vals
376
377     def get_driver_status_field(self, field, ifname=None):
378         vals = self.get_driver_status(ifname)
379         if field in vals:
380             return vals[field]
381         return None
382
383     def get_mcc(self):
384         mcc = int(self.get_driver_status_field('capa.num_multichan_concurrent'))
385         return 1 if mcc < 2 else mcc
386
387     def get_mib(self):
388         res = self.request("MIB")
389         lines = res.splitlines()
390         vals = dict()
391         for l in lines:
392             try:
393                 [name,value] = l.split('=', 1)
394                 vals[name] = value
395             except ValueError, e:
396                 logger.info(self.ifname + ": Ignore unexpected MIB line: " + l)
397         return vals
398
399     def p2p_dev_addr(self):
400         return self.get_status_field("p2p_device_address")
401
402     def p2p_interface_addr(self):
403         return self.get_group_status_field("address")
404
405     def own_addr(self):
406         try:
407             res = self.p2p_interface_addr()
408         except:
409             res = self.p2p_dev_addr()
410         return res
411
412     def p2p_listen(self):
413         return self.global_request("P2P_LISTEN")
414
415     def p2p_ext_listen(self, period, interval):
416         return self.global_request("P2P_EXT_LISTEN %d %d" % (period, interval))
417
418     def p2p_cancel_ext_listen(self):
419         return self.global_request("P2P_EXT_LISTEN")
420
421     def p2p_find(self, social=False, progressive=False, dev_id=None,
422                  dev_type=None, delay=None, freq=None):
423         cmd = "P2P_FIND"
424         if social:
425             cmd = cmd + " type=social"
426         elif progressive:
427             cmd = cmd + " type=progressive"
428         if dev_id:
429             cmd = cmd + " dev_id=" + dev_id
430         if dev_type:
431             cmd = cmd + " dev_type=" + dev_type
432         if delay:
433             cmd = cmd + " delay=" + str(delay)
434         if freq:
435             cmd = cmd + " freq=" + str(freq)
436         return self.global_request(cmd)
437
438     def p2p_stop_find(self):
439         return self.global_request("P2P_STOP_FIND")
440
441     def wps_read_pin(self):
442         self.pin = self.request("WPS_PIN get").rstrip("\n")
443         if "FAIL" in self.pin:
444             raise Exception("Could not generate PIN")
445         return self.pin
446
447     def peer_known(self, peer, full=True):
448         res = self.global_request("P2P_PEER " + peer)
449         if peer.lower() not in res.lower():
450             return False
451         if not full:
452             return True
453         return "[PROBE_REQ_ONLY]" not in res
454
455     def discover_peer(self, peer, full=True, timeout=15, social=True,
456                       force_find=False, freq=None):
457         logger.info(self.ifname + ": Trying to discover peer " + peer)
458         if not force_find and self.peer_known(peer, full):
459             return True
460         self.p2p_find(social, freq=freq)
461         count = 0
462         while count < timeout * 4:
463             time.sleep(0.25)
464             count = count + 1
465             if self.peer_known(peer, full):
466                 return True
467         return False
468
469     def get_peer(self, peer):
470         res = self.global_request("P2P_PEER " + peer)
471         if peer.lower() not in res.lower():
472             raise Exception("Peer information not available")
473         lines = res.splitlines()
474         vals = dict()
475         for l in lines:
476             if '=' in l:
477                 [name,value] = l.split('=', 1)
478                 vals[name] = value
479         return vals
480
481     def group_form_result(self, ev, expect_failure=False, go_neg_res=None):
482         if expect_failure:
483             if "P2P-GROUP-STARTED" in ev:
484                 raise Exception("Group formation succeeded when expecting failure")
485             exp = r'<.>(P2P-GO-NEG-FAILURE) status=([0-9]*)'
486             s = re.split(exp, ev)
487             if len(s) < 3:
488                 return None
489             res = {}
490             res['result'] = 'go-neg-failed'
491             res['status'] = int(s[2])
492             return res
493
494         if "P2P-GROUP-STARTED" not in ev:
495             raise Exception("No P2P-GROUP-STARTED event seen")
496
497         exp = r'<.>(P2P-GROUP-STARTED) ([^ ]*) ([^ ]*) ssid="(.*)" freq=([0-9]*) ((?:psk=.*)|(?:passphrase=".*")) go_dev_addr=([0-9a-f:]*) ip_addr=([0-9.]*) ip_mask=([0-9.]*) go_ip_addr=([0-9.]*)'
498         s = re.split(exp, ev)
499         if len(s) < 11:
500             exp = r'<.>(P2P-GROUP-STARTED) ([^ ]*) ([^ ]*) ssid="(.*)" freq=([0-9]*) ((?:psk=.*)|(?:passphrase=".*")) go_dev_addr=([0-9a-f:]*)'
501             s = re.split(exp, ev)
502             if len(s) < 8:
503                 raise Exception("Could not parse P2P-GROUP-STARTED")
504         res = {}
505         res['result'] = 'success'
506         res['ifname'] = s[2]
507         self.group_ifname = s[2]
508         try:
509             self.gctrl_mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, self.group_ifname))
510             self.gctrl_mon.attach()
511         except:
512             logger.debug("Could not open monitor socket for group interface")
513             self.gctrl_mon = None
514         res['role'] = s[3]
515         res['ssid'] = s[4]
516         res['freq'] = s[5]
517         if "[PERSISTENT]" in ev:
518             res['persistent'] = True
519         else:
520             res['persistent'] = False
521         p = re.match(r'psk=([0-9a-f]*)', s[6])
522         if p:
523             res['psk'] = p.group(1)
524         p = re.match(r'passphrase="(.*)"', s[6])
525         if p:
526             res['passphrase'] = p.group(1)
527         res['go_dev_addr'] = s[7]
528
529         if len(s) > 8 and len(s[8]) > 0:
530             res['ip_addr'] = s[8]
531         if len(s) > 9:
532             res['ip_mask'] = s[9]
533         if len(s) > 10:
534             res['go_ip_addr'] = s[10]
535
536         if go_neg_res:
537             exp = r'<.>(P2P-GO-NEG-SUCCESS) role=(GO|client) freq=([0-9]*)'
538             s = re.split(exp, go_neg_res)
539             if len(s) < 4:
540                 raise Exception("Could not parse P2P-GO-NEG-SUCCESS")
541             res['go_neg_role'] = s[2]
542             res['go_neg_freq'] = s[3]
543
544         return res
545
546     def p2p_go_neg_auth(self, peer, pin, method, go_intent=None,
547                         persistent=False, freq=None, freq2=None,
548                         max_oper_chwidth=None, ht40=False, vht=False):
549         if not self.discover_peer(peer):
550             raise Exception("Peer " + peer + " not found")
551         self.dump_monitor()
552         if pin:
553             cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
554         else:
555             cmd = "P2P_CONNECT " + peer + " " + method + " auth"
556         if go_intent:
557             cmd = cmd + ' go_intent=' + str(go_intent)
558         if freq:
559             cmd = cmd + ' freq=' + str(freq)
560         if freq2:
561             cmd = cmd + ' freq2=' + str(freq2)
562         if max_oper_chwidth:
563             cmd = cmd + ' max_oper_chwidth=' + str(max_oper_chwidth)
564         if ht40:
565             cmd = cmd + ' ht40'
566         if vht:
567             cmd = cmd + ' vht'
568         if persistent:
569             cmd = cmd + " persistent"
570         if "OK" in self.global_request(cmd):
571             return None
572         raise Exception("P2P_CONNECT (auth) failed")
573
574     def p2p_go_neg_auth_result(self, timeout=1, expect_failure=False):
575         go_neg_res = None
576         ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
577                                      "P2P-GO-NEG-FAILURE"], timeout);
578         if ev is None:
579             if expect_failure:
580                 return None
581             raise Exception("Group formation timed out")
582         if "P2P-GO-NEG-SUCCESS" in ev:
583             go_neg_res = ev
584             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout);
585             if ev is None:
586                 if expect_failure:
587                     return None
588                 raise Exception("Group formation timed out")
589         self.dump_monitor()
590         return self.group_form_result(ev, expect_failure, go_neg_res)
591
592     def p2p_go_neg_init(self, peer, pin, method, timeout=0, go_intent=None,
593                         expect_failure=False, persistent=False,
594                         persistent_id=None, freq=None, provdisc=False,
595                         wait_group=True, freq2=None, max_oper_chwidth=None,
596                         ht40=False, vht=False):
597         if not self.discover_peer(peer):
598             raise Exception("Peer " + peer + " not found")
599         self.dump_monitor()
600         if pin:
601             cmd = "P2P_CONNECT " + peer + " " + pin + " " + method
602         else:
603             cmd = "P2P_CONNECT " + peer + " " + method
604         if go_intent:
605             cmd = cmd + ' go_intent=' + str(go_intent)
606         if freq:
607             cmd = cmd + ' freq=' + str(freq)
608         if freq2:
609             cmd = cmd + ' freq2=' + str(freq2)
610         if max_oper_chwidth:
611             cmd = cmd + ' max_oper_chwidth=' + str(max_oper_chwidth)
612         if ht40:
613             cmd = cmd + ' ht40'
614         if vht:
615             cmd = cmd + ' vht'
616         if persistent:
617             cmd = cmd + " persistent"
618         elif persistent_id:
619             cmd = cmd + " persistent=" + persistent_id
620         if provdisc:
621             cmd = cmd + " provdisc"
622         if "OK" in self.global_request(cmd):
623             if timeout == 0:
624                 self.dump_monitor()
625                 return None
626             go_neg_res = None
627             ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
628                                          "P2P-GO-NEG-FAILURE"], timeout)
629             if ev is None:
630                 if expect_failure:
631                     return None
632                 raise Exception("Group formation timed out")
633             if "P2P-GO-NEG-SUCCESS" in ev:
634                 if not wait_group:
635                     return ev
636                 go_neg_res = ev
637                 ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
638                 if ev is None:
639                     if expect_failure:
640                         return None
641                     raise Exception("Group formation timed out")
642             self.dump_monitor()
643             return self.group_form_result(ev, expect_failure, go_neg_res)
644         raise Exception("P2P_CONNECT failed")
645
646     def wait_event(self, events, timeout=10):
647         start = os.times()[4]
648         while True:
649             while self.mon.pending():
650                 ev = self.mon.recv()
651                 logger.debug(self.ifname + ": " + ev)
652                 for event in events:
653                     if event in ev:
654                         return ev
655             now = os.times()[4]
656             remaining = start + timeout - now
657             if remaining <= 0:
658                 break
659             if not self.mon.pending(timeout=remaining):
660                 break
661         return None
662
663     def wait_global_event(self, events, timeout):
664         if self.global_iface is None:
665             self.wait_event(events, timeout)
666         else:
667             start = os.times()[4]
668             while True:
669                 while self.global_mon.pending():
670                     ev = self.global_mon.recv()
671                     logger.debug(self.ifname + "(global): " + ev)
672                     for event in events:
673                         if event in ev:
674                             return ev
675                 now = os.times()[4]
676                 remaining = start + timeout - now
677                 if remaining <= 0:
678                     break
679                 if not self.global_mon.pending(timeout=remaining):
680                     break
681         return None
682
683     def wait_group_event(self, events, timeout=10):
684         if self.group_ifname and self.group_ifname != self.ifname:
685             if self.gctrl_mon is None:
686                 return None
687             start = os.times()[4]
688             while True:
689                 while self.gctrl_mon.pending():
690                     ev = self.gctrl_mon.recv()
691                     logger.debug(self.group_ifname + ": " + ev)
692                     for event in events:
693                         if event in ev:
694                             return ev
695                 now = os.times()[4]
696                 remaining = start + timeout - now
697                 if remaining <= 0:
698                     break
699                 if not self.gctrl_mon.pending(timeout=remaining):
700                     break
701             return None
702
703         return self.wait_event(events, timeout)
704
705     def wait_go_ending_session(self):
706         if self.gctrl_mon:
707             try:
708                 self.gctrl_mon.detach()
709             except:
710                 pass
711             self.gctrl_mon = None
712         ev = self.wait_global_event(["P2P-GROUP-REMOVED"], timeout=3)
713         if ev is None:
714             raise Exception("Group removal event timed out")
715         if "reason=GO_ENDING_SESSION" not in ev:
716             raise Exception("Unexpected group removal reason")
717
718     def dump_monitor(self):
719         count_iface = 0
720         count_global = 0
721         while self.mon.pending():
722             ev = self.mon.recv()
723             logger.debug(self.ifname + ": " + ev)
724             count_iface += 1
725         while self.global_mon and self.global_mon.pending():
726             ev = self.global_mon.recv()
727             logger.debug(self.ifname + "(global): " + ev)
728             count_global += 1
729         return (count_iface, count_global)
730
731     def remove_group(self, ifname=None):
732         if self.gctrl_mon:
733             try:
734                 self.gctrl_mon.detach()
735             except:
736                 pass
737             self.gctrl_mon = None
738         if ifname is None:
739             ifname = self.group_ifname if self.group_ifname else self.ifname
740         if "OK" not in self.global_request("P2P_GROUP_REMOVE " + ifname):
741             raise Exception("Group could not be removed")
742         self.group_ifname = None
743
744     def p2p_start_go(self, persistent=None, freq=None, no_event_clear=False):
745         self.dump_monitor()
746         cmd = "P2P_GROUP_ADD"
747         if persistent is None:
748             pass
749         elif persistent is True:
750             cmd = cmd + " persistent"
751         else:
752             cmd = cmd + " persistent=" + str(persistent)
753         if freq:
754             cmd = cmd + " freq=" + str(freq)
755         if "OK" in self.global_request(cmd):
756             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout=5)
757             if ev is None:
758                 raise Exception("GO start up timed out")
759             if not no_event_clear:
760                 self.dump_monitor()
761             return self.group_form_result(ev)
762         raise Exception("P2P_GROUP_ADD failed")
763
764     def p2p_go_authorize_client(self, pin):
765         cmd = "WPS_PIN any " + pin
766         if "FAIL" in self.group_request(cmd):
767             raise Exception("Failed to authorize client connection on GO")
768         return None
769
770     def p2p_go_authorize_client_pbc(self):
771         cmd = "WPS_PBC"
772         if "FAIL" in self.group_request(cmd):
773             raise Exception("Failed to authorize client connection on GO")
774         return None
775
776     def p2p_connect_group(self, go_addr, pin, timeout=0, social=False,
777                           freq=None):
778         self.dump_monitor()
779         if not self.discover_peer(go_addr, social=social, freq=freq):
780             if social or not self.discover_peer(go_addr, social=social):
781                 raise Exception("GO " + go_addr + " not found")
782         self.dump_monitor()
783         cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
784         if freq:
785             cmd += " freq=" + str(freq)
786         if "OK" in self.global_request(cmd):
787             if timeout == 0:
788                 self.dump_monitor()
789                 return None
790             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
791             if ev is None:
792                 raise Exception("Joining the group timed out")
793             self.dump_monitor()
794             return self.group_form_result(ev)
795         raise Exception("P2P_CONNECT(join) failed")
796
797     def tdls_setup(self, peer):
798         cmd = "TDLS_SETUP " + peer
799         if "FAIL" in self.group_request(cmd):
800             raise Exception("Failed to request TDLS setup")
801         return None
802
803     def tdls_teardown(self, peer):
804         cmd = "TDLS_TEARDOWN " + peer
805         if "FAIL" in self.group_request(cmd):
806             raise Exception("Failed to request TDLS teardown")
807         return None
808
809     def tdls_link_status(self, peer):
810         cmd = "TDLS_LINK_STATUS " + peer
811         ret = self.group_request(cmd)
812         if "FAIL" in ret:
813             raise Exception("Failed to request TDLS link status")
814         return ret
815
816     def tspecs(self):
817         """Return (tsid, up) tuples representing current tspecs"""
818         res = self.request("WMM_AC_STATUS")
819         tspecs = re.findall(r"TSID=(\d+) UP=(\d+)", res)
820         tspecs = [tuple(map(int, tspec)) for tspec in tspecs]
821
822         logger.debug("tspecs: " + str(tspecs))
823         return tspecs
824
825     def add_ts(self, tsid, up, direction="downlink", expect_failure=False,
826                extra=None):
827         params = {
828             "sba": 9000,
829             "nominal_msdu_size": 1500,
830             "min_phy_rate": 6000000,
831             "mean_data_rate": 1500,
832         }
833         cmd = "WMM_AC_ADDTS %s tsid=%d up=%d" % (direction, tsid, up)
834         for (key, value) in params.iteritems():
835             cmd += " %s=%d" % (key, value)
836         if extra:
837             cmd += " " + extra
838
839         if self.request(cmd).strip() != "OK":
840             raise Exception("ADDTS failed (tsid=%d up=%d)" % (tsid, up))
841
842         if expect_failure:
843             ev = self.wait_event(["TSPEC-REQ-FAILED"], timeout=2)
844             if ev is None:
845                 raise Exception("ADDTS failed (time out while waiting failure)")
846             if "tsid=%d" % (tsid) not in ev:
847                 raise Exception("ADDTS failed (invalid tsid in TSPEC-REQ-FAILED")
848             return
849
850         ev = self.wait_event(["TSPEC-ADDED"], timeout=1)
851         if ev is None:
852             raise Exception("ADDTS failed (time out)")
853         if "tsid=%d" % (tsid) not in ev:
854             raise Exception("ADDTS failed (invalid tsid in TSPEC-ADDED)")
855
856         if not (tsid, up) in self.tspecs():
857             raise Exception("ADDTS failed (tsid not in tspec list)")
858
859     def del_ts(self, tsid):
860         if self.request("WMM_AC_DELTS %d" % (tsid)).strip() != "OK":
861             raise Exception("DELTS failed")
862
863         ev = self.wait_event(["TSPEC-REMOVED"], timeout=1)
864         if ev is None:
865             raise Exception("DELTS failed (time out)")
866         if "tsid=%d" % (tsid) not in ev:
867             raise Exception("DELTS failed (invalid tsid in TSPEC-REMOVED)")
868
869         tspecs = [(t, u) for (t, u) in self.tspecs() if t == tsid]
870         if tspecs:
871             raise Exception("DELTS failed (still in tspec list)")
872
873     def connect(self, ssid=None, ssid2=None, **kwargs):
874         logger.info("Connect STA " + self.ifname + " to AP")
875         id = self.add_network()
876         if ssid:
877             self.set_network_quoted(id, "ssid", ssid)
878         elif ssid2:
879             self.set_network(id, "ssid", ssid2)
880
881         quoted = [ "psk", "identity", "anonymous_identity", "password",
882                    "ca_cert", "client_cert", "private_key",
883                    "private_key_passwd", "ca_cert2", "client_cert2",
884                    "private_key2", "phase1", "phase2", "domain_suffix_match",
885                    "altsubject_match", "subject_match", "pac_file", "dh_file",
886                    "bgscan", "ht_mcs", "id_str", "openssl_ciphers",
887                    "domain_match" ]
888         for field in quoted:
889             if field in kwargs and kwargs[field]:
890                 self.set_network_quoted(id, field, kwargs[field])
891
892         not_quoted = [ "proto", "key_mgmt", "ieee80211w", "pairwise",
893                        "group", "wep_key0", "wep_key1", "wep_key2", "wep_key3",
894                        "wep_tx_keyidx", "scan_freq", "freq_list", "eap",
895                        "eapol_flags", "fragment_size", "scan_ssid", "auth_alg",
896                        "wpa_ptk_rekey", "disable_ht", "disable_vht", "bssid",
897                        "disable_max_amsdu", "ampdu_factor", "ampdu_density",
898                        "disable_ht40", "disable_sgi", "disable_ldpc",
899                        "ht40_intolerant", "update_identifier", "mac_addr",
900                        "erp", "bg_scan_period", "bssid_blacklist",
901                        "bssid_whitelist", "mem_only_psk", "eap_workaround" ]
902         for field in not_quoted:
903             if field in kwargs and kwargs[field]:
904                 self.set_network(id, field, kwargs[field])
905
906         if "raw_psk" in kwargs and kwargs['raw_psk']:
907             self.set_network(id, "psk", kwargs['raw_psk'])
908         if "password_hex" in kwargs and kwargs['password_hex']:
909             self.set_network(id, "password", kwargs['password_hex'])
910         if "peerkey" in kwargs and kwargs['peerkey']:
911             self.set_network(id, "peerkey", "1")
912         if "okc" in kwargs and kwargs['okc']:
913             self.set_network(id, "proactive_key_caching", "1")
914         if "ocsp" in kwargs and kwargs['ocsp']:
915             self.set_network(id, "ocsp", str(kwargs['ocsp']))
916         if "only_add_network" in kwargs and kwargs['only_add_network']:
917             return id
918         if "wait_connect" not in kwargs or kwargs['wait_connect']:
919             if "eap" in kwargs:
920                 self.connect_network(id, timeout=20)
921             else:
922                 self.connect_network(id)
923         else:
924             self.dump_monitor()
925             self.select_network(id)
926         return id
927
928     def scan(self, type=None, freq=None, no_wait=False, only_new=False):
929         if type:
930             cmd = "SCAN TYPE=" + type
931         else:
932             cmd = "SCAN"
933         if freq:
934             cmd = cmd + " freq=" + str(freq)
935         if only_new:
936             cmd += " only_new=1"
937         if not no_wait:
938             self.dump_monitor()
939         if not "OK" in self.request(cmd):
940             raise Exception("Failed to trigger scan")
941         if no_wait:
942             return
943         ev = self.wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
944         if ev is None:
945             raise Exception("Scan timed out")
946
947     def scan_for_bss(self, bssid, freq=None, force_scan=False, only_new=False):
948         if not force_scan and self.get_bss(bssid) is not None:
949             return
950         for i in range(0, 10):
951             self.scan(freq=freq, type="ONLY", only_new=only_new)
952             if self.get_bss(bssid) is not None:
953                 return
954         raise Exception("Could not find BSS " + bssid + " in scan")
955
956     def flush_scan_cache(self, freq=2417):
957         self.request("BSS_FLUSH 0")
958         self.scan(freq=freq, only_new=True)
959         res = self.request("SCAN_RESULTS")
960         if len(res.splitlines()) > 1:
961             self.request("BSS_FLUSH 0")
962             self.scan(freq=2422, only_new=True)
963             res = self.request("SCAN_RESULTS")
964             if len(res.splitlines()) > 1:
965                 logger.info("flush_scan_cache: Could not clear all BSS entries. These remain:\n" + res)
966
967     def roam(self, bssid, fail_test=False):
968         self.dump_monitor()
969         if "OK" not in self.request("ROAM " + bssid):
970             raise Exception("ROAM failed")
971         if fail_test:
972             ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
973             if ev is not None:
974                 raise Exception("Unexpected connection")
975             self.dump_monitor()
976             return
977         self.wait_connected(timeout=10, error="Roaming with the AP timed out")
978         self.dump_monitor()
979
980     def roam_over_ds(self, bssid, fail_test=False):
981         self.dump_monitor()
982         if "OK" not in self.request("FT_DS " + bssid):
983             raise Exception("FT_DS failed")
984         if fail_test:
985             ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
986             if ev is not None:
987                 raise Exception("Unexpected connection")
988             self.dump_monitor()
989             return
990         self.wait_connected(timeout=10, error="Roaming with the AP timed out")
991         self.dump_monitor()
992
993     def wps_reg(self, bssid, pin, new_ssid=None, key_mgmt=None, cipher=None,
994                 new_passphrase=None, no_wait=False):
995         self.dump_monitor()
996         if new_ssid:
997             self.request("WPS_REG " + bssid + " " + pin + " " +
998                          new_ssid.encode("hex") + " " + key_mgmt + " " +
999                          cipher + " " + new_passphrase.encode("hex"))
1000             if no_wait:
1001                 return
1002             ev = self.wait_event(["WPS-SUCCESS"], timeout=15)
1003         else:
1004             self.request("WPS_REG " + bssid + " " + pin)
1005             if no_wait:
1006                 return
1007             ev = self.wait_event(["WPS-CRED-RECEIVED"], timeout=15)
1008             if ev is None:
1009                 raise Exception("WPS cred timed out")
1010             ev = self.wait_event(["WPS-FAIL"], timeout=15)
1011         if ev is None:
1012             raise Exception("WPS timed out")
1013         self.wait_connected(timeout=15)
1014
1015     def relog(self):
1016         self.global_request("RELOG")
1017
1018     def wait_completed(self, timeout=10):
1019         for i in range(0, timeout * 2):
1020             if self.get_status_field("wpa_state") == "COMPLETED":
1021                 return
1022             time.sleep(0.5)
1023         raise Exception("Timeout while waiting for COMPLETED state")
1024
1025     def get_capability(self, field):
1026         res = self.request("GET_CAPABILITY " + field)
1027         if "FAIL" in res:
1028             return None
1029         return res.split(' ')
1030
1031     def get_bss(self, bssid, ifname=None):
1032         if not ifname or ifname == self.ifname:
1033             res = self.request("BSS " + bssid)
1034         elif ifname == self.group_ifname:
1035             res = self.group_request("BSS " + bssid)
1036         else:
1037             return None
1038
1039         if "FAIL" in res:
1040             return None
1041         lines = res.splitlines()
1042         vals = dict()
1043         for l in lines:
1044             [name,value] = l.split('=', 1)
1045             vals[name] = value
1046         if len(vals) == 0:
1047             return None
1048         return vals
1049
1050     def get_pmksa(self, bssid):
1051         res = self.request("PMKSA")
1052         lines = res.splitlines()
1053         for l in lines:
1054             if bssid not in l:
1055                 continue
1056             vals = dict()
1057             [index,aa,pmkid,expiration,opportunistic] = l.split(' ')
1058             vals['index'] = index
1059             vals['pmkid'] = pmkid
1060             vals['expiration'] = expiration
1061             vals['opportunistic'] = opportunistic
1062             return vals
1063         return None
1064
1065     def get_sta(self, addr, info=None, next=False):
1066         cmd = "STA-NEXT " if next else "STA "
1067         if addr is None:
1068             res = self.request("STA-FIRST")
1069         elif info:
1070             res = self.request(cmd + addr + " " + info)
1071         else:
1072             res = self.request(cmd + addr)
1073         lines = res.splitlines()
1074         vals = dict()
1075         first = True
1076         for l in lines:
1077             if first:
1078                 vals['addr'] = l
1079                 first = False
1080             else:
1081                 [name,value] = l.split('=', 1)
1082                 vals[name] = value
1083         return vals
1084
1085     def mgmt_rx(self, timeout=5):
1086         ev = self.wait_event(["MGMT-RX"], timeout=timeout)
1087         if ev is None:
1088             return None
1089         msg = {}
1090         items = ev.split(' ')
1091         field,val = items[1].split('=')
1092         if field != "freq":
1093             raise Exception("Unexpected MGMT-RX event format: " + ev)
1094         msg['freq'] = val
1095         frame = binascii.unhexlify(items[4])
1096         msg['frame'] = frame
1097
1098         hdr = struct.unpack('<HH6B6B6BH', frame[0:24])
1099         msg['fc'] = hdr[0]
1100         msg['subtype'] = (hdr[0] >> 4) & 0xf
1101         hdr = hdr[1:]
1102         msg['duration'] = hdr[0]
1103         hdr = hdr[1:]
1104         msg['da'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
1105         hdr = hdr[6:]
1106         msg['sa'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
1107         hdr = hdr[6:]
1108         msg['bssid'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
1109         hdr = hdr[6:]
1110         msg['seq_ctrl'] = hdr[0]
1111         msg['payload'] = frame[24:]
1112
1113         return msg
1114
1115     def wait_connected(self, timeout=10, error="Connection timed out"):
1116         ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
1117         if ev is None:
1118             raise Exception(error)
1119         return ev
1120
1121     def wait_disconnected(self, timeout=10, error="Disconnection timed out"):
1122         ev = self.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=timeout)
1123         if ev is None:
1124             raise Exception(error)
1125         return ev
1126
1127     def get_group_ifname(self):
1128         return self.group_ifname if self.group_ifname else self.ifname
1129
1130     def get_config(self):
1131         res = self.request("DUMP")
1132         if res.startswith("FAIL"):
1133             raise Exception("DUMP failed")
1134         lines = res.splitlines()
1135         vals = dict()
1136         for l in lines:
1137             [name,value] = l.split('=', 1)
1138             vals[name] = value
1139         return vals
1140
1141     def asp_provision(self, peer, adv_id, adv_mac, session_id, session_mac,
1142                       method="1000", info="", status=None, cpt=None, role=None):
1143         if status is None:
1144             cmd = "P2P_ASP_PROVISION"
1145             params = "info='%s' method=%s" % (info, method)
1146         else:
1147             cmd = "P2P_ASP_PROVISION_RESP"
1148             params = "status=%d" % status
1149
1150         if role is not None:
1151             params += " role=" + role
1152         if cpt is not None:
1153             params += " cpt=" + cpt
1154
1155         if "OK" not in self.global_request("%s %s adv_id=%s adv_mac=%s session=%d session_mac=%s %s" %
1156                                            (cmd, peer, adv_id, adv_mac, session_id, session_mac, params)):
1157             raise Exception("%s request failed" % cmd)