Merge tag 'mm-stable-2024-05-17-19-19' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.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 43 * nlmsg_cancel() cancel message construction
8e69b345
JK
44 * nlmsg_consume() free a netlink message (expected)
45 * nlmsg_free() free a netlink message (drop)
bfa83a9e
TG
46 *
47 * Message Sending:
48 * nlmsg_multicast() multicast message to several groups
49 * nlmsg_unicast() unicast a message to a single socket
d387f6ad 50 * nlmsg_notify() send notification message
bfa83a9e
TG
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 *
97676b6b
TG
72 * Misc:
73 * nlmsg_report() report back to application?
74 *
bfa83a9e
TG
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:
d1ec3b77 90 * struct nlattr netlink attribute header
bfa83a9e
TG
91 *
92 * Attribute Construction:
fe4944e5
TG
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
bfa83a9e 95 * nla_put(skb, type, len, data) add attribute to skb
fe4944e5 96 * nla_put_nohdr(skb, len, data) add attribute w/o hdr
01480e1c 97 * nla_append(skb, len, data) append data to skb
bfa83a9e
TG
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
26837012
REB
103 * nla_put_u64_64bit(skb, type,
104 * value, padattr) add u64 attribute to skb
4778e0be
JP
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
756a2f59
ND
108 * nla_put_s64(skb, type, value,
109 * padattr) add s64 attribute to skb
bfa83a9e
TG
110 * nla_put_string(skb, type, str) add string attribute to skb
111 * nla_put_flag(skb, type) add flag attribute to skb
2175d87c
ND
112 * nla_put_msecs(skb, type, jiffies,
113 * padattr) add msecs attribute to skb
930345ea
JB
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
bfa83a9e 116 *
bfa83a9e
TG
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:
374d345d
JK
132 * nla_get_uint(nla) get payload for a uint attribute
133 * nla_get_sint(nla) get payload for a sint attribute
bfa83a9e
TG
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
4778e0be
JP
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
bfa83a9e
TG
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
872f6903 148 * nla_strscpy(dst, nla, size) copy attribute to a sized string
bfa83a9e
TG
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
4fe5d5c0 155 * nla_validate_nested() validate a stream of nested attributes
bfa83a9e 156 * nla_find() find attribute in stream of attributes
fe4944e5 157 * nla_find_nested() find attribute in nested attributes
bfa83a9e 158 * nla_parse() parse and validate stream of attrs
43955a45 159 * nla_parse_nested() parse nested attributes
bfa83a9e 160 * nla_for_each_attr() loop over all attributes
e8058a49
JB
161 * nla_for_each_attr_type() loop over all attributes with the
162 * given type
22acb19a 163 * nla_for_each_nested() loop over the nested attributes
e8058a49
JB
164 * nla_for_each_nested_type() loop over the nested attributes with
165 * the given type
bfa83a9e
TG
166 *=========================================================================
167 */
168
169 /**
170 * Standard attribute types to specify validation policy
171 */
172enum {
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,
1501d135 182 NLA_NESTED_ARRAY,
a5531a5d 183 NLA_NUL_STRING,
d30045a0 184 NLA_BINARY,
4778e0be
JP
185 NLA_S8,
186 NLA_S16,
187 NLA_S32,
188 NLA_S64,
64c83d83 189 NLA_BITFIELD32,
568b742a 190 NLA_REJECT,
ecaf75ff
FW
191 NLA_BE16,
192 NLA_BE32,
374d345d
JK
193 NLA_SINT,
194 NLA_UINT,
bfa83a9e
TG
195 __NLA_TYPE_MAX,
196};
197
198#define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
199
d06a09b9
JB
200struct netlink_range_validation {
201 u64 min, max;
202};
203
204struct netlink_range_validation_signed {
205 s64 min, max;
206};
207
3e48be05
JB
208enum nla_policy_validation {
209 NLA_VALIDATE_NONE,
210 NLA_VALIDATE_RANGE,
8aa26c57 211 NLA_VALIDATE_RANGE_WARN_TOO_LONG,
3e48be05
JB
212 NLA_VALIDATE_MIN,
213 NLA_VALIDATE_MAX,
bdbb4e29 214 NLA_VALIDATE_MASK,
d06a09b9 215 NLA_VALIDATE_RANGE_PTR,
33188bd6 216 NLA_VALIDATE_FUNCTION,
3e48be05
JB
217};
218
bfa83a9e
TG
219/**
220 * struct nla_policy - attribute validation policy
221 * @type: Type of attribute or NLA_UNSPEC
3e48be05 222 * @validation_type: type of attribute validation done in addition to
33188bd6 223 * type-specific validation (e.g. range, function call), see
3e48be05 224 * &enum nla_policy_validation
a5531a5d 225 * @len: Type specific length of payload
bfa83a9e
TG
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 *
a5531a5d
TG
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
d30045a0 234 * NLA_BINARY Maximum length of attribute payload
8aa26c57 235 * (but see also below with the validation type)
1501d135
JB
236 * NLA_NESTED,
237 * NLA_NESTED_ARRAY Length verification is done by checking len of
9a659a35 238 * nested header (or empty); len field is used if
47a1494b 239 * nested_policy is also used, for the max attr
9a659a35 240 * number in the nested policy.
374d345d 241 * NLA_SINT, NLA_UINT,
4b6cc728
JB
242 * NLA_U8, NLA_U16,
243 * NLA_U32, NLA_U64,
4778e0be
JP
244 * NLA_S8, NLA_S16,
245 * NLA_S32, NLA_S64,
ecaf75ff 246 * NLA_BE16, NLA_BE32,
4b6cc728
JB
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"
568b742a
JB
250 * NLA_BITFIELD32 Unused
251 * NLA_REJECT Unused
4b6cc728 252 * All other Minimum length of attribute payload
a5531a5d 253 *
47a1494b 254 * Meaning of validation union:
568b742a 255 * NLA_BITFIELD32 This is a 32-bit bitmap/bitselector attribute and
47a1494b
JB
256 * `bitfield32_valid' is the u32 value of valid flags
257 * NLA_REJECT This attribute is always rejected and `reject_message'
568b742a
JB
258 * may point to a string to report as the error instead
259 * of the generic one in extended ACK.
47a1494b
JB
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.
9a659a35
JB
263 * Note that nla_parse() will validate, but of course not
264 * parse, the nested sub-policies.
47a1494b
JB
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.
374d345d 273 * NLA_UINT,
3e48be05
JB
274 * NLA_U8,
275 * NLA_U16,
276 * NLA_U32,
277 * NLA_U64,
ecaf75ff
FW
278 * NLA_BE16,
279 * NLA_BE32,
374d345d 280 * NLA_SINT,
3e48be05
JB
281 * NLA_S8,
282 * NLA_S16,
283 * NLA_S32,
47a1494b
JB
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
3e48be05
JB
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
70eb3911
JB
291 * of s16 - do that using the NLA_POLICY_FULL_RANGE()
292 * or NLA_POLICY_FULL_RANGE_SIGNED() macros instead.
47a1494b
JB
293 * Use the NLA_POLICY_MIN(), NLA_POLICY_MAX() and
294 * NLA_POLICY_RANGE() macros.
374d345d 295 * NLA_UINT,
d06a09b9
JB
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().
374d345d 304 * NLA_SINT,
d06a09b9
JB
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().
8aa26c57
JB
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 *
3e48be05 318 * All other Unused - but note that it's a union
568b742a 319 *
33188bd6 320 * Meaning of `validate' field, use via NLA_POLICY_VALIDATE_FN:
47a1494b 321 * NLA_BINARY Validation function called for the attribute.
33188bd6
JB
322 * All other Unused - but note that it's a union
323 *
bfa83a9e 324 * Example:
47a1494b
JB
325 *
326 * static const u32 myvalidflags = 0xff231023;
327 *
b54452b0 328 * static const struct nla_policy my_policy[ATTR_MAX+1] = {
bfa83a9e 329 * [ATTR_FOO] = { .type = NLA_U16 },
d30045a0 330 * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
8aa26c57 331 * [ATTR_BAZ] = NLA_POLICY_EXACT_LEN(sizeof(struct mystruct)),
47a1494b 332 * [ATTR_GOO] = NLA_POLICY_BITFIELD32(myvalidflags),
bfa83a9e
TG
333 * };
334 */
335struct nla_policy {
3e48be05
JB
336 u8 type;
337 u8 validation_type;
a5531a5d 338 u16 len;
3e48be05 339 union {
7354c902
JK
340 /**
341 * @strict_start_type: first attribute to validate strictly
342 *
343 * This entry is special, and used for the attribute at index 0
56738f46
JB
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
8aa26c57
JB
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.
56738f46
JB
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;
7354c902
JK
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;
ea23fbd2
JK
368 const struct netlink_range_validation *range;
369 const struct netlink_range_validation_signed *range_signed;
7354c902
JK
370 struct {
371 s16 min, max;
7354c902
JK
372 };
373 int (*validate)(const struct nlattr *attr,
374 struct netlink_ext_ack *extack);
3e48be05 375 };
bfa83a9e
TG
376};
377
b60b87fc
JB
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
23323289 381#define _NLA_POLICY_NESTED(maxattr, policy) \
47a1494b 382 { .type = NLA_NESTED, .nested_policy = policy, .len = maxattr }
23323289 383#define _NLA_POLICY_NESTED_ARRAY(maxattr, policy) \
47a1494b 384 { .type = NLA_NESTED_ARRAY, .nested_policy = policy, .len = maxattr }
23323289
JB
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)
47a1494b
JB
389#define NLA_POLICY_BITFIELD32(valid) \
390 { .type = NLA_BITFIELD32, .bitfield32_valid = valid }
9a659a35 391
5fac9b7c
FW
392#define __NLA_IS_UINT_TYPE(tp) \
393 (tp == NLA_U8 || tp == NLA_U16 || tp == NLA_U32 || \
374d345d
JK
394 tp == NLA_U64 || tp == NLA_UINT || \
395 tp == NLA_BE16 || tp == NLA_BE32)
ddcf3b70 396#define __NLA_IS_SINT_TYPE(tp) \
374d345d
JK
397 (tp == NLA_S8 || tp == NLA_S16 || tp == NLA_S32 || tp == NLA_S64 || \
398 tp == NLA_SINT)
ddcf3b70 399
5886d932 400#define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition))
bdbb4e29
JK
401#define NLA_ENSURE_UINT_TYPE(tp) \
402 (__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp)) + tp)
8aa26c57 403#define NLA_ENSURE_UINT_OR_BINARY_TYPE(tp) \
ddcf3b70 404 (__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) || \
8aa26c57
JB
405 tp == NLA_MSECS || \
406 tp == NLA_BINARY) + tp)
d06a09b9 407#define NLA_ENSURE_SINT_TYPE(tp) \
ddcf3b70 408 (__NLA_ENSURE(__NLA_IS_SINT_TYPE(tp)) + tp)
8aa26c57 409#define NLA_ENSURE_INT_OR_BINARY_TYPE(tp) \
ddcf3b70
JK
410 (__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) || \
411 __NLA_IS_SINT_TYPE(tp) || \
8aa26c57
JB
412 tp == NLA_MSECS || \
413 tp == NLA_BINARY) + tp)
33188bd6
JB
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)
3e48be05
JB
419
420#define NLA_POLICY_RANGE(tp, _min, _max) { \
8aa26c57 421 .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp), \
3e48be05
JB
422 .validation_type = NLA_VALIDATE_RANGE, \
423 .min = _min, \
424 .max = _max \
425}
426
d06a09b9 427#define NLA_POLICY_FULL_RANGE(tp, _range) { \
8aa26c57 428 .type = NLA_ENSURE_UINT_OR_BINARY_TYPE(tp), \
d06a09b9
JB
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
3e48be05 439#define NLA_POLICY_MIN(tp, _min) { \
8aa26c57 440 .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp), \
3e48be05
JB
441 .validation_type = NLA_VALIDATE_MIN, \
442 .min = _min, \
443}
444
445#define NLA_POLICY_MAX(tp, _max) { \
8aa26c57 446 .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp), \
3e48be05
JB
447 .validation_type = NLA_VALIDATE_MAX, \
448 .max = _max, \
449}
450
bdbb4e29
JK
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
33188bd6
JB
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
8aa26c57
JB
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
4e902c57
TG
473/**
474 * struct nl_info - netlink source information
475 * @nlh: Netlink message header of original request
3de205cd 476 * @nl_net: Network namespace
15e47304 477 * @portid: Netlink PORTID of requesting application
3de205cd 478 * @skip_notify: Skip netlink notifications to user space
c82481f7 479 * @skip_notify_kernel: Skip selected in-kernel notifications
4e902c57
TG
480 */
481struct nl_info {
482 struct nlmsghdr *nlh;
4d1169c1 483 struct net *nl_net;
15e47304 484 u32 portid;
c82481f7
IS
485 u8 skip_notify:1,
486 skip_notify_kernel:1;
4e902c57
TG
487};
488
8cb08174
JB
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.)
b424e432
MK
503 * @NL_VALIDATE_NESTED: Check that NLA_F_NESTED is set for NLA_NESTED(_ARRAY)
504 * and unset for other policies.
8cb08174
JB
505 */
506enum 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),
b424e432 512 NL_VALIDATE_NESTED = BIT(4),
8cb08174
JB
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 |\
b424e432
MK
520 NL_VALIDATE_STRICT_ATTRS |\
521 NL_VALIDATE_NESTED)
8cb08174 522
4f69053b 523int netlink_rcv_skb(struct sk_buff *skb,
2d4bc933
JB
524 int (*cb)(struct sk_buff *, struct nlmsghdr *,
525 struct netlink_ext_ack *));
4f69053b
JP
526int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
527 unsigned int group, int report, gfp_t flags);
82ace47a 528
8cb08174
JB
529int __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);
532int __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);
4f69053b
JP
535int nla_policy_len(const struct nla_policy *, int);
536struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
872f6903 537ssize_t nla_strscpy(char *dst, const struct nlattr *nla, size_t dstsize);
2cf0c8b3 538char *nla_strdup(const struct nlattr *nla, gfp_t flags);
4f69053b
JP
539int nla_memcpy(void *dest, const struct nlattr *src, int count);
540int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
541int nla_strcmp(const struct nlattr *nla, const char *str);
542struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
089bf1a6
ND
543struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
544 int attrlen, int padattr);
4f69053b
JP
545void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
546struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
089bf1a6
ND
547struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype,
548 int attrlen, int padattr);
4f69053b
JP
549void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
550void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
551 const void *data);
089bf1a6
ND
552void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
553 const void *data, int padattr);
4f69053b
JP
554void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
555int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);
089bf1a6
ND
556int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
557 const void *data, int padattr);
4f69053b
JP
558int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
559int nla_append(struct sk_buff *skb, int attrlen, const void *data);
bfa83a9e
TG
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 */
569static 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 */
578static 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 */
587static 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
70f23fd6 594 * @nlh: netlink message header
bfa83a9e
TG
595 */
596static 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 */
605static 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 */
615static 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 */
627static 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 */
637static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
638{
619e803d 639 return (remaining >= (int) sizeof(struct nlmsghdr) &&
bfa83a9e
TG
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 */
3654654f
JE
652static inline struct nlmsghdr *
653nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
bfa83a9e
TG
654{
655 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
656
657 *remaining -= totlen;
658
659 return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
660}
661
3de64403
JB
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 */
678static 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
bfa83a9e 687/**
8cb08174
JB
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 */
703static 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 */
728static 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
bfa83a9e
TG
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
8cb08174 745 * @validate: validation strictness
fceb6435 746 * @extack: extended ACK report struct
bfa83a9e
TG
747 *
748 * See nla_parse()
749 */
8cb08174
JB
750static 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)
bfa83a9e 755{
3d0d4337
DA
756 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) {
757 NL_SET_ERR_MSG(extack, "Invalid header length");
bfa83a9e 758 return -EINVAL;
3d0d4337 759 }
bfa83a9e 760
8cb08174
JB
761 return __nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
762 nlmsg_attrlen(nlh, hdrlen), policy, validate,
763 extack);
bfa83a9e
TG
764}
765
3de64403
JB
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
0bf73255 772 * @policy: validation policy
3de64403
JB
773 * @extack: extended ACK report struct
774 *
775 * See nla_parse()
776 */
777static 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{
d00ee64e
DA
782 return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
783 NL_VALIDATE_STRICT, extack);
3de64403
JB
784}
785
8cb08174
JB
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
0bf73255 792 * @policy: validation policy
8cb08174
JB
793 * @extack: extended ACK report struct
794 *
795 * See nla_parse_deprecated()
796 */
797static 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)
a5f6cba2 801{
8cb08174
JB
802 return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy,
803 NL_VALIDATE_LIBERAL, extack);
804}
a5f6cba2 805
8cb08174
JB
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
0bf73255 812 * @policy: validation policy
8cb08174
JB
813 * @extack: extended ACK report struct
814 *
815 * See nla_parse_deprecated_strict()
816 */
817static inline int
818nlmsg_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);
a5f6cba2
DA
825}
826
bfa83a9e
TG
827/**
828 * nlmsg_find_attr - find a specific attribute in a netlink message
829 * @nlh: netlink message header
830 * @hdrlen: length of familiy specific header
831 * @attrtype: type of attribute to look for
832 *
833 * Returns the first attribute which matches the specified type.
834 */
6b8c92ba 835static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
bfa83a9e
TG
836 int hdrlen, int attrtype)
837{
838 return nla_find(nlmsg_attrdata(nlh, hdrlen),
839 nlmsg_attrlen(nlh, hdrlen), attrtype);
840}
841
842/**
8cb08174
JB
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
8cb08174
JB
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 documenation of struct nla_policy for more details.
853 *
854 * Returns 0 on success or a negative error code.
855 */
856static 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
3de64403
JB
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
3de64403
JB
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 documenation of struct nla_policy for more details.
876 *
877 * Returns 0 on success or a negative error code.
878 */
879static 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}
8cb08174
JB
886
887/**
888 * nlmsg_validate_deprecated - validate a netlink message including attributes
bfa83a9e
TG
889 * @nlh: netlinket message header
890 * @hdrlen: length of familiy specific header
891 * @maxtype: maximum attribute type to be expected
892 * @policy: validation policy
fceb6435 893 * @extack: extended ACK report struct
bfa83a9e 894 */
8cb08174
JB
895static 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)
bfa83a9e
TG
899{
900 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
901 return -EINVAL;
902
8cb08174
JB
903 return __nla_validate(nlmsg_attrdata(nlh, hdrlen),
904 nlmsg_attrlen(nlh, hdrlen), maxtype,
905 policy, NL_VALIDATE_LIBERAL, extack);
bfa83a9e
TG
906}
907
8cb08174
JB
908
909
97676b6b
TG
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 */
3a6c2b41 916static inline int nlmsg_report(const struct nlmsghdr *nlh)
97676b6b 917{
f9b282b3 918 return nlh ? !!(nlh->nlmsg_flags & NLM_F_ECHO) : 0;
97676b6b
TG
919}
920
1d997f10
HL
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 */
927static inline u32 nlmsg_seq(const struct nlmsghdr *nlh)
928{
929 return nlh ? nlh->nlmsg_seq : 0;
930}
931
bfa83a9e
TG
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 familiy 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
bfa83a9e
TG
943/**
944 * nlmsg_put - Add a new netlink message to an skb
945 * @skb: socket buffer to store message in
2c6ba4b1 946 * @portid: netlink PORTID of requesting application
bfa83a9e
TG
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 */
15e47304 955static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
bfa83a9e
TG
956 int type, int payload, int flags)
957{
958 if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
959 return NULL;
960
15e47304 961 return __nlmsg_put(skb, portid, seq, type, payload, flags);
bfa83a9e
TG
962}
963
738136a0
JK
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 */
974static 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
bfa83a9e
TG
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 */
996static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
997 struct netlink_callback *cb,
998 int type, int payload,
999 int flags)
1000{
15e47304 1001 return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
bfa83a9e
TG
1002 type, payload, flags);
1003}
1004
1005/**
1006 * nlmsg_new - Allocate a new netlink message
339bf98f 1007 * @payload: size of the message payload
fe4944e5 1008 * @flags: the type of memory to allocate.
bfa83a9e 1009 *
339bf98f
TG
1010 * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
1011 * and a good default is needed.
bfa83a9e 1012 */
339bf98f 1013static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
bfa83a9e 1014{
339bf98f 1015 return alloc_skb(nlmsg_total_size(payload), flags);
bfa83a9e
TG
1016}
1017
ac40916a
LR
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 */
1027static inline struct sk_buff *nlmsg_new_large(size_t payload)
1028{
1029 return netlink_alloc_large_skb(nlmsg_total_size(payload), 0);
1030}
1031
bfa83a9e
TG
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 appeneded
1038 * attributes. Only necessary if attributes have been added to
1039 * the message.
bfa83a9e 1040 */
053c095a 1041static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
bfa83a9e 1042{
27a884dc 1043 nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
bfa83a9e
TG
1044}
1045
fe4944e5
TG
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 */
1052static inline void *nlmsg_get_pos(struct sk_buff *skb)
1053{
27a884dc 1054 return skb_tail_pointer(skb);
fe4944e5
TG
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 *
bc3ed28c 1062 * Trims the message to the provided mark.
fe4944e5 1063 */
bc3ed28c 1064static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
fe4944e5 1065{
149118d8
TG
1066 if (mark) {
1067 WARN_ON((unsigned char *) mark < skb->data);
fe4944e5 1068 skb_trim(skb, (unsigned char *) mark - skb->data);
149118d8 1069 }
fe4944e5
TG
1070}
1071
bfa83a9e
TG
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
bc3ed28c 1078 * attributes from the socket buffer again.
bfa83a9e 1079 */
bc3ed28c 1080static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
bfa83a9e 1081{
bc3ed28c 1082 nlmsg_trim(skb, nlh);
bfa83a9e
TG
1083}
1084
1085/**
8e69b345 1086 * nlmsg_free - drop a netlink message
bfa83a9e
TG
1087 * @skb: socket buffer of netlink message
1088 */
1089static inline void nlmsg_free(struct sk_buff *skb)
1090{
1091 kfree_skb(skb);
1092}
1093
8e69b345
JK
1094/**
1095 * nlmsg_consume - free a netlink message
1096 * @skb: socket buffer of netlink message
1097 */
1098static inline void nlmsg_consume(struct sk_buff *skb)
1099{
1100 consume_skb(skb);
1101}
1102
bfa83a9e 1103/**
971b4ad8 1104 * nlmsg_multicast_filtered - multicast a netlink message with filter function
bfa83a9e
TG
1105 * @sk: netlink socket to spread messages to
1106 * @skb: netlink message as socket buffer
15e47304 1107 * @portid: own netlink portid to avoid sending to yourself
bfa83a9e 1108 * @group: multicast group id
d387f6ad 1109 * @flags: allocation flags
971b4ad8
JP
1110 * @filter: filter function
1111 * @filter_data: filter function private data
1112 *
1113 * Return: 0 on success, negative error code for failure.
bfa83a9e 1114 */
971b4ad8
JP
1115static 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)
bfa83a9e
TG
1120{
1121 int err;
1122
1123 NETLINK_CB(skb).dst_group = group;
1124
971b4ad8
JP
1125 err = netlink_broadcast_filtered(sk, skb, portid, group, flags,
1126 filter, filter_data);
bfa83a9e
TG
1127 if (err > 0)
1128 err = 0;
1129
1130 return err;
1131}
1132
971b4ad8
JP
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 */
1141static 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
bfa83a9e
TG
1148/**
1149 * nlmsg_unicast - unicast a netlink message
1150 * @sk: netlink socket to spread message to
1151 * @skb: netlink message as socket buffer
15e47304 1152 * @portid: netlink portid of the destination socket
bfa83a9e 1153 */
15e47304 1154static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
bfa83a9e
TG
1155{
1156 int err;
1157
15e47304 1158 err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
bfa83a9e
TG
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
670dc283
JB
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 */
1192static inline void
1193nl_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
bfa83a9e
TG
1201/**************************************************************************
1202 * Netlink Attributes
1203 **************************************************************************/
1204
1205/**
1206 * nla_attr_size - length of attribute not including padding
1207 * @payload: length of payload
1208 */
1209static 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 */
1218static 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 */
1227static inline int nla_padlen(int payload)
1228{
1229 return nla_total_size(payload) - nla_attr_size(payload);
1230}
1231
8f4c1f9b
TG
1232/**
1233 * nla_type - attribute type
1234 * @nla: netlink attribute
1235 */
1236static inline int nla_type(const struct nlattr *nla)
1237{
1238 return nla->nla_type & NLA_TYPE_MASK;
1239}
1240
bfa83a9e
TG
1241/**
1242 * nla_data - head of payload
1243 * @nla: netlink attribute
1244 */
1245static 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 */
172db56d 1254static inline u16 nla_len(const struct nlattr *nla)
bfa83a9e
TG
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 */
1264static inline int nla_ok(const struct nlattr *nla, int remaining)
1265{
3e1ed981
AD
1266 return remaining >= (int) sizeof(*nla) &&
1267 nla->nla_len >= sizeof(*nla) &&
bfa83a9e
TG
1268 nla->nla_len <= remaining;
1269}
1270
1271/**
d1ec3b77 1272 * nla_next - next netlink attribute in attribute stream
bfa83a9e
TG
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 */
1279static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
1280{
3b2c75d3 1281 unsigned int totlen = NLA_ALIGN(nla->nla_len);
bfa83a9e
TG
1282
1283 *remaining -= totlen;
1284 return (struct nlattr *) ((char *) nla + totlen);
1285}
1286
fe4944e5
TG
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 */
3654654f
JE
1294static inline struct nlattr *
1295nla_find_nested(const struct nlattr *nla, int attrtype)
fe4944e5
TG
1296{
1297 return nla_find(nla_data(nla), nla_len(nla), attrtype);
1298}
1299
3de64403
JB
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 */
1310static 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{
b424e432
MK
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
3de64403
JB
1320 return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
1321 NL_VALIDATE_STRICT, extack);
1322}
1323
bfa83a9e 1324/**
8cb08174 1325 * nla_parse_nested_deprecated - parse nested attributes
bfa83a9e
TG
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
fceb6435 1330 * @extack: extended ACK report struct
bfa83a9e 1331 *
8cb08174 1332 * See nla_parse_deprecated()
bfa83a9e 1333 */
8cb08174
JB
1334static 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)
bfa83a9e 1338{
8cb08174
JB
1339 return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy,
1340 NL_VALIDATE_LIBERAL, extack);
bfa83a9e 1341}
1092cb21 1342
bfa83a9e 1343/**
d1ec3b77 1344 * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
bfa83a9e
TG
1345 * @skb: socket buffer to add attribute to
1346 * @attrtype: attribute type
1347 * @value: numeric value
1348 */
1349static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
1350{
b4391db4
AB
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);
bfa83a9e
TG
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 */
1363static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
1364{
b4391db4
AB
1365 u16 tmp = value;
1366
1367 return nla_put(skb, attrtype, sizeof(u16), &tmp);
bfa83a9e
TG
1368}
1369
569a8fc3
DM
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 */
1376static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
1377{
b4391db4
AB
1378 __be16 tmp = value;
1379
1380 return nla_put(skb, attrtype, sizeof(__be16), &tmp);
569a8fc3
DM
1381}
1382
6c1dd3b6
DM
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 */
1389static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
1390{
b4391db4
AB
1391 __be16 tmp = value;
1392
1393 return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
6c1dd3b6
DM
1394}
1395
24c410dc
DM
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 */
1402static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
1403{
b4391db4
AB
1404 __le16 tmp = value;
1405
1406 return nla_put(skb, attrtype, sizeof(__le16), &tmp);
24c410dc
DM
1407}
1408
bfa83a9e
TG
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 */
1415static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
1416{
b4391db4
AB
1417 u32 tmp = value;
1418
1419 return nla_put(skb, attrtype, sizeof(u32), &tmp);
bfa83a9e
TG
1420}
1421
374d345d
JK
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 */
1428static 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
bfa83a9e 1438/**
569a8fc3
DM
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 */
1444static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
1445{
b4391db4
AB
1446 __be32 tmp = value;
1447
1448 return nla_put(skb, attrtype, sizeof(__be32), &tmp);
569a8fc3
DM
1449}
1450
6c1dd3b6
DM
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 */
1457static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
1458{
b4391db4
AB
1459 __be32 tmp = value;
1460
1461 return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp);
6c1dd3b6
DM
1462}
1463
24c410dc
DM
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 */
1470static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value)
1471{
b4391db4
AB
1472 __le32 tmp = value;
1473
1474 return nla_put(skb, attrtype, sizeof(__le32), &tmp);
24c410dc
DM
1475}
1476
73520786
ND
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 */
1484static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype,
1485 u64 value, int padattr)
1486{
b4391db4
AB
1487 u64 tmp = value;
1488
1489 return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
73520786
ND
1490}
1491
569a8fc3 1492/**
b46f6ded 1493 * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it
569a8fc3
DM
1494 * @skb: socket buffer to add attribute to
1495 * @attrtype: attribute type
1496 * @value: numeric value
b46f6ded 1497 * @padattr: attribute type for the padding
569a8fc3 1498 */
b46f6ded
ND
1499static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value,
1500 int padattr)
569a8fc3 1501{
b4391db4
AB
1502 __be64 tmp = value;
1503
1504 return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr);
569a8fc3
DM
1505}
1506
6c1dd3b6 1507/**
e9bbe898 1508 * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it
6c1dd3b6
DM
1509 * @skb: socket buffer to add attribute to
1510 * @attrtype: attribute type
1511 * @value: numeric value
e9bbe898 1512 * @padattr: attribute type for the padding
6c1dd3b6 1513 */
e9bbe898
ND
1514static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value,
1515 int padattr)
6c1dd3b6 1516{
b4391db4
AB
1517 __be64 tmp = value;
1518
1519 return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp,
e9bbe898 1520 padattr);
6c1dd3b6
DM
1521}
1522
24c410dc 1523/**
e7479122 1524 * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it
24c410dc
DM
1525 * @skb: socket buffer to add attribute to
1526 * @attrtype: attribute type
1527 * @value: numeric value
e7479122 1528 * @padattr: attribute type for the padding
24c410dc 1529 */
e7479122
ND
1530static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value,
1531 int padattr)
24c410dc 1532{
b4391db4
AB
1533 __le64 tmp = value;
1534
1535 return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr);
24c410dc
DM
1536}
1537
4778e0be
JP
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 */
1544static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
1545{
b4391db4
AB
1546 s8 tmp = value;
1547
1548 return nla_put(skb, attrtype, sizeof(s8), &tmp);
4778e0be
JP
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 */
1557static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
1558{
b4391db4
AB
1559 s16 tmp = value;
1560
1561 return nla_put(skb, attrtype, sizeof(s16), &tmp);
4778e0be
JP
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 */
1570static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
1571{
b4391db4
AB
1572 s32 tmp = value;
1573
1574 return nla_put(skb, attrtype, sizeof(s32), &tmp);
4778e0be
JP
1575}
1576
1577/**
756a2f59 1578 * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it
4778e0be
JP
1579 * @skb: socket buffer to add attribute to
1580 * @attrtype: attribute type
1581 * @value: numeric value
756a2f59 1582 * @padattr: attribute type for the padding
4778e0be 1583 */
756a2f59
ND
1584static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value,
1585 int padattr)
4778e0be 1586{
b4391db4
AB
1587 s64 tmp = value;
1588
1589 return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr);
4778e0be
JP
1590}
1591
374d345d
JK
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 */
1598static 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
bfa83a9e
TG
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 */
1614static 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 */
1625static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
1626{
1627 return nla_put(skb, attrtype, 0, NULL);
1628}
1629
1630/**
2175d87c 1631 * nla_put_msecs - Add a msecs netlink attribute to a skb and align it
bfa83a9e
TG
1632 * @skb: socket buffer to add attribute to
1633 * @attrtype: attribute type
d87de1f3 1634 * @njiffies: number of jiffies to convert to msecs
2175d87c 1635 * @padattr: attribute type for the padding
bfa83a9e
TG
1636 */
1637static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
2175d87c 1638 unsigned long njiffies, int padattr)
bfa83a9e 1639{
d87de1f3 1640 u64 tmp = jiffies_to_msecs(njiffies);
2175d87c
ND
1641
1642 return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
bfa83a9e
TG
1643}
1644
930345ea
JB
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 */
1652static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
1653 __be32 addr)
1654{
b4391db4
AB
1655 __be32 tmp = addr;
1656
1657 return nla_put_be32(skb, attrtype, tmp);
930345ea
JB
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 */
1667static 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
8953b077
JP
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 */
1680static 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
bfa83a9e
TG
1688/**
1689 * nla_get_u32 - return payload of u32 attribute
1690 * @nla: u32 netlink attribute
1691 */
b057efd4 1692static inline u32 nla_get_u32(const struct nlattr *nla)
bfa83a9e
TG
1693{
1694 return *(u32 *) nla_data(nla);
1695}
1696
00012e5b
AV
1697/**
1698 * nla_get_be32 - return payload of __be32 attribute
1699 * @nla: __be32 netlink attribute
1700 */
b057efd4 1701static inline __be32 nla_get_be32(const struct nlattr *nla)
00012e5b
AV
1702{
1703 return *(__be32 *) nla_data(nla);
1704}
1705
c648a013
AA
1706/**
1707 * nla_get_le32 - return payload of __le32 attribute
1708 * @nla: __le32 netlink attribute
1709 */
1710static inline __le32 nla_get_le32(const struct nlattr *nla)
1711{
1712 return *(__le32 *) nla_data(nla);
1713}
1714
bfa83a9e
TG
1715/**
1716 * nla_get_u16 - return payload of u16 attribute
1717 * @nla: u16 netlink attribute
1718 */
b057efd4 1719static inline u16 nla_get_u16(const struct nlattr *nla)
bfa83a9e
TG
1720{
1721 return *(u16 *) nla_data(nla);
1722}
1723
838965ba
PM
1724/**
1725 * nla_get_be16 - return payload of __be16 attribute
1726 * @nla: __be16 netlink attribute
1727 */
b057efd4 1728static inline __be16 nla_get_be16(const struct nlattr *nla)
838965ba
PM
1729{
1730 return *(__be16 *) nla_data(nla);
1731}
1732
4a89c256
TG
1733/**
1734 * nla_get_le16 - return payload of __le16 attribute
1735 * @nla: __le16 netlink attribute
1736 */
b057efd4 1737static inline __le16 nla_get_le16(const struct nlattr *nla)
4a89c256
TG
1738{
1739 return *(__le16 *) nla_data(nla);
1740}
1741
bfa83a9e
TG
1742/**
1743 * nla_get_u8 - return payload of u8 attribute
1744 * @nla: u8 netlink attribute
1745 */
b057efd4 1746static inline u8 nla_get_u8(const struct nlattr *nla)
bfa83a9e
TG
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 */
b057efd4 1755static inline u64 nla_get_u64(const struct nlattr *nla)
bfa83a9e
TG
1756{
1757 u64 tmp;
1758
1759 nla_memcpy(&tmp, nla, sizeof(tmp));
1760
1761 return tmp;
1762}
1763
374d345d
JK
1764/**
1765 * nla_get_uint - return payload of uint attribute
1766 * @nla: uint netlink attribute
1767 */
1768static 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
a17c8598
PNA
1775/**
1776 * nla_get_be64 - return payload of __be64 attribute
1777 * @nla: __be64 netlink attribute
1778 */
1779static inline __be64 nla_get_be64(const struct nlattr *nla)
1780{
f5d410f2
PNA
1781 __be64 tmp;
1782
1783 nla_memcpy(&tmp, nla, sizeof(tmp));
1784
4778e0be
JP
1785 return tmp;
1786}
1787
c648a013
AA
1788/**
1789 * nla_get_le64 - return payload of __le64 attribute
1790 * @nla: __le64 netlink attribute
1791 */
1792static inline __le64 nla_get_le64(const struct nlattr *nla)
1793{
1794 return *(__le64 *) nla_data(nla);
1795}
1796
4778e0be
JP
1797/**
1798 * nla_get_s32 - return payload of s32 attribute
1799 * @nla: s32 netlink attribute
1800 */
1801static 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 */
1810static 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 */
1819static 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 */
1828static inline s64 nla_get_s64(const struct nlattr *nla)
1829{
1830 s64 tmp;
1831
1832 nla_memcpy(&tmp, nla, sizeof(tmp));
1833
f5d410f2 1834 return tmp;
a17c8598
PNA
1835}
1836
374d345d
JK
1837/**
1838 * nla_get_sint - return payload of uint attribute
1839 * @nla: uint netlink attribute
1840 */
1841static 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
bfa83a9e
TG
1848/**
1849 * nla_get_flag - return payload of flag attribute
1850 * @nla: flag netlink attribute
1851 */
b057efd4 1852static inline int nla_get_flag(const struct nlattr *nla)
bfa83a9e
TG
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 */
b057efd4 1863static inline unsigned long nla_get_msecs(const struct nlattr *nla)
bfa83a9e
TG
1864{
1865 u64 msecs = nla_get_u64(nla);
1866
1867 return msecs_to_jiffies((unsigned long) msecs);
1868}
1869
67b61f6c
JB
1870/**
1871 * nla_get_in_addr - return payload of IPv4 address attribute
1872 * @nla: IPv4 address netlink attribute
1873 */
1874static 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 */
1883static 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
64c83d83
JHS
1891/**
1892 * nla_get_bitfield32 - return payload of 32 bitfield attribute
1893 * @nla: nla_bitfield32 attribute
1894 */
1895static 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
b15ca182
TG
1903/**
1904 * nla_memdup - duplicate attribute memory (kmemdup)
1905 * @src: netlink attribute to duplicate from
1906 * @gfp: GFP mask
1907 */
2c321f3f 1908static inline void *nla_memdup_noprof(const struct nlattr *src, gfp_t gfp)
b15ca182 1909{
2c321f3f 1910 return kmemdup_noprof(nla_data(src), nla_len(src), gfp);
b15ca182 1911}
2c321f3f 1912#define nla_memdup(...) alloc_hooks(nla_memdup_noprof(__VA_ARGS__))
b15ca182 1913
bfa83a9e 1914/**
ae0be8de 1915 * nla_nest_start_noflag - Start a new level of nested attributes
bfa83a9e
TG
1916 * @skb: socket buffer to add attributes to
1917 * @attrtype: attribute type of container
1918 *
ae0be8de
MK
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
bfa83a9e 1924 */
ae0be8de
MK
1925static inline struct nlattr *nla_nest_start_noflag(struct sk_buff *skb,
1926 int attrtype)
bfa83a9e 1927{
27a884dc 1928 struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
bfa83a9e
TG
1929
1930 if (nla_put(skb, attrtype, 0, NULL) < 0)
1931 return NULL;
1932
1933 return start;
1934}
1935
ae0be8de
MK
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 */
1946static 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
bfa83a9e
TG
1951/**
1952 * nla_nest_end - Finalize nesting of attributes
d1ec3b77 1953 * @skb: socket buffer the attributes are stored in
bfa83a9e
TG
1954 * @start: container attribute
1955 *
1956 * Corrects the container attribute header to include the all
1957 * appeneded attributes.
1958 *
1959 * Returns the total data length of the skb.
1960 */
1961static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
1962{
27a884dc 1963 start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
bfa83a9e
TG
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
bc3ed28c 1973 * attributes. Returns -EMSGSIZE
bfa83a9e 1974 */
bc3ed28c 1975static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
bfa83a9e 1976{
bc3ed28c 1977 nlmsg_trim(skb, start);
bfa83a9e
TG
1978}
1979
4fe5d5c0 1980/**
32d5109a 1981 * __nla_validate_nested - Validate a stream of nested attributes
4fe5d5c0
PM
1982 * @start: container attribute
1983 * @maxtype: maximum attribute type to be expected
1984 * @policy: validation policy
8cb08174 1985 * @validate: validation strictness
fceb6435 1986 * @extack: extended ACK report struct
4fe5d5c0
PM
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 documenation of struct nla_policy for more details.
1991 *
1992 * Returns 0 on success or a negative error code.
1993 */
8cb08174
JB
1994static 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
901bb989 2003static inline int
32d5109a
MK
2004nla_validate_nested(const struct nlattr *start, int maxtype,
2005 const struct nla_policy *policy,
2006 struct netlink_ext_ack *extack)
901bb989
JB
2007{
2008 return __nla_validate_nested(start, maxtype, policy,
2009 NL_VALIDATE_STRICT, extack);
2010}
2011
8cb08174
JB
2012static inline int
2013nla_validate_nested_deprecated(const struct nlattr *start, int maxtype,
2014 const struct nla_policy *policy,
2015 struct netlink_ext_ack *extack)
4fe5d5c0 2016{
8cb08174
JB
2017 return __nla_validate_nested(start, maxtype, policy,
2018 NL_VALIDATE_LIBERAL, extack);
4fe5d5c0
PM
2019}
2020
089bf1a6
ND
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 */
2028static 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
35c58459
DM
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
cca1d815 2050 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
35c58459
DM
2051 *
2052 * Returns zero on success or a negative error code.
2053 */
2054static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
2055{
089bf1a6 2056 if (nla_need_padding_for_64bit(skb) &&
cca1d815
ED
2057 !nla_reserve(skb, padattr, 0))
2058 return -EMSGSIZE;
089bf1a6 2059
35c58459
DM
2060 return 0;
2061}
2062
2063/**
2064 * nla_total_size_64bit - total length of attribute including padding
2065 * @payload: length of payload
2066 */
2067static inline int nla_total_size_64bit(int payload)
2068{
2069 return NLA_ALIGN(nla_attr_size(payload))
cca1d815 2070#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
35c58459
DM
2071 + NLA_ALIGN(nla_attr_size(0))
2072#endif
2073 ;
2074}
2075
bfa83a9e
TG
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
e8058a49
JB
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
fe4944e5
TG
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
e8058a49
JB
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
941d8ebc
SH
2120/**
2121 * nla_is_last - Test if attribute is last in stream
2122 * @nla: attribute to test
2123 * @rem: bytes remaining in stream
2124 */
2125static inline bool nla_is_last(const struct nlattr *nla, int rem)
2126{
2127 return nla->nla_len == rem;
2128}
2129
2c28ae48
JB
2130void nla_get_range_unsigned(const struct nla_policy *pt,
2131 struct netlink_range_validation *range);
2132void nla_get_range_signed(const struct nla_policy *pt,
2133 struct netlink_range_validation_signed *range);
2134
adc84845
JK
2135struct netlink_policy_dump_state;
2136
04a351a6
JB
2137int netlink_policy_dump_add_policy(struct netlink_policy_dump_state **pstate,
2138 const struct nla_policy *policy,
2139 unsigned int maxtype);
2140int netlink_policy_dump_get_policy_idx(struct netlink_policy_dump_state *state,
2141 const struct nla_policy *policy,
2142 unsigned int maxtype);
adc84845
JK
2143bool netlink_policy_dump_loop(struct netlink_policy_dump_state *state);
2144int netlink_policy_dump_write(struct sk_buff *skb,
2145 struct netlink_policy_dump_state *state);
44f3625b
JB
2146int netlink_policy_dump_attr_size_estimate(const struct nla_policy *pt);
2147int netlink_policy_dump_write_attr(struct sk_buff *skb,
2148 const struct nla_policy *pt,
2149 int nestattr);
adc84845 2150void netlink_policy_dump_free(struct netlink_policy_dump_state *state);
d07dcf9a 2151
bfa83a9e 2152#endif