genetlink.h 6.7 KB

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