blob: b6eaca122db871ab4b269a99dcbabda0fe9bcf90 [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/**
8 * struct genl_family - generic netlink family
9 * @id: protocol family idenfitier
10 * @hdrsize: length of user specific header in bytes
11 * @name: name of family
12 * @version: protocol version
13 * @maxattr: maximum number of attributes supported
14 * @attrbuf: buffer to store parsed attributes
15 * @ops_list: list of all assigned operations
16 * @family_list: family list
17 */
18struct genl_family
19{
20 unsigned int id;
21 unsigned int hdrsize;
22 char name[GENL_NAMSIZ];
23 unsigned int version;
24 unsigned int maxattr;
Thomas Graf482a8522005-11-10 02:25:56 +010025 struct nlattr ** attrbuf; /* private */
26 struct list_head ops_list; /* private */
27 struct list_head family_list; /* private */
28};
29
Thomas Graf482a8522005-11-10 02:25:56 +010030/**
31 * struct genl_info - receiving information
32 * @snd_seq: sending sequence number
33 * @snd_pid: netlink pid of sender
34 * @nlhdr: netlink message header
35 * @genlhdr: generic netlink message header
36 * @userhdr: user specific header
37 * @attrs: netlink attributes
38 */
39struct genl_info
40{
41 u32 snd_seq;
42 u32 snd_pid;
43 struct nlmsghdr * nlhdr;
44 struct genlmsghdr * genlhdr;
45 void * userhdr;
46 struct nlattr ** attrs;
47};
48
49/**
50 * struct genl_ops - generic netlink operations
51 * @cmd: command identifier
52 * @flags: flags
53 * @policy: attribute validation policy
54 * @doit: standard command callback
55 * @dumpit: callback for dumpers
Jamal Hadi Salima4d13662006-12-01 20:07:42 -080056 * @done: completion callback for dumps
Thomas Graf482a8522005-11-10 02:25:56 +010057 * @ops_list: operations list
58 */
59struct genl_ops
60{
Per Lidenb461d2f2006-01-03 14:13:29 -080061 u8 cmd;
Thomas Graf482a8522005-11-10 02:25:56 +010062 unsigned int flags;
Patrick McHardyef7c79e2007-06-05 12:38:30 -070063 const struct nla_policy *policy;
Thomas Graf482a8522005-11-10 02:25:56 +010064 int (*doit)(struct sk_buff *skb,
65 struct genl_info *info);
66 int (*dumpit)(struct sk_buff *skb,
67 struct netlink_callback *cb);
Jamal Hadi Salima4d13662006-12-01 20:07:42 -080068 int (*done)(struct netlink_callback *cb);
Thomas Graf482a8522005-11-10 02:25:56 +010069 struct list_head ops_list;
70};
71
72extern int genl_register_family(struct genl_family *family);
73extern int genl_unregister_family(struct genl_family *family);
74extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
75extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
76
77extern struct sock *genl_sock;
78
79/**
80 * genlmsg_put - Add generic netlink header to netlink message
81 * @skb: socket buffer holding the message
82 * @pid: netlink pid the message is addressed to
83 * @seq: sequence number (usually the one of the sender)
Thomas Graf17c157c2006-11-14 19:46:02 -080084 * @family: generic netlink family
Thomas Graf482a8522005-11-10 02:25:56 +010085 * @flags netlink message flags
86 * @cmd: generic netlink command
Thomas Graf482a8522005-11-10 02:25:56 +010087 *
88 * Returns pointer to user specific header
89 */
90static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
Thomas Graf17c157c2006-11-14 19:46:02 -080091 struct genl_family *family, int flags, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +010092{
93 struct nlmsghdr *nlh;
94 struct genlmsghdr *hdr;
95
Thomas Graf17c157c2006-11-14 19:46:02 -080096 nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
97 family->hdrsize, flags);
Thomas Graf482a8522005-11-10 02:25:56 +010098 if (nlh == NULL)
99 return NULL;
100
101 hdr = nlmsg_data(nlh);
102 hdr->cmd = cmd;
Thomas Graf17c157c2006-11-14 19:46:02 -0800103 hdr->version = family->version;
Thomas Graf482a8522005-11-10 02:25:56 +0100104 hdr->reserved = 0;
105
106 return (char *) hdr + GENL_HDRLEN;
107}
108
109/**
Thomas Graf17c157c2006-11-14 19:46:02 -0800110 * genlmsg_put_reply - Add generic netlink header to a reply message
111 * @skb: socket buffer holding the message
112 * @info: receiver info
113 * @family: generic netlink family
114 * @flags: netlink message flags
115 * @cmd: generic netlink command
116 *
117 * Returns pointer to user specific header
118 */
119static inline void *genlmsg_put_reply(struct sk_buff *skb,
120 struct genl_info *info,
121 struct genl_family *family,
122 int flags, u8 cmd)
123{
124 return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
125 flags, cmd);
126}
127
128/**
Thomas Graf482a8522005-11-10 02:25:56 +0100129 * genlmsg_end - Finalize a generic netlink message
130 * @skb: socket buffer the message is stored in
131 * @hdr: user specific header
132 */
133static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
134{
135 return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
136}
137
138/**
139 * genlmsg_cancel - Cancel construction of a generic netlink message
140 * @skb: socket buffer the message is stored in
141 * @hdr: generic netlink message header
142 */
143static inline int genlmsg_cancel(struct sk_buff *skb, void *hdr)
144{
145 return nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
146}
147
148/**
149 * genlmsg_multicast - multicast a netlink message
150 * @skb: netlink message as socket buffer
151 * @pid: own netlink pid to avoid sending to yourself
152 * @group: multicast group id
Thomas Grafd387f6a2006-08-15 00:31:06 -0700153 * @flags: allocation flags
Thomas Graf482a8522005-11-10 02:25:56 +0100154 */
155static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
Thomas Grafd387f6a2006-08-15 00:31:06 -0700156 unsigned int group, gfp_t flags)
Thomas Graf482a8522005-11-10 02:25:56 +0100157{
Thomas Grafd387f6a2006-08-15 00:31:06 -0700158 return nlmsg_multicast(genl_sock, skb, pid, group, flags);
Thomas Graf482a8522005-11-10 02:25:56 +0100159}
160
161/**
162 * genlmsg_unicast - unicast a netlink message
163 * @skb: netlink message as socket buffer
164 * @pid: netlink pid of the destination socket
165 */
166static inline int genlmsg_unicast(struct sk_buff *skb, u32 pid)
167{
168 return nlmsg_unicast(genl_sock, skb, pid);
169}
170
Balbir Singhfb0ba6bd2006-07-14 00:24:39 -0700171/**
Thomas Graf81878d22006-11-14 19:45:27 -0800172 * genlmsg_reply - reply to a request
173 * @skb: netlink message to be sent back
174 * @info: receiver information
175 */
176static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
177{
178 return genlmsg_unicast(skb, info->snd_pid);
179}
180
181/**
Balbir Singhfb0ba6bd2006-07-14 00:24:39 -0700182 * gennlmsg_data - head of message payload
183 * @gnlh: genetlink messsage header
184 */
185static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
186{
187 return ((unsigned char *) gnlh + GENL_HDRLEN);
188}
189
190/**
191 * genlmsg_len - length of message payload
192 * @gnlh: genetlink message header
193 */
194static inline int genlmsg_len(const struct genlmsghdr *gnlh)
195{
196 struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
197 NLMSG_HDRLEN);
198 return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
199}
200
Balbir Singh17db9522006-09-30 23:28:51 -0700201/**
202 * genlmsg_msg_size - length of genetlink message not including padding
203 * @payload: length of message payload
204 */
205static inline int genlmsg_msg_size(int payload)
206{
207 return GENL_HDRLEN + payload;
208}
209
210/**
211 * genlmsg_total_size - length of genetlink message including padding
212 * @payload: length of message payload
213 */
214static inline int genlmsg_total_size(int payload)
215{
216 return NLMSG_ALIGN(genlmsg_msg_size(payload));
217}
218
Thomas Graf3dabc712006-11-14 19:44:52 -0800219/**
220 * genlmsg_new - Allocate a new generic netlink message
221 * @payload: size of the message payload
222 * @flags: the type of memory to allocate.
223 */
224static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
225{
226 return nlmsg_new(genlmsg_total_size(payload), flags);
227}
228
229
Thomas Graf482a8522005-11-10 02:25:56 +0100230#endif /* __NET_GENERIC_NETLINK_H */