Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 1 | #ifndef __NET_NETLINK_H |
| 2 | #define __NET_NETLINK_H |
| 3 | |
| 4 | #include <linux/types.h> |
| 5 | #include <linux/netlink.h> |
| 6 | |
| 7 | /* ======================================================================== |
| 8 | * Netlink Messages and Attributes Interface (As Seen On TV) |
| 9 | * ------------------------------------------------------------------------ |
| 10 | * Messages Interface |
| 11 | * ------------------------------------------------------------------------ |
| 12 | * |
| 13 | * Message Format: |
| 14 | * <--- nlmsg_total_size(payload) ---> |
| 15 | * <-- nlmsg_msg_size(payload) -> |
| 16 | * +----------+- - -+-------------+- - -+-------- - - |
| 17 | * | nlmsghdr | Pad | Payload | Pad | nlmsghdr |
| 18 | * +----------+- - -+-------------+- - -+-------- - - |
| 19 | * nlmsg_data(nlh)---^ ^ |
| 20 | * nlmsg_next(nlh)-----------------------+ |
| 21 | * |
| 22 | * Payload Format: |
| 23 | * <---------------------- nlmsg_len(nlh) ---------------------> |
| 24 | * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) -> |
| 25 | * +----------------------+- - -+--------------------------------+ |
| 26 | * | Family Header | Pad | Attributes | |
| 27 | * +----------------------+- - -+--------------------------------+ |
| 28 | * nlmsg_attrdata(nlh, hdrlen)---^ |
| 29 | * |
| 30 | * Data Structures: |
| 31 | * struct nlmsghdr netlink message header |
| 32 | * |
| 33 | * Message Construction: |
| 34 | * nlmsg_new() create a new netlink message |
| 35 | * nlmsg_put() add a netlink message to an skb |
| 36 | * nlmsg_put_answer() callback based nlmsg_put() |
| 37 | * nlmsg_end() finanlize netlink message |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 38 | * nlmsg_get_pos() return current position in message |
| 39 | * nlmsg_trim() trim part of message |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 40 | * nlmsg_cancel() cancel message construction |
| 41 | * nlmsg_free() free a netlink message |
| 42 | * |
| 43 | * Message Sending: |
| 44 | * nlmsg_multicast() multicast message to several groups |
| 45 | * nlmsg_unicast() unicast a message to a single socket |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame^] | 46 | * nlmsg_notify() send notification message |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 47 | * |
| 48 | * Message Length Calculations: |
| 49 | * nlmsg_msg_size(payload) length of message w/o padding |
| 50 | * nlmsg_total_size(payload) length of message w/ padding |
| 51 | * nlmsg_padlen(payload) length of padding at tail |
| 52 | * |
| 53 | * Message Payload Access: |
| 54 | * nlmsg_data(nlh) head of message payload |
| 55 | * nlmsg_len(nlh) length of message payload |
| 56 | * nlmsg_attrdata(nlh, hdrlen) head of attributes data |
| 57 | * nlmsg_attrlen(nlh, hdrlen) length of attributes data |
| 58 | * |
| 59 | * Message Parsing: |
| 60 | * nlmsg_ok(nlh, remaining) does nlh fit into remaining bytes? |
| 61 | * nlmsg_next(nlh, remaining) get next netlink message |
| 62 | * nlmsg_parse() parse attributes of a message |
| 63 | * nlmsg_find_attr() find an attribute in a message |
| 64 | * nlmsg_for_each_msg() loop over all messages |
| 65 | * nlmsg_validate() validate netlink message incl. attrs |
| 66 | * nlmsg_for_each_attr() loop over all attributes |
| 67 | * |
| 68 | * ------------------------------------------------------------------------ |
| 69 | * Attributes Interface |
| 70 | * ------------------------------------------------------------------------ |
| 71 | * |
| 72 | * Attribute Format: |
| 73 | * <------- nla_total_size(payload) -------> |
| 74 | * <---- nla_attr_size(payload) -----> |
| 75 | * +----------+- - -+- - - - - - - - - +- - -+-------- - - |
| 76 | * | Header | Pad | Payload | Pad | Header |
| 77 | * +----------+- - -+- - - - - - - - - +- - -+-------- - - |
| 78 | * <- nla_len(nla) -> ^ |
| 79 | * nla_data(nla)----^ | |
| 80 | * nla_next(nla)-----------------------------' |
| 81 | * |
| 82 | * Data Structures: |
| 83 | * struct nlattr netlink attribtue header |
| 84 | * |
| 85 | * Attribute Construction: |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 86 | * nla_reserve(skb, type, len) reserve room for an attribute |
| 87 | * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 88 | * nla_put(skb, type, len, data) add attribute to skb |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 89 | * nla_put_nohdr(skb, len, data) add attribute w/o hdr |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 90 | * |
| 91 | * Attribute Construction for Basic Types: |
| 92 | * nla_put_u8(skb, type, value) add u8 attribute to skb |
| 93 | * nla_put_u16(skb, type, value) add u16 attribute to skb |
| 94 | * nla_put_u32(skb, type, value) add u32 attribute to skb |
| 95 | * nla_put_u64(skb, type, value) add u64 attribute to skb |
| 96 | * nla_put_string(skb, type, str) add string attribute to skb |
| 97 | * nla_put_flag(skb, type) add flag attribute to skb |
| 98 | * nla_put_msecs(skb, type, jiffies) add msecs attribute to skb |
| 99 | * |
| 100 | * Exceptions Based Attribute Construction: |
| 101 | * NLA_PUT(skb, type, len, data) add attribute to skb |
| 102 | * NLA_PUT_U8(skb, type, value) add u8 attribute to skb |
| 103 | * NLA_PUT_U16(skb, type, value) add u16 attribute to skb |
| 104 | * NLA_PUT_U32(skb, type, value) add u32 attribute to skb |
| 105 | * NLA_PUT_U64(skb, type, value) add u64 attribute to skb |
| 106 | * NLA_PUT_STRING(skb, type, str) add string attribute to skb |
| 107 | * NLA_PUT_FLAG(skb, type) add flag attribute to skb |
| 108 | * NLA_PUT_MSECS(skb, type, jiffies) add msecs attribute to skb |
| 109 | * |
| 110 | * The meaning of these functions is equal to their lower case |
| 111 | * variants but they jump to the label nla_put_failure in case |
| 112 | * of a failure. |
| 113 | * |
| 114 | * Nested Attributes Construction: |
| 115 | * nla_nest_start(skb, type) start a nested attribute |
| 116 | * nla_nest_end(skb, nla) finalize a nested attribute |
| 117 | * nla_nest_cancel(skb, nla) cancel nested attribute construction |
| 118 | * |
| 119 | * Attribute Length Calculations: |
| 120 | * nla_attr_size(payload) length of attribute w/o padding |
| 121 | * nla_total_size(payload) length of attribute w/ padding |
| 122 | * nla_padlen(payload) length of padding |
| 123 | * |
| 124 | * Attribute Payload Access: |
| 125 | * nla_data(nla) head of attribute payload |
| 126 | * nla_len(nla) length of attribute payload |
| 127 | * |
| 128 | * Attribute Payload Access for Basic Types: |
| 129 | * nla_get_u8(nla) get payload for a u8 attribute |
| 130 | * nla_get_u16(nla) get payload for a u16 attribute |
| 131 | * nla_get_u32(nla) get payload for a u32 attribute |
| 132 | * nla_get_u64(nla) get payload for a u64 attribute |
| 133 | * nla_get_flag(nla) return 1 if flag is true |
| 134 | * nla_get_msecs(nla) get payload for a msecs attribute |
| 135 | * |
| 136 | * Attribute Misc: |
| 137 | * nla_memcpy(dest, nla, count) copy attribute into memory |
| 138 | * nla_memcmp(nla, data, size) compare attribute with memory area |
| 139 | * nla_strlcpy(dst, nla, size) copy attribute to a sized string |
| 140 | * nla_strcmp(nla, str) compare attribute with string |
| 141 | * |
| 142 | * Attribute Parsing: |
| 143 | * nla_ok(nla, remaining) does nla fit into remaining bytes? |
| 144 | * nla_next(nla, remaining) get next netlink attribute |
| 145 | * nla_validate() validate a stream of attributes |
| 146 | * nla_find() find attribute in stream of attributes |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 147 | * nla_find_nested() find attribute in nested attributes |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 148 | * nla_parse() parse and validate stream of attrs |
| 149 | * nla_parse_nested() parse nested attribuets |
| 150 | * nla_for_each_attr() loop over all attributes |
| 151 | *========================================================================= |
| 152 | */ |
| 153 | |
| 154 | /** |
| 155 | * Standard attribute types to specify validation policy |
| 156 | */ |
| 157 | enum { |
| 158 | NLA_UNSPEC, |
| 159 | NLA_U8, |
| 160 | NLA_U16, |
| 161 | NLA_U32, |
| 162 | NLA_U64, |
| 163 | NLA_STRING, |
| 164 | NLA_FLAG, |
| 165 | NLA_MSECS, |
| 166 | NLA_NESTED, |
| 167 | __NLA_TYPE_MAX, |
| 168 | }; |
| 169 | |
| 170 | #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1) |
| 171 | |
| 172 | /** |
| 173 | * struct nla_policy - attribute validation policy |
| 174 | * @type: Type of attribute or NLA_UNSPEC |
| 175 | * @minlen: Minimal length of payload required to be available |
| 176 | * |
| 177 | * Policies are defined as arrays of this struct, the array must be |
| 178 | * accessible by attribute type up to the highest identifier to be expected. |
| 179 | * |
| 180 | * Example: |
| 181 | * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = { |
| 182 | * [ATTR_FOO] = { .type = NLA_U16 }, |
| 183 | * [ATTR_BAR] = { .type = NLA_STRING }, |
| 184 | * [ATTR_BAZ] = { .minlen = sizeof(struct mystruct) }, |
| 185 | * }; |
| 186 | */ |
| 187 | struct nla_policy { |
| 188 | u16 type; |
| 189 | u16 minlen; |
| 190 | }; |
| 191 | |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 192 | extern void netlink_run_queue(struct sock *sk, unsigned int *qlen, |
| 193 | int (*cb)(struct sk_buff *, |
| 194 | struct nlmsghdr *, int *)); |
| 195 | extern void netlink_queue_skip(struct nlmsghdr *nlh, |
| 196 | struct sk_buff *skb); |
| 197 | |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 198 | extern int nla_validate(struct nlattr *head, int len, int maxtype, |
| 199 | struct nla_policy *policy); |
| 200 | extern int nla_parse(struct nlattr *tb[], int maxtype, |
| 201 | struct nlattr *head, int len, |
| 202 | struct nla_policy *policy); |
| 203 | extern struct nlattr * nla_find(struct nlattr *head, int len, int attrtype); |
| 204 | extern size_t nla_strlcpy(char *dst, const struct nlattr *nla, |
| 205 | size_t dstsize); |
| 206 | extern int nla_memcpy(void *dest, struct nlattr *src, int count); |
| 207 | extern int nla_memcmp(const struct nlattr *nla, const void *data, |
| 208 | size_t size); |
| 209 | extern int nla_strcmp(const struct nlattr *nla, const char *str); |
| 210 | extern struct nlattr * __nla_reserve(struct sk_buff *skb, int attrtype, |
| 211 | int attrlen); |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 212 | extern void * __nla_reserve_nohdr(struct sk_buff *skb, int attrlen); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 213 | extern struct nlattr * nla_reserve(struct sk_buff *skb, int attrtype, |
| 214 | int attrlen); |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 215 | extern void * nla_reserve_nohdr(struct sk_buff *skb, int attrlen); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 216 | extern void __nla_put(struct sk_buff *skb, int attrtype, |
| 217 | int attrlen, const void *data); |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 218 | extern void __nla_put_nohdr(struct sk_buff *skb, int attrlen, |
| 219 | const void *data); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 220 | extern int nla_put(struct sk_buff *skb, int attrtype, |
| 221 | int attrlen, const void *data); |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 222 | extern int nla_put_nohdr(struct sk_buff *skb, int attrlen, |
| 223 | const void *data); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 224 | |
| 225 | /************************************************************************** |
| 226 | * Netlink Messages |
| 227 | **************************************************************************/ |
| 228 | |
| 229 | /** |
| 230 | * nlmsg_msg_size - length of netlink message not including padding |
| 231 | * @payload: length of message payload |
| 232 | */ |
| 233 | static inline int nlmsg_msg_size(int payload) |
| 234 | { |
| 235 | return NLMSG_HDRLEN + payload; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * nlmsg_total_size - length of netlink message including padding |
| 240 | * @payload: length of message payload |
| 241 | */ |
| 242 | static inline int nlmsg_total_size(int payload) |
| 243 | { |
| 244 | return NLMSG_ALIGN(nlmsg_msg_size(payload)); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * nlmsg_padlen - length of padding at the message's tail |
| 249 | * @payload: length of message payload |
| 250 | */ |
| 251 | static inline int nlmsg_padlen(int payload) |
| 252 | { |
| 253 | return nlmsg_total_size(payload) - nlmsg_msg_size(payload); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * nlmsg_data - head of message payload |
| 258 | * @nlh: netlink messsage header |
| 259 | */ |
| 260 | static inline void *nlmsg_data(const struct nlmsghdr *nlh) |
| 261 | { |
| 262 | return (unsigned char *) nlh + NLMSG_HDRLEN; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * nlmsg_len - length of message payload |
| 267 | * @nlh: netlink message header |
| 268 | */ |
| 269 | static inline int nlmsg_len(const struct nlmsghdr *nlh) |
| 270 | { |
| 271 | return nlh->nlmsg_len - NLMSG_HDRLEN; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * nlmsg_attrdata - head of attributes data |
| 276 | * @nlh: netlink message header |
| 277 | * @hdrlen: length of family specific header |
| 278 | */ |
| 279 | static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, |
| 280 | int hdrlen) |
| 281 | { |
| 282 | unsigned char *data = nlmsg_data(nlh); |
| 283 | return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen)); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * nlmsg_attrlen - length of attributes data |
| 288 | * @nlh: netlink message header |
| 289 | * @hdrlen: length of family specific header |
| 290 | */ |
| 291 | static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen) |
| 292 | { |
| 293 | return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * nlmsg_ok - check if the netlink message fits into the remaining bytes |
| 298 | * @nlh: netlink message header |
| 299 | * @remaining: number of bytes remaining in message stream |
| 300 | */ |
| 301 | static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining) |
| 302 | { |
| 303 | return (remaining >= sizeof(struct nlmsghdr) && |
| 304 | nlh->nlmsg_len >= sizeof(struct nlmsghdr) && |
| 305 | nlh->nlmsg_len <= remaining); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * nlmsg_next - next netlink message in message stream |
| 310 | * @nlh: netlink message header |
| 311 | * @remaining: number of bytes remaining in message stream |
| 312 | * |
| 313 | * Returns the next netlink message in the message stream and |
| 314 | * decrements remaining by the size of the current message. |
| 315 | */ |
| 316 | static inline struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining) |
| 317 | { |
| 318 | int totlen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 319 | |
| 320 | *remaining -= totlen; |
| 321 | |
| 322 | return (struct nlmsghdr *) ((unsigned char *) nlh + totlen); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * nlmsg_parse - parse attributes of a netlink message |
| 327 | * @nlh: netlink message header |
| 328 | * @hdrlen: length of family specific header |
| 329 | * @tb: destination array with maxtype+1 elements |
| 330 | * @maxtype: maximum attribute type to be expected |
| 331 | * @policy: validation policy |
| 332 | * |
| 333 | * See nla_parse() |
| 334 | */ |
| 335 | static inline int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, |
| 336 | struct nlattr *tb[], int maxtype, |
| 337 | struct nla_policy *policy) |
| 338 | { |
| 339 | if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) |
| 340 | return -EINVAL; |
| 341 | |
| 342 | return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), |
| 343 | nlmsg_attrlen(nlh, hdrlen), policy); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * nlmsg_find_attr - find a specific attribute in a netlink message |
| 348 | * @nlh: netlink message header |
| 349 | * @hdrlen: length of familiy specific header |
| 350 | * @attrtype: type of attribute to look for |
| 351 | * |
| 352 | * Returns the first attribute which matches the specified type. |
| 353 | */ |
| 354 | static inline struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh, |
| 355 | int hdrlen, int attrtype) |
| 356 | { |
| 357 | return nla_find(nlmsg_attrdata(nlh, hdrlen), |
| 358 | nlmsg_attrlen(nlh, hdrlen), attrtype); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * nlmsg_validate - validate a netlink message including attributes |
| 363 | * @nlh: netlinket message header |
| 364 | * @hdrlen: length of familiy specific header |
| 365 | * @maxtype: maximum attribute type to be expected |
| 366 | * @policy: validation policy |
| 367 | */ |
| 368 | static inline int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype, |
| 369 | struct nla_policy *policy) |
| 370 | { |
| 371 | if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) |
| 372 | return -EINVAL; |
| 373 | |
| 374 | return nla_validate(nlmsg_attrdata(nlh, hdrlen), |
| 375 | nlmsg_attrlen(nlh, hdrlen), maxtype, policy); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * nlmsg_for_each_attr - iterate over a stream of attributes |
| 380 | * @pos: loop counter, set to current attribute |
| 381 | * @nlh: netlink message header |
| 382 | * @hdrlen: length of familiy specific header |
| 383 | * @rem: initialized to len, holds bytes currently remaining in stream |
| 384 | */ |
| 385 | #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \ |
| 386 | nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \ |
| 387 | nlmsg_attrlen(nlh, hdrlen), rem) |
| 388 | |
| 389 | #if 0 |
| 390 | /* FIXME: Enable once all users have been converted */ |
| 391 | |
| 392 | /** |
| 393 | * __nlmsg_put - Add a new netlink message to an skb |
| 394 | * @skb: socket buffer to store message in |
| 395 | * @pid: netlink process id |
| 396 | * @seq: sequence number of message |
| 397 | * @type: message type |
| 398 | * @payload: length of message payload |
| 399 | * @flags: message flags |
| 400 | * |
| 401 | * The caller is responsible to ensure that the skb provides enough |
| 402 | * tailroom for both the netlink header and payload. |
| 403 | */ |
| 404 | static inline struct nlmsghdr *__nlmsg_put(struct sk_buff *skb, u32 pid, |
| 405 | u32 seq, int type, int payload, |
| 406 | int flags) |
| 407 | { |
| 408 | struct nlmsghdr *nlh; |
| 409 | |
| 410 | nlh = (struct nlmsghdr *) skb_put(skb, nlmsg_total_size(payload)); |
| 411 | nlh->nlmsg_type = type; |
| 412 | nlh->nlmsg_len = nlmsg_msg_size(payload); |
| 413 | nlh->nlmsg_flags = flags; |
| 414 | nlh->nlmsg_pid = pid; |
| 415 | nlh->nlmsg_seq = seq; |
| 416 | |
| 417 | memset((unsigned char *) nlmsg_data(nlh) + payload, 0, |
| 418 | nlmsg_padlen(payload)); |
| 419 | |
| 420 | return nlh; |
| 421 | } |
| 422 | #endif |
| 423 | |
| 424 | /** |
| 425 | * nlmsg_put - Add a new netlink message to an skb |
| 426 | * @skb: socket buffer to store message in |
| 427 | * @pid: netlink process id |
| 428 | * @seq: sequence number of message |
| 429 | * @type: message type |
| 430 | * @payload: length of message payload |
| 431 | * @flags: message flags |
| 432 | * |
| 433 | * Returns NULL if the tailroom of the skb is insufficient to store |
| 434 | * the message header and payload. |
| 435 | */ |
| 436 | static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq, |
| 437 | int type, int payload, int flags) |
| 438 | { |
| 439 | if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload))) |
| 440 | return NULL; |
| 441 | |
| 442 | return __nlmsg_put(skb, pid, seq, type, payload, flags); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * nlmsg_put_answer - Add a new callback based netlink message to an skb |
| 447 | * @skb: socket buffer to store message in |
| 448 | * @cb: netlink callback |
| 449 | * @type: message type |
| 450 | * @payload: length of message payload |
| 451 | * @flags: message flags |
| 452 | * |
| 453 | * Returns NULL if the tailroom of the skb is insufficient to store |
| 454 | * the message header and payload. |
| 455 | */ |
| 456 | static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb, |
| 457 | struct netlink_callback *cb, |
| 458 | int type, int payload, |
| 459 | int flags) |
| 460 | { |
| 461 | return nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, |
| 462 | type, payload, flags); |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * nlmsg_new - Allocate a new netlink message |
| 467 | * @size: maximum size of message |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 468 | * @flags: the type of memory to allocate. |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 469 | * |
| 470 | * Use NLMSG_GOODSIZE if size isn't know and you need a good default size. |
| 471 | */ |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 472 | static inline struct sk_buff *nlmsg_new(int size, gfp_t flags) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 473 | { |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 474 | return alloc_skb(size, flags); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /** |
| 478 | * nlmsg_end - Finalize a netlink message |
| 479 | * @skb: socket buffer the message is stored in |
| 480 | * @nlh: netlink message header |
| 481 | * |
| 482 | * Corrects the netlink message header to include the appeneded |
| 483 | * attributes. Only necessary if attributes have been added to |
| 484 | * the message. |
| 485 | * |
| 486 | * Returns the total data length of the skb. |
| 487 | */ |
| 488 | static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 489 | { |
| 490 | nlh->nlmsg_len = skb->tail - (unsigned char *) nlh; |
| 491 | |
| 492 | return skb->len; |
| 493 | } |
| 494 | |
| 495 | /** |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 496 | * nlmsg_get_pos - return current position in netlink message |
| 497 | * @skb: socket buffer the message is stored in |
| 498 | * |
| 499 | * Returns a pointer to the current tail of the message. |
| 500 | */ |
| 501 | static inline void *nlmsg_get_pos(struct sk_buff *skb) |
| 502 | { |
| 503 | return skb->tail; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * nlmsg_trim - Trim message to a mark |
| 508 | * @skb: socket buffer the message is stored in |
| 509 | * @mark: mark to trim to |
| 510 | * |
| 511 | * Trims the message to the provided mark. Returns -1. |
| 512 | */ |
| 513 | static inline int nlmsg_trim(struct sk_buff *skb, void *mark) |
| 514 | { |
| 515 | if (mark) |
| 516 | skb_trim(skb, (unsigned char *) mark - skb->data); |
| 517 | |
| 518 | return -1; |
| 519 | } |
| 520 | |
| 521 | /** |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 522 | * nlmsg_cancel - Cancel construction of a netlink message |
| 523 | * @skb: socket buffer the message is stored in |
| 524 | * @nlh: netlink message header |
| 525 | * |
| 526 | * Removes the complete netlink message including all |
| 527 | * attributes from the socket buffer again. Returns -1. |
| 528 | */ |
| 529 | static inline int nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 530 | { |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 531 | return nlmsg_trim(skb, nlh); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /** |
| 535 | * nlmsg_free - free a netlink message |
| 536 | * @skb: socket buffer of netlink message |
| 537 | */ |
| 538 | static inline void nlmsg_free(struct sk_buff *skb) |
| 539 | { |
| 540 | kfree_skb(skb); |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * nlmsg_multicast - multicast a netlink message |
| 545 | * @sk: netlink socket to spread messages to |
| 546 | * @skb: netlink message as socket buffer |
| 547 | * @pid: own netlink pid to avoid sending to yourself |
| 548 | * @group: multicast group id |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame^] | 549 | * @flags: allocation flags |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 550 | */ |
| 551 | static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame^] | 552 | u32 pid, unsigned int group, gfp_t flags) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 553 | { |
| 554 | int err; |
| 555 | |
| 556 | NETLINK_CB(skb).dst_group = group; |
| 557 | |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame^] | 558 | err = netlink_broadcast(sk, skb, pid, group, flags); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 559 | if (err > 0) |
| 560 | err = 0; |
| 561 | |
| 562 | return err; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * nlmsg_unicast - unicast a netlink message |
| 567 | * @sk: netlink socket to spread message to |
| 568 | * @skb: netlink message as socket buffer |
| 569 | * @pid: netlink pid of the destination socket |
| 570 | */ |
| 571 | static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 pid) |
| 572 | { |
| 573 | int err; |
| 574 | |
| 575 | err = netlink_unicast(sk, skb, pid, MSG_DONTWAIT); |
| 576 | if (err > 0) |
| 577 | err = 0; |
| 578 | |
| 579 | return err; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * nlmsg_for_each_msg - iterate over a stream of messages |
| 584 | * @pos: loop counter, set to current message |
| 585 | * @head: head of message stream |
| 586 | * @len: length of message stream |
| 587 | * @rem: initialized to len, holds bytes currently remaining in stream |
| 588 | */ |
| 589 | #define nlmsg_for_each_msg(pos, head, len, rem) \ |
| 590 | for (pos = head, rem = len; \ |
| 591 | nlmsg_ok(pos, rem); \ |
| 592 | pos = nlmsg_next(pos, &(rem))) |
| 593 | |
| 594 | /************************************************************************** |
| 595 | * Netlink Attributes |
| 596 | **************************************************************************/ |
| 597 | |
| 598 | /** |
| 599 | * nla_attr_size - length of attribute not including padding |
| 600 | * @payload: length of payload |
| 601 | */ |
| 602 | static inline int nla_attr_size(int payload) |
| 603 | { |
| 604 | return NLA_HDRLEN + payload; |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * nla_total_size - total length of attribute including padding |
| 609 | * @payload: length of payload |
| 610 | */ |
| 611 | static inline int nla_total_size(int payload) |
| 612 | { |
| 613 | return NLA_ALIGN(nla_attr_size(payload)); |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * nla_padlen - length of padding at the tail of attribute |
| 618 | * @payload: length of payload |
| 619 | */ |
| 620 | static inline int nla_padlen(int payload) |
| 621 | { |
| 622 | return nla_total_size(payload) - nla_attr_size(payload); |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * nla_data - head of payload |
| 627 | * @nla: netlink attribute |
| 628 | */ |
| 629 | static inline void *nla_data(const struct nlattr *nla) |
| 630 | { |
| 631 | return (char *) nla + NLA_HDRLEN; |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * nla_len - length of payload |
| 636 | * @nla: netlink attribute |
| 637 | */ |
| 638 | static inline int nla_len(const struct nlattr *nla) |
| 639 | { |
| 640 | return nla->nla_len - NLA_HDRLEN; |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * nla_ok - check if the netlink attribute fits into the remaining bytes |
| 645 | * @nla: netlink attribute |
| 646 | * @remaining: number of bytes remaining in attribute stream |
| 647 | */ |
| 648 | static inline int nla_ok(const struct nlattr *nla, int remaining) |
| 649 | { |
| 650 | return remaining >= sizeof(*nla) && |
| 651 | nla->nla_len >= sizeof(*nla) && |
| 652 | nla->nla_len <= remaining; |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * nla_next - next netlink attribte in attribute stream |
| 657 | * @nla: netlink attribute |
| 658 | * @remaining: number of bytes remaining in attribute stream |
| 659 | * |
| 660 | * Returns the next netlink attribute in the attribute stream and |
| 661 | * decrements remaining by the size of the current attribute. |
| 662 | */ |
| 663 | static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining) |
| 664 | { |
| 665 | int totlen = NLA_ALIGN(nla->nla_len); |
| 666 | |
| 667 | *remaining -= totlen; |
| 668 | return (struct nlattr *) ((char *) nla + totlen); |
| 669 | } |
| 670 | |
| 671 | /** |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 672 | * nla_find_nested - find attribute in a set of nested attributes |
| 673 | * @nla: attribute containing the nested attributes |
| 674 | * @attrtype: type of attribute to look for |
| 675 | * |
| 676 | * Returns the first attribute which matches the specified type. |
| 677 | */ |
| 678 | static inline struct nlattr *nla_find_nested(struct nlattr *nla, int attrtype) |
| 679 | { |
| 680 | return nla_find(nla_data(nla), nla_len(nla), attrtype); |
| 681 | } |
| 682 | |
| 683 | /** |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 684 | * nla_parse_nested - parse nested attributes |
| 685 | * @tb: destination array with maxtype+1 elements |
| 686 | * @maxtype: maximum attribute type to be expected |
| 687 | * @nla: attribute containing the nested attributes |
| 688 | * @policy: validation policy |
| 689 | * |
| 690 | * See nla_parse() |
| 691 | */ |
| 692 | static inline int nla_parse_nested(struct nlattr *tb[], int maxtype, |
| 693 | struct nlattr *nla, |
| 694 | struct nla_policy *policy) |
| 695 | { |
| 696 | return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy); |
| 697 | } |
| 698 | /** |
| 699 | * nla_put_u8 - Add a u16 netlink attribute to a socket buffer |
| 700 | * @skb: socket buffer to add attribute to |
| 701 | * @attrtype: attribute type |
| 702 | * @value: numeric value |
| 703 | */ |
| 704 | static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value) |
| 705 | { |
| 706 | return nla_put(skb, attrtype, sizeof(u8), &value); |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * nla_put_u16 - Add a u16 netlink attribute to a socket buffer |
| 711 | * @skb: socket buffer to add attribute to |
| 712 | * @attrtype: attribute type |
| 713 | * @value: numeric value |
| 714 | */ |
| 715 | static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value) |
| 716 | { |
| 717 | return nla_put(skb, attrtype, sizeof(u16), &value); |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * nla_put_u32 - Add a u32 netlink attribute to a socket buffer |
| 722 | * @skb: socket buffer to add attribute to |
| 723 | * @attrtype: attribute type |
| 724 | * @value: numeric value |
| 725 | */ |
| 726 | static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value) |
| 727 | { |
| 728 | return nla_put(skb, attrtype, sizeof(u32), &value); |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * nla_put_64 - Add a u64 netlink attribute to a socket buffer |
| 733 | * @skb: socket buffer to add attribute to |
| 734 | * @attrtype: attribute type |
| 735 | * @value: numeric value |
| 736 | */ |
| 737 | static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value) |
| 738 | { |
| 739 | return nla_put(skb, attrtype, sizeof(u64), &value); |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * nla_put_string - Add a string netlink attribute to a socket buffer |
| 744 | * @skb: socket buffer to add attribute to |
| 745 | * @attrtype: attribute type |
| 746 | * @str: NUL terminated string |
| 747 | */ |
| 748 | static inline int nla_put_string(struct sk_buff *skb, int attrtype, |
| 749 | const char *str) |
| 750 | { |
| 751 | return nla_put(skb, attrtype, strlen(str) + 1, str); |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * nla_put_flag - Add a flag netlink attribute to a socket buffer |
| 756 | * @skb: socket buffer to add attribute to |
| 757 | * @attrtype: attribute type |
| 758 | */ |
| 759 | static inline int nla_put_flag(struct sk_buff *skb, int attrtype) |
| 760 | { |
| 761 | return nla_put(skb, attrtype, 0, NULL); |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * nla_put_msecs - Add a msecs netlink attribute to a socket buffer |
| 766 | * @skb: socket buffer to add attribute to |
| 767 | * @attrtype: attribute type |
| 768 | * @jiffies: number of msecs in jiffies |
| 769 | */ |
| 770 | static inline int nla_put_msecs(struct sk_buff *skb, int attrtype, |
| 771 | unsigned long jiffies) |
| 772 | { |
| 773 | u64 tmp = jiffies_to_msecs(jiffies); |
| 774 | return nla_put(skb, attrtype, sizeof(u64), &tmp); |
| 775 | } |
| 776 | |
| 777 | #define NLA_PUT(skb, attrtype, attrlen, data) \ |
| 778 | do { \ |
| 779 | if (nla_put(skb, attrtype, attrlen, data) < 0) \ |
| 780 | goto nla_put_failure; \ |
| 781 | } while(0) |
| 782 | |
| 783 | #define NLA_PUT_TYPE(skb, type, attrtype, value) \ |
| 784 | do { \ |
| 785 | type __tmp = value; \ |
| 786 | NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \ |
| 787 | } while(0) |
| 788 | |
| 789 | #define NLA_PUT_U8(skb, attrtype, value) \ |
| 790 | NLA_PUT_TYPE(skb, u8, attrtype, value) |
| 791 | |
| 792 | #define NLA_PUT_U16(skb, attrtype, value) \ |
| 793 | NLA_PUT_TYPE(skb, u16, attrtype, value) |
| 794 | |
| 795 | #define NLA_PUT_U32(skb, attrtype, value) \ |
| 796 | NLA_PUT_TYPE(skb, u32, attrtype, value) |
| 797 | |
| 798 | #define NLA_PUT_U64(skb, attrtype, value) \ |
| 799 | NLA_PUT_TYPE(skb, u64, attrtype, value) |
| 800 | |
| 801 | #define NLA_PUT_STRING(skb, attrtype, value) \ |
| 802 | NLA_PUT(skb, attrtype, strlen(value) + 1, value) |
| 803 | |
| 804 | #define NLA_PUT_FLAG(skb, attrtype, value) \ |
| 805 | NLA_PUT(skb, attrtype, 0, NULL) |
| 806 | |
| 807 | #define NLA_PUT_MSECS(skb, attrtype, jiffies) \ |
| 808 | NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies)) |
| 809 | |
| 810 | /** |
| 811 | * nla_get_u32 - return payload of u32 attribute |
| 812 | * @nla: u32 netlink attribute |
| 813 | */ |
| 814 | static inline u32 nla_get_u32(struct nlattr *nla) |
| 815 | { |
| 816 | return *(u32 *) nla_data(nla); |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * nla_get_u16 - return payload of u16 attribute |
| 821 | * @nla: u16 netlink attribute |
| 822 | */ |
| 823 | static inline u16 nla_get_u16(struct nlattr *nla) |
| 824 | { |
| 825 | return *(u16 *) nla_data(nla); |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * nla_get_u8 - return payload of u8 attribute |
| 830 | * @nla: u8 netlink attribute |
| 831 | */ |
| 832 | static inline u8 nla_get_u8(struct nlattr *nla) |
| 833 | { |
| 834 | return *(u8 *) nla_data(nla); |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * nla_get_u64 - return payload of u64 attribute |
| 839 | * @nla: u64 netlink attribute |
| 840 | */ |
| 841 | static inline u64 nla_get_u64(struct nlattr *nla) |
| 842 | { |
| 843 | u64 tmp; |
| 844 | |
| 845 | nla_memcpy(&tmp, nla, sizeof(tmp)); |
| 846 | |
| 847 | return tmp; |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * nla_get_flag - return payload of flag attribute |
| 852 | * @nla: flag netlink attribute |
| 853 | */ |
| 854 | static inline int nla_get_flag(struct nlattr *nla) |
| 855 | { |
| 856 | return !!nla; |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * nla_get_msecs - return payload of msecs attribute |
| 861 | * @nla: msecs netlink attribute |
| 862 | * |
| 863 | * Returns the number of milliseconds in jiffies. |
| 864 | */ |
| 865 | static inline unsigned long nla_get_msecs(struct nlattr *nla) |
| 866 | { |
| 867 | u64 msecs = nla_get_u64(nla); |
| 868 | |
| 869 | return msecs_to_jiffies((unsigned long) msecs); |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * nla_nest_start - Start a new level of nested attributes |
| 874 | * @skb: socket buffer to add attributes to |
| 875 | * @attrtype: attribute type of container |
| 876 | * |
| 877 | * Returns the container attribute |
| 878 | */ |
| 879 | static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype) |
| 880 | { |
| 881 | struct nlattr *start = (struct nlattr *) skb->tail; |
| 882 | |
| 883 | if (nla_put(skb, attrtype, 0, NULL) < 0) |
| 884 | return NULL; |
| 885 | |
| 886 | return start; |
| 887 | } |
| 888 | |
| 889 | /** |
| 890 | * nla_nest_end - Finalize nesting of attributes |
| 891 | * @skb: socket buffer the attribtues are stored in |
| 892 | * @start: container attribute |
| 893 | * |
| 894 | * Corrects the container attribute header to include the all |
| 895 | * appeneded attributes. |
| 896 | * |
| 897 | * Returns the total data length of the skb. |
| 898 | */ |
| 899 | static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start) |
| 900 | { |
| 901 | start->nla_len = skb->tail - (unsigned char *) start; |
| 902 | return skb->len; |
| 903 | } |
| 904 | |
| 905 | /** |
| 906 | * nla_nest_cancel - Cancel nesting of attributes |
| 907 | * @skb: socket buffer the message is stored in |
| 908 | * @start: container attribute |
| 909 | * |
| 910 | * Removes the container attribute and including all nested |
| 911 | * attributes. Returns -1. |
| 912 | */ |
| 913 | static inline int nla_nest_cancel(struct sk_buff *skb, struct nlattr *start) |
| 914 | { |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 915 | return nlmsg_trim(skb, start); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | /** |
| 919 | * nla_for_each_attr - iterate over a stream of attributes |
| 920 | * @pos: loop counter, set to current attribute |
| 921 | * @head: head of attribute stream |
| 922 | * @len: length of attribute stream |
| 923 | * @rem: initialized to len, holds bytes currently remaining in stream |
| 924 | */ |
| 925 | #define nla_for_each_attr(pos, head, len, rem) \ |
| 926 | for (pos = head, rem = len; \ |
| 927 | nla_ok(pos, rem); \ |
| 928 | pos = nla_next(pos, &(rem))) |
| 929 | |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 930 | /** |
| 931 | * nla_for_each_nested - iterate over nested attributes |
| 932 | * @pos: loop counter, set to current attribute |
| 933 | * @nla: attribute containing the nested attributes |
| 934 | * @rem: initialized to len, holds bytes currently remaining in stream |
| 935 | */ |
| 936 | #define nla_for_each_nested(pos, nla, rem) \ |
| 937 | nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem) |
| 938 | |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 939 | #endif |