mngt.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * lib/genl/mngt.c Generic Netlink Management
  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
  13. * @defgroup genl_mngt Management
  14. *
  15. * @par 1) Registering a generic netlink module
  16. * @code
  17. * #include <netlink/genl/mngt.h>
  18. *
  19. * // First step is to define all the commands being used in
  20. * // particular generic netlink family. The ID and name are
  21. * // mandatory to be filled out. A callback function and
  22. * // most the attribute policy that comes with it must be
  23. * // defined for commands expected to be issued towards
  24. * // userspace.
  25. * static struct genl_cmd foo_cmds[] = {
  26. * {
  27. * .c_id = FOO_CMD_NEW,
  28. * .c_name = "NEWFOO" ,
  29. * .c_maxattr = FOO_ATTR_MAX,
  30. * .c_attr_policy = foo_policy,
  31. * .c_msg_parser = foo_msg_parser,
  32. * },
  33. * {
  34. * .c_id = FOO_CMD_DEL,
  35. * .c_name = "DELFOO" ,
  36. * },
  37. * };
  38. *
  39. * // The list of commands must then be integrated into a
  40. * // struct genl_ops serving as handle for this particular
  41. * // family.
  42. * static struct genl_ops my_genl_ops = {
  43. * .o_cmds = foo_cmds,
  44. * .o_ncmds = ARRAY_SIZE(foo_cmds),
  45. * };
  46. *
  47. * // Using the above struct genl_ops an arbitary number of
  48. * // cache handles can be associated to it.
  49. * //
  50. * // The macro GENL_HDRSIZE() must be used to specify the
  51. * // length of the header to automatically take headers on
  52. * // generic layers into account.
  53. * //
  54. * // The macro GENL_FAMILY() is used to represent the generic
  55. * // netlink family id.
  56. * static struct nl_cache_ops genl_foo_ops = {
  57. * .co_name = "genl/foo",
  58. * .co_hdrsize = GENL_HDRSIZE(sizeof(struct my_hdr)),
  59. * .co_msgtypes = GENL_FAMILY(GENL_ID_GENERATE, "foo"),
  60. * .co_genl = &my_genl_ops,
  61. * .co_protocol = NETLINK_GENERIC,
  62. * .co_request_update = foo_request_update,
  63. * .co_obj_ops = &genl_foo_ops,
  64. * };
  65. *
  66. * // Finally each cache handle for a generic netlink family
  67. * // must be registered using genl_register().
  68. * static void __init foo_init(void)
  69. * {
  70. * genl_register(&genl_foo_ops);
  71. * }
  72. *
  73. * // ... respectively unregsted again.
  74. * static void __exit foo_exit(void)
  75. * {
  76. * genl_unregister(&genl_foo_ops);
  77. * }
  78. * @endcode
  79. * @{
  80. */
  81. #include <netlink-generic.h>
  82. #include <netlink/netlink.h>
  83. #include <netlink/genl/genl.h>
  84. #include <netlink/genl/mngt.h>
  85. #include <netlink/genl/family.h>
  86. #include <netlink/genl/ctrl.h>
  87. #include <netlink/utils.h>
  88. static NL_LIST_HEAD(genl_ops_list);
  89. static int genl_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
  90. struct nlmsghdr *nlh, struct nl_parser_param *pp)
  91. {
  92. int i, err;
  93. struct genlmsghdr *ghdr;
  94. struct genl_cmd *cmd;
  95. ghdr = nlmsg_data(nlh);
  96. if (ops->co_genl == NULL)
  97. BUG();
  98. for (i = 0; i < ops->co_genl->o_ncmds; i++) {
  99. cmd = &ops->co_genl->o_cmds[i];
  100. if (cmd->c_id == ghdr->cmd)
  101. goto found;
  102. }
  103. err = nl_errno(ENOENT);
  104. goto errout;
  105. found:
  106. if (cmd->c_msg_parser == NULL)
  107. err = nl_error(EOPNOTSUPP, "No message parser found.");
  108. else {
  109. struct nlattr *tb[cmd->c_maxattr + 1];
  110. struct genl_info info = {
  111. .who = who,
  112. .nlh = nlh,
  113. .genlhdr = ghdr,
  114. .userhdr = genlmsg_data(ghdr),
  115. .attrs = tb,
  116. };
  117. err = nlmsg_parse(nlh, ops->co_hdrsize, tb, cmd->c_maxattr,
  118. cmd->c_attr_policy);
  119. if (err < 0)
  120. goto errout;
  121. err = cmd->c_msg_parser(ops, cmd, &info, pp);
  122. }
  123. errout:
  124. return err;
  125. }
  126. char *genl_op2name(int family, int op, char *buf, size_t len)
  127. {
  128. struct genl_ops *ops;
  129. int i;
  130. nl_list_for_each_entry(ops, &genl_ops_list, o_list) {
  131. if (ops->o_family == family) {
  132. for (i = 0; i < ops->o_ncmds; i++) {
  133. struct genl_cmd *cmd;
  134. cmd = &ops->o_cmds[i];
  135. if (cmd->c_id == op) {
  136. strncpy(buf, cmd->c_name, len - 1);
  137. return buf;
  138. }
  139. }
  140. }
  141. }
  142. strncpy(buf, "unknown", len - 1);
  143. return NULL;
  144. }
  145. /**
  146. * @name Register/Unregister
  147. * @{
  148. */
  149. /**
  150. * Register generic netlink operations
  151. * @arg ops cache operations
  152. */
  153. int genl_register(struct nl_cache_ops *ops)
  154. {
  155. int err;
  156. if (ops->co_protocol != NETLINK_GENERIC) {
  157. err = nl_error(EINVAL, "cache operations not for protocol " \
  158. "NETLINK_GENERIC (protocol=%s)",
  159. ops->co_protocol);
  160. goto errout;
  161. }
  162. if (ops->co_hdrsize < GENL_HDRSIZE(0)) {
  163. err = nl_error(EINVAL, "co_hdrsize too short, probably " \
  164. "not including genlmsghdr, minsize=%d",
  165. GENL_HDRSIZE(0));
  166. goto errout;
  167. }
  168. if (ops->co_genl == NULL) {
  169. err = nl_error(EINVAL, "co_genl is NULL, must provide " \
  170. "valid genl operations");
  171. goto errout;
  172. }
  173. ops->co_genl->o_cache_ops = ops;
  174. ops->co_genl->o_name = ops->co_msgtypes[0].mt_name;
  175. ops->co_genl->o_family = ops->co_msgtypes[0].mt_id;
  176. ops->co_msg_parser = genl_msg_parser;
  177. /* FIXME: check for dup */
  178. nl_list_add_tail(&ops->co_genl->o_list, &genl_ops_list);
  179. err = nl_cache_mngt_register(ops);
  180. errout:
  181. return err;
  182. }
  183. /**
  184. * Unregister generic netlink operations
  185. * @arg ops cache operations
  186. */
  187. void genl_unregister(struct nl_cache_ops *ops)
  188. {
  189. nl_cache_mngt_unregister(ops);
  190. nl_list_del(&ops->co_genl->o_list);
  191. }
  192. /** @} */
  193. /**
  194. * @name Resolving ID/Name
  195. * @{
  196. */
  197. static int __genl_ops_resolve(struct nl_cache *ctrl, struct genl_ops *ops)
  198. {
  199. struct genl_family *family;
  200. family = genl_ctrl_search_by_name(ctrl, ops->o_name);
  201. if (family != NULL) {
  202. ops->o_id = genl_family_get_id(family);
  203. genl_family_put(family);
  204. return 0;
  205. }
  206. return nl_error(ENOENT, "Unable to find generic netlink family \"%s\"",
  207. ops->o_name);
  208. }
  209. int genl_ops_resolve(struct nl_handle *handle, struct genl_ops *ops)
  210. {
  211. struct nl_cache *ctrl;
  212. int err;
  213. ctrl = genl_ctrl_alloc_cache(handle);
  214. if (ctrl == NULL) {
  215. err = nl_get_errno();
  216. goto errout;
  217. }
  218. err = __genl_ops_resolve(ctrl, ops);
  219. nl_cache_free(ctrl);
  220. errout:
  221. return err;
  222. }
  223. int genl_mngt_resolve(struct nl_handle *handle)
  224. {
  225. struct nl_cache *ctrl;
  226. struct genl_ops *ops;
  227. int err = 0;
  228. ctrl = genl_ctrl_alloc_cache(handle);
  229. if (ctrl == NULL) {
  230. err = nl_get_errno();
  231. goto errout;
  232. }
  233. nl_list_for_each_entry(ops, &genl_ops_list, o_list) {
  234. err = __genl_ops_resolve(ctrl, ops);
  235. }
  236. nl_cache_free(ctrl);
  237. errout:
  238. return err;
  239. }
  240. /** @} */
  241. /** @} */