]> git.ipfire.org Git - thirdparty/iw.git/blobdiff - iw.c
iw: nan: Handle NAN Events
[thirdparty/iw.git] / iw.c
diff --git a/iw.c b/iw.c
index 6ec5a12536e74f80bf3f43b0e95ae7957185d316..76f152d775d8af83be3b7be6a2356c50c9719214 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>
@@ -62,6 +63,11 @@ static int nl80211_init(struct nl80211_state *state)
 
        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;
 }
 
@@ -561,8 +594,9 @@ int main(int argc, char **argv)
                err = __handle_cmd(&nlstate, II_WDEV, argc, argv, &cmd);
        } else {
                int idx;
-               enum id_input idby = II_NONE;
+               enum id_input idby;
  detect:
+               idby = II_NONE;
                if ((idx = if_nametoindex(argv[0])) != 0)
                        idby = II_NETDEV;
                else if ((idx = phy_lookup(argv[0])) >= 0)