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