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