• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nl80211 userspace tool
3  *
4  * Copyright 2007, 2008	Johannes Berg <johannes@sipsolutions.net>
5  */
6 
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <net/if.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <stdbool.h>
16 #include <linux/netlink.h>
17 
18 #include <netlink/genl/genl.h>
19 #include <netlink/genl/family.h>
20 #include <netlink/genl/ctrl.h>
21 #include <netlink/msg.h>
22 #include <netlink/attr.h>
23 
24 #include "nl80211.h"
25 #include "iw.h"
26 
27 /* libnl 1.x compatibility code */
28 #if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30)
nl_socket_alloc(void)29 static inline struct nl_handle *nl_socket_alloc(void)
30 {
31 	return nl_handle_alloc();
32 }
33 
nl_socket_free(struct nl_sock * h)34 static inline void nl_socket_free(struct nl_sock *h)
35 {
36 	nl_handle_destroy(h);
37 }
38 
nl_socket_set_buffer_size(struct nl_sock * sk,int rxbuf,int txbuf)39 static inline int nl_socket_set_buffer_size(struct nl_sock *sk,
40 					    int rxbuf, int txbuf)
41 {
42 	return nl_set_buffer_size(sk, rxbuf, txbuf);
43 }
44 #endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 */
45 
46 int iw_debug = 0;
47 
nl80211_init(struct nl80211_state * state)48 static int nl80211_init(struct nl80211_state *state)
49 {
50 	int err;
51 
52 	state->nl_sock = nl_socket_alloc();
53 	if (!state->nl_sock) {
54 		fprintf(stderr, "Failed to allocate netlink socket.\n");
55 		return -ENOMEM;
56 	}
57 
58 	if (genl_connect(state->nl_sock)) {
59 		fprintf(stderr, "Failed to connect to generic netlink.\n");
60 		err = -ENOLINK;
61 		goto out_handle_destroy;
62 	}
63 
64 	nl_socket_set_buffer_size(state->nl_sock, 8192, 8192);
65 
66 	/* try to set NETLINK_EXT_ACK to 1, ignoring errors */
67 	err = 1;
68 	setsockopt(nl_socket_get_fd(state->nl_sock), SOL_NETLINK,
69 		   NETLINK_EXT_ACK, &err, sizeof(err));
70 
71 	state->nl80211_id = genl_ctrl_resolve(state->nl_sock, "nl80211");
72 	if (state->nl80211_id < 0) {
73 		fprintf(stderr, "nl80211 not found.\n");
74 		err = -ENOENT;
75 		goto out_handle_destroy;
76 	}
77 
78 	return 0;
79 
80  out_handle_destroy:
81 	nl_socket_free(state->nl_sock);
82 	return err;
83 }
84 
nl80211_cleanup(struct nl80211_state * state)85 static void nl80211_cleanup(struct nl80211_state *state)
86 {
87 	nl_socket_free(state->nl_sock);
88 }
89 
90 static int cmd_size;
91 
92 extern struct cmd *__start___cmd[];
93 extern struct cmd *__stop___cmd;
94 
95 #define for_each_cmd(_cmd, i)					\
96 	for (i = 0; i < &__stop___cmd - __start___cmd; i++)	\
97 		if ((_cmd = __start___cmd[i]))
98 
99 
__usage_cmd(const struct cmd * cmd,char * indent,bool full)100 static void __usage_cmd(const struct cmd *cmd, char *indent, bool full)
101 {
102 	const char *start, *lend, *end;
103 
104 	printf("%s", indent);
105 
106 	switch (cmd->idby) {
107 	case CIB_NONE:
108 		break;
109 	case CIB_PHY:
110 		printf("phy <phyname> ");
111 		break;
112 	case CIB_NETDEV:
113 		printf("dev <devname> ");
114 		break;
115 	case CIB_WDEV:
116 		printf("wdev <idx> ");
117 		break;
118 	}
119 	if (cmd->parent && cmd->parent->name)
120 		printf("%s ", cmd->parent->name);
121 	printf("%s", cmd->name);
122 
123 	if (cmd->args) {
124 		/* print line by line */
125 		start = cmd->args;
126 		end = strchr(start, '\0');
127 		printf(" ");
128 		do {
129 			lend = strchr(start, '\n');
130 			if (!lend)
131 				lend = end;
132 			if (start != cmd->args) {
133 				printf("\t");
134 				switch (cmd->idby) {
135 				case CIB_NONE:
136 					break;
137 				case CIB_PHY:
138 					printf("phy <phyname> ");
139 					break;
140 				case CIB_NETDEV:
141 					printf("dev <devname> ");
142 					break;
143 				case CIB_WDEV:
144 					printf("wdev <idx> ");
145 					break;
146 				}
147 				if (cmd->parent && cmd->parent->name)
148 					printf("%s ", cmd->parent->name);
149 				printf("%s ", cmd->name);
150 			}
151 			printf("%.*s\n", (int)(lend - start), start);
152 			start = lend + 1;
153 		} while (end != lend);
154 	} else
155 		printf("\n");
156 
157 	if (!full || !cmd->help)
158 		return;
159 
160 	/* hack */
161 	if (strlen(indent))
162 		indent = "\t\t";
163 	else
164 		printf("\n");
165 
166 	/* print line by line */
167 	start = cmd->help;
168 	end = strchr(start, '\0');
169 	do {
170 		lend = strchr(start, '\n');
171 		if (!lend)
172 			lend = end;
173 		printf("%s", indent);
174 		printf("%.*s\n", (int)(lend - start), start);
175 		start = lend + 1;
176 	} while (end != lend);
177 
178 	printf("\n");
179 }
180 
usage_options(void)181 static void usage_options(void)
182 {
183 	printf("Options:\n");
184 	printf("\t--debug\t\tenable netlink debugging\n");
185 }
186 
187 static const char *argv0;
188 
usage(int argc,char ** argv)189 static void usage(int argc, char **argv)
190 {
191 	const struct cmd *section, *cmd;
192 	bool full = argc >= 0;
193 	const char *sect_filt = NULL;
194 	const char *cmd_filt = NULL;
195 	unsigned int i, j;
196 
197 	if (argc > 0)
198 		sect_filt = argv[0];
199 
200 	if (argc > 1)
201 		cmd_filt = argv[1];
202 
203 	printf("Usage:\t%s [options] command\n", argv0);
204 	usage_options();
205 	printf("\t--version\tshow version (%s)\n", iw_version);
206 	printf("Commands:\n");
207 	for_each_cmd(section, i) {
208 		if (section->parent)
209 			continue;
210 
211 		if (sect_filt && strcmp(section->name, sect_filt))
212 			continue;
213 
214 		if (section->handler && !section->hidden)
215 			__usage_cmd(section, "\t", full);
216 
217 		for_each_cmd(cmd, j) {
218 			if (section != cmd->parent)
219 				continue;
220 			if (!cmd->handler || cmd->hidden)
221 				continue;
222 			if (cmd_filt && strcmp(cmd->name, cmd_filt))
223 				continue;
224 			__usage_cmd(cmd, "\t", full);
225 		}
226 	}
227 	printf("\nCommands that use the netdev ('dev') can also be given the\n"
228 	       "'wdev' instead to identify the device.\n");
229 	printf("\nYou can omit the 'phy' or 'dev' if "
230 			"the identification is unique,\n"
231 			"e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
232 			"(Don't when scripting.)\n\n"
233 			"Do NOT screenscrape this tool, we don't "
234 			"consider its output stable.\n\n");
235 }
236 
print_help(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)237 static int print_help(struct nl80211_state *state,
238 		      struct nl_msg *msg,
239 		      int argc, char **argv,
240 		      enum id_input id)
241 {
242 	exit(3);
243 }
244 TOPLEVEL(help, "[command]", 0, 0, CIB_NONE, print_help,
245 	 "Print usage for all or a specific command, e.g.\n"
246 	 "\"help wowlan\" or \"help wowlan enable\".");
247 
usage_cmd(const struct cmd * cmd)248 static void usage_cmd(const struct cmd *cmd)
249 {
250 	printf("Usage:\t%s [options] ", argv0);
251 	__usage_cmd(cmd, "", true);
252 	usage_options();
253 }
254 
version(void)255 static void version(void)
256 {
257 	printf("iw version %s\n", iw_version);
258 }
259 
phy_lookup(char * name)260 static int phy_lookup(char *name)
261 {
262 	char buf[200];
263 	int fd, pos;
264 
265 	snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
266 
267 	fd = open(buf, O_RDONLY);
268 	if (fd < 0)
269 		return -1;
270 	pos = read(fd, buf, sizeof(buf) - 1);
271 	if (pos < 0) {
272 		close(fd);
273 		return -1;
274 	}
275 	buf[pos] = '\0';
276 	close(fd);
277 	return atoi(buf);
278 }
279 
error_handler(struct sockaddr_nl * nla,struct nlmsgerr * err,void * arg)280 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
281 			 void *arg)
282 {
283 	struct nlmsghdr *nlh = (struct nlmsghdr *)err - 1;
284 	int len = nlh->nlmsg_len;
285 	struct nlattr *attrs;
286 	struct nlattr *tb[NLMSGERR_ATTR_MAX + 1];
287 	int *ret = arg;
288 	int ack_len = sizeof(*nlh) + sizeof(int) + sizeof(*nlh);
289 
290 	if (err->error > 0) {
291 		/*
292 		 * This is illegal, per netlink(7), but not impossible (think
293 		 * "vendor commands"). Callers really expect negative error
294 		 * codes, so make that happen.
295 		 */
296 		fprintf(stderr,
297 			"ERROR: received positive netlink error code %d\n",
298 			err->error);
299 		*ret = -EPROTO;
300 	} else {
301 		*ret = err->error;
302 	}
303 
304 	if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
305 		return NL_STOP;
306 
307 	if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
308 		ack_len += err->msg.nlmsg_len - sizeof(*nlh);
309 
310 	if (len <= ack_len)
311 		return NL_STOP;
312 
313 	attrs = (void *)((unsigned char *)nlh + ack_len);
314 	len -= ack_len;
315 
316 	nla_parse(tb, NLMSGERR_ATTR_MAX, attrs, len, NULL);
317 	if (tb[NLMSGERR_ATTR_MSG]) {
318 		len = strnlen((char *)nla_data(tb[NLMSGERR_ATTR_MSG]),
319 			      nla_len(tb[NLMSGERR_ATTR_MSG]));
320 		fprintf(stderr, "kernel reports: %*s\n", len,
321 			(char *)nla_data(tb[NLMSGERR_ATTR_MSG]));
322 	}
323 
324 	return NL_STOP;
325 }
326 
finish_handler(struct nl_msg * msg,void * arg)327 static int finish_handler(struct nl_msg *msg, void *arg)
328 {
329 	int *ret = arg;
330 	*ret = 0;
331 	return NL_SKIP;
332 }
333 
ack_handler(struct nl_msg * msg,void * arg)334 static int ack_handler(struct nl_msg *msg, void *arg)
335 {
336 	int *ret = arg;
337 	*ret = 0;
338 	return NL_STOP;
339 }
340 
341 static int (*registered_handler)(struct nl_msg *, void *);
342 static void *registered_handler_data;
343 
register_handler(int (* handler)(struct nl_msg *,void *),void * data)344 void register_handler(int (*handler)(struct nl_msg *, void *), void *data)
345 {
346 	registered_handler = handler;
347 	registered_handler_data = data;
348 }
349 
valid_handler(struct nl_msg * msg,void * arg)350 int valid_handler(struct nl_msg *msg, void *arg)
351 {
352 	if (registered_handler)
353 		return registered_handler(msg, registered_handler_data);
354 
355 	return NL_OK;
356 }
357 
__handle_cmd(struct nl80211_state * state,enum id_input idby,int argc,char ** argv,const struct cmd ** cmdout)358 static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
359 			int argc, char **argv, const struct cmd **cmdout)
360 {
361 	const struct cmd *cmd, *match = NULL, *sectcmd;
362 	struct nl_cb *cb;
363 	struct nl_cb *s_cb;
364 	struct nl_msg *msg;
365 	signed long long devidx = 0;
366 	int err, o_argc, i;
367 	const char *command, *section;
368 	char *tmp, **o_argv;
369 	enum command_identify_by command_idby = CIB_NONE;
370 
371 	if (argc <= 1 && idby != II_NONE)
372 		return 1;
373 
374 	o_argc = argc;
375 	o_argv = argv;
376 
377 	switch (idby) {
378 	case II_PHY_IDX:
379 		command_idby = CIB_PHY;
380 		devidx = strtoul(*argv + 4, &tmp, 0);
381 		if (*tmp != '\0')
382 			return 1;
383 		argc--;
384 		argv++;
385 		break;
386 	case II_PHY_NAME:
387 		command_idby = CIB_PHY;
388 		devidx = phy_lookup(*argv);
389 		argc--;
390 		argv++;
391 		break;
392 	case II_NETDEV:
393 		command_idby = CIB_NETDEV;
394 		devidx = if_nametoindex(*argv);
395 		if (devidx == 0)
396 			devidx = -1;
397 		argc--;
398 		argv++;
399 		break;
400 	case II_WDEV:
401 		command_idby = CIB_WDEV;
402 		devidx = strtoll(*argv, &tmp, 0);
403 		if (*tmp != '\0')
404 			return 1;
405 		argc--;
406 		argv++;
407 	default:
408 		break;
409 	}
410 
411 	if (devidx < 0)
412 		return -errno;
413 
414 	section = *argv;
415 	argc--;
416 	argv++;
417 
418 	for_each_cmd(sectcmd, i) {
419 		if (sectcmd->parent)
420 			continue;
421 		/* ok ... bit of a hack for the dupe 'info' section */
422 		if (match && sectcmd->idby != command_idby)
423 			continue;
424 		if (strcmp(sectcmd->name, section) == 0)
425 			match = sectcmd;
426 	}
427 
428 	sectcmd = match;
429 	match = NULL;
430 	if (!sectcmd)
431 		return 1;
432 
433 	if (argc > 0) {
434 		command = *argv;
435 
436 		for_each_cmd(cmd, i) {
437 			if (!cmd->handler)
438 				continue;
439 			if (cmd->parent != sectcmd)
440 				continue;
441 			/*
442 			 * ignore mismatch id by, but allow WDEV
443 			 * in place of NETDEV
444 			 */
445 			if (cmd->idby != command_idby &&
446 			    !(cmd->idby == CIB_NETDEV &&
447 			      command_idby == CIB_WDEV))
448 				continue;
449 			if (strcmp(cmd->name, command))
450 				continue;
451 			if (argc > 1 && !cmd->args)
452 				continue;
453 			match = cmd;
454 			break;
455 		}
456 
457 		if (match) {
458 			argc--;
459 			argv++;
460 		}
461 	}
462 
463 	if (match)
464 		cmd = match;
465 	else {
466 		/* Use the section itself, if possible. */
467 		cmd = sectcmd;
468 		if (argc && !cmd->args)
469 			return 1;
470 		if (cmd->idby != command_idby &&
471 		    !(cmd->idby == CIB_NETDEV && command_idby == CIB_WDEV))
472 			return 1;
473 		if (!cmd->handler)
474 			return 1;
475 	}
476 
477 	if (cmd->selector) {
478 		cmd = cmd->selector(argc, argv);
479 		if (!cmd)
480 			return 1;
481 	}
482 
483 	if (cmdout)
484 		*cmdout = cmd;
485 
486 	if (!cmd->cmd) {
487 		argc = o_argc;
488 		argv = o_argv;
489 		return cmd->handler(state, NULL, argc, argv, idby);
490 	}
491 
492 	msg = nlmsg_alloc();
493 	if (!msg) {
494 		fprintf(stderr, "failed to allocate netlink message\n");
495 		return 2;
496 	}
497 
498 	cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
499 	s_cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
500 	if (!cb || !s_cb) {
501 		fprintf(stderr, "failed to allocate netlink callbacks\n");
502 		err = 2;
503 		goto out;
504 	}
505 
506 	genlmsg_put(msg, 0, 0, state->nl80211_id, 0,
507 		    cmd->nl_msg_flags, cmd->cmd, 0);
508 
509 	switch (command_idby) {
510 	case CIB_PHY:
511 		NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
512 		break;
513 	case CIB_NETDEV:
514 		NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
515 		break;
516 	case CIB_WDEV:
517 		NLA_PUT_U64(msg, NL80211_ATTR_WDEV, devidx);
518 		break;
519 	default:
520 		break;
521 	}
522 
523 	err = cmd->handler(state, msg, argc, argv, idby);
524 	if (err)
525 		goto out;
526 
527 	nl_socket_set_cb(state->nl_sock, s_cb);
528 
529 	err = nl_send_auto_complete(state->nl_sock, msg);
530 	if (err < 0)
531 		goto out;
532 
533 	err = 1;
534 
535 	nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
536 	nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
537 	nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
538 	nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, valid_handler, NULL);
539 
540 	while (err > 0)
541 		nl_recvmsgs(state->nl_sock, cb);
542  out:
543 	nl_cb_put(cb);
544 	nl_cb_put(s_cb);
545 	nlmsg_free(msg);
546 	return err;
547  nla_put_failure:
548 	fprintf(stderr, "building message failed\n");
549 	return 2;
550 }
551 
handle_cmd(struct nl80211_state * state,enum id_input idby,int argc,char ** argv)552 int handle_cmd(struct nl80211_state *state, enum id_input idby,
553 	       int argc, char **argv)
554 {
555 	return __handle_cmd(state, idby, argc, argv, NULL);
556 }
557 
558 /*
559  * Unfortunately, I don't know how densely the linker packs the struct cmd.
560  * For example, if you have a 72-byte struct cmd, the linker will pad each
561  * out to 96 bytes before putting them together in the section. There must
562  * be some algorithm, but I haven't found it yet.
563  *
564  * We used to calculate this by taking the (abs value of) the difference
565  * between __section_get and __section_set, but if LTO is enabled then this
566  * stops working because the entries of the "__cmd" section get rearranged
567  * freely by the compiler/linker.
568  *
569  * Fix this by using yet another "__sizer" section that only contains these
570  * two entries - then the (abs value of) the difference between them will
571  * be how they get packed and that can be used to iterate the __cmd section
572  * as well.
573  */
574 static struct cmd sizer1 __attribute__((section("__sizer"))) = {};
575 static struct cmd sizer2 __attribute__((section("__sizer"))) = {};
576 
main(int argc,char ** argv)577 int main(int argc, char **argv)
578 {
579 	struct nl80211_state nlstate;
580 	int err;
581 	const struct cmd *cmd = NULL;
582 
583 	/* calculate command size including padding */
584 	cmd_size = labs((long)&sizer2 - (long)&sizer1);
585 	/* strip off self */
586 	argc--;
587 	argv0 = *argv++;
588 
589 	if (argc > 0 && strcmp(*argv, "--debug") == 0) {
590 		iw_debug = 1;
591 		argc--;
592 		argv++;
593 	}
594 
595 	if (argc > 0 && strcmp(*argv, "--version") == 0) {
596 		version();
597 		return 0;
598 	}
599 
600 	/* need to treat "help" command specially so it works w/o nl80211 */
601 	if (argc == 0 || strcmp(*argv, "help") == 0) {
602 		usage(argc - 1, argv + 1);
603 		return 0;
604 	}
605 
606 	err = nl80211_init(&nlstate);
607 	if (err)
608 		return 1;
609 
610 	if (strcmp(*argv, "dev") == 0 && argc > 1) {
611 		argc--;
612 		argv++;
613 		err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
614 	} else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
615 		if (strlen(*argv) == 3) {
616 			argc--;
617 			argv++;
618 			err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
619 		} else if (*(*argv + 3) == '#')
620 			err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
621 		else
622 			goto detect;
623 	} else if (strcmp(*argv, "wdev") == 0 && argc > 1) {
624 		argc--;
625 		argv++;
626 		err = __handle_cmd(&nlstate, II_WDEV, argc, argv, &cmd);
627 	} else {
628 		int idx;
629 		enum id_input idby;
630  detect:
631 		idby = II_NONE;
632 		if ((idx = if_nametoindex(argv[0])) != 0)
633 			idby = II_NETDEV;
634 		else if ((idx = phy_lookup(argv[0])) >= 0)
635 			idby = II_PHY_NAME;
636 		err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
637 	}
638 
639 	if (err == HANDLER_RET_USAGE) {
640 		if (cmd)
641 			usage_cmd(cmd);
642 		else
643 			usage(0, NULL);
644 	} else if (err == HANDLER_RET_DONE) {
645 		err = 0;
646 	} else if (err < 0)
647 		fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
648 
649 	nl80211_cleanup(&nlstate);
650 
651 	return err;
652 }
653