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> |
| 15 | |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 16 | #include <netlink/genl/genl.h> |
| 17 | #include <netlink/genl/family.h> |
| 18 | #include <netlink/genl/ctrl.h> |
| 19 | #include <netlink/msg.h> |
| 20 | #include <netlink/attr.h> |
| 21 | #include <linux/nl80211.h> |
| 22 | |
| 23 | #include "iw.h" |
Johannes Berg | d711f01 | 2008-09-16 21:56:25 +0200 | [diff] [blame] | 24 | #include "version.h" |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 25 | |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 26 | int debug = 0; |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 27 | |
| 28 | static int nl80211_init(struct nl80211_state *state) |
| 29 | { |
| 30 | int err; |
| 31 | |
| 32 | state->nl_handle = nl_handle_alloc(); |
| 33 | if (!state->nl_handle) { |
| 34 | fprintf(stderr, "Failed to allocate netlink handle.\n"); |
| 35 | return -ENOMEM; |
| 36 | } |
| 37 | |
| 38 | if (genl_connect(state->nl_handle)) { |
| 39 | fprintf(stderr, "Failed to connect to generic netlink.\n"); |
| 40 | err = -ENOLINK; |
| 41 | goto out_handle_destroy; |
| 42 | } |
| 43 | |
| 44 | state->nl_cache = genl_ctrl_alloc_cache(state->nl_handle); |
| 45 | if (!state->nl_cache) { |
| 46 | fprintf(stderr, "Failed to allocate generic netlink cache.\n"); |
| 47 | err = -ENOMEM; |
| 48 | goto out_handle_destroy; |
| 49 | } |
| 50 | |
| 51 | state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211"); |
| 52 | if (!state->nl80211) { |
| 53 | fprintf(stderr, "nl80211 not found.\n"); |
| 54 | err = -ENOENT; |
| 55 | goto out_cache_free; |
| 56 | } |
| 57 | |
| 58 | return 0; |
| 59 | |
| 60 | out_cache_free: |
| 61 | nl_cache_free(state->nl_cache); |
| 62 | out_handle_destroy: |
| 63 | nl_handle_destroy(state->nl_handle); |
| 64 | return err; |
| 65 | } |
| 66 | |
| 67 | static void nl80211_cleanup(struct nl80211_state *state) |
| 68 | { |
| 69 | genl_family_put(state->nl80211); |
| 70 | nl_cache_free(state->nl_cache); |
| 71 | nl_handle_destroy(state->nl_handle); |
| 72 | } |
| 73 | |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 74 | static void usage(const char *argv0) |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 75 | { |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 76 | struct cmd *cmd; |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 77 | |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 78 | fprintf(stderr, "Usage:\t%s [options] command\n", argv0); |
| 79 | fprintf(stderr, "Options:\n"); |
Johannes Berg | d711f01 | 2008-09-16 21:56:25 +0200 | [diff] [blame] | 80 | fprintf(stderr, "\t--debug\t\tenable netlink debugging\n"); |
| 81 | fprintf(stderr, "\t--version\tshow version\n"); |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 82 | fprintf(stderr, "Commands:\n"); |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 83 | for (cmd = &__start___cmd; cmd < &__stop___cmd; cmd++) { |
| 84 | switch (cmd->idby) { |
| 85 | case CIB_NONE: |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 86 | fprintf(stderr, "\t"); |
Johannes Berg | d631650 | 2008-09-16 17:05:33 +0200 | [diff] [blame] | 87 | /* fall through */ |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 88 | case CIB_PHY: |
Johannes Berg | d631650 | 2008-09-16 17:05:33 +0200 | [diff] [blame] | 89 | if (cmd->idby == CIB_PHY) |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 90 | fprintf(stderr, "\tphy <phyname> "); |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 91 | /* fall through */ |
| 92 | case CIB_NETDEV: |
| 93 | if (cmd->idby == CIB_NETDEV) |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 94 | fprintf(stderr, "\tdev <devname> "); |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 95 | if (cmd->section) |
| 96 | fprintf(stderr, "%s ", cmd->section); |
| 97 | fprintf(stderr, "%s", cmd->name); |
| 98 | if (cmd->args) |
| 99 | fprintf(stderr, " %s", cmd->args); |
| 100 | fprintf(stderr, "\n"); |
| 101 | break; |
| 102 | } |
| 103 | } |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Johannes Berg | d711f01 | 2008-09-16 21:56:25 +0200 | [diff] [blame] | 106 | static void version(void) |
| 107 | { |
Johannes Berg | 2dc285b | 2008-09-16 22:12:48 +0200 | [diff] [blame^] | 108 | printf("iw version " IW_VERSION "\n"); |
Johannes Berg | d711f01 | 2008-09-16 21:56:25 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 111 | static int phy_lookup(char *name) |
Mike Kershaw | 1cdd901 | 2007-09-29 19:17:20 -0400 | [diff] [blame] | 112 | { |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 113 | char buf[200]; |
| 114 | int fd, pos; |
| 115 | |
| 116 | snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name); |
| 117 | |
| 118 | fd = open(buf, O_RDONLY); |
| 119 | pos = read(fd, buf, sizeof(buf) - 1); |
| 120 | if (pos < 0) |
| 121 | return -1; |
| 122 | buf[pos] = '\0'; |
| 123 | return atoi(buf); |
| 124 | } |
| 125 | |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 126 | static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, |
| 127 | void *arg) |
| 128 | { |
| 129 | int *ret = arg; |
| 130 | *ret = err->error; |
| 131 | return NL_STOP; |
| 132 | } |
| 133 | |
Johannes Berg | 561c5b7 | 2008-09-16 19:06:53 +0200 | [diff] [blame] | 134 | static int finish_handler(struct nl_msg *msg, void *arg) |
| 135 | { |
| 136 | return NL_SKIP; |
| 137 | } |
| 138 | |
| 139 | static int ack_handler(struct nl_msg *msg, void *arg) |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 140 | { |
| 141 | int *ret = arg; |
| 142 | *ret = 0; |
| 143 | return NL_STOP; |
| 144 | } |
| 145 | |
Johannes Berg | d631650 | 2008-09-16 17:05:33 +0200 | [diff] [blame] | 146 | static int handle_cmd(struct nl80211_state *state, |
| 147 | enum command_identify_by idby, |
| 148 | int argc, char **argv) |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 149 | { |
| 150 | struct cmd *cmd; |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 151 | struct nl_cb *cb = NULL; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 152 | struct nl_msg *msg; |
| 153 | int devidx = 0; |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 154 | int err; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 155 | const char *command, *section; |
| 156 | |
Johannes Berg | d631650 | 2008-09-16 17:05:33 +0200 | [diff] [blame] | 157 | if (argc <= 1 && idby != CIB_NONE) |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 158 | return 1; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 159 | |
| 160 | switch (idby) { |
| 161 | case CIB_PHY: |
| 162 | devidx = phy_lookup(*argv); |
| 163 | argc--; |
| 164 | argv++; |
| 165 | break; |
| 166 | case CIB_NETDEV: |
| 167 | devidx = if_nametoindex(*argv); |
| 168 | argc--; |
| 169 | argv++; |
| 170 | break; |
| 171 | default: |
| 172 | break; |
| 173 | } |
| 174 | |
| 175 | section = command = *argv; |
| 176 | argc--; |
| 177 | argv++; |
| 178 | |
| 179 | for (cmd = &__start___cmd; cmd < &__stop___cmd; cmd++) { |
| 180 | if (cmd->idby != idby) |
| 181 | continue; |
| 182 | if (cmd->section) { |
| 183 | if (strcmp(cmd->section, section)) |
| 184 | continue; |
| 185 | /* this is a bit icky ... */ |
| 186 | if (command == section) { |
| 187 | if (argc <= 0) |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 188 | return 1; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 189 | command = *argv; |
| 190 | argc--; |
| 191 | argv++; |
| 192 | } |
| 193 | } else if (section != command) |
| 194 | continue; |
| 195 | if (strcmp(cmd->name, command)) |
| 196 | continue; |
| 197 | if (argc && !cmd->args) |
| 198 | continue; |
| 199 | break; |
| 200 | } |
| 201 | |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 202 | if (cmd == &__stop___cmd) |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 203 | return 1; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 204 | |
| 205 | msg = nlmsg_alloc(); |
| 206 | if (!msg) { |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 207 | fprintf(stderr, "failed to allocate netlink message\n"); |
| 208 | return 2; |
| 209 | } |
| 210 | |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 211 | cb = nl_cb_alloc(debug ? NL_CB_DEBUG : NL_CB_DEFAULT); |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 212 | if (!cb) { |
| 213 | fprintf(stderr, "failed to allocate netlink callbacks\n"); |
| 214 | err = 2; |
| 215 | goto out_free_msg; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0, |
| 219 | cmd->nl_msg_flags, cmd->cmd, 0); |
| 220 | |
| 221 | switch (idby) { |
| 222 | case CIB_PHY: |
| 223 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx); |
| 224 | break; |
| 225 | case CIB_NETDEV: |
| 226 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx); |
| 227 | break; |
| 228 | default: |
| 229 | break; |
| 230 | } |
| 231 | |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 232 | err = cmd->handler(cb, msg, argc, argv); |
| 233 | if (err) |
| 234 | goto out; |
| 235 | |
| 236 | err = nl_send_auto_complete(state->nl_handle, msg); |
| 237 | if (err < 0) |
| 238 | goto out; |
| 239 | |
| 240 | nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); |
Johannes Berg | 561c5b7 | 2008-09-16 19:06:53 +0200 | [diff] [blame] | 241 | nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, NULL); |
| 242 | 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] | 243 | |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 244 | nl_recvmsgs(state->nl_handle, cb); |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 245 | out: |
| 246 | nl_cb_put(cb); |
| 247 | out_free_msg: |
| 248 | nlmsg_free(msg); |
| 249 | return err; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 250 | nla_put_failure: |
| 251 | fprintf(stderr, "building message failed\n"); |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 252 | return 2; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 255 | int main(int argc, char **argv) |
| 256 | { |
| 257 | struct nl80211_state nlstate; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 258 | int err; |
| 259 | const char *argv0; |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 260 | |
| 261 | err = nl80211_init(&nlstate); |
| 262 | if (err) |
| 263 | return 1; |
| 264 | |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 265 | /* strip off self */ |
| 266 | argc--; |
Mike Kershaw | 1cdd901 | 2007-09-29 19:17:20 -0400 | [diff] [blame] | 267 | argv0 = *argv++; |
| 268 | |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 269 | if (argc > 0 && strcmp(*argv, "--debug") == 0) { |
| 270 | debug = 1; |
| 271 | argc--; |
| 272 | argv++; |
| 273 | } |
| 274 | |
Johannes Berg | d711f01 | 2008-09-16 21:56:25 +0200 | [diff] [blame] | 275 | if (argc > 0 && strcmp(*argv, "--version") == 0) { |
| 276 | version(); |
| 277 | return 0; |
| 278 | } |
| 279 | |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 280 | if (argc == 0 || strcmp(*argv, "help") == 0) { |
Mike Kershaw | 1cdd901 | 2007-09-29 19:17:20 -0400 | [diff] [blame] | 281 | usage(argv0); |
| 282 | goto out; |
| 283 | } |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 284 | |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 285 | if (strcmp(*argv, "dev") == 0) { |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 286 | argc--; |
| 287 | argv++; |
Johannes Berg | d631650 | 2008-09-16 17:05:33 +0200 | [diff] [blame] | 288 | err = handle_cmd(&nlstate, CIB_NETDEV, argc, argv); |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 289 | } else if (strcmp(*argv, "phy") == 0) { |
| 290 | argc--; |
| 291 | argv++; |
Johannes Berg | d631650 | 2008-09-16 17:05:33 +0200 | [diff] [blame] | 292 | err = handle_cmd(&nlstate, CIB_PHY, argc, argv); |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 293 | } else |
Johannes Berg | d631650 | 2008-09-16 17:05:33 +0200 | [diff] [blame] | 294 | err = handle_cmd(&nlstate, CIB_NONE, argc, argv); |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 295 | |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 296 | if (err == 1) |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 297 | usage(argv0); |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 298 | if (err < 0) |
Johannes Berg | b49be3e | 2008-09-16 18:54:39 +0200 | [diff] [blame] | 299 | fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err); |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 300 | |
| 301 | out: |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 302 | nl80211_cleanup(&nlstate); |
| 303 | |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 304 | return err; |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 305 | } |