xref: /openbmc/linux/include/net/netlink.h (revision 70f23fd6)
1bfa83a9eSThomas Graf #ifndef __NET_NETLINK_H
2bfa83a9eSThomas Graf #define __NET_NETLINK_H
3bfa83a9eSThomas Graf 
4bfa83a9eSThomas Graf #include <linux/types.h>
5bfa83a9eSThomas Graf #include <linux/netlink.h>
6d7fe0f24SAl Viro #include <linux/jiffies.h>
7bfa83a9eSThomas Graf 
8bfa83a9eSThomas Graf /* ========================================================================
9bfa83a9eSThomas Graf  *         Netlink Messages and Attributes Interface (As Seen On TV)
10bfa83a9eSThomas Graf  * ------------------------------------------------------------------------
11bfa83a9eSThomas Graf  *                          Messages Interface
12bfa83a9eSThomas Graf  * ------------------------------------------------------------------------
13bfa83a9eSThomas Graf  *
14bfa83a9eSThomas Graf  * Message Format:
15bfa83a9eSThomas Graf  *    <--- nlmsg_total_size(payload)  --->
16bfa83a9eSThomas Graf  *    <-- nlmsg_msg_size(payload) ->
17bfa83a9eSThomas Graf  *   +----------+- - -+-------------+- - -+-------- - -
18bfa83a9eSThomas Graf  *   | nlmsghdr | Pad |   Payload   | Pad | nlmsghdr
19bfa83a9eSThomas Graf  *   +----------+- - -+-------------+- - -+-------- - -
20bfa83a9eSThomas Graf  *   nlmsg_data(nlh)---^                   ^
21bfa83a9eSThomas Graf  *   nlmsg_next(nlh)-----------------------+
22bfa83a9eSThomas Graf  *
23bfa83a9eSThomas Graf  * Payload Format:
24bfa83a9eSThomas Graf  *    <---------------------- nlmsg_len(nlh) --------------------->
25bfa83a9eSThomas Graf  *    <------ hdrlen ------>       <- nlmsg_attrlen(nlh, hdrlen) ->
26bfa83a9eSThomas Graf  *   +----------------------+- - -+--------------------------------+
27bfa83a9eSThomas Graf  *   |     Family Header    | Pad |           Attributes           |
28bfa83a9eSThomas Graf  *   +----------------------+- - -+--------------------------------+
29bfa83a9eSThomas Graf  *   nlmsg_attrdata(nlh, hdrlen)---^
30bfa83a9eSThomas Graf  *
31bfa83a9eSThomas Graf  * Data Structures:
32bfa83a9eSThomas Graf  *   struct nlmsghdr			netlink message header
33bfa83a9eSThomas Graf  *
34bfa83a9eSThomas Graf  * Message Construction:
35bfa83a9eSThomas Graf  *   nlmsg_new()			create a new netlink message
36bfa83a9eSThomas Graf  *   nlmsg_put()			add a netlink message to an skb
37bfa83a9eSThomas Graf  *   nlmsg_put_answer()			callback based nlmsg_put()
381dc8d8c0SJustin P. Mattock  *   nlmsg_end()			finalize netlink message
39fe4944e5SThomas Graf  *   nlmsg_get_pos()			return current position in message
40fe4944e5SThomas Graf  *   nlmsg_trim()			trim part of message
41bfa83a9eSThomas Graf  *   nlmsg_cancel()			cancel message construction
42bfa83a9eSThomas Graf  *   nlmsg_free()			free a netlink message
43bfa83a9eSThomas Graf  *
44bfa83a9eSThomas Graf  * Message Sending:
45bfa83a9eSThomas Graf  *   nlmsg_multicast()			multicast message to several groups
46bfa83a9eSThomas Graf  *   nlmsg_unicast()			unicast a message to a single socket
47d387f6adSThomas Graf  *   nlmsg_notify()			send notification message
48bfa83a9eSThomas Graf  *
49bfa83a9eSThomas Graf  * Message Length Calculations:
50bfa83a9eSThomas Graf  *   nlmsg_msg_size(payload)		length of message w/o padding
51bfa83a9eSThomas Graf  *   nlmsg_total_size(payload)		length of message w/ padding
52bfa83a9eSThomas Graf  *   nlmsg_padlen(payload)		length of padding at tail
53bfa83a9eSThomas Graf  *
54bfa83a9eSThomas Graf  * Message Payload Access:
55bfa83a9eSThomas Graf  *   nlmsg_data(nlh)			head of message payload
56bfa83a9eSThomas Graf  *   nlmsg_len(nlh)			length of message payload
57bfa83a9eSThomas Graf  *   nlmsg_attrdata(nlh, hdrlen)	head of attributes data
58bfa83a9eSThomas Graf  *   nlmsg_attrlen(nlh, hdrlen)		length of attributes data
59bfa83a9eSThomas Graf  *
60bfa83a9eSThomas Graf  * Message Parsing:
61bfa83a9eSThomas Graf  *   nlmsg_ok(nlh, remaining)		does nlh fit into remaining bytes?
62bfa83a9eSThomas Graf  *   nlmsg_next(nlh, remaining)		get next netlink message
63bfa83a9eSThomas Graf  *   nlmsg_parse()			parse attributes of a message
64bfa83a9eSThomas Graf  *   nlmsg_find_attr()			find an attribute in a message
65bfa83a9eSThomas Graf  *   nlmsg_for_each_msg()		loop over all messages
66bfa83a9eSThomas Graf  *   nlmsg_validate()			validate netlink message incl. attrs
67bfa83a9eSThomas Graf  *   nlmsg_for_each_attr()		loop over all attributes
68bfa83a9eSThomas Graf  *
6997676b6bSThomas Graf  * Misc:
7097676b6bSThomas Graf  *   nlmsg_report()			report back to application?
7197676b6bSThomas Graf  *
72bfa83a9eSThomas Graf  * ------------------------------------------------------------------------
73bfa83a9eSThomas Graf  *                          Attributes Interface
74bfa83a9eSThomas Graf  * ------------------------------------------------------------------------
75bfa83a9eSThomas Graf  *
76bfa83a9eSThomas Graf  * Attribute Format:
77bfa83a9eSThomas Graf  *    <------- nla_total_size(payload) ------->
78bfa83a9eSThomas Graf  *    <---- nla_attr_size(payload) ----->
79bfa83a9eSThomas Graf  *   +----------+- - -+- - - - - - - - - +- - -+-------- - -
80bfa83a9eSThomas Graf  *   |  Header  | Pad |     Payload      | Pad |  Header
81bfa83a9eSThomas Graf  *   +----------+- - -+- - - - - - - - - +- - -+-------- - -
82bfa83a9eSThomas Graf  *                     <- nla_len(nla) ->      ^
83bfa83a9eSThomas Graf  *   nla_data(nla)----^                        |
84bfa83a9eSThomas Graf  *   nla_next(nla)-----------------------------'
85bfa83a9eSThomas Graf  *
86bfa83a9eSThomas Graf  * Data Structures:
87d1ec3b77SPierre Ynard  *   struct nlattr			netlink attribute header
88bfa83a9eSThomas Graf  *
89bfa83a9eSThomas Graf  * Attribute Construction:
90fe4944e5SThomas Graf  *   nla_reserve(skb, type, len)	reserve room for an attribute
91fe4944e5SThomas Graf  *   nla_reserve_nohdr(skb, len)	reserve room for an attribute w/o hdr
92bfa83a9eSThomas Graf  *   nla_put(skb, type, len, data)	add attribute to skb
93fe4944e5SThomas Graf  *   nla_put_nohdr(skb, len, data)	add attribute w/o hdr
9401480e1cSPatrick McHardy  *   nla_append(skb, len, data)		append data to skb
95bfa83a9eSThomas Graf  *
96bfa83a9eSThomas Graf  * Attribute Construction for Basic Types:
97bfa83a9eSThomas Graf  *   nla_put_u8(skb, type, value)	add u8 attribute to skb
98bfa83a9eSThomas Graf  *   nla_put_u16(skb, type, value)	add u16 attribute to skb
99bfa83a9eSThomas Graf  *   nla_put_u32(skb, type, value)	add u32 attribute to skb
100bfa83a9eSThomas Graf  *   nla_put_u64(skb, type, value)	add u64 attribute to skb
101bfa83a9eSThomas Graf  *   nla_put_string(skb, type, str)	add string attribute to skb
102bfa83a9eSThomas Graf  *   nla_put_flag(skb, type)		add flag attribute to skb
103bfa83a9eSThomas Graf  *   nla_put_msecs(skb, type, jiffies)	add msecs attribute to skb
104bfa83a9eSThomas Graf  *
105bfa83a9eSThomas Graf  * Exceptions Based Attribute Construction:
106bfa83a9eSThomas Graf  *   NLA_PUT(skb, type, len, data)	add attribute to skb
107bfa83a9eSThomas Graf  *   NLA_PUT_U8(skb, type, value)	add u8 attribute to skb
108bfa83a9eSThomas Graf  *   NLA_PUT_U16(skb, type, value)	add u16 attribute to skb
109bfa83a9eSThomas Graf  *   NLA_PUT_U32(skb, type, value)	add u32 attribute to skb
110bfa83a9eSThomas Graf  *   NLA_PUT_U64(skb, type, value)	add u64 attribute to skb
111bfa83a9eSThomas Graf  *   NLA_PUT_STRING(skb, type, str)	add string attribute to skb
112bfa83a9eSThomas Graf  *   NLA_PUT_FLAG(skb, type)		add flag attribute to skb
113bfa83a9eSThomas Graf  *   NLA_PUT_MSECS(skb, type, jiffies)	add msecs attribute to skb
114bfa83a9eSThomas Graf  *
115bfa83a9eSThomas Graf  *   The meaning of these functions is equal to their lower case
116bfa83a9eSThomas Graf  *   variants but they jump to the label nla_put_failure in case
117bfa83a9eSThomas Graf  *   of a failure.
118bfa83a9eSThomas Graf  *
119bfa83a9eSThomas Graf  * Nested Attributes Construction:
120bfa83a9eSThomas Graf  *   nla_nest_start(skb, type)		start a nested attribute
121bfa83a9eSThomas Graf  *   nla_nest_end(skb, nla)		finalize a nested attribute
122bfa83a9eSThomas Graf  *   nla_nest_cancel(skb, nla)		cancel nested attribute construction
123bfa83a9eSThomas Graf  *
124bfa83a9eSThomas Graf  * Attribute Length Calculations:
125bfa83a9eSThomas Graf  *   nla_attr_size(payload)		length of attribute w/o padding
126bfa83a9eSThomas Graf  *   nla_total_size(payload)		length of attribute w/ padding
127bfa83a9eSThomas Graf  *   nla_padlen(payload)		length of padding
128bfa83a9eSThomas Graf  *
129bfa83a9eSThomas Graf  * Attribute Payload Access:
130bfa83a9eSThomas Graf  *   nla_data(nla)			head of attribute payload
131bfa83a9eSThomas Graf  *   nla_len(nla)			length of attribute payload
132bfa83a9eSThomas Graf  *
133bfa83a9eSThomas Graf  * Attribute Payload Access for Basic Types:
134bfa83a9eSThomas Graf  *   nla_get_u8(nla)			get payload for a u8 attribute
135bfa83a9eSThomas Graf  *   nla_get_u16(nla)			get payload for a u16 attribute
136bfa83a9eSThomas Graf  *   nla_get_u32(nla)			get payload for a u32 attribute
137bfa83a9eSThomas Graf  *   nla_get_u64(nla)			get payload for a u64 attribute
138bfa83a9eSThomas Graf  *   nla_get_flag(nla)			return 1 if flag is true
139bfa83a9eSThomas Graf  *   nla_get_msecs(nla)			get payload for a msecs attribute
140bfa83a9eSThomas Graf  *
141bfa83a9eSThomas Graf  * Attribute Misc:
142bfa83a9eSThomas Graf  *   nla_memcpy(dest, nla, count)	copy attribute into memory
143bfa83a9eSThomas Graf  *   nla_memcmp(nla, data, size)	compare attribute with memory area
144bfa83a9eSThomas Graf  *   nla_strlcpy(dst, nla, size)	copy attribute to a sized string
145bfa83a9eSThomas Graf  *   nla_strcmp(nla, str)		compare attribute with string
146bfa83a9eSThomas Graf  *
147bfa83a9eSThomas Graf  * Attribute Parsing:
148bfa83a9eSThomas Graf  *   nla_ok(nla, remaining)		does nla fit into remaining bytes?
149bfa83a9eSThomas Graf  *   nla_next(nla, remaining)		get next netlink attribute
150bfa83a9eSThomas Graf  *   nla_validate()			validate a stream of attributes
1514fe5d5c0SPaul Moore  *   nla_validate_nested()		validate a stream of nested attributes
152bfa83a9eSThomas Graf  *   nla_find()				find attribute in stream of attributes
153fe4944e5SThomas Graf  *   nla_find_nested()			find attribute in nested attributes
154bfa83a9eSThomas Graf  *   nla_parse()			parse and validate stream of attrs
155bfa83a9eSThomas Graf  *   nla_parse_nested()			parse nested attribuets
156bfa83a9eSThomas Graf  *   nla_for_each_attr()		loop over all attributes
15722acb19aSPaul Moore  *   nla_for_each_nested()		loop over the nested attributes
158bfa83a9eSThomas Graf  *=========================================================================
159bfa83a9eSThomas Graf  */
160bfa83a9eSThomas Graf 
161bfa83a9eSThomas Graf  /**
162bfa83a9eSThomas Graf   * Standard attribute types to specify validation policy
163bfa83a9eSThomas Graf   */
164bfa83a9eSThomas Graf enum {
165bfa83a9eSThomas Graf 	NLA_UNSPEC,
166bfa83a9eSThomas Graf 	NLA_U8,
167bfa83a9eSThomas Graf 	NLA_U16,
168bfa83a9eSThomas Graf 	NLA_U32,
169bfa83a9eSThomas Graf 	NLA_U64,
170bfa83a9eSThomas Graf 	NLA_STRING,
171bfa83a9eSThomas Graf 	NLA_FLAG,
172bfa83a9eSThomas Graf 	NLA_MSECS,
173bfa83a9eSThomas Graf 	NLA_NESTED,
1741092cb21SPatrick McHardy 	NLA_NESTED_COMPAT,
175a5531a5dSThomas Graf 	NLA_NUL_STRING,
176d30045a0SJohannes Berg 	NLA_BINARY,
177bfa83a9eSThomas Graf 	__NLA_TYPE_MAX,
178bfa83a9eSThomas Graf };
179bfa83a9eSThomas Graf 
180bfa83a9eSThomas Graf #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
181bfa83a9eSThomas Graf 
182bfa83a9eSThomas Graf /**
183bfa83a9eSThomas Graf  * struct nla_policy - attribute validation policy
184bfa83a9eSThomas Graf  * @type: Type of attribute or NLA_UNSPEC
185a5531a5dSThomas Graf  * @len: Type specific length of payload
186bfa83a9eSThomas Graf  *
187bfa83a9eSThomas Graf  * Policies are defined as arrays of this struct, the array must be
188bfa83a9eSThomas Graf  * accessible by attribute type up to the highest identifier to be expected.
189bfa83a9eSThomas Graf  *
190a5531a5dSThomas Graf  * Meaning of `len' field:
191a5531a5dSThomas Graf  *    NLA_STRING           Maximum length of string
192a5531a5dSThomas Graf  *    NLA_NUL_STRING       Maximum length of string (excluding NUL)
193a5531a5dSThomas Graf  *    NLA_FLAG             Unused
194d30045a0SJohannes Berg  *    NLA_BINARY           Maximum length of attribute payload
1951092cb21SPatrick McHardy  *    NLA_NESTED_COMPAT    Exact length of structure payload
196a5531a5dSThomas Graf  *    All other            Exact length of attribute payload
197a5531a5dSThomas Graf  *
198bfa83a9eSThomas Graf  * Example:
199b54452b0SAlexey Dobriyan  * static const struct nla_policy my_policy[ATTR_MAX+1] = {
200bfa83a9eSThomas Graf  * 	[ATTR_FOO] = { .type = NLA_U16 },
201d30045a0SJohannes Berg  *	[ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
202a5531a5dSThomas Graf  *	[ATTR_BAZ] = { .len = sizeof(struct mystruct) },
203bfa83a9eSThomas Graf  * };
204bfa83a9eSThomas Graf  */
205bfa83a9eSThomas Graf struct nla_policy {
206bfa83a9eSThomas Graf 	u16		type;
207a5531a5dSThomas Graf 	u16		len;
208bfa83a9eSThomas Graf };
209bfa83a9eSThomas Graf 
2104e902c57SThomas Graf /**
2114e902c57SThomas Graf  * struct nl_info - netlink source information
2124e902c57SThomas Graf  * @nlh: Netlink message header of original request
2134e902c57SThomas Graf  * @pid: Netlink PID of requesting application
2144e902c57SThomas Graf  */
2154e902c57SThomas Graf struct nl_info {
2164e902c57SThomas Graf 	struct nlmsghdr		*nlh;
2174d1169c1SDenis V. Lunev 	struct net		*nl_net;
2184e902c57SThomas Graf 	u32			pid;
2194e902c57SThomas Graf };
2204e902c57SThomas Graf 
221cd40b7d3SDenis V. Lunev extern int		netlink_rcv_skb(struct sk_buff *skb,
22282ace47aSThomas Graf 					int (*cb)(struct sk_buff *,
2231d00a4ebSThomas Graf 						  struct nlmsghdr *));
22497676b6bSThomas Graf extern int		nlmsg_notify(struct sock *sk, struct sk_buff *skb,
22597676b6bSThomas Graf 				     u32 pid, unsigned int group, int report,
22697676b6bSThomas Graf 				     gfp_t flags);
22782ace47aSThomas Graf 
2283654654fSJan Engelhardt extern int		nla_validate(const struct nlattr *head,
2293654654fSJan Engelhardt 				     int len, int maxtype,
230ef7c79edSPatrick McHardy 				     const struct nla_policy *policy);
2313654654fSJan Engelhardt extern int		nla_parse(struct nlattr **tb, int maxtype,
2323654654fSJan Engelhardt 				  const struct nlattr *head, int len,
233ef7c79edSPatrick McHardy 				  const struct nla_policy *policy);
234e487eb99SHolger Eitzenberger extern int		nla_policy_len(const struct nla_policy *, int);
2353654654fSJan Engelhardt extern struct nlattr *	nla_find(const struct nlattr *head,
2363654654fSJan Engelhardt 				 int len, int attrtype);
237bfa83a9eSThomas Graf extern size_t		nla_strlcpy(char *dst, const struct nlattr *nla,
238bfa83a9eSThomas Graf 				    size_t dstsize);
239b057efd4SPatrick McHardy extern int		nla_memcpy(void *dest, const struct nlattr *src, int count);
240bfa83a9eSThomas Graf extern int		nla_memcmp(const struct nlattr *nla, const void *data,
241bfa83a9eSThomas Graf 				   size_t size);
242bfa83a9eSThomas Graf extern int		nla_strcmp(const struct nlattr *nla, const char *str);
243bfa83a9eSThomas Graf extern struct nlattr *	__nla_reserve(struct sk_buff *skb, int attrtype,
244bfa83a9eSThomas Graf 				      int attrlen);
245fe4944e5SThomas Graf extern void *		__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
246bfa83a9eSThomas Graf extern struct nlattr *	nla_reserve(struct sk_buff *skb, int attrtype,
247bfa83a9eSThomas Graf 				    int attrlen);
248fe4944e5SThomas Graf extern void *		nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
249bfa83a9eSThomas Graf extern void		__nla_put(struct sk_buff *skb, int attrtype,
250bfa83a9eSThomas Graf 				  int attrlen, const void *data);
251fe4944e5SThomas Graf extern void		__nla_put_nohdr(struct sk_buff *skb, int attrlen,
252fe4944e5SThomas Graf 					const void *data);
253bfa83a9eSThomas Graf extern int		nla_put(struct sk_buff *skb, int attrtype,
254bfa83a9eSThomas Graf 				int attrlen, const void *data);
255fe4944e5SThomas Graf extern int		nla_put_nohdr(struct sk_buff *skb, int attrlen,
256fe4944e5SThomas Graf 				      const void *data);
25701480e1cSPatrick McHardy extern int		nla_append(struct sk_buff *skb, int attrlen,
25801480e1cSPatrick McHardy 				   const void *data);
259bfa83a9eSThomas Graf 
260bfa83a9eSThomas Graf /**************************************************************************
261bfa83a9eSThomas Graf  * Netlink Messages
262bfa83a9eSThomas Graf  **************************************************************************/
263bfa83a9eSThomas Graf 
264bfa83a9eSThomas Graf /**
265bfa83a9eSThomas Graf  * nlmsg_msg_size - length of netlink message not including padding
266bfa83a9eSThomas Graf  * @payload: length of message payload
267bfa83a9eSThomas Graf  */
268bfa83a9eSThomas Graf static inline int nlmsg_msg_size(int payload)
269bfa83a9eSThomas Graf {
270bfa83a9eSThomas Graf 	return NLMSG_HDRLEN + payload;
271bfa83a9eSThomas Graf }
272bfa83a9eSThomas Graf 
273bfa83a9eSThomas Graf /**
274bfa83a9eSThomas Graf  * nlmsg_total_size - length of netlink message including padding
275bfa83a9eSThomas Graf  * @payload: length of message payload
276bfa83a9eSThomas Graf  */
277bfa83a9eSThomas Graf static inline int nlmsg_total_size(int payload)
278bfa83a9eSThomas Graf {
279bfa83a9eSThomas Graf 	return NLMSG_ALIGN(nlmsg_msg_size(payload));
280bfa83a9eSThomas Graf }
281bfa83a9eSThomas Graf 
282bfa83a9eSThomas Graf /**
283bfa83a9eSThomas Graf  * nlmsg_padlen - length of padding at the message's tail
284bfa83a9eSThomas Graf  * @payload: length of message payload
285bfa83a9eSThomas Graf  */
286bfa83a9eSThomas Graf static inline int nlmsg_padlen(int payload)
287bfa83a9eSThomas Graf {
288bfa83a9eSThomas Graf 	return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
289bfa83a9eSThomas Graf }
290bfa83a9eSThomas Graf 
291bfa83a9eSThomas Graf /**
292bfa83a9eSThomas Graf  * nlmsg_data - head of message payload
29370f23fd6SJustin P. Mattock  * @nlh: netlink message header
294bfa83a9eSThomas Graf  */
295bfa83a9eSThomas Graf static inline void *nlmsg_data(const struct nlmsghdr *nlh)
296bfa83a9eSThomas Graf {
297bfa83a9eSThomas Graf 	return (unsigned char *) nlh + NLMSG_HDRLEN;
298bfa83a9eSThomas Graf }
299bfa83a9eSThomas Graf 
300bfa83a9eSThomas Graf /**
301bfa83a9eSThomas Graf  * nlmsg_len - length of message payload
302bfa83a9eSThomas Graf  * @nlh: netlink message header
303bfa83a9eSThomas Graf  */
304bfa83a9eSThomas Graf static inline int nlmsg_len(const struct nlmsghdr *nlh)
305bfa83a9eSThomas Graf {
306bfa83a9eSThomas Graf 	return nlh->nlmsg_len - NLMSG_HDRLEN;
307bfa83a9eSThomas Graf }
308bfa83a9eSThomas Graf 
309bfa83a9eSThomas Graf /**
310bfa83a9eSThomas Graf  * nlmsg_attrdata - head of attributes data
311bfa83a9eSThomas Graf  * @nlh: netlink message header
312bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
313bfa83a9eSThomas Graf  */
314bfa83a9eSThomas Graf static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
315bfa83a9eSThomas Graf 					    int hdrlen)
316bfa83a9eSThomas Graf {
317bfa83a9eSThomas Graf 	unsigned char *data = nlmsg_data(nlh);
318bfa83a9eSThomas Graf 	return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
319bfa83a9eSThomas Graf }
320bfa83a9eSThomas Graf 
321bfa83a9eSThomas Graf /**
322bfa83a9eSThomas Graf  * nlmsg_attrlen - length of attributes data
323bfa83a9eSThomas Graf  * @nlh: netlink message header
324bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
325bfa83a9eSThomas Graf  */
326bfa83a9eSThomas Graf static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
327bfa83a9eSThomas Graf {
328bfa83a9eSThomas Graf 	return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
329bfa83a9eSThomas Graf }
330bfa83a9eSThomas Graf 
331bfa83a9eSThomas Graf /**
332bfa83a9eSThomas Graf  * nlmsg_ok - check if the netlink message fits into the remaining bytes
333bfa83a9eSThomas Graf  * @nlh: netlink message header
334bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in message stream
335bfa83a9eSThomas Graf  */
336bfa83a9eSThomas Graf static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
337bfa83a9eSThomas Graf {
338619e803dSVegard Nossum 	return (remaining >= (int) sizeof(struct nlmsghdr) &&
339bfa83a9eSThomas Graf 		nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
340bfa83a9eSThomas Graf 		nlh->nlmsg_len <= remaining);
341bfa83a9eSThomas Graf }
342bfa83a9eSThomas Graf 
343bfa83a9eSThomas Graf /**
344bfa83a9eSThomas Graf  * nlmsg_next - next netlink message in message stream
345bfa83a9eSThomas Graf  * @nlh: netlink message header
346bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in message stream
347bfa83a9eSThomas Graf  *
348bfa83a9eSThomas Graf  * Returns the next netlink message in the message stream and
349bfa83a9eSThomas Graf  * decrements remaining by the size of the current message.
350bfa83a9eSThomas Graf  */
3513654654fSJan Engelhardt static inline struct nlmsghdr *
3523654654fSJan Engelhardt nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
353bfa83a9eSThomas Graf {
354bfa83a9eSThomas Graf 	int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
355bfa83a9eSThomas Graf 
356bfa83a9eSThomas Graf 	*remaining -= totlen;
357bfa83a9eSThomas Graf 
358bfa83a9eSThomas Graf 	return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
359bfa83a9eSThomas Graf }
360bfa83a9eSThomas Graf 
361bfa83a9eSThomas Graf /**
362bfa83a9eSThomas Graf  * nlmsg_parse - parse attributes of a netlink message
363bfa83a9eSThomas Graf  * @nlh: netlink message header
364bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
365bfa83a9eSThomas Graf  * @tb: destination array with maxtype+1 elements
366bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
367bfa83a9eSThomas Graf  * @policy: validation policy
368bfa83a9eSThomas Graf  *
369bfa83a9eSThomas Graf  * See nla_parse()
370bfa83a9eSThomas Graf  */
3713a6c2b41SPatrick McHardy static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
372bfa83a9eSThomas Graf 			      struct nlattr *tb[], int maxtype,
373ef7c79edSPatrick McHardy 			      const struct nla_policy *policy)
374bfa83a9eSThomas Graf {
375bfa83a9eSThomas Graf 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
376bfa83a9eSThomas Graf 		return -EINVAL;
377bfa83a9eSThomas Graf 
378bfa83a9eSThomas Graf 	return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
379bfa83a9eSThomas Graf 			 nlmsg_attrlen(nlh, hdrlen), policy);
380bfa83a9eSThomas Graf }
381bfa83a9eSThomas Graf 
382bfa83a9eSThomas Graf /**
383bfa83a9eSThomas Graf  * nlmsg_find_attr - find a specific attribute in a netlink message
384bfa83a9eSThomas Graf  * @nlh: netlink message header
385bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
386bfa83a9eSThomas Graf  * @attrtype: type of attribute to look for
387bfa83a9eSThomas Graf  *
388bfa83a9eSThomas Graf  * Returns the first attribute which matches the specified type.
389bfa83a9eSThomas Graf  */
3906b8c92baSNelson Elhage static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
391bfa83a9eSThomas Graf 					     int hdrlen, int attrtype)
392bfa83a9eSThomas Graf {
393bfa83a9eSThomas Graf 	return nla_find(nlmsg_attrdata(nlh, hdrlen),
394bfa83a9eSThomas Graf 			nlmsg_attrlen(nlh, hdrlen), attrtype);
395bfa83a9eSThomas Graf }
396bfa83a9eSThomas Graf 
397bfa83a9eSThomas Graf /**
398bfa83a9eSThomas Graf  * nlmsg_validate - validate a netlink message including attributes
399bfa83a9eSThomas Graf  * @nlh: netlinket message header
400bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
401bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
402bfa83a9eSThomas Graf  * @policy: validation policy
403bfa83a9eSThomas Graf  */
4043654654fSJan Engelhardt static inline int nlmsg_validate(const struct nlmsghdr *nlh,
4053654654fSJan Engelhardt 				 int hdrlen, int maxtype,
406ef7c79edSPatrick McHardy 				 const struct nla_policy *policy)
407bfa83a9eSThomas Graf {
408bfa83a9eSThomas Graf 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
409bfa83a9eSThomas Graf 		return -EINVAL;
410bfa83a9eSThomas Graf 
411bfa83a9eSThomas Graf 	return nla_validate(nlmsg_attrdata(nlh, hdrlen),
412bfa83a9eSThomas Graf 			    nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
413bfa83a9eSThomas Graf }
414bfa83a9eSThomas Graf 
415bfa83a9eSThomas Graf /**
41697676b6bSThomas Graf  * nlmsg_report - need to report back to application?
41797676b6bSThomas Graf  * @nlh: netlink message header
41897676b6bSThomas Graf  *
41997676b6bSThomas Graf  * Returns 1 if a report back to the application is requested.
42097676b6bSThomas Graf  */
4213a6c2b41SPatrick McHardy static inline int nlmsg_report(const struct nlmsghdr *nlh)
42297676b6bSThomas Graf {
42397676b6bSThomas Graf 	return !!(nlh->nlmsg_flags & NLM_F_ECHO);
42497676b6bSThomas Graf }
42597676b6bSThomas Graf 
42697676b6bSThomas Graf /**
427bfa83a9eSThomas Graf  * nlmsg_for_each_attr - iterate over a stream of attributes
428bfa83a9eSThomas Graf  * @pos: loop counter, set to current attribute
429bfa83a9eSThomas Graf  * @nlh: netlink message header
430bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
431bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
432bfa83a9eSThomas Graf  */
433bfa83a9eSThomas Graf #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
434bfa83a9eSThomas Graf 	nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
435bfa83a9eSThomas Graf 			  nlmsg_attrlen(nlh, hdrlen), rem)
436bfa83a9eSThomas Graf 
437bfa83a9eSThomas Graf #if 0
438bfa83a9eSThomas Graf /* FIXME: Enable once all users have been converted */
439bfa83a9eSThomas Graf 
440bfa83a9eSThomas Graf /**
441bfa83a9eSThomas Graf  * __nlmsg_put - Add a new netlink message to an skb
442bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
443bfa83a9eSThomas Graf  * @pid: netlink process id
444bfa83a9eSThomas Graf  * @seq: sequence number of message
445bfa83a9eSThomas Graf  * @type: message type
446bfa83a9eSThomas Graf  * @payload: length of message payload
447bfa83a9eSThomas Graf  * @flags: message flags
448bfa83a9eSThomas Graf  *
449bfa83a9eSThomas Graf  * The caller is responsible to ensure that the skb provides enough
450bfa83a9eSThomas Graf  * tailroom for both the netlink header and payload.
451bfa83a9eSThomas Graf  */
452bfa83a9eSThomas Graf static inline struct nlmsghdr *__nlmsg_put(struct sk_buff *skb, u32 pid,
453bfa83a9eSThomas Graf 					   u32 seq, int type, int payload,
454bfa83a9eSThomas Graf 					   int flags)
455bfa83a9eSThomas Graf {
456bfa83a9eSThomas Graf 	struct nlmsghdr *nlh;
457bfa83a9eSThomas Graf 
458bfa83a9eSThomas Graf 	nlh = (struct nlmsghdr *) skb_put(skb, nlmsg_total_size(payload));
459bfa83a9eSThomas Graf 	nlh->nlmsg_type = type;
460bfa83a9eSThomas Graf 	nlh->nlmsg_len = nlmsg_msg_size(payload);
461bfa83a9eSThomas Graf 	nlh->nlmsg_flags = flags;
462bfa83a9eSThomas Graf 	nlh->nlmsg_pid = pid;
463bfa83a9eSThomas Graf 	nlh->nlmsg_seq = seq;
464bfa83a9eSThomas Graf 
465bfa83a9eSThomas Graf 	memset((unsigned char *) nlmsg_data(nlh) + payload, 0,
466bfa83a9eSThomas Graf 	       nlmsg_padlen(payload));
467bfa83a9eSThomas Graf 
468bfa83a9eSThomas Graf 	return nlh;
469bfa83a9eSThomas Graf }
470bfa83a9eSThomas Graf #endif
471bfa83a9eSThomas Graf 
472bfa83a9eSThomas Graf /**
473bfa83a9eSThomas Graf  * nlmsg_put - Add a new netlink message to an skb
474bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
475bfa83a9eSThomas Graf  * @pid: netlink process id
476bfa83a9eSThomas Graf  * @seq: sequence number of message
477bfa83a9eSThomas Graf  * @type: message type
478bfa83a9eSThomas Graf  * @payload: length of message payload
479bfa83a9eSThomas Graf  * @flags: message flags
480bfa83a9eSThomas Graf  *
481bfa83a9eSThomas Graf  * Returns NULL if the tailroom of the skb is insufficient to store
482bfa83a9eSThomas Graf  * the message header and payload.
483bfa83a9eSThomas Graf  */
484bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
485bfa83a9eSThomas Graf 					 int type, int payload, int flags)
486bfa83a9eSThomas Graf {
487bfa83a9eSThomas Graf 	if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
488bfa83a9eSThomas Graf 		return NULL;
489bfa83a9eSThomas Graf 
490bfa83a9eSThomas Graf 	return __nlmsg_put(skb, pid, seq, type, payload, flags);
491bfa83a9eSThomas Graf }
492bfa83a9eSThomas Graf 
493bfa83a9eSThomas Graf /**
494bfa83a9eSThomas Graf  * nlmsg_put_answer - Add a new callback based netlink message to an skb
495bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
496bfa83a9eSThomas Graf  * @cb: netlink callback
497bfa83a9eSThomas Graf  * @type: message type
498bfa83a9eSThomas Graf  * @payload: length of message payload
499bfa83a9eSThomas Graf  * @flags: message flags
500bfa83a9eSThomas Graf  *
501bfa83a9eSThomas Graf  * Returns NULL if the tailroom of the skb is insufficient to store
502bfa83a9eSThomas Graf  * the message header and payload.
503bfa83a9eSThomas Graf  */
504bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
505bfa83a9eSThomas Graf 						struct netlink_callback *cb,
506bfa83a9eSThomas Graf 						int type, int payload,
507bfa83a9eSThomas Graf 						int flags)
508bfa83a9eSThomas Graf {
509bfa83a9eSThomas Graf 	return nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
510bfa83a9eSThomas Graf 			 type, payload, flags);
511bfa83a9eSThomas Graf }
512bfa83a9eSThomas Graf 
513bfa83a9eSThomas Graf /**
514bfa83a9eSThomas Graf  * nlmsg_new - Allocate a new netlink message
515339bf98fSThomas Graf  * @payload: size of the message payload
516fe4944e5SThomas Graf  * @flags: the type of memory to allocate.
517bfa83a9eSThomas Graf  *
518339bf98fSThomas Graf  * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
519339bf98fSThomas Graf  * and a good default is needed.
520bfa83a9eSThomas Graf  */
521339bf98fSThomas Graf static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
522bfa83a9eSThomas Graf {
523339bf98fSThomas Graf 	return alloc_skb(nlmsg_total_size(payload), flags);
524bfa83a9eSThomas Graf }
525bfa83a9eSThomas Graf 
526bfa83a9eSThomas Graf /**
527bfa83a9eSThomas Graf  * nlmsg_end - Finalize a netlink message
528bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
529bfa83a9eSThomas Graf  * @nlh: netlink message header
530bfa83a9eSThomas Graf  *
531bfa83a9eSThomas Graf  * Corrects the netlink message header to include the appeneded
532bfa83a9eSThomas Graf  * attributes. Only necessary if attributes have been added to
533bfa83a9eSThomas Graf  * the message.
534bfa83a9eSThomas Graf  *
535bfa83a9eSThomas Graf  * Returns the total data length of the skb.
536bfa83a9eSThomas Graf  */
537bfa83a9eSThomas Graf static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
538bfa83a9eSThomas Graf {
53927a884dcSArnaldo Carvalho de Melo 	nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
540bfa83a9eSThomas Graf 
541bfa83a9eSThomas Graf 	return skb->len;
542bfa83a9eSThomas Graf }
543bfa83a9eSThomas Graf 
544bfa83a9eSThomas Graf /**
545fe4944e5SThomas Graf  * nlmsg_get_pos - return current position in netlink message
546fe4944e5SThomas Graf  * @skb: socket buffer the message is stored in
547fe4944e5SThomas Graf  *
548fe4944e5SThomas Graf  * Returns a pointer to the current tail of the message.
549fe4944e5SThomas Graf  */
550fe4944e5SThomas Graf static inline void *nlmsg_get_pos(struct sk_buff *skb)
551fe4944e5SThomas Graf {
55227a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
553fe4944e5SThomas Graf }
554fe4944e5SThomas Graf 
555fe4944e5SThomas Graf /**
556fe4944e5SThomas Graf  * nlmsg_trim - Trim message to a mark
557fe4944e5SThomas Graf  * @skb: socket buffer the message is stored in
558fe4944e5SThomas Graf  * @mark: mark to trim to
559fe4944e5SThomas Graf  *
560bc3ed28cSThomas Graf  * Trims the message to the provided mark.
561fe4944e5SThomas Graf  */
562bc3ed28cSThomas Graf static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
563fe4944e5SThomas Graf {
564fe4944e5SThomas Graf 	if (mark)
565fe4944e5SThomas Graf 		skb_trim(skb, (unsigned char *) mark - skb->data);
566fe4944e5SThomas Graf }
567fe4944e5SThomas Graf 
568fe4944e5SThomas Graf /**
569bfa83a9eSThomas Graf  * nlmsg_cancel - Cancel construction of a netlink message
570bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
571bfa83a9eSThomas Graf  * @nlh: netlink message header
572bfa83a9eSThomas Graf  *
573bfa83a9eSThomas Graf  * Removes the complete netlink message including all
574bc3ed28cSThomas Graf  * attributes from the socket buffer again.
575bfa83a9eSThomas Graf  */
576bc3ed28cSThomas Graf static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
577bfa83a9eSThomas Graf {
578bc3ed28cSThomas Graf 	nlmsg_trim(skb, nlh);
579bfa83a9eSThomas Graf }
580bfa83a9eSThomas Graf 
581bfa83a9eSThomas Graf /**
582bfa83a9eSThomas Graf  * nlmsg_free - free a netlink message
583bfa83a9eSThomas Graf  * @skb: socket buffer of netlink message
584bfa83a9eSThomas Graf  */
585bfa83a9eSThomas Graf static inline void nlmsg_free(struct sk_buff *skb)
586bfa83a9eSThomas Graf {
587bfa83a9eSThomas Graf 	kfree_skb(skb);
588bfa83a9eSThomas Graf }
589bfa83a9eSThomas Graf 
590bfa83a9eSThomas Graf /**
591bfa83a9eSThomas Graf  * nlmsg_multicast - multicast a netlink message
592bfa83a9eSThomas Graf  * @sk: netlink socket to spread messages to
593bfa83a9eSThomas Graf  * @skb: netlink message as socket buffer
594bfa83a9eSThomas Graf  * @pid: own netlink pid to avoid sending to yourself
595bfa83a9eSThomas Graf  * @group: multicast group id
596d387f6adSThomas Graf  * @flags: allocation flags
597bfa83a9eSThomas Graf  */
598bfa83a9eSThomas Graf static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
599d387f6adSThomas Graf 				  u32 pid, unsigned int group, gfp_t flags)
600bfa83a9eSThomas Graf {
601bfa83a9eSThomas Graf 	int err;
602bfa83a9eSThomas Graf 
603bfa83a9eSThomas Graf 	NETLINK_CB(skb).dst_group = group;
604bfa83a9eSThomas Graf 
605d387f6adSThomas Graf 	err = netlink_broadcast(sk, skb, pid, group, flags);
606bfa83a9eSThomas Graf 	if (err > 0)
607bfa83a9eSThomas Graf 		err = 0;
608bfa83a9eSThomas Graf 
609bfa83a9eSThomas Graf 	return err;
610bfa83a9eSThomas Graf }
611bfa83a9eSThomas Graf 
612bfa83a9eSThomas Graf /**
613bfa83a9eSThomas Graf  * nlmsg_unicast - unicast a netlink message
614bfa83a9eSThomas Graf  * @sk: netlink socket to spread message to
615bfa83a9eSThomas Graf  * @skb: netlink message as socket buffer
616bfa83a9eSThomas Graf  * @pid: netlink pid of the destination socket
617bfa83a9eSThomas Graf  */
618bfa83a9eSThomas Graf static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 pid)
619bfa83a9eSThomas Graf {
620bfa83a9eSThomas Graf 	int err;
621bfa83a9eSThomas Graf 
622bfa83a9eSThomas Graf 	err = netlink_unicast(sk, skb, pid, MSG_DONTWAIT);
623bfa83a9eSThomas Graf 	if (err > 0)
624bfa83a9eSThomas Graf 		err = 0;
625bfa83a9eSThomas Graf 
626bfa83a9eSThomas Graf 	return err;
627bfa83a9eSThomas Graf }
628bfa83a9eSThomas Graf 
629bfa83a9eSThomas Graf /**
630bfa83a9eSThomas Graf  * nlmsg_for_each_msg - iterate over a stream of messages
631bfa83a9eSThomas Graf  * @pos: loop counter, set to current message
632bfa83a9eSThomas Graf  * @head: head of message stream
633bfa83a9eSThomas Graf  * @len: length of message stream
634bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
635bfa83a9eSThomas Graf  */
636bfa83a9eSThomas Graf #define nlmsg_for_each_msg(pos, head, len, rem) \
637bfa83a9eSThomas Graf 	for (pos = head, rem = len; \
638bfa83a9eSThomas Graf 	     nlmsg_ok(pos, rem); \
639bfa83a9eSThomas Graf 	     pos = nlmsg_next(pos, &(rem)))
640bfa83a9eSThomas Graf 
641bfa83a9eSThomas Graf /**************************************************************************
642bfa83a9eSThomas Graf  * Netlink Attributes
643bfa83a9eSThomas Graf  **************************************************************************/
644bfa83a9eSThomas Graf 
645bfa83a9eSThomas Graf /**
646bfa83a9eSThomas Graf  * nla_attr_size - length of attribute not including padding
647bfa83a9eSThomas Graf  * @payload: length of payload
648bfa83a9eSThomas Graf  */
649bfa83a9eSThomas Graf static inline int nla_attr_size(int payload)
650bfa83a9eSThomas Graf {
651bfa83a9eSThomas Graf 	return NLA_HDRLEN + payload;
652bfa83a9eSThomas Graf }
653bfa83a9eSThomas Graf 
654bfa83a9eSThomas Graf /**
655bfa83a9eSThomas Graf  * nla_total_size - total length of attribute including padding
656bfa83a9eSThomas Graf  * @payload: length of payload
657bfa83a9eSThomas Graf  */
658bfa83a9eSThomas Graf static inline int nla_total_size(int payload)
659bfa83a9eSThomas Graf {
660bfa83a9eSThomas Graf 	return NLA_ALIGN(nla_attr_size(payload));
661bfa83a9eSThomas Graf }
662bfa83a9eSThomas Graf 
663bfa83a9eSThomas Graf /**
664bfa83a9eSThomas Graf  * nla_padlen - length of padding at the tail of attribute
665bfa83a9eSThomas Graf  * @payload: length of payload
666bfa83a9eSThomas Graf  */
667bfa83a9eSThomas Graf static inline int nla_padlen(int payload)
668bfa83a9eSThomas Graf {
669bfa83a9eSThomas Graf 	return nla_total_size(payload) - nla_attr_size(payload);
670bfa83a9eSThomas Graf }
671bfa83a9eSThomas Graf 
672bfa83a9eSThomas Graf /**
6738f4c1f9bSThomas Graf  * nla_type - attribute type
6748f4c1f9bSThomas Graf  * @nla: netlink attribute
6758f4c1f9bSThomas Graf  */
6768f4c1f9bSThomas Graf static inline int nla_type(const struct nlattr *nla)
6778f4c1f9bSThomas Graf {
6788f4c1f9bSThomas Graf 	return nla->nla_type & NLA_TYPE_MASK;
6798f4c1f9bSThomas Graf }
6808f4c1f9bSThomas Graf 
6818f4c1f9bSThomas Graf /**
682bfa83a9eSThomas Graf  * nla_data - head of payload
683bfa83a9eSThomas Graf  * @nla: netlink attribute
684bfa83a9eSThomas Graf  */
685bfa83a9eSThomas Graf static inline void *nla_data(const struct nlattr *nla)
686bfa83a9eSThomas Graf {
687bfa83a9eSThomas Graf 	return (char *) nla + NLA_HDRLEN;
688bfa83a9eSThomas Graf }
689bfa83a9eSThomas Graf 
690bfa83a9eSThomas Graf /**
691bfa83a9eSThomas Graf  * nla_len - length of payload
692bfa83a9eSThomas Graf  * @nla: netlink attribute
693bfa83a9eSThomas Graf  */
694bfa83a9eSThomas Graf static inline int nla_len(const struct nlattr *nla)
695bfa83a9eSThomas Graf {
696bfa83a9eSThomas Graf 	return nla->nla_len - NLA_HDRLEN;
697bfa83a9eSThomas Graf }
698bfa83a9eSThomas Graf 
699bfa83a9eSThomas Graf /**
700bfa83a9eSThomas Graf  * nla_ok - check if the netlink attribute fits into the remaining bytes
701bfa83a9eSThomas Graf  * @nla: netlink attribute
702bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in attribute stream
703bfa83a9eSThomas Graf  */
704bfa83a9eSThomas Graf static inline int nla_ok(const struct nlattr *nla, int remaining)
705bfa83a9eSThomas Graf {
7061045b03eSVegard Nossum 	return remaining >= (int) sizeof(*nla) &&
707bfa83a9eSThomas Graf 	       nla->nla_len >= sizeof(*nla) &&
708bfa83a9eSThomas Graf 	       nla->nla_len <= remaining;
709bfa83a9eSThomas Graf }
710bfa83a9eSThomas Graf 
711bfa83a9eSThomas Graf /**
712d1ec3b77SPierre Ynard  * nla_next - next netlink attribute in attribute stream
713bfa83a9eSThomas Graf  * @nla: netlink attribute
714bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in attribute stream
715bfa83a9eSThomas Graf  *
716bfa83a9eSThomas Graf  * Returns the next netlink attribute in the attribute stream and
717bfa83a9eSThomas Graf  * decrements remaining by the size of the current attribute.
718bfa83a9eSThomas Graf  */
719bfa83a9eSThomas Graf static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
720bfa83a9eSThomas Graf {
721bfa83a9eSThomas Graf 	int totlen = NLA_ALIGN(nla->nla_len);
722bfa83a9eSThomas Graf 
723bfa83a9eSThomas Graf 	*remaining -= totlen;
724bfa83a9eSThomas Graf 	return (struct nlattr *) ((char *) nla + totlen);
725bfa83a9eSThomas Graf }
726bfa83a9eSThomas Graf 
727bfa83a9eSThomas Graf /**
728fe4944e5SThomas Graf  * nla_find_nested - find attribute in a set of nested attributes
729fe4944e5SThomas Graf  * @nla: attribute containing the nested attributes
730fe4944e5SThomas Graf  * @attrtype: type of attribute to look for
731fe4944e5SThomas Graf  *
732fe4944e5SThomas Graf  * Returns the first attribute which matches the specified type.
733fe4944e5SThomas Graf  */
7343654654fSJan Engelhardt static inline struct nlattr *
7353654654fSJan Engelhardt nla_find_nested(const struct nlattr *nla, int attrtype)
736fe4944e5SThomas Graf {
737fe4944e5SThomas Graf 	return nla_find(nla_data(nla), nla_len(nla), attrtype);
738fe4944e5SThomas Graf }
739fe4944e5SThomas Graf 
740fe4944e5SThomas Graf /**
741bfa83a9eSThomas Graf  * nla_parse_nested - parse nested attributes
742bfa83a9eSThomas Graf  * @tb: destination array with maxtype+1 elements
743bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
744bfa83a9eSThomas Graf  * @nla: attribute containing the nested attributes
745bfa83a9eSThomas Graf  * @policy: validation policy
746bfa83a9eSThomas Graf  *
747bfa83a9eSThomas Graf  * See nla_parse()
748bfa83a9eSThomas Graf  */
749bfa83a9eSThomas Graf static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
750b057efd4SPatrick McHardy 				   const struct nlattr *nla,
751ef7c79edSPatrick McHardy 				   const struct nla_policy *policy)
752bfa83a9eSThomas Graf {
753bfa83a9eSThomas Graf 	return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
754bfa83a9eSThomas Graf }
7551092cb21SPatrick McHardy 
7561092cb21SPatrick McHardy /**
757d1ec3b77SPierre Ynard  * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
758bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
759bfa83a9eSThomas Graf  * @attrtype: attribute type
760bfa83a9eSThomas Graf  * @value: numeric value
761bfa83a9eSThomas Graf  */
762bfa83a9eSThomas Graf static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
763bfa83a9eSThomas Graf {
764bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, sizeof(u8), &value);
765bfa83a9eSThomas Graf }
766bfa83a9eSThomas Graf 
767bfa83a9eSThomas Graf /**
768bfa83a9eSThomas Graf  * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
769bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
770bfa83a9eSThomas Graf  * @attrtype: attribute type
771bfa83a9eSThomas Graf  * @value: numeric value
772bfa83a9eSThomas Graf  */
773bfa83a9eSThomas Graf static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
774bfa83a9eSThomas Graf {
775bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, sizeof(u16), &value);
776bfa83a9eSThomas Graf }
777bfa83a9eSThomas Graf 
778bfa83a9eSThomas Graf /**
779bfa83a9eSThomas Graf  * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
780bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
781bfa83a9eSThomas Graf  * @attrtype: attribute type
782bfa83a9eSThomas Graf  * @value: numeric value
783bfa83a9eSThomas Graf  */
784bfa83a9eSThomas Graf static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
785bfa83a9eSThomas Graf {
786bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, sizeof(u32), &value);
787bfa83a9eSThomas Graf }
788bfa83a9eSThomas Graf 
789bfa83a9eSThomas Graf /**
790bfa83a9eSThomas Graf  * nla_put_64 - Add a u64 netlink attribute to a socket buffer
791bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
792bfa83a9eSThomas Graf  * @attrtype: attribute type
793bfa83a9eSThomas Graf  * @value: numeric value
794bfa83a9eSThomas Graf  */
795bfa83a9eSThomas Graf static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value)
796bfa83a9eSThomas Graf {
797bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, sizeof(u64), &value);
798bfa83a9eSThomas Graf }
799bfa83a9eSThomas Graf 
800bfa83a9eSThomas Graf /**
801bfa83a9eSThomas Graf  * nla_put_string - Add a string netlink attribute to a socket buffer
802bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
803bfa83a9eSThomas Graf  * @attrtype: attribute type
804bfa83a9eSThomas Graf  * @str: NUL terminated string
805bfa83a9eSThomas Graf  */
806bfa83a9eSThomas Graf static inline int nla_put_string(struct sk_buff *skb, int attrtype,
807bfa83a9eSThomas Graf 				 const char *str)
808bfa83a9eSThomas Graf {
809bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, strlen(str) + 1, str);
810bfa83a9eSThomas Graf }
811bfa83a9eSThomas Graf 
812bfa83a9eSThomas Graf /**
813bfa83a9eSThomas Graf  * nla_put_flag - Add a flag netlink attribute to a socket buffer
814bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
815bfa83a9eSThomas Graf  * @attrtype: attribute type
816bfa83a9eSThomas Graf  */
817bfa83a9eSThomas Graf static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
818bfa83a9eSThomas Graf {
819bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, 0, NULL);
820bfa83a9eSThomas Graf }
821bfa83a9eSThomas Graf 
822bfa83a9eSThomas Graf /**
823bfa83a9eSThomas Graf  * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
824bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
825bfa83a9eSThomas Graf  * @attrtype: attribute type
826bfa83a9eSThomas Graf  * @jiffies: number of msecs in jiffies
827bfa83a9eSThomas Graf  */
828bfa83a9eSThomas Graf static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
829bfa83a9eSThomas Graf 				unsigned long jiffies)
830bfa83a9eSThomas Graf {
831bfa83a9eSThomas Graf 	u64 tmp = jiffies_to_msecs(jiffies);
832bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, sizeof(u64), &tmp);
833bfa83a9eSThomas Graf }
834bfa83a9eSThomas Graf 
835bfa83a9eSThomas Graf #define NLA_PUT(skb, attrtype, attrlen, data) \
836bfa83a9eSThomas Graf 	do { \
837f4d900a2SPatrick McHardy 		if (unlikely(nla_put(skb, attrtype, attrlen, data) < 0)) \
838bfa83a9eSThomas Graf 			goto nla_put_failure; \
839bfa83a9eSThomas Graf 	} while(0)
840bfa83a9eSThomas Graf 
841bfa83a9eSThomas Graf #define NLA_PUT_TYPE(skb, type, attrtype, value) \
842bfa83a9eSThomas Graf 	do { \
843bfa83a9eSThomas Graf 		type __tmp = value; \
844bfa83a9eSThomas Graf 		NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \
845bfa83a9eSThomas Graf 	} while(0)
846bfa83a9eSThomas Graf 
847bfa83a9eSThomas Graf #define NLA_PUT_U8(skb, attrtype, value) \
848bfa83a9eSThomas Graf 	NLA_PUT_TYPE(skb, u8, attrtype, value)
849bfa83a9eSThomas Graf 
850bfa83a9eSThomas Graf #define NLA_PUT_U16(skb, attrtype, value) \
851bfa83a9eSThomas Graf 	NLA_PUT_TYPE(skb, u16, attrtype, value)
852bfa83a9eSThomas Graf 
8534a89c256SThomas Graf #define NLA_PUT_LE16(skb, attrtype, value) \
8544a89c256SThomas Graf 	NLA_PUT_TYPE(skb, __le16, attrtype, value)
8554a89c256SThomas Graf 
856838965baSPatrick McHardy #define NLA_PUT_BE16(skb, attrtype, value) \
857838965baSPatrick McHardy 	NLA_PUT_TYPE(skb, __be16, attrtype, value)
858838965baSPatrick McHardy 
859f703651eSJozsef Kadlecsik #define NLA_PUT_NET16(skb, attrtype, value) \
860f703651eSJozsef Kadlecsik 	NLA_PUT_BE16(skb, attrtype | NLA_F_NET_BYTEORDER, value)
861f703651eSJozsef Kadlecsik 
862bfa83a9eSThomas Graf #define NLA_PUT_U32(skb, attrtype, value) \
863bfa83a9eSThomas Graf 	NLA_PUT_TYPE(skb, u32, attrtype, value)
864bfa83a9eSThomas Graf 
86500012e5bSAl Viro #define NLA_PUT_BE32(skb, attrtype, value) \
86600012e5bSAl Viro 	NLA_PUT_TYPE(skb, __be32, attrtype, value)
86700012e5bSAl Viro 
868f703651eSJozsef Kadlecsik #define NLA_PUT_NET32(skb, attrtype, value) \
869f703651eSJozsef Kadlecsik 	NLA_PUT_BE32(skb, attrtype | NLA_F_NET_BYTEORDER, value)
870f703651eSJozsef Kadlecsik 
871bfa83a9eSThomas Graf #define NLA_PUT_U64(skb, attrtype, value) \
872bfa83a9eSThomas Graf 	NLA_PUT_TYPE(skb, u64, attrtype, value)
873bfa83a9eSThomas Graf 
87407a7c107SKrzysztof Piotr Oledzki #define NLA_PUT_BE64(skb, attrtype, value) \
87507a7c107SKrzysztof Piotr Oledzki 	NLA_PUT_TYPE(skb, __be64, attrtype, value)
87607a7c107SKrzysztof Piotr Oledzki 
877f703651eSJozsef Kadlecsik #define NLA_PUT_NET64(skb, attrtype, value) \
878f703651eSJozsef Kadlecsik 	NLA_PUT_BE64(skb, attrtype | NLA_F_NET_BYTEORDER, value)
879f703651eSJozsef Kadlecsik 
880bfa83a9eSThomas Graf #define NLA_PUT_STRING(skb, attrtype, value) \
881bfa83a9eSThomas Graf 	NLA_PUT(skb, attrtype, strlen(value) + 1, value)
882bfa83a9eSThomas Graf 
883ff5dfe73SJohannes Berg #define NLA_PUT_FLAG(skb, attrtype) \
884bfa83a9eSThomas Graf 	NLA_PUT(skb, attrtype, 0, NULL)
885bfa83a9eSThomas Graf 
886bfa83a9eSThomas Graf #define NLA_PUT_MSECS(skb, attrtype, jiffies) \
887bfa83a9eSThomas Graf 	NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies))
888bfa83a9eSThomas Graf 
889bfa83a9eSThomas Graf /**
890bfa83a9eSThomas Graf  * nla_get_u32 - return payload of u32 attribute
891bfa83a9eSThomas Graf  * @nla: u32 netlink attribute
892bfa83a9eSThomas Graf  */
893b057efd4SPatrick McHardy static inline u32 nla_get_u32(const struct nlattr *nla)
894bfa83a9eSThomas Graf {
895bfa83a9eSThomas Graf 	return *(u32 *) nla_data(nla);
896bfa83a9eSThomas Graf }
897bfa83a9eSThomas Graf 
898bfa83a9eSThomas Graf /**
89900012e5bSAl Viro  * nla_get_be32 - return payload of __be32 attribute
90000012e5bSAl Viro  * @nla: __be32 netlink attribute
90100012e5bSAl Viro  */
902b057efd4SPatrick McHardy static inline __be32 nla_get_be32(const struct nlattr *nla)
90300012e5bSAl Viro {
90400012e5bSAl Viro 	return *(__be32 *) nla_data(nla);
90500012e5bSAl Viro }
90600012e5bSAl Viro 
90700012e5bSAl Viro /**
908bfa83a9eSThomas Graf  * nla_get_u16 - return payload of u16 attribute
909bfa83a9eSThomas Graf  * @nla: u16 netlink attribute
910bfa83a9eSThomas Graf  */
911b057efd4SPatrick McHardy static inline u16 nla_get_u16(const struct nlattr *nla)
912bfa83a9eSThomas Graf {
913bfa83a9eSThomas Graf 	return *(u16 *) nla_data(nla);
914bfa83a9eSThomas Graf }
915bfa83a9eSThomas Graf 
916bfa83a9eSThomas Graf /**
917838965baSPatrick McHardy  * nla_get_be16 - return payload of __be16 attribute
918838965baSPatrick McHardy  * @nla: __be16 netlink attribute
919838965baSPatrick McHardy  */
920b057efd4SPatrick McHardy static inline __be16 nla_get_be16(const struct nlattr *nla)
921838965baSPatrick McHardy {
922838965baSPatrick McHardy 	return *(__be16 *) nla_data(nla);
923838965baSPatrick McHardy }
924838965baSPatrick McHardy 
925838965baSPatrick McHardy /**
9264a89c256SThomas Graf  * nla_get_le16 - return payload of __le16 attribute
9274a89c256SThomas Graf  * @nla: __le16 netlink attribute
9284a89c256SThomas Graf  */
929b057efd4SPatrick McHardy static inline __le16 nla_get_le16(const struct nlattr *nla)
9304a89c256SThomas Graf {
9314a89c256SThomas Graf 	return *(__le16 *) nla_data(nla);
9324a89c256SThomas Graf }
9334a89c256SThomas Graf 
9344a89c256SThomas Graf /**
935bfa83a9eSThomas Graf  * nla_get_u8 - return payload of u8 attribute
936bfa83a9eSThomas Graf  * @nla: u8 netlink attribute
937bfa83a9eSThomas Graf  */
938b057efd4SPatrick McHardy static inline u8 nla_get_u8(const struct nlattr *nla)
939bfa83a9eSThomas Graf {
940bfa83a9eSThomas Graf 	return *(u8 *) nla_data(nla);
941bfa83a9eSThomas Graf }
942bfa83a9eSThomas Graf 
943bfa83a9eSThomas Graf /**
944bfa83a9eSThomas Graf  * nla_get_u64 - return payload of u64 attribute
945bfa83a9eSThomas Graf  * @nla: u64 netlink attribute
946bfa83a9eSThomas Graf  */
947b057efd4SPatrick McHardy static inline u64 nla_get_u64(const struct nlattr *nla)
948bfa83a9eSThomas Graf {
949bfa83a9eSThomas Graf 	u64 tmp;
950bfa83a9eSThomas Graf 
951bfa83a9eSThomas Graf 	nla_memcpy(&tmp, nla, sizeof(tmp));
952bfa83a9eSThomas Graf 
953bfa83a9eSThomas Graf 	return tmp;
954bfa83a9eSThomas Graf }
955bfa83a9eSThomas Graf 
956bfa83a9eSThomas Graf /**
957a17c8598SPablo Neira Ayuso  * nla_get_be64 - return payload of __be64 attribute
958a17c8598SPablo Neira Ayuso  * @nla: __be64 netlink attribute
959a17c8598SPablo Neira Ayuso  */
960a17c8598SPablo Neira Ayuso static inline __be64 nla_get_be64(const struct nlattr *nla)
961a17c8598SPablo Neira Ayuso {
962f5d410f2SPablo Neira Ayuso 	__be64 tmp;
963f5d410f2SPablo Neira Ayuso 
964f5d410f2SPablo Neira Ayuso 	nla_memcpy(&tmp, nla, sizeof(tmp));
965f5d410f2SPablo Neira Ayuso 
966f5d410f2SPablo Neira Ayuso 	return tmp;
967a17c8598SPablo Neira Ayuso }
968a17c8598SPablo Neira Ayuso 
969a17c8598SPablo Neira Ayuso /**
970bfa83a9eSThomas Graf  * nla_get_flag - return payload of flag attribute
971bfa83a9eSThomas Graf  * @nla: flag netlink attribute
972bfa83a9eSThomas Graf  */
973b057efd4SPatrick McHardy static inline int nla_get_flag(const struct nlattr *nla)
974bfa83a9eSThomas Graf {
975bfa83a9eSThomas Graf 	return !!nla;
976bfa83a9eSThomas Graf }
977bfa83a9eSThomas Graf 
978bfa83a9eSThomas Graf /**
979bfa83a9eSThomas Graf  * nla_get_msecs - return payload of msecs attribute
980bfa83a9eSThomas Graf  * @nla: msecs netlink attribute
981bfa83a9eSThomas Graf  *
982bfa83a9eSThomas Graf  * Returns the number of milliseconds in jiffies.
983bfa83a9eSThomas Graf  */
984b057efd4SPatrick McHardy static inline unsigned long nla_get_msecs(const struct nlattr *nla)
985bfa83a9eSThomas Graf {
986bfa83a9eSThomas Graf 	u64 msecs = nla_get_u64(nla);
987bfa83a9eSThomas Graf 
988bfa83a9eSThomas Graf 	return msecs_to_jiffies((unsigned long) msecs);
989bfa83a9eSThomas Graf }
990bfa83a9eSThomas Graf 
991bfa83a9eSThomas Graf /**
992bfa83a9eSThomas Graf  * nla_nest_start - Start a new level of nested attributes
993bfa83a9eSThomas Graf  * @skb: socket buffer to add attributes to
994bfa83a9eSThomas Graf  * @attrtype: attribute type of container
995bfa83a9eSThomas Graf  *
996bfa83a9eSThomas Graf  * Returns the container attribute
997bfa83a9eSThomas Graf  */
998bfa83a9eSThomas Graf static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
999bfa83a9eSThomas Graf {
100027a884dcSArnaldo Carvalho de Melo 	struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
1001bfa83a9eSThomas Graf 
1002bfa83a9eSThomas Graf 	if (nla_put(skb, attrtype, 0, NULL) < 0)
1003bfa83a9eSThomas Graf 		return NULL;
1004bfa83a9eSThomas Graf 
1005bfa83a9eSThomas Graf 	return start;
1006bfa83a9eSThomas Graf }
1007bfa83a9eSThomas Graf 
1008bfa83a9eSThomas Graf /**
1009bfa83a9eSThomas Graf  * nla_nest_end - Finalize nesting of attributes
1010d1ec3b77SPierre Ynard  * @skb: socket buffer the attributes are stored in
1011bfa83a9eSThomas Graf  * @start: container attribute
1012bfa83a9eSThomas Graf  *
1013bfa83a9eSThomas Graf  * Corrects the container attribute header to include the all
1014bfa83a9eSThomas Graf  * appeneded attributes.
1015bfa83a9eSThomas Graf  *
1016bfa83a9eSThomas Graf  * Returns the total data length of the skb.
1017bfa83a9eSThomas Graf  */
1018bfa83a9eSThomas Graf static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
1019bfa83a9eSThomas Graf {
102027a884dcSArnaldo Carvalho de Melo 	start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
1021bfa83a9eSThomas Graf 	return skb->len;
1022bfa83a9eSThomas Graf }
1023bfa83a9eSThomas Graf 
1024bfa83a9eSThomas Graf /**
1025bfa83a9eSThomas Graf  * nla_nest_cancel - Cancel nesting of attributes
1026bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
1027bfa83a9eSThomas Graf  * @start: container attribute
1028bfa83a9eSThomas Graf  *
1029bfa83a9eSThomas Graf  * Removes the container attribute and including all nested
1030bc3ed28cSThomas Graf  * attributes. Returns -EMSGSIZE
1031bfa83a9eSThomas Graf  */
1032bc3ed28cSThomas Graf static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
1033bfa83a9eSThomas Graf {
1034bc3ed28cSThomas Graf 	nlmsg_trim(skb, start);
1035bfa83a9eSThomas Graf }
1036bfa83a9eSThomas Graf 
1037bfa83a9eSThomas Graf /**
10384fe5d5c0SPaul Moore  * nla_validate_nested - Validate a stream of nested attributes
10394fe5d5c0SPaul Moore  * @start: container attribute
10404fe5d5c0SPaul Moore  * @maxtype: maximum attribute type to be expected
10414fe5d5c0SPaul Moore  * @policy: validation policy
10424fe5d5c0SPaul Moore  *
10434fe5d5c0SPaul Moore  * Validates all attributes in the nested attribute stream against the
10444fe5d5c0SPaul Moore  * specified policy. Attributes with a type exceeding maxtype will be
10454fe5d5c0SPaul Moore  * ignored. See documenation of struct nla_policy for more details.
10464fe5d5c0SPaul Moore  *
10474fe5d5c0SPaul Moore  * Returns 0 on success or a negative error code.
10484fe5d5c0SPaul Moore  */
10493654654fSJan Engelhardt static inline int nla_validate_nested(const struct nlattr *start, int maxtype,
1050ef7c79edSPatrick McHardy 				      const struct nla_policy *policy)
10514fe5d5c0SPaul Moore {
10524fe5d5c0SPaul Moore 	return nla_validate(nla_data(start), nla_len(start), maxtype, policy);
10534fe5d5c0SPaul Moore }
10544fe5d5c0SPaul Moore 
10554fe5d5c0SPaul Moore /**
1056bfa83a9eSThomas Graf  * nla_for_each_attr - iterate over a stream of attributes
1057bfa83a9eSThomas Graf  * @pos: loop counter, set to current attribute
1058bfa83a9eSThomas Graf  * @head: head of attribute stream
1059bfa83a9eSThomas Graf  * @len: length of attribute stream
1060bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
1061bfa83a9eSThomas Graf  */
1062bfa83a9eSThomas Graf #define nla_for_each_attr(pos, head, len, rem) \
1063bfa83a9eSThomas Graf 	for (pos = head, rem = len; \
1064bfa83a9eSThomas Graf 	     nla_ok(pos, rem); \
1065bfa83a9eSThomas Graf 	     pos = nla_next(pos, &(rem)))
1066bfa83a9eSThomas Graf 
1067fe4944e5SThomas Graf /**
1068fe4944e5SThomas Graf  * nla_for_each_nested - iterate over nested attributes
1069fe4944e5SThomas Graf  * @pos: loop counter, set to current attribute
1070fe4944e5SThomas Graf  * @nla: attribute containing the nested attributes
1071fe4944e5SThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
1072fe4944e5SThomas Graf  */
1073fe4944e5SThomas Graf #define nla_for_each_nested(pos, nla, rem) \
1074fe4944e5SThomas Graf 	nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
1075fe4944e5SThomas Graf 
1076bfa83a9eSThomas Graf #endif
1077