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>
17 typedef struct _any_t
{
26 /* describes a mesh parameter */
27 struct mesh_param_descr
{
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
*);
35 /* utility functions for manipulating and printing u8/u16/u32 values and
37 static int _my_nla_put_u8(struct nl_msg
*n
, int mesh_param_num
, _any
*value
)
39 return nla_put(n
, mesh_param_num
, sizeof(uint8_t), &value
->u
.as_8
);
42 static int _my_nla_put_u16(struct nl_msg
*n
, int mesh_param_num
, _any
*value
)
44 return nla_put(n
, mesh_param_num
, sizeof(uint16_t), &value
->u
.as_16
);
47 static int _my_nla_put_u32(struct nl_msg
*n
, int mesh_param_num
, _any
*value
)
49 return nla_put(n
, mesh_param_num
, sizeof(uint32_t), &value
->u
.as_32
);
52 static uint32_t _parse_u8(const char *str
, _any
*ret
)
55 unsigned long int v
= strtoul(str
, &endptr
, 10);
60 ret
->u
.as_8
= (uint8_t)v
;
64 static uint32_t _parse_u8_as_bool(const char *str
, _any
*ret
)
67 unsigned long int v
= strtoul(str
, &endptr
, 10);
72 ret
->u
.as_8
= (uint8_t)v
;
76 static uint32_t _parse_u16(const char *str
, _any
*ret
)
79 long int v
= strtol(str
, &endptr
, 10);
82 if ((v
< 0) || (v
> 0xffff))
84 ret
->u
.as_16
= (uint16_t)v
;
88 static uint32_t _parse_u32(const char *str
, _any
*ret
)
91 long long int v
= strtoll(str
, &endptr
, 10);
94 if ((v
< 0) || (v
> 0xffffffff))
96 ret
->u
.as_32
= (uint32_t)v
;
100 static uint32_t _parse_s32(const char *str
, _any
*ret
)
103 long int v
= strtol(str
, &endptr
, 10);
108 ret
->u
.as_s32
= (int32_t)v
;
112 static uint32_t _parse_u32_power_mode(const char *str
, _any
*ret
)
116 /* Parse attribute for the name of power mode */
117 if (!strcmp(str
, "active"))
118 v
= NL80211_MESH_POWER_ACTIVE
;
119 else if (!strcmp(str
, "light"))
120 v
= NL80211_MESH_POWER_LIGHT_SLEEP
;
121 else if (!strcmp(str
, "deep"))
122 v
= NL80211_MESH_POWER_DEEP_SLEEP
;
126 ret
->u
.as_32
= (uint32_t)v
;
130 static void _print_u8(struct nlattr
*a
)
132 printf("%d", nla_get_u8(a
));
135 static void _print_u16(struct nlattr
*a
)
137 printf("%d", nla_get_u16(a
));
140 static void _print_u16_timeout(struct nlattr
*a
)
142 printf("%d milliseconds", nla_get_u16(a
));
145 static void _print_u16_in_TUs(struct nlattr
*a
)
147 printf("%d TUs", nla_get_u16(a
));
150 static void _print_u32(struct nlattr
*a
)
152 printf("%d", nla_get_u32(a
));
155 static void _print_u32_timeout(struct nlattr
*a
)
157 printf("%u milliseconds", nla_get_u32(a
));
160 static void _print_u32_in_TUs(struct nlattr
*a
)
162 printf("%d TUs", nla_get_u32(a
));
165 static void _print_u32_power_mode(struct nlattr
*a
)
167 unsigned long v
= nla_get_u32(a
);
170 case NL80211_MESH_POWER_ACTIVE
:
173 case NL80211_MESH_POWER_LIGHT_SLEEP
:
176 case NL80211_MESH_POWER_DEEP_SLEEP
:
185 static void _print_s32_in_dBm(struct nlattr
*a
)
187 printf("%d dBm", (int32_t) nla_get_u32(a
));
191 /* The current mesh parameters */
192 const static struct mesh_param_descr _mesh_param_descrs
[] =
194 {"mesh_retry_timeout",
195 NL80211_MESHCONF_RETRY_TIMEOUT
,
196 _my_nla_put_u16
, _parse_u16
, _print_u16_timeout
},
197 {"mesh_confirm_timeout",
198 NL80211_MESHCONF_CONFIRM_TIMEOUT
,
199 _my_nla_put_u16
, _parse_u16
, _print_u16_timeout
},
200 {"mesh_holding_timeout",
201 NL80211_MESHCONF_HOLDING_TIMEOUT
,
202 _my_nla_put_u16
, _parse_u16
, _print_u16_timeout
},
203 {"mesh_max_peer_links",
204 NL80211_MESHCONF_MAX_PEER_LINKS
,
205 _my_nla_put_u16
, _parse_u16
, _print_u16
},
207 NL80211_MESHCONF_MAX_RETRIES
,
208 _my_nla_put_u8
, _parse_u8
, _print_u8
},
210 NL80211_MESHCONF_TTL
,
211 _my_nla_put_u8
, _parse_u8
, _print_u8
},
213 NL80211_MESHCONF_ELEMENT_TTL
,
214 _my_nla_put_u8
, _parse_u8
, _print_u8
},
215 {"mesh_auto_open_plinks",
216 NL80211_MESHCONF_AUTO_OPEN_PLINKS
,
217 _my_nla_put_u8
, _parse_u8_as_bool
, _print_u8
},
218 {"mesh_hwmp_max_preq_retries",
219 NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES
,
220 _my_nla_put_u8
, _parse_u8
, _print_u8
},
221 {"mesh_path_refresh_time",
222 NL80211_MESHCONF_PATH_REFRESH_TIME
,
223 _my_nla_put_u32
, _parse_u32
, _print_u32_timeout
},
224 {"mesh_min_discovery_timeout",
225 NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT
,
226 _my_nla_put_u16
, _parse_u16
, _print_u16_timeout
},
227 {"mesh_hwmp_active_path_timeout",
228 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT
,
229 _my_nla_put_u32
, _parse_u32
, _print_u32_in_TUs
},
230 {"mesh_hwmp_preq_min_interval",
231 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL
,
232 _my_nla_put_u16
, _parse_u16
, _print_u16_in_TUs
},
233 {"mesh_hwmp_net_diameter_traversal_time",
234 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME
,
235 _my_nla_put_u16
, _parse_u16
, _print_u16_in_TUs
},
236 {"mesh_hwmp_rootmode", NL80211_MESHCONF_HWMP_ROOTMODE
,
237 _my_nla_put_u8
, _parse_u8
, _print_u8
},
238 {"mesh_hwmp_rann_interval", NL80211_MESHCONF_HWMP_RANN_INTERVAL
,
239 _my_nla_put_u16
, _parse_u16
, _print_u16_in_TUs
},
240 {"mesh_gate_announcements", NL80211_MESHCONF_GATE_ANNOUNCEMENTS
,
241 _my_nla_put_u8
, _parse_u8
, _print_u8
},
242 {"mesh_fwding", NL80211_MESHCONF_FORWARDING
,
243 _my_nla_put_u8
, _parse_u8_as_bool
, _print_u8
},
244 {"mesh_sync_offset_max_neighor",
245 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR
,
246 _my_nla_put_u32
, _parse_u32
, _print_u32
},
247 {"mesh_rssi_threshold", NL80211_MESHCONF_RSSI_THRESHOLD
,
248 _my_nla_put_u32
, _parse_s32
, _print_s32_in_dBm
},
249 {"mesh_hwmp_active_path_to_root_timeout",
250 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT
,
251 _my_nla_put_u32
, _parse_u32
, _print_u32_in_TUs
},
252 {"mesh_hwmp_root_interval", NL80211_MESHCONF_HWMP_ROOT_INTERVAL
,
253 _my_nla_put_u16
, _parse_u16
, _print_u16_in_TUs
},
254 {"mesh_hwmp_confirmation_interval",
255 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL
,
256 _my_nla_put_u16
, _parse_u16
, _print_u16_in_TUs
},
257 {"mesh_power_mode", NL80211_MESHCONF_POWER_MODE
,
258 _my_nla_put_u32
, _parse_u32_power_mode
, _print_u32_power_mode
},
259 {"mesh_awake_window", NL80211_MESHCONF_AWAKE_WINDOW
,
260 _my_nla_put_u16
, _parse_u16
, _print_u16_in_TUs
},
263 static void print_all_mesh_param_descr(void)
267 printf("Possible mesh parameters are:\n");
269 for (i
= 0; i
< ARRAY_SIZE(_mesh_param_descrs
); i
++)
270 printf(" - %s\n", _mesh_param_descrs
[i
].name
);
273 static const struct mesh_param_descr
*find_mesh_param(const char *name
)
277 /* Find out what mesh parameter we want to change. */
278 for (i
= 0; i
< ARRAY_SIZE(_mesh_param_descrs
); i
++) {
279 if (strcmp(_mesh_param_descrs
[i
].name
, name
) == 0)
280 return _mesh_param_descrs
+ i
;
283 print_all_mesh_param_descr();
288 static int set_interface_meshparam(struct nl80211_state
*state
,
291 int argc
, char **argv
,
294 const struct mesh_param_descr
*mdescr
;
295 struct nlattr
*container
;
299 container
= nla_nest_start(msg
, NL80211_ATTR_MESH_PARAMS
);
311 memset(&any
, 0, sizeof(_any
));
314 value
= strchr(name
, '=');
321 /* backward compat -- accept w/o '=' */
323 printf("Must specify a value for %s.\n", name
);
331 mdescr
= find_mesh_param(name
);
335 /* Parse the new value */
336 ret
= mdescr
->parse_fn(value
, &any
);
338 if (mdescr
->mesh_param_num
339 == NL80211_MESHCONF_POWER_MODE
)
340 printf("%s must be set to active, light or "
341 "deep.\n", mdescr
->name
);
343 printf("%s must be set to a number "
344 "between 0 and %u\n",
350 err
= mdescr
->nla_put_fn(msg
, mdescr
->mesh_param_num
, &any
);
354 nla_nest_end(msg
, container
);
359 COMMAND(set
, mesh_param
, "<param>=<value> [<param>=<value>]*",
360 NL80211_CMD_SET_MESH_PARAMS
, 0, CIB_NETDEV
, set_interface_meshparam
,
361 "Set mesh parameter (run command without any to see available ones).");
364 static int print_mesh_param_handler(struct nl_msg
*msg
, void *arg
)
366 const struct mesh_param_descr
*mdescr
= arg
;
367 struct nlattr
*attrs
[NL80211_ATTR_MAX
+ 1];
368 struct nlattr
*parent_attr
;
369 struct nlattr
*mesh_params
[NL80211_MESHCONF_ATTR_MAX
+ 1];
370 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
372 /* locate NL80211_ATTR_MESH_PARAMS */
373 nla_parse(attrs
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
374 genlmsg_attrlen(gnlh
, 0), NULL
);
375 parent_attr
= attrs
[NL80211_ATTR_MESH_PARAMS
];
379 /* unpack the mesh parameters */
380 if (nla_parse_nested(mesh_params
, NL80211_MESHCONF_ATTR_MAX
,
387 for (i
= 0; i
< ARRAY_SIZE(_mesh_param_descrs
); i
++) {
388 mdescr
= &_mesh_param_descrs
[i
];
389 printf("%s = ", mdescr
->name
);
390 mdescr
->nla_print_fn(mesh_params
[mdescr
->mesh_param_num
]);
396 /* print out the mesh parameter */
397 mdescr
->nla_print_fn(mesh_params
[mdescr
->mesh_param_num
]);
402 static int get_interface_meshparam(struct nl80211_state
*state
,
405 int argc
, char **argv
,
408 const struct mesh_param_descr
*mdescr
= NULL
;
414 mdescr
= find_mesh_param(argv
[0]);
419 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
,
420 print_mesh_param_handler
, (void *)mdescr
);
424 COMMAND(get
, mesh_param
, "[<param>]",
425 NL80211_CMD_GET_MESH_PARAMS
, 0, CIB_NETDEV
, get_interface_meshparam
,
426 "Retrieve mesh parameter (run command without any to see available ones).");
428 static int join_mesh(struct nl80211_state
*state
, struct nl_cb
*cb
,
429 struct nl_msg
*msg
, int argc
, char **argv
,
432 struct nlattr
*container
;
434 int bintval
, dtim_period
;
440 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(argv
[0]), argv
[0]);
444 if (argc
> 1 && strcmp(argv
[0], "mcast-rate") == 0) {
448 rate
= strtod(argv
[0], &end
);
452 NLA_PUT_U32(msg
, NL80211_ATTR_MCAST_RATE
, (int)(rate
* 10));
457 if (argc
> 1 && strcmp(argv
[0], "beacon-interval") == 0) {
461 bintval
= strtoul(argv
[0], &end
, 10);
465 NLA_PUT_U32(msg
, NL80211_ATTR_BEACON_INTERVAL
, bintval
);
470 if (argc
> 1 && strcmp(argv
[0], "dtim-period") == 0) {
474 dtim_period
= strtoul(argv
[0], &end
, 10);
478 NLA_PUT_U32(msg
, NL80211_ATTR_DTIM_PERIOD
, dtim_period
);
483 container
= nla_nest_start(msg
, NL80211_ATTR_MESH_SETUP
);
487 if (argc
> 1 && strcmp(argv
[0], "vendor_sync") == 0) {
490 if (strcmp(argv
[0], "on") == 0)
492 NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC
, 1);
495 NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC
, 0);
499 /* parse and put other NL80211_ATTR_MESH_SETUP elements here */
501 nla_nest_end(msg
, container
);
505 return set_interface_meshparam(state
, cb
, msg
, argc
, argv
, id
);
509 COMMAND(mesh
, join
, "<mesh ID> [mcast-rate <rate in Mbps>]"
510 " [beacon-interval <time in TUs>] [dtim-period <value>]"
511 " [vendor_sync on|off] [<param>=<value>]*",
512 NL80211_CMD_JOIN_MESH
, 0, CIB_NETDEV
, join_mesh
,
513 "Join a mesh with the given mesh ID with mcast-rate and mesh parameters.");
515 static int leave_mesh(struct nl80211_state
*state
, struct nl_cb
*cb
,
516 struct nl_msg
*msg
, int argc
, char **argv
,
524 COMMAND(mesh
, leave
, NULL
, NL80211_CMD_LEAVE_MESH
, 0, CIB_NETDEV
, leave_mesh
,