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