blob: 26858043c2c3371fbff1ce2b9ad940b6a67a930e [file] [log] [blame]
developer66e89bc2024-04-23 14:50:01 +08001From cc660ccb9a83ec23b672eef178e9b494d95e763d Mon Sep 17 00:00:00 2001
2From: =?UTF-8?q?Rapha=C3=ABl=20M=C3=A9lotte?= <raphael.melotte@mind.be>
3Date: Thu, 12 Jan 2023 13:25:25 +0100
4Subject: [PATCH 04/28] iw: add support for retrieving keys
5MIME-Version: 1.0
6Content-Type: text/plain; charset=UTF-8
7Content-Transfer-Encoding: 8bit
8
9For debugging purposes, it can be useful to be able to retrieve keys.
10
11Add a "iw key get" command, to be able to retrieve keys when the key
12index is known. A new "key" section is also introduced, in preparation
13for future key-related commands.
14
15Example retrieving a pairwise key:
16iw dev wlan0 key get 0 02:02:03:04:05:06
17
18Example retrieving a group key:
19iw dev wlan0 key get 1
20
21Note that only the outer ATTR_KEY_DATA (and seq) is reported, the
22nested KEY_DATA (and seq) within ATTR_KEY is not.
23
24Signed-off-by: Raphaël Mélotte <raphael.melotte@mind.be>
25Link: https://lore.kernel.org/r/20230112122525.2257298-1-raphael.melotte@mind.be
26Signed-off-by: Johannes Berg <johannes.berg@intel.com>
27---
28 keys.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29 1 file changed, 83 insertions(+)
30 create mode 100644 keys.c
31
32diff --git a/keys.c b/keys.c
33new file mode 100644
34index 0000000..65aa426
35--- /dev/null
36+++ b/keys.c
37@@ -0,0 +1,83 @@
38+#include <errno.h>
39+#include <netlink/genl/genl.h>
40+#include <netlink/genl/family.h>
41+#include <netlink/genl/ctrl.h>
42+#include <netlink/msg.h>
43+#include <netlink/attr.h>
44+#include "nl80211.h"
45+#include "iw.h"
46+
47+SECTION(key);
48+
49+static int print_keys(struct nl_msg *msg, void *arg)
50+{
51+ struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
52+ struct nlattr *tb[NL80211_ATTR_MAX + 1];
53+
54+ nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
55+ genlmsg_attrlen(gnlh, 0), NULL);
56+
57+ if (!tb[NL80211_ATTR_KEY_IDX]) {
58+ fprintf(stderr, "KEY_IDX missing!\n");
59+ return NL_SKIP;
60+ }
61+
62+ if (!tb[NL80211_ATTR_KEY_DATA]) {
63+ fprintf(stderr, "ATTR_KEY_DATA missing!\n");
64+ return NL_SKIP;
65+ }
66+
67+ iw_hexdump("Key", nla_data(tb[NL80211_ATTR_KEY_DATA]),
68+ nla_len(tb[NL80211_ATTR_KEY_DATA]));
69+
70+ if (!tb[NL80211_ATTR_KEY_SEQ]) {
71+ fprintf(stderr, "ATTR_KEY_SEQ missing!\n");
72+ return NL_SKIP;
73+ }
74+
75+ iw_hexdump("Key seq", nla_data(tb[NL80211_ATTR_KEY_SEQ]),
76+ nla_len(tb[NL80211_ATTR_KEY_SEQ]));
77+
78+ return NL_OK;
79+}
80+
81+static int handle_get_key(struct nl80211_state *state,
82+ struct nl_msg *msg, int argc, char **argv,
83+ enum id_input id)
84+{
85+ char *end;
86+ unsigned char mac[6];
87+
88+ /* key index */
89+ if (argc) {
90+ nla_put_u8(msg, NL80211_ATTR_KEY_IDX, strtoul(argv[0], &end, 10));
91+ if (*end != '\0')
92+ return -EINVAL;
93+ argv++;
94+ argc--;
95+ }
96+
97+ /* mac */
98+ if (argc) {
99+ if (mac_addr_a2n(mac, argv[0]) == 0) {
100+ NLA_PUT(msg, NL80211_ATTR_MAC, 6, mac);
101+ nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
102+ NL80211_KEYTYPE_PAIRWISE);
103+ argv++;
104+ argc--;
105+ } else {
106+ return -EINVAL;
107+ }
108+ } else {
109+ nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, NL80211_KEYTYPE_GROUP);
110+ }
111+
112+ register_handler(print_keys, NULL);
113+ return 0;
114+
115+ nla_put_failure:
116+ return -ENOSPC;
117+}
118+COMMAND(key, get, "<key index> <MAC address>",
119+ NL80211_CMD_GET_KEY, 0, CIB_NETDEV, handle_get_key,
120+ "Retrieve a key and key sequence.\n");
121--
1222.39.2
123