blob: dc99566653cadbd2c3f98b78b0b13b93e97510f0 [file] [log] [blame]
Johannes Bergcad53b32007-09-28 22:11:34 +02001/*
2 * nl80211 userspace tool
3 *
Johannes Berg2a1fced2008-01-16 00:39:55 +01004 * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
Johannes Bergcad53b32007-09-28 22:11:34 +02005 */
6
7#include <errno.h>
8#include <stdio.h>
Johannes Bergd5ac8ad2008-04-03 15:24:13 +02009#include <string.h>
Johannes Bergbd396f22008-09-16 16:56:09 +020010#include <net/if.h>
11#include <sys/types.h>
12#include <sys/stat.h>
13#include <fcntl.h>
14#include <unistd.h>
Johannes Berg27c49ed2009-05-05 12:19:13 +020015#include <stdbool.h>
Johannes Bergc5514492011-12-07 09:08:40 +010016
Johannes Bergcad53b32007-09-28 22:11:34 +020017#include <netlink/genl/genl.h>
18#include <netlink/genl/family.h>
Johannes Bergc5514492011-12-07 09:08:40 +010019#include <netlink/genl/ctrl.h>
Johannes Bergcad53b32007-09-28 22:11:34 +020020#include <netlink/msg.h>
21#include <netlink/attr.h>
Johannes Bergcad53b32007-09-28 22:11:34 +020022
Johannes Bergf408e012008-09-18 19:32:11 +020023#include "nl80211.h"
Johannes Bergcad53b32007-09-28 22:11:34 +020024#include "iw.h"
25
Yegor Yefremovded667b2011-07-21 15:06:17 +000026/* libnl 1.x compatibility code */
27#if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30)
Pat Erleydfd13ee2008-12-13 01:04:05 +010028static inline struct nl_handle *nl_socket_alloc(void)
29{
30 return nl_handle_alloc();
31}
32
Pat Erley57077d62009-01-29 14:35:18 +010033static inline void nl_socket_free(struct nl_sock *h)
Pat Erleydfd13ee2008-12-13 01:04:05 +010034{
35 nl_handle_destroy(h);
36}
Johannes Bergb9be8932013-02-20 12:54:44 +010037
38static inline int nl_socket_set_buffer_size(struct nl_sock *sk,
39 int rxbuf, int txbuf)
40{
41 return nl_set_buffer_size(sk, rxbuf, txbuf);
42}
Yegor Yefremovded667b2011-07-21 15:06:17 +000043#endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 */
Pat Erleydfd13ee2008-12-13 01:04:05 +010044
Johannes Berg957a0f02009-05-05 14:10:09 +020045int iw_debug = 0;
Johannes Bergcad53b32007-09-28 22:11:34 +020046
47static int nl80211_init(struct nl80211_state *state)
48{
49 int err;
50
Pat Erley57077d62009-01-29 14:35:18 +010051 state->nl_sock = nl_socket_alloc();
52 if (!state->nl_sock) {
53 fprintf(stderr, "Failed to allocate netlink socket.\n");
Johannes Bergcad53b32007-09-28 22:11:34 +020054 return -ENOMEM;
55 }
56
Johannes Bergb9be8932013-02-20 12:54:44 +010057 nl_socket_set_buffer_size(state->nl_sock, 8192, 8192);
58
Pat Erley57077d62009-01-29 14:35:18 +010059 if (genl_connect(state->nl_sock)) {
Johannes Bergcad53b32007-09-28 22:11:34 +020060 fprintf(stderr, "Failed to connect to generic netlink.\n");
61 err = -ENOLINK;
62 goto out_handle_destroy;
63 }
64
Johannes Bergf09cee62011-11-10 15:25:53 +010065 state->nl80211_id = genl_ctrl_resolve(state->nl_sock, "nl80211");
66 if (state->nl80211_id < 0) {
Johannes Bergcad53b32007-09-28 22:11:34 +020067 fprintf(stderr, "nl80211 not found.\n");
68 err = -ENOENT;
Johannes Bergf09cee62011-11-10 15:25:53 +010069 goto out_handle_destroy;
Johannes Bergcad53b32007-09-28 22:11:34 +020070 }
71
72 return 0;
73
Johannes Bergcad53b32007-09-28 22:11:34 +020074 out_handle_destroy:
Pat Erley57077d62009-01-29 14:35:18 +010075 nl_socket_free(state->nl_sock);
Johannes Bergcad53b32007-09-28 22:11:34 +020076 return err;
77}
78
79static void nl80211_cleanup(struct nl80211_state *state)
80{
Pat Erley57077d62009-01-29 14:35:18 +010081 nl_socket_free(state->nl_sock);
Johannes Bergcad53b32007-09-28 22:11:34 +020082}
83
Johannes Berg403b9c82008-09-17 00:19:53 +020084static int cmd_size;
85
Johannes Berg4698bfc2009-08-24 12:53:34 +020086extern struct cmd __start___cmd;
87extern struct cmd __stop___cmd;
88
89#define for_each_cmd(_cmd) \
90 for (_cmd = &__start___cmd; _cmd < &__stop___cmd; \
91 _cmd = (const struct cmd *)((char *)_cmd + cmd_size))
92
93
94static void __usage_cmd(const struct cmd *cmd, char *indent, bool full)
Johannes Berg3bb116d2009-05-05 14:06:39 +020095{
Johannes Berg01ae06f2009-05-05 14:48:16 +020096 const char *start, *lend, *end;
97
Holger Schurigbd663892009-08-24 11:17:19 +020098 printf("%s", indent);
Johannes Berg4f0cae72009-05-05 14:17:28 +020099
Johannes Berg3bb116d2009-05-05 14:06:39 +0200100 switch (cmd->idby) {
101 case CIB_NONE:
Johannes Berg3bb116d2009-05-05 14:06:39 +0200102 break;
103 case CIB_PHY:
Holger Schurigbd663892009-08-24 11:17:19 +0200104 printf("phy <phyname> ");
Johannes Berg3bb116d2009-05-05 14:06:39 +0200105 break;
106 case CIB_NETDEV:
Holger Schurigbd663892009-08-24 11:17:19 +0200107 printf("dev <devname> ");
Johannes Berg3bb116d2009-05-05 14:06:39 +0200108 break;
Johannes Berg290a3dd2012-07-19 08:15:41 +0200109 case CIB_WDEV:
110 printf("wdev <idx> ");
111 break;
Johannes Berg3bb116d2009-05-05 14:06:39 +0200112 }
Johannes Berg4698bfc2009-08-24 12:53:34 +0200113 if (cmd->parent && cmd->parent->name)
114 printf("%s ", cmd->parent->name);
Holger Schurigbd663892009-08-24 11:17:19 +0200115 printf("%s", cmd->name);
Johannes Bergeb795502012-11-20 13:47:49 +0100116
117 if (cmd->args) {
118 /* print line by line */
119 start = cmd->args;
120 end = strchr(start, '\0');
121 printf(" ");
122 do {
123 lend = strchr(start, '\n');
124 if (!lend)
125 lend = end;
126 if (start != cmd->args) {
127 printf("\t");
128 switch (cmd->idby) {
129 case CIB_NONE:
130 break;
131 case CIB_PHY:
132 printf("phy <phyname> ");
133 break;
134 case CIB_NETDEV:
135 printf("dev <devname> ");
136 break;
137 case CIB_WDEV:
138 printf("wdev <idx> ");
139 break;
140 }
141 if (cmd->parent && cmd->parent->name)
142 printf("%s ", cmd->parent->name);
143 printf("%s ", cmd->name);
144 }
145 printf("%.*s\n", (int)(lend - start), start);
146 start = lend + 1;
147 } while (end != lend);
148 } else
149 printf("\n");
Johannes Berg01ae06f2009-05-05 14:48:16 +0200150
151 if (!full || !cmd->help)
152 return;
153
154 /* hack */
155 if (strlen(indent))
156 indent = "\t\t";
157 else
Holger Schurigbd663892009-08-24 11:17:19 +0200158 printf("\n");
Johannes Berg01ae06f2009-05-05 14:48:16 +0200159
160 /* print line by line */
161 start = cmd->help;
162 end = strchr(start, '\0');
163 do {
164 lend = strchr(start, '\n');
165 if (!lend)
166 lend = end;
Holger Schurigbd663892009-08-24 11:17:19 +0200167 printf("%s", indent);
168 printf("%.*s\n", (int)(lend - start), start);
Johannes Berg01ae06f2009-05-05 14:48:16 +0200169 start = lend + 1;
170 } while (end != lend);
171
Holger Schurigbd663892009-08-24 11:17:19 +0200172 printf("\n");
Johannes Berg3bb116d2009-05-05 14:06:39 +0200173}
174
Johannes Berg4f0cae72009-05-05 14:17:28 +0200175static void usage_options(void)
176{
Holger Schurigbd663892009-08-24 11:17:19 +0200177 printf("Options:\n");
178 printf("\t--debug\t\tenable netlink debugging\n");
Johannes Berg4f0cae72009-05-05 14:17:28 +0200179}
180
Johannes Berg01ae06f2009-05-05 14:48:16 +0200181static const char *argv0;
182
Johannes Bergf3ac8bf2012-03-26 11:15:49 +0200183static void usage(int argc, char **argv)
Johannes Berg45c72122007-09-28 23:47:38 +0200184{
Johannes Berg4698bfc2009-08-24 12:53:34 +0200185 const struct cmd *section, *cmd;
Johannes Bergf3ac8bf2012-03-26 11:15:49 +0200186 bool full = argc >= 0;
187 const char *sect_filt = NULL;
188 const char *cmd_filt = NULL;
189
190 if (argc > 0)
191 sect_filt = argv[0];
192
193 if (argc > 1)
194 cmd_filt = argv[1];
Johannes Berg45c72122007-09-28 23:47:38 +0200195
Holger Schurigbd663892009-08-24 11:17:19 +0200196 printf("Usage:\t%s [options] command\n", argv0);
Johannes Berg4f0cae72009-05-05 14:17:28 +0200197 usage_options();
Holger Schurigbd663892009-08-24 11:17:19 +0200198 printf("\t--version\tshow version (%s)\n", iw_version);
199 printf("Commands:\n");
Johannes Berg4698bfc2009-08-24 12:53:34 +0200200 for_each_cmd(section) {
201 if (section->parent)
Johannes Berg403b9c82008-09-17 00:19:53 +0200202 continue;
Johannes Berg4698bfc2009-08-24 12:53:34 +0200203
Johannes Bergf3ac8bf2012-03-26 11:15:49 +0200204 if (sect_filt && strcmp(section->name, sect_filt))
205 continue;
206
Johannes Berg4698bfc2009-08-24 12:53:34 +0200207 if (section->handler && !section->hidden)
208 __usage_cmd(section, "\t", full);
209
210 for_each_cmd(cmd) {
211 if (section != cmd->parent)
212 continue;
213 if (!cmd->handler || cmd->hidden)
214 continue;
Johannes Bergf3ac8bf2012-03-26 11:15:49 +0200215 if (cmd_filt && strcmp(cmd->name, cmd_filt))
216 continue;
Johannes Berg4698bfc2009-08-24 12:53:34 +0200217 __usage_cmd(cmd, "\t", full);
218 }
Johannes Bergbd396f22008-09-16 16:56:09 +0200219 }
Johannes Berg75f42042012-07-19 08:36:05 +0200220 printf("\nCommands that use the netdev ('dev') can also be given the\n"
221 "'wdev' instead to identify the device.\n");
Holger Schurigbd663892009-08-24 11:17:19 +0200222 printf("\nYou can omit the 'phy' or 'dev' if "
Johannes Bergf4ec76d2009-07-06 13:10:39 +0200223 "the identification is unique,\n"
Johannes Berg8aefee92009-07-06 13:14:41 +0200224 "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
Johannes Bergfbdb8d02009-07-30 14:12:53 +0200225 "(Don't when scripting.)\n\n"
226 "Do NOT screenscrape this tool, we don't "
227 "consider its output stable.\n\n");
Johannes Berg45c72122007-09-28 23:47:38 +0200228}
229
Johannes Berg01ae06f2009-05-05 14:48:16 +0200230static int print_help(struct nl80211_state *state,
231 struct nl_cb *cb,
232 struct nl_msg *msg,
Johannes Berg05514f92012-07-19 11:50:50 +0200233 int argc, char **argv,
234 enum id_input id)
Johannes Berg01ae06f2009-05-05 14:48:16 +0200235{
236 exit(3);
237}
Johannes Bergf3ac8bf2012-03-26 11:15:49 +0200238TOPLEVEL(help, "[command]", 0, 0, CIB_NONE, print_help,
239 "Print usage for all or a specific command, e.g.\n"
240 "\"help wowlan\" or \"help wowlan enable\".");
Johannes Berg01ae06f2009-05-05 14:48:16 +0200241
Johannes Berg4698bfc2009-08-24 12:53:34 +0200242static void usage_cmd(const struct cmd *cmd)
Johannes Berg4f0cae72009-05-05 14:17:28 +0200243{
Holger Schurigbd663892009-08-24 11:17:19 +0200244 printf("Usage:\t%s [options] ", argv0);
Johannes Berg01ae06f2009-05-05 14:48:16 +0200245 __usage_cmd(cmd, "", true);
Johannes Berg4f0cae72009-05-05 14:17:28 +0200246 usage_options();
247}
248
Johannes Bergd711f012008-09-16 21:56:25 +0200249static void version(void)
250{
Johannes Berg133b0692009-04-21 00:53:00 +0200251 printf("iw version %s\n", iw_version);
Johannes Bergd711f012008-09-16 21:56:25 +0200252}
253
Johannes Bergbd396f22008-09-16 16:56:09 +0200254static int phy_lookup(char *name)
Mike Kershaw1cdd9012007-09-29 19:17:20 -0400255{
Johannes Bergbd396f22008-09-16 16:56:09 +0200256 char buf[200];
257 int fd, pos;
258
259 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
260
261 fd = open(buf, O_RDONLY);
Johannes Berg989e97c2009-03-12 11:13:36 +0100262 if (fd < 0)
263 return -1;
Johannes Bergbd396f22008-09-16 16:56:09 +0200264 pos = read(fd, buf, sizeof(buf) - 1);
Eric Sesterhenn8f253ee2009-11-02 10:46:38 +0100265 if (pos < 0) {
266 close(fd);
Johannes Bergbd396f22008-09-16 16:56:09 +0200267 return -1;
Eric Sesterhenn8f253ee2009-11-02 10:46:38 +0100268 }
Johannes Bergbd396f22008-09-16 16:56:09 +0200269 buf[pos] = '\0';
Eric Sesterhenn8f253ee2009-11-02 10:46:38 +0100270 close(fd);
Johannes Bergbd396f22008-09-16 16:56:09 +0200271 return atoi(buf);
272}
273
Johannes Berg70391cc2008-09-16 18:35:06 +0200274static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
275 void *arg)
276{
277 int *ret = arg;
278 *ret = err->error;
279 return NL_STOP;
280}
281
Johannes Berg561c5b72008-09-16 19:06:53 +0200282static int finish_handler(struct nl_msg *msg, void *arg)
283{
Johannes Berg59c418c2008-10-24 21:45:01 +0200284 int *ret = arg;
285 *ret = 0;
Johannes Berg561c5b72008-09-16 19:06:53 +0200286 return NL_SKIP;
287}
288
289static int ack_handler(struct nl_msg *msg, void *arg)
Johannes Berg70391cc2008-09-16 18:35:06 +0200290{
291 int *ret = arg;
292 *ret = 0;
293 return NL_STOP;
294}
295
Johannes Berg4f0cae72009-05-05 14:17:28 +0200296static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
Johannes Berg4698bfc2009-08-24 12:53:34 +0200297 int argc, char **argv, const struct cmd **cmdout)
Johannes Bergbd396f22008-09-16 16:56:09 +0200298{
Johannes Berg4698bfc2009-08-24 12:53:34 +0200299 const struct cmd *cmd, *match = NULL, *sectcmd;
Johannes Berg7c37a242009-04-08 13:13:28 +0200300 struct nl_cb *cb;
Sean Robinson5cb60f92011-03-17 07:58:41 -0700301 struct nl_cb *s_cb;
Johannes Bergbd396f22008-09-16 16:56:09 +0200302 struct nl_msg *msg;
Johannes Berg290a3dd2012-07-19 08:15:41 +0200303 signed long long devidx = 0;
Johannes Bergbb60b4a2009-04-08 14:11:24 +0200304 int err, o_argc;
Johannes Bergbd396f22008-09-16 16:56:09 +0200305 const char *command, *section;
Johannes Bergbb60b4a2009-04-08 14:11:24 +0200306 char *tmp, **o_argv;
Johannes Berg99273632009-03-20 09:59:30 +0100307 enum command_identify_by command_idby = CIB_NONE;
Johannes Bergbd396f22008-09-16 16:56:09 +0200308
Johannes Berg99273632009-03-20 09:59:30 +0100309 if (argc <= 1 && idby != II_NONE)
Johannes Berg5e75fd02008-09-16 18:13:12 +0200310 return 1;
Johannes Bergbd396f22008-09-16 16:56:09 +0200311
Johannes Bergbb60b4a2009-04-08 14:11:24 +0200312 o_argc = argc;
313 o_argv = argv;
314
Johannes Bergbd396f22008-09-16 16:56:09 +0200315 switch (idby) {
Johannes Berg99273632009-03-20 09:59:30 +0100316 case II_PHY_IDX:
317 command_idby = CIB_PHY;
318 devidx = strtoul(*argv + 4, &tmp, 0);
319 if (*tmp != '\0')
320 return 1;
321 argc--;
322 argv++;
323 break;
324 case II_PHY_NAME:
325 command_idby = CIB_PHY;
Johannes Bergbd396f22008-09-16 16:56:09 +0200326 devidx = phy_lookup(*argv);
327 argc--;
328 argv++;
329 break;
Johannes Berg99273632009-03-20 09:59:30 +0100330 case II_NETDEV:
331 command_idby = CIB_NETDEV;
Johannes Bergbd396f22008-09-16 16:56:09 +0200332 devidx = if_nametoindex(*argv);
Johannes Berg989e97c2009-03-12 11:13:36 +0100333 if (devidx == 0)
334 devidx = -1;
Johannes Bergbd396f22008-09-16 16:56:09 +0200335 argc--;
336 argv++;
337 break;
Johannes Berg290a3dd2012-07-19 08:15:41 +0200338 case II_WDEV:
339 command_idby = CIB_WDEV;
340 devidx = strtoll(*argv, &tmp, 0);
341 if (*tmp != '\0')
342 return 1;
343 argc--;
344 argv++;
Johannes Bergbd396f22008-09-16 16:56:09 +0200345 default:
346 break;
347 }
348
Johannes Berg989e97c2009-03-12 11:13:36 +0100349 if (devidx < 0)
350 return -errno;
351
Johannes Berg4698bfc2009-08-24 12:53:34 +0200352 section = *argv;
Johannes Bergbd396f22008-09-16 16:56:09 +0200353 argc--;
354 argv++;
355
Johannes Berg4698bfc2009-08-24 12:53:34 +0200356 for_each_cmd(sectcmd) {
357 if (sectcmd->parent)
Johannes Berg403b9c82008-09-17 00:19:53 +0200358 continue;
Johannes Berg4698bfc2009-08-24 12:53:34 +0200359 /* ok ... bit of a hack for the dupe 'info' section */
360 if (match && sectcmd->idby != command_idby)
Johannes Bergbd396f22008-09-16 16:56:09 +0200361 continue;
Johannes Berg4698bfc2009-08-24 12:53:34 +0200362 if (strcmp(sectcmd->name, section) == 0)
363 match = sectcmd;
Johannes Bergbd396f22008-09-16 16:56:09 +0200364 }
365
Johannes Berg4698bfc2009-08-24 12:53:34 +0200366 sectcmd = match;
367 match = NULL;
368 if (!sectcmd)
Johannes Berg5e75fd02008-09-16 18:13:12 +0200369 return 1;
Johannes Bergbd396f22008-09-16 16:56:09 +0200370
Johannes Berg4698bfc2009-08-24 12:53:34 +0200371 if (argc > 0) {
372 command = *argv;
373
374 for_each_cmd(cmd) {
375 if (!cmd->handler)
376 continue;
377 if (cmd->parent != sectcmd)
378 continue;
Johannes Berg75f42042012-07-19 08:36:05 +0200379 /*
380 * ignore mismatch id by, but allow WDEV
381 * in place of NETDEV
382 */
383 if (cmd->idby != command_idby &&
384 !(cmd->idby == CIB_NETDEV &&
385 command_idby == CIB_WDEV))
Johannes Berg4698bfc2009-08-24 12:53:34 +0200386 continue;
387 if (strcmp(cmd->name, command))
388 continue;
389 if (argc > 1 && !cmd->args)
390 continue;
391 match = cmd;
392 break;
393 }
394
395 if (match) {
396 argc--;
397 argv++;
398 }
399 }
400
401 if (match)
402 cmd = match;
403 else {
404 /* Use the section itself, if possible. */
405 cmd = sectcmd;
406 if (argc && !cmd->args)
407 return 1;
Johannes Berg75f42042012-07-19 08:36:05 +0200408 if (cmd->idby != command_idby &&
409 !(cmd->idby == CIB_NETDEV && command_idby == CIB_WDEV))
Johannes Berg4698bfc2009-08-24 12:53:34 +0200410 return 1;
411 if (!cmd->handler)
412 return 1;
413 }
414
Johannes Berg1633ddf2010-11-24 08:16:47 +0100415 if (cmd->selector) {
416 cmd = cmd->selector(argc, argv);
417 if (!cmd)
418 return 1;
419 }
420
Johannes Berg4f0cae72009-05-05 14:17:28 +0200421 if (cmdout)
422 *cmdout = cmd;
423
Johannes Bergbb60b4a2009-04-08 14:11:24 +0200424 if (!cmd->cmd) {
425 argc = o_argc;
426 argv = o_argv;
Johannes Berg05514f92012-07-19 11:50:50 +0200427 return cmd->handler(state, NULL, NULL, argc, argv, idby);
Johannes Bergbb60b4a2009-04-08 14:11:24 +0200428 }
429
Johannes Bergbd396f22008-09-16 16:56:09 +0200430 msg = nlmsg_alloc();
431 if (!msg) {
Johannes Berg70391cc2008-09-16 18:35:06 +0200432 fprintf(stderr, "failed to allocate netlink message\n");
433 return 2;
434 }
435
Johannes Berg957a0f02009-05-05 14:10:09 +0200436 cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
Sean Robinson5cb60f92011-03-17 07:58:41 -0700437 s_cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
438 if (!cb || !s_cb) {
Johannes Berg70391cc2008-09-16 18:35:06 +0200439 fprintf(stderr, "failed to allocate netlink callbacks\n");
440 err = 2;
441 goto out_free_msg;
Johannes Bergbd396f22008-09-16 16:56:09 +0200442 }
443
Johannes Bergf09cee62011-11-10 15:25:53 +0100444 genlmsg_put(msg, 0, 0, state->nl80211_id, 0,
Johannes Bergbd396f22008-09-16 16:56:09 +0200445 cmd->nl_msg_flags, cmd->cmd, 0);
446
Johannes Berg99273632009-03-20 09:59:30 +0100447 switch (command_idby) {
Johannes Bergbd396f22008-09-16 16:56:09 +0200448 case CIB_PHY:
449 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
450 break;
451 case CIB_NETDEV:
452 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
453 break;
Johannes Berg290a3dd2012-07-19 08:15:41 +0200454 case CIB_WDEV:
455 NLA_PUT_U64(msg, NL80211_ATTR_WDEV, devidx);
456 break;
Johannes Bergbd396f22008-09-16 16:56:09 +0200457 default:
458 break;
459 }
460
Johannes Berg05514f92012-07-19 11:50:50 +0200461 err = cmd->handler(state, cb, msg, argc, argv, idby);
Johannes Berg70391cc2008-09-16 18:35:06 +0200462 if (err)
463 goto out;
464
Sean Robinson5cb60f92011-03-17 07:58:41 -0700465 nl_socket_set_cb(state->nl_sock, s_cb);
466
Pat Erley57077d62009-01-29 14:35:18 +0100467 err = nl_send_auto_complete(state->nl_sock, msg);
Johannes Berg70391cc2008-09-16 18:35:06 +0200468 if (err < 0)
469 goto out;
470
Johannes Bergc5c44712008-09-29 16:49:06 +0200471 err = 1;
472
Johannes Berg70391cc2008-09-16 18:35:06 +0200473 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
Johannes Berg59c418c2008-10-24 21:45:01 +0200474 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
Johannes Berg561c5b72008-09-16 19:06:53 +0200475 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
Johannes Berg70391cc2008-09-16 18:35:06 +0200476
Johannes Bergc5c44712008-09-29 16:49:06 +0200477 while (err > 0)
Pat Erley57077d62009-01-29 14:35:18 +0100478 nl_recvmsgs(state->nl_sock, cb);
Johannes Berg70391cc2008-09-16 18:35:06 +0200479 out:
480 nl_cb_put(cb);
481 out_free_msg:
482 nlmsg_free(msg);
483 return err;
Johannes Bergbd396f22008-09-16 16:56:09 +0200484 nla_put_failure:
485 fprintf(stderr, "building message failed\n");
Johannes Berg70391cc2008-09-16 18:35:06 +0200486 return 2;
Johannes Bergbd396f22008-09-16 16:56:09 +0200487}
488
Johannes Berg4f0cae72009-05-05 14:17:28 +0200489int handle_cmd(struct nl80211_state *state, enum id_input idby,
490 int argc, char **argv)
491{
492 return __handle_cmd(state, idby, argc, argv, NULL);
493}
494
Johannes Bergcad53b32007-09-28 22:11:34 +0200495int main(int argc, char **argv)
496{
497 struct nl80211_state nlstate;
Johannes Bergbd396f22008-09-16 16:56:09 +0200498 int err;
Johannes Berg4698bfc2009-08-24 12:53:34 +0200499 const struct cmd *cmd = NULL;
Johannes Bergcad53b32007-09-28 22:11:34 +0200500
Johannes Bergf408e012008-09-18 19:32:11 +0200501 /* calculate command size including padding */
Johannes Berg4698bfc2009-08-24 12:53:34 +0200502 cmd_size = abs((long)&__section_set - (long)&__section_get);
Johannes Berg45c72122007-09-28 23:47:38 +0200503 /* strip off self */
504 argc--;
Mike Kershaw1cdd9012007-09-29 19:17:20 -0400505 argv0 = *argv++;
506
Johannes Berg59c49f02008-09-16 21:00:32 +0200507 if (argc > 0 && strcmp(*argv, "--debug") == 0) {
Johannes Berg957a0f02009-05-05 14:10:09 +0200508 iw_debug = 1;
Johannes Berg59c49f02008-09-16 21:00:32 +0200509 argc--;
510 argv++;
511 }
512
Johannes Bergd711f012008-09-16 21:56:25 +0200513 if (argc > 0 && strcmp(*argv, "--version") == 0) {
514 version();
515 return 0;
516 }
517
Johannes Berg01ae06f2009-05-05 14:48:16 +0200518 /* need to treat "help" command specially so it works w/o nl80211 */
Johannes Bergbd396f22008-09-16 16:56:09 +0200519 if (argc == 0 || strcmp(*argv, "help") == 0) {
Johannes Bergf3ac8bf2012-03-26 11:15:49 +0200520 usage(argc - 1, argv + 1);
Johannes Berg4a972f82008-09-16 23:21:56 +0200521 return 0;
Mike Kershaw1cdd9012007-09-29 19:17:20 -0400522 }
Johannes Bergcad53b32007-09-28 22:11:34 +0200523
Johannes Berg2bdb6bd2008-09-16 23:20:20 +0200524 err = nl80211_init(&nlstate);
525 if (err)
526 return 1;
527
Johannes Berg957a0f02009-05-05 14:10:09 +0200528 if (strcmp(*argv, "dev") == 0 && argc > 1) {
Luis R. Rodriguez14a03802008-08-29 16:01:56 -0700529 argc--;
530 argv++;
Johannes Berg4f0cae72009-05-05 14:17:28 +0200531 err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
Johannes Berg811ec682009-04-19 15:05:38 +0200532 } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) {
Johannes Berg99273632009-03-20 09:59:30 +0100533 if (strlen(*argv) == 3) {
534 argc--;
535 argv++;
Johannes Berg4f0cae72009-05-05 14:17:28 +0200536 err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd);
Johannes Berg99273632009-03-20 09:59:30 +0100537 } else if (*(*argv + 3) == '#')
Johannes Berg4f0cae72009-05-05 14:17:28 +0200538 err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
Johannes Berg99273632009-03-20 09:59:30 +0100539 else
Johannes Bergf4ec76d2009-07-06 13:10:39 +0200540 goto detect;
Johannes Berg290a3dd2012-07-19 08:15:41 +0200541 } else if (strcmp(*argv, "wdev") == 0 && argc > 1) {
542 argc--;
543 argv++;
544 err = __handle_cmd(&nlstate, II_WDEV, argc, argv, &cmd);
Johannes Bergf4ec76d2009-07-06 13:10:39 +0200545 } else {
546 int idx;
547 enum id_input idby = II_NONE;
548 detect:
549 if ((idx = if_nametoindex(argv[0])) != 0)
550 idby = II_NETDEV;
Johannes Berg66f8ca42009-07-07 21:37:17 +0200551 else if ((idx = phy_lookup(argv[0])) >= 0)
Johannes Bergf4ec76d2009-07-06 13:10:39 +0200552 idby = II_PHY_NAME;
553 err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
554 }
Luis R. Rodriguez14a03802008-08-29 16:01:56 -0700555
Johannes Berg4f0cae72009-05-05 14:17:28 +0200556 if (err == 1) {
557 if (cmd)
Johannes Berg01ae06f2009-05-05 14:48:16 +0200558 usage_cmd(cmd);
Johannes Berg4f0cae72009-05-05 14:17:28 +0200559 else
Johannes Bergf3ac8bf2012-03-26 11:15:49 +0200560 usage(0, NULL);
Johannes Berg4f0cae72009-05-05 14:17:28 +0200561 } else if (err < 0)
Johannes Bergb49be3e2008-09-16 18:54:39 +0200562 fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);
Johannes Berg45c72122007-09-28 23:47:38 +0200563
Johannes Bergcad53b32007-09-28 22:11:34 +0200564 nl80211_cleanup(&nlstate);
565
Johannes Berg45c72122007-09-28 23:47:38 +0200566 return err;
Johannes Bergcad53b32007-09-28 22:11:34 +0200567}