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
;
113 static void _print_u8(struct nlattr
*a
)
115 printf("%d", nla_get_u8(a
));
118 static void _print_u16(struct nlattr
*a
)
120 printf("%d", nla_get_u16(a
));
123 static void _print_u16_timeout(struct nlattr
*a
)
125 printf("%d milliseconds", nla_get_u16(a
));
128 static void _print_u16_in_TUs(struct nlattr
*a
)
130 printf("%d TUs", nla_get_u16(a
));
133 static void _print_u32(struct nlattr
*a
)
135 printf("%d", nla_get_u32(a
));
138 static void _print_u32_timeout(struct nlattr
*a
)
140 printf("%u milliseconds", nla_get_u32(a
));
143 static void _print_u32_in_TUs(struct nlattr
*a
)
145 printf("%d TUs", nla_get_u32(a
));
148 static void _print_s32_in_dBm(struct nlattr
*a
)
150 printf("%d dBm", (int32_t) nla_get_u32(a
));
154 /* The current mesh parameters */
155 const static struct mesh_param_descr _mesh_param_descrs
[] =
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
},
170 NL80211_MESHCONF_MAX_RETRIES
,
171 _my_nla_put_u8
, _parse_u8
, _print_u8
},
173 NL80211_MESHCONF_TTL
,
174 _my_nla_put_u8
, _parse_u8
, _print_u8
},
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
},
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
},
214 static void print_all_mesh_param_descr(void)
218 printf("Possible mesh parameters are:\n");
220 for (i
= 0; i
< ARRAY_SIZE(_mesh_param_descrs
); i
++)
221 printf(" - %s\n", _mesh_param_descrs
[i
].name
);
224 static const struct mesh_param_descr
*find_mesh_param(const char *name
)
227 const struct mesh_param_descr
*mdescr
= NULL
;
229 /* Find out what mesh parameter we want to change. */
230 for (i
= 0; i
< ARRAY_SIZE(_mesh_param_descrs
); i
++) {
231 if (!strcmp(_mesh_param_descrs
[i
].name
, name
))
232 return _mesh_param_descrs
+ i
;
236 print_all_mesh_param_descr();
243 static int set_interface_meshparam(struct nl80211_state
*state
,
246 int argc
, char **argv
)
248 const struct mesh_param_descr
*mdescr
;
249 struct nlattr
*container
;
253 container
= nla_nest_start(msg
, NL80211_ATTR_MESH_PARAMS
);
265 memset(&any
, 0, sizeof(_any
));
268 value
= strchr(name
, '=');
275 /* backward compat -- accept w/o '=' */
277 printf("Must specify a value for %s.\n", name
);
285 mdescr
= find_mesh_param(name
);
289 /* Parse the new value */
290 ret
= mdescr
->parse_fn(value
, &any
);
292 printf("%s must be set to a number "
293 "between 0 and %u\n", mdescr
->name
, ret
);
297 err
= mdescr
->nla_put_fn(msg
, mdescr
->mesh_param_num
, &any
);
301 nla_nest_end(msg
, container
);
306 COMMAND(set
, mesh_param
, "<param>=<value> [<param>=<value>]*",
307 NL80211_CMD_SET_MESH_PARAMS
, 0, CIB_NETDEV
, set_interface_meshparam
,
308 "Set mesh parameter (run command without any to see available ones).");
311 static int print_mesh_param_handler(struct nl_msg
*msg
, void *arg
)
313 const struct mesh_param_descr
*mdescr
= arg
;
314 struct nlattr
*attrs
[NL80211_ATTR_MAX
+ 1];
315 struct nlattr
*parent_attr
;
316 struct nlattr
*mesh_params
[NL80211_MESHCONF_ATTR_MAX
+ 1];
317 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
319 /* locate NL80211_ATTR_MESH_PARAMS */
320 nla_parse(attrs
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
321 genlmsg_attrlen(gnlh
, 0), NULL
);
322 parent_attr
= attrs
[NL80211_ATTR_MESH_PARAMS
];
326 /* unpack the mesh parameters */
327 if (nla_parse_nested(mesh_params
, NL80211_MESHCONF_ATTR_MAX
,
334 for (i
= 0; i
< ARRAY_SIZE(_mesh_param_descrs
); i
++) {
335 mdescr
= &_mesh_param_descrs
[i
];
336 printf("%s = ", mdescr
->name
);
337 mdescr
->nla_print_fn(mesh_params
[mdescr
->mesh_param_num
]);
343 /* print out the mesh parameter */
344 mdescr
->nla_print_fn(mesh_params
[mdescr
->mesh_param_num
]);
349 static int get_interface_meshparam(struct nl80211_state
*state
,
352 int argc
, char **argv
)
354 const struct mesh_param_descr
*mdescr
= NULL
;
360 mdescr
= find_mesh_param(argv
[0]);
365 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
,
366 print_mesh_param_handler
, (void *)mdescr
);
370 COMMAND(get
, mesh_param
, "[<param>]",
371 NL80211_CMD_GET_MESH_PARAMS
, 0, CIB_NETDEV
, get_interface_meshparam
,
372 "Retrieve mesh parameter (run command without any to see available ones).");
374 static int join_mesh(struct nl80211_state
*state
, struct nl_cb
*cb
,
375 struct nl_msg
*msg
, int argc
, char **argv
)
377 struct nlattr
*container
;
384 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(argv
[0]), argv
[0]);
388 if (argc
> 1 && strcmp(argv
[0], "mcast-rate") == 0) {
392 rate
= strtod(argv
[0], &end
);
396 NLA_PUT_U32(msg
, NL80211_ATTR_MCAST_RATE
, (int)(rate
* 10));
401 container
= nla_nest_start(msg
, NL80211_ATTR_MESH_SETUP
);
405 if (argc
> 1 && strcmp(argv
[0], "vendor_sync") == 0) {
408 if (strcmp(argv
[0], "on") == 0)
410 NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC
, 1);
413 NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC
, 0);
417 /* parse and put other NL80211_ATTR_MESH_SETUP elements here */
419 nla_nest_end(msg
, container
);
423 return set_interface_meshparam(state
, cb
, msg
, argc
, argv
);
427 COMMAND(mesh
, join
, "<mesh ID> [mcast-rate <rate in Mbps>] [vendor_sync on|off]"
428 " [<param>=<value>]*",
429 NL80211_CMD_JOIN_MESH
, 0, CIB_NETDEV
, join_mesh
,
430 "Join a mesh with the given mesh ID with mcast-rate and mesh parameters.");
432 static int leave_mesh(struct nl80211_state
*state
, struct nl_cb
*cb
,
433 struct nl_msg
*msg
, int argc
, char **argv
)
440 COMMAND(mesh
, leave
, NULL
, NL80211_CMD_LEAVE_MESH
, 0, CIB_NETDEV
, leave_mesh
,