]> git.ipfire.org Git - thirdparty/iw.git/blob - mesh.c
iw: Allow setting RSSI threshold for peering
[thirdparty/iw.git] / mesh.c
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
14 SECTION(mesh);
15
16
17 typedef struct _any_t {
18 union {
19 uint32_t as_32;
20 int32_t as_s32;
21 uint16_t as_16;
22 uint8_t as_8;
23 } u;
24 } _any;
25
26 /* describes a mesh parameter */
27 struct mesh_param_descr {
28 const char *name;
29 enum nl80211_meshconf_params mesh_param_num;
30 int (*nla_put_fn)(struct nl_msg*, int, _any*);
31 uint32_t (*parse_fn)(const char*, _any*);
32 void (*nla_print_fn)(struct nlattr *);
33 };
34
35 /* utility functions for manipulating and printing u8/u16/u32 values and
36 * timesouts. */
37 static int _my_nla_put_u8(struct nl_msg *n, int mesh_param_num, _any *value)
38 {
39 return nla_put(n, mesh_param_num, sizeof(uint8_t), &value->u.as_8);
40 }
41
42 static int _my_nla_put_u16(struct nl_msg *n, int mesh_param_num, _any *value)
43 {
44 return nla_put(n, mesh_param_num, sizeof(uint16_t), &value->u.as_16);
45 }
46
47 static int _my_nla_put_u32(struct nl_msg *n, int mesh_param_num, _any *value)
48 {
49 return nla_put(n, mesh_param_num, sizeof(uint32_t), &value->u.as_32);
50 }
51
52 static uint32_t _parse_u8(const char *str, _any *ret)
53 {
54 char *endptr = NULL;
55 unsigned long int v = strtoul(str, &endptr, 10);
56 if (*endptr != '\0')
57 return 0xff;
58 if (v > 0xff)
59 return 0xff;
60 ret->u.as_8 = (uint8_t)v;
61 return 0;
62 }
63
64 static uint32_t _parse_u8_as_bool(const char *str, _any *ret)
65 {
66 char *endptr = NULL;
67 unsigned long int v = strtoul(str, &endptr, 10);
68 if (*endptr != '\0')
69 return 0x1;
70 if (v > 0x1)
71 return 0x1;
72 ret->u.as_8 = (uint8_t)v;
73 return 0;
74 }
75
76 static uint32_t _parse_u16(const char *str, _any *ret)
77 {
78 char *endptr = NULL;
79 long int v = strtol(str, &endptr, 10);
80 if (*endptr != '\0')
81 return 0xffff;
82 if ((v < 0) || (v > 0xffff))
83 return 0xffff;
84 ret->u.as_16 = (uint16_t)v;
85 return 0;
86 }
87
88 static uint32_t _parse_u32(const char *str, _any *ret)
89 {
90 char *endptr = NULL;
91 long long int v = strtoll(str, &endptr, 10);
92 if (*endptr != '\0')
93 return 0xffffffff;
94 if ((v < 0) || (v > 0xffffffff))
95 return 0xffffffff;
96 ret->u.as_32 = (uint32_t)v;
97 return 0;
98 }
99
100 static uint32_t _parse_s32(const char *str, _any *ret)
101 {
102 char *endptr = NULL;
103 long int v = strtol(str, &endptr, 10);
104 if (*endptr != '\0')
105 return 0xffffffff;
106 if (v > 0xff)
107 return 0xffffffff;
108 ret->u.as_s32 = (int32_t)v;
109 return 0;
110 }
111
112
113 static void _print_u8(struct nlattr *a)
114 {
115 printf("%d", nla_get_u8(a));
116 }
117
118 static void _print_u16(struct nlattr *a)
119 {
120 printf("%d", nla_get_u16(a));
121 }
122
123 static void _print_u16_timeout(struct nlattr *a)
124 {
125 printf("%d milliseconds", nla_get_u16(a));
126 }
127
128 static void _print_u16_in_TUs(struct nlattr *a)
129 {
130 printf("%d TUs", nla_get_u16(a));
131 }
132
133 static void _print_u32(struct nlattr *a)
134 {
135 printf("%d", nla_get_u32(a));
136 }
137
138 static void _print_u32_timeout(struct nlattr *a)
139 {
140 printf("%u milliseconds", nla_get_u32(a));
141 }
142
143 static void _print_u32_in_TUs(struct nlattr *a)
144 {
145 printf("%d TUs", nla_get_u32(a));
146 }
147
148 static void _print_s32_in_dBm(struct nlattr *a)
149 {
150 printf("%d dBm", (int32_t) nla_get_u32(a));
151 }
152
153
154 /* The current mesh parameters */
155 const static struct mesh_param_descr _mesh_param_descrs[] =
156 {
157 {"mesh_retry_timeout",
158 NL80211_MESHCONF_RETRY_TIMEOUT,
159 _my_nla_put_u16, _parse_u16, _print_u16_timeout},
160 {"mesh_confirm_timeout",
161 NL80211_MESHCONF_CONFIRM_TIMEOUT,
162 _my_nla_put_u16, _parse_u16, _print_u16_timeout},
163 {"mesh_holding_timeout",
164 NL80211_MESHCONF_HOLDING_TIMEOUT,
165 _my_nla_put_u16, _parse_u16, _print_u16_timeout},
166 {"mesh_max_peer_links",
167 NL80211_MESHCONF_MAX_PEER_LINKS,
168 _my_nla_put_u16, _parse_u16, _print_u16},
169 {"mesh_max_retries",
170 NL80211_MESHCONF_MAX_RETRIES,
171 _my_nla_put_u8, _parse_u8, _print_u8},
172 {"mesh_ttl",
173 NL80211_MESHCONF_TTL,
174 _my_nla_put_u8, _parse_u8, _print_u8},
175 {"mesh_element_ttl",
176 NL80211_MESHCONF_ELEMENT_TTL,
177 _my_nla_put_u8, _parse_u8, _print_u8},
178 {"mesh_auto_open_plinks",
179 NL80211_MESHCONF_AUTO_OPEN_PLINKS,
180 _my_nla_put_u8, _parse_u8_as_bool, _print_u8},
181 {"mesh_hwmp_max_preq_retries",
182 NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
183 _my_nla_put_u8, _parse_u8, _print_u8},
184 {"mesh_path_refresh_time",
185 NL80211_MESHCONF_PATH_REFRESH_TIME,
186 _my_nla_put_u32, _parse_u32, _print_u32_timeout},
187 {"mesh_min_discovery_timeout",
188 NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
189 _my_nla_put_u16, _parse_u16, _print_u16_timeout},
190 {"mesh_hwmp_active_path_timeout",
191 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
192 _my_nla_put_u32, _parse_u32, _print_u32_in_TUs},
193 {"mesh_hwmp_preq_min_interval",
194 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
195 _my_nla_put_u16, _parse_u16, _print_u16_in_TUs},
196 {"mesh_hwmp_net_diameter_traversal_time",
197 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
198 _my_nla_put_u16, _parse_u16, _print_u16_in_TUs},
199 {"mesh_hwmp_rootmode", NL80211_MESHCONF_HWMP_ROOTMODE,
200 _my_nla_put_u8, _parse_u8, _print_u8},
201 {"mesh_hwmp_rann_interval", NL80211_MESHCONF_HWMP_RANN_INTERVAL,
202 _my_nla_put_u16, _parse_u16, _print_u16},
203 {"mesh_gate_announcements", NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
204 _my_nla_put_u8, _parse_u8, _print_u8},
205 {"mesh_fwding", NL80211_MESHCONF_FORWARDING,
206 _my_nla_put_u8, _parse_u8_as_bool, _print_u8},
207 {"mesh_sync_offset_max_neighor",
208 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
209 _my_nla_put_u32, _parse_u32, _print_u32},
210 {"mesh_rssi_threshold", NL80211_MESHCONF_RSSI_THRESHOLD,
211 _my_nla_put_u32, _parse_s32, _print_s32_in_dBm},
212 };
213
214 static void print_all_mesh_param_descr(void)
215 {
216 int i;
217
218 printf("Possible mesh parameters are:\n");
219
220 for (i = 0; i < ARRAY_SIZE(_mesh_param_descrs); i++)
221 printf(" - %s\n", _mesh_param_descrs[i].name);
222 }
223
224 static const struct mesh_param_descr *find_mesh_param(const char *name)
225 {
226 int i;
227 const struct mesh_param_descr *mdescr = NULL;
228
229 /* Find out what mesh parameter we want to change. */
230 for (i = 0; i < ARRAY_SIZE(_mesh_param_descrs); i++) {
231 if (!strcmp(_mesh_param_descrs[i].name, name))
232 return _mesh_param_descrs + i;
233 }
234
235 if (!mdescr) {
236 print_all_mesh_param_descr();
237 return NULL;
238 }
239 return mdescr;
240 }
241
242 /* Setter */
243 static int set_interface_meshparam(struct nl80211_state *state,
244 struct nl_cb *cb,
245 struct nl_msg *msg,
246 int argc, char **argv)
247 {
248 const struct mesh_param_descr *mdescr;
249 struct nlattr *container;
250 uint32_t ret;
251 int err;
252
253 container = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
254 if (!container)
255 return -ENOBUFS;
256
257 if (!argc)
258 return 1;
259
260 while (argc) {
261 const char *name;
262 char *value;
263 _any any;
264
265 memset(&any, 0, sizeof(_any));
266
267 name = argv[0];
268 value = strchr(name, '=');
269 if (value) {
270 *value = '\0';
271 value++;
272 argc--;
273 argv++;
274 } else {
275 /* backward compat -- accept w/o '=' */
276 if (argc < 2) {
277 printf("Must specify a value for %s.\n", name);
278 return 2;
279 }
280 value = argv[1];
281 argc -= 2;
282 argv += 2;
283 }
284
285 mdescr = find_mesh_param(name);
286 if (!mdescr)
287 return 2;
288
289 /* Parse the new value */
290 ret = mdescr->parse_fn(value, &any);
291 if (ret != 0) {
292 printf("%s must be set to a number "
293 "between 0 and %u\n", mdescr->name, ret);
294 return 2;
295 }
296
297 err = mdescr->nla_put_fn(msg, mdescr->mesh_param_num, &any);
298 if (err)
299 return err;
300 }
301 nla_nest_end(msg, container);
302
303 return err;
304 }
305
306 COMMAND(set, mesh_param, "<param>=<value> [<param>=<value>]*",
307 NL80211_CMD_SET_MESH_PARAMS, 0, CIB_NETDEV, set_interface_meshparam,
308 "Set mesh parameter (run command without any to see available ones).");
309
310 /* Getter */
311 static int print_mesh_param_handler(struct nl_msg *msg, void *arg)
312 {
313 const struct mesh_param_descr *mdescr = arg;
314 struct nlattr *attrs[NL80211_ATTR_MAX + 1];
315 struct nlattr *parent_attr;
316 struct nlattr *mesh_params[NL80211_MESHCONF_ATTR_MAX + 1];
317 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
318
319 /* locate NL80211_ATTR_MESH_PARAMS */
320 nla_parse(attrs, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
321 genlmsg_attrlen(gnlh, 0), NULL);
322 parent_attr = attrs[NL80211_ATTR_MESH_PARAMS];
323 if (!parent_attr)
324 return -EINVAL;
325
326 /* unpack the mesh parameters */
327 if (nla_parse_nested(mesh_params, NL80211_MESHCONF_ATTR_MAX,
328 parent_attr, NULL))
329 return -EINVAL;
330
331 if (!mdescr) {
332 int i;
333
334 for (i = 0; i < ARRAY_SIZE(_mesh_param_descrs); i++) {
335 mdescr = &_mesh_param_descrs[i];
336 printf("%s = ", mdescr->name);
337 mdescr->nla_print_fn(mesh_params[mdescr->mesh_param_num]);
338 printf("\n");
339 }
340 return NL_SKIP;
341 }
342
343 /* print out the mesh parameter */
344 mdescr->nla_print_fn(mesh_params[mdescr->mesh_param_num]);
345 printf("\n");
346 return NL_SKIP;
347 }
348
349 static int get_interface_meshparam(struct nl80211_state *state,
350 struct nl_cb *cb,
351 struct nl_msg *msg,
352 int argc, char **argv)
353 {
354 const struct mesh_param_descr *mdescr = NULL;
355
356 if (argc > 1)
357 return 1;
358
359 if (argc == 1) {
360 mdescr = find_mesh_param(argv[0]);
361 if (!mdescr)
362 return 2;
363 }
364
365 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
366 print_mesh_param_handler, (void *)mdescr);
367 return 0;
368 }
369
370 COMMAND(get, mesh_param, "[<param>]",
371 NL80211_CMD_GET_MESH_PARAMS, 0, CIB_NETDEV, get_interface_meshparam,
372 "Retrieve mesh parameter (run command without any to see available ones).");
373
374 static int join_mesh(struct nl80211_state *state, struct nl_cb *cb,
375 struct nl_msg *msg, int argc, char **argv)
376 {
377 struct nlattr *container;
378 float rate;
379 char *end;
380
381 if (argc < 1)
382 return 1;
383
384 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(argv[0]), argv[0]);
385 argc--;
386 argv++;
387
388 if (argc > 1 && strcmp(argv[0], "mcast-rate") == 0) {
389 argv++;
390 argc--;
391
392 rate = strtod(argv[0], &end);
393 if (*end != '\0')
394 return 1;
395
396 NLA_PUT_U32(msg, NL80211_ATTR_MCAST_RATE, (int)(rate * 10));
397 argv++;
398 argc--;
399 }
400
401 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
402 if (!container)
403 return -ENOBUFS;
404
405 if (argc > 1 && strcmp(argv[0], "vendor_sync") == 0) {
406 argv++;
407 argc--;
408 if (strcmp(argv[0], "on") == 0)
409 NLA_PUT_U8(msg,
410 NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC, 1);
411 else
412 NLA_PUT_U8(msg,
413 NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC, 0);
414 argv++;
415 argc--;
416 }
417 /* parse and put other NL80211_ATTR_MESH_SETUP elements here */
418
419 nla_nest_end(msg, container);
420
421 if (!argc)
422 return 0;
423 return set_interface_meshparam(state, cb, msg, argc, argv);
424 nla_put_failure:
425 return -ENOBUFS;
426 }
427 COMMAND(mesh, join, "<mesh ID> [mcast-rate <rate in Mbps>] [vendor_sync on|off]"
428 " [<param>=<value>]*",
429 NL80211_CMD_JOIN_MESH, 0, CIB_NETDEV, join_mesh,
430 "Join a mesh with the given mesh ID with mcast-rate and mesh parameters.");
431
432 static int leave_mesh(struct nl80211_state *state, struct nl_cb *cb,
433 struct nl_msg *msg, int argc, char **argv)
434 {
435 if (argc)
436 return 1;
437
438 return 0;
439 }
440 COMMAND(mesh, leave, NULL, NL80211_CMD_LEAVE_MESH, 0, CIB_NETDEV, leave_mesh,
441 "Leave a mesh.");