genetlink.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. /**
  7. * struct genl_multicast_group - generic netlink multicast group
  8. * @name: name of the multicast group, names are per-family
  9. * @id: multicast group ID, assigned by the core, to use with
  10. * genlmsg_multicast().
  11. * @list: list entry for linking
  12. * @family: pointer to family, need not be set before registering
  13. */
  14. struct genl_multicast_group {
  15. struct genl_family *family; /* private */
  16. struct list_head list; /* private */
  17. char name[GENL_NAMSIZ];
  18. u32 id;
  19. };
  20. /**
  21. * struct genl_family - generic netlink family
  22. * @id: protocol family idenfitier
  23. * @hdrsize: length of user specific header in bytes
  24. * @name: name of family
  25. * @version: protocol version
  26. * @maxattr: maximum number of attributes supported
  27. * @netnsok: set to true if the family can handle network
  28. * namespaces and should be presented in all of them
  29. * @attrbuf: buffer to store parsed attributes
  30. * @ops_list: list of all assigned operations
  31. * @family_list: family list
  32. * @mcast_groups: multicast groups list
  33. */
  34. struct genl_family {
  35. unsigned int id;
  36. unsigned int hdrsize;
  37. char name[GENL_NAMSIZ];
  38. unsigned int version;
  39. unsigned int maxattr;
  40. bool netnsok;
  41. struct nlattr ** attrbuf; /* private */
  42. struct list_head ops_list; /* private */
  43. struct list_head family_list; /* private */
  44. struct list_head mcast_groups; /* private */
  45. };
  46. /**
  47. * struct genl_info - receiving information
  48. * @snd_seq: sending sequence number
  49. * @snd_pid: netlink pid of sender
  50. * @nlhdr: netlink message header
  51. * @genlhdr: generic netlink message header
  52. * @userhdr: user specific header
  53. * @attrs: netlink attributes
  54. */
  55. struct genl_info {
  56. u32 snd_seq;
  57. u32 snd_pid;
  58. struct nlmsghdr * nlhdr;
  59. struct genlmsghdr * genlhdr;
  60. void * userhdr;
  61. struct nlattr ** attrs;
  62. #ifdef CONFIG_NET_NS
  63. struct net * _net;
  64. #endif
  65. };
  66. static inline struct net *genl_info_net(struct genl_info *info)
  67. {
  68. return read_pnet(&info->_net);
  69. }
  70. static inline void genl_info_net_set(struct genl_info *info, struct net *net)
  71. {
  72. write_pnet(&info->_net, net);
  73. }
  74. /**
  75. * struct genl_ops - generic netlink operations
  76. * @cmd: command identifier
  77. * @flags: flags
  78. * @policy: attribute validation policy
  79. * @doit: standard command callback
  80. * @dumpit: callback for dumpers
  81. * @done: completion callback for dumps
  82. * @ops_list: operations list
  83. */
  84. struct genl_ops {
  85. u8 cmd;
  86. unsigned int flags;
  87. const struct nla_policy *policy;
  88. int (*doit)(struct sk_buff *skb,
  89. struct genl_info *info);
  90. int (*dumpit)(struct sk_buff *skb,
  91. struct netlink_callback *cb);
  92. int (*done)(struct netlink_callback *cb);
  93. struct list_head ops_list;
  94. };
  95. extern int genl_register_family(struct genl_family *family);
  96. extern int genl_register_family_with_ops(struct genl_family *family,
  97. struct genl_ops *ops, size_t n_ops);
  98. extern int genl_unregister_family(struct genl_family *family);
  99. extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
  100. extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
  101. extern int genl_register_mc_group(struct genl_family *family,
  102. struct genl_multicast_group *grp);
  103. extern void genl_unregister_mc_group(struct genl_family *family,
  104. struct genl_multicast_group *grp);
  105. /**
  106. * genlmsg_put - Add generic netlink header to netlink message
  107. * @skb: socket buffer holding the message
  108. * @pid: netlink pid the message is addressed to
  109. * @seq: sequence number (usually the one of the sender)
  110. * @family: generic netlink family
  111. * @flags netlink message flags
  112. * @cmd: generic netlink command
  113. *
  114. * Returns pointer to user specific header
  115. */
  116. static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
  117. struct genl_family *family, int flags, u8 cmd)
  118. {
  119. struct nlmsghdr *nlh;
  120. struct genlmsghdr *hdr;
  121. nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
  122. family->hdrsize, flags);
  123. if (nlh == NULL)
  124. return NULL;
  125. hdr = nlmsg_data(nlh);
  126. hdr->cmd = cmd;
  127. hdr->version = family->version;
  128. hdr->reserved = 0;
  129. return (char *) hdr + GENL_HDRLEN;
  130. }
  131. /**
  132. * genlmsg_put_reply - Add generic netlink header to a reply message
  133. * @skb: socket buffer holding the message
  134. * @info: receiver info
  135. * @family: generic netlink family
  136. * @flags: netlink message flags
  137. * @cmd: generic netlink command
  138. *
  139. * Returns pointer to user specific header
  140. */
  141. static inline void *genlmsg_put_reply(struct sk_buff *skb,
  142. struct genl_info *info,
  143. struct genl_family *family,
  144. int flags, u8 cmd)
  145. {
  146. return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
  147. flags, cmd);
  148. }
  149. /**
  150. * genlmsg_end - Finalize a generic netlink message
  151. * @skb: socket buffer the message is stored in
  152. * @hdr: user specific header
  153. */
  154. static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
  155. {
  156. return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  157. }
  158. /**
  159. * genlmsg_cancel - Cancel construction of a generic netlink message
  160. * @skb: socket buffer the message is stored in
  161. * @hdr: generic netlink message header
  162. */
  163. static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
  164. {
  165. nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  166. }
  167. /**
  168. * genlmsg_multicast_netns - multicast a netlink message to a specific netns
  169. * @net: the net namespace
  170. * @skb: netlink message as socket buffer
  171. * @pid: own netlink pid to avoid sending to yourself
  172. * @group: multicast group id
  173. * @flags: allocation flags
  174. */
  175. static inline int genlmsg_multicast_netns(struct net *net, struct sk_buff *skb,
  176. u32 pid, unsigned int group, gfp_t flags)
  177. {
  178. return nlmsg_multicast(net->genl_sock, skb, pid, group, flags);
  179. }
  180. /**
  181. * genlmsg_multicast - multicast a netlink message to the default netns
  182. * @skb: netlink message as socket buffer
  183. * @pid: own netlink pid to avoid sending to yourself
  184. * @group: multicast group id
  185. * @flags: allocation flags
  186. */
  187. static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
  188. unsigned int group, gfp_t flags)
  189. {
  190. return genlmsg_multicast_netns(&init_net, skb, pid, group, flags);
  191. }
  192. /**
  193. * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
  194. * @skb: netlink message as socket buffer
  195. * @pid: own netlink pid to avoid sending to yourself
  196. * @group: multicast group id
  197. * @flags: allocation flags
  198. *
  199. * This function must hold the RTNL or rcu_read_lock().
  200. */
  201. int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid,
  202. unsigned int group, gfp_t flags);
  203. /**
  204. * genlmsg_unicast - unicast a netlink message
  205. * @skb: netlink message as socket buffer
  206. * @pid: netlink pid of the destination socket
  207. */
  208. static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 pid)
  209. {
  210. return nlmsg_unicast(net->genl_sock, skb, pid);
  211. }
  212. /**
  213. * genlmsg_reply - reply to a request
  214. * @skb: netlink message to be sent back
  215. * @info: receiver information
  216. */
  217. static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
  218. {
  219. return genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
  220. }
  221. /**
  222. * gennlmsg_data - head of message payload
  223. * @gnlh: genetlink messsage header
  224. */
  225. static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
  226. {
  227. return ((unsigned char *) gnlh + GENL_HDRLEN);
  228. }
  229. /**
  230. * genlmsg_len - length of message payload
  231. * @gnlh: genetlink message header
  232. */
  233. static inline int genlmsg_len(const struct genlmsghdr *gnlh)
  234. {
  235. struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
  236. NLMSG_HDRLEN);
  237. return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
  238. }
  239. /**
  240. * genlmsg_msg_size - length of genetlink message not including padding
  241. * @payload: length of message payload
  242. */
  243. static inline int genlmsg_msg_size(int payload)
  244. {
  245. return GENL_HDRLEN + payload;
  246. }
  247. /**
  248. * genlmsg_total_size - length of genetlink message including padding
  249. * @payload: length of message payload
  250. */
  251. static inline int genlmsg_total_size(int payload)
  252. {
  253. return NLMSG_ALIGN(genlmsg_msg_size(payload));
  254. }
  255. /**
  256. * genlmsg_new - Allocate a new generic netlink message
  257. * @payload: size of the message payload
  258. * @flags: the type of memory to allocate.
  259. */
  260. static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
  261. {
  262. return nlmsg_new(genlmsg_total_size(payload), flags);
  263. }
  264. #endif /* __NET_GENERIC_NETLINK_H */