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-2006 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_handle *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();
64 err = nl_errno(ENOMEM);
68 if (info->attrs[CTRL_ATTR_FAMILY_NAME] == NULL) {
69 err = nl_error(EINVAL, "Missing family name TLV");
73 if (info->attrs[CTRL_ATTR_FAMILY_ID] == NULL) {
74 err = nl_error(EINVAL, "Missing family id TLV");
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 = nl_errno(EINVAL);
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);
137 genl_family_put(family);
142 * @name Cache Management
146 struct nl_cache *genl_ctrl_alloc_cache(struct nl_handle *handle)
148 struct nl_cache * cache;
150 cache = nl_cache_alloc(&genl_ctrl_ops);
154 if (handle && nl_cache_refill(handle, cache) < 0) {
155 nl_cache_free(cache);
163 * Look up generic netlink family by id in the provided cache.
164 * @arg cache Generic netlink family cache.
165 * @arg id Family identifier.
167 * Searches through the cache looking for a registered family
168 * matching the specified identifier. The caller will own a
169 * reference on the returned object which needs to be given
170 * back after usage using genl_family_put().
172 * @return Generic netlink family object or NULL if no match was found.
174 struct genl_family *genl_ctrl_search(struct nl_cache *cache, int id)
176 struct genl_family *fam;
178 if (cache->c_ops != &genl_ctrl_ops)
181 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
182 if (fam->gf_id == id) {
183 nl_object_get((struct nl_object *) fam);
197 * Look up generic netlink family by family name in the provided cache.
198 * @arg cache Generic netlink family cache.
199 * @arg name Family name.
201 * Searches through the cache looking for a registered family
202 * matching the specified name. The caller will own a reference
203 * on the returned object which needs to be given back after
204 * usage using genl_family_put().
206 * @return Generic netlink family object or NULL if no match was found.
208 struct genl_family *genl_ctrl_search_by_name(struct nl_cache *cache,
211 struct genl_family *fam;
213 if (cache->c_ops != &genl_ctrl_ops)
216 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
217 if (!strcmp(name, fam->gf_name)) {
218 nl_object_get((struct nl_object *) fam);
229 * Resolve generic netlink family name to its identifier
230 * @arg handle Netlink Handle
231 * @arg name Name of generic netlink family
233 * Resolves the generic netlink family name to its identifer and returns
236 * @return A positive identifier or a negative error code.
238 int genl_ctrl_resolve(struct nl_handle *handle, const char *name)
240 struct nl_cache *cache;
241 struct genl_family *family;
244 cache = genl_ctrl_alloc_cache(handle);
246 return nl_get_errno();
248 family = genl_ctrl_search_by_name(cache, name);
249 if (family == NULL) {
250 err = nl_error(ENOENT, "Generic Netlink Family not found");
254 err = genl_family_get_id(family);
255 genl_family_put(family);
257 nl_cache_free(cache);
264 static struct genl_cmd genl_cmds[] = {
266 .c_id = CTRL_CMD_NEWFAMILY,
267 .c_name = "NEWFAMILY" ,
268 .c_maxattr = CTRL_ATTR_MAX,
269 .c_attr_policy = ctrl_policy,
270 .c_msg_parser = ctrl_msg_parser,
273 .c_id = CTRL_CMD_DELFAMILY,
274 .c_name = "DELFAMILY" ,
277 .c_id = CTRL_CMD_GETFAMILY,
278 .c_name = "GETFAMILY" ,
281 .c_id = CTRL_CMD_NEWOPS,
285 .c_id = CTRL_CMD_DELOPS,
290 static struct genl_ops genl_ops = {
292 .o_ncmds = ARRAY_SIZE(genl_cmds),
296 extern struct nl_object_ops genl_family_ops;
299 static struct nl_cache_ops genl_ctrl_ops = {
300 .co_name = "genl/family",
301 .co_hdrsize = GENL_HDRSIZE(0),
302 .co_msgtypes = GENL_FAMILY(GENL_ID_CTRL, "nlctrl"),
303 .co_genl = &genl_ops,
304 .co_protocol = NETLINK_GENERIC,
305 .co_request_update = ctrl_request_update,
306 .co_obj_ops = &genl_family_ops,
309 static void __init ctrl_init(void)
311 genl_register(&genl_ctrl_ops);
314 static void __exit ctrl_exit(void)
316 genl_unregister(&genl_ctrl_ops);