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 156bfa83a9eSThomas Graf * nla_parse_nested() parse nested attribuets 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, 175a5531a5dSThomas Graf NLA_NUL_STRING, 176d30045a0SJohannes Berg NLA_BINARY, 1774778e0beSJiri Pirko NLA_S8, 1784778e0beSJiri Pirko NLA_S16, 1794778e0beSJiri Pirko NLA_S32, 1804778e0beSJiri Pirko NLA_S64, 18164c83d83SJamal Hadi Salim NLA_BITFIELD32, 182568b742aSJohannes Berg NLA_REJECT, 183b60b87fcSJohannes Berg NLA_EXACT_LEN, 184b60b87fcSJohannes Berg NLA_EXACT_LEN_WARN, 185bfa83a9eSThomas Graf __NLA_TYPE_MAX, 186bfa83a9eSThomas Graf }; 187bfa83a9eSThomas Graf 188bfa83a9eSThomas Graf #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1) 189bfa83a9eSThomas Graf 190bfa83a9eSThomas Graf /** 191bfa83a9eSThomas Graf * struct nla_policy - attribute validation policy 192bfa83a9eSThomas Graf * @type: Type of attribute or NLA_UNSPEC 193a5531a5dSThomas Graf * @len: Type specific length of payload 194bfa83a9eSThomas Graf * 195bfa83a9eSThomas Graf * Policies are defined as arrays of this struct, the array must be 196bfa83a9eSThomas Graf * accessible by attribute type up to the highest identifier to be expected. 197bfa83a9eSThomas Graf * 198a5531a5dSThomas Graf * Meaning of `len' field: 199a5531a5dSThomas Graf * NLA_STRING Maximum length of string 200a5531a5dSThomas Graf * NLA_NUL_STRING Maximum length of string (excluding NUL) 201a5531a5dSThomas Graf * NLA_FLAG Unused 202d30045a0SJohannes Berg * NLA_BINARY Maximum length of attribute payload 2034b6cc728SJohannes Berg * NLA_NESTED Don't use `len' field -- length verification is 2044b6cc728SJohannes Berg * done by checking len of nested header (or empty) 2054b6cc728SJohannes Berg * NLA_U8, NLA_U16, 2064b6cc728SJohannes Berg * NLA_U32, NLA_U64, 2074778e0beSJiri Pirko * NLA_S8, NLA_S16, 2084778e0beSJiri Pirko * NLA_S32, NLA_S64, 2094b6cc728SJohannes Berg * NLA_MSECS Leaving the length field zero will verify the 2104b6cc728SJohannes Berg * given type fits, using it verifies minimum length 2114b6cc728SJohannes Berg * just like "All other" 212568b742aSJohannes Berg * NLA_BITFIELD32 Unused 213568b742aSJohannes Berg * NLA_REJECT Unused 214b60b87fcSJohannes Berg * NLA_EXACT_LEN Attribute must have exactly this length, otherwise 215b60b87fcSJohannes Berg * it is rejected. 216b60b87fcSJohannes Berg * NLA_EXACT_LEN_WARN Attribute should have exactly this length, a warning 217b60b87fcSJohannes Berg * is logged if it is longer, shorter is rejected. 2184b6cc728SJohannes Berg * All other Minimum length of attribute payload 219a5531a5dSThomas Graf * 220568b742aSJohannes Berg * Meaning of `validation_data' field: 221568b742aSJohannes Berg * NLA_BITFIELD32 This is a 32-bit bitmap/bitselector attribute and 222568b742aSJohannes Berg * validation data must point to a u32 value of valid 223568b742aSJohannes Berg * flags 224568b742aSJohannes Berg * NLA_REJECT This attribute is always rejected and validation data 225568b742aSJohannes Berg * may point to a string to report as the error instead 226568b742aSJohannes Berg * of the generic one in extended ACK. 227568b742aSJohannes Berg * All other Unused 228568b742aSJohannes Berg * 229bfa83a9eSThomas Graf * Example: 230b54452b0SAlexey Dobriyan * static const struct nla_policy my_policy[ATTR_MAX+1] = { 231bfa83a9eSThomas Graf * [ATTR_FOO] = { .type = NLA_U16 }, 232d30045a0SJohannes Berg * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ }, 233a5531a5dSThomas Graf * [ATTR_BAZ] = { .len = sizeof(struct mystruct) }, 23464c83d83SJamal Hadi Salim * [ATTR_GOO] = { .type = NLA_BITFIELD32, .validation_data = &myvalidflags }, 235bfa83a9eSThomas Graf * }; 236bfa83a9eSThomas Graf */ 237bfa83a9eSThomas Graf struct nla_policy { 238bfa83a9eSThomas Graf u16 type; 239a5531a5dSThomas Graf u16 len; 24048fde90aSJohannes Berg const void *validation_data; 241bfa83a9eSThomas Graf }; 242bfa83a9eSThomas Graf 243b60b87fcSJohannes Berg #define NLA_POLICY_EXACT_LEN(_len) { .type = NLA_EXACT_LEN, .len = _len } 244b60b87fcSJohannes Berg #define NLA_POLICY_EXACT_LEN_WARN(_len) { .type = NLA_EXACT_LEN_WARN, \ 245b60b87fcSJohannes Berg .len = _len } 246b60b87fcSJohannes Berg 247b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR NLA_POLICY_EXACT_LEN(ETH_ALEN) 248b60b87fcSJohannes Berg #define NLA_POLICY_ETH_ADDR_COMPAT NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN) 249b60b87fcSJohannes Berg 2504e902c57SThomas Graf /** 2514e902c57SThomas Graf * struct nl_info - netlink source information 2524e902c57SThomas Graf * @nlh: Netlink message header of original request 25315e47304SEric W. Biederman * @portid: Netlink PORTID of requesting application 2544e902c57SThomas Graf */ 2554e902c57SThomas Graf struct nl_info { 2564e902c57SThomas Graf struct nlmsghdr *nlh; 2574d1169c1SDenis V. Lunev struct net *nl_net; 25815e47304SEric W. Biederman u32 portid; 2593b1137feSDavid Ahern bool skip_notify; 2604e902c57SThomas Graf }; 2614e902c57SThomas Graf 2624f69053bSJoe Perches int netlink_rcv_skb(struct sk_buff *skb, 2632d4bc933SJohannes Berg int (*cb)(struct sk_buff *, struct nlmsghdr *, 2642d4bc933SJohannes Berg struct netlink_ext_ack *)); 2654f69053bSJoe Perches int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid, 2664f69053bSJoe Perches unsigned int group, int report, gfp_t flags); 26782ace47aSThomas Graf 2684f69053bSJoe Perches int nla_validate(const struct nlattr *head, int len, int maxtype, 269fceb6435SJohannes Berg const struct nla_policy *policy, 270fceb6435SJohannes Berg struct netlink_ext_ack *extack); 2714f69053bSJoe Perches int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, 272fceb6435SJohannes Berg int len, const struct nla_policy *policy, 273fceb6435SJohannes Berg struct netlink_ext_ack *extack); 2744f69053bSJoe Perches int nla_policy_len(const struct nla_policy *, int); 2754f69053bSJoe Perches struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype); 2764f69053bSJoe Perches size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize); 2772cf0c8b3SPhil Sutter char *nla_strdup(const struct nlattr *nla, gfp_t flags); 2784f69053bSJoe Perches int nla_memcpy(void *dest, const struct nlattr *src, int count); 2794f69053bSJoe Perches int nla_memcmp(const struct nlattr *nla, const void *data, size_t size); 2804f69053bSJoe Perches int nla_strcmp(const struct nlattr *nla, const char *str); 2814f69053bSJoe Perches struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 282089bf1a6SNicolas Dichtel struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype, 283089bf1a6SNicolas Dichtel int attrlen, int padattr); 2844f69053bSJoe Perches void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 2854f69053bSJoe Perches struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 286089bf1a6SNicolas Dichtel struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, 287089bf1a6SNicolas Dichtel int attrlen, int padattr); 2884f69053bSJoe Perches void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 2894f69053bSJoe Perches void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, 290fe4944e5SThomas Graf const void *data); 291089bf1a6SNicolas Dichtel void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 292089bf1a6SNicolas Dichtel const void *data, int padattr); 2934f69053bSJoe Perches void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 2944f69053bSJoe Perches int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data); 295089bf1a6SNicolas Dichtel int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 296089bf1a6SNicolas Dichtel const void *data, int padattr); 2974f69053bSJoe Perches int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 2984f69053bSJoe Perches int nla_append(struct sk_buff *skb, int attrlen, const void *data); 299bfa83a9eSThomas Graf 300bfa83a9eSThomas Graf /************************************************************************** 301bfa83a9eSThomas Graf * Netlink Messages 302bfa83a9eSThomas Graf **************************************************************************/ 303bfa83a9eSThomas Graf 304bfa83a9eSThomas Graf /** 305bfa83a9eSThomas Graf * nlmsg_msg_size - length of netlink message not including padding 306bfa83a9eSThomas Graf * @payload: length of message payload 307bfa83a9eSThomas Graf */ 308bfa83a9eSThomas Graf static inline int nlmsg_msg_size(int payload) 309bfa83a9eSThomas Graf { 310bfa83a9eSThomas Graf return NLMSG_HDRLEN + payload; 311bfa83a9eSThomas Graf } 312bfa83a9eSThomas Graf 313bfa83a9eSThomas Graf /** 314bfa83a9eSThomas Graf * nlmsg_total_size - length of netlink message including padding 315bfa83a9eSThomas Graf * @payload: length of message payload 316bfa83a9eSThomas Graf */ 317bfa83a9eSThomas Graf static inline int nlmsg_total_size(int payload) 318bfa83a9eSThomas Graf { 319bfa83a9eSThomas Graf return NLMSG_ALIGN(nlmsg_msg_size(payload)); 320bfa83a9eSThomas Graf } 321bfa83a9eSThomas Graf 322bfa83a9eSThomas Graf /** 323bfa83a9eSThomas Graf * nlmsg_padlen - length of padding at the message's tail 324bfa83a9eSThomas Graf * @payload: length of message payload 325bfa83a9eSThomas Graf */ 326bfa83a9eSThomas Graf static inline int nlmsg_padlen(int payload) 327bfa83a9eSThomas Graf { 328bfa83a9eSThomas Graf return nlmsg_total_size(payload) - nlmsg_msg_size(payload); 329bfa83a9eSThomas Graf } 330bfa83a9eSThomas Graf 331bfa83a9eSThomas Graf /** 332bfa83a9eSThomas Graf * nlmsg_data - head of message payload 33370f23fd6SJustin P. Mattock * @nlh: netlink message header 334bfa83a9eSThomas Graf */ 335bfa83a9eSThomas Graf static inline void *nlmsg_data(const struct nlmsghdr *nlh) 336bfa83a9eSThomas Graf { 337bfa83a9eSThomas Graf return (unsigned char *) nlh + NLMSG_HDRLEN; 338bfa83a9eSThomas Graf } 339bfa83a9eSThomas Graf 340bfa83a9eSThomas Graf /** 341bfa83a9eSThomas Graf * nlmsg_len - length of message payload 342bfa83a9eSThomas Graf * @nlh: netlink message header 343bfa83a9eSThomas Graf */ 344bfa83a9eSThomas Graf static inline int nlmsg_len(const struct nlmsghdr *nlh) 345bfa83a9eSThomas Graf { 346bfa83a9eSThomas Graf return nlh->nlmsg_len - NLMSG_HDRLEN; 347bfa83a9eSThomas Graf } 348bfa83a9eSThomas Graf 349bfa83a9eSThomas Graf /** 350bfa83a9eSThomas Graf * nlmsg_attrdata - head of attributes data 351bfa83a9eSThomas Graf * @nlh: netlink message header 352bfa83a9eSThomas Graf * @hdrlen: length of family specific header 353bfa83a9eSThomas Graf */ 354bfa83a9eSThomas Graf static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, 355bfa83a9eSThomas Graf int hdrlen) 356bfa83a9eSThomas Graf { 357bfa83a9eSThomas Graf unsigned char *data = nlmsg_data(nlh); 358bfa83a9eSThomas Graf return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen)); 359bfa83a9eSThomas Graf } 360bfa83a9eSThomas Graf 361bfa83a9eSThomas Graf /** 362bfa83a9eSThomas Graf * nlmsg_attrlen - length of attributes data 363bfa83a9eSThomas Graf * @nlh: netlink message header 364bfa83a9eSThomas Graf * @hdrlen: length of family specific header 365bfa83a9eSThomas Graf */ 366bfa83a9eSThomas Graf static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen) 367bfa83a9eSThomas Graf { 368bfa83a9eSThomas Graf return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen); 369bfa83a9eSThomas Graf } 370bfa83a9eSThomas Graf 371bfa83a9eSThomas Graf /** 372bfa83a9eSThomas Graf * nlmsg_ok - check if the netlink message fits into the remaining bytes 373bfa83a9eSThomas Graf * @nlh: netlink message header 374bfa83a9eSThomas Graf * @remaining: number of bytes remaining in message stream 375bfa83a9eSThomas Graf */ 376bfa83a9eSThomas Graf static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining) 377bfa83a9eSThomas Graf { 378619e803dSVegard Nossum return (remaining >= (int) sizeof(struct nlmsghdr) && 379bfa83a9eSThomas Graf nlh->nlmsg_len >= sizeof(struct nlmsghdr) && 380bfa83a9eSThomas Graf nlh->nlmsg_len <= remaining); 381bfa83a9eSThomas Graf } 382bfa83a9eSThomas Graf 383bfa83a9eSThomas Graf /** 384bfa83a9eSThomas Graf * nlmsg_next - next netlink message in message stream 385bfa83a9eSThomas Graf * @nlh: netlink message header 386bfa83a9eSThomas Graf * @remaining: number of bytes remaining in message stream 387bfa83a9eSThomas Graf * 388bfa83a9eSThomas Graf * Returns the next netlink message in the message stream and 389bfa83a9eSThomas Graf * decrements remaining by the size of the current message. 390bfa83a9eSThomas Graf */ 3913654654fSJan Engelhardt static inline struct nlmsghdr * 3923654654fSJan Engelhardt nlmsg_next(const struct nlmsghdr *nlh, int *remaining) 393bfa83a9eSThomas Graf { 394bfa83a9eSThomas Graf int totlen = NLMSG_ALIGN(nlh->nlmsg_len); 395bfa83a9eSThomas Graf 396bfa83a9eSThomas Graf *remaining -= totlen; 397bfa83a9eSThomas Graf 398bfa83a9eSThomas Graf return (struct nlmsghdr *) ((unsigned char *) nlh + totlen); 399bfa83a9eSThomas Graf } 400bfa83a9eSThomas Graf 401bfa83a9eSThomas Graf /** 402bfa83a9eSThomas Graf * nlmsg_parse - parse attributes of a netlink message 403bfa83a9eSThomas Graf * @nlh: netlink message header 404bfa83a9eSThomas Graf * @hdrlen: length of family specific header 405bfa83a9eSThomas Graf * @tb: destination array with maxtype+1 elements 406bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 407bfa83a9eSThomas Graf * @policy: validation policy 408fceb6435SJohannes Berg * @extack: extended ACK report struct 409bfa83a9eSThomas Graf * 410bfa83a9eSThomas Graf * See nla_parse() 411bfa83a9eSThomas Graf */ 4123a6c2b41SPatrick McHardy static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen, 413bfa83a9eSThomas Graf struct nlattr *tb[], int maxtype, 414fceb6435SJohannes Berg const struct nla_policy *policy, 415fceb6435SJohannes Berg struct netlink_ext_ack *extack) 416bfa83a9eSThomas Graf { 417bfa83a9eSThomas Graf if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) 418bfa83a9eSThomas Graf return -EINVAL; 419bfa83a9eSThomas Graf 420bfa83a9eSThomas Graf return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), 421fceb6435SJohannes Berg nlmsg_attrlen(nlh, hdrlen), policy, extack); 422bfa83a9eSThomas Graf } 423bfa83a9eSThomas Graf 424bfa83a9eSThomas Graf /** 425bfa83a9eSThomas Graf * nlmsg_find_attr - find a specific attribute in a netlink message 426bfa83a9eSThomas Graf * @nlh: netlink message header 427bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 428bfa83a9eSThomas Graf * @attrtype: type of attribute to look for 429bfa83a9eSThomas Graf * 430bfa83a9eSThomas Graf * Returns the first attribute which matches the specified type. 431bfa83a9eSThomas Graf */ 4326b8c92baSNelson Elhage static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh, 433bfa83a9eSThomas Graf int hdrlen, int attrtype) 434bfa83a9eSThomas Graf { 435bfa83a9eSThomas Graf return nla_find(nlmsg_attrdata(nlh, hdrlen), 436bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), attrtype); 437bfa83a9eSThomas Graf } 438bfa83a9eSThomas Graf 439bfa83a9eSThomas Graf /** 440bfa83a9eSThomas Graf * nlmsg_validate - validate a netlink message including attributes 441bfa83a9eSThomas Graf * @nlh: netlinket message header 442bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 443bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 444bfa83a9eSThomas Graf * @policy: validation policy 445fceb6435SJohannes Berg * @extack: extended ACK report struct 446bfa83a9eSThomas Graf */ 4473654654fSJan Engelhardt static inline int nlmsg_validate(const struct nlmsghdr *nlh, 4483654654fSJan Engelhardt int hdrlen, int maxtype, 449fceb6435SJohannes Berg const struct nla_policy *policy, 450fceb6435SJohannes Berg struct netlink_ext_ack *extack) 451bfa83a9eSThomas Graf { 452bfa83a9eSThomas Graf if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) 453bfa83a9eSThomas Graf return -EINVAL; 454bfa83a9eSThomas Graf 455bfa83a9eSThomas Graf return nla_validate(nlmsg_attrdata(nlh, hdrlen), 456fceb6435SJohannes Berg nlmsg_attrlen(nlh, hdrlen), maxtype, policy, 457fceb6435SJohannes Berg extack); 458bfa83a9eSThomas Graf } 459bfa83a9eSThomas Graf 460bfa83a9eSThomas Graf /** 46197676b6bSThomas Graf * nlmsg_report - need to report back to application? 46297676b6bSThomas Graf * @nlh: netlink message header 46397676b6bSThomas Graf * 46497676b6bSThomas Graf * Returns 1 if a report back to the application is requested. 46597676b6bSThomas Graf */ 4663a6c2b41SPatrick McHardy static inline int nlmsg_report(const struct nlmsghdr *nlh) 46797676b6bSThomas Graf { 46897676b6bSThomas Graf return !!(nlh->nlmsg_flags & NLM_F_ECHO); 46997676b6bSThomas Graf } 47097676b6bSThomas Graf 47197676b6bSThomas Graf /** 472bfa83a9eSThomas Graf * nlmsg_for_each_attr - iterate over a stream of attributes 473bfa83a9eSThomas Graf * @pos: loop counter, set to current attribute 474bfa83a9eSThomas Graf * @nlh: netlink message header 475bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 476bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 477bfa83a9eSThomas Graf */ 478bfa83a9eSThomas Graf #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \ 479bfa83a9eSThomas Graf nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \ 480bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), rem) 481bfa83a9eSThomas Graf 482bfa83a9eSThomas Graf /** 483bfa83a9eSThomas Graf * nlmsg_put - Add a new netlink message to an skb 484bfa83a9eSThomas Graf * @skb: socket buffer to store message in 4852c6ba4b1SNicolas Dichtel * @portid: netlink PORTID of requesting application 486bfa83a9eSThomas Graf * @seq: sequence number of message 487bfa83a9eSThomas Graf * @type: message type 488bfa83a9eSThomas Graf * @payload: length of message payload 489bfa83a9eSThomas Graf * @flags: message flags 490bfa83a9eSThomas Graf * 491bfa83a9eSThomas Graf * Returns NULL if the tailroom of the skb is insufficient to store 492bfa83a9eSThomas Graf * the message header and payload. 493bfa83a9eSThomas Graf */ 49415e47304SEric W. Biederman static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, 495bfa83a9eSThomas Graf int type, int payload, int flags) 496bfa83a9eSThomas Graf { 497bfa83a9eSThomas Graf if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload))) 498bfa83a9eSThomas Graf return NULL; 499bfa83a9eSThomas Graf 50015e47304SEric W. Biederman return __nlmsg_put(skb, portid, seq, type, payload, flags); 501bfa83a9eSThomas Graf } 502bfa83a9eSThomas Graf 503bfa83a9eSThomas Graf /** 504bfa83a9eSThomas Graf * nlmsg_put_answer - Add a new callback based netlink message to an skb 505bfa83a9eSThomas Graf * @skb: socket buffer to store message in 506bfa83a9eSThomas Graf * @cb: netlink callback 507bfa83a9eSThomas Graf * @type: message type 508bfa83a9eSThomas Graf * @payload: length of message payload 509bfa83a9eSThomas Graf * @flags: message flags 510bfa83a9eSThomas Graf * 511bfa83a9eSThomas Graf * Returns NULL if the tailroom of the skb is insufficient to store 512bfa83a9eSThomas Graf * the message header and payload. 513bfa83a9eSThomas Graf */ 514bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb, 515bfa83a9eSThomas Graf struct netlink_callback *cb, 516bfa83a9eSThomas Graf int type, int payload, 517bfa83a9eSThomas Graf int flags) 518bfa83a9eSThomas Graf { 51915e47304SEric W. Biederman return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 520bfa83a9eSThomas Graf type, payload, flags); 521bfa83a9eSThomas Graf } 522bfa83a9eSThomas Graf 523bfa83a9eSThomas Graf /** 524bfa83a9eSThomas Graf * nlmsg_new - Allocate a new netlink message 525339bf98fSThomas Graf * @payload: size of the message payload 526fe4944e5SThomas Graf * @flags: the type of memory to allocate. 527bfa83a9eSThomas Graf * 528339bf98fSThomas Graf * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known 529339bf98fSThomas Graf * and a good default is needed. 530bfa83a9eSThomas Graf */ 531339bf98fSThomas Graf static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags) 532bfa83a9eSThomas Graf { 533339bf98fSThomas Graf return alloc_skb(nlmsg_total_size(payload), flags); 534bfa83a9eSThomas Graf } 535bfa83a9eSThomas Graf 536bfa83a9eSThomas Graf /** 537bfa83a9eSThomas Graf * nlmsg_end - Finalize a netlink message 538bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 539bfa83a9eSThomas Graf * @nlh: netlink message header 540bfa83a9eSThomas Graf * 541bfa83a9eSThomas Graf * Corrects the netlink message header to include the appeneded 542bfa83a9eSThomas Graf * attributes. Only necessary if attributes have been added to 543bfa83a9eSThomas Graf * the message. 544bfa83a9eSThomas Graf */ 545053c095aSJohannes Berg static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh) 546bfa83a9eSThomas Graf { 54727a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh; 548bfa83a9eSThomas Graf } 549bfa83a9eSThomas Graf 550bfa83a9eSThomas Graf /** 551fe4944e5SThomas Graf * nlmsg_get_pos - return current position in netlink message 552fe4944e5SThomas Graf * @skb: socket buffer the message is stored in 553fe4944e5SThomas Graf * 554fe4944e5SThomas Graf * Returns a pointer to the current tail of the message. 555fe4944e5SThomas Graf */ 556fe4944e5SThomas Graf static inline void *nlmsg_get_pos(struct sk_buff *skb) 557fe4944e5SThomas Graf { 55827a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 559fe4944e5SThomas Graf } 560fe4944e5SThomas Graf 561fe4944e5SThomas Graf /** 562fe4944e5SThomas Graf * nlmsg_trim - Trim message to a mark 563fe4944e5SThomas Graf * @skb: socket buffer the message is stored in 564fe4944e5SThomas Graf * @mark: mark to trim to 565fe4944e5SThomas Graf * 566bc3ed28cSThomas Graf * Trims the message to the provided mark. 567fe4944e5SThomas Graf */ 568bc3ed28cSThomas Graf static inline void nlmsg_trim(struct sk_buff *skb, const void *mark) 569fe4944e5SThomas Graf { 570149118d8SThomas Graf if (mark) { 571149118d8SThomas Graf WARN_ON((unsigned char *) mark < skb->data); 572fe4944e5SThomas Graf skb_trim(skb, (unsigned char *) mark - skb->data); 573fe4944e5SThomas Graf } 574149118d8SThomas Graf } 575fe4944e5SThomas Graf 576fe4944e5SThomas Graf /** 577bfa83a9eSThomas Graf * nlmsg_cancel - Cancel construction of a netlink message 578bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 579bfa83a9eSThomas Graf * @nlh: netlink message header 580bfa83a9eSThomas Graf * 581bfa83a9eSThomas Graf * Removes the complete netlink message including all 582bc3ed28cSThomas Graf * attributes from the socket buffer again. 583bfa83a9eSThomas Graf */ 584bc3ed28cSThomas Graf static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh) 585bfa83a9eSThomas Graf { 586bc3ed28cSThomas Graf nlmsg_trim(skb, nlh); 587bfa83a9eSThomas Graf } 588bfa83a9eSThomas Graf 589bfa83a9eSThomas Graf /** 590bfa83a9eSThomas Graf * nlmsg_free - free a netlink message 591bfa83a9eSThomas Graf * @skb: socket buffer of netlink message 592bfa83a9eSThomas Graf */ 593bfa83a9eSThomas Graf static inline void nlmsg_free(struct sk_buff *skb) 594bfa83a9eSThomas Graf { 595bfa83a9eSThomas Graf kfree_skb(skb); 596bfa83a9eSThomas Graf } 597bfa83a9eSThomas Graf 598bfa83a9eSThomas Graf /** 599bfa83a9eSThomas Graf * nlmsg_multicast - multicast a netlink message 600bfa83a9eSThomas Graf * @sk: netlink socket to spread messages to 601bfa83a9eSThomas Graf * @skb: netlink message as socket buffer 60215e47304SEric W. Biederman * @portid: own netlink portid to avoid sending to yourself 603bfa83a9eSThomas Graf * @group: multicast group id 604d387f6adSThomas Graf * @flags: allocation flags 605bfa83a9eSThomas Graf */ 606bfa83a9eSThomas Graf static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, 60715e47304SEric W. Biederman u32 portid, unsigned int group, gfp_t flags) 608bfa83a9eSThomas Graf { 609bfa83a9eSThomas Graf int err; 610bfa83a9eSThomas Graf 611bfa83a9eSThomas Graf NETLINK_CB(skb).dst_group = group; 612bfa83a9eSThomas Graf 61315e47304SEric W. Biederman err = netlink_broadcast(sk, skb, portid, group, flags); 614bfa83a9eSThomas Graf if (err > 0) 615bfa83a9eSThomas Graf err = 0; 616bfa83a9eSThomas Graf 617bfa83a9eSThomas Graf return err; 618bfa83a9eSThomas Graf } 619bfa83a9eSThomas Graf 620bfa83a9eSThomas Graf /** 621bfa83a9eSThomas Graf * nlmsg_unicast - unicast a netlink message 622bfa83a9eSThomas Graf * @sk: netlink socket to spread message to 623bfa83a9eSThomas Graf * @skb: netlink message as socket buffer 62415e47304SEric W. Biederman * @portid: netlink portid of the destination socket 625bfa83a9eSThomas Graf */ 62615e47304SEric W. Biederman static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid) 627bfa83a9eSThomas Graf { 628bfa83a9eSThomas Graf int err; 629bfa83a9eSThomas Graf 63015e47304SEric W. Biederman err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT); 631bfa83a9eSThomas Graf if (err > 0) 632bfa83a9eSThomas Graf err = 0; 633bfa83a9eSThomas Graf 634bfa83a9eSThomas Graf return err; 635bfa83a9eSThomas Graf } 636bfa83a9eSThomas Graf 637bfa83a9eSThomas Graf /** 638bfa83a9eSThomas Graf * nlmsg_for_each_msg - iterate over a stream of messages 639bfa83a9eSThomas Graf * @pos: loop counter, set to current message 640bfa83a9eSThomas Graf * @head: head of message stream 641bfa83a9eSThomas Graf * @len: length of message stream 642bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 643bfa83a9eSThomas Graf */ 644bfa83a9eSThomas Graf #define nlmsg_for_each_msg(pos, head, len, rem) \ 645bfa83a9eSThomas Graf for (pos = head, rem = len; \ 646bfa83a9eSThomas Graf nlmsg_ok(pos, rem); \ 647bfa83a9eSThomas Graf pos = nlmsg_next(pos, &(rem))) 648bfa83a9eSThomas Graf 649670dc283SJohannes Berg /** 650670dc283SJohannes Berg * nl_dump_check_consistent - check if sequence is consistent and advertise if not 651670dc283SJohannes Berg * @cb: netlink callback structure that stores the sequence number 652670dc283SJohannes Berg * @nlh: netlink message header to write the flag to 653670dc283SJohannes Berg * 654670dc283SJohannes Berg * This function checks if the sequence (generation) number changed during dump 655670dc283SJohannes Berg * and if it did, advertises it in the netlink message header. 656670dc283SJohannes Berg * 657670dc283SJohannes Berg * The correct way to use it is to set cb->seq to the generation counter when 658670dc283SJohannes Berg * all locks for dumping have been acquired, and then call this function for 659670dc283SJohannes Berg * each message that is generated. 660670dc283SJohannes Berg * 661670dc283SJohannes Berg * Note that due to initialisation concerns, 0 is an invalid sequence number 662670dc283SJohannes Berg * and must not be used by code that uses this functionality. 663670dc283SJohannes Berg */ 664670dc283SJohannes Berg static inline void 665670dc283SJohannes Berg nl_dump_check_consistent(struct netlink_callback *cb, 666670dc283SJohannes Berg struct nlmsghdr *nlh) 667670dc283SJohannes Berg { 668670dc283SJohannes Berg if (cb->prev_seq && cb->seq != cb->prev_seq) 669670dc283SJohannes Berg nlh->nlmsg_flags |= NLM_F_DUMP_INTR; 670670dc283SJohannes Berg cb->prev_seq = cb->seq; 671670dc283SJohannes Berg } 672670dc283SJohannes Berg 673bfa83a9eSThomas Graf /************************************************************************** 674bfa83a9eSThomas Graf * Netlink Attributes 675bfa83a9eSThomas Graf **************************************************************************/ 676bfa83a9eSThomas Graf 677bfa83a9eSThomas Graf /** 678bfa83a9eSThomas Graf * nla_attr_size - length of attribute not including padding 679bfa83a9eSThomas Graf * @payload: length of payload 680bfa83a9eSThomas Graf */ 681bfa83a9eSThomas Graf static inline int nla_attr_size(int payload) 682bfa83a9eSThomas Graf { 683bfa83a9eSThomas Graf return NLA_HDRLEN + payload; 684bfa83a9eSThomas Graf } 685bfa83a9eSThomas Graf 686bfa83a9eSThomas Graf /** 687bfa83a9eSThomas Graf * nla_total_size - total length of attribute including padding 688bfa83a9eSThomas Graf * @payload: length of payload 689bfa83a9eSThomas Graf */ 690bfa83a9eSThomas Graf static inline int nla_total_size(int payload) 691bfa83a9eSThomas Graf { 692bfa83a9eSThomas Graf return NLA_ALIGN(nla_attr_size(payload)); 693bfa83a9eSThomas Graf } 694bfa83a9eSThomas Graf 695bfa83a9eSThomas Graf /** 696bfa83a9eSThomas Graf * nla_padlen - length of padding at the tail of attribute 697bfa83a9eSThomas Graf * @payload: length of payload 698bfa83a9eSThomas Graf */ 699bfa83a9eSThomas Graf static inline int nla_padlen(int payload) 700bfa83a9eSThomas Graf { 701bfa83a9eSThomas Graf return nla_total_size(payload) - nla_attr_size(payload); 702bfa83a9eSThomas Graf } 703bfa83a9eSThomas Graf 704bfa83a9eSThomas Graf /** 7058f4c1f9bSThomas Graf * nla_type - attribute type 7068f4c1f9bSThomas Graf * @nla: netlink attribute 7078f4c1f9bSThomas Graf */ 7088f4c1f9bSThomas Graf static inline int nla_type(const struct nlattr *nla) 7098f4c1f9bSThomas Graf { 7108f4c1f9bSThomas Graf return nla->nla_type & NLA_TYPE_MASK; 7118f4c1f9bSThomas Graf } 7128f4c1f9bSThomas Graf 7138f4c1f9bSThomas Graf /** 714bfa83a9eSThomas Graf * nla_data - head of payload 715bfa83a9eSThomas Graf * @nla: netlink attribute 716bfa83a9eSThomas Graf */ 717bfa83a9eSThomas Graf static inline void *nla_data(const struct nlattr *nla) 718bfa83a9eSThomas Graf { 719bfa83a9eSThomas Graf return (char *) nla + NLA_HDRLEN; 720bfa83a9eSThomas Graf } 721bfa83a9eSThomas Graf 722bfa83a9eSThomas Graf /** 723bfa83a9eSThomas Graf * nla_len - length of payload 724bfa83a9eSThomas Graf * @nla: netlink attribute 725bfa83a9eSThomas Graf */ 726bfa83a9eSThomas Graf static inline int nla_len(const struct nlattr *nla) 727bfa83a9eSThomas Graf { 728bfa83a9eSThomas Graf return nla->nla_len - NLA_HDRLEN; 729bfa83a9eSThomas Graf } 730bfa83a9eSThomas Graf 731bfa83a9eSThomas Graf /** 732bfa83a9eSThomas Graf * nla_ok - check if the netlink attribute fits into the remaining bytes 733bfa83a9eSThomas Graf * @nla: netlink attribute 734bfa83a9eSThomas Graf * @remaining: number of bytes remaining in attribute stream 735bfa83a9eSThomas Graf */ 736bfa83a9eSThomas Graf static inline int nla_ok(const struct nlattr *nla, int remaining) 737bfa83a9eSThomas Graf { 7383e1ed981SAlexey Dobriyan return remaining >= (int) sizeof(*nla) && 7393e1ed981SAlexey Dobriyan nla->nla_len >= sizeof(*nla) && 740bfa83a9eSThomas Graf nla->nla_len <= remaining; 741bfa83a9eSThomas Graf } 742bfa83a9eSThomas Graf 743bfa83a9eSThomas Graf /** 744d1ec3b77SPierre Ynard * nla_next - next netlink attribute in attribute stream 745bfa83a9eSThomas Graf * @nla: netlink attribute 746bfa83a9eSThomas Graf * @remaining: number of bytes remaining in attribute stream 747bfa83a9eSThomas Graf * 748bfa83a9eSThomas Graf * Returns the next netlink attribute in the attribute stream and 749bfa83a9eSThomas Graf * decrements remaining by the size of the current attribute. 750bfa83a9eSThomas Graf */ 751bfa83a9eSThomas Graf static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining) 752bfa83a9eSThomas Graf { 7533b2c75d3SAlexey Dobriyan unsigned int totlen = NLA_ALIGN(nla->nla_len); 754bfa83a9eSThomas Graf 755bfa83a9eSThomas Graf *remaining -= totlen; 756bfa83a9eSThomas Graf return (struct nlattr *) ((char *) nla + totlen); 757bfa83a9eSThomas Graf } 758bfa83a9eSThomas Graf 759bfa83a9eSThomas Graf /** 760fe4944e5SThomas Graf * nla_find_nested - find attribute in a set of nested attributes 761fe4944e5SThomas Graf * @nla: attribute containing the nested attributes 762fe4944e5SThomas Graf * @attrtype: type of attribute to look for 763fe4944e5SThomas Graf * 764fe4944e5SThomas Graf * Returns the first attribute which matches the specified type. 765fe4944e5SThomas Graf */ 7663654654fSJan Engelhardt static inline struct nlattr * 7673654654fSJan Engelhardt nla_find_nested(const struct nlattr *nla, int attrtype) 768fe4944e5SThomas Graf { 769fe4944e5SThomas Graf return nla_find(nla_data(nla), nla_len(nla), attrtype); 770fe4944e5SThomas Graf } 771fe4944e5SThomas Graf 772fe4944e5SThomas Graf /** 773bfa83a9eSThomas Graf * nla_parse_nested - parse nested attributes 774bfa83a9eSThomas Graf * @tb: destination array with maxtype+1 elements 775bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 776bfa83a9eSThomas Graf * @nla: attribute containing the nested attributes 777bfa83a9eSThomas Graf * @policy: validation policy 778fceb6435SJohannes Berg * @extack: extended ACK report struct 779bfa83a9eSThomas Graf * 780bfa83a9eSThomas Graf * See nla_parse() 781bfa83a9eSThomas Graf */ 782bfa83a9eSThomas Graf static inline int nla_parse_nested(struct nlattr *tb[], int maxtype, 783b057efd4SPatrick McHardy const struct nlattr *nla, 784fceb6435SJohannes Berg const struct nla_policy *policy, 785fceb6435SJohannes Berg struct netlink_ext_ack *extack) 786bfa83a9eSThomas Graf { 787fceb6435SJohannes Berg return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy, 788fceb6435SJohannes Berg extack); 789bfa83a9eSThomas Graf } 7901092cb21SPatrick McHardy 7911092cb21SPatrick McHardy /** 792d1ec3b77SPierre Ynard * nla_put_u8 - Add a u8 netlink attribute to a socket buffer 793bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 794bfa83a9eSThomas Graf * @attrtype: attribute type 795bfa83a9eSThomas Graf * @value: numeric value 796bfa83a9eSThomas Graf */ 797bfa83a9eSThomas Graf static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value) 798bfa83a9eSThomas Graf { 799b4391db4SArnd Bergmann /* temporary variables to work around GCC PR81715 with asan-stack=1 */ 800b4391db4SArnd Bergmann u8 tmp = value; 801b4391db4SArnd Bergmann 802b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(u8), &tmp); 803bfa83a9eSThomas Graf } 804bfa83a9eSThomas Graf 805bfa83a9eSThomas Graf /** 806bfa83a9eSThomas Graf * nla_put_u16 - Add a u16 netlink attribute to a socket buffer 807bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 808bfa83a9eSThomas Graf * @attrtype: attribute type 809bfa83a9eSThomas Graf * @value: numeric value 810bfa83a9eSThomas Graf */ 811bfa83a9eSThomas Graf static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value) 812bfa83a9eSThomas Graf { 813b4391db4SArnd Bergmann u16 tmp = value; 814b4391db4SArnd Bergmann 815b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(u16), &tmp); 816bfa83a9eSThomas Graf } 817bfa83a9eSThomas Graf 818bfa83a9eSThomas Graf /** 819569a8fc3SDavid S. Miller * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer 820569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 821569a8fc3SDavid S. Miller * @attrtype: attribute type 822569a8fc3SDavid S. Miller * @value: numeric value 823569a8fc3SDavid S. Miller */ 824569a8fc3SDavid S. Miller static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value) 825569a8fc3SDavid S. Miller { 826b4391db4SArnd Bergmann __be16 tmp = value; 827b4391db4SArnd Bergmann 828b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__be16), &tmp); 829569a8fc3SDavid S. Miller } 830569a8fc3SDavid S. Miller 831569a8fc3SDavid S. Miller /** 8326c1dd3b6SDavid S. Miller * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer 8336c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 8346c1dd3b6SDavid S. Miller * @attrtype: attribute type 8356c1dd3b6SDavid S. Miller * @value: numeric value 8366c1dd3b6SDavid S. Miller */ 8376c1dd3b6SDavid S. Miller static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value) 8386c1dd3b6SDavid S. Miller { 839b4391db4SArnd Bergmann __be16 tmp = value; 840b4391db4SArnd Bergmann 841b4391db4SArnd Bergmann return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, tmp); 8426c1dd3b6SDavid S. Miller } 8436c1dd3b6SDavid S. Miller 8446c1dd3b6SDavid S. Miller /** 84524c410dcSDavid S. Miller * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer 84624c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 84724c410dcSDavid S. Miller * @attrtype: attribute type 84824c410dcSDavid S. Miller * @value: numeric value 84924c410dcSDavid S. Miller */ 85024c410dcSDavid S. Miller static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value) 85124c410dcSDavid S. Miller { 852b4391db4SArnd Bergmann __le16 tmp = value; 853b4391db4SArnd Bergmann 854b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__le16), &tmp); 85524c410dcSDavid S. Miller } 85624c410dcSDavid S. Miller 85724c410dcSDavid S. Miller /** 858bfa83a9eSThomas Graf * nla_put_u32 - Add a u32 netlink attribute to a socket buffer 859bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 860bfa83a9eSThomas Graf * @attrtype: attribute type 861bfa83a9eSThomas Graf * @value: numeric value 862bfa83a9eSThomas Graf */ 863bfa83a9eSThomas Graf static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value) 864bfa83a9eSThomas Graf { 865b4391db4SArnd Bergmann u32 tmp = value; 866b4391db4SArnd Bergmann 867b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(u32), &tmp); 868bfa83a9eSThomas Graf } 869bfa83a9eSThomas Graf 870bfa83a9eSThomas Graf /** 871569a8fc3SDavid S. Miller * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer 872569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 873569a8fc3SDavid S. Miller * @attrtype: attribute type 874569a8fc3SDavid S. Miller * @value: numeric value 875569a8fc3SDavid S. Miller */ 876569a8fc3SDavid S. Miller static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value) 877569a8fc3SDavid S. Miller { 878b4391db4SArnd Bergmann __be32 tmp = value; 879b4391db4SArnd Bergmann 880b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__be32), &tmp); 881569a8fc3SDavid S. Miller } 882569a8fc3SDavid S. Miller 883569a8fc3SDavid S. Miller /** 8846c1dd3b6SDavid S. Miller * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer 8856c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 8866c1dd3b6SDavid S. Miller * @attrtype: attribute type 8876c1dd3b6SDavid S. Miller * @value: numeric value 8886c1dd3b6SDavid S. Miller */ 8896c1dd3b6SDavid S. Miller static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value) 8906c1dd3b6SDavid S. Miller { 891b4391db4SArnd Bergmann __be32 tmp = value; 892b4391db4SArnd Bergmann 893b4391db4SArnd Bergmann return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, tmp); 8946c1dd3b6SDavid S. Miller } 8956c1dd3b6SDavid S. Miller 8966c1dd3b6SDavid S. Miller /** 89724c410dcSDavid S. Miller * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer 89824c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 89924c410dcSDavid S. Miller * @attrtype: attribute type 90024c410dcSDavid S. Miller * @value: numeric value 90124c410dcSDavid S. Miller */ 90224c410dcSDavid S. Miller static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value) 90324c410dcSDavid S. Miller { 904b4391db4SArnd Bergmann __le32 tmp = value; 905b4391db4SArnd Bergmann 906b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(__le32), &tmp); 90724c410dcSDavid S. Miller } 90824c410dcSDavid S. Miller 90924c410dcSDavid S. Miller /** 91073520786SNicolas Dichtel * nla_put_u64_64bit - Add a u64 netlink attribute to a skb and align it 91173520786SNicolas Dichtel * @skb: socket buffer to add attribute to 91273520786SNicolas Dichtel * @attrtype: attribute type 91373520786SNicolas Dichtel * @value: numeric value 91473520786SNicolas Dichtel * @padattr: attribute type for the padding 91573520786SNicolas Dichtel */ 91673520786SNicolas Dichtel static inline int nla_put_u64_64bit(struct sk_buff *skb, int attrtype, 91773520786SNicolas Dichtel u64 value, int padattr) 91873520786SNicolas Dichtel { 919b4391db4SArnd Bergmann u64 tmp = value; 920b4391db4SArnd Bergmann 921b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr); 92273520786SNicolas Dichtel } 92373520786SNicolas Dichtel 92473520786SNicolas Dichtel /** 925b46f6dedSNicolas Dichtel * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it 926569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 927569a8fc3SDavid S. Miller * @attrtype: attribute type 928569a8fc3SDavid S. Miller * @value: numeric value 929b46f6dedSNicolas Dichtel * @padattr: attribute type for the padding 930569a8fc3SDavid S. Miller */ 931b46f6dedSNicolas Dichtel static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value, 932b46f6dedSNicolas Dichtel int padattr) 933b46f6dedSNicolas Dichtel { 934b4391db4SArnd Bergmann __be64 tmp = value; 935b4391db4SArnd Bergmann 936b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(__be64), &tmp, padattr); 937b46f6dedSNicolas Dichtel } 938b46f6dedSNicolas Dichtel 939569a8fc3SDavid S. Miller /** 940e9bbe898SNicolas Dichtel * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it 9416c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 9426c1dd3b6SDavid S. Miller * @attrtype: attribute type 9436c1dd3b6SDavid S. Miller * @value: numeric value 944e9bbe898SNicolas Dichtel * @padattr: attribute type for the padding 9456c1dd3b6SDavid S. Miller */ 946e9bbe898SNicolas Dichtel static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value, 947e9bbe898SNicolas Dichtel int padattr) 9486c1dd3b6SDavid S. Miller { 949b4391db4SArnd Bergmann __be64 tmp = value; 950b4391db4SArnd Bergmann 951b4391db4SArnd Bergmann return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, tmp, 952e9bbe898SNicolas Dichtel padattr); 9536c1dd3b6SDavid S. Miller } 9546c1dd3b6SDavid S. Miller 9556c1dd3b6SDavid S. Miller /** 956e7479122SNicolas Dichtel * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it 95724c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 95824c410dcSDavid S. Miller * @attrtype: attribute type 95924c410dcSDavid S. Miller * @value: numeric value 960e7479122SNicolas Dichtel * @padattr: attribute type for the padding 96124c410dcSDavid S. Miller */ 962e7479122SNicolas Dichtel static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value, 963e7479122SNicolas Dichtel int padattr) 96424c410dcSDavid S. Miller { 965b4391db4SArnd Bergmann __le64 tmp = value; 966b4391db4SArnd Bergmann 967b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(__le64), &tmp, padattr); 96824c410dcSDavid S. Miller } 96924c410dcSDavid S. Miller 97024c410dcSDavid S. Miller /** 9714778e0beSJiri Pirko * nla_put_s8 - Add a s8 netlink attribute to a socket buffer 9724778e0beSJiri Pirko * @skb: socket buffer to add attribute to 9734778e0beSJiri Pirko * @attrtype: attribute type 9744778e0beSJiri Pirko * @value: numeric value 9754778e0beSJiri Pirko */ 9764778e0beSJiri Pirko static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value) 9774778e0beSJiri Pirko { 978b4391db4SArnd Bergmann s8 tmp = value; 979b4391db4SArnd Bergmann 980b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(s8), &tmp); 9814778e0beSJiri Pirko } 9824778e0beSJiri Pirko 9834778e0beSJiri Pirko /** 9844778e0beSJiri Pirko * nla_put_s16 - Add a s16 netlink attribute to a socket buffer 9854778e0beSJiri Pirko * @skb: socket buffer to add attribute to 9864778e0beSJiri Pirko * @attrtype: attribute type 9874778e0beSJiri Pirko * @value: numeric value 9884778e0beSJiri Pirko */ 9894778e0beSJiri Pirko static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value) 9904778e0beSJiri Pirko { 991b4391db4SArnd Bergmann s16 tmp = value; 992b4391db4SArnd Bergmann 993b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(s16), &tmp); 9944778e0beSJiri Pirko } 9954778e0beSJiri Pirko 9964778e0beSJiri Pirko /** 9974778e0beSJiri Pirko * nla_put_s32 - Add a s32 netlink attribute to a socket buffer 9984778e0beSJiri Pirko * @skb: socket buffer to add attribute to 9994778e0beSJiri Pirko * @attrtype: attribute type 10004778e0beSJiri Pirko * @value: numeric value 10014778e0beSJiri Pirko */ 10024778e0beSJiri Pirko static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value) 10034778e0beSJiri Pirko { 1004b4391db4SArnd Bergmann s32 tmp = value; 1005b4391db4SArnd Bergmann 1006b4391db4SArnd Bergmann return nla_put(skb, attrtype, sizeof(s32), &tmp); 10074778e0beSJiri Pirko } 10084778e0beSJiri Pirko 10094778e0beSJiri Pirko /** 1010756a2f59SNicolas Dichtel * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it 10114778e0beSJiri Pirko * @skb: socket buffer to add attribute to 10124778e0beSJiri Pirko * @attrtype: attribute type 10134778e0beSJiri Pirko * @value: numeric value 1014756a2f59SNicolas Dichtel * @padattr: attribute type for the padding 10154778e0beSJiri Pirko */ 1016756a2f59SNicolas Dichtel static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value, 1017756a2f59SNicolas Dichtel int padattr) 10184778e0beSJiri Pirko { 1019b4391db4SArnd Bergmann s64 tmp = value; 1020b4391db4SArnd Bergmann 1021b4391db4SArnd Bergmann return nla_put_64bit(skb, attrtype, sizeof(s64), &tmp, padattr); 10224778e0beSJiri Pirko } 10234778e0beSJiri Pirko 10244778e0beSJiri Pirko /** 1025bfa83a9eSThomas Graf * nla_put_string - Add a string netlink attribute to a socket buffer 1026bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 1027bfa83a9eSThomas Graf * @attrtype: attribute type 1028bfa83a9eSThomas Graf * @str: NUL terminated string 1029bfa83a9eSThomas Graf */ 1030bfa83a9eSThomas Graf static inline int nla_put_string(struct sk_buff *skb, int attrtype, 1031bfa83a9eSThomas Graf const char *str) 1032bfa83a9eSThomas Graf { 1033bfa83a9eSThomas Graf return nla_put(skb, attrtype, strlen(str) + 1, str); 1034bfa83a9eSThomas Graf } 1035bfa83a9eSThomas Graf 1036bfa83a9eSThomas Graf /** 1037bfa83a9eSThomas Graf * nla_put_flag - Add a flag netlink attribute to a socket buffer 1038bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 1039bfa83a9eSThomas Graf * @attrtype: attribute type 1040bfa83a9eSThomas Graf */ 1041bfa83a9eSThomas Graf static inline int nla_put_flag(struct sk_buff *skb, int attrtype) 1042bfa83a9eSThomas Graf { 1043bfa83a9eSThomas Graf return nla_put(skb, attrtype, 0, NULL); 1044bfa83a9eSThomas Graf } 1045bfa83a9eSThomas Graf 1046bfa83a9eSThomas Graf /** 10472175d87cSNicolas Dichtel * nla_put_msecs - Add a msecs netlink attribute to a skb and align it 1048bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 1049bfa83a9eSThomas Graf * @attrtype: attribute type 1050d87de1f3SMark Rustad * @njiffies: number of jiffies to convert to msecs 10512175d87cSNicolas Dichtel * @padattr: attribute type for the padding 1052bfa83a9eSThomas Graf */ 1053bfa83a9eSThomas Graf static inline int nla_put_msecs(struct sk_buff *skb, int attrtype, 10542175d87cSNicolas Dichtel unsigned long njiffies, int padattr) 1055bfa83a9eSThomas Graf { 1056d87de1f3SMark Rustad u64 tmp = jiffies_to_msecs(njiffies); 10572175d87cSNicolas Dichtel 10582175d87cSNicolas Dichtel return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr); 1059bfa83a9eSThomas Graf } 1060bfa83a9eSThomas Graf 1061bfa83a9eSThomas Graf /** 1062930345eaSJiri Benc * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket 1063930345eaSJiri Benc * buffer 1064930345eaSJiri Benc * @skb: socket buffer to add attribute to 1065930345eaSJiri Benc * @attrtype: attribute type 1066930345eaSJiri Benc * @addr: IPv4 address 1067930345eaSJiri Benc */ 1068930345eaSJiri Benc static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype, 1069930345eaSJiri Benc __be32 addr) 1070930345eaSJiri Benc { 1071b4391db4SArnd Bergmann __be32 tmp = addr; 1072b4391db4SArnd Bergmann 1073b4391db4SArnd Bergmann return nla_put_be32(skb, attrtype, tmp); 1074930345eaSJiri Benc } 1075930345eaSJiri Benc 1076930345eaSJiri Benc /** 1077930345eaSJiri Benc * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket 1078930345eaSJiri Benc * buffer 1079930345eaSJiri Benc * @skb: socket buffer to add attribute to 1080930345eaSJiri Benc * @attrtype: attribute type 1081930345eaSJiri Benc * @addr: IPv6 address 1082930345eaSJiri Benc */ 1083930345eaSJiri Benc static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype, 1084930345eaSJiri Benc const struct in6_addr *addr) 1085930345eaSJiri Benc { 1086930345eaSJiri Benc return nla_put(skb, attrtype, sizeof(*addr), addr); 1087930345eaSJiri Benc } 1088930345eaSJiri Benc 1089930345eaSJiri Benc /** 1090bfa83a9eSThomas Graf * nla_get_u32 - return payload of u32 attribute 1091bfa83a9eSThomas Graf * @nla: u32 netlink attribute 1092bfa83a9eSThomas Graf */ 1093b057efd4SPatrick McHardy static inline u32 nla_get_u32(const struct nlattr *nla) 1094bfa83a9eSThomas Graf { 1095bfa83a9eSThomas Graf return *(u32 *) nla_data(nla); 1096bfa83a9eSThomas Graf } 1097bfa83a9eSThomas Graf 1098bfa83a9eSThomas Graf /** 109900012e5bSAl Viro * nla_get_be32 - return payload of __be32 attribute 110000012e5bSAl Viro * @nla: __be32 netlink attribute 110100012e5bSAl Viro */ 1102b057efd4SPatrick McHardy static inline __be32 nla_get_be32(const struct nlattr *nla) 110300012e5bSAl Viro { 110400012e5bSAl Viro return *(__be32 *) nla_data(nla); 110500012e5bSAl Viro } 110600012e5bSAl Viro 110700012e5bSAl Viro /** 1108c648a013SAlexander Aring * nla_get_le32 - return payload of __le32 attribute 1109c648a013SAlexander Aring * @nla: __le32 netlink attribute 1110c648a013SAlexander Aring */ 1111c648a013SAlexander Aring static inline __le32 nla_get_le32(const struct nlattr *nla) 1112c648a013SAlexander Aring { 1113c648a013SAlexander Aring return *(__le32 *) nla_data(nla); 1114c648a013SAlexander Aring } 1115c648a013SAlexander Aring 1116c648a013SAlexander Aring /** 1117bfa83a9eSThomas Graf * nla_get_u16 - return payload of u16 attribute 1118bfa83a9eSThomas Graf * @nla: u16 netlink attribute 1119bfa83a9eSThomas Graf */ 1120b057efd4SPatrick McHardy static inline u16 nla_get_u16(const struct nlattr *nla) 1121bfa83a9eSThomas Graf { 1122bfa83a9eSThomas Graf return *(u16 *) nla_data(nla); 1123bfa83a9eSThomas Graf } 1124bfa83a9eSThomas Graf 1125bfa83a9eSThomas Graf /** 1126838965baSPatrick McHardy * nla_get_be16 - return payload of __be16 attribute 1127838965baSPatrick McHardy * @nla: __be16 netlink attribute 1128838965baSPatrick McHardy */ 1129b057efd4SPatrick McHardy static inline __be16 nla_get_be16(const struct nlattr *nla) 1130838965baSPatrick McHardy { 1131838965baSPatrick McHardy return *(__be16 *) nla_data(nla); 1132838965baSPatrick McHardy } 1133838965baSPatrick McHardy 1134838965baSPatrick McHardy /** 11354a89c256SThomas Graf * nla_get_le16 - return payload of __le16 attribute 11364a89c256SThomas Graf * @nla: __le16 netlink attribute 11374a89c256SThomas Graf */ 1138b057efd4SPatrick McHardy static inline __le16 nla_get_le16(const struct nlattr *nla) 11394a89c256SThomas Graf { 11404a89c256SThomas Graf return *(__le16 *) nla_data(nla); 11414a89c256SThomas Graf } 11424a89c256SThomas Graf 11434a89c256SThomas Graf /** 1144bfa83a9eSThomas Graf * nla_get_u8 - return payload of u8 attribute 1145bfa83a9eSThomas Graf * @nla: u8 netlink attribute 1146bfa83a9eSThomas Graf */ 1147b057efd4SPatrick McHardy static inline u8 nla_get_u8(const struct nlattr *nla) 1148bfa83a9eSThomas Graf { 1149bfa83a9eSThomas Graf return *(u8 *) nla_data(nla); 1150bfa83a9eSThomas Graf } 1151bfa83a9eSThomas Graf 1152bfa83a9eSThomas Graf /** 1153bfa83a9eSThomas Graf * nla_get_u64 - return payload of u64 attribute 1154bfa83a9eSThomas Graf * @nla: u64 netlink attribute 1155bfa83a9eSThomas Graf */ 1156b057efd4SPatrick McHardy static inline u64 nla_get_u64(const struct nlattr *nla) 1157bfa83a9eSThomas Graf { 1158bfa83a9eSThomas Graf u64 tmp; 1159bfa83a9eSThomas Graf 1160bfa83a9eSThomas Graf nla_memcpy(&tmp, nla, sizeof(tmp)); 1161bfa83a9eSThomas Graf 1162bfa83a9eSThomas Graf return tmp; 1163bfa83a9eSThomas Graf } 1164bfa83a9eSThomas Graf 1165bfa83a9eSThomas Graf /** 1166a17c8598SPablo Neira Ayuso * nla_get_be64 - return payload of __be64 attribute 1167a17c8598SPablo Neira Ayuso * @nla: __be64 netlink attribute 1168a17c8598SPablo Neira Ayuso */ 1169a17c8598SPablo Neira Ayuso static inline __be64 nla_get_be64(const struct nlattr *nla) 1170a17c8598SPablo Neira Ayuso { 1171f5d410f2SPablo Neira Ayuso __be64 tmp; 1172f5d410f2SPablo Neira Ayuso 1173f5d410f2SPablo Neira Ayuso nla_memcpy(&tmp, nla, sizeof(tmp)); 1174f5d410f2SPablo Neira Ayuso 1175f5d410f2SPablo Neira Ayuso return tmp; 1176a17c8598SPablo Neira Ayuso } 1177a17c8598SPablo Neira Ayuso 1178a17c8598SPablo Neira Ayuso /** 1179c648a013SAlexander Aring * nla_get_le64 - return payload of __le64 attribute 1180c648a013SAlexander Aring * @nla: __le64 netlink attribute 1181c648a013SAlexander Aring */ 1182c648a013SAlexander Aring static inline __le64 nla_get_le64(const struct nlattr *nla) 1183c648a013SAlexander Aring { 1184c648a013SAlexander Aring return *(__le64 *) nla_data(nla); 1185c648a013SAlexander Aring } 1186c648a013SAlexander Aring 1187c648a013SAlexander Aring /** 11884778e0beSJiri Pirko * nla_get_s32 - return payload of s32 attribute 11894778e0beSJiri Pirko * @nla: s32 netlink attribute 11904778e0beSJiri Pirko */ 11914778e0beSJiri Pirko static inline s32 nla_get_s32(const struct nlattr *nla) 11924778e0beSJiri Pirko { 11934778e0beSJiri Pirko return *(s32 *) nla_data(nla); 11944778e0beSJiri Pirko } 11954778e0beSJiri Pirko 11964778e0beSJiri Pirko /** 11974778e0beSJiri Pirko * nla_get_s16 - return payload of s16 attribute 11984778e0beSJiri Pirko * @nla: s16 netlink attribute 11994778e0beSJiri Pirko */ 12004778e0beSJiri Pirko static inline s16 nla_get_s16(const struct nlattr *nla) 12014778e0beSJiri Pirko { 12024778e0beSJiri Pirko return *(s16 *) nla_data(nla); 12034778e0beSJiri Pirko } 12044778e0beSJiri Pirko 12054778e0beSJiri Pirko /** 12064778e0beSJiri Pirko * nla_get_s8 - return payload of s8 attribute 12074778e0beSJiri Pirko * @nla: s8 netlink attribute 12084778e0beSJiri Pirko */ 12094778e0beSJiri Pirko static inline s8 nla_get_s8(const struct nlattr *nla) 12104778e0beSJiri Pirko { 12114778e0beSJiri Pirko return *(s8 *) nla_data(nla); 12124778e0beSJiri Pirko } 12134778e0beSJiri Pirko 12144778e0beSJiri Pirko /** 12154778e0beSJiri Pirko * nla_get_s64 - return payload of s64 attribute 12164778e0beSJiri Pirko * @nla: s64 netlink attribute 12174778e0beSJiri Pirko */ 12184778e0beSJiri Pirko static inline s64 nla_get_s64(const struct nlattr *nla) 12194778e0beSJiri Pirko { 12204778e0beSJiri Pirko s64 tmp; 12214778e0beSJiri Pirko 12224778e0beSJiri Pirko nla_memcpy(&tmp, nla, sizeof(tmp)); 12234778e0beSJiri Pirko 12244778e0beSJiri Pirko return tmp; 12254778e0beSJiri Pirko } 12264778e0beSJiri Pirko 12274778e0beSJiri Pirko /** 1228bfa83a9eSThomas Graf * nla_get_flag - return payload of flag attribute 1229bfa83a9eSThomas Graf * @nla: flag netlink attribute 1230bfa83a9eSThomas Graf */ 1231b057efd4SPatrick McHardy static inline int nla_get_flag(const struct nlattr *nla) 1232bfa83a9eSThomas Graf { 1233bfa83a9eSThomas Graf return !!nla; 1234bfa83a9eSThomas Graf } 1235bfa83a9eSThomas Graf 1236bfa83a9eSThomas Graf /** 1237bfa83a9eSThomas Graf * nla_get_msecs - return payload of msecs attribute 1238bfa83a9eSThomas Graf * @nla: msecs netlink attribute 1239bfa83a9eSThomas Graf * 1240bfa83a9eSThomas Graf * Returns the number of milliseconds in jiffies. 1241bfa83a9eSThomas Graf */ 1242b057efd4SPatrick McHardy static inline unsigned long nla_get_msecs(const struct nlattr *nla) 1243bfa83a9eSThomas Graf { 1244bfa83a9eSThomas Graf u64 msecs = nla_get_u64(nla); 1245bfa83a9eSThomas Graf 1246bfa83a9eSThomas Graf return msecs_to_jiffies((unsigned long) msecs); 1247bfa83a9eSThomas Graf } 1248bfa83a9eSThomas Graf 1249bfa83a9eSThomas Graf /** 125067b61f6cSJiri Benc * nla_get_in_addr - return payload of IPv4 address attribute 125167b61f6cSJiri Benc * @nla: IPv4 address netlink attribute 125267b61f6cSJiri Benc */ 125367b61f6cSJiri Benc static inline __be32 nla_get_in_addr(const struct nlattr *nla) 125467b61f6cSJiri Benc { 125567b61f6cSJiri Benc return *(__be32 *) nla_data(nla); 125667b61f6cSJiri Benc } 125767b61f6cSJiri Benc 125867b61f6cSJiri Benc /** 125967b61f6cSJiri Benc * nla_get_in6_addr - return payload of IPv6 address attribute 126067b61f6cSJiri Benc * @nla: IPv6 address netlink attribute 126167b61f6cSJiri Benc */ 126267b61f6cSJiri Benc static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla) 126367b61f6cSJiri Benc { 126467b61f6cSJiri Benc struct in6_addr tmp; 126567b61f6cSJiri Benc 126667b61f6cSJiri Benc nla_memcpy(&tmp, nla, sizeof(tmp)); 126767b61f6cSJiri Benc return tmp; 126867b61f6cSJiri Benc } 126967b61f6cSJiri Benc 127067b61f6cSJiri Benc /** 127164c83d83SJamal Hadi Salim * nla_get_bitfield32 - return payload of 32 bitfield attribute 127264c83d83SJamal Hadi Salim * @nla: nla_bitfield32 attribute 127364c83d83SJamal Hadi Salim */ 127464c83d83SJamal Hadi Salim static inline struct nla_bitfield32 nla_get_bitfield32(const struct nlattr *nla) 127564c83d83SJamal Hadi Salim { 127664c83d83SJamal Hadi Salim struct nla_bitfield32 tmp; 127764c83d83SJamal Hadi Salim 127864c83d83SJamal Hadi Salim nla_memcpy(&tmp, nla, sizeof(tmp)); 127964c83d83SJamal Hadi Salim return tmp; 128064c83d83SJamal Hadi Salim } 128164c83d83SJamal Hadi Salim 128264c83d83SJamal Hadi Salim /** 1283b15ca182SThomas Graf * nla_memdup - duplicate attribute memory (kmemdup) 1284b15ca182SThomas Graf * @src: netlink attribute to duplicate from 1285b15ca182SThomas Graf * @gfp: GFP mask 1286b15ca182SThomas Graf */ 1287b15ca182SThomas Graf static inline void *nla_memdup(const struct nlattr *src, gfp_t gfp) 1288b15ca182SThomas Graf { 1289b15ca182SThomas Graf return kmemdup(nla_data(src), nla_len(src), gfp); 1290b15ca182SThomas Graf } 1291b15ca182SThomas Graf 1292b15ca182SThomas Graf /** 1293bfa83a9eSThomas Graf * nla_nest_start - Start a new level of nested attributes 1294bfa83a9eSThomas Graf * @skb: socket buffer to add attributes to 1295bfa83a9eSThomas Graf * @attrtype: attribute type of container 1296bfa83a9eSThomas Graf * 1297bfa83a9eSThomas Graf * Returns the container attribute 1298bfa83a9eSThomas Graf */ 1299bfa83a9eSThomas Graf static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype) 1300bfa83a9eSThomas Graf { 130127a884dcSArnaldo Carvalho de Melo struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb); 1302bfa83a9eSThomas Graf 1303bfa83a9eSThomas Graf if (nla_put(skb, attrtype, 0, NULL) < 0) 1304bfa83a9eSThomas Graf return NULL; 1305bfa83a9eSThomas Graf 1306bfa83a9eSThomas Graf return start; 1307bfa83a9eSThomas Graf } 1308bfa83a9eSThomas Graf 1309bfa83a9eSThomas Graf /** 1310bfa83a9eSThomas Graf * nla_nest_end - Finalize nesting of attributes 1311d1ec3b77SPierre Ynard * @skb: socket buffer the attributes are stored in 1312bfa83a9eSThomas Graf * @start: container attribute 1313bfa83a9eSThomas Graf * 1314bfa83a9eSThomas Graf * Corrects the container attribute header to include the all 1315bfa83a9eSThomas Graf * appeneded attributes. 1316bfa83a9eSThomas Graf * 1317bfa83a9eSThomas Graf * Returns the total data length of the skb. 1318bfa83a9eSThomas Graf */ 1319bfa83a9eSThomas Graf static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start) 1320bfa83a9eSThomas Graf { 132127a884dcSArnaldo Carvalho de Melo start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start; 1322bfa83a9eSThomas Graf return skb->len; 1323bfa83a9eSThomas Graf } 1324bfa83a9eSThomas Graf 1325bfa83a9eSThomas Graf /** 1326bfa83a9eSThomas Graf * nla_nest_cancel - Cancel nesting of attributes 1327bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 1328bfa83a9eSThomas Graf * @start: container attribute 1329bfa83a9eSThomas Graf * 1330bfa83a9eSThomas Graf * Removes the container attribute and including all nested 1331bc3ed28cSThomas Graf * attributes. Returns -EMSGSIZE 1332bfa83a9eSThomas Graf */ 1333bc3ed28cSThomas Graf static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start) 1334bfa83a9eSThomas Graf { 1335bc3ed28cSThomas Graf nlmsg_trim(skb, start); 1336bfa83a9eSThomas Graf } 1337bfa83a9eSThomas Graf 1338bfa83a9eSThomas Graf /** 13394fe5d5c0SPaul Moore * nla_validate_nested - Validate a stream of nested attributes 13404fe5d5c0SPaul Moore * @start: container attribute 13414fe5d5c0SPaul Moore * @maxtype: maximum attribute type to be expected 13424fe5d5c0SPaul Moore * @policy: validation policy 1343fceb6435SJohannes Berg * @extack: extended ACK report struct 13444fe5d5c0SPaul Moore * 13454fe5d5c0SPaul Moore * Validates all attributes in the nested attribute stream against the 13464fe5d5c0SPaul Moore * specified policy. Attributes with a type exceeding maxtype will be 13474fe5d5c0SPaul Moore * ignored. See documenation of struct nla_policy for more details. 13484fe5d5c0SPaul Moore * 13494fe5d5c0SPaul Moore * Returns 0 on success or a negative error code. 13504fe5d5c0SPaul Moore */ 13513654654fSJan Engelhardt static inline int nla_validate_nested(const struct nlattr *start, int maxtype, 1352fceb6435SJohannes Berg const struct nla_policy *policy, 1353fceb6435SJohannes Berg struct netlink_ext_ack *extack) 13544fe5d5c0SPaul Moore { 1355fceb6435SJohannes Berg return nla_validate(nla_data(start), nla_len(start), maxtype, policy, 1356fceb6435SJohannes Berg extack); 13574fe5d5c0SPaul Moore } 13584fe5d5c0SPaul Moore 13594fe5d5c0SPaul Moore /** 1360089bf1a6SNicolas Dichtel * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute 1361089bf1a6SNicolas Dichtel * @skb: socket buffer the message is stored in 1362089bf1a6SNicolas Dichtel * 1363089bf1a6SNicolas Dichtel * Return true if padding is needed to align the next attribute (nla_data()) to 1364089bf1a6SNicolas Dichtel * a 64-bit aligned area. 1365089bf1a6SNicolas Dichtel */ 1366089bf1a6SNicolas Dichtel static inline bool nla_need_padding_for_64bit(struct sk_buff *skb) 1367089bf1a6SNicolas Dichtel { 1368089bf1a6SNicolas Dichtel #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1369089bf1a6SNicolas Dichtel /* The nlattr header is 4 bytes in size, that's why we test 1370089bf1a6SNicolas Dichtel * if the skb->data _is_ aligned. A NOP attribute, plus 1371089bf1a6SNicolas Dichtel * nlattr header for next attribute, will make nla_data() 1372089bf1a6SNicolas Dichtel * 8-byte aligned. 1373089bf1a6SNicolas Dichtel */ 1374089bf1a6SNicolas Dichtel if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8)) 1375089bf1a6SNicolas Dichtel return true; 1376089bf1a6SNicolas Dichtel #endif 1377089bf1a6SNicolas Dichtel return false; 1378089bf1a6SNicolas Dichtel } 1379089bf1a6SNicolas Dichtel 1380089bf1a6SNicolas Dichtel /** 138135c58459SDavid S. Miller * nla_align_64bit - 64-bit align the nla_data() of next attribute 138235c58459SDavid S. Miller * @skb: socket buffer the message is stored in 138335c58459SDavid S. Miller * @padattr: attribute type for the padding 138435c58459SDavid S. Miller * 138535c58459SDavid S. Miller * Conditionally emit a padding netlink attribute in order to make 138635c58459SDavid S. Miller * the next attribute we emit have a 64-bit aligned nla_data() area. 138735c58459SDavid S. Miller * This will only be done in architectures which do not have 1388cca1d815SEric Dumazet * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined. 138935c58459SDavid S. Miller * 139035c58459SDavid S. Miller * Returns zero on success or a negative error code. 139135c58459SDavid S. Miller */ 139235c58459SDavid S. Miller static inline int nla_align_64bit(struct sk_buff *skb, int padattr) 139335c58459SDavid S. Miller { 1394089bf1a6SNicolas Dichtel if (nla_need_padding_for_64bit(skb) && 1395cca1d815SEric Dumazet !nla_reserve(skb, padattr, 0)) 139635c58459SDavid S. Miller return -EMSGSIZE; 1397089bf1a6SNicolas Dichtel 139835c58459SDavid S. Miller return 0; 139935c58459SDavid S. Miller } 140035c58459SDavid S. Miller 140135c58459SDavid S. Miller /** 140235c58459SDavid S. Miller * nla_total_size_64bit - total length of attribute including padding 140335c58459SDavid S. Miller * @payload: length of payload 140435c58459SDavid S. Miller */ 140535c58459SDavid S. Miller static inline int nla_total_size_64bit(int payload) 140635c58459SDavid S. Miller { 140735c58459SDavid S. Miller return NLA_ALIGN(nla_attr_size(payload)) 1408cca1d815SEric Dumazet #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 140935c58459SDavid S. Miller + NLA_ALIGN(nla_attr_size(0)) 141035c58459SDavid S. Miller #endif 141135c58459SDavid S. Miller ; 141235c58459SDavid S. Miller } 141335c58459SDavid S. Miller 141435c58459SDavid S. Miller /** 1415bfa83a9eSThomas Graf * nla_for_each_attr - iterate over a stream of attributes 1416bfa83a9eSThomas Graf * @pos: loop counter, set to current attribute 1417bfa83a9eSThomas Graf * @head: head of attribute stream 1418bfa83a9eSThomas Graf * @len: length of attribute stream 1419bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 1420bfa83a9eSThomas Graf */ 1421bfa83a9eSThomas Graf #define nla_for_each_attr(pos, head, len, rem) \ 1422bfa83a9eSThomas Graf for (pos = head, rem = len; \ 1423bfa83a9eSThomas Graf nla_ok(pos, rem); \ 1424bfa83a9eSThomas Graf pos = nla_next(pos, &(rem))) 1425bfa83a9eSThomas Graf 1426fe4944e5SThomas Graf /** 1427fe4944e5SThomas Graf * nla_for_each_nested - iterate over nested attributes 1428fe4944e5SThomas Graf * @pos: loop counter, set to current attribute 1429fe4944e5SThomas Graf * @nla: attribute containing the nested attributes 1430fe4944e5SThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 1431fe4944e5SThomas Graf */ 1432fe4944e5SThomas Graf #define nla_for_each_nested(pos, nla, rem) \ 1433fe4944e5SThomas Graf nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem) 1434fe4944e5SThomas Graf 1435941d8ebcSSimon Horman /** 1436941d8ebcSSimon Horman * nla_is_last - Test if attribute is last in stream 1437941d8ebcSSimon Horman * @nla: attribute to test 1438941d8ebcSSimon Horman * @rem: bytes remaining in stream 1439941d8ebcSSimon Horman */ 1440941d8ebcSSimon Horman static inline bool nla_is_last(const struct nlattr *nla, int rem) 1441941d8ebcSSimon Horman { 1442941d8ebcSSimon Horman return nla->nla_len == rem; 1443941d8ebcSSimon Horman } 1444941d8ebcSSimon Horman 1445bfa83a9eSThomas Graf #endif 1446