genl_magic_func.h 12 KB

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