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