ctrl.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * lib/genl/ctrl.c Generic Netlink Controller
  3. *
  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
  7. * of the License.
  8. *
  9. * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup genl_mngt
  13. * @defgroup ctrl Controller
  14. * @brief
  15. *
  16. * @{
  17. */
  18. #include <netlink-generic.h>
  19. #include <netlink/netlink.h>
  20. #include <netlink/genl/genl.h>
  21. #include <netlink/genl/family.h>
  22. #include <netlink/genl/mngt.h>
  23. #include <netlink/genl/ctrl.h>
  24. #include <netlink/utils.h>
  25. /** @cond SKIP */
  26. #define CTRL_VERSION 0x0001
  27. static struct nl_cache_ops genl_ctrl_ops;
  28. /** @endcond */
  29. static int ctrl_request_update(struct nl_cache *c, struct nl_handle *h)
  30. {
  31. return genl_send_simple(h, GENL_ID_CTRL, CTRL_CMD_GETFAMILY,
  32. CTRL_VERSION, NLM_F_DUMP);
  33. }
  34. static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
  35. [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
  36. [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_STRING,
  37. .maxlen = GENL_NAMSIZ },
  38. [CTRL_ATTR_VERSION] = { .type = NLA_U32 },
  39. [CTRL_ATTR_HDRSIZE] = { .type = NLA_U32 },
  40. [CTRL_ATTR_MAXATTR] = { .type = NLA_U32 },
  41. [CTRL_ATTR_OPS] = { .type = NLA_NESTED },
  42. };
  43. static struct nla_policy family_op_policy[CTRL_ATTR_OP_MAX+1] = {
  44. [CTRL_ATTR_OP_ID] = { .type = NLA_U32 },
  45. [CTRL_ATTR_OP_FLAGS] = { .type = NLA_U32 },
  46. };
  47. static int ctrl_msg_parser(struct nl_cache_ops *ops, struct genl_cmd *cmd,
  48. struct genl_info *info, void *arg)
  49. {
  50. struct genl_family *family;
  51. struct nl_parser_param *pp = arg;
  52. int err;
  53. family = genl_family_alloc();
  54. if (family == NULL) {
  55. err = nl_errno(ENOMEM);
  56. goto errout;
  57. }
  58. if (info->attrs[CTRL_ATTR_FAMILY_NAME] == NULL) {
  59. err = nl_error(EINVAL, "Missing family name TLV");
  60. goto errout;
  61. }
  62. if (info->attrs[CTRL_ATTR_FAMILY_ID] == NULL) {
  63. err = nl_error(EINVAL, "Missing family id TLV");
  64. goto errout;
  65. }
  66. family->ce_msgtype = info->nlh->nlmsg_type;
  67. genl_family_set_id(family,
  68. nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]));
  69. genl_family_set_name(family,
  70. nla_get_string(info->attrs[CTRL_ATTR_FAMILY_NAME]));
  71. if (info->attrs[CTRL_ATTR_VERSION]) {
  72. uint32_t version = nla_get_u32(info->attrs[CTRL_ATTR_VERSION]);
  73. genl_family_set_version(family, version);
  74. }
  75. if (info->attrs[CTRL_ATTR_HDRSIZE]) {
  76. uint32_t hdrsize = nla_get_u32(info->attrs[CTRL_ATTR_HDRSIZE]);
  77. genl_family_set_hdrsize(family, hdrsize);
  78. }
  79. if (info->attrs[CTRL_ATTR_MAXATTR]) {
  80. uint32_t maxattr = nla_get_u32(info->attrs[CTRL_ATTR_MAXATTR]);
  81. genl_family_set_maxattr(family, maxattr);
  82. }
  83. if (info->attrs[CTRL_ATTR_OPS]) {
  84. struct nlattr *nla, *nla_ops;
  85. int remaining;
  86. nla_ops = info->attrs[CTRL_ATTR_OPS];
  87. nla_for_each_nested(nla, nla_ops, remaining) {
  88. struct nlattr *tb[CTRL_ATTR_OP_MAX+1];
  89. int flags = 0, id;
  90. err = nla_parse_nested(tb, CTRL_ATTR_OP_MAX, nla,
  91. family_op_policy);
  92. if (err < 0)
  93. goto errout;
  94. if (tb[CTRL_ATTR_OP_ID] == NULL) {
  95. err = nl_errno(EINVAL);
  96. goto errout;
  97. }
  98. id = nla_get_u32(tb[CTRL_ATTR_OP_ID]);
  99. if (tb[CTRL_ATTR_OP_FLAGS])
  100. flags = nla_get_u32(tb[CTRL_ATTR_OP_FLAGS]);
  101. err = genl_family_add_op(family, id, flags);
  102. if (err < 0)
  103. goto errout;
  104. }
  105. }
  106. err = pp->pp_cb((struct nl_object *) family, pp);
  107. if (err < 0)
  108. goto errout;
  109. err = P_ACCEPT;
  110. errout:
  111. genl_family_put(family);
  112. return err;
  113. }
  114. /**
  115. * @name Cache Management
  116. * @{
  117. */
  118. struct nl_cache *genl_ctrl_alloc_cache(struct nl_handle *handle)
  119. {
  120. struct nl_cache * cache;
  121. cache = nl_cache_alloc(&genl_ctrl_ops);
  122. if (cache == NULL)
  123. return NULL;
  124. if (handle && nl_cache_refill(handle, cache) < 0) {
  125. nl_cache_free(cache);
  126. return NULL;
  127. }
  128. return cache;
  129. }
  130. /**
  131. * Look up generic netlink family by id in the provided cache.
  132. * @arg cache Generic netlink family cache.
  133. * @arg id Family identifier.
  134. *
  135. * Searches through the cache looking for a registered family
  136. * matching the specified identifier. The caller will own a
  137. * reference on the returned object which needs to be given
  138. * back after usage using genl_family_put().
  139. *
  140. * @return Generic netlink family object or NULL if no match was found.
  141. */
  142. struct genl_family *genl_ctrl_search(struct nl_cache *cache, int id)
  143. {
  144. struct genl_family *fam;
  145. if (cache->c_ops != &genl_ctrl_ops)
  146. BUG();
  147. nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
  148. if (fam->gf_id == id) {
  149. nl_object_get((struct nl_object *) fam);
  150. return fam;
  151. }
  152. }
  153. return NULL;
  154. }
  155. /**
  156. * @name Resolver
  157. * @{
  158. */
  159. /**
  160. * Look up generic netlink family by family name in the provided cache.
  161. * @arg cache Generic netlink family cache.
  162. * @arg name Family name.
  163. *
  164. * Searches through the cache looking for a registered family
  165. * matching the specified name. The caller will own a reference
  166. * on the returned object which needs to be given back after
  167. * usage using genl_family_put().
  168. *
  169. * @return Generic netlink family object or NULL if no match was found.
  170. */
  171. struct genl_family *genl_ctrl_search_by_name(struct nl_cache *cache,
  172. const char *name)
  173. {
  174. struct genl_family *fam;
  175. if (cache->c_ops != &genl_ctrl_ops)
  176. BUG();
  177. nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
  178. if (!strcmp(name, fam->gf_name)) {
  179. nl_object_get((struct nl_object *) fam);
  180. return fam;
  181. }
  182. }
  183. return NULL;
  184. }
  185. /** @} */
  186. /**
  187. * Resolve generic netlink family name to its identifier
  188. * @arg handle Netlink Handle
  189. * @arg name Name of generic netlink family
  190. *
  191. * Resolves the generic netlink family name to its identifer and returns
  192. * it.
  193. *
  194. * @return A positive identifier or a negative error code.
  195. */
  196. int genl_ctrl_resolve(struct nl_handle *handle, const char *name)
  197. {
  198. struct nl_cache *cache;
  199. struct genl_family *family;
  200. int err;
  201. cache = genl_ctrl_alloc_cache(handle);
  202. if (cache == NULL)
  203. return nl_get_errno();
  204. family = genl_ctrl_search_by_name(cache, name);
  205. if (family == NULL) {
  206. err = nl_error(ENOENT, "Generic Netlink Family not found");
  207. goto errout;
  208. }
  209. err = genl_family_get_id(family);
  210. genl_family_put(family);
  211. errout:
  212. nl_cache_free(cache);
  213. return err;
  214. }
  215. /** @} */
  216. static struct genl_cmd genl_cmds[] = {
  217. {
  218. .c_id = CTRL_CMD_NEWFAMILY,
  219. .c_name = "NEWFAMILY" ,
  220. .c_maxattr = CTRL_ATTR_MAX,
  221. .c_attr_policy = ctrl_policy,
  222. .c_msg_parser = ctrl_msg_parser,
  223. },
  224. {
  225. .c_id = CTRL_CMD_DELFAMILY,
  226. .c_name = "DELFAMILY" ,
  227. },
  228. {
  229. .c_id = CTRL_CMD_GETFAMILY,
  230. .c_name = "GETFAMILY" ,
  231. },
  232. {
  233. .c_id = CTRL_CMD_NEWOPS,
  234. .c_name = "NEWOPS" ,
  235. },
  236. {
  237. .c_id = CTRL_CMD_DELOPS,
  238. .c_name = "DELOPS" ,
  239. },
  240. };
  241. static struct genl_ops genl_ops = {
  242. .o_cmds = genl_cmds,
  243. .o_ncmds = ARRAY_SIZE(genl_cmds),
  244. };
  245. /** @cond SKIP */
  246. extern struct nl_object_ops genl_family_ops;
  247. /** @endcond */
  248. static struct nl_cache_ops genl_ctrl_ops = {
  249. .co_name = "genl/family",
  250. .co_hdrsize = GENL_HDRSIZE(0),
  251. .co_msgtypes = GENL_FAMILY(GENL_ID_CTRL, "nlctrl"),
  252. .co_genl = &genl_ops,
  253. .co_protocol = NETLINK_GENERIC,
  254. .co_request_update = ctrl_request_update,
  255. .co_obj_ops = &genl_family_ops,
  256. };
  257. static void __init ctrl_init(void)
  258. {
  259. genl_register(&genl_ctrl_ops);
  260. }
  261. static void __exit ctrl_exit(void)
  262. {
  263. genl_unregister(&genl_ctrl_ops);
  264. }
  265. /** @} */