genl_magic_func.h 12 KB

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