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