]> git.ipfire.org Git - thirdparty/iw.git/blobdiff - mpath.c
update nl80211.h
[thirdparty/iw.git] / mpath.c
diff --git a/mpath.c b/mpath.c
index dd634d9e9fa10ec72418e28ad4dd007768ba6df5..2a559c293cc0db2b74358106cfd4f704bacae4df 100644 (file)
--- a/mpath.c
+++ b/mpath.c
@@ -1,4 +1,3 @@
-#include <linux/nl80211.h>
 #include <net/if.h>
 #include <errno.h>
 #include <string.h>
@@ -9,8 +8,11 @@
 #include <netlink/msg.h>
 #include <netlink/attr.h>
 
+#include "nl80211.h"
 #include "iw.h"
 
+SECTION(mpath);
+
 enum plink_state {
        LISTEN,
        OPN_SNT,
@@ -21,27 +23,6 @@ enum plink_state {
        BLOCKED
 };
 
-enum plink_actions {
-       PLINK_ACTION_UNDEFINED,
-       PLINK_ACTION_OPEN,
-       PLINK_ACTION_BLOCK,
-};
-
-
-static int wait_handler(struct nl_msg *msg, void *arg)
-{
-       int *finished = arg;
-
-       *finished = 1;
-       return NL_STOP;
-}
-
-static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
-                        void *arg)
-{
-       fprintf(stderr, "nl80211 error %d\n", err->error);
-       exit(err->error);
-}
 
 static int print_mpath_handler(struct nl_msg *msg, void *arg)
 {
@@ -51,12 +32,14 @@ static int print_mpath_handler(struct nl_msg *msg, void *arg)
        char dst[20], next_hop[20], dev[20];
        static struct nla_policy mpath_policy[NL80211_MPATH_INFO_MAX + 1] = {
                [NL80211_MPATH_INFO_FRAME_QLEN] = { .type = NLA_U32 },
-               [NL80211_MPATH_INFO_DSN] = { .type = NLA_U32 },
+               [NL80211_MPATH_INFO_SN] = { .type = NLA_U32 },
                [NL80211_MPATH_INFO_METRIC] = { .type = NLA_U32 },
                [NL80211_MPATH_INFO_EXPTIME] = { .type = NLA_U32 },
                [NL80211_MPATH_INFO_DISCOVERY_TIMEOUT] = { .type = NLA_U32 },
                [NL80211_MPATH_INFO_DISCOVERY_RETRIES] = { .type = NLA_U8 },
                [NL80211_MPATH_INFO_FLAGS] = { .type = NLA_U8 },
+               [NL80211_MPATH_INFO_HOP_COUNT] = { .type = NLA_U8 },
+               [NL80211_MPATH_INFO_PATH_CHANGE] = { .type = NLA_U32 },
        };
 
        nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
@@ -69,13 +52,13 @@ static int print_mpath_handler(struct nl_msg *msg, void *arg)
         */
 
        if (!tb[NL80211_ATTR_MPATH_INFO]) {
-               fprintf(stderr, "mpath info missing!");
+               fprintf(stderr, "mpath info missing!\n");
                return NL_SKIP;
        }
        if (nla_parse_nested(pinfo, NL80211_MPATH_INFO_MAX,
                             tb[NL80211_ATTR_MPATH_INFO],
                             mpath_policy)) {
-               fprintf(stderr, "failed to parse nested attributes!");
+               fprintf(stderr, "failed to parse nested attributes!\n");
                return NL_SKIP;
        }
 
@@ -83,9 +66,9 @@ static int print_mpath_handler(struct nl_msg *msg, void *arg)
        mac_addr_n2a(next_hop, nla_data(tb[NL80211_ATTR_MPATH_NEXT_HOP]));
        if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
        printf("%s %s %s", dst, next_hop, dev);
-       if (pinfo[NL80211_MPATH_INFO_DSN])
+       if (pinfo[NL80211_MPATH_INFO_SN])
                printf("\t%u",
-                       nla_get_u32(pinfo[NL80211_MPATH_INFO_DSN]));
+                       nla_get_u32(pinfo[NL80211_MPATH_INFO_SN]));
        if (pinfo[NL80211_MPATH_INFO_METRIC])
                printf("\t%u",
                        nla_get_u32(pinfo[NL80211_MPATH_INFO_METRIC]));
@@ -104,18 +87,61 @@ static int print_mpath_handler(struct nl_msg *msg, void *arg)
        if (pinfo[NL80211_MPATH_INFO_FLAGS])
                printf("\t0x%x",
                        nla_get_u8(pinfo[NL80211_MPATH_INFO_FLAGS]));
+       if (pinfo[NL80211_MPATH_INFO_HOP_COUNT])
+               printf("\t%u",
+                      nla_get_u8(pinfo[NL80211_MPATH_INFO_HOP_COUNT]));
+       if (pinfo[NL80211_MPATH_INFO_PATH_CHANGE])
+               printf("\t%u",
+                      nla_get_u32(pinfo[NL80211_MPATH_INFO_PATH_CHANGE]));
 
        printf("\n");
        return NL_SKIP;
 }
 
+static int handle_mpath_probe(struct nl80211_state *state,
+                             struct nl_msg *msg,
+                             int argc, char **argv,
+                             enum id_input id)
+{
+       unsigned char dst[ETH_ALEN];
+       unsigned char *frame;
+       size_t frame_len;
+
+       if (argc < 3)
+               return 1;
+
+       if (mac_addr_a2n(dst, argv[0])) {
+               fprintf(stderr, "invalid mac address\n");
+               return 2;
+       }
+
+       if (strcmp("frame", argv[1]) != 0)
+               return 1;
+
+       frame = parse_hex(argv[2], &frame_len);
+       if (!frame) {
+               fprintf(stderr, "invalid frame pattern: %p\n", frame);
+               return 2;
+       }
+
+       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
+       NLA_PUT(msg, NL80211_ATTR_FRAME, frame_len, frame);
+
+       return 0;
+ nla_put_failure:
+       return -ENOBUFS;
+}
+COMMAND(mpath, probe, "<destination MAC address> frame <frame>",
+       NL80211_CMD_PROBE_MESH_LINK, 0, CIB_NETDEV, handle_mpath_probe,
+       "Inject ethernet frame to given peer overriding the next hop\n"
+       "lookup from mpath table.\n."
+       "Example: iw dev wlan0 mpath probe xx:xx:xx:xx:xx:xx frame 01:xx:xx:00\n");
+
 static int handle_mpath_get(struct nl80211_state *state,
                            struct nl_msg *msg,
-                           int argc, char **argv)
+                           int argc, char **argv,
+                           enum id_input id)
 {
-       struct nl_cb *cb = NULL;
-       int err = -ENOMEM;
-       int finished = 0;
        unsigned char dst[ETH_ALEN];
 
        if (argc < 1)
@@ -133,40 +159,24 @@ static int handle_mpath_get(struct nl80211_state *state,
 
        NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
 
-       cb = nl_cb_alloc(NL_CB_CUSTOM);
-       if (!cb)
-               goto out;
-
-       if (nl_send_auto_complete(state->nl_handle, msg) < 0)
-               goto out;
-
-       nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_mpath_handler, NULL);
-       nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, wait_handler, &finished);
-       nl_cb_err(cb, NL_CB_CUSTOM, error_handler, NULL);
-
-       nl_recvmsgs(state->nl_handle, cb);
-       err = 0;
-
-       if (!finished)
-               err = nl_wait_for_ack(state->nl_handle);
+       register_handler(print_mpath_handler, NULL);
 
- out:
-       nl_cb_put(cb);
+       return 0;
  nla_put_failure:
-       return err;
+       return -ENOBUFS;
 }
 COMMAND(mpath, get, "<MAC address>",
-       NL80211_CMD_GET_MPATH, 0, CIB_NETDEV, handle_mpath_get);
+       NL80211_CMD_GET_MPATH, 0, CIB_NETDEV, handle_mpath_get,
+       "Get information on mesh path to the given node.");
 COMMAND(mpath, del, "<MAC address>",
-       NL80211_CMD_DEL_MPATH, 0, CIB_NETDEV, handle_mpath_get);
+       NL80211_CMD_DEL_MPATH, 0, CIB_NETDEV, handle_mpath_get,
+       "Remove the mesh path to the given node.");
 
 static int handle_mpath_set(struct nl80211_state *state,
                            struct nl_msg *msg,
-                           int argc, char **argv)
+                           int argc, char **argv,
+                           enum id_input id)
 {
-       struct nl_cb *cb = NULL;
-       int err = -ENOMEM;
-       int finished = 0;
        unsigned char dst[ETH_ALEN];
        unsigned char next_hop[ETH_ALEN];
 
@@ -195,67 +205,31 @@ static int handle_mpath_set(struct nl80211_state *state,
        if (argc)
                return 1;
 
-       msg = nlmsg_alloc();
-       if (!msg)
-               goto out;
-
        NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
        NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
 
-       cb = nl_cb_alloc(NL_CB_CUSTOM);
-       if (!cb)
-               goto out;
-
-       if ((err = nl_send_auto_complete(state->nl_handle, msg)) < 0)
-               goto out;
-
-       nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_mpath_handler, NULL);
-       nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, wait_handler, &finished);
-       nl_cb_err(cb, NL_CB_CUSTOM, error_handler, NULL);
-
-       nl_recvmsgs(state->nl_handle, cb);
-       err = 0;
-
-       if (!finished)
-               err = nl_wait_for_ack(state->nl_handle);
-
- out:
-       nl_cb_put(cb);
+       register_handler(print_mpath_handler, NULL);
+       return 0;
  nla_put_failure:
-       return err;
+       return -ENOBUFS;
 }
 COMMAND(mpath, new, "<destination MAC address> next_hop <next hop MAC address>",
-       NL80211_CMD_NEW_MPATH, 0, CIB_NETDEV, handle_mpath_set);
+       NL80211_CMD_NEW_MPATH, 0, CIB_NETDEV, handle_mpath_set,
+       "Create a new mesh path (instead of relying on automatic discovery).");
 COMMAND(mpath, set, "<destination MAC address> next_hop <next hop MAC address>",
-       NL80211_CMD_SET_MPATH, 0, CIB_NETDEV, handle_mpath_set);
+       NL80211_CMD_SET_MPATH, 0, CIB_NETDEV, handle_mpath_set,
+       "Set an existing mesh path's next hop.");
 
 static int handle_mpath_dump(struct nl80211_state *state,
                             struct nl_msg *msg,
-                            int argc, char **argv)
+                            int argc, char **argv,
+                            enum id_input id)
 {
-       struct nl_cb *cb = NULL;
-       int err = -ENOMEM;
-       int finished = 0;
-
-       cb = nl_cb_alloc(NL_CB_CUSTOM);
-       if (!cb)
-               goto out;
-
-       if ((err = nl_send_auto_complete(state->nl_handle, msg)) < 0)
-               goto out;
-
-       nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_mpath_handler, NULL);
-       nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, wait_handler, &finished);
-
-       nl_recvmsgs(state->nl_handle, cb);
-       err = 0;
-
-       if (!finished)
-               err = nl_wait_for_ack(state->nl_handle);
-
- out:
-       nl_cb_put(cb);
-       return err;
+       printf("DEST ADDR         NEXT HOP          IFACE\tSN\tMETRIC\tQLEN\t"
+              "EXPTIME\tDTIM\tDRET\tFLAGS\tHOP_COUNT\tPATH_CHANGE\n");
+       register_handler(print_mpath_handler, NULL);
+       return 0;
 }
 COMMAND(mpath, dump, NULL,
-       NL80211_CMD_GET_MPATH, NLM_F_DUMP, CIB_NETDEV, handle_mpath_dump);
+       NL80211_CMD_GET_MPATH, NLM_F_DUMP, CIB_NETDEV, handle_mpath_dump,
+       "List known mesh paths.");