blob: 7fd131c9a8ccee66ab22ca8f6838784fc72a13fd [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
56 * @ops_list: operations list
57 */
58struct genl_ops
59{
Per Lidenb461d2f22006-01-03 14:13:29 -080060 u8 cmd;
Thomas Graf482a8522005-11-10 02:25:56 +010061 unsigned int flags;
62 struct nla_policy *policy;
63 int (*doit)(struct sk_buff *skb,
64 struct genl_info *info);
65 int (*dumpit)(struct sk_buff *skb,
66 struct netlink_callback *cb);
67 struct list_head ops_list;
68};
69
70extern int genl_register_family(struct genl_family *family);
71extern int genl_unregister_family(struct genl_family *family);
72extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
73extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
74
75extern struct sock *genl_sock;
76
77/**
78 * genlmsg_put - Add generic netlink header to netlink message
79 * @skb: socket buffer holding the message
80 * @pid: netlink pid the message is addressed to
81 * @seq: sequence number (usually the one of the sender)
Thomas Graf17c157c2006-11-14 19:46:02 -080082 * @family: generic netlink family
Thomas Graf482a8522005-11-10 02:25:56 +010083 * @flags netlink message flags
84 * @cmd: generic netlink command
Thomas Graf482a8522005-11-10 02:25:56 +010085 *
86 * Returns pointer to user specific header
87 */
88static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
Thomas Graf17c157c2006-11-14 19:46:02 -080089 struct genl_family *family, int flags, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +010090{
91 struct nlmsghdr *nlh;
92 struct genlmsghdr *hdr;
93
Thomas Graf17c157c2006-11-14 19:46:02 -080094 nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
95 family->hdrsize, flags);
Thomas Graf482a8522005-11-10 02:25:56 +010096 if (nlh == NULL)
97 return NULL;
98
99 hdr = nlmsg_data(nlh);
100 hdr->cmd = cmd;
Thomas Graf17c157c2006-11-14 19:46:02 -0800101 hdr->version = family->version;
Thomas Graf482a8522005-11-10 02:25:56 +0100102 hdr->reserved = 0;
103
104 return (char *) hdr + GENL_HDRLEN;
105}
106
107/**
Thomas Graf17c157c2006-11-14 19:46:02 -0800108 * genlmsg_put_reply - Add generic netlink header to a reply message
109 * @skb: socket buffer holding the message
110 * @info: receiver info
111 * @family: generic netlink family
112 * @flags: netlink message flags
113 * @cmd: generic netlink command
114 *
115 * Returns pointer to user specific header
116 */
117static inline void *genlmsg_put_reply(struct sk_buff *skb,
118 struct genl_info *info,
119 struct genl_family *family,
120 int flags, u8 cmd)
121{
122 return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
123 flags, cmd);
124}
125
126/**
Thomas Graf482a8522005-11-10 02:25:56 +0100127 * genlmsg_end - Finalize a generic netlink message
128 * @skb: socket buffer the message is stored in
129 * @hdr: user specific header
130 */
131static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
132{
133 return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
134}
135
136/**
137 * genlmsg_cancel - Cancel construction of a generic netlink message
138 * @skb: socket buffer the message is stored in
139 * @hdr: generic netlink message header
140 */
141static inline int genlmsg_cancel(struct sk_buff *skb, void *hdr)
142{
143 return nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
144}
145
146/**
147 * genlmsg_multicast - multicast a netlink message
148 * @skb: netlink message as socket buffer
149 * @pid: own netlink pid to avoid sending to yourself
150 * @group: multicast group id
Thomas Grafd387f6a2006-08-15 00:31:06 -0700151 * @flags: allocation flags
Thomas Graf482a8522005-11-10 02:25:56 +0100152 */
153static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
Thomas Grafd387f6a2006-08-15 00:31:06 -0700154 unsigned int group, gfp_t flags)
Thomas Graf482a8522005-11-10 02:25:56 +0100155{
Thomas Grafd387f6a2006-08-15 00:31:06 -0700156 return nlmsg_multicast(genl_sock, skb, pid, group, flags);
Thomas Graf482a8522005-11-10 02:25:56 +0100157}
158
159/**
160 * genlmsg_unicast - unicast a netlink message
161 * @skb: netlink message as socket buffer
162 * @pid: netlink pid of the destination socket
163 */
164static inline int genlmsg_unicast(struct sk_buff *skb, u32 pid)
165{
166 return nlmsg_unicast(genl_sock, skb, pid);
167}
168
Balbir Singhfb0ba6bd2006-07-14 00:24:39 -0700169/**
Thomas Graf81878d22006-11-14 19:45:27 -0800170 * genlmsg_reply - reply to a request
171 * @skb: netlink message to be sent back
172 * @info: receiver information
173 */
174static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
175{
176 return genlmsg_unicast(skb, info->snd_pid);
177}
178
179/**
Balbir Singhfb0ba6bd2006-07-14 00:24:39 -0700180 * gennlmsg_data - head of message payload
181 * @gnlh: genetlink messsage header
182 */
183static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
184{
185 return ((unsigned char *) gnlh + GENL_HDRLEN);
186}
187
188/**
189 * genlmsg_len - length of message payload
190 * @gnlh: genetlink message header
191 */
192static inline int genlmsg_len(const struct genlmsghdr *gnlh)
193{
194 struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
195 NLMSG_HDRLEN);
196 return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
197}
198
Balbir Singh17db9522006-09-30 23:28:51 -0700199/**
200 * genlmsg_msg_size - length of genetlink message not including padding
201 * @payload: length of message payload
202 */
203static inline int genlmsg_msg_size(int payload)
204{
205 return GENL_HDRLEN + payload;
206}
207
208/**
209 * genlmsg_total_size - length of genetlink message including padding
210 * @payload: length of message payload
211 */
212static inline int genlmsg_total_size(int payload)
213{
214 return NLMSG_ALIGN(genlmsg_msg_size(payload));
215}
216
Thomas Graf3dabc712006-11-14 19:44:52 -0800217/**
218 * genlmsg_new - Allocate a new generic netlink message
219 * @payload: size of the message payload
220 * @flags: the type of memory to allocate.
221 */
222static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
223{
224 return nlmsg_new(genlmsg_total_size(payload), flags);
225}
226
227
Thomas Graf482a8522005-11-10 02:25:56 +0100228#endif /* __NET_GENERIC_NETLINK_H */