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