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