genetlink.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #define GENL_ADMIN_PERM 0x01
  28. /**
  29. * struct genl_info - receiving information
  30. * @snd_seq: sending sequence number
  31. * @snd_pid: netlink pid of sender
  32. * @nlhdr: netlink message header
  33. * @genlhdr: generic netlink message header
  34. * @userhdr: user specific header
  35. * @attrs: netlink attributes
  36. */
  37. struct genl_info
  38. {
  39. u32 snd_seq;
  40. u32 snd_pid;
  41. struct nlmsghdr * nlhdr;
  42. struct genlmsghdr * genlhdr;
  43. void * userhdr;
  44. struct nlattr ** attrs;
  45. };
  46. /**
  47. * struct genl_ops - generic netlink operations
  48. * @cmd: command identifier
  49. * @flags: flags
  50. * @policy: attribute validation policy
  51. * @doit: standard command callback
  52. * @dumpit: callback for dumpers
  53. * @ops_list: operations list
  54. */
  55. struct genl_ops
  56. {
  57. u8 cmd;
  58. unsigned int flags;
  59. 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. struct list_head ops_list;
  65. };
  66. extern int genl_register_family(struct genl_family *family);
  67. extern int genl_unregister_family(struct genl_family *family);
  68. extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
  69. extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
  70. extern struct sock *genl_sock;
  71. /**
  72. * genlmsg_put - Add generic netlink header to netlink message
  73. * @skb: socket buffer holding the message
  74. * @pid: netlink pid the message is addressed to
  75. * @seq: sequence number (usually the one of the sender)
  76. * @type: netlink message type
  77. * @hdrlen: length of the user specific header
  78. * @flags netlink message flags
  79. * @cmd: generic netlink command
  80. * @version: version
  81. *
  82. * Returns pointer to user specific header
  83. */
  84. static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
  85. int type, int hdrlen, int flags,
  86. u8 cmd, u8 version)
  87. {
  88. struct nlmsghdr *nlh;
  89. struct genlmsghdr *hdr;
  90. nlh = nlmsg_put(skb, pid, seq, type, GENL_HDRLEN + hdrlen, flags);
  91. if (nlh == NULL)
  92. return NULL;
  93. hdr = nlmsg_data(nlh);
  94. hdr->cmd = cmd;
  95. hdr->version = version;
  96. hdr->reserved = 0;
  97. return (char *) hdr + GENL_HDRLEN;
  98. }
  99. /**
  100. * genlmsg_end - Finalize a generic netlink message
  101. * @skb: socket buffer the message is stored in
  102. * @hdr: user specific header
  103. */
  104. static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
  105. {
  106. return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  107. }
  108. /**
  109. * genlmsg_cancel - Cancel construction of a generic netlink message
  110. * @skb: socket buffer the message is stored in
  111. * @hdr: generic netlink message header
  112. */
  113. static inline int genlmsg_cancel(struct sk_buff *skb, void *hdr)
  114. {
  115. return nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  116. }
  117. /**
  118. * genlmsg_multicast - multicast a netlink message
  119. * @skb: netlink message as socket buffer
  120. * @pid: own netlink pid to avoid sending to yourself
  121. * @group: multicast group id
  122. */
  123. static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
  124. unsigned int group)
  125. {
  126. return nlmsg_multicast(genl_sock, skb, pid, group);
  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. #endif /* __NET_GENERIC_NETLINK_H */