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