]> git.ipfire.org Git - thirdparty/iw.git/blob - mesh.c
add P2P Device handling primitives
[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_in_TUs},
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 {"mesh_hwmp_active_path_to_root_timeout",
213 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
214 _my_nla_put_u32, _parse_u32, _print_u32_in_TUs},
215 {"mesh_hwmp_root_interval", NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
216 _my_nla_put_u16, _parse_u16, _print_u16_in_TUs},
217 {"mesh_hwmp_confirmation_interval",
218 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
219 _my_nla_put_u16, _parse_u16, _print_u16_in_TUs},
220 };
221
222 static void print_all_mesh_param_descr(void)
223 {
224 int i;
225
226 printf("Possible mesh parameters are:\n");
227
228 for (i = 0; i < ARRAY_SIZE(_mesh_param_descrs); i++)
229 printf(" - %s\n", _mesh_param_descrs[i].name);
230 }
231
232 static const struct mesh_param_descr *find_mesh_param(const char *name)
233 {
234 int i;
235
236 /* Find out what mesh parameter we want to change. */
237 for (i = 0; i < ARRAY_SIZE(_mesh_param_descrs); i++) {
238 if (strcmp(_mesh_param_descrs[i].name, name) == 0)
239 return _mesh_param_descrs + i;
240 }
241
242 print_all_mesh_param_descr();
243 return NULL;
244 }
245
246 /* Setter */
247 static int set_interface_meshparam(struct nl80211_state *state,
248 struct nl_cb *cb,
249 struct nl_msg *msg,
250 int argc, char **argv,
251 enum id_input id)
252 {
253 const struct mesh_param_descr *mdescr;
254 struct nlattr *container;
255 uint32_t ret;
256 int err;
257
258 container = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
259 if (!container)
260 return -ENOBUFS;
261
262 if (!argc)
263 return 1;
264
265 while (argc) {
266 const char *name;
267 char *value;
268 _any any;
269
270 memset(&any, 0, sizeof(_any));
271
272 name = argv[0];
273 value = strchr(name, '=');
274 if (value) {
275 *value = '\0';
276 value++;
277 argc--;
278 argv++;
279 } else {
280 /* backward compat -- accept w/o '=' */
281 if (argc < 2) {
282 printf("Must specify a value for %s.\n", name);
283 return 2;
284 }
285 value = argv[1];
286 argc -= 2;
287 argv += 2;
288 }
289
290 mdescr = find_mesh_param(name);
291 if (!mdescr)
292 return 2;
293
294 /* Parse the new value */
295 ret = mdescr->parse_fn(value, &any);
296 if (ret != 0) {
297 printf("%s must be set to a number "
298 "between 0 and %u\n", mdescr->name, ret);
299 return 2;
300 }
301
302 err = mdescr->nla_put_fn(msg, mdescr->mesh_param_num, &any);
303 if (err)
304 return err;
305 }
306 nla_nest_end(msg, container);
307
308 return err;
309 }
310
311 COMMAND(set, mesh_param, "<param>=<value> [<param>=<value>]*",
312 NL80211_CMD_SET_MESH_PARAMS, 0, CIB_NETDEV, set_interface_meshparam,
313 "Set mesh parameter (run command without any to see available ones).");
314
315 /* Getter */
316 static int print_mesh_param_handler(struct nl_msg *msg, void *arg)
317 {
318 const struct mesh_param_descr *mdescr = arg;
319 struct nlattr *attrs[NL80211_ATTR_MAX + 1];
320 struct nlattr *parent_attr;
321 struct nlattr *mesh_params[NL80211_MESHCONF_ATTR_MAX + 1];
322 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
323
324 /* locate NL80211_ATTR_MESH_PARAMS */
325 nla_parse(attrs, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
326 genlmsg_attrlen(gnlh, 0), NULL);
327 parent_attr = attrs[NL80211_ATTR_MESH_PARAMS];
328 if (!parent_attr)
329 return -EINVAL;
330
331 /* unpack the mesh parameters */
332 if (nla_parse_nested(mesh_params, NL80211_MESHCONF_ATTR_MAX,
333 parent_attr, NULL))
334 return -EINVAL;
335
336 if (!mdescr) {
337 int i;
338
339 for (i = 0; i < ARRAY_SIZE(_mesh_param_descrs); i++) {
340 mdescr = &_mesh_param_descrs[i];
341 printf("%s = ", mdescr->name);
342 mdescr->nla_print_fn(mesh_params[mdescr->mesh_param_num]);
343 printf("\n");
344 }
345 return NL_SKIP;
346 }
347
348 /* print out the mesh parameter */
349 mdescr->nla_print_fn(mesh_params[mdescr->mesh_param_num]);
350 printf("\n");
351 return NL_SKIP;
352 }
353
354 static int get_interface_meshparam(struct nl80211_state *state,
355 struct nl_cb *cb,
356 struct nl_msg *msg,
357 int argc, char **argv,
358 enum id_input id)
359 {
360 const struct mesh_param_descr *mdescr = NULL;
361
362 if (argc > 1)
363 return 1;
364
365 if (argc == 1) {
366 mdescr = find_mesh_param(argv[0]);
367 if (!mdescr)
368 return 2;
369 }
370
371 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
372 print_mesh_param_handler, (void *)mdescr);
373 return 0;
374 }
375
376 COMMAND(get, mesh_param, "[<param>]",
377 NL80211_CMD_GET_MESH_PARAMS, 0, CIB_NETDEV, get_interface_meshparam,
378 "Retrieve mesh parameter (run command without any to see available ones).");
379
380 static int join_mesh(struct nl80211_state *state, struct nl_cb *cb,
381 struct nl_msg *msg, int argc, char **argv,
382 enum id_input id)
383 {
384 struct nlattr *container;
385 float rate;
386 char *end;
387
388 if (argc < 1)
389 return 1;
390
391 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(argv[0]), argv[0]);
392 argc--;
393 argv++;
394
395 if (argc > 1 && strcmp(argv[0], "mcast-rate") == 0) {
396 argv++;
397 argc--;
398
399 rate = strtod(argv[0], &end);
400 if (*end != '\0')
401 return 1;
402
403 NLA_PUT_U32(msg, NL80211_ATTR_MCAST_RATE, (int)(rate * 10));
404 argv++;
405 argc--;
406 }
407
408 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
409 if (!container)
410 return -ENOBUFS;
411
412 if (argc > 1 && strcmp(argv[0], "vendor_sync") == 0) {
413 argv++;
414 argc--;
415 if (strcmp(argv[0], "on") == 0)
416 NLA_PUT_U8(msg,
417 NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC, 1);
418 else
419 NLA_PUT_U8(msg,
420 NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC, 0);
421 argv++;
422 argc--;
423 }
424 /* parse and put other NL80211_ATTR_MESH_SETUP elements here */
425
426 nla_nest_end(msg, container);
427
428 if (!argc)
429 return 0;
430 return set_interface_meshparam(state, cb, msg, argc, argv, id);
431 nla_put_failure:
432 return -ENOBUFS;
433 }
434 COMMAND(mesh, join, "<mesh ID> [mcast-rate <rate in Mbps>] [vendor_sync on|off]"
435 " [<param>=<value>]*",
436 NL80211_CMD_JOIN_MESH, 0, CIB_NETDEV, join_mesh,
437 "Join a mesh with the given mesh ID with mcast-rate and mesh parameters.");
438
439 static int leave_mesh(struct nl80211_state *state, struct nl_cb *cb,
440 struct nl_msg *msg, int argc, char **argv,
441 enum id_input id)
442 {
443 if (argc)
444 return 1;
445
446 return 0;
447 }
448 COMMAND(mesh, leave, NULL, NL80211_CMD_LEAVE_MESH, 0, CIB_NETDEV, leave_mesh,
449 "Leave a mesh.");