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> 7930345eaSJiri Benc #include <linux/in6.h> 8bfa83a9eSThomas Graf 9bfa83a9eSThomas Graf /* ======================================================================== 10bfa83a9eSThomas Graf * Netlink Messages and Attributes Interface (As Seen On TV) 11bfa83a9eSThomas Graf * ------------------------------------------------------------------------ 12bfa83a9eSThomas Graf * Messages Interface 13bfa83a9eSThomas Graf * ------------------------------------------------------------------------ 14bfa83a9eSThomas Graf * 15bfa83a9eSThomas Graf * Message Format: 16bfa83a9eSThomas Graf * <--- nlmsg_total_size(payload) ---> 17bfa83a9eSThomas Graf * <-- nlmsg_msg_size(payload) -> 18bfa83a9eSThomas Graf * +----------+- - -+-------------+- - -+-------- - - 19bfa83a9eSThomas Graf * | nlmsghdr | Pad | Payload | Pad | nlmsghdr 20bfa83a9eSThomas Graf * +----------+- - -+-------------+- - -+-------- - - 21bfa83a9eSThomas Graf * nlmsg_data(nlh)---^ ^ 22bfa83a9eSThomas Graf * nlmsg_next(nlh)-----------------------+ 23bfa83a9eSThomas Graf * 24bfa83a9eSThomas Graf * Payload Format: 25bfa83a9eSThomas Graf * <---------------------- nlmsg_len(nlh) ---------------------> 26bfa83a9eSThomas Graf * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) -> 27bfa83a9eSThomas Graf * +----------------------+- - -+--------------------------------+ 28bfa83a9eSThomas Graf * | Family Header | Pad | Attributes | 29bfa83a9eSThomas Graf * +----------------------+- - -+--------------------------------+ 30bfa83a9eSThomas Graf * nlmsg_attrdata(nlh, hdrlen)---^ 31bfa83a9eSThomas Graf * 32bfa83a9eSThomas Graf * Data Structures: 33bfa83a9eSThomas Graf * struct nlmsghdr netlink message header 34bfa83a9eSThomas Graf * 35bfa83a9eSThomas Graf * Message Construction: 36bfa83a9eSThomas Graf * nlmsg_new() create a new netlink message 37bfa83a9eSThomas Graf * nlmsg_put() add a netlink message to an skb 38bfa83a9eSThomas Graf * nlmsg_put_answer() callback based nlmsg_put() 391dc8d8c0SJustin P. Mattock * nlmsg_end() finalize netlink message 40fe4944e5SThomas Graf * nlmsg_get_pos() return current position in message 41fe4944e5SThomas Graf * nlmsg_trim() trim part of message 42bfa83a9eSThomas Graf * nlmsg_cancel() cancel message construction 43bfa83a9eSThomas Graf * nlmsg_free() free a netlink message 44bfa83a9eSThomas Graf * 45bfa83a9eSThomas Graf * Message Sending: 46bfa83a9eSThomas Graf * nlmsg_multicast() multicast message to several groups 47bfa83a9eSThomas Graf * nlmsg_unicast() unicast a message to a single socket 48d387f6adSThomas Graf * nlmsg_notify() send notification message 49bfa83a9eSThomas Graf * 50bfa83a9eSThomas Graf * Message Length Calculations: 51bfa83a9eSThomas Graf * nlmsg_msg_size(payload) length of message w/o padding 52bfa83a9eSThomas Graf * nlmsg_total_size(payload) length of message w/ padding 53bfa83a9eSThomas Graf * nlmsg_padlen(payload) length of padding at tail 54bfa83a9eSThomas Graf * 55bfa83a9eSThomas Graf * Message Payload Access: 56bfa83a9eSThomas Graf * nlmsg_data(nlh) head of message payload 57bfa83a9eSThomas Graf * nlmsg_len(nlh) length of message payload 58bfa83a9eSThomas Graf * nlmsg_attrdata(nlh, hdrlen) head of attributes data 59bfa83a9eSThomas Graf * nlmsg_attrlen(nlh, hdrlen) length of attributes data 60bfa83a9eSThomas Graf * 61bfa83a9eSThomas Graf * Message Parsing: 62bfa83a9eSThomas Graf * nlmsg_ok(nlh, remaining) does nlh fit into remaining bytes? 63bfa83a9eSThomas Graf * nlmsg_next(nlh, remaining) get next netlink message 64bfa83a9eSThomas Graf * nlmsg_parse() parse attributes of a message 65bfa83a9eSThomas Graf * nlmsg_find_attr() find an attribute in a message 66bfa83a9eSThomas Graf * nlmsg_for_each_msg() loop over all messages 67bfa83a9eSThomas Graf * nlmsg_validate() validate netlink message incl. attrs 68bfa83a9eSThomas Graf * nlmsg_for_each_attr() loop over all attributes 69bfa83a9eSThomas Graf * 7097676b6bSThomas Graf * Misc: 7197676b6bSThomas Graf * nlmsg_report() report back to application? 7297676b6bSThomas Graf * 73bfa83a9eSThomas Graf * ------------------------------------------------------------------------ 74bfa83a9eSThomas Graf * Attributes Interface 75bfa83a9eSThomas Graf * ------------------------------------------------------------------------ 76bfa83a9eSThomas Graf * 77bfa83a9eSThomas Graf * Attribute Format: 78bfa83a9eSThomas Graf * <------- nla_total_size(payload) -------> 79bfa83a9eSThomas Graf * <---- nla_attr_size(payload) -----> 80bfa83a9eSThomas Graf * +----------+- - -+- - - - - - - - - +- - -+-------- - - 81bfa83a9eSThomas Graf * | Header | Pad | Payload | Pad | Header 82bfa83a9eSThomas Graf * +----------+- - -+- - - - - - - - - +- - -+-------- - - 83bfa83a9eSThomas Graf * <- nla_len(nla) -> ^ 84bfa83a9eSThomas Graf * nla_data(nla)----^ | 85bfa83a9eSThomas Graf * nla_next(nla)-----------------------------' 86bfa83a9eSThomas Graf * 87bfa83a9eSThomas Graf * Data Structures: 88d1ec3b77SPierre Ynard * struct nlattr netlink attribute header 89bfa83a9eSThomas Graf * 90bfa83a9eSThomas Graf * Attribute Construction: 91fe4944e5SThomas Graf * nla_reserve(skb, type, len) reserve room for an attribute 92fe4944e5SThomas Graf * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr 93bfa83a9eSThomas Graf * nla_put(skb, type, len, data) add attribute to skb 94fe4944e5SThomas Graf * nla_put_nohdr(skb, len, data) add attribute w/o hdr 9501480e1cSPatrick McHardy * nla_append(skb, len, data) append data to skb 96bfa83a9eSThomas Graf * 97bfa83a9eSThomas Graf * Attribute Construction for Basic Types: 98bfa83a9eSThomas Graf * nla_put_u8(skb, type, value) add u8 attribute to skb 99bfa83a9eSThomas Graf * nla_put_u16(skb, type, value) add u16 attribute to skb 100bfa83a9eSThomas Graf * nla_put_u32(skb, type, value) add u32 attribute to skb 101bfa83a9eSThomas Graf * nla_put_u64(skb, type, value) add u64 attribute to skb 1024778e0beSJiri Pirko * nla_put_s8(skb, type, value) add s8 attribute to skb 1034778e0beSJiri Pirko * nla_put_s16(skb, type, value) add s16 attribute to skb 1044778e0beSJiri Pirko * nla_put_s32(skb, type, value) add s32 attribute to skb 1054778e0beSJiri Pirko * nla_put_s64(skb, type, value) add s64 attribute to skb 106bfa83a9eSThomas Graf * nla_put_string(skb, type, str) add string attribute to skb 107bfa83a9eSThomas Graf * nla_put_flag(skb, type) add flag attribute to skb 108bfa83a9eSThomas Graf * nla_put_msecs(skb, type, jiffies) add msecs attribute to skb 109930345eaSJiri Benc * nla_put_in_addr(skb, type, addr) add IPv4 address attribute to skb 110930345eaSJiri Benc * nla_put_in6_addr(skb, type, addr) add IPv6 address attribute to skb 111bfa83a9eSThomas Graf * 112bfa83a9eSThomas Graf * Nested Attributes Construction: 113bfa83a9eSThomas Graf * nla_nest_start(skb, type) start a nested attribute 114bfa83a9eSThomas Graf * nla_nest_end(skb, nla) finalize a nested attribute 115bfa83a9eSThomas Graf * nla_nest_cancel(skb, nla) cancel nested attribute construction 116bfa83a9eSThomas Graf * 117bfa83a9eSThomas Graf * Attribute Length Calculations: 118bfa83a9eSThomas Graf * nla_attr_size(payload) length of attribute w/o padding 119bfa83a9eSThomas Graf * nla_total_size(payload) length of attribute w/ padding 120bfa83a9eSThomas Graf * nla_padlen(payload) length of padding 121bfa83a9eSThomas Graf * 122bfa83a9eSThomas Graf * Attribute Payload Access: 123bfa83a9eSThomas Graf * nla_data(nla) head of attribute payload 124bfa83a9eSThomas Graf * nla_len(nla) length of attribute payload 125bfa83a9eSThomas Graf * 126bfa83a9eSThomas Graf * Attribute Payload Access for Basic Types: 127bfa83a9eSThomas Graf * nla_get_u8(nla) get payload for a u8 attribute 128bfa83a9eSThomas Graf * nla_get_u16(nla) get payload for a u16 attribute 129bfa83a9eSThomas Graf * nla_get_u32(nla) get payload for a u32 attribute 130bfa83a9eSThomas Graf * nla_get_u64(nla) get payload for a u64 attribute 1314778e0beSJiri Pirko * nla_get_s8(nla) get payload for a s8 attribute 1324778e0beSJiri Pirko * nla_get_s16(nla) get payload for a s16 attribute 1334778e0beSJiri Pirko * nla_get_s32(nla) get payload for a s32 attribute 1344778e0beSJiri Pirko * nla_get_s64(nla) get payload for a s64 attribute 135bfa83a9eSThomas Graf * nla_get_flag(nla) return 1 if flag is true 136bfa83a9eSThomas Graf * nla_get_msecs(nla) get payload for a msecs attribute 137bfa83a9eSThomas Graf * 138bfa83a9eSThomas Graf * Attribute Misc: 139bfa83a9eSThomas Graf * nla_memcpy(dest, nla, count) copy attribute into memory 140bfa83a9eSThomas Graf * nla_memcmp(nla, data, size) compare attribute with memory area 141bfa83a9eSThomas Graf * nla_strlcpy(dst, nla, size) copy attribute to a sized string 142bfa83a9eSThomas Graf * nla_strcmp(nla, str) compare attribute with string 143bfa83a9eSThomas Graf * 144bfa83a9eSThomas Graf * Attribute Parsing: 145bfa83a9eSThomas Graf * nla_ok(nla, remaining) does nla fit into remaining bytes? 146bfa83a9eSThomas Graf * nla_next(nla, remaining) get next netlink attribute 147bfa83a9eSThomas Graf * nla_validate() validate a stream of attributes 1484fe5d5c0SPaul Moore * nla_validate_nested() validate a stream of nested attributes 149bfa83a9eSThomas Graf * nla_find() find attribute in stream of attributes 150fe4944e5SThomas Graf * nla_find_nested() find attribute in nested attributes 151bfa83a9eSThomas Graf * nla_parse() parse and validate stream of attrs 152bfa83a9eSThomas Graf * nla_parse_nested() parse nested attribuets 153bfa83a9eSThomas Graf * nla_for_each_attr() loop over all attributes 15422acb19aSPaul Moore * nla_for_each_nested() loop over the nested attributes 155bfa83a9eSThomas Graf *========================================================================= 156bfa83a9eSThomas Graf */ 157bfa83a9eSThomas Graf 158bfa83a9eSThomas Graf /** 159bfa83a9eSThomas Graf * Standard attribute types to specify validation policy 160bfa83a9eSThomas Graf */ 161bfa83a9eSThomas Graf enum { 162bfa83a9eSThomas Graf NLA_UNSPEC, 163bfa83a9eSThomas Graf NLA_U8, 164bfa83a9eSThomas Graf NLA_U16, 165bfa83a9eSThomas Graf NLA_U32, 166bfa83a9eSThomas Graf NLA_U64, 167bfa83a9eSThomas Graf NLA_STRING, 168bfa83a9eSThomas Graf NLA_FLAG, 169bfa83a9eSThomas Graf NLA_MSECS, 170bfa83a9eSThomas Graf NLA_NESTED, 1711092cb21SPatrick McHardy NLA_NESTED_COMPAT, 172a5531a5dSThomas Graf NLA_NUL_STRING, 173d30045a0SJohannes Berg NLA_BINARY, 1744778e0beSJiri Pirko NLA_S8, 1754778e0beSJiri Pirko NLA_S16, 1764778e0beSJiri Pirko NLA_S32, 1774778e0beSJiri Pirko NLA_S64, 178bfa83a9eSThomas Graf __NLA_TYPE_MAX, 179bfa83a9eSThomas Graf }; 180bfa83a9eSThomas Graf 181bfa83a9eSThomas Graf #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1) 182bfa83a9eSThomas Graf 183bfa83a9eSThomas Graf /** 184bfa83a9eSThomas Graf * struct nla_policy - attribute validation policy 185bfa83a9eSThomas Graf * @type: Type of attribute or NLA_UNSPEC 186a5531a5dSThomas Graf * @len: Type specific length of payload 187bfa83a9eSThomas Graf * 188bfa83a9eSThomas Graf * Policies are defined as arrays of this struct, the array must be 189bfa83a9eSThomas Graf * accessible by attribute type up to the highest identifier to be expected. 190bfa83a9eSThomas Graf * 191a5531a5dSThomas Graf * Meaning of `len' field: 192a5531a5dSThomas Graf * NLA_STRING Maximum length of string 193a5531a5dSThomas Graf * NLA_NUL_STRING Maximum length of string (excluding NUL) 194a5531a5dSThomas Graf * NLA_FLAG Unused 195d30045a0SJohannes Berg * NLA_BINARY Maximum length of attribute payload 1964b6cc728SJohannes Berg * NLA_NESTED Don't use `len' field -- length verification is 1974b6cc728SJohannes Berg * done by checking len of nested header (or empty) 1984b6cc728SJohannes Berg * NLA_NESTED_COMPAT Minimum length of structure payload 1994b6cc728SJohannes Berg * NLA_U8, NLA_U16, 2004b6cc728SJohannes Berg * NLA_U32, NLA_U64, 2014778e0beSJiri Pirko * NLA_S8, NLA_S16, 2024778e0beSJiri Pirko * NLA_S32, NLA_S64, 2034b6cc728SJohannes Berg * NLA_MSECS Leaving the length field zero will verify the 2044b6cc728SJohannes Berg * given type fits, using it verifies minimum length 2054b6cc728SJohannes Berg * just like "All other" 2064b6cc728SJohannes Berg * All other Minimum length of attribute payload 207a5531a5dSThomas Graf * 208bfa83a9eSThomas Graf * Example: 209b54452b0SAlexey Dobriyan * static const struct nla_policy my_policy[ATTR_MAX+1] = { 210bfa83a9eSThomas Graf * [ATTR_FOO] = { .type = NLA_U16 }, 211d30045a0SJohannes Berg * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ }, 212a5531a5dSThomas Graf * [ATTR_BAZ] = { .len = sizeof(struct mystruct) }, 213bfa83a9eSThomas Graf * }; 214bfa83a9eSThomas Graf */ 215bfa83a9eSThomas Graf struct nla_policy { 216bfa83a9eSThomas Graf u16 type; 217a5531a5dSThomas Graf u16 len; 218bfa83a9eSThomas Graf }; 219bfa83a9eSThomas Graf 2204e902c57SThomas Graf /** 2214e902c57SThomas Graf * struct nl_info - netlink source information 2224e902c57SThomas Graf * @nlh: Netlink message header of original request 22315e47304SEric W. Biederman * @portid: Netlink PORTID of requesting application 2244e902c57SThomas Graf */ 2254e902c57SThomas Graf struct nl_info { 2264e902c57SThomas Graf struct nlmsghdr *nlh; 2274d1169c1SDenis V. Lunev struct net *nl_net; 22815e47304SEric W. Biederman u32 portid; 2294e902c57SThomas Graf }; 2304e902c57SThomas Graf 2314f69053bSJoe Perches int netlink_rcv_skb(struct sk_buff *skb, 2324f69053bSJoe Perches int (*cb)(struct sk_buff *, struct nlmsghdr *)); 2334f69053bSJoe Perches int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid, 2344f69053bSJoe Perches unsigned int group, int report, gfp_t flags); 23582ace47aSThomas Graf 2364f69053bSJoe Perches int nla_validate(const struct nlattr *head, int len, int maxtype, 237ef7c79edSPatrick McHardy const struct nla_policy *policy); 2384f69053bSJoe Perches int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, 2394f69053bSJoe Perches int len, const struct nla_policy *policy); 2404f69053bSJoe Perches int nla_policy_len(const struct nla_policy *, int); 2414f69053bSJoe Perches struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype); 2424f69053bSJoe Perches size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize); 2434f69053bSJoe Perches int nla_memcpy(void *dest, const struct nlattr *src, int count); 2444f69053bSJoe Perches int nla_memcmp(const struct nlattr *nla, const void *data, size_t size); 2454f69053bSJoe Perches int nla_strcmp(const struct nlattr *nla, const char *str); 2464f69053bSJoe Perches struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 247089bf1a6SNicolas Dichtel struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype, 248089bf1a6SNicolas Dichtel int attrlen, int padattr); 2494f69053bSJoe Perches void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 2504f69053bSJoe Perches struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen); 251089bf1a6SNicolas Dichtel struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, 252089bf1a6SNicolas Dichtel int attrlen, int padattr); 2534f69053bSJoe Perches void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen); 2544f69053bSJoe Perches void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, 255fe4944e5SThomas Graf const void *data); 256089bf1a6SNicolas Dichtel void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 257089bf1a6SNicolas Dichtel const void *data, int padattr); 2584f69053bSJoe Perches void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 2594f69053bSJoe Perches int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data); 260089bf1a6SNicolas Dichtel int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, 261089bf1a6SNicolas Dichtel const void *data, int padattr); 2624f69053bSJoe Perches int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data); 2634f69053bSJoe Perches int nla_append(struct sk_buff *skb, int attrlen, const void *data); 264bfa83a9eSThomas Graf 265bfa83a9eSThomas Graf /************************************************************************** 266bfa83a9eSThomas Graf * Netlink Messages 267bfa83a9eSThomas Graf **************************************************************************/ 268bfa83a9eSThomas Graf 269bfa83a9eSThomas Graf /** 270bfa83a9eSThomas Graf * nlmsg_msg_size - length of netlink message not including padding 271bfa83a9eSThomas Graf * @payload: length of message payload 272bfa83a9eSThomas Graf */ 273bfa83a9eSThomas Graf static inline int nlmsg_msg_size(int payload) 274bfa83a9eSThomas Graf { 275bfa83a9eSThomas Graf return NLMSG_HDRLEN + payload; 276bfa83a9eSThomas Graf } 277bfa83a9eSThomas Graf 278bfa83a9eSThomas Graf /** 279bfa83a9eSThomas Graf * nlmsg_total_size - length of netlink message including padding 280bfa83a9eSThomas Graf * @payload: length of message payload 281bfa83a9eSThomas Graf */ 282bfa83a9eSThomas Graf static inline int nlmsg_total_size(int payload) 283bfa83a9eSThomas Graf { 284bfa83a9eSThomas Graf return NLMSG_ALIGN(nlmsg_msg_size(payload)); 285bfa83a9eSThomas Graf } 286bfa83a9eSThomas Graf 287bfa83a9eSThomas Graf /** 288bfa83a9eSThomas Graf * nlmsg_padlen - length of padding at the message's tail 289bfa83a9eSThomas Graf * @payload: length of message payload 290bfa83a9eSThomas Graf */ 291bfa83a9eSThomas Graf static inline int nlmsg_padlen(int payload) 292bfa83a9eSThomas Graf { 293bfa83a9eSThomas Graf return nlmsg_total_size(payload) - nlmsg_msg_size(payload); 294bfa83a9eSThomas Graf } 295bfa83a9eSThomas Graf 296bfa83a9eSThomas Graf /** 297bfa83a9eSThomas Graf * nlmsg_data - head of message payload 29870f23fd6SJustin P. Mattock * @nlh: netlink message header 299bfa83a9eSThomas Graf */ 300bfa83a9eSThomas Graf static inline void *nlmsg_data(const struct nlmsghdr *nlh) 301bfa83a9eSThomas Graf { 302bfa83a9eSThomas Graf return (unsigned char *) nlh + NLMSG_HDRLEN; 303bfa83a9eSThomas Graf } 304bfa83a9eSThomas Graf 305bfa83a9eSThomas Graf /** 306bfa83a9eSThomas Graf * nlmsg_len - length of message payload 307bfa83a9eSThomas Graf * @nlh: netlink message header 308bfa83a9eSThomas Graf */ 309bfa83a9eSThomas Graf static inline int nlmsg_len(const struct nlmsghdr *nlh) 310bfa83a9eSThomas Graf { 311bfa83a9eSThomas Graf return nlh->nlmsg_len - NLMSG_HDRLEN; 312bfa83a9eSThomas Graf } 313bfa83a9eSThomas Graf 314bfa83a9eSThomas Graf /** 315bfa83a9eSThomas Graf * nlmsg_attrdata - head of attributes data 316bfa83a9eSThomas Graf * @nlh: netlink message header 317bfa83a9eSThomas Graf * @hdrlen: length of family specific header 318bfa83a9eSThomas Graf */ 319bfa83a9eSThomas Graf static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, 320bfa83a9eSThomas Graf int hdrlen) 321bfa83a9eSThomas Graf { 322bfa83a9eSThomas Graf unsigned char *data = nlmsg_data(nlh); 323bfa83a9eSThomas Graf return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen)); 324bfa83a9eSThomas Graf } 325bfa83a9eSThomas Graf 326bfa83a9eSThomas Graf /** 327bfa83a9eSThomas Graf * nlmsg_attrlen - length of attributes data 328bfa83a9eSThomas Graf * @nlh: netlink message header 329bfa83a9eSThomas Graf * @hdrlen: length of family specific header 330bfa83a9eSThomas Graf */ 331bfa83a9eSThomas Graf static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen) 332bfa83a9eSThomas Graf { 333bfa83a9eSThomas Graf return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen); 334bfa83a9eSThomas Graf } 335bfa83a9eSThomas Graf 336bfa83a9eSThomas Graf /** 337bfa83a9eSThomas Graf * nlmsg_ok - check if the netlink message fits into the remaining bytes 338bfa83a9eSThomas Graf * @nlh: netlink message header 339bfa83a9eSThomas Graf * @remaining: number of bytes remaining in message stream 340bfa83a9eSThomas Graf */ 341bfa83a9eSThomas Graf static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining) 342bfa83a9eSThomas Graf { 343619e803dSVegard Nossum return (remaining >= (int) sizeof(struct nlmsghdr) && 344bfa83a9eSThomas Graf nlh->nlmsg_len >= sizeof(struct nlmsghdr) && 345bfa83a9eSThomas Graf nlh->nlmsg_len <= remaining); 346bfa83a9eSThomas Graf } 347bfa83a9eSThomas Graf 348bfa83a9eSThomas Graf /** 349bfa83a9eSThomas Graf * nlmsg_next - next netlink message in message stream 350bfa83a9eSThomas Graf * @nlh: netlink message header 351bfa83a9eSThomas Graf * @remaining: number of bytes remaining in message stream 352bfa83a9eSThomas Graf * 353bfa83a9eSThomas Graf * Returns the next netlink message in the message stream and 354bfa83a9eSThomas Graf * decrements remaining by the size of the current message. 355bfa83a9eSThomas Graf */ 3563654654fSJan Engelhardt static inline struct nlmsghdr * 3573654654fSJan Engelhardt nlmsg_next(const struct nlmsghdr *nlh, int *remaining) 358bfa83a9eSThomas Graf { 359bfa83a9eSThomas Graf int totlen = NLMSG_ALIGN(nlh->nlmsg_len); 360bfa83a9eSThomas Graf 361bfa83a9eSThomas Graf *remaining -= totlen; 362bfa83a9eSThomas Graf 363bfa83a9eSThomas Graf return (struct nlmsghdr *) ((unsigned char *) nlh + totlen); 364bfa83a9eSThomas Graf } 365bfa83a9eSThomas Graf 366bfa83a9eSThomas Graf /** 367bfa83a9eSThomas Graf * nlmsg_parse - parse attributes of a netlink message 368bfa83a9eSThomas Graf * @nlh: netlink message header 369bfa83a9eSThomas Graf * @hdrlen: length of family specific header 370bfa83a9eSThomas Graf * @tb: destination array with maxtype+1 elements 371bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 372bfa83a9eSThomas Graf * @policy: validation policy 373bfa83a9eSThomas Graf * 374bfa83a9eSThomas Graf * See nla_parse() 375bfa83a9eSThomas Graf */ 3763a6c2b41SPatrick McHardy static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen, 377bfa83a9eSThomas Graf struct nlattr *tb[], int maxtype, 378ef7c79edSPatrick McHardy const struct nla_policy *policy) 379bfa83a9eSThomas Graf { 380bfa83a9eSThomas Graf if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) 381bfa83a9eSThomas Graf return -EINVAL; 382bfa83a9eSThomas Graf 383bfa83a9eSThomas Graf return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), 384bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), policy); 385bfa83a9eSThomas Graf } 386bfa83a9eSThomas Graf 387bfa83a9eSThomas Graf /** 388bfa83a9eSThomas Graf * nlmsg_find_attr - find a specific attribute in a netlink message 389bfa83a9eSThomas Graf * @nlh: netlink message header 390bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 391bfa83a9eSThomas Graf * @attrtype: type of attribute to look for 392bfa83a9eSThomas Graf * 393bfa83a9eSThomas Graf * Returns the first attribute which matches the specified type. 394bfa83a9eSThomas Graf */ 3956b8c92baSNelson Elhage static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh, 396bfa83a9eSThomas Graf int hdrlen, int attrtype) 397bfa83a9eSThomas Graf { 398bfa83a9eSThomas Graf return nla_find(nlmsg_attrdata(nlh, hdrlen), 399bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), attrtype); 400bfa83a9eSThomas Graf } 401bfa83a9eSThomas Graf 402bfa83a9eSThomas Graf /** 403bfa83a9eSThomas Graf * nlmsg_validate - validate a netlink message including attributes 404bfa83a9eSThomas Graf * @nlh: netlinket message header 405bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 406bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 407bfa83a9eSThomas Graf * @policy: validation policy 408bfa83a9eSThomas Graf */ 4093654654fSJan Engelhardt static inline int nlmsg_validate(const struct nlmsghdr *nlh, 4103654654fSJan Engelhardt int hdrlen, int maxtype, 411ef7c79edSPatrick McHardy const struct nla_policy *policy) 412bfa83a9eSThomas Graf { 413bfa83a9eSThomas Graf if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) 414bfa83a9eSThomas Graf return -EINVAL; 415bfa83a9eSThomas Graf 416bfa83a9eSThomas Graf return nla_validate(nlmsg_attrdata(nlh, hdrlen), 417bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), maxtype, policy); 418bfa83a9eSThomas Graf } 419bfa83a9eSThomas Graf 420bfa83a9eSThomas Graf /** 42197676b6bSThomas Graf * nlmsg_report - need to report back to application? 42297676b6bSThomas Graf * @nlh: netlink message header 42397676b6bSThomas Graf * 42497676b6bSThomas Graf * Returns 1 if a report back to the application is requested. 42597676b6bSThomas Graf */ 4263a6c2b41SPatrick McHardy static inline int nlmsg_report(const struct nlmsghdr *nlh) 42797676b6bSThomas Graf { 42897676b6bSThomas Graf return !!(nlh->nlmsg_flags & NLM_F_ECHO); 42997676b6bSThomas Graf } 43097676b6bSThomas Graf 43197676b6bSThomas Graf /** 432bfa83a9eSThomas Graf * nlmsg_for_each_attr - iterate over a stream of attributes 433bfa83a9eSThomas Graf * @pos: loop counter, set to current attribute 434bfa83a9eSThomas Graf * @nlh: netlink message header 435bfa83a9eSThomas Graf * @hdrlen: length of familiy specific header 436bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 437bfa83a9eSThomas Graf */ 438bfa83a9eSThomas Graf #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \ 439bfa83a9eSThomas Graf nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \ 440bfa83a9eSThomas Graf nlmsg_attrlen(nlh, hdrlen), rem) 441bfa83a9eSThomas Graf 442bfa83a9eSThomas Graf /** 443bfa83a9eSThomas Graf * nlmsg_put - Add a new netlink message to an skb 444bfa83a9eSThomas Graf * @skb: socket buffer to store message in 4452c6ba4b1SNicolas Dichtel * @portid: netlink PORTID of requesting application 446bfa83a9eSThomas Graf * @seq: sequence number of message 447bfa83a9eSThomas Graf * @type: message type 448bfa83a9eSThomas Graf * @payload: length of message payload 449bfa83a9eSThomas Graf * @flags: message flags 450bfa83a9eSThomas Graf * 451bfa83a9eSThomas Graf * Returns NULL if the tailroom of the skb is insufficient to store 452bfa83a9eSThomas Graf * the message header and payload. 453bfa83a9eSThomas Graf */ 45415e47304SEric W. Biederman static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, 455bfa83a9eSThomas Graf int type, int payload, int flags) 456bfa83a9eSThomas Graf { 457bfa83a9eSThomas Graf if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload))) 458bfa83a9eSThomas Graf return NULL; 459bfa83a9eSThomas Graf 46015e47304SEric W. Biederman return __nlmsg_put(skb, portid, seq, type, payload, flags); 461bfa83a9eSThomas Graf } 462bfa83a9eSThomas Graf 463bfa83a9eSThomas Graf /** 464bfa83a9eSThomas Graf * nlmsg_put_answer - Add a new callback based netlink message to an skb 465bfa83a9eSThomas Graf * @skb: socket buffer to store message in 466bfa83a9eSThomas Graf * @cb: netlink callback 467bfa83a9eSThomas Graf * @type: message type 468bfa83a9eSThomas Graf * @payload: length of message payload 469bfa83a9eSThomas Graf * @flags: message flags 470bfa83a9eSThomas Graf * 471bfa83a9eSThomas Graf * Returns NULL if the tailroom of the skb is insufficient to store 472bfa83a9eSThomas Graf * the message header and payload. 473bfa83a9eSThomas Graf */ 474bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb, 475bfa83a9eSThomas Graf struct netlink_callback *cb, 476bfa83a9eSThomas Graf int type, int payload, 477bfa83a9eSThomas Graf int flags) 478bfa83a9eSThomas Graf { 47915e47304SEric W. Biederman return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 480bfa83a9eSThomas Graf type, payload, flags); 481bfa83a9eSThomas Graf } 482bfa83a9eSThomas Graf 483bfa83a9eSThomas Graf /** 484bfa83a9eSThomas Graf * nlmsg_new - Allocate a new netlink message 485339bf98fSThomas Graf * @payload: size of the message payload 486fe4944e5SThomas Graf * @flags: the type of memory to allocate. 487bfa83a9eSThomas Graf * 488339bf98fSThomas Graf * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known 489339bf98fSThomas Graf * and a good default is needed. 490bfa83a9eSThomas Graf */ 491339bf98fSThomas Graf static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags) 492bfa83a9eSThomas Graf { 493339bf98fSThomas Graf return alloc_skb(nlmsg_total_size(payload), flags); 494bfa83a9eSThomas Graf } 495bfa83a9eSThomas Graf 496bfa83a9eSThomas Graf /** 497bfa83a9eSThomas Graf * nlmsg_end - Finalize a netlink message 498bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 499bfa83a9eSThomas Graf * @nlh: netlink message header 500bfa83a9eSThomas Graf * 501bfa83a9eSThomas Graf * Corrects the netlink message header to include the appeneded 502bfa83a9eSThomas Graf * attributes. Only necessary if attributes have been added to 503bfa83a9eSThomas Graf * the message. 504bfa83a9eSThomas Graf */ 505053c095aSJohannes Berg static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh) 506bfa83a9eSThomas Graf { 50727a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh; 508bfa83a9eSThomas Graf } 509bfa83a9eSThomas Graf 510bfa83a9eSThomas Graf /** 511fe4944e5SThomas Graf * nlmsg_get_pos - return current position in netlink message 512fe4944e5SThomas Graf * @skb: socket buffer the message is stored in 513fe4944e5SThomas Graf * 514fe4944e5SThomas Graf * Returns a pointer to the current tail of the message. 515fe4944e5SThomas Graf */ 516fe4944e5SThomas Graf static inline void *nlmsg_get_pos(struct sk_buff *skb) 517fe4944e5SThomas Graf { 51827a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 519fe4944e5SThomas Graf } 520fe4944e5SThomas Graf 521fe4944e5SThomas Graf /** 522fe4944e5SThomas Graf * nlmsg_trim - Trim message to a mark 523fe4944e5SThomas Graf * @skb: socket buffer the message is stored in 524fe4944e5SThomas Graf * @mark: mark to trim to 525fe4944e5SThomas Graf * 526bc3ed28cSThomas Graf * Trims the message to the provided mark. 527fe4944e5SThomas Graf */ 528bc3ed28cSThomas Graf static inline void nlmsg_trim(struct sk_buff *skb, const void *mark) 529fe4944e5SThomas Graf { 530149118d8SThomas Graf if (mark) { 531149118d8SThomas Graf WARN_ON((unsigned char *) mark < skb->data); 532fe4944e5SThomas Graf skb_trim(skb, (unsigned char *) mark - skb->data); 533fe4944e5SThomas Graf } 534149118d8SThomas Graf } 535fe4944e5SThomas Graf 536fe4944e5SThomas Graf /** 537bfa83a9eSThomas Graf * nlmsg_cancel - Cancel construction of a netlink message 538bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 539bfa83a9eSThomas Graf * @nlh: netlink message header 540bfa83a9eSThomas Graf * 541bfa83a9eSThomas Graf * Removes the complete netlink message including all 542bc3ed28cSThomas Graf * attributes from the socket buffer again. 543bfa83a9eSThomas Graf */ 544bc3ed28cSThomas Graf static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh) 545bfa83a9eSThomas Graf { 546bc3ed28cSThomas Graf nlmsg_trim(skb, nlh); 547bfa83a9eSThomas Graf } 548bfa83a9eSThomas Graf 549bfa83a9eSThomas Graf /** 550bfa83a9eSThomas Graf * nlmsg_free - free a netlink message 551bfa83a9eSThomas Graf * @skb: socket buffer of netlink message 552bfa83a9eSThomas Graf */ 553bfa83a9eSThomas Graf static inline void nlmsg_free(struct sk_buff *skb) 554bfa83a9eSThomas Graf { 555bfa83a9eSThomas Graf kfree_skb(skb); 556bfa83a9eSThomas Graf } 557bfa83a9eSThomas Graf 558bfa83a9eSThomas Graf /** 559bfa83a9eSThomas Graf * nlmsg_multicast - multicast a netlink message 560bfa83a9eSThomas Graf * @sk: netlink socket to spread messages to 561bfa83a9eSThomas Graf * @skb: netlink message as socket buffer 56215e47304SEric W. Biederman * @portid: own netlink portid to avoid sending to yourself 563bfa83a9eSThomas Graf * @group: multicast group id 564d387f6adSThomas Graf * @flags: allocation flags 565bfa83a9eSThomas Graf */ 566bfa83a9eSThomas Graf static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, 56715e47304SEric W. Biederman u32 portid, unsigned int group, gfp_t flags) 568bfa83a9eSThomas Graf { 569bfa83a9eSThomas Graf int err; 570bfa83a9eSThomas Graf 571bfa83a9eSThomas Graf NETLINK_CB(skb).dst_group = group; 572bfa83a9eSThomas Graf 57315e47304SEric W. Biederman err = netlink_broadcast(sk, skb, portid, group, flags); 574bfa83a9eSThomas Graf if (err > 0) 575bfa83a9eSThomas Graf err = 0; 576bfa83a9eSThomas Graf 577bfa83a9eSThomas Graf return err; 578bfa83a9eSThomas Graf } 579bfa83a9eSThomas Graf 580bfa83a9eSThomas Graf /** 581bfa83a9eSThomas Graf * nlmsg_unicast - unicast a netlink message 582bfa83a9eSThomas Graf * @sk: netlink socket to spread message to 583bfa83a9eSThomas Graf * @skb: netlink message as socket buffer 58415e47304SEric W. Biederman * @portid: netlink portid of the destination socket 585bfa83a9eSThomas Graf */ 58615e47304SEric W. Biederman static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid) 587bfa83a9eSThomas Graf { 588bfa83a9eSThomas Graf int err; 589bfa83a9eSThomas Graf 59015e47304SEric W. Biederman err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT); 591bfa83a9eSThomas Graf if (err > 0) 592bfa83a9eSThomas Graf err = 0; 593bfa83a9eSThomas Graf 594bfa83a9eSThomas Graf return err; 595bfa83a9eSThomas Graf } 596bfa83a9eSThomas Graf 597bfa83a9eSThomas Graf /** 598bfa83a9eSThomas Graf * nlmsg_for_each_msg - iterate over a stream of messages 599bfa83a9eSThomas Graf * @pos: loop counter, set to current message 600bfa83a9eSThomas Graf * @head: head of message stream 601bfa83a9eSThomas Graf * @len: length of message stream 602bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 603bfa83a9eSThomas Graf */ 604bfa83a9eSThomas Graf #define nlmsg_for_each_msg(pos, head, len, rem) \ 605bfa83a9eSThomas Graf for (pos = head, rem = len; \ 606bfa83a9eSThomas Graf nlmsg_ok(pos, rem); \ 607bfa83a9eSThomas Graf pos = nlmsg_next(pos, &(rem))) 608bfa83a9eSThomas Graf 609670dc283SJohannes Berg /** 610670dc283SJohannes Berg * nl_dump_check_consistent - check if sequence is consistent and advertise if not 611670dc283SJohannes Berg * @cb: netlink callback structure that stores the sequence number 612670dc283SJohannes Berg * @nlh: netlink message header to write the flag to 613670dc283SJohannes Berg * 614670dc283SJohannes Berg * This function checks if the sequence (generation) number changed during dump 615670dc283SJohannes Berg * and if it did, advertises it in the netlink message header. 616670dc283SJohannes Berg * 617670dc283SJohannes Berg * The correct way to use it is to set cb->seq to the generation counter when 618670dc283SJohannes Berg * all locks for dumping have been acquired, and then call this function for 619670dc283SJohannes Berg * each message that is generated. 620670dc283SJohannes Berg * 621670dc283SJohannes Berg * Note that due to initialisation concerns, 0 is an invalid sequence number 622670dc283SJohannes Berg * and must not be used by code that uses this functionality. 623670dc283SJohannes Berg */ 624670dc283SJohannes Berg static inline void 625670dc283SJohannes Berg nl_dump_check_consistent(struct netlink_callback *cb, 626670dc283SJohannes Berg struct nlmsghdr *nlh) 627670dc283SJohannes Berg { 628670dc283SJohannes Berg if (cb->prev_seq && cb->seq != cb->prev_seq) 629670dc283SJohannes Berg nlh->nlmsg_flags |= NLM_F_DUMP_INTR; 630670dc283SJohannes Berg cb->prev_seq = cb->seq; 631670dc283SJohannes Berg } 632670dc283SJohannes Berg 633bfa83a9eSThomas Graf /************************************************************************** 634bfa83a9eSThomas Graf * Netlink Attributes 635bfa83a9eSThomas Graf **************************************************************************/ 636bfa83a9eSThomas Graf 637bfa83a9eSThomas Graf /** 638bfa83a9eSThomas Graf * nla_attr_size - length of attribute not including padding 639bfa83a9eSThomas Graf * @payload: length of payload 640bfa83a9eSThomas Graf */ 641bfa83a9eSThomas Graf static inline int nla_attr_size(int payload) 642bfa83a9eSThomas Graf { 643bfa83a9eSThomas Graf return NLA_HDRLEN + payload; 644bfa83a9eSThomas Graf } 645bfa83a9eSThomas Graf 646bfa83a9eSThomas Graf /** 647bfa83a9eSThomas Graf * nla_total_size - total length of attribute including padding 648bfa83a9eSThomas Graf * @payload: length of payload 649bfa83a9eSThomas Graf */ 650bfa83a9eSThomas Graf static inline int nla_total_size(int payload) 651bfa83a9eSThomas Graf { 652bfa83a9eSThomas Graf return NLA_ALIGN(nla_attr_size(payload)); 653bfa83a9eSThomas Graf } 654bfa83a9eSThomas Graf 655bfa83a9eSThomas Graf /** 656bfa83a9eSThomas Graf * nla_padlen - length of padding at the tail of attribute 657bfa83a9eSThomas Graf * @payload: length of payload 658bfa83a9eSThomas Graf */ 659bfa83a9eSThomas Graf static inline int nla_padlen(int payload) 660bfa83a9eSThomas Graf { 661bfa83a9eSThomas Graf return nla_total_size(payload) - nla_attr_size(payload); 662bfa83a9eSThomas Graf } 663bfa83a9eSThomas Graf 664bfa83a9eSThomas Graf /** 6658f4c1f9bSThomas Graf * nla_type - attribute type 6668f4c1f9bSThomas Graf * @nla: netlink attribute 6678f4c1f9bSThomas Graf */ 6688f4c1f9bSThomas Graf static inline int nla_type(const struct nlattr *nla) 6698f4c1f9bSThomas Graf { 6708f4c1f9bSThomas Graf return nla->nla_type & NLA_TYPE_MASK; 6718f4c1f9bSThomas Graf } 6728f4c1f9bSThomas Graf 6738f4c1f9bSThomas Graf /** 674bfa83a9eSThomas Graf * nla_data - head of payload 675bfa83a9eSThomas Graf * @nla: netlink attribute 676bfa83a9eSThomas Graf */ 677bfa83a9eSThomas Graf static inline void *nla_data(const struct nlattr *nla) 678bfa83a9eSThomas Graf { 679bfa83a9eSThomas Graf return (char *) nla + NLA_HDRLEN; 680bfa83a9eSThomas Graf } 681bfa83a9eSThomas Graf 682bfa83a9eSThomas Graf /** 683bfa83a9eSThomas Graf * nla_len - length of payload 684bfa83a9eSThomas Graf * @nla: netlink attribute 685bfa83a9eSThomas Graf */ 686bfa83a9eSThomas Graf static inline int nla_len(const struct nlattr *nla) 687bfa83a9eSThomas Graf { 688bfa83a9eSThomas Graf return nla->nla_len - NLA_HDRLEN; 689bfa83a9eSThomas Graf } 690bfa83a9eSThomas Graf 691bfa83a9eSThomas Graf /** 692bfa83a9eSThomas Graf * nla_ok - check if the netlink attribute fits into the remaining bytes 693bfa83a9eSThomas Graf * @nla: netlink attribute 694bfa83a9eSThomas Graf * @remaining: number of bytes remaining in attribute stream 695bfa83a9eSThomas Graf */ 696bfa83a9eSThomas Graf static inline int nla_ok(const struct nlattr *nla, int remaining) 697bfa83a9eSThomas Graf { 6981045b03eSVegard Nossum return remaining >= (int) sizeof(*nla) && 699bfa83a9eSThomas Graf nla->nla_len >= sizeof(*nla) && 700bfa83a9eSThomas Graf nla->nla_len <= remaining; 701bfa83a9eSThomas Graf } 702bfa83a9eSThomas Graf 703bfa83a9eSThomas Graf /** 704d1ec3b77SPierre Ynard * nla_next - next netlink attribute in attribute stream 705bfa83a9eSThomas Graf * @nla: netlink attribute 706bfa83a9eSThomas Graf * @remaining: number of bytes remaining in attribute stream 707bfa83a9eSThomas Graf * 708bfa83a9eSThomas Graf * Returns the next netlink attribute in the attribute stream and 709bfa83a9eSThomas Graf * decrements remaining by the size of the current attribute. 710bfa83a9eSThomas Graf */ 711bfa83a9eSThomas Graf static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining) 712bfa83a9eSThomas Graf { 713bfa83a9eSThomas Graf int totlen = NLA_ALIGN(nla->nla_len); 714bfa83a9eSThomas Graf 715bfa83a9eSThomas Graf *remaining -= totlen; 716bfa83a9eSThomas Graf return (struct nlattr *) ((char *) nla + totlen); 717bfa83a9eSThomas Graf } 718bfa83a9eSThomas Graf 719bfa83a9eSThomas Graf /** 720fe4944e5SThomas Graf * nla_find_nested - find attribute in a set of nested attributes 721fe4944e5SThomas Graf * @nla: attribute containing the nested attributes 722fe4944e5SThomas Graf * @attrtype: type of attribute to look for 723fe4944e5SThomas Graf * 724fe4944e5SThomas Graf * Returns the first attribute which matches the specified type. 725fe4944e5SThomas Graf */ 7263654654fSJan Engelhardt static inline struct nlattr * 7273654654fSJan Engelhardt nla_find_nested(const struct nlattr *nla, int attrtype) 728fe4944e5SThomas Graf { 729fe4944e5SThomas Graf return nla_find(nla_data(nla), nla_len(nla), attrtype); 730fe4944e5SThomas Graf } 731fe4944e5SThomas Graf 732fe4944e5SThomas Graf /** 733bfa83a9eSThomas Graf * nla_parse_nested - parse nested attributes 734bfa83a9eSThomas Graf * @tb: destination array with maxtype+1 elements 735bfa83a9eSThomas Graf * @maxtype: maximum attribute type to be expected 736bfa83a9eSThomas Graf * @nla: attribute containing the nested attributes 737bfa83a9eSThomas Graf * @policy: validation policy 738bfa83a9eSThomas Graf * 739bfa83a9eSThomas Graf * See nla_parse() 740bfa83a9eSThomas Graf */ 741bfa83a9eSThomas Graf static inline int nla_parse_nested(struct nlattr *tb[], int maxtype, 742b057efd4SPatrick McHardy const struct nlattr *nla, 743ef7c79edSPatrick McHardy const struct nla_policy *policy) 744bfa83a9eSThomas Graf { 745bfa83a9eSThomas Graf return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy); 746bfa83a9eSThomas Graf } 7471092cb21SPatrick McHardy 7481092cb21SPatrick McHardy /** 749d1ec3b77SPierre Ynard * nla_put_u8 - Add a u8 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_u8(struct sk_buff *skb, int attrtype, u8 value) 755bfa83a9eSThomas Graf { 756bfa83a9eSThomas Graf return nla_put(skb, attrtype, sizeof(u8), &value); 757bfa83a9eSThomas Graf } 758bfa83a9eSThomas Graf 759bfa83a9eSThomas Graf /** 760bfa83a9eSThomas Graf * nla_put_u16 - Add a u16 netlink attribute to a socket buffer 761bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 762bfa83a9eSThomas Graf * @attrtype: attribute type 763bfa83a9eSThomas Graf * @value: numeric value 764bfa83a9eSThomas Graf */ 765bfa83a9eSThomas Graf static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value) 766bfa83a9eSThomas Graf { 767bfa83a9eSThomas Graf return nla_put(skb, attrtype, sizeof(u16), &value); 768bfa83a9eSThomas Graf } 769bfa83a9eSThomas Graf 770bfa83a9eSThomas Graf /** 771569a8fc3SDavid S. Miller * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer 772569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 773569a8fc3SDavid S. Miller * @attrtype: attribute type 774569a8fc3SDavid S. Miller * @value: numeric value 775569a8fc3SDavid S. Miller */ 776569a8fc3SDavid S. Miller static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value) 777569a8fc3SDavid S. Miller { 778569a8fc3SDavid S. Miller return nla_put(skb, attrtype, sizeof(__be16), &value); 779569a8fc3SDavid S. Miller } 780569a8fc3SDavid S. Miller 781569a8fc3SDavid S. Miller /** 7826c1dd3b6SDavid S. Miller * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer 7836c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 7846c1dd3b6SDavid S. Miller * @attrtype: attribute type 7856c1dd3b6SDavid S. Miller * @value: numeric value 7866c1dd3b6SDavid S. Miller */ 7876c1dd3b6SDavid S. Miller static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value) 7886c1dd3b6SDavid S. Miller { 7896c1dd3b6SDavid S. Miller return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, value); 7906c1dd3b6SDavid S. Miller } 7916c1dd3b6SDavid S. Miller 7926c1dd3b6SDavid S. Miller /** 79324c410dcSDavid S. Miller * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer 79424c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 79524c410dcSDavid S. Miller * @attrtype: attribute type 79624c410dcSDavid S. Miller * @value: numeric value 79724c410dcSDavid S. Miller */ 79824c410dcSDavid S. Miller static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value) 79924c410dcSDavid S. Miller { 80024c410dcSDavid S. Miller return nla_put(skb, attrtype, sizeof(__le16), &value); 80124c410dcSDavid S. Miller } 80224c410dcSDavid S. Miller 80324c410dcSDavid S. Miller /** 804bfa83a9eSThomas Graf * nla_put_u32 - Add a u32 netlink attribute to a socket buffer 805bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 806bfa83a9eSThomas Graf * @attrtype: attribute type 807bfa83a9eSThomas Graf * @value: numeric value 808bfa83a9eSThomas Graf */ 809bfa83a9eSThomas Graf static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value) 810bfa83a9eSThomas Graf { 811bfa83a9eSThomas Graf return nla_put(skb, attrtype, sizeof(u32), &value); 812bfa83a9eSThomas Graf } 813bfa83a9eSThomas Graf 814bfa83a9eSThomas Graf /** 815569a8fc3SDavid S. Miller * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer 816569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 817569a8fc3SDavid S. Miller * @attrtype: attribute type 818569a8fc3SDavid S. Miller * @value: numeric value 819569a8fc3SDavid S. Miller */ 820569a8fc3SDavid S. Miller static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value) 821569a8fc3SDavid S. Miller { 822569a8fc3SDavid S. Miller return nla_put(skb, attrtype, sizeof(__be32), &value); 823569a8fc3SDavid S. Miller } 824569a8fc3SDavid S. Miller 825569a8fc3SDavid S. Miller /** 8266c1dd3b6SDavid S. Miller * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer 8276c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 8286c1dd3b6SDavid S. Miller * @attrtype: attribute type 8296c1dd3b6SDavid S. Miller * @value: numeric value 8306c1dd3b6SDavid S. Miller */ 8316c1dd3b6SDavid S. Miller static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value) 8326c1dd3b6SDavid S. Miller { 8336c1dd3b6SDavid S. Miller return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, value); 8346c1dd3b6SDavid S. Miller } 8356c1dd3b6SDavid S. Miller 8366c1dd3b6SDavid S. Miller /** 83724c410dcSDavid S. Miller * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer 83824c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 83924c410dcSDavid S. Miller * @attrtype: attribute type 84024c410dcSDavid S. Miller * @value: numeric value 84124c410dcSDavid S. Miller */ 84224c410dcSDavid S. Miller static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value) 84324c410dcSDavid S. Miller { 84424c410dcSDavid S. Miller return nla_put(skb, attrtype, sizeof(__le32), &value); 84524c410dcSDavid S. Miller } 84624c410dcSDavid S. Miller 84724c410dcSDavid S. Miller /** 848569a8fc3SDavid S. Miller * nla_put_u64 - Add a u64 netlink attribute to a socket buffer 849bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 850bfa83a9eSThomas Graf * @attrtype: attribute type 851bfa83a9eSThomas Graf * @value: numeric value 852bfa83a9eSThomas Graf */ 853bfa83a9eSThomas Graf static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value) 854bfa83a9eSThomas Graf { 855bfa83a9eSThomas Graf return nla_put(skb, attrtype, sizeof(u64), &value); 856bfa83a9eSThomas Graf } 857bfa83a9eSThomas Graf 858bfa83a9eSThomas Graf /** 859569a8fc3SDavid S. Miller * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer 860569a8fc3SDavid S. Miller * @skb: socket buffer to add attribute to 861569a8fc3SDavid S. Miller * @attrtype: attribute type 862569a8fc3SDavid S. Miller * @value: numeric value 863569a8fc3SDavid S. Miller */ 864569a8fc3SDavid S. Miller static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value) 865569a8fc3SDavid S. Miller { 866569a8fc3SDavid S. Miller return nla_put(skb, attrtype, sizeof(__be64), &value); 867569a8fc3SDavid S. Miller } 868569a8fc3SDavid S. Miller 869569a8fc3SDavid S. Miller /** 8706c1dd3b6SDavid S. Miller * nla_put_net64 - Add 64-bit network byte order netlink attribute to a socket buffer 8716c1dd3b6SDavid S. Miller * @skb: socket buffer to add attribute to 8726c1dd3b6SDavid S. Miller * @attrtype: attribute type 8736c1dd3b6SDavid S. Miller * @value: numeric value 8746c1dd3b6SDavid S. Miller */ 8756c1dd3b6SDavid S. Miller static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value) 8766c1dd3b6SDavid S. Miller { 8776c1dd3b6SDavid S. Miller return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, value); 8786c1dd3b6SDavid S. Miller } 8796c1dd3b6SDavid S. Miller 8806c1dd3b6SDavid S. Miller /** 88124c410dcSDavid S. Miller * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer 88224c410dcSDavid S. Miller * @skb: socket buffer to add attribute to 88324c410dcSDavid S. Miller * @attrtype: attribute type 88424c410dcSDavid S. Miller * @value: numeric value 88524c410dcSDavid S. Miller */ 88624c410dcSDavid S. Miller static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value) 88724c410dcSDavid S. Miller { 88824c410dcSDavid S. Miller return nla_put(skb, attrtype, sizeof(__le64), &value); 88924c410dcSDavid S. Miller } 89024c410dcSDavid S. Miller 89124c410dcSDavid S. Miller /** 8924778e0beSJiri Pirko * nla_put_s8 - Add a s8 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_s8(struct sk_buff *skb, int attrtype, s8 value) 8984778e0beSJiri Pirko { 8994778e0beSJiri Pirko return nla_put(skb, attrtype, sizeof(s8), &value); 9004778e0beSJiri Pirko } 9014778e0beSJiri Pirko 9024778e0beSJiri Pirko /** 9034778e0beSJiri Pirko * nla_put_s16 - Add a s16 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_s16(struct sk_buff *skb, int attrtype, s16 value) 9094778e0beSJiri Pirko { 9104778e0beSJiri Pirko return nla_put(skb, attrtype, sizeof(s16), &value); 9114778e0beSJiri Pirko } 9124778e0beSJiri Pirko 9134778e0beSJiri Pirko /** 9144778e0beSJiri Pirko * nla_put_s32 - Add a s32 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_s32(struct sk_buff *skb, int attrtype, s32 value) 9204778e0beSJiri Pirko { 9214778e0beSJiri Pirko return nla_put(skb, attrtype, sizeof(s32), &value); 9224778e0beSJiri Pirko } 9234778e0beSJiri Pirko 9244778e0beSJiri Pirko /** 9254778e0beSJiri Pirko * nla_put_s64 - Add a s64 netlink attribute to a socket buffer 9264778e0beSJiri Pirko * @skb: socket buffer to add attribute to 9274778e0beSJiri Pirko * @attrtype: attribute type 9284778e0beSJiri Pirko * @value: numeric value 9294778e0beSJiri Pirko */ 9304778e0beSJiri Pirko static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value) 9314778e0beSJiri Pirko { 9324778e0beSJiri Pirko return nla_put(skb, attrtype, sizeof(s64), &value); 9334778e0beSJiri Pirko } 9344778e0beSJiri Pirko 9354778e0beSJiri Pirko /** 936bfa83a9eSThomas Graf * nla_put_string - Add a string netlink attribute to a socket buffer 937bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 938bfa83a9eSThomas Graf * @attrtype: attribute type 939bfa83a9eSThomas Graf * @str: NUL terminated string 940bfa83a9eSThomas Graf */ 941bfa83a9eSThomas Graf static inline int nla_put_string(struct sk_buff *skb, int attrtype, 942bfa83a9eSThomas Graf const char *str) 943bfa83a9eSThomas Graf { 944bfa83a9eSThomas Graf return nla_put(skb, attrtype, strlen(str) + 1, str); 945bfa83a9eSThomas Graf } 946bfa83a9eSThomas Graf 947bfa83a9eSThomas Graf /** 948bfa83a9eSThomas Graf * nla_put_flag - Add a flag netlink attribute to a socket buffer 949bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 950bfa83a9eSThomas Graf * @attrtype: attribute type 951bfa83a9eSThomas Graf */ 952bfa83a9eSThomas Graf static inline int nla_put_flag(struct sk_buff *skb, int attrtype) 953bfa83a9eSThomas Graf { 954bfa83a9eSThomas Graf return nla_put(skb, attrtype, 0, NULL); 955bfa83a9eSThomas Graf } 956bfa83a9eSThomas Graf 957bfa83a9eSThomas Graf /** 958bfa83a9eSThomas Graf * nla_put_msecs - Add a msecs netlink attribute to a socket buffer 959bfa83a9eSThomas Graf * @skb: socket buffer to add attribute to 960bfa83a9eSThomas Graf * @attrtype: attribute type 961d87de1f3SMark Rustad * @njiffies: number of jiffies to convert to msecs 962bfa83a9eSThomas Graf */ 963bfa83a9eSThomas Graf static inline int nla_put_msecs(struct sk_buff *skb, int attrtype, 964d87de1f3SMark Rustad unsigned long njiffies) 965bfa83a9eSThomas Graf { 966d87de1f3SMark Rustad u64 tmp = jiffies_to_msecs(njiffies); 967bfa83a9eSThomas Graf return nla_put(skb, attrtype, sizeof(u64), &tmp); 968bfa83a9eSThomas Graf } 969bfa83a9eSThomas Graf 970bfa83a9eSThomas Graf /** 971930345eaSJiri Benc * nla_put_in_addr - Add an IPv4 address netlink attribute to a socket 972930345eaSJiri Benc * buffer 973930345eaSJiri Benc * @skb: socket buffer to add attribute to 974930345eaSJiri Benc * @attrtype: attribute type 975930345eaSJiri Benc * @addr: IPv4 address 976930345eaSJiri Benc */ 977930345eaSJiri Benc static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype, 978930345eaSJiri Benc __be32 addr) 979930345eaSJiri Benc { 980930345eaSJiri Benc return nla_put_be32(skb, attrtype, addr); 981930345eaSJiri Benc } 982930345eaSJiri Benc 983930345eaSJiri Benc /** 984930345eaSJiri Benc * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket 985930345eaSJiri Benc * buffer 986930345eaSJiri Benc * @skb: socket buffer to add attribute to 987930345eaSJiri Benc * @attrtype: attribute type 988930345eaSJiri Benc * @addr: IPv6 address 989930345eaSJiri Benc */ 990930345eaSJiri Benc static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype, 991930345eaSJiri Benc const struct in6_addr *addr) 992930345eaSJiri Benc { 993930345eaSJiri Benc return nla_put(skb, attrtype, sizeof(*addr), addr); 994930345eaSJiri Benc } 995930345eaSJiri Benc 996930345eaSJiri Benc /** 997bfa83a9eSThomas Graf * nla_get_u32 - return payload of u32 attribute 998bfa83a9eSThomas Graf * @nla: u32 netlink attribute 999bfa83a9eSThomas Graf */ 1000b057efd4SPatrick McHardy static inline u32 nla_get_u32(const struct nlattr *nla) 1001bfa83a9eSThomas Graf { 1002bfa83a9eSThomas Graf return *(u32 *) nla_data(nla); 1003bfa83a9eSThomas Graf } 1004bfa83a9eSThomas Graf 1005bfa83a9eSThomas Graf /** 100600012e5bSAl Viro * nla_get_be32 - return payload of __be32 attribute 100700012e5bSAl Viro * @nla: __be32 netlink attribute 100800012e5bSAl Viro */ 1009b057efd4SPatrick McHardy static inline __be32 nla_get_be32(const struct nlattr *nla) 101000012e5bSAl Viro { 101100012e5bSAl Viro return *(__be32 *) nla_data(nla); 101200012e5bSAl Viro } 101300012e5bSAl Viro 101400012e5bSAl Viro /** 1015c648a013SAlexander Aring * nla_get_le32 - return payload of __le32 attribute 1016c648a013SAlexander Aring * @nla: __le32 netlink attribute 1017c648a013SAlexander Aring */ 1018c648a013SAlexander Aring static inline __le32 nla_get_le32(const struct nlattr *nla) 1019c648a013SAlexander Aring { 1020c648a013SAlexander Aring return *(__le32 *) nla_data(nla); 1021c648a013SAlexander Aring } 1022c648a013SAlexander Aring 1023c648a013SAlexander Aring /** 1024bfa83a9eSThomas Graf * nla_get_u16 - return payload of u16 attribute 1025bfa83a9eSThomas Graf * @nla: u16 netlink attribute 1026bfa83a9eSThomas Graf */ 1027b057efd4SPatrick McHardy static inline u16 nla_get_u16(const struct nlattr *nla) 1028bfa83a9eSThomas Graf { 1029bfa83a9eSThomas Graf return *(u16 *) nla_data(nla); 1030bfa83a9eSThomas Graf } 1031bfa83a9eSThomas Graf 1032bfa83a9eSThomas Graf /** 1033838965baSPatrick McHardy * nla_get_be16 - return payload of __be16 attribute 1034838965baSPatrick McHardy * @nla: __be16 netlink attribute 1035838965baSPatrick McHardy */ 1036b057efd4SPatrick McHardy static inline __be16 nla_get_be16(const struct nlattr *nla) 1037838965baSPatrick McHardy { 1038838965baSPatrick McHardy return *(__be16 *) nla_data(nla); 1039838965baSPatrick McHardy } 1040838965baSPatrick McHardy 1041838965baSPatrick McHardy /** 10424a89c256SThomas Graf * nla_get_le16 - return payload of __le16 attribute 10434a89c256SThomas Graf * @nla: __le16 netlink attribute 10444a89c256SThomas Graf */ 1045b057efd4SPatrick McHardy static inline __le16 nla_get_le16(const struct nlattr *nla) 10464a89c256SThomas Graf { 10474a89c256SThomas Graf return *(__le16 *) nla_data(nla); 10484a89c256SThomas Graf } 10494a89c256SThomas Graf 10504a89c256SThomas Graf /** 1051bfa83a9eSThomas Graf * nla_get_u8 - return payload of u8 attribute 1052bfa83a9eSThomas Graf * @nla: u8 netlink attribute 1053bfa83a9eSThomas Graf */ 1054b057efd4SPatrick McHardy static inline u8 nla_get_u8(const struct nlattr *nla) 1055bfa83a9eSThomas Graf { 1056bfa83a9eSThomas Graf return *(u8 *) nla_data(nla); 1057bfa83a9eSThomas Graf } 1058bfa83a9eSThomas Graf 1059bfa83a9eSThomas Graf /** 1060bfa83a9eSThomas Graf * nla_get_u64 - return payload of u64 attribute 1061bfa83a9eSThomas Graf * @nla: u64 netlink attribute 1062bfa83a9eSThomas Graf */ 1063b057efd4SPatrick McHardy static inline u64 nla_get_u64(const struct nlattr *nla) 1064bfa83a9eSThomas Graf { 1065bfa83a9eSThomas Graf u64 tmp; 1066bfa83a9eSThomas Graf 1067bfa83a9eSThomas Graf nla_memcpy(&tmp, nla, sizeof(tmp)); 1068bfa83a9eSThomas Graf 1069bfa83a9eSThomas Graf return tmp; 1070bfa83a9eSThomas Graf } 1071bfa83a9eSThomas Graf 1072bfa83a9eSThomas Graf /** 1073a17c8598SPablo Neira Ayuso * nla_get_be64 - return payload of __be64 attribute 1074a17c8598SPablo Neira Ayuso * @nla: __be64 netlink attribute 1075a17c8598SPablo Neira Ayuso */ 1076a17c8598SPablo Neira Ayuso static inline __be64 nla_get_be64(const struct nlattr *nla) 1077a17c8598SPablo Neira Ayuso { 1078f5d410f2SPablo Neira Ayuso __be64 tmp; 1079f5d410f2SPablo Neira Ayuso 1080f5d410f2SPablo Neira Ayuso nla_memcpy(&tmp, nla, sizeof(tmp)); 1081f5d410f2SPablo Neira Ayuso 1082f5d410f2SPablo Neira Ayuso return tmp; 1083a17c8598SPablo Neira Ayuso } 1084a17c8598SPablo Neira Ayuso 1085a17c8598SPablo Neira Ayuso /** 1086c648a013SAlexander Aring * nla_get_le64 - return payload of __le64 attribute 1087c648a013SAlexander Aring * @nla: __le64 netlink attribute 1088c648a013SAlexander Aring */ 1089c648a013SAlexander Aring static inline __le64 nla_get_le64(const struct nlattr *nla) 1090c648a013SAlexander Aring { 1091c648a013SAlexander Aring return *(__le64 *) nla_data(nla); 1092c648a013SAlexander Aring } 1093c648a013SAlexander Aring 1094c648a013SAlexander Aring /** 10954778e0beSJiri Pirko * nla_get_s32 - return payload of s32 attribute 10964778e0beSJiri Pirko * @nla: s32 netlink attribute 10974778e0beSJiri Pirko */ 10984778e0beSJiri Pirko static inline s32 nla_get_s32(const struct nlattr *nla) 10994778e0beSJiri Pirko { 11004778e0beSJiri Pirko return *(s32 *) nla_data(nla); 11014778e0beSJiri Pirko } 11024778e0beSJiri Pirko 11034778e0beSJiri Pirko /** 11044778e0beSJiri Pirko * nla_get_s16 - return payload of s16 attribute 11054778e0beSJiri Pirko * @nla: s16 netlink attribute 11064778e0beSJiri Pirko */ 11074778e0beSJiri Pirko static inline s16 nla_get_s16(const struct nlattr *nla) 11084778e0beSJiri Pirko { 11094778e0beSJiri Pirko return *(s16 *) nla_data(nla); 11104778e0beSJiri Pirko } 11114778e0beSJiri Pirko 11124778e0beSJiri Pirko /** 11134778e0beSJiri Pirko * nla_get_s8 - return payload of s8 attribute 11144778e0beSJiri Pirko * @nla: s8 netlink attribute 11154778e0beSJiri Pirko */ 11164778e0beSJiri Pirko static inline s8 nla_get_s8(const struct nlattr *nla) 11174778e0beSJiri Pirko { 11184778e0beSJiri Pirko return *(s8 *) nla_data(nla); 11194778e0beSJiri Pirko } 11204778e0beSJiri Pirko 11214778e0beSJiri Pirko /** 11224778e0beSJiri Pirko * nla_get_s64 - return payload of s64 attribute 11234778e0beSJiri Pirko * @nla: s64 netlink attribute 11244778e0beSJiri Pirko */ 11254778e0beSJiri Pirko static inline s64 nla_get_s64(const struct nlattr *nla) 11264778e0beSJiri Pirko { 11274778e0beSJiri Pirko s64 tmp; 11284778e0beSJiri Pirko 11294778e0beSJiri Pirko nla_memcpy(&tmp, nla, sizeof(tmp)); 11304778e0beSJiri Pirko 11314778e0beSJiri Pirko return tmp; 11324778e0beSJiri Pirko } 11334778e0beSJiri Pirko 11344778e0beSJiri Pirko /** 1135bfa83a9eSThomas Graf * nla_get_flag - return payload of flag attribute 1136bfa83a9eSThomas Graf * @nla: flag netlink attribute 1137bfa83a9eSThomas Graf */ 1138b057efd4SPatrick McHardy static inline int nla_get_flag(const struct nlattr *nla) 1139bfa83a9eSThomas Graf { 1140bfa83a9eSThomas Graf return !!nla; 1141bfa83a9eSThomas Graf } 1142bfa83a9eSThomas Graf 1143bfa83a9eSThomas Graf /** 1144bfa83a9eSThomas Graf * nla_get_msecs - return payload of msecs attribute 1145bfa83a9eSThomas Graf * @nla: msecs netlink attribute 1146bfa83a9eSThomas Graf * 1147bfa83a9eSThomas Graf * Returns the number of milliseconds in jiffies. 1148bfa83a9eSThomas Graf */ 1149b057efd4SPatrick McHardy static inline unsigned long nla_get_msecs(const struct nlattr *nla) 1150bfa83a9eSThomas Graf { 1151bfa83a9eSThomas Graf u64 msecs = nla_get_u64(nla); 1152bfa83a9eSThomas Graf 1153bfa83a9eSThomas Graf return msecs_to_jiffies((unsigned long) msecs); 1154bfa83a9eSThomas Graf } 1155bfa83a9eSThomas Graf 1156bfa83a9eSThomas Graf /** 115767b61f6cSJiri Benc * nla_get_in_addr - return payload of IPv4 address attribute 115867b61f6cSJiri Benc * @nla: IPv4 address netlink attribute 115967b61f6cSJiri Benc */ 116067b61f6cSJiri Benc static inline __be32 nla_get_in_addr(const struct nlattr *nla) 116167b61f6cSJiri Benc { 116267b61f6cSJiri Benc return *(__be32 *) nla_data(nla); 116367b61f6cSJiri Benc } 116467b61f6cSJiri Benc 116567b61f6cSJiri Benc /** 116667b61f6cSJiri Benc * nla_get_in6_addr - return payload of IPv6 address attribute 116767b61f6cSJiri Benc * @nla: IPv6 address netlink attribute 116867b61f6cSJiri Benc */ 116967b61f6cSJiri Benc static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla) 117067b61f6cSJiri Benc { 117167b61f6cSJiri Benc struct in6_addr tmp; 117267b61f6cSJiri Benc 117367b61f6cSJiri Benc nla_memcpy(&tmp, nla, sizeof(tmp)); 117467b61f6cSJiri Benc return tmp; 117567b61f6cSJiri Benc } 117667b61f6cSJiri Benc 117767b61f6cSJiri Benc /** 1178bfa83a9eSThomas Graf * nla_nest_start - Start a new level of nested attributes 1179bfa83a9eSThomas Graf * @skb: socket buffer to add attributes to 1180bfa83a9eSThomas Graf * @attrtype: attribute type of container 1181bfa83a9eSThomas Graf * 1182bfa83a9eSThomas Graf * Returns the container attribute 1183bfa83a9eSThomas Graf */ 1184bfa83a9eSThomas Graf static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype) 1185bfa83a9eSThomas Graf { 118627a884dcSArnaldo Carvalho de Melo struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb); 1187bfa83a9eSThomas Graf 1188bfa83a9eSThomas Graf if (nla_put(skb, attrtype, 0, NULL) < 0) 1189bfa83a9eSThomas Graf return NULL; 1190bfa83a9eSThomas Graf 1191bfa83a9eSThomas Graf return start; 1192bfa83a9eSThomas Graf } 1193bfa83a9eSThomas Graf 1194bfa83a9eSThomas Graf /** 1195bfa83a9eSThomas Graf * nla_nest_end - Finalize nesting of attributes 1196d1ec3b77SPierre Ynard * @skb: socket buffer the attributes are stored in 1197bfa83a9eSThomas Graf * @start: container attribute 1198bfa83a9eSThomas Graf * 1199bfa83a9eSThomas Graf * Corrects the container attribute header to include the all 1200bfa83a9eSThomas Graf * appeneded attributes. 1201bfa83a9eSThomas Graf * 1202bfa83a9eSThomas Graf * Returns the total data length of the skb. 1203bfa83a9eSThomas Graf */ 1204bfa83a9eSThomas Graf static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start) 1205bfa83a9eSThomas Graf { 120627a884dcSArnaldo Carvalho de Melo start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start; 1207bfa83a9eSThomas Graf return skb->len; 1208bfa83a9eSThomas Graf } 1209bfa83a9eSThomas Graf 1210bfa83a9eSThomas Graf /** 1211bfa83a9eSThomas Graf * nla_nest_cancel - Cancel nesting of attributes 1212bfa83a9eSThomas Graf * @skb: socket buffer the message is stored in 1213bfa83a9eSThomas Graf * @start: container attribute 1214bfa83a9eSThomas Graf * 1215bfa83a9eSThomas Graf * Removes the container attribute and including all nested 1216bc3ed28cSThomas Graf * attributes. Returns -EMSGSIZE 1217bfa83a9eSThomas Graf */ 1218bc3ed28cSThomas Graf static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start) 1219bfa83a9eSThomas Graf { 1220bc3ed28cSThomas Graf nlmsg_trim(skb, start); 1221bfa83a9eSThomas Graf } 1222bfa83a9eSThomas Graf 1223bfa83a9eSThomas Graf /** 12244fe5d5c0SPaul Moore * nla_validate_nested - Validate a stream of nested attributes 12254fe5d5c0SPaul Moore * @start: container attribute 12264fe5d5c0SPaul Moore * @maxtype: maximum attribute type to be expected 12274fe5d5c0SPaul Moore * @policy: validation policy 12284fe5d5c0SPaul Moore * 12294fe5d5c0SPaul Moore * Validates all attributes in the nested attribute stream against the 12304fe5d5c0SPaul Moore * specified policy. Attributes with a type exceeding maxtype will be 12314fe5d5c0SPaul Moore * ignored. See documenation of struct nla_policy for more details. 12324fe5d5c0SPaul Moore * 12334fe5d5c0SPaul Moore * Returns 0 on success or a negative error code. 12344fe5d5c0SPaul Moore */ 12353654654fSJan Engelhardt static inline int nla_validate_nested(const struct nlattr *start, int maxtype, 1236ef7c79edSPatrick McHardy const struct nla_policy *policy) 12374fe5d5c0SPaul Moore { 12384fe5d5c0SPaul Moore return nla_validate(nla_data(start), nla_len(start), maxtype, policy); 12394fe5d5c0SPaul Moore } 12404fe5d5c0SPaul Moore 12414fe5d5c0SPaul Moore /** 1242089bf1a6SNicolas Dichtel * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute 1243089bf1a6SNicolas Dichtel * @skb: socket buffer the message is stored in 1244089bf1a6SNicolas Dichtel * 1245089bf1a6SNicolas Dichtel * Return true if padding is needed to align the next attribute (nla_data()) to 1246089bf1a6SNicolas Dichtel * a 64-bit aligned area. 1247089bf1a6SNicolas Dichtel */ 1248089bf1a6SNicolas Dichtel static inline bool nla_need_padding_for_64bit(struct sk_buff *skb) 1249089bf1a6SNicolas Dichtel { 1250089bf1a6SNicolas Dichtel #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1251089bf1a6SNicolas Dichtel /* The nlattr header is 4 bytes in size, that's why we test 1252089bf1a6SNicolas Dichtel * if the skb->data _is_ aligned. A NOP attribute, plus 1253089bf1a6SNicolas Dichtel * nlattr header for next attribute, will make nla_data() 1254089bf1a6SNicolas Dichtel * 8-byte aligned. 1255089bf1a6SNicolas Dichtel */ 1256089bf1a6SNicolas Dichtel if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8)) 1257089bf1a6SNicolas Dichtel return true; 1258089bf1a6SNicolas Dichtel #endif 1259089bf1a6SNicolas Dichtel return false; 1260089bf1a6SNicolas Dichtel } 1261089bf1a6SNicolas Dichtel 1262089bf1a6SNicolas Dichtel /** 126335c58459SDavid S. Miller * nla_align_64bit - 64-bit align the nla_data() of next attribute 126435c58459SDavid S. Miller * @skb: socket buffer the message is stored in 126535c58459SDavid S. Miller * @padattr: attribute type for the padding 126635c58459SDavid S. Miller * 126735c58459SDavid S. Miller * Conditionally emit a padding netlink attribute in order to make 126835c58459SDavid S. Miller * the next attribute we emit have a 64-bit aligned nla_data() area. 126935c58459SDavid S. Miller * This will only be done in architectures which do not have 1270cca1d815SEric Dumazet * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined. 127135c58459SDavid S. Miller * 127235c58459SDavid S. Miller * Returns zero on success or a negative error code. 127335c58459SDavid S. Miller */ 127435c58459SDavid S. Miller static inline int nla_align_64bit(struct sk_buff *skb, int padattr) 127535c58459SDavid S. Miller { 1276089bf1a6SNicolas Dichtel if (nla_need_padding_for_64bit(skb) && 1277cca1d815SEric Dumazet !nla_reserve(skb, padattr, 0)) 127835c58459SDavid S. Miller return -EMSGSIZE; 1279089bf1a6SNicolas Dichtel 128035c58459SDavid S. Miller return 0; 128135c58459SDavid S. Miller } 128235c58459SDavid S. Miller 128335c58459SDavid S. Miller /** 128435c58459SDavid S. Miller * nla_total_size_64bit - total length of attribute including padding 128535c58459SDavid S. Miller * @payload: length of payload 128635c58459SDavid S. Miller */ 128735c58459SDavid S. Miller static inline int nla_total_size_64bit(int payload) 128835c58459SDavid S. Miller { 128935c58459SDavid S. Miller return NLA_ALIGN(nla_attr_size(payload)) 1290cca1d815SEric Dumazet #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 129135c58459SDavid S. Miller + NLA_ALIGN(nla_attr_size(0)) 129235c58459SDavid S. Miller #endif 129335c58459SDavid S. Miller ; 129435c58459SDavid S. Miller } 129535c58459SDavid S. Miller 129635c58459SDavid S. Miller /** 1297bfa83a9eSThomas Graf * nla_for_each_attr - iterate over a stream of attributes 1298bfa83a9eSThomas Graf * @pos: loop counter, set to current attribute 1299bfa83a9eSThomas Graf * @head: head of attribute stream 1300bfa83a9eSThomas Graf * @len: length of attribute stream 1301bfa83a9eSThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 1302bfa83a9eSThomas Graf */ 1303bfa83a9eSThomas Graf #define nla_for_each_attr(pos, head, len, rem) \ 1304bfa83a9eSThomas Graf for (pos = head, rem = len; \ 1305bfa83a9eSThomas Graf nla_ok(pos, rem); \ 1306bfa83a9eSThomas Graf pos = nla_next(pos, &(rem))) 1307bfa83a9eSThomas Graf 1308fe4944e5SThomas Graf /** 1309fe4944e5SThomas Graf * nla_for_each_nested - iterate over nested attributes 1310fe4944e5SThomas Graf * @pos: loop counter, set to current attribute 1311fe4944e5SThomas Graf * @nla: attribute containing the nested attributes 1312fe4944e5SThomas Graf * @rem: initialized to len, holds bytes currently remaining in stream 1313fe4944e5SThomas Graf */ 1314fe4944e5SThomas Graf #define nla_for_each_nested(pos, nla, rem) \ 1315fe4944e5SThomas Graf nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem) 1316fe4944e5SThomas Graf 1317941d8ebcSSimon Horman /** 1318941d8ebcSSimon Horman * nla_is_last - Test if attribute is last in stream 1319941d8ebcSSimon Horman * @nla: attribute to test 1320941d8ebcSSimon Horman * @rem: bytes remaining in stream 1321941d8ebcSSimon Horman */ 1322941d8ebcSSimon Horman static inline bool nla_is_last(const struct nlattr *nla, int rem) 1323941d8ebcSSimon Horman { 1324941d8ebcSSimon Horman return nla->nla_len == rem; 1325941d8ebcSSimon Horman } 1326941d8ebcSSimon Horman 1327bfa83a9eSThomas Graf #endif 1328