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