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-2008 Thomas Graf <tgraf@suug.ch>
14 * @defgroup ctrl Controller
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
31 static struct nl_cache_ops genl_ctrl_ops;
34 static int ctrl_request_update(struct nl_cache *c, struct nl_sock *h)
36 return genl_send_simple(h, GENL_ID_CTRL, CTRL_CMD_GETFAMILY,
37 CTRL_VERSION, NLM_F_DUMP);
40 static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
41 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
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 },
47 [CTRL_ATTR_OPS] = { .type = NLA_NESTED },
50 static struct nla_policy family_op_policy[CTRL_ATTR_OP_MAX+1] = {
51 [CTRL_ATTR_OP_ID] = { .type = NLA_U32 },
52 [CTRL_ATTR_OP_FLAGS] = { .type = NLA_U32 },
55 static int ctrl_msg_parser(struct nl_cache_ops *ops, struct genl_cmd *cmd,
56 struct genl_info *info, void *arg)
58 struct genl_family *family;
59 struct nl_parser_param *pp = arg;
62 family = genl_family_alloc();
68 if (info->attrs[CTRL_ATTR_FAMILY_NAME] == NULL) {
69 err = -NLE_MISSING_ATTR;
73 if (info->attrs[CTRL_ATTR_FAMILY_ID] == NULL) {
74 err = -NLE_MISSING_ATTR;
78 family->ce_msgtype = info->nlh->nlmsg_type;
79 genl_family_set_id(family,
80 nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]));
81 genl_family_set_name(family,
82 nla_get_string(info->attrs[CTRL_ATTR_FAMILY_NAME]));
84 if (info->attrs[CTRL_ATTR_VERSION]) {
85 uint32_t version = nla_get_u32(info->attrs[CTRL_ATTR_VERSION]);
86 genl_family_set_version(family, version);
89 if (info->attrs[CTRL_ATTR_HDRSIZE]) {
90 uint32_t hdrsize = nla_get_u32(info->attrs[CTRL_ATTR_HDRSIZE]);
91 genl_family_set_hdrsize(family, hdrsize);
94 if (info->attrs[CTRL_ATTR_MAXATTR]) {
95 uint32_t maxattr = nla_get_u32(info->attrs[CTRL_ATTR_MAXATTR]);
96 genl_family_set_maxattr(family, maxattr);
99 if (info->attrs[CTRL_ATTR_OPS]) {
100 struct nlattr *nla, *nla_ops;
103 nla_ops = info->attrs[CTRL_ATTR_OPS];
104 nla_for_each_nested(nla, nla_ops, remaining) {
105 struct nlattr *tb[CTRL_ATTR_OP_MAX+1];
108 err = nla_parse_nested(tb, CTRL_ATTR_OP_MAX, nla,
113 if (tb[CTRL_ATTR_OP_ID] == NULL) {
114 err = -NLE_MISSING_ATTR;
118 id = nla_get_u32(tb[CTRL_ATTR_OP_ID]);
120 if (tb[CTRL_ATTR_OP_FLAGS])
121 flags = nla_get_u32(tb[CTRL_ATTR_OP_FLAGS]);
123 err = genl_family_add_op(family, id, flags);
130 err = pp->pp_cb((struct nl_object *) family, pp);
132 genl_family_put(family);
137 * @name Cache Management
141 int genl_ctrl_alloc_cache(struct nl_sock *sock, struct nl_cache **result)
143 return nl_cache_alloc_and_fill(&genl_ctrl_ops, sock, result);
147 * Look up generic netlink family by id in the provided cache.
148 * @arg cache Generic netlink family cache.
149 * @arg id Family identifier.
151 * Searches through the cache looking for a registered family
152 * matching the specified identifier. The caller will own a
153 * reference on the returned object which needs to be given
154 * back after usage using genl_family_put().
156 * @return Generic netlink family object or NULL if no match was found.
158 struct genl_family *genl_ctrl_search(struct nl_cache *cache, int id)
160 struct genl_family *fam;
162 if (cache->c_ops != &genl_ctrl_ops)
165 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
166 if (fam->gf_id == id) {
167 nl_object_get((struct nl_object *) fam);
181 * Look up generic netlink family by family name in the provided cache.
182 * @arg cache Generic netlink family cache.
183 * @arg name Family name.
185 * Searches through the cache looking for a registered family
186 * matching the specified name. The caller will own a reference
187 * on the returned object which needs to be given back after
188 * usage using genl_family_put().
190 * @return Generic netlink family object or NULL if no match was found.
192 struct genl_family *genl_ctrl_search_by_name(struct nl_cache *cache,
195 struct genl_family *fam;
197 if (cache->c_ops != &genl_ctrl_ops)
200 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
201 if (!strcmp(name, fam->gf_name)) {
202 nl_object_get((struct nl_object *) fam);
213 * Resolve generic netlink family name to its identifier
214 * @arg sk Netlink socket.
215 * @arg name Name of generic netlink family
217 * Resolves the generic netlink family name to its identifer and returns
220 * @return A positive identifier or a negative error code.
222 int genl_ctrl_resolve(struct nl_sock *sk, const char *name)
224 struct nl_cache *cache;
225 struct genl_family *family;
228 if ((err = genl_ctrl_alloc_cache(sk, &cache)) < 0)
231 family = genl_ctrl_search_by_name(cache, name);
232 if (family == NULL) {
233 err = -NLE_OBJ_NOTFOUND;
237 err = genl_family_get_id(family);
238 genl_family_put(family);
240 nl_cache_free(cache);
247 static struct genl_cmd genl_cmds[] = {
249 .c_id = CTRL_CMD_NEWFAMILY,
250 .c_name = "NEWFAMILY" ,
251 .c_maxattr = CTRL_ATTR_MAX,
252 .c_attr_policy = ctrl_policy,
253 .c_msg_parser = ctrl_msg_parser,
256 .c_id = CTRL_CMD_DELFAMILY,
257 .c_name = "DELFAMILY" ,
260 .c_id = CTRL_CMD_GETFAMILY,
261 .c_name = "GETFAMILY" ,
264 .c_id = CTRL_CMD_NEWOPS,
268 .c_id = CTRL_CMD_DELOPS,
273 static struct genl_ops genl_ops = {
275 .o_ncmds = ARRAY_SIZE(genl_cmds),
279 extern struct nl_object_ops genl_family_ops;
282 static struct nl_cache_ops genl_ctrl_ops = {
283 .co_name = "genl/family",
284 .co_hdrsize = GENL_HDRSIZE(0),
285 .co_msgtypes = GENL_FAMILY(GENL_ID_CTRL, "nlctrl"),
286 .co_genl = &genl_ops,
287 .co_protocol = NETLINK_GENERIC,
288 .co_request_update = ctrl_request_update,
289 .co_obj_ops = &genl_family_ops,
292 static void __init ctrl_init(void)
294 genl_register(&genl_ctrl_ops);
297 static void __exit ctrl_exit(void)
299 genl_unregister(&genl_ctrl_ops);