PageRenderTime 29ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/include/net/genetlink.h

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