]> git.ipfire.org Git - thirdparty/iw.git/blame - station.c
iw: display DFS regulatory domain
[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
4698bfc2
JB
14SECTION(station);
15
3d1e8704
LCC
16enum plink_state {
17 LISTEN,
18 OPN_SNT,
19 OPN_RCVD,
20 CNF_RCVD,
21 ESTAB,
22 HOLDING,
23 BLOCKED
24};
25
8012ad28
MP
26static void print_power_mode(struct nlattr *a)
27{
28 enum nl80211_mesh_power_mode pm = nla_get_u32(a);
29
30 switch (pm) {
31 case NL80211_MESH_POWER_ACTIVE:
32 printf("ACTIVE");
33 break;
34 case NL80211_MESH_POWER_LIGHT_SLEEP:
35 printf("LIGHT SLEEP");
36 break;
37 case NL80211_MESH_POWER_DEEP_SLEEP:
38 printf("DEEP SLEEP");
39 break;
40 default:
41 printf("UNKNOWN");
42 break;
43 }
44}
3d1e8704 45
3d1e8704
LCC
46static int print_sta_handler(struct nl_msg *msg, void *arg)
47{
48 struct nlattr *tb[NL80211_ATTR_MAX + 1];
49 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
50 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
9cd3f1c1 51 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
3d1e8704 52 char mac_addr[20], state_name[10], dev[20];
b638215d 53 struct nl80211_sta_flag_update *sta_flags;
3d1e8704
LCC
54 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
55 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
56 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
57 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
859677cb
JB
58 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
59 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
9cd3f1c1 60 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
ea3ade85 61 [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 },
9cd3f1c1 62 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
3d1e8704
LCC
63 [NL80211_STA_INFO_LLID] = { .type = NLA_U16 },
64 [NL80211_STA_INFO_PLID] = { .type = NLA_U16 },
65 [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
0f5868ee
BR
66 [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
67 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
b638215d
HS
68 [NL80211_STA_INFO_STA_FLAGS] =
69 { .minlen = sizeof(struct nl80211_sta_flag_update) },
8012ad28
MP
70 [NL80211_STA_INFO_LOCAL_PM] = { .type = NLA_U32},
71 [NL80211_STA_INFO_PEER_PM] = { .type = NLA_U32},
72 [NL80211_STA_INFO_NONPEER_PM] = { .type = NLA_U32},
3d1e8704
LCC
73 };
74
9cd3f1c1
HR
75 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
76 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
f0148acd 77 [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
9cd3f1c1
HR
78 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
79 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
80 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
81 };
82
3d1e8704
LCC
83 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
84 genlmsg_attrlen(gnlh, 0), NULL);
85
86 /*
87 * TODO: validate the interface and mac address!
88 * Otherwise, there's a race condition as soon as
89 * the kernel starts sending station notifications.
90 */
91
92 if (!tb[NL80211_ATTR_STA_INFO]) {
5fe70c0e 93 fprintf(stderr, "sta stats missing!\n");
3d1e8704
LCC
94 return NL_SKIP;
95 }
96 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
97 tb[NL80211_ATTR_STA_INFO],
98 stats_policy)) {
5fe70c0e 99 fprintf(stderr, "failed to parse nested attributes!\n");
3d1e8704
LCC
100 return NL_SKIP;
101 }
102
103 mac_addr_n2a(mac_addr, nla_data(tb[NL80211_ATTR_MAC]));
104 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
fbb181fa 105 printf("Station %s (on %s)", mac_addr, dev);
3d1e8704
LCC
106
107 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
f5c9799c 108 printf("\n\tinactive time:\t%u ms",
3d1e8704
LCC
109 nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]));
110 if (sinfo[NL80211_STA_INFO_RX_BYTES])
f5c9799c 111 printf("\n\trx bytes:\t%u",
3d1e8704 112 nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]));
859677cb 113 if (sinfo[NL80211_STA_INFO_RX_PACKETS])
f5c9799c 114 printf("\n\trx packets:\t%u",
859677cb 115 nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]));
3d1e8704 116 if (sinfo[NL80211_STA_INFO_TX_BYTES])
f5c9799c 117 printf("\n\ttx bytes:\t%u",
3d1e8704 118 nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]));
859677cb 119 if (sinfo[NL80211_STA_INFO_TX_PACKETS])
f5c9799c 120 printf("\n\ttx packets:\t%u",
859677cb 121 nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]));
0f5868ee
BR
122 if (sinfo[NL80211_STA_INFO_TX_RETRIES])
123 printf("\n\ttx retries:\t%u",
124 nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]));
125 if (sinfo[NL80211_STA_INFO_TX_FAILED])
126 printf("\n\ttx failed:\t%u",
127 nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]));
9cd3f1c1
HR
128 if (sinfo[NL80211_STA_INFO_SIGNAL])
129 printf("\n\tsignal: \t%d dBm",
130 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]));
ba292ae9
BR
131 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
132 printf("\n\tsignal avg:\t%d dBm",
133 (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]));
ea3ade85
AN
134 if (sinfo[NL80211_STA_INFO_T_OFFSET])
135 printf("\n\tToffset:\t%lld us",
136 (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]));
9cd3f1c1
HR
137
138 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
139 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
140 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy)) {
5fe70c0e 141 fprintf(stderr, "failed to parse nested rate attributes!\n");
9cd3f1c1 142 } else {
f0148acd 143 int rate = 0;
9cd3f1c1 144 printf("\n\ttx bitrate:\t");
f0148acd
VK
145 if (rinfo[NL80211_RATE_INFO_BITRATE32])
146 rate = nla_get_u32(rinfo[NL80211_RATE_INFO_BITRATE32]);
147 else if (rinfo[NL80211_RATE_INFO_BITRATE])
148 rate = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
149 if (rate > 0)
9cd3f1c1 150 printf("%d.%d MBit/s", rate / 10, rate % 10);
9cd3f1c1
HR
151
152 if (rinfo[NL80211_RATE_INFO_MCS])
153 printf(" MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_MCS]));
154 if (rinfo[NL80211_RATE_INFO_40_MHZ_WIDTH])
155 printf(" 40Mhz");
156 if (rinfo[NL80211_RATE_INFO_SHORT_GI])
157 printf(" short GI");
158 }
159 }
160
3d1e8704 161 if (sinfo[NL80211_STA_INFO_LLID])
fbb181fa 162 printf("\n\tmesh llid:\t%d",
3d1e8704
LCC
163 nla_get_u16(sinfo[NL80211_STA_INFO_LLID]));
164 if (sinfo[NL80211_STA_INFO_PLID])
fbb181fa 165 printf("\n\tmesh plid:\t%d",
3d1e8704
LCC
166 nla_get_u16(sinfo[NL80211_STA_INFO_PLID]));
167 if (sinfo[NL80211_STA_INFO_PLINK_STATE]) {
dbaabba1 168 switch (nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE])) {
3d1e8704
LCC
169 case LISTEN:
170 strcpy(state_name, "LISTEN");
171 break;
172 case OPN_SNT:
173 strcpy(state_name, "OPN_SNT");
174 break;
175 case OPN_RCVD:
176 strcpy(state_name, "OPN_RCVD");
177 break;
178 case CNF_RCVD:
179 strcpy(state_name, "CNF_RCVD");
180 break;
181 case ESTAB:
182 strcpy(state_name, "ESTAB");
183 break;
184 case HOLDING:
185 strcpy(state_name, "HOLDING");
186 break;
187 case BLOCKED:
188 strcpy(state_name, "BLOCKED");
189 break;
190 default:
191 strcpy(state_name, "UNKNOWN");
192 break;
193 }
fbb181fa 194 printf("\n\tmesh plink:\t%s", state_name);
3d1e8704 195 }
8012ad28
MP
196 if (sinfo[NL80211_STA_INFO_LOCAL_PM]) {
197 printf("\n\tmesh local PS mode:\t");
198 print_power_mode(sinfo[NL80211_STA_INFO_LOCAL_PM]);
199 }
200 if (sinfo[NL80211_STA_INFO_PEER_PM]) {
201 printf("\n\tmesh peer PS mode:\t");
202 print_power_mode(sinfo[NL80211_STA_INFO_PEER_PM]);
203 }
204 if (sinfo[NL80211_STA_INFO_NONPEER_PM]) {
205 printf("\n\tmesh non-peer PS mode:\t");
206 print_power_mode(sinfo[NL80211_STA_INFO_NONPEER_PM]);
207 }
3d1e8704 208
b638215d
HS
209 if (sinfo[NL80211_STA_INFO_STA_FLAGS]) {
210 sta_flags = (struct nl80211_sta_flag_update *)
211 nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]);
212
213 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
214 printf("\n\tauthorized:\t");
215 if (sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED))
216 printf("yes");
217 else
218 printf("no");
219 }
220
221 if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED)) {
222 printf("\n\tauthenticated:\t");
223 if (sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
224 printf("yes");
225 else
226 printf("no");
227 }
228
229 if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) {
230 printf("\n\tpreamble:\t");
231 if (sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
232 printf("short");
233 else
234 printf("long");
235 }
236
237 if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME)) {
238 printf("\n\tWMM/WME:\t");
239 if (sta_flags->set & BIT(NL80211_STA_FLAG_WME))
240 printf("yes");
241 else
242 printf("no");
243 }
244
245 if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP)) {
246 printf("\n\tMFP:\t\t");
247 if (sta_flags->set & BIT(NL80211_STA_FLAG_MFP))
248 printf("yes");
249 else
250 printf("no");
251 }
252
253 if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER)) {
254 printf("\n\tTDLS peer:\t\t");
255 if (sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER))
256 printf("yes");
257 else
258 printf("no");
259 }
260 }
261
3d1e8704
LCC
262 printf("\n");
263 return NL_SKIP;
264}
265
7c37a24d
JB
266static int handle_station_get(struct nl80211_state *state,
267 struct nl_cb *cb,
b1ca19a8 268 struct nl_msg *msg,
05514f95
JB
269 int argc, char **argv,
270 enum id_input id)
3d1e8704 271{
3d1e8704
LCC
272 unsigned char mac_addr[ETH_ALEN];
273
b1ca19a8 274 if (argc < 1)
5e75fd04 275 return 1;
3d1e8704
LCC
276
277 if (mac_addr_a2n(mac_addr, argv[0])) {
278 fprintf(stderr, "invalid mac address\n");
5e75fd04 279 return 2;
3d1e8704
LCC
280 }
281
282 argc--;
283 argv++;
284
b1ca19a8 285 if (argc)
5e75fd04 286 return 1;
3d1e8704
LCC
287
288 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
3d1e8704 289
3d1e8704 290 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
3d1e8704 291
70391ccf 292 return 0;
3d1e8704 293 nla_put_failure:
70391ccf 294 return -ENOBUFS;
3d1e8704 295}
b1ca19a8 296COMMAND(station, get, "<MAC address>",
70cf4544
JB
297 NL80211_CMD_GET_STATION, 0, CIB_NETDEV, handle_station_get,
298 "Get information for a specific station.");
b1ca19a8 299COMMAND(station, del, "<MAC address>",
70cf4544
JB
300 NL80211_CMD_DEL_STATION, 0, CIB_NETDEV, handle_station_get,
301 "Remove the given station entry (use with caution!)");
3d1e8704 302
1633ddf7
JB
303static const struct cmd *station_set_plink;
304static const struct cmd *station_set_vlan;
8012ad28 305static const struct cmd *station_set_mesh_power_mode;
1633ddf7
JB
306
307static const struct cmd *select_station_cmd(int argc, char **argv)
308{
309 if (argc < 2)
310 return NULL;
311 if (strcmp(argv[1], "plink_action") == 0)
312 return station_set_plink;
313 if (strcmp(argv[1], "vlan") == 0)
314 return station_set_vlan;
8012ad28
MP
315 if (strcmp(argv[1], "mesh_power_mode") == 0)
316 return station_set_mesh_power_mode;
1633ddf7
JB
317 return NULL;
318}
319
ce0fc33a 320static int handle_station_set_plink(struct nl80211_state *state,
7c37a24d 321 struct nl_cb *cb,
b1ca19a8 322 struct nl_msg *msg,
05514f95
JB
323 int argc, char **argv,
324 enum id_input id)
3d1e8704 325{
3d1e8704
LCC
326 unsigned char plink_action;
327 unsigned char mac_addr[ETH_ALEN];
328
b1ca19a8 329 if (argc < 3)
5e75fd04 330 return 1;
3d1e8704
LCC
331
332 if (mac_addr_a2n(mac_addr, argv[0])) {
333 fprintf(stderr, "invalid mac address\n");
5e75fd04 334 return 2;
3d1e8704
LCC
335 }
336 argc--;
337 argv++;
338
b1ca19a8
JB
339 if (strcmp("plink_action", argv[0]) != 0)
340 return 1;
3d1e8704
LCC
341 argc--;
342 argv++;
343
344 if (strcmp("open", argv[0]) == 0)
ac38f8ad 345 plink_action = NL80211_PLINK_ACTION_OPEN;
3d1e8704 346 else if (strcmp("block", argv[0]) == 0)
ac38f8ad 347 plink_action = NL80211_PLINK_ACTION_BLOCK;
3d1e8704
LCC
348 else {
349 fprintf(stderr, "plink action not supported\n");
5e75fd04 350 return 2;
3d1e8704
LCC
351 }
352 argc--;
353 argv++;
354
b1ca19a8 355 if (argc)
5e75fd04 356 return 1;
3d1e8704
LCC
357
358 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
3d1e8704
LCC
359 NLA_PUT_U8(msg, NL80211_ATTR_STA_PLINK_ACTION, plink_action);
360
70391ccf 361 return 0;
3d1e8704 362 nla_put_failure:
70391ccf 363 return -ENOBUFS;
3d1e8704 364}
1633ddf7 365COMMAND_ALIAS(station, set, "<MAC address> plink_action <open|block>",
ce0fc33a 366 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_plink,
1633ddf7
JB
367 "Set mesh peer link action for this station (peer).",
368 select_station_cmd, station_set_plink);
b1ca19a8 369
ce0fc33a 370static int handle_station_set_vlan(struct nl80211_state *state,
05514f95
JB
371 struct nl_cb *cb,
372 struct nl_msg *msg,
373 int argc, char **argv,
374 enum id_input id)
ce0fc33a
FF
375{
376 unsigned char mac_addr[ETH_ALEN];
377 unsigned long sta_vlan = 0;
378 char *err = NULL;
379
380 if (argc < 3)
381 return 1;
382
383 if (mac_addr_a2n(mac_addr, argv[0])) {
384 fprintf(stderr, "invalid mac address\n");
385 return 2;
386 }
387 argc--;
388 argv++;
389
390 if (strcmp("vlan", argv[0]) != 0)
391 return 1;
392 argc--;
393 argv++;
394
395 sta_vlan = strtoul(argv[0], &err, 0);
396 if (err && *err) {
397 fprintf(stderr, "invalid vlan id\n");
398 return 2;
399 }
400 argc--;
401 argv++;
402
403 if (argc)
404 return 1;
405
406 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
407 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN, sta_vlan);
408
409 return 0;
410 nla_put_failure:
411 return -ENOBUFS;
412}
1633ddf7 413COMMAND_ALIAS(station, set, "<MAC address> vlan <ifindex>",
ce0fc33a 414 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_vlan,
1633ddf7
JB
415 "Set an AP VLAN for this station.",
416 select_station_cmd, station_set_vlan);
ce0fc33a 417
8012ad28
MP
418static int handle_station_set_mesh_power_mode(struct nl80211_state *state,
419 struct nl_cb *cb,
420 struct nl_msg *msg,
421 int argc, char **argv,
422 enum id_input id)
423{
424 unsigned char mesh_power_mode;
425 unsigned char mac_addr[ETH_ALEN];
426
427 if (argc < 3)
428 return 1;
429
430 if (mac_addr_a2n(mac_addr, argv[0])) {
431 fprintf(stderr, "invalid mac address\n");
432 return 2;
433 }
434 argc--;
435 argv++;
436
437 if (strcmp("mesh_power_mode", argv[0]) != 0)
438 return 1;
439 argc--;
440 argv++;
441
442 if (strcmp("active", argv[0]) == 0)
443 mesh_power_mode = NL80211_MESH_POWER_ACTIVE;
444 else if (strcmp("light", argv[0]) == 0)
445 mesh_power_mode = NL80211_MESH_POWER_LIGHT_SLEEP;
446 else if (strcmp("deep", argv[0]) == 0)
447 mesh_power_mode = NL80211_MESH_POWER_DEEP_SLEEP;
448 else {
449 fprintf(stderr, "unknown mesh power mode\n");
450 return 2;
451 }
452 argc--;
453 argv++;
454
455 if (argc)
456 return 1;
457
458 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
459 NLA_PUT_U32(msg, NL80211_ATTR_LOCAL_MESH_POWER_MODE, mesh_power_mode);
460
461 return 0;
462nla_put_failure:
463 return -ENOBUFS;
464}
465COMMAND_ALIAS(station, set, "<MAC address> mesh_power_mode "
466 "<active|light|deep>", NL80211_CMD_SET_STATION, 0, CIB_NETDEV,
467 handle_station_set_mesh_power_mode,
468 "Set link-specific mesh power mode for this station",
469 select_station_cmd, station_set_mesh_power_mode);
ce0fc33a 470
7c37a24d
JB
471static int handle_station_dump(struct nl80211_state *state,
472 struct nl_cb *cb,
b1ca19a8 473 struct nl_msg *msg,
05514f95
JB
474 int argc, char **argv,
475 enum id_input id)
3d1e8704 476{
3d1e8704 477 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
70391ccf 478 return 0;
3d1e8704 479}
b1ca19a8 480COMMAND(station, dump, NULL,
70cf4544
JB
481 NL80211_CMD_GET_STATION, NLM_F_DUMP, CIB_NETDEV, handle_station_dump,
482 "List all stations known, e.g. the AP on managed interfaces");