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