genetlink.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 <net/netlink.h>
  6. #include <net/net_namespace.h>
  7. #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
  8. /**
  9. * struct genl_multicast_group - generic netlink multicast group
  10. * @name: name of the multicast group, names are per-family
  11. */
  12. struct genl_multicast_group {
  13. char name[GENL_NAMSIZ];
  14. };
  15. struct genl_ops;
  16. struct genl_info;
  17. /**
  18. * struct genl_family - generic netlink family
  19. * @id: protocol family identifier (private)
  20. * @hdrsize: length of user specific header in bytes
  21. * @name: name of family
  22. * @version: protocol version
  23. * @maxattr: maximum number of attributes supported
  24. * @netnsok: set to true if the family can handle network
  25. * namespaces and should be presented in all of them
  26. * @parallel_ops: operations can be called in parallel and aren't
  27. * synchronized by the core genetlink code
  28. * @pre_doit: called before an operation's doit callback, it may
  29. * do additional, common, filtering and return an error
  30. * @post_doit: called after an operation's doit callback, it may
  31. * undo operations done by pre_doit, for example release locks
  32. * @attrbuf: buffer to store parsed attributes (private)
  33. * @mcgrps: multicast groups used by this family
  34. * @n_mcgrps: number of multicast groups
  35. * @mcgrp_offset: starting number of multicast group IDs in this family
  36. * (private)
  37. * @ops: the operations supported by this family
  38. * @n_ops: number of operations supported by this family
  39. */
  40. struct genl_family {
  41. int id; /* private */
  42. unsigned int hdrsize;
  43. char name[GENL_NAMSIZ];
  44. unsigned int version;
  45. unsigned int maxattr;
  46. bool netnsok;
  47. bool parallel_ops;
  48. int (*pre_doit)(const struct genl_ops *ops,
  49. struct sk_buff *skb,
  50. struct genl_info *info);
  51. void (*post_doit)(const struct genl_ops *ops,
  52. struct sk_buff *skb,
  53. struct genl_info *info);
  54. struct nlattr ** attrbuf; /* private */
  55. const struct genl_ops * ops;
  56. const struct genl_multicast_group *mcgrps;
  57. unsigned int n_ops;
  58. unsigned int n_mcgrps;
  59. unsigned int mcgrp_offset; /* private */
  60. struct module *module;
  61. };
  62. struct nlattr **genl_family_attrbuf(const struct genl_family *family);
  63. /**
  64. * struct genl_info - receiving information
  65. * @snd_seq: sending sequence number
  66. * @snd_portid: netlink portid of sender
  67. * @nlhdr: netlink message header
  68. * @genlhdr: generic netlink message header
  69. * @userhdr: user specific header
  70. * @attrs: netlink attributes
  71. * @_net: network namespace
  72. * @user_ptr: user pointers
  73. * @extack: extended ACK report struct
  74. */
  75. struct genl_info {
  76. u32 snd_seq;
  77. u32 snd_portid;
  78. struct nlmsghdr * nlhdr;
  79. struct genlmsghdr * genlhdr;
  80. void * userhdr;
  81. struct nlattr ** attrs;
  82. possible_net_t _net;
  83. void * user_ptr[2];
  84. struct netlink_ext_ack *extack;
  85. };
  86. static inline struct net *genl_info_net(struct genl_info *info)
  87. {
  88. return read_pnet(&info->_net);
  89. }
  90. static inline void genl_info_net_set(struct genl_info *info, struct net *net)
  91. {
  92. write_pnet(&info->_net, net);
  93. }
  94. #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG((info)->extack, msg)
  95. static inline int genl_err_attr(struct genl_info *info, int err,
  96. struct nlattr *attr)
  97. {
  98. info->extack->bad_attr = attr;
  99. return err;
  100. }
  101. /**
  102. * struct genl_ops - generic netlink operations
  103. * @cmd: command identifier
  104. * @internal_flags: flags used by the family
  105. * @flags: flags
  106. * @policy: attribute validation policy
  107. * @doit: standard command callback
  108. * @start: start callback for dumps
  109. * @dumpit: callback for dumpers
  110. * @done: completion callback for dumps
  111. */
  112. struct genl_ops {
  113. const struct nla_policy *policy;
  114. int (*doit)(struct sk_buff *skb,
  115. struct genl_info *info);
  116. int (*start)(struct netlink_callback *cb);
  117. int (*dumpit)(struct sk_buff *skb,
  118. struct netlink_callback *cb);
  119. int (*done)(struct netlink_callback *cb);
  120. u8 cmd;
  121. u8 internal_flags;
  122. u8 flags;
  123. };
  124. int genl_register_family(struct genl_family *family);
  125. int genl_unregister_family(const struct genl_family *family);
  126. void genl_notify(const struct genl_family *family, struct sk_buff *skb,
  127. struct genl_info *info, u32 group, gfp_t flags);
  128. void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  129. const struct genl_family *family, int flags, u8 cmd);
  130. /**
  131. * genlmsg_nlhdr - Obtain netlink header from user specified header
  132. * @user_hdr: user header as returned from genlmsg_put()
  133. *
  134. * Returns pointer to netlink header.
  135. */
  136. static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
  137. {
  138. return (struct nlmsghdr *)((char *)user_hdr -
  139. GENL_HDRLEN -
  140. NLMSG_HDRLEN);
  141. }
  142. /**
  143. * genlmsg_parse - parse attributes of a genetlink message
  144. * @nlh: netlink message header
  145. * @family: genetlink message family
  146. * @tb: destination array with maxtype+1 elements
  147. * @maxtype: maximum attribute type to be expected
  148. * @policy: validation policy
  149. * @extack: extended ACK report struct
  150. */
  151. static inline int genlmsg_parse(const struct nlmsghdr *nlh,
  152. const struct genl_family *family,
  153. struct nlattr *tb[], int maxtype,
  154. const struct nla_policy *policy,
  155. struct netlink_ext_ack *extack)
  156. {
  157. return nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
  158. policy, extack);
  159. }
  160. /**
  161. * genl_dump_check_consistent - check if sequence is consistent and advertise if not
  162. * @cb: netlink callback structure that stores the sequence number
  163. * @user_hdr: user header as returned from genlmsg_put()
  164. *
  165. * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
  166. * simpler to use with generic netlink.
  167. */
  168. static inline void genl_dump_check_consistent(struct netlink_callback *cb,
  169. void *user_hdr)
  170. {
  171. nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr));
  172. }
  173. /**
  174. * genlmsg_put_reply - Add generic netlink header to a reply message
  175. * @skb: socket buffer holding the message
  176. * @info: receiver info
  177. * @family: generic netlink family
  178. * @flags: netlink message flags
  179. * @cmd: generic netlink command
  180. *
  181. * Returns pointer to user specific header
  182. */
  183. static inline void *genlmsg_put_reply(struct sk_buff *skb,
  184. struct genl_info *info,
  185. const struct genl_family *family,
  186. int flags, u8 cmd)
  187. {
  188. return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
  189. flags, cmd);
  190. }
  191. /**
  192. * genlmsg_end - Finalize a generic netlink message
  193. * @skb: socket buffer the message is stored in
  194. * @hdr: user specific header
  195. */
  196. static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
  197. {
  198. nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  199. }
  200. /**
  201. * genlmsg_cancel - Cancel construction of a generic netlink message
  202. * @skb: socket buffer the message is stored in
  203. * @hdr: generic netlink message header
  204. */
  205. static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
  206. {
  207. if (hdr)
  208. nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  209. }
  210. /**
  211. * genlmsg_multicast_netns - multicast a netlink message to a specific netns
  212. * @family: the generic netlink family
  213. * @net: the net namespace
  214. * @skb: netlink message as socket buffer
  215. * @portid: own netlink portid to avoid sending to yourself
  216. * @group: offset of multicast group in groups array
  217. * @flags: allocation flags
  218. */
  219. static inline int genlmsg_multicast_netns(const struct genl_family *family,
  220. struct net *net, struct sk_buff *skb,
  221. u32 portid, unsigned int group, gfp_t flags)
  222. {
  223. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  224. return -EINVAL;
  225. group = family->mcgrp_offset + group;
  226. return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
  227. }
  228. /**
  229. * genlmsg_multicast - multicast a netlink message to the default netns
  230. * @family: the generic netlink family
  231. * @skb: netlink message as socket buffer
  232. * @portid: own netlink portid to avoid sending to yourself
  233. * @group: offset of multicast group in groups array
  234. * @flags: allocation flags
  235. */
  236. static inline int genlmsg_multicast(const struct genl_family *family,
  237. struct sk_buff *skb, u32 portid,
  238. unsigned int group, gfp_t flags)
  239. {
  240. return genlmsg_multicast_netns(family, &init_net, skb,
  241. portid, group, flags);
  242. }
  243. /**
  244. * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
  245. * @family: the generic netlink family
  246. * @skb: netlink message as socket buffer
  247. * @portid: own netlink portid to avoid sending to yourself
  248. * @group: offset of multicast group in groups array
  249. * @flags: allocation flags
  250. *
  251. * This function must hold the RTNL or rcu_read_lock().
  252. */
  253. int genlmsg_multicast_allns(const struct genl_family *family,
  254. struct sk_buff *skb, u32 portid,
  255. unsigned int group, gfp_t flags);
  256. /**
  257. * genlmsg_unicast - unicast a netlink message
  258. * @skb: netlink message as socket buffer
  259. * @portid: netlink portid of the destination socket
  260. */
  261. static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
  262. {
  263. return nlmsg_unicast(net->genl_sock, skb, portid);
  264. }
  265. /**
  266. * genlmsg_reply - reply to a request
  267. * @skb: netlink message to be sent back
  268. * @info: receiver information
  269. */
  270. static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
  271. {
  272. return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
  273. }
  274. /**
  275. * gennlmsg_data - head of message payload
  276. * @gnlh: genetlink message header
  277. */
  278. static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
  279. {
  280. return ((unsigned char *) gnlh + GENL_HDRLEN);
  281. }
  282. /**
  283. * genlmsg_len - length of message payload
  284. * @gnlh: genetlink message header
  285. */
  286. static inline int genlmsg_len(const struct genlmsghdr *gnlh)
  287. {
  288. struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
  289. NLMSG_HDRLEN);
  290. return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
  291. }
  292. /**
  293. * genlmsg_msg_size - length of genetlink message not including padding
  294. * @payload: length of message payload
  295. */
  296. static inline int genlmsg_msg_size(int payload)
  297. {
  298. return GENL_HDRLEN + payload;
  299. }
  300. /**
  301. * genlmsg_total_size - length of genetlink message including padding
  302. * @payload: length of message payload
  303. */
  304. static inline int genlmsg_total_size(int payload)
  305. {
  306. return NLMSG_ALIGN(genlmsg_msg_size(payload));
  307. }
  308. /**
  309. * genlmsg_new - Allocate a new generic netlink message
  310. * @payload: size of the message payload
  311. * @flags: the type of memory to allocate.
  312. */
  313. static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
  314. {
  315. return nlmsg_new(genlmsg_total_size(payload), flags);
  316. }
  317. /**
  318. * genl_set_err - report error to genetlink broadcast listeners
  319. * @family: the generic netlink family
  320. * @net: the network namespace to report the error to
  321. * @portid: the PORTID of a process that we want to skip (if any)
  322. * @group: the broadcast group that will notice the error
  323. * (this is the offset of the multicast group in the groups array)
  324. * @code: error code, must be negative (as usual in kernelspace)
  325. *
  326. * This function returns the number of broadcast listeners that have set the
  327. * NETLINK_RECV_NO_ENOBUFS socket option.
  328. */
  329. static inline int genl_set_err(const struct genl_family *family,
  330. struct net *net, u32 portid,
  331. u32 group, int code)
  332. {
  333. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  334. return -EINVAL;
  335. group = family->mcgrp_offset + group;
  336. return netlink_set_err(net->genl_sock, portid, group, code);
  337. }
  338. static inline int genl_has_listeners(const struct genl_family *family,
  339. struct net *net, unsigned int group)
  340. {
  341. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  342. return -EINVAL;
  343. group = family->mcgrp_offset + group;
  344. return netlink_has_listeners(net->genl_sock, group);
  345. }
  346. #endif /* __NET_GENERIC_NETLINK_H */