]> git.ipfire.org Git - thirdparty/iw.git/blob - station.c
update nl80211.h
[thirdparty/iw.git] / station.c
1 #include <net/if.h>
2 #include <errno.h>
3 #include <string.h>
4
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>
10
11 #include "nl80211.h"
12 #include "iw.h"
13
14 SECTION(station);
15
16 enum plink_state {
17 LISTEN,
18 OPN_SNT,
19 OPN_RCVD,
20 CNF_RCVD,
21 ESTAB,
22 HOLDING,
23 BLOCKED
24 };
25
26 static 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 }
45
46 static 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];
51 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
52 char mac_addr[20], state_name[10], dev[20];
53 struct nl80211_sta_flag_update *sta_flags;
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 },
58 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
59 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
60 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
61 [NL80211_STA_INFO_T_OFFSET] = { .type = NLA_U64 },
62 [NL80211_STA_INFO_TX_BITRATE] = { .type = NLA_NESTED },
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 },
66 [NL80211_STA_INFO_TX_RETRIES] = { .type = NLA_U32 },
67 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
68 [NL80211_STA_INFO_STA_FLAGS] =
69 { .minlen = sizeof(struct nl80211_sta_flag_update) },
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},
73 };
74
75 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
76 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
77 [NL80211_RATE_INFO_BITRATE32] = { .type = NLA_U32 },
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
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]) {
93 fprintf(stderr, "sta stats missing!\n");
94 return NL_SKIP;
95 }
96 if (nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
97 tb[NL80211_ATTR_STA_INFO],
98 stats_policy)) {
99 fprintf(stderr, "failed to parse nested attributes!\n");
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);
105 printf("Station %s (on %s)", mac_addr, dev);
106
107 if (sinfo[NL80211_STA_INFO_INACTIVE_TIME])
108 printf("\n\tinactive time:\t%u ms",
109 nla_get_u32(sinfo[NL80211_STA_INFO_INACTIVE_TIME]));
110 if (sinfo[NL80211_STA_INFO_RX_BYTES])
111 printf("\n\trx bytes:\t%u",
112 nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]));
113 if (sinfo[NL80211_STA_INFO_RX_PACKETS])
114 printf("\n\trx packets:\t%u",
115 nla_get_u32(sinfo[NL80211_STA_INFO_RX_PACKETS]));
116 if (sinfo[NL80211_STA_INFO_TX_BYTES])
117 printf("\n\ttx bytes:\t%u",
118 nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]));
119 if (sinfo[NL80211_STA_INFO_TX_PACKETS])
120 printf("\n\ttx packets:\t%u",
121 nla_get_u32(sinfo[NL80211_STA_INFO_TX_PACKETS]));
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]));
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]));
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]));
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]));
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)) {
141 fprintf(stderr, "failed to parse nested rate attributes!\n");
142 } else {
143 int rate = 0;
144 printf("\n\ttx bitrate:\t");
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)
150 printf("%d.%d MBit/s", rate / 10, rate % 10);
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
161 if (sinfo[NL80211_STA_INFO_LLID])
162 printf("\n\tmesh llid:\t%d",
163 nla_get_u16(sinfo[NL80211_STA_INFO_LLID]));
164 if (sinfo[NL80211_STA_INFO_PLID])
165 printf("\n\tmesh plid:\t%d",
166 nla_get_u16(sinfo[NL80211_STA_INFO_PLID]));
167 if (sinfo[NL80211_STA_INFO_PLINK_STATE]) {
168 switch (nla_get_u8(sinfo[NL80211_STA_INFO_PLINK_STATE])) {
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 }
194 printf("\n\tmesh plink:\t%s", state_name);
195 }
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 }
208
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
262 printf("\n");
263 return NL_SKIP;
264 }
265
266 static int handle_station_get(struct nl80211_state *state,
267 struct nl_cb *cb,
268 struct nl_msg *msg,
269 int argc, char **argv,
270 enum id_input id)
271 {
272 unsigned char mac_addr[ETH_ALEN];
273
274 if (argc < 1)
275 return 1;
276
277 if (mac_addr_a2n(mac_addr, argv[0])) {
278 fprintf(stderr, "invalid mac address\n");
279 return 2;
280 }
281
282 argc--;
283 argv++;
284
285 if (argc)
286 return 1;
287
288 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
289
290 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
291
292 return 0;
293 nla_put_failure:
294 return -ENOBUFS;
295 }
296 COMMAND(station, get, "<MAC address>",
297 NL80211_CMD_GET_STATION, 0, CIB_NETDEV, handle_station_get,
298 "Get information for a specific station.");
299 COMMAND(station, del, "<MAC address>",
300 NL80211_CMD_DEL_STATION, 0, CIB_NETDEV, handle_station_get,
301 "Remove the given station entry (use with caution!)");
302
303 static const struct cmd *station_set_plink;
304 static const struct cmd *station_set_vlan;
305 static const struct cmd *station_set_mesh_power_mode;
306
307 static 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;
315 if (strcmp(argv[1], "mesh_power_mode") == 0)
316 return station_set_mesh_power_mode;
317 return NULL;
318 }
319
320 static int handle_station_set_plink(struct nl80211_state *state,
321 struct nl_cb *cb,
322 struct nl_msg *msg,
323 int argc, char **argv,
324 enum id_input id)
325 {
326 unsigned char plink_action;
327 unsigned char mac_addr[ETH_ALEN];
328
329 if (argc < 3)
330 return 1;
331
332 if (mac_addr_a2n(mac_addr, argv[0])) {
333 fprintf(stderr, "invalid mac address\n");
334 return 2;
335 }
336 argc--;
337 argv++;
338
339 if (strcmp("plink_action", argv[0]) != 0)
340 return 1;
341 argc--;
342 argv++;
343
344 if (strcmp("open", argv[0]) == 0)
345 plink_action = NL80211_PLINK_ACTION_OPEN;
346 else if (strcmp("block", argv[0]) == 0)
347 plink_action = NL80211_PLINK_ACTION_BLOCK;
348 else {
349 fprintf(stderr, "plink action not supported\n");
350 return 2;
351 }
352 argc--;
353 argv++;
354
355 if (argc)
356 return 1;
357
358 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
359 NLA_PUT_U8(msg, NL80211_ATTR_STA_PLINK_ACTION, plink_action);
360
361 return 0;
362 nla_put_failure:
363 return -ENOBUFS;
364 }
365 COMMAND_ALIAS(station, set, "<MAC address> plink_action <open|block>",
366 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_plink,
367 "Set mesh peer link action for this station (peer).",
368 select_station_cmd, station_set_plink);
369
370 static int handle_station_set_vlan(struct nl80211_state *state,
371 struct nl_cb *cb,
372 struct nl_msg *msg,
373 int argc, char **argv,
374 enum id_input id)
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 }
413 COMMAND_ALIAS(station, set, "<MAC address> vlan <ifindex>",
414 NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_vlan,
415 "Set an AP VLAN for this station.",
416 select_station_cmd, station_set_vlan);
417
418 static 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;
462 nla_put_failure:
463 return -ENOBUFS;
464 }
465 COMMAND_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);
470
471 static int handle_station_dump(struct nl80211_state *state,
472 struct nl_cb *cb,
473 struct nl_msg *msg,
474 int argc, char **argv,
475 enum id_input id)
476 {
477 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_sta_handler, NULL);
478 return 0;
479 }
480 COMMAND(station, dump, NULL,
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");