1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_NETLINK_H
3 #define __NET_NETLINK_H
5 #include <linux/types.h>
6 #include <linux/netlink.h>
7 #include <linux/jiffies.h>
10 /* ========================================================================
11 * Netlink Messages and Attributes Interface (As Seen On TV)
12 * ------------------------------------------------------------------------
14 * ------------------------------------------------------------------------
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)-----------------------+
26 * <---------------------- nlmsg_len(nlh) --------------------->
27 * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) ->
28 * +----------------------+- - -+--------------------------------+
29 * | Family Header | Pad | Attributes |
30 * +----------------------+- - -+--------------------------------+
31 * nlmsg_attrdata(nlh, hdrlen)---^
34 * struct nlmsghdr netlink message header
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_consume() free a netlink message (expected)
45 * nlmsg_free() free a netlink message (drop)
48 * nlmsg_multicast() multicast message to several groups
49 * nlmsg_unicast() unicast a message to a single socket
50 * nlmsg_notify() send notification message
52 * Message Length Calculations:
53 * nlmsg_msg_size(payload) length of message w/o padding
54 * nlmsg_total_size(payload) length of message w/ padding
55 * nlmsg_padlen(payload) length of padding at tail
57 * Message Payload Access:
58 * nlmsg_data(nlh) head of message payload
59 * nlmsg_len(nlh) length of message payload
60 * nlmsg_attrdata(nlh, hdrlen) head of attributes data
61 * nlmsg_attrlen(nlh, hdrlen) length of attributes data
64 * nlmsg_ok(nlh, remaining) does nlh fit into remaining bytes?
65 * nlmsg_next(nlh, remaining) get next netlink message
66 * nlmsg_parse() parse attributes of a message
67 * nlmsg_find_attr() find an attribute in a message
68 * nlmsg_for_each_msg() loop over all messages
69 * nlmsg_validate() validate netlink message incl. attrs
70 * nlmsg_for_each_attr() loop over all attributes
73 * nlmsg_report() report back to application?
75 * ------------------------------------------------------------------------
76 * Attributes Interface
77 * ------------------------------------------------------------------------
80 * <------- nla_total_size(payload) ------->
81 * <---- nla_attr_size(payload) ----->
82 * +----------+- - -+- - - - - - - - - +- - -+-------- - -
83 * | Header | Pad | Payload | Pad | Header
84 * +----------+- - -+- - - - - - - - - +- - -+-------- - -
85 * <- nla_len(nla) -> ^
86 * nla_data(nla)----^ |
87 * nla_next(nla)-----------------------------'
90 * struct nlattr netlink attribute header
92 * Attribute Construction:
93 * nla_reserve(skb, type, len) reserve room for an attribute
94 * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr
95 * nla_put(skb, type, len, data) add attribute to skb
96 * nla_put_nohdr(skb, len, data) add attribute w/o hdr
97 * nla_append(skb, len, data) append data to skb
99 * Attribute Construction for Basic Types:
100 * nla_put_u8(skb, type, value) add u8 attribute to skb
101 * nla_put_u16(skb, type, value) add u16 attribute to skb
102 * nla_put_u32(skb, type, value) add u32 attribute to skb
103 * nla_put_u64_64bit(skb, type,
104 * value, padattr) add u64 attribute to skb
105 * nla_put_s8(skb, type, value) add s8 attribute to skb
106 * nla_put_s16(skb, type, value) add s16 attribute to skb
107 * nla_put_s32(skb, type, value) add s32 attribute to skb
108 * nla_put_s64(skb, type, value,
109 * padattr) add s64 attribute to skb
110 * nla_put_string(skb, type, str) add string attribute to skb
111 * nla_put_flag(skb, type) add flag attribute to skb
112 * nla_put_msecs(skb, type, jiffies,
113 * padattr) add msecs attribute to skb
114 * nla_put_in_addr(skb, type, addr) add IPv4 address attribute to skb
115 * nla_put_in6_addr(skb, type, addr) add IPv6 address attribute to skb
117 * Nested Attributes Construction:
118 * nla_nest_start(skb, type) start a nested attribute
119 * nla_nest_end(skb, nla) finalize a nested attribute
120 * nla_nest_cancel(skb, nla) cancel nested attribute construction
121 * nla_put_empty_nest(skb, type) create an empty nest
123 * Attribute Length Calculations:
124 * nla_attr_size(payload) length of attribute w/o padding
125 * nla_total_size(payload) length of attribute w/ padding
126 * nla_padlen(payload) length of padding
128 * Attribute Payload Access:
129 * nla_data(nla) head of attribute payload
130 * nla_len(nla) length of attribute payload
132 * Attribute Payload Access for Basic Types:
133 * nla_get_uint(nla) get payload for a uint attribute
134 * nla_get_sint(nla) get payload for a sint attribute
135 * nla_get_u8(nla) get payload for a u8 attribute
136 * nla_get_u16(nla) get payload for a u16 attribute
137 * nla_get_u32(nla) get payload for a u32 attribute
138 * nla_get_u64(nla) get payload for a u64 attribute
139 * nla_get_s8(nla) get payload for a s8 attribute
140 * nla_get_s16(nla) get payload for a s16 attribute
141 * nla_get_s32(nla) get payload for a s32 attribute
142 * nla_get_s64(nla) get payload for a s64 attribute
143 * nla_get_flag(nla) return 1 if flag is true
144 * nla_get_msecs(nla) get payload for a msecs attribute
146 * The same functions also exist with _default().
149 * nla_memcpy(dest, nla, count) copy attribute into memory
150 * nla_memcmp(nla, data, size) compare attribute with memory area
151 * nla_strscpy(dst, nla, size) copy attribute to a sized string
152 * nla_strcmp(nla, str) compare attribute with string
155 * nla_ok(nla, remaining) does nla fit into remaining bytes?
156 * nla_next(nla, remaining) get next netlink attribute
157 * nla_validate() validate a stream of attributes
158 * nla_validate_nested() validate a stream of nested attributes
159 * nla_find() find attribute in stream of attributes
160 * nla_find_nested() find attribute in nested attributes
161 * nla_parse() parse and validate stream of attrs
162 * nla_parse_nested() parse nested attributes
163 * nla_for_each_attr() loop over all attributes
164 * nla_for_each_attr_type() loop over all attributes with the
166 * nla_for_each_nested() loop over the nested attributes
167 * nla_for_each_nested_type() loop over the nested attributes with
169 *=========================================================================
173 * Standard attribute types to specify validation policy
201 #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
203 struct netlink_range_validation {
207 struct netlink_range_validation_signed {
211 enum nla_policy_validation {
214 NLA_VALIDATE_RANGE_WARN_TOO_LONG,
218 NLA_VALIDATE_RANGE_PTR,
219 NLA_VALIDATE_FUNCTION,
223 * struct nla_policy - attribute validation policy
224 * @type: Type of attribute or NLA_UNSPEC
225 * @validation_type: type of attribute validation done in addition to
226 * type-specific validation (e.g. range, function call), see
227 * &enum nla_policy_validation
228 * @len: Type specific length of payload
230 * Policies are defined as arrays of this struct, the array must be
231 * accessible by attribute type up to the highest identifier to be expected.
233 * Meaning of `len' field:
234 * NLA_STRING Maximum length of string
235 * NLA_NUL_STRING Maximum length of string (excluding NUL)
237 * NLA_BINARY Maximum length of attribute payload
238 * (but see also below with the validation type)
240 * NLA_NESTED_ARRAY Length verification is done by checking len of
241 * nested header (or empty); len field is used if
242 * nested_policy is also used, for the max attr
243 * number in the nested policy.
244 * NLA_SINT, NLA_UINT,
249 * NLA_BE16, NLA_BE32,
250 * NLA_MSECS Leaving the length field zero will verify the
251 * given type fits, using it verifies minimum length
252 * just like "All other"
253 * NLA_BITFIELD32 Unused
255 * All other Minimum length of attribute payload
257 * Meaning of validation union:
258 * NLA_BITFIELD32 This is a 32-bit bitmap/bitselector attribute and
259 * `bitfield32_valid' is the u32 value of valid flags
260 * NLA_REJECT This attribute is always rejected and `reject_message'
261 * may point to a string to report as the error instead
262 * of the generic one in extended ACK.
263 * NLA_NESTED `nested_policy' to a nested policy to validate, must
264 * also set `len' to the max attribute number. Use the
265 * provided NLA_POLICY_NESTED() macro.
266 * Note that nla_parse() will validate, but of course not
267 * parse, the nested sub-policies.
268 * NLA_NESTED_ARRAY `nested_policy' points to a nested policy to validate,
269 * must also set `len' to the max attribute number. Use
270 * the provided NLA_POLICY_NESTED_ARRAY() macro.
271 * The difference to NLA_NESTED is the structure:
272 * NLA_NESTED has the nested attributes directly inside
273 * while an array has the nested attributes at another
274 * level down and the attribute types directly in the
275 * nesting don't matter.
287 * NLA_S64 The `min' and `max' fields are used depending on the
288 * validation_type field, if that is min/max/range then
289 * the min, max or both are used (respectively) to check
290 * the value of the integer attribute.
291 * Note that in the interest of code simplicity and
292 * struct size both limits are s16, so you cannot
293 * enforce a range that doesn't fall within the range
294 * of s16 - do that using the NLA_POLICY_FULL_RANGE()
295 * or NLA_POLICY_FULL_RANGE_SIGNED() macros instead.
296 * Use the NLA_POLICY_MIN(), NLA_POLICY_MAX() and
297 * NLA_POLICY_RANGE() macros.
302 * NLA_U64 If the validation_type field instead is set to
303 * NLA_VALIDATE_RANGE_PTR, `range' must be a pointer
304 * to a struct netlink_range_validation that indicates
305 * the min/max values.
306 * Use NLA_POLICY_FULL_RANGE().
311 * NLA_S64 If the validation_type field instead is set to
312 * NLA_VALIDATE_RANGE_PTR, `range_signed' must be a
313 * pointer to a struct netlink_range_validation_signed
314 * that indicates the min/max values.
315 * Use NLA_POLICY_FULL_RANGE_SIGNED().
317 * NLA_BINARY If the validation type is like the ones for integers
318 * above, then the min/max length (not value like for
319 * integers) of the attribute is enforced.
321 * All other Unused - but note that it's a union
323 * Meaning of `validate' field, use via NLA_POLICY_VALIDATE_FN:
329 * NLA_BINARY Validation function called for the attribute.
331 * All other Unused - but note that it's a union
335 * static const u32 myvalidflags = 0xff231023;
337 * static const struct nla_policy my_policy[ATTR_MAX+1] = {
338 * [ATTR_FOO] = { .type = NLA_U16 },
339 * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
340 * [ATTR_BAZ] = NLA_POLICY_EXACT_LEN(sizeof(struct mystruct)),
341 * [ATTR_GOO] = NLA_POLICY_BITFIELD32(myvalidflags),
350 * @strict_start_type: first attribute to validate strictly
352 * This entry is special, and used for the attribute at index 0
353 * only, and specifies special data about the policy, namely it
354 * specifies the "boundary type" where strict length validation
355 * starts for any attribute types >= this value, also, strict
356 * nesting validation starts here.
358 * Additionally, it means that NLA_UNSPEC is actually NLA_REJECT
359 * for any types >= this, so need to use NLA_POLICY_MIN_LEN() to
360 * get the previous pure { .len = xyz } behaviour. The advantage
361 * of this is that types not specified in the policy will be
364 * For completely new families it should be set to 1 so that the
365 * validation is enforced for all attributes. For existing ones
366 * it should be set at least when new attributes are added to
367 * the enum used by the policy, and be set to the new value that
368 * was added to enforce strict validation from thereon.
370 u16 strict_start_type;
372 /* private: use NLA_POLICY_*() to set */
373 const u32 bitfield32_valid;
375 const char *reject_message;
376 const struct nla_policy *nested_policy;
377 const struct netlink_range_validation *range;
378 const struct netlink_range_validation_signed *range_signed;
382 int (*validate)(const struct nlattr *attr,
383 struct netlink_ext_ack *extack);
387 #define NLA_POLICY_ETH_ADDR NLA_POLICY_EXACT_LEN(ETH_ALEN)
388 #define NLA_POLICY_ETH_ADDR_COMPAT NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN)
390 #define _NLA_POLICY_NESTED(maxattr, policy) \
391 { .type = NLA_NESTED, .nested_policy = policy, .len = maxattr }
392 #define _NLA_POLICY_NESTED_ARRAY(maxattr, policy) \
393 { .type = NLA_NESTED_ARRAY, .nested_policy = policy, .len = maxattr }
394 #define NLA_POLICY_NESTED(policy) \
395 _NLA_POLICY_NESTED(ARRAY_SIZE(policy) - 1, policy)
396 #define NLA_POLICY_NESTED_ARRAY(policy) \
397 _NLA_POLICY_NESTED_ARRAY(ARRAY_SIZE(policy) - 1, policy)
398 #define NLA_POLICY_BITFIELD32(valid) \
399 { .type = NLA_BITFIELD32, .bitfield32_valid = valid }
401 #define __NLA_IS_UINT_TYPE(tp) \
402 (tp == NLA_U8 || tp == NLA_U16 || tp == NLA_U32 || \
403 tp == NLA_U64 || tp == NLA_UINT || \
404 tp == NLA_BE16 || tp == NLA_BE32)
405 #define __NLA_IS_SINT_TYPE(tp) \
406 (tp == NLA_S8 || tp == NLA_S16 || tp == NLA_S32 || tp == NLA_S64 || \
409 #define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition))
410 #define NLA_ENSURE_UINT_TYPE(tp) \
411 (__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp)) + tp)
412 #define NLA_ENSURE_UINT_OR_BINARY_TYPE(tp) \
413 (__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) || \
415 tp == NLA_BINARY) + tp)
416 #define NLA_ENSURE_SINT_TYPE(tp) \
417 (__NLA_ENSURE(__NLA_IS_SINT_TYPE(tp)) + tp)
418 #define NLA_ENSURE_INT_OR_BINARY_TYPE(tp) \
419 (__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) || \
420 __NLA_IS_SINT_TYPE(tp) || \
422 tp == NLA_BINARY) + tp)
423 #define NLA_ENSURE_NO_VALIDATION_PTR(tp) \
424 (__NLA_ENSURE(tp != NLA_BITFIELD32 && \
425 tp != NLA_REJECT && \
426 tp != NLA_NESTED && \
427 tp != NLA_NESTED_ARRAY) + tp)
429 #define NLA_POLICY_RANGE(tp, _min, _max) { \
430 .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp), \
431 .validation_type = NLA_VALIDATE_RANGE, \
436 #define NLA_POLICY_FULL_RANGE(tp, _range) { \
437 .type = NLA_ENSURE_UINT_OR_BINARY_TYPE(tp), \
438 .validation_type = NLA_VALIDATE_RANGE_PTR, \
442 #define NLA_POLICY_FULL_RANGE_SIGNED(tp, _range) { \
443 .type = NLA_ENSURE_SINT_TYPE(tp), \
444 .validation_type = NLA_VALIDATE_RANGE_PTR, \
445 .range_signed = _range, \
448 #define NLA_POLICY_MIN(tp, _min) { \
449 .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp), \
450 .validation_type = NLA_VALIDATE_MIN, \
454 #define NLA_POLICY_MAX(tp, _max) { \
455 .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp), \
456 .validation_type = NLA_VALIDATE_MAX, \
460 #define NLA_POLICY_MASK(tp, _mask) { \
461 .type = NLA_ENSURE_UINT_TYPE(tp), \
462 .validation_type = NLA_VALIDATE_MASK, \
466 #define NLA_POLICY_VALIDATE_FN(tp, fn, ...) { \
467 .type = NLA_ENSURE_NO_VALIDATION_PTR(tp), \
468 .validation_type = NLA_VALIDATE_FUNCTION, \
470 .len = __VA_ARGS__ + 0, \
473 #define NLA_POLICY_EXACT_LEN(_len) NLA_POLICY_RANGE(NLA_BINARY, _len, _len)
474 #define NLA_POLICY_EXACT_LEN_WARN(_len) { \
475 .type = NLA_BINARY, \
476 .validation_type = NLA_VALIDATE_RANGE_WARN_TOO_LONG, \
480 #define NLA_POLICY_MIN_LEN(_len) NLA_POLICY_MIN(NLA_BINARY, _len)
481 #define NLA_POLICY_MAX_LEN(_len) NLA_POLICY_MAX(NLA_BINARY, _len)
484 * struct nl_info - netlink source information
485 * @nlh: Netlink message header of original request
486 * @nl_net: Network namespace
487 * @portid: Netlink PORTID of requesting application
488 * @skip_notify: Skip netlink notifications to user space
489 * @skip_notify_kernel: Skip selected in-kernel notifications
492 struct nlmsghdr *nlh;
496 skip_notify_kernel:1;
500 * enum netlink_validation - netlink message/attribute validation levels
501 * @NL_VALIDATE_LIBERAL: Old-style "be liberal" validation, not caring about
502 * extra data at the end of the message, attributes being longer than
503 * they should be, or unknown attributes being present.
504 * @NL_VALIDATE_TRAILING: Reject junk data encountered after attribute parsing.
505 * @NL_VALIDATE_MAXTYPE: Reject attributes > max type; Together with _TRAILING
506 * this is equivalent to the old nla_parse_strict()/nlmsg_parse_strict().
507 * @NL_VALIDATE_UNSPEC: Reject attributes with NLA_UNSPEC in the policy.
508 * This can safely be set by the kernel when the given policy has no
509 * NLA_UNSPEC anymore, and can thus be used to ensure policy entries
510 * are enforced going forward.
511 * @NL_VALIDATE_STRICT_ATTRS: strict attribute policy parsing (e.g.
512 * U8, U16, U32 must have exact size, etc.)
513 * @NL_VALIDATE_NESTED: Check that NLA_F_NESTED is set for NLA_NESTED(_ARRAY)
514 * and unset for other policies.
516 enum netlink_validation {
517 NL_VALIDATE_LIBERAL = 0,
518 NL_VALIDATE_TRAILING = BIT(0),
519 NL_VALIDATE_MAXTYPE = BIT(1),
520 NL_VALIDATE_UNSPEC = BIT(2),
521 NL_VALIDATE_STRICT_ATTRS = BIT(3),
522 NL_VALIDATE_NESTED = BIT(4),
525 #define NL_VALIDATE_DEPRECATED_STRICT (NL_VALIDATE_TRAILING |\
527 #define NL_VALIDATE_STRICT (NL_VALIDATE_TRAILING |\
528 NL_VALIDATE_MAXTYPE |\
529 NL_VALIDATE_UNSPEC |\
530 NL_VALIDATE_STRICT_ATTRS |\
533 int netlink_rcv_skb(struct sk_buff *skb,
534 int (*cb)(struct sk_buff *, struct nlmsghdr *,
535 struct netlink_ext_ack *));
536 int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
537 unsigned int group, int report, gfp_t flags);
539 int __nla_validate(const struct nlattr *head, int len, int maxtype,
540 const struct nla_policy *policy, unsigned int validate,
541 struct netlink_ext_ack *extack);
542 int __nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
543 int len, const struct nla_policy *policy, unsigned int validate,
544 struct netlink_ext_ack *extack);
545 int nla_policy_len(const struct nla_policy *, int);
546 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
547 ssize_t nla_strscpy(char *dst, const struct nlattr *nla, size_t dstsize);
548 char *nla_strdup(const struct nlattr *nla, gfp_t flags);
549 int nla_memcpy(void *dest, const struct nlattr *src, int count);
550 int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
551 int nla_strcmp(const struct nlattr *nla, const char *str);
552 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
553 struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
554 int attrlen, int padattr);
555 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
556 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
557 struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype,
558 int attrlen, int padattr);
559 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
560 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
562 void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
563 const void *data, int padattr);
564 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
565 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);
566 int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
567 const void *data, int padattr);
568 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
569 int nla_append(struct sk_buff *skb, int attrlen, const void *data);
571 /**************************************************************************
573 **************************************************************************/
576 * nlmsg_msg_size - length of netlink message not including padding
577 * @payload: length of message payload
579 static inline int nlmsg_msg_size(int payload)
581 return NLMSG_HDRLEN + payload;
585 * nlmsg_total_size - length of netlink message including padding
586 * @payload: length of message payload
588 static inline int nlmsg_total_size(int payload)
590 return NLMSG_ALIGN(nlmsg_msg_size(payload));
594 * nlmsg_padlen - length of padding at the message's tail
595 * @payload: length of message payload
597 static inline int nlmsg_padlen(int payload)
599 return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
603 * nlmsg_data - head of message payload
604 * @nlh: netlink message header
606 static inline void *nlmsg_data(const struct nlmsghdr *nlh)
608 return (unsigned char *) nlh + NLMSG_HDRLEN;
612 * nlmsg_len - length of message payload
613 * @nlh: netlink message header
615 static inline int nlmsg_len(const struct nlmsghdr *nlh)
617 return nlh->nlmsg_len - NLMSG_HDRLEN;
621 * nlmsg_payload - message payload if the data fits in the len
622 * @nlh: netlink message header
623 * @len: struct length
625 * Returns: The netlink message payload/data if the length is sufficient,
628 static inline void *nlmsg_payload(const struct nlmsghdr *nlh, size_t len)
630 if (nlh->nlmsg_len < nlmsg_msg_size(len))
633 return nlmsg_data(nlh);
637 * nlmsg_attrdata - head of attributes data
638 * @nlh: netlink message header
639 * @hdrlen: length of family specific header
641 static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
644 unsigned char *data = nlmsg_data(nlh);
645 return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
649 * nlmsg_attrlen - length of attributes data
650 * @nlh: netlink message header
651 * @hdrlen: length of family specific header
653 static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
655 return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
659 * nlmsg_ok - check if the netlink message fits into the remaining bytes
660 * @nlh: netlink message header
661 * @remaining: number of bytes remaining in message stream
663 static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
665 return (remaining >= (int) sizeof(struct nlmsghdr) &&
666 nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
667 nlh->nlmsg_len <= remaining);
671 * nlmsg_next - next netlink message in message stream
672 * @nlh: netlink message header
673 * @remaining: number of bytes remaining in message stream
675 * Returns: the next netlink message in the message stream and
676 * decrements remaining by the size of the current message.
678 static inline struct nlmsghdr *
679 nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
681 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
683 *remaining -= totlen;
685 return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
689 * nla_parse - Parse a stream of attributes into a tb buffer
690 * @tb: destination array with maxtype+1 elements
691 * @maxtype: maximum attribute type to be expected
692 * @head: head of attribute stream
693 * @len: length of attribute stream
694 * @policy: validation policy
695 * @extack: extended ACK pointer
697 * Parses a stream of attributes and stores a pointer to each attribute in
698 * the tb array accessible via the attribute type. Attributes with a type
699 * exceeding maxtype will be rejected, policy must be specified, attributes
700 * will be validated in the strictest way possible.
702 * Returns: 0 on success or a negative error code.
704 static inline int nla_parse(struct nlattr **tb, int maxtype,
705 const struct nlattr *head, int len,
706 const struct nla_policy *policy,
707 struct netlink_ext_ack *extack)
709 return __nla_parse(tb, maxtype, head, len, policy,
710 NL_VALIDATE_STRICT, extack);
714 * nla_parse_deprecated - Parse a stream of attributes into a tb buffer
715 * @tb: destination array with maxtype+1 elements
716 * @maxtype: maximum attribute type to be expected
717 * @head: head of attribute stream
718 * @len: length of attribute stream
719 * @policy: validation policy
720 * @extack: extended ACK pointer
722 * Parses a stream of attributes and stores a pointer to each attribute in
723 * the tb array accessible via the attribute type. Attributes with a type
724 * exceeding maxtype will be ignored and attributes from the policy are not
725 * always strictly validated (only for new attributes).
727 * Returns: 0 on success or a negative error code.
729 static inline int nla_parse_deprecated(struct nlattr **tb, int maxtype,
730 const struct nlattr *head, int len,
731 const struct nla_policy *policy,
732 struct netlink_ext_ack *extack)
734 return __nla_parse(tb, maxtype, head, len, policy,
735 NL_VALIDATE_LIBERAL, extack);
739 * nla_parse_deprecated_strict - Parse a stream of attributes into a tb buffer
740 * @tb: destination array with maxtype+1 elements
741 * @maxtype: maximum attribute type to be expected
742 * @head: head of attribute stream
743 * @len: length of attribute stream
744 * @policy: validation policy
745 * @extack: extended ACK pointer
747 * Parses a stream of attributes and stores a pointer to each attribute in
748 * the tb array accessible via the attribute type. Attributes with a type
749 * exceeding maxtype will be rejected as well as trailing data, but the
750 * policy is not completely strictly validated (only for new attributes).
752 * Returns: 0 on success or a negative error code.
754 static inline int nla_parse_deprecated_strict(struct nlattr **tb, int maxtype,
755 const struct nlattr *head,
757 const struct nla_policy *policy,
758 struct netlink_ext_ack *extack)
760 return __nla_parse(tb, maxtype, head, len, policy,
761 NL_VALIDATE_DEPRECATED_STRICT, extack);
765 * __nlmsg_parse - parse attributes of a netlink message
766 * @nlh: netlink message header
767 * @hdrlen: length of family specific header
768 * @tb: destination array with maxtype+1 elements
769 * @maxtype: maximum attribute type to be expected
770 * @policy: validation policy
771 * @validate: validation strictness
772 * @extack: extended ACK report struct
776 static inline int __nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
777 struct nlattr *tb[], int maxtype,
778 const struct nla_policy *policy,
779 unsigned int validate,
780 struct netlink_ext_ack *extack)
782 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) {
783 NL_SET_ERR_MSG(extack, "Invalid header length");
787 return __nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
788 nlmsg_attrlen(nlh, hdrlen), policy, validate,
793 * nlmsg_parse - parse attributes of a netlink message
794 * @nlh: netlink message header
795 * @hdrlen: length of family specific header
796 * @tb: destination array with maxtype+1 elements
797 * @maxtype: maximum attribute type to be expected
798 * @policy: validation policy
799 * @extack: extended ACK report struct
803 static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
804 struct nlattr *tb[], int maxtype,
805 const struct nla_policy *policy,
806 struct netlink_ext_ack *extack)
808 return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
809 NL_VALIDATE_STRICT, extack);
813 * nlmsg_parse_deprecated - parse attributes of a netlink message
814 * @nlh: netlink message header
815 * @hdrlen: length of family specific header
816 * @tb: destination array with maxtype+1 elements
817 * @maxtype: maximum attribute type to be expected
818 * @policy: validation policy
819 * @extack: extended ACK report struct
821 * See nla_parse_deprecated()
823 static inline int nlmsg_parse_deprecated(const struct nlmsghdr *nlh, int hdrlen,
824 struct nlattr *tb[], int maxtype,
825 const struct nla_policy *policy,
826 struct netlink_ext_ack *extack)
828 return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
829 NL_VALIDATE_LIBERAL, extack);
833 * nlmsg_parse_deprecated_strict - parse attributes of a netlink message
834 * @nlh: netlink message header
835 * @hdrlen: length of family specific header
836 * @tb: destination array with maxtype+1 elements
837 * @maxtype: maximum attribute type to be expected
838 * @policy: validation policy
839 * @extack: extended ACK report struct
841 * See nla_parse_deprecated_strict()
844 nlmsg_parse_deprecated_strict(const struct nlmsghdr *nlh, int hdrlen,
845 struct nlattr *tb[], int maxtype,
846 const struct nla_policy *policy,
847 struct netlink_ext_ack *extack)
849 return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
850 NL_VALIDATE_DEPRECATED_STRICT, extack);
854 * nlmsg_find_attr - find a specific attribute in a netlink message
855 * @nlh: netlink message header
856 * @hdrlen: length of family specific header
857 * @attrtype: type of attribute to look for
859 * Returns: the first attribute which matches the specified type.
861 static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
862 int hdrlen, int attrtype)
864 return nla_find(nlmsg_attrdata(nlh, hdrlen),
865 nlmsg_attrlen(nlh, hdrlen), attrtype);
869 * nla_validate_deprecated - Validate a stream of attributes
870 * @head: head of attribute stream
871 * @len: length of attribute stream
872 * @maxtype: maximum attribute type to be expected
873 * @policy: validation policy
874 * @extack: extended ACK report struct
876 * Validates all attributes in the specified attribute stream against the
877 * specified policy. Validation is done in liberal mode.
878 * See documentation of struct nla_policy for more details.
880 * Returns: 0 on success or a negative error code.
882 static inline int nla_validate_deprecated(const struct nlattr *head, int len,
884 const struct nla_policy *policy,
885 struct netlink_ext_ack *extack)
887 return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_LIBERAL,
892 * nla_validate - Validate a stream of attributes
893 * @head: head of attribute stream
894 * @len: length of attribute stream
895 * @maxtype: maximum attribute type to be expected
896 * @policy: validation policy
897 * @extack: extended ACK report struct
899 * Validates all attributes in the specified attribute stream against the
900 * specified policy. Validation is done in strict mode.
901 * See documentation of struct nla_policy for more details.
903 * Returns: 0 on success or a negative error code.
905 static inline int nla_validate(const struct nlattr *head, int len, int maxtype,
906 const struct nla_policy *policy,
907 struct netlink_ext_ack *extack)
909 return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_STRICT,
914 * nlmsg_validate_deprecated - validate a netlink message including attributes
915 * @nlh: netlinket message header
916 * @hdrlen: length of family specific header
917 * @maxtype: maximum attribute type to be expected
918 * @policy: validation policy
919 * @extack: extended ACK report struct
921 static inline int nlmsg_validate_deprecated(const struct nlmsghdr *nlh,
922 int hdrlen, int maxtype,
923 const struct nla_policy *policy,
924 struct netlink_ext_ack *extack)
926 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
929 return __nla_validate(nlmsg_attrdata(nlh, hdrlen),
930 nlmsg_attrlen(nlh, hdrlen), maxtype,
931 policy, NL_VALIDATE_LIBERAL, extack);
937 * nlmsg_report - need to report back to application?
938 * @nlh: netlink message header
940 * Returns: 1 if a report back to the application is requested.
942 static inline int nlmsg_report(const struct nlmsghdr *nlh)
944 return nlh ? !!(nlh->nlmsg_flags & NLM_F_ECHO) : 0;
948 * nlmsg_seq - return the seq number of netlink message
949 * @nlh: netlink message header
951 * Returns: 0 if netlink message is NULL
953 static inline u32 nlmsg_seq(const struct nlmsghdr *nlh)
955 return nlh ? nlh->nlmsg_seq : 0;
959 * nlmsg_for_each_attr - iterate over a stream of attributes
960 * @pos: loop counter, set to current attribute
961 * @nlh: netlink message header
962 * @hdrlen: length of family specific header
963 * @rem: initialized to len, holds bytes currently remaining in stream
965 #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
966 nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
967 nlmsg_attrlen(nlh, hdrlen), rem)
970 * nlmsg_put - Add a new netlink message to an skb
971 * @skb: socket buffer to store message in
972 * @portid: netlink PORTID of requesting application
973 * @seq: sequence number of message
974 * @type: message type
975 * @payload: length of message payload
976 * @flags: message flags
978 * Returns: NULL if the tailroom of the skb is insufficient to store
979 * the message header and payload.
981 static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
982 int type, int payload, int flags)
984 if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
987 return __nlmsg_put(skb, portid, seq, type, payload, flags);
991 * nlmsg_append - Add more data to a nlmsg in a skb
992 * @skb: socket buffer to store message in
993 * @size: length of message payload
995 * Append data to an existing nlmsg, used when constructing a message
996 * with multiple fixed-format headers (which is rare).
997 * Returns: NULL if the tailroom of the skb is insufficient to store
1000 static inline void *nlmsg_append(struct sk_buff *skb, u32 size)
1002 if (unlikely(skb_tailroom(skb) < NLMSG_ALIGN(size)))
1005 if (NLMSG_ALIGN(size) - size)
1006 memset(skb_tail_pointer(skb) + size, 0,
1007 NLMSG_ALIGN(size) - size);
1008 return __skb_put(skb, NLMSG_ALIGN(size));
1012 * nlmsg_put_answer - Add a new callback based netlink message to an skb
1013 * @skb: socket buffer to store message in
1014 * @cb: netlink callback
1015 * @type: message type
1016 * @payload: length of message payload
1017 * @flags: message flags
1019 * Returns: NULL if the tailroom of the skb is insufficient to store
1020 * the message header and payload.
1022 static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
1023 struct netlink_callback *cb,
1024 int type, int payload,
1027 return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1028 type, payload, flags);
1032 * nlmsg_new - Allocate a new netlink message
1033 * @payload: size of the message payload
1034 * @flags: the type of memory to allocate.
1036 * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
1037 * and a good default is needed.
1039 static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
1041 return alloc_skb(nlmsg_total_size(payload), flags);
1045 * nlmsg_new_large - Allocate a new netlink message with non-contiguous
1047 * @payload: size of the message payload
1049 * The allocated skb is unable to have frag page for shinfo->frags*,
1050 * as the NULL setting for skb->head in netlink_skb_destructor() will
1051 * bypass most of the handling in skb_release_data()
1053 static inline struct sk_buff *nlmsg_new_large(size_t payload)
1055 return netlink_alloc_large_skb(nlmsg_total_size(payload), 0);
1059 * nlmsg_end - Finalize a netlink message
1060 * @skb: socket buffer the message is stored in
1061 * @nlh: netlink message header
1063 * Corrects the netlink message header to include the appended
1064 * attributes. Only necessary if attributes have been added to
1067 static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
1069 nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
1073 * nlmsg_get_pos - return current position in netlink message
1074 * @skb: socket buffer the message is stored in
1076 * Returns: a pointer to the current tail of the message.
1078 static inline void *nlmsg_get_pos(struct sk_buff *skb)
1080 return skb_tail_pointer(skb);
1084 * nlmsg_trim - Trim message to a mark
1085 * @skb: socket buffer the message is stored in
1086 * @mark: mark to trim to
1088 * Trims the message to the provided mark.
1090 static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
1093 WARN_ON((unsigned char *) mark < skb->data);
1094 skb_trim(skb, (unsigned char *) mark - skb->data);
1099 * nlmsg_cancel - Cancel construction of a netlink message
1100 * @skb: socket buffer the message is stored in
1101 * @nlh: netlink message header
1103 * Removes the complete netlink message including all
1104 * attributes from the socket buffer again.
1106 static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
1108 nlmsg_trim(skb, nlh);
1112 * nlmsg_free - drop a netlink message
1113 * @skb: socket buffer of netlink message
1115 static inline void nlmsg_free(struct sk_buff *skb)
1121 * nlmsg_consume - free a netlink message
1122 * @skb: socket buffer of netlink message
1124 static inline void nlmsg_consume(struct sk_buff *skb)
1130 * nlmsg_multicast_filtered - multicast a netlink message with filter function
1131 * @sk: netlink socket to spread messages to
1132 * @skb: netlink message as socket buffer
1133 * @portid: own netlink portid to avoid sending to yourself
1134 * @group: multicast group id
1135 * @flags: allocation flags
1136 * @filter: filter function
1137 * @filter_data: filter function private data
1139 * Return: 0 on success, negative error code for failure.
1141 static inline int nlmsg_multicast_filtered(struct sock *sk, struct sk_buff *skb,
1142 u32 portid, unsigned int group,
1144 netlink_filter_fn filter,
1149 NETLINK_CB(skb).dst_group = group;
1151 err = netlink_broadcast_filtered(sk, skb, portid, group, flags,
1152 filter, filter_data);
1160 * nlmsg_multicast - multicast a netlink message
1161 * @sk: netlink socket to spread messages to
1162 * @skb: netlink message as socket buffer
1163 * @portid: own netlink portid to avoid sending to yourself
1164 * @group: multicast group id
1165 * @flags: allocation flags
1167 static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
1168 u32 portid, unsigned int group, gfp_t flags)
1170 return nlmsg_multicast_filtered(sk, skb, portid, group, flags,
1175 * nlmsg_unicast - unicast a netlink message
1176 * @sk: netlink socket to spread message to
1177 * @skb: netlink message as socket buffer
1178 * @portid: netlink portid of the destination socket
1180 static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
1184 err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
1192 * nlmsg_for_each_msg - iterate over a stream of messages
1193 * @pos: loop counter, set to current message
1194 * @head: head of message stream
1195 * @len: length of message stream
1196 * @rem: initialized to len, holds bytes currently remaining in stream
1198 #define nlmsg_for_each_msg(pos, head, len, rem) \
1199 for (pos = head, rem = len; \
1200 nlmsg_ok(pos, rem); \
1201 pos = nlmsg_next(pos, &(rem)))
1204 * nl_dump_check_consistent - check if sequence is consistent and advertise if not
1205 * @cb: netlink callback structure that stores the sequence number
1206 * @nlh: netlink message header to write the flag to
1208 * This function checks if the sequence (generation) number changed during dump
1209 * and if it did, advertises it in the netlink message header.
1211 * The correct way to use it is to set cb->seq to the generation counter when
1212 * all locks for dumping have been acquired, and then call this function for
1213 * each message that is generated.
1215 * Note that due to initialisation concerns, 0 is an invalid sequence number
1216 * and must not be used by code that uses this functionality.
1219 nl_dump_check_consistent(struct netlink_callback *cb,
1220 struct nlmsghdr *nlh)
1222 if (cb->prev_seq && cb->seq != cb->prev_seq)
1223 nlh->nlmsg_flags |= NLM_F_DUMP_INTR;
1224 cb->prev_seq = cb->seq;
1227 /**************************************************************************
1228 * Netlink Attributes
1229 **************************************************************************/
1232 * nla_attr_size - length of attribute not including padding
1233 * @payload: length of payload
1235 static inline int nla_attr_size(int payload)
1237 return NLA_HDRLEN + payload;
1241 * nla_total_size - total length of attribute including padding
1242 * @payload: length of payload
1244 static inline int nla_total_size(int payload)
1246 return NLA_ALIGN(nla_attr_size(payload));
1250 * nla_padlen - length of padding at the tail of attribute
1251 * @payload: length of payload
1253 static inline int nla_padlen(int payload)
1255 return nla_total_size(payload) - nla_attr_size(payload);
1259 * nla_type - attribute type
1260 * @nla: netlink attribute
1262 static inline int nla_type(const struct nlattr *nla)
1264 return nla->nla_type & NLA_TYPE_MASK;
1268 * nla_data - head of payload
1269 * @nla: netlink attribute
1271 static inline void *nla_data(const struct nlattr *nla)
1273 return (char *) nla + NLA_HDRLEN;
1277 * nla_len - length of payload
1278 * @nla: netlink attribute
1280 static inline u16 nla_len(const struct nlattr *nla)
1282 return nla->nla_len - NLA_HDRLEN;
1286 * nla_ok - check if the netlink attribute fits into the remaining bytes
1287 * @nla: netlink attribute
1288 * @remaining: number of bytes remaining in attribute stream
1290 static inline int nla_ok(const struct nlattr *nla, int remaining)
1292 return remaining >= (int) sizeof(*nla) &&
1293 nla->nla_len >= sizeof(*nla) &&
1294 nla->nla_len <= remaining;
1298 * nla_next - next netlink attribute in attribute stream
1299 * @nla: netlink attribute
1300 * @remaining: number of bytes remaining in attribute stream
1302 * Returns: the next netlink attribute in the attribute stream and
1303 * decrements remaining by the size of the current attribute.
1305 static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
1307 unsigned int totlen = NLA_ALIGN(nla->nla_len);
1309 *remaining -= totlen;
1310 return (struct nlattr *) ((char *) nla + totlen);
1314 * nla_find_nested - find attribute in a set of nested attributes
1315 * @nla: attribute containing the nested attributes
1316 * @attrtype: type of attribute to look for
1318 * Returns: the first attribute which matches the specified type.
1320 static inline struct nlattr *
1321 nla_find_nested(const struct nlattr *nla, int attrtype)
1323 return nla_find(nla_data(nla), nla_len(nla), attrtype);
1327 * nla_parse_nested - parse nested attributes
1328 * @tb: destination array with maxtype+1 elements
1329 * @maxtype: maximum attribute type to be expected
1330 * @nla: attribute containing the nested attributes
1331 * @policy: validation policy
1332 * @extack: extended ACK report struct
1336 static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
1337 const struct nlattr *nla,
1338 const struct nla_policy *policy,
1339 struct netlink_ext_ack *extack)
1341 if (!(nla->nla_type & NLA_F_NESTED)) {
1342 NL_SET_ERR_MSG_ATTR(extack, nla, "NLA_F_NESTED is missing");
1346 return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
1347 NL_VALIDATE_STRICT, extack);
1351 * nla_parse_nested_deprecated - parse nested attributes
1352 * @tb: destination array with maxtype+1 elements
1353 * @maxtype: maximum attribute type to be expected
1354 * @nla: attribute containing the nested attributes
1355 * @policy: validation policy
1356 * @extack: extended ACK report struct
1358 * See nla_parse_deprecated()
1360 static inline int nla_parse_nested_deprecated(struct nlattr *tb[], int maxtype,
1361 const struct nlattr *nla,
1362 const struct nla_policy *policy,
1363 struct netlink_ext_ack *extack)
1365 return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
1366 NL_VALIDATE_LIBERAL, extack);
1370 * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
1371 * @skb: socket buffer to add attribute to
1372 * @attrtype: attribute type
1373 * @value: numeric value
1375 static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
1377 /* temporary variables to work around GCC PR81715 with asan-stack=1 */
1380 return nla_put(skb, attrtype, sizeof(u8), &tmp);
1384 * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
1385 * @skb: socket buffer to add attribute to
1386 * @attrtype: attribute type
1387 * @value: numeric value
1389 static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
1393 return nla_put(skb, attrtype, sizeof(u16), &tmp);
1397 * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer
1398 * @skb: socket buffer to add attribute to
1399 * @attrtype: attribute type
1400 * @value: numeric value
1402 static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
1406 return nla_put(skb, attrtype, sizeof(__be16), &tmp);
1410 * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer
1411 * @skb: socket buffer to add attribute to
1412 * @attrtype: attribute type
1413 * @value: numeric value
1415 static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
1419 return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
1423 * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer
1424 * @skb: socket buffer to add attribute to
1425 * @attrtype: attribute type
1426 * @value: numeric value
1428 static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
1432 return nla_put(skb, attrtype, sizeof(__le16), &tmp);
1436 * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
1437 * @skb: socket buffer to add attribute to
1438 * @attrtype: attribute type
1439 * @value: numeric value
1441 static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
1445 return nla_put(skb, attrtype, sizeof(u32), &tmp);
1449 * nla_put_uint - Add a variable-size unsigned int to a socket buffer
1450 * @skb: socket buffer to add attribute to
1451 * @attrtype: attribute type
1452 * @value: numeric value
1454 static inline int nla_put_uint(struct sk_buff *skb, int attrtype, u64 value)
1460 return nla_put_u32(skb, attrtype, tmp32);
1461 return nla_put(skb, attrtype, sizeof(u64), &tmp64);
1465 * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer
1466 * @skb: socket buffer to add attribute to
1467 * @attrtype: attribute type
1468 * @value: numeric value
1470 static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
1474 return nla_put(skb, attrtype, sizeof(__be32), &tmp);
1478 * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer
1479 * @skb: socket buffer to add attribute to
1480 * @attrtype: attribute type
1481 * @value: numeric value
1483 static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
1487 return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
1491 * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer
1492 * @skb: socket buffer to add attribute to
1493 * @attrtype: attribute type
1494 * @value: numeric value
1496 static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value)
1500 return nla_put(skb, attrtype, sizeof(__le32), &tmp);
1504 * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it
1505 * @skb: socket buffer to add attribute to
1506 * @attrtype: attribute type
1507 * @value: numeric value
1508 * @padattr: attribute type for the padding
1510 static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype,
1511 u64 value, int padattr)
1515 return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
1519 * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it
1520 * @skb: socket buffer to add attribute to
1521 * @attrtype: attribute type
1522 * @value: numeric value
1523 * @padattr: attribute type for the padding
1525 static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value,
1530 return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr);
1534 * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it
1535 * @skb: socket buffer to add attribute to
1536 * @attrtype: attribute type
1537 * @value: numeric value
1538 * @padattr: attribute type for the padding
1540 static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value,
1545 return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp,
1550 * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it
1551 * @skb: socket buffer to add attribute to
1552 * @attrtype: attribute type
1553 * @value: numeric value
1554 * @padattr: attribute type for the padding
1556 static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value,
1561 return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr);
1565 * nla_put_s8 - Add a s8 netlink attribute to a socket buffer
1566 * @skb: socket buffer to add attribute to
1567 * @attrtype: attribute type
1568 * @value: numeric value
1570 static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
1574 return nla_put(skb, attrtype, sizeof(s8), &tmp);
1578 * nla_put_s16 - Add a s16 netlink attribute to a socket buffer
1579 * @skb: socket buffer to add attribute to
1580 * @attrtype: attribute type
1581 * @value: numeric value
1583 static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
1587 return nla_put(skb, attrtype, sizeof(s16), &tmp);
1591 * nla_put_s32 - Add a s32 netlink attribute to a socket buffer
1592 * @skb: socket buffer to add attribute to
1593 * @attrtype: attribute type
1594 * @value: numeric value
1596 static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
1600 return nla_put(skb, attrtype, sizeof(s32), &tmp);
1604 * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it
1605 * @skb: socket buffer to add attribute to
1606 * @attrtype: attribute type
1607 * @value: numeric value
1608 * @padattr: attribute type for the padding
1610 static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value,
1615 return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr);
1619 * nla_put_sint - Add a variable-size signed int to a socket buffer
1620 * @skb: socket buffer to add attribute to
1621 * @attrtype: attribute type
1622 * @value: numeric value
1624 static inline int nla_put_sint(struct sk_buff *skb, int attrtype, s64 value)
1630 return nla_put_s32(skb, attrtype, tmp32);
1631 return nla_put(skb, attrtype, sizeof(s64), &tmp64);
1635 * nla_put_string - Add a string netlink attribute to a socket buffer
1636 * @skb: socket buffer to add attribute to
1637 * @attrtype: attribute type
1638 * @str: NUL terminated string
1640 static inline int nla_put_string(struct sk_buff *skb, int attrtype,
1643 return nla_put(skb, attrtype, strlen(str) + 1, str);
1647 * nla_put_flag - Add a flag netlink attribute to a socket buffer
1648 * @skb: socket buffer to add attribute to
1649 * @attrtype: attribute type
1651 static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
1653 return nla_put(skb, attrtype, 0, NULL);
1657 * nla_put_msecs - Add a msecs netlink attribute to a skb and align it
1658 * @skb: socket buffer to add attribute to
1659 * @attrtype: attribute type
1660 * @njiffies: number of jiffies to convert to msecs
1661 * @padattr: attribute type for the padding
1663 static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
1664 unsigned long njiffies, int padattr)
1666 u64 tmp = jiffies_to_msecs(njiffies);
1668 return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
1672 * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket
1674 * @skb: socket buffer to add attribute to
1675 * @attrtype: attribute type
1676 * @addr: IPv4 address
1678 static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
1683 return nla_put_be32(skb, attrtype, tmp);
1687 * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket
1689 * @skb: socket buffer to add attribute to
1690 * @attrtype: attribute type
1691 * @addr: IPv6 address
1693 static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype,
1694 const struct in6_addr *addr)
1696 return nla_put(skb, attrtype, sizeof(*addr), addr);
1700 * nla_put_bitfield32 - Add a bitfield32 netlink attribute to a socket buffer
1701 * @skb: socket buffer to add attribute to
1702 * @attrtype: attribute type
1703 * @value: value carrying bits
1704 * @selector: selector of valid bits
1706 static inline int nla_put_bitfield32(struct sk_buff *skb, int attrtype,
1707 __u32 value, __u32 selector)
1709 struct nla_bitfield32 tmp = { value, selector, };
1711 return nla_put(skb, attrtype, sizeof(tmp), &tmp);
1715 * nla_get_u32 - return payload of u32 attribute
1716 * @nla: u32 netlink attribute
1718 static inline u32 nla_get_u32(const struct nlattr *nla)
1720 return *(u32 *) nla_data(nla);
1724 * nla_get_u32_default - return payload of u32 attribute or default
1725 * @nla: u32 netlink attribute, may be %NULL
1726 * @defvalue: default value to use if @nla is %NULL
1728 * Return: the value of the attribute, or the default value if not present
1730 static inline u32 nla_get_u32_default(const struct nlattr *nla, u32 defvalue)
1734 return nla_get_u32(nla);
1738 * nla_get_be32 - return payload of __be32 attribute
1739 * @nla: __be32 netlink attribute
1741 static inline __be32 nla_get_be32(const struct nlattr *nla)
1743 return *(__be32 *) nla_data(nla);
1747 * nla_get_be32_default - return payload of be32 attribute or default
1748 * @nla: __be32 netlink attribute, may be %NULL
1749 * @defvalue: default value to use if @nla is %NULL
1751 * Return: the value of the attribute, or the default value if not present
1753 static inline __be32 nla_get_be32_default(const struct nlattr *nla,
1758 return nla_get_be32(nla);
1762 * nla_get_le32 - return payload of __le32 attribute
1763 * @nla: __le32 netlink attribute
1765 static inline __le32 nla_get_le32(const struct nlattr *nla)
1767 return *(__le32 *) nla_data(nla);
1771 * nla_get_le32_default - return payload of le32 attribute or default
1772 * @nla: __le32 netlink attribute, may be %NULL
1773 * @defvalue: default value to use if @nla is %NULL
1775 * Return: the value of the attribute, or the default value if not present
1777 static inline __le32 nla_get_le32_default(const struct nlattr *nla,
1782 return nla_get_le32(nla);
1786 * nla_get_u16 - return payload of u16 attribute
1787 * @nla: u16 netlink attribute
1789 static inline u16 nla_get_u16(const struct nlattr *nla)
1791 return *(u16 *) nla_data(nla);
1795 * nla_get_u16_default - return payload of u16 attribute or default
1796 * @nla: u16 netlink attribute, may be %NULL
1797 * @defvalue: default value to use if @nla is %NULL
1799 * Return: the value of the attribute, or the default value if not present
1801 static inline u16 nla_get_u16_default(const struct nlattr *nla, u16 defvalue)
1805 return nla_get_u16(nla);
1809 * nla_get_be16 - return payload of __be16 attribute
1810 * @nla: __be16 netlink attribute
1812 static inline __be16 nla_get_be16(const struct nlattr *nla)
1814 return *(__be16 *) nla_data(nla);
1818 * nla_get_be16_default - return payload of be16 attribute or default
1819 * @nla: __be16 netlink attribute, may be %NULL
1820 * @defvalue: default value to use if @nla is %NULL
1822 * Return: the value of the attribute, or the default value if not present
1824 static inline __be16 nla_get_be16_default(const struct nlattr *nla,
1829 return nla_get_be16(nla);
1833 * nla_get_le16 - return payload of __le16 attribute
1834 * @nla: __le16 netlink attribute
1836 static inline __le16 nla_get_le16(const struct nlattr *nla)
1838 return *(__le16 *) nla_data(nla);
1842 * nla_get_le16_default - return payload of le16 attribute or default
1843 * @nla: __le16 netlink attribute, may be %NULL
1844 * @defvalue: default value to use if @nla is %NULL
1846 * Return: the value of the attribute, or the default value if not present
1848 static inline __le16 nla_get_le16_default(const struct nlattr *nla,
1853 return nla_get_le16(nla);
1857 * nla_get_u8 - return payload of u8 attribute
1858 * @nla: u8 netlink attribute
1860 static inline u8 nla_get_u8(const struct nlattr *nla)
1862 return *(u8 *) nla_data(nla);
1866 * nla_get_u8_default - return payload of u8 attribute or default
1867 * @nla: u8 netlink attribute, may be %NULL
1868 * @defvalue: default value to use if @nla is %NULL
1870 * Return: the value of the attribute, or the default value if not present
1872 static inline u8 nla_get_u8_default(const struct nlattr *nla, u8 defvalue)
1876 return nla_get_u8(nla);
1880 * nla_get_u64 - return payload of u64 attribute
1881 * @nla: u64 netlink attribute
1883 static inline u64 nla_get_u64(const struct nlattr *nla)
1887 nla_memcpy(&tmp, nla, sizeof(tmp));
1893 * nla_get_u64_default - return payload of u64 attribute or default
1894 * @nla: u64 netlink attribute, may be %NULL
1895 * @defvalue: default value to use if @nla is %NULL
1897 * Return: the value of the attribute, or the default value if not present
1899 static inline u64 nla_get_u64_default(const struct nlattr *nla, u64 defvalue)
1903 return nla_get_u64(nla);
1907 * nla_get_uint - return payload of uint attribute
1908 * @nla: uint netlink attribute
1910 static inline u64 nla_get_uint(const struct nlattr *nla)
1912 if (nla_len(nla) == sizeof(u32))
1913 return nla_get_u32(nla);
1914 return nla_get_u64(nla);
1918 * nla_get_uint_default - return payload of uint attribute or default
1919 * @nla: uint netlink attribute, may be %NULL
1920 * @defvalue: default value to use if @nla is %NULL
1922 * Return: the value of the attribute, or the default value if not present
1924 static inline u64 nla_get_uint_default(const struct nlattr *nla, u64 defvalue)
1928 return nla_get_uint(nla);
1932 * nla_get_be64 - return payload of __be64 attribute
1933 * @nla: __be64 netlink attribute
1935 static inline __be64 nla_get_be64(const struct nlattr *nla)
1939 nla_memcpy(&tmp, nla, sizeof(tmp));
1945 * nla_get_be64_default - return payload of be64 attribute or default
1946 * @nla: __be64 netlink attribute, may be %NULL
1947 * @defvalue: default value to use if @nla is %NULL
1949 * Return: the value of the attribute, or the default value if not present
1951 static inline __be64 nla_get_be64_default(const struct nlattr *nla,
1956 return nla_get_be64(nla);
1960 * nla_get_le64 - return payload of __le64 attribute
1961 * @nla: __le64 netlink attribute
1963 static inline __le64 nla_get_le64(const struct nlattr *nla)
1965 return *(__le64 *) nla_data(nla);
1969 * nla_get_le64_default - return payload of le64 attribute or default
1970 * @nla: __le64 netlink attribute, may be %NULL
1971 * @defvalue: default value to use if @nla is %NULL
1973 * Return: the value of the attribute, or the default value if not present
1975 static inline __le64 nla_get_le64_default(const struct nlattr *nla,
1980 return nla_get_le64(nla);
1984 * nla_get_s32 - return payload of s32 attribute
1985 * @nla: s32 netlink attribute
1987 static inline s32 nla_get_s32(const struct nlattr *nla)
1989 return *(s32 *) nla_data(nla);
1993 * nla_get_s32_default - return payload of s32 attribute or default
1994 * @nla: s32 netlink attribute, may be %NULL
1995 * @defvalue: default value to use if @nla is %NULL
1997 * Return: the value of the attribute, or the default value if not present
1999 static inline s32 nla_get_s32_default(const struct nlattr *nla, s32 defvalue)
2003 return nla_get_s32(nla);
2007 * nla_get_s16 - return payload of s16 attribute
2008 * @nla: s16 netlink attribute
2010 static inline s16 nla_get_s16(const struct nlattr *nla)
2012 return *(s16 *) nla_data(nla);
2016 * nla_get_s16_default - return payload of s16 attribute or default
2017 * @nla: s16 netlink attribute, may be %NULL
2018 * @defvalue: default value to use if @nla is %NULL
2020 * Return: the value of the attribute, or the default value if not present
2022 static inline s16 nla_get_s16_default(const struct nlattr *nla, s16 defvalue)
2026 return nla_get_s16(nla);
2030 * nla_get_s8 - return payload of s8 attribute
2031 * @nla: s8 netlink attribute
2033 static inline s8 nla_get_s8(const struct nlattr *nla)
2035 return *(s8 *) nla_data(nla);
2039 * nla_get_s8_default - return payload of s8 attribute or default
2040 * @nla: s8 netlink attribute, may be %NULL
2041 * @defvalue: default value to use if @nla is %NULL
2043 * Return: the value of the attribute, or the default value if not present
2045 static inline s8 nla_get_s8_default(const struct nlattr *nla, s8 defvalue)
2049 return nla_get_s8(nla);
2053 * nla_get_s64 - return payload of s64 attribute
2054 * @nla: s64 netlink attribute
2056 static inline s64 nla_get_s64(const struct nlattr *nla)
2060 nla_memcpy(&tmp, nla, sizeof(tmp));
2066 * nla_get_s64_default - return payload of s64 attribute or default
2067 * @nla: s64 netlink attribute, may be %NULL
2068 * @defvalue: default value to use if @nla is %NULL
2070 * Return: the value of the attribute, or the default value if not present
2072 static inline s64 nla_get_s64_default(const struct nlattr *nla, s64 defvalue)
2076 return nla_get_s64(nla);
2080 * nla_get_sint - return payload of uint attribute
2081 * @nla: uint netlink attribute
2083 static inline s64 nla_get_sint(const struct nlattr *nla)
2085 if (nla_len(nla) == sizeof(s32))
2086 return nla_get_s32(nla);
2087 return nla_get_s64(nla);
2091 * nla_get_sint_default - return payload of sint attribute or default
2092 * @nla: sint netlink attribute, may be %NULL
2093 * @defvalue: default value to use if @nla is %NULL
2095 * Return: the value of the attribute, or the default value if not present
2097 static inline s64 nla_get_sint_default(const struct nlattr *nla, s64 defvalue)
2101 return nla_get_sint(nla);
2105 * nla_get_flag - return payload of flag attribute
2106 * @nla: flag netlink attribute
2108 static inline int nla_get_flag(const struct nlattr *nla)
2114 * nla_get_msecs - return payload of msecs attribute
2115 * @nla: msecs netlink attribute
2117 * Returns: the number of milliseconds in jiffies.
2119 static inline unsigned long nla_get_msecs(const struct nlattr *nla)
2121 u64 msecs = nla_get_u64(nla);
2123 return msecs_to_jiffies((unsigned long) msecs);
2127 * nla_get_msecs_default - return payload of msecs attribute or default
2128 * @nla: msecs netlink attribute, may be %NULL
2129 * @defvalue: default value to use if @nla is %NULL
2131 * Return: the value of the attribute, or the default value if not present
2133 static inline unsigned long nla_get_msecs_default(const struct nlattr *nla,
2134 unsigned long defvalue)
2138 return nla_get_msecs(nla);
2142 * nla_get_in_addr - return payload of IPv4 address attribute
2143 * @nla: IPv4 address netlink attribute
2145 static inline __be32 nla_get_in_addr(const struct nlattr *nla)
2147 return *(__be32 *) nla_data(nla);
2151 * nla_get_in_addr_default - return payload of be32 attribute or default
2152 * @nla: IPv4 address netlink attribute, may be %NULL
2153 * @defvalue: default value to use if @nla is %NULL
2155 * Return: the value of the attribute, or the default value if not present
2157 static inline __be32 nla_get_in_addr_default(const struct nlattr *nla,
2162 return nla_get_in_addr(nla);
2166 * nla_get_in6_addr - return payload of IPv6 address attribute
2167 * @nla: IPv6 address netlink attribute
2169 static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla)
2171 struct in6_addr tmp;
2173 nla_memcpy(&tmp, nla, sizeof(tmp));
2178 * nla_get_bitfield32 - return payload of 32 bitfield attribute
2179 * @nla: nla_bitfield32 attribute
2181 static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla)
2183 struct nla_bitfield32 tmp;
2185 nla_memcpy(&tmp, nla, sizeof(tmp));
2190 * nla_memdup - duplicate attribute memory (kmemdup)
2191 * @src: netlink attribute to duplicate from
2194 static inline void *nla_memdup_noprof(const struct nlattr *src, gfp_t gfp)
2196 return kmemdup_noprof(nla_data(src), nla_len(src), gfp);
2198 #define nla_memdup(...) alloc_hooks(nla_memdup_noprof(__VA_ARGS__))
2201 * nla_nest_start_noflag - Start a new level of nested attributes
2202 * @skb: socket buffer to add attributes to
2203 * @attrtype: attribute type of container
2205 * This function exists for backward compatibility to use in APIs which never
2206 * marked their nest attributes with NLA_F_NESTED flag. New APIs should use
2207 * nla_nest_start() which sets the flag.
2209 * Returns: the container attribute or NULL on error
2211 static inline struct nlattr *nla_nest_start_noflag(struct sk_buff *skb,
2214 struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
2216 if (nla_put(skb, attrtype, 0, NULL) < 0)
2223 * nla_nest_start - Start a new level of nested attributes, with NLA_F_NESTED
2224 * @skb: socket buffer to add attributes to
2225 * @attrtype: attribute type of container
2227 * Unlike nla_nest_start_noflag(), mark the nest attribute with NLA_F_NESTED
2228 * flag. This is the preferred function to use in new code.
2230 * Returns: the container attribute or NULL on error
2232 static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
2234 return nla_nest_start_noflag(skb, attrtype | NLA_F_NESTED);
2238 * nla_nest_end - Finalize nesting of attributes
2239 * @skb: socket buffer the attributes are stored in
2240 * @start: container attribute
2242 * Corrects the container attribute header to include the all
2243 * appended attributes.
2245 * Returns: the total data length of the skb.
2247 static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
2249 start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
2254 * nla_nest_cancel - Cancel nesting of attributes
2255 * @skb: socket buffer the message is stored in
2256 * @start: container attribute
2258 * Removes the container attribute and including all nested
2259 * attributes. Returns -EMSGSIZE
2261 static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
2263 nlmsg_trim(skb, start);
2267 * nla_put_empty_nest - Create an empty nest
2268 * @skb: socket buffer the message is stored in
2269 * @attrtype: attribute type of the container
2271 * This function is a helper for creating empty nests.
2273 * Returns: 0 when successful or -EMSGSIZE on failure.
2275 static inline int nla_put_empty_nest(struct sk_buff *skb, int attrtype)
2277 return nla_nest_start(skb, attrtype) ? 0 : -EMSGSIZE;
2281 * __nla_validate_nested - Validate a stream of nested attributes
2282 * @start: container attribute
2283 * @maxtype: maximum attribute type to be expected
2284 * @policy: validation policy
2285 * @validate: validation strictness
2286 * @extack: extended ACK report struct
2288 * Validates all attributes in the nested attribute stream against the
2289 * specified policy. Attributes with a type exceeding maxtype will be
2290 * ignored. See documentation of struct nla_policy for more details.
2292 * Returns: 0 on success or a negative error code.
2294 static inline int __nla_validate_nested(const struct nlattr *start, int maxtype,
2295 const struct nla_policy *policy,
2296 unsigned int validate,
2297 struct netlink_ext_ack *extack)
2299 return __nla_validate(nla_data(start), nla_len(start), maxtype, policy,
2304 nla_validate_nested(const struct nlattr *start, int maxtype,
2305 const struct nla_policy *policy,
2306 struct netlink_ext_ack *extack)
2308 return __nla_validate_nested(start, maxtype, policy,
2309 NL_VALIDATE_STRICT, extack);
2313 nla_validate_nested_deprecated(const struct nlattr *start, int maxtype,
2314 const struct nla_policy *policy,
2315 struct netlink_ext_ack *extack)
2317 return __nla_validate_nested(start, maxtype, policy,
2318 NL_VALIDATE_LIBERAL, extack);
2322 * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute
2323 * @skb: socket buffer the message is stored in
2325 * Return: true if padding is needed to align the next attribute (nla_data()) to
2326 * a 64-bit aligned area.
2328 static inline bool nla_need_padding_for_64bit(struct sk_buff *skb)
2330 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2331 /* The nlattr header is 4 bytes in size, that's why we test
2332 * if the skb->data _is_ aligned. A NOP attribute, plus
2333 * nlattr header for next attribute, will make nla_data()
2336 if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
2343 * nla_align_64bit - 64-bit align the nla_data() of next attribute
2344 * @skb: socket buffer the message is stored in
2345 * @padattr: attribute type for the padding
2347 * Conditionally emit a padding netlink attribute in order to make
2348 * the next attribute we emit have a 64-bit aligned nla_data() area.
2349 * This will only be done in architectures which do not have
2350 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
2352 * Returns: zero on success or a negative error code.
2354 static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
2356 if (nla_need_padding_for_64bit(skb) &&
2357 !nla_reserve(skb, padattr, 0))
2364 * nla_total_size_64bit - total length of attribute including padding
2365 * @payload: length of payload
2367 static inline int nla_total_size_64bit(int payload)
2369 return NLA_ALIGN(nla_attr_size(payload))
2370 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2371 + NLA_ALIGN(nla_attr_size(0))
2377 * nla_for_each_attr - iterate over a stream of attributes
2378 * @pos: loop counter, set to current attribute
2379 * @head: head of attribute stream
2380 * @len: length of attribute stream
2381 * @rem: initialized to len, holds bytes currently remaining in stream
2383 #define nla_for_each_attr(pos, head, len, rem) \
2384 for (pos = head, rem = len; \
2386 pos = nla_next(pos, &(rem)))
2389 * nla_for_each_attr_type - iterate over a stream of attributes
2390 * @pos: loop counter, set to current attribute
2391 * @type: required attribute type for @pos
2392 * @head: head of attribute stream
2393 * @len: length of attribute stream
2394 * @rem: initialized to len, holds bytes currently remaining in stream
2396 #define nla_for_each_attr_type(pos, type, head, len, rem) \
2397 nla_for_each_attr(pos, head, len, rem) \
2398 if (nla_type(pos) == type)
2401 * nla_for_each_nested - iterate over nested attributes
2402 * @pos: loop counter, set to current attribute
2403 * @nla: attribute containing the nested attributes
2404 * @rem: initialized to len, holds bytes currently remaining in stream
2406 #define nla_for_each_nested(pos, nla, rem) \
2407 nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
2410 * nla_for_each_nested_type - iterate over nested attributes
2411 * @pos: loop counter, set to current attribute
2412 * @type: required attribute type for @pos
2413 * @nla: attribute containing the nested attributes
2414 * @rem: initialized to len, holds bytes currently remaining in stream
2416 #define nla_for_each_nested_type(pos, type, nla, rem) \
2417 nla_for_each_nested(pos, nla, rem) \
2418 if (nla_type(pos) == type)
2421 * nla_is_last - Test if attribute is last in stream
2422 * @nla: attribute to test
2423 * @rem: bytes remaining in stream
2425 static inline bool nla_is_last(const struct nlattr *nla, int rem)
2427 return nla->nla_len == rem;
2430 void nla_get_range_unsigned(const struct nla_policy *pt,
2431 struct netlink_range_validation *range);
2432 void nla_get_range_signed(const struct nla_policy *pt,
2433 struct netlink_range_validation_signed *range);
2435 struct netlink_policy_dump_state;
2437 int netlink_policy_dump_add_policy(struct netlink_policy_dump_state **pstate,
2438 const struct nla_policy *policy,
2439 unsigned int maxtype);
2440 int netlink_policy_dump_get_policy_idx(struct netlink_policy_dump_state *state,
2441 const struct nla_policy *policy,
2442 unsigned int maxtype);
2443 bool netlink_policy_dump_loop(struct netlink_policy_dump_state *state);
2444 int netlink_policy_dump_write(struct sk_buff *skb,
2445 struct netlink_policy_dump_state *state);
2446 int netlink_policy_dump_attr_size_estimate(const struct nla_policy *pt);
2447 int netlink_policy_dump_write_attr(struct sk_buff *skb,
2448 const struct nla_policy *pt,
2450 void netlink_policy_dump_free(struct netlink_policy_dump_state *state);