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