]> git.ipfire.org Git - thirdparty/iw.git/blame - mesh.c
iw: handle all mesh config parameters
[thirdparty/iw.git] / mesh.c
CommitLineData
46c1ad1f 1#include <errno.h>
2#include <string.h>
3
4#include <netlink/genl/genl.h>
5#include <netlink/genl/family.h>
6#include <netlink/genl/ctrl.h>
7#include <netlink/msg.h>
8#include <netlink/attr.h>
9
10#include "nl80211.h"
11#include "iw.h"
12
400efd3e
JB
13SECTION(mesh);
14
15
46c1ad1f 16typedef struct _any_t {
17 union {
18 uint32_t as_32;
be97e513 19 int32_t as_s32;
46c1ad1f 20 uint16_t as_16;
21 uint8_t as_8;
22 } u;
23} _any;
24
25/* describes a mesh parameter */
26struct mesh_param_descr {
27 const char *name;
28 enum nl80211_meshconf_params mesh_param_num;
29 int (*nla_put_fn)(struct nl_msg*, int, _any*);
30 uint32_t (*parse_fn)(const char*, _any*);
31 void (*nla_print_fn)(struct nlattr *);
32};
33
34/* utility functions for manipulating and printing u8/u16/u32 values and
35 * timesouts. */
36static int _my_nla_put_u8(struct nl_msg *n, int mesh_param_num, _any *value)
37{
38 return nla_put(n, mesh_param_num, sizeof(uint8_t), &value->u.as_8);
39}
40
41static int _my_nla_put_u16(struct nl_msg *n, int mesh_param_num, _any *value)
42{
43 return nla_put(n, mesh_param_num, sizeof(uint16_t), &value->u.as_16);
44}
45
46static int _my_nla_put_u32(struct nl_msg *n, int mesh_param_num, _any *value)
47{
48 return nla_put(n, mesh_param_num, sizeof(uint32_t), &value->u.as_32);
49}
50
51static uint32_t _parse_u8(const char *str, _any *ret)
52{
53 char *endptr = NULL;
54 unsigned long int v = strtoul(str, &endptr, 10);
55 if (*endptr != '\0')
56 return 0xff;
57 if (v > 0xff)
58 return 0xff;
59 ret->u.as_8 = (uint8_t)v;
60 return 0;
61}
62
63static uint32_t _parse_u8_as_bool(const char *str, _any *ret)
64{
65 char *endptr = NULL;
66 unsigned long int v = strtoul(str, &endptr, 10);
67 if (*endptr != '\0')
68 return 0x1;
69 if (v > 0x1)
70 return 0x1;
71 ret->u.as_8 = (uint8_t)v;
72 return 0;
73}
74
75static uint32_t _parse_u16(const char *str, _any *ret)
76{
77 char *endptr = NULL;
78 long int v = strtol(str, &endptr, 10);
79 if (*endptr != '\0')
80 return 0xffff;
81 if ((v < 0) || (v > 0xffff))
82 return 0xffff;
83 ret->u.as_16 = (uint16_t)v;
84 return 0;
85}
86
87static uint32_t _parse_u32(const char *str, _any *ret)
88{
89 char *endptr = NULL;
90 long long int v = strtoll(str, &endptr, 10);
91 if (*endptr != '\0')
92 return 0xffffffff;
93 if ((v < 0) || (v > 0xffffffff))
94 return 0xffffffff;
95 ret->u.as_32 = (uint32_t)v;
96 return 0;
97}
98
be97e513
AN
99static uint32_t _parse_s32(const char *str, _any *ret)
100{
101 char *endptr = NULL;
102 long int v = strtol(str, &endptr, 10);
103 if (*endptr != '\0')
104 return 0xffffffff;
105 if (v > 0xff)
106 return 0xffffffff;
107 ret->u.as_s32 = (int32_t)v;
108 return 0;
109}
110
46758fa3
MP
111static uint32_t _parse_u32_power_mode(const char *str, _any *ret)
112{
113 unsigned long int v;
114
115 /* Parse attribute for the name of power mode */
116 if (!strcmp(str, "active"))
117 v = NL80211_MESH_POWER_ACTIVE;
118 else if (!strcmp(str, "light"))
119 v = NL80211_MESH_POWER_LIGHT_SLEEP;
120 else if (!strcmp(str, "deep"))
121 v = NL80211_MESH_POWER_DEEP_SLEEP;
122 else
123 return 0xff;
124
125 ret->u.as_32 = (uint32_t)v;
126 return 0;
127}
be97e513 128
656aa246 129static void _print_u8(struct nlattr *a)
46c1ad1f 130{
131 printf("%d", nla_get_u8(a));
132}
133
656aa246 134static void _print_u16(struct nlattr *a)
46c1ad1f 135{
136 printf("%d", nla_get_u16(a));
137}
138
656aa246 139static void _print_u16_timeout(struct nlattr *a)
46c1ad1f 140{
141 printf("%d milliseconds", nla_get_u16(a));
142}
143
656aa246 144static void _print_u16_in_TUs(struct nlattr *a)
46c1ad1f 145{
146 printf("%d TUs", nla_get_u16(a));
147}
148
d9f730c4
AN
149static void _print_u32(struct nlattr *a)
150{
151 printf("%d", nla_get_u32(a));
152}
153
656aa246 154static void _print_u32_timeout(struct nlattr *a)
46c1ad1f 155{
156 printf("%u milliseconds", nla_get_u32(a));
157}
158
770f80c9
CT
159static void _print_u32_in_seconds(struct nlattr *a)
160{
161 printf("%d seconds", nla_get_u32(a));
162}
163
656aa246 164static void _print_u32_in_TUs(struct nlattr *a)
46c1ad1f 165{
166 printf("%d TUs", nla_get_u32(a));
167}
168
46758fa3
MP
169static void _print_u32_power_mode(struct nlattr *a)
170{
171 unsigned long v = nla_get_u32(a);
172
173 switch (v) {
174 case NL80211_MESH_POWER_ACTIVE:
175 printf("active");
176 break;
177 case NL80211_MESH_POWER_LIGHT_SLEEP:
178 printf("light");
179 break;
180 case NL80211_MESH_POWER_DEEP_SLEEP:
181 printf("deep");
182 break;
183 default:
184 printf("undefined");
185 break;
186 }
187}
188
be97e513
AN
189static void _print_s32_in_dBm(struct nlattr *a)
190{
191 printf("%d dBm", (int32_t) nla_get_u32(a));
192}
193
194
46c1ad1f 195/* The current mesh parameters */
6ab936f0 196static const struct mesh_param_descr _mesh_param_descrs[] =
46c1ad1f 197{
198 {"mesh_retry_timeout",
199 NL80211_MESHCONF_RETRY_TIMEOUT,
200 _my_nla_put_u16, _parse_u16, _print_u16_timeout},
201 {"mesh_confirm_timeout",
202 NL80211_MESHCONF_CONFIRM_TIMEOUT,
203 _my_nla_put_u16, _parse_u16, _print_u16_timeout},
204 {"mesh_holding_timeout",
205 NL80211_MESHCONF_HOLDING_TIMEOUT,
206 _my_nla_put_u16, _parse_u16, _print_u16_timeout},
207 {"mesh_max_peer_links",
208 NL80211_MESHCONF_MAX_PEER_LINKS,
209 _my_nla_put_u16, _parse_u16, _print_u16},
210 {"mesh_max_retries",
211 NL80211_MESHCONF_MAX_RETRIES,
212 _my_nla_put_u8, _parse_u8, _print_u8},
213 {"mesh_ttl",
214 NL80211_MESHCONF_TTL,
215 _my_nla_put_u8, _parse_u8, _print_u8},
247beb99
JB
216 {"mesh_element_ttl",
217 NL80211_MESHCONF_ELEMENT_TTL,
218 _my_nla_put_u8, _parse_u8, _print_u8},
46c1ad1f 219 {"mesh_auto_open_plinks",
220 NL80211_MESHCONF_AUTO_OPEN_PLINKS,
221 _my_nla_put_u8, _parse_u8_as_bool, _print_u8},
222 {"mesh_hwmp_max_preq_retries",
223 NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
224 _my_nla_put_u8, _parse_u8, _print_u8},
225 {"mesh_path_refresh_time",
226 NL80211_MESHCONF_PATH_REFRESH_TIME,
227 _my_nla_put_u32, _parse_u32, _print_u32_timeout},
228 {"mesh_min_discovery_timeout",
229 NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
230 _my_nla_put_u16, _parse_u16, _print_u16_timeout},
231 {"mesh_hwmp_active_path_timeout",
232 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
233 _my_nla_put_u32, _parse_u32, _print_u32_in_TUs},
234 {"mesh_hwmp_preq_min_interval",
235 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
236 _my_nla_put_u16, _parse_u16, _print_u16_in_TUs},
237 {"mesh_hwmp_net_diameter_traversal_time",
238 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
239 _my_nla_put_u16, _parse_u16, _print_u16_in_TUs},
cc37218f
RP
240 {"mesh_hwmp_rootmode", NL80211_MESHCONF_HWMP_ROOTMODE,
241 _my_nla_put_u8, _parse_u8, _print_u8},
b9aa711a 242 {"mesh_hwmp_rann_interval", NL80211_MESHCONF_HWMP_RANN_INTERVAL,
b1b5713b 243 _my_nla_put_u16, _parse_u16, _print_u16_in_TUs},
b9aa711a
JC
244 {"mesh_gate_announcements", NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
245 _my_nla_put_u8, _parse_u8, _print_u8},
accd0d23
CYY
246 {"mesh_fwding", NL80211_MESHCONF_FORWARDING,
247 _my_nla_put_u8, _parse_u8_as_bool, _print_u8},
d9f730c4
AN
248 {"mesh_sync_offset_max_neighor",
249 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
250 _my_nla_put_u32, _parse_u32, _print_u32},
be97e513
AN
251 {"mesh_rssi_threshold", NL80211_MESHCONF_RSSI_THRESHOLD,
252 _my_nla_put_u32, _parse_s32, _print_s32_in_dBm},
b1b5713b
CYY
253 {"mesh_hwmp_active_path_to_root_timeout",
254 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
255 _my_nla_put_u32, _parse_u32, _print_u32_in_TUs},
256 {"mesh_hwmp_root_interval", NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
257 _my_nla_put_u16, _parse_u16, _print_u16_in_TUs},
258 {"mesh_hwmp_confirmation_interval",
259 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
260 _my_nla_put_u16, _parse_u16, _print_u16_in_TUs},
46758fa3
MP
261 {"mesh_power_mode", NL80211_MESHCONF_POWER_MODE,
262 _my_nla_put_u32, _parse_u32_power_mode, _print_u32_power_mode},
263 {"mesh_awake_window", NL80211_MESHCONF_AWAKE_WINDOW,
264 _my_nla_put_u16, _parse_u16, _print_u16_in_TUs},
770f80c9
CT
265 {"mesh_plink_timeout", NL80211_MESHCONF_PLINK_TIMEOUT,
266 _my_nla_put_u32, _parse_u32, _print_u32_in_seconds},
0789fd27
MT
267 {"mesh_connected_to_gate", NL80211_MESHCONF_CONNECTED_TO_GATE,
268 _my_nla_put_u8, _parse_u8_as_bool, _print_u8},
94ded888
LL
269 {"mesh_nolearn", NL80211_MESHCONF_NOLEARN,
270 _my_nla_put_u8, _parse_u8_as_bool, _print_u8},
0789fd27
MT
271 {"mesh_connected_to_as", NL80211_MESHCONF_CONNECTED_TO_AS,
272 _my_nla_put_u8, _parse_u8_as_bool, _print_u8},
46c1ad1f 273};
274
275static void print_all_mesh_param_descr(void)
276{
0ee571d5 277 unsigned int i;
46c1ad1f 278
f089471a
JB
279 printf("Possible mesh parameters are:\n");
280
281 for (i = 0; i < ARRAY_SIZE(_mesh_param_descrs); i++)
282 printf(" - %s\n", _mesh_param_descrs[i].name);
46c1ad1f 283}
284
a34f62cf 285static const struct mesh_param_descr *find_mesh_param(const char *name)
46c1ad1f 286{
0ee571d5 287 unsigned int i;
46c1ad1f 288
289 /* Find out what mesh parameter we want to change. */
4d0d2ea7 290 for (i = 0; i < ARRAY_SIZE(_mesh_param_descrs); i++) {
83cb979b 291 if (strcmp(_mesh_param_descrs[i].name, name) == 0)
46c1ad1f 292 return _mesh_param_descrs + i;
4d0d2ea7 293 }
46c1ad1f 294
83cb979b 295 return NULL;
46c1ad1f 296}
297
298/* Setter */
7c37a24d 299static int set_interface_meshparam(struct nl80211_state *state,
7c37a24d 300 struct nl_msg *msg,
05514f95
JB
301 int argc, char **argv,
302 enum id_input id)
46c1ad1f 303{
46c1ad1f 304 const struct mesh_param_descr *mdescr;
656aa246 305 struct nlattr *container;
a34f62cf 306 uint32_t ret;
1ea6085c 307 int err = 2;
46c1ad1f 308
656aa246 309 container = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
46c1ad1f 310 if (!container)
311 return -ENOBUFS;
a34f62cf 312
33cbe6cd
OO
313 if (!argc) {
314 print_all_mesh_param_descr();
a34f62cf 315 return 1;
33cbe6cd 316 }
a34f62cf
JB
317
318 while (argc) {
319 const char *name;
320 char *value;
321 _any any;
322
323 memset(&any, 0, sizeof(_any));
324
325 name = argv[0];
326 value = strchr(name, '=');
327 if (value) {
328 *value = '\0';
329 value++;
330 argc--;
331 argv++;
332 } else {
333 /* backward compat -- accept w/o '=' */
334 if (argc < 2) {
335 printf("Must specify a value for %s.\n", name);
336 return 2;
337 }
338 value = argv[1];
339 argc -= 2;
340 argv += 2;
341 }
342
343 mdescr = find_mesh_param(name);
33cbe6cd
OO
344 if (!mdescr) {
345 printf("Could not find the parameter %s.\n", name);
346 print_all_mesh_param_descr();
a34f62cf 347 return 2;
33cbe6cd 348 }
a34f62cf
JB
349
350 /* Parse the new value */
351 ret = mdescr->parse_fn(value, &any);
352 if (ret != 0) {
46758fa3
MP
353 if (mdescr->mesh_param_num
354 == NL80211_MESHCONF_POWER_MODE)
355 printf("%s must be set to active, light or "
356 "deep.\n", mdescr->name);
357 else
358 printf("%s must be set to a number "
359 "between 0 and %u\n",
360 mdescr->name, ret);
361
a34f62cf
JB
362 return 2;
363 }
364
365 err = mdescr->nla_put_fn(msg, mdescr->mesh_param_num, &any);
366 if (err)
367 return err;
368 }
46c1ad1f 369 nla_nest_end(msg, container);
370
371 return err;
372}
373
a34f62cf 374COMMAND(set, mesh_param, "<param>=<value> [<param>=<value>]*",
70cf4544
JB
375 NL80211_CMD_SET_MESH_PARAMS, 0, CIB_NETDEV, set_interface_meshparam,
376 "Set mesh parameter (run command without any to see available ones).");
46c1ad1f 377
378/* Getter */
379static int print_mesh_param_handler(struct nl_msg *msg, void *arg)
380{
381 const struct mesh_param_descr *mdescr = arg;
382 struct nlattr *attrs[NL80211_ATTR_MAX + 1];
383 struct nlattr *parent_attr;
384 struct nlattr *mesh_params[NL80211_MESHCONF_ATTR_MAX + 1];
385 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
386
387 /* locate NL80211_ATTR_MESH_PARAMS */
388 nla_parse(attrs, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
389 genlmsg_attrlen(gnlh, 0), NULL);
390 parent_attr = attrs[NL80211_ATTR_MESH_PARAMS];
391 if (!parent_attr)
392 return -EINVAL;
393
394 /* unpack the mesh parameters */
395 if (nla_parse_nested(mesh_params, NL80211_MESHCONF_ATTR_MAX,
97e21372 396 parent_attr, NULL))
46c1ad1f 397 return -EINVAL;
398
97e21372 399 if (!mdescr) {
0ee571d5 400 unsigned int i;
97e21372
JB
401
402 for (i = 0; i < ARRAY_SIZE(_mesh_param_descrs); i++) {
403 mdescr = &_mesh_param_descrs[i];
404 printf("%s = ", mdescr->name);
405 mdescr->nla_print_fn(mesh_params[mdescr->mesh_param_num]);
406 printf("\n");
407 }
408 return NL_SKIP;
409 }
410
46c1ad1f 411 /* print out the mesh parameter */
412 mdescr->nla_print_fn(mesh_params[mdescr->mesh_param_num]);
413 printf("\n");
414 return NL_SKIP;
415}
416
7c37a24d 417static int get_interface_meshparam(struct nl80211_state *state,
7c37a24d 418 struct nl_msg *msg,
05514f95
JB
419 int argc, char **argv,
420 enum id_input id)
46c1ad1f 421{
97e21372 422 const struct mesh_param_descr *mdescr = NULL;
46c1ad1f 423
33cbe6cd
OO
424 if (argc == 0) {
425 print_all_mesh_param_descr();
97e21372 426 return 1;
33cbe6cd 427 } else if (argc == 1) {
a34f62cf 428 mdescr = find_mesh_param(argv[0]);
33cbe6cd
OO
429 if (!mdescr) {
430 printf("Could not find the parameter %s.\n", argv[0]);
431 print_all_mesh_param_descr();
97e21372 432 return 2;
33cbe6cd
OO
433 }
434 } else {
435 return 1;
97e21372 436 }
46c1ad1f 437
34b23014 438 register_handler(print_mesh_param_handler, (void *)mdescr);
46c1ad1f 439 return 0;
440}
441
97e21372 442COMMAND(get, mesh_param, "[<param>]",
70cf4544
JB
443 NL80211_CMD_GET_MESH_PARAMS, 0, CIB_NETDEV, get_interface_meshparam,
444 "Retrieve mesh parameter (run command without any to see available ones).");
400efd3e 445
34b23014 446static int join_mesh(struct nl80211_state *state,
05514f95
JB
447 struct nl_msg *msg, int argc, char **argv,
448 enum id_input id)
400efd3e 449{
9560e43d 450 struct nlattr *container;
7d01a091 451 float rate;
c8473cb1 452 unsigned char rates[NL80211_MAX_SUPP_RATES];
0ee571d5 453 int bintval, dtim_period, n_rates = 0;
c8473cb1 454 char *end, *value = NULL, *sptr = NULL;
7d01a091 455
400efd3e
JB
456 if (argc < 1)
457 return 1;
458
459 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(argv[0]), argv[0]);
460 argc--;
461 argv++;
462
957baf81
CYY
463 /* freq */
464 if (argc > 1 && strcmp(argv[0], "freq") == 0) {
3e1debef
BB
465 struct chandef chandef;
466 int err, parsed;
957baf81 467
3e1debef
BB
468 err = parse_freqchan(&chandef, false, argc - 1, argv + 1,
469 &parsed);
470 if (err)
471 return err;
957baf81 472
3e1debef
BB
473 argv += parsed + 1;
474 argc -= parsed + 1;
957baf81 475
bcdceae1 476 err = put_chandef(msg, &chandef);
3e1debef
BB
477 if (err)
478 return err;
957baf81
CYY
479 }
480
c8473cb1
CYY
481 /* basic rates */
482 if (argc > 1 && strcmp(argv[0], "basic-rates") == 0) {
483 argv++;
484 argc--;
485
486 value = strtok_r(argv[0], ",", &sptr);
487
488 while (value && n_rates < NL80211_MAX_SUPP_RATES) {
489 rate = strtod(value, &end);
490 rates[n_rates] = rate * 2;
491
492 /* filter out suspicious values */
493 if (*end != '\0' || !rates[n_rates] ||
494 rate*2 != rates[n_rates])
495 return 1;
496
497 n_rates++;
498 value = strtok_r(NULL, ",", &sptr);
499 }
500
501 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, n_rates, rates);
502 argv++;
503 argc--;
504 }
505
506 /* multicast rate */
7d01a091
CYY
507 if (argc > 1 && strcmp(argv[0], "mcast-rate") == 0) {
508 argv++;
509 argc--;
510
511 rate = strtod(argv[0], &end);
512 if (*end != '\0')
513 return 1;
514
515 NLA_PUT_U32(msg, NL80211_ATTR_MCAST_RATE, (int)(rate * 10));
516 argv++;
517 argc--;
518 }
519
a8c2e76d
MP
520 if (argc > 1 && strcmp(argv[0], "beacon-interval") == 0) {
521 argc--;
522 argv++;
523
524 bintval = strtoul(argv[0], &end, 10);
525 if (*end != '\0')
526 return 1;
527
528 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, bintval);
529 argv++;
530 argc--;
531 }
532
533 if (argc > 1 && strcmp(argv[0], "dtim-period") == 0) {
534 argc--;
535 argv++;
536
537 dtim_period = strtoul(argv[0], &end, 10);
538 if (*end != '\0')
539 return 1;
540
541 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
542 argv++;
543 argc--;
544 }
545
9560e43d
AN
546 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
547 if (!container)
548 return -ENOBUFS;
549
550 if (argc > 1 && strcmp(argv[0], "vendor_sync") == 0) {
551 argv++;
552 argc--;
553 if (strcmp(argv[0], "on") == 0)
554 NLA_PUT_U8(msg,
555 NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC, 1);
556 else
557 NLA_PUT_U8(msg,
558 NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC, 0);
559 argv++;
560 argc--;
561 }
562 /* parse and put other NL80211_ATTR_MESH_SETUP elements here */
563
564 nla_nest_end(msg, container);
565
400efd3e
JB
566 if (!argc)
567 return 0;
34b23014 568 return set_interface_meshparam(state, msg, argc, argv, id);
400efd3e
JB
569 nla_put_failure:
570 return -ENOBUFS;
571}
8366d195 572COMMAND(mesh, join, "<mesh ID> [[freq <freq in MHz> <NOHT|HT20|HT40+|HT40-|80MHz>]"
c8473cb1 573 " [basic-rates <rate in Mbps,rate2,...>]], [mcast-rate <rate in Mbps>]"
a8c2e76d
MP
574 " [beacon-interval <time in TUs>] [dtim-period <value>]"
575 " [vendor_sync on|off] [<param>=<value>]*",
400efd3e 576 NL80211_CMD_JOIN_MESH, 0, CIB_NETDEV, join_mesh,
c8473cb1
CYY
577 "Join a mesh with the given mesh ID with frequency, basic-rates,\n"
578 "mcast-rate and mesh parameters. Basic-rates are applied only if\n"
579 "frequency is provided.");
400efd3e 580
34b23014 581static int leave_mesh(struct nl80211_state *state,
05514f95
JB
582 struct nl_msg *msg, int argc, char **argv,
583 enum id_input id)
400efd3e
JB
584{
585 if (argc)
586 return 1;
587
588 return 0;
589}
590COMMAND(mesh, leave, NULL, NL80211_CMD_LEAVE_MESH, 0, CIB_NETDEV, leave_mesh,
591 "Leave a mesh.");