20 #include <netlink-generic.h>
21 #include <netlink/netlink.h>
22 #include <netlink/genl/genl.h>
23 #include <netlink/genl/family.h>
24 #include <netlink/genl/mngt.h>
25 #include <netlink/genl/ctrl.h>
26 #include <netlink/utils.h>
29 #define CTRL_VERSION 0x0001
34 static int ctrl_request_update(
struct nl_cache *c,
struct nl_sock *h)
37 CTRL_VERSION, NLM_F_DUMP);
40 static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
42 [CTRL_ATTR_FAMILY_NAME] = { .type =
NLA_STRING,
43 .maxlen = GENL_NAMSIZ },
44 [CTRL_ATTR_VERSION] = { .type =
NLA_U32 },
45 [CTRL_ATTR_HDRSIZE] = { .type =
NLA_U32 },
46 [CTRL_ATTR_MAXATTR] = { .type =
NLA_U32 },
48 [CTRL_ATTR_MCAST_GROUPS] = { .type =
NLA_NESTED },
51 static struct nla_policy family_op_policy[CTRL_ATTR_OP_MAX+1] = {
53 [CTRL_ATTR_OP_FLAGS] = { .type =
NLA_U32 },
56 static struct nla_policy family_grp_policy[CTRL_ATTR_MCAST_GRP_MAX+1] = {
58 [CTRL_ATTR_MCAST_GRP_ID] = { .type =
NLA_U32 },
64 struct genl_family *family;
68 family = genl_family_alloc();
74 if (info->attrs[CTRL_ATTR_FAMILY_NAME] == NULL) {
75 err = -NLE_MISSING_ATTR;
79 if (info->attrs[CTRL_ATTR_FAMILY_ID] == NULL) {
80 err = -NLE_MISSING_ATTR;
84 family->ce_msgtype = info->nlh->nlmsg_type;
85 genl_family_set_id(family,
87 genl_family_set_name(family,
90 if (info->attrs[CTRL_ATTR_VERSION]) {
91 uint32_t version =
nla_get_u32(info->attrs[CTRL_ATTR_VERSION]);
92 genl_family_set_version(family, version);
95 if (info->attrs[CTRL_ATTR_HDRSIZE]) {
96 uint32_t hdrsize =
nla_get_u32(info->attrs[CTRL_ATTR_HDRSIZE]);
97 genl_family_set_hdrsize(family, hdrsize);
100 if (info->attrs[CTRL_ATTR_MAXATTR]) {
101 uint32_t maxattr =
nla_get_u32(info->attrs[CTRL_ATTR_MAXATTR]);
102 genl_family_set_maxattr(family, maxattr);
105 if (info->attrs[CTRL_ATTR_OPS]) {
106 struct nlattr *nla, *nla_ops;
109 nla_ops = info->attrs[CTRL_ATTR_OPS];
111 struct nlattr *tb[CTRL_ATTR_OP_MAX+1];
119 if (tb[CTRL_ATTR_OP_ID] == NULL) {
120 err = -NLE_MISSING_ATTR;
126 if (tb[CTRL_ATTR_OP_FLAGS])
129 err = genl_family_add_op(family,
id, flags);
136 if (info->attrs[CTRL_ATTR_MCAST_GROUPS]) {
137 struct nlattr *nla, *nla_grps;
140 nla_grps = info->attrs[CTRL_ATTR_MCAST_GROUPS];
142 struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX+1];
151 if (tb[CTRL_ATTR_MCAST_GRP_ID] == NULL) {
152 err = -NLE_MISSING_ATTR;
157 if (tb[CTRL_ATTR_MCAST_GRP_NAME] == NULL) {
158 err = -NLE_MISSING_ATTR;
163 err = genl_family_add_grp(family,
id, name);
172 genl_family_put(family);
181 int genl_ctrl_alloc_cache(
struct nl_sock *sock,
struct nl_cache **result)
200 struct genl_family *fam;
202 if (cache->c_ops != &genl_ctrl_ops)
205 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
206 if (fam->gf_id ==
id) {
235 struct genl_family *fam;
237 if (cache->c_ops != &genl_ctrl_ops)
240 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
241 if (!strcmp(name, fam->gf_name)) {
264 struct nl_cache *cache;
265 struct genl_family *family;
268 if ((err = genl_ctrl_alloc_cache(sk, &cache)) < 0)
272 if (family == NULL) {
273 err = -NLE_OBJ_NOTFOUND;
277 err = genl_family_get_id(family);
278 genl_family_put(family);
285 static int genl_ctrl_grp_by_name(
const struct genl_family *family,
286 const char *grp_name)
288 struct genl_family_grp *grp;
290 nl_list_for_each_entry(grp, &family->gf_mc_grps, list) {
291 if (!strcmp(grp->name, grp_name)) {
296 return -NLE_OBJ_NOTFOUND;
299 int genl_ctrl_resolve_grp(
struct nl_sock *sk,
const char *family_name,
300 const char *grp_name)
302 struct nl_cache *cache;
303 struct genl_family *family;
306 if ((err = genl_ctrl_alloc_cache(sk, &cache)) < 0)
310 if (family == NULL) {
311 err = -NLE_OBJ_NOTFOUND;
315 err = genl_ctrl_grp_by_name(family, grp_name);
316 genl_family_put(family);
325 static struct genl_cmd genl_cmds[] = {
327 .
c_id = CTRL_CMD_NEWFAMILY,
328 .c_name =
"NEWFAMILY" ,
329 .c_maxattr = CTRL_ATTR_MAX,
330 .c_attr_policy = ctrl_policy,
331 .c_msg_parser = ctrl_msg_parser,
334 .c_id = CTRL_CMD_DELFAMILY,
335 .c_name =
"DELFAMILY" ,
338 .c_id = CTRL_CMD_GETFAMILY,
339 .c_name =
"GETFAMILY" ,
342 .c_id = CTRL_CMD_NEWOPS,
346 .c_id = CTRL_CMD_DELOPS,
353 .o_ncmds = ARRAY_SIZE(genl_cmds),
362 .co_hdrsize = GENL_HDRSIZE(0),
363 .co_msgtypes = GENL_FAMILY(GENL_ID_CTRL,
"nlctrl"),
364 .co_genl = &genl_ops,
365 .co_protocol = NETLINK_GENERIC,
366 .co_request_update = ctrl_request_update,
367 .co_obj_ops = &genl_family_ops,
370 static void __init ctrl_init(
void)
375 static void __exit ctrl_exit(
void)