xref: /openbmc/linux/include/net/netlink.h (revision bdbb4e29)
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,
184bfa83a9eSThomas Graf 	__NLA_TYPE_MAX,
185bfa83a9eSThomas Graf };
186bfa83a9eSThomas Graf 
187bfa83a9eSThomas Graf #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
188bfa83a9eSThomas Graf 
189d06a09b9SJohannes Berg struct netlink_range_validation {
190d06a09b9SJohannes Berg 	u64 min, max;
191d06a09b9SJohannes Berg };
192d06a09b9SJohannes Berg 
193d06a09b9SJohannes Berg struct netlink_range_validation_signed {
194d06a09b9SJohannes Berg 	s64 min, max;
195d06a09b9SJohannes Berg };
196d06a09b9SJohannes Berg 
1973e48be05SJohannes Berg enum nla_policy_validation {
1983e48be05SJohannes Berg 	NLA_VALIDATE_NONE,
1993e48be05SJohannes Berg 	NLA_VALIDATE_RANGE,
2008aa26c57SJohannes Berg 	NLA_VALIDATE_RANGE_WARN_TOO_LONG,
2013e48be05SJohannes Berg 	NLA_VALIDATE_MIN,
2023e48be05SJohannes Berg 	NLA_VALIDATE_MAX,
203bdbb4e29SJakub Kicinski 	NLA_VALIDATE_MASK,
204d06a09b9SJohannes Berg 	NLA_VALIDATE_RANGE_PTR,
20533188bd6SJohannes Berg 	NLA_VALIDATE_FUNCTION,
2063e48be05SJohannes Berg };
2073e48be05SJohannes Berg 
208bfa83a9eSThomas Graf /**
209bfa83a9eSThomas Graf  * struct nla_policy - attribute validation policy
210bfa83a9eSThomas Graf  * @type: Type of attribute or NLA_UNSPEC
2113e48be05SJohannes Berg  * @validation_type: type of attribute validation done in addition to
21233188bd6SJohannes Berg  *	type-specific validation (e.g. range, function call), see
2133e48be05SJohannes Berg  *	&enum nla_policy_validation
214a5531a5dSThomas Graf  * @len: Type specific length of payload
215bfa83a9eSThomas Graf  *
216bfa83a9eSThomas Graf  * Policies are defined as arrays of this struct, the array must be
217bfa83a9eSThomas Graf  * accessible by attribute type up to the highest identifier to be expected.
218bfa83a9eSThomas Graf  *
219a5531a5dSThomas Graf  * Meaning of `len' field:
220a5531a5dSThomas Graf  *    NLA_STRING           Maximum length of string
221a5531a5dSThomas Graf  *    NLA_NUL_STRING       Maximum length of string (excluding NUL)
222a5531a5dSThomas Graf  *    NLA_FLAG             Unused
223d30045a0SJohannes Berg  *    NLA_BINARY           Maximum length of attribute payload
2248aa26c57SJohannes Berg  *                         (but see also below with the validation type)
2251501d135SJohannes Berg  *    NLA_NESTED,
2261501d135SJohannes Berg  *    NLA_NESTED_ARRAY     Length verification is done by checking len of
2279a659a35SJohannes Berg  *                         nested header (or empty); len field is used if
22847a1494bSJohannes Berg  *                         nested_policy is also used, for the max attr
2299a659a35SJohannes Berg  *                         number in the nested policy.
2304b6cc728SJohannes Berg  *    NLA_U8, NLA_U16,
2314b6cc728SJohannes Berg  *    NLA_U32, NLA_U64,
2324778e0beSJiri Pirko  *    NLA_S8, NLA_S16,
2334778e0beSJiri Pirko  *    NLA_S32, NLA_S64,
2344b6cc728SJohannes Berg  *    NLA_MSECS            Leaving the length field zero will verify the
2354b6cc728SJohannes Berg  *                         given type fits, using it verifies minimum length
2364b6cc728SJohannes Berg  *                         just like "All other"
237568b742aSJohannes Berg  *    NLA_BITFIELD32       Unused
238568b742aSJohannes Berg  *    NLA_REJECT           Unused
2394b6cc728SJohannes Berg  *    All other            Minimum length of attribute payload
240a5531a5dSThomas Graf  *
24147a1494bSJohannes Berg  * Meaning of validation union:
242568b742aSJohannes Berg  *    NLA_BITFIELD32       This is a 32-bit bitmap/bitselector attribute and
24347a1494bSJohannes Berg  *                         `bitfield32_valid' is the u32 value of valid flags
24447a1494bSJohannes Berg  *    NLA_REJECT           This attribute is always rejected and `reject_message'
245568b742aSJohannes Berg  *                         may point to a string to report as the error instead
246568b742aSJohannes Berg  *                         of the generic one in extended ACK.
24747a1494bSJohannes Berg  *    NLA_NESTED           `nested_policy' to a nested policy to validate, must
24847a1494bSJohannes Berg  *                         also set `len' to the max attribute number. Use the
24947a1494bSJohannes Berg  *                         provided NLA_POLICY_NESTED() macro.
2509a659a35SJohannes Berg  *                         Note that nla_parse() will validate, but of course not
2519a659a35SJohannes Berg  *                         parse, the nested sub-policies.
25247a1494bSJohannes Berg  *    NLA_NESTED_ARRAY     `nested_policy' points to a nested policy to validate,
25347a1494bSJohannes Berg  *                         must also set `len' to the max attribute number. Use
25447a1494bSJohannes Berg  *                         the provided NLA_POLICY_NESTED_ARRAY() macro.
25547a1494bSJohannes Berg  *                         The difference to NLA_NESTED is the structure:
25647a1494bSJohannes Berg  *                         NLA_NESTED has the nested attributes directly inside
25747a1494bSJohannes Berg  *                         while an array has the nested attributes at another
25847a1494bSJohannes Berg  *                         level down and the attribute types directly in the
25947a1494bSJohannes Berg  *                         nesting don't matter.
2603e48be05SJohannes Berg  *    NLA_U8,
2613e48be05SJohannes Berg  *    NLA_U16,
2623e48be05SJohannes Berg  *    NLA_U32,
2633e48be05SJohannes Berg  *    NLA_U64,
2643e48be05SJohannes Berg  *    NLA_S8,
2653e48be05SJohannes Berg  *    NLA_S16,
2663e48be05SJohannes Berg  *    NLA_S32,
26747a1494bSJohannes Berg  *    NLA_S64              The `min' and `max' fields are used depending on the
26847a1494bSJohannes Berg  *                         validation_type field, if that is min/max/range then
26947a1494bSJohannes Berg  *                         the min, max or both are used (respectively) to check
2703e48be05SJohannes Berg  *                         the value of the integer attribute.
2713e48be05SJohannes Berg  *                         Note that in the interest of code simplicity and
2723e48be05SJohannes Berg  *                         struct size both limits are s16, so you cannot
2733e48be05SJohannes Berg  *                         enforce a range that doesn't fall within the range
2743e48be05SJohannes Berg  *                         of s16 - do that as usual in the code instead.
27547a1494bSJohannes Berg  *                         Use the NLA_POLICY_MIN(), NLA_POLICY_MAX() and
27647a1494bSJohannes Berg  *                         NLA_POLICY_RANGE() macros.
277d06a09b9SJohannes Berg  *    NLA_U8,
278d06a09b9SJohannes Berg  *    NLA_U16,
279d06a09b9SJohannes Berg  *    NLA_U32,
280d06a09b9SJohannes Berg  *    NLA_U64              If the validation_type field instead is set to
281d06a09b9SJohannes Berg  *                         NLA_VALIDATE_RANGE_PTR, `range' must be a pointer
282d06a09b9SJohannes Berg  *                         to a struct netlink_range_validation that indicates
283d06a09b9SJohannes Berg  *                         the min/max values.
284d06a09b9SJohannes Berg  *                         Use NLA_POLICY_FULL_RANGE().
285d06a09b9SJohannes Berg  *    NLA_S8,
286d06a09b9SJohannes Berg  *    NLA_S16,
287d06a09b9SJohannes Berg  *    NLA_S32,
288d06a09b9SJohannes Berg  *    NLA_S64              If the validation_type field instead is set to
289d06a09b9SJohannes Berg  *                         NLA_VALIDATE_RANGE_PTR, `range_signed' must be a
290d06a09b9SJohannes Berg  *                         pointer to a struct netlink_range_validation_signed
291d06a09b9SJohannes Berg  *                         that indicates the min/max values.
292d06a09b9SJohannes Berg  *                         Use NLA_POLICY_FULL_RANGE_SIGNED().
2938aa26c57SJohannes Berg  *
2948aa26c57SJohannes Berg  *    NLA_BINARY           If the validation type is like the ones for integers
2958aa26c57SJohannes Berg  *                         above, then the min/max length (not value like for
2968aa26c57SJohannes Berg  *                         integers) of the attribute is enforced.
2978aa26c57SJohannes Berg  *
2983e48be05SJohannes Berg  *    All other            Unused - but note that it's a union
299568b742aSJohannes Berg  *
30033188bd6SJohannes Berg  * Meaning of `validate' field, use via NLA_POLICY_VALIDATE_FN:
30147a1494bSJohannes Berg  *    NLA_BINARY           Validation function called for the attribute.
30233188bd6SJohannes Berg  *    All other            Unused - but note that it's a union
30333188bd6SJohannes Berg  *
304bfa83a9eSThomas Graf  * Example:
30547a1494bSJohannes Berg  *
30647a1494bSJohannes Berg  * static const u32 myvalidflags = 0xff231023;
30747a1494bSJohannes Berg  *
308b54452b0SAlexey Dobriyan  * static const struct nla_policy my_policy[ATTR_MAX+1] = {
309bfa83a9eSThomas Graf  * 	[ATTR_FOO] = { .type = NLA_U16 },
310d30045a0SJohannes Berg  *	[ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
3118aa26c57SJohannes Berg  *	[ATTR_BAZ] = NLA_POLICY_EXACT_LEN(sizeof(struct mystruct)),
31247a1494bSJohannes Berg  *	[ATTR_GOO] = NLA_POLICY_BITFIELD32(myvalidflags),
313bfa83a9eSThomas Graf  * };
314bfa83a9eSThomas Graf  */
315bfa83a9eSThomas Graf struct nla_policy {
3163e48be05SJohannes Berg 	u8		type;
3173e48be05SJohannes Berg 	u8		validation_type;
318a5531a5dSThomas Graf 	u16		len;
3193e48be05SJohannes Berg 	union {
32047a1494bSJohannes Berg 		const u32 bitfield32_valid;
321bdbb4e29SJakub Kicinski 		const u32 mask;
32247a1494bSJohannes Berg 		const char *reject_message;
32347a1494bSJohannes Berg 		const struct nla_policy *nested_policy;
324d06a09b9SJohannes Berg 		struct netlink_range_validation *range;
325d06a09b9SJohannes Berg 		struct netlink_range_validation_signed *range_signed;
3263e48be05SJohannes Berg 		struct {
3273e48be05SJohannes Berg 			s16 min, max;
3283e48be05SJohannes Berg 		};
32933188bd6SJohannes Berg 		int (*validate)(const struct nlattr *attr,
33033188bd6SJohannes Berg 				struct netlink_ext_ack *extack);
33156738f46SJohannes Berg 		/* This entry is special, and used for the attribute at index 0
33256738f46SJohannes Berg 		 * only, and specifies special data about the policy, namely it
33356738f46SJohannes Berg 		 * specifies the "boundary type" where strict length validation
33456738f46SJohannes Berg 		 * starts for any attribute types >= this value, also, strict
33556738f46SJohannes Berg 		 * nesting validation starts here.
33656738f46SJohannes Berg 		 *
33756738f46SJohannes Berg 		 * Additionally, it means that NLA_UNSPEC is actually NLA_REJECT
3388aa26c57SJohannes Berg 		 * for any types >= this, so need to use NLA_POLICY_MIN_LEN() to
3398aa26c57SJohannes Berg 		 * get the previous pure { .len = xyz } behaviour. The advantage
3408aa26c57SJohannes Berg 		 * of this is that types not specified in the policy will be
3418aa26c57SJohannes Berg 		 * rejected.
34256738f46SJohannes Berg 		 *
34356738f46SJohannes Berg 		 * For completely new families it should be set to 1 so that the
34456738f46SJohannes Berg 		 * validation is enforced for all attributes. For existing ones
34556738f46SJohannes Berg 		 * it should be set at least when new attributes are added to
34656738f46SJohannes Berg 		 * the enum used by the policy, and be set to the new value that
34756738f46SJohannes Berg 		 * was added to enforce strict validation from thereon.
34856738f46SJohannes Berg 		 */
34956738f46SJohannes Berg 		u16 strict_start_type;
3503e48be05SJohannes Berg 	};
351bfa83a9eSThomas Graf };
352bfa83a9eSThomas Graf 
353b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR		NLA_POLICY_EXACT_LEN(ETH_ALEN)
354b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR_COMPAT	NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN)
355b60b87fcSJohannes Berg 
35623323289SJohannes Berg #define _NLA_POLICY_NESTED(maxattr, policy) \
35747a1494bSJohannes Berg 	{ .type = NLA_NESTED, .nested_policy = policy, .len = maxattr }
35823323289SJohannes Berg #define _NLA_POLICY_NESTED_ARRAY(maxattr, policy) \
35947a1494bSJohannes Berg 	{ .type = NLA_NESTED_ARRAY, .nested_policy = policy, .len = maxattr }
36023323289SJohannes Berg #define NLA_POLICY_NESTED(policy) \
36123323289SJohannes Berg 	_NLA_POLICY_NESTED(ARRAY_SIZE(policy) - 1, policy)
36223323289SJohannes Berg #define NLA_POLICY_NESTED_ARRAY(policy) \
36323323289SJohannes Berg 	_NLA_POLICY_NESTED_ARRAY(ARRAY_SIZE(policy) - 1, policy)
36447a1494bSJohannes Berg #define NLA_POLICY_BITFIELD32(valid) \
36547a1494bSJohannes Berg 	{ .type = NLA_BITFIELD32, .bitfield32_valid = valid }
3669a659a35SJohannes Berg 
367ddcf3b70SJakub Kicinski #define __NLA_IS_UINT_TYPE(tp)						\
368ddcf3b70SJakub Kicinski 	(tp == NLA_U8 || tp == NLA_U16 || tp == NLA_U32 || tp == NLA_U64)
369ddcf3b70SJakub Kicinski #define __NLA_IS_SINT_TYPE(tp)						\
370ddcf3b70SJakub Kicinski 	(tp == NLA_S8 || tp == NLA_S16 || tp == NLA_S32 || tp == NLA_S64)
371ddcf3b70SJakub Kicinski 
3725886d932SJohannes Berg #define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition))
373bdbb4e29SJakub Kicinski #define NLA_ENSURE_UINT_TYPE(tp)			\
374bdbb4e29SJakub Kicinski 	(__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp)) + tp)
3758aa26c57SJohannes Berg #define NLA_ENSURE_UINT_OR_BINARY_TYPE(tp)		\
376ddcf3b70SJakub Kicinski 	(__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) ||	\
3778aa26c57SJohannes Berg 		      tp == NLA_MSECS ||		\
3788aa26c57SJohannes Berg 		      tp == NLA_BINARY) + tp)
379d06a09b9SJohannes Berg #define NLA_ENSURE_SINT_TYPE(tp)			\
380ddcf3b70SJakub Kicinski 	(__NLA_ENSURE(__NLA_IS_SINT_TYPE(tp)) + tp)
3818aa26c57SJohannes Berg #define NLA_ENSURE_INT_OR_BINARY_TYPE(tp)		\
382ddcf3b70SJakub Kicinski 	(__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) ||		\
383ddcf3b70SJakub Kicinski 		      __NLA_IS_SINT_TYPE(tp) ||		\
3848aa26c57SJohannes Berg 		      tp == NLA_MSECS ||		\
3858aa26c57SJohannes Berg 		      tp == NLA_BINARY) + tp)
38633188bd6SJohannes Berg #define NLA_ENSURE_NO_VALIDATION_PTR(tp)		\
38733188bd6SJohannes Berg 	(__NLA_ENSURE(tp != NLA_BITFIELD32 &&		\
38833188bd6SJohannes Berg 		      tp != NLA_REJECT &&		\
38933188bd6SJohannes Berg 		      tp != NLA_NESTED &&		\
39033188bd6SJohannes Berg 		      tp != NLA_NESTED_ARRAY) + tp)
3913e48be05SJohannes Berg 
3923e48be05SJohannes Berg #define NLA_POLICY_RANGE(tp, _min, _max) {		\
3938aa26c57SJohannes Berg 	.type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp),	\
3943e48be05SJohannes Berg 	.validation_type = NLA_VALIDATE_RANGE,		\
3953e48be05SJohannes Berg 	.min = _min,					\
3963e48be05SJohannes Berg 	.max = _max					\
3973e48be05SJohannes Berg }
3983e48be05SJohannes Berg 
399d06a09b9SJohannes Berg #define NLA_POLICY_FULL_RANGE(tp, _range) {		\
4008aa26c57SJohannes Berg 	.type = NLA_ENSURE_UINT_OR_BINARY_TYPE(tp),	\
401d06a09b9SJohannes Berg 	.validation_type = NLA_VALIDATE_RANGE_PTR,	\
402d06a09b9SJohannes Berg 	.range = _range,				\
403d06a09b9SJohannes Berg }
404d06a09b9SJohannes Berg 
405d06a09b9SJohannes Berg #define NLA_POLICY_FULL_RANGE_SIGNED(tp, _range) {	\
406d06a09b9SJohannes Berg 	.type = NLA_ENSURE_SINT_TYPE(tp),		\
407d06a09b9SJohannes Berg 	.validation_type = NLA_VALIDATE_RANGE_PTR,	\
408d06a09b9SJohannes Berg 	.range_signed = _range,				\
409d06a09b9SJohannes Berg }
410d06a09b9SJohannes Berg 
4113e48be05SJohannes Berg #define NLA_POLICY_MIN(tp, _min) {			\
4128aa26c57SJohannes Berg 	.type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp),	\
4133e48be05SJohannes Berg 	.validation_type = NLA_VALIDATE_MIN,		\
4143e48be05SJohannes Berg 	.min = _min,					\
4153e48be05SJohannes Berg }
4163e48be05SJohannes Berg 
4173e48be05SJohannes Berg #define NLA_POLICY_MAX(tp, _max) {			\
4188aa26c57SJohannes Berg 	.type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp),	\
4193e48be05SJohannes Berg 	.validation_type = NLA_VALIDATE_MAX,		\
4203e48be05SJohannes Berg 	.max = _max,					\
4213e48be05SJohannes Berg }
4223e48be05SJohannes Berg 
423bdbb4e29SJakub Kicinski #define NLA_POLICY_MASK(tp, _mask) {			\
424bdbb4e29SJakub Kicinski 	.type = NLA_ENSURE_UINT_TYPE(tp),		\
425bdbb4e29SJakub Kicinski 	.validation_type = NLA_VALIDATE_MASK,		\
426bdbb4e29SJakub Kicinski 	.mask = _mask,					\
427bdbb4e29SJakub Kicinski }
428bdbb4e29SJakub Kicinski 
42933188bd6SJohannes Berg #define NLA_POLICY_VALIDATE_FN(tp, fn, ...) {		\
43033188bd6SJohannes Berg 	.type = NLA_ENSURE_NO_VALIDATION_PTR(tp),	\
43133188bd6SJohannes Berg 	.validation_type = NLA_VALIDATE_FUNCTION,	\
43233188bd6SJohannes Berg 	.validate = fn,					\
43333188bd6SJohannes Berg 	.len = __VA_ARGS__ + 0,				\
43433188bd6SJohannes Berg }
43533188bd6SJohannes Berg 
4368aa26c57SJohannes Berg #define NLA_POLICY_EXACT_LEN(_len)	NLA_POLICY_RANGE(NLA_BINARY, _len, _len)
4378aa26c57SJohannes Berg #define NLA_POLICY_EXACT_LEN_WARN(_len) {			\
4388aa26c57SJohannes Berg 	.type = NLA_BINARY,					\
4398aa26c57SJohannes Berg 	.validation_type = NLA_VALIDATE_RANGE_WARN_TOO_LONG,	\
4408aa26c57SJohannes Berg 	.min = _len,						\
4418aa26c57SJohannes Berg 	.max = _len						\
4428aa26c57SJohannes Berg }
4438aa26c57SJohannes Berg #define NLA_POLICY_MIN_LEN(_len)	NLA_POLICY_MIN(NLA_BINARY, _len)
4448aa26c57SJohannes Berg 
4454e902c57SThomas Graf /**
4464e902c57SThomas Graf  * struct nl_info - netlink source information
4474e902c57SThomas Graf  * @nlh: Netlink message header of original request
4483de205cdSIdo Schimmel  * @nl_net: Network namespace
44915e47304SEric W. Biederman  * @portid: Netlink PORTID of requesting application
4503de205cdSIdo Schimmel  * @skip_notify: Skip netlink notifications to user space
451c82481f7SIdo Schimmel  * @skip_notify_kernel: Skip selected in-kernel notifications
4524e902c57SThomas Graf  */
4534e902c57SThomas Graf struct nl_info {
4544e902c57SThomas Graf 	struct nlmsghdr		*nlh;
4554d1169c1SDenis V. Lunev 	struct net		*nl_net;
45615e47304SEric W. Biederman 	u32			portid;
457c82481f7SIdo Schimmel 	u8			skip_notify:1,
458c82481f7SIdo Schimmel 				skip_notify_kernel:1;
4594e902c57SThomas Graf };
4604e902c57SThomas Graf 
4618cb08174SJohannes Berg /**
4628cb08174SJohannes Berg  * enum netlink_validation - netlink message/attribute validation levels
4638cb08174SJohannes Berg  * @NL_VALIDATE_LIBERAL: Old-style "be liberal" validation, not caring about
4648cb08174SJohannes Berg  *	extra data at the end of the message, attributes being longer than
4658cb08174SJohannes Berg  *	they should be, or unknown attributes being present.
4668cb08174SJohannes Berg  * @NL_VALIDATE_TRAILING: Reject junk data encountered after attribute parsing.
4678cb08174SJohannes Berg  * @NL_VALIDATE_MAXTYPE: Reject attributes > max type; Together with _TRAILING
4688cb08174SJohannes Berg  *	this is equivalent to the old nla_parse_strict()/nlmsg_parse_strict().
4698cb08174SJohannes Berg  * @NL_VALIDATE_UNSPEC: Reject attributes with NLA_UNSPEC in the policy.
4708cb08174SJohannes Berg  *	This can safely be set by the kernel when the given policy has no
4718cb08174SJohannes Berg  *	NLA_UNSPEC anymore, and can thus be used to ensure policy entries
4728cb08174SJohannes Berg  *	are enforced going forward.
4738cb08174SJohannes Berg  * @NL_VALIDATE_STRICT_ATTRS: strict attribute policy parsing (e.g.
4748cb08174SJohannes Berg  *	U8, U16, U32 must have exact size, etc.)
475b424e432SMichal Kubecek  * @NL_VALIDATE_NESTED: Check that NLA_F_NESTED is set for NLA_NESTED(_ARRAY)
476b424e432SMichal Kubecek  *	and unset for other policies.
4778cb08174SJohannes Berg  */
4788cb08174SJohannes Berg enum netlink_validation {
4798cb08174SJohannes Berg 	NL_VALIDATE_LIBERAL = 0,
4808cb08174SJohannes Berg 	NL_VALIDATE_TRAILING = BIT(0),
4818cb08174SJohannes Berg 	NL_VALIDATE_MAXTYPE = BIT(1),
4828cb08174SJohannes Berg 	NL_VALIDATE_UNSPEC = BIT(2),
4838cb08174SJohannes Berg 	NL_VALIDATE_STRICT_ATTRS = BIT(3),
484b424e432SMichal Kubecek 	NL_VALIDATE_NESTED = BIT(4),
4858cb08174SJohannes Berg };
4868cb08174SJohannes Berg 
4878cb08174SJohannes Berg #define NL_VALIDATE_DEPRECATED_STRICT (NL_VALIDATE_TRAILING |\
4888cb08174SJohannes Berg 				       NL_VALIDATE_MAXTYPE)
4898cb08174SJohannes Berg #define NL_VALIDATE_STRICT (NL_VALIDATE_TRAILING |\
4908cb08174SJohannes Berg 			    NL_VALIDATE_MAXTYPE |\
4918cb08174SJohannes Berg 			    NL_VALIDATE_UNSPEC |\
492b424e432SMichal Kubecek 			    NL_VALIDATE_STRICT_ATTRS |\
493b424e432SMichal Kubecek 			    NL_VALIDATE_NESTED)
4948cb08174SJohannes Berg 
4954f69053bSJoe Perches int netlink_rcv_skb(struct sk_buff *skb,
4962d4bc933SJohannes Berg 		    int (*cb)(struct sk_buff *, struct nlmsghdr *,
4972d4bc933SJohannes Berg 			      struct netlink_ext_ack *));
4984f69053bSJoe Perches int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
4994f69053bSJoe Perches 		 unsigned int group, int report, gfp_t flags);
50082ace47aSThomas Graf 
5018cb08174SJohannes Berg int __nla_validate(const struct nlattr *head, int len, int maxtype,
5028cb08174SJohannes Berg 		   const struct nla_policy *policy, unsigned int validate,
503fceb6435SJohannes Berg 		   struct netlink_ext_ack *extack);
5048cb08174SJohannes Berg int __nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
5058cb08174SJohannes Berg 		int len, const struct nla_policy *policy, unsigned int validate,
506a5f6cba2SDavid Ahern 		struct netlink_ext_ack *extack);
5074f69053bSJoe Perches int nla_policy_len(const struct nla_policy *, int);
5084f69053bSJoe Perches struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
5094f69053bSJoe Perches size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize);
5102cf0c8b3SPhil Sutter char *nla_strdup(const struct nlattr *nla, gfp_t flags);
5114f69053bSJoe Perches int nla_memcpy(void *dest, const struct nlattr *src, int count);
5124f69053bSJoe Perches int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
5134f69053bSJoe Perches int nla_strcmp(const struct nlattr *nla, const char *str);
5144f69053bSJoe Perches struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
515089bf1a6SNicolas Dichtel struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
516089bf1a6SNicolas Dichtel 				   int attrlen, int padattr);
5174f69053bSJoe Perches void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
5184f69053bSJoe Perches struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
519089bf1a6SNicolas Dichtel struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype,
520089bf1a6SNicolas Dichtel 				 int attrlen, int padattr);
5214f69053bSJoe Perches void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
5224f69053bSJoe Perches void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
523fe4944e5SThomas Graf 	       const void *data);
524089bf1a6SNicolas Dichtel void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
525089bf1a6SNicolas Dichtel 		     const void *data, int padattr);
5264f69053bSJoe Perches void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
5274f69053bSJoe Perches int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);
528089bf1a6SNicolas Dichtel int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
529089bf1a6SNicolas Dichtel 		  const void *data, int padattr);
5304f69053bSJoe Perches int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
5314f69053bSJoe Perches int nla_append(struct sk_buff *skb, int attrlen, const void *data);
532bfa83a9eSThomas Graf 
533bfa83a9eSThomas Graf /**************************************************************************
534bfa83a9eSThomas Graf  * Netlink Messages
535bfa83a9eSThomas Graf  **************************************************************************/
536bfa83a9eSThomas Graf 
537bfa83a9eSThomas Graf /**
538bfa83a9eSThomas Graf  * nlmsg_msg_size - length of netlink message not including padding
539bfa83a9eSThomas Graf  * @payload: length of message payload
540bfa83a9eSThomas Graf  */
541bfa83a9eSThomas Graf static inline int nlmsg_msg_size(int payload)
542bfa83a9eSThomas Graf {
543bfa83a9eSThomas Graf 	return NLMSG_HDRLEN + payload;
544bfa83a9eSThomas Graf }
545bfa83a9eSThomas Graf 
546bfa83a9eSThomas Graf /**
547bfa83a9eSThomas Graf  * nlmsg_total_size - length of netlink message including padding
548bfa83a9eSThomas Graf  * @payload: length of message payload
549bfa83a9eSThomas Graf  */
550bfa83a9eSThomas Graf static inline int nlmsg_total_size(int payload)
551bfa83a9eSThomas Graf {
552bfa83a9eSThomas Graf 	return NLMSG_ALIGN(nlmsg_msg_size(payload));
553bfa83a9eSThomas Graf }
554bfa83a9eSThomas Graf 
555bfa83a9eSThomas Graf /**
556bfa83a9eSThomas Graf  * nlmsg_padlen - length of padding at the message's tail
557bfa83a9eSThomas Graf  * @payload: length of message payload
558bfa83a9eSThomas Graf  */
559bfa83a9eSThomas Graf static inline int nlmsg_padlen(int payload)
560bfa83a9eSThomas Graf {
561bfa83a9eSThomas Graf 	return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
562bfa83a9eSThomas Graf }
563bfa83a9eSThomas Graf 
564bfa83a9eSThomas Graf /**
565bfa83a9eSThomas Graf  * nlmsg_data - head of message payload
56670f23fd6SJustin P. Mattock  * @nlh: netlink message header
567bfa83a9eSThomas Graf  */
568bfa83a9eSThomas Graf static inline void *nlmsg_data(const struct nlmsghdr *nlh)
569bfa83a9eSThomas Graf {
570bfa83a9eSThomas Graf 	return (unsigned char *) nlh + NLMSG_HDRLEN;
571bfa83a9eSThomas Graf }
572bfa83a9eSThomas Graf 
573bfa83a9eSThomas Graf /**
574bfa83a9eSThomas Graf  * nlmsg_len - length of message payload
575bfa83a9eSThomas Graf  * @nlh: netlink message header
576bfa83a9eSThomas Graf  */
577bfa83a9eSThomas Graf static inline int nlmsg_len(const struct nlmsghdr *nlh)
578bfa83a9eSThomas Graf {
579bfa83a9eSThomas Graf 	return nlh->nlmsg_len - NLMSG_HDRLEN;
580bfa83a9eSThomas Graf }
581bfa83a9eSThomas Graf 
582bfa83a9eSThomas Graf /**
583bfa83a9eSThomas Graf  * nlmsg_attrdata - head of attributes data
584bfa83a9eSThomas Graf  * @nlh: netlink message header
585bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
586bfa83a9eSThomas Graf  */
587bfa83a9eSThomas Graf static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
588bfa83a9eSThomas Graf 					    int hdrlen)
589bfa83a9eSThomas Graf {
590bfa83a9eSThomas Graf 	unsigned char *data = nlmsg_data(nlh);
591bfa83a9eSThomas Graf 	return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
592bfa83a9eSThomas Graf }
593bfa83a9eSThomas Graf 
594bfa83a9eSThomas Graf /**
595bfa83a9eSThomas Graf  * nlmsg_attrlen - length of attributes data
596bfa83a9eSThomas Graf  * @nlh: netlink message header
597bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
598bfa83a9eSThomas Graf  */
599bfa83a9eSThomas Graf static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
600bfa83a9eSThomas Graf {
601bfa83a9eSThomas Graf 	return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
602bfa83a9eSThomas Graf }
603bfa83a9eSThomas Graf 
604bfa83a9eSThomas Graf /**
605bfa83a9eSThomas Graf  * nlmsg_ok - check if the netlink message fits into the remaining bytes
606bfa83a9eSThomas Graf  * @nlh: netlink message header
607bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in message stream
608bfa83a9eSThomas Graf  */
609bfa83a9eSThomas Graf static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
610bfa83a9eSThomas Graf {
611619e803dSVegard Nossum 	return (remaining >= (int) sizeof(struct nlmsghdr) &&
612bfa83a9eSThomas Graf 		nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
613bfa83a9eSThomas Graf 		nlh->nlmsg_len <= remaining);
614bfa83a9eSThomas Graf }
615bfa83a9eSThomas Graf 
616bfa83a9eSThomas Graf /**
617bfa83a9eSThomas Graf  * nlmsg_next - next netlink message in message stream
618bfa83a9eSThomas Graf  * @nlh: netlink message header
619bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in message stream
620bfa83a9eSThomas Graf  *
621bfa83a9eSThomas Graf  * Returns the next netlink message in the message stream and
622bfa83a9eSThomas Graf  * decrements remaining by the size of the current message.
623bfa83a9eSThomas Graf  */
6243654654fSJan Engelhardt static inline struct nlmsghdr *
6253654654fSJan Engelhardt nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
626bfa83a9eSThomas Graf {
627bfa83a9eSThomas Graf 	int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
628bfa83a9eSThomas Graf 
629bfa83a9eSThomas Graf 	*remaining -= totlen;
630bfa83a9eSThomas Graf 
631bfa83a9eSThomas Graf 	return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
632bfa83a9eSThomas Graf }
633bfa83a9eSThomas Graf 
634bfa83a9eSThomas Graf /**
6353de64403SJohannes Berg  * nla_parse - Parse a stream of attributes into a tb buffer
6363de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
6373de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
6383de64403SJohannes Berg  * @head: head of attribute stream
6393de64403SJohannes Berg  * @len: length of attribute stream
6403de64403SJohannes Berg  * @policy: validation policy
6413de64403SJohannes Berg  * @extack: extended ACK pointer
6423de64403SJohannes Berg  *
6433de64403SJohannes Berg  * Parses a stream of attributes and stores a pointer to each attribute in
6443de64403SJohannes Berg  * the tb array accessible via the attribute type. Attributes with a type
6453de64403SJohannes Berg  * exceeding maxtype will be rejected, policy must be specified, attributes
6463de64403SJohannes Berg  * will be validated in the strictest way possible.
6473de64403SJohannes Berg  *
6483de64403SJohannes Berg  * Returns 0 on success or a negative error code.
6493de64403SJohannes Berg  */
6503de64403SJohannes Berg static inline int nla_parse(struct nlattr **tb, int maxtype,
6513de64403SJohannes Berg 			    const struct nlattr *head, int len,
6523de64403SJohannes Berg 			    const struct nla_policy *policy,
6533de64403SJohannes Berg 			    struct netlink_ext_ack *extack)
6543de64403SJohannes Berg {
6553de64403SJohannes Berg 	return __nla_parse(tb, maxtype, head, len, policy,
6563de64403SJohannes Berg 			   NL_VALIDATE_STRICT, extack);
6573de64403SJohannes Berg }
6583de64403SJohannes Berg 
6593de64403SJohannes Berg /**
6608cb08174SJohannes Berg  * nla_parse_deprecated - Parse a stream of attributes into a tb buffer
6618cb08174SJohannes Berg  * @tb: destination array with maxtype+1 elements
6628cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
6638cb08174SJohannes Berg  * @head: head of attribute stream
6648cb08174SJohannes Berg  * @len: length of attribute stream
6658cb08174SJohannes Berg  * @policy: validation policy
6668cb08174SJohannes Berg  * @extack: extended ACK pointer
6678cb08174SJohannes Berg  *
6688cb08174SJohannes Berg  * Parses a stream of attributes and stores a pointer to each attribute in
6698cb08174SJohannes Berg  * the tb array accessible via the attribute type. Attributes with a type
6708cb08174SJohannes Berg  * exceeding maxtype will be ignored and attributes from the policy are not
6718cb08174SJohannes Berg  * always strictly validated (only for new attributes).
6728cb08174SJohannes Berg  *
6738cb08174SJohannes Berg  * Returns 0 on success or a negative error code.
6748cb08174SJohannes Berg  */
6758cb08174SJohannes Berg static inline int nla_parse_deprecated(struct nlattr **tb, int maxtype,
6768cb08174SJohannes Berg 				       const struct nlattr *head, int len,
6778cb08174SJohannes Berg 				       const struct nla_policy *policy,
6788cb08174SJohannes Berg 				       struct netlink_ext_ack *extack)
6798cb08174SJohannes Berg {
6808cb08174SJohannes Berg 	return __nla_parse(tb, maxtype, head, len, policy,
6818cb08174SJohannes Berg 			   NL_VALIDATE_LIBERAL, extack);
6828cb08174SJohannes Berg }
6838cb08174SJohannes Berg 
6848cb08174SJohannes Berg /**
6858cb08174SJohannes Berg  * nla_parse_deprecated_strict - Parse a stream of attributes into a tb buffer
6868cb08174SJohannes Berg  * @tb: destination array with maxtype+1 elements
6878cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
6888cb08174SJohannes Berg  * @head: head of attribute stream
6898cb08174SJohannes Berg  * @len: length of attribute stream
6908cb08174SJohannes Berg  * @policy: validation policy
6918cb08174SJohannes Berg  * @extack: extended ACK pointer
6928cb08174SJohannes Berg  *
6938cb08174SJohannes Berg  * Parses a stream of attributes and stores a pointer to each attribute in
6948cb08174SJohannes Berg  * the tb array accessible via the attribute type. Attributes with a type
6958cb08174SJohannes Berg  * exceeding maxtype will be rejected as well as trailing data, but the
6968cb08174SJohannes Berg  * policy is not completely strictly validated (only for new attributes).
6978cb08174SJohannes Berg  *
6988cb08174SJohannes Berg  * Returns 0 on success or a negative error code.
6998cb08174SJohannes Berg  */
7008cb08174SJohannes Berg static inline int nla_parse_deprecated_strict(struct nlattr **tb, int maxtype,
7018cb08174SJohannes Berg 					      const struct nlattr *head,
7028cb08174SJohannes Berg 					      int len,
7038cb08174SJohannes Berg 					      const struct nla_policy *policy,
7048cb08174SJohannes Berg 					      struct netlink_ext_ack *extack)
7058cb08174SJohannes Berg {
7068cb08174SJohannes Berg 	return __nla_parse(tb, maxtype, head, len, policy,
7078cb08174SJohannes Berg 			   NL_VALIDATE_DEPRECATED_STRICT, extack);
7088cb08174SJohannes Berg }
7098cb08174SJohannes Berg 
7108cb08174SJohannes Berg /**
7118cb08174SJohannes Berg  * __nlmsg_parse - parse attributes of a netlink message
712bfa83a9eSThomas Graf  * @nlh: netlink message header
713bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
714bfa83a9eSThomas Graf  * @tb: destination array with maxtype+1 elements
715bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
716bfa83a9eSThomas Graf  * @policy: validation policy
7178cb08174SJohannes Berg  * @validate: validation strictness
718fceb6435SJohannes Berg  * @extack: extended ACK report struct
719bfa83a9eSThomas Graf  *
720bfa83a9eSThomas Graf  * See nla_parse()
721bfa83a9eSThomas Graf  */
7228cb08174SJohannes Berg static inline int __nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
723bfa83a9eSThomas Graf 				struct nlattr *tb[], int maxtype,
724fceb6435SJohannes Berg 				const struct nla_policy *policy,
7258cb08174SJohannes Berg 				unsigned int validate,
726fceb6435SJohannes Berg 				struct netlink_ext_ack *extack)
727bfa83a9eSThomas Graf {
7283d0d4337SDavid Ahern 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) {
7293d0d4337SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid header length");
730bfa83a9eSThomas Graf 		return -EINVAL;
7313d0d4337SDavid Ahern 	}
732bfa83a9eSThomas Graf 
7338cb08174SJohannes Berg 	return __nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
7348cb08174SJohannes Berg 			   nlmsg_attrlen(nlh, hdrlen), policy, validate,
7358cb08174SJohannes Berg 			   extack);
736bfa83a9eSThomas Graf }
737bfa83a9eSThomas Graf 
7388cb08174SJohannes Berg /**
7393de64403SJohannes Berg  * nlmsg_parse - parse attributes of a netlink message
7403de64403SJohannes Berg  * @nlh: netlink message header
7413de64403SJohannes Berg  * @hdrlen: length of family specific header
7423de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
7433de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
7443de64403SJohannes Berg  * @extack: extended ACK report struct
7453de64403SJohannes Berg  *
7463de64403SJohannes Berg  * See nla_parse()
7473de64403SJohannes Berg  */
7483de64403SJohannes Berg static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
7493de64403SJohannes Berg 			      struct nlattr *tb[], int maxtype,
7503de64403SJohannes Berg 			      const struct nla_policy *policy,
7513de64403SJohannes Berg 			      struct netlink_ext_ack *extack)
7523de64403SJohannes Berg {
753d00ee64eSDavid Ahern 	return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
7543de64403SJohannes Berg 			     NL_VALIDATE_STRICT, extack);
7553de64403SJohannes Berg }
7563de64403SJohannes Berg 
7573de64403SJohannes Berg /**
7588cb08174SJohannes Berg  * nlmsg_parse_deprecated - parse attributes of a netlink message
7598cb08174SJohannes Berg  * @nlh: netlink message header
7608cb08174SJohannes Berg  * @hdrlen: length of family specific header
7618cb08174SJohannes Berg  * @tb: destination array with maxtype+1 elements
7628cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
7638cb08174SJohannes Berg  * @extack: extended ACK report struct
7648cb08174SJohannes Berg  *
7658cb08174SJohannes Berg  * See nla_parse_deprecated()
7668cb08174SJohannes Berg  */
7678cb08174SJohannes Berg static inline int nlmsg_parse_deprecated(const struct nlmsghdr *nlh, int hdrlen,
768a5f6cba2SDavid Ahern 					 struct nlattr *tb[], int maxtype,
769a5f6cba2SDavid Ahern 					 const struct nla_policy *policy,
770a5f6cba2SDavid Ahern 					 struct netlink_ext_ack *extack)
771a5f6cba2SDavid Ahern {
7728cb08174SJohannes Berg 	return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
7738cb08174SJohannes Berg 			     NL_VALIDATE_LIBERAL, extack);
774a5f6cba2SDavid Ahern }
775a5f6cba2SDavid Ahern 
7768cb08174SJohannes Berg /**
7778cb08174SJohannes Berg  * nlmsg_parse_deprecated_strict - parse attributes of a netlink message
7788cb08174SJohannes Berg  * @nlh: netlink message header
7798cb08174SJohannes Berg  * @hdrlen: length of family specific header
7808cb08174SJohannes Berg  * @tb: destination array with maxtype+1 elements
7818cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
7828cb08174SJohannes Berg  * @extack: extended ACK report struct
7838cb08174SJohannes Berg  *
7848cb08174SJohannes Berg  * See nla_parse_deprecated_strict()
7858cb08174SJohannes Berg  */
7868cb08174SJohannes Berg static inline int
7878cb08174SJohannes Berg nlmsg_parse_deprecated_strict(const struct nlmsghdr *nlh, int hdrlen,
7888cb08174SJohannes Berg 			      struct nlattr *tb[], int maxtype,
7898cb08174SJohannes Berg 			      const struct nla_policy *policy,
7908cb08174SJohannes Berg 			      struct netlink_ext_ack *extack)
7918cb08174SJohannes Berg {
7928cb08174SJohannes Berg 	return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
7938cb08174SJohannes Berg 			     NL_VALIDATE_DEPRECATED_STRICT, extack);
794a5f6cba2SDavid Ahern }
795a5f6cba2SDavid Ahern 
796bfa83a9eSThomas Graf /**
797bfa83a9eSThomas Graf  * nlmsg_find_attr - find a specific attribute in a netlink message
798bfa83a9eSThomas Graf  * @nlh: netlink message header
799bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
800bfa83a9eSThomas Graf  * @attrtype: type of attribute to look for
801bfa83a9eSThomas Graf  *
802bfa83a9eSThomas Graf  * Returns the first attribute which matches the specified type.
803bfa83a9eSThomas Graf  */
8046b8c92baSNelson Elhage static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
805bfa83a9eSThomas Graf 					     int hdrlen, int attrtype)
806bfa83a9eSThomas Graf {
807bfa83a9eSThomas Graf 	return nla_find(nlmsg_attrdata(nlh, hdrlen),
808bfa83a9eSThomas Graf 			nlmsg_attrlen(nlh, hdrlen), attrtype);
809bfa83a9eSThomas Graf }
810bfa83a9eSThomas Graf 
811bfa83a9eSThomas Graf /**
8128cb08174SJohannes Berg  * nla_validate_deprecated - Validate a stream of attributes
8138cb08174SJohannes Berg  * @head: head of attribute stream
8148cb08174SJohannes Berg  * @len: length of attribute stream
8158cb08174SJohannes Berg  * @maxtype: maximum attribute type to be expected
8168cb08174SJohannes Berg  * @policy: validation policy
8178cb08174SJohannes Berg  * @validate: validation strictness
8188cb08174SJohannes Berg  * @extack: extended ACK report struct
8198cb08174SJohannes Berg  *
8208cb08174SJohannes Berg  * Validates all attributes in the specified attribute stream against the
8218cb08174SJohannes Berg  * specified policy. Validation is done in liberal mode.
8228cb08174SJohannes Berg  * See documenation of struct nla_policy for more details.
8238cb08174SJohannes Berg  *
8248cb08174SJohannes Berg  * Returns 0 on success or a negative error code.
8258cb08174SJohannes Berg  */
8268cb08174SJohannes Berg static inline int nla_validate_deprecated(const struct nlattr *head, int len,
8278cb08174SJohannes Berg 					  int maxtype,
8288cb08174SJohannes Berg 					  const struct nla_policy *policy,
8298cb08174SJohannes Berg 					  struct netlink_ext_ack *extack)
8308cb08174SJohannes Berg {
8318cb08174SJohannes Berg 	return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_LIBERAL,
8328cb08174SJohannes Berg 			      extack);
8338cb08174SJohannes Berg }
8348cb08174SJohannes Berg 
8353de64403SJohannes Berg /**
8363de64403SJohannes Berg  * nla_validate - Validate a stream of attributes
8373de64403SJohannes Berg  * @head: head of attribute stream
8383de64403SJohannes Berg  * @len: length of attribute stream
8393de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
8403de64403SJohannes Berg  * @policy: validation policy
8413de64403SJohannes Berg  * @extack: extended ACK report struct
8423de64403SJohannes Berg  *
8433de64403SJohannes Berg  * Validates all attributes in the specified attribute stream against the
8443de64403SJohannes Berg  * specified policy. Validation is done in strict mode.
8453de64403SJohannes Berg  * See documenation of struct nla_policy for more details.
8463de64403SJohannes Berg  *
8473de64403SJohannes Berg  * Returns 0 on success or a negative error code.
8483de64403SJohannes Berg  */
8493de64403SJohannes Berg static inline int nla_validate(const struct nlattr *head, int len, int maxtype,
8503de64403SJohannes Berg 			       const struct nla_policy *policy,
8513de64403SJohannes Berg 			       struct netlink_ext_ack *extack)
8523de64403SJohannes Berg {
8533de64403SJohannes Berg 	return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_STRICT,
8543de64403SJohannes Berg 			      extack);
8553de64403SJohannes Berg }
8568cb08174SJohannes Berg 
8578cb08174SJohannes Berg /**
8588cb08174SJohannes Berg  * nlmsg_validate_deprecated - validate a netlink message including attributes
859bfa83a9eSThomas Graf  * @nlh: netlinket message header
860bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
861bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
862bfa83a9eSThomas Graf  * @policy: validation policy
863fceb6435SJohannes Berg  * @extack: extended ACK report struct
864bfa83a9eSThomas Graf  */
8658cb08174SJohannes Berg static inline int nlmsg_validate_deprecated(const struct nlmsghdr *nlh,
8663654654fSJan Engelhardt 					    int hdrlen, int maxtype,
867fceb6435SJohannes Berg 					    const struct nla_policy *policy,
868fceb6435SJohannes Berg 					    struct netlink_ext_ack *extack)
869bfa83a9eSThomas Graf {
870bfa83a9eSThomas Graf 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
871bfa83a9eSThomas Graf 		return -EINVAL;
872bfa83a9eSThomas Graf 
8738cb08174SJohannes Berg 	return __nla_validate(nlmsg_attrdata(nlh, hdrlen),
8748cb08174SJohannes Berg 			      nlmsg_attrlen(nlh, hdrlen), maxtype,
8758cb08174SJohannes Berg 			      policy, NL_VALIDATE_LIBERAL, extack);
876bfa83a9eSThomas Graf }
877bfa83a9eSThomas Graf 
8788cb08174SJohannes Berg 
8798cb08174SJohannes Berg 
880bfa83a9eSThomas Graf /**
88197676b6bSThomas Graf  * nlmsg_report - need to report back to application?
88297676b6bSThomas Graf  * @nlh: netlink message header
88397676b6bSThomas Graf  *
88497676b6bSThomas Graf  * Returns 1 if a report back to the application is requested.
88597676b6bSThomas Graf  */
8863a6c2b41SPatrick McHardy static inline int nlmsg_report(const struct nlmsghdr *nlh)
88797676b6bSThomas Graf {
88897676b6bSThomas Graf 	return !!(nlh->nlmsg_flags & NLM_F_ECHO);
88997676b6bSThomas Graf }
89097676b6bSThomas Graf 
89197676b6bSThomas Graf /**
892bfa83a9eSThomas Graf  * nlmsg_for_each_attr - iterate over a stream of attributes
893bfa83a9eSThomas Graf  * @pos: loop counter, set to current attribute
894bfa83a9eSThomas Graf  * @nlh: netlink message header
895bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
896bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
897bfa83a9eSThomas Graf  */
898bfa83a9eSThomas Graf #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
899bfa83a9eSThomas Graf 	nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
900bfa83a9eSThomas Graf 			  nlmsg_attrlen(nlh, hdrlen), rem)
901bfa83a9eSThomas Graf 
902bfa83a9eSThomas Graf /**
903bfa83a9eSThomas Graf  * nlmsg_put - Add a new netlink message to an skb
904bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
9052c6ba4b1SNicolas Dichtel  * @portid: netlink PORTID of requesting application
906bfa83a9eSThomas Graf  * @seq: sequence number of message
907bfa83a9eSThomas Graf  * @type: message type
908bfa83a9eSThomas Graf  * @payload: length of message payload
909bfa83a9eSThomas Graf  * @flags: message flags
910bfa83a9eSThomas Graf  *
911bfa83a9eSThomas Graf  * Returns NULL if the tailroom of the skb is insufficient to store
912bfa83a9eSThomas Graf  * the message header and payload.
913bfa83a9eSThomas Graf  */
91415e47304SEric W. Biederman static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
915bfa83a9eSThomas Graf 					 int type, int payload, int flags)
916bfa83a9eSThomas Graf {
917bfa83a9eSThomas Graf 	if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
918bfa83a9eSThomas Graf 		return NULL;
919bfa83a9eSThomas Graf 
92015e47304SEric W. Biederman 	return __nlmsg_put(skb, portid, seq, type, payload, flags);
921bfa83a9eSThomas Graf }
922bfa83a9eSThomas Graf 
923bfa83a9eSThomas Graf /**
924bfa83a9eSThomas Graf  * nlmsg_put_answer - Add a new callback based netlink message to an skb
925bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
926bfa83a9eSThomas Graf  * @cb: netlink callback
927bfa83a9eSThomas Graf  * @type: message type
928bfa83a9eSThomas Graf  * @payload: length of message payload
929bfa83a9eSThomas Graf  * @flags: message flags
930bfa83a9eSThomas Graf  *
931bfa83a9eSThomas Graf  * Returns NULL if the tailroom of the skb is insufficient to store
932bfa83a9eSThomas Graf  * the message header and payload.
933bfa83a9eSThomas Graf  */
934bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
935bfa83a9eSThomas Graf 						struct netlink_callback *cb,
936bfa83a9eSThomas Graf 						int type, int payload,
937bfa83a9eSThomas Graf 						int flags)
938bfa83a9eSThomas Graf {
93915e47304SEric W. Biederman 	return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
940bfa83a9eSThomas Graf 			 type, payload, flags);
941bfa83a9eSThomas Graf }
942bfa83a9eSThomas Graf 
943bfa83a9eSThomas Graf /**
944bfa83a9eSThomas Graf  * nlmsg_new - Allocate a new netlink message
945339bf98fSThomas Graf  * @payload: size of the message payload
946fe4944e5SThomas Graf  * @flags: the type of memory to allocate.
947bfa83a9eSThomas Graf  *
948339bf98fSThomas Graf  * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
949339bf98fSThomas Graf  * and a good default is needed.
950bfa83a9eSThomas Graf  */
951339bf98fSThomas Graf static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
952bfa83a9eSThomas Graf {
953339bf98fSThomas Graf 	return alloc_skb(nlmsg_total_size(payload), flags);
954bfa83a9eSThomas Graf }
955bfa83a9eSThomas Graf 
956bfa83a9eSThomas Graf /**
957bfa83a9eSThomas Graf  * nlmsg_end - Finalize a netlink message
958bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
959bfa83a9eSThomas Graf  * @nlh: netlink message header
960bfa83a9eSThomas Graf  *
961bfa83a9eSThomas Graf  * Corrects the netlink message header to include the appeneded
962bfa83a9eSThomas Graf  * attributes. Only necessary if attributes have been added to
963bfa83a9eSThomas Graf  * the message.
964bfa83a9eSThomas Graf  */
965053c095aSJohannes Berg static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
966bfa83a9eSThomas Graf {
96727a884dcSArnaldo Carvalho de Melo 	nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
968bfa83a9eSThomas Graf }
969bfa83a9eSThomas Graf 
970bfa83a9eSThomas Graf /**
971fe4944e5SThomas Graf  * nlmsg_get_pos - return current position in netlink message
972fe4944e5SThomas Graf  * @skb: socket buffer the message is stored in
973fe4944e5SThomas Graf  *
974fe4944e5SThomas Graf  * Returns a pointer to the current tail of the message.
975fe4944e5SThomas Graf  */
976fe4944e5SThomas Graf static inline void *nlmsg_get_pos(struct sk_buff *skb)
977fe4944e5SThomas Graf {
97827a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
979fe4944e5SThomas Graf }
980fe4944e5SThomas Graf 
981fe4944e5SThomas Graf /**
982fe4944e5SThomas Graf  * nlmsg_trim - Trim message to a mark
983fe4944e5SThomas Graf  * @skb: socket buffer the message is stored in
984fe4944e5SThomas Graf  * @mark: mark to trim to
985fe4944e5SThomas Graf  *
986bc3ed28cSThomas Graf  * Trims the message to the provided mark.
987fe4944e5SThomas Graf  */
988bc3ed28cSThomas Graf static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
989fe4944e5SThomas Graf {
990149118d8SThomas Graf 	if (mark) {
991149118d8SThomas Graf 		WARN_ON((unsigned char *) mark < skb->data);
992fe4944e5SThomas Graf 		skb_trim(skb, (unsigned char *) mark - skb->data);
993fe4944e5SThomas Graf 	}
994149118d8SThomas Graf }
995fe4944e5SThomas Graf 
996fe4944e5SThomas Graf /**
997bfa83a9eSThomas Graf  * nlmsg_cancel - Cancel construction of a netlink message
998bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
999bfa83a9eSThomas Graf  * @nlh: netlink message header
1000bfa83a9eSThomas Graf  *
1001bfa83a9eSThomas Graf  * Removes the complete netlink message including all
1002bc3ed28cSThomas Graf  * attributes from the socket buffer again.
1003bfa83a9eSThomas Graf  */
1004bc3ed28cSThomas Graf static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
1005bfa83a9eSThomas Graf {
1006bc3ed28cSThomas Graf 	nlmsg_trim(skb, nlh);
1007bfa83a9eSThomas Graf }
1008bfa83a9eSThomas Graf 
1009bfa83a9eSThomas Graf /**
1010bfa83a9eSThomas Graf  * nlmsg_free - free a netlink message
1011bfa83a9eSThomas Graf  * @skb: socket buffer of netlink message
1012bfa83a9eSThomas Graf  */
1013bfa83a9eSThomas Graf static inline void nlmsg_free(struct sk_buff *skb)
1014bfa83a9eSThomas Graf {
1015bfa83a9eSThomas Graf 	kfree_skb(skb);
1016bfa83a9eSThomas Graf }
1017bfa83a9eSThomas Graf 
1018bfa83a9eSThomas Graf /**
1019bfa83a9eSThomas Graf  * nlmsg_multicast - multicast a netlink message
1020bfa83a9eSThomas Graf  * @sk: netlink socket to spread messages to
1021bfa83a9eSThomas Graf  * @skb: netlink message as socket buffer
102215e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
1023bfa83a9eSThomas Graf  * @group: multicast group id
1024d387f6adSThomas Graf  * @flags: allocation flags
1025bfa83a9eSThomas Graf  */
1026bfa83a9eSThomas Graf static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
102715e47304SEric W. Biederman 				  u32 portid, unsigned int group, gfp_t flags)
1028bfa83a9eSThomas Graf {
1029bfa83a9eSThomas Graf 	int err;
1030bfa83a9eSThomas Graf 
1031bfa83a9eSThomas Graf 	NETLINK_CB(skb).dst_group = group;
1032bfa83a9eSThomas Graf 
103315e47304SEric W. Biederman 	err = netlink_broadcast(sk, skb, portid, group, flags);
1034bfa83a9eSThomas Graf 	if (err > 0)
1035bfa83a9eSThomas Graf 		err = 0;
1036bfa83a9eSThomas Graf 
1037bfa83a9eSThomas Graf 	return err;
1038bfa83a9eSThomas Graf }
1039bfa83a9eSThomas Graf 
1040bfa83a9eSThomas Graf /**
1041bfa83a9eSThomas Graf  * nlmsg_unicast - unicast a netlink message
1042bfa83a9eSThomas Graf  * @sk: netlink socket to spread message to
1043bfa83a9eSThomas Graf  * @skb: netlink message as socket buffer
104415e47304SEric W. Biederman  * @portid: netlink portid of the destination socket
1045bfa83a9eSThomas Graf  */
104615e47304SEric W. Biederman static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
1047bfa83a9eSThomas Graf {
1048bfa83a9eSThomas Graf 	int err;
1049bfa83a9eSThomas Graf 
105015e47304SEric W. Biederman 	err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
1051bfa83a9eSThomas Graf 	if (err > 0)
1052bfa83a9eSThomas Graf 		err = 0;
1053bfa83a9eSThomas Graf 
1054bfa83a9eSThomas Graf 	return err;
1055bfa83a9eSThomas Graf }
1056bfa83a9eSThomas Graf 
1057bfa83a9eSThomas Graf /**
1058bfa83a9eSThomas Graf  * nlmsg_for_each_msg - iterate over a stream of messages
1059bfa83a9eSThomas Graf  * @pos: loop counter, set to current message
1060bfa83a9eSThomas Graf  * @head: head of message stream
1061bfa83a9eSThomas Graf  * @len: length of message stream
1062bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
1063bfa83a9eSThomas Graf  */
1064bfa83a9eSThomas Graf #define nlmsg_for_each_msg(pos, head, len, rem) \
1065bfa83a9eSThomas Graf 	for (pos = head, rem = len; \
1066bfa83a9eSThomas Graf 	     nlmsg_ok(pos, rem); \
1067bfa83a9eSThomas Graf 	     pos = nlmsg_next(pos, &(rem)))
1068bfa83a9eSThomas Graf 
1069670dc283SJohannes Berg /**
1070670dc283SJohannes Berg  * nl_dump_check_consistent - check if sequence is consistent and advertise if not
1071670dc283SJohannes Berg  * @cb: netlink callback structure that stores the sequence number
1072670dc283SJohannes Berg  * @nlh: netlink message header to write the flag to
1073670dc283SJohannes Berg  *
1074670dc283SJohannes Berg  * This function checks if the sequence (generation) number changed during dump
1075670dc283SJohannes Berg  * and if it did, advertises it in the netlink message header.
1076670dc283SJohannes Berg  *
1077670dc283SJohannes Berg  * The correct way to use it is to set cb->seq to the generation counter when
1078670dc283SJohannes Berg  * all locks for dumping have been acquired, and then call this function for
1079670dc283SJohannes Berg  * each message that is generated.
1080670dc283SJohannes Berg  *
1081670dc283SJohannes Berg  * Note that due to initialisation concerns, 0 is an invalid sequence number
1082670dc283SJohannes Berg  * and must not be used by code that uses this functionality.
1083670dc283SJohannes Berg  */
1084670dc283SJohannes Berg static inline void
1085670dc283SJohannes Berg nl_dump_check_consistent(struct netlink_callback *cb,
1086670dc283SJohannes Berg 			 struct nlmsghdr *nlh)
1087670dc283SJohannes Berg {
1088670dc283SJohannes Berg 	if (cb->prev_seq && cb->seq != cb->prev_seq)
1089670dc283SJohannes Berg 		nlh->nlmsg_flags |= NLM_F_DUMP_INTR;
1090670dc283SJohannes Berg 	cb->prev_seq = cb->seq;
1091670dc283SJohannes Berg }
1092670dc283SJohannes Berg 
1093bfa83a9eSThomas Graf /**************************************************************************
1094bfa83a9eSThomas Graf  * Netlink Attributes
1095bfa83a9eSThomas Graf  **************************************************************************/
1096bfa83a9eSThomas Graf 
1097bfa83a9eSThomas Graf /**
1098bfa83a9eSThomas Graf  * nla_attr_size - length of attribute not including padding
1099bfa83a9eSThomas Graf  * @payload: length of payload
1100bfa83a9eSThomas Graf  */
1101bfa83a9eSThomas Graf static inline int nla_attr_size(int payload)
1102bfa83a9eSThomas Graf {
1103bfa83a9eSThomas Graf 	return NLA_HDRLEN + payload;
1104bfa83a9eSThomas Graf }
1105bfa83a9eSThomas Graf 
1106bfa83a9eSThomas Graf /**
1107bfa83a9eSThomas Graf  * nla_total_size - total length of attribute including padding
1108bfa83a9eSThomas Graf  * @payload: length of payload
1109bfa83a9eSThomas Graf  */
1110bfa83a9eSThomas Graf static inline int nla_total_size(int payload)
1111bfa83a9eSThomas Graf {
1112bfa83a9eSThomas Graf 	return NLA_ALIGN(nla_attr_size(payload));
1113bfa83a9eSThomas Graf }
1114bfa83a9eSThomas Graf 
1115bfa83a9eSThomas Graf /**
1116bfa83a9eSThomas Graf  * nla_padlen - length of padding at the tail of attribute
1117bfa83a9eSThomas Graf  * @payload: length of payload
1118bfa83a9eSThomas Graf  */
1119bfa83a9eSThomas Graf static inline int nla_padlen(int payload)
1120bfa83a9eSThomas Graf {
1121bfa83a9eSThomas Graf 	return nla_total_size(payload) - nla_attr_size(payload);
1122bfa83a9eSThomas Graf }
1123bfa83a9eSThomas Graf 
1124bfa83a9eSThomas Graf /**
11258f4c1f9bSThomas Graf  * nla_type - attribute type
11268f4c1f9bSThomas Graf  * @nla: netlink attribute
11278f4c1f9bSThomas Graf  */
11288f4c1f9bSThomas Graf static inline int nla_type(const struct nlattr *nla)
11298f4c1f9bSThomas Graf {
11308f4c1f9bSThomas Graf 	return nla->nla_type & NLA_TYPE_MASK;
11318f4c1f9bSThomas Graf }
11328f4c1f9bSThomas Graf 
11338f4c1f9bSThomas Graf /**
1134bfa83a9eSThomas Graf  * nla_data - head of payload
1135bfa83a9eSThomas Graf  * @nla: netlink attribute
1136bfa83a9eSThomas Graf  */
1137bfa83a9eSThomas Graf static inline void *nla_data(const struct nlattr *nla)
1138bfa83a9eSThomas Graf {
1139bfa83a9eSThomas Graf 	return (char *) nla + NLA_HDRLEN;
1140bfa83a9eSThomas Graf }
1141bfa83a9eSThomas Graf 
1142bfa83a9eSThomas Graf /**
1143bfa83a9eSThomas Graf  * nla_len - length of payload
1144bfa83a9eSThomas Graf  * @nla: netlink attribute
1145bfa83a9eSThomas Graf  */
1146bfa83a9eSThomas Graf static inline int nla_len(const struct nlattr *nla)
1147bfa83a9eSThomas Graf {
1148bfa83a9eSThomas Graf 	return nla->nla_len - NLA_HDRLEN;
1149bfa83a9eSThomas Graf }
1150bfa83a9eSThomas Graf 
1151bfa83a9eSThomas Graf /**
1152bfa83a9eSThomas Graf  * nla_ok - check if the netlink attribute fits into the remaining bytes
1153bfa83a9eSThomas Graf  * @nla: netlink attribute
1154bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in attribute stream
1155bfa83a9eSThomas Graf  */
1156bfa83a9eSThomas Graf static inline int nla_ok(const struct nlattr *nla, int remaining)
1157bfa83a9eSThomas Graf {
11583e1ed981SAlexey Dobriyan 	return remaining >= (int) sizeof(*nla) &&
11593e1ed981SAlexey Dobriyan 	       nla->nla_len >= sizeof(*nla) &&
1160bfa83a9eSThomas Graf 	       nla->nla_len <= remaining;
1161bfa83a9eSThomas Graf }
1162bfa83a9eSThomas Graf 
1163bfa83a9eSThomas Graf /**
1164d1ec3b77SPierre Ynard  * nla_next - next netlink attribute in attribute stream
1165bfa83a9eSThomas Graf  * @nla: netlink attribute
1166bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in attribute stream
1167bfa83a9eSThomas Graf  *
1168bfa83a9eSThomas Graf  * Returns the next netlink attribute in the attribute stream and
1169bfa83a9eSThomas Graf  * decrements remaining by the size of the current attribute.
1170bfa83a9eSThomas Graf  */
1171bfa83a9eSThomas Graf static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
1172bfa83a9eSThomas Graf {
11733b2c75d3SAlexey Dobriyan 	unsigned int totlen = NLA_ALIGN(nla->nla_len);
1174bfa83a9eSThomas Graf 
1175bfa83a9eSThomas Graf 	*remaining -= totlen;
1176bfa83a9eSThomas Graf 	return (struct nlattr *) ((char *) nla + totlen);
1177bfa83a9eSThomas Graf }
1178bfa83a9eSThomas Graf 
1179bfa83a9eSThomas Graf /**
1180fe4944e5SThomas Graf  * nla_find_nested - find attribute in a set of nested attributes
1181fe4944e5SThomas Graf  * @nla: attribute containing the nested attributes
1182fe4944e5SThomas Graf  * @attrtype: type of attribute to look for
1183fe4944e5SThomas Graf  *
1184fe4944e5SThomas Graf  * Returns the first attribute which matches the specified type.
1185fe4944e5SThomas Graf  */
11863654654fSJan Engelhardt static inline struct nlattr *
11873654654fSJan Engelhardt nla_find_nested(const struct nlattr *nla, int attrtype)
1188fe4944e5SThomas Graf {
1189fe4944e5SThomas Graf 	return nla_find(nla_data(nla), nla_len(nla), attrtype);
1190fe4944e5SThomas Graf }
1191fe4944e5SThomas Graf 
1192fe4944e5SThomas Graf /**
11933de64403SJohannes Berg  * nla_parse_nested - parse nested attributes
11943de64403SJohannes Berg  * @tb: destination array with maxtype+1 elements
11953de64403SJohannes Berg  * @maxtype: maximum attribute type to be expected
11963de64403SJohannes Berg  * @nla: attribute containing the nested attributes
11973de64403SJohannes Berg  * @policy: validation policy
11983de64403SJohannes Berg  * @extack: extended ACK report struct
11993de64403SJohannes Berg  *
12003de64403SJohannes Berg  * See nla_parse()
12013de64403SJohannes Berg  */
12023de64403SJohannes Berg static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
12033de64403SJohannes Berg 				   const struct nlattr *nla,
12043de64403SJohannes Berg 				   const struct nla_policy *policy,
12053de64403SJohannes Berg 				   struct netlink_ext_ack *extack)
12063de64403SJohannes Berg {
1207b424e432SMichal Kubecek 	if (!(nla->nla_type & NLA_F_NESTED)) {
1208b424e432SMichal Kubecek 		NL_SET_ERR_MSG_ATTR(extack, nla, "NLA_F_NESTED is missing");
1209b424e432SMichal Kubecek 		return -EINVAL;
1210b424e432SMichal Kubecek 	}
1211b424e432SMichal Kubecek 
12123de64403SJohannes Berg 	return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
12133de64403SJohannes Berg 			   NL_VALIDATE_STRICT, extack);
12143de64403SJohannes Berg }
12153de64403SJohannes Berg 
12163de64403SJohannes Berg /**
12178cb08174SJohannes Berg  * nla_parse_nested_deprecated - parse nested attributes
1218bfa83a9eSThomas Graf  * @tb: destination array with maxtype+1 elements
1219bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
1220bfa83a9eSThomas Graf  * @nla: attribute containing the nested attributes
1221bfa83a9eSThomas Graf  * @policy: validation policy
1222fceb6435SJohannes Berg  * @extack: extended ACK report struct
1223bfa83a9eSThomas Graf  *
12248cb08174SJohannes Berg  * See nla_parse_deprecated()
1225bfa83a9eSThomas Graf  */
12268cb08174SJohannes Berg static inline int nla_parse_nested_deprecated(struct nlattr *tb[], int maxtype,
1227b057efd4SPatrick McHardy 					      const struct nlattr *nla,
1228fceb6435SJohannes Berg 					      const struct nla_policy *policy,
1229fceb6435SJohannes Berg 					      struct netlink_ext_ack *extack)
1230bfa83a9eSThomas Graf {
12318cb08174SJohannes Berg 	return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
12328cb08174SJohannes Berg 			   NL_VALIDATE_LIBERAL, extack);
1233bfa83a9eSThomas Graf }
12341092cb21SPatrick McHardy 
12351092cb21SPatrick McHardy /**
1236d1ec3b77SPierre Ynard  * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
1237bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1238bfa83a9eSThomas Graf  * @attrtype: attribute type
1239bfa83a9eSThomas Graf  * @value: numeric value
1240bfa83a9eSThomas Graf  */
1241bfa83a9eSThomas Graf static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
1242bfa83a9eSThomas Graf {
1243b4391db4SArnd Bergmann 	/* temporary variables to work around GCC PR81715 with asan-stack=1 */
1244b4391db4SArnd Bergmann 	u8 tmp = value;
1245b4391db4SArnd Bergmann 
1246b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(u8), &tmp);
1247bfa83a9eSThomas Graf }
1248bfa83a9eSThomas Graf 
1249bfa83a9eSThomas Graf /**
1250bfa83a9eSThomas Graf  * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
1251bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1252bfa83a9eSThomas Graf  * @attrtype: attribute type
1253bfa83a9eSThomas Graf  * @value: numeric value
1254bfa83a9eSThomas Graf  */
1255bfa83a9eSThomas Graf static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
1256bfa83a9eSThomas Graf {
1257b4391db4SArnd Bergmann 	u16 tmp = value;
1258b4391db4SArnd Bergmann 
1259b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(u16), &tmp);
1260bfa83a9eSThomas Graf }
1261bfa83a9eSThomas Graf 
1262bfa83a9eSThomas Graf /**
1263569a8fc3SDavid S. Miller  * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer
1264569a8fc3SDavid S. Miller  * @skb: socket buffer to add attribute to
1265569a8fc3SDavid S. Miller  * @attrtype: attribute type
1266569a8fc3SDavid S. Miller  * @value: numeric value
1267569a8fc3SDavid S. Miller  */
1268569a8fc3SDavid S. Miller static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
1269569a8fc3SDavid S. Miller {
1270b4391db4SArnd Bergmann 	__be16 tmp = value;
1271b4391db4SArnd Bergmann 
1272b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__be16), &tmp);
1273569a8fc3SDavid S. Miller }
1274569a8fc3SDavid S. Miller 
1275569a8fc3SDavid S. Miller /**
12766c1dd3b6SDavid S. Miller  * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer
12776c1dd3b6SDavid S. Miller  * @skb: socket buffer to add attribute to
12786c1dd3b6SDavid S. Miller  * @attrtype: attribute type
12796c1dd3b6SDavid S. Miller  * @value: numeric value
12806c1dd3b6SDavid S. Miller  */
12816c1dd3b6SDavid S. Miller static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
12826c1dd3b6SDavid S. Miller {
1283b4391db4SArnd Bergmann 	__be16 tmp = value;
1284b4391db4SArnd Bergmann 
1285b4391db4SArnd Bergmann 	return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
12866c1dd3b6SDavid S. Miller }
12876c1dd3b6SDavid S. Miller 
12886c1dd3b6SDavid S. Miller /**
128924c410dcSDavid S. Miller  * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer
129024c410dcSDavid S. Miller  * @skb: socket buffer to add attribute to
129124c410dcSDavid S. Miller  * @attrtype: attribute type
129224c410dcSDavid S. Miller  * @value: numeric value
129324c410dcSDavid S. Miller  */
129424c410dcSDavid S. Miller static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
129524c410dcSDavid S. Miller {
1296b4391db4SArnd Bergmann 	__le16 tmp = value;
1297b4391db4SArnd Bergmann 
1298b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__le16), &tmp);
129924c410dcSDavid S. Miller }
130024c410dcSDavid S. Miller 
130124c410dcSDavid S. Miller /**
1302bfa83a9eSThomas Graf  * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
1303bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1304bfa83a9eSThomas Graf  * @attrtype: attribute type
1305bfa83a9eSThomas Graf  * @value: numeric value
1306bfa83a9eSThomas Graf  */
1307bfa83a9eSThomas Graf static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
1308bfa83a9eSThomas Graf {
1309b4391db4SArnd Bergmann 	u32 tmp = value;
1310b4391db4SArnd Bergmann 
1311b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(u32), &tmp);
1312bfa83a9eSThomas Graf }
1313bfa83a9eSThomas Graf 
1314bfa83a9eSThomas Graf /**
1315569a8fc3SDavid S. Miller  * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer
1316569a8fc3SDavid S. Miller  * @skb: socket buffer to add attribute to
1317569a8fc3SDavid S. Miller  * @attrtype: attribute type
1318569a8fc3SDavid S. Miller  * @value: numeric value
1319569a8fc3SDavid S. Miller  */
1320569a8fc3SDavid S. Miller static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
1321569a8fc3SDavid S. Miller {
1322b4391db4SArnd Bergmann 	__be32 tmp = value;
1323b4391db4SArnd Bergmann 
1324b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__be32), &tmp);
1325569a8fc3SDavid S. Miller }
1326569a8fc3SDavid S. Miller 
1327569a8fc3SDavid S. Miller /**
13286c1dd3b6SDavid S. Miller  * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer
13296c1dd3b6SDavid S. Miller  * @skb: socket buffer to add attribute to
13306c1dd3b6SDavid S. Miller  * @attrtype: attribute type
13316c1dd3b6SDavid S. Miller  * @value: numeric value
13326c1dd3b6SDavid S. Miller  */
13336c1dd3b6SDavid S. Miller static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
13346c1dd3b6SDavid S. Miller {
1335b4391db4SArnd Bergmann 	__be32 tmp = value;
1336b4391db4SArnd Bergmann 
1337b4391db4SArnd Bergmann 	return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
13386c1dd3b6SDavid S. Miller }
13396c1dd3b6SDavid S. Miller 
13406c1dd3b6SDavid S. Miller /**
134124c410dcSDavid S. Miller  * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer
134224c410dcSDavid S. Miller  * @skb: socket buffer to add attribute to
134324c410dcSDavid S. Miller  * @attrtype: attribute type
134424c410dcSDavid S. Miller  * @value: numeric value
134524c410dcSDavid S. Miller  */
134624c410dcSDavid S. Miller static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value)
134724c410dcSDavid S. Miller {
1348b4391db4SArnd Bergmann 	__le32 tmp = value;
1349b4391db4SArnd Bergmann 
1350b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(__le32), &tmp);
135124c410dcSDavid S. Miller }
135224c410dcSDavid S. Miller 
135324c410dcSDavid S. Miller /**
135473520786SNicolas Dichtel  * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it
135573520786SNicolas Dichtel  * @skb: socket buffer to add attribute to
135673520786SNicolas Dichtel  * @attrtype: attribute type
135773520786SNicolas Dichtel  * @value: numeric value
135873520786SNicolas Dichtel  * @padattr: attribute type for the padding
135973520786SNicolas Dichtel  */
136073520786SNicolas Dichtel static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype,
136173520786SNicolas Dichtel 				    u64 value, int padattr)
136273520786SNicolas Dichtel {
1363b4391db4SArnd Bergmann 	u64 tmp = value;
1364b4391db4SArnd Bergmann 
1365b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
136673520786SNicolas Dichtel }
136773520786SNicolas Dichtel 
136873520786SNicolas Dichtel /**
1369b46f6dedSNicolas Dichtel  * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it
1370569a8fc3SDavid S. Miller  * @skb: socket buffer to add attribute to
1371569a8fc3SDavid S. Miller  * @attrtype: attribute type
1372569a8fc3SDavid S. Miller  * @value: numeric value
1373b46f6dedSNicolas Dichtel  * @padattr: attribute type for the padding
1374569a8fc3SDavid S. Miller  */
1375b46f6dedSNicolas Dichtel static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value,
1376b46f6dedSNicolas Dichtel 			       int padattr)
1377b46f6dedSNicolas Dichtel {
1378b4391db4SArnd Bergmann 	__be64 tmp = value;
1379b4391db4SArnd Bergmann 
1380b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr);
1381b46f6dedSNicolas Dichtel }
1382b46f6dedSNicolas Dichtel 
1383569a8fc3SDavid S. Miller /**
1384e9bbe898SNicolas Dichtel  * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it
13856c1dd3b6SDavid S. Miller  * @skb: socket buffer to add attribute to
13866c1dd3b6SDavid S. Miller  * @attrtype: attribute type
13876c1dd3b6SDavid S. Miller  * @value: numeric value
1388e9bbe898SNicolas Dichtel  * @padattr: attribute type for the padding
13896c1dd3b6SDavid S. Miller  */
1390e9bbe898SNicolas Dichtel static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value,
1391e9bbe898SNicolas Dichtel 				int padattr)
13926c1dd3b6SDavid S. Miller {
1393b4391db4SArnd Bergmann 	__be64 tmp = value;
1394b4391db4SArnd Bergmann 
1395b4391db4SArnd Bergmann 	return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp,
1396e9bbe898SNicolas Dichtel 			    padattr);
13976c1dd3b6SDavid S. Miller }
13986c1dd3b6SDavid S. Miller 
13996c1dd3b6SDavid S. Miller /**
1400e7479122SNicolas Dichtel  * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it
140124c410dcSDavid S. Miller  * @skb: socket buffer to add attribute to
140224c410dcSDavid S. Miller  * @attrtype: attribute type
140324c410dcSDavid S. Miller  * @value: numeric value
1404e7479122SNicolas Dichtel  * @padattr: attribute type for the padding
140524c410dcSDavid S. Miller  */
1406e7479122SNicolas Dichtel static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value,
1407e7479122SNicolas Dichtel 			       int padattr)
140824c410dcSDavid S. Miller {
1409b4391db4SArnd Bergmann 	__le64 tmp = value;
1410b4391db4SArnd Bergmann 
1411b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr);
141224c410dcSDavid S. Miller }
141324c410dcSDavid S. Miller 
141424c410dcSDavid S. Miller /**
14154778e0beSJiri Pirko  * nla_put_s8 - Add a s8 netlink attribute to a socket buffer
14164778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
14174778e0beSJiri Pirko  * @attrtype: attribute type
14184778e0beSJiri Pirko  * @value: numeric value
14194778e0beSJiri Pirko  */
14204778e0beSJiri Pirko static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
14214778e0beSJiri Pirko {
1422b4391db4SArnd Bergmann 	s8 tmp = value;
1423b4391db4SArnd Bergmann 
1424b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(s8), &tmp);
14254778e0beSJiri Pirko }
14264778e0beSJiri Pirko 
14274778e0beSJiri Pirko /**
14284778e0beSJiri Pirko  * nla_put_s16 - Add a s16 netlink attribute to a socket buffer
14294778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
14304778e0beSJiri Pirko  * @attrtype: attribute type
14314778e0beSJiri Pirko  * @value: numeric value
14324778e0beSJiri Pirko  */
14334778e0beSJiri Pirko static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
14344778e0beSJiri Pirko {
1435b4391db4SArnd Bergmann 	s16 tmp = value;
1436b4391db4SArnd Bergmann 
1437b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(s16), &tmp);
14384778e0beSJiri Pirko }
14394778e0beSJiri Pirko 
14404778e0beSJiri Pirko /**
14414778e0beSJiri Pirko  * nla_put_s32 - Add a s32 netlink attribute to a socket buffer
14424778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
14434778e0beSJiri Pirko  * @attrtype: attribute type
14444778e0beSJiri Pirko  * @value: numeric value
14454778e0beSJiri Pirko  */
14464778e0beSJiri Pirko static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
14474778e0beSJiri Pirko {
1448b4391db4SArnd Bergmann 	s32 tmp = value;
1449b4391db4SArnd Bergmann 
1450b4391db4SArnd Bergmann 	return nla_put(skb, attrtype, sizeof(s32), &tmp);
14514778e0beSJiri Pirko }
14524778e0beSJiri Pirko 
14534778e0beSJiri Pirko /**
1454756a2f59SNicolas Dichtel  * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it
14554778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
14564778e0beSJiri Pirko  * @attrtype: attribute type
14574778e0beSJiri Pirko  * @value: numeric value
1458756a2f59SNicolas Dichtel  * @padattr: attribute type for the padding
14594778e0beSJiri Pirko  */
1460756a2f59SNicolas Dichtel static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value,
1461756a2f59SNicolas Dichtel 			      int padattr)
14624778e0beSJiri Pirko {
1463b4391db4SArnd Bergmann 	s64 tmp = value;
1464b4391db4SArnd Bergmann 
1465b4391db4SArnd Bergmann 	return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr);
14664778e0beSJiri Pirko }
14674778e0beSJiri Pirko 
14684778e0beSJiri Pirko /**
1469bfa83a9eSThomas Graf  * nla_put_string - Add a string netlink attribute to a socket buffer
1470bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1471bfa83a9eSThomas Graf  * @attrtype: attribute type
1472bfa83a9eSThomas Graf  * @str: NUL terminated string
1473bfa83a9eSThomas Graf  */
1474bfa83a9eSThomas Graf static inline int nla_put_string(struct sk_buff *skb, int attrtype,
1475bfa83a9eSThomas Graf 				 const char *str)
1476bfa83a9eSThomas Graf {
1477bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, strlen(str) + 1, str);
1478bfa83a9eSThomas Graf }
1479bfa83a9eSThomas Graf 
1480bfa83a9eSThomas Graf /**
1481bfa83a9eSThomas Graf  * nla_put_flag - Add a flag netlink attribute to a socket buffer
1482bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1483bfa83a9eSThomas Graf  * @attrtype: attribute type
1484bfa83a9eSThomas Graf  */
1485bfa83a9eSThomas Graf static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
1486bfa83a9eSThomas Graf {
1487bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, 0, NULL);
1488bfa83a9eSThomas Graf }
1489bfa83a9eSThomas Graf 
1490bfa83a9eSThomas Graf /**
14912175d87cSNicolas Dichtel  * nla_put_msecs - Add a msecs netlink attribute to a skb and align it
1492bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
1493bfa83a9eSThomas Graf  * @attrtype: attribute type
1494d87de1f3SMark Rustad  * @njiffies: number of jiffies to convert to msecs
14952175d87cSNicolas Dichtel  * @padattr: attribute type for the padding
1496bfa83a9eSThomas Graf  */
1497bfa83a9eSThomas Graf static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
14982175d87cSNicolas Dichtel 				unsigned long njiffies, int padattr)
1499bfa83a9eSThomas Graf {
1500d87de1f3SMark Rustad 	u64 tmp = jiffies_to_msecs(njiffies);
15012175d87cSNicolas Dichtel 
15022175d87cSNicolas Dichtel 	return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
1503bfa83a9eSThomas Graf }
1504bfa83a9eSThomas Graf 
1505bfa83a9eSThomas Graf /**
1506930345eaSJiri Benc  * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket
1507930345eaSJiri Benc  * buffer
1508930345eaSJiri Benc  * @skb: socket buffer to add attribute to
1509930345eaSJiri Benc  * @attrtype: attribute type
1510930345eaSJiri Benc  * @addr: IPv4 address
1511930345eaSJiri Benc  */
1512930345eaSJiri Benc static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
1513930345eaSJiri Benc 				  __be32 addr)
1514930345eaSJiri Benc {
1515b4391db4SArnd Bergmann 	__be32 tmp = addr;
1516b4391db4SArnd Bergmann 
1517b4391db4SArnd Bergmann 	return nla_put_be32(skb, attrtype, tmp);
1518930345eaSJiri Benc }
1519930345eaSJiri Benc 
1520930345eaSJiri Benc /**
1521930345eaSJiri Benc  * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket
1522930345eaSJiri Benc  * buffer
1523930345eaSJiri Benc  * @skb: socket buffer to add attribute to
1524930345eaSJiri Benc  * @attrtype: attribute type
1525930345eaSJiri Benc  * @addr: IPv6 address
1526930345eaSJiri Benc  */
1527930345eaSJiri Benc static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype,
1528930345eaSJiri Benc 				   const struct in6_addr *addr)
1529930345eaSJiri Benc {
1530930345eaSJiri Benc 	return nla_put(skb, attrtype, sizeof(*addr), addr);
1531930345eaSJiri Benc }
1532930345eaSJiri Benc 
1533930345eaSJiri Benc /**
15348953b077SJiri Pirko  * nla_put_bitfield32 - Add a bitfield32 netlink attribute to a socket buffer
15358953b077SJiri Pirko  * @skb: socket buffer to add attribute to
15368953b077SJiri Pirko  * @attrtype: attribute type
15378953b077SJiri Pirko  * @value: value carrying bits
15388953b077SJiri Pirko  * @selector: selector of valid bits
15398953b077SJiri Pirko  */
15408953b077SJiri Pirko static inline int nla_put_bitfield32(struct sk_buff *skb, int attrtype,
15418953b077SJiri Pirko 				     __u32 value, __u32 selector)
15428953b077SJiri Pirko {
15438953b077SJiri Pirko 	struct nla_bitfield32 tmp = { value, selector, };
15448953b077SJiri Pirko 
15458953b077SJiri Pirko 	return nla_put(skb, attrtype, sizeof(tmp), &tmp);
15468953b077SJiri Pirko }
15478953b077SJiri Pirko 
15488953b077SJiri Pirko /**
1549bfa83a9eSThomas Graf  * nla_get_u32 - return payload of u32 attribute
1550bfa83a9eSThomas Graf  * @nla: u32 netlink attribute
1551bfa83a9eSThomas Graf  */
1552b057efd4SPatrick McHardy static inline u32 nla_get_u32(const struct nlattr *nla)
1553bfa83a9eSThomas Graf {
1554bfa83a9eSThomas Graf 	return *(u32 *) nla_data(nla);
1555bfa83a9eSThomas Graf }
1556bfa83a9eSThomas Graf 
1557bfa83a9eSThomas Graf /**
155800012e5bSAl Viro  * nla_get_be32 - return payload of __be32 attribute
155900012e5bSAl Viro  * @nla: __be32 netlink attribute
156000012e5bSAl Viro  */
1561b057efd4SPatrick McHardy static inline __be32 nla_get_be32(const struct nlattr *nla)
156200012e5bSAl Viro {
156300012e5bSAl Viro 	return *(__be32 *) nla_data(nla);
156400012e5bSAl Viro }
156500012e5bSAl Viro 
156600012e5bSAl Viro /**
1567c648a013SAlexander Aring  * nla_get_le32 - return payload of __le32 attribute
1568c648a013SAlexander Aring  * @nla: __le32 netlink attribute
1569c648a013SAlexander Aring  */
1570c648a013SAlexander Aring static inline __le32 nla_get_le32(const struct nlattr *nla)
1571c648a013SAlexander Aring {
1572c648a013SAlexander Aring 	return *(__le32 *) nla_data(nla);
1573c648a013SAlexander Aring }
1574c648a013SAlexander Aring 
1575c648a013SAlexander Aring /**
1576bfa83a9eSThomas Graf  * nla_get_u16 - return payload of u16 attribute
1577bfa83a9eSThomas Graf  * @nla: u16 netlink attribute
1578bfa83a9eSThomas Graf  */
1579b057efd4SPatrick McHardy static inline u16 nla_get_u16(const struct nlattr *nla)
1580bfa83a9eSThomas Graf {
1581bfa83a9eSThomas Graf 	return *(u16 *) nla_data(nla);
1582bfa83a9eSThomas Graf }
1583bfa83a9eSThomas Graf 
1584bfa83a9eSThomas Graf /**
1585838965baSPatrick McHardy  * nla_get_be16 - return payload of __be16 attribute
1586838965baSPatrick McHardy  * @nla: __be16 netlink attribute
1587838965baSPatrick McHardy  */
1588b057efd4SPatrick McHardy static inline __be16 nla_get_be16(const struct nlattr *nla)
1589838965baSPatrick McHardy {
1590838965baSPatrick McHardy 	return *(__be16 *) nla_data(nla);
1591838965baSPatrick McHardy }
1592838965baSPatrick McHardy 
1593838965baSPatrick McHardy /**
15944a89c256SThomas Graf  * nla_get_le16 - return payload of __le16 attribute
15954a89c256SThomas Graf  * @nla: __le16 netlink attribute
15964a89c256SThomas Graf  */
1597b057efd4SPatrick McHardy static inline __le16 nla_get_le16(const struct nlattr *nla)
15984a89c256SThomas Graf {
15994a89c256SThomas Graf 	return *(__le16 *) nla_data(nla);
16004a89c256SThomas Graf }
16014a89c256SThomas Graf 
16024a89c256SThomas Graf /**
1603bfa83a9eSThomas Graf  * nla_get_u8 - return payload of u8 attribute
1604bfa83a9eSThomas Graf  * @nla: u8 netlink attribute
1605bfa83a9eSThomas Graf  */
1606b057efd4SPatrick McHardy static inline u8 nla_get_u8(const struct nlattr *nla)
1607bfa83a9eSThomas Graf {
1608bfa83a9eSThomas Graf 	return *(u8 *) nla_data(nla);
1609bfa83a9eSThomas Graf }
1610bfa83a9eSThomas Graf 
1611bfa83a9eSThomas Graf /**
1612bfa83a9eSThomas Graf  * nla_get_u64 - return payload of u64 attribute
1613bfa83a9eSThomas Graf  * @nla: u64 netlink attribute
1614bfa83a9eSThomas Graf  */
1615b057efd4SPatrick McHardy static inline u64 nla_get_u64(const struct nlattr *nla)
1616bfa83a9eSThomas Graf {
1617bfa83a9eSThomas Graf 	u64 tmp;
1618bfa83a9eSThomas Graf 
1619bfa83a9eSThomas Graf 	nla_memcpy(&tmp, nla, sizeof(tmp));
1620bfa83a9eSThomas Graf 
1621bfa83a9eSThomas Graf 	return tmp;
1622bfa83a9eSThomas Graf }
1623bfa83a9eSThomas Graf 
1624bfa83a9eSThomas Graf /**
1625a17c8598SPablo Neira Ayuso  * nla_get_be64 - return payload of __be64 attribute
1626a17c8598SPablo Neira Ayuso  * @nla: __be64 netlink attribute
1627a17c8598SPablo Neira Ayuso  */
1628a17c8598SPablo Neira Ayuso static inline __be64 nla_get_be64(const struct nlattr *nla)
1629a17c8598SPablo Neira Ayuso {
1630f5d410f2SPablo Neira Ayuso 	__be64 tmp;
1631f5d410f2SPablo Neira Ayuso 
1632f5d410f2SPablo Neira Ayuso 	nla_memcpy(&tmp, nla, sizeof(tmp));
1633f5d410f2SPablo Neira Ayuso 
1634f5d410f2SPablo Neira Ayuso 	return tmp;
1635a17c8598SPablo Neira Ayuso }
1636a17c8598SPablo Neira Ayuso 
1637a17c8598SPablo Neira Ayuso /**
1638c648a013SAlexander Aring  * nla_get_le64 - return payload of __le64 attribute
1639c648a013SAlexander Aring  * @nla: __le64 netlink attribute
1640c648a013SAlexander Aring  */
1641c648a013SAlexander Aring static inline __le64 nla_get_le64(const struct nlattr *nla)
1642c648a013SAlexander Aring {
1643c648a013SAlexander Aring 	return *(__le64 *) nla_data(nla);
1644c648a013SAlexander Aring }
1645c648a013SAlexander Aring 
1646c648a013SAlexander Aring /**
16474778e0beSJiri Pirko  * nla_get_s32 - return payload of s32 attribute
16484778e0beSJiri Pirko  * @nla: s32 netlink attribute
16494778e0beSJiri Pirko  */
16504778e0beSJiri Pirko static inline s32 nla_get_s32(const struct nlattr *nla)
16514778e0beSJiri Pirko {
16524778e0beSJiri Pirko 	return *(s32 *) nla_data(nla);
16534778e0beSJiri Pirko }
16544778e0beSJiri Pirko 
16554778e0beSJiri Pirko /**
16564778e0beSJiri Pirko  * nla_get_s16 - return payload of s16 attribute
16574778e0beSJiri Pirko  * @nla: s16 netlink attribute
16584778e0beSJiri Pirko  */
16594778e0beSJiri Pirko static inline s16 nla_get_s16(const struct nlattr *nla)
16604778e0beSJiri Pirko {
16614778e0beSJiri Pirko 	return *(s16 *) nla_data(nla);
16624778e0beSJiri Pirko }
16634778e0beSJiri Pirko 
16644778e0beSJiri Pirko /**
16654778e0beSJiri Pirko  * nla_get_s8 - return payload of s8 attribute
16664778e0beSJiri Pirko  * @nla: s8 netlink attribute
16674778e0beSJiri Pirko  */
16684778e0beSJiri Pirko static inline s8 nla_get_s8(const struct nlattr *nla)
16694778e0beSJiri Pirko {
16704778e0beSJiri Pirko 	return *(s8 *) nla_data(nla);
16714778e0beSJiri Pirko }
16724778e0beSJiri Pirko 
16734778e0beSJiri Pirko /**
16744778e0beSJiri Pirko  * nla_get_s64 - return payload of s64 attribute
16754778e0beSJiri Pirko  * @nla: s64 netlink attribute
16764778e0beSJiri Pirko  */
16774778e0beSJiri Pirko static inline s64 nla_get_s64(const struct nlattr *nla)
16784778e0beSJiri Pirko {
16794778e0beSJiri Pirko 	s64 tmp;
16804778e0beSJiri Pirko 
16814778e0beSJiri Pirko 	nla_memcpy(&tmp, nla, sizeof(tmp));
16824778e0beSJiri Pirko 
16834778e0beSJiri Pirko 	return tmp;
16844778e0beSJiri Pirko }
16854778e0beSJiri Pirko 
16864778e0beSJiri Pirko /**
1687bfa83a9eSThomas Graf  * nla_get_flag - return payload of flag attribute
1688bfa83a9eSThomas Graf  * @nla: flag netlink attribute
1689bfa83a9eSThomas Graf  */
1690b057efd4SPatrick McHardy static inline int nla_get_flag(const struct nlattr *nla)
1691bfa83a9eSThomas Graf {
1692bfa83a9eSThomas Graf 	return !!nla;
1693bfa83a9eSThomas Graf }
1694bfa83a9eSThomas Graf 
1695bfa83a9eSThomas Graf /**
1696bfa83a9eSThomas Graf  * nla_get_msecs - return payload of msecs attribute
1697bfa83a9eSThomas Graf  * @nla: msecs netlink attribute
1698bfa83a9eSThomas Graf  *
1699bfa83a9eSThomas Graf  * Returns the number of milliseconds in jiffies.
1700bfa83a9eSThomas Graf  */
1701b057efd4SPatrick McHardy static inline unsigned long nla_get_msecs(const struct nlattr *nla)
1702bfa83a9eSThomas Graf {
1703bfa83a9eSThomas Graf 	u64 msecs = nla_get_u64(nla);
1704bfa83a9eSThomas Graf 
1705bfa83a9eSThomas Graf 	return msecs_to_jiffies((unsigned long) msecs);
1706bfa83a9eSThomas Graf }
1707bfa83a9eSThomas Graf 
1708bfa83a9eSThomas Graf /**
170967b61f6cSJiri Benc  * nla_get_in_addr - return payload of IPv4 address attribute
171067b61f6cSJiri Benc  * @nla: IPv4 address netlink attribute
171167b61f6cSJiri Benc  */
171267b61f6cSJiri Benc static inline __be32 nla_get_in_addr(const struct nlattr *nla)
171367b61f6cSJiri Benc {
171467b61f6cSJiri Benc 	return *(__be32 *) nla_data(nla);
171567b61f6cSJiri Benc }
171667b61f6cSJiri Benc 
171767b61f6cSJiri Benc /**
171867b61f6cSJiri Benc  * nla_get_in6_addr - return payload of IPv6 address attribute
171967b61f6cSJiri Benc  * @nla: IPv6 address netlink attribute
172067b61f6cSJiri Benc  */
172167b61f6cSJiri Benc static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla)
172267b61f6cSJiri Benc {
172367b61f6cSJiri Benc 	struct in6_addr tmp;
172467b61f6cSJiri Benc 
172567b61f6cSJiri Benc 	nla_memcpy(&tmp, nla, sizeof(tmp));
172667b61f6cSJiri Benc 	return tmp;
172767b61f6cSJiri Benc }
172867b61f6cSJiri Benc 
172967b61f6cSJiri Benc /**
173064c83d83SJamal Hadi Salim  * nla_get_bitfield32 - return payload of 32 bitfield attribute
173164c83d83SJamal Hadi Salim  * @nla: nla_bitfield32 attribute
173264c83d83SJamal Hadi Salim  */
173364c83d83SJamal Hadi Salim static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla)
173464c83d83SJamal Hadi Salim {
173564c83d83SJamal Hadi Salim 	struct nla_bitfield32 tmp;
173664c83d83SJamal Hadi Salim 
173764c83d83SJamal Hadi Salim 	nla_memcpy(&tmp, nla, sizeof(tmp));
173864c83d83SJamal Hadi Salim 	return tmp;
173964c83d83SJamal Hadi Salim }
174064c83d83SJamal Hadi Salim 
174164c83d83SJamal Hadi Salim /**
1742b15ca182SThomas Graf  * nla_memdup - duplicate attribute memory (kmemdup)
1743b15ca182SThomas Graf  * @src: netlink attribute to duplicate from
1744b15ca182SThomas Graf  * @gfp: GFP mask
1745b15ca182SThomas Graf  */
1746b15ca182SThomas Graf static inline void *nla_memdup(const struct nlattr *src, gfp_t gfp)
1747b15ca182SThomas Graf {
1748b15ca182SThomas Graf 	return kmemdup(nla_data(src), nla_len(src), gfp);
1749b15ca182SThomas Graf }
1750b15ca182SThomas Graf 
1751b15ca182SThomas Graf /**
1752ae0be8deSMichal Kubecek  * nla_nest_start_noflag - Start a new level of nested attributes
1753bfa83a9eSThomas Graf  * @skb: socket buffer to add attributes to
1754bfa83a9eSThomas Graf  * @attrtype: attribute type of container
1755bfa83a9eSThomas Graf  *
1756ae0be8deSMichal Kubecek  * This function exists for backward compatibility to use in APIs which never
1757ae0be8deSMichal Kubecek  * marked their nest attributes with NLA_F_NESTED flag. New APIs should use
1758ae0be8deSMichal Kubecek  * nla_nest_start() which sets the flag.
1759ae0be8deSMichal Kubecek  *
1760ae0be8deSMichal Kubecek  * Returns the container attribute or NULL on error
1761bfa83a9eSThomas Graf  */
1762ae0be8deSMichal Kubecek static inline struct nlattr *nla_nest_start_noflag(struct sk_buff *skb,
1763ae0be8deSMichal Kubecek 						   int attrtype)
1764bfa83a9eSThomas Graf {
176527a884dcSArnaldo Carvalho de Melo 	struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
1766bfa83a9eSThomas Graf 
1767bfa83a9eSThomas Graf 	if (nla_put(skb, attrtype, 0, NULL) < 0)
1768bfa83a9eSThomas Graf 		return NULL;
1769bfa83a9eSThomas Graf 
1770bfa83a9eSThomas Graf 	return start;
1771bfa83a9eSThomas Graf }
1772bfa83a9eSThomas Graf 
1773bfa83a9eSThomas Graf /**
1774ae0be8deSMichal Kubecek  * nla_nest_start - Start a new level of nested attributes, with NLA_F_NESTED
1775ae0be8deSMichal Kubecek  * @skb: socket buffer to add attributes to
1776ae0be8deSMichal Kubecek  * @attrtype: attribute type of container
1777ae0be8deSMichal Kubecek  *
1778ae0be8deSMichal Kubecek  * Unlike nla_nest_start_noflag(), mark the nest attribute with NLA_F_NESTED
1779ae0be8deSMichal Kubecek  * flag. This is the preferred function to use in new code.
1780ae0be8deSMichal Kubecek  *
1781ae0be8deSMichal Kubecek  * Returns the container attribute or NULL on error
1782ae0be8deSMichal Kubecek  */
1783ae0be8deSMichal Kubecek static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
1784ae0be8deSMichal Kubecek {
1785ae0be8deSMichal Kubecek 	return nla_nest_start_noflag(skb, attrtype | NLA_F_NESTED);
1786ae0be8deSMichal Kubecek }
1787ae0be8deSMichal Kubecek 
1788ae0be8deSMichal Kubecek /**
1789bfa83a9eSThomas Graf  * nla_nest_end - Finalize nesting of attributes
1790d1ec3b77SPierre Ynard  * @skb: socket buffer the attributes are stored in
1791bfa83a9eSThomas Graf  * @start: container attribute
1792bfa83a9eSThomas Graf  *
1793bfa83a9eSThomas Graf  * Corrects the container attribute header to include the all
1794bfa83a9eSThomas Graf  * appeneded attributes.
1795bfa83a9eSThomas Graf  *
1796bfa83a9eSThomas Graf  * Returns the total data length of the skb.
1797bfa83a9eSThomas Graf  */
1798bfa83a9eSThomas Graf static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
1799bfa83a9eSThomas Graf {
180027a884dcSArnaldo Carvalho de Melo 	start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
1801bfa83a9eSThomas Graf 	return skb->len;
1802bfa83a9eSThomas Graf }
1803bfa83a9eSThomas Graf 
1804bfa83a9eSThomas Graf /**
1805bfa83a9eSThomas Graf  * nla_nest_cancel - Cancel nesting of attributes
1806bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
1807bfa83a9eSThomas Graf  * @start: container attribute
1808bfa83a9eSThomas Graf  *
1809bfa83a9eSThomas Graf  * Removes the container attribute and including all nested
1810bc3ed28cSThomas Graf  * attributes. Returns -EMSGSIZE
1811bfa83a9eSThomas Graf  */
1812bc3ed28cSThomas Graf static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
1813bfa83a9eSThomas Graf {
1814bc3ed28cSThomas Graf 	nlmsg_trim(skb, start);
1815bfa83a9eSThomas Graf }
1816bfa83a9eSThomas Graf 
1817bfa83a9eSThomas Graf /**
181832d5109aSMichal Kubecek  * __nla_validate_nested - Validate a stream of nested attributes
18194fe5d5c0SPaul Moore  * @start: container attribute
18204fe5d5c0SPaul Moore  * @maxtype: maximum attribute type to be expected
18214fe5d5c0SPaul Moore  * @policy: validation policy
18228cb08174SJohannes Berg  * @validate: validation strictness
1823fceb6435SJohannes Berg  * @extack: extended ACK report struct
18244fe5d5c0SPaul Moore  *
18254fe5d5c0SPaul Moore  * Validates all attributes in the nested attribute stream against the
18264fe5d5c0SPaul Moore  * specified policy. Attributes with a type exceeding maxtype will be
18274fe5d5c0SPaul Moore  * ignored. See documenation of struct nla_policy for more details.
18284fe5d5c0SPaul Moore  *
18294fe5d5c0SPaul Moore  * Returns 0 on success or a negative error code.
18304fe5d5c0SPaul Moore  */
18318cb08174SJohannes Berg static inline int __nla_validate_nested(const struct nlattr *start, int maxtype,
18328cb08174SJohannes Berg 					const struct nla_policy *policy,
18338cb08174SJohannes Berg 					unsigned int validate,
18348cb08174SJohannes Berg 					struct netlink_ext_ack *extack)
18358cb08174SJohannes Berg {
18368cb08174SJohannes Berg 	return __nla_validate(nla_data(start), nla_len(start), maxtype, policy,
18378cb08174SJohannes Berg 			      validate, extack);
18388cb08174SJohannes Berg }
18398cb08174SJohannes Berg 
18408cb08174SJohannes Berg static inline int
184132d5109aSMichal Kubecek nla_validate_nested(const struct nlattr *start, int maxtype,
1842901bb989SJohannes Berg 		    const struct nla_policy *policy,
1843901bb989SJohannes Berg 		    struct netlink_ext_ack *extack)
1844901bb989SJohannes Berg {
1845901bb989SJohannes Berg 	return __nla_validate_nested(start, maxtype, policy,
1846901bb989SJohannes Berg 				     NL_VALIDATE_STRICT, extack);
1847901bb989SJohannes Berg }
1848901bb989SJohannes Berg 
1849901bb989SJohannes Berg static inline int
18508cb08174SJohannes Berg nla_validate_nested_deprecated(const struct nlattr *start, int maxtype,
1851fceb6435SJohannes Berg 			       const struct nla_policy *policy,
1852fceb6435SJohannes Berg 			       struct netlink_ext_ack *extack)
18534fe5d5c0SPaul Moore {
18548cb08174SJohannes Berg 	return __nla_validate_nested(start, maxtype, policy,
18558cb08174SJohannes Berg 				     NL_VALIDATE_LIBERAL, extack);
18564fe5d5c0SPaul Moore }
18574fe5d5c0SPaul Moore 
18584fe5d5c0SPaul Moore /**
1859089bf1a6SNicolas Dichtel  * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute
1860089bf1a6SNicolas Dichtel  * @skb: socket buffer the message is stored in
1861089bf1a6SNicolas Dichtel  *
1862089bf1a6SNicolas Dichtel  * Return true if padding is needed to align the next attribute (nla_data()) to
1863089bf1a6SNicolas Dichtel  * a 64-bit aligned area.
1864089bf1a6SNicolas Dichtel  */
1865089bf1a6SNicolas Dichtel static inline bool nla_need_padding_for_64bit(struct sk_buff *skb)
1866089bf1a6SNicolas Dichtel {
1867089bf1a6SNicolas Dichtel #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1868089bf1a6SNicolas Dichtel 	/* The nlattr header is 4 bytes in size, that's why we test
1869089bf1a6SNicolas Dichtel 	 * if the skb->data _is_ aligned.  A NOP attribute, plus
1870089bf1a6SNicolas Dichtel 	 * nlattr header for next attribute, will make nla_data()
1871089bf1a6SNicolas Dichtel 	 * 8-byte aligned.
1872089bf1a6SNicolas Dichtel 	 */
1873089bf1a6SNicolas Dichtel 	if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
1874089bf1a6SNicolas Dichtel 		return true;
1875089bf1a6SNicolas Dichtel #endif
1876089bf1a6SNicolas Dichtel 	return false;
1877089bf1a6SNicolas Dichtel }
1878089bf1a6SNicolas Dichtel 
1879089bf1a6SNicolas Dichtel /**
188035c58459SDavid S. Miller  * nla_align_64bit - 64-bit align the nla_data() of next attribute
188135c58459SDavid S. Miller  * @skb: socket buffer the message is stored in
188235c58459SDavid S. Miller  * @padattr: attribute type for the padding
188335c58459SDavid S. Miller  *
188435c58459SDavid S. Miller  * Conditionally emit a padding netlink attribute in order to make
188535c58459SDavid S. Miller  * the next attribute we emit have a 64-bit aligned nla_data() area.
188635c58459SDavid S. Miller  * This will only be done in architectures which do not have
1887cca1d815SEric Dumazet  * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
188835c58459SDavid S. Miller  *
188935c58459SDavid S. Miller  * Returns zero on success or a negative error code.
189035c58459SDavid S. Miller  */
189135c58459SDavid S. Miller static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
189235c58459SDavid S. Miller {
1893089bf1a6SNicolas Dichtel 	if (nla_need_padding_for_64bit(skb) &&
1894cca1d815SEric Dumazet 	    !nla_reserve(skb, padattr, 0))
189535c58459SDavid S. Miller 		return -EMSGSIZE;
1896089bf1a6SNicolas Dichtel 
189735c58459SDavid S. Miller 	return 0;
189835c58459SDavid S. Miller }
189935c58459SDavid S. Miller 
190035c58459SDavid S. Miller /**
190135c58459SDavid S. Miller  * nla_total_size_64bit - total length of attribute including padding
190235c58459SDavid S. Miller  * @payload: length of payload
190335c58459SDavid S. Miller  */
190435c58459SDavid S. Miller static inline int nla_total_size_64bit(int payload)
190535c58459SDavid S. Miller {
190635c58459SDavid S. Miller 	return NLA_ALIGN(nla_attr_size(payload))
1907cca1d815SEric Dumazet #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
190835c58459SDavid S. Miller 		+ NLA_ALIGN(nla_attr_size(0))
190935c58459SDavid S. Miller #endif
191035c58459SDavid S. Miller 		;
191135c58459SDavid S. Miller }
191235c58459SDavid S. Miller 
191335c58459SDavid S. Miller /**
1914bfa83a9eSThomas Graf  * nla_for_each_attr - iterate over a stream of attributes
1915bfa83a9eSThomas Graf  * @pos: loop counter, set to current attribute
1916bfa83a9eSThomas Graf  * @head: head of attribute stream
1917bfa83a9eSThomas Graf  * @len: length of attribute stream
1918bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
1919bfa83a9eSThomas Graf  */
1920bfa83a9eSThomas Graf #define nla_for_each_attr(pos, head, len, rem) \
1921bfa83a9eSThomas Graf 	for (pos = head, rem = len; \
1922bfa83a9eSThomas Graf 	     nla_ok(pos, rem); \
1923bfa83a9eSThomas Graf 	     pos = nla_next(pos, &(rem)))
1924bfa83a9eSThomas Graf 
1925fe4944e5SThomas Graf /**
1926fe4944e5SThomas Graf  * nla_for_each_nested - iterate over nested attributes
1927fe4944e5SThomas Graf  * @pos: loop counter, set to current attribute
1928fe4944e5SThomas Graf  * @nla: attribute containing the nested attributes
1929fe4944e5SThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
1930fe4944e5SThomas Graf  */
1931fe4944e5SThomas Graf #define nla_for_each_nested(pos, nla, rem) \
1932fe4944e5SThomas Graf 	nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
1933fe4944e5SThomas Graf 
1934941d8ebcSSimon Horman /**
1935941d8ebcSSimon Horman  * nla_is_last - Test if attribute is last in stream
1936941d8ebcSSimon Horman  * @nla: attribute to test
1937941d8ebcSSimon Horman  * @rem: bytes remaining in stream
1938941d8ebcSSimon Horman  */
1939941d8ebcSSimon Horman static inline bool nla_is_last(const struct nlattr *nla, int rem)
1940941d8ebcSSimon Horman {
1941941d8ebcSSimon Horman 	return nla->nla_len == rem;
1942941d8ebcSSimon Horman }
1943941d8ebcSSimon Horman 
19442c28ae48SJohannes Berg void nla_get_range_unsigned(const struct nla_policy *pt,
19452c28ae48SJohannes Berg 			    struct netlink_range_validation *range);
19462c28ae48SJohannes Berg void nla_get_range_signed(const struct nla_policy *pt,
19472c28ae48SJohannes Berg 			  struct netlink_range_validation_signed *range);
19482c28ae48SJohannes Berg 
1949adc84845SJakub Kicinski struct netlink_policy_dump_state;
1950adc84845SJakub Kicinski 
195104a351a6SJohannes Berg int netlink_policy_dump_add_policy(struct netlink_policy_dump_state **pstate,
195204a351a6SJohannes Berg 				   const struct nla_policy *policy,
195304a351a6SJohannes Berg 				   unsigned int maxtype);
195404a351a6SJohannes Berg int netlink_policy_dump_get_policy_idx(struct netlink_policy_dump_state *state,
195504a351a6SJohannes Berg 				       const struct nla_policy *policy,
195604a351a6SJohannes Berg 				       unsigned int maxtype);
1957adc84845SJakub Kicinski bool netlink_policy_dump_loop(struct netlink_policy_dump_state *state);
1958adc84845SJakub Kicinski int netlink_policy_dump_write(struct sk_buff *skb,
1959adc84845SJakub Kicinski 			      struct netlink_policy_dump_state *state);
1960adc84845SJakub Kicinski void netlink_policy_dump_free(struct netlink_policy_dump_state *state);
1961d07dcf9aSJohannes Berg 
1962bfa83a9eSThomas Graf #endif
1963