blob: decdda54682970302164242f8982ceeee669773d [file] [log] [blame]
Thomas Graf482a8522005-11-10 02:25:56 +01001#ifndef __NET_GENERIC_NETLINK_H
2#define __NET_GENERIC_NETLINK_H
3
4#include <linux/genetlink.h>
5#include <net/netlink.h>
6
7/**
Johannes Berg2dbba6f2007-07-18 15:47:52 -07008 * struct genl_multicast_group - generic netlink multicast group
9 * @name: name of the multicast group, names are per-family
10 * @id: multicast group ID, assigned by the core, to use with
11 * genlmsg_multicast().
12 * @list: list entry for linking
13 * @family: pointer to family, need not be set before registering
14 */
15struct genl_multicast_group
16{
17 struct genl_family *family; /* private */
18 struct list_head list; /* private */
19 char name[GENL_NAMSIZ];
20 u32 id;
21};
22
23/**
Thomas Graf482a8522005-11-10 02:25:56 +010024 * struct genl_family - generic netlink family
25 * @id: protocol family idenfitier
26 * @hdrsize: length of user specific header in bytes
27 * @name: name of family
28 * @version: protocol version
29 * @maxattr: maximum number of attributes supported
30 * @attrbuf: buffer to store parsed attributes
31 * @ops_list: list of all assigned operations
32 * @family_list: family list
Johannes Berg2dbba6f2007-07-18 15:47:52 -070033 * @mcast_groups: multicast groups list
Thomas Graf482a8522005-11-10 02:25:56 +010034 */
35struct genl_family
36{
37 unsigned int id;
38 unsigned int hdrsize;
39 char name[GENL_NAMSIZ];
40 unsigned int version;
41 unsigned int maxattr;
Thomas Graf482a8522005-11-10 02:25:56 +010042 struct nlattr ** attrbuf; /* private */
43 struct list_head ops_list; /* private */
44 struct list_head family_list; /* private */
Johannes Berg2dbba6f2007-07-18 15:47:52 -070045 struct list_head mcast_groups; /* private */
Thomas Graf482a8522005-11-10 02:25:56 +010046};
47
Thomas Graf482a8522005-11-10 02:25:56 +010048/**
49 * struct genl_info - receiving information
50 * @snd_seq: sending sequence number
51 * @snd_pid: netlink pid of sender
52 * @nlhdr: netlink message header
53 * @genlhdr: generic netlink message header
54 * @userhdr: user specific header
55 * @attrs: netlink attributes
56 */
57struct genl_info
58{
59 u32 snd_seq;
60 u32 snd_pid;
61 struct nlmsghdr * nlhdr;
62 struct genlmsghdr * genlhdr;
63 void * userhdr;
64 struct nlattr ** attrs;
65};
66
67/**
68 * struct genl_ops - generic netlink operations
69 * @cmd: command identifier
70 * @flags: flags
71 * @policy: attribute validation policy
72 * @doit: standard command callback
73 * @dumpit: callback for dumpers
Jamal Hadi Salima4d13662006-12-01 20:07:42 -080074 * @done: completion callback for dumps
Thomas Graf482a8522005-11-10 02:25:56 +010075 * @ops_list: operations list
76 */
77struct genl_ops
78{
Per Lidenb461d2f2006-01-03 14:13:29 -080079 u8 cmd;
Thomas Graf482a8522005-11-10 02:25:56 +010080 unsigned int flags;
Patrick McHardyef7c79e2007-06-05 12:38:30 -070081 const struct nla_policy *policy;
Thomas Graf482a8522005-11-10 02:25:56 +010082 int (*doit)(struct sk_buff *skb,
83 struct genl_info *info);
84 int (*dumpit)(struct sk_buff *skb,
85 struct netlink_callback *cb);
Jamal Hadi Salima4d13662006-12-01 20:07:42 -080086 int (*done)(struct netlink_callback *cb);
Thomas Graf482a8522005-11-10 02:25:56 +010087 struct list_head ops_list;
88};
89
90extern int genl_register_family(struct genl_family *family);
91extern int genl_unregister_family(struct genl_family *family);
92extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
93extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
Johannes Berg2dbba6f2007-07-18 15:47:52 -070094extern int genl_register_mc_group(struct genl_family *family,
95 struct genl_multicast_group *grp);
96extern void genl_unregister_mc_group(struct genl_family *family,
97 struct genl_multicast_group *grp);
Thomas Graf482a8522005-11-10 02:25:56 +010098
99extern struct sock *genl_sock;
100
101/**
102 * genlmsg_put - Add generic netlink header to netlink message
103 * @skb: socket buffer holding the message
104 * @pid: netlink pid the message is addressed to
105 * @seq: sequence number (usually the one of the sender)
Thomas Graf17c157c2006-11-14 19:46:02 -0800106 * @family: generic netlink family
Thomas Graf482a8522005-11-10 02:25:56 +0100107 * @flags netlink message flags
108 * @cmd: generic netlink command
Thomas Graf482a8522005-11-10 02:25:56 +0100109 *
110 * Returns pointer to user specific header
111 */
112static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
Thomas Graf17c157c2006-11-14 19:46:02 -0800113 struct genl_family *family, int flags, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100114{
115 struct nlmsghdr *nlh;
116 struct genlmsghdr *hdr;
117
Thomas Graf17c157c2006-11-14 19:46:02 -0800118 nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
119 family->hdrsize, flags);
Thomas Graf482a8522005-11-10 02:25:56 +0100120 if (nlh == NULL)
121 return NULL;
122
123 hdr = nlmsg_data(nlh);
124 hdr->cmd = cmd;
Thomas Graf17c157c2006-11-14 19:46:02 -0800125 hdr->version = family->version;
Thomas Graf482a8522005-11-10 02:25:56 +0100126 hdr->reserved = 0;
127
128 return (char *) hdr + GENL_HDRLEN;
129}
130
131/**
Thomas Graf17c157c2006-11-14 19:46:02 -0800132 * genlmsg_put_reply - Add generic netlink header to a reply message
133 * @skb: socket buffer holding the message
134 * @info: receiver info
135 * @family: generic netlink family
136 * @flags: netlink message flags
137 * @cmd: generic netlink command
138 *
139 * Returns pointer to user specific header
140 */
141static inline void *genlmsg_put_reply(struct sk_buff *skb,
142 struct genl_info *info,
143 struct genl_family *family,
144 int flags, u8 cmd)
145{
146 return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
147 flags, cmd);
148}
149
150/**
Thomas Graf482a8522005-11-10 02:25:56 +0100151 * genlmsg_end - Finalize a generic netlink message
152 * @skb: socket buffer the message is stored in
153 * @hdr: user specific header
154 */
155static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
156{
157 return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
158}
159
160/**
161 * genlmsg_cancel - Cancel construction of a generic netlink message
162 * @skb: socket buffer the message is stored in
163 * @hdr: generic netlink message header
164 */
165static inline int genlmsg_cancel(struct sk_buff *skb, void *hdr)
166{
167 return nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
168}
169
170/**
171 * genlmsg_multicast - multicast a netlink message
172 * @skb: netlink message as socket buffer
173 * @pid: own netlink pid to avoid sending to yourself
174 * @group: multicast group id
Thomas Grafd387f6a2006-08-15 00:31:06 -0700175 * @flags: allocation flags
Thomas Graf482a8522005-11-10 02:25:56 +0100176 */
177static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
Thomas Grafd387f6a2006-08-15 00:31:06 -0700178 unsigned int group, gfp_t flags)
Thomas Graf482a8522005-11-10 02:25:56 +0100179{
Thomas Grafd387f6a2006-08-15 00:31:06 -0700180 return nlmsg_multicast(genl_sock, skb, pid, group, flags);
Thomas Graf482a8522005-11-10 02:25:56 +0100181}
182
183/**
184 * genlmsg_unicast - unicast a netlink message
185 * @skb: netlink message as socket buffer
186 * @pid: netlink pid of the destination socket
187 */
188static inline int genlmsg_unicast(struct sk_buff *skb, u32 pid)
189{
190 return nlmsg_unicast(genl_sock, skb, pid);
191}
192
Balbir Singhfb0ba6bd2006-07-14 00:24:39 -0700193/**
Thomas Graf81878d22006-11-14 19:45:27 -0800194 * genlmsg_reply - reply to a request
195 * @skb: netlink message to be sent back
196 * @info: receiver information
197 */
198static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
199{
200 return genlmsg_unicast(skb, info->snd_pid);
201}
202
203/**
Balbir Singhfb0ba6bd2006-07-14 00:24:39 -0700204 * gennlmsg_data - head of message payload
205 * @gnlh: genetlink messsage header
206 */
207static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
208{
209 return ((unsigned char *) gnlh + GENL_HDRLEN);
210}
211
212/**
213 * genlmsg_len - length of message payload
214 * @gnlh: genetlink message header
215 */
216static inline int genlmsg_len(const struct genlmsghdr *gnlh)
217{
218 struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
219 NLMSG_HDRLEN);
220 return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
221}
222
Balbir Singh17db9522006-09-30 23:28:51 -0700223/**
224 * genlmsg_msg_size - length of genetlink message not including padding
225 * @payload: length of message payload
226 */
227static inline int genlmsg_msg_size(int payload)
228{
229 return GENL_HDRLEN + payload;
230}
231
232/**
233 * genlmsg_total_size - length of genetlink message including padding
234 * @payload: length of message payload
235 */
236static inline int genlmsg_total_size(int payload)
237{
238 return NLMSG_ALIGN(genlmsg_msg_size(payload));
239}
240
Thomas Graf3dabc712006-11-14 19:44:52 -0800241/**
242 * genlmsg_new - Allocate a new generic netlink message
243 * @payload: size of the message payload
244 * @flags: the type of memory to allocate.
245 */
246static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
247{
248 return nlmsg_new(genlmsg_total_size(payload), flags);
249}
250
251
Thomas Graf482a8522005-11-10 02:25:56 +0100252#endif /* __NET_GENERIC_NETLINK_H */