genl_magic_struct.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 __str_field_def(attr_nr, attr_flag, name, maxlen) \
  103. __str_field(attr_nr, attr_flag, name, maxlen)
  104. #define GENL_op_init(args...) args
  105. #define GENL_doit(handler) \
  106. .doit = handler, \
  107. .flags = GENL_ADMIN_PERM,
  108. #define GENL_dumpit(handler) \
  109. .dumpit = handler, \
  110. .flags = GENL_ADMIN_PERM,
  111. /* }}}1
  112. * Magic: define the enum symbols for genl_ops
  113. * Magic: define the enum symbols for top level attributes
  114. * Magic: define the enum symbols for nested attributes
  115. * {{{2
  116. */
  117. #undef GENL_struct
  118. #define GENL_struct(tag_name, tag_number, s_name, s_fields)
  119. #undef GENL_mc_group
  120. #define GENL_mc_group(group)
  121. #undef GENL_notification
  122. #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
  123. op_name = op_num,
  124. #undef GENL_op
  125. #define GENL_op(op_name, op_num, handler, tla_list) \
  126. op_name = op_num,
  127. enum {
  128. #include GENL_MAGIC_INCLUDE_FILE
  129. };
  130. #undef GENL_notification
  131. #define GENL_notification(op_name, op_num, mcast_group, tla_list)
  132. #undef GENL_op
  133. #define GENL_op(op_name, op_num, handler, attr_list)
  134. #undef GENL_struct
  135. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  136. tag_name = tag_number,
  137. enum {
  138. #include GENL_MAGIC_INCLUDE_FILE
  139. };
  140. #undef GENL_struct
  141. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  142. enum { \
  143. s_fields \
  144. };
  145. #undef __field
  146. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put) \
  147. T_ ## name = (__u16)(attr_nr | attr_flag),
  148. #undef __array
  149. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, __get, __put) \
  150. T_ ## name = (__u16)(attr_nr | attr_flag),
  151. #include GENL_MAGIC_INCLUDE_FILE
  152. /* }}}1
  153. * Magic: compile time assert unique numbers for operations
  154. * Magic: -"- unique numbers for top level attributes
  155. * Magic: -"- unique numbers for nested attributes
  156. * {{{2
  157. */
  158. #undef GENL_struct
  159. #define GENL_struct(tag_name, tag_number, s_name, s_fields)
  160. #undef GENL_op
  161. #define GENL_op(op_name, op_num, handler, attr_list) \
  162. case op_name:
  163. #undef GENL_notification
  164. #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
  165. case op_name:
  166. static inline void ct_assert_unique_operations(void)
  167. {
  168. switch (0) {
  169. #include GENL_MAGIC_INCLUDE_FILE
  170. ;
  171. }
  172. }
  173. #undef GENL_op
  174. #define GENL_op(op_name, op_num, handler, attr_list)
  175. #undef GENL_notification
  176. #define GENL_notification(op_name, op_num, mcast_group, tla_list)
  177. #undef GENL_struct
  178. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  179. case tag_number:
  180. static inline void ct_assert_unique_top_level_attributes(void)
  181. {
  182. switch (0) {
  183. #include GENL_MAGIC_INCLUDE_FILE
  184. ;
  185. }
  186. }
  187. #undef GENL_struct
  188. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  189. static inline void ct_assert_unique_ ## s_name ## _attributes(void) \
  190. { \
  191. switch (0) { \
  192. s_fields \
  193. ; \
  194. } \
  195. }
  196. #undef __field
  197. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put) \
  198. case attr_nr:
  199. #undef __array
  200. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, __get, __put) \
  201. case attr_nr:
  202. #include GENL_MAGIC_INCLUDE_FILE
  203. /* }}}1
  204. * Magic: declare structs
  205. * struct <name> {
  206. * fields
  207. * };
  208. * {{{2
  209. */
  210. #undef GENL_struct
  211. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  212. struct s_name { s_fields };
  213. #undef __field
  214. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put) \
  215. type name;
  216. #undef __array
  217. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, __get, __put) \
  218. type name[maxlen]; \
  219. __u32 name ## _len;
  220. #include GENL_MAGIC_INCLUDE_FILE
  221. /* }}}1 */
  222. #endif /* GENL_MAGIC_STRUCT_H */
  223. /* vim: set foldmethod=marker nofoldenable : */