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