Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * nl80211 userspace tool |
| 3 | * |
Johannes Berg | 2a1fced | 2008-01-16 00:39:55 +0100 | [diff] [blame] | 4 | * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net> |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <stdio.h> |
Johannes Berg | d5ac8ad | 2008-04-03 15:24:13 +0200 | [diff] [blame] | 9 | #include <string.h> |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 10 | #include <net/if.h> |
| 11 | #include <sys/types.h> |
| 12 | #include <sys/stat.h> |
| 13 | #include <fcntl.h> |
| 14 | #include <unistd.h> |
Johannes Berg | 27c49ed | 2009-05-05 12:19:13 +0200 | [diff] [blame] | 15 | #include <stdbool.h> |
Johannes Berg | c551449 | 2011-12-07 09:08:40 +0100 | [diff] [blame] | 16 | |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 17 | #include <netlink/genl/genl.h> |
| 18 | #include <netlink/genl/family.h> |
Johannes Berg | c551449 | 2011-12-07 09:08:40 +0100 | [diff] [blame] | 19 | #include <netlink/genl/ctrl.h> |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 20 | #include <netlink/msg.h> |
| 21 | #include <netlink/attr.h> |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 22 | |
Johannes Berg | f408e01 | 2008-09-18 19:32:11 +0200 | [diff] [blame] | 23 | #include "nl80211.h" |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 24 | #include "iw.h" |
| 25 | |
Yegor Yefremov | ded667b | 2011-07-21 15:06:17 +0000 | [diff] [blame] | 26 | /* libnl 1.x compatibility code */ |
| 27 | #if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30) |
Pat Erley | dfd13ee | 2008-12-13 01:04:05 +0100 | [diff] [blame] | 28 | static inline struct nl_handle *nl_socket_alloc(void) |
| 29 | { |
| 30 | return nl_handle_alloc(); |
| 31 | } |
| 32 | |
Pat Erley | 57077d6 | 2009-01-29 14:35:18 +0100 | [diff] [blame] | 33 | static inline void nl_socket_free(struct nl_sock *h) |
Pat Erley | dfd13ee | 2008-12-13 01:04:05 +0100 | [diff] [blame] | 34 | { |
| 35 | nl_handle_destroy(h); |
| 36 | } |
Johannes Berg | b9be893 | 2013-02-20 12:54:44 +0100 | [diff] [blame] | 37 | |
| 38 | static 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 Yefremov | ded667b | 2011-07-21 15:06:17 +0000 | [diff] [blame] | 43 | #endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 */ |
Pat Erley | dfd13ee | 2008-12-13 01:04:05 +0100 | [diff] [blame] | 44 | |
Johannes Berg | 957a0f0 | 2009-05-05 14:10:09 +0200 | [diff] [blame] | 45 | int iw_debug = 0; |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 46 | |
| 47 | static int nl80211_init(struct nl80211_state *state) |
| 48 | { |
| 49 | int err; |
| 50 | |
Pat Erley | 57077d6 | 2009-01-29 14:35:18 +0100 | [diff] [blame] | 51 | state->nl_sock = nl_socket_alloc(); |
| 52 | if (!state->nl_sock) { |
| 53 | fprintf(stderr, "Failed to allocate netlink socket.\n"); |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 54 | return -ENOMEM; |
| 55 | } |
| 56 | |
Johannes Berg | b9be893 | 2013-02-20 12:54:44 +0100 | [diff] [blame] | 57 | nl_socket_set_buffer_size(state->nl_sock, 8192, 8192); |
| 58 | |
Pat Erley | 57077d6 | 2009-01-29 14:35:18 +0100 | [diff] [blame] | 59 | if (genl_connect(state->nl_sock)) { |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 60 | fprintf(stderr, "Failed to connect to generic netlink.\n"); |
| 61 | err = -ENOLINK; |
| 62 | goto out_handle_destroy; |
| 63 | } |
| 64 | |
Johannes Berg | f09cee6 | 2011-11-10 15:25:53 +0100 | [diff] [blame] | 65 | state->nl80211_id = genl_ctrl_resolve(state->nl_sock, "nl80211"); |
| 66 | if (state->nl80211_id < 0) { |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 67 | fprintf(stderr, "nl80211 not found.\n"); |
| 68 | err = -ENOENT; |
Johannes Berg | f09cee6 | 2011-11-10 15:25:53 +0100 | [diff] [blame] | 69 | goto out_handle_destroy; |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 74 | out_handle_destroy: |
Pat Erley | 57077d6 | 2009-01-29 14:35:18 +0100 | [diff] [blame] | 75 | nl_socket_free(state->nl_sock); |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 76 | return err; |
| 77 | } |
| 78 | |
| 79 | static void nl80211_cleanup(struct nl80211_state *state) |
| 80 | { |
Pat Erley | 57077d6 | 2009-01-29 14:35:18 +0100 | [diff] [blame] | 81 | nl_socket_free(state->nl_sock); |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Johannes Berg | 403b9c8 | 2008-09-17 00:19:53 +0200 | [diff] [blame] | 84 | static int cmd_size; |
| 85 | |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 86 | extern struct cmd __start___cmd; |
| 87 | extern 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 | |
| 94 | static void __usage_cmd(const struct cmd *cmd, char *indent, bool full) |
Johannes Berg | 3bb116d | 2009-05-05 14:06:39 +0200 | [diff] [blame] | 95 | { |
Johannes Berg | 01ae06f | 2009-05-05 14:48:16 +0200 | [diff] [blame] | 96 | const char *start, *lend, *end; |
| 97 | |
Holger Schurig | bd66389 | 2009-08-24 11:17:19 +0200 | [diff] [blame] | 98 | printf("%s", indent); |
Johannes Berg | 4f0cae7 | 2009-05-05 14:17:28 +0200 | [diff] [blame] | 99 | |
Johannes Berg | 3bb116d | 2009-05-05 14:06:39 +0200 | [diff] [blame] | 100 | switch (cmd->idby) { |
| 101 | case CIB_NONE: |
Johannes Berg | 3bb116d | 2009-05-05 14:06:39 +0200 | [diff] [blame] | 102 | break; |
| 103 | case CIB_PHY: |
Holger Schurig | bd66389 | 2009-08-24 11:17:19 +0200 | [diff] [blame] | 104 | printf("phy <phyname> "); |
Johannes Berg | 3bb116d | 2009-05-05 14:06:39 +0200 | [diff] [blame] | 105 | break; |
| 106 | case CIB_NETDEV: |
Holger Schurig | bd66389 | 2009-08-24 11:17:19 +0200 | [diff] [blame] | 107 | printf("dev <devname> "); |
Johannes Berg | 3bb116d | 2009-05-05 14:06:39 +0200 | [diff] [blame] | 108 | break; |
Johannes Berg | 290a3dd | 2012-07-19 08:15:41 +0200 | [diff] [blame] | 109 | case CIB_WDEV: |
| 110 | printf("wdev <idx> "); |
| 111 | break; |
Johannes Berg | 3bb116d | 2009-05-05 14:06:39 +0200 | [diff] [blame] | 112 | } |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 113 | if (cmd->parent && cmd->parent->name) |
| 114 | printf("%s ", cmd->parent->name); |
Holger Schurig | bd66389 | 2009-08-24 11:17:19 +0200 | [diff] [blame] | 115 | printf("%s", cmd->name); |
Johannes Berg | eb79550 | 2012-11-20 13:47:49 +0100 | [diff] [blame] | 116 | |
| 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 Berg | 01ae06f | 2009-05-05 14:48:16 +0200 | [diff] [blame] | 150 | |
| 151 | if (!full || !cmd->help) |
| 152 | return; |
| 153 | |
| 154 | /* hack */ |
| 155 | if (strlen(indent)) |
| 156 | indent = "\t\t"; |
| 157 | else |
Holger Schurig | bd66389 | 2009-08-24 11:17:19 +0200 | [diff] [blame] | 158 | printf("\n"); |
Johannes Berg | 01ae06f | 2009-05-05 14:48:16 +0200 | [diff] [blame] | 159 | |
| 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 Schurig | bd66389 | 2009-08-24 11:17:19 +0200 | [diff] [blame] | 167 | printf("%s", indent); |
| 168 | printf("%.*s\n", (int)(lend - start), start); |
Johannes Berg | 01ae06f | 2009-05-05 14:48:16 +0200 | [diff] [blame] | 169 | start = lend + 1; |
| 170 | } while (end != lend); |
| 171 | |
Holger Schurig | bd66389 | 2009-08-24 11:17:19 +0200 | [diff] [blame] | 172 | printf("\n"); |
Johannes Berg | 3bb116d | 2009-05-05 14:06:39 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Johannes Berg | 4f0cae7 | 2009-05-05 14:17:28 +0200 | [diff] [blame] | 175 | static void usage_options(void) |
| 176 | { |
Holger Schurig | bd66389 | 2009-08-24 11:17:19 +0200 | [diff] [blame] | 177 | printf("Options:\n"); |
| 178 | printf("\t--debug\t\tenable netlink debugging\n"); |
Johannes Berg | 4f0cae7 | 2009-05-05 14:17:28 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Johannes Berg | 01ae06f | 2009-05-05 14:48:16 +0200 | [diff] [blame] | 181 | static const char *argv0; |
| 182 | |
Johannes Berg | f3ac8bf | 2012-03-26 11:15:49 +0200 | [diff] [blame] | 183 | static void usage(int argc, char **argv) |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 184 | { |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 185 | const struct cmd *section, *cmd; |
Johannes Berg | f3ac8bf | 2012-03-26 11:15:49 +0200 | [diff] [blame] | 186 | 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 Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 195 | |
Holger Schurig | bd66389 | 2009-08-24 11:17:19 +0200 | [diff] [blame] | 196 | printf("Usage:\t%s [options] command\n", argv0); |
Johannes Berg | 4f0cae7 | 2009-05-05 14:17:28 +0200 | [diff] [blame] | 197 | usage_options(); |
Holger Schurig | bd66389 | 2009-08-24 11:17:19 +0200 | [diff] [blame] | 198 | printf("\t--version\tshow version (%s)\n", iw_version); |
| 199 | printf("Commands:\n"); |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 200 | for_each_cmd(section) { |
| 201 | if (section->parent) |
Johannes Berg | 403b9c8 | 2008-09-17 00:19:53 +0200 | [diff] [blame] | 202 | continue; |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 203 | |
Johannes Berg | f3ac8bf | 2012-03-26 11:15:49 +0200 | [diff] [blame] | 204 | if (sect_filt && strcmp(section->name, sect_filt)) |
| 205 | continue; |
| 206 | |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 207 | 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 Berg | f3ac8bf | 2012-03-26 11:15:49 +0200 | [diff] [blame] | 215 | if (cmd_filt && strcmp(cmd->name, cmd_filt)) |
| 216 | continue; |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 217 | __usage_cmd(cmd, "\t", full); |
| 218 | } |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 219 | } |
Johannes Berg | 75f4204 | 2012-07-19 08:36:05 +0200 | [diff] [blame] | 220 | printf("\nCommands that use the netdev ('dev') can also be given the\n" |
| 221 | "'wdev' instead to identify the device.\n"); |
Holger Schurig | bd66389 | 2009-08-24 11:17:19 +0200 | [diff] [blame] | 222 | printf("\nYou can omit the 'phy' or 'dev' if " |
Johannes Berg | f4ec76d | 2009-07-06 13:10:39 +0200 | [diff] [blame] | 223 | "the identification is unique,\n" |
Johannes Berg | 8aefee9 | 2009-07-06 13:14:41 +0200 | [diff] [blame] | 224 | "e.g. \"iw wlan0 info\" or \"iw phy0 info\". " |
Johannes Berg | fbdb8d0 | 2009-07-30 14:12:53 +0200 | [diff] [blame] | 225 | "(Don't when scripting.)\n\n" |
| 226 | "Do NOT screenscrape this tool, we don't " |
| 227 | "consider its output stable.\n\n"); |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Johannes Berg | 01ae06f | 2009-05-05 14:48:16 +0200 | [diff] [blame] | 230 | static int print_help(struct nl80211_state *state, |
| 231 | struct nl_cb *cb, |
| 232 | struct nl_msg *msg, |
Johannes Berg | 05514f9 | 2012-07-19 11:50:50 +0200 | [diff] [blame] | 233 | int argc, char **argv, |
| 234 | enum id_input id) |
Johannes Berg | 01ae06f | 2009-05-05 14:48:16 +0200 | [diff] [blame] | 235 | { |
| 236 | exit(3); |
| 237 | } |
Johannes Berg | f3ac8bf | 2012-03-26 11:15:49 +0200 | [diff] [blame] | 238 | TOPLEVEL(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 Berg | 01ae06f | 2009-05-05 14:48:16 +0200 | [diff] [blame] | 241 | |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 242 | static void usage_cmd(const struct cmd *cmd) |
Johannes Berg | 4f0cae7 | 2009-05-05 14:17:28 +0200 | [diff] [blame] | 243 | { |
Holger Schurig | bd66389 | 2009-08-24 11:17:19 +0200 | [diff] [blame] | 244 | printf("Usage:\t%s [options] ", argv0); |
Johannes Berg | 01ae06f | 2009-05-05 14:48:16 +0200 | [diff] [blame] | 245 | __usage_cmd(cmd, "", true); |
Johannes Berg | 4f0cae7 | 2009-05-05 14:17:28 +0200 | [diff] [blame] | 246 | usage_options(); |
| 247 | } |
| 248 | |
Johannes Berg | d711f01 | 2008-09-16 21:56:25 +0200 | [diff] [blame] | 249 | static void version(void) |
| 250 | { |
Johannes Berg | 133b069 | 2009-04-21 00:53:00 +0200 | [diff] [blame] | 251 | printf("iw version %s\n", iw_version); |
Johannes Berg | d711f01 | 2008-09-16 21:56:25 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 254 | static int phy_lookup(char *name) |
Mike Kershaw | 1cdd901 | 2007-09-29 19:17:20 -0400 | [diff] [blame] | 255 | { |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 256 | 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 Berg | 989e97c | 2009-03-12 11:13:36 +0100 | [diff] [blame] | 262 | if (fd < 0) |
| 263 | return -1; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 264 | pos = read(fd, buf, sizeof(buf) - 1); |
Eric Sesterhenn | 8f253ee | 2009-11-02 10:46:38 +0100 | [diff] [blame] | 265 | if (pos < 0) { |
| 266 | close(fd); |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 267 | return -1; |
Eric Sesterhenn | 8f253ee | 2009-11-02 10:46:38 +0100 | [diff] [blame] | 268 | } |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 269 | buf[pos] = '\0'; |
Eric Sesterhenn | 8f253ee | 2009-11-02 10:46:38 +0100 | [diff] [blame] | 270 | close(fd); |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 271 | return atoi(buf); |
| 272 | } |
| 273 | |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 274 | static 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 Berg | 561c5b7 | 2008-09-16 19:06:53 +0200 | [diff] [blame] | 282 | static int finish_handler(struct nl_msg *msg, void *arg) |
| 283 | { |
Johannes Berg | 59c418c | 2008-10-24 21:45:01 +0200 | [diff] [blame] | 284 | int *ret = arg; |
| 285 | *ret = 0; |
Johannes Berg | 561c5b7 | 2008-09-16 19:06:53 +0200 | [diff] [blame] | 286 | return NL_SKIP; |
| 287 | } |
| 288 | |
| 289 | static int ack_handler(struct nl_msg *msg, void *arg) |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 290 | { |
| 291 | int *ret = arg; |
| 292 | *ret = 0; |
| 293 | return NL_STOP; |
| 294 | } |
| 295 | |
Johannes Berg | 4f0cae7 | 2009-05-05 14:17:28 +0200 | [diff] [blame] | 296 | static int __handle_cmd(struct nl80211_state *state, enum id_input idby, |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 297 | int argc, char **argv, const struct cmd **cmdout) |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 298 | { |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 299 | const struct cmd *cmd, *match = NULL, *sectcmd; |
Johannes Berg | 7c37a24 | 2009-04-08 13:13:28 +0200 | [diff] [blame] | 300 | struct nl_cb *cb; |
Sean Robinson | 5cb60f9 | 2011-03-17 07:58:41 -0700 | [diff] [blame] | 301 | struct nl_cb *s_cb; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 302 | struct nl_msg *msg; |
Johannes Berg | 290a3dd | 2012-07-19 08:15:41 +0200 | [diff] [blame] | 303 | signed long long devidx = 0; |
Johannes Berg | bb60b4a | 2009-04-08 14:11:24 +0200 | [diff] [blame] | 304 | int err, o_argc; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 305 | const char *command, *section; |
Johannes Berg | bb60b4a | 2009-04-08 14:11:24 +0200 | [diff] [blame] | 306 | char *tmp, **o_argv; |
Johannes Berg | 9927363 | 2009-03-20 09:59:30 +0100 | [diff] [blame] | 307 | enum command_identify_by command_idby = CIB_NONE; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 308 | |
Johannes Berg | 9927363 | 2009-03-20 09:59:30 +0100 | [diff] [blame] | 309 | if (argc <= 1 && idby != II_NONE) |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 310 | return 1; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 311 | |
Johannes Berg | bb60b4a | 2009-04-08 14:11:24 +0200 | [diff] [blame] | 312 | o_argc = argc; |
| 313 | o_argv = argv; |
| 314 | |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 315 | switch (idby) { |
Johannes Berg | 9927363 | 2009-03-20 09:59:30 +0100 | [diff] [blame] | 316 | 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 Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 326 | devidx = phy_lookup(*argv); |
| 327 | argc--; |
| 328 | argv++; |
| 329 | break; |
Johannes Berg | 9927363 | 2009-03-20 09:59:30 +0100 | [diff] [blame] | 330 | case II_NETDEV: |
| 331 | command_idby = CIB_NETDEV; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 332 | devidx = if_nametoindex(*argv); |
Johannes Berg | 989e97c | 2009-03-12 11:13:36 +0100 | [diff] [blame] | 333 | if (devidx == 0) |
| 334 | devidx = -1; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 335 | argc--; |
| 336 | argv++; |
| 337 | break; |
Johannes Berg | 290a3dd | 2012-07-19 08:15:41 +0200 | [diff] [blame] | 338 | 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 Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 345 | default: |
| 346 | break; |
| 347 | } |
| 348 | |
Johannes Berg | 989e97c | 2009-03-12 11:13:36 +0100 | [diff] [blame] | 349 | if (devidx < 0) |
| 350 | return -errno; |
| 351 | |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 352 | section = *argv; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 353 | argc--; |
| 354 | argv++; |
| 355 | |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 356 | for_each_cmd(sectcmd) { |
| 357 | if (sectcmd->parent) |
Johannes Berg | 403b9c8 | 2008-09-17 00:19:53 +0200 | [diff] [blame] | 358 | continue; |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 359 | /* ok ... bit of a hack for the dupe 'info' section */ |
| 360 | if (match && sectcmd->idby != command_idby) |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 361 | continue; |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 362 | if (strcmp(sectcmd->name, section) == 0) |
| 363 | match = sectcmd; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 366 | sectcmd = match; |
| 367 | match = NULL; |
| 368 | if (!sectcmd) |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 369 | return 1; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 370 | |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 371 | 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 Berg | 75f4204 | 2012-07-19 08:36:05 +0200 | [diff] [blame] | 379 | /* |
| 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 Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 386 | 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 Berg | 75f4204 | 2012-07-19 08:36:05 +0200 | [diff] [blame] | 408 | if (cmd->idby != command_idby && |
| 409 | !(cmd->idby == CIB_NETDEV && command_idby == CIB_WDEV)) |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 410 | return 1; |
| 411 | if (!cmd->handler) |
| 412 | return 1; |
| 413 | } |
| 414 | |
Johannes Berg | 1633ddf | 2010-11-24 08:16:47 +0100 | [diff] [blame] | 415 | if (cmd->selector) { |
| 416 | cmd = cmd->selector(argc, argv); |
| 417 | if (!cmd) |
| 418 | return 1; |
| 419 | } |
| 420 | |
Johannes Berg | 4f0cae7 | 2009-05-05 14:17:28 +0200 | [diff] [blame] | 421 | if (cmdout) |
| 422 | *cmdout = cmd; |
| 423 | |
Johannes Berg | bb60b4a | 2009-04-08 14:11:24 +0200 | [diff] [blame] | 424 | if (!cmd->cmd) { |
| 425 | argc = o_argc; |
| 426 | argv = o_argv; |
Johannes Berg | 05514f9 | 2012-07-19 11:50:50 +0200 | [diff] [blame] | 427 | return cmd->handler(state, NULL, NULL, argc, argv, idby); |
Johannes Berg | bb60b4a | 2009-04-08 14:11:24 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 430 | msg = nlmsg_alloc(); |
| 431 | if (!msg) { |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 432 | fprintf(stderr, "failed to allocate netlink message\n"); |
| 433 | return 2; |
| 434 | } |
| 435 | |
Johannes Berg | 957a0f0 | 2009-05-05 14:10:09 +0200 | [diff] [blame] | 436 | cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT); |
Sean Robinson | 5cb60f9 | 2011-03-17 07:58:41 -0700 | [diff] [blame] | 437 | s_cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT); |
| 438 | if (!cb || !s_cb) { |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 439 | fprintf(stderr, "failed to allocate netlink callbacks\n"); |
| 440 | err = 2; |
| 441 | goto out_free_msg; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 442 | } |
| 443 | |
Johannes Berg | f09cee6 | 2011-11-10 15:25:53 +0100 | [diff] [blame] | 444 | genlmsg_put(msg, 0, 0, state->nl80211_id, 0, |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 445 | cmd->nl_msg_flags, cmd->cmd, 0); |
| 446 | |
Johannes Berg | 9927363 | 2009-03-20 09:59:30 +0100 | [diff] [blame] | 447 | switch (command_idby) { |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 448 | 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 Berg | 290a3dd | 2012-07-19 08:15:41 +0200 | [diff] [blame] | 454 | case CIB_WDEV: |
| 455 | NLA_PUT_U64(msg, NL80211_ATTR_WDEV, devidx); |
| 456 | break; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 457 | default: |
| 458 | break; |
| 459 | } |
| 460 | |
Johannes Berg | 05514f9 | 2012-07-19 11:50:50 +0200 | [diff] [blame] | 461 | err = cmd->handler(state, cb, msg, argc, argv, idby); |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 462 | if (err) |
| 463 | goto out; |
| 464 | |
Sean Robinson | 5cb60f9 | 2011-03-17 07:58:41 -0700 | [diff] [blame] | 465 | nl_socket_set_cb(state->nl_sock, s_cb); |
| 466 | |
Pat Erley | 57077d6 | 2009-01-29 14:35:18 +0100 | [diff] [blame] | 467 | err = nl_send_auto_complete(state->nl_sock, msg); |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 468 | if (err < 0) |
| 469 | goto out; |
| 470 | |
Johannes Berg | c5c4471 | 2008-09-29 16:49:06 +0200 | [diff] [blame] | 471 | err = 1; |
| 472 | |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 473 | nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); |
Johannes Berg | 59c418c | 2008-10-24 21:45:01 +0200 | [diff] [blame] | 474 | nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err); |
Johannes Berg | 561c5b7 | 2008-09-16 19:06:53 +0200 | [diff] [blame] | 475 | nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err); |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 476 | |
Johannes Berg | c5c4471 | 2008-09-29 16:49:06 +0200 | [diff] [blame] | 477 | while (err > 0) |
Pat Erley | 57077d6 | 2009-01-29 14:35:18 +0100 | [diff] [blame] | 478 | nl_recvmsgs(state->nl_sock, cb); |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 479 | out: |
| 480 | nl_cb_put(cb); |
| 481 | out_free_msg: |
| 482 | nlmsg_free(msg); |
| 483 | return err; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 484 | nla_put_failure: |
| 485 | fprintf(stderr, "building message failed\n"); |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 486 | return 2; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 487 | } |
| 488 | |
Johannes Berg | 4f0cae7 | 2009-05-05 14:17:28 +0200 | [diff] [blame] | 489 | int 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 Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 495 | int main(int argc, char **argv) |
| 496 | { |
| 497 | struct nl80211_state nlstate; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 498 | int err; |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 499 | const struct cmd *cmd = NULL; |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 500 | |
Johannes Berg | f408e01 | 2008-09-18 19:32:11 +0200 | [diff] [blame] | 501 | /* calculate command size including padding */ |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 502 | cmd_size = abs((long)&__section_set - (long)&__section_get); |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 503 | /* strip off self */ |
| 504 | argc--; |
Mike Kershaw | 1cdd901 | 2007-09-29 19:17:20 -0400 | [diff] [blame] | 505 | argv0 = *argv++; |
| 506 | |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 507 | if (argc > 0 && strcmp(*argv, "--debug") == 0) { |
Johannes Berg | 957a0f0 | 2009-05-05 14:10:09 +0200 | [diff] [blame] | 508 | iw_debug = 1; |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 509 | argc--; |
| 510 | argv++; |
| 511 | } |
| 512 | |
Johannes Berg | d711f01 | 2008-09-16 21:56:25 +0200 | [diff] [blame] | 513 | if (argc > 0 && strcmp(*argv, "--version") == 0) { |
| 514 | version(); |
| 515 | return 0; |
| 516 | } |
| 517 | |
Johannes Berg | 01ae06f | 2009-05-05 14:48:16 +0200 | [diff] [blame] | 518 | /* need to treat "help" command specially so it works w/o nl80211 */ |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 519 | if (argc == 0 || strcmp(*argv, "help") == 0) { |
Johannes Berg | f3ac8bf | 2012-03-26 11:15:49 +0200 | [diff] [blame] | 520 | usage(argc - 1, argv + 1); |
Johannes Berg | 4a972f8 | 2008-09-16 23:21:56 +0200 | [diff] [blame] | 521 | return 0; |
Mike Kershaw | 1cdd901 | 2007-09-29 19:17:20 -0400 | [diff] [blame] | 522 | } |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 523 | |
Johannes Berg | 2bdb6bd | 2008-09-16 23:20:20 +0200 | [diff] [blame] | 524 | err = nl80211_init(&nlstate); |
| 525 | if (err) |
| 526 | return 1; |
| 527 | |
Johannes Berg | 957a0f0 | 2009-05-05 14:10:09 +0200 | [diff] [blame] | 528 | if (strcmp(*argv, "dev") == 0 && argc > 1) { |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 529 | argc--; |
| 530 | argv++; |
Johannes Berg | 4f0cae7 | 2009-05-05 14:17:28 +0200 | [diff] [blame] | 531 | err = __handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd); |
Johannes Berg | 811ec68 | 2009-04-19 15:05:38 +0200 | [diff] [blame] | 532 | } else if (strncmp(*argv, "phy", 3) == 0 && argc > 1) { |
Johannes Berg | 9927363 | 2009-03-20 09:59:30 +0100 | [diff] [blame] | 533 | if (strlen(*argv) == 3) { |
| 534 | argc--; |
| 535 | argv++; |
Johannes Berg | 4f0cae7 | 2009-05-05 14:17:28 +0200 | [diff] [blame] | 536 | err = __handle_cmd(&nlstate, II_PHY_NAME, argc, argv, &cmd); |
Johannes Berg | 9927363 | 2009-03-20 09:59:30 +0100 | [diff] [blame] | 537 | } else if (*(*argv + 3) == '#') |
Johannes Berg | 4f0cae7 | 2009-05-05 14:17:28 +0200 | [diff] [blame] | 538 | err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd); |
Johannes Berg | 9927363 | 2009-03-20 09:59:30 +0100 | [diff] [blame] | 539 | else |
Johannes Berg | f4ec76d | 2009-07-06 13:10:39 +0200 | [diff] [blame] | 540 | goto detect; |
Johannes Berg | 290a3dd | 2012-07-19 08:15:41 +0200 | [diff] [blame] | 541 | } else if (strcmp(*argv, "wdev") == 0 && argc > 1) { |
| 542 | argc--; |
| 543 | argv++; |
| 544 | err = __handle_cmd(&nlstate, II_WDEV, argc, argv, &cmd); |
Johannes Berg | f4ec76d | 2009-07-06 13:10:39 +0200 | [diff] [blame] | 545 | } 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 Berg | 66f8ca4 | 2009-07-07 21:37:17 +0200 | [diff] [blame] | 551 | else if ((idx = phy_lookup(argv[0])) >= 0) |
Johannes Berg | f4ec76d | 2009-07-06 13:10:39 +0200 | [diff] [blame] | 552 | idby = II_PHY_NAME; |
| 553 | err = __handle_cmd(&nlstate, idby, argc, argv, &cmd); |
| 554 | } |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 555 | |
Johannes Berg | 4f0cae7 | 2009-05-05 14:17:28 +0200 | [diff] [blame] | 556 | if (err == 1) { |
| 557 | if (cmd) |
Johannes Berg | 01ae06f | 2009-05-05 14:48:16 +0200 | [diff] [blame] | 558 | usage_cmd(cmd); |
Johannes Berg | 4f0cae7 | 2009-05-05 14:17:28 +0200 | [diff] [blame] | 559 | else |
Johannes Berg | f3ac8bf | 2012-03-26 11:15:49 +0200 | [diff] [blame] | 560 | usage(0, NULL); |
Johannes Berg | 4f0cae7 | 2009-05-05 14:17:28 +0200 | [diff] [blame] | 561 | } else if (err < 0) |
Johannes Berg | b49be3e | 2008-09-16 18:54:39 +0200 | [diff] [blame] | 562 | fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err); |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 563 | |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 564 | nl80211_cleanup(&nlstate); |
| 565 | |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 566 | return err; |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 567 | } |