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> |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 21 | |
Johannes Berg | f408e01 | 2008-09-18 19:32:11 +0200 | [diff] [blame] | 22 | #include "nl80211.h" |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 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 | |
Pat Erley | dfd13ee | 2008-12-13 01:04:05 +0100 | [diff] [blame] | 26 | #ifndef CONFIG_LIBNL20 |
| 27 | /* libnl 2.0 compatibility code */ |
| 28 | |
| 29 | static inline struct nl_handle *nl_socket_alloc(void) |
| 30 | { |
| 31 | return nl_handle_alloc(); |
| 32 | } |
| 33 | |
| 34 | static inline void nl_socket_free(struct nl_handle *h) |
| 35 | { |
| 36 | nl_handle_destroy(h); |
| 37 | } |
| 38 | |
| 39 | static inline int __genl_ctrl_alloc_cache(struct nl_handle *h, struct nl_cache **cache) |
| 40 | { |
| 41 | struct nl_cache *tmp = genl_ctrl_alloc_cache(h); |
| 42 | if (!tmp) |
| 43 | return -ENOMEM; |
| 44 | *cache = tmp; |
| 45 | return 0; |
| 46 | } |
| 47 | #define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache |
| 48 | #endif /* CONFIG_LIBNL20 */ |
| 49 | |
Johannes Berg | 656aa24 | 2008-12-08 18:30:22 +0100 | [diff] [blame] | 50 | static int debug = 0; |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 51 | |
| 52 | static int nl80211_init(struct nl80211_state *state) |
| 53 | { |
| 54 | int err; |
| 55 | |
Pat Erley | dfd13ee | 2008-12-13 01:04:05 +0100 | [diff] [blame] | 56 | state->nl_handle = nl_socket_alloc(); |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 57 | if (!state->nl_handle) { |
| 58 | fprintf(stderr, "Failed to allocate netlink handle.\n"); |
| 59 | return -ENOMEM; |
| 60 | } |
| 61 | |
| 62 | if (genl_connect(state->nl_handle)) { |
| 63 | fprintf(stderr, "Failed to connect to generic netlink.\n"); |
| 64 | err = -ENOLINK; |
| 65 | goto out_handle_destroy; |
| 66 | } |
| 67 | |
Pat Erley | dfd13ee | 2008-12-13 01:04:05 +0100 | [diff] [blame] | 68 | if (genl_ctrl_alloc_cache(state->nl_handle, &state->nl_cache)) { |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 69 | fprintf(stderr, "Failed to allocate generic netlink cache.\n"); |
| 70 | err = -ENOMEM; |
| 71 | goto out_handle_destroy; |
| 72 | } |
| 73 | |
| 74 | state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211"); |
| 75 | if (!state->nl80211) { |
| 76 | fprintf(stderr, "nl80211 not found.\n"); |
| 77 | err = -ENOENT; |
| 78 | goto out_cache_free; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | |
| 83 | out_cache_free: |
| 84 | nl_cache_free(state->nl_cache); |
| 85 | out_handle_destroy: |
Pat Erley | dfd13ee | 2008-12-13 01:04:05 +0100 | [diff] [blame] | 86 | nl_socket_free(state->nl_handle); |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 87 | return err; |
| 88 | } |
| 89 | |
| 90 | static void nl80211_cleanup(struct nl80211_state *state) |
| 91 | { |
| 92 | genl_family_put(state->nl80211); |
| 93 | nl_cache_free(state->nl_cache); |
Pat Erley | dfd13ee | 2008-12-13 01:04:05 +0100 | [diff] [blame] | 94 | nl_socket_free(state->nl_handle); |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Johannes Berg | ce5af55 | 2008-09-17 13:27:04 +0200 | [diff] [blame] | 97 | __COMMAND(NULL, NULL, NULL, 0, 0, 0, CIB_NONE, NULL); |
| 98 | __COMMAND(NULL, NULL, NULL, 1, 0, 0, CIB_NONE, NULL); |
Johannes Berg | 403b9c8 | 2008-09-17 00:19:53 +0200 | [diff] [blame] | 99 | |
| 100 | static int cmd_size; |
| 101 | |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 102 | static void usage(const char *argv0) |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 103 | { |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 104 | struct cmd *cmd; |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 105 | |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 106 | fprintf(stderr, "Usage:\t%s [options] command\n", argv0); |
| 107 | fprintf(stderr, "Options:\n"); |
Johannes Berg | d711f01 | 2008-09-16 21:56:25 +0200 | [diff] [blame] | 108 | fprintf(stderr, "\t--debug\t\tenable netlink debugging\n"); |
| 109 | fprintf(stderr, "\t--version\tshow version\n"); |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 110 | fprintf(stderr, "Commands:\n"); |
Johannes Berg | 2c61ba6 | 2008-10-24 20:24:44 +0200 | [diff] [blame] | 111 | fprintf(stderr, "\thelp\n"); |
| 112 | fprintf(stderr, "\tevent\n"); |
Johannes Berg | 403b9c8 | 2008-09-17 00:19:53 +0200 | [diff] [blame] | 113 | for (cmd = &__start___cmd; cmd < &__stop___cmd; |
| 114 | cmd = (struct cmd *)((char *)cmd + cmd_size)) { |
Johannes Berg | ce5af55 | 2008-09-17 13:27:04 +0200 | [diff] [blame] | 115 | if (!cmd->handler || cmd->hidden) |
Johannes Berg | 403b9c8 | 2008-09-17 00:19:53 +0200 | [diff] [blame] | 116 | continue; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 117 | switch (cmd->idby) { |
| 118 | case CIB_NONE: |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 119 | fprintf(stderr, "\t"); |
Johannes Berg | d631650 | 2008-09-16 17:05:33 +0200 | [diff] [blame] | 120 | /* fall through */ |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 121 | case CIB_PHY: |
Johannes Berg | d631650 | 2008-09-16 17:05:33 +0200 | [diff] [blame] | 122 | if (cmd->idby == CIB_PHY) |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 123 | fprintf(stderr, "\tphy <phyname> "); |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 124 | /* fall through */ |
| 125 | case CIB_NETDEV: |
| 126 | if (cmd->idby == CIB_NETDEV) |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 127 | fprintf(stderr, "\tdev <devname> "); |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 128 | if (cmd->section) |
| 129 | fprintf(stderr, "%s ", cmd->section); |
| 130 | fprintf(stderr, "%s", cmd->name); |
| 131 | if (cmd->args) |
| 132 | fprintf(stderr, " %s", cmd->args); |
| 133 | fprintf(stderr, "\n"); |
| 134 | break; |
| 135 | } |
| 136 | } |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Johannes Berg | d711f01 | 2008-09-16 21:56:25 +0200 | [diff] [blame] | 139 | static void version(void) |
| 140 | { |
Johannes Berg | 2dc285b | 2008-09-16 22:12:48 +0200 | [diff] [blame] | 141 | printf("iw version " IW_VERSION "\n"); |
Johannes Berg | d711f01 | 2008-09-16 21:56:25 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 144 | static int phy_lookup(char *name) |
Mike Kershaw | 1cdd901 | 2007-09-29 19:17:20 -0400 | [diff] [blame] | 145 | { |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 146 | char buf[200]; |
| 147 | int fd, pos; |
| 148 | |
| 149 | snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name); |
| 150 | |
| 151 | fd = open(buf, O_RDONLY); |
| 152 | pos = read(fd, buf, sizeof(buf) - 1); |
| 153 | if (pos < 0) |
| 154 | return -1; |
| 155 | buf[pos] = '\0'; |
| 156 | return atoi(buf); |
| 157 | } |
| 158 | |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 159 | static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, |
| 160 | void *arg) |
| 161 | { |
| 162 | int *ret = arg; |
| 163 | *ret = err->error; |
| 164 | return NL_STOP; |
| 165 | } |
| 166 | |
Johannes Berg | 561c5b7 | 2008-09-16 19:06:53 +0200 | [diff] [blame] | 167 | static int finish_handler(struct nl_msg *msg, void *arg) |
| 168 | { |
Johannes Berg | 59c418c | 2008-10-24 21:45:01 +0200 | [diff] [blame] | 169 | int *ret = arg; |
| 170 | *ret = 0; |
Johannes Berg | 561c5b7 | 2008-09-16 19:06:53 +0200 | [diff] [blame] | 171 | return NL_SKIP; |
| 172 | } |
| 173 | |
| 174 | static int ack_handler(struct nl_msg *msg, void *arg) |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 175 | { |
| 176 | int *ret = arg; |
| 177 | *ret = 0; |
| 178 | return NL_STOP; |
| 179 | } |
| 180 | |
Johannes Berg | d631650 | 2008-09-16 17:05:33 +0200 | [diff] [blame] | 181 | static int handle_cmd(struct nl80211_state *state, |
| 182 | enum command_identify_by idby, |
| 183 | int argc, char **argv) |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 184 | { |
| 185 | struct cmd *cmd; |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 186 | struct nl_cb *cb = NULL; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 187 | struct nl_msg *msg; |
| 188 | int devidx = 0; |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 189 | int err; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 190 | const char *command, *section; |
| 191 | |
Johannes Berg | d631650 | 2008-09-16 17:05:33 +0200 | [diff] [blame] | 192 | if (argc <= 1 && idby != CIB_NONE) |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 193 | return 1; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 194 | |
| 195 | switch (idby) { |
| 196 | case CIB_PHY: |
| 197 | devidx = phy_lookup(*argv); |
| 198 | argc--; |
| 199 | argv++; |
| 200 | break; |
| 201 | case CIB_NETDEV: |
| 202 | devidx = if_nametoindex(*argv); |
| 203 | argc--; |
| 204 | argv++; |
| 205 | break; |
| 206 | default: |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | section = command = *argv; |
| 211 | argc--; |
| 212 | argv++; |
| 213 | |
Johannes Berg | 403b9c8 | 2008-09-17 00:19:53 +0200 | [diff] [blame] | 214 | for (cmd = &__start___cmd; cmd < &__stop___cmd; |
| 215 | cmd = (struct cmd *)((char *)cmd + cmd_size)) { |
| 216 | if (!cmd->handler) |
| 217 | continue; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 218 | if (cmd->idby != idby) |
| 219 | continue; |
| 220 | if (cmd->section) { |
| 221 | if (strcmp(cmd->section, section)) |
| 222 | continue; |
| 223 | /* this is a bit icky ... */ |
| 224 | if (command == section) { |
| 225 | if (argc <= 0) |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 226 | return 1; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 227 | command = *argv; |
| 228 | argc--; |
| 229 | argv++; |
| 230 | } |
| 231 | } else if (section != command) |
| 232 | continue; |
| 233 | if (strcmp(cmd->name, command)) |
| 234 | continue; |
| 235 | if (argc && !cmd->args) |
| 236 | continue; |
| 237 | break; |
| 238 | } |
| 239 | |
Johannes Berg | 7470103 | 2008-09-17 10:20:09 +0200 | [diff] [blame] | 240 | if (cmd >= &__stop___cmd) |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 241 | return 1; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 242 | |
| 243 | msg = nlmsg_alloc(); |
| 244 | if (!msg) { |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 245 | fprintf(stderr, "failed to allocate netlink message\n"); |
| 246 | return 2; |
| 247 | } |
| 248 | |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 249 | cb = nl_cb_alloc(debug ? NL_CB_DEBUG : NL_CB_DEFAULT); |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 250 | if (!cb) { |
| 251 | fprintf(stderr, "failed to allocate netlink callbacks\n"); |
| 252 | err = 2; |
| 253 | goto out_free_msg; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0, |
| 257 | cmd->nl_msg_flags, cmd->cmd, 0); |
| 258 | |
| 259 | switch (idby) { |
| 260 | case CIB_PHY: |
| 261 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx); |
| 262 | break; |
| 263 | case CIB_NETDEV: |
| 264 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx); |
| 265 | break; |
| 266 | default: |
| 267 | break; |
| 268 | } |
| 269 | |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 270 | err = cmd->handler(cb, msg, argc, argv); |
| 271 | if (err) |
| 272 | goto out; |
| 273 | |
| 274 | err = nl_send_auto_complete(state->nl_handle, msg); |
| 275 | if (err < 0) |
| 276 | goto out; |
| 277 | |
Johannes Berg | c5c4471 | 2008-09-29 16:49:06 +0200 | [diff] [blame] | 278 | err = 1; |
| 279 | |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 280 | nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); |
Johannes Berg | 59c418c | 2008-10-24 21:45:01 +0200 | [diff] [blame] | 281 | 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] | 282 | 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] | 283 | |
Johannes Berg | c5c4471 | 2008-09-29 16:49:06 +0200 | [diff] [blame] | 284 | while (err > 0) |
| 285 | nl_recvmsgs(state->nl_handle, cb); |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 286 | out: |
| 287 | nl_cb_put(cb); |
| 288 | out_free_msg: |
| 289 | nlmsg_free(msg); |
| 290 | return err; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 291 | nla_put_failure: |
| 292 | fprintf(stderr, "building message failed\n"); |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 293 | return 2; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 294 | } |
| 295 | |
Johannes Berg | 2c61ba6 | 2008-10-24 20:24:44 +0200 | [diff] [blame] | 296 | static int no_seq_check(struct nl_msg *msg, void *arg) |
| 297 | { |
| 298 | return NL_OK; |
| 299 | } |
| 300 | |
| 301 | static int print_event(struct nl_msg *msg, void *arg) |
| 302 | { |
| 303 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); |
| 304 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; |
| 305 | |
| 306 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), |
| 307 | genlmsg_attrlen(gnlh, 0), NULL); |
| 308 | |
| 309 | switch (gnlh->cmd) { |
Johannes Berg | 03109dc | 2008-12-08 18:32:43 +0100 | [diff] [blame] | 310 | case NL80211_CMD_NEW_WIPHY: |
Johannes Berg | 2c61ba6 | 2008-10-24 20:24:44 +0200 | [diff] [blame] | 311 | printf("wiphy rename: phy #%d to %s\n", |
| 312 | nla_get_u32(tb[NL80211_ATTR_WIPHY]), |
| 313 | nla_get_string(tb[NL80211_ATTR_WIPHY_NAME])); |
| 314 | break; |
Johannes Berg | 03109dc | 2008-12-08 18:32:43 +0100 | [diff] [blame] | 315 | default: |
| 316 | printf("unknown event: %d\n", gnlh->cmd); |
| 317 | break; |
Johannes Berg | 2c61ba6 | 2008-10-24 20:24:44 +0200 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | return NL_SKIP; |
| 321 | } |
| 322 | |
| 323 | static int listen_events(struct nl80211_state *state, |
| 324 | int argc, char **argv) |
| 325 | { |
| 326 | int mcid, ret; |
| 327 | struct nl_cb *cb = nl_cb_alloc(debug ? NL_CB_DEBUG : NL_CB_DEFAULT); |
| 328 | |
| 329 | if (!cb) { |
| 330 | fprintf(stderr, "failed to allocate netlink callbacks\n"); |
| 331 | return -ENOMEM; |
| 332 | } |
| 333 | |
| 334 | mcid = nl_get_multicast_id(state->nl_handle, "nl80211", "config"); |
| 335 | if (mcid < 0) |
| 336 | return mcid; |
| 337 | |
| 338 | ret = nl_socket_add_membership(state->nl_handle, mcid); |
| 339 | if (ret) |
| 340 | return ret; |
| 341 | |
| 342 | /* no sequence checking for multicast messages */ |
| 343 | nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); |
| 344 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_event, NULL); |
| 345 | |
| 346 | while (1) |
| 347 | nl_recvmsgs(state->nl_handle, cb); |
| 348 | |
| 349 | nl_cb_put(cb); |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 354 | int main(int argc, char **argv) |
| 355 | { |
| 356 | struct nl80211_state nlstate; |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 357 | int err; |
| 358 | const char *argv0; |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 359 | |
Johannes Berg | f408e01 | 2008-09-18 19:32:11 +0200 | [diff] [blame] | 360 | /* calculate command size including padding */ |
Johannes Berg | ce5af55 | 2008-09-17 13:27:04 +0200 | [diff] [blame] | 361 | cmd_size = abs((long)&__cmd_NULL_1_CIB_NONE_0 |
| 362 | - (long)&__cmd_NULL_0_CIB_NONE_0); |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 363 | /* strip off self */ |
| 364 | argc--; |
Mike Kershaw | 1cdd901 | 2007-09-29 19:17:20 -0400 | [diff] [blame] | 365 | argv0 = *argv++; |
| 366 | |
Johannes Berg | 59c49f0 | 2008-09-16 21:00:32 +0200 | [diff] [blame] | 367 | if (argc > 0 && strcmp(*argv, "--debug") == 0) { |
| 368 | debug = 1; |
| 369 | argc--; |
| 370 | argv++; |
| 371 | } |
| 372 | |
Johannes Berg | d711f01 | 2008-09-16 21:56:25 +0200 | [diff] [blame] | 373 | if (argc > 0 && strcmp(*argv, "--version") == 0) { |
| 374 | version(); |
| 375 | return 0; |
| 376 | } |
| 377 | |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 378 | if (argc == 0 || strcmp(*argv, "help") == 0) { |
Mike Kershaw | 1cdd901 | 2007-09-29 19:17:20 -0400 | [diff] [blame] | 379 | usage(argv0); |
Johannes Berg | 4a972f8 | 2008-09-16 23:21:56 +0200 | [diff] [blame] | 380 | return 0; |
Mike Kershaw | 1cdd901 | 2007-09-29 19:17:20 -0400 | [diff] [blame] | 381 | } |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 382 | |
Johannes Berg | 2bdb6bd | 2008-09-16 23:20:20 +0200 | [diff] [blame] | 383 | err = nl80211_init(&nlstate); |
| 384 | if (err) |
| 385 | return 1; |
| 386 | |
Johannes Berg | 2c61ba6 | 2008-10-24 20:24:44 +0200 | [diff] [blame] | 387 | if (strcmp(*argv, "event") == 0) { |
| 388 | err = listen_events(&nlstate, argc, argv); |
| 389 | } else if (strcmp(*argv, "dev") == 0) { |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 390 | argc--; |
| 391 | argv++; |
Johannes Berg | d631650 | 2008-09-16 17:05:33 +0200 | [diff] [blame] | 392 | err = handle_cmd(&nlstate, CIB_NETDEV, argc, argv); |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 393 | } else if (strcmp(*argv, "phy") == 0) { |
| 394 | argc--; |
| 395 | argv++; |
Johannes Berg | d631650 | 2008-09-16 17:05:33 +0200 | [diff] [blame] | 396 | err = handle_cmd(&nlstate, CIB_PHY, argc, argv); |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 397 | } else |
Johannes Berg | d631650 | 2008-09-16 17:05:33 +0200 | [diff] [blame] | 398 | err = handle_cmd(&nlstate, CIB_NONE, argc, argv); |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 399 | |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 400 | if (err == 1) |
Johannes Berg | bd396f2 | 2008-09-16 16:56:09 +0200 | [diff] [blame] | 401 | usage(argv0); |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 402 | if (err < 0) |
Johannes Berg | b49be3e | 2008-09-16 18:54:39 +0200 | [diff] [blame] | 403 | fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err); |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 404 | |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 405 | nl80211_cleanup(&nlstate); |
| 406 | |
Johannes Berg | 45c7212 | 2007-09-28 23:47:38 +0200 | [diff] [blame] | 407 | return err; |
Johannes Berg | cad53b3 | 2007-09-28 22:11:34 +0200 | [diff] [blame] | 408 | } |