xref: /openbmc/linux/include/net/netlink.h (revision 5fac9b7c)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2bfa83a9eSThomas Graf #ifndef __NET_NETLINK_H
3bfa83a9eSThomas Graf #define __NET_NETLINK_H
4bfa83a9eSThomas Graf 
5bfa83a9eSThomas Graf #include <linux/types.h>
6bfa83a9eSThomas Graf #include <linux/netlink.h>
7d7fe0f24SAl Viro #include <linux/jiffies.h>
8930345eaSJiri Benc #include <linux/in6.h>
9bfa83a9eSThomas Graf 
10bfa83a9eSThomas Graf /* ========================================================================
11bfa83a9eSThomas Graf  *         Netlink Messages and Attributes Interface (As Seen On TV)
12bfa83a9eSThomas Graf  * ------------------------------------------------------------------------
13bfa83a9eSThomas Graf  *                          Messages Interface
14bfa83a9eSThomas Graf  * ------------------------------------------------------------------------
15bfa83a9eSThomas Graf  *
16bfa83a9eSThomas Graf  * Message Format:
17bfa83a9eSThomas Graf  *    <--- nlmsg_total_size(payload)  --->
18bfa83a9eSThomas Graf  *    <-- nlmsg_msg_size(payload) ->
19bfa83a9eSThomas Graf  *   +----------+- - -+-------------+- - -+-------- - -
20bfa83a9eSThomas Graf  *   | nlmsghdr | Pad |   Payload   | Pad | nlmsghdr
21bfa83a9eSThomas Graf  *   +----------+- - -+-------------+- - -+-------- - -
22bfa83a9eSThomas Graf  *   nlmsg_data(nlh)---^                   ^
23bfa83a9eSThomas Graf  *   nlmsg_next(nlh)-----------------------+
24bfa83a9eSThomas Graf  *
25bfa83a9eSThomas Graf  * Payload Format:
26bfa83a9eSThomas Graf  *    <---------------------- nlmsg_len(nlh) --------------------->
27bfa83a9eSThomas Graf  *    <------ hdrlen ------>       <- nlmsg_attrlen(nlh, hdrlen) ->
28bfa83a9eSThomas Graf  *   +----------------------+- - -+--------------------------------+
29bfa83a9eSThomas Graf  *   |     Family Header    | Pad |           Attributes           |
30bfa83a9eSThomas Graf  *   +----------------------+- - -+--------------------------------+
31bfa83a9eSThomas Graf  *   nlmsg_attrdata(nlh, hdrlen)---^
32bfa83a9eSThomas Graf  *
33bfa83a9eSThomas Graf  * Data Structures:
34bfa83a9eSThomas Graf  *   struct nlmsghdr			netlink message header
35bfa83a9eSThomas Graf  *
36bfa83a9eSThomas Graf  * Message Construction:
37bfa83a9eSThomas Graf  *   nlmsg_new()			create a new netlink message
38bfa83a9eSThomas Graf  *   nlmsg_put()			add a netlink message to an skb
39bfa83a9eSThomas Graf  *   nlmsg_put_answer()			callback based nlmsg_put()
401dc8d8c0SJustin P. Mattock  *   nlmsg_end()			finalize netlink message
41fe4944e5SThomas Graf  *   nlmsg_get_pos()			return current position in message
42fe4944e5SThomas Graf  *   nlmsg_trim()			trim part of message
43bfa83a9eSThomas Graf  *   nlmsg_cancel()			cancel message construction
44bfa83a9eSThomas Graf  *   nlmsg_free()			free a netlink message
45bfa83a9eSThomas Graf  *
46bfa83a9eSThomas Graf  * Message Sending:
47bfa83a9eSThomas Graf  *   nlmsg_multicast()			multicast message to several groups
48bfa83a9eSThomas Graf  *   nlmsg_unicast()			unicast a message to a single socket
49d387f6adSThomas Graf  *   nlmsg_notify()			send notification message
50bfa83a9eSThomas Graf  *
51bfa83a9eSThomas Graf  * Message Length Calculations:
52bfa83a9eSThomas Graf  *   nlmsg_msg_size(payload)		length of message w/o padding
53bfa83a9eSThomas Graf  *   nlmsg_total_size(payload)		length of message w/ padding
54bfa83a9eSThomas Graf  *   nlmsg_padlen(payload)		length of padding at tail
55bfa83a9eSThomas Graf  *
56bfa83a9eSThomas Graf  * Message Payload Access:
57bfa83a9eSThomas Graf  *   nlmsg_data(nlh)			head of message payload
58bfa83a9eSThomas Graf  *   nlmsg_len(nlh)			length of message payload
59bfa83a9eSThomas Graf  *   nlmsg_attrdata(nlh, hdrlen)	head of attributes data
60bfa83a9eSThomas Graf  *   nlmsg_attrlen(nlh, hdrlen)		length of attributes data
61bfa83a9eSThomas Graf  *
62bfa83a9eSThomas Graf  * Message Parsing:
63bfa83a9eSThomas Graf  *   nlmsg_ok(nlh, remaining)		does nlh fit into remaining bytes?
64bfa83a9eSThomas Graf  *   nlmsg_next(nlh, remaining)		get next netlink message
65bfa83a9eSThomas Graf  *   nlmsg_parse()			parse attributes of a message
66bfa83a9eSThomas Graf  *   nlmsg_find_attr()			find an attribute in a message
67bfa83a9eSThomas Graf  *   nlmsg_for_each_msg()		loop over all messages
68bfa83a9eSThomas Graf  *   nlmsg_validate()			validate netlink message incl. attrs
69bfa83a9eSThomas Graf  *   nlmsg_for_each_attr()		loop over all attributes
70bfa83a9eSThomas Graf  *
7197676b6bSThomas Graf  * Misc:
7297676b6bSThomas Graf  *   nlmsg_report()			report back to application?
7397676b6bSThomas Graf  *
74bfa83a9eSThomas Graf  * ------------------------------------------------------------------------
75bfa83a9eSThomas Graf  *                          Attributes Interface
76bfa83a9eSThomas Graf  * ------------------------------------------------------------------------
77bfa83a9eSThomas Graf  *
78bfa83a9eSThomas Graf  * Attribute Format:
79bfa83a9eSThomas Graf  *    <------- nla_total_size(payload) ------->
80bfa83a9eSThomas Graf  *    <---- nla_attr_size(payload) ----->
81bfa83a9eSThomas Graf  *   +----------+- - -+- - - - - - - - - +- - -+-------- - -
82bfa83a9eSThomas Graf  *   |  Header  | Pad |     Payload      | Pad |  Header
83bfa83a9eSThomas Graf  *   +----------+- - -+- - - - - - - - - +- - -+-------- - -
84bfa83a9eSThomas Graf  *                     <- nla_len(nla) ->      ^
85bfa83a9eSThomas Graf  *   nla_data(nla)----^                        |
86bfa83a9eSThomas Graf  *   nla_next(nla)-----------------------------'
87bfa83a9eSThomas Graf  *
88bfa83a9eSThomas Graf  * Data Structures:
89d1ec3b77SPierre Ynard  *   struct nlattr			netlink attribute header
90bfa83a9eSThomas Graf  *
91bfa83a9eSThomas Graf  * Attribute Construction:
92fe4944e5SThomas Graf  *   nla_reserve(skb, type, len)	reserve room for an attribute
93fe4944e5SThomas Graf  *   nla_reserve_nohdr(skb, len)	reserve room for an attribute w/o hdr
94bfa83a9eSThomas Graf  *   nla_put(skb, type, len, data)	add attribute to skb
95fe4944e5SThomas Graf  *   nla_put_nohdr(skb, len, data)	add attribute w/o hdr
9601480e1cSPatrick McHardy  *   nla_append(skb, len, data)		append data to skb
97bfa83a9eSThomas Graf  *
98bfa83a9eSThomas Graf  * Attribute Construction for Basic Types:
99bfa83a9eSThomas Graf  *   nla_put_u8(skb, type, value)	add u8 attribute to skb
100bfa83a9eSThomas Graf  *   nla_put_u16(skb, type, value)	add u16 attribute to skb
101bfa83a9eSThomas Graf  *   nla_put_u32(skb, type, value)	add u32 attribute to skb
10226837012SRolf Eike Beer  *   nla_put_u64_64bit(skb, type,
10350225243SNicolas Dichtel  *                     value, padattr)	add u64 attribute to skb
1044778e0beSJiri Pirko  *   nla_put_s8(skb, type, value)	add s8 attribute to skb
1054778e0beSJiri Pirko  *   nla_put_s16(skb, type, value)	add s16 attribute to skb
1064778e0beSJiri Pirko  *   nla_put_s32(skb, type, value)	add s32 attribute to skb
107756a2f59SNicolas Dichtel  *   nla_put_s64(skb, type, value,
108756a2f59SNicolas Dichtel  *               padattr)		add s64 attribute to skb
109bfa83a9eSThomas Graf  *   nla_put_string(skb, type, str)	add string attribute to skb
110bfa83a9eSThomas Graf  *   nla_put_flag(skb, type)		add flag attribute to skb
1112175d87cSNicolas Dichtel  *   nla_put_msecs(skb, type, jiffies,
1122175d87cSNicolas Dichtel  *                 padattr)		add msecs attribute to skb
113930345eaSJiri Benc  *   nla_put_in_addr(skb, type, addr)	add IPv4 address attribute to skb
114930345eaSJiri Benc  *   nla_put_in6_addr(skb, type, addr)	add IPv6 address attribute to skb
115bfa83a9eSThomas Graf  *
116bfa83a9eSThomas Graf  * Nested Attributes Construction:
117bfa83a9eSThomas Graf  *   nla_nest_start(skb, type)		start a nested attribute
118bfa83a9eSThomas Graf  *   nla_nest_end(skb, nla)		finalize a nested attribute
119bfa83a9eSThomas Graf  *   nla_nest_cancel(skb, nla)		cancel nested attribute construction
120bfa83a9eSThomas Graf  *
121bfa83a9eSThomas Graf  * Attribute Length Calculations:
122bfa83a9eSThomas Graf  *   nla_attr_size(payload)		length of attribute w/o padding
123bfa83a9eSThomas Graf  *   nla_total_size(payload)		length of attribute w/ padding
124bfa83a9eSThomas Graf  *   nla_padlen(payload)		length of padding
125bfa83a9eSThomas Graf  *
126bfa83a9eSThomas Graf  * Attribute Payload Access:
127bfa83a9eSThomas Graf  *   nla_data(nla)			head of attribute payload
128bfa83a9eSThomas Graf  *   nla_len(nla)			length of attribute payload
129bfa83a9eSThomas Graf  *
130bfa83a9eSThomas Graf  * Attribute Payload Access for Basic Types:
131bfa83a9eSThomas Graf  *   nla_get_u8(nla)			get payload for a u8 attribute
132bfa83a9eSThomas Graf  *   nla_get_u16(nla)			get payload for a u16 attribute
133bfa83a9eSThomas Graf  *   nla_get_u32(nla)			get payload for a u32 attribute
134bfa83a9eSThomas Graf  *   nla_get_u64(nla)			get payload for a u64 attribute
1354778e0beSJiri Pirko  *   nla_get_s8(nla)			get payload for a s8 attribute
1364778e0beSJiri Pirko  *   nla_get_s16(nla)			get payload for a s16 attribute
1374778e0beSJiri Pirko  *   nla_get_s32(nla)			get payload for a s32 attribute
1384778e0beSJiri Pirko  *   nla_get_s64(nla)			get payload for a s64 attribute
139bfa83a9eSThomas Graf  *   nla_get_flag(nla)			return 1 if flag is true
140bfa83a9eSThomas Graf  *   nla_get_msecs(nla)			get payload for a msecs attribute
141bfa83a9eSThomas Graf  *
142bfa83a9eSThomas Graf  * Attribute Misc:
143bfa83a9eSThomas Graf  *   nla_memcpy(dest, nla, count)	copy attribute into memory
144bfa83a9eSThomas Graf  *   nla_memcmp(nla, data, size)	compare attribute with memory area
145872f6903SFrancis Laniel  *   nla_strscpy(dst, nla, size)	copy attribute to a sized string
146bfa83a9eSThomas Graf  *   nla_strcmp(nla, str)		compare attribute with string
147bfa83a9eSThomas Graf  *
148bfa83a9eSThomas Graf  * Attribute Parsing:
149bfa83a9eSThomas Graf  *   nla_ok(nla, remaining)		does nla fit into remaining bytes?
150bfa83a9eSThomas Graf  *   nla_next(nla, remaining)		get next netlink attribute
151bfa83a9eSThomas Graf  *   nla_validate()			validate a stream of attributes
1524fe5d5c0SPaul Moore  *   nla_validate_nested()		validate a stream of nested attributes
153bfa83a9eSThomas Graf  *   nla_find()				find attribute in stream of attributes
154fe4944e5SThomas Graf  *   nla_find_nested()			find attribute in nested attributes
155bfa83a9eSThomas Graf  *   nla_parse()			parse and validate stream of attrs
15643955a45SJohannes Berg  *   nla_parse_nested()			parse nested attributes
157bfa83a9eSThomas Graf  *   nla_for_each_attr()		loop over all attributes
15822acb19aSPaul Moore  *   nla_for_each_nested()		loop over the nested attributes
159bfa83a9eSThomas Graf  *=========================================================================
160bfa83a9eSThomas Graf  */
161bfa83a9eSThomas Graf 
162bfa83a9eSThomas Graf  /**
163bfa83a9eSThomas Graf   * Standard attribute types to specify validation policy
164bfa83a9eSThomas Graf   */
165bfa83a9eSThomas Graf enum {
166bfa83a9eSThomas Graf 	NLA_UNSPEC,
167bfa83a9eSThomas Graf 	NLA_U8,
168bfa83a9eSThomas Graf 	NLA_U16,
169bfa83a9eSThomas Graf 	NLA_U32,
170bfa83a9eSThomas Graf 	NLA_U64,
171bfa83a9eSThomas Graf 	NLA_STRING,
172bfa83a9eSThomas Graf 	NLA_FLAG,
173bfa83a9eSThomas Graf 	NLA_MSECS,
174bfa83a9eSThomas Graf 	NLA_NESTED,
1751501d135SJohannes Berg 	NLA_NESTED_ARRAY,
176a5531a5dSThomas Graf 	NLA_NUL_STRING,
177d30045a0SJohannes Berg 	NLA_BINARY,
1784778e0beSJiri Pirko 	NLA_S8,
1794778e0beSJiri Pirko 	NLA_S16,
1804778e0beSJiri Pirko 	NLA_S32,
1814778e0beSJiri Pirko 	NLA_S64,
18264c83d83SJamal Hadi Salim 	NLA_BITFIELD32,
183568b742aSJohannes Berg 	NLA_REJECT,
184ecaf75ffSFlorian Westphal 	NLA_BE16,
185ecaf75ffSFlorian Westphal 	NLA_BE32,
186bfa83a9eSThomas Graf 	__NLA_TYPE_MAX,
187bfa83a9eSThomas Graf };
188bfa83a9eSThomas Graf 
189bfa83a9eSThomas Graf #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
190bfa83a9eSThomas Graf 
191d06a09b9SJohannes Berg struct netlink_range_validation {
192d06a09b9SJohannes Berg 	u64 min, max;
193d06a09b9SJohannes Berg };
194d06a09b9SJohannes Berg 
195d06a09b9SJohannes Berg struct netlink_range_validation_signed {
196d06a09b9SJohannes Berg 	s64 min, max;
197d06a09b9SJohannes Berg };
198d06a09b9SJohannes Berg 
1993e48be05SJohannes Berg enum nla_policy_validation {
2003e48be05SJohannes Berg 	NLA_VALIDATE_NONE,
2013e48be05SJohannes Berg 	NLA_VALIDATE_RANGE,
2028aa26c57SJohannes Berg 	NLA_VALIDATE_RANGE_WARN_TOO_LONG,
2033e48be05SJohannes Berg 	NLA_VALIDATE_MIN,
2043e48be05SJohannes Berg 	NLA_VALIDATE_MAX,
205bdbb4e29SJakub Kicinski 	NLA_VALIDATE_MASK,
206d06a09b9SJohannes Berg 	NLA_VALIDATE_RANGE_PTR,
20733188bd6SJohannes Berg 	NLA_VALIDATE_FUNCTION,
2083e48be05SJohannes Berg };
2093e48be05SJohannes Berg 
210bfa83a9eSThomas Graf /**
211bfa83a9eSThomas Graf  * struct nla_policy - attribute validation policy
212bfa83a9eSThomas Graf  * @type: Type of attribute or NLA_UNSPEC
2133e48be05SJohannes Berg  * @validation_type: type of attribute validation done in addition to
21433188bd6SJohannes Berg  *	type-specific validation (e.g. range, function call), see
2153e48be05SJohannes Berg  *	&enum nla_policy_validation
216a5531a5dSThomas Graf  * @len: Type specific length of payload
217bfa83a9eSThomas Graf  *
218bfa83a9eSThomas Graf  * Policies are defined as arrays of this struct, the array must be
219bfa83a9eSThomas Graf  * accessible by attribute type up to the highest identifier to be expected.
220bfa83a9eSThomas Graf  *
221a5531a5dSThomas Graf  * Meaning of `len' field:
222a5531a5dSThomas Graf  *    NLA_STRING           Maximum length of string
223a5531a5dSThomas Graf  *    NLA_NUL_STRING       Maximum length of string (excluding NUL)
224a5531a5dSThomas Graf  *    NLA_FLAG             Unused
225d30045a0SJohannes Berg  *    NLA_BINARY           Maximum length of attribute payload
2268aa26c57SJohannes Berg  *                         (but see also below with the validation type)
2271501d135SJohannes Berg  *    NLA_NESTED,
2281501d135SJohannes Berg  *    NLA_NESTED_ARRAY     Length verification is done by checking len of
2299a659a35SJohannes Berg  *                         nested header (or empty); len field is used if
23047a1494bSJohannes Berg  *                         nested_policy is also used, for the max attr
2319a659a35SJohannes Berg  *                         number in the nested policy.
2324b6cc728SJohannes Berg  *    NLA_U8, NLA_U16,
2334b6cc728SJohannes Berg  *    NLA_U32, NLA_U64,
2344778e0beSJiri Pirko  *    NLA_S8, NLA_S16,
2354778e0beSJiri Pirko  *    NLA_S32, NLA_S64,
236ecaf75ffSFlorian Westphal  *    NLA_BE16, NLA_BE32,
2374b6cc728SJohannes Berg  *    NLA_MSECS            Leaving the length field zero will verify the
2384b6cc728SJohannes Berg  *                         given type fits, using it verifies minimum length
2394b6cc728SJohannes Berg  *                         just like "All other"
240568b742aSJohannes Berg  *    NLA_BITFIELD32       Unused
241568b742aSJohannes Berg  *    NLA_REJECT           Unused
2424b6cc728SJohannes Berg  *    All other            Minimum length of attribute payload
243a5531a5dSThomas Graf  *
24447a1494bSJohannes Berg  * Meaning of validation union:
245568b742aSJohannes Berg  *    NLA_BITFIELD32       This is a 32-bit bitmap/bitselector attribute and
24647a1494bSJohannes Berg  *                         `bitfield32_valid' is the u32 value of valid flags
24747a1494bSJohannes Berg  *    NLA_REJECT           This attribute is always rejected and `reject_message'
248568b742aSJohannes Berg  *                         may point to a string to report as the error instead
249568b742aSJohannes Berg  *                         of the generic one in extended ACK.
25047a1494bSJohannes Berg  *    NLA_NESTED           `nested_policy' to a nested policy to validate, must
25147a1494bSJohannes Berg  *                         also set `len' to the max attribute number. Use the
25247a1494bSJohannes Berg  *                         provided NLA_POLICY_NESTED() macro.
2539a659a35SJohannes Berg  *                         Note that nla_parse() will validate, but of course not
2549a659a35SJohannes Berg  *                         parse, the nested sub-policies.
25547a1494bSJohannes Berg  *    NLA_NESTED_ARRAY     `nested_policy' points to a nested policy to validate,
25647a1494bSJohannes Berg  *                         must also set `len' to the max attribute number. Use
25747a1494bSJohannes Berg  *                         the provided NLA_POLICY_NESTED_ARRAY() macro.
25847a1494bSJohannes Berg  *                         The difference to NLA_NESTED is the structure:
25947a1494bSJohannes Berg  *                         NLA_NESTED has the nested attributes directly inside
26047a1494bSJohannes Berg  *                         while an array has the nested attributes at another
26147a1494bSJohannes Berg  *                         level down and the attribute types directly in the
26247a1494bSJohannes Berg  *                         nesting don't matter.
2633e48be05SJohannes Berg  *    NLA_U8,
2643e48be05SJohannes Berg  *    NLA_U16,
2653e48be05SJohannes Berg  *    NLA_U32,
2663e48be05SJohannes Berg  *    NLA_U64,
267ecaf75ffSFlorian Westphal  *    NLA_BE16,
268ecaf75ffSFlorian Westphal  *    NLA_BE32,
2693e48be05SJohannes Berg  *    NLA_S8,
2703e48be05SJohannes Berg  *    NLA_S16,
2713e48be05SJohannes Berg  *    NLA_S32,
27247a1494bSJohannes Berg  *    NLA_S64              The `min' and `max' fields are used depending on the
27347a1494bSJohannes Berg  *                         validation_type field, if that is min/max/range then
27447a1494bSJohannes Berg  *                         the min, max or both are used (respectively) to check
2753e48be05SJohannes Berg  *                         the value of the integer attribute.
2763e48be05SJohannes Berg  *                         Note that in the interest of code simplicity and
2773e48be05SJohannes Berg  *                         struct size both limits are s16, so you cannot
2783e48be05SJohannes Berg  *                         enforce a range that doesn't fall within the range
27970eb3911SJohannes Berg  *                         of s16 - do that using the NLA_POLICY_FULL_RANGE()
28070eb3911SJohannes Berg  *                         or NLA_POLICY_FULL_RANGE_SIGNED() macros instead.
28147a1494bSJohannes Berg  *                         Use the NLA_POLICY_MIN(), NLA_POLICY_MAX() and
28247a1494bSJohannes Berg  *                         NLA_POLICY_RANGE() macros.
283d06a09b9SJohannes Berg  *    NLA_U8,
284d06a09b9SJohannes Berg  *    NLA_U16,
285d06a09b9SJohannes Berg  *    NLA_U32,
286d06a09b9SJohannes Berg  *    NLA_U64              If the validation_type field instead is set to
287d06a09b9SJohannes Berg  *                         NLA_VALIDATE_RANGE_PTR, `range' must be a pointer
288d06a09b9SJohannes Berg  *                         to a struct netlink_range_validation that indicates
289d06a09b9SJohannes Berg  *                         the min/max values.
290d06a09b9SJohannes Berg  *                         Use NLA_POLICY_FULL_RANGE().
291d06a09b9SJohannes Berg  *    NLA_S8,
292d06a09b9SJohannes Berg  *    NLA_S16,
293d06a09b9SJohannes Berg  *    NLA_S32,
294d06a09b9SJohannes Berg  *    NLA_S64              If the validation_type field instead is set to
295d06a09b9SJohannes Berg  *                         NLA_VALIDATE_RANGE_PTR, `range_signed' must be a
296d06a09b9SJohannes Berg  *                         pointer to a struct netlink_range_validation_signed
297d06a09b9SJohannes Berg  *                         that indicates the min/max values.
298d06a09b9SJohannes Berg  *                         Use NLA_POLICY_FULL_RANGE_SIGNED().
2998aa26c57SJohannes Berg  *
3008aa26c57SJohannes Berg  *    NLA_BINARY           If the validation type is like the ones for integers
3018aa26c57SJohannes Berg  *                         above, then the min/max length (not value like for
3028aa26c57SJohannes Berg  *                         integers) of the attribute is enforced.
3038aa26c57SJohannes Berg  *
3043e48be05SJohannes Berg  *    All other            Unused - but note that it's a union
305568b742aSJohannes Berg  *
30633188bd6SJohannes Berg  * Meaning of `validate' field, use via NLA_POLICY_VALIDATE_FN:
30747a1494bSJohannes Berg  *    NLA_BINARY           Validation function called for the attribute.
30833188bd6SJohannes Berg  *    All other            Unused - but note that it's a union
30933188bd6SJohannes Berg  *
310bfa83a9eSThomas Graf  * Example:
31147a1494bSJohannes Berg  *
31247a1494bSJohannes Berg  * static const u32 myvalidflags = 0xff231023;
31347a1494bSJohannes Berg  *
314b54452b0SAlexey Dobriyan  * static const struct nla_policy my_policy[ATTR_MAX+1] = {
315bfa83a9eSThomas Graf  * 	[ATTR_FOO] = { .type = NLA_U16 },
316d30045a0SJohannes Berg  *	[ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
3178aa26c57SJohannes Berg  *	[ATTR_BAZ] = NLA_POLICY_EXACT_LEN(sizeof(struct mystruct)),
31847a1494bSJohannes Berg  *	[ATTR_GOO] = NLA_POLICY_BITFIELD32(myvalidflags),
319bfa83a9eSThomas Graf  * };
320bfa83a9eSThomas Graf  */
321bfa83a9eSThomas Graf struct nla_policy {
3223e48be05SJohannes Berg 	u8		type;
3233e48be05SJohannes Berg 	u8		validation_type;
324a5531a5dSThomas Graf 	u16		len;
3253e48be05SJohannes Berg 	union {
3267354c902SJakub Kicinski 		/**
3277354c902SJakub Kicinski 		 * @strict_start_type: first attribute to validate strictly
3287354c902SJakub Kicinski 		 *
3297354c902SJakub Kicinski 		 * This entry is special, and used for the attribute at index 0
33056738f46SJohannes Berg 		 * only, and specifies special data about the policy, namely it
33156738f46SJohannes Berg 		 * specifies the "boundary type" where strict length validation
33256738f46SJohannes Berg 		 * starts for any attribute types >= this value, also, strict
33356738f46SJohannes Berg 		 * nesting validation starts here.
33456738f46SJohannes Berg 		 *
33556738f46SJohannes Berg 		 * Additionally, it means that NLA_UNSPEC is actually NLA_REJECT
3368aa26c57SJohannes Berg 		 * for any types >= this, so need to use NLA_POLICY_MIN_LEN() to
3378aa26c57SJohannes Berg 		 * get the previous pure { .len = xyz } behaviour. The advantage
3388aa26c57SJohannes Berg 		 * of this is that types not specified in the policy will be
3398aa26c57SJohannes Berg 		 * rejected.
34056738f46SJohannes Berg 		 *
34156738f46SJohannes Berg 		 * For completely new families it should be set to 1 so that the
34256738f46SJohannes Berg 		 * validation is enforced for all attributes. For existing ones
34356738f46SJohannes Berg 		 * it should be set at least when new attributes are added to
34456738f46SJohannes Berg 		 * the enum used by the policy, and be set to the new value that
34556738f46SJohannes Berg 		 * was added to enforce strict validation from thereon.
34656738f46SJohannes Berg 		 */
34756738f46SJohannes Berg 		u16 strict_start_type;
3487354c902SJakub Kicinski 
3497354c902SJakub Kicinski 		/* private: use NLA_POLICY_*() to set */
3507354c902SJakub Kicinski 		const u32 bitfield32_valid;
3517354c902SJakub Kicinski 		const u32 mask;
3527354c902SJakub Kicinski 		const char *reject_message;
3537354c902SJakub Kicinski 		const struct nla_policy *nested_policy;
3547354c902SJakub Kicinski 		struct netlink_range_validation *range;
3557354c902SJakub Kicinski 		struct netlink_range_validation_signed *range_signed;
3567354c902SJakub Kicinski 		struct {
3577354c902SJakub Kicinski 			s16 min, max;
3587354c902SJakub Kicinski 		};
3597354c902SJakub Kicinski 		int (*validate)(const struct nlattr *attr,
3607354c902SJakub Kicinski 				struct netlink_ext_ack *extack);
3613e48be05SJohannes Berg 	};
362bfa83a9eSThomas Graf };
363bfa83a9eSThomas Graf 
364b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR		NLA_POLICY_EXACT_LEN(ETH_ALEN)
365b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR_COMPAT	NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN)
366b60b87fcSJohannes Berg 
36723323289SJohannes Berg #define _NLA_POLICY_NESTED(maxattr, policy) \
36847a1494bSJohannes Berg 	{ .type = NLA_NESTED, .nested_policy = policy, .len = maxattr }
36923323289SJohannes Berg #define _NLA_POLICY_NESTED_ARRAY(maxattr, policy) \
37047a1494bSJohannes Berg 	{ .type = NLA_NESTED_ARRAY, .nested_policy = policy, .len = maxattr }
37123323289SJohannes Berg #define NLA_POLICY_NESTED(policy) \
37223323289SJohannes Berg 	_NLA_POLICY_NESTED(ARRAY_SIZE(policy) - 1, policy)
37323323289SJohannes Berg #define NLA_POLICY_NESTED_ARRAY(policy) \
37423323289SJohannes Berg 	_NLA_POLICY_NESTED_ARRAY(ARRAY_SIZE(policy) - 1, policy)
37547a1494bSJohannes Berg #define NLA_POLICY_BITFIELD32(valid) \
37647a1494bSJohannes Berg 	{ .type = NLA_BITFIELD32, .bitfield32_valid = valid }
3779a659a35SJohannes Berg 
378ddcf3b70SJakub Kicinski #define __NLA_IS_UINT_TYPE(tp)					\
379*5fac9b7cSFlorian Westphal 	(tp == NLA_U8 || tp == NLA_U16 || tp == NLA_U32 ||	\
380*5fac9b7cSFlorian Westphal 	 tp == NLA_U64 || tp == NLA_BE16 || tp == NLA_BE32)
381ddcf3b70SJakub Kicinski #define __NLA_IS_SINT_TYPE(tp)						\
382ddcf3b70SJakub Kicinski 	(tp == NLA_S8 || tp == NLA_S16 || tp == NLA_S32 || tp == NLA_S64)
383ddcf3b70SJakub Kicinski 
3845886d932SJohannes Berg #define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition))
385bdbb4e29SJakub Kicinski #define NLA_ENSURE_UINT_TYPE(tp)			\
386bdbb4e29SJakub Kicinski 	(__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp)) + tp)
3878aa26c57SJohannes Berg #define NLA_ENSURE_UINT_OR_BINARY_TYPE(tp)		\
388ddcf3b70SJakub Kicinski 	(__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) ||	\
3898aa26c57SJohannes Berg 		      tp == NLA_MSECS ||		\
3908aa26c57SJohannes Berg 		      tp == NLA_BINARY) + tp)
391d06a09b9SJohannes Berg #define NLA_ENSURE_SINT_TYPE(tp)			\
392ddcf3b70SJakub Kicinski 	(__NLA_ENSURE(__NLA_IS_SINT_TYPE(tp)) + tp)
3938aa26c57SJohannes Berg #define NLA_ENSURE_INT_OR_BINARY_TYPE(tp)		\
394ddcf3b70SJakub Kicinski 	(__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) ||		\
395ddcf3b70SJakub Kicinski 		      __NLA_IS_SINT_TYPE(tp) ||		\
3968aa26c57SJohannes Berg 		      tp == NLA_MSECS ||		\
3978aa26c57SJohannes Berg 		      tp == NLA_BINARY) + tp)
39833188bd6SJohannes Berg #define NLA_ENSURE_NO_VALIDATION_PTR(tp)		\
39933188bd6SJohannes Berg 	(__NLA_ENSURE(tp != NLA_BITFIELD32 &&		\
40033188bd6SJohannes Berg 		      tp != NLA_REJECT &&		\
40133188bd6SJohannes Berg 		      tp != NLA_NESTED &&		\
40233188bd6SJohannes Berg 		      tp != NLA_NESTED_ARRAY) + tp)
4033e48be05SJohannes Berg 
4043e48be05SJohannes Berg #define NLA_POLICY_RANGE(tp, _min, _max) {		\
4058aa26c57SJohannes Berg 	.type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp),	\
4063e48be05SJohannes Berg 	.validation_type = NLA_VALIDATE_RANGE,		\
4073e48be05SJohannes Berg 	.min = _min,					\
4083e48be05SJohannes Berg 	.max = _max					\
4093e48be05SJohannes Berg }
4103e48be05SJohannes Berg 
411d06a09b9SJohannes Berg #define NLA_POLICY_FULL_RANGE(tp, _range) {		\
4128aa26c57SJohannes Berg 	.type = NLA_ENSURE_UINT_OR_BINARY_TYPE(tp),	\
413d06a09b9SJohannes Berg 	.validation_type = NLA_VALIDATE_RANGE_PTR,	\
414d06a09b9SJohannes Berg 	.range = _range,				\
415d06a09b9SJohannes Berg }
416d06a09b9SJohannes Berg 
417d06a09b9SJohannes Berg #define NLA_POLICY_FULL_RANGE_SIGNED(tp, _range) {	\
418d06a09b9SJohannes Berg 	.type = NLA_ENSURE_SINT_TYPE(tp),		\
419d06a09b9SJohannes Berg 	.validation_type = NLA_VALIDATE_RANGE_PTR,	\
420d06a09b9SJohannes Berg 	.range_signed = _range,				\
421d06a09b9SJohannes Berg }
422d06a09b9SJohannes Berg 
4233e48be05SJohannes Berg #define NLA_POLICY_MIN(tp, _min) {			\
4248aa26c57SJohannes Berg 	.type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp),	\
4253e48be05SJohannes Berg 	.validation_type = NLA_VALIDATE_MIN,		\
4263e48be05SJohannes Berg 	.min = _min,					\
4273e48be05SJohannes Berg }
4283e48be05SJohannes Berg 
4293e48be05SJohannes Berg #define NLA_POLICY_MAX(tp, _max) {			\
4308aa26c57SJohannes Berg 	.type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp),	\
4313e48be05SJohannes Berg 	.validation_type = NLA_VALIDATE_MAX,		\
4323e48be05SJohannes Berg 	.max = _max,					\
4333e48be05SJohannes Berg }
4343e48be05SJohannes Berg 
435bdbb4e29SJakub Kicinski #define NLA_POLICY_MASK(tp, _mask) {			\
436bdbb4e29SJakub Kicinski 	.type = NLA_ENSURE_UINT_TYPE(tp),		\
437bdbb4e29SJakub Kicinski 	.validation_type = NLA_VALIDATE_MASK,		\
438bdbb4e29SJakub Kicinski 	.mask = _mask,					\
439bdbb4e29SJakub Kicinski }
440bdbb4e29SJakub Kicinski 
44133188bd6SJohannes Berg #define NLA_POLICY_VALIDATE_FN(tp, fn, ...) {		\
44233188bd6SJohannes Berg 	.type = NLA_ENSURE_NO_VALIDATION_PTR(tp),	\
44333188bd6SJohannes Berg 	.validation_type = NLA_VALIDATE_FUNCTION,	\
44433188bd6SJohannes Berg 	.validate = fn,					\
44533188bd6SJohannes Berg 	.len = __VA_ARGS__ + 0,				\
44633188bd6SJohannes Berg }
44733188bd6SJohannes Berg 
4488aa26c57SJohannes Berg #define NLA_POLICY_EXACT_LEN(_len)	NLA_POLICY_RANGE(NLA_BINARY, _len, _len)
4498aa26c57SJohannes Berg #define NLA_POLICY_EXACT_LEN_WARN(_len) {			\
4508aa26c57SJohannes Berg 	.type = NLA_BINARY,					\
4518aa26c57SJohannes Berg 	.validation_type = NLA_VALIDATE_RANGE_WARN_TOO_LONG,	\
4528aa26c57SJohannes Berg 	.min = _len,						\
4538aa26c57SJohannes Berg 	.max = _len						\
4548aa26c57SJohannes Berg }
4558aa26c57SJohannes Berg #define NLA_POLICY_MIN_LEN(_len)	NLA_POLICY_MIN(NLA_BINARY, _len)
4568aa26c57SJohannes Berg 
4574e902c57SThomas Graf /**
4584e902c57SThomas Graf  * struct nl_info - netlink source information
4594e902c57SThomas Graf  * @nlh: Netlink message header of original request
4603de205cdSIdo Schimmel  * @nl_net: Network namespace
46115e47304SEric W. Biederman  * @portid: Netlink PORTID of requesting application
4623de205cdSIdo Schimmel  * @skip_notify: Skip netlink notifications to user space
463c82481f7SIdo Schimmel  * @skip_notify_kernel: Skip selected in-kernel notifications
4644e902c57SThomas Graf  */
4654e902c57SThomas Graf struct nl_info {
4664e902c57SThomas Graf 	struct nlmsghdr		*nlh;
4674d1169c1SDenis V. Lunev 	struct net		*nl_net;
46815e47304SEric W. Biederman 	u32			portid;
469c82481f7SIdo Schimmel 	u8			skip_notify:1,
470c82481f7SIdo Schimmel 				skip_notify_kernel:1;
4714e902c57SThomas Graf };
4724e902c57SThomas Graf 
4738cb08174SJohannes Berg /**
4748cb08174SJohannes Berg  * enum netlink_validation - netlink message/attribute validation levels
4758cb08174SJohannes Berg  * @NL_VALIDATE_LIBERAL: Old-style "be liberal" validation, not caring about
4768cb08174SJohannes Berg  *	extra data at the end of the message, attributes being longer than
4778cb08174SJohannes Berg  *	they should be, or unknown attributes being present.
4788cb08174SJohannes Berg  * @NL_VALIDATE_TRAILING: Reject junk data encountered after attribute parsing.
4798cb08174SJohannes Berg  * @NL_VALIDATE_MAXTYPE: Reject attributes > max type; Together with _TRAILING
4808cb08174SJohannes Berg  *	this is equivalent to the old nla_parse_strict()/nlmsg_parse_strict().
4818cb08174SJohannes Berg  * @NL_VALIDATE_UNSPEC: Reject attributes with NLA_UNSPEC in the policy.
4828cb08174SJohannes Berg  *	This can safely be set by the kernel when the given policy has no
4838cb08174SJohannes Berg  *	NLA_UNSPEC anymore, and can thus be used to ensure policy entries
4848cb08174SJohannes Berg  *	are enforced going forward.
4858cb08174SJohannes Berg  * @NL_VALIDATE_STRICT_ATTRS: strict attribute policy parsing (e.g.
4868cb08174SJohannes Berg  *	U8, U16, U32 must have exact size, etc.)
487b424e432SMichal Kubecek  * @NL_VALIDATE_NESTED: Check that NLA_F_NESTED is set for NLA_NESTED(_ARRAY)
488b424e432SMichal Kubecek  *	and unset for other policies.
4898cb08174SJohannes Berg  */
4908cb08174SJohannes Berg enum netlink_validation {
4918cb08174SJohannes Berg 	NL_VALIDATE_LIBERAL = 0,
4928cb08174SJohannes Berg 	NL_VALIDATE_TRAILING = BIT(0),
4938cb08174SJohannes Berg 	NL_VALIDATE_MAXTYPE = BIT(1),
4948cb08174SJohannes Berg 	NL_VALIDATE_UNSPEC = BIT(2),
4958cb08174SJohannes Berg 	NL_VALIDATE_STRICT_ATTRS = BIT(3),
496b424e432SMichal Kubecek 	NL_VALIDATE_NESTED = BIT(4),
4978cb08174SJohannes Berg };
4988cb08174SJohannes Berg 
4998cb08174SJohannes Berg #define NL_VALIDATE_DEPRECATED_STRICT (NL_VALIDATE_TRAILING |\
5008cb08174SJohannes Berg 				       NL_VALIDATE_MAXTYPE)
5018cb08174SJohannes Berg #define NL_VALIDATE_STRICT (NL_VALIDATE_TRAILING |\
5028cb08174SJohannes Berg 			    NL_VALIDATE_MAXTYPE |\
5038cb08174SJohannes Berg 			    NL_VALIDATE_UNSPEC |\
504b424e432SMichal Kubecek 			    NL_VALIDATE_STRICT_ATTRS |\
505b424e432SMichal Kubecek 			    NL_VALIDATE_NESTED)
5068cb08174SJohannes Berg 
5074f69053bSJoe Perches int netlink_rcv_skb(struct sk_buff *skb,
5082d4bc933SJohannes Berg 		    int (*cb)(struct sk_buff *, struct nlmsghdr *,
5092d4bc933SJohannes Berg 			      struct netlink_ext_ack *));
5104f69053bSJoe Perches int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
5114f69053bSJoe Perches 		 unsigned int group, int report, gfp_t flags);
51282ace47aSThomas Graf 
5138cb08174SJohannes Berg int __nla_validate(const struct nlattr *head, int len, int maxtype,
5148cb08174SJohannes Berg 		   const struct nla_policy *policy, unsigned int validate,
515fceb6435SJohannes Berg 		   struct netlink_ext_ack *extack);
5168cb08174SJohannes Berg int __nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
5178cb08174SJohannes Berg 		int len, const struct nla_policy *policy, unsigned int validate,
518a5f6cba2SDavid Ahern 		struct netlink_ext_ack *extack);
5194f69053bSJoe Perches int nla_policy_len(const struct nla_policy *, int);
5204f69053bSJoe Perches struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
521872f6903SFrancis Laniel ssize_t nla_strscpy(char *dst, const struct nlattr *nla, size_t dstsize);
5222cf0c8b3SPhil Sutter char *nla_strdup(const struct nlattr *nla, gfp_t flags);
5234f69053bSJoe Perches int nla_memcpy(void *dest, const struct nlattr *src, int count);
5244f69053bSJoe Perches int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
5254f69053bSJoe Perches int nla_strcmp(const struct nlattr *nla, const char *str);
5264f69053bSJoe Perches struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
527089bf1a6SNicolas Dichtel struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
528089bf1a6SNicolas Dichtel 				   int attrlen, int padattr);
5294f69053bSJoe Perches void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
5304f69053bSJoe Perches struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
531089bf1a6SNicolas Dichtel struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype,
532089bf1a6SNicolas Dichtel 				 int attrlen, int padattr);
5334f69053bSJoe Perches void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
5344f69053bSJoe Perches void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
535fe4944e5SThomas Graf 	       const void *data);
536089bf1a6SNicolas Dichtel void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
537089bf1a6SNicolas Dichtel 		     const void *data, int padattr);
5384f69053bSJoe Perches void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
5394f69053bSJoe Perches int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);
540089bf1a6SNicolas Dichtel int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
541089bf1a6SNicolas Dichtel 		  const void *data, int padattr);
5424f69053bSJoe Perches int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
5434f69053bSJoe Perches int nla_append(struct sk_buff *skb, int attrlen, const void *data);
544bfa83a9eSThomas Graf 
545bfa83a9eSThomas Graf /**************************************************************************
546bfa83a9eSThomas Graf  * Netlink Messages
547bfa83a9eSThomas Graf  **************************************************************************/
548bfa83a9eSThomas Graf 
549bfa83a9eSThomas Graf /**
550bfa83a9eSThomas Graf  * nlmsg_msg_size - length of netlink message not including padding
551bfa83a9eSThomas Graf  * @payload: length of message payload
552bfa83a9eSThomas Graf  */
nlmsg_msg_size(int payload)553bfa83a9eSThomas Graf static inline int nlmsg_msg_size(int payload)
554bfa83a9eSThomas Graf {
555bfa83a9eSThomas Graf 	return NLMSG_HDRLEN + payload;
556bfa83a9eSThomas Graf }
557bfa83a9eSThomas Graf 
558bfa83a9eSThomas Graf /**
559bfa83a9eSThomas Graf  * nlmsg_total_size - length of netlink message including padding
560bfa83a9eSThomas Graf  * @payload: length of message payload
561bfa83a9eSThomas Graf  */
nlmsg_total_size(int payload)562bfa83a9eSThomas Graf static inline int nlmsg_total_size(int payload)
563bfa83a9eSThomas Graf {
564bfa83a9eSThomas Graf 	return NLMSG_ALIGN(nlmsg_msg_size(payload));
565bfa83a9eSThomas Graf }
566bfa83a9eSThomas Graf 
567bfa83a9eSThomas Graf /**
568bfa83a9eSThomas Graf  * nlmsg_padlen - length of padding at the message's tail
569bfa83a9eSThomas Graf  * @payload: length of message payload
570bfa83a9eSThomas Graf  */
nlmsg_padlen(int payload)571bfa83a9eSThomas Graf static inline int nlmsg_padlen(int payload)
572bfa83a9eSThomas Graf {
573bfa83a9eSThomas Graf 	return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
574bfa83a9eSThomas Graf }
575bfa83a9eSThomas Graf 
576bfa83a9eSThomas Graf /**
577bfa83a9eSThomas Graf  * nlmsg_data - head of message payload
57870f23fd6SJustin P. Mattock  * @nlh: netlink message header
579bfa83a9eSThomas Graf  */
nlmsg_data(const struct nlmsghdr * nlh)580bfa83a9eSThomas Graf static inline void *nlmsg_data(const struct nlmsghdr *nlh)
581bfa83a9eSThomas Graf {
582bfa83a9eSThomas Graf 	return (unsigned char *) nlh + NLMSG_HDRLEN;
583bfa83a9eSThomas Graf }
584bfa83a9eSThomas Graf 
585bfa83a9eSThomas Graf /**
586bfa83a9eSThomas Graf  * nlmsg_len - length of message payload
587bfa83a9eSThomas Graf  * @nlh: netlink message header
588bfa83a9eSThomas Graf  */
nlmsg_len(const struct nlmsghdr * nlh)589bfa83a9eSThomas Graf static inline int nlmsg_len(const struct nlmsghdr *nlh)
590bfa83a9eSThomas Graf {
591bfa83a9eSThomas Graf 	return nlh->nlmsg_len - NLMSG_HDRLEN;
592bfa83a9eSThomas Graf }
593bfa83a9eSThomas Graf 
594bfa83a9eSThomas Graf /**
595bfa83a9eSThomas Graf  * nlmsg_attrdata - head of attributes data
596bfa83a9eSThomas Graf  * @nlh: netlink message header
597bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
598bfa83a9eSThomas Graf  */
nlmsg_attrdata(const struct nlmsghdr * nlh,int hdrlen)599bfa83a9eSThomas Graf static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
600bfa83a9eSThomas Graf 					    int hdrlen)
601bfa83a9eSThomas Graf {
602bfa83a9eSThomas Graf 	unsigned char *data = nlmsg_data(nlh);
603bfa83a9eSThomas Graf 	return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
604bfa83a9eSThomas Graf }
605bfa83a9eSThomas Graf 
606bfa83a9eSThomas Graf /**
607bfa83a9eSThomas Graf  * nlmsg_attrlen - length of attributes data
608bfa83a9eSThomas Graf  * @nlh: netlink message header
609bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
610bfa83a9eSThomas Graf  */
nlmsg_attrlen(const struct nlmsghdr * nlh,int hdrlen)611bfa83a9eSThomas Graf static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
612bfa83a9eSThomas Graf {
613bfa83a9eSThomas Graf 	return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
614bfa83a9eSThomas Graf }
615bfa83a9eSThomas Graf 
616bfa83a9eSThomas Graf /**
617bfa83a9eSThomas Graf  * nlmsg_ok - check if the netlink message fits into the remaining bytes
618bfa83a9eSThomas Graf  * @nlh: netlink message header
619bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in message stream
620bfa83a9eSThomas Graf  */
nlmsg_ok(const struct nlmsghdr * nlh,int remaining)621bfa83a9eSThomas Graf static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
622bfa83a9eSThomas Graf {
623619e803dSVegard Nossum 	return (remaining >= (int) sizeof(struct nlmsghdr) &&
624bfa83a9eSThomas Graf 		nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
625bfa83a9eSThomas Graf 		nlh->nlmsg_len <= remaining);
626bfa83a9eSThomas Graf }
627bfa83a9eSThomas Graf 
628bfa83a9eSThomas Graf /**
629bfa83a9eSThomas Graf  * nlmsg_next - next netlink message in message stream
630bfa83a9eSThomas Graf  * @nlh: netlink message header
631bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in message stream
632bfa83a9eSThomas Graf  *
633bfa83a9eSThomas Graf  * Returns the next netlink message in the message stream and
634bfa83a9eSThomas Graf  * decrements remaining by the size of the current message.
635bfa83a9eSThomas Graf  */
6363654654fSJan Engelhardt static inline struct nlmsghdr *
nlmsg_next(const struct nlmsghdr * nlh,int * remaining)6373654654fSJan Engelhardt nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
638bfa83a9eSThomas Graf {
639bfa83a9eSThomas Graf 	int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
640bfa83a9eSThomas Graf 
641bfa83a9eSThomas Graf 	*remaining -= totlen;
642bfa83a9eSThomas Graf 
643bfa83a9eSThomas Graf 	return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
644bfa83a9eSThomas Graf }
645bfa83a9eSThomas Graf 
646bfa83a9eSThomas Graf /**
6473de64403SJohannes Berg  * nla_parse - Parse a stream of attributes into a tb buffer
6483de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
6493de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
6503de64403SJohannes Berg  * @head: head of attribute stream
6513de64403SJohannes Berg  * @len: length of attribute stream
6523de64403SJohannes Berg  * @policy: validation policy
6533de64403SJohannes Berg  * @extack: extended ACK pointer
6543de64403SJohannes Berg  *
6553de64403SJohannes Berg  * Parses a stream of attributes and stores a pointer to each attribute in
6563de64403SJohannes Berg  * the tb array accessible via the attribute type. Attributes with a type
6573de64403SJohannes Berg  * exceeding maxtype will be rejected, policy must be specified, attributes
6583de64403SJohannes Berg  * will be validated in the strictest way possible.
6593de64403SJohannes Berg  *
6603de64403SJohannes Berg  * Returns 0 on success or a negative error code.
6613de64403SJohannes Berg  */
nla_parse(struct nlattr ** tb,int maxtype,const struct nlattr * head,int len,const struct nla_policy * policy,struct netlink_ext_ack * extack)6623de64403SJohannes Berg static inline int nla_parse(struct nlattr **tb, int maxtype,
6633de64403SJohannes Berg 			    const struct nlattr *head, int len,
6643de64403SJohannes Berg 			    const struct nla_policy *policy,
6653de64403SJohannes Berg 			    struct netlink_ext_ack *extack)
6663de64403SJohannes Berg {
6673de64403SJohannes Berg 	return __nla_parse(tb, maxtype, head, len, policy,
6683de64403SJohannes Berg 			   NL_VALIDATE_STRICT, extack);
6693de64403SJohannes Berg }
6703de64403SJohannes Berg 
6713de64403SJohannes Berg /**
6728cb08174SJohannes Berg  * nla_parse_deprecated - Parse a stream of attributes into a tb buffer
6738cb08174SJohannes Berg  * @tb: destination array with maxtype+1 elements
6748cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
6758cb08174SJohannes Berg  * @head: head of attribute stream
6768cb08174SJohannes Berg  * @len: length of attribute stream
6778cb08174SJohannes Berg  * @policy: validation policy
6788cb08174SJohannes Berg  * @extack: extended ACK pointer
6798cb08174SJohannes Berg  *
6808cb08174SJohannes Berg  * Parses a stream of attributes and stores a pointer to each attribute in
6818cb08174SJohannes Berg  * the tb array accessible via the attribute type. Attributes with a type
6828cb08174SJohannes Berg  * exceeding maxtype will be ignored and attributes from the policy are not
6838cb08174SJohannes Berg  * always strictly validated (only for new attributes).
6848cb08174SJohannes Berg  *
6858cb08174SJohannes Berg  * Returns 0 on success or a negative error code.
6868cb08174SJohannes Berg  */
nla_parse_deprecated(struct nlattr ** tb,int maxtype,const struct nlattr * head,int len,const struct nla_policy * policy,struct netlink_ext_ack * extack)6878cb08174SJohannes Berg static inline int nla_parse_deprecated(struct nlattr **tb, int maxtype,
6888cb08174SJohannes Berg 				       const struct nlattr *head, int len,
6898cb08174SJohannes Berg 				       const struct nla_policy *policy,
6908cb08174SJohannes Berg 				       struct netlink_ext_ack *extack)
6918cb08174SJohannes Berg {
6928cb08174SJohannes Berg 	return __nla_parse(tb, maxtype, head, len, policy,
6938cb08174SJohannes Berg 			   NL_VALIDATE_LIBERAL, extack);
6948cb08174SJohannes Berg }
6958cb08174SJohannes Berg 
6968cb08174SJohannes Berg /**
6978cb08174SJohannes Berg  * nla_parse_deprecated_strict - Parse a stream of attributes into a tb buffer
6988cb08174SJohannes Berg  * @tb: destination array with maxtype+1 elements
6998cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
7008cb08174SJohannes Berg  * @head: head of attribute stream
7018cb08174SJohannes Berg  * @len: length of attribute stream
7028cb08174SJohannes Berg  * @policy: validation policy
7038cb08174SJohannes Berg  * @extack: extended ACK pointer
7048cb08174SJohannes Berg  *
7058cb08174SJohannes Berg  * Parses a stream of attributes and stores a pointer to each attribute in
7068cb08174SJohannes Berg  * the tb array accessible via the attribute type. Attributes with a type
7078cb08174SJohannes Berg  * exceeding maxtype will be rejected as well as trailing data, but the
7088cb08174SJohannes Berg  * policy is not completely strictly validated (only for new attributes).
7098cb08174SJohannes Berg  *
7108cb08174SJohannes Berg  * Returns 0 on success or a negative error code.
7118cb08174SJohannes Berg  */
nla_parse_deprecated_strict(struct nlattr ** tb,int maxtype,const struct nlattr * head,int len,const struct nla_policy * policy,struct netlink_ext_ack * extack)7128cb08174SJohannes Berg static inline int nla_parse_deprecated_strict(struct nlattr **tb, int maxtype,
7138cb08174SJohannes Berg 					      const struct nlattr *head,
7148cb08174SJohannes Berg 					      int len,
7158cb08174SJohannes Berg 					      const struct nla_policy *policy,
7168cb08174SJohannes Berg 					      struct netlink_ext_ack *extack)
7178cb08174SJohannes Berg {
7188cb08174SJohannes Berg 	return __nla_parse(tb, maxtype, head, len, policy,
7198cb08174SJohannes Berg 			   NL_VALIDATE_DEPRECATED_STRICT, extack);
7208cb08174SJohannes Berg }
7218cb08174SJohannes Berg 
7228cb08174SJohannes Berg /**
7238cb08174SJohannes Berg  * __nlmsg_parse - parse attributes of a netlink message
724bfa83a9eSThomas Graf  * @nlh: netlink message header
725bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
726bfa83a9eSThomas Graf  * @tb: destination array with maxtype+1 elements
727bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
728bfa83a9eSThomas Graf  * @policy: validation policy
7298cb08174SJohannes Berg  * @validate: validation strictness
730fceb6435SJohannes Berg  * @extack: extended ACK report struct
731bfa83a9eSThomas Graf  *
732bfa83a9eSThomas Graf  * See nla_parse()
733bfa83a9eSThomas Graf  */
__nlmsg_parse(const struct nlmsghdr * nlh,int hdrlen,struct nlattr * tb[],int maxtype,const struct nla_policy * policy,unsigned int validate,struct netlink_ext_ack * extack)7348cb08174SJohannes Berg static inline int __nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
735bfa83a9eSThomas Graf 				struct nlattr *tb[], int maxtype,
736fceb6435SJohannes Berg 				const struct nla_policy *policy,
7378cb08174SJohannes Berg 				unsigned int validate,
738fceb6435SJohannes Berg 				struct netlink_ext_ack *extack)
739bfa83a9eSThomas Graf {
7403d0d4337SDavid Ahern 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) {
7413d0d4337SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid header length");
742bfa83a9eSThomas Graf 		return -EINVAL;
7433d0d4337SDavid Ahern 	}
744bfa83a9eSThomas Graf 
7458cb08174SJohannes Berg 	return __nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
7468cb08174SJohannes Berg 			   nlmsg_attrlen(nlh, hdrlen), policy, validate,
7478cb08174SJohannes Berg 			   extack);
748bfa83a9eSThomas Graf }
749bfa83a9eSThomas Graf 
7508cb08174SJohannes Berg /**
7513de64403SJohannes Berg  * nlmsg_parse - parse attributes of a netlink message
7523de64403SJohannes Berg  * @nlh: netlink message header
7533de64403SJohannes Berg  * @hdrlen: length of family specific header
7543de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
7553de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
7560bf73255SZhengchao Shao  * @policy: validation policy
7573de64403SJohannes Berg  * @extack: extended ACK report struct
7583de64403SJohannes Berg  *
7593de64403SJohannes Berg  * See nla_parse()
7603de64403SJohannes Berg  */
nlmsg_parse(const struct nlmsghdr * nlh,int hdrlen,struct nlattr * tb[],int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)7613de64403SJohannes Berg static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
7623de64403SJohannes Berg 			      struct nlattr *tb[], int maxtype,
7633de64403SJohannes Berg 			      const struct nla_policy *policy,
7643de64403SJohannes Berg 			      struct netlink_ext_ack *extack)
7653de64403SJohannes Berg {
766d00ee64eSDavid Ahern 	return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
7673de64403SJohannes Berg 			     NL_VALIDATE_STRICT, extack);
7683de64403SJohannes Berg }
7693de64403SJohannes Berg 
7703de64403SJohannes Berg /**
7718cb08174SJohannes Berg  * nlmsg_parse_deprecated - parse attributes of a netlink message
7728cb08174SJohannes Berg  * @nlh: netlink message header
7738cb08174SJohannes Berg  * @hdrlen: length of family specific header
7748cb08174SJohannes Berg  * @tb: destination array with maxtype+1 elements
7758cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
7760bf73255SZhengchao Shao  * @policy: validation policy
7778cb08174SJohannes Berg  * @extack: extended ACK report struct
7788cb08174SJohannes Berg  *
7798cb08174SJohannes Berg  * See nla_parse_deprecated()
7808cb08174SJohannes Berg  */
nlmsg_parse_deprecated(const struct nlmsghdr * nlh,int hdrlen,struct nlattr * tb[],int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)7818cb08174SJohannes Berg static inline int nlmsg_parse_deprecated(const struct nlmsghdr *nlh, int hdrlen,
782a5f6cba2SDavid Ahern 					 struct nlattr *tb[], int maxtype,
783a5f6cba2SDavid Ahern 					 const struct nla_policy *policy,
784a5f6cba2SDavid Ahern 					 struct netlink_ext_ack *extack)
785a5f6cba2SDavid Ahern {
7868cb08174SJohannes Berg 	return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
7878cb08174SJohannes Berg 			     NL_VALIDATE_LIBERAL, extack);
788a5f6cba2SDavid Ahern }
789a5f6cba2SDavid Ahern 
7908cb08174SJohannes Berg /**
7918cb08174SJohannes Berg  * nlmsg_parse_deprecated_strict - parse attributes of a netlink message
7928cb08174SJohannes Berg  * @nlh: netlink message header
7938cb08174SJohannes Berg  * @hdrlen: length of family specific header
7948cb08174SJohannes Berg  * @tb: destination array with maxtype+1 elements
7958cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
7960bf73255SZhengchao Shao  * @policy: validation policy
7978cb08174SJohannes Berg  * @extack: extended ACK report struct
7988cb08174SJohannes Berg  *
7998cb08174SJohannes Berg  * See nla_parse_deprecated_strict()
8008cb08174SJohannes Berg  */
8018cb08174SJohannes Berg static inline int
nlmsg_parse_deprecated_strict(const struct nlmsghdr * nlh,int hdrlen,struct nlattr * tb[],int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)8028cb08174SJohannes Berg nlmsg_parse_deprecated_strict(const struct nlmsghdr *nlh, int hdrlen,
8038cb08174SJohannes Berg 			      struct nlattr *tb[], int maxtype,
8048cb08174SJohannes Berg 			      const struct nla_policy *policy,
8058cb08174SJohannes Berg 			      struct netlink_ext_ack *extack)
8068cb08174SJohannes Berg {
8078cb08174SJohannes Berg 	return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
8088cb08174SJohannes Berg 			     NL_VALIDATE_DEPRECATED_STRICT, extack);
809a5f6cba2SDavid Ahern }
810a5f6cba2SDavid Ahern 
811bfa83a9eSThomas Graf /**
812bfa83a9eSThomas Graf  * nlmsg_find_attr - find a specific attribute in a netlink message
813bfa83a9eSThomas Graf  * @nlh: netlink message header
814bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
815bfa83a9eSThomas Graf  * @attrtype: type of attribute to look for
816bfa83a9eSThomas Graf  *
817bfa83a9eSThomas Graf  * Returns the first attribute which matches the specified type.
818bfa83a9eSThomas Graf  */
nlmsg_find_attr(const struct nlmsghdr * nlh,int hdrlen,int attrtype)8196b8c92baSNelson Elhage static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
820bfa83a9eSThomas Graf 					     int hdrlen, int attrtype)
821bfa83a9eSThomas Graf {
822bfa83a9eSThomas Graf 	return nla_find(nlmsg_attrdata(nlh, hdrlen),
823bfa83a9eSThomas Graf 			nlmsg_attrlen(nlh, hdrlen), attrtype);
824bfa83a9eSThomas Graf }
825bfa83a9eSThomas Graf 
826bfa83a9eSThomas Graf /**
8278cb08174SJohannes Berg  * nla_validate_deprecated - Validate a stream of attributes
8288cb08174SJohannes Berg  * @head: head of attribute stream
8298cb08174SJohannes Berg  * @len: length of attribute stream
8308cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
8318cb08174SJohannes Berg  * @policy: validation policy
8328cb08174SJohannes Berg  * @extack: extended ACK report struct
8338cb08174SJohannes Berg  *
8348cb08174SJohannes Berg  * Validates all attributes in the specified attribute stream against the
8358cb08174SJohannes Berg  * specified policy. Validation is done in liberal mode.
8368cb08174SJohannes Berg  * See documenation of struct nla_policy for more details.
8378cb08174SJohannes Berg  *
8388cb08174SJohannes Berg  * Returns 0 on success or a negative error code.
8398cb08174SJohannes Berg  */
nla_validate_deprecated(const struct nlattr * head,int len,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)8408cb08174SJohannes Berg static inline int nla_validate_deprecated(const struct nlattr *head, int len,
8418cb08174SJohannes Berg 					  int maxtype,
8428cb08174SJohannes Berg 					  const struct nla_policy *policy,
8438cb08174SJohannes Berg 					  struct netlink_ext_ack *extack)
8448cb08174SJohannes Berg {
8458cb08174SJohannes Berg 	return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_LIBERAL,
8468cb08174SJohannes Berg 			      extack);
8478cb08174SJohannes Berg }
8488cb08174SJohannes Berg 
8493de64403SJohannes Berg /**
8503de64403SJohannes Berg  * nla_validate - Validate a stream of attributes
8513de64403SJohannes Berg  * @head: head of attribute stream
8523de64403SJohannes Berg  * @len: length of attribute stream
8533de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
8543de64403SJohannes Berg  * @policy: validation policy
8553de64403SJohannes Berg  * @extack: extended ACK report struct
8563de64403SJohannes Berg  *
8573de64403SJohannes Berg  * Validates all attributes in the specified attribute stream against the
8583de64403SJohannes Berg  * specified policy. Validation is done in strict mode.
8593de64403SJohannes Berg  * See documenation of struct nla_policy for more details.
8603de64403SJohannes Berg  *
8613de64403SJohannes Berg  * Returns 0 on success or a negative error code.
8623de64403SJohannes Berg  */
nla_validate(const struct nlattr * head,int len,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)8633de64403SJohannes Berg static inline int nla_validate(const struct nlattr *head, int len, int maxtype,
8643de64403SJohannes Berg 			       const struct nla_policy *policy,
8653de64403SJohannes Berg 			       struct netlink_ext_ack *extack)
8663de64403SJohannes Berg {
8673de64403SJohannes Berg 	return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_STRICT,
8683de64403SJohannes Berg 			      extack);
8693de64403SJohannes Berg }
8708cb08174SJohannes Berg 
8718cb08174SJohannes Berg /**
8728cb08174SJohannes Berg  * nlmsg_validate_deprecated - validate a netlink message including attributes
873bfa83a9eSThomas Graf  * @nlh: netlinket message header
874bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
875bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
876bfa83a9eSThomas Graf  * @policy: validation policy
877fceb6435SJohannes Berg  * @extack: extended ACK report struct
878bfa83a9eSThomas Graf  */
nlmsg_validate_deprecated(const struct nlmsghdr * nlh,int hdrlen,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)8798cb08174SJohannes Berg static inline int nlmsg_validate_deprecated(const struct nlmsghdr *nlh,
8803654654fSJan Engelhardt 					    int hdrlen, int maxtype,
881fceb6435SJohannes Berg 					    const struct nla_policy *policy,
882fceb6435SJohannes Berg 					    struct netlink_ext_ack *extack)
883bfa83a9eSThomas Graf {
884bfa83a9eSThomas Graf 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
885bfa83a9eSThomas Graf 		return -EINVAL;
886bfa83a9eSThomas Graf 
8878cb08174SJohannes Berg 	return __nla_validate(nlmsg_attrdata(nlh, hdrlen),
8888cb08174SJohannes Berg 			      nlmsg_attrlen(nlh, hdrlen), maxtype,
8898cb08174SJohannes Berg 			      policy, NL_VALIDATE_LIBERAL, extack);
890bfa83a9eSThomas Graf }
891bfa83a9eSThomas Graf 
8928cb08174SJohannes Berg 
8938cb08174SJohannes Berg 
894bfa83a9eSThomas Graf /**
89597676b6bSThomas Graf  * nlmsg_report - need to report back to application?
89697676b6bSThomas Graf  * @nlh: netlink message header
89797676b6bSThomas Graf  *
89897676b6bSThomas Graf  * Returns 1 if a report back to the application is requested.
89997676b6bSThomas Graf  */
nlmsg_report(const struct nlmsghdr * nlh)9003a6c2b41SPatrick McHardy static inline int nlmsg_report(const struct nlmsghdr *nlh)
90197676b6bSThomas Graf {
902f9b282b3SYajun Deng 	return nlh ? !!(nlh->nlmsg_flags & NLM_F_ECHO) : 0;
90397676b6bSThomas Graf }
90497676b6bSThomas Graf 
90597676b6bSThomas Graf /**
9061d997f10SHangbin Liu  * nlmsg_seq - return the seq number of netlink message
9071d997f10SHangbin Liu  * @nlh: netlink message header
9081d997f10SHangbin Liu  *
9091d997f10SHangbin Liu  * Returns 0 if netlink message is NULL
9101d997f10SHangbin Liu  */
nlmsg_seq(const struct nlmsghdr * nlh)9111d997f10SHangbin Liu static inline u32 nlmsg_seq(const struct nlmsghdr *nlh)
9121d997f10SHangbin Liu {
9131d997f10SHangbin Liu 	return nlh ? nlh->nlmsg_seq : 0;
9141d997f10SHangbin Liu }
9151d997f10SHangbin Liu 
9161d997f10SHangbin Liu /**
917bfa83a9eSThomas Graf  * nlmsg_for_each_attr - iterate over a stream of attributes
918bfa83a9eSThomas Graf  * @pos: loop counter, set to current attribute
919bfa83a9eSThomas Graf  * @nlh: netlink message header
920bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
921bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
922bfa83a9eSThomas Graf  */
923bfa83a9eSThomas Graf #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
924bfa83a9eSThomas Graf 	nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
925bfa83a9eSThomas Graf 			  nlmsg_attrlen(nlh, hdrlen), rem)
926bfa83a9eSThomas Graf 
927bfa83a9eSThomas Graf /**
928bfa83a9eSThomas Graf  * nlmsg_put - Add a new netlink message to an skb
929bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
9302c6ba4b1SNicolas Dichtel  * @portid: netlink PORTID of requesting application
931bfa83a9eSThomas Graf  * @seq: sequence number of message
932bfa83a9eSThomas Graf  * @type: message type
933bfa83a9eSThomas Graf  * @payload: length of message payload
934bfa83a9eSThomas Graf  * @flags: message flags
935bfa83a9eSThomas Graf  *
936bfa83a9eSThomas Graf  * Returns NULL if the tailroom of the skb is insufficient to store
937bfa83a9eSThomas Graf  * the message header and payload.
938bfa83a9eSThomas Graf  */
nlmsg_put(struct sk_buff * skb,u32 portid,u32 seq,int type,int payload,int flags)93915e47304SEric W. Biederman static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
940bfa83a9eSThomas Graf 					 int type, int payload, int flags)
941bfa83a9eSThomas Graf {
942bfa83a9eSThomas Graf 	if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
943bfa83a9eSThomas Graf 		return NULL;
944bfa83a9eSThomas Graf 
94515e47304SEric W. Biederman 	return __nlmsg_put(skb, portid, seq, type, payload, flags);
946bfa83a9eSThomas Graf }
947bfa83a9eSThomas Graf 
948bfa83a9eSThomas Graf /**
949738136a0SJakub Kicinski  * nlmsg_append - Add more data to a nlmsg in a skb
950738136a0SJakub Kicinski  * @skb: socket buffer to store message in
951738136a0SJakub Kicinski  * @size: length of message payload
952738136a0SJakub Kicinski  *
953738136a0SJakub Kicinski  * Append data to an existing nlmsg, used when constructing a message
954738136a0SJakub Kicinski  * with multiple fixed-format headers (which is rare).
955738136a0SJakub Kicinski  * Returns NULL if the tailroom of the skb is insufficient to store
956738136a0SJakub Kicinski  * the extra payload.
957738136a0SJakub Kicinski  */
nlmsg_append(struct sk_buff * skb,u32 size)958738136a0SJakub Kicinski static inline void *nlmsg_append(struct sk_buff *skb, u32 size)
959738136a0SJakub Kicinski {
960738136a0SJakub Kicinski 	if (unlikely(skb_tailroom(skb) < NLMSG_ALIGN(size)))
961738136a0SJakub Kicinski 		return NULL;
962738136a0SJakub Kicinski 
963738136a0SJakub Kicinski 	if (NLMSG_ALIGN(size) - size)
964738136a0SJakub Kicinski 		memset(skb_tail_pointer(skb) + size, 0,
965738136a0SJakub Kicinski 		       NLMSG_ALIGN(size) - size);
966738136a0SJakub Kicinski 	return __skb_put(skb, NLMSG_ALIGN(size));
967738136a0SJakub Kicinski }
968738136a0SJakub Kicinski 
969738136a0SJakub Kicinski /**
970bfa83a9eSThomas Graf  * nlmsg_put_answer - Add a new callback based netlink message to an skb
971bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
972bfa83a9eSThomas Graf  * @cb: netlink callback
973bfa83a9eSThomas Graf  * @type: message type
974bfa83a9eSThomas Graf  * @payload: length of message payload
975bfa83a9eSThomas Graf  * @flags: message flags
976bfa83a9eSThomas Graf  *
977bfa83a9eSThomas Graf  * Returns NULL if the tailroom of the skb is insufficient to store
978bfa83a9eSThomas Graf  * the message header and payload.
979bfa83a9eSThomas Graf  */
nlmsg_put_answer(struct sk_buff * skb,struct netlink_callback * cb,int type,int payload,int flags)980bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
981bfa83a9eSThomas Graf 						struct netlink_callback *cb,
982bfa83a9eSThomas Graf 						int type, int payload,
983bfa83a9eSThomas Graf 						int flags)
984bfa83a9eSThomas Graf {
98515e47304SEric W. Biederman 	return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
986bfa83a9eSThomas Graf 			 type, payload, flags);
987bfa83a9eSThomas Graf }
988bfa83a9eSThomas Graf 
989bfa83a9eSThomas Graf /**
990bfa83a9eSThomas Graf  * nlmsg_new - Allocate a new netlink message
991339bf98fSThomas Graf  * @payload: size of the message payload
992fe4944e5SThomas Graf  * @flags: the type of memory to allocate.
993bfa83a9eSThomas Graf  *
994339bf98fSThomas Graf  * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
995339bf98fSThomas Graf  * and a good default is needed.
996bfa83a9eSThomas Graf  */
nlmsg_new(size_t payload,gfp_t flags)997339bf98fSThomas Graf static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
998bfa83a9eSThomas Graf {
999339bf98fSThomas Graf 	return alloc_skb(nlmsg_total_size(payload), flags);
1000bfa83a9eSThomas Graf }
1001bfa83a9eSThomas Graf 
1002bfa83a9eSThomas Graf /**
1003bfa83a9eSThomas Graf  * nlmsg_end - Finalize a netlink message
1004bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
1005bfa83a9eSThomas Graf  * @nlh: netlink message header
1006bfa83a9eSThomas Graf  *
1007bfa83a9eSThomas Graf  * Corrects the netlink message header to include the appeneded
1008bfa83a9eSThomas Graf  * attributes. Only necessary if attributes have been added to
1009bfa83a9eSThomas Graf  * the message.
1010bfa83a9eSThomas Graf  */
nlmsg_end(struct sk_buff * skb,struct nlmsghdr * nlh)1011053c095aSJohannes Berg static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
1012bfa83a9eSThomas Graf {
101327a884dcSArnaldo Carvalho de Melo 	nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
1014bfa83a9eSThomas Graf }
1015bfa83a9eSThomas Graf 
1016bfa83a9eSThomas Graf /**
1017fe4944e5SThomas Graf  * nlmsg_get_pos - return current position in netlink message
1018fe4944e5SThomas Graf  * @skb: socket buffer the message is stored in
1019fe4944e5SThomas Graf  *
1020fe4944e5SThomas Graf  * Returns a pointer to the current tail of the message.
1021fe4944e5SThomas Graf  */
nlmsg_get_pos(struct sk_buff * skb)1022fe4944e5SThomas Graf static inline void *nlmsg_get_pos(struct sk_buff *skb)
1023fe4944e5SThomas Graf {
102427a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
1025fe4944e5SThomas Graf }
1026fe4944e5SThomas Graf 
1027fe4944e5SThomas Graf /**
1028fe4944e5SThomas Graf  * nlmsg_trim - Trim message to a mark
1029fe4944e5SThomas Graf  * @skb: socket buffer the message is stored in
1030fe4944e5SThomas Graf  * @mark: mark to trim to
1031fe4944e5SThomas Graf  *
1032bc3ed28cSThomas Graf  * Trims the message to the provided mark.
1033fe4944e5SThomas Graf  */
nlmsg_trim(struct sk_buff * skb,const void * mark)1034bc3ed28cSThomas Graf static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
1035fe4944e5SThomas Graf {
1036149118d8SThomas Graf 	if (mark) {
1037149118d8SThomas Graf 		WARN_ON((unsigned char *) mark < skb->data);
1038fe4944e5SThomas Graf 		skb_trim(skb, (unsigned char *) mark - skb->data);
1039fe4944e5SThomas Graf 	}
1040149118d8SThomas Graf }
1041fe4944e5SThomas Graf 
1042fe4944e5SThomas Graf /**
1043bfa83a9eSThomas Graf  * nlmsg_cancel - Cancel construction of a netlink message
1044bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
1045bfa83a9eSThomas Graf  * @nlh: netlink message header
1046bfa83a9eSThomas Graf  *
1047bfa83a9eSThomas Graf  * Removes the complete netlink message including all
1048bc3ed28cSThomas Graf  * attributes from the socket buffer again.
1049bfa83a9eSThomas Graf  */
nlmsg_cancel(struct sk_buff * skb,struct nlmsghdr * nlh)1050bc3ed28cSThomas Graf static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
1051bfa83a9eSThomas Graf {
1052bc3ed28cSThomas Graf 	nlmsg_trim(skb, nlh);
1053bfa83a9eSThomas Graf }
1054bfa83a9eSThomas Graf 
1055bfa83a9eSThomas Graf /**
1056bfa83a9eSThomas Graf  * nlmsg_free - free a netlink message
1057bfa83a9eSThomas Graf  * @skb: socket buffer of netlink message
1058bfa83a9eSThomas Graf  */
nlmsg_free(struct sk_buff * skb)1059bfa83a9eSThomas Graf static inline void nlmsg_free(struct sk_buff *skb)
1060bfa83a9eSThomas Graf {
1061bfa83a9eSThomas Graf 	kfree_skb(skb);
1062bfa83a9eSThomas Graf }
1063bfa83a9eSThomas Graf 
1064bfa83a9eSThomas Graf /**
1065bfa83a9eSThomas Graf  * nlmsg_multicast - multicast a netlink message
1066bfa83a9eSThomas Graf  * @sk: netlink socket to spread messages to
1067bfa83a9eSThomas Graf  * @skb: netlink message as socket buffer
106815e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
1069bfa83a9eSThomas Graf  * @group: multicast group id
1070d387f6adSThomas Graf  * @flags: allocation flags
1071bfa83a9eSThomas Graf  */
nlmsg_multicast(struct sock * sk,struct sk_buff * skb,u32 portid,unsigned int group,gfp_t flags)1072bfa83a9eSThomas Graf static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
107315e47304SEric W. Biederman 				  u32 portid, unsigned int group, gfp_t flags)
1074bfa83a9eSThomas Graf {
1075bfa83a9eSThomas Graf 	int err;
1076bfa83a9eSThomas Graf 
1077bfa83a9eSThomas Graf 	NETLINK_CB(skb).dst_group = group;
1078bfa83a9eSThomas Graf 
107915e47304SEric W. Biederman 	err = netlink_broadcast(sk, skb, portid, group, flags);
1080bfa83a9eSThomas Graf 	if (err > 0)
1081bfa83a9eSThomas Graf 		err = 0;
1082bfa83a9eSThomas Graf 
1083bfa83a9eSThomas Graf 	return err;
1084bfa83a9eSThomas Graf }
1085bfa83a9eSThomas Graf 
1086bfa83a9eSThomas Graf /**
1087bfa83a9eSThomas Graf  * nlmsg_unicast - unicast a netlink message
1088bfa83a9eSThomas Graf  * @sk: netlink socket to spread message to
1089bfa83a9eSThomas Graf  * @skb: netlink message as socket buffer
109015e47304SEric W. Biederman  * @portid: netlink portid of the destination socket
1091bfa83a9eSThomas Graf  */
nlmsg_unicast(struct sock * sk,struct sk_buff * skb,u32 portid)109215e47304SEric W. Biederman static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
1093bfa83a9eSThomas Graf {
1094bfa83a9eSThomas Graf 	int err;
1095bfa83a9eSThomas Graf 
109615e47304SEric W. Biederman 	err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
1097bfa83a9eSThomas Graf 	if (err > 0)
1098bfa83a9eSThomas Graf 		err = 0;
1099bfa83a9eSThomas Graf 
1100bfa83a9eSThomas Graf 	return err;
1101bfa83a9eSThomas Graf }
1102bfa83a9eSThomas Graf 
1103bfa83a9eSThomas Graf /**
1104bfa83a9eSThomas Graf  * nlmsg_for_each_msg - iterate over a stream of messages
1105bfa83a9eSThomas Graf  * @pos: loop counter, set to current message
1106bfa83a9eSThomas Graf  * @head: head of message stream
1107bfa83a9eSThomas Graf  * @len: length of message stream
1108bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
1109bfa83a9eSThomas Graf  */
1110bfa83a9eSThomas Graf #define nlmsg_for_each_msg(pos, head, len, rem) \
1111bfa83a9eSThomas Graf 	for (pos = head, rem = len; \
1112bfa83a9eSThomas Graf 	     nlmsg_ok(pos, rem); \
1113bfa83a9eSThomas Graf 	     pos = nlmsg_next(pos, &(rem)))
1114bfa83a9eSThomas Graf 
1115670dc283SJohannes Berg /**
1116670dc283SJohannes Berg  * nl_dump_check_consistent - check if sequence is consistent and advertise if not
1117670dc283SJohannes Berg  * @cb: netlink callback structure that stores the sequence number
1118670dc283SJohannes Berg  * @nlh: netlink message header to write the flag to
1119670dc283SJohannes Berg  *
1120670dc283SJohannes Berg  * This function checks if the sequence (generation) number changed during dump
1121670dc283SJohannes Berg  * and if it did, advertises it in the netlink message header.
1122670dc283SJohannes Berg  *
1123670dc283SJohannes Berg  * The correct way to use it is to set cb->seq to the generation counter when
1124670dc283SJohannes Berg  * all locks for dumping have been acquired, and then call this function for
1125670dc283SJohannes Berg  * each message that is generated.
1126670dc283SJohannes Berg  *
1127670dc283SJohannes Berg  * Note that due to initialisation concerns, 0 is an invalid sequence number
1128670dc283SJohannes Berg  * and must not be used by code that uses this functionality.
1129670dc283SJohannes Berg  */
1130670dc283SJohannes Berg static inline void
nl_dump_check_consistent(struct netlink_callback * cb,struct nlmsghdr * nlh)1131670dc283SJohannes Berg nl_dump_check_consistent(struct netlink_callback *cb,
1132670dc283SJohannes Berg 			 struct nlmsghdr *nlh)
1133670dc283SJohannes Berg {
1134670dc283SJohannes Berg 	if (cb->prev_seq && cb->seq != cb->prev_seq)
1135670dc283SJohannes Berg 		nlh->nlmsg_flags |= NLM_F_DUMP_INTR;
1136670dc283SJohannes Berg 	cb->prev_seq = cb->seq;
1137670dc283SJohannes Berg }
1138670dc283SJohannes Berg 
1139bfa83a9eSThomas Graf /**************************************************************************
1140bfa83a9eSThomas Graf  * Netlink Attributes
1141bfa83a9eSThomas Graf  **************************************************************************/
1142bfa83a9eSThomas Graf 
1143bfa83a9eSThomas Graf /**
1144bfa83a9eSThomas Graf  * nla_attr_size - length of attribute not including padding
1145bfa83a9eSThomas Graf  * @payload: length of payload
1146bfa83a9eSThomas Graf  */
nla_attr_size(int payload)1147bfa83a9eSThomas Graf static inline int nla_attr_size(int payload)
1148bfa83a9eSThomas Graf {
1149bfa83a9eSThomas Graf 	return NLA_HDRLEN + payload;
1150bfa83a9eSThomas Graf }
1151bfa83a9eSThomas Graf 
1152bfa83a9eSThomas Graf /**
1153bfa83a9eSThomas Graf  * nla_total_size - total length of attribute including padding
1154bfa83a9eSThomas Graf  * @payload: length of payload
1155bfa83a9eSThomas Graf  */
nla_total_size(int payload)1156bfa83a9eSThomas Graf static inline int nla_total_size(int payload)
1157bfa83a9eSThomas Graf {
1158bfa83a9eSThomas Graf 	return NLA_ALIGN(nla_attr_size(payload));
1159bfa83a9eSThomas Graf }
1160bfa83a9eSThomas Graf 
1161bfa83a9eSThomas Graf /**
1162bfa83a9eSThomas Graf  * nla_padlen - length of padding at the tail of attribute
1163bfa83a9eSThomas Graf  * @payload: length of payload
1164bfa83a9eSThomas Graf  */
nla_padlen(int payload)1165bfa83a9eSThomas Graf static inline int nla_padlen(int payload)
1166bfa83a9eSThomas Graf {
1167bfa83a9eSThomas Graf 	return nla_total_size(payload) - nla_attr_size(payload);
1168bfa83a9eSThomas Graf }
1169bfa83a9eSThomas Graf 
1170bfa83a9eSThomas Graf /**
11718f4c1f9bSThomas Graf  * nla_type - attribute type
11728f4c1f9bSThomas Graf  * @nla: netlink attribute
11738f4c1f9bSThomas Graf  */
nla_type(const struct nlattr * nla)11748f4c1f9bSThomas Graf static inline int nla_type(const struct nlattr *nla)
11758f4c1f9bSThomas Graf {
11768f4c1f9bSThomas Graf 	return nla->nla_type & NLA_TYPE_MASK;
11778f4c1f9bSThomas Graf }
11788f4c1f9bSThomas Graf 
11798f4c1f9bSThomas Graf /**
1180bfa83a9eSThomas Graf  * nla_data - head of payload
1181bfa83a9eSThomas Graf  * @nla: netlink attribute
1182bfa83a9eSThomas Graf  */
nla_data(const struct nlattr * nla)1183bfa83a9eSThomas Graf static inline void *nla_data(const struct nlattr *nla)
1184bfa83a9eSThomas Graf {
1185bfa83a9eSThomas Graf 	return (char *) nla + NLA_HDRLEN;
1186bfa83a9eSThomas Graf }
1187bfa83a9eSThomas Graf 
1188bfa83a9eSThomas Graf /**
1189bfa83a9eSThomas Graf  * nla_len - length of payload
1190bfa83a9eSThomas Graf  * @nla: netlink attribute
1191bfa83a9eSThomas Graf  */
nla_len(const struct nlattr * nla)1192bfa83a9eSThomas Graf static inline int nla_len(const struct nlattr *nla)
1193bfa83a9eSThomas Graf {
1194bfa83a9eSThomas Graf 	return nla->nla_len - NLA_HDRLEN;
1195bfa83a9eSThomas Graf }
1196bfa83a9eSThomas Graf 
1197bfa83a9eSThomas Graf /**
1198bfa83a9eSThomas Graf  * nla_ok - check if the netlink attribute fits into the remaining bytes
1199bfa83a9eSThomas Graf  * @nla: netlink attribute
1200bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in attribute stream
1201bfa83a9eSThomas Graf  */
nla_ok(const struct nlattr * nla,int remaining)1202bfa83a9eSThomas Graf static inline int nla_ok(const struct nlattr *nla, int remaining)
1203bfa83a9eSThomas Graf {
12043e1ed981SAlexey Dobriyan 	return remaining >= (int) sizeof(*nla) &&
12053e1ed981SAlexey Dobriyan 	       nla->nla_len >= sizeof(*nla) &&
1206bfa83a9eSThomas Graf 	       nla->nla_len <= remaining;
1207bfa83a9eSThomas Graf }
1208bfa83a9eSThomas Graf 
1209bfa83a9eSThomas Graf /**
1210d1ec3b77SPierre Ynard  * nla_next - next netlink attribute in attribute stream
1211bfa83a9eSThomas Graf  * @nla: netlink attribute
1212bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in attribute stream
1213bfa83a9eSThomas Graf  *
1214bfa83a9eSThomas Graf  * Returns the next netlink attribute in the attribute stream and
1215bfa83a9eSThomas Graf  * decrements remaining by the size of the current attribute.
1216bfa83a9eSThomas Graf  */
nla_next(const struct nlattr * nla,int * remaining)1217bfa83a9eSThomas Graf static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
1218bfa83a9eSThomas Graf {
12193b2c75d3SAlexey Dobriyan 	unsigned int totlen = NLA_ALIGN(nla->nla_len);
1220bfa83a9eSThomas Graf 
1221bfa83a9eSThomas Graf 	*remaining -= totlen;
1222bfa83a9eSThomas Graf 	return (struct nlattr *) ((char *) nla + totlen);
1223bfa83a9eSThomas Graf }
1224bfa83a9eSThomas Graf 
1225bfa83a9eSThomas Graf /**
1226fe4944e5SThomas Graf  * nla_find_nested - find attribute in a set of nested attributes
1227fe4944e5SThomas Graf  * @nla: attribute containing the nested attributes
1228fe4944e5SThomas Graf  * @attrtype: type of attribute to look for
1229fe4944e5SThomas Graf  *
1230fe4944e5SThomas Graf  * Returns the first attribute which matches the specified type.
1231fe4944e5SThomas Graf  */
12323654654fSJan Engelhardt static inline struct nlattr *
nla_find_nested(const struct nlattr * nla,int attrtype)12333654654fSJan Engelhardt nla_find_nested(const struct nlattr *nla, int attrtype)
1234fe4944e5SThomas Graf {
1235fe4944e5SThomas Graf 	return nla_find(nla_data(nla), nla_len(nla), attrtype);
1236fe4944e5SThomas Graf }
1237fe4944e5SThomas Graf 
1238fe4944e5SThomas Graf /**
12393de64403SJohannes Berg  * nla_parse_nested - parse nested attributes
12403de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
12413de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
12423de64403SJohannes Berg  * @nla: attribute containing the nested attributes
12433de64403SJohannes Berg  * @policy: validation policy
12443de64403SJohannes Berg  * @extack: extended ACK report struct
12453de64403SJohannes Berg  *
12463de64403SJohannes Berg  * See nla_parse()
12473de64403SJohannes Berg  */
nla_parse_nested(struct nlattr * tb[],int maxtype,const struct nlattr * nla,const struct nla_policy * policy,struct netlink_ext_ack * extack)12483de64403SJohannes Berg static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
12493de64403SJohannes Berg 				   const struct nlattr *nla,
12503de64403SJohannes Berg 				   const struct nla_policy *policy,
12513de64403SJohannes Berg 				   struct netlink_ext_ack *extack)
12523de64403SJohannes Berg {
1253b424e432SMichal Kubecek 	if (!(nla->nla_type & NLA_F_NESTED)) {
1254b424e432SMichal Kubecek 		NL_SET_ERR_MSG_ATTR(extack, nla, "NLA_F_NESTED is missing");
1255b424e432SMichal Kubecek 		return -EINVAL;
1256b424e432SMichal Kubecek 	}
1257b424e432SMichal Kubecek 
12583de64403SJohannes Berg 	return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
12593de64403SJohannes Berg 			   NL_VALIDATE_STRICT, extack);
12603de64403SJohannes Berg }
12613de64403SJohannes Berg 
12623de64403SJohannes Berg /**
12638cb08174SJohannes Berg  * nla_parse_nested_deprecated - parse nested attributes
1264bfa83a9eSThomas Graf  * @tb: destination array with maxtype+1 elements
1265bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
1266bfa83a9eSThomas Graf  * @nla: attribute containing the nested attributes
1267bfa83a9eSThomas Graf  * @policy: validation policy
1268fceb6435SJohannes Berg  * @extack: extended ACK report struct
1269bfa83a9eSThomas Graf  *
12708cb08174SJohannes Berg  * See nla_parse_deprecated()
1271bfa83a9eSThomas Graf  */
nla_parse_nested_deprecated(struct nlattr * tb[],int maxtype,const struct nlattr * nla,const struct nla_policy * policy,struct netlink_ext_ack * extack)12728cb08174SJohannes Berg static inline int nla_parse_nested_deprecated(struct nlattr *tb[], int maxtype,
1273b057efd4SPatrick McHardy 					      const struct nlattr *nla,
1274fceb6435SJohannes Berg 					      const struct nla_policy *policy,
1275fceb6435SJohannes Berg 					      struct netlink_ext_ack *extack)
1276bfa83a9eSThomas Graf {
12778cb08174SJohannes Berg 	return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
12788cb08174SJohannes Berg 			   NL_VALIDATE_LIBERAL, extack);
1279bfa83a9eSThomas Graf }
12801092cb21SPatrick McHardy 
12811092cb21SPatrick McHardy /**
1282d1ec3b77SPierre Ynard  * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
1283bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1284bfa83a9eSThomas Graf  * @attrtype: attribute type
1285bfa83a9eSThomas Graf  * @value: numeric value
1286bfa83a9eSThomas Graf  */
nla_put_u8(struct sk_buff * skb,int attrtype,u8 value)1287bfa83a9eSThomas Graf static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
1288bfa83a9eSThomas Graf {
1289b4391db4SArnd Bergmann 	/* temporary variables to work around GCC PR81715 with asan-stack=1 */
1290b4391db4SArnd Bergmann 	u8 tmp = value;
1291b4391db4SArnd Bergmann 
1292b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(u8), &tmp);
1293bfa83a9eSThomas Graf }
1294bfa83a9eSThomas Graf 
1295bfa83a9eSThomas Graf /**
1296bfa83a9eSThomas Graf  * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
1297bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1298bfa83a9eSThomas Graf  * @attrtype: attribute type
1299bfa83a9eSThomas Graf  * @value: numeric value
1300bfa83a9eSThomas Graf  */
nla_put_u16(struct sk_buff * skb,int attrtype,u16 value)1301bfa83a9eSThomas Graf static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
1302bfa83a9eSThomas Graf {
1303b4391db4SArnd Bergmann 	u16 tmp = value;
1304b4391db4SArnd Bergmann 
1305b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(u16), &tmp);
1306bfa83a9eSThomas Graf }
1307bfa83a9eSThomas Graf 
1308bfa83a9eSThomas Graf /**
1309569a8fc3SDavid S. Miller  * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer
1310569a8fc3SDavid S. Miller  * @skb: socket buffer to add attribute to
1311569a8fc3SDavid S. Miller  * @attrtype: attribute type
1312569a8fc3SDavid S. Miller  * @value: numeric value
1313569a8fc3SDavid S. Miller  */
nla_put_be16(struct sk_buff * skb,int attrtype,__be16 value)1314569a8fc3SDavid S. Miller static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
1315569a8fc3SDavid S. Miller {
1316b4391db4SArnd Bergmann 	__be16 tmp = value;
1317b4391db4SArnd Bergmann 
1318b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__be16), &tmp);
1319569a8fc3SDavid S. Miller }
1320569a8fc3SDavid S. Miller 
1321569a8fc3SDavid S. Miller /**
13226c1dd3b6SDavid S. Miller  * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer
13236c1dd3b6SDavid S. Miller  * @skb: socket buffer to add attribute to
13246c1dd3b6SDavid S. Miller  * @attrtype: attribute type
13256c1dd3b6SDavid S. Miller  * @value: numeric value
13266c1dd3b6SDavid S. Miller  */
nla_put_net16(struct sk_buff * skb,int attrtype,__be16 value)13276c1dd3b6SDavid S. Miller static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
13286c1dd3b6SDavid S. Miller {
1329b4391db4SArnd Bergmann 	__be16 tmp = value;
1330b4391db4SArnd Bergmann 
1331b4391db4SArnd Bergmann 	return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
13326c1dd3b6SDavid S. Miller }
13336c1dd3b6SDavid S. Miller 
13346c1dd3b6SDavid S. Miller /**
133524c410dcSDavid S. Miller  * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer
133624c410dcSDavid S. Miller  * @skb: socket buffer to add attribute to
133724c410dcSDavid S. Miller  * @attrtype: attribute type
133824c410dcSDavid S. Miller  * @value: numeric value
133924c410dcSDavid S. Miller  */
nla_put_le16(struct sk_buff * skb,int attrtype,__le16 value)134024c410dcSDavid S. Miller static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
134124c410dcSDavid S. Miller {
1342b4391db4SArnd Bergmann 	__le16 tmp = value;
1343b4391db4SArnd Bergmann 
1344b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__le16), &tmp);
134524c410dcSDavid S. Miller }
134624c410dcSDavid S. Miller 
134724c410dcSDavid S. Miller /**
1348bfa83a9eSThomas Graf  * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
1349bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1350bfa83a9eSThomas Graf  * @attrtype: attribute type
1351bfa83a9eSThomas Graf  * @value: numeric value
1352bfa83a9eSThomas Graf  */
nla_put_u32(struct sk_buff * skb,int attrtype,u32 value)1353bfa83a9eSThomas Graf static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
1354bfa83a9eSThomas Graf {
1355b4391db4SArnd Bergmann 	u32 tmp = value;
1356b4391db4SArnd Bergmann 
1357b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(u32), &tmp);
1358bfa83a9eSThomas Graf }
1359bfa83a9eSThomas Graf 
1360bfa83a9eSThomas Graf /**
1361569a8fc3SDavid S. Miller  * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer
1362569a8fc3SDavid S. Miller  * @skb: socket buffer to add attribute to
1363569a8fc3SDavid S. Miller  * @attrtype: attribute type
1364569a8fc3SDavid S. Miller  * @value: numeric value
1365569a8fc3SDavid S. Miller  */
nla_put_be32(struct sk_buff * skb,int attrtype,__be32 value)1366569a8fc3SDavid S. Miller static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
1367569a8fc3SDavid S. Miller {
1368b4391db4SArnd Bergmann 	__be32 tmp = value;
1369b4391db4SArnd Bergmann 
1370b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__be32), &tmp);
1371569a8fc3SDavid S. Miller }
1372569a8fc3SDavid S. Miller 
1373569a8fc3SDavid S. Miller /**
13746c1dd3b6SDavid S. Miller  * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer
13756c1dd3b6SDavid S. Miller  * @skb: socket buffer to add attribute to
13766c1dd3b6SDavid S. Miller  * @attrtype: attribute type
13776c1dd3b6SDavid S. Miller  * @value: numeric value
13786c1dd3b6SDavid S. Miller  */
nla_put_net32(struct sk_buff * skb,int attrtype,__be32 value)13796c1dd3b6SDavid S. Miller static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
13806c1dd3b6SDavid S. Miller {
1381b4391db4SArnd Bergmann 	__be32 tmp = value;
1382b4391db4SArnd Bergmann 
1383b4391db4SArnd Bergmann 	return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
13846c1dd3b6SDavid S. Miller }
13856c1dd3b6SDavid S. Miller 
13866c1dd3b6SDavid S. Miller /**
138724c410dcSDavid S. Miller  * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer
138824c410dcSDavid S. Miller  * @skb: socket buffer to add attribute to
138924c410dcSDavid S. Miller  * @attrtype: attribute type
139024c410dcSDavid S. Miller  * @value: numeric value
139124c410dcSDavid S. Miller  */
nla_put_le32(struct sk_buff * skb,int attrtype,__le32 value)139224c410dcSDavid S. Miller static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value)
139324c410dcSDavid S. Miller {
1394b4391db4SArnd Bergmann 	__le32 tmp = value;
1395b4391db4SArnd Bergmann 
1396b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__le32), &tmp);
139724c410dcSDavid S. Miller }
139824c410dcSDavid S. Miller 
139924c410dcSDavid S. Miller /**
140073520786SNicolas Dichtel  * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it
140173520786SNicolas Dichtel  * @skb: socket buffer to add attribute to
140273520786SNicolas Dichtel  * @attrtype: attribute type
140373520786SNicolas Dichtel  * @value: numeric value
140473520786SNicolas Dichtel  * @padattr: attribute type for the padding
140573520786SNicolas Dichtel  */
nla_put_u64_64bit(struct sk_buff * skb,int attrtype,u64 value,int padattr)140673520786SNicolas Dichtel static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype,
140773520786SNicolas Dichtel 				    u64 value, int padattr)
140873520786SNicolas Dichtel {
1409b4391db4SArnd Bergmann 	u64 tmp = value;
1410b4391db4SArnd Bergmann 
1411b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
141273520786SNicolas Dichtel }
141373520786SNicolas Dichtel 
141473520786SNicolas Dichtel /**
1415b46f6dedSNicolas Dichtel  * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it
1416569a8fc3SDavid S. Miller  * @skb: socket buffer to add attribute to
1417569a8fc3SDavid S. Miller  * @attrtype: attribute type
1418569a8fc3SDavid S. Miller  * @value: numeric value
1419b46f6dedSNicolas Dichtel  * @padattr: attribute type for the padding
1420569a8fc3SDavid S. Miller  */
nla_put_be64(struct sk_buff * skb,int attrtype,__be64 value,int padattr)1421b46f6dedSNicolas Dichtel static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value,
1422b46f6dedSNicolas Dichtel 			       int padattr)
1423b46f6dedSNicolas Dichtel {
1424b4391db4SArnd Bergmann 	__be64 tmp = value;
1425b4391db4SArnd Bergmann 
1426b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr);
1427b46f6dedSNicolas Dichtel }
1428b46f6dedSNicolas Dichtel 
1429569a8fc3SDavid S. Miller /**
1430e9bbe898SNicolas Dichtel  * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it
14316c1dd3b6SDavid S. Miller  * @skb: socket buffer to add attribute to
14326c1dd3b6SDavid S. Miller  * @attrtype: attribute type
14336c1dd3b6SDavid S. Miller  * @value: numeric value
1434e9bbe898SNicolas Dichtel  * @padattr: attribute type for the padding
14356c1dd3b6SDavid S. Miller  */
nla_put_net64(struct sk_buff * skb,int attrtype,__be64 value,int padattr)1436e9bbe898SNicolas Dichtel static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value,
1437e9bbe898SNicolas Dichtel 				int padattr)
14386c1dd3b6SDavid S. Miller {
1439b4391db4SArnd Bergmann 	__be64 tmp = value;
1440b4391db4SArnd Bergmann 
1441b4391db4SArnd Bergmann 	return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp,
1442e9bbe898SNicolas Dichtel 			    padattr);
14436c1dd3b6SDavid S. Miller }
14446c1dd3b6SDavid S. Miller 
14456c1dd3b6SDavid S. Miller /**
1446e7479122SNicolas Dichtel  * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it
144724c410dcSDavid S. Miller  * @skb: socket buffer to add attribute to
144824c410dcSDavid S. Miller  * @attrtype: attribute type
144924c410dcSDavid S. Miller  * @value: numeric value
1450e7479122SNicolas Dichtel  * @padattr: attribute type for the padding
145124c410dcSDavid S. Miller  */
nla_put_le64(struct sk_buff * skb,int attrtype,__le64 value,int padattr)1452e7479122SNicolas Dichtel static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value,
1453e7479122SNicolas Dichtel 			       int padattr)
145424c410dcSDavid S. Miller {
1455b4391db4SArnd Bergmann 	__le64 tmp = value;
1456b4391db4SArnd Bergmann 
1457b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr);
145824c410dcSDavid S. Miller }
145924c410dcSDavid S. Miller 
146024c410dcSDavid S. Miller /**
14614778e0beSJiri Pirko  * nla_put_s8 - Add a s8 netlink attribute to a socket buffer
14624778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
14634778e0beSJiri Pirko  * @attrtype: attribute type
14644778e0beSJiri Pirko  * @value: numeric value
14654778e0beSJiri Pirko  */
nla_put_s8(struct sk_buff * skb,int attrtype,s8 value)14664778e0beSJiri Pirko static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
14674778e0beSJiri Pirko {
1468b4391db4SArnd Bergmann 	s8 tmp = value;
1469b4391db4SArnd Bergmann 
1470b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(s8), &tmp);
14714778e0beSJiri Pirko }
14724778e0beSJiri Pirko 
14734778e0beSJiri Pirko /**
14744778e0beSJiri Pirko  * nla_put_s16 - Add a s16 netlink attribute to a socket buffer
14754778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
14764778e0beSJiri Pirko  * @attrtype: attribute type
14774778e0beSJiri Pirko  * @value: numeric value
14784778e0beSJiri Pirko  */
nla_put_s16(struct sk_buff * skb,int attrtype,s16 value)14794778e0beSJiri Pirko static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
14804778e0beSJiri Pirko {
1481b4391db4SArnd Bergmann 	s16 tmp = value;
1482b4391db4SArnd Bergmann 
1483b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(s16), &tmp);
14844778e0beSJiri Pirko }
14854778e0beSJiri Pirko 
14864778e0beSJiri Pirko /**
14874778e0beSJiri Pirko  * nla_put_s32 - Add a s32 netlink attribute to a socket buffer
14884778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
14894778e0beSJiri Pirko  * @attrtype: attribute type
14904778e0beSJiri Pirko  * @value: numeric value
14914778e0beSJiri Pirko  */
nla_put_s32(struct sk_buff * skb,int attrtype,s32 value)14924778e0beSJiri Pirko static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
14934778e0beSJiri Pirko {
1494b4391db4SArnd Bergmann 	s32 tmp = value;
1495b4391db4SArnd Bergmann 
1496b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(s32), &tmp);
14974778e0beSJiri Pirko }
14984778e0beSJiri Pirko 
14994778e0beSJiri Pirko /**
1500756a2f59SNicolas Dichtel  * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it
15014778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
15024778e0beSJiri Pirko  * @attrtype: attribute type
15034778e0beSJiri Pirko  * @value: numeric value
1504756a2f59SNicolas Dichtel  * @padattr: attribute type for the padding
15054778e0beSJiri Pirko  */
nla_put_s64(struct sk_buff * skb,int attrtype,s64 value,int padattr)1506756a2f59SNicolas Dichtel static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value,
1507756a2f59SNicolas Dichtel 			      int padattr)
15084778e0beSJiri Pirko {
1509b4391db4SArnd Bergmann 	s64 tmp = value;
1510b4391db4SArnd Bergmann 
1511b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr);
15124778e0beSJiri Pirko }
15134778e0beSJiri Pirko 
15144778e0beSJiri Pirko /**
1515bfa83a9eSThomas Graf  * nla_put_string - Add a string netlink attribute to a socket buffer
1516bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1517bfa83a9eSThomas Graf  * @attrtype: attribute type
1518bfa83a9eSThomas Graf  * @str: NUL terminated string
1519bfa83a9eSThomas Graf  */
nla_put_string(struct sk_buff * skb,int attrtype,const char * str)1520bfa83a9eSThomas Graf static inline int nla_put_string(struct sk_buff *skb, int attrtype,
1521bfa83a9eSThomas Graf 				 const char *str)
1522bfa83a9eSThomas Graf {
1523bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, strlen(str) + 1, str);
1524bfa83a9eSThomas Graf }
1525bfa83a9eSThomas Graf 
1526bfa83a9eSThomas Graf /**
1527bfa83a9eSThomas Graf  * nla_put_flag - Add a flag netlink attribute to a socket buffer
1528bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1529bfa83a9eSThomas Graf  * @attrtype: attribute type
1530bfa83a9eSThomas Graf  */
nla_put_flag(struct sk_buff * skb,int attrtype)1531bfa83a9eSThomas Graf static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
1532bfa83a9eSThomas Graf {
1533bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, 0, NULL);
1534bfa83a9eSThomas Graf }
1535bfa83a9eSThomas Graf 
1536bfa83a9eSThomas Graf /**
15372175d87cSNicolas Dichtel  * nla_put_msecs - Add a msecs netlink attribute to a skb and align it
1538bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1539bfa83a9eSThomas Graf  * @attrtype: attribute type
1540d87de1f3SMark Rustad  * @njiffies: number of jiffies to convert to msecs
15412175d87cSNicolas Dichtel  * @padattr: attribute type for the padding
1542bfa83a9eSThomas Graf  */
nla_put_msecs(struct sk_buff * skb,int attrtype,unsigned long njiffies,int padattr)1543bfa83a9eSThomas Graf static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
15442175d87cSNicolas Dichtel 				unsigned long njiffies, int padattr)
1545bfa83a9eSThomas Graf {
1546d87de1f3SMark Rustad 	u64 tmp = jiffies_to_msecs(njiffies);
15472175d87cSNicolas Dichtel 
15482175d87cSNicolas Dichtel 	return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
1549bfa83a9eSThomas Graf }
1550bfa83a9eSThomas Graf 
1551bfa83a9eSThomas Graf /**
1552930345eaSJiri Benc  * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket
1553930345eaSJiri Benc  * buffer
1554930345eaSJiri Benc  * @skb: socket buffer to add attribute to
1555930345eaSJiri Benc  * @attrtype: attribute type
1556930345eaSJiri Benc  * @addr: IPv4 address
1557930345eaSJiri Benc  */
nla_put_in_addr(struct sk_buff * skb,int attrtype,__be32 addr)1558930345eaSJiri Benc static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
1559930345eaSJiri Benc 				  __be32 addr)
1560930345eaSJiri Benc {
1561b4391db4SArnd Bergmann 	__be32 tmp = addr;
1562b4391db4SArnd Bergmann 
1563b4391db4SArnd Bergmann 	return nla_put_be32(skb, attrtype, tmp);
1564930345eaSJiri Benc }
1565930345eaSJiri Benc 
1566930345eaSJiri Benc /**
1567930345eaSJiri Benc  * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket
1568930345eaSJiri Benc  * buffer
1569930345eaSJiri Benc  * @skb: socket buffer to add attribute to
1570930345eaSJiri Benc  * @attrtype: attribute type
1571930345eaSJiri Benc  * @addr: IPv6 address
1572930345eaSJiri Benc  */
nla_put_in6_addr(struct sk_buff * skb,int attrtype,const struct in6_addr * addr)1573930345eaSJiri Benc static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype,
1574930345eaSJiri Benc 				   const struct in6_addr *addr)
1575930345eaSJiri Benc {
1576930345eaSJiri Benc 	return nla_put(skb, attrtype, sizeof(*addr), addr);
1577930345eaSJiri Benc }
1578930345eaSJiri Benc 
1579930345eaSJiri Benc /**
15808953b077SJiri Pirko  * nla_put_bitfield32 - Add a bitfield32 netlink attribute to a socket buffer
15818953b077SJiri Pirko  * @skb: socket buffer to add attribute to
15828953b077SJiri Pirko  * @attrtype: attribute type
15838953b077SJiri Pirko  * @value: value carrying bits
15848953b077SJiri Pirko  * @selector: selector of valid bits
15858953b077SJiri Pirko  */
nla_put_bitfield32(struct sk_buff * skb,int attrtype,__u32 value,__u32 selector)15868953b077SJiri Pirko static inline int nla_put_bitfield32(struct sk_buff *skb, int attrtype,
15878953b077SJiri Pirko 				     __u32 value, __u32 selector)
15888953b077SJiri Pirko {
15898953b077SJiri Pirko 	struct nla_bitfield32 tmp = { value, selector, };
15908953b077SJiri Pirko 
15918953b077SJiri Pirko 	return nla_put(skb, attrtype, sizeof(tmp), &tmp);
15928953b077SJiri Pirko }
15938953b077SJiri Pirko 
15948953b077SJiri Pirko /**
1595bfa83a9eSThomas Graf  * nla_get_u32 - return payload of u32 attribute
1596bfa83a9eSThomas Graf  * @nla: u32 netlink attribute
1597bfa83a9eSThomas Graf  */
nla_get_u32(const struct nlattr * nla)1598b057efd4SPatrick McHardy static inline u32 nla_get_u32(const struct nlattr *nla)
1599bfa83a9eSThomas Graf {
1600bfa83a9eSThomas Graf 	return *(u32 *) nla_data(nla);
1601bfa83a9eSThomas Graf }
1602bfa83a9eSThomas Graf 
1603bfa83a9eSThomas Graf /**
160400012e5bSAl Viro  * nla_get_be32 - return payload of __be32 attribute
160500012e5bSAl Viro  * @nla: __be32 netlink attribute
160600012e5bSAl Viro  */
nla_get_be32(const struct nlattr * nla)1607b057efd4SPatrick McHardy static inline __be32 nla_get_be32(const struct nlattr *nla)
160800012e5bSAl Viro {
160900012e5bSAl Viro 	return *(__be32 *) nla_data(nla);
161000012e5bSAl Viro }
161100012e5bSAl Viro 
161200012e5bSAl Viro /**
1613c648a013SAlexander Aring  * nla_get_le32 - return payload of __le32 attribute
1614c648a013SAlexander Aring  * @nla: __le32 netlink attribute
1615c648a013SAlexander Aring  */
nla_get_le32(const struct nlattr * nla)1616c648a013SAlexander Aring static inline __le32 nla_get_le32(const struct nlattr *nla)
1617c648a013SAlexander Aring {
1618c648a013SAlexander Aring 	return *(__le32 *) nla_data(nla);
1619c648a013SAlexander Aring }
1620c648a013SAlexander Aring 
1621c648a013SAlexander Aring /**
1622bfa83a9eSThomas Graf  * nla_get_u16 - return payload of u16 attribute
1623bfa83a9eSThomas Graf  * @nla: u16 netlink attribute
1624bfa83a9eSThomas Graf  */
nla_get_u16(const struct nlattr * nla)1625b057efd4SPatrick McHardy static inline u16 nla_get_u16(const struct nlattr *nla)
1626bfa83a9eSThomas Graf {
1627bfa83a9eSThomas Graf 	return *(u16 *) nla_data(nla);
1628bfa83a9eSThomas Graf }
1629bfa83a9eSThomas Graf 
1630bfa83a9eSThomas Graf /**
1631838965baSPatrick McHardy  * nla_get_be16 - return payload of __be16 attribute
1632838965baSPatrick McHardy  * @nla: __be16 netlink attribute
1633838965baSPatrick McHardy  */
nla_get_be16(const struct nlattr * nla)1634b057efd4SPatrick McHardy static inline __be16 nla_get_be16(const struct nlattr *nla)
1635838965baSPatrick McHardy {
1636838965baSPatrick McHardy 	return *(__be16 *) nla_data(nla);
1637838965baSPatrick McHardy }
1638838965baSPatrick McHardy 
1639838965baSPatrick McHardy /**
16404a89c256SThomas Graf  * nla_get_le16 - return payload of __le16 attribute
16414a89c256SThomas Graf  * @nla: __le16 netlink attribute
16424a89c256SThomas Graf  */
nla_get_le16(const struct nlattr * nla)1643b057efd4SPatrick McHardy static inline __le16 nla_get_le16(const struct nlattr *nla)
16444a89c256SThomas Graf {
16454a89c256SThomas Graf 	return *(__le16 *) nla_data(nla);
16464a89c256SThomas Graf }
16474a89c256SThomas Graf 
16484a89c256SThomas Graf /**
1649bfa83a9eSThomas Graf  * nla_get_u8 - return payload of u8 attribute
1650bfa83a9eSThomas Graf  * @nla: u8 netlink attribute
1651bfa83a9eSThomas Graf  */
nla_get_u8(const struct nlattr * nla)1652b057efd4SPatrick McHardy static inline u8 nla_get_u8(const struct nlattr *nla)
1653bfa83a9eSThomas Graf {
1654bfa83a9eSThomas Graf 	return *(u8 *) nla_data(nla);
1655bfa83a9eSThomas Graf }
1656bfa83a9eSThomas Graf 
1657bfa83a9eSThomas Graf /**
1658bfa83a9eSThomas Graf  * nla_get_u64 - return payload of u64 attribute
1659bfa83a9eSThomas Graf  * @nla: u64 netlink attribute
1660bfa83a9eSThomas Graf  */
nla_get_u64(const struct nlattr * nla)1661b057efd4SPatrick McHardy static inline u64 nla_get_u64(const struct nlattr *nla)
1662bfa83a9eSThomas Graf {
1663bfa83a9eSThomas Graf 	u64 tmp;
1664bfa83a9eSThomas Graf 
1665bfa83a9eSThomas Graf 	nla_memcpy(&tmp, nla, sizeof(tmp));
1666bfa83a9eSThomas Graf 
1667bfa83a9eSThomas Graf 	return tmp;
1668bfa83a9eSThomas Graf }
1669bfa83a9eSThomas Graf 
1670bfa83a9eSThomas Graf /**
1671a17c8598SPablo Neira Ayuso  * nla_get_be64 - return payload of __be64 attribute
1672a17c8598SPablo Neira Ayuso  * @nla: __be64 netlink attribute
1673a17c8598SPablo Neira Ayuso  */
nla_get_be64(const struct nlattr * nla)1674a17c8598SPablo Neira Ayuso static inline __be64 nla_get_be64(const struct nlattr *nla)
1675a17c8598SPablo Neira Ayuso {
1676f5d410f2SPablo Neira Ayuso 	__be64 tmp;
1677f5d410f2SPablo Neira Ayuso 
1678f5d410f2SPablo Neira Ayuso 	nla_memcpy(&tmp, nla, sizeof(tmp));
1679f5d410f2SPablo Neira Ayuso 
1680f5d410f2SPablo Neira Ayuso 	return tmp;
1681a17c8598SPablo Neira Ayuso }
1682a17c8598SPablo Neira Ayuso 
1683a17c8598SPablo Neira Ayuso /**
1684c648a013SAlexander Aring  * nla_get_le64 - return payload of __le64 attribute
1685c648a013SAlexander Aring  * @nla: __le64 netlink attribute
1686c648a013SAlexander Aring  */
nla_get_le64(const struct nlattr * nla)1687c648a013SAlexander Aring static inline __le64 nla_get_le64(const struct nlattr *nla)
1688c648a013SAlexander Aring {
1689c648a013SAlexander Aring 	return *(__le64 *) nla_data(nla);
1690c648a013SAlexander Aring }
1691c648a013SAlexander Aring 
1692c648a013SAlexander Aring /**
16934778e0beSJiri Pirko  * nla_get_s32 - return payload of s32 attribute
16944778e0beSJiri Pirko  * @nla: s32 netlink attribute
16954778e0beSJiri Pirko  */
nla_get_s32(const struct nlattr * nla)16964778e0beSJiri Pirko static inline s32 nla_get_s32(const struct nlattr *nla)
16974778e0beSJiri Pirko {
16984778e0beSJiri Pirko 	return *(s32 *) nla_data(nla);
16994778e0beSJiri Pirko }
17004778e0beSJiri Pirko 
17014778e0beSJiri Pirko /**
17024778e0beSJiri Pirko  * nla_get_s16 - return payload of s16 attribute
17034778e0beSJiri Pirko  * @nla: s16 netlink attribute
17044778e0beSJiri Pirko  */
nla_get_s16(const struct nlattr * nla)17054778e0beSJiri Pirko static inline s16 nla_get_s16(const struct nlattr *nla)
17064778e0beSJiri Pirko {
17074778e0beSJiri Pirko 	return *(s16 *) nla_data(nla);
17084778e0beSJiri Pirko }
17094778e0beSJiri Pirko 
17104778e0beSJiri Pirko /**
17114778e0beSJiri Pirko  * nla_get_s8 - return payload of s8 attribute
17124778e0beSJiri Pirko  * @nla: s8 netlink attribute
17134778e0beSJiri Pirko  */
nla_get_s8(const struct nlattr * nla)17144778e0beSJiri Pirko static inline s8 nla_get_s8(const struct nlattr *nla)
17154778e0beSJiri Pirko {
17164778e0beSJiri Pirko 	return *(s8 *) nla_data(nla);
17174778e0beSJiri Pirko }
17184778e0beSJiri Pirko 
17194778e0beSJiri Pirko /**
17204778e0beSJiri Pirko  * nla_get_s64 - return payload of s64 attribute
17214778e0beSJiri Pirko  * @nla: s64 netlink attribute
17224778e0beSJiri Pirko  */
nla_get_s64(const struct nlattr * nla)17234778e0beSJiri Pirko static inline s64 nla_get_s64(const struct nlattr *nla)
17244778e0beSJiri Pirko {
17254778e0beSJiri Pirko 	s64 tmp;
17264778e0beSJiri Pirko 
17274778e0beSJiri Pirko 	nla_memcpy(&tmp, nla, sizeof(tmp));
17284778e0beSJiri Pirko 
17294778e0beSJiri Pirko 	return tmp;
17304778e0beSJiri Pirko }
17314778e0beSJiri Pirko 
17324778e0beSJiri Pirko /**
1733bfa83a9eSThomas Graf  * nla_get_flag - return payload of flag attribute
1734bfa83a9eSThomas Graf  * @nla: flag netlink attribute
1735bfa83a9eSThomas Graf  */
nla_get_flag(const struct nlattr * nla)1736b057efd4SPatrick McHardy static inline int nla_get_flag(const struct nlattr *nla)
1737bfa83a9eSThomas Graf {
1738bfa83a9eSThomas Graf 	return !!nla;
1739bfa83a9eSThomas Graf }
1740bfa83a9eSThomas Graf 
1741bfa83a9eSThomas Graf /**
1742bfa83a9eSThomas Graf  * nla_get_msecs - return payload of msecs attribute
1743bfa83a9eSThomas Graf  * @nla: msecs netlink attribute
1744bfa83a9eSThomas Graf  *
1745bfa83a9eSThomas Graf  * Returns the number of milliseconds in jiffies.
1746bfa83a9eSThomas Graf  */
nla_get_msecs(const struct nlattr * nla)1747b057efd4SPatrick McHardy static inline unsigned long nla_get_msecs(const struct nlattr *nla)
1748bfa83a9eSThomas Graf {
1749bfa83a9eSThomas Graf 	u64 msecs = nla_get_u64(nla);
1750bfa83a9eSThomas Graf 
1751bfa83a9eSThomas Graf 	return msecs_to_jiffies((unsigned long) msecs);
1752bfa83a9eSThomas Graf }
1753bfa83a9eSThomas Graf 
1754bfa83a9eSThomas Graf /**
175567b61f6cSJiri Benc  * nla_get_in_addr - return payload of IPv4 address attribute
175667b61f6cSJiri Benc  * @nla: IPv4 address netlink attribute
175767b61f6cSJiri Benc  */
nla_get_in_addr(const struct nlattr * nla)175867b61f6cSJiri Benc static inline __be32 nla_get_in_addr(const struct nlattr *nla)
175967b61f6cSJiri Benc {
176067b61f6cSJiri Benc 	return *(__be32 *) nla_data(nla);
176167b61f6cSJiri Benc }
176267b61f6cSJiri Benc 
176367b61f6cSJiri Benc /**
176467b61f6cSJiri Benc  * nla_get_in6_addr - return payload of IPv6 address attribute
176567b61f6cSJiri Benc  * @nla: IPv6 address netlink attribute
176667b61f6cSJiri Benc  */
nla_get_in6_addr(const struct nlattr * nla)176767b61f6cSJiri Benc static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla)
176867b61f6cSJiri Benc {
176967b61f6cSJiri Benc 	struct in6_addr tmp;
177067b61f6cSJiri Benc 
177167b61f6cSJiri Benc 	nla_memcpy(&tmp, nla, sizeof(tmp));
177267b61f6cSJiri Benc 	return tmp;
177367b61f6cSJiri Benc }
177467b61f6cSJiri Benc 
177567b61f6cSJiri Benc /**
177664c83d83SJamal Hadi Salim  * nla_get_bitfield32 - return payload of 32 bitfield attribute
177764c83d83SJamal Hadi Salim  * @nla: nla_bitfield32 attribute
177864c83d83SJamal Hadi Salim  */
nla_get_bitfield32(const struct nlattr * nla)177964c83d83SJamal Hadi Salim static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla)
178064c83d83SJamal Hadi Salim {
178164c83d83SJamal Hadi Salim 	struct nla_bitfield32 tmp;
178264c83d83SJamal Hadi Salim 
178364c83d83SJamal Hadi Salim 	nla_memcpy(&tmp, nla, sizeof(tmp));
178464c83d83SJamal Hadi Salim 	return tmp;
178564c83d83SJamal Hadi Salim }
178664c83d83SJamal Hadi Salim 
178764c83d83SJamal Hadi Salim /**
1788b15ca182SThomas Graf  * nla_memdup - duplicate attribute memory (kmemdup)
1789b15ca182SThomas Graf  * @src: netlink attribute to duplicate from
1790b15ca182SThomas Graf  * @gfp: GFP mask
1791b15ca182SThomas Graf  */
nla_memdup(const struct nlattr * src,gfp_t gfp)1792b15ca182SThomas Graf static inline void *nla_memdup(const struct nlattr *src, gfp_t gfp)
1793b15ca182SThomas Graf {
1794b15ca182SThomas Graf 	return kmemdup(nla_data(src), nla_len(src), gfp);
1795b15ca182SThomas Graf }
1796b15ca182SThomas Graf 
1797b15ca182SThomas Graf /**
1798ae0be8deSMichal Kubecek  * nla_nest_start_noflag - Start a new level of nested attributes
1799bfa83a9eSThomas Graf  * @skb: socket buffer to add attributes to
1800bfa83a9eSThomas Graf  * @attrtype: attribute type of container
1801bfa83a9eSThomas Graf  *
1802ae0be8deSMichal Kubecek  * This function exists for backward compatibility to use in APIs which never
1803ae0be8deSMichal Kubecek  * marked their nest attributes with NLA_F_NESTED flag. New APIs should use
1804ae0be8deSMichal Kubecek  * nla_nest_start() which sets the flag.
1805ae0be8deSMichal Kubecek  *
1806ae0be8deSMichal Kubecek  * Returns the container attribute or NULL on error
1807bfa83a9eSThomas Graf  */
nla_nest_start_noflag(struct sk_buff * skb,int attrtype)1808ae0be8deSMichal Kubecek static inline struct nlattr *nla_nest_start_noflag(struct sk_buff *skb,
1809ae0be8deSMichal Kubecek 						   int attrtype)
1810bfa83a9eSThomas Graf {
181127a884dcSArnaldo Carvalho de Melo 	struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
1812bfa83a9eSThomas Graf 
1813bfa83a9eSThomas Graf 	if (nla_put(skb, attrtype, 0, NULL) < 0)
1814bfa83a9eSThomas Graf 		return NULL;
1815bfa83a9eSThomas Graf 
1816bfa83a9eSThomas Graf 	return start;
1817bfa83a9eSThomas Graf }
1818bfa83a9eSThomas Graf 
1819bfa83a9eSThomas Graf /**
1820ae0be8deSMichal Kubecek  * nla_nest_start - Start a new level of nested attributes, with NLA_F_NESTED
1821ae0be8deSMichal Kubecek  * @skb: socket buffer to add attributes to
1822ae0be8deSMichal Kubecek  * @attrtype: attribute type of container
1823ae0be8deSMichal Kubecek  *
1824ae0be8deSMichal Kubecek  * Unlike nla_nest_start_noflag(), mark the nest attribute with NLA_F_NESTED
1825ae0be8deSMichal Kubecek  * flag. This is the preferred function to use in new code.
1826ae0be8deSMichal Kubecek  *
1827ae0be8deSMichal Kubecek  * Returns the container attribute or NULL on error
1828ae0be8deSMichal Kubecek  */
nla_nest_start(struct sk_buff * skb,int attrtype)1829ae0be8deSMichal Kubecek static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
1830ae0be8deSMichal Kubecek {
1831ae0be8deSMichal Kubecek 	return nla_nest_start_noflag(skb, attrtype | NLA_F_NESTED);
1832ae0be8deSMichal Kubecek }
1833ae0be8deSMichal Kubecek 
1834ae0be8deSMichal Kubecek /**
1835bfa83a9eSThomas Graf  * nla_nest_end - Finalize nesting of attributes
1836d1ec3b77SPierre Ynard  * @skb: socket buffer the attributes are stored in
1837bfa83a9eSThomas Graf  * @start: container attribute
1838bfa83a9eSThomas Graf  *
1839bfa83a9eSThomas Graf  * Corrects the container attribute header to include the all
1840bfa83a9eSThomas Graf  * appeneded attributes.
1841bfa83a9eSThomas Graf  *
1842bfa83a9eSThomas Graf  * Returns the total data length of the skb.
1843bfa83a9eSThomas Graf  */
nla_nest_end(struct sk_buff * skb,struct nlattr * start)1844bfa83a9eSThomas Graf static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
1845bfa83a9eSThomas Graf {
184627a884dcSArnaldo Carvalho de Melo 	start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
1847bfa83a9eSThomas Graf 	return skb->len;
1848bfa83a9eSThomas Graf }
1849bfa83a9eSThomas Graf 
1850bfa83a9eSThomas Graf /**
1851bfa83a9eSThomas Graf  * nla_nest_cancel - Cancel nesting of attributes
1852bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
1853bfa83a9eSThomas Graf  * @start: container attribute
1854bfa83a9eSThomas Graf  *
1855bfa83a9eSThomas Graf  * Removes the container attribute and including all nested
1856bc3ed28cSThomas Graf  * attributes. Returns -EMSGSIZE
1857bfa83a9eSThomas Graf  */
nla_nest_cancel(struct sk_buff * skb,struct nlattr * start)1858bc3ed28cSThomas Graf static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
1859bfa83a9eSThomas Graf {
1860bc3ed28cSThomas Graf 	nlmsg_trim(skb, start);
1861bfa83a9eSThomas Graf }
1862bfa83a9eSThomas Graf 
1863bfa83a9eSThomas Graf /**
186432d5109aSMichal Kubecek  * __nla_validate_nested - Validate a stream of nested attributes
18654fe5d5c0SPaul Moore  * @start: container attribute
18664fe5d5c0SPaul Moore  * @maxtype: maximum attribute type to be expected
18674fe5d5c0SPaul Moore  * @policy: validation policy
18688cb08174SJohannes Berg  * @validate: validation strictness
1869fceb6435SJohannes Berg  * @extack: extended ACK report struct
18704fe5d5c0SPaul Moore  *
18714fe5d5c0SPaul Moore  * Validates all attributes in the nested attribute stream against the
18724fe5d5c0SPaul Moore  * specified policy. Attributes with a type exceeding maxtype will be
18734fe5d5c0SPaul Moore  * ignored. See documenation of struct nla_policy for more details.
18744fe5d5c0SPaul Moore  *
18754fe5d5c0SPaul Moore  * Returns 0 on success or a negative error code.
18764fe5d5c0SPaul Moore  */
__nla_validate_nested(const struct nlattr * start,int maxtype,const struct nla_policy * policy,unsigned int validate,struct netlink_ext_ack * extack)18778cb08174SJohannes Berg static inline int __nla_validate_nested(const struct nlattr *start, int maxtype,
18788cb08174SJohannes Berg 					const struct nla_policy *policy,
18798cb08174SJohannes Berg 					unsigned int validate,
18808cb08174SJohannes Berg 					struct netlink_ext_ack *extack)
18818cb08174SJohannes Berg {
18828cb08174SJohannes Berg 	return __nla_validate(nla_data(start), nla_len(start), maxtype, policy,
18838cb08174SJohannes Berg 			      validate, extack);
18848cb08174SJohannes Berg }
18858cb08174SJohannes Berg 
18868cb08174SJohannes Berg static inline int
nla_validate_nested(const struct nlattr * start,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)188732d5109aSMichal Kubecek nla_validate_nested(const struct nlattr *start, int maxtype,
1888901bb989SJohannes Berg 		    const struct nla_policy *policy,
1889901bb989SJohannes Berg 		    struct netlink_ext_ack *extack)
1890901bb989SJohannes Berg {
1891901bb989SJohannes Berg 	return __nla_validate_nested(start, maxtype, policy,
1892901bb989SJohannes Berg 				     NL_VALIDATE_STRICT, extack);
1893901bb989SJohannes Berg }
1894901bb989SJohannes Berg 
1895901bb989SJohannes Berg static inline int
nla_validate_nested_deprecated(const struct nlattr * start,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)18968cb08174SJohannes Berg nla_validate_nested_deprecated(const struct nlattr *start, int maxtype,
1897fceb6435SJohannes Berg 			       const struct nla_policy *policy,
1898fceb6435SJohannes Berg 			       struct netlink_ext_ack *extack)
18994fe5d5c0SPaul Moore {
19008cb08174SJohannes Berg 	return __nla_validate_nested(start, maxtype, policy,
19018cb08174SJohannes Berg 				     NL_VALIDATE_LIBERAL, extack);
19024fe5d5c0SPaul Moore }
19034fe5d5c0SPaul Moore 
19044fe5d5c0SPaul Moore /**
1905089bf1a6SNicolas Dichtel  * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute
1906089bf1a6SNicolas Dichtel  * @skb: socket buffer the message is stored in
1907089bf1a6SNicolas Dichtel  *
1908089bf1a6SNicolas Dichtel  * Return true if padding is needed to align the next attribute (nla_data()) to
1909089bf1a6SNicolas Dichtel  * a 64-bit aligned area.
1910089bf1a6SNicolas Dichtel  */
nla_need_padding_for_64bit(struct sk_buff * skb)1911089bf1a6SNicolas Dichtel static inline bool nla_need_padding_for_64bit(struct sk_buff *skb)
1912089bf1a6SNicolas Dichtel {
1913089bf1a6SNicolas Dichtel #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1914089bf1a6SNicolas Dichtel 	/* The nlattr header is 4 bytes in size, that's why we test
1915089bf1a6SNicolas Dichtel 	 * if the skb->data _is_ aligned.  A NOP attribute, plus
1916089bf1a6SNicolas Dichtel 	 * nlattr header for next attribute, will make nla_data()
1917089bf1a6SNicolas Dichtel 	 * 8-byte aligned.
1918089bf1a6SNicolas Dichtel 	 */
1919089bf1a6SNicolas Dichtel 	if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
1920089bf1a6SNicolas Dichtel 		return true;
1921089bf1a6SNicolas Dichtel #endif
1922089bf1a6SNicolas Dichtel 	return false;
1923089bf1a6SNicolas Dichtel }
1924089bf1a6SNicolas Dichtel 
1925089bf1a6SNicolas Dichtel /**
192635c58459SDavid S. Miller  * nla_align_64bit - 64-bit align the nla_data() of next attribute
192735c58459SDavid S. Miller  * @skb: socket buffer the message is stored in
192835c58459SDavid S. Miller  * @padattr: attribute type for the padding
192935c58459SDavid S. Miller  *
193035c58459SDavid S. Miller  * Conditionally emit a padding netlink attribute in order to make
193135c58459SDavid S. Miller  * the next attribute we emit have a 64-bit aligned nla_data() area.
193235c58459SDavid S. Miller  * This will only be done in architectures which do not have
1933cca1d815SEric Dumazet  * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
193435c58459SDavid S. Miller  *
193535c58459SDavid S. Miller  * Returns zero on success or a negative error code.
193635c58459SDavid S. Miller  */
nla_align_64bit(struct sk_buff * skb,int padattr)193735c58459SDavid S. Miller static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
193835c58459SDavid S. Miller {
1939089bf1a6SNicolas Dichtel 	if (nla_need_padding_for_64bit(skb) &&
1940cca1d815SEric Dumazet 	    !nla_reserve(skb, padattr, 0))
194135c58459SDavid S. Miller 		return -EMSGSIZE;
1942089bf1a6SNicolas Dichtel 
194335c58459SDavid S. Miller 	return 0;
194435c58459SDavid S. Miller }
194535c58459SDavid S. Miller 
194635c58459SDavid S. Miller /**
194735c58459SDavid S. Miller  * nla_total_size_64bit - total length of attribute including padding
194835c58459SDavid S. Miller  * @payload: length of payload
194935c58459SDavid S. Miller  */
nla_total_size_64bit(int payload)195035c58459SDavid S. Miller static inline int nla_total_size_64bit(int payload)
195135c58459SDavid S. Miller {
195235c58459SDavid S. Miller 	return NLA_ALIGN(nla_attr_size(payload))
1953cca1d815SEric Dumazet #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
195435c58459SDavid S. Miller 		+ NLA_ALIGN(nla_attr_size(0))
195535c58459SDavid S. Miller #endif
195635c58459SDavid S. Miller 		;
195735c58459SDavid S. Miller }
195835c58459SDavid S. Miller 
195935c58459SDavid S. Miller /**
1960bfa83a9eSThomas Graf  * nla_for_each_attr - iterate over a stream of attributes
1961bfa83a9eSThomas Graf  * @pos: loop counter, set to current attribute
1962bfa83a9eSThomas Graf  * @head: head of attribute stream
1963bfa83a9eSThomas Graf  * @len: length of attribute stream
1964bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
1965bfa83a9eSThomas Graf  */
1966bfa83a9eSThomas Graf #define nla_for_each_attr(pos, head, len, rem) \
1967bfa83a9eSThomas Graf 	for (pos = head, rem = len; \
1968bfa83a9eSThomas Graf 	     nla_ok(pos, rem); \
1969bfa83a9eSThomas Graf 	     pos = nla_next(pos, &(rem)))
1970bfa83a9eSThomas Graf 
1971fe4944e5SThomas Graf /**
1972fe4944e5SThomas Graf  * nla_for_each_nested - iterate over nested attributes
1973fe4944e5SThomas Graf  * @pos: loop counter, set to current attribute
1974fe4944e5SThomas Graf  * @nla: attribute containing the nested attributes
1975fe4944e5SThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
1976fe4944e5SThomas Graf  */
1977fe4944e5SThomas Graf #define nla_for_each_nested(pos, nla, rem) \
1978fe4944e5SThomas Graf 	nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
1979fe4944e5SThomas Graf 
1980941d8ebcSSimon Horman /**
1981941d8ebcSSimon Horman  * nla_is_last - Test if attribute is last in stream
1982941d8ebcSSimon Horman  * @nla: attribute to test
1983941d8ebcSSimon Horman  * @rem: bytes remaining in stream
1984941d8ebcSSimon Horman  */
nla_is_last(const struct nlattr * nla,int rem)1985941d8ebcSSimon Horman static inline bool nla_is_last(const struct nlattr *nla, int rem)
1986941d8ebcSSimon Horman {
1987941d8ebcSSimon Horman 	return nla->nla_len == rem;
1988941d8ebcSSimon Horman }
1989941d8ebcSSimon Horman 
19902c28ae48SJohannes Berg void nla_get_range_unsigned(const struct nla_policy *pt,
19912c28ae48SJohannes Berg 			    struct netlink_range_validation *range);
19922c28ae48SJohannes Berg void nla_get_range_signed(const struct nla_policy *pt,
19932c28ae48SJohannes Berg 			  struct netlink_range_validation_signed *range);
19942c28ae48SJohannes Berg 
1995adc84845SJakub Kicinski struct netlink_policy_dump_state;
1996adc84845SJakub Kicinski 
199704a351a6SJohannes Berg int netlink_policy_dump_add_policy(struct netlink_policy_dump_state **pstate,
199804a351a6SJohannes Berg 				   const struct nla_policy *policy,
199904a351a6SJohannes Berg 				   unsigned int maxtype);
200004a351a6SJohannes Berg int netlink_policy_dump_get_policy_idx(struct netlink_policy_dump_state *state,
200104a351a6SJohannes Berg 				       const struct nla_policy *policy,
200204a351a6SJohannes Berg 				       unsigned int maxtype);
2003adc84845SJakub Kicinski bool netlink_policy_dump_loop(struct netlink_policy_dump_state *state);
2004adc84845SJakub Kicinski int netlink_policy_dump_write(struct sk_buff *skb,
2005adc84845SJakub Kicinski 			      struct netlink_policy_dump_state *state);
200644f3625bSJohannes Berg int netlink_policy_dump_attr_size_estimate(const struct nla_policy *pt);
200744f3625bSJohannes Berg int netlink_policy_dump_write_attr(struct sk_buff *skb,
200844f3625bSJohannes Berg 				   const struct nla_policy *pt,
200944f3625bSJohannes Berg 				   int nestattr);
2010adc84845SJakub Kicinski void netlink_policy_dump_free(struct netlink_policy_dump_state *state);
2011d07dcf9aSJohannes Berg 
2012bfa83a9eSThomas Graf #endif
2013