genetlink.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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_register_family_with_ops(struct genl_family *family,
  86. struct genl_ops *ops, size_t n_ops);
  87. extern int genl_unregister_family(struct genl_family *family);
  88. extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
  89. extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
  90. extern int genl_register_mc_group(struct genl_family *family,
  91. struct genl_multicast_group *grp);
  92. extern void genl_unregister_mc_group(struct genl_family *family,
  93. struct genl_multicast_group *grp);
  94. extern struct sock *genl_sock;
  95. /**
  96. * genlmsg_put - Add generic netlink header to netlink message
  97. * @skb: socket buffer holding the message
  98. * @pid: netlink pid the message is addressed to
  99. * @seq: sequence number (usually the one of the sender)
  100. * @family: generic netlink family
  101. * @flags netlink message flags
  102. * @cmd: generic netlink command
  103. *
  104. * Returns pointer to user specific header
  105. */
  106. static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
  107. struct genl_family *family, int flags, u8 cmd)
  108. {
  109. struct nlmsghdr *nlh;
  110. struct genlmsghdr *hdr;
  111. nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
  112. family->hdrsize, flags);
  113. if (nlh == NULL)
  114. return NULL;
  115. hdr = nlmsg_data(nlh);
  116. hdr->cmd = cmd;
  117. hdr->version = family->version;
  118. hdr->reserved = 0;
  119. return (char *) hdr + GENL_HDRLEN;
  120. }
  121. /**
  122. * genlmsg_put_reply - Add generic netlink header to a reply message
  123. * @skb: socket buffer holding the message
  124. * @info: receiver info
  125. * @family: generic netlink family
  126. * @flags: netlink message flags
  127. * @cmd: generic netlink command
  128. *
  129. * Returns pointer to user specific header
  130. */
  131. static inline void *genlmsg_put_reply(struct sk_buff *skb,
  132. struct genl_info *info,
  133. struct genl_family *family,
  134. int flags, u8 cmd)
  135. {
  136. return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
  137. flags, cmd);
  138. }
  139. /**
  140. * genlmsg_end - Finalize a generic netlink message
  141. * @skb: socket buffer the message is stored in
  142. * @hdr: user specific header
  143. */
  144. static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
  145. {
  146. return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  147. }
  148. /**
  149. * genlmsg_cancel - Cancel construction of a generic netlink message
  150. * @skb: socket buffer the message is stored in
  151. * @hdr: generic netlink message header
  152. */
  153. static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
  154. {
  155. nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  156. }
  157. /**
  158. * genlmsg_multicast - multicast a netlink message
  159. * @skb: netlink message as socket buffer
  160. * @pid: own netlink pid to avoid sending to yourself
  161. * @group: multicast group id
  162. * @flags: allocation flags
  163. */
  164. static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
  165. unsigned int group, gfp_t flags)
  166. {
  167. return nlmsg_multicast(genl_sock, skb, pid, group, flags);
  168. }
  169. /**
  170. * genlmsg_unicast - unicast a netlink message
  171. * @skb: netlink message as socket buffer
  172. * @pid: netlink pid of the destination socket
  173. */
  174. static inline int genlmsg_unicast(struct sk_buff *skb, u32 pid)
  175. {
  176. return nlmsg_unicast(genl_sock, skb, pid);
  177. }
  178. /**
  179. * genlmsg_reply - reply to a request
  180. * @skb: netlink message to be sent back
  181. * @info: receiver information
  182. */
  183. static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
  184. {
  185. return genlmsg_unicast(skb, info->snd_pid);
  186. }
  187. /**
  188. * gennlmsg_data - head of message payload
  189. * @gnlh: genetlink messsage header
  190. */
  191. static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
  192. {
  193. return ((unsigned char *) gnlh + GENL_HDRLEN);
  194. }
  195. /**
  196. * genlmsg_len - length of message payload
  197. * @gnlh: genetlink message header
  198. */
  199. static inline int genlmsg_len(const struct genlmsghdr *gnlh)
  200. {
  201. struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
  202. NLMSG_HDRLEN);
  203. return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
  204. }
  205. /**
  206. * genlmsg_msg_size - length of genetlink message not including padding
  207. * @payload: length of message payload
  208. */
  209. static inline int genlmsg_msg_size(int payload)
  210. {
  211. return GENL_HDRLEN + payload;
  212. }
  213. /**
  214. * genlmsg_total_size - length of genetlink message including padding
  215. * @payload: length of message payload
  216. */
  217. static inline int genlmsg_total_size(int payload)
  218. {
  219. return NLMSG_ALIGN(genlmsg_msg_size(payload));
  220. }
  221. /**
  222. * genlmsg_new - Allocate a new generic netlink message
  223. * @payload: size of the message payload
  224. * @flags: the type of memory to allocate.
  225. */
  226. static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
  227. {
  228. return nlmsg_new(genlmsg_total_size(payload), flags);
  229. }
  230. #endif /* __NET_GENERIC_NETLINK_H */