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