genetlink.h 8.3 KB

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