genetlink.h 6.0 KB

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