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