1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 2bfa83a9eSThomas Graf #ifndef __NET_NETLINK_H 3bfa83a9eSThomas Graf #define __NET_NETLINK_H 4bfa83a9eSThomas Graf 5bfa83a9eSThomas Graf #include <linux/types.h> 6bfa83a9eSThomas Graf #include <linux/netlink.h> 7d7fe0f24SAl Viro #include <linux/jiffies.h> 8930345eaSJiri Benc #include <linux/in6.h> 9bfa83a9eSThomas Graf 10bfa83a9eSThomas Graf /* ======================================================================== 11bfa83a9eSThomas Graf * Netlink Messages and Attributes Interface (As Seen On TV) 12bfa83a9eSThomas Graf * ------------------------------------------------------------------------ 13bfa83a9eSThomas Graf * Messages Interface 14bfa83a9eSThomas Graf * ------------------------------------------------------------------------ 15bfa83a9eSThomas Graf * 16bfa83a9eSThomas Graf * Message Format: 17bfa83a9eSThomas Graf * <--- nlmsg_total_size(payload) ---> 18bfa83a9eSThomas Graf * <-- nlmsg_msg_size(payload) -> 19bfa83a9eSThomas Graf * +----------+- - -+-------------+- - -+-------- - - 20bfa83a9eSThomas Graf * | nlmsghdr | Pad | Payload | Pad | nlmsghdr 21bfa83a9eSThomas Graf * +----------+- - -+-------------+- - -+-------- - - 22bfa83a9eSThomas Graf * nlmsg_data(nlh)---^ ^ 23bfa83a9eSThomas Graf * nlmsg_next(nlh)-----------------------+ 24bfa83a9eSThomas Graf * 25bfa83a9eSThomas Graf * Payload Format: 26bfa83a9eSThomas Graf * <---------------------- nlmsg_len(nlh) ---------------------> 27bfa83a9eSThomas Graf * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) -> 28bfa83a9eSThomas Graf * +----------------------+- - -+--------------------------------+ 29bfa83a9eSThomas Graf * | Family Header | Pad | Attributes | 30bfa83a9eSThomas Graf * +----------------------+- - -+--------------------------------+ 31bfa83a9eSThomas Graf * nlmsg_attrdata(nlh, hdrlen)---^ 32bfa83a9eSThomas Graf * 33bfa83a9eSThomas Graf * Data Structures: 34bfa83a9eSThomas Graf * struct nlmsghdr netlink message header 35bfa83a9eSThomas Graf * 36bfa83a9eSThomas Graf * Message Construction: 37bfa83a9eSThomas Graf * nlmsg_new() create a new netlink message 38bfa83a9eSThomas Graf * nlmsg_put() add a netlink message to an skb 39bfa83a9eSThomas Graf * nlmsg_put_answer() callback based nlmsg_put() 401dc8d8c0SJustin P. Mattock * nlmsg_end() finalize netlink message 41fe4944e5SThomas Graf * nlmsg_get_pos() return current position in message 42fe4944e5SThomas Graf * nlmsg_trim() trim part of message 43bfa83a9eSThomas Graf * nlmsg_cancel() cancel message construction 44bfa83a9eSThomas Graf * nlmsg_free() free a netlink message 45bfa83a9eSThomas Graf * 46bfa83a9eSThomas Graf * Message Sending: 47bfa83a9eSThomas Graf * nlmsg_multicast() multicast message to several groups 48bfa83a9eSThomas Graf * nlmsg_unicast() unicast a message to a single socket 49d387f6adSThomas Graf * nlmsg_notify() send notification message 50bfa83a9eSThomas Graf * 51bfa83a9eSThomas Graf * Message Length Calculations: 52bfa83a9eSThomas Graf * nlmsg_msg_size(payload) length of message w/o padding 53bfa83a9eSThomas Graf * nlmsg_total_size(payload) length of message w/ padding 54bfa83a9eSThomas Graf * nlmsg_padlen(payload) length of padding at tail 55bfa83a9eSThomas Graf * 56bfa83a9eSThomas Graf * Message Payload Access: 57bfa83a9eSThomas Graf * nlmsg_data(nlh) head of message payload 58bfa83a9eSThomas Graf * nlmsg_len(nlh) length of message payload 59bfa83a9eSThomas Graf * nlmsg_attrdata(nlh, hdrlen) head of attributes data 60bfa83a9eSThomas Graf * nlmsg_attrlen(nlh, hdrlen) length of attributes data 61bfa83a9eSThomas Graf * 62bfa83a9eSThomas Graf * Message Parsing: 63bfa83a9eSThomas Graf * nlmsg_ok(nlh, remaining) does nlh fit into remaining bytes? 64bfa83a9eSThomas Graf * nlmsg_next(nlh, remaining) get next netlink message 65bfa83a9eSThomas Graf * nlmsg_parse() parse attributes of a message 66bfa83a9eSThomas Graf * nlmsg_find_attr() find an attribute in a message 67bfa83a9eSThomas Graf * nlmsg_for_each_msg() loop over all messages 68bfa83a9eSThomas Graf * nlmsg_validate() validate netlink message incl. attrs 69bfa83a9eSThomas Graf * nlmsg_for_each_attr() loop over all attributes 70bfa83a9eSThomas Graf * 7197676b6bSThomas Graf * Misc: 7297676b6bSThomas Graf * nlmsg_report() report back to application? 7397676b6bSThomas Graf * 74bfa83a9eSThomas Graf * ------------------------------------------------------------------------ 75bfa83a9eSThomas Graf * Attributes Interface 76bfa83a9eSThomas Graf * ------------------------------------------------------------------------ 77bfa83a9eSThomas Graf * 78bfa83a9eSThomas Graf * Attribute Format: 79bfa83a9eSThomas Graf * <------- nla_total_size(payload) -------> 80bfa83a9eSThomas Graf * <---- nla_attr_size(payload) -----> 81bfa83a9eSThomas Graf * +----------+- - -+- - - - - - - - - +- - -+-------- - - 82bfa83a9eSThomas Graf * | Header | Pad | Payload | Pad | Header 83bfa83a9eSThomas Graf * +----------+- - -+- - - - - - - - - +- - -+-------- - - 84bfa83a9eSThomas Graf * <- nla_len(nla) -> ^ 85bfa83a9eSThomas Graf * nla_data(nla)----^ | 86bfa83a9eSThomas Graf * nla_next(nla)-----------------------------' 87bfa83a9eSThomas Graf * 88bfa83a9eSThomas Graf * Data Structures: 89d1ec3b77SPierre Ynard * struct nlattr netlink attribute header 90bfa83a9eSThomas Graf * 91bfa83a9eSThomas Graf * Attribute Construction: 92fe4944e5SThomas Graf * nla_reserve(skb, type, len) reserve room for an attribute 93fe4944e5SThomas Graf * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr 94bfa83a9eSThomas Graf * nla_put(skb, type, len, data) add attribute to skb 95fe4944e5SThomas Graf * nla_put_nohdr(skb, len, data) add attribute w/o hdr 9601480e1cSPatrick McHardy * nla_append(skb, len, data) append data to skb 97bfa83a9eSThomas Graf * 98bfa83a9eSThomas Graf * Attribute Construction for Basic Types: 99bfa83a9eSThomas Graf * nla_put_u8(skb, type, value) add u8 attribute to skb 100bfa83a9eSThomas Graf * nla_put_u16(skb, type, value) add u16 attribute to skb 101bfa83a9eSThomas Graf * nla_put_u32(skb, type, value) add u32 attribute to skb 10226837012SRolf Eike Beer * nla_put_u64_64bit(skb, type, 10350225243SNicolas Dichtel * value, padattr) add u64 attribute to skb 1044778e0beSJiri Pirko * nla_put_s8(skb, type, value) add s8 attribute to skb 1054778e0beSJiri Pirko * nla_put_s16(skb, type, value) add s16 attribute to skb 1064778e0beSJiri Pirko * nla_put_s32(skb, type, value) add s32 attribute to skb 107756a2f59SNicolas Dichtel * nla_put_s64(skb, type, value, 108756a2f59SNicolas Dichtel * padattr) add s64 attribute to skb 109bfa83a9eSThomas Graf * nla_put_string(skb, type, str) add string attribute to skb 110bfa83a9eSThomas Graf * nla_put_flag(skb, type) add flag attribute to skb 1112175d87cSNicolas Dichtel * nla_put_msecs(skb, type, jiffies, 1122175d87cSNicolas Dichtel * padattr) add msecs attribute to skb 113930345eaSJiri Benc * nla_put_in_addr(skb, type, addr) add IPv4 address attribute to skb 114930345eaSJiri Benc * nla_put_in6_addr(skb, type, addr) add IPv6 address attribute to skb 115bfa83a9eSThomas Graf * 116bfa83a9eSThomas Graf * Nested Attributes Construction: 117bfa83a9eSThomas Graf * nla_nest_start(skb, type) start a nested attribute 118bfa83a9eSThomas Graf * nla_nest_end(skb, nla) finalize a nested attribute 119bfa83a9eSThomas Graf * nla_nest_cancel(skb, nla) cancel nested attribute construction 120bfa83a9eSThomas Graf * 121bfa83a9eSThomas Graf * Attribute Length Calculations: 122bfa83a9eSThomas Graf * nla_attr_size(payload) length of attribute w/o padding 123bfa83a9eSThomas Graf * nla_total_size(payload) length of attribute w/ padding 124bfa83a9eSThomas Graf * nla_padlen(payload) length of padding 125bfa83a9eSThomas Graf * 126bfa83a9eSThomas Graf * Attribute Payload Access: 127bfa83a9eSThomas Graf * nla_data(nla) head of attribute payload 128bfa83a9eSThomas Graf * nla_len(nla) length of attribute payload 129bfa83a9eSThomas Graf * 130bfa83a9eSThomas Graf * Attribute Payload Access for Basic Types: 131bfa83a9eSThomas Graf * nla_get_u8(nla) get payload for a u8 attribute 132bfa83a9eSThomas Graf * nla_get_u16(nla) get payload for a u16 attribute 133bfa83a9eSThomas Graf * nla_get_u32(nla) get payload for a u32 attribute 134bfa83a9eSThomas Graf * nla_get_u64(nla) get payload for a u64 attribute 1354778e0beSJiri Pirko * nla_get_s8(nla) get payload for a s8 attribute 1364778e0beSJiri Pirko * nla_get_s16(nla) get payload for a s16 attribute 1374778e0beSJiri Pirko * nla_get_s32(nla) get payload for a s32 attribute 1384778e0beSJiri Pirko * nla_get_s64(nla) get payload for a s64 attribute 139bfa83a9eSThomas Graf * nla_get_flag(nla) return 1 if flag is true 140bfa83a9eSThomas Graf * nla_get_msecs(nla) get payload for a msecs attribute 141bfa83a9eSThomas Graf * 142bfa83a9eSThomas Graf * Attribute Misc: 143bfa83a9eSThomas Graf * nla_memcpy(dest, nla, count) copy attribute into memory 144bfa83a9eSThomas Graf * nla_memcmp(nla, data, size) compare attribute with memory area 145bfa83a9eSThomas Graf * nla_strlcpy(dst, nla, size) copy attribute to a sized string 146bfa83a9eSThomas Graf * nla_strcmp(nla, str) compare attribute with string 147bfa83a9eSThomas Graf * 148bfa83a9eSThomas Graf * Attribute Parsing: 149bfa83a9eSThomas Graf * nla_ok(nla, remaining) does nla fit into remaining bytes? 150bfa83a9eSThomas Graf * nla_next(nla, remaining) get next netlink attribute 151bfa83a9eSThomas Graf * nla_validate() validate a stream of attributes 1524fe5d5c0SPaul Moore * nla_validate_nested() validate a stream of nested attributes 153bfa83a9eSThomas Graf * nla_find() find attribute in stream of attributes 154fe4944e5SThomas Graf * nla_find_nested() find attribute in nested attributes 155bfa83a9eSThomas Graf * nla_parse() parse and validate stream of attrs 15643955a45SJohannes Berg * nla_parse_nested() parse nested attributes 157bfa83a9eSThomas Graf * nla_for_each_attr() loop over all attributes 15822acb19aSPaul Moore * nla_for_each_nested() loop over the nested attributes 159bfa83a9eSThomas Graf *========================================================================= 160bfa83a9eSThomas Graf */ 161bfa83a9eSThomas Graf 162bfa83a9eSThomas Graf /** 163bfa83a9eSThomas Graf * Standard attribute types to specify validation policy 164bfa83a9eSThomas Graf */ 165bfa83a9eSThomas Graf enum { 166bfa83a9eSThomas Graf NLA_UNSPEC, 167bfa83a9eSThomas Graf NLA_U8, 168bfa83a9eSThomas Graf NLA_U16, 169bfa83a9eSThomas Graf NLA_U32, 170bfa83a9eSThomas Graf NLA_U64, 171bfa83a9eSThomas Graf NLA_STRING, 172bfa83a9eSThomas Graf NLA_FLAG, 173bfa83a9eSThomas Graf NLA_MSECS, 174bfa83a9eSThomas Graf NLA_NESTED, 1751501d135SJohannes Berg NLA_NESTED_ARRAY, 176a5531a5dSThomas Graf NLA_NUL_STRING, 177d30045a0SJohannes Berg NLA_BINARY, 1784778e0beSJiri Pirko NLA_S8, 1794778e0beSJiri Pirko NLA_S16, 1804778e0beSJiri Pirko NLA_S32, 1814778e0beSJiri Pirko NLA_S64, 18264c83d83SJamal Hadi Salim NLA_BITFIELD32, 183568b742aSJohannes Berg NLA_REJECT, 184bfa83a9eSThomas Graf __NLA_TYPE_MAX, 185bfa83a9eSThomas Graf }; 186bfa83a9eSThomas Graf 187bfa83a9eSThomas Graf #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1) 188bfa83a9eSThomas Graf 189d06a09b9SJohannes Berg struct netlink_range_validation { 190d06a09b9SJohannes Berg u64 min, max; 191d06a09b9SJohannes Berg }; 192d06a09b9SJohannes Berg 193d06a09b9SJohannes Berg struct netlink_range_validation_signed { 194d06a09b9SJohannes Berg s64 min, max; 195d06a09b9SJohannes Berg }; 196d06a09b9SJohannes Berg 1973e48be05SJohannes Berg enum nla_policy_validation { 1983e48be05SJohannes Berg NLA_VALIDATE_NONE, 1993e48be05SJohannes Berg NLA_VALIDATE_RANGE, 2008aa26c57SJohannes Berg NLA_VALIDATE_RANGE_WARN_TOO_LONG, 2013e48be05SJohannes Berg NLA_VALIDATE_MIN, 2023e48be05SJohannes Berg NLA_VALIDATE_MAX, 203d06a09b9SJohannes Berg NLA_VALIDATE_RANGE_PTR, 20433188bd6SJohannes Berg NLA_VALIDATE_FUNCTION, 2053e48be05SJohannes Berg }; 2063e48be05SJohannes Berg 207bfa83a9eSThomas Graf /** 208bfa83a9eSThomas Graf * struct nla_policy - attribute validation policy 209bfa83a9eSThomas Graf * @type: Type of attribute or NLA_UNSPEC 2103e48be05SJohannes Berg * @validation_type: type of attribute validation done in addition to 21133188bd6SJohannes Berg * type-specific validation (e.g. range, function call), see 2123e48be05SJohannes Berg * &enum nla_policy_validation 213a5531a5dSThomas Graf * @len: Type specific length of payload 214bfa83a9eSThomas Graf * 215bfa83a9eSThomas Graf * Policies are defined as arrays of this struct, the array must be 216bfa83a9eSThomas Graf * accessible by attribute type up to the highest identifier to be expected. 217bfa83a9eSThomas Graf * 218a5531a5dSThomas Graf * Meaning of `len' field: 219a5531a5dSThomas Graf * NLA_STRING Maximum length of string 220a5531a5dSThomas Graf * NLA_NUL_STRING Maximum length of string (excluding NUL) 221a5531a5dSThomas Graf * NLA_FLAG Unused 222d30045a0SJohannes Berg * NLA_BINARY Maximum length of attribute payload 2238aa26c57SJohannes Berg * (but see also below with the validation type) 2241501d135SJohannes Berg * NLA_NESTED, 2251501d135SJohannes Berg * NLA_NESTED_ARRAY Length verification is done by checking len of 2269a659a35SJohannes Berg * nested header (or empty); len field is used if 22747a1494bSJohannes Berg * nested_policy is also used, for the max attr 2289a659a35SJohannes Berg * number in the nested policy. 2294b6cc728SJohannes Berg * NLA_U8, NLA_U16, 2304b6cc728SJohannes Berg * NLA_U32, NLA_U64, 2314778e0beSJiri Pirko * NLA_S8, NLA_S16, 2324778e0beSJiri Pirko * NLA_S32, NLA_S64, 2334b6cc728SJohannes Berg * NLA_MSECS Leaving the length field zero will verify the 2344b6cc728SJohannes Berg * given type fits, using it verifies minimum length 2354b6cc728SJohannes Berg * just like "All other" 236568b742aSJohannes Berg * NLA_BITFIELD32 Unused 237568b742aSJohannes Berg * NLA_REJECT Unused 2384b6cc728SJohannes Berg * All other Minimum length of attribute payload 239a5531a5dSThomas Graf * 24047a1494bSJohannes Berg * Meaning of validation union: 241568b742aSJohannes Berg * NLA_BITFIELD32 This is a 32-bit bitmap/bitselector attribute and 24247a1494bSJohannes Berg * `bitfield32_valid' is the u32 value of valid flags 24347a1494bSJohannes Berg * NLA_REJECT This attribute is always rejected and `reject_message' 244568b742aSJohannes Berg * may point to a string to report as the error instead 245568b742aSJohannes Berg * of the generic one in extended ACK. 24647a1494bSJohannes Berg * NLA_NESTED `nested_policy' to a nested policy to validate, must 24747a1494bSJohannes Berg * also set `len' to the max attribute number. Use the 24847a1494bSJohannes Berg * provided NLA_POLICY_NESTED() macro. 2499a659a35SJohannes Berg * Note that nla_parse() will validate, but of course not 2509a659a35SJohannes Berg * parse, the nested sub-policies. 25147a1494bSJohannes Berg * NLA_NESTED_ARRAY `nested_policy' points to a nested policy to validate, 25247a1494bSJohannes Berg * must also set `len' to the max attribute number. Use 25347a1494bSJohannes Berg * the provided NLA_POLICY_NESTED_ARRAY() macro. 25447a1494bSJohannes Berg * The difference to NLA_NESTED is the structure: 25547a1494bSJohannes Berg * NLA_NESTED has the nested attributes directly inside 25647a1494bSJohannes Berg * while an array has the nested attributes at another 25747a1494bSJohannes Berg * level down and the attribute types directly in the 25847a1494bSJohannes Berg * nesting don't matter. 2593e48be05SJohannes Berg * NLA_U8, 2603e48be05SJohannes Berg * NLA_U16, 2613e48be05SJohannes Berg * NLA_U32, 2623e48be05SJohannes Berg * NLA_U64, 2633e48be05SJohannes Berg * NLA_S8, 2643e48be05SJohannes Berg * NLA_S16, 2653e48be05SJohannes Berg * NLA_S32, 26647a1494bSJohannes Berg * NLA_S64 The `min' and `max' fields are used depending on the 26747a1494bSJohannes Berg * validation_type field, if that is min/max/range then 26847a1494bSJohannes Berg * the min, max or both are used (respectively) to check 2693e48be05SJohannes Berg * the value of the integer attribute. 2703e48be05SJohannes Berg * Note that in the interest of code simplicity and 2713e48be05SJohannes Berg * struct size both limits are s16, so you cannot 2723e48be05SJohannes Berg * enforce a range that doesn't fall within the range 2733e48be05SJohannes Berg * of s16 - do that as usual in the code instead. 27447a1494bSJohannes Berg * Use the NLA_POLICY_MIN(), NLA_POLICY_MAX() and 27547a1494bSJohannes Berg * NLA_POLICY_RANGE() macros. 276d06a09b9SJohannes Berg * NLA_U8, 277d06a09b9SJohannes Berg * NLA_U16, 278d06a09b9SJohannes Berg * NLA_U32, 279d06a09b9SJohannes Berg * NLA_U64 If the validation_type field instead is set to 280d06a09b9SJohannes Berg * NLA_VALIDATE_RANGE_PTR, `range' must be a pointer 281d06a09b9SJohannes Berg * to a struct netlink_range_validation that indicates 282d06a09b9SJohannes Berg * the min/max values. 283d06a09b9SJohannes Berg * Use NLA_POLICY_FULL_RANGE(). 284d06a09b9SJohannes Berg * NLA_S8, 285d06a09b9SJohannes Berg * NLA_S16, 286d06a09b9SJohannes Berg * NLA_S32, 287d06a09b9SJohannes Berg * NLA_S64 If the validation_type field instead is set to 288d06a09b9SJohannes Berg * NLA_VALIDATE_RANGE_PTR, `range_signed' must be a 289d06a09b9SJohannes Berg * pointer to a struct netlink_range_validation_signed 290d06a09b9SJohannes Berg * that indicates the min/max values. 291d06a09b9SJohannes Berg * Use NLA_POLICY_FULL_RANGE_SIGNED(). 2928aa26c57SJohannes Berg * 2938aa26c57SJohannes Berg * NLA_BINARY If the validation type is like the ones for integers 2948aa26c57SJohannes Berg * above, then the min/max length (not value like for 2958aa26c57SJohannes Berg * integers) of the attribute is enforced. 2968aa26c57SJohannes Berg * 2973e48be05SJohannes Berg * All other Unused - but note that it's a union 298568b742aSJohannes Berg * 29933188bd6SJohannes Berg * Meaning of `validate' field, use via NLA_POLICY_VALIDATE_FN: 30047a1494bSJohannes Berg * NLA_BINARY Validation function called for the attribute. 30133188bd6SJohannes Berg * All other Unused - but note that it's a union 30233188bd6SJohannes Berg * 303bfa83a9eSThomas Graf * Example: 30447a1494bSJohannes Berg * 30547a1494bSJohannes Berg * static const u32 myvalidflags = 0xff231023; 30647a1494bSJohannes Berg * 307b54452b0SAlexey Dobriyan * static const struct nla_policy my_policy[ATTR_MAX+1] = { 308bfa83a9eSThomas Graf * [ATTR_FOO] = { .type = NLA_U16 }, 309d30045a0SJohannes Berg * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ }, 3108aa26c57SJohannes Berg * [ATTR_BAZ] = NLA_POLICY_EXACT_LEN(sizeof(struct mystruct)), 31147a1494bSJohannes Berg * [ATTR_GOO] = NLA_POLICY_BITFIELD32(myvalidflags), 312bfa83a9eSThomas Graf * }; 313bfa83a9eSThomas Graf */ 314bfa83a9eSThomas Graf struct nla_policy { 3153e48be05SJohannes Berg u8 type; 3163e48be05SJohannes Berg u8 validation_type; 317a5531a5dSThomas Graf u16 len; 3183e48be05SJohannes Berg union { 31947a1494bSJohannes Berg const u32 bitfield32_valid; 32047a1494bSJohannes Berg const char *reject_message; 32147a1494bSJohannes Berg const struct nla_policy *nested_policy; 322d06a09b9SJohannes Berg struct netlink_range_validation *range; 323d06a09b9SJohannes Berg struct netlink_range_validation_signed *range_signed; 3243e48be05SJohannes Berg struct { 3253e48be05SJohannes Berg s16 min, max; 3263e48be05SJohannes Berg }; 32733188bd6SJohannes Berg int (*validate)(const struct nlattr *attr, 32833188bd6SJohannes Berg struct netlink_ext_ack *extack); 32956738f46SJohannes Berg /* This entry is special, and used for the attribute at index 0 33056738f46SJohannes Berg * only, and specifies special data about the policy, namely it 33156738f46SJohannes Berg * specifies the "boundary type" where strict length validation 33256738f46SJohannes Berg * starts for any attribute types >= this value, also, strict 33356738f46SJohannes Berg * nesting validation starts here. 33456738f46SJohannes Berg * 33556738f46SJohannes Berg * Additionally, it means that NLA_UNSPEC is actually NLA_REJECT 3368aa26c57SJohannes Berg * for any types >= this, so need to use NLA_POLICY_MIN_LEN() to 3378aa26c57SJohannes Berg * get the previous pure { .len = xyz } behaviour. The advantage 3388aa26c57SJohannes Berg * of this is that types not specified in the policy will be 3398aa26c57SJohannes Berg * rejected. 34056738f46SJohannes Berg * 34156738f46SJohannes Berg * For completely new families it should be set to 1 so that the 34256738f46SJohannes Berg * validation is enforced for all attributes. For existing ones 34356738f46SJohannes Berg * it should be set at least when new attributes are added to 34456738f46SJohannes Berg * the enum used by the policy, and be set to the new value that 34556738f46SJohannes Berg * was added to enforce strict validation from thereon. 34656738f46SJohannes Berg */ 34756738f46SJohannes Berg u16 strict_start_type; 3483e48be05SJohannes Berg }; 349bfa83a9eSThomas Graf }; 350bfa83a9eSThomas Graf 351b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR NLA_POLICY_EXACT_LEN(ETH_ALEN) 352b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR_COMPAT NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN) 353b60b87fcSJohannes Berg 35423323289SJohannes Berg #define _NLA_POLICY_NESTED(maxattr, policy) \ 35547a1494bSJohannes Berg { .type = NLA_NESTED, .nested_policy = policy, .len = maxattr } 35623323289SJohannes Berg #define _NLA_POLICY_NESTED_ARRAY(maxattr, policy) \ 35747a1494bSJohannes Berg { .type = NLA_NESTED_ARRAY, .nested_policy = policy, .len = maxattr } 35823323289SJohannes Berg #define NLA_POLICY_NESTED(policy) \ 35923323289SJohannes Berg _NLA_POLICY_NESTED(ARRAY_SIZE(policy) - 1, policy) 36023323289SJohannes Berg #define NLA_POLICY_NESTED_ARRAY(policy) \ 36123323289SJohannes Berg _NLA_POLICY_NESTED_ARRAY(ARRAY_SIZE(policy) - 1, policy) 36247a1494bSJohannes Berg #define NLA_POLICY_BITFIELD32(valid) \ 36347a1494bSJohannes Berg { .type = NLA_BITFIELD32, .bitfield32_valid = valid } 3649a659a35SJohannes Berg 3655886d932SJohannes Berg #define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition)) 3668aa26c57SJohannes Berg #define NLA_ENSURE_UINT_OR_BINARY_TYPE(tp) \ 367d06a09b9SJohannes Berg (__NLA_ENSURE(tp == NLA_U8 || tp == NLA_U16 || \ 368da4063bdSJohannes Berg tp == NLA_U32 || tp == NLA_U64 || \ 3698aa26c57SJohannes Berg tp == NLA_MSECS || \ 3708aa26c57SJohannes Berg tp == NLA_BINARY) + tp) 371d06a09b9SJohannes Berg #define NLA_ENSURE_SINT_TYPE(tp) \ 372d06a09b9SJohannes Berg (__NLA_ENSURE(tp == NLA_S8 || tp == NLA_S16 || \ 373d06a09b9SJohannes Berg tp == NLA_S32 || tp == NLA_S64) + tp) 3748aa26c57SJohannes Berg #define NLA_ENSURE_INT_OR_BINARY_TYPE(tp) \ 3753e48be05SJohannes Berg (__NLA_ENSURE(tp == NLA_S8 || tp == NLA_U8 || \ 3763e48be05SJohannes Berg tp == NLA_S16 || tp == NLA_U16 || \ 3773e48be05SJohannes Berg tp == NLA_S32 || tp == NLA_U32 || \ 378da4063bdSJohannes Berg tp == NLA_S64 || tp == NLA_U64 || \ 3798aa26c57SJohannes Berg tp == NLA_MSECS || \ 3808aa26c57SJohannes Berg tp == NLA_BINARY) + tp) 38133188bd6SJohannes Berg #define NLA_ENSURE_NO_VALIDATION_PTR(tp) \ 38233188bd6SJohannes Berg (__NLA_ENSURE(tp != NLA_BITFIELD32 && \ 38333188bd6SJohannes Berg tp != NLA_REJECT && \ 38433188bd6SJohannes Berg tp != NLA_NESTED && \ 38533188bd6SJohannes Berg tp != NLA_NESTED_ARRAY) + tp) 3863e48be05SJohannes Berg 3873e48be05SJohannes Berg #define NLA_POLICY_RANGE(tp, _min, _max) { \ 3888aa26c57SJohannes Berg .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp), \ 3893e48be05SJohannes Berg .validation_type = NLA_VALIDATE_RANGE, \ 3903e48be05SJohannes Berg .min = _min, \ 3913e48be05SJohannes Berg .max = _max \ 3923e48be05SJohannes Berg } 3933e48be05SJohannes Berg 394d06a09b9SJohannes Berg #define NLA_POLICY_FULL_RANGE(tp, _range) { \ 3958aa26c57SJohannes Berg .type = NLA_ENSURE_UINT_OR_BINARY_TYPE(tp), \ 396d06a09b9SJohannes Berg .validation_type = NLA_VALIDATE_RANGE_PTR, \ 397d06a09b9SJohannes Berg .range = _range, \ 398d06a09b9SJohannes Berg } 399d06a09b9SJohannes Berg 400d06a09b9SJohannes Berg #define NLA_POLICY_FULL_RANGE_SIGNED(tp, _range) { \ 401d06a09b9SJohannes Berg .type = NLA_ENSURE_SINT_TYPE(tp), \ 402d06a09b9SJohannes Berg .validation_type = NLA_VALIDATE_RANGE_PTR, \ 403d06a09b9SJohannes Berg .range_signed = _range, \ 404d06a09b9SJohannes Berg } 405d06a09b9SJohannes Berg 4063e48be05SJohannes Berg #define NLA_POLICY_MIN(tp, _min) { \ 4078aa26c57SJohannes Berg .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp), \ 4083e48be05SJohannes Berg .validation_type = NLA_VALIDATE_MIN, \ 4093e48be05SJohannes Berg .min = _min, \ 4103e48be05SJohannes Berg } 4113e48be05SJohannes Berg 4123e48be05SJohannes Berg #define NLA_POLICY_MAX(tp, _max) { \ 4138aa26c57SJohannes Berg .type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp), \ 4143e48be05SJohannes Berg .validation_type = NLA_VALIDATE_MAX, \ 4153e48be05SJohannes Berg .max = _max, \ 4163e48be05SJohannes Berg } 4173e48be05SJohannes Berg 41833188bd6SJohannes Berg #define NLA_POLICY_VALIDATE_FN(tp, fn, ...) { \ 41933188bd6SJohannes Berg .type = NLA_ENSURE_NO_VALIDATION_PTR(tp), \ 42033188bd6SJohannes Berg .validation_type = NLA_VALIDATE_FUNCTION, \ 42133188bd6SJohannes Berg .validate = fn, \ 42233188bd6SJohannes Berg .len = __VA_ARGS__ + 0, \ 42333188bd6SJohannes Berg } 42433188bd6SJohannes Berg 4258aa26c57SJohannes Berg #define NLA_POLICY_EXACT_LEN(_len) NLA_POLICY_RANGE(NLA_BINARY, _len, _len) 4268aa26c57SJohannes Berg #define NLA_POLICY_EXACT_LEN_WARN(_len) { \ 4278aa26c57SJohannes Berg .type = NLA_BINARY, \ 4288aa26c57SJohannes Berg .validation_type = NLA_VALIDATE_RANGE_WARN_TOO_LONG, \ 4298aa26c57SJohannes Berg .min = _len, \ 4308aa26c57SJohannes Berg .max = _len \ 4318aa26c57SJohannes Berg } 4328aa26c57SJohannes Berg #define NLA_POLICY_MIN_LEN(_len) NLA_POLICY_MIN(NLA_BINARY, _len) 4338aa26c57SJohannes Berg 4344e902c57SThomas Graf /** 4354e902c57SThomas Graf * struct nl_info - netlink source information 4364e902c57SThomas Graf * @nlh: Netlink message header of original request 4373de205cdSIdo Schimmel * @nl_net: Network namespace 43815e47304SEric W. Biederman * @portid: Netlink PORTID of requesting application 4393de205cdSIdo Schimmel * @skip_notify: Skip netlink notifications to user space 440c82481f7SIdo Schimmel * @skip_notify_kernel: Skip selected in-kernel notifications 4414e902c57SThomas Graf */ 4424e902c57SThomas Graf struct nl_info { 4434e902c57SThomas Graf struct nlmsghdr *nlh; 4444d1169c1SDenis V. Lunev struct net *nl_net; 44515e47304SEric W. Biederman u32 portid; 446c82481f7SIdo Schimmel u8 skip_notify:1, 447c82481f7SIdo Schimmel skip_notify_kernel:1; 4484e902c57SThomas Graf }; 4494e902c57SThomas Graf 4508cb08174SJohannes Berg /** 4518cb08174SJohannes Berg * enum netlink_validation - netlink message/attribute validation levels 4528cb08174SJohannes Berg * @NL_VALIDATE_LIBERAL: Old-style "be liberal" validation, not caring about 4538cb08174SJohannes Berg * extra data at the end of the message, attributes being longer than 4548cb08174SJohannes Berg * they should be, or unknown attributes being present. 4558cb08174SJohannes Berg * @NL_VALIDATE_TRAILING: Reject junk data encountered after attribute parsing. 4568cb08174SJohannes Berg * @NL_VALIDATE_MAXTYPE: Reject attributes > max type; Together with _TRAILING 4578cb08174SJohannes Berg * this is equivalent to the old nla_parse_strict()/nlmsg_parse_strict(). 4588cb08174SJohannes Berg * @NL_VALIDATE_UNSPEC: Reject attributes with NLA_UNSPEC in the policy. 4598cb08174SJohannes Berg * This can safely be set by the kernel when the given policy has no 4608cb08174SJohannes Berg * NLA_UNSPEC anymore, and can thus be used to ensure policy entries 4618cb08174SJohannes Berg * are enforced going forward. 4628cb08174SJohannes Berg * @NL_VALIDATE_STRICT_ATTRS: strict attribute policy parsing (e.g. 4638cb08174SJohannes Berg * U8, U16, U32 must have exact size, etc.) 464b424e432SMichal Kubecek * @NL_VALIDATE_NESTED: Check that NLA_F_NESTED is set for NLA_NESTED(_ARRAY) 465b424e432SMichal Kubecek * and unset for other policies. 4668cb08174SJohannes Berg */ 4678cb08174SJohannes Berg enum netlink_validation { 4688cb08174SJohannes Berg NL_VALIDATE_LIBERAL = 0, 4698cb08174SJohannes Berg NL_VALIDATE_TRAILING = BIT(0), 4708cb08174SJohannes Berg NL_VALIDATE_MAXTYPE = BIT(1), 4718cb08174SJohannes Berg NL_VALIDATE_UNSPEC = BIT(2), 4728cb08174SJohannes Berg NL_VALIDATE_STRICT_ATTRS = BIT(3), 473b424e432SMichal Kubecek NL_VALIDATE_NESTED = BIT(4), 4748cb08174SJohannes Berg }; 4758cb08174SJohannes Berg 4768cb08174SJohannes Berg #define NL_VALIDATE_DEPRECATED_STRICT (NL_VALIDATE_TRAILING |\ 4778cb08174SJohannes Berg NL_VALIDATE_MAXTYPE) 4788cb08174SJohannes Berg #define NL_VALIDATE_STRICT (NL_VALIDATE_TRAILING |\ 4798cb08174SJohannes Berg NL_VALIDATE_MAXTYPE |\ 4808cb08174SJohannes Berg NL_VALIDATE_UNSPEC |\ 481b424e432SMichal Kubecek NL_VALIDATE_STRICT_ATTRS |\ 482b424e432SMichal Kubecek NL_VALIDATE_NESTED) 4838cb08174SJohannes Berg 4844f69053bSJoe Perches int netlink_rcv_skb(struct sk_buff *skb, 4852d4bc933SJohannes Berg int (*cb)(struct sk_buff *, struct nlmsghdr *, 4862d4bc933SJohannes Berg struct netlink_ext_ack *)); 4874f69053bSJoe Perches int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid, 4884f69053bSJoe Perches unsigned int group, int report, gfp_t flags); 48982ace47aSThomas Graf 4908cb08174SJohannes Berg int __nla_validate(const struct nlattr *head, int len, int maxtype, 4918cb08174SJohannes Berg const struct nla_policy *policy, unsigned int validate, 492fceb6435SJohannes Berg struct netlink_ext_ack *extack); 4938cb08174SJohannes Berg int __nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, 4948cb08174SJohannes Berg int len, const struct nla_policy *policy, unsigned int validate, 495a5f6cba2SDavid Ahern struct netlink_ext_ack *extack); 4964f69053bSJoe Perches int nla_policy_len(const struct nla_policy *, int); 4974f69053bSJoe Perches struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype); 4984f69053bSJoe Perches size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize); 4992cf0c8b3SPhil Sutter char *nla_strdup(const struct nlattr *nla, gfp_t flags); 5004f69053bSJoe Perches int nla_memcpy(void *dest, const struct nlattr *src, int count); 5014f69053bSJoe Perches int nla_memcmp(const struct nlattr *nla, const void *data, size_t size); 5024f69053bSJoe Perches int nla_strcmp(const struct nlattr *nla, const char *str); 5034f69053bSJoe Perches struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 504089bf1a6SNicolas Dichtel struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype, 505089bf1a6SNicolas Dichtel int attrlen, int padattr); 5064f69053bSJoe Perches void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 5074f69053bSJoe Perches struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 508089bf1a6SNicolas Dichtel struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, 509089bf1a6SNicolas Dichtel int attrlen, int padattr); 5104f69053bSJoe Perches void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 5114f69053bSJoe Perches void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, 512fe4944e5SThomas Graf const void *data); 513089bf1a6SNicolas Dichtel void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 514089bf1a6SNicolas Dichtel const void *data, int padattr); 5154f69053bSJoe Perches void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 5164f69053bSJoe Perches int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data); 517089bf1a6SNicolas Dichtel int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 518089bf1a6SNicolas Dichtel const void *data, int padattr); 5194f69053bSJoe Perches int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 5204f69053bSJoe Perches int nla_append(struct sk_buff *skb, int attrlen, const void *data); 521bfa83a9eSThomas Graf 522bfa83a9eSThomas Graf /************************************************************************** 523bfa83a9eSThomas Graf * Netlink Messages 524bfa83a9eSThomas Graf **************************************************************************/ 525bfa83a9eSThomas Graf 526bfa83a9eSThomas Graf /** 527bfa83a9eSThomas Graf * nlmsg_msg_size - length of netlink message not including padding 528bfa83a9eSThomas Graf * @payload: length of message payload 529bfa83a9eSThomas Graf */ 530bfa83a9eSThomas Graf static inline int nlmsg_msg_size(int payload) 531bfa83a9eSThomas Graf { 532bfa83a9eSThomas Graf return NLMSG_HDRLEN + payload; 533bfa83a9eSThomas Graf } 534bfa83a9eSThomas Graf 535bfa83a9eSThomas Graf /** 536bfa83a9eSThomas Graf * nlmsg_total_size - length of netlink message including padding 537bfa83a9eSThomas Graf * @payload: length of message payload 538bfa83a9eSThomas Graf */ 539bfa83a9eSThomas Graf static inline int nlmsg_total_size(int payload) 540bfa83a9eSThomas Graf { 541bfa83a9eSThomas Graf return NLMSG_ALIGN(nlmsg_msg_size(payload)); 542bfa83a9eSThomas Graf } 543bfa83a9eSThomas Graf 544bfa83a9eSThomas Graf /** 545bfa83a9eSThomas Graf * nlmsg_padlen - length of padding at the message's tail 546bfa83a9eSThomas Graf * @payload: length of message payload 547bfa83a9eSThomas Graf */ 548bfa83a9eSThomas Graf static inline int nlmsg_padlen(int payload) 549bfa83a9eSThomas Graf { 550bfa83a9eSThomas Graf return nlmsg_total_size(payload) - nlmsg_msg_size(payload); 551bfa83a9eSThomas Graf } 552bfa83a9eSThomas Graf 553bfa83a9eSThomas Graf /** 554bfa83a9eSThomas Graf * nlmsg_data - head of message payload 55570f23fd6SJustin P. Mattock * @nlh: netlink message header 556bfa83a9eSThomas Graf */ 557bfa83a9eSThomas Graf static inline void *nlmsg_data(const struct nlmsghdr *nlh) 558bfa83a9eSThomas Graf { 559bfa83a9eSThomas Graf return (unsigned char *) nlh + NLMSG_HDRLEN; 560bfa83a9eSThomas Graf } 561bfa83a9eSThomas Graf 562bfa83a9eSThomas Graf /** 563bfa83a9eSThomas Graf * nlmsg_len - length of message payload 564bfa83a9eSThomas Graf * @nlh: netlink message header 565bfa83a9eSThomas Graf */ 566bfa83a9eSThomas Graf static inline int nlmsg_len(const struct nlmsghdr *nlh) 567bfa83a9eSThomas Graf { 568bfa83a9eSThomas Graf return nlh->nlmsg_len - NLMSG_HDRLEN; 569bfa83a9eSThomas Graf } 570bfa83a9eSThomas Graf 571bfa83a9eSThomas Graf /** 572bfa83a9eSThomas Graf * nlmsg_attrdata - head of attributes data 573bfa83a9eSThomas Graf * @nlh: netlink message header 574bfa83a9eSThomas Graf * @hdrlen: length of family specific header 575bfa83a9eSThomas Graf */ 576bfa83a9eSThomas Graf static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, 577bfa83a9eSThomas Graf int hdrlen) 578bfa83a9eSThomas Graf { 579bfa83a9eSThomas Graf unsigned char *data = nlmsg_data(nlh); 580bfa83a9eSThomas Graf return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen)); 581bfa83a9eSThomas Graf } 582bfa83a9eSThomas Graf 583bfa83a9eSThomas Graf /** 584bfa83a9eSThomas Graf * nlmsg_attrlen - length of attributes data 585bfa83a9eSThomas Graf * @nlh: netlink message header 586bfa83a9eSThomas Graf * @hdrlen: length of family specific header 587bfa83a9eSThomas Graf */ 588bfa83a9eSThomas Graf static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen) 589bfa83a9eSThomas Graf { 590bfa83a9eSThomas Graf return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen); 591bfa83a9eSThomas Graf } 592bfa83a9eSThomas Graf 593bfa83a9eSThomas Graf /** 594bfa83a9eSThomas Graf * nlmsg_ok - check if the netlink message fits into the remaining bytes 595bfa83a9eSThomas Graf * @nlh: netlink message header 596bfa83a9eSThomas Graf * @remaining: number of bytes remaining in message stream 597bfa83a9eSThomas Graf */ 598bfa83a9eSThomas Graf static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining) 599bfa83a9eSThomas Graf { 600619e803dSVegard Nossum return (remaining >= (int) sizeof(struct nlmsghdr) && 601bfa83a9eSThomas Graf nlh->nlmsg_len >= sizeof(struct nlmsghdr) && 602bfa83a9eSThomas Graf nlh->nlmsg_len <= remaining); 603bfa83a9eSThomas Graf } 604bfa83a9eSThomas Graf 605bfa83a9eSThomas Graf /** 606bfa83a9eSThomas Graf * nlmsg_next - next netlink message in message stream 607bfa83a9eSThomas Graf * @nlh: netlink message header 608bfa83a9eSThomas Graf * @remaining: number of bytes remaining in message stream 609bfa83a9eSThomas Graf * 610bfa83a9eSThomas Graf * Returns the next netlink message in the message stream and 611bfa83a9eSThomas Graf * decrements remaining by the size of the current message. 612bfa83a9eSThomas Graf */ 6133654654fSJan Engelhardt static inline struct nlmsghdr * 6143654654fSJan Engelhardt nlmsg_next(const struct nlmsghdr *nlh, int *remaining) 615bfa83a9eSThomas Graf { 616bfa83a9eSThomas Graf int totlen = NLMSG_ALIGN(nlh->nlmsg_len); 617bfa83a9eSThomas Graf 618bfa83a9eSThomas Graf *remaining -= totlen; 619bfa83a9eSThomas Graf 620bfa83a9eSThomas Graf return (struct nlmsghdr *) ((unsigned char *) nlh + totlen); 621bfa83a9eSThomas Graf } 622bfa83a9eSThomas Graf 623bfa83a9eSThomas Graf /** 6243de64403SJohannes Berg * nla_parse - Parse a stream of attributes into a tb buffer 6253de64403SJohannes Berg * @tb: destination array with maxtype+1 elements 6263de64403SJohannes Berg * @maxtype: maximum attribute type to be expected 6273de64403SJohannes Berg * @head: head of attribute stream 6283de64403SJohannes Berg * @len: length of attribute stream 6293de64403SJohannes Berg * @policy: validation policy 6303de64403SJohannes Berg * @extack: extended ACK pointer 6313de64403SJohannes Berg * 6323de64403SJohannes Berg * Parses a stream of attributes and stores a pointer to each attribute in 6333de64403SJohannes Berg * the tb array accessible via the attribute type. Attributes with a type 6343de64403SJohannes Berg * exceeding maxtype will be rejected, policy must be specified, attributes 6353de64403SJohannes Berg * will be validated in the strictest way possible. 6363de64403SJohannes Berg * 6373de64403SJohannes Berg * Returns 0 on success or a negative error code. 6383de64403SJohannes Berg */ 6393de64403SJohannes Berg static inline int nla_parse(struct nlattr **tb, int maxtype, 6403de64403SJohannes Berg const struct nlattr *head, int len, 6413de64403SJohannes Berg const struct nla_policy *policy, 6423de64403SJohannes Berg struct netlink_ext_ack *extack) 6433de64403SJohannes Berg { 6443de64403SJohannes Berg return __nla_parse(tb, maxtype, head, len, policy, 6453de64403SJohannes Berg NL_VALIDATE_STRICT, extack); 6463de64403SJohannes Berg } 6473de64403SJohannes Berg 6483de64403SJohannes Berg /** 6498cb08174SJohannes Berg * nla_parse_deprecated - Parse a stream of attributes into a tb buffer 6508cb08174SJohannes Berg * @tb: destination array with maxtype+1 elements 6518cb08174SJohannes Berg * @maxtype: maximum attribute type to be expected 6528cb08174SJohannes Berg * @head: head of attribute stream 6538cb08174SJohannes Berg * @len: length of attribute stream 6548cb08174SJohannes Berg * @policy: validation policy 6558cb08174SJohannes Berg * @extack: extended ACK pointer 6568cb08174SJohannes Berg * 6578cb08174SJohannes Berg * Parses a stream of attributes and stores a pointer to each attribute in 6588cb08174SJohannes Berg * the tb array accessible via the attribute type. Attributes with a type 6598cb08174SJohannes Berg * exceeding maxtype will be ignored and attributes from the policy are not 6608cb08174SJohannes Berg * always strictly validated (only for new attributes). 6618cb08174SJohannes Berg * 6628cb08174SJohannes Berg * Returns 0 on success or a negative error code. 6638cb08174SJohannes Berg */ 6648cb08174SJohannes Berg static inline int nla_parse_deprecated(struct nlattr **tb, int maxtype, 6658cb08174SJohannes Berg const struct nlattr *head, int len, 6668cb08174SJohannes Berg const struct nla_policy *policy, 6678cb08174SJohannes Berg struct netlink_ext_ack *extack) 6688cb08174SJohannes Berg { 6698cb08174SJohannes Berg return __nla_parse(tb, maxtype, head, len, policy, 6708cb08174SJohannes Berg NL_VALIDATE_LIBERAL, extack); 6718cb08174SJohannes Berg } 6728cb08174SJohannes Berg 6738cb08174SJohannes Berg /** 6748cb08174SJohannes Berg * nla_parse_deprecated_strict - Parse a stream of attributes into a tb buffer 6758cb08174SJohannes Berg * @tb: destination array with maxtype+1 elements 6768cb08174SJohannes Berg * @maxtype: maximum attribute type to be expected 6778cb08174SJohannes Berg * @head: head of attribute stream 6788cb08174SJohannes Berg * @len: length of attribute stream 6798cb08174SJohannes Berg * @policy: validation policy 6808cb08174SJohannes Berg * @extack: extended ACK pointer 6818cb08174SJohannes Berg * 6828cb08174SJohannes Berg * Parses a stream of attributes and stores a pointer to each attribute in 6838cb08174SJohannes Berg * the tb array accessible via the attribute type. Attributes with a type 6848cb08174SJohannes Berg * exceeding maxtype will be rejected as well as trailing data, but the 6858cb08174SJohannes Berg * policy is not completely strictly validated (only for new attributes). 6868cb08174SJohannes Berg * 6878cb08174SJohannes Berg * Returns 0 on success or a negative error code. 6888cb08174SJohannes Berg */ 6898cb08174SJohannes Berg static inline int nla_parse_deprecated_strict(struct nlattr **tb, int maxtype, 6908cb08174SJohannes Berg const struct nlattr *head, 6918cb08174SJohannes Berg int len, 6928cb08174SJohannes Berg const struct nla_policy *policy, 6938cb08174SJohannes Berg struct netlink_ext_ack *extack) 6948cb08174SJohannes Berg { 6958cb08174SJohannes Berg return __nla_parse(tb, maxtype, head, len, policy, 6968cb08174SJohannes Berg NL_VALIDATE_DEPRECATED_STRICT, extack); 6978cb08174SJohannes Berg } 6988cb08174SJohannes Berg 6998cb08174SJohannes Berg /** 7008cb08174SJohannes Berg * __nlmsg_parse - parse attributes of a netlink message 701bfa83a9eSThomas Graf * @nlh: netlink message header 702bfa83a9eSThomas Graf * @hdrlen: length of family specific header 703bfa83a9eSThomas Graf * @tb: destination array with maxtype+1 elements 704bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 705bfa83a9eSThomas Graf * @policy: validation policy 7068cb08174SJohannes Berg * @validate: validation strictness 707fceb6435SJohannes Berg * @extack: extended ACK report struct 708bfa83a9eSThomas Graf * 709bfa83a9eSThomas Graf * See nla_parse() 710bfa83a9eSThomas Graf */ 7118cb08174SJohannes Berg static inline int __nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen, 712bfa83a9eSThomas Graf struct nlattr *tb[], int maxtype, 713fceb6435SJohannes Berg const struct nla_policy *policy, 7148cb08174SJohannes Berg unsigned int validate, 715fceb6435SJohannes Berg struct netlink_ext_ack *extack) 716bfa83a9eSThomas Graf { 7173d0d4337SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) { 7183d0d4337SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header length"); 719bfa83a9eSThomas Graf return -EINVAL; 7203d0d4337SDavid Ahern } 721bfa83a9eSThomas Graf 7228cb08174SJohannes Berg return __nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), 7238cb08174SJohannes Berg nlmsg_attrlen(nlh, hdrlen), policy, validate, 7248cb08174SJohannes Berg extack); 725bfa83a9eSThomas Graf } 726bfa83a9eSThomas Graf 7278cb08174SJohannes Berg /** 7283de64403SJohannes Berg * nlmsg_parse - parse attributes of a netlink message 7293de64403SJohannes Berg * @nlh: netlink message header 7303de64403SJohannes Berg * @hdrlen: length of family specific header 7313de64403SJohannes Berg * @tb: destination array with maxtype+1 elements 7323de64403SJohannes Berg * @maxtype: maximum attribute type to be expected 7333de64403SJohannes Berg * @validate: validation strictness 7343de64403SJohannes Berg * @extack: extended ACK report struct 7353de64403SJohannes Berg * 7363de64403SJohannes Berg * See nla_parse() 7373de64403SJohannes Berg */ 7383de64403SJohannes Berg static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen, 7393de64403SJohannes Berg struct nlattr *tb[], int maxtype, 7403de64403SJohannes Berg const struct nla_policy *policy, 7413de64403SJohannes Berg struct netlink_ext_ack *extack) 7423de64403SJohannes Berg { 743d00ee64eSDavid Ahern return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy, 7443de64403SJohannes Berg NL_VALIDATE_STRICT, extack); 7453de64403SJohannes Berg } 7463de64403SJohannes Berg 7473de64403SJohannes Berg /** 7488cb08174SJohannes Berg * nlmsg_parse_deprecated - parse attributes of a netlink message 7498cb08174SJohannes Berg * @nlh: netlink message header 7508cb08174SJohannes Berg * @hdrlen: length of family specific header 7518cb08174SJohannes Berg * @tb: destination array with maxtype+1 elements 7528cb08174SJohannes Berg * @maxtype: maximum attribute type to be expected 7538cb08174SJohannes Berg * @extack: extended ACK report struct 7548cb08174SJohannes Berg * 7558cb08174SJohannes Berg * See nla_parse_deprecated() 7568cb08174SJohannes Berg */ 7578cb08174SJohannes Berg static inline int nlmsg_parse_deprecated(const struct nlmsghdr *nlh, int hdrlen, 758a5f6cba2SDavid Ahern struct nlattr *tb[], int maxtype, 759a5f6cba2SDavid Ahern const struct nla_policy *policy, 760a5f6cba2SDavid Ahern struct netlink_ext_ack *extack) 761a5f6cba2SDavid Ahern { 7628cb08174SJohannes Berg return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy, 7638cb08174SJohannes Berg NL_VALIDATE_LIBERAL, extack); 764a5f6cba2SDavid Ahern } 765a5f6cba2SDavid Ahern 7668cb08174SJohannes Berg /** 7678cb08174SJohannes Berg * nlmsg_parse_deprecated_strict - parse attributes of a netlink message 7688cb08174SJohannes Berg * @nlh: netlink message header 7698cb08174SJohannes Berg * @hdrlen: length of family specific header 7708cb08174SJohannes Berg * @tb: destination array with maxtype+1 elements 7718cb08174SJohannes Berg * @maxtype: maximum attribute type to be expected 7728cb08174SJohannes Berg * @extack: extended ACK report struct 7738cb08174SJohannes Berg * 7748cb08174SJohannes Berg * See nla_parse_deprecated_strict() 7758cb08174SJohannes Berg */ 7768cb08174SJohannes Berg static inline int 7778cb08174SJohannes Berg nlmsg_parse_deprecated_strict(const struct nlmsghdr *nlh, int hdrlen, 7788cb08174SJohannes Berg struct nlattr *tb[], int maxtype, 7798cb08174SJohannes Berg const struct nla_policy *policy, 7808cb08174SJohannes Berg struct netlink_ext_ack *extack) 7818cb08174SJohannes Berg { 7828cb08174SJohannes Berg return __nlmsg_parse(nlh, hdrlen, tb, maxtype, policy, 7838cb08174SJohannes Berg NL_VALIDATE_DEPRECATED_STRICT, extack); 784a5f6cba2SDavid Ahern } 785a5f6cba2SDavid Ahern 786bfa83a9eSThomas Graf /** 787bfa83a9eSThomas Graf * nlmsg_find_attr - find a specific attribute in a netlink message 788bfa83a9eSThomas Graf * @nlh: netlink message header 789bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 790bfa83a9eSThomas Graf * @attrtype: type of attribute to look for 791bfa83a9eSThomas Graf * 792bfa83a9eSThomas Graf * Returns the first attribute which matches the specified type. 793bfa83a9eSThomas Graf */ 7946b8c92baSNelson Elhage static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh, 795bfa83a9eSThomas Graf int hdrlen, int attrtype) 796bfa83a9eSThomas Graf { 797bfa83a9eSThomas Graf return nla_find(nlmsg_attrdata(nlh, hdrlen), 798bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), attrtype); 799bfa83a9eSThomas Graf } 800bfa83a9eSThomas Graf 801bfa83a9eSThomas Graf /** 8028cb08174SJohannes Berg * nla_validate_deprecated - Validate a stream of attributes 8038cb08174SJohannes Berg * @head: head of attribute stream 8048cb08174SJohannes Berg * @len: length of attribute stream 8058cb08174SJohannes Berg * @maxtype: maximum attribute type to be expected 8068cb08174SJohannes Berg * @policy: validation policy 8078cb08174SJohannes Berg * @validate: validation strictness 8088cb08174SJohannes Berg * @extack: extended ACK report struct 8098cb08174SJohannes Berg * 8108cb08174SJohannes Berg * Validates all attributes in the specified attribute stream against the 8118cb08174SJohannes Berg * specified policy. Validation is done in liberal mode. 8128cb08174SJohannes Berg * See documenation of struct nla_policy for more details. 8138cb08174SJohannes Berg * 8148cb08174SJohannes Berg * Returns 0 on success or a negative error code. 8158cb08174SJohannes Berg */ 8168cb08174SJohannes Berg static inline int nla_validate_deprecated(const struct nlattr *head, int len, 8178cb08174SJohannes Berg int maxtype, 8188cb08174SJohannes Berg const struct nla_policy *policy, 8198cb08174SJohannes Berg struct netlink_ext_ack *extack) 8208cb08174SJohannes Berg { 8218cb08174SJohannes Berg return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_LIBERAL, 8228cb08174SJohannes Berg extack); 8238cb08174SJohannes Berg } 8248cb08174SJohannes Berg 8253de64403SJohannes Berg /** 8263de64403SJohannes Berg * nla_validate - Validate a stream of attributes 8273de64403SJohannes Berg * @head: head of attribute stream 8283de64403SJohannes Berg * @len: length of attribute stream 8293de64403SJohannes Berg * @maxtype: maximum attribute type to be expected 8303de64403SJohannes Berg * @policy: validation policy 8313de64403SJohannes Berg * @validate: validation strictness 8323de64403SJohannes Berg * @extack: extended ACK report struct 8333de64403SJohannes Berg * 8343de64403SJohannes Berg * Validates all attributes in the specified attribute stream against the 8353de64403SJohannes Berg * specified policy. Validation is done in strict mode. 8363de64403SJohannes Berg * See documenation of struct nla_policy for more details. 8373de64403SJohannes Berg * 8383de64403SJohannes Berg * Returns 0 on success or a negative error code. 8393de64403SJohannes Berg */ 8403de64403SJohannes Berg static inline int nla_validate(const struct nlattr *head, int len, int maxtype, 8413de64403SJohannes Berg const struct nla_policy *policy, 8423de64403SJohannes Berg struct netlink_ext_ack *extack) 8433de64403SJohannes Berg { 8443de64403SJohannes Berg return __nla_validate(head, len, maxtype, policy, NL_VALIDATE_STRICT, 8453de64403SJohannes Berg extack); 8463de64403SJohannes Berg } 8478cb08174SJohannes Berg 8488cb08174SJohannes Berg /** 8498cb08174SJohannes Berg * nlmsg_validate_deprecated - validate a netlink message including attributes 850bfa83a9eSThomas Graf * @nlh: netlinket message header 851bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 852bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 853bfa83a9eSThomas Graf * @policy: validation policy 854fceb6435SJohannes Berg * @extack: extended ACK report struct 855bfa83a9eSThomas Graf */ 8568cb08174SJohannes Berg static inline int nlmsg_validate_deprecated(const struct nlmsghdr *nlh, 8573654654fSJan Engelhardt int hdrlen, int maxtype, 858fceb6435SJohannes Berg const struct nla_policy *policy, 859fceb6435SJohannes Berg struct netlink_ext_ack *extack) 860bfa83a9eSThomas Graf { 861bfa83a9eSThomas Graf if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) 862bfa83a9eSThomas Graf return -EINVAL; 863bfa83a9eSThomas Graf 8648cb08174SJohannes Berg return __nla_validate(nlmsg_attrdata(nlh, hdrlen), 8658cb08174SJohannes Berg nlmsg_attrlen(nlh, hdrlen), maxtype, 8668cb08174SJohannes Berg policy, NL_VALIDATE_LIBERAL, extack); 867bfa83a9eSThomas Graf } 868bfa83a9eSThomas Graf 8698cb08174SJohannes Berg 8708cb08174SJohannes Berg 871bfa83a9eSThomas Graf /** 87297676b6bSThomas Graf * nlmsg_report - need to report back to application? 87397676b6bSThomas Graf * @nlh: netlink message header 87497676b6bSThomas Graf * 87597676b6bSThomas Graf * Returns 1 if a report back to the application is requested. 87697676b6bSThomas Graf */ 8773a6c2b41SPatrick McHardy static inline int nlmsg_report(const struct nlmsghdr *nlh) 87897676b6bSThomas Graf { 87997676b6bSThomas Graf return !!(nlh->nlmsg_flags & NLM_F_ECHO); 88097676b6bSThomas Graf } 88197676b6bSThomas Graf 88297676b6bSThomas Graf /** 883bfa83a9eSThomas Graf * nlmsg_for_each_attr - iterate over a stream of attributes 884bfa83a9eSThomas Graf * @pos: loop counter, set to current attribute 885bfa83a9eSThomas Graf * @nlh: netlink message header 886bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 887bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 888bfa83a9eSThomas Graf */ 889bfa83a9eSThomas Graf #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \ 890bfa83a9eSThomas Graf nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \ 891bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), rem) 892bfa83a9eSThomas Graf 893bfa83a9eSThomas Graf /** 894bfa83a9eSThomas Graf * nlmsg_put - Add a new netlink message to an skb 895bfa83a9eSThomas Graf * @skb: socket buffer to store message in 8962c6ba4b1SNicolas Dichtel * @portid: netlink PORTID of requesting application 897bfa83a9eSThomas Graf * @seq: sequence number of message 898bfa83a9eSThomas Graf * @type: message type 899bfa83a9eSThomas Graf * @payload: length of message payload 900bfa83a9eSThomas Graf * @flags: message flags 901bfa83a9eSThomas Graf * 902bfa83a9eSThomas Graf * Returns NULL if the tailroom of the skb is insufficient to store 903bfa83a9eSThomas Graf * the message header and payload. 904bfa83a9eSThomas Graf */ 90515e47304SEric W. Biederman static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, 906bfa83a9eSThomas Graf int type, int payload, int flags) 907bfa83a9eSThomas Graf { 908bfa83a9eSThomas Graf if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload))) 909bfa83a9eSThomas Graf return NULL; 910bfa83a9eSThomas Graf 91115e47304SEric W. Biederman return __nlmsg_put(skb, portid, seq, type, payload, flags); 912bfa83a9eSThomas Graf } 913bfa83a9eSThomas Graf 914bfa83a9eSThomas Graf /** 915bfa83a9eSThomas Graf * nlmsg_put_answer - Add a new callback based netlink message to an skb 916bfa83a9eSThomas Graf * @skb: socket buffer to store message in 917bfa83a9eSThomas Graf * @cb: netlink callback 918bfa83a9eSThomas Graf * @type: message type 919bfa83a9eSThomas Graf * @payload: length of message payload 920bfa83a9eSThomas Graf * @flags: message flags 921bfa83a9eSThomas Graf * 922bfa83a9eSThomas Graf * Returns NULL if the tailroom of the skb is insufficient to store 923bfa83a9eSThomas Graf * the message header and payload. 924bfa83a9eSThomas Graf */ 925bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb, 926bfa83a9eSThomas Graf struct netlink_callback *cb, 927bfa83a9eSThomas Graf int type, int payload, 928bfa83a9eSThomas Graf int flags) 929bfa83a9eSThomas Graf { 93015e47304SEric W. Biederman return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 931bfa83a9eSThomas Graf type, payload, flags); 932bfa83a9eSThomas Graf } 933bfa83a9eSThomas Graf 934bfa83a9eSThomas Graf /** 935bfa83a9eSThomas Graf * nlmsg_new - Allocate a new netlink message 936339bf98fSThomas Graf * @payload: size of the message payload 937fe4944e5SThomas Graf * @flags: the type of memory to allocate. 938bfa83a9eSThomas Graf * 939339bf98fSThomas Graf * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known 940339bf98fSThomas Graf * and a good default is needed. 941bfa83a9eSThomas Graf */ 942339bf98fSThomas Graf static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags) 943bfa83a9eSThomas Graf { 944339bf98fSThomas Graf return alloc_skb(nlmsg_total_size(payload), flags); 945bfa83a9eSThomas Graf } 946bfa83a9eSThomas Graf 947bfa83a9eSThomas Graf /** 948bfa83a9eSThomas Graf * nlmsg_end - Finalize a netlink message 949bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 950bfa83a9eSThomas Graf * @nlh: netlink message header 951bfa83a9eSThomas Graf * 952bfa83a9eSThomas Graf * Corrects the netlink message header to include the appeneded 953bfa83a9eSThomas Graf * attributes. Only necessary if attributes have been added to 954bfa83a9eSThomas Graf * the message. 955bfa83a9eSThomas Graf */ 956053c095aSJohannes Berg static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh) 957bfa83a9eSThomas Graf { 95827a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh; 959bfa83a9eSThomas Graf } 960bfa83a9eSThomas Graf 961bfa83a9eSThomas Graf /** 962fe4944e5SThomas Graf * nlmsg_get_pos - return current position in netlink message 963fe4944e5SThomas Graf * @skb: socket buffer the message is stored in 964fe4944e5SThomas Graf * 965fe4944e5SThomas Graf * Returns a pointer to the current tail of the message. 966fe4944e5SThomas Graf */ 967fe4944e5SThomas Graf static inline void *nlmsg_get_pos(struct sk_buff *skb) 968fe4944e5SThomas Graf { 96927a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 970fe4944e5SThomas Graf } 971fe4944e5SThomas Graf 972fe4944e5SThomas Graf /** 973fe4944e5SThomas Graf * nlmsg_trim - Trim message to a mark 974fe4944e5SThomas Graf * @skb: socket buffer the message is stored in 975fe4944e5SThomas Graf * @mark: mark to trim to 976fe4944e5SThomas Graf * 977bc3ed28cSThomas Graf * Trims the message to the provided mark. 978fe4944e5SThomas Graf */ 979bc3ed28cSThomas Graf static inline void nlmsg_trim(struct sk_buff *skb, const void *mark) 980fe4944e5SThomas Graf { 981149118d8SThomas Graf if (mark) { 982149118d8SThomas Graf WARN_ON((unsigned char *) mark < skb->data); 983fe4944e5SThomas Graf skb_trim(skb, (unsigned char *) mark - skb->data); 984fe4944e5SThomas Graf } 985149118d8SThomas Graf } 986fe4944e5SThomas Graf 987fe4944e5SThomas Graf /** 988bfa83a9eSThomas Graf * nlmsg_cancel - Cancel construction of a netlink message 989bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 990bfa83a9eSThomas Graf * @nlh: netlink message header 991bfa83a9eSThomas Graf * 992bfa83a9eSThomas Graf * Removes the complete netlink message including all 993bc3ed28cSThomas Graf * attributes from the socket buffer again. 994bfa83a9eSThomas Graf */ 995bc3ed28cSThomas Graf static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh) 996bfa83a9eSThomas Graf { 997bc3ed28cSThomas Graf nlmsg_trim(skb, nlh); 998bfa83a9eSThomas Graf } 999bfa83a9eSThomas Graf 1000bfa83a9eSThomas Graf /** 1001bfa83a9eSThomas Graf * nlmsg_free - free a netlink message 1002bfa83a9eSThomas Graf * @skb: socket buffer of netlink message 1003bfa83a9eSThomas Graf */ 1004bfa83a9eSThomas Graf static inline void nlmsg_free(struct sk_buff *skb) 1005bfa83a9eSThomas Graf { 1006bfa83a9eSThomas Graf kfree_skb(skb); 1007bfa83a9eSThomas Graf } 1008bfa83a9eSThomas Graf 1009bfa83a9eSThomas Graf /** 1010bfa83a9eSThomas Graf * nlmsg_multicast - multicast a netlink message 1011bfa83a9eSThomas Graf * @sk: netlink socket to spread messages to 1012bfa83a9eSThomas Graf * @skb: netlink message as socket buffer 101315e47304SEric W. Biederman * @portid: own netlink portid to avoid sending to yourself 1014bfa83a9eSThomas Graf * @group: multicast group id 1015d387f6adSThomas Graf * @flags: allocation flags 1016bfa83a9eSThomas Graf */ 1017bfa83a9eSThomas Graf static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, 101815e47304SEric W. Biederman u32 portid, unsigned int group, gfp_t flags) 1019bfa83a9eSThomas Graf { 1020bfa83a9eSThomas Graf int err; 1021bfa83a9eSThomas Graf 1022bfa83a9eSThomas Graf NETLINK_CB(skb).dst_group = group; 1023bfa83a9eSThomas Graf 102415e47304SEric W. Biederman err = netlink_broadcast(sk, skb, portid, group, flags); 1025bfa83a9eSThomas Graf if (err > 0) 1026bfa83a9eSThomas Graf err = 0; 1027bfa83a9eSThomas Graf 1028bfa83a9eSThomas Graf return err; 1029bfa83a9eSThomas Graf } 1030bfa83a9eSThomas Graf 1031bfa83a9eSThomas Graf /** 1032bfa83a9eSThomas Graf * nlmsg_unicast - unicast a netlink message 1033bfa83a9eSThomas Graf * @sk: netlink socket to spread message to 1034bfa83a9eSThomas Graf * @skb: netlink message as socket buffer 103515e47304SEric W. Biederman * @portid: netlink portid of the destination socket 1036bfa83a9eSThomas Graf */ 103715e47304SEric W. Biederman static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid) 1038bfa83a9eSThomas Graf { 1039bfa83a9eSThomas Graf int err; 1040bfa83a9eSThomas Graf 104115e47304SEric W. Biederman err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT); 1042bfa83a9eSThomas Graf if (err > 0) 1043bfa83a9eSThomas Graf err = 0; 1044bfa83a9eSThomas Graf 1045bfa83a9eSThomas Graf return err; 1046bfa83a9eSThomas Graf } 1047bfa83a9eSThomas Graf 1048bfa83a9eSThomas Graf /** 1049bfa83a9eSThomas Graf * nlmsg_for_each_msg - iterate over a stream of messages 1050bfa83a9eSThomas Graf * @pos: loop counter, set to current message 1051bfa83a9eSThomas Graf * @head: head of message stream 1052bfa83a9eSThomas Graf * @len: length of message stream 1053bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 1054bfa83a9eSThomas Graf */ 1055bfa83a9eSThomas Graf #define nlmsg_for_each_msg(pos, head, len, rem) \ 1056bfa83a9eSThomas Graf for (pos = head, rem = len; \ 1057bfa83a9eSThomas Graf nlmsg_ok(pos, rem); \ 1058bfa83a9eSThomas Graf pos = nlmsg_next(pos, &(rem))) 1059bfa83a9eSThomas Graf 1060670dc283SJohannes Berg /** 1061670dc283SJohannes Berg * nl_dump_check_consistent - check if sequence is consistent and advertise if not 1062670dc283SJohannes Berg * @cb: netlink callback structure that stores the sequence number 1063670dc283SJohannes Berg * @nlh: netlink message header to write the flag to 1064670dc283SJohannes Berg * 1065670dc283SJohannes Berg * This function checks if the sequence (generation) number changed during dump 1066670dc283SJohannes Berg * and if it did, advertises it in the netlink message header. 1067670dc283SJohannes Berg * 1068670dc283SJohannes Berg * The correct way to use it is to set cb->seq to the generation counter when 1069670dc283SJohannes Berg * all locks for dumping have been acquired, and then call this function for 1070670dc283SJohannes Berg * each message that is generated. 1071670dc283SJohannes Berg * 1072670dc283SJohannes Berg * Note that due to initialisation concerns, 0 is an invalid sequence number 1073670dc283SJohannes Berg * and must not be used by code that uses this functionality. 1074670dc283SJohannes Berg */ 1075670dc283SJohannes Berg static inline void 1076670dc283SJohannes Berg nl_dump_check_consistent(struct netlink_callback *cb, 1077670dc283SJohannes Berg struct nlmsghdr *nlh) 1078670dc283SJohannes Berg { 1079670dc283SJohannes Berg if (cb->prev_seq && cb->seq != cb->prev_seq) 1080670dc283SJohannes Berg nlh->nlmsg_flags |= NLM_F_DUMP_INTR; 1081670dc283SJohannes Berg cb->prev_seq = cb->seq; 1082670dc283SJohannes Berg } 1083670dc283SJohannes Berg 1084bfa83a9eSThomas Graf /************************************************************************** 1085bfa83a9eSThomas Graf * Netlink Attributes 1086bfa83a9eSThomas Graf **************************************************************************/ 1087bfa83a9eSThomas Graf 1088bfa83a9eSThomas Graf /** 1089bfa83a9eSThomas Graf * nla_attr_size - length of attribute not including padding 1090bfa83a9eSThomas Graf * @payload: length of payload 1091bfa83a9eSThomas Graf */ 1092bfa83a9eSThomas Graf static inline int nla_attr_size(int payload) 1093bfa83a9eSThomas Graf { 1094bfa83a9eSThomas Graf return NLA_HDRLEN + payload; 1095bfa83a9eSThomas Graf } 1096bfa83a9eSThomas Graf 1097bfa83a9eSThomas Graf /** 1098bfa83a9eSThomas Graf * nla_total_size - total length of attribute including padding 1099bfa83a9eSThomas Graf * @payload: length of payload 1100bfa83a9eSThomas Graf */ 1101bfa83a9eSThomas Graf static inline int nla_total_size(int payload) 1102bfa83a9eSThomas Graf { 1103bfa83a9eSThomas Graf return NLA_ALIGN(nla_attr_size(payload)); 1104bfa83a9eSThomas Graf } 1105bfa83a9eSThomas Graf 1106bfa83a9eSThomas Graf /** 1107bfa83a9eSThomas Graf * nla_padlen - length of padding at the tail of attribute 1108bfa83a9eSThomas Graf * @payload: length of payload 1109bfa83a9eSThomas Graf */ 1110bfa83a9eSThomas Graf static inline int nla_padlen(int payload) 1111bfa83a9eSThomas Graf { 1112bfa83a9eSThomas Graf return nla_total_size(payload) - nla_attr_size(payload); 1113bfa83a9eSThomas Graf } 1114bfa83a9eSThomas Graf 1115bfa83a9eSThomas Graf /** 11168f4c1f9bSThomas Graf * nla_type - attribute type 11178f4c1f9bSThomas Graf * @nla: netlink attribute 11188f4c1f9bSThomas Graf */ 11198f4c1f9bSThomas Graf static inline int nla_type(const struct nlattr *nla) 11208f4c1f9bSThomas Graf { 11218f4c1f9bSThomas Graf return nla->nla_type & NLA_TYPE_MASK; 11228f4c1f9bSThomas Graf } 11238f4c1f9bSThomas Graf 11248f4c1f9bSThomas Graf /** 1125bfa83a9eSThomas Graf * nla_data - head of payload 1126bfa83a9eSThomas Graf * @nla: netlink attribute 1127bfa83a9eSThomas Graf */ 1128bfa83a9eSThomas Graf static inline void *nla_data(const struct nlattr *nla) 1129bfa83a9eSThomas Graf { 1130bfa83a9eSThomas Graf return (char *) nla + NLA_HDRLEN; 1131bfa83a9eSThomas Graf } 1132bfa83a9eSThomas Graf 1133bfa83a9eSThomas Graf /** 1134bfa83a9eSThomas Graf * nla_len - length of payload 1135bfa83a9eSThomas Graf * @nla: netlink attribute 1136bfa83a9eSThomas Graf */ 1137bfa83a9eSThomas Graf static inline int nla_len(const struct nlattr *nla) 1138bfa83a9eSThomas Graf { 1139bfa83a9eSThomas Graf return nla->nla_len - NLA_HDRLEN; 1140bfa83a9eSThomas Graf } 1141bfa83a9eSThomas Graf 1142bfa83a9eSThomas Graf /** 1143bfa83a9eSThomas Graf * nla_ok - check if the netlink attribute fits into the remaining bytes 1144bfa83a9eSThomas Graf * @nla: netlink attribute 1145bfa83a9eSThomas Graf * @remaining: number of bytes remaining in attribute stream 1146bfa83a9eSThomas Graf */ 1147bfa83a9eSThomas Graf static inline int nla_ok(const struct nlattr *nla, int remaining) 1148bfa83a9eSThomas Graf { 11493e1ed981SAlexey Dobriyan return remaining >= (int) sizeof(*nla) && 11503e1ed981SAlexey Dobriyan nla->nla_len >= sizeof(*nla) && 1151bfa83a9eSThomas Graf nla->nla_len <= remaining; 1152bfa83a9eSThomas Graf } 1153bfa83a9eSThomas Graf 1154bfa83a9eSThomas Graf /** 1155d1ec3b77SPierre Ynard * nla_next - next netlink attribute in attribute stream 1156bfa83a9eSThomas Graf * @nla: netlink attribute 1157bfa83a9eSThomas Graf * @remaining: number of bytes remaining in attribute stream 1158bfa83a9eSThomas Graf * 1159bfa83a9eSThomas Graf * Returns the next netlink attribute in the attribute stream and 1160bfa83a9eSThomas Graf * decrements remaining by the size of the current attribute. 1161bfa83a9eSThomas Graf */ 1162bfa83a9eSThomas Graf static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining) 1163bfa83a9eSThomas Graf { 11643b2c75d3SAlexey Dobriyan unsigned int totlen = NLA_ALIGN(nla->nla_len); 1165bfa83a9eSThomas Graf 1166bfa83a9eSThomas Graf *remaining -= totlen; 1167bfa83a9eSThomas Graf return (struct nlattr *) ((char *) nla + totlen); 1168bfa83a9eSThomas Graf } 1169bfa83a9eSThomas Graf 1170bfa83a9eSThomas Graf /** 1171fe4944e5SThomas Graf * nla_find_nested - find attribute in a set of nested attributes 1172fe4944e5SThomas Graf * @nla: attribute containing the nested attributes 1173fe4944e5SThomas Graf * @attrtype: type of attribute to look for 1174fe4944e5SThomas Graf * 1175fe4944e5SThomas Graf * Returns the first attribute which matches the specified type. 1176fe4944e5SThomas Graf */ 11773654654fSJan Engelhardt static inline struct nlattr * 11783654654fSJan Engelhardt nla_find_nested(const struct nlattr *nla, int attrtype) 1179fe4944e5SThomas Graf { 1180fe4944e5SThomas Graf return nla_find(nla_data(nla), nla_len(nla), attrtype); 1181fe4944e5SThomas Graf } 1182fe4944e5SThomas Graf 1183fe4944e5SThomas Graf /** 11843de64403SJohannes Berg * nla_parse_nested - parse nested attributes 11853de64403SJohannes Berg * @tb: destination array with maxtype+1 elements 11863de64403SJohannes Berg * @maxtype: maximum attribute type to be expected 11873de64403SJohannes Berg * @nla: attribute containing the nested attributes 11883de64403SJohannes Berg * @policy: validation policy 11893de64403SJohannes Berg * @extack: extended ACK report struct 11903de64403SJohannes Berg * 11913de64403SJohannes Berg * See nla_parse() 11923de64403SJohannes Berg */ 11933de64403SJohannes Berg static inline int nla_parse_nested(struct nlattr *tb[], int maxtype, 11943de64403SJohannes Berg const struct nlattr *nla, 11953de64403SJohannes Berg const struct nla_policy *policy, 11963de64403SJohannes Berg struct netlink_ext_ack *extack) 11973de64403SJohannes Berg { 1198b424e432SMichal Kubecek if (!(nla->nla_type & NLA_F_NESTED)) { 1199b424e432SMichal Kubecek NL_SET_ERR_MSG_ATTR(extack, nla, "NLA_F_NESTED is missing"); 1200b424e432SMichal Kubecek return -EINVAL; 1201b424e432SMichal Kubecek } 1202b424e432SMichal Kubecek 12033de64403SJohannes Berg return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy, 12043de64403SJohannes Berg NL_VALIDATE_STRICT, extack); 12053de64403SJohannes Berg } 12063de64403SJohannes Berg 12073de64403SJohannes Berg /** 12088cb08174SJohannes Berg * nla_parse_nested_deprecated - parse nested attributes 1209bfa83a9eSThomas Graf * @tb: destination array with maxtype+1 elements 1210bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 1211bfa83a9eSThomas Graf * @nla: attribute containing the nested attributes 1212bfa83a9eSThomas Graf * @policy: validation policy 1213fceb6435SJohannes Berg * @extack: extended ACK report struct 1214bfa83a9eSThomas Graf * 12158cb08174SJohannes Berg * See nla_parse_deprecated() 1216bfa83a9eSThomas Graf */ 12178cb08174SJohannes Berg static inline int nla_parse_nested_deprecated(struct nlattr *tb[], int maxtype, 1218b057efd4SPatrick McHardy const struct nlattr *nla, 1219fceb6435SJohannes Berg const struct nla_policy *policy, 1220fceb6435SJohannes Berg struct netlink_ext_ack *extack) 1221bfa83a9eSThomas Graf { 12228cb08174SJohannes Berg return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy, 12238cb08174SJohannes Berg NL_VALIDATE_LIBERAL, extack); 1224bfa83a9eSThomas Graf } 12251092cb21SPatrick McHardy 12261092cb21SPatrick McHardy /** 1227d1ec3b77SPierre Ynard * nla_put_u8 - Add a u8 netlink attribute to a socket buffer 1228bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 1229bfa83a9eSThomas Graf * @attrtype: attribute type 1230bfa83a9eSThomas Graf * @value: numeric value 1231bfa83a9eSThomas Graf */ 1232bfa83a9eSThomas Graf static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value) 1233bfa83a9eSThomas Graf { 1234b4391db4SArnd Bergmann /* temporary variables to work around GCC PR81715 with asan-stack=1 */ 1235b4391db4SArnd Bergmann u8 tmp = value; 1236b4391db4SArnd Bergmann 1237b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(u8), &tmp); 1238bfa83a9eSThomas Graf } 1239bfa83a9eSThomas Graf 1240bfa83a9eSThomas Graf /** 1241bfa83a9eSThomas Graf * nla_put_u16 - Add a u16 netlink attribute to a socket buffer 1242bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 1243bfa83a9eSThomas Graf * @attrtype: attribute type 1244bfa83a9eSThomas Graf * @value: numeric value 1245bfa83a9eSThomas Graf */ 1246bfa83a9eSThomas Graf static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value) 1247bfa83a9eSThomas Graf { 1248b4391db4SArnd Bergmann u16 tmp = value; 1249b4391db4SArnd Bergmann 1250b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(u16), &tmp); 1251bfa83a9eSThomas Graf } 1252bfa83a9eSThomas Graf 1253bfa83a9eSThomas Graf /** 1254569a8fc3SDavid S. Miller * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer 1255569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 1256569a8fc3SDavid S. Miller * @attrtype: attribute type 1257569a8fc3SDavid S. Miller * @value: numeric value 1258569a8fc3SDavid S. Miller */ 1259569a8fc3SDavid S. Miller static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value) 1260569a8fc3SDavid S. Miller { 1261b4391db4SArnd Bergmann __be16 tmp = value; 1262b4391db4SArnd Bergmann 1263b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__be16), &tmp); 1264569a8fc3SDavid S. Miller } 1265569a8fc3SDavid S. Miller 1266569a8fc3SDavid S. Miller /** 12676c1dd3b6SDavid S. Miller * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer 12686c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 12696c1dd3b6SDavid S. Miller * @attrtype: attribute type 12706c1dd3b6SDavid S. Miller * @value: numeric value 12716c1dd3b6SDavid S. Miller */ 12726c1dd3b6SDavid S. Miller static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value) 12736c1dd3b6SDavid S. Miller { 1274b4391db4SArnd Bergmann __be16 tmp = value; 1275b4391db4SArnd Bergmann 1276b4391db4SArnd Bergmann return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp); 12776c1dd3b6SDavid S. Miller } 12786c1dd3b6SDavid S. Miller 12796c1dd3b6SDavid S. Miller /** 128024c410dcSDavid S. Miller * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer 128124c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 128224c410dcSDavid S. Miller * @attrtype: attribute type 128324c410dcSDavid S. Miller * @value: numeric value 128424c410dcSDavid S. Miller */ 128524c410dcSDavid S. Miller static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value) 128624c410dcSDavid S. Miller { 1287b4391db4SArnd Bergmann __le16 tmp = value; 1288b4391db4SArnd Bergmann 1289b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__le16), &tmp); 129024c410dcSDavid S. Miller } 129124c410dcSDavid S. Miller 129224c410dcSDavid S. Miller /** 1293bfa83a9eSThomas Graf * nla_put_u32 - Add a u32 netlink attribute to a socket buffer 1294bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 1295bfa83a9eSThomas Graf * @attrtype: attribute type 1296bfa83a9eSThomas Graf * @value: numeric value 1297bfa83a9eSThomas Graf */ 1298bfa83a9eSThomas Graf static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value) 1299bfa83a9eSThomas Graf { 1300b4391db4SArnd Bergmann u32 tmp = value; 1301b4391db4SArnd Bergmann 1302b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(u32), &tmp); 1303bfa83a9eSThomas Graf } 1304bfa83a9eSThomas Graf 1305bfa83a9eSThomas Graf /** 1306569a8fc3SDavid S. Miller * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer 1307569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 1308569a8fc3SDavid S. Miller * @attrtype: attribute type 1309569a8fc3SDavid S. Miller * @value: numeric value 1310569a8fc3SDavid S. Miller */ 1311569a8fc3SDavid S. Miller static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value) 1312569a8fc3SDavid S. Miller { 1313b4391db4SArnd Bergmann __be32 tmp = value; 1314b4391db4SArnd Bergmann 1315b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__be32), &tmp); 1316569a8fc3SDavid S. Miller } 1317569a8fc3SDavid S. Miller 1318569a8fc3SDavid S. Miller /** 13196c1dd3b6SDavid S. Miller * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer 13206c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 13216c1dd3b6SDavid S. Miller * @attrtype: attribute type 13226c1dd3b6SDavid S. Miller * @value: numeric value 13236c1dd3b6SDavid S. Miller */ 13246c1dd3b6SDavid S. Miller static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value) 13256c1dd3b6SDavid S. Miller { 1326b4391db4SArnd Bergmann __be32 tmp = value; 1327b4391db4SArnd Bergmann 1328b4391db4SArnd Bergmann return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp); 13296c1dd3b6SDavid S. Miller } 13306c1dd3b6SDavid S. Miller 13316c1dd3b6SDavid S. Miller /** 133224c410dcSDavid S. Miller * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer 133324c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 133424c410dcSDavid S. Miller * @attrtype: attribute type 133524c410dcSDavid S. Miller * @value: numeric value 133624c410dcSDavid S. Miller */ 133724c410dcSDavid S. Miller static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value) 133824c410dcSDavid S. Miller { 1339b4391db4SArnd Bergmann __le32 tmp = value; 1340b4391db4SArnd Bergmann 1341b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__le32), &tmp); 134224c410dcSDavid S. Miller } 134324c410dcSDavid S. Miller 134424c410dcSDavid S. Miller /** 134573520786SNicolas Dichtel * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it 134673520786SNicolas Dichtel * @skb: socket buffer to add attribute to 134773520786SNicolas Dichtel * @attrtype: attribute type 134873520786SNicolas Dichtel * @value: numeric value 134973520786SNicolas Dichtel * @padattr: attribute type for the padding 135073520786SNicolas Dichtel */ 135173520786SNicolas Dichtel static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype, 135273520786SNicolas Dichtel u64 value, int padattr) 135373520786SNicolas Dichtel { 1354b4391db4SArnd Bergmann u64 tmp = value; 1355b4391db4SArnd Bergmann 1356b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr); 135773520786SNicolas Dichtel } 135873520786SNicolas Dichtel 135973520786SNicolas Dichtel /** 1360b46f6dedSNicolas Dichtel * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it 1361569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 1362569a8fc3SDavid S. Miller * @attrtype: attribute type 1363569a8fc3SDavid S. Miller * @value: numeric value 1364b46f6dedSNicolas Dichtel * @padattr: attribute type for the padding 1365569a8fc3SDavid S. Miller */ 1366b46f6dedSNicolas Dichtel static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value, 1367b46f6dedSNicolas Dichtel int padattr) 1368b46f6dedSNicolas Dichtel { 1369b4391db4SArnd Bergmann __be64 tmp = value; 1370b4391db4SArnd Bergmann 1371b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr); 1372b46f6dedSNicolas Dichtel } 1373b46f6dedSNicolas Dichtel 1374569a8fc3SDavid S. Miller /** 1375e9bbe898SNicolas Dichtel * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it 13766c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 13776c1dd3b6SDavid S. Miller * @attrtype: attribute type 13786c1dd3b6SDavid S. Miller * @value: numeric value 1379e9bbe898SNicolas Dichtel * @padattr: attribute type for the padding 13806c1dd3b6SDavid S. Miller */ 1381e9bbe898SNicolas Dichtel static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value, 1382e9bbe898SNicolas Dichtel int padattr) 13836c1dd3b6SDavid S. Miller { 1384b4391db4SArnd Bergmann __be64 tmp = value; 1385b4391db4SArnd Bergmann 1386b4391db4SArnd Bergmann return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp, 1387e9bbe898SNicolas Dichtel padattr); 13886c1dd3b6SDavid S. Miller } 13896c1dd3b6SDavid S. Miller 13906c1dd3b6SDavid S. Miller /** 1391e7479122SNicolas Dichtel * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it 139224c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 139324c410dcSDavid S. Miller * @attrtype: attribute type 139424c410dcSDavid S. Miller * @value: numeric value 1395e7479122SNicolas Dichtel * @padattr: attribute type for the padding 139624c410dcSDavid S. Miller */ 1397e7479122SNicolas Dichtel static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value, 1398e7479122SNicolas Dichtel int padattr) 139924c410dcSDavid S. Miller { 1400b4391db4SArnd Bergmann __le64 tmp = value; 1401b4391db4SArnd Bergmann 1402b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr); 140324c410dcSDavid S. Miller } 140424c410dcSDavid S. Miller 140524c410dcSDavid S. Miller /** 14064778e0beSJiri Pirko * nla_put_s8 - Add a s8 netlink attribute to a socket buffer 14074778e0beSJiri Pirko * @skb: socket buffer to add attribute to 14084778e0beSJiri Pirko * @attrtype: attribute type 14094778e0beSJiri Pirko * @value: numeric value 14104778e0beSJiri Pirko */ 14114778e0beSJiri Pirko static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value) 14124778e0beSJiri Pirko { 1413b4391db4SArnd Bergmann s8 tmp = value; 1414b4391db4SArnd Bergmann 1415b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(s8), &tmp); 14164778e0beSJiri Pirko } 14174778e0beSJiri Pirko 14184778e0beSJiri Pirko /** 14194778e0beSJiri Pirko * nla_put_s16 - Add a s16 netlink attribute to a socket buffer 14204778e0beSJiri Pirko * @skb: socket buffer to add attribute to 14214778e0beSJiri Pirko * @attrtype: attribute type 14224778e0beSJiri Pirko * @value: numeric value 14234778e0beSJiri Pirko */ 14244778e0beSJiri Pirko static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value) 14254778e0beSJiri Pirko { 1426b4391db4SArnd Bergmann s16 tmp = value; 1427b4391db4SArnd Bergmann 1428b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(s16), &tmp); 14294778e0beSJiri Pirko } 14304778e0beSJiri Pirko 14314778e0beSJiri Pirko /** 14324778e0beSJiri Pirko * nla_put_s32 - Add a s32 netlink attribute to a socket buffer 14334778e0beSJiri Pirko * @skb: socket buffer to add attribute to 14344778e0beSJiri Pirko * @attrtype: attribute type 14354778e0beSJiri Pirko * @value: numeric value 14364778e0beSJiri Pirko */ 14374778e0beSJiri Pirko static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value) 14384778e0beSJiri Pirko { 1439b4391db4SArnd Bergmann s32 tmp = value; 1440b4391db4SArnd Bergmann 1441b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(s32), &tmp); 14424778e0beSJiri Pirko } 14434778e0beSJiri Pirko 14444778e0beSJiri Pirko /** 1445756a2f59SNicolas Dichtel * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it 14464778e0beSJiri Pirko * @skb: socket buffer to add attribute to 14474778e0beSJiri Pirko * @attrtype: attribute type 14484778e0beSJiri Pirko * @value: numeric value 1449756a2f59SNicolas Dichtel * @padattr: attribute type for the padding 14504778e0beSJiri Pirko */ 1451756a2f59SNicolas Dichtel static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value, 1452756a2f59SNicolas Dichtel int padattr) 14534778e0beSJiri Pirko { 1454b4391db4SArnd Bergmann s64 tmp = value; 1455b4391db4SArnd Bergmann 1456b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr); 14574778e0beSJiri Pirko } 14584778e0beSJiri Pirko 14594778e0beSJiri Pirko /** 1460bfa83a9eSThomas Graf * nla_put_string - Add a string netlink attribute to a socket buffer 1461bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 1462bfa83a9eSThomas Graf * @attrtype: attribute type 1463bfa83a9eSThomas Graf * @str: NUL terminated string 1464bfa83a9eSThomas Graf */ 1465bfa83a9eSThomas Graf static inline int nla_put_string(struct sk_buff *skb, int attrtype, 1466bfa83a9eSThomas Graf const char *str) 1467bfa83a9eSThomas Graf { 1468bfa83a9eSThomas Graf return nla_put(skb, attrtype, strlen(str) + 1, str); 1469bfa83a9eSThomas Graf } 1470bfa83a9eSThomas Graf 1471bfa83a9eSThomas Graf /** 1472bfa83a9eSThomas Graf * nla_put_flag - Add a flag netlink attribute to a socket buffer 1473bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 1474bfa83a9eSThomas Graf * @attrtype: attribute type 1475bfa83a9eSThomas Graf */ 1476bfa83a9eSThomas Graf static inline int nla_put_flag(struct sk_buff *skb, int attrtype) 1477bfa83a9eSThomas Graf { 1478bfa83a9eSThomas Graf return nla_put(skb, attrtype, 0, NULL); 1479bfa83a9eSThomas Graf } 1480bfa83a9eSThomas Graf 1481bfa83a9eSThomas Graf /** 14822175d87cSNicolas Dichtel * nla_put_msecs - Add a msecs netlink attribute to a skb and align it 1483bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 1484bfa83a9eSThomas Graf * @attrtype: attribute type 1485d87de1f3SMark Rustad * @njiffies: number of jiffies to convert to msecs 14862175d87cSNicolas Dichtel * @padattr: attribute type for the padding 1487bfa83a9eSThomas Graf */ 1488bfa83a9eSThomas Graf static inline int nla_put_msecs(struct sk_buff *skb, int attrtype, 14892175d87cSNicolas Dichtel unsigned long njiffies, int padattr) 1490bfa83a9eSThomas Graf { 1491d87de1f3SMark Rustad u64 tmp = jiffies_to_msecs(njiffies); 14922175d87cSNicolas Dichtel 14932175d87cSNicolas Dichtel return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr); 1494bfa83a9eSThomas Graf } 1495bfa83a9eSThomas Graf 1496bfa83a9eSThomas Graf /** 1497930345eaSJiri Benc * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket 1498930345eaSJiri Benc * buffer 1499930345eaSJiri Benc * @skb: socket buffer to add attribute to 1500930345eaSJiri Benc * @attrtype: attribute type 1501930345eaSJiri Benc * @addr: IPv4 address 1502930345eaSJiri Benc */ 1503930345eaSJiri Benc static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype, 1504930345eaSJiri Benc __be32 addr) 1505930345eaSJiri Benc { 1506b4391db4SArnd Bergmann __be32 tmp = addr; 1507b4391db4SArnd Bergmann 1508b4391db4SArnd Bergmann return nla_put_be32(skb, attrtype, tmp); 1509930345eaSJiri Benc } 1510930345eaSJiri Benc 1511930345eaSJiri Benc /** 1512930345eaSJiri Benc * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket 1513930345eaSJiri Benc * buffer 1514930345eaSJiri Benc * @skb: socket buffer to add attribute to 1515930345eaSJiri Benc * @attrtype: attribute type 1516930345eaSJiri Benc * @addr: IPv6 address 1517930345eaSJiri Benc */ 1518930345eaSJiri Benc static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype, 1519930345eaSJiri Benc const struct in6_addr *addr) 1520930345eaSJiri Benc { 1521930345eaSJiri Benc return nla_put(skb, attrtype, sizeof(*addr), addr); 1522930345eaSJiri Benc } 1523930345eaSJiri Benc 1524930345eaSJiri Benc /** 15258953b077SJiri Pirko * nla_put_bitfield32 - Add a bitfield32 netlink attribute to a socket buffer 15268953b077SJiri Pirko * @skb: socket buffer to add attribute to 15278953b077SJiri Pirko * @attrtype: attribute type 15288953b077SJiri Pirko * @value: value carrying bits 15298953b077SJiri Pirko * @selector: selector of valid bits 15308953b077SJiri Pirko */ 15318953b077SJiri Pirko static inline int nla_put_bitfield32(struct sk_buff *skb, int attrtype, 15328953b077SJiri Pirko __u32 value, __u32 selector) 15338953b077SJiri Pirko { 15348953b077SJiri Pirko struct nla_bitfield32 tmp = { value, selector, }; 15358953b077SJiri Pirko 15368953b077SJiri Pirko return nla_put(skb, attrtype, sizeof(tmp), &tmp); 15378953b077SJiri Pirko } 15388953b077SJiri Pirko 15398953b077SJiri Pirko /** 1540bfa83a9eSThomas Graf * nla_get_u32 - return payload of u32 attribute 1541bfa83a9eSThomas Graf * @nla: u32 netlink attribute 1542bfa83a9eSThomas Graf */ 1543b057efd4SPatrick McHardy static inline u32 nla_get_u32(const struct nlattr *nla) 1544bfa83a9eSThomas Graf { 1545bfa83a9eSThomas Graf return *(u32 *) nla_data(nla); 1546bfa83a9eSThomas Graf } 1547bfa83a9eSThomas Graf 1548bfa83a9eSThomas Graf /** 154900012e5bSAl Viro * nla_get_be32 - return payload of __be32 attribute 155000012e5bSAl Viro * @nla: __be32 netlink attribute 155100012e5bSAl Viro */ 1552b057efd4SPatrick McHardy static inline __be32 nla_get_be32(const struct nlattr *nla) 155300012e5bSAl Viro { 155400012e5bSAl Viro return *(__be32 *) nla_data(nla); 155500012e5bSAl Viro } 155600012e5bSAl Viro 155700012e5bSAl Viro /** 1558c648a013SAlexander Aring * nla_get_le32 - return payload of __le32 attribute 1559c648a013SAlexander Aring * @nla: __le32 netlink attribute 1560c648a013SAlexander Aring */ 1561c648a013SAlexander Aring static inline __le32 nla_get_le32(const struct nlattr *nla) 1562c648a013SAlexander Aring { 1563c648a013SAlexander Aring return *(__le32 *) nla_data(nla); 1564c648a013SAlexander Aring } 1565c648a013SAlexander Aring 1566c648a013SAlexander Aring /** 1567bfa83a9eSThomas Graf * nla_get_u16 - return payload of u16 attribute 1568bfa83a9eSThomas Graf * @nla: u16 netlink attribute 1569bfa83a9eSThomas Graf */ 1570b057efd4SPatrick McHardy static inline u16 nla_get_u16(const struct nlattr *nla) 1571bfa83a9eSThomas Graf { 1572bfa83a9eSThomas Graf return *(u16 *) nla_data(nla); 1573bfa83a9eSThomas Graf } 1574bfa83a9eSThomas Graf 1575bfa83a9eSThomas Graf /** 1576838965baSPatrick McHardy * nla_get_be16 - return payload of __be16 attribute 1577838965baSPatrick McHardy * @nla: __be16 netlink attribute 1578838965baSPatrick McHardy */ 1579b057efd4SPatrick McHardy static inline __be16 nla_get_be16(const struct nlattr *nla) 1580838965baSPatrick McHardy { 1581838965baSPatrick McHardy return *(__be16 *) nla_data(nla); 1582838965baSPatrick McHardy } 1583838965baSPatrick McHardy 1584838965baSPatrick McHardy /** 15854a89c256SThomas Graf * nla_get_le16 - return payload of __le16 attribute 15864a89c256SThomas Graf * @nla: __le16 netlink attribute 15874a89c256SThomas Graf */ 1588b057efd4SPatrick McHardy static inline __le16 nla_get_le16(const struct nlattr *nla) 15894a89c256SThomas Graf { 15904a89c256SThomas Graf return *(__le16 *) nla_data(nla); 15914a89c256SThomas Graf } 15924a89c256SThomas Graf 15934a89c256SThomas Graf /** 1594bfa83a9eSThomas Graf * nla_get_u8 - return payload of u8 attribute 1595bfa83a9eSThomas Graf * @nla: u8 netlink attribute 1596bfa83a9eSThomas Graf */ 1597b057efd4SPatrick McHardy static inline u8 nla_get_u8(const struct nlattr *nla) 1598bfa83a9eSThomas Graf { 1599bfa83a9eSThomas Graf return *(u8 *) nla_data(nla); 1600bfa83a9eSThomas Graf } 1601bfa83a9eSThomas Graf 1602bfa83a9eSThomas Graf /** 1603bfa83a9eSThomas Graf * nla_get_u64 - return payload of u64 attribute 1604bfa83a9eSThomas Graf * @nla: u64 netlink attribute 1605bfa83a9eSThomas Graf */ 1606b057efd4SPatrick McHardy static inline u64 nla_get_u64(const struct nlattr *nla) 1607bfa83a9eSThomas Graf { 1608bfa83a9eSThomas Graf u64 tmp; 1609bfa83a9eSThomas Graf 1610bfa83a9eSThomas Graf nla_memcpy(&tmp, nla, sizeof(tmp)); 1611bfa83a9eSThomas Graf 1612bfa83a9eSThomas Graf return tmp; 1613bfa83a9eSThomas Graf } 1614bfa83a9eSThomas Graf 1615bfa83a9eSThomas Graf /** 1616a17c8598SPablo Neira Ayuso * nla_get_be64 - return payload of __be64 attribute 1617a17c8598SPablo Neira Ayuso * @nla: __be64 netlink attribute 1618a17c8598SPablo Neira Ayuso */ 1619a17c8598SPablo Neira Ayuso static inline __be64 nla_get_be64(const struct nlattr *nla) 1620a17c8598SPablo Neira Ayuso { 1621f5d410f2SPablo Neira Ayuso __be64 tmp; 1622f5d410f2SPablo Neira Ayuso 1623f5d410f2SPablo Neira Ayuso nla_memcpy(&tmp, nla, sizeof(tmp)); 1624f5d410f2SPablo Neira Ayuso 1625f5d410f2SPablo Neira Ayuso return tmp; 1626a17c8598SPablo Neira Ayuso } 1627a17c8598SPablo Neira Ayuso 1628a17c8598SPablo Neira Ayuso /** 1629c648a013SAlexander Aring * nla_get_le64 - return payload of __le64 attribute 1630c648a013SAlexander Aring * @nla: __le64 netlink attribute 1631c648a013SAlexander Aring */ 1632c648a013SAlexander Aring static inline __le64 nla_get_le64(const struct nlattr *nla) 1633c648a013SAlexander Aring { 1634c648a013SAlexander Aring return *(__le64 *) nla_data(nla); 1635c648a013SAlexander Aring } 1636c648a013SAlexander Aring 1637c648a013SAlexander Aring /** 16384778e0beSJiri Pirko * nla_get_s32 - return payload of s32 attribute 16394778e0beSJiri Pirko * @nla: s32 netlink attribute 16404778e0beSJiri Pirko */ 16414778e0beSJiri Pirko static inline s32 nla_get_s32(const struct nlattr *nla) 16424778e0beSJiri Pirko { 16434778e0beSJiri Pirko return *(s32 *) nla_data(nla); 16444778e0beSJiri Pirko } 16454778e0beSJiri Pirko 16464778e0beSJiri Pirko /** 16474778e0beSJiri Pirko * nla_get_s16 - return payload of s16 attribute 16484778e0beSJiri Pirko * @nla: s16 netlink attribute 16494778e0beSJiri Pirko */ 16504778e0beSJiri Pirko static inline s16 nla_get_s16(const struct nlattr *nla) 16514778e0beSJiri Pirko { 16524778e0beSJiri Pirko return *(s16 *) nla_data(nla); 16534778e0beSJiri Pirko } 16544778e0beSJiri Pirko 16554778e0beSJiri Pirko /** 16564778e0beSJiri Pirko * nla_get_s8 - return payload of s8 attribute 16574778e0beSJiri Pirko * @nla: s8 netlink attribute 16584778e0beSJiri Pirko */ 16594778e0beSJiri Pirko static inline s8 nla_get_s8(const struct nlattr *nla) 16604778e0beSJiri Pirko { 16614778e0beSJiri Pirko return *(s8 *) nla_data(nla); 16624778e0beSJiri Pirko } 16634778e0beSJiri Pirko 16644778e0beSJiri Pirko /** 16654778e0beSJiri Pirko * nla_get_s64 - return payload of s64 attribute 16664778e0beSJiri Pirko * @nla: s64 netlink attribute 16674778e0beSJiri Pirko */ 16684778e0beSJiri Pirko static inline s64 nla_get_s64(const struct nlattr *nla) 16694778e0beSJiri Pirko { 16704778e0beSJiri Pirko s64 tmp; 16714778e0beSJiri Pirko 16724778e0beSJiri Pirko nla_memcpy(&tmp, nla, sizeof(tmp)); 16734778e0beSJiri Pirko 16744778e0beSJiri Pirko return tmp; 16754778e0beSJiri Pirko } 16764778e0beSJiri Pirko 16774778e0beSJiri Pirko /** 1678bfa83a9eSThomas Graf * nla_get_flag - return payload of flag attribute 1679bfa83a9eSThomas Graf * @nla: flag netlink attribute 1680bfa83a9eSThomas Graf */ 1681b057efd4SPatrick McHardy static inline int nla_get_flag(const struct nlattr *nla) 1682bfa83a9eSThomas Graf { 1683bfa83a9eSThomas Graf return !!nla; 1684bfa83a9eSThomas Graf } 1685bfa83a9eSThomas Graf 1686bfa83a9eSThomas Graf /** 1687bfa83a9eSThomas Graf * nla_get_msecs - return payload of msecs attribute 1688bfa83a9eSThomas Graf * @nla: msecs netlink attribute 1689bfa83a9eSThomas Graf * 1690bfa83a9eSThomas Graf * Returns the number of milliseconds in jiffies. 1691bfa83a9eSThomas Graf */ 1692b057efd4SPatrick McHardy static inline unsigned long nla_get_msecs(const struct nlattr *nla) 1693bfa83a9eSThomas Graf { 1694bfa83a9eSThomas Graf u64 msecs = nla_get_u64(nla); 1695bfa83a9eSThomas Graf 1696bfa83a9eSThomas Graf return msecs_to_jiffies((unsigned long) msecs); 1697bfa83a9eSThomas Graf } 1698bfa83a9eSThomas Graf 1699bfa83a9eSThomas Graf /** 170067b61f6cSJiri Benc * nla_get_in_addr - return payload of IPv4 address attribute 170167b61f6cSJiri Benc * @nla: IPv4 address netlink attribute 170267b61f6cSJiri Benc */ 170367b61f6cSJiri Benc static inline __be32 nla_get_in_addr(const struct nlattr *nla) 170467b61f6cSJiri Benc { 170567b61f6cSJiri Benc return *(__be32 *) nla_data(nla); 170667b61f6cSJiri Benc } 170767b61f6cSJiri Benc 170867b61f6cSJiri Benc /** 170967b61f6cSJiri Benc * nla_get_in6_addr - return payload of IPv6 address attribute 171067b61f6cSJiri Benc * @nla: IPv6 address netlink attribute 171167b61f6cSJiri Benc */ 171267b61f6cSJiri Benc static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla) 171367b61f6cSJiri Benc { 171467b61f6cSJiri Benc struct in6_addr tmp; 171567b61f6cSJiri Benc 171667b61f6cSJiri Benc nla_memcpy(&tmp, nla, sizeof(tmp)); 171767b61f6cSJiri Benc return tmp; 171867b61f6cSJiri Benc } 171967b61f6cSJiri Benc 172067b61f6cSJiri Benc /** 172164c83d83SJamal Hadi Salim * nla_get_bitfield32 - return payload of 32 bitfield attribute 172264c83d83SJamal Hadi Salim * @nla: nla_bitfield32 attribute 172364c83d83SJamal Hadi Salim */ 172464c83d83SJamal Hadi Salim static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla) 172564c83d83SJamal Hadi Salim { 172664c83d83SJamal Hadi Salim struct nla_bitfield32 tmp; 172764c83d83SJamal Hadi Salim 172864c83d83SJamal Hadi Salim nla_memcpy(&tmp, nla, sizeof(tmp)); 172964c83d83SJamal Hadi Salim return tmp; 173064c83d83SJamal Hadi Salim } 173164c83d83SJamal Hadi Salim 173264c83d83SJamal Hadi Salim /** 1733b15ca182SThomas Graf * nla_memdup - duplicate attribute memory (kmemdup) 1734b15ca182SThomas Graf * @src: netlink attribute to duplicate from 1735b15ca182SThomas Graf * @gfp: GFP mask 1736b15ca182SThomas Graf */ 1737b15ca182SThomas Graf static inline void *nla_memdup(const struct nlattr *src, gfp_t gfp) 1738b15ca182SThomas Graf { 1739b15ca182SThomas Graf return kmemdup(nla_data(src), nla_len(src), gfp); 1740b15ca182SThomas Graf } 1741b15ca182SThomas Graf 1742b15ca182SThomas Graf /** 1743ae0be8deSMichal Kubecek * nla_nest_start_noflag - Start a new level of nested attributes 1744bfa83a9eSThomas Graf * @skb: socket buffer to add attributes to 1745bfa83a9eSThomas Graf * @attrtype: attribute type of container 1746bfa83a9eSThomas Graf * 1747ae0be8deSMichal Kubecek * This function exists for backward compatibility to use in APIs which never 1748ae0be8deSMichal Kubecek * marked their nest attributes with NLA_F_NESTED flag. New APIs should use 1749ae0be8deSMichal Kubecek * nla_nest_start() which sets the flag. 1750ae0be8deSMichal Kubecek * 1751ae0be8deSMichal Kubecek * Returns the container attribute or NULL on error 1752bfa83a9eSThomas Graf */ 1753ae0be8deSMichal Kubecek static inline struct nlattr *nla_nest_start_noflag(struct sk_buff *skb, 1754ae0be8deSMichal Kubecek int attrtype) 1755bfa83a9eSThomas Graf { 175627a884dcSArnaldo Carvalho de Melo struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb); 1757bfa83a9eSThomas Graf 1758bfa83a9eSThomas Graf if (nla_put(skb, attrtype, 0, NULL) < 0) 1759bfa83a9eSThomas Graf return NULL; 1760bfa83a9eSThomas Graf 1761bfa83a9eSThomas Graf return start; 1762bfa83a9eSThomas Graf } 1763bfa83a9eSThomas Graf 1764bfa83a9eSThomas Graf /** 1765ae0be8deSMichal Kubecek * nla_nest_start - Start a new level of nested attributes, with NLA_F_NESTED 1766ae0be8deSMichal Kubecek * @skb: socket buffer to add attributes to 1767ae0be8deSMichal Kubecek * @attrtype: attribute type of container 1768ae0be8deSMichal Kubecek * 1769ae0be8deSMichal Kubecek * Unlike nla_nest_start_noflag(), mark the nest attribute with NLA_F_NESTED 1770ae0be8deSMichal Kubecek * flag. This is the preferred function to use in new code. 1771ae0be8deSMichal Kubecek * 1772ae0be8deSMichal Kubecek * Returns the container attribute or NULL on error 1773ae0be8deSMichal Kubecek */ 1774ae0be8deSMichal Kubecek static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype) 1775ae0be8deSMichal Kubecek { 1776ae0be8deSMichal Kubecek return nla_nest_start_noflag(skb, attrtype | NLA_F_NESTED); 1777ae0be8deSMichal Kubecek } 1778ae0be8deSMichal Kubecek 1779ae0be8deSMichal Kubecek /** 1780bfa83a9eSThomas Graf * nla_nest_end - Finalize nesting of attributes 1781d1ec3b77SPierre Ynard * @skb: socket buffer the attributes are stored in 1782bfa83a9eSThomas Graf * @start: container attribute 1783bfa83a9eSThomas Graf * 1784bfa83a9eSThomas Graf * Corrects the container attribute header to include the all 1785bfa83a9eSThomas Graf * appeneded attributes. 1786bfa83a9eSThomas Graf * 1787bfa83a9eSThomas Graf * Returns the total data length of the skb. 1788bfa83a9eSThomas Graf */ 1789bfa83a9eSThomas Graf static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start) 1790bfa83a9eSThomas Graf { 179127a884dcSArnaldo Carvalho de Melo start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start; 1792bfa83a9eSThomas Graf return skb->len; 1793bfa83a9eSThomas Graf } 1794bfa83a9eSThomas Graf 1795bfa83a9eSThomas Graf /** 1796bfa83a9eSThomas Graf * nla_nest_cancel - Cancel nesting of attributes 1797bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 1798bfa83a9eSThomas Graf * @start: container attribute 1799bfa83a9eSThomas Graf * 1800bfa83a9eSThomas Graf * Removes the container attribute and including all nested 1801bc3ed28cSThomas Graf * attributes. Returns -EMSGSIZE 1802bfa83a9eSThomas Graf */ 1803bc3ed28cSThomas Graf static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start) 1804bfa83a9eSThomas Graf { 1805bc3ed28cSThomas Graf nlmsg_trim(skb, start); 1806bfa83a9eSThomas Graf } 1807bfa83a9eSThomas Graf 1808bfa83a9eSThomas Graf /** 180932d5109aSMichal Kubecek * __nla_validate_nested - Validate a stream of nested attributes 18104fe5d5c0SPaul Moore * @start: container attribute 18114fe5d5c0SPaul Moore * @maxtype: maximum attribute type to be expected 18124fe5d5c0SPaul Moore * @policy: validation policy 18138cb08174SJohannes Berg * @validate: validation strictness 1814fceb6435SJohannes Berg * @extack: extended ACK report struct 18154fe5d5c0SPaul Moore * 18164fe5d5c0SPaul Moore * Validates all attributes in the nested attribute stream against the 18174fe5d5c0SPaul Moore * specified policy. Attributes with a type exceeding maxtype will be 18184fe5d5c0SPaul Moore * ignored. See documenation of struct nla_policy for more details. 18194fe5d5c0SPaul Moore * 18204fe5d5c0SPaul Moore * Returns 0 on success or a negative error code. 18214fe5d5c0SPaul Moore */ 18228cb08174SJohannes Berg static inline int __nla_validate_nested(const struct nlattr *start, int maxtype, 18238cb08174SJohannes Berg const struct nla_policy *policy, 18248cb08174SJohannes Berg unsigned int validate, 18258cb08174SJohannes Berg struct netlink_ext_ack *extack) 18268cb08174SJohannes Berg { 18278cb08174SJohannes Berg return __nla_validate(nla_data(start), nla_len(start), maxtype, policy, 18288cb08174SJohannes Berg validate, extack); 18298cb08174SJohannes Berg } 18308cb08174SJohannes Berg 18318cb08174SJohannes Berg static inline int 183232d5109aSMichal Kubecek nla_validate_nested(const struct nlattr *start, int maxtype, 1833901bb989SJohannes Berg const struct nla_policy *policy, 1834901bb989SJohannes Berg struct netlink_ext_ack *extack) 1835901bb989SJohannes Berg { 1836901bb989SJohannes Berg return __nla_validate_nested(start, maxtype, policy, 1837901bb989SJohannes Berg NL_VALIDATE_STRICT, extack); 1838901bb989SJohannes Berg } 1839901bb989SJohannes Berg 1840901bb989SJohannes Berg static inline int 18418cb08174SJohannes Berg nla_validate_nested_deprecated(const struct nlattr *start, int maxtype, 1842fceb6435SJohannes Berg const struct nla_policy *policy, 1843fceb6435SJohannes Berg struct netlink_ext_ack *extack) 18444fe5d5c0SPaul Moore { 18458cb08174SJohannes Berg return __nla_validate_nested(start, maxtype, policy, 18468cb08174SJohannes Berg NL_VALIDATE_LIBERAL, extack); 18474fe5d5c0SPaul Moore } 18484fe5d5c0SPaul Moore 18494fe5d5c0SPaul Moore /** 1850089bf1a6SNicolas Dichtel * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute 1851089bf1a6SNicolas Dichtel * @skb: socket buffer the message is stored in 1852089bf1a6SNicolas Dichtel * 1853089bf1a6SNicolas Dichtel * Return true if padding is needed to align the next attribute (nla_data()) to 1854089bf1a6SNicolas Dichtel * a 64-bit aligned area. 1855089bf1a6SNicolas Dichtel */ 1856089bf1a6SNicolas Dichtel static inline bool nla_need_padding_for_64bit(struct sk_buff *skb) 1857089bf1a6SNicolas Dichtel { 1858089bf1a6SNicolas Dichtel #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1859089bf1a6SNicolas Dichtel /* The nlattr header is 4 bytes in size, that's why we test 1860089bf1a6SNicolas Dichtel * if the skb->data _is_ aligned. A NOP attribute, plus 1861089bf1a6SNicolas Dichtel * nlattr header for next attribute, will make nla_data() 1862089bf1a6SNicolas Dichtel * 8-byte aligned. 1863089bf1a6SNicolas Dichtel */ 1864089bf1a6SNicolas Dichtel if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8)) 1865089bf1a6SNicolas Dichtel return true; 1866089bf1a6SNicolas Dichtel #endif 1867089bf1a6SNicolas Dichtel return false; 1868089bf1a6SNicolas Dichtel } 1869089bf1a6SNicolas Dichtel 1870089bf1a6SNicolas Dichtel /** 187135c58459SDavid S. Miller * nla_align_64bit - 64-bit align the nla_data() of next attribute 187235c58459SDavid S. Miller * @skb: socket buffer the message is stored in 187335c58459SDavid S. Miller * @padattr: attribute type for the padding 187435c58459SDavid S. Miller * 187535c58459SDavid S. Miller * Conditionally emit a padding netlink attribute in order to make 187635c58459SDavid S. Miller * the next attribute we emit have a 64-bit aligned nla_data() area. 187735c58459SDavid S. Miller * This will only be done in architectures which do not have 1878cca1d815SEric Dumazet * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined. 187935c58459SDavid S. Miller * 188035c58459SDavid S. Miller * Returns zero on success or a negative error code. 188135c58459SDavid S. Miller */ 188235c58459SDavid S. Miller static inline int nla_align_64bit(struct sk_buff *skb, int padattr) 188335c58459SDavid S. Miller { 1884089bf1a6SNicolas Dichtel if (nla_need_padding_for_64bit(skb) && 1885cca1d815SEric Dumazet !nla_reserve(skb, padattr, 0)) 188635c58459SDavid S. Miller return -EMSGSIZE; 1887089bf1a6SNicolas Dichtel 188835c58459SDavid S. Miller return 0; 188935c58459SDavid S. Miller } 189035c58459SDavid S. Miller 189135c58459SDavid S. Miller /** 189235c58459SDavid S. Miller * nla_total_size_64bit - total length of attribute including padding 189335c58459SDavid S. Miller * @payload: length of payload 189435c58459SDavid S. Miller */ 189535c58459SDavid S. Miller static inline int nla_total_size_64bit(int payload) 189635c58459SDavid S. Miller { 189735c58459SDavid S. Miller return NLA_ALIGN(nla_attr_size(payload)) 1898cca1d815SEric Dumazet #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 189935c58459SDavid S. Miller + NLA_ALIGN(nla_attr_size(0)) 190035c58459SDavid S. Miller #endif 190135c58459SDavid S. Miller ; 190235c58459SDavid S. Miller } 190335c58459SDavid S. Miller 190435c58459SDavid S. Miller /** 1905bfa83a9eSThomas Graf * nla_for_each_attr - iterate over a stream of attributes 1906bfa83a9eSThomas Graf * @pos: loop counter, set to current attribute 1907bfa83a9eSThomas Graf * @head: head of attribute stream 1908bfa83a9eSThomas Graf * @len: length of attribute stream 1909bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 1910bfa83a9eSThomas Graf */ 1911bfa83a9eSThomas Graf #define nla_for_each_attr(pos, head, len, rem) \ 1912bfa83a9eSThomas Graf for (pos = head, rem = len; \ 1913bfa83a9eSThomas Graf nla_ok(pos, rem); \ 1914bfa83a9eSThomas Graf pos = nla_next(pos, &(rem))) 1915bfa83a9eSThomas Graf 1916fe4944e5SThomas Graf /** 1917fe4944e5SThomas Graf * nla_for_each_nested - iterate over nested attributes 1918fe4944e5SThomas Graf * @pos: loop counter, set to current attribute 1919fe4944e5SThomas Graf * @nla: attribute containing the nested attributes 1920fe4944e5SThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 1921fe4944e5SThomas Graf */ 1922fe4944e5SThomas Graf #define nla_for_each_nested(pos, nla, rem) \ 1923fe4944e5SThomas Graf nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem) 1924fe4944e5SThomas Graf 1925941d8ebcSSimon Horman /** 1926941d8ebcSSimon Horman * nla_is_last - Test if attribute is last in stream 1927941d8ebcSSimon Horman * @nla: attribute to test 1928941d8ebcSSimon Horman * @rem: bytes remaining in stream 1929941d8ebcSSimon Horman */ 1930941d8ebcSSimon Horman static inline bool nla_is_last(const struct nlattr *nla, int rem) 1931941d8ebcSSimon Horman { 1932941d8ebcSSimon Horman return nla->nla_len == rem; 1933941d8ebcSSimon Horman } 1934941d8ebcSSimon Horman 19352c28ae48SJohannes Berg void nla_get_range_unsigned(const struct nla_policy *pt, 19362c28ae48SJohannes Berg struct netlink_range_validation *range); 19372c28ae48SJohannes Berg void nla_get_range_signed(const struct nla_policy *pt, 19382c28ae48SJohannes Berg struct netlink_range_validation_signed *range); 19392c28ae48SJohannes Berg 1940d07dcf9aSJohannes Berg int netlink_policy_dump_start(const struct nla_policy *policy, 1941d07dcf9aSJohannes Berg unsigned int maxtype, 1942d07dcf9aSJohannes Berg unsigned long *state); 1943d07dcf9aSJohannes Berg bool netlink_policy_dump_loop(unsigned long *state); 1944d07dcf9aSJohannes Berg int netlink_policy_dump_write(struct sk_buff *skb, unsigned long state); 1945d07dcf9aSJohannes Berg 1946bfa83a9eSThomas Graf #endif 1947