2020-01-27 20:39:54 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
#
|
|
|
|
# Example nfcpy to wpa_supplicant wrapper for DPP NFC operations
|
|
|
|
# Copyright (c) 2012-2013, Jouni Malinen <j@w1.fi>
|
|
|
|
# Copyright (c) 2019-2020, The Linux Foundation
|
|
|
|
#
|
|
|
|
# This software may be distributed under the terms of the BSD license.
|
|
|
|
# See README for more details.
|
|
|
|
|
|
|
|
import os
|
2020-05-15 01:26:01 +02:00
|
|
|
import struct
|
2020-01-27 20:39:54 +01:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import threading
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
import nfc
|
|
|
|
import ndef
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2020-05-14 20:29:25 +02:00
|
|
|
scriptsdir = os.path.dirname(os.path.realpath(sys.modules[__name__].__file__))
|
2020-01-27 20:39:54 +01:00
|
|
|
sys.path.append(os.path.join(scriptsdir, '..', '..', 'wpaspy'))
|
|
|
|
import wpaspy
|
|
|
|
|
|
|
|
wpas_ctrl = '/var/run/wpa_supplicant'
|
|
|
|
ifname = None
|
|
|
|
init_on_touch = False
|
|
|
|
in_raw_mode = False
|
|
|
|
prev_tcgetattr = 0
|
|
|
|
no_input = False
|
|
|
|
srv = None
|
2020-07-30 16:09:56 +02:00
|
|
|
hs_client = None
|
|
|
|
need_client = False
|
2020-01-27 20:39:54 +01:00
|
|
|
continue_loop = True
|
|
|
|
terminate_now = False
|
|
|
|
summary_file = None
|
|
|
|
success_file = None
|
2020-05-15 11:10:59 +02:00
|
|
|
my_crn_ready = False
|
2020-05-15 01:26:01 +02:00
|
|
|
my_crn = None
|
|
|
|
peer_crn = None
|
2020-05-15 13:20:26 +02:00
|
|
|
hs_sent = False
|
2020-07-23 00:30:30 +02:00
|
|
|
netrole = None
|
2020-07-23 11:26:46 +02:00
|
|
|
operation_success = False
|
2020-05-15 11:03:53 +02:00
|
|
|
mutex = threading.Lock()
|
2020-07-30 16:09:56 +02:00
|
|
|
client_mutex = threading.Lock()
|
2020-07-23 23:51:49 +02:00
|
|
|
no_alt_proposal = False
|
2020-07-30 16:09:56 +02:00
|
|
|
i_m_selector = False
|
2020-01-27 20:39:54 +01:00
|
|
|
|
2020-07-23 11:10:26 +02:00
|
|
|
C_NORMAL = '\033[0m'
|
|
|
|
C_RED = '\033[91m'
|
|
|
|
C_GREEN = '\033[92m'
|
|
|
|
C_BLUE = '\033[94m'
|
|
|
|
C_MAGENTA = '\033[95m'
|
|
|
|
C_CYAN = '\033[96m'
|
|
|
|
|
|
|
|
def summary(txt, color=None):
|
2020-05-15 11:03:53 +02:00
|
|
|
with mutex:
|
2020-07-23 11:10:26 +02:00
|
|
|
if color:
|
|
|
|
print(color + txt + C_NORMAL)
|
|
|
|
else:
|
|
|
|
print(txt)
|
2020-05-15 11:03:53 +02:00
|
|
|
if summary_file:
|
|
|
|
with open(summary_file, 'a') as f:
|
|
|
|
f.write(txt + "\n")
|
2020-01-27 20:39:54 +01:00
|
|
|
|
|
|
|
def success_report(txt):
|
|
|
|
summary(txt)
|
|
|
|
if success_file:
|
|
|
|
with open(success_file, 'a') as f:
|
|
|
|
f.write(txt + "\n")
|
|
|
|
|
|
|
|
def wpas_connect():
|
|
|
|
ifaces = []
|
|
|
|
if os.path.isdir(wpas_ctrl):
|
|
|
|
try:
|
|
|
|
ifaces = [os.path.join(wpas_ctrl, i) for i in os.listdir(wpas_ctrl)]
|
|
|
|
except OSError as error:
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Could not find wpa_supplicant: %s", str(error))
|
2020-01-27 20:39:54 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
if len(ifaces) < 1:
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("No wpa_supplicant control interface found")
|
2020-01-27 20:39:54 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
for ctrl in ifaces:
|
2020-07-30 11:51:56 +02:00
|
|
|
if ifname and ifname not in ctrl:
|
|
|
|
continue
|
|
|
|
if os.path.basename(ctrl).startswith("p2p-dev-"):
|
|
|
|
# skip P2P management interface
|
|
|
|
continue
|
2020-01-27 20:39:54 +01:00
|
|
|
try:
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Trying to use control interface " + ctrl)
|
2020-01-27 20:39:54 +01:00
|
|
|
wpas = wpaspy.Ctrl(ctrl)
|
|
|
|
return wpas
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
2020-07-30 11:51:56 +02:00
|
|
|
summary("Could not connect to wpa_supplicant")
|
2020-01-27 20:39:54 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
def dpp_nfc_uri_process(uri):
|
|
|
|
wpas = wpas_connect()
|
|
|
|
if wpas is None:
|
|
|
|
return False
|
|
|
|
peer_id = wpas.request("DPP_NFC_URI " + uri)
|
|
|
|
if "FAIL" in peer_id:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Could not parse DPP URI from NFC URI record", color=C_RED)
|
2020-01-27 20:39:54 +01:00
|
|
|
return False
|
|
|
|
peer_id = int(peer_id)
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("peer_id=%d for URI from NFC Tag: %s" % (peer_id, uri))
|
2020-01-27 20:39:54 +01:00
|
|
|
cmd = "DPP_AUTH_INIT peer=%d" % peer_id
|
2020-05-14 20:46:50 +02:00
|
|
|
global enrollee_only, configurator_only, config_params
|
|
|
|
if enrollee_only:
|
|
|
|
cmd += " role=enrollee"
|
|
|
|
elif configurator_only:
|
|
|
|
cmd += " role=configurator"
|
|
|
|
if config_params:
|
|
|
|
cmd += " " + config_params
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Initiate DPP authentication: " + cmd)
|
2020-01-27 20:39:54 +01:00
|
|
|
res = wpas.request(cmd)
|
|
|
|
if "OK" not in res:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Failed to initiate DPP Authentication", color=C_RED)
|
2020-01-27 20:39:54 +01:00
|
|
|
return False
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("DPP Authentication initiated")
|
2020-01-27 20:39:54 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
def dpp_hs_tag_read(record):
|
|
|
|
wpas = wpas_connect()
|
|
|
|
if wpas is None:
|
|
|
|
return False
|
2020-05-15 11:03:53 +02:00
|
|
|
summary(record)
|
2020-01-27 20:39:54 +01:00
|
|
|
if len(record.data) < 5:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Too short DPP HS", color=C_RED)
|
2020-01-27 20:39:54 +01:00
|
|
|
return False
|
|
|
|
if record.data[0] != 0:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Unexpected URI Identifier Code", color=C_RED)
|
2020-01-27 20:39:54 +01:00
|
|
|
return False
|
|
|
|
uribuf = record.data[1:]
|
|
|
|
try:
|
|
|
|
uri = uribuf.decode()
|
|
|
|
except:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Invalid URI payload", color=C_RED)
|
2020-01-27 20:39:54 +01:00
|
|
|
return False
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("URI: " + uri)
|
2020-01-27 20:39:54 +01:00
|
|
|
if not uri.startswith("DPP:"):
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Not a DPP URI", color=C_RED)
|
2020-01-27 20:39:54 +01:00
|
|
|
return False
|
|
|
|
return dpp_nfc_uri_process(uri)
|
|
|
|
|
|
|
|
def get_status(wpas, extra=None):
|
|
|
|
if extra:
|
|
|
|
extra = "-" + extra
|
|
|
|
else:
|
|
|
|
extra = ""
|
|
|
|
res = wpas.request("STATUS" + extra)
|
|
|
|
lines = res.splitlines()
|
|
|
|
vals = dict()
|
|
|
|
for l in lines:
|
|
|
|
try:
|
|
|
|
[name, value] = l.split('=', 1)
|
|
|
|
except ValueError:
|
2020-05-15 13:46:41 +02:00
|
|
|
summary("Ignore unexpected status line: %s" % l)
|
2020-01-27 20:39:54 +01:00
|
|
|
continue
|
|
|
|
vals[name] = value
|
|
|
|
return vals
|
|
|
|
|
|
|
|
def get_status_field(wpas, field, extra=None):
|
|
|
|
vals = get_status(wpas, extra)
|
|
|
|
if field in vals:
|
|
|
|
return vals[field]
|
|
|
|
return None
|
|
|
|
|
|
|
|
def own_addr(wpas):
|
2020-05-14 23:44:27 +02:00
|
|
|
addr = get_status_field(wpas, "address")
|
|
|
|
if addr is None:
|
|
|
|
addr = get_status_field(wpas, "bssid[0]")
|
|
|
|
return addr
|
2020-01-27 20:39:54 +01:00
|
|
|
|
|
|
|
def dpp_bootstrap_gen(wpas, type="qrcode", chan=None, mac=None, info=None,
|
|
|
|
curve=None, key=None):
|
|
|
|
cmd = "DPP_BOOTSTRAP_GEN type=" + type
|
|
|
|
if chan:
|
|
|
|
cmd += " chan=" + chan
|
|
|
|
if mac:
|
|
|
|
if mac is True:
|
|
|
|
mac = own_addr(wpas)
|
2020-05-14 23:31:32 +02:00
|
|
|
if mac is None:
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Could not determine local MAC address for bootstrap info")
|
2020-05-14 23:31:32 +02:00
|
|
|
else:
|
|
|
|
cmd += " mac=" + mac.replace(':', '')
|
2020-01-27 20:39:54 +01:00
|
|
|
if info:
|
|
|
|
cmd += " info=" + info
|
|
|
|
if curve:
|
|
|
|
cmd += " curve=" + curve
|
|
|
|
if key:
|
|
|
|
cmd += " key=" + key
|
|
|
|
res = wpas.request(cmd)
|
|
|
|
if "FAIL" in res:
|
|
|
|
raise Exception("Failed to generate bootstrapping info")
|
|
|
|
return int(res)
|
|
|
|
|
2020-06-23 12:24:38 +02:00
|
|
|
def wpas_get_nfc_uri(start_listen=True, pick_channel=False, chan_override=None):
|
2020-07-23 00:30:30 +02:00
|
|
|
listen_freq = 2412
|
2020-01-27 20:39:54 +01:00
|
|
|
wpas = wpas_connect()
|
|
|
|
if wpas is None:
|
|
|
|
return None
|
2020-02-06 22:22:39 +01:00
|
|
|
global own_id, chanlist
|
2020-06-23 12:24:38 +02:00
|
|
|
if chan_override:
|
|
|
|
chan = chan_override
|
|
|
|
else:
|
|
|
|
chan = chanlist
|
2020-05-15 13:17:40 +02:00
|
|
|
if chan is None and get_status_field(wpas, "bssid[0]"):
|
|
|
|
freq = get_status_field(wpas, "freq")
|
|
|
|
if freq:
|
|
|
|
freq = int(freq)
|
|
|
|
if freq >= 2412 and freq <= 2462:
|
|
|
|
chan = "81/%d" % ((freq - 2407) / 5)
|
|
|
|
summary("Use current AP operating channel (%d MHz) as the URI channel list (%s)" % (freq, chan))
|
2020-07-23 00:30:30 +02:00
|
|
|
listen_freq = freq
|
2020-05-15 13:17:40 +02:00
|
|
|
if chan is None and pick_channel:
|
|
|
|
chan = "81/6"
|
|
|
|
summary("Use channel 2437 MHz since no other preference provided")
|
2020-07-23 00:30:30 +02:00
|
|
|
listen_freq = 2437
|
2020-05-15 13:17:40 +02:00
|
|
|
own_id = dpp_bootstrap_gen(wpas, type="nfc-uri", chan=chan, mac=True)
|
2020-01-27 20:39:54 +01:00
|
|
|
res = wpas.request("DPP_BOOTSTRAP_GET_URI %d" % own_id).rstrip()
|
|
|
|
if "FAIL" in res:
|
|
|
|
return None
|
|
|
|
if start_listen:
|
2020-07-23 00:30:30 +02:00
|
|
|
cmd = "DPP_LISTEN %d" % listen_freq
|
|
|
|
global netrole
|
|
|
|
if netrole:
|
|
|
|
cmd += " netrole=" + netrole
|
2020-07-23 10:32:21 +02:00
|
|
|
res2 = wpas.request(cmd)
|
|
|
|
if "OK" not in res2:
|
2020-07-23 00:30:30 +02:00
|
|
|
raise Exception("Failed to start listen operation (%s)" % cmd)
|
2020-01-27 20:39:54 +01:00
|
|
|
return res
|
|
|
|
|
|
|
|
def wpas_report_handover_req(uri):
|
|
|
|
wpas = wpas_connect()
|
|
|
|
if wpas is None:
|
|
|
|
return None
|
|
|
|
global own_id
|
|
|
|
cmd = "DPP_NFC_HANDOVER_REQ own=%d uri=%s" % (own_id, uri)
|
|
|
|
return wpas.request(cmd)
|
|
|
|
|
|
|
|
def wpas_report_handover_sel(uri):
|
|
|
|
wpas = wpas_connect()
|
|
|
|
if wpas is None:
|
|
|
|
return None
|
|
|
|
global own_id
|
|
|
|
cmd = "DPP_NFC_HANDOVER_SEL own=%d uri=%s" % (own_id, uri)
|
|
|
|
return wpas.request(cmd)
|
|
|
|
|
2020-06-23 12:24:38 +02:00
|
|
|
def dpp_handover_client(llc, alt=False):
|
2020-07-30 16:09:56 +02:00
|
|
|
summary("About to start run_dpp_handover_client (alt=%s)" % str(alt))
|
|
|
|
if alt:
|
|
|
|
global need_client
|
|
|
|
need_client = True
|
|
|
|
with client_mutex:
|
|
|
|
summary("Start run_dpp_handover_client (client_mutex held; alt=%s))" % str(alt))
|
|
|
|
if alt:
|
|
|
|
global i_m_selector
|
|
|
|
i_m_selector = False
|
|
|
|
run_dpp_handover_client(llc, alt)
|
|
|
|
summary("Done run_dpp_handover_client (alt=%s)" % str(alt))
|
|
|
|
|
|
|
|
def run_dpp_handover_client(llc, alt=False):
|
2020-06-23 12:24:38 +02:00
|
|
|
chan_override = None
|
2020-07-24 11:46:20 +02:00
|
|
|
global alt_proposal_used
|
2020-06-23 12:24:38 +02:00
|
|
|
if alt:
|
|
|
|
global altchanlist
|
|
|
|
chan_override = altchanlist
|
2020-07-24 11:46:20 +02:00
|
|
|
alt_proposal_used = True
|
2020-07-23 23:30:38 +02:00
|
|
|
global test_uri, test_alt_uri
|
|
|
|
if test_uri:
|
|
|
|
summary("TEST MODE: Using specified URI (alt=%s)" % str(alt))
|
|
|
|
uri = test_alt_uri if alt else test_uri
|
|
|
|
else:
|
|
|
|
uri = wpas_get_nfc_uri(start_listen=False, chan_override=chan_override)
|
2020-05-14 23:31:32 +02:00
|
|
|
if uri is None:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Cannot start handover client - no bootstrap URI available",
|
|
|
|
color=C_RED)
|
2020-05-14 23:31:32 +02:00
|
|
|
return
|
2020-01-27 20:39:54 +01:00
|
|
|
uri = ndef.UriRecord(uri)
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("NFC URI record for DPP: " + str(uri))
|
2020-01-27 20:39:54 +01:00
|
|
|
carrier = ndef.Record('application/vnd.wfa.dpp', 'A', uri.data)
|
2020-07-23 23:30:38 +02:00
|
|
|
global test_crn
|
|
|
|
if test_crn:
|
|
|
|
prev, = struct.unpack('>H', test_crn)
|
|
|
|
summary("TEST MODE: Use specified crn %d" % prev)
|
|
|
|
crn = test_crn
|
|
|
|
test_crn = struct.pack('>H', prev + 0x10)
|
|
|
|
else:
|
|
|
|
crn = os.urandom(2)
|
2020-05-15 01:26:01 +02:00
|
|
|
hr = ndef.HandoverRequestRecord(version="1.4", crn=crn)
|
2020-01-27 20:39:54 +01:00
|
|
|
hr.add_alternative_carrier('active', carrier.name)
|
|
|
|
message = [hr, carrier]
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("NFC Handover Request message for DPP: " + str(message))
|
2020-01-27 20:39:54 +01:00
|
|
|
|
2020-05-15 01:26:01 +02:00
|
|
|
global peer_crn
|
2020-06-24 21:37:52 +02:00
|
|
|
if peer_crn is not None and not alt:
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("NFC handover request from peer was already received - do not send own")
|
2020-05-15 01:26:01 +02:00
|
|
|
return
|
2020-07-30 16:09:56 +02:00
|
|
|
global hs_client
|
|
|
|
if hs_client:
|
|
|
|
summary("Use already started handover client")
|
|
|
|
client = hs_client
|
|
|
|
else:
|
|
|
|
summary("Start handover client")
|
|
|
|
client = nfc.handover.HandoverClient(llc)
|
|
|
|
try:
|
|
|
|
summary("Trying to initiate NFC connection handover")
|
|
|
|
client.connect()
|
|
|
|
summary("Connected for handover")
|
|
|
|
except nfc.llcp.ConnectRefused:
|
|
|
|
summary("Handover connection refused")
|
|
|
|
client.close()
|
|
|
|
return
|
|
|
|
except Exception as e:
|
|
|
|
summary("Other exception: " + str(e))
|
|
|
|
client.close()
|
|
|
|
return
|
|
|
|
hs_client = client;
|
2020-01-27 20:39:54 +01:00
|
|
|
|
2020-06-24 21:37:52 +02:00
|
|
|
if peer_crn is not None and not alt:
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("NFC handover request from peer was already received - do not send own")
|
2020-05-15 01:26:01 +02:00
|
|
|
return
|
|
|
|
|
2020-01-27 20:39:54 +01:00
|
|
|
summary("Sending handover request")
|
|
|
|
|
2020-07-30 16:09:56 +02:00
|
|
|
global my_crn, my_crn_ready, hs_sent, need_client
|
2020-05-15 11:10:59 +02:00
|
|
|
my_crn_ready = True
|
|
|
|
|
2020-01-27 20:39:54 +01:00
|
|
|
if not client.send_records(message):
|
2020-05-15 11:10:59 +02:00
|
|
|
my_crn_ready = False
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Failed to send handover request", color=C_RED)
|
2020-07-30 16:09:56 +02:00
|
|
|
hs_client = None
|
2020-01-27 20:39:54 +01:00
|
|
|
client.close()
|
|
|
|
return
|
|
|
|
|
2020-05-15 01:26:01 +02:00
|
|
|
my_crn, = struct.unpack('>H', crn)
|
|
|
|
|
2020-01-27 20:39:54 +01:00
|
|
|
summary("Receiving handover response")
|
2020-05-15 13:20:26 +02:00
|
|
|
try:
|
|
|
|
message = client.recv_records(timeout=3.0)
|
|
|
|
except Exception as e:
|
|
|
|
# This is fine if we are the handover selector
|
|
|
|
if hs_sent:
|
|
|
|
summary("Client receive failed as expected since I'm the handover server: %s" % str(e))
|
2020-07-24 11:46:20 +02:00
|
|
|
elif alt_proposal_used and not alt:
|
|
|
|
summary("Client received failed for initial proposal as expected since alternative proposal was also used: %s" % str(e))
|
2020-05-15 13:20:26 +02:00
|
|
|
else:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Client receive failed: %s" % str(e), color=C_RED)
|
2020-05-15 13:20:26 +02:00
|
|
|
message = None
|
2020-01-27 20:39:54 +01:00
|
|
|
if message is None:
|
2020-05-15 13:20:26 +02:00
|
|
|
if hs_sent:
|
|
|
|
summary("No response received as expected since I'm the handover server")
|
2020-07-24 11:46:20 +02:00
|
|
|
elif alt_proposal_used and not alt:
|
|
|
|
summary("No response received for initial proposal as expected since alternative proposal was also used")
|
2020-07-30 16:09:56 +02:00
|
|
|
elif need_client:
|
|
|
|
summary("No response received, but handover client is still needed")
|
2020-05-15 13:20:26 +02:00
|
|
|
else:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("No response received", color=C_RED)
|
2020-07-30 16:09:56 +02:00
|
|
|
hs_client = None
|
|
|
|
client.close()
|
2020-01-27 20:39:54 +01:00
|
|
|
return
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Received message: " + str(message))
|
2020-01-27 20:39:54 +01:00
|
|
|
if len(message) < 1 or \
|
|
|
|
not isinstance(message[0], ndef.HandoverSelectRecord):
|
|
|
|
summary("Response was not Hs - received: " + message.type)
|
2020-07-30 16:09:56 +02:00
|
|
|
hs_client = None
|
2020-01-27 20:39:54 +01:00
|
|
|
client.close()
|
|
|
|
return
|
|
|
|
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Received handover select message")
|
|
|
|
summary("alternative carriers: " + str(message[0].alternative_carriers))
|
2020-07-30 16:09:56 +02:00
|
|
|
global i_m_selector
|
|
|
|
if i_m_selector:
|
|
|
|
summary("Ignore the received select since I'm the handover selector")
|
|
|
|
return
|
2020-01-27 20:39:54 +01:00
|
|
|
|
2020-07-24 11:46:20 +02:00
|
|
|
if alt_proposal_used and not alt:
|
|
|
|
summary("Ignore received handover select for the initial proposal since alternative proposal was sent")
|
|
|
|
client.close()
|
|
|
|
return
|
|
|
|
|
2020-02-06 22:47:54 +01:00
|
|
|
dpp_found = False
|
2020-01-27 20:39:54 +01:00
|
|
|
for carrier in message:
|
|
|
|
if isinstance(carrier, ndef.HandoverSelectRecord):
|
|
|
|
continue
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Remote carrier type: " + carrier.type)
|
2020-01-27 20:39:54 +01:00
|
|
|
if carrier.type == "application/vnd.wfa.dpp":
|
|
|
|
if len(carrier.data) == 0 or carrier.data[0] != 0:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("URI Identifier Code 'None' not seen", color=C_RED)
|
2020-01-27 20:39:54 +01:00
|
|
|
continue
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("DPP carrier type match - send to wpa_supplicant")
|
2020-02-06 22:47:54 +01:00
|
|
|
dpp_found = True
|
2020-01-27 20:39:54 +01:00
|
|
|
uri = carrier.data[1:].decode("utf-8")
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("DPP URI: " + uri)
|
2020-07-23 23:30:38 +02:00
|
|
|
if test_uri:
|
|
|
|
summary("TEST MODE: Fake processing")
|
|
|
|
break
|
2020-01-27 20:39:54 +01:00
|
|
|
res = wpas_report_handover_sel(uri)
|
|
|
|
if res is None or "FAIL" in res:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("DPP handover report rejected", color=C_RED)
|
2020-01-27 20:39:54 +01:00
|
|
|
break
|
|
|
|
|
|
|
|
success_report("DPP handover reported successfully (initiator)")
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("peer_id=" + res)
|
2020-01-27 20:39:54 +01:00
|
|
|
peer_id = int(res)
|
|
|
|
wpas = wpas_connect()
|
|
|
|
if wpas is None:
|
|
|
|
break
|
2020-05-11 23:57:44 +02:00
|
|
|
|
|
|
|
global enrollee_only
|
|
|
|
global config_params
|
|
|
|
if enrollee_only:
|
|
|
|
extra = " role=enrollee"
|
|
|
|
elif config_params:
|
|
|
|
extra = " role=configurator " + config_params
|
|
|
|
else:
|
|
|
|
# TODO: Single Configurator instance
|
|
|
|
res = wpas.request("DPP_CONFIGURATOR_ADD")
|
|
|
|
if "FAIL" in res:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Failed to initiate Configurator", color=C_RED)
|
2020-05-11 23:57:44 +02:00
|
|
|
break
|
|
|
|
conf_id = int(res)
|
|
|
|
extra = " conf=sta-dpp configurator=%d" % conf_id
|
2020-01-27 20:39:54 +01:00
|
|
|
global own_id
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Initiate DPP authentication")
|
2020-05-11 23:57:44 +02:00
|
|
|
cmd = "DPP_AUTH_INIT peer=%d own=%d" % (peer_id, own_id)
|
|
|
|
cmd += extra
|
2020-01-27 20:39:54 +01:00
|
|
|
res = wpas.request(cmd)
|
|
|
|
if "FAIL" in res:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Failed to initiate DPP authentication", color=C_RED)
|
2020-01-27 20:39:54 +01:00
|
|
|
break
|
|
|
|
|
2020-07-23 23:51:49 +02:00
|
|
|
global no_alt_proposal
|
|
|
|
if not dpp_found and no_alt_proposal:
|
|
|
|
summary("DPP carrier not seen in response - do not allow alternative proposal anymore")
|
|
|
|
elif not dpp_found:
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("DPP carrier not seen in response - allow peer to initiate a new handover with different parameters")
|
2020-06-23 12:24:38 +02:00
|
|
|
my_crn_ready = False
|
|
|
|
my_crn = None
|
|
|
|
peer_crn = None
|
|
|
|
hs_sent = False
|
2020-07-30 16:09:56 +02:00
|
|
|
hs_client = None
|
2020-02-06 22:47:54 +01:00
|
|
|
client.close()
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Returning from dpp_handover_client")
|
2020-02-06 22:47:54 +01:00
|
|
|
return
|
|
|
|
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Remove peer")
|
2020-07-30 16:09:56 +02:00
|
|
|
hs_client = None
|
2020-01-27 20:39:54 +01:00
|
|
|
client.close()
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Done with handover")
|
2020-01-27 20:39:54 +01:00
|
|
|
global only_one
|
|
|
|
if only_one:
|
|
|
|
print("only_one -> stop loop")
|
|
|
|
global continue_loop
|
|
|
|
continue_loop = False
|
|
|
|
|
|
|
|
global no_wait
|
|
|
|
if no_wait:
|
|
|
|
print("Trying to exit..")
|
|
|
|
global terminate_now
|
|
|
|
terminate_now = True
|
|
|
|
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Returning from dpp_handover_client")
|
2020-01-27 20:39:54 +01:00
|
|
|
|
|
|
|
class HandoverServer(nfc.handover.HandoverServer):
|
|
|
|
def __init__(self, llc):
|
|
|
|
super(HandoverServer, self).__init__(llc)
|
|
|
|
self.sent_carrier = None
|
|
|
|
self.ho_server_processing = False
|
|
|
|
self.success = False
|
2020-02-06 22:47:54 +01:00
|
|
|
self.try_own = False
|
2020-06-23 12:24:38 +02:00
|
|
|
self.llc = llc
|
2020-01-27 20:39:54 +01:00
|
|
|
|
|
|
|
def process_handover_request_message(self, records):
|
|
|
|
self.ho_server_processing = True
|
2020-05-15 11:03:53 +02:00
|
|
|
global in_raw_mode
|
|
|
|
was_in_raw_mode = in_raw_mode
|
2020-01-27 20:39:54 +01:00
|
|
|
clear_raw_mode()
|
2020-05-15 11:03:53 +02:00
|
|
|
if was_in_raw_mode:
|
|
|
|
print("\n")
|
|
|
|
summary("HandoverServer - request received: " + str(records))
|
2020-01-27 20:39:54 +01:00
|
|
|
|
2020-05-15 11:10:59 +02:00
|
|
|
global my_crn, peer_crn, my_crn_ready
|
2020-05-15 01:26:01 +02:00
|
|
|
|
|
|
|
for carrier in records:
|
|
|
|
if not isinstance(carrier, ndef.HandoverRequestRecord):
|
|
|
|
continue
|
|
|
|
if carrier.collision_resolution_number:
|
|
|
|
peer_crn = carrier.collision_resolution_number
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("peer_crn: %d" % peer_crn)
|
2020-05-15 01:26:01 +02:00
|
|
|
|
2020-05-15 11:10:59 +02:00
|
|
|
if my_crn is None and my_crn_ready:
|
|
|
|
summary("Still trying to send own handover request - wait a moment to see if that succeeds before checking crn values")
|
|
|
|
for i in range(10):
|
|
|
|
if my_crn is not None:
|
|
|
|
break
|
|
|
|
time.sleep(0.01)
|
2020-05-15 01:26:01 +02:00
|
|
|
if my_crn is not None:
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("my_crn: %d" % my_crn)
|
2020-05-15 01:26:01 +02:00
|
|
|
|
|
|
|
if my_crn is not None and peer_crn is not None:
|
|
|
|
if my_crn == peer_crn:
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Same crn used - automatic collision resolution failed")
|
2020-05-15 01:26:01 +02:00
|
|
|
# TODO: Should generate a new Handover Request message
|
|
|
|
return ''
|
|
|
|
if ((my_crn & 1) == (peer_crn & 1) and my_crn > peer_crn) or \
|
|
|
|
((my_crn & 1) != (peer_crn & 1) and my_crn < peer_crn):
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("I'm the Handover Selector Device")
|
2020-07-30 16:09:56 +02:00
|
|
|
global i_m_selector
|
|
|
|
i_m_selector = True
|
2020-05-15 01:26:01 +02:00
|
|
|
else:
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Peer is the Handover Selector device")
|
|
|
|
summary("Ignore the received request.")
|
2020-05-15 01:26:01 +02:00
|
|
|
return ''
|
|
|
|
|
2020-01-27 20:39:54 +01:00
|
|
|
hs = ndef.HandoverSelectRecord('1.4')
|
|
|
|
sel = [hs]
|
|
|
|
|
|
|
|
found = False
|
|
|
|
|
|
|
|
for carrier in records:
|
|
|
|
if isinstance(carrier, ndef.HandoverRequestRecord):
|
|
|
|
continue
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Remote carrier type: " + carrier.type)
|
2020-01-27 20:39:54 +01:00
|
|
|
if carrier.type == "application/vnd.wfa.dpp":
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("DPP carrier type match - add DPP carrier record")
|
2020-01-27 20:39:54 +01:00
|
|
|
if len(carrier.data) == 0 or carrier.data[0] != 0:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("URI Identifier Code 'None' not seen", color=C_RED)
|
2020-01-27 20:39:54 +01:00
|
|
|
continue
|
|
|
|
uri = carrier.data[1:].decode("utf-8")
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Received DPP URI: " + uri)
|
2020-01-27 20:39:54 +01:00
|
|
|
|
2020-07-23 23:30:38 +02:00
|
|
|
global test_uri, test_alt_uri
|
|
|
|
if test_uri:
|
|
|
|
summary("TEST MODE: Using specified URI")
|
|
|
|
data = test_sel_uri if test_sel_uri else test_uri
|
|
|
|
else:
|
|
|
|
data = wpas_get_nfc_uri(start_listen=False,
|
|
|
|
pick_channel=True)
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Own URI (pre-processing): %s" % data)
|
2020-01-27 20:39:54 +01:00
|
|
|
|
2020-07-23 23:30:38 +02:00
|
|
|
if test_uri:
|
|
|
|
summary("TEST MODE: Fake processing")
|
|
|
|
res = "OK"
|
2020-07-30 19:35:10 +02:00
|
|
|
data += " [%s]" % uri
|
2020-07-23 23:30:38 +02:00
|
|
|
else:
|
|
|
|
res = wpas_report_handover_req(uri)
|
2020-01-27 20:39:54 +01:00
|
|
|
if res is None or "FAIL" in res:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("DPP handover request processing failed",
|
|
|
|
color=C_RED)
|
2020-06-23 12:24:38 +02:00
|
|
|
global altchanlist
|
|
|
|
if altchanlist:
|
|
|
|
data = wpas_get_nfc_uri(start_listen=False,
|
|
|
|
chan_override=altchanlist)
|
|
|
|
summary("Own URI (try another channel list): %s" % data)
|
2020-01-27 20:39:54 +01:00
|
|
|
continue
|
|
|
|
|
2020-07-23 23:30:38 +02:00
|
|
|
if test_alt_uri:
|
|
|
|
summary("TEST MODE: Reject initial proposal")
|
|
|
|
continue
|
|
|
|
|
2020-01-27 20:39:54 +01:00
|
|
|
found = True
|
|
|
|
|
2020-07-23 23:30:38 +02:00
|
|
|
if not test_uri:
|
|
|
|
wpas = wpas_connect()
|
|
|
|
if wpas is None:
|
|
|
|
continue
|
|
|
|
global own_id
|
|
|
|
data = wpas.request("DPP_BOOTSTRAP_GET_URI %d" % own_id).rstrip()
|
|
|
|
if "FAIL" in data:
|
|
|
|
continue
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Own URI (post-processing): %s" % data)
|
2020-01-27 20:39:54 +01:00
|
|
|
uri = ndef.UriRecord(data)
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Own bootstrapping NFC URI record: " + str(uri))
|
2020-01-27 20:39:54 +01:00
|
|
|
|
2020-07-23 23:30:38 +02:00
|
|
|
if not test_uri:
|
|
|
|
info = wpas.request("DPP_BOOTSTRAP_INFO %d" % own_id)
|
|
|
|
freq = None
|
|
|
|
for line in info.splitlines():
|
|
|
|
if line.startswith("use_freq="):
|
|
|
|
freq = int(line.split('=')[1])
|
|
|
|
if freq is None or freq == 0:
|
|
|
|
summary("No channel negotiated over NFC - use channel 6")
|
|
|
|
freq = 2437
|
|
|
|
else:
|
|
|
|
summary("Negotiated channel: %d MHz" % freq)
|
|
|
|
if get_status_field(wpas, "bssid[0]"):
|
|
|
|
summary("Own AP freq: %s MHz" % str(get_status_field(wpas, "freq")))
|
|
|
|
if get_status_field(wpas, "beacon_set", extra="DRIVER") is None:
|
|
|
|
summary("Enable beaconing to have radio ready for RX")
|
|
|
|
wpas.request("DISABLE")
|
|
|
|
wpas.request("SET start_disabled 0")
|
|
|
|
wpas.request("ENABLE")
|
|
|
|
cmd = "DPP_LISTEN %d" % freq
|
|
|
|
global enrollee_only
|
|
|
|
global configurator_only
|
|
|
|
if enrollee_only:
|
|
|
|
cmd += " role=enrollee"
|
|
|
|
elif configurator_only:
|
|
|
|
cmd += " role=configurator"
|
|
|
|
summary(cmd)
|
|
|
|
res = wpas.request(cmd)
|
|
|
|
if "OK" not in res:
|
|
|
|
summary("Failed to start DPP listen", color=C_RED)
|
|
|
|
break
|
2020-01-27 20:39:54 +01:00
|
|
|
|
|
|
|
carrier = ndef.Record('application/vnd.wfa.dpp', 'A', uri.data)
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Own DPP carrier record: " + str(carrier))
|
2020-01-27 20:39:54 +01:00
|
|
|
hs.add_alternative_carrier('active', carrier.name)
|
|
|
|
sel = [hs, carrier]
|
|
|
|
break
|
|
|
|
|
2020-07-23 23:51:49 +02:00
|
|
|
global hs_sent, no_alt_proposal
|
2020-01-27 20:39:54 +01:00
|
|
|
summary("Sending handover select: " + str(sel))
|
2020-02-06 22:47:54 +01:00
|
|
|
if found:
|
2020-06-23 12:24:38 +02:00
|
|
|
summary("Handover completed successfully")
|
2020-02-06 22:47:54 +01:00
|
|
|
self.success = True
|
2020-06-24 21:37:52 +02:00
|
|
|
hs_sent = True
|
2020-07-23 23:51:49 +02:00
|
|
|
elif no_alt_proposal:
|
|
|
|
summary("Do not try alternative proposal anymore - handover failed",
|
|
|
|
color=C_RED)
|
|
|
|
hs_sent = True
|
2020-02-06 22:47:54 +01:00
|
|
|
else:
|
2020-06-23 12:24:38 +02:00
|
|
|
summary("Try to initiate with alternative parameters")
|
2020-02-06 22:47:54 +01:00
|
|
|
self.try_own = True
|
2020-06-24 21:37:52 +02:00
|
|
|
hs_sent = False
|
2020-07-23 23:51:49 +02:00
|
|
|
no_alt_proposal = True
|
2020-06-24 21:37:52 +02:00
|
|
|
threading.Thread(target=llcp_worker, args=(self.llc, True)).start()
|
2020-01-27 20:39:54 +01:00
|
|
|
return sel
|
|
|
|
|
|
|
|
def clear_raw_mode():
|
|
|
|
import sys, tty, termios
|
|
|
|
global prev_tcgetattr, in_raw_mode
|
|
|
|
if not in_raw_mode:
|
|
|
|
return
|
|
|
|
fd = sys.stdin.fileno()
|
|
|
|
termios.tcsetattr(fd, termios.TCSADRAIN, prev_tcgetattr)
|
|
|
|
in_raw_mode = False
|
|
|
|
|
|
|
|
def getch():
|
|
|
|
import sys, tty, termios, select
|
|
|
|
global prev_tcgetattr, in_raw_mode
|
|
|
|
fd = sys.stdin.fileno()
|
|
|
|
prev_tcgetattr = termios.tcgetattr(fd)
|
|
|
|
ch = None
|
|
|
|
try:
|
|
|
|
tty.setraw(fd)
|
|
|
|
in_raw_mode = True
|
|
|
|
[i, o, e] = select.select([fd], [], [], 0.05)
|
|
|
|
if i:
|
|
|
|
ch = sys.stdin.read(1)
|
|
|
|
finally:
|
|
|
|
termios.tcsetattr(fd, termios.TCSADRAIN, prev_tcgetattr)
|
|
|
|
in_raw_mode = False
|
|
|
|
return ch
|
|
|
|
|
|
|
|
def dpp_tag_read(tag):
|
|
|
|
success = False
|
|
|
|
for record in tag.ndef.records:
|
2020-05-15 11:03:53 +02:00
|
|
|
summary(record)
|
|
|
|
summary("record type " + record.type)
|
2020-01-27 20:39:54 +01:00
|
|
|
if record.type == "application/vnd.wfa.dpp":
|
|
|
|
summary("DPP HS tag - send to wpa_supplicant")
|
|
|
|
success = dpp_hs_tag_read(record)
|
|
|
|
break
|
|
|
|
if isinstance(record, ndef.UriRecord):
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("URI record: uri=" + record.uri)
|
|
|
|
summary("URI record: iri=" + record.iri)
|
2020-01-27 20:39:54 +01:00
|
|
|
if record.iri.startswith("DPP:"):
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("DPP URI")
|
2020-01-27 20:39:54 +01:00
|
|
|
if not dpp_nfc_uri_process(record.iri):
|
|
|
|
break
|
|
|
|
success = True
|
|
|
|
else:
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Ignore unknown URI")
|
2020-01-27 20:39:54 +01:00
|
|
|
break
|
|
|
|
|
|
|
|
if success:
|
|
|
|
success_report("Tag read succeeded")
|
|
|
|
|
|
|
|
return success
|
|
|
|
|
|
|
|
def rdwr_connected_write_tag(tag):
|
|
|
|
summary("Tag found - writing - " + str(tag))
|
2020-07-23 10:27:27 +02:00
|
|
|
if not tag.ndef:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Not a formatted NDEF tag", color=C_RED)
|
2020-07-23 10:27:27 +02:00
|
|
|
return
|
2020-01-27 20:39:54 +01:00
|
|
|
if not tag.ndef.is_writeable:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Not a writable tag", color=C_RED)
|
2020-01-27 20:39:54 +01:00
|
|
|
return
|
|
|
|
global dpp_tag_data
|
|
|
|
if tag.ndef.capacity < len(dpp_tag_data):
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Not enough room for the message")
|
2020-01-27 20:39:54 +01:00
|
|
|
return
|
2020-07-23 11:10:26 +02:00
|
|
|
try:
|
|
|
|
tag.ndef.records = dpp_tag_data
|
|
|
|
except ValueError as e:
|
|
|
|
summary("Writing the tag failed: %s" % str(e), color=C_RED)
|
|
|
|
return
|
2020-01-27 20:39:54 +01:00
|
|
|
success_report("Tag write succeeded")
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Tag writing completed - remove tag", color=C_GREEN)
|
2020-07-23 11:26:46 +02:00
|
|
|
global only_one, operation_success
|
|
|
|
operation_success = True
|
2020-01-27 20:39:54 +01:00
|
|
|
if only_one:
|
|
|
|
global continue_loop
|
|
|
|
continue_loop = False
|
|
|
|
global dpp_sel_wait_remove
|
|
|
|
return dpp_sel_wait_remove
|
|
|
|
|
|
|
|
def write_nfc_uri(clf, wait_remove=True):
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Write NFC URI record")
|
2020-01-27 20:39:54 +01:00
|
|
|
data = wpas_get_nfc_uri()
|
|
|
|
if data is None:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Could not get NFC URI from wpa_supplicant", color=C_RED)
|
2020-01-27 20:39:54 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
global dpp_sel_wait_remove
|
|
|
|
dpp_sel_wait_remove = wait_remove
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("URI: %s" % data)
|
2020-01-27 20:39:54 +01:00
|
|
|
uri = ndef.UriRecord(data)
|
2020-05-15 11:03:53 +02:00
|
|
|
summary(uri)
|
2020-01-27 20:39:54 +01:00
|
|
|
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Touch an NFC tag to write URI record", color=C_CYAN)
|
2020-01-27 20:39:54 +01:00
|
|
|
global dpp_tag_data
|
|
|
|
dpp_tag_data = [uri]
|
|
|
|
clf.connect(rdwr={'on-connect': rdwr_connected_write_tag})
|
|
|
|
|
|
|
|
def write_nfc_hs(clf, wait_remove=True):
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Write NFC Handover Select record on a tag")
|
2020-01-27 20:39:54 +01:00
|
|
|
data = wpas_get_nfc_uri()
|
|
|
|
if data is None:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Could not get NFC URI from wpa_supplicant", color=C_RED)
|
2020-01-27 20:39:54 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
global dpp_sel_wait_remove
|
|
|
|
dpp_sel_wait_remove = wait_remove
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("URI: %s" % data)
|
2020-01-27 20:39:54 +01:00
|
|
|
uri = ndef.UriRecord(data)
|
2020-05-15 11:03:53 +02:00
|
|
|
summary(uri)
|
2020-01-27 20:39:54 +01:00
|
|
|
carrier = ndef.Record('application/vnd.wfa.dpp', 'A', uri.data)
|
|
|
|
hs = ndef.HandoverSelectRecord('1.4')
|
|
|
|
hs.add_alternative_carrier('active', carrier.name)
|
2020-05-15 11:03:53 +02:00
|
|
|
summary(hs)
|
|
|
|
summary(carrier)
|
2020-01-27 20:39:54 +01:00
|
|
|
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Touch an NFC tag to write HS record", color=C_CYAN)
|
2020-01-27 20:39:54 +01:00
|
|
|
global dpp_tag_data
|
|
|
|
dpp_tag_data = [hs, carrier]
|
2020-05-15 11:03:53 +02:00
|
|
|
summary(dpp_tag_data)
|
2020-01-27 20:39:54 +01:00
|
|
|
clf.connect(rdwr={'on-connect': rdwr_connected_write_tag})
|
|
|
|
|
|
|
|
def rdwr_connected(tag):
|
|
|
|
global only_one, no_wait
|
|
|
|
summary("Tag connected: " + str(tag))
|
|
|
|
|
|
|
|
if tag.ndef:
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("NDEF tag: " + tag.type)
|
|
|
|
summary(tag.ndef.records)
|
2020-01-27 20:39:54 +01:00
|
|
|
success = dpp_tag_read(tag)
|
|
|
|
if only_one and success:
|
|
|
|
global continue_loop
|
|
|
|
continue_loop = False
|
|
|
|
else:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Not an NDEF tag - remove tag", color=C_RED)
|
2020-01-27 20:39:54 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
return not no_wait
|
|
|
|
|
2020-06-24 21:37:52 +02:00
|
|
|
def llcp_worker(llc, try_alt):
|
|
|
|
print("Start of llcp_worker()")
|
|
|
|
if try_alt:
|
|
|
|
summary("Starting handover client (try_alt)")
|
|
|
|
dpp_handover_client(llc, alt=True)
|
|
|
|
summary("Exiting llcp_worker thread (try_alt)")
|
|
|
|
return
|
2020-01-27 20:39:54 +01:00
|
|
|
global init_on_touch
|
|
|
|
if init_on_touch:
|
2020-06-24 21:37:52 +02:00
|
|
|
summary("Starting handover client (init_on_touch)")
|
2020-01-27 20:39:54 +01:00
|
|
|
dpp_handover_client(llc)
|
2020-06-24 21:37:52 +02:00
|
|
|
summary("Exiting llcp_worker thread (init_on_touch)")
|
2020-01-27 20:39:54 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
global no_input
|
|
|
|
if no_input:
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Wait for handover to complete")
|
2020-01-27 20:39:54 +01:00
|
|
|
else:
|
|
|
|
print("Wait for handover to complete - press 'i' to initiate")
|
|
|
|
global srv
|
|
|
|
global wait_connection
|
|
|
|
while not wait_connection and srv.sent_carrier is None:
|
2020-02-06 22:47:54 +01:00
|
|
|
if srv.try_own:
|
|
|
|
srv.try_own = False
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Try to initiate another handover with own parameters")
|
2020-06-23 12:24:38 +02:00
|
|
|
global peer_crn, my_crn, my_crn_ready, hs_sent
|
|
|
|
my_crn_ready = False
|
|
|
|
my_crn = None
|
|
|
|
peer_crn = None
|
|
|
|
hs_sent = False
|
|
|
|
dpp_handover_client(llc, alt=True)
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Exiting llcp_worker thread (retry with own parameters)")
|
2020-02-06 22:47:54 +01:00
|
|
|
return
|
2020-01-27 20:39:54 +01:00
|
|
|
if srv.ho_server_processing:
|
|
|
|
time.sleep(0.025)
|
|
|
|
elif no_input:
|
|
|
|
time.sleep(0.5)
|
|
|
|
else:
|
|
|
|
res = getch()
|
|
|
|
if res != 'i':
|
|
|
|
continue
|
|
|
|
clear_raw_mode()
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Starting handover client")
|
2020-01-27 20:39:54 +01:00
|
|
|
dpp_handover_client(llc)
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Exiting llcp_worker thread (manual init)")
|
2020-01-27 20:39:54 +01:00
|
|
|
return
|
|
|
|
|
2020-05-15 11:03:53 +02:00
|
|
|
global in_raw_mode
|
|
|
|
was_in_raw_mode = in_raw_mode
|
2020-01-27 20:39:54 +01:00
|
|
|
clear_raw_mode()
|
2020-05-15 11:03:53 +02:00
|
|
|
if was_in_raw_mode:
|
|
|
|
print("\r")
|
|
|
|
summary("Exiting llcp_worker thread")
|
2020-01-27 20:39:54 +01:00
|
|
|
|
|
|
|
def llcp_startup(llc):
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Start LLCP server")
|
2020-01-27 20:39:54 +01:00
|
|
|
global srv
|
|
|
|
srv = HandoverServer(llc)
|
|
|
|
return llc
|
|
|
|
|
|
|
|
def llcp_connected(llc):
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("P2P LLCP connected")
|
2020-05-15 13:20:26 +02:00
|
|
|
global wait_connection, my_crn, peer_crn, my_crn_ready, hs_sent
|
2020-07-30 16:09:56 +02:00
|
|
|
global no_alt_proposal, alt_proposal_used, need_client, i_m_selector
|
2020-01-27 20:39:54 +01:00
|
|
|
wait_connection = False
|
2020-05-15 11:10:59 +02:00
|
|
|
my_crn_ready = False
|
2020-05-15 01:26:01 +02:00
|
|
|
my_crn = None
|
|
|
|
peer_crn = None
|
2020-05-15 13:20:26 +02:00
|
|
|
hs_sent = False
|
2020-07-23 23:51:49 +02:00
|
|
|
no_alt_proposal = False
|
2020-07-24 11:46:20 +02:00
|
|
|
alt_proposal_used = False
|
2020-07-30 16:09:56 +02:00
|
|
|
need_client = False
|
|
|
|
i_m_selector = False
|
2020-05-15 00:21:01 +02:00
|
|
|
global srv
|
|
|
|
srv.start()
|
2020-01-27 20:39:54 +01:00
|
|
|
if init_on_touch or not no_input:
|
2020-06-24 21:37:52 +02:00
|
|
|
threading.Thread(target=llcp_worker, args=(llc, False)).start()
|
2020-01-27 20:39:54 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
def llcp_release(llc):
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("LLCP release")
|
2020-07-30 16:09:56 +02:00
|
|
|
global hs_client
|
|
|
|
if hs_client:
|
|
|
|
hs_client.close()
|
|
|
|
hs_client = None
|
2020-01-27 20:39:54 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
def terminate_loop():
|
|
|
|
global terminate_now
|
|
|
|
return terminate_now
|
|
|
|
|
|
|
|
def main():
|
|
|
|
clf = nfc.ContactlessFrontend()
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='nfcpy to wpa_supplicant integration for DPP NFC operations')
|
|
|
|
parser.add_argument('-d', const=logging.DEBUG, default=logging.INFO,
|
|
|
|
action='store_const', dest='loglevel',
|
|
|
|
help='verbose debug output')
|
|
|
|
parser.add_argument('-q', const=logging.WARNING, action='store_const',
|
|
|
|
dest='loglevel', help='be quiet')
|
|
|
|
parser.add_argument('--only-one', '-1', action='store_true',
|
|
|
|
help='run only one operation and exit')
|
|
|
|
parser.add_argument('--init-on-touch', '-I', action='store_true',
|
|
|
|
help='initiate handover on touch')
|
|
|
|
parser.add_argument('--no-wait', action='store_true',
|
|
|
|
help='do not wait for tag to be removed before exiting')
|
|
|
|
parser.add_argument('--ifname', '-i',
|
|
|
|
help='network interface name')
|
|
|
|
parser.add_argument('--no-input', '-a', action='store_true',
|
|
|
|
help='do not use stdout input to initiate handover')
|
|
|
|
parser.add_argument('--tag-read-only', '-t', action='store_true',
|
|
|
|
help='tag read only (do not allow connection handover)')
|
|
|
|
parser.add_argument('--handover-only', action='store_true',
|
|
|
|
help='connection handover only (do not allow tag read)')
|
2020-05-11 23:57:44 +02:00
|
|
|
parser.add_argument('--enrollee', action='store_true',
|
|
|
|
help='run as Enrollee-only')
|
|
|
|
parser.add_argument('--configurator', action='store_true',
|
|
|
|
help='run as Configurator-only')
|
|
|
|
parser.add_argument('--config-params', default='',
|
|
|
|
help='configurator parameters')
|
2020-05-14 20:52:09 +02:00
|
|
|
parser.add_argument('--ctrl', default='/var/run/wpa_supplicant',
|
|
|
|
help='wpa_supplicant/hostapd control interface')
|
2020-01-27 20:39:54 +01:00
|
|
|
parser.add_argument('--summary',
|
|
|
|
help='summary file for writing status updates')
|
|
|
|
parser.add_argument('--success',
|
|
|
|
help='success file for writing success update')
|
|
|
|
parser.add_argument('--device', default='usb', help='NFC device to open')
|
2020-05-15 13:17:40 +02:00
|
|
|
parser.add_argument('--chan', default=None, help='channel list')
|
2020-06-23 12:24:38 +02:00
|
|
|
parser.add_argument('--altchan', default=None, help='alternative channel list')
|
2020-07-23 00:30:30 +02:00
|
|
|
parser.add_argument('--netrole', default=None, help='netrole for Enrollee')
|
2020-07-23 23:30:38 +02:00
|
|
|
parser.add_argument('--test-uri', default=None,
|
|
|
|
help='test mode: initial URI')
|
|
|
|
parser.add_argument('--test-alt-uri', default=None,
|
|
|
|
help='test mode: alternative URI')
|
|
|
|
parser.add_argument('--test-sel-uri', default=None,
|
|
|
|
help='test mode: handover select URI')
|
|
|
|
parser.add_argument('--test-crn', default=None,
|
|
|
|
help='test mode: hardcoded crn')
|
2020-01-27 20:39:54 +01:00
|
|
|
parser.add_argument('command', choices=['write-nfc-uri',
|
|
|
|
'write-nfc-hs'],
|
|
|
|
nargs='?')
|
|
|
|
args = parser.parse_args()
|
2020-05-15 11:03:53 +02:00
|
|
|
summary(args)
|
2020-01-27 20:39:54 +01:00
|
|
|
|
|
|
|
global only_one
|
|
|
|
only_one = args.only_one
|
|
|
|
|
|
|
|
global no_wait
|
|
|
|
no_wait = args.no_wait
|
|
|
|
|
2020-07-23 23:30:38 +02:00
|
|
|
global chanlist, altchanlist, netrole, test_uri, test_alt_uri, test_sel_uri
|
|
|
|
global test_crn
|
2020-02-06 22:22:39 +01:00
|
|
|
chanlist = args.chan
|
2020-06-23 12:24:38 +02:00
|
|
|
altchanlist = args.altchan
|
2020-07-23 00:30:30 +02:00
|
|
|
netrole = args.netrole
|
2020-07-23 23:30:38 +02:00
|
|
|
test_uri = args.test_uri
|
|
|
|
test_alt_uri = args.test_alt_uri
|
|
|
|
test_sel_uri = args.test_sel_uri
|
|
|
|
if args.test_crn:
|
|
|
|
test_crn = struct.pack('>H', int(args.test_crn))
|
|
|
|
else:
|
|
|
|
test_crn = None
|
2020-02-06 22:22:39 +01:00
|
|
|
|
2020-01-27 20:39:54 +01:00
|
|
|
logging.basicConfig(level=args.loglevel)
|
2020-07-30 15:49:40 +02:00
|
|
|
for l in ['nfc.clf.rcs380',
|
|
|
|
'nfc.clf.transport',
|
|
|
|
'nfc.clf.device',
|
|
|
|
'nfc.clf.__init__',
|
|
|
|
'nfc.llcp',
|
|
|
|
'nfc.handover']:
|
|
|
|
log = logging.getLogger(l)
|
|
|
|
log.setLevel(args.loglevel)
|
2020-01-27 20:39:54 +01:00
|
|
|
|
|
|
|
global init_on_touch
|
|
|
|
init_on_touch = args.init_on_touch
|
|
|
|
|
2020-05-11 23:57:44 +02:00
|
|
|
global enrollee_only
|
|
|
|
enrollee_only = args.enrollee
|
|
|
|
|
|
|
|
global configurator_only
|
|
|
|
configurator_only = args.configurator
|
|
|
|
|
|
|
|
global config_params
|
|
|
|
config_params = args.config_params
|
|
|
|
|
2020-01-27 20:39:54 +01:00
|
|
|
if args.ifname:
|
|
|
|
global ifname
|
|
|
|
ifname = args.ifname
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("Selected ifname " + ifname)
|
2020-01-27 20:39:54 +01:00
|
|
|
|
2020-05-14 20:52:09 +02:00
|
|
|
if args.ctrl:
|
|
|
|
global wpas_ctrl
|
|
|
|
wpas_ctrl = args.ctrl
|
|
|
|
|
2020-01-27 20:39:54 +01:00
|
|
|
if args.summary:
|
|
|
|
global summary_file
|
|
|
|
summary_file = args.summary
|
|
|
|
|
|
|
|
if args.success:
|
|
|
|
global success_file
|
|
|
|
success_file = args.success
|
|
|
|
|
|
|
|
if args.no_input:
|
|
|
|
global no_input
|
|
|
|
no_input = True
|
|
|
|
|
|
|
|
clf = nfc.ContactlessFrontend()
|
|
|
|
global wait_connection
|
|
|
|
|
|
|
|
try:
|
|
|
|
if not clf.open(args.device):
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Could not open connection with an NFC device", color=C_RED)
|
2020-07-23 11:26:46 +02:00
|
|
|
raise SystemExit(1)
|
2020-01-27 20:39:54 +01:00
|
|
|
|
|
|
|
if args.command == "write-nfc-uri":
|
|
|
|
write_nfc_uri(clf, wait_remove=not args.no_wait)
|
2020-07-23 11:26:46 +02:00
|
|
|
if not operation_success:
|
|
|
|
raise SystemExit(1)
|
2020-01-27 20:39:54 +01:00
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
if args.command == "write-nfc-hs":
|
|
|
|
write_nfc_hs(clf, wait_remove=not args.no_wait)
|
2020-07-23 11:26:46 +02:00
|
|
|
if not operation_success:
|
|
|
|
raise SystemExit(1)
|
2020-01-27 20:39:54 +01:00
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
global continue_loop
|
|
|
|
while continue_loop:
|
2020-05-15 11:03:53 +02:00
|
|
|
global in_raw_mode
|
|
|
|
was_in_raw_mode = in_raw_mode
|
2020-01-27 20:39:54 +01:00
|
|
|
clear_raw_mode()
|
2020-05-15 11:03:53 +02:00
|
|
|
if was_in_raw_mode:
|
|
|
|
print("\r")
|
2020-07-23 11:10:26 +02:00
|
|
|
if args.handover_only:
|
|
|
|
summary("Waiting a peer to be touched", color=C_MAGENTA)
|
|
|
|
elif args.tag_read_only:
|
|
|
|
summary("Waiting for a tag to be touched", color=C_BLUE)
|
2020-06-22 23:57:18 +02:00
|
|
|
else:
|
2020-07-23 11:10:26 +02:00
|
|
|
summary("Waiting for a tag or peer to be touched",
|
|
|
|
color=C_GREEN)
|
2020-01-27 20:39:54 +01:00
|
|
|
wait_connection = True
|
|
|
|
try:
|
|
|
|
if args.tag_read_only:
|
|
|
|
if not clf.connect(rdwr={'on-connect': rdwr_connected}):
|
|
|
|
break
|
|
|
|
elif args.handover_only:
|
|
|
|
if not clf.connect(llcp={'on-startup': llcp_startup,
|
|
|
|
'on-connect': llcp_connected,
|
|
|
|
'on-release': llcp_release},
|
|
|
|
terminate=terminate_loop):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if not clf.connect(rdwr={'on-connect': rdwr_connected},
|
|
|
|
llcp={'on-startup': llcp_startup,
|
|
|
|
'on-connect': llcp_connected,
|
|
|
|
'on-release': llcp_release},
|
|
|
|
terminate=terminate_loop):
|
|
|
|
break
|
|
|
|
except Exception as e:
|
2020-05-15 11:03:53 +02:00
|
|
|
summary("clf.connect failed: " + str(e))
|
2020-01-27 20:39:54 +01:00
|
|
|
break
|
|
|
|
|
|
|
|
global srv
|
|
|
|
if only_one and srv and srv.success:
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise SystemExit
|
|
|
|
finally:
|
|
|
|
clf.close()
|
|
|
|
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|