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, 1866f455f5fSJohannes Berg NLA_MIN_LEN, 187bfa83a9eSThomas Graf __NLA_TYPE_MAX, 188bfa83a9eSThomas Graf }; 189bfa83a9eSThomas Graf 190bfa83a9eSThomas Graf #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1) 191bfa83a9eSThomas Graf 1923e48be05SJohannes Berg enum nla_policy_validation { 1933e48be05SJohannes Berg NLA_VALIDATE_NONE, 1943e48be05SJohannes Berg NLA_VALIDATE_RANGE, 1953e48be05SJohannes Berg NLA_VALIDATE_MIN, 1963e48be05SJohannes Berg NLA_VALIDATE_MAX, 19733188bd6SJohannes Berg NLA_VALIDATE_FUNCTION, 1983e48be05SJohannes Berg }; 1993e48be05SJohannes Berg 200bfa83a9eSThomas Graf /** 201bfa83a9eSThomas Graf * struct nla_policy - attribute validation policy 202bfa83a9eSThomas Graf * @type: Type of attribute or NLA_UNSPEC 2033e48be05SJohannes Berg * @validation_type: type of attribute validation done in addition to 20433188bd6SJohannes Berg * type-specific validation (e.g. range, function call), see 2053e48be05SJohannes Berg * &enum nla_policy_validation 206a5531a5dSThomas Graf * @len: Type specific length of payload 207bfa83a9eSThomas Graf * 208bfa83a9eSThomas Graf * Policies are defined as arrays of this struct, the array must be 209bfa83a9eSThomas Graf * accessible by attribute type up to the highest identifier to be expected. 210bfa83a9eSThomas Graf * 211a5531a5dSThomas Graf * Meaning of `len' field: 212a5531a5dSThomas Graf * NLA_STRING Maximum length of string 213a5531a5dSThomas Graf * NLA_NUL_STRING Maximum length of string (excluding NUL) 214a5531a5dSThomas Graf * NLA_FLAG Unused 215d30045a0SJohannes Berg * NLA_BINARY Maximum length of attribute payload 2166f455f5fSJohannes Berg * NLA_MIN_LEN Minimum length of attribute payload 2171501d135SJohannes Berg * NLA_NESTED, 2181501d135SJohannes Berg * NLA_NESTED_ARRAY Length verification is done by checking len of 2199a659a35SJohannes Berg * nested header (or empty); len field is used if 2209a659a35SJohannes Berg * validation_data is also used, for the max attr 2219a659a35SJohannes Berg * number in the nested policy. 2224b6cc728SJohannes Berg * NLA_U8, NLA_U16, 2234b6cc728SJohannes Berg * NLA_U32, NLA_U64, 2244778e0beSJiri Pirko * NLA_S8, NLA_S16, 2254778e0beSJiri Pirko * NLA_S32, NLA_S64, 2264b6cc728SJohannes Berg * NLA_MSECS Leaving the length field zero will verify the 2274b6cc728SJohannes Berg * given type fits, using it verifies minimum length 2284b6cc728SJohannes Berg * just like "All other" 229568b742aSJohannes Berg * NLA_BITFIELD32 Unused 230568b742aSJohannes Berg * NLA_REJECT Unused 231b60b87fcSJohannes Berg * NLA_EXACT_LEN Attribute must have exactly this length, otherwise 232b60b87fcSJohannes Berg * it is rejected. 233b60b87fcSJohannes Berg * NLA_EXACT_LEN_WARN Attribute should have exactly this length, a warning 234b60b87fcSJohannes Berg * is logged if it is longer, shorter is rejected. 2356f455f5fSJohannes Berg * NLA_MIN_LEN Minimum length of attribute payload 2364b6cc728SJohannes Berg * All other Minimum length of attribute payload 237a5531a5dSThomas Graf * 238568b742aSJohannes Berg * Meaning of `validation_data' field: 239568b742aSJohannes Berg * NLA_BITFIELD32 This is a 32-bit bitmap/bitselector attribute and 240568b742aSJohannes Berg * validation data must point to a u32 value of valid 241568b742aSJohannes Berg * flags 242568b742aSJohannes Berg * NLA_REJECT This attribute is always rejected and validation data 243568b742aSJohannes Berg * may point to a string to report as the error instead 244568b742aSJohannes Berg * of the generic one in extended ACK. 2459a659a35SJohannes Berg * NLA_NESTED Points to a nested policy to validate, must also set 2469a659a35SJohannes Berg * `len' to the max attribute number. 2479a659a35SJohannes Berg * Note that nla_parse() will validate, but of course not 2489a659a35SJohannes Berg * parse, the nested sub-policies. 2491501d135SJohannes Berg * NLA_NESTED_ARRAY Points to a nested policy to validate, must also set 2501501d135SJohannes Berg * `len' to the max attribute number. The difference to 2511501d135SJohannes Berg * NLA_NESTED is the structure - NLA_NESTED has the 2521501d135SJohannes Berg * nested attributes directly inside, while an array has 2531501d135SJohannes Berg * the nested attributes at another level down and the 2541501d135SJohannes Berg * attributes directly in the nesting don't matter. 2553e48be05SJohannes Berg * All other Unused - but note that it's a union 2563e48be05SJohannes Berg * 2573e48be05SJohannes Berg * Meaning of `min' and `max' fields, use via NLA_POLICY_MIN, NLA_POLICY_MAX 2583e48be05SJohannes Berg * and NLA_POLICY_RANGE: 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, 2663e48be05SJohannes Berg * NLA_S64 These are used depending on the validation_type 2673e48be05SJohannes Berg * field, if that is min/max/range then the minimum, 2683e48be05SJohannes Berg * maximum and 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. 2743e48be05SJohannes Berg * All other Unused - but note that it's a union 275568b742aSJohannes Berg * 27633188bd6SJohannes Berg * Meaning of `validate' field, use via NLA_POLICY_VALIDATE_FN: 27733188bd6SJohannes Berg * NLA_BINARY Validation function called for the attribute, 27833188bd6SJohannes Berg * not compatible with use of the validation_data 27933188bd6SJohannes Berg * as in NLA_BITFIELD32, NLA_REJECT, NLA_NESTED and 28033188bd6SJohannes Berg * NLA_NESTED_ARRAY. 28133188bd6SJohannes Berg * All other Unused - but note that it's a union 28233188bd6SJohannes Berg * 283bfa83a9eSThomas Graf * Example: 284b54452b0SAlexey Dobriyan * static const struct nla_policy my_policy[ATTR_MAX+1] = { 285bfa83a9eSThomas Graf * [ATTR_FOO] = { .type = NLA_U16 }, 286d30045a0SJohannes Berg * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ }, 2876f455f5fSJohannes Berg * [ATTR_BAZ] = { .type = NLA_EXACT_LEN, .len = sizeof(struct mystruct) }, 28864c83d83SJamal Hadi Salim * [ATTR_GOO] = { .type = NLA_BITFIELD32, .validation_data = &myvalidflags }, 289bfa83a9eSThomas Graf * }; 290bfa83a9eSThomas Graf */ 291bfa83a9eSThomas Graf struct nla_policy { 2923e48be05SJohannes Berg u8 type; 2933e48be05SJohannes Berg u8 validation_type; 294a5531a5dSThomas Graf u16 len; 2953e48be05SJohannes Berg union { 29648fde90aSJohannes Berg const void *validation_data; 2973e48be05SJohannes Berg struct { 2983e48be05SJohannes Berg s16 min, max; 2993e48be05SJohannes Berg }; 30033188bd6SJohannes Berg int (*validate)(const struct nlattr *attr, 30133188bd6SJohannes Berg struct netlink_ext_ack *extack); 3023e48be05SJohannes Berg }; 303bfa83a9eSThomas Graf }; 304bfa83a9eSThomas Graf 305b60b87fcSJohannes Berg #define NLA_POLICY_EXACT_LEN(_len) { .type = NLA_EXACT_LEN, .len = _len } 306b60b87fcSJohannes Berg #define NLA_POLICY_EXACT_LEN_WARN(_len) { .type = NLA_EXACT_LEN_WARN, \ 307b60b87fcSJohannes Berg .len = _len } 3086f455f5fSJohannes Berg #define NLA_POLICY_MIN_LEN(_len) { .type = NLA_MIN_LEN, .len = _len } 309b60b87fcSJohannes Berg 310b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR NLA_POLICY_EXACT_LEN(ETH_ALEN) 311b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR_COMPAT NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN) 312b60b87fcSJohannes Berg 31323323289SJohannes Berg #define _NLA_POLICY_NESTED(maxattr, policy) \ 3149a659a35SJohannes Berg { .type = NLA_NESTED, .validation_data = policy, .len = maxattr } 31523323289SJohannes Berg #define _NLA_POLICY_NESTED_ARRAY(maxattr, policy) \ 3161501d135SJohannes Berg { .type = NLA_NESTED_ARRAY, .validation_data = policy, .len = maxattr } 31723323289SJohannes Berg #define NLA_POLICY_NESTED(policy) \ 31823323289SJohannes Berg _NLA_POLICY_NESTED(ARRAY_SIZE(policy) - 1, policy) 31923323289SJohannes Berg #define NLA_POLICY_NESTED_ARRAY(policy) \ 32023323289SJohannes Berg _NLA_POLICY_NESTED_ARRAY(ARRAY_SIZE(policy) - 1, policy) 3219a659a35SJohannes Berg 3225886d932SJohannes Berg #define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition)) 3233e48be05SJohannes Berg #define NLA_ENSURE_INT_TYPE(tp) \ 3243e48be05SJohannes Berg (__NLA_ENSURE(tp == NLA_S8 || tp == NLA_U8 || \ 3253e48be05SJohannes Berg tp == NLA_S16 || tp == NLA_U16 || \ 3263e48be05SJohannes Berg tp == NLA_S32 || tp == NLA_U32 || \ 3273e48be05SJohannes Berg tp == NLA_S64 || tp == NLA_U64) + tp) 32833188bd6SJohannes Berg #define NLA_ENSURE_NO_VALIDATION_PTR(tp) \ 32933188bd6SJohannes Berg (__NLA_ENSURE(tp != NLA_BITFIELD32 && \ 33033188bd6SJohannes Berg tp != NLA_REJECT && \ 33133188bd6SJohannes Berg tp != NLA_NESTED && \ 33233188bd6SJohannes Berg tp != NLA_NESTED_ARRAY) + tp) 3333e48be05SJohannes Berg 3343e48be05SJohannes Berg #define NLA_POLICY_RANGE(tp, _min, _max) { \ 3353e48be05SJohannes Berg .type = NLA_ENSURE_INT_TYPE(tp), \ 3363e48be05SJohannes Berg .validation_type = NLA_VALIDATE_RANGE, \ 3373e48be05SJohannes Berg .min = _min, \ 3383e48be05SJohannes Berg .max = _max \ 3393e48be05SJohannes Berg } 3403e48be05SJohannes Berg 3413e48be05SJohannes Berg #define NLA_POLICY_MIN(tp, _min) { \ 3423e48be05SJohannes Berg .type = NLA_ENSURE_INT_TYPE(tp), \ 3433e48be05SJohannes Berg .validation_type = NLA_VALIDATE_MIN, \ 3443e48be05SJohannes Berg .min = _min, \ 3453e48be05SJohannes Berg } 3463e48be05SJohannes Berg 3473e48be05SJohannes Berg #define NLA_POLICY_MAX(tp, _max) { \ 3483e48be05SJohannes Berg .type = NLA_ENSURE_INT_TYPE(tp), \ 3493e48be05SJohannes Berg .validation_type = NLA_VALIDATE_MAX, \ 3503e48be05SJohannes Berg .max = _max, \ 3513e48be05SJohannes Berg } 3523e48be05SJohannes Berg 35333188bd6SJohannes Berg #define NLA_POLICY_VALIDATE_FN(tp, fn, ...) { \ 35433188bd6SJohannes Berg .type = NLA_ENSURE_NO_VALIDATION_PTR(tp), \ 35533188bd6SJohannes Berg .validation_type = NLA_VALIDATE_FUNCTION, \ 35633188bd6SJohannes Berg .validate = fn, \ 35733188bd6SJohannes Berg .len = __VA_ARGS__ + 0, \ 35833188bd6SJohannes Berg } 35933188bd6SJohannes Berg 3604e902c57SThomas Graf /** 3614e902c57SThomas Graf * struct nl_info - netlink source information 3624e902c57SThomas Graf * @nlh: Netlink message header of original request 36315e47304SEric W. Biederman * @portid: Netlink PORTID of requesting application 3644e902c57SThomas Graf */ 3654e902c57SThomas Graf struct nl_info { 3664e902c57SThomas Graf struct nlmsghdr *nlh; 3674d1169c1SDenis V. Lunev struct net *nl_net; 36815e47304SEric W. Biederman u32 portid; 3693b1137feSDavid Ahern bool skip_notify; 3704e902c57SThomas Graf }; 3714e902c57SThomas Graf 3724f69053bSJoe Perches int netlink_rcv_skb(struct sk_buff *skb, 3732d4bc933SJohannes Berg int (*cb)(struct sk_buff *, struct nlmsghdr *, 3742d4bc933SJohannes Berg struct netlink_ext_ack *)); 3754f69053bSJoe Perches int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid, 3764f69053bSJoe Perches unsigned int group, int report, gfp_t flags); 37782ace47aSThomas Graf 3784f69053bSJoe Perches int nla_validate(const struct nlattr *head, int len, int maxtype, 379fceb6435SJohannes Berg const struct nla_policy *policy, 380fceb6435SJohannes Berg struct netlink_ext_ack *extack); 3814f69053bSJoe Perches int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, 382fceb6435SJohannes Berg int len, const struct nla_policy *policy, 383fceb6435SJohannes Berg struct netlink_ext_ack *extack); 384a5f6cba2SDavid Ahern int nla_parse_strict(struct nlattr **tb, int maxtype, const struct nlattr *head, 385a5f6cba2SDavid Ahern int len, const struct nla_policy *policy, 386a5f6cba2SDavid Ahern struct netlink_ext_ack *extack); 3874f69053bSJoe Perches int nla_policy_len(const struct nla_policy *, int); 3884f69053bSJoe Perches struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype); 3894f69053bSJoe Perches size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize); 3902cf0c8b3SPhil Sutter char *nla_strdup(const struct nlattr *nla, gfp_t flags); 3914f69053bSJoe Perches int nla_memcpy(void *dest, const struct nlattr *src, int count); 3924f69053bSJoe Perches int nla_memcmp(const struct nlattr *nla, const void *data, size_t size); 3934f69053bSJoe Perches int nla_strcmp(const struct nlattr *nla, const char *str); 3944f69053bSJoe Perches struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 395089bf1a6SNicolas Dichtel struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype, 396089bf1a6SNicolas Dichtel int attrlen, int padattr); 3974f69053bSJoe Perches void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 3984f69053bSJoe Perches struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 399089bf1a6SNicolas Dichtel struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, 400089bf1a6SNicolas Dichtel int attrlen, int padattr); 4014f69053bSJoe Perches void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 4024f69053bSJoe Perches void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, 403fe4944e5SThomas Graf const void *data); 404089bf1a6SNicolas Dichtel void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 405089bf1a6SNicolas Dichtel const void *data, int padattr); 4064f69053bSJoe Perches void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 4074f69053bSJoe Perches int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data); 408089bf1a6SNicolas Dichtel int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 409089bf1a6SNicolas Dichtel const void *data, int padattr); 4104f69053bSJoe Perches int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 4114f69053bSJoe Perches int nla_append(struct sk_buff *skb, int attrlen, const void *data); 412bfa83a9eSThomas Graf 413bfa83a9eSThomas Graf /************************************************************************** 414bfa83a9eSThomas Graf * Netlink Messages 415bfa83a9eSThomas Graf **************************************************************************/ 416bfa83a9eSThomas Graf 417bfa83a9eSThomas Graf /** 418bfa83a9eSThomas Graf * nlmsg_msg_size - length of netlink message not including padding 419bfa83a9eSThomas Graf * @payload: length of message payload 420bfa83a9eSThomas Graf */ 421bfa83a9eSThomas Graf static inline int nlmsg_msg_size(int payload) 422bfa83a9eSThomas Graf { 423bfa83a9eSThomas Graf return NLMSG_HDRLEN + payload; 424bfa83a9eSThomas Graf } 425bfa83a9eSThomas Graf 426bfa83a9eSThomas Graf /** 427bfa83a9eSThomas Graf * nlmsg_total_size - length of netlink message including padding 428bfa83a9eSThomas Graf * @payload: length of message payload 429bfa83a9eSThomas Graf */ 430bfa83a9eSThomas Graf static inline int nlmsg_total_size(int payload) 431bfa83a9eSThomas Graf { 432bfa83a9eSThomas Graf return NLMSG_ALIGN(nlmsg_msg_size(payload)); 433bfa83a9eSThomas Graf } 434bfa83a9eSThomas Graf 435bfa83a9eSThomas Graf /** 436bfa83a9eSThomas Graf * nlmsg_padlen - length of padding at the message's tail 437bfa83a9eSThomas Graf * @payload: length of message payload 438bfa83a9eSThomas Graf */ 439bfa83a9eSThomas Graf static inline int nlmsg_padlen(int payload) 440bfa83a9eSThomas Graf { 441bfa83a9eSThomas Graf return nlmsg_total_size(payload) - nlmsg_msg_size(payload); 442bfa83a9eSThomas Graf } 443bfa83a9eSThomas Graf 444bfa83a9eSThomas Graf /** 445bfa83a9eSThomas Graf * nlmsg_data - head of message payload 44670f23fd6SJustin P. Mattock * @nlh: netlink message header 447bfa83a9eSThomas Graf */ 448bfa83a9eSThomas Graf static inline void *nlmsg_data(const struct nlmsghdr *nlh) 449bfa83a9eSThomas Graf { 450bfa83a9eSThomas Graf return (unsigned char *) nlh + NLMSG_HDRLEN; 451bfa83a9eSThomas Graf } 452bfa83a9eSThomas Graf 453bfa83a9eSThomas Graf /** 454bfa83a9eSThomas Graf * nlmsg_len - length of message payload 455bfa83a9eSThomas Graf * @nlh: netlink message header 456bfa83a9eSThomas Graf */ 457bfa83a9eSThomas Graf static inline int nlmsg_len(const struct nlmsghdr *nlh) 458bfa83a9eSThomas Graf { 459bfa83a9eSThomas Graf return nlh->nlmsg_len - NLMSG_HDRLEN; 460bfa83a9eSThomas Graf } 461bfa83a9eSThomas Graf 462bfa83a9eSThomas Graf /** 463bfa83a9eSThomas Graf * nlmsg_attrdata - head of attributes data 464bfa83a9eSThomas Graf * @nlh: netlink message header 465bfa83a9eSThomas Graf * @hdrlen: length of family specific header 466bfa83a9eSThomas Graf */ 467bfa83a9eSThomas Graf static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, 468bfa83a9eSThomas Graf int hdrlen) 469bfa83a9eSThomas Graf { 470bfa83a9eSThomas Graf unsigned char *data = nlmsg_data(nlh); 471bfa83a9eSThomas Graf return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen)); 472bfa83a9eSThomas Graf } 473bfa83a9eSThomas Graf 474bfa83a9eSThomas Graf /** 475bfa83a9eSThomas Graf * nlmsg_attrlen - length of attributes data 476bfa83a9eSThomas Graf * @nlh: netlink message header 477bfa83a9eSThomas Graf * @hdrlen: length of family specific header 478bfa83a9eSThomas Graf */ 479bfa83a9eSThomas Graf static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen) 480bfa83a9eSThomas Graf { 481bfa83a9eSThomas Graf return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen); 482bfa83a9eSThomas Graf } 483bfa83a9eSThomas Graf 484bfa83a9eSThomas Graf /** 485bfa83a9eSThomas Graf * nlmsg_ok - check if the netlink message fits into the remaining bytes 486bfa83a9eSThomas Graf * @nlh: netlink message header 487bfa83a9eSThomas Graf * @remaining: number of bytes remaining in message stream 488bfa83a9eSThomas Graf */ 489bfa83a9eSThomas Graf static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining) 490bfa83a9eSThomas Graf { 491619e803dSVegard Nossum return (remaining >= (int) sizeof(struct nlmsghdr) && 492bfa83a9eSThomas Graf nlh->nlmsg_len >= sizeof(struct nlmsghdr) && 493bfa83a9eSThomas Graf nlh->nlmsg_len <= remaining); 494bfa83a9eSThomas Graf } 495bfa83a9eSThomas Graf 496bfa83a9eSThomas Graf /** 497bfa83a9eSThomas Graf * nlmsg_next - next netlink message in message stream 498bfa83a9eSThomas Graf * @nlh: netlink message header 499bfa83a9eSThomas Graf * @remaining: number of bytes remaining in message stream 500bfa83a9eSThomas Graf * 501bfa83a9eSThomas Graf * Returns the next netlink message in the message stream and 502bfa83a9eSThomas Graf * decrements remaining by the size of the current message. 503bfa83a9eSThomas Graf */ 5043654654fSJan Engelhardt static inline struct nlmsghdr * 5053654654fSJan Engelhardt nlmsg_next(const struct nlmsghdr *nlh, int *remaining) 506bfa83a9eSThomas Graf { 507bfa83a9eSThomas Graf int totlen = NLMSG_ALIGN(nlh->nlmsg_len); 508bfa83a9eSThomas Graf 509bfa83a9eSThomas Graf *remaining -= totlen; 510bfa83a9eSThomas Graf 511bfa83a9eSThomas Graf return (struct nlmsghdr *) ((unsigned char *) nlh + totlen); 512bfa83a9eSThomas Graf } 513bfa83a9eSThomas Graf 514bfa83a9eSThomas Graf /** 515bfa83a9eSThomas Graf * nlmsg_parse - parse attributes of a netlink message 516bfa83a9eSThomas Graf * @nlh: netlink message header 517bfa83a9eSThomas Graf * @hdrlen: length of family specific header 518bfa83a9eSThomas Graf * @tb: destination array with maxtype+1 elements 519bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 520bfa83a9eSThomas Graf * @policy: validation policy 521fceb6435SJohannes Berg * @extack: extended ACK report struct 522bfa83a9eSThomas Graf * 523bfa83a9eSThomas Graf * See nla_parse() 524bfa83a9eSThomas Graf */ 5253a6c2b41SPatrick McHardy static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen, 526bfa83a9eSThomas Graf struct nlattr *tb[], int maxtype, 527fceb6435SJohannes Berg const struct nla_policy *policy, 528fceb6435SJohannes Berg struct netlink_ext_ack *extack) 529bfa83a9eSThomas Graf { 5303d0d4337SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) { 5313d0d4337SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header length"); 532bfa83a9eSThomas Graf return -EINVAL; 5333d0d4337SDavid Ahern } 534bfa83a9eSThomas Graf 535bfa83a9eSThomas Graf return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), 536fceb6435SJohannes Berg nlmsg_attrlen(nlh, hdrlen), policy, extack); 537bfa83a9eSThomas Graf } 538bfa83a9eSThomas Graf 539a5f6cba2SDavid Ahern static inline int nlmsg_parse_strict(const struct nlmsghdr *nlh, int hdrlen, 540a5f6cba2SDavid Ahern struct nlattr *tb[], int maxtype, 541a5f6cba2SDavid Ahern const struct nla_policy *policy, 542a5f6cba2SDavid Ahern struct netlink_ext_ack *extack) 543a5f6cba2SDavid Ahern { 544a5f6cba2SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) { 545a5f6cba2SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header length"); 546a5f6cba2SDavid Ahern return -EINVAL; 547a5f6cba2SDavid Ahern } 548a5f6cba2SDavid Ahern 549a5f6cba2SDavid Ahern return nla_parse_strict(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), 550a5f6cba2SDavid Ahern nlmsg_attrlen(nlh, hdrlen), policy, extack); 551a5f6cba2SDavid Ahern } 552a5f6cba2SDavid Ahern 553bfa83a9eSThomas Graf /** 554bfa83a9eSThomas Graf * nlmsg_find_attr - find a specific attribute in a netlink message 555bfa83a9eSThomas Graf * @nlh: netlink message header 556bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 557bfa83a9eSThomas Graf * @attrtype: type of attribute to look for 558bfa83a9eSThomas Graf * 559bfa83a9eSThomas Graf * Returns the first attribute which matches the specified type. 560bfa83a9eSThomas Graf */ 5616b8c92baSNelson Elhage static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh, 562bfa83a9eSThomas Graf int hdrlen, int attrtype) 563bfa83a9eSThomas Graf { 564bfa83a9eSThomas Graf return nla_find(nlmsg_attrdata(nlh, hdrlen), 565bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), attrtype); 566bfa83a9eSThomas Graf } 567bfa83a9eSThomas Graf 568bfa83a9eSThomas Graf /** 569bfa83a9eSThomas Graf * nlmsg_validate - validate a netlink message including attributes 570bfa83a9eSThomas Graf * @nlh: netlinket message header 571bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 572bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 573bfa83a9eSThomas Graf * @policy: validation policy 574fceb6435SJohannes Berg * @extack: extended ACK report struct 575bfa83a9eSThomas Graf */ 5763654654fSJan Engelhardt static inline int nlmsg_validate(const struct nlmsghdr *nlh, 5773654654fSJan Engelhardt int hdrlen, int maxtype, 578fceb6435SJohannes Berg const struct nla_policy *policy, 579fceb6435SJohannes Berg struct netlink_ext_ack *extack) 580bfa83a9eSThomas Graf { 581bfa83a9eSThomas Graf if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) 582bfa83a9eSThomas Graf return -EINVAL; 583bfa83a9eSThomas Graf 584bfa83a9eSThomas Graf return nla_validate(nlmsg_attrdata(nlh, hdrlen), 585fceb6435SJohannes Berg nlmsg_attrlen(nlh, hdrlen), maxtype, policy, 586fceb6435SJohannes Berg extack); 587bfa83a9eSThomas Graf } 588bfa83a9eSThomas Graf 589bfa83a9eSThomas Graf /** 59097676b6bSThomas Graf * nlmsg_report - need to report back to application? 59197676b6bSThomas Graf * @nlh: netlink message header 59297676b6bSThomas Graf * 59397676b6bSThomas Graf * Returns 1 if a report back to the application is requested. 59497676b6bSThomas Graf */ 5953a6c2b41SPatrick McHardy static inline int nlmsg_report(const struct nlmsghdr *nlh) 59697676b6bSThomas Graf { 59797676b6bSThomas Graf return !!(nlh->nlmsg_flags & NLM_F_ECHO); 59897676b6bSThomas Graf } 59997676b6bSThomas Graf 60097676b6bSThomas Graf /** 601bfa83a9eSThomas Graf * nlmsg_for_each_attr - iterate over a stream of attributes 602bfa83a9eSThomas Graf * @pos: loop counter, set to current attribute 603bfa83a9eSThomas Graf * @nlh: netlink message header 604bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 605bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 606bfa83a9eSThomas Graf */ 607bfa83a9eSThomas Graf #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \ 608bfa83a9eSThomas Graf nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \ 609bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), rem) 610bfa83a9eSThomas Graf 611bfa83a9eSThomas Graf /** 612bfa83a9eSThomas Graf * nlmsg_put - Add a new netlink message to an skb 613bfa83a9eSThomas Graf * @skb: socket buffer to store message in 6142c6ba4b1SNicolas Dichtel * @portid: netlink PORTID of requesting application 615bfa83a9eSThomas Graf * @seq: sequence number of message 616bfa83a9eSThomas Graf * @type: message type 617bfa83a9eSThomas Graf * @payload: length of message payload 618bfa83a9eSThomas Graf * @flags: message flags 619bfa83a9eSThomas Graf * 620bfa83a9eSThomas Graf * Returns NULL if the tailroom of the skb is insufficient to store 621bfa83a9eSThomas Graf * the message header and payload. 622bfa83a9eSThomas Graf */ 62315e47304SEric W. Biederman static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, 624bfa83a9eSThomas Graf int type, int payload, int flags) 625bfa83a9eSThomas Graf { 626bfa83a9eSThomas Graf if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload))) 627bfa83a9eSThomas Graf return NULL; 628bfa83a9eSThomas Graf 62915e47304SEric W. Biederman return __nlmsg_put(skb, portid, seq, type, payload, flags); 630bfa83a9eSThomas Graf } 631bfa83a9eSThomas Graf 632bfa83a9eSThomas Graf /** 633bfa83a9eSThomas Graf * nlmsg_put_answer - Add a new callback based netlink message to an skb 634bfa83a9eSThomas Graf * @skb: socket buffer to store message in 635bfa83a9eSThomas Graf * @cb: netlink callback 636bfa83a9eSThomas Graf * @type: message type 637bfa83a9eSThomas Graf * @payload: length of message payload 638bfa83a9eSThomas Graf * @flags: message flags 639bfa83a9eSThomas Graf * 640bfa83a9eSThomas Graf * Returns NULL if the tailroom of the skb is insufficient to store 641bfa83a9eSThomas Graf * the message header and payload. 642bfa83a9eSThomas Graf */ 643bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb, 644bfa83a9eSThomas Graf struct netlink_callback *cb, 645bfa83a9eSThomas Graf int type, int payload, 646bfa83a9eSThomas Graf int flags) 647bfa83a9eSThomas Graf { 64815e47304SEric W. Biederman return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 649bfa83a9eSThomas Graf type, payload, flags); 650bfa83a9eSThomas Graf } 651bfa83a9eSThomas Graf 652bfa83a9eSThomas Graf /** 653bfa83a9eSThomas Graf * nlmsg_new - Allocate a new netlink message 654339bf98fSThomas Graf * @payload: size of the message payload 655fe4944e5SThomas Graf * @flags: the type of memory to allocate. 656bfa83a9eSThomas Graf * 657339bf98fSThomas Graf * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known 658339bf98fSThomas Graf * and a good default is needed. 659bfa83a9eSThomas Graf */ 660339bf98fSThomas Graf static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags) 661bfa83a9eSThomas Graf { 662339bf98fSThomas Graf return alloc_skb(nlmsg_total_size(payload), flags); 663bfa83a9eSThomas Graf } 664bfa83a9eSThomas Graf 665bfa83a9eSThomas Graf /** 666bfa83a9eSThomas Graf * nlmsg_end - Finalize a netlink message 667bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 668bfa83a9eSThomas Graf * @nlh: netlink message header 669bfa83a9eSThomas Graf * 670bfa83a9eSThomas Graf * Corrects the netlink message header to include the appeneded 671bfa83a9eSThomas Graf * attributes. Only necessary if attributes have been added to 672bfa83a9eSThomas Graf * the message. 673bfa83a9eSThomas Graf */ 674053c095aSJohannes Berg static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh) 675bfa83a9eSThomas Graf { 67627a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh; 677bfa83a9eSThomas Graf } 678bfa83a9eSThomas Graf 679bfa83a9eSThomas Graf /** 680fe4944e5SThomas Graf * nlmsg_get_pos - return current position in netlink message 681fe4944e5SThomas Graf * @skb: socket buffer the message is stored in 682fe4944e5SThomas Graf * 683fe4944e5SThomas Graf * Returns a pointer to the current tail of the message. 684fe4944e5SThomas Graf */ 685fe4944e5SThomas Graf static inline void *nlmsg_get_pos(struct sk_buff *skb) 686fe4944e5SThomas Graf { 68727a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 688fe4944e5SThomas Graf } 689fe4944e5SThomas Graf 690fe4944e5SThomas Graf /** 691fe4944e5SThomas Graf * nlmsg_trim - Trim message to a mark 692fe4944e5SThomas Graf * @skb: socket buffer the message is stored in 693fe4944e5SThomas Graf * @mark: mark to trim to 694fe4944e5SThomas Graf * 695bc3ed28cSThomas Graf * Trims the message to the provided mark. 696fe4944e5SThomas Graf */ 697bc3ed28cSThomas Graf static inline void nlmsg_trim(struct sk_buff *skb, const void *mark) 698fe4944e5SThomas Graf { 699149118d8SThomas Graf if (mark) { 700149118d8SThomas Graf WARN_ON((unsigned char *) mark < skb->data); 701fe4944e5SThomas Graf skb_trim(skb, (unsigned char *) mark - skb->data); 702fe4944e5SThomas Graf } 703149118d8SThomas Graf } 704fe4944e5SThomas Graf 705fe4944e5SThomas Graf /** 706bfa83a9eSThomas Graf * nlmsg_cancel - Cancel construction of a netlink message 707bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 708bfa83a9eSThomas Graf * @nlh: netlink message header 709bfa83a9eSThomas Graf * 710bfa83a9eSThomas Graf * Removes the complete netlink message including all 711bc3ed28cSThomas Graf * attributes from the socket buffer again. 712bfa83a9eSThomas Graf */ 713bc3ed28cSThomas Graf static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh) 714bfa83a9eSThomas Graf { 715bc3ed28cSThomas Graf nlmsg_trim(skb, nlh); 716bfa83a9eSThomas Graf } 717bfa83a9eSThomas Graf 718bfa83a9eSThomas Graf /** 719bfa83a9eSThomas Graf * nlmsg_free - free a netlink message 720bfa83a9eSThomas Graf * @skb: socket buffer of netlink message 721bfa83a9eSThomas Graf */ 722bfa83a9eSThomas Graf static inline void nlmsg_free(struct sk_buff *skb) 723bfa83a9eSThomas Graf { 724bfa83a9eSThomas Graf kfree_skb(skb); 725bfa83a9eSThomas Graf } 726bfa83a9eSThomas Graf 727bfa83a9eSThomas Graf /** 728bfa83a9eSThomas Graf * nlmsg_multicast - multicast a netlink message 729bfa83a9eSThomas Graf * @sk: netlink socket to spread messages to 730bfa83a9eSThomas Graf * @skb: netlink message as socket buffer 73115e47304SEric W. Biederman * @portid: own netlink portid to avoid sending to yourself 732bfa83a9eSThomas Graf * @group: multicast group id 733d387f6adSThomas Graf * @flags: allocation flags 734bfa83a9eSThomas Graf */ 735bfa83a9eSThomas Graf static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, 73615e47304SEric W. Biederman u32 portid, unsigned int group, gfp_t flags) 737bfa83a9eSThomas Graf { 738bfa83a9eSThomas Graf int err; 739bfa83a9eSThomas Graf 740bfa83a9eSThomas Graf NETLINK_CB(skb).dst_group = group; 741bfa83a9eSThomas Graf 74215e47304SEric W. Biederman err = netlink_broadcast(sk, skb, portid, group, flags); 743bfa83a9eSThomas Graf if (err > 0) 744bfa83a9eSThomas Graf err = 0; 745bfa83a9eSThomas Graf 746bfa83a9eSThomas Graf return err; 747bfa83a9eSThomas Graf } 748bfa83a9eSThomas Graf 749bfa83a9eSThomas Graf /** 750bfa83a9eSThomas Graf * nlmsg_unicast - unicast a netlink message 751bfa83a9eSThomas Graf * @sk: netlink socket to spread message to 752bfa83a9eSThomas Graf * @skb: netlink message as socket buffer 75315e47304SEric W. Biederman * @portid: netlink portid of the destination socket 754bfa83a9eSThomas Graf */ 75515e47304SEric W. Biederman static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid) 756bfa83a9eSThomas Graf { 757bfa83a9eSThomas Graf int err; 758bfa83a9eSThomas Graf 75915e47304SEric W. Biederman err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT); 760bfa83a9eSThomas Graf if (err > 0) 761bfa83a9eSThomas Graf err = 0; 762bfa83a9eSThomas Graf 763bfa83a9eSThomas Graf return err; 764bfa83a9eSThomas Graf } 765bfa83a9eSThomas Graf 766bfa83a9eSThomas Graf /** 767bfa83a9eSThomas Graf * nlmsg_for_each_msg - iterate over a stream of messages 768bfa83a9eSThomas Graf * @pos: loop counter, set to current message 769bfa83a9eSThomas Graf * @head: head of message stream 770bfa83a9eSThomas Graf * @len: length of message stream 771bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 772bfa83a9eSThomas Graf */ 773bfa83a9eSThomas Graf #define nlmsg_for_each_msg(pos, head, len, rem) \ 774bfa83a9eSThomas Graf for (pos = head, rem = len; \ 775bfa83a9eSThomas Graf nlmsg_ok(pos, rem); \ 776bfa83a9eSThomas Graf pos = nlmsg_next(pos, &(rem))) 777bfa83a9eSThomas Graf 778670dc283SJohannes Berg /** 779670dc283SJohannes Berg * nl_dump_check_consistent - check if sequence is consistent and advertise if not 780670dc283SJohannes Berg * @cb: netlink callback structure that stores the sequence number 781670dc283SJohannes Berg * @nlh: netlink message header to write the flag to 782670dc283SJohannes Berg * 783670dc283SJohannes Berg * This function checks if the sequence (generation) number changed during dump 784670dc283SJohannes Berg * and if it did, advertises it in the netlink message header. 785670dc283SJohannes Berg * 786670dc283SJohannes Berg * The correct way to use it is to set cb->seq to the generation counter when 787670dc283SJohannes Berg * all locks for dumping have been acquired, and then call this function for 788670dc283SJohannes Berg * each message that is generated. 789670dc283SJohannes Berg * 790670dc283SJohannes Berg * Note that due to initialisation concerns, 0 is an invalid sequence number 791670dc283SJohannes Berg * and must not be used by code that uses this functionality. 792670dc283SJohannes Berg */ 793670dc283SJohannes Berg static inline void 794670dc283SJohannes Berg nl_dump_check_consistent(struct netlink_callback *cb, 795670dc283SJohannes Berg struct nlmsghdr *nlh) 796670dc283SJohannes Berg { 797670dc283SJohannes Berg if (cb->prev_seq && cb->seq != cb->prev_seq) 798670dc283SJohannes Berg nlh->nlmsg_flags |= NLM_F_DUMP_INTR; 799670dc283SJohannes Berg cb->prev_seq = cb->seq; 800670dc283SJohannes Berg } 801670dc283SJohannes Berg 802bfa83a9eSThomas Graf /************************************************************************** 803bfa83a9eSThomas Graf * Netlink Attributes 804bfa83a9eSThomas Graf **************************************************************************/ 805bfa83a9eSThomas Graf 806bfa83a9eSThomas Graf /** 807bfa83a9eSThomas Graf * nla_attr_size - length of attribute not including padding 808bfa83a9eSThomas Graf * @payload: length of payload 809bfa83a9eSThomas Graf */ 810bfa83a9eSThomas Graf static inline int nla_attr_size(int payload) 811bfa83a9eSThomas Graf { 812bfa83a9eSThomas Graf return NLA_HDRLEN + payload; 813bfa83a9eSThomas Graf } 814bfa83a9eSThomas Graf 815bfa83a9eSThomas Graf /** 816bfa83a9eSThomas Graf * nla_total_size - total length of attribute including padding 817bfa83a9eSThomas Graf * @payload: length of payload 818bfa83a9eSThomas Graf */ 819bfa83a9eSThomas Graf static inline int nla_total_size(int payload) 820bfa83a9eSThomas Graf { 821bfa83a9eSThomas Graf return NLA_ALIGN(nla_attr_size(payload)); 822bfa83a9eSThomas Graf } 823bfa83a9eSThomas Graf 824bfa83a9eSThomas Graf /** 825bfa83a9eSThomas Graf * nla_padlen - length of padding at the tail of attribute 826bfa83a9eSThomas Graf * @payload: length of payload 827bfa83a9eSThomas Graf */ 828bfa83a9eSThomas Graf static inline int nla_padlen(int payload) 829bfa83a9eSThomas Graf { 830bfa83a9eSThomas Graf return nla_total_size(payload) - nla_attr_size(payload); 831bfa83a9eSThomas Graf } 832bfa83a9eSThomas Graf 833bfa83a9eSThomas Graf /** 8348f4c1f9bSThomas Graf * nla_type - attribute type 8358f4c1f9bSThomas Graf * @nla: netlink attribute 8368f4c1f9bSThomas Graf */ 8378f4c1f9bSThomas Graf static inline int nla_type(const struct nlattr *nla) 8388f4c1f9bSThomas Graf { 8398f4c1f9bSThomas Graf return nla->nla_type & NLA_TYPE_MASK; 8408f4c1f9bSThomas Graf } 8418f4c1f9bSThomas Graf 8428f4c1f9bSThomas Graf /** 843bfa83a9eSThomas Graf * nla_data - head of payload 844bfa83a9eSThomas Graf * @nla: netlink attribute 845bfa83a9eSThomas Graf */ 846bfa83a9eSThomas Graf static inline void *nla_data(const struct nlattr *nla) 847bfa83a9eSThomas Graf { 848bfa83a9eSThomas Graf return (char *) nla + NLA_HDRLEN; 849bfa83a9eSThomas Graf } 850bfa83a9eSThomas Graf 851bfa83a9eSThomas Graf /** 852bfa83a9eSThomas Graf * nla_len - length of payload 853bfa83a9eSThomas Graf * @nla: netlink attribute 854bfa83a9eSThomas Graf */ 855bfa83a9eSThomas Graf static inline int nla_len(const struct nlattr *nla) 856bfa83a9eSThomas Graf { 857bfa83a9eSThomas Graf return nla->nla_len - NLA_HDRLEN; 858bfa83a9eSThomas Graf } 859bfa83a9eSThomas Graf 860bfa83a9eSThomas Graf /** 861bfa83a9eSThomas Graf * nla_ok - check if the netlink attribute fits into the remaining bytes 862bfa83a9eSThomas Graf * @nla: netlink attribute 863bfa83a9eSThomas Graf * @remaining: number of bytes remaining in attribute stream 864bfa83a9eSThomas Graf */ 865bfa83a9eSThomas Graf static inline int nla_ok(const struct nlattr *nla, int remaining) 866bfa83a9eSThomas Graf { 8673e1ed981SAlexey Dobriyan return remaining >= (int) sizeof(*nla) && 8683e1ed981SAlexey Dobriyan nla->nla_len >= sizeof(*nla) && 869bfa83a9eSThomas Graf nla->nla_len <= remaining; 870bfa83a9eSThomas Graf } 871bfa83a9eSThomas Graf 872bfa83a9eSThomas Graf /** 873d1ec3b77SPierre Ynard * nla_next - next netlink attribute in attribute stream 874bfa83a9eSThomas Graf * @nla: netlink attribute 875bfa83a9eSThomas Graf * @remaining: number of bytes remaining in attribute stream 876bfa83a9eSThomas Graf * 877bfa83a9eSThomas Graf * Returns the next netlink attribute in the attribute stream and 878bfa83a9eSThomas Graf * decrements remaining by the size of the current attribute. 879bfa83a9eSThomas Graf */ 880bfa83a9eSThomas Graf static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining) 881bfa83a9eSThomas Graf { 8823b2c75d3SAlexey Dobriyan unsigned int totlen = NLA_ALIGN(nla->nla_len); 883bfa83a9eSThomas Graf 884bfa83a9eSThomas Graf *remaining -= totlen; 885bfa83a9eSThomas Graf return (struct nlattr *) ((char *) nla + totlen); 886bfa83a9eSThomas Graf } 887bfa83a9eSThomas Graf 888bfa83a9eSThomas Graf /** 889fe4944e5SThomas Graf * nla_find_nested - find attribute in a set of nested attributes 890fe4944e5SThomas Graf * @nla: attribute containing the nested attributes 891fe4944e5SThomas Graf * @attrtype: type of attribute to look for 892fe4944e5SThomas Graf * 893fe4944e5SThomas Graf * Returns the first attribute which matches the specified type. 894fe4944e5SThomas Graf */ 8953654654fSJan Engelhardt static inline struct nlattr * 8963654654fSJan Engelhardt nla_find_nested(const struct nlattr *nla, int attrtype) 897fe4944e5SThomas Graf { 898fe4944e5SThomas Graf return nla_find(nla_data(nla), nla_len(nla), attrtype); 899fe4944e5SThomas Graf } 900fe4944e5SThomas Graf 901fe4944e5SThomas Graf /** 902bfa83a9eSThomas Graf * nla_parse_nested - parse nested attributes 903bfa83a9eSThomas Graf * @tb: destination array with maxtype+1 elements 904bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 905bfa83a9eSThomas Graf * @nla: attribute containing the nested attributes 906bfa83a9eSThomas Graf * @policy: validation policy 907fceb6435SJohannes Berg * @extack: extended ACK report struct 908bfa83a9eSThomas Graf * 909bfa83a9eSThomas Graf * See nla_parse() 910bfa83a9eSThomas Graf */ 911bfa83a9eSThomas Graf static inline int nla_parse_nested(struct nlattr *tb[], int maxtype, 912b057efd4SPatrick McHardy const struct nlattr *nla, 913fceb6435SJohannes Berg const struct nla_policy *policy, 914fceb6435SJohannes Berg struct netlink_ext_ack *extack) 915bfa83a9eSThomas Graf { 916fceb6435SJohannes Berg return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy, 917fceb6435SJohannes Berg extack); 918bfa83a9eSThomas Graf } 9191092cb21SPatrick McHardy 9201092cb21SPatrick McHardy /** 921d1ec3b77SPierre Ynard * nla_put_u8 - Add a u8 netlink attribute to a socket buffer 922bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 923bfa83a9eSThomas Graf * @attrtype: attribute type 924bfa83a9eSThomas Graf * @value: numeric value 925bfa83a9eSThomas Graf */ 926bfa83a9eSThomas Graf static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value) 927bfa83a9eSThomas Graf { 928b4391db4SArnd Bergmann /* temporary variables to work around GCC PR81715 with asan-stack=1 */ 929b4391db4SArnd Bergmann u8 tmp = value; 930b4391db4SArnd Bergmann 931b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(u8), &tmp); 932bfa83a9eSThomas Graf } 933bfa83a9eSThomas Graf 934bfa83a9eSThomas Graf /** 935bfa83a9eSThomas Graf * nla_put_u16 - Add a u16 netlink attribute to a socket buffer 936bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 937bfa83a9eSThomas Graf * @attrtype: attribute type 938bfa83a9eSThomas Graf * @value: numeric value 939bfa83a9eSThomas Graf */ 940bfa83a9eSThomas Graf static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value) 941bfa83a9eSThomas Graf { 942b4391db4SArnd Bergmann u16 tmp = value; 943b4391db4SArnd Bergmann 944b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(u16), &tmp); 945bfa83a9eSThomas Graf } 946bfa83a9eSThomas Graf 947bfa83a9eSThomas Graf /** 948569a8fc3SDavid S. Miller * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer 949569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 950569a8fc3SDavid S. Miller * @attrtype: attribute type 951569a8fc3SDavid S. Miller * @value: numeric value 952569a8fc3SDavid S. Miller */ 953569a8fc3SDavid S. Miller static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value) 954569a8fc3SDavid S. Miller { 955b4391db4SArnd Bergmann __be16 tmp = value; 956b4391db4SArnd Bergmann 957b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__be16), &tmp); 958569a8fc3SDavid S. Miller } 959569a8fc3SDavid S. Miller 960569a8fc3SDavid S. Miller /** 9616c1dd3b6SDavid S. Miller * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer 9626c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 9636c1dd3b6SDavid S. Miller * @attrtype: attribute type 9646c1dd3b6SDavid S. Miller * @value: numeric value 9656c1dd3b6SDavid S. Miller */ 9666c1dd3b6SDavid S. Miller static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value) 9676c1dd3b6SDavid S. Miller { 968b4391db4SArnd Bergmann __be16 tmp = value; 969b4391db4SArnd Bergmann 970b4391db4SArnd Bergmann return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp); 9716c1dd3b6SDavid S. Miller } 9726c1dd3b6SDavid S. Miller 9736c1dd3b6SDavid S. Miller /** 97424c410dcSDavid S. Miller * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer 97524c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 97624c410dcSDavid S. Miller * @attrtype: attribute type 97724c410dcSDavid S. Miller * @value: numeric value 97824c410dcSDavid S. Miller */ 97924c410dcSDavid S. Miller static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value) 98024c410dcSDavid S. Miller { 981b4391db4SArnd Bergmann __le16 tmp = value; 982b4391db4SArnd Bergmann 983b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__le16), &tmp); 98424c410dcSDavid S. Miller } 98524c410dcSDavid S. Miller 98624c410dcSDavid S. Miller /** 987bfa83a9eSThomas Graf * nla_put_u32 - Add a u32 netlink attribute to a socket buffer 988bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 989bfa83a9eSThomas Graf * @attrtype: attribute type 990bfa83a9eSThomas Graf * @value: numeric value 991bfa83a9eSThomas Graf */ 992bfa83a9eSThomas Graf static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value) 993bfa83a9eSThomas Graf { 994b4391db4SArnd Bergmann u32 tmp = value; 995b4391db4SArnd Bergmann 996b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(u32), &tmp); 997bfa83a9eSThomas Graf } 998bfa83a9eSThomas Graf 999bfa83a9eSThomas Graf /** 1000569a8fc3SDavid S. Miller * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer 1001569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 1002569a8fc3SDavid S. Miller * @attrtype: attribute type 1003569a8fc3SDavid S. Miller * @value: numeric value 1004569a8fc3SDavid S. Miller */ 1005569a8fc3SDavid S. Miller static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value) 1006569a8fc3SDavid S. Miller { 1007b4391db4SArnd Bergmann __be32 tmp = value; 1008b4391db4SArnd Bergmann 1009b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__be32), &tmp); 1010569a8fc3SDavid S. Miller } 1011569a8fc3SDavid S. Miller 1012569a8fc3SDavid S. Miller /** 10136c1dd3b6SDavid S. Miller * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer 10146c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 10156c1dd3b6SDavid S. Miller * @attrtype: attribute type 10166c1dd3b6SDavid S. Miller * @value: numeric value 10176c1dd3b6SDavid S. Miller */ 10186c1dd3b6SDavid S. Miller static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value) 10196c1dd3b6SDavid S. Miller { 1020b4391db4SArnd Bergmann __be32 tmp = value; 1021b4391db4SArnd Bergmann 1022b4391db4SArnd Bergmann return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp); 10236c1dd3b6SDavid S. Miller } 10246c1dd3b6SDavid S. Miller 10256c1dd3b6SDavid S. Miller /** 102624c410dcSDavid S. Miller * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer 102724c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 102824c410dcSDavid S. Miller * @attrtype: attribute type 102924c410dcSDavid S. Miller * @value: numeric value 103024c410dcSDavid S. Miller */ 103124c410dcSDavid S. Miller static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value) 103224c410dcSDavid S. Miller { 1033b4391db4SArnd Bergmann __le32 tmp = value; 1034b4391db4SArnd Bergmann 1035b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__le32), &tmp); 103624c410dcSDavid S. Miller } 103724c410dcSDavid S. Miller 103824c410dcSDavid S. Miller /** 103973520786SNicolas Dichtel * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it 104073520786SNicolas Dichtel * @skb: socket buffer to add attribute to 104173520786SNicolas Dichtel * @attrtype: attribute type 104273520786SNicolas Dichtel * @value: numeric value 104373520786SNicolas Dichtel * @padattr: attribute type for the padding 104473520786SNicolas Dichtel */ 104573520786SNicolas Dichtel static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype, 104673520786SNicolas Dichtel u64 value, int padattr) 104773520786SNicolas Dichtel { 1048b4391db4SArnd Bergmann u64 tmp = value; 1049b4391db4SArnd Bergmann 1050b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr); 105173520786SNicolas Dichtel } 105273520786SNicolas Dichtel 105373520786SNicolas Dichtel /** 1054b46f6dedSNicolas Dichtel * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it 1055569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 1056569a8fc3SDavid S. Miller * @attrtype: attribute type 1057569a8fc3SDavid S. Miller * @value: numeric value 1058b46f6dedSNicolas Dichtel * @padattr: attribute type for the padding 1059569a8fc3SDavid S. Miller */ 1060b46f6dedSNicolas Dichtel static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value, 1061b46f6dedSNicolas Dichtel int padattr) 1062b46f6dedSNicolas Dichtel { 1063b4391db4SArnd Bergmann __be64 tmp = value; 1064b4391db4SArnd Bergmann 1065b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr); 1066b46f6dedSNicolas Dichtel } 1067b46f6dedSNicolas Dichtel 1068569a8fc3SDavid S. Miller /** 1069e9bbe898SNicolas Dichtel * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it 10706c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 10716c1dd3b6SDavid S. Miller * @attrtype: attribute type 10726c1dd3b6SDavid S. Miller * @value: numeric value 1073e9bbe898SNicolas Dichtel * @padattr: attribute type for the padding 10746c1dd3b6SDavid S. Miller */ 1075e9bbe898SNicolas Dichtel static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value, 1076e9bbe898SNicolas Dichtel int padattr) 10776c1dd3b6SDavid S. Miller { 1078b4391db4SArnd Bergmann __be64 tmp = value; 1079b4391db4SArnd Bergmann 1080b4391db4SArnd Bergmann return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp, 1081e9bbe898SNicolas Dichtel padattr); 10826c1dd3b6SDavid S. Miller } 10836c1dd3b6SDavid S. Miller 10846c1dd3b6SDavid S. Miller /** 1085e7479122SNicolas Dichtel * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it 108624c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 108724c410dcSDavid S. Miller * @attrtype: attribute type 108824c410dcSDavid S. Miller * @value: numeric value 1089e7479122SNicolas Dichtel * @padattr: attribute type for the padding 109024c410dcSDavid S. Miller */ 1091e7479122SNicolas Dichtel static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value, 1092e7479122SNicolas Dichtel int padattr) 109324c410dcSDavid S. Miller { 1094b4391db4SArnd Bergmann __le64 tmp = value; 1095b4391db4SArnd Bergmann 1096b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr); 109724c410dcSDavid S. Miller } 109824c410dcSDavid S. Miller 109924c410dcSDavid S. Miller /** 11004778e0beSJiri Pirko * nla_put_s8 - Add a s8 netlink attribute to a socket buffer 11014778e0beSJiri Pirko * @skb: socket buffer to add attribute to 11024778e0beSJiri Pirko * @attrtype: attribute type 11034778e0beSJiri Pirko * @value: numeric value 11044778e0beSJiri Pirko */ 11054778e0beSJiri Pirko static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value) 11064778e0beSJiri Pirko { 1107b4391db4SArnd Bergmann s8 tmp = value; 1108b4391db4SArnd Bergmann 1109b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(s8), &tmp); 11104778e0beSJiri Pirko } 11114778e0beSJiri Pirko 11124778e0beSJiri Pirko /** 11134778e0beSJiri Pirko * nla_put_s16 - Add a s16 netlink attribute to a socket buffer 11144778e0beSJiri Pirko * @skb: socket buffer to add attribute to 11154778e0beSJiri Pirko * @attrtype: attribute type 11164778e0beSJiri Pirko * @value: numeric value 11174778e0beSJiri Pirko */ 11184778e0beSJiri Pirko static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value) 11194778e0beSJiri Pirko { 1120b4391db4SArnd Bergmann s16 tmp = value; 1121b4391db4SArnd Bergmann 1122b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(s16), &tmp); 11234778e0beSJiri Pirko } 11244778e0beSJiri Pirko 11254778e0beSJiri Pirko /** 11264778e0beSJiri Pirko * nla_put_s32 - Add a s32 netlink attribute to a socket buffer 11274778e0beSJiri Pirko * @skb: socket buffer to add attribute to 11284778e0beSJiri Pirko * @attrtype: attribute type 11294778e0beSJiri Pirko * @value: numeric value 11304778e0beSJiri Pirko */ 11314778e0beSJiri Pirko static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value) 11324778e0beSJiri Pirko { 1133b4391db4SArnd Bergmann s32 tmp = value; 1134b4391db4SArnd Bergmann 1135b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(s32), &tmp); 11364778e0beSJiri Pirko } 11374778e0beSJiri Pirko 11384778e0beSJiri Pirko /** 1139756a2f59SNicolas Dichtel * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it 11404778e0beSJiri Pirko * @skb: socket buffer to add attribute to 11414778e0beSJiri Pirko * @attrtype: attribute type 11424778e0beSJiri Pirko * @value: numeric value 1143756a2f59SNicolas Dichtel * @padattr: attribute type for the padding 11444778e0beSJiri Pirko */ 1145756a2f59SNicolas Dichtel static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value, 1146756a2f59SNicolas Dichtel int padattr) 11474778e0beSJiri Pirko { 1148b4391db4SArnd Bergmann s64 tmp = value; 1149b4391db4SArnd Bergmann 1150b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr); 11514778e0beSJiri Pirko } 11524778e0beSJiri Pirko 11534778e0beSJiri Pirko /** 1154bfa83a9eSThomas Graf * nla_put_string - Add a string netlink attribute to a socket buffer 1155bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 1156bfa83a9eSThomas Graf * @attrtype: attribute type 1157bfa83a9eSThomas Graf * @str: NUL terminated string 1158bfa83a9eSThomas Graf */ 1159bfa83a9eSThomas Graf static inline int nla_put_string(struct sk_buff *skb, int attrtype, 1160bfa83a9eSThomas Graf const char *str) 1161bfa83a9eSThomas Graf { 1162bfa83a9eSThomas Graf return nla_put(skb, attrtype, strlen(str) + 1, str); 1163bfa83a9eSThomas Graf } 1164bfa83a9eSThomas Graf 1165bfa83a9eSThomas Graf /** 1166bfa83a9eSThomas Graf * nla_put_flag - Add a flag netlink attribute to a socket buffer 1167bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 1168bfa83a9eSThomas Graf * @attrtype: attribute type 1169bfa83a9eSThomas Graf */ 1170bfa83a9eSThomas Graf static inline int nla_put_flag(struct sk_buff *skb, int attrtype) 1171bfa83a9eSThomas Graf { 1172bfa83a9eSThomas Graf return nla_put(skb, attrtype, 0, NULL); 1173bfa83a9eSThomas Graf } 1174bfa83a9eSThomas Graf 1175bfa83a9eSThomas Graf /** 11762175d87cSNicolas Dichtel * nla_put_msecs - Add a msecs netlink attribute to a skb and align it 1177bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 1178bfa83a9eSThomas Graf * @attrtype: attribute type 1179d87de1f3SMark Rustad * @njiffies: number of jiffies to convert to msecs 11802175d87cSNicolas Dichtel * @padattr: attribute type for the padding 1181bfa83a9eSThomas Graf */ 1182bfa83a9eSThomas Graf static inline int nla_put_msecs(struct sk_buff *skb, int attrtype, 11832175d87cSNicolas Dichtel unsigned long njiffies, int padattr) 1184bfa83a9eSThomas Graf { 1185d87de1f3SMark Rustad u64 tmp = jiffies_to_msecs(njiffies); 11862175d87cSNicolas Dichtel 11872175d87cSNicolas Dichtel return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr); 1188bfa83a9eSThomas Graf } 1189bfa83a9eSThomas Graf 1190bfa83a9eSThomas Graf /** 1191930345eaSJiri Benc * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket 1192930345eaSJiri Benc * buffer 1193930345eaSJiri Benc * @skb: socket buffer to add attribute to 1194930345eaSJiri Benc * @attrtype: attribute type 1195930345eaSJiri Benc * @addr: IPv4 address 1196930345eaSJiri Benc */ 1197930345eaSJiri Benc static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype, 1198930345eaSJiri Benc __be32 addr) 1199930345eaSJiri Benc { 1200b4391db4SArnd Bergmann __be32 tmp = addr; 1201b4391db4SArnd Bergmann 1202b4391db4SArnd Bergmann return nla_put_be32(skb, attrtype, tmp); 1203930345eaSJiri Benc } 1204930345eaSJiri Benc 1205930345eaSJiri Benc /** 1206930345eaSJiri Benc * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket 1207930345eaSJiri Benc * buffer 1208930345eaSJiri Benc * @skb: socket buffer to add attribute to 1209930345eaSJiri Benc * @attrtype: attribute type 1210930345eaSJiri Benc * @addr: IPv6 address 1211930345eaSJiri Benc */ 1212930345eaSJiri Benc static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype, 1213930345eaSJiri Benc const struct in6_addr *addr) 1214930345eaSJiri Benc { 1215930345eaSJiri Benc return nla_put(skb, attrtype, sizeof(*addr), addr); 1216930345eaSJiri Benc } 1217930345eaSJiri Benc 1218930345eaSJiri Benc /** 1219bfa83a9eSThomas Graf * nla_get_u32 - return payload of u32 attribute 1220bfa83a9eSThomas Graf * @nla: u32 netlink attribute 1221bfa83a9eSThomas Graf */ 1222b057efd4SPatrick McHardy static inline u32 nla_get_u32(const struct nlattr *nla) 1223bfa83a9eSThomas Graf { 1224bfa83a9eSThomas Graf return *(u32 *) nla_data(nla); 1225bfa83a9eSThomas Graf } 1226bfa83a9eSThomas Graf 1227bfa83a9eSThomas Graf /** 122800012e5bSAl Viro * nla_get_be32 - return payload of __be32 attribute 122900012e5bSAl Viro * @nla: __be32 netlink attribute 123000012e5bSAl Viro */ 1231b057efd4SPatrick McHardy static inline __be32 nla_get_be32(const struct nlattr *nla) 123200012e5bSAl Viro { 123300012e5bSAl Viro return *(__be32 *) nla_data(nla); 123400012e5bSAl Viro } 123500012e5bSAl Viro 123600012e5bSAl Viro /** 1237c648a013SAlexander Aring * nla_get_le32 - return payload of __le32 attribute 1238c648a013SAlexander Aring * @nla: __le32 netlink attribute 1239c648a013SAlexander Aring */ 1240c648a013SAlexander Aring static inline __le32 nla_get_le32(const struct nlattr *nla) 1241c648a013SAlexander Aring { 1242c648a013SAlexander Aring return *(__le32 *) nla_data(nla); 1243c648a013SAlexander Aring } 1244c648a013SAlexander Aring 1245c648a013SAlexander Aring /** 1246bfa83a9eSThomas Graf * nla_get_u16 - return payload of u16 attribute 1247bfa83a9eSThomas Graf * @nla: u16 netlink attribute 1248bfa83a9eSThomas Graf */ 1249b057efd4SPatrick McHardy static inline u16 nla_get_u16(const struct nlattr *nla) 1250bfa83a9eSThomas Graf { 1251bfa83a9eSThomas Graf return *(u16 *) nla_data(nla); 1252bfa83a9eSThomas Graf } 1253bfa83a9eSThomas Graf 1254bfa83a9eSThomas Graf /** 1255838965baSPatrick McHardy * nla_get_be16 - return payload of __be16 attribute 1256838965baSPatrick McHardy * @nla: __be16 netlink attribute 1257838965baSPatrick McHardy */ 1258b057efd4SPatrick McHardy static inline __be16 nla_get_be16(const struct nlattr *nla) 1259838965baSPatrick McHardy { 1260838965baSPatrick McHardy return *(__be16 *) nla_data(nla); 1261838965baSPatrick McHardy } 1262838965baSPatrick McHardy 1263838965baSPatrick McHardy /** 12644a89c256SThomas Graf * nla_get_le16 - return payload of __le16 attribute 12654a89c256SThomas Graf * @nla: __le16 netlink attribute 12664a89c256SThomas Graf */ 1267b057efd4SPatrick McHardy static inline __le16 nla_get_le16(const struct nlattr *nla) 12684a89c256SThomas Graf { 12694a89c256SThomas Graf return *(__le16 *) nla_data(nla); 12704a89c256SThomas Graf } 12714a89c256SThomas Graf 12724a89c256SThomas Graf /** 1273bfa83a9eSThomas Graf * nla_get_u8 - return payload of u8 attribute 1274bfa83a9eSThomas Graf * @nla: u8 netlink attribute 1275bfa83a9eSThomas Graf */ 1276b057efd4SPatrick McHardy static inline u8 nla_get_u8(const struct nlattr *nla) 1277bfa83a9eSThomas Graf { 1278bfa83a9eSThomas Graf return *(u8 *) nla_data(nla); 1279bfa83a9eSThomas Graf } 1280bfa83a9eSThomas Graf 1281bfa83a9eSThomas Graf /** 1282bfa83a9eSThomas Graf * nla_get_u64 - return payload of u64 attribute 1283bfa83a9eSThomas Graf * @nla: u64 netlink attribute 1284bfa83a9eSThomas Graf */ 1285b057efd4SPatrick McHardy static inline u64 nla_get_u64(const struct nlattr *nla) 1286bfa83a9eSThomas Graf { 1287bfa83a9eSThomas Graf u64 tmp; 1288bfa83a9eSThomas Graf 1289bfa83a9eSThomas Graf nla_memcpy(&tmp, nla, sizeof(tmp)); 1290bfa83a9eSThomas Graf 1291bfa83a9eSThomas Graf return tmp; 1292bfa83a9eSThomas Graf } 1293bfa83a9eSThomas Graf 1294bfa83a9eSThomas Graf /** 1295a17c8598SPablo Neira Ayuso * nla_get_be64 - return payload of __be64 attribute 1296a17c8598SPablo Neira Ayuso * @nla: __be64 netlink attribute 1297a17c8598SPablo Neira Ayuso */ 1298a17c8598SPablo Neira Ayuso static inline __be64 nla_get_be64(const struct nlattr *nla) 1299a17c8598SPablo Neira Ayuso { 1300f5d410f2SPablo Neira Ayuso __be64 tmp; 1301f5d410f2SPablo Neira Ayuso 1302f5d410f2SPablo Neira Ayuso nla_memcpy(&tmp, nla, sizeof(tmp)); 1303f5d410f2SPablo Neira Ayuso 1304f5d410f2SPablo Neira Ayuso return tmp; 1305a17c8598SPablo Neira Ayuso } 1306a17c8598SPablo Neira Ayuso 1307a17c8598SPablo Neira Ayuso /** 1308c648a013SAlexander Aring * nla_get_le64 - return payload of __le64 attribute 1309c648a013SAlexander Aring * @nla: __le64 netlink attribute 1310c648a013SAlexander Aring */ 1311c648a013SAlexander Aring static inline __le64 nla_get_le64(const struct nlattr *nla) 1312c648a013SAlexander Aring { 1313c648a013SAlexander Aring return *(__le64 *) nla_data(nla); 1314c648a013SAlexander Aring } 1315c648a013SAlexander Aring 1316c648a013SAlexander Aring /** 13174778e0beSJiri Pirko * nla_get_s32 - return payload of s32 attribute 13184778e0beSJiri Pirko * @nla: s32 netlink attribute 13194778e0beSJiri Pirko */ 13204778e0beSJiri Pirko static inline s32 nla_get_s32(const struct nlattr *nla) 13214778e0beSJiri Pirko { 13224778e0beSJiri Pirko return *(s32 *) nla_data(nla); 13234778e0beSJiri Pirko } 13244778e0beSJiri Pirko 13254778e0beSJiri Pirko /** 13264778e0beSJiri Pirko * nla_get_s16 - return payload of s16 attribute 13274778e0beSJiri Pirko * @nla: s16 netlink attribute 13284778e0beSJiri Pirko */ 13294778e0beSJiri Pirko static inline s16 nla_get_s16(const struct nlattr *nla) 13304778e0beSJiri Pirko { 13314778e0beSJiri Pirko return *(s16 *) nla_data(nla); 13324778e0beSJiri Pirko } 13334778e0beSJiri Pirko 13344778e0beSJiri Pirko /** 13354778e0beSJiri Pirko * nla_get_s8 - return payload of s8 attribute 13364778e0beSJiri Pirko * @nla: s8 netlink attribute 13374778e0beSJiri Pirko */ 13384778e0beSJiri Pirko static inline s8 nla_get_s8(const struct nlattr *nla) 13394778e0beSJiri Pirko { 13404778e0beSJiri Pirko return *(s8 *) nla_data(nla); 13414778e0beSJiri Pirko } 13424778e0beSJiri Pirko 13434778e0beSJiri Pirko /** 13444778e0beSJiri Pirko * nla_get_s64 - return payload of s64 attribute 13454778e0beSJiri Pirko * @nla: s64 netlink attribute 13464778e0beSJiri Pirko */ 13474778e0beSJiri Pirko static inline s64 nla_get_s64(const struct nlattr *nla) 13484778e0beSJiri Pirko { 13494778e0beSJiri Pirko s64 tmp; 13504778e0beSJiri Pirko 13514778e0beSJiri Pirko nla_memcpy(&tmp, nla, sizeof(tmp)); 13524778e0beSJiri Pirko 13534778e0beSJiri Pirko return tmp; 13544778e0beSJiri Pirko } 13554778e0beSJiri Pirko 13564778e0beSJiri Pirko /** 1357bfa83a9eSThomas Graf * nla_get_flag - return payload of flag attribute 1358bfa83a9eSThomas Graf * @nla: flag netlink attribute 1359bfa83a9eSThomas Graf */ 1360b057efd4SPatrick McHardy static inline int nla_get_flag(const struct nlattr *nla) 1361bfa83a9eSThomas Graf { 1362bfa83a9eSThomas Graf return !!nla; 1363bfa83a9eSThomas Graf } 1364bfa83a9eSThomas Graf 1365bfa83a9eSThomas Graf /** 1366bfa83a9eSThomas Graf * nla_get_msecs - return payload of msecs attribute 1367bfa83a9eSThomas Graf * @nla: msecs netlink attribute 1368bfa83a9eSThomas Graf * 1369bfa83a9eSThomas Graf * Returns the number of milliseconds in jiffies. 1370bfa83a9eSThomas Graf */ 1371b057efd4SPatrick McHardy static inline unsigned long nla_get_msecs(const struct nlattr *nla) 1372bfa83a9eSThomas Graf { 1373bfa83a9eSThomas Graf u64 msecs = nla_get_u64(nla); 1374bfa83a9eSThomas Graf 1375bfa83a9eSThomas Graf return msecs_to_jiffies((unsigned long) msecs); 1376bfa83a9eSThomas Graf } 1377bfa83a9eSThomas Graf 1378bfa83a9eSThomas Graf /** 137967b61f6cSJiri Benc * nla_get_in_addr - return payload of IPv4 address attribute 138067b61f6cSJiri Benc * @nla: IPv4 address netlink attribute 138167b61f6cSJiri Benc */ 138267b61f6cSJiri Benc static inline __be32 nla_get_in_addr(const struct nlattr *nla) 138367b61f6cSJiri Benc { 138467b61f6cSJiri Benc return *(__be32 *) nla_data(nla); 138567b61f6cSJiri Benc } 138667b61f6cSJiri Benc 138767b61f6cSJiri Benc /** 138867b61f6cSJiri Benc * nla_get_in6_addr - return payload of IPv6 address attribute 138967b61f6cSJiri Benc * @nla: IPv6 address netlink attribute 139067b61f6cSJiri Benc */ 139167b61f6cSJiri Benc static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla) 139267b61f6cSJiri Benc { 139367b61f6cSJiri Benc struct in6_addr tmp; 139467b61f6cSJiri Benc 139567b61f6cSJiri Benc nla_memcpy(&tmp, nla, sizeof(tmp)); 139667b61f6cSJiri Benc return tmp; 139767b61f6cSJiri Benc } 139867b61f6cSJiri Benc 139967b61f6cSJiri Benc /** 140064c83d83SJamal Hadi Salim * nla_get_bitfield32 - return payload of 32 bitfield attribute 140164c83d83SJamal Hadi Salim * @nla: nla_bitfield32 attribute 140264c83d83SJamal Hadi Salim */ 140364c83d83SJamal Hadi Salim static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla) 140464c83d83SJamal Hadi Salim { 140564c83d83SJamal Hadi Salim struct nla_bitfield32 tmp; 140664c83d83SJamal Hadi Salim 140764c83d83SJamal Hadi Salim nla_memcpy(&tmp, nla, sizeof(tmp)); 140864c83d83SJamal Hadi Salim return tmp; 140964c83d83SJamal Hadi Salim } 141064c83d83SJamal Hadi Salim 141164c83d83SJamal Hadi Salim /** 1412b15ca182SThomas Graf * nla_memdup - duplicate attribute memory (kmemdup) 1413b15ca182SThomas Graf * @src: netlink attribute to duplicate from 1414b15ca182SThomas Graf * @gfp: GFP mask 1415b15ca182SThomas Graf */ 1416b15ca182SThomas Graf static inline void *nla_memdup(const struct nlattr *src, gfp_t gfp) 1417b15ca182SThomas Graf { 1418b15ca182SThomas Graf return kmemdup(nla_data(src), nla_len(src), gfp); 1419b15ca182SThomas Graf } 1420b15ca182SThomas Graf 1421b15ca182SThomas Graf /** 1422ae0be8deSMichal Kubecek * nla_nest_start_noflag - Start a new level of nested attributes 1423bfa83a9eSThomas Graf * @skb: socket buffer to add attributes to 1424bfa83a9eSThomas Graf * @attrtype: attribute type of container 1425bfa83a9eSThomas Graf * 1426ae0be8deSMichal Kubecek * This function exists for backward compatibility to use in APIs which never 1427ae0be8deSMichal Kubecek * marked their nest attributes with NLA_F_NESTED flag. New APIs should use 1428ae0be8deSMichal Kubecek * nla_nest_start() which sets the flag. 1429ae0be8deSMichal Kubecek * 1430ae0be8deSMichal Kubecek * Returns the container attribute or NULL on error 1431bfa83a9eSThomas Graf */ 1432ae0be8deSMichal Kubecek static inline struct nlattr *nla_nest_start_noflag(struct sk_buff *skb, 1433ae0be8deSMichal Kubecek int attrtype) 1434bfa83a9eSThomas Graf { 143527a884dcSArnaldo Carvalho de Melo struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb); 1436bfa83a9eSThomas Graf 1437bfa83a9eSThomas Graf if (nla_put(skb, attrtype, 0, NULL) < 0) 1438bfa83a9eSThomas Graf return NULL; 1439bfa83a9eSThomas Graf 1440bfa83a9eSThomas Graf return start; 1441bfa83a9eSThomas Graf } 1442bfa83a9eSThomas Graf 1443bfa83a9eSThomas Graf /** 1444ae0be8deSMichal Kubecek * nla_nest_start - Start a new level of nested attributes, with NLA_F_NESTED 1445ae0be8deSMichal Kubecek * @skb: socket buffer to add attributes to 1446ae0be8deSMichal Kubecek * @attrtype: attribute type of container 1447ae0be8deSMichal Kubecek * 1448ae0be8deSMichal Kubecek * Unlike nla_nest_start_noflag(), mark the nest attribute with NLA_F_NESTED 1449ae0be8deSMichal Kubecek * flag. This is the preferred function to use in new code. 1450ae0be8deSMichal Kubecek * 1451ae0be8deSMichal Kubecek * Returns the container attribute or NULL on error 1452ae0be8deSMichal Kubecek */ 1453ae0be8deSMichal Kubecek static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype) 1454ae0be8deSMichal Kubecek { 1455ae0be8deSMichal Kubecek return nla_nest_start_noflag(skb, attrtype | NLA_F_NESTED); 1456ae0be8deSMichal Kubecek } 1457ae0be8deSMichal Kubecek 1458ae0be8deSMichal Kubecek /** 1459bfa83a9eSThomas Graf * nla_nest_end - Finalize nesting of attributes 1460d1ec3b77SPierre Ynard * @skb: socket buffer the attributes are stored in 1461bfa83a9eSThomas Graf * @start: container attribute 1462bfa83a9eSThomas Graf * 1463bfa83a9eSThomas Graf * Corrects the container attribute header to include the all 1464bfa83a9eSThomas Graf * appeneded attributes. 1465bfa83a9eSThomas Graf * 1466bfa83a9eSThomas Graf * Returns the total data length of the skb. 1467bfa83a9eSThomas Graf */ 1468bfa83a9eSThomas Graf static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start) 1469bfa83a9eSThomas Graf { 147027a884dcSArnaldo Carvalho de Melo start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start; 1471bfa83a9eSThomas Graf return skb->len; 1472bfa83a9eSThomas Graf } 1473bfa83a9eSThomas Graf 1474bfa83a9eSThomas Graf /** 1475bfa83a9eSThomas Graf * nla_nest_cancel - Cancel nesting of attributes 1476bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 1477bfa83a9eSThomas Graf * @start: container attribute 1478bfa83a9eSThomas Graf * 1479bfa83a9eSThomas Graf * Removes the container attribute and including all nested 1480bc3ed28cSThomas Graf * attributes. Returns -EMSGSIZE 1481bfa83a9eSThomas Graf */ 1482bc3ed28cSThomas Graf static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start) 1483bfa83a9eSThomas Graf { 1484bc3ed28cSThomas Graf nlmsg_trim(skb, start); 1485bfa83a9eSThomas Graf } 1486bfa83a9eSThomas Graf 1487bfa83a9eSThomas Graf /** 14884fe5d5c0SPaul Moore * nla_validate_nested - Validate a stream of nested attributes 14894fe5d5c0SPaul Moore * @start: container attribute 14904fe5d5c0SPaul Moore * @maxtype: maximum attribute type to be expected 14914fe5d5c0SPaul Moore * @policy: validation policy 1492fceb6435SJohannes Berg * @extack: extended ACK report struct 14934fe5d5c0SPaul Moore * 14944fe5d5c0SPaul Moore * Validates all attributes in the nested attribute stream against the 14954fe5d5c0SPaul Moore * specified policy. Attributes with a type exceeding maxtype will be 14964fe5d5c0SPaul Moore * ignored. See documenation of struct nla_policy for more details. 14974fe5d5c0SPaul Moore * 14984fe5d5c0SPaul Moore * Returns 0 on success or a negative error code. 14994fe5d5c0SPaul Moore */ 15003654654fSJan Engelhardt static inline int nla_validate_nested(const struct nlattr *start, int maxtype, 1501fceb6435SJohannes Berg const struct nla_policy *policy, 1502fceb6435SJohannes Berg struct netlink_ext_ack *extack) 15034fe5d5c0SPaul Moore { 1504fceb6435SJohannes Berg return nla_validate(nla_data(start), nla_len(start), maxtype, policy, 1505fceb6435SJohannes Berg extack); 15064fe5d5c0SPaul Moore } 15074fe5d5c0SPaul Moore 15084fe5d5c0SPaul Moore /** 1509089bf1a6SNicolas Dichtel * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute 1510089bf1a6SNicolas Dichtel * @skb: socket buffer the message is stored in 1511089bf1a6SNicolas Dichtel * 1512089bf1a6SNicolas Dichtel * Return true if padding is needed to align the next attribute (nla_data()) to 1513089bf1a6SNicolas Dichtel * a 64-bit aligned area. 1514089bf1a6SNicolas Dichtel */ 1515089bf1a6SNicolas Dichtel static inline bool nla_need_padding_for_64bit(struct sk_buff *skb) 1516089bf1a6SNicolas Dichtel { 1517089bf1a6SNicolas Dichtel #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1518089bf1a6SNicolas Dichtel /* The nlattr header is 4 bytes in size, that's why we test 1519089bf1a6SNicolas Dichtel * if the skb->data _is_ aligned. A NOP attribute, plus 1520089bf1a6SNicolas Dichtel * nlattr header for next attribute, will make nla_data() 1521089bf1a6SNicolas Dichtel * 8-byte aligned. 1522089bf1a6SNicolas Dichtel */ 1523089bf1a6SNicolas Dichtel if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8)) 1524089bf1a6SNicolas Dichtel return true; 1525089bf1a6SNicolas Dichtel #endif 1526089bf1a6SNicolas Dichtel return false; 1527089bf1a6SNicolas Dichtel } 1528089bf1a6SNicolas Dichtel 1529089bf1a6SNicolas Dichtel /** 153035c58459SDavid S. Miller * nla_align_64bit - 64-bit align the nla_data() of next attribute 153135c58459SDavid S. Miller * @skb: socket buffer the message is stored in 153235c58459SDavid S. Miller * @padattr: attribute type for the padding 153335c58459SDavid S. Miller * 153435c58459SDavid S. Miller * Conditionally emit a padding netlink attribute in order to make 153535c58459SDavid S. Miller * the next attribute we emit have a 64-bit aligned nla_data() area. 153635c58459SDavid S. Miller * This will only be done in architectures which do not have 1537cca1d815SEric Dumazet * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined. 153835c58459SDavid S. Miller * 153935c58459SDavid S. Miller * Returns zero on success or a negative error code. 154035c58459SDavid S. Miller */ 154135c58459SDavid S. Miller static inline int nla_align_64bit(struct sk_buff *skb, int padattr) 154235c58459SDavid S. Miller { 1543089bf1a6SNicolas Dichtel if (nla_need_padding_for_64bit(skb) && 1544cca1d815SEric Dumazet !nla_reserve(skb, padattr, 0)) 154535c58459SDavid S. Miller return -EMSGSIZE; 1546089bf1a6SNicolas Dichtel 154735c58459SDavid S. Miller return 0; 154835c58459SDavid S. Miller } 154935c58459SDavid S. Miller 155035c58459SDavid S. Miller /** 155135c58459SDavid S. Miller * nla_total_size_64bit - total length of attribute including padding 155235c58459SDavid S. Miller * @payload: length of payload 155335c58459SDavid S. Miller */ 155435c58459SDavid S. Miller static inline int nla_total_size_64bit(int payload) 155535c58459SDavid S. Miller { 155635c58459SDavid S. Miller return NLA_ALIGN(nla_attr_size(payload)) 1557cca1d815SEric Dumazet #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 155835c58459SDavid S. Miller + NLA_ALIGN(nla_attr_size(0)) 155935c58459SDavid S. Miller #endif 156035c58459SDavid S. Miller ; 156135c58459SDavid S. Miller } 156235c58459SDavid S. Miller 156335c58459SDavid S. Miller /** 1564bfa83a9eSThomas Graf * nla_for_each_attr - iterate over a stream of attributes 1565bfa83a9eSThomas Graf * @pos: loop counter, set to current attribute 1566bfa83a9eSThomas Graf * @head: head of attribute stream 1567bfa83a9eSThomas Graf * @len: length of attribute stream 1568bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 1569bfa83a9eSThomas Graf */ 1570bfa83a9eSThomas Graf #define nla_for_each_attr(pos, head, len, rem) \ 1571bfa83a9eSThomas Graf for (pos = head, rem = len; \ 1572bfa83a9eSThomas Graf nla_ok(pos, rem); \ 1573bfa83a9eSThomas Graf pos = nla_next(pos, &(rem))) 1574bfa83a9eSThomas Graf 1575fe4944e5SThomas Graf /** 1576fe4944e5SThomas Graf * nla_for_each_nested - iterate over nested attributes 1577fe4944e5SThomas Graf * @pos: loop counter, set to current attribute 1578fe4944e5SThomas Graf * @nla: attribute containing the nested attributes 1579fe4944e5SThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 1580fe4944e5SThomas Graf */ 1581fe4944e5SThomas Graf #define nla_for_each_nested(pos, nla, rem) \ 1582fe4944e5SThomas Graf nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem) 1583fe4944e5SThomas Graf 1584941d8ebcSSimon Horman /** 1585941d8ebcSSimon Horman * nla_is_last - Test if attribute is last in stream 1586941d8ebcSSimon Horman * @nla: attribute to test 1587941d8ebcSSimon Horman * @rem: bytes remaining in stream 1588941d8ebcSSimon Horman */ 1589941d8ebcSSimon Horman static inline bool nla_is_last(const struct nlattr *nla, int rem) 1590941d8ebcSSimon Horman { 1591941d8ebcSSimon Horman return nla->nla_len == rem; 1592941d8ebcSSimon Horman } 1593941d8ebcSSimon Horman 1594bfa83a9eSThomas Graf #endif 1595