]> git.ipfire.org Git - thirdparty/iw.git/blobdiff - iw.c
iw: don't use NULL pointer in nla_nest_end()
[thirdparty/iw.git] / iw.c
diff --git a/iw.c b/iw.c
index b5e668cb6f55e1f04c957eeb97b554240f0c87f7..0f511d98f2e1e151648a14aa7d830ce995964716 100644 (file)
--- a/iw.c
+++ b/iw.c
@@ -34,6 +34,12 @@ static inline void nl_socket_free(struct nl_sock *h)
 {
        nl_handle_destroy(h);
 }
+
+static inline int nl_socket_set_buffer_size(struct nl_sock *sk,
+                                           int rxbuf, int txbuf)
+{
+       return nl_set_buffer_size(sk, rxbuf, txbuf);
+}
 #endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 */
 
 int iw_debug = 0;
@@ -48,6 +54,8 @@ static int nl80211_init(struct nl80211_state *state)
                return -ENOMEM;
        }
 
+       nl_socket_set_buffer_size(state->nl_sock, 8192, 8192);
+
        if (genl_connect(state->nl_sock)) {
                fprintf(stderr, "Failed to connect to generic netlink.\n");
                err = -ENOLINK;
@@ -98,13 +106,47 @@ static void __usage_cmd(const struct cmd *cmd, char *indent, bool full)
        case CIB_NETDEV:
                printf("dev <devname> ");
                break;
+       case CIB_WDEV:
+               printf("wdev <idx> ");
+               break;
        }
        if (cmd->parent && cmd->parent->name)
                printf("%s ", cmd->parent->name);
        printf("%s", cmd->name);
-       if (cmd->args)
-               printf(" %s", cmd->args);
-       printf("\n");
+
+       if (cmd->args) {
+               /* print line by line */
+               start = cmd->args;
+               end = strchr(start, '\0');
+               printf(" ");
+               do {
+                       lend = strchr(start, '\n');
+                       if (!lend)
+                               lend = end;
+                       if (start != cmd->args) {
+                               printf("\t");
+                               switch (cmd->idby) {
+                               case CIB_NONE:
+                                       break;
+                               case CIB_PHY:
+                                       printf("phy <phyname> ");
+                                       break;
+                               case CIB_NETDEV:
+                                       printf("dev <devname> ");
+                                       break;
+                               case CIB_WDEV:
+                                       printf("wdev <idx> ");
+                                       break;
+                               }
+                               if (cmd->parent && cmd->parent->name)
+                                       printf("%s ", cmd->parent->name);
+                               printf("%s ", cmd->name);
+                       }
+                       printf("%.*s\n", (int)(lend - start), start);
+                       start = lend + 1;
+               } while (end != lend);
+       } else
+               printf("\n");
 
        if (!full || !cmd->help)
                return;
@@ -138,9 +180,18 @@ static void usage_options(void)
 
 static const char *argv0;
 
-static void usage(bool full)
+static void usage(int argc, char **argv)
 {
        const struct cmd *section, *cmd;
+       bool full = argc >= 0;
+       const char *sect_filt = NULL;
+       const char *cmd_filt = NULL;
+
+       if (argc > 0)
+               sect_filt = argv[0];
+
+       if (argc > 1)
+               cmd_filt = argv[1];
 
        printf("Usage:\t%s [options] command\n", argv0);
        usage_options();
@@ -150,6 +201,9 @@ static void usage(bool full)
                if (section->parent)
                        continue;
 
+               if (sect_filt && strcmp(section->name, sect_filt))
+                       continue;
+
                if (section->handler && !section->hidden)
                        __usage_cmd(section, "\t", full);
 
@@ -158,9 +212,13 @@ static void usage(bool full)
                                continue;
                        if (!cmd->handler || cmd->hidden)
                                continue;
+                       if (cmd_filt && strcmp(cmd->name, cmd_filt))
+                               continue;
                        __usage_cmd(cmd, "\t", full);
                }
        }
+       printf("\nCommands that use the netdev ('dev') can also be given the\n"
+              "'wdev' instead to identify the device.\n");
        printf("\nYou can omit the 'phy' or 'dev' if "
                        "the identification is unique,\n"
                        "e.g. \"iw wlan0 info\" or \"iw phy0 info\". "
@@ -170,14 +228,15 @@ static void usage(bool full)
 }
 
 static int print_help(struct nl80211_state *state,
-                     struct nl_cb *cb,
                      struct nl_msg *msg,
-                     int argc, char **argv)
+                     int argc, char **argv,
+                     enum id_input id)
 {
        exit(3);
 }
-TOPLEVEL(help, NULL, 0, 0, CIB_NONE, print_help,
-        "Print usage for each command.");
+TOPLEVEL(help, "[command]", 0, 0, CIB_NONE, print_help,
+        "Print usage for all or a specific command, e.g.\n"
+        "\"help wowlan\" or \"help wowlan enable\".");
 
 static void usage_cmd(const struct cmd *cmd)
 {
@@ -233,6 +292,23 @@ static int ack_handler(struct nl_msg *msg, void *arg)
        return NL_STOP;
 }
 
+static int (*registered_handler)(struct nl_msg *, void *);
+static void *registered_handler_data;
+
+void register_handler(int (*handler)(struct nl_msg *, void *), void *data)
+{
+       registered_handler = handler;
+       registered_handler_data = data;
+}
+
+int valid_handler(struct nl_msg *msg, void *arg)
+{
+       if (registered_handler)
+               return registered_handler(msg, registered_handler_data);
+
+       return NL_OK;
+}
+
 static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
                        int argc, char **argv, const struct cmd **cmdout)
 {
@@ -240,7 +316,7 @@ static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
        struct nl_cb *cb;
        struct nl_cb *s_cb;
        struct nl_msg *msg;
-       int devidx = 0;
+       signed long long devidx = 0;
        int err, o_argc;
        const char *command, *section;
        char *tmp, **o_argv;
@@ -275,6 +351,13 @@ static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
                argc--;
                argv++;
                break;
+       case II_WDEV:
+               command_idby = CIB_WDEV;
+               devidx = strtoll(*argv, &tmp, 0);
+               if (*tmp != '\0')
+                       return 1;
+               argc--;
+               argv++;
        default:
                break;
        }
@@ -309,7 +392,13 @@ static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
                                continue;
                        if (cmd->parent != sectcmd)
                                continue;
-                       if (cmd->idby != command_idby)
+                       /*
+                        * ignore mismatch id by, but allow WDEV
+                        * in place of NETDEV
+                        */
+                       if (cmd->idby != command_idby &&
+                           !(cmd->idby == CIB_NETDEV &&
+                             command_idby == CIB_WDEV))
                                continue;
                        if (strcmp(cmd->name, command))
                                continue;
@@ -332,7 +421,8 @@ static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
                cmd = sectcmd;
                if (argc && !cmd->args)
                        return 1;
-               if (cmd->idby != command_idby)
+               if (cmd->idby != command_idby &&
+                   !(cmd->idby == CIB_NETDEV && command_idby == CIB_WDEV))
                        return 1;
                if (!cmd->handler)
                        return 1;
@@ -350,7 +440,7 @@ static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
        if (!cmd->cmd) {
                argc = o_argc;
                argv = o_argv;
-               return cmd->handler(state, NULL, NULL, argc, argv);
+               return cmd->handler(state, NULL, argc, argv, idby);
        }
 
        msg = nlmsg_alloc();
@@ -364,7 +454,7 @@ static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
        if (!cb || !s_cb) {
                fprintf(stderr, "failed to allocate netlink callbacks\n");
                err = 2;
-               goto out_free_msg;
+               goto out;
        }
 
        genlmsg_put(msg, 0, 0, state->nl80211_id, 0,
@@ -377,11 +467,14 @@ static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
        case CIB_NETDEV:
                NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
                break;
+       case CIB_WDEV:
+               NLA_PUT_U64(msg, NL80211_ATTR_WDEV, devidx);
+               break;
        default:
                break;
        }
 
-       err = cmd->handler(state, cb, msg, argc, argv);
+       err = cmd->handler(state, msg, argc, argv, idby);
        if (err)
                goto out;
 
@@ -396,12 +489,13 @@ static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
        nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
        nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
        nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
+       nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, valid_handler, NULL);
 
        while (err > 0)
                nl_recvmsgs(state->nl_sock, cb);
  out:
        nl_cb_put(cb);
- out_free_msg:
+       nl_cb_put(s_cb);
        nlmsg_free(msg);
        return err;
  nla_put_failure:
@@ -440,7 +534,7 @@ int main(int argc, char **argv)
 
        /* need to treat "help" command specially so it works w/o nl80211 */
        if (argc == 0 || strcmp(*argv, "help") == 0) {
-               usage(argc != 0);
+               usage(argc - 1, argv + 1);
                return 0;
        }
 
@@ -461,6 +555,10 @@ int main(int argc, char **argv)
                        err = __handle_cmd(&nlstate, II_PHY_IDX, argc, argv, &cmd);
                else
                        goto detect;
+       } else if (strcmp(*argv, "wdev") == 0 && argc > 1) {
+               argc--;
+               argv++;
+               err = __handle_cmd(&nlstate, II_WDEV, argc, argv, &cmd);
        } else {
                int idx;
                enum id_input idby = II_NONE;
@@ -476,7 +574,7 @@ int main(int argc, char **argv)
                if (cmd)
                        usage_cmd(cmd);
                else
-                       usage(false);
+                       usage(0, NULL);
        } else if (err < 0)
                fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);