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