genl_mngt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <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 = -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. /**
  127. * @name Register/Unregister
  128. * @{
  129. */
  130. /**
  131. * Register generic netlink operations
  132. * @arg ops cache operations
  133. */
  134. int genl_register(struct nl_cache_ops *ops)
  135. {
  136. int err;
  137. if (ops->co_protocol != NETLINK_GENERIC) {
  138. err = -NLE_PROTO_MISMATCH;
  139. goto errout;
  140. }
  141. if ((size_t) ops->co_hdrsize < GENL_HDRSIZE(0)) {
  142. err = -NLE_INVAL;
  143. goto errout;
  144. }
  145. if (ops->co_genl == NULL) {
  146. err = -NLE_INVAL;
  147. goto errout;
  148. }
  149. ops->co_genl->o_cache_ops = ops;
  150. ops->co_genl->o_name = ops->co_msgtypes[0].mt_name;
  151. ops->co_genl->o_family = ops->co_msgtypes[0].mt_id;
  152. ops->co_msg_parser = genl_msg_parser;
  153. /* FIXME: check for dup */
  154. nl_list_add_tail(&ops->co_genl->o_list, &genl_ops_list);
  155. err = nl_cache_mngt_register(ops);
  156. errout:
  157. return err;
  158. }
  159. /**
  160. * Unregister generic netlink operations
  161. * @arg ops cache operations
  162. */
  163. void genl_unregister(struct nl_cache_ops *ops)
  164. {
  165. nl_cache_mngt_unregister(ops);
  166. nl_list_del(&ops->co_genl->o_list);
  167. }
  168. /** @} */
  169. /** @} */