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