]> git.ipfire.org Git - thirdparty/iw.git/blame - mpath.c
fix assoc/auth event parsing
[thirdparty/iw.git] / mpath.c
CommitLineData
2ef1be68 1#include <net/if.h>
3d1e8704 2#include <errno.h>
d5ac8ad3 3#include <string.h>
2ef1be68 4
3d1e8704
LCC
5#include <netlink/genl/genl.h>
6#include <netlink/genl/family.h>
7#include <netlink/genl/ctrl.h>
8#include <netlink/msg.h>
9#include <netlink/attr.h>
3d1e8704 10
f408e01b 11#include "nl80211.h"
3d1e8704
LCC
12#include "iw.h"
13
14enum plink_state {
15 LISTEN,
16 OPN_SNT,
17 OPN_RCVD,
18 CNF_RCVD,
19 ESTAB,
20 HOLDING,
21 BLOCKED
22};
23
24enum plink_actions {
25 PLINK_ACTION_UNDEFINED,
26 PLINK_ACTION_OPEN,
27 PLINK_ACTION_BLOCK,
28};
29
30
3d1e8704
LCC
31static int print_mpath_handler(struct nl_msg *msg, void *arg)
32{
33 struct nlattr *tb[NL80211_ATTR_MAX + 1];
34 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
35 struct nlattr *pinfo[NL80211_MPATH_INFO_MAX + 1];
36 char dst[20], next_hop[20], dev[20];
37 static struct nla_policy mpath_policy[NL80211_MPATH_INFO_MAX + 1] = {
38 [NL80211_MPATH_INFO_FRAME_QLEN] = { .type = NLA_U32 },
39 [NL80211_MPATH_INFO_DSN] = { .type = NLA_U32 },
40 [NL80211_MPATH_INFO_METRIC] = { .type = NLA_U32 },
41 [NL80211_MPATH_INFO_EXPTIME] = { .type = NLA_U32 },
42 [NL80211_MPATH_INFO_DISCOVERY_TIMEOUT] = { .type = NLA_U32 },
43 [NL80211_MPATH_INFO_DISCOVERY_RETRIES] = { .type = NLA_U8 },
44 [NL80211_MPATH_INFO_FLAGS] = { .type = NLA_U8 },
45 };
46
47 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
48 genlmsg_attrlen(gnlh, 0), NULL);
49
50 /*
51 * TODO: validate the interface and mac address!
52 * Otherwise, there's a race condition as soon as
53 * the kernel starts sending mpath notifications.
54 */
55
56 if (!tb[NL80211_ATTR_MPATH_INFO]) {
57 fprintf(stderr, "mpath info missing!");
58 return NL_SKIP;
59 }
60 if (nla_parse_nested(pinfo, NL80211_MPATH_INFO_MAX,
61 tb[NL80211_ATTR_MPATH_INFO],
62 mpath_policy)) {
63 fprintf(stderr, "failed to parse nested attributes!");
64 return NL_SKIP;
65 }
66
67 mac_addr_n2a(dst, nla_data(tb[NL80211_ATTR_MAC]));
68 mac_addr_n2a(next_hop, nla_data(tb[NL80211_ATTR_MPATH_NEXT_HOP]));
69 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
70 printf("%s %s %s", dst, next_hop, dev);
71 if (pinfo[NL80211_MPATH_INFO_DSN])
72 printf("\t%u",
73 nla_get_u32(pinfo[NL80211_MPATH_INFO_DSN]));
74 if (pinfo[NL80211_MPATH_INFO_METRIC])
75 printf("\t%u",
76 nla_get_u32(pinfo[NL80211_MPATH_INFO_METRIC]));
77 if (pinfo[NL80211_MPATH_INFO_FRAME_QLEN])
78 printf("\t%u",
79 nla_get_u32(pinfo[NL80211_MPATH_INFO_FRAME_QLEN]));
80 if (pinfo[NL80211_MPATH_INFO_EXPTIME])
81 printf("\t%u",
82 nla_get_u32(pinfo[NL80211_MPATH_INFO_EXPTIME]));
83 if (pinfo[NL80211_MPATH_INFO_DISCOVERY_TIMEOUT])
84 printf("\t%u",
85 nla_get_u32(pinfo[NL80211_MPATH_INFO_DISCOVERY_TIMEOUT]));
86 if (pinfo[NL80211_MPATH_INFO_DISCOVERY_RETRIES])
87 printf("\t%u",
88 nla_get_u8(pinfo[NL80211_MPATH_INFO_DISCOVERY_RETRIES]));
89 if (pinfo[NL80211_MPATH_INFO_FLAGS])
90 printf("\t0x%x",
91 nla_get_u8(pinfo[NL80211_MPATH_INFO_FLAGS]));
92
93 printf("\n");
94 return NL_SKIP;
95}
96
7c37a24d
JB
97static int handle_mpath_get(struct nl80211_state *state,
98 struct nl_cb *cb,
f5f937fb
JB
99 struct nl_msg *msg,
100 int argc, char **argv)
3d1e8704 101{
3d1e8704
LCC
102 unsigned char dst[ETH_ALEN];
103
f5f937fb 104 if (argc < 1)
5e75fd04 105 return 1;
3d1e8704
LCC
106
107 if (mac_addr_a2n(dst, argv[0])) {
108 fprintf(stderr, "invalid mac address\n");
5e75fd04 109 return 2;
3d1e8704
LCC
110 }
111 argc--;
112 argv++;
113
f5f937fb 114 if (argc)
5e75fd04 115 return 1;
3d1e8704
LCC
116
117 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
3d1e8704 118
3d1e8704 119 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_mpath_handler, NULL);
3d1e8704 120
70391ccf 121 return 0;
3d1e8704 122 nla_put_failure:
70391ccf 123 return -ENOBUFS;
3d1e8704 124}
f5f937fb 125COMMAND(mpath, get, "<MAC address>",
70cf4544
JB
126 NL80211_CMD_GET_MPATH, 0, CIB_NETDEV, handle_mpath_get,
127 "Get information on mesh path to the given node.");
f5f937fb 128COMMAND(mpath, del, "<MAC address>",
70cf4544
JB
129 NL80211_CMD_DEL_MPATH, 0, CIB_NETDEV, handle_mpath_get,
130 "Remove the mesh path to the given node.");
f5f937fb 131
7c37a24d
JB
132static int handle_mpath_set(struct nl80211_state *state,
133 struct nl_cb *cb,
f5f937fb
JB
134 struct nl_msg *msg,
135 int argc, char **argv)
3d1e8704 136{
3d1e8704
LCC
137 unsigned char dst[ETH_ALEN];
138 unsigned char next_hop[ETH_ALEN];
139
f5f937fb 140 if (argc < 3)
5e75fd04 141 return 1;
3d1e8704
LCC
142
143 if (mac_addr_a2n(dst, argv[0])) {
144 fprintf(stderr, "invalid destination mac address\n");
5e75fd04 145 return 2;
3d1e8704
LCC
146 }
147 argc--;
148 argv++;
149
f5f937fb 150 if (strcmp("next_hop", argv[0]) != 0)
5e75fd04 151 return 1;
3d1e8704
LCC
152 argc--;
153 argv++;
154
155 if (mac_addr_a2n(next_hop, argv[0])) {
156 fprintf(stderr, "invalid next hop mac address\n");
5e75fd04 157 return 2;
3d1e8704
LCC
158 }
159 argc--;
160 argv++;
161
f5f937fb 162 if (argc)
5e75fd04 163 return 1;
3d1e8704 164
3d1e8704 165 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
3d1e8704
LCC
166 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
167
3d1e8704 168 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_mpath_handler, NULL);
70391ccf 169 return 0;
3d1e8704 170 nla_put_failure:
70391ccf 171 return -ENOBUFS;
3d1e8704 172}
f5f937fb 173COMMAND(mpath, new, "<destination MAC address> next_hop <next hop MAC address>",
70cf4544
JB
174 NL80211_CMD_NEW_MPATH, 0, CIB_NETDEV, handle_mpath_set,
175 "Create a new mesh path (instead of relying on automatic discovery).");
f5f937fb 176COMMAND(mpath, set, "<destination MAC address> next_hop <next hop MAC address>",
70cf4544
JB
177 NL80211_CMD_SET_MPATH, 0, CIB_NETDEV, handle_mpath_set,
178 "Set an existing mesh path's next hop.");
3d1e8704 179
7c37a24d
JB
180static int handle_mpath_dump(struct nl80211_state *state,
181 struct nl_cb *cb,
f5f937fb
JB
182 struct nl_msg *msg,
183 int argc, char **argv)
3d1e8704 184{
3d1e8704 185 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_mpath_handler, NULL);
70391ccf 186 return 0;
3d1e8704 187}
f5f937fb 188COMMAND(mpath, dump, NULL,
70cf4544
JB
189 NL80211_CMD_GET_MPATH, NLM_F_DUMP, CIB_NETDEV, handle_mpath_dump,
190 "List known mesh paths.");