2013-03-02 00:05:03 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# Python class for controlling wpa_supplicant
|
|
|
|
# Copyright (c) 2013, Jouni Malinen <j@w1.fi>
|
|
|
|
#
|
|
|
|
# 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 12:36:35 +01:00
|
|
|
import re
|
2013-03-02 00:05:03 +01:00
|
|
|
import wpaspy
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
wpas_ctrl = '/var/run/wpa_supplicant'
|
|
|
|
|
|
|
|
class WpaSupplicant:
|
|
|
|
def __init__(self, ifname):
|
|
|
|
self.ifname = ifname
|
2013-03-09 16:34:08 +01:00
|
|
|
self.group_ifname = None
|
2013-03-02 00:05:03 +01:00
|
|
|
self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
|
|
|
|
self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
|
|
|
|
self.mon.attach()
|
|
|
|
|
|
|
|
def request(self, cmd):
|
|
|
|
logger.debug(self.ifname + ": CTRL: " + cmd)
|
|
|
|
return self.ctrl.request(cmd)
|
|
|
|
|
2013-03-09 16:34:08 +01: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-02 00:05:03 +01:00
|
|
|
def ping(self):
|
|
|
|
return "PONG" in self.request("PING")
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
self.request("P2P_STOP_FIND")
|
|
|
|
self.request("P2P_FLUSH")
|
|
|
|
self.request("P2P_GROUP_REMOVE *")
|
|
|
|
self.request("REMOVE_NETWORK *")
|
|
|
|
self.request("REMOVE_CRED *")
|
2013-03-27 12:57:48 +01:00
|
|
|
self.request("SET tdls_disabled 0")
|
|
|
|
self.request("SET tdls_testing 0")
|
2013-03-09 16:34:08 +01:00
|
|
|
self.group_ifname = None
|
2013-03-02 00:05:03 +01:00
|
|
|
|
2013-03-09 18:01:56 +01: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-03-27 12:57:48 +01:00
|
|
|
def select_network(self, id):
|
|
|
|
id = self.request("SELECT_NETWORK " + str(id))
|
|
|
|
if "FAIL" in id:
|
|
|
|
raise Exception("SELECT_NETWORK failed")
|
|
|
|
return None
|
|
|
|
|
|
|
|
def connect_network(self, id):
|
|
|
|
self.dump_monitor()
|
|
|
|
self.select_network(id)
|
|
|
|
ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
|
|
|
|
if ev is None:
|
|
|
|
raise Exception("Association with the AP timed out")
|
|
|
|
self.dump_monitor()
|
|
|
|
|
2013-03-02 00:05:03 +01:00
|
|
|
def get_status(self, field):
|
|
|
|
res = self.request("STATUS")
|
|
|
|
lines = res.splitlines()
|
|
|
|
for l in lines:
|
|
|
|
[name,value] = l.split('=', 1)
|
|
|
|
if name == field:
|
|
|
|
return value
|
|
|
|
return None
|
|
|
|
|
2013-03-09 17:24:32 +01:00
|
|
|
def get_group_status(self, field):
|
|
|
|
res = self.group_request("STATUS")
|
|
|
|
lines = res.splitlines()
|
|
|
|
for l in lines:
|
|
|
|
[name,value] = l.split('=', 1)
|
|
|
|
if name == field:
|
|
|
|
return value
|
|
|
|
return None
|
|
|
|
|
2013-03-02 00:05:03 +01:00
|
|
|
def p2p_dev_addr(self):
|
|
|
|
return self.get_status("p2p_device_address")
|
|
|
|
|
2013-03-09 17:24:32 +01:00
|
|
|
def p2p_interface_addr(self):
|
|
|
|
return self.get_group_status("address")
|
|
|
|
|
2013-03-02 00:05:03 +01:00
|
|
|
def p2p_listen(self):
|
|
|
|
return self.request("P2P_LISTEN")
|
|
|
|
|
|
|
|
def p2p_find(self, social=False):
|
|
|
|
if social:
|
|
|
|
return self.request("P2P_FIND type=social")
|
|
|
|
return self.request("P2P_FIND")
|
|
|
|
|
2013-03-17 09:58:48 +01:00
|
|
|
def p2p_stop_find(self):
|
|
|
|
return self.request("P2P_STOP_FIND")
|
|
|
|
|
2013-03-02 00:05:03 +01:00
|
|
|
def wps_read_pin(self):
|
|
|
|
#TODO: make this random
|
|
|
|
self.pin = "12345670"
|
|
|
|
return self.pin
|
|
|
|
|
|
|
|
def peer_known(self, peer, full=True):
|
|
|
|
res = self.request("P2P_PEER " + peer)
|
|
|
|
if peer.lower() not in res.lower():
|
|
|
|
return False
|
|
|
|
if not full:
|
|
|
|
return True
|
|
|
|
return "[PROBE_REQ_ONLY]" not in res
|
|
|
|
|
2013-03-16 10:47:01 +01:00
|
|
|
def discover_peer(self, peer, full=True, timeout=15, social=True):
|
2013-03-02 00:05:03 +01:00
|
|
|
logger.info(self.ifname + ": Trying to discover peer " + peer)
|
|
|
|
if self.peer_known(peer, full):
|
|
|
|
return True
|
2013-03-16 10:47:01 +01:00
|
|
|
self.p2p_find(social)
|
2013-03-02 00:05:03 +01:00
|
|
|
count = 0
|
|
|
|
while count < timeout:
|
|
|
|
time.sleep(1)
|
|
|
|
count = count + 1
|
|
|
|
if self.peer_known(peer, full):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2013-03-09 14:52:57 +01:00
|
|
|
def group_form_result(self, ev, expect_failure=False):
|
|
|
|
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 12:36:35 +01: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 16:34:08 +01:00
|
|
|
self.group_ifname = s[2]
|
2013-03-09 12:36:35 +01:00
|
|
|
res['role'] = s[3]
|
|
|
|
res['ssid'] = s[4]
|
|
|
|
res['freq'] = s[5]
|
|
|
|
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]
|
|
|
|
return res
|
|
|
|
|
2013-03-09 12:52:59 +01:00
|
|
|
def p2p_go_neg_auth(self, peer, pin, method, go_intent=None):
|
2013-03-02 00:05:03 +01: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 12:52:59 +01:00
|
|
|
if go_intent:
|
|
|
|
cmd = cmd + ' go_intent=' + str(go_intent)
|
2013-03-02 00:05:03 +01:00
|
|
|
if "OK" in self.request(cmd):
|
|
|
|
return None
|
|
|
|
raise Exception("P2P_CONNECT (auth) failed")
|
|
|
|
|
2013-03-09 12:52:59 +01:00
|
|
|
def p2p_go_neg_auth_result(self, timeout=1, expect_failure=False):
|
2013-03-09 14:52:57 +01:00
|
|
|
ev = self.wait_event(["P2P-GROUP-STARTED","P2P-GO-NEG-FAILURE"], timeout);
|
2013-03-09 12:36:35 +01:00
|
|
|
if ev is None:
|
2013-03-09 12:52:59 +01:00
|
|
|
if expect_failure:
|
|
|
|
return None
|
2013-03-09 12:36:35 +01:00
|
|
|
raise Exception("Group formation timed out")
|
|
|
|
self.dump_monitor()
|
2013-03-09 14:52:57 +01:00
|
|
|
return self.group_form_result(ev, expect_failure)
|
2013-03-09 12:36:35 +01:00
|
|
|
|
2013-03-09 12:52:59 +01:00
|
|
|
def p2p_go_neg_init(self, peer, pin, method, timeout=0, go_intent=None, expect_failure=False):
|
2013-03-02 00:05:03 +01:00
|
|
|
if not self.discover_peer(peer):
|
|
|
|
raise Exception("Peer " + peer + " not found")
|
|
|
|
self.dump_monitor()
|
2013-03-17 10:10:59 +01:00
|
|
|
if pin:
|
|
|
|
cmd = "P2P_CONNECT " + peer + " " + pin + " " + method
|
|
|
|
else:
|
|
|
|
cmd = "P2P_CONNECT " + peer + " " + method
|
2013-03-09 12:52:59 +01:00
|
|
|
if go_intent:
|
|
|
|
cmd = cmd + ' go_intent=' + str(go_intent)
|
2013-03-02 00:05:03 +01:00
|
|
|
if "OK" in self.request(cmd):
|
|
|
|
if timeout == 0:
|
|
|
|
self.dump_monitor()
|
|
|
|
return None
|
2013-03-09 14:52:57 +01:00
|
|
|
ev = self.wait_event(["P2P-GROUP-STARTED","P2P-GO-NEG-FAILURE"], timeout)
|
2013-03-09 12:36:35 +01:00
|
|
|
if ev is None:
|
2013-03-09 12:52:59 +01:00
|
|
|
if expect_failure:
|
|
|
|
return None
|
2013-03-09 12:36:35 +01:00
|
|
|
raise Exception("Group formation timed out")
|
|
|
|
self.dump_monitor()
|
2013-03-09 14:52:57 +01:00
|
|
|
return self.group_form_result(ev, expect_failure)
|
2013-03-02 00:05:03 +01:00
|
|
|
raise Exception("P2P_CONNECT failed")
|
|
|
|
|
2013-03-09 14:52:57 +01:00
|
|
|
def wait_event(self, events, timeout):
|
2013-03-02 00:05:03 +01:00
|
|
|
count = 0
|
2013-03-16 10:21:22 +01:00
|
|
|
while count < timeout * 10:
|
2013-03-02 00:05:03 +01:00
|
|
|
count = count + 1
|
2013-03-09 12:36:35 +01:00
|
|
|
time.sleep(0.1)
|
2013-03-02 00:05:03 +01:00
|
|
|
while self.mon.pending():
|
|
|
|
ev = self.mon.recv()
|
2013-03-09 12:52:59 +01:00
|
|
|
logger.debug(self.ifname + ": " + ev)
|
2013-03-09 14:52:57 +01:00
|
|
|
for event in events:
|
|
|
|
if event in ev:
|
|
|
|
return ev
|
2013-03-09 12:36:35 +01:00
|
|
|
return None
|
2013-03-02 00:05:03 +01:00
|
|
|
|
|
|
|
def dump_monitor(self):
|
|
|
|
while self.mon.pending():
|
|
|
|
ev = self.mon.recv()
|
|
|
|
logger.debug(self.ifname + ": " + ev)
|
2013-03-02 10:38:56 +01:00
|
|
|
|
2013-03-02 11:22:28 +01:00
|
|
|
def remove_group(self, ifname=None):
|
|
|
|
if ifname is None:
|
2013-03-09 16:34:08 +01:00
|
|
|
ifname = self.group_ifname if self.group_ifname else self.iname
|
2013-03-02 10:38:56 +01:00
|
|
|
if "OK" not in self.request("P2P_GROUP_REMOVE " + ifname):
|
|
|
|
raise Exception("Group could not be removed")
|
2013-03-09 16:34:08 +01:00
|
|
|
self.group_ifname = None
|
2013-03-09 15:30:25 +01:00
|
|
|
|
2013-03-09 18:01:56 +01:00
|
|
|
def p2p_start_go(self, persistent=None):
|
2013-03-09 15:30:25 +01:00
|
|
|
self.dump_monitor()
|
|
|
|
cmd = "P2P_GROUP_ADD"
|
2013-03-09 18:01:56 +01:00
|
|
|
if persistent is None:
|
|
|
|
pass
|
|
|
|
elif persistent is True:
|
|
|
|
cmd = cmd + " persistent"
|
|
|
|
else:
|
|
|
|
cmd = cmd + " persistent=" + str(persistent)
|
2013-03-09 15:30:25 +01:00
|
|
|
if "OK" in self.request(cmd):
|
|
|
|
ev = self.wait_event(["P2P-GROUP-STARTED"], timeout=5)
|
|
|
|
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 16:34:08 +01:00
|
|
|
if "FAIL" in self.group_request(cmd):
|
2013-03-09 15:30:25 +01:00
|
|
|
raise Exception("Failed to authorize client connection on GO")
|
|
|
|
return None
|
|
|
|
|
|
|
|
def p2p_connect_group(self, go_addr, pin, timeout=0):
|
|
|
|
self.dump_monitor()
|
2013-03-16 10:47:01 +01:00
|
|
|
if not self.discover_peer(go_addr, social=False):
|
2013-03-09 15:30:25 +01:00
|
|
|
raise Exception("GO " + go_addr + " not found")
|
|
|
|
self.dump_monitor()
|
|
|
|
cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
|
|
|
|
if "OK" in self.request(cmd):
|
|
|
|
if timeout == 0:
|
|
|
|
self.dump_monitor()
|
|
|
|
return None
|
|
|
|
ev = self.wait_event(["P2P-GROUP-STARTED"], timeout)
|
|
|
|
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 17:24:32 +01: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
|