Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __NET_NETLINK_H
0003 #define __NET_NETLINK_H
0004 
0005 #include <linux/types.h>
0006 #include <linux/netlink.h>
0007 #include <linux/jiffies.h>
0008 #include <linux/in6.h>
0009 
0010 /* ========================================================================
0011  *         Netlink Messages and Attributes Interface (As Seen On TV)
0012  * ------------------------------------------------------------------------
0013  *                          Messages Interface
0014  * ------------------------------------------------------------------------
0015  *
0016  * Message Format:
0017  *    <--- nlmsg_total_size(payload)  --->
0018  *    <-- nlmsg_msg_size(payload) ->
0019  *   +----------+- - -+-------------+- - -+-------- - -
0020  *   | nlmsghdr | Pad |   Payload   | Pad | nlmsghdr
0021  *   +----------+- - -+-------------+- - -+-------- - -
0022  *   nlmsg_data(nlh)---^                   ^
0023  *   nlmsg_next(nlh)-----------------------+
0024  *
0025  * Payload Format:
0026  *    <---------------------- nlmsg_len(nlh) --------------------->
0027  *    <------ hdrlen ------>       <- nlmsg_attrlen(nlh, hdrlen) ->
0028  *   +----------------------+- - -+--------------------------------+
0029  *   |     Family Header    | Pad |           Attributes           |
0030  *   +----------------------+- - -+--------------------------------+
0031  *   nlmsg_attrdata(nlh, hdrlen)---^
0032  *
0033  * Data Structures:
0034  *   struct nlmsghdr            netlink message header
0035  *
0036  * Message Construction:
0037  *   nlmsg_new()            create a new netlink message
0038  *   nlmsg_put()            add a netlink message to an skb
0039  *   nlmsg_put_answer()         callback based nlmsg_put()
0040  *   nlmsg_end()            finalize netlink message
0041  *   nlmsg_get_pos()            return current position in message
0042  *   nlmsg_trim()           trim part of message
0043  *   nlmsg_cancel()         cancel message construction
0044  *   nlmsg_free()           free a netlink message
0045  *
0046  * Message Sending:
0047  *   nlmsg_multicast()          multicast message to several groups
0048  *   nlmsg_unicast()            unicast a message to a single socket
0049  *   nlmsg_notify()         send notification message
0050  *
0051  * Message Length Calculations:
0052  *   nlmsg_msg_size(payload)        length of message w/o padding
0053  *   nlmsg_total_size(payload)      length of message w/ padding
0054  *   nlmsg_padlen(payload)      length of padding at tail
0055  *
0056  * Message Payload Access:
0057  *   nlmsg_data(nlh)            head of message payload
0058  *   nlmsg_len(nlh)         length of message payload
0059  *   nlmsg_attrdata(nlh, hdrlen)    head of attributes data
0060  *   nlmsg_attrlen(nlh, hdrlen)     length of attributes data
0061  *
0062  * Message Parsing:
0063  *   nlmsg_ok(nlh, remaining)       does nlh fit into remaining bytes?
0064  *   nlmsg_next(nlh, remaining)     get next netlink message
0065  *   nlmsg_parse()          parse attributes of a message
0066  *   nlmsg_find_attr()          find an attribute in a message
0067  *   nlmsg_for_each_msg()       loop over all messages
0068  *   nlmsg_validate()           validate netlink message incl. attrs
0069  *   nlmsg_for_each_attr()      loop over all attributes
0070  *
0071  * Misc:
0072  *   nlmsg_report()         report back to application?
0073  *
0074  * ------------------------------------------------------------------------
0075  *                          Attributes Interface
0076  * ------------------------------------------------------------------------
0077  *
0078  * Attribute Format:
0079  *    <------- nla_total_size(payload) ------->
0080  *    <---- nla_attr_size(payload) ----->
0081  *   +----------+- - -+- - - - - - - - - +- - -+-------- - -
0082  *   |  Header  | Pad |     Payload      | Pad |  Header
0083  *   +----------+- - -+- - - - - - - - - +- - -+-------- - -
0084  *                     <- nla_len(nla) ->      ^
0085  *   nla_data(nla)----^                        |
0086  *   nla_next(nla)-----------------------------'
0087  *
0088  * Data Structures:
0089  *   struct nlattr          netlink attribute header
0090  *
0091  * Attribute Construction:
0092  *   nla_reserve(skb, type, len)    reserve room for an attribute
0093  *   nla_reserve_nohdr(skb, len)    reserve room for an attribute w/o hdr
0094  *   nla_put(skb, type, len, data)  add attribute to skb
0095  *   nla_put_nohdr(skb, len, data)  add attribute w/o hdr
0096  *   nla_append(skb, len, data)     append data to skb
0097  *
0098  * Attribute Construction for Basic Types:
0099  *   nla_put_u8(skb, type, value)   add u8 attribute to skb
0100  *   nla_put_u16(skb, type, value)  add u16 attribute to skb
0101  *   nla_put_u32(skb, type, value)  add u32 attribute to skb
0102  *   nla_put_u64_64bit(skb, type,
0103  *                     value, padattr)  add u64 attribute to skb
0104  *   nla_put_s8(skb, type, value)   add s8 attribute to skb
0105  *   nla_put_s16(skb, type, value)  add s16 attribute to skb
0106  *   nla_put_s32(skb, type, value)  add s32 attribute to skb
0107  *   nla_put_s64(skb, type, value,
0108  *               padattr)       add s64 attribute to skb
0109  *   nla_put_string(skb, type, str) add string attribute to skb
0110  *   nla_put_flag(skb, type)        add flag attribute to skb
0111  *   nla_put_msecs(skb, type, jiffies,
0112  *                 padattr)     add msecs attribute to skb
0113  *   nla_put_in_addr(skb, type, addr)   add IPv4 address attribute to skb
0114  *   nla_put_in6_addr(skb, type, addr)  add IPv6 address attribute to skb
0115  *
0116  * Nested Attributes Construction:
0117  *   nla_nest_start(skb, type)      start a nested attribute
0118  *   nla_nest_end(skb, nla)     finalize a nested attribute
0119  *   nla_nest_cancel(skb, nla)      cancel nested attribute construction
0120  *
0121  * Attribute Length Calculations:
0122  *   nla_attr_size(payload)     length of attribute w/o padding
0123  *   nla_total_size(payload)        length of attribute w/ padding
0124  *   nla_padlen(payload)        length of padding
0125  *
0126  * Attribute Payload Access:
0127  *   nla_data(nla)          head of attribute payload
0128  *   nla_len(nla)           length of attribute payload
0129  *
0130  * Attribute Payload Access for Basic Types:
0131  *   nla_get_u8(nla)            get payload for a u8 attribute
0132  *   nla_get_u16(nla)           get payload for a u16 attribute
0133  *   nla_get_u32(nla)           get payload for a u32 attribute
0134  *   nla_get_u64(nla)           get payload for a u64 attribute
0135  *   nla_get_s8(nla)            get payload for a s8 attribute
0136  *   nla_get_s16(nla)           get payload for a s16 attribute
0137  *   nla_get_s32(nla)           get payload for a s32 attribute
0138  *   nla_get_s64(nla)           get payload for a s64 attribute
0139  *   nla_get_flag(nla)          return 1 if flag is true
0140  *   nla_get_msecs(nla)         get payload for a msecs attribute
0141  *
0142  * Attribute Misc:
0143  *   nla_memcpy(dest, nla, count)   copy attribute into memory
0144  *   nla_memcmp(nla, data, size)    compare attribute with memory area
0145  *   nla_strscpy(dst, nla, size)    copy attribute to a sized string
0146  *   nla_strcmp(nla, str)       compare attribute with string
0147  *
0148  * Attribute Parsing:
0149  *   nla_ok(nla, remaining)     does nla fit into remaining bytes?
0150  *   nla_next(nla, remaining)       get next netlink attribute
0151  *   nla_validate()         validate a stream of attributes
0152  *   nla_validate_nested()      validate a stream of nested attributes
0153  *   nla_find()             find attribute in stream of attributes
0154  *   nla_find_nested()          find attribute in nested attributes
0155  *   nla_parse()            parse and validate stream of attrs
0156  *   nla_parse_nested()         parse nested attributes
0157  *   nla_for_each_attr()        loop over all attributes
0158  *   nla_for_each_nested()      loop over the nested attributes
0159  *=========================================================================
0160  */
0161 
0162  /**
0163   * Standard attribute types to specify validation policy
0164   */
0165 enum {
0166     NLA_UNSPEC,
0167     NLA_U8,
0168     NLA_U16,
0169     NLA_U32,
0170     NLA_U64,
0171     NLA_STRING,
0172     NLA_FLAG,
0173     NLA_MSECS,
0174     NLA_NESTED,
0175     NLA_NESTED_ARRAY,
0176     NLA_NUL_STRING,
0177     NLA_BINARY,
0178     NLA_S8,
0179     NLA_S16,
0180     NLA_S32,
0181     NLA_S64,
0182     NLA_BITFIELD32,
0183     NLA_REJECT,
0184     __NLA_TYPE_MAX,
0185 };
0186 
0187 #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
0188 
0189 struct netlink_range_validation {
0190     u64 min, max;
0191 };
0192 
0193 struct netlink_range_validation_signed {
0194     s64 min, max;
0195 };
0196 
0197 enum nla_policy_validation {
0198     NLA_VALIDATE_NONE,
0199     NLA_VALIDATE_RANGE,
0200     NLA_VALIDATE_RANGE_WARN_TOO_LONG,
0201     NLA_VALIDATE_MIN,
0202     NLA_VALIDATE_MAX,
0203     NLA_VALIDATE_MASK,
0204     NLA_VALIDATE_RANGE_PTR,
0205     NLA_VALIDATE_FUNCTION,
0206 };
0207 
0208 /**
0209  * struct nla_policy - attribute validation policy
0210  * @type: Type of attribute or NLA_UNSPEC
0211  * @validation_type: type of attribute validation done in addition to
0212  *  type-specific validation (e.g. range, function call), see
0213  *  &enum nla_policy_validation
0214  * @len: Type specific length of payload
0215  *
0216  * Policies are defined as arrays of this struct, the array must be
0217  * accessible by attribute type up to the highest identifier to be expected.
0218  *
0219  * Meaning of `len' field:
0220  *    NLA_STRING           Maximum length of string
0221  *    NLA_NUL_STRING       Maximum length of string (excluding NUL)
0222  *    NLA_FLAG             Unused
0223  *    NLA_BINARY           Maximum length of attribute payload
0224  *                         (but see also below with the validation type)
0225  *    NLA_NESTED,
0226  *    NLA_NESTED_ARRAY     Length verification is done by checking len of
0227  *                         nested header (or empty); len field is used if
0228  *                         nested_policy is also used, for the max attr
0229  *                         number in the nested policy.
0230  *    NLA_U8, NLA_U16,
0231  *    NLA_U32, NLA_U64,
0232  *    NLA_S8, NLA_S16,
0233  *    NLA_S32, NLA_S64,
0234  *    NLA_MSECS            Leaving the length field zero will verify the
0235  *                         given type fits, using it verifies minimum length
0236  *                         just like "All other"
0237  *    NLA_BITFIELD32       Unused
0238  *    NLA_REJECT           Unused
0239  *    All other            Minimum length of attribute payload
0240  *
0241  * Meaning of validation union:
0242  *    NLA_BITFIELD32       This is a 32-bit bitmap/bitselector attribute and
0243  *                         `bitfield32_valid' is the u32 value of valid flags
0244  *    NLA_REJECT           This attribute is always rejected and `reject_message'
0245  *                         may point to a string to report as the error instead
0246  *                         of the generic one in extended ACK.
0247  *    NLA_NESTED           `nested_policy' to a nested policy to validate, must
0248  *                         also set `len' to the max attribute number. Use the
0249  *                         provided NLA_POLICY_NESTED() macro.
0250  *                         Note that nla_parse() will validate, but of course not
0251  *                         parse, the nested sub-policies.
0252  *    NLA_NESTED_ARRAY     `nested_policy' points to a nested policy to validate,
0253  *                         must also set `len' to the max attribute number. Use
0254  *                         the provided NLA_POLICY_NESTED_ARRAY() macro.
0255  *                         The difference to NLA_NESTED is the structure:
0256  *                         NLA_NESTED has the nested attributes directly inside
0257  *                         while an array has the nested attributes at another
0258  *                         level down and the attribute types directly in the
0259  *                         nesting don't matter.
0260  *    NLA_U8,
0261  *    NLA_U16,
0262  *    NLA_U32,
0263  *    NLA_U64,
0264  *    NLA_S8,
0265  *    NLA_S16,
0266  *    NLA_S32,
0267  *    NLA_S64              The `min' and `max' fields are used depending on the
0268  *                         validation_type field, if that is min/max/range then
0269  *                         the min, max or both are used (respectively) to check
0270  *                         the value of the integer attribute.
0271  *                         Note that in the interest of code simplicity and
0272  *                         struct size both limits are s16, so you cannot
0273  *                         enforce a range that doesn't fall within the range
0274  *                         of s16 - do that as usual in the code instead.
0275  *                         Use the NLA_POLICY_MIN(), NLA_POLICY_MAX() and
0276  *                         NLA_POLICY_RANGE() macros.
0277  *    NLA_U8,
0278  *    NLA_U16,
0279  *    NLA_U32,
0280  *    NLA_U64              If the validation_type field instead is set to
0281  *                         NLA_VALIDATE_RANGE_PTR, `range' must be a pointer
0282  *                         to a struct netlink_range_validation that indicates
0283  *                         the min/max values.
0284  *                         Use NLA_POLICY_FULL_RANGE().
0285  *    NLA_S8,
0286  *    NLA_S16,
0287  *    NLA_S32,
0288  *    NLA_S64              If the validation_type field instead is set to
0289  *                         NLA_VALIDATE_RANGE_PTR, `range_signed' must be a
0290  *                         pointer to a struct netlink_range_validation_signed
0291  *                         that indicates the min/max values.
0292  *                         Use NLA_POLICY_FULL_RANGE_SIGNED().
0293  *
0294  *    NLA_BINARY           If the validation type is like the ones for integers
0295  *                         above, then the min/max length (not value like for
0296  *                         integers) of the attribute is enforced.
0297  *
0298  *    All other            Unused - but note that it's a union
0299  *
0300  * Meaning of `validate' field, use via NLA_POLICY_VALIDATE_FN:
0301  *    NLA_BINARY           Validation function called for the attribute.
0302  *    All other            Unused - but note that it's a union
0303  *
0304  * Example:
0305  *
0306  * static const u32 myvalidflags = 0xff231023;
0307  *
0308  * static const struct nla_policy my_policy[ATTR_MAX+1] = {
0309  *  [ATTR_FOO] = { .type = NLA_U16 },
0310  *  [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
0311  *  [ATTR_BAZ] = NLA_POLICY_EXACT_LEN(sizeof(struct mystruct)),
0312  *  [ATTR_GOO] = NLA_POLICY_BITFIELD32(myvalidflags),
0313  * };
0314  */
0315 struct nla_policy {
0316     u8      type;
0317     u8      validation_type;
0318     u16     len;
0319     union {
0320         const u32 bitfield32_valid;
0321         const u32 mask;
0322         const char *reject_message;
0323         const struct nla_policy *nested_policy;
0324         struct netlink_range_validation *range;
0325         struct netlink_range_validation_signed *range_signed;
0326         struct {
0327             s16 min, max;
0328         };
0329         int (*validate)(const struct nlattr *attr,
0330                 struct netlink_ext_ack *extack);
0331         /* This entry is special, and used for the attribute at index 0
0332          * only, and specifies special data about the policy, namely it
0333          * specifies the "boundary type" where strict length validation
0334          * starts for any attribute types >= this value, also, strict
0335          * nesting validation starts here.
0336          *
0337          * Additionally, it means that NLA_UNSPEC is actually NLA_REJECT
0338          * for any types >= this, so need to use NLA_POLICY_MIN_LEN() to
0339          * get the previous pure { .len = xyz } behaviour. The advantage
0340          * of this is that types not specified in the policy will be
0341          * rejected.
0342          *
0343          * For completely new families it should be set to 1 so that the
0344          * validation is enforced for all attributes. For existing ones
0345          * it should be set at least when new attributes are added to
0346          * the enum used by the policy, and be set to the new value that
0347          * was added to enforce strict validation from thereon.
0348          */
0349         u16 strict_start_type;
0350     };
0351 };
0352 
0353 #define NLA_POLICY_ETH_ADDR     NLA_POLICY_EXACT_LEN(ETH_ALEN)
0354 #define NLA_POLICY_ETH_ADDR_COMPAT  NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN)
0355 
0356 #define _NLA_POLICY_NESTED(maxattr, policy) \
0357     { .type = NLA_NESTED, .nested_policy = policy, .len = maxattr }
0358 #define _NLA_POLICY_NESTED_ARRAY(maxattr, policy) \
0359     { .type = NLA_NESTED_ARRAY, .nested_policy = policy, .len = maxattr }
0360 #define NLA_POLICY_NESTED(policy) \
0361     _NLA_POLICY_NESTED(ARRAY_SIZE(policy) - 1, policy)
0362 #define NLA_POLICY_NESTED_ARRAY(policy) \
0363     _NLA_POLICY_NESTED_ARRAY(ARRAY_SIZE(policy) - 1, policy)
0364 #define NLA_POLICY_BITFIELD32(valid) \
0365     { .type = NLA_BITFIELD32, .bitfield32_valid = valid }
0366 
0367 #define __NLA_IS_UINT_TYPE(tp)                      \
0368     (tp == NLA_U8 || tp == NLA_U16 || tp == NLA_U32 || tp == NLA_U64)
0369 #define __NLA_IS_SINT_TYPE(tp)                      \
0370     (tp == NLA_S8 || tp == NLA_S16 || tp == NLA_S32 || tp == NLA_S64)
0371 
0372 #define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition))
0373 #define NLA_ENSURE_UINT_TYPE(tp)            \
0374     (__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp)) + tp)
0375 #define NLA_ENSURE_UINT_OR_BINARY_TYPE(tp)      \
0376     (__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) || \
0377               tp == NLA_MSECS ||        \
0378               tp == NLA_BINARY) + tp)
0379 #define NLA_ENSURE_SINT_TYPE(tp)            \
0380     (__NLA_ENSURE(__NLA_IS_SINT_TYPE(tp)) + tp)
0381 #define NLA_ENSURE_INT_OR_BINARY_TYPE(tp)       \
0382     (__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) ||     \
0383               __NLA_IS_SINT_TYPE(tp) ||     \
0384               tp == NLA_MSECS ||        \
0385               tp == NLA_BINARY) + tp)
0386 #define NLA_ENSURE_NO_VALIDATION_PTR(tp)        \
0387     (__NLA_ENSURE(tp != NLA_BITFIELD32 &&       \
0388               tp != NLA_REJECT &&       \
0389               tp != NLA_NESTED &&       \
0390               tp != NLA_NESTED_ARRAY) + tp)
0391 
0392 #define NLA_POLICY_RANGE(tp, _min, _max) {      \
0393     .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp),  \
0394     .validation_type = NLA_VALIDATE_RANGE,      \
0395     .min = _min,                    \
0396     .max = _max                 \
0397 }
0398 
0399 #define NLA_POLICY_FULL_RANGE(tp, _range) {     \
0400     .type = NLA_ENSURE_UINT_OR_BINARY_TYPE(tp), \
0401     .validation_type = NLA_VALIDATE_RANGE_PTR,  \
0402     .range = _range,                \
0403 }
0404 
0405 #define NLA_POLICY_FULL_RANGE_SIGNED(tp, _range) {  \
0406     .type = NLA_ENSURE_SINT_TYPE(tp),       \
0407     .validation_type = NLA_VALIDATE_RANGE_PTR,  \
0408     .range_signed = _range,             \
0409 }
0410 
0411 #define NLA_POLICY_MIN(tp, _min) {          \
0412     .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp),  \
0413     .validation_type = NLA_VALIDATE_MIN,        \
0414     .min = _min,                    \
0415 }
0416 
0417 #define NLA_POLICY_MAX(tp, _max) {          \
0418     .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp),  \
0419     .validation_type = NLA_VALIDATE_MAX,        \
0420     .max = _max,                    \
0421 }
0422 
0423 #define NLA_POLICY_MASK(tp, _mask) {            \
0424     .type = NLA_ENSURE_UINT_TYPE(tp),       \
0425     .validation_type = NLA_VALIDATE_MASK,       \
0426     .mask = _mask,                  \
0427 }
0428 
0429 #define NLA_POLICY_VALIDATE_FN(tp, fn, ...) {       \
0430     .type = NLA_ENSURE_NO_VALIDATION_PTR(tp),   \
0431     .validation_type = NLA_VALIDATE_FUNCTION,   \
0432     .validate = fn,                 \
0433     .len = __VA_ARGS__ + 0,             \
0434 }
0435 
0436 #define NLA_POLICY_EXACT_LEN(_len)  NLA_POLICY_RANGE(NLA_BINARY, _len, _len)
0437 #define NLA_POLICY_EXACT_LEN_WARN(_len) {           \
0438     .type = NLA_BINARY,                 \
0439     .validation_type = NLA_VALIDATE_RANGE_WARN_TOO_LONG,    \
0440     .min = _len,                        \
0441     .max = _len                     \
0442 }
0443 #define NLA_POLICY_MIN_LEN(_len)    NLA_POLICY_MIN(NLA_BINARY, _len)
0444 
0445 /**
0446  * struct nl_info - netlink source information
0447  * @nlh: Netlink message header of original request
0448  * @nl_net: Network namespace
0449  * @portid: Netlink PORTID of requesting application
0450  * @skip_notify: Skip netlink notifications to user space
0451  * @skip_notify_kernel: Skip selected in-kernel notifications
0452  */
0453 struct nl_info {
0454     struct nlmsghdr     *nlh;
0455     struct net      *nl_net;
0456     u32         portid;
0457     u8          skip_notify:1,
0458                 skip_notify_kernel:1;
0459 };
0460 
0461 /**
0462  * enum netlink_validation - netlink message/attribute validation levels
0463  * @NL_VALIDATE_LIBERAL: Old-style "be liberal" validation, not caring about
0464  *  extra data at the end of the message, attributes being longer than
0465  *  they should be, or unknown attributes being present.
0466  * @NL_VALIDATE_TRAILING: Reject junk data encountered after attribute parsing.
0467  * @NL_VALIDATE_MAXTYPE: Reject attributes > max type; Together with _TRAILING
0468  *  this is equivalent to the old nla_parse_strict()/nlmsg_parse_strict().
0469  * @NL_VALIDATE_UNSPEC: Reject attributes with NLA_UNSPEC in the policy.
0470  *  This can safely be set by the kernel when the given policy has no
0471  *  NLA_UNSPEC anymore, and can thus be used to ensure policy entries
0472  *  are enforced going forward.
0473  * @NL_VALIDATE_STRICT_ATTRS: strict attribute policy parsing (e.g.
0474  *  U8, U16, U32 must have exact size, etc.)
0475  * @NL_VALIDATE_NESTED: Check that NLA_F_NESTED is set for NLA_NESTED(_ARRAY)
0476  *  and unset for other policies.
0477  */
0478 enum netlink_validation {
0479     NL_VALIDATE_LIBERAL = 0,
0480     NL_VALIDATE_TRAILING = BIT(0),
0481     NL_VALIDATE_MAXTYPE = BIT(1),
0482     NL_VALIDATE_UNSPEC = BIT(2),
0483     NL_VALIDATE_STRICT_ATTRS = BIT(3),
0484     NL_VALIDATE_NESTED = BIT(4),
0485 };
0486 
0487 #define NL_VALIDATE_DEPRECATED_STRICT (NL_VALIDATE_TRAILING |\
0488                        NL_VALIDATE_MAXTYPE)
0489 #define NL_VALIDATE_STRICT (NL_VALIDATE_TRAILING |\
0490                 NL_VALIDATE_MAXTYPE |\
0491                 NL_VALIDATE_UNSPEC |\
0492                 NL_VALIDATE_STRICT_ATTRS |\
0493                 NL_VALIDATE_NESTED)
0494 
0495 int netlink_rcv_skb(struct sk_buff *skb,
0496             int (*cb)(struct sk_buff *, struct nlmsghdr *,
0497                   struct netlink_ext_ack *));
0498 int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
0499          unsigned int group, int report, gfp_t flags);
0500 
0501 int __nla_validate(const struct nlattr *head, int len, int maxtype,
0502            const struct nla_policy *policy, unsigned int validate,
0503            struct netlink_ext_ack *extack);
0504 int __nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
0505         int len, const struct nla_policy *policy, unsigned int validate,
0506         struct netlink_ext_ack *extack);
0507 int nla_policy_len(const struct nla_policy *, int);
0508 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
0509 ssize_t nla_strscpy(char *dst, const struct nlattr *nla, size_t dstsize);
0510 char *nla_strdup(const struct nlattr *nla, gfp_t flags);
0511 int nla_memcpy(void *dest, const struct nlattr *src, int count);
0512 int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
0513 int nla_strcmp(const struct nlattr *nla, const char *str);
0514 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
0515 struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
0516                    int attrlen, int padattr);
0517 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
0518 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
0519 struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype,
0520                  int attrlen, int padattr);
0521 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
0522 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
0523            const void *data);
0524 void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
0525              const void *data, int padattr);
0526 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
0527 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);
0528 int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
0529           const void *data, int padattr);
0530 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
0531 int nla_append(struct sk_buff *skb, int attrlen, const void *data);
0532 
0533 /**************************************************************************
0534  * Netlink Messages
0535  **************************************************************************/
0536 
0537 /**
0538  * nlmsg_msg_size - length of netlink message not including padding
0539  * @payload: length of message payload
0540  */
0541 static inline int nlmsg_msg_size(int payload)
0542 {
0543     return NLMSG_HDRLEN + payload;
0544 }
0545 
0546 /**
0547  * nlmsg_total_size - length of netlink message including padding
0548  * @payload: length of message payload
0549  */
0550 static inline int nlmsg_total_size(int payload)
0551 {
0552     return NLMSG_ALIGN(nlmsg_msg_size(payload));
0553 }
0554 
0555 /**
0556  * nlmsg_padlen - length of padding at the message's tail
0557  * @payload: length of message payload
0558  */
0559 static inline int nlmsg_padlen(int payload)
0560 {
0561     return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
0562 }
0563 
0564 /**
0565  * nlmsg_data - head of message payload
0566  * @nlh: netlink message header
0567  */
0568 static inline void *nlmsg_data(const struct nlmsghdr *nlh)
0569 {
0570     return (unsigned char *) nlh + NLMSG_HDRLEN;
0571 }
0572 
0573 /**
0574  * nlmsg_len - length of message payload
0575  * @nlh: netlink message header
0576  */
0577 static inline int nlmsg_len(const struct nlmsghdr *nlh)
0578 {
0579     return nlh->nlmsg_len - NLMSG_HDRLEN;
0580 }
0581 
0582 /**
0583  * nlmsg_attrdata - head of attributes data
0584  * @nlh: netlink message header
0585  * @hdrlen: length of family specific header
0586  */
0587 static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
0588                         int hdrlen)
0589 {
0590     unsigned char *data = nlmsg_data(nlh);
0591     return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
0592 }
0593 
0594 /**
0595  * nlmsg_attrlen - length of attributes data
0596  * @nlh: netlink message header
0597  * @hdrlen: length of family specific header
0598  */
0599 static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
0600 {
0601     return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
0602 }
0603 
0604 /**
0605  * nlmsg_ok - check if the netlink message fits into the remaining bytes
0606  * @nlh: netlink message header
0607  * @remaining: number of bytes remaining in message stream
0608  */
0609 static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
0610 {
0611     return (remaining >= (int) sizeof(struct nlmsghdr) &&
0612         nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
0613         nlh->nlmsg_len <= remaining);
0614 }
0615 
0616 /**
0617  * nlmsg_next - next netlink message in message stream
0618  * @nlh: netlink message header
0619  * @remaining: number of bytes remaining in message stream
0620  *
0621  * Returns the next netlink message in the message stream and
0622  * decrements remaining by the size of the current message.
0623  */
0624 static inline struct nlmsghdr *
0625 nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
0626 {
0627     int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
0628 
0629     *remaining -= totlen;
0630 
0631     return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
0632 }
0633 
0634 /**
0635  * nla_parse - Parse a stream of attributes into a tb buffer
0636  * @tb: destination array with maxtype+1 elements
0637  * @maxtype: maximum attribute type to be expected
0638  * @head: head of attribute stream
0639  * @len: length of attribute stream
0640  * @policy: validation policy
0641  * @extack: extended ACK pointer
0642  *
0643  * Parses a stream of attributes and stores a pointer to each attribute in
0644  * the tb array accessible via the attribute type. Attributes with a type
0645  * exceeding maxtype will be rejected, policy must be specified, attributes
0646  * will be validated in the strictest way possible.
0647  *
0648  * Returns 0 on success or a negative error code.
0649  */
0650 static inline int nla_parse(struct nlattr **tb, int maxtype,
0651                 const struct nlattr *head, int len,
0652                 const struct nla_policy *policy,
0653                 struct netlink_ext_ack *extack)
0654 {
0655     return __nla_parse(tb, maxtype, head, len, policy,
0656                NL_VALIDATE_STRICT, extack);
0657 }
0658 
0659 /**
0660  * nla_parse_deprecated - Parse a stream of attributes into a tb buffer
0661  * @tb: destination array with maxtype+1 elements
0662  * @maxtype: maximum attribute type to be expected
0663  * @head: head of attribute stream
0664  * @len: length of attribute stream
0665  * @policy: validation policy
0666  * @extack: extended ACK pointer
0667  *
0668  * Parses a stream of attributes and stores a pointer to each attribute in
0669  * the tb array accessible via the attribute type. Attributes with a type
0670  * exceeding maxtype will be ignored and attributes from the policy are not
0671  * always strictly validated (only for new attributes).
0672  *
0673  * Returns 0 on success or a negative error code.
0674  */
0675 static inline int nla_parse_deprecated(struct nlattr **tb, int maxtype,
0676                        const struct nlattr *head, int len,
0677                        const struct nla_policy *policy,
0678                        struct netlink_ext_ack *extack)
0679 {
0680     return __nla_parse(tb, maxtype, head, len, policy,
0681                NL_VALIDATE_LIBERAL, extack);
0682 }
0683 
0684 /**
0685  * nla_parse_deprecated_strict - Parse a stream of attributes into a tb buffer
0686  * @tb: destination array with maxtype+1 elements
0687  * @maxtype: maximum attribute type to be expected
0688  * @head: head of attribute stream
0689  * @len: length of attribute stream
0690  * @policy: validation policy
0691  * @extack: extended ACK pointer
0692  *
0693  * Parses a stream of attributes and stores a pointer to each attribute in
0694  * the tb array accessible via the attribute type. Attributes with a type
0695  * exceeding maxtype will be rejected as well as trailing data, but the
0696  * policy is not completely strictly validated (only for new attributes).
0697  *
0698  * Returns 0 on success or a negative error code.
0699  */
0700 static inline int nla_parse_deprecated_strict(struct nlattr **tb, int maxtype,
0701                           const struct nlattr *head,
0702                           int len,
0703                           const struct nla_policy *policy,
0704                           struct netlink_ext_ack *extack)
0705 {
0706     return __nla_parse(tb, maxtype, head, len, policy,
0707                NL_VALIDATE_DEPRECATED_STRICT, extack);
0708 }
0709 
0710 /**
0711  * __nlmsg_parse - parse attributes of a netlink message
0712  * @nlh: netlink message header
0713  * @hdrlen: length of family specific header
0714  * @tb: destination array with maxtype+1 elements
0715  * @maxtype: maximum attribute type to be expected
0716  * @policy: validation policy
0717  * @validate: validation strictness
0718  * @extack: extended ACK report struct
0719  *
0720  * See nla_parse()
0721  */
0722 static inline int __nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
0723                 struct nlattr *tb[], int maxtype,
0724                 const struct nla_policy *policy,
0725                 unsigned int validate,
0726                 struct netlink_ext_ack *extack)
0727 {
0728     if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) {
0729         NL_SET_ERR_MSG(extack, "Invalid header length");
0730         return -EINVAL;
0731     }
0732 
0733     return __nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
0734                nlmsg_attrlen(nlh, hdrlen), policy, validate,
0735                extack);
0736 }
0737 
0738 /**
0739  * nlmsg_parse - parse attributes of a netlink message
0740  * @nlh: netlink message header
0741  * @hdrlen: length of family specific header
0742  * @tb: destination array with maxtype+1 elements
0743  * @maxtype: maximum attribute type to be expected
0744  * @extack: extended ACK report struct
0745  *
0746  * See nla_parse()
0747  */
0748 static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
0749                   struct nlattr *tb[], int maxtype,
0750                   const struct nla_policy *policy,
0751                   struct netlink_ext_ack *extack)
0752 {
0753     return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
0754                  NL_VALIDATE_STRICT, extack);
0755 }
0756 
0757 /**
0758  * nlmsg_parse_deprecated - parse attributes of a netlink message
0759  * @nlh: netlink message header
0760  * @hdrlen: length of family specific header
0761  * @tb: destination array with maxtype+1 elements
0762  * @maxtype: maximum attribute type to be expected
0763  * @extack: extended ACK report struct
0764  *
0765  * See nla_parse_deprecated()
0766  */
0767 static inline int nlmsg_parse_deprecated(const struct nlmsghdr *nlh, int hdrlen,
0768                      struct nlattr *tb[], int maxtype,
0769                      const struct nla_policy *policy,
0770                      struct netlink_ext_ack *extack)
0771 {
0772     return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
0773                  NL_VALIDATE_LIBERAL, extack);
0774 }
0775 
0776 /**
0777  * nlmsg_parse_deprecated_strict - parse attributes of a netlink message
0778  * @nlh: netlink message header
0779  * @hdrlen: length of family specific header
0780  * @tb: destination array with maxtype+1 elements
0781  * @maxtype: maximum attribute type to be expected
0782  * @extack: extended ACK report struct
0783  *
0784  * See nla_parse_deprecated_strict()
0785  */
0786 static inline int
0787 nlmsg_parse_deprecated_strict(const struct nlmsghdr *nlh, int hdrlen,
0788                   struct nlattr *tb[], int maxtype,
0789                   const struct nla_policy *policy,
0790                   struct netlink_ext_ack *extack)
0791 {
0792     return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
0793                  NL_VALIDATE_DEPRECATED_STRICT, extack);
0794 }
0795 
0796 /**
0797  * nlmsg_find_attr - find a specific attribute in a netlink message
0798  * @nlh: netlink message header
0799  * @hdrlen: length of familiy specific header
0800  * @attrtype: type of attribute to look for
0801  *
0802  * Returns the first attribute which matches the specified type.
0803  */
0804 static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
0805                          int hdrlen, int attrtype)
0806 {
0807     return nla_find(nlmsg_attrdata(nlh, hdrlen),
0808             nlmsg_attrlen(nlh, hdrlen), attrtype);
0809 }
0810 
0811 /**
0812  * nla_validate_deprecated - Validate a stream of attributes
0813  * @head: head of attribute stream
0814  * @len: length of attribute stream
0815  * @maxtype: maximum attribute type to be expected
0816  * @policy: validation policy
0817  * @validate: validation strictness
0818  * @extack: extended ACK report struct
0819  *
0820  * Validates all attributes in the specified attribute stream against the
0821  * specified policy. Validation is done in liberal mode.
0822  * See documenation of struct nla_policy for more details.
0823  *
0824  * Returns 0 on success or a negative error code.
0825  */
0826 static inline int nla_validate_deprecated(const struct nlattr *head, int len,
0827                       int maxtype,
0828                       const struct nla_policy *policy,
0829                       struct netlink_ext_ack *extack)
0830 {
0831     return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_LIBERAL,
0832                   extack);
0833 }
0834 
0835 /**
0836  * nla_validate - Validate a stream of attributes
0837  * @head: head of attribute stream
0838  * @len: length of attribute stream
0839  * @maxtype: maximum attribute type to be expected
0840  * @policy: validation policy
0841  * @extack: extended ACK report struct
0842  *
0843  * Validates all attributes in the specified attribute stream against the
0844  * specified policy. Validation is done in strict mode.
0845  * See documenation of struct nla_policy for more details.
0846  *
0847  * Returns 0 on success or a negative error code.
0848  */
0849 static inline int nla_validate(const struct nlattr *head, int len, int maxtype,
0850                    const struct nla_policy *policy,
0851                    struct netlink_ext_ack *extack)
0852 {
0853     return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_STRICT,
0854                   extack);
0855 }
0856 
0857 /**
0858  * nlmsg_validate_deprecated - validate a netlink message including attributes
0859  * @nlh: netlinket message header
0860  * @hdrlen: length of familiy specific header
0861  * @maxtype: maximum attribute type to be expected
0862  * @policy: validation policy
0863  * @extack: extended ACK report struct
0864  */
0865 static inline int nlmsg_validate_deprecated(const struct nlmsghdr *nlh,
0866                         int hdrlen, int maxtype,
0867                         const struct nla_policy *policy,
0868                         struct netlink_ext_ack *extack)
0869 {
0870     if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
0871         return -EINVAL;
0872 
0873     return __nla_validate(nlmsg_attrdata(nlh, hdrlen),
0874                   nlmsg_attrlen(nlh, hdrlen), maxtype,
0875                   policy, NL_VALIDATE_LIBERAL, extack);
0876 }
0877 
0878 
0879 
0880 /**
0881  * nlmsg_report - need to report back to application?
0882  * @nlh: netlink message header
0883  *
0884  * Returns 1 if a report back to the application is requested.
0885  */
0886 static inline int nlmsg_report(const struct nlmsghdr *nlh)
0887 {
0888     return nlh ? !!(nlh->nlmsg_flags & NLM_F_ECHO) : 0;
0889 }
0890 
0891 /**
0892  * nlmsg_for_each_attr - iterate over a stream of attributes
0893  * @pos: loop counter, set to current attribute
0894  * @nlh: netlink message header
0895  * @hdrlen: length of familiy specific header
0896  * @rem: initialized to len, holds bytes currently remaining in stream
0897  */
0898 #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
0899     nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
0900               nlmsg_attrlen(nlh, hdrlen), rem)
0901 
0902 /**
0903  * nlmsg_put - Add a new netlink message to an skb
0904  * @skb: socket buffer to store message in
0905  * @portid: netlink PORTID of requesting application
0906  * @seq: sequence number of message
0907  * @type: message type
0908  * @payload: length of message payload
0909  * @flags: message flags
0910  *
0911  * Returns NULL if the tailroom of the skb is insufficient to store
0912  * the message header and payload.
0913  */
0914 static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
0915                      int type, int payload, int flags)
0916 {
0917     if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
0918         return NULL;
0919 
0920     return __nlmsg_put(skb, portid, seq, type, payload, flags);
0921 }
0922 
0923 /**
0924  * nlmsg_put_answer - Add a new callback based netlink message to an skb
0925  * @skb: socket buffer to store message in
0926  * @cb: netlink callback
0927  * @type: message type
0928  * @payload: length of message payload
0929  * @flags: message flags
0930  *
0931  * Returns NULL if the tailroom of the skb is insufficient to store
0932  * the message header and payload.
0933  */
0934 static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
0935                         struct netlink_callback *cb,
0936                         int type, int payload,
0937                         int flags)
0938 {
0939     return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
0940              type, payload, flags);
0941 }
0942 
0943 /**
0944  * nlmsg_new - Allocate a new netlink message
0945  * @payload: size of the message payload
0946  * @flags: the type of memory to allocate.
0947  *
0948  * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
0949  * and a good default is needed.
0950  */
0951 static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
0952 {
0953     return alloc_skb(nlmsg_total_size(payload), flags);
0954 }
0955 
0956 /**
0957  * nlmsg_end - Finalize a netlink message
0958  * @skb: socket buffer the message is stored in
0959  * @nlh: netlink message header
0960  *
0961  * Corrects the netlink message header to include the appeneded
0962  * attributes. Only necessary if attributes have been added to
0963  * the message.
0964  */
0965 static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
0966 {
0967     nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
0968 }
0969 
0970 /**
0971  * nlmsg_get_pos - return current position in netlink message
0972  * @skb: socket buffer the message is stored in
0973  *
0974  * Returns a pointer to the current tail of the message.
0975  */
0976 static inline void *nlmsg_get_pos(struct sk_buff *skb)
0977 {
0978     return skb_tail_pointer(skb);
0979 }
0980 
0981 /**
0982  * nlmsg_trim - Trim message to a mark
0983  * @skb: socket buffer the message is stored in
0984  * @mark: mark to trim to
0985  *
0986  * Trims the message to the provided mark.
0987  */
0988 static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
0989 {
0990     if (mark) {
0991         WARN_ON((unsigned char *) mark < skb->data);
0992         skb_trim(skb, (unsigned char *) mark - skb->data);
0993     }
0994 }
0995 
0996 /**
0997  * nlmsg_cancel - Cancel construction of a netlink message
0998  * @skb: socket buffer the message is stored in
0999  * @nlh: netlink message header
1000  *
1001  * Removes the complete netlink message including all
1002  * attributes from the socket buffer again.
1003  */
1004 static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
1005 {
1006     nlmsg_trim(skb, nlh);
1007 }
1008 
1009 /**
1010  * nlmsg_free - free a netlink message
1011  * @skb: socket buffer of netlink message
1012  */
1013 static inline void nlmsg_free(struct sk_buff *skb)
1014 {
1015     kfree_skb(skb);
1016 }
1017 
1018 /**
1019  * nlmsg_multicast - multicast a netlink message
1020  * @sk: netlink socket to spread messages to
1021  * @skb: netlink message as socket buffer
1022  * @portid: own netlink portid to avoid sending to yourself
1023  * @group: multicast group id
1024  * @flags: allocation flags
1025  */
1026 static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
1027                   u32 portid, unsigned int group, gfp_t flags)
1028 {
1029     int err;
1030 
1031     NETLINK_CB(skb).dst_group = group;
1032 
1033     err = netlink_broadcast(sk, skb, portid, group, flags);
1034     if (err > 0)
1035         err = 0;
1036 
1037     return err;
1038 }
1039 
1040 /**
1041  * nlmsg_unicast - unicast a netlink message
1042  * @sk: netlink socket to spread message to
1043  * @skb: netlink message as socket buffer
1044  * @portid: netlink portid of the destination socket
1045  */
1046 static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
1047 {
1048     int err;
1049 
1050     err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
1051     if (err > 0)
1052         err = 0;
1053 
1054     return err;
1055 }
1056 
1057 /**
1058  * nlmsg_for_each_msg - iterate over a stream of messages
1059  * @pos: loop counter, set to current message
1060  * @head: head of message stream
1061  * @len: length of message stream
1062  * @rem: initialized to len, holds bytes currently remaining in stream
1063  */
1064 #define nlmsg_for_each_msg(pos, head, len, rem) \
1065     for (pos = head, rem = len; \
1066          nlmsg_ok(pos, rem); \
1067          pos = nlmsg_next(pos, &(rem)))
1068 
1069 /**
1070  * nl_dump_check_consistent - check if sequence is consistent and advertise if not
1071  * @cb: netlink callback structure that stores the sequence number
1072  * @nlh: netlink message header to write the flag to
1073  *
1074  * This function checks if the sequence (generation) number changed during dump
1075  * and if it did, advertises it in the netlink message header.
1076  *
1077  * The correct way to use it is to set cb->seq to the generation counter when
1078  * all locks for dumping have been acquired, and then call this function for
1079  * each message that is generated.
1080  *
1081  * Note that due to initialisation concerns, 0 is an invalid sequence number
1082  * and must not be used by code that uses this functionality.
1083  */
1084 static inline void
1085 nl_dump_check_consistent(struct netlink_callback *cb,
1086              struct nlmsghdr *nlh)
1087 {
1088     if (cb->prev_seq && cb->seq != cb->prev_seq)
1089         nlh->nlmsg_flags |= NLM_F_DUMP_INTR;
1090     cb->prev_seq = cb->seq;
1091 }
1092 
1093 /**************************************************************************
1094  * Netlink Attributes
1095  **************************************************************************/
1096 
1097 /**
1098  * nla_attr_size - length of attribute not including padding
1099  * @payload: length of payload
1100  */
1101 static inline int nla_attr_size(int payload)
1102 {
1103     return NLA_HDRLEN + payload;
1104 }
1105 
1106 /**
1107  * nla_total_size - total length of attribute including padding
1108  * @payload: length of payload
1109  */
1110 static inline int nla_total_size(int payload)
1111 {
1112     return NLA_ALIGN(nla_attr_size(payload));
1113 }
1114 
1115 /**
1116  * nla_padlen - length of padding at the tail of attribute
1117  * @payload: length of payload
1118  */
1119 static inline int nla_padlen(int payload)
1120 {
1121     return nla_total_size(payload) - nla_attr_size(payload);
1122 }
1123 
1124 /**
1125  * nla_type - attribute type
1126  * @nla: netlink attribute
1127  */
1128 static inline int nla_type(const struct nlattr *nla)
1129 {
1130     return nla->nla_type & NLA_TYPE_MASK;
1131 }
1132 
1133 /**
1134  * nla_data - head of payload
1135  * @nla: netlink attribute
1136  */
1137 static inline void *nla_data(const struct nlattr *nla)
1138 {
1139     return (char *) nla + NLA_HDRLEN;
1140 }
1141 
1142 /**
1143  * nla_len - length of payload
1144  * @nla: netlink attribute
1145  */
1146 static inline int nla_len(const struct nlattr *nla)
1147 {
1148     return nla->nla_len - NLA_HDRLEN;
1149 }
1150 
1151 /**
1152  * nla_ok - check if the netlink attribute fits into the remaining bytes
1153  * @nla: netlink attribute
1154  * @remaining: number of bytes remaining in attribute stream
1155  */
1156 static inline int nla_ok(const struct nlattr *nla, int remaining)
1157 {
1158     return remaining >= (int) sizeof(*nla) &&
1159            nla->nla_len >= sizeof(*nla) &&
1160            nla->nla_len <= remaining;
1161 }
1162 
1163 /**
1164  * nla_next - next netlink attribute in attribute stream
1165  * @nla: netlink attribute
1166  * @remaining: number of bytes remaining in attribute stream
1167  *
1168  * Returns the next netlink attribute in the attribute stream and
1169  * decrements remaining by the size of the current attribute.
1170  */
1171 static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
1172 {
1173     unsigned int totlen = NLA_ALIGN(nla->nla_len);
1174 
1175     *remaining -= totlen;
1176     return (struct nlattr *) ((char *) nla + totlen);
1177 }
1178 
1179 /**
1180  * nla_find_nested - find attribute in a set of nested attributes
1181  * @nla: attribute containing the nested attributes
1182  * @attrtype: type of attribute to look for
1183  *
1184  * Returns the first attribute which matches the specified type.
1185  */
1186 static inline struct nlattr *
1187 nla_find_nested(const struct nlattr *nla, int attrtype)
1188 {
1189     return nla_find(nla_data(nla), nla_len(nla), attrtype);
1190 }
1191 
1192 /**
1193  * nla_parse_nested - parse nested attributes
1194  * @tb: destination array with maxtype+1 elements
1195  * @maxtype: maximum attribute type to be expected
1196  * @nla: attribute containing the nested attributes
1197  * @policy: validation policy
1198  * @extack: extended ACK report struct
1199  *
1200  * See nla_parse()
1201  */
1202 static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
1203                    const struct nlattr *nla,
1204                    const struct nla_policy *policy,
1205                    struct netlink_ext_ack *extack)
1206 {
1207     if (!(nla->nla_type & NLA_F_NESTED)) {
1208         NL_SET_ERR_MSG_ATTR(extack, nla, "NLA_F_NESTED is missing");
1209         return -EINVAL;
1210     }
1211 
1212     return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
1213                NL_VALIDATE_STRICT, extack);
1214 }
1215 
1216 /**
1217  * nla_parse_nested_deprecated - parse nested attributes
1218  * @tb: destination array with maxtype+1 elements
1219  * @maxtype: maximum attribute type to be expected
1220  * @nla: attribute containing the nested attributes
1221  * @policy: validation policy
1222  * @extack: extended ACK report struct
1223  *
1224  * See nla_parse_deprecated()
1225  */
1226 static inline int nla_parse_nested_deprecated(struct nlattr *tb[], int maxtype,
1227                           const struct nlattr *nla,
1228                           const struct nla_policy *policy,
1229                           struct netlink_ext_ack *extack)
1230 {
1231     return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
1232                NL_VALIDATE_LIBERAL, extack);
1233 }
1234 
1235 /**
1236  * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
1237  * @skb: socket buffer to add attribute to
1238  * @attrtype: attribute type
1239  * @value: numeric value
1240  */
1241 static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
1242 {
1243     /* temporary variables to work around GCC PR81715 with asan-stack=1 */
1244     u8 tmp = value;
1245 
1246     return nla_put(skb, attrtype, sizeof(u8), &tmp);
1247 }
1248 
1249 /**
1250  * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
1251  * @skb: socket buffer to add attribute to
1252  * @attrtype: attribute type
1253  * @value: numeric value
1254  */
1255 static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
1256 {
1257     u16 tmp = value;
1258 
1259     return nla_put(skb, attrtype, sizeof(u16), &tmp);
1260 }
1261 
1262 /**
1263  * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer
1264  * @skb: socket buffer to add attribute to
1265  * @attrtype: attribute type
1266  * @value: numeric value
1267  */
1268 static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
1269 {
1270     __be16 tmp = value;
1271 
1272     return nla_put(skb, attrtype, sizeof(__be16), &tmp);
1273 }
1274 
1275 /**
1276  * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer
1277  * @skb: socket buffer to add attribute to
1278  * @attrtype: attribute type
1279  * @value: numeric value
1280  */
1281 static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
1282 {
1283     __be16 tmp = value;
1284 
1285     return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
1286 }
1287 
1288 /**
1289  * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer
1290  * @skb: socket buffer to add attribute to
1291  * @attrtype: attribute type
1292  * @value: numeric value
1293  */
1294 static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
1295 {
1296     __le16 tmp = value;
1297 
1298     return nla_put(skb, attrtype, sizeof(__le16), &tmp);
1299 }
1300 
1301 /**
1302  * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
1303  * @skb: socket buffer to add attribute to
1304  * @attrtype: attribute type
1305  * @value: numeric value
1306  */
1307 static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
1308 {
1309     u32 tmp = value;
1310 
1311     return nla_put(skb, attrtype, sizeof(u32), &tmp);
1312 }
1313 
1314 /**
1315  * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer
1316  * @skb: socket buffer to add attribute to
1317  * @attrtype: attribute type
1318  * @value: numeric value
1319  */
1320 static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
1321 {
1322     __be32 tmp = value;
1323 
1324     return nla_put(skb, attrtype, sizeof(__be32), &tmp);
1325 }
1326 
1327 /**
1328  * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer
1329  * @skb: socket buffer to add attribute to
1330  * @attrtype: attribute type
1331  * @value: numeric value
1332  */
1333 static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
1334 {
1335     __be32 tmp = value;
1336 
1337     return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
1338 }
1339 
1340 /**
1341  * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer
1342  * @skb: socket buffer to add attribute to
1343  * @attrtype: attribute type
1344  * @value: numeric value
1345  */
1346 static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value)
1347 {
1348     __le32 tmp = value;
1349 
1350     return nla_put(skb, attrtype, sizeof(__le32), &tmp);
1351 }
1352 
1353 /**
1354  * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it
1355  * @skb: socket buffer to add attribute to
1356  * @attrtype: attribute type
1357  * @value: numeric value
1358  * @padattr: attribute type for the padding
1359  */
1360 static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype,
1361                     u64 value, int padattr)
1362 {
1363     u64 tmp = value;
1364 
1365     return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
1366 }
1367 
1368 /**
1369  * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it
1370  * @skb: socket buffer to add attribute to
1371  * @attrtype: attribute type
1372  * @value: numeric value
1373  * @padattr: attribute type for the padding
1374  */
1375 static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value,
1376                    int padattr)
1377 {
1378     __be64 tmp = value;
1379 
1380     return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr);
1381 }
1382 
1383 /**
1384  * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it
1385  * @skb: socket buffer to add attribute to
1386  * @attrtype: attribute type
1387  * @value: numeric value
1388  * @padattr: attribute type for the padding
1389  */
1390 static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value,
1391                 int padattr)
1392 {
1393     __be64 tmp = value;
1394 
1395     return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp,
1396                 padattr);
1397 }
1398 
1399 /**
1400  * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it
1401  * @skb: socket buffer to add attribute to
1402  * @attrtype: attribute type
1403  * @value: numeric value
1404  * @padattr: attribute type for the padding
1405  */
1406 static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value,
1407                    int padattr)
1408 {
1409     __le64 tmp = value;
1410 
1411     return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr);
1412 }
1413 
1414 /**
1415  * nla_put_s8 - Add a s8 netlink attribute to a socket buffer
1416  * @skb: socket buffer to add attribute to
1417  * @attrtype: attribute type
1418  * @value: numeric value
1419  */
1420 static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
1421 {
1422     s8 tmp = value;
1423 
1424     return nla_put(skb, attrtype, sizeof(s8), &tmp);
1425 }
1426 
1427 /**
1428  * nla_put_s16 - Add a s16 netlink attribute to a socket buffer
1429  * @skb: socket buffer to add attribute to
1430  * @attrtype: attribute type
1431  * @value: numeric value
1432  */
1433 static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
1434 {
1435     s16 tmp = value;
1436 
1437     return nla_put(skb, attrtype, sizeof(s16), &tmp);
1438 }
1439 
1440 /**
1441  * nla_put_s32 - Add a s32 netlink attribute to a socket buffer
1442  * @skb: socket buffer to add attribute to
1443  * @attrtype: attribute type
1444  * @value: numeric value
1445  */
1446 static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
1447 {
1448     s32 tmp = value;
1449 
1450     return nla_put(skb, attrtype, sizeof(s32), &tmp);
1451 }
1452 
1453 /**
1454  * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it
1455  * @skb: socket buffer to add attribute to
1456  * @attrtype: attribute type
1457  * @value: numeric value
1458  * @padattr: attribute type for the padding
1459  */
1460 static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value,
1461                   int padattr)
1462 {
1463     s64 tmp = value;
1464 
1465     return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr);
1466 }
1467 
1468 /**
1469  * nla_put_string - Add a string netlink attribute to a socket buffer
1470  * @skb: socket buffer to add attribute to
1471  * @attrtype: attribute type
1472  * @str: NUL terminated string
1473  */
1474 static inline int nla_put_string(struct sk_buff *skb, int attrtype,
1475                  const char *str)
1476 {
1477     return nla_put(skb, attrtype, strlen(str) + 1, str);
1478 }
1479 
1480 /**
1481  * nla_put_flag - Add a flag netlink attribute to a socket buffer
1482  * @skb: socket buffer to add attribute to
1483  * @attrtype: attribute type
1484  */
1485 static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
1486 {
1487     return nla_put(skb, attrtype, 0, NULL);
1488 }
1489 
1490 /**
1491  * nla_put_msecs - Add a msecs netlink attribute to a skb and align it
1492  * @skb: socket buffer to add attribute to
1493  * @attrtype: attribute type
1494  * @njiffies: number of jiffies to convert to msecs
1495  * @padattr: attribute type for the padding
1496  */
1497 static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
1498                 unsigned long njiffies, int padattr)
1499 {
1500     u64 tmp = jiffies_to_msecs(njiffies);
1501 
1502     return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
1503 }
1504 
1505 /**
1506  * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket
1507  * buffer
1508  * @skb: socket buffer to add attribute to
1509  * @attrtype: attribute type
1510  * @addr: IPv4 address
1511  */
1512 static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
1513                   __be32 addr)
1514 {
1515     __be32 tmp = addr;
1516 
1517     return nla_put_be32(skb, attrtype, tmp);
1518 }
1519 
1520 /**
1521  * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket
1522  * buffer
1523  * @skb: socket buffer to add attribute to
1524  * @attrtype: attribute type
1525  * @addr: IPv6 address
1526  */
1527 static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype,
1528                    const struct in6_addr *addr)
1529 {
1530     return nla_put(skb, attrtype, sizeof(*addr), addr);
1531 }
1532 
1533 /**
1534  * nla_put_bitfield32 - Add a bitfield32 netlink attribute to a socket buffer
1535  * @skb: socket buffer to add attribute to
1536  * @attrtype: attribute type
1537  * @value: value carrying bits
1538  * @selector: selector of valid bits
1539  */
1540 static inline int nla_put_bitfield32(struct sk_buff *skb, int attrtype,
1541                      __u32 value, __u32 selector)
1542 {
1543     struct nla_bitfield32 tmp = { value, selector, };
1544 
1545     return nla_put(skb, attrtype, sizeof(tmp), &tmp);
1546 }
1547 
1548 /**
1549  * nla_get_u32 - return payload of u32 attribute
1550  * @nla: u32 netlink attribute
1551  */
1552 static inline u32 nla_get_u32(const struct nlattr *nla)
1553 {
1554     return *(u32 *) nla_data(nla);
1555 }
1556 
1557 /**
1558  * nla_get_be32 - return payload of __be32 attribute
1559  * @nla: __be32 netlink attribute
1560  */
1561 static inline __be32 nla_get_be32(const struct nlattr *nla)
1562 {
1563     return *(__be32 *) nla_data(nla);
1564 }
1565 
1566 /**
1567  * nla_get_le32 - return payload of __le32 attribute
1568  * @nla: __le32 netlink attribute
1569  */
1570 static inline __le32 nla_get_le32(const struct nlattr *nla)
1571 {
1572     return *(__le32 *) nla_data(nla);
1573 }
1574 
1575 /**
1576  * nla_get_u16 - return payload of u16 attribute
1577  * @nla: u16 netlink attribute
1578  */
1579 static inline u16 nla_get_u16(const struct nlattr *nla)
1580 {
1581     return *(u16 *) nla_data(nla);
1582 }
1583 
1584 /**
1585  * nla_get_be16 - return payload of __be16 attribute
1586  * @nla: __be16 netlink attribute
1587  */
1588 static inline __be16 nla_get_be16(const struct nlattr *nla)
1589 {
1590     return *(__be16 *) nla_data(nla);
1591 }
1592 
1593 /**
1594  * nla_get_le16 - return payload of __le16 attribute
1595  * @nla: __le16 netlink attribute
1596  */
1597 static inline __le16 nla_get_le16(const struct nlattr *nla)
1598 {
1599     return *(__le16 *) nla_data(nla);
1600 }
1601 
1602 /**
1603  * nla_get_u8 - return payload of u8 attribute
1604  * @nla: u8 netlink attribute
1605  */
1606 static inline u8 nla_get_u8(const struct nlattr *nla)
1607 {
1608     return *(u8 *) nla_data(nla);
1609 }
1610 
1611 /**
1612  * nla_get_u64 - return payload of u64 attribute
1613  * @nla: u64 netlink attribute
1614  */
1615 static inline u64 nla_get_u64(const struct nlattr *nla)
1616 {
1617     u64 tmp;
1618 
1619     nla_memcpy(&tmp, nla, sizeof(tmp));
1620 
1621     return tmp;
1622 }
1623 
1624 /**
1625  * nla_get_be64 - return payload of __be64 attribute
1626  * @nla: __be64 netlink attribute
1627  */
1628 static inline __be64 nla_get_be64(const struct nlattr *nla)
1629 {
1630     __be64 tmp;
1631 
1632     nla_memcpy(&tmp, nla, sizeof(tmp));
1633 
1634     return tmp;
1635 }
1636 
1637 /**
1638  * nla_get_le64 - return payload of __le64 attribute
1639  * @nla: __le64 netlink attribute
1640  */
1641 static inline __le64 nla_get_le64(const struct nlattr *nla)
1642 {
1643     return *(__le64 *) nla_data(nla);
1644 }
1645 
1646 /**
1647  * nla_get_s32 - return payload of s32 attribute
1648  * @nla: s32 netlink attribute
1649  */
1650 static inline s32 nla_get_s32(const struct nlattr *nla)
1651 {
1652     return *(s32 *) nla_data(nla);
1653 }
1654 
1655 /**
1656  * nla_get_s16 - return payload of s16 attribute
1657  * @nla: s16 netlink attribute
1658  */
1659 static inline s16 nla_get_s16(const struct nlattr *nla)
1660 {
1661     return *(s16 *) nla_data(nla);
1662 }
1663 
1664 /**
1665  * nla_get_s8 - return payload of s8 attribute
1666  * @nla: s8 netlink attribute
1667  */
1668 static inline s8 nla_get_s8(const struct nlattr *nla)
1669 {
1670     return *(s8 *) nla_data(nla);
1671 }
1672 
1673 /**
1674  * nla_get_s64 - return payload of s64 attribute
1675  * @nla: s64 netlink attribute
1676  */
1677 static inline s64 nla_get_s64(const struct nlattr *nla)
1678 {
1679     s64 tmp;
1680 
1681     nla_memcpy(&tmp, nla, sizeof(tmp));
1682 
1683     return tmp;
1684 }
1685 
1686 /**
1687  * nla_get_flag - return payload of flag attribute
1688  * @nla: flag netlink attribute
1689  */
1690 static inline int nla_get_flag(const struct nlattr *nla)
1691 {
1692     return !!nla;
1693 }
1694 
1695 /**
1696  * nla_get_msecs - return payload of msecs attribute
1697  * @nla: msecs netlink attribute
1698  *
1699  * Returns the number of milliseconds in jiffies.
1700  */
1701 static inline unsigned long nla_get_msecs(const struct nlattr *nla)
1702 {
1703     u64 msecs = nla_get_u64(nla);
1704 
1705     return msecs_to_jiffies((unsigned long) msecs);
1706 }
1707 
1708 /**
1709  * nla_get_in_addr - return payload of IPv4 address attribute
1710  * @nla: IPv4 address netlink attribute
1711  */
1712 static inline __be32 nla_get_in_addr(const struct nlattr *nla)
1713 {
1714     return *(__be32 *) nla_data(nla);
1715 }
1716 
1717 /**
1718  * nla_get_in6_addr - return payload of IPv6 address attribute
1719  * @nla: IPv6 address netlink attribute
1720  */
1721 static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla)
1722 {
1723     struct in6_addr tmp;
1724 
1725     nla_memcpy(&tmp, nla, sizeof(tmp));
1726     return tmp;
1727 }
1728 
1729 /**
1730  * nla_get_bitfield32 - return payload of 32 bitfield attribute
1731  * @nla: nla_bitfield32 attribute
1732  */
1733 static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla)
1734 {
1735     struct nla_bitfield32 tmp;
1736 
1737     nla_memcpy(&tmp, nla, sizeof(tmp));
1738     return tmp;
1739 }
1740 
1741 /**
1742  * nla_memdup - duplicate attribute memory (kmemdup)
1743  * @src: netlink attribute to duplicate from
1744  * @gfp: GFP mask
1745  */
1746 static inline void *nla_memdup(const struct nlattr *src, gfp_t gfp)
1747 {
1748     return kmemdup(nla_data(src), nla_len(src), gfp);
1749 }
1750 
1751 /**
1752  * nla_nest_start_noflag - Start a new level of nested attributes
1753  * @skb: socket buffer to add attributes to
1754  * @attrtype: attribute type of container
1755  *
1756  * This function exists for backward compatibility to use in APIs which never
1757  * marked their nest attributes with NLA_F_NESTED flag. New APIs should use
1758  * nla_nest_start() which sets the flag.
1759  *
1760  * Returns the container attribute or NULL on error
1761  */
1762 static inline struct nlattr *nla_nest_start_noflag(struct sk_buff *skb,
1763                            int attrtype)
1764 {
1765     struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
1766 
1767     if (nla_put(skb, attrtype, 0, NULL) < 0)
1768         return NULL;
1769 
1770     return start;
1771 }
1772 
1773 /**
1774  * nla_nest_start - Start a new level of nested attributes, with NLA_F_NESTED
1775  * @skb: socket buffer to add attributes to
1776  * @attrtype: attribute type of container
1777  *
1778  * Unlike nla_nest_start_noflag(), mark the nest attribute with NLA_F_NESTED
1779  * flag. This is the preferred function to use in new code.
1780  *
1781  * Returns the container attribute or NULL on error
1782  */
1783 static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
1784 {
1785     return nla_nest_start_noflag(skb, attrtype | NLA_F_NESTED);
1786 }
1787 
1788 /**
1789  * nla_nest_end - Finalize nesting of attributes
1790  * @skb: socket buffer the attributes are stored in
1791  * @start: container attribute
1792  *
1793  * Corrects the container attribute header to include the all
1794  * appeneded attributes.
1795  *
1796  * Returns the total data length of the skb.
1797  */
1798 static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
1799 {
1800     start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
1801     return skb->len;
1802 }
1803 
1804 /**
1805  * nla_nest_cancel - Cancel nesting of attributes
1806  * @skb: socket buffer the message is stored in
1807  * @start: container attribute
1808  *
1809  * Removes the container attribute and including all nested
1810  * attributes. Returns -EMSGSIZE
1811  */
1812 static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
1813 {
1814     nlmsg_trim(skb, start);
1815 }
1816 
1817 /**
1818  * __nla_validate_nested - Validate a stream of nested attributes
1819  * @start: container attribute
1820  * @maxtype: maximum attribute type to be expected
1821  * @policy: validation policy
1822  * @validate: validation strictness
1823  * @extack: extended ACK report struct
1824  *
1825  * Validates all attributes in the nested attribute stream against the
1826  * specified policy. Attributes with a type exceeding maxtype will be
1827  * ignored. See documenation of struct nla_policy for more details.
1828  *
1829  * Returns 0 on success or a negative error code.
1830  */
1831 static inline int __nla_validate_nested(const struct nlattr *start, int maxtype,
1832                     const struct nla_policy *policy,
1833                     unsigned int validate,
1834                     struct netlink_ext_ack *extack)
1835 {
1836     return __nla_validate(nla_data(start), nla_len(start), maxtype, policy,
1837                   validate, extack);
1838 }
1839 
1840 static inline int
1841 nla_validate_nested(const struct nlattr *start, int maxtype,
1842             const struct nla_policy *policy,
1843             struct netlink_ext_ack *extack)
1844 {
1845     return __nla_validate_nested(start, maxtype, policy,
1846                      NL_VALIDATE_STRICT, extack);
1847 }
1848 
1849 static inline int
1850 nla_validate_nested_deprecated(const struct nlattr *start, int maxtype,
1851                    const struct nla_policy *policy,
1852                    struct netlink_ext_ack *extack)
1853 {
1854     return __nla_validate_nested(start, maxtype, policy,
1855                      NL_VALIDATE_LIBERAL, extack);
1856 }
1857 
1858 /**
1859  * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute
1860  * @skb: socket buffer the message is stored in
1861  *
1862  * Return true if padding is needed to align the next attribute (nla_data()) to
1863  * a 64-bit aligned area.
1864  */
1865 static inline bool nla_need_padding_for_64bit(struct sk_buff *skb)
1866 {
1867 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1868     /* The nlattr header is 4 bytes in size, that's why we test
1869      * if the skb->data _is_ aligned.  A NOP attribute, plus
1870      * nlattr header for next attribute, will make nla_data()
1871      * 8-byte aligned.
1872      */
1873     if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
1874         return true;
1875 #endif
1876     return false;
1877 }
1878 
1879 /**
1880  * nla_align_64bit - 64-bit align the nla_data() of next attribute
1881  * @skb: socket buffer the message is stored in
1882  * @padattr: attribute type for the padding
1883  *
1884  * Conditionally emit a padding netlink attribute in order to make
1885  * the next attribute we emit have a 64-bit aligned nla_data() area.
1886  * This will only be done in architectures which do not have
1887  * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
1888  *
1889  * Returns zero on success or a negative error code.
1890  */
1891 static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
1892 {
1893     if (nla_need_padding_for_64bit(skb) &&
1894         !nla_reserve(skb, padattr, 0))
1895         return -EMSGSIZE;
1896 
1897     return 0;
1898 }
1899 
1900 /**
1901  * nla_total_size_64bit - total length of attribute including padding
1902  * @payload: length of payload
1903  */
1904 static inline int nla_total_size_64bit(int payload)
1905 {
1906     return NLA_ALIGN(nla_attr_size(payload))
1907 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1908         + NLA_ALIGN(nla_attr_size(0))
1909 #endif
1910         ;
1911 }
1912 
1913 /**
1914  * nla_for_each_attr - iterate over a stream of attributes
1915  * @pos: loop counter, set to current attribute
1916  * @head: head of attribute stream
1917  * @len: length of attribute stream
1918  * @rem: initialized to len, holds bytes currently remaining in stream
1919  */
1920 #define nla_for_each_attr(pos, head, len, rem) \
1921     for (pos = head, rem = len; \
1922          nla_ok(pos, rem); \
1923          pos = nla_next(pos, &(rem)))
1924 
1925 /**
1926  * nla_for_each_nested - iterate over nested attributes
1927  * @pos: loop counter, set to current attribute
1928  * @nla: attribute containing the nested attributes
1929  * @rem: initialized to len, holds bytes currently remaining in stream
1930  */
1931 #define nla_for_each_nested(pos, nla, rem) \
1932     nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
1933 
1934 /**
1935  * nla_is_last - Test if attribute is last in stream
1936  * @nla: attribute to test
1937  * @rem: bytes remaining in stream
1938  */
1939 static inline bool nla_is_last(const struct nlattr *nla, int rem)
1940 {
1941     return nla->nla_len == rem;
1942 }
1943 
1944 void nla_get_range_unsigned(const struct nla_policy *pt,
1945                 struct netlink_range_validation *range);
1946 void nla_get_range_signed(const struct nla_policy *pt,
1947               struct netlink_range_validation_signed *range);
1948 
1949 struct netlink_policy_dump_state;
1950 
1951 int netlink_policy_dump_add_policy(struct netlink_policy_dump_state **pstate,
1952                    const struct nla_policy *policy,
1953                    unsigned int maxtype);
1954 int netlink_policy_dump_get_policy_idx(struct netlink_policy_dump_state *state,
1955                        const struct nla_policy *policy,
1956                        unsigned int maxtype);
1957 bool netlink_policy_dump_loop(struct netlink_policy_dump_state *state);
1958 int netlink_policy_dump_write(struct sk_buff *skb,
1959                   struct netlink_policy_dump_state *state);
1960 int netlink_policy_dump_attr_size_estimate(const struct nla_policy *pt);
1961 int netlink_policy_dump_write_attr(struct sk_buff *skb,
1962                    const struct nla_policy *pt,
1963                    int nestattr);
1964 void netlink_policy_dump_free(struct netlink_policy_dump_state *state);
1965 
1966 #endif