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