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