xref: /openbmc/linux/include/net/netlink.h (revision 2175d87c)
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
105756a2f59SNicolas Dichtel  *   nla_put_s64(skb, type, value,
106756a2f59SNicolas Dichtel  *               padattr)		add s64 attribute to skb
107bfa83a9eSThomas Graf  *   nla_put_string(skb, type, str)	add string attribute to skb
108bfa83a9eSThomas Graf  *   nla_put_flag(skb, type)		add flag attribute to skb
1092175d87cSNicolas Dichtel  *   nla_put_msecs(skb, type, jiffies,
1102175d87cSNicolas Dichtel  *                 padattr)		add msecs attribute to skb
111930345eaSJiri Benc  *   nla_put_in_addr(skb, type, addr)	add IPv4 address attribute to skb
112930345eaSJiri Benc  *   nla_put_in6_addr(skb, type, addr)	add IPv6 address attribute to skb
113bfa83a9eSThomas Graf  *
114bfa83a9eSThomas Graf  * Nested Attributes Construction:
115bfa83a9eSThomas Graf  *   nla_nest_start(skb, type)		start a nested attribute
116bfa83a9eSThomas Graf  *   nla_nest_end(skb, nla)		finalize a nested attribute
117bfa83a9eSThomas Graf  *   nla_nest_cancel(skb, nla)		cancel nested attribute construction
118bfa83a9eSThomas Graf  *
119bfa83a9eSThomas Graf  * Attribute Length Calculations:
120bfa83a9eSThomas Graf  *   nla_attr_size(payload)		length of attribute w/o padding
121bfa83a9eSThomas Graf  *   nla_total_size(payload)		length of attribute w/ padding
122bfa83a9eSThomas Graf  *   nla_padlen(payload)		length of padding
123bfa83a9eSThomas Graf  *
124bfa83a9eSThomas Graf  * Attribute Payload Access:
125bfa83a9eSThomas Graf  *   nla_data(nla)			head of attribute payload
126bfa83a9eSThomas Graf  *   nla_len(nla)			length of attribute payload
127bfa83a9eSThomas Graf  *
128bfa83a9eSThomas Graf  * Attribute Payload Access for Basic Types:
129bfa83a9eSThomas Graf  *   nla_get_u8(nla)			get payload for a u8 attribute
130bfa83a9eSThomas Graf  *   nla_get_u16(nla)			get payload for a u16 attribute
131bfa83a9eSThomas Graf  *   nla_get_u32(nla)			get payload for a u32 attribute
132bfa83a9eSThomas Graf  *   nla_get_u64(nla)			get payload for a u64 attribute
1334778e0beSJiri Pirko  *   nla_get_s8(nla)			get payload for a s8 attribute
1344778e0beSJiri Pirko  *   nla_get_s16(nla)			get payload for a s16 attribute
1354778e0beSJiri Pirko  *   nla_get_s32(nla)			get payload for a s32 attribute
1364778e0beSJiri Pirko  *   nla_get_s64(nla)			get payload for a s64 attribute
137bfa83a9eSThomas Graf  *   nla_get_flag(nla)			return 1 if flag is true
138bfa83a9eSThomas Graf  *   nla_get_msecs(nla)			get payload for a msecs attribute
139bfa83a9eSThomas Graf  *
140bfa83a9eSThomas Graf  * Attribute Misc:
141bfa83a9eSThomas Graf  *   nla_memcpy(dest, nla, count)	copy attribute into memory
142bfa83a9eSThomas Graf  *   nla_memcmp(nla, data, size)	compare attribute with memory area
143bfa83a9eSThomas Graf  *   nla_strlcpy(dst, nla, size)	copy attribute to a sized string
144bfa83a9eSThomas Graf  *   nla_strcmp(nla, str)		compare attribute with string
145bfa83a9eSThomas Graf  *
146bfa83a9eSThomas Graf  * Attribute Parsing:
147bfa83a9eSThomas Graf  *   nla_ok(nla, remaining)		does nla fit into remaining bytes?
148bfa83a9eSThomas Graf  *   nla_next(nla, remaining)		get next netlink attribute
149bfa83a9eSThomas Graf  *   nla_validate()			validate a stream of attributes
1504fe5d5c0SPaul Moore  *   nla_validate_nested()		validate a stream of nested attributes
151bfa83a9eSThomas Graf  *   nla_find()				find attribute in stream of attributes
152fe4944e5SThomas Graf  *   nla_find_nested()			find attribute in nested attributes
153bfa83a9eSThomas Graf  *   nla_parse()			parse and validate stream of attrs
154bfa83a9eSThomas Graf  *   nla_parse_nested()			parse nested attribuets
155bfa83a9eSThomas Graf  *   nla_for_each_attr()		loop over all attributes
15622acb19aSPaul Moore  *   nla_for_each_nested()		loop over the nested attributes
157bfa83a9eSThomas Graf  *=========================================================================
158bfa83a9eSThomas Graf  */
159bfa83a9eSThomas Graf 
160bfa83a9eSThomas Graf  /**
161bfa83a9eSThomas Graf   * Standard attribute types to specify validation policy
162bfa83a9eSThomas Graf   */
163bfa83a9eSThomas Graf enum {
164bfa83a9eSThomas Graf 	NLA_UNSPEC,
165bfa83a9eSThomas Graf 	NLA_U8,
166bfa83a9eSThomas Graf 	NLA_U16,
167bfa83a9eSThomas Graf 	NLA_U32,
168bfa83a9eSThomas Graf 	NLA_U64,
169bfa83a9eSThomas Graf 	NLA_STRING,
170bfa83a9eSThomas Graf 	NLA_FLAG,
171bfa83a9eSThomas Graf 	NLA_MSECS,
172bfa83a9eSThomas Graf 	NLA_NESTED,
1731092cb21SPatrick McHardy 	NLA_NESTED_COMPAT,
174a5531a5dSThomas Graf 	NLA_NUL_STRING,
175d30045a0SJohannes Berg 	NLA_BINARY,
1764778e0beSJiri Pirko 	NLA_S8,
1774778e0beSJiri Pirko 	NLA_S16,
1784778e0beSJiri Pirko 	NLA_S32,
1794778e0beSJiri Pirko 	NLA_S64,
180bfa83a9eSThomas Graf 	__NLA_TYPE_MAX,
181bfa83a9eSThomas Graf };
182bfa83a9eSThomas Graf 
183bfa83a9eSThomas Graf #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
184bfa83a9eSThomas Graf 
185bfa83a9eSThomas Graf /**
186bfa83a9eSThomas Graf  * struct nla_policy - attribute validation policy
187bfa83a9eSThomas Graf  * @type: Type of attribute or NLA_UNSPEC
188a5531a5dSThomas Graf  * @len: Type specific length of payload
189bfa83a9eSThomas Graf  *
190bfa83a9eSThomas Graf  * Policies are defined as arrays of this struct, the array must be
191bfa83a9eSThomas Graf  * accessible by attribute type up to the highest identifier to be expected.
192bfa83a9eSThomas Graf  *
193a5531a5dSThomas Graf  * Meaning of `len' field:
194a5531a5dSThomas Graf  *    NLA_STRING           Maximum length of string
195a5531a5dSThomas Graf  *    NLA_NUL_STRING       Maximum length of string (excluding NUL)
196a5531a5dSThomas Graf  *    NLA_FLAG             Unused
197d30045a0SJohannes Berg  *    NLA_BINARY           Maximum length of attribute payload
1984b6cc728SJohannes Berg  *    NLA_NESTED           Don't use `len' field -- length verification is
1994b6cc728SJohannes Berg  *                         done by checking len of nested header (or empty)
2004b6cc728SJohannes Berg  *    NLA_NESTED_COMPAT    Minimum length of structure payload
2014b6cc728SJohannes Berg  *    NLA_U8, NLA_U16,
2024b6cc728SJohannes Berg  *    NLA_U32, NLA_U64,
2034778e0beSJiri Pirko  *    NLA_S8, NLA_S16,
2044778e0beSJiri Pirko  *    NLA_S32, NLA_S64,
2054b6cc728SJohannes Berg  *    NLA_MSECS            Leaving the length field zero will verify the
2064b6cc728SJohannes Berg  *                         given type fits, using it verifies minimum length
2074b6cc728SJohannes Berg  *                         just like "All other"
2084b6cc728SJohannes Berg  *    All other            Minimum length of attribute payload
209a5531a5dSThomas Graf  *
210bfa83a9eSThomas Graf  * Example:
211b54452b0SAlexey Dobriyan  * static const struct nla_policy my_policy[ATTR_MAX+1] = {
212bfa83a9eSThomas Graf  * 	[ATTR_FOO] = { .type = NLA_U16 },
213d30045a0SJohannes Berg  *	[ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
214a5531a5dSThomas Graf  *	[ATTR_BAZ] = { .len = sizeof(struct mystruct) },
215bfa83a9eSThomas Graf  * };
216bfa83a9eSThomas Graf  */
217bfa83a9eSThomas Graf struct nla_policy {
218bfa83a9eSThomas Graf 	u16		type;
219a5531a5dSThomas Graf 	u16		len;
220bfa83a9eSThomas Graf };
221bfa83a9eSThomas Graf 
2224e902c57SThomas Graf /**
2234e902c57SThomas Graf  * struct nl_info - netlink source information
2244e902c57SThomas Graf  * @nlh: Netlink message header of original request
22515e47304SEric W. Biederman  * @portid: Netlink PORTID of requesting application
2264e902c57SThomas Graf  */
2274e902c57SThomas Graf struct nl_info {
2284e902c57SThomas Graf 	struct nlmsghdr		*nlh;
2294d1169c1SDenis V. Lunev 	struct net		*nl_net;
23015e47304SEric W. Biederman 	u32			portid;
2314e902c57SThomas Graf };
2324e902c57SThomas Graf 
2334f69053bSJoe Perches int netlink_rcv_skb(struct sk_buff *skb,
2344f69053bSJoe Perches 		    int (*cb)(struct sk_buff *, struct nlmsghdr *));
2354f69053bSJoe Perches int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
2364f69053bSJoe Perches 		 unsigned int group, int report, gfp_t flags);
23782ace47aSThomas Graf 
2384f69053bSJoe Perches int nla_validate(const struct nlattr *head, int len, int maxtype,
239ef7c79edSPatrick McHardy 		 const struct nla_policy *policy);
2404f69053bSJoe Perches int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head,
2414f69053bSJoe Perches 	      int len, const struct nla_policy *policy);
2424f69053bSJoe Perches int nla_policy_len(const struct nla_policy *, int);
2434f69053bSJoe Perches struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype);
2444f69053bSJoe Perches size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize);
2454f69053bSJoe Perches int nla_memcpy(void *dest, const struct nlattr *src, int count);
2464f69053bSJoe Perches int nla_memcmp(const struct nlattr *nla, const void *data, size_t size);
2474f69053bSJoe Perches int nla_strcmp(const struct nlattr *nla, const char *str);
2484f69053bSJoe Perches struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
249089bf1a6SNicolas Dichtel struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
250089bf1a6SNicolas Dichtel 				   int attrlen, int padattr);
2514f69053bSJoe Perches void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
2524f69053bSJoe Perches struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen);
253089bf1a6SNicolas Dichtel struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype,
254089bf1a6SNicolas Dichtel 				 int attrlen, int padattr);
2554f69053bSJoe Perches void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
2564f69053bSJoe Perches void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
257fe4944e5SThomas Graf 	       const void *data);
258089bf1a6SNicolas Dichtel void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
259089bf1a6SNicolas Dichtel 		     const void *data, int padattr);
2604f69053bSJoe Perches void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
2614f69053bSJoe Perches int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);
262089bf1a6SNicolas Dichtel int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
263089bf1a6SNicolas Dichtel 		  const void *data, int padattr);
2644f69053bSJoe Perches int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data);
2654f69053bSJoe Perches int nla_append(struct sk_buff *skb, int attrlen, const void *data);
266bfa83a9eSThomas Graf 
267bfa83a9eSThomas Graf /**************************************************************************
268bfa83a9eSThomas Graf  * Netlink Messages
269bfa83a9eSThomas Graf  **************************************************************************/
270bfa83a9eSThomas Graf 
271bfa83a9eSThomas Graf /**
272bfa83a9eSThomas Graf  * nlmsg_msg_size - length of netlink message not including padding
273bfa83a9eSThomas Graf  * @payload: length of message payload
274bfa83a9eSThomas Graf  */
275bfa83a9eSThomas Graf static inline int nlmsg_msg_size(int payload)
276bfa83a9eSThomas Graf {
277bfa83a9eSThomas Graf 	return NLMSG_HDRLEN + payload;
278bfa83a9eSThomas Graf }
279bfa83a9eSThomas Graf 
280bfa83a9eSThomas Graf /**
281bfa83a9eSThomas Graf  * nlmsg_total_size - length of netlink message including padding
282bfa83a9eSThomas Graf  * @payload: length of message payload
283bfa83a9eSThomas Graf  */
284bfa83a9eSThomas Graf static inline int nlmsg_total_size(int payload)
285bfa83a9eSThomas Graf {
286bfa83a9eSThomas Graf 	return NLMSG_ALIGN(nlmsg_msg_size(payload));
287bfa83a9eSThomas Graf }
288bfa83a9eSThomas Graf 
289bfa83a9eSThomas Graf /**
290bfa83a9eSThomas Graf  * nlmsg_padlen - length of padding at the message's tail
291bfa83a9eSThomas Graf  * @payload: length of message payload
292bfa83a9eSThomas Graf  */
293bfa83a9eSThomas Graf static inline int nlmsg_padlen(int payload)
294bfa83a9eSThomas Graf {
295bfa83a9eSThomas Graf 	return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
296bfa83a9eSThomas Graf }
297bfa83a9eSThomas Graf 
298bfa83a9eSThomas Graf /**
299bfa83a9eSThomas Graf  * nlmsg_data - head of message payload
30070f23fd6SJustin P. Mattock  * @nlh: netlink message header
301bfa83a9eSThomas Graf  */
302bfa83a9eSThomas Graf static inline void *nlmsg_data(const struct nlmsghdr *nlh)
303bfa83a9eSThomas Graf {
304bfa83a9eSThomas Graf 	return (unsigned char *) nlh + NLMSG_HDRLEN;
305bfa83a9eSThomas Graf }
306bfa83a9eSThomas Graf 
307bfa83a9eSThomas Graf /**
308bfa83a9eSThomas Graf  * nlmsg_len - length of message payload
309bfa83a9eSThomas Graf  * @nlh: netlink message header
310bfa83a9eSThomas Graf  */
311bfa83a9eSThomas Graf static inline int nlmsg_len(const struct nlmsghdr *nlh)
312bfa83a9eSThomas Graf {
313bfa83a9eSThomas Graf 	return nlh->nlmsg_len - NLMSG_HDRLEN;
314bfa83a9eSThomas Graf }
315bfa83a9eSThomas Graf 
316bfa83a9eSThomas Graf /**
317bfa83a9eSThomas Graf  * nlmsg_attrdata - head of attributes data
318bfa83a9eSThomas Graf  * @nlh: netlink message header
319bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
320bfa83a9eSThomas Graf  */
321bfa83a9eSThomas Graf static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
322bfa83a9eSThomas Graf 					    int hdrlen)
323bfa83a9eSThomas Graf {
324bfa83a9eSThomas Graf 	unsigned char *data = nlmsg_data(nlh);
325bfa83a9eSThomas Graf 	return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
326bfa83a9eSThomas Graf }
327bfa83a9eSThomas Graf 
328bfa83a9eSThomas Graf /**
329bfa83a9eSThomas Graf  * nlmsg_attrlen - length of attributes data
330bfa83a9eSThomas Graf  * @nlh: netlink message header
331bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
332bfa83a9eSThomas Graf  */
333bfa83a9eSThomas Graf static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
334bfa83a9eSThomas Graf {
335bfa83a9eSThomas Graf 	return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
336bfa83a9eSThomas Graf }
337bfa83a9eSThomas Graf 
338bfa83a9eSThomas Graf /**
339bfa83a9eSThomas Graf  * nlmsg_ok - check if the netlink message fits into the remaining bytes
340bfa83a9eSThomas Graf  * @nlh: netlink message header
341bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in message stream
342bfa83a9eSThomas Graf  */
343bfa83a9eSThomas Graf static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
344bfa83a9eSThomas Graf {
345619e803dSVegard Nossum 	return (remaining >= (int) sizeof(struct nlmsghdr) &&
346bfa83a9eSThomas Graf 		nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
347bfa83a9eSThomas Graf 		nlh->nlmsg_len <= remaining);
348bfa83a9eSThomas Graf }
349bfa83a9eSThomas Graf 
350bfa83a9eSThomas Graf /**
351bfa83a9eSThomas Graf  * nlmsg_next - next netlink message in message stream
352bfa83a9eSThomas Graf  * @nlh: netlink message header
353bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in message stream
354bfa83a9eSThomas Graf  *
355bfa83a9eSThomas Graf  * Returns the next netlink message in the message stream and
356bfa83a9eSThomas Graf  * decrements remaining by the size of the current message.
357bfa83a9eSThomas Graf  */
3583654654fSJan Engelhardt static inline struct nlmsghdr *
3593654654fSJan Engelhardt nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
360bfa83a9eSThomas Graf {
361bfa83a9eSThomas Graf 	int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
362bfa83a9eSThomas Graf 
363bfa83a9eSThomas Graf 	*remaining -= totlen;
364bfa83a9eSThomas Graf 
365bfa83a9eSThomas Graf 	return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
366bfa83a9eSThomas Graf }
367bfa83a9eSThomas Graf 
368bfa83a9eSThomas Graf /**
369bfa83a9eSThomas Graf  * nlmsg_parse - parse attributes of a netlink message
370bfa83a9eSThomas Graf  * @nlh: netlink message header
371bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
372bfa83a9eSThomas Graf  * @tb: destination array with maxtype+1 elements
373bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
374bfa83a9eSThomas Graf  * @policy: validation policy
375bfa83a9eSThomas Graf  *
376bfa83a9eSThomas Graf  * See nla_parse()
377bfa83a9eSThomas Graf  */
3783a6c2b41SPatrick McHardy static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
379bfa83a9eSThomas Graf 			      struct nlattr *tb[], int maxtype,
380ef7c79edSPatrick McHardy 			      const struct nla_policy *policy)
381bfa83a9eSThomas Graf {
382bfa83a9eSThomas Graf 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
383bfa83a9eSThomas Graf 		return -EINVAL;
384bfa83a9eSThomas Graf 
385bfa83a9eSThomas Graf 	return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
386bfa83a9eSThomas Graf 			 nlmsg_attrlen(nlh, hdrlen), policy);
387bfa83a9eSThomas Graf }
388bfa83a9eSThomas Graf 
389bfa83a9eSThomas Graf /**
390bfa83a9eSThomas Graf  * nlmsg_find_attr - find a specific attribute in a netlink message
391bfa83a9eSThomas Graf  * @nlh: netlink message header
392bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
393bfa83a9eSThomas Graf  * @attrtype: type of attribute to look for
394bfa83a9eSThomas Graf  *
395bfa83a9eSThomas Graf  * Returns the first attribute which matches the specified type.
396bfa83a9eSThomas Graf  */
3976b8c92baSNelson Elhage static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
398bfa83a9eSThomas Graf 					     int hdrlen, int attrtype)
399bfa83a9eSThomas Graf {
400bfa83a9eSThomas Graf 	return nla_find(nlmsg_attrdata(nlh, hdrlen),
401bfa83a9eSThomas Graf 			nlmsg_attrlen(nlh, hdrlen), attrtype);
402bfa83a9eSThomas Graf }
403bfa83a9eSThomas Graf 
404bfa83a9eSThomas Graf /**
405bfa83a9eSThomas Graf  * nlmsg_validate - validate a netlink message including attributes
406bfa83a9eSThomas Graf  * @nlh: netlinket message header
407bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
408bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
409bfa83a9eSThomas Graf  * @policy: validation policy
410bfa83a9eSThomas Graf  */
4113654654fSJan Engelhardt static inline int nlmsg_validate(const struct nlmsghdr *nlh,
4123654654fSJan Engelhardt 				 int hdrlen, int maxtype,
413ef7c79edSPatrick McHardy 				 const struct nla_policy *policy)
414bfa83a9eSThomas Graf {
415bfa83a9eSThomas Graf 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
416bfa83a9eSThomas Graf 		return -EINVAL;
417bfa83a9eSThomas Graf 
418bfa83a9eSThomas Graf 	return nla_validate(nlmsg_attrdata(nlh, hdrlen),
419bfa83a9eSThomas Graf 			    nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
420bfa83a9eSThomas Graf }
421bfa83a9eSThomas Graf 
422bfa83a9eSThomas Graf /**
42397676b6bSThomas Graf  * nlmsg_report - need to report back to application?
42497676b6bSThomas Graf  * @nlh: netlink message header
42597676b6bSThomas Graf  *
42697676b6bSThomas Graf  * Returns 1 if a report back to the application is requested.
42797676b6bSThomas Graf  */
4283a6c2b41SPatrick McHardy static inline int nlmsg_report(const struct nlmsghdr *nlh)
42997676b6bSThomas Graf {
43097676b6bSThomas Graf 	return !!(nlh->nlmsg_flags & NLM_F_ECHO);
43197676b6bSThomas Graf }
43297676b6bSThomas Graf 
43397676b6bSThomas Graf /**
434bfa83a9eSThomas Graf  * nlmsg_for_each_attr - iterate over a stream of attributes
435bfa83a9eSThomas Graf  * @pos: loop counter, set to current attribute
436bfa83a9eSThomas Graf  * @nlh: netlink message header
437bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
438bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
439bfa83a9eSThomas Graf  */
440bfa83a9eSThomas Graf #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
441bfa83a9eSThomas Graf 	nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
442bfa83a9eSThomas Graf 			  nlmsg_attrlen(nlh, hdrlen), rem)
443bfa83a9eSThomas Graf 
444bfa83a9eSThomas Graf /**
445bfa83a9eSThomas Graf  * nlmsg_put - Add a new netlink message to an skb
446bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
4472c6ba4b1SNicolas Dichtel  * @portid: netlink PORTID of requesting application
448bfa83a9eSThomas Graf  * @seq: sequence number of message
449bfa83a9eSThomas Graf  * @type: message type
450bfa83a9eSThomas Graf  * @payload: length of message payload
451bfa83a9eSThomas Graf  * @flags: message flags
452bfa83a9eSThomas Graf  *
453bfa83a9eSThomas Graf  * Returns NULL if the tailroom of the skb is insufficient to store
454bfa83a9eSThomas Graf  * the message header and payload.
455bfa83a9eSThomas Graf  */
45615e47304SEric W. Biederman static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
457bfa83a9eSThomas Graf 					 int type, int payload, int flags)
458bfa83a9eSThomas Graf {
459bfa83a9eSThomas Graf 	if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
460bfa83a9eSThomas Graf 		return NULL;
461bfa83a9eSThomas Graf 
46215e47304SEric W. Biederman 	return __nlmsg_put(skb, portid, seq, type, payload, flags);
463bfa83a9eSThomas Graf }
464bfa83a9eSThomas Graf 
465bfa83a9eSThomas Graf /**
466bfa83a9eSThomas Graf  * nlmsg_put_answer - Add a new callback based netlink message to an skb
467bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
468bfa83a9eSThomas Graf  * @cb: netlink callback
469bfa83a9eSThomas Graf  * @type: message type
470bfa83a9eSThomas Graf  * @payload: length of message payload
471bfa83a9eSThomas Graf  * @flags: message flags
472bfa83a9eSThomas Graf  *
473bfa83a9eSThomas Graf  * Returns NULL if the tailroom of the skb is insufficient to store
474bfa83a9eSThomas Graf  * the message header and payload.
475bfa83a9eSThomas Graf  */
476bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
477bfa83a9eSThomas Graf 						struct netlink_callback *cb,
478bfa83a9eSThomas Graf 						int type, int payload,
479bfa83a9eSThomas Graf 						int flags)
480bfa83a9eSThomas Graf {
48115e47304SEric W. Biederman 	return nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
482bfa83a9eSThomas Graf 			 type, payload, flags);
483bfa83a9eSThomas Graf }
484bfa83a9eSThomas Graf 
485bfa83a9eSThomas Graf /**
486bfa83a9eSThomas Graf  * nlmsg_new - Allocate a new netlink message
487339bf98fSThomas Graf  * @payload: size of the message payload
488fe4944e5SThomas Graf  * @flags: the type of memory to allocate.
489bfa83a9eSThomas Graf  *
490339bf98fSThomas Graf  * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
491339bf98fSThomas Graf  * and a good default is needed.
492bfa83a9eSThomas Graf  */
493339bf98fSThomas Graf static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
494bfa83a9eSThomas Graf {
495339bf98fSThomas Graf 	return alloc_skb(nlmsg_total_size(payload), flags);
496bfa83a9eSThomas Graf }
497bfa83a9eSThomas Graf 
498bfa83a9eSThomas Graf /**
499bfa83a9eSThomas Graf  * nlmsg_end - Finalize a netlink message
500bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
501bfa83a9eSThomas Graf  * @nlh: netlink message header
502bfa83a9eSThomas Graf  *
503bfa83a9eSThomas Graf  * Corrects the netlink message header to include the appeneded
504bfa83a9eSThomas Graf  * attributes. Only necessary if attributes have been added to
505bfa83a9eSThomas Graf  * the message.
506bfa83a9eSThomas Graf  */
507053c095aSJohannes Berg static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
508bfa83a9eSThomas Graf {
50927a884dcSArnaldo Carvalho de Melo 	nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
510bfa83a9eSThomas Graf }
511bfa83a9eSThomas Graf 
512bfa83a9eSThomas Graf /**
513fe4944e5SThomas Graf  * nlmsg_get_pos - return current position in netlink message
514fe4944e5SThomas Graf  * @skb: socket buffer the message is stored in
515fe4944e5SThomas Graf  *
516fe4944e5SThomas Graf  * Returns a pointer to the current tail of the message.
517fe4944e5SThomas Graf  */
518fe4944e5SThomas Graf static inline void *nlmsg_get_pos(struct sk_buff *skb)
519fe4944e5SThomas Graf {
52027a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
521fe4944e5SThomas Graf }
522fe4944e5SThomas Graf 
523fe4944e5SThomas Graf /**
524fe4944e5SThomas Graf  * nlmsg_trim - Trim message to a mark
525fe4944e5SThomas Graf  * @skb: socket buffer the message is stored in
526fe4944e5SThomas Graf  * @mark: mark to trim to
527fe4944e5SThomas Graf  *
528bc3ed28cSThomas Graf  * Trims the message to the provided mark.
529fe4944e5SThomas Graf  */
530bc3ed28cSThomas Graf static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
531fe4944e5SThomas Graf {
532149118d8SThomas Graf 	if (mark) {
533149118d8SThomas Graf 		WARN_ON((unsigned char *) mark < skb->data);
534fe4944e5SThomas Graf 		skb_trim(skb, (unsigned char *) mark - skb->data);
535fe4944e5SThomas Graf 	}
536149118d8SThomas Graf }
537fe4944e5SThomas Graf 
538fe4944e5SThomas Graf /**
539bfa83a9eSThomas Graf  * nlmsg_cancel - Cancel construction of a netlink message
540bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
541bfa83a9eSThomas Graf  * @nlh: netlink message header
542bfa83a9eSThomas Graf  *
543bfa83a9eSThomas Graf  * Removes the complete netlink message including all
544bc3ed28cSThomas Graf  * attributes from the socket buffer again.
545bfa83a9eSThomas Graf  */
546bc3ed28cSThomas Graf static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
547bfa83a9eSThomas Graf {
548bc3ed28cSThomas Graf 	nlmsg_trim(skb, nlh);
549bfa83a9eSThomas Graf }
550bfa83a9eSThomas Graf 
551bfa83a9eSThomas Graf /**
552bfa83a9eSThomas Graf  * nlmsg_free - free a netlink message
553bfa83a9eSThomas Graf  * @skb: socket buffer of netlink message
554bfa83a9eSThomas Graf  */
555bfa83a9eSThomas Graf static inline void nlmsg_free(struct sk_buff *skb)
556bfa83a9eSThomas Graf {
557bfa83a9eSThomas Graf 	kfree_skb(skb);
558bfa83a9eSThomas Graf }
559bfa83a9eSThomas Graf 
560bfa83a9eSThomas Graf /**
561bfa83a9eSThomas Graf  * nlmsg_multicast - multicast a netlink message
562bfa83a9eSThomas Graf  * @sk: netlink socket to spread messages to
563bfa83a9eSThomas Graf  * @skb: netlink message as socket buffer
56415e47304SEric W. Biederman  * @portid: own netlink portid to avoid sending to yourself
565bfa83a9eSThomas Graf  * @group: multicast group id
566d387f6adSThomas Graf  * @flags: allocation flags
567bfa83a9eSThomas Graf  */
568bfa83a9eSThomas Graf static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
56915e47304SEric W. Biederman 				  u32 portid, unsigned int group, gfp_t flags)
570bfa83a9eSThomas Graf {
571bfa83a9eSThomas Graf 	int err;
572bfa83a9eSThomas Graf 
573bfa83a9eSThomas Graf 	NETLINK_CB(skb).dst_group = group;
574bfa83a9eSThomas Graf 
57515e47304SEric W. Biederman 	err = netlink_broadcast(sk, skb, portid, group, flags);
576bfa83a9eSThomas Graf 	if (err > 0)
577bfa83a9eSThomas Graf 		err = 0;
578bfa83a9eSThomas Graf 
579bfa83a9eSThomas Graf 	return err;
580bfa83a9eSThomas Graf }
581bfa83a9eSThomas Graf 
582bfa83a9eSThomas Graf /**
583bfa83a9eSThomas Graf  * nlmsg_unicast - unicast a netlink message
584bfa83a9eSThomas Graf  * @sk: netlink socket to spread message to
585bfa83a9eSThomas Graf  * @skb: netlink message as socket buffer
58615e47304SEric W. Biederman  * @portid: netlink portid of the destination socket
587bfa83a9eSThomas Graf  */
58815e47304SEric W. Biederman static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid)
589bfa83a9eSThomas Graf {
590bfa83a9eSThomas Graf 	int err;
591bfa83a9eSThomas Graf 
59215e47304SEric W. Biederman 	err = netlink_unicast(sk, skb, portid, MSG_DONTWAIT);
593bfa83a9eSThomas Graf 	if (err > 0)
594bfa83a9eSThomas Graf 		err = 0;
595bfa83a9eSThomas Graf 
596bfa83a9eSThomas Graf 	return err;
597bfa83a9eSThomas Graf }
598bfa83a9eSThomas Graf 
599bfa83a9eSThomas Graf /**
600bfa83a9eSThomas Graf  * nlmsg_for_each_msg - iterate over a stream of messages
601bfa83a9eSThomas Graf  * @pos: loop counter, set to current message
602bfa83a9eSThomas Graf  * @head: head of message stream
603bfa83a9eSThomas Graf  * @len: length of message stream
604bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
605bfa83a9eSThomas Graf  */
606bfa83a9eSThomas Graf #define nlmsg_for_each_msg(pos, head, len, rem) \
607bfa83a9eSThomas Graf 	for (pos = head, rem = len; \
608bfa83a9eSThomas Graf 	     nlmsg_ok(pos, rem); \
609bfa83a9eSThomas Graf 	     pos = nlmsg_next(pos, &(rem)))
610bfa83a9eSThomas Graf 
611670dc283SJohannes Berg /**
612670dc283SJohannes Berg  * nl_dump_check_consistent - check if sequence is consistent and advertise if not
613670dc283SJohannes Berg  * @cb: netlink callback structure that stores the sequence number
614670dc283SJohannes Berg  * @nlh: netlink message header to write the flag to
615670dc283SJohannes Berg  *
616670dc283SJohannes Berg  * This function checks if the sequence (generation) number changed during dump
617670dc283SJohannes Berg  * and if it did, advertises it in the netlink message header.
618670dc283SJohannes Berg  *
619670dc283SJohannes Berg  * The correct way to use it is to set cb->seq to the generation counter when
620670dc283SJohannes Berg  * all locks for dumping have been acquired, and then call this function for
621670dc283SJohannes Berg  * each message that is generated.
622670dc283SJohannes Berg  *
623670dc283SJohannes Berg  * Note that due to initialisation concerns, 0 is an invalid sequence number
624670dc283SJohannes Berg  * and must not be used by code that uses this functionality.
625670dc283SJohannes Berg  */
626670dc283SJohannes Berg static inline void
627670dc283SJohannes Berg nl_dump_check_consistent(struct netlink_callback *cb,
628670dc283SJohannes Berg 			 struct nlmsghdr *nlh)
629670dc283SJohannes Berg {
630670dc283SJohannes Berg 	if (cb->prev_seq && cb->seq != cb->prev_seq)
631670dc283SJohannes Berg 		nlh->nlmsg_flags |= NLM_F_DUMP_INTR;
632670dc283SJohannes Berg 	cb->prev_seq = cb->seq;
633670dc283SJohannes Berg }
634670dc283SJohannes Berg 
635bfa83a9eSThomas Graf /**************************************************************************
636bfa83a9eSThomas Graf  * Netlink Attributes
637bfa83a9eSThomas Graf  **************************************************************************/
638bfa83a9eSThomas Graf 
639bfa83a9eSThomas Graf /**
640bfa83a9eSThomas Graf  * nla_attr_size - length of attribute not including padding
641bfa83a9eSThomas Graf  * @payload: length of payload
642bfa83a9eSThomas Graf  */
643bfa83a9eSThomas Graf static inline int nla_attr_size(int payload)
644bfa83a9eSThomas Graf {
645bfa83a9eSThomas Graf 	return NLA_HDRLEN + payload;
646bfa83a9eSThomas Graf }
647bfa83a9eSThomas Graf 
648bfa83a9eSThomas Graf /**
649bfa83a9eSThomas Graf  * nla_total_size - total length of attribute including padding
650bfa83a9eSThomas Graf  * @payload: length of payload
651bfa83a9eSThomas Graf  */
652bfa83a9eSThomas Graf static inline int nla_total_size(int payload)
653bfa83a9eSThomas Graf {
654bfa83a9eSThomas Graf 	return NLA_ALIGN(nla_attr_size(payload));
655bfa83a9eSThomas Graf }
656bfa83a9eSThomas Graf 
657bfa83a9eSThomas Graf /**
658bfa83a9eSThomas Graf  * nla_padlen - length of padding at the tail of attribute
659bfa83a9eSThomas Graf  * @payload: length of payload
660bfa83a9eSThomas Graf  */
661bfa83a9eSThomas Graf static inline int nla_padlen(int payload)
662bfa83a9eSThomas Graf {
663bfa83a9eSThomas Graf 	return nla_total_size(payload) - nla_attr_size(payload);
664bfa83a9eSThomas Graf }
665bfa83a9eSThomas Graf 
666bfa83a9eSThomas Graf /**
6678f4c1f9bSThomas Graf  * nla_type - attribute type
6688f4c1f9bSThomas Graf  * @nla: netlink attribute
6698f4c1f9bSThomas Graf  */
6708f4c1f9bSThomas Graf static inline int nla_type(const struct nlattr *nla)
6718f4c1f9bSThomas Graf {
6728f4c1f9bSThomas Graf 	return nla->nla_type & NLA_TYPE_MASK;
6738f4c1f9bSThomas Graf }
6748f4c1f9bSThomas Graf 
6758f4c1f9bSThomas Graf /**
676bfa83a9eSThomas Graf  * nla_data - head of payload
677bfa83a9eSThomas Graf  * @nla: netlink attribute
678bfa83a9eSThomas Graf  */
679bfa83a9eSThomas Graf static inline void *nla_data(const struct nlattr *nla)
680bfa83a9eSThomas Graf {
681bfa83a9eSThomas Graf 	return (char *) nla + NLA_HDRLEN;
682bfa83a9eSThomas Graf }
683bfa83a9eSThomas Graf 
684bfa83a9eSThomas Graf /**
685bfa83a9eSThomas Graf  * nla_len - length of payload
686bfa83a9eSThomas Graf  * @nla: netlink attribute
687bfa83a9eSThomas Graf  */
688bfa83a9eSThomas Graf static inline int nla_len(const struct nlattr *nla)
689bfa83a9eSThomas Graf {
690bfa83a9eSThomas Graf 	return nla->nla_len - NLA_HDRLEN;
691bfa83a9eSThomas Graf }
692bfa83a9eSThomas Graf 
693bfa83a9eSThomas Graf /**
694bfa83a9eSThomas Graf  * nla_ok - check if the netlink attribute fits into the remaining bytes
695bfa83a9eSThomas Graf  * @nla: netlink attribute
696bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in attribute stream
697bfa83a9eSThomas Graf  */
698bfa83a9eSThomas Graf static inline int nla_ok(const struct nlattr *nla, int remaining)
699bfa83a9eSThomas Graf {
7001045b03eSVegard Nossum 	return remaining >= (int) sizeof(*nla) &&
701bfa83a9eSThomas Graf 	       nla->nla_len >= sizeof(*nla) &&
702bfa83a9eSThomas Graf 	       nla->nla_len <= remaining;
703bfa83a9eSThomas Graf }
704bfa83a9eSThomas Graf 
705bfa83a9eSThomas Graf /**
706d1ec3b77SPierre Ynard  * nla_next - next netlink attribute in attribute stream
707bfa83a9eSThomas Graf  * @nla: netlink attribute
708bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in attribute stream
709bfa83a9eSThomas Graf  *
710bfa83a9eSThomas Graf  * Returns the next netlink attribute in the attribute stream and
711bfa83a9eSThomas Graf  * decrements remaining by the size of the current attribute.
712bfa83a9eSThomas Graf  */
713bfa83a9eSThomas Graf static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
714bfa83a9eSThomas Graf {
715bfa83a9eSThomas Graf 	int totlen = NLA_ALIGN(nla->nla_len);
716bfa83a9eSThomas Graf 
717bfa83a9eSThomas Graf 	*remaining -= totlen;
718bfa83a9eSThomas Graf 	return (struct nlattr *) ((char *) nla + totlen);
719bfa83a9eSThomas Graf }
720bfa83a9eSThomas Graf 
721bfa83a9eSThomas Graf /**
722fe4944e5SThomas Graf  * nla_find_nested - find attribute in a set of nested attributes
723fe4944e5SThomas Graf  * @nla: attribute containing the nested attributes
724fe4944e5SThomas Graf  * @attrtype: type of attribute to look for
725fe4944e5SThomas Graf  *
726fe4944e5SThomas Graf  * Returns the first attribute which matches the specified type.
727fe4944e5SThomas Graf  */
7283654654fSJan Engelhardt static inline struct nlattr *
7293654654fSJan Engelhardt nla_find_nested(const struct nlattr *nla, int attrtype)
730fe4944e5SThomas Graf {
731fe4944e5SThomas Graf 	return nla_find(nla_data(nla), nla_len(nla), attrtype);
732fe4944e5SThomas Graf }
733fe4944e5SThomas Graf 
734fe4944e5SThomas Graf /**
735bfa83a9eSThomas Graf  * nla_parse_nested - parse nested attributes
736bfa83a9eSThomas Graf  * @tb: destination array with maxtype+1 elements
737bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
738bfa83a9eSThomas Graf  * @nla: attribute containing the nested attributes
739bfa83a9eSThomas Graf  * @policy: validation policy
740bfa83a9eSThomas Graf  *
741bfa83a9eSThomas Graf  * See nla_parse()
742bfa83a9eSThomas Graf  */
743bfa83a9eSThomas Graf static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
744b057efd4SPatrick McHardy 				   const struct nlattr *nla,
745ef7c79edSPatrick McHardy 				   const struct nla_policy *policy)
746bfa83a9eSThomas Graf {
747bfa83a9eSThomas Graf 	return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
748bfa83a9eSThomas Graf }
7491092cb21SPatrick McHardy 
7501092cb21SPatrick McHardy /**
751d1ec3b77SPierre Ynard  * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
752bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
753bfa83a9eSThomas Graf  * @attrtype: attribute type
754bfa83a9eSThomas Graf  * @value: numeric value
755bfa83a9eSThomas Graf  */
756bfa83a9eSThomas Graf static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
757bfa83a9eSThomas Graf {
758bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, sizeof(u8), &value);
759bfa83a9eSThomas Graf }
760bfa83a9eSThomas Graf 
761bfa83a9eSThomas Graf /**
762bfa83a9eSThomas Graf  * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
763bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
764bfa83a9eSThomas Graf  * @attrtype: attribute type
765bfa83a9eSThomas Graf  * @value: numeric value
766bfa83a9eSThomas Graf  */
767bfa83a9eSThomas Graf static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
768bfa83a9eSThomas Graf {
769bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, sizeof(u16), &value);
770bfa83a9eSThomas Graf }
771bfa83a9eSThomas Graf 
772bfa83a9eSThomas Graf /**
773569a8fc3SDavid S. Miller  * nla_put_be16 - Add a __be16 netlink attribute to a socket buffer
774569a8fc3SDavid S. Miller  * @skb: socket buffer to add attribute to
775569a8fc3SDavid S. Miller  * @attrtype: attribute type
776569a8fc3SDavid S. Miller  * @value: numeric value
777569a8fc3SDavid S. Miller  */
778569a8fc3SDavid S. Miller static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
779569a8fc3SDavid S. Miller {
780569a8fc3SDavid S. Miller 	return nla_put(skb, attrtype, sizeof(__be16), &value);
781569a8fc3SDavid S. Miller }
782569a8fc3SDavid S. Miller 
783569a8fc3SDavid S. Miller /**
7846c1dd3b6SDavid S. Miller  * nla_put_net16 - Add 16-bit network byte order netlink attribute to a socket buffer
7856c1dd3b6SDavid S. Miller  * @skb: socket buffer to add attribute to
7866c1dd3b6SDavid S. Miller  * @attrtype: attribute type
7876c1dd3b6SDavid S. Miller  * @value: numeric value
7886c1dd3b6SDavid S. Miller  */
7896c1dd3b6SDavid S. Miller static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
7906c1dd3b6SDavid S. Miller {
7916c1dd3b6SDavid S. Miller 	return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, value);
7926c1dd3b6SDavid S. Miller }
7936c1dd3b6SDavid S. Miller 
7946c1dd3b6SDavid S. Miller /**
79524c410dcSDavid S. Miller  * nla_put_le16 - Add a __le16 netlink attribute to a socket buffer
79624c410dcSDavid S. Miller  * @skb: socket buffer to add attribute to
79724c410dcSDavid S. Miller  * @attrtype: attribute type
79824c410dcSDavid S. Miller  * @value: numeric value
79924c410dcSDavid S. Miller  */
80024c410dcSDavid S. Miller static inline int nla_put_le16(struct sk_buff *skb, int attrtype, __le16 value)
80124c410dcSDavid S. Miller {
80224c410dcSDavid S. Miller 	return nla_put(skb, attrtype, sizeof(__le16), &value);
80324c410dcSDavid S. Miller }
80424c410dcSDavid S. Miller 
80524c410dcSDavid S. Miller /**
806bfa83a9eSThomas Graf  * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
807bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
808bfa83a9eSThomas Graf  * @attrtype: attribute type
809bfa83a9eSThomas Graf  * @value: numeric value
810bfa83a9eSThomas Graf  */
811bfa83a9eSThomas Graf static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
812bfa83a9eSThomas Graf {
813bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, sizeof(u32), &value);
814bfa83a9eSThomas Graf }
815bfa83a9eSThomas Graf 
816bfa83a9eSThomas Graf /**
817569a8fc3SDavid S. Miller  * nla_put_be32 - Add a __be32 netlink attribute to a socket buffer
818569a8fc3SDavid S. Miller  * @skb: socket buffer to add attribute to
819569a8fc3SDavid S. Miller  * @attrtype: attribute type
820569a8fc3SDavid S. Miller  * @value: numeric value
821569a8fc3SDavid S. Miller  */
822569a8fc3SDavid S. Miller static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
823569a8fc3SDavid S. Miller {
824569a8fc3SDavid S. Miller 	return nla_put(skb, attrtype, sizeof(__be32), &value);
825569a8fc3SDavid S. Miller }
826569a8fc3SDavid S. Miller 
827569a8fc3SDavid S. Miller /**
8286c1dd3b6SDavid S. Miller  * nla_put_net32 - Add 32-bit network byte order netlink attribute to a socket buffer
8296c1dd3b6SDavid S. Miller  * @skb: socket buffer to add attribute to
8306c1dd3b6SDavid S. Miller  * @attrtype: attribute type
8316c1dd3b6SDavid S. Miller  * @value: numeric value
8326c1dd3b6SDavid S. Miller  */
8336c1dd3b6SDavid S. Miller static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
8346c1dd3b6SDavid S. Miller {
8356c1dd3b6SDavid S. Miller 	return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, value);
8366c1dd3b6SDavid S. Miller }
8376c1dd3b6SDavid S. Miller 
8386c1dd3b6SDavid S. Miller /**
83924c410dcSDavid S. Miller  * nla_put_le32 - Add a __le32 netlink attribute to a socket buffer
84024c410dcSDavid S. Miller  * @skb: socket buffer to add attribute to
84124c410dcSDavid S. Miller  * @attrtype: attribute type
84224c410dcSDavid S. Miller  * @value: numeric value
84324c410dcSDavid S. Miller  */
84424c410dcSDavid S. Miller static inline int nla_put_le32(struct sk_buff *skb, int attrtype, __le32 value)
84524c410dcSDavid S. Miller {
84624c410dcSDavid S. Miller 	return nla_put(skb, attrtype, sizeof(__le32), &value);
84724c410dcSDavid S. Miller }
84824c410dcSDavid S. Miller 
84924c410dcSDavid S. Miller /**
850569a8fc3SDavid S. Miller  * nla_put_u64 - Add a u64 netlink attribute to a socket buffer
851bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
852bfa83a9eSThomas Graf  * @attrtype: attribute type
853bfa83a9eSThomas Graf  * @value: numeric value
854bfa83a9eSThomas Graf  */
855bfa83a9eSThomas Graf static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value)
856bfa83a9eSThomas Graf {
857bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, sizeof(u64), &value);
858bfa83a9eSThomas Graf }
859bfa83a9eSThomas Graf 
860bfa83a9eSThomas Graf /**
861b46f6dedSNicolas Dichtel  * nla_put_be64 - Add a __be64 netlink attribute to a socket buffer and align it
862569a8fc3SDavid S. Miller  * @skb: socket buffer to add attribute to
863569a8fc3SDavid S. Miller  * @attrtype: attribute type
864569a8fc3SDavid S. Miller  * @value: numeric value
865b46f6dedSNicolas Dichtel  * @padattr: attribute type for the padding
866569a8fc3SDavid S. Miller  */
867b46f6dedSNicolas Dichtel static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value,
868b46f6dedSNicolas Dichtel 			       int padattr)
869b46f6dedSNicolas Dichtel {
870b46f6dedSNicolas Dichtel 	return nla_put_64bit(skb, attrtype, sizeof(__be64), &value, padattr);
871b46f6dedSNicolas Dichtel }
872b46f6dedSNicolas Dichtel 
873569a8fc3SDavid S. Miller /**
874e9bbe898SNicolas Dichtel  * nla_put_net64 - Add 64-bit network byte order nlattr to a skb and align it
8756c1dd3b6SDavid S. Miller  * @skb: socket buffer to add attribute to
8766c1dd3b6SDavid S. Miller  * @attrtype: attribute type
8776c1dd3b6SDavid S. Miller  * @value: numeric value
878e9bbe898SNicolas Dichtel  * @padattr: attribute type for the padding
8796c1dd3b6SDavid S. Miller  */
880e9bbe898SNicolas Dichtel static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value,
881e9bbe898SNicolas Dichtel 				int padattr)
8826c1dd3b6SDavid S. Miller {
883e9bbe898SNicolas Dichtel 	return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, value,
884e9bbe898SNicolas Dichtel 			    padattr);
8856c1dd3b6SDavid S. Miller }
8866c1dd3b6SDavid S. Miller 
8876c1dd3b6SDavid S. Miller /**
888e7479122SNicolas Dichtel  * nla_put_le64 - Add a __le64 netlink attribute to a socket buffer and align it
88924c410dcSDavid S. Miller  * @skb: socket buffer to add attribute to
89024c410dcSDavid S. Miller  * @attrtype: attribute type
89124c410dcSDavid S. Miller  * @value: numeric value
892e7479122SNicolas Dichtel  * @padattr: attribute type for the padding
89324c410dcSDavid S. Miller  */
894e7479122SNicolas Dichtel static inline int nla_put_le64(struct sk_buff *skb, int attrtype, __le64 value,
895e7479122SNicolas Dichtel 			       int padattr)
89624c410dcSDavid S. Miller {
897e7479122SNicolas Dichtel 	return nla_put_64bit(skb, attrtype, sizeof(__le64), &value, padattr);
89824c410dcSDavid S. Miller }
89924c410dcSDavid S. Miller 
90024c410dcSDavid S. Miller /**
9014778e0beSJiri Pirko  * nla_put_s8 - Add a s8 netlink attribute to a socket buffer
9024778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
9034778e0beSJiri Pirko  * @attrtype: attribute type
9044778e0beSJiri Pirko  * @value: numeric value
9054778e0beSJiri Pirko  */
9064778e0beSJiri Pirko static inline int nla_put_s8(struct sk_buff *skb, int attrtype, s8 value)
9074778e0beSJiri Pirko {
9084778e0beSJiri Pirko 	return nla_put(skb, attrtype, sizeof(s8), &value);
9094778e0beSJiri Pirko }
9104778e0beSJiri Pirko 
9114778e0beSJiri Pirko /**
9124778e0beSJiri Pirko  * nla_put_s16 - Add a s16 netlink attribute to a socket buffer
9134778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
9144778e0beSJiri Pirko  * @attrtype: attribute type
9154778e0beSJiri Pirko  * @value: numeric value
9164778e0beSJiri Pirko  */
9174778e0beSJiri Pirko static inline int nla_put_s16(struct sk_buff *skb, int attrtype, s16 value)
9184778e0beSJiri Pirko {
9194778e0beSJiri Pirko 	return nla_put(skb, attrtype, sizeof(s16), &value);
9204778e0beSJiri Pirko }
9214778e0beSJiri Pirko 
9224778e0beSJiri Pirko /**
9234778e0beSJiri Pirko  * nla_put_s32 - Add a s32 netlink attribute to a socket buffer
9244778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
9254778e0beSJiri Pirko  * @attrtype: attribute type
9264778e0beSJiri Pirko  * @value: numeric value
9274778e0beSJiri Pirko  */
9284778e0beSJiri Pirko static inline int nla_put_s32(struct sk_buff *skb, int attrtype, s32 value)
9294778e0beSJiri Pirko {
9304778e0beSJiri Pirko 	return nla_put(skb, attrtype, sizeof(s32), &value);
9314778e0beSJiri Pirko }
9324778e0beSJiri Pirko 
9334778e0beSJiri Pirko /**
934756a2f59SNicolas Dichtel  * nla_put_s64 - Add a s64 netlink attribute to a socket buffer and align it
9354778e0beSJiri Pirko  * @skb: socket buffer to add attribute to
9364778e0beSJiri Pirko  * @attrtype: attribute type
9374778e0beSJiri Pirko  * @value: numeric value
938756a2f59SNicolas Dichtel  * @padattr: attribute type for the padding
9394778e0beSJiri Pirko  */
940756a2f59SNicolas Dichtel static inline int nla_put_s64(struct sk_buff *skb, int attrtype, s64 value,
941756a2f59SNicolas Dichtel 			      int padattr)
9424778e0beSJiri Pirko {
943756a2f59SNicolas Dichtel 	return nla_put_64bit(skb, attrtype, sizeof(s64), &value, padattr);
9444778e0beSJiri Pirko }
9454778e0beSJiri Pirko 
9464778e0beSJiri Pirko /**
947bfa83a9eSThomas Graf  * nla_put_string - Add a string netlink attribute to a socket buffer
948bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
949bfa83a9eSThomas Graf  * @attrtype: attribute type
950bfa83a9eSThomas Graf  * @str: NUL terminated string
951bfa83a9eSThomas Graf  */
952bfa83a9eSThomas Graf static inline int nla_put_string(struct sk_buff *skb, int attrtype,
953bfa83a9eSThomas Graf 				 const char *str)
954bfa83a9eSThomas Graf {
955bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, strlen(str) + 1, str);
956bfa83a9eSThomas Graf }
957bfa83a9eSThomas Graf 
958bfa83a9eSThomas Graf /**
959bfa83a9eSThomas Graf  * nla_put_flag - Add a flag netlink attribute to a socket buffer
960bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
961bfa83a9eSThomas Graf  * @attrtype: attribute type
962bfa83a9eSThomas Graf  */
963bfa83a9eSThomas Graf static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
964bfa83a9eSThomas Graf {
965bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, 0, NULL);
966bfa83a9eSThomas Graf }
967bfa83a9eSThomas Graf 
968bfa83a9eSThomas Graf /**
9692175d87cSNicolas Dichtel  * nla_put_msecs - Add a msecs netlink attribute to a skb and align it
970bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
971bfa83a9eSThomas Graf  * @attrtype: attribute type
972d87de1f3SMark Rustad  * @njiffies: number of jiffies to convert to msecs
9732175d87cSNicolas Dichtel  * @padattr: attribute type for the padding
974bfa83a9eSThomas Graf  */
975bfa83a9eSThomas Graf static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
9762175d87cSNicolas Dichtel 				unsigned long njiffies, int padattr)
977bfa83a9eSThomas Graf {
978d87de1f3SMark Rustad 	u64 tmp = jiffies_to_msecs(njiffies);
9792175d87cSNicolas Dichtel 
9802175d87cSNicolas Dichtel 	return nla_put_64bit(skb, attrtype, sizeof(u64), &tmp, padattr);
981bfa83a9eSThomas Graf }
982bfa83a9eSThomas Graf 
983bfa83a9eSThomas Graf /**
984930345eaSJiri Benc  * nla_put_in_addr - Add an IPv4 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: IPv4 address
989930345eaSJiri Benc  */
990930345eaSJiri Benc static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
991930345eaSJiri Benc 				  __be32 addr)
992930345eaSJiri Benc {
993930345eaSJiri Benc 	return nla_put_be32(skb, attrtype, addr);
994930345eaSJiri Benc }
995930345eaSJiri Benc 
996930345eaSJiri Benc /**
997930345eaSJiri Benc  * nla_put_in6_addr - Add an IPv6 address netlink attribute to a socket
998930345eaSJiri Benc  * buffer
999930345eaSJiri Benc  * @skb: socket buffer to add attribute to
1000930345eaSJiri Benc  * @attrtype: attribute type
1001930345eaSJiri Benc  * @addr: IPv6 address
1002930345eaSJiri Benc  */
1003930345eaSJiri Benc static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype,
1004930345eaSJiri Benc 				   const struct in6_addr *addr)
1005930345eaSJiri Benc {
1006930345eaSJiri Benc 	return nla_put(skb, attrtype, sizeof(*addr), addr);
1007930345eaSJiri Benc }
1008930345eaSJiri Benc 
1009930345eaSJiri Benc /**
1010bfa83a9eSThomas Graf  * nla_get_u32 - return payload of u32 attribute
1011bfa83a9eSThomas Graf  * @nla: u32 netlink attribute
1012bfa83a9eSThomas Graf  */
1013b057efd4SPatrick McHardy static inline u32 nla_get_u32(const struct nlattr *nla)
1014bfa83a9eSThomas Graf {
1015bfa83a9eSThomas Graf 	return *(u32 *) nla_data(nla);
1016bfa83a9eSThomas Graf }
1017bfa83a9eSThomas Graf 
1018bfa83a9eSThomas Graf /**
101900012e5bSAl Viro  * nla_get_be32 - return payload of __be32 attribute
102000012e5bSAl Viro  * @nla: __be32 netlink attribute
102100012e5bSAl Viro  */
1022b057efd4SPatrick McHardy static inline __be32 nla_get_be32(const struct nlattr *nla)
102300012e5bSAl Viro {
102400012e5bSAl Viro 	return *(__be32 *) nla_data(nla);
102500012e5bSAl Viro }
102600012e5bSAl Viro 
102700012e5bSAl Viro /**
1028c648a013SAlexander Aring  * nla_get_le32 - return payload of __le32 attribute
1029c648a013SAlexander Aring  * @nla: __le32 netlink attribute
1030c648a013SAlexander Aring  */
1031c648a013SAlexander Aring static inline __le32 nla_get_le32(const struct nlattr *nla)
1032c648a013SAlexander Aring {
1033c648a013SAlexander Aring 	return *(__le32 *) nla_data(nla);
1034c648a013SAlexander Aring }
1035c648a013SAlexander Aring 
1036c648a013SAlexander Aring /**
1037bfa83a9eSThomas Graf  * nla_get_u16 - return payload of u16 attribute
1038bfa83a9eSThomas Graf  * @nla: u16 netlink attribute
1039bfa83a9eSThomas Graf  */
1040b057efd4SPatrick McHardy static inline u16 nla_get_u16(const struct nlattr *nla)
1041bfa83a9eSThomas Graf {
1042bfa83a9eSThomas Graf 	return *(u16 *) nla_data(nla);
1043bfa83a9eSThomas Graf }
1044bfa83a9eSThomas Graf 
1045bfa83a9eSThomas Graf /**
1046838965baSPatrick McHardy  * nla_get_be16 - return payload of __be16 attribute
1047838965baSPatrick McHardy  * @nla: __be16 netlink attribute
1048838965baSPatrick McHardy  */
1049b057efd4SPatrick McHardy static inline __be16 nla_get_be16(const struct nlattr *nla)
1050838965baSPatrick McHardy {
1051838965baSPatrick McHardy 	return *(__be16 *) nla_data(nla);
1052838965baSPatrick McHardy }
1053838965baSPatrick McHardy 
1054838965baSPatrick McHardy /**
10554a89c256SThomas Graf  * nla_get_le16 - return payload of __le16 attribute
10564a89c256SThomas Graf  * @nla: __le16 netlink attribute
10574a89c256SThomas Graf  */
1058b057efd4SPatrick McHardy static inline __le16 nla_get_le16(const struct nlattr *nla)
10594a89c256SThomas Graf {
10604a89c256SThomas Graf 	return *(__le16 *) nla_data(nla);
10614a89c256SThomas Graf }
10624a89c256SThomas Graf 
10634a89c256SThomas Graf /**
1064bfa83a9eSThomas Graf  * nla_get_u8 - return payload of u8 attribute
1065bfa83a9eSThomas Graf  * @nla: u8 netlink attribute
1066bfa83a9eSThomas Graf  */
1067b057efd4SPatrick McHardy static inline u8 nla_get_u8(const struct nlattr *nla)
1068bfa83a9eSThomas Graf {
1069bfa83a9eSThomas Graf 	return *(u8 *) nla_data(nla);
1070bfa83a9eSThomas Graf }
1071bfa83a9eSThomas Graf 
1072bfa83a9eSThomas Graf /**
1073bfa83a9eSThomas Graf  * nla_get_u64 - return payload of u64 attribute
1074bfa83a9eSThomas Graf  * @nla: u64 netlink attribute
1075bfa83a9eSThomas Graf  */
1076b057efd4SPatrick McHardy static inline u64 nla_get_u64(const struct nlattr *nla)
1077bfa83a9eSThomas Graf {
1078bfa83a9eSThomas Graf 	u64 tmp;
1079bfa83a9eSThomas Graf 
1080bfa83a9eSThomas Graf 	nla_memcpy(&tmp, nla, sizeof(tmp));
1081bfa83a9eSThomas Graf 
1082bfa83a9eSThomas Graf 	return tmp;
1083bfa83a9eSThomas Graf }
1084bfa83a9eSThomas Graf 
1085bfa83a9eSThomas Graf /**
1086a17c8598SPablo Neira Ayuso  * nla_get_be64 - return payload of __be64 attribute
1087a17c8598SPablo Neira Ayuso  * @nla: __be64 netlink attribute
1088a17c8598SPablo Neira Ayuso  */
1089a17c8598SPablo Neira Ayuso static inline __be64 nla_get_be64(const struct nlattr *nla)
1090a17c8598SPablo Neira Ayuso {
1091f5d410f2SPablo Neira Ayuso 	__be64 tmp;
1092f5d410f2SPablo Neira Ayuso 
1093f5d410f2SPablo Neira Ayuso 	nla_memcpy(&tmp, nla, sizeof(tmp));
1094f5d410f2SPablo Neira Ayuso 
1095f5d410f2SPablo Neira Ayuso 	return tmp;
1096a17c8598SPablo Neira Ayuso }
1097a17c8598SPablo Neira Ayuso 
1098a17c8598SPablo Neira Ayuso /**
1099c648a013SAlexander Aring  * nla_get_le64 - return payload of __le64 attribute
1100c648a013SAlexander Aring  * @nla: __le64 netlink attribute
1101c648a013SAlexander Aring  */
1102c648a013SAlexander Aring static inline __le64 nla_get_le64(const struct nlattr *nla)
1103c648a013SAlexander Aring {
1104c648a013SAlexander Aring 	return *(__le64 *) nla_data(nla);
1105c648a013SAlexander Aring }
1106c648a013SAlexander Aring 
1107c648a013SAlexander Aring /**
11084778e0beSJiri Pirko  * nla_get_s32 - return payload of s32 attribute
11094778e0beSJiri Pirko  * @nla: s32 netlink attribute
11104778e0beSJiri Pirko  */
11114778e0beSJiri Pirko static inline s32 nla_get_s32(const struct nlattr *nla)
11124778e0beSJiri Pirko {
11134778e0beSJiri Pirko 	return *(s32 *) nla_data(nla);
11144778e0beSJiri Pirko }
11154778e0beSJiri Pirko 
11164778e0beSJiri Pirko /**
11174778e0beSJiri Pirko  * nla_get_s16 - return payload of s16 attribute
11184778e0beSJiri Pirko  * @nla: s16 netlink attribute
11194778e0beSJiri Pirko  */
11204778e0beSJiri Pirko static inline s16 nla_get_s16(const struct nlattr *nla)
11214778e0beSJiri Pirko {
11224778e0beSJiri Pirko 	return *(s16 *) nla_data(nla);
11234778e0beSJiri Pirko }
11244778e0beSJiri Pirko 
11254778e0beSJiri Pirko /**
11264778e0beSJiri Pirko  * nla_get_s8 - return payload of s8 attribute
11274778e0beSJiri Pirko  * @nla: s8 netlink attribute
11284778e0beSJiri Pirko  */
11294778e0beSJiri Pirko static inline s8 nla_get_s8(const struct nlattr *nla)
11304778e0beSJiri Pirko {
11314778e0beSJiri Pirko 	return *(s8 *) nla_data(nla);
11324778e0beSJiri Pirko }
11334778e0beSJiri Pirko 
11344778e0beSJiri Pirko /**
11354778e0beSJiri Pirko  * nla_get_s64 - return payload of s64 attribute
11364778e0beSJiri Pirko  * @nla: s64 netlink attribute
11374778e0beSJiri Pirko  */
11384778e0beSJiri Pirko static inline s64 nla_get_s64(const struct nlattr *nla)
11394778e0beSJiri Pirko {
11404778e0beSJiri Pirko 	s64 tmp;
11414778e0beSJiri Pirko 
11424778e0beSJiri Pirko 	nla_memcpy(&tmp, nla, sizeof(tmp));
11434778e0beSJiri Pirko 
11444778e0beSJiri Pirko 	return tmp;
11454778e0beSJiri Pirko }
11464778e0beSJiri Pirko 
11474778e0beSJiri Pirko /**
1148bfa83a9eSThomas Graf  * nla_get_flag - return payload of flag attribute
1149bfa83a9eSThomas Graf  * @nla: flag netlink attribute
1150bfa83a9eSThomas Graf  */
1151b057efd4SPatrick McHardy static inline int nla_get_flag(const struct nlattr *nla)
1152bfa83a9eSThomas Graf {
1153bfa83a9eSThomas Graf 	return !!nla;
1154bfa83a9eSThomas Graf }
1155bfa83a9eSThomas Graf 
1156bfa83a9eSThomas Graf /**
1157bfa83a9eSThomas Graf  * nla_get_msecs - return payload of msecs attribute
1158bfa83a9eSThomas Graf  * @nla: msecs netlink attribute
1159bfa83a9eSThomas Graf  *
1160bfa83a9eSThomas Graf  * Returns the number of milliseconds in jiffies.
1161bfa83a9eSThomas Graf  */
1162b057efd4SPatrick McHardy static inline unsigned long nla_get_msecs(const struct nlattr *nla)
1163bfa83a9eSThomas Graf {
1164bfa83a9eSThomas Graf 	u64 msecs = nla_get_u64(nla);
1165bfa83a9eSThomas Graf 
1166bfa83a9eSThomas Graf 	return msecs_to_jiffies((unsigned long) msecs);
1167bfa83a9eSThomas Graf }
1168bfa83a9eSThomas Graf 
1169bfa83a9eSThomas Graf /**
117067b61f6cSJiri Benc  * nla_get_in_addr - return payload of IPv4 address attribute
117167b61f6cSJiri Benc  * @nla: IPv4 address netlink attribute
117267b61f6cSJiri Benc  */
117367b61f6cSJiri Benc static inline __be32 nla_get_in_addr(const struct nlattr *nla)
117467b61f6cSJiri Benc {
117567b61f6cSJiri Benc 	return *(__be32 *) nla_data(nla);
117667b61f6cSJiri Benc }
117767b61f6cSJiri Benc 
117867b61f6cSJiri Benc /**
117967b61f6cSJiri Benc  * nla_get_in6_addr - return payload of IPv6 address attribute
118067b61f6cSJiri Benc  * @nla: IPv6 address netlink attribute
118167b61f6cSJiri Benc  */
118267b61f6cSJiri Benc static inline struct in6_addr nla_get_in6_addr(const struct nlattr *nla)
118367b61f6cSJiri Benc {
118467b61f6cSJiri Benc 	struct in6_addr tmp;
118567b61f6cSJiri Benc 
118667b61f6cSJiri Benc 	nla_memcpy(&tmp, nla, sizeof(tmp));
118767b61f6cSJiri Benc 	return tmp;
118867b61f6cSJiri Benc }
118967b61f6cSJiri Benc 
119067b61f6cSJiri Benc /**
1191bfa83a9eSThomas Graf  * nla_nest_start - Start a new level of nested attributes
1192bfa83a9eSThomas Graf  * @skb: socket buffer to add attributes to
1193bfa83a9eSThomas Graf  * @attrtype: attribute type of container
1194bfa83a9eSThomas Graf  *
1195bfa83a9eSThomas Graf  * Returns the container attribute
1196bfa83a9eSThomas Graf  */
1197bfa83a9eSThomas Graf static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
1198bfa83a9eSThomas Graf {
119927a884dcSArnaldo Carvalho de Melo 	struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
1200bfa83a9eSThomas Graf 
1201bfa83a9eSThomas Graf 	if (nla_put(skb, attrtype, 0, NULL) < 0)
1202bfa83a9eSThomas Graf 		return NULL;
1203bfa83a9eSThomas Graf 
1204bfa83a9eSThomas Graf 	return start;
1205bfa83a9eSThomas Graf }
1206bfa83a9eSThomas Graf 
1207bfa83a9eSThomas Graf /**
1208bfa83a9eSThomas Graf  * nla_nest_end - Finalize nesting of attributes
1209d1ec3b77SPierre Ynard  * @skb: socket buffer the attributes are stored in
1210bfa83a9eSThomas Graf  * @start: container attribute
1211bfa83a9eSThomas Graf  *
1212bfa83a9eSThomas Graf  * Corrects the container attribute header to include the all
1213bfa83a9eSThomas Graf  * appeneded attributes.
1214bfa83a9eSThomas Graf  *
1215bfa83a9eSThomas Graf  * Returns the total data length of the skb.
1216bfa83a9eSThomas Graf  */
1217bfa83a9eSThomas Graf static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
1218bfa83a9eSThomas Graf {
121927a884dcSArnaldo Carvalho de Melo 	start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
1220bfa83a9eSThomas Graf 	return skb->len;
1221bfa83a9eSThomas Graf }
1222bfa83a9eSThomas Graf 
1223bfa83a9eSThomas Graf /**
1224bfa83a9eSThomas Graf  * nla_nest_cancel - Cancel nesting of attributes
1225bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
1226bfa83a9eSThomas Graf  * @start: container attribute
1227bfa83a9eSThomas Graf  *
1228bfa83a9eSThomas Graf  * Removes the container attribute and including all nested
1229bc3ed28cSThomas Graf  * attributes. Returns -EMSGSIZE
1230bfa83a9eSThomas Graf  */
1231bc3ed28cSThomas Graf static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
1232bfa83a9eSThomas Graf {
1233bc3ed28cSThomas Graf 	nlmsg_trim(skb, start);
1234bfa83a9eSThomas Graf }
1235bfa83a9eSThomas Graf 
1236bfa83a9eSThomas Graf /**
12374fe5d5c0SPaul Moore  * nla_validate_nested - Validate a stream of nested attributes
12384fe5d5c0SPaul Moore  * @start: container attribute
12394fe5d5c0SPaul Moore  * @maxtype: maximum attribute type to be expected
12404fe5d5c0SPaul Moore  * @policy: validation policy
12414fe5d5c0SPaul Moore  *
12424fe5d5c0SPaul Moore  * Validates all attributes in the nested attribute stream against the
12434fe5d5c0SPaul Moore  * specified policy. Attributes with a type exceeding maxtype will be
12444fe5d5c0SPaul Moore  * ignored. See documenation of struct nla_policy for more details.
12454fe5d5c0SPaul Moore  *
12464fe5d5c0SPaul Moore  * Returns 0 on success or a negative error code.
12474fe5d5c0SPaul Moore  */
12483654654fSJan Engelhardt static inline int nla_validate_nested(const struct nlattr *start, int maxtype,
1249ef7c79edSPatrick McHardy 				      const struct nla_policy *policy)
12504fe5d5c0SPaul Moore {
12514fe5d5c0SPaul Moore 	return nla_validate(nla_data(start), nla_len(start), maxtype, policy);
12524fe5d5c0SPaul Moore }
12534fe5d5c0SPaul Moore 
12544fe5d5c0SPaul Moore /**
1255089bf1a6SNicolas Dichtel  * nla_need_padding_for_64bit - test 64-bit alignment of the next attribute
1256089bf1a6SNicolas Dichtel  * @skb: socket buffer the message is stored in
1257089bf1a6SNicolas Dichtel  *
1258089bf1a6SNicolas Dichtel  * Return true if padding is needed to align the next attribute (nla_data()) to
1259089bf1a6SNicolas Dichtel  * a 64-bit aligned area.
1260089bf1a6SNicolas Dichtel  */
1261089bf1a6SNicolas Dichtel static inline bool nla_need_padding_for_64bit(struct sk_buff *skb)
1262089bf1a6SNicolas Dichtel {
1263089bf1a6SNicolas Dichtel #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1264089bf1a6SNicolas Dichtel 	/* The nlattr header is 4 bytes in size, that's why we test
1265089bf1a6SNicolas Dichtel 	 * if the skb->data _is_ aligned.  A NOP attribute, plus
1266089bf1a6SNicolas Dichtel 	 * nlattr header for next attribute, will make nla_data()
1267089bf1a6SNicolas Dichtel 	 * 8-byte aligned.
1268089bf1a6SNicolas Dichtel 	 */
1269089bf1a6SNicolas Dichtel 	if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
1270089bf1a6SNicolas Dichtel 		return true;
1271089bf1a6SNicolas Dichtel #endif
1272089bf1a6SNicolas Dichtel 	return false;
1273089bf1a6SNicolas Dichtel }
1274089bf1a6SNicolas Dichtel 
1275089bf1a6SNicolas Dichtel /**
127635c58459SDavid S. Miller  * nla_align_64bit - 64-bit align the nla_data() of next attribute
127735c58459SDavid S. Miller  * @skb: socket buffer the message is stored in
127835c58459SDavid S. Miller  * @padattr: attribute type for the padding
127935c58459SDavid S. Miller  *
128035c58459SDavid S. Miller  * Conditionally emit a padding netlink attribute in order to make
128135c58459SDavid S. Miller  * the next attribute we emit have a 64-bit aligned nla_data() area.
128235c58459SDavid S. Miller  * This will only be done in architectures which do not have
1283cca1d815SEric Dumazet  * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS defined.
128435c58459SDavid S. Miller  *
128535c58459SDavid S. Miller  * Returns zero on success or a negative error code.
128635c58459SDavid S. Miller  */
128735c58459SDavid S. Miller static inline int nla_align_64bit(struct sk_buff *skb, int padattr)
128835c58459SDavid S. Miller {
1289089bf1a6SNicolas Dichtel 	if (nla_need_padding_for_64bit(skb) &&
1290cca1d815SEric Dumazet 	    !nla_reserve(skb, padattr, 0))
129135c58459SDavid S. Miller 		return -EMSGSIZE;
1292089bf1a6SNicolas Dichtel 
129335c58459SDavid S. Miller 	return 0;
129435c58459SDavid S. Miller }
129535c58459SDavid S. Miller 
129635c58459SDavid S. Miller /**
129735c58459SDavid S. Miller  * nla_total_size_64bit - total length of attribute including padding
129835c58459SDavid S. Miller  * @payload: length of payload
129935c58459SDavid S. Miller  */
130035c58459SDavid S. Miller static inline int nla_total_size_64bit(int payload)
130135c58459SDavid S. Miller {
130235c58459SDavid S. Miller 	return NLA_ALIGN(nla_attr_size(payload))
1303cca1d815SEric Dumazet #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
130435c58459SDavid S. Miller 		+ NLA_ALIGN(nla_attr_size(0))
130535c58459SDavid S. Miller #endif
130635c58459SDavid S. Miller 		;
130735c58459SDavid S. Miller }
130835c58459SDavid S. Miller 
130935c58459SDavid S. Miller /**
1310bfa83a9eSThomas Graf  * nla_for_each_attr - iterate over a stream of attributes
1311bfa83a9eSThomas Graf  * @pos: loop counter, set to current attribute
1312bfa83a9eSThomas Graf  * @head: head of attribute stream
1313bfa83a9eSThomas Graf  * @len: length of attribute stream
1314bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
1315bfa83a9eSThomas Graf  */
1316bfa83a9eSThomas Graf #define nla_for_each_attr(pos, head, len, rem) \
1317bfa83a9eSThomas Graf 	for (pos = head, rem = len; \
1318bfa83a9eSThomas Graf 	     nla_ok(pos, rem); \
1319bfa83a9eSThomas Graf 	     pos = nla_next(pos, &(rem)))
1320bfa83a9eSThomas Graf 
1321fe4944e5SThomas Graf /**
1322fe4944e5SThomas Graf  * nla_for_each_nested - iterate over nested attributes
1323fe4944e5SThomas Graf  * @pos: loop counter, set to current attribute
1324fe4944e5SThomas Graf  * @nla: attribute containing the nested attributes
1325fe4944e5SThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
1326fe4944e5SThomas Graf  */
1327fe4944e5SThomas Graf #define nla_for_each_nested(pos, nla, rem) \
1328fe4944e5SThomas Graf 	nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
1329fe4944e5SThomas Graf 
1330941d8ebcSSimon Horman /**
1331941d8ebcSSimon Horman  * nla_is_last - Test if attribute is last in stream
1332941d8ebcSSimon Horman  * @nla: attribute to test
1333941d8ebcSSimon Horman  * @rem: bytes remaining in stream
1334941d8ebcSSimon Horman  */
1335941d8ebcSSimon Horman static inline bool nla_is_last(const struct nlattr *nla, int rem)
1336941d8ebcSSimon Horman {
1337941d8ebcSSimon Horman 	return nla->nla_len == rem;
1338941d8ebcSSimon Horman }
1339941d8ebcSSimon Horman 
1340bfa83a9eSThomas Graf #endif
1341