#include <fcntl.h>
#include <unistd.h>
#include <stdbool.h>
+#include <linux/netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/family.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;
goto out_handle_destroy;
}
+ nl_socket_set_buffer_size(state->nl_sock, 8192, 8192);
+
+ /* try to set NETLINK_EXT_ACK to 1, ignoring errors */
+ err = 1;
+ setsockopt(nl_socket_get_fd(state->nl_sock), SOL_NETLINK,
+ NETLINK_EXT_ACK, &err, sizeof(err));
+
state->nl80211_id = genl_ctrl_resolve(state->nl_sock, "nl80211");
if (state->nl80211_id < 0) {
fprintf(stderr, "nl80211 not found.\n");
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;
__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\". "
}
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);
}
static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
void *arg)
{
+ struct nlmsghdr *nlh = (struct nlmsghdr *)err - 1;
+ int len = nlh->nlmsg_len;
+ struct nlattr *attrs;
+ struct nlattr *tb[NLMSGERR_ATTR_MAX + 1];
int *ret = arg;
+ int ack_len = sizeof(*nlh) + sizeof(int) + sizeof(*nlh);
+
*ret = err->error;
+
+ if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
+ return NL_STOP;
+
+ if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
+ ack_len += err->msg.nlmsg_len - sizeof(*nlh);
+
+ if (len <= ack_len)
+ return NL_STOP;
+
+ attrs = (void *)((unsigned char *)nlh + ack_len);
+ len -= ack_len;
+
+ nla_parse(tb, NLMSGERR_ATTR_MAX, attrs, len, NULL);
+ if (tb[NLMSGERR_ATTR_MSG]) {
+ len = strnlen((char *)nla_data(tb[NLMSGERR_ATTR_MSG]),
+ nla_len(tb[NLMSGERR_ATTR_MSG]));
+ fprintf(stderr, "kernel reports: %*s\n", len,
+ (char *)nla_data(tb[NLMSGERR_ATTR_MSG]));
+ }
+
return NL_STOP;
}
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)
{
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;
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;
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();
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,
break;
}
- err = cmd->handler(state, cb, msg, argc, argv);
+ err = cmd->handler(state, msg, argc, argv, idby);
if (err)
goto out;
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:
const struct cmd *cmd = NULL;
/* calculate command size including padding */
- cmd_size = abs((long)&__section_set - (long)&__section_get);
+ cmd_size = labs((long)&__section_set - (long)&__section_get);
/* strip off self */
argc--;
argv0 = *argv++;
err = __handle_cmd(&nlstate, idby, argc, argv, &cmd);
}
- if (err == 1) {
+ if (err == HANDLER_RET_USAGE) {
if (cmd)
usage_cmd(cmd);
else
usage(0, NULL);
+ } else if (err == HANDLER_RET_DONE) {
+ err = 0;
} else if (err < 0)
fprintf(stderr, "command failed: %s (%d)\n", strerror(-err), err);