genetlink.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * @ops_list: operations list
  53. */
  54. struct genl_ops
  55. {
  56. u8 cmd;
  57. unsigned int flags;
  58. struct nla_policy *policy;
  59. int (*doit)(struct sk_buff *skb,
  60. struct genl_info *info);
  61. int (*dumpit)(struct sk_buff *skb,
  62. struct netlink_callback *cb);
  63. struct list_head ops_list;
  64. };
  65. extern int genl_register_family(struct genl_family *family);
  66. extern int genl_unregister_family(struct genl_family *family);
  67. extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
  68. extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
  69. extern struct sock *genl_sock;
  70. /**
  71. * genlmsg_put - Add generic netlink header to netlink message
  72. * @skb: socket buffer holding the message
  73. * @pid: netlink pid the message is addressed to
  74. * @seq: sequence number (usually the one of the sender)
  75. * @type: netlink message type
  76. * @hdrlen: length of the user specific header
  77. * @flags netlink message flags
  78. * @cmd: generic netlink command
  79. * @version: version
  80. *
  81. * Returns pointer to user specific header
  82. */
  83. static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
  84. int type, int hdrlen, int flags,
  85. u8 cmd, u8 version)
  86. {
  87. struct nlmsghdr *nlh;
  88. struct genlmsghdr *hdr;
  89. nlh = nlmsg_put(skb, pid, seq, type, GENL_HDRLEN + hdrlen, flags);
  90. if (nlh == NULL)
  91. return NULL;
  92. hdr = nlmsg_data(nlh);
  93. hdr->cmd = cmd;
  94. hdr->version = version;
  95. hdr->reserved = 0;
  96. return (char *) hdr + GENL_HDRLEN;
  97. }
  98. /**
  99. * genlmsg_end - Finalize a generic netlink message
  100. * @skb: socket buffer the message is stored in
  101. * @hdr: user specific header
  102. */
  103. static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
  104. {
  105. return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  106. }
  107. /**
  108. * genlmsg_cancel - Cancel construction of a generic netlink message
  109. * @skb: socket buffer the message is stored in
  110. * @hdr: generic netlink message header
  111. */
  112. static inline int genlmsg_cancel(struct sk_buff *skb, void *hdr)
  113. {
  114. return nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  115. }
  116. /**
  117. * genlmsg_multicast - multicast a netlink message
  118. * @skb: netlink message as socket buffer
  119. * @pid: own netlink pid to avoid sending to yourself
  120. * @group: multicast group id
  121. * @flags: allocation flags
  122. */
  123. static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
  124. unsigned int group, gfp_t flags)
  125. {
  126. return nlmsg_multicast(genl_sock, skb, pid, group, flags);
  127. }
  128. /**
  129. * genlmsg_unicast - unicast a netlink message
  130. * @skb: netlink message as socket buffer
  131. * @pid: netlink pid of the destination socket
  132. */
  133. static inline int genlmsg_unicast(struct sk_buff *skb, u32 pid)
  134. {
  135. return nlmsg_unicast(genl_sock, skb, pid);
  136. }
  137. /**
  138. * gennlmsg_data - head of message payload
  139. * @gnlh: genetlink messsage header
  140. */
  141. static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
  142. {
  143. return ((unsigned char *) gnlh + GENL_HDRLEN);
  144. }
  145. /**
  146. * genlmsg_len - length of message payload
  147. * @gnlh: genetlink message header
  148. */
  149. static inline int genlmsg_len(const struct genlmsghdr *gnlh)
  150. {
  151. struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
  152. NLMSG_HDRLEN);
  153. return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
  154. }
  155. #endif /* __NET_GENERIC_NETLINK_H */