genetlink.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_GENERIC_NETLINK_H
  3. #define __NET_GENERIC_NETLINK_H
  4. #include <linux/genetlink.h>
  5. #include <linux/android_kabi.h>
  6. #include <net/netlink.h>
  7. #include <net/net_namespace.h>
  8. #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
  9. /**
  10. * struct genl_multicast_group - generic netlink multicast group
  11. * @name: name of the multicast group, names are per-family
  12. * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
  13. */
  14. struct genl_multicast_group {
  15. char name[GENL_NAMSIZ];
  16. u8 flags;
  17. };
  18. struct genl_ops;
  19. struct genl_info;
  20. /**
  21. * struct genl_family - generic netlink family
  22. * @id: protocol family identifier (private)
  23. * @hdrsize: length of user specific header in bytes
  24. * @name: name of family
  25. * @version: protocol version
  26. * @maxattr: maximum number of attributes supported
  27. * @policy: netlink policy
  28. * @netnsok: set to true if the family can handle network
  29. * namespaces and should be presented in all of them
  30. * @parallel_ops: operations can be called in parallel and aren't
  31. * synchronized by the core genetlink code
  32. * @pre_doit: called before an operation's doit callback, it may
  33. * do additional, common, filtering and return an error
  34. * @post_doit: called after an operation's doit callback, it may
  35. * undo operations done by pre_doit, for example release locks
  36. * @module: pointer to the owning module (set to THIS_MODULE)
  37. * @mcgrps: multicast groups used by this family
  38. * @n_mcgrps: number of multicast groups
  39. * @resv_start_op: first operation for which reserved fields of the header
  40. * can be validated and policies are required (see below);
  41. * new families should leave this field at zero
  42. * @mcgrp_offset: starting number of multicast group IDs in this family
  43. * (private)
  44. * @ops: the operations supported by this family
  45. * @n_ops: number of operations supported by this family
  46. * @small_ops: the small-struct operations supported by this family
  47. * @n_small_ops: number of small-struct operations supported by this family
  48. *
  49. * Attribute policies (the combination of @policy and @maxattr fields)
  50. * can be attached at the family level or at the operation level.
  51. * If both are present the per-operation policy takes precedence.
  52. * For operations before @resv_start_op lack of policy means that the core
  53. * will perform no attribute parsing or validation. For newer operations
  54. * if policy is not provided core will reject all TLV attributes.
  55. */
  56. struct genl_family {
  57. int id; /* private */
  58. unsigned int hdrsize;
  59. char name[GENL_NAMSIZ];
  60. unsigned int version;
  61. unsigned int maxattr;
  62. unsigned int mcgrp_offset; /* private */
  63. u8 netnsok:1;
  64. u8 parallel_ops:1;
  65. u8 n_ops;
  66. u8 n_small_ops;
  67. u8 n_mcgrps;
  68. u8 resv_start_op;
  69. const struct nla_policy *policy;
  70. int (*pre_doit)(const struct genl_ops *ops,
  71. struct sk_buff *skb,
  72. struct genl_info *info);
  73. void (*post_doit)(const struct genl_ops *ops,
  74. struct sk_buff *skb,
  75. struct genl_info *info);
  76. const struct genl_ops * ops;
  77. const struct genl_small_ops *small_ops;
  78. const struct genl_multicast_group *mcgrps;
  79. struct module *module;
  80. ANDROID_KABI_RESERVE(1);
  81. };
  82. /**
  83. * struct genl_info - receiving information
  84. * @snd_seq: sending sequence number
  85. * @snd_portid: netlink portid of sender
  86. * @nlhdr: netlink message header
  87. * @genlhdr: generic netlink message header
  88. * @userhdr: user specific header
  89. * @attrs: netlink attributes
  90. * @_net: network namespace
  91. * @user_ptr: user pointers
  92. * @extack: extended ACK report struct
  93. */
  94. struct genl_info {
  95. u32 snd_seq;
  96. u32 snd_portid;
  97. struct nlmsghdr * nlhdr;
  98. struct genlmsghdr * genlhdr;
  99. void * userhdr;
  100. struct nlattr ** attrs;
  101. possible_net_t _net;
  102. void * user_ptr[2];
  103. struct netlink_ext_ack *extack;
  104. };
  105. static inline struct net *genl_info_net(struct genl_info *info)
  106. {
  107. return read_pnet(&info->_net);
  108. }
  109. static inline void genl_info_net_set(struct genl_info *info, struct net *net)
  110. {
  111. write_pnet(&info->_net, net);
  112. }
  113. #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
  114. /* Report that a root attribute is missing */
  115. #define GENL_REQ_ATTR_CHECK(info, attr) ({ \
  116. struct genl_info *__info = (info); \
  117. \
  118. NL_REQ_ATTR_CHECK(__info->extack, NULL, __info->attrs, (attr)); \
  119. })
  120. enum genl_validate_flags {
  121. GENL_DONT_VALIDATE_STRICT = BIT(0),
  122. GENL_DONT_VALIDATE_DUMP = BIT(1),
  123. GENL_DONT_VALIDATE_DUMP_STRICT = BIT(2),
  124. };
  125. /**
  126. * struct genl_small_ops - generic netlink operations (small version)
  127. * @cmd: command identifier
  128. * @internal_flags: flags used by the family
  129. * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
  130. * @validate: validation flags from enum genl_validate_flags
  131. * @doit: standard command callback
  132. * @dumpit: callback for dumpers
  133. *
  134. * This is a cut-down version of struct genl_ops for users who don't need
  135. * most of the ancillary infra and want to save space.
  136. */
  137. struct genl_small_ops {
  138. int (*doit)(struct sk_buff *skb, struct genl_info *info);
  139. int (*dumpit)(struct sk_buff *skb, struct netlink_callback *cb);
  140. u8 cmd;
  141. u8 internal_flags;
  142. u8 flags;
  143. u8 validate;
  144. };
  145. /**
  146. * struct genl_ops - generic netlink operations
  147. * @cmd: command identifier
  148. * @internal_flags: flags used by the family
  149. * @flags: GENL_* flags (%GENL_ADMIN_PERM or %GENL_UNS_ADMIN_PERM)
  150. * @maxattr: maximum number of attributes supported
  151. * @policy: netlink policy (takes precedence over family policy)
  152. * @validate: validation flags from enum genl_validate_flags
  153. * @doit: standard command callback
  154. * @start: start callback for dumps
  155. * @dumpit: callback for dumpers
  156. * @done: completion callback for dumps
  157. */
  158. struct genl_ops {
  159. int (*doit)(struct sk_buff *skb,
  160. struct genl_info *info);
  161. int (*start)(struct netlink_callback *cb);
  162. int (*dumpit)(struct sk_buff *skb,
  163. struct netlink_callback *cb);
  164. int (*done)(struct netlink_callback *cb);
  165. const struct nla_policy *policy;
  166. unsigned int maxattr;
  167. u8 cmd;
  168. u8 internal_flags;
  169. u8 flags;
  170. u8 validate;
  171. ANDROID_KABI_RESERVE(1);
  172. };
  173. /**
  174. * struct genl_dumpit_info - info that is available during dumpit op call
  175. * @family: generic netlink family - for internal genl code usage
  176. * @op: generic netlink ops - for internal genl code usage
  177. * @attrs: netlink attributes
  178. */
  179. struct genl_dumpit_info {
  180. const struct genl_family *family;
  181. struct genl_ops op;
  182. struct nlattr **attrs;
  183. };
  184. static inline const struct genl_dumpit_info *
  185. genl_dumpit_info(struct netlink_callback *cb)
  186. {
  187. return cb->data;
  188. }
  189. int genl_register_family(struct genl_family *family);
  190. int genl_unregister_family(const struct genl_family *family);
  191. void genl_notify(const struct genl_family *family, struct sk_buff *skb,
  192. struct genl_info *info, u32 group, gfp_t flags);
  193. void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  194. const struct genl_family *family, int flags, u8 cmd);
  195. /**
  196. * genlmsg_nlhdr - Obtain netlink header from user specified header
  197. * @user_hdr: user header as returned from genlmsg_put()
  198. *
  199. * Returns pointer to netlink header.
  200. */
  201. static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
  202. {
  203. return (struct nlmsghdr *)((char *)user_hdr -
  204. GENL_HDRLEN -
  205. NLMSG_HDRLEN);
  206. }
  207. /**
  208. * genlmsg_parse_deprecated - parse attributes of a genetlink message
  209. * @nlh: netlink message header
  210. * @family: genetlink message family
  211. * @tb: destination array with maxtype+1 elements
  212. * @maxtype: maximum attribute type to be expected
  213. * @policy: validation policy
  214. * @extack: extended ACK report struct
  215. */
  216. static inline int genlmsg_parse_deprecated(const struct nlmsghdr *nlh,
  217. const struct genl_family *family,
  218. struct nlattr *tb[], int maxtype,
  219. const struct nla_policy *policy,
  220. struct netlink_ext_ack *extack)
  221. {
  222. return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
  223. policy, NL_VALIDATE_LIBERAL, extack);
  224. }
  225. /**
  226. * genlmsg_parse - parse attributes of a genetlink message
  227. * @nlh: netlink message header
  228. * @family: genetlink message family
  229. * @tb: destination array with maxtype+1 elements
  230. * @maxtype: maximum attribute type to be expected
  231. * @policy: validation policy
  232. * @extack: extended ACK report struct
  233. */
  234. static inline int genlmsg_parse(const struct nlmsghdr *nlh,
  235. const struct genl_family *family,
  236. struct nlattr *tb[], int maxtype,
  237. const struct nla_policy *policy,
  238. struct netlink_ext_ack *extack)
  239. {
  240. return __nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
  241. policy, NL_VALIDATE_STRICT, extack);
  242. }
  243. /**
  244. * genl_dump_check_consistent - check if sequence is consistent and advertise if not
  245. * @cb: netlink callback structure that stores the sequence number
  246. * @user_hdr: user header as returned from genlmsg_put()
  247. *
  248. * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
  249. * simpler to use with generic netlink.
  250. */
  251. static inline void genl_dump_check_consistent(struct netlink_callback *cb,
  252. void *user_hdr)
  253. {
  254. nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
  255. }
  256. /**
  257. * genlmsg_put_reply - Add generic netlink header to a reply message
  258. * @skb: socket buffer holding the message
  259. * @info: receiver info
  260. * @family: generic netlink family
  261. * @flags: netlink message flags
  262. * @cmd: generic netlink command
  263. *
  264. * Returns pointer to user specific header
  265. */
  266. static inline void *genlmsg_put_reply(struct sk_buff *skb,
  267. struct genl_info *info,
  268. const struct genl_family *family,
  269. int flags, u8 cmd)
  270. {
  271. return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
  272. flags, cmd);
  273. }
  274. /**
  275. * genlmsg_end - Finalize a generic netlink message
  276. * @skb: socket buffer the message is stored in
  277. * @hdr: user specific header
  278. */
  279. static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
  280. {
  281. nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  282. }
  283. /**
  284. * genlmsg_cancel - Cancel construction of a generic netlink message
  285. * @skb: socket buffer the message is stored in
  286. * @hdr: generic netlink message header
  287. */
  288. static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
  289. {
  290. if (hdr)
  291. nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  292. }
  293. /**
  294. * genlmsg_multicast_netns - multicast a netlink message to a specific netns
  295. * @family: the generic netlink family
  296. * @net: the net namespace
  297. * @skb: netlink message as socket buffer
  298. * @portid: own netlink portid to avoid sending to yourself
  299. * @group: offset of multicast group in groups array
  300. * @flags: allocation flags
  301. */
  302. static inline int genlmsg_multicast_netns(const struct genl_family *family,
  303. struct net *net, struct sk_buff *skb,
  304. u32 portid, unsigned int group, gfp_t flags)
  305. {
  306. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  307. return -EINVAL;
  308. group = family->mcgrp_offset + group;
  309. return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
  310. }
  311. /**
  312. * genlmsg_multicast - multicast a netlink message to the default netns
  313. * @family: the generic netlink family
  314. * @skb: netlink message as socket buffer
  315. * @portid: own netlink portid to avoid sending to yourself
  316. * @group: offset of multicast group in groups array
  317. * @flags: allocation flags
  318. */
  319. static inline int genlmsg_multicast(const struct genl_family *family,
  320. struct sk_buff *skb, u32 portid,
  321. unsigned int group, gfp_t flags)
  322. {
  323. return genlmsg_multicast_netns(family, &init_net, skb,
  324. portid, group, flags);
  325. }
  326. /**
  327. * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
  328. * @family: the generic netlink family
  329. * @skb: netlink message as socket buffer
  330. * @portid: own netlink portid to avoid sending to yourself
  331. * @group: offset of multicast group in groups array
  332. * @flags: allocation flags
  333. *
  334. * This function must hold the RTNL or rcu_read_lock().
  335. */
  336. int genlmsg_multicast_allns(const struct genl_family *family,
  337. struct sk_buff *skb, u32 portid,
  338. unsigned int group, gfp_t flags);
  339. /**
  340. * genlmsg_unicast - unicast a netlink message
  341. * @net: network namespace to look up @portid in
  342. * @skb: netlink message as socket buffer
  343. * @portid: netlink portid of the destination socket
  344. */
  345. static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
  346. {
  347. return nlmsg_unicast(net->genl_sock, skb, portid);
  348. }
  349. /**
  350. * genlmsg_reply - reply to a request
  351. * @skb: netlink message to be sent back
  352. * @info: receiver information
  353. */
  354. static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
  355. {
  356. return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
  357. }
  358. /**
  359. * genlmsg_data - head of message payload
  360. * @gnlh: genetlink message header
  361. */
  362. static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
  363. {
  364. return ((unsigned char *) gnlh + GENL_HDRLEN);
  365. }
  366. /**
  367. * genlmsg_len - length of message payload
  368. * @gnlh: genetlink message header
  369. */
  370. static inline int genlmsg_len(const struct genlmsghdr *gnlh)
  371. {
  372. struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
  373. NLMSG_HDRLEN);
  374. return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
  375. }
  376. /**
  377. * genlmsg_msg_size - length of genetlink message not including padding
  378. * @payload: length of message payload
  379. */
  380. static inline int genlmsg_msg_size(int payload)
  381. {
  382. return GENL_HDRLEN + payload;
  383. }
  384. /**
  385. * genlmsg_total_size - length of genetlink message including padding
  386. * @payload: length of message payload
  387. */
  388. static inline int genlmsg_total_size(int payload)
  389. {
  390. return NLMSG_ALIGN(genlmsg_msg_size(payload));
  391. }
  392. /**
  393. * genlmsg_new - Allocate a new generic netlink message
  394. * @payload: size of the message payload
  395. * @flags: the type of memory to allocate.
  396. */
  397. static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
  398. {
  399. return nlmsg_new(genlmsg_total_size(payload), flags);
  400. }
  401. /**
  402. * genl_set_err - report error to genetlink broadcast listeners
  403. * @family: the generic netlink family
  404. * @net: the network namespace to report the error to
  405. * @portid: the PORTID of a process that we want to skip (if any)
  406. * @group: the broadcast group that will notice the error
  407. * (this is the offset of the multicast group in the groups array)
  408. * @code: error code, must be negative (as usual in kernelspace)
  409. *
  410. * This function returns the number of broadcast listeners that have set the
  411. * NETLINK_RECV_NO_ENOBUFS socket option.
  412. */
  413. static inline int genl_set_err(const struct genl_family *family,
  414. struct net *net, u32 portid,
  415. u32 group, int code)
  416. {
  417. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  418. return -EINVAL;
  419. group = family->mcgrp_offset + group;
  420. return netlink_set_err(net->genl_sock, portid, group, code);
  421. }
  422. static inline int genl_has_listeners(const struct genl_family *family,
  423. struct net *net, unsigned int group)
  424. {
  425. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  426. return -EINVAL;
  427. group = family->mcgrp_offset + group;
  428. return netlink_has_listeners(net->genl_sock, group);
  429. }
  430. #endif /* __NET_GENERIC_NETLINK_H */