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