genl_magic_struct.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. /* This will not be present in the __u16 .nla_type, but can be
  54. * triggered on in <struct>_to_skb, to exclude "sensitive"
  55. * information from broadcasts, or on unpriviledged get requests.
  56. * This is useful because genetlink multicast groups can be listened in
  57. * on by anyone. */
  58. GENLA_F_SENSITIVE = 1 << 16,
  59. };
  60. #define __nla_type(x) ((__u16)((__u16)(x) & (__u16)NLA_TYPE_MASK))
  61. /* }}}1
  62. * MAGIC
  63. * multi-include macro expansion magic starts here
  64. */
  65. /* MAGIC helpers {{{2 */
  66. /* possible field types */
  67. #define __flg_field(attr_nr, attr_flag, name) \
  68. __field(attr_nr, attr_flag, name, NLA_FLAG, char, \
  69. nla_get_flag, __nla_put_flag)
  70. #define __u8_field(attr_nr, attr_flag, name) \
  71. __field(attr_nr, attr_flag, name, NLA_U8, unsigned char, \
  72. nla_get_u8, NLA_PUT_U8)
  73. #define __u16_field(attr_nr, attr_flag, name) \
  74. __field(attr_nr, attr_flag, name, NLA_U16, __u16, \
  75. nla_get_u16, NLA_PUT_U16)
  76. #define __u32_field(attr_nr, attr_flag, name) \
  77. __field(attr_nr, attr_flag, name, NLA_U32, __u32, \
  78. nla_get_u32, NLA_PUT_U32)
  79. #define __u64_field(attr_nr, attr_flag, name) \
  80. __field(attr_nr, attr_flag, name, NLA_U64, __u64, \
  81. nla_get_u64, NLA_PUT_U64)
  82. #define __str_field(attr_nr, attr_flag, name, maxlen) \
  83. __array(attr_nr, attr_flag, name, NLA_NUL_STRING, char, maxlen, \
  84. nla_strlcpy, NLA_PUT)
  85. #define __bin_field(attr_nr, attr_flag, name, maxlen) \
  86. __array(attr_nr, attr_flag, name, NLA_BINARY, char, maxlen, \
  87. nla_memcpy, NLA_PUT)
  88. #define __nla_put_flag(skb, attrtype, value) \
  89. do { \
  90. if (value) \
  91. NLA_PUT_FLAG(skb, attrtype); \
  92. } while (0)
  93. #define GENL_op_init(args...) args
  94. #define GENL_doit(handler) \
  95. .doit = handler, \
  96. .flags = GENL_ADMIN_PERM,
  97. #define GENL_dumpit(handler) \
  98. .dumpit = handler, \
  99. .flags = GENL_ADMIN_PERM,
  100. /* }}}1
  101. * Magic: define the enum symbols for genl_ops
  102. * Magic: define the enum symbols for top level attributes
  103. * Magic: define the enum symbols for nested attributes
  104. * {{{2
  105. */
  106. #undef GENL_struct
  107. #define GENL_struct(tag_name, tag_number, s_name, s_fields)
  108. #undef GENL_mc_group
  109. #define GENL_mc_group(group)
  110. #undef GENL_notification
  111. #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
  112. op_name = op_num,
  113. #undef GENL_op
  114. #define GENL_op(op_name, op_num, handler, tla_list) \
  115. op_name = op_num,
  116. enum {
  117. #include GENL_MAGIC_INCLUDE_FILE
  118. };
  119. #undef GENL_notification
  120. #define GENL_notification(op_name, op_num, mcast_group, tla_list)
  121. #undef GENL_op
  122. #define GENL_op(op_name, op_num, handler, attr_list)
  123. #undef GENL_struct
  124. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  125. tag_name = tag_number,
  126. enum {
  127. #include GENL_MAGIC_INCLUDE_FILE
  128. };
  129. #undef GENL_struct
  130. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  131. enum { \
  132. s_fields \
  133. };
  134. #undef __field
  135. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put) \
  136. T_ ## name = (__u16)(attr_nr | attr_flag),
  137. #undef __array
  138. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, __get, __put) \
  139. T_ ## name = (__u16)(attr_nr | attr_flag),
  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. ;
  160. }
  161. }
  162. #undef GENL_op
  163. #define GENL_op(op_name, op_num, handler, attr_list)
  164. #undef GENL_notification
  165. #define GENL_notification(op_name, op_num, mcast_group, tla_list)
  166. #undef GENL_struct
  167. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  168. case tag_number:
  169. static inline void ct_assert_unique_top_level_attributes(void)
  170. {
  171. switch (0) {
  172. #include GENL_MAGIC_INCLUDE_FILE
  173. ;
  174. }
  175. }
  176. #undef GENL_struct
  177. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  178. static inline void ct_assert_unique_ ## s_name ## _attributes(void) \
  179. { \
  180. switch (0) { \
  181. s_fields \
  182. ; \
  183. } \
  184. }
  185. #undef __field
  186. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put) \
  187. case attr_nr:
  188. #undef __array
  189. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, __get, __put) \
  190. case attr_nr:
  191. #include GENL_MAGIC_INCLUDE_FILE
  192. /* }}}1
  193. * Magic: declare structs
  194. * struct <name> {
  195. * fields
  196. * };
  197. * {{{2
  198. */
  199. #undef GENL_struct
  200. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  201. struct s_name { s_fields };
  202. #undef __field
  203. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put) \
  204. type name;
  205. #undef __array
  206. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, __get, __put) \
  207. type name[maxlen]; \
  208. __u32 name ## _len;
  209. #include GENL_MAGIC_INCLUDE_FILE
  210. /* }}}1 */
  211. #endif /* GENL_MAGIC_STRUCT_H */
  212. /* vim: set foldmethod=marker nofoldenable : */