]> git.ipfire.org Git - thirdparty/iw.git/blame - station.c
0.9.16
[thirdparty/iw.git] / station.c
CommitLineData
2ef1be68 1#include <net/if.h>
3d1e8704 2#include <errno.h>
d5ac8ad3 3#include <string.h>
2ef1be68 4
3d1e8704
LCC
5#include <netlink/genl/genl.h>
6#include <netlink/genl/family.h>
7#include <netlink/genl/ctrl.h>
8#include <netlink/msg.h>
9#include <netlink/attr.h>
3d1e8704 10
f408e01b 11#include "nl80211.h"
3d1e8704
LCC
12#include "iw.h"
13
14enum plink_state {
15 LISTEN,
16 OPN_SNT,
17 OPN_RCVD,
18 CNF_RCVD,
19 ESTAB,
20 HOLDING,
21 BLOCKED
22};
23
24enum plink_actions {
25 PLINK_ACTION_UNDEFINED,
26 PLINK_ACTION_OPEN,
27 PLINK_ACTION_BLOCK,
28};
29
30
3d1e8704
LCC
31static int print_sta_handler(struct nl_msg *msg, void *arg)
32{
33 struct nlattr *tb[NL80211_ATTR_MAX + 1];
34 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
35 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
9cd3f1c1 36 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
3d1e8704
LCC
37 char mac_addr[20], state_name[10], dev[20];
38 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
39 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
40 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
41 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
859677cb
JB
42 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
43 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
9cd3f1c1
HR
44 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
45 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
3d1e8704
LCC
46 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
47 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
48 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
49 };
50
9cd3f1c1
HR
51 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
52 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
53 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
54 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
55 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
56 };
57
3d1e8704
LCC
58 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
59 genlmsg_attrlen(gnlh, 0), NULL);
60
61 /*
62 * TODO: validate the interface and mac address!
63 * Otherwise, there's a race condition as soon as
64 * the kernel starts sending station notifications.
65 */
66
67 if (!tb[NL80211_ATTR_STA_INFO]) {
68 fprintf(stderr, "sta stats missing!");
69 return NL_SKIP;
70 }
71 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
72 tb[NL80211_ATTR_STA_INFO],
73 stats_policy)) {
74 fprintf(stderr, "failed to parse nested attributes!");
75 return NL_SKIP;
76 }
77
78 mac_addr_n2a(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
79 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
fbb181fa 80 printf("Station %s (on %s)", mac_addr, dev);
3d1e8704
LCC
81
82 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
fbb181fa 83 printf("\n\tinactive time:\t%d ms",
3d1e8704
LCC
84 nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]));
85 if (sinfo[NL80211_STA_INFO_RX_BYTES])
fbb181fa 86 printf("\n\trx bytes:\t%d",
3d1e8704 87 nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]));
859677cb
JB
88 if (sinfo[NL80211_STA_INFO_RX_PACKETS])
89 printf("\n\trx packets:\t%d",
90 nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]));
3d1e8704 91 if (sinfo[NL80211_STA_INFO_TX_BYTES])
fbb181fa 92 printf("\n\ttx bytes:\t%d",
3d1e8704 93 nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]));
859677cb
JB
94 if (sinfo[NL80211_STA_INFO_TX_PACKETS])
95 printf("\n\ttx packets:\t%d",
96 nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]));
9cd3f1c1
HR
97 if (sinfo[NL80211_STA_INFO_SIGNAL])
98 printf("\n\tsignal: \t%d dBm",
99 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]));
100
101 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
102 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
103 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) {
104 fprintf(stderr, "failed to parse nested rate attributes!");
105 } else {
106 printf("\n\ttx bitrate:\t");
107 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
108 int rate = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
109 printf("%d.%d MBit/s", rate / 10, rate % 10);
110 }
111
112 if (rinfo[NL80211_RATE_INFO_MCS])
113 printf(" MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]));
114 if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
115 printf(" 40Mhz");
116 if (rinfo[NL80211_RATE_INFO_SHORT_GI])
117 printf(" short GI");
118 }
119 }
120
3d1e8704 121 if (sinfo[NL80211_STA_INFO_LLID])
fbb181fa 122 printf("\n\tmesh llid:\t%d",
3d1e8704
LCC
123 nla_get_u16(sinfo[NL80211_STA_INFO_LLID]));
124 if (sinfo[NL80211_STA_INFO_PLID])
fbb181fa 125 printf("\n\tmesh plid:\t%d",
3d1e8704
LCC
126 nla_get_u16(sinfo[NL80211_STA_INFO_PLID]));
127 if (sinfo[NL80211_STA_INFO_PLINK_STATE]) {
128 switch (nla_get_u16(sinfo[NL80211_STA_INFO_PLINK_STATE])) {
129 case LISTEN:
130 strcpy(state_name, "LISTEN");
131 break;
132 case OPN_SNT:
133 strcpy(state_name, "OPN_SNT");
134 break;
135 case OPN_RCVD:
136 strcpy(state_name, "OPN_RCVD");
137 break;
138 case CNF_RCVD:
139 strcpy(state_name, "CNF_RCVD");
140 break;
141 case ESTAB:
142 strcpy(state_name, "ESTAB");
143 break;
144 case HOLDING:
145 strcpy(state_name, "HOLDING");
146 break;
147 case BLOCKED:
148 strcpy(state_name, "BLOCKED");
149 break;
150 default:
151 strcpy(state_name, "UNKNOWN");
152 break;
153 }
fbb181fa 154 printf("\n\tmesh plink:\t%s", state_name);
3d1e8704
LCC
155 }
156
157 printf("\n");
158 return NL_SKIP;
159}
160
7c37a24d
JB
161static int handle_station_get(struct nl80211_state *state,
162 struct nl_cb *cb,
b1ca19a8
JB
163 struct nl_msg *msg,
164 int argc, char **argv)
3d1e8704 165{
3d1e8704
LCC
166 unsigned char mac_addr[ETH_ALEN];
167
b1ca19a8 168 if (argc < 1)
5e75fd04 169 return 1;
3d1e8704
LCC
170
171 if (mac_addr_a2n(mac_addr, argv[0])) {
172 fprintf(stderr, "invalid mac address\n");
5e75fd04 173 return 2;
3d1e8704
LCC
174 }
175
176 argc--;
177 argv++;
178
b1ca19a8 179 if (argc)
5e75fd04 180 return 1;
3d1e8704
LCC
181
182 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
3d1e8704 183
3d1e8704 184 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
3d1e8704 185
70391ccf 186 return 0;
3d1e8704 187 nla_put_failure:
70391ccf 188 return -ENOBUFS;
3d1e8704 189}
b1ca19a8 190COMMAND(station, get, "<MAC address>",
70cf4544
JB
191 NL80211_CMD_GET_STATION, 0, CIB_NETDEV, handle_station_get,
192 "Get information for a specific station.");
b1ca19a8 193COMMAND(station, del, "<MAC address>",
70cf4544
JB
194 NL80211_CMD_DEL_STATION, 0, CIB_NETDEV, handle_station_get,
195 "Remove the given station entry (use with caution!)");
3d1e8704 196
7c37a24d
JB
197static int handle_station_set(struct nl80211_state *state,
198 struct nl_cb *cb,
b1ca19a8
JB
199 struct nl_msg *msg,
200 int argc, char **argv)
3d1e8704 201{
3d1e8704
LCC
202 unsigned char plink_action;
203 unsigned char mac_addr[ETH_ALEN];
204
b1ca19a8 205 if (argc < 3)
5e75fd04 206 return 1;
3d1e8704
LCC
207
208 if (mac_addr_a2n(mac_addr, argv[0])) {
209 fprintf(stderr, "invalid mac address\n");
5e75fd04 210 return 2;
3d1e8704
LCC
211 }
212 argc--;
213 argv++;
214
b1ca19a8
JB
215 if (strcmp("plink_action", argv[0]) != 0)
216 return 1;
3d1e8704
LCC
217 argc--;
218 argv++;
219
220 if (strcmp("open", argv[0]) == 0)
221 plink_action = PLINK_ACTION_OPEN;
222 else if (strcmp("block", argv[0]) == 0)
223 plink_action = PLINK_ACTION_BLOCK;
224 else {
225 fprintf(stderr, "plink action not supported\n");
5e75fd04 226 return 2;
3d1e8704
LCC
227 }
228 argc--;
229 argv++;
230
b1ca19a8 231 if (argc)
5e75fd04 232 return 1;
3d1e8704
LCC
233
234 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
3d1e8704
LCC
235 NLA_PUT_U8(msg, NL80211_ATTR_STA_PLINK_ACTION, plink_action);
236
70391ccf 237 return 0;
3d1e8704 238 nla_put_failure:
70391ccf 239 return -ENOBUFS;
3d1e8704 240}
b1ca19a8 241COMMAND(station, set, "<MAC address> plink_action <open|block>",
70cf4544
JB
242 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set,
243 "Set mesh peer link action for this station (peer).");
b1ca19a8 244
7c37a24d
JB
245static int handle_station_dump(struct nl80211_state *state,
246 struct nl_cb *cb,
b1ca19a8
JB
247 struct nl_msg *msg,
248 int argc, char **argv)
3d1e8704 249{
3d1e8704 250 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
70391ccf 251 return 0;
3d1e8704 252}
b1ca19a8 253COMMAND(station, dump, NULL,
70cf4544
JB
254 NL80211_CMD_GET_STATION, NLM_F_DUMP, CIB_NETDEV, handle_station_dump,
255 "List all stations known, e.g. the AP on managed interfaces");