genetlink.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #ifndef __NET_GENERIC_NETLINK_H
  2. #define __NET_GENERIC_NETLINK_H
  3. #include <linux/genetlink.h>
  4. #include <net/netlink.h>
  5. #include <net/net_namespace.h>
  6. #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
  7. /**
  8. * struct genl_multicast_group - generic netlink multicast group
  9. * @name: name of the multicast group, names are per-family
  10. */
  11. struct genl_multicast_group {
  12. char name[GENL_NAMSIZ];
  13. };
  14. struct genl_ops;
  15. struct genl_info;
  16. /**
  17. * struct genl_family - generic netlink family
  18. * @id: protocol family idenfitier
  19. * @hdrsize: length of user specific header in bytes
  20. * @name: name of family
  21. * @version: protocol version
  22. * @maxattr: maximum number of attributes supported
  23. * @netnsok: set to true if the family can handle network
  24. * namespaces and should be presented in all of them
  25. * @pre_doit: called before an operation's doit callback, it may
  26. * do additional, common, filtering and return an error
  27. * @post_doit: called after an operation's doit callback, it may
  28. * undo operations done by pre_doit, for example release locks
  29. * @attrbuf: buffer to store parsed attributes
  30. * @family_list: family list
  31. * @mcgrps: multicast groups used by this family (private)
  32. * @n_mcgrps: number of multicast groups (private)
  33. * @mcgrp_offset: starting number of multicast group IDs in this family
  34. * @ops: the operations supported by this family (private)
  35. * @n_ops: number of operations supported by this family (private)
  36. */
  37. struct genl_family {
  38. unsigned int id;
  39. unsigned int hdrsize;
  40. char name[GENL_NAMSIZ];
  41. unsigned int version;
  42. unsigned int maxattr;
  43. bool netnsok;
  44. bool parallel_ops;
  45. int (*pre_doit)(const struct genl_ops *ops,
  46. struct sk_buff *skb,
  47. struct genl_info *info);
  48. void (*post_doit)(const struct genl_ops *ops,
  49. struct sk_buff *skb,
  50. struct genl_info *info);
  51. struct nlattr ** attrbuf; /* private */
  52. const struct genl_ops * ops; /* private */
  53. const struct genl_multicast_group *mcgrps; /* private */
  54. unsigned int n_ops; /* private */
  55. unsigned int n_mcgrps; /* private */
  56. unsigned int mcgrp_offset; /* private */
  57. struct list_head family_list; /* private */
  58. struct module *module;
  59. };
  60. /**
  61. * struct genl_info - receiving information
  62. * @snd_seq: sending sequence number
  63. * @snd_portid: netlink portid of sender
  64. * @nlhdr: netlink message header
  65. * @genlhdr: generic netlink message header
  66. * @userhdr: user specific header
  67. * @attrs: netlink attributes
  68. * @_net: network namespace
  69. * @user_ptr: user pointers
  70. */
  71. struct genl_info {
  72. u32 snd_seq;
  73. u32 snd_portid;
  74. struct nlmsghdr * nlhdr;
  75. struct genlmsghdr * genlhdr;
  76. void * userhdr;
  77. struct nlattr ** attrs;
  78. #ifdef CONFIG_NET_NS
  79. struct net * _net;
  80. #endif
  81. void * user_ptr[2];
  82. };
  83. static inline struct net *genl_info_net(struct genl_info *info)
  84. {
  85. return read_pnet(&info->_net);
  86. }
  87. static inline void genl_info_net_set(struct genl_info *info, struct net *net)
  88. {
  89. write_pnet(&info->_net, net);
  90. }
  91. /**
  92. * struct genl_ops - generic netlink operations
  93. * @cmd: command identifier
  94. * @internal_flags: flags used by the family
  95. * @flags: flags
  96. * @policy: attribute validation policy
  97. * @doit: standard command callback
  98. * @dumpit: callback for dumpers
  99. * @done: completion callback for dumps
  100. * @ops_list: operations list
  101. */
  102. struct genl_ops {
  103. const struct nla_policy *policy;
  104. int (*doit)(struct sk_buff *skb,
  105. struct genl_info *info);
  106. int (*dumpit)(struct sk_buff *skb,
  107. struct netlink_callback *cb);
  108. int (*done)(struct netlink_callback *cb);
  109. u8 cmd;
  110. u8 internal_flags;
  111. u8 flags;
  112. };
  113. int __genl_register_family(struct genl_family *family);
  114. static inline int genl_register_family(struct genl_family *family)
  115. {
  116. family->module = THIS_MODULE;
  117. return __genl_register_family(family);
  118. }
  119. /**
  120. * genl_register_family_with_ops - register a generic netlink family with ops
  121. * @family: generic netlink family
  122. * @ops: operations to be registered
  123. * @n_ops: number of elements to register
  124. *
  125. * Registers the specified family and operations from the specified table.
  126. * Only one family may be registered with the same family name or identifier.
  127. *
  128. * The family id may equal GENL_ID_GENERATE causing an unique id to
  129. * be automatically generated and assigned.
  130. *
  131. * Either a doit or dumpit callback must be specified for every registered
  132. * operation or the function will fail. Only one operation structure per
  133. * command identifier may be registered.
  134. *
  135. * See include/net/genetlink.h for more documenation on the operations
  136. * structure.
  137. *
  138. * Return 0 on success or a negative error code.
  139. */
  140. static inline int
  141. _genl_register_family_with_ops_grps(struct genl_family *family,
  142. const struct genl_ops *ops, size_t n_ops,
  143. const struct genl_multicast_group *mcgrps,
  144. size_t n_mcgrps)
  145. {
  146. family->module = THIS_MODULE;
  147. family->ops = ops;
  148. family->n_ops = n_ops;
  149. family->mcgrps = mcgrps;
  150. family->n_mcgrps = n_mcgrps;
  151. return __genl_register_family(family);
  152. }
  153. #define genl_register_family_with_ops(family, ops) \
  154. _genl_register_family_with_ops_grps((family), \
  155. (ops), ARRAY_SIZE(ops), \
  156. NULL, 0)
  157. #define genl_register_family_with_ops_groups(family, ops, grps) \
  158. _genl_register_family_with_ops_grps((family), \
  159. (ops), ARRAY_SIZE(ops), \
  160. (grps), ARRAY_SIZE(grps))
  161. int genl_unregister_family(struct genl_family *family);
  162. void genl_notify(struct genl_family *family,
  163. struct sk_buff *skb, struct net *net, u32 portid,
  164. u32 group, struct nlmsghdr *nlh, gfp_t flags);
  165. void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  166. struct genl_family *family, int flags, u8 cmd);
  167. /**
  168. * genlmsg_nlhdr - Obtain netlink header from user specified header
  169. * @user_hdr: user header as returned from genlmsg_put()
  170. * @family: generic netlink family
  171. *
  172. * Returns pointer to netlink header.
  173. */
  174. static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr,
  175. struct genl_family *family)
  176. {
  177. return (struct nlmsghdr *)((char *)user_hdr -
  178. family->hdrsize -
  179. GENL_HDRLEN -
  180. NLMSG_HDRLEN);
  181. }
  182. /**
  183. * genl_dump_check_consistent - check if sequence is consistent and advertise if not
  184. * @cb: netlink callback structure that stores the sequence number
  185. * @user_hdr: user header as returned from genlmsg_put()
  186. * @family: generic netlink family
  187. *
  188. * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
  189. * simpler to use with generic netlink.
  190. */
  191. static inline void genl_dump_check_consistent(struct netlink_callback *cb,
  192. void *user_hdr,
  193. struct genl_family *family)
  194. {
  195. nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr, family));
  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. 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 int genlmsg_end(struct sk_buff *skb, void *hdr)
  221. {
  222. return 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(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(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(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(struct genl_family *family, struct net *net,
  354. u32 portid, u32 group, int code)
  355. {
  356. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  357. return -EINVAL;
  358. group = family->mcgrp_offset + group;
  359. return netlink_set_err(net->genl_sock, portid, group, code);
  360. }
  361. #endif /* __NET_GENERIC_NETLINK_H */