genetlink.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 module * owner;
  24. struct nlattr ** attrbuf; /* private */
  25. struct list_head ops_list; /* private */
  26. struct list_head family_list; /* private */
  27. };
  28. #define GENL_ADMIN_PERM 0x01
  29. /**
  30. * struct genl_info - receiving information
  31. * @snd_seq: sending sequence number
  32. * @snd_pid: netlink pid of sender
  33. * @nlhdr: netlink message header
  34. * @genlhdr: generic netlink message header
  35. * @userhdr: user specific header
  36. * @attrs: netlink attributes
  37. */
  38. struct genl_info
  39. {
  40. u32 snd_seq;
  41. u32 snd_pid;
  42. struct nlmsghdr * nlhdr;
  43. struct genlmsghdr * genlhdr;
  44. void * userhdr;
  45. struct nlattr ** attrs;
  46. };
  47. /**
  48. * struct genl_ops - generic netlink operations
  49. * @cmd: command identifier
  50. * @flags: flags
  51. * @policy: attribute validation policy
  52. * @doit: standard command callback
  53. * @dumpit: callback for dumpers
  54. * @ops_list: operations list
  55. */
  56. struct genl_ops
  57. {
  58. unsigned int cmd;
  59. unsigned int flags;
  60. struct nla_policy *policy;
  61. int (*doit)(struct sk_buff *skb,
  62. struct genl_info *info);
  63. int (*dumpit)(struct sk_buff *skb,
  64. 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. * @type: netlink message type
  78. * @hdrlen: length of the user specific header
  79. * @flags netlink message flags
  80. * @cmd: generic netlink command
  81. * @version: version
  82. *
  83. * Returns pointer to user specific header
  84. */
  85. static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
  86. int type, int hdrlen, int flags,
  87. u8 cmd, u8 version)
  88. {
  89. struct nlmsghdr *nlh;
  90. struct genlmsghdr *hdr;
  91. nlh = nlmsg_put(skb, pid, seq, type, GENL_HDRLEN + hdrlen, flags);
  92. if (nlh == NULL)
  93. return NULL;
  94. hdr = nlmsg_data(nlh);
  95. hdr->cmd = cmd;
  96. hdr->version = version;
  97. hdr->reserved = 0;
  98. return (char *) hdr + GENL_HDRLEN;
  99. }
  100. /**
  101. * genlmsg_end - Finalize a generic netlink message
  102. * @skb: socket buffer the message is stored in
  103. * @hdr: user specific header
  104. */
  105. static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
  106. {
  107. return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  108. }
  109. /**
  110. * genlmsg_cancel - Cancel construction of a generic netlink message
  111. * @skb: socket buffer the message is stored in
  112. * @hdr: generic netlink message header
  113. */
  114. static inline int genlmsg_cancel(struct sk_buff *skb, void *hdr)
  115. {
  116. return nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  117. }
  118. /**
  119. * genlmsg_multicast - multicast a netlink message
  120. * @skb: netlink message as socket buffer
  121. * @pid: own netlink pid to avoid sending to yourself
  122. * @group: multicast group id
  123. */
  124. static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
  125. unsigned int group)
  126. {
  127. return nlmsg_multicast(genl_sock, skb, pid, group);
  128. }
  129. /**
  130. * genlmsg_unicast - unicast a netlink message
  131. * @skb: netlink message as socket buffer
  132. * @pid: netlink pid of the destination socket
  133. */
  134. static inline int genlmsg_unicast(struct sk_buff *skb, u32 pid)
  135. {
  136. return nlmsg_unicast(genl_sock, skb, pid);
  137. }
  138. #endif /* __NET_GENERIC_NETLINK_H */