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, 184b60b87fcSJohannes Berg NLA_EXACT_LEN, 185b60b87fcSJohannes Berg NLA_EXACT_LEN_WARN, 186bfa83a9eSThomas Graf __NLA_TYPE_MAX, 187bfa83a9eSThomas Graf }; 188bfa83a9eSThomas Graf 189bfa83a9eSThomas Graf #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1) 190bfa83a9eSThomas Graf 1913e48be05SJohannes Berg enum nla_policy_validation { 1923e48be05SJohannes Berg NLA_VALIDATE_NONE, 1933e48be05SJohannes Berg NLA_VALIDATE_RANGE, 1943e48be05SJohannes Berg NLA_VALIDATE_MIN, 1953e48be05SJohannes Berg NLA_VALIDATE_MAX, 19633188bd6SJohannes Berg NLA_VALIDATE_FUNCTION, 1973e48be05SJohannes Berg }; 1983e48be05SJohannes Berg 199bfa83a9eSThomas Graf /** 200bfa83a9eSThomas Graf * struct nla_policy - attribute validation policy 201bfa83a9eSThomas Graf * @type: Type of attribute or NLA_UNSPEC 2023e48be05SJohannes Berg * @validation_type: type of attribute validation done in addition to 20333188bd6SJohannes Berg * type-specific validation (e.g. range, function call), see 2043e48be05SJohannes Berg * &enum nla_policy_validation 205a5531a5dSThomas Graf * @len: Type specific length of payload 206bfa83a9eSThomas Graf * 207bfa83a9eSThomas Graf * Policies are defined as arrays of this struct, the array must be 208bfa83a9eSThomas Graf * accessible by attribute type up to the highest identifier to be expected. 209bfa83a9eSThomas Graf * 210a5531a5dSThomas Graf * Meaning of `len' field: 211a5531a5dSThomas Graf * NLA_STRING Maximum length of string 212a5531a5dSThomas Graf * NLA_NUL_STRING Maximum length of string (excluding NUL) 213a5531a5dSThomas Graf * NLA_FLAG Unused 214d30045a0SJohannes Berg * NLA_BINARY Maximum length of attribute payload 2151501d135SJohannes Berg * NLA_NESTED, 2161501d135SJohannes Berg * NLA_NESTED_ARRAY Length verification is done by checking len of 2179a659a35SJohannes Berg * nested header (or empty); len field is used if 2189a659a35SJohannes Berg * validation_data is also used, for the max attr 2199a659a35SJohannes Berg * number in the nested policy. 2204b6cc728SJohannes Berg * NLA_U8, NLA_U16, 2214b6cc728SJohannes Berg * NLA_U32, NLA_U64, 2224778e0beSJiri Pirko * NLA_S8, NLA_S16, 2234778e0beSJiri Pirko * NLA_S32, NLA_S64, 2244b6cc728SJohannes Berg * NLA_MSECS Leaving the length field zero will verify the 2254b6cc728SJohannes Berg * given type fits, using it verifies minimum length 2264b6cc728SJohannes Berg * just like "All other" 227568b742aSJohannes Berg * NLA_BITFIELD32 Unused 228568b742aSJohannes Berg * NLA_REJECT Unused 229b60b87fcSJohannes Berg * NLA_EXACT_LEN Attribute must have exactly this length, otherwise 230b60b87fcSJohannes Berg * it is rejected. 231b60b87fcSJohannes Berg * NLA_EXACT_LEN_WARN Attribute should have exactly this length, a warning 232b60b87fcSJohannes Berg * is logged if it is longer, shorter is rejected. 2334b6cc728SJohannes Berg * All other Minimum length of attribute payload 234a5531a5dSThomas Graf * 235568b742aSJohannes Berg * Meaning of `validation_data' field: 236568b742aSJohannes Berg * NLA_BITFIELD32 This is a 32-bit bitmap/bitselector attribute and 237568b742aSJohannes Berg * validation data must point to a u32 value of valid 238568b742aSJohannes Berg * flags 239568b742aSJohannes Berg * NLA_REJECT This attribute is always rejected and validation data 240568b742aSJohannes Berg * may point to a string to report as the error instead 241568b742aSJohannes Berg * of the generic one in extended ACK. 2429a659a35SJohannes Berg * NLA_NESTED Points to a nested policy to validate, must also set 2439a659a35SJohannes Berg * `len' to the max attribute number. 2449a659a35SJohannes Berg * Note that nla_parse() will validate, but of course not 2459a659a35SJohannes Berg * parse, the nested sub-policies. 2461501d135SJohannes Berg * NLA_NESTED_ARRAY Points to a nested policy to validate, must also set 2471501d135SJohannes Berg * `len' to the max attribute number. The difference to 2481501d135SJohannes Berg * NLA_NESTED is the structure - NLA_NESTED has the 2491501d135SJohannes Berg * nested attributes directly inside, while an array has 2501501d135SJohannes Berg * the nested attributes at another level down and the 2511501d135SJohannes Berg * attributes directly in the nesting don't matter. 2523e48be05SJohannes Berg * All other Unused - but note that it's a union 2533e48be05SJohannes Berg * 2543e48be05SJohannes Berg * Meaning of `min' and `max' fields, use via NLA_POLICY_MIN, NLA_POLICY_MAX 2553e48be05SJohannes Berg * and NLA_POLICY_RANGE: 2563e48be05SJohannes Berg * NLA_U8, 2573e48be05SJohannes Berg * NLA_U16, 2583e48be05SJohannes Berg * NLA_U32, 2593e48be05SJohannes Berg * NLA_U64, 2603e48be05SJohannes Berg * NLA_S8, 2613e48be05SJohannes Berg * NLA_S16, 2623e48be05SJohannes Berg * NLA_S32, 2633e48be05SJohannes Berg * NLA_S64 These are used depending on the validation_type 2643e48be05SJohannes Berg * field, if that is min/max/range then the minimum, 2653e48be05SJohannes Berg * maximum and both are used (respectively) to check 2663e48be05SJohannes Berg * the value of the integer attribute. 2673e48be05SJohannes Berg * Note that in the interest of code simplicity and 2683e48be05SJohannes Berg * struct size both limits are s16, so you cannot 2693e48be05SJohannes Berg * enforce a range that doesn't fall within the range 2703e48be05SJohannes Berg * of s16 - do that as usual in the code instead. 2713e48be05SJohannes Berg * All other Unused - but note that it's a union 272568b742aSJohannes Berg * 27333188bd6SJohannes Berg * Meaning of `validate' field, use via NLA_POLICY_VALIDATE_FN: 27433188bd6SJohannes Berg * NLA_BINARY Validation function called for the attribute, 27533188bd6SJohannes Berg * not compatible with use of the validation_data 27633188bd6SJohannes Berg * as in NLA_BITFIELD32, NLA_REJECT, NLA_NESTED and 27733188bd6SJohannes Berg * NLA_NESTED_ARRAY. 27833188bd6SJohannes Berg * All other Unused - but note that it's a union 27933188bd6SJohannes Berg * 280bfa83a9eSThomas Graf * Example: 281b54452b0SAlexey Dobriyan * static const struct nla_policy my_policy[ATTR_MAX+1] = { 282bfa83a9eSThomas Graf * [ATTR_FOO] = { .type = NLA_U16 }, 283d30045a0SJohannes Berg * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ }, 284a5531a5dSThomas Graf * [ATTR_BAZ] = { .len = sizeof(struct mystruct) }, 28564c83d83SJamal Hadi Salim * [ATTR_GOO] = { .type = NLA_BITFIELD32, .validation_data = &myvalidflags }, 286bfa83a9eSThomas Graf * }; 287bfa83a9eSThomas Graf */ 288bfa83a9eSThomas Graf struct nla_policy { 2893e48be05SJohannes Berg u8 type; 2903e48be05SJohannes Berg u8 validation_type; 291a5531a5dSThomas Graf u16 len; 2923e48be05SJohannes Berg union { 29348fde90aSJohannes Berg const void *validation_data; 2943e48be05SJohannes Berg struct { 2953e48be05SJohannes Berg s16 min, max; 2963e48be05SJohannes Berg }; 29733188bd6SJohannes Berg int (*validate)(const struct nlattr *attr, 29833188bd6SJohannes Berg struct netlink_ext_ack *extack); 2993e48be05SJohannes Berg }; 300bfa83a9eSThomas Graf }; 301bfa83a9eSThomas Graf 302b60b87fcSJohannes Berg #define NLA_POLICY_EXACT_LEN(_len) { .type = NLA_EXACT_LEN, .len = _len } 303b60b87fcSJohannes Berg #define NLA_POLICY_EXACT_LEN_WARN(_len) { .type = NLA_EXACT_LEN_WARN, \ 304b60b87fcSJohannes Berg .len = _len } 305b60b87fcSJohannes Berg 306b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR NLA_POLICY_EXACT_LEN(ETH_ALEN) 307b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR_COMPAT NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN) 308b60b87fcSJohannes Berg 3099a659a35SJohannes Berg #define NLA_POLICY_NESTED(maxattr, policy) \ 3109a659a35SJohannes Berg { .type = NLA_NESTED, .validation_data = policy, .len = maxattr } 3111501d135SJohannes Berg #define NLA_POLICY_NESTED_ARRAY(maxattr, policy) \ 3121501d135SJohannes Berg { .type = NLA_NESTED_ARRAY, .validation_data = policy, .len = maxattr } 3139a659a35SJohannes Berg 3143e48be05SJohannes Berg #define __NLA_ENSURE(condition) (sizeof(char[1 - 2*!(condition)]) - 1) 3153e48be05SJohannes Berg #define NLA_ENSURE_INT_TYPE(tp) \ 3163e48be05SJohannes Berg (__NLA_ENSURE(tp == NLA_S8 || tp == NLA_U8 || \ 3173e48be05SJohannes Berg tp == NLA_S16 || tp == NLA_U16 || \ 3183e48be05SJohannes Berg tp == NLA_S32 || tp == NLA_U32 || \ 3193e48be05SJohannes Berg tp == NLA_S64 || tp == NLA_U64) + tp) 32033188bd6SJohannes Berg #define NLA_ENSURE_NO_VALIDATION_PTR(tp) \ 32133188bd6SJohannes Berg (__NLA_ENSURE(tp != NLA_BITFIELD32 && \ 32233188bd6SJohannes Berg tp != NLA_REJECT && \ 32333188bd6SJohannes Berg tp != NLA_NESTED && \ 32433188bd6SJohannes Berg tp != NLA_NESTED_ARRAY) + tp) 3253e48be05SJohannes Berg 3263e48be05SJohannes Berg #define NLA_POLICY_RANGE(tp, _min, _max) { \ 3273e48be05SJohannes Berg .type = NLA_ENSURE_INT_TYPE(tp), \ 3283e48be05SJohannes Berg .validation_type = NLA_VALIDATE_RANGE, \ 3293e48be05SJohannes Berg .min = _min, \ 3303e48be05SJohannes Berg .max = _max \ 3313e48be05SJohannes Berg } 3323e48be05SJohannes Berg 3333e48be05SJohannes Berg #define NLA_POLICY_MIN(tp, _min) { \ 3343e48be05SJohannes Berg .type = NLA_ENSURE_INT_TYPE(tp), \ 3353e48be05SJohannes Berg .validation_type = NLA_VALIDATE_MIN, \ 3363e48be05SJohannes Berg .min = _min, \ 3373e48be05SJohannes Berg } 3383e48be05SJohannes Berg 3393e48be05SJohannes Berg #define NLA_POLICY_MAX(tp, _max) { \ 3403e48be05SJohannes Berg .type = NLA_ENSURE_INT_TYPE(tp), \ 3413e48be05SJohannes Berg .validation_type = NLA_VALIDATE_MAX, \ 3423e48be05SJohannes Berg .max = _max, \ 3433e48be05SJohannes Berg } 3443e48be05SJohannes Berg 34533188bd6SJohannes Berg #define NLA_POLICY_VALIDATE_FN(tp, fn, ...) { \ 34633188bd6SJohannes Berg .type = NLA_ENSURE_NO_VALIDATION_PTR(tp), \ 34733188bd6SJohannes Berg .validation_type = NLA_VALIDATE_FUNCTION, \ 34833188bd6SJohannes Berg .validate = fn, \ 34933188bd6SJohannes Berg .len = __VA_ARGS__ + 0, \ 35033188bd6SJohannes Berg } 35133188bd6SJohannes Berg 3524e902c57SThomas Graf /** 3534e902c57SThomas Graf * struct nl_info - netlink source information 3544e902c57SThomas Graf * @nlh: Netlink message header of original request 35515e47304SEric W. Biederman * @portid: Netlink PORTID of requesting application 3564e902c57SThomas Graf */ 3574e902c57SThomas Graf struct nl_info { 3584e902c57SThomas Graf struct nlmsghdr *nlh; 3594d1169c1SDenis V. Lunev struct net *nl_net; 36015e47304SEric W. Biederman u32 portid; 3613b1137feSDavid Ahern bool skip_notify; 3624e902c57SThomas Graf }; 3634e902c57SThomas Graf 3644f69053bSJoe Perches int netlink_rcv_skb(struct sk_buff *skb, 3652d4bc933SJohannes Berg int (*cb)(struct sk_buff *, struct nlmsghdr *, 3662d4bc933SJohannes Berg struct netlink_ext_ack *)); 3674f69053bSJoe Perches int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid, 3684f69053bSJoe Perches unsigned int group, int report, gfp_t flags); 36982ace47aSThomas Graf 3704f69053bSJoe Perches int nla_validate(const struct nlattr *head, int len, int maxtype, 371fceb6435SJohannes Berg const struct nla_policy *policy, 372fceb6435SJohannes Berg struct netlink_ext_ack *extack); 3734f69053bSJoe Perches int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, 374fceb6435SJohannes Berg int len, const struct nla_policy *policy, 375fceb6435SJohannes Berg struct netlink_ext_ack *extack); 376a5f6cba2SDavid Ahern int nla_parse_strict(struct nlattr **tb, int maxtype, const struct nlattr *head, 377a5f6cba2SDavid Ahern int len, const struct nla_policy *policy, 378a5f6cba2SDavid Ahern struct netlink_ext_ack *extack); 3794f69053bSJoe Perches int nla_policy_len(const struct nla_policy *, int); 3804f69053bSJoe Perches struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype); 3814f69053bSJoe Perches size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize); 3822cf0c8b3SPhil Sutter char *nla_strdup(const struct nlattr *nla, gfp_t flags); 3834f69053bSJoe Perches int nla_memcpy(void *dest, const struct nlattr *src, int count); 3844f69053bSJoe Perches int nla_memcmp(const struct nlattr *nla, const void *data, size_t size); 3854f69053bSJoe Perches int nla_strcmp(const struct nlattr *nla, const char *str); 3864f69053bSJoe Perches struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 387089bf1a6SNicolas Dichtel struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype, 388089bf1a6SNicolas Dichtel int attrlen, int padattr); 3894f69053bSJoe Perches void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 3904f69053bSJoe Perches struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 391089bf1a6SNicolas Dichtel struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, 392089bf1a6SNicolas Dichtel int attrlen, int padattr); 3934f69053bSJoe Perches void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 3944f69053bSJoe Perches void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, 395fe4944e5SThomas Graf const void *data); 396089bf1a6SNicolas Dichtel void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 397089bf1a6SNicolas Dichtel const void *data, int padattr); 3984f69053bSJoe Perches void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 3994f69053bSJoe Perches int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data); 400089bf1a6SNicolas Dichtel int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 401089bf1a6SNicolas Dichtel const void *data, int padattr); 4024f69053bSJoe Perches int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 4034f69053bSJoe Perches int nla_append(struct sk_buff *skb, int attrlen, const void *data); 404bfa83a9eSThomas Graf 405bfa83a9eSThomas Graf /************************************************************************** 406bfa83a9eSThomas Graf * Netlink Messages 407bfa83a9eSThomas Graf **************************************************************************/ 408bfa83a9eSThomas Graf 409bfa83a9eSThomas Graf /** 410bfa83a9eSThomas Graf * nlmsg_msg_size - length of netlink message not including padding 411bfa83a9eSThomas Graf * @payload: length of message payload 412bfa83a9eSThomas Graf */ 413bfa83a9eSThomas Graf static inline int nlmsg_msg_size(int payload) 414bfa83a9eSThomas Graf { 415bfa83a9eSThomas Graf return NLMSG_HDRLEN + payload; 416bfa83a9eSThomas Graf } 417bfa83a9eSThomas Graf 418bfa83a9eSThomas Graf /** 419bfa83a9eSThomas Graf * nlmsg_total_size - length of netlink message including padding 420bfa83a9eSThomas Graf * @payload: length of message payload 421bfa83a9eSThomas Graf */ 422bfa83a9eSThomas Graf static inline int nlmsg_total_size(int payload) 423bfa83a9eSThomas Graf { 424bfa83a9eSThomas Graf return NLMSG_ALIGN(nlmsg_msg_size(payload)); 425bfa83a9eSThomas Graf } 426bfa83a9eSThomas Graf 427bfa83a9eSThomas Graf /** 428bfa83a9eSThomas Graf * nlmsg_padlen - length of padding at the message's tail 429bfa83a9eSThomas Graf * @payload: length of message payload 430bfa83a9eSThomas Graf */ 431bfa83a9eSThomas Graf static inline int nlmsg_padlen(int payload) 432bfa83a9eSThomas Graf { 433bfa83a9eSThomas Graf return nlmsg_total_size(payload) - nlmsg_msg_size(payload); 434bfa83a9eSThomas Graf } 435bfa83a9eSThomas Graf 436bfa83a9eSThomas Graf /** 437bfa83a9eSThomas Graf * nlmsg_data - head of message payload 43870f23fd6SJustin P. Mattock * @nlh: netlink message header 439bfa83a9eSThomas Graf */ 440bfa83a9eSThomas Graf static inline void *nlmsg_data(const struct nlmsghdr *nlh) 441bfa83a9eSThomas Graf { 442bfa83a9eSThomas Graf return (unsigned char *) nlh + NLMSG_HDRLEN; 443bfa83a9eSThomas Graf } 444bfa83a9eSThomas Graf 445bfa83a9eSThomas Graf /** 446bfa83a9eSThomas Graf * nlmsg_len - length of message payload 447bfa83a9eSThomas Graf * @nlh: netlink message header 448bfa83a9eSThomas Graf */ 449bfa83a9eSThomas Graf static inline int nlmsg_len(const struct nlmsghdr *nlh) 450bfa83a9eSThomas Graf { 451bfa83a9eSThomas Graf return nlh->nlmsg_len - NLMSG_HDRLEN; 452bfa83a9eSThomas Graf } 453bfa83a9eSThomas Graf 454bfa83a9eSThomas Graf /** 455bfa83a9eSThomas Graf * nlmsg_attrdata - head of attributes data 456bfa83a9eSThomas Graf * @nlh: netlink message header 457bfa83a9eSThomas Graf * @hdrlen: length of family specific header 458bfa83a9eSThomas Graf */ 459bfa83a9eSThomas Graf static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, 460bfa83a9eSThomas Graf int hdrlen) 461bfa83a9eSThomas Graf { 462bfa83a9eSThomas Graf unsigned char *data = nlmsg_data(nlh); 463bfa83a9eSThomas Graf return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen)); 464bfa83a9eSThomas Graf } 465bfa83a9eSThomas Graf 466bfa83a9eSThomas Graf /** 467bfa83a9eSThomas Graf * nlmsg_attrlen - length of attributes data 468bfa83a9eSThomas Graf * @nlh: netlink message header 469bfa83a9eSThomas Graf * @hdrlen: length of family specific header 470bfa83a9eSThomas Graf */ 471bfa83a9eSThomas Graf static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen) 472bfa83a9eSThomas Graf { 473bfa83a9eSThomas Graf return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen); 474bfa83a9eSThomas Graf } 475bfa83a9eSThomas Graf 476bfa83a9eSThomas Graf /** 477bfa83a9eSThomas Graf * nlmsg_ok - check if the netlink message fits into the remaining bytes 478bfa83a9eSThomas Graf * @nlh: netlink message header 479bfa83a9eSThomas Graf * @remaining: number of bytes remaining in message stream 480bfa83a9eSThomas Graf */ 481bfa83a9eSThomas Graf static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining) 482bfa83a9eSThomas Graf { 483619e803dSVegard Nossum return (remaining >= (int) sizeof(struct nlmsghdr) && 484bfa83a9eSThomas Graf nlh->nlmsg_len >= sizeof(struct nlmsghdr) && 485bfa83a9eSThomas Graf nlh->nlmsg_len <= remaining); 486bfa83a9eSThomas Graf } 487bfa83a9eSThomas Graf 488bfa83a9eSThomas Graf /** 489bfa83a9eSThomas Graf * nlmsg_next - next netlink message in message stream 490bfa83a9eSThomas Graf * @nlh: netlink message header 491bfa83a9eSThomas Graf * @remaining: number of bytes remaining in message stream 492bfa83a9eSThomas Graf * 493bfa83a9eSThomas Graf * Returns the next netlink message in the message stream and 494bfa83a9eSThomas Graf * decrements remaining by the size of the current message. 495bfa83a9eSThomas Graf */ 4963654654fSJan Engelhardt static inline struct nlmsghdr * 4973654654fSJan Engelhardt nlmsg_next(const struct nlmsghdr *nlh, int *remaining) 498bfa83a9eSThomas Graf { 499bfa83a9eSThomas Graf int totlen = NLMSG_ALIGN(nlh->nlmsg_len); 500bfa83a9eSThomas Graf 501bfa83a9eSThomas Graf *remaining -= totlen; 502bfa83a9eSThomas Graf 503bfa83a9eSThomas Graf return (struct nlmsghdr *) ((unsigned char *) nlh + totlen); 504bfa83a9eSThomas Graf } 505bfa83a9eSThomas Graf 506bfa83a9eSThomas Graf /** 507bfa83a9eSThomas Graf * nlmsg_parse - parse attributes of a netlink message 508bfa83a9eSThomas Graf * @nlh: netlink message header 509bfa83a9eSThomas Graf * @hdrlen: length of family specific header 510bfa83a9eSThomas Graf * @tb: destination array with maxtype+1 elements 511bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 512bfa83a9eSThomas Graf * @policy: validation policy 513fceb6435SJohannes Berg * @extack: extended ACK report struct 514bfa83a9eSThomas Graf * 515bfa83a9eSThomas Graf * See nla_parse() 516bfa83a9eSThomas Graf */ 5173a6c2b41SPatrick McHardy static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen, 518bfa83a9eSThomas Graf struct nlattr *tb[], int maxtype, 519fceb6435SJohannes Berg const struct nla_policy *policy, 520fceb6435SJohannes Berg struct netlink_ext_ack *extack) 521bfa83a9eSThomas Graf { 5223d0d4337SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) { 5233d0d4337SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header length"); 524bfa83a9eSThomas Graf return -EINVAL; 5253d0d4337SDavid Ahern } 526bfa83a9eSThomas Graf 527bfa83a9eSThomas Graf return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), 528fceb6435SJohannes Berg nlmsg_attrlen(nlh, hdrlen), policy, extack); 529bfa83a9eSThomas Graf } 530bfa83a9eSThomas Graf 531a5f6cba2SDavid Ahern static inline int nlmsg_parse_strict(const struct nlmsghdr *nlh, int hdrlen, 532a5f6cba2SDavid Ahern struct nlattr *tb[], int maxtype, 533a5f6cba2SDavid Ahern const struct nla_policy *policy, 534a5f6cba2SDavid Ahern struct netlink_ext_ack *extack) 535a5f6cba2SDavid Ahern { 536a5f6cba2SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) { 537a5f6cba2SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header length"); 538a5f6cba2SDavid Ahern return -EINVAL; 539a5f6cba2SDavid Ahern } 540a5f6cba2SDavid Ahern 541a5f6cba2SDavid Ahern return nla_parse_strict(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), 542a5f6cba2SDavid Ahern nlmsg_attrlen(nlh, hdrlen), policy, extack); 543a5f6cba2SDavid Ahern } 544a5f6cba2SDavid Ahern 545bfa83a9eSThomas Graf /** 546bfa83a9eSThomas Graf * nlmsg_find_attr - find a specific attribute in a netlink message 547bfa83a9eSThomas Graf * @nlh: netlink message header 548bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 549bfa83a9eSThomas Graf * @attrtype: type of attribute to look for 550bfa83a9eSThomas Graf * 551bfa83a9eSThomas Graf * Returns the first attribute which matches the specified type. 552bfa83a9eSThomas Graf */ 5536b8c92baSNelson Elhage static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh, 554bfa83a9eSThomas Graf int hdrlen, int attrtype) 555bfa83a9eSThomas Graf { 556bfa83a9eSThomas Graf return nla_find(nlmsg_attrdata(nlh, hdrlen), 557bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), attrtype); 558bfa83a9eSThomas Graf } 559bfa83a9eSThomas Graf 560bfa83a9eSThomas Graf /** 561bfa83a9eSThomas Graf * nlmsg_validate - validate a netlink message including attributes 562bfa83a9eSThomas Graf * @nlh: netlinket message header 563bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 564bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 565bfa83a9eSThomas Graf * @policy: validation policy 566fceb6435SJohannes Berg * @extack: extended ACK report struct 567bfa83a9eSThomas Graf */ 5683654654fSJan Engelhardt static inline int nlmsg_validate(const struct nlmsghdr *nlh, 5693654654fSJan Engelhardt int hdrlen, int maxtype, 570fceb6435SJohannes Berg const struct nla_policy *policy, 571fceb6435SJohannes Berg struct netlink_ext_ack *extack) 572bfa83a9eSThomas Graf { 573bfa83a9eSThomas Graf if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) 574bfa83a9eSThomas Graf return -EINVAL; 575bfa83a9eSThomas Graf 576bfa83a9eSThomas Graf return nla_validate(nlmsg_attrdata(nlh, hdrlen), 577fceb6435SJohannes Berg nlmsg_attrlen(nlh, hdrlen), maxtype, policy, 578fceb6435SJohannes Berg extack); 579bfa83a9eSThomas Graf } 580bfa83a9eSThomas Graf 581bfa83a9eSThomas Graf /** 58297676b6bSThomas Graf * nlmsg_report - need to report back to application? 58397676b6bSThomas Graf * @nlh: netlink message header 58497676b6bSThomas Graf * 58597676b6bSThomas Graf * Returns 1 if a report back to the application is requested. 58697676b6bSThomas Graf */ 5873a6c2b41SPatrick McHardy static inline int nlmsg_report(const struct nlmsghdr *nlh) 58897676b6bSThomas Graf { 58997676b6bSThomas Graf return !!(nlh->nlmsg_flags & NLM_F_ECHO); 59097676b6bSThomas Graf } 59197676b6bSThomas Graf 59297676b6bSThomas Graf /** 593bfa83a9eSThomas Graf * nlmsg_for_each_attr - iterate over a stream of attributes 594bfa83a9eSThomas Graf * @pos: loop counter, set to current attribute 595bfa83a9eSThomas Graf * @nlh: netlink message header 596bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 597bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 598bfa83a9eSThomas Graf */ 599bfa83a9eSThomas Graf #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \ 600bfa83a9eSThomas Graf nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \ 601bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), rem) 602bfa83a9eSThomas Graf 603bfa83a9eSThomas Graf /** 604bfa83a9eSThomas Graf * nlmsg_put - Add a new netlink message to an skb 605bfa83a9eSThomas Graf * @skb: socket buffer to store message in 6062c6ba4b1SNicolas Dichtel * @portid: netlink PORTID of requesting application 607bfa83a9eSThomas Graf * @seq: sequence number of message 608bfa83a9eSThomas Graf * @type: message type 609bfa83a9eSThomas Graf * @payload: length of message payload 610bfa83a9eSThomas Graf * @flags: message flags 611bfa83a9eSThomas Graf * 612bfa83a9eSThomas Graf * Returns NULL if the tailroom of the skb is insufficient to store 613bfa83a9eSThomas Graf * the message header and payload. 614bfa83a9eSThomas Graf */ 61515e47304SEric W. Biederman static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, 616bfa83a9eSThomas Graf int type, int payload, int flags) 617bfa83a9eSThomas Graf { 618bfa83a9eSThomas Graf if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload))) 619bfa83a9eSThomas Graf return NULL; 620bfa83a9eSThomas Graf 62115e47304SEric W. Biederman return __nlmsg_put(skb, portid, seq, type, payload, flags); 622bfa83a9eSThomas Graf } 623bfa83a9eSThomas Graf 624bfa83a9eSThomas Graf /** 625bfa83a9eSThomas Graf * nlmsg_put_answer - Add a new callback based netlink message to an skb 626bfa83a9eSThomas Graf * @skb: socket buffer to store message in 627bfa83a9eSThomas Graf * @cb: netlink callback 628bfa83a9eSThomas Graf * @type: message type 629bfa83a9eSThomas Graf * @payload: length of message payload 630bfa83a9eSThomas Graf * @flags: message flags 631bfa83a9eSThomas Graf * 632bfa83a9eSThomas Graf * Returns NULL if the tailroom of the skb is insufficient to store 633bfa83a9eSThomas Graf * the message header and payload. 634bfa83a9eSThomas Graf */ 635bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb, 636bfa83a9eSThomas Graf struct netlink_callback *cb, 637bfa83a9eSThomas Graf int type, int payload, 638bfa83a9eSThomas Graf int flags) 639bfa83a9eSThomas Graf { 64015e47304SEric W. Biederman return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 641bfa83a9eSThomas Graf type, payload, flags); 642bfa83a9eSThomas Graf } 643bfa83a9eSThomas Graf 644bfa83a9eSThomas Graf /** 645bfa83a9eSThomas Graf * nlmsg_new - Allocate a new netlink message 646339bf98fSThomas Graf * @payload: size of the message payload 647fe4944e5SThomas Graf * @flags: the type of memory to allocate. 648bfa83a9eSThomas Graf * 649339bf98fSThomas Graf * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known 650339bf98fSThomas Graf * and a good default is needed. 651bfa83a9eSThomas Graf */ 652339bf98fSThomas Graf static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags) 653bfa83a9eSThomas Graf { 654339bf98fSThomas Graf return alloc_skb(nlmsg_total_size(payload), flags); 655bfa83a9eSThomas Graf } 656bfa83a9eSThomas Graf 657bfa83a9eSThomas Graf /** 658bfa83a9eSThomas Graf * nlmsg_end - Finalize a netlink message 659bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 660bfa83a9eSThomas Graf * @nlh: netlink message header 661bfa83a9eSThomas Graf * 662bfa83a9eSThomas Graf * Corrects the netlink message header to include the appeneded 663bfa83a9eSThomas Graf * attributes. Only necessary if attributes have been added to 664bfa83a9eSThomas Graf * the message. 665bfa83a9eSThomas Graf */ 666053c095aSJohannes Berg static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh) 667bfa83a9eSThomas Graf { 66827a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh; 669bfa83a9eSThomas Graf } 670bfa83a9eSThomas Graf 671bfa83a9eSThomas Graf /** 672fe4944e5SThomas Graf * nlmsg_get_pos - return current position in netlink message 673fe4944e5SThomas Graf * @skb: socket buffer the message is stored in 674fe4944e5SThomas Graf * 675fe4944e5SThomas Graf * Returns a pointer to the current tail of the message. 676fe4944e5SThomas Graf */ 677fe4944e5SThomas Graf static inline void *nlmsg_get_pos(struct sk_buff *skb) 678fe4944e5SThomas Graf { 67927a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 680fe4944e5SThomas Graf } 681fe4944e5SThomas Graf 682fe4944e5SThomas Graf /** 683fe4944e5SThomas Graf * nlmsg_trim - Trim message to a mark 684fe4944e5SThomas Graf * @skb: socket buffer the message is stored in 685fe4944e5SThomas Graf * @mark: mark to trim to 686fe4944e5SThomas Graf * 687bc3ed28cSThomas Graf * Trims the message to the provided mark. 688fe4944e5SThomas Graf */ 689bc3ed28cSThomas Graf static inline void nlmsg_trim(struct sk_buff *skb, const void *mark) 690fe4944e5SThomas Graf { 691149118d8SThomas Graf if (mark) { 692149118d8SThomas Graf WARN_ON((unsigned char *) mark < skb->data); 693fe4944e5SThomas Graf skb_trim(skb, (unsigned char *) mark - skb->data); 694fe4944e5SThomas Graf } 695149118d8SThomas Graf } 696fe4944e5SThomas Graf 697fe4944e5SThomas Graf /** 698bfa83a9eSThomas Graf * nlmsg_cancel - Cancel construction of a netlink message 699bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 700bfa83a9eSThomas Graf * @nlh: netlink message header 701bfa83a9eSThomas Graf * 702bfa83a9eSThomas Graf * Removes the complete netlink message including all 703bc3ed28cSThomas Graf * attributes from the socket buffer again. 704bfa83a9eSThomas Graf */ 705bc3ed28cSThomas Graf static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh) 706bfa83a9eSThomas Graf { 707bc3ed28cSThomas Graf nlmsg_trim(skb, nlh); 708bfa83a9eSThomas Graf } 709bfa83a9eSThomas Graf 710bfa83a9eSThomas Graf /** 711bfa83a9eSThomas Graf * nlmsg_free - free a netlink message 712bfa83a9eSThomas Graf * @skb: socket buffer of netlink message 713bfa83a9eSThomas Graf */ 714bfa83a9eSThomas Graf static inline void nlmsg_free(struct sk_buff *skb) 715bfa83a9eSThomas Graf { 716bfa83a9eSThomas Graf kfree_skb(skb); 717bfa83a9eSThomas Graf } 718bfa83a9eSThomas Graf 719bfa83a9eSThomas Graf /** 720bfa83a9eSThomas Graf * nlmsg_multicast - multicast a netlink message 721bfa83a9eSThomas Graf * @sk: netlink socket to spread messages to 722bfa83a9eSThomas Graf * @skb: netlink message as socket buffer 72315e47304SEric W. Biederman * @portid: own netlink portid to avoid sending to yourself 724bfa83a9eSThomas Graf * @group: multicast group id 725d387f6adSThomas Graf * @flags: allocation flags 726bfa83a9eSThomas Graf */ 727bfa83a9eSThomas Graf static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, 72815e47304SEric W. Biederman u32 portid, unsigned int group, gfp_t flags) 729bfa83a9eSThomas Graf { 730bfa83a9eSThomas Graf int err; 731bfa83a9eSThomas Graf 732bfa83a9eSThomas Graf NETLINK_CB(skb).dst_group = group; 733bfa83a9eSThomas Graf 73415e47304SEric W. Biederman err = netlink_broadcast(sk, skb, portid, group, flags); 735bfa83a9eSThomas Graf if (err > 0) 736bfa83a9eSThomas Graf err = 0; 737bfa83a9eSThomas Graf 738bfa83a9eSThomas Graf return err; 739bfa83a9eSThomas Graf } 740bfa83a9eSThomas Graf 741bfa83a9eSThomas Graf /** 742bfa83a9eSThomas Graf * nlmsg_unicast - unicast a netlink message 743bfa83a9eSThomas Graf * @sk: netlink socket to spread message to 744bfa83a9eSThomas Graf * @skb: netlink message as socket buffer 74515e47304SEric W. Biederman * @portid: netlink portid of the destination socket 746bfa83a9eSThomas Graf */ 74715e47304SEric W. Biederman static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid) 748bfa83a9eSThomas Graf { 749bfa83a9eSThomas Graf int err; 750bfa83a9eSThomas Graf 75115e47304SEric W. Biederman err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT); 752bfa83a9eSThomas Graf if (err > 0) 753bfa83a9eSThomas Graf err = 0; 754bfa83a9eSThomas Graf 755bfa83a9eSThomas Graf return err; 756bfa83a9eSThomas Graf } 757bfa83a9eSThomas Graf 758bfa83a9eSThomas Graf /** 759bfa83a9eSThomas Graf * nlmsg_for_each_msg - iterate over a stream of messages 760bfa83a9eSThomas Graf * @pos: loop counter, set to current message 761bfa83a9eSThomas Graf * @head: head of message stream 762bfa83a9eSThomas Graf * @len: length of message stream 763bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 764bfa83a9eSThomas Graf */ 765bfa83a9eSThomas Graf #define nlmsg_for_each_msg(pos, head, len, rem) \ 766bfa83a9eSThomas Graf for (pos = head, rem = len; \ 767bfa83a9eSThomas Graf nlmsg_ok(pos, rem); \ 768bfa83a9eSThomas Graf pos = nlmsg_next(pos, &(rem))) 769bfa83a9eSThomas Graf 770670dc283SJohannes Berg /** 771670dc283SJohannes Berg * nl_dump_check_consistent - check if sequence is consistent and advertise if not 772670dc283SJohannes Berg * @cb: netlink callback structure that stores the sequence number 773670dc283SJohannes Berg * @nlh: netlink message header to write the flag to 774670dc283SJohannes Berg * 775670dc283SJohannes Berg * This function checks if the sequence (generation) number changed during dump 776670dc283SJohannes Berg * and if it did, advertises it in the netlink message header. 777670dc283SJohannes Berg * 778670dc283SJohannes Berg * The correct way to use it is to set cb->seq to the generation counter when 779670dc283SJohannes Berg * all locks for dumping have been acquired, and then call this function for 780670dc283SJohannes Berg * each message that is generated. 781670dc283SJohannes Berg * 782670dc283SJohannes Berg * Note that due to initialisation concerns, 0 is an invalid sequence number 783670dc283SJohannes Berg * and must not be used by code that uses this functionality. 784670dc283SJohannes Berg */ 785670dc283SJohannes Berg static inline void 786670dc283SJohannes Berg nl_dump_check_consistent(struct netlink_callback *cb, 787670dc283SJohannes Berg struct nlmsghdr *nlh) 788670dc283SJohannes Berg { 789670dc283SJohannes Berg if (cb->prev_seq && cb->seq != cb->prev_seq) 790670dc283SJohannes Berg nlh->nlmsg_flags |= NLM_F_DUMP_INTR; 791670dc283SJohannes Berg cb->prev_seq = cb->seq; 792670dc283SJohannes Berg } 793670dc283SJohannes Berg 794bfa83a9eSThomas Graf /************************************************************************** 795bfa83a9eSThomas Graf * Netlink Attributes 796bfa83a9eSThomas Graf **************************************************************************/ 797bfa83a9eSThomas Graf 798bfa83a9eSThomas Graf /** 799bfa83a9eSThomas Graf * nla_attr_size - length of attribute not including padding 800bfa83a9eSThomas Graf * @payload: length of payload 801bfa83a9eSThomas Graf */ 802bfa83a9eSThomas Graf static inline int nla_attr_size(int payload) 803bfa83a9eSThomas Graf { 804bfa83a9eSThomas Graf return NLA_HDRLEN + payload; 805bfa83a9eSThomas Graf } 806bfa83a9eSThomas Graf 807bfa83a9eSThomas Graf /** 808bfa83a9eSThomas Graf * nla_total_size - total length of attribute including padding 809bfa83a9eSThomas Graf * @payload: length of payload 810bfa83a9eSThomas Graf */ 811bfa83a9eSThomas Graf static inline int nla_total_size(int payload) 812bfa83a9eSThomas Graf { 813bfa83a9eSThomas Graf return NLA_ALIGN(nla_attr_size(payload)); 814bfa83a9eSThomas Graf } 815bfa83a9eSThomas Graf 816bfa83a9eSThomas Graf /** 817bfa83a9eSThomas Graf * nla_padlen - length of padding at the tail of attribute 818bfa83a9eSThomas Graf * @payload: length of payload 819bfa83a9eSThomas Graf */ 820bfa83a9eSThomas Graf static inline int nla_padlen(int payload) 821bfa83a9eSThomas Graf { 822bfa83a9eSThomas Graf return nla_total_size(payload) - nla_attr_size(payload); 823bfa83a9eSThomas Graf } 824bfa83a9eSThomas Graf 825bfa83a9eSThomas Graf /** 8268f4c1f9bSThomas Graf * nla_type - attribute type 8278f4c1f9bSThomas Graf * @nla: netlink attribute 8288f4c1f9bSThomas Graf */ 8298f4c1f9bSThomas Graf static inline int nla_type(const struct nlattr *nla) 8308f4c1f9bSThomas Graf { 8318f4c1f9bSThomas Graf return nla->nla_type & NLA_TYPE_MASK; 8328f4c1f9bSThomas Graf } 8338f4c1f9bSThomas Graf 8348f4c1f9bSThomas Graf /** 835bfa83a9eSThomas Graf * nla_data - head of payload 836bfa83a9eSThomas Graf * @nla: netlink attribute 837bfa83a9eSThomas Graf */ 838bfa83a9eSThomas Graf static inline void *nla_data(const struct nlattr *nla) 839bfa83a9eSThomas Graf { 840bfa83a9eSThomas Graf return (char *) nla + NLA_HDRLEN; 841bfa83a9eSThomas Graf } 842bfa83a9eSThomas Graf 843bfa83a9eSThomas Graf /** 844bfa83a9eSThomas Graf * nla_len - length of payload 845bfa83a9eSThomas Graf * @nla: netlink attribute 846bfa83a9eSThomas Graf */ 847bfa83a9eSThomas Graf static inline int nla_len(const struct nlattr *nla) 848bfa83a9eSThomas Graf { 849bfa83a9eSThomas Graf return nla->nla_len - NLA_HDRLEN; 850bfa83a9eSThomas Graf } 851bfa83a9eSThomas Graf 852bfa83a9eSThomas Graf /** 853bfa83a9eSThomas Graf * nla_ok - check if the netlink attribute fits into the remaining bytes 854bfa83a9eSThomas Graf * @nla: netlink attribute 855bfa83a9eSThomas Graf * @remaining: number of bytes remaining in attribute stream 856bfa83a9eSThomas Graf */ 857bfa83a9eSThomas Graf static inline int nla_ok(const struct nlattr *nla, int remaining) 858bfa83a9eSThomas Graf { 8593e1ed981SAlexey Dobriyan return remaining >= (int) sizeof(*nla) && 8603e1ed981SAlexey Dobriyan nla->nla_len >= sizeof(*nla) && 861bfa83a9eSThomas Graf nla->nla_len <= remaining; 862bfa83a9eSThomas Graf } 863bfa83a9eSThomas Graf 864bfa83a9eSThomas Graf /** 865d1ec3b77SPierre Ynard * nla_next - next netlink attribute in attribute stream 866bfa83a9eSThomas Graf * @nla: netlink attribute 867bfa83a9eSThomas Graf * @remaining: number of bytes remaining in attribute stream 868bfa83a9eSThomas Graf * 869bfa83a9eSThomas Graf * Returns the next netlink attribute in the attribute stream and 870bfa83a9eSThomas Graf * decrements remaining by the size of the current attribute. 871bfa83a9eSThomas Graf */ 872bfa83a9eSThomas Graf static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining) 873bfa83a9eSThomas Graf { 8743b2c75d3SAlexey Dobriyan unsigned int totlen = NLA_ALIGN(nla->nla_len); 875bfa83a9eSThomas Graf 876bfa83a9eSThomas Graf *remaining -= totlen; 877bfa83a9eSThomas Graf return (struct nlattr *) ((char *) nla + totlen); 878bfa83a9eSThomas Graf } 879bfa83a9eSThomas Graf 880bfa83a9eSThomas Graf /** 881fe4944e5SThomas Graf * nla_find_nested - find attribute in a set of nested attributes 882fe4944e5SThomas Graf * @nla: attribute containing the nested attributes 883fe4944e5SThomas Graf * @attrtype: type of attribute to look for 884fe4944e5SThomas Graf * 885fe4944e5SThomas Graf * Returns the first attribute which matches the specified type. 886fe4944e5SThomas Graf */ 8873654654fSJan Engelhardt static inline struct nlattr * 8883654654fSJan Engelhardt nla_find_nested(const struct nlattr *nla, int attrtype) 889fe4944e5SThomas Graf { 890fe4944e5SThomas Graf return nla_find(nla_data(nla), nla_len(nla), attrtype); 891fe4944e5SThomas Graf } 892fe4944e5SThomas Graf 893fe4944e5SThomas Graf /** 894bfa83a9eSThomas Graf * nla_parse_nested - parse nested attributes 895bfa83a9eSThomas Graf * @tb: destination array with maxtype+1 elements 896bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 897bfa83a9eSThomas Graf * @nla: attribute containing the nested attributes 898bfa83a9eSThomas Graf * @policy: validation policy 899fceb6435SJohannes Berg * @extack: extended ACK report struct 900bfa83a9eSThomas Graf * 901bfa83a9eSThomas Graf * See nla_parse() 902bfa83a9eSThomas Graf */ 903bfa83a9eSThomas Graf static inline int nla_parse_nested(struct nlattr *tb[], int maxtype, 904b057efd4SPatrick McHardy const struct nlattr *nla, 905fceb6435SJohannes Berg const struct nla_policy *policy, 906fceb6435SJohannes Berg struct netlink_ext_ack *extack) 907bfa83a9eSThomas Graf { 908fceb6435SJohannes Berg return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy, 909fceb6435SJohannes Berg extack); 910bfa83a9eSThomas Graf } 9111092cb21SPatrick McHardy 9121092cb21SPatrick McHardy /** 913d1ec3b77SPierre Ynard * nla_put_u8 - Add a u8 netlink attribute to a socket buffer 914bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 915bfa83a9eSThomas Graf * @attrtype: attribute type 916bfa83a9eSThomas Graf * @value: numeric value 917bfa83a9eSThomas Graf */ 918bfa83a9eSThomas Graf static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value) 919bfa83a9eSThomas Graf { 920b4391db4SArnd Bergmann /* temporary variables to work around GCC PR81715 with asan-stack=1 */ 921b4391db4SArnd Bergmann u8 tmp = value; 922b4391db4SArnd Bergmann 923b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(u8), &tmp); 924bfa83a9eSThomas Graf } 925bfa83a9eSThomas Graf 926bfa83a9eSThomas Graf /** 927bfa83a9eSThomas Graf * nla_put_u16 - Add a u16 netlink attribute to a socket buffer 928bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 929bfa83a9eSThomas Graf * @attrtype: attribute type 930bfa83a9eSThomas Graf * @value: numeric value 931bfa83a9eSThomas Graf */ 932bfa83a9eSThomas Graf static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value) 933bfa83a9eSThomas Graf { 934b4391db4SArnd Bergmann u16 tmp = value; 935b4391db4SArnd Bergmann 936b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(u16), &tmp); 937bfa83a9eSThomas Graf } 938bfa83a9eSThomas Graf 939bfa83a9eSThomas Graf /** 940569a8fc3SDavid S. Miller * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer 941569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 942569a8fc3SDavid S. Miller * @attrtype: attribute type 943569a8fc3SDavid S. Miller * @value: numeric value 944569a8fc3SDavid S. Miller */ 945569a8fc3SDavid S. Miller static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value) 946569a8fc3SDavid S. Miller { 947b4391db4SArnd Bergmann __be16 tmp = value; 948b4391db4SArnd Bergmann 949b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__be16), &tmp); 950569a8fc3SDavid S. Miller } 951569a8fc3SDavid S. Miller 952569a8fc3SDavid S. Miller /** 9536c1dd3b6SDavid S. Miller * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer 9546c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 9556c1dd3b6SDavid S. Miller * @attrtype: attribute type 9566c1dd3b6SDavid S. Miller * @value: numeric value 9576c1dd3b6SDavid S. Miller */ 9586c1dd3b6SDavid S. Miller static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value) 9596c1dd3b6SDavid S. Miller { 960b4391db4SArnd Bergmann __be16 tmp = value; 961b4391db4SArnd Bergmann 962b4391db4SArnd Bergmann return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp); 9636c1dd3b6SDavid S. Miller } 9646c1dd3b6SDavid S. Miller 9656c1dd3b6SDavid S. Miller /** 96624c410dcSDavid S. Miller * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer 96724c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 96824c410dcSDavid S. Miller * @attrtype: attribute type 96924c410dcSDavid S. Miller * @value: numeric value 97024c410dcSDavid S. Miller */ 97124c410dcSDavid S. Miller static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value) 97224c410dcSDavid S. Miller { 973b4391db4SArnd Bergmann __le16 tmp = value; 974b4391db4SArnd Bergmann 975b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__le16), &tmp); 97624c410dcSDavid S. Miller } 97724c410dcSDavid S. Miller 97824c410dcSDavid S. Miller /** 979bfa83a9eSThomas Graf * nla_put_u32 - Add a u32 netlink attribute to a socket buffer 980bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 981bfa83a9eSThomas Graf * @attrtype: attribute type 982bfa83a9eSThomas Graf * @value: numeric value 983bfa83a9eSThomas Graf */ 984bfa83a9eSThomas Graf static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value) 985bfa83a9eSThomas Graf { 986b4391db4SArnd Bergmann u32 tmp = value; 987b4391db4SArnd Bergmann 988b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(u32), &tmp); 989bfa83a9eSThomas Graf } 990bfa83a9eSThomas Graf 991bfa83a9eSThomas Graf /** 992569a8fc3SDavid S. Miller * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer 993569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 994569a8fc3SDavid S. Miller * @attrtype: attribute type 995569a8fc3SDavid S. Miller * @value: numeric value 996569a8fc3SDavid S. Miller */ 997569a8fc3SDavid S. Miller static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value) 998569a8fc3SDavid S. Miller { 999b4391db4SArnd Bergmann __be32 tmp = value; 1000b4391db4SArnd Bergmann 1001b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__be32), &tmp); 1002569a8fc3SDavid S. Miller } 1003569a8fc3SDavid S. Miller 1004569a8fc3SDavid S. Miller /** 10056c1dd3b6SDavid S. Miller * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer 10066c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 10076c1dd3b6SDavid S. Miller * @attrtype: attribute type 10086c1dd3b6SDavid S. Miller * @value: numeric value 10096c1dd3b6SDavid S. Miller */ 10106c1dd3b6SDavid S. Miller static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value) 10116c1dd3b6SDavid S. Miller { 1012b4391db4SArnd Bergmann __be32 tmp = value; 1013b4391db4SArnd Bergmann 1014b4391db4SArnd Bergmann return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp); 10156c1dd3b6SDavid S. Miller } 10166c1dd3b6SDavid S. Miller 10176c1dd3b6SDavid S. Miller /** 101824c410dcSDavid S. Miller * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer 101924c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 102024c410dcSDavid S. Miller * @attrtype: attribute type 102124c410dcSDavid S. Miller * @value: numeric value 102224c410dcSDavid S. Miller */ 102324c410dcSDavid S. Miller static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value) 102424c410dcSDavid S. Miller { 1025b4391db4SArnd Bergmann __le32 tmp = value; 1026b4391db4SArnd Bergmann 1027b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__le32), &tmp); 102824c410dcSDavid S. Miller } 102924c410dcSDavid S. Miller 103024c410dcSDavid S. Miller /** 103173520786SNicolas Dichtel * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it 103273520786SNicolas Dichtel * @skb: socket buffer to add attribute to 103373520786SNicolas Dichtel * @attrtype: attribute type 103473520786SNicolas Dichtel * @value: numeric value 103573520786SNicolas Dichtel * @padattr: attribute type for the padding 103673520786SNicolas Dichtel */ 103773520786SNicolas Dichtel static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype, 103873520786SNicolas Dichtel u64 value, int padattr) 103973520786SNicolas Dichtel { 1040b4391db4SArnd Bergmann u64 tmp = value; 1041b4391db4SArnd Bergmann 1042b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr); 104373520786SNicolas Dichtel } 104473520786SNicolas Dichtel 104573520786SNicolas Dichtel /** 1046b46f6dedSNicolas Dichtel * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it 1047569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 1048569a8fc3SDavid S. Miller * @attrtype: attribute type 1049569a8fc3SDavid S. Miller * @value: numeric value 1050b46f6dedSNicolas Dichtel * @padattr: attribute type for the padding 1051569a8fc3SDavid S. Miller */ 1052b46f6dedSNicolas Dichtel static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value, 1053b46f6dedSNicolas Dichtel int padattr) 1054b46f6dedSNicolas Dichtel { 1055b4391db4SArnd Bergmann __be64 tmp = value; 1056b4391db4SArnd Bergmann 1057b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr); 1058b46f6dedSNicolas Dichtel } 1059b46f6dedSNicolas Dichtel 1060569a8fc3SDavid S. Miller /** 1061e9bbe898SNicolas Dichtel * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it 10626c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 10636c1dd3b6SDavid S. Miller * @attrtype: attribute type 10646c1dd3b6SDavid S. Miller * @value: numeric value 1065e9bbe898SNicolas Dichtel * @padattr: attribute type for the padding 10666c1dd3b6SDavid S. Miller */ 1067e9bbe898SNicolas Dichtel static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value, 1068e9bbe898SNicolas Dichtel int padattr) 10696c1dd3b6SDavid S. Miller { 1070b4391db4SArnd Bergmann __be64 tmp = value; 1071b4391db4SArnd Bergmann 1072b4391db4SArnd Bergmann return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp, 1073e9bbe898SNicolas Dichtel padattr); 10746c1dd3b6SDavid S. Miller } 10756c1dd3b6SDavid S. Miller 10766c1dd3b6SDavid S. Miller /** 1077e7479122SNicolas Dichtel * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it 107824c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 107924c410dcSDavid S. Miller * @attrtype: attribute type 108024c410dcSDavid S. Miller * @value: numeric value 1081e7479122SNicolas Dichtel * @padattr: attribute type for the padding 108224c410dcSDavid S. Miller */ 1083e7479122SNicolas Dichtel static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value, 1084e7479122SNicolas Dichtel int padattr) 108524c410dcSDavid S. Miller { 1086b4391db4SArnd Bergmann __le64 tmp = value; 1087b4391db4SArnd Bergmann 1088b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr); 108924c410dcSDavid S. Miller } 109024c410dcSDavid S. Miller 109124c410dcSDavid S. Miller /** 10924778e0beSJiri Pirko * nla_put_s8 - Add a s8 netlink attribute to a socket buffer 10934778e0beSJiri Pirko * @skb: socket buffer to add attribute to 10944778e0beSJiri Pirko * @attrtype: attribute type 10954778e0beSJiri Pirko * @value: numeric value 10964778e0beSJiri Pirko */ 10974778e0beSJiri Pirko static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value) 10984778e0beSJiri Pirko { 1099b4391db4SArnd Bergmann s8 tmp = value; 1100b4391db4SArnd Bergmann 1101b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(s8), &tmp); 11024778e0beSJiri Pirko } 11034778e0beSJiri Pirko 11044778e0beSJiri Pirko /** 11054778e0beSJiri Pirko * nla_put_s16 - Add a s16 netlink attribute to a socket buffer 11064778e0beSJiri Pirko * @skb: socket buffer to add attribute to 11074778e0beSJiri Pirko * @attrtype: attribute type 11084778e0beSJiri Pirko * @value: numeric value 11094778e0beSJiri Pirko */ 11104778e0beSJiri Pirko static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value) 11114778e0beSJiri Pirko { 1112b4391db4SArnd Bergmann s16 tmp = value; 1113b4391db4SArnd Bergmann 1114b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(s16), &tmp); 11154778e0beSJiri Pirko } 11164778e0beSJiri Pirko 11174778e0beSJiri Pirko /** 11184778e0beSJiri Pirko * nla_put_s32 - Add a s32 netlink attribute to a socket buffer 11194778e0beSJiri Pirko * @skb: socket buffer to add attribute to 11204778e0beSJiri Pirko * @attrtype: attribute type 11214778e0beSJiri Pirko * @value: numeric value 11224778e0beSJiri Pirko */ 11234778e0beSJiri Pirko static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value) 11244778e0beSJiri Pirko { 1125b4391db4SArnd Bergmann s32 tmp = value; 1126b4391db4SArnd Bergmann 1127b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(s32), &tmp); 11284778e0beSJiri Pirko } 11294778e0beSJiri Pirko 11304778e0beSJiri Pirko /** 1131756a2f59SNicolas Dichtel * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it 11324778e0beSJiri Pirko * @skb: socket buffer to add attribute to 11334778e0beSJiri Pirko * @attrtype: attribute type 11344778e0beSJiri Pirko * @value: numeric value 1135756a2f59SNicolas Dichtel * @padattr: attribute type for the padding 11364778e0beSJiri Pirko */ 1137756a2f59SNicolas Dichtel static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value, 1138756a2f59SNicolas Dichtel int padattr) 11394778e0beSJiri Pirko { 1140b4391db4SArnd Bergmann s64 tmp = value; 1141b4391db4SArnd Bergmann 1142b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr); 11434778e0beSJiri Pirko } 11444778e0beSJiri Pirko 11454778e0beSJiri Pirko /** 1146bfa83a9eSThomas Graf * nla_put_string - Add a string netlink attribute to a socket buffer 1147bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 1148bfa83a9eSThomas Graf * @attrtype: attribute type 1149bfa83a9eSThomas Graf * @str: NUL terminated string 1150bfa83a9eSThomas Graf */ 1151bfa83a9eSThomas Graf static inline int nla_put_string(struct sk_buff *skb, int attrtype, 1152bfa83a9eSThomas Graf const char *str) 1153bfa83a9eSThomas Graf { 1154bfa83a9eSThomas Graf return nla_put(skb, attrtype, strlen(str) + 1, str); 1155bfa83a9eSThomas Graf } 1156bfa83a9eSThomas Graf 1157bfa83a9eSThomas Graf /** 1158bfa83a9eSThomas Graf * nla_put_flag - Add a flag netlink attribute to a socket buffer 1159bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 1160bfa83a9eSThomas Graf * @attrtype: attribute type 1161bfa83a9eSThomas Graf */ 1162bfa83a9eSThomas Graf static inline int nla_put_flag(struct sk_buff *skb, int attrtype) 1163bfa83a9eSThomas Graf { 1164bfa83a9eSThomas Graf return nla_put(skb, attrtype, 0, NULL); 1165bfa83a9eSThomas Graf } 1166bfa83a9eSThomas Graf 1167bfa83a9eSThomas Graf /** 11682175d87cSNicolas Dichtel * nla_put_msecs - Add a msecs netlink attribute to a skb and align it 1169bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 1170bfa83a9eSThomas Graf * @attrtype: attribute type 1171d87de1f3SMark Rustad * @njiffies: number of jiffies to convert to msecs 11722175d87cSNicolas Dichtel * @padattr: attribute type for the padding 1173bfa83a9eSThomas Graf */ 1174bfa83a9eSThomas Graf static inline int nla_put_msecs(struct sk_buff *skb, int attrtype, 11752175d87cSNicolas Dichtel unsigned long njiffies, int padattr) 1176bfa83a9eSThomas Graf { 1177d87de1f3SMark Rustad u64 tmp = jiffies_to_msecs(njiffies); 11782175d87cSNicolas Dichtel 11792175d87cSNicolas Dichtel return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr); 1180bfa83a9eSThomas Graf } 1181bfa83a9eSThomas Graf 1182bfa83a9eSThomas Graf /** 1183930345eaSJiri Benc * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket 1184930345eaSJiri Benc * buffer 1185930345eaSJiri Benc * @skb: socket buffer to add attribute to 1186930345eaSJiri Benc * @attrtype: attribute type 1187930345eaSJiri Benc * @addr: IPv4 address 1188930345eaSJiri Benc */ 1189930345eaSJiri Benc static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype, 1190930345eaSJiri Benc __be32 addr) 1191930345eaSJiri Benc { 1192b4391db4SArnd Bergmann __be32 tmp = addr; 1193b4391db4SArnd Bergmann 1194b4391db4SArnd Bergmann return nla_put_be32(skb, attrtype, tmp); 1195930345eaSJiri Benc } 1196930345eaSJiri Benc 1197930345eaSJiri Benc /** 1198930345eaSJiri Benc * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket 1199930345eaSJiri Benc * buffer 1200930345eaSJiri Benc * @skb: socket buffer to add attribute to 1201930345eaSJiri Benc * @attrtype: attribute type 1202930345eaSJiri Benc * @addr: IPv6 address 1203930345eaSJiri Benc */ 1204930345eaSJiri Benc static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype, 1205930345eaSJiri Benc const struct in6_addr *addr) 1206930345eaSJiri Benc { 1207930345eaSJiri Benc return nla_put(skb, attrtype, sizeof(*addr), addr); 1208930345eaSJiri Benc } 1209930345eaSJiri Benc 1210930345eaSJiri Benc /** 1211bfa83a9eSThomas Graf * nla_get_u32 - return payload of u32 attribute 1212bfa83a9eSThomas Graf * @nla: u32 netlink attribute 1213bfa83a9eSThomas Graf */ 1214b057efd4SPatrick McHardy static inline u32 nla_get_u32(const struct nlattr *nla) 1215bfa83a9eSThomas Graf { 1216bfa83a9eSThomas Graf return *(u32 *) nla_data(nla); 1217bfa83a9eSThomas Graf } 1218bfa83a9eSThomas Graf 1219bfa83a9eSThomas Graf /** 122000012e5bSAl Viro * nla_get_be32 - return payload of __be32 attribute 122100012e5bSAl Viro * @nla: __be32 netlink attribute 122200012e5bSAl Viro */ 1223b057efd4SPatrick McHardy static inline __be32 nla_get_be32(const struct nlattr *nla) 122400012e5bSAl Viro { 122500012e5bSAl Viro return *(__be32 *) nla_data(nla); 122600012e5bSAl Viro } 122700012e5bSAl Viro 122800012e5bSAl Viro /** 1229c648a013SAlexander Aring * nla_get_le32 - return payload of __le32 attribute 1230c648a013SAlexander Aring * @nla: __le32 netlink attribute 1231c648a013SAlexander Aring */ 1232c648a013SAlexander Aring static inline __le32 nla_get_le32(const struct nlattr *nla) 1233c648a013SAlexander Aring { 1234c648a013SAlexander Aring return *(__le32 *) nla_data(nla); 1235c648a013SAlexander Aring } 1236c648a013SAlexander Aring 1237c648a013SAlexander Aring /** 1238bfa83a9eSThomas Graf * nla_get_u16 - return payload of u16 attribute 1239bfa83a9eSThomas Graf * @nla: u16 netlink attribute 1240bfa83a9eSThomas Graf */ 1241b057efd4SPatrick McHardy static inline u16 nla_get_u16(const struct nlattr *nla) 1242bfa83a9eSThomas Graf { 1243bfa83a9eSThomas Graf return *(u16 *) nla_data(nla); 1244bfa83a9eSThomas Graf } 1245bfa83a9eSThomas Graf 1246bfa83a9eSThomas Graf /** 1247838965baSPatrick McHardy * nla_get_be16 - return payload of __be16 attribute 1248838965baSPatrick McHardy * @nla: __be16 netlink attribute 1249838965baSPatrick McHardy */ 1250b057efd4SPatrick McHardy static inline __be16 nla_get_be16(const struct nlattr *nla) 1251838965baSPatrick McHardy { 1252838965baSPatrick McHardy return *(__be16 *) nla_data(nla); 1253838965baSPatrick McHardy } 1254838965baSPatrick McHardy 1255838965baSPatrick McHardy /** 12564a89c256SThomas Graf * nla_get_le16 - return payload of __le16 attribute 12574a89c256SThomas Graf * @nla: __le16 netlink attribute 12584a89c256SThomas Graf */ 1259b057efd4SPatrick McHardy static inline __le16 nla_get_le16(const struct nlattr *nla) 12604a89c256SThomas Graf { 12614a89c256SThomas Graf return *(__le16 *) nla_data(nla); 12624a89c256SThomas Graf } 12634a89c256SThomas Graf 12644a89c256SThomas Graf /** 1265bfa83a9eSThomas Graf * nla_get_u8 - return payload of u8 attribute 1266bfa83a9eSThomas Graf * @nla: u8 netlink attribute 1267bfa83a9eSThomas Graf */ 1268b057efd4SPatrick McHardy static inline u8 nla_get_u8(const struct nlattr *nla) 1269bfa83a9eSThomas Graf { 1270bfa83a9eSThomas Graf return *(u8 *) nla_data(nla); 1271bfa83a9eSThomas Graf } 1272bfa83a9eSThomas Graf 1273bfa83a9eSThomas Graf /** 1274bfa83a9eSThomas Graf * nla_get_u64 - return payload of u64 attribute 1275bfa83a9eSThomas Graf * @nla: u64 netlink attribute 1276bfa83a9eSThomas Graf */ 1277b057efd4SPatrick McHardy static inline u64 nla_get_u64(const struct nlattr *nla) 1278bfa83a9eSThomas Graf { 1279bfa83a9eSThomas Graf u64 tmp; 1280bfa83a9eSThomas Graf 1281bfa83a9eSThomas Graf nla_memcpy(&tmp, nla, sizeof(tmp)); 1282bfa83a9eSThomas Graf 1283bfa83a9eSThomas Graf return tmp; 1284bfa83a9eSThomas Graf } 1285bfa83a9eSThomas Graf 1286bfa83a9eSThomas Graf /** 1287a17c8598SPablo Neira Ayuso * nla_get_be64 - return payload of __be64 attribute 1288a17c8598SPablo Neira Ayuso * @nla: __be64 netlink attribute 1289a17c8598SPablo Neira Ayuso */ 1290a17c8598SPablo Neira Ayuso static inline __be64 nla_get_be64(const struct nlattr *nla) 1291a17c8598SPablo Neira Ayuso { 1292f5d410f2SPablo Neira Ayuso __be64 tmp; 1293f5d410f2SPablo Neira Ayuso 1294f5d410f2SPablo Neira Ayuso nla_memcpy(&tmp, nla, sizeof(tmp)); 1295f5d410f2SPablo Neira Ayuso 1296f5d410f2SPablo Neira Ayuso return tmp; 1297a17c8598SPablo Neira Ayuso } 1298a17c8598SPablo Neira Ayuso 1299a17c8598SPablo Neira Ayuso /** 1300c648a013SAlexander Aring * nla_get_le64 - return payload of __le64 attribute 1301c648a013SAlexander Aring * @nla: __le64 netlink attribute 1302c648a013SAlexander Aring */ 1303c648a013SAlexander Aring static inline __le64 nla_get_le64(const struct nlattr *nla) 1304c648a013SAlexander Aring { 1305c648a013SAlexander Aring return *(__le64 *) nla_data(nla); 1306c648a013SAlexander Aring } 1307c648a013SAlexander Aring 1308c648a013SAlexander Aring /** 13094778e0beSJiri Pirko * nla_get_s32 - return payload of s32 attribute 13104778e0beSJiri Pirko * @nla: s32 netlink attribute 13114778e0beSJiri Pirko */ 13124778e0beSJiri Pirko static inline s32 nla_get_s32(const struct nlattr *nla) 13134778e0beSJiri Pirko { 13144778e0beSJiri Pirko return *(s32 *) nla_data(nla); 13154778e0beSJiri Pirko } 13164778e0beSJiri Pirko 13174778e0beSJiri Pirko /** 13184778e0beSJiri Pirko * nla_get_s16 - return payload of s16 attribute 13194778e0beSJiri Pirko * @nla: s16 netlink attribute 13204778e0beSJiri Pirko */ 13214778e0beSJiri Pirko static inline s16 nla_get_s16(const struct nlattr *nla) 13224778e0beSJiri Pirko { 13234778e0beSJiri Pirko return *(s16 *) nla_data(nla); 13244778e0beSJiri Pirko } 13254778e0beSJiri Pirko 13264778e0beSJiri Pirko /** 13274778e0beSJiri Pirko * nla_get_s8 - return payload of s8 attribute 13284778e0beSJiri Pirko * @nla: s8 netlink attribute 13294778e0beSJiri Pirko */ 13304778e0beSJiri Pirko static inline s8 nla_get_s8(const struct nlattr *nla) 13314778e0beSJiri Pirko { 13324778e0beSJiri Pirko return *(s8 *) nla_data(nla); 13334778e0beSJiri Pirko } 13344778e0beSJiri Pirko 13354778e0beSJiri Pirko /** 13364778e0beSJiri Pirko * nla_get_s64 - return payload of s64 attribute 13374778e0beSJiri Pirko * @nla: s64 netlink attribute 13384778e0beSJiri Pirko */ 13394778e0beSJiri Pirko static inline s64 nla_get_s64(const struct nlattr *nla) 13404778e0beSJiri Pirko { 13414778e0beSJiri Pirko s64 tmp; 13424778e0beSJiri Pirko 13434778e0beSJiri Pirko nla_memcpy(&tmp, nla, sizeof(tmp)); 13444778e0beSJiri Pirko 13454778e0beSJiri Pirko return tmp; 13464778e0beSJiri Pirko } 13474778e0beSJiri Pirko 13484778e0beSJiri Pirko /** 1349bfa83a9eSThomas Graf * nla_get_flag - return payload of flag attribute 1350bfa83a9eSThomas Graf * @nla: flag netlink attribute 1351bfa83a9eSThomas Graf */ 1352b057efd4SPatrick McHardy static inline int nla_get_flag(const struct nlattr *nla) 1353bfa83a9eSThomas Graf { 1354bfa83a9eSThomas Graf return !!nla; 1355bfa83a9eSThomas Graf } 1356bfa83a9eSThomas Graf 1357bfa83a9eSThomas Graf /** 1358bfa83a9eSThomas Graf * nla_get_msecs - return payload of msecs attribute 1359bfa83a9eSThomas Graf * @nla: msecs netlink attribute 1360bfa83a9eSThomas Graf * 1361bfa83a9eSThomas Graf * Returns the number of milliseconds in jiffies. 1362bfa83a9eSThomas Graf */ 1363b057efd4SPatrick McHardy static inline unsigned long nla_get_msecs(const struct nlattr *nla) 1364bfa83a9eSThomas Graf { 1365bfa83a9eSThomas Graf u64 msecs = nla_get_u64(nla); 1366bfa83a9eSThomas Graf 1367bfa83a9eSThomas Graf return msecs_to_jiffies((unsigned long) msecs); 1368bfa83a9eSThomas Graf } 1369bfa83a9eSThomas Graf 1370bfa83a9eSThomas Graf /** 137167b61f6cSJiri Benc * nla_get_in_addr - return payload of IPv4 address attribute 137267b61f6cSJiri Benc * @nla: IPv4 address netlink attribute 137367b61f6cSJiri Benc */ 137467b61f6cSJiri Benc static inline __be32 nla_get_in_addr(const struct nlattr *nla) 137567b61f6cSJiri Benc { 137667b61f6cSJiri Benc return *(__be32 *) nla_data(nla); 137767b61f6cSJiri Benc } 137867b61f6cSJiri Benc 137967b61f6cSJiri Benc /** 138067b61f6cSJiri Benc * nla_get_in6_addr - return payload of IPv6 address attribute 138167b61f6cSJiri Benc * @nla: IPv6 address netlink attribute 138267b61f6cSJiri Benc */ 138367b61f6cSJiri Benc static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla) 138467b61f6cSJiri Benc { 138567b61f6cSJiri Benc struct in6_addr tmp; 138667b61f6cSJiri Benc 138767b61f6cSJiri Benc nla_memcpy(&tmp, nla, sizeof(tmp)); 138867b61f6cSJiri Benc return tmp; 138967b61f6cSJiri Benc } 139067b61f6cSJiri Benc 139167b61f6cSJiri Benc /** 139264c83d83SJamal Hadi Salim * nla_get_bitfield32 - return payload of 32 bitfield attribute 139364c83d83SJamal Hadi Salim * @nla: nla_bitfield32 attribute 139464c83d83SJamal Hadi Salim */ 139564c83d83SJamal Hadi Salim static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla) 139664c83d83SJamal Hadi Salim { 139764c83d83SJamal Hadi Salim struct nla_bitfield32 tmp; 139864c83d83SJamal Hadi Salim 139964c83d83SJamal Hadi Salim nla_memcpy(&tmp, nla, sizeof(tmp)); 140064c83d83SJamal Hadi Salim return tmp; 140164c83d83SJamal Hadi Salim } 140264c83d83SJamal Hadi Salim 140364c83d83SJamal Hadi Salim /** 1404b15ca182SThomas Graf * nla_memdup - duplicate attribute memory (kmemdup) 1405b15ca182SThomas Graf * @src: netlink attribute to duplicate from 1406b15ca182SThomas Graf * @gfp: GFP mask 1407b15ca182SThomas Graf */ 1408b15ca182SThomas Graf static inline void *nla_memdup(const struct nlattr *src, gfp_t gfp) 1409b15ca182SThomas Graf { 1410b15ca182SThomas Graf return kmemdup(nla_data(src), nla_len(src), gfp); 1411b15ca182SThomas Graf } 1412b15ca182SThomas Graf 1413b15ca182SThomas Graf /** 1414bfa83a9eSThomas Graf * nla_nest_start - Start a new level of nested attributes 1415bfa83a9eSThomas Graf * @skb: socket buffer to add attributes to 1416bfa83a9eSThomas Graf * @attrtype: attribute type of container 1417bfa83a9eSThomas Graf * 1418bfa83a9eSThomas Graf * Returns the container attribute 1419bfa83a9eSThomas Graf */ 1420bfa83a9eSThomas Graf static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype) 1421bfa83a9eSThomas Graf { 142227a884dcSArnaldo Carvalho de Melo struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb); 1423bfa83a9eSThomas Graf 1424bfa83a9eSThomas Graf if (nla_put(skb, attrtype, 0, NULL) < 0) 1425bfa83a9eSThomas Graf return NULL; 1426bfa83a9eSThomas Graf 1427bfa83a9eSThomas Graf return start; 1428bfa83a9eSThomas Graf } 1429bfa83a9eSThomas Graf 1430bfa83a9eSThomas Graf /** 1431bfa83a9eSThomas Graf * nla_nest_end - Finalize nesting of attributes 1432d1ec3b77SPierre Ynard * @skb: socket buffer the attributes are stored in 1433bfa83a9eSThomas Graf * @start: container attribute 1434bfa83a9eSThomas Graf * 1435bfa83a9eSThomas Graf * Corrects the container attribute header to include the all 1436bfa83a9eSThomas Graf * appeneded attributes. 1437bfa83a9eSThomas Graf * 1438bfa83a9eSThomas Graf * Returns the total data length of the skb. 1439bfa83a9eSThomas Graf */ 1440bfa83a9eSThomas Graf static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start) 1441bfa83a9eSThomas Graf { 144227a884dcSArnaldo Carvalho de Melo start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start; 1443bfa83a9eSThomas Graf return skb->len; 1444bfa83a9eSThomas Graf } 1445bfa83a9eSThomas Graf 1446bfa83a9eSThomas Graf /** 1447bfa83a9eSThomas Graf * nla_nest_cancel - Cancel nesting of attributes 1448bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 1449bfa83a9eSThomas Graf * @start: container attribute 1450bfa83a9eSThomas Graf * 1451bfa83a9eSThomas Graf * Removes the container attribute and including all nested 1452bc3ed28cSThomas Graf * attributes. Returns -EMSGSIZE 1453bfa83a9eSThomas Graf */ 1454bc3ed28cSThomas Graf static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start) 1455bfa83a9eSThomas Graf { 1456bc3ed28cSThomas Graf nlmsg_trim(skb, start); 1457bfa83a9eSThomas Graf } 1458bfa83a9eSThomas Graf 1459bfa83a9eSThomas Graf /** 14604fe5d5c0SPaul Moore * nla_validate_nested - Validate a stream of nested attributes 14614fe5d5c0SPaul Moore * @start: container attribute 14624fe5d5c0SPaul Moore * @maxtype: maximum attribute type to be expected 14634fe5d5c0SPaul Moore * @policy: validation policy 1464fceb6435SJohannes Berg * @extack: extended ACK report struct 14654fe5d5c0SPaul Moore * 14664fe5d5c0SPaul Moore * Validates all attributes in the nested attribute stream against the 14674fe5d5c0SPaul Moore * specified policy. Attributes with a type exceeding maxtype will be 14684fe5d5c0SPaul Moore * ignored. See documenation of struct nla_policy for more details. 14694fe5d5c0SPaul Moore * 14704fe5d5c0SPaul Moore * Returns 0 on success or a negative error code. 14714fe5d5c0SPaul Moore */ 14723654654fSJan Engelhardt static inline int nla_validate_nested(const struct nlattr *start, int maxtype, 1473fceb6435SJohannes Berg const struct nla_policy *policy, 1474fceb6435SJohannes Berg struct netlink_ext_ack *extack) 14754fe5d5c0SPaul Moore { 1476fceb6435SJohannes Berg return nla_validate(nla_data(start), nla_len(start), maxtype, policy, 1477fceb6435SJohannes Berg extack); 14784fe5d5c0SPaul Moore } 14794fe5d5c0SPaul Moore 14804fe5d5c0SPaul Moore /** 1481089bf1a6SNicolas Dichtel * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute 1482089bf1a6SNicolas Dichtel * @skb: socket buffer the message is stored in 1483089bf1a6SNicolas Dichtel * 1484089bf1a6SNicolas Dichtel * Return true if padding is needed to align the next attribute (nla_data()) to 1485089bf1a6SNicolas Dichtel * a 64-bit aligned area. 1486089bf1a6SNicolas Dichtel */ 1487089bf1a6SNicolas Dichtel static inline bool nla_need_padding_for_64bit(struct sk_buff *skb) 1488089bf1a6SNicolas Dichtel { 1489089bf1a6SNicolas Dichtel #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1490089bf1a6SNicolas Dichtel /* The nlattr header is 4 bytes in size, that's why we test 1491089bf1a6SNicolas Dichtel * if the skb->data _is_ aligned. A NOP attribute, plus 1492089bf1a6SNicolas Dichtel * nlattr header for next attribute, will make nla_data() 1493089bf1a6SNicolas Dichtel * 8-byte aligned. 1494089bf1a6SNicolas Dichtel */ 1495089bf1a6SNicolas Dichtel if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8)) 1496089bf1a6SNicolas Dichtel return true; 1497089bf1a6SNicolas Dichtel #endif 1498089bf1a6SNicolas Dichtel return false; 1499089bf1a6SNicolas Dichtel } 1500089bf1a6SNicolas Dichtel 1501089bf1a6SNicolas Dichtel /** 150235c58459SDavid S. Miller * nla_align_64bit - 64-bit align the nla_data() of next attribute 150335c58459SDavid S. Miller * @skb: socket buffer the message is stored in 150435c58459SDavid S. Miller * @padattr: attribute type for the padding 150535c58459SDavid S. Miller * 150635c58459SDavid S. Miller * Conditionally emit a padding netlink attribute in order to make 150735c58459SDavid S. Miller * the next attribute we emit have a 64-bit aligned nla_data() area. 150835c58459SDavid S. Miller * This will only be done in architectures which do not have 1509cca1d815SEric Dumazet * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined. 151035c58459SDavid S. Miller * 151135c58459SDavid S. Miller * Returns zero on success or a negative error code. 151235c58459SDavid S. Miller */ 151335c58459SDavid S. Miller static inline int nla_align_64bit(struct sk_buff *skb, int padattr) 151435c58459SDavid S. Miller { 1515089bf1a6SNicolas Dichtel if (nla_need_padding_for_64bit(skb) && 1516cca1d815SEric Dumazet !nla_reserve(skb, padattr, 0)) 151735c58459SDavid S. Miller return -EMSGSIZE; 1518089bf1a6SNicolas Dichtel 151935c58459SDavid S. Miller return 0; 152035c58459SDavid S. Miller } 152135c58459SDavid S. Miller 152235c58459SDavid S. Miller /** 152335c58459SDavid S. Miller * nla_total_size_64bit - total length of attribute including padding 152435c58459SDavid S. Miller * @payload: length of payload 152535c58459SDavid S. Miller */ 152635c58459SDavid S. Miller static inline int nla_total_size_64bit(int payload) 152735c58459SDavid S. Miller { 152835c58459SDavid S. Miller return NLA_ALIGN(nla_attr_size(payload)) 1529cca1d815SEric Dumazet #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 153035c58459SDavid S. Miller + NLA_ALIGN(nla_attr_size(0)) 153135c58459SDavid S. Miller #endif 153235c58459SDavid S. Miller ; 153335c58459SDavid S. Miller } 153435c58459SDavid S. Miller 153535c58459SDavid S. Miller /** 1536bfa83a9eSThomas Graf * nla_for_each_attr - iterate over a stream of attributes 1537bfa83a9eSThomas Graf * @pos: loop counter, set to current attribute 1538bfa83a9eSThomas Graf * @head: head of attribute stream 1539bfa83a9eSThomas Graf * @len: length of attribute stream 1540bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 1541bfa83a9eSThomas Graf */ 1542bfa83a9eSThomas Graf #define nla_for_each_attr(pos, head, len, rem) \ 1543bfa83a9eSThomas Graf for (pos = head, rem = len; \ 1544bfa83a9eSThomas Graf nla_ok(pos, rem); \ 1545bfa83a9eSThomas Graf pos = nla_next(pos, &(rem))) 1546bfa83a9eSThomas Graf 1547fe4944e5SThomas Graf /** 1548fe4944e5SThomas Graf * nla_for_each_nested - iterate over nested attributes 1549fe4944e5SThomas Graf * @pos: loop counter, set to current attribute 1550fe4944e5SThomas Graf * @nla: attribute containing the nested attributes 1551fe4944e5SThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 1552fe4944e5SThomas Graf */ 1553fe4944e5SThomas Graf #define nla_for_each_nested(pos, nla, rem) \ 1554fe4944e5SThomas Graf nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem) 1555fe4944e5SThomas Graf 1556941d8ebcSSimon Horman /** 1557941d8ebcSSimon Horman * nla_is_last - Test if attribute is last in stream 1558941d8ebcSSimon Horman * @nla: attribute to test 1559941d8ebcSSimon Horman * @rem: bytes remaining in stream 1560941d8ebcSSimon Horman */ 1561941d8ebcSSimon Horman static inline bool nla_is_last(const struct nlattr *nla, int rem) 1562941d8ebcSSimon Horman { 1563941d8ebcSSimon Horman return nla->nla_len == rem; 1564941d8ebcSSimon Horman } 1565941d8ebcSSimon Horman 1566bfa83a9eSThomas Graf #endif 1567