xref: /aosp_15_r20/external/iw/interface.c (revision 92022041c981f431db0b590d0c3272306d0ea2a2)
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 
parse_mumimo_options(int * _argc,char *** _argv,struct nl_msg * msg)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 
parse_mntr_flags(int * _argc,char *** _argv,struct nl_msg * msg)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 */
get_if_type(int * argc,char *** argv,enum nl80211_iftype * type,bool need_type)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 
parse_4addr_flag(const char * value,struct nl_msg * msg)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 
handle_interface_add(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)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 
handle_interface_del(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)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 
channel_type_name(enum nl80211_channel_type channel_type)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 
channel_width_name(enum nl80211_chan_width width)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 	case NL80211_CHAN_WIDTH_320:
366 		return "320 MHz";
367 	default:
368 		return "unknown";
369 	}
370 }
371 
print_iface_handler(struct nl_msg * msg,void * arg)372 static int print_iface_handler(struct nl_msg *msg, void *arg)
373 {
374 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
375 	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
376 	unsigned int *wiphy = arg;
377 	const char *indent = "";
378 
379 	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
380 		  genlmsg_attrlen(gnlh, 0), NULL);
381 
382 	if (wiphy && tb_msg[NL80211_ATTR_WIPHY]) {
383 		unsigned int thiswiphy = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]);
384 		indent = "\t";
385 		if (*wiphy != thiswiphy)
386 			printf("phy#%d\n", thiswiphy);
387 		*wiphy = thiswiphy;
388 	}
389 
390 	if (tb_msg[NL80211_ATTR_IFNAME])
391 		printf("%sInterface %s\n", indent, nla_get_string(tb_msg[NL80211_ATTR_IFNAME]));
392 	else
393 		printf("%sUnnamed/non-netdev interface\n", indent);
394 	if (tb_msg[NL80211_ATTR_IFINDEX])
395 		printf("%s\tifindex %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX]));
396 	if (tb_msg[NL80211_ATTR_WDEV])
397 		printf("%s\twdev 0x%llx\n", indent,
398 		       (unsigned long long)nla_get_u64(tb_msg[NL80211_ATTR_WDEV]));
399 	if (tb_msg[NL80211_ATTR_MAC]) {
400 		char mac_addr[20];
401 		mac_addr_n2a(mac_addr, nla_data(tb_msg[NL80211_ATTR_MAC]));
402 		printf("%s\taddr %s\n", indent, mac_addr);
403 	}
404 	if (tb_msg[NL80211_ATTR_SSID]) {
405 		printf("%s\tssid ", indent);
406 		print_ssid_escaped(nla_len(tb_msg[NL80211_ATTR_SSID]),
407 				   nla_data(tb_msg[NL80211_ATTR_SSID]));
408 		printf("\n");
409 	}
410 	if (tb_msg[NL80211_ATTR_IFTYPE])
411 		printf("%s\ttype %s\n", indent, iftype_name(nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE])));
412 	if (!wiphy && tb_msg[NL80211_ATTR_WIPHY])
413 		printf("%s\twiphy %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]));
414 	if (tb_msg[NL80211_ATTR_WIPHY_FREQ]) {
415 		uint32_t freq = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_FREQ]);
416 
417 		printf("%s\tchannel %d (%d MHz)", indent,
418 		       ieee80211_frequency_to_channel(freq), freq);
419 
420 		if (tb_msg[NL80211_ATTR_CHANNEL_WIDTH]) {
421 			printf(", width: %s",
422 				channel_width_name(nla_get_u32(tb_msg[NL80211_ATTR_CHANNEL_WIDTH])));
423 			if (tb_msg[NL80211_ATTR_CENTER_FREQ1])
424 				printf(", center1: %d MHz",
425 					nla_get_u32(tb_msg[NL80211_ATTR_CENTER_FREQ1]));
426 			if (tb_msg[NL80211_ATTR_CENTER_FREQ2])
427 				printf(", center2: %d MHz",
428 					nla_get_u32(tb_msg[NL80211_ATTR_CENTER_FREQ2]));
429 		} else if (tb_msg[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
430 			enum nl80211_channel_type channel_type;
431 
432 			channel_type = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
433 			printf(" %s", channel_type_name(channel_type));
434 		}
435 
436 		printf("\n");
437 	}
438 
439 	if (tb_msg[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]) {
440 		int32_t txp = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]);
441 
442 		printf("%s\ttxpower %d.%.2d dBm\n",
443 		       indent, txp / 100, txp % 100);
444 	}
445 
446 	if (tb_msg[NL80211_ATTR_TXQ_STATS]) {
447 		char buf[150];
448 		parse_txq_stats(buf, sizeof(buf), tb_msg[NL80211_ATTR_TXQ_STATS], 1, -1, indent);
449 		printf("%s\tmulticast TXQ:%s\n", indent, buf);
450 	}
451 
452 	if (tb_msg[NL80211_ATTR_4ADDR]) {
453 		uint8_t use_4addr = nla_get_u8(tb_msg[NL80211_ATTR_4ADDR]);
454 		if (use_4addr)
455 			printf("%s\t4addr: on\n", indent);
456 	}
457 
458 	return NL_SKIP;
459 }
460 
handle_interface_info(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)461 static int handle_interface_info(struct nl80211_state *state,
462 				 struct nl_msg *msg,
463 				 int argc, char **argv,
464 				 enum id_input id)
465 {
466 	register_handler(print_iface_handler, NULL);
467 	return 0;
468 }
469 TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info,
470 	 "Show information for this interface.");
471 
handle_interface_set(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)472 static int handle_interface_set(struct nl80211_state *state,
473 				struct nl_msg *msg,
474 				int argc, char **argv,
475 				enum id_input id)
476 {
477 	if (!argc)
478 		return 1;
479 
480 	NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR);
481 
482 	switch (parse_mntr_flags(&argc, &argv, msg)) {
483 	case 0:
484 		return 0;
485 	case 1:
486 		return 1;
487 	case -ENOMEM:
488 		fprintf(stderr, "failed to allocate flags\n");
489 		return 2;
490 	case -EINVAL:
491 		fprintf(stderr, "unknown flag %s\n", *argv);
492 		return 2;
493 	default:
494 		return 2;
495 	}
496  nla_put_failure:
497 	return -ENOBUFS;
498 }
499 COMMAND(set, monitor, "<flag>*",
500 	NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_set,
501 	"Set monitor flags. Valid flags are:\n"
502 	VALID_FLAGS);
503 
handle_interface_meshid(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)504 static int handle_interface_meshid(struct nl80211_state *state,
505 				   struct nl_msg *msg,
506 				   int argc, char **argv,
507 				   enum id_input id)
508 {
509 	char *mesh_id = NULL;
510 
511 	if (argc != 1)
512 		return 1;
513 
514 	mesh_id = argv[0];
515 
516 	NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
517 
518 	return 0;
519  nla_put_failure:
520 	return -ENOBUFS;
521 }
522 COMMAND(set, meshid, "<meshid>",
523 	NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_meshid, NULL);
524 
525 static unsigned int dev_dump_wiphy;
526 
handle_dev_dump(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)527 static int handle_dev_dump(struct nl80211_state *state,
528 			   struct nl_msg *msg,
529 			   int argc, char **argv,
530 			   enum id_input id)
531 {
532 	dev_dump_wiphy = -1;
533 	register_handler(print_iface_handler, &dev_dump_wiphy);
534 	return 0;
535 }
536 TOPLEVEL(dev, NULL, NL80211_CMD_GET_INTERFACE, NLM_F_DUMP, CIB_NONE, handle_dev_dump,
537 	 "List all network interfaces for wireless hardware.");
538 
handle_interface_type(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)539 static int handle_interface_type(struct nl80211_state *state,
540 				 struct nl_msg *msg,
541 				 int argc, char **argv,
542 				 enum id_input id)
543 {
544 	enum nl80211_iftype type;
545 	int tpset;
546 
547 	tpset = get_if_type(&argc, &argv, &type, false);
548 	if (tpset)
549 		return tpset;
550 
551 	if (argc)
552 		return 1;
553 
554 	NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
555 
556 	return 0;
557  nla_put_failure:
558 	return -ENOBUFS;
559 }
560 COMMAND(set, type, "<type>",
561 	NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_type,
562 	"Set interface type/mode.\n"
563 	IFACE_TYPES);
564 
handle_interface_4addr(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)565 static int handle_interface_4addr(struct nl80211_state *state,
566 				  struct nl_msg *msg,
567 				  int argc, char **argv,
568 				  enum id_input id)
569 {
570 	if (argc != 1)
571 		return 1;
572 	return parse_4addr_flag(argv[0], msg);
573 }
574 COMMAND(set, 4addr, "<on|off>",
575 	NL80211_CMD_SET_INTERFACE, 0, CIB_NETDEV, handle_interface_4addr,
576 	"Set interface 4addr (WDS) mode.");
577 
handle_interface_noack_map(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)578 static int handle_interface_noack_map(struct nl80211_state *state,
579 				      struct nl_msg *msg,
580 				      int argc, char **argv,
581 				      enum id_input id)
582 {
583 	uint16_t noack_map;
584 	char *end;
585 
586 	if (argc != 1)
587 		return 1;
588 
589 	noack_map = strtoul(argv[0], &end, 16);
590 	if (*end)
591 		return 1;
592 
593 	NLA_PUT_U16(msg, NL80211_ATTR_NOACK_MAP, noack_map);
594 
595 	return 0;
596  nla_put_failure:
597 	return -ENOBUFS;
598 
599 }
600 COMMAND(set, noack_map, "<map>",
601 	NL80211_CMD_SET_NOACK_MAP, 0, CIB_NETDEV, handle_interface_noack_map,
602 	"Set the NoAck map for the TIDs. (0x0009 = BE, 0x0006 = BK, 0x0030 = VI, 0x00C0 = VO)");
603 
604 
handle_interface_wds_peer(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)605 static int handle_interface_wds_peer(struct nl80211_state *state,
606 				     struct nl_msg *msg,
607 				     int argc, char **argv,
608 				     enum id_input id)
609 {
610 	unsigned char mac_addr[ETH_ALEN];
611 
612 	if (argc < 1)
613 		return 1;
614 
615 	if (mac_addr_a2n(mac_addr, argv[0])) {
616 		fprintf(stderr, "Invalid MAC address\n");
617 		return 2;
618 	}
619 
620 	argc--;
621 	argv++;
622 
623 	if (argc)
624 		return 1;
625 
626 	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
627 
628 	return 0;
629  nla_put_failure:
630 	return -ENOBUFS;
631 }
632 COMMAND(set, peer, "<MAC address>",
633 	NL80211_CMD_SET_WDS_PEER, 0, CIB_NETDEV, handle_interface_wds_peer,
634 	"Set interface WDS peer.");
635 
set_mcast_rate(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)636 static int set_mcast_rate(struct nl80211_state *state,
637 			  struct nl_msg *msg,
638 			  int argc, char **argv,
639 			  enum id_input id)
640 {
641 	float rate;
642 	char *end;
643 
644 	if (argc != 1)
645 		return 1;
646 
647 	rate = strtod(argv[0], &end);
648 	if (*end != '\0')
649 		return 1;
650 
651 	NLA_PUT_U32(msg, NL80211_ATTR_MCAST_RATE, (int)(rate * 10));
652 
653 	return 0;
654 nla_put_failure:
655 	return -ENOBUFS;
656 }
657 
658 COMMAND(set, mcast_rate, "<rate in Mbps>",
659 	NL80211_CMD_SET_MCAST_RATE, 0, CIB_NETDEV, set_mcast_rate,
660 	"Set the multicast bitrate.");
661 
662 
handle_chanfreq(struct nl80211_state * state,struct nl_msg * msg,bool chan,int argc,char ** argv,enum id_input id)663 static int handle_chanfreq(struct nl80211_state *state, struct nl_msg *msg,
664 			   bool chan, int argc, char **argv,
665 			   enum id_input id)
666 {
667 	struct chandef chandef;
668 	int res;
669 	int parsed;
670 	char *end;
671 
672 	res = parse_freqchan(&chandef, chan, argc, argv, &parsed);
673 	if (res)
674 		return res;
675 
676 	argc -= parsed;
677 	argv += parsed;
678 
679 	while (argc) {
680 		unsigned int beacons = 10;
681 
682 		if (strcmp(argv[0], "beacons") == 0) {
683 			if (argc < 2)
684 				return 1;
685 
686 			beacons = strtol(argv[1], &end, 10);
687 			if (*end)
688 				return 1;
689 
690 			argc -= 2;
691 			argv += 2;
692 
693 			NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, beacons);
694 		} else if (strcmp(argv[0], "block-tx") == 0) {
695 			argc -= 1;
696 			argv += 1;
697 
698 			NLA_PUT_FLAG(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX);
699 		} else {
700 			return 1;
701 		}
702 	}
703 
704 	return put_chandef(msg, &chandef);
705 
706  nla_put_failure:
707 	return -ENOBUFS;
708 }
709 
handle_freq(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)710 static int handle_freq(struct nl80211_state *state, struct nl_msg *msg,
711 		       int argc, char **argv,
712 		       enum id_input id)
713 {
714 	return handle_chanfreq(state, msg, false, argc, argv, id);
715 }
716 
handle_chan(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)717 static int handle_chan(struct nl80211_state *state, struct nl_msg *msg,
718 		       int argc, char **argv,
719 		       enum id_input id)
720 {
721 	return handle_chanfreq(state, msg, true, argc, argv, id);
722 }
723 
724 SECTION(switch);
725 COMMAND(switch, freq,
726 	"<freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz] [beacons <count>] [block-tx]\n"
727 	"<control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]] [beacons <count>] [block-tx]",
728 	NL80211_CMD_CHANNEL_SWITCH, 0, CIB_NETDEV, handle_freq,
729 	"Switch the operating channel by sending a channel switch announcement (CSA).");
730 COMMAND(switch, channel, "<channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz] [beacons <count>] [block-tx]",
731 	NL80211_CMD_CHANNEL_SWITCH, 0, CIB_NETDEV, handle_chan, NULL);
732 
733 
toggle_tid_param(const char * argv0,const char * argv1,struct nl_msg * msg,uint32_t attr)734 static int toggle_tid_param(const char *argv0, const char *argv1,
735 			    struct nl_msg *msg, uint32_t attr)
736 {
737 	uint8_t val;
738 
739 	if (strcmp(argv1, "on") == 0) {
740 		val = NL80211_TID_CONFIG_ENABLE;
741 	} else if (strcmp(argv1, "off") == 0) {
742 		val = NL80211_TID_CONFIG_DISABLE;
743 	} else {
744 		fprintf(stderr, "Invalid %s parameter: %s\n", argv0, argv1);
745 		return 2;
746 	}
747 
748 	NLA_PUT_U8(msg, attr, val);
749 	return 0;
750 
751  nla_put_failure:
752 	return -ENOBUFS;
753 }
754 
handle_tid_config(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)755 static int handle_tid_config(struct nl80211_state *state,
756 			     struct nl_msg *msg,
757 			     int argc, char **argv,
758 			     enum id_input id)
759 {
760 	struct nlattr *tids_array = NULL;
761 	struct nlattr *tids_entry = NULL;
762 	enum nl80211_tx_rate_setting txrate_type;
763 	unsigned char peer[ETH_ALEN];
764 	int tids_num = 0;
765 	char *end;
766 	int ret;
767 	enum {
768 		PS_ADDR,
769 		PS_TIDS,
770 		PS_CONF,
771 	} parse_state = PS_ADDR;
772 	unsigned int attr;
773 
774 	while (argc) {
775 		switch (parse_state) {
776 		case PS_ADDR:
777 			if (strcmp(argv[0], "peer") == 0) {
778 				if (argc < 2) {
779 					fprintf(stderr, "Not enough args for %s\n", argv[0]);
780 					return HANDLER_RET_USAGE;
781 				}
782 
783 				if (mac_addr_a2n(peer, argv[1])) {
784 					fprintf(stderr, "Invalid MAC address\n");
785 					return 2;
786 				}
787 
788 				NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
789 
790 				argc -= 2;
791 				argv += 2;
792 				parse_state = PS_TIDS;
793 
794 			} else if (strcmp(argv[0], "tids") == 0) {
795 				parse_state = PS_TIDS;
796 			} else {
797 				fprintf(stderr, "Peer MAC address expected\n");
798 				return HANDLER_RET_USAGE;
799 			}
800 
801 			break;
802 		case PS_TIDS:
803 			if (strcmp(argv[0], "tids") == 0) {
804 				if (argc < 2) {
805 					fprintf(stderr, "not enough args for %s\n", argv[0]);
806 					return HANDLER_RET_USAGE;
807 				}
808 
809 				if (!tids_array) {
810 					tids_array = nla_nest_start(msg, NL80211_ATTR_TID_CONFIG);
811 					if (!tids_array)
812 						return -ENOBUFS;
813 				}
814 
815 				if (tids_entry) {
816 					nla_nest_end(msg, tids_entry);
817 					tids_num++;
818 				}
819 
820 				tids_entry = nla_nest_start(msg, tids_num);
821 				if (!tids_entry)
822 					return -ENOBUFS;
823 
824 				NLA_PUT_U16(msg, NL80211_TID_CONFIG_ATTR_TIDS, strtol(argv[1], &end, 0));
825 				if (*end) {
826 					fprintf(stderr, "Invalid TID mask value: %s\n", argv[1]);
827 					return 2;
828 				}
829 
830 				argc -= 2;
831 				argv += 2;
832 				parse_state = PS_CONF;
833 			} else {
834 				fprintf(stderr, "TID mask expected\n");
835 				return HANDLER_RET_USAGE;
836 			}
837 
838 			break;
839 		case PS_CONF:
840 			if (strcmp(argv[0], "tids") == 0) {
841 				parse_state = PS_TIDS;
842 			} else if (strcmp(argv[0], "override") == 0) {
843 				NLA_PUT_FLAG(msg, NL80211_TID_CONFIG_ATTR_OVERRIDE);
844 
845 				argc -= 1;
846 				argv += 1;
847 			} else if (strcmp(argv[0], "ampdu") == 0) {
848 				if (argc < 2) {
849 					fprintf(stderr, "not enough args for %s\n", argv[0]);
850 					return HANDLER_RET_USAGE;
851 				}
852 
853 				ret = toggle_tid_param(argv[0], argv[1], msg,
854 						       NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
855 				if (ret)
856 					return ret;
857 
858 				argc -= 2;
859 				argv += 2;
860 			} else if (strcmp(argv[0], "amsdu") == 0) {
861 				if (argc < 2) {
862 					fprintf(stderr, "not enough args for %s\n", argv[0]);
863 					return HANDLER_RET_USAGE;
864 				}
865 
866 				ret = toggle_tid_param(argv[0], argv[1], msg,
867 						       NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
868 				if (ret)
869 					return ret;
870 
871 				argc -= 2;
872 				argv += 2;
873 			} else if (strcmp(argv[0], "noack") == 0) {
874 				if (argc < 2) {
875 					fprintf(stderr, "not enough args for %s\n", argv[0]);
876 					return HANDLER_RET_USAGE;
877 				}
878 
879 				ret = toggle_tid_param(argv[0], argv[1], msg,
880 						       NL80211_TID_CONFIG_ATTR_NOACK);
881 				if (ret)
882 					return ret;
883 
884 				argc -= 2;
885 				argv += 2;
886 			} else if (strcmp(argv[0], "rtscts") == 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_RTSCTS_CTRL);
894 				if (ret)
895 					return ret;
896 
897 				argc -= 2;
898 				argv += 2;
899 			} else if (strcmp(argv[0], "sretry") == 0) {
900 				if (argc < 2) {
901 					fprintf(stderr, "not enough args for %s\n", argv[0]);
902 					return HANDLER_RET_USAGE;
903 				}
904 
905 				NLA_PUT_U8(msg, NL80211_TID_CONFIG_ATTR_RETRY_SHORT, strtol(argv[1], &end, 0));
906 				if (*end) {
907 					fprintf(stderr, "Invalid short_retry value: %s\n", argv[1]);
908 					return 2;
909 				}
910 
911 				argc -= 2;
912 				argv += 2;
913 			} else if (strcmp(argv[0], "lretry") == 0) {
914 				if (argc < 2) {
915 					fprintf(stderr, "not enough args for %s\n", argv[0]);
916 					return HANDLER_RET_USAGE;
917 				}
918 
919 				NLA_PUT_U8(msg, NL80211_TID_CONFIG_ATTR_RETRY_LONG, strtol(argv[1], &end, 0));
920 				if (*end) {
921 					fprintf(stderr, "Invalid long_retry value: %s\n", argv[1]);
922 					return 2;
923 				}
924 
925 				argc -= 2;
926 				argv += 2;
927 			} else if (strcmp(argv[0], "bitrates") == 0) {
928 				if (argc < 2) {
929 					fprintf(stderr, "not enough args for %s\n", argv[0]);
930 					return HANDLER_RET_USAGE;
931 				}
932 				if (!strcmp(argv[1], "auto"))
933 					txrate_type = NL80211_TX_RATE_AUTOMATIC;
934 				else if (!strcmp(argv[1], "fixed"))
935 					txrate_type = NL80211_TX_RATE_FIXED;
936 				else if (!strcmp(argv[1], "limit"))
937 					txrate_type = NL80211_TX_RATE_LIMITED;
938 				else {
939 					printf("Invalid parameter: %s\n", argv[0]);
940 					return 2;
941 				}
942 				NLA_PUT_U8(msg, NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE, txrate_type);
943 				argc -= 2;
944 				argv += 2;
945 				if (txrate_type != NL80211_TX_RATE_AUTOMATIC) {
946 					attr = NL80211_TID_CONFIG_ATTR_TX_RATE;
947 					ret = set_bitrates(msg, argc, argv,
948 							   attr);
949 					if (ret < 2)
950 						return 1;
951 
952 					argc -= ret;
953 					argv += ret;
954 				}
955 			} else {
956 				fprintf(stderr, "Unknown parameter: %s\n", argv[0]);
957 				return HANDLER_RET_USAGE;
958 			}
959 
960 			break;
961 		default:
962 			fprintf(stderr, "Failed to parse: internal failure\n");
963 			return HANDLER_RET_USAGE;
964 		}
965 	}
966 
967 	if (tids_entry)
968 		nla_nest_end(msg, tids_entry);
969 
970 	if (tids_array)
971 		nla_nest_end(msg, tids_array);
972 
973 	return 0;
974 
975 nla_put_failure:
976 	return -ENOBUFS;
977 }
978 
979 COMMAND(set, tidconf, "[peer <MAC address>] tids <mask> [override] [sretry <num>] [lretry <num>] "
980 	"[ampdu [on|off]] [amsdu [on|off]] [noack [on|off]] [rtscts [on|off]]"
981 	"[bitrates <type [auto|fixed|limit]> [legacy-<2.4|5> <legacy rate in Mbps>*] [ht-mcs-<2.4|5> <MCS index>*]"
982 	" [vht-mcs-<2.4|5> <NSS:MCSx,MCSy... | NSS:MCSx-MCSy>*] [sgi-2.4|lgi-2.4] [sgi-5|lgi-5]]",
983 	NL80211_CMD_SET_TID_CONFIG, 0, CIB_NETDEV, handle_tid_config,
984 	"Setup per-node TID specific configuration for TIDs selected by bitmask.\n"
985 	"If MAC address is not specified, then supplied TID configuration\n"
986 	"applied to all the peers.\n"
987 	"Examples:\n"
988 	"  $ iw dev wlan0 set tidconf tids 0x1 ampdu off\n"
989 	"  $ iw dev wlan0 set tidconf tids 0x5 ampdu off amsdu off rtscts on\n"
990 	"  $ iw dev wlan0 set tidconf tids 0x3 override ampdu on noack on rtscts on\n"
991 	"  $ iw dev wlan0 set tidconf peer xx:xx:xx:xx:xx:xx tids 0x1 ampdu off tids 0x3 amsdu off rtscts on\n"
992 	"  $ iw dev wlan0 set tidconf peer xx:xx:xx:xx:xx:xx tids 0x2 bitrates auto\n"
993 	"  $ iw dev wlan0 set tidconf peer xx:xx:xx:xx:xx:xx tids 0x2 bitrates limit vht-mcs-5 4:9\n"
994 	);
995