xref: /openbmc/linux/include/net/netlink.h (revision 32d5109a)
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
145bfa83a9eSThomas Graf  *   nla_strlcpy(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,
184b60b87fcSJohannes Berg 	NLA_EXACT_LEN,
185b60b87fcSJohannes Berg 	NLA_EXACT_LEN_WARN,
1866f455f5fSJohannes Berg 	NLA_MIN_LEN,
187bfa83a9eSThomas Graf 	__NLA_TYPE_MAX,
188bfa83a9eSThomas Graf };
189bfa83a9eSThomas Graf 
190bfa83a9eSThomas Graf #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
191bfa83a9eSThomas Graf 
1923e48be05SJohannes Berg enum nla_policy_validation {
1933e48be05SJohannes Berg 	NLA_VALIDATE_NONE,
1943e48be05SJohannes Berg 	NLA_VALIDATE_RANGE,
1953e48be05SJohannes Berg 	NLA_VALIDATE_MIN,
1963e48be05SJohannes Berg 	NLA_VALIDATE_MAX,
19733188bd6SJohannes Berg 	NLA_VALIDATE_FUNCTION,
1983e48be05SJohannes Berg };
1993e48be05SJohannes Berg 
200bfa83a9eSThomas Graf /**
201bfa83a9eSThomas Graf  * struct nla_policy - attribute validation policy
202bfa83a9eSThomas Graf  * @type: Type of attribute or NLA_UNSPEC
2033e48be05SJohannes Berg  * @validation_type: type of attribute validation done in addition to
20433188bd6SJohannes Berg  *	type-specific validation (e.g. range, function call), see
2053e48be05SJohannes Berg  *	&enum nla_policy_validation
206a5531a5dSThomas Graf  * @len: Type specific length of payload
207bfa83a9eSThomas Graf  *
208bfa83a9eSThomas Graf  * Policies are defined as arrays of this struct, the array must be
209bfa83a9eSThomas Graf  * accessible by attribute type up to the highest identifier to be expected.
210bfa83a9eSThomas Graf  *
211a5531a5dSThomas Graf  * Meaning of `len' field:
212a5531a5dSThomas Graf  *    NLA_STRING           Maximum length of string
213a5531a5dSThomas Graf  *    NLA_NUL_STRING       Maximum length of string (excluding NUL)
214a5531a5dSThomas Graf  *    NLA_FLAG             Unused
215d30045a0SJohannes Berg  *    NLA_BINARY           Maximum length of attribute payload
2166f455f5fSJohannes Berg  *    NLA_MIN_LEN          Minimum length of attribute payload
2171501d135SJohannes Berg  *    NLA_NESTED,
2181501d135SJohannes Berg  *    NLA_NESTED_ARRAY     Length verification is done by checking len of
2199a659a35SJohannes Berg  *                         nested header (or empty); len field is used if
2209a659a35SJohannes Berg  *                         validation_data is also used, for the max attr
2219a659a35SJohannes Berg  *                         number in the nested policy.
2224b6cc728SJohannes Berg  *    NLA_U8, NLA_U16,
2234b6cc728SJohannes Berg  *    NLA_U32, NLA_U64,
2244778e0beSJiri Pirko  *    NLA_S8, NLA_S16,
2254778e0beSJiri Pirko  *    NLA_S32, NLA_S64,
2264b6cc728SJohannes Berg  *    NLA_MSECS            Leaving the length field zero will verify the
2274b6cc728SJohannes Berg  *                         given type fits, using it verifies minimum length
2284b6cc728SJohannes Berg  *                         just like "All other"
229568b742aSJohannes Berg  *    NLA_BITFIELD32       Unused
230568b742aSJohannes Berg  *    NLA_REJECT           Unused
231b60b87fcSJohannes Berg  *    NLA_EXACT_LEN        Attribute must have exactly this length, otherwise
232b60b87fcSJohannes Berg  *                         it is rejected.
233b60b87fcSJohannes Berg  *    NLA_EXACT_LEN_WARN   Attribute should have exactly this length, a warning
234b60b87fcSJohannes Berg  *                         is logged if it is longer, shorter is rejected.
2356f455f5fSJohannes Berg  *    NLA_MIN_LEN          Minimum length of attribute payload
2364b6cc728SJohannes Berg  *    All other            Minimum length of attribute payload
237a5531a5dSThomas Graf  *
238568b742aSJohannes Berg  * Meaning of `validation_data' field:
239568b742aSJohannes Berg  *    NLA_BITFIELD32       This is a 32-bit bitmap/bitselector attribute and
240568b742aSJohannes Berg  *                         validation data must point to a u32 value of valid
241568b742aSJohannes Berg  *                         flags
242568b742aSJohannes Berg  *    NLA_REJECT           This attribute is always rejected and validation data
243568b742aSJohannes Berg  *                         may point to a string to report as the error instead
244568b742aSJohannes Berg  *                         of the generic one in extended ACK.
2459a659a35SJohannes Berg  *    NLA_NESTED           Points to a nested policy to validate, must also set
2469a659a35SJohannes Berg  *                         `len' to the max attribute number.
2479a659a35SJohannes Berg  *                         Note that nla_parse() will validate, but of course not
2489a659a35SJohannes Berg  *                         parse, the nested sub-policies.
2491501d135SJohannes Berg  *    NLA_NESTED_ARRAY     Points to a nested policy to validate, must also set
2501501d135SJohannes Berg  *                         `len' to the max attribute number. The difference to
2511501d135SJohannes Berg  *                         NLA_NESTED is the structure - NLA_NESTED has the
2521501d135SJohannes Berg  *                         nested attributes directly inside, while an array has
2531501d135SJohannes Berg  *                         the nested attributes at another level down and the
2541501d135SJohannes Berg  *                         attributes directly in the nesting don't matter.
2553e48be05SJohannes Berg  *    All other            Unused - but note that it's a union
2563e48be05SJohannes Berg  *
2573e48be05SJohannes Berg  * Meaning of `min' and `max' fields, use via NLA_POLICY_MIN, NLA_POLICY_MAX
2583e48be05SJohannes Berg  * and NLA_POLICY_RANGE:
2593e48be05SJohannes Berg  *    NLA_U8,
2603e48be05SJohannes Berg  *    NLA_U16,
2613e48be05SJohannes Berg  *    NLA_U32,
2623e48be05SJohannes Berg  *    NLA_U64,
2633e48be05SJohannes Berg  *    NLA_S8,
2643e48be05SJohannes Berg  *    NLA_S16,
2653e48be05SJohannes Berg  *    NLA_S32,
2663e48be05SJohannes Berg  *    NLA_S64              These are used depending on the validation_type
2673e48be05SJohannes Berg  *                         field, if that is min/max/range then the minimum,
2683e48be05SJohannes Berg  *                         maximum and both are used (respectively) to check
2693e48be05SJohannes Berg  *                         the value of the integer attribute.
2703e48be05SJohannes Berg  *                         Note that in the interest of code simplicity and
2713e48be05SJohannes Berg  *                         struct size both limits are s16, so you cannot
2723e48be05SJohannes Berg  *                         enforce a range that doesn't fall within the range
2733e48be05SJohannes Berg  *                         of s16 - do that as usual in the code instead.
2743e48be05SJohannes Berg  *    All other            Unused - but note that it's a union
275568b742aSJohannes Berg  *
27633188bd6SJohannes Berg  * Meaning of `validate' field, use via NLA_POLICY_VALIDATE_FN:
27733188bd6SJohannes Berg  *    NLA_BINARY           Validation function called for the attribute,
27833188bd6SJohannes Berg  *                         not compatible with use of the validation_data
27933188bd6SJohannes Berg  *                         as in NLA_BITFIELD32, NLA_REJECT, NLA_NESTED and
28033188bd6SJohannes Berg  *                         NLA_NESTED_ARRAY.
28133188bd6SJohannes Berg  *    All other            Unused - but note that it's a union
28233188bd6SJohannes Berg  *
283bfa83a9eSThomas Graf  * Example:
284b54452b0SAlexey Dobriyan  * static const struct nla_policy my_policy[ATTR_MAX+1] = {
285bfa83a9eSThomas Graf  * 	[ATTR_FOO] = { .type = NLA_U16 },
286d30045a0SJohannes Berg  *	[ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
2876f455f5fSJohannes Berg  *	[ATTR_BAZ] = { .type = NLA_EXACT_LEN, .len = sizeof(struct mystruct) },
28864c83d83SJamal Hadi Salim  *	[ATTR_GOO] = { .type = NLA_BITFIELD32, .validation_data = &myvalidflags },
289bfa83a9eSThomas Graf  * };
290bfa83a9eSThomas Graf  */
291bfa83a9eSThomas Graf struct nla_policy {
2923e48be05SJohannes Berg 	u8		type;
2933e48be05SJohannes Berg 	u8		validation_type;
294a5531a5dSThomas Graf 	u16		len;
2953e48be05SJohannes Berg 	union {
29648fde90aSJohannes Berg 		const void *validation_data;
2973e48be05SJohannes Berg 		struct {
2983e48be05SJohannes Berg 			s16 min, max;
2993e48be05SJohannes Berg 		};
30033188bd6SJohannes Berg 		int (*validate)(const struct nlattr *attr,
30133188bd6SJohannes Berg 				struct netlink_ext_ack *extack);
30256738f46SJohannes Berg 		/* This entry is special, and used for the attribute at index 0
30356738f46SJohannes Berg 		 * only, and specifies special data about the policy, namely it
30456738f46SJohannes Berg 		 * specifies the "boundary type" where strict length validation
30556738f46SJohannes Berg 		 * starts for any attribute types >= this value, also, strict
30656738f46SJohannes Berg 		 * nesting validation starts here.
30756738f46SJohannes Berg 		 *
30856738f46SJohannes Berg 		 * Additionally, it means that NLA_UNSPEC is actually NLA_REJECT
30956738f46SJohannes Berg 		 * for any types >= this, so need to use NLA_MIN_LEN to get the
31056738f46SJohannes Berg 		 * previous pure { .len = xyz } behaviour. The advantage of this
31156738f46SJohannes Berg 		 * is that types not specified in the policy will be rejected.
31256738f46SJohannes Berg 		 *
31356738f46SJohannes Berg 		 * For completely new families it should be set to 1 so that the
31456738f46SJohannes Berg 		 * validation is enforced for all attributes. For existing ones
31556738f46SJohannes Berg 		 * it should be set at least when new attributes are added to
31656738f46SJohannes Berg 		 * the enum used by the policy, and be set to the new value that
31756738f46SJohannes Berg 		 * was added to enforce strict validation from thereon.
31856738f46SJohannes Berg 		 */
31956738f46SJohannes Berg 		u16 strict_start_type;
3203e48be05SJohannes Berg 	};
321bfa83a9eSThomas Graf };
322bfa83a9eSThomas Graf 
323b60b87fcSJohannes Berg #define NLA_POLICY_EXACT_LEN(_len)	{ .type = NLA_EXACT_LEN, .len = _len }
324b60b87fcSJohannes Berg #define NLA_POLICY_EXACT_LEN_WARN(_len)	{ .type = NLA_EXACT_LEN_WARN, \
325b60b87fcSJohannes Berg 					  .len = _len }
3266f455f5fSJohannes Berg #define NLA_POLICY_MIN_LEN(_len)	{ .type = NLA_MIN_LEN, .len = _len }
327b60b87fcSJohannes Berg 
328b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR		NLA_POLICY_EXACT_LEN(ETH_ALEN)
329b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR_COMPAT	NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN)
330b60b87fcSJohannes Berg 
33123323289SJohannes Berg #define _NLA_POLICY_NESTED(maxattr, policy) \
3329a659a35SJohannes Berg 	{ .type = NLA_NESTED, .validation_data = policy, .len = maxattr }
33323323289SJohannes Berg #define _NLA_POLICY_NESTED_ARRAY(maxattr, policy) \
3341501d135SJohannes Berg 	{ .type = NLA_NESTED_ARRAY, .validation_data = policy, .len = maxattr }
33523323289SJohannes Berg #define NLA_POLICY_NESTED(policy) \
33623323289SJohannes Berg 	_NLA_POLICY_NESTED(ARRAY_SIZE(policy) - 1, policy)
33723323289SJohannes Berg #define NLA_POLICY_NESTED_ARRAY(policy) \
33823323289SJohannes Berg 	_NLA_POLICY_NESTED_ARRAY(ARRAY_SIZE(policy) - 1, policy)
3399a659a35SJohannes Berg 
3405886d932SJohannes Berg #define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition))
3413e48be05SJohannes Berg #define NLA_ENSURE_INT_TYPE(tp)				\
3423e48be05SJohannes Berg 	(__NLA_ENSURE(tp == NLA_S8 || tp == NLA_U8 ||	\
3433e48be05SJohannes Berg 		      tp == NLA_S16 || tp == NLA_U16 ||	\
3443e48be05SJohannes Berg 		      tp == NLA_S32 || tp == NLA_U32 ||	\
3453e48be05SJohannes Berg 		      tp == NLA_S64 || tp == NLA_U64) + tp)
34633188bd6SJohannes Berg #define NLA_ENSURE_NO_VALIDATION_PTR(tp)		\
34733188bd6SJohannes Berg 	(__NLA_ENSURE(tp != NLA_BITFIELD32 &&		\
34833188bd6SJohannes Berg 		      tp != NLA_REJECT &&		\
34933188bd6SJohannes Berg 		      tp != NLA_NESTED &&		\
35033188bd6SJohannes Berg 		      tp != NLA_NESTED_ARRAY) + tp)
3513e48be05SJohannes Berg 
3523e48be05SJohannes Berg #define NLA_POLICY_RANGE(tp, _min, _max) {		\
3533e48be05SJohannes Berg 	.type = NLA_ENSURE_INT_TYPE(tp),		\
3543e48be05SJohannes Berg 	.validation_type = NLA_VALIDATE_RANGE,		\
3553e48be05SJohannes Berg 	.min = _min,					\
3563e48be05SJohannes Berg 	.max = _max					\
3573e48be05SJohannes Berg }
3583e48be05SJohannes Berg 
3593e48be05SJohannes Berg #define NLA_POLICY_MIN(tp, _min) {			\
3603e48be05SJohannes Berg 	.type = NLA_ENSURE_INT_TYPE(tp),		\
3613e48be05SJohannes Berg 	.validation_type = NLA_VALIDATE_MIN,		\
3623e48be05SJohannes Berg 	.min = _min,					\
3633e48be05SJohannes Berg }
3643e48be05SJohannes Berg 
3653e48be05SJohannes Berg #define NLA_POLICY_MAX(tp, _max) {			\
3663e48be05SJohannes Berg 	.type = NLA_ENSURE_INT_TYPE(tp),		\
3673e48be05SJohannes Berg 	.validation_type = NLA_VALIDATE_MAX,		\
3683e48be05SJohannes Berg 	.max = _max,					\
3693e48be05SJohannes Berg }
3703e48be05SJohannes Berg 
37133188bd6SJohannes Berg #define NLA_POLICY_VALIDATE_FN(tp, fn, ...) {		\
37233188bd6SJohannes Berg 	.type = NLA_ENSURE_NO_VALIDATION_PTR(tp),	\
37333188bd6SJohannes Berg 	.validation_type = NLA_VALIDATE_FUNCTION,	\
37433188bd6SJohannes Berg 	.validate = fn,					\
37533188bd6SJohannes Berg 	.len = __VA_ARGS__ + 0,				\
37633188bd6SJohannes Berg }
37733188bd6SJohannes Berg 
3784e902c57SThomas Graf /**
3794e902c57SThomas Graf  * struct nl_info - netlink source information
3804e902c57SThomas Graf  * @nlh: Netlink message header of original request
3813de205cdSIdo Schimmel  * @nl_net: Network namespace
38215e47304SEric W. Biederman  * @portid: Netlink PORTID of requesting application
3833de205cdSIdo Schimmel  * @skip_notify: Skip netlink notifications to user space
384c82481f7SIdo Schimmel  * @skip_notify_kernel: Skip selected in-kernel notifications
3854e902c57SThomas Graf  */
3864e902c57SThomas Graf struct nl_info {
3874e902c57SThomas Graf 	struct nlmsghdr		*nlh;
3884d1169c1SDenis V. Lunev 	struct net		*nl_net;
38915e47304SEric W. Biederman 	u32			portid;
390c82481f7SIdo Schimmel 	u8			skip_notify:1,
391c82481f7SIdo Schimmel 				skip_notify_kernel:1;
3924e902c57SThomas Graf };
3934e902c57SThomas Graf 
3948cb08174SJohannes Berg /**
3958cb08174SJohannes Berg  * enum netlink_validation - netlink message/attribute validation levels
3968cb08174SJohannes Berg  * @NL_VALIDATE_LIBERAL: Old-style "be liberal" validation, not caring about
3978cb08174SJohannes Berg  *	extra data at the end of the message, attributes being longer than
3988cb08174SJohannes Berg  *	they should be, or unknown attributes being present.
3998cb08174SJohannes Berg  * @NL_VALIDATE_TRAILING: Reject junk data encountered after attribute parsing.
4008cb08174SJohannes Berg  * @NL_VALIDATE_MAXTYPE: Reject attributes > max type; Together with _TRAILING
4018cb08174SJohannes Berg  *	this is equivalent to the old nla_parse_strict()/nlmsg_parse_strict().
4028cb08174SJohannes Berg  * @NL_VALIDATE_UNSPEC: Reject attributes with NLA_UNSPEC in the policy.
4038cb08174SJohannes Berg  *	This can safely be set by the kernel when the given policy has no
4048cb08174SJohannes Berg  *	NLA_UNSPEC anymore, and can thus be used to ensure policy entries
4058cb08174SJohannes Berg  *	are enforced going forward.
4068cb08174SJohannes Berg  * @NL_VALIDATE_STRICT_ATTRS: strict attribute policy parsing (e.g.
4078cb08174SJohannes Berg  *	U8, U16, U32 must have exact size, etc.)
408b424e432SMichal Kubecek  * @NL_VALIDATE_NESTED: Check that NLA_F_NESTED is set for NLA_NESTED(_ARRAY)
409b424e432SMichal Kubecek  *	and unset for other policies.
4108cb08174SJohannes Berg  */
4118cb08174SJohannes Berg enum netlink_validation {
4128cb08174SJohannes Berg 	NL_VALIDATE_LIBERAL = 0,
4138cb08174SJohannes Berg 	NL_VALIDATE_TRAILING = BIT(0),
4148cb08174SJohannes Berg 	NL_VALIDATE_MAXTYPE = BIT(1),
4158cb08174SJohannes Berg 	NL_VALIDATE_UNSPEC = BIT(2),
4168cb08174SJohannes Berg 	NL_VALIDATE_STRICT_ATTRS = BIT(3),
417b424e432SMichal Kubecek 	NL_VALIDATE_NESTED = BIT(4),
4188cb08174SJohannes Berg };
4198cb08174SJohannes Berg 
4208cb08174SJohannes Berg #define NL_VALIDATE_DEPRECATED_STRICT (NL_VALIDATE_TRAILING |\
4218cb08174SJohannes Berg 				       NL_VALIDATE_MAXTYPE)
4228cb08174SJohannes Berg #define NL_VALIDATE_STRICT (NL_VALIDATE_TRAILING |\
4238cb08174SJohannes Berg 			    NL_VALIDATE_MAXTYPE |\
4248cb08174SJohannes Berg 			    NL_VALIDATE_UNSPEC |\
425b424e432SMichal Kubecek 			    NL_VALIDATE_STRICT_ATTRS |\
426b424e432SMichal Kubecek 			    NL_VALIDATE_NESTED)
4278cb08174SJohannes Berg 
4284f69053bSJoe Perches int netlink_rcv_skb(struct sk_buff *skb,
4292d4bc933SJohannes Berg 		    int (*cb)(struct sk_buff *, struct nlmsghdr *,
4302d4bc933SJohannes Berg 			      struct netlink_ext_ack *));
4314f69053bSJoe Perches int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
4324f69053bSJoe Perches 		 unsigned int group, int report, gfp_t flags);
43382ace47aSThomas Graf 
4348cb08174SJohannes Berg int __nla_validate(const struct nlattr *head, int len, int maxtype,
4358cb08174SJohannes Berg 		   const struct nla_policy *policy, unsigned int validate,
436fceb6435SJohannes Berg 		   struct netlink_ext_ack *extack);
4378cb08174SJohannes Berg int __nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
4388cb08174SJohannes Berg 		int len, const struct nla_policy *policy, unsigned int validate,
439a5f6cba2SDavid Ahern 		struct netlink_ext_ack *extack);
4404f69053bSJoe Perches int nla_policy_len(const struct nla_policy *, int);
4414f69053bSJoe Perches struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
4424f69053bSJoe Perches size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize);
4432cf0c8b3SPhil Sutter char *nla_strdup(const struct nlattr *nla, gfp_t flags);
4444f69053bSJoe Perches int nla_memcpy(void *dest, const struct nlattr *src, int count);
4454f69053bSJoe Perches int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
4464f69053bSJoe Perches int nla_strcmp(const struct nlattr *nla, const char *str);
4474f69053bSJoe Perches struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
448089bf1a6SNicolas Dichtel struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
449089bf1a6SNicolas Dichtel 				   int attrlen, int padattr);
4504f69053bSJoe Perches void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
4514f69053bSJoe Perches struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
452089bf1a6SNicolas Dichtel struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype,
453089bf1a6SNicolas Dichtel 				 int attrlen, int padattr);
4544f69053bSJoe Perches void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
4554f69053bSJoe Perches void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
456fe4944e5SThomas Graf 	       const void *data);
457089bf1a6SNicolas Dichtel void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
458089bf1a6SNicolas Dichtel 		     const void *data, int padattr);
4594f69053bSJoe Perches void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
4604f69053bSJoe Perches int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);
461089bf1a6SNicolas Dichtel int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
462089bf1a6SNicolas Dichtel 		  const void *data, int padattr);
4634f69053bSJoe Perches int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
4644f69053bSJoe Perches int nla_append(struct sk_buff *skb, int attrlen, const void *data);
465bfa83a9eSThomas Graf 
466bfa83a9eSThomas Graf /**************************************************************************
467bfa83a9eSThomas Graf  * Netlink Messages
468bfa83a9eSThomas Graf  **************************************************************************/
469bfa83a9eSThomas Graf 
470bfa83a9eSThomas Graf /**
471bfa83a9eSThomas Graf  * nlmsg_msg_size - length of netlink message not including padding
472bfa83a9eSThomas Graf  * @payload: length of message payload
473bfa83a9eSThomas Graf  */
474bfa83a9eSThomas Graf static inline int nlmsg_msg_size(int payload)
475bfa83a9eSThomas Graf {
476bfa83a9eSThomas Graf 	return NLMSG_HDRLEN + payload;
477bfa83a9eSThomas Graf }
478bfa83a9eSThomas Graf 
479bfa83a9eSThomas Graf /**
480bfa83a9eSThomas Graf  * nlmsg_total_size - length of netlink message including padding
481bfa83a9eSThomas Graf  * @payload: length of message payload
482bfa83a9eSThomas Graf  */
483bfa83a9eSThomas Graf static inline int nlmsg_total_size(int payload)
484bfa83a9eSThomas Graf {
485bfa83a9eSThomas Graf 	return NLMSG_ALIGN(nlmsg_msg_size(payload));
486bfa83a9eSThomas Graf }
487bfa83a9eSThomas Graf 
488bfa83a9eSThomas Graf /**
489bfa83a9eSThomas Graf  * nlmsg_padlen - length of padding at the message's tail
490bfa83a9eSThomas Graf  * @payload: length of message payload
491bfa83a9eSThomas Graf  */
492bfa83a9eSThomas Graf static inline int nlmsg_padlen(int payload)
493bfa83a9eSThomas Graf {
494bfa83a9eSThomas Graf 	return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
495bfa83a9eSThomas Graf }
496bfa83a9eSThomas Graf 
497bfa83a9eSThomas Graf /**
498bfa83a9eSThomas Graf  * nlmsg_data - head of message payload
49970f23fd6SJustin P. Mattock  * @nlh: netlink message header
500bfa83a9eSThomas Graf  */
501bfa83a9eSThomas Graf static inline void *nlmsg_data(const struct nlmsghdr *nlh)
502bfa83a9eSThomas Graf {
503bfa83a9eSThomas Graf 	return (unsigned char *) nlh + NLMSG_HDRLEN;
504bfa83a9eSThomas Graf }
505bfa83a9eSThomas Graf 
506bfa83a9eSThomas Graf /**
507bfa83a9eSThomas Graf  * nlmsg_len - length of message payload
508bfa83a9eSThomas Graf  * @nlh: netlink message header
509bfa83a9eSThomas Graf  */
510bfa83a9eSThomas Graf static inline int nlmsg_len(const struct nlmsghdr *nlh)
511bfa83a9eSThomas Graf {
512bfa83a9eSThomas Graf 	return nlh->nlmsg_len - NLMSG_HDRLEN;
513bfa83a9eSThomas Graf }
514bfa83a9eSThomas Graf 
515bfa83a9eSThomas Graf /**
516bfa83a9eSThomas Graf  * nlmsg_attrdata - head of attributes data
517bfa83a9eSThomas Graf  * @nlh: netlink message header
518bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
519bfa83a9eSThomas Graf  */
520bfa83a9eSThomas Graf static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
521bfa83a9eSThomas Graf 					    int hdrlen)
522bfa83a9eSThomas Graf {
523bfa83a9eSThomas Graf 	unsigned char *data = nlmsg_data(nlh);
524bfa83a9eSThomas Graf 	return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
525bfa83a9eSThomas Graf }
526bfa83a9eSThomas Graf 
527bfa83a9eSThomas Graf /**
528bfa83a9eSThomas Graf  * nlmsg_attrlen - length of attributes data
529bfa83a9eSThomas Graf  * @nlh: netlink message header
530bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
531bfa83a9eSThomas Graf  */
532bfa83a9eSThomas Graf static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
533bfa83a9eSThomas Graf {
534bfa83a9eSThomas Graf 	return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
535bfa83a9eSThomas Graf }
536bfa83a9eSThomas Graf 
537bfa83a9eSThomas Graf /**
538bfa83a9eSThomas Graf  * nlmsg_ok - check if the netlink message fits into the remaining bytes
539bfa83a9eSThomas Graf  * @nlh: netlink message header
540bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in message stream
541bfa83a9eSThomas Graf  */
542bfa83a9eSThomas Graf static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
543bfa83a9eSThomas Graf {
544619e803dSVegard Nossum 	return (remaining >= (int) sizeof(struct nlmsghdr) &&
545bfa83a9eSThomas Graf 		nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
546bfa83a9eSThomas Graf 		nlh->nlmsg_len <= remaining);
547bfa83a9eSThomas Graf }
548bfa83a9eSThomas Graf 
549bfa83a9eSThomas Graf /**
550bfa83a9eSThomas Graf  * nlmsg_next - next netlink message in message stream
551bfa83a9eSThomas Graf  * @nlh: netlink message header
552bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in message stream
553bfa83a9eSThomas Graf  *
554bfa83a9eSThomas Graf  * Returns the next netlink message in the message stream and
555bfa83a9eSThomas Graf  * decrements remaining by the size of the current message.
556bfa83a9eSThomas Graf  */
5573654654fSJan Engelhardt static inline struct nlmsghdr *
5583654654fSJan Engelhardt nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
559bfa83a9eSThomas Graf {
560bfa83a9eSThomas Graf 	int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
561bfa83a9eSThomas Graf 
562bfa83a9eSThomas Graf 	*remaining -= totlen;
563bfa83a9eSThomas Graf 
564bfa83a9eSThomas Graf 	return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
565bfa83a9eSThomas Graf }
566bfa83a9eSThomas Graf 
567bfa83a9eSThomas Graf /**
5683de64403SJohannes Berg  * nla_parse - Parse a stream of attributes into a tb buffer
5693de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
5703de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
5713de64403SJohannes Berg  * @head: head of attribute stream
5723de64403SJohannes Berg  * @len: length of attribute stream
5733de64403SJohannes Berg  * @policy: validation policy
5743de64403SJohannes Berg  * @extack: extended ACK pointer
5753de64403SJohannes Berg  *
5763de64403SJohannes Berg  * Parses a stream of attributes and stores a pointer to each attribute in
5773de64403SJohannes Berg  * the tb array accessible via the attribute type. Attributes with a type
5783de64403SJohannes Berg  * exceeding maxtype will be rejected, policy must be specified, attributes
5793de64403SJohannes Berg  * will be validated in the strictest way possible.
5803de64403SJohannes Berg  *
5813de64403SJohannes Berg  * Returns 0 on success or a negative error code.
5823de64403SJohannes Berg  */
5833de64403SJohannes Berg static inline int nla_parse(struct nlattr **tb, int maxtype,
5843de64403SJohannes Berg 			    const struct nlattr *head, int len,
5853de64403SJohannes Berg 			    const struct nla_policy *policy,
5863de64403SJohannes Berg 			    struct netlink_ext_ack *extack)
5873de64403SJohannes Berg {
5883de64403SJohannes Berg 	return __nla_parse(tb, maxtype, head, len, policy,
5893de64403SJohannes Berg 			   NL_VALIDATE_STRICT, extack);
5903de64403SJohannes Berg }
5913de64403SJohannes Berg 
5923de64403SJohannes Berg /**
5938cb08174SJohannes Berg  * nla_parse_deprecated - Parse a stream of attributes into a tb buffer
5948cb08174SJohannes Berg  * @tb: destination array with maxtype+1 elements
5958cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
5968cb08174SJohannes Berg  * @head: head of attribute stream
5978cb08174SJohannes Berg  * @len: length of attribute stream
5988cb08174SJohannes Berg  * @policy: validation policy
5998cb08174SJohannes Berg  * @extack: extended ACK pointer
6008cb08174SJohannes Berg  *
6018cb08174SJohannes Berg  * Parses a stream of attributes and stores a pointer to each attribute in
6028cb08174SJohannes Berg  * the tb array accessible via the attribute type. Attributes with a type
6038cb08174SJohannes Berg  * exceeding maxtype will be ignored and attributes from the policy are not
6048cb08174SJohannes Berg  * always strictly validated (only for new attributes).
6058cb08174SJohannes Berg  *
6068cb08174SJohannes Berg  * Returns 0 on success or a negative error code.
6078cb08174SJohannes Berg  */
6088cb08174SJohannes Berg static inline int nla_parse_deprecated(struct nlattr **tb, int maxtype,
6098cb08174SJohannes Berg 				       const struct nlattr *head, int len,
6108cb08174SJohannes Berg 				       const struct nla_policy *policy,
6118cb08174SJohannes Berg 				       struct netlink_ext_ack *extack)
6128cb08174SJohannes Berg {
6138cb08174SJohannes Berg 	return __nla_parse(tb, maxtype, head, len, policy,
6148cb08174SJohannes Berg 			   NL_VALIDATE_LIBERAL, extack);
6158cb08174SJohannes Berg }
6168cb08174SJohannes Berg 
6178cb08174SJohannes Berg /**
6188cb08174SJohannes Berg  * nla_parse_deprecated_strict - Parse a stream of attributes into a tb buffer
6198cb08174SJohannes Berg  * @tb: destination array with maxtype+1 elements
6208cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
6218cb08174SJohannes Berg  * @head: head of attribute stream
6228cb08174SJohannes Berg  * @len: length of attribute stream
6238cb08174SJohannes Berg  * @policy: validation policy
6248cb08174SJohannes Berg  * @extack: extended ACK pointer
6258cb08174SJohannes Berg  *
6268cb08174SJohannes Berg  * Parses a stream of attributes and stores a pointer to each attribute in
6278cb08174SJohannes Berg  * the tb array accessible via the attribute type. Attributes with a type
6288cb08174SJohannes Berg  * exceeding maxtype will be rejected as well as trailing data, but the
6298cb08174SJohannes Berg  * policy is not completely strictly validated (only for new attributes).
6308cb08174SJohannes Berg  *
6318cb08174SJohannes Berg  * Returns 0 on success or a negative error code.
6328cb08174SJohannes Berg  */
6338cb08174SJohannes Berg static inline int nla_parse_deprecated_strict(struct nlattr **tb, int maxtype,
6348cb08174SJohannes Berg 					      const struct nlattr *head,
6358cb08174SJohannes Berg 					      int len,
6368cb08174SJohannes Berg 					      const struct nla_policy *policy,
6378cb08174SJohannes Berg 					      struct netlink_ext_ack *extack)
6388cb08174SJohannes Berg {
6398cb08174SJohannes Berg 	return __nla_parse(tb, maxtype, head, len, policy,
6408cb08174SJohannes Berg 			   NL_VALIDATE_DEPRECATED_STRICT, extack);
6418cb08174SJohannes Berg }
6428cb08174SJohannes Berg 
6438cb08174SJohannes Berg /**
6448cb08174SJohannes Berg  * __nlmsg_parse - parse attributes of a netlink message
645bfa83a9eSThomas Graf  * @nlh: netlink message header
646bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
647bfa83a9eSThomas Graf  * @tb: destination array with maxtype+1 elements
648bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
649bfa83a9eSThomas Graf  * @policy: validation policy
6508cb08174SJohannes Berg  * @validate: validation strictness
651fceb6435SJohannes Berg  * @extack: extended ACK report struct
652bfa83a9eSThomas Graf  *
653bfa83a9eSThomas Graf  * See nla_parse()
654bfa83a9eSThomas Graf  */
6558cb08174SJohannes Berg static inline int __nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
656bfa83a9eSThomas Graf 				struct nlattr *tb[], int maxtype,
657fceb6435SJohannes Berg 				const struct nla_policy *policy,
6588cb08174SJohannes Berg 				unsigned int validate,
659fceb6435SJohannes Berg 				struct netlink_ext_ack *extack)
660bfa83a9eSThomas Graf {
6613d0d4337SDavid Ahern 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) {
6623d0d4337SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid header length");
663bfa83a9eSThomas Graf 		return -EINVAL;
6643d0d4337SDavid Ahern 	}
665bfa83a9eSThomas Graf 
6668cb08174SJohannes Berg 	return __nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
6678cb08174SJohannes Berg 			   nlmsg_attrlen(nlh, hdrlen), policy, validate,
6688cb08174SJohannes Berg 			   extack);
669bfa83a9eSThomas Graf }
670bfa83a9eSThomas Graf 
6718cb08174SJohannes Berg /**
6723de64403SJohannes Berg  * nlmsg_parse - parse attributes of a netlink message
6733de64403SJohannes Berg  * @nlh: netlink message header
6743de64403SJohannes Berg  * @hdrlen: length of family specific header
6753de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
6763de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
6773de64403SJohannes Berg  * @validate: validation strictness
6783de64403SJohannes Berg  * @extack: extended ACK report struct
6793de64403SJohannes Berg  *
6803de64403SJohannes Berg  * See nla_parse()
6813de64403SJohannes Berg  */
6823de64403SJohannes Berg static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
6833de64403SJohannes Berg 			      struct nlattr *tb[], int maxtype,
6843de64403SJohannes Berg 			      const struct nla_policy *policy,
6853de64403SJohannes Berg 			      struct netlink_ext_ack *extack)
6863de64403SJohannes Berg {
687d00ee64eSDavid Ahern 	return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
6883de64403SJohannes Berg 			     NL_VALIDATE_STRICT, extack);
6893de64403SJohannes Berg }
6903de64403SJohannes Berg 
6913de64403SJohannes Berg /**
6928cb08174SJohannes Berg  * nlmsg_parse_deprecated - parse attributes of a netlink message
6938cb08174SJohannes Berg  * @nlh: netlink message header
6948cb08174SJohannes Berg  * @hdrlen: length of family specific header
6958cb08174SJohannes Berg  * @tb: destination array with maxtype+1 elements
6968cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
6978cb08174SJohannes Berg  * @extack: extended ACK report struct
6988cb08174SJohannes Berg  *
6998cb08174SJohannes Berg  * See nla_parse_deprecated()
7008cb08174SJohannes Berg  */
7018cb08174SJohannes Berg static inline int nlmsg_parse_deprecated(const struct nlmsghdr *nlh, int hdrlen,
702a5f6cba2SDavid Ahern 					 struct nlattr *tb[], int maxtype,
703a5f6cba2SDavid Ahern 					 const struct nla_policy *policy,
704a5f6cba2SDavid Ahern 					 struct netlink_ext_ack *extack)
705a5f6cba2SDavid Ahern {
7068cb08174SJohannes Berg 	return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
7078cb08174SJohannes Berg 			     NL_VALIDATE_LIBERAL, extack);
708a5f6cba2SDavid Ahern }
709a5f6cba2SDavid Ahern 
7108cb08174SJohannes Berg /**
7118cb08174SJohannes Berg  * nlmsg_parse_deprecated_strict - parse attributes of a netlink message
7128cb08174SJohannes Berg  * @nlh: netlink message header
7138cb08174SJohannes Berg  * @hdrlen: length of family specific header
7148cb08174SJohannes Berg  * @tb: destination array with maxtype+1 elements
7158cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
7168cb08174SJohannes Berg  * @extack: extended ACK report struct
7178cb08174SJohannes Berg  *
7188cb08174SJohannes Berg  * See nla_parse_deprecated_strict()
7198cb08174SJohannes Berg  */
7208cb08174SJohannes Berg static inline int
7218cb08174SJohannes Berg nlmsg_parse_deprecated_strict(const struct nlmsghdr *nlh, int hdrlen,
7228cb08174SJohannes Berg 			      struct nlattr *tb[], int maxtype,
7238cb08174SJohannes Berg 			      const struct nla_policy *policy,
7248cb08174SJohannes Berg 			      struct netlink_ext_ack *extack)
7258cb08174SJohannes Berg {
7268cb08174SJohannes Berg 	return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
7278cb08174SJohannes Berg 			     NL_VALIDATE_DEPRECATED_STRICT, extack);
728a5f6cba2SDavid Ahern }
729a5f6cba2SDavid Ahern 
730bfa83a9eSThomas Graf /**
731bfa83a9eSThomas Graf  * nlmsg_find_attr - find a specific attribute in a netlink message
732bfa83a9eSThomas Graf  * @nlh: netlink message header
733bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
734bfa83a9eSThomas Graf  * @attrtype: type of attribute to look for
735bfa83a9eSThomas Graf  *
736bfa83a9eSThomas Graf  * Returns the first attribute which matches the specified type.
737bfa83a9eSThomas Graf  */
7386b8c92baSNelson Elhage static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
739bfa83a9eSThomas Graf 					     int hdrlen, int attrtype)
740bfa83a9eSThomas Graf {
741bfa83a9eSThomas Graf 	return nla_find(nlmsg_attrdata(nlh, hdrlen),
742bfa83a9eSThomas Graf 			nlmsg_attrlen(nlh, hdrlen), attrtype);
743bfa83a9eSThomas Graf }
744bfa83a9eSThomas Graf 
745bfa83a9eSThomas Graf /**
7468cb08174SJohannes Berg  * nla_validate_deprecated - Validate a stream of attributes
7478cb08174SJohannes Berg  * @head: head of attribute stream
7488cb08174SJohannes Berg  * @len: length of attribute stream
7498cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
7508cb08174SJohannes Berg  * @policy: validation policy
7518cb08174SJohannes Berg  * @validate: validation strictness
7528cb08174SJohannes Berg  * @extack: extended ACK report struct
7538cb08174SJohannes Berg  *
7548cb08174SJohannes Berg  * Validates all attributes in the specified attribute stream against the
7558cb08174SJohannes Berg  * specified policy. Validation is done in liberal mode.
7568cb08174SJohannes Berg  * See documenation of struct nla_policy for more details.
7578cb08174SJohannes Berg  *
7588cb08174SJohannes Berg  * Returns 0 on success or a negative error code.
7598cb08174SJohannes Berg  */
7608cb08174SJohannes Berg static inline int nla_validate_deprecated(const struct nlattr *head, int len,
7618cb08174SJohannes Berg 					  int maxtype,
7628cb08174SJohannes Berg 					  const struct nla_policy *policy,
7638cb08174SJohannes Berg 					  struct netlink_ext_ack *extack)
7648cb08174SJohannes Berg {
7658cb08174SJohannes Berg 	return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_LIBERAL,
7668cb08174SJohannes Berg 			      extack);
7678cb08174SJohannes Berg }
7688cb08174SJohannes Berg 
7693de64403SJohannes Berg /**
7703de64403SJohannes Berg  * nla_validate - Validate a stream of attributes
7713de64403SJohannes Berg  * @head: head of attribute stream
7723de64403SJohannes Berg  * @len: length of attribute stream
7733de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
7743de64403SJohannes Berg  * @policy: validation policy
7753de64403SJohannes Berg  * @validate: validation strictness
7763de64403SJohannes Berg  * @extack: extended ACK report struct
7773de64403SJohannes Berg  *
7783de64403SJohannes Berg  * Validates all attributes in the specified attribute stream against the
7793de64403SJohannes Berg  * specified policy. Validation is done in strict mode.
7803de64403SJohannes Berg  * See documenation of struct nla_policy for more details.
7813de64403SJohannes Berg  *
7823de64403SJohannes Berg  * Returns 0 on success or a negative error code.
7833de64403SJohannes Berg  */
7843de64403SJohannes Berg static inline int nla_validate(const struct nlattr *head, int len, int maxtype,
7853de64403SJohannes Berg 			       const struct nla_policy *policy,
7863de64403SJohannes Berg 			       struct netlink_ext_ack *extack)
7873de64403SJohannes Berg {
7883de64403SJohannes Berg 	return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_STRICT,
7893de64403SJohannes Berg 			      extack);
7903de64403SJohannes Berg }
7918cb08174SJohannes Berg 
7928cb08174SJohannes Berg /**
7938cb08174SJohannes Berg  * nlmsg_validate_deprecated - validate a netlink message including attributes
794bfa83a9eSThomas Graf  * @nlh: netlinket message header
795bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
796bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
797bfa83a9eSThomas Graf  * @policy: validation policy
798fceb6435SJohannes Berg  * @extack: extended ACK report struct
799bfa83a9eSThomas Graf  */
8008cb08174SJohannes Berg static inline int nlmsg_validate_deprecated(const struct nlmsghdr *nlh,
8013654654fSJan Engelhardt 					    int hdrlen, int maxtype,
802fceb6435SJohannes Berg 					    const struct nla_policy *policy,
803fceb6435SJohannes Berg 					    struct netlink_ext_ack *extack)
804bfa83a9eSThomas Graf {
805bfa83a9eSThomas Graf 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
806bfa83a9eSThomas Graf 		return -EINVAL;
807bfa83a9eSThomas Graf 
8088cb08174SJohannes Berg 	return __nla_validate(nlmsg_attrdata(nlh, hdrlen),
8098cb08174SJohannes Berg 			      nlmsg_attrlen(nlh, hdrlen), maxtype,
8108cb08174SJohannes Berg 			      policy, NL_VALIDATE_LIBERAL, extack);
811bfa83a9eSThomas Graf }
812bfa83a9eSThomas Graf 
8138cb08174SJohannes Berg 
8148cb08174SJohannes Berg 
815bfa83a9eSThomas Graf /**
81697676b6bSThomas Graf  * nlmsg_report - need to report back to application?
81797676b6bSThomas Graf  * @nlh: netlink message header
81897676b6bSThomas Graf  *
81997676b6bSThomas Graf  * Returns 1 if a report back to the application is requested.
82097676b6bSThomas Graf  */
8213a6c2b41SPatrick McHardy static inline int nlmsg_report(const struct nlmsghdr *nlh)
82297676b6bSThomas Graf {
82397676b6bSThomas Graf 	return !!(nlh->nlmsg_flags & NLM_F_ECHO);
82497676b6bSThomas Graf }
82597676b6bSThomas Graf 
82697676b6bSThomas Graf /**
827bfa83a9eSThomas Graf  * nlmsg_for_each_attr - iterate over a stream of attributes
828bfa83a9eSThomas Graf  * @pos: loop counter, set to current attribute
829bfa83a9eSThomas Graf  * @nlh: netlink message header
830bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
831bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
832bfa83a9eSThomas Graf  */
833bfa83a9eSThomas Graf #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
834bfa83a9eSThomas Graf 	nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
835bfa83a9eSThomas Graf 			  nlmsg_attrlen(nlh, hdrlen), rem)
836bfa83a9eSThomas Graf 
837bfa83a9eSThomas Graf /**
838bfa83a9eSThomas Graf  * nlmsg_put - Add a new netlink message to an skb
839bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
8402c6ba4b1SNicolas Dichtel  * @portid: netlink PORTID of requesting application
841bfa83a9eSThomas Graf  * @seq: sequence number of message
842bfa83a9eSThomas Graf  * @type: message type
843bfa83a9eSThomas Graf  * @payload: length of message payload
844bfa83a9eSThomas Graf  * @flags: message flags
845bfa83a9eSThomas Graf  *
846bfa83a9eSThomas Graf  * Returns NULL if the tailroom of the skb is insufficient to store
847bfa83a9eSThomas Graf  * the message header and payload.
848bfa83a9eSThomas Graf  */
84915e47304SEric W. Biederman static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
850bfa83a9eSThomas Graf 					 int type, int payload, int flags)
851bfa83a9eSThomas Graf {
852bfa83a9eSThomas Graf 	if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
853bfa83a9eSThomas Graf 		return NULL;
854bfa83a9eSThomas Graf 
85515e47304SEric W. Biederman 	return __nlmsg_put(skb, portid, seq, type, payload, flags);
856bfa83a9eSThomas Graf }
857bfa83a9eSThomas Graf 
858bfa83a9eSThomas Graf /**
859bfa83a9eSThomas Graf  * nlmsg_put_answer - Add a new callback based netlink message to an skb
860bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
861bfa83a9eSThomas Graf  * @cb: netlink callback
862bfa83a9eSThomas Graf  * @type: message type
863bfa83a9eSThomas Graf  * @payload: length of message payload
864bfa83a9eSThomas Graf  * @flags: message flags
865bfa83a9eSThomas Graf  *
866bfa83a9eSThomas Graf  * Returns NULL if the tailroom of the skb is insufficient to store
867bfa83a9eSThomas Graf  * the message header and payload.
868bfa83a9eSThomas Graf  */
869bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
870bfa83a9eSThomas Graf 						struct netlink_callback *cb,
871bfa83a9eSThomas Graf 						int type, int payload,
872bfa83a9eSThomas Graf 						int flags)
873bfa83a9eSThomas Graf {
87415e47304SEric W. Biederman 	return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
875bfa83a9eSThomas Graf 			 type, payload, flags);
876bfa83a9eSThomas Graf }
877bfa83a9eSThomas Graf 
878bfa83a9eSThomas Graf /**
879bfa83a9eSThomas Graf  * nlmsg_new - Allocate a new netlink message
880339bf98fSThomas Graf  * @payload: size of the message payload
881fe4944e5SThomas Graf  * @flags: the type of memory to allocate.
882bfa83a9eSThomas Graf  *
883339bf98fSThomas Graf  * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
884339bf98fSThomas Graf  * and a good default is needed.
885bfa83a9eSThomas Graf  */
886339bf98fSThomas Graf static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
887bfa83a9eSThomas Graf {
888339bf98fSThomas Graf 	return alloc_skb(nlmsg_total_size(payload), flags);
889bfa83a9eSThomas Graf }
890bfa83a9eSThomas Graf 
891bfa83a9eSThomas Graf /**
892bfa83a9eSThomas Graf  * nlmsg_end - Finalize a netlink message
893bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
894bfa83a9eSThomas Graf  * @nlh: netlink message header
895bfa83a9eSThomas Graf  *
896bfa83a9eSThomas Graf  * Corrects the netlink message header to include the appeneded
897bfa83a9eSThomas Graf  * attributes. Only necessary if attributes have been added to
898bfa83a9eSThomas Graf  * the message.
899bfa83a9eSThomas Graf  */
900053c095aSJohannes Berg static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
901bfa83a9eSThomas Graf {
90227a884dcSArnaldo Carvalho de Melo 	nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
903bfa83a9eSThomas Graf }
904bfa83a9eSThomas Graf 
905bfa83a9eSThomas Graf /**
906fe4944e5SThomas Graf  * nlmsg_get_pos - return current position in netlink message
907fe4944e5SThomas Graf  * @skb: socket buffer the message is stored in
908fe4944e5SThomas Graf  *
909fe4944e5SThomas Graf  * Returns a pointer to the current tail of the message.
910fe4944e5SThomas Graf  */
911fe4944e5SThomas Graf static inline void *nlmsg_get_pos(struct sk_buff *skb)
912fe4944e5SThomas Graf {
91327a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
914fe4944e5SThomas Graf }
915fe4944e5SThomas Graf 
916fe4944e5SThomas Graf /**
917fe4944e5SThomas Graf  * nlmsg_trim - Trim message to a mark
918fe4944e5SThomas Graf  * @skb: socket buffer the message is stored in
919fe4944e5SThomas Graf  * @mark: mark to trim to
920fe4944e5SThomas Graf  *
921bc3ed28cSThomas Graf  * Trims the message to the provided mark.
922fe4944e5SThomas Graf  */
923bc3ed28cSThomas Graf static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
924fe4944e5SThomas Graf {
925149118d8SThomas Graf 	if (mark) {
926149118d8SThomas Graf 		WARN_ON((unsigned char *) mark < skb->data);
927fe4944e5SThomas Graf 		skb_trim(skb, (unsigned char *) mark - skb->data);
928fe4944e5SThomas Graf 	}
929149118d8SThomas Graf }
930fe4944e5SThomas Graf 
931fe4944e5SThomas Graf /**
932bfa83a9eSThomas Graf  * nlmsg_cancel - Cancel construction of a netlink message
933bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
934bfa83a9eSThomas Graf  * @nlh: netlink message header
935bfa83a9eSThomas Graf  *
936bfa83a9eSThomas Graf  * Removes the complete netlink message including all
937bc3ed28cSThomas Graf  * attributes from the socket buffer again.
938bfa83a9eSThomas Graf  */
939bc3ed28cSThomas Graf static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
940bfa83a9eSThomas Graf {
941bc3ed28cSThomas Graf 	nlmsg_trim(skb, nlh);
942bfa83a9eSThomas Graf }
943bfa83a9eSThomas Graf 
944bfa83a9eSThomas Graf /**
945bfa83a9eSThomas Graf  * nlmsg_free - free a netlink message
946bfa83a9eSThomas Graf  * @skb: socket buffer of netlink message
947bfa83a9eSThomas Graf  */
948bfa83a9eSThomas Graf static inline void nlmsg_free(struct sk_buff *skb)
949bfa83a9eSThomas Graf {
950bfa83a9eSThomas Graf 	kfree_skb(skb);
951bfa83a9eSThomas Graf }
952bfa83a9eSThomas Graf 
953bfa83a9eSThomas Graf /**
954bfa83a9eSThomas Graf  * nlmsg_multicast - multicast a netlink message
955bfa83a9eSThomas Graf  * @sk: netlink socket to spread messages to
956bfa83a9eSThomas Graf  * @skb: netlink message as socket buffer
95715e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
958bfa83a9eSThomas Graf  * @group: multicast group id
959d387f6adSThomas Graf  * @flags: allocation flags
960bfa83a9eSThomas Graf  */
961bfa83a9eSThomas Graf static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
96215e47304SEric W. Biederman 				  u32 portid, unsigned int group, gfp_t flags)
963bfa83a9eSThomas Graf {
964bfa83a9eSThomas Graf 	int err;
965bfa83a9eSThomas Graf 
966bfa83a9eSThomas Graf 	NETLINK_CB(skb).dst_group = group;
967bfa83a9eSThomas Graf 
96815e47304SEric W. Biederman 	err = netlink_broadcast(sk, skb, portid, group, flags);
969bfa83a9eSThomas Graf 	if (err > 0)
970bfa83a9eSThomas Graf 		err = 0;
971bfa83a9eSThomas Graf 
972bfa83a9eSThomas Graf 	return err;
973bfa83a9eSThomas Graf }
974bfa83a9eSThomas Graf 
975bfa83a9eSThomas Graf /**
976bfa83a9eSThomas Graf  * nlmsg_unicast - unicast a netlink message
977bfa83a9eSThomas Graf  * @sk: netlink socket to spread message to
978bfa83a9eSThomas Graf  * @skb: netlink message as socket buffer
97915e47304SEric W. Biederman  * @portid: netlink portid of the destination socket
980bfa83a9eSThomas Graf  */
98115e47304SEric W. Biederman static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
982bfa83a9eSThomas Graf {
983bfa83a9eSThomas Graf 	int err;
984bfa83a9eSThomas Graf 
98515e47304SEric W. Biederman 	err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
986bfa83a9eSThomas Graf 	if (err > 0)
987bfa83a9eSThomas Graf 		err = 0;
988bfa83a9eSThomas Graf 
989bfa83a9eSThomas Graf 	return err;
990bfa83a9eSThomas Graf }
991bfa83a9eSThomas Graf 
992bfa83a9eSThomas Graf /**
993bfa83a9eSThomas Graf  * nlmsg_for_each_msg - iterate over a stream of messages
994bfa83a9eSThomas Graf  * @pos: loop counter, set to current message
995bfa83a9eSThomas Graf  * @head: head of message stream
996bfa83a9eSThomas Graf  * @len: length of message stream
997bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
998bfa83a9eSThomas Graf  */
999bfa83a9eSThomas Graf #define nlmsg_for_each_msg(pos, head, len, rem) \
1000bfa83a9eSThomas Graf 	for (pos = head, rem = len; \
1001bfa83a9eSThomas Graf 	     nlmsg_ok(pos, rem); \
1002bfa83a9eSThomas Graf 	     pos = nlmsg_next(pos, &(rem)))
1003bfa83a9eSThomas Graf 
1004670dc283SJohannes Berg /**
1005670dc283SJohannes Berg  * nl_dump_check_consistent - check if sequence is consistent and advertise if not
1006670dc283SJohannes Berg  * @cb: netlink callback structure that stores the sequence number
1007670dc283SJohannes Berg  * @nlh: netlink message header to write the flag to
1008670dc283SJohannes Berg  *
1009670dc283SJohannes Berg  * This function checks if the sequence (generation) number changed during dump
1010670dc283SJohannes Berg  * and if it did, advertises it in the netlink message header.
1011670dc283SJohannes Berg  *
1012670dc283SJohannes Berg  * The correct way to use it is to set cb->seq to the generation counter when
1013670dc283SJohannes Berg  * all locks for dumping have been acquired, and then call this function for
1014670dc283SJohannes Berg  * each message that is generated.
1015670dc283SJohannes Berg  *
1016670dc283SJohannes Berg  * Note that due to initialisation concerns, 0 is an invalid sequence number
1017670dc283SJohannes Berg  * and must not be used by code that uses this functionality.
1018670dc283SJohannes Berg  */
1019670dc283SJohannes Berg static inline void
1020670dc283SJohannes Berg nl_dump_check_consistent(struct netlink_callback *cb,
1021670dc283SJohannes Berg 			 struct nlmsghdr *nlh)
1022670dc283SJohannes Berg {
1023670dc283SJohannes Berg 	if (cb->prev_seq && cb->seq != cb->prev_seq)
1024670dc283SJohannes Berg 		nlh->nlmsg_flags |= NLM_F_DUMP_INTR;
1025670dc283SJohannes Berg 	cb->prev_seq = cb->seq;
1026670dc283SJohannes Berg }
1027670dc283SJohannes Berg 
1028bfa83a9eSThomas Graf /**************************************************************************
1029bfa83a9eSThomas Graf  * Netlink Attributes
1030bfa83a9eSThomas Graf  **************************************************************************/
1031bfa83a9eSThomas Graf 
1032bfa83a9eSThomas Graf /**
1033bfa83a9eSThomas Graf  * nla_attr_size - length of attribute not including padding
1034bfa83a9eSThomas Graf  * @payload: length of payload
1035bfa83a9eSThomas Graf  */
1036bfa83a9eSThomas Graf static inline int nla_attr_size(int payload)
1037bfa83a9eSThomas Graf {
1038bfa83a9eSThomas Graf 	return NLA_HDRLEN + payload;
1039bfa83a9eSThomas Graf }
1040bfa83a9eSThomas Graf 
1041bfa83a9eSThomas Graf /**
1042bfa83a9eSThomas Graf  * nla_total_size - total length of attribute including padding
1043bfa83a9eSThomas Graf  * @payload: length of payload
1044bfa83a9eSThomas Graf  */
1045bfa83a9eSThomas Graf static inline int nla_total_size(int payload)
1046bfa83a9eSThomas Graf {
1047bfa83a9eSThomas Graf 	return NLA_ALIGN(nla_attr_size(payload));
1048bfa83a9eSThomas Graf }
1049bfa83a9eSThomas Graf 
1050bfa83a9eSThomas Graf /**
1051bfa83a9eSThomas Graf  * nla_padlen - length of padding at the tail of attribute
1052bfa83a9eSThomas Graf  * @payload: length of payload
1053bfa83a9eSThomas Graf  */
1054bfa83a9eSThomas Graf static inline int nla_padlen(int payload)
1055bfa83a9eSThomas Graf {
1056bfa83a9eSThomas Graf 	return nla_total_size(payload) - nla_attr_size(payload);
1057bfa83a9eSThomas Graf }
1058bfa83a9eSThomas Graf 
1059bfa83a9eSThomas Graf /**
10608f4c1f9bSThomas Graf  * nla_type - attribute type
10618f4c1f9bSThomas Graf  * @nla: netlink attribute
10628f4c1f9bSThomas Graf  */
10638f4c1f9bSThomas Graf static inline int nla_type(const struct nlattr *nla)
10648f4c1f9bSThomas Graf {
10658f4c1f9bSThomas Graf 	return nla->nla_type & NLA_TYPE_MASK;
10668f4c1f9bSThomas Graf }
10678f4c1f9bSThomas Graf 
10688f4c1f9bSThomas Graf /**
1069bfa83a9eSThomas Graf  * nla_data - head of payload
1070bfa83a9eSThomas Graf  * @nla: netlink attribute
1071bfa83a9eSThomas Graf  */
1072bfa83a9eSThomas Graf static inline void *nla_data(const struct nlattr *nla)
1073bfa83a9eSThomas Graf {
1074bfa83a9eSThomas Graf 	return (char *) nla + NLA_HDRLEN;
1075bfa83a9eSThomas Graf }
1076bfa83a9eSThomas Graf 
1077bfa83a9eSThomas Graf /**
1078bfa83a9eSThomas Graf  * nla_len - length of payload
1079bfa83a9eSThomas Graf  * @nla: netlink attribute
1080bfa83a9eSThomas Graf  */
1081bfa83a9eSThomas Graf static inline int nla_len(const struct nlattr *nla)
1082bfa83a9eSThomas Graf {
1083bfa83a9eSThomas Graf 	return nla->nla_len - NLA_HDRLEN;
1084bfa83a9eSThomas Graf }
1085bfa83a9eSThomas Graf 
1086bfa83a9eSThomas Graf /**
1087bfa83a9eSThomas Graf  * nla_ok - check if the netlink attribute fits into the remaining bytes
1088bfa83a9eSThomas Graf  * @nla: netlink attribute
1089bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in attribute stream
1090bfa83a9eSThomas Graf  */
1091bfa83a9eSThomas Graf static inline int nla_ok(const struct nlattr *nla, int remaining)
1092bfa83a9eSThomas Graf {
10933e1ed981SAlexey Dobriyan 	return remaining >= (int) sizeof(*nla) &&
10943e1ed981SAlexey Dobriyan 	       nla->nla_len >= sizeof(*nla) &&
1095bfa83a9eSThomas Graf 	       nla->nla_len <= remaining;
1096bfa83a9eSThomas Graf }
1097bfa83a9eSThomas Graf 
1098bfa83a9eSThomas Graf /**
1099d1ec3b77SPierre Ynard  * nla_next - next netlink attribute in attribute stream
1100bfa83a9eSThomas Graf  * @nla: netlink attribute
1101bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in attribute stream
1102bfa83a9eSThomas Graf  *
1103bfa83a9eSThomas Graf  * Returns the next netlink attribute in the attribute stream and
1104bfa83a9eSThomas Graf  * decrements remaining by the size of the current attribute.
1105bfa83a9eSThomas Graf  */
1106bfa83a9eSThomas Graf static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
1107bfa83a9eSThomas Graf {
11083b2c75d3SAlexey Dobriyan 	unsigned int totlen = NLA_ALIGN(nla->nla_len);
1109bfa83a9eSThomas Graf 
1110bfa83a9eSThomas Graf 	*remaining -= totlen;
1111bfa83a9eSThomas Graf 	return (struct nlattr *) ((char *) nla + totlen);
1112bfa83a9eSThomas Graf }
1113bfa83a9eSThomas Graf 
1114bfa83a9eSThomas Graf /**
1115fe4944e5SThomas Graf  * nla_find_nested - find attribute in a set of nested attributes
1116fe4944e5SThomas Graf  * @nla: attribute containing the nested attributes
1117fe4944e5SThomas Graf  * @attrtype: type of attribute to look for
1118fe4944e5SThomas Graf  *
1119fe4944e5SThomas Graf  * Returns the first attribute which matches the specified type.
1120fe4944e5SThomas Graf  */
11213654654fSJan Engelhardt static inline struct nlattr *
11223654654fSJan Engelhardt nla_find_nested(const struct nlattr *nla, int attrtype)
1123fe4944e5SThomas Graf {
1124fe4944e5SThomas Graf 	return nla_find(nla_data(nla), nla_len(nla), attrtype);
1125fe4944e5SThomas Graf }
1126fe4944e5SThomas Graf 
1127fe4944e5SThomas Graf /**
11283de64403SJohannes Berg  * nla_parse_nested - parse nested attributes
11293de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
11303de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
11313de64403SJohannes Berg  * @nla: attribute containing the nested attributes
11323de64403SJohannes Berg  * @policy: validation policy
11333de64403SJohannes Berg  * @extack: extended ACK report struct
11343de64403SJohannes Berg  *
11353de64403SJohannes Berg  * See nla_parse()
11363de64403SJohannes Berg  */
11373de64403SJohannes Berg static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
11383de64403SJohannes Berg 				   const struct nlattr *nla,
11393de64403SJohannes Berg 				   const struct nla_policy *policy,
11403de64403SJohannes Berg 				   struct netlink_ext_ack *extack)
11413de64403SJohannes Berg {
1142b424e432SMichal Kubecek 	if (!(nla->nla_type & NLA_F_NESTED)) {
1143b424e432SMichal Kubecek 		NL_SET_ERR_MSG_ATTR(extack, nla, "NLA_F_NESTED is missing");
1144b424e432SMichal Kubecek 		return -EINVAL;
1145b424e432SMichal Kubecek 	}
1146b424e432SMichal Kubecek 
11473de64403SJohannes Berg 	return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
11483de64403SJohannes Berg 			   NL_VALIDATE_STRICT, extack);
11493de64403SJohannes Berg }
11503de64403SJohannes Berg 
11513de64403SJohannes Berg /**
11528cb08174SJohannes Berg  * nla_parse_nested_deprecated - parse nested attributes
1153bfa83a9eSThomas Graf  * @tb: destination array with maxtype+1 elements
1154bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
1155bfa83a9eSThomas Graf  * @nla: attribute containing the nested attributes
1156bfa83a9eSThomas Graf  * @policy: validation policy
1157fceb6435SJohannes Berg  * @extack: extended ACK report struct
1158bfa83a9eSThomas Graf  *
11598cb08174SJohannes Berg  * See nla_parse_deprecated()
1160bfa83a9eSThomas Graf  */
11618cb08174SJohannes Berg static inline int nla_parse_nested_deprecated(struct nlattr *tb[], int maxtype,
1162b057efd4SPatrick McHardy 					      const struct nlattr *nla,
1163fceb6435SJohannes Berg 					      const struct nla_policy *policy,
1164fceb6435SJohannes Berg 					      struct netlink_ext_ack *extack)
1165bfa83a9eSThomas Graf {
11668cb08174SJohannes Berg 	return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
11678cb08174SJohannes Berg 			   NL_VALIDATE_LIBERAL, extack);
1168bfa83a9eSThomas Graf }
11691092cb21SPatrick McHardy 
11701092cb21SPatrick McHardy /**
1171d1ec3b77SPierre Ynard  * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
1172bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1173bfa83a9eSThomas Graf  * @attrtype: attribute type
1174bfa83a9eSThomas Graf  * @value: numeric value
1175bfa83a9eSThomas Graf  */
1176bfa83a9eSThomas Graf static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
1177bfa83a9eSThomas Graf {
1178b4391db4SArnd Bergmann 	/* temporary variables to work around GCC PR81715 with asan-stack=1 */
1179b4391db4SArnd Bergmann 	u8 tmp = value;
1180b4391db4SArnd Bergmann 
1181b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(u8), &tmp);
1182bfa83a9eSThomas Graf }
1183bfa83a9eSThomas Graf 
1184bfa83a9eSThomas Graf /**
1185bfa83a9eSThomas Graf  * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
1186bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1187bfa83a9eSThomas Graf  * @attrtype: attribute type
1188bfa83a9eSThomas Graf  * @value: numeric value
1189bfa83a9eSThomas Graf  */
1190bfa83a9eSThomas Graf static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
1191bfa83a9eSThomas Graf {
1192b4391db4SArnd Bergmann 	u16 tmp = value;
1193b4391db4SArnd Bergmann 
1194b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(u16), &tmp);
1195bfa83a9eSThomas Graf }
1196bfa83a9eSThomas Graf 
1197bfa83a9eSThomas Graf /**
1198569a8fc3SDavid S. Miller  * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer
1199569a8fc3SDavid S. Miller  * @skb: socket buffer to add attribute to
1200569a8fc3SDavid S. Miller  * @attrtype: attribute type
1201569a8fc3SDavid S. Miller  * @value: numeric value
1202569a8fc3SDavid S. Miller  */
1203569a8fc3SDavid S. Miller static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
1204569a8fc3SDavid S. Miller {
1205b4391db4SArnd Bergmann 	__be16 tmp = value;
1206b4391db4SArnd Bergmann 
1207b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__be16), &tmp);
1208569a8fc3SDavid S. Miller }
1209569a8fc3SDavid S. Miller 
1210569a8fc3SDavid S. Miller /**
12116c1dd3b6SDavid S. Miller  * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer
12126c1dd3b6SDavid S. Miller  * @skb: socket buffer to add attribute to
12136c1dd3b6SDavid S. Miller  * @attrtype: attribute type
12146c1dd3b6SDavid S. Miller  * @value: numeric value
12156c1dd3b6SDavid S. Miller  */
12166c1dd3b6SDavid S. Miller static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
12176c1dd3b6SDavid S. Miller {
1218b4391db4SArnd Bergmann 	__be16 tmp = value;
1219b4391db4SArnd Bergmann 
1220b4391db4SArnd Bergmann 	return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
12216c1dd3b6SDavid S. Miller }
12226c1dd3b6SDavid S. Miller 
12236c1dd3b6SDavid S. Miller /**
122424c410dcSDavid S. Miller  * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer
122524c410dcSDavid S. Miller  * @skb: socket buffer to add attribute to
122624c410dcSDavid S. Miller  * @attrtype: attribute type
122724c410dcSDavid S. Miller  * @value: numeric value
122824c410dcSDavid S. Miller  */
122924c410dcSDavid S. Miller static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
123024c410dcSDavid S. Miller {
1231b4391db4SArnd Bergmann 	__le16 tmp = value;
1232b4391db4SArnd Bergmann 
1233b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__le16), &tmp);
123424c410dcSDavid S. Miller }
123524c410dcSDavid S. Miller 
123624c410dcSDavid S. Miller /**
1237bfa83a9eSThomas Graf  * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
1238bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1239bfa83a9eSThomas Graf  * @attrtype: attribute type
1240bfa83a9eSThomas Graf  * @value: numeric value
1241bfa83a9eSThomas Graf  */
1242bfa83a9eSThomas Graf static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
1243bfa83a9eSThomas Graf {
1244b4391db4SArnd Bergmann 	u32 tmp = value;
1245b4391db4SArnd Bergmann 
1246b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(u32), &tmp);
1247bfa83a9eSThomas Graf }
1248bfa83a9eSThomas Graf 
1249bfa83a9eSThomas Graf /**
1250569a8fc3SDavid S. Miller  * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer
1251569a8fc3SDavid S. Miller  * @skb: socket buffer to add attribute to
1252569a8fc3SDavid S. Miller  * @attrtype: attribute type
1253569a8fc3SDavid S. Miller  * @value: numeric value
1254569a8fc3SDavid S. Miller  */
1255569a8fc3SDavid S. Miller static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
1256569a8fc3SDavid S. Miller {
1257b4391db4SArnd Bergmann 	__be32 tmp = value;
1258b4391db4SArnd Bergmann 
1259b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__be32), &tmp);
1260569a8fc3SDavid S. Miller }
1261569a8fc3SDavid S. Miller 
1262569a8fc3SDavid S. Miller /**
12636c1dd3b6SDavid S. Miller  * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer
12646c1dd3b6SDavid S. Miller  * @skb: socket buffer to add attribute to
12656c1dd3b6SDavid S. Miller  * @attrtype: attribute type
12666c1dd3b6SDavid S. Miller  * @value: numeric value
12676c1dd3b6SDavid S. Miller  */
12686c1dd3b6SDavid S. Miller static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
12696c1dd3b6SDavid S. Miller {
1270b4391db4SArnd Bergmann 	__be32 tmp = value;
1271b4391db4SArnd Bergmann 
1272b4391db4SArnd Bergmann 	return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
12736c1dd3b6SDavid S. Miller }
12746c1dd3b6SDavid S. Miller 
12756c1dd3b6SDavid S. Miller /**
127624c410dcSDavid S. Miller  * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer
127724c410dcSDavid S. Miller  * @skb: socket buffer to add attribute to
127824c410dcSDavid S. Miller  * @attrtype: attribute type
127924c410dcSDavid S. Miller  * @value: numeric value
128024c410dcSDavid S. Miller  */
128124c410dcSDavid S. Miller static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value)
128224c410dcSDavid S. Miller {
1283b4391db4SArnd Bergmann 	__le32 tmp = value;
1284b4391db4SArnd Bergmann 
1285b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__le32), &tmp);
128624c410dcSDavid S. Miller }
128724c410dcSDavid S. Miller 
128824c410dcSDavid S. Miller /**
128973520786SNicolas Dichtel  * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it
129073520786SNicolas Dichtel  * @skb: socket buffer to add attribute to
129173520786SNicolas Dichtel  * @attrtype: attribute type
129273520786SNicolas Dichtel  * @value: numeric value
129373520786SNicolas Dichtel  * @padattr: attribute type for the padding
129473520786SNicolas Dichtel  */
129573520786SNicolas Dichtel static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype,
129673520786SNicolas Dichtel 				    u64 value, int padattr)
129773520786SNicolas Dichtel {
1298b4391db4SArnd Bergmann 	u64 tmp = value;
1299b4391db4SArnd Bergmann 
1300b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
130173520786SNicolas Dichtel }
130273520786SNicolas Dichtel 
130373520786SNicolas Dichtel /**
1304b46f6dedSNicolas Dichtel  * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it
1305569a8fc3SDavid S. Miller  * @skb: socket buffer to add attribute to
1306569a8fc3SDavid S. Miller  * @attrtype: attribute type
1307569a8fc3SDavid S. Miller  * @value: numeric value
1308b46f6dedSNicolas Dichtel  * @padattr: attribute type for the padding
1309569a8fc3SDavid S. Miller  */
1310b46f6dedSNicolas Dichtel static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value,
1311b46f6dedSNicolas Dichtel 			       int padattr)
1312b46f6dedSNicolas Dichtel {
1313b4391db4SArnd Bergmann 	__be64 tmp = value;
1314b4391db4SArnd Bergmann 
1315b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr);
1316b46f6dedSNicolas Dichtel }
1317b46f6dedSNicolas Dichtel 
1318569a8fc3SDavid S. Miller /**
1319e9bbe898SNicolas Dichtel  * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it
13206c1dd3b6SDavid S. Miller  * @skb: socket buffer to add attribute to
13216c1dd3b6SDavid S. Miller  * @attrtype: attribute type
13226c1dd3b6SDavid S. Miller  * @value: numeric value
1323e9bbe898SNicolas Dichtel  * @padattr: attribute type for the padding
13246c1dd3b6SDavid S. Miller  */
1325e9bbe898SNicolas Dichtel static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value,
1326e9bbe898SNicolas Dichtel 				int padattr)
13276c1dd3b6SDavid S. Miller {
1328b4391db4SArnd Bergmann 	__be64 tmp = value;
1329b4391db4SArnd Bergmann 
1330b4391db4SArnd Bergmann 	return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp,
1331e9bbe898SNicolas Dichtel 			    padattr);
13326c1dd3b6SDavid S. Miller }
13336c1dd3b6SDavid S. Miller 
13346c1dd3b6SDavid S. Miller /**
1335e7479122SNicolas Dichtel  * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it
133624c410dcSDavid S. Miller  * @skb: socket buffer to add attribute to
133724c410dcSDavid S. Miller  * @attrtype: attribute type
133824c410dcSDavid S. Miller  * @value: numeric value
1339e7479122SNicolas Dichtel  * @padattr: attribute type for the padding
134024c410dcSDavid S. Miller  */
1341e7479122SNicolas Dichtel static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value,
1342e7479122SNicolas Dichtel 			       int padattr)
134324c410dcSDavid S. Miller {
1344b4391db4SArnd Bergmann 	__le64 tmp = value;
1345b4391db4SArnd Bergmann 
1346b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr);
134724c410dcSDavid S. Miller }
134824c410dcSDavid S. Miller 
134924c410dcSDavid S. Miller /**
13504778e0beSJiri Pirko  * nla_put_s8 - Add a s8 netlink attribute to a socket buffer
13514778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
13524778e0beSJiri Pirko  * @attrtype: attribute type
13534778e0beSJiri Pirko  * @value: numeric value
13544778e0beSJiri Pirko  */
13554778e0beSJiri Pirko static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
13564778e0beSJiri Pirko {
1357b4391db4SArnd Bergmann 	s8 tmp = value;
1358b4391db4SArnd Bergmann 
1359b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(s8), &tmp);
13604778e0beSJiri Pirko }
13614778e0beSJiri Pirko 
13624778e0beSJiri Pirko /**
13634778e0beSJiri Pirko  * nla_put_s16 - Add a s16 netlink attribute to a socket buffer
13644778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
13654778e0beSJiri Pirko  * @attrtype: attribute type
13664778e0beSJiri Pirko  * @value: numeric value
13674778e0beSJiri Pirko  */
13684778e0beSJiri Pirko static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
13694778e0beSJiri Pirko {
1370b4391db4SArnd Bergmann 	s16 tmp = value;
1371b4391db4SArnd Bergmann 
1372b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(s16), &tmp);
13734778e0beSJiri Pirko }
13744778e0beSJiri Pirko 
13754778e0beSJiri Pirko /**
13764778e0beSJiri Pirko  * nla_put_s32 - Add a s32 netlink attribute to a socket buffer
13774778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
13784778e0beSJiri Pirko  * @attrtype: attribute type
13794778e0beSJiri Pirko  * @value: numeric value
13804778e0beSJiri Pirko  */
13814778e0beSJiri Pirko static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
13824778e0beSJiri Pirko {
1383b4391db4SArnd Bergmann 	s32 tmp = value;
1384b4391db4SArnd Bergmann 
1385b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(s32), &tmp);
13864778e0beSJiri Pirko }
13874778e0beSJiri Pirko 
13884778e0beSJiri Pirko /**
1389756a2f59SNicolas Dichtel  * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it
13904778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
13914778e0beSJiri Pirko  * @attrtype: attribute type
13924778e0beSJiri Pirko  * @value: numeric value
1393756a2f59SNicolas Dichtel  * @padattr: attribute type for the padding
13944778e0beSJiri Pirko  */
1395756a2f59SNicolas Dichtel static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value,
1396756a2f59SNicolas Dichtel 			      int padattr)
13974778e0beSJiri Pirko {
1398b4391db4SArnd Bergmann 	s64 tmp = value;
1399b4391db4SArnd Bergmann 
1400b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr);
14014778e0beSJiri Pirko }
14024778e0beSJiri Pirko 
14034778e0beSJiri Pirko /**
1404bfa83a9eSThomas Graf  * nla_put_string - Add a string netlink attribute to a socket buffer
1405bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1406bfa83a9eSThomas Graf  * @attrtype: attribute type
1407bfa83a9eSThomas Graf  * @str: NUL terminated string
1408bfa83a9eSThomas Graf  */
1409bfa83a9eSThomas Graf static inline int nla_put_string(struct sk_buff *skb, int attrtype,
1410bfa83a9eSThomas Graf 				 const char *str)
1411bfa83a9eSThomas Graf {
1412bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, strlen(str) + 1, str);
1413bfa83a9eSThomas Graf }
1414bfa83a9eSThomas Graf 
1415bfa83a9eSThomas Graf /**
1416bfa83a9eSThomas Graf  * nla_put_flag - Add a flag netlink attribute to a socket buffer
1417bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1418bfa83a9eSThomas Graf  * @attrtype: attribute type
1419bfa83a9eSThomas Graf  */
1420bfa83a9eSThomas Graf static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
1421bfa83a9eSThomas Graf {
1422bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, 0, NULL);
1423bfa83a9eSThomas Graf }
1424bfa83a9eSThomas Graf 
1425bfa83a9eSThomas Graf /**
14262175d87cSNicolas Dichtel  * nla_put_msecs - Add a msecs netlink attribute to a skb and align it
1427bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1428bfa83a9eSThomas Graf  * @attrtype: attribute type
1429d87de1f3SMark Rustad  * @njiffies: number of jiffies to convert to msecs
14302175d87cSNicolas Dichtel  * @padattr: attribute type for the padding
1431bfa83a9eSThomas Graf  */
1432bfa83a9eSThomas Graf static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
14332175d87cSNicolas Dichtel 				unsigned long njiffies, int padattr)
1434bfa83a9eSThomas Graf {
1435d87de1f3SMark Rustad 	u64 tmp = jiffies_to_msecs(njiffies);
14362175d87cSNicolas Dichtel 
14372175d87cSNicolas Dichtel 	return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
1438bfa83a9eSThomas Graf }
1439bfa83a9eSThomas Graf 
1440bfa83a9eSThomas Graf /**
1441930345eaSJiri Benc  * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket
1442930345eaSJiri Benc  * buffer
1443930345eaSJiri Benc  * @skb: socket buffer to add attribute to
1444930345eaSJiri Benc  * @attrtype: attribute type
1445930345eaSJiri Benc  * @addr: IPv4 address
1446930345eaSJiri Benc  */
1447930345eaSJiri Benc static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
1448930345eaSJiri Benc 				  __be32 addr)
1449930345eaSJiri Benc {
1450b4391db4SArnd Bergmann 	__be32 tmp = addr;
1451b4391db4SArnd Bergmann 
1452b4391db4SArnd Bergmann 	return nla_put_be32(skb, attrtype, tmp);
1453930345eaSJiri Benc }
1454930345eaSJiri Benc 
1455930345eaSJiri Benc /**
1456930345eaSJiri Benc  * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket
1457930345eaSJiri Benc  * buffer
1458930345eaSJiri Benc  * @skb: socket buffer to add attribute to
1459930345eaSJiri Benc  * @attrtype: attribute type
1460930345eaSJiri Benc  * @addr: IPv6 address
1461930345eaSJiri Benc  */
1462930345eaSJiri Benc static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype,
1463930345eaSJiri Benc 				   const struct in6_addr *addr)
1464930345eaSJiri Benc {
1465930345eaSJiri Benc 	return nla_put(skb, attrtype, sizeof(*addr), addr);
1466930345eaSJiri Benc }
1467930345eaSJiri Benc 
1468930345eaSJiri Benc /**
1469bfa83a9eSThomas Graf  * nla_get_u32 - return payload of u32 attribute
1470bfa83a9eSThomas Graf  * @nla: u32 netlink attribute
1471bfa83a9eSThomas Graf  */
1472b057efd4SPatrick McHardy static inline u32 nla_get_u32(const struct nlattr *nla)
1473bfa83a9eSThomas Graf {
1474bfa83a9eSThomas Graf 	return *(u32 *) nla_data(nla);
1475bfa83a9eSThomas Graf }
1476bfa83a9eSThomas Graf 
1477bfa83a9eSThomas Graf /**
147800012e5bSAl Viro  * nla_get_be32 - return payload of __be32 attribute
147900012e5bSAl Viro  * @nla: __be32 netlink attribute
148000012e5bSAl Viro  */
1481b057efd4SPatrick McHardy static inline __be32 nla_get_be32(const struct nlattr *nla)
148200012e5bSAl Viro {
148300012e5bSAl Viro 	return *(__be32 *) nla_data(nla);
148400012e5bSAl Viro }
148500012e5bSAl Viro 
148600012e5bSAl Viro /**
1487c648a013SAlexander Aring  * nla_get_le32 - return payload of __le32 attribute
1488c648a013SAlexander Aring  * @nla: __le32 netlink attribute
1489c648a013SAlexander Aring  */
1490c648a013SAlexander Aring static inline __le32 nla_get_le32(const struct nlattr *nla)
1491c648a013SAlexander Aring {
1492c648a013SAlexander Aring 	return *(__le32 *) nla_data(nla);
1493c648a013SAlexander Aring }
1494c648a013SAlexander Aring 
1495c648a013SAlexander Aring /**
1496bfa83a9eSThomas Graf  * nla_get_u16 - return payload of u16 attribute
1497bfa83a9eSThomas Graf  * @nla: u16 netlink attribute
1498bfa83a9eSThomas Graf  */
1499b057efd4SPatrick McHardy static inline u16 nla_get_u16(const struct nlattr *nla)
1500bfa83a9eSThomas Graf {
1501bfa83a9eSThomas Graf 	return *(u16 *) nla_data(nla);
1502bfa83a9eSThomas Graf }
1503bfa83a9eSThomas Graf 
1504bfa83a9eSThomas Graf /**
1505838965baSPatrick McHardy  * nla_get_be16 - return payload of __be16 attribute
1506838965baSPatrick McHardy  * @nla: __be16 netlink attribute
1507838965baSPatrick McHardy  */
1508b057efd4SPatrick McHardy static inline __be16 nla_get_be16(const struct nlattr *nla)
1509838965baSPatrick McHardy {
1510838965baSPatrick McHardy 	return *(__be16 *) nla_data(nla);
1511838965baSPatrick McHardy }
1512838965baSPatrick McHardy 
1513838965baSPatrick McHardy /**
15144a89c256SThomas Graf  * nla_get_le16 - return payload of __le16 attribute
15154a89c256SThomas Graf  * @nla: __le16 netlink attribute
15164a89c256SThomas Graf  */
1517b057efd4SPatrick McHardy static inline __le16 nla_get_le16(const struct nlattr *nla)
15184a89c256SThomas Graf {
15194a89c256SThomas Graf 	return *(__le16 *) nla_data(nla);
15204a89c256SThomas Graf }
15214a89c256SThomas Graf 
15224a89c256SThomas Graf /**
1523bfa83a9eSThomas Graf  * nla_get_u8 - return payload of u8 attribute
1524bfa83a9eSThomas Graf  * @nla: u8 netlink attribute
1525bfa83a9eSThomas Graf  */
1526b057efd4SPatrick McHardy static inline u8 nla_get_u8(const struct nlattr *nla)
1527bfa83a9eSThomas Graf {
1528bfa83a9eSThomas Graf 	return *(u8 *) nla_data(nla);
1529bfa83a9eSThomas Graf }
1530bfa83a9eSThomas Graf 
1531bfa83a9eSThomas Graf /**
1532bfa83a9eSThomas Graf  * nla_get_u64 - return payload of u64 attribute
1533bfa83a9eSThomas Graf  * @nla: u64 netlink attribute
1534bfa83a9eSThomas Graf  */
1535b057efd4SPatrick McHardy static inline u64 nla_get_u64(const struct nlattr *nla)
1536bfa83a9eSThomas Graf {
1537bfa83a9eSThomas Graf 	u64 tmp;
1538bfa83a9eSThomas Graf 
1539bfa83a9eSThomas Graf 	nla_memcpy(&tmp, nla, sizeof(tmp));
1540bfa83a9eSThomas Graf 
1541bfa83a9eSThomas Graf 	return tmp;
1542bfa83a9eSThomas Graf }
1543bfa83a9eSThomas Graf 
1544bfa83a9eSThomas Graf /**
1545a17c8598SPablo Neira Ayuso  * nla_get_be64 - return payload of __be64 attribute
1546a17c8598SPablo Neira Ayuso  * @nla: __be64 netlink attribute
1547a17c8598SPablo Neira Ayuso  */
1548a17c8598SPablo Neira Ayuso static inline __be64 nla_get_be64(const struct nlattr *nla)
1549a17c8598SPablo Neira Ayuso {
1550f5d410f2SPablo Neira Ayuso 	__be64 tmp;
1551f5d410f2SPablo Neira Ayuso 
1552f5d410f2SPablo Neira Ayuso 	nla_memcpy(&tmp, nla, sizeof(tmp));
1553f5d410f2SPablo Neira Ayuso 
1554f5d410f2SPablo Neira Ayuso 	return tmp;
1555a17c8598SPablo Neira Ayuso }
1556a17c8598SPablo Neira Ayuso 
1557a17c8598SPablo Neira Ayuso /**
1558c648a013SAlexander Aring  * nla_get_le64 - return payload of __le64 attribute
1559c648a013SAlexander Aring  * @nla: __le64 netlink attribute
1560c648a013SAlexander Aring  */
1561c648a013SAlexander Aring static inline __le64 nla_get_le64(const struct nlattr *nla)
1562c648a013SAlexander Aring {
1563c648a013SAlexander Aring 	return *(__le64 *) nla_data(nla);
1564c648a013SAlexander Aring }
1565c648a013SAlexander Aring 
1566c648a013SAlexander Aring /**
15674778e0beSJiri Pirko  * nla_get_s32 - return payload of s32 attribute
15684778e0beSJiri Pirko  * @nla: s32 netlink attribute
15694778e0beSJiri Pirko  */
15704778e0beSJiri Pirko static inline s32 nla_get_s32(const struct nlattr *nla)
15714778e0beSJiri Pirko {
15724778e0beSJiri Pirko 	return *(s32 *) nla_data(nla);
15734778e0beSJiri Pirko }
15744778e0beSJiri Pirko 
15754778e0beSJiri Pirko /**
15764778e0beSJiri Pirko  * nla_get_s16 - return payload of s16 attribute
15774778e0beSJiri Pirko  * @nla: s16 netlink attribute
15784778e0beSJiri Pirko  */
15794778e0beSJiri Pirko static inline s16 nla_get_s16(const struct nlattr *nla)
15804778e0beSJiri Pirko {
15814778e0beSJiri Pirko 	return *(s16 *) nla_data(nla);
15824778e0beSJiri Pirko }
15834778e0beSJiri Pirko 
15844778e0beSJiri Pirko /**
15854778e0beSJiri Pirko  * nla_get_s8 - return payload of s8 attribute
15864778e0beSJiri Pirko  * @nla: s8 netlink attribute
15874778e0beSJiri Pirko  */
15884778e0beSJiri Pirko static inline s8 nla_get_s8(const struct nlattr *nla)
15894778e0beSJiri Pirko {
15904778e0beSJiri Pirko 	return *(s8 *) nla_data(nla);
15914778e0beSJiri Pirko }
15924778e0beSJiri Pirko 
15934778e0beSJiri Pirko /**
15944778e0beSJiri Pirko  * nla_get_s64 - return payload of s64 attribute
15954778e0beSJiri Pirko  * @nla: s64 netlink attribute
15964778e0beSJiri Pirko  */
15974778e0beSJiri Pirko static inline s64 nla_get_s64(const struct nlattr *nla)
15984778e0beSJiri Pirko {
15994778e0beSJiri Pirko 	s64 tmp;
16004778e0beSJiri Pirko 
16014778e0beSJiri Pirko 	nla_memcpy(&tmp, nla, sizeof(tmp));
16024778e0beSJiri Pirko 
16034778e0beSJiri Pirko 	return tmp;
16044778e0beSJiri Pirko }
16054778e0beSJiri Pirko 
16064778e0beSJiri Pirko /**
1607bfa83a9eSThomas Graf  * nla_get_flag - return payload of flag attribute
1608bfa83a9eSThomas Graf  * @nla: flag netlink attribute
1609bfa83a9eSThomas Graf  */
1610b057efd4SPatrick McHardy static inline int nla_get_flag(const struct nlattr *nla)
1611bfa83a9eSThomas Graf {
1612bfa83a9eSThomas Graf 	return !!nla;
1613bfa83a9eSThomas Graf }
1614bfa83a9eSThomas Graf 
1615bfa83a9eSThomas Graf /**
1616bfa83a9eSThomas Graf  * nla_get_msecs - return payload of msecs attribute
1617bfa83a9eSThomas Graf  * @nla: msecs netlink attribute
1618bfa83a9eSThomas Graf  *
1619bfa83a9eSThomas Graf  * Returns the number of milliseconds in jiffies.
1620bfa83a9eSThomas Graf  */
1621b057efd4SPatrick McHardy static inline unsigned long nla_get_msecs(const struct nlattr *nla)
1622bfa83a9eSThomas Graf {
1623bfa83a9eSThomas Graf 	u64 msecs = nla_get_u64(nla);
1624bfa83a9eSThomas Graf 
1625bfa83a9eSThomas Graf 	return msecs_to_jiffies((unsigned long) msecs);
1626bfa83a9eSThomas Graf }
1627bfa83a9eSThomas Graf 
1628bfa83a9eSThomas Graf /**
162967b61f6cSJiri Benc  * nla_get_in_addr - return payload of IPv4 address attribute
163067b61f6cSJiri Benc  * @nla: IPv4 address netlink attribute
163167b61f6cSJiri Benc  */
163267b61f6cSJiri Benc static inline __be32 nla_get_in_addr(const struct nlattr *nla)
163367b61f6cSJiri Benc {
163467b61f6cSJiri Benc 	return *(__be32 *) nla_data(nla);
163567b61f6cSJiri Benc }
163667b61f6cSJiri Benc 
163767b61f6cSJiri Benc /**
163867b61f6cSJiri Benc  * nla_get_in6_addr - return payload of IPv6 address attribute
163967b61f6cSJiri Benc  * @nla: IPv6 address netlink attribute
164067b61f6cSJiri Benc  */
164167b61f6cSJiri Benc static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla)
164267b61f6cSJiri Benc {
164367b61f6cSJiri Benc 	struct in6_addr tmp;
164467b61f6cSJiri Benc 
164567b61f6cSJiri Benc 	nla_memcpy(&tmp, nla, sizeof(tmp));
164667b61f6cSJiri Benc 	return tmp;
164767b61f6cSJiri Benc }
164867b61f6cSJiri Benc 
164967b61f6cSJiri Benc /**
165064c83d83SJamal Hadi Salim  * nla_get_bitfield32 - return payload of 32 bitfield attribute
165164c83d83SJamal Hadi Salim  * @nla: nla_bitfield32 attribute
165264c83d83SJamal Hadi Salim  */
165364c83d83SJamal Hadi Salim static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla)
165464c83d83SJamal Hadi Salim {
165564c83d83SJamal Hadi Salim 	struct nla_bitfield32 tmp;
165664c83d83SJamal Hadi Salim 
165764c83d83SJamal Hadi Salim 	nla_memcpy(&tmp, nla, sizeof(tmp));
165864c83d83SJamal Hadi Salim 	return tmp;
165964c83d83SJamal Hadi Salim }
166064c83d83SJamal Hadi Salim 
166164c83d83SJamal Hadi Salim /**
1662b15ca182SThomas Graf  * nla_memdup - duplicate attribute memory (kmemdup)
1663b15ca182SThomas Graf  * @src: netlink attribute to duplicate from
1664b15ca182SThomas Graf  * @gfp: GFP mask
1665b15ca182SThomas Graf  */
1666b15ca182SThomas Graf static inline void *nla_memdup(const struct nlattr *src, gfp_t gfp)
1667b15ca182SThomas Graf {
1668b15ca182SThomas Graf 	return kmemdup(nla_data(src), nla_len(src), gfp);
1669b15ca182SThomas Graf }
1670b15ca182SThomas Graf 
1671b15ca182SThomas Graf /**
1672ae0be8deSMichal Kubecek  * nla_nest_start_noflag - Start a new level of nested attributes
1673bfa83a9eSThomas Graf  * @skb: socket buffer to add attributes to
1674bfa83a9eSThomas Graf  * @attrtype: attribute type of container
1675bfa83a9eSThomas Graf  *
1676ae0be8deSMichal Kubecek  * This function exists for backward compatibility to use in APIs which never
1677ae0be8deSMichal Kubecek  * marked their nest attributes with NLA_F_NESTED flag. New APIs should use
1678ae0be8deSMichal Kubecek  * nla_nest_start() which sets the flag.
1679ae0be8deSMichal Kubecek  *
1680ae0be8deSMichal Kubecek  * Returns the container attribute or NULL on error
1681bfa83a9eSThomas Graf  */
1682ae0be8deSMichal Kubecek static inline struct nlattr *nla_nest_start_noflag(struct sk_buff *skb,
1683ae0be8deSMichal Kubecek 						   int attrtype)
1684bfa83a9eSThomas Graf {
168527a884dcSArnaldo Carvalho de Melo 	struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
1686bfa83a9eSThomas Graf 
1687bfa83a9eSThomas Graf 	if (nla_put(skb, attrtype, 0, NULL) < 0)
1688bfa83a9eSThomas Graf 		return NULL;
1689bfa83a9eSThomas Graf 
1690bfa83a9eSThomas Graf 	return start;
1691bfa83a9eSThomas Graf }
1692bfa83a9eSThomas Graf 
1693bfa83a9eSThomas Graf /**
1694ae0be8deSMichal Kubecek  * nla_nest_start - Start a new level of nested attributes, with NLA_F_NESTED
1695ae0be8deSMichal Kubecek  * @skb: socket buffer to add attributes to
1696ae0be8deSMichal Kubecek  * @attrtype: attribute type of container
1697ae0be8deSMichal Kubecek  *
1698ae0be8deSMichal Kubecek  * Unlike nla_nest_start_noflag(), mark the nest attribute with NLA_F_NESTED
1699ae0be8deSMichal Kubecek  * flag. This is the preferred function to use in new code.
1700ae0be8deSMichal Kubecek  *
1701ae0be8deSMichal Kubecek  * Returns the container attribute or NULL on error
1702ae0be8deSMichal Kubecek  */
1703ae0be8deSMichal Kubecek static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
1704ae0be8deSMichal Kubecek {
1705ae0be8deSMichal Kubecek 	return nla_nest_start_noflag(skb, attrtype | NLA_F_NESTED);
1706ae0be8deSMichal Kubecek }
1707ae0be8deSMichal Kubecek 
1708ae0be8deSMichal Kubecek /**
1709bfa83a9eSThomas Graf  * nla_nest_end - Finalize nesting of attributes
1710d1ec3b77SPierre Ynard  * @skb: socket buffer the attributes are stored in
1711bfa83a9eSThomas Graf  * @start: container attribute
1712bfa83a9eSThomas Graf  *
1713bfa83a9eSThomas Graf  * Corrects the container attribute header to include the all
1714bfa83a9eSThomas Graf  * appeneded attributes.
1715bfa83a9eSThomas Graf  *
1716bfa83a9eSThomas Graf  * Returns the total data length of the skb.
1717bfa83a9eSThomas Graf  */
1718bfa83a9eSThomas Graf static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
1719bfa83a9eSThomas Graf {
172027a884dcSArnaldo Carvalho de Melo 	start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
1721bfa83a9eSThomas Graf 	return skb->len;
1722bfa83a9eSThomas Graf }
1723bfa83a9eSThomas Graf 
1724bfa83a9eSThomas Graf /**
1725bfa83a9eSThomas Graf  * nla_nest_cancel - Cancel nesting of attributes
1726bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
1727bfa83a9eSThomas Graf  * @start: container attribute
1728bfa83a9eSThomas Graf  *
1729bfa83a9eSThomas Graf  * Removes the container attribute and including all nested
1730bc3ed28cSThomas Graf  * attributes. Returns -EMSGSIZE
1731bfa83a9eSThomas Graf  */
1732bc3ed28cSThomas Graf static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
1733bfa83a9eSThomas Graf {
1734bc3ed28cSThomas Graf 	nlmsg_trim(skb, start);
1735bfa83a9eSThomas Graf }
1736bfa83a9eSThomas Graf 
1737bfa83a9eSThomas Graf /**
173832d5109aSMichal Kubecek  * __nla_validate_nested - Validate a stream of nested attributes
17394fe5d5c0SPaul Moore  * @start: container attribute
17404fe5d5c0SPaul Moore  * @maxtype: maximum attribute type to be expected
17414fe5d5c0SPaul Moore  * @policy: validation policy
17428cb08174SJohannes Berg  * @validate: validation strictness
1743fceb6435SJohannes Berg  * @extack: extended ACK report struct
17444fe5d5c0SPaul Moore  *
17454fe5d5c0SPaul Moore  * Validates all attributes in the nested attribute stream against the
17464fe5d5c0SPaul Moore  * specified policy. Attributes with a type exceeding maxtype will be
17474fe5d5c0SPaul Moore  * ignored. See documenation of struct nla_policy for more details.
17484fe5d5c0SPaul Moore  *
17494fe5d5c0SPaul Moore  * Returns 0 on success or a negative error code.
17504fe5d5c0SPaul Moore  */
17518cb08174SJohannes Berg static inline int __nla_validate_nested(const struct nlattr *start, int maxtype,
17528cb08174SJohannes Berg 					const struct nla_policy *policy,
17538cb08174SJohannes Berg 					unsigned int validate,
17548cb08174SJohannes Berg 					struct netlink_ext_ack *extack)
17558cb08174SJohannes Berg {
17568cb08174SJohannes Berg 	return __nla_validate(nla_data(start), nla_len(start), maxtype, policy,
17578cb08174SJohannes Berg 			      validate, extack);
17588cb08174SJohannes Berg }
17598cb08174SJohannes Berg 
17608cb08174SJohannes Berg static inline int
176132d5109aSMichal Kubecek nla_validate_nested(const struct nlattr *start, int maxtype,
1762901bb989SJohannes Berg 		    const struct nla_policy *policy,
1763901bb989SJohannes Berg 		    struct netlink_ext_ack *extack)
1764901bb989SJohannes Berg {
1765901bb989SJohannes Berg 	return __nla_validate_nested(start, maxtype, policy,
1766901bb989SJohannes Berg 				     NL_VALIDATE_STRICT, extack);
1767901bb989SJohannes Berg }
1768901bb989SJohannes Berg 
1769901bb989SJohannes Berg static inline int
17708cb08174SJohannes Berg nla_validate_nested_deprecated(const struct nlattr *start, int maxtype,
1771fceb6435SJohannes Berg 			       const struct nla_policy *policy,
1772fceb6435SJohannes Berg 			       struct netlink_ext_ack *extack)
17734fe5d5c0SPaul Moore {
17748cb08174SJohannes Berg 	return __nla_validate_nested(start, maxtype, policy,
17758cb08174SJohannes Berg 				     NL_VALIDATE_LIBERAL, extack);
17764fe5d5c0SPaul Moore }
17774fe5d5c0SPaul Moore 
17784fe5d5c0SPaul Moore /**
1779089bf1a6SNicolas Dichtel  * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute
1780089bf1a6SNicolas Dichtel  * @skb: socket buffer the message is stored in
1781089bf1a6SNicolas Dichtel  *
1782089bf1a6SNicolas Dichtel  * Return true if padding is needed to align the next attribute (nla_data()) to
1783089bf1a6SNicolas Dichtel  * a 64-bit aligned area.
1784089bf1a6SNicolas Dichtel  */
1785089bf1a6SNicolas Dichtel static inline bool nla_need_padding_for_64bit(struct sk_buff *skb)
1786089bf1a6SNicolas Dichtel {
1787089bf1a6SNicolas Dichtel #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1788089bf1a6SNicolas Dichtel 	/* The nlattr header is 4 bytes in size, that's why we test
1789089bf1a6SNicolas Dichtel 	 * if the skb->data _is_ aligned.  A NOP attribute, plus
1790089bf1a6SNicolas Dichtel 	 * nlattr header for next attribute, will make nla_data()
1791089bf1a6SNicolas Dichtel 	 * 8-byte aligned.
1792089bf1a6SNicolas Dichtel 	 */
1793089bf1a6SNicolas Dichtel 	if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
1794089bf1a6SNicolas Dichtel 		return true;
1795089bf1a6SNicolas Dichtel #endif
1796089bf1a6SNicolas Dichtel 	return false;
1797089bf1a6SNicolas Dichtel }
1798089bf1a6SNicolas Dichtel 
1799089bf1a6SNicolas Dichtel /**
180035c58459SDavid S. Miller  * nla_align_64bit - 64-bit align the nla_data() of next attribute
180135c58459SDavid S. Miller  * @skb: socket buffer the message is stored in
180235c58459SDavid S. Miller  * @padattr: attribute type for the padding
180335c58459SDavid S. Miller  *
180435c58459SDavid S. Miller  * Conditionally emit a padding netlink attribute in order to make
180535c58459SDavid S. Miller  * the next attribute we emit have a 64-bit aligned nla_data() area.
180635c58459SDavid S. Miller  * This will only be done in architectures which do not have
1807cca1d815SEric Dumazet  * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
180835c58459SDavid S. Miller  *
180935c58459SDavid S. Miller  * Returns zero on success or a negative error code.
181035c58459SDavid S. Miller  */
181135c58459SDavid S. Miller static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
181235c58459SDavid S. Miller {
1813089bf1a6SNicolas Dichtel 	if (nla_need_padding_for_64bit(skb) &&
1814cca1d815SEric Dumazet 	    !nla_reserve(skb, padattr, 0))
181535c58459SDavid S. Miller 		return -EMSGSIZE;
1816089bf1a6SNicolas Dichtel 
181735c58459SDavid S. Miller 	return 0;
181835c58459SDavid S. Miller }
181935c58459SDavid S. Miller 
182035c58459SDavid S. Miller /**
182135c58459SDavid S. Miller  * nla_total_size_64bit - total length of attribute including padding
182235c58459SDavid S. Miller  * @payload: length of payload
182335c58459SDavid S. Miller  */
182435c58459SDavid S. Miller static inline int nla_total_size_64bit(int payload)
182535c58459SDavid S. Miller {
182635c58459SDavid S. Miller 	return NLA_ALIGN(nla_attr_size(payload))
1827cca1d815SEric Dumazet #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
182835c58459SDavid S. Miller 		+ NLA_ALIGN(nla_attr_size(0))
182935c58459SDavid S. Miller #endif
183035c58459SDavid S. Miller 		;
183135c58459SDavid S. Miller }
183235c58459SDavid S. Miller 
183335c58459SDavid S. Miller /**
1834bfa83a9eSThomas Graf  * nla_for_each_attr - iterate over a stream of attributes
1835bfa83a9eSThomas Graf  * @pos: loop counter, set to current attribute
1836bfa83a9eSThomas Graf  * @head: head of attribute stream
1837bfa83a9eSThomas Graf  * @len: length of attribute stream
1838bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
1839bfa83a9eSThomas Graf  */
1840bfa83a9eSThomas Graf #define nla_for_each_attr(pos, head, len, rem) \
1841bfa83a9eSThomas Graf 	for (pos = head, rem = len; \
1842bfa83a9eSThomas Graf 	     nla_ok(pos, rem); \
1843bfa83a9eSThomas Graf 	     pos = nla_next(pos, &(rem)))
1844bfa83a9eSThomas Graf 
1845fe4944e5SThomas Graf /**
1846fe4944e5SThomas Graf  * nla_for_each_nested - iterate over nested attributes
1847fe4944e5SThomas Graf  * @pos: loop counter, set to current attribute
1848fe4944e5SThomas Graf  * @nla: attribute containing the nested attributes
1849fe4944e5SThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
1850fe4944e5SThomas Graf  */
1851fe4944e5SThomas Graf #define nla_for_each_nested(pos, nla, rem) \
1852fe4944e5SThomas Graf 	nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
1853fe4944e5SThomas Graf 
1854941d8ebcSSimon Horman /**
1855941d8ebcSSimon Horman  * nla_is_last - Test if attribute is last in stream
1856941d8ebcSSimon Horman  * @nla: attribute to test
1857941d8ebcSSimon Horman  * @rem: bytes remaining in stream
1858941d8ebcSSimon Horman  */
1859941d8ebcSSimon Horman static inline bool nla_is_last(const struct nlattr *nla, int rem)
1860941d8ebcSSimon Horman {
1861941d8ebcSSimon Horman 	return nla->nla_len == rem;
1862941d8ebcSSimon Horman }
1863941d8ebcSSimon Horman 
1864bfa83a9eSThomas Graf #endif
1865