1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * NETLINK Netlink attributes 4 * 5 * Authors: Thomas Graf <tgraf@suug.ch> 6 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 7 */ 8 9 #include <linux/export.h> 10 #include <linux/kernel.h> 11 #include <linux/errno.h> 12 #include <linux/jiffies.h> 13 #include <linux/skbuff.h> 14 #include <linux/string.h> 15 #include <linux/types.h> 16 #include <net/netlink.h> 17 18 /* For these data types, attribute length should be exactly the given 19 * size. However, to maintain compatibility with broken commands, if the 20 * attribute length does not match the expected size a warning is emitted 21 * to the user that the command is sending invalid data and needs to be fixed. 22 */ 23 static const u8 nla_attr_len[NLA_TYPE_MAX+1] = { 24 [NLA_U8] = sizeof(u8), 25 [NLA_U16] = sizeof(u16), 26 [NLA_U32] = sizeof(u32), 27 [NLA_U64] = sizeof(u64), 28 [NLA_S8] = sizeof(s8), 29 [NLA_S16] = sizeof(s16), 30 [NLA_S32] = sizeof(s32), 31 [NLA_S64] = sizeof(s64), 32 }; 33 34 static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = { 35 [NLA_U8] = sizeof(u8), 36 [NLA_U16] = sizeof(u16), 37 [NLA_U32] = sizeof(u32), 38 [NLA_U64] = sizeof(u64), 39 [NLA_MSECS] = sizeof(u64), 40 [NLA_NESTED] = NLA_HDRLEN, 41 [NLA_S8] = sizeof(s8), 42 [NLA_S16] = sizeof(s16), 43 [NLA_S32] = sizeof(s32), 44 [NLA_S64] = sizeof(s64), 45 }; 46 47 static int validate_nla_bitfield32(const struct nlattr *nla, 48 const u32 *valid_flags_mask) 49 { 50 const struct nla_bitfield32 *bf = nla_data(nla); 51 52 if (!valid_flags_mask) 53 return -EINVAL; 54 55 /*disallow invalid bit selector */ 56 if (bf->selector & ~*valid_flags_mask) 57 return -EINVAL; 58 59 /*disallow invalid bit values */ 60 if (bf->value & ~*valid_flags_mask) 61 return -EINVAL; 62 63 /*disallow valid bit values that are not selected*/ 64 if (bf->value & ~bf->selector) 65 return -EINVAL; 66 67 return 0; 68 } 69 70 static int nla_validate_array(const struct nlattr *head, int len, int maxtype, 71 const struct nla_policy *policy, 72 struct netlink_ext_ack *extack, 73 unsigned int validate) 74 { 75 const struct nlattr *entry; 76 int rem; 77 78 nla_for_each_attr(entry, head, len, rem) { 79 int ret; 80 81 if (nla_len(entry) == 0) 82 continue; 83 84 if (nla_len(entry) < NLA_HDRLEN) { 85 NL_SET_ERR_MSG_ATTR(extack, entry, 86 "Array element too short"); 87 return -ERANGE; 88 } 89 90 ret = __nla_validate(nla_data(entry), nla_len(entry), 91 maxtype, policy, validate, extack); 92 if (ret < 0) 93 return ret; 94 } 95 96 return 0; 97 } 98 99 static int nla_validate_int_range(const struct nla_policy *pt, 100 const struct nlattr *nla, 101 struct netlink_ext_ack *extack) 102 { 103 bool validate_min, validate_max; 104 s64 value; 105 106 validate_min = pt->validation_type == NLA_VALIDATE_RANGE || 107 pt->validation_type == NLA_VALIDATE_MIN; 108 validate_max = pt->validation_type == NLA_VALIDATE_RANGE || 109 pt->validation_type == NLA_VALIDATE_MAX; 110 111 switch (pt->type) { 112 case NLA_U8: 113 value = nla_get_u8(nla); 114 break; 115 case NLA_U16: 116 value = nla_get_u16(nla); 117 break; 118 case NLA_U32: 119 value = nla_get_u32(nla); 120 break; 121 case NLA_S8: 122 value = nla_get_s8(nla); 123 break; 124 case NLA_S16: 125 value = nla_get_s16(nla); 126 break; 127 case NLA_S32: 128 value = nla_get_s32(nla); 129 break; 130 case NLA_S64: 131 value = nla_get_s64(nla); 132 break; 133 case NLA_U64: 134 /* treat this one specially, since it may not fit into s64 */ 135 if ((validate_min && nla_get_u64(nla) < pt->min) || 136 (validate_max && nla_get_u64(nla) > pt->max)) { 137 NL_SET_ERR_MSG_ATTR(extack, nla, 138 "integer out of range"); 139 return -ERANGE; 140 } 141 return 0; 142 default: 143 WARN_ON(1); 144 return -EINVAL; 145 } 146 147 if ((validate_min && value < pt->min) || 148 (validate_max && value > pt->max)) { 149 NL_SET_ERR_MSG_ATTR(extack, nla, 150 "integer out of range"); 151 return -ERANGE; 152 } 153 154 return 0; 155 } 156 157 static int validate_nla(const struct nlattr *nla, int maxtype, 158 const struct nla_policy *policy, unsigned int validate, 159 struct netlink_ext_ack *extack) 160 { 161 const struct nla_policy *pt; 162 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla); 163 int err = -ERANGE; 164 165 if (type <= 0 || type > maxtype) 166 return 0; 167 168 pt = &policy[type]; 169 170 BUG_ON(pt->type > NLA_TYPE_MAX); 171 172 if ((nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) || 173 (pt->type == NLA_EXACT_LEN_WARN && attrlen != pt->len)) { 174 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n", 175 current->comm, type); 176 if (validate & NL_VALIDATE_STRICT_ATTRS) { 177 NL_SET_ERR_MSG_ATTR(extack, nla, 178 "invalid attribute length"); 179 return -EINVAL; 180 } 181 } 182 183 switch (pt->type) { 184 case NLA_EXACT_LEN: 185 if (attrlen != pt->len) 186 goto out_err; 187 break; 188 189 case NLA_REJECT: 190 if (extack && pt->validation_data) { 191 NL_SET_BAD_ATTR(extack, nla); 192 extack->_msg = pt->validation_data; 193 return -EINVAL; 194 } 195 err = -EINVAL; 196 goto out_err; 197 198 case NLA_FLAG: 199 if (attrlen > 0) 200 goto out_err; 201 break; 202 203 case NLA_BITFIELD32: 204 if (attrlen != sizeof(struct nla_bitfield32)) 205 goto out_err; 206 207 err = validate_nla_bitfield32(nla, pt->validation_data); 208 if (err) 209 goto out_err; 210 break; 211 212 case NLA_NUL_STRING: 213 if (pt->len) 214 minlen = min_t(int, attrlen, pt->len + 1); 215 else 216 minlen = attrlen; 217 218 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) { 219 err = -EINVAL; 220 goto out_err; 221 } 222 /* fall through */ 223 224 case NLA_STRING: 225 if (attrlen < 1) 226 goto out_err; 227 228 if (pt->len) { 229 char *buf = nla_data(nla); 230 231 if (buf[attrlen - 1] == '\0') 232 attrlen--; 233 234 if (attrlen > pt->len) 235 goto out_err; 236 } 237 break; 238 239 case NLA_BINARY: 240 if (pt->len && attrlen > pt->len) 241 goto out_err; 242 break; 243 244 case NLA_NESTED: 245 /* a nested attributes is allowed to be empty; if its not, 246 * it must have a size of at least NLA_HDRLEN. 247 */ 248 if (attrlen == 0) 249 break; 250 if (attrlen < NLA_HDRLEN) 251 goto out_err; 252 if (pt->validation_data) { 253 err = __nla_validate(nla_data(nla), nla_len(nla), pt->len, 254 pt->validation_data, validate, 255 extack); 256 if (err < 0) { 257 /* 258 * return directly to preserve the inner 259 * error message/attribute pointer 260 */ 261 return err; 262 } 263 } 264 break; 265 case NLA_NESTED_ARRAY: 266 /* a nested array attribute is allowed to be empty; if its not, 267 * it must have a size of at least NLA_HDRLEN. 268 */ 269 if (attrlen == 0) 270 break; 271 if (attrlen < NLA_HDRLEN) 272 goto out_err; 273 if (pt->validation_data) { 274 int err; 275 276 err = nla_validate_array(nla_data(nla), nla_len(nla), 277 pt->len, pt->validation_data, 278 extack, validate); 279 if (err < 0) { 280 /* 281 * return directly to preserve the inner 282 * error message/attribute pointer 283 */ 284 return err; 285 } 286 } 287 break; 288 289 case NLA_UNSPEC: 290 if (validate & NL_VALIDATE_UNSPEC) { 291 NL_SET_ERR_MSG_ATTR(extack, nla, 292 "Unsupported attribute"); 293 return -EINVAL; 294 } 295 /* fall through */ 296 case NLA_MIN_LEN: 297 if (attrlen < pt->len) 298 goto out_err; 299 break; 300 301 default: 302 if (pt->len) 303 minlen = pt->len; 304 else 305 minlen = nla_attr_minlen[pt->type]; 306 307 if (attrlen < minlen) 308 goto out_err; 309 } 310 311 /* further validation */ 312 switch (pt->validation_type) { 313 case NLA_VALIDATE_NONE: 314 /* nothing to do */ 315 break; 316 case NLA_VALIDATE_RANGE: 317 case NLA_VALIDATE_MIN: 318 case NLA_VALIDATE_MAX: 319 err = nla_validate_int_range(pt, nla, extack); 320 if (err) 321 return err; 322 break; 323 case NLA_VALIDATE_FUNCTION: 324 if (pt->validate) { 325 err = pt->validate(nla, extack); 326 if (err) 327 return err; 328 } 329 break; 330 } 331 332 return 0; 333 out_err: 334 NL_SET_ERR_MSG_ATTR(extack, nla, "Attribute failed policy validation"); 335 return err; 336 } 337 338 static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype, 339 const struct nla_policy *policy, 340 unsigned int validate, 341 struct netlink_ext_ack *extack, 342 struct nlattr **tb) 343 { 344 const struct nlattr *nla; 345 int rem; 346 347 if (tb) 348 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); 349 350 nla_for_each_attr(nla, head, len, rem) { 351 u16 type = nla_type(nla); 352 353 if (type == 0 || type > maxtype) { 354 if (validate & NL_VALIDATE_MAXTYPE) { 355 NL_SET_ERR_MSG(extack, "Unknown attribute type"); 356 return -EINVAL; 357 } 358 continue; 359 } 360 if (policy) { 361 int err = validate_nla(nla, maxtype, policy, 362 validate, extack); 363 364 if (err < 0) 365 return err; 366 } 367 368 if (tb) 369 tb[type] = (struct nlattr *)nla; 370 } 371 372 if (unlikely(rem > 0)) { 373 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n", 374 rem, current->comm); 375 NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes"); 376 if (validate & NL_VALIDATE_TRAILING) 377 return -EINVAL; 378 } 379 380 return 0; 381 } 382 383 /** 384 * __nla_validate - Validate a stream of attributes 385 * @head: head of attribute stream 386 * @len: length of attribute stream 387 * @maxtype: maximum attribute type to be expected 388 * @policy: validation policy 389 * @validate: validation strictness 390 * @extack: extended ACK report struct 391 * 392 * Validates all attributes in the specified attribute stream against the 393 * specified policy. Validation depends on the validate flags passed, see 394 * &enum netlink_validation for more details on that. 395 * See documenation of struct nla_policy for more details. 396 * 397 * Returns 0 on success or a negative error code. 398 */ 399 int __nla_validate(const struct nlattr *head, int len, int maxtype, 400 const struct nla_policy *policy, unsigned int validate, 401 struct netlink_ext_ack *extack) 402 { 403 return __nla_validate_parse(head, len, maxtype, policy, validate, 404 extack, NULL); 405 } 406 EXPORT_SYMBOL(__nla_validate); 407 408 /** 409 * nla_policy_len - Determin the max. length of a policy 410 * @policy: policy to use 411 * @n: number of policies 412 * 413 * Determines the max. length of the policy. It is currently used 414 * to allocated Netlink buffers roughly the size of the actual 415 * message. 416 * 417 * Returns 0 on success or a negative error code. 418 */ 419 int 420 nla_policy_len(const struct nla_policy *p, int n) 421 { 422 int i, len = 0; 423 424 for (i = 0; i < n; i++, p++) { 425 if (p->len) 426 len += nla_total_size(p->len); 427 else if (nla_attr_len[p->type]) 428 len += nla_total_size(nla_attr_len[p->type]); 429 else if (nla_attr_minlen[p->type]) 430 len += nla_total_size(nla_attr_minlen[p->type]); 431 } 432 433 return len; 434 } 435 EXPORT_SYMBOL(nla_policy_len); 436 437 /** 438 * __nla_parse - Parse a stream of attributes into a tb buffer 439 * @tb: destination array with maxtype+1 elements 440 * @maxtype: maximum attribute type to be expected 441 * @head: head of attribute stream 442 * @len: length of attribute stream 443 * @policy: validation policy 444 * @validate: validation strictness 445 * @extack: extended ACK pointer 446 * 447 * Parses a stream of attributes and stores a pointer to each attribute in 448 * the tb array accessible via the attribute type. 449 * Validation is controlled by the @validate parameter. 450 * 451 * Returns 0 on success or a negative error code. 452 */ 453 int __nla_parse(struct nlattr **tb, int maxtype, 454 const struct nlattr *head, int len, 455 const struct nla_policy *policy, unsigned int validate, 456 struct netlink_ext_ack *extack) 457 { 458 return __nla_validate_parse(head, len, maxtype, policy, validate, 459 extack, tb); 460 } 461 EXPORT_SYMBOL(__nla_parse); 462 463 /** 464 * nla_find - Find a specific attribute in a stream of attributes 465 * @head: head of attribute stream 466 * @len: length of attribute stream 467 * @attrtype: type of attribute to look for 468 * 469 * Returns the first attribute in the stream matching the specified type. 470 */ 471 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype) 472 { 473 const struct nlattr *nla; 474 int rem; 475 476 nla_for_each_attr(nla, head, len, rem) 477 if (nla_type(nla) == attrtype) 478 return (struct nlattr *)nla; 479 480 return NULL; 481 } 482 EXPORT_SYMBOL(nla_find); 483 484 /** 485 * nla_strlcpy - Copy string attribute payload into a sized buffer 486 * @dst: where to copy the string to 487 * @nla: attribute to copy the string from 488 * @dstsize: size of destination buffer 489 * 490 * Copies at most dstsize - 1 bytes into the destination buffer. 491 * The result is always a valid NUL-terminated string. Unlike 492 * strlcpy the destination buffer is always padded out. 493 * 494 * Returns the length of the source buffer. 495 */ 496 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize) 497 { 498 size_t srclen = nla_len(nla); 499 char *src = nla_data(nla); 500 501 if (srclen > 0 && src[srclen - 1] == '\0') 502 srclen--; 503 504 if (dstsize > 0) { 505 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen; 506 507 memset(dst, 0, dstsize); 508 memcpy(dst, src, len); 509 } 510 511 return srclen; 512 } 513 EXPORT_SYMBOL(nla_strlcpy); 514 515 /** 516 * nla_strdup - Copy string attribute payload into a newly allocated buffer 517 * @nla: attribute to copy the string from 518 * @flags: the type of memory to allocate (see kmalloc). 519 * 520 * Returns a pointer to the allocated buffer or NULL on error. 521 */ 522 char *nla_strdup(const struct nlattr *nla, gfp_t flags) 523 { 524 size_t srclen = nla_len(nla); 525 char *src = nla_data(nla), *dst; 526 527 if (srclen > 0 && src[srclen - 1] == '\0') 528 srclen--; 529 530 dst = kmalloc(srclen + 1, flags); 531 if (dst != NULL) { 532 memcpy(dst, src, srclen); 533 dst[srclen] = '\0'; 534 } 535 return dst; 536 } 537 EXPORT_SYMBOL(nla_strdup); 538 539 /** 540 * nla_memcpy - Copy a netlink attribute into another memory area 541 * @dest: where to copy to memcpy 542 * @src: netlink attribute to copy from 543 * @count: size of the destination area 544 * 545 * Note: The number of bytes copied is limited by the length of 546 * attribute's payload. memcpy 547 * 548 * Returns the number of bytes copied. 549 */ 550 int nla_memcpy(void *dest, const struct nlattr *src, int count) 551 { 552 int minlen = min_t(int, count, nla_len(src)); 553 554 memcpy(dest, nla_data(src), minlen); 555 if (count > minlen) 556 memset(dest + minlen, 0, count - minlen); 557 558 return minlen; 559 } 560 EXPORT_SYMBOL(nla_memcpy); 561 562 /** 563 * nla_memcmp - Compare an attribute with sized memory area 564 * @nla: netlink attribute 565 * @data: memory area 566 * @size: size of memory area 567 */ 568 int nla_memcmp(const struct nlattr *nla, const void *data, 569 size_t size) 570 { 571 int d = nla_len(nla) - size; 572 573 if (d == 0) 574 d = memcmp(nla_data(nla), data, size); 575 576 return d; 577 } 578 EXPORT_SYMBOL(nla_memcmp); 579 580 /** 581 * nla_strcmp - Compare a string attribute against a string 582 * @nla: netlink string attribute 583 * @str: another string 584 */ 585 int nla_strcmp(const struct nlattr *nla, const char *str) 586 { 587 int len = strlen(str); 588 char *buf = nla_data(nla); 589 int attrlen = nla_len(nla); 590 int d; 591 592 if (attrlen > 0 && buf[attrlen - 1] == '\0') 593 attrlen--; 594 595 d = attrlen - len; 596 if (d == 0) 597 d = memcmp(nla_data(nla), str, len); 598 599 return d; 600 } 601 EXPORT_SYMBOL(nla_strcmp); 602 603 #ifdef CONFIG_NET 604 /** 605 * __nla_reserve - reserve room for attribute on the skb 606 * @skb: socket buffer to reserve room on 607 * @attrtype: attribute type 608 * @attrlen: length of attribute payload 609 * 610 * Adds a netlink attribute header to a socket buffer and reserves 611 * room for the payload but does not copy it. 612 * 613 * The caller is responsible to ensure that the skb provides enough 614 * tailroom for the attribute header and payload. 615 */ 616 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) 617 { 618 struct nlattr *nla; 619 620 nla = skb_put(skb, nla_total_size(attrlen)); 621 nla->nla_type = attrtype; 622 nla->nla_len = nla_attr_size(attrlen); 623 624 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen)); 625 626 return nla; 627 } 628 EXPORT_SYMBOL(__nla_reserve); 629 630 /** 631 * __nla_reserve_64bit - reserve room for attribute on the skb and align it 632 * @skb: socket buffer to reserve room on 633 * @attrtype: attribute type 634 * @attrlen: length of attribute payload 635 * @padattr: attribute type for the padding 636 * 637 * Adds a netlink attribute header to a socket buffer and reserves 638 * room for the payload but does not copy it. It also ensure that this 639 * attribute will have a 64-bit aligned nla_data() area. 640 * 641 * The caller is responsible to ensure that the skb provides enough 642 * tailroom for the attribute header and payload. 643 */ 644 struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype, 645 int attrlen, int padattr) 646 { 647 if (nla_need_padding_for_64bit(skb)) 648 nla_align_64bit(skb, padattr); 649 650 return __nla_reserve(skb, attrtype, attrlen); 651 } 652 EXPORT_SYMBOL(__nla_reserve_64bit); 653 654 /** 655 * __nla_reserve_nohdr - reserve room for attribute without header 656 * @skb: socket buffer to reserve room on 657 * @attrlen: length of attribute payload 658 * 659 * Reserves room for attribute payload without a header. 660 * 661 * The caller is responsible to ensure that the skb provides enough 662 * tailroom for the payload. 663 */ 664 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen) 665 { 666 return skb_put_zero(skb, NLA_ALIGN(attrlen)); 667 } 668 EXPORT_SYMBOL(__nla_reserve_nohdr); 669 670 /** 671 * nla_reserve - reserve room for attribute on the skb 672 * @skb: socket buffer to reserve room on 673 * @attrtype: attribute type 674 * @attrlen: length of attribute payload 675 * 676 * Adds a netlink attribute header to a socket buffer and reserves 677 * room for the payload but does not copy it. 678 * 679 * Returns NULL if the tailroom of the skb is insufficient to store 680 * the attribute header and payload. 681 */ 682 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) 683 { 684 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) 685 return NULL; 686 687 return __nla_reserve(skb, attrtype, attrlen); 688 } 689 EXPORT_SYMBOL(nla_reserve); 690 691 /** 692 * nla_reserve_64bit - reserve room for attribute on the skb and align it 693 * @skb: socket buffer to reserve room on 694 * @attrtype: attribute type 695 * @attrlen: length of attribute payload 696 * @padattr: attribute type for the padding 697 * 698 * Adds a netlink attribute header to a socket buffer and reserves 699 * room for the payload but does not copy it. It also ensure that this 700 * attribute will have a 64-bit aligned nla_data() area. 701 * 702 * Returns NULL if the tailroom of the skb is insufficient to store 703 * the attribute header and payload. 704 */ 705 struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen, 706 int padattr) 707 { 708 size_t len; 709 710 if (nla_need_padding_for_64bit(skb)) 711 len = nla_total_size_64bit(attrlen); 712 else 713 len = nla_total_size(attrlen); 714 if (unlikely(skb_tailroom(skb) < len)) 715 return NULL; 716 717 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr); 718 } 719 EXPORT_SYMBOL(nla_reserve_64bit); 720 721 /** 722 * nla_reserve_nohdr - reserve room for attribute without header 723 * @skb: socket buffer to reserve room on 724 * @attrlen: length of attribute payload 725 * 726 * Reserves room for attribute payload without a header. 727 * 728 * Returns NULL if the tailroom of the skb is insufficient to store 729 * the attribute payload. 730 */ 731 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen) 732 { 733 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) 734 return NULL; 735 736 return __nla_reserve_nohdr(skb, attrlen); 737 } 738 EXPORT_SYMBOL(nla_reserve_nohdr); 739 740 /** 741 * __nla_put - Add a netlink attribute to a socket buffer 742 * @skb: socket buffer to add attribute to 743 * @attrtype: attribute type 744 * @attrlen: length of attribute payload 745 * @data: head of attribute payload 746 * 747 * The caller is responsible to ensure that the skb provides enough 748 * tailroom for the attribute header and payload. 749 */ 750 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, 751 const void *data) 752 { 753 struct nlattr *nla; 754 755 nla = __nla_reserve(skb, attrtype, attrlen); 756 memcpy(nla_data(nla), data, attrlen); 757 } 758 EXPORT_SYMBOL(__nla_put); 759 760 /** 761 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it 762 * @skb: socket buffer to add attribute to 763 * @attrtype: attribute type 764 * @attrlen: length of attribute payload 765 * @data: head of attribute payload 766 * @padattr: attribute type for the padding 767 * 768 * The caller is responsible to ensure that the skb provides enough 769 * tailroom for the attribute header and payload. 770 */ 771 void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 772 const void *data, int padattr) 773 { 774 struct nlattr *nla; 775 776 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr); 777 memcpy(nla_data(nla), data, attrlen); 778 } 779 EXPORT_SYMBOL(__nla_put_64bit); 780 781 /** 782 * __nla_put_nohdr - Add a netlink attribute without header 783 * @skb: socket buffer to add attribute to 784 * @attrlen: length of attribute payload 785 * @data: head of attribute payload 786 * 787 * The caller is responsible to ensure that the skb provides enough 788 * tailroom for the attribute payload. 789 */ 790 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) 791 { 792 void *start; 793 794 start = __nla_reserve_nohdr(skb, attrlen); 795 memcpy(start, data, attrlen); 796 } 797 EXPORT_SYMBOL(__nla_put_nohdr); 798 799 /** 800 * nla_put - Add a netlink attribute to a socket buffer 801 * @skb: socket buffer to add attribute to 802 * @attrtype: attribute type 803 * @attrlen: length of attribute payload 804 * @data: head of attribute payload 805 * 806 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store 807 * the attribute header and payload. 808 */ 809 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data) 810 { 811 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) 812 return -EMSGSIZE; 813 814 __nla_put(skb, attrtype, attrlen, data); 815 return 0; 816 } 817 EXPORT_SYMBOL(nla_put); 818 819 /** 820 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it 821 * @skb: socket buffer to add attribute to 822 * @attrtype: attribute type 823 * @attrlen: length of attribute payload 824 * @data: head of attribute payload 825 * @padattr: attribute type for the padding 826 * 827 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store 828 * the attribute header and payload. 829 */ 830 int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 831 const void *data, int padattr) 832 { 833 size_t len; 834 835 if (nla_need_padding_for_64bit(skb)) 836 len = nla_total_size_64bit(attrlen); 837 else 838 len = nla_total_size(attrlen); 839 if (unlikely(skb_tailroom(skb) < len)) 840 return -EMSGSIZE; 841 842 __nla_put_64bit(skb, attrtype, attrlen, data, padattr); 843 return 0; 844 } 845 EXPORT_SYMBOL(nla_put_64bit); 846 847 /** 848 * nla_put_nohdr - Add a netlink attribute without header 849 * @skb: socket buffer to add attribute to 850 * @attrlen: length of attribute payload 851 * @data: head of attribute payload 852 * 853 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store 854 * the attribute payload. 855 */ 856 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) 857 { 858 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) 859 return -EMSGSIZE; 860 861 __nla_put_nohdr(skb, attrlen, data); 862 return 0; 863 } 864 EXPORT_SYMBOL(nla_put_nohdr); 865 866 /** 867 * nla_append - Add a netlink attribute without header or padding 868 * @skb: socket buffer to add attribute to 869 * @attrlen: length of attribute payload 870 * @data: head of attribute payload 871 * 872 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store 873 * the attribute payload. 874 */ 875 int nla_append(struct sk_buff *skb, int attrlen, const void *data) 876 { 877 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) 878 return -EMSGSIZE; 879 880 skb_put_data(skb, data, attrlen); 881 return 0; 882 } 883 EXPORT_SYMBOL(nla_append); 884 #endif 885