Merge branch 'mini2440-dev-likely' into mini2440-dev
[linux-2.6/mini2440.git] / include / net / genetlink.h
blob2a1c06874c42f212970513f585d248c148b58281
1 #ifndef __NET_GENERIC_NETLINK_H
2 #define __NET_GENERIC_NETLINK_H
4 #include <linux/genetlink.h>
5 #include <net/netlink.h>
6 #include <net/net_namespace.h>
8 /**
9 * struct genl_multicast_group - generic netlink multicast group
10 * @name: name of the multicast group, names are per-family
11 * @id: multicast group ID, assigned by the core, to use with
12 * genlmsg_multicast().
13 * @list: list entry for linking
14 * @family: pointer to family, need not be set before registering
16 struct genl_multicast_group
18 struct genl_family *family; /* private */
19 struct list_head list; /* private */
20 char name[GENL_NAMSIZ];
21 u32 id;
24 /**
25 * struct genl_family - generic netlink family
26 * @id: protocol family idenfitier
27 * @hdrsize: length of user specific header in bytes
28 * @name: name of family
29 * @version: protocol version
30 * @maxattr: maximum number of attributes supported
31 * @netnsok: set to true if the family can handle network
32 * namespaces and should be presented in all of them
33 * @attrbuf: buffer to store parsed attributes
34 * @ops_list: list of all assigned operations
35 * @family_list: family list
36 * @mcast_groups: multicast groups list
38 struct genl_family
40 unsigned int id;
41 unsigned int hdrsize;
42 char name[GENL_NAMSIZ];
43 unsigned int version;
44 unsigned int maxattr;
45 bool netnsok;
46 struct nlattr ** attrbuf; /* private */
47 struct list_head ops_list; /* private */
48 struct list_head family_list; /* private */
49 struct list_head mcast_groups; /* private */
52 /**
53 * struct genl_info - receiving information
54 * @snd_seq: sending sequence number
55 * @snd_pid: netlink pid of sender
56 * @nlhdr: netlink message header
57 * @genlhdr: generic netlink message header
58 * @userhdr: user specific header
59 * @attrs: netlink attributes
61 struct genl_info
63 u32 snd_seq;
64 u32 snd_pid;
65 struct nlmsghdr * nlhdr;
66 struct genlmsghdr * genlhdr;
67 void * userhdr;
68 struct nlattr ** attrs;
69 #ifdef CONFIG_NET_NS
70 struct net * _net;
71 #endif
74 #ifdef CONFIG_NET_NS
75 static inline struct net *genl_info_net(struct genl_info *info)
77 return info->_net;
80 static inline void genl_info_net_set(struct genl_info *info, struct net *net)
82 info->_net = net;
84 #else
85 static inline struct net *genl_info_net(struct genl_info *info)
87 return &init_net;
90 static inline void genl_info_net_set(struct genl_info *info, struct net *net)
93 #endif
95 /**
96 * struct genl_ops - generic netlink operations
97 * @cmd: command identifier
98 * @flags: flags
99 * @policy: attribute validation policy
100 * @doit: standard command callback
101 * @dumpit: callback for dumpers
102 * @done: completion callback for dumps
103 * @ops_list: operations list
105 struct genl_ops
107 u8 cmd;
108 unsigned int flags;
109 const struct nla_policy *policy;
110 int (*doit)(struct sk_buff *skb,
111 struct genl_info *info);
112 int (*dumpit)(struct sk_buff *skb,
113 struct netlink_callback *cb);
114 int (*done)(struct netlink_callback *cb);
115 struct list_head ops_list;
118 extern int genl_register_family(struct genl_family *family);
119 extern int genl_register_family_with_ops(struct genl_family *family,
120 struct genl_ops *ops, size_t n_ops);
121 extern int genl_unregister_family(struct genl_family *family);
122 extern int genl_register_ops(struct genl_family *, struct genl_ops *ops);
123 extern int genl_unregister_ops(struct genl_family *, struct genl_ops *ops);
124 extern int genl_register_mc_group(struct genl_family *family,
125 struct genl_multicast_group *grp);
126 extern void genl_unregister_mc_group(struct genl_family *family,
127 struct genl_multicast_group *grp);
130 * genlmsg_put - Add generic netlink header to netlink message
131 * @skb: socket buffer holding the message
132 * @pid: netlink pid the message is addressed to
133 * @seq: sequence number (usually the one of the sender)
134 * @family: generic netlink family
135 * @flags netlink message flags
136 * @cmd: generic netlink command
138 * Returns pointer to user specific header
140 static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
141 struct genl_family *family, int flags, u8 cmd)
143 struct nlmsghdr *nlh;
144 struct genlmsghdr *hdr;
146 nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
147 family->hdrsize, flags);
148 if (nlh == NULL)
149 return NULL;
151 hdr = nlmsg_data(nlh);
152 hdr->cmd = cmd;
153 hdr->version = family->version;
154 hdr->reserved = 0;
156 return (char *) hdr + GENL_HDRLEN;
160 * genlmsg_put_reply - Add generic netlink header to a reply message
161 * @skb: socket buffer holding the message
162 * @info: receiver info
163 * @family: generic netlink family
164 * @flags: netlink message flags
165 * @cmd: generic netlink command
167 * Returns pointer to user specific header
169 static inline void *genlmsg_put_reply(struct sk_buff *skb,
170 struct genl_info *info,
171 struct genl_family *family,
172 int flags, u8 cmd)
174 return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
175 flags, cmd);
179 * genlmsg_end - Finalize a generic netlink message
180 * @skb: socket buffer the message is stored in
181 * @hdr: user specific header
183 static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
185 return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
189 * genlmsg_cancel - Cancel construction of a generic netlink message
190 * @skb: socket buffer the message is stored in
191 * @hdr: generic netlink message header
193 static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
195 nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
199 * genlmsg_multicast_netns - multicast a netlink message to a specific netns
200 * @net: the net namespace
201 * @skb: netlink message as socket buffer
202 * @pid: own netlink pid to avoid sending to yourself
203 * @group: multicast group id
204 * @flags: allocation flags
206 static inline int genlmsg_multicast_netns(struct net *net, struct sk_buff *skb,
207 u32 pid, unsigned int group, gfp_t flags)
209 return nlmsg_multicast(net->genl_sock, skb, pid, group, flags);
213 * genlmsg_multicast - multicast a netlink message to the default netns
214 * @skb: netlink message as socket buffer
215 * @pid: own netlink pid to avoid sending to yourself
216 * @group: multicast group id
217 * @flags: allocation flags
219 static inline int genlmsg_multicast(struct sk_buff *skb, u32 pid,
220 unsigned int group, gfp_t flags)
222 return genlmsg_multicast_netns(&init_net, skb, pid, group, flags);
226 * genlmsg_multicast_allns - multicast a netlink message to all net namespaces
227 * @skb: netlink message as socket buffer
228 * @pid: own netlink pid to avoid sending to yourself
229 * @group: multicast group id
230 * @flags: allocation flags
232 * This function must hold the RTNL or rcu_read_lock().
234 int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid,
235 unsigned int group, gfp_t flags);
238 * genlmsg_unicast - unicast a netlink message
239 * @skb: netlink message as socket buffer
240 * @pid: netlink pid of the destination socket
242 static inline int genlmsg_unicast(struct net *net, struct sk_buff *skb, u32 pid)
244 return nlmsg_unicast(net->genl_sock, skb, pid);
248 * genlmsg_reply - reply to a request
249 * @skb: netlink message to be sent back
250 * @info: receiver information
252 static inline int genlmsg_reply(struct sk_buff *skb, struct genl_info *info)
254 return genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
258 * gennlmsg_data - head of message payload
259 * @gnlh: genetlink messsage header
261 static inline void *genlmsg_data(const struct genlmsghdr *gnlh)
263 return ((unsigned char *) gnlh + GENL_HDRLEN);
267 * genlmsg_len - length of message payload
268 * @gnlh: genetlink message header
270 static inline int genlmsg_len(const struct genlmsghdr *gnlh)
272 struct nlmsghdr *nlh = (struct nlmsghdr *)((unsigned char *)gnlh -
273 NLMSG_HDRLEN);
274 return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
278 * genlmsg_msg_size - length of genetlink message not including padding
279 * @payload: length of message payload
281 static inline int genlmsg_msg_size(int payload)
283 return GENL_HDRLEN + payload;
287 * genlmsg_total_size - length of genetlink message including padding
288 * @payload: length of message payload
290 static inline int genlmsg_total_size(int payload)
292 return NLMSG_ALIGN(genlmsg_msg_size(payload));
296 * genlmsg_new - Allocate a new generic netlink message
297 * @payload: size of the message payload
298 * @flags: the type of memory to allocate.
300 static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
302 return nlmsg_new(genlmsg_total_size(payload), flags);
306 #endif /* __NET_GENERIC_NETLINK_H */