genl_ctrl.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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-2008 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_sock *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. [CTRL_ATTR_MCAST_GROUPS] = { .type = NLA_NESTED },
  43. };
  44. static struct nla_policy family_op_policy[CTRL_ATTR_OP_MAX+1] = {
  45. [CTRL_ATTR_OP_ID] = { .type = NLA_U32 },
  46. [CTRL_ATTR_OP_FLAGS] = { .type = NLA_U32 },
  47. };
  48. static struct nla_policy family_grp_policy[CTRL_ATTR_MCAST_GRP_MAX+1] = {
  49. [CTRL_ATTR_MCAST_GRP_NAME] = { .type = NLA_STRING },
  50. [CTRL_ATTR_MCAST_GRP_ID] = { .type = NLA_U32 },
  51. };
  52. static int ctrl_msg_parser(struct nl_cache_ops *ops, struct genl_cmd *cmd,
  53. struct genl_info *info, void *arg)
  54. {
  55. struct genl_family *family;
  56. struct nl_parser_param *pp = arg;
  57. int err;
  58. family = genl_family_alloc();
  59. if (family == NULL) {
  60. err = -NLE_NOMEM;
  61. goto errout;
  62. }
  63. if (info->attrs[CTRL_ATTR_FAMILY_NAME] == NULL) {
  64. err = -NLE_MISSING_ATTR;
  65. goto errout;
  66. }
  67. if (info->attrs[CTRL_ATTR_FAMILY_ID] == NULL) {
  68. err = -NLE_MISSING_ATTR;
  69. goto errout;
  70. }
  71. family->ce_msgtype = info->nlh->nlmsg_type;
  72. genl_family_set_id(family,
  73. nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]));
  74. genl_family_set_name(family,
  75. nla_get_string(info->attrs[CTRL_ATTR_FAMILY_NAME]));
  76. if (info->attrs[CTRL_ATTR_VERSION]) {
  77. uint32_t version = nla_get_u32(info->attrs[CTRL_ATTR_VERSION]);
  78. genl_family_set_version(family, version);
  79. }
  80. if (info->attrs[CTRL_ATTR_HDRSIZE]) {
  81. uint32_t hdrsize = nla_get_u32(info->attrs[CTRL_ATTR_HDRSIZE]);
  82. genl_family_set_hdrsize(family, hdrsize);
  83. }
  84. if (info->attrs[CTRL_ATTR_MAXATTR]) {
  85. uint32_t maxattr = nla_get_u32(info->attrs[CTRL_ATTR_MAXATTR]);
  86. genl_family_set_maxattr(family, maxattr);
  87. }
  88. if (info->attrs[CTRL_ATTR_OPS]) {
  89. struct nlattr *nla, *nla_ops;
  90. int remaining;
  91. nla_ops = info->attrs[CTRL_ATTR_OPS];
  92. nla_for_each_nested(nla, nla_ops, remaining) {
  93. struct nlattr *tb[CTRL_ATTR_OP_MAX+1];
  94. int flags = 0, id;
  95. err = nla_parse_nested(tb, CTRL_ATTR_OP_MAX, nla,
  96. family_op_policy);
  97. if (err < 0)
  98. goto errout;
  99. if (tb[CTRL_ATTR_OP_ID] == NULL) {
  100. err = -NLE_MISSING_ATTR;
  101. goto errout;
  102. }
  103. id = nla_get_u32(tb[CTRL_ATTR_OP_ID]);
  104. if (tb[CTRL_ATTR_OP_FLAGS])
  105. flags = nla_get_u32(tb[CTRL_ATTR_OP_FLAGS]);
  106. err = genl_family_add_op(family, id, flags);
  107. if (err < 0)
  108. goto errout;
  109. }
  110. }
  111. if (info->attrs[CTRL_ATTR_MCAST_GROUPS]) {
  112. struct nlattr *nla, *nla_grps;
  113. int remaining;
  114. nla_grps = info->attrs[CTRL_ATTR_MCAST_GROUPS];
  115. nla_for_each_nested(nla, nla_grps, remaining) {
  116. struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX+1];
  117. int id;
  118. const char * name;
  119. err = nla_parse_nested(tb, CTRL_ATTR_MCAST_GRP_MAX, nla,
  120. family_grp_policy);
  121. if (err < 0)
  122. goto errout;
  123. if (tb[CTRL_ATTR_MCAST_GRP_ID] == NULL) {
  124. err = -NLE_MISSING_ATTR;
  125. goto errout;
  126. }
  127. id = nla_get_u32(tb[CTRL_ATTR_MCAST_GRP_ID]);
  128. if (tb[CTRL_ATTR_MCAST_GRP_NAME] == NULL) {
  129. err = -NLE_MISSING_ATTR;
  130. goto errout;
  131. }
  132. name = nla_get_string(tb[CTRL_ATTR_MCAST_GRP_NAME]);
  133. err = genl_family_add_grp(family, id, name);
  134. if (err < 0)
  135. goto errout;
  136. }
  137. }
  138. err = pp->pp_cb((struct nl_object *) family, pp);
  139. errout:
  140. genl_family_put(family);
  141. return err;
  142. }
  143. /**
  144. * @name Cache Management
  145. * @{
  146. */
  147. int genl_ctrl_alloc_cache(struct nl_sock *sock, struct nl_cache **result)
  148. {
  149. return nl_cache_alloc_and_fill(&genl_ctrl_ops, sock, result);
  150. }
  151. /**
  152. * Look up generic netlink family by id in the provided cache.
  153. * @arg cache Generic netlink family cache.
  154. * @arg id Family identifier.
  155. *
  156. * Searches through the cache looking for a registered family
  157. * matching the specified identifier. The caller will own a
  158. * reference on the returned object which needs to be given
  159. * back after usage using genl_family_put().
  160. *
  161. * @return Generic netlink family object or NULL if no match was found.
  162. */
  163. struct genl_family *genl_ctrl_search(struct nl_cache *cache, int id)
  164. {
  165. struct genl_family *fam;
  166. if (cache->c_ops != &genl_ctrl_ops)
  167. BUG();
  168. nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
  169. if (fam->gf_id == id) {
  170. nl_object_get((struct nl_object *) fam);
  171. return fam;
  172. }
  173. }
  174. return NULL;
  175. }
  176. /**
  177. * @name Resolver
  178. * @{
  179. */
  180. /**
  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.
  184. *
  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().
  189. *
  190. * @return Generic netlink family object or NULL if no match was found.
  191. */
  192. struct genl_family *genl_ctrl_search_by_name(struct nl_cache *cache,
  193. const char *name)
  194. {
  195. struct genl_family *fam;
  196. if (cache->c_ops != &genl_ctrl_ops)
  197. BUG();
  198. nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
  199. if (!strcmp(name, fam->gf_name)) {
  200. nl_object_get((struct nl_object *) fam);
  201. return fam;
  202. }
  203. }
  204. return NULL;
  205. }
  206. /** @} */
  207. /**
  208. * Resolve generic netlink family name to its identifier
  209. * @arg sk Netlink socket.
  210. * @arg name Name of generic netlink family
  211. *
  212. * Resolves the generic netlink family name to its identifer and returns
  213. * it.
  214. *
  215. * @return A positive identifier or a negative error code.
  216. */
  217. int genl_ctrl_resolve(struct nl_sock *sk, const char *name)
  218. {
  219. struct nl_cache *cache;
  220. struct genl_family *family;
  221. int err;
  222. if ((err = genl_ctrl_alloc_cache(sk, &cache)) < 0)
  223. return err;
  224. family = genl_ctrl_search_by_name(cache, name);
  225. if (family == NULL) {
  226. err = -NLE_OBJ_NOTFOUND;
  227. goto errout;
  228. }
  229. err = genl_family_get_id(family);
  230. genl_family_put(family);
  231. errout:
  232. nl_cache_free(cache);
  233. return err;
  234. }
  235. static int genl_ctrl_grp_by_name(const struct genl_family *family,
  236. const char *grp_name)
  237. {
  238. struct genl_family_grp *grp;
  239. nl_list_for_each_entry(grp, &family->gf_mc_grps, list) {
  240. if (!strcmp(grp->name, grp_name)) {
  241. return grp->id;
  242. }
  243. }
  244. return -NLE_OBJ_NOTFOUND;
  245. }
  246. int genl_ctrl_resolve_grp(struct nl_sock *sk, const char *family_name,
  247. const char *grp_name)
  248. {
  249. struct nl_cache *cache;
  250. struct genl_family *family;
  251. int err;
  252. if ((err = genl_ctrl_alloc_cache(sk, &cache)) < 0)
  253. return err;
  254. family = genl_ctrl_search_by_name(cache, family_name);
  255. if (family == NULL) {
  256. err = -NLE_OBJ_NOTFOUND;
  257. goto errout;
  258. }
  259. err = genl_ctrl_grp_by_name(family, grp_name);
  260. genl_family_put(family);
  261. errout:
  262. nl_cache_free(cache);
  263. return err;
  264. }
  265. /** @} */
  266. static struct genl_cmd genl_cmds[] = {
  267. {
  268. .c_id = CTRL_CMD_NEWFAMILY,
  269. .c_name = "NEWFAMILY" ,
  270. .c_maxattr = CTRL_ATTR_MAX,
  271. .c_attr_policy = ctrl_policy,
  272. .c_msg_parser = ctrl_msg_parser,
  273. },
  274. {
  275. .c_id = CTRL_CMD_DELFAMILY,
  276. .c_name = "DELFAMILY" ,
  277. },
  278. {
  279. .c_id = CTRL_CMD_GETFAMILY,
  280. .c_name = "GETFAMILY" ,
  281. },
  282. {
  283. .c_id = CTRL_CMD_NEWOPS,
  284. .c_name = "NEWOPS" ,
  285. },
  286. {
  287. .c_id = CTRL_CMD_DELOPS,
  288. .c_name = "DELOPS" ,
  289. },
  290. };
  291. static struct genl_ops genl_ops = {
  292. .o_cmds = genl_cmds,
  293. .o_ncmds = ARRAY_SIZE(genl_cmds),
  294. };
  295. /** @cond SKIP */
  296. extern struct nl_object_ops genl_family_ops;
  297. /** @endcond */
  298. static struct nl_cache_ops genl_ctrl_ops = {
  299. .co_name = "genl/family",
  300. .co_hdrsize = GENL_HDRSIZE(0),
  301. .co_msgtypes = GENL_FAMILY(GENL_ID_CTRL, "nlctrl"),
  302. .co_genl = &genl_ops,
  303. .co_protocol = NETLINK_GENERIC,
  304. .co_request_update = ctrl_request_update,
  305. .co_obj_ops = &genl_family_ops,
  306. };
  307. static void __init ctrl_init(void)
  308. {
  309. genl_register(&genl_ctrl_ops);
  310. }
  311. static void __exit ctrl_exit(void)
  312. {
  313. genl_unregister(&genl_ctrl_ops);
  314. }
  315. /** @} */