]> git.ipfire.org Git - thirdparty/hostap.git/blobdiff - tests/hwsim/wpasupplicant.py
tests: P2P_FIND with freq parameter to scan a single channel
[thirdparty/hostap.git] / tests / hwsim / wpasupplicant.py
index a276fea5a3d00bdb6ac9a9f2a6c77abc0f157ed1..9565b9d4f8094f945f0d3b4edb1d2157919c4b74 100644 (file)
@@ -1,5 +1,3 @@
-#!/usr/bin/python
-#
 # Python class for controlling wpa_supplicant
 # Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
 #
@@ -9,7 +7,9 @@
 import os
 import time
 import logging
+import binascii
 import re
+import struct
 import subprocess
 import wpaspy
 
@@ -19,6 +19,7 @@ wpas_ctrl = '/var/run/wpa_supplicant'
 class WpaSupplicant:
     def __init__(self, ifname=None, global_iface=None):
         self.group_ifname = None
+        self.gctrl_mon = None
         if ifname:
             self.set_ifname(ifname)
         else:
@@ -29,6 +30,15 @@ class WpaSupplicant:
             self.global_ctrl = wpaspy.Ctrl(global_iface)
             self.global_mon = wpaspy.Ctrl(global_iface)
             self.global_mon.attach()
+        else:
+            self.global_mon = None
+
+    def close_ctrl(self):
+        if self.global_mon:
+            self.global_mon.detach()
+            self.global_mon = None
+            self.global_ctrl = None
+        self.remove_ifname()
 
     def set_ifname(self, ifname):
         self.ifname = ifname
@@ -43,15 +53,20 @@ class WpaSupplicant:
             self.ctrl = None
             self.ifname = None
 
-    def interface_add(self, ifname, driver="nl80211", drv_params=None):
+    def interface_add(self, ifname, config="", driver="nl80211",
+                      drv_params=None, br_ifname=None):
         try:
             groups = subprocess.check_output(["id"])
             group = "admin" if "(admin)" in groups else "adm"
         except Exception, e:
             group = "admin"
-        cmd = "INTERFACE_ADD " + ifname + "\t\t" + driver + "\tDIR=/var/run/wpa_supplicant GROUP=" + group
+        cmd = "INTERFACE_ADD " + ifname + "\t" + config + "\t" + driver + "\tDIR=/var/run/wpa_supplicant GROUP=" + group
         if drv_params:
             cmd = cmd + '\t' + drv_params
+        if br_ifname:
+            if not drv_params:
+                cmd += '\t'
+            cmd += '\t' + br_ifname
         if "FAIL" in self.global_request(cmd):
             raise Exception("Failed to add a dynamic wpa_supplicant interface")
         self.set_ifname(ifname)
@@ -60,16 +75,16 @@ class WpaSupplicant:
         self.remove_ifname()
         self.global_request("INTERFACE_REMOVE " + ifname)
 
-    def request(self, cmd):
+    def request(self, cmd, timeout=10):
         logger.debug(self.ifname + ": CTRL: " + cmd)
-        return self.ctrl.request(cmd)
+        return self.ctrl.request(cmd, timeout=timeout)
 
     def global_request(self, cmd):
         if self.global_iface is None:
             self.request(cmd)
         else:
             ifname = self.ifname or self.global_iface
-            logger.debug(ifname + ": CTRL: " + cmd)
+            logger.debug(ifname + ": CTRL(global): " + cmd)
             return self.global_ctrl.request(cmd)
 
     def group_request(self, cmd):
@@ -82,18 +97,26 @@ class WpaSupplicant:
     def ping(self):
         return "PONG" in self.request("PING")
 
+    def global_ping(self):
+        return "PONG" in self.global_request("PING")
+
     def reset(self):
+        self.dump_monitor()
         res = self.request("FLUSH")
         if not "OK" in res:
             logger.info("FLUSH to " + self.ifname + " failed: " + res)
-        self.request("SET external_sim 0")
-        self.request("SET hessid 00:00:00:00:00:00")
-        self.request("SET access_network_type 15")
         self.request("SET p2p_add_cli_chan 0")
         self.request("SET p2p_no_go_freq ")
         self.request("SET p2p_pref_chan ")
         self.request("SET p2p_no_group_iface 1")
         self.request("SET p2p_go_intent 7")
+        self.request("SET ignore_old_scan_res 0")
+        if self.gctrl_mon:
+            try:
+                self.gctrl_mon.detach()
+            except:
+                pass
+            self.gctrl_mon = None
         self.group_ifname = None
         self.dump_monitor()
 
@@ -110,13 +133,13 @@ class WpaSupplicant:
             logger.error(self.ifname + ": Driver scan state did not clear")
             print "Trying to clear cfg80211/mac80211 scan state"
             try:
-                cmd = ["sudo", "ifconfig", self.ifname, "down"]
+                cmd = ["ifconfig", self.ifname, "down"]
                 subprocess.call(cmd)
             except subprocess.CalledProcessError, e:
                 logger.info("ifconfig failed: " + str(e.returncode))
                 logger.info(e.output)
             try:
-                cmd = ["sudo", "ifconfig", self.ifname, "up"]
+                cmd = ["ifconfig", self.ifname, "up"]
                 subprocess.call(cmd)
             except subprocess.CalledProcessError, e:
                 logger.info("ifconfig failed: " + str(e.returncode))
@@ -176,9 +199,19 @@ class WpaSupplicant:
             networks.append(network)
         return networks
 
-    def hs20_enable(self):
+    def hs20_enable(self, auto_interworking=False):
         self.request("SET interworking 1")
         self.request("SET hs20 1")
+        if auto_interworking:
+            self.request("SET auto_interworking 1")
+        else:
+            self.request("SET auto_interworking 0")
+
+    def interworking_add_network(self, bssid):
+        id = self.request("INTERWORKING_ADD_NETWORK " + bssid)
+        if "FAIL" in id or "OK" in id:
+            raise Exception("INTERWORKING_ADD_NETWORK failed")
+        return int(id)
 
     def add_cred(self):
         id = self.request("ADD_CRED")
@@ -204,40 +237,65 @@ class WpaSupplicant:
             raise Exception("SET_CRED failed")
         return None
 
+    def get_cred(self, id, field):
+        return self.request("GET_CRED " + str(id) + " " + field)
+
     def add_cred_values(self, params):
         id = self.add_cred()
 
         quoted = [ "realm", "username", "password", "domain", "imsi",
                    "excluded_ssid", "milenage", "ca_cert", "client_cert",
-                   "private_key" ]
+                   "private_key", "domain_suffix_match", "provisioning_sp",
+                   "roaming_partner", "phase1", "phase2" ]
         for field in quoted:
             if field in params:
                 self.set_cred_quoted(id, field, params[field])
 
-        not_quoted = [ "eap", "roaming_consortium",
-                       "required_roaming_consortium" ]
+        not_quoted = [ "eap", "roaming_consortium", "priority",
+                       "required_roaming_consortium", "sp_priority",
+                       "max_bss_load", "update_identifier", "req_conn_capab",
+                       "min_dl_bandwidth_home", "min_ul_bandwidth_home",
+                       "min_dl_bandwidth_roaming", "min_ul_bandwidth_roaming" ]
         for field in not_quoted:
             if field in params:
                 self.set_cred(id, field, params[field])
 
         return id;
 
-    def select_network(self, id):
-        id = self.request("SELECT_NETWORK " + str(id))
+    def select_network(self, id, freq=None):
+        if freq:
+            extra = " freq=" + str(freq)
+        else:
+            extra = ""
+        id = self.request("SELECT_NETWORK " + str(id) + extra)
         if "FAIL" in id:
             raise Exception("SELECT_NETWORK failed")
         return None
 
+    def mesh_group_add(self, id):
+        id = self.request("MESH_GROUP_ADD " + str(id))
+        if "FAIL" in id:
+            raise Exception("MESH_GROUP_ADD failed")
+        return None
+
+    def mesh_group_remove(self):
+        id = self.request("MESH_GROUP_REMOVE " + str(self.ifname))
+        if "FAIL" in id:
+            raise Exception("MESH_GROUP_REMOVE failed")
+        return None
+
     def connect_network(self, id, timeout=10):
         self.dump_monitor()
         self.select_network(id)
-        ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
-        if ev is None:
-            raise Exception("Association with the AP timed out")
+        self.wait_connected(timeout=timeout)
         self.dump_monitor()
 
-    def get_status(self):
-        res = self.request("STATUS")
+    def get_status(self, extra=None):
+        if extra:
+            extra = "-" + extra
+        else:
+            extra = ""
+        res = self.request("STATUS" + extra)
         lines = res.splitlines()
         vals = dict()
         for l in lines:
@@ -248,23 +306,31 @@ class WpaSupplicant:
                 logger.info(self.ifname + ": Ignore unexpected STATUS line: " + l)
         return vals
 
-    def get_status_field(self, field):
-        vals = self.get_status()
+    def get_status_field(self, field, extra=None):
+        vals = self.get_status(extra)
         if field in vals:
             return vals[field]
         return None
 
-    def get_group_status(self):
-        res = self.group_request("STATUS")
+    def get_group_status(self, extra=None):
+        if extra:
+            extra = "-" + extra
+        else:
+            extra = ""
+        res = self.group_request("STATUS" + extra)
         lines = res.splitlines()
         vals = dict()
         for l in lines:
-            [name,value] = l.split('=', 1)
+            try:
+                [name,value] = l.split('=', 1)
+            except ValueError:
+                logger.info(self.ifname + ": Ignore unexpected status line: " + l)
+                continue
             vals[name] = value
         return vals
 
-    def get_group_status_field(self, field):
-        vals = self.get_group_status()
+    def get_group_status_field(self, field, extra=None):
+        vals = self.get_group_status(extra)
         if field in vals:
             return vals[field]
         return None
@@ -274,7 +340,11 @@ class WpaSupplicant:
         lines = res.splitlines()
         vals = dict()
         for l in lines:
-            [name,value] = l.split('=', 1)
+            try:
+                [name,value] = l.split('=', 1)
+            except ValueError:
+                logger.info(self.ifname + ": Ignore unexpected status-driver line: " + l)
+                continue
             vals[name] = value
         return vals
 
@@ -284,31 +354,62 @@ class WpaSupplicant:
             return vals[field]
         return None
 
+    def get_mcc(self):
+       mcc = int(self.get_driver_status_field('capa.num_multichan_concurrent'))
+       return 1 if mcc < 2 else mcc
+
+    def get_mib(self):
+        res = self.request("MIB")
+        lines = res.splitlines()
+        vals = dict()
+        for l in lines:
+            try:
+                [name,value] = l.split('=', 1)
+                vals[name] = value
+            except ValueError, e:
+                logger.info(self.ifname + ": Ignore unexpected MIB line: " + l)
+        return vals
+
     def p2p_dev_addr(self):
         return self.get_status_field("p2p_device_address")
 
     def p2p_interface_addr(self):
         return self.get_group_status_field("address")
 
+    def own_addr(self):
+        try:
+            res = self.p2p_interface_addr()
+        except:
+            res = self.p2p_dev_addr()
+        return res
+
     def p2p_listen(self):
         return self.global_request("P2P_LISTEN")
 
-    def p2p_find(self, social=False, dev_id=None, dev_type=None):
+    def p2p_find(self, social=False, progressive=False, dev_id=None,
+                 dev_type=None, delay=None, freq=None):
         cmd = "P2P_FIND"
         if social:
             cmd = cmd + " type=social"
+        elif progressive:
+            cmd = cmd + " type=progressive"
         if dev_id:
             cmd = cmd + " dev_id=" + dev_id
         if dev_type:
             cmd = cmd + " dev_type=" + dev_type
+        if delay:
+            cmd = cmd + " delay=" + str(delay)
+        if freq:
+            cmd = cmd + " freq=" + str(freq)
         return self.global_request(cmd)
 
     def p2p_stop_find(self):
         return self.global_request("P2P_STOP_FIND")
 
     def wps_read_pin(self):
-        #TODO: make this random
-        self.pin = "12345670"
+        self.pin = self.request("WPS_PIN get").rstrip("\n")
+        if "FAIL" in self.pin:
+            raise Exception("Could not generate PIN")
         return self.pin
 
     def peer_known(self, peer, full=True):
@@ -325,8 +426,8 @@ class WpaSupplicant:
             return True
         self.p2p_find(social)
         count = 0
-        while count < timeout:
-            time.sleep(1)
+        while count < timeout * 4:
+            time.sleep(0.25)
             count = count + 1
             if self.peer_known(peer, full):
                 return True
@@ -371,6 +472,12 @@ class WpaSupplicant:
         res['result'] = 'success'
         res['ifname'] = s[2]
         self.group_ifname = s[2]
+        try:
+            self.gctrl_mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, self.group_ifname))
+            self.gctrl_mon.attach()
+        except:
+            logger.debug("Could not open monitor socket for group interface")
+            self.gctrl_mon = None
         res['role'] = s[3]
         res['ssid'] = s[4]
         res['freq'] = s[5]
@@ -407,7 +514,10 @@ class WpaSupplicant:
         if not self.discover_peer(peer):
             raise Exception("Peer " + peer + " not found")
         self.dump_monitor()
-        cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
+        if pin:
+            cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
+        else:
+            cmd = "P2P_CONNECT " + peer + " " + method + " auth"
         if go_intent:
             cmd = cmd + ' go_intent=' + str(go_intent)
         if freq:
@@ -436,7 +546,7 @@ class WpaSupplicant:
         self.dump_monitor()
         return self.group_form_result(ev, expect_failure, go_neg_res)
 
-    def p2p_go_neg_init(self, peer, pin, method, timeout=0, go_intent=None, expect_failure=False, persistent=False, freq=None):
+    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):
         if not self.discover_peer(peer):
             raise Exception("Peer " + peer + " not found")
         self.dump_monitor()
@@ -450,6 +560,10 @@ class WpaSupplicant:
             cmd = cmd + ' freq=' + str(freq)
         if persistent:
             cmd = cmd + " persistent"
+        elif persistent_id:
+            cmd = cmd + " persistent=" + persistent_id
+        if provdisc:
+            cmd = cmd + " provdisc"
         if "OK" in self.global_request(cmd):
             if timeout == 0:
                 self.dump_monitor()
@@ -462,6 +576,8 @@ class WpaSupplicant:
                     return None
                 raise Exception("Group formation timed out")
             if "P2P-GO-NEG-SUCCESS" in ev:
+                if not wait_group:
+                    return ev
                 go_neg_res = ev
                 ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
                 if ev is None:
@@ -509,8 +625,36 @@ class WpaSupplicant:
                     break
         return None
 
+    def wait_group_event(self, events, timeout=10):
+        if self.group_ifname and self.group_ifname != self.ifname:
+            if self.gctrl_mon is None:
+                return None
+            start = os.times()[4]
+            while True:
+                while self.gctrl_mon.pending():
+                    ev = self.gctrl_mon.recv()
+                    logger.debug(self.group_ifname + ": " + ev)
+                    for event in events:
+                        if event in ev:
+                            return ev
+                now = os.times()[4]
+                remaining = start + timeout - now
+                if remaining <= 0:
+                    break
+                if not self.gctrl_mon.pending(timeout=remaining):
+                    break
+            return None
+
+        return self.wait_event(events, timeout)
+
     def wait_go_ending_session(self):
-        ev = self.wait_event(["P2P-GROUP-REMOVED"], timeout=3)
+        if self.gctrl_mon:
+            try:
+                self.gctrl_mon.detach()
+            except:
+                pass
+            self.gctrl_mon = None
+        ev = self.wait_global_event(["P2P-GROUP-REMOVED"], timeout=3)
         if ev is None:
             raise Exception("Group removal event timed out")
         if "reason=GO_ENDING_SESSION" not in ev:
@@ -520,18 +664,24 @@ class WpaSupplicant:
         while self.mon.pending():
             ev = self.mon.recv()
             logger.debug(self.ifname + ": " + ev)
-        while self.global_mon.pending():
+        while self.global_mon and self.global_mon.pending():
             ev = self.global_mon.recv()
             logger.debug(self.ifname + "(global): " + ev)
 
     def remove_group(self, ifname=None):
+        if self.gctrl_mon:
+            try:
+                self.gctrl_mon.detach()
+            except:
+                pass
+            self.gctrl_mon = None
         if ifname is None:
             ifname = self.group_ifname if self.group_ifname else self.ifname
         if "OK" not in self.global_request("P2P_GROUP_REMOVE " + ifname):
             raise Exception("Group could not be removed")
         self.group_ifname = None
 
-    def p2p_start_go(self, persistent=None, freq=None):
+    def p2p_start_go(self, persistent=None, freq=None, no_event_clear=False):
         self.dump_monitor()
         cmd = "P2P_GROUP_ADD"
         if persistent is None:
@@ -546,7 +696,8 @@ class WpaSupplicant:
             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout=5)
             if ev is None:
                 raise Exception("GO start up timed out")
-            self.dump_monitor()
+            if not no_event_clear:
+                self.dump_monitor()
             return self.group_form_result(ev)
         raise Exception("P2P_GROUP_ADD failed")
 
@@ -562,12 +713,16 @@ class WpaSupplicant:
             raise Exception("Failed to authorize client connection on GO")
         return None
 
-    def p2p_connect_group(self, go_addr, pin, timeout=0, social=False):
+    def p2p_connect_group(self, go_addr, pin, timeout=0, social=False,
+                          freq=None):
         self.dump_monitor()
         if not self.discover_peer(go_addr, social=social):
-            raise Exception("GO " + go_addr + " not found")
+            if social or not self.discover_peer(go_addr, social=social):
+                raise Exception("GO " + go_addr + " not found")
         self.dump_monitor()
         cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
+        if freq:
+            cmd += " freq=" + str(freq)
         if "OK" in self.global_request(cmd):
             if timeout == 0:
                 self.dump_monitor()
@@ -591,102 +746,110 @@ class WpaSupplicant:
             raise Exception("Failed to request TDLS teardown")
         return None
 
-    def connect(self, ssid=None, ssid2=None, psk=None, proto=None,
-                key_mgmt=None, wep_key0=None,
-                ieee80211w=None, pairwise=None, group=None, scan_freq=None,
-                eap=None, identity=None, anonymous_identity=None,
-                password=None, phase1=None, phase2=None, ca_cert=None,
-                domain_suffix_match=None, password_hex=None,
-                client_cert=None, private_key=None, peerkey=False, okc=False,
-                eapol_flags=None, fragment_size=None,
-                wait_connect=True, only_add_network=False,
-                ca_cert2=None, client_cert2=None, private_key2=None,
-                scan_ssid=None, raw_psk=None, pac_file=None,
-                subject_match=None, altsubject_match=None,
-                private_key_passwd=None, ocsp=None, auth_alg=None):
+    def tspecs(self):
+        """Return (tsid, up) tuples representing current tspecs"""
+        res = self.request("WMM_AC_STATUS")
+        tspecs = re.findall(r"TSID=(\d+) UP=(\d+)", res)
+        tspecs = [tuple(map(int, tspec)) for tspec in tspecs]
+
+        logger.debug("tspecs: " + str(tspecs))
+        return tspecs
+
+    def add_ts(self, tsid, up, direction="downlink", expect_failure=False,
+               extra=None):
+        params = {
+            "sba": 9000,
+            "nominal_msdu_size": 1500,
+            "min_phy_rate": 6000000,
+            "mean_data_rate": 1500,
+        }
+        cmd = "WMM_AC_ADDTS %s tsid=%d up=%d" % (direction, tsid, up)
+        for (key, value) in params.iteritems():
+            cmd += " %s=%d" % (key, value)
+        if extra:
+            cmd += " " + extra
+
+        if self.request(cmd).strip() != "OK":
+            raise Exception("ADDTS failed (tsid=%d up=%d)" % (tsid, up))
+
+        if expect_failure:
+            ev = self.wait_event(["TSPEC-REQ-FAILED"], timeout=2)
+            if ev is None:
+                raise Exception("ADDTS failed (time out while waiting failure)")
+            if "tsid=%d" % (tsid) not in ev:
+                raise Exception("ADDTS failed (invalid tsid in TSPEC-REQ-FAILED")
+            return
+
+        ev = self.wait_event(["TSPEC-ADDED"], timeout=1)
+        if ev is None:
+            raise Exception("ADDTS failed (time out)")
+        if "tsid=%d" % (tsid) not in ev:
+            raise Exception("ADDTS failed (invalid tsid in TSPEC-ADDED)")
+
+        if not (tsid, up) in self.tspecs():
+            raise Exception("ADDTS failed (tsid not in tspec list)")
+
+    def del_ts(self, tsid):
+        if self.request("WMM_AC_DELTS %d" % (tsid)).strip() != "OK":
+            raise Exception("DELTS failed")
+
+        ev = self.wait_event(["TSPEC-REMOVED"], timeout=1)
+        if ev is None:
+            raise Exception("DELTS failed (time out)")
+        if "tsid=%d" % (tsid) not in ev:
+            raise Exception("DELTS failed (invalid tsid in TSPEC-REMOVED)")
+
+        tspecs = [(t, u) for (t, u) in self.tspecs() if t == tsid]
+        if tspecs:
+            raise Exception("DELTS failed (still in tspec list)")
+
+    def connect(self, ssid=None, ssid2=None, **kwargs):
         logger.info("Connect STA " + self.ifname + " to AP")
         id = self.add_network()
         if ssid:
             self.set_network_quoted(id, "ssid", ssid)
         elif ssid2:
             self.set_network(id, "ssid", ssid2)
-        if psk:
-            self.set_network_quoted(id, "psk", psk)
-        if raw_psk:
-            self.set_network(id, "psk", raw_psk)
-        if proto:
-            self.set_network(id, "proto", proto)
-        if key_mgmt:
-            self.set_network(id, "key_mgmt", key_mgmt)
-        if ieee80211w:
-            self.set_network(id, "ieee80211w", ieee80211w)
-        if pairwise:
-            self.set_network(id, "pairwise", pairwise)
-        if group:
-            self.set_network(id, "group", group)
-        if wep_key0:
-            self.set_network(id, "wep_key0", wep_key0)
-        if scan_freq:
-            self.set_network(id, "scan_freq", scan_freq)
-        if eap:
-            self.set_network(id, "eap", eap)
-        if identity:
-            self.set_network_quoted(id, "identity", identity)
-        if anonymous_identity:
-            self.set_network_quoted(id, "anonymous_identity",
-                                    anonymous_identity)
-        if password:
-            self.set_network_quoted(id, "password", password)
-        if password_hex:
-            self.set_network(id, "password", password_hex)
-        if ca_cert:
-            self.set_network_quoted(id, "ca_cert", ca_cert)
-        if client_cert:
-            self.set_network_quoted(id, "client_cert", client_cert)
-        if private_key:
-            self.set_network_quoted(id, "private_key", private_key)
-        if private_key_passwd:
-            self.set_network_quoted(id, "private_key_passwd",
-                                    private_key_passwd)
-        if ca_cert2:
-            self.set_network_quoted(id, "ca_cert2", ca_cert2)
-        if client_cert2:
-            self.set_network_quoted(id, "client_cert2", client_cert2)
-        if private_key2:
-            self.set_network_quoted(id, "private_key2", private_key2)
-        if phase1:
-            self.set_network_quoted(id, "phase1", phase1)
-        if phase2:
-            self.set_network_quoted(id, "phase2", phase2)
-        if domain_suffix_match:
-            self.set_network_quoted(id, "domain_suffix_match",
-                                    domain_suffix_match)
-        if altsubject_match:
-            self.set_network_quoted(id, "altsubject_match",
-                                    altsubject_match)
-        if subject_match:
-            self.set_network_quoted(id, "subject_match",
-                                    subject_match)
-        if peerkey:
+
+        quoted = [ "psk", "identity", "anonymous_identity", "password",
+                   "ca_cert", "client_cert", "private_key",
+                   "private_key_passwd", "ca_cert2", "client_cert2",
+                   "private_key2", "phase1", "phase2", "domain_suffix_match",
+                   "altsubject_match", "subject_match", "pac_file", "dh_file",
+                   "bgscan", "ht_mcs", "id_str", "openssl_ciphers",
+                   "domain_match" ]
+        for field in quoted:
+            if field in kwargs and kwargs[field]:
+                self.set_network_quoted(id, field, kwargs[field])
+
+        not_quoted = [ "proto", "key_mgmt", "ieee80211w", "pairwise",
+                       "group", "wep_key0", "wep_key1", "wep_key2", "wep_key3",
+                       "wep_tx_keyidx", "scan_freq", "eap",
+                       "eapol_flags", "fragment_size", "scan_ssid", "auth_alg",
+                       "wpa_ptk_rekey", "disable_ht", "disable_vht", "bssid",
+                       "disable_max_amsdu", "ampdu_factor", "ampdu_density",
+                       "disable_ht40", "disable_sgi", "disable_ldpc",
+                       "ht40_intolerant", "update_identifier", "mac_addr",
+                       "erp", "bg_scan_period", "bssid_blacklist",
+                       "bssid_whitelist" ]
+        for field in not_quoted:
+            if field in kwargs and kwargs[field]:
+                self.set_network(id, field, kwargs[field])
+
+        if "raw_psk" in kwargs and kwargs['raw_psk']:
+            self.set_network(id, "psk", kwargs['raw_psk'])
+        if "password_hex" in kwargs and kwargs['password_hex']:
+            self.set_network(id, "password", kwargs['password_hex'])
+        if "peerkey" in kwargs and kwargs['peerkey']:
             self.set_network(id, "peerkey", "1")
-        if okc:
+        if "okc" in kwargs and kwargs['okc']:
             self.set_network(id, "proactive_key_caching", "1")
-        if eapol_flags:
-            self.set_network(id, "eapol_flags", eapol_flags)
-        if fragment_size:
-            self.set_network(id, "fragment_size", fragment_size)
-        if scan_ssid:
-            self.set_network(id, "scan_ssid", scan_ssid)
-        if pac_file:
-            self.set_network_quoted(id, "pac_file", pac_file)
-        if ocsp:
-            self.set_network(id, "ocsp", str(ocsp))
-        if auth_alg:
-            self.set_network(id, "auth_alg", auth_alg)
-        if only_add_network:
+        if "ocsp" in kwargs and kwargs['ocsp']:
+            self.set_network(id, "ocsp", str(kwargs['ocsp']))
+        if "only_add_network" in kwargs and kwargs['only_add_network']:
             return id
-        if wait_connect:
-            if eap:
+        if "wait_connect" not in kwargs or kwargs['wait_connect']:
+            if "eap" in kwargs:
                 self.connect_network(id, timeout=20)
             else:
                 self.connect_network(id)
@@ -695,13 +858,15 @@ class WpaSupplicant:
             self.select_network(id)
         return id
 
-    def scan(self, type=None, freq=None, no_wait=False):
+    def scan(self, type=None, freq=None, no_wait=False, only_new=False):
         if type:
             cmd = "SCAN TYPE=" + type
         else:
             cmd = "SCAN"
         if freq:
-            cmd = cmd + " freq=" + freq
+            cmd = cmd + " freq=" + str(freq)
+        if only_new:
+            cmd += " only_new=1"
         if not no_wait:
             self.dump_monitor()
         if not "OK" in self.request(cmd):
@@ -712,20 +877,43 @@ class WpaSupplicant:
         if ev is None:
             raise Exception("Scan timed out")
 
-    def roam(self, bssid):
+    def scan_for_bss(self, bssid, freq=None, force_scan=False, only_new=False):
+        if not force_scan and self.get_bss(bssid) is not None:
+            return
+        for i in range(0, 10):
+            self.scan(freq=freq, type="ONLY", only_new=only_new)
+            if self.get_bss(bssid) is not None:
+                return
+        raise Exception("Could not find BSS " + bssid + " in scan")
+
+    def flush_scan_cache(self, freq=2417):
+        self.request("BSS_FLUSH 0")
+        self.scan(freq=freq, only_new=True)
+
+    def roam(self, bssid, fail_test=False):
         self.dump_monitor()
-        self.request("ROAM " + bssid)
-        ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
-        if ev is None:
-            raise Exception("Roaming with the AP timed out")
+        if "OK" not in self.request("ROAM " + bssid):
+            raise Exception("ROAM failed")
+        if fail_test:
+            ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
+            if ev is not None:
+                raise Exception("Unexpected connection")
+            self.dump_monitor()
+            return
+        self.wait_connected(timeout=10, error="Roaming with the AP timed out")
         self.dump_monitor()
 
-    def roam_over_ds(self, bssid):
+    def roam_over_ds(self, bssid, fail_test=False):
         self.dump_monitor()
-        self.request("FT_DS " + bssid)
-        ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
-        if ev is None:
-            raise Exception("Roaming with the AP timed out")
+        if "OK" not in self.request("FT_DS " + bssid):
+            raise Exception("FT_DS failed")
+        if fail_test:
+            ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
+            if ev is not None:
+                raise Exception("Unexpected connection")
+            self.dump_monitor()
+            return
+        self.wait_connected(timeout=10, error="Roaming with the AP timed out")
         self.dump_monitor()
 
     def wps_reg(self, bssid, pin, new_ssid=None, key_mgmt=None, cipher=None,
@@ -748,12 +936,10 @@ class WpaSupplicant:
             ev = self.wait_event(["WPS-FAIL"], timeout=15)
         if ev is None:
             raise Exception("WPS timed out")
-        ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
-        if ev is None:
-            raise Exception("Association with the AP timed out")
+        self.wait_connected(timeout=15)
 
     def relog(self):
-        self.request("RELOG")
+        self.global_request("RELOG")
 
     def wait_completed(self, timeout=10):
         for i in range(0, timeout * 2):
@@ -768,13 +954,23 @@ class WpaSupplicant:
             return None
         return res.split(' ')
 
-    def get_bss(self, bssid):
-        res = self.request("BSS " + bssid)
+    def get_bss(self, bssid, ifname=None):
+       if not ifname or ifname == self.ifname:
+            res = self.request("BSS " + bssid)
+        elif ifname == self.group_ifname:
+            res = self.group_request("BSS " + bssid)
+        else:
+            return None
+
+        if "FAIL" in res:
+            return None
         lines = res.splitlines()
         vals = dict()
         for l in lines:
             [name,value] = l.split('=', 1)
             vals[name] = value
+        if len(vals) == 0:
+            return None
         return vals
 
     def get_pmksa(self, bssid):
@@ -811,3 +1007,59 @@ class WpaSupplicant:
                 [name,value] = l.split('=', 1)
                 vals[name] = value
         return vals
+
+    def mgmt_rx(self, timeout=5):
+        ev = self.wait_event(["MGMT-RX"], timeout=timeout)
+        if ev is None:
+            return None
+        msg = {}
+        items = ev.split(' ')
+        field,val = items[1].split('=')
+        if field != "freq":
+            raise Exception("Unexpected MGMT-RX event format: " + ev)
+        msg['freq'] = val
+        frame = binascii.unhexlify(items[4])
+        msg['frame'] = frame
+
+        hdr = struct.unpack('<HH6B6B6BH', frame[0:24])
+        msg['fc'] = hdr[0]
+        msg['subtype'] = (hdr[0] >> 4) & 0xf
+        hdr = hdr[1:]
+        msg['duration'] = hdr[0]
+        hdr = hdr[1:]
+        msg['da'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
+        hdr = hdr[6:]
+        msg['sa'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
+        hdr = hdr[6:]
+        msg['bssid'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
+        hdr = hdr[6:]
+        msg['seq_ctrl'] = hdr[0]
+        msg['payload'] = frame[24:]
+
+        return msg
+
+    def wait_connected(self, timeout=10, error="Connection timed out"):
+        ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
+        if ev is None:
+            raise Exception(error)
+        return ev
+
+    def wait_disconnected(self, timeout=10, error="Disconnection timed out"):
+        ev = self.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=timeout)
+        if ev is None:
+            raise Exception(error)
+        return ev
+
+    def get_group_ifname(self):
+        return self.group_ifname if self.group_ifname else self.ifname
+
+    def get_config(self):
+        res = self.request("DUMP")
+        if res.startswith("FAIL"):
+            raise Exception("DUMP failed")
+        lines = res.splitlines()
+        vals = dict()
+        for l in lines:
+            [name,value] = l.split('=', 1)
+            vals[name] = value
+        return vals