]> git.ipfire.org Git - thirdparty/iw.git/blame - interface.c
bump version to 6.9
[thirdparty/iw.git] / interface.c
CommitLineData
45c7212c 1#include <errno.h>
d5ac8ad3 2#include <string.h>
d17fe27e 3#include <stdbool.h>
2ef1be68 4
45c7212c
JB
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>
45c7212c 10
f408e01b 11#include "nl80211.h"
45c7212c
JB
12#include "iw.h"
13
1ae3d621
JB
14#define VALID_FLAGS "none: no special flags\n"\
15 "fcsfail: show frames with FCS errors\n"\
16 "control: show control frames\n"\
17 "otherbss: show frames from other BSSes\n"\
068ee232 18 "cook: use cooked mode\n"\
386bacb2
AE
19 "active: use active mode (ACK incoming unicast packets)\n"\
20 "mumimo-groupid <GROUP_ID>: use MUMIMO according to a group id\n"\
21 "mumimo-follow-mac <MAC_ADDRESS>: use MUMIMO according to a MAC address"
1ae3d621 22
4698bfc2
JB
23SECTION(interface);
24
cd293761 25static char *mntr_flags[NL80211_MNTR_FLAG_MAX + 1] = {
dd65496b 26 "none",
cd293761
JB
27 "fcsfail",
28 "plcpfail",
29 "control",
30 "otherbss",
31 "cook",
068ee232 32 "active",
cd293761
JB
33};
34
386bacb2
AE
35static int parse_mumimo_options(int *_argc, char ***_argv, struct nl_msg *msg)
36{
37 uint8_t mumimo_group[VHT_MUMIMO_GROUP_LEN];
38 unsigned char mac_addr[ETH_ALEN];
39 char **argv = *_argv;
40 int argc = *_argc;
41 int i;
42 unsigned int val;
43
44 if (strcmp(*argv, "mumimo-groupid") == 0) {
45 argc--;
46 argv++;
47 if (!argc || strlen(*argv) != VHT_MUMIMO_GROUP_LEN*2) {
48 fprintf(stderr, "Invalid groupID: %s\n", *argv);
49 return 1;
50 }
51
52 for (i = 0; i < VHT_MUMIMO_GROUP_LEN; i++) {
53 if (sscanf((*argv) + i*2, "%2x", &val) != 1) {
54 fprintf(stderr, "Failed reading groupID\n");
55 return 1;
56 }
57 mumimo_group[i] = val;
58 }
59
60 NLA_PUT(msg,
61 NL80211_ATTR_MU_MIMO_GROUP_DATA,
62 VHT_MUMIMO_GROUP_LEN,
63 mumimo_group);
64 argc--;
65 argv++;
66 } else if (strcmp(*argv, "mumimo-follow-mac") == 0) {
67 argc--;
68 argv++;
69 if (!argc || mac_addr_a2n(mac_addr, *argv)) {
70 fprintf(stderr, "Invalid MAC address\n");
71 return 1;
72 }
73 NLA_PUT(msg, NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR,
74 ETH_ALEN, mac_addr);
75 argc--;
76 argv++;
77 }
78 nla_put_failure:
79 *_argc = argc;
80 *_argv = argv;
81 return 0;
82}
83
dd65496b
JB
84static int parse_mntr_flags(int *_argc, char ***_argv,
85 struct nl_msg *msg)
86{
87 struct nl_msg *flags;
88 int err = -ENOBUFS;
89 enum nl80211_mntr_flags flag;
90 int argc = *_argc;
91 char **argv = *_argv;
92
93 flags = nlmsg_alloc();
94 if (!flags)
95 return -ENOMEM;
96
97 while (argc) {
98 int ok = 0;
386bacb2
AE
99
100 /* parse MU-MIMO options */
101 err = parse_mumimo_options(&argc, &argv, msg);
102 if (err)
103 goto out;
104 else if (!argc)
105 break;
106
107 /* parse monitor flags */
dd65496b 108 for (flag = __NL80211_MNTR_FLAG_INVALID;
e14cc99e 109 flag <= NL80211_MNTR_FLAG_MAX; flag++) {
dd65496b
JB
110 if (strcmp(*argv, mntr_flags[flag]) == 0) {
111 ok = 1;
4a3bf753
JB
112 /*
113 * This shouldn't be adding "flag" if that is
114 * zero, but due to a problem in the kernel's
115 * nl80211 code (using NLA_NESTED policy) it
116 * will reject an empty nested attribute but
117 * not one that contains an invalid attribute
118 */
dd65496b
JB
119 NLA_PUT_FLAG(flags, flag);
120 break;
121 }
122 }
123 if (!ok) {
124 err = -EINVAL;
125 goto out;
126 }
127 argc--;
128 argv++;
129 }
130
131 nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
132 err = 0;
133 nla_put_failure:
134 out:
135 nlmsg_free(flags);
136
137 *_argc = argc;
138 *_argv = argv;
139
140 return err;
141}
142
1ae3d621
JB
143/* for help */
144#define IFACE_TYPES "Valid interface types are: managed, ibss, monitor, mesh, wds."
145
d17fe27e
JB
146/* return 0 if ok, internal error otherwise */
147static int get_if_type(int *argc, char ***argv, enum nl80211_iftype *type,
148 bool need_type)
45c7212c
JB
149{
150 char *tpstr;
151
d17fe27e
JB
152 if (*argc < 1 + !!need_type)
153 return 1;
45c7212c 154
d17fe27e
JB
155 if (need_type && strcmp((*argv)[0], "type"))
156 return 1;
45c7212c 157
d17fe27e
JB
158 tpstr = (*argv)[!!need_type];
159 *argc -= 1 + !!need_type;
160 *argv += 1 + !!need_type;
45c7212c
JB
161
162 if (strcmp(tpstr, "adhoc") == 0 ||
163 strcmp(tpstr, "ibss") == 0) {
164 *type = NL80211_IFTYPE_ADHOC;
d17fe27e 165 return 0;
3955e524
RL
166 } else if (strcmp(tpstr, "ocb") == 0) {
167 *type = NL80211_IFTYPE_OCB;
168 return 0;
45c7212c
JB
169 } else if (strcmp(tpstr, "monitor") == 0) {
170 *type = NL80211_IFTYPE_MONITOR;
d17fe27e 171 return 0;
e08b5488
JB
172 } else if (strcmp(tpstr, "master") == 0 ||
173 strcmp(tpstr, "ap") == 0) {
c1d44a6c 174 *type = NL80211_IFTYPE_UNSPECIFIED;
e08b5488
JB
175 fprintf(stderr, "You need to run a management daemon, e.g. hostapd,\n");
176 fprintf(stderr, "see http://wireless.kernel.org/en/users/Documentation/hostapd\n");
177 fprintf(stderr, "for more information on how to do that.\n");
c1d44a6c 178 return 2;
4d3a72da 179 } else if (strcmp(tpstr, "__ap") == 0) {
45c7212c 180 *type = NL80211_IFTYPE_AP;
d17fe27e 181 return 0;
4d3a72da 182 } else if (strcmp(tpstr, "__ap_vlan") == 0) {
45c7212c 183 *type = NL80211_IFTYPE_AP_VLAN;
d17fe27e 184 return 0;
45c7212c
JB
185 } else if (strcmp(tpstr, "wds") == 0) {
186 *type = NL80211_IFTYPE_WDS;
d17fe27e 187 return 0;
a65df165
JB
188 } else if (strcmp(tpstr, "managed") == 0 ||
189 strcmp(tpstr, "mgd") == 0 ||
190 strcmp(tpstr, "station") == 0) {
45c7212c 191 *type = NL80211_IFTYPE_STATION;
d17fe27e 192 return 0;
3d1e8704 193 } else if (strcmp(tpstr, "mp") == 0 ||
a65df165 194 strcmp(tpstr, "mesh") == 0) {
3d1e8704 195 *type = NL80211_IFTYPE_MESH_POINT;
d17fe27e 196 return 0;
a4464243
JB
197 } else if (strcmp(tpstr, "__p2pcl") == 0) {
198 *type = NL80211_IFTYPE_P2P_CLIENT;
199 return 0;
8d5d7ba7
JB
200 } else if (strcmp(tpstr, "__p2pdev") == 0) {
201 *type = NL80211_IFTYPE_P2P_DEVICE;
202 return 0;
a4464243
JB
203 } else if (strcmp(tpstr, "__p2pgo") == 0) {
204 *type = NL80211_IFTYPE_P2P_GO;
205 return 0;
ed9b77ec
AO
206 } else if (strcmp(tpstr, "__nan") == 0) {
207 *type = NL80211_IFTYPE_NAN;
208 return 0;
45c7212c
JB
209 }
210
45c7212c 211 fprintf(stderr, "invalid interface type %s\n", tpstr);
d17fe27e 212 return 2;
45c7212c
JB
213}
214
39566cca
FF
215static int parse_4addr_flag(const char *value, struct nl_msg *msg)
216{
217 if (strcmp(value, "on") == 0)
218 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, 1);
219 else if (strcmp(value, "off") == 0)
220 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, 0);
221 else
222 return 1;
223 return 0;
224
225nla_put_failure:
226 return 1;
227}
228
7c37a24d 229static int handle_interface_add(struct nl80211_state *state,
bd396f2a 230 struct nl_msg *msg,
05514f95
JB
231 int argc, char **argv,
232 enum id_input id)
45c7212c 233{
2dfd6bfa 234 char *name;
3d1e8704 235 char *mesh_id = NULL;
45c7212c 236 enum nl80211_iftype type;
70391ccf 237 int tpset;
ced5522d
BG
238 unsigned char mac_addr[ETH_ALEN];
239 int found_mac = 0;
45c7212c 240
bd396f2a 241 if (argc < 1)
5e75fd04 242 return 1;
45c7212c 243
2dfd6bfa 244 name = argv[0];
45c7212c
JB
245 argc--;
246 argv++;
247
d17fe27e
JB
248 tpset = get_if_type(&argc, &argv, &type, true);
249 if (tpset)
c1d44a6c 250 return tpset;
45c7212c 251
ced5522d 252try_another:
3d1e8704 253 if (argc) {
dd65496b
JB
254 if (strcmp(argv[0], "mesh_id") == 0) {
255 argc--;
256 argv++;
257
258 if (!argc)
259 return 1;
260 mesh_id = argv[0];
261 argc--;
262 argv++;
ced5522d
BG
263 } else if (strcmp(argv[0], "addr") == 0) {
264 argc--;
265 argv++;
266 if (mac_addr_a2n(mac_addr, argv[0])) {
267 fprintf(stderr, "Invalid MAC address\n");
268 return 2;
269 }
270 argc--;
271 argv++;
272 found_mac = 1;
273 goto try_another;
39566cca
FF
274 } else if (strcmp(argv[0], "4addr") == 0) {
275 argc--;
276 argv++;
277 if (parse_4addr_flag(argv[0], msg)) {
278 fprintf(stderr, "4addr error\n");
279 return 2;
280 }
281 argc--;
282 argv++;
dd65496b
JB
283 } else if (strcmp(argv[0], "flags") == 0) {
284 argc--;
285 argv++;
286 if (parse_mntr_flags(&argc, &argv, msg)) {
287 fprintf(stderr, "flags error\n");
288 return 2;
289 }
290 } else {
5e75fd04 291 return 1;
dd65496b 292 }
3d1e8704
LCC
293 }
294
5e75fd04
JB
295 if (argc)
296 return 1;
45c7212c 297
45c7212c 298 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name);
6d28ce25 299 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
3d1e8704
LCC
300 if (mesh_id)
301 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
ced5522d
BG
302 if (found_mac)
303 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
45c7212c 304
70391ccf 305 return 0;
5e75fd04 306 nla_put_failure:
70391ccf 307 return -ENOBUFS;
45c7212c 308}
ced5522d 309COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*] [addr <mac-addr>]",
1ae3d621
JB
310 NL80211_CMD_NEW_INTERFACE, 0, CIB_PHY, handle_interface_add,
311 "Add a new virtual interface with the given configuration.\n"
312 IFACE_TYPES "\n\n"
313 "The flags are only used for monitor interfaces, valid flags are:\n"
314 VALID_FLAGS "\n\n"
315 "The mesh_id is used only for mesh mode.");
ced5522d 316COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*] [addr <mac-addr>]",
01ae06f9 317 NL80211_CMD_NEW_INTERFACE, 0, CIB_NETDEV, handle_interface_add, NULL);
45c7212c 318
7c37a24d 319static int handle_interface_del(struct nl80211_state *state,
bd396f2a 320 struct nl_msg *msg,
05514f95
JB
321 int argc, char **argv,
322 enum id_input id)
3fcfe40e 323{
70391ccf 324 return 0;
3fcfe40e 325}
1ae3d621
JB
326TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del,
327 "Remove this virtual interface");
ce5af55c 328HIDDEN(interface, del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del);
3fcfe40e 329
3adcc1f4
PF
330static char *channel_type_name(enum nl80211_channel_type channel_type)
331{
332 switch (channel_type) {
333 case NL80211_CHAN_NO_HT:
334 return "NO HT";
335 case NL80211_CHAN_HT20:
336 return "HT20";
337 case NL80211_CHAN_HT40MINUS:
338 return "HT40-";
339 case NL80211_CHAN_HT40PLUS:
340 return "HT40+";
341 default:
342 return "unknown";
343 }
344}
345
c5df9eb6 346char *channel_width_name(enum nl80211_chan_width width)
7c60bb76
JB
347{
348 switch (width) {
349 case NL80211_CHAN_WIDTH_20_NOHT:
350 return "20 MHz (no HT)";
351 case NL80211_CHAN_WIDTH_20:
352 return "20 MHz";
353 case NL80211_CHAN_WIDTH_40:
354 return "40 MHz";
355 case NL80211_CHAN_WIDTH_80:
356 return "80 MHz";
357 case NL80211_CHAN_WIDTH_80P80:
358 return "80+80 MHz";
359 case NL80211_CHAN_WIDTH_160:
360 return "160 MHz";
432b5ae2
BC
361 case NL80211_CHAN_WIDTH_5:
362 return "5 MHz";
363 case NL80211_CHAN_WIDTH_10:
364 return "10 MHz";
5a71b722
IP
365 case NL80211_CHAN_WIDTH_320:
366 return "320 MHz";
7c60bb76
JB
367 default:
368 return "unknown";
369 }
370}
371
7794573f
JB
372static void print_channel(struct nlattr **tb)
373{
374 uint32_t freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
375
376 printf("channel %d (%d MHz)",
377 ieee80211_frequency_to_channel(freq), freq);
378
379 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
380 printf(", width: %s",
381 channel_width_name(nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH])));
382 if (tb[NL80211_ATTR_CENTER_FREQ1])
383 printf(", center1: %d MHz",
384 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]));
385 if (tb[NL80211_ATTR_CENTER_FREQ2])
386 printf(", center2: %d MHz",
387 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]));
c2c89feb
JB
388
389 if (tb[NL80211_ATTR_PUNCT_BITMAP]) {
390 uint32_t punct = nla_get_u32(tb[NL80211_ATTR_PUNCT_BITMAP]);
391
392 if (punct)
393 printf(", punctured: 0x%x", punct);
394 }
7794573f
JB
395 } else if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
396 enum nl80211_channel_type channel_type;
397
398 channel_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
399 printf(" %s", channel_type_name(channel_type));
400 }
401}
402
541ef425
JB
403static int print_iface_handler(struct nl_msg *msg, void *arg)
404{
405 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
406 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
674567b8
JB
407 unsigned int *wiphy = arg;
408 const char *indent = "";
541ef425
JB
409
410 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
411 genlmsg_attrlen(gnlh, 0), NULL);
412
674567b8
JB
413 if (wiphy && tb_msg[NL80211_ATTR_WIPHY]) {
414 unsigned int thiswiphy = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]);
415 indent = "\t";
416 if (*wiphy != thiswiphy)
417 printf("phy#%d\n", thiswiphy);
418 *wiphy = thiswiphy;
419 }
420
541ef425 421 if (tb_msg[NL80211_ATTR_IFNAME])
674567b8 422 printf("%sInterface %s\n", indent, nla_get_string(tb_msg[NL80211_ATTR_IFNAME]));
3508c5c9
JB
423 else
424 printf("%sUnnamed/non-netdev interface\n", indent);
541ef425 425 if (tb_msg[NL80211_ATTR_IFINDEX])
674567b8 426 printf("%s\tifindex %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX]));
3508c5c9
JB
427 if (tb_msg[NL80211_ATTR_WDEV])
428 printf("%s\twdev 0x%llx\n", indent,
429 (unsigned long long)nla_get_u64(tb_msg[NL80211_ATTR_WDEV]));
430 if (tb_msg[NL80211_ATTR_MAC]) {
431 char mac_addr[20];
432 mac_addr_n2a(mac_addr, nla_data(tb_msg[NL80211_ATTR_MAC]));
433 printf("%s\taddr %s\n", indent, mac_addr);
434 }
01009c3d
AQ
435 if (tb_msg[NL80211_ATTR_SSID]) {
436 printf("%s\tssid ", indent);
437 print_ssid_escaped(nla_len(tb_msg[NL80211_ATTR_SSID]),
438 nla_data(tb_msg[NL80211_ATTR_SSID]));
439 printf("\n");
440 }
541ef425 441 if (tb_msg[NL80211_ATTR_IFTYPE])
674567b8 442 printf("%s\ttype %s\n", indent, iftype_name(nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE])));
47b76de4 443 if (!wiphy && tb_msg[NL80211_ATTR_WIPHY])
5b050af4 444 printf("%s\twiphy %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]));
3adcc1f4 445 if (tb_msg[NL80211_ATTR_WIPHY_FREQ]) {
7794573f
JB
446 printf("%s\t", indent);
447 print_channel(tb_msg);
7c60bb76 448 printf("\n");
3adcc1f4 449 }
541ef425 450
619d28ab 451 if (tb_msg[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]) {
71340cdc 452 int32_t txp = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]);
619d28ab
RM
453
454 printf("%s\ttxpower %d.%.2d dBm\n",
455 indent, txp / 100, txp % 100);
456 }
457
910792c0
THJ
458 if (tb_msg[NL80211_ATTR_TXQ_STATS]) {
459 char buf[150];
460 parse_txq_stats(buf, sizeof(buf), tb_msg[NL80211_ATTR_TXQ_STATS], 1, -1, indent);
461 printf("%s\tmulticast TXQ:%s\n", indent, buf);
462 }
463
e99bc38e
AQ
464 if (tb_msg[NL80211_ATTR_4ADDR]) {
465 uint8_t use_4addr = nla_get_u8(tb_msg[NL80211_ATTR_4ADDR]);
466 if (use_4addr)
467 printf("%s\t4addr: on\n", indent);
468 }
469
7794573f
JB
470 if (tb_msg[NL80211_ATTR_MLO_LINKS]) {
471 struct nlattr *link;
472 int n;
473
474 printf("%s\tMLD with links:\n", indent);
475
476 nla_for_each_nested(link, tb_msg[NL80211_ATTR_MLO_LINKS], n) {
477 struct nlattr *tb[NL80211_ATTR_MAX + 1];
478
479 nla_parse_nested(tb, NL80211_ATTR_MAX, link, NULL);
480 printf("%s\t - link", indent);
481 if (tb[NL80211_ATTR_MLO_LINK_ID])
482 printf(" ID %2d", nla_get_u32(tb[NL80211_ATTR_MLO_LINK_ID]));
483 if (tb[NL80211_ATTR_MAC]) {
484 char buf[20];
485
486 mac_addr_n2a(buf, nla_data(tb[NL80211_ATTR_MAC]));
487 printf(" link addr %s", buf);
488 }
489 if (tb[NL80211_ATTR_WIPHY_FREQ]) {
490 printf("\n%s\t ", indent);
491 print_channel(tb);
492 }
493 printf("\n");
494 }
495 }
496
541ef425
JB
497 return NL_SKIP;
498}
499
7c37a24d 500static int handle_interface_info(struct nl80211_state *state,
bd396f2a 501 struct nl_msg *msg,
05514f95
JB
502 int argc, char **argv,
503 enum id_input id)
541ef425 504{
34b23014 505 register_handler(print_iface_handler, NULL);
70391ccf 506 return 0;
541ef425 507}
1ae3d621
JB
508TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info,
509 "Show information for this interface.");
cd293761 510
7c37a24d 511static int handle_interface_set(struct nl80211_state *state,
cd293761 512 struct nl_msg *msg,
05514f95
JB
513 int argc, char **argv,
514 enum id_input id)
cd293761 515{
cd293761
JB
516 if (!argc)
517 return 1;
518
cd293761
JB
519 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
520
dd65496b
JB
521 switch (parse_mntr_flags(&argc, &argv, msg)) {
522 case 0:
523 return 0;
386bacb2
AE
524 case 1:
525 return 1;
dd65496b
JB
526 case -ENOMEM:
527 fprintf(stderr, "failed to allocate flags\n");
528 return 2;
529 case -EINVAL:
530 fprintf(stderr, "unknown flag %s\n", *argv);
531 return 2;
532 default:
533 return 2;
cd293761 534 }
cd293761 535 nla_put_failure:
dd65496b 536 return -ENOBUFS;
cd293761 537}
1ae3d621
JB
538COMMAND(set, monitor, "<flag>*",
539 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_set,
540 "Set monitor flags. Valid flags are:\n"
541 VALID_FLAGS);
82d028d0 542
7c37a24d 543static int handle_interface_meshid(struct nl80211_state *state,
82d028d0 544 struct nl_msg *msg,
05514f95
JB
545 int argc, char **argv,
546 enum id_input id)
82d028d0
JB
547{
548 char *mesh_id = NULL;
549
550 if (argc != 1)
551 return 1;
552
553 mesh_id = argv[0];
554
555 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
556
557 return 0;
558 nla_put_failure:
559 return -ENOBUFS;
560}
561COMMAND(set, meshid, "<meshid>",
01ae06f9 562 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_meshid, NULL);
674567b8
JB
563
564static unsigned int dev_dump_wiphy;
565
566static int handle_dev_dump(struct nl80211_state *state,
674567b8 567 struct nl_msg *msg,
05514f95
JB
568 int argc, char **argv,
569 enum id_input id)
674567b8
JB
570{
571 dev_dump_wiphy = -1;
34b23014 572 register_handler(print_iface_handler, &dev_dump_wiphy);
674567b8
JB
573 return 0;
574}
1ae3d621
JB
575TOPLEVEL(dev, NULL, NL80211_CMD_GET_INTERFACE, NLM_F_DUMP, CIB_NONE, handle_dev_dump,
576 "List all network interfaces for wireless hardware.");
d17fe27e
JB
577
578static int handle_interface_type(struct nl80211_state *state,
d17fe27e 579 struct nl_msg *msg,
05514f95
JB
580 int argc, char **argv,
581 enum id_input id)
d17fe27e
JB
582{
583 enum nl80211_iftype type;
584 int tpset;
585
586 tpset = get_if_type(&argc, &argv, &type, false);
587 if (tpset)
588 return tpset;
589
590 if (argc)
591 return 1;
592
593 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
594
595 return 0;
596 nla_put_failure:
597 return -ENOBUFS;
598}
599COMMAND(set, type, "<type>",
1ae3d621
JB
600 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_type,
601 "Set interface type/mode.\n"
602 IFACE_TYPES);
20e4ff8a
JF
603
604static int handle_interface_4addr(struct nl80211_state *state,
05514f95
JB
605 struct nl_msg *msg,
606 int argc, char **argv,
607 enum id_input id)
20e4ff8a
JF
608{
609 if (argc != 1)
610 return 1;
611 return parse_4addr_flag(argv[0], msg);
612}
613COMMAND(set, 4addr, "<on|off>",
614 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_4addr,
5548dfd7 615 "Set interface 4addr (WDS) mode.");
88c06599 616
83a7bd0d 617static int handle_interface_noack_map(struct nl80211_state *state,
83a7bd0d 618 struct nl_msg *msg,
05514f95
JB
619 int argc, char **argv,
620 enum id_input id)
83a7bd0d
SW
621{
622 uint16_t noack_map;
623 char *end;
624
625 if (argc != 1)
626 return 1;
627
628 noack_map = strtoul(argv[0], &end, 16);
629 if (*end)
630 return 1;
631
632 NLA_PUT_U16(msg, NL80211_ATTR_NOACK_MAP, noack_map);
633
634 return 0;
635 nla_put_failure:
636 return -ENOBUFS;
637
638}
639COMMAND(set, noack_map, "<map>",
640 NL80211_CMD_SET_NOACK_MAP, 0, CIB_NETDEV, handle_interface_noack_map,
641 "Set the NoAck map for the TIDs. (0x0009 = BE, 0x0006 = BK, 0x0030 = VI, 0x00C0 = VO)");
642
643
88c06599 644static int handle_interface_wds_peer(struct nl80211_state *state,
88c06599 645 struct nl_msg *msg,
05514f95
JB
646 int argc, char **argv,
647 enum id_input id)
88c06599
BJ
648{
649 unsigned char mac_addr[ETH_ALEN];
650
651 if (argc < 1)
652 return 1;
653
654 if (mac_addr_a2n(mac_addr, argv[0])) {
5548dfd7 655 fprintf(stderr, "Invalid MAC address\n");
88c06599
BJ
656 return 2;
657 }
658
659 argc--;
660 argv++;
661
662 if (argc)
663 return 1;
664
665 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
666
667 return 0;
668 nla_put_failure:
669 return -ENOBUFS;
670}
671COMMAND(set, peer, "<MAC address>",
672 NL80211_CMD_SET_WDS_PEER, 0, CIB_NETDEV, handle_interface_wds_peer,
5548dfd7 673 "Set interface WDS peer.");
ffaac0f7
AQ
674
675static int set_mcast_rate(struct nl80211_state *state,
ffaac0f7
AQ
676 struct nl_msg *msg,
677 int argc, char **argv,
678 enum id_input id)
679{
680 float rate;
681 char *end;
682
86a9801f
OO
683 if (argc != 1)
684 return 1;
ffaac0f7
AQ
685
686 rate = strtod(argv[0], &end);
687 if (*end != '\0')
688 return 1;
689
690 NLA_PUT_U32(msg, NL80211_ATTR_MCAST_RATE, (int)(rate * 10));
691
692 return 0;
693nla_put_failure:
694 return -ENOBUFS;
695}
696
697COMMAND(set, mcast_rate, "<rate in Mbps>",
698 NL80211_CMD_SET_MCAST_RATE, 0, CIB_NETDEV, set_mcast_rate,
699 "Set the multicast bitrate.");
444674c8
BB
700
701
702static int handle_chanfreq(struct nl80211_state *state, struct nl_msg *msg,
703 bool chan, int argc, char **argv,
704 enum id_input id)
705{
706 struct chandef chandef;
707 int res;
708 int parsed;
709 char *end;
710
a32046bc 711 res = parse_freqchan(&chandef, chan, argc, argv, &parsed, false);
444674c8
BB
712 if (res)
713 return res;
714
715 argc -= parsed;
716 argv += parsed;
717
718 while (argc) {
719 unsigned int beacons = 10;
720
721 if (strcmp(argv[0], "beacons") == 0) {
722 if (argc < 2)
723 return 1;
724
725 beacons = strtol(argv[1], &end, 10);
726 if (*end)
727 return 1;
728
729 argc -= 2;
730 argv += 2;
731
732 NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, beacons);
733 } else if (strcmp(argv[0], "block-tx") == 0) {
734 argc -= 1;
735 argv += 1;
736
737 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
738 } else {
739 return 1;
740 }
741 }
742
743 return put_chandef(msg, &chandef);
744
745 nla_put_failure:
746 return -ENOBUFS;
747}
748
749static int handle_freq(struct nl80211_state *state, struct nl_msg *msg,
750 int argc, char **argv,
751 enum id_input id)
752{
753 return handle_chanfreq(state, msg, false, argc, argv, id);
754}
755
756static int handle_chan(struct nl80211_state *state, struct nl_msg *msg,
757 int argc, char **argv,
758 enum id_input id)
759{
760 return handle_chanfreq(state, msg, true, argc, argv, id);
761}
762
763SECTION(switch);
764COMMAND(switch, freq,
765 "<freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz] [beacons <count>] [block-tx]\n"
766 "<control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]] [beacons <count>] [block-tx]",
767 NL80211_CMD_CHANNEL_SWITCH, 0, CIB_NETDEV, handle_freq,
768 "Switch the operating channel by sending a channel switch announcement (CSA).");
769COMMAND(switch, channel, "<channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz] [beacons <count>] [block-tx]",
770 NL80211_CMD_CHANNEL_SWITCH, 0, CIB_NETDEV, handle_chan, NULL);
71ad41cd
SM
771
772
773static int toggle_tid_param(const char *argv0, const char *argv1,
774 struct nl_msg *msg, uint32_t attr)
775{
776 uint8_t val;
777
778 if (strcmp(argv1, "on") == 0) {
779 val = NL80211_TID_CONFIG_ENABLE;
780 } else if (strcmp(argv1, "off") == 0) {
781 val = NL80211_TID_CONFIG_DISABLE;
782 } else {
783 fprintf(stderr, "Invalid %s parameter: %s\n", argv0, argv1);
784 return 2;
785 }
786
787 NLA_PUT_U8(msg, attr, val);
788 return 0;
789
790 nla_put_failure:
791 return -ENOBUFS;
792}
793
794static int handle_tid_config(struct nl80211_state *state,
795 struct nl_msg *msg,
796 int argc, char **argv,
797 enum id_input id)
798{
799 struct nlattr *tids_array = NULL;
800 struct nlattr *tids_entry = NULL;
4d2749a6 801 enum nl80211_tx_rate_setting txrate_type;
71ad41cd
SM
802 unsigned char peer[ETH_ALEN];
803 int tids_num = 0;
804 char *end;
805 int ret;
806 enum {
807 PS_ADDR,
808 PS_TIDS,
809 PS_CONF,
810 } parse_state = PS_ADDR;
4d2749a6 811 unsigned int attr;
71ad41cd
SM
812
813 while (argc) {
814 switch (parse_state) {
815 case PS_ADDR:
816 if (strcmp(argv[0], "peer") == 0) {
817 if (argc < 2) {
818 fprintf(stderr, "Not enough args for %s\n", argv[0]);
819 return HANDLER_RET_USAGE;
820 }
821
822 if (mac_addr_a2n(peer, argv[1])) {
823 fprintf(stderr, "Invalid MAC address\n");
824 return 2;
825 }
826
827 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
828
829 argc -= 2;
830 argv += 2;
831 parse_state = PS_TIDS;
832
833 } else if (strcmp(argv[0], "tids") == 0) {
834 parse_state = PS_TIDS;
835 } else {
836 fprintf(stderr, "Peer MAC address expected\n");
837 return HANDLER_RET_USAGE;
838 }
839
840 break;
841 case PS_TIDS:
842 if (strcmp(argv[0], "tids") == 0) {
843 if (argc < 2) {
844 fprintf(stderr, "not enough args for %s\n", argv[0]);
845 return HANDLER_RET_USAGE;
846 }
847
848 if (!tids_array) {
849 tids_array = nla_nest_start(msg, NL80211_ATTR_TID_CONFIG);
850 if (!tids_array)
851 return -ENOBUFS;
852 }
853
854 if (tids_entry) {
855 nla_nest_end(msg, tids_entry);
856 tids_num++;
857 }
858
859 tids_entry = nla_nest_start(msg, tids_num);
860 if (!tids_entry)
861 return -ENOBUFS;
862
863 NLA_PUT_U16(msg, NL80211_TID_CONFIG_ATTR_TIDS, strtol(argv[1], &end, 0));
864 if (*end) {
865 fprintf(stderr, "Invalid TID mask value: %s\n", argv[1]);
866 return 2;
867 }
868
869 argc -= 2;
870 argv += 2;
871 parse_state = PS_CONF;
872 } else {
873 fprintf(stderr, "TID mask expected\n");
874 return HANDLER_RET_USAGE;
875 }
876
877 break;
878 case PS_CONF:
879 if (strcmp(argv[0], "tids") == 0) {
880 parse_state = PS_TIDS;
881 } else if (strcmp(argv[0], "override") == 0) {
882 NLA_PUT_FLAG(msg, NL80211_TID_CONFIG_ATTR_OVERRIDE);
883
884 argc -= 1;
885 argv += 1;
886 } else if (strcmp(argv[0], "ampdu") == 0) {
887 if (argc < 2) {
888 fprintf(stderr, "not enough args for %s\n", argv[0]);
889 return HANDLER_RET_USAGE;
890 }
891
892 ret = toggle_tid_param(argv[0], argv[1], msg,
893 NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
894 if (ret)
895 return ret;
896
897 argc -= 2;
898 argv += 2;
899 } else if (strcmp(argv[0], "amsdu") == 0) {
900 if (argc < 2) {
901 fprintf(stderr, "not enough args for %s\n", argv[0]);
902 return HANDLER_RET_USAGE;
903 }
904
905 ret = toggle_tid_param(argv[0], argv[1], msg,
906 NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
907 if (ret)
908 return ret;
909
910 argc -= 2;
911 argv += 2;
912 } else if (strcmp(argv[0], "noack") == 0) {
913 if (argc < 2) {
914 fprintf(stderr, "not enough args for %s\n", argv[0]);
915 return HANDLER_RET_USAGE;
916 }
917
918 ret = toggle_tid_param(argv[0], argv[1], msg,
919 NL80211_TID_CONFIG_ATTR_NOACK);
920 if (ret)
921 return ret;
922
923 argc -= 2;
924 argv += 2;
925 } else if (strcmp(argv[0], "rtscts") == 0) {
926 if (argc < 2) {
927 fprintf(stderr, "not enough args for %s\n", argv[0]);
928 return HANDLER_RET_USAGE;
929 }
930
931 ret = toggle_tid_param(argv[0], argv[1], msg,
932 NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
933 if (ret)
934 return ret;
935
936 argc -= 2;
937 argv += 2;
938 } else if (strcmp(argv[0], "sretry") == 0) {
939 if (argc < 2) {
940 fprintf(stderr, "not enough args for %s\n", argv[0]);
941 return HANDLER_RET_USAGE;
942 }
943
944 NLA_PUT_U8(msg, NL80211_TID_CONFIG_ATTR_RETRY_SHORT, strtol(argv[1], &end, 0));
945 if (*end) {
946 fprintf(stderr, "Invalid short_retry value: %s\n", argv[1]);
947 return 2;
948 }
949
950 argc -= 2;
951 argv += 2;
952 } else if (strcmp(argv[0], "lretry") == 0) {
953 if (argc < 2) {
954 fprintf(stderr, "not enough args for %s\n", argv[0]);
955 return HANDLER_RET_USAGE;
956 }
957
958 NLA_PUT_U8(msg, NL80211_TID_CONFIG_ATTR_RETRY_LONG, strtol(argv[1], &end, 0));
959 if (*end) {
960 fprintf(stderr, "Invalid long_retry value: %s\n", argv[1]);
961 return 2;
962 }
963
964 argc -= 2;
965 argv += 2;
4d2749a6
TC
966 } else if (strcmp(argv[0], "bitrates") == 0) {
967 if (argc < 2) {
968 fprintf(stderr, "not enough args for %s\n", argv[0]);
969 return HANDLER_RET_USAGE;
970 }
971 if (!strcmp(argv[1], "auto"))
972 txrate_type = NL80211_TX_RATE_AUTOMATIC;
973 else if (!strcmp(argv[1], "fixed"))
974 txrate_type = NL80211_TX_RATE_FIXED;
975 else if (!strcmp(argv[1], "limit"))
976 txrate_type = NL80211_TX_RATE_LIMITED;
977 else {
978 printf("Invalid parameter: %s\n", argv[0]);
979 return 2;
980 }
981 NLA_PUT_U8(msg, NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE, txrate_type);
982 argc -= 2;
983 argv += 2;
984 if (txrate_type != NL80211_TX_RATE_AUTOMATIC) {
985 attr = NL80211_TID_CONFIG_ATTR_TX_RATE;
986 ret = set_bitrates(msg, argc, argv,
987 attr);
988 if (ret < 2)
989 return 1;
990
991 argc -= ret;
992 argv += ret;
993 }
71ad41cd
SM
994 } else {
995 fprintf(stderr, "Unknown parameter: %s\n", argv[0]);
996 return HANDLER_RET_USAGE;
997 }
998
999 break;
1000 default:
1001 fprintf(stderr, "Failed to parse: internal failure\n");
1002 return HANDLER_RET_USAGE;
1003 }
1004 }
1005
1006 if (tids_entry)
1007 nla_nest_end(msg, tids_entry);
1008
1009 if (tids_array)
1010 nla_nest_end(msg, tids_array);
1011
1012 return 0;
1013
1014nla_put_failure:
1015 return -ENOBUFS;
1016}
1017
1018COMMAND(set, tidconf, "[peer <MAC address>] tids <mask> [override] [sretry <num>] [lretry <num>] "
4d2749a6
TC
1019 "[ampdu [on|off]] [amsdu [on|off]] [noack [on|off]] [rtscts [on|off]]"
1020 "[bitrates <type [auto|fixed|limit]> [legacy-<2.4|5> <legacy rate in Mbps>*] [ht-mcs-<2.4|5> <MCS index>*]"
1021 " [vht-mcs-<2.4|5> <NSS:MCSx,MCSy... | NSS:MCSx-MCSy>*] [sgi-2.4|lgi-2.4] [sgi-5|lgi-5]]",
71ad41cd
SM
1022 NL80211_CMD_SET_TID_CONFIG, 0, CIB_NETDEV, handle_tid_config,
1023 "Setup per-node TID specific configuration for TIDs selected by bitmask.\n"
1024 "If MAC address is not specified, then supplied TID configuration\n"
1025 "applied to all the peers.\n"
1026 "Examples:\n"
e90b693b
TC
1027 " $ iw dev wlan0 set tidconf tids 0x1 ampdu off\n"
1028 " $ iw dev wlan0 set tidconf tids 0x5 ampdu off amsdu off rtscts on\n"
1029 " $ iw dev wlan0 set tidconf tids 0x3 override ampdu on noack on rtscts on\n"
1030 " $ iw dev wlan0 set tidconf peer xx:xx:xx:xx:xx:xx tids 0x1 ampdu off tids 0x3 amsdu off rtscts on\n"
4d2749a6
TC
1031 " $ iw dev wlan0 set tidconf peer xx:xx:xx:xx:xx:xx tids 0x2 bitrates auto\n"
1032 " $ iw dev wlan0 set tidconf peer xx:xx:xx:xx:xx:xx tids 0x2 bitrates limit vht-mcs-5 4:9\n"
71ad41cd 1033 );