genl_magic_func.h 14 KB

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