genl_magic_struct.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #ifndef GENL_MAGIC_STRUCT_H
  2. #define GENL_MAGIC_STRUCT_H
  3. #ifndef GENL_MAGIC_FAMILY
  4. # error "you need to define GENL_MAGIC_FAMILY before inclusion"
  5. #endif
  6. #ifndef GENL_MAGIC_VERSION
  7. # error "you need to define GENL_MAGIC_VERSION before inclusion"
  8. #endif
  9. #ifndef GENL_MAGIC_INCLUDE_FILE
  10. # error "you need to define GENL_MAGIC_INCLUDE_FILE before inclusion"
  11. #endif
  12. #include <linux/genetlink.h>
  13. #include <linux/types.h>
  14. #define CONCAT__(a,b) a ## b
  15. #define CONCAT_(a,b) CONCAT__(a,b)
  16. extern int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void);
  17. extern void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void);
  18. /*
  19. * Extension of genl attribute validation policies {{{2
  20. */
  21. /**
  22. * GENLA_F_FLAGS - policy type flags to ease compatible ABI evolvement
  23. *
  24. * @GENLA_F_REQUIRED: attribute has to be present, or message is considered invalid.
  25. * Adding new REQUIRED attributes breaks ABI compatibility, so don't do that.
  26. *
  27. * @GENLA_F_MANDATORY: if present, receiver _must_ understand it.
  28. * Without this, unknown attributes (> maxtype) are _silently_ ignored
  29. * by validate_nla().
  30. *
  31. * To be used for API extensions, so older kernel can reject requests for not
  32. * yet implemented features, if newer userland tries to use them even though
  33. * the genl_family version clearly indicates they are not available.
  34. *
  35. * @GENLA_F_MAY_IGNORE: To clearly document the fact, for good measure.
  36. * To be used for API extensions for things that have sane defaults,
  37. * so newer userland can still talk to older kernel, knowing it will
  38. * silently ignore these attributes if not yet known.
  39. *
  40. * NOTE: These flags overload
  41. * NLA_F_NESTED (1 << 15)
  42. * NLA_F_NET_BYTEORDER (1 << 14)
  43. * from linux/netlink.h, which are not useful for validate_nla():
  44. * NET_BYTEORDER is not used anywhere, and NESTED would be specified by setting
  45. * .type = NLA_NESTED in the appropriate policy.
  46. *
  47. * See also: nla_type()
  48. */
  49. enum {
  50. GENLA_F_MAY_IGNORE = 0,
  51. GENLA_F_MANDATORY = 1 << 14,
  52. GENLA_F_REQUIRED = 1 << 15,
  53. /* Below will not be present in the __u16 .nla_type, but can be
  54. * triggered on in <struct>_to_skb resp. <struct>_from_attrs */
  55. /* To exclude "sensitive" information from broadcasts, or on
  56. * unpriviledged get requests. This is useful because genetlink
  57. * multicast groups can be listened in on by anyone. */
  58. GENLA_F_SENSITIVE = 1 << 16,
  59. /* INVARIAN options cannot be changed at runtime.
  60. * Useful to share an attribute policy and struct definition,
  61. * between some "create" and "change" commands,
  62. * but disallow certain fields to be changed online.
  63. */
  64. GENLA_F_INVARIANT = 1 << 17,
  65. };
  66. #define __nla_type(x) ((__u16)((__u16)(x) & (__u16)NLA_TYPE_MASK))
  67. /* }}}1
  68. * MAGIC
  69. * multi-include macro expansion magic starts here
  70. */
  71. /* MAGIC helpers {{{2 */
  72. /* possible field types */
  73. #define __flg_field(attr_nr, attr_flag, name) \
  74. __field(attr_nr, attr_flag, name, NLA_U8, char, \
  75. nla_get_u8, NLA_PUT_U8)
  76. #define __u8_field(attr_nr, attr_flag, name) \
  77. __field(attr_nr, attr_flag, name, NLA_U8, unsigned char, \
  78. nla_get_u8, NLA_PUT_U8)
  79. #define __u16_field(attr_nr, attr_flag, name) \
  80. __field(attr_nr, attr_flag, name, NLA_U16, __u16, \
  81. nla_get_u16, NLA_PUT_U16)
  82. #define __u32_field(attr_nr, attr_flag, name) \
  83. __field(attr_nr, attr_flag, name, NLA_U32, __u32, \
  84. nla_get_u32, NLA_PUT_U32)
  85. #define __s32_field(attr_nr, attr_flag, name) \
  86. __field(attr_nr, attr_flag, name, NLA_U32, __s32, \
  87. nla_get_u32, NLA_PUT_U32)
  88. #define __u64_field(attr_nr, attr_flag, name) \
  89. __field(attr_nr, attr_flag, name, NLA_U64, __u64, \
  90. nla_get_u64, NLA_PUT_U64)
  91. #define __str_field(attr_nr, attr_flag, name, maxlen) \
  92. __array(attr_nr, attr_flag, name, NLA_NUL_STRING, char, maxlen, \
  93. nla_strlcpy, NLA_PUT)
  94. #define __bin_field(attr_nr, attr_flag, name, maxlen) \
  95. __array(attr_nr, attr_flag, name, NLA_BINARY, char, maxlen, \
  96. nla_memcpy, NLA_PUT)
  97. /* fields with default values */
  98. #define __flg_field_def(attr_nr, attr_flag, name, default) \
  99. __flg_field(attr_nr, attr_flag, name)
  100. #define __u32_field_def(attr_nr, attr_flag, name, default) \
  101. __u32_field(attr_nr, attr_flag, name)
  102. #define __s32_field_def(attr_nr, attr_flag, name, default) \
  103. __s32_field(attr_nr, attr_flag, name)
  104. #define __str_field_def(attr_nr, attr_flag, name, maxlen) \
  105. __str_field(attr_nr, attr_flag, name, maxlen)
  106. #define GENL_op_init(args...) args
  107. #define GENL_doit(handler) \
  108. .doit = handler, \
  109. .flags = GENL_ADMIN_PERM,
  110. #define GENL_dumpit(handler) \
  111. .dumpit = handler, \
  112. .flags = GENL_ADMIN_PERM,
  113. /* }}}1
  114. * Magic: define the enum symbols for genl_ops
  115. * Magic: define the enum symbols for top level attributes
  116. * Magic: define the enum symbols for nested attributes
  117. * {{{2
  118. */
  119. #undef GENL_struct
  120. #define GENL_struct(tag_name, tag_number, s_name, s_fields)
  121. #undef GENL_mc_group
  122. #define GENL_mc_group(group)
  123. #undef GENL_notification
  124. #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
  125. op_name = op_num,
  126. #undef GENL_op
  127. #define GENL_op(op_name, op_num, handler, tla_list) \
  128. op_name = op_num,
  129. enum {
  130. #include GENL_MAGIC_INCLUDE_FILE
  131. };
  132. #undef GENL_notification
  133. #define GENL_notification(op_name, op_num, mcast_group, tla_list)
  134. #undef GENL_op
  135. #define GENL_op(op_name, op_num, handler, attr_list)
  136. #undef GENL_struct
  137. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  138. tag_name = tag_number,
  139. enum {
  140. #include GENL_MAGIC_INCLUDE_FILE
  141. };
  142. #undef GENL_struct
  143. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  144. enum { \
  145. s_fields \
  146. };
  147. #undef __field
  148. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put) \
  149. T_ ## name = (__u16)(attr_nr | attr_flag),
  150. #undef __array
  151. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, __get, __put) \
  152. T_ ## name = (__u16)(attr_nr | attr_flag),
  153. #include GENL_MAGIC_INCLUDE_FILE
  154. /* }}}1
  155. * Magic: compile time assert unique numbers for operations
  156. * Magic: -"- unique numbers for top level attributes
  157. * Magic: -"- unique numbers for nested attributes
  158. * {{{2
  159. */
  160. #undef GENL_struct
  161. #define GENL_struct(tag_name, tag_number, s_name, s_fields)
  162. #undef GENL_op
  163. #define GENL_op(op_name, op_num, handler, attr_list) \
  164. case op_name:
  165. #undef GENL_notification
  166. #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
  167. case op_name:
  168. static inline void ct_assert_unique_operations(void)
  169. {
  170. switch (0) {
  171. #include GENL_MAGIC_INCLUDE_FILE
  172. ;
  173. }
  174. }
  175. #undef GENL_op
  176. #define GENL_op(op_name, op_num, handler, attr_list)
  177. #undef GENL_notification
  178. #define GENL_notification(op_name, op_num, mcast_group, tla_list)
  179. #undef GENL_struct
  180. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  181. case tag_number:
  182. static inline void ct_assert_unique_top_level_attributes(void)
  183. {
  184. switch (0) {
  185. #include GENL_MAGIC_INCLUDE_FILE
  186. ;
  187. }
  188. }
  189. #undef GENL_struct
  190. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  191. static inline void ct_assert_unique_ ## s_name ## _attributes(void) \
  192. { \
  193. switch (0) { \
  194. s_fields \
  195. ; \
  196. } \
  197. }
  198. #undef __field
  199. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put) \
  200. case attr_nr:
  201. #undef __array
  202. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, __get, __put) \
  203. case attr_nr:
  204. #include GENL_MAGIC_INCLUDE_FILE
  205. /* }}}1
  206. * Magic: declare structs
  207. * struct <name> {
  208. * fields
  209. * };
  210. * {{{2
  211. */
  212. #undef GENL_struct
  213. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  214. struct s_name { s_fields };
  215. #undef __field
  216. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put) \
  217. type name;
  218. #undef __array
  219. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, __get, __put) \
  220. type name[maxlen]; \
  221. __u32 name ## _len;
  222. #include GENL_MAGIC_INCLUDE_FILE
  223. /* }}}1 */
  224. #endif /* GENL_MAGIC_STRUCT_H */
  225. /* vim: set foldmethod=marker nofoldenable : */