]> git.ipfire.org Git - thirdparty/iw.git/blobdiff - iw.c
iw: fix some scan code indentation
[thirdparty/iw.git] / iw.c
diff --git a/iw.c b/iw.c
index ec5673619559243d11885e9bed3e708f550677ab..daa2a77008667fd806c9d694d113783cb09387ab 100644 (file)
--- a/iw.c
+++ b/iw.c
@@ -13,6 +13,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdbool.h>
+#include <linux/netlink.h>
 
 #include <netlink/genl/genl.h>
 #include <netlink/genl/family.h>
@@ -54,14 +55,19 @@ 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;
                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");
@@ -273,8 +279,35 @@ static int phy_lookup(char *name)
 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;
 }
 
@@ -454,7 +487,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,
@@ -495,7 +528,7 @@ static int __handle_cmd(struct nl80211_state *state, enum id_input idby,
                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:
@@ -516,7 +549,7 @@ int main(int argc, char **argv)
        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++;
@@ -570,11 +603,13 @@ int main(int argc, char **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);