tests: Close wlan5 control interface monitor more explicitly
[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         cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
505         if go_intent:
506             cmd = cmd + ' go_intent=' + str(go_intent)
507         if freq:
508             cmd = cmd + ' freq=' + str(freq)
509         if persistent:
510             cmd = cmd + " persistent"
511         if "OK" in self.global_request(cmd):
512             return None
513         raise Exception("P2P_CONNECT (auth) failed")
514
515     def p2p_go_neg_auth_result(self, timeout=1, expect_failure=False):
516         go_neg_res = None
517         ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
518                                      "P2P-GO-NEG-FAILURE"], timeout);
519         if ev is None:
520             if expect_failure:
521                 return None
522             raise Exception("Group formation timed out")
523         if "P2P-GO-NEG-SUCCESS" in ev:
524             go_neg_res = ev
525             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout);
526             if ev is None:
527                 if expect_failure:
528                     return None
529                 raise Exception("Group formation timed out")
530         self.dump_monitor()
531         return self.group_form_result(ev, expect_failure, go_neg_res)
532
533     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):
534         if not self.discover_peer(peer):
535             raise Exception("Peer " + peer + " not found")
536         self.dump_monitor()
537         if pin:
538             cmd = "P2P_CONNECT " + peer + " " + pin + " " + method
539         else:
540             cmd = "P2P_CONNECT " + peer + " " + method
541         if go_intent:
542             cmd = cmd + ' go_intent=' + str(go_intent)
543         if freq:
544             cmd = cmd + ' freq=' + str(freq)
545         if persistent:
546             cmd = cmd + " persistent"
547         elif persistent_id:
548             cmd = cmd + " persistent=" + persistent_id
549         if provdisc:
550             cmd = cmd + " provdisc"
551         if "OK" in self.global_request(cmd):
552             if timeout == 0:
553                 self.dump_monitor()
554                 return None
555             go_neg_res = None
556             ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
557                                          "P2P-GO-NEG-FAILURE"], timeout)
558             if ev is None:
559                 if expect_failure:
560                     return None
561                 raise Exception("Group formation timed out")
562             if "P2P-GO-NEG-SUCCESS" in ev:
563                 if not wait_group:
564                     return ev
565                 go_neg_res = ev
566                 ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
567                 if ev is None:
568                     if expect_failure:
569                         return None
570                     raise Exception("Group formation timed out")
571             self.dump_monitor()
572             return self.group_form_result(ev, expect_failure, go_neg_res)
573         raise Exception("P2P_CONNECT failed")
574
575     def wait_event(self, events, timeout=10):
576         start = os.times()[4]
577         while True:
578             while self.mon.pending():
579                 ev = self.mon.recv()
580                 logger.debug(self.ifname + ": " + ev)
581                 for event in events:
582                     if event in ev:
583                         return ev
584             now = os.times()[4]
585             remaining = start + timeout - now
586             if remaining <= 0:
587                 break
588             if not self.mon.pending(timeout=remaining):
589                 break
590         return None
591
592     def wait_global_event(self, events, timeout):
593         if self.global_iface is None:
594             self.wait_event(events, timeout)
595         else:
596             start = os.times()[4]
597             while True:
598                 while self.global_mon.pending():
599                     ev = self.global_mon.recv()
600                     logger.debug(self.ifname + "(global): " + ev)
601                     for event in events:
602                         if event in ev:
603                             return ev
604                 now = os.times()[4]
605                 remaining = start + timeout - now
606                 if remaining <= 0:
607                     break
608                 if not self.global_mon.pending(timeout=remaining):
609                     break
610         return None
611
612     def wait_group_event(self, events, timeout=10):
613         if self.group_ifname and self.group_ifname != self.ifname:
614             if self.gctrl_mon is None:
615                 return None
616             start = os.times()[4]
617             while True:
618                 while self.gctrl_mon.pending():
619                     ev = self.gctrl_mon.recv()
620                     logger.debug(self.group_ifname + ": " + ev)
621                     for event in events:
622                         if event in ev:
623                             return ev
624                 now = os.times()[4]
625                 remaining = start + timeout - now
626                 if remaining <= 0:
627                     break
628                 if not self.gctrl_mon.pending(timeout=remaining):
629                     break
630             return None
631
632         return self.wait_event(events, timeout)
633
634     def wait_go_ending_session(self):
635         if self.gctrl_mon:
636             try:
637                 self.gctrl_mon.detach()
638             except:
639                 pass
640             self.gctrl_mon = None
641         ev = self.wait_event(["P2P-GROUP-REMOVED"], timeout=3)
642         if ev is None:
643             raise Exception("Group removal event timed out")
644         if "reason=GO_ENDING_SESSION" not in ev:
645             raise Exception("Unexpected group removal reason")
646
647     def dump_monitor(self):
648         while self.mon.pending():
649             ev = self.mon.recv()
650             logger.debug(self.ifname + ": " + ev)
651         while self.global_mon and self.global_mon.pending():
652             ev = self.global_mon.recv()
653             logger.debug(self.ifname + "(global): " + ev)
654
655     def remove_group(self, ifname=None):
656         if self.gctrl_mon:
657             try:
658                 self.gctrl_mon.detach()
659             except:
660                 pass
661             self.gctrl_mon = None
662         if ifname is None:
663             ifname = self.group_ifname if self.group_ifname else self.ifname
664         if "OK" not in self.global_request("P2P_GROUP_REMOVE " + ifname):
665             raise Exception("Group could not be removed")
666         self.group_ifname = None
667
668     def p2p_start_go(self, persistent=None, freq=None, no_event_clear=False):
669         self.dump_monitor()
670         cmd = "P2P_GROUP_ADD"
671         if persistent is None:
672             pass
673         elif persistent is True:
674             cmd = cmd + " persistent"
675         else:
676             cmd = cmd + " persistent=" + str(persistent)
677         if freq:
678             cmd = cmd + " freq=" + str(freq)
679         if "OK" in self.global_request(cmd):
680             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout=5)
681             if ev is None:
682                 raise Exception("GO start up timed out")
683             if not no_event_clear:
684                 self.dump_monitor()
685             return self.group_form_result(ev)
686         raise Exception("P2P_GROUP_ADD failed")
687
688     def p2p_go_authorize_client(self, pin):
689         cmd = "WPS_PIN any " + pin
690         if "FAIL" in self.group_request(cmd):
691             raise Exception("Failed to authorize client connection on GO")
692         return None
693
694     def p2p_go_authorize_client_pbc(self):
695         cmd = "WPS_PBC"
696         if "FAIL" in self.group_request(cmd):
697             raise Exception("Failed to authorize client connection on GO")
698         return None
699
700     def p2p_connect_group(self, go_addr, pin, timeout=0, social=False,
701                           freq=None):
702         self.dump_monitor()
703         if not self.discover_peer(go_addr, social=social):
704             if social or not self.discover_peer(go_addr, social=social):
705                 raise Exception("GO " + go_addr + " not found")
706         self.dump_monitor()
707         cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
708         if freq:
709             cmd += " freq=" + str(freq)
710         if "OK" in self.global_request(cmd):
711             if timeout == 0:
712                 self.dump_monitor()
713                 return None
714             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
715             if ev is None:
716                 raise Exception("Joining the group timed out")
717             self.dump_monitor()
718             return self.group_form_result(ev)
719         raise Exception("P2P_CONNECT(join) failed")
720
721     def tdls_setup(self, peer):
722         cmd = "TDLS_SETUP " + peer
723         if "FAIL" in self.group_request(cmd):
724             raise Exception("Failed to request TDLS setup")
725         return None
726
727     def tdls_teardown(self, peer):
728         cmd = "TDLS_TEARDOWN " + peer
729         if "FAIL" in self.group_request(cmd):
730             raise Exception("Failed to request TDLS teardown")
731         return None
732
733     def tspecs(self):
734         """Return (tsid, up) tuples representing current tspecs"""
735         res = self.request("WMM_AC_STATUS")
736         tspecs = re.findall(r"TSID=(\d+) UP=(\d+)", res)
737         tspecs = [tuple(map(int, tspec)) for tspec in tspecs]
738
739         logger.debug("tspecs: " + str(tspecs))
740         return tspecs
741
742     def add_ts(self, tsid, up, direction="downlink", expect_failure=False,
743                extra=None):
744         params = {
745             "sba": 9000,
746             "nominal_msdu_size": 1500,
747             "min_phy_rate": 6000000,
748             "mean_data_rate": 1500,
749         }
750         cmd = "WMM_AC_ADDTS %s tsid=%d up=%d" % (direction, tsid, up)
751         for (key, value) in params.iteritems():
752             cmd += " %s=%d" % (key, value)
753         if extra:
754             cmd += " " + extra
755
756         if self.request(cmd).strip() != "OK":
757             raise Exception("ADDTS failed (tsid=%d up=%d)" % (tsid, up))
758
759         if expect_failure:
760             ev = self.wait_event(["TSPEC-REQ-FAILED"], timeout=2)
761             if ev is None:
762                 raise Exception("ADDTS failed (time out while waiting failure)")
763             if "tsid=%d" % (tsid) not in ev:
764                 raise Exception("ADDTS failed (invalid tsid in TSPEC-REQ-FAILED")
765             return
766
767         ev = self.wait_event(["TSPEC-ADDED"], timeout=1)
768         if ev is None:
769             raise Exception("ADDTS failed (time out)")
770         if "tsid=%d" % (tsid) not in ev:
771             raise Exception("ADDTS failed (invalid tsid in TSPEC-ADDED)")
772
773         if not (tsid, up) in self.tspecs():
774             raise Exception("ADDTS failed (tsid not in tspec list)")
775
776     def del_ts(self, tsid):
777         if self.request("WMM_AC_DELTS %d" % (tsid)).strip() != "OK":
778             raise Exception("DELTS failed")
779
780         ev = self.wait_event(["TSPEC-REMOVED"], timeout=1)
781         if ev is None:
782             raise Exception("DELTS failed (time out)")
783         if "tsid=%d" % (tsid) not in ev:
784             raise Exception("DELTS failed (invalid tsid in TSPEC-REMOVED)")
785
786         tspecs = [(t, u) for (t, u) in self.tspecs() if t == tsid]
787         if tspecs:
788             raise Exception("DELTS failed (still in tspec list)")
789
790     def connect(self, ssid=None, ssid2=None, **kwargs):
791         logger.info("Connect STA " + self.ifname + " to AP")
792         id = self.add_network()
793         if ssid:
794             self.set_network_quoted(id, "ssid", ssid)
795         elif ssid2:
796             self.set_network(id, "ssid", ssid2)
797
798         quoted = [ "psk", "identity", "anonymous_identity", "password",
799                    "ca_cert", "client_cert", "private_key",
800                    "private_key_passwd", "ca_cert2", "client_cert2",
801                    "private_key2", "phase1", "phase2", "domain_suffix_match",
802                    "altsubject_match", "subject_match", "pac_file", "dh_file",
803                    "bgscan", "ht_mcs", "id_str", "openssl_ciphers",
804                    "domain_match" ]
805         for field in quoted:
806             if field in kwargs and kwargs[field]:
807                 self.set_network_quoted(id, field, kwargs[field])
808
809         not_quoted = [ "proto", "key_mgmt", "ieee80211w", "pairwise",
810                        "group", "wep_key0", "wep_key1", "wep_key2", "wep_key3",
811                        "wep_tx_keyidx", "scan_freq", "eap",
812                        "eapol_flags", "fragment_size", "scan_ssid", "auth_alg",
813                        "wpa_ptk_rekey", "disable_ht", "disable_vht", "bssid",
814                        "disable_max_amsdu", "ampdu_factor", "ampdu_density",
815                        "disable_ht40", "disable_sgi", "disable_ldpc",
816                        "ht40_intolerant", "update_identifier", "mac_addr",
817                        "erp", "bg_scan_period", "bssid_blacklist",
818                        "bssid_whitelist" ]
819         for field in not_quoted:
820             if field in kwargs and kwargs[field]:
821                 self.set_network(id, field, kwargs[field])
822
823         if "raw_psk" in kwargs and kwargs['raw_psk']:
824             self.set_network(id, "psk", kwargs['raw_psk'])
825         if "password_hex" in kwargs and kwargs['password_hex']:
826             self.set_network(id, "password", kwargs['password_hex'])
827         if "peerkey" in kwargs and kwargs['peerkey']:
828             self.set_network(id, "peerkey", "1")
829         if "okc" in kwargs and kwargs['okc']:
830             self.set_network(id, "proactive_key_caching", "1")
831         if "ocsp" in kwargs and kwargs['ocsp']:
832             self.set_network(id, "ocsp", str(kwargs['ocsp']))
833         if "only_add_network" in kwargs and kwargs['only_add_network']:
834             return id
835         if "wait_connect" not in kwargs or kwargs['wait_connect']:
836             if "eap" in kwargs:
837                 self.connect_network(id, timeout=20)
838             else:
839                 self.connect_network(id)
840         else:
841             self.dump_monitor()
842             self.select_network(id)
843         return id
844
845     def scan(self, type=None, freq=None, no_wait=False, only_new=False):
846         if type:
847             cmd = "SCAN TYPE=" + type
848         else:
849             cmd = "SCAN"
850         if freq:
851             cmd = cmd + " freq=" + str(freq)
852         if only_new:
853             cmd += " only_new=1"
854         if not no_wait:
855             self.dump_monitor()
856         if not "OK" in self.request(cmd):
857             raise Exception("Failed to trigger scan")
858         if no_wait:
859             return
860         ev = self.wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
861         if ev is None:
862             raise Exception("Scan timed out")
863
864     def scan_for_bss(self, bssid, freq=None, force_scan=False):
865         if not force_scan and self.get_bss(bssid) is not None:
866             return
867         for i in range(0, 10):
868             self.scan(freq=freq, type="ONLY")
869             if self.get_bss(bssid) is not None:
870                 return
871         raise Exception("Could not find BSS " + bssid + " in scan")
872
873     def flush_scan_cache(self, freq=2417):
874         self.request("BSS_FLUSH 0")
875         self.scan(freq=freq, only_new=True)
876
877     def roam(self, bssid, fail_test=False):
878         self.dump_monitor()
879         if "OK" not in self.request("ROAM " + bssid):
880             raise Exception("ROAM failed")
881         if fail_test:
882             ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
883             if ev is not None:
884                 raise Exception("Unexpected connection")
885             self.dump_monitor()
886             return
887         self.wait_connected(timeout=10, error="Roaming with the AP timed out")
888         self.dump_monitor()
889
890     def roam_over_ds(self, bssid, fail_test=False):
891         self.dump_monitor()
892         if "OK" not in self.request("FT_DS " + bssid):
893             raise Exception("FT_DS failed")
894         if fail_test:
895             ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
896             if ev is not None:
897                 raise Exception("Unexpected connection")
898             self.dump_monitor()
899             return
900         self.wait_connected(timeout=10, error="Roaming with the AP timed out")
901         self.dump_monitor()
902
903     def wps_reg(self, bssid, pin, new_ssid=None, key_mgmt=None, cipher=None,
904                 new_passphrase=None, no_wait=False):
905         self.dump_monitor()
906         if new_ssid:
907             self.request("WPS_REG " + bssid + " " + pin + " " +
908                          new_ssid.encode("hex") + " " + key_mgmt + " " +
909                          cipher + " " + new_passphrase.encode("hex"))
910             if no_wait:
911                 return
912             ev = self.wait_event(["WPS-SUCCESS"], timeout=15)
913         else:
914             self.request("WPS_REG " + bssid + " " + pin)
915             if no_wait:
916                 return
917             ev = self.wait_event(["WPS-CRED-RECEIVED"], timeout=15)
918             if ev is None:
919                 raise Exception("WPS cred timed out")
920             ev = self.wait_event(["WPS-FAIL"], timeout=15)
921         if ev is None:
922             raise Exception("WPS timed out")
923         self.wait_connected(timeout=15)
924
925     def relog(self):
926         self.global_request("RELOG")
927
928     def wait_completed(self, timeout=10):
929         for i in range(0, timeout * 2):
930             if self.get_status_field("wpa_state") == "COMPLETED":
931                 return
932             time.sleep(0.5)
933         raise Exception("Timeout while waiting for COMPLETED state")
934
935     def get_capability(self, field):
936         res = self.request("GET_CAPABILITY " + field)
937         if "FAIL" in res:
938             return None
939         return res.split(' ')
940
941     def get_bss(self, bssid):
942         res = self.request("BSS " + bssid)
943         if "FAIL" in res:
944             return None
945         lines = res.splitlines()
946         vals = dict()
947         for l in lines:
948             [name,value] = l.split('=', 1)
949             vals[name] = value
950         if len(vals) == 0:
951             return None
952         return vals
953
954     def get_pmksa(self, bssid):
955         res = self.request("PMKSA")
956         lines = res.splitlines()
957         for l in lines:
958             if bssid not in l:
959                 continue
960             vals = dict()
961             [index,aa,pmkid,expiration,opportunistic] = l.split(' ')
962             vals['index'] = index
963             vals['pmkid'] = pmkid
964             vals['expiration'] = expiration
965             vals['opportunistic'] = opportunistic
966             return vals
967         return None
968
969     def get_sta(self, addr, info=None, next=False):
970         cmd = "STA-NEXT " if next else "STA "
971         if addr is None:
972             res = self.request("STA-FIRST")
973         elif info:
974             res = self.request(cmd + addr + " " + info)
975         else:
976             res = self.request(cmd + addr)
977         lines = res.splitlines()
978         vals = dict()
979         first = True
980         for l in lines:
981             if first:
982                 vals['addr'] = l
983                 first = False
984             else:
985                 [name,value] = l.split('=', 1)
986                 vals[name] = value
987         return vals
988
989     def mgmt_rx(self, timeout=5):
990         ev = self.wait_event(["MGMT-RX"], timeout=timeout)
991         if ev is None:
992             return None
993         msg = {}
994         items = ev.split(' ')
995         field,val = items[1].split('=')
996         if field != "freq":
997             raise Exception("Unexpected MGMT-RX event format: " + ev)
998         msg['freq'] = val
999         frame = binascii.unhexlify(items[4])
1000         msg['frame'] = frame
1001
1002         hdr = struct.unpack('<HH6B6B6BH', frame[0:24])
1003         msg['fc'] = hdr[0]
1004         msg['subtype'] = (hdr[0] >> 4) & 0xf
1005         hdr = hdr[1:]
1006         msg['duration'] = hdr[0]
1007         hdr = hdr[1:]
1008         msg['da'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
1009         hdr = hdr[6:]
1010         msg['sa'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
1011         hdr = hdr[6:]
1012         msg['bssid'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
1013         hdr = hdr[6:]
1014         msg['seq_ctrl'] = hdr[0]
1015         msg['payload'] = frame[24:]
1016
1017         return msg
1018
1019     def wait_connected(self, timeout=10, error="Connection timed out"):
1020         ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
1021         if ev is None:
1022             raise Exception(error)
1023         return ev
1024
1025     def wait_disconnected(self, timeout=10, error="Disconnection timed out"):
1026         ev = self.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=timeout)
1027         if ev is None:
1028             raise Exception(error)
1029         return ev