1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __NET_NETLINK_H 3 #define __NET_NETLINK_H 4 5 #include <linux/types.h> 6 #include <linux/netlink.h> 7 #include <linux/jiffies.h> 8 #include <linux/in6.h> 9 10 /* ======================================================================== 11 * Netlink Messages and Attributes Interface (As Seen On TV) 12 * ------------------------------------------------------------------------ 13 * Messages Interface 14 * ------------------------------------------------------------------------ 15 * 16 * Message Format: 17 * <--- nlmsg_total_size(payload) ---> 18 * <-- nlmsg_msg_size(payload) -> 19 * +----------+- - -+-------------+- - -+-------- - - 20 * | nlmsghdr | Pad | Payload | Pad | nlmsghdr 21 * +----------+- - -+-------------+- - -+-------- - - 22 * nlmsg_data(nlh)---^ ^ 23 * nlmsg_next(nlh)-----------------------+ 24 * 25 * Payload Format: 26 * <---------------------- nlmsg_len(nlh) ---------------------> 27 * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) -> 28 * +----------------------+- - -+--------------------------------+ 29 * | Family Header | Pad | Attributes | 30 * +----------------------+- - -+--------------------------------+ 31 * nlmsg_attrdata(nlh, hdrlen)---^ 32 * 33 * Data Structures: 34 * struct nlmsghdr netlink message header 35 * 36 * Message Construction: 37 * nlmsg_new() create a new netlink message 38 * nlmsg_put() add a netlink message to an skb 39 * nlmsg_put_answer() callback based nlmsg_put() 40 * nlmsg_end() finalize netlink message 41 * nlmsg_get_pos() return current position in message 42 * nlmsg_trim() trim part of message 43 * nlmsg_cancel() cancel message construction 44 * nlmsg_free() free a netlink message 45 * 46 * Message Sending: 47 * nlmsg_multicast() multicast message to several groups 48 * nlmsg_unicast() unicast a message to a single socket 49 * nlmsg_notify() send notification message 50 * 51 * Message Length Calculations: 52 * nlmsg_msg_size(payload) length of message w/o padding 53 * nlmsg_total_size(payload) length of message w/ padding 54 * nlmsg_padlen(payload) length of padding at tail 55 * 56 * Message Payload Access: 57 * nlmsg_data(nlh) head of message payload 58 * nlmsg_len(nlh) length of message payload 59 * nlmsg_attrdata(nlh, hdrlen) head of attributes data 60 * nlmsg_attrlen(nlh, hdrlen) length of attributes data 61 * 62 * Message Parsing: 63 * nlmsg_ok(nlh, remaining) does nlh fit into remaining bytes? 64 * nlmsg_next(nlh, remaining) get next netlink message 65 * nlmsg_parse() parse attributes of a message 66 * nlmsg_find_attr() find an attribute in a message 67 * nlmsg_for_each_msg() loop over all messages 68 * nlmsg_validate() validate netlink message incl. attrs 69 * nlmsg_for_each_attr() loop over all attributes 70 * 71 * Misc: 72 * nlmsg_report() report back to application? 73 * 74 * ------------------------------------------------------------------------ 75 * Attributes Interface 76 * ------------------------------------------------------------------------ 77 * 78 * Attribute Format: 79 * <------- nla_total_size(payload) -------> 80 * <---- nla_attr_size(payload) -----> 81 * +----------+- - -+- - - - - - - - - +- - -+-------- - - 82 * | Header | Pad | Payload | Pad | Header 83 * +----------+- - -+- - - - - - - - - +- - -+-------- - - 84 * <- nla_len(nla) -> ^ 85 * nla_data(nla)----^ | 86 * nla_next(nla)-----------------------------' 87 * 88 * Data Structures: 89 * struct nlattr netlink attribute header 90 * 91 * Attribute Construction: 92 * nla_reserve(skb, type, len) reserve room for an attribute 93 * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr 94 * nla_put(skb, type, len, data) add attribute to skb 95 * nla_put_nohdr(skb, len, data) add attribute w/o hdr 96 * nla_append(skb, len, data) append data to skb 97 * 98 * Attribute Construction for Basic Types: 99 * nla_put_u8(skb, type, value) add u8 attribute to skb 100 * nla_put_u16(skb, type, value) add u16 attribute to skb 101 * nla_put_u32(skb, type, value) add u32 attribute to skb 102 * nla_put_u64_64bit(skb, type, 103 * value, padattr) add u64 attribute to skb 104 * nla_put_s8(skb, type, value) add s8 attribute to skb 105 * nla_put_s16(skb, type, value) add s16 attribute to skb 106 * nla_put_s32(skb, type, value) add s32 attribute to skb 107 * nla_put_s64(skb, type, value, 108 * padattr) add s64 attribute to skb 109 * nla_put_string(skb, type, str) add string attribute to skb 110 * nla_put_flag(skb, type) add flag attribute to skb 111 * nla_put_msecs(skb, type, jiffies, 112 * padattr) add msecs attribute to skb 113 * nla_put_in_addr(skb, type, addr) add IPv4 address attribute to skb 114 * nla_put_in6_addr(skb, type, addr) add IPv6 address attribute to skb 115 * 116 * Nested Attributes Construction: 117 * nla_nest_start(skb, type) start a nested attribute 118 * nla_nest_end(skb, nla) finalize a nested attribute 119 * nla_nest_cancel(skb, nla) cancel nested attribute construction 120 * 121 * Attribute Length Calculations: 122 * nla_attr_size(payload) length of attribute w/o padding 123 * nla_total_size(payload) length of attribute w/ padding 124 * nla_padlen(payload) length of padding 125 * 126 * Attribute Payload Access: 127 * nla_data(nla) head of attribute payload 128 * nla_len(nla) length of attribute payload 129 * 130 * Attribute Payload Access for Basic Types: 131 * nla_get_u8(nla) get payload for a u8 attribute 132 * nla_get_u16(nla) get payload for a u16 attribute 133 * nla_get_u32(nla) get payload for a u32 attribute 134 * nla_get_u64(nla) get payload for a u64 attribute 135 * nla_get_s8(nla) get payload for a s8 attribute 136 * nla_get_s16(nla) get payload for a s16 attribute 137 * nla_get_s32(nla) get payload for a s32 attribute 138 * nla_get_s64(nla) get payload for a s64 attribute 139 * nla_get_flag(nla) return 1 if flag is true 140 * nla_get_msecs(nla) get payload for a msecs attribute 141 * 142 * Attribute Misc: 143 * nla_memcpy(dest, nla, count) copy attribute into memory 144 * nla_memcmp(nla, data, size) compare attribute with memory area 145 * nla_strlcpy(dst, nla, size) copy attribute to a sized string 146 * nla_strcmp(nla, str) compare attribute with string 147 * 148 * Attribute Parsing: 149 * nla_ok(nla, remaining) does nla fit into remaining bytes? 150 * nla_next(nla, remaining) get next netlink attribute 151 * nla_validate() validate a stream of attributes 152 * nla_validate_nested() validate a stream of nested attributes 153 * nla_find() find attribute in stream of attributes 154 * nla_find_nested() find attribute in nested attributes 155 * nla_parse() parse and validate stream of attrs 156 * nla_parse_nested() parse nested attribuets 157 * nla_for_each_attr() loop over all attributes 158 * nla_for_each_nested() loop over the nested attributes 159 *========================================================================= 160 */ 161 162 /** 163 * Standard attribute types to specify validation policy 164 */ 165 enum { 166 NLA_UNSPEC, 167 NLA_U8, 168 NLA_U16, 169 NLA_U32, 170 NLA_U64, 171 NLA_STRING, 172 NLA_FLAG, 173 NLA_MSECS, 174 NLA_NESTED, 175 NLA_NESTED_COMPAT, 176 NLA_NUL_STRING, 177 NLA_BINARY, 178 NLA_S8, 179 NLA_S16, 180 NLA_S32, 181 NLA_S64, 182 NLA_BITFIELD32, 183 NLA_REJECT, 184 NLA_EXACT_LEN, 185 NLA_EXACT_LEN_WARN, 186 __NLA_TYPE_MAX, 187 }; 188 189 #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1) 190 191 /** 192 * struct nla_policy - attribute validation policy 193 * @type: Type of attribute or NLA_UNSPEC 194 * @len: Type specific length of payload 195 * 196 * Policies are defined as arrays of this struct, the array must be 197 * accessible by attribute type up to the highest identifier to be expected. 198 * 199 * Meaning of `len' field: 200 * NLA_STRING Maximum length of string 201 * NLA_NUL_STRING Maximum length of string (excluding NUL) 202 * NLA_FLAG Unused 203 * NLA_BINARY Maximum length of attribute payload 204 * NLA_NESTED Don't use `len' field -- length verification is 205 * done by checking len of nested header (or empty) 206 * NLA_NESTED_COMPAT Minimum length of structure payload 207 * NLA_U8, NLA_U16, 208 * NLA_U32, NLA_U64, 209 * NLA_S8, NLA_S16, 210 * NLA_S32, NLA_S64, 211 * NLA_MSECS Leaving the length field zero will verify the 212 * given type fits, using it verifies minimum length 213 * just like "All other" 214 * NLA_BITFIELD32 Unused 215 * NLA_REJECT Unused 216 * NLA_EXACT_LEN Attribute must have exactly this length, otherwise 217 * it is rejected. 218 * NLA_EXACT_LEN_WARN Attribute should have exactly this length, a warning 219 * is logged if it is longer, shorter is rejected. 220 * All other Minimum length of attribute payload 221 * 222 * Meaning of `validation_data' field: 223 * NLA_BITFIELD32 This is a 32-bit bitmap/bitselector attribute and 224 * validation data must point to a u32 value of valid 225 * flags 226 * NLA_REJECT This attribute is always rejected and validation data 227 * may point to a string to report as the error instead 228 * of the generic one in extended ACK. 229 * All other Unused 230 * 231 * Example: 232 * static const struct nla_policy my_policy[ATTR_MAX+1] = { 233 * [ATTR_FOO] = { .type = NLA_U16 }, 234 * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ }, 235 * [ATTR_BAZ] = { .len = sizeof(struct mystruct) }, 236 * [ATTR_GOO] = { .type = NLA_BITFIELD32, .validation_data = &myvalidflags }, 237 * }; 238 */ 239 struct nla_policy { 240 u16 type; 241 u16 len; 242 void *validation_data; 243 }; 244 245 #define NLA_POLICY_EXACT_LEN(_len) { .type = NLA_EXACT_LEN, .len = _len } 246 #define NLA_POLICY_EXACT_LEN_WARN(_len) { .type = NLA_EXACT_LEN_WARN, \ 247 .len = _len } 248 249 #define NLA_POLICY_ETH_ADDR NLA_POLICY_EXACT_LEN(ETH_ALEN) 250 #define NLA_POLICY_ETH_ADDR_COMPAT NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN) 251 252 /** 253 * struct nl_info - netlink source information 254 * @nlh: Netlink message header of original request 255 * @portid: Netlink PORTID of requesting application 256 */ 257 struct nl_info { 258 struct nlmsghdr *nlh; 259 struct net *nl_net; 260 u32 portid; 261 bool skip_notify; 262 }; 263 264 int netlink_rcv_skb(struct sk_buff *skb, 265 int (*cb)(struct sk_buff *, struct nlmsghdr *, 266 struct netlink_ext_ack *)); 267 int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid, 268 unsigned int group, int report, gfp_t flags); 269 270 int nla_validate(const struct nlattr *head, int len, int maxtype, 271 const struct nla_policy *policy, 272 struct netlink_ext_ack *extack); 273 int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, 274 int len, const struct nla_policy *policy, 275 struct netlink_ext_ack *extack); 276 int nla_policy_len(const struct nla_policy *, int); 277 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype); 278 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize); 279 char *nla_strdup(const struct nlattr *nla, gfp_t flags); 280 int nla_memcpy(void *dest, const struct nlattr *src, int count); 281 int nla_memcmp(const struct nlattr *nla, const void *data, size_t size); 282 int nla_strcmp(const struct nlattr *nla, const char *str); 283 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 284 struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype, 285 int attrlen, int padattr); 286 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 287 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 288 struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, 289 int attrlen, int padattr); 290 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 291 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, 292 const void *data); 293 void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 294 const void *data, int padattr); 295 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 296 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data); 297 int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 298 const void *data, int padattr); 299 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 300 int nla_append(struct sk_buff *skb, int attrlen, const void *data); 301 302 /************************************************************************** 303 * Netlink Messages 304 **************************************************************************/ 305 306 /** 307 * nlmsg_msg_size - length of netlink message not including padding 308 * @payload: length of message payload 309 */ 310 static inline int nlmsg_msg_size(int payload) 311 { 312 return NLMSG_HDRLEN + payload; 313 } 314 315 /** 316 * nlmsg_total_size - length of netlink message including padding 317 * @payload: length of message payload 318 */ 319 static inline int nlmsg_total_size(int payload) 320 { 321 return NLMSG_ALIGN(nlmsg_msg_size(payload)); 322 } 323 324 /** 325 * nlmsg_padlen - length of padding at the message's tail 326 * @payload: length of message payload 327 */ 328 static inline int nlmsg_padlen(int payload) 329 { 330 return nlmsg_total_size(payload) - nlmsg_msg_size(payload); 331 } 332 333 /** 334 * nlmsg_data - head of message payload 335 * @nlh: netlink message header 336 */ 337 static inline void *nlmsg_data(const struct nlmsghdr *nlh) 338 { 339 return (unsigned char *) nlh + NLMSG_HDRLEN; 340 } 341 342 /** 343 * nlmsg_len - length of message payload 344 * @nlh: netlink message header 345 */ 346 static inline int nlmsg_len(const struct nlmsghdr *nlh) 347 { 348 return nlh->nlmsg_len - NLMSG_HDRLEN; 349 } 350 351 /** 352 * nlmsg_attrdata - head of attributes data 353 * @nlh: netlink message header 354 * @hdrlen: length of family specific header 355 */ 356 static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, 357 int hdrlen) 358 { 359 unsigned char *data = nlmsg_data(nlh); 360 return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen)); 361 } 362 363 /** 364 * nlmsg_attrlen - length of attributes data 365 * @nlh: netlink message header 366 * @hdrlen: length of family specific header 367 */ 368 static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen) 369 { 370 return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen); 371 } 372 373 /** 374 * nlmsg_ok - check if the netlink message fits into the remaining bytes 375 * @nlh: netlink message header 376 * @remaining: number of bytes remaining in message stream 377 */ 378 static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining) 379 { 380 return (remaining >= (int) sizeof(struct nlmsghdr) && 381 nlh->nlmsg_len >= sizeof(struct nlmsghdr) && 382 nlh->nlmsg_len <= remaining); 383 } 384 385 /** 386 * nlmsg_next - next netlink message in message stream 387 * @nlh: netlink message header 388 * @remaining: number of bytes remaining in message stream 389 * 390 * Returns the next netlink message in the message stream and 391 * decrements remaining by the size of the current message. 392 */ 393 static inline struct nlmsghdr * 394 nlmsg_next(const struct nlmsghdr *nlh, int *remaining) 395 { 396 int totlen = NLMSG_ALIGN(nlh->nlmsg_len); 397 398 *remaining -= totlen; 399 400 return (struct nlmsghdr *) ((unsigned char *) nlh + totlen); 401 } 402 403 /** 404 * nlmsg_parse - parse attributes of a netlink message 405 * @nlh: netlink message header 406 * @hdrlen: length of family specific header 407 * @tb: destination array with maxtype+1 elements 408 * @maxtype: maximum attribute type to be expected 409 * @policy: validation policy 410 * @extack: extended ACK report struct 411 * 412 * See nla_parse() 413 */ 414 static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen, 415 struct nlattr *tb[], int maxtype, 416 const struct nla_policy *policy, 417 struct netlink_ext_ack *extack) 418 { 419 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) 420 return -EINVAL; 421 422 return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), 423 nlmsg_attrlen(nlh, hdrlen), policy, extack); 424 } 425 426 /** 427 * nlmsg_find_attr - find a specific attribute in a netlink message 428 * @nlh: netlink message header 429 * @hdrlen: length of familiy specific header 430 * @attrtype: type of attribute to look for 431 * 432 * Returns the first attribute which matches the specified type. 433 */ 434 static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh, 435 int hdrlen, int attrtype) 436 { 437 return nla_find(nlmsg_attrdata(nlh, hdrlen), 438 nlmsg_attrlen(nlh, hdrlen), attrtype); 439 } 440 441 /** 442 * nlmsg_validate - validate a netlink message including attributes 443 * @nlh: netlinket message header 444 * @hdrlen: length of familiy specific header 445 * @maxtype: maximum attribute type to be expected 446 * @policy: validation policy 447 * @extack: extended ACK report struct 448 */ 449 static inline int nlmsg_validate(const struct nlmsghdr *nlh, 450 int hdrlen, int maxtype, 451 const struct nla_policy *policy, 452 struct netlink_ext_ack *extack) 453 { 454 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) 455 return -EINVAL; 456 457 return nla_validate(nlmsg_attrdata(nlh, hdrlen), 458 nlmsg_attrlen(nlh, hdrlen), maxtype, policy, 459 extack); 460 } 461 462 /** 463 * nlmsg_report - need to report back to application? 464 * @nlh: netlink message header 465 * 466 * Returns 1 if a report back to the application is requested. 467 */ 468 static inline int nlmsg_report(const struct nlmsghdr *nlh) 469 { 470 return !!(nlh->nlmsg_flags & NLM_F_ECHO); 471 } 472 473 /** 474 * nlmsg_for_each_attr - iterate over a stream of attributes 475 * @pos: loop counter, set to current attribute 476 * @nlh: netlink message header 477 * @hdrlen: length of familiy specific header 478 * @rem: initialized to len, holds bytes currently remaining in stream 479 */ 480 #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \ 481 nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \ 482 nlmsg_attrlen(nlh, hdrlen), rem) 483 484 /** 485 * nlmsg_put - Add a new netlink message to an skb 486 * @skb: socket buffer to store message in 487 * @portid: netlink PORTID of requesting application 488 * @seq: sequence number of message 489 * @type: message type 490 * @payload: length of message payload 491 * @flags: message flags 492 * 493 * Returns NULL if the tailroom of the skb is insufficient to store 494 * the message header and payload. 495 */ 496 static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, 497 int type, int payload, int flags) 498 { 499 if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload))) 500 return NULL; 501 502 return __nlmsg_put(skb, portid, seq, type, payload, flags); 503 } 504 505 /** 506 * nlmsg_put_answer - Add a new callback based netlink message to an skb 507 * @skb: socket buffer to store message in 508 * @cb: netlink callback 509 * @type: message type 510 * @payload: length of message payload 511 * @flags: message flags 512 * 513 * Returns NULL if the tailroom of the skb is insufficient to store 514 * the message header and payload. 515 */ 516 static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb, 517 struct netlink_callback *cb, 518 int type, int payload, 519 int flags) 520 { 521 return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 522 type, payload, flags); 523 } 524 525 /** 526 * nlmsg_new - Allocate a new netlink message 527 * @payload: size of the message payload 528 * @flags: the type of memory to allocate. 529 * 530 * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known 531 * and a good default is needed. 532 */ 533 static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags) 534 { 535 return alloc_skb(nlmsg_total_size(payload), flags); 536 } 537 538 /** 539 * nlmsg_end - Finalize a netlink message 540 * @skb: socket buffer the message is stored in 541 * @nlh: netlink message header 542 * 543 * Corrects the netlink message header to include the appeneded 544 * attributes. Only necessary if attributes have been added to 545 * the message. 546 */ 547 static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh) 548 { 549 nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh; 550 } 551 552 /** 553 * nlmsg_get_pos - return current position in netlink message 554 * @skb: socket buffer the message is stored in 555 * 556 * Returns a pointer to the current tail of the message. 557 */ 558 static inline void *nlmsg_get_pos(struct sk_buff *skb) 559 { 560 return skb_tail_pointer(skb); 561 } 562 563 /** 564 * nlmsg_trim - Trim message to a mark 565 * @skb: socket buffer the message is stored in 566 * @mark: mark to trim to 567 * 568 * Trims the message to the provided mark. 569 */ 570 static inline void nlmsg_trim(struct sk_buff *skb, const void *mark) 571 { 572 if (mark) { 573 WARN_ON((unsigned char *) mark < skb->data); 574 skb_trim(skb, (unsigned char *) mark - skb->data); 575 } 576 } 577 578 /** 579 * nlmsg_cancel - Cancel construction of a netlink message 580 * @skb: socket buffer the message is stored in 581 * @nlh: netlink message header 582 * 583 * Removes the complete netlink message including all 584 * attributes from the socket buffer again. 585 */ 586 static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh) 587 { 588 nlmsg_trim(skb, nlh); 589 } 590 591 /** 592 * nlmsg_free - free a netlink message 593 * @skb: socket buffer of netlink message 594 */ 595 static inline void nlmsg_free(struct sk_buff *skb) 596 { 597 kfree_skb(skb); 598 } 599 600 /** 601 * nlmsg_multicast - multicast a netlink message 602 * @sk: netlink socket to spread messages to 603 * @skb: netlink message as socket buffer 604 * @portid: own netlink portid to avoid sending to yourself 605 * @group: multicast group id 606 * @flags: allocation flags 607 */ 608 static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, 609 u32 portid, unsigned int group, gfp_t flags) 610 { 611 int err; 612 613 NETLINK_CB(skb).dst_group = group; 614 615 err = netlink_broadcast(sk, skb, portid, group, flags); 616 if (err > 0) 617 err = 0; 618 619 return err; 620 } 621 622 /** 623 * nlmsg_unicast - unicast a netlink message 624 * @sk: netlink socket to spread message to 625 * @skb: netlink message as socket buffer 626 * @portid: netlink portid of the destination socket 627 */ 628 static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid) 629 { 630 int err; 631 632 err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT); 633 if (err > 0) 634 err = 0; 635 636 return err; 637 } 638 639 /** 640 * nlmsg_for_each_msg - iterate over a stream of messages 641 * @pos: loop counter, set to current message 642 * @head: head of message stream 643 * @len: length of message stream 644 * @rem: initialized to len, holds bytes currently remaining in stream 645 */ 646 #define nlmsg_for_each_msg(pos, head, len, rem) \ 647 for (pos = head, rem = len; \ 648 nlmsg_ok(pos, rem); \ 649 pos = nlmsg_next(pos, &(rem))) 650 651 /** 652 * nl_dump_check_consistent - check if sequence is consistent and advertise if not 653 * @cb: netlink callback structure that stores the sequence number 654 * @nlh: netlink message header to write the flag to 655 * 656 * This function checks if the sequence (generation) number changed during dump 657 * and if it did, advertises it in the netlink message header. 658 * 659 * The correct way to use it is to set cb->seq to the generation counter when 660 * all locks for dumping have been acquired, and then call this function for 661 * each message that is generated. 662 * 663 * Note that due to initialisation concerns, 0 is an invalid sequence number 664 * and must not be used by code that uses this functionality. 665 */ 666 static inline void 667 nl_dump_check_consistent(struct netlink_callback *cb, 668 struct nlmsghdr *nlh) 669 { 670 if (cb->prev_seq && cb->seq != cb->prev_seq) 671 nlh->nlmsg_flags |= NLM_F_DUMP_INTR; 672 cb->prev_seq = cb->seq; 673 } 674 675 /************************************************************************** 676 * Netlink Attributes 677 **************************************************************************/ 678 679 /** 680 * nla_attr_size - length of attribute not including padding 681 * @payload: length of payload 682 */ 683 static inline int nla_attr_size(int payload) 684 { 685 return NLA_HDRLEN + payload; 686 } 687 688 /** 689 * nla_total_size - total length of attribute including padding 690 * @payload: length of payload 691 */ 692 static inline int nla_total_size(int payload) 693 { 694 return NLA_ALIGN(nla_attr_size(payload)); 695 } 696 697 /** 698 * nla_padlen - length of padding at the tail of attribute 699 * @payload: length of payload 700 */ 701 static inline int nla_padlen(int payload) 702 { 703 return nla_total_size(payload) - nla_attr_size(payload); 704 } 705 706 /** 707 * nla_type - attribute type 708 * @nla: netlink attribute 709 */ 710 static inline int nla_type(const struct nlattr *nla) 711 { 712 return nla->nla_type & NLA_TYPE_MASK; 713 } 714 715 /** 716 * nla_data - head of payload 717 * @nla: netlink attribute 718 */ 719 static inline void *nla_data(const struct nlattr *nla) 720 { 721 return (char *) nla + NLA_HDRLEN; 722 } 723 724 /** 725 * nla_len - length of payload 726 * @nla: netlink attribute 727 */ 728 static inline int nla_len(const struct nlattr *nla) 729 { 730 return nla->nla_len - NLA_HDRLEN; 731 } 732 733 /** 734 * nla_ok - check if the netlink attribute fits into the remaining bytes 735 * @nla: netlink attribute 736 * @remaining: number of bytes remaining in attribute stream 737 */ 738 static inline int nla_ok(const struct nlattr *nla, int remaining) 739 { 740 return remaining >= (int) sizeof(*nla) && 741 nla->nla_len >= sizeof(*nla) && 742 nla->nla_len <= remaining; 743 } 744 745 /** 746 * nla_next - next netlink attribute in attribute stream 747 * @nla: netlink attribute 748 * @remaining: number of bytes remaining in attribute stream 749 * 750 * Returns the next netlink attribute in the attribute stream and 751 * decrements remaining by the size of the current attribute. 752 */ 753 static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining) 754 { 755 unsigned int totlen = NLA_ALIGN(nla->nla_len); 756 757 *remaining -= totlen; 758 return (struct nlattr *) ((char *) nla + totlen); 759 } 760 761 /** 762 * nla_find_nested - find attribute in a set of nested attributes 763 * @nla: attribute containing the nested attributes 764 * @attrtype: type of attribute to look for 765 * 766 * Returns the first attribute which matches the specified type. 767 */ 768 static inline struct nlattr * 769 nla_find_nested(const struct nlattr *nla, int attrtype) 770 { 771 return nla_find(nla_data(nla), nla_len(nla), attrtype); 772 } 773 774 /** 775 * nla_parse_nested - parse nested attributes 776 * @tb: destination array with maxtype+1 elements 777 * @maxtype: maximum attribute type to be expected 778 * @nla: attribute containing the nested attributes 779 * @policy: validation policy 780 * @extack: extended ACK report struct 781 * 782 * See nla_parse() 783 */ 784 static inline int nla_parse_nested(struct nlattr *tb[], int maxtype, 785 const struct nlattr *nla, 786 const struct nla_policy *policy, 787 struct netlink_ext_ack *extack) 788 { 789 return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy, 790 extack); 791 } 792 793 /** 794 * nla_put_u8 - Add a u8 netlink attribute to a socket buffer 795 * @skb: socket buffer to add attribute to 796 * @attrtype: attribute type 797 * @value: numeric value 798 */ 799 static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value) 800 { 801 /* temporary variables to work around GCC PR81715 with asan-stack=1 */ 802 u8 tmp = value; 803 804 return nla_put(skb, attrtype, sizeof(u8), &tmp); 805 } 806 807 /** 808 * nla_put_u16 - Add a u16 netlink attribute to a socket buffer 809 * @skb: socket buffer to add attribute to 810 * @attrtype: attribute type 811 * @value: numeric value 812 */ 813 static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value) 814 { 815 u16 tmp = value; 816 817 return nla_put(skb, attrtype, sizeof(u16), &tmp); 818 } 819 820 /** 821 * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer 822 * @skb: socket buffer to add attribute to 823 * @attrtype: attribute type 824 * @value: numeric value 825 */ 826 static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value) 827 { 828 __be16 tmp = value; 829 830 return nla_put(skb, attrtype, sizeof(__be16), &tmp); 831 } 832 833 /** 834 * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer 835 * @skb: socket buffer to add attribute to 836 * @attrtype: attribute type 837 * @value: numeric value 838 */ 839 static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value) 840 { 841 __be16 tmp = value; 842 843 return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp); 844 } 845 846 /** 847 * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer 848 * @skb: socket buffer to add attribute to 849 * @attrtype: attribute type 850 * @value: numeric value 851 */ 852 static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value) 853 { 854 __le16 tmp = value; 855 856 return nla_put(skb, attrtype, sizeof(__le16), &tmp); 857 } 858 859 /** 860 * nla_put_u32 - Add a u32 netlink attribute to a socket buffer 861 * @skb: socket buffer to add attribute to 862 * @attrtype: attribute type 863 * @value: numeric value 864 */ 865 static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value) 866 { 867 u32 tmp = value; 868 869 return nla_put(skb, attrtype, sizeof(u32), &tmp); 870 } 871 872 /** 873 * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer 874 * @skb: socket buffer to add attribute to 875 * @attrtype: attribute type 876 * @value: numeric value 877 */ 878 static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value) 879 { 880 __be32 tmp = value; 881 882 return nla_put(skb, attrtype, sizeof(__be32), &tmp); 883 } 884 885 /** 886 * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer 887 * @skb: socket buffer to add attribute to 888 * @attrtype: attribute type 889 * @value: numeric value 890 */ 891 static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value) 892 { 893 __be32 tmp = value; 894 895 return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp); 896 } 897 898 /** 899 * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer 900 * @skb: socket buffer to add attribute to 901 * @attrtype: attribute type 902 * @value: numeric value 903 */ 904 static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value) 905 { 906 __le32 tmp = value; 907 908 return nla_put(skb, attrtype, sizeof(__le32), &tmp); 909 } 910 911 /** 912 * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it 913 * @skb: socket buffer to add attribute to 914 * @attrtype: attribute type 915 * @value: numeric value 916 * @padattr: attribute type for the padding 917 */ 918 static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype, 919 u64 value, int padattr) 920 { 921 u64 tmp = value; 922 923 return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr); 924 } 925 926 /** 927 * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it 928 * @skb: socket buffer to add attribute to 929 * @attrtype: attribute type 930 * @value: numeric value 931 * @padattr: attribute type for the padding 932 */ 933 static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value, 934 int padattr) 935 { 936 __be64 tmp = value; 937 938 return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr); 939 } 940 941 /** 942 * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it 943 * @skb: socket buffer to add attribute to 944 * @attrtype: attribute type 945 * @value: numeric value 946 * @padattr: attribute type for the padding 947 */ 948 static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value, 949 int padattr) 950 { 951 __be64 tmp = value; 952 953 return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp, 954 padattr); 955 } 956 957 /** 958 * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it 959 * @skb: socket buffer to add attribute to 960 * @attrtype: attribute type 961 * @value: numeric value 962 * @padattr: attribute type for the padding 963 */ 964 static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value, 965 int padattr) 966 { 967 __le64 tmp = value; 968 969 return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr); 970 } 971 972 /** 973 * nla_put_s8 - Add a s8 netlink attribute to a socket buffer 974 * @skb: socket buffer to add attribute to 975 * @attrtype: attribute type 976 * @value: numeric value 977 */ 978 static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value) 979 { 980 s8 tmp = value; 981 982 return nla_put(skb, attrtype, sizeof(s8), &tmp); 983 } 984 985 /** 986 * nla_put_s16 - Add a s16 netlink attribute to a socket buffer 987 * @skb: socket buffer to add attribute to 988 * @attrtype: attribute type 989 * @value: numeric value 990 */ 991 static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value) 992 { 993 s16 tmp = value; 994 995 return nla_put(skb, attrtype, sizeof(s16), &tmp); 996 } 997 998 /** 999 * nla_put_s32 - Add a s32 netlink attribute to a socket buffer 1000 * @skb: socket buffer to add attribute to 1001 * @attrtype: attribute type 1002 * @value: numeric value 1003 */ 1004 static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value) 1005 { 1006 s32 tmp = value; 1007 1008 return nla_put(skb, attrtype, sizeof(s32), &tmp); 1009 } 1010 1011 /** 1012 * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it 1013 * @skb: socket buffer to add attribute to 1014 * @attrtype: attribute type 1015 * @value: numeric value 1016 * @padattr: attribute type for the padding 1017 */ 1018 static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value, 1019 int padattr) 1020 { 1021 s64 tmp = value; 1022 1023 return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr); 1024 } 1025 1026 /** 1027 * nla_put_string - Add a string netlink attribute to a socket buffer 1028 * @skb: socket buffer to add attribute to 1029 * @attrtype: attribute type 1030 * @str: NUL terminated string 1031 */ 1032 static inline int nla_put_string(struct sk_buff *skb, int attrtype, 1033 const char *str) 1034 { 1035 return nla_put(skb, attrtype, strlen(str) + 1, str); 1036 } 1037 1038 /** 1039 * nla_put_flag - Add a flag netlink attribute to a socket buffer 1040 * @skb: socket buffer to add attribute to 1041 * @attrtype: attribute type 1042 */ 1043 static inline int nla_put_flag(struct sk_buff *skb, int attrtype) 1044 { 1045 return nla_put(skb, attrtype, 0, NULL); 1046 } 1047 1048 /** 1049 * nla_put_msecs - Add a msecs netlink attribute to a skb and align it 1050 * @skb: socket buffer to add attribute to 1051 * @attrtype: attribute type 1052 * @njiffies: number of jiffies to convert to msecs 1053 * @padattr: attribute type for the padding 1054 */ 1055 static inline int nla_put_msecs(struct sk_buff *skb, int attrtype, 1056 unsigned long njiffies, int padattr) 1057 { 1058 u64 tmp = jiffies_to_msecs(njiffies); 1059 1060 return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr); 1061 } 1062 1063 /** 1064 * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket 1065 * buffer 1066 * @skb: socket buffer to add attribute to 1067 * @attrtype: attribute type 1068 * @addr: IPv4 address 1069 */ 1070 static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype, 1071 __be32 addr) 1072 { 1073 __be32 tmp = addr; 1074 1075 return nla_put_be32(skb, attrtype, tmp); 1076 } 1077 1078 /** 1079 * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket 1080 * buffer 1081 * @skb: socket buffer to add attribute to 1082 * @attrtype: attribute type 1083 * @addr: IPv6 address 1084 */ 1085 static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype, 1086 const struct in6_addr *addr) 1087 { 1088 return nla_put(skb, attrtype, sizeof(*addr), addr); 1089 } 1090 1091 /** 1092 * nla_get_u32 - return payload of u32 attribute 1093 * @nla: u32 netlink attribute 1094 */ 1095 static inline u32 nla_get_u32(const struct nlattr *nla) 1096 { 1097 return *(u32 *) nla_data(nla); 1098 } 1099 1100 /** 1101 * nla_get_be32 - return payload of __be32 attribute 1102 * @nla: __be32 netlink attribute 1103 */ 1104 static inline __be32 nla_get_be32(const struct nlattr *nla) 1105 { 1106 return *(__be32 *) nla_data(nla); 1107 } 1108 1109 /** 1110 * nla_get_le32 - return payload of __le32 attribute 1111 * @nla: __le32 netlink attribute 1112 */ 1113 static inline __le32 nla_get_le32(const struct nlattr *nla) 1114 { 1115 return *(__le32 *) nla_data(nla); 1116 } 1117 1118 /** 1119 * nla_get_u16 - return payload of u16 attribute 1120 * @nla: u16 netlink attribute 1121 */ 1122 static inline u16 nla_get_u16(const struct nlattr *nla) 1123 { 1124 return *(u16 *) nla_data(nla); 1125 } 1126 1127 /** 1128 * nla_get_be16 - return payload of __be16 attribute 1129 * @nla: __be16 netlink attribute 1130 */ 1131 static inline __be16 nla_get_be16(const struct nlattr *nla) 1132 { 1133 return *(__be16 *) nla_data(nla); 1134 } 1135 1136 /** 1137 * nla_get_le16 - return payload of __le16 attribute 1138 * @nla: __le16 netlink attribute 1139 */ 1140 static inline __le16 nla_get_le16(const struct nlattr *nla) 1141 { 1142 return *(__le16 *) nla_data(nla); 1143 } 1144 1145 /** 1146 * nla_get_u8 - return payload of u8 attribute 1147 * @nla: u8 netlink attribute 1148 */ 1149 static inline u8 nla_get_u8(const struct nlattr *nla) 1150 { 1151 return *(u8 *) nla_data(nla); 1152 } 1153 1154 /** 1155 * nla_get_u64 - return payload of u64 attribute 1156 * @nla: u64 netlink attribute 1157 */ 1158 static inline u64 nla_get_u64(const struct nlattr *nla) 1159 { 1160 u64 tmp; 1161 1162 nla_memcpy(&tmp, nla, sizeof(tmp)); 1163 1164 return tmp; 1165 } 1166 1167 /** 1168 * nla_get_be64 - return payload of __be64 attribute 1169 * @nla: __be64 netlink attribute 1170 */ 1171 static inline __be64 nla_get_be64(const struct nlattr *nla) 1172 { 1173 __be64 tmp; 1174 1175 nla_memcpy(&tmp, nla, sizeof(tmp)); 1176 1177 return tmp; 1178 } 1179 1180 /** 1181 * nla_get_le64 - return payload of __le64 attribute 1182 * @nla: __le64 netlink attribute 1183 */ 1184 static inline __le64 nla_get_le64(const struct nlattr *nla) 1185 { 1186 return *(__le64 *) nla_data(nla); 1187 } 1188 1189 /** 1190 * nla_get_s32 - return payload of s32 attribute 1191 * @nla: s32 netlink attribute 1192 */ 1193 static inline s32 nla_get_s32(const struct nlattr *nla) 1194 { 1195 return *(s32 *) nla_data(nla); 1196 } 1197 1198 /** 1199 * nla_get_s16 - return payload of s16 attribute 1200 * @nla: s16 netlink attribute 1201 */ 1202 static inline s16 nla_get_s16(const struct nlattr *nla) 1203 { 1204 return *(s16 *) nla_data(nla); 1205 } 1206 1207 /** 1208 * nla_get_s8 - return payload of s8 attribute 1209 * @nla: s8 netlink attribute 1210 */ 1211 static inline s8 nla_get_s8(const struct nlattr *nla) 1212 { 1213 return *(s8 *) nla_data(nla); 1214 } 1215 1216 /** 1217 * nla_get_s64 - return payload of s64 attribute 1218 * @nla: s64 netlink attribute 1219 */ 1220 static inline s64 nla_get_s64(const struct nlattr *nla) 1221 { 1222 s64 tmp; 1223 1224 nla_memcpy(&tmp, nla, sizeof(tmp)); 1225 1226 return tmp; 1227 } 1228 1229 /** 1230 * nla_get_flag - return payload of flag attribute 1231 * @nla: flag netlink attribute 1232 */ 1233 static inline int nla_get_flag(const struct nlattr *nla) 1234 { 1235 return !!nla; 1236 } 1237 1238 /** 1239 * nla_get_msecs - return payload of msecs attribute 1240 * @nla: msecs netlink attribute 1241 * 1242 * Returns the number of milliseconds in jiffies. 1243 */ 1244 static inline unsigned long nla_get_msecs(const struct nlattr *nla) 1245 { 1246 u64 msecs = nla_get_u64(nla); 1247 1248 return msecs_to_jiffies((unsigned long) msecs); 1249 } 1250 1251 /** 1252 * nla_get_in_addr - return payload of IPv4 address attribute 1253 * @nla: IPv4 address netlink attribute 1254 */ 1255 static inline __be32 nla_get_in_addr(const struct nlattr *nla) 1256 { 1257 return *(__be32 *) nla_data(nla); 1258 } 1259 1260 /** 1261 * nla_get_in6_addr - return payload of IPv6 address attribute 1262 * @nla: IPv6 address netlink attribute 1263 */ 1264 static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla) 1265 { 1266 struct in6_addr tmp; 1267 1268 nla_memcpy(&tmp, nla, sizeof(tmp)); 1269 return tmp; 1270 } 1271 1272 /** 1273 * nla_get_bitfield32 - return payload of 32 bitfield attribute 1274 * @nla: nla_bitfield32 attribute 1275 */ 1276 static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla) 1277 { 1278 struct nla_bitfield32 tmp; 1279 1280 nla_memcpy(&tmp, nla, sizeof(tmp)); 1281 return tmp; 1282 } 1283 1284 /** 1285 * nla_memdup - duplicate attribute memory (kmemdup) 1286 * @src: netlink attribute to duplicate from 1287 * @gfp: GFP mask 1288 */ 1289 static inline void *nla_memdup(const struct nlattr *src, gfp_t gfp) 1290 { 1291 return kmemdup(nla_data(src), nla_len(src), gfp); 1292 } 1293 1294 /** 1295 * nla_nest_start - Start a new level of nested attributes 1296 * @skb: socket buffer to add attributes to 1297 * @attrtype: attribute type of container 1298 * 1299 * Returns the container attribute 1300 */ 1301 static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype) 1302 { 1303 struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb); 1304 1305 if (nla_put(skb, attrtype, 0, NULL) < 0) 1306 return NULL; 1307 1308 return start; 1309 } 1310 1311 /** 1312 * nla_nest_end - Finalize nesting of attributes 1313 * @skb: socket buffer the attributes are stored in 1314 * @start: container attribute 1315 * 1316 * Corrects the container attribute header to include the all 1317 * appeneded attributes. 1318 * 1319 * Returns the total data length of the skb. 1320 */ 1321 static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start) 1322 { 1323 start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start; 1324 return skb->len; 1325 } 1326 1327 /** 1328 * nla_nest_cancel - Cancel nesting of attributes 1329 * @skb: socket buffer the message is stored in 1330 * @start: container attribute 1331 * 1332 * Removes the container attribute and including all nested 1333 * attributes. Returns -EMSGSIZE 1334 */ 1335 static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start) 1336 { 1337 nlmsg_trim(skb, start); 1338 } 1339 1340 /** 1341 * nla_validate_nested - Validate a stream of nested attributes 1342 * @start: container attribute 1343 * @maxtype: maximum attribute type to be expected 1344 * @policy: validation policy 1345 * @extack: extended ACK report struct 1346 * 1347 * Validates all attributes in the nested attribute stream against the 1348 * specified policy. Attributes with a type exceeding maxtype will be 1349 * ignored. See documenation of struct nla_policy for more details. 1350 * 1351 * Returns 0 on success or a negative error code. 1352 */ 1353 static inline int nla_validate_nested(const struct nlattr *start, int maxtype, 1354 const struct nla_policy *policy, 1355 struct netlink_ext_ack *extack) 1356 { 1357 return nla_validate(nla_data(start), nla_len(start), maxtype, policy, 1358 extack); 1359 } 1360 1361 /** 1362 * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute 1363 * @skb: socket buffer the message is stored in 1364 * 1365 * Return true if padding is needed to align the next attribute (nla_data()) to 1366 * a 64-bit aligned area. 1367 */ 1368 static inline bool nla_need_padding_for_64bit(struct sk_buff *skb) 1369 { 1370 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1371 /* The nlattr header is 4 bytes in size, that's why we test 1372 * if the skb->data _is_ aligned. A NOP attribute, plus 1373 * nlattr header for next attribute, will make nla_data() 1374 * 8-byte aligned. 1375 */ 1376 if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8)) 1377 return true; 1378 #endif 1379 return false; 1380 } 1381 1382 /** 1383 * nla_align_64bit - 64-bit align the nla_data() of next attribute 1384 * @skb: socket buffer the message is stored in 1385 * @padattr: attribute type for the padding 1386 * 1387 * Conditionally emit a padding netlink attribute in order to make 1388 * the next attribute we emit have a 64-bit aligned nla_data() area. 1389 * This will only be done in architectures which do not have 1390 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined. 1391 * 1392 * Returns zero on success or a negative error code. 1393 */ 1394 static inline int nla_align_64bit(struct sk_buff *skb, int padattr) 1395 { 1396 if (nla_need_padding_for_64bit(skb) && 1397 !nla_reserve(skb, padattr, 0)) 1398 return -EMSGSIZE; 1399 1400 return 0; 1401 } 1402 1403 /** 1404 * nla_total_size_64bit - total length of attribute including padding 1405 * @payload: length of payload 1406 */ 1407 static inline int nla_total_size_64bit(int payload) 1408 { 1409 return NLA_ALIGN(nla_attr_size(payload)) 1410 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1411 + NLA_ALIGN(nla_attr_size(0)) 1412 #endif 1413 ; 1414 } 1415 1416 /** 1417 * nla_for_each_attr - iterate over a stream of attributes 1418 * @pos: loop counter, set to current attribute 1419 * @head: head of attribute stream 1420 * @len: length of attribute stream 1421 * @rem: initialized to len, holds bytes currently remaining in stream 1422 */ 1423 #define nla_for_each_attr(pos, head, len, rem) \ 1424 for (pos = head, rem = len; \ 1425 nla_ok(pos, rem); \ 1426 pos = nla_next(pos, &(rem))) 1427 1428 /** 1429 * nla_for_each_nested - iterate over nested attributes 1430 * @pos: loop counter, set to current attribute 1431 * @nla: attribute containing the nested attributes 1432 * @rem: initialized to len, holds bytes currently remaining in stream 1433 */ 1434 #define nla_for_each_nested(pos, nla, rem) \ 1435 nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem) 1436 1437 /** 1438 * nla_is_last - Test if attribute is last in stream 1439 * @nla: attribute to test 1440 * @rem: bytes remaining in stream 1441 */ 1442 static inline bool nla_is_last(const struct nlattr *nla, int rem) 1443 { 1444 return nla->nla_len == rem; 1445 } 1446 1447 #endif 1448