genetlink.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #ifndef __NET_GENERIC_NETLINK_H
  2. #define __NET_GENERIC_NETLINK_H
  3. #include <linux/genetlink.h>
  4. #include <net/netlink.h>
  5. #include <net/net_namespace.h>
  6. /**
  7. * struct genl_multicast_group - generic netlink multicast group
  8. * @name: name of the multicast group, names are per-family
  9. * @id: multicast group ID, assigned by the core, to use with
  10. * genlmsg_multicast().
  11. * @list: list entry for linking
  12. * @family: pointer to family, need not be set before registering
  13. */
  14. struct genl_multicast_group {
  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. * @netnsok: set to true if the family can handle network
  28. * namespaces and should be presented in all of them
  29. * @attrbuf: buffer to store parsed attributes
  30. * @ops_list: list of all assigned operations
  31. * @family_list: family list
  32. * @mcast_groups: multicast groups list
  33. */
  34. struct genl_family {
  35. unsigned int id;
  36. unsigned int hdrsize;
  37. char name[GENL_NAMSIZ];
  38. unsigned int version;
  39. unsigned int maxattr;
  40. bool netnsok;
  41. struct nlattr ** attrbuf; /* private */
  42. struct list_head ops_list; /* private */
  43. struct list_head family_list; /* private */
  44. struct list_head mcast_groups; /* private */
  45. };
  46. /**
  47. * struct genl_info - receiving information
  48. * @snd_seq: sending sequence number
  49. * @snd_pid: netlink pid of sender
  50. * @nlhdr: netlink message header
  51. * @genlhdr: generic netlink message header
  52. * @userhdr: user specific header
  53. * @attrs: netlink attributes
  54. */
  55. struct genl_info {
  56. u32 snd_seq;
  57. u32 snd_pid;
  58. struct nlmsghdr * nlhdr;
  59. struct genlmsghdr * genlhdr;
  60. void * userhdr;
  61. struct nlattr ** attrs;
  62. #ifdef CONFIG_NET_NS
  63. struct net * _net;
  64. #endif
  65. };
  66. #ifdef CONFIG_NET_NS
  67. static inline struct net *genl_info_net(struct genl_info *info)
  68. {
  69. return info->_net;
  70. }
  71. static inline void genl_info_net_set(struct genl_info *info, struct net *net)
  72. {
  73. info->_net = net;
  74. }
  75. #else
  76. static inline struct net *genl_info_net(struct genl_info *info)
  77. {
  78. return &init_net;
  79. }
  80. static inline void genl_info_net_set(struct genl_info *info, struct net *net)
  81. {
  82. }
  83. #endif
  84. /**
  85. * struct genl_ops - generic netlink operations
  86. * @cmd: command identifier
  87. * @flags: flags
  88. * @policy: attribute validation policy
  89. * @doit: standard command callback
  90. * @dumpit: callback for dumpers
  91. * @done: completion callback for dumps
  92. * @ops_list: operations list
  93. */
  94. struct genl_ops {
  95. u8 cmd;
  96. unsigned int flags;
  97. const struct nla_policy *policy;
  98. int (*doit)(struct sk_buff *skb,
  99. struct genl_info *info);
  100. int (*dumpit)(struct sk_buff *skb,
  101. struct netlink_callback *cb);
  102. int (*done)(struct netlink_callback *cb);
  103. struct list_head ops_list;
  104. };
  105. extern int genl_register_family(struct genl_family *family);
  106. extern int genl_register_family_with_ops(struct genl_family *family,
  107. struct genl_ops *ops, size_t n_ops);
  108. extern int genl_unregister_family(struct genl_family *family);
  109. extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
  110. extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
  111. extern int genl_register_mc_group(struct genl_family *family,
  112. struct genl_multicast_group *grp);
  113. extern void genl_unregister_mc_group(struct genl_family *family,
  114. struct genl_multicast_group *grp);
  115. /**
  116. * genlmsg_put - Add generic netlink header to netlink message
  117. * @skb: socket buffer holding the message
  118. * @pid: netlink pid the message is addressed to
  119. * @seq: sequence number (usually the one of the sender)
  120. * @family: generic netlink family
  121. * @flags netlink message flags
  122. * @cmd: generic netlink command
  123. *
  124. * Returns pointer to user specific header
  125. */
  126. static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
  127. struct genl_family *family, int flags, u8 cmd)
  128. {
  129. struct nlmsghdr *nlh;
  130. struct genlmsghdr *hdr;
  131. nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
  132. family->hdrsize, flags);
  133. if (nlh == NULL)
  134. return NULL;
  135. hdr = nlmsg_data(nlh);
  136. hdr->cmd = cmd;
  137. hdr->version = family->version;
  138. hdr->reserved = 0;
  139. return (char *) hdr + GENL_HDRLEN;
  140. }
  141. /**
  142. * genlmsg_put_reply - Add generic netlink header to a reply message
  143. * @skb: socket buffer holding the message
  144. * @info: receiver info
  145. * @family: generic netlink family
  146. * @flags: netlink message flags
  147. * @cmd: generic netlink command
  148. *
  149. * Returns pointer to user specific header
  150. */
  151. static inline void *genlmsg_put_reply(struct sk_buff *skb,
  152. struct genl_info *info,
  153. struct genl_family *family,
  154. int flags, u8 cmd)
  155. {
  156. return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
  157. flags, cmd);
  158. }
  159. /**
  160. * genlmsg_end - Finalize a generic netlink message
  161. * @skb: socket buffer the message is stored in
  162. * @hdr: user specific header
  163. */
  164. static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
  165. {
  166. return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  167. }
  168. /**
  169. * genlmsg_cancel - Cancel construction of a generic netlink message
  170. * @skb: socket buffer the message is stored in
  171. * @hdr: generic netlink message header
  172. */
  173. static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
  174. {
  175. nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
  176. }
  177. /**
  178. * genlmsg_multicast_netns - multicast a netlink message to a specific netns
  179. * @net: the net namespace
  180. * @skb: netlink message as socket buffer
  181. * @pid: own netlink pid to avoid sending to yourself
  182. * @group: multicast group id
  183. * @flags: allocation flags
  184. */
  185. static inline int genlmsg_multicast_netns(struct net *net, struct sk_buff *skb,
  186. u32 pid, unsigned int group, gfp_t flags)
  187. {
  188. return nlmsg_multicast(net->genl_sock, skb, pid, group, flags);
  189. }
  190. /**
  191. * genlmsg_multicast - multicast a netlink message to the default netns
  192. * @skb: netlink message as socket buffer
  193. * @pid: own netlink pid to avoid sending to yourself
  194. * @group: multicast group id
  195. * @flags: allocation flags
  196. */
  197. static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
  198. unsigned int group, gfp_t flags)
  199. {
  200. return genlmsg_multicast_netns(&init_net, skb, pid, group, flags);
  201. }
  202. /**
  203. * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
  204. * @skb: netlink message as socket buffer
  205. * @pid: own netlink pid to avoid sending to yourself
  206. * @group: multicast group id
  207. * @flags: allocation flags
  208. *
  209. * This function must hold the RTNL or rcu_read_lock().
  210. */
  211. int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid,
  212. unsigned int group, gfp_t flags);
  213. /**
  214. * genlmsg_unicast - unicast a netlink message
  215. * @skb: netlink message as socket buffer
  216. * @pid: netlink pid of the destination socket
  217. */
  218. static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 pid)
  219. {
  220. return nlmsg_unicast(net->genl_sock, skb, pid);
  221. }
  222. /**
  223. * genlmsg_reply - reply to a request
  224. * @skb: netlink message to be sent back
  225. * @info: receiver information
  226. */
  227. static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
  228. {
  229. return genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
  230. }
  231. /**
  232. * gennlmsg_data - head of message payload
  233. * @gnlh: genetlink messsage header
  234. */
  235. static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
  236. {
  237. return ((unsigned char *) gnlh + GENL_HDRLEN);
  238. }
  239. /**
  240. * genlmsg_len - length of message payload
  241. * @gnlh: genetlink message header
  242. */
  243. static inline int genlmsg_len(const struct genlmsghdr *gnlh)
  244. {
  245. struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
  246. NLMSG_HDRLEN);
  247. return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
  248. }
  249. /**
  250. * genlmsg_msg_size - length of genetlink message not including padding
  251. * @payload: length of message payload
  252. */
  253. static inline int genlmsg_msg_size(int payload)
  254. {
  255. return GENL_HDRLEN + payload;
  256. }
  257. /**
  258. * genlmsg_total_size - length of genetlink message including padding
  259. * @payload: length of message payload
  260. */
  261. static inline int genlmsg_total_size(int payload)
  262. {
  263. return NLMSG_ALIGN(genlmsg_msg_size(payload));
  264. }
  265. /**
  266. * genlmsg_new - Allocate a new generic netlink message
  267. * @payload: size of the message payload
  268. * @flags: the type of memory to allocate.
  269. */
  270. static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
  271. {
  272. return nlmsg_new(genlmsg_total_size(payload), flags);
  273. }
  274. #endif /* __NET_GENERIC_NETLINK_H */