0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/export.h>
0010 #include <linux/kernel.h>
0011 #include <linux/errno.h>
0012 #include <linux/jiffies.h>
0013 #include <linux/skbuff.h>
0014 #include <linux/string.h>
0015 #include <linux/types.h>
0016 #include <net/netlink.h>
0017
0018
0019
0020
0021
0022
0023 static const u8 nla_attr_len[NLA_TYPE_MAX+1] = {
0024 [NLA_U8] = sizeof(u8),
0025 [NLA_U16] = sizeof(u16),
0026 [NLA_U32] = sizeof(u32),
0027 [NLA_U64] = sizeof(u64),
0028 [NLA_S8] = sizeof(s8),
0029 [NLA_S16] = sizeof(s16),
0030 [NLA_S32] = sizeof(s32),
0031 [NLA_S64] = sizeof(s64),
0032 };
0033
0034 static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = {
0035 [NLA_U8] = sizeof(u8),
0036 [NLA_U16] = sizeof(u16),
0037 [NLA_U32] = sizeof(u32),
0038 [NLA_U64] = sizeof(u64),
0039 [NLA_MSECS] = sizeof(u64),
0040 [NLA_NESTED] = NLA_HDRLEN,
0041 [NLA_S8] = sizeof(s8),
0042 [NLA_S16] = sizeof(s16),
0043 [NLA_S32] = sizeof(s32),
0044 [NLA_S64] = sizeof(s64),
0045 };
0046
0047
0048
0049
0050
0051
0052
0053 #define MAX_POLICY_RECURSION_DEPTH 10
0054
0055 static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
0056 const struct nla_policy *policy,
0057 unsigned int validate,
0058 struct netlink_ext_ack *extack,
0059 struct nlattr **tb, unsigned int depth);
0060
0061 static int validate_nla_bitfield32(const struct nlattr *nla,
0062 const u32 valid_flags_mask)
0063 {
0064 const struct nla_bitfield32 *bf = nla_data(nla);
0065
0066 if (!valid_flags_mask)
0067 return -EINVAL;
0068
0069
0070 if (bf->selector & ~valid_flags_mask)
0071 return -EINVAL;
0072
0073
0074 if (bf->value & ~valid_flags_mask)
0075 return -EINVAL;
0076
0077
0078 if (bf->value & ~bf->selector)
0079 return -EINVAL;
0080
0081 return 0;
0082 }
0083
0084 static int nla_validate_array(const struct nlattr *head, int len, int maxtype,
0085 const struct nla_policy *policy,
0086 struct netlink_ext_ack *extack,
0087 unsigned int validate, unsigned int depth)
0088 {
0089 const struct nlattr *entry;
0090 int rem;
0091
0092 nla_for_each_attr(entry, head, len, rem) {
0093 int ret;
0094
0095 if (nla_len(entry) == 0)
0096 continue;
0097
0098 if (nla_len(entry) < NLA_HDRLEN) {
0099 NL_SET_ERR_MSG_ATTR_POL(extack, entry, policy,
0100 "Array element too short");
0101 return -ERANGE;
0102 }
0103
0104 ret = __nla_validate_parse(nla_data(entry), nla_len(entry),
0105 maxtype, policy, validate, extack,
0106 NULL, depth + 1);
0107 if (ret < 0)
0108 return ret;
0109 }
0110
0111 return 0;
0112 }
0113
0114 void nla_get_range_unsigned(const struct nla_policy *pt,
0115 struct netlink_range_validation *range)
0116 {
0117 WARN_ON_ONCE(pt->validation_type != NLA_VALIDATE_RANGE_PTR &&
0118 (pt->min < 0 || pt->max < 0));
0119
0120 range->min = 0;
0121
0122 switch (pt->type) {
0123 case NLA_U8:
0124 range->max = U8_MAX;
0125 break;
0126 case NLA_U16:
0127 case NLA_BINARY:
0128 range->max = U16_MAX;
0129 break;
0130 case NLA_U32:
0131 range->max = U32_MAX;
0132 break;
0133 case NLA_U64:
0134 case NLA_MSECS:
0135 range->max = U64_MAX;
0136 break;
0137 default:
0138 WARN_ON_ONCE(1);
0139 return;
0140 }
0141
0142 switch (pt->validation_type) {
0143 case NLA_VALIDATE_RANGE:
0144 case NLA_VALIDATE_RANGE_WARN_TOO_LONG:
0145 range->min = pt->min;
0146 range->max = pt->max;
0147 break;
0148 case NLA_VALIDATE_RANGE_PTR:
0149 *range = *pt->range;
0150 break;
0151 case NLA_VALIDATE_MIN:
0152 range->min = pt->min;
0153 break;
0154 case NLA_VALIDATE_MAX:
0155 range->max = pt->max;
0156 break;
0157 default:
0158 break;
0159 }
0160 }
0161
0162 static int nla_validate_range_unsigned(const struct nla_policy *pt,
0163 const struct nlattr *nla,
0164 struct netlink_ext_ack *extack,
0165 unsigned int validate)
0166 {
0167 struct netlink_range_validation range;
0168 u64 value;
0169
0170 switch (pt->type) {
0171 case NLA_U8:
0172 value = nla_get_u8(nla);
0173 break;
0174 case NLA_U16:
0175 value = nla_get_u16(nla);
0176 break;
0177 case NLA_U32:
0178 value = nla_get_u32(nla);
0179 break;
0180 case NLA_U64:
0181 case NLA_MSECS:
0182 value = nla_get_u64(nla);
0183 break;
0184 case NLA_BINARY:
0185 value = nla_len(nla);
0186 break;
0187 default:
0188 return -EINVAL;
0189 }
0190
0191 nla_get_range_unsigned(pt, &range);
0192
0193 if (pt->validation_type == NLA_VALIDATE_RANGE_WARN_TOO_LONG &&
0194 pt->type == NLA_BINARY && value > range.max) {
0195 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
0196 current->comm, pt->type);
0197 if (validate & NL_VALIDATE_STRICT_ATTRS) {
0198 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
0199 "invalid attribute length");
0200 return -EINVAL;
0201 }
0202
0203
0204 return 0;
0205 }
0206
0207 if (value < range.min || value > range.max) {
0208 bool binary = pt->type == NLA_BINARY;
0209
0210 if (binary)
0211 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
0212 "binary attribute size out of range");
0213 else
0214 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
0215 "integer out of range");
0216
0217 return -ERANGE;
0218 }
0219
0220 return 0;
0221 }
0222
0223 void nla_get_range_signed(const struct nla_policy *pt,
0224 struct netlink_range_validation_signed *range)
0225 {
0226 switch (pt->type) {
0227 case NLA_S8:
0228 range->min = S8_MIN;
0229 range->max = S8_MAX;
0230 break;
0231 case NLA_S16:
0232 range->min = S16_MIN;
0233 range->max = S16_MAX;
0234 break;
0235 case NLA_S32:
0236 range->min = S32_MIN;
0237 range->max = S32_MAX;
0238 break;
0239 case NLA_S64:
0240 range->min = S64_MIN;
0241 range->max = S64_MAX;
0242 break;
0243 default:
0244 WARN_ON_ONCE(1);
0245 return;
0246 }
0247
0248 switch (pt->validation_type) {
0249 case NLA_VALIDATE_RANGE:
0250 range->min = pt->min;
0251 range->max = pt->max;
0252 break;
0253 case NLA_VALIDATE_RANGE_PTR:
0254 *range = *pt->range_signed;
0255 break;
0256 case NLA_VALIDATE_MIN:
0257 range->min = pt->min;
0258 break;
0259 case NLA_VALIDATE_MAX:
0260 range->max = pt->max;
0261 break;
0262 default:
0263 break;
0264 }
0265 }
0266
0267 static int nla_validate_int_range_signed(const struct nla_policy *pt,
0268 const struct nlattr *nla,
0269 struct netlink_ext_ack *extack)
0270 {
0271 struct netlink_range_validation_signed range;
0272 s64 value;
0273
0274 switch (pt->type) {
0275 case NLA_S8:
0276 value = nla_get_s8(nla);
0277 break;
0278 case NLA_S16:
0279 value = nla_get_s16(nla);
0280 break;
0281 case NLA_S32:
0282 value = nla_get_s32(nla);
0283 break;
0284 case NLA_S64:
0285 value = nla_get_s64(nla);
0286 break;
0287 default:
0288 return -EINVAL;
0289 }
0290
0291 nla_get_range_signed(pt, &range);
0292
0293 if (value < range.min || value > range.max) {
0294 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
0295 "integer out of range");
0296 return -ERANGE;
0297 }
0298
0299 return 0;
0300 }
0301
0302 static int nla_validate_int_range(const struct nla_policy *pt,
0303 const struct nlattr *nla,
0304 struct netlink_ext_ack *extack,
0305 unsigned int validate)
0306 {
0307 switch (pt->type) {
0308 case NLA_U8:
0309 case NLA_U16:
0310 case NLA_U32:
0311 case NLA_U64:
0312 case NLA_MSECS:
0313 case NLA_BINARY:
0314 return nla_validate_range_unsigned(pt, nla, extack, validate);
0315 case NLA_S8:
0316 case NLA_S16:
0317 case NLA_S32:
0318 case NLA_S64:
0319 return nla_validate_int_range_signed(pt, nla, extack);
0320 default:
0321 WARN_ON(1);
0322 return -EINVAL;
0323 }
0324 }
0325
0326 static int nla_validate_mask(const struct nla_policy *pt,
0327 const struct nlattr *nla,
0328 struct netlink_ext_ack *extack)
0329 {
0330 u64 value;
0331
0332 switch (pt->type) {
0333 case NLA_U8:
0334 value = nla_get_u8(nla);
0335 break;
0336 case NLA_U16:
0337 value = nla_get_u16(nla);
0338 break;
0339 case NLA_U32:
0340 value = nla_get_u32(nla);
0341 break;
0342 case NLA_U64:
0343 value = nla_get_u64(nla);
0344 break;
0345 default:
0346 return -EINVAL;
0347 }
0348
0349 if (value & ~(u64)pt->mask) {
0350 NL_SET_ERR_MSG_ATTR(extack, nla, "reserved bit set");
0351 return -EINVAL;
0352 }
0353
0354 return 0;
0355 }
0356
0357 static int validate_nla(const struct nlattr *nla, int maxtype,
0358 const struct nla_policy *policy, unsigned int validate,
0359 struct netlink_ext_ack *extack, unsigned int depth)
0360 {
0361 u16 strict_start_type = policy[0].strict_start_type;
0362 const struct nla_policy *pt;
0363 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
0364 int err = -ERANGE;
0365
0366 if (strict_start_type && type >= strict_start_type)
0367 validate |= NL_VALIDATE_STRICT;
0368
0369 if (type <= 0 || type > maxtype)
0370 return 0;
0371
0372 pt = &policy[type];
0373
0374 BUG_ON(pt->type > NLA_TYPE_MAX);
0375
0376 if (nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) {
0377 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
0378 current->comm, type);
0379 if (validate & NL_VALIDATE_STRICT_ATTRS) {
0380 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
0381 "invalid attribute length");
0382 return -EINVAL;
0383 }
0384 }
0385
0386 if (validate & NL_VALIDATE_NESTED) {
0387 if ((pt->type == NLA_NESTED || pt->type == NLA_NESTED_ARRAY) &&
0388 !(nla->nla_type & NLA_F_NESTED)) {
0389 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
0390 "NLA_F_NESTED is missing");
0391 return -EINVAL;
0392 }
0393 if (pt->type != NLA_NESTED && pt->type != NLA_NESTED_ARRAY &&
0394 pt->type != NLA_UNSPEC && (nla->nla_type & NLA_F_NESTED)) {
0395 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
0396 "NLA_F_NESTED not expected");
0397 return -EINVAL;
0398 }
0399 }
0400
0401 switch (pt->type) {
0402 case NLA_REJECT:
0403 if (extack && pt->reject_message) {
0404 NL_SET_BAD_ATTR(extack, nla);
0405 extack->_msg = pt->reject_message;
0406 return -EINVAL;
0407 }
0408 err = -EINVAL;
0409 goto out_err;
0410
0411 case NLA_FLAG:
0412 if (attrlen > 0)
0413 goto out_err;
0414 break;
0415
0416 case NLA_BITFIELD32:
0417 if (attrlen != sizeof(struct nla_bitfield32))
0418 goto out_err;
0419
0420 err = validate_nla_bitfield32(nla, pt->bitfield32_valid);
0421 if (err)
0422 goto out_err;
0423 break;
0424
0425 case NLA_NUL_STRING:
0426 if (pt->len)
0427 minlen = min_t(int, attrlen, pt->len + 1);
0428 else
0429 minlen = attrlen;
0430
0431 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
0432 err = -EINVAL;
0433 goto out_err;
0434 }
0435 fallthrough;
0436
0437 case NLA_STRING:
0438 if (attrlen < 1)
0439 goto out_err;
0440
0441 if (pt->len) {
0442 char *buf = nla_data(nla);
0443
0444 if (buf[attrlen - 1] == '\0')
0445 attrlen--;
0446
0447 if (attrlen > pt->len)
0448 goto out_err;
0449 }
0450 break;
0451
0452 case NLA_BINARY:
0453 if (pt->len && attrlen > pt->len)
0454 goto out_err;
0455 break;
0456
0457 case NLA_NESTED:
0458
0459
0460
0461 if (attrlen == 0)
0462 break;
0463 if (attrlen < NLA_HDRLEN)
0464 goto out_err;
0465 if (pt->nested_policy) {
0466 err = __nla_validate_parse(nla_data(nla), nla_len(nla),
0467 pt->len, pt->nested_policy,
0468 validate, extack, NULL,
0469 depth + 1);
0470 if (err < 0) {
0471
0472
0473
0474
0475 return err;
0476 }
0477 }
0478 break;
0479 case NLA_NESTED_ARRAY:
0480
0481
0482
0483 if (attrlen == 0)
0484 break;
0485 if (attrlen < NLA_HDRLEN)
0486 goto out_err;
0487 if (pt->nested_policy) {
0488 int err;
0489
0490 err = nla_validate_array(nla_data(nla), nla_len(nla),
0491 pt->len, pt->nested_policy,
0492 extack, validate, depth);
0493 if (err < 0) {
0494
0495
0496
0497
0498 return err;
0499 }
0500 }
0501 break;
0502
0503 case NLA_UNSPEC:
0504 if (validate & NL_VALIDATE_UNSPEC) {
0505 NL_SET_ERR_MSG_ATTR(extack, nla,
0506 "Unsupported attribute");
0507 return -EINVAL;
0508 }
0509 if (attrlen < pt->len)
0510 goto out_err;
0511 break;
0512
0513 default:
0514 if (pt->len)
0515 minlen = pt->len;
0516 else
0517 minlen = nla_attr_minlen[pt->type];
0518
0519 if (attrlen < minlen)
0520 goto out_err;
0521 }
0522
0523
0524 switch (pt->validation_type) {
0525 case NLA_VALIDATE_NONE:
0526
0527 break;
0528 case NLA_VALIDATE_RANGE_PTR:
0529 case NLA_VALIDATE_RANGE:
0530 case NLA_VALIDATE_RANGE_WARN_TOO_LONG:
0531 case NLA_VALIDATE_MIN:
0532 case NLA_VALIDATE_MAX:
0533 err = nla_validate_int_range(pt, nla, extack, validate);
0534 if (err)
0535 return err;
0536 break;
0537 case NLA_VALIDATE_MASK:
0538 err = nla_validate_mask(pt, nla, extack);
0539 if (err)
0540 return err;
0541 break;
0542 case NLA_VALIDATE_FUNCTION:
0543 if (pt->validate) {
0544 err = pt->validate(nla, extack);
0545 if (err)
0546 return err;
0547 }
0548 break;
0549 }
0550
0551 return 0;
0552 out_err:
0553 NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
0554 "Attribute failed policy validation");
0555 return err;
0556 }
0557
0558 static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
0559 const struct nla_policy *policy,
0560 unsigned int validate,
0561 struct netlink_ext_ack *extack,
0562 struct nlattr **tb, unsigned int depth)
0563 {
0564 const struct nlattr *nla;
0565 int rem;
0566
0567 if (depth >= MAX_POLICY_RECURSION_DEPTH) {
0568 NL_SET_ERR_MSG(extack,
0569 "allowed policy recursion depth exceeded");
0570 return -EINVAL;
0571 }
0572
0573 if (tb)
0574 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
0575
0576 nla_for_each_attr(nla, head, len, rem) {
0577 u16 type = nla_type(nla);
0578
0579 if (type == 0 || type > maxtype) {
0580 if (validate & NL_VALIDATE_MAXTYPE) {
0581 NL_SET_ERR_MSG_ATTR(extack, nla,
0582 "Unknown attribute type");
0583 return -EINVAL;
0584 }
0585 continue;
0586 }
0587 if (policy) {
0588 int err = validate_nla(nla, maxtype, policy,
0589 validate, extack, depth);
0590
0591 if (err < 0)
0592 return err;
0593 }
0594
0595 if (tb)
0596 tb[type] = (struct nlattr *)nla;
0597 }
0598
0599 if (unlikely(rem > 0)) {
0600 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
0601 rem, current->comm);
0602 NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes");
0603 if (validate & NL_VALIDATE_TRAILING)
0604 return -EINVAL;
0605 }
0606
0607 return 0;
0608 }
0609
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622
0623
0624
0625
0626 int __nla_validate(const struct nlattr *head, int len, int maxtype,
0627 const struct nla_policy *policy, unsigned int validate,
0628 struct netlink_ext_ack *extack)
0629 {
0630 return __nla_validate_parse(head, len, maxtype, policy, validate,
0631 extack, NULL, 0);
0632 }
0633 EXPORT_SYMBOL(__nla_validate);
0634
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646 int
0647 nla_policy_len(const struct nla_policy *p, int n)
0648 {
0649 int i, len = 0;
0650
0651 for (i = 0; i < n; i++, p++) {
0652 if (p->len)
0653 len += nla_total_size(p->len);
0654 else if (nla_attr_len[p->type])
0655 len += nla_total_size(nla_attr_len[p->type]);
0656 else if (nla_attr_minlen[p->type])
0657 len += nla_total_size(nla_attr_minlen[p->type]);
0658 }
0659
0660 return len;
0661 }
0662 EXPORT_SYMBOL(nla_policy_len);
0663
0664
0665
0666
0667
0668
0669
0670
0671
0672
0673
0674
0675
0676
0677
0678
0679
0680 int __nla_parse(struct nlattr **tb, int maxtype,
0681 const struct nlattr *head, int len,
0682 const struct nla_policy *policy, unsigned int validate,
0683 struct netlink_ext_ack *extack)
0684 {
0685 return __nla_validate_parse(head, len, maxtype, policy, validate,
0686 extack, tb, 0);
0687 }
0688 EXPORT_SYMBOL(__nla_parse);
0689
0690
0691
0692
0693
0694
0695
0696
0697
0698 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
0699 {
0700 const struct nlattr *nla;
0701 int rem;
0702
0703 nla_for_each_attr(nla, head, len, rem)
0704 if (nla_type(nla) == attrtype)
0705 return (struct nlattr *)nla;
0706
0707 return NULL;
0708 }
0709 EXPORT_SYMBOL(nla_find);
0710
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725 ssize_t nla_strscpy(char *dst, const struct nlattr *nla, size_t dstsize)
0726 {
0727 size_t srclen = nla_len(nla);
0728 char *src = nla_data(nla);
0729 ssize_t ret;
0730 size_t len;
0731
0732 if (dstsize == 0 || WARN_ON_ONCE(dstsize > U16_MAX))
0733 return -E2BIG;
0734
0735 if (srclen > 0 && src[srclen - 1] == '\0')
0736 srclen--;
0737
0738 if (srclen >= dstsize) {
0739 len = dstsize - 1;
0740 ret = -E2BIG;
0741 } else {
0742 len = srclen;
0743 ret = len;
0744 }
0745
0746 memcpy(dst, src, len);
0747
0748 memset(dst + len, 0, dstsize - len);
0749
0750 return ret;
0751 }
0752 EXPORT_SYMBOL(nla_strscpy);
0753
0754
0755
0756
0757
0758
0759
0760
0761 char *nla_strdup(const struct nlattr *nla, gfp_t flags)
0762 {
0763 size_t srclen = nla_len(nla);
0764 char *src = nla_data(nla), *dst;
0765
0766 if (srclen > 0 && src[srclen - 1] == '\0')
0767 srclen--;
0768
0769 dst = kmalloc(srclen + 1, flags);
0770 if (dst != NULL) {
0771 memcpy(dst, src, srclen);
0772 dst[srclen] = '\0';
0773 }
0774 return dst;
0775 }
0776 EXPORT_SYMBOL(nla_strdup);
0777
0778
0779
0780
0781
0782
0783
0784
0785
0786
0787
0788
0789 int nla_memcpy(void *dest, const struct nlattr *src, int count)
0790 {
0791 int minlen = min_t(int, count, nla_len(src));
0792
0793 memcpy(dest, nla_data(src), minlen);
0794 if (count > minlen)
0795 memset(dest + minlen, 0, count - minlen);
0796
0797 return minlen;
0798 }
0799 EXPORT_SYMBOL(nla_memcpy);
0800
0801
0802
0803
0804
0805
0806
0807 int nla_memcmp(const struct nlattr *nla, const void *data,
0808 size_t size)
0809 {
0810 int d = nla_len(nla) - size;
0811
0812 if (d == 0)
0813 d = memcmp(nla_data(nla), data, size);
0814
0815 return d;
0816 }
0817 EXPORT_SYMBOL(nla_memcmp);
0818
0819
0820
0821
0822
0823
0824 int nla_strcmp(const struct nlattr *nla, const char *str)
0825 {
0826 int len = strlen(str);
0827 char *buf = nla_data(nla);
0828 int attrlen = nla_len(nla);
0829 int d;
0830
0831 while (attrlen > 0 && buf[attrlen - 1] == '\0')
0832 attrlen--;
0833
0834 d = attrlen - len;
0835 if (d == 0)
0836 d = memcmp(nla_data(nla), str, len);
0837
0838 return d;
0839 }
0840 EXPORT_SYMBOL(nla_strcmp);
0841
0842 #ifdef CONFIG_NET
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853
0854
0855 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
0856 {
0857 struct nlattr *nla;
0858
0859 nla = skb_put(skb, nla_total_size(attrlen));
0860 nla->nla_type = attrtype;
0861 nla->nla_len = nla_attr_size(attrlen);
0862
0863 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
0864
0865 return nla;
0866 }
0867 EXPORT_SYMBOL(__nla_reserve);
0868
0869
0870
0871
0872
0873
0874
0875
0876
0877
0878
0879
0880
0881
0882
0883 struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
0884 int attrlen, int padattr)
0885 {
0886 nla_align_64bit(skb, padattr);
0887
0888 return __nla_reserve(skb, attrtype, attrlen);
0889 }
0890 EXPORT_SYMBOL(__nla_reserve_64bit);
0891
0892
0893
0894
0895
0896
0897
0898
0899
0900
0901
0902 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
0903 {
0904 return skb_put_zero(skb, NLA_ALIGN(attrlen));
0905 }
0906 EXPORT_SYMBOL(__nla_reserve_nohdr);
0907
0908
0909
0910
0911
0912
0913
0914
0915
0916
0917
0918
0919
0920 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
0921 {
0922 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
0923 return NULL;
0924
0925 return __nla_reserve(skb, attrtype, attrlen);
0926 }
0927 EXPORT_SYMBOL(nla_reserve);
0928
0929
0930
0931
0932
0933
0934
0935
0936
0937
0938
0939
0940
0941
0942
0943 struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
0944 int padattr)
0945 {
0946 size_t len;
0947
0948 if (nla_need_padding_for_64bit(skb))
0949 len = nla_total_size_64bit(attrlen);
0950 else
0951 len = nla_total_size(attrlen);
0952 if (unlikely(skb_tailroom(skb) < len))
0953 return NULL;
0954
0955 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
0956 }
0957 EXPORT_SYMBOL(nla_reserve_64bit);
0958
0959
0960
0961
0962
0963
0964
0965
0966
0967
0968
0969 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
0970 {
0971 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
0972 return NULL;
0973
0974 return __nla_reserve_nohdr(skb, attrlen);
0975 }
0976 EXPORT_SYMBOL(nla_reserve_nohdr);
0977
0978
0979
0980
0981
0982
0983
0984
0985
0986
0987
0988 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
0989 const void *data)
0990 {
0991 struct nlattr *nla;
0992
0993 nla = __nla_reserve(skb, attrtype, attrlen);
0994 memcpy(nla_data(nla), data, attrlen);
0995 }
0996 EXPORT_SYMBOL(__nla_put);
0997
0998
0999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009 void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
1010 const void *data, int padattr)
1011 {
1012 struct nlattr *nla;
1013
1014 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
1015 memcpy(nla_data(nla), data, attrlen);
1016 }
1017 EXPORT_SYMBOL(__nla_put_64bit);
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
1029 {
1030 void *start;
1031
1032 start = __nla_reserve_nohdr(skb, attrlen);
1033 memcpy(start, data, attrlen);
1034 }
1035 EXPORT_SYMBOL(__nla_put_nohdr);
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
1048 {
1049 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
1050 return -EMSGSIZE;
1051
1052 __nla_put(skb, attrtype, attrlen, data);
1053 return 0;
1054 }
1055 EXPORT_SYMBOL(nla_put);
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068 int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
1069 const void *data, int padattr)
1070 {
1071 size_t len;
1072
1073 if (nla_need_padding_for_64bit(skb))
1074 len = nla_total_size_64bit(attrlen);
1075 else
1076 len = nla_total_size(attrlen);
1077 if (unlikely(skb_tailroom(skb) < len))
1078 return -EMSGSIZE;
1079
1080 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
1081 return 0;
1082 }
1083 EXPORT_SYMBOL(nla_put_64bit);
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
1095 {
1096 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
1097 return -EMSGSIZE;
1098
1099 __nla_put_nohdr(skb, attrlen, data);
1100 return 0;
1101 }
1102 EXPORT_SYMBOL(nla_put_nohdr);
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113 int nla_append(struct sk_buff *skb, int attrlen, const void *data)
1114 {
1115 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
1116 return -EMSGSIZE;
1117
1118 skb_put_data(skb, data, attrlen);
1119 return 0;
1120 }
1121 EXPORT_SYMBOL(nla_append);
1122 #endif