genl_mngt.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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-2008 Thomas Graf <[email protected]>
  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 = -NLE_MSGTYPE_NOSUPPORT;
  104. goto errout;
  105. found:
  106. if (cmd->c_msg_parser == NULL)
  107. err = -NLE_OPNOTSUPP;
  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. #ifdef disabled
  127. char *genl_op2name(int family, int op, char *buf, size_t len)
  128. {
  129. struct genl_ops *ops;
  130. int i;
  131. nl_list_for_each_entry(ops, &genl_ops_list, o_list) {
  132. if (ops->o_family == family) {
  133. for (i = 0; i < ops->o_ncmds; i++) {
  134. struct genl_cmd *cmd;
  135. cmd = &ops->o_cmds[i];
  136. if (cmd->c_id == op) {
  137. strncpy(buf, cmd->c_name, len - 1);
  138. return buf;
  139. }
  140. }
  141. }
  142. }
  143. strncpy(buf, "unknown", len - 1);
  144. return NULL;
  145. }
  146. #endif
  147. /**
  148. * @name Register/Unregister
  149. * @{
  150. */
  151. /**
  152. * Register generic netlink operations
  153. * @arg ops cache operations
  154. */
  155. int genl_register(struct nl_cache_ops *ops)
  156. {
  157. int err;
  158. if (ops->co_protocol != NETLINK_GENERIC) {
  159. err = -NLE_PROTO_MISMATCH;
  160. goto errout;
  161. }
  162. if (ops->co_hdrsize < GENL_HDRSIZE(0)) {
  163. err = -NLE_INVAL;
  164. goto errout;
  165. }
  166. if (ops->co_genl == NULL) {
  167. err = -NLE_INVAL;
  168. goto errout;
  169. }
  170. ops->co_genl->o_cache_ops = ops;
  171. ops->co_genl->o_name = ops->co_msgtypes[0].mt_name;
  172. ops->co_genl->o_family = ops->co_msgtypes[0].mt_id;
  173. ops->co_msg_parser = genl_msg_parser;
  174. /* FIXME: check for dup */
  175. nl_list_add_tail(&ops->co_genl->o_list, &genl_ops_list);
  176. err = nl_cache_mngt_register(ops);
  177. errout:
  178. return err;
  179. }
  180. /**
  181. * Unregister generic netlink operations
  182. * @arg ops cache operations
  183. */
  184. void genl_unregister(struct nl_cache_ops *ops)
  185. {
  186. nl_cache_mngt_unregister(ops);
  187. nl_list_del(&ops->co_genl->o_list);
  188. }
  189. /** @} */
  190. /**
  191. * @name Resolving ID/Name
  192. * @{
  193. */
  194. #ifdef disabled
  195. static int __genl_ops_resolve(struct nl_cache *ctrl, struct genl_ops *ops)
  196. {
  197. struct genl_family *family;
  198. family = genl_ctrl_search_by_name(ctrl, ops->o_name);
  199. if (family != NULL) {
  200. ops->o_id = genl_family_get_id(family);
  201. genl_family_put(family);
  202. return 0;
  203. }
  204. return -NLE_OBJ_NOTFOUND;
  205. }
  206. int genl_ops_resolve(struct nl_sock *sk, struct genl_ops *ops)
  207. {
  208. struct nl_cache *ctrl;
  209. int err;
  210. if ((err = genl_ctrl_alloc_cache(sk, &ctrl)) < 0)
  211. goto errout;
  212. err = __genl_ops_resolve(ctrl, ops);
  213. nl_cache_free(ctrl);
  214. errout:
  215. return err;
  216. }
  217. int genl_mngt_resolve(struct nl_sock *sk)
  218. {
  219. struct nl_cache *ctrl;
  220. struct genl_ops *ops;
  221. int err = 0;
  222. if ((err = genl_ctrl_alloc_cache(sk, &ctrl)) < 0)
  223. goto errout;
  224. nl_list_for_each_entry(ops, &genl_ops_list, o_list) {
  225. err = __genl_ops_resolve(ctrl, ops);
  226. }
  227. nl_cache_free(ctrl);
  228. errout:
  229. return err;
  230. }
  231. #endif
  232. /** @} */
  233. /** @} */