genl_magic_func.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. static inline int drbd_nla_check_mandatory(int maxtype, struct nlattr *nla)
  126. {
  127. struct nlattr *head = nla_data(nla);
  128. int len = nla_len(nla);
  129. int rem;
  130. /*
  131. * validate_nla (called from nla_parse_nested) ignores attributes
  132. * beyond maxtype, and does not understand the DRBD_GENLA_F_MANDATORY flag.
  133. * In order to have it validate attributes with the DRBD_GENLA_F_MANDATORY
  134. * flag set also, check and remove that flag before calling
  135. * nla_parse_nested.
  136. */
  137. nla_for_each_attr(nla, head, len, rem) {
  138. if (nla->nla_type & DRBD_GENLA_F_MANDATORY) {
  139. nla->nla_type &= ~DRBD_GENLA_F_MANDATORY;
  140. if (nla_type(nla) > maxtype)
  141. return -EOPNOTSUPP;
  142. }
  143. }
  144. return 0;
  145. }
  146. static inline int drbd_nla_parse_nested(struct nlattr *tb[], int maxtype,
  147. struct nlattr *nla,
  148. const struct nla_policy *policy)
  149. {
  150. int err;
  151. err = drbd_nla_check_mandatory(maxtype, nla);
  152. if (!err)
  153. err = nla_parse_nested(tb, maxtype, nla, policy);
  154. return err;
  155. }
  156. #undef GENL_struct
  157. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  158. /* *_from_attrs functions are static, but potentially unused */ \
  159. static int __ ## s_name ## _from_attrs(struct s_name *s, \
  160. struct genl_info *info, bool exclude_invariants) \
  161. { \
  162. const int maxtype = ARRAY_SIZE(s_name ## _nl_policy)-1; \
  163. struct nlattr *tla = info->attrs[tag_number]; \
  164. struct nlattr **ntb = nested_attr_tb; \
  165. struct nlattr *nla; \
  166. int err; \
  167. BUILD_BUG_ON(ARRAY_SIZE(s_name ## _nl_policy) > ARRAY_SIZE(nested_attr_tb)); \
  168. if (!tla) \
  169. return -ENOMSG; \
  170. DPRINT_TLA(#s_name, "<=-", #tag_name); \
  171. err = drbd_nla_parse_nested(ntb, maxtype, tla, s_name ## _nl_policy); \
  172. if (err) \
  173. return err; \
  174. \
  175. s_fields \
  176. return 0; \
  177. } __attribute__((unused)) \
  178. static int s_name ## _from_attrs(struct s_name *s, \
  179. struct genl_info *info) \
  180. { \
  181. return __ ## s_name ## _from_attrs(s, info, false); \
  182. } __attribute__((unused)) \
  183. static int s_name ## _from_attrs_for_change(struct s_name *s, \
  184. struct genl_info *info) \
  185. { \
  186. return __ ## s_name ## _from_attrs(s, info, true); \
  187. } __attribute__((unused)) \
  188. #define __assign(attr_nr, attr_flag, name, nla_type, type, assignment...) \
  189. nla = ntb[attr_nr]; \
  190. if (nla) { \
  191. if (exclude_invariants && ((attr_flag) & DRBD_F_INVARIANT)) { \
  192. pr_info("<< must not change invariant attr: %s\n", #name); \
  193. return -EEXIST; \
  194. } \
  195. assignment; \
  196. } else if (exclude_invariants && ((attr_flag) & DRBD_F_INVARIANT)) { \
  197. /* attribute missing from payload, */ \
  198. /* which was expected */ \
  199. } else if ((attr_flag) & DRBD_F_REQUIRED) { \
  200. pr_info("<< missing attr: %s\n", #name); \
  201. return -ENOMSG; \
  202. }
  203. #undef __field
  204. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
  205. __is_signed) \
  206. __assign(attr_nr, attr_flag, name, nla_type, type, \
  207. if (s) \
  208. s->name = __get(nla); \
  209. DPRINT_FIELD("<<", nla_type, name, s, nla))
  210. /* validate_nla() already checked nla_len <= maxlen appropriately. */
  211. #undef __array
  212. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
  213. __get, __put, __is_signed) \
  214. __assign(attr_nr, attr_flag, name, nla_type, type, \
  215. if (s) \
  216. s->name ## _len = \
  217. __get(s->name, nla, maxlen); \
  218. DPRINT_ARRAY("<<", nla_type, name, s, nla))
  219. #include GENL_MAGIC_INCLUDE_FILE
  220. #undef GENL_struct
  221. #define GENL_struct(tag_name, tag_number, s_name, s_fields)
  222. /*
  223. * Magic: define op number to op name mapping {{{1
  224. * {{{2
  225. */
  226. const char *CONCAT_(GENL_MAGIC_FAMILY, _genl_cmd_to_str)(__u8 cmd)
  227. {
  228. switch (cmd) {
  229. #undef GENL_op
  230. #define GENL_op(op_name, op_num, handler, tla_list) \
  231. case op_num: return #op_name;
  232. #include GENL_MAGIC_INCLUDE_FILE
  233. default:
  234. return "unknown";
  235. }
  236. }
  237. #ifdef __KERNEL__
  238. #include <linux/stringify.h>
  239. /*
  240. * Magic: define genl_ops {{{1
  241. * {{{2
  242. */
  243. #undef GENL_op
  244. #define GENL_op(op_name, op_num, handler, tla_list) \
  245. { \
  246. handler \
  247. .cmd = op_name, \
  248. .policy = CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy), \
  249. },
  250. #define ZZZ_genl_ops CONCAT_(GENL_MAGIC_FAMILY, _genl_ops)
  251. static struct genl_ops ZZZ_genl_ops[] __read_mostly = {
  252. #include GENL_MAGIC_INCLUDE_FILE
  253. };
  254. #undef GENL_op
  255. #define GENL_op(op_name, op_num, handler, tla_list)
  256. /*
  257. * Define the genl_family, multicast groups, {{{1
  258. * and provide register/unregister functions.
  259. * {{{2
  260. */
  261. #define ZZZ_genl_family CONCAT_(GENL_MAGIC_FAMILY, _genl_family)
  262. static struct genl_family ZZZ_genl_family __read_mostly = {
  263. .id = GENL_ID_GENERATE,
  264. .name = __stringify(GENL_MAGIC_FAMILY),
  265. .version = GENL_MAGIC_VERSION,
  266. #ifdef GENL_MAGIC_FAMILY_HDRSZ
  267. .hdrsize = NLA_ALIGN(GENL_MAGIC_FAMILY_HDRSZ),
  268. #endif
  269. .maxattr = ARRAY_SIZE(drbd_tla_nl_policy)-1,
  270. };
  271. /*
  272. * Magic: define multicast groups
  273. * Magic: define multicast group registration helper
  274. */
  275. #undef GENL_mc_group
  276. #define GENL_mc_group(group) \
  277. static struct genl_multicast_group \
  278. CONCAT_(GENL_MAGIC_FAMILY, _mcg_ ## group) __read_mostly = { \
  279. .name = #group, \
  280. }; \
  281. static int CONCAT_(GENL_MAGIC_FAMILY, _genl_multicast_ ## group)( \
  282. struct sk_buff *skb, gfp_t flags) \
  283. { \
  284. unsigned int group_id = \
  285. CONCAT_(GENL_MAGIC_FAMILY, _mcg_ ## group).id; \
  286. if (!group_id) \
  287. return -EINVAL; \
  288. return genlmsg_multicast(skb, 0, group_id, flags); \
  289. }
  290. #include GENL_MAGIC_INCLUDE_FILE
  291. int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void)
  292. {
  293. int err = genl_register_family_with_ops(&ZZZ_genl_family,
  294. ZZZ_genl_ops, ARRAY_SIZE(ZZZ_genl_ops));
  295. if (err)
  296. return err;
  297. #undef GENL_mc_group
  298. #define GENL_mc_group(group) \
  299. err = genl_register_mc_group(&ZZZ_genl_family, \
  300. &CONCAT_(GENL_MAGIC_FAMILY, _mcg_ ## group)); \
  301. if (err) \
  302. goto fail; \
  303. else \
  304. pr_info("%s: mcg %s: %u\n", #group, \
  305. __stringify(GENL_MAGIC_FAMILY), \
  306. CONCAT_(GENL_MAGIC_FAMILY, _mcg_ ## group).id);
  307. #include GENL_MAGIC_INCLUDE_FILE
  308. #undef GENL_mc_group
  309. #define GENL_mc_group(group)
  310. return 0;
  311. fail:
  312. genl_unregister_family(&ZZZ_genl_family);
  313. return err;
  314. }
  315. void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void)
  316. {
  317. genl_unregister_family(&ZZZ_genl_family);
  318. }
  319. /*
  320. * Magic: provide conversion functions {{{1
  321. * populate skb from struct.
  322. * {{{2
  323. */
  324. #undef GENL_op
  325. #define GENL_op(op_name, op_num, handler, tla_list)
  326. #undef GENL_struct
  327. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  328. static int s_name ## _to_skb(struct sk_buff *skb, struct s_name *s, \
  329. const bool exclude_sensitive) \
  330. { \
  331. struct nlattr *tla = nla_nest_start(skb, tag_number); \
  332. if (!tla) \
  333. goto nla_put_failure; \
  334. DPRINT_TLA(#s_name, "-=>", #tag_name); \
  335. s_fields \
  336. nla_nest_end(skb, tla); \
  337. return 0; \
  338. \
  339. nla_put_failure: \
  340. if (tla) \
  341. nla_nest_cancel(skb, tla); \
  342. return -EMSGSIZE; \
  343. } \
  344. static inline int s_name ## _to_priv_skb(struct sk_buff *skb, \
  345. struct s_name *s) \
  346. { \
  347. return s_name ## _to_skb(skb, s, 0); \
  348. } \
  349. static inline int s_name ## _to_unpriv_skb(struct sk_buff *skb, \
  350. struct s_name *s) \
  351. { \
  352. return s_name ## _to_skb(skb, s, 1); \
  353. }
  354. #undef __field
  355. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
  356. __is_signed) \
  357. if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \
  358. DPRINT_FIELD(">>", nla_type, name, s, NULL); \
  359. __put(skb, attr_nr, s->name); \
  360. }
  361. #undef __array
  362. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
  363. __get, __put, __is_signed) \
  364. if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \
  365. DPRINT_ARRAY(">>",nla_type, name, s, NULL); \
  366. __put(skb, attr_nr, min_t(int, maxlen, \
  367. s->name ## _len + (nla_type == NLA_NUL_STRING)),\
  368. s->name); \
  369. }
  370. #include GENL_MAGIC_INCLUDE_FILE
  371. /* Functions for initializing structs to default values. */
  372. #undef __field
  373. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
  374. __is_signed)
  375. #undef __array
  376. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
  377. __get, __put, __is_signed)
  378. #undef __u32_field_def
  379. #define __u32_field_def(attr_nr, attr_flag, name, default) \
  380. x->name = default;
  381. #undef __s32_field_def
  382. #define __s32_field_def(attr_nr, attr_flag, name, default) \
  383. x->name = default;
  384. #undef __flg_field_def
  385. #define __flg_field_def(attr_nr, attr_flag, name, default) \
  386. x->name = default;
  387. #undef __str_field_def
  388. #define __str_field_def(attr_nr, attr_flag, name, maxlen) \
  389. memset(x->name, 0, sizeof(x->name)); \
  390. x->name ## _len = 0;
  391. #undef GENL_struct
  392. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  393. static void set_ ## s_name ## _defaults(struct s_name *x) __attribute__((unused)); \
  394. static void set_ ## s_name ## _defaults(struct s_name *x) { \
  395. s_fields \
  396. }
  397. #include GENL_MAGIC_INCLUDE_FILE
  398. #endif /* __KERNEL__ */
  399. /* }}}1 */
  400. #endif /* GENL_MAGIC_FUNC_H */
  401. /* vim: set foldmethod=marker foldlevel=1 nofoldenable : */