2 * lib/genl/ctrl.c Generic Netlink Controller
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
14 * @defgroup genl_ctrl Controller (Resolver)
16 * Resolves Generic Netlink family names to numeric identifiers.
18 * The controller is a component in the kernel that resolves Generic Netlink
19 * family names to their numeric identifiers. This module provides functions
20 * to query the controller to access the resolving functionality.
24 #include <netlink-private/genl.h>
25 #include <netlink/netlink.h>
26 #include <netlink/genl/genl.h>
27 #include <netlink/genl/family.h>
28 #include <netlink/genl/mngt.h>
29 #include <netlink/genl/ctrl.h>
30 #include <netlink/utils.h>
33 #define CTRL_VERSION 0x0001
35 static struct nl_cache_ops genl_ctrl_ops;
37 static int ctrl_request_update(struct nl_cache *c, struct nl_sock *h)
39 return genl_send_simple(h, GENL_ID_CTRL, CTRL_CMD_GETFAMILY,
40 CTRL_VERSION, NLM_F_DUMP);
43 static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
44 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
45 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_STRING,
46 .maxlen = GENL_NAMSIZ },
47 [CTRL_ATTR_VERSION] = { .type = NLA_U32 },
48 [CTRL_ATTR_HDRSIZE] = { .type = NLA_U32 },
49 [CTRL_ATTR_MAXATTR] = { .type = NLA_U32 },
50 [CTRL_ATTR_OPS] = { .type = NLA_NESTED },
51 [CTRL_ATTR_MCAST_GROUPS] = { .type = NLA_NESTED },
54 static struct nla_policy family_op_policy[CTRL_ATTR_OP_MAX+1] = {
55 [CTRL_ATTR_OP_ID] = { .type = NLA_U32 },
56 [CTRL_ATTR_OP_FLAGS] = { .type = NLA_U32 },
59 static struct nla_policy family_grp_policy[CTRL_ATTR_MCAST_GRP_MAX+1] = {
60 [CTRL_ATTR_MCAST_GRP_NAME] = { .type = NLA_STRING },
61 [CTRL_ATTR_MCAST_GRP_ID] = { .type = NLA_U32 },
64 static int parse_mcast_grps(struct genl_family *family, struct nlattr *grp_attr)
72 nla_for_each_nested(nla, grp_attr, remaining) {
73 struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX+1];
77 err = nla_parse_nested(tb, CTRL_ATTR_MCAST_GRP_MAX, nla,
82 if (tb[CTRL_ATTR_MCAST_GRP_ID] == NULL) {
83 err = -NLE_MISSING_ATTR;
86 id = nla_get_u32(tb[CTRL_ATTR_MCAST_GRP_ID]);
88 if (tb[CTRL_ATTR_MCAST_GRP_NAME] == NULL) {
89 err = -NLE_MISSING_ATTR;
92 name = nla_get_string(tb[CTRL_ATTR_MCAST_GRP_NAME]);
94 err = genl_family_add_grp(family, id, name);
105 static int ctrl_msg_parser(struct nl_cache_ops *ops, struct genl_cmd *cmd,
106 struct genl_info *info, void *arg)
108 struct genl_family *family;
109 struct nl_parser_param *pp = arg;
112 family = genl_family_alloc();
113 if (family == NULL) {
118 if (info->attrs[CTRL_ATTR_FAMILY_NAME] == NULL) {
119 err = -NLE_MISSING_ATTR;
123 if (info->attrs[CTRL_ATTR_FAMILY_ID] == NULL) {
124 err = -NLE_MISSING_ATTR;
128 family->ce_msgtype = info->nlh->nlmsg_type;
129 genl_family_set_id(family,
130 nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]));
131 genl_family_set_name(family,
132 nla_get_string(info->attrs[CTRL_ATTR_FAMILY_NAME]));
134 if (info->attrs[CTRL_ATTR_VERSION]) {
135 uint32_t version = nla_get_u32(info->attrs[CTRL_ATTR_VERSION]);
136 genl_family_set_version(family, version);
139 if (info->attrs[CTRL_ATTR_HDRSIZE]) {
140 uint32_t hdrsize = nla_get_u32(info->attrs[CTRL_ATTR_HDRSIZE]);
141 genl_family_set_hdrsize(family, hdrsize);
144 if (info->attrs[CTRL_ATTR_MAXATTR]) {
145 uint32_t maxattr = nla_get_u32(info->attrs[CTRL_ATTR_MAXATTR]);
146 genl_family_set_maxattr(family, maxattr);
149 if (info->attrs[CTRL_ATTR_OPS]) {
150 struct nlattr *nla, *nla_ops;
153 nla_ops = info->attrs[CTRL_ATTR_OPS];
154 nla_for_each_nested(nla, nla_ops, remaining) {
155 struct nlattr *tb[CTRL_ATTR_OP_MAX+1];
158 err = nla_parse_nested(tb, CTRL_ATTR_OP_MAX, nla,
163 if (tb[CTRL_ATTR_OP_ID] == NULL) {
164 err = -NLE_MISSING_ATTR;
168 id = nla_get_u32(tb[CTRL_ATTR_OP_ID]);
170 if (tb[CTRL_ATTR_OP_FLAGS])
171 flags = nla_get_u32(tb[CTRL_ATTR_OP_FLAGS]);
173 err = genl_family_add_op(family, id, flags);
180 if (info->attrs[CTRL_ATTR_MCAST_GROUPS]) {
181 err = parse_mcast_grps(family, info->attrs[CTRL_ATTR_MCAST_GROUPS]);
186 err = pp->pp_cb((struct nl_object *) family, pp);
188 genl_family_put(family);
193 * process responses from from the query sent by genl_ctrl_probe_by_name
194 * @arg nl_msg Returned message.
195 * @arg name genl_family structure to fill out.
197 * Process returned messages, filling out the missing informatino in the
198 * genl_family structure
200 * @return Indicator to keep processing frames or not
203 static int probe_response(struct nl_msg *msg, void *arg)
205 struct nlattr *tb[CTRL_ATTR_MAX+1];
206 struct nlmsghdr *nlh = nlmsg_hdr(msg);
207 struct genl_family *ret = (struct genl_family *)arg;
209 if (genlmsg_parse(nlh, 0, tb, CTRL_ATTR_MAX, ctrl_policy))
212 if (tb[CTRL_ATTR_FAMILY_ID])
213 genl_family_set_id(ret, nla_get_u16(tb[CTRL_ATTR_FAMILY_ID]));
215 if (tb[CTRL_ATTR_MCAST_GROUPS])
216 if (parse_mcast_grps(ret, tb[CTRL_ATTR_MCAST_GROUPS]) < 0)
223 * Look up generic netlink family by family name querying the kernel directly
225 * @arg name Family name.
227 * Directly query's the kernel for a given family name. The caller will own a
228 * reference on the returned object which needsd to be given back after usage
229 * using genl_family_put.
231 * Note: This API call differs from genl_ctrl_search_by_name in that it querys
232 * the kernel directly, alowing for module autoload to take place to resolve the
233 * family request. Using an nl_cache prevents that operation
235 * @return Generic netlink family object or NULL if no match was found.
237 static struct genl_family *genl_ctrl_probe_by_name(struct nl_sock *sk,
241 struct genl_family *ret;
242 struct nl_cb *cb, *orig;
245 ret = genl_family_alloc();
249 genl_family_set_name(ret, name);
255 if (!(orig = nl_socket_get_cb(sk)))
258 cb = nl_cb_clone(orig);
263 if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, GENL_ID_CTRL,
264 0, 0, CTRL_CMD_GETFAMILY, 1)) {
269 if (nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, name) < 0)
272 rc = nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, probe_response,
277 rc = nl_send_auto_complete(sk, msg);
281 rc = nl_recvmsgs(sk, cb);
285 /* If search was successful, request may be ACKed after data */
286 rc = wait_for_ack(sk);
290 if (genl_family_get_id(ret) != 0) {
301 genl_family_put(ret);
311 * @name Controller Cache
313 * The controller cache allows to keep a local copy of the list of all
314 * kernel side registered Generic Netlink families to quickly resolve
315 * multiple Generic Netlink family names without requiring to communicate
316 * with the kernel for each resolving iteration.
322 * Allocate a new controller cache
323 * @arg sk Generic Netlink socket
324 * @arg result Pointer to store resulting cache
326 * Allocates a new cache mirroring the state of the controller and stores it
327 * in \c *result. The allocated cache will contain a list of all currently
328 * registered kernel side Generic Netlink families. The cache is meant to be
329 * used to resolve family names locally.
331 * @return 0 on success or a negative error code.
333 int genl_ctrl_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
335 return nl_cache_alloc_and_fill(&genl_ctrl_ops, sk, result);
339 * Search controller cache for a numeric address match
340 * @arg cache Controller cache
341 * @arg id Numeric family identifier.
343 * Searches a previously allocated controller cache and looks for an entry
344 * that matches the specified numeric family identifier \c id. If a match
345 * is found successfully, the reference count of the matching object is
346 * increased by one before the objet is returned.
348 * @see genl_ctrl_alloc_cache()
349 * @see genl_ctrl_search_by_name()
350 * @see genl_family_put()
352 * @return Generic Netlink family object or NULL if no match was found.
354 struct genl_family *genl_ctrl_search(struct nl_cache *cache, int id)
356 struct genl_family *fam;
358 if (cache->c_ops != &genl_ctrl_ops)
361 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
362 if (fam->gf_id == id) {
363 nl_object_get((struct nl_object *) fam);
372 * Search controller cache for a family name match
373 * @arg cache Controller cache
374 * @arg name Name of Generic Netlink family
376 * Searches a previously allocated controller cache and looks for an entry
377 * that matches the specified family \c name. If a match is found successfully,
378 * the reference count of the matching object is increased by one before the
381 * @see genl_ctrl_alloc_cache()
382 * @see genl_ctrl_search()
383 * @see genl_family_put()
385 * @return Generic Netlink family object or NULL if no match was found.
387 struct genl_family *genl_ctrl_search_by_name(struct nl_cache *cache,
390 struct genl_family *fam;
392 if (cache->c_ops != &genl_ctrl_ops)
395 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
396 if (!strcmp(name, fam->gf_name)) {
397 nl_object_get((struct nl_object *) fam);
408 * @name Direct Resolvers
410 * These functions communicate directly with the kernel and do not require
411 * a cache to be kept up to date.
417 * Resolve Generic Netlink family name to numeric identifier
418 * @arg sk Generic Netlink socket.
419 * @arg name Name of Generic Netlink family
421 * Resolves the Generic Netlink family name to the corresponding numeric
422 * family identifier. This function queries the kernel directly, use
423 * genl_ctrl_search_by_name() if you need to resolve multiple names.
425 * @see genl_ctrl_search_by_name()
427 * @return The numeric family identifier or a negative error code.
429 int genl_ctrl_resolve(struct nl_sock *sk, const char *name)
431 struct genl_family *family;
434 family = genl_ctrl_probe_by_name(sk, name);
435 if (family == NULL) {
436 err = -NLE_OBJ_NOTFOUND;
440 err = genl_family_get_id(family);
441 genl_family_put(family);
446 static int genl_ctrl_grp_by_name(const struct genl_family *family,
447 const char *grp_name)
449 struct genl_family_grp *grp;
451 nl_list_for_each_entry(grp, &family->gf_mc_grps, list) {
452 if (!strcmp(grp->name, grp_name)) {
457 return -NLE_OBJ_NOTFOUND;
461 * Resolve Generic Netlink family group name
462 * @arg sk Generic Netlink socket
463 * @arg family_name Name of Generic Netlink family
464 * @arg grp_name Name of group to resolve
466 * Looks up the family object and resolves the group name to the numeric
469 * @return Numeric group identifier or a negative error code.
471 int genl_ctrl_resolve_grp(struct nl_sock *sk, const char *family_name,
472 const char *grp_name)
475 struct genl_family *family;
478 family = genl_ctrl_probe_by_name(sk, family_name);
479 if (family == NULL) {
480 err = -NLE_OBJ_NOTFOUND;
484 err = genl_ctrl_grp_by_name(family, grp_name);
485 genl_family_put(family);
493 static struct genl_cmd genl_cmds[] = {
495 .c_id = CTRL_CMD_NEWFAMILY,
496 .c_name = "NEWFAMILY" ,
497 .c_maxattr = CTRL_ATTR_MAX,
498 .c_attr_policy = ctrl_policy,
499 .c_msg_parser = ctrl_msg_parser,
502 .c_id = CTRL_CMD_DELFAMILY,
503 .c_name = "DELFAMILY" ,
506 .c_id = CTRL_CMD_GETFAMILY,
507 .c_name = "GETFAMILY" ,
510 .c_id = CTRL_CMD_NEWOPS,
514 .c_id = CTRL_CMD_DELOPS,
519 static struct genl_ops genl_ops = {
521 .o_ncmds = ARRAY_SIZE(genl_cmds),
524 extern struct nl_object_ops genl_family_ops;
526 static struct nl_cache_ops genl_ctrl_ops = {
527 .co_name = "genl/family",
528 .co_hdrsize = GENL_HDRSIZE(0),
529 .co_msgtypes = GENL_FAMILY(GENL_ID_CTRL, "nlctrl"),
530 .co_genl = &genl_ops,
531 .co_protocol = NETLINK_GENERIC,
532 .co_request_update = ctrl_request_update,
533 .co_obj_ops = &genl_family_ops,
536 static void __init ctrl_init(void)
538 genl_register(&genl_ctrl_ops);
541 static void __exit ctrl_exit(void)
543 genl_unregister(&genl_ctrl_ops);