xref: /OK3568_Linux_fs/kernel/include/net/netlink.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef __NET_NETLINK_H
3*4882a593Smuzhiyun #define __NET_NETLINK_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/types.h>
6*4882a593Smuzhiyun #include <linux/netlink.h>
7*4882a593Smuzhiyun #include <linux/jiffies.h>
8*4882a593Smuzhiyun #include <linux/in6.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /* ========================================================================
11*4882a593Smuzhiyun  *         Netlink Messages and Attributes Interface (As Seen On TV)
12*4882a593Smuzhiyun  * ------------------------------------------------------------------------
13*4882a593Smuzhiyun  *                          Messages Interface
14*4882a593Smuzhiyun  * ------------------------------------------------------------------------
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * Message Format:
17*4882a593Smuzhiyun  *    <--- nlmsg_total_size(payload)  --->
18*4882a593Smuzhiyun  *    <-- nlmsg_msg_size(payload) ->
19*4882a593Smuzhiyun  *   +----------+- - -+-------------+- - -+-------- - -
20*4882a593Smuzhiyun  *   | nlmsghdr | Pad |   Payload   | Pad | nlmsghdr
21*4882a593Smuzhiyun  *   +----------+- - -+-------------+- - -+-------- - -
22*4882a593Smuzhiyun  *   nlmsg_data(nlh)---^                   ^
23*4882a593Smuzhiyun  *   nlmsg_next(nlh)-----------------------+
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * Payload Format:
26*4882a593Smuzhiyun  *    <---------------------- nlmsg_len(nlh) --------------------->
27*4882a593Smuzhiyun  *    <------ hdrlen ------>       <- nlmsg_attrlen(nlh, hdrlen) ->
28*4882a593Smuzhiyun  *   +----------------------+- - -+--------------------------------+
29*4882a593Smuzhiyun  *   |     Family Header    | Pad |           Attributes           |
30*4882a593Smuzhiyun  *   +----------------------+- - -+--------------------------------+
31*4882a593Smuzhiyun  *   nlmsg_attrdata(nlh, hdrlen)---^
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * Data Structures:
34*4882a593Smuzhiyun  *   struct nlmsghdr			netlink message header
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * Message Construction:
37*4882a593Smuzhiyun  *   nlmsg_new()			create a new netlink message
38*4882a593Smuzhiyun  *   nlmsg_put()			add a netlink message to an skb
39*4882a593Smuzhiyun  *   nlmsg_put_answer()			callback based nlmsg_put()
40*4882a593Smuzhiyun  *   nlmsg_end()			finalize netlink message
41*4882a593Smuzhiyun  *   nlmsg_get_pos()			return current position in message
42*4882a593Smuzhiyun  *   nlmsg_trim()			trim part of message
43*4882a593Smuzhiyun  *   nlmsg_cancel()			cancel message construction
44*4882a593Smuzhiyun  *   nlmsg_free()			free a netlink message
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * Message Sending:
47*4882a593Smuzhiyun  *   nlmsg_multicast()			multicast message to several groups
48*4882a593Smuzhiyun  *   nlmsg_unicast()			unicast a message to a single socket
49*4882a593Smuzhiyun  *   nlmsg_notify()			send notification message
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * Message Length Calculations:
52*4882a593Smuzhiyun  *   nlmsg_msg_size(payload)		length of message w/o padding
53*4882a593Smuzhiyun  *   nlmsg_total_size(payload)		length of message w/ padding
54*4882a593Smuzhiyun  *   nlmsg_padlen(payload)		length of padding at tail
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  * Message Payload Access:
57*4882a593Smuzhiyun  *   nlmsg_data(nlh)			head of message payload
58*4882a593Smuzhiyun  *   nlmsg_len(nlh)			length of message payload
59*4882a593Smuzhiyun  *   nlmsg_attrdata(nlh, hdrlen)	head of attributes data
60*4882a593Smuzhiyun  *   nlmsg_attrlen(nlh, hdrlen)		length of attributes data
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * Message Parsing:
63*4882a593Smuzhiyun  *   nlmsg_ok(nlh, remaining)		does nlh fit into remaining bytes?
64*4882a593Smuzhiyun  *   nlmsg_next(nlh, remaining)		get next netlink message
65*4882a593Smuzhiyun  *   nlmsg_parse()			parse attributes of a message
66*4882a593Smuzhiyun  *   nlmsg_find_attr()			find an attribute in a message
67*4882a593Smuzhiyun  *   nlmsg_for_each_msg()		loop over all messages
68*4882a593Smuzhiyun  *   nlmsg_validate()			validate netlink message incl. attrs
69*4882a593Smuzhiyun  *   nlmsg_for_each_attr()		loop over all attributes
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * Misc:
72*4882a593Smuzhiyun  *   nlmsg_report()			report back to application?
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * ------------------------------------------------------------------------
75*4882a593Smuzhiyun  *                          Attributes Interface
76*4882a593Smuzhiyun  * ------------------------------------------------------------------------
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * Attribute Format:
79*4882a593Smuzhiyun  *    <------- nla_total_size(payload) ------->
80*4882a593Smuzhiyun  *    <---- nla_attr_size(payload) ----->
81*4882a593Smuzhiyun  *   +----------+- - -+- - - - - - - - - +- - -+-------- - -
82*4882a593Smuzhiyun  *   |  Header  | Pad |     Payload      | Pad |  Header
83*4882a593Smuzhiyun  *   +----------+- - -+- - - - - - - - - +- - -+-------- - -
84*4882a593Smuzhiyun  *                     <- nla_len(nla) ->      ^
85*4882a593Smuzhiyun  *   nla_data(nla)----^                        |
86*4882a593Smuzhiyun  *   nla_next(nla)-----------------------------'
87*4882a593Smuzhiyun  *
88*4882a593Smuzhiyun  * Data Structures:
89*4882a593Smuzhiyun  *   struct nlattr			netlink attribute header
90*4882a593Smuzhiyun  *
91*4882a593Smuzhiyun  * Attribute Construction:
92*4882a593Smuzhiyun  *   nla_reserve(skb, type, len)	reserve room for an attribute
93*4882a593Smuzhiyun  *   nla_reserve_nohdr(skb, len)	reserve room for an attribute w/o hdr
94*4882a593Smuzhiyun  *   nla_put(skb, type, len, data)	add attribute to skb
95*4882a593Smuzhiyun  *   nla_put_nohdr(skb, len, data)	add attribute w/o hdr
96*4882a593Smuzhiyun  *   nla_append(skb, len, data)		append data to skb
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  * Attribute Construction for Basic Types:
99*4882a593Smuzhiyun  *   nla_put_u8(skb, type, value)	add u8 attribute to skb
100*4882a593Smuzhiyun  *   nla_put_u16(skb, type, value)	add u16 attribute to skb
101*4882a593Smuzhiyun  *   nla_put_u32(skb, type, value)	add u32 attribute to skb
102*4882a593Smuzhiyun  *   nla_put_u64_64bit(skb, type,
103*4882a593Smuzhiyun  *                     value, padattr)	add u64 attribute to skb
104*4882a593Smuzhiyun  *   nla_put_s8(skb, type, value)	add s8 attribute to skb
105*4882a593Smuzhiyun  *   nla_put_s16(skb, type, value)	add s16 attribute to skb
106*4882a593Smuzhiyun  *   nla_put_s32(skb, type, value)	add s32 attribute to skb
107*4882a593Smuzhiyun  *   nla_put_s64(skb, type, value,
108*4882a593Smuzhiyun  *               padattr)		add s64 attribute to skb
109*4882a593Smuzhiyun  *   nla_put_string(skb, type, str)	add string attribute to skb
110*4882a593Smuzhiyun  *   nla_put_flag(skb, type)		add flag attribute to skb
111*4882a593Smuzhiyun  *   nla_put_msecs(skb, type, jiffies,
112*4882a593Smuzhiyun  *                 padattr)		add msecs attribute to skb
113*4882a593Smuzhiyun  *   nla_put_in_addr(skb, type, addr)	add IPv4 address attribute to skb
114*4882a593Smuzhiyun  *   nla_put_in6_addr(skb, type, addr)	add IPv6 address attribute to skb
115*4882a593Smuzhiyun  *
116*4882a593Smuzhiyun  * Nested Attributes Construction:
117*4882a593Smuzhiyun  *   nla_nest_start(skb, type)		start a nested attribute
118*4882a593Smuzhiyun  *   nla_nest_end(skb, nla)		finalize a nested attribute
119*4882a593Smuzhiyun  *   nla_nest_cancel(skb, nla)		cancel nested attribute construction
120*4882a593Smuzhiyun  *
121*4882a593Smuzhiyun  * Attribute Length Calculations:
122*4882a593Smuzhiyun  *   nla_attr_size(payload)		length of attribute w/o padding
123*4882a593Smuzhiyun  *   nla_total_size(payload)		length of attribute w/ padding
124*4882a593Smuzhiyun  *   nla_padlen(payload)		length of padding
125*4882a593Smuzhiyun  *
126*4882a593Smuzhiyun  * Attribute Payload Access:
127*4882a593Smuzhiyun  *   nla_data(nla)			head of attribute payload
128*4882a593Smuzhiyun  *   nla_len(nla)			length of attribute payload
129*4882a593Smuzhiyun  *
130*4882a593Smuzhiyun  * Attribute Payload Access for Basic Types:
131*4882a593Smuzhiyun  *   nla_get_u8(nla)			get payload for a u8 attribute
132*4882a593Smuzhiyun  *   nla_get_u16(nla)			get payload for a u16 attribute
133*4882a593Smuzhiyun  *   nla_get_u32(nla)			get payload for a u32 attribute
134*4882a593Smuzhiyun  *   nla_get_u64(nla)			get payload for a u64 attribute
135*4882a593Smuzhiyun  *   nla_get_s8(nla)			get payload for a s8 attribute
136*4882a593Smuzhiyun  *   nla_get_s16(nla)			get payload for a s16 attribute
137*4882a593Smuzhiyun  *   nla_get_s32(nla)			get payload for a s32 attribute
138*4882a593Smuzhiyun  *   nla_get_s64(nla)			get payload for a s64 attribute
139*4882a593Smuzhiyun  *   nla_get_flag(nla)			return 1 if flag is true
140*4882a593Smuzhiyun  *   nla_get_msecs(nla)			get payload for a msecs attribute
141*4882a593Smuzhiyun  *
142*4882a593Smuzhiyun  * Attribute Misc:
143*4882a593Smuzhiyun  *   nla_memcpy(dest, nla, count)	copy attribute into memory
144*4882a593Smuzhiyun  *   nla_memcmp(nla, data, size)	compare attribute with memory area
145*4882a593Smuzhiyun  *   nla_strlcpy(dst, nla, size)	copy attribute to a sized string
146*4882a593Smuzhiyun  *   nla_strcmp(nla, str)		compare attribute with string
147*4882a593Smuzhiyun  *
148*4882a593Smuzhiyun  * Attribute Parsing:
149*4882a593Smuzhiyun  *   nla_ok(nla, remaining)		does nla fit into remaining bytes?
150*4882a593Smuzhiyun  *   nla_next(nla, remaining)		get next netlink attribute
151*4882a593Smuzhiyun  *   nla_validate()			validate a stream of attributes
152*4882a593Smuzhiyun  *   nla_validate_nested()		validate a stream of nested attributes
153*4882a593Smuzhiyun  *   nla_find()				find attribute in stream of attributes
154*4882a593Smuzhiyun  *   nla_find_nested()			find attribute in nested attributes
155*4882a593Smuzhiyun  *   nla_parse()			parse and validate stream of attrs
156*4882a593Smuzhiyun  *   nla_parse_nested()			parse nested attributes
157*4882a593Smuzhiyun  *   nla_for_each_attr()		loop over all attributes
158*4882a593Smuzhiyun  *   nla_for_each_nested()		loop over the nested attributes
159*4882a593Smuzhiyun  *=========================================================================
160*4882a593Smuzhiyun  */
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun  /**
163*4882a593Smuzhiyun   * Standard attribute types to specify validation policy
164*4882a593Smuzhiyun   */
165*4882a593Smuzhiyun enum {
166*4882a593Smuzhiyun 	NLA_UNSPEC,
167*4882a593Smuzhiyun 	NLA_U8,
168*4882a593Smuzhiyun 	NLA_U16,
169*4882a593Smuzhiyun 	NLA_U32,
170*4882a593Smuzhiyun 	NLA_U64,
171*4882a593Smuzhiyun 	NLA_STRING,
172*4882a593Smuzhiyun 	NLA_FLAG,
173*4882a593Smuzhiyun 	NLA_MSECS,
174*4882a593Smuzhiyun 	NLA_NESTED,
175*4882a593Smuzhiyun 	NLA_NESTED_ARRAY,
176*4882a593Smuzhiyun 	NLA_NUL_STRING,
177*4882a593Smuzhiyun 	NLA_BINARY,
178*4882a593Smuzhiyun 	NLA_S8,
179*4882a593Smuzhiyun 	NLA_S16,
180*4882a593Smuzhiyun 	NLA_S32,
181*4882a593Smuzhiyun 	NLA_S64,
182*4882a593Smuzhiyun 	NLA_BITFIELD32,
183*4882a593Smuzhiyun 	NLA_REJECT,
184*4882a593Smuzhiyun 	__NLA_TYPE_MAX,
185*4882a593Smuzhiyun };
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun struct netlink_range_validation {
190*4882a593Smuzhiyun 	u64 min, max;
191*4882a593Smuzhiyun };
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun struct netlink_range_validation_signed {
194*4882a593Smuzhiyun 	s64 min, max;
195*4882a593Smuzhiyun };
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun enum nla_policy_validation {
198*4882a593Smuzhiyun 	NLA_VALIDATE_NONE,
199*4882a593Smuzhiyun 	NLA_VALIDATE_RANGE,
200*4882a593Smuzhiyun 	NLA_VALIDATE_RANGE_WARN_TOO_LONG,
201*4882a593Smuzhiyun 	NLA_VALIDATE_MIN,
202*4882a593Smuzhiyun 	NLA_VALIDATE_MAX,
203*4882a593Smuzhiyun 	NLA_VALIDATE_MASK,
204*4882a593Smuzhiyun 	NLA_VALIDATE_RANGE_PTR,
205*4882a593Smuzhiyun 	NLA_VALIDATE_FUNCTION,
206*4882a593Smuzhiyun };
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun /**
209*4882a593Smuzhiyun  * struct nla_policy - attribute validation policy
210*4882a593Smuzhiyun  * @type: Type of attribute or NLA_UNSPEC
211*4882a593Smuzhiyun  * @validation_type: type of attribute validation done in addition to
212*4882a593Smuzhiyun  *	type-specific validation (e.g. range, function call), see
213*4882a593Smuzhiyun  *	&enum nla_policy_validation
214*4882a593Smuzhiyun  * @len: Type specific length of payload
215*4882a593Smuzhiyun  *
216*4882a593Smuzhiyun  * Policies are defined as arrays of this struct, the array must be
217*4882a593Smuzhiyun  * accessible by attribute type up to the highest identifier to be expected.
218*4882a593Smuzhiyun  *
219*4882a593Smuzhiyun  * Meaning of `len' field:
220*4882a593Smuzhiyun  *    NLA_STRING           Maximum length of string
221*4882a593Smuzhiyun  *    NLA_NUL_STRING       Maximum length of string (excluding NUL)
222*4882a593Smuzhiyun  *    NLA_FLAG             Unused
223*4882a593Smuzhiyun  *    NLA_BINARY           Maximum length of attribute payload
224*4882a593Smuzhiyun  *                         (but see also below with the validation type)
225*4882a593Smuzhiyun  *    NLA_NESTED,
226*4882a593Smuzhiyun  *    NLA_NESTED_ARRAY     Length verification is done by checking len of
227*4882a593Smuzhiyun  *                         nested header (or empty); len field is used if
228*4882a593Smuzhiyun  *                         nested_policy is also used, for the max attr
229*4882a593Smuzhiyun  *                         number in the nested policy.
230*4882a593Smuzhiyun  *    NLA_U8, NLA_U16,
231*4882a593Smuzhiyun  *    NLA_U32, NLA_U64,
232*4882a593Smuzhiyun  *    NLA_S8, NLA_S16,
233*4882a593Smuzhiyun  *    NLA_S32, NLA_S64,
234*4882a593Smuzhiyun  *    NLA_MSECS            Leaving the length field zero will verify the
235*4882a593Smuzhiyun  *                         given type fits, using it verifies minimum length
236*4882a593Smuzhiyun  *                         just like "All other"
237*4882a593Smuzhiyun  *    NLA_BITFIELD32       Unused
238*4882a593Smuzhiyun  *    NLA_REJECT           Unused
239*4882a593Smuzhiyun  *    All other            Minimum length of attribute payload
240*4882a593Smuzhiyun  *
241*4882a593Smuzhiyun  * Meaning of validation union:
242*4882a593Smuzhiyun  *    NLA_BITFIELD32       This is a 32-bit bitmap/bitselector attribute and
243*4882a593Smuzhiyun  *                         `bitfield32_valid' is the u32 value of valid flags
244*4882a593Smuzhiyun  *    NLA_REJECT           This attribute is always rejected and `reject_message'
245*4882a593Smuzhiyun  *                         may point to a string to report as the error instead
246*4882a593Smuzhiyun  *                         of the generic one in extended ACK.
247*4882a593Smuzhiyun  *    NLA_NESTED           `nested_policy' to a nested policy to validate, must
248*4882a593Smuzhiyun  *                         also set `len' to the max attribute number. Use the
249*4882a593Smuzhiyun  *                         provided NLA_POLICY_NESTED() macro.
250*4882a593Smuzhiyun  *                         Note that nla_parse() will validate, but of course not
251*4882a593Smuzhiyun  *                         parse, the nested sub-policies.
252*4882a593Smuzhiyun  *    NLA_NESTED_ARRAY     `nested_policy' points to a nested policy to validate,
253*4882a593Smuzhiyun  *                         must also set `len' to the max attribute number. Use
254*4882a593Smuzhiyun  *                         the provided NLA_POLICY_NESTED_ARRAY() macro.
255*4882a593Smuzhiyun  *                         The difference to NLA_NESTED is the structure:
256*4882a593Smuzhiyun  *                         NLA_NESTED has the nested attributes directly inside
257*4882a593Smuzhiyun  *                         while an array has the nested attributes at another
258*4882a593Smuzhiyun  *                         level down and the attribute types directly in the
259*4882a593Smuzhiyun  *                         nesting don't matter.
260*4882a593Smuzhiyun  *    NLA_U8,
261*4882a593Smuzhiyun  *    NLA_U16,
262*4882a593Smuzhiyun  *    NLA_U32,
263*4882a593Smuzhiyun  *    NLA_U64,
264*4882a593Smuzhiyun  *    NLA_S8,
265*4882a593Smuzhiyun  *    NLA_S16,
266*4882a593Smuzhiyun  *    NLA_S32,
267*4882a593Smuzhiyun  *    NLA_S64              The `min' and `max' fields are used depending on the
268*4882a593Smuzhiyun  *                         validation_type field, if that is min/max/range then
269*4882a593Smuzhiyun  *                         the min, max or both are used (respectively) to check
270*4882a593Smuzhiyun  *                         the value of the integer attribute.
271*4882a593Smuzhiyun  *                         Note that in the interest of code simplicity and
272*4882a593Smuzhiyun  *                         struct size both limits are s16, so you cannot
273*4882a593Smuzhiyun  *                         enforce a range that doesn't fall within the range
274*4882a593Smuzhiyun  *                         of s16 - do that as usual in the code instead.
275*4882a593Smuzhiyun  *                         Use the NLA_POLICY_MIN(), NLA_POLICY_MAX() and
276*4882a593Smuzhiyun  *                         NLA_POLICY_RANGE() macros.
277*4882a593Smuzhiyun  *    NLA_U8,
278*4882a593Smuzhiyun  *    NLA_U16,
279*4882a593Smuzhiyun  *    NLA_U32,
280*4882a593Smuzhiyun  *    NLA_U64              If the validation_type field instead is set to
281*4882a593Smuzhiyun  *                         NLA_VALIDATE_RANGE_PTR, `range' must be a pointer
282*4882a593Smuzhiyun  *                         to a struct netlink_range_validation that indicates
283*4882a593Smuzhiyun  *                         the min/max values.
284*4882a593Smuzhiyun  *                         Use NLA_POLICY_FULL_RANGE().
285*4882a593Smuzhiyun  *    NLA_S8,
286*4882a593Smuzhiyun  *    NLA_S16,
287*4882a593Smuzhiyun  *    NLA_S32,
288*4882a593Smuzhiyun  *    NLA_S64              If the validation_type field instead is set to
289*4882a593Smuzhiyun  *                         NLA_VALIDATE_RANGE_PTR, `range_signed' must be a
290*4882a593Smuzhiyun  *                         pointer to a struct netlink_range_validation_signed
291*4882a593Smuzhiyun  *                         that indicates the min/max values.
292*4882a593Smuzhiyun  *                         Use NLA_POLICY_FULL_RANGE_SIGNED().
293*4882a593Smuzhiyun  *
294*4882a593Smuzhiyun  *    NLA_BINARY           If the validation type is like the ones for integers
295*4882a593Smuzhiyun  *                         above, then the min/max length (not value like for
296*4882a593Smuzhiyun  *                         integers) of the attribute is enforced.
297*4882a593Smuzhiyun  *
298*4882a593Smuzhiyun  *    All other            Unused - but note that it's a union
299*4882a593Smuzhiyun  *
300*4882a593Smuzhiyun  * Meaning of `validate' field, use via NLA_POLICY_VALIDATE_FN:
301*4882a593Smuzhiyun  *    NLA_BINARY           Validation function called for the attribute.
302*4882a593Smuzhiyun  *    All other            Unused - but note that it's a union
303*4882a593Smuzhiyun  *
304*4882a593Smuzhiyun  * Example:
305*4882a593Smuzhiyun  *
306*4882a593Smuzhiyun  * static const u32 myvalidflags = 0xff231023;
307*4882a593Smuzhiyun  *
308*4882a593Smuzhiyun  * static const struct nla_policy my_policy[ATTR_MAX+1] = {
309*4882a593Smuzhiyun  * 	[ATTR_FOO] = { .type = NLA_U16 },
310*4882a593Smuzhiyun  *	[ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
311*4882a593Smuzhiyun  *	[ATTR_BAZ] = NLA_POLICY_EXACT_LEN(sizeof(struct mystruct)),
312*4882a593Smuzhiyun  *	[ATTR_GOO] = NLA_POLICY_BITFIELD32(myvalidflags),
313*4882a593Smuzhiyun  * };
314*4882a593Smuzhiyun  */
315*4882a593Smuzhiyun struct nla_policy {
316*4882a593Smuzhiyun 	u8		type;
317*4882a593Smuzhiyun 	u8		validation_type;
318*4882a593Smuzhiyun 	u16		len;
319*4882a593Smuzhiyun 	union {
320*4882a593Smuzhiyun 		const u32 bitfield32_valid;
321*4882a593Smuzhiyun 		const u32 mask;
322*4882a593Smuzhiyun 		const char *reject_message;
323*4882a593Smuzhiyun 		const struct nla_policy *nested_policy;
324*4882a593Smuzhiyun 		struct netlink_range_validation *range;
325*4882a593Smuzhiyun 		struct netlink_range_validation_signed *range_signed;
326*4882a593Smuzhiyun 		struct {
327*4882a593Smuzhiyun 			s16 min, max;
328*4882a593Smuzhiyun 		};
329*4882a593Smuzhiyun 		int (*validate)(const struct nlattr *attr,
330*4882a593Smuzhiyun 				struct netlink_ext_ack *extack);
331*4882a593Smuzhiyun 		/* This entry is special, and used for the attribute at index 0
332*4882a593Smuzhiyun 		 * only, and specifies special data about the policy, namely it
333*4882a593Smuzhiyun 		 * specifies the "boundary type" where strict length validation
334*4882a593Smuzhiyun 		 * starts for any attribute types >= this value, also, strict
335*4882a593Smuzhiyun 		 * nesting validation starts here.
336*4882a593Smuzhiyun 		 *
337*4882a593Smuzhiyun 		 * Additionally, it means that NLA_UNSPEC is actually NLA_REJECT
338*4882a593Smuzhiyun 		 * for any types >= this, so need to use NLA_POLICY_MIN_LEN() to
339*4882a593Smuzhiyun 		 * get the previous pure { .len = xyz } behaviour. The advantage
340*4882a593Smuzhiyun 		 * of this is that types not specified in the policy will be
341*4882a593Smuzhiyun 		 * rejected.
342*4882a593Smuzhiyun 		 *
343*4882a593Smuzhiyun 		 * For completely new families it should be set to 1 so that the
344*4882a593Smuzhiyun 		 * validation is enforced for all attributes. For existing ones
345*4882a593Smuzhiyun 		 * it should be set at least when new attributes are added to
346*4882a593Smuzhiyun 		 * the enum used by the policy, and be set to the new value that
347*4882a593Smuzhiyun 		 * was added to enforce strict validation from thereon.
348*4882a593Smuzhiyun 		 */
349*4882a593Smuzhiyun 		u16 strict_start_type;
350*4882a593Smuzhiyun 	};
351*4882a593Smuzhiyun };
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun #define NLA_POLICY_ETH_ADDR		NLA_POLICY_EXACT_LEN(ETH_ALEN)
354*4882a593Smuzhiyun #define NLA_POLICY_ETH_ADDR_COMPAT	NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN)
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun #define _NLA_POLICY_NESTED(maxattr, policy) \
357*4882a593Smuzhiyun 	{ .type = NLA_NESTED, .nested_policy = policy, .len = maxattr }
358*4882a593Smuzhiyun #define _NLA_POLICY_NESTED_ARRAY(maxattr, policy) \
359*4882a593Smuzhiyun 	{ .type = NLA_NESTED_ARRAY, .nested_policy = policy, .len = maxattr }
360*4882a593Smuzhiyun #define NLA_POLICY_NESTED(policy) \
361*4882a593Smuzhiyun 	_NLA_POLICY_NESTED(ARRAY_SIZE(policy) - 1, policy)
362*4882a593Smuzhiyun #define NLA_POLICY_NESTED_ARRAY(policy) \
363*4882a593Smuzhiyun 	_NLA_POLICY_NESTED_ARRAY(ARRAY_SIZE(policy) - 1, policy)
364*4882a593Smuzhiyun #define NLA_POLICY_BITFIELD32(valid) \
365*4882a593Smuzhiyun 	{ .type = NLA_BITFIELD32, .bitfield32_valid = valid }
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun #define __NLA_IS_UINT_TYPE(tp)						\
368*4882a593Smuzhiyun 	(tp == NLA_U8 || tp == NLA_U16 || tp == NLA_U32 || tp == NLA_U64)
369*4882a593Smuzhiyun #define __NLA_IS_SINT_TYPE(tp)						\
370*4882a593Smuzhiyun 	(tp == NLA_S8 || tp == NLA_S16 || tp == NLA_S32 || tp == NLA_S64)
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun #define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition))
373*4882a593Smuzhiyun #define NLA_ENSURE_UINT_TYPE(tp)			\
374*4882a593Smuzhiyun 	(__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp)) + tp)
375*4882a593Smuzhiyun #define NLA_ENSURE_UINT_OR_BINARY_TYPE(tp)		\
376*4882a593Smuzhiyun 	(__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) ||	\
377*4882a593Smuzhiyun 		      tp == NLA_MSECS ||		\
378*4882a593Smuzhiyun 		      tp == NLA_BINARY) + tp)
379*4882a593Smuzhiyun #define NLA_ENSURE_SINT_TYPE(tp)			\
380*4882a593Smuzhiyun 	(__NLA_ENSURE(__NLA_IS_SINT_TYPE(tp)) + tp)
381*4882a593Smuzhiyun #define NLA_ENSURE_INT_OR_BINARY_TYPE(tp)		\
382*4882a593Smuzhiyun 	(__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) ||		\
383*4882a593Smuzhiyun 		      __NLA_IS_SINT_TYPE(tp) ||		\
384*4882a593Smuzhiyun 		      tp == NLA_MSECS ||		\
385*4882a593Smuzhiyun 		      tp == NLA_BINARY) + tp)
386*4882a593Smuzhiyun #define NLA_ENSURE_NO_VALIDATION_PTR(tp)		\
387*4882a593Smuzhiyun 	(__NLA_ENSURE(tp != NLA_BITFIELD32 &&		\
388*4882a593Smuzhiyun 		      tp != NLA_REJECT &&		\
389*4882a593Smuzhiyun 		      tp != NLA_NESTED &&		\
390*4882a593Smuzhiyun 		      tp != NLA_NESTED_ARRAY) + tp)
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun #define NLA_POLICY_RANGE(tp, _min, _max) {		\
393*4882a593Smuzhiyun 	.type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp),	\
394*4882a593Smuzhiyun 	.validation_type = NLA_VALIDATE_RANGE,		\
395*4882a593Smuzhiyun 	.min = _min,					\
396*4882a593Smuzhiyun 	.max = _max					\
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun #define NLA_POLICY_FULL_RANGE(tp, _range) {		\
400*4882a593Smuzhiyun 	.type = NLA_ENSURE_UINT_OR_BINARY_TYPE(tp),	\
401*4882a593Smuzhiyun 	.validation_type = NLA_VALIDATE_RANGE_PTR,	\
402*4882a593Smuzhiyun 	.range = _range,				\
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun #define NLA_POLICY_FULL_RANGE_SIGNED(tp, _range) {	\
406*4882a593Smuzhiyun 	.type = NLA_ENSURE_SINT_TYPE(tp),		\
407*4882a593Smuzhiyun 	.validation_type = NLA_VALIDATE_RANGE_PTR,	\
408*4882a593Smuzhiyun 	.range_signed = _range,				\
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun #define NLA_POLICY_MIN(tp, _min) {			\
412*4882a593Smuzhiyun 	.type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp),	\
413*4882a593Smuzhiyun 	.validation_type = NLA_VALIDATE_MIN,		\
414*4882a593Smuzhiyun 	.min = _min,					\
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun #define NLA_POLICY_MAX(tp, _max) {			\
418*4882a593Smuzhiyun 	.type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp),	\
419*4882a593Smuzhiyun 	.validation_type = NLA_VALIDATE_MAX,		\
420*4882a593Smuzhiyun 	.max = _max,					\
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun #define NLA_POLICY_MASK(tp, _mask) {			\
424*4882a593Smuzhiyun 	.type = NLA_ENSURE_UINT_TYPE(tp),		\
425*4882a593Smuzhiyun 	.validation_type = NLA_VALIDATE_MASK,		\
426*4882a593Smuzhiyun 	.mask = _mask,					\
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun #define NLA_POLICY_VALIDATE_FN(tp, fn, ...) {		\
430*4882a593Smuzhiyun 	.type = NLA_ENSURE_NO_VALIDATION_PTR(tp),	\
431*4882a593Smuzhiyun 	.validation_type = NLA_VALIDATE_FUNCTION,	\
432*4882a593Smuzhiyun 	.validate = fn,					\
433*4882a593Smuzhiyun 	.len = __VA_ARGS__ + 0,				\
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun #define NLA_POLICY_EXACT_LEN(_len)	NLA_POLICY_RANGE(NLA_BINARY, _len, _len)
437*4882a593Smuzhiyun #define NLA_POLICY_EXACT_LEN_WARN(_len) {			\
438*4882a593Smuzhiyun 	.type = NLA_BINARY,					\
439*4882a593Smuzhiyun 	.validation_type = NLA_VALIDATE_RANGE_WARN_TOO_LONG,	\
440*4882a593Smuzhiyun 	.min = _len,						\
441*4882a593Smuzhiyun 	.max = _len						\
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun #define NLA_POLICY_MIN_LEN(_len)	NLA_POLICY_MIN(NLA_BINARY, _len)
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun /**
446*4882a593Smuzhiyun  * struct nl_info - netlink source information
447*4882a593Smuzhiyun  * @nlh: Netlink message header of original request
448*4882a593Smuzhiyun  * @nl_net: Network namespace
449*4882a593Smuzhiyun  * @portid: Netlink PORTID of requesting application
450*4882a593Smuzhiyun  * @skip_notify: Skip netlink notifications to user space
451*4882a593Smuzhiyun  * @skip_notify_kernel: Skip selected in-kernel notifications
452*4882a593Smuzhiyun  */
453*4882a593Smuzhiyun struct nl_info {
454*4882a593Smuzhiyun 	struct nlmsghdr		*nlh;
455*4882a593Smuzhiyun 	struct net		*nl_net;
456*4882a593Smuzhiyun 	u32			portid;
457*4882a593Smuzhiyun 	u8			skip_notify:1,
458*4882a593Smuzhiyun 				skip_notify_kernel:1;
459*4882a593Smuzhiyun };
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun /**
462*4882a593Smuzhiyun  * enum netlink_validation - netlink message/attribute validation levels
463*4882a593Smuzhiyun  * @NL_VALIDATE_LIBERAL: Old-style "be liberal" validation, not caring about
464*4882a593Smuzhiyun  *	extra data at the end of the message, attributes being longer than
465*4882a593Smuzhiyun  *	they should be, or unknown attributes being present.
466*4882a593Smuzhiyun  * @NL_VALIDATE_TRAILING: Reject junk data encountered after attribute parsing.
467*4882a593Smuzhiyun  * @NL_VALIDATE_MAXTYPE: Reject attributes > max type; Together with _TRAILING
468*4882a593Smuzhiyun  *	this is equivalent to the old nla_parse_strict()/nlmsg_parse_strict().
469*4882a593Smuzhiyun  * @NL_VALIDATE_UNSPEC: Reject attributes with NLA_UNSPEC in the policy.
470*4882a593Smuzhiyun  *	This can safely be set by the kernel when the given policy has no
471*4882a593Smuzhiyun  *	NLA_UNSPEC anymore, and can thus be used to ensure policy entries
472*4882a593Smuzhiyun  *	are enforced going forward.
473*4882a593Smuzhiyun  * @NL_VALIDATE_STRICT_ATTRS: strict attribute policy parsing (e.g.
474*4882a593Smuzhiyun  *	U8, U16, U32 must have exact size, etc.)
475*4882a593Smuzhiyun  * @NL_VALIDATE_NESTED: Check that NLA_F_NESTED is set for NLA_NESTED(_ARRAY)
476*4882a593Smuzhiyun  *	and unset for other policies.
477*4882a593Smuzhiyun  */
478*4882a593Smuzhiyun enum netlink_validation {
479*4882a593Smuzhiyun 	NL_VALIDATE_LIBERAL = 0,
480*4882a593Smuzhiyun 	NL_VALIDATE_TRAILING = BIT(0),
481*4882a593Smuzhiyun 	NL_VALIDATE_MAXTYPE = BIT(1),
482*4882a593Smuzhiyun 	NL_VALIDATE_UNSPEC = BIT(2),
483*4882a593Smuzhiyun 	NL_VALIDATE_STRICT_ATTRS = BIT(3),
484*4882a593Smuzhiyun 	NL_VALIDATE_NESTED = BIT(4),
485*4882a593Smuzhiyun };
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun #define NL_VALIDATE_DEPRECATED_STRICT (NL_VALIDATE_TRAILING |\
488*4882a593Smuzhiyun 				       NL_VALIDATE_MAXTYPE)
489*4882a593Smuzhiyun #define NL_VALIDATE_STRICT (NL_VALIDATE_TRAILING |\
490*4882a593Smuzhiyun 			    NL_VALIDATE_MAXTYPE |\
491*4882a593Smuzhiyun 			    NL_VALIDATE_UNSPEC |\
492*4882a593Smuzhiyun 			    NL_VALIDATE_STRICT_ATTRS |\
493*4882a593Smuzhiyun 			    NL_VALIDATE_NESTED)
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun int netlink_rcv_skb(struct sk_buff *skb,
496*4882a593Smuzhiyun 		    int (*cb)(struct sk_buff *, struct nlmsghdr *,
497*4882a593Smuzhiyun 			      struct netlink_ext_ack *));
498*4882a593Smuzhiyun int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
499*4882a593Smuzhiyun 		 unsigned int group, int report, gfp_t flags);
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun int __nla_validate(const struct nlattr *head, int len, int maxtype,
502*4882a593Smuzhiyun 		   const struct nla_policy *policy, unsigned int validate,
503*4882a593Smuzhiyun 		   struct netlink_ext_ack *extack);
504*4882a593Smuzhiyun int __nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
505*4882a593Smuzhiyun 		int len, const struct nla_policy *policy, unsigned int validate,
506*4882a593Smuzhiyun 		struct netlink_ext_ack *extack);
507*4882a593Smuzhiyun int nla_policy_len(const struct nla_policy *, int);
508*4882a593Smuzhiyun struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
509*4882a593Smuzhiyun size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize);
510*4882a593Smuzhiyun char *nla_strdup(const struct nlattr *nla, gfp_t flags);
511*4882a593Smuzhiyun int nla_memcpy(void *dest, const struct nlattr *src, int count);
512*4882a593Smuzhiyun int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
513*4882a593Smuzhiyun int nla_strcmp(const struct nlattr *nla, const char *str);
514*4882a593Smuzhiyun struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
515*4882a593Smuzhiyun struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
516*4882a593Smuzhiyun 				   int attrlen, int padattr);
517*4882a593Smuzhiyun void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
518*4882a593Smuzhiyun struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
519*4882a593Smuzhiyun struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype,
520*4882a593Smuzhiyun 				 int attrlen, int padattr);
521*4882a593Smuzhiyun void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
522*4882a593Smuzhiyun void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
523*4882a593Smuzhiyun 	       const void *data);
524*4882a593Smuzhiyun void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
525*4882a593Smuzhiyun 		     const void *data, int padattr);
526*4882a593Smuzhiyun void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
527*4882a593Smuzhiyun int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);
528*4882a593Smuzhiyun int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
529*4882a593Smuzhiyun 		  const void *data, int padattr);
530*4882a593Smuzhiyun int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
531*4882a593Smuzhiyun int nla_append(struct sk_buff *skb, int attrlen, const void *data);
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun /**************************************************************************
534*4882a593Smuzhiyun  * Netlink Messages
535*4882a593Smuzhiyun  **************************************************************************/
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun /**
538*4882a593Smuzhiyun  * nlmsg_msg_size - length of netlink message not including padding
539*4882a593Smuzhiyun  * @payload: length of message payload
540*4882a593Smuzhiyun  */
nlmsg_msg_size(int payload)541*4882a593Smuzhiyun static inline int nlmsg_msg_size(int payload)
542*4882a593Smuzhiyun {
543*4882a593Smuzhiyun 	return NLMSG_HDRLEN + payload;
544*4882a593Smuzhiyun }
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun /**
547*4882a593Smuzhiyun  * nlmsg_total_size - length of netlink message including padding
548*4882a593Smuzhiyun  * @payload: length of message payload
549*4882a593Smuzhiyun  */
nlmsg_total_size(int payload)550*4882a593Smuzhiyun static inline int nlmsg_total_size(int payload)
551*4882a593Smuzhiyun {
552*4882a593Smuzhiyun 	return NLMSG_ALIGN(nlmsg_msg_size(payload));
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun /**
556*4882a593Smuzhiyun  * nlmsg_padlen - length of padding at the message's tail
557*4882a593Smuzhiyun  * @payload: length of message payload
558*4882a593Smuzhiyun  */
nlmsg_padlen(int payload)559*4882a593Smuzhiyun static inline int nlmsg_padlen(int payload)
560*4882a593Smuzhiyun {
561*4882a593Smuzhiyun 	return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun /**
565*4882a593Smuzhiyun  * nlmsg_data - head of message payload
566*4882a593Smuzhiyun  * @nlh: netlink message header
567*4882a593Smuzhiyun  */
nlmsg_data(const struct nlmsghdr * nlh)568*4882a593Smuzhiyun static inline void *nlmsg_data(const struct nlmsghdr *nlh)
569*4882a593Smuzhiyun {
570*4882a593Smuzhiyun 	return (unsigned char *) nlh + NLMSG_HDRLEN;
571*4882a593Smuzhiyun }
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun /**
574*4882a593Smuzhiyun  * nlmsg_len - length of message payload
575*4882a593Smuzhiyun  * @nlh: netlink message header
576*4882a593Smuzhiyun  */
nlmsg_len(const struct nlmsghdr * nlh)577*4882a593Smuzhiyun static inline int nlmsg_len(const struct nlmsghdr *nlh)
578*4882a593Smuzhiyun {
579*4882a593Smuzhiyun 	return nlh->nlmsg_len - NLMSG_HDRLEN;
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun /**
583*4882a593Smuzhiyun  * nlmsg_attrdata - head of attributes data
584*4882a593Smuzhiyun  * @nlh: netlink message header
585*4882a593Smuzhiyun  * @hdrlen: length of family specific header
586*4882a593Smuzhiyun  */
nlmsg_attrdata(const struct nlmsghdr * nlh,int hdrlen)587*4882a593Smuzhiyun static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
588*4882a593Smuzhiyun 					    int hdrlen)
589*4882a593Smuzhiyun {
590*4882a593Smuzhiyun 	unsigned char *data = nlmsg_data(nlh);
591*4882a593Smuzhiyun 	return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun /**
595*4882a593Smuzhiyun  * nlmsg_attrlen - length of attributes data
596*4882a593Smuzhiyun  * @nlh: netlink message header
597*4882a593Smuzhiyun  * @hdrlen: length of family specific header
598*4882a593Smuzhiyun  */
nlmsg_attrlen(const struct nlmsghdr * nlh,int hdrlen)599*4882a593Smuzhiyun static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
600*4882a593Smuzhiyun {
601*4882a593Smuzhiyun 	return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun /**
605*4882a593Smuzhiyun  * nlmsg_ok - check if the netlink message fits into the remaining bytes
606*4882a593Smuzhiyun  * @nlh: netlink message header
607*4882a593Smuzhiyun  * @remaining: number of bytes remaining in message stream
608*4882a593Smuzhiyun  */
nlmsg_ok(const struct nlmsghdr * nlh,int remaining)609*4882a593Smuzhiyun static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun 	return (remaining >= (int) sizeof(struct nlmsghdr) &&
612*4882a593Smuzhiyun 		nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
613*4882a593Smuzhiyun 		nlh->nlmsg_len <= remaining);
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun /**
617*4882a593Smuzhiyun  * nlmsg_next - next netlink message in message stream
618*4882a593Smuzhiyun  * @nlh: netlink message header
619*4882a593Smuzhiyun  * @remaining: number of bytes remaining in message stream
620*4882a593Smuzhiyun  *
621*4882a593Smuzhiyun  * Returns the next netlink message in the message stream and
622*4882a593Smuzhiyun  * decrements remaining by the size of the current message.
623*4882a593Smuzhiyun  */
624*4882a593Smuzhiyun static inline struct nlmsghdr *
nlmsg_next(const struct nlmsghdr * nlh,int * remaining)625*4882a593Smuzhiyun nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun 	int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	*remaining -= totlen;
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
632*4882a593Smuzhiyun }
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun /**
635*4882a593Smuzhiyun  * nla_parse - Parse a stream of attributes into a tb buffer
636*4882a593Smuzhiyun  * @tb: destination array with maxtype+1 elements
637*4882a593Smuzhiyun  * @maxtype: maximum attribute type to be expected
638*4882a593Smuzhiyun  * @head: head of attribute stream
639*4882a593Smuzhiyun  * @len: length of attribute stream
640*4882a593Smuzhiyun  * @policy: validation policy
641*4882a593Smuzhiyun  * @extack: extended ACK pointer
642*4882a593Smuzhiyun  *
643*4882a593Smuzhiyun  * Parses a stream of attributes and stores a pointer to each attribute in
644*4882a593Smuzhiyun  * the tb array accessible via the attribute type. Attributes with a type
645*4882a593Smuzhiyun  * exceeding maxtype will be rejected, policy must be specified, attributes
646*4882a593Smuzhiyun  * will be validated in the strictest way possible.
647*4882a593Smuzhiyun  *
648*4882a593Smuzhiyun  * Returns 0 on success or a negative error code.
649*4882a593Smuzhiyun  */
nla_parse(struct nlattr ** tb,int maxtype,const struct nlattr * head,int len,const struct nla_policy * policy,struct netlink_ext_ack * extack)650*4882a593Smuzhiyun static inline int nla_parse(struct nlattr **tb, int maxtype,
651*4882a593Smuzhiyun 			    const struct nlattr *head, int len,
652*4882a593Smuzhiyun 			    const struct nla_policy *policy,
653*4882a593Smuzhiyun 			    struct netlink_ext_ack *extack)
654*4882a593Smuzhiyun {
655*4882a593Smuzhiyun 	return __nla_parse(tb, maxtype, head, len, policy,
656*4882a593Smuzhiyun 			   NL_VALIDATE_STRICT, extack);
657*4882a593Smuzhiyun }
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun /**
660*4882a593Smuzhiyun  * nla_parse_deprecated - Parse a stream of attributes into a tb buffer
661*4882a593Smuzhiyun  * @tb: destination array with maxtype+1 elements
662*4882a593Smuzhiyun  * @maxtype: maximum attribute type to be expected
663*4882a593Smuzhiyun  * @head: head of attribute stream
664*4882a593Smuzhiyun  * @len: length of attribute stream
665*4882a593Smuzhiyun  * @policy: validation policy
666*4882a593Smuzhiyun  * @extack: extended ACK pointer
667*4882a593Smuzhiyun  *
668*4882a593Smuzhiyun  * Parses a stream of attributes and stores a pointer to each attribute in
669*4882a593Smuzhiyun  * the tb array accessible via the attribute type. Attributes with a type
670*4882a593Smuzhiyun  * exceeding maxtype will be ignored and attributes from the policy are not
671*4882a593Smuzhiyun  * always strictly validated (only for new attributes).
672*4882a593Smuzhiyun  *
673*4882a593Smuzhiyun  * Returns 0 on success or a negative error code.
674*4882a593Smuzhiyun  */
nla_parse_deprecated(struct nlattr ** tb,int maxtype,const struct nlattr * head,int len,const struct nla_policy * policy,struct netlink_ext_ack * extack)675*4882a593Smuzhiyun static inline int nla_parse_deprecated(struct nlattr **tb, int maxtype,
676*4882a593Smuzhiyun 				       const struct nlattr *head, int len,
677*4882a593Smuzhiyun 				       const struct nla_policy *policy,
678*4882a593Smuzhiyun 				       struct netlink_ext_ack *extack)
679*4882a593Smuzhiyun {
680*4882a593Smuzhiyun 	return __nla_parse(tb, maxtype, head, len, policy,
681*4882a593Smuzhiyun 			   NL_VALIDATE_LIBERAL, extack);
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun /**
685*4882a593Smuzhiyun  * nla_parse_deprecated_strict - Parse a stream of attributes into a tb buffer
686*4882a593Smuzhiyun  * @tb: destination array with maxtype+1 elements
687*4882a593Smuzhiyun  * @maxtype: maximum attribute type to be expected
688*4882a593Smuzhiyun  * @head: head of attribute stream
689*4882a593Smuzhiyun  * @len: length of attribute stream
690*4882a593Smuzhiyun  * @policy: validation policy
691*4882a593Smuzhiyun  * @extack: extended ACK pointer
692*4882a593Smuzhiyun  *
693*4882a593Smuzhiyun  * Parses a stream of attributes and stores a pointer to each attribute in
694*4882a593Smuzhiyun  * the tb array accessible via the attribute type. Attributes with a type
695*4882a593Smuzhiyun  * exceeding maxtype will be rejected as well as trailing data, but the
696*4882a593Smuzhiyun  * policy is not completely strictly validated (only for new attributes).
697*4882a593Smuzhiyun  *
698*4882a593Smuzhiyun  * Returns 0 on success or a negative error code.
699*4882a593Smuzhiyun  */
nla_parse_deprecated_strict(struct nlattr ** tb,int maxtype,const struct nlattr * head,int len,const struct nla_policy * policy,struct netlink_ext_ack * extack)700*4882a593Smuzhiyun static inline int nla_parse_deprecated_strict(struct nlattr **tb, int maxtype,
701*4882a593Smuzhiyun 					      const struct nlattr *head,
702*4882a593Smuzhiyun 					      int len,
703*4882a593Smuzhiyun 					      const struct nla_policy *policy,
704*4882a593Smuzhiyun 					      struct netlink_ext_ack *extack)
705*4882a593Smuzhiyun {
706*4882a593Smuzhiyun 	return __nla_parse(tb, maxtype, head, len, policy,
707*4882a593Smuzhiyun 			   NL_VALIDATE_DEPRECATED_STRICT, extack);
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun /**
711*4882a593Smuzhiyun  * __nlmsg_parse - parse attributes of a netlink message
712*4882a593Smuzhiyun  * @nlh: netlink message header
713*4882a593Smuzhiyun  * @hdrlen: length of family specific header
714*4882a593Smuzhiyun  * @tb: destination array with maxtype+1 elements
715*4882a593Smuzhiyun  * @maxtype: maximum attribute type to be expected
716*4882a593Smuzhiyun  * @policy: validation policy
717*4882a593Smuzhiyun  * @validate: validation strictness
718*4882a593Smuzhiyun  * @extack: extended ACK report struct
719*4882a593Smuzhiyun  *
720*4882a593Smuzhiyun  * See nla_parse()
721*4882a593Smuzhiyun  */
__nlmsg_parse(const struct nlmsghdr * nlh,int hdrlen,struct nlattr * tb[],int maxtype,const struct nla_policy * policy,unsigned int validate,struct netlink_ext_ack * extack)722*4882a593Smuzhiyun static inline int __nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
723*4882a593Smuzhiyun 				struct nlattr *tb[], int maxtype,
724*4882a593Smuzhiyun 				const struct nla_policy *policy,
725*4882a593Smuzhiyun 				unsigned int validate,
726*4882a593Smuzhiyun 				struct netlink_ext_ack *extack)
727*4882a593Smuzhiyun {
728*4882a593Smuzhiyun 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) {
729*4882a593Smuzhiyun 		NL_SET_ERR_MSG(extack, "Invalid header length");
730*4882a593Smuzhiyun 		return -EINVAL;
731*4882a593Smuzhiyun 	}
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	return __nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
734*4882a593Smuzhiyun 			   nlmsg_attrlen(nlh, hdrlen), policy, validate,
735*4882a593Smuzhiyun 			   extack);
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun /**
739*4882a593Smuzhiyun  * nlmsg_parse - parse attributes of a netlink message
740*4882a593Smuzhiyun  * @nlh: netlink message header
741*4882a593Smuzhiyun  * @hdrlen: length of family specific header
742*4882a593Smuzhiyun  * @tb: destination array with maxtype+1 elements
743*4882a593Smuzhiyun  * @maxtype: maximum attribute type to be expected
744*4882a593Smuzhiyun  * @extack: extended ACK report struct
745*4882a593Smuzhiyun  *
746*4882a593Smuzhiyun  * See nla_parse()
747*4882a593Smuzhiyun  */
nlmsg_parse(const struct nlmsghdr * nlh,int hdrlen,struct nlattr * tb[],int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)748*4882a593Smuzhiyun static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
749*4882a593Smuzhiyun 			      struct nlattr *tb[], int maxtype,
750*4882a593Smuzhiyun 			      const struct nla_policy *policy,
751*4882a593Smuzhiyun 			      struct netlink_ext_ack *extack)
752*4882a593Smuzhiyun {
753*4882a593Smuzhiyun 	return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
754*4882a593Smuzhiyun 			     NL_VALIDATE_STRICT, extack);
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun /**
758*4882a593Smuzhiyun  * nlmsg_parse_deprecated - parse attributes of a netlink message
759*4882a593Smuzhiyun  * @nlh: netlink message header
760*4882a593Smuzhiyun  * @hdrlen: length of family specific header
761*4882a593Smuzhiyun  * @tb: destination array with maxtype+1 elements
762*4882a593Smuzhiyun  * @maxtype: maximum attribute type to be expected
763*4882a593Smuzhiyun  * @extack: extended ACK report struct
764*4882a593Smuzhiyun  *
765*4882a593Smuzhiyun  * See nla_parse_deprecated()
766*4882a593Smuzhiyun  */
nlmsg_parse_deprecated(const struct nlmsghdr * nlh,int hdrlen,struct nlattr * tb[],int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)767*4882a593Smuzhiyun static inline int nlmsg_parse_deprecated(const struct nlmsghdr *nlh, int hdrlen,
768*4882a593Smuzhiyun 					 struct nlattr *tb[], int maxtype,
769*4882a593Smuzhiyun 					 const struct nla_policy *policy,
770*4882a593Smuzhiyun 					 struct netlink_ext_ack *extack)
771*4882a593Smuzhiyun {
772*4882a593Smuzhiyun 	return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
773*4882a593Smuzhiyun 			     NL_VALIDATE_LIBERAL, extack);
774*4882a593Smuzhiyun }
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun /**
777*4882a593Smuzhiyun  * nlmsg_parse_deprecated_strict - parse attributes of a netlink message
778*4882a593Smuzhiyun  * @nlh: netlink message header
779*4882a593Smuzhiyun  * @hdrlen: length of family specific header
780*4882a593Smuzhiyun  * @tb: destination array with maxtype+1 elements
781*4882a593Smuzhiyun  * @maxtype: maximum attribute type to be expected
782*4882a593Smuzhiyun  * @extack: extended ACK report struct
783*4882a593Smuzhiyun  *
784*4882a593Smuzhiyun  * See nla_parse_deprecated_strict()
785*4882a593Smuzhiyun  */
786*4882a593Smuzhiyun static inline int
nlmsg_parse_deprecated_strict(const struct nlmsghdr * nlh,int hdrlen,struct nlattr * tb[],int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)787*4882a593Smuzhiyun nlmsg_parse_deprecated_strict(const struct nlmsghdr *nlh, int hdrlen,
788*4882a593Smuzhiyun 			      struct nlattr *tb[], int maxtype,
789*4882a593Smuzhiyun 			      const struct nla_policy *policy,
790*4882a593Smuzhiyun 			      struct netlink_ext_ack *extack)
791*4882a593Smuzhiyun {
792*4882a593Smuzhiyun 	return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
793*4882a593Smuzhiyun 			     NL_VALIDATE_DEPRECATED_STRICT, extack);
794*4882a593Smuzhiyun }
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun /**
797*4882a593Smuzhiyun  * nlmsg_find_attr - find a specific attribute in a netlink message
798*4882a593Smuzhiyun  * @nlh: netlink message header
799*4882a593Smuzhiyun  * @hdrlen: length of familiy specific header
800*4882a593Smuzhiyun  * @attrtype: type of attribute to look for
801*4882a593Smuzhiyun  *
802*4882a593Smuzhiyun  * Returns the first attribute which matches the specified type.
803*4882a593Smuzhiyun  */
nlmsg_find_attr(const struct nlmsghdr * nlh,int hdrlen,int attrtype)804*4882a593Smuzhiyun static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
805*4882a593Smuzhiyun 					     int hdrlen, int attrtype)
806*4882a593Smuzhiyun {
807*4882a593Smuzhiyun 	return nla_find(nlmsg_attrdata(nlh, hdrlen),
808*4882a593Smuzhiyun 			nlmsg_attrlen(nlh, hdrlen), attrtype);
809*4882a593Smuzhiyun }
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun /**
812*4882a593Smuzhiyun  * nla_validate_deprecated - Validate a stream of attributes
813*4882a593Smuzhiyun  * @head: head of attribute stream
814*4882a593Smuzhiyun  * @len: length of attribute stream
815*4882a593Smuzhiyun  * @maxtype: maximum attribute type to be expected
816*4882a593Smuzhiyun  * @policy: validation policy
817*4882a593Smuzhiyun  * @validate: validation strictness
818*4882a593Smuzhiyun  * @extack: extended ACK report struct
819*4882a593Smuzhiyun  *
820*4882a593Smuzhiyun  * Validates all attributes in the specified attribute stream against the
821*4882a593Smuzhiyun  * specified policy. Validation is done in liberal mode.
822*4882a593Smuzhiyun  * See documenation of struct nla_policy for more details.
823*4882a593Smuzhiyun  *
824*4882a593Smuzhiyun  * Returns 0 on success or a negative error code.
825*4882a593Smuzhiyun  */
nla_validate_deprecated(const struct nlattr * head,int len,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)826*4882a593Smuzhiyun static inline int nla_validate_deprecated(const struct nlattr *head, int len,
827*4882a593Smuzhiyun 					  int maxtype,
828*4882a593Smuzhiyun 					  const struct nla_policy *policy,
829*4882a593Smuzhiyun 					  struct netlink_ext_ack *extack)
830*4882a593Smuzhiyun {
831*4882a593Smuzhiyun 	return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_LIBERAL,
832*4882a593Smuzhiyun 			      extack);
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun /**
836*4882a593Smuzhiyun  * nla_validate - Validate a stream of attributes
837*4882a593Smuzhiyun  * @head: head of attribute stream
838*4882a593Smuzhiyun  * @len: length of attribute stream
839*4882a593Smuzhiyun  * @maxtype: maximum attribute type to be expected
840*4882a593Smuzhiyun  * @policy: validation policy
841*4882a593Smuzhiyun  * @extack: extended ACK report struct
842*4882a593Smuzhiyun  *
843*4882a593Smuzhiyun  * Validates all attributes in the specified attribute stream against the
844*4882a593Smuzhiyun  * specified policy. Validation is done in strict mode.
845*4882a593Smuzhiyun  * See documenation of struct nla_policy for more details.
846*4882a593Smuzhiyun  *
847*4882a593Smuzhiyun  * Returns 0 on success or a negative error code.
848*4882a593Smuzhiyun  */
nla_validate(const struct nlattr * head,int len,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)849*4882a593Smuzhiyun static inline int nla_validate(const struct nlattr *head, int len, int maxtype,
850*4882a593Smuzhiyun 			       const struct nla_policy *policy,
851*4882a593Smuzhiyun 			       struct netlink_ext_ack *extack)
852*4882a593Smuzhiyun {
853*4882a593Smuzhiyun 	return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_STRICT,
854*4882a593Smuzhiyun 			      extack);
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun /**
858*4882a593Smuzhiyun  * nlmsg_validate_deprecated - validate a netlink message including attributes
859*4882a593Smuzhiyun  * @nlh: netlinket message header
860*4882a593Smuzhiyun  * @hdrlen: length of familiy specific header
861*4882a593Smuzhiyun  * @maxtype: maximum attribute type to be expected
862*4882a593Smuzhiyun  * @policy: validation policy
863*4882a593Smuzhiyun  * @extack: extended ACK report struct
864*4882a593Smuzhiyun  */
nlmsg_validate_deprecated(const struct nlmsghdr * nlh,int hdrlen,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)865*4882a593Smuzhiyun static inline int nlmsg_validate_deprecated(const struct nlmsghdr *nlh,
866*4882a593Smuzhiyun 					    int hdrlen, int maxtype,
867*4882a593Smuzhiyun 					    const struct nla_policy *policy,
868*4882a593Smuzhiyun 					    struct netlink_ext_ack *extack)
869*4882a593Smuzhiyun {
870*4882a593Smuzhiyun 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
871*4882a593Smuzhiyun 		return -EINVAL;
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	return __nla_validate(nlmsg_attrdata(nlh, hdrlen),
874*4882a593Smuzhiyun 			      nlmsg_attrlen(nlh, hdrlen), maxtype,
875*4882a593Smuzhiyun 			      policy, NL_VALIDATE_LIBERAL, extack);
876*4882a593Smuzhiyun }
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun /**
881*4882a593Smuzhiyun  * nlmsg_report - need to report back to application?
882*4882a593Smuzhiyun  * @nlh: netlink message header
883*4882a593Smuzhiyun  *
884*4882a593Smuzhiyun  * Returns 1 if a report back to the application is requested.
885*4882a593Smuzhiyun  */
nlmsg_report(const struct nlmsghdr * nlh)886*4882a593Smuzhiyun static inline int nlmsg_report(const struct nlmsghdr *nlh)
887*4882a593Smuzhiyun {
888*4882a593Smuzhiyun 	return !!(nlh->nlmsg_flags & NLM_F_ECHO);
889*4882a593Smuzhiyun }
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun /**
892*4882a593Smuzhiyun  * nlmsg_for_each_attr - iterate over a stream of attributes
893*4882a593Smuzhiyun  * @pos: loop counter, set to current attribute
894*4882a593Smuzhiyun  * @nlh: netlink message header
895*4882a593Smuzhiyun  * @hdrlen: length of familiy specific header
896*4882a593Smuzhiyun  * @rem: initialized to len, holds bytes currently remaining in stream
897*4882a593Smuzhiyun  */
898*4882a593Smuzhiyun #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
899*4882a593Smuzhiyun 	nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
900*4882a593Smuzhiyun 			  nlmsg_attrlen(nlh, hdrlen), rem)
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun /**
903*4882a593Smuzhiyun  * nlmsg_put - Add a new netlink message to an skb
904*4882a593Smuzhiyun  * @skb: socket buffer to store message in
905*4882a593Smuzhiyun  * @portid: netlink PORTID of requesting application
906*4882a593Smuzhiyun  * @seq: sequence number of message
907*4882a593Smuzhiyun  * @type: message type
908*4882a593Smuzhiyun  * @payload: length of message payload
909*4882a593Smuzhiyun  * @flags: message flags
910*4882a593Smuzhiyun  *
911*4882a593Smuzhiyun  * Returns NULL if the tailroom of the skb is insufficient to store
912*4882a593Smuzhiyun  * the message header and payload.
913*4882a593Smuzhiyun  */
nlmsg_put(struct sk_buff * skb,u32 portid,u32 seq,int type,int payload,int flags)914*4882a593Smuzhiyun static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
915*4882a593Smuzhiyun 					 int type, int payload, int flags)
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun 	if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
918*4882a593Smuzhiyun 		return NULL;
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	return __nlmsg_put(skb, portid, seq, type, payload, flags);
921*4882a593Smuzhiyun }
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun /**
924*4882a593Smuzhiyun  * nlmsg_put_answer - Add a new callback based netlink message to an skb
925*4882a593Smuzhiyun  * @skb: socket buffer to store message in
926*4882a593Smuzhiyun  * @cb: netlink callback
927*4882a593Smuzhiyun  * @type: message type
928*4882a593Smuzhiyun  * @payload: length of message payload
929*4882a593Smuzhiyun  * @flags: message flags
930*4882a593Smuzhiyun  *
931*4882a593Smuzhiyun  * Returns NULL if the tailroom of the skb is insufficient to store
932*4882a593Smuzhiyun  * the message header and payload.
933*4882a593Smuzhiyun  */
nlmsg_put_answer(struct sk_buff * skb,struct netlink_callback * cb,int type,int payload,int flags)934*4882a593Smuzhiyun static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
935*4882a593Smuzhiyun 						struct netlink_callback *cb,
936*4882a593Smuzhiyun 						int type, int payload,
937*4882a593Smuzhiyun 						int flags)
938*4882a593Smuzhiyun {
939*4882a593Smuzhiyun 	return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
940*4882a593Smuzhiyun 			 type, payload, flags);
941*4882a593Smuzhiyun }
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun /**
944*4882a593Smuzhiyun  * nlmsg_new - Allocate a new netlink message
945*4882a593Smuzhiyun  * @payload: size of the message payload
946*4882a593Smuzhiyun  * @flags: the type of memory to allocate.
947*4882a593Smuzhiyun  *
948*4882a593Smuzhiyun  * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
949*4882a593Smuzhiyun  * and a good default is needed.
950*4882a593Smuzhiyun  */
nlmsg_new(size_t payload,gfp_t flags)951*4882a593Smuzhiyun static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
952*4882a593Smuzhiyun {
953*4882a593Smuzhiyun 	return alloc_skb(nlmsg_total_size(payload), flags);
954*4882a593Smuzhiyun }
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun /**
957*4882a593Smuzhiyun  * nlmsg_end - Finalize a netlink message
958*4882a593Smuzhiyun  * @skb: socket buffer the message is stored in
959*4882a593Smuzhiyun  * @nlh: netlink message header
960*4882a593Smuzhiyun  *
961*4882a593Smuzhiyun  * Corrects the netlink message header to include the appeneded
962*4882a593Smuzhiyun  * attributes. Only necessary if attributes have been added to
963*4882a593Smuzhiyun  * the message.
964*4882a593Smuzhiyun  */
nlmsg_end(struct sk_buff * skb,struct nlmsghdr * nlh)965*4882a593Smuzhiyun static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
966*4882a593Smuzhiyun {
967*4882a593Smuzhiyun 	nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
968*4882a593Smuzhiyun }
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun /**
971*4882a593Smuzhiyun  * nlmsg_get_pos - return current position in netlink message
972*4882a593Smuzhiyun  * @skb: socket buffer the message is stored in
973*4882a593Smuzhiyun  *
974*4882a593Smuzhiyun  * Returns a pointer to the current tail of the message.
975*4882a593Smuzhiyun  */
nlmsg_get_pos(struct sk_buff * skb)976*4882a593Smuzhiyun static inline void *nlmsg_get_pos(struct sk_buff *skb)
977*4882a593Smuzhiyun {
978*4882a593Smuzhiyun 	return skb_tail_pointer(skb);
979*4882a593Smuzhiyun }
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun /**
982*4882a593Smuzhiyun  * nlmsg_trim - Trim message to a mark
983*4882a593Smuzhiyun  * @skb: socket buffer the message is stored in
984*4882a593Smuzhiyun  * @mark: mark to trim to
985*4882a593Smuzhiyun  *
986*4882a593Smuzhiyun  * Trims the message to the provided mark.
987*4882a593Smuzhiyun  */
nlmsg_trim(struct sk_buff * skb,const void * mark)988*4882a593Smuzhiyun static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
989*4882a593Smuzhiyun {
990*4882a593Smuzhiyun 	if (mark) {
991*4882a593Smuzhiyun 		WARN_ON((unsigned char *) mark < skb->data);
992*4882a593Smuzhiyun 		skb_trim(skb, (unsigned char *) mark - skb->data);
993*4882a593Smuzhiyun 	}
994*4882a593Smuzhiyun }
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun /**
997*4882a593Smuzhiyun  * nlmsg_cancel - Cancel construction of a netlink message
998*4882a593Smuzhiyun  * @skb: socket buffer the message is stored in
999*4882a593Smuzhiyun  * @nlh: netlink message header
1000*4882a593Smuzhiyun  *
1001*4882a593Smuzhiyun  * Removes the complete netlink message including all
1002*4882a593Smuzhiyun  * attributes from the socket buffer again.
1003*4882a593Smuzhiyun  */
nlmsg_cancel(struct sk_buff * skb,struct nlmsghdr * nlh)1004*4882a593Smuzhiyun static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
1005*4882a593Smuzhiyun {
1006*4882a593Smuzhiyun 	nlmsg_trim(skb, nlh);
1007*4882a593Smuzhiyun }
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun /**
1010*4882a593Smuzhiyun  * nlmsg_free - free a netlink message
1011*4882a593Smuzhiyun  * @skb: socket buffer of netlink message
1012*4882a593Smuzhiyun  */
nlmsg_free(struct sk_buff * skb)1013*4882a593Smuzhiyun static inline void nlmsg_free(struct sk_buff *skb)
1014*4882a593Smuzhiyun {
1015*4882a593Smuzhiyun 	kfree_skb(skb);
1016*4882a593Smuzhiyun }
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun /**
1019*4882a593Smuzhiyun  * nlmsg_multicast - multicast a netlink message
1020*4882a593Smuzhiyun  * @sk: netlink socket to spread messages to
1021*4882a593Smuzhiyun  * @skb: netlink message as socket buffer
1022*4882a593Smuzhiyun  * @portid: own netlink portid to avoid sending to yourself
1023*4882a593Smuzhiyun  * @group: multicast group id
1024*4882a593Smuzhiyun  * @flags: allocation flags
1025*4882a593Smuzhiyun  */
nlmsg_multicast(struct sock * sk,struct sk_buff * skb,u32 portid,unsigned int group,gfp_t flags)1026*4882a593Smuzhiyun static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
1027*4882a593Smuzhiyun 				  u32 portid, unsigned int group, gfp_t flags)
1028*4882a593Smuzhiyun {
1029*4882a593Smuzhiyun 	int err;
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun 	NETLINK_CB(skb).dst_group = group;
1032*4882a593Smuzhiyun 
1033*4882a593Smuzhiyun 	err = netlink_broadcast(sk, skb, portid, group, flags);
1034*4882a593Smuzhiyun 	if (err > 0)
1035*4882a593Smuzhiyun 		err = 0;
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 	return err;
1038*4882a593Smuzhiyun }
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun /**
1041*4882a593Smuzhiyun  * nlmsg_unicast - unicast a netlink message
1042*4882a593Smuzhiyun  * @sk: netlink socket to spread message to
1043*4882a593Smuzhiyun  * @skb: netlink message as socket buffer
1044*4882a593Smuzhiyun  * @portid: netlink portid of the destination socket
1045*4882a593Smuzhiyun  */
nlmsg_unicast(struct sock * sk,struct sk_buff * skb,u32 portid)1046*4882a593Smuzhiyun static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
1047*4882a593Smuzhiyun {
1048*4882a593Smuzhiyun 	int err;
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
1051*4882a593Smuzhiyun 	if (err > 0)
1052*4882a593Smuzhiyun 		err = 0;
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun 	return err;
1055*4882a593Smuzhiyun }
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun /**
1058*4882a593Smuzhiyun  * nlmsg_for_each_msg - iterate over a stream of messages
1059*4882a593Smuzhiyun  * @pos: loop counter, set to current message
1060*4882a593Smuzhiyun  * @head: head of message stream
1061*4882a593Smuzhiyun  * @len: length of message stream
1062*4882a593Smuzhiyun  * @rem: initialized to len, holds bytes currently remaining in stream
1063*4882a593Smuzhiyun  */
1064*4882a593Smuzhiyun #define nlmsg_for_each_msg(pos, head, len, rem) \
1065*4882a593Smuzhiyun 	for (pos = head, rem = len; \
1066*4882a593Smuzhiyun 	     nlmsg_ok(pos, rem); \
1067*4882a593Smuzhiyun 	     pos = nlmsg_next(pos, &(rem)))
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun /**
1070*4882a593Smuzhiyun  * nl_dump_check_consistent - check if sequence is consistent and advertise if not
1071*4882a593Smuzhiyun  * @cb: netlink callback structure that stores the sequence number
1072*4882a593Smuzhiyun  * @nlh: netlink message header to write the flag to
1073*4882a593Smuzhiyun  *
1074*4882a593Smuzhiyun  * This function checks if the sequence (generation) number changed during dump
1075*4882a593Smuzhiyun  * and if it did, advertises it in the netlink message header.
1076*4882a593Smuzhiyun  *
1077*4882a593Smuzhiyun  * The correct way to use it is to set cb->seq to the generation counter when
1078*4882a593Smuzhiyun  * all locks for dumping have been acquired, and then call this function for
1079*4882a593Smuzhiyun  * each message that is generated.
1080*4882a593Smuzhiyun  *
1081*4882a593Smuzhiyun  * Note that due to initialisation concerns, 0 is an invalid sequence number
1082*4882a593Smuzhiyun  * and must not be used by code that uses this functionality.
1083*4882a593Smuzhiyun  */
1084*4882a593Smuzhiyun static inline void
nl_dump_check_consistent(struct netlink_callback * cb,struct nlmsghdr * nlh)1085*4882a593Smuzhiyun nl_dump_check_consistent(struct netlink_callback *cb,
1086*4882a593Smuzhiyun 			 struct nlmsghdr *nlh)
1087*4882a593Smuzhiyun {
1088*4882a593Smuzhiyun 	if (cb->prev_seq && cb->seq != cb->prev_seq)
1089*4882a593Smuzhiyun 		nlh->nlmsg_flags |= NLM_F_DUMP_INTR;
1090*4882a593Smuzhiyun 	cb->prev_seq = cb->seq;
1091*4882a593Smuzhiyun }
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun /**************************************************************************
1094*4882a593Smuzhiyun  * Netlink Attributes
1095*4882a593Smuzhiyun  **************************************************************************/
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun /**
1098*4882a593Smuzhiyun  * nla_attr_size - length of attribute not including padding
1099*4882a593Smuzhiyun  * @payload: length of payload
1100*4882a593Smuzhiyun  */
nla_attr_size(int payload)1101*4882a593Smuzhiyun static inline int nla_attr_size(int payload)
1102*4882a593Smuzhiyun {
1103*4882a593Smuzhiyun 	return NLA_HDRLEN + payload;
1104*4882a593Smuzhiyun }
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun /**
1107*4882a593Smuzhiyun  * nla_total_size - total length of attribute including padding
1108*4882a593Smuzhiyun  * @payload: length of payload
1109*4882a593Smuzhiyun  */
nla_total_size(int payload)1110*4882a593Smuzhiyun static inline int nla_total_size(int payload)
1111*4882a593Smuzhiyun {
1112*4882a593Smuzhiyun 	return NLA_ALIGN(nla_attr_size(payload));
1113*4882a593Smuzhiyun }
1114*4882a593Smuzhiyun 
1115*4882a593Smuzhiyun /**
1116*4882a593Smuzhiyun  * nla_padlen - length of padding at the tail of attribute
1117*4882a593Smuzhiyun  * @payload: length of payload
1118*4882a593Smuzhiyun  */
nla_padlen(int payload)1119*4882a593Smuzhiyun static inline int nla_padlen(int payload)
1120*4882a593Smuzhiyun {
1121*4882a593Smuzhiyun 	return nla_total_size(payload) - nla_attr_size(payload);
1122*4882a593Smuzhiyun }
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun /**
1125*4882a593Smuzhiyun  * nla_type - attribute type
1126*4882a593Smuzhiyun  * @nla: netlink attribute
1127*4882a593Smuzhiyun  */
nla_type(const struct nlattr * nla)1128*4882a593Smuzhiyun static inline int nla_type(const struct nlattr *nla)
1129*4882a593Smuzhiyun {
1130*4882a593Smuzhiyun 	return nla->nla_type & NLA_TYPE_MASK;
1131*4882a593Smuzhiyun }
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun /**
1134*4882a593Smuzhiyun  * nla_data - head of payload
1135*4882a593Smuzhiyun  * @nla: netlink attribute
1136*4882a593Smuzhiyun  */
nla_data(const struct nlattr * nla)1137*4882a593Smuzhiyun static inline void *nla_data(const struct nlattr *nla)
1138*4882a593Smuzhiyun {
1139*4882a593Smuzhiyun 	return (char *) nla + NLA_HDRLEN;
1140*4882a593Smuzhiyun }
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun /**
1143*4882a593Smuzhiyun  * nla_len - length of payload
1144*4882a593Smuzhiyun  * @nla: netlink attribute
1145*4882a593Smuzhiyun  */
nla_len(const struct nlattr * nla)1146*4882a593Smuzhiyun static inline int nla_len(const struct nlattr *nla)
1147*4882a593Smuzhiyun {
1148*4882a593Smuzhiyun 	return nla->nla_len - NLA_HDRLEN;
1149*4882a593Smuzhiyun }
1150*4882a593Smuzhiyun 
1151*4882a593Smuzhiyun /**
1152*4882a593Smuzhiyun  * nla_ok - check if the netlink attribute fits into the remaining bytes
1153*4882a593Smuzhiyun  * @nla: netlink attribute
1154*4882a593Smuzhiyun  * @remaining: number of bytes remaining in attribute stream
1155*4882a593Smuzhiyun  */
nla_ok(const struct nlattr * nla,int remaining)1156*4882a593Smuzhiyun static inline int nla_ok(const struct nlattr *nla, int remaining)
1157*4882a593Smuzhiyun {
1158*4882a593Smuzhiyun 	return remaining >= (int) sizeof(*nla) &&
1159*4882a593Smuzhiyun 	       nla->nla_len >= sizeof(*nla) &&
1160*4882a593Smuzhiyun 	       nla->nla_len <= remaining;
1161*4882a593Smuzhiyun }
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun /**
1164*4882a593Smuzhiyun  * nla_next - next netlink attribute in attribute stream
1165*4882a593Smuzhiyun  * @nla: netlink attribute
1166*4882a593Smuzhiyun  * @remaining: number of bytes remaining in attribute stream
1167*4882a593Smuzhiyun  *
1168*4882a593Smuzhiyun  * Returns the next netlink attribute in the attribute stream and
1169*4882a593Smuzhiyun  * decrements remaining by the size of the current attribute.
1170*4882a593Smuzhiyun  */
nla_next(const struct nlattr * nla,int * remaining)1171*4882a593Smuzhiyun static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
1172*4882a593Smuzhiyun {
1173*4882a593Smuzhiyun 	unsigned int totlen = NLA_ALIGN(nla->nla_len);
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun 	*remaining -= totlen;
1176*4882a593Smuzhiyun 	return (struct nlattr *) ((char *) nla + totlen);
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun /**
1180*4882a593Smuzhiyun  * nla_find_nested - find attribute in a set of nested attributes
1181*4882a593Smuzhiyun  * @nla: attribute containing the nested attributes
1182*4882a593Smuzhiyun  * @attrtype: type of attribute to look for
1183*4882a593Smuzhiyun  *
1184*4882a593Smuzhiyun  * Returns the first attribute which matches the specified type.
1185*4882a593Smuzhiyun  */
1186*4882a593Smuzhiyun static inline struct nlattr *
nla_find_nested(const struct nlattr * nla,int attrtype)1187*4882a593Smuzhiyun nla_find_nested(const struct nlattr *nla, int attrtype)
1188*4882a593Smuzhiyun {
1189*4882a593Smuzhiyun 	return nla_find(nla_data(nla), nla_len(nla), attrtype);
1190*4882a593Smuzhiyun }
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun /**
1193*4882a593Smuzhiyun  * nla_parse_nested - parse nested attributes
1194*4882a593Smuzhiyun  * @tb: destination array with maxtype+1 elements
1195*4882a593Smuzhiyun  * @maxtype: maximum attribute type to be expected
1196*4882a593Smuzhiyun  * @nla: attribute containing the nested attributes
1197*4882a593Smuzhiyun  * @policy: validation policy
1198*4882a593Smuzhiyun  * @extack: extended ACK report struct
1199*4882a593Smuzhiyun  *
1200*4882a593Smuzhiyun  * See nla_parse()
1201*4882a593Smuzhiyun  */
nla_parse_nested(struct nlattr * tb[],int maxtype,const struct nlattr * nla,const struct nla_policy * policy,struct netlink_ext_ack * extack)1202*4882a593Smuzhiyun static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
1203*4882a593Smuzhiyun 				   const struct nlattr *nla,
1204*4882a593Smuzhiyun 				   const struct nla_policy *policy,
1205*4882a593Smuzhiyun 				   struct netlink_ext_ack *extack)
1206*4882a593Smuzhiyun {
1207*4882a593Smuzhiyun 	if (!(nla->nla_type & NLA_F_NESTED)) {
1208*4882a593Smuzhiyun 		NL_SET_ERR_MSG_ATTR(extack, nla, "NLA_F_NESTED is missing");
1209*4882a593Smuzhiyun 		return -EINVAL;
1210*4882a593Smuzhiyun 	}
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 	return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
1213*4882a593Smuzhiyun 			   NL_VALIDATE_STRICT, extack);
1214*4882a593Smuzhiyun }
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun /**
1217*4882a593Smuzhiyun  * nla_parse_nested_deprecated - parse nested attributes
1218*4882a593Smuzhiyun  * @tb: destination array with maxtype+1 elements
1219*4882a593Smuzhiyun  * @maxtype: maximum attribute type to be expected
1220*4882a593Smuzhiyun  * @nla: attribute containing the nested attributes
1221*4882a593Smuzhiyun  * @policy: validation policy
1222*4882a593Smuzhiyun  * @extack: extended ACK report struct
1223*4882a593Smuzhiyun  *
1224*4882a593Smuzhiyun  * See nla_parse_deprecated()
1225*4882a593Smuzhiyun  */
nla_parse_nested_deprecated(struct nlattr * tb[],int maxtype,const struct nlattr * nla,const struct nla_policy * policy,struct netlink_ext_ack * extack)1226*4882a593Smuzhiyun static inline int nla_parse_nested_deprecated(struct nlattr *tb[], int maxtype,
1227*4882a593Smuzhiyun 					      const struct nlattr *nla,
1228*4882a593Smuzhiyun 					      const struct nla_policy *policy,
1229*4882a593Smuzhiyun 					      struct netlink_ext_ack *extack)
1230*4882a593Smuzhiyun {
1231*4882a593Smuzhiyun 	return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
1232*4882a593Smuzhiyun 			   NL_VALIDATE_LIBERAL, extack);
1233*4882a593Smuzhiyun }
1234*4882a593Smuzhiyun 
1235*4882a593Smuzhiyun /**
1236*4882a593Smuzhiyun  * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
1237*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1238*4882a593Smuzhiyun  * @attrtype: attribute type
1239*4882a593Smuzhiyun  * @value: numeric value
1240*4882a593Smuzhiyun  */
nla_put_u8(struct sk_buff * skb,int attrtype,u8 value)1241*4882a593Smuzhiyun static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
1242*4882a593Smuzhiyun {
1243*4882a593Smuzhiyun 	/* temporary variables to work around GCC PR81715 with asan-stack=1 */
1244*4882a593Smuzhiyun 	u8 tmp = value;
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 	return nla_put(skb, attrtype, sizeof(u8), &tmp);
1247*4882a593Smuzhiyun }
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun /**
1250*4882a593Smuzhiyun  * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
1251*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1252*4882a593Smuzhiyun  * @attrtype: attribute type
1253*4882a593Smuzhiyun  * @value: numeric value
1254*4882a593Smuzhiyun  */
nla_put_u16(struct sk_buff * skb,int attrtype,u16 value)1255*4882a593Smuzhiyun static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
1256*4882a593Smuzhiyun {
1257*4882a593Smuzhiyun 	u16 tmp = value;
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun 	return nla_put(skb, attrtype, sizeof(u16), &tmp);
1260*4882a593Smuzhiyun }
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun /**
1263*4882a593Smuzhiyun  * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer
1264*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1265*4882a593Smuzhiyun  * @attrtype: attribute type
1266*4882a593Smuzhiyun  * @value: numeric value
1267*4882a593Smuzhiyun  */
nla_put_be16(struct sk_buff * skb,int attrtype,__be16 value)1268*4882a593Smuzhiyun static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
1269*4882a593Smuzhiyun {
1270*4882a593Smuzhiyun 	__be16 tmp = value;
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun 	return nla_put(skb, attrtype, sizeof(__be16), &tmp);
1273*4882a593Smuzhiyun }
1274*4882a593Smuzhiyun 
1275*4882a593Smuzhiyun /**
1276*4882a593Smuzhiyun  * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer
1277*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1278*4882a593Smuzhiyun  * @attrtype: attribute type
1279*4882a593Smuzhiyun  * @value: numeric value
1280*4882a593Smuzhiyun  */
nla_put_net16(struct sk_buff * skb,int attrtype,__be16 value)1281*4882a593Smuzhiyun static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
1282*4882a593Smuzhiyun {
1283*4882a593Smuzhiyun 	__be16 tmp = value;
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun 	return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
1286*4882a593Smuzhiyun }
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun /**
1289*4882a593Smuzhiyun  * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer
1290*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1291*4882a593Smuzhiyun  * @attrtype: attribute type
1292*4882a593Smuzhiyun  * @value: numeric value
1293*4882a593Smuzhiyun  */
nla_put_le16(struct sk_buff * skb,int attrtype,__le16 value)1294*4882a593Smuzhiyun static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
1295*4882a593Smuzhiyun {
1296*4882a593Smuzhiyun 	__le16 tmp = value;
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun 	return nla_put(skb, attrtype, sizeof(__le16), &tmp);
1299*4882a593Smuzhiyun }
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun /**
1302*4882a593Smuzhiyun  * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
1303*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1304*4882a593Smuzhiyun  * @attrtype: attribute type
1305*4882a593Smuzhiyun  * @value: numeric value
1306*4882a593Smuzhiyun  */
nla_put_u32(struct sk_buff * skb,int attrtype,u32 value)1307*4882a593Smuzhiyun static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
1308*4882a593Smuzhiyun {
1309*4882a593Smuzhiyun 	u32 tmp = value;
1310*4882a593Smuzhiyun 
1311*4882a593Smuzhiyun 	return nla_put(skb, attrtype, sizeof(u32), &tmp);
1312*4882a593Smuzhiyun }
1313*4882a593Smuzhiyun 
1314*4882a593Smuzhiyun /**
1315*4882a593Smuzhiyun  * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer
1316*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1317*4882a593Smuzhiyun  * @attrtype: attribute type
1318*4882a593Smuzhiyun  * @value: numeric value
1319*4882a593Smuzhiyun  */
nla_put_be32(struct sk_buff * skb,int attrtype,__be32 value)1320*4882a593Smuzhiyun static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
1321*4882a593Smuzhiyun {
1322*4882a593Smuzhiyun 	__be32 tmp = value;
1323*4882a593Smuzhiyun 
1324*4882a593Smuzhiyun 	return nla_put(skb, attrtype, sizeof(__be32), &tmp);
1325*4882a593Smuzhiyun }
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun /**
1328*4882a593Smuzhiyun  * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer
1329*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1330*4882a593Smuzhiyun  * @attrtype: attribute type
1331*4882a593Smuzhiyun  * @value: numeric value
1332*4882a593Smuzhiyun  */
nla_put_net32(struct sk_buff * skb,int attrtype,__be32 value)1333*4882a593Smuzhiyun static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
1334*4882a593Smuzhiyun {
1335*4882a593Smuzhiyun 	__be32 tmp = value;
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
1338*4882a593Smuzhiyun }
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun /**
1341*4882a593Smuzhiyun  * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer
1342*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1343*4882a593Smuzhiyun  * @attrtype: attribute type
1344*4882a593Smuzhiyun  * @value: numeric value
1345*4882a593Smuzhiyun  */
nla_put_le32(struct sk_buff * skb,int attrtype,__le32 value)1346*4882a593Smuzhiyun static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value)
1347*4882a593Smuzhiyun {
1348*4882a593Smuzhiyun 	__le32 tmp = value;
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun 	return nla_put(skb, attrtype, sizeof(__le32), &tmp);
1351*4882a593Smuzhiyun }
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun /**
1354*4882a593Smuzhiyun  * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it
1355*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1356*4882a593Smuzhiyun  * @attrtype: attribute type
1357*4882a593Smuzhiyun  * @value: numeric value
1358*4882a593Smuzhiyun  * @padattr: attribute type for the padding
1359*4882a593Smuzhiyun  */
nla_put_u64_64bit(struct sk_buff * skb,int attrtype,u64 value,int padattr)1360*4882a593Smuzhiyun static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype,
1361*4882a593Smuzhiyun 				    u64 value, int padattr)
1362*4882a593Smuzhiyun {
1363*4882a593Smuzhiyun 	u64 tmp = value;
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun 	return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
1366*4882a593Smuzhiyun }
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun /**
1369*4882a593Smuzhiyun  * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it
1370*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1371*4882a593Smuzhiyun  * @attrtype: attribute type
1372*4882a593Smuzhiyun  * @value: numeric value
1373*4882a593Smuzhiyun  * @padattr: attribute type for the padding
1374*4882a593Smuzhiyun  */
nla_put_be64(struct sk_buff * skb,int attrtype,__be64 value,int padattr)1375*4882a593Smuzhiyun static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value,
1376*4882a593Smuzhiyun 			       int padattr)
1377*4882a593Smuzhiyun {
1378*4882a593Smuzhiyun 	__be64 tmp = value;
1379*4882a593Smuzhiyun 
1380*4882a593Smuzhiyun 	return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr);
1381*4882a593Smuzhiyun }
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun /**
1384*4882a593Smuzhiyun  * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it
1385*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1386*4882a593Smuzhiyun  * @attrtype: attribute type
1387*4882a593Smuzhiyun  * @value: numeric value
1388*4882a593Smuzhiyun  * @padattr: attribute type for the padding
1389*4882a593Smuzhiyun  */
nla_put_net64(struct sk_buff * skb,int attrtype,__be64 value,int padattr)1390*4882a593Smuzhiyun static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value,
1391*4882a593Smuzhiyun 				int padattr)
1392*4882a593Smuzhiyun {
1393*4882a593Smuzhiyun 	__be64 tmp = value;
1394*4882a593Smuzhiyun 
1395*4882a593Smuzhiyun 	return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp,
1396*4882a593Smuzhiyun 			    padattr);
1397*4882a593Smuzhiyun }
1398*4882a593Smuzhiyun 
1399*4882a593Smuzhiyun /**
1400*4882a593Smuzhiyun  * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it
1401*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1402*4882a593Smuzhiyun  * @attrtype: attribute type
1403*4882a593Smuzhiyun  * @value: numeric value
1404*4882a593Smuzhiyun  * @padattr: attribute type for the padding
1405*4882a593Smuzhiyun  */
nla_put_le64(struct sk_buff * skb,int attrtype,__le64 value,int padattr)1406*4882a593Smuzhiyun static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value,
1407*4882a593Smuzhiyun 			       int padattr)
1408*4882a593Smuzhiyun {
1409*4882a593Smuzhiyun 	__le64 tmp = value;
1410*4882a593Smuzhiyun 
1411*4882a593Smuzhiyun 	return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr);
1412*4882a593Smuzhiyun }
1413*4882a593Smuzhiyun 
1414*4882a593Smuzhiyun /**
1415*4882a593Smuzhiyun  * nla_put_s8 - Add a s8 netlink attribute to a socket buffer
1416*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1417*4882a593Smuzhiyun  * @attrtype: attribute type
1418*4882a593Smuzhiyun  * @value: numeric value
1419*4882a593Smuzhiyun  */
nla_put_s8(struct sk_buff * skb,int attrtype,s8 value)1420*4882a593Smuzhiyun static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
1421*4882a593Smuzhiyun {
1422*4882a593Smuzhiyun 	s8 tmp = value;
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun 	return nla_put(skb, attrtype, sizeof(s8), &tmp);
1425*4882a593Smuzhiyun }
1426*4882a593Smuzhiyun 
1427*4882a593Smuzhiyun /**
1428*4882a593Smuzhiyun  * nla_put_s16 - Add a s16 netlink attribute to a socket buffer
1429*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1430*4882a593Smuzhiyun  * @attrtype: attribute type
1431*4882a593Smuzhiyun  * @value: numeric value
1432*4882a593Smuzhiyun  */
nla_put_s16(struct sk_buff * skb,int attrtype,s16 value)1433*4882a593Smuzhiyun static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
1434*4882a593Smuzhiyun {
1435*4882a593Smuzhiyun 	s16 tmp = value;
1436*4882a593Smuzhiyun 
1437*4882a593Smuzhiyun 	return nla_put(skb, attrtype, sizeof(s16), &tmp);
1438*4882a593Smuzhiyun }
1439*4882a593Smuzhiyun 
1440*4882a593Smuzhiyun /**
1441*4882a593Smuzhiyun  * nla_put_s32 - Add a s32 netlink attribute to a socket buffer
1442*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1443*4882a593Smuzhiyun  * @attrtype: attribute type
1444*4882a593Smuzhiyun  * @value: numeric value
1445*4882a593Smuzhiyun  */
nla_put_s32(struct sk_buff * skb,int attrtype,s32 value)1446*4882a593Smuzhiyun static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
1447*4882a593Smuzhiyun {
1448*4882a593Smuzhiyun 	s32 tmp = value;
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun 	return nla_put(skb, attrtype, sizeof(s32), &tmp);
1451*4882a593Smuzhiyun }
1452*4882a593Smuzhiyun 
1453*4882a593Smuzhiyun /**
1454*4882a593Smuzhiyun  * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it
1455*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1456*4882a593Smuzhiyun  * @attrtype: attribute type
1457*4882a593Smuzhiyun  * @value: numeric value
1458*4882a593Smuzhiyun  * @padattr: attribute type for the padding
1459*4882a593Smuzhiyun  */
nla_put_s64(struct sk_buff * skb,int attrtype,s64 value,int padattr)1460*4882a593Smuzhiyun static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value,
1461*4882a593Smuzhiyun 			      int padattr)
1462*4882a593Smuzhiyun {
1463*4882a593Smuzhiyun 	s64 tmp = value;
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun 	return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr);
1466*4882a593Smuzhiyun }
1467*4882a593Smuzhiyun 
1468*4882a593Smuzhiyun /**
1469*4882a593Smuzhiyun  * nla_put_string - Add a string netlink attribute to a socket buffer
1470*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1471*4882a593Smuzhiyun  * @attrtype: attribute type
1472*4882a593Smuzhiyun  * @str: NUL terminated string
1473*4882a593Smuzhiyun  */
nla_put_string(struct sk_buff * skb,int attrtype,const char * str)1474*4882a593Smuzhiyun static inline int nla_put_string(struct sk_buff *skb, int attrtype,
1475*4882a593Smuzhiyun 				 const char *str)
1476*4882a593Smuzhiyun {
1477*4882a593Smuzhiyun 	return nla_put(skb, attrtype, strlen(str) + 1, str);
1478*4882a593Smuzhiyun }
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun /**
1481*4882a593Smuzhiyun  * nla_put_flag - Add a flag netlink attribute to a socket buffer
1482*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1483*4882a593Smuzhiyun  * @attrtype: attribute type
1484*4882a593Smuzhiyun  */
nla_put_flag(struct sk_buff * skb,int attrtype)1485*4882a593Smuzhiyun static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
1486*4882a593Smuzhiyun {
1487*4882a593Smuzhiyun 	return nla_put(skb, attrtype, 0, NULL);
1488*4882a593Smuzhiyun }
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun /**
1491*4882a593Smuzhiyun  * nla_put_msecs - Add a msecs netlink attribute to a skb and align it
1492*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1493*4882a593Smuzhiyun  * @attrtype: attribute type
1494*4882a593Smuzhiyun  * @njiffies: number of jiffies to convert to msecs
1495*4882a593Smuzhiyun  * @padattr: attribute type for the padding
1496*4882a593Smuzhiyun  */
nla_put_msecs(struct sk_buff * skb,int attrtype,unsigned long njiffies,int padattr)1497*4882a593Smuzhiyun static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
1498*4882a593Smuzhiyun 				unsigned long njiffies, int padattr)
1499*4882a593Smuzhiyun {
1500*4882a593Smuzhiyun 	u64 tmp = jiffies_to_msecs(njiffies);
1501*4882a593Smuzhiyun 
1502*4882a593Smuzhiyun 	return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
1503*4882a593Smuzhiyun }
1504*4882a593Smuzhiyun 
1505*4882a593Smuzhiyun /**
1506*4882a593Smuzhiyun  * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket
1507*4882a593Smuzhiyun  * buffer
1508*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1509*4882a593Smuzhiyun  * @attrtype: attribute type
1510*4882a593Smuzhiyun  * @addr: IPv4 address
1511*4882a593Smuzhiyun  */
nla_put_in_addr(struct sk_buff * skb,int attrtype,__be32 addr)1512*4882a593Smuzhiyun static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
1513*4882a593Smuzhiyun 				  __be32 addr)
1514*4882a593Smuzhiyun {
1515*4882a593Smuzhiyun 	__be32 tmp = addr;
1516*4882a593Smuzhiyun 
1517*4882a593Smuzhiyun 	return nla_put_be32(skb, attrtype, tmp);
1518*4882a593Smuzhiyun }
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun /**
1521*4882a593Smuzhiyun  * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket
1522*4882a593Smuzhiyun  * buffer
1523*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1524*4882a593Smuzhiyun  * @attrtype: attribute type
1525*4882a593Smuzhiyun  * @addr: IPv6 address
1526*4882a593Smuzhiyun  */
nla_put_in6_addr(struct sk_buff * skb,int attrtype,const struct in6_addr * addr)1527*4882a593Smuzhiyun static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype,
1528*4882a593Smuzhiyun 				   const struct in6_addr *addr)
1529*4882a593Smuzhiyun {
1530*4882a593Smuzhiyun 	return nla_put(skb, attrtype, sizeof(*addr), addr);
1531*4882a593Smuzhiyun }
1532*4882a593Smuzhiyun 
1533*4882a593Smuzhiyun /**
1534*4882a593Smuzhiyun  * nla_put_bitfield32 - Add a bitfield32 netlink attribute to a socket buffer
1535*4882a593Smuzhiyun  * @skb: socket buffer to add attribute to
1536*4882a593Smuzhiyun  * @attrtype: attribute type
1537*4882a593Smuzhiyun  * @value: value carrying bits
1538*4882a593Smuzhiyun  * @selector: selector of valid bits
1539*4882a593Smuzhiyun  */
nla_put_bitfield32(struct sk_buff * skb,int attrtype,__u32 value,__u32 selector)1540*4882a593Smuzhiyun static inline int nla_put_bitfield32(struct sk_buff *skb, int attrtype,
1541*4882a593Smuzhiyun 				     __u32 value, __u32 selector)
1542*4882a593Smuzhiyun {
1543*4882a593Smuzhiyun 	struct nla_bitfield32 tmp = { value, selector, };
1544*4882a593Smuzhiyun 
1545*4882a593Smuzhiyun 	return nla_put(skb, attrtype, sizeof(tmp), &tmp);
1546*4882a593Smuzhiyun }
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun /**
1549*4882a593Smuzhiyun  * nla_get_u32 - return payload of u32 attribute
1550*4882a593Smuzhiyun  * @nla: u32 netlink attribute
1551*4882a593Smuzhiyun  */
nla_get_u32(const struct nlattr * nla)1552*4882a593Smuzhiyun static inline u32 nla_get_u32(const struct nlattr *nla)
1553*4882a593Smuzhiyun {
1554*4882a593Smuzhiyun 	return *(u32 *) nla_data(nla);
1555*4882a593Smuzhiyun }
1556*4882a593Smuzhiyun 
1557*4882a593Smuzhiyun /**
1558*4882a593Smuzhiyun  * nla_get_be32 - return payload of __be32 attribute
1559*4882a593Smuzhiyun  * @nla: __be32 netlink attribute
1560*4882a593Smuzhiyun  */
nla_get_be32(const struct nlattr * nla)1561*4882a593Smuzhiyun static inline __be32 nla_get_be32(const struct nlattr *nla)
1562*4882a593Smuzhiyun {
1563*4882a593Smuzhiyun 	return *(__be32 *) nla_data(nla);
1564*4882a593Smuzhiyun }
1565*4882a593Smuzhiyun 
1566*4882a593Smuzhiyun /**
1567*4882a593Smuzhiyun  * nla_get_le32 - return payload of __le32 attribute
1568*4882a593Smuzhiyun  * @nla: __le32 netlink attribute
1569*4882a593Smuzhiyun  */
nla_get_le32(const struct nlattr * nla)1570*4882a593Smuzhiyun static inline __le32 nla_get_le32(const struct nlattr *nla)
1571*4882a593Smuzhiyun {
1572*4882a593Smuzhiyun 	return *(__le32 *) nla_data(nla);
1573*4882a593Smuzhiyun }
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun /**
1576*4882a593Smuzhiyun  * nla_get_u16 - return payload of u16 attribute
1577*4882a593Smuzhiyun  * @nla: u16 netlink attribute
1578*4882a593Smuzhiyun  */
nla_get_u16(const struct nlattr * nla)1579*4882a593Smuzhiyun static inline u16 nla_get_u16(const struct nlattr *nla)
1580*4882a593Smuzhiyun {
1581*4882a593Smuzhiyun 	return *(u16 *) nla_data(nla);
1582*4882a593Smuzhiyun }
1583*4882a593Smuzhiyun 
1584*4882a593Smuzhiyun /**
1585*4882a593Smuzhiyun  * nla_get_be16 - return payload of __be16 attribute
1586*4882a593Smuzhiyun  * @nla: __be16 netlink attribute
1587*4882a593Smuzhiyun  */
nla_get_be16(const struct nlattr * nla)1588*4882a593Smuzhiyun static inline __be16 nla_get_be16(const struct nlattr *nla)
1589*4882a593Smuzhiyun {
1590*4882a593Smuzhiyun 	return *(__be16 *) nla_data(nla);
1591*4882a593Smuzhiyun }
1592*4882a593Smuzhiyun 
1593*4882a593Smuzhiyun /**
1594*4882a593Smuzhiyun  * nla_get_le16 - return payload of __le16 attribute
1595*4882a593Smuzhiyun  * @nla: __le16 netlink attribute
1596*4882a593Smuzhiyun  */
nla_get_le16(const struct nlattr * nla)1597*4882a593Smuzhiyun static inline __le16 nla_get_le16(const struct nlattr *nla)
1598*4882a593Smuzhiyun {
1599*4882a593Smuzhiyun 	return *(__le16 *) nla_data(nla);
1600*4882a593Smuzhiyun }
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun /**
1603*4882a593Smuzhiyun  * nla_get_u8 - return payload of u8 attribute
1604*4882a593Smuzhiyun  * @nla: u8 netlink attribute
1605*4882a593Smuzhiyun  */
nla_get_u8(const struct nlattr * nla)1606*4882a593Smuzhiyun static inline u8 nla_get_u8(const struct nlattr *nla)
1607*4882a593Smuzhiyun {
1608*4882a593Smuzhiyun 	return *(u8 *) nla_data(nla);
1609*4882a593Smuzhiyun }
1610*4882a593Smuzhiyun 
1611*4882a593Smuzhiyun /**
1612*4882a593Smuzhiyun  * nla_get_u64 - return payload of u64 attribute
1613*4882a593Smuzhiyun  * @nla: u64 netlink attribute
1614*4882a593Smuzhiyun  */
nla_get_u64(const struct nlattr * nla)1615*4882a593Smuzhiyun static inline u64 nla_get_u64(const struct nlattr *nla)
1616*4882a593Smuzhiyun {
1617*4882a593Smuzhiyun 	u64 tmp;
1618*4882a593Smuzhiyun 
1619*4882a593Smuzhiyun 	nla_memcpy(&tmp, nla, sizeof(tmp));
1620*4882a593Smuzhiyun 
1621*4882a593Smuzhiyun 	return tmp;
1622*4882a593Smuzhiyun }
1623*4882a593Smuzhiyun 
1624*4882a593Smuzhiyun /**
1625*4882a593Smuzhiyun  * nla_get_be64 - return payload of __be64 attribute
1626*4882a593Smuzhiyun  * @nla: __be64 netlink attribute
1627*4882a593Smuzhiyun  */
nla_get_be64(const struct nlattr * nla)1628*4882a593Smuzhiyun static inline __be64 nla_get_be64(const struct nlattr *nla)
1629*4882a593Smuzhiyun {
1630*4882a593Smuzhiyun 	__be64 tmp;
1631*4882a593Smuzhiyun 
1632*4882a593Smuzhiyun 	nla_memcpy(&tmp, nla, sizeof(tmp));
1633*4882a593Smuzhiyun 
1634*4882a593Smuzhiyun 	return tmp;
1635*4882a593Smuzhiyun }
1636*4882a593Smuzhiyun 
1637*4882a593Smuzhiyun /**
1638*4882a593Smuzhiyun  * nla_get_le64 - return payload of __le64 attribute
1639*4882a593Smuzhiyun  * @nla: __le64 netlink attribute
1640*4882a593Smuzhiyun  */
nla_get_le64(const struct nlattr * nla)1641*4882a593Smuzhiyun static inline __le64 nla_get_le64(const struct nlattr *nla)
1642*4882a593Smuzhiyun {
1643*4882a593Smuzhiyun 	return *(__le64 *) nla_data(nla);
1644*4882a593Smuzhiyun }
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun /**
1647*4882a593Smuzhiyun  * nla_get_s32 - return payload of s32 attribute
1648*4882a593Smuzhiyun  * @nla: s32 netlink attribute
1649*4882a593Smuzhiyun  */
nla_get_s32(const struct nlattr * nla)1650*4882a593Smuzhiyun static inline s32 nla_get_s32(const struct nlattr *nla)
1651*4882a593Smuzhiyun {
1652*4882a593Smuzhiyun 	return *(s32 *) nla_data(nla);
1653*4882a593Smuzhiyun }
1654*4882a593Smuzhiyun 
1655*4882a593Smuzhiyun /**
1656*4882a593Smuzhiyun  * nla_get_s16 - return payload of s16 attribute
1657*4882a593Smuzhiyun  * @nla: s16 netlink attribute
1658*4882a593Smuzhiyun  */
nla_get_s16(const struct nlattr * nla)1659*4882a593Smuzhiyun static inline s16 nla_get_s16(const struct nlattr *nla)
1660*4882a593Smuzhiyun {
1661*4882a593Smuzhiyun 	return *(s16 *) nla_data(nla);
1662*4882a593Smuzhiyun }
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun /**
1665*4882a593Smuzhiyun  * nla_get_s8 - return payload of s8 attribute
1666*4882a593Smuzhiyun  * @nla: s8 netlink attribute
1667*4882a593Smuzhiyun  */
nla_get_s8(const struct nlattr * nla)1668*4882a593Smuzhiyun static inline s8 nla_get_s8(const struct nlattr *nla)
1669*4882a593Smuzhiyun {
1670*4882a593Smuzhiyun 	return *(s8 *) nla_data(nla);
1671*4882a593Smuzhiyun }
1672*4882a593Smuzhiyun 
1673*4882a593Smuzhiyun /**
1674*4882a593Smuzhiyun  * nla_get_s64 - return payload of s64 attribute
1675*4882a593Smuzhiyun  * @nla: s64 netlink attribute
1676*4882a593Smuzhiyun  */
nla_get_s64(const struct nlattr * nla)1677*4882a593Smuzhiyun static inline s64 nla_get_s64(const struct nlattr *nla)
1678*4882a593Smuzhiyun {
1679*4882a593Smuzhiyun 	s64 tmp;
1680*4882a593Smuzhiyun 
1681*4882a593Smuzhiyun 	nla_memcpy(&tmp, nla, sizeof(tmp));
1682*4882a593Smuzhiyun 
1683*4882a593Smuzhiyun 	return tmp;
1684*4882a593Smuzhiyun }
1685*4882a593Smuzhiyun 
1686*4882a593Smuzhiyun /**
1687*4882a593Smuzhiyun  * nla_get_flag - return payload of flag attribute
1688*4882a593Smuzhiyun  * @nla: flag netlink attribute
1689*4882a593Smuzhiyun  */
nla_get_flag(const struct nlattr * nla)1690*4882a593Smuzhiyun static inline int nla_get_flag(const struct nlattr *nla)
1691*4882a593Smuzhiyun {
1692*4882a593Smuzhiyun 	return !!nla;
1693*4882a593Smuzhiyun }
1694*4882a593Smuzhiyun 
1695*4882a593Smuzhiyun /**
1696*4882a593Smuzhiyun  * nla_get_msecs - return payload of msecs attribute
1697*4882a593Smuzhiyun  * @nla: msecs netlink attribute
1698*4882a593Smuzhiyun  *
1699*4882a593Smuzhiyun  * Returns the number of milliseconds in jiffies.
1700*4882a593Smuzhiyun  */
nla_get_msecs(const struct nlattr * nla)1701*4882a593Smuzhiyun static inline unsigned long nla_get_msecs(const struct nlattr *nla)
1702*4882a593Smuzhiyun {
1703*4882a593Smuzhiyun 	u64 msecs = nla_get_u64(nla);
1704*4882a593Smuzhiyun 
1705*4882a593Smuzhiyun 	return msecs_to_jiffies((unsigned long) msecs);
1706*4882a593Smuzhiyun }
1707*4882a593Smuzhiyun 
1708*4882a593Smuzhiyun /**
1709*4882a593Smuzhiyun  * nla_get_in_addr - return payload of IPv4 address attribute
1710*4882a593Smuzhiyun  * @nla: IPv4 address netlink attribute
1711*4882a593Smuzhiyun  */
nla_get_in_addr(const struct nlattr * nla)1712*4882a593Smuzhiyun static inline __be32 nla_get_in_addr(const struct nlattr *nla)
1713*4882a593Smuzhiyun {
1714*4882a593Smuzhiyun 	return *(__be32 *) nla_data(nla);
1715*4882a593Smuzhiyun }
1716*4882a593Smuzhiyun 
1717*4882a593Smuzhiyun /**
1718*4882a593Smuzhiyun  * nla_get_in6_addr - return payload of IPv6 address attribute
1719*4882a593Smuzhiyun  * @nla: IPv6 address netlink attribute
1720*4882a593Smuzhiyun  */
nla_get_in6_addr(const struct nlattr * nla)1721*4882a593Smuzhiyun static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla)
1722*4882a593Smuzhiyun {
1723*4882a593Smuzhiyun 	struct in6_addr tmp;
1724*4882a593Smuzhiyun 
1725*4882a593Smuzhiyun 	nla_memcpy(&tmp, nla, sizeof(tmp));
1726*4882a593Smuzhiyun 	return tmp;
1727*4882a593Smuzhiyun }
1728*4882a593Smuzhiyun 
1729*4882a593Smuzhiyun /**
1730*4882a593Smuzhiyun  * nla_get_bitfield32 - return payload of 32 bitfield attribute
1731*4882a593Smuzhiyun  * @nla: nla_bitfield32 attribute
1732*4882a593Smuzhiyun  */
nla_get_bitfield32(const struct nlattr * nla)1733*4882a593Smuzhiyun static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla)
1734*4882a593Smuzhiyun {
1735*4882a593Smuzhiyun 	struct nla_bitfield32 tmp;
1736*4882a593Smuzhiyun 
1737*4882a593Smuzhiyun 	nla_memcpy(&tmp, nla, sizeof(tmp));
1738*4882a593Smuzhiyun 	return tmp;
1739*4882a593Smuzhiyun }
1740*4882a593Smuzhiyun 
1741*4882a593Smuzhiyun /**
1742*4882a593Smuzhiyun  * nla_memdup - duplicate attribute memory (kmemdup)
1743*4882a593Smuzhiyun  * @src: netlink attribute to duplicate from
1744*4882a593Smuzhiyun  * @gfp: GFP mask
1745*4882a593Smuzhiyun  */
nla_memdup(const struct nlattr * src,gfp_t gfp)1746*4882a593Smuzhiyun static inline void *nla_memdup(const struct nlattr *src, gfp_t gfp)
1747*4882a593Smuzhiyun {
1748*4882a593Smuzhiyun 	return kmemdup(nla_data(src), nla_len(src), gfp);
1749*4882a593Smuzhiyun }
1750*4882a593Smuzhiyun 
1751*4882a593Smuzhiyun /**
1752*4882a593Smuzhiyun  * nla_nest_start_noflag - Start a new level of nested attributes
1753*4882a593Smuzhiyun  * @skb: socket buffer to add attributes to
1754*4882a593Smuzhiyun  * @attrtype: attribute type of container
1755*4882a593Smuzhiyun  *
1756*4882a593Smuzhiyun  * This function exists for backward compatibility to use in APIs which never
1757*4882a593Smuzhiyun  * marked their nest attributes with NLA_F_NESTED flag. New APIs should use
1758*4882a593Smuzhiyun  * nla_nest_start() which sets the flag.
1759*4882a593Smuzhiyun  *
1760*4882a593Smuzhiyun  * Returns the container attribute or NULL on error
1761*4882a593Smuzhiyun  */
nla_nest_start_noflag(struct sk_buff * skb,int attrtype)1762*4882a593Smuzhiyun static inline struct nlattr *nla_nest_start_noflag(struct sk_buff *skb,
1763*4882a593Smuzhiyun 						   int attrtype)
1764*4882a593Smuzhiyun {
1765*4882a593Smuzhiyun 	struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
1766*4882a593Smuzhiyun 
1767*4882a593Smuzhiyun 	if (nla_put(skb, attrtype, 0, NULL) < 0)
1768*4882a593Smuzhiyun 		return NULL;
1769*4882a593Smuzhiyun 
1770*4882a593Smuzhiyun 	return start;
1771*4882a593Smuzhiyun }
1772*4882a593Smuzhiyun 
1773*4882a593Smuzhiyun /**
1774*4882a593Smuzhiyun  * nla_nest_start - Start a new level of nested attributes, with NLA_F_NESTED
1775*4882a593Smuzhiyun  * @skb: socket buffer to add attributes to
1776*4882a593Smuzhiyun  * @attrtype: attribute type of container
1777*4882a593Smuzhiyun  *
1778*4882a593Smuzhiyun  * Unlike nla_nest_start_noflag(), mark the nest attribute with NLA_F_NESTED
1779*4882a593Smuzhiyun  * flag. This is the preferred function to use in new code.
1780*4882a593Smuzhiyun  *
1781*4882a593Smuzhiyun  * Returns the container attribute or NULL on error
1782*4882a593Smuzhiyun  */
nla_nest_start(struct sk_buff * skb,int attrtype)1783*4882a593Smuzhiyun static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
1784*4882a593Smuzhiyun {
1785*4882a593Smuzhiyun 	return nla_nest_start_noflag(skb, attrtype | NLA_F_NESTED);
1786*4882a593Smuzhiyun }
1787*4882a593Smuzhiyun 
1788*4882a593Smuzhiyun /**
1789*4882a593Smuzhiyun  * nla_nest_end - Finalize nesting of attributes
1790*4882a593Smuzhiyun  * @skb: socket buffer the attributes are stored in
1791*4882a593Smuzhiyun  * @start: container attribute
1792*4882a593Smuzhiyun  *
1793*4882a593Smuzhiyun  * Corrects the container attribute header to include the all
1794*4882a593Smuzhiyun  * appeneded attributes.
1795*4882a593Smuzhiyun  *
1796*4882a593Smuzhiyun  * Returns the total data length of the skb.
1797*4882a593Smuzhiyun  */
nla_nest_end(struct sk_buff * skb,struct nlattr * start)1798*4882a593Smuzhiyun static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
1799*4882a593Smuzhiyun {
1800*4882a593Smuzhiyun 	start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
1801*4882a593Smuzhiyun 	return skb->len;
1802*4882a593Smuzhiyun }
1803*4882a593Smuzhiyun 
1804*4882a593Smuzhiyun /**
1805*4882a593Smuzhiyun  * nla_nest_cancel - Cancel nesting of attributes
1806*4882a593Smuzhiyun  * @skb: socket buffer the message is stored in
1807*4882a593Smuzhiyun  * @start: container attribute
1808*4882a593Smuzhiyun  *
1809*4882a593Smuzhiyun  * Removes the container attribute and including all nested
1810*4882a593Smuzhiyun  * attributes. Returns -EMSGSIZE
1811*4882a593Smuzhiyun  */
nla_nest_cancel(struct sk_buff * skb,struct nlattr * start)1812*4882a593Smuzhiyun static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
1813*4882a593Smuzhiyun {
1814*4882a593Smuzhiyun 	nlmsg_trim(skb, start);
1815*4882a593Smuzhiyun }
1816*4882a593Smuzhiyun 
1817*4882a593Smuzhiyun /**
1818*4882a593Smuzhiyun  * __nla_validate_nested - Validate a stream of nested attributes
1819*4882a593Smuzhiyun  * @start: container attribute
1820*4882a593Smuzhiyun  * @maxtype: maximum attribute type to be expected
1821*4882a593Smuzhiyun  * @policy: validation policy
1822*4882a593Smuzhiyun  * @validate: validation strictness
1823*4882a593Smuzhiyun  * @extack: extended ACK report struct
1824*4882a593Smuzhiyun  *
1825*4882a593Smuzhiyun  * Validates all attributes in the nested attribute stream against the
1826*4882a593Smuzhiyun  * specified policy. Attributes with a type exceeding maxtype will be
1827*4882a593Smuzhiyun  * ignored. See documenation of struct nla_policy for more details.
1828*4882a593Smuzhiyun  *
1829*4882a593Smuzhiyun  * Returns 0 on success or a negative error code.
1830*4882a593Smuzhiyun  */
__nla_validate_nested(const struct nlattr * start,int maxtype,const struct nla_policy * policy,unsigned int validate,struct netlink_ext_ack * extack)1831*4882a593Smuzhiyun static inline int __nla_validate_nested(const struct nlattr *start, int maxtype,
1832*4882a593Smuzhiyun 					const struct nla_policy *policy,
1833*4882a593Smuzhiyun 					unsigned int validate,
1834*4882a593Smuzhiyun 					struct netlink_ext_ack *extack)
1835*4882a593Smuzhiyun {
1836*4882a593Smuzhiyun 	return __nla_validate(nla_data(start), nla_len(start), maxtype, policy,
1837*4882a593Smuzhiyun 			      validate, extack);
1838*4882a593Smuzhiyun }
1839*4882a593Smuzhiyun 
1840*4882a593Smuzhiyun static inline int
nla_validate_nested(const struct nlattr * start,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)1841*4882a593Smuzhiyun nla_validate_nested(const struct nlattr *start, int maxtype,
1842*4882a593Smuzhiyun 		    const struct nla_policy *policy,
1843*4882a593Smuzhiyun 		    struct netlink_ext_ack *extack)
1844*4882a593Smuzhiyun {
1845*4882a593Smuzhiyun 	return __nla_validate_nested(start, maxtype, policy,
1846*4882a593Smuzhiyun 				     NL_VALIDATE_STRICT, extack);
1847*4882a593Smuzhiyun }
1848*4882a593Smuzhiyun 
1849*4882a593Smuzhiyun static inline int
nla_validate_nested_deprecated(const struct nlattr * start,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)1850*4882a593Smuzhiyun nla_validate_nested_deprecated(const struct nlattr *start, int maxtype,
1851*4882a593Smuzhiyun 			       const struct nla_policy *policy,
1852*4882a593Smuzhiyun 			       struct netlink_ext_ack *extack)
1853*4882a593Smuzhiyun {
1854*4882a593Smuzhiyun 	return __nla_validate_nested(start, maxtype, policy,
1855*4882a593Smuzhiyun 				     NL_VALIDATE_LIBERAL, extack);
1856*4882a593Smuzhiyun }
1857*4882a593Smuzhiyun 
1858*4882a593Smuzhiyun /**
1859*4882a593Smuzhiyun  * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute
1860*4882a593Smuzhiyun  * @skb: socket buffer the message is stored in
1861*4882a593Smuzhiyun  *
1862*4882a593Smuzhiyun  * Return true if padding is needed to align the next attribute (nla_data()) to
1863*4882a593Smuzhiyun  * a 64-bit aligned area.
1864*4882a593Smuzhiyun  */
nla_need_padding_for_64bit(struct sk_buff * skb)1865*4882a593Smuzhiyun static inline bool nla_need_padding_for_64bit(struct sk_buff *skb)
1866*4882a593Smuzhiyun {
1867*4882a593Smuzhiyun #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1868*4882a593Smuzhiyun 	/* The nlattr header is 4 bytes in size, that's why we test
1869*4882a593Smuzhiyun 	 * if the skb->data _is_ aligned.  A NOP attribute, plus
1870*4882a593Smuzhiyun 	 * nlattr header for next attribute, will make nla_data()
1871*4882a593Smuzhiyun 	 * 8-byte aligned.
1872*4882a593Smuzhiyun 	 */
1873*4882a593Smuzhiyun 	if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
1874*4882a593Smuzhiyun 		return true;
1875*4882a593Smuzhiyun #endif
1876*4882a593Smuzhiyun 	return false;
1877*4882a593Smuzhiyun }
1878*4882a593Smuzhiyun 
1879*4882a593Smuzhiyun /**
1880*4882a593Smuzhiyun  * nla_align_64bit - 64-bit align the nla_data() of next attribute
1881*4882a593Smuzhiyun  * @skb: socket buffer the message is stored in
1882*4882a593Smuzhiyun  * @padattr: attribute type for the padding
1883*4882a593Smuzhiyun  *
1884*4882a593Smuzhiyun  * Conditionally emit a padding netlink attribute in order to make
1885*4882a593Smuzhiyun  * the next attribute we emit have a 64-bit aligned nla_data() area.
1886*4882a593Smuzhiyun  * This will only be done in architectures which do not have
1887*4882a593Smuzhiyun  * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
1888*4882a593Smuzhiyun  *
1889*4882a593Smuzhiyun  * Returns zero on success or a negative error code.
1890*4882a593Smuzhiyun  */
nla_align_64bit(struct sk_buff * skb,int padattr)1891*4882a593Smuzhiyun static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
1892*4882a593Smuzhiyun {
1893*4882a593Smuzhiyun 	if (nla_need_padding_for_64bit(skb) &&
1894*4882a593Smuzhiyun 	    !nla_reserve(skb, padattr, 0))
1895*4882a593Smuzhiyun 		return -EMSGSIZE;
1896*4882a593Smuzhiyun 
1897*4882a593Smuzhiyun 	return 0;
1898*4882a593Smuzhiyun }
1899*4882a593Smuzhiyun 
1900*4882a593Smuzhiyun /**
1901*4882a593Smuzhiyun  * nla_total_size_64bit - total length of attribute including padding
1902*4882a593Smuzhiyun  * @payload: length of payload
1903*4882a593Smuzhiyun  */
nla_total_size_64bit(int payload)1904*4882a593Smuzhiyun static inline int nla_total_size_64bit(int payload)
1905*4882a593Smuzhiyun {
1906*4882a593Smuzhiyun 	return NLA_ALIGN(nla_attr_size(payload))
1907*4882a593Smuzhiyun #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1908*4882a593Smuzhiyun 		+ NLA_ALIGN(nla_attr_size(0))
1909*4882a593Smuzhiyun #endif
1910*4882a593Smuzhiyun 		;
1911*4882a593Smuzhiyun }
1912*4882a593Smuzhiyun 
1913*4882a593Smuzhiyun /**
1914*4882a593Smuzhiyun  * nla_for_each_attr - iterate over a stream of attributes
1915*4882a593Smuzhiyun  * @pos: loop counter, set to current attribute
1916*4882a593Smuzhiyun  * @head: head of attribute stream
1917*4882a593Smuzhiyun  * @len: length of attribute stream
1918*4882a593Smuzhiyun  * @rem: initialized to len, holds bytes currently remaining in stream
1919*4882a593Smuzhiyun  */
1920*4882a593Smuzhiyun #define nla_for_each_attr(pos, head, len, rem) \
1921*4882a593Smuzhiyun 	for (pos = head, rem = len; \
1922*4882a593Smuzhiyun 	     nla_ok(pos, rem); \
1923*4882a593Smuzhiyun 	     pos = nla_next(pos, &(rem)))
1924*4882a593Smuzhiyun 
1925*4882a593Smuzhiyun /**
1926*4882a593Smuzhiyun  * nla_for_each_nested - iterate over nested attributes
1927*4882a593Smuzhiyun  * @pos: loop counter, set to current attribute
1928*4882a593Smuzhiyun  * @nla: attribute containing the nested attributes
1929*4882a593Smuzhiyun  * @rem: initialized to len, holds bytes currently remaining in stream
1930*4882a593Smuzhiyun  */
1931*4882a593Smuzhiyun #define nla_for_each_nested(pos, nla, rem) \
1932*4882a593Smuzhiyun 	nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
1933*4882a593Smuzhiyun 
1934*4882a593Smuzhiyun /**
1935*4882a593Smuzhiyun  * nla_is_last - Test if attribute is last in stream
1936*4882a593Smuzhiyun  * @nla: attribute to test
1937*4882a593Smuzhiyun  * @rem: bytes remaining in stream
1938*4882a593Smuzhiyun  */
nla_is_last(const struct nlattr * nla,int rem)1939*4882a593Smuzhiyun static inline bool nla_is_last(const struct nlattr *nla, int rem)
1940*4882a593Smuzhiyun {
1941*4882a593Smuzhiyun 	return nla->nla_len == rem;
1942*4882a593Smuzhiyun }
1943*4882a593Smuzhiyun 
1944*4882a593Smuzhiyun void nla_get_range_unsigned(const struct nla_policy *pt,
1945*4882a593Smuzhiyun 			    struct netlink_range_validation *range);
1946*4882a593Smuzhiyun void nla_get_range_signed(const struct nla_policy *pt,
1947*4882a593Smuzhiyun 			  struct netlink_range_validation_signed *range);
1948*4882a593Smuzhiyun 
1949*4882a593Smuzhiyun struct netlink_policy_dump_state;
1950*4882a593Smuzhiyun 
1951*4882a593Smuzhiyun int netlink_policy_dump_add_policy(struct netlink_policy_dump_state **pstate,
1952*4882a593Smuzhiyun 				   const struct nla_policy *policy,
1953*4882a593Smuzhiyun 				   unsigned int maxtype);
1954*4882a593Smuzhiyun int netlink_policy_dump_get_policy_idx(struct netlink_policy_dump_state *state,
1955*4882a593Smuzhiyun 				       const struct nla_policy *policy,
1956*4882a593Smuzhiyun 				       unsigned int maxtype);
1957*4882a593Smuzhiyun bool netlink_policy_dump_loop(struct netlink_policy_dump_state *state);
1958*4882a593Smuzhiyun int netlink_policy_dump_write(struct sk_buff *skb,
1959*4882a593Smuzhiyun 			      struct netlink_policy_dump_state *state);
1960*4882a593Smuzhiyun int netlink_policy_dump_attr_size_estimate(const struct nla_policy *pt);
1961*4882a593Smuzhiyun int netlink_policy_dump_write_attr(struct sk_buff *skb,
1962*4882a593Smuzhiyun 				   const struct nla_policy *pt,
1963*4882a593Smuzhiyun 				   int nestattr);
1964*4882a593Smuzhiyun void netlink_policy_dump_free(struct netlink_policy_dump_state *state);
1965*4882a593Smuzhiyun 
1966*4882a593Smuzhiyun #endif
1967