1bfa83a9eSThomas Graf #ifndef __NET_NETLINK_H 2bfa83a9eSThomas Graf #define __NET_NETLINK_H 3bfa83a9eSThomas Graf 4bfa83a9eSThomas Graf #include <linux/types.h> 5bfa83a9eSThomas Graf #include <linux/netlink.h> 6d7fe0f24SAl Viro #include <linux/jiffies.h> 7bfa83a9eSThomas Graf 8bfa83a9eSThomas Graf /* ======================================================================== 9bfa83a9eSThomas Graf * Netlink Messages and Attributes Interface (As Seen On TV) 10bfa83a9eSThomas Graf * ------------------------------------------------------------------------ 11bfa83a9eSThomas Graf * Messages Interface 12bfa83a9eSThomas Graf * ------------------------------------------------------------------------ 13bfa83a9eSThomas Graf * 14bfa83a9eSThomas Graf * Message Format: 15bfa83a9eSThomas Graf * <--- nlmsg_total_size(payload) ---> 16bfa83a9eSThomas Graf * <-- nlmsg_msg_size(payload) -> 17bfa83a9eSThomas Graf * +----------+- - -+-------------+- - -+-------- - - 18bfa83a9eSThomas Graf * | nlmsghdr | Pad | Payload | Pad | nlmsghdr 19bfa83a9eSThomas Graf * +----------+- - -+-------------+- - -+-------- - - 20bfa83a9eSThomas Graf * nlmsg_data(nlh)---^ ^ 21bfa83a9eSThomas Graf * nlmsg_next(nlh)-----------------------+ 22bfa83a9eSThomas Graf * 23bfa83a9eSThomas Graf * Payload Format: 24bfa83a9eSThomas Graf * <---------------------- nlmsg_len(nlh) ---------------------> 25bfa83a9eSThomas Graf * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) -> 26bfa83a9eSThomas Graf * +----------------------+- - -+--------------------------------+ 27bfa83a9eSThomas Graf * | Family Header | Pad | Attributes | 28bfa83a9eSThomas Graf * +----------------------+- - -+--------------------------------+ 29bfa83a9eSThomas Graf * nlmsg_attrdata(nlh, hdrlen)---^ 30bfa83a9eSThomas Graf * 31bfa83a9eSThomas Graf * Data Structures: 32bfa83a9eSThomas Graf * struct nlmsghdr netlink message header 33bfa83a9eSThomas Graf * 34bfa83a9eSThomas Graf * Message Construction: 35bfa83a9eSThomas Graf * nlmsg_new() create a new netlink message 36bfa83a9eSThomas Graf * nlmsg_put() add a netlink message to an skb 37bfa83a9eSThomas Graf * nlmsg_put_answer() callback based nlmsg_put() 381dc8d8c0SJustin P. Mattock * nlmsg_end() finalize netlink message 39fe4944e5SThomas Graf * nlmsg_get_pos() return current position in message 40fe4944e5SThomas Graf * nlmsg_trim() trim part of message 41bfa83a9eSThomas Graf * nlmsg_cancel() cancel message construction 42bfa83a9eSThomas Graf * nlmsg_free() free a netlink message 43bfa83a9eSThomas Graf * 44bfa83a9eSThomas Graf * Message Sending: 45bfa83a9eSThomas Graf * nlmsg_multicast() multicast message to several groups 46bfa83a9eSThomas Graf * nlmsg_unicast() unicast a message to a single socket 47d387f6adSThomas Graf * nlmsg_notify() send notification message 48bfa83a9eSThomas Graf * 49bfa83a9eSThomas Graf * Message Length Calculations: 50bfa83a9eSThomas Graf * nlmsg_msg_size(payload) length of message w/o padding 51bfa83a9eSThomas Graf * nlmsg_total_size(payload) length of message w/ padding 52bfa83a9eSThomas Graf * nlmsg_padlen(payload) length of padding at tail 53bfa83a9eSThomas Graf * 54bfa83a9eSThomas Graf * Message Payload Access: 55bfa83a9eSThomas Graf * nlmsg_data(nlh) head of message payload 56bfa83a9eSThomas Graf * nlmsg_len(nlh) length of message payload 57bfa83a9eSThomas Graf * nlmsg_attrdata(nlh, hdrlen) head of attributes data 58bfa83a9eSThomas Graf * nlmsg_attrlen(nlh, hdrlen) length of attributes data 59bfa83a9eSThomas Graf * 60bfa83a9eSThomas Graf * Message Parsing: 61bfa83a9eSThomas Graf * nlmsg_ok(nlh, remaining) does nlh fit into remaining bytes? 62bfa83a9eSThomas Graf * nlmsg_next(nlh, remaining) get next netlink message 63bfa83a9eSThomas Graf * nlmsg_parse() parse attributes of a message 64bfa83a9eSThomas Graf * nlmsg_find_attr() find an attribute in a message 65bfa83a9eSThomas Graf * nlmsg_for_each_msg() loop over all messages 66bfa83a9eSThomas Graf * nlmsg_validate() validate netlink message incl. attrs 67bfa83a9eSThomas Graf * nlmsg_for_each_attr() loop over all attributes 68bfa83a9eSThomas Graf * 6997676b6bSThomas Graf * Misc: 7097676b6bSThomas Graf * nlmsg_report() report back to application? 7197676b6bSThomas Graf * 72bfa83a9eSThomas Graf * ------------------------------------------------------------------------ 73bfa83a9eSThomas Graf * Attributes Interface 74bfa83a9eSThomas Graf * ------------------------------------------------------------------------ 75bfa83a9eSThomas Graf * 76bfa83a9eSThomas Graf * Attribute Format: 77bfa83a9eSThomas Graf * <------- nla_total_size(payload) -------> 78bfa83a9eSThomas Graf * <---- nla_attr_size(payload) -----> 79bfa83a9eSThomas Graf * +----------+- - -+- - - - - - - - - +- - -+-------- - - 80bfa83a9eSThomas Graf * | Header | Pad | Payload | Pad | Header 81bfa83a9eSThomas Graf * +----------+- - -+- - - - - - - - - +- - -+-------- - - 82bfa83a9eSThomas Graf * <- nla_len(nla) -> ^ 83bfa83a9eSThomas Graf * nla_data(nla)----^ | 84bfa83a9eSThomas Graf * nla_next(nla)-----------------------------' 85bfa83a9eSThomas Graf * 86bfa83a9eSThomas Graf * Data Structures: 87d1ec3b77SPierre Ynard * struct nlattr netlink attribute header 88bfa83a9eSThomas Graf * 89bfa83a9eSThomas Graf * Attribute Construction: 90fe4944e5SThomas Graf * nla_reserve(skb, type, len) reserve room for an attribute 91fe4944e5SThomas Graf * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr 92bfa83a9eSThomas Graf * nla_put(skb, type, len, data) add attribute to skb 93fe4944e5SThomas Graf * nla_put_nohdr(skb, len, data) add attribute w/o hdr 9401480e1cSPatrick McHardy * nla_append(skb, len, data) append data to skb 95bfa83a9eSThomas Graf * 96bfa83a9eSThomas Graf * Attribute Construction for Basic Types: 97bfa83a9eSThomas Graf * nla_put_u8(skb, type, value) add u8 attribute to skb 98bfa83a9eSThomas Graf * nla_put_u16(skb, type, value) add u16 attribute to skb 99bfa83a9eSThomas Graf * nla_put_u32(skb, type, value) add u32 attribute to skb 100bfa83a9eSThomas Graf * nla_put_u64(skb, type, value) add u64 attribute to skb 1014778e0beSJiri Pirko * nla_put_s8(skb, type, value) add s8 attribute to skb 1024778e0beSJiri Pirko * nla_put_s16(skb, type, value) add s16 attribute to skb 1034778e0beSJiri Pirko * nla_put_s32(skb, type, value) add s32 attribute to skb 1044778e0beSJiri Pirko * nla_put_s64(skb, type, value) add s64 attribute to skb 105bfa83a9eSThomas Graf * nla_put_string(skb, type, str) add string attribute to skb 106bfa83a9eSThomas Graf * nla_put_flag(skb, type) add flag attribute to skb 107bfa83a9eSThomas Graf * nla_put_msecs(skb, type, jiffies) add msecs attribute to skb 108bfa83a9eSThomas Graf * 109bfa83a9eSThomas Graf * Nested Attributes Construction: 110bfa83a9eSThomas Graf * nla_nest_start(skb, type) start a nested attribute 111bfa83a9eSThomas Graf * nla_nest_end(skb, nla) finalize a nested attribute 112bfa83a9eSThomas Graf * nla_nest_cancel(skb, nla) cancel nested attribute construction 113bfa83a9eSThomas Graf * 114bfa83a9eSThomas Graf * Attribute Length Calculations: 115bfa83a9eSThomas Graf * nla_attr_size(payload) length of attribute w/o padding 116bfa83a9eSThomas Graf * nla_total_size(payload) length of attribute w/ padding 117bfa83a9eSThomas Graf * nla_padlen(payload) length of padding 118bfa83a9eSThomas Graf * 119bfa83a9eSThomas Graf * Attribute Payload Access: 120bfa83a9eSThomas Graf * nla_data(nla) head of attribute payload 121bfa83a9eSThomas Graf * nla_len(nla) length of attribute payload 122bfa83a9eSThomas Graf * 123bfa83a9eSThomas Graf * Attribute Payload Access for Basic Types: 124bfa83a9eSThomas Graf * nla_get_u8(nla) get payload for a u8 attribute 125bfa83a9eSThomas Graf * nla_get_u16(nla) get payload for a u16 attribute 126bfa83a9eSThomas Graf * nla_get_u32(nla) get payload for a u32 attribute 127bfa83a9eSThomas Graf * nla_get_u64(nla) get payload for a u64 attribute 1284778e0beSJiri Pirko * nla_get_s8(nla) get payload for a s8 attribute 1294778e0beSJiri Pirko * nla_get_s16(nla) get payload for a s16 attribute 1304778e0beSJiri Pirko * nla_get_s32(nla) get payload for a s32 attribute 1314778e0beSJiri Pirko * nla_get_s64(nla) get payload for a s64 attribute 132bfa83a9eSThomas Graf * nla_get_flag(nla) return 1 if flag is true 133bfa83a9eSThomas Graf * nla_get_msecs(nla) get payload for a msecs attribute 134bfa83a9eSThomas Graf * 135bfa83a9eSThomas Graf * Attribute Misc: 136bfa83a9eSThomas Graf * nla_memcpy(dest, nla, count) copy attribute into memory 137bfa83a9eSThomas Graf * nla_memcmp(nla, data, size) compare attribute with memory area 138bfa83a9eSThomas Graf * nla_strlcpy(dst, nla, size) copy attribute to a sized string 139bfa83a9eSThomas Graf * nla_strcmp(nla, str) compare attribute with string 140bfa83a9eSThomas Graf * 141bfa83a9eSThomas Graf * Attribute Parsing: 142bfa83a9eSThomas Graf * nla_ok(nla, remaining) does nla fit into remaining bytes? 143bfa83a9eSThomas Graf * nla_next(nla, remaining) get next netlink attribute 144bfa83a9eSThomas Graf * nla_validate() validate a stream of attributes 1454fe5d5c0SPaul Moore * nla_validate_nested() validate a stream of nested attributes 146bfa83a9eSThomas Graf * nla_find() find attribute in stream of attributes 147fe4944e5SThomas Graf * nla_find_nested() find attribute in nested attributes 148bfa83a9eSThomas Graf * nla_parse() parse and validate stream of attrs 149bfa83a9eSThomas Graf * nla_parse_nested() parse nested attribuets 150bfa83a9eSThomas Graf * nla_for_each_attr() loop over all attributes 15122acb19aSPaul Moore * nla_for_each_nested() loop over the nested attributes 152bfa83a9eSThomas Graf *========================================================================= 153bfa83a9eSThomas Graf */ 154bfa83a9eSThomas Graf 155bfa83a9eSThomas Graf /** 156bfa83a9eSThomas Graf * Standard attribute types to specify validation policy 157bfa83a9eSThomas Graf */ 158bfa83a9eSThomas Graf enum { 159bfa83a9eSThomas Graf NLA_UNSPEC, 160bfa83a9eSThomas Graf NLA_U8, 161bfa83a9eSThomas Graf NLA_U16, 162bfa83a9eSThomas Graf NLA_U32, 163bfa83a9eSThomas Graf NLA_U64, 164bfa83a9eSThomas Graf NLA_STRING, 165bfa83a9eSThomas Graf NLA_FLAG, 166bfa83a9eSThomas Graf NLA_MSECS, 167bfa83a9eSThomas Graf NLA_NESTED, 1681092cb21SPatrick McHardy NLA_NESTED_COMPAT, 169a5531a5dSThomas Graf NLA_NUL_STRING, 170d30045a0SJohannes Berg NLA_BINARY, 1714778e0beSJiri Pirko NLA_S8, 1724778e0beSJiri Pirko NLA_S16, 1734778e0beSJiri Pirko NLA_S32, 1744778e0beSJiri Pirko NLA_S64, 175bfa83a9eSThomas Graf __NLA_TYPE_MAX, 176bfa83a9eSThomas Graf }; 177bfa83a9eSThomas Graf 178bfa83a9eSThomas Graf #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1) 179bfa83a9eSThomas Graf 180bfa83a9eSThomas Graf /** 181bfa83a9eSThomas Graf * struct nla_policy - attribute validation policy 182bfa83a9eSThomas Graf * @type: Type of attribute or NLA_UNSPEC 183a5531a5dSThomas Graf * @len: Type specific length of payload 184bfa83a9eSThomas Graf * 185bfa83a9eSThomas Graf * Policies are defined as arrays of this struct, the array must be 186bfa83a9eSThomas Graf * accessible by attribute type up to the highest identifier to be expected. 187bfa83a9eSThomas Graf * 188a5531a5dSThomas Graf * Meaning of `len' field: 189a5531a5dSThomas Graf * NLA_STRING Maximum length of string 190a5531a5dSThomas Graf * NLA_NUL_STRING Maximum length of string (excluding NUL) 191a5531a5dSThomas Graf * NLA_FLAG Unused 192d30045a0SJohannes Berg * NLA_BINARY Maximum length of attribute payload 1934b6cc728SJohannes Berg * NLA_NESTED Don't use `len' field -- length verification is 1944b6cc728SJohannes Berg * done by checking len of nested header (or empty) 1954b6cc728SJohannes Berg * NLA_NESTED_COMPAT Minimum length of structure payload 1964b6cc728SJohannes Berg * NLA_U8, NLA_U16, 1974b6cc728SJohannes Berg * NLA_U32, NLA_U64, 1984778e0beSJiri Pirko * NLA_S8, NLA_S16, 1994778e0beSJiri Pirko * NLA_S32, NLA_S64, 2004b6cc728SJohannes Berg * NLA_MSECS Leaving the length field zero will verify the 2014b6cc728SJohannes Berg * given type fits, using it verifies minimum length 2024b6cc728SJohannes Berg * just like "All other" 2034b6cc728SJohannes Berg * All other Minimum length of attribute payload 204a5531a5dSThomas Graf * 205bfa83a9eSThomas Graf * Example: 206b54452b0SAlexey Dobriyan * static const struct nla_policy my_policy[ATTR_MAX+1] = { 207bfa83a9eSThomas Graf * [ATTR_FOO] = { .type = NLA_U16 }, 208d30045a0SJohannes Berg * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ }, 209a5531a5dSThomas Graf * [ATTR_BAZ] = { .len = sizeof(struct mystruct) }, 210bfa83a9eSThomas Graf * }; 211bfa83a9eSThomas Graf */ 212bfa83a9eSThomas Graf struct nla_policy { 213bfa83a9eSThomas Graf u16 type; 214a5531a5dSThomas Graf u16 len; 215bfa83a9eSThomas Graf }; 216bfa83a9eSThomas Graf 2174e902c57SThomas Graf /** 2184e902c57SThomas Graf * struct nl_info - netlink source information 2194e902c57SThomas Graf * @nlh: Netlink message header of original request 22015e47304SEric W. Biederman * @portid: Netlink PORTID of requesting application 2214e902c57SThomas Graf */ 2224e902c57SThomas Graf struct nl_info { 2234e902c57SThomas Graf struct nlmsghdr *nlh; 2244d1169c1SDenis V. Lunev struct net *nl_net; 22515e47304SEric W. Biederman u32 portid; 2264e902c57SThomas Graf }; 2274e902c57SThomas Graf 2284f69053bSJoe Perches int netlink_rcv_skb(struct sk_buff *skb, 2294f69053bSJoe Perches int (*cb)(struct sk_buff *, struct nlmsghdr *)); 2304f69053bSJoe Perches int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid, 2314f69053bSJoe Perches unsigned int group, int report, gfp_t flags); 23282ace47aSThomas Graf 2334f69053bSJoe Perches int nla_validate(const struct nlattr *head, int len, int maxtype, 234ef7c79edSPatrick McHardy const struct nla_policy *policy); 2354f69053bSJoe Perches int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, 2364f69053bSJoe Perches int len, const struct nla_policy *policy); 2374f69053bSJoe Perches int nla_policy_len(const struct nla_policy *, int); 2384f69053bSJoe Perches struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype); 2394f69053bSJoe Perches size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize); 2404f69053bSJoe Perches int nla_memcpy(void *dest, const struct nlattr *src, int count); 2414f69053bSJoe Perches int nla_memcmp(const struct nlattr *nla, const void *data, size_t size); 2424f69053bSJoe Perches int nla_strcmp(const struct nlattr *nla, const char *str); 2434f69053bSJoe Perches struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 2444f69053bSJoe Perches void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 2454f69053bSJoe Perches struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 2464f69053bSJoe Perches void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 2474f69053bSJoe Perches void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, 248fe4944e5SThomas Graf const void *data); 2494f69053bSJoe Perches void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 2504f69053bSJoe Perches int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data); 2514f69053bSJoe Perches int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 2524f69053bSJoe Perches int nla_append(struct sk_buff *skb, int attrlen, const void *data); 253bfa83a9eSThomas Graf 254bfa83a9eSThomas Graf /************************************************************************** 255bfa83a9eSThomas Graf * Netlink Messages 256bfa83a9eSThomas Graf **************************************************************************/ 257bfa83a9eSThomas Graf 258bfa83a9eSThomas Graf /** 259bfa83a9eSThomas Graf * nlmsg_msg_size - length of netlink message not including padding 260bfa83a9eSThomas Graf * @payload: length of message payload 261bfa83a9eSThomas Graf */ 262bfa83a9eSThomas Graf static inline int nlmsg_msg_size(int payload) 263bfa83a9eSThomas Graf { 264bfa83a9eSThomas Graf return NLMSG_HDRLEN + payload; 265bfa83a9eSThomas Graf } 266bfa83a9eSThomas Graf 267bfa83a9eSThomas Graf /** 268bfa83a9eSThomas Graf * nlmsg_total_size - length of netlink message including padding 269bfa83a9eSThomas Graf * @payload: length of message payload 270bfa83a9eSThomas Graf */ 271bfa83a9eSThomas Graf static inline int nlmsg_total_size(int payload) 272bfa83a9eSThomas Graf { 273bfa83a9eSThomas Graf return NLMSG_ALIGN(nlmsg_msg_size(payload)); 274bfa83a9eSThomas Graf } 275bfa83a9eSThomas Graf 276bfa83a9eSThomas Graf /** 277bfa83a9eSThomas Graf * nlmsg_padlen - length of padding at the message's tail 278bfa83a9eSThomas Graf * @payload: length of message payload 279bfa83a9eSThomas Graf */ 280bfa83a9eSThomas Graf static inline int nlmsg_padlen(int payload) 281bfa83a9eSThomas Graf { 282bfa83a9eSThomas Graf return nlmsg_total_size(payload) - nlmsg_msg_size(payload); 283bfa83a9eSThomas Graf } 284bfa83a9eSThomas Graf 285bfa83a9eSThomas Graf /** 286bfa83a9eSThomas Graf * nlmsg_data - head of message payload 28770f23fd6SJustin P. Mattock * @nlh: netlink message header 288bfa83a9eSThomas Graf */ 289bfa83a9eSThomas Graf static inline void *nlmsg_data(const struct nlmsghdr *nlh) 290bfa83a9eSThomas Graf { 291bfa83a9eSThomas Graf return (unsigned char *) nlh + NLMSG_HDRLEN; 292bfa83a9eSThomas Graf } 293bfa83a9eSThomas Graf 294bfa83a9eSThomas Graf /** 295bfa83a9eSThomas Graf * nlmsg_len - length of message payload 296bfa83a9eSThomas Graf * @nlh: netlink message header 297bfa83a9eSThomas Graf */ 298bfa83a9eSThomas Graf static inline int nlmsg_len(const struct nlmsghdr *nlh) 299bfa83a9eSThomas Graf { 300bfa83a9eSThomas Graf return nlh->nlmsg_len - NLMSG_HDRLEN; 301bfa83a9eSThomas Graf } 302bfa83a9eSThomas Graf 303bfa83a9eSThomas Graf /** 304bfa83a9eSThomas Graf * nlmsg_attrdata - head of attributes data 305bfa83a9eSThomas Graf * @nlh: netlink message header 306bfa83a9eSThomas Graf * @hdrlen: length of family specific header 307bfa83a9eSThomas Graf */ 308bfa83a9eSThomas Graf static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, 309bfa83a9eSThomas Graf int hdrlen) 310bfa83a9eSThomas Graf { 311bfa83a9eSThomas Graf unsigned char *data = nlmsg_data(nlh); 312bfa83a9eSThomas Graf return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen)); 313bfa83a9eSThomas Graf } 314bfa83a9eSThomas Graf 315bfa83a9eSThomas Graf /** 316bfa83a9eSThomas Graf * nlmsg_attrlen - length of attributes data 317bfa83a9eSThomas Graf * @nlh: netlink message header 318bfa83a9eSThomas Graf * @hdrlen: length of family specific header 319bfa83a9eSThomas Graf */ 320bfa83a9eSThomas Graf static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen) 321bfa83a9eSThomas Graf { 322bfa83a9eSThomas Graf return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen); 323bfa83a9eSThomas Graf } 324bfa83a9eSThomas Graf 325bfa83a9eSThomas Graf /** 326bfa83a9eSThomas Graf * nlmsg_ok - check if the netlink message fits into the remaining bytes 327bfa83a9eSThomas Graf * @nlh: netlink message header 328bfa83a9eSThomas Graf * @remaining: number of bytes remaining in message stream 329bfa83a9eSThomas Graf */ 330bfa83a9eSThomas Graf static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining) 331bfa83a9eSThomas Graf { 332619e803dSVegard Nossum return (remaining >= (int) sizeof(struct nlmsghdr) && 333bfa83a9eSThomas Graf nlh->nlmsg_len >= sizeof(struct nlmsghdr) && 334bfa83a9eSThomas Graf nlh->nlmsg_len <= remaining); 335bfa83a9eSThomas Graf } 336bfa83a9eSThomas Graf 337bfa83a9eSThomas Graf /** 338bfa83a9eSThomas Graf * nlmsg_next - next netlink message in message stream 339bfa83a9eSThomas Graf * @nlh: netlink message header 340bfa83a9eSThomas Graf * @remaining: number of bytes remaining in message stream 341bfa83a9eSThomas Graf * 342bfa83a9eSThomas Graf * Returns the next netlink message in the message stream and 343bfa83a9eSThomas Graf * decrements remaining by the size of the current message. 344bfa83a9eSThomas Graf */ 3453654654fSJan Engelhardt static inline struct nlmsghdr * 3463654654fSJan Engelhardt nlmsg_next(const struct nlmsghdr *nlh, int *remaining) 347bfa83a9eSThomas Graf { 348bfa83a9eSThomas Graf int totlen = NLMSG_ALIGN(nlh->nlmsg_len); 349bfa83a9eSThomas Graf 350bfa83a9eSThomas Graf *remaining -= totlen; 351bfa83a9eSThomas Graf 352bfa83a9eSThomas Graf return (struct nlmsghdr *) ((unsigned char *) nlh + totlen); 353bfa83a9eSThomas Graf } 354bfa83a9eSThomas Graf 355bfa83a9eSThomas Graf /** 356bfa83a9eSThomas Graf * nlmsg_parse - parse attributes of a netlink message 357bfa83a9eSThomas Graf * @nlh: netlink message header 358bfa83a9eSThomas Graf * @hdrlen: length of family specific header 359bfa83a9eSThomas Graf * @tb: destination array with maxtype+1 elements 360bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 361bfa83a9eSThomas Graf * @policy: validation policy 362bfa83a9eSThomas Graf * 363bfa83a9eSThomas Graf * See nla_parse() 364bfa83a9eSThomas Graf */ 3653a6c2b41SPatrick McHardy static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen, 366bfa83a9eSThomas Graf struct nlattr *tb[], int maxtype, 367ef7c79edSPatrick McHardy const struct nla_policy *policy) 368bfa83a9eSThomas Graf { 369bfa83a9eSThomas Graf if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) 370bfa83a9eSThomas Graf return -EINVAL; 371bfa83a9eSThomas Graf 372bfa83a9eSThomas Graf return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), 373bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), policy); 374bfa83a9eSThomas Graf } 375bfa83a9eSThomas Graf 376bfa83a9eSThomas Graf /** 377bfa83a9eSThomas Graf * nlmsg_find_attr - find a specific attribute in a netlink message 378bfa83a9eSThomas Graf * @nlh: netlink message header 379bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 380bfa83a9eSThomas Graf * @attrtype: type of attribute to look for 381bfa83a9eSThomas Graf * 382bfa83a9eSThomas Graf * Returns the first attribute which matches the specified type. 383bfa83a9eSThomas Graf */ 3846b8c92baSNelson Elhage static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh, 385bfa83a9eSThomas Graf int hdrlen, int attrtype) 386bfa83a9eSThomas Graf { 387bfa83a9eSThomas Graf return nla_find(nlmsg_attrdata(nlh, hdrlen), 388bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), attrtype); 389bfa83a9eSThomas Graf } 390bfa83a9eSThomas Graf 391bfa83a9eSThomas Graf /** 392bfa83a9eSThomas Graf * nlmsg_validate - validate a netlink message including attributes 393bfa83a9eSThomas Graf * @nlh: netlinket message header 394bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 395bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 396bfa83a9eSThomas Graf * @policy: validation policy 397bfa83a9eSThomas Graf */ 3983654654fSJan Engelhardt static inline int nlmsg_validate(const struct nlmsghdr *nlh, 3993654654fSJan Engelhardt int hdrlen, int maxtype, 400ef7c79edSPatrick McHardy const struct nla_policy *policy) 401bfa83a9eSThomas Graf { 402bfa83a9eSThomas Graf if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) 403bfa83a9eSThomas Graf return -EINVAL; 404bfa83a9eSThomas Graf 405bfa83a9eSThomas Graf return nla_validate(nlmsg_attrdata(nlh, hdrlen), 406bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), maxtype, policy); 407bfa83a9eSThomas Graf } 408bfa83a9eSThomas Graf 409bfa83a9eSThomas Graf /** 41097676b6bSThomas Graf * nlmsg_report - need to report back to application? 41197676b6bSThomas Graf * @nlh: netlink message header 41297676b6bSThomas Graf * 41397676b6bSThomas Graf * Returns 1 if a report back to the application is requested. 41497676b6bSThomas Graf */ 4153a6c2b41SPatrick McHardy static inline int nlmsg_report(const struct nlmsghdr *nlh) 41697676b6bSThomas Graf { 41797676b6bSThomas Graf return !!(nlh->nlmsg_flags & NLM_F_ECHO); 41897676b6bSThomas Graf } 41997676b6bSThomas Graf 42097676b6bSThomas Graf /** 421bfa83a9eSThomas Graf * nlmsg_for_each_attr - iterate over a stream of attributes 422bfa83a9eSThomas Graf * @pos: loop counter, set to current attribute 423bfa83a9eSThomas Graf * @nlh: netlink message header 424bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 425bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 426bfa83a9eSThomas Graf */ 427bfa83a9eSThomas Graf #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \ 428bfa83a9eSThomas Graf nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \ 429bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), rem) 430bfa83a9eSThomas Graf 431bfa83a9eSThomas Graf /** 432bfa83a9eSThomas Graf * nlmsg_put - Add a new netlink message to an skb 433bfa83a9eSThomas Graf * @skb: socket buffer to store message in 4342c6ba4b1SNicolas Dichtel * @portid: netlink PORTID of requesting application 435bfa83a9eSThomas Graf * @seq: sequence number of message 436bfa83a9eSThomas Graf * @type: message type 437bfa83a9eSThomas Graf * @payload: length of message payload 438bfa83a9eSThomas Graf * @flags: message flags 439bfa83a9eSThomas Graf * 440bfa83a9eSThomas Graf * Returns NULL if the tailroom of the skb is insufficient to store 441bfa83a9eSThomas Graf * the message header and payload. 442bfa83a9eSThomas Graf */ 44315e47304SEric W. Biederman static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, 444bfa83a9eSThomas Graf int type, int payload, int flags) 445bfa83a9eSThomas Graf { 446bfa83a9eSThomas Graf if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload))) 447bfa83a9eSThomas Graf return NULL; 448bfa83a9eSThomas Graf 44915e47304SEric W. Biederman return __nlmsg_put(skb, portid, seq, type, payload, flags); 450bfa83a9eSThomas Graf } 451bfa83a9eSThomas Graf 452bfa83a9eSThomas Graf /** 453bfa83a9eSThomas Graf * nlmsg_put_answer - Add a new callback based netlink message to an skb 454bfa83a9eSThomas Graf * @skb: socket buffer to store message in 455bfa83a9eSThomas Graf * @cb: netlink callback 456bfa83a9eSThomas Graf * @type: message type 457bfa83a9eSThomas Graf * @payload: length of message payload 458bfa83a9eSThomas Graf * @flags: message flags 459bfa83a9eSThomas Graf * 460bfa83a9eSThomas Graf * Returns NULL if the tailroom of the skb is insufficient to store 461bfa83a9eSThomas Graf * the message header and payload. 462bfa83a9eSThomas Graf */ 463bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb, 464bfa83a9eSThomas Graf struct netlink_callback *cb, 465bfa83a9eSThomas Graf int type, int payload, 466bfa83a9eSThomas Graf int flags) 467bfa83a9eSThomas Graf { 46815e47304SEric W. Biederman return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 469bfa83a9eSThomas Graf type, payload, flags); 470bfa83a9eSThomas Graf } 471bfa83a9eSThomas Graf 472bfa83a9eSThomas Graf /** 473bfa83a9eSThomas Graf * nlmsg_new - Allocate a new netlink message 474339bf98fSThomas Graf * @payload: size of the message payload 475fe4944e5SThomas Graf * @flags: the type of memory to allocate. 476bfa83a9eSThomas Graf * 477339bf98fSThomas Graf * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known 478339bf98fSThomas Graf * and a good default is needed. 479bfa83a9eSThomas Graf */ 480339bf98fSThomas Graf static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags) 481bfa83a9eSThomas Graf { 482339bf98fSThomas Graf return alloc_skb(nlmsg_total_size(payload), flags); 483bfa83a9eSThomas Graf } 484bfa83a9eSThomas Graf 485bfa83a9eSThomas Graf /** 486bfa83a9eSThomas Graf * nlmsg_end - Finalize a netlink message 487bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 488bfa83a9eSThomas Graf * @nlh: netlink message header 489bfa83a9eSThomas Graf * 490bfa83a9eSThomas Graf * Corrects the netlink message header to include the appeneded 491bfa83a9eSThomas Graf * attributes. Only necessary if attributes have been added to 492bfa83a9eSThomas Graf * the message. 493bfa83a9eSThomas Graf */ 494053c095aSJohannes Berg static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh) 495bfa83a9eSThomas Graf { 49627a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh; 497bfa83a9eSThomas Graf } 498bfa83a9eSThomas Graf 499bfa83a9eSThomas Graf /** 500fe4944e5SThomas Graf * nlmsg_get_pos - return current position in netlink message 501fe4944e5SThomas Graf * @skb: socket buffer the message is stored in 502fe4944e5SThomas Graf * 503fe4944e5SThomas Graf * Returns a pointer to the current tail of the message. 504fe4944e5SThomas Graf */ 505fe4944e5SThomas Graf static inline void *nlmsg_get_pos(struct sk_buff *skb) 506fe4944e5SThomas Graf { 50727a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 508fe4944e5SThomas Graf } 509fe4944e5SThomas Graf 510fe4944e5SThomas Graf /** 511fe4944e5SThomas Graf * nlmsg_trim - Trim message to a mark 512fe4944e5SThomas Graf * @skb: socket buffer the message is stored in 513fe4944e5SThomas Graf * @mark: mark to trim to 514fe4944e5SThomas Graf * 515bc3ed28cSThomas Graf * Trims the message to the provided mark. 516fe4944e5SThomas Graf */ 517bc3ed28cSThomas Graf static inline void nlmsg_trim(struct sk_buff *skb, const void *mark) 518fe4944e5SThomas Graf { 519149118d8SThomas Graf if (mark) { 520149118d8SThomas Graf WARN_ON((unsigned char *) mark < skb->data); 521fe4944e5SThomas Graf skb_trim(skb, (unsigned char *) mark - skb->data); 522fe4944e5SThomas Graf } 523149118d8SThomas Graf } 524fe4944e5SThomas Graf 525fe4944e5SThomas Graf /** 526bfa83a9eSThomas Graf * nlmsg_cancel - Cancel construction of a netlink message 527bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 528bfa83a9eSThomas Graf * @nlh: netlink message header 529bfa83a9eSThomas Graf * 530bfa83a9eSThomas Graf * Removes the complete netlink message including all 531bc3ed28cSThomas Graf * attributes from the socket buffer again. 532bfa83a9eSThomas Graf */ 533bc3ed28cSThomas Graf static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh) 534bfa83a9eSThomas Graf { 535bc3ed28cSThomas Graf nlmsg_trim(skb, nlh); 536bfa83a9eSThomas Graf } 537bfa83a9eSThomas Graf 538bfa83a9eSThomas Graf /** 539bfa83a9eSThomas Graf * nlmsg_free - free a netlink message 540bfa83a9eSThomas Graf * @skb: socket buffer of netlink message 541bfa83a9eSThomas Graf */ 542bfa83a9eSThomas Graf static inline void nlmsg_free(struct sk_buff *skb) 543bfa83a9eSThomas Graf { 544bfa83a9eSThomas Graf kfree_skb(skb); 545bfa83a9eSThomas Graf } 546bfa83a9eSThomas Graf 547bfa83a9eSThomas Graf /** 548bfa83a9eSThomas Graf * nlmsg_multicast - multicast a netlink message 549bfa83a9eSThomas Graf * @sk: netlink socket to spread messages to 550bfa83a9eSThomas Graf * @skb: netlink message as socket buffer 55115e47304SEric W. Biederman * @portid: own netlink portid to avoid sending to yourself 552bfa83a9eSThomas Graf * @group: multicast group id 553d387f6adSThomas Graf * @flags: allocation flags 554bfa83a9eSThomas Graf */ 555bfa83a9eSThomas Graf static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, 55615e47304SEric W. Biederman u32 portid, unsigned int group, gfp_t flags) 557bfa83a9eSThomas Graf { 558bfa83a9eSThomas Graf int err; 559bfa83a9eSThomas Graf 560bfa83a9eSThomas Graf NETLINK_CB(skb).dst_group = group; 561bfa83a9eSThomas Graf 56215e47304SEric W. Biederman err = netlink_broadcast(sk, skb, portid, group, flags); 563bfa83a9eSThomas Graf if (err > 0) 564bfa83a9eSThomas Graf err = 0; 565bfa83a9eSThomas Graf 566bfa83a9eSThomas Graf return err; 567bfa83a9eSThomas Graf } 568bfa83a9eSThomas Graf 569bfa83a9eSThomas Graf /** 570bfa83a9eSThomas Graf * nlmsg_unicast - unicast a netlink message 571bfa83a9eSThomas Graf * @sk: netlink socket to spread message to 572bfa83a9eSThomas Graf * @skb: netlink message as socket buffer 57315e47304SEric W. Biederman * @portid: netlink portid of the destination socket 574bfa83a9eSThomas Graf */ 57515e47304SEric W. Biederman static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid) 576bfa83a9eSThomas Graf { 577bfa83a9eSThomas Graf int err; 578bfa83a9eSThomas Graf 57915e47304SEric W. Biederman err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT); 580bfa83a9eSThomas Graf if (err > 0) 581bfa83a9eSThomas Graf err = 0; 582bfa83a9eSThomas Graf 583bfa83a9eSThomas Graf return err; 584bfa83a9eSThomas Graf } 585bfa83a9eSThomas Graf 586bfa83a9eSThomas Graf /** 587bfa83a9eSThomas Graf * nlmsg_for_each_msg - iterate over a stream of messages 588bfa83a9eSThomas Graf * @pos: loop counter, set to current message 589bfa83a9eSThomas Graf * @head: head of message stream 590bfa83a9eSThomas Graf * @len: length of message stream 591bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 592bfa83a9eSThomas Graf */ 593bfa83a9eSThomas Graf #define nlmsg_for_each_msg(pos, head, len, rem) \ 594bfa83a9eSThomas Graf for (pos = head, rem = len; \ 595bfa83a9eSThomas Graf nlmsg_ok(pos, rem); \ 596bfa83a9eSThomas Graf pos = nlmsg_next(pos, &(rem))) 597bfa83a9eSThomas Graf 598670dc283SJohannes Berg /** 599670dc283SJohannes Berg * nl_dump_check_consistent - check if sequence is consistent and advertise if not 600670dc283SJohannes Berg * @cb: netlink callback structure that stores the sequence number 601670dc283SJohannes Berg * @nlh: netlink message header to write the flag to 602670dc283SJohannes Berg * 603670dc283SJohannes Berg * This function checks if the sequence (generation) number changed during dump 604670dc283SJohannes Berg * and if it did, advertises it in the netlink message header. 605670dc283SJohannes Berg * 606670dc283SJohannes Berg * The correct way to use it is to set cb->seq to the generation counter when 607670dc283SJohannes Berg * all locks for dumping have been acquired, and then call this function for 608670dc283SJohannes Berg * each message that is generated. 609670dc283SJohannes Berg * 610670dc283SJohannes Berg * Note that due to initialisation concerns, 0 is an invalid sequence number 611670dc283SJohannes Berg * and must not be used by code that uses this functionality. 612670dc283SJohannes Berg */ 613670dc283SJohannes Berg static inline void 614670dc283SJohannes Berg nl_dump_check_consistent(struct netlink_callback *cb, 615670dc283SJohannes Berg struct nlmsghdr *nlh) 616670dc283SJohannes Berg { 617670dc283SJohannes Berg if (cb->prev_seq && cb->seq != cb->prev_seq) 618670dc283SJohannes Berg nlh->nlmsg_flags |= NLM_F_DUMP_INTR; 619670dc283SJohannes Berg cb->prev_seq = cb->seq; 620670dc283SJohannes Berg } 621670dc283SJohannes Berg 622bfa83a9eSThomas Graf /************************************************************************** 623bfa83a9eSThomas Graf * Netlink Attributes 624bfa83a9eSThomas Graf **************************************************************************/ 625bfa83a9eSThomas Graf 626bfa83a9eSThomas Graf /** 627bfa83a9eSThomas Graf * nla_attr_size - length of attribute not including padding 628bfa83a9eSThomas Graf * @payload: length of payload 629bfa83a9eSThomas Graf */ 630bfa83a9eSThomas Graf static inline int nla_attr_size(int payload) 631bfa83a9eSThomas Graf { 632bfa83a9eSThomas Graf return NLA_HDRLEN + payload; 633bfa83a9eSThomas Graf } 634bfa83a9eSThomas Graf 635bfa83a9eSThomas Graf /** 636bfa83a9eSThomas Graf * nla_total_size - total length of attribute including padding 637bfa83a9eSThomas Graf * @payload: length of payload 638bfa83a9eSThomas Graf */ 639bfa83a9eSThomas Graf static inline int nla_total_size(int payload) 640bfa83a9eSThomas Graf { 641bfa83a9eSThomas Graf return NLA_ALIGN(nla_attr_size(payload)); 642bfa83a9eSThomas Graf } 643bfa83a9eSThomas Graf 644bfa83a9eSThomas Graf /** 645bfa83a9eSThomas Graf * nla_padlen - length of padding at the tail of attribute 646bfa83a9eSThomas Graf * @payload: length of payload 647bfa83a9eSThomas Graf */ 648bfa83a9eSThomas Graf static inline int nla_padlen(int payload) 649bfa83a9eSThomas Graf { 650bfa83a9eSThomas Graf return nla_total_size(payload) - nla_attr_size(payload); 651bfa83a9eSThomas Graf } 652bfa83a9eSThomas Graf 653bfa83a9eSThomas Graf /** 6548f4c1f9bSThomas Graf * nla_type - attribute type 6558f4c1f9bSThomas Graf * @nla: netlink attribute 6568f4c1f9bSThomas Graf */ 6578f4c1f9bSThomas Graf static inline int nla_type(const struct nlattr *nla) 6588f4c1f9bSThomas Graf { 6598f4c1f9bSThomas Graf return nla->nla_type & NLA_TYPE_MASK; 6608f4c1f9bSThomas Graf } 6618f4c1f9bSThomas Graf 6628f4c1f9bSThomas Graf /** 663bfa83a9eSThomas Graf * nla_data - head of payload 664bfa83a9eSThomas Graf * @nla: netlink attribute 665bfa83a9eSThomas Graf */ 666bfa83a9eSThomas Graf static inline void *nla_data(const struct nlattr *nla) 667bfa83a9eSThomas Graf { 668bfa83a9eSThomas Graf return (char *) nla + NLA_HDRLEN; 669bfa83a9eSThomas Graf } 670bfa83a9eSThomas Graf 671bfa83a9eSThomas Graf /** 672bfa83a9eSThomas Graf * nla_len - length of payload 673bfa83a9eSThomas Graf * @nla: netlink attribute 674bfa83a9eSThomas Graf */ 675bfa83a9eSThomas Graf static inline int nla_len(const struct nlattr *nla) 676bfa83a9eSThomas Graf { 677bfa83a9eSThomas Graf return nla->nla_len - NLA_HDRLEN; 678bfa83a9eSThomas Graf } 679bfa83a9eSThomas Graf 680bfa83a9eSThomas Graf /** 681bfa83a9eSThomas Graf * nla_ok - check if the netlink attribute fits into the remaining bytes 682bfa83a9eSThomas Graf * @nla: netlink attribute 683bfa83a9eSThomas Graf * @remaining: number of bytes remaining in attribute stream 684bfa83a9eSThomas Graf */ 685bfa83a9eSThomas Graf static inline int nla_ok(const struct nlattr *nla, int remaining) 686bfa83a9eSThomas Graf { 6871045b03eSVegard Nossum return remaining >= (int) sizeof(*nla) && 688bfa83a9eSThomas Graf nla->nla_len >= sizeof(*nla) && 689bfa83a9eSThomas Graf nla->nla_len <= remaining; 690bfa83a9eSThomas Graf } 691bfa83a9eSThomas Graf 692bfa83a9eSThomas Graf /** 693d1ec3b77SPierre Ynard * nla_next - next netlink attribute in attribute stream 694bfa83a9eSThomas Graf * @nla: netlink attribute 695bfa83a9eSThomas Graf * @remaining: number of bytes remaining in attribute stream 696bfa83a9eSThomas Graf * 697bfa83a9eSThomas Graf * Returns the next netlink attribute in the attribute stream and 698bfa83a9eSThomas Graf * decrements remaining by the size of the current attribute. 699bfa83a9eSThomas Graf */ 700bfa83a9eSThomas Graf static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining) 701bfa83a9eSThomas Graf { 702bfa83a9eSThomas Graf int totlen = NLA_ALIGN(nla->nla_len); 703bfa83a9eSThomas Graf 704bfa83a9eSThomas Graf *remaining -= totlen; 705bfa83a9eSThomas Graf return (struct nlattr *) ((char *) nla + totlen); 706bfa83a9eSThomas Graf } 707bfa83a9eSThomas Graf 708bfa83a9eSThomas Graf /** 709fe4944e5SThomas Graf * nla_find_nested - find attribute in a set of nested attributes 710fe4944e5SThomas Graf * @nla: attribute containing the nested attributes 711fe4944e5SThomas Graf * @attrtype: type of attribute to look for 712fe4944e5SThomas Graf * 713fe4944e5SThomas Graf * Returns the first attribute which matches the specified type. 714fe4944e5SThomas Graf */ 7153654654fSJan Engelhardt static inline struct nlattr * 7163654654fSJan Engelhardt nla_find_nested(const struct nlattr *nla, int attrtype) 717fe4944e5SThomas Graf { 718fe4944e5SThomas Graf return nla_find(nla_data(nla), nla_len(nla), attrtype); 719fe4944e5SThomas Graf } 720fe4944e5SThomas Graf 721fe4944e5SThomas Graf /** 722bfa83a9eSThomas Graf * nla_parse_nested - parse nested attributes 723bfa83a9eSThomas Graf * @tb: destination array with maxtype+1 elements 724bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 725bfa83a9eSThomas Graf * @nla: attribute containing the nested attributes 726bfa83a9eSThomas Graf * @policy: validation policy 727bfa83a9eSThomas Graf * 728bfa83a9eSThomas Graf * See nla_parse() 729bfa83a9eSThomas Graf */ 730bfa83a9eSThomas Graf static inline int nla_parse_nested(struct nlattr *tb[], int maxtype, 731b057efd4SPatrick McHardy const struct nlattr *nla, 732ef7c79edSPatrick McHardy const struct nla_policy *policy) 733bfa83a9eSThomas Graf { 734bfa83a9eSThomas Graf return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy); 735bfa83a9eSThomas Graf } 7361092cb21SPatrick McHardy 7371092cb21SPatrick McHardy /** 738d1ec3b77SPierre Ynard * nla_put_u8 - Add a u8 netlink attribute to a socket buffer 739bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 740bfa83a9eSThomas Graf * @attrtype: attribute type 741bfa83a9eSThomas Graf * @value: numeric value 742bfa83a9eSThomas Graf */ 743bfa83a9eSThomas Graf static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value) 744bfa83a9eSThomas Graf { 745bfa83a9eSThomas Graf return nla_put(skb, attrtype, sizeof(u8), &value); 746bfa83a9eSThomas Graf } 747bfa83a9eSThomas Graf 748bfa83a9eSThomas Graf /** 749bfa83a9eSThomas Graf * nla_put_u16 - Add a u16 netlink attribute to a socket buffer 750bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 751bfa83a9eSThomas Graf * @attrtype: attribute type 752bfa83a9eSThomas Graf * @value: numeric value 753bfa83a9eSThomas Graf */ 754bfa83a9eSThomas Graf static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value) 755bfa83a9eSThomas Graf { 756bfa83a9eSThomas Graf return nla_put(skb, attrtype, sizeof(u16), &value); 757bfa83a9eSThomas Graf } 758bfa83a9eSThomas Graf 759bfa83a9eSThomas Graf /** 760569a8fc3SDavid S. Miller * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer 761569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 762569a8fc3SDavid S. Miller * @attrtype: attribute type 763569a8fc3SDavid S. Miller * @value: numeric value 764569a8fc3SDavid S. Miller */ 765569a8fc3SDavid S. Miller static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value) 766569a8fc3SDavid S. Miller { 767569a8fc3SDavid S. Miller return nla_put(skb, attrtype, sizeof(__be16), &value); 768569a8fc3SDavid S. Miller } 769569a8fc3SDavid S. Miller 770569a8fc3SDavid S. Miller /** 7716c1dd3b6SDavid S. Miller * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer 7726c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 7736c1dd3b6SDavid S. Miller * @attrtype: attribute type 7746c1dd3b6SDavid S. Miller * @value: numeric value 7756c1dd3b6SDavid S. Miller */ 7766c1dd3b6SDavid S. Miller static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value) 7776c1dd3b6SDavid S. Miller { 7786c1dd3b6SDavid S. Miller return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, value); 7796c1dd3b6SDavid S. Miller } 7806c1dd3b6SDavid S. Miller 7816c1dd3b6SDavid S. Miller /** 78224c410dcSDavid S. Miller * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer 78324c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 78424c410dcSDavid S. Miller * @attrtype: attribute type 78524c410dcSDavid S. Miller * @value: numeric value 78624c410dcSDavid S. Miller */ 78724c410dcSDavid S. Miller static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value) 78824c410dcSDavid S. Miller { 78924c410dcSDavid S. Miller return nla_put(skb, attrtype, sizeof(__le16), &value); 79024c410dcSDavid S. Miller } 79124c410dcSDavid S. Miller 79224c410dcSDavid S. Miller /** 793bfa83a9eSThomas Graf * nla_put_u32 - Add a u32 netlink attribute to a socket buffer 794bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 795bfa83a9eSThomas Graf * @attrtype: attribute type 796bfa83a9eSThomas Graf * @value: numeric value 797bfa83a9eSThomas Graf */ 798bfa83a9eSThomas Graf static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value) 799bfa83a9eSThomas Graf { 800bfa83a9eSThomas Graf return nla_put(skb, attrtype, sizeof(u32), &value); 801bfa83a9eSThomas Graf } 802bfa83a9eSThomas Graf 803bfa83a9eSThomas Graf /** 804569a8fc3SDavid S. Miller * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer 805569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 806569a8fc3SDavid S. Miller * @attrtype: attribute type 807569a8fc3SDavid S. Miller * @value: numeric value 808569a8fc3SDavid S. Miller */ 809569a8fc3SDavid S. Miller static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value) 810569a8fc3SDavid S. Miller { 811569a8fc3SDavid S. Miller return nla_put(skb, attrtype, sizeof(__be32), &value); 812569a8fc3SDavid S. Miller } 813569a8fc3SDavid S. Miller 814569a8fc3SDavid S. Miller /** 8156c1dd3b6SDavid S. Miller * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer 8166c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 8176c1dd3b6SDavid S. Miller * @attrtype: attribute type 8186c1dd3b6SDavid S. Miller * @value: numeric value 8196c1dd3b6SDavid S. Miller */ 8206c1dd3b6SDavid S. Miller static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value) 8216c1dd3b6SDavid S. Miller { 8226c1dd3b6SDavid S. Miller return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, value); 8236c1dd3b6SDavid S. Miller } 8246c1dd3b6SDavid S. Miller 8256c1dd3b6SDavid S. Miller /** 82624c410dcSDavid S. Miller * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer 82724c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 82824c410dcSDavid S. Miller * @attrtype: attribute type 82924c410dcSDavid S. Miller * @value: numeric value 83024c410dcSDavid S. Miller */ 83124c410dcSDavid S. Miller static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value) 83224c410dcSDavid S. Miller { 83324c410dcSDavid S. Miller return nla_put(skb, attrtype, sizeof(__le32), &value); 83424c410dcSDavid S. Miller } 83524c410dcSDavid S. Miller 83624c410dcSDavid S. Miller /** 837569a8fc3SDavid S. Miller * nla_put_u64 - Add a u64 netlink attribute to a socket buffer 838bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 839bfa83a9eSThomas Graf * @attrtype: attribute type 840bfa83a9eSThomas Graf * @value: numeric value 841bfa83a9eSThomas Graf */ 842bfa83a9eSThomas Graf static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value) 843bfa83a9eSThomas Graf { 844bfa83a9eSThomas Graf return nla_put(skb, attrtype, sizeof(u64), &value); 845bfa83a9eSThomas Graf } 846bfa83a9eSThomas Graf 847bfa83a9eSThomas Graf /** 848569a8fc3SDavid S. Miller * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer 849569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 850569a8fc3SDavid S. Miller * @attrtype: attribute type 851569a8fc3SDavid S. Miller * @value: numeric value 852569a8fc3SDavid S. Miller */ 853569a8fc3SDavid S. Miller static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value) 854569a8fc3SDavid S. Miller { 855569a8fc3SDavid S. Miller return nla_put(skb, attrtype, sizeof(__be64), &value); 856569a8fc3SDavid S. Miller } 857569a8fc3SDavid S. Miller 858569a8fc3SDavid S. Miller /** 8596c1dd3b6SDavid S. Miller * nla_put_net64 - Add 64-bit network byte order netlink attribute to a socket buffer 8606c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 8616c1dd3b6SDavid S. Miller * @attrtype: attribute type 8626c1dd3b6SDavid S. Miller * @value: numeric value 8636c1dd3b6SDavid S. Miller */ 8646c1dd3b6SDavid S. Miller static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value) 8656c1dd3b6SDavid S. Miller { 8666c1dd3b6SDavid S. Miller return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, value); 8676c1dd3b6SDavid S. Miller } 8686c1dd3b6SDavid S. Miller 8696c1dd3b6SDavid S. Miller /** 87024c410dcSDavid S. Miller * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer 87124c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 87224c410dcSDavid S. Miller * @attrtype: attribute type 87324c410dcSDavid S. Miller * @value: numeric value 87424c410dcSDavid S. Miller */ 87524c410dcSDavid S. Miller static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value) 87624c410dcSDavid S. Miller { 87724c410dcSDavid S. Miller return nla_put(skb, attrtype, sizeof(__le64), &value); 87824c410dcSDavid S. Miller } 87924c410dcSDavid S. Miller 88024c410dcSDavid S. Miller /** 8814778e0beSJiri Pirko * nla_put_s8 - Add a s8 netlink attribute to a socket buffer 8824778e0beSJiri Pirko * @skb: socket buffer to add attribute to 8834778e0beSJiri Pirko * @attrtype: attribute type 8844778e0beSJiri Pirko * @value: numeric value 8854778e0beSJiri Pirko */ 8864778e0beSJiri Pirko static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value) 8874778e0beSJiri Pirko { 8884778e0beSJiri Pirko return nla_put(skb, attrtype, sizeof(s8), &value); 8894778e0beSJiri Pirko } 8904778e0beSJiri Pirko 8914778e0beSJiri Pirko /** 8924778e0beSJiri Pirko * nla_put_s16 - Add a s16 netlink attribute to a socket buffer 8934778e0beSJiri Pirko * @skb: socket buffer to add attribute to 8944778e0beSJiri Pirko * @attrtype: attribute type 8954778e0beSJiri Pirko * @value: numeric value 8964778e0beSJiri Pirko */ 8974778e0beSJiri Pirko static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value) 8984778e0beSJiri Pirko { 8994778e0beSJiri Pirko return nla_put(skb, attrtype, sizeof(s16), &value); 9004778e0beSJiri Pirko } 9014778e0beSJiri Pirko 9024778e0beSJiri Pirko /** 9034778e0beSJiri Pirko * nla_put_s32 - Add a s32 netlink attribute to a socket buffer 9044778e0beSJiri Pirko * @skb: socket buffer to add attribute to 9054778e0beSJiri Pirko * @attrtype: attribute type 9064778e0beSJiri Pirko * @value: numeric value 9074778e0beSJiri Pirko */ 9084778e0beSJiri Pirko static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value) 9094778e0beSJiri Pirko { 9104778e0beSJiri Pirko return nla_put(skb, attrtype, sizeof(s32), &value); 9114778e0beSJiri Pirko } 9124778e0beSJiri Pirko 9134778e0beSJiri Pirko /** 9144778e0beSJiri Pirko * nla_put_s64 - Add a s64 netlink attribute to a socket buffer 9154778e0beSJiri Pirko * @skb: socket buffer to add attribute to 9164778e0beSJiri Pirko * @attrtype: attribute type 9174778e0beSJiri Pirko * @value: numeric value 9184778e0beSJiri Pirko */ 9194778e0beSJiri Pirko static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value) 9204778e0beSJiri Pirko { 9214778e0beSJiri Pirko return nla_put(skb, attrtype, sizeof(s64), &value); 9224778e0beSJiri Pirko } 9234778e0beSJiri Pirko 9244778e0beSJiri Pirko /** 925bfa83a9eSThomas Graf * nla_put_string - Add a string netlink attribute to a socket buffer 926bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 927bfa83a9eSThomas Graf * @attrtype: attribute type 928bfa83a9eSThomas Graf * @str: NUL terminated string 929bfa83a9eSThomas Graf */ 930bfa83a9eSThomas Graf static inline int nla_put_string(struct sk_buff *skb, int attrtype, 931bfa83a9eSThomas Graf const char *str) 932bfa83a9eSThomas Graf { 933bfa83a9eSThomas Graf return nla_put(skb, attrtype, strlen(str) + 1, str); 934bfa83a9eSThomas Graf } 935bfa83a9eSThomas Graf 936bfa83a9eSThomas Graf /** 937bfa83a9eSThomas Graf * nla_put_flag - Add a flag netlink attribute to a socket buffer 938bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 939bfa83a9eSThomas Graf * @attrtype: attribute type 940bfa83a9eSThomas Graf */ 941bfa83a9eSThomas Graf static inline int nla_put_flag(struct sk_buff *skb, int attrtype) 942bfa83a9eSThomas Graf { 943bfa83a9eSThomas Graf return nla_put(skb, attrtype, 0, NULL); 944bfa83a9eSThomas Graf } 945bfa83a9eSThomas Graf 946bfa83a9eSThomas Graf /** 947bfa83a9eSThomas Graf * nla_put_msecs - Add a msecs netlink attribute to a socket buffer 948bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 949bfa83a9eSThomas Graf * @attrtype: attribute type 950d87de1f3SMark Rustad * @njiffies: number of jiffies to convert to msecs 951bfa83a9eSThomas Graf */ 952bfa83a9eSThomas Graf static inline int nla_put_msecs(struct sk_buff *skb, int attrtype, 953d87de1f3SMark Rustad unsigned long njiffies) 954bfa83a9eSThomas Graf { 955d87de1f3SMark Rustad u64 tmp = jiffies_to_msecs(njiffies); 956bfa83a9eSThomas Graf return nla_put(skb, attrtype, sizeof(u64), &tmp); 957bfa83a9eSThomas Graf } 958bfa83a9eSThomas Graf 959bfa83a9eSThomas Graf /** 960bfa83a9eSThomas Graf * nla_get_u32 - return payload of u32 attribute 961bfa83a9eSThomas Graf * @nla: u32 netlink attribute 962bfa83a9eSThomas Graf */ 963b057efd4SPatrick McHardy static inline u32 nla_get_u32(const struct nlattr *nla) 964bfa83a9eSThomas Graf { 965bfa83a9eSThomas Graf return *(u32 *) nla_data(nla); 966bfa83a9eSThomas Graf } 967bfa83a9eSThomas Graf 968bfa83a9eSThomas Graf /** 96900012e5bSAl Viro * nla_get_be32 - return payload of __be32 attribute 97000012e5bSAl Viro * @nla: __be32 netlink attribute 97100012e5bSAl Viro */ 972b057efd4SPatrick McHardy static inline __be32 nla_get_be32(const struct nlattr *nla) 97300012e5bSAl Viro { 97400012e5bSAl Viro return *(__be32 *) nla_data(nla); 97500012e5bSAl Viro } 97600012e5bSAl Viro 97700012e5bSAl Viro /** 978bfa83a9eSThomas Graf * nla_get_u16 - return payload of u16 attribute 979bfa83a9eSThomas Graf * @nla: u16 netlink attribute 980bfa83a9eSThomas Graf */ 981b057efd4SPatrick McHardy static inline u16 nla_get_u16(const struct nlattr *nla) 982bfa83a9eSThomas Graf { 983bfa83a9eSThomas Graf return *(u16 *) nla_data(nla); 984bfa83a9eSThomas Graf } 985bfa83a9eSThomas Graf 986bfa83a9eSThomas Graf /** 987838965baSPatrick McHardy * nla_get_be16 - return payload of __be16 attribute 988838965baSPatrick McHardy * @nla: __be16 netlink attribute 989838965baSPatrick McHardy */ 990b057efd4SPatrick McHardy static inline __be16 nla_get_be16(const struct nlattr *nla) 991838965baSPatrick McHardy { 992838965baSPatrick McHardy return *(__be16 *) nla_data(nla); 993838965baSPatrick McHardy } 994838965baSPatrick McHardy 995838965baSPatrick McHardy /** 9964a89c256SThomas Graf * nla_get_le16 - return payload of __le16 attribute 9974a89c256SThomas Graf * @nla: __le16 netlink attribute 9984a89c256SThomas Graf */ 999b057efd4SPatrick McHardy static inline __le16 nla_get_le16(const struct nlattr *nla) 10004a89c256SThomas Graf { 10014a89c256SThomas Graf return *(__le16 *) nla_data(nla); 10024a89c256SThomas Graf } 10034a89c256SThomas Graf 10044a89c256SThomas Graf /** 1005bfa83a9eSThomas Graf * nla_get_u8 - return payload of u8 attribute 1006bfa83a9eSThomas Graf * @nla: u8 netlink attribute 1007bfa83a9eSThomas Graf */ 1008b057efd4SPatrick McHardy static inline u8 nla_get_u8(const struct nlattr *nla) 1009bfa83a9eSThomas Graf { 1010bfa83a9eSThomas Graf return *(u8 *) nla_data(nla); 1011bfa83a9eSThomas Graf } 1012bfa83a9eSThomas Graf 1013bfa83a9eSThomas Graf /** 1014bfa83a9eSThomas Graf * nla_get_u64 - return payload of u64 attribute 1015bfa83a9eSThomas Graf * @nla: u64 netlink attribute 1016bfa83a9eSThomas Graf */ 1017b057efd4SPatrick McHardy static inline u64 nla_get_u64(const struct nlattr *nla) 1018bfa83a9eSThomas Graf { 1019bfa83a9eSThomas Graf u64 tmp; 1020bfa83a9eSThomas Graf 1021bfa83a9eSThomas Graf nla_memcpy(&tmp, nla, sizeof(tmp)); 1022bfa83a9eSThomas Graf 1023bfa83a9eSThomas Graf return tmp; 1024bfa83a9eSThomas Graf } 1025bfa83a9eSThomas Graf 1026bfa83a9eSThomas Graf /** 1027a17c8598SPablo Neira Ayuso * nla_get_be64 - return payload of __be64 attribute 1028a17c8598SPablo Neira Ayuso * @nla: __be64 netlink attribute 1029a17c8598SPablo Neira Ayuso */ 1030a17c8598SPablo Neira Ayuso static inline __be64 nla_get_be64(const struct nlattr *nla) 1031a17c8598SPablo Neira Ayuso { 1032f5d410f2SPablo Neira Ayuso __be64 tmp; 1033f5d410f2SPablo Neira Ayuso 1034f5d410f2SPablo Neira Ayuso nla_memcpy(&tmp, nla, sizeof(tmp)); 1035f5d410f2SPablo Neira Ayuso 1036f5d410f2SPablo Neira Ayuso return tmp; 1037a17c8598SPablo Neira Ayuso } 1038a17c8598SPablo Neira Ayuso 1039a17c8598SPablo Neira Ayuso /** 10404778e0beSJiri Pirko * nla_get_s32 - return payload of s32 attribute 10414778e0beSJiri Pirko * @nla: s32 netlink attribute 10424778e0beSJiri Pirko */ 10434778e0beSJiri Pirko static inline s32 nla_get_s32(const struct nlattr *nla) 10444778e0beSJiri Pirko { 10454778e0beSJiri Pirko return *(s32 *) nla_data(nla); 10464778e0beSJiri Pirko } 10474778e0beSJiri Pirko 10484778e0beSJiri Pirko /** 10494778e0beSJiri Pirko * nla_get_s16 - return payload of s16 attribute 10504778e0beSJiri Pirko * @nla: s16 netlink attribute 10514778e0beSJiri Pirko */ 10524778e0beSJiri Pirko static inline s16 nla_get_s16(const struct nlattr *nla) 10534778e0beSJiri Pirko { 10544778e0beSJiri Pirko return *(s16 *) nla_data(nla); 10554778e0beSJiri Pirko } 10564778e0beSJiri Pirko 10574778e0beSJiri Pirko /** 10584778e0beSJiri Pirko * nla_get_s8 - return payload of s8 attribute 10594778e0beSJiri Pirko * @nla: s8 netlink attribute 10604778e0beSJiri Pirko */ 10614778e0beSJiri Pirko static inline s8 nla_get_s8(const struct nlattr *nla) 10624778e0beSJiri Pirko { 10634778e0beSJiri Pirko return *(s8 *) nla_data(nla); 10644778e0beSJiri Pirko } 10654778e0beSJiri Pirko 10664778e0beSJiri Pirko /** 10674778e0beSJiri Pirko * nla_get_s64 - return payload of s64 attribute 10684778e0beSJiri Pirko * @nla: s64 netlink attribute 10694778e0beSJiri Pirko */ 10704778e0beSJiri Pirko static inline s64 nla_get_s64(const struct nlattr *nla) 10714778e0beSJiri Pirko { 10724778e0beSJiri Pirko s64 tmp; 10734778e0beSJiri Pirko 10744778e0beSJiri Pirko nla_memcpy(&tmp, nla, sizeof(tmp)); 10754778e0beSJiri Pirko 10764778e0beSJiri Pirko return tmp; 10774778e0beSJiri Pirko } 10784778e0beSJiri Pirko 10794778e0beSJiri Pirko /** 1080bfa83a9eSThomas Graf * nla_get_flag - return payload of flag attribute 1081bfa83a9eSThomas Graf * @nla: flag netlink attribute 1082bfa83a9eSThomas Graf */ 1083b057efd4SPatrick McHardy static inline int nla_get_flag(const struct nlattr *nla) 1084bfa83a9eSThomas Graf { 1085bfa83a9eSThomas Graf return !!nla; 1086bfa83a9eSThomas Graf } 1087bfa83a9eSThomas Graf 1088bfa83a9eSThomas Graf /** 1089bfa83a9eSThomas Graf * nla_get_msecs - return payload of msecs attribute 1090bfa83a9eSThomas Graf * @nla: msecs netlink attribute 1091bfa83a9eSThomas Graf * 1092bfa83a9eSThomas Graf * Returns the number of milliseconds in jiffies. 1093bfa83a9eSThomas Graf */ 1094b057efd4SPatrick McHardy static inline unsigned long nla_get_msecs(const struct nlattr *nla) 1095bfa83a9eSThomas Graf { 1096bfa83a9eSThomas Graf u64 msecs = nla_get_u64(nla); 1097bfa83a9eSThomas Graf 1098bfa83a9eSThomas Graf return msecs_to_jiffies((unsigned long) msecs); 1099bfa83a9eSThomas Graf } 1100bfa83a9eSThomas Graf 1101bfa83a9eSThomas Graf /** 1102bfa83a9eSThomas Graf * nla_nest_start - Start a new level of nested attributes 1103bfa83a9eSThomas Graf * @skb: socket buffer to add attributes to 1104bfa83a9eSThomas Graf * @attrtype: attribute type of container 1105bfa83a9eSThomas Graf * 1106bfa83a9eSThomas Graf * Returns the container attribute 1107bfa83a9eSThomas Graf */ 1108bfa83a9eSThomas Graf static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype) 1109bfa83a9eSThomas Graf { 111027a884dcSArnaldo Carvalho de Melo struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb); 1111bfa83a9eSThomas Graf 1112bfa83a9eSThomas Graf if (nla_put(skb, attrtype, 0, NULL) < 0) 1113bfa83a9eSThomas Graf return NULL; 1114bfa83a9eSThomas Graf 1115bfa83a9eSThomas Graf return start; 1116bfa83a9eSThomas Graf } 1117bfa83a9eSThomas Graf 1118bfa83a9eSThomas Graf /** 1119bfa83a9eSThomas Graf * nla_nest_end - Finalize nesting of attributes 1120d1ec3b77SPierre Ynard * @skb: socket buffer the attributes are stored in 1121bfa83a9eSThomas Graf * @start: container attribute 1122bfa83a9eSThomas Graf * 1123bfa83a9eSThomas Graf * Corrects the container attribute header to include the all 1124bfa83a9eSThomas Graf * appeneded attributes. 1125bfa83a9eSThomas Graf * 1126bfa83a9eSThomas Graf * Returns the total data length of the skb. 1127bfa83a9eSThomas Graf */ 1128bfa83a9eSThomas Graf static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start) 1129bfa83a9eSThomas Graf { 113027a884dcSArnaldo Carvalho de Melo start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start; 1131bfa83a9eSThomas Graf return skb->len; 1132bfa83a9eSThomas Graf } 1133bfa83a9eSThomas Graf 1134bfa83a9eSThomas Graf /** 1135bfa83a9eSThomas Graf * nla_nest_cancel - Cancel nesting of attributes 1136bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 1137bfa83a9eSThomas Graf * @start: container attribute 1138bfa83a9eSThomas Graf * 1139bfa83a9eSThomas Graf * Removes the container attribute and including all nested 1140bc3ed28cSThomas Graf * attributes. Returns -EMSGSIZE 1141bfa83a9eSThomas Graf */ 1142bc3ed28cSThomas Graf static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start) 1143bfa83a9eSThomas Graf { 1144bc3ed28cSThomas Graf nlmsg_trim(skb, start); 1145bfa83a9eSThomas Graf } 1146bfa83a9eSThomas Graf 1147bfa83a9eSThomas Graf /** 11484fe5d5c0SPaul Moore * nla_validate_nested - Validate a stream of nested attributes 11494fe5d5c0SPaul Moore * @start: container attribute 11504fe5d5c0SPaul Moore * @maxtype: maximum attribute type to be expected 11514fe5d5c0SPaul Moore * @policy: validation policy 11524fe5d5c0SPaul Moore * 11534fe5d5c0SPaul Moore * Validates all attributes in the nested attribute stream against the 11544fe5d5c0SPaul Moore * specified policy. Attributes with a type exceeding maxtype will be 11554fe5d5c0SPaul Moore * ignored. See documenation of struct nla_policy for more details. 11564fe5d5c0SPaul Moore * 11574fe5d5c0SPaul Moore * Returns 0 on success or a negative error code. 11584fe5d5c0SPaul Moore */ 11593654654fSJan Engelhardt static inline int nla_validate_nested(const struct nlattr *start, int maxtype, 1160ef7c79edSPatrick McHardy const struct nla_policy *policy) 11614fe5d5c0SPaul Moore { 11624fe5d5c0SPaul Moore return nla_validate(nla_data(start), nla_len(start), maxtype, policy); 11634fe5d5c0SPaul Moore } 11644fe5d5c0SPaul Moore 11654fe5d5c0SPaul Moore /** 1166bfa83a9eSThomas Graf * nla_for_each_attr - iterate over a stream of attributes 1167bfa83a9eSThomas Graf * @pos: loop counter, set to current attribute 1168bfa83a9eSThomas Graf * @head: head of attribute stream 1169bfa83a9eSThomas Graf * @len: length of attribute stream 1170bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 1171bfa83a9eSThomas Graf */ 1172bfa83a9eSThomas Graf #define nla_for_each_attr(pos, head, len, rem) \ 1173bfa83a9eSThomas Graf for (pos = head, rem = len; \ 1174bfa83a9eSThomas Graf nla_ok(pos, rem); \ 1175bfa83a9eSThomas Graf pos = nla_next(pos, &(rem))) 1176bfa83a9eSThomas Graf 1177fe4944e5SThomas Graf /** 1178fe4944e5SThomas Graf * nla_for_each_nested - iterate over nested attributes 1179fe4944e5SThomas Graf * @pos: loop counter, set to current attribute 1180fe4944e5SThomas Graf * @nla: attribute containing the nested attributes 1181fe4944e5SThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 1182fe4944e5SThomas Graf */ 1183fe4944e5SThomas Graf #define nla_for_each_nested(pos, nla, rem) \ 1184fe4944e5SThomas Graf nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem) 1185fe4944e5SThomas Graf 1186941d8ebcSSimon Horman /** 1187941d8ebcSSimon Horman * nla_is_last - Test if attribute is last in stream 1188941d8ebcSSimon Horman * @nla: attribute to test 1189941d8ebcSSimon Horman * @rem: bytes remaining in stream 1190941d8ebcSSimon Horman */ 1191941d8ebcSSimon Horman static inline bool nla_is_last(const struct nlattr *nla, int rem) 1192941d8ebcSSimon Horman { 1193941d8ebcSSimon Horman return nla->nla_len == rem; 1194941d8ebcSSimon Horman } 1195941d8ebcSSimon Horman 1196bfa83a9eSThomas Graf #endif 1197