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
{
25 /* describes a mesh parameter */
26 struct mesh_param_descr
{
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
*);
34 /* utility functions for manipulating and printing u8/u16/u32 values and
36 static int _my_nla_put_u8(struct nl_msg
*n
, int mesh_param_num
, _any
*value
)
38 return nla_put(n
, mesh_param_num
, sizeof(uint8_t), &value
->u
.as_8
);
41 static int _my_nla_put_u16(struct nl_msg
*n
, int mesh_param_num
, _any
*value
)
43 return nla_put(n
, mesh_param_num
, sizeof(uint16_t), &value
->u
.as_16
);
46 static int _my_nla_put_u32(struct nl_msg
*n
, int mesh_param_num
, _any
*value
)
48 return nla_put(n
, mesh_param_num
, sizeof(uint32_t), &value
->u
.as_32
);
51 static uint32_t _parse_u8(const char *str
, _any
*ret
)
54 unsigned long int v
= strtoul(str
, &endptr
, 10);
59 ret
->u
.as_8
= (uint8_t)v
;
63 static uint32_t _parse_u8_as_bool(const char *str
, _any
*ret
)
66 unsigned long int v
= strtoul(str
, &endptr
, 10);
71 ret
->u
.as_8
= (uint8_t)v
;
75 static uint32_t _parse_u16(const char *str
, _any
*ret
)
78 long int v
= strtol(str
, &endptr
, 10);
81 if ((v
< 0) || (v
> 0xffff))
83 ret
->u
.as_16
= (uint16_t)v
;
87 static uint32_t _parse_u32(const char *str
, _any
*ret
)
90 long long int v
= strtoll(str
, &endptr
, 10);
93 if ((v
< 0) || (v
> 0xffffffff))
95 ret
->u
.as_32
= (uint32_t)v
;
99 static void _print_u8(struct nlattr
*a
)
101 printf("%d", nla_get_u8(a
));
104 static void _print_u16(struct nlattr
*a
)
106 printf("%d", nla_get_u16(a
));
109 static void _print_u16_timeout(struct nlattr
*a
)
111 printf("%d milliseconds", nla_get_u16(a
));
114 static void _print_u16_in_TUs(struct nlattr
*a
)
116 printf("%d TUs", nla_get_u16(a
));
119 static void _print_u32_timeout(struct nlattr
*a
)
121 printf("%u milliseconds", nla_get_u32(a
));
124 static void _print_u32_in_TUs(struct nlattr
*a
)
126 printf("%d TUs", nla_get_u32(a
));
129 /* The current mesh parameters */
130 const static struct mesh_param_descr _mesh_param_descrs
[] =
132 {"mesh_retry_timeout",
133 NL80211_MESHCONF_RETRY_TIMEOUT
,
134 _my_nla_put_u16
, _parse_u16
, _print_u16_timeout
},
135 {"mesh_confirm_timeout",
136 NL80211_MESHCONF_CONFIRM_TIMEOUT
,
137 _my_nla_put_u16
, _parse_u16
, _print_u16_timeout
},
138 {"mesh_holding_timeout",
139 NL80211_MESHCONF_HOLDING_TIMEOUT
,
140 _my_nla_put_u16
, _parse_u16
, _print_u16_timeout
},
141 {"mesh_max_peer_links",
142 NL80211_MESHCONF_MAX_PEER_LINKS
,
143 _my_nla_put_u16
, _parse_u16
, _print_u16
},
145 NL80211_MESHCONF_MAX_RETRIES
,
146 _my_nla_put_u8
, _parse_u8
, _print_u8
},
148 NL80211_MESHCONF_TTL
,
149 _my_nla_put_u8
, _parse_u8
, _print_u8
},
151 NL80211_MESHCONF_ELEMENT_TTL
,
152 _my_nla_put_u8
, _parse_u8
, _print_u8
},
153 {"mesh_auto_open_plinks",
154 NL80211_MESHCONF_AUTO_OPEN_PLINKS
,
155 _my_nla_put_u8
, _parse_u8_as_bool
, _print_u8
},
156 {"mesh_hwmp_max_preq_retries",
157 NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES
,
158 _my_nla_put_u8
, _parse_u8
, _print_u8
},
159 {"mesh_path_refresh_time",
160 NL80211_MESHCONF_PATH_REFRESH_TIME
,
161 _my_nla_put_u32
, _parse_u32
, _print_u32_timeout
},
162 {"mesh_min_discovery_timeout",
163 NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT
,
164 _my_nla_put_u16
, _parse_u16
, _print_u16_timeout
},
165 {"mesh_hwmp_active_path_timeout",
166 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT
,
167 _my_nla_put_u32
, _parse_u32
, _print_u32_in_TUs
},
168 {"mesh_hwmp_preq_min_interval",
169 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL
,
170 _my_nla_put_u16
, _parse_u16
, _print_u16_in_TUs
},
171 {"mesh_hwmp_net_diameter_traversal_time",
172 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME
,
173 _my_nla_put_u16
, _parse_u16
, _print_u16_in_TUs
},
174 {"mesh_hwmp_rootmode", NL80211_MESHCONF_HWMP_ROOTMODE
,
175 _my_nla_put_u8
, _parse_u8
, _print_u8
},
176 {"mesh_hwmp_rann_interval", NL80211_MESHCONF_HWMP_RANN_INTERVAL
,
177 _my_nla_put_u16
, _parse_u16
, _print_u16
},
178 {"mesh_gate_announcements", NL80211_MESHCONF_GATE_ANNOUNCEMENTS
,
179 _my_nla_put_u8
, _parse_u8
, _print_u8
},
182 static void print_all_mesh_param_descr(void)
186 printf("Possible mesh parameters are:\n");
188 for (i
= 0; i
< ARRAY_SIZE(_mesh_param_descrs
); i
++)
189 printf(" - %s\n", _mesh_param_descrs
[i
].name
);
192 static const struct mesh_param_descr
*find_mesh_param(const char *name
)
195 const struct mesh_param_descr
*mdescr
= NULL
;
197 /* Find out what mesh parameter we want to change. */
198 for (i
= 0; i
< ARRAY_SIZE(_mesh_param_descrs
); i
++) {
199 if (!strcmp(_mesh_param_descrs
[i
].name
, name
))
200 return _mesh_param_descrs
+ i
;
204 print_all_mesh_param_descr();
211 static int set_interface_meshparam(struct nl80211_state
*state
,
214 int argc
, char **argv
)
216 const struct mesh_param_descr
*mdescr
;
217 struct nlattr
*container
;
221 container
= nla_nest_start(msg
, NL80211_ATTR_MESH_PARAMS
);
233 memset(&any
, 0, sizeof(_any
));
236 value
= strchr(name
, '=');
243 /* backward compat -- accept w/o '=' */
245 printf("Must specify a value for %s.\n", name
);
253 mdescr
= find_mesh_param(name
);
257 /* Parse the new value */
258 ret
= mdescr
->parse_fn(value
, &any
);
260 printf("%s must be set to a number "
261 "between 0 and %u\n", mdescr
->name
, ret
);
265 err
= mdescr
->nla_put_fn(msg
, mdescr
->mesh_param_num
, &any
);
269 nla_nest_end(msg
, container
);
274 COMMAND(set
, mesh_param
, "<param>=<value> [<param>=<value>]*",
275 NL80211_CMD_SET_MESH_PARAMS
, 0, CIB_NETDEV
, set_interface_meshparam
,
276 "Set mesh parameter (run command without any to see available ones).");
279 static int print_mesh_param_handler(struct nl_msg
*msg
, void *arg
)
281 const struct mesh_param_descr
*mdescr
= arg
;
282 struct nlattr
*attrs
[NL80211_ATTR_MAX
+ 1];
283 struct nlattr
*parent_attr
;
284 struct nlattr
*mesh_params
[NL80211_MESHCONF_ATTR_MAX
+ 1];
285 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
287 /* locate NL80211_ATTR_MESH_PARAMS */
288 nla_parse(attrs
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
289 genlmsg_attrlen(gnlh
, 0), NULL
);
290 parent_attr
= attrs
[NL80211_ATTR_MESH_PARAMS
];
294 /* unpack the mesh parameters */
295 if (nla_parse_nested(mesh_params
, NL80211_MESHCONF_ATTR_MAX
,
302 for (i
= 0; i
< ARRAY_SIZE(_mesh_param_descrs
); i
++) {
303 mdescr
= &_mesh_param_descrs
[i
];
304 printf("%s = ", mdescr
->name
);
305 mdescr
->nla_print_fn(mesh_params
[mdescr
->mesh_param_num
]);
311 /* print out the mesh parameter */
312 mdescr
->nla_print_fn(mesh_params
[mdescr
->mesh_param_num
]);
317 static int get_interface_meshparam(struct nl80211_state
*state
,
320 int argc
, char **argv
)
322 const struct mesh_param_descr
*mdescr
= NULL
;
328 mdescr
= find_mesh_param(argv
[0]);
333 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
,
334 print_mesh_param_handler
, (void *)mdescr
);
338 COMMAND(get
, mesh_param
, "[<param>]",
339 NL80211_CMD_GET_MESH_PARAMS
, 0, CIB_NETDEV
, get_interface_meshparam
,
340 "Retrieve mesh parameter (run command without any to see available ones).");
342 static int join_mesh(struct nl80211_state
*state
, struct nl_cb
*cb
,
343 struct nl_msg
*msg
, int argc
, char **argv
)
348 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(argv
[0]), argv
[0]);
354 return set_interface_meshparam(state
, cb
, msg
, argc
, argv
);
358 COMMAND(mesh
, join
, "<mesh ID> [<param>=<value>]*",
359 NL80211_CMD_JOIN_MESH
, 0, CIB_NETDEV
, join_mesh
,
360 "Join a mesh with the given mesh ID and mesh parameters.");
362 static int leave_mesh(struct nl80211_state
*state
, struct nl_cb
*cb
,
363 struct nl_msg
*msg
, int argc
, char **argv
)
370 COMMAND(mesh
, leave
, NULL
, NL80211_CMD_LEAVE_MESH
, 0, CIB_NETDEV
, leave_mesh
,