genl_magic_struct.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef GENL_MAGIC_STRUCT_H
  3. #define GENL_MAGIC_STRUCT_H
  4. #ifndef GENL_MAGIC_FAMILY
  5. # error "you need to define GENL_MAGIC_FAMILY before inclusion"
  6. #endif
  7. #ifndef GENL_MAGIC_VERSION
  8. # error "you need to define GENL_MAGIC_VERSION before inclusion"
  9. #endif
  10. #ifndef GENL_MAGIC_INCLUDE_FILE
  11. # error "you need to define GENL_MAGIC_INCLUDE_FILE before inclusion"
  12. #endif
  13. #include <linux/genetlink.h>
  14. #include <linux/types.h>
  15. #define CONCAT__(a,b) a ## b
  16. #define CONCAT_(a,b) CONCAT__(a,b)
  17. extern int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void);
  18. extern void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void);
  19. /*
  20. * Extension of genl attribute validation policies {{{2
  21. */
  22. /*
  23. * @DRBD_GENLA_F_MANDATORY: By default, netlink ignores attributes it does not
  24. * know about. This flag can be set in nlattr->nla_type to indicate that this
  25. * attribute must not be ignored.
  26. *
  27. * We check and remove this flag in drbd_nla_check_mandatory() before
  28. * validating the attribute types and lengths via nla_parse_nested().
  29. */
  30. #define DRBD_GENLA_F_MANDATORY (1 << 14)
  31. /*
  32. * Flags specific to drbd and not visible at the netlink layer, used in
  33. * <struct>_from_attrs and <struct>_to_skb:
  34. *
  35. * @DRBD_F_REQUIRED: Attribute is required; a request without this attribute is
  36. * invalid.
  37. *
  38. * @DRBD_F_SENSITIVE: Attribute includes sensitive information and must not be
  39. * included in unpriviledged get requests or broadcasts.
  40. *
  41. * @DRBD_F_INVARIANT: Attribute is set when an object is initially created, but
  42. * cannot subsequently be changed.
  43. */
  44. #define DRBD_F_REQUIRED (1 << 0)
  45. #define DRBD_F_SENSITIVE (1 << 1)
  46. #define DRBD_F_INVARIANT (1 << 2)
  47. #define __nla_type(x) ((__u16)((x) & NLA_TYPE_MASK & ~DRBD_GENLA_F_MANDATORY))
  48. /* }}}1
  49. * MAGIC
  50. * multi-include macro expansion magic starts here
  51. */
  52. /* MAGIC helpers {{{2 */
  53. static inline int nla_put_u64_0pad(struct sk_buff *skb, int attrtype, u64 value)
  54. {
  55. return nla_put_64bit(skb, attrtype, sizeof(u64), &value, 0);
  56. }
  57. /* possible field types */
  58. #define __flg_field(attr_nr, attr_flag, name) \
  59. __field(attr_nr, attr_flag, name, NLA_U8, char, \
  60. nla_get_u8, nla_put_u8, false)
  61. #define __u8_field(attr_nr, attr_flag, name) \
  62. __field(attr_nr, attr_flag, name, NLA_U8, unsigned char, \
  63. nla_get_u8, nla_put_u8, false)
  64. #define __u16_field(attr_nr, attr_flag, name) \
  65. __field(attr_nr, attr_flag, name, NLA_U16, __u16, \
  66. nla_get_u16, nla_put_u16, false)
  67. #define __u32_field(attr_nr, attr_flag, name) \
  68. __field(attr_nr, attr_flag, name, NLA_U32, __u32, \
  69. nla_get_u32, nla_put_u32, false)
  70. #define __s32_field(attr_nr, attr_flag, name) \
  71. __field(attr_nr, attr_flag, name, NLA_U32, __s32, \
  72. nla_get_u32, nla_put_u32, true)
  73. #define __u64_field(attr_nr, attr_flag, name) \
  74. __field(attr_nr, attr_flag, name, NLA_U64, __u64, \
  75. nla_get_u64, nla_put_u64_0pad, false)
  76. #define __str_field(attr_nr, attr_flag, name, maxlen) \
  77. __array(attr_nr, attr_flag, name, NLA_NUL_STRING, char, maxlen, \
  78. nla_strlcpy, nla_put, false)
  79. #define __bin_field(attr_nr, attr_flag, name, maxlen) \
  80. __array(attr_nr, attr_flag, name, NLA_BINARY, char, maxlen, \
  81. nla_memcpy, nla_put, false)
  82. /* fields with default values */
  83. #define __flg_field_def(attr_nr, attr_flag, name, default) \
  84. __flg_field(attr_nr, attr_flag, name)
  85. #define __u32_field_def(attr_nr, attr_flag, name, default) \
  86. __u32_field(attr_nr, attr_flag, name)
  87. #define __s32_field_def(attr_nr, attr_flag, name, default) \
  88. __s32_field(attr_nr, attr_flag, name)
  89. #define __str_field_def(attr_nr, attr_flag, name, maxlen) \
  90. __str_field(attr_nr, attr_flag, name, maxlen)
  91. #define GENL_op_init(args...) args
  92. #define GENL_doit(handler) \
  93. .doit = handler, \
  94. .flags = GENL_ADMIN_PERM,
  95. #define GENL_dumpit(handler) \
  96. .dumpit = handler, \
  97. .flags = GENL_ADMIN_PERM,
  98. /* }}}1
  99. * Magic: define the enum symbols for genl_ops
  100. * Magic: define the enum symbols for top level attributes
  101. * Magic: define the enum symbols for nested attributes
  102. * {{{2
  103. */
  104. #undef GENL_struct
  105. #define GENL_struct(tag_name, tag_number, s_name, s_fields)
  106. #undef GENL_mc_group
  107. #define GENL_mc_group(group)
  108. #undef GENL_notification
  109. #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
  110. op_name = op_num,
  111. #undef GENL_op
  112. #define GENL_op(op_name, op_num, handler, tla_list) \
  113. op_name = op_num,
  114. enum {
  115. #include GENL_MAGIC_INCLUDE_FILE
  116. };
  117. #undef GENL_notification
  118. #define GENL_notification(op_name, op_num, mcast_group, tla_list)
  119. #undef GENL_op
  120. #define GENL_op(op_name, op_num, handler, attr_list)
  121. #undef GENL_struct
  122. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  123. tag_name = tag_number,
  124. enum {
  125. #include GENL_MAGIC_INCLUDE_FILE
  126. };
  127. #undef GENL_struct
  128. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  129. enum { \
  130. s_fields \
  131. };
  132. #undef __field
  133. #define __field(attr_nr, attr_flag, name, nla_type, type, \
  134. __get, __put, __is_signed) \
  135. T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
  136. #undef __array
  137. #define __array(attr_nr, attr_flag, name, nla_type, type, \
  138. maxlen, __get, __put, __is_signed) \
  139. T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
  140. #include GENL_MAGIC_INCLUDE_FILE
  141. /* }}}1
  142. * Magic: compile time assert unique numbers for operations
  143. * Magic: -"- unique numbers for top level attributes
  144. * Magic: -"- unique numbers for nested attributes
  145. * {{{2
  146. */
  147. #undef GENL_struct
  148. #define GENL_struct(tag_name, tag_number, s_name, s_fields)
  149. #undef GENL_op
  150. #define GENL_op(op_name, op_num, handler, attr_list) \
  151. case op_name:
  152. #undef GENL_notification
  153. #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
  154. case op_name:
  155. static inline void ct_assert_unique_operations(void)
  156. {
  157. switch (0) {
  158. #include GENL_MAGIC_INCLUDE_FILE
  159. case 0:
  160. ;
  161. }
  162. }
  163. #undef GENL_op
  164. #define GENL_op(op_name, op_num, handler, attr_list)
  165. #undef GENL_notification
  166. #define GENL_notification(op_name, op_num, mcast_group, tla_list)
  167. #undef GENL_struct
  168. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  169. case tag_number:
  170. static inline void ct_assert_unique_top_level_attributes(void)
  171. {
  172. switch (0) {
  173. #include GENL_MAGIC_INCLUDE_FILE
  174. case 0:
  175. ;
  176. }
  177. }
  178. #undef GENL_struct
  179. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  180. static inline void ct_assert_unique_ ## s_name ## _attributes(void) \
  181. { \
  182. switch (0) { \
  183. s_fields \
  184. case 0: \
  185. ; \
  186. } \
  187. }
  188. #undef __field
  189. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
  190. __is_signed) \
  191. case attr_nr:
  192. #undef __array
  193. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
  194. __get, __put, __is_signed) \
  195. case attr_nr:
  196. #include GENL_MAGIC_INCLUDE_FILE
  197. /* }}}1
  198. * Magic: declare structs
  199. * struct <name> {
  200. * fields
  201. * };
  202. * {{{2
  203. */
  204. #undef GENL_struct
  205. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  206. struct s_name { s_fields };
  207. #undef __field
  208. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
  209. __is_signed) \
  210. type name;
  211. #undef __array
  212. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
  213. __get, __put, __is_signed) \
  214. type name[maxlen]; \
  215. __u32 name ## _len;
  216. #include GENL_MAGIC_INCLUDE_FILE
  217. #undef GENL_struct
  218. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  219. enum { \
  220. s_fields \
  221. };
  222. #undef __field
  223. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
  224. is_signed) \
  225. F_ ## name ## _IS_SIGNED = is_signed,
  226. #undef __array
  227. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
  228. __get, __put, is_signed) \
  229. F_ ## name ## _IS_SIGNED = is_signed,
  230. #include GENL_MAGIC_INCLUDE_FILE
  231. /* }}}1 */
  232. #endif /* GENL_MAGIC_STRUCT_H */
  233. /* vim: set foldmethod=marker nofoldenable : */