2013-03-01 18:05:03 -05:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# Python class for controlling wpa_supplicant
|
2014-01-03 07:34:59 -05:00
|
|
|
# Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
|
2013-03-01 18:05:03 -05:00
|
|
|
#
|
|
|
|
# This software may be distributed under the terms of the BSD license.
|
|
|
|
# See README for more details.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import logging
|
2013-03-09 06:36:35 -05:00
|
|
|
import re
|
2013-09-28 10:31:54 -04:00
|
|
|
import subprocess
|
2013-03-01 18:05:03 -05:00
|
|
|
import wpaspy
|
|
|
|
|
2013-10-31 06:46:42 -04:00
|
|
|
logger = logging.getLogger()
|
2013-03-01 18:05:03 -05:00
|
|
|
wpas_ctrl = '/var/run/wpa_supplicant'
|
|
|
|
|
|
|
|
class WpaSupplicant:
|
2013-12-30 16:08:25 -05:00
|
|
|
def __init__(self, ifname=None, global_iface=None):
|
2013-03-09 10:34:08 -05:00
|
|
|
self.group_ifname = None
|
2013-12-30 16:08:25 -05:00
|
|
|
if ifname:
|
|
|
|
self.set_ifname(ifname)
|
|
|
|
else:
|
|
|
|
self.ifname = None
|
2013-03-01 18:05:03 -05:00
|
|
|
|
2013-06-30 17:13:11 -04:00
|
|
|
self.global_iface = global_iface
|
|
|
|
if global_iface:
|
|
|
|
self.global_ctrl = wpaspy.Ctrl(global_iface)
|
|
|
|
self.global_mon = wpaspy.Ctrl(global_iface)
|
|
|
|
self.global_mon.attach()
|
|
|
|
|
2013-12-30 16:08:25 -05:00
|
|
|
def set_ifname(self, ifname):
|
|
|
|
self.ifname = ifname
|
|
|
|
self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
|
|
|
|
self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
|
|
|
|
self.mon.attach()
|
|
|
|
|
|
|
|
def remove_ifname(self):
|
|
|
|
if self.ifname:
|
|
|
|
self.mon.detach()
|
|
|
|
self.mon = None
|
|
|
|
self.ctrl = None
|
|
|
|
self.ifname = None
|
|
|
|
|
2013-12-30 17:17:02 -05:00
|
|
|
def interface_add(self, ifname, driver="nl80211", drv_params=None):
|
2013-12-30 16:08:25 -05:00
|
|
|
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
|
2013-12-30 17:17:02 -05:00
|
|
|
if drv_params:
|
|
|
|
cmd = cmd + '\t' + drv_params
|
2013-12-30 16:08:25 -05:00
|
|
|
if "FAIL" in self.global_request(cmd):
|
|
|
|
raise Exception("Failed to add a dynamic wpa_supplicant interface")
|
|
|
|
self.set_ifname(ifname)
|
|
|
|
|
|
|
|
def interface_remove(self, ifname):
|
|
|
|
self.remove_ifname()
|
|
|
|
self.global_request("INTERFACE_REMOVE " + ifname)
|
|
|
|
|
2013-03-01 18:05:03 -05:00
|
|
|
def request(self, cmd):
|
|
|
|
logger.debug(self.ifname + ": CTRL: " + cmd)
|
|
|
|
return self.ctrl.request(cmd)
|
|
|
|
|
2013-06-30 17:13:11 -04:00
|
|
|
def global_request(self, cmd):
|
|
|
|
if self.global_iface is None:
|
|
|
|
self.request(cmd)
|
|
|
|
else:
|
2013-12-30 16:08:25 -05:00
|
|
|
ifname = self.ifname or self.global_iface
|
|
|
|
logger.debug(ifname + ": CTRL: " + cmd)
|
2013-06-30 17:13:11 -04:00
|
|
|
return self.global_ctrl.request(cmd)
|
|
|
|
|
2013-03-09 10:34:08 -05:00
|
|
|
def group_request(self, cmd):
|
|
|
|
if self.group_ifname and self.group_ifname != self.ifname:
|
|
|
|
logger.debug(self.group_ifname + ": CTRL: " + cmd)
|
|
|
|
gctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, self.group_ifname))
|
|
|
|
return gctrl.request(cmd)
|
|
|
|
return self.request(cmd)
|
|
|
|
|
2013-03-01 18:05:03 -05:00
|
|
|
def ping(self):
|
|
|
|
return "PONG" in self.request("PING")
|
|
|
|
|
|
|
|
def reset(self):
|
2013-09-03 05:13:25 -04:00
|
|
|
res = self.request("FLUSH")
|
|
|
|
if not "OK" in res:
|
|
|
|
logger.info("FLUSH to " + self.ifname + " failed: " + res)
|
2013-10-20 06:41:23 -04:00
|
|
|
self.request("SET external_sim 0")
|
2013-12-25 11:55:08 -05:00
|
|
|
self.request("SET hessid 00:00:00:00:00:00")
|
|
|
|
self.request("SET access_network_type 15")
|
2013-10-26 13:47:08 -04:00
|
|
|
self.request("SET p2p_add_cli_chan 0")
|
|
|
|
self.request("SET p2p_no_go_freq ")
|
|
|
|
self.request("SET p2p_pref_chan ")
|
2013-11-24 08:32:52 -05:00
|
|
|
self.request("SET p2p_no_group_iface 1")
|
2013-03-09 10:34:08 -05:00
|
|
|
self.group_ifname = None
|
2013-04-28 15:00:58 -04:00
|
|
|
self.dump_monitor()
|
2013-09-28 10:31:54 -04:00
|
|
|
|
|
|
|
iter = 0
|
|
|
|
while iter < 60:
|
|
|
|
state = self.get_driver_status_field("scan_state")
|
|
|
|
if "SCAN_STARTED" in state or "SCAN_REQUESTED" in state:
|
|
|
|
logger.info(self.ifname + ": Waiting for scan operation to complete before continuing")
|
|
|
|
time.sleep(1)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
iter = iter + 1
|
|
|
|
if iter == 60:
|
|
|
|
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"]
|
|
|
|
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"]
|
|
|
|
subprocess.call(cmd)
|
|
|
|
except subprocess.CalledProcessError, e:
|
|
|
|
logger.info("ifconfig failed: " + str(e.returncode))
|
|
|
|
logger.info(e.output)
|
2014-01-13 13:26:22 -05:00
|
|
|
if iter > 0:
|
|
|
|
# The ongoing scan could have discovered BSSes or P2P peers
|
|
|
|
logger.info("Run FLUSH again since scan was in progress")
|
|
|
|
self.request("FLUSH")
|
2014-01-14 16:37:15 -05:00
|
|
|
self.dump_monitor()
|
2013-09-28 10:31:54 -04:00
|
|
|
|
2013-09-03 05:13:25 -04:00
|
|
|
if not self.ping():
|
|
|
|
logger.info("No PING response from " + self.ifname + " after reset")
|
2013-03-01 18:05:03 -05:00
|
|
|
|
2013-03-09 12:01:56 -05:00
|
|
|
def add_network(self):
|
|
|
|
id = self.request("ADD_NETWORK")
|
|
|
|
if "FAIL" in id:
|
|
|
|
raise Exception("ADD_NETWORK failed")
|
|
|
|
return int(id)
|
|
|
|
|
|
|
|
def remove_network(self, id):
|
|
|
|
id = self.request("REMOVE_NETWORK " + str(id))
|
|
|
|
if "FAIL" in id:
|
|
|
|
raise Exception("REMOVE_NETWORK failed")
|
|
|
|
return None
|
|
|
|
|
|
|
|
def set_network(self, id, field, value):
|
|
|
|
res = self.request("SET_NETWORK " + str(id) + " " + field + " " + value)
|
|
|
|
if "FAIL" in res:
|
|
|
|
raise Exception("SET_NETWORK failed")
|
|
|
|
return None
|
|
|
|
|
|
|
|
def set_network_quoted(self, id, field, value):
|
|
|
|
res = self.request("SET_NETWORK " + str(id) + " " + field + ' "' + value + '"')
|
|
|
|
if "FAIL" in res:
|
|
|
|
raise Exception("SET_NETWORK failed")
|
|
|
|
return None
|
|
|
|
|
2013-11-06 16:35:19 -05:00
|
|
|
def list_networks(self):
|
|
|
|
res = self.request("LIST_NETWORKS")
|
|
|
|
lines = res.splitlines()
|
|
|
|
networks = []
|
|
|
|
for l in lines:
|
|
|
|
if "network id" in l:
|
|
|
|
continue
|
|
|
|
[id,ssid,bssid,flags] = l.split('\t')
|
|
|
|
network = {}
|
|
|
|
network['id'] = id
|
|
|
|
network['ssid'] = ssid
|
|
|
|
network['bssid'] = bssid
|
|
|
|
network['flags'] = flags
|
|
|
|
networks.append(network)
|
|
|
|
return networks
|
|
|
|
|
2013-10-29 08:20:29 -04:00
|
|
|
def hs20_enable(self):
|
|
|
|
self.request("SET interworking 1")
|
|
|
|
self.request("SET hs20 1")
|
|
|
|
|
2013-03-31 18:01:24 -04:00
|
|
|
def add_cred(self):
|
|
|
|
id = self.request("ADD_CRED")
|
|
|
|
if "FAIL" in id:
|
|
|
|
raise Exception("ADD_CRED failed")
|
|
|
|
return int(id)
|
|
|
|
|
|
|
|
def remove_cred(self, id):
|
|
|
|
id = self.request("REMOVE_CRED " + str(id))
|
|
|
|
if "FAIL" in id:
|
|
|
|
raise Exception("REMOVE_CRED failed")
|
|
|
|
return None
|
|
|
|
|
|
|
|
def set_cred(self, id, field, value):
|
|
|
|
res = self.request("SET_CRED " + str(id) + " " + field + " " + value)
|
|
|
|
if "FAIL" in res:
|
|
|
|
raise Exception("SET_CRED failed")
|
|
|
|
return None
|
|
|
|
|
|
|
|
def set_cred_quoted(self, id, field, value):
|
|
|
|
res = self.request("SET_CRED " + str(id) + " " + field + ' "' + value + '"')
|
|
|
|
if "FAIL" in res:
|
|
|
|
raise Exception("SET_CRED failed")
|
|
|
|
return None
|
|
|
|
|
2013-11-04 06:09:46 -05:00
|
|
|
def add_cred_values(self, params):
|
2013-10-29 08:20:29 -04:00
|
|
|
id = self.add_cred()
|
2013-11-04 06:09:46 -05:00
|
|
|
|
2013-11-04 06:22:55 -05:00
|
|
|
quoted = [ "realm", "username", "password", "domain", "imsi",
|
2013-12-26 07:08:42 -05:00
|
|
|
"excluded_ssid", "milenage", "ca_cert", "client_cert",
|
|
|
|
"private_key" ]
|
2013-11-04 06:09:46 -05:00
|
|
|
for field in quoted:
|
|
|
|
if field in params:
|
|
|
|
self.set_cred_quoted(id, field, params[field])
|
|
|
|
|
2013-12-26 06:18:46 -05:00
|
|
|
not_quoted = [ "eap", "roaming_consortium",
|
|
|
|
"required_roaming_consortium" ]
|
2013-11-04 06:09:46 -05:00
|
|
|
for field in not_quoted:
|
|
|
|
if field in params:
|
|
|
|
self.set_cred(id, field, params[field])
|
|
|
|
|
2013-10-29 08:20:29 -04:00
|
|
|
return id;
|
|
|
|
|
2013-03-27 07:57:48 -04:00
|
|
|
def select_network(self, id):
|
|
|
|
id = self.request("SELECT_NETWORK " + str(id))
|
|
|
|
if "FAIL" in id:
|
|
|
|
raise Exception("SELECT_NETWORK failed")
|
|
|
|
return None
|
|
|
|
|
2014-01-13 13:47:01 -05:00
|
|
|
def connect_network(self, id, timeout=10):
|
2013-03-27 07:57:48 -04:00
|
|
|
self.dump_monitor()
|
|
|
|
self.select_network(id)
|
2014-01-13 13:47:01 -05:00
|
|
|
ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
|
2013-03-27 07:57:48 -04:00
|
|
|
if ev is None:
|
|
|
|
raise Exception("Association with the AP timed out")
|
|
|
|
self.dump_monitor()
|
|
|
|
|
2013-03-31 05:33:49 -04:00
|
|
|
def get_status(self):
|
2013-03-01 18:05:03 -05:00
|
|
|
res = self.request("STATUS")
|
|
|
|
lines = res.splitlines()
|
2013-03-31 05:33:49 -04:00
|
|
|
vals = dict()
|
2013-03-01 18:05:03 -05:00
|
|
|
for l in lines:
|
|
|
|
[name,value] = l.split('=', 1)
|
2013-03-31 05:33:49 -04:00
|
|
|
vals[name] = value
|
|
|
|
return vals
|
|
|
|
|
|
|
|
def get_status_field(self, field):
|
|
|
|
vals = self.get_status()
|
|
|
|
if field in vals:
|
|
|
|
return vals[field]
|
2013-03-01 18:05:03 -05:00
|
|
|
return None
|
|
|
|
|
2013-03-31 05:33:49 -04:00
|
|
|
def get_group_status(self):
|
2013-03-09 11:24:32 -05:00
|
|
|
res = self.group_request("STATUS")
|
|
|
|
lines = res.splitlines()
|
2013-03-31 05:33:49 -04:00
|
|
|
vals = dict()
|
2013-03-09 11:24:32 -05:00
|
|
|
for l in lines:
|
|
|
|
[name,value] = l.split('=', 1)
|
2013-03-31 05:33:49 -04:00
|
|
|
vals[name] = value
|
|
|
|
return vals
|
|
|
|
|
|
|
|
def get_group_status_field(self, field):
|
|
|
|
vals = self.get_group_status()
|
|
|
|
if field in vals:
|
|
|
|
return vals[field]
|
2013-03-09 11:24:32 -05:00
|
|
|
return None
|
|
|
|
|
2013-09-28 10:31:54 -04:00
|
|
|
def get_driver_status(self):
|
|
|
|
res = self.request("STATUS-DRIVER")
|
|
|
|
lines = res.splitlines()
|
|
|
|
vals = dict()
|
|
|
|
for l in lines:
|
|
|
|
[name,value] = l.split('=', 1)
|
|
|
|
vals[name] = value
|
|
|
|
return vals
|
|
|
|
|
|
|
|
def get_driver_status_field(self, field):
|
|
|
|
vals = self.get_driver_status()
|
|
|
|
if field in vals:
|
|
|
|
return vals[field]
|
|
|
|
return None
|
|
|
|
|
2013-03-01 18:05:03 -05:00
|
|
|
def p2p_dev_addr(self):
|
2013-03-31 05:33:49 -04:00
|
|
|
return self.get_status_field("p2p_device_address")
|
2013-03-01 18:05:03 -05:00
|
|
|
|
2013-03-09 11:24:32 -05:00
|
|
|
def p2p_interface_addr(self):
|
2013-03-31 05:33:49 -04:00
|
|
|
return self.get_group_status_field("address")
|
2013-03-09 11:24:32 -05:00
|
|
|
|
2013-03-01 18:05:03 -05:00
|
|
|
def p2p_listen(self):
|
2013-06-30 17:13:11 -04:00
|
|
|
return self.global_request("P2P_LISTEN")
|
2013-03-01 18:05:03 -05:00
|
|
|
|
2014-01-08 15:25:58 -05:00
|
|
|
def p2p_find(self, social=False, dev_id=None, dev_type=None):
|
|
|
|
cmd = "P2P_FIND"
|
2013-03-01 18:05:03 -05:00
|
|
|
if social:
|
2014-01-08 15:25:58 -05:00
|
|
|
cmd = cmd + " type=social"
|
|
|
|
if dev_id:
|
|
|
|
cmd = cmd + " dev_id=" + dev_id
|
|
|
|
if dev_type:
|
|
|
|
cmd = cmd + " dev_type=" + dev_type
|
|
|
|
return self.global_request(cmd)
|
2013-03-01 18:05:03 -05:00
|
|
|
|
2013-03-17 04:58:48 -04:00
|
|
|
def p2p_stop_find(self):
|
2013-06-30 17:13:11 -04:00
|
|
|
return self.global_request("P2P_STOP_FIND")
|
2013-03-17 04:58:48 -04:00
|
|
|
|
2013-03-01 18:05:03 -05:00
|
|
|
def wps_read_pin(self):
|
|
|
|
#TODO: make this random
|
|
|
|
self.pin = "12345670"
|
|
|
|
return self.pin
|
|
|
|
|
|
|
|
def peer_known(self, peer, full=True):
|
2013-06-30 17:13:11 -04:00
|
|
|
res = self.global_request("P2P_PEER " + peer)
|
2013-03-01 18:05:03 -05:00
|
|
|
if peer.lower() not in res.lower():
|
|
|
|
return False
|
|
|
|
if not full:
|
|
|
|
return True
|
|
|
|
return "[PROBE_REQ_ONLY]" not in res
|
|
|
|
|
2014-01-05 09:42:48 -05:00
|
|
|
def discover_peer(self, peer, full=True, timeout=15, social=True, force_find=False):
|
2013-03-01 18:05:03 -05:00
|
|
|
logger.info(self.ifname + ": Trying to discover peer " + peer)
|
2014-01-05 09:42:48 -05:00
|
|
|
if not force_find and self.peer_known(peer, full):
|
2013-03-01 18:05:03 -05:00
|
|
|
return True
|
2013-03-16 05:47:01 -04:00
|
|
|
self.p2p_find(social)
|
2013-03-01 18:05:03 -05:00
|
|
|
count = 0
|
|
|
|
while count < timeout:
|
|
|
|
time.sleep(1)
|
|
|
|
count = count + 1
|
|
|
|
if self.peer_known(peer, full):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2013-09-01 12:24:12 -04:00
|
|
|
def get_peer(self, peer):
|
|
|
|
res = self.global_request("P2P_PEER " + peer)
|
|
|
|
if peer.lower() not in res.lower():
|
|
|
|
raise Exception("Peer information not available")
|
|
|
|
lines = res.splitlines()
|
|
|
|
vals = dict()
|
|
|
|
for l in lines:
|
|
|
|
if '=' in l:
|
|
|
|
[name,value] = l.split('=', 1)
|
|
|
|
vals[name] = value
|
|
|
|
return vals
|
|
|
|
|
2013-10-26 13:47:08 -04:00
|
|
|
def group_form_result(self, ev, expect_failure=False, go_neg_res=None):
|
2013-03-09 08:52:57 -05:00
|
|
|
if expect_failure:
|
|
|
|
if "P2P-GROUP-STARTED" in ev:
|
|
|
|
raise Exception("Group formation succeeded when expecting failure")
|
|
|
|
exp = r'<.>(P2P-GO-NEG-FAILURE) status=([0-9]*)'
|
|
|
|
s = re.split(exp, ev)
|
|
|
|
if len(s) < 3:
|
|
|
|
return None
|
|
|
|
res = {}
|
|
|
|
res['result'] = 'go-neg-failed'
|
|
|
|
res['status'] = int(s[2])
|
|
|
|
return res
|
|
|
|
|
|
|
|
if "P2P-GROUP-STARTED" not in ev:
|
|
|
|
raise Exception("No P2P-GROUP-STARTED event seen")
|
|
|
|
|
2013-03-09 06:36:35 -05:00
|
|
|
exp = r'<.>(P2P-GROUP-STARTED) ([^ ]*) ([^ ]*) ssid="(.*)" freq=([0-9]*) ((?:psk=.*)|(?:passphrase=".*")) go_dev_addr=([0-9a-f:]*)'
|
|
|
|
s = re.split(exp, ev)
|
|
|
|
if len(s) < 8:
|
|
|
|
raise Exception("Could not parse P2P-GROUP-STARTED")
|
|
|
|
res = {}
|
|
|
|
res['result'] = 'success'
|
|
|
|
res['ifname'] = s[2]
|
2013-03-09 10:34:08 -05:00
|
|
|
self.group_ifname = s[2]
|
2013-03-09 06:36:35 -05:00
|
|
|
res['role'] = s[3]
|
|
|
|
res['ssid'] = s[4]
|
|
|
|
res['freq'] = s[5]
|
2013-09-01 12:24:12 -04:00
|
|
|
if "[PERSISTENT]" in ev:
|
|
|
|
res['persistent'] = True
|
|
|
|
else:
|
|
|
|
res['persistent'] = False
|
2013-03-09 06:36:35 -05:00
|
|
|
p = re.match(r'psk=([0-9a-f]*)', s[6])
|
|
|
|
if p:
|
|
|
|
res['psk'] = p.group(1)
|
|
|
|
p = re.match(r'passphrase="(.*)"', s[6])
|
|
|
|
if p:
|
|
|
|
res['passphrase'] = p.group(1)
|
|
|
|
res['go_dev_addr'] = s[7]
|
2013-10-26 13:47:08 -04:00
|
|
|
|
|
|
|
if go_neg_res:
|
|
|
|
exp = r'<.>(P2P-GO-NEG-SUCCESS) role=(GO|client) freq=([0-9]*)'
|
|
|
|
s = re.split(exp, go_neg_res)
|
|
|
|
if len(s) < 4:
|
|
|
|
raise Exception("Could not parse P2P-GO-NEG-SUCCESS")
|
|
|
|
res['go_neg_role'] = s[2]
|
|
|
|
res['go_neg_freq'] = s[3]
|
|
|
|
|
2013-03-09 06:36:35 -05:00
|
|
|
return res
|
|
|
|
|
2013-10-26 13:47:08 -04:00
|
|
|
def p2p_go_neg_auth(self, peer, pin, method, go_intent=None, persistent=False, freq=None):
|
2013-03-01 18:05:03 -05:00
|
|
|
if not self.discover_peer(peer):
|
|
|
|
raise Exception("Peer " + peer + " not found")
|
|
|
|
self.dump_monitor()
|
|
|
|
cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
|
2013-03-09 06:52:59 -05:00
|
|
|
if go_intent:
|
|
|
|
cmd = cmd + ' go_intent=' + str(go_intent)
|
2013-10-26 13:47:08 -04:00
|
|
|
if freq:
|
|
|
|
cmd = cmd + ' freq=' + str(freq)
|
2013-09-01 12:24:12 -04:00
|
|
|
if persistent:
|
|
|
|
cmd = cmd + " persistent"
|
2013-06-30 17:13:11 -04:00
|
|
|
if "OK" in self.global_request(cmd):
|
2013-03-01 18:05:03 -05:00
|
|
|
return None
|
|
|
|
raise Exception("P2P_CONNECT (auth) failed")
|
|
|
|
|
2013-03-09 06:52:59 -05:00
|
|
|
def p2p_go_neg_auth_result(self, timeout=1, expect_failure=False):
|
2013-10-26 13:47:08 -04:00
|
|
|
go_neg_res = None
|
|
|
|
ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
|
|
|
|
"P2P-GO-NEG-FAILURE"], timeout);
|
2013-03-09 06:36:35 -05:00
|
|
|
if ev is None:
|
2013-03-09 06:52:59 -05:00
|
|
|
if expect_failure:
|
|
|
|
return None
|
2013-03-09 06:36:35 -05:00
|
|
|
raise Exception("Group formation timed out")
|
2013-10-26 13:47:08 -04:00
|
|
|
if "P2P-GO-NEG-SUCCESS" in ev:
|
|
|
|
go_neg_res = ev
|
|
|
|
ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout);
|
|
|
|
if ev is None:
|
|
|
|
if expect_failure:
|
|
|
|
return None
|
|
|
|
raise Exception("Group formation timed out")
|
2013-03-09 06:36:35 -05:00
|
|
|
self.dump_monitor()
|
2013-10-26 13:47:08 -04:00
|
|
|
return self.group_form_result(ev, expect_failure, go_neg_res)
|
2013-03-09 06:36:35 -05:00
|
|
|
|
2013-09-28 05:09:40 -04:00
|
|
|
def p2p_go_neg_init(self, peer, pin, method, timeout=0, go_intent=None, expect_failure=False, persistent=False, freq=None):
|
2013-03-01 18:05:03 -05:00
|
|
|
if not self.discover_peer(peer):
|
|
|
|
raise Exception("Peer " + peer + " not found")
|
|
|
|
self.dump_monitor()
|
2013-03-17 05:10:59 -04:00
|
|
|
if pin:
|
|
|
|
cmd = "P2P_CONNECT " + peer + " " + pin + " " + method
|
|
|
|
else:
|
|
|
|
cmd = "P2P_CONNECT " + peer + " " + method
|
2013-03-09 06:52:59 -05:00
|
|
|
if go_intent:
|
|
|
|
cmd = cmd + ' go_intent=' + str(go_intent)
|
2013-09-28 05:09:40 -04:00
|
|
|
if freq:
|
|
|
|
cmd = cmd + ' freq=' + str(freq)
|
2013-09-01 12:24:12 -04:00
|
|
|
if persistent:
|
|
|
|
cmd = cmd + " persistent"
|
2013-06-30 17:13:11 -04:00
|
|
|
if "OK" in self.global_request(cmd):
|
2013-03-01 18:05:03 -05:00
|
|
|
if timeout == 0:
|
|
|
|
self.dump_monitor()
|
|
|
|
return None
|
2013-10-26 13:47:08 -04:00
|
|
|
go_neg_res = None
|
|
|
|
ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
|
|
|
|
"P2P-GO-NEG-FAILURE"], timeout)
|
2013-03-09 06:36:35 -05:00
|
|
|
if ev is None:
|
2013-03-09 06:52:59 -05:00
|
|
|
if expect_failure:
|
|
|
|
return None
|
2013-03-09 06:36:35 -05:00
|
|
|
raise Exception("Group formation timed out")
|
2013-10-26 13:47:08 -04:00
|
|
|
if "P2P-GO-NEG-SUCCESS" in ev:
|
|
|
|
go_neg_res = ev
|
|
|
|
ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
|
|
|
|
if ev is None:
|
|
|
|
if expect_failure:
|
|
|
|
return None
|
|
|
|
raise Exception("Group formation timed out")
|
2013-03-09 06:36:35 -05:00
|
|
|
self.dump_monitor()
|
2013-10-26 13:47:08 -04:00
|
|
|
return self.group_form_result(ev, expect_failure, go_neg_res)
|
2013-03-01 18:05:03 -05:00
|
|
|
raise Exception("P2P_CONNECT failed")
|
|
|
|
|
2013-12-26 11:18:10 -05:00
|
|
|
def wait_event(self, events, timeout=10):
|
2014-01-05 01:02:06 -05:00
|
|
|
start = os.times()[4]
|
|
|
|
while True:
|
2013-03-01 18:05:03 -05:00
|
|
|
while self.mon.pending():
|
|
|
|
ev = self.mon.recv()
|
2013-03-09 06:52:59 -05:00
|
|
|
logger.debug(self.ifname + ": " + ev)
|
2013-03-09 08:52:57 -05:00
|
|
|
for event in events:
|
|
|
|
if event in ev:
|
|
|
|
return ev
|
2014-01-05 01:02:06 -05:00
|
|
|
now = os.times()[4]
|
|
|
|
remaining = start + timeout - now
|
|
|
|
if remaining <= 0:
|
|
|
|
break
|
|
|
|
if not self.mon.pending(timeout=remaining):
|
|
|
|
break
|
2013-03-09 06:36:35 -05:00
|
|
|
return None
|
2013-03-01 18:05:03 -05:00
|
|
|
|
2013-06-30 17:13:11 -04:00
|
|
|
def wait_global_event(self, events, timeout):
|
|
|
|
if self.global_iface is None:
|
|
|
|
self.wait_event(events, timeout)
|
|
|
|
else:
|
2014-01-05 01:02:06 -05:00
|
|
|
start = os.times()[4]
|
|
|
|
while True:
|
2013-06-30 17:13:11 -04:00
|
|
|
while self.global_mon.pending():
|
|
|
|
ev = self.global_mon.recv()
|
2013-09-25 09:45:45 -04:00
|
|
|
logger.debug(self.ifname + "(global): " + ev)
|
2013-06-30 17:13:11 -04:00
|
|
|
for event in events:
|
|
|
|
if event in ev:
|
|
|
|
return ev
|
2014-01-05 01:02:06 -05:00
|
|
|
now = os.times()[4]
|
|
|
|
remaining = start + timeout - now
|
|
|
|
if remaining <= 0:
|
|
|
|
break
|
|
|
|
if not self.global_mon.pending(timeout=remaining):
|
|
|
|
break
|
2013-06-30 17:13:11 -04:00
|
|
|
return None
|
|
|
|
|
2013-09-01 12:30:08 -04:00
|
|
|
def wait_go_ending_session(self):
|
|
|
|
ev = self.wait_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:
|
|
|
|
raise Exception("Unexpected group removal reason")
|
|
|
|
|
2013-03-01 18:05:03 -05:00
|
|
|
def dump_monitor(self):
|
|
|
|
while self.mon.pending():
|
|
|
|
ev = self.mon.recv()
|
|
|
|
logger.debug(self.ifname + ": " + ev)
|
2013-09-25 09:45:45 -04:00
|
|
|
while self.global_mon.pending():
|
|
|
|
ev = self.global_mon.recv()
|
|
|
|
logger.debug(self.ifname + "(global): " + ev)
|
2013-03-02 04:38:56 -05:00
|
|
|
|
2013-03-02 05:22:28 -05:00
|
|
|
def remove_group(self, ifname=None):
|
|
|
|
if ifname is None:
|
2013-09-01 12:24:12 -04:00
|
|
|
ifname = self.group_ifname if self.group_ifname else self.ifname
|
2013-06-30 17:13:11 -04:00
|
|
|
if "OK" not in self.global_request("P2P_GROUP_REMOVE " + ifname):
|
2013-03-02 04:38:56 -05:00
|
|
|
raise Exception("Group could not be removed")
|
2013-03-09 10:34:08 -05:00
|
|
|
self.group_ifname = None
|
2013-03-09 09:30:25 -05:00
|
|
|
|
2013-03-30 14:16:30 -04:00
|
|
|
def p2p_start_go(self, persistent=None, freq=None):
|
2013-03-09 09:30:25 -05:00
|
|
|
self.dump_monitor()
|
|
|
|
cmd = "P2P_GROUP_ADD"
|
2013-03-09 12:01:56 -05:00
|
|
|
if persistent is None:
|
|
|
|
pass
|
|
|
|
elif persistent is True:
|
|
|
|
cmd = cmd + " persistent"
|
|
|
|
else:
|
|
|
|
cmd = cmd + " persistent=" + str(persistent)
|
2013-03-30 14:16:30 -04:00
|
|
|
if freq:
|
2013-09-28 05:09:40 -04:00
|
|
|
cmd = cmd + " freq=" + str(freq)
|
2013-06-30 17:13:11 -04:00
|
|
|
if "OK" in self.global_request(cmd):
|
|
|
|
ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout=5)
|
2013-03-09 09:30:25 -05:00
|
|
|
if ev is None:
|
|
|
|
raise Exception("GO start up timed out")
|
|
|
|
self.dump_monitor()
|
|
|
|
return self.group_form_result(ev)
|
|
|
|
raise Exception("P2P_GROUP_ADD failed")
|
|
|
|
|
|
|
|
def p2p_go_authorize_client(self, pin):
|
|
|
|
cmd = "WPS_PIN any " + pin
|
2013-03-09 10:34:08 -05:00
|
|
|
if "FAIL" in self.group_request(cmd):
|
2013-03-09 09:30:25 -05:00
|
|
|
raise Exception("Failed to authorize client connection on GO")
|
|
|
|
return None
|
|
|
|
|
2013-09-01 04:56:55 -04:00
|
|
|
def p2p_go_authorize_client_pbc(self):
|
|
|
|
cmd = "WPS_PBC"
|
|
|
|
if "FAIL" in self.group_request(cmd):
|
|
|
|
raise Exception("Failed to authorize client connection on GO")
|
|
|
|
return None
|
|
|
|
|
2013-11-24 13:50:11 -05:00
|
|
|
def p2p_connect_group(self, go_addr, pin, timeout=0, social=False):
|
2013-03-09 09:30:25 -05:00
|
|
|
self.dump_monitor()
|
2013-11-24 13:50:11 -05:00
|
|
|
if not self.discover_peer(go_addr, social=social):
|
2013-03-09 09:30:25 -05:00
|
|
|
raise Exception("GO " + go_addr + " not found")
|
|
|
|
self.dump_monitor()
|
|
|
|
cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
|
2013-06-30 17:13:11 -04:00
|
|
|
if "OK" in self.global_request(cmd):
|
2013-03-09 09:30:25 -05:00
|
|
|
if timeout == 0:
|
|
|
|
self.dump_monitor()
|
|
|
|
return None
|
2013-06-30 17:13:11 -04:00
|
|
|
ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
|
2013-03-09 09:30:25 -05:00
|
|
|
if ev is None:
|
|
|
|
raise Exception("Joining the group timed out")
|
|
|
|
self.dump_monitor()
|
|
|
|
return self.group_form_result(ev)
|
|
|
|
raise Exception("P2P_CONNECT(join) failed")
|
2013-03-09 11:24:32 -05:00
|
|
|
|
|
|
|
def tdls_setup(self, peer):
|
|
|
|
cmd = "TDLS_SETUP " + peer
|
|
|
|
if "FAIL" in self.group_request(cmd):
|
|
|
|
raise Exception("Failed to request TDLS setup")
|
|
|
|
return None
|
|
|
|
|
|
|
|
def tdls_teardown(self, peer):
|
|
|
|
cmd = "TDLS_TEARDOWN " + peer
|
|
|
|
if "FAIL" in self.group_request(cmd):
|
|
|
|
raise Exception("Failed to request TDLS teardown")
|
|
|
|
return None
|
2013-03-29 14:41:33 -04:00
|
|
|
|
2013-12-31 06:21:18 -05:00
|
|
|
def connect(self, ssid=None, ssid2=None, psk=None, proto=None,
|
|
|
|
key_mgmt=None, wep_key0=None,
|
2013-09-29 13:35:26 -04:00
|
|
|
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,
|
2013-11-03 04:58:38 -05:00
|
|
|
domain_suffix_match=None, password_hex=None,
|
2013-12-29 12:25:42 -05:00
|
|
|
client_cert=None, private_key=None, peerkey=False, okc=False,
|
2014-01-05 11:03:29 -05:00
|
|
|
eapol_flags=None, fragment_size=None,
|
2014-01-05 12:20:00 -05:00
|
|
|
wait_connect=True, only_add_network=False,
|
2014-01-07 13:02:04 -05:00
|
|
|
ca_cert2=None, client_cert2=None, private_key2=None,
|
2014-01-08 09:31:22 -05:00
|
|
|
scan_ssid=None, raw_psk=None, pac_file=None,
|
2014-01-08 10:18:22 -05:00
|
|
|
subject_match=None, altsubject_match=None,
|
2014-01-08 10:45:56 -05:00
|
|
|
private_key_passwd=None, ocsp=None):
|
2013-03-29 14:41:33 -04:00
|
|
|
logger.info("Connect STA " + self.ifname + " to AP")
|
|
|
|
id = self.add_network()
|
2013-12-31 06:21:18 -05:00
|
|
|
if ssid:
|
|
|
|
self.set_network_quoted(id, "ssid", ssid)
|
|
|
|
elif ssid2:
|
|
|
|
self.set_network(id, "ssid", ssid2)
|
2013-03-29 14:41:33 -04:00
|
|
|
if psk:
|
|
|
|
self.set_network_quoted(id, "psk", psk)
|
2014-01-07 13:44:41 -05:00
|
|
|
if raw_psk:
|
|
|
|
self.set_network(id, "psk", raw_psk)
|
2013-03-29 14:41:33 -04:00
|
|
|
if proto:
|
|
|
|
self.set_network(id, "proto", proto)
|
|
|
|
if key_mgmt:
|
|
|
|
self.set_network(id, "key_mgmt", key_mgmt)
|
2013-03-30 07:47:22 -04:00
|
|
|
if ieee80211w:
|
|
|
|
self.set_network(id, "ieee80211w", ieee80211w)
|
2013-09-29 05:42:05 -04:00
|
|
|
if pairwise:
|
|
|
|
self.set_network(id, "pairwise", pairwise)
|
|
|
|
if group:
|
|
|
|
self.set_network(id, "group", group)
|
2013-03-29 14:41:33 -04:00
|
|
|
if wep_key0:
|
|
|
|
self.set_network(id, "wep_key0", wep_key0)
|
2013-09-29 05:42:05 -04:00
|
|
|
if scan_freq:
|
|
|
|
self.set_network(id, "scan_freq", scan_freq)
|
2013-09-29 13:35:26 -04:00
|
|
|
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)
|
2013-11-03 04:58:38 -05:00
|
|
|
if password_hex:
|
|
|
|
self.set_network(id, "password", password_hex)
|
2013-09-29 13:35:26 -04:00
|
|
|
if ca_cert:
|
|
|
|
self.set_network_quoted(id, "ca_cert", ca_cert)
|
2013-11-03 05:25:44 -05:00
|
|
|
if client_cert:
|
|
|
|
self.set_network_quoted(id, "client_cert", client_cert)
|
|
|
|
if private_key:
|
|
|
|
self.set_network_quoted(id, "private_key", private_key)
|
2014-01-08 10:18:22 -05:00
|
|
|
if private_key_passwd:
|
|
|
|
self.set_network_quoted(id, "private_key_passwd",
|
|
|
|
private_key_passwd)
|
2014-01-05 12:20:00 -05:00
|
|
|
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)
|
2013-09-29 13:35:26 -04:00
|
|
|
if phase1:
|
|
|
|
self.set_network_quoted(id, "phase1", phase1)
|
|
|
|
if phase2:
|
|
|
|
self.set_network_quoted(id, "phase2", phase2)
|
2013-10-20 12:47:39 -04:00
|
|
|
if domain_suffix_match:
|
|
|
|
self.set_network_quoted(id, "domain_suffix_match",
|
|
|
|
domain_suffix_match)
|
2014-01-08 09:31:22 -05:00
|
|
|
if altsubject_match:
|
|
|
|
self.set_network_quoted(id, "altsubject_match",
|
|
|
|
altsubject_match)
|
|
|
|
if subject_match:
|
|
|
|
self.set_network_quoted(id, "subject_match",
|
|
|
|
subject_match)
|
2013-12-28 04:43:42 -05:00
|
|
|
if peerkey:
|
|
|
|
self.set_network(id, "peerkey", "1")
|
2013-12-29 12:25:42 -05:00
|
|
|
if okc:
|
|
|
|
self.set_network(id, "proactive_key_caching", "1")
|
2013-12-31 05:07:33 -05:00
|
|
|
if eapol_flags:
|
|
|
|
self.set_network(id, "eapol_flags", eapol_flags)
|
2014-01-05 11:03:29 -05:00
|
|
|
if fragment_size:
|
|
|
|
self.set_network(id, "fragment_size", fragment_size)
|
2014-01-07 13:02:04 -05:00
|
|
|
if scan_ssid:
|
|
|
|
self.set_network(id, "scan_ssid", scan_ssid)
|
2014-01-08 03:21:37 -05:00
|
|
|
if pac_file:
|
|
|
|
self.set_network_quoted(id, "pac_file", pac_file)
|
2014-01-08 10:45:56 -05:00
|
|
|
if ocsp:
|
|
|
|
self.set_network(id, "ocsp", str(ocsp))
|
2013-12-29 05:24:25 -05:00
|
|
|
if only_add_network:
|
|
|
|
return id
|
2013-09-29 13:35:26 -04:00
|
|
|
if wait_connect:
|
2014-01-13 13:47:01 -05:00
|
|
|
if eap:
|
|
|
|
self.connect_network(id, timeout=20)
|
|
|
|
else:
|
|
|
|
self.connect_network(id)
|
2013-09-29 13:35:26 -04:00
|
|
|
else:
|
|
|
|
self.dump_monitor()
|
|
|
|
self.select_network(id)
|
2013-10-29 11:14:02 -04:00
|
|
|
return id
|
2013-03-30 05:28:39 -04:00
|
|
|
|
2014-01-03 07:34:59 -05:00
|
|
|
def scan(self, type=None, freq=None, no_wait=False):
|
2013-03-30 05:28:39 -04:00
|
|
|
if type:
|
|
|
|
cmd = "SCAN TYPE=" + type
|
|
|
|
else:
|
|
|
|
cmd = "SCAN"
|
2013-12-25 13:17:32 -05:00
|
|
|
if freq:
|
|
|
|
cmd = cmd + " freq=" + freq
|
2014-01-03 07:34:59 -05:00
|
|
|
if not no_wait:
|
|
|
|
self.dump_monitor()
|
2013-03-30 05:28:39 -04:00
|
|
|
if not "OK" in self.request(cmd):
|
|
|
|
raise Exception("Failed to trigger scan")
|
2014-01-03 07:34:59 -05:00
|
|
|
if no_wait:
|
|
|
|
return
|
2013-03-30 05:28:39 -04:00
|
|
|
ev = self.wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
|
|
|
|
if ev is None:
|
|
|
|
raise Exception("Scan timed out")
|
|
|
|
|
|
|
|
def roam(self, bssid):
|
|
|
|
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")
|
|
|
|
self.dump_monitor()
|
2013-04-28 15:00:58 -04:00
|
|
|
|
2013-12-28 04:52:19 -05:00
|
|
|
def roam_over_ds(self, bssid):
|
|
|
|
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")
|
|
|
|
self.dump_monitor()
|
|
|
|
|
2013-04-28 15:00:58 -04:00
|
|
|
def wps_reg(self, bssid, pin, new_ssid=None, key_mgmt=None, cipher=None,
|
2013-12-28 10:47:32 -05:00
|
|
|
new_passphrase=None, no_wait=False):
|
2013-04-28 15:00:58 -04:00
|
|
|
self.dump_monitor()
|
|
|
|
if new_ssid:
|
|
|
|
self.request("WPS_REG " + bssid + " " + pin + " " +
|
|
|
|
new_ssid.encode("hex") + " " + key_mgmt + " " +
|
|
|
|
cipher + " " + new_passphrase.encode("hex"))
|
2013-12-28 10:47:32 -05:00
|
|
|
if no_wait:
|
|
|
|
return
|
2013-04-28 15:00:58 -04:00
|
|
|
ev = self.wait_event(["WPS-SUCCESS"], timeout=15)
|
|
|
|
else:
|
|
|
|
self.request("WPS_REG " + bssid + " " + pin)
|
2013-12-28 10:47:32 -05:00
|
|
|
if no_wait:
|
|
|
|
return
|
2013-04-28 15:00:58 -04:00
|
|
|
ev = self.wait_event(["WPS-CRED-RECEIVED"], timeout=15)
|
|
|
|
if ev is None:
|
|
|
|
raise Exception("WPS cred timed out")
|
|
|
|
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")
|
2013-11-02 07:04:06 -04:00
|
|
|
|
|
|
|
def relog(self):
|
|
|
|
self.request("RELOG")
|
2013-11-24 13:50:11 -05:00
|
|
|
|
|
|
|
def wait_completed(self, timeout=10):
|
|
|
|
for i in range(0, timeout * 2):
|
|
|
|
if self.get_status_field("wpa_state") == "COMPLETED":
|
|
|
|
return
|
|
|
|
time.sleep(0.5)
|
|
|
|
raise Exception("Timeout while waiting for COMPLETED state")
|
2013-12-25 04:17:32 -05:00
|
|
|
|
|
|
|
def get_capability(self, field):
|
|
|
|
res = self.request("GET_CAPABILITY " + field)
|
|
|
|
if "FAIL" in res:
|
|
|
|
return None
|
|
|
|
return res.split(' ')
|
2013-12-25 13:49:02 -05:00
|
|
|
|
|
|
|
def get_bss(self, bssid):
|
|
|
|
res = self.request("BSS " + bssid)
|
|
|
|
lines = res.splitlines()
|
|
|
|
vals = dict()
|
|
|
|
for l in lines:
|
|
|
|
[name,value] = l.split('=', 1)
|
|
|
|
vals[name] = value
|
|
|
|
return vals
|
2013-12-29 12:25:42 -05:00
|
|
|
|
|
|
|
def get_pmksa(self, bssid):
|
|
|
|
res = self.request("PMKSA")
|
|
|
|
lines = res.splitlines()
|
|
|
|
for l in lines:
|
|
|
|
if bssid not in l:
|
|
|
|
continue
|
|
|
|
vals = dict()
|
|
|
|
[index,aa,pmkid,expiration,opportunistic] = l.split(' ')
|
|
|
|
vals['index'] = index
|
|
|
|
vals['pmkid'] = pmkid
|
|
|
|
vals['expiration'] = expiration
|
|
|
|
vals['opportunistic'] = opportunistic
|
|
|
|
return vals
|
|
|
|
return None
|