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