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