]> git.ipfire.org Git - thirdparty/iw.git/blob - interface.c
iw: separate wait/print when waiting for an event
[thirdparty/iw.git] / interface.c
1 #include <errno.h>
2 #include <string.h>
3 #include <stdbool.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 #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"\
18 "cook: use cooked mode\n"\
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"
22
23 SECTION(interface);
24
25 static char *mntr_flags[NL80211_MNTR_FLAG_MAX + 1] = {
26 "none",
27 "fcsfail",
28 "plcpfail",
29 "control",
30 "otherbss",
31 "cook",
32 "active",
33 };
34
35 static 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
84 static 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;
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 */
108 for (flag = __NL80211_MNTR_FLAG_INVALID;
109 flag <= NL80211_MNTR_FLAG_MAX; flag++) {
110 if (strcmp(*argv, mntr_flags[flag]) == 0) {
111 ok = 1;
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 */
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
143 /* for help */
144 #define IFACE_TYPES "Valid interface types are: managed, ibss, monitor, mesh, wds."
145
146 /* return 0 if ok, internal error otherwise */
147 static int get_if_type(int *argc, char ***argv, enum nl80211_iftype *type,
148 bool need_type)
149 {
150 char *tpstr;
151
152 if (*argc < 1 + !!need_type)
153 return 1;
154
155 if (need_type && strcmp((*argv)[0], "type"))
156 return 1;
157
158 tpstr = (*argv)[!!need_type];
159 *argc -= 1 + !!need_type;
160 *argv += 1 + !!need_type;
161
162 if (strcmp(tpstr, "adhoc") == 0 ||
163 strcmp(tpstr, "ibss") == 0) {
164 *type = NL80211_IFTYPE_ADHOC;
165 return 0;
166 } else if (strcmp(tpstr, "ocb") == 0) {
167 *type = NL80211_IFTYPE_OCB;
168 return 0;
169 } else if (strcmp(tpstr, "monitor") == 0) {
170 *type = NL80211_IFTYPE_MONITOR;
171 return 0;
172 } else if (strcmp(tpstr, "master") == 0 ||
173 strcmp(tpstr, "ap") == 0) {
174 *type = NL80211_IFTYPE_UNSPECIFIED;
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");
178 return 2;
179 } else if (strcmp(tpstr, "__ap") == 0) {
180 *type = NL80211_IFTYPE_AP;
181 return 0;
182 } else if (strcmp(tpstr, "__ap_vlan") == 0) {
183 *type = NL80211_IFTYPE_AP_VLAN;
184 return 0;
185 } else if (strcmp(tpstr, "wds") == 0) {
186 *type = NL80211_IFTYPE_WDS;
187 return 0;
188 } else if (strcmp(tpstr, "managed") == 0 ||
189 strcmp(tpstr, "mgd") == 0 ||
190 strcmp(tpstr, "station") == 0) {
191 *type = NL80211_IFTYPE_STATION;
192 return 0;
193 } else if (strcmp(tpstr, "mp") == 0 ||
194 strcmp(tpstr, "mesh") == 0) {
195 *type = NL80211_IFTYPE_MESH_POINT;
196 return 0;
197 } else if (strcmp(tpstr, "__p2pcl") == 0) {
198 *type = NL80211_IFTYPE_P2P_CLIENT;
199 return 0;
200 } else if (strcmp(tpstr, "__p2pdev") == 0) {
201 *type = NL80211_IFTYPE_P2P_DEVICE;
202 return 0;
203 } else if (strcmp(tpstr, "__p2pgo") == 0) {
204 *type = NL80211_IFTYPE_P2P_GO;
205 return 0;
206 } else if (strcmp(tpstr, "__nan") == 0) {
207 *type = NL80211_IFTYPE_NAN;
208 return 0;
209 }
210
211 fprintf(stderr, "invalid interface type %s\n", tpstr);
212 return 2;
213 }
214
215 static 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
225 nla_put_failure:
226 return 1;
227 }
228
229 static int handle_interface_add(struct nl80211_state *state,
230 struct nl_msg *msg,
231 int argc, char **argv,
232 enum id_input id)
233 {
234 char *name;
235 char *mesh_id = NULL;
236 enum nl80211_iftype type;
237 int tpset;
238 unsigned char mac_addr[ETH_ALEN];
239 int found_mac = 0;
240
241 if (argc < 1)
242 return 1;
243
244 name = argv[0];
245 argc--;
246 argv++;
247
248 tpset = get_if_type(&argc, &argv, &type, true);
249 if (tpset)
250 return tpset;
251
252 try_another:
253 if (argc) {
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++;
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;
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++;
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 {
291 return 1;
292 }
293 }
294
295 if (argc)
296 return 1;
297
298 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name);
299 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
300 if (mesh_id)
301 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
302 if (found_mac)
303 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
304
305 return 0;
306 nla_put_failure:
307 return -ENOBUFS;
308 }
309 COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*] [addr <mac-addr>]",
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.");
316 COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*] [addr <mac-addr>]",
317 NL80211_CMD_NEW_INTERFACE, 0, CIB_NETDEV, handle_interface_add, NULL);
318
319 static int handle_interface_del(struct nl80211_state *state,
320 struct nl_msg *msg,
321 int argc, char **argv,
322 enum id_input id)
323 {
324 return 0;
325 }
326 TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del,
327 "Remove this virtual interface");
328 HIDDEN(interface, del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del);
329
330 static 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
346 char *channel_width_name(enum nl80211_chan_width width)
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";
361 case NL80211_CHAN_WIDTH_5:
362 return "5 MHz";
363 case NL80211_CHAN_WIDTH_10:
364 return "10 MHz";
365 default:
366 return "unknown";
367 }
368 }
369
370 static int print_iface_handler(struct nl_msg *msg, void *arg)
371 {
372 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
373 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
374 unsigned int *wiphy = arg;
375 const char *indent = "";
376
377 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
378 genlmsg_attrlen(gnlh, 0), NULL);
379
380 if (wiphy && tb_msg[NL80211_ATTR_WIPHY]) {
381 unsigned int thiswiphy = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]);
382 indent = "\t";
383 if (*wiphy != thiswiphy)
384 printf("phy#%d\n", thiswiphy);
385 *wiphy = thiswiphy;
386 }
387
388 if (tb_msg[NL80211_ATTR_IFNAME])
389 printf("%sInterface %s\n", indent, nla_get_string(tb_msg[NL80211_ATTR_IFNAME]));
390 else
391 printf("%sUnnamed/non-netdev interface\n", indent);
392 if (tb_msg[NL80211_ATTR_IFINDEX])
393 printf("%s\tifindex %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX]));
394 if (tb_msg[NL80211_ATTR_WDEV])
395 printf("%s\twdev 0x%llx\n", indent,
396 (unsigned long long)nla_get_u64(tb_msg[NL80211_ATTR_WDEV]));
397 if (tb_msg[NL80211_ATTR_MAC]) {
398 char mac_addr[20];
399 mac_addr_n2a(mac_addr, nla_data(tb_msg[NL80211_ATTR_MAC]));
400 printf("%s\taddr %s\n", indent, mac_addr);
401 }
402 if (tb_msg[NL80211_ATTR_SSID]) {
403 printf("%s\tssid ", indent);
404 print_ssid_escaped(nla_len(tb_msg[NL80211_ATTR_SSID]),
405 nla_data(tb_msg[NL80211_ATTR_SSID]));
406 printf("\n");
407 }
408 if (tb_msg[NL80211_ATTR_IFTYPE])
409 printf("%s\ttype %s\n", indent, iftype_name(nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE])));
410 if (!wiphy && tb_msg[NL80211_ATTR_WIPHY])
411 printf("%s\twiphy %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]));
412 if (tb_msg[NL80211_ATTR_WIPHY_FREQ]) {
413 uint32_t freq = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_FREQ]);
414
415 printf("%s\tchannel %d (%d MHz)", indent,
416 ieee80211_frequency_to_channel(freq), freq);
417
418 if (tb_msg[NL80211_ATTR_CHANNEL_WIDTH]) {
419 printf(", width: %s",
420 channel_width_name(nla_get_u32(tb_msg[NL80211_ATTR_CHANNEL_WIDTH])));
421 if (tb_msg[NL80211_ATTR_CENTER_FREQ1])
422 printf(", center1: %d MHz",
423 nla_get_u32(tb_msg[NL80211_ATTR_CENTER_FREQ1]));
424 if (tb_msg[NL80211_ATTR_CENTER_FREQ2])
425 printf(", center2: %d MHz",
426 nla_get_u32(tb_msg[NL80211_ATTR_CENTER_FREQ2]));
427 } else if (tb_msg[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
428 enum nl80211_channel_type channel_type;
429
430 channel_type = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
431 printf(" %s", channel_type_name(channel_type));
432 }
433
434 printf("\n");
435 }
436
437 if (tb_msg[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]) {
438 uint32_t txp = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]);
439
440 printf("%s\ttxpower %d.%.2d dBm\n",
441 indent, txp / 100, txp % 100);
442 }
443
444 if (tb_msg[NL80211_ATTR_TXQ_STATS]) {
445 char buf[150];
446 parse_txq_stats(buf, sizeof(buf), tb_msg[NL80211_ATTR_TXQ_STATS], 1, -1, indent);
447 printf("%s\tmulticast TXQ:%s\n", indent, buf);
448 }
449
450 return NL_SKIP;
451 }
452
453 static int handle_interface_info(struct nl80211_state *state,
454 struct nl_msg *msg,
455 int argc, char **argv,
456 enum id_input id)
457 {
458 register_handler(print_iface_handler, NULL);
459 return 0;
460 }
461 TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info,
462 "Show information for this interface.");
463
464 static int handle_interface_set(struct nl80211_state *state,
465 struct nl_msg *msg,
466 int argc, char **argv,
467 enum id_input id)
468 {
469 if (!argc)
470 return 1;
471
472 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
473
474 switch (parse_mntr_flags(&argc, &argv, msg)) {
475 case 0:
476 return 0;
477 case 1:
478 return 1;
479 case -ENOMEM:
480 fprintf(stderr, "failed to allocate flags\n");
481 return 2;
482 case -EINVAL:
483 fprintf(stderr, "unknown flag %s\n", *argv);
484 return 2;
485 default:
486 return 2;
487 }
488 nla_put_failure:
489 return -ENOBUFS;
490 }
491 COMMAND(set, monitor, "<flag>*",
492 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_set,
493 "Set monitor flags. Valid flags are:\n"
494 VALID_FLAGS);
495
496 static int handle_interface_meshid(struct nl80211_state *state,
497 struct nl_msg *msg,
498 int argc, char **argv,
499 enum id_input id)
500 {
501 char *mesh_id = NULL;
502
503 if (argc != 1)
504 return 1;
505
506 mesh_id = argv[0];
507
508 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
509
510 return 0;
511 nla_put_failure:
512 return -ENOBUFS;
513 }
514 COMMAND(set, meshid, "<meshid>",
515 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_meshid, NULL);
516
517 static unsigned int dev_dump_wiphy;
518
519 static int handle_dev_dump(struct nl80211_state *state,
520 struct nl_msg *msg,
521 int argc, char **argv,
522 enum id_input id)
523 {
524 dev_dump_wiphy = -1;
525 register_handler(print_iface_handler, &dev_dump_wiphy);
526 return 0;
527 }
528 TOPLEVEL(dev, NULL, NL80211_CMD_GET_INTERFACE, NLM_F_DUMP, CIB_NONE, handle_dev_dump,
529 "List all network interfaces for wireless hardware.");
530
531 static int handle_interface_type(struct nl80211_state *state,
532 struct nl_msg *msg,
533 int argc, char **argv,
534 enum id_input id)
535 {
536 enum nl80211_iftype type;
537 int tpset;
538
539 tpset = get_if_type(&argc, &argv, &type, false);
540 if (tpset)
541 return tpset;
542
543 if (argc)
544 return 1;
545
546 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
547
548 return 0;
549 nla_put_failure:
550 return -ENOBUFS;
551 }
552 COMMAND(set, type, "<type>",
553 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_type,
554 "Set interface type/mode.\n"
555 IFACE_TYPES);
556
557 static int handle_interface_4addr(struct nl80211_state *state,
558 struct nl_msg *msg,
559 int argc, char **argv,
560 enum id_input id)
561 {
562 if (argc != 1)
563 return 1;
564 return parse_4addr_flag(argv[0], msg);
565 }
566 COMMAND(set, 4addr, "<on|off>",
567 NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_4addr,
568 "Set interface 4addr (WDS) mode.");
569
570 static int handle_interface_noack_map(struct nl80211_state *state,
571 struct nl_msg *msg,
572 int argc, char **argv,
573 enum id_input id)
574 {
575 uint16_t noack_map;
576 char *end;
577
578 if (argc != 1)
579 return 1;
580
581 noack_map = strtoul(argv[0], &end, 16);
582 if (*end)
583 return 1;
584
585 NLA_PUT_U16(msg, NL80211_ATTR_NOACK_MAP, noack_map);
586
587 return 0;
588 nla_put_failure:
589 return -ENOBUFS;
590
591 }
592 COMMAND(set, noack_map, "<map>",
593 NL80211_CMD_SET_NOACK_MAP, 0, CIB_NETDEV, handle_interface_noack_map,
594 "Set the NoAck map for the TIDs. (0x0009 = BE, 0x0006 = BK, 0x0030 = VI, 0x00C0 = VO)");
595
596
597 static int handle_interface_wds_peer(struct nl80211_state *state,
598 struct nl_msg *msg,
599 int argc, char **argv,
600 enum id_input id)
601 {
602 unsigned char mac_addr[ETH_ALEN];
603
604 if (argc < 1)
605 return 1;
606
607 if (mac_addr_a2n(mac_addr, argv[0])) {
608 fprintf(stderr, "Invalid MAC address\n");
609 return 2;
610 }
611
612 argc--;
613 argv++;
614
615 if (argc)
616 return 1;
617
618 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
619
620 return 0;
621 nla_put_failure:
622 return -ENOBUFS;
623 }
624 COMMAND(set, peer, "<MAC address>",
625 NL80211_CMD_SET_WDS_PEER, 0, CIB_NETDEV, handle_interface_wds_peer,
626 "Set interface WDS peer.");
627
628 static int set_mcast_rate(struct nl80211_state *state,
629 struct nl_msg *msg,
630 int argc, char **argv,
631 enum id_input id)
632 {
633 float rate;
634 char *end;
635
636 if (argc != 1)
637 return 1;
638
639 rate = strtod(argv[0], &end);
640 if (*end != '\0')
641 return 1;
642
643 NLA_PUT_U32(msg, NL80211_ATTR_MCAST_RATE, (int)(rate * 10));
644
645 return 0;
646 nla_put_failure:
647 return -ENOBUFS;
648 }
649
650 COMMAND(set, mcast_rate, "<rate in Mbps>",
651 NL80211_CMD_SET_MCAST_RATE, 0, CIB_NETDEV, set_mcast_rate,
652 "Set the multicast bitrate.");
653
654
655 static int handle_chanfreq(struct nl80211_state *state, struct nl_msg *msg,
656 bool chan, int argc, char **argv,
657 enum id_input id)
658 {
659 struct chandef chandef;
660 int res;
661 int parsed;
662 char *end;
663
664 res = parse_freqchan(&chandef, chan, argc, argv, &parsed);
665 if (res)
666 return res;
667
668 argc -= parsed;
669 argv += parsed;
670
671 while (argc) {
672 unsigned int beacons = 10;
673
674 if (strcmp(argv[0], "beacons") == 0) {
675 if (argc < 2)
676 return 1;
677
678 beacons = strtol(argv[1], &end, 10);
679 if (*end)
680 return 1;
681
682 argc -= 2;
683 argv += 2;
684
685 NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, beacons);
686 } else if (strcmp(argv[0], "block-tx") == 0) {
687 argc -= 1;
688 argv += 1;
689
690 NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
691 } else {
692 return 1;
693 }
694 }
695
696 return put_chandef(msg, &chandef);
697
698 nla_put_failure:
699 return -ENOBUFS;
700 }
701
702 static int handle_freq(struct nl80211_state *state, struct nl_msg *msg,
703 int argc, char **argv,
704 enum id_input id)
705 {
706 return handle_chanfreq(state, msg, false, argc, argv, id);
707 }
708
709 static int handle_chan(struct nl80211_state *state, struct nl_msg *msg,
710 int argc, char **argv,
711 enum id_input id)
712 {
713 return handle_chanfreq(state, msg, true, argc, argv, id);
714 }
715
716 SECTION(switch);
717 COMMAND(switch, freq,
718 "<freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz] [beacons <count>] [block-tx]\n"
719 "<control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]] [beacons <count>] [block-tx]",
720 NL80211_CMD_CHANNEL_SWITCH, 0, CIB_NETDEV, handle_freq,
721 "Switch the operating channel by sending a channel switch announcement (CSA).");
722 COMMAND(switch, channel, "<channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz] [beacons <count>] [block-tx]",
723 NL80211_CMD_CHANNEL_SWITCH, 0, CIB_NETDEV, handle_chan, NULL);