]> git.ipfire.org Git - thirdparty/iw.git/blob - mesh.c
iw: prepare get_cf1 for use in join_* functions
[thirdparty/iw.git] / mesh.c
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
13 SECTION(mesh);
14
15
16 typedef struct _any_t {
17 union {
18 uint32_t as_32;
19 int32_t as_s32;
20 uint16_t as_16;
21 uint8_t as_8;
22 } u;
23 } _any;
24
25 /* describes a mesh parameter */
26 struct 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. */
36 static 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
41 static 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
46 static 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
51 static 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
63 static 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
75 static 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
87 static 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
99 static 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
111 static 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 }
128
129 static void _print_u8(struct nlattr *a)
130 {
131 printf("%d", nla_get_u8(a));
132 }
133
134 static void _print_u16(struct nlattr *a)
135 {
136 printf("%d", nla_get_u16(a));
137 }
138
139 static void _print_u16_timeout(struct nlattr *a)
140 {
141 printf("%d milliseconds", nla_get_u16(a));
142 }
143
144 static void _print_u16_in_TUs(struct nlattr *a)
145 {
146 printf("%d TUs", nla_get_u16(a));
147 }
148
149 static void _print_u32(struct nlattr *a)
150 {
151 printf("%d", nla_get_u32(a));
152 }
153
154 static void _print_u32_timeout(struct nlattr *a)
155 {
156 printf("%u milliseconds", nla_get_u32(a));
157 }
158
159 static void _print_u32_in_seconds(struct nlattr *a)
160 {
161 printf("%d seconds", nla_get_u32(a));
162 }
163
164 static void _print_u32_in_TUs(struct nlattr *a)
165 {
166 printf("%d TUs", nla_get_u32(a));
167 }
168
169 static 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
189 static void _print_s32_in_dBm(struct nlattr *a)
190 {
191 printf("%d dBm", (int32_t) nla_get_u32(a));
192 }
193
194
195 /* The current mesh parameters */
196 const static struct mesh_param_descr _mesh_param_descrs[] =
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},
216 {"mesh_element_ttl",
217 NL80211_MESHCONF_ELEMENT_TTL,
218 _my_nla_put_u8, _parse_u8, _print_u8},
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},
240 {"mesh_hwmp_rootmode", NL80211_MESHCONF_HWMP_ROOTMODE,
241 _my_nla_put_u8, _parse_u8, _print_u8},
242 {"mesh_hwmp_rann_interval", NL80211_MESHCONF_HWMP_RANN_INTERVAL,
243 _my_nla_put_u16, _parse_u16, _print_u16_in_TUs},
244 {"mesh_gate_announcements", NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
245 _my_nla_put_u8, _parse_u8, _print_u8},
246 {"mesh_fwding", NL80211_MESHCONF_FORWARDING,
247 _my_nla_put_u8, _parse_u8_as_bool, _print_u8},
248 {"mesh_sync_offset_max_neighor",
249 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
250 _my_nla_put_u32, _parse_u32, _print_u32},
251 {"mesh_rssi_threshold", NL80211_MESHCONF_RSSI_THRESHOLD,
252 _my_nla_put_u32, _parse_s32, _print_s32_in_dBm},
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},
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},
265 {"mesh_plink_timeout", NL80211_MESHCONF_PLINK_TIMEOUT,
266 _my_nla_put_u32, _parse_u32, _print_u32_in_seconds},
267 };
268
269 static void print_all_mesh_param_descr(void)
270 {
271 unsigned int i;
272
273 printf("Possible mesh parameters are:\n");
274
275 for (i = 0; i < ARRAY_SIZE(_mesh_param_descrs); i++)
276 printf(" - %s\n", _mesh_param_descrs[i].name);
277 }
278
279 static const struct mesh_param_descr *find_mesh_param(const char *name)
280 {
281 unsigned int i;
282
283 /* Find out what mesh parameter we want to change. */
284 for (i = 0; i < ARRAY_SIZE(_mesh_param_descrs); i++) {
285 if (strcmp(_mesh_param_descrs[i].name, name) == 0)
286 return _mesh_param_descrs + i;
287 }
288
289 return NULL;
290 }
291
292 /* Setter */
293 static int set_interface_meshparam(struct nl80211_state *state,
294 struct nl_msg *msg,
295 int argc, char **argv,
296 enum id_input id)
297 {
298 const struct mesh_param_descr *mdescr;
299 struct nlattr *container;
300 uint32_t ret;
301 int err;
302
303 container = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
304 if (!container)
305 return -ENOBUFS;
306
307 if (!argc) {
308 print_all_mesh_param_descr();
309 return 1;
310 }
311
312 while (argc) {
313 const char *name;
314 char *value;
315 _any any;
316
317 memset(&any, 0, sizeof(_any));
318
319 name = argv[0];
320 value = strchr(name, '=');
321 if (value) {
322 *value = '\0';
323 value++;
324 argc--;
325 argv++;
326 } else {
327 /* backward compat -- accept w/o '=' */
328 if (argc < 2) {
329 printf("Must specify a value for %s.\n", name);
330 return 2;
331 }
332 value = argv[1];
333 argc -= 2;
334 argv += 2;
335 }
336
337 mdescr = find_mesh_param(name);
338 if (!mdescr) {
339 printf("Could not find the parameter %s.\n", name);
340 print_all_mesh_param_descr();
341 return 2;
342 }
343
344 /* Parse the new value */
345 ret = mdescr->parse_fn(value, &any);
346 if (ret != 0) {
347 if (mdescr->mesh_param_num
348 == NL80211_MESHCONF_POWER_MODE)
349 printf("%s must be set to active, light or "
350 "deep.\n", mdescr->name);
351 else
352 printf("%s must be set to a number "
353 "between 0 and %u\n",
354 mdescr->name, ret);
355
356 return 2;
357 }
358
359 err = mdescr->nla_put_fn(msg, mdescr->mesh_param_num, &any);
360 if (err)
361 return err;
362 }
363 nla_nest_end(msg, container);
364
365 return err;
366 }
367
368 COMMAND(set, mesh_param, "<param>=<value> [<param>=<value>]*",
369 NL80211_CMD_SET_MESH_PARAMS, 0, CIB_NETDEV, set_interface_meshparam,
370 "Set mesh parameter (run command without any to see available ones).");
371
372 /* Getter */
373 static int print_mesh_param_handler(struct nl_msg *msg, void *arg)
374 {
375 const struct mesh_param_descr *mdescr = arg;
376 struct nlattr *attrs[NL80211_ATTR_MAX + 1];
377 struct nlattr *parent_attr;
378 struct nlattr *mesh_params[NL80211_MESHCONF_ATTR_MAX + 1];
379 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
380
381 /* locate NL80211_ATTR_MESH_PARAMS */
382 nla_parse(attrs, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
383 genlmsg_attrlen(gnlh, 0), NULL);
384 parent_attr = attrs[NL80211_ATTR_MESH_PARAMS];
385 if (!parent_attr)
386 return -EINVAL;
387
388 /* unpack the mesh parameters */
389 if (nla_parse_nested(mesh_params, NL80211_MESHCONF_ATTR_MAX,
390 parent_attr, NULL))
391 return -EINVAL;
392
393 if (!mdescr) {
394 unsigned int i;
395
396 for (i = 0; i < ARRAY_SIZE(_mesh_param_descrs); i++) {
397 mdescr = &_mesh_param_descrs[i];
398 printf("%s = ", mdescr->name);
399 mdescr->nla_print_fn(mesh_params[mdescr->mesh_param_num]);
400 printf("\n");
401 }
402 return NL_SKIP;
403 }
404
405 /* print out the mesh parameter */
406 mdescr->nla_print_fn(mesh_params[mdescr->mesh_param_num]);
407 printf("\n");
408 return NL_SKIP;
409 }
410
411 static int get_interface_meshparam(struct nl80211_state *state,
412 struct nl_msg *msg,
413 int argc, char **argv,
414 enum id_input id)
415 {
416 const struct mesh_param_descr *mdescr = NULL;
417
418 if (argc == 0) {
419 print_all_mesh_param_descr();
420 return 1;
421 } else if (argc == 1) {
422 mdescr = find_mesh_param(argv[0]);
423 if (!mdescr) {
424 printf("Could not find the parameter %s.\n", argv[0]);
425 print_all_mesh_param_descr();
426 return 2;
427 }
428 } else {
429 return 1;
430 }
431
432 register_handler(print_mesh_param_handler, (void *)mdescr);
433 return 0;
434 }
435
436 COMMAND(get, mesh_param, "[<param>]",
437 NL80211_CMD_GET_MESH_PARAMS, 0, CIB_NETDEV, get_interface_meshparam,
438 "Retrieve mesh parameter (run command without any to see available ones).");
439
440 static int join_mesh(struct nl80211_state *state,
441 struct nl_msg *msg, int argc, char **argv,
442 enum id_input id)
443 {
444 struct nlattr *container;
445 float rate;
446 unsigned char rates[NL80211_MAX_SUPP_RATES];
447 int bintval, dtim_period, n_rates = 0;
448 char *end, *value = NULL, *sptr = NULL;
449 unsigned int i;
450 unsigned long freq = 0;
451 const struct chanmode *chanmode_selected = NULL;
452 static const struct chanmode chanmode[] = {
453 { .name = "HT20",
454 .width = NL80211_CHAN_WIDTH_20,
455 .freq1_diff = 0,
456 .chantype = NL80211_CHAN_HT20 },
457 { .name = "HT40+",
458 .width = NL80211_CHAN_WIDTH_40,
459 .freq1_diff = 10,
460 .chantype = NL80211_CHAN_HT40PLUS },
461 { .name = "HT40-",
462 .width = NL80211_CHAN_WIDTH_40,
463 .freq1_diff = -10,
464 .chantype = NL80211_CHAN_HT40MINUS },
465 { .name = "NOHT",
466 .width = NL80211_CHAN_WIDTH_20_NOHT,
467 .freq1_diff = 0,
468 .chantype = NL80211_CHAN_NO_HT },
469 };
470
471 if (argc < 1)
472 return 1;
473
474 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(argv[0]), argv[0]);
475 argc--;
476 argv++;
477
478 /* freq */
479 if (argc > 1 && strcmp(argv[0], "freq") == 0) {
480 argv++;
481 argc--;
482
483 freq = strtoul(argv[0], &end, 10);
484 if (*end != '\0')
485 return 1;
486 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
487
488 argv++;
489 argc--;
490 }
491
492 /* channel type */
493 if (argc) {
494 for (i = 0; i < ARRAY_SIZE(chanmode); i++) {
495 if (strcasecmp(chanmode[i].name, argv[0]) == 0) {
496 chanmode_selected = &chanmode[i];
497 break;
498 }
499 }
500
501 if (chanmode_selected) {
502 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
503 chanmode_selected->width);
504 NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1,
505 get_cf1(chanmode_selected, freq));
506 if (chanmode_selected->chantype != -1)
507 NLA_PUT_U32(msg,
508 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
509 chanmode_selected->chantype);
510
511 argv++;
512 argc--;
513 }
514 }
515
516 /* basic rates */
517 if (argc > 1 && strcmp(argv[0], "basic-rates") == 0) {
518 argv++;
519 argc--;
520
521 value = strtok_r(argv[0], ",", &sptr);
522
523 while (value && n_rates < NL80211_MAX_SUPP_RATES) {
524 rate = strtod(value, &end);
525 rates[n_rates] = rate * 2;
526
527 /* filter out suspicious values */
528 if (*end != '\0' || !rates[n_rates] ||
529 rate*2 != rates[n_rates])
530 return 1;
531
532 n_rates++;
533 value = strtok_r(NULL, ",", &sptr);
534 }
535
536 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, n_rates, rates);
537 argv++;
538 argc--;
539 }
540
541 /* multicast rate */
542 if (argc > 1 && strcmp(argv[0], "mcast-rate") == 0) {
543 argv++;
544 argc--;
545
546 rate = strtod(argv[0], &end);
547 if (*end != '\0')
548 return 1;
549
550 NLA_PUT_U32(msg, NL80211_ATTR_MCAST_RATE, (int)(rate * 10));
551 argv++;
552 argc--;
553 }
554
555 if (argc > 1 && strcmp(argv[0], "beacon-interval") == 0) {
556 argc--;
557 argv++;
558
559 bintval = strtoul(argv[0], &end, 10);
560 if (*end != '\0')
561 return 1;
562
563 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, bintval);
564 argv++;
565 argc--;
566 }
567
568 if (argc > 1 && strcmp(argv[0], "dtim-period") == 0) {
569 argc--;
570 argv++;
571
572 dtim_period = strtoul(argv[0], &end, 10);
573 if (*end != '\0')
574 return 1;
575
576 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
577 argv++;
578 argc--;
579 }
580
581 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
582 if (!container)
583 return -ENOBUFS;
584
585 if (argc > 1 && strcmp(argv[0], "vendor_sync") == 0) {
586 argv++;
587 argc--;
588 if (strcmp(argv[0], "on") == 0)
589 NLA_PUT_U8(msg,
590 NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC, 1);
591 else
592 NLA_PUT_U8(msg,
593 NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC, 0);
594 argv++;
595 argc--;
596 }
597 /* parse and put other NL80211_ATTR_MESH_SETUP elements here */
598
599 nla_nest_end(msg, container);
600
601 if (!argc)
602 return 0;
603 return set_interface_meshparam(state, msg, argc, argv, id);
604 nla_put_failure:
605 return -ENOBUFS;
606 }
607 COMMAND(mesh, join, "<mesh ID> [[freq <freq in MHz> <HT20|HT40+|HT40-|NOHT>]"
608 " [basic-rates <rate in Mbps,rate2,...>]], [mcast-rate <rate in Mbps>]"
609 " [beacon-interval <time in TUs>] [dtim-period <value>]"
610 " [vendor_sync on|off] [<param>=<value>]*",
611 NL80211_CMD_JOIN_MESH, 0, CIB_NETDEV, join_mesh,
612 "Join a mesh with the given mesh ID with frequency, basic-rates,\n"
613 "mcast-rate and mesh parameters. Basic-rates are applied only if\n"
614 "frequency is provided.");
615
616 static int leave_mesh(struct nl80211_state *state,
617 struct nl_msg *msg, int argc, char **argv,
618 enum id_input id)
619 {
620 if (argc)
621 return 1;
622
623 return 0;
624 }
625 COMMAND(mesh, leave, NULL, NL80211_CMD_LEAVE_MESH, 0, CIB_NETDEV, leave_mesh,
626 "Leave a mesh.");