xref: /openbmc/linux/include/net/netlink.h (revision 3e48be05)
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
156bfa83a9eSThomas Graf  *   nla_parse_nested()			parse nested attribuets
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,
186bfa83a9eSThomas Graf 	__NLA_TYPE_MAX,
187bfa83a9eSThomas Graf };
188bfa83a9eSThomas Graf 
189bfa83a9eSThomas Graf #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
190bfa83a9eSThomas Graf 
1913e48be05SJohannes Berg enum nla_policy_validation {
1923e48be05SJohannes Berg 	NLA_VALIDATE_NONE,
1933e48be05SJohannes Berg 	NLA_VALIDATE_RANGE,
1943e48be05SJohannes Berg 	NLA_VALIDATE_MIN,
1953e48be05SJohannes Berg 	NLA_VALIDATE_MAX,
1963e48be05SJohannes Berg };
1973e48be05SJohannes Berg 
198bfa83a9eSThomas Graf /**
199bfa83a9eSThomas Graf  * struct nla_policy - attribute validation policy
200bfa83a9eSThomas Graf  * @type: Type of attribute or NLA_UNSPEC
2013e48be05SJohannes Berg  * @validation_type: type of attribute validation done in addition to
2023e48be05SJohannes Berg  *	type-specific validation (e.g. range), see
2033e48be05SJohannes Berg  *	&enum nla_policy_validation
204a5531a5dSThomas Graf  * @len: Type specific length of payload
205bfa83a9eSThomas Graf  *
206bfa83a9eSThomas Graf  * Policies are defined as arrays of this struct, the array must be
207bfa83a9eSThomas Graf  * accessible by attribute type up to the highest identifier to be expected.
208bfa83a9eSThomas Graf  *
209a5531a5dSThomas Graf  * Meaning of `len' field:
210a5531a5dSThomas Graf  *    NLA_STRING           Maximum length of string
211a5531a5dSThomas Graf  *    NLA_NUL_STRING       Maximum length of string (excluding NUL)
212a5531a5dSThomas Graf  *    NLA_FLAG             Unused
213d30045a0SJohannes Berg  *    NLA_BINARY           Maximum length of attribute payload
2141501d135SJohannes Berg  *    NLA_NESTED,
2151501d135SJohannes Berg  *    NLA_NESTED_ARRAY     Length verification is done by checking len of
2169a659a35SJohannes Berg  *                         nested header (or empty); len field is used if
2179a659a35SJohannes Berg  *                         validation_data is also used, for the max attr
2189a659a35SJohannes Berg  *                         number in the nested policy.
2194b6cc728SJohannes Berg  *    NLA_U8, NLA_U16,
2204b6cc728SJohannes Berg  *    NLA_U32, NLA_U64,
2214778e0beSJiri Pirko  *    NLA_S8, NLA_S16,
2224778e0beSJiri Pirko  *    NLA_S32, NLA_S64,
2234b6cc728SJohannes Berg  *    NLA_MSECS            Leaving the length field zero will verify the
2244b6cc728SJohannes Berg  *                         given type fits, using it verifies minimum length
2254b6cc728SJohannes Berg  *                         just like "All other"
226568b742aSJohannes Berg  *    NLA_BITFIELD32       Unused
227568b742aSJohannes Berg  *    NLA_REJECT           Unused
228b60b87fcSJohannes Berg  *    NLA_EXACT_LEN        Attribute must have exactly this length, otherwise
229b60b87fcSJohannes Berg  *                         it is rejected.
230b60b87fcSJohannes Berg  *    NLA_EXACT_LEN_WARN   Attribute should have exactly this length, a warning
231b60b87fcSJohannes Berg  *                         is logged if it is longer, shorter is rejected.
2324b6cc728SJohannes Berg  *    All other            Minimum length of attribute payload
233a5531a5dSThomas Graf  *
234568b742aSJohannes Berg  * Meaning of `validation_data' field:
235568b742aSJohannes Berg  *    NLA_BITFIELD32       This is a 32-bit bitmap/bitselector attribute and
236568b742aSJohannes Berg  *                         validation data must point to a u32 value of valid
237568b742aSJohannes Berg  *                         flags
238568b742aSJohannes Berg  *    NLA_REJECT           This attribute is always rejected and validation data
239568b742aSJohannes Berg  *                         may point to a string to report as the error instead
240568b742aSJohannes Berg  *                         of the generic one in extended ACK.
2419a659a35SJohannes Berg  *    NLA_NESTED           Points to a nested policy to validate, must also set
2429a659a35SJohannes Berg  *                         `len' to the max attribute number.
2439a659a35SJohannes Berg  *                         Note that nla_parse() will validate, but of course not
2449a659a35SJohannes Berg  *                         parse, the nested sub-policies.
2451501d135SJohannes Berg  *    NLA_NESTED_ARRAY     Points to a nested policy to validate, must also set
2461501d135SJohannes Berg  *                         `len' to the max attribute number. The difference to
2471501d135SJohannes Berg  *                         NLA_NESTED is the structure - NLA_NESTED has the
2481501d135SJohannes Berg  *                         nested attributes directly inside, while an array has
2491501d135SJohannes Berg  *                         the nested attributes at another level down and the
2501501d135SJohannes Berg  *                         attributes directly in the nesting don't matter.
2513e48be05SJohannes Berg  *    All other            Unused - but note that it's a union
2523e48be05SJohannes Berg  *
2533e48be05SJohannes Berg  * Meaning of `min' and `max' fields, use via NLA_POLICY_MIN, NLA_POLICY_MAX
2543e48be05SJohannes Berg  * and NLA_POLICY_RANGE:
2553e48be05SJohannes Berg  *    NLA_U8,
2563e48be05SJohannes Berg  *    NLA_U16,
2573e48be05SJohannes Berg  *    NLA_U32,
2583e48be05SJohannes Berg  *    NLA_U64,
2593e48be05SJohannes Berg  *    NLA_S8,
2603e48be05SJohannes Berg  *    NLA_S16,
2613e48be05SJohannes Berg  *    NLA_S32,
2623e48be05SJohannes Berg  *    NLA_S64              These are used depending on the validation_type
2633e48be05SJohannes Berg  *                         field, if that is min/max/range then the minimum,
2643e48be05SJohannes Berg  *                         maximum and both are used (respectively) to check
2653e48be05SJohannes Berg  *                         the value of the integer attribute.
2663e48be05SJohannes Berg  *                         Note that in the interest of code simplicity and
2673e48be05SJohannes Berg  *                         struct size both limits are s16, so you cannot
2683e48be05SJohannes Berg  *                         enforce a range that doesn't fall within the range
2693e48be05SJohannes Berg  *                         of s16 - do that as usual in the code instead.
2703e48be05SJohannes Berg  *    All other            Unused - but note that it's a union
271568b742aSJohannes Berg  *
272bfa83a9eSThomas Graf  * Example:
273b54452b0SAlexey Dobriyan  * static const struct nla_policy my_policy[ATTR_MAX+1] = {
274bfa83a9eSThomas Graf  * 	[ATTR_FOO] = { .type = NLA_U16 },
275d30045a0SJohannes Berg  *	[ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
276a5531a5dSThomas Graf  *	[ATTR_BAZ] = { .len = sizeof(struct mystruct) },
27764c83d83SJamal Hadi Salim  *	[ATTR_GOO] = { .type = NLA_BITFIELD32, .validation_data = &myvalidflags },
278bfa83a9eSThomas Graf  * };
279bfa83a9eSThomas Graf  */
280bfa83a9eSThomas Graf struct nla_policy {
2813e48be05SJohannes Berg 	u8		type;
2823e48be05SJohannes Berg 	u8		validation_type;
283a5531a5dSThomas Graf 	u16		len;
2843e48be05SJohannes Berg 	union {
28548fde90aSJohannes Berg 		const void *validation_data;
2863e48be05SJohannes Berg 		struct {
2873e48be05SJohannes Berg 			s16 min, max;
2883e48be05SJohannes Berg 		};
2893e48be05SJohannes Berg 	};
290bfa83a9eSThomas Graf };
291bfa83a9eSThomas Graf 
292b60b87fcSJohannes Berg #define NLA_POLICY_EXACT_LEN(_len)	{ .type = NLA_EXACT_LEN, .len = _len }
293b60b87fcSJohannes Berg #define NLA_POLICY_EXACT_LEN_WARN(_len)	{ .type = NLA_EXACT_LEN_WARN, \
294b60b87fcSJohannes Berg 					  .len = _len }
295b60b87fcSJohannes Berg 
296b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR		NLA_POLICY_EXACT_LEN(ETH_ALEN)
297b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR_COMPAT	NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN)
298b60b87fcSJohannes Berg 
2999a659a35SJohannes Berg #define NLA_POLICY_NESTED(maxattr, policy) \
3009a659a35SJohannes Berg 	{ .type = NLA_NESTED, .validation_data = policy, .len = maxattr }
3011501d135SJohannes Berg #define NLA_POLICY_NESTED_ARRAY(maxattr, policy) \
3021501d135SJohannes Berg 	{ .type = NLA_NESTED_ARRAY, .validation_data = policy, .len = maxattr }
3039a659a35SJohannes Berg 
3043e48be05SJohannes Berg #define __NLA_ENSURE(condition) (sizeof(char[1 - 2*!(condition)]) - 1)
3053e48be05SJohannes Berg #define NLA_ENSURE_INT_TYPE(tp)				\
3063e48be05SJohannes Berg 	(__NLA_ENSURE(tp == NLA_S8 || tp == NLA_U8 ||	\
3073e48be05SJohannes Berg 		      tp == NLA_S16 || tp == NLA_U16 ||	\
3083e48be05SJohannes Berg 		      tp == NLA_S32 || tp == NLA_U32 ||	\
3093e48be05SJohannes Berg 		      tp == NLA_S64 || tp == NLA_U64) + tp)
3103e48be05SJohannes Berg 
3113e48be05SJohannes Berg #define NLA_POLICY_RANGE(tp, _min, _max) {		\
3123e48be05SJohannes Berg 	.type = NLA_ENSURE_INT_TYPE(tp),		\
3133e48be05SJohannes Berg 	.validation_type = NLA_VALIDATE_RANGE,		\
3143e48be05SJohannes Berg 	.min = _min,					\
3153e48be05SJohannes Berg 	.max = _max					\
3163e48be05SJohannes Berg }
3173e48be05SJohannes Berg 
3183e48be05SJohannes Berg #define NLA_POLICY_MIN(tp, _min) {			\
3193e48be05SJohannes Berg 	.type = NLA_ENSURE_INT_TYPE(tp),		\
3203e48be05SJohannes Berg 	.validation_type = NLA_VALIDATE_MIN,		\
3213e48be05SJohannes Berg 	.min = _min,					\
3223e48be05SJohannes Berg }
3233e48be05SJohannes Berg 
3243e48be05SJohannes Berg #define NLA_POLICY_MAX(tp, _max) {			\
3253e48be05SJohannes Berg 	.type = NLA_ENSURE_INT_TYPE(tp),		\
3263e48be05SJohannes Berg 	.validation_type = NLA_VALIDATE_MAX,		\
3273e48be05SJohannes Berg 	.max = _max,					\
3283e48be05SJohannes Berg }
3293e48be05SJohannes Berg 
3304e902c57SThomas Graf /**
3314e902c57SThomas Graf  * struct nl_info - netlink source information
3324e902c57SThomas Graf  * @nlh: Netlink message header of original request
33315e47304SEric W. Biederman  * @portid: Netlink PORTID of requesting application
3344e902c57SThomas Graf  */
3354e902c57SThomas Graf struct nl_info {
3364e902c57SThomas Graf 	struct nlmsghdr		*nlh;
3374d1169c1SDenis V. Lunev 	struct net		*nl_net;
33815e47304SEric W. Biederman 	u32			portid;
3393b1137feSDavid Ahern 	bool			skip_notify;
3404e902c57SThomas Graf };
3414e902c57SThomas Graf 
3424f69053bSJoe Perches int netlink_rcv_skb(struct sk_buff *skb,
3432d4bc933SJohannes Berg 		    int (*cb)(struct sk_buff *, struct nlmsghdr *,
3442d4bc933SJohannes Berg 			      struct netlink_ext_ack *));
3454f69053bSJoe Perches int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
3464f69053bSJoe Perches 		 unsigned int group, int report, gfp_t flags);
34782ace47aSThomas Graf 
3484f69053bSJoe Perches int nla_validate(const struct nlattr *head, int len, int maxtype,
349fceb6435SJohannes Berg 		 const struct nla_policy *policy,
350fceb6435SJohannes Berg 		 struct netlink_ext_ack *extack);
3514f69053bSJoe Perches int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
352fceb6435SJohannes Berg 	      int len, const struct nla_policy *policy,
353fceb6435SJohannes Berg 	      struct netlink_ext_ack *extack);
3544f69053bSJoe Perches int nla_policy_len(const struct nla_policy *, int);
3554f69053bSJoe Perches struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
3564f69053bSJoe Perches size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize);
3572cf0c8b3SPhil Sutter char *nla_strdup(const struct nlattr *nla, gfp_t flags);
3584f69053bSJoe Perches int nla_memcpy(void *dest, const struct nlattr *src, int count);
3594f69053bSJoe Perches int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
3604f69053bSJoe Perches int nla_strcmp(const struct nlattr *nla, const char *str);
3614f69053bSJoe Perches struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
362089bf1a6SNicolas Dichtel struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
363089bf1a6SNicolas Dichtel 				   int attrlen, int padattr);
3644f69053bSJoe Perches void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
3654f69053bSJoe Perches struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
366089bf1a6SNicolas Dichtel struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype,
367089bf1a6SNicolas Dichtel 				 int attrlen, int padattr);
3684f69053bSJoe Perches void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
3694f69053bSJoe Perches void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
370fe4944e5SThomas Graf 	       const void *data);
371089bf1a6SNicolas Dichtel void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
372089bf1a6SNicolas Dichtel 		     const void *data, int padattr);
3734f69053bSJoe Perches void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
3744f69053bSJoe Perches int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);
375089bf1a6SNicolas Dichtel int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
376089bf1a6SNicolas Dichtel 		  const void *data, int padattr);
3774f69053bSJoe Perches int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
3784f69053bSJoe Perches int nla_append(struct sk_buff *skb, int attrlen, const void *data);
379bfa83a9eSThomas Graf 
380bfa83a9eSThomas Graf /**************************************************************************
381bfa83a9eSThomas Graf  * Netlink Messages
382bfa83a9eSThomas Graf  **************************************************************************/
383bfa83a9eSThomas Graf 
384bfa83a9eSThomas Graf /**
385bfa83a9eSThomas Graf  * nlmsg_msg_size - length of netlink message not including padding
386bfa83a9eSThomas Graf  * @payload: length of message payload
387bfa83a9eSThomas Graf  */
388bfa83a9eSThomas Graf static inline int nlmsg_msg_size(int payload)
389bfa83a9eSThomas Graf {
390bfa83a9eSThomas Graf 	return NLMSG_HDRLEN + payload;
391bfa83a9eSThomas Graf }
392bfa83a9eSThomas Graf 
393bfa83a9eSThomas Graf /**
394bfa83a9eSThomas Graf  * nlmsg_total_size - length of netlink message including padding
395bfa83a9eSThomas Graf  * @payload: length of message payload
396bfa83a9eSThomas Graf  */
397bfa83a9eSThomas Graf static inline int nlmsg_total_size(int payload)
398bfa83a9eSThomas Graf {
399bfa83a9eSThomas Graf 	return NLMSG_ALIGN(nlmsg_msg_size(payload));
400bfa83a9eSThomas Graf }
401bfa83a9eSThomas Graf 
402bfa83a9eSThomas Graf /**
403bfa83a9eSThomas Graf  * nlmsg_padlen - length of padding at the message's tail
404bfa83a9eSThomas Graf  * @payload: length of message payload
405bfa83a9eSThomas Graf  */
406bfa83a9eSThomas Graf static inline int nlmsg_padlen(int payload)
407bfa83a9eSThomas Graf {
408bfa83a9eSThomas Graf 	return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
409bfa83a9eSThomas Graf }
410bfa83a9eSThomas Graf 
411bfa83a9eSThomas Graf /**
412bfa83a9eSThomas Graf  * nlmsg_data - head of message payload
41370f23fd6SJustin P. Mattock  * @nlh: netlink message header
414bfa83a9eSThomas Graf  */
415bfa83a9eSThomas Graf static inline void *nlmsg_data(const struct nlmsghdr *nlh)
416bfa83a9eSThomas Graf {
417bfa83a9eSThomas Graf 	return (unsigned char *) nlh + NLMSG_HDRLEN;
418bfa83a9eSThomas Graf }
419bfa83a9eSThomas Graf 
420bfa83a9eSThomas Graf /**
421bfa83a9eSThomas Graf  * nlmsg_len - length of message payload
422bfa83a9eSThomas Graf  * @nlh: netlink message header
423bfa83a9eSThomas Graf  */
424bfa83a9eSThomas Graf static inline int nlmsg_len(const struct nlmsghdr *nlh)
425bfa83a9eSThomas Graf {
426bfa83a9eSThomas Graf 	return nlh->nlmsg_len - NLMSG_HDRLEN;
427bfa83a9eSThomas Graf }
428bfa83a9eSThomas Graf 
429bfa83a9eSThomas Graf /**
430bfa83a9eSThomas Graf  * nlmsg_attrdata - head of attributes data
431bfa83a9eSThomas Graf  * @nlh: netlink message header
432bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
433bfa83a9eSThomas Graf  */
434bfa83a9eSThomas Graf static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
435bfa83a9eSThomas Graf 					    int hdrlen)
436bfa83a9eSThomas Graf {
437bfa83a9eSThomas Graf 	unsigned char *data = nlmsg_data(nlh);
438bfa83a9eSThomas Graf 	return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
439bfa83a9eSThomas Graf }
440bfa83a9eSThomas Graf 
441bfa83a9eSThomas Graf /**
442bfa83a9eSThomas Graf  * nlmsg_attrlen - length of attributes data
443bfa83a9eSThomas Graf  * @nlh: netlink message header
444bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
445bfa83a9eSThomas Graf  */
446bfa83a9eSThomas Graf static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
447bfa83a9eSThomas Graf {
448bfa83a9eSThomas Graf 	return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
449bfa83a9eSThomas Graf }
450bfa83a9eSThomas Graf 
451bfa83a9eSThomas Graf /**
452bfa83a9eSThomas Graf  * nlmsg_ok - check if the netlink message fits into the remaining bytes
453bfa83a9eSThomas Graf  * @nlh: netlink message header
454bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in message stream
455bfa83a9eSThomas Graf  */
456bfa83a9eSThomas Graf static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
457bfa83a9eSThomas Graf {
458619e803dSVegard Nossum 	return (remaining >= (int) sizeof(struct nlmsghdr) &&
459bfa83a9eSThomas Graf 		nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
460bfa83a9eSThomas Graf 		nlh->nlmsg_len <= remaining);
461bfa83a9eSThomas Graf }
462bfa83a9eSThomas Graf 
463bfa83a9eSThomas Graf /**
464bfa83a9eSThomas Graf  * nlmsg_next - next netlink message in message stream
465bfa83a9eSThomas Graf  * @nlh: netlink message header
466bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in message stream
467bfa83a9eSThomas Graf  *
468bfa83a9eSThomas Graf  * Returns the next netlink message in the message stream and
469bfa83a9eSThomas Graf  * decrements remaining by the size of the current message.
470bfa83a9eSThomas Graf  */
4713654654fSJan Engelhardt static inline struct nlmsghdr *
4723654654fSJan Engelhardt nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
473bfa83a9eSThomas Graf {
474bfa83a9eSThomas Graf 	int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
475bfa83a9eSThomas Graf 
476bfa83a9eSThomas Graf 	*remaining -= totlen;
477bfa83a9eSThomas Graf 
478bfa83a9eSThomas Graf 	return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
479bfa83a9eSThomas Graf }
480bfa83a9eSThomas Graf 
481bfa83a9eSThomas Graf /**
482bfa83a9eSThomas Graf  * nlmsg_parse - parse attributes of a netlink message
483bfa83a9eSThomas Graf  * @nlh: netlink message header
484bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
485bfa83a9eSThomas Graf  * @tb: destination array with maxtype+1 elements
486bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
487bfa83a9eSThomas Graf  * @policy: validation policy
488fceb6435SJohannes Berg  * @extack: extended ACK report struct
489bfa83a9eSThomas Graf  *
490bfa83a9eSThomas Graf  * See nla_parse()
491bfa83a9eSThomas Graf  */
4923a6c2b41SPatrick McHardy static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
493bfa83a9eSThomas Graf 			      struct nlattr *tb[], int maxtype,
494fceb6435SJohannes Berg 			      const struct nla_policy *policy,
495fceb6435SJohannes Berg 			      struct netlink_ext_ack *extack)
496bfa83a9eSThomas Graf {
497bfa83a9eSThomas Graf 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
498bfa83a9eSThomas Graf 		return -EINVAL;
499bfa83a9eSThomas Graf 
500bfa83a9eSThomas Graf 	return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
501fceb6435SJohannes Berg 			 nlmsg_attrlen(nlh, hdrlen), policy, extack);
502bfa83a9eSThomas Graf }
503bfa83a9eSThomas Graf 
504bfa83a9eSThomas Graf /**
505bfa83a9eSThomas Graf  * nlmsg_find_attr - find a specific attribute in a netlink message
506bfa83a9eSThomas Graf  * @nlh: netlink message header
507bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
508bfa83a9eSThomas Graf  * @attrtype: type of attribute to look for
509bfa83a9eSThomas Graf  *
510bfa83a9eSThomas Graf  * Returns the first attribute which matches the specified type.
511bfa83a9eSThomas Graf  */
5126b8c92baSNelson Elhage static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
513bfa83a9eSThomas Graf 					     int hdrlen, int attrtype)
514bfa83a9eSThomas Graf {
515bfa83a9eSThomas Graf 	return nla_find(nlmsg_attrdata(nlh, hdrlen),
516bfa83a9eSThomas Graf 			nlmsg_attrlen(nlh, hdrlen), attrtype);
517bfa83a9eSThomas Graf }
518bfa83a9eSThomas Graf 
519bfa83a9eSThomas Graf /**
520bfa83a9eSThomas Graf  * nlmsg_validate - validate a netlink message including attributes
521bfa83a9eSThomas Graf  * @nlh: netlinket message header
522bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
523bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
524bfa83a9eSThomas Graf  * @policy: validation policy
525fceb6435SJohannes Berg  * @extack: extended ACK report struct
526bfa83a9eSThomas Graf  */
5273654654fSJan Engelhardt static inline int nlmsg_validate(const struct nlmsghdr *nlh,
5283654654fSJan Engelhardt 				 int hdrlen, int maxtype,
529fceb6435SJohannes Berg 				 const struct nla_policy *policy,
530fceb6435SJohannes Berg 				 struct netlink_ext_ack *extack)
531bfa83a9eSThomas Graf {
532bfa83a9eSThomas Graf 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
533bfa83a9eSThomas Graf 		return -EINVAL;
534bfa83a9eSThomas Graf 
535bfa83a9eSThomas Graf 	return nla_validate(nlmsg_attrdata(nlh, hdrlen),
536fceb6435SJohannes Berg 			    nlmsg_attrlen(nlh, hdrlen), maxtype, policy,
537fceb6435SJohannes Berg 			    extack);
538bfa83a9eSThomas Graf }
539bfa83a9eSThomas Graf 
540bfa83a9eSThomas Graf /**
54197676b6bSThomas Graf  * nlmsg_report - need to report back to application?
54297676b6bSThomas Graf  * @nlh: netlink message header
54397676b6bSThomas Graf  *
54497676b6bSThomas Graf  * Returns 1 if a report back to the application is requested.
54597676b6bSThomas Graf  */
5463a6c2b41SPatrick McHardy static inline int nlmsg_report(const struct nlmsghdr *nlh)
54797676b6bSThomas Graf {
54897676b6bSThomas Graf 	return !!(nlh->nlmsg_flags & NLM_F_ECHO);
54997676b6bSThomas Graf }
55097676b6bSThomas Graf 
55197676b6bSThomas Graf /**
552bfa83a9eSThomas Graf  * nlmsg_for_each_attr - iterate over a stream of attributes
553bfa83a9eSThomas Graf  * @pos: loop counter, set to current attribute
554bfa83a9eSThomas Graf  * @nlh: netlink message header
555bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
556bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
557bfa83a9eSThomas Graf  */
558bfa83a9eSThomas Graf #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
559bfa83a9eSThomas Graf 	nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
560bfa83a9eSThomas Graf 			  nlmsg_attrlen(nlh, hdrlen), rem)
561bfa83a9eSThomas Graf 
562bfa83a9eSThomas Graf /**
563bfa83a9eSThomas Graf  * nlmsg_put - Add a new netlink message to an skb
564bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
5652c6ba4b1SNicolas Dichtel  * @portid: netlink PORTID of requesting application
566bfa83a9eSThomas Graf  * @seq: sequence number of message
567bfa83a9eSThomas Graf  * @type: message type
568bfa83a9eSThomas Graf  * @payload: length of message payload
569bfa83a9eSThomas Graf  * @flags: message flags
570bfa83a9eSThomas Graf  *
571bfa83a9eSThomas Graf  * Returns NULL if the tailroom of the skb is insufficient to store
572bfa83a9eSThomas Graf  * the message header and payload.
573bfa83a9eSThomas Graf  */
57415e47304SEric W. Biederman static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
575bfa83a9eSThomas Graf 					 int type, int payload, int flags)
576bfa83a9eSThomas Graf {
577bfa83a9eSThomas Graf 	if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
578bfa83a9eSThomas Graf 		return NULL;
579bfa83a9eSThomas Graf 
58015e47304SEric W. Biederman 	return __nlmsg_put(skb, portid, seq, type, payload, flags);
581bfa83a9eSThomas Graf }
582bfa83a9eSThomas Graf 
583bfa83a9eSThomas Graf /**
584bfa83a9eSThomas Graf  * nlmsg_put_answer - Add a new callback based netlink message to an skb
585bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
586bfa83a9eSThomas Graf  * @cb: netlink callback
587bfa83a9eSThomas Graf  * @type: message type
588bfa83a9eSThomas Graf  * @payload: length of message payload
589bfa83a9eSThomas Graf  * @flags: message flags
590bfa83a9eSThomas Graf  *
591bfa83a9eSThomas Graf  * Returns NULL if the tailroom of the skb is insufficient to store
592bfa83a9eSThomas Graf  * the message header and payload.
593bfa83a9eSThomas Graf  */
594bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
595bfa83a9eSThomas Graf 						struct netlink_callback *cb,
596bfa83a9eSThomas Graf 						int type, int payload,
597bfa83a9eSThomas Graf 						int flags)
598bfa83a9eSThomas Graf {
59915e47304SEric W. Biederman 	return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
600bfa83a9eSThomas Graf 			 type, payload, flags);
601bfa83a9eSThomas Graf }
602bfa83a9eSThomas Graf 
603bfa83a9eSThomas Graf /**
604bfa83a9eSThomas Graf  * nlmsg_new - Allocate a new netlink message
605339bf98fSThomas Graf  * @payload: size of the message payload
606fe4944e5SThomas Graf  * @flags: the type of memory to allocate.
607bfa83a9eSThomas Graf  *
608339bf98fSThomas Graf  * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
609339bf98fSThomas Graf  * and a good default is needed.
610bfa83a9eSThomas Graf  */
611339bf98fSThomas Graf static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
612bfa83a9eSThomas Graf {
613339bf98fSThomas Graf 	return alloc_skb(nlmsg_total_size(payload), flags);
614bfa83a9eSThomas Graf }
615bfa83a9eSThomas Graf 
616bfa83a9eSThomas Graf /**
617bfa83a9eSThomas Graf  * nlmsg_end - Finalize a netlink message
618bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
619bfa83a9eSThomas Graf  * @nlh: netlink message header
620bfa83a9eSThomas Graf  *
621bfa83a9eSThomas Graf  * Corrects the netlink message header to include the appeneded
622bfa83a9eSThomas Graf  * attributes. Only necessary if attributes have been added to
623bfa83a9eSThomas Graf  * the message.
624bfa83a9eSThomas Graf  */
625053c095aSJohannes Berg static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
626bfa83a9eSThomas Graf {
62727a884dcSArnaldo Carvalho de Melo 	nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
628bfa83a9eSThomas Graf }
629bfa83a9eSThomas Graf 
630bfa83a9eSThomas Graf /**
631fe4944e5SThomas Graf  * nlmsg_get_pos - return current position in netlink message
632fe4944e5SThomas Graf  * @skb: socket buffer the message is stored in
633fe4944e5SThomas Graf  *
634fe4944e5SThomas Graf  * Returns a pointer to the current tail of the message.
635fe4944e5SThomas Graf  */
636fe4944e5SThomas Graf static inline void *nlmsg_get_pos(struct sk_buff *skb)
637fe4944e5SThomas Graf {
63827a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
639fe4944e5SThomas Graf }
640fe4944e5SThomas Graf 
641fe4944e5SThomas Graf /**
642fe4944e5SThomas Graf  * nlmsg_trim - Trim message to a mark
643fe4944e5SThomas Graf  * @skb: socket buffer the message is stored in
644fe4944e5SThomas Graf  * @mark: mark to trim to
645fe4944e5SThomas Graf  *
646bc3ed28cSThomas Graf  * Trims the message to the provided mark.
647fe4944e5SThomas Graf  */
648bc3ed28cSThomas Graf static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
649fe4944e5SThomas Graf {
650149118d8SThomas Graf 	if (mark) {
651149118d8SThomas Graf 		WARN_ON((unsigned char *) mark < skb->data);
652fe4944e5SThomas Graf 		skb_trim(skb, (unsigned char *) mark - skb->data);
653fe4944e5SThomas Graf 	}
654149118d8SThomas Graf }
655fe4944e5SThomas Graf 
656fe4944e5SThomas Graf /**
657bfa83a9eSThomas Graf  * nlmsg_cancel - Cancel construction of a netlink message
658bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
659bfa83a9eSThomas Graf  * @nlh: netlink message header
660bfa83a9eSThomas Graf  *
661bfa83a9eSThomas Graf  * Removes the complete netlink message including all
662bc3ed28cSThomas Graf  * attributes from the socket buffer again.
663bfa83a9eSThomas Graf  */
664bc3ed28cSThomas Graf static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
665bfa83a9eSThomas Graf {
666bc3ed28cSThomas Graf 	nlmsg_trim(skb, nlh);
667bfa83a9eSThomas Graf }
668bfa83a9eSThomas Graf 
669bfa83a9eSThomas Graf /**
670bfa83a9eSThomas Graf  * nlmsg_free - free a netlink message
671bfa83a9eSThomas Graf  * @skb: socket buffer of netlink message
672bfa83a9eSThomas Graf  */
673bfa83a9eSThomas Graf static inline void nlmsg_free(struct sk_buff *skb)
674bfa83a9eSThomas Graf {
675bfa83a9eSThomas Graf 	kfree_skb(skb);
676bfa83a9eSThomas Graf }
677bfa83a9eSThomas Graf 
678bfa83a9eSThomas Graf /**
679bfa83a9eSThomas Graf  * nlmsg_multicast - multicast a netlink message
680bfa83a9eSThomas Graf  * @sk: netlink socket to spread messages to
681bfa83a9eSThomas Graf  * @skb: netlink message as socket buffer
68215e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
683bfa83a9eSThomas Graf  * @group: multicast group id
684d387f6adSThomas Graf  * @flags: allocation flags
685bfa83a9eSThomas Graf  */
686bfa83a9eSThomas Graf static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
68715e47304SEric W. Biederman 				  u32 portid, unsigned int group, gfp_t flags)
688bfa83a9eSThomas Graf {
689bfa83a9eSThomas Graf 	int err;
690bfa83a9eSThomas Graf 
691bfa83a9eSThomas Graf 	NETLINK_CB(skb).dst_group = group;
692bfa83a9eSThomas Graf 
69315e47304SEric W. Biederman 	err = netlink_broadcast(sk, skb, portid, group, flags);
694bfa83a9eSThomas Graf 	if (err > 0)
695bfa83a9eSThomas Graf 		err = 0;
696bfa83a9eSThomas Graf 
697bfa83a9eSThomas Graf 	return err;
698bfa83a9eSThomas Graf }
699bfa83a9eSThomas Graf 
700bfa83a9eSThomas Graf /**
701bfa83a9eSThomas Graf  * nlmsg_unicast - unicast a netlink message
702bfa83a9eSThomas Graf  * @sk: netlink socket to spread message to
703bfa83a9eSThomas Graf  * @skb: netlink message as socket buffer
70415e47304SEric W. Biederman  * @portid: netlink portid of the destination socket
705bfa83a9eSThomas Graf  */
70615e47304SEric W. Biederman static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
707bfa83a9eSThomas Graf {
708bfa83a9eSThomas Graf 	int err;
709bfa83a9eSThomas Graf 
71015e47304SEric W. Biederman 	err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
711bfa83a9eSThomas Graf 	if (err > 0)
712bfa83a9eSThomas Graf 		err = 0;
713bfa83a9eSThomas Graf 
714bfa83a9eSThomas Graf 	return err;
715bfa83a9eSThomas Graf }
716bfa83a9eSThomas Graf 
717bfa83a9eSThomas Graf /**
718bfa83a9eSThomas Graf  * nlmsg_for_each_msg - iterate over a stream of messages
719bfa83a9eSThomas Graf  * @pos: loop counter, set to current message
720bfa83a9eSThomas Graf  * @head: head of message stream
721bfa83a9eSThomas Graf  * @len: length of message stream
722bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
723bfa83a9eSThomas Graf  */
724bfa83a9eSThomas Graf #define nlmsg_for_each_msg(pos, head, len, rem) \
725bfa83a9eSThomas Graf 	for (pos = head, rem = len; \
726bfa83a9eSThomas Graf 	     nlmsg_ok(pos, rem); \
727bfa83a9eSThomas Graf 	     pos = nlmsg_next(pos, &(rem)))
728bfa83a9eSThomas Graf 
729670dc283SJohannes Berg /**
730670dc283SJohannes Berg  * nl_dump_check_consistent - check if sequence is consistent and advertise if not
731670dc283SJohannes Berg  * @cb: netlink callback structure that stores the sequence number
732670dc283SJohannes Berg  * @nlh: netlink message header to write the flag to
733670dc283SJohannes Berg  *
734670dc283SJohannes Berg  * This function checks if the sequence (generation) number changed during dump
735670dc283SJohannes Berg  * and if it did, advertises it in the netlink message header.
736670dc283SJohannes Berg  *
737670dc283SJohannes Berg  * The correct way to use it is to set cb->seq to the generation counter when
738670dc283SJohannes Berg  * all locks for dumping have been acquired, and then call this function for
739670dc283SJohannes Berg  * each message that is generated.
740670dc283SJohannes Berg  *
741670dc283SJohannes Berg  * Note that due to initialisation concerns, 0 is an invalid sequence number
742670dc283SJohannes Berg  * and must not be used by code that uses this functionality.
743670dc283SJohannes Berg  */
744670dc283SJohannes Berg static inline void
745670dc283SJohannes Berg nl_dump_check_consistent(struct netlink_callback *cb,
746670dc283SJohannes Berg 			 struct nlmsghdr *nlh)
747670dc283SJohannes Berg {
748670dc283SJohannes Berg 	if (cb->prev_seq && cb->seq != cb->prev_seq)
749670dc283SJohannes Berg 		nlh->nlmsg_flags |= NLM_F_DUMP_INTR;
750670dc283SJohannes Berg 	cb->prev_seq = cb->seq;
751670dc283SJohannes Berg }
752670dc283SJohannes Berg 
753bfa83a9eSThomas Graf /**************************************************************************
754bfa83a9eSThomas Graf  * Netlink Attributes
755bfa83a9eSThomas Graf  **************************************************************************/
756bfa83a9eSThomas Graf 
757bfa83a9eSThomas Graf /**
758bfa83a9eSThomas Graf  * nla_attr_size - length of attribute not including padding
759bfa83a9eSThomas Graf  * @payload: length of payload
760bfa83a9eSThomas Graf  */
761bfa83a9eSThomas Graf static inline int nla_attr_size(int payload)
762bfa83a9eSThomas Graf {
763bfa83a9eSThomas Graf 	return NLA_HDRLEN + payload;
764bfa83a9eSThomas Graf }
765bfa83a9eSThomas Graf 
766bfa83a9eSThomas Graf /**
767bfa83a9eSThomas Graf  * nla_total_size - total length of attribute including padding
768bfa83a9eSThomas Graf  * @payload: length of payload
769bfa83a9eSThomas Graf  */
770bfa83a9eSThomas Graf static inline int nla_total_size(int payload)
771bfa83a9eSThomas Graf {
772bfa83a9eSThomas Graf 	return NLA_ALIGN(nla_attr_size(payload));
773bfa83a9eSThomas Graf }
774bfa83a9eSThomas Graf 
775bfa83a9eSThomas Graf /**
776bfa83a9eSThomas Graf  * nla_padlen - length of padding at the tail of attribute
777bfa83a9eSThomas Graf  * @payload: length of payload
778bfa83a9eSThomas Graf  */
779bfa83a9eSThomas Graf static inline int nla_padlen(int payload)
780bfa83a9eSThomas Graf {
781bfa83a9eSThomas Graf 	return nla_total_size(payload) - nla_attr_size(payload);
782bfa83a9eSThomas Graf }
783bfa83a9eSThomas Graf 
784bfa83a9eSThomas Graf /**
7858f4c1f9bSThomas Graf  * nla_type - attribute type
7868f4c1f9bSThomas Graf  * @nla: netlink attribute
7878f4c1f9bSThomas Graf  */
7888f4c1f9bSThomas Graf static inline int nla_type(const struct nlattr *nla)
7898f4c1f9bSThomas Graf {
7908f4c1f9bSThomas Graf 	return nla->nla_type & NLA_TYPE_MASK;
7918f4c1f9bSThomas Graf }
7928f4c1f9bSThomas Graf 
7938f4c1f9bSThomas Graf /**
794bfa83a9eSThomas Graf  * nla_data - head of payload
795bfa83a9eSThomas Graf  * @nla: netlink attribute
796bfa83a9eSThomas Graf  */
797bfa83a9eSThomas Graf static inline void *nla_data(const struct nlattr *nla)
798bfa83a9eSThomas Graf {
799bfa83a9eSThomas Graf 	return (char *) nla + NLA_HDRLEN;
800bfa83a9eSThomas Graf }
801bfa83a9eSThomas Graf 
802bfa83a9eSThomas Graf /**
803bfa83a9eSThomas Graf  * nla_len - length of payload
804bfa83a9eSThomas Graf  * @nla: netlink attribute
805bfa83a9eSThomas Graf  */
806bfa83a9eSThomas Graf static inline int nla_len(const struct nlattr *nla)
807bfa83a9eSThomas Graf {
808bfa83a9eSThomas Graf 	return nla->nla_len - NLA_HDRLEN;
809bfa83a9eSThomas Graf }
810bfa83a9eSThomas Graf 
811bfa83a9eSThomas Graf /**
812bfa83a9eSThomas Graf  * nla_ok - check if the netlink attribute fits into the remaining bytes
813bfa83a9eSThomas Graf  * @nla: netlink attribute
814bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in attribute stream
815bfa83a9eSThomas Graf  */
816bfa83a9eSThomas Graf static inline int nla_ok(const struct nlattr *nla, int remaining)
817bfa83a9eSThomas Graf {
8183e1ed981SAlexey Dobriyan 	return remaining >= (int) sizeof(*nla) &&
8193e1ed981SAlexey Dobriyan 	       nla->nla_len >= sizeof(*nla) &&
820bfa83a9eSThomas Graf 	       nla->nla_len <= remaining;
821bfa83a9eSThomas Graf }
822bfa83a9eSThomas Graf 
823bfa83a9eSThomas Graf /**
824d1ec3b77SPierre Ynard  * nla_next - next netlink attribute in attribute stream
825bfa83a9eSThomas Graf  * @nla: netlink attribute
826bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in attribute stream
827bfa83a9eSThomas Graf  *
828bfa83a9eSThomas Graf  * Returns the next netlink attribute in the attribute stream and
829bfa83a9eSThomas Graf  * decrements remaining by the size of the current attribute.
830bfa83a9eSThomas Graf  */
831bfa83a9eSThomas Graf static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
832bfa83a9eSThomas Graf {
8333b2c75d3SAlexey Dobriyan 	unsigned int totlen = NLA_ALIGN(nla->nla_len);
834bfa83a9eSThomas Graf 
835bfa83a9eSThomas Graf 	*remaining -= totlen;
836bfa83a9eSThomas Graf 	return (struct nlattr *) ((char *) nla + totlen);
837bfa83a9eSThomas Graf }
838bfa83a9eSThomas Graf 
839bfa83a9eSThomas Graf /**
840fe4944e5SThomas Graf  * nla_find_nested - find attribute in a set of nested attributes
841fe4944e5SThomas Graf  * @nla: attribute containing the nested attributes
842fe4944e5SThomas Graf  * @attrtype: type of attribute to look for
843fe4944e5SThomas Graf  *
844fe4944e5SThomas Graf  * Returns the first attribute which matches the specified type.
845fe4944e5SThomas Graf  */
8463654654fSJan Engelhardt static inline struct nlattr *
8473654654fSJan Engelhardt nla_find_nested(const struct nlattr *nla, int attrtype)
848fe4944e5SThomas Graf {
849fe4944e5SThomas Graf 	return nla_find(nla_data(nla), nla_len(nla), attrtype);
850fe4944e5SThomas Graf }
851fe4944e5SThomas Graf 
852fe4944e5SThomas Graf /**
853bfa83a9eSThomas Graf  * nla_parse_nested - parse nested attributes
854bfa83a9eSThomas Graf  * @tb: destination array with maxtype+1 elements
855bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
856bfa83a9eSThomas Graf  * @nla: attribute containing the nested attributes
857bfa83a9eSThomas Graf  * @policy: validation policy
858fceb6435SJohannes Berg  * @extack: extended ACK report struct
859bfa83a9eSThomas Graf  *
860bfa83a9eSThomas Graf  * See nla_parse()
861bfa83a9eSThomas Graf  */
862bfa83a9eSThomas Graf static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
863b057efd4SPatrick McHardy 				   const struct nlattr *nla,
864fceb6435SJohannes Berg 				   const struct nla_policy *policy,
865fceb6435SJohannes Berg 				   struct netlink_ext_ack *extack)
866bfa83a9eSThomas Graf {
867fceb6435SJohannes Berg 	return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
868fceb6435SJohannes Berg 			 extack);
869bfa83a9eSThomas Graf }
8701092cb21SPatrick McHardy 
8711092cb21SPatrick McHardy /**
872d1ec3b77SPierre Ynard  * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
873bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
874bfa83a9eSThomas Graf  * @attrtype: attribute type
875bfa83a9eSThomas Graf  * @value: numeric value
876bfa83a9eSThomas Graf  */
877bfa83a9eSThomas Graf static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
878bfa83a9eSThomas Graf {
879b4391db4SArnd Bergmann 	/* temporary variables to work around GCC PR81715 with asan-stack=1 */
880b4391db4SArnd Bergmann 	u8 tmp = value;
881b4391db4SArnd Bergmann 
882b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(u8), &tmp);
883bfa83a9eSThomas Graf }
884bfa83a9eSThomas Graf 
885bfa83a9eSThomas Graf /**
886bfa83a9eSThomas Graf  * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
887bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
888bfa83a9eSThomas Graf  * @attrtype: attribute type
889bfa83a9eSThomas Graf  * @value: numeric value
890bfa83a9eSThomas Graf  */
891bfa83a9eSThomas Graf static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
892bfa83a9eSThomas Graf {
893b4391db4SArnd Bergmann 	u16 tmp = value;
894b4391db4SArnd Bergmann 
895b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(u16), &tmp);
896bfa83a9eSThomas Graf }
897bfa83a9eSThomas Graf 
898bfa83a9eSThomas Graf /**
899569a8fc3SDavid S. Miller  * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer
900569a8fc3SDavid S. Miller  * @skb: socket buffer to add attribute to
901569a8fc3SDavid S. Miller  * @attrtype: attribute type
902569a8fc3SDavid S. Miller  * @value: numeric value
903569a8fc3SDavid S. Miller  */
904569a8fc3SDavid S. Miller static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
905569a8fc3SDavid S. Miller {
906b4391db4SArnd Bergmann 	__be16 tmp = value;
907b4391db4SArnd Bergmann 
908b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__be16), &tmp);
909569a8fc3SDavid S. Miller }
910569a8fc3SDavid S. Miller 
911569a8fc3SDavid S. Miller /**
9126c1dd3b6SDavid S. Miller  * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer
9136c1dd3b6SDavid S. Miller  * @skb: socket buffer to add attribute to
9146c1dd3b6SDavid S. Miller  * @attrtype: attribute type
9156c1dd3b6SDavid S. Miller  * @value: numeric value
9166c1dd3b6SDavid S. Miller  */
9176c1dd3b6SDavid S. Miller static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
9186c1dd3b6SDavid S. Miller {
919b4391db4SArnd Bergmann 	__be16 tmp = value;
920b4391db4SArnd Bergmann 
921b4391db4SArnd Bergmann 	return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
9226c1dd3b6SDavid S. Miller }
9236c1dd3b6SDavid S. Miller 
9246c1dd3b6SDavid S. Miller /**
92524c410dcSDavid S. Miller  * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer
92624c410dcSDavid S. Miller  * @skb: socket buffer to add attribute to
92724c410dcSDavid S. Miller  * @attrtype: attribute type
92824c410dcSDavid S. Miller  * @value: numeric value
92924c410dcSDavid S. Miller  */
93024c410dcSDavid S. Miller static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
93124c410dcSDavid S. Miller {
932b4391db4SArnd Bergmann 	__le16 tmp = value;
933b4391db4SArnd Bergmann 
934b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__le16), &tmp);
93524c410dcSDavid S. Miller }
93624c410dcSDavid S. Miller 
93724c410dcSDavid S. Miller /**
938bfa83a9eSThomas Graf  * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
939bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
940bfa83a9eSThomas Graf  * @attrtype: attribute type
941bfa83a9eSThomas Graf  * @value: numeric value
942bfa83a9eSThomas Graf  */
943bfa83a9eSThomas Graf static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
944bfa83a9eSThomas Graf {
945b4391db4SArnd Bergmann 	u32 tmp = value;
946b4391db4SArnd Bergmann 
947b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(u32), &tmp);
948bfa83a9eSThomas Graf }
949bfa83a9eSThomas Graf 
950bfa83a9eSThomas Graf /**
951569a8fc3SDavid S. Miller  * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer
952569a8fc3SDavid S. Miller  * @skb: socket buffer to add attribute to
953569a8fc3SDavid S. Miller  * @attrtype: attribute type
954569a8fc3SDavid S. Miller  * @value: numeric value
955569a8fc3SDavid S. Miller  */
956569a8fc3SDavid S. Miller static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
957569a8fc3SDavid S. Miller {
958b4391db4SArnd Bergmann 	__be32 tmp = value;
959b4391db4SArnd Bergmann 
960b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__be32), &tmp);
961569a8fc3SDavid S. Miller }
962569a8fc3SDavid S. Miller 
963569a8fc3SDavid S. Miller /**
9646c1dd3b6SDavid S. Miller  * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer
9656c1dd3b6SDavid S. Miller  * @skb: socket buffer to add attribute to
9666c1dd3b6SDavid S. Miller  * @attrtype: attribute type
9676c1dd3b6SDavid S. Miller  * @value: numeric value
9686c1dd3b6SDavid S. Miller  */
9696c1dd3b6SDavid S. Miller static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
9706c1dd3b6SDavid S. Miller {
971b4391db4SArnd Bergmann 	__be32 tmp = value;
972b4391db4SArnd Bergmann 
973b4391db4SArnd Bergmann 	return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
9746c1dd3b6SDavid S. Miller }
9756c1dd3b6SDavid S. Miller 
9766c1dd3b6SDavid S. Miller /**
97724c410dcSDavid S. Miller  * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer
97824c410dcSDavid S. Miller  * @skb: socket buffer to add attribute to
97924c410dcSDavid S. Miller  * @attrtype: attribute type
98024c410dcSDavid S. Miller  * @value: numeric value
98124c410dcSDavid S. Miller  */
98224c410dcSDavid S. Miller static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value)
98324c410dcSDavid S. Miller {
984b4391db4SArnd Bergmann 	__le32 tmp = value;
985b4391db4SArnd Bergmann 
986b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__le32), &tmp);
98724c410dcSDavid S. Miller }
98824c410dcSDavid S. Miller 
98924c410dcSDavid S. Miller /**
99073520786SNicolas Dichtel  * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it
99173520786SNicolas Dichtel  * @skb: socket buffer to add attribute to
99273520786SNicolas Dichtel  * @attrtype: attribute type
99373520786SNicolas Dichtel  * @value: numeric value
99473520786SNicolas Dichtel  * @padattr: attribute type for the padding
99573520786SNicolas Dichtel  */
99673520786SNicolas Dichtel static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype,
99773520786SNicolas Dichtel 				    u64 value, int padattr)
99873520786SNicolas Dichtel {
999b4391db4SArnd Bergmann 	u64 tmp = value;
1000b4391db4SArnd Bergmann 
1001b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
100273520786SNicolas Dichtel }
100373520786SNicolas Dichtel 
100473520786SNicolas Dichtel /**
1005b46f6dedSNicolas Dichtel  * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it
1006569a8fc3SDavid S. Miller  * @skb: socket buffer to add attribute to
1007569a8fc3SDavid S. Miller  * @attrtype: attribute type
1008569a8fc3SDavid S. Miller  * @value: numeric value
1009b46f6dedSNicolas Dichtel  * @padattr: attribute type for the padding
1010569a8fc3SDavid S. Miller  */
1011b46f6dedSNicolas Dichtel static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value,
1012b46f6dedSNicolas Dichtel 			       int padattr)
1013b46f6dedSNicolas Dichtel {
1014b4391db4SArnd Bergmann 	__be64 tmp = value;
1015b4391db4SArnd Bergmann 
1016b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr);
1017b46f6dedSNicolas Dichtel }
1018b46f6dedSNicolas Dichtel 
1019569a8fc3SDavid S. Miller /**
1020e9bbe898SNicolas Dichtel  * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it
10216c1dd3b6SDavid S. Miller  * @skb: socket buffer to add attribute to
10226c1dd3b6SDavid S. Miller  * @attrtype: attribute type
10236c1dd3b6SDavid S. Miller  * @value: numeric value
1024e9bbe898SNicolas Dichtel  * @padattr: attribute type for the padding
10256c1dd3b6SDavid S. Miller  */
1026e9bbe898SNicolas Dichtel static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value,
1027e9bbe898SNicolas Dichtel 				int padattr)
10286c1dd3b6SDavid S. Miller {
1029b4391db4SArnd Bergmann 	__be64 tmp = value;
1030b4391db4SArnd Bergmann 
1031b4391db4SArnd Bergmann 	return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp,
1032e9bbe898SNicolas Dichtel 			    padattr);
10336c1dd3b6SDavid S. Miller }
10346c1dd3b6SDavid S. Miller 
10356c1dd3b6SDavid S. Miller /**
1036e7479122SNicolas Dichtel  * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it
103724c410dcSDavid S. Miller  * @skb: socket buffer to add attribute to
103824c410dcSDavid S. Miller  * @attrtype: attribute type
103924c410dcSDavid S. Miller  * @value: numeric value
1040e7479122SNicolas Dichtel  * @padattr: attribute type for the padding
104124c410dcSDavid S. Miller  */
1042e7479122SNicolas Dichtel static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value,
1043e7479122SNicolas Dichtel 			       int padattr)
104424c410dcSDavid S. Miller {
1045b4391db4SArnd Bergmann 	__le64 tmp = value;
1046b4391db4SArnd Bergmann 
1047b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr);
104824c410dcSDavid S. Miller }
104924c410dcSDavid S. Miller 
105024c410dcSDavid S. Miller /**
10514778e0beSJiri Pirko  * nla_put_s8 - Add a s8 netlink attribute to a socket buffer
10524778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
10534778e0beSJiri Pirko  * @attrtype: attribute type
10544778e0beSJiri Pirko  * @value: numeric value
10554778e0beSJiri Pirko  */
10564778e0beSJiri Pirko static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
10574778e0beSJiri Pirko {
1058b4391db4SArnd Bergmann 	s8 tmp = value;
1059b4391db4SArnd Bergmann 
1060b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(s8), &tmp);
10614778e0beSJiri Pirko }
10624778e0beSJiri Pirko 
10634778e0beSJiri Pirko /**
10644778e0beSJiri Pirko  * nla_put_s16 - Add a s16 netlink attribute to a socket buffer
10654778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
10664778e0beSJiri Pirko  * @attrtype: attribute type
10674778e0beSJiri Pirko  * @value: numeric value
10684778e0beSJiri Pirko  */
10694778e0beSJiri Pirko static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
10704778e0beSJiri Pirko {
1071b4391db4SArnd Bergmann 	s16 tmp = value;
1072b4391db4SArnd Bergmann 
1073b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(s16), &tmp);
10744778e0beSJiri Pirko }
10754778e0beSJiri Pirko 
10764778e0beSJiri Pirko /**
10774778e0beSJiri Pirko  * nla_put_s32 - Add a s32 netlink attribute to a socket buffer
10784778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
10794778e0beSJiri Pirko  * @attrtype: attribute type
10804778e0beSJiri Pirko  * @value: numeric value
10814778e0beSJiri Pirko  */
10824778e0beSJiri Pirko static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
10834778e0beSJiri Pirko {
1084b4391db4SArnd Bergmann 	s32 tmp = value;
1085b4391db4SArnd Bergmann 
1086b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(s32), &tmp);
10874778e0beSJiri Pirko }
10884778e0beSJiri Pirko 
10894778e0beSJiri Pirko /**
1090756a2f59SNicolas Dichtel  * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it
10914778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
10924778e0beSJiri Pirko  * @attrtype: attribute type
10934778e0beSJiri Pirko  * @value: numeric value
1094756a2f59SNicolas Dichtel  * @padattr: attribute type for the padding
10954778e0beSJiri Pirko  */
1096756a2f59SNicolas Dichtel static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value,
1097756a2f59SNicolas Dichtel 			      int padattr)
10984778e0beSJiri Pirko {
1099b4391db4SArnd Bergmann 	s64 tmp = value;
1100b4391db4SArnd Bergmann 
1101b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr);
11024778e0beSJiri Pirko }
11034778e0beSJiri Pirko 
11044778e0beSJiri Pirko /**
1105bfa83a9eSThomas Graf  * nla_put_string - Add a string netlink attribute to a socket buffer
1106bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1107bfa83a9eSThomas Graf  * @attrtype: attribute type
1108bfa83a9eSThomas Graf  * @str: NUL terminated string
1109bfa83a9eSThomas Graf  */
1110bfa83a9eSThomas Graf static inline int nla_put_string(struct sk_buff *skb, int attrtype,
1111bfa83a9eSThomas Graf 				 const char *str)
1112bfa83a9eSThomas Graf {
1113bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, strlen(str) + 1, str);
1114bfa83a9eSThomas Graf }
1115bfa83a9eSThomas Graf 
1116bfa83a9eSThomas Graf /**
1117bfa83a9eSThomas Graf  * nla_put_flag - Add a flag netlink attribute to a socket buffer
1118bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1119bfa83a9eSThomas Graf  * @attrtype: attribute type
1120bfa83a9eSThomas Graf  */
1121bfa83a9eSThomas Graf static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
1122bfa83a9eSThomas Graf {
1123bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, 0, NULL);
1124bfa83a9eSThomas Graf }
1125bfa83a9eSThomas Graf 
1126bfa83a9eSThomas Graf /**
11272175d87cSNicolas Dichtel  * nla_put_msecs - Add a msecs netlink attribute to a skb and align it
1128bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1129bfa83a9eSThomas Graf  * @attrtype: attribute type
1130d87de1f3SMark Rustad  * @njiffies: number of jiffies to convert to msecs
11312175d87cSNicolas Dichtel  * @padattr: attribute type for the padding
1132bfa83a9eSThomas Graf  */
1133bfa83a9eSThomas Graf static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
11342175d87cSNicolas Dichtel 				unsigned long njiffies, int padattr)
1135bfa83a9eSThomas Graf {
1136d87de1f3SMark Rustad 	u64 tmp = jiffies_to_msecs(njiffies);
11372175d87cSNicolas Dichtel 
11382175d87cSNicolas Dichtel 	return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
1139bfa83a9eSThomas Graf }
1140bfa83a9eSThomas Graf 
1141bfa83a9eSThomas Graf /**
1142930345eaSJiri Benc  * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket
1143930345eaSJiri Benc  * buffer
1144930345eaSJiri Benc  * @skb: socket buffer to add attribute to
1145930345eaSJiri Benc  * @attrtype: attribute type
1146930345eaSJiri Benc  * @addr: IPv4 address
1147930345eaSJiri Benc  */
1148930345eaSJiri Benc static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
1149930345eaSJiri Benc 				  __be32 addr)
1150930345eaSJiri Benc {
1151b4391db4SArnd Bergmann 	__be32 tmp = addr;
1152b4391db4SArnd Bergmann 
1153b4391db4SArnd Bergmann 	return nla_put_be32(skb, attrtype, tmp);
1154930345eaSJiri Benc }
1155930345eaSJiri Benc 
1156930345eaSJiri Benc /**
1157930345eaSJiri Benc  * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket
1158930345eaSJiri Benc  * buffer
1159930345eaSJiri Benc  * @skb: socket buffer to add attribute to
1160930345eaSJiri Benc  * @attrtype: attribute type
1161930345eaSJiri Benc  * @addr: IPv6 address
1162930345eaSJiri Benc  */
1163930345eaSJiri Benc static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype,
1164930345eaSJiri Benc 				   const struct in6_addr *addr)
1165930345eaSJiri Benc {
1166930345eaSJiri Benc 	return nla_put(skb, attrtype, sizeof(*addr), addr);
1167930345eaSJiri Benc }
1168930345eaSJiri Benc 
1169930345eaSJiri Benc /**
1170bfa83a9eSThomas Graf  * nla_get_u32 - return payload of u32 attribute
1171bfa83a9eSThomas Graf  * @nla: u32 netlink attribute
1172bfa83a9eSThomas Graf  */
1173b057efd4SPatrick McHardy static inline u32 nla_get_u32(const struct nlattr *nla)
1174bfa83a9eSThomas Graf {
1175bfa83a9eSThomas Graf 	return *(u32 *) nla_data(nla);
1176bfa83a9eSThomas Graf }
1177bfa83a9eSThomas Graf 
1178bfa83a9eSThomas Graf /**
117900012e5bSAl Viro  * nla_get_be32 - return payload of __be32 attribute
118000012e5bSAl Viro  * @nla: __be32 netlink attribute
118100012e5bSAl Viro  */
1182b057efd4SPatrick McHardy static inline __be32 nla_get_be32(const struct nlattr *nla)
118300012e5bSAl Viro {
118400012e5bSAl Viro 	return *(__be32 *) nla_data(nla);
118500012e5bSAl Viro }
118600012e5bSAl Viro 
118700012e5bSAl Viro /**
1188c648a013SAlexander Aring  * nla_get_le32 - return payload of __le32 attribute
1189c648a013SAlexander Aring  * @nla: __le32 netlink attribute
1190c648a013SAlexander Aring  */
1191c648a013SAlexander Aring static inline __le32 nla_get_le32(const struct nlattr *nla)
1192c648a013SAlexander Aring {
1193c648a013SAlexander Aring 	return *(__le32 *) nla_data(nla);
1194c648a013SAlexander Aring }
1195c648a013SAlexander Aring 
1196c648a013SAlexander Aring /**
1197bfa83a9eSThomas Graf  * nla_get_u16 - return payload of u16 attribute
1198bfa83a9eSThomas Graf  * @nla: u16 netlink attribute
1199bfa83a9eSThomas Graf  */
1200b057efd4SPatrick McHardy static inline u16 nla_get_u16(const struct nlattr *nla)
1201bfa83a9eSThomas Graf {
1202bfa83a9eSThomas Graf 	return *(u16 *) nla_data(nla);
1203bfa83a9eSThomas Graf }
1204bfa83a9eSThomas Graf 
1205bfa83a9eSThomas Graf /**
1206838965baSPatrick McHardy  * nla_get_be16 - return payload of __be16 attribute
1207838965baSPatrick McHardy  * @nla: __be16 netlink attribute
1208838965baSPatrick McHardy  */
1209b057efd4SPatrick McHardy static inline __be16 nla_get_be16(const struct nlattr *nla)
1210838965baSPatrick McHardy {
1211838965baSPatrick McHardy 	return *(__be16 *) nla_data(nla);
1212838965baSPatrick McHardy }
1213838965baSPatrick McHardy 
1214838965baSPatrick McHardy /**
12154a89c256SThomas Graf  * nla_get_le16 - return payload of __le16 attribute
12164a89c256SThomas Graf  * @nla: __le16 netlink attribute
12174a89c256SThomas Graf  */
1218b057efd4SPatrick McHardy static inline __le16 nla_get_le16(const struct nlattr *nla)
12194a89c256SThomas Graf {
12204a89c256SThomas Graf 	return *(__le16 *) nla_data(nla);
12214a89c256SThomas Graf }
12224a89c256SThomas Graf 
12234a89c256SThomas Graf /**
1224bfa83a9eSThomas Graf  * nla_get_u8 - return payload of u8 attribute
1225bfa83a9eSThomas Graf  * @nla: u8 netlink attribute
1226bfa83a9eSThomas Graf  */
1227b057efd4SPatrick McHardy static inline u8 nla_get_u8(const struct nlattr *nla)
1228bfa83a9eSThomas Graf {
1229bfa83a9eSThomas Graf 	return *(u8 *) nla_data(nla);
1230bfa83a9eSThomas Graf }
1231bfa83a9eSThomas Graf 
1232bfa83a9eSThomas Graf /**
1233bfa83a9eSThomas Graf  * nla_get_u64 - return payload of u64 attribute
1234bfa83a9eSThomas Graf  * @nla: u64 netlink attribute
1235bfa83a9eSThomas Graf  */
1236b057efd4SPatrick McHardy static inline u64 nla_get_u64(const struct nlattr *nla)
1237bfa83a9eSThomas Graf {
1238bfa83a9eSThomas Graf 	u64 tmp;
1239bfa83a9eSThomas Graf 
1240bfa83a9eSThomas Graf 	nla_memcpy(&tmp, nla, sizeof(tmp));
1241bfa83a9eSThomas Graf 
1242bfa83a9eSThomas Graf 	return tmp;
1243bfa83a9eSThomas Graf }
1244bfa83a9eSThomas Graf 
1245bfa83a9eSThomas Graf /**
1246a17c8598SPablo Neira Ayuso  * nla_get_be64 - return payload of __be64 attribute
1247a17c8598SPablo Neira Ayuso  * @nla: __be64 netlink attribute
1248a17c8598SPablo Neira Ayuso  */
1249a17c8598SPablo Neira Ayuso static inline __be64 nla_get_be64(const struct nlattr *nla)
1250a17c8598SPablo Neira Ayuso {
1251f5d410f2SPablo Neira Ayuso 	__be64 tmp;
1252f5d410f2SPablo Neira Ayuso 
1253f5d410f2SPablo Neira Ayuso 	nla_memcpy(&tmp, nla, sizeof(tmp));
1254f5d410f2SPablo Neira Ayuso 
1255f5d410f2SPablo Neira Ayuso 	return tmp;
1256a17c8598SPablo Neira Ayuso }
1257a17c8598SPablo Neira Ayuso 
1258a17c8598SPablo Neira Ayuso /**
1259c648a013SAlexander Aring  * nla_get_le64 - return payload of __le64 attribute
1260c648a013SAlexander Aring  * @nla: __le64 netlink attribute
1261c648a013SAlexander Aring  */
1262c648a013SAlexander Aring static inline __le64 nla_get_le64(const struct nlattr *nla)
1263c648a013SAlexander Aring {
1264c648a013SAlexander Aring 	return *(__le64 *) nla_data(nla);
1265c648a013SAlexander Aring }
1266c648a013SAlexander Aring 
1267c648a013SAlexander Aring /**
12684778e0beSJiri Pirko  * nla_get_s32 - return payload of s32 attribute
12694778e0beSJiri Pirko  * @nla: s32 netlink attribute
12704778e0beSJiri Pirko  */
12714778e0beSJiri Pirko static inline s32 nla_get_s32(const struct nlattr *nla)
12724778e0beSJiri Pirko {
12734778e0beSJiri Pirko 	return *(s32 *) nla_data(nla);
12744778e0beSJiri Pirko }
12754778e0beSJiri Pirko 
12764778e0beSJiri Pirko /**
12774778e0beSJiri Pirko  * nla_get_s16 - return payload of s16 attribute
12784778e0beSJiri Pirko  * @nla: s16 netlink attribute
12794778e0beSJiri Pirko  */
12804778e0beSJiri Pirko static inline s16 nla_get_s16(const struct nlattr *nla)
12814778e0beSJiri Pirko {
12824778e0beSJiri Pirko 	return *(s16 *) nla_data(nla);
12834778e0beSJiri Pirko }
12844778e0beSJiri Pirko 
12854778e0beSJiri Pirko /**
12864778e0beSJiri Pirko  * nla_get_s8 - return payload of s8 attribute
12874778e0beSJiri Pirko  * @nla: s8 netlink attribute
12884778e0beSJiri Pirko  */
12894778e0beSJiri Pirko static inline s8 nla_get_s8(const struct nlattr *nla)
12904778e0beSJiri Pirko {
12914778e0beSJiri Pirko 	return *(s8 *) nla_data(nla);
12924778e0beSJiri Pirko }
12934778e0beSJiri Pirko 
12944778e0beSJiri Pirko /**
12954778e0beSJiri Pirko  * nla_get_s64 - return payload of s64 attribute
12964778e0beSJiri Pirko  * @nla: s64 netlink attribute
12974778e0beSJiri Pirko  */
12984778e0beSJiri Pirko static inline s64 nla_get_s64(const struct nlattr *nla)
12994778e0beSJiri Pirko {
13004778e0beSJiri Pirko 	s64 tmp;
13014778e0beSJiri Pirko 
13024778e0beSJiri Pirko 	nla_memcpy(&tmp, nla, sizeof(tmp));
13034778e0beSJiri Pirko 
13044778e0beSJiri Pirko 	return tmp;
13054778e0beSJiri Pirko }
13064778e0beSJiri Pirko 
13074778e0beSJiri Pirko /**
1308bfa83a9eSThomas Graf  * nla_get_flag - return payload of flag attribute
1309bfa83a9eSThomas Graf  * @nla: flag netlink attribute
1310bfa83a9eSThomas Graf  */
1311b057efd4SPatrick McHardy static inline int nla_get_flag(const struct nlattr *nla)
1312bfa83a9eSThomas Graf {
1313bfa83a9eSThomas Graf 	return !!nla;
1314bfa83a9eSThomas Graf }
1315bfa83a9eSThomas Graf 
1316bfa83a9eSThomas Graf /**
1317bfa83a9eSThomas Graf  * nla_get_msecs - return payload of msecs attribute
1318bfa83a9eSThomas Graf  * @nla: msecs netlink attribute
1319bfa83a9eSThomas Graf  *
1320bfa83a9eSThomas Graf  * Returns the number of milliseconds in jiffies.
1321bfa83a9eSThomas Graf  */
1322b057efd4SPatrick McHardy static inline unsigned long nla_get_msecs(const struct nlattr *nla)
1323bfa83a9eSThomas Graf {
1324bfa83a9eSThomas Graf 	u64 msecs = nla_get_u64(nla);
1325bfa83a9eSThomas Graf 
1326bfa83a9eSThomas Graf 	return msecs_to_jiffies((unsigned long) msecs);
1327bfa83a9eSThomas Graf }
1328bfa83a9eSThomas Graf 
1329bfa83a9eSThomas Graf /**
133067b61f6cSJiri Benc  * nla_get_in_addr - return payload of IPv4 address attribute
133167b61f6cSJiri Benc  * @nla: IPv4 address netlink attribute
133267b61f6cSJiri Benc  */
133367b61f6cSJiri Benc static inline __be32 nla_get_in_addr(const struct nlattr *nla)
133467b61f6cSJiri Benc {
133567b61f6cSJiri Benc 	return *(__be32 *) nla_data(nla);
133667b61f6cSJiri Benc }
133767b61f6cSJiri Benc 
133867b61f6cSJiri Benc /**
133967b61f6cSJiri Benc  * nla_get_in6_addr - return payload of IPv6 address attribute
134067b61f6cSJiri Benc  * @nla: IPv6 address netlink attribute
134167b61f6cSJiri Benc  */
134267b61f6cSJiri Benc static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla)
134367b61f6cSJiri Benc {
134467b61f6cSJiri Benc 	struct in6_addr tmp;
134567b61f6cSJiri Benc 
134667b61f6cSJiri Benc 	nla_memcpy(&tmp, nla, sizeof(tmp));
134767b61f6cSJiri Benc 	return tmp;
134867b61f6cSJiri Benc }
134967b61f6cSJiri Benc 
135067b61f6cSJiri Benc /**
135164c83d83SJamal Hadi Salim  * nla_get_bitfield32 - return payload of 32 bitfield attribute
135264c83d83SJamal Hadi Salim  * @nla: nla_bitfield32 attribute
135364c83d83SJamal Hadi Salim  */
135464c83d83SJamal Hadi Salim static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla)
135564c83d83SJamal Hadi Salim {
135664c83d83SJamal Hadi Salim 	struct nla_bitfield32 tmp;
135764c83d83SJamal Hadi Salim 
135864c83d83SJamal Hadi Salim 	nla_memcpy(&tmp, nla, sizeof(tmp));
135964c83d83SJamal Hadi Salim 	return tmp;
136064c83d83SJamal Hadi Salim }
136164c83d83SJamal Hadi Salim 
136264c83d83SJamal Hadi Salim /**
1363b15ca182SThomas Graf  * nla_memdup - duplicate attribute memory (kmemdup)
1364b15ca182SThomas Graf  * @src: netlink attribute to duplicate from
1365b15ca182SThomas Graf  * @gfp: GFP mask
1366b15ca182SThomas Graf  */
1367b15ca182SThomas Graf static inline void *nla_memdup(const struct nlattr *src, gfp_t gfp)
1368b15ca182SThomas Graf {
1369b15ca182SThomas Graf 	return kmemdup(nla_data(src), nla_len(src), gfp);
1370b15ca182SThomas Graf }
1371b15ca182SThomas Graf 
1372b15ca182SThomas Graf /**
1373bfa83a9eSThomas Graf  * nla_nest_start - Start a new level of nested attributes
1374bfa83a9eSThomas Graf  * @skb: socket buffer to add attributes to
1375bfa83a9eSThomas Graf  * @attrtype: attribute type of container
1376bfa83a9eSThomas Graf  *
1377bfa83a9eSThomas Graf  * Returns the container attribute
1378bfa83a9eSThomas Graf  */
1379bfa83a9eSThomas Graf static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
1380bfa83a9eSThomas Graf {
138127a884dcSArnaldo Carvalho de Melo 	struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
1382bfa83a9eSThomas Graf 
1383bfa83a9eSThomas Graf 	if (nla_put(skb, attrtype, 0, NULL) < 0)
1384bfa83a9eSThomas Graf 		return NULL;
1385bfa83a9eSThomas Graf 
1386bfa83a9eSThomas Graf 	return start;
1387bfa83a9eSThomas Graf }
1388bfa83a9eSThomas Graf 
1389bfa83a9eSThomas Graf /**
1390bfa83a9eSThomas Graf  * nla_nest_end - Finalize nesting of attributes
1391d1ec3b77SPierre Ynard  * @skb: socket buffer the attributes are stored in
1392bfa83a9eSThomas Graf  * @start: container attribute
1393bfa83a9eSThomas Graf  *
1394bfa83a9eSThomas Graf  * Corrects the container attribute header to include the all
1395bfa83a9eSThomas Graf  * appeneded attributes.
1396bfa83a9eSThomas Graf  *
1397bfa83a9eSThomas Graf  * Returns the total data length of the skb.
1398bfa83a9eSThomas Graf  */
1399bfa83a9eSThomas Graf static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
1400bfa83a9eSThomas Graf {
140127a884dcSArnaldo Carvalho de Melo 	start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
1402bfa83a9eSThomas Graf 	return skb->len;
1403bfa83a9eSThomas Graf }
1404bfa83a9eSThomas Graf 
1405bfa83a9eSThomas Graf /**
1406bfa83a9eSThomas Graf  * nla_nest_cancel - Cancel nesting of attributes
1407bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
1408bfa83a9eSThomas Graf  * @start: container attribute
1409bfa83a9eSThomas Graf  *
1410bfa83a9eSThomas Graf  * Removes the container attribute and including all nested
1411bc3ed28cSThomas Graf  * attributes. Returns -EMSGSIZE
1412bfa83a9eSThomas Graf  */
1413bc3ed28cSThomas Graf static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
1414bfa83a9eSThomas Graf {
1415bc3ed28cSThomas Graf 	nlmsg_trim(skb, start);
1416bfa83a9eSThomas Graf }
1417bfa83a9eSThomas Graf 
1418bfa83a9eSThomas Graf /**
14194fe5d5c0SPaul Moore  * nla_validate_nested - Validate a stream of nested attributes
14204fe5d5c0SPaul Moore  * @start: container attribute
14214fe5d5c0SPaul Moore  * @maxtype: maximum attribute type to be expected
14224fe5d5c0SPaul Moore  * @policy: validation policy
1423fceb6435SJohannes Berg  * @extack: extended ACK report struct
14244fe5d5c0SPaul Moore  *
14254fe5d5c0SPaul Moore  * Validates all attributes in the nested attribute stream against the
14264fe5d5c0SPaul Moore  * specified policy. Attributes with a type exceeding maxtype will be
14274fe5d5c0SPaul Moore  * ignored. See documenation of struct nla_policy for more details.
14284fe5d5c0SPaul Moore  *
14294fe5d5c0SPaul Moore  * Returns 0 on success or a negative error code.
14304fe5d5c0SPaul Moore  */
14313654654fSJan Engelhardt static inline int nla_validate_nested(const struct nlattr *start, int maxtype,
1432fceb6435SJohannes Berg 				      const struct nla_policy *policy,
1433fceb6435SJohannes Berg 				      struct netlink_ext_ack *extack)
14344fe5d5c0SPaul Moore {
1435fceb6435SJohannes Berg 	return nla_validate(nla_data(start), nla_len(start), maxtype, policy,
1436fceb6435SJohannes Berg 			    extack);
14374fe5d5c0SPaul Moore }
14384fe5d5c0SPaul Moore 
14394fe5d5c0SPaul Moore /**
1440089bf1a6SNicolas Dichtel  * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute
1441089bf1a6SNicolas Dichtel  * @skb: socket buffer the message is stored in
1442089bf1a6SNicolas Dichtel  *
1443089bf1a6SNicolas Dichtel  * Return true if padding is needed to align the next attribute (nla_data()) to
1444089bf1a6SNicolas Dichtel  * a 64-bit aligned area.
1445089bf1a6SNicolas Dichtel  */
1446089bf1a6SNicolas Dichtel static inline bool nla_need_padding_for_64bit(struct sk_buff *skb)
1447089bf1a6SNicolas Dichtel {
1448089bf1a6SNicolas Dichtel #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1449089bf1a6SNicolas Dichtel 	/* The nlattr header is 4 bytes in size, that's why we test
1450089bf1a6SNicolas Dichtel 	 * if the skb->data _is_ aligned.  A NOP attribute, plus
1451089bf1a6SNicolas Dichtel 	 * nlattr header for next attribute, will make nla_data()
1452089bf1a6SNicolas Dichtel 	 * 8-byte aligned.
1453089bf1a6SNicolas Dichtel 	 */
1454089bf1a6SNicolas Dichtel 	if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
1455089bf1a6SNicolas Dichtel 		return true;
1456089bf1a6SNicolas Dichtel #endif
1457089bf1a6SNicolas Dichtel 	return false;
1458089bf1a6SNicolas Dichtel }
1459089bf1a6SNicolas Dichtel 
1460089bf1a6SNicolas Dichtel /**
146135c58459SDavid S. Miller  * nla_align_64bit - 64-bit align the nla_data() of next attribute
146235c58459SDavid S. Miller  * @skb: socket buffer the message is stored in
146335c58459SDavid S. Miller  * @padattr: attribute type for the padding
146435c58459SDavid S. Miller  *
146535c58459SDavid S. Miller  * Conditionally emit a padding netlink attribute in order to make
146635c58459SDavid S. Miller  * the next attribute we emit have a 64-bit aligned nla_data() area.
146735c58459SDavid S. Miller  * This will only be done in architectures which do not have
1468cca1d815SEric Dumazet  * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
146935c58459SDavid S. Miller  *
147035c58459SDavid S. Miller  * Returns zero on success or a negative error code.
147135c58459SDavid S. Miller  */
147235c58459SDavid S. Miller static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
147335c58459SDavid S. Miller {
1474089bf1a6SNicolas Dichtel 	if (nla_need_padding_for_64bit(skb) &&
1475cca1d815SEric Dumazet 	    !nla_reserve(skb, padattr, 0))
147635c58459SDavid S. Miller 		return -EMSGSIZE;
1477089bf1a6SNicolas Dichtel 
147835c58459SDavid S. Miller 	return 0;
147935c58459SDavid S. Miller }
148035c58459SDavid S. Miller 
148135c58459SDavid S. Miller /**
148235c58459SDavid S. Miller  * nla_total_size_64bit - total length of attribute including padding
148335c58459SDavid S. Miller  * @payload: length of payload
148435c58459SDavid S. Miller  */
148535c58459SDavid S. Miller static inline int nla_total_size_64bit(int payload)
148635c58459SDavid S. Miller {
148735c58459SDavid S. Miller 	return NLA_ALIGN(nla_attr_size(payload))
1488cca1d815SEric Dumazet #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
148935c58459SDavid S. Miller 		+ NLA_ALIGN(nla_attr_size(0))
149035c58459SDavid S. Miller #endif
149135c58459SDavid S. Miller 		;
149235c58459SDavid S. Miller }
149335c58459SDavid S. Miller 
149435c58459SDavid S. Miller /**
1495bfa83a9eSThomas Graf  * nla_for_each_attr - iterate over a stream of attributes
1496bfa83a9eSThomas Graf  * @pos: loop counter, set to current attribute
1497bfa83a9eSThomas Graf  * @head: head of attribute stream
1498bfa83a9eSThomas Graf  * @len: length of attribute stream
1499bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
1500bfa83a9eSThomas Graf  */
1501bfa83a9eSThomas Graf #define nla_for_each_attr(pos, head, len, rem) \
1502bfa83a9eSThomas Graf 	for (pos = head, rem = len; \
1503bfa83a9eSThomas Graf 	     nla_ok(pos, rem); \
1504bfa83a9eSThomas Graf 	     pos = nla_next(pos, &(rem)))
1505bfa83a9eSThomas Graf 
1506fe4944e5SThomas Graf /**
1507fe4944e5SThomas Graf  * nla_for_each_nested - iterate over nested attributes
1508fe4944e5SThomas Graf  * @pos: loop counter, set to current attribute
1509fe4944e5SThomas Graf  * @nla: attribute containing the nested attributes
1510fe4944e5SThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
1511fe4944e5SThomas Graf  */
1512fe4944e5SThomas Graf #define nla_for_each_nested(pos, nla, rem) \
1513fe4944e5SThomas Graf 	nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
1514fe4944e5SThomas Graf 
1515941d8ebcSSimon Horman /**
1516941d8ebcSSimon Horman  * nla_is_last - Test if attribute is last in stream
1517941d8ebcSSimon Horman  * @nla: attribute to test
1518941d8ebcSSimon Horman  * @rem: bytes remaining in stream
1519941d8ebcSSimon Horman  */
1520941d8ebcSSimon Horman static inline bool nla_is_last(const struct nlattr *nla, int rem)
1521941d8ebcSSimon Horman {
1522941d8ebcSSimon Horman 	return nla->nla_len == rem;
1523941d8ebcSSimon Horman }
1524941d8ebcSSimon Horman 
1525bfa83a9eSThomas Graf #endif
1526