genetlink.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. * @family: generic netlink family
  134. *
  135. * Returns pointer to netlink header.
  136. */
  137. static inline struct nlmsghdr *
  138. genlmsg_nlhdr(void *user_hdr, const struct genl_family *family)
  139. {
  140. return (struct nlmsghdr *)((char *)user_hdr -
  141. family->hdrsize -
  142. GENL_HDRLEN -
  143. NLMSG_HDRLEN);
  144. }
  145. /**
  146. * genlmsg_parse - parse attributes of a genetlink message
  147. * @nlh: netlink message header
  148. * @family: genetlink message family
  149. * @tb: destination array with maxtype+1 elements
  150. * @maxtype: maximum attribute type to be expected
  151. * @policy: validation policy
  152. * @extack: extended ACK report struct
  153. */
  154. static inline int genlmsg_parse(const struct nlmsghdr *nlh,
  155. const struct genl_family *family,
  156. struct nlattr *tb[], int maxtype,
  157. const struct nla_policy *policy,
  158. struct netlink_ext_ack *extack)
  159. {
  160. return nlmsg_parse(nlh, family->hdrsize + GENL_HDRLEN, tb, maxtype,
  161. policy, extack);
  162. }
  163. /**
  164. * genl_dump_check_consistent - check if sequence is consistent and advertise if not
  165. * @cb: netlink callback structure that stores the sequence number
  166. * @user_hdr: user header as returned from genlmsg_put()
  167. * @family: generic netlink family
  168. *
  169. * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
  170. * simpler to use with generic netlink.
  171. */
  172. static inline void genl_dump_check_consistent(struct netlink_callback *cb,
  173. void *user_hdr,
  174. const struct genl_family *family)
  175. {
  176. nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr, family));
  177. }
  178. /**
  179. * genlmsg_put_reply - Add generic netlink header to a reply message
  180. * @skb: socket buffer holding the message
  181. * @info: receiver info
  182. * @family: generic netlink family
  183. * @flags: netlink message flags
  184. * @cmd: generic netlink command
  185. *
  186. * Returns pointer to user specific header
  187. */
  188. static inline void *genlmsg_put_reply(struct sk_buff *skb,
  189. struct genl_info *info,
  190. const struct genl_family *family,
  191. int flags, u8 cmd)
  192. {
  193. return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
  194. flags, cmd);
  195. }
  196. /**
  197. * genlmsg_end - Finalize a generic netlink message
  198. * @skb: socket buffer the message is stored in
  199. * @hdr: user specific header
  200. */
  201. static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
  202. {
  203. nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  204. }
  205. /**
  206. * genlmsg_cancel - Cancel construction of a generic netlink message
  207. * @skb: socket buffer the message is stored in
  208. * @hdr: generic netlink message header
  209. */
  210. static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
  211. {
  212. if (hdr)
  213. nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  214. }
  215. /**
  216. * genlmsg_multicast_netns - multicast a netlink message to a specific netns
  217. * @family: the generic netlink family
  218. * @net: the net namespace
  219. * @skb: netlink message as socket buffer
  220. * @portid: own netlink portid to avoid sending to yourself
  221. * @group: offset of multicast group in groups array
  222. * @flags: allocation flags
  223. */
  224. static inline int genlmsg_multicast_netns(const struct genl_family *family,
  225. struct net *net, struct sk_buff *skb,
  226. u32 portid, unsigned int group, gfp_t flags)
  227. {
  228. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  229. return -EINVAL;
  230. group = family->mcgrp_offset + group;
  231. return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
  232. }
  233. /**
  234. * genlmsg_multicast - multicast a netlink message to the default netns
  235. * @family: the generic netlink family
  236. * @skb: netlink message as socket buffer
  237. * @portid: own netlink portid to avoid sending to yourself
  238. * @group: offset of multicast group in groups array
  239. * @flags: allocation flags
  240. */
  241. static inline int genlmsg_multicast(const struct genl_family *family,
  242. struct sk_buff *skb, u32 portid,
  243. unsigned int group, gfp_t flags)
  244. {
  245. return genlmsg_multicast_netns(family, &init_net, skb,
  246. portid, group, flags);
  247. }
  248. /**
  249. * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
  250. * @family: the generic netlink family
  251. * @skb: netlink message as socket buffer
  252. * @portid: own netlink portid to avoid sending to yourself
  253. * @group: offset of multicast group in groups array
  254. * @flags: allocation flags
  255. *
  256. * This function must hold the RTNL or rcu_read_lock().
  257. */
  258. int genlmsg_multicast_allns(const struct genl_family *family,
  259. struct sk_buff *skb, u32 portid,
  260. unsigned int group, gfp_t flags);
  261. /**
  262. * genlmsg_unicast - unicast a netlink message
  263. * @skb: netlink message as socket buffer
  264. * @portid: netlink portid of the destination socket
  265. */
  266. static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
  267. {
  268. return nlmsg_unicast(net->genl_sock, skb, portid);
  269. }
  270. /**
  271. * genlmsg_reply - reply to a request
  272. * @skb: netlink message to be sent back
  273. * @info: receiver information
  274. */
  275. static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
  276. {
  277. return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
  278. }
  279. /**
  280. * gennlmsg_data - head of message payload
  281. * @gnlh: genetlink message header
  282. */
  283. static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
  284. {
  285. return ((unsigned char *) gnlh + GENL_HDRLEN);
  286. }
  287. /**
  288. * genlmsg_len - length of message payload
  289. * @gnlh: genetlink message header
  290. */
  291. static inline int genlmsg_len(const struct genlmsghdr *gnlh)
  292. {
  293. struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
  294. NLMSG_HDRLEN);
  295. return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
  296. }
  297. /**
  298. * genlmsg_msg_size - length of genetlink message not including padding
  299. * @payload: length of message payload
  300. */
  301. static inline int genlmsg_msg_size(int payload)
  302. {
  303. return GENL_HDRLEN + payload;
  304. }
  305. /**
  306. * genlmsg_total_size - length of genetlink message including padding
  307. * @payload: length of message payload
  308. */
  309. static inline int genlmsg_total_size(int payload)
  310. {
  311. return NLMSG_ALIGN(genlmsg_msg_size(payload));
  312. }
  313. /**
  314. * genlmsg_new - Allocate a new generic netlink message
  315. * @payload: size of the message payload
  316. * @flags: the type of memory to allocate.
  317. */
  318. static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
  319. {
  320. return nlmsg_new(genlmsg_total_size(payload), flags);
  321. }
  322. /**
  323. * genl_set_err - report error to genetlink broadcast listeners
  324. * @family: the generic netlink family
  325. * @net: the network namespace to report the error to
  326. * @portid: the PORTID of a process that we want to skip (if any)
  327. * @group: the broadcast group that will notice the error
  328. * (this is the offset of the multicast group in the groups array)
  329. * @code: error code, must be negative (as usual in kernelspace)
  330. *
  331. * This function returns the number of broadcast listeners that have set the
  332. * NETLINK_RECV_NO_ENOBUFS socket option.
  333. */
  334. static inline int genl_set_err(const struct genl_family *family,
  335. struct net *net, u32 portid,
  336. u32 group, int code)
  337. {
  338. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  339. return -EINVAL;
  340. group = family->mcgrp_offset + group;
  341. return netlink_set_err(net->genl_sock, portid, group, code);
  342. }
  343. static inline int genl_has_listeners(const struct genl_family *family,
  344. struct net *net, unsigned int group)
  345. {
  346. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  347. return -EINVAL;
  348. group = family->mcgrp_offset + group;
  349. return netlink_has_listeners(net->genl_sock, group);
  350. }
  351. #endif /* __NET_GENERIC_NETLINK_H */