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_free() free a netlink message 45 * 46 * Message Sending: 47 * nlmsg_multicast() multicast message to several groups 48 * nlmsg_unicast() unicast a message to a single socket 49 * nlmsg_notify() send notification message 50 * 51 * Message Length Calculations: 52 * nlmsg_msg_size(payload) length of message w/o padding 53 * nlmsg_total_size(payload) length of message w/ padding 54 * nlmsg_padlen(payload) length of padding at tail 55 * 56 * Message Payload Access: 57 * nlmsg_data(nlh) head of message payload 58 * nlmsg_len(nlh) length of message payload 59 * nlmsg_attrdata(nlh, hdrlen) head of attributes data 60 * nlmsg_attrlen(nlh, hdrlen) length of attributes data 61 * 62 * Message Parsing: 63 * nlmsg_ok(nlh, remaining) does nlh fit into remaining bytes? 64 * nlmsg_next(nlh, remaining) get next netlink message 65 * nlmsg_parse() parse attributes of a message 66 * nlmsg_find_attr() find an attribute in a message 67 * nlmsg_for_each_msg() loop over all messages 68 * nlmsg_validate() validate netlink message incl. attrs 69 * nlmsg_for_each_attr() loop over all attributes 70 * 71 * Misc: 72 * nlmsg_report() report back to application? 73 * 74 * ------------------------------------------------------------------------ 75 * Attributes Interface 76 * ------------------------------------------------------------------------ 77 * 78 * Attribute Format: 79 * <------- nla_total_size(payload) -------> 80 * <---- nla_attr_size(payload) -----> 81 * +----------+- - -+- - - - - - - - - +- - -+-------- - - 82 * | Header | Pad | Payload | Pad | Header 83 * +----------+- - -+- - - - - - - - - +- - -+-------- - - 84 * <- nla_len(nla) -> ^ 85 * nla_data(nla)----^ | 86 * nla_next(nla)-----------------------------' 87 * 88 * Data Structures: 89 * struct nlattr netlink attribute header 90 * 91 * Attribute Construction: 92 * nla_reserve(skb, type, len) reserve room for an attribute 93 * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr 94 * nla_put(skb, type, len, data) add attribute to skb 95 * nla_put_nohdr(skb, len, data) add attribute w/o hdr 96 * nla_append(skb, len, data) append data to skb 97 * 98 * Attribute Construction for Basic Types: 99 * nla_put_u8(skb, type, value) add u8 attribute to skb 100 * nla_put_u16(skb, type, value) add u16 attribute to skb 101 * nla_put_u32(skb, type, value) add u32 attribute to skb 102 * nla_put_u64_64bit(skb, type, 103 * value, padattr) add u64 attribute to skb 104 * nla_put_s8(skb, type, value) add s8 attribute to skb 105 * nla_put_s16(skb, type, value) add s16 attribute to skb 106 * nla_put_s32(skb, type, value) add s32 attribute to skb 107 * nla_put_s64(skb, type, value, 108 * padattr) add s64 attribute to skb 109 * nla_put_string(skb, type, str) add string attribute to skb 110 * nla_put_flag(skb, type) add flag attribute to skb 111 * nla_put_msecs(skb, type, jiffies, 112 * padattr) add msecs attribute to skb 113 * nla_put_in_addr(skb, type, addr) add IPv4 address attribute to skb 114 * nla_put_in6_addr(skb, type, addr) add IPv6 address attribute to skb 115 * 116 * Nested Attributes Construction: 117 * nla_nest_start(skb, type) start a nested attribute 118 * nla_nest_end(skb, nla) finalize a nested attribute 119 * nla_nest_cancel(skb, nla) cancel nested attribute construction 120 * 121 * Attribute Length Calculations: 122 * nla_attr_size(payload) length of attribute w/o padding 123 * nla_total_size(payload) length of attribute w/ padding 124 * nla_padlen(payload) length of padding 125 * 126 * Attribute Payload Access: 127 * nla_data(nla) head of attribute payload 128 * nla_len(nla) length of attribute payload 129 * 130 * Attribute Payload Access for Basic Types: 131 * nla_get_u8(nla) get payload for a u8 attribute 132 * nla_get_u16(nla) get payload for a u16 attribute 133 * nla_get_u32(nla) get payload for a u32 attribute 134 * nla_get_u64(nla) get payload for a u64 attribute 135 * nla_get_s8(nla) get payload for a s8 attribute 136 * nla_get_s16(nla) get payload for a s16 attribute 137 * nla_get_s32(nla) get payload for a s32 attribute 138 * nla_get_s64(nla) get payload for a s64 attribute 139 * nla_get_flag(nla) return 1 if flag is true 140 * nla_get_msecs(nla) get payload for a msecs attribute 141 * 142 * Attribute Misc: 143 * nla_memcpy(dest, nla, count) copy attribute into memory 144 * nla_memcmp(nla, data, size) compare attribute with memory area 145 * nla_strlcpy(dst, nla, size) copy attribute to a sized string 146 * nla_strcmp(nla, str) compare attribute with string 147 * 148 * Attribute Parsing: 149 * nla_ok(nla, remaining) does nla fit into remaining bytes? 150 * nla_next(nla, remaining) get next netlink attribute 151 * nla_validate() validate a stream of attributes 152 * nla_validate_nested() validate a stream of nested attributes 153 * nla_find() find attribute in stream of attributes 154 * nla_find_nested() find attribute in nested attributes 155 * nla_parse() parse and validate stream of attrs 156 * nla_parse_nested() parse nested attributes 157 * nla_for_each_attr() loop over all attributes 158 * nla_for_each_nested() loop over the nested attributes 159 *========================================================================= 160 */ 161 162 /** 163 * Standard attribute types to specify validation policy 164 */ 165 enum { 166 NLA_UNSPEC, 167 NLA_U8, 168 NLA_U16, 169 NLA_U32, 170 NLA_U64, 171 NLA_STRING, 172 NLA_FLAG, 173 NLA_MSECS, 174 NLA_NESTED, 175 NLA_NESTED_ARRAY, 176 NLA_NUL_STRING, 177 NLA_BINARY, 178 NLA_S8, 179 NLA_S16, 180 NLA_S32, 181 NLA_S64, 182 NLA_BITFIELD32, 183 NLA_REJECT, 184 NLA_EXACT_LEN, 185 NLA_EXACT_LEN_WARN, 186 NLA_MIN_LEN, 187 __NLA_TYPE_MAX, 188 }; 189 190 #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1) 191 192 enum nla_policy_validation { 193 NLA_VALIDATE_NONE, 194 NLA_VALIDATE_RANGE, 195 NLA_VALIDATE_MIN, 196 NLA_VALIDATE_MAX, 197 NLA_VALIDATE_FUNCTION, 198 }; 199 200 /** 201 * struct nla_policy - attribute validation policy 202 * @type: Type of attribute or NLA_UNSPEC 203 * @validation_type: type of attribute validation done in addition to 204 * type-specific validation (e.g. range, function call), see 205 * &enum nla_policy_validation 206 * @len: Type specific length of payload 207 * 208 * Policies are defined as arrays of this struct, the array must be 209 * accessible by attribute type up to the highest identifier to be expected. 210 * 211 * Meaning of `len' field: 212 * NLA_STRING Maximum length of string 213 * NLA_NUL_STRING Maximum length of string (excluding NUL) 214 * NLA_FLAG Unused 215 * NLA_BINARY Maximum length of attribute payload 216 * NLA_MIN_LEN Minimum length of attribute payload 217 * NLA_NESTED, 218 * NLA_NESTED_ARRAY Length verification is done by checking len of 219 * nested header (or empty); len field is used if 220 * validation_data is also used, for the max attr 221 * number in the nested policy. 222 * NLA_U8, NLA_U16, 223 * NLA_U32, NLA_U64, 224 * NLA_S8, NLA_S16, 225 * NLA_S32, NLA_S64, 226 * NLA_MSECS Leaving the length field zero will verify the 227 * given type fits, using it verifies minimum length 228 * just like "All other" 229 * NLA_BITFIELD32 Unused 230 * NLA_REJECT Unused 231 * NLA_EXACT_LEN Attribute must have exactly this length, otherwise 232 * it is rejected. 233 * NLA_EXACT_LEN_WARN Attribute should have exactly this length, a warning 234 * is logged if it is longer, shorter is rejected. 235 * NLA_MIN_LEN Minimum length of attribute payload 236 * All other Minimum length of attribute payload 237 * 238 * Meaning of `validation_data' field: 239 * NLA_BITFIELD32 This is a 32-bit bitmap/bitselector attribute and 240 * validation data must point to a u32 value of valid 241 * flags 242 * NLA_REJECT This attribute is always rejected and validation data 243 * may point to a string to report as the error instead 244 * of the generic one in extended ACK. 245 * NLA_NESTED Points to a nested policy to validate, must also set 246 * `len' to the max attribute number. 247 * Note that nla_parse() will validate, but of course not 248 * parse, the nested sub-policies. 249 * NLA_NESTED_ARRAY Points to a nested policy to validate, must also set 250 * `len' to the max attribute number. The difference to 251 * NLA_NESTED is the structure - NLA_NESTED has the 252 * nested attributes directly inside, while an array has 253 * the nested attributes at another level down and the 254 * attributes directly in the nesting don't matter. 255 * All other Unused - but note that it's a union 256 * 257 * Meaning of `min' and `max' fields, use via NLA_POLICY_MIN, NLA_POLICY_MAX 258 * and NLA_POLICY_RANGE: 259 * NLA_U8, 260 * NLA_U16, 261 * NLA_U32, 262 * NLA_U64, 263 * NLA_S8, 264 * NLA_S16, 265 * NLA_S32, 266 * NLA_S64 These are used depending on the validation_type 267 * field, if that is min/max/range then the minimum, 268 * maximum and both are used (respectively) to check 269 * the value of the integer attribute. 270 * Note that in the interest of code simplicity and 271 * struct size both limits are s16, so you cannot 272 * enforce a range that doesn't fall within the range 273 * of s16 - do that as usual in the code instead. 274 * All other Unused - but note that it's a union 275 * 276 * Meaning of `validate' field, use via NLA_POLICY_VALIDATE_FN: 277 * NLA_BINARY Validation function called for the attribute, 278 * not compatible with use of the validation_data 279 * as in NLA_BITFIELD32, NLA_REJECT, NLA_NESTED and 280 * NLA_NESTED_ARRAY. 281 * All other Unused - but note that it's a union 282 * 283 * Example: 284 * static const struct nla_policy my_policy[ATTR_MAX+1] = { 285 * [ATTR_FOO] = { .type = NLA_U16 }, 286 * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ }, 287 * [ATTR_BAZ] = { .type = NLA_EXACT_LEN, .len = sizeof(struct mystruct) }, 288 * [ATTR_GOO] = { .type = NLA_BITFIELD32, .validation_data = &myvalidflags }, 289 * }; 290 */ 291 struct nla_policy { 292 u8 type; 293 u8 validation_type; 294 u16 len; 295 union { 296 const void *validation_data; 297 struct { 298 s16 min, max; 299 }; 300 int (*validate)(const struct nlattr *attr, 301 struct netlink_ext_ack *extack); 302 }; 303 }; 304 305 #define NLA_POLICY_EXACT_LEN(_len) { .type = NLA_EXACT_LEN, .len = _len } 306 #define NLA_POLICY_EXACT_LEN_WARN(_len) { .type = NLA_EXACT_LEN_WARN, \ 307 .len = _len } 308 #define NLA_POLICY_MIN_LEN(_len) { .type = NLA_MIN_LEN, .len = _len } 309 310 #define NLA_POLICY_ETH_ADDR NLA_POLICY_EXACT_LEN(ETH_ALEN) 311 #define NLA_POLICY_ETH_ADDR_COMPAT NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN) 312 313 #define _NLA_POLICY_NESTED(maxattr, policy) \ 314 { .type = NLA_NESTED, .validation_data = policy, .len = maxattr } 315 #define _NLA_POLICY_NESTED_ARRAY(maxattr, policy) \ 316 { .type = NLA_NESTED_ARRAY, .validation_data = policy, .len = maxattr } 317 #define NLA_POLICY_NESTED(policy) \ 318 _NLA_POLICY_NESTED(ARRAY_SIZE(policy) - 1, policy) 319 #define NLA_POLICY_NESTED_ARRAY(policy) \ 320 _NLA_POLICY_NESTED_ARRAY(ARRAY_SIZE(policy) - 1, policy) 321 322 #define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition)) 323 #define NLA_ENSURE_INT_TYPE(tp) \ 324 (__NLA_ENSURE(tp == NLA_S8 || tp == NLA_U8 || \ 325 tp == NLA_S16 || tp == NLA_U16 || \ 326 tp == NLA_S32 || tp == NLA_U32 || \ 327 tp == NLA_S64 || tp == NLA_U64) + tp) 328 #define NLA_ENSURE_NO_VALIDATION_PTR(tp) \ 329 (__NLA_ENSURE(tp != NLA_BITFIELD32 && \ 330 tp != NLA_REJECT && \ 331 tp != NLA_NESTED && \ 332 tp != NLA_NESTED_ARRAY) + tp) 333 334 #define NLA_POLICY_RANGE(tp, _min, _max) { \ 335 .type = NLA_ENSURE_INT_TYPE(tp), \ 336 .validation_type = NLA_VALIDATE_RANGE, \ 337 .min = _min, \ 338 .max = _max \ 339 } 340 341 #define NLA_POLICY_MIN(tp, _min) { \ 342 .type = NLA_ENSURE_INT_TYPE(tp), \ 343 .validation_type = NLA_VALIDATE_MIN, \ 344 .min = _min, \ 345 } 346 347 #define NLA_POLICY_MAX(tp, _max) { \ 348 .type = NLA_ENSURE_INT_TYPE(tp), \ 349 .validation_type = NLA_VALIDATE_MAX, \ 350 .max = _max, \ 351 } 352 353 #define NLA_POLICY_VALIDATE_FN(tp, fn, ...) { \ 354 .type = NLA_ENSURE_NO_VALIDATION_PTR(tp), \ 355 .validation_type = NLA_VALIDATE_FUNCTION, \ 356 .validate = fn, \ 357 .len = __VA_ARGS__ + 0, \ 358 } 359 360 /** 361 * struct nl_info - netlink source information 362 * @nlh: Netlink message header of original request 363 * @portid: Netlink PORTID of requesting application 364 */ 365 struct nl_info { 366 struct nlmsghdr *nlh; 367 struct net *nl_net; 368 u32 portid; 369 bool skip_notify; 370 }; 371 372 /** 373 * enum netlink_validation - netlink message/attribute validation levels 374 * @NL_VALIDATE_LIBERAL: Old-style "be liberal" validation, not caring about 375 * extra data at the end of the message, attributes being longer than 376 * they should be, or unknown attributes being present. 377 * @NL_VALIDATE_TRAILING: Reject junk data encountered after attribute parsing. 378 * @NL_VALIDATE_MAXTYPE: Reject attributes > max type; Together with _TRAILING 379 * this is equivalent to the old nla_parse_strict()/nlmsg_parse_strict(). 380 * @NL_VALIDATE_UNSPEC: Reject attributes with NLA_UNSPEC in the policy. 381 * This can safely be set by the kernel when the given policy has no 382 * NLA_UNSPEC anymore, and can thus be used to ensure policy entries 383 * are enforced going forward. 384 * @NL_VALIDATE_STRICT_ATTRS: strict attribute policy parsing (e.g. 385 * U8, U16, U32 must have exact size, etc.) 386 */ 387 enum netlink_validation { 388 NL_VALIDATE_LIBERAL = 0, 389 NL_VALIDATE_TRAILING = BIT(0), 390 NL_VALIDATE_MAXTYPE = BIT(1), 391 NL_VALIDATE_UNSPEC = BIT(2), 392 NL_VALIDATE_STRICT_ATTRS = BIT(3), 393 }; 394 395 #define NL_VALIDATE_DEPRECATED_STRICT (NL_VALIDATE_TRAILING |\ 396 NL_VALIDATE_MAXTYPE) 397 #define NL_VALIDATE_STRICT (NL_VALIDATE_TRAILING |\ 398 NL_VALIDATE_MAXTYPE |\ 399 NL_VALIDATE_UNSPEC |\ 400 NL_VALIDATE_STRICT_ATTRS) 401 402 int netlink_rcv_skb(struct sk_buff *skb, 403 int (*cb)(struct sk_buff *, struct nlmsghdr *, 404 struct netlink_ext_ack *)); 405 int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid, 406 unsigned int group, int report, gfp_t flags); 407 408 int __nla_validate(const struct nlattr *head, int len, int maxtype, 409 const struct nla_policy *policy, unsigned int validate, 410 struct netlink_ext_ack *extack); 411 int __nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, 412 int len, const struct nla_policy *policy, unsigned int validate, 413 struct netlink_ext_ack *extack); 414 int nla_policy_len(const struct nla_policy *, int); 415 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype); 416 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize); 417 char *nla_strdup(const struct nlattr *nla, gfp_t flags); 418 int nla_memcpy(void *dest, const struct nlattr *src, int count); 419 int nla_memcmp(const struct nlattr *nla, const void *data, size_t size); 420 int nla_strcmp(const struct nlattr *nla, const char *str); 421 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 422 struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype, 423 int attrlen, int padattr); 424 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 425 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 426 struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, 427 int attrlen, int padattr); 428 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 429 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, 430 const void *data); 431 void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 432 const void *data, int padattr); 433 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 434 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data); 435 int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 436 const void *data, int padattr); 437 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 438 int nla_append(struct sk_buff *skb, int attrlen, const void *data); 439 440 /************************************************************************** 441 * Netlink Messages 442 **************************************************************************/ 443 444 /** 445 * nlmsg_msg_size - length of netlink message not including padding 446 * @payload: length of message payload 447 */ 448 static inline int nlmsg_msg_size(int payload) 449 { 450 return NLMSG_HDRLEN + payload; 451 } 452 453 /** 454 * nlmsg_total_size - length of netlink message including padding 455 * @payload: length of message payload 456 */ 457 static inline int nlmsg_total_size(int payload) 458 { 459 return NLMSG_ALIGN(nlmsg_msg_size(payload)); 460 } 461 462 /** 463 * nlmsg_padlen - length of padding at the message's tail 464 * @payload: length of message payload 465 */ 466 static inline int nlmsg_padlen(int payload) 467 { 468 return nlmsg_total_size(payload) - nlmsg_msg_size(payload); 469 } 470 471 /** 472 * nlmsg_data - head of message payload 473 * @nlh: netlink message header 474 */ 475 static inline void *nlmsg_data(const struct nlmsghdr *nlh) 476 { 477 return (unsigned char *) nlh + NLMSG_HDRLEN; 478 } 479 480 /** 481 * nlmsg_len - length of message payload 482 * @nlh: netlink message header 483 */ 484 static inline int nlmsg_len(const struct nlmsghdr *nlh) 485 { 486 return nlh->nlmsg_len - NLMSG_HDRLEN; 487 } 488 489 /** 490 * nlmsg_attrdata - head of attributes data 491 * @nlh: netlink message header 492 * @hdrlen: length of family specific header 493 */ 494 static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, 495 int hdrlen) 496 { 497 unsigned char *data = nlmsg_data(nlh); 498 return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen)); 499 } 500 501 /** 502 * nlmsg_attrlen - length of attributes data 503 * @nlh: netlink message header 504 * @hdrlen: length of family specific header 505 */ 506 static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen) 507 { 508 return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen); 509 } 510 511 /** 512 * nlmsg_ok - check if the netlink message fits into the remaining bytes 513 * @nlh: netlink message header 514 * @remaining: number of bytes remaining in message stream 515 */ 516 static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining) 517 { 518 return (remaining >= (int) sizeof(struct nlmsghdr) && 519 nlh->nlmsg_len >= sizeof(struct nlmsghdr) && 520 nlh->nlmsg_len <= remaining); 521 } 522 523 /** 524 * nlmsg_next - next netlink message in message stream 525 * @nlh: netlink message header 526 * @remaining: number of bytes remaining in message stream 527 * 528 * Returns the next netlink message in the message stream and 529 * decrements remaining by the size of the current message. 530 */ 531 static inline struct nlmsghdr * 532 nlmsg_next(const struct nlmsghdr *nlh, int *remaining) 533 { 534 int totlen = NLMSG_ALIGN(nlh->nlmsg_len); 535 536 *remaining -= totlen; 537 538 return (struct nlmsghdr *) ((unsigned char *) nlh + totlen); 539 } 540 541 /** 542 * nla_parse_deprecated - Parse a stream of attributes into a tb buffer 543 * @tb: destination array with maxtype+1 elements 544 * @maxtype: maximum attribute type to be expected 545 * @head: head of attribute stream 546 * @len: length of attribute stream 547 * @policy: validation policy 548 * @extack: extended ACK pointer 549 * 550 * Parses a stream of attributes and stores a pointer to each attribute in 551 * the tb array accessible via the attribute type. Attributes with a type 552 * exceeding maxtype will be ignored and attributes from the policy are not 553 * always strictly validated (only for new attributes). 554 * 555 * Returns 0 on success or a negative error code. 556 */ 557 static inline int nla_parse_deprecated(struct nlattr **tb, int maxtype, 558 const struct nlattr *head, int len, 559 const struct nla_policy *policy, 560 struct netlink_ext_ack *extack) 561 { 562 return __nla_parse(tb, maxtype, head, len, policy, 563 NL_VALIDATE_LIBERAL, extack); 564 } 565 566 /** 567 * nla_parse_deprecated_strict - Parse a stream of attributes into a tb buffer 568 * @tb: destination array with maxtype+1 elements 569 * @maxtype: maximum attribute type to be expected 570 * @head: head of attribute stream 571 * @len: length of attribute stream 572 * @policy: validation policy 573 * @extack: extended ACK pointer 574 * 575 * Parses a stream of attributes and stores a pointer to each attribute in 576 * the tb array accessible via the attribute type. Attributes with a type 577 * exceeding maxtype will be rejected as well as trailing data, but the 578 * policy is not completely strictly validated (only for new attributes). 579 * 580 * Returns 0 on success or a negative error code. 581 */ 582 static inline int nla_parse_deprecated_strict(struct nlattr **tb, int maxtype, 583 const struct nlattr *head, 584 int len, 585 const struct nla_policy *policy, 586 struct netlink_ext_ack *extack) 587 { 588 return __nla_parse(tb, maxtype, head, len, policy, 589 NL_VALIDATE_DEPRECATED_STRICT, extack); 590 } 591 592 /** 593 * __nlmsg_parse - parse attributes of a netlink message 594 * @nlh: netlink message header 595 * @hdrlen: length of family specific header 596 * @tb: destination array with maxtype+1 elements 597 * @maxtype: maximum attribute type to be expected 598 * @policy: validation policy 599 * @validate: validation strictness 600 * @extack: extended ACK report struct 601 * 602 * See nla_parse() 603 */ 604 static inline int __nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen, 605 struct nlattr *tb[], int maxtype, 606 const struct nla_policy *policy, 607 unsigned int validate, 608 struct netlink_ext_ack *extack) 609 { 610 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) { 611 NL_SET_ERR_MSG(extack, "Invalid header length"); 612 return -EINVAL; 613 } 614 615 return __nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), 616 nlmsg_attrlen(nlh, hdrlen), policy, validate, 617 extack); 618 } 619 620 /** 621 * nlmsg_parse_deprecated - parse attributes of a netlink message 622 * @nlh: netlink message header 623 * @hdrlen: length of family specific header 624 * @tb: destination array with maxtype+1 elements 625 * @maxtype: maximum attribute type to be expected 626 * @extack: extended ACK report struct 627 * 628 * See nla_parse_deprecated() 629 */ 630 static inline int nlmsg_parse_deprecated(const struct nlmsghdr *nlh, int hdrlen, 631 struct nlattr *tb[], int maxtype, 632 const struct nla_policy *policy, 633 struct netlink_ext_ack *extack) 634 { 635 return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy, 636 NL_VALIDATE_LIBERAL, extack); 637 } 638 639 /** 640 * nlmsg_parse_deprecated_strict - parse attributes of a netlink message 641 * @nlh: netlink message header 642 * @hdrlen: length of family specific header 643 * @tb: destination array with maxtype+1 elements 644 * @maxtype: maximum attribute type to be expected 645 * @extack: extended ACK report struct 646 * 647 * See nla_parse_deprecated_strict() 648 */ 649 static inline int 650 nlmsg_parse_deprecated_strict(const struct nlmsghdr *nlh, int hdrlen, 651 struct nlattr *tb[], int maxtype, 652 const struct nla_policy *policy, 653 struct netlink_ext_ack *extack) 654 { 655 return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy, 656 NL_VALIDATE_DEPRECATED_STRICT, extack); 657 } 658 659 /** 660 * nlmsg_find_attr - find a specific attribute in a netlink message 661 * @nlh: netlink message header 662 * @hdrlen: length of familiy specific header 663 * @attrtype: type of attribute to look for 664 * 665 * Returns the first attribute which matches the specified type. 666 */ 667 static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh, 668 int hdrlen, int attrtype) 669 { 670 return nla_find(nlmsg_attrdata(nlh, hdrlen), 671 nlmsg_attrlen(nlh, hdrlen), attrtype); 672 } 673 674 /** 675 * nla_validate_deprecated - Validate a stream of attributes 676 * @head: head of attribute stream 677 * @len: length of attribute stream 678 * @maxtype: maximum attribute type to be expected 679 * @policy: validation policy 680 * @validate: validation strictness 681 * @extack: extended ACK report struct 682 * 683 * Validates all attributes in the specified attribute stream against the 684 * specified policy. Validation is done in liberal mode. 685 * See documenation of struct nla_policy for more details. 686 * 687 * Returns 0 on success or a negative error code. 688 */ 689 static inline int nla_validate_deprecated(const struct nlattr *head, int len, 690 int maxtype, 691 const struct nla_policy *policy, 692 struct netlink_ext_ack *extack) 693 { 694 return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_LIBERAL, 695 extack); 696 } 697 698 699 /** 700 * nlmsg_validate_deprecated - validate a netlink message including attributes 701 * @nlh: netlinket message header 702 * @hdrlen: length of familiy specific header 703 * @maxtype: maximum attribute type to be expected 704 * @policy: validation policy 705 * @extack: extended ACK report struct 706 */ 707 static inline int nlmsg_validate_deprecated(const struct nlmsghdr *nlh, 708 int hdrlen, int maxtype, 709 const struct nla_policy *policy, 710 struct netlink_ext_ack *extack) 711 { 712 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) 713 return -EINVAL; 714 715 return __nla_validate(nlmsg_attrdata(nlh, hdrlen), 716 nlmsg_attrlen(nlh, hdrlen), maxtype, 717 policy, NL_VALIDATE_LIBERAL, extack); 718 } 719 720 721 722 /** 723 * nlmsg_report - need to report back to application? 724 * @nlh: netlink message header 725 * 726 * Returns 1 if a report back to the application is requested. 727 */ 728 static inline int nlmsg_report(const struct nlmsghdr *nlh) 729 { 730 return !!(nlh->nlmsg_flags & NLM_F_ECHO); 731 } 732 733 /** 734 * nlmsg_for_each_attr - iterate over a stream of attributes 735 * @pos: loop counter, set to current attribute 736 * @nlh: netlink message header 737 * @hdrlen: length of familiy specific header 738 * @rem: initialized to len, holds bytes currently remaining in stream 739 */ 740 #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \ 741 nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \ 742 nlmsg_attrlen(nlh, hdrlen), rem) 743 744 /** 745 * nlmsg_put - Add a new netlink message to an skb 746 * @skb: socket buffer to store message in 747 * @portid: netlink PORTID of requesting application 748 * @seq: sequence number of message 749 * @type: message type 750 * @payload: length of message payload 751 * @flags: message flags 752 * 753 * Returns NULL if the tailroom of the skb is insufficient to store 754 * the message header and payload. 755 */ 756 static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, 757 int type, int payload, int flags) 758 { 759 if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload))) 760 return NULL; 761 762 return __nlmsg_put(skb, portid, seq, type, payload, flags); 763 } 764 765 /** 766 * nlmsg_put_answer - Add a new callback based netlink message to an skb 767 * @skb: socket buffer to store message in 768 * @cb: netlink callback 769 * @type: message type 770 * @payload: length of message payload 771 * @flags: message flags 772 * 773 * Returns NULL if the tailroom of the skb is insufficient to store 774 * the message header and payload. 775 */ 776 static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb, 777 struct netlink_callback *cb, 778 int type, int payload, 779 int flags) 780 { 781 return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 782 type, payload, flags); 783 } 784 785 /** 786 * nlmsg_new - Allocate a new netlink message 787 * @payload: size of the message payload 788 * @flags: the type of memory to allocate. 789 * 790 * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known 791 * and a good default is needed. 792 */ 793 static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags) 794 { 795 return alloc_skb(nlmsg_total_size(payload), flags); 796 } 797 798 /** 799 * nlmsg_end - Finalize a netlink message 800 * @skb: socket buffer the message is stored in 801 * @nlh: netlink message header 802 * 803 * Corrects the netlink message header to include the appeneded 804 * attributes. Only necessary if attributes have been added to 805 * the message. 806 */ 807 static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh) 808 { 809 nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh; 810 } 811 812 /** 813 * nlmsg_get_pos - return current position in netlink message 814 * @skb: socket buffer the message is stored in 815 * 816 * Returns a pointer to the current tail of the message. 817 */ 818 static inline void *nlmsg_get_pos(struct sk_buff *skb) 819 { 820 return skb_tail_pointer(skb); 821 } 822 823 /** 824 * nlmsg_trim - Trim message to a mark 825 * @skb: socket buffer the message is stored in 826 * @mark: mark to trim to 827 * 828 * Trims the message to the provided mark. 829 */ 830 static inline void nlmsg_trim(struct sk_buff *skb, const void *mark) 831 { 832 if (mark) { 833 WARN_ON((unsigned char *) mark < skb->data); 834 skb_trim(skb, (unsigned char *) mark - skb->data); 835 } 836 } 837 838 /** 839 * nlmsg_cancel - Cancel construction of a netlink message 840 * @skb: socket buffer the message is stored in 841 * @nlh: netlink message header 842 * 843 * Removes the complete netlink message including all 844 * attributes from the socket buffer again. 845 */ 846 static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh) 847 { 848 nlmsg_trim(skb, nlh); 849 } 850 851 /** 852 * nlmsg_free - free a netlink message 853 * @skb: socket buffer of netlink message 854 */ 855 static inline void nlmsg_free(struct sk_buff *skb) 856 { 857 kfree_skb(skb); 858 } 859 860 /** 861 * nlmsg_multicast - multicast a netlink message 862 * @sk: netlink socket to spread messages to 863 * @skb: netlink message as socket buffer 864 * @portid: own netlink portid to avoid sending to yourself 865 * @group: multicast group id 866 * @flags: allocation flags 867 */ 868 static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, 869 u32 portid, unsigned int group, gfp_t flags) 870 { 871 int err; 872 873 NETLINK_CB(skb).dst_group = group; 874 875 err = netlink_broadcast(sk, skb, portid, group, flags); 876 if (err > 0) 877 err = 0; 878 879 return err; 880 } 881 882 /** 883 * nlmsg_unicast - unicast a netlink message 884 * @sk: netlink socket to spread message to 885 * @skb: netlink message as socket buffer 886 * @portid: netlink portid of the destination socket 887 */ 888 static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid) 889 { 890 int err; 891 892 err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT); 893 if (err > 0) 894 err = 0; 895 896 return err; 897 } 898 899 /** 900 * nlmsg_for_each_msg - iterate over a stream of messages 901 * @pos: loop counter, set to current message 902 * @head: head of message stream 903 * @len: length of message stream 904 * @rem: initialized to len, holds bytes currently remaining in stream 905 */ 906 #define nlmsg_for_each_msg(pos, head, len, rem) \ 907 for (pos = head, rem = len; \ 908 nlmsg_ok(pos, rem); \ 909 pos = nlmsg_next(pos, &(rem))) 910 911 /** 912 * nl_dump_check_consistent - check if sequence is consistent and advertise if not 913 * @cb: netlink callback structure that stores the sequence number 914 * @nlh: netlink message header to write the flag to 915 * 916 * This function checks if the sequence (generation) number changed during dump 917 * and if it did, advertises it in the netlink message header. 918 * 919 * The correct way to use it is to set cb->seq to the generation counter when 920 * all locks for dumping have been acquired, and then call this function for 921 * each message that is generated. 922 * 923 * Note that due to initialisation concerns, 0 is an invalid sequence number 924 * and must not be used by code that uses this functionality. 925 */ 926 static inline void 927 nl_dump_check_consistent(struct netlink_callback *cb, 928 struct nlmsghdr *nlh) 929 { 930 if (cb->prev_seq && cb->seq != cb->prev_seq) 931 nlh->nlmsg_flags |= NLM_F_DUMP_INTR; 932 cb->prev_seq = cb->seq; 933 } 934 935 /************************************************************************** 936 * Netlink Attributes 937 **************************************************************************/ 938 939 /** 940 * nla_attr_size - length of attribute not including padding 941 * @payload: length of payload 942 */ 943 static inline int nla_attr_size(int payload) 944 { 945 return NLA_HDRLEN + payload; 946 } 947 948 /** 949 * nla_total_size - total length of attribute including padding 950 * @payload: length of payload 951 */ 952 static inline int nla_total_size(int payload) 953 { 954 return NLA_ALIGN(nla_attr_size(payload)); 955 } 956 957 /** 958 * nla_padlen - length of padding at the tail of attribute 959 * @payload: length of payload 960 */ 961 static inline int nla_padlen(int payload) 962 { 963 return nla_total_size(payload) - nla_attr_size(payload); 964 } 965 966 /** 967 * nla_type - attribute type 968 * @nla: netlink attribute 969 */ 970 static inline int nla_type(const struct nlattr *nla) 971 { 972 return nla->nla_type & NLA_TYPE_MASK; 973 } 974 975 /** 976 * nla_data - head of payload 977 * @nla: netlink attribute 978 */ 979 static inline void *nla_data(const struct nlattr *nla) 980 { 981 return (char *) nla + NLA_HDRLEN; 982 } 983 984 /** 985 * nla_len - length of payload 986 * @nla: netlink attribute 987 */ 988 static inline int nla_len(const struct nlattr *nla) 989 { 990 return nla->nla_len - NLA_HDRLEN; 991 } 992 993 /** 994 * nla_ok - check if the netlink attribute fits into the remaining bytes 995 * @nla: netlink attribute 996 * @remaining: number of bytes remaining in attribute stream 997 */ 998 static inline int nla_ok(const struct nlattr *nla, int remaining) 999 { 1000 return remaining >= (int) sizeof(*nla) && 1001 nla->nla_len >= sizeof(*nla) && 1002 nla->nla_len <= remaining; 1003 } 1004 1005 /** 1006 * nla_next - next netlink attribute in attribute stream 1007 * @nla: netlink attribute 1008 * @remaining: number of bytes remaining in attribute stream 1009 * 1010 * Returns the next netlink attribute in the attribute stream and 1011 * decrements remaining by the size of the current attribute. 1012 */ 1013 static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining) 1014 { 1015 unsigned int totlen = NLA_ALIGN(nla->nla_len); 1016 1017 *remaining -= totlen; 1018 return (struct nlattr *) ((char *) nla + totlen); 1019 } 1020 1021 /** 1022 * nla_find_nested - find attribute in a set of nested attributes 1023 * @nla: attribute containing the nested attributes 1024 * @attrtype: type of attribute to look for 1025 * 1026 * Returns the first attribute which matches the specified type. 1027 */ 1028 static inline struct nlattr * 1029 nla_find_nested(const struct nlattr *nla, int attrtype) 1030 { 1031 return nla_find(nla_data(nla), nla_len(nla), attrtype); 1032 } 1033 1034 /** 1035 * nla_parse_nested_deprecated - parse nested attributes 1036 * @tb: destination array with maxtype+1 elements 1037 * @maxtype: maximum attribute type to be expected 1038 * @nla: attribute containing the nested attributes 1039 * @policy: validation policy 1040 * @extack: extended ACK report struct 1041 * 1042 * See nla_parse_deprecated() 1043 */ 1044 static inline int nla_parse_nested_deprecated(struct nlattr *tb[], int maxtype, 1045 const struct nlattr *nla, 1046 const struct nla_policy *policy, 1047 struct netlink_ext_ack *extack) 1048 { 1049 return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy, 1050 NL_VALIDATE_LIBERAL, extack); 1051 } 1052 1053 /** 1054 * nla_put_u8 - Add a u8 netlink attribute to a socket buffer 1055 * @skb: socket buffer to add attribute to 1056 * @attrtype: attribute type 1057 * @value: numeric value 1058 */ 1059 static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value) 1060 { 1061 /* temporary variables to work around GCC PR81715 with asan-stack=1 */ 1062 u8 tmp = value; 1063 1064 return nla_put(skb, attrtype, sizeof(u8), &tmp); 1065 } 1066 1067 /** 1068 * nla_put_u16 - Add a u16 netlink attribute to a socket buffer 1069 * @skb: socket buffer to add attribute to 1070 * @attrtype: attribute type 1071 * @value: numeric value 1072 */ 1073 static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value) 1074 { 1075 u16 tmp = value; 1076 1077 return nla_put(skb, attrtype, sizeof(u16), &tmp); 1078 } 1079 1080 /** 1081 * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer 1082 * @skb: socket buffer to add attribute to 1083 * @attrtype: attribute type 1084 * @value: numeric value 1085 */ 1086 static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value) 1087 { 1088 __be16 tmp = value; 1089 1090 return nla_put(skb, attrtype, sizeof(__be16), &tmp); 1091 } 1092 1093 /** 1094 * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer 1095 * @skb: socket buffer to add attribute to 1096 * @attrtype: attribute type 1097 * @value: numeric value 1098 */ 1099 static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value) 1100 { 1101 __be16 tmp = value; 1102 1103 return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp); 1104 } 1105 1106 /** 1107 * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer 1108 * @skb: socket buffer to add attribute to 1109 * @attrtype: attribute type 1110 * @value: numeric value 1111 */ 1112 static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value) 1113 { 1114 __le16 tmp = value; 1115 1116 return nla_put(skb, attrtype, sizeof(__le16), &tmp); 1117 } 1118 1119 /** 1120 * nla_put_u32 - Add a u32 netlink attribute to a socket buffer 1121 * @skb: socket buffer to add attribute to 1122 * @attrtype: attribute type 1123 * @value: numeric value 1124 */ 1125 static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value) 1126 { 1127 u32 tmp = value; 1128 1129 return nla_put(skb, attrtype, sizeof(u32), &tmp); 1130 } 1131 1132 /** 1133 * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer 1134 * @skb: socket buffer to add attribute to 1135 * @attrtype: attribute type 1136 * @value: numeric value 1137 */ 1138 static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value) 1139 { 1140 __be32 tmp = value; 1141 1142 return nla_put(skb, attrtype, sizeof(__be32), &tmp); 1143 } 1144 1145 /** 1146 * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer 1147 * @skb: socket buffer to add attribute to 1148 * @attrtype: attribute type 1149 * @value: numeric value 1150 */ 1151 static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value) 1152 { 1153 __be32 tmp = value; 1154 1155 return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp); 1156 } 1157 1158 /** 1159 * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer 1160 * @skb: socket buffer to add attribute to 1161 * @attrtype: attribute type 1162 * @value: numeric value 1163 */ 1164 static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value) 1165 { 1166 __le32 tmp = value; 1167 1168 return nla_put(skb, attrtype, sizeof(__le32), &tmp); 1169 } 1170 1171 /** 1172 * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it 1173 * @skb: socket buffer to add attribute to 1174 * @attrtype: attribute type 1175 * @value: numeric value 1176 * @padattr: attribute type for the padding 1177 */ 1178 static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype, 1179 u64 value, int padattr) 1180 { 1181 u64 tmp = value; 1182 1183 return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr); 1184 } 1185 1186 /** 1187 * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it 1188 * @skb: socket buffer to add attribute to 1189 * @attrtype: attribute type 1190 * @value: numeric value 1191 * @padattr: attribute type for the padding 1192 */ 1193 static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value, 1194 int padattr) 1195 { 1196 __be64 tmp = value; 1197 1198 return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr); 1199 } 1200 1201 /** 1202 * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it 1203 * @skb: socket buffer to add attribute to 1204 * @attrtype: attribute type 1205 * @value: numeric value 1206 * @padattr: attribute type for the padding 1207 */ 1208 static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value, 1209 int padattr) 1210 { 1211 __be64 tmp = value; 1212 1213 return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp, 1214 padattr); 1215 } 1216 1217 /** 1218 * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it 1219 * @skb: socket buffer to add attribute to 1220 * @attrtype: attribute type 1221 * @value: numeric value 1222 * @padattr: attribute type for the padding 1223 */ 1224 static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value, 1225 int padattr) 1226 { 1227 __le64 tmp = value; 1228 1229 return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr); 1230 } 1231 1232 /** 1233 * nla_put_s8 - Add a s8 netlink attribute to a socket buffer 1234 * @skb: socket buffer to add attribute to 1235 * @attrtype: attribute type 1236 * @value: numeric value 1237 */ 1238 static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value) 1239 { 1240 s8 tmp = value; 1241 1242 return nla_put(skb, attrtype, sizeof(s8), &tmp); 1243 } 1244 1245 /** 1246 * nla_put_s16 - Add a s16 netlink attribute to a socket buffer 1247 * @skb: socket buffer to add attribute to 1248 * @attrtype: attribute type 1249 * @value: numeric value 1250 */ 1251 static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value) 1252 { 1253 s16 tmp = value; 1254 1255 return nla_put(skb, attrtype, sizeof(s16), &tmp); 1256 } 1257 1258 /** 1259 * nla_put_s32 - Add a s32 netlink attribute to a socket buffer 1260 * @skb: socket buffer to add attribute to 1261 * @attrtype: attribute type 1262 * @value: numeric value 1263 */ 1264 static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value) 1265 { 1266 s32 tmp = value; 1267 1268 return nla_put(skb, attrtype, sizeof(s32), &tmp); 1269 } 1270 1271 /** 1272 * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it 1273 * @skb: socket buffer to add attribute to 1274 * @attrtype: attribute type 1275 * @value: numeric value 1276 * @padattr: attribute type for the padding 1277 */ 1278 static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value, 1279 int padattr) 1280 { 1281 s64 tmp = value; 1282 1283 return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr); 1284 } 1285 1286 /** 1287 * nla_put_string - Add a string netlink attribute to a socket buffer 1288 * @skb: socket buffer to add attribute to 1289 * @attrtype: attribute type 1290 * @str: NUL terminated string 1291 */ 1292 static inline int nla_put_string(struct sk_buff *skb, int attrtype, 1293 const char *str) 1294 { 1295 return nla_put(skb, attrtype, strlen(str) + 1, str); 1296 } 1297 1298 /** 1299 * nla_put_flag - Add a flag netlink attribute to a socket buffer 1300 * @skb: socket buffer to add attribute to 1301 * @attrtype: attribute type 1302 */ 1303 static inline int nla_put_flag(struct sk_buff *skb, int attrtype) 1304 { 1305 return nla_put(skb, attrtype, 0, NULL); 1306 } 1307 1308 /** 1309 * nla_put_msecs - Add a msecs netlink attribute to a skb and align it 1310 * @skb: socket buffer to add attribute to 1311 * @attrtype: attribute type 1312 * @njiffies: number of jiffies to convert to msecs 1313 * @padattr: attribute type for the padding 1314 */ 1315 static inline int nla_put_msecs(struct sk_buff *skb, int attrtype, 1316 unsigned long njiffies, int padattr) 1317 { 1318 u64 tmp = jiffies_to_msecs(njiffies); 1319 1320 return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr); 1321 } 1322 1323 /** 1324 * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket 1325 * buffer 1326 * @skb: socket buffer to add attribute to 1327 * @attrtype: attribute type 1328 * @addr: IPv4 address 1329 */ 1330 static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype, 1331 __be32 addr) 1332 { 1333 __be32 tmp = addr; 1334 1335 return nla_put_be32(skb, attrtype, tmp); 1336 } 1337 1338 /** 1339 * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket 1340 * buffer 1341 * @skb: socket buffer to add attribute to 1342 * @attrtype: attribute type 1343 * @addr: IPv6 address 1344 */ 1345 static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype, 1346 const struct in6_addr *addr) 1347 { 1348 return nla_put(skb, attrtype, sizeof(*addr), addr); 1349 } 1350 1351 /** 1352 * nla_get_u32 - return payload of u32 attribute 1353 * @nla: u32 netlink attribute 1354 */ 1355 static inline u32 nla_get_u32(const struct nlattr *nla) 1356 { 1357 return *(u32 *) nla_data(nla); 1358 } 1359 1360 /** 1361 * nla_get_be32 - return payload of __be32 attribute 1362 * @nla: __be32 netlink attribute 1363 */ 1364 static inline __be32 nla_get_be32(const struct nlattr *nla) 1365 { 1366 return *(__be32 *) nla_data(nla); 1367 } 1368 1369 /** 1370 * nla_get_le32 - return payload of __le32 attribute 1371 * @nla: __le32 netlink attribute 1372 */ 1373 static inline __le32 nla_get_le32(const struct nlattr *nla) 1374 { 1375 return *(__le32 *) nla_data(nla); 1376 } 1377 1378 /** 1379 * nla_get_u16 - return payload of u16 attribute 1380 * @nla: u16 netlink attribute 1381 */ 1382 static inline u16 nla_get_u16(const struct nlattr *nla) 1383 { 1384 return *(u16 *) nla_data(nla); 1385 } 1386 1387 /** 1388 * nla_get_be16 - return payload of __be16 attribute 1389 * @nla: __be16 netlink attribute 1390 */ 1391 static inline __be16 nla_get_be16(const struct nlattr *nla) 1392 { 1393 return *(__be16 *) nla_data(nla); 1394 } 1395 1396 /** 1397 * nla_get_le16 - return payload of __le16 attribute 1398 * @nla: __le16 netlink attribute 1399 */ 1400 static inline __le16 nla_get_le16(const struct nlattr *nla) 1401 { 1402 return *(__le16 *) nla_data(nla); 1403 } 1404 1405 /** 1406 * nla_get_u8 - return payload of u8 attribute 1407 * @nla: u8 netlink attribute 1408 */ 1409 static inline u8 nla_get_u8(const struct nlattr *nla) 1410 { 1411 return *(u8 *) nla_data(nla); 1412 } 1413 1414 /** 1415 * nla_get_u64 - return payload of u64 attribute 1416 * @nla: u64 netlink attribute 1417 */ 1418 static inline u64 nla_get_u64(const struct nlattr *nla) 1419 { 1420 u64 tmp; 1421 1422 nla_memcpy(&tmp, nla, sizeof(tmp)); 1423 1424 return tmp; 1425 } 1426 1427 /** 1428 * nla_get_be64 - return payload of __be64 attribute 1429 * @nla: __be64 netlink attribute 1430 */ 1431 static inline __be64 nla_get_be64(const struct nlattr *nla) 1432 { 1433 __be64 tmp; 1434 1435 nla_memcpy(&tmp, nla, sizeof(tmp)); 1436 1437 return tmp; 1438 } 1439 1440 /** 1441 * nla_get_le64 - return payload of __le64 attribute 1442 * @nla: __le64 netlink attribute 1443 */ 1444 static inline __le64 nla_get_le64(const struct nlattr *nla) 1445 { 1446 return *(__le64 *) nla_data(nla); 1447 } 1448 1449 /** 1450 * nla_get_s32 - return payload of s32 attribute 1451 * @nla: s32 netlink attribute 1452 */ 1453 static inline s32 nla_get_s32(const struct nlattr *nla) 1454 { 1455 return *(s32 *) nla_data(nla); 1456 } 1457 1458 /** 1459 * nla_get_s16 - return payload of s16 attribute 1460 * @nla: s16 netlink attribute 1461 */ 1462 static inline s16 nla_get_s16(const struct nlattr *nla) 1463 { 1464 return *(s16 *) nla_data(nla); 1465 } 1466 1467 /** 1468 * nla_get_s8 - return payload of s8 attribute 1469 * @nla: s8 netlink attribute 1470 */ 1471 static inline s8 nla_get_s8(const struct nlattr *nla) 1472 { 1473 return *(s8 *) nla_data(nla); 1474 } 1475 1476 /** 1477 * nla_get_s64 - return payload of s64 attribute 1478 * @nla: s64 netlink attribute 1479 */ 1480 static inline s64 nla_get_s64(const struct nlattr *nla) 1481 { 1482 s64 tmp; 1483 1484 nla_memcpy(&tmp, nla, sizeof(tmp)); 1485 1486 return tmp; 1487 } 1488 1489 /** 1490 * nla_get_flag - return payload of flag attribute 1491 * @nla: flag netlink attribute 1492 */ 1493 static inline int nla_get_flag(const struct nlattr *nla) 1494 { 1495 return !!nla; 1496 } 1497 1498 /** 1499 * nla_get_msecs - return payload of msecs attribute 1500 * @nla: msecs netlink attribute 1501 * 1502 * Returns the number of milliseconds in jiffies. 1503 */ 1504 static inline unsigned long nla_get_msecs(const struct nlattr *nla) 1505 { 1506 u64 msecs = nla_get_u64(nla); 1507 1508 return msecs_to_jiffies((unsigned long) msecs); 1509 } 1510 1511 /** 1512 * nla_get_in_addr - return payload of IPv4 address attribute 1513 * @nla: IPv4 address netlink attribute 1514 */ 1515 static inline __be32 nla_get_in_addr(const struct nlattr *nla) 1516 { 1517 return *(__be32 *) nla_data(nla); 1518 } 1519 1520 /** 1521 * nla_get_in6_addr - return payload of IPv6 address attribute 1522 * @nla: IPv6 address netlink attribute 1523 */ 1524 static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla) 1525 { 1526 struct in6_addr tmp; 1527 1528 nla_memcpy(&tmp, nla, sizeof(tmp)); 1529 return tmp; 1530 } 1531 1532 /** 1533 * nla_get_bitfield32 - return payload of 32 bitfield attribute 1534 * @nla: nla_bitfield32 attribute 1535 */ 1536 static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla) 1537 { 1538 struct nla_bitfield32 tmp; 1539 1540 nla_memcpy(&tmp, nla, sizeof(tmp)); 1541 return tmp; 1542 } 1543 1544 /** 1545 * nla_memdup - duplicate attribute memory (kmemdup) 1546 * @src: netlink attribute to duplicate from 1547 * @gfp: GFP mask 1548 */ 1549 static inline void *nla_memdup(const struct nlattr *src, gfp_t gfp) 1550 { 1551 return kmemdup(nla_data(src), nla_len(src), gfp); 1552 } 1553 1554 /** 1555 * nla_nest_start_noflag - Start a new level of nested attributes 1556 * @skb: socket buffer to add attributes to 1557 * @attrtype: attribute type of container 1558 * 1559 * This function exists for backward compatibility to use in APIs which never 1560 * marked their nest attributes with NLA_F_NESTED flag. New APIs should use 1561 * nla_nest_start() which sets the flag. 1562 * 1563 * Returns the container attribute or NULL on error 1564 */ 1565 static inline struct nlattr *nla_nest_start_noflag(struct sk_buff *skb, 1566 int attrtype) 1567 { 1568 struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb); 1569 1570 if (nla_put(skb, attrtype, 0, NULL) < 0) 1571 return NULL; 1572 1573 return start; 1574 } 1575 1576 /** 1577 * nla_nest_start - Start a new level of nested attributes, with NLA_F_NESTED 1578 * @skb: socket buffer to add attributes to 1579 * @attrtype: attribute type of container 1580 * 1581 * Unlike nla_nest_start_noflag(), mark the nest attribute with NLA_F_NESTED 1582 * flag. This is the preferred function to use in new code. 1583 * 1584 * Returns the container attribute or NULL on error 1585 */ 1586 static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype) 1587 { 1588 return nla_nest_start_noflag(skb, attrtype | NLA_F_NESTED); 1589 } 1590 1591 /** 1592 * nla_nest_end - Finalize nesting of attributes 1593 * @skb: socket buffer the attributes are stored in 1594 * @start: container attribute 1595 * 1596 * Corrects the container attribute header to include the all 1597 * appeneded attributes. 1598 * 1599 * Returns the total data length of the skb. 1600 */ 1601 static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start) 1602 { 1603 start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start; 1604 return skb->len; 1605 } 1606 1607 /** 1608 * nla_nest_cancel - Cancel nesting of attributes 1609 * @skb: socket buffer the message is stored in 1610 * @start: container attribute 1611 * 1612 * Removes the container attribute and including all nested 1613 * attributes. Returns -EMSGSIZE 1614 */ 1615 static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start) 1616 { 1617 nlmsg_trim(skb, start); 1618 } 1619 1620 /** 1621 * nla_validate_nested - Validate a stream of nested attributes 1622 * @start: container attribute 1623 * @maxtype: maximum attribute type to be expected 1624 * @policy: validation policy 1625 * @validate: validation strictness 1626 * @extack: extended ACK report struct 1627 * 1628 * Validates all attributes in the nested attribute stream against the 1629 * specified policy. Attributes with a type exceeding maxtype will be 1630 * ignored. See documenation of struct nla_policy for more details. 1631 * 1632 * Returns 0 on success or a negative error code. 1633 */ 1634 static inline int __nla_validate_nested(const struct nlattr *start, int maxtype, 1635 const struct nla_policy *policy, 1636 unsigned int validate, 1637 struct netlink_ext_ack *extack) 1638 { 1639 return __nla_validate(nla_data(start), nla_len(start), maxtype, policy, 1640 validate, extack); 1641 } 1642 1643 static inline int 1644 nla_validate_nested_deprecated(const struct nlattr *start, int maxtype, 1645 const struct nla_policy *policy, 1646 struct netlink_ext_ack *extack) 1647 { 1648 return __nla_validate_nested(start, maxtype, policy, 1649 NL_VALIDATE_LIBERAL, extack); 1650 } 1651 1652 /** 1653 * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute 1654 * @skb: socket buffer the message is stored in 1655 * 1656 * Return true if padding is needed to align the next attribute (nla_data()) to 1657 * a 64-bit aligned area. 1658 */ 1659 static inline bool nla_need_padding_for_64bit(struct sk_buff *skb) 1660 { 1661 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1662 /* The nlattr header is 4 bytes in size, that's why we test 1663 * if the skb->data _is_ aligned. A NOP attribute, plus 1664 * nlattr header for next attribute, will make nla_data() 1665 * 8-byte aligned. 1666 */ 1667 if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8)) 1668 return true; 1669 #endif 1670 return false; 1671 } 1672 1673 /** 1674 * nla_align_64bit - 64-bit align the nla_data() of next attribute 1675 * @skb: socket buffer the message is stored in 1676 * @padattr: attribute type for the padding 1677 * 1678 * Conditionally emit a padding netlink attribute in order to make 1679 * the next attribute we emit have a 64-bit aligned nla_data() area. 1680 * This will only be done in architectures which do not have 1681 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined. 1682 * 1683 * Returns zero on success or a negative error code. 1684 */ 1685 static inline int nla_align_64bit(struct sk_buff *skb, int padattr) 1686 { 1687 if (nla_need_padding_for_64bit(skb) && 1688 !nla_reserve(skb, padattr, 0)) 1689 return -EMSGSIZE; 1690 1691 return 0; 1692 } 1693 1694 /** 1695 * nla_total_size_64bit - total length of attribute including padding 1696 * @payload: length of payload 1697 */ 1698 static inline int nla_total_size_64bit(int payload) 1699 { 1700 return NLA_ALIGN(nla_attr_size(payload)) 1701 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1702 + NLA_ALIGN(nla_attr_size(0)) 1703 #endif 1704 ; 1705 } 1706 1707 /** 1708 * nla_for_each_attr - iterate over a stream of attributes 1709 * @pos: loop counter, set to current attribute 1710 * @head: head of attribute stream 1711 * @len: length of attribute stream 1712 * @rem: initialized to len, holds bytes currently remaining in stream 1713 */ 1714 #define nla_for_each_attr(pos, head, len, rem) \ 1715 for (pos = head, rem = len; \ 1716 nla_ok(pos, rem); \ 1717 pos = nla_next(pos, &(rem))) 1718 1719 /** 1720 * nla_for_each_nested - iterate over nested attributes 1721 * @pos: loop counter, set to current attribute 1722 * @nla: attribute containing the nested attributes 1723 * @rem: initialized to len, holds bytes currently remaining in stream 1724 */ 1725 #define nla_for_each_nested(pos, nla, rem) \ 1726 nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem) 1727 1728 /** 1729 * nla_is_last - Test if attribute is last in stream 1730 * @nla: attribute to test 1731 * @rem: bytes remaining in stream 1732 */ 1733 static inline bool nla_is_last(const struct nlattr *nla, int rem) 1734 { 1735 return nla->nla_len == rem; 1736 } 1737 1738 #endif 1739