Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * NETLINK      Netlink attributes
0004  *
0005  *      Authors:    Thomas Graf <tgraf@suug.ch>
0006  *              Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
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 /* For these data types, attribute length should be exactly the given
0019  * size. However, to maintain compatibility with broken commands, if the
0020  * attribute length does not match the expected size a warning is emitted
0021  * to the user that the command is sending invalid data and needs to be fixed.
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  * Nested policies might refer back to the original
0049  * policy in some cases, and userspace could try to
0050  * abuse that and recurse by nesting in the right
0051  * ways. Limit recursion to avoid this problem.
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     /*disallow invalid bit selector */
0070     if (bf->selector & ~valid_flags_mask)
0071         return -EINVAL;
0072 
0073     /*disallow invalid bit values */
0074     if (bf->value & ~valid_flags_mask)
0075         return -EINVAL;
0076 
0077     /*disallow valid bit values that are not selected*/
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         /* this assumes min <= max (don't validate against min) */
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         /* a nested attributes is allowed to be empty; if its not,
0459          * it must have a size of at least NLA_HDRLEN.
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                  * return directly to preserve the inner
0473                  * error message/attribute pointer
0474                  */
0475                 return err;
0476             }
0477         }
0478         break;
0479     case NLA_NESTED_ARRAY:
0480         /* a nested array attribute is allowed to be empty; if its not,
0481          * it must have a size of at least NLA_HDRLEN.
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                  * return directly to preserve the inner
0496                  * error message/attribute pointer
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     /* further validation */
0524     switch (pt->validation_type) {
0525     case NLA_VALIDATE_NONE:
0526         /* nothing to do */
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  * __nla_validate - Validate a stream of attributes
0612  * @head: head of attribute stream
0613  * @len: length of attribute stream
0614  * @maxtype: maximum attribute type to be expected
0615  * @policy: validation policy
0616  * @validate: validation strictness
0617  * @extack: extended ACK report struct
0618  *
0619  * Validates all attributes in the specified attribute stream against the
0620  * specified policy. Validation depends on the validate flags passed, see
0621  * &enum netlink_validation for more details on that.
0622  * See documentation of struct nla_policy for more details.
0623  *
0624  * Returns 0 on success or a negative error code.
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  * nla_policy_len - Determine the max. length of a policy
0637  * @policy: policy to use
0638  * @n: number of policies
0639  *
0640  * Determines the max. length of the policy.  It is currently used
0641  * to allocated Netlink buffers roughly the size of the actual
0642  * message.
0643  *
0644  * Returns 0 on success or a negative error code.
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  * __nla_parse - Parse a stream of attributes into a tb buffer
0666  * @tb: destination array with maxtype+1 elements
0667  * @maxtype: maximum attribute type to be expected
0668  * @head: head of attribute stream
0669  * @len: length of attribute stream
0670  * @policy: validation policy
0671  * @validate: validation strictness
0672  * @extack: extended ACK pointer
0673  *
0674  * Parses a stream of attributes and stores a pointer to each attribute in
0675  * the tb array accessible via the attribute type.
0676  * Validation is controlled by the @validate parameter.
0677  *
0678  * Returns 0 on success or a negative error code.
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  * nla_find - Find a specific attribute in a stream of attributes
0692  * @head: head of attribute stream
0693  * @len: length of attribute stream
0694  * @attrtype: type of attribute to look for
0695  *
0696  * Returns the first attribute in the stream matching the specified type.
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  * nla_strscpy - Copy string attribute payload into a sized buffer
0713  * @dst: Where to copy the string to.
0714  * @nla: Attribute to copy the string from.
0715  * @dstsize: Size of destination buffer.
0716  *
0717  * Copies at most dstsize - 1 bytes into the destination buffer.
0718  * Unlike strlcpy the destination buffer is always padded out.
0719  *
0720  * Return:
0721  * * srclen - Returns @nla length (not including the trailing %NUL).
0722  * * -E2BIG - If @dstsize is 0 or greater than U16_MAX or @nla length greater
0723  *            than @dstsize.
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     /* Zero pad end of dst. */
0748     memset(dst + len, 0, dstsize - len);
0749 
0750     return ret;
0751 }
0752 EXPORT_SYMBOL(nla_strscpy);
0753 
0754 /**
0755  * nla_strdup - Copy string attribute payload into a newly allocated buffer
0756  * @nla: attribute to copy the string from
0757  * @flags: the type of memory to allocate (see kmalloc).
0758  *
0759  * Returns a pointer to the allocated buffer or NULL on error.
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  * nla_memcpy - Copy a netlink attribute into another memory area
0780  * @dest: where to copy to memcpy
0781  * @src: netlink attribute to copy from
0782  * @count: size of the destination area
0783  *
0784  * Note: The number of bytes copied is limited by the length of
0785  *       attribute's payload. memcpy
0786  *
0787  * Returns the number of bytes copied.
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  * nla_memcmp - Compare an attribute with sized memory area
0803  * @nla: netlink attribute
0804  * @data: memory area
0805  * @size: size of memory area
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  * nla_strcmp - Compare a string attribute against a string
0821  * @nla: netlink string attribute
0822  * @str: another string
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  * __nla_reserve - reserve room for attribute on the skb
0845  * @skb: socket buffer to reserve room on
0846  * @attrtype: attribute type
0847  * @attrlen: length of attribute payload
0848  *
0849  * Adds a netlink attribute header to a socket buffer and reserves
0850  * room for the payload but does not copy it.
0851  *
0852  * The caller is responsible to ensure that the skb provides enough
0853  * tailroom for the attribute header and payload.
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  * __nla_reserve_64bit - reserve room for attribute on the skb and align it
0871  * @skb: socket buffer to reserve room on
0872  * @attrtype: attribute type
0873  * @attrlen: length of attribute payload
0874  * @padattr: attribute type for the padding
0875  *
0876  * Adds a netlink attribute header to a socket buffer and reserves
0877  * room for the payload but does not copy it. It also ensure that this
0878  * attribute will have a 64-bit aligned nla_data() area.
0879  *
0880  * The caller is responsible to ensure that the skb provides enough
0881  * tailroom for the attribute header and payload.
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  * __nla_reserve_nohdr - reserve room for attribute without header
0894  * @skb: socket buffer to reserve room on
0895  * @attrlen: length of attribute payload
0896  *
0897  * Reserves room for attribute payload without a header.
0898  *
0899  * The caller is responsible to ensure that the skb provides enough
0900  * tailroom for the payload.
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  * nla_reserve - reserve room for attribute on the skb
0910  * @skb: socket buffer to reserve room on
0911  * @attrtype: attribute type
0912  * @attrlen: length of attribute payload
0913  *
0914  * Adds a netlink attribute header to a socket buffer and reserves
0915  * room for the payload but does not copy it.
0916  *
0917  * Returns NULL if the tailroom of the skb is insufficient to store
0918  * the attribute header and payload.
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  * nla_reserve_64bit - reserve room for attribute on the skb and align it
0931  * @skb: socket buffer to reserve room on
0932  * @attrtype: attribute type
0933  * @attrlen: length of attribute payload
0934  * @padattr: attribute type for the padding
0935  *
0936  * Adds a netlink attribute header to a socket buffer and reserves
0937  * room for the payload but does not copy it. It also ensure that this
0938  * attribute will have a 64-bit aligned nla_data() area.
0939  *
0940  * Returns NULL if the tailroom of the skb is insufficient to store
0941  * the attribute header and payload.
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  * nla_reserve_nohdr - reserve room for attribute without header
0961  * @skb: socket buffer to reserve room on
0962  * @attrlen: length of attribute payload
0963  *
0964  * Reserves room for attribute payload without a header.
0965  *
0966  * Returns NULL if the tailroom of the skb is insufficient to store
0967  * the attribute payload.
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  * __nla_put - Add a netlink attribute to a socket buffer
0980  * @skb: socket buffer to add attribute to
0981  * @attrtype: attribute type
0982  * @attrlen: length of attribute payload
0983  * @data: head of attribute payload
0984  *
0985  * The caller is responsible to ensure that the skb provides enough
0986  * tailroom for the attribute header and payload.
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  * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
1000  * @skb: socket buffer to add attribute to
1001  * @attrtype: attribute type
1002  * @attrlen: length of attribute payload
1003  * @data: head of attribute payload
1004  * @padattr: attribute type for the padding
1005  *
1006  * The caller is responsible to ensure that the skb provides enough
1007  * tailroom for the attribute header and payload.
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  * __nla_put_nohdr - Add a netlink attribute without header
1021  * @skb: socket buffer to add attribute to
1022  * @attrlen: length of attribute payload
1023  * @data: head of attribute payload
1024  *
1025  * The caller is responsible to ensure that the skb provides enough
1026  * tailroom for the attribute payload.
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  * nla_put - Add a netlink attribute to a socket buffer
1039  * @skb: socket buffer to add attribute to
1040  * @attrtype: attribute type
1041  * @attrlen: length of attribute payload
1042  * @data: head of attribute payload
1043  *
1044  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1045  * the attribute header and payload.
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  * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
1059  * @skb: socket buffer to add attribute to
1060  * @attrtype: attribute type
1061  * @attrlen: length of attribute payload
1062  * @data: head of attribute payload
1063  * @padattr: attribute type for the padding
1064  *
1065  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1066  * the attribute header and payload.
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  * nla_put_nohdr - Add a netlink attribute without header
1087  * @skb: socket buffer to add attribute to
1088  * @attrlen: length of attribute payload
1089  * @data: head of attribute payload
1090  *
1091  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1092  * the attribute payload.
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  * nla_append - Add a netlink attribute without header or padding
1106  * @skb: socket buffer to add attribute to
1107  * @attrlen: length of attribute payload
1108  * @data: head of attribute payload
1109  *
1110  * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
1111  * the attribute payload.
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