]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - include/net/netlink.h
Merge tag 'spdx-5.2-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[mirror_ubuntu-eoan-kernel.git] / include / net / netlink.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
bfa83a9e
TG
2#ifndef __NET_NETLINK_H
3#define __NET_NETLINK_H
4
5#include <linux/types.h>
6#include <linux/netlink.h>
d7fe0f24 7#include <linux/jiffies.h>
930345ea 8#include <linux/in6.h>
bfa83a9e
TG
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()
1dc8d8c0 40 * nlmsg_end() finalize netlink message
fe4944e5
TG
41 * nlmsg_get_pos() return current position in message
42 * nlmsg_trim() trim part of message
bfa83a9e
TG
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
d387f6ad 49 * nlmsg_notify() send notification message
bfa83a9e
TG
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 *
97676b6b
TG
71 * Misc:
72 * nlmsg_report() report back to application?
73 *
bfa83a9e
TG
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:
d1ec3b77 89 * struct nlattr netlink attribute header
bfa83a9e
TG
90 *
91 * Attribute Construction:
fe4944e5
TG
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
bfa83a9e 94 * nla_put(skb, type, len, data) add attribute to skb
fe4944e5 95 * nla_put_nohdr(skb, len, data) add attribute w/o hdr
01480e1c 96 * nla_append(skb, len, data) append data to skb
bfa83a9e
TG
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
26837012
REB
102 * nla_put_u64_64bit(skb, type,
103 * value, padattr) add u64 attribute to skb
4778e0be
JP
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
756a2f59
ND
107 * nla_put_s64(skb, type, value,
108 * padattr) add s64 attribute to skb
bfa83a9e
TG
109 * nla_put_string(skb, type, str) add string attribute to skb
110 * nla_put_flag(skb, type) add flag attribute to skb
2175d87c
ND
111 * nla_put_msecs(skb, type, jiffies,
112 * padattr) add msecs attribute to skb
930345ea
JB
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
bfa83a9e 115 *
bfa83a9e
TG
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
4778e0be
JP
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
bfa83a9e
TG
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
4fe5d5c0 152 * nla_validate_nested() validate a stream of nested attributes
bfa83a9e 153 * nla_find() find attribute in stream of attributes
fe4944e5 154 * nla_find_nested() find attribute in nested attributes
bfa83a9e 155 * nla_parse() parse and validate stream of attrs
43955a45 156 * nla_parse_nested() parse nested attributes
bfa83a9e 157 * nla_for_each_attr() loop over all attributes
22acb19a 158 * nla_for_each_nested() loop over the nested attributes
bfa83a9e
TG
159 *=========================================================================
160 */
161
162 /**
163 * Standard attribute types to specify validation policy
164 */
165enum {
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,
1501d135 175 NLA_NESTED_ARRAY,
a5531a5d 176 NLA_NUL_STRING,
d30045a0 177 NLA_BINARY,
4778e0be
JP
178 NLA_S8,
179 NLA_S16,
180 NLA_S32,
181 NLA_S64,
64c83d83 182 NLA_BITFIELD32,
568b742a 183 NLA_REJECT,
b60b87fc
JB
184 NLA_EXACT_LEN,
185 NLA_EXACT_LEN_WARN,
6f455f5f 186 NLA_MIN_LEN,
bfa83a9e
TG
187 __NLA_TYPE_MAX,
188};
189
190#define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
191
3e48be05
JB
192enum nla_policy_validation {
193 NLA_VALIDATE_NONE,
194 NLA_VALIDATE_RANGE,
195 NLA_VALIDATE_MIN,
196 NLA_VALIDATE_MAX,
33188bd6 197 NLA_VALIDATE_FUNCTION,
3e48be05
JB
198};
199
bfa83a9e
TG
200/**
201 * struct nla_policy - attribute validation policy
202 * @type: Type of attribute or NLA_UNSPEC
3e48be05 203 * @validation_type: type of attribute validation done in addition to
33188bd6 204 * type-specific validation (e.g. range, function call), see
3e48be05 205 * &enum nla_policy_validation
a5531a5d 206 * @len: Type specific length of payload
bfa83a9e
TG
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 *
a5531a5d
TG
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
d30045a0 215 * NLA_BINARY Maximum length of attribute payload
6f455f5f 216 * NLA_MIN_LEN Minimum length of attribute payload
1501d135
JB
217 * NLA_NESTED,
218 * NLA_NESTED_ARRAY Length verification is done by checking len of
9a659a35
JB
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.
4b6cc728
JB
222 * NLA_U8, NLA_U16,
223 * NLA_U32, NLA_U64,
4778e0be
JP
224 * NLA_S8, NLA_S16,
225 * NLA_S32, NLA_S64,
4b6cc728
JB
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"
568b742a
JB
229 * NLA_BITFIELD32 Unused
230 * NLA_REJECT Unused
b60b87fc
JB
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.
6f455f5f 235 * NLA_MIN_LEN Minimum length of attribute payload
4b6cc728 236 * All other Minimum length of attribute payload
a5531a5d 237 *
568b742a
JB
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.
9a659a35
JB
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.
1501d135
JB
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.
3e48be05
JB
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
568b742a 275 *
33188bd6
JB
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 *
bfa83a9e 283 * Example:
b54452b0 284 * static const struct nla_policy my_policy[ATTR_MAX+1] = {
bfa83a9e 285 * [ATTR_FOO] = { .type = NLA_U16 },
d30045a0 286 * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
6f455f5f 287 * [ATTR_BAZ] = { .type = NLA_EXACT_LEN, .len = sizeof(struct mystruct) },
64c83d83 288 * [ATTR_GOO] = { .type = NLA_BITFIELD32, .validation_data = &myvalidflags },
bfa83a9e
TG
289 * };
290 */
291struct nla_policy {
3e48be05
JB
292 u8 type;
293 u8 validation_type;
a5531a5d 294 u16 len;
3e48be05
JB
295 union {
296 const void *validation_data;
297 struct {
298 s16 min, max;
299 };
33188bd6
JB
300 int (*validate)(const struct nlattr *attr,
301 struct netlink_ext_ack *extack);
56738f46
JB
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;
3e48be05 320 };
bfa83a9e
TG
321};
322
b60b87fc
JB
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 }
6f455f5f 326#define NLA_POLICY_MIN_LEN(_len) { .type = NLA_MIN_LEN, .len = _len }
b60b87fc
JB
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
23323289 331#define _NLA_POLICY_NESTED(maxattr, policy) \
9a659a35 332 { .type = NLA_NESTED, .validation_data = policy, .len = maxattr }
23323289 333#define _NLA_POLICY_NESTED_ARRAY(maxattr, policy) \
1501d135 334 { .type = NLA_NESTED_ARRAY, .validation_data = policy, .len = maxattr }
23323289
JB
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)
9a659a35 339
5886d932 340#define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition))
3e48be05
JB
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)
33188bd6
JB
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)
3e48be05
JB
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
33188bd6
JB
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
4e902c57
TG
378/**
379 * struct nl_info - netlink source information
380 * @nlh: Netlink message header of original request
15e47304 381 * @portid: Netlink PORTID of requesting application
4e902c57
TG
382 */
383struct nl_info {
384 struct nlmsghdr *nlh;
4d1169c1 385 struct net *nl_net;
15e47304 386 u32 portid;
3b1137fe 387 bool skip_notify;
4e902c57
TG
388};
389
8cb08174
JB
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.)
b424e432
MK
404 * @NL_VALIDATE_NESTED: Check that NLA_F_NESTED is set for NLA_NESTED(_ARRAY)
405 * and unset for other policies.
8cb08174
JB
406 */
407enum 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),
b424e432 413 NL_VALIDATE_NESTED = BIT(4),
8cb08174
JB
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 |\
b424e432
MK
421 NL_VALIDATE_STRICT_ATTRS |\
422 NL_VALIDATE_NESTED)
8cb08174 423
4f69053b 424int netlink_rcv_skb(struct sk_buff *skb,
2d4bc933
JB
425 int (*cb)(struct sk_buff *, struct nlmsghdr *,
426 struct netlink_ext_ack *));
4f69053b
JP
427int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
428 unsigned int group, int report, gfp_t flags);
82ace47a 429
8cb08174
JB
430int __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);
433int __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);
4f69053b
JP
436int nla_policy_len(const struct nla_policy *, int);
437struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
438size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize);
2cf0c8b3 439char *nla_strdup(const struct nlattr *nla, gfp_t flags);
4f69053b
JP
440int nla_memcpy(void *dest, const struct nlattr *src, int count);
441int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
442int nla_strcmp(const struct nlattr *nla, const char *str);
443struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
089bf1a6
ND
444struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
445 int attrlen, int padattr);
4f69053b
JP
446void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
447struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
089bf1a6
ND
448struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype,
449 int attrlen, int padattr);
4f69053b
JP
450void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
451void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
452 const void *data);
089bf1a6
ND
453void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
454 const void *data, int padattr);
4f69053b
JP
455void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
456int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);
089bf1a6
ND
457int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
458 const void *data, int padattr);
4f69053b
JP
459int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
460int nla_append(struct sk_buff *skb, int attrlen, const void *data);
bfa83a9e
TG
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 */
470static 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 */
479static 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 */
488static 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
70f23fd6 495 * @nlh: netlink message header
bfa83a9e
TG
496 */
497static 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 */
506static 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 */
516static 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 */
528static 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 */
538static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
539{
619e803d 540 return (remaining >= (int) sizeof(struct nlmsghdr) &&
bfa83a9e
TG
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 */
3654654f
JE
553static inline struct nlmsghdr *
554nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
bfa83a9e
TG
555{
556 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
557
558 *remaining -= totlen;
559
560 return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
561}
562
3de64403
JB
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 */
579static 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
bfa83a9e 588/**
8cb08174
JB
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 */
604static 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 */
629static 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
bfa83a9e
TG
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
8cb08174 646 * @validate: validation strictness
fceb6435 647 * @extack: extended ACK report struct
bfa83a9e
TG
648 *
649 * See nla_parse()
650 */
8cb08174
JB
651static 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)
bfa83a9e 656{
3d0d4337
DA
657 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) {
658 NL_SET_ERR_MSG(extack, "Invalid header length");
bfa83a9e 659 return -EINVAL;
3d0d4337 660 }
bfa83a9e 661
8cb08174
JB
662 return __nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
663 nlmsg_attrlen(nlh, hdrlen), policy, validate,
664 extack);
bfa83a9e
TG
665}
666
3de64403
JB
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 */
678static 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
8cb08174
JB
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 */
698static 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)
a5f6cba2 702{
8cb08174
JB
703 return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
704 NL_VALIDATE_LIBERAL, extack);
705}
a5f6cba2 706
8cb08174
JB
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 */
717static inline int
718nlmsg_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);
a5f6cba2
DA
725}
726
bfa83a9e
TG
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 */
6b8c92ba 735static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
bfa83a9e
TG
736 int hdrlen, int attrtype)
737{
738 return nla_find(nlmsg_attrdata(nlh, hdrlen),
739 nlmsg_attrlen(nlh, hdrlen), attrtype);
740}
741
742/**
8cb08174
JB
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 */
757static 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
3de64403
JB
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 */
781static 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}
8cb08174
JB
788
789/**
790 * nlmsg_validate_deprecated - validate a netlink message including attributes
bfa83a9e
TG
791 * @nlh: netlinket message header
792 * @hdrlen: length of familiy specific header
793 * @maxtype: maximum attribute type to be expected
794 * @policy: validation policy
fceb6435 795 * @extack: extended ACK report struct
bfa83a9e 796 */
8cb08174
JB
797static 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)
bfa83a9e
TG
801{
802 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
803 return -EINVAL;
804
8cb08174
JB
805 return __nla_validate(nlmsg_attrdata(nlh, hdrlen),
806 nlmsg_attrlen(nlh, hdrlen), maxtype,
807 policy, NL_VALIDATE_LIBERAL, extack);
bfa83a9e
TG
808}
809
8cb08174
JB
810
811
97676b6b
TG
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 */
3a6c2b41 818static inline int nlmsg_report(const struct nlmsghdr *nlh)
97676b6b
TG
819{
820 return !!(nlh->nlmsg_flags & NLM_F_ECHO);
821}
822
bfa83a9e
TG
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
bfa83a9e
TG
834/**
835 * nlmsg_put - Add a new netlink message to an skb
836 * @skb: socket buffer to store message in
2c6ba4b1 837 * @portid: netlink PORTID of requesting application
bfa83a9e
TG
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 */
15e47304 846static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
bfa83a9e
TG
847 int type, int payload, int flags)
848{
849 if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
850 return NULL;
851
15e47304 852 return __nlmsg_put(skb, portid, seq, type, payload, flags);
bfa83a9e
TG
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 */
866static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
867 struct netlink_callback *cb,
868 int type, int payload,
869 int flags)
870{
15e47304 871 return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
bfa83a9e
TG
872 type, payload, flags);
873}
874
875/**
876 * nlmsg_new - Allocate a new netlink message
339bf98f 877 * @payload: size of the message payload
fe4944e5 878 * @flags: the type of memory to allocate.
bfa83a9e 879 *
339bf98f
TG
880 * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
881 * and a good default is needed.
bfa83a9e 882 */
339bf98f 883static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
bfa83a9e 884{
339bf98f 885 return alloc_skb(nlmsg_total_size(payload), flags);
bfa83a9e
TG
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.
bfa83a9e 896 */
053c095a 897static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
bfa83a9e 898{
27a884dc 899 nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
bfa83a9e
TG
900}
901
fe4944e5
TG
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 */
908static inline void *nlmsg_get_pos(struct sk_buff *skb)
909{
27a884dc 910 return skb_tail_pointer(skb);
fe4944e5
TG
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 *
bc3ed28c 918 * Trims the message to the provided mark.
fe4944e5 919 */
bc3ed28c 920static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
fe4944e5 921{
149118d8
TG
922 if (mark) {
923 WARN_ON((unsigned char *) mark < skb->data);
fe4944e5 924 skb_trim(skb, (unsigned char *) mark - skb->data);
149118d8 925 }
fe4944e5
TG
926}
927
bfa83a9e
TG
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
bc3ed28c 934 * attributes from the socket buffer again.
bfa83a9e 935 */
bc3ed28c 936static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
bfa83a9e 937{
bc3ed28c 938 nlmsg_trim(skb, nlh);
bfa83a9e
TG
939}
940
941/**
942 * nlmsg_free - free a netlink message
943 * @skb: socket buffer of netlink message
944 */
945static 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
15e47304 954 * @portid: own netlink portid to avoid sending to yourself
bfa83a9e 955 * @group: multicast group id
d387f6ad 956 * @flags: allocation flags
bfa83a9e
TG
957 */
958static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
15e47304 959 u32 portid, unsigned int group, gfp_t flags)
bfa83a9e
TG
960{
961 int err;
962
963 NETLINK_CB(skb).dst_group = group;
964
15e47304 965 err = netlink_broadcast(sk, skb, portid, group, flags);
bfa83a9e
TG
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
15e47304 976 * @portid: netlink portid of the destination socket
bfa83a9e 977 */
15e47304 978static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
bfa83a9e
TG
979{
980 int err;
981
15e47304 982 err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
bfa83a9e
TG
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
670dc283
JB
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 */
1016static inline void
1017nl_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
bfa83a9e
TG
1025/**************************************************************************
1026 * Netlink Attributes
1027 **************************************************************************/
1028
1029/**
1030 * nla_attr_size - length of attribute not including padding
1031 * @payload: length of payload
1032 */
1033static 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 */
1042static 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 */
1051static inline int nla_padlen(int payload)
1052{
1053 return nla_total_size(payload) - nla_attr_size(payload);
1054}
1055
8f4c1f9b
TG
1056/**
1057 * nla_type - attribute type
1058 * @nla: netlink attribute
1059 */
1060static inline int nla_type(const struct nlattr *nla)
1061{
1062 return nla->nla_type & NLA_TYPE_MASK;
1063}
1064
bfa83a9e
TG
1065/**
1066 * nla_data - head of payload
1067 * @nla: netlink attribute
1068 */
1069static 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 */
1078static 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 */
1088static inline int nla_ok(const struct nlattr *nla, int remaining)
1089{
3e1ed981
AD
1090 return remaining >= (int) sizeof(*nla) &&
1091 nla->nla_len >= sizeof(*nla) &&
bfa83a9e
TG
1092 nla->nla_len <= remaining;
1093}
1094
1095/**
d1ec3b77 1096 * nla_next - next netlink attribute in attribute stream
bfa83a9e
TG
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 */
1103static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
1104{
3b2c75d3 1105 unsigned int totlen = NLA_ALIGN(nla->nla_len);
bfa83a9e
TG
1106
1107 *remaining -= totlen;
1108 return (struct nlattr *) ((char *) nla + totlen);
1109}
1110
fe4944e5
TG
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 */
3654654f
JE
1118static inline struct nlattr *
1119nla_find_nested(const struct nlattr *nla, int attrtype)
fe4944e5
TG
1120{
1121 return nla_find(nla_data(nla), nla_len(nla), attrtype);
1122}
1123
3de64403
JB
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 */
1134static 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{
b424e432
MK
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
3de64403
JB
1144 return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
1145 NL_VALIDATE_STRICT, extack);
1146}
1147
bfa83a9e 1148/**
8cb08174 1149 * nla_parse_nested_deprecated - parse nested attributes
bfa83a9e
TG
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
fceb6435 1154 * @extack: extended ACK report struct
bfa83a9e 1155 *
8cb08174 1156 * See nla_parse_deprecated()
bfa83a9e 1157 */
8cb08174
JB
1158static 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)
bfa83a9e 1162{
8cb08174
JB
1163 return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
1164 NL_VALIDATE_LIBERAL, extack);
bfa83a9e 1165}
1092cb21 1166
bfa83a9e 1167/**
d1ec3b77 1168 * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
bfa83a9e
TG
1169 * @skb: socket buffer to add attribute to
1170 * @attrtype: attribute type
1171 * @value: numeric value
1172 */
1173static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
1174{
b4391db4
AB
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);
bfa83a9e
TG
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 */
1187static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
1188{
b4391db4
AB
1189 u16 tmp = value;
1190
1191 return nla_put(skb, attrtype, sizeof(u16), &tmp);
bfa83a9e
TG
1192}
1193
569a8fc3
DM
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 */
1200static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
1201{
b4391db4
AB
1202 __be16 tmp = value;
1203
1204 return nla_put(skb, attrtype, sizeof(__be16), &tmp);
569a8fc3
DM
1205}
1206
6c1dd3b6
DM
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 */
1213static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
1214{
b4391db4
AB
1215 __be16 tmp = value;
1216
1217 return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
6c1dd3b6
DM
1218}
1219
24c410dc
DM
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 */
1226static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
1227{
b4391db4
AB
1228 __le16 tmp = value;
1229
1230 return nla_put(skb, attrtype, sizeof(__le16), &tmp);
24c410dc
DM
1231}
1232
bfa83a9e
TG
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 */
1239static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
1240{
b4391db4
AB
1241 u32 tmp = value;
1242
1243 return nla_put(skb, attrtype, sizeof(u32), &tmp);
bfa83a9e
TG
1244}
1245
1246/**
569a8fc3
DM
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 */
1252static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
1253{
b4391db4
AB
1254 __be32 tmp = value;
1255
1256 return nla_put(skb, attrtype, sizeof(__be32), &tmp);
569a8fc3
DM
1257}
1258
6c1dd3b6
DM
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 */
1265static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
1266{
b4391db4
AB
1267 __be32 tmp = value;
1268
1269 return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
6c1dd3b6
DM
1270}
1271
24c410dc
DM
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 */
1278static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value)
1279{
b4391db4
AB
1280 __le32 tmp = value;
1281
1282 return nla_put(skb, attrtype, sizeof(__le32), &tmp);
24c410dc
DM
1283}
1284
73520786
ND
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 */
1292static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype,
1293 u64 value, int padattr)
1294{
b4391db4
AB
1295 u64 tmp = value;
1296
1297 return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
73520786
ND
1298}
1299
569a8fc3 1300/**
b46f6ded 1301 * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it
569a8fc3
DM
1302 * @skb: socket buffer to add attribute to
1303 * @attrtype: attribute type
1304 * @value: numeric value
b46f6ded 1305 * @padattr: attribute type for the padding
569a8fc3 1306 */
b46f6ded
ND
1307static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value,
1308 int padattr)
569a8fc3 1309{
b4391db4
AB
1310 __be64 tmp = value;
1311
1312 return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr);
569a8fc3
DM
1313}
1314
6c1dd3b6 1315/**
e9bbe898 1316 * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it
6c1dd3b6
DM
1317 * @skb: socket buffer to add attribute to
1318 * @attrtype: attribute type
1319 * @value: numeric value
e9bbe898 1320 * @padattr: attribute type for the padding
6c1dd3b6 1321 */
e9bbe898
ND
1322static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value,
1323 int padattr)
6c1dd3b6 1324{
b4391db4
AB
1325 __be64 tmp = value;
1326
1327 return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp,
e9bbe898 1328 padattr);
6c1dd3b6
DM
1329}
1330
24c410dc 1331/**
e7479122 1332 * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it
24c410dc
DM
1333 * @skb: socket buffer to add attribute to
1334 * @attrtype: attribute type
1335 * @value: numeric value
e7479122 1336 * @padattr: attribute type for the padding
24c410dc 1337 */
e7479122
ND
1338static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value,
1339 int padattr)
24c410dc 1340{
b4391db4
AB
1341 __le64 tmp = value;
1342
1343 return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr);
24c410dc
DM
1344}
1345
4778e0be
JP
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 */
1352static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
1353{
b4391db4
AB
1354 s8 tmp = value;
1355
1356 return nla_put(skb, attrtype, sizeof(s8), &tmp);
4778e0be
JP
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 */
1365static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
1366{
b4391db4
AB
1367 s16 tmp = value;
1368
1369 return nla_put(skb, attrtype, sizeof(s16), &tmp);
4778e0be
JP
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 */
1378static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
1379{
b4391db4
AB
1380 s32 tmp = value;
1381
1382 return nla_put(skb, attrtype, sizeof(s32), &tmp);
4778e0be
JP
1383}
1384
1385/**
756a2f59 1386 * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it
4778e0be
JP
1387 * @skb: socket buffer to add attribute to
1388 * @attrtype: attribute type
1389 * @value: numeric value
756a2f59 1390 * @padattr: attribute type for the padding
4778e0be 1391 */
756a2f59
ND
1392static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value,
1393 int padattr)
4778e0be 1394{
b4391db4
AB
1395 s64 tmp = value;
1396
1397 return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr);
4778e0be
JP
1398}
1399
bfa83a9e
TG
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 */
1406static 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 */
1417static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
1418{
1419 return nla_put(skb, attrtype, 0, NULL);
1420}
1421
1422/**
2175d87c 1423 * nla_put_msecs - Add a msecs netlink attribute to a skb and align it
bfa83a9e
TG
1424 * @skb: socket buffer to add attribute to
1425 * @attrtype: attribute type
d87de1f3 1426 * @njiffies: number of jiffies to convert to msecs
2175d87c 1427 * @padattr: attribute type for the padding
bfa83a9e
TG
1428 */
1429static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
2175d87c 1430 unsigned long njiffies, int padattr)
bfa83a9e 1431{
d87de1f3 1432 u64 tmp = jiffies_to_msecs(njiffies);
2175d87c
ND
1433
1434 return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
bfa83a9e
TG
1435}
1436
930345ea
JB
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 */
1444static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
1445 __be32 addr)
1446{
b4391db4
AB
1447 __be32 tmp = addr;
1448
1449 return nla_put_be32(skb, attrtype, tmp);
930345ea
JB
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 */
1459static 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
bfa83a9e
TG
1465/**
1466 * nla_get_u32 - return payload of u32 attribute
1467 * @nla: u32 netlink attribute
1468 */
b057efd4 1469static inline u32 nla_get_u32(const struct nlattr *nla)
bfa83a9e
TG
1470{
1471 return *(u32 *) nla_data(nla);
1472}
1473
00012e5b
AV
1474/**
1475 * nla_get_be32 - return payload of __be32 attribute
1476 * @nla: __be32 netlink attribute
1477 */
b057efd4 1478static inline __be32 nla_get_be32(const struct nlattr *nla)
00012e5b
AV
1479{
1480 return *(__be32 *) nla_data(nla);
1481}
1482
c648a013
AA
1483/**
1484 * nla_get_le32 - return payload of __le32 attribute
1485 * @nla: __le32 netlink attribute
1486 */
1487static inline __le32 nla_get_le32(const struct nlattr *nla)
1488{
1489 return *(__le32 *) nla_data(nla);
1490}
1491
bfa83a9e
TG
1492/**
1493 * nla_get_u16 - return payload of u16 attribute
1494 * @nla: u16 netlink attribute
1495 */
b057efd4 1496static inline u16 nla_get_u16(const struct nlattr *nla)
bfa83a9e
TG
1497{
1498 return *(u16 *) nla_data(nla);
1499}
1500
838965ba
PM
1501/**
1502 * nla_get_be16 - return payload of __be16 attribute
1503 * @nla: __be16 netlink attribute
1504 */
b057efd4 1505static inline __be16 nla_get_be16(const struct nlattr *nla)
838965ba
PM
1506{
1507 return *(__be16 *) nla_data(nla);
1508}
1509
4a89c256
TG
1510/**
1511 * nla_get_le16 - return payload of __le16 attribute
1512 * @nla: __le16 netlink attribute
1513 */
b057efd4 1514static inline __le16 nla_get_le16(const struct nlattr *nla)
4a89c256
TG
1515{
1516 return *(__le16 *) nla_data(nla);
1517}
1518
bfa83a9e
TG
1519/**
1520 * nla_get_u8 - return payload of u8 attribute
1521 * @nla: u8 netlink attribute
1522 */
b057efd4 1523static inline u8 nla_get_u8(const struct nlattr *nla)
bfa83a9e
TG
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 */
b057efd4 1532static inline u64 nla_get_u64(const struct nlattr *nla)
bfa83a9e
TG
1533{
1534 u64 tmp;
1535
1536 nla_memcpy(&tmp, nla, sizeof(tmp));
1537
1538 return tmp;
1539}
1540
a17c8598
PNA
1541/**
1542 * nla_get_be64 - return payload of __be64 attribute
1543 * @nla: __be64 netlink attribute
1544 */
1545static inline __be64 nla_get_be64(const struct nlattr *nla)
1546{
f5d410f2
PNA
1547 __be64 tmp;
1548
1549 nla_memcpy(&tmp, nla, sizeof(tmp));
1550
4778e0be
JP
1551 return tmp;
1552}
1553
c648a013
AA
1554/**
1555 * nla_get_le64 - return payload of __le64 attribute
1556 * @nla: __le64 netlink attribute
1557 */
1558static inline __le64 nla_get_le64(const struct nlattr *nla)
1559{
1560 return *(__le64 *) nla_data(nla);
1561}
1562
4778e0be
JP
1563/**
1564 * nla_get_s32 - return payload of s32 attribute
1565 * @nla: s32 netlink attribute
1566 */
1567static 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 */
1576static 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 */
1585static 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 */
1594static inline s64 nla_get_s64(const struct nlattr *nla)
1595{
1596 s64 tmp;
1597
1598 nla_memcpy(&tmp, nla, sizeof(tmp));
1599
f5d410f2 1600 return tmp;
a17c8598
PNA
1601}
1602
bfa83a9e
TG
1603/**
1604 * nla_get_flag - return payload of flag attribute
1605 * @nla: flag netlink attribute
1606 */
b057efd4 1607static inline int nla_get_flag(const struct nlattr *nla)
bfa83a9e
TG
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 */
b057efd4 1618static inline unsigned long nla_get_msecs(const struct nlattr *nla)
bfa83a9e
TG
1619{
1620 u64 msecs = nla_get_u64(nla);
1621
1622 return msecs_to_jiffies((unsigned long) msecs);
1623}
1624
67b61f6c
JB
1625/**
1626 * nla_get_in_addr - return payload of IPv4 address attribute
1627 * @nla: IPv4 address netlink attribute
1628 */
1629static 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 */
1638static 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
64c83d83
JHS
1646/**
1647 * nla_get_bitfield32 - return payload of 32 bitfield attribute
1648 * @nla: nla_bitfield32 attribute
1649 */
1650static 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
b15ca182
TG
1658/**
1659 * nla_memdup - duplicate attribute memory (kmemdup)
1660 * @src: netlink attribute to duplicate from
1661 * @gfp: GFP mask
1662 */
1663static inline void *nla_memdup(const struct nlattr *src, gfp_t gfp)
1664{
1665 return kmemdup(nla_data(src), nla_len(src), gfp);
1666}
1667
bfa83a9e 1668/**
ae0be8de 1669 * nla_nest_start_noflag - Start a new level of nested attributes
bfa83a9e
TG
1670 * @skb: socket buffer to add attributes to
1671 * @attrtype: attribute type of container
1672 *
ae0be8de
MK
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
bfa83a9e 1678 */
ae0be8de
MK
1679static inline struct nlattr *nla_nest_start_noflag(struct sk_buff *skb,
1680 int attrtype)
bfa83a9e 1681{
27a884dc 1682 struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
bfa83a9e
TG
1683
1684 if (nla_put(skb, attrtype, 0, NULL) < 0)
1685 return NULL;
1686
1687 return start;
1688}
1689
ae0be8de
MK
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 */
1700static 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
bfa83a9e
TG
1705/**
1706 * nla_nest_end - Finalize nesting of attributes
d1ec3b77 1707 * @skb: socket buffer the attributes are stored in
bfa83a9e
TG
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 */
1715static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
1716{
27a884dc 1717 start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
bfa83a9e
TG
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
bc3ed28c 1727 * attributes. Returns -EMSGSIZE
bfa83a9e 1728 */
bc3ed28c 1729static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
bfa83a9e 1730{
bc3ed28c 1731 nlmsg_trim(skb, start);
bfa83a9e
TG
1732}
1733
4fe5d5c0
PM
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
8cb08174 1739 * @validate: validation strictness
fceb6435 1740 * @extack: extended ACK report struct
4fe5d5c0
PM
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 */
8cb08174
JB
1748static 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
1757static inline int
1758nla_validate_nested_deprecated(const struct nlattr *start, int maxtype,
1759 const struct nla_policy *policy,
1760 struct netlink_ext_ack *extack)
4fe5d5c0 1761{
8cb08174
JB
1762 return __nla_validate_nested(start, maxtype, policy,
1763 NL_VALIDATE_LIBERAL, extack);
4fe5d5c0
PM
1764}
1765
089bf1a6
ND
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 */
1773static 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
35c58459
DM
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
cca1d815 1795 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
35c58459
DM
1796 *
1797 * Returns zero on success or a negative error code.
1798 */
1799static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
1800{
089bf1a6 1801 if (nla_need_padding_for_64bit(skb) &&
cca1d815
ED
1802 !nla_reserve(skb, padattr, 0))
1803 return -EMSGSIZE;
089bf1a6 1804
35c58459
DM
1805 return 0;
1806}
1807
1808/**
1809 * nla_total_size_64bit - total length of attribute including padding
1810 * @payload: length of payload
1811 */
1812static inline int nla_total_size_64bit(int payload)
1813{
1814 return NLA_ALIGN(nla_attr_size(payload))
cca1d815 1815#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
35c58459
DM
1816 + NLA_ALIGN(nla_attr_size(0))
1817#endif
1818 ;
1819}
1820
bfa83a9e
TG
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
fe4944e5
TG
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
941d8ebc
SH
1842/**
1843 * nla_is_last - Test if attribute is last in stream
1844 * @nla: attribute to test
1845 * @rem: bytes remaining in stream
1846 */
1847static inline bool nla_is_last(const struct nlattr *nla, int rem)
1848{
1849 return nla->nla_len == rem;
1850}
1851
bfa83a9e 1852#endif