From d4dc3014bb2b70bee8504ed4b1a8348acef7da74 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 1 May 2005 14:54:11 +0000 Subject: Support storing and deleting of stored link keys --- tools/Makefile.am | 2 +- tools/hciconfig.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/tools/Makefile.am b/tools/Makefile.am index 1743771b..e9574f3e 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -25,7 +25,7 @@ bin_PROGRAMS = hcitool l2ping sdptool ciptool $(dfutool_programs) noinst_PROGRAMS = hcisecfilter ppporc pskey hciconfig_SOURCES = hciconfig.c csr.h csr.c -hciconfig_LDADD = @BLUEZ_LIBS@ +hciconfig_LDADD = @BLUEZ_LIBS@ $(top_builddir)/common/libtextfile.a hcitool_SOURCES = hcitool.c oui.h oui.c hcitool_LDADD = @BLUEZ_LIBS@ $(top_builddir)/common/libtextfile.a diff --git a/tools/hciconfig.c b/tools/hciconfig.c index df9b211e..5f72ce66 100644 --- a/tools/hciconfig.c +++ b/tools/hciconfig.c @@ -45,6 +45,7 @@ #include #include +#include "textfile.h" #include "csr.h" static struct hci_dev_info di; @@ -689,6 +690,101 @@ static void cmd_voice(int ctl, int hdev, char *opt) } } +static int get_link_key(const bdaddr_t *local, const bdaddr_t *peer, uint8_t *key) +{ + char filename[PATH_MAX + 1], addr[18], tmp[3], *str; + int i; + + ba2str(local, addr); + snprintf(filename, PATH_MAX, "%s/%s/linkkeys", STORAGEDIR, addr); + + ba2str(peer, addr); + str = textfile_get(filename, addr); + if (!str) + return -EIO; + + memset(tmp, 0, sizeof(tmp)); + for (i = 0; i < 16; i++) { + memcpy(tmp, str + (i * 2), 2); + key[i] = (uint8_t) strtol(tmp, NULL, 16); + } + + free(str); + + return 0; +} + +static void cmd_putkey(int ctl, int hdev, char *opt) +{ + struct hci_dev_info di; + bdaddr_t bdaddr; + uint8_t key[16]; + int dd; + + if (!opt) + return; + + dd = hci_open_dev(hdev); + if (dd < 0) { + fprintf(stderr, "Can't open device hci%d: %s (%d)\n", + hdev, strerror(errno), errno); + exit(1); + } + + if (hci_devinfo(hdev, &di) < 0) { + fprintf(stderr, "Can't get device info for hci%d: %s (%d)\n", + hdev, strerror(errno), errno); + exit(1); + } + + str2ba(opt, &bdaddr); + if (get_link_key(&di.bdaddr, &bdaddr, key) < 0) { + fprintf(stderr, "Can't find link key for %s on hci%d\n", opt, hdev); + exit(1); + } + + if (hci_write_stored_link_key(dd, &bdaddr, key, 1000) < 0) { + fprintf(stderr, "Can't write stored link key on hci%d: %s (%d)\n", + hdev, strerror(errno), errno); + exit(1); + } + + hci_close_dev(dd); +} + +static void cmd_delkey(int ctl, int hdev, char *opt) +{ + bdaddr_t bdaddr; + uint8_t all; + int dd; + + if (!opt) + return; + + dd = hci_open_dev(hdev); + if (dd < 0) { + fprintf(stderr, "Can't open device hci%d: %s (%d)\n", + hdev, strerror(errno), errno); + exit(1); + } + + if (!strcasecmp(opt, "all")) { + bacpy(&bdaddr, BDADDR_ANY); + all = 1; + } else { + str2ba(opt, &bdaddr); + all = 0; + } + + if (hci_delete_stored_link_key(dd, &bdaddr, all, 1000) < 0) { + fprintf(stderr, "Can't delete stored link key on hci%d: %s (%d)\n", + hdev, strerror(errno), errno); + exit(1); + } + + hci_close_dev(dd); +} + static void cmd_commands(int ctl, int hdev, char *opt) { uint8_t cmds[64]; @@ -702,7 +798,7 @@ static void cmd_commands(int ctl, int hdev, char *opt) } if (hci_read_local_commands(dd, cmds, 1000) < 0) { - fprintf(stderr, "Can't read support commands hci%d: %s (%d)\n", + fprintf(stderr, "Can't read support commands on hci%d: %s (%d)\n", hdev, strerror(errno), errno); exit(1); } @@ -1235,6 +1331,8 @@ static struct { { "afhmode", cmd_afh_mode, "[mode]", "Get/Set AFH mode" }, { "aclmtu", cmd_aclmtu, "", "Set ACL MTU and number of packets" }, { "scomtu", cmd_scomtu, "", "Set SCO MTU and number of packets" }, + { "putkey", cmd_putkey, "", "Store link key on the device" }, + { "delkey", cmd_delkey, "", "Delete link key from the device" }, { "commands", cmd_commands, 0, "Display supported commands" }, { "features", cmd_features, 0, "Display device features" }, { "version", cmd_version, 0, "Display version information" }, -- cgit