xref: /linux/include/net/netlink.h (revision 1a9239bb4253f9076b5b4b2a1a4e8d7defd77a95)
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  */
nlmsg_msg_size(int payload)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  */
nlmsg_total_size(int payload)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  */
nlmsg_padlen(int payload)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  */
nlmsg_data(const struct nlmsghdr * nlh)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  */
nlmsg_len(const struct nlmsghdr * nlh)609 static inline int nlmsg_len(const struct nlmsghdr *nlh)
610 {
611 	return nlh->nlmsg_len - NLMSG_HDRLEN;
612 }
613 
614 /**
615  * nlmsg_attrdata - head of attributes data
616  * @nlh: netlink message header
617  * @hdrlen: length of family specific header
618  */
nlmsg_attrdata(const struct nlmsghdr * nlh,int hdrlen)619 static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
620 					    int hdrlen)
621 {
622 	unsigned char *data = nlmsg_data(nlh);
623 	return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
624 }
625 
626 /**
627  * nlmsg_attrlen - length of attributes data
628  * @nlh: netlink message header
629  * @hdrlen: length of family specific header
630  */
nlmsg_attrlen(const struct nlmsghdr * nlh,int hdrlen)631 static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
632 {
633 	return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
634 }
635 
636 /**
637  * nlmsg_ok - check if the netlink message fits into the remaining bytes
638  * @nlh: netlink message header
639  * @remaining: number of bytes remaining in message stream
640  */
nlmsg_ok(const struct nlmsghdr * nlh,int remaining)641 static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
642 {
643 	return (remaining >= (int) sizeof(struct nlmsghdr) &&
644 		nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
645 		nlh->nlmsg_len <= remaining);
646 }
647 
648 /**
649  * nlmsg_next - next netlink message in message stream
650  * @nlh: netlink message header
651  * @remaining: number of bytes remaining in message stream
652  *
653  * Returns: the next netlink message in the message stream and
654  * decrements remaining by the size of the current message.
655  */
656 static inline struct nlmsghdr *
nlmsg_next(const struct nlmsghdr * nlh,int * remaining)657 nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
658 {
659 	int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
660 
661 	*remaining -= totlen;
662 
663 	return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
664 }
665 
666 /**
667  * nla_parse - Parse a stream of attributes into a tb buffer
668  * @tb: destination array with maxtype+1 elements
669  * @maxtype: maximum attribute type to be expected
670  * @head: head of attribute stream
671  * @len: length of attribute stream
672  * @policy: validation policy
673  * @extack: extended ACK pointer
674  *
675  * Parses a stream of attributes and stores a pointer to each attribute in
676  * the tb array accessible via the attribute type. Attributes with a type
677  * exceeding maxtype will be rejected, policy must be specified, attributes
678  * will be validated in the strictest way possible.
679  *
680  * Returns: 0 on success or a negative error code.
681  */
nla_parse(struct nlattr ** tb,int maxtype,const struct nlattr * head,int len,const struct nla_policy * policy,struct netlink_ext_ack * extack)682 static inline int nla_parse(struct nlattr **tb, int maxtype,
683 			    const struct nlattr *head, int len,
684 			    const struct nla_policy *policy,
685 			    struct netlink_ext_ack *extack)
686 {
687 	return __nla_parse(tb, maxtype, head, len, policy,
688 			   NL_VALIDATE_STRICT, extack);
689 }
690 
691 /**
692  * nla_parse_deprecated - Parse a stream of attributes into a tb buffer
693  * @tb: destination array with maxtype+1 elements
694  * @maxtype: maximum attribute type to be expected
695  * @head: head of attribute stream
696  * @len: length of attribute stream
697  * @policy: validation policy
698  * @extack: extended ACK pointer
699  *
700  * Parses a stream of attributes and stores a pointer to each attribute in
701  * the tb array accessible via the attribute type. Attributes with a type
702  * exceeding maxtype will be ignored and attributes from the policy are not
703  * always strictly validated (only for new attributes).
704  *
705  * Returns: 0 on success or a negative error code.
706  */
nla_parse_deprecated(struct nlattr ** tb,int maxtype,const struct nlattr * head,int len,const struct nla_policy * policy,struct netlink_ext_ack * extack)707 static inline int nla_parse_deprecated(struct nlattr **tb, int maxtype,
708 				       const struct nlattr *head, int len,
709 				       const struct nla_policy *policy,
710 				       struct netlink_ext_ack *extack)
711 {
712 	return __nla_parse(tb, maxtype, head, len, policy,
713 			   NL_VALIDATE_LIBERAL, extack);
714 }
715 
716 /**
717  * nla_parse_deprecated_strict - Parse a stream of attributes into a tb buffer
718  * @tb: destination array with maxtype+1 elements
719  * @maxtype: maximum attribute type to be expected
720  * @head: head of attribute stream
721  * @len: length of attribute stream
722  * @policy: validation policy
723  * @extack: extended ACK pointer
724  *
725  * Parses a stream of attributes and stores a pointer to each attribute in
726  * the tb array accessible via the attribute type. Attributes with a type
727  * exceeding maxtype will be rejected as well as trailing data, but the
728  * policy is not completely strictly validated (only for new attributes).
729  *
730  * Returns: 0 on success or a negative error code.
731  */
nla_parse_deprecated_strict(struct nlattr ** tb,int maxtype,const struct nlattr * head,int len,const struct nla_policy * policy,struct netlink_ext_ack * extack)732 static inline int nla_parse_deprecated_strict(struct nlattr **tb, int maxtype,
733 					      const struct nlattr *head,
734 					      int len,
735 					      const struct nla_policy *policy,
736 					      struct netlink_ext_ack *extack)
737 {
738 	return __nla_parse(tb, maxtype, head, len, policy,
739 			   NL_VALIDATE_DEPRECATED_STRICT, extack);
740 }
741 
742 /**
743  * __nlmsg_parse - parse attributes of a netlink message
744  * @nlh: netlink message header
745  * @hdrlen: length of family specific header
746  * @tb: destination array with maxtype+1 elements
747  * @maxtype: maximum attribute type to be expected
748  * @policy: validation policy
749  * @validate: validation strictness
750  * @extack: extended ACK report struct
751  *
752  * See nla_parse()
753  */
__nlmsg_parse(const struct nlmsghdr * nlh,int hdrlen,struct nlattr * tb[],int maxtype,const struct nla_policy * policy,unsigned int validate,struct netlink_ext_ack * extack)754 static inline int __nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
755 				struct nlattr *tb[], int maxtype,
756 				const struct nla_policy *policy,
757 				unsigned int validate,
758 				struct netlink_ext_ack *extack)
759 {
760 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) {
761 		NL_SET_ERR_MSG(extack, "Invalid header length");
762 		return -EINVAL;
763 	}
764 
765 	return __nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
766 			   nlmsg_attrlen(nlh, hdrlen), policy, validate,
767 			   extack);
768 }
769 
770 /**
771  * nlmsg_parse - parse attributes of a netlink message
772  * @nlh: netlink message header
773  * @hdrlen: length of family specific header
774  * @tb: destination array with maxtype+1 elements
775  * @maxtype: maximum attribute type to be expected
776  * @policy: validation policy
777  * @extack: extended ACK report struct
778  *
779  * See nla_parse()
780  */
nlmsg_parse(const struct nlmsghdr * nlh,int hdrlen,struct nlattr * tb[],int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)781 static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
782 			      struct nlattr *tb[], int maxtype,
783 			      const struct nla_policy *policy,
784 			      struct netlink_ext_ack *extack)
785 {
786 	return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
787 			     NL_VALIDATE_STRICT, extack);
788 }
789 
790 /**
791  * nlmsg_parse_deprecated - parse attributes of a netlink message
792  * @nlh: netlink message header
793  * @hdrlen: length of family specific header
794  * @tb: destination array with maxtype+1 elements
795  * @maxtype: maximum attribute type to be expected
796  * @policy: validation policy
797  * @extack: extended ACK report struct
798  *
799  * See nla_parse_deprecated()
800  */
nlmsg_parse_deprecated(const struct nlmsghdr * nlh,int hdrlen,struct nlattr * tb[],int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)801 static inline int nlmsg_parse_deprecated(const struct nlmsghdr *nlh, int hdrlen,
802 					 struct nlattr *tb[], int maxtype,
803 					 const struct nla_policy *policy,
804 					 struct netlink_ext_ack *extack)
805 {
806 	return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
807 			     NL_VALIDATE_LIBERAL, extack);
808 }
809 
810 /**
811  * nlmsg_parse_deprecated_strict - parse attributes of a netlink message
812  * @nlh: netlink message header
813  * @hdrlen: length of family specific header
814  * @tb: destination array with maxtype+1 elements
815  * @maxtype: maximum attribute type to be expected
816  * @policy: validation policy
817  * @extack: extended ACK report struct
818  *
819  * See nla_parse_deprecated_strict()
820  */
821 static inline int
nlmsg_parse_deprecated_strict(const struct nlmsghdr * nlh,int hdrlen,struct nlattr * tb[],int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)822 nlmsg_parse_deprecated_strict(const struct nlmsghdr *nlh, int hdrlen,
823 			      struct nlattr *tb[], int maxtype,
824 			      const struct nla_policy *policy,
825 			      struct netlink_ext_ack *extack)
826 {
827 	return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
828 			     NL_VALIDATE_DEPRECATED_STRICT, extack);
829 }
830 
831 /**
832  * nlmsg_find_attr - find a specific attribute in a netlink message
833  * @nlh: netlink message header
834  * @hdrlen: length of family specific header
835  * @attrtype: type of attribute to look for
836  *
837  * Returns: the first attribute which matches the specified type.
838  */
nlmsg_find_attr(const struct nlmsghdr * nlh,int hdrlen,int attrtype)839 static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
840 					     int hdrlen, int attrtype)
841 {
842 	return nla_find(nlmsg_attrdata(nlh, hdrlen),
843 			nlmsg_attrlen(nlh, hdrlen), attrtype);
844 }
845 
846 /**
847  * nla_validate_deprecated - Validate a stream of attributes
848  * @head: head of attribute stream
849  * @len: length of attribute stream
850  * @maxtype: maximum attribute type to be expected
851  * @policy: validation policy
852  * @extack: extended ACK report struct
853  *
854  * Validates all attributes in the specified attribute stream against the
855  * specified policy. Validation is done in liberal mode.
856  * See documentation of struct nla_policy for more details.
857  *
858  * Returns: 0 on success or a negative error code.
859  */
nla_validate_deprecated(const struct nlattr * head,int len,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)860 static inline int nla_validate_deprecated(const struct nlattr *head, int len,
861 					  int maxtype,
862 					  const struct nla_policy *policy,
863 					  struct netlink_ext_ack *extack)
864 {
865 	return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_LIBERAL,
866 			      extack);
867 }
868 
869 /**
870  * nla_validate - Validate a stream of attributes
871  * @head: head of attribute stream
872  * @len: length of attribute stream
873  * @maxtype: maximum attribute type to be expected
874  * @policy: validation policy
875  * @extack: extended ACK report struct
876  *
877  * Validates all attributes in the specified attribute stream against the
878  * specified policy. Validation is done in strict mode.
879  * See documentation of struct nla_policy for more details.
880  *
881  * Returns: 0 on success or a negative error code.
882  */
nla_validate(const struct nlattr * head,int len,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)883 static inline int nla_validate(const struct nlattr *head, int len, int maxtype,
884 			       const struct nla_policy *policy,
885 			       struct netlink_ext_ack *extack)
886 {
887 	return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_STRICT,
888 			      extack);
889 }
890 
891 /**
892  * nlmsg_validate_deprecated - validate a netlink message including attributes
893  * @nlh: netlinket message header
894  * @hdrlen: length of family specific header
895  * @maxtype: maximum attribute type to be expected
896  * @policy: validation policy
897  * @extack: extended ACK report struct
898  */
nlmsg_validate_deprecated(const struct nlmsghdr * nlh,int hdrlen,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)899 static inline int nlmsg_validate_deprecated(const struct nlmsghdr *nlh,
900 					    int hdrlen, int maxtype,
901 					    const struct nla_policy *policy,
902 					    struct netlink_ext_ack *extack)
903 {
904 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
905 		return -EINVAL;
906 
907 	return __nla_validate(nlmsg_attrdata(nlh, hdrlen),
908 			      nlmsg_attrlen(nlh, hdrlen), maxtype,
909 			      policy, NL_VALIDATE_LIBERAL, extack);
910 }
911 
912 
913 
914 /**
915  * nlmsg_report - need to report back to application?
916  * @nlh: netlink message header
917  *
918  * Returns: 1 if a report back to the application is requested.
919  */
nlmsg_report(const struct nlmsghdr * nlh)920 static inline int nlmsg_report(const struct nlmsghdr *nlh)
921 {
922 	return nlh ? !!(nlh->nlmsg_flags & NLM_F_ECHO) : 0;
923 }
924 
925 /**
926  * nlmsg_seq - return the seq number of netlink message
927  * @nlh: netlink message header
928  *
929  * Returns: 0 if netlink message is NULL
930  */
nlmsg_seq(const struct nlmsghdr * nlh)931 static inline u32 nlmsg_seq(const struct nlmsghdr *nlh)
932 {
933 	return nlh ? nlh->nlmsg_seq : 0;
934 }
935 
936 /**
937  * nlmsg_for_each_attr - iterate over a stream of attributes
938  * @pos: loop counter, set to current attribute
939  * @nlh: netlink message header
940  * @hdrlen: length of family specific header
941  * @rem: initialized to len, holds bytes currently remaining in stream
942  */
943 #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
944 	nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
945 			  nlmsg_attrlen(nlh, hdrlen), rem)
946 
947 /**
948  * nlmsg_put - Add a new netlink message to an skb
949  * @skb: socket buffer to store message in
950  * @portid: netlink PORTID of requesting application
951  * @seq: sequence number of message
952  * @type: message type
953  * @payload: length of message payload
954  * @flags: message flags
955  *
956  * Returns: NULL if the tailroom of the skb is insufficient to store
957  * the message header and payload.
958  */
nlmsg_put(struct sk_buff * skb,u32 portid,u32 seq,int type,int payload,int flags)959 static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
960 					 int type, int payload, int flags)
961 {
962 	if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
963 		return NULL;
964 
965 	return __nlmsg_put(skb, portid, seq, type, payload, flags);
966 }
967 
968 /**
969  * nlmsg_append - Add more data to a nlmsg in a skb
970  * @skb: socket buffer to store message in
971  * @size: length of message payload
972  *
973  * Append data to an existing nlmsg, used when constructing a message
974  * with multiple fixed-format headers (which is rare).
975  * Returns: NULL if the tailroom of the skb is insufficient to store
976  * the extra payload.
977  */
nlmsg_append(struct sk_buff * skb,u32 size)978 static inline void *nlmsg_append(struct sk_buff *skb, u32 size)
979 {
980 	if (unlikely(skb_tailroom(skb) < NLMSG_ALIGN(size)))
981 		return NULL;
982 
983 	if (NLMSG_ALIGN(size) - size)
984 		memset(skb_tail_pointer(skb) + size, 0,
985 		       NLMSG_ALIGN(size) - size);
986 	return __skb_put(skb, NLMSG_ALIGN(size));
987 }
988 
989 /**
990  * nlmsg_put_answer - Add a new callback based netlink message to an skb
991  * @skb: socket buffer to store message in
992  * @cb: netlink callback
993  * @type: message type
994  * @payload: length of message payload
995  * @flags: message flags
996  *
997  * Returns: NULL if the tailroom of the skb is insufficient to store
998  * the message header and payload.
999  */
nlmsg_put_answer(struct sk_buff * skb,struct netlink_callback * cb,int type,int payload,int flags)1000 static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
1001 						struct netlink_callback *cb,
1002 						int type, int payload,
1003 						int flags)
1004 {
1005 	return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1006 			 type, payload, flags);
1007 }
1008 
1009 /**
1010  * nlmsg_new - Allocate a new netlink message
1011  * @payload: size of the message payload
1012  * @flags: the type of memory to allocate.
1013  *
1014  * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
1015  * and a good default is needed.
1016  */
nlmsg_new(size_t payload,gfp_t flags)1017 static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
1018 {
1019 	return alloc_skb(nlmsg_total_size(payload), flags);
1020 }
1021 
1022 /**
1023  * nlmsg_new_large - Allocate a new netlink message with non-contiguous
1024  * physical memory
1025  * @payload: size of the message payload
1026  *
1027  * The allocated skb is unable to have frag page for shinfo->frags*,
1028  * as the NULL setting for skb->head in netlink_skb_destructor() will
1029  * bypass most of the handling in skb_release_data()
1030  */
nlmsg_new_large(size_t payload)1031 static inline struct sk_buff *nlmsg_new_large(size_t payload)
1032 {
1033 	return netlink_alloc_large_skb(nlmsg_total_size(payload), 0);
1034 }
1035 
1036 /**
1037  * nlmsg_end - Finalize a netlink message
1038  * @skb: socket buffer the message is stored in
1039  * @nlh: netlink message header
1040  *
1041  * Corrects the netlink message header to include the appended
1042  * attributes. Only necessary if attributes have been added to
1043  * the message.
1044  */
nlmsg_end(struct sk_buff * skb,struct nlmsghdr * nlh)1045 static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
1046 {
1047 	nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
1048 }
1049 
1050 /**
1051  * nlmsg_get_pos - return current position in netlink message
1052  * @skb: socket buffer the message is stored in
1053  *
1054  * Returns: a pointer to the current tail of the message.
1055  */
nlmsg_get_pos(struct sk_buff * skb)1056 static inline void *nlmsg_get_pos(struct sk_buff *skb)
1057 {
1058 	return skb_tail_pointer(skb);
1059 }
1060 
1061 /**
1062  * nlmsg_trim - Trim message to a mark
1063  * @skb: socket buffer the message is stored in
1064  * @mark: mark to trim to
1065  *
1066  * Trims the message to the provided mark.
1067  */
nlmsg_trim(struct sk_buff * skb,const void * mark)1068 static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
1069 {
1070 	if (mark) {
1071 		WARN_ON((unsigned char *) mark < skb->data);
1072 		skb_trim(skb, (unsigned char *) mark - skb->data);
1073 	}
1074 }
1075 
1076 /**
1077  * nlmsg_cancel - Cancel construction of a netlink message
1078  * @skb: socket buffer the message is stored in
1079  * @nlh: netlink message header
1080  *
1081  * Removes the complete netlink message including all
1082  * attributes from the socket buffer again.
1083  */
nlmsg_cancel(struct sk_buff * skb,struct nlmsghdr * nlh)1084 static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
1085 {
1086 	nlmsg_trim(skb, nlh);
1087 }
1088 
1089 /**
1090  * nlmsg_free - drop a netlink message
1091  * @skb: socket buffer of netlink message
1092  */
nlmsg_free(struct sk_buff * skb)1093 static inline void nlmsg_free(struct sk_buff *skb)
1094 {
1095 	kfree_skb(skb);
1096 }
1097 
1098 /**
1099  * nlmsg_consume - free a netlink message
1100  * @skb: socket buffer of netlink message
1101  */
nlmsg_consume(struct sk_buff * skb)1102 static inline void nlmsg_consume(struct sk_buff *skb)
1103 {
1104 	consume_skb(skb);
1105 }
1106 
1107 /**
1108  * nlmsg_multicast_filtered - multicast a netlink message with filter function
1109  * @sk: netlink socket to spread messages to
1110  * @skb: netlink message as socket buffer
1111  * @portid: own netlink portid to avoid sending to yourself
1112  * @group: multicast group id
1113  * @flags: allocation flags
1114  * @filter: filter function
1115  * @filter_data: filter function private data
1116  *
1117  * Return: 0 on success, negative error code for failure.
1118  */
nlmsg_multicast_filtered(struct sock * sk,struct sk_buff * skb,u32 portid,unsigned int group,gfp_t flags,netlink_filter_fn filter,void * filter_data)1119 static inline int nlmsg_multicast_filtered(struct sock *sk, struct sk_buff *skb,
1120 					   u32 portid, unsigned int group,
1121 					   gfp_t flags,
1122 					   netlink_filter_fn filter,
1123 					   void *filter_data)
1124 {
1125 	int err;
1126 
1127 	NETLINK_CB(skb).dst_group = group;
1128 
1129 	err = netlink_broadcast_filtered(sk, skb, portid, group, flags,
1130 					 filter, filter_data);
1131 	if (err > 0)
1132 		err = 0;
1133 
1134 	return err;
1135 }
1136 
1137 /**
1138  * nlmsg_multicast - multicast a netlink message
1139  * @sk: netlink socket to spread messages to
1140  * @skb: netlink message as socket buffer
1141  * @portid: own netlink portid to avoid sending to yourself
1142  * @group: multicast group id
1143  * @flags: allocation flags
1144  */
nlmsg_multicast(struct sock * sk,struct sk_buff * skb,u32 portid,unsigned int group,gfp_t flags)1145 static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
1146 				  u32 portid, unsigned int group, gfp_t flags)
1147 {
1148 	return nlmsg_multicast_filtered(sk, skb, portid, group, flags,
1149 					NULL, NULL);
1150 }
1151 
1152 /**
1153  * nlmsg_unicast - unicast a netlink message
1154  * @sk: netlink socket to spread message to
1155  * @skb: netlink message as socket buffer
1156  * @portid: netlink portid of the destination socket
1157  */
nlmsg_unicast(struct sock * sk,struct sk_buff * skb,u32 portid)1158 static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
1159 {
1160 	int err;
1161 
1162 	err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
1163 	if (err > 0)
1164 		err = 0;
1165 
1166 	return err;
1167 }
1168 
1169 /**
1170  * nlmsg_for_each_msg - iterate over a stream of messages
1171  * @pos: loop counter, set to current message
1172  * @head: head of message stream
1173  * @len: length of message stream
1174  * @rem: initialized to len, holds bytes currently remaining in stream
1175  */
1176 #define nlmsg_for_each_msg(pos, head, len, rem) \
1177 	for (pos = head, rem = len; \
1178 	     nlmsg_ok(pos, rem); \
1179 	     pos = nlmsg_next(pos, &(rem)))
1180 
1181 /**
1182  * nl_dump_check_consistent - check if sequence is consistent and advertise if not
1183  * @cb: netlink callback structure that stores the sequence number
1184  * @nlh: netlink message header to write the flag to
1185  *
1186  * This function checks if the sequence (generation) number changed during dump
1187  * and if it did, advertises it in the netlink message header.
1188  *
1189  * The correct way to use it is to set cb->seq to the generation counter when
1190  * all locks for dumping have been acquired, and then call this function for
1191  * each message that is generated.
1192  *
1193  * Note that due to initialisation concerns, 0 is an invalid sequence number
1194  * and must not be used by code that uses this functionality.
1195  */
1196 static inline void
nl_dump_check_consistent(struct netlink_callback * cb,struct nlmsghdr * nlh)1197 nl_dump_check_consistent(struct netlink_callback *cb,
1198 			 struct nlmsghdr *nlh)
1199 {
1200 	if (cb->prev_seq && cb->seq != cb->prev_seq)
1201 		nlh->nlmsg_flags |= NLM_F_DUMP_INTR;
1202 	cb->prev_seq = cb->seq;
1203 }
1204 
1205 /**************************************************************************
1206  * Netlink Attributes
1207  **************************************************************************/
1208 
1209 /**
1210  * nla_attr_size - length of attribute not including padding
1211  * @payload: length of payload
1212  */
nla_attr_size(int payload)1213 static inline int nla_attr_size(int payload)
1214 {
1215 	return NLA_HDRLEN + payload;
1216 }
1217 
1218 /**
1219  * nla_total_size - total length of attribute including padding
1220  * @payload: length of payload
1221  */
nla_total_size(int payload)1222 static inline int nla_total_size(int payload)
1223 {
1224 	return NLA_ALIGN(nla_attr_size(payload));
1225 }
1226 
1227 /**
1228  * nla_padlen - length of padding at the tail of attribute
1229  * @payload: length of payload
1230  */
nla_padlen(int payload)1231 static inline int nla_padlen(int payload)
1232 {
1233 	return nla_total_size(payload) - nla_attr_size(payload);
1234 }
1235 
1236 /**
1237  * nla_type - attribute type
1238  * @nla: netlink attribute
1239  */
nla_type(const struct nlattr * nla)1240 static inline int nla_type(const struct nlattr *nla)
1241 {
1242 	return nla->nla_type & NLA_TYPE_MASK;
1243 }
1244 
1245 /**
1246  * nla_data - head of payload
1247  * @nla: netlink attribute
1248  */
nla_data(const struct nlattr * nla)1249 static inline void *nla_data(const struct nlattr *nla)
1250 {
1251 	return (char *) nla + NLA_HDRLEN;
1252 }
1253 
1254 /**
1255  * nla_len - length of payload
1256  * @nla: netlink attribute
1257  */
nla_len(const struct nlattr * nla)1258 static inline u16 nla_len(const struct nlattr *nla)
1259 {
1260 	return nla->nla_len - NLA_HDRLEN;
1261 }
1262 
1263 /**
1264  * nla_ok - check if the netlink attribute fits into the remaining bytes
1265  * @nla: netlink attribute
1266  * @remaining: number of bytes remaining in attribute stream
1267  */
nla_ok(const struct nlattr * nla,int remaining)1268 static inline int nla_ok(const struct nlattr *nla, int remaining)
1269 {
1270 	return remaining >= (int) sizeof(*nla) &&
1271 	       nla->nla_len >= sizeof(*nla) &&
1272 	       nla->nla_len <= remaining;
1273 }
1274 
1275 /**
1276  * nla_next - next netlink attribute in attribute stream
1277  * @nla: netlink attribute
1278  * @remaining: number of bytes remaining in attribute stream
1279  *
1280  * Returns: the next netlink attribute in the attribute stream and
1281  * decrements remaining by the size of the current attribute.
1282  */
nla_next(const struct nlattr * nla,int * remaining)1283 static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
1284 {
1285 	unsigned int totlen = NLA_ALIGN(nla->nla_len);
1286 
1287 	*remaining -= totlen;
1288 	return (struct nlattr *) ((char *) nla + totlen);
1289 }
1290 
1291 /**
1292  * nla_find_nested - find attribute in a set of nested attributes
1293  * @nla: attribute containing the nested attributes
1294  * @attrtype: type of attribute to look for
1295  *
1296  * Returns: the first attribute which matches the specified type.
1297  */
1298 static inline struct nlattr *
nla_find_nested(const struct nlattr * nla,int attrtype)1299 nla_find_nested(const struct nlattr *nla, int attrtype)
1300 {
1301 	return nla_find(nla_data(nla), nla_len(nla), attrtype);
1302 }
1303 
1304 /**
1305  * nla_parse_nested - parse nested attributes
1306  * @tb: destination array with maxtype+1 elements
1307  * @maxtype: maximum attribute type to be expected
1308  * @nla: attribute containing the nested attributes
1309  * @policy: validation policy
1310  * @extack: extended ACK report struct
1311  *
1312  * See nla_parse()
1313  */
nla_parse_nested(struct nlattr * tb[],int maxtype,const struct nlattr * nla,const struct nla_policy * policy,struct netlink_ext_ack * extack)1314 static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
1315 				   const struct nlattr *nla,
1316 				   const struct nla_policy *policy,
1317 				   struct netlink_ext_ack *extack)
1318 {
1319 	if (!(nla->nla_type & NLA_F_NESTED)) {
1320 		NL_SET_ERR_MSG_ATTR(extack, nla, "NLA_F_NESTED is missing");
1321 		return -EINVAL;
1322 	}
1323 
1324 	return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
1325 			   NL_VALIDATE_STRICT, extack);
1326 }
1327 
1328 /**
1329  * nla_parse_nested_deprecated - parse nested attributes
1330  * @tb: destination array with maxtype+1 elements
1331  * @maxtype: maximum attribute type to be expected
1332  * @nla: attribute containing the nested attributes
1333  * @policy: validation policy
1334  * @extack: extended ACK report struct
1335  *
1336  * See nla_parse_deprecated()
1337  */
nla_parse_nested_deprecated(struct nlattr * tb[],int maxtype,const struct nlattr * nla,const struct nla_policy * policy,struct netlink_ext_ack * extack)1338 static inline int nla_parse_nested_deprecated(struct nlattr *tb[], int maxtype,
1339 					      const struct nlattr *nla,
1340 					      const struct nla_policy *policy,
1341 					      struct netlink_ext_ack *extack)
1342 {
1343 	return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
1344 			   NL_VALIDATE_LIBERAL, extack);
1345 }
1346 
1347 /**
1348  * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
1349  * @skb: socket buffer to add attribute to
1350  * @attrtype: attribute type
1351  * @value: numeric value
1352  */
nla_put_u8(struct sk_buff * skb,int attrtype,u8 value)1353 static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
1354 {
1355 	/* temporary variables to work around GCC PR81715 with asan-stack=1 */
1356 	u8 tmp = value;
1357 
1358 	return nla_put(skb, attrtype, sizeof(u8), &tmp);
1359 }
1360 
1361 /**
1362  * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
1363  * @skb: socket buffer to add attribute to
1364  * @attrtype: attribute type
1365  * @value: numeric value
1366  */
nla_put_u16(struct sk_buff * skb,int attrtype,u16 value)1367 static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
1368 {
1369 	u16 tmp = value;
1370 
1371 	return nla_put(skb, attrtype, sizeof(u16), &tmp);
1372 }
1373 
1374 /**
1375  * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer
1376  * @skb: socket buffer to add attribute to
1377  * @attrtype: attribute type
1378  * @value: numeric value
1379  */
nla_put_be16(struct sk_buff * skb,int attrtype,__be16 value)1380 static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
1381 {
1382 	__be16 tmp = value;
1383 
1384 	return nla_put(skb, attrtype, sizeof(__be16), &tmp);
1385 }
1386 
1387 /**
1388  * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer
1389  * @skb: socket buffer to add attribute to
1390  * @attrtype: attribute type
1391  * @value: numeric value
1392  */
nla_put_net16(struct sk_buff * skb,int attrtype,__be16 value)1393 static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
1394 {
1395 	__be16 tmp = value;
1396 
1397 	return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
1398 }
1399 
1400 /**
1401  * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer
1402  * @skb: socket buffer to add attribute to
1403  * @attrtype: attribute type
1404  * @value: numeric value
1405  */
nla_put_le16(struct sk_buff * skb,int attrtype,__le16 value)1406 static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
1407 {
1408 	__le16 tmp = value;
1409 
1410 	return nla_put(skb, attrtype, sizeof(__le16), &tmp);
1411 }
1412 
1413 /**
1414  * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
1415  * @skb: socket buffer to add attribute to
1416  * @attrtype: attribute type
1417  * @value: numeric value
1418  */
nla_put_u32(struct sk_buff * skb,int attrtype,u32 value)1419 static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
1420 {
1421 	u32 tmp = value;
1422 
1423 	return nla_put(skb, attrtype, sizeof(u32), &tmp);
1424 }
1425 
1426 /**
1427  * nla_put_uint - Add a variable-size unsigned int to a socket buffer
1428  * @skb: socket buffer to add attribute to
1429  * @attrtype: attribute type
1430  * @value: numeric value
1431  */
nla_put_uint(struct sk_buff * skb,int attrtype,u64 value)1432 static inline int nla_put_uint(struct sk_buff *skb, int attrtype, u64 value)
1433 {
1434 	u64 tmp64 = value;
1435 	u32 tmp32 = value;
1436 
1437 	if (tmp64 == tmp32)
1438 		return nla_put_u32(skb, attrtype, tmp32);
1439 	return nla_put(skb, attrtype, sizeof(u64), &tmp64);
1440 }
1441 
1442 /**
1443  * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer
1444  * @skb: socket buffer to add attribute to
1445  * @attrtype: attribute type
1446  * @value: numeric value
1447  */
nla_put_be32(struct sk_buff * skb,int attrtype,__be32 value)1448 static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
1449 {
1450 	__be32 tmp = value;
1451 
1452 	return nla_put(skb, attrtype, sizeof(__be32), &tmp);
1453 }
1454 
1455 /**
1456  * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer
1457  * @skb: socket buffer to add attribute to
1458  * @attrtype: attribute type
1459  * @value: numeric value
1460  */
nla_put_net32(struct sk_buff * skb,int attrtype,__be32 value)1461 static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
1462 {
1463 	__be32 tmp = value;
1464 
1465 	return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
1466 }
1467 
1468 /**
1469  * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer
1470  * @skb: socket buffer to add attribute to
1471  * @attrtype: attribute type
1472  * @value: numeric value
1473  */
nla_put_le32(struct sk_buff * skb,int attrtype,__le32 value)1474 static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value)
1475 {
1476 	__le32 tmp = value;
1477 
1478 	return nla_put(skb, attrtype, sizeof(__le32), &tmp);
1479 }
1480 
1481 /**
1482  * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it
1483  * @skb: socket buffer to add attribute to
1484  * @attrtype: attribute type
1485  * @value: numeric value
1486  * @padattr: attribute type for the padding
1487  */
nla_put_u64_64bit(struct sk_buff * skb,int attrtype,u64 value,int padattr)1488 static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype,
1489 				    u64 value, int padattr)
1490 {
1491 	u64 tmp = value;
1492 
1493 	return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
1494 }
1495 
1496 /**
1497  * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it
1498  * @skb: socket buffer to add attribute to
1499  * @attrtype: attribute type
1500  * @value: numeric value
1501  * @padattr: attribute type for the padding
1502  */
nla_put_be64(struct sk_buff * skb,int attrtype,__be64 value,int padattr)1503 static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value,
1504 			       int padattr)
1505 {
1506 	__be64 tmp = value;
1507 
1508 	return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr);
1509 }
1510 
1511 /**
1512  * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it
1513  * @skb: socket buffer to add attribute to
1514  * @attrtype: attribute type
1515  * @value: numeric value
1516  * @padattr: attribute type for the padding
1517  */
nla_put_net64(struct sk_buff * skb,int attrtype,__be64 value,int padattr)1518 static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value,
1519 				int padattr)
1520 {
1521 	__be64 tmp = value;
1522 
1523 	return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp,
1524 			    padattr);
1525 }
1526 
1527 /**
1528  * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer 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  */
nla_put_le64(struct sk_buff * skb,int attrtype,__le64 value,int padattr)1534 static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value,
1535 			       int padattr)
1536 {
1537 	__le64 tmp = value;
1538 
1539 	return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr);
1540 }
1541 
1542 /**
1543  * nla_put_s8 - Add a s8 netlink attribute to a socket buffer
1544  * @skb: socket buffer to add attribute to
1545  * @attrtype: attribute type
1546  * @value: numeric value
1547  */
nla_put_s8(struct sk_buff * skb,int attrtype,s8 value)1548 static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
1549 {
1550 	s8 tmp = value;
1551 
1552 	return nla_put(skb, attrtype, sizeof(s8), &tmp);
1553 }
1554 
1555 /**
1556  * nla_put_s16 - Add a s16 netlink attribute to a socket buffer
1557  * @skb: socket buffer to add attribute to
1558  * @attrtype: attribute type
1559  * @value: numeric value
1560  */
nla_put_s16(struct sk_buff * skb,int attrtype,s16 value)1561 static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
1562 {
1563 	s16 tmp = value;
1564 
1565 	return nla_put(skb, attrtype, sizeof(s16), &tmp);
1566 }
1567 
1568 /**
1569  * nla_put_s32 - Add a s32 netlink attribute to a socket buffer
1570  * @skb: socket buffer to add attribute to
1571  * @attrtype: attribute type
1572  * @value: numeric value
1573  */
nla_put_s32(struct sk_buff * skb,int attrtype,s32 value)1574 static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
1575 {
1576 	s32 tmp = value;
1577 
1578 	return nla_put(skb, attrtype, sizeof(s32), &tmp);
1579 }
1580 
1581 /**
1582  * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it
1583  * @skb: socket buffer to add attribute to
1584  * @attrtype: attribute type
1585  * @value: numeric value
1586  * @padattr: attribute type for the padding
1587  */
nla_put_s64(struct sk_buff * skb,int attrtype,s64 value,int padattr)1588 static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value,
1589 			      int padattr)
1590 {
1591 	s64 tmp = value;
1592 
1593 	return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr);
1594 }
1595 
1596 /**
1597  * nla_put_sint - Add a variable-size signed int to a socket buffer
1598  * @skb: socket buffer to add attribute to
1599  * @attrtype: attribute type
1600  * @value: numeric value
1601  */
nla_put_sint(struct sk_buff * skb,int attrtype,s64 value)1602 static inline int nla_put_sint(struct sk_buff *skb, int attrtype, s64 value)
1603 {
1604 	s64 tmp64 = value;
1605 	s32 tmp32 = value;
1606 
1607 	if (tmp64 == tmp32)
1608 		return nla_put_s32(skb, attrtype, tmp32);
1609 	return nla_put(skb, attrtype, sizeof(s64), &tmp64);
1610 }
1611 
1612 /**
1613  * nla_put_string - Add a string netlink attribute to a socket buffer
1614  * @skb: socket buffer to add attribute to
1615  * @attrtype: attribute type
1616  * @str: NUL terminated string
1617  */
nla_put_string(struct sk_buff * skb,int attrtype,const char * str)1618 static inline int nla_put_string(struct sk_buff *skb, int attrtype,
1619 				 const char *str)
1620 {
1621 	return nla_put(skb, attrtype, strlen(str) + 1, str);
1622 }
1623 
1624 /**
1625  * nla_put_flag - Add a flag netlink attribute to a socket buffer
1626  * @skb: socket buffer to add attribute to
1627  * @attrtype: attribute type
1628  */
nla_put_flag(struct sk_buff * skb,int attrtype)1629 static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
1630 {
1631 	return nla_put(skb, attrtype, 0, NULL);
1632 }
1633 
1634 /**
1635  * nla_put_msecs - Add a msecs netlink attribute to a skb and align it
1636  * @skb: socket buffer to add attribute to
1637  * @attrtype: attribute type
1638  * @njiffies: number of jiffies to convert to msecs
1639  * @padattr: attribute type for the padding
1640  */
nla_put_msecs(struct sk_buff * skb,int attrtype,unsigned long njiffies,int padattr)1641 static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
1642 				unsigned long njiffies, int padattr)
1643 {
1644 	u64 tmp = jiffies_to_msecs(njiffies);
1645 
1646 	return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
1647 }
1648 
1649 /**
1650  * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket
1651  * buffer
1652  * @skb: socket buffer to add attribute to
1653  * @attrtype: attribute type
1654  * @addr: IPv4 address
1655  */
nla_put_in_addr(struct sk_buff * skb,int attrtype,__be32 addr)1656 static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
1657 				  __be32 addr)
1658 {
1659 	__be32 tmp = addr;
1660 
1661 	return nla_put_be32(skb, attrtype, tmp);
1662 }
1663 
1664 /**
1665  * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket
1666  * buffer
1667  * @skb: socket buffer to add attribute to
1668  * @attrtype: attribute type
1669  * @addr: IPv6 address
1670  */
nla_put_in6_addr(struct sk_buff * skb,int attrtype,const struct in6_addr * addr)1671 static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype,
1672 				   const struct in6_addr *addr)
1673 {
1674 	return nla_put(skb, attrtype, sizeof(*addr), addr);
1675 }
1676 
1677 /**
1678  * nla_put_bitfield32 - Add a bitfield32 netlink attribute to a socket buffer
1679  * @skb: socket buffer to add attribute to
1680  * @attrtype: attribute type
1681  * @value: value carrying bits
1682  * @selector: selector of valid bits
1683  */
nla_put_bitfield32(struct sk_buff * skb,int attrtype,__u32 value,__u32 selector)1684 static inline int nla_put_bitfield32(struct sk_buff *skb, int attrtype,
1685 				     __u32 value, __u32 selector)
1686 {
1687 	struct nla_bitfield32 tmp = { value, selector, };
1688 
1689 	return nla_put(skb, attrtype, sizeof(tmp), &tmp);
1690 }
1691 
1692 /**
1693  * nla_get_u32 - return payload of u32 attribute
1694  * @nla: u32 netlink attribute
1695  */
nla_get_u32(const struct nlattr * nla)1696 static inline u32 nla_get_u32(const struct nlattr *nla)
1697 {
1698 	return *(u32 *) nla_data(nla);
1699 }
1700 
1701 /**
1702  * nla_get_u32_default - return payload of u32 attribute or default
1703  * @nla: u32 netlink attribute, may be %NULL
1704  * @defvalue: default value to use if @nla is %NULL
1705  *
1706  * Return: the value of the attribute, or the default value if not present
1707  */
nla_get_u32_default(const struct nlattr * nla,u32 defvalue)1708 static inline u32 nla_get_u32_default(const struct nlattr *nla, u32 defvalue)
1709 {
1710 	if (!nla)
1711 		return defvalue;
1712 	return nla_get_u32(nla);
1713 }
1714 
1715 /**
1716  * nla_get_be32 - return payload of __be32 attribute
1717  * @nla: __be32 netlink attribute
1718  */
nla_get_be32(const struct nlattr * nla)1719 static inline __be32 nla_get_be32(const struct nlattr *nla)
1720 {
1721 	return *(__be32 *) nla_data(nla);
1722 }
1723 
1724 /**
1725  * nla_get_be32_default - return payload of be32 attribute or default
1726  * @nla: __be32 netlink attribute, may be %NULL
1727  * @defvalue: default value to use if @nla is %NULL
1728  *
1729  * Return: the value of the attribute, or the default value if not present
1730  */
nla_get_be32_default(const struct nlattr * nla,__be32 defvalue)1731 static inline __be32 nla_get_be32_default(const struct nlattr *nla,
1732 					  __be32 defvalue)
1733 {
1734 	if (!nla)
1735 		return defvalue;
1736 	return nla_get_be32(nla);
1737 }
1738 
1739 /**
1740  * nla_get_le32 - return payload of __le32 attribute
1741  * @nla: __le32 netlink attribute
1742  */
nla_get_le32(const struct nlattr * nla)1743 static inline __le32 nla_get_le32(const struct nlattr *nla)
1744 {
1745 	return *(__le32 *) nla_data(nla);
1746 }
1747 
1748 /**
1749  * nla_get_le32_default - return payload of le32 attribute or default
1750  * @nla: __le32 netlink attribute, may be %NULL
1751  * @defvalue: default value to use if @nla is %NULL
1752  *
1753  * Return: the value of the attribute, or the default value if not present
1754  */
nla_get_le32_default(const struct nlattr * nla,__le32 defvalue)1755 static inline __le32 nla_get_le32_default(const struct nlattr *nla,
1756 					  __le32 defvalue)
1757 {
1758 	if (!nla)
1759 		return defvalue;
1760 	return nla_get_le32(nla);
1761 }
1762 
1763 /**
1764  * nla_get_u16 - return payload of u16 attribute
1765  * @nla: u16 netlink attribute
1766  */
nla_get_u16(const struct nlattr * nla)1767 static inline u16 nla_get_u16(const struct nlattr *nla)
1768 {
1769 	return *(u16 *) nla_data(nla);
1770 }
1771 
1772 /**
1773  * nla_get_u16_default - return payload of u16 attribute or default
1774  * @nla: u16 netlink attribute, may be %NULL
1775  * @defvalue: default value to use if @nla is %NULL
1776  *
1777  * Return: the value of the attribute, or the default value if not present
1778  */
nla_get_u16_default(const struct nlattr * nla,u16 defvalue)1779 static inline u16 nla_get_u16_default(const struct nlattr *nla, u16 defvalue)
1780 {
1781 	if (!nla)
1782 		return defvalue;
1783 	return nla_get_u16(nla);
1784 }
1785 
1786 /**
1787  * nla_get_be16 - return payload of __be16 attribute
1788  * @nla: __be16 netlink attribute
1789  */
nla_get_be16(const struct nlattr * nla)1790 static inline __be16 nla_get_be16(const struct nlattr *nla)
1791 {
1792 	return *(__be16 *) nla_data(nla);
1793 }
1794 
1795 /**
1796  * nla_get_be16_default - return payload of be16 attribute or default
1797  * @nla: __be16 netlink attribute, may be %NULL
1798  * @defvalue: default value to use if @nla is %NULL
1799  *
1800  * Return: the value of the attribute, or the default value if not present
1801  */
nla_get_be16_default(const struct nlattr * nla,__be16 defvalue)1802 static inline __be16 nla_get_be16_default(const struct nlattr *nla,
1803 					  __be16 defvalue)
1804 {
1805 	if (!nla)
1806 		return defvalue;
1807 	return nla_get_be16(nla);
1808 }
1809 
1810 /**
1811  * nla_get_le16 - return payload of __le16 attribute
1812  * @nla: __le16 netlink attribute
1813  */
nla_get_le16(const struct nlattr * nla)1814 static inline __le16 nla_get_le16(const struct nlattr *nla)
1815 {
1816 	return *(__le16 *) nla_data(nla);
1817 }
1818 
1819 /**
1820  * nla_get_le16_default - return payload of le16 attribute or default
1821  * @nla: __le16 netlink attribute, may be %NULL
1822  * @defvalue: default value to use if @nla is %NULL
1823  *
1824  * Return: the value of the attribute, or the default value if not present
1825  */
nla_get_le16_default(const struct nlattr * nla,__le16 defvalue)1826 static inline __le16 nla_get_le16_default(const struct nlattr *nla,
1827 					  __le16 defvalue)
1828 {
1829 	if (!nla)
1830 		return defvalue;
1831 	return nla_get_le16(nla);
1832 }
1833 
1834 /**
1835  * nla_get_u8 - return payload of u8 attribute
1836  * @nla: u8 netlink attribute
1837  */
nla_get_u8(const struct nlattr * nla)1838 static inline u8 nla_get_u8(const struct nlattr *nla)
1839 {
1840 	return *(u8 *) nla_data(nla);
1841 }
1842 
1843 /**
1844  * nla_get_u8_default - return payload of u8 attribute or default
1845  * @nla: u8 netlink attribute, may be %NULL
1846  * @defvalue: default value to use if @nla is %NULL
1847  *
1848  * Return: the value of the attribute, or the default value if not present
1849  */
nla_get_u8_default(const struct nlattr * nla,u8 defvalue)1850 static inline u8 nla_get_u8_default(const struct nlattr *nla, u8 defvalue)
1851 {
1852 	if (!nla)
1853 		return defvalue;
1854 	return nla_get_u8(nla);
1855 }
1856 
1857 /**
1858  * nla_get_u64 - return payload of u64 attribute
1859  * @nla: u64 netlink attribute
1860  */
nla_get_u64(const struct nlattr * nla)1861 static inline u64 nla_get_u64(const struct nlattr *nla)
1862 {
1863 	u64 tmp;
1864 
1865 	nla_memcpy(&tmp, nla, sizeof(tmp));
1866 
1867 	return tmp;
1868 }
1869 
1870 /**
1871  * nla_get_u64_default - return payload of u64 attribute or default
1872  * @nla: u64 netlink attribute, may be %NULL
1873  * @defvalue: default value to use if @nla is %NULL
1874  *
1875  * Return: the value of the attribute, or the default value if not present
1876  */
nla_get_u64_default(const struct nlattr * nla,u64 defvalue)1877 static inline u64 nla_get_u64_default(const struct nlattr *nla, u64 defvalue)
1878 {
1879 	if (!nla)
1880 		return defvalue;
1881 	return nla_get_u64(nla);
1882 }
1883 
1884 /**
1885  * nla_get_uint - return payload of uint attribute
1886  * @nla: uint netlink attribute
1887  */
nla_get_uint(const struct nlattr * nla)1888 static inline u64 nla_get_uint(const struct nlattr *nla)
1889 {
1890 	if (nla_len(nla) == sizeof(u32))
1891 		return nla_get_u32(nla);
1892 	return nla_get_u64(nla);
1893 }
1894 
1895 /**
1896  * nla_get_uint_default - return payload of uint attribute or default
1897  * @nla: uint netlink attribute, may be %NULL
1898  * @defvalue: default value to use if @nla is %NULL
1899  *
1900  * Return: the value of the attribute, or the default value if not present
1901  */
nla_get_uint_default(const struct nlattr * nla,u64 defvalue)1902 static inline u64 nla_get_uint_default(const struct nlattr *nla, u64 defvalue)
1903 {
1904 	if (!nla)
1905 		return defvalue;
1906 	return nla_get_uint(nla);
1907 }
1908 
1909 /**
1910  * nla_get_be64 - return payload of __be64 attribute
1911  * @nla: __be64 netlink attribute
1912  */
nla_get_be64(const struct nlattr * nla)1913 static inline __be64 nla_get_be64(const struct nlattr *nla)
1914 {
1915 	__be64 tmp;
1916 
1917 	nla_memcpy(&tmp, nla, sizeof(tmp));
1918 
1919 	return tmp;
1920 }
1921 
1922 /**
1923  * nla_get_be64_default - return payload of be64 attribute or default
1924  * @nla: __be64 netlink attribute, may be %NULL
1925  * @defvalue: default value to use if @nla is %NULL
1926  *
1927  * Return: the value of the attribute, or the default value if not present
1928  */
nla_get_be64_default(const struct nlattr * nla,__be64 defvalue)1929 static inline __be64 nla_get_be64_default(const struct nlattr *nla,
1930 					  __be64 defvalue)
1931 {
1932 	if (!nla)
1933 		return defvalue;
1934 	return nla_get_be64(nla);
1935 }
1936 
1937 /**
1938  * nla_get_le64 - return payload of __le64 attribute
1939  * @nla: __le64 netlink attribute
1940  */
nla_get_le64(const struct nlattr * nla)1941 static inline __le64 nla_get_le64(const struct nlattr *nla)
1942 {
1943 	return *(__le64 *) nla_data(nla);
1944 }
1945 
1946 /**
1947  * nla_get_le64_default - return payload of le64 attribute or default
1948  * @nla: __le64 netlink attribute, may be %NULL
1949  * @defvalue: default value to use if @nla is %NULL
1950  *
1951  * Return: the value of the attribute, or the default value if not present
1952  */
nla_get_le64_default(const struct nlattr * nla,__le64 defvalue)1953 static inline __le64 nla_get_le64_default(const struct nlattr *nla,
1954 					  __le64 defvalue)
1955 {
1956 	if (!nla)
1957 		return defvalue;
1958 	return nla_get_le64(nla);
1959 }
1960 
1961 /**
1962  * nla_get_s32 - return payload of s32 attribute
1963  * @nla: s32 netlink attribute
1964  */
nla_get_s32(const struct nlattr * nla)1965 static inline s32 nla_get_s32(const struct nlattr *nla)
1966 {
1967 	return *(s32 *) nla_data(nla);
1968 }
1969 
1970 /**
1971  * nla_get_s32_default - return payload of s32 attribute or default
1972  * @nla: s32 netlink attribute, may be %NULL
1973  * @defvalue: default value to use if @nla is %NULL
1974  *
1975  * Return: the value of the attribute, or the default value if not present
1976  */
nla_get_s32_default(const struct nlattr * nla,s32 defvalue)1977 static inline s32 nla_get_s32_default(const struct nlattr *nla, s32 defvalue)
1978 {
1979 	if (!nla)
1980 		return defvalue;
1981 	return nla_get_s32(nla);
1982 }
1983 
1984 /**
1985  * nla_get_s16 - return payload of s16 attribute
1986  * @nla: s16 netlink attribute
1987  */
nla_get_s16(const struct nlattr * nla)1988 static inline s16 nla_get_s16(const struct nlattr *nla)
1989 {
1990 	return *(s16 *) nla_data(nla);
1991 }
1992 
1993 /**
1994  * nla_get_s16_default - return payload of s16 attribute or default
1995  * @nla: s16 netlink attribute, may be %NULL
1996  * @defvalue: default value to use if @nla is %NULL
1997  *
1998  * Return: the value of the attribute, or the default value if not present
1999  */
nla_get_s16_default(const struct nlattr * nla,s16 defvalue)2000 static inline s16 nla_get_s16_default(const struct nlattr *nla, s16 defvalue)
2001 {
2002 	if (!nla)
2003 		return defvalue;
2004 	return nla_get_s16(nla);
2005 }
2006 
2007 /**
2008  * nla_get_s8 - return payload of s8 attribute
2009  * @nla: s8 netlink attribute
2010  */
nla_get_s8(const struct nlattr * nla)2011 static inline s8 nla_get_s8(const struct nlattr *nla)
2012 {
2013 	return *(s8 *) nla_data(nla);
2014 }
2015 
2016 /**
2017  * nla_get_s8_default - return payload of s8 attribute or default
2018  * @nla: s8 netlink attribute, may be %NULL
2019  * @defvalue: default value to use if @nla is %NULL
2020  *
2021  * Return: the value of the attribute, or the default value if not present
2022  */
nla_get_s8_default(const struct nlattr * nla,s8 defvalue)2023 static inline s8 nla_get_s8_default(const struct nlattr *nla, s8 defvalue)
2024 {
2025 	if (!nla)
2026 		return defvalue;
2027 	return nla_get_s8(nla);
2028 }
2029 
2030 /**
2031  * nla_get_s64 - return payload of s64 attribute
2032  * @nla: s64 netlink attribute
2033  */
nla_get_s64(const struct nlattr * nla)2034 static inline s64 nla_get_s64(const struct nlattr *nla)
2035 {
2036 	s64 tmp;
2037 
2038 	nla_memcpy(&tmp, nla, sizeof(tmp));
2039 
2040 	return tmp;
2041 }
2042 
2043 /**
2044  * nla_get_s64_default - return payload of s64 attribute or default
2045  * @nla: s64 netlink attribute, may be %NULL
2046  * @defvalue: default value to use if @nla is %NULL
2047  *
2048  * Return: the value of the attribute, or the default value if not present
2049  */
nla_get_s64_default(const struct nlattr * nla,s64 defvalue)2050 static inline s64 nla_get_s64_default(const struct nlattr *nla, s64 defvalue)
2051 {
2052 	if (!nla)
2053 		return defvalue;
2054 	return nla_get_s64(nla);
2055 }
2056 
2057 /**
2058  * nla_get_sint - return payload of uint attribute
2059  * @nla: uint netlink attribute
2060  */
nla_get_sint(const struct nlattr * nla)2061 static inline s64 nla_get_sint(const struct nlattr *nla)
2062 {
2063 	if (nla_len(nla) == sizeof(s32))
2064 		return nla_get_s32(nla);
2065 	return nla_get_s64(nla);
2066 }
2067 
2068 /**
2069  * nla_get_sint_default - return payload of sint attribute or default
2070  * @nla: sint netlink attribute, may be %NULL
2071  * @defvalue: default value to use if @nla is %NULL
2072  *
2073  * Return: the value of the attribute, or the default value if not present
2074  */
nla_get_sint_default(const struct nlattr * nla,s64 defvalue)2075 static inline s64 nla_get_sint_default(const struct nlattr *nla, s64 defvalue)
2076 {
2077 	if (!nla)
2078 		return defvalue;
2079 	return nla_get_sint(nla);
2080 }
2081 
2082 /**
2083  * nla_get_flag - return payload of flag attribute
2084  * @nla: flag netlink attribute
2085  */
nla_get_flag(const struct nlattr * nla)2086 static inline int nla_get_flag(const struct nlattr *nla)
2087 {
2088 	return !!nla;
2089 }
2090 
2091 /**
2092  * nla_get_msecs - return payload of msecs attribute
2093  * @nla: msecs netlink attribute
2094  *
2095  * Returns: the number of milliseconds in jiffies.
2096  */
nla_get_msecs(const struct nlattr * nla)2097 static inline unsigned long nla_get_msecs(const struct nlattr *nla)
2098 {
2099 	u64 msecs = nla_get_u64(nla);
2100 
2101 	return msecs_to_jiffies((unsigned long) msecs);
2102 }
2103 
2104 /**
2105  * nla_get_msecs_default - return payload of msecs attribute or default
2106  * @nla: msecs netlink attribute, may be %NULL
2107  * @defvalue: default value to use if @nla is %NULL
2108  *
2109  * Return: the value of the attribute, or the default value if not present
2110  */
nla_get_msecs_default(const struct nlattr * nla,unsigned long defvalue)2111 static inline unsigned long nla_get_msecs_default(const struct nlattr *nla,
2112 						  unsigned long defvalue)
2113 {
2114 	if (!nla)
2115 		return defvalue;
2116 	return nla_get_msecs(nla);
2117 }
2118 
2119 /**
2120  * nla_get_in_addr - return payload of IPv4 address attribute
2121  * @nla: IPv4 address netlink attribute
2122  */
nla_get_in_addr(const struct nlattr * nla)2123 static inline __be32 nla_get_in_addr(const struct nlattr *nla)
2124 {
2125 	return *(__be32 *) nla_data(nla);
2126 }
2127 
2128 /**
2129  * nla_get_in_addr_default - return payload of be32 attribute or default
2130  * @nla: IPv4 address netlink attribute, may be %NULL
2131  * @defvalue: default value to use if @nla is %NULL
2132  *
2133  * Return: the value of the attribute, or the default value if not present
2134  */
nla_get_in_addr_default(const struct nlattr * nla,__be32 defvalue)2135 static inline __be32 nla_get_in_addr_default(const struct nlattr *nla,
2136 					     __be32 defvalue)
2137 {
2138 	if (!nla)
2139 		return defvalue;
2140 	return nla_get_in_addr(nla);
2141 }
2142 
2143 /**
2144  * nla_get_in6_addr - return payload of IPv6 address attribute
2145  * @nla: IPv6 address netlink attribute
2146  */
nla_get_in6_addr(const struct nlattr * nla)2147 static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla)
2148 {
2149 	struct in6_addr tmp;
2150 
2151 	nla_memcpy(&tmp, nla, sizeof(tmp));
2152 	return tmp;
2153 }
2154 
2155 /**
2156  * nla_get_bitfield32 - return payload of 32 bitfield attribute
2157  * @nla: nla_bitfield32 attribute
2158  */
nla_get_bitfield32(const struct nlattr * nla)2159 static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla)
2160 {
2161 	struct nla_bitfield32 tmp;
2162 
2163 	nla_memcpy(&tmp, nla, sizeof(tmp));
2164 	return tmp;
2165 }
2166 
2167 /**
2168  * nla_memdup - duplicate attribute memory (kmemdup)
2169  * @src: netlink attribute to duplicate from
2170  * @gfp: GFP mask
2171  */
nla_memdup_noprof(const struct nlattr * src,gfp_t gfp)2172 static inline void *nla_memdup_noprof(const struct nlattr *src, gfp_t gfp)
2173 {
2174 	return kmemdup_noprof(nla_data(src), nla_len(src), gfp);
2175 }
2176 #define nla_memdup(...)	alloc_hooks(nla_memdup_noprof(__VA_ARGS__))
2177 
2178 /**
2179  * nla_nest_start_noflag - Start a new level of nested attributes
2180  * @skb: socket buffer to add attributes to
2181  * @attrtype: attribute type of container
2182  *
2183  * This function exists for backward compatibility to use in APIs which never
2184  * marked their nest attributes with NLA_F_NESTED flag. New APIs should use
2185  * nla_nest_start() which sets the flag.
2186  *
2187  * Returns: the container attribute or NULL on error
2188  */
nla_nest_start_noflag(struct sk_buff * skb,int attrtype)2189 static inline struct nlattr *nla_nest_start_noflag(struct sk_buff *skb,
2190 						   int attrtype)
2191 {
2192 	struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
2193 
2194 	if (nla_put(skb, attrtype, 0, NULL) < 0)
2195 		return NULL;
2196 
2197 	return start;
2198 }
2199 
2200 /**
2201  * nla_nest_start - Start a new level of nested attributes, with NLA_F_NESTED
2202  * @skb: socket buffer to add attributes to
2203  * @attrtype: attribute type of container
2204  *
2205  * Unlike nla_nest_start_noflag(), mark the nest attribute with NLA_F_NESTED
2206  * flag. This is the preferred function to use in new code.
2207  *
2208  * Returns: the container attribute or NULL on error
2209  */
nla_nest_start(struct sk_buff * skb,int attrtype)2210 static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
2211 {
2212 	return nla_nest_start_noflag(skb, attrtype | NLA_F_NESTED);
2213 }
2214 
2215 /**
2216  * nla_nest_end - Finalize nesting of attributes
2217  * @skb: socket buffer the attributes are stored in
2218  * @start: container attribute
2219  *
2220  * Corrects the container attribute header to include the all
2221  * appended attributes.
2222  *
2223  * Returns: the total data length of the skb.
2224  */
nla_nest_end(struct sk_buff * skb,struct nlattr * start)2225 static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
2226 {
2227 	start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
2228 	return skb->len;
2229 }
2230 
2231 /**
2232  * nla_nest_cancel - Cancel nesting of attributes
2233  * @skb: socket buffer the message is stored in
2234  * @start: container attribute
2235  *
2236  * Removes the container attribute and including all nested
2237  * attributes. Returns -EMSGSIZE
2238  */
nla_nest_cancel(struct sk_buff * skb,struct nlattr * start)2239 static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
2240 {
2241 	nlmsg_trim(skb, start);
2242 }
2243 
2244 /**
2245  * nla_put_empty_nest - Create an empty nest
2246  * @skb: socket buffer the message is stored in
2247  * @attrtype: attribute type of the container
2248  *
2249  * This function is a helper for creating empty nests.
2250  *
2251  * Returns: 0 when successful or -EMSGSIZE on failure.
2252  */
nla_put_empty_nest(struct sk_buff * skb,int attrtype)2253 static inline int nla_put_empty_nest(struct sk_buff *skb, int attrtype)
2254 {
2255 	return nla_nest_start(skb, attrtype) ? 0 : -EMSGSIZE;
2256 }
2257 
2258 /**
2259  * __nla_validate_nested - Validate a stream of nested attributes
2260  * @start: container attribute
2261  * @maxtype: maximum attribute type to be expected
2262  * @policy: validation policy
2263  * @validate: validation strictness
2264  * @extack: extended ACK report struct
2265  *
2266  * Validates all attributes in the nested attribute stream against the
2267  * specified policy. Attributes with a type exceeding maxtype will be
2268  * ignored. See documentation of struct nla_policy for more details.
2269  *
2270  * Returns: 0 on success or a negative error code.
2271  */
__nla_validate_nested(const struct nlattr * start,int maxtype,const struct nla_policy * policy,unsigned int validate,struct netlink_ext_ack * extack)2272 static inline int __nla_validate_nested(const struct nlattr *start, int maxtype,
2273 					const struct nla_policy *policy,
2274 					unsigned int validate,
2275 					struct netlink_ext_ack *extack)
2276 {
2277 	return __nla_validate(nla_data(start), nla_len(start), maxtype, policy,
2278 			      validate, extack);
2279 }
2280 
2281 static inline int
nla_validate_nested(const struct nlattr * start,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)2282 nla_validate_nested(const struct nlattr *start, int maxtype,
2283 		    const struct nla_policy *policy,
2284 		    struct netlink_ext_ack *extack)
2285 {
2286 	return __nla_validate_nested(start, maxtype, policy,
2287 				     NL_VALIDATE_STRICT, extack);
2288 }
2289 
2290 static inline int
nla_validate_nested_deprecated(const struct nlattr * start,int maxtype,const struct nla_policy * policy,struct netlink_ext_ack * extack)2291 nla_validate_nested_deprecated(const struct nlattr *start, int maxtype,
2292 			       const struct nla_policy *policy,
2293 			       struct netlink_ext_ack *extack)
2294 {
2295 	return __nla_validate_nested(start, maxtype, policy,
2296 				     NL_VALIDATE_LIBERAL, extack);
2297 }
2298 
2299 /**
2300  * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute
2301  * @skb: socket buffer the message is stored in
2302  *
2303  * Return: true if padding is needed to align the next attribute (nla_data()) to
2304  * a 64-bit aligned area.
2305  */
nla_need_padding_for_64bit(struct sk_buff * skb)2306 static inline bool nla_need_padding_for_64bit(struct sk_buff *skb)
2307 {
2308 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2309 	/* The nlattr header is 4 bytes in size, that's why we test
2310 	 * if the skb->data _is_ aligned.  A NOP attribute, plus
2311 	 * nlattr header for next attribute, will make nla_data()
2312 	 * 8-byte aligned.
2313 	 */
2314 	if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
2315 		return true;
2316 #endif
2317 	return false;
2318 }
2319 
2320 /**
2321  * nla_align_64bit - 64-bit align the nla_data() of next attribute
2322  * @skb: socket buffer the message is stored in
2323  * @padattr: attribute type for the padding
2324  *
2325  * Conditionally emit a padding netlink attribute in order to make
2326  * the next attribute we emit have a 64-bit aligned nla_data() area.
2327  * This will only be done in architectures which do not have
2328  * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
2329  *
2330  * Returns: zero on success or a negative error code.
2331  */
nla_align_64bit(struct sk_buff * skb,int padattr)2332 static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
2333 {
2334 	if (nla_need_padding_for_64bit(skb) &&
2335 	    !nla_reserve(skb, padattr, 0))
2336 		return -EMSGSIZE;
2337 
2338 	return 0;
2339 }
2340 
2341 /**
2342  * nla_total_size_64bit - total length of attribute including padding
2343  * @payload: length of payload
2344  */
nla_total_size_64bit(int payload)2345 static inline int nla_total_size_64bit(int payload)
2346 {
2347 	return NLA_ALIGN(nla_attr_size(payload))
2348 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2349 		+ NLA_ALIGN(nla_attr_size(0))
2350 #endif
2351 		;
2352 }
2353 
2354 /**
2355  * nla_for_each_attr - iterate over a stream of attributes
2356  * @pos: loop counter, set to current attribute
2357  * @head: head of attribute stream
2358  * @len: length of attribute stream
2359  * @rem: initialized to len, holds bytes currently remaining in stream
2360  */
2361 #define nla_for_each_attr(pos, head, len, rem) \
2362 	for (pos = head, rem = len; \
2363 	     nla_ok(pos, rem); \
2364 	     pos = nla_next(pos, &(rem)))
2365 
2366 /**
2367  * nla_for_each_attr_type - iterate over a stream of attributes
2368  * @pos: loop counter, set to current attribute
2369  * @type: required attribute type for @pos
2370  * @head: head of attribute stream
2371  * @len: length of attribute stream
2372  * @rem: initialized to len, holds bytes currently remaining in stream
2373  */
2374 #define nla_for_each_attr_type(pos, type, head, len, rem) \
2375 	nla_for_each_attr(pos, head, len, rem) \
2376 		if (nla_type(pos) == type)
2377 
2378 /**
2379  * nla_for_each_nested - iterate over nested attributes
2380  * @pos: loop counter, set to current attribute
2381  * @nla: attribute containing the nested attributes
2382  * @rem: initialized to len, holds bytes currently remaining in stream
2383  */
2384 #define nla_for_each_nested(pos, nla, rem) \
2385 	nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
2386 
2387 /**
2388  * nla_for_each_nested_type - iterate over nested attributes
2389  * @pos: loop counter, set to current attribute
2390  * @type: required attribute type for @pos
2391  * @nla: attribute containing the nested attributes
2392  * @rem: initialized to len, holds bytes currently remaining in stream
2393  */
2394 #define nla_for_each_nested_type(pos, type, nla, rem) \
2395 	nla_for_each_nested(pos, nla, rem) \
2396 		if (nla_type(pos) == type)
2397 
2398 /**
2399  * nla_is_last - Test if attribute is last in stream
2400  * @nla: attribute to test
2401  * @rem: bytes remaining in stream
2402  */
nla_is_last(const struct nlattr * nla,int rem)2403 static inline bool nla_is_last(const struct nlattr *nla, int rem)
2404 {
2405 	return nla->nla_len == rem;
2406 }
2407 
2408 void nla_get_range_unsigned(const struct nla_policy *pt,
2409 			    struct netlink_range_validation *range);
2410 void nla_get_range_signed(const struct nla_policy *pt,
2411 			  struct netlink_range_validation_signed *range);
2412 
2413 struct netlink_policy_dump_state;
2414 
2415 int netlink_policy_dump_add_policy(struct netlink_policy_dump_state **pstate,
2416 				   const struct nla_policy *policy,
2417 				   unsigned int maxtype);
2418 int netlink_policy_dump_get_policy_idx(struct netlink_policy_dump_state *state,
2419 				       const struct nla_policy *policy,
2420 				       unsigned int maxtype);
2421 bool netlink_policy_dump_loop(struct netlink_policy_dump_state *state);
2422 int netlink_policy_dump_write(struct sk_buff *skb,
2423 			      struct netlink_policy_dump_state *state);
2424 int netlink_policy_dump_attr_size_estimate(const struct nla_policy *pt);
2425 int netlink_policy_dump_write_attr(struct sk_buff *skb,
2426 				   const struct nla_policy *pt,
2427 				   int nestattr);
2428 void netlink_policy_dump_free(struct netlink_policy_dump_state *state);
2429 
2430 #endif
2431