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