genl_magic_func.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #ifndef GENL_MAGIC_FUNC_H
  2. #define GENL_MAGIC_FUNC_H
  3. #include <linux/genl_magic_struct.h>
  4. /*
  5. * Magic: declare tla policy {{{1
  6. * Magic: declare nested policies
  7. * {{{2
  8. */
  9. #undef GENL_mc_group
  10. #define GENL_mc_group(group)
  11. #undef GENL_notification
  12. #define GENL_notification(op_name, op_num, mcast_group, tla_list)
  13. #undef GENL_op
  14. #define GENL_op(op_name, op_num, handler, tla_list)
  15. #undef GENL_struct
  16. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  17. [tag_name] = { .type = NLA_NESTED },
  18. static struct nla_policy CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy)[] = {
  19. #include GENL_MAGIC_INCLUDE_FILE
  20. };
  21. #undef GENL_struct
  22. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  23. static struct nla_policy s_name ## _nl_policy[] __read_mostly = \
  24. { s_fields };
  25. #undef __field
  26. #define __field(attr_nr, attr_flag, name, nla_type, _type, __get, \
  27. __put, __is_signed) \
  28. [attr_nr] = { .type = nla_type },
  29. #undef __array
  30. #define __array(attr_nr, attr_flag, name, nla_type, _type, maxlen, \
  31. __get, __put, __is_signed) \
  32. [attr_nr] = { .type = nla_type, \
  33. .len = maxlen - (nla_type == NLA_NUL_STRING) },
  34. #include GENL_MAGIC_INCLUDE_FILE
  35. #ifndef __KERNEL__
  36. #ifndef pr_info
  37. #define pr_info(args...) fprintf(stderr, args);
  38. #endif
  39. #endif
  40. #ifdef GENL_MAGIC_DEBUG
  41. static void dprint_field(const char *dir, int nla_type,
  42. const char *name, void *valp)
  43. {
  44. __u64 val = valp ? *(__u32 *)valp : 1;
  45. switch (nla_type) {
  46. case NLA_U8: val = (__u8)val;
  47. case NLA_U16: val = (__u16)val;
  48. case NLA_U32: val = (__u32)val;
  49. pr_info("%s attr %s: %d 0x%08x\n", dir,
  50. name, (int)val, (unsigned)val);
  51. break;
  52. case NLA_U64:
  53. val = *(__u64*)valp;
  54. pr_info("%s attr %s: %lld 0x%08llx\n", dir,
  55. name, (long long)val, (unsigned long long)val);
  56. break;
  57. case NLA_FLAG:
  58. if (val)
  59. pr_info("%s attr %s: set\n", dir, name);
  60. break;
  61. }
  62. }
  63. static void dprint_array(const char *dir, int nla_type,
  64. const char *name, const char *val, unsigned len)
  65. {
  66. switch (nla_type) {
  67. case NLA_NUL_STRING:
  68. if (len && val[len-1] == '\0')
  69. len--;
  70. pr_info("%s attr %s: [len:%u] '%s'\n", dir, name, len, val);
  71. break;
  72. default:
  73. /* we can always show 4 byte,
  74. * thats what nlattr are aligned to. */
  75. pr_info("%s attr %s: [len:%u] %02x%02x%02x%02x ...\n",
  76. dir, name, len, val[0], val[1], val[2], val[3]);
  77. }
  78. }
  79. #define DPRINT_TLA(a, op, b) pr_info("%s %s %s\n", a, op, b);
  80. /* Name is a member field name of the struct s.
  81. * If s is NULL (only parsing, no copy requested in *_from_attrs()),
  82. * nla is supposed to point to the attribute containing the information
  83. * corresponding to that struct member. */
  84. #define DPRINT_FIELD(dir, nla_type, name, s, nla) \
  85. do { \
  86. if (s) \
  87. dprint_field(dir, nla_type, #name, &s->name); \
  88. else if (nla) \
  89. dprint_field(dir, nla_type, #name, \
  90. (nla_type == NLA_FLAG) ? NULL \
  91. : nla_data(nla)); \
  92. } while (0)
  93. #define DPRINT_ARRAY(dir, nla_type, name, s, nla) \
  94. do { \
  95. if (s) \
  96. dprint_array(dir, nla_type, #name, \
  97. s->name, s->name ## _len); \
  98. else if (nla) \
  99. dprint_array(dir, nla_type, #name, \
  100. nla_data(nla), nla_len(nla)); \
  101. } while (0)
  102. #else
  103. #define DPRINT_TLA(a, op, b) do {} while (0)
  104. #define DPRINT_FIELD(dir, nla_type, name, s, nla) do {} while (0)
  105. #define DPRINT_ARRAY(dir, nla_type, name, s, nla) do {} while (0)
  106. #endif
  107. /*
  108. * Magic: provide conversion functions {{{1
  109. * populate struct from attribute table:
  110. * {{{2
  111. */
  112. /* processing of generic netlink messages is serialized.
  113. * use one static buffer for parsing of nested attributes */
  114. static struct nlattr *nested_attr_tb[128];
  115. #ifndef BUILD_BUG_ON
  116. /* Force a compilation error if condition is true */
  117. #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
  118. /* Force a compilation error if condition is true, but also produce a
  119. result (of value 0 and type size_t), so the expression can be used
  120. e.g. in a structure initializer (or where-ever else comma expressions
  121. aren't permitted). */
  122. #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
  123. #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
  124. #endif
  125. #undef GENL_struct
  126. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  127. /* *_from_attrs functions are static, but potentially unused */ \
  128. static int __ ## s_name ## _from_attrs(struct s_name *s, \
  129. struct genl_info *info, bool exclude_invariants) \
  130. { \
  131. const int maxtype = ARRAY_SIZE(s_name ## _nl_policy)-1; \
  132. struct nlattr *tla = info->attrs[tag_number]; \
  133. struct nlattr **ntb = nested_attr_tb; \
  134. struct nlattr *nla; \
  135. int err; \
  136. BUILD_BUG_ON(ARRAY_SIZE(s_name ## _nl_policy) > ARRAY_SIZE(nested_attr_tb)); \
  137. if (!tla) \
  138. return -ENOMSG; \
  139. DPRINT_TLA(#s_name, "<=-", #tag_name); \
  140. err = drbd_nla_parse_nested(ntb, maxtype, tla, s_name ## _nl_policy); \
  141. if (err) \
  142. return err; \
  143. \
  144. s_fields \
  145. return 0; \
  146. } __attribute__((unused)) \
  147. static int s_name ## _from_attrs(struct s_name *s, \
  148. struct genl_info *info) \
  149. { \
  150. return __ ## s_name ## _from_attrs(s, info, false); \
  151. } __attribute__((unused)) \
  152. static int s_name ## _from_attrs_for_change(struct s_name *s, \
  153. struct genl_info *info) \
  154. { \
  155. return __ ## s_name ## _from_attrs(s, info, true); \
  156. } __attribute__((unused)) \
  157. #define __assign(attr_nr, attr_flag, name, nla_type, type, assignment...) \
  158. nla = ntb[attr_nr]; \
  159. if (nla) { \
  160. if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \
  161. pr_info("<< must not change invariant attr: %s\n", #name); \
  162. return -EEXIST; \
  163. } \
  164. assignment; \
  165. } else if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \
  166. /* attribute missing from payload, */ \
  167. /* which was expected */ \
  168. } else if ((attr_flag) & DRBD_F_REQUIRED) { \
  169. pr_info("<< missing attr: %s\n", #name); \
  170. return -ENOMSG; \
  171. }
  172. #undef __field
  173. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
  174. __is_signed) \
  175. __assign(attr_nr, attr_flag, name, nla_type, type, \
  176. if (s) \
  177. s->name = __get(nla); \
  178. DPRINT_FIELD("<<", nla_type, name, s, nla))
  179. /* validate_nla() already checked nla_len <= maxlen appropriately. */
  180. #undef __array
  181. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
  182. __get, __put, __is_signed) \
  183. __assign(attr_nr, attr_flag, name, nla_type, type, \
  184. if (s) \
  185. s->name ## _len = \
  186. __get(s->name, nla, maxlen); \
  187. DPRINT_ARRAY("<<", nla_type, name, s, nla))
  188. #include GENL_MAGIC_INCLUDE_FILE
  189. #undef GENL_struct
  190. #define GENL_struct(tag_name, tag_number, s_name, s_fields)
  191. /*
  192. * Magic: define op number to op name mapping {{{1
  193. * {{{2
  194. */
  195. const char *CONCAT_(GENL_MAGIC_FAMILY, _genl_cmd_to_str)(__u8 cmd)
  196. {
  197. switch (cmd) {
  198. #undef GENL_op
  199. #define GENL_op(op_name, op_num, handler, tla_list) \
  200. case op_num: return #op_name;
  201. #include GENL_MAGIC_INCLUDE_FILE
  202. default:
  203. return "unknown";
  204. }
  205. }
  206. #ifdef __KERNEL__
  207. #include <linux/stringify.h>
  208. /*
  209. * Magic: define genl_ops {{{1
  210. * {{{2
  211. */
  212. #undef GENL_op
  213. #define GENL_op(op_name, op_num, handler, tla_list) \
  214. { \
  215. handler \
  216. .cmd = op_name, \
  217. .policy = CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy), \
  218. },
  219. #define ZZZ_genl_ops CONCAT_(GENL_MAGIC_FAMILY, _genl_ops)
  220. static struct genl_ops ZZZ_genl_ops[] __read_mostly = {
  221. #include GENL_MAGIC_INCLUDE_FILE
  222. };
  223. #undef GENL_op
  224. #define GENL_op(op_name, op_num, handler, tla_list)
  225. /*
  226. * Define the genl_family, multicast groups, {{{1
  227. * and provide register/unregister functions.
  228. * {{{2
  229. */
  230. #define ZZZ_genl_family CONCAT_(GENL_MAGIC_FAMILY, _genl_family)
  231. static struct genl_family ZZZ_genl_family __read_mostly = {
  232. .id = GENL_ID_GENERATE,
  233. .name = __stringify(GENL_MAGIC_FAMILY),
  234. .version = GENL_MAGIC_VERSION,
  235. #ifdef GENL_MAGIC_FAMILY_HDRSZ
  236. .hdrsize = NLA_ALIGN(GENL_MAGIC_FAMILY_HDRSZ),
  237. #endif
  238. .maxattr = ARRAY_SIZE(drbd_tla_nl_policy)-1,
  239. };
  240. /*
  241. * Magic: define multicast groups
  242. * Magic: define multicast group registration helper
  243. */
  244. #define ZZZ_genl_mcgrps CONCAT_(GENL_MAGIC_FAMILY, _genl_mcgrps)
  245. static const struct genl_multicast_group ZZZ_genl_mcgrps[] = {
  246. #undef GENL_mc_group
  247. #define GENL_mc_group(group) { .name = #group, },
  248. #include GENL_MAGIC_INCLUDE_FILE
  249. };
  250. enum CONCAT_(GENL_MAGIC_FAMILY, group_ids) {
  251. #undef GENL_mc_group
  252. #define GENL_mc_group(group) CONCAT_(GENL_MAGIC_FAMILY, _group_ ## group),
  253. #include GENL_MAGIC_INCLUDE_FILE
  254. };
  255. #undef GENL_mc_group
  256. #define GENL_mc_group(group) \
  257. static int CONCAT_(GENL_MAGIC_FAMILY, _genl_multicast_ ## group)( \
  258. struct sk_buff *skb, gfp_t flags) \
  259. { \
  260. unsigned int group_id = \
  261. CONCAT_(GENL_MAGIC_FAMILY, _group_ ## group); \
  262. return genlmsg_multicast(&ZZZ_genl_family, skb, 0, \
  263. group_id, flags); \
  264. }
  265. #include GENL_MAGIC_INCLUDE_FILE
  266. #undef GENL_mc_group
  267. #define GENL_mc_group(group)
  268. int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void)
  269. {
  270. return genl_register_family_with_ops_groups(&ZZZ_genl_family, \
  271. ZZZ_genl_ops, \
  272. ZZZ_genl_mcgrps);
  273. }
  274. void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void)
  275. {
  276. genl_unregister_family(&ZZZ_genl_family);
  277. }
  278. /*
  279. * Magic: provide conversion functions {{{1
  280. * populate skb from struct.
  281. * {{{2
  282. */
  283. #undef GENL_op
  284. #define GENL_op(op_name, op_num, handler, tla_list)
  285. #undef GENL_struct
  286. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  287. static int s_name ## _to_skb(struct sk_buff *skb, struct s_name *s, \
  288. const bool exclude_sensitive) \
  289. { \
  290. struct nlattr *tla = nla_nest_start(skb, tag_number); \
  291. if (!tla) \
  292. goto nla_put_failure; \
  293. DPRINT_TLA(#s_name, "-=>", #tag_name); \
  294. s_fields \
  295. nla_nest_end(skb, tla); \
  296. return 0; \
  297. \
  298. nla_put_failure: \
  299. if (tla) \
  300. nla_nest_cancel(skb, tla); \
  301. return -EMSGSIZE; \
  302. } \
  303. static inline int s_name ## _to_priv_skb(struct sk_buff *skb, \
  304. struct s_name *s) \
  305. { \
  306. return s_name ## _to_skb(skb, s, 0); \
  307. } \
  308. static inline int s_name ## _to_unpriv_skb(struct sk_buff *skb, \
  309. struct s_name *s) \
  310. { \
  311. return s_name ## _to_skb(skb, s, 1); \
  312. }
  313. #undef __field
  314. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
  315. __is_signed) \
  316. if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \
  317. DPRINT_FIELD(">>", nla_type, name, s, NULL); \
  318. if (__put(skb, attr_nr, s->name)) \
  319. goto nla_put_failure; \
  320. }
  321. #undef __array
  322. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
  323. __get, __put, __is_signed) \
  324. if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \
  325. DPRINT_ARRAY(">>",nla_type, name, s, NULL); \
  326. if (__put(skb, attr_nr, min_t(int, maxlen, \
  327. s->name ## _len + (nla_type == NLA_NUL_STRING)),\
  328. s->name)) \
  329. goto nla_put_failure; \
  330. }
  331. #include GENL_MAGIC_INCLUDE_FILE
  332. /* Functions for initializing structs to default values. */
  333. #undef __field
  334. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
  335. __is_signed)
  336. #undef __array
  337. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
  338. __get, __put, __is_signed)
  339. #undef __u32_field_def
  340. #define __u32_field_def(attr_nr, attr_flag, name, default) \
  341. x->name = default;
  342. #undef __s32_field_def
  343. #define __s32_field_def(attr_nr, attr_flag, name, default) \
  344. x->name = default;
  345. #undef __flg_field_def
  346. #define __flg_field_def(attr_nr, attr_flag, name, default) \
  347. x->name = default;
  348. #undef __str_field_def
  349. #define __str_field_def(attr_nr, attr_flag, name, maxlen) \
  350. memset(x->name, 0, sizeof(x->name)); \
  351. x->name ## _len = 0;
  352. #undef GENL_struct
  353. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  354. static void set_ ## s_name ## _defaults(struct s_name *x) __attribute__((unused)); \
  355. static void set_ ## s_name ## _defaults(struct s_name *x) { \
  356. s_fields \
  357. }
  358. #include GENL_MAGIC_INCLUDE_FILE
  359. #endif /* __KERNEL__ */
  360. /* }}}1 */
  361. #endif /* GENL_MAGIC_FUNC_H */
  362. /* vim: set foldmethod=marker foldlevel=1 nofoldenable : */