genetlink.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. * @dst_sk: destination socket
  71. */
  72. struct genl_info {
  73. u32 snd_seq;
  74. u32 snd_portid;
  75. struct nlmsghdr * nlhdr;
  76. struct genlmsghdr * genlhdr;
  77. void * userhdr;
  78. struct nlattr ** attrs;
  79. #ifdef CONFIG_NET_NS
  80. struct net * _net;
  81. #endif
  82. void * user_ptr[2];
  83. struct sock * dst_sk;
  84. };
  85. static inline struct net *genl_info_net(struct genl_info *info)
  86. {
  87. return read_pnet(&info->_net);
  88. }
  89. static inline void genl_info_net_set(struct genl_info *info, struct net *net)
  90. {
  91. write_pnet(&info->_net, net);
  92. }
  93. /**
  94. * struct genl_ops - generic netlink operations
  95. * @cmd: command identifier
  96. * @internal_flags: flags used by the family
  97. * @flags: flags
  98. * @policy: attribute validation policy
  99. * @doit: standard command callback
  100. * @dumpit: callback for dumpers
  101. * @done: completion callback for dumps
  102. * @ops_list: operations list
  103. */
  104. struct genl_ops {
  105. const struct nla_policy *policy;
  106. int (*doit)(struct sk_buff *skb,
  107. struct genl_info *info);
  108. int (*dumpit)(struct sk_buff *skb,
  109. struct netlink_callback *cb);
  110. int (*done)(struct netlink_callback *cb);
  111. u8 cmd;
  112. u8 internal_flags;
  113. u8 flags;
  114. };
  115. int __genl_register_family(struct genl_family *family);
  116. static inline int genl_register_family(struct genl_family *family)
  117. {
  118. family->module = THIS_MODULE;
  119. return __genl_register_family(family);
  120. }
  121. /**
  122. * genl_register_family_with_ops - register a generic netlink family with ops
  123. * @family: generic netlink family
  124. * @ops: operations to be registered
  125. * @n_ops: number of elements to register
  126. *
  127. * Registers the specified family and operations from the specified table.
  128. * Only one family may be registered with the same family name or identifier.
  129. *
  130. * The family id may equal GENL_ID_GENERATE causing an unique id to
  131. * be automatically generated and assigned.
  132. *
  133. * Either a doit or dumpit callback must be specified for every registered
  134. * operation or the function will fail. Only one operation structure per
  135. * command identifier may be registered.
  136. *
  137. * See include/net/genetlink.h for more documenation on the operations
  138. * structure.
  139. *
  140. * Return 0 on success or a negative error code.
  141. */
  142. static inline int
  143. _genl_register_family_with_ops_grps(struct genl_family *family,
  144. const struct genl_ops *ops, size_t n_ops,
  145. const struct genl_multicast_group *mcgrps,
  146. size_t n_mcgrps)
  147. {
  148. family->module = THIS_MODULE;
  149. family->ops = ops;
  150. family->n_ops = n_ops;
  151. family->mcgrps = mcgrps;
  152. family->n_mcgrps = n_mcgrps;
  153. return __genl_register_family(family);
  154. }
  155. #define genl_register_family_with_ops(family, ops) \
  156. _genl_register_family_with_ops_grps((family), \
  157. (ops), ARRAY_SIZE(ops), \
  158. NULL, 0)
  159. #define genl_register_family_with_ops_groups(family, ops, grps) \
  160. _genl_register_family_with_ops_grps((family), \
  161. (ops), ARRAY_SIZE(ops), \
  162. (grps), ARRAY_SIZE(grps))
  163. int genl_unregister_family(struct genl_family *family);
  164. void genl_notify(struct genl_family *family,
  165. struct sk_buff *skb, struct net *net, u32 portid,
  166. u32 group, struct nlmsghdr *nlh, gfp_t flags);
  167. struct sk_buff *genlmsg_new_unicast(size_t payload, struct genl_info *info,
  168. gfp_t flags);
  169. void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
  170. struct genl_family *family, int flags, u8 cmd);
  171. /**
  172. * genlmsg_nlhdr - Obtain netlink header from user specified header
  173. * @user_hdr: user header as returned from genlmsg_put()
  174. * @family: generic netlink family
  175. *
  176. * Returns pointer to netlink header.
  177. */
  178. static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr,
  179. struct genl_family *family)
  180. {
  181. return (struct nlmsghdr *)((char *)user_hdr -
  182. family->hdrsize -
  183. GENL_HDRLEN -
  184. NLMSG_HDRLEN);
  185. }
  186. /**
  187. * genl_dump_check_consistent - check if sequence is consistent and advertise if not
  188. * @cb: netlink callback structure that stores the sequence number
  189. * @user_hdr: user header as returned from genlmsg_put()
  190. * @family: generic netlink family
  191. *
  192. * Cf. nl_dump_check_consistent(), this just provides a wrapper to make it
  193. * simpler to use with generic netlink.
  194. */
  195. static inline void genl_dump_check_consistent(struct netlink_callback *cb,
  196. void *user_hdr,
  197. struct genl_family *family)
  198. {
  199. nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr, family));
  200. }
  201. /**
  202. * genlmsg_put_reply - Add generic netlink header to a reply message
  203. * @skb: socket buffer holding the message
  204. * @info: receiver info
  205. * @family: generic netlink family
  206. * @flags: netlink message flags
  207. * @cmd: generic netlink command
  208. *
  209. * Returns pointer to user specific header
  210. */
  211. static inline void *genlmsg_put_reply(struct sk_buff *skb,
  212. struct genl_info *info,
  213. struct genl_family *family,
  214. int flags, u8 cmd)
  215. {
  216. return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
  217. flags, cmd);
  218. }
  219. /**
  220. * genlmsg_end - Finalize a generic netlink message
  221. * @skb: socket buffer the message is stored in
  222. * @hdr: user specific header
  223. */
  224. static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
  225. {
  226. return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  227. }
  228. /**
  229. * genlmsg_cancel - Cancel construction of a generic netlink message
  230. * @skb: socket buffer the message is stored in
  231. * @hdr: generic netlink message header
  232. */
  233. static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
  234. {
  235. if (hdr)
  236. nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  237. }
  238. /**
  239. * genlmsg_multicast_netns - multicast a netlink message to a specific netns
  240. * @family: the generic netlink family
  241. * @net: the net namespace
  242. * @skb: netlink message as socket buffer
  243. * @portid: own netlink portid to avoid sending to yourself
  244. * @group: offset of multicast group in groups array
  245. * @flags: allocation flags
  246. */
  247. static inline int genlmsg_multicast_netns(struct genl_family *family,
  248. struct net *net, struct sk_buff *skb,
  249. u32 portid, unsigned int group, gfp_t flags)
  250. {
  251. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  252. return -EINVAL;
  253. group = family->mcgrp_offset + group;
  254. return nlmsg_multicast(net->genl_sock, skb, portid, group, flags);
  255. }
  256. /**
  257. * genlmsg_multicast - multicast a netlink message to the default netns
  258. * @family: the generic netlink family
  259. * @skb: netlink message as socket buffer
  260. * @portid: own netlink portid to avoid sending to yourself
  261. * @group: offset of multicast group in groups array
  262. * @flags: allocation flags
  263. */
  264. static inline int genlmsg_multicast(struct genl_family *family,
  265. struct sk_buff *skb, u32 portid,
  266. unsigned int group, gfp_t flags)
  267. {
  268. return genlmsg_multicast_netns(family, &init_net, skb,
  269. portid, group, flags);
  270. }
  271. /**
  272. * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
  273. * @family: the generic netlink family
  274. * @skb: netlink message as socket buffer
  275. * @portid: own netlink portid to avoid sending to yourself
  276. * @group: offset of multicast group in groups array
  277. * @flags: allocation flags
  278. *
  279. * This function must hold the RTNL or rcu_read_lock().
  280. */
  281. int genlmsg_multicast_allns(struct genl_family *family,
  282. struct sk_buff *skb, u32 portid,
  283. unsigned int group, gfp_t flags);
  284. /**
  285. * genlmsg_unicast - unicast a netlink message
  286. * @skb: netlink message as socket buffer
  287. * @portid: netlink portid of the destination socket
  288. */
  289. static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 portid)
  290. {
  291. return nlmsg_unicast(net->genl_sock, skb, portid);
  292. }
  293. /**
  294. * genlmsg_reply - reply to a request
  295. * @skb: netlink message to be sent back
  296. * @info: receiver information
  297. */
  298. static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
  299. {
  300. return genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
  301. }
  302. /**
  303. * gennlmsg_data - head of message payload
  304. * @gnlh: genetlink message header
  305. */
  306. static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
  307. {
  308. return ((unsigned char *) gnlh + GENL_HDRLEN);
  309. }
  310. /**
  311. * genlmsg_len - length of message payload
  312. * @gnlh: genetlink message header
  313. */
  314. static inline int genlmsg_len(const struct genlmsghdr *gnlh)
  315. {
  316. struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
  317. NLMSG_HDRLEN);
  318. return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
  319. }
  320. /**
  321. * genlmsg_msg_size - length of genetlink message not including padding
  322. * @payload: length of message payload
  323. */
  324. static inline int genlmsg_msg_size(int payload)
  325. {
  326. return GENL_HDRLEN + payload;
  327. }
  328. /**
  329. * genlmsg_total_size - length of genetlink message including padding
  330. * @payload: length of message payload
  331. */
  332. static inline int genlmsg_total_size(int payload)
  333. {
  334. return NLMSG_ALIGN(genlmsg_msg_size(payload));
  335. }
  336. /**
  337. * genlmsg_new - Allocate a new generic netlink message
  338. * @payload: size of the message payload
  339. * @flags: the type of memory to allocate.
  340. */
  341. static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
  342. {
  343. return nlmsg_new(genlmsg_total_size(payload), flags);
  344. }
  345. /**
  346. * genl_set_err - report error to genetlink broadcast listeners
  347. * @family: the generic netlink family
  348. * @net: the network namespace to report the error to
  349. * @portid: the PORTID of a process that we want to skip (if any)
  350. * @group: the broadcast group that will notice the error
  351. * (this is the offset of the multicast group in the groups array)
  352. * @code: error code, must be negative (as usual in kernelspace)
  353. *
  354. * This function returns the number of broadcast listeners that have set the
  355. * NETLINK_RECV_NO_ENOBUFS socket option.
  356. */
  357. static inline int genl_set_err(struct genl_family *family, struct net *net,
  358. u32 portid, u32 group, int code)
  359. {
  360. if (WARN_ON_ONCE(group >= family->n_mcgrps))
  361. return -EINVAL;
  362. group = family->mcgrp_offset + group;
  363. return netlink_set_err(net->genl_sock, portid, group, code);
  364. }
  365. #endif /* __NET_GENERIC_NETLINK_H */