tests: P2P device discovery and control character in Device Name
[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             if res.startswith("FAIL"):
366                 return dict()
367         lines = res.splitlines()
368         vals = dict()
369         for l in lines:
370             try:
371                 [name,value] = l.split('=', 1)
372             except ValueError:
373                 logger.info(self.ifname + ": Ignore unexpected status-driver line: " + l)
374                 continue
375             vals[name] = value
376         return vals
377
378     def get_driver_status_field(self, field, ifname=None):
379         vals = self.get_driver_status(ifname)
380         if field in vals:
381             return vals[field]
382         return None
383
384     def get_mcc(self):
385         mcc = int(self.get_driver_status_field('capa.num_multichan_concurrent'))
386         return 1 if mcc < 2 else mcc
387
388     def get_mib(self):
389         res = self.request("MIB")
390         lines = res.splitlines()
391         vals = dict()
392         for l in lines:
393             try:
394                 [name,value] = l.split('=', 1)
395                 vals[name] = value
396             except ValueError, e:
397                 logger.info(self.ifname + ": Ignore unexpected MIB line: " + l)
398         return vals
399
400     def p2p_dev_addr(self):
401         return self.get_status_field("p2p_device_address")
402
403     def p2p_interface_addr(self):
404         return self.get_group_status_field("address")
405
406     def own_addr(self):
407         try:
408             res = self.p2p_interface_addr()
409         except:
410             res = self.p2p_dev_addr()
411         return res
412
413     def p2p_listen(self):
414         return self.global_request("P2P_LISTEN")
415
416     def p2p_ext_listen(self, period, interval):
417         return self.global_request("P2P_EXT_LISTEN %d %d" % (period, interval))
418
419     def p2p_cancel_ext_listen(self):
420         return self.global_request("P2P_EXT_LISTEN")
421
422     def p2p_find(self, social=False, progressive=False, dev_id=None,
423                  dev_type=None, delay=None, freq=None):
424         cmd = "P2P_FIND"
425         if social:
426             cmd = cmd + " type=social"
427         elif progressive:
428             cmd = cmd + " type=progressive"
429         if dev_id:
430             cmd = cmd + " dev_id=" + dev_id
431         if dev_type:
432             cmd = cmd + " dev_type=" + dev_type
433         if delay:
434             cmd = cmd + " delay=" + str(delay)
435         if freq:
436             cmd = cmd + " freq=" + str(freq)
437         return self.global_request(cmd)
438
439     def p2p_stop_find(self):
440         return self.global_request("P2P_STOP_FIND")
441
442     def wps_read_pin(self):
443         self.pin = self.request("WPS_PIN get").rstrip("\n")
444         if "FAIL" in self.pin:
445             raise Exception("Could not generate PIN")
446         return self.pin
447
448     def peer_known(self, peer, full=True):
449         res = self.global_request("P2P_PEER " + peer)
450         if peer.lower() not in res.lower():
451             return False
452         if not full:
453             return True
454         return "[PROBE_REQ_ONLY]" not in res
455
456     def discover_peer(self, peer, full=True, timeout=15, social=True,
457                       force_find=False, freq=None):
458         logger.info(self.ifname + ": Trying to discover peer " + peer)
459         if not force_find and self.peer_known(peer, full):
460             return True
461         self.p2p_find(social, freq=freq)
462         count = 0
463         while count < timeout * 4:
464             time.sleep(0.25)
465             count = count + 1
466             if self.peer_known(peer, full):
467                 return True
468         return False
469
470     def get_peer(self, peer):
471         res = self.global_request("P2P_PEER " + peer)
472         if peer.lower() not in res.lower():
473             raise Exception("Peer information not available")
474         lines = res.splitlines()
475         vals = dict()
476         for l in lines:
477             if '=' in l:
478                 [name,value] = l.split('=', 1)
479                 vals[name] = value
480         return vals
481
482     def group_form_result(self, ev, expect_failure=False, go_neg_res=None):
483         if expect_failure:
484             if "P2P-GROUP-STARTED" in ev:
485                 raise Exception("Group formation succeeded when expecting failure")
486             exp = r'<.>(P2P-GO-NEG-FAILURE) status=([0-9]*)'
487             s = re.split(exp, ev)
488             if len(s) < 3:
489                 return None
490             res = {}
491             res['result'] = 'go-neg-failed'
492             res['status'] = int(s[2])
493             return res
494
495         if "P2P-GROUP-STARTED" not in ev:
496             raise Exception("No P2P-GROUP-STARTED event seen")
497
498         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.]*)'
499         s = re.split(exp, ev)
500         if len(s) < 11:
501             exp = r'<.>(P2P-GROUP-STARTED) ([^ ]*) ([^ ]*) ssid="(.*)" freq=([0-9]*) ((?:psk=.*)|(?:passphrase=".*")) go_dev_addr=([0-9a-f:]*)'
502             s = re.split(exp, ev)
503             if len(s) < 8:
504                 raise Exception("Could not parse P2P-GROUP-STARTED")
505         res = {}
506         res['result'] = 'success'
507         res['ifname'] = s[2]
508         self.group_ifname = s[2]
509         try:
510             self.gctrl_mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, self.group_ifname))
511             self.gctrl_mon.attach()
512         except:
513             logger.debug("Could not open monitor socket for group interface")
514             self.gctrl_mon = None
515         res['role'] = s[3]
516         res['ssid'] = s[4]
517         res['freq'] = s[5]
518         if "[PERSISTENT]" in ev:
519             res['persistent'] = True
520         else:
521             res['persistent'] = False
522         p = re.match(r'psk=([0-9a-f]*)', s[6])
523         if p:
524             res['psk'] = p.group(1)
525         p = re.match(r'passphrase="(.*)"', s[6])
526         if p:
527             res['passphrase'] = p.group(1)
528         res['go_dev_addr'] = s[7]
529
530         if len(s) > 8 and len(s[8]) > 0:
531             res['ip_addr'] = s[8]
532         if len(s) > 9:
533             res['ip_mask'] = s[9]
534         if len(s) > 10:
535             res['go_ip_addr'] = s[10]
536
537         if go_neg_res:
538             exp = r'<.>(P2P-GO-NEG-SUCCESS) role=(GO|client) freq=([0-9]*)'
539             s = re.split(exp, go_neg_res)
540             if len(s) < 4:
541                 raise Exception("Could not parse P2P-GO-NEG-SUCCESS")
542             res['go_neg_role'] = s[2]
543             res['go_neg_freq'] = s[3]
544
545         return res
546
547     def p2p_go_neg_auth(self, peer, pin, method, go_intent=None, persistent=False, freq=None):
548         if not self.discover_peer(peer):
549             raise Exception("Peer " + peer + " not found")
550         self.dump_monitor()
551         if pin:
552             cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
553         else:
554             cmd = "P2P_CONNECT " + peer + " " + method + " auth"
555         if go_intent:
556             cmd = cmd + ' go_intent=' + str(go_intent)
557         if freq:
558             cmd = cmd + ' freq=' + str(freq)
559         if persistent:
560             cmd = cmd + " persistent"
561         if "OK" in self.global_request(cmd):
562             return None
563         raise Exception("P2P_CONNECT (auth) failed")
564
565     def p2p_go_neg_auth_result(self, timeout=1, expect_failure=False):
566         go_neg_res = None
567         ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
568                                      "P2P-GO-NEG-FAILURE"], timeout);
569         if ev is None:
570             if expect_failure:
571                 return None
572             raise Exception("Group formation timed out")
573         if "P2P-GO-NEG-SUCCESS" in ev:
574             go_neg_res = ev
575             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout);
576             if ev is None:
577                 if expect_failure:
578                     return None
579                 raise Exception("Group formation timed out")
580         self.dump_monitor()
581         return self.group_form_result(ev, expect_failure, go_neg_res)
582
583     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):
584         if not self.discover_peer(peer):
585             raise Exception("Peer " + peer + " not found")
586         self.dump_monitor()
587         if pin:
588             cmd = "P2P_CONNECT " + peer + " " + pin + " " + method
589         else:
590             cmd = "P2P_CONNECT " + peer + " " + method
591         if go_intent:
592             cmd = cmd + ' go_intent=' + str(go_intent)
593         if freq:
594             cmd = cmd + ' freq=' + str(freq)
595         if persistent:
596             cmd = cmd + " persistent"
597         elif persistent_id:
598             cmd = cmd + " persistent=" + persistent_id
599         if provdisc:
600             cmd = cmd + " provdisc"
601         if "OK" in self.global_request(cmd):
602             if timeout == 0:
603                 self.dump_monitor()
604                 return None
605             go_neg_res = None
606             ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
607                                          "P2P-GO-NEG-FAILURE"], timeout)
608             if ev is None:
609                 if expect_failure:
610                     return None
611                 raise Exception("Group formation timed out")
612             if "P2P-GO-NEG-SUCCESS" in ev:
613                 if not wait_group:
614                     return ev
615                 go_neg_res = ev
616                 ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
617                 if ev is None:
618                     if expect_failure:
619                         return None
620                     raise Exception("Group formation timed out")
621             self.dump_monitor()
622             return self.group_form_result(ev, expect_failure, go_neg_res)
623         raise Exception("P2P_CONNECT failed")
624
625     def wait_event(self, events, timeout=10):
626         start = os.times()[4]
627         while True:
628             while self.mon.pending():
629                 ev = self.mon.recv()
630                 logger.debug(self.ifname + ": " + ev)
631                 for event in events:
632                     if event in ev:
633                         return ev
634             now = os.times()[4]
635             remaining = start + timeout - now
636             if remaining <= 0:
637                 break
638             if not self.mon.pending(timeout=remaining):
639                 break
640         return None
641
642     def wait_global_event(self, events, timeout):
643         if self.global_iface is None:
644             self.wait_event(events, timeout)
645         else:
646             start = os.times()[4]
647             while True:
648                 while self.global_mon.pending():
649                     ev = self.global_mon.recv()
650                     logger.debug(self.ifname + "(global): " + ev)
651                     for event in events:
652                         if event in ev:
653                             return ev
654                 now = os.times()[4]
655                 remaining = start + timeout - now
656                 if remaining <= 0:
657                     break
658                 if not self.global_mon.pending(timeout=remaining):
659                     break
660         return None
661
662     def wait_group_event(self, events, timeout=10):
663         if self.group_ifname and self.group_ifname != self.ifname:
664             if self.gctrl_mon is None:
665                 return None
666             start = os.times()[4]
667             while True:
668                 while self.gctrl_mon.pending():
669                     ev = self.gctrl_mon.recv()
670                     logger.debug(self.group_ifname + ": " + ev)
671                     for event in events:
672                         if event in ev:
673                             return ev
674                 now = os.times()[4]
675                 remaining = start + timeout - now
676                 if remaining <= 0:
677                     break
678                 if not self.gctrl_mon.pending(timeout=remaining):
679                     break
680             return None
681
682         return self.wait_event(events, timeout)
683
684     def wait_go_ending_session(self):
685         if self.gctrl_mon:
686             try:
687                 self.gctrl_mon.detach()
688             except:
689                 pass
690             self.gctrl_mon = None
691         ev = self.wait_global_event(["P2P-GROUP-REMOVED"], timeout=3)
692         if ev is None:
693             raise Exception("Group removal event timed out")
694         if "reason=GO_ENDING_SESSION" not in ev:
695             raise Exception("Unexpected group removal reason")
696
697     def dump_monitor(self):
698         count_iface = 0
699         count_global = 0
700         while self.mon.pending():
701             ev = self.mon.recv()
702             logger.debug(self.ifname + ": " + ev)
703             count_iface += 1
704         while self.global_mon and self.global_mon.pending():
705             ev = self.global_mon.recv()
706             logger.debug(self.ifname + "(global): " + ev)
707             count_global += 1
708         return (count_iface, count_global)
709
710     def remove_group(self, ifname=None):
711         if self.gctrl_mon:
712             try:
713                 self.gctrl_mon.detach()
714             except:
715                 pass
716             self.gctrl_mon = None
717         if ifname is None:
718             ifname = self.group_ifname if self.group_ifname else self.ifname
719         if "OK" not in self.global_request("P2P_GROUP_REMOVE " + ifname):
720             raise Exception("Group could not be removed")
721         self.group_ifname = None
722
723     def p2p_start_go(self, persistent=None, freq=None, no_event_clear=False):
724         self.dump_monitor()
725         cmd = "P2P_GROUP_ADD"
726         if persistent is None:
727             pass
728         elif persistent is True:
729             cmd = cmd + " persistent"
730         else:
731             cmd = cmd + " persistent=" + str(persistent)
732         if freq:
733             cmd = cmd + " freq=" + str(freq)
734         if "OK" in self.global_request(cmd):
735             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout=5)
736             if ev is None:
737                 raise Exception("GO start up timed out")
738             if not no_event_clear:
739                 self.dump_monitor()
740             return self.group_form_result(ev)
741         raise Exception("P2P_GROUP_ADD failed")
742
743     def p2p_go_authorize_client(self, pin):
744         cmd = "WPS_PIN any " + pin
745         if "FAIL" in self.group_request(cmd):
746             raise Exception("Failed to authorize client connection on GO")
747         return None
748
749     def p2p_go_authorize_client_pbc(self):
750         cmd = "WPS_PBC"
751         if "FAIL" in self.group_request(cmd):
752             raise Exception("Failed to authorize client connection on GO")
753         return None
754
755     def p2p_connect_group(self, go_addr, pin, timeout=0, social=False,
756                           freq=None):
757         self.dump_monitor()
758         if not self.discover_peer(go_addr, social=social, freq=freq):
759             if social or not self.discover_peer(go_addr, social=social):
760                 raise Exception("GO " + go_addr + " not found")
761         self.dump_monitor()
762         cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
763         if freq:
764             cmd += " freq=" + str(freq)
765         if "OK" in self.global_request(cmd):
766             if timeout == 0:
767                 self.dump_monitor()
768                 return None
769             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
770             if ev is None:
771                 raise Exception("Joining the group timed out")
772             self.dump_monitor()
773             return self.group_form_result(ev)
774         raise Exception("P2P_CONNECT(join) failed")
775
776     def tdls_setup(self, peer):
777         cmd = "TDLS_SETUP " + peer
778         if "FAIL" in self.group_request(cmd):
779             raise Exception("Failed to request TDLS setup")
780         return None
781
782     def tdls_teardown(self, peer):
783         cmd = "TDLS_TEARDOWN " + peer
784         if "FAIL" in self.group_request(cmd):
785             raise Exception("Failed to request TDLS teardown")
786         return None
787
788     def tdls_link_status(self, peer):
789         cmd = "TDLS_LINK_STATUS " + peer
790         ret = self.group_request(cmd)
791         if "FAIL" in ret:
792             raise Exception("Failed to request TDLS link status")
793         return ret
794
795     def tspecs(self):
796         """Return (tsid, up) tuples representing current tspecs"""
797         res = self.request("WMM_AC_STATUS")
798         tspecs = re.findall(r"TSID=(\d+) UP=(\d+)", res)
799         tspecs = [tuple(map(int, tspec)) for tspec in tspecs]
800
801         logger.debug("tspecs: " + str(tspecs))
802         return tspecs
803
804     def add_ts(self, tsid, up, direction="downlink", expect_failure=False,
805                extra=None):
806         params = {
807             "sba": 9000,
808             "nominal_msdu_size": 1500,
809             "min_phy_rate": 6000000,
810             "mean_data_rate": 1500,
811         }
812         cmd = "WMM_AC_ADDTS %s tsid=%d up=%d" % (direction, tsid, up)
813         for (key, value) in params.iteritems():
814             cmd += " %s=%d" % (key, value)
815         if extra:
816             cmd += " " + extra
817
818         if self.request(cmd).strip() != "OK":
819             raise Exception("ADDTS failed (tsid=%d up=%d)" % (tsid, up))
820
821         if expect_failure:
822             ev = self.wait_event(["TSPEC-REQ-FAILED"], timeout=2)
823             if ev is None:
824                 raise Exception("ADDTS failed (time out while waiting failure)")
825             if "tsid=%d" % (tsid) not in ev:
826                 raise Exception("ADDTS failed (invalid tsid in TSPEC-REQ-FAILED")
827             return
828
829         ev = self.wait_event(["TSPEC-ADDED"], timeout=1)
830         if ev is None:
831             raise Exception("ADDTS failed (time out)")
832         if "tsid=%d" % (tsid) not in ev:
833             raise Exception("ADDTS failed (invalid tsid in TSPEC-ADDED)")
834
835         if not (tsid, up) in self.tspecs():
836             raise Exception("ADDTS failed (tsid not in tspec list)")
837
838     def del_ts(self, tsid):
839         if self.request("WMM_AC_DELTS %d" % (tsid)).strip() != "OK":
840             raise Exception("DELTS failed")
841
842         ev = self.wait_event(["TSPEC-REMOVED"], timeout=1)
843         if ev is None:
844             raise Exception("DELTS failed (time out)")
845         if "tsid=%d" % (tsid) not in ev:
846             raise Exception("DELTS failed (invalid tsid in TSPEC-REMOVED)")
847
848         tspecs = [(t, u) for (t, u) in self.tspecs() if t == tsid]
849         if tspecs:
850             raise Exception("DELTS failed (still in tspec list)")
851
852     def connect(self, ssid=None, ssid2=None, **kwargs):
853         logger.info("Connect STA " + self.ifname + " to AP")
854         id = self.add_network()
855         if ssid:
856             self.set_network_quoted(id, "ssid", ssid)
857         elif ssid2:
858             self.set_network(id, "ssid", ssid2)
859
860         quoted = [ "psk", "identity", "anonymous_identity", "password",
861                    "ca_cert", "client_cert", "private_key",
862                    "private_key_passwd", "ca_cert2", "client_cert2",
863                    "private_key2", "phase1", "phase2", "domain_suffix_match",
864                    "altsubject_match", "subject_match", "pac_file", "dh_file",
865                    "bgscan", "ht_mcs", "id_str", "openssl_ciphers",
866                    "domain_match" ]
867         for field in quoted:
868             if field in kwargs and kwargs[field]:
869                 self.set_network_quoted(id, field, kwargs[field])
870
871         not_quoted = [ "proto", "key_mgmt", "ieee80211w", "pairwise",
872                        "group", "wep_key0", "wep_key1", "wep_key2", "wep_key3",
873                        "wep_tx_keyidx", "scan_freq", "freq_list", "eap",
874                        "eapol_flags", "fragment_size", "scan_ssid", "auth_alg",
875                        "wpa_ptk_rekey", "disable_ht", "disable_vht", "bssid",
876                        "disable_max_amsdu", "ampdu_factor", "ampdu_density",
877                        "disable_ht40", "disable_sgi", "disable_ldpc",
878                        "ht40_intolerant", "update_identifier", "mac_addr",
879                        "erp", "bg_scan_period", "bssid_blacklist",
880                        "bssid_whitelist", "mem_only_psk", "eap_workaround" ]
881         for field in not_quoted:
882             if field in kwargs and kwargs[field]:
883                 self.set_network(id, field, kwargs[field])
884
885         if "raw_psk" in kwargs and kwargs['raw_psk']:
886             self.set_network(id, "psk", kwargs['raw_psk'])
887         if "password_hex" in kwargs and kwargs['password_hex']:
888             self.set_network(id, "password", kwargs['password_hex'])
889         if "peerkey" in kwargs and kwargs['peerkey']:
890             self.set_network(id, "peerkey", "1")
891         if "okc" in kwargs and kwargs['okc']:
892             self.set_network(id, "proactive_key_caching", "1")
893         if "ocsp" in kwargs and kwargs['ocsp']:
894             self.set_network(id, "ocsp", str(kwargs['ocsp']))
895         if "only_add_network" in kwargs and kwargs['only_add_network']:
896             return id
897         if "wait_connect" not in kwargs or kwargs['wait_connect']:
898             if "eap" in kwargs:
899                 self.connect_network(id, timeout=20)
900             else:
901                 self.connect_network(id)
902         else:
903             self.dump_monitor()
904             self.select_network(id)
905         return id
906
907     def scan(self, type=None, freq=None, no_wait=False, only_new=False):
908         if type:
909             cmd = "SCAN TYPE=" + type
910         else:
911             cmd = "SCAN"
912         if freq:
913             cmd = cmd + " freq=" + str(freq)
914         if only_new:
915             cmd += " only_new=1"
916         if not no_wait:
917             self.dump_monitor()
918         if not "OK" in self.request(cmd):
919             raise Exception("Failed to trigger scan")
920         if no_wait:
921             return
922         ev = self.wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
923         if ev is None:
924             raise Exception("Scan timed out")
925
926     def scan_for_bss(self, bssid, freq=None, force_scan=False, only_new=False):
927         if not force_scan and self.get_bss(bssid) is not None:
928             return
929         for i in range(0, 10):
930             self.scan(freq=freq, type="ONLY", only_new=only_new)
931             if self.get_bss(bssid) is not None:
932                 return
933         raise Exception("Could not find BSS " + bssid + " in scan")
934
935     def flush_scan_cache(self, freq=2417):
936         self.request("BSS_FLUSH 0")
937         self.scan(freq=freq, only_new=True)
938         res = self.request("SCAN_RESULTS")
939         if len(res.splitlines()) > 1:
940             self.request("BSS_FLUSH 0")
941             self.scan(freq=2422, only_new=True)
942             res = self.request("SCAN_RESULTS")
943             if len(res.splitlines()) > 1:
944                 logger.info("flush_scan_cache: Could not clear all BSS entries. These remain:\n" + res)
945
946     def roam(self, bssid, fail_test=False):
947         self.dump_monitor()
948         if "OK" not in self.request("ROAM " + bssid):
949             raise Exception("ROAM failed")
950         if fail_test:
951             ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
952             if ev is not None:
953                 raise Exception("Unexpected connection")
954             self.dump_monitor()
955             return
956         self.wait_connected(timeout=10, error="Roaming with the AP timed out")
957         self.dump_monitor()
958
959     def roam_over_ds(self, bssid, fail_test=False):
960         self.dump_monitor()
961         if "OK" not in self.request("FT_DS " + bssid):
962             raise Exception("FT_DS failed")
963         if fail_test:
964             ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
965             if ev is not None:
966                 raise Exception("Unexpected connection")
967             self.dump_monitor()
968             return
969         self.wait_connected(timeout=10, error="Roaming with the AP timed out")
970         self.dump_monitor()
971
972     def wps_reg(self, bssid, pin, new_ssid=None, key_mgmt=None, cipher=None,
973                 new_passphrase=None, no_wait=False):
974         self.dump_monitor()
975         if new_ssid:
976             self.request("WPS_REG " + bssid + " " + pin + " " +
977                          new_ssid.encode("hex") + " " + key_mgmt + " " +
978                          cipher + " " + new_passphrase.encode("hex"))
979             if no_wait:
980                 return
981             ev = self.wait_event(["WPS-SUCCESS"], timeout=15)
982         else:
983             self.request("WPS_REG " + bssid + " " + pin)
984             if no_wait:
985                 return
986             ev = self.wait_event(["WPS-CRED-RECEIVED"], timeout=15)
987             if ev is None:
988                 raise Exception("WPS cred timed out")
989             ev = self.wait_event(["WPS-FAIL"], timeout=15)
990         if ev is None:
991             raise Exception("WPS timed out")
992         self.wait_connected(timeout=15)
993
994     def relog(self):
995         self.global_request("RELOG")
996
997     def wait_completed(self, timeout=10):
998         for i in range(0, timeout * 2):
999             if self.get_status_field("wpa_state") == "COMPLETED":
1000                 return
1001             time.sleep(0.5)
1002         raise Exception("Timeout while waiting for COMPLETED state")
1003
1004     def get_capability(self, field):
1005         res = self.request("GET_CAPABILITY " + field)
1006         if "FAIL" in res:
1007             return None
1008         return res.split(' ')
1009
1010     def get_bss(self, bssid, ifname=None):
1011         if not ifname or ifname == self.ifname:
1012             res = self.request("BSS " + bssid)
1013         elif ifname == self.group_ifname:
1014             res = self.group_request("BSS " + bssid)
1015         else:
1016             return None
1017
1018         if "FAIL" in res:
1019             return None
1020         lines = res.splitlines()
1021         vals = dict()
1022         for l in lines:
1023             [name,value] = l.split('=', 1)
1024             vals[name] = value
1025         if len(vals) == 0:
1026             return None
1027         return vals
1028
1029     def get_pmksa(self, bssid):
1030         res = self.request("PMKSA")
1031         lines = res.splitlines()
1032         for l in lines:
1033             if bssid not in l:
1034                 continue
1035             vals = dict()
1036             [index,aa,pmkid,expiration,opportunistic] = l.split(' ')
1037             vals['index'] = index
1038             vals['pmkid'] = pmkid
1039             vals['expiration'] = expiration
1040             vals['opportunistic'] = opportunistic
1041             return vals
1042         return None
1043
1044     def get_sta(self, addr, info=None, next=False):
1045         cmd = "STA-NEXT " if next else "STA "
1046         if addr is None:
1047             res = self.request("STA-FIRST")
1048         elif info:
1049             res = self.request(cmd + addr + " " + info)
1050         else:
1051             res = self.request(cmd + addr)
1052         lines = res.splitlines()
1053         vals = dict()
1054         first = True
1055         for l in lines:
1056             if first:
1057                 vals['addr'] = l
1058                 first = False
1059             else:
1060                 [name,value] = l.split('=', 1)
1061                 vals[name] = value
1062         return vals
1063
1064     def mgmt_rx(self, timeout=5):
1065         ev = self.wait_event(["MGMT-RX"], timeout=timeout)
1066         if ev is None:
1067             return None
1068         msg = {}
1069         items = ev.split(' ')
1070         field,val = items[1].split('=')
1071         if field != "freq":
1072             raise Exception("Unexpected MGMT-RX event format: " + ev)
1073         msg['freq'] = val
1074         frame = binascii.unhexlify(items[4])
1075         msg['frame'] = frame
1076
1077         hdr = struct.unpack('<HH6B6B6BH', frame[0:24])
1078         msg['fc'] = hdr[0]
1079         msg['subtype'] = (hdr[0] >> 4) & 0xf
1080         hdr = hdr[1:]
1081         msg['duration'] = hdr[0]
1082         hdr = hdr[1:]
1083         msg['da'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
1084         hdr = hdr[6:]
1085         msg['sa'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
1086         hdr = hdr[6:]
1087         msg['bssid'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
1088         hdr = hdr[6:]
1089         msg['seq_ctrl'] = hdr[0]
1090         msg['payload'] = frame[24:]
1091
1092         return msg
1093
1094     def wait_connected(self, timeout=10, error="Connection timed out"):
1095         ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
1096         if ev is None:
1097             raise Exception(error)
1098         return ev
1099
1100     def wait_disconnected(self, timeout=10, error="Disconnection timed out"):
1101         ev = self.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=timeout)
1102         if ev is None:
1103             raise Exception(error)
1104         return ev
1105
1106     def get_group_ifname(self):
1107         return self.group_ifname if self.group_ifname else self.ifname
1108
1109     def get_config(self):
1110         res = self.request("DUMP")
1111         if res.startswith("FAIL"):
1112             raise Exception("DUMP failed")
1113         lines = res.splitlines()
1114         vals = dict()
1115         for l in lines:
1116             [name,value] = l.split('=', 1)
1117             vals[name] = value
1118         return vals
1119
1120     def asp_provision(self, peer, adv_id, adv_mac, session_id, session_mac,
1121                       method="1000", info="", status=None, cpt=None, role=None):
1122         if status is None:
1123             cmd = "P2P_ASP_PROVISION"
1124             params = "info='%s' method=%s" % (info, method)
1125         else:
1126             cmd = "P2P_ASP_PROVISION_RESP"
1127             params = "status=%d" % status
1128
1129         if role is not None:
1130             params += " role=" + role
1131         if cpt is not None:
1132             params += " cpt=" + cpt
1133
1134         if "OK" not in self.global_request("%s %s adv_id=%s adv_mac=%s session=%d session_mac=%s %s" %
1135                                            (cmd, peer, adv_id, adv_mac, session_id, session_mac, params)):
1136             raise Exception("%s request failed" % cmd)