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