xref: /openbmc/linux/include/net/netlink.h (revision a5531a5d)
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>
6bfa83a9eSThomas Graf 
7bfa83a9eSThomas Graf /* ========================================================================
8bfa83a9eSThomas Graf  *         Netlink Messages and Attributes Interface (As Seen On TV)
9bfa83a9eSThomas Graf  * ------------------------------------------------------------------------
10bfa83a9eSThomas Graf  *                          Messages Interface
11bfa83a9eSThomas Graf  * ------------------------------------------------------------------------
12bfa83a9eSThomas Graf  *
13bfa83a9eSThomas Graf  * Message Format:
14bfa83a9eSThomas Graf  *    <--- nlmsg_total_size(payload)  --->
15bfa83a9eSThomas Graf  *    <-- nlmsg_msg_size(payload) ->
16bfa83a9eSThomas Graf  *   +----------+- - -+-------------+- - -+-------- - -
17bfa83a9eSThomas Graf  *   | nlmsghdr | Pad |   Payload   | Pad | nlmsghdr
18bfa83a9eSThomas Graf  *   +----------+- - -+-------------+- - -+-------- - -
19bfa83a9eSThomas Graf  *   nlmsg_data(nlh)---^                   ^
20bfa83a9eSThomas Graf  *   nlmsg_next(nlh)-----------------------+
21bfa83a9eSThomas Graf  *
22bfa83a9eSThomas Graf  * Payload Format:
23bfa83a9eSThomas Graf  *    <---------------------- nlmsg_len(nlh) --------------------->
24bfa83a9eSThomas Graf  *    <------ hdrlen ------>       <- nlmsg_attrlen(nlh, hdrlen) ->
25bfa83a9eSThomas Graf  *   +----------------------+- - -+--------------------------------+
26bfa83a9eSThomas Graf  *   |     Family Header    | Pad |           Attributes           |
27bfa83a9eSThomas Graf  *   +----------------------+- - -+--------------------------------+
28bfa83a9eSThomas Graf  *   nlmsg_attrdata(nlh, hdrlen)---^
29bfa83a9eSThomas Graf  *
30bfa83a9eSThomas Graf  * Data Structures:
31bfa83a9eSThomas Graf  *   struct nlmsghdr			netlink message header
32bfa83a9eSThomas Graf  *
33bfa83a9eSThomas Graf  * Message Construction:
34bfa83a9eSThomas Graf  *   nlmsg_new()			create a new netlink message
35bfa83a9eSThomas Graf  *   nlmsg_put()			add a netlink message to an skb
36bfa83a9eSThomas Graf  *   nlmsg_put_answer()			callback based nlmsg_put()
37bfa83a9eSThomas Graf  *   nlmsg_end()			finanlize netlink message
38fe4944e5SThomas Graf  *   nlmsg_get_pos()			return current position in message
39fe4944e5SThomas Graf  *   nlmsg_trim()			trim part of message
40bfa83a9eSThomas Graf  *   nlmsg_cancel()			cancel message construction
41bfa83a9eSThomas Graf  *   nlmsg_free()			free a netlink message
42bfa83a9eSThomas Graf  *
43bfa83a9eSThomas Graf  * Message Sending:
44bfa83a9eSThomas Graf  *   nlmsg_multicast()			multicast message to several groups
45bfa83a9eSThomas Graf  *   nlmsg_unicast()			unicast a message to a single socket
46d387f6adSThomas Graf  *   nlmsg_notify()			send notification message
47bfa83a9eSThomas Graf  *
48bfa83a9eSThomas Graf  * Message Length Calculations:
49bfa83a9eSThomas Graf  *   nlmsg_msg_size(payload)		length of message w/o padding
50bfa83a9eSThomas Graf  *   nlmsg_total_size(payload)		length of message w/ padding
51bfa83a9eSThomas Graf  *   nlmsg_padlen(payload)		length of padding at tail
52bfa83a9eSThomas Graf  *
53bfa83a9eSThomas Graf  * Message Payload Access:
54bfa83a9eSThomas Graf  *   nlmsg_data(nlh)			head of message payload
55bfa83a9eSThomas Graf  *   nlmsg_len(nlh)			length of message payload
56bfa83a9eSThomas Graf  *   nlmsg_attrdata(nlh, hdrlen)	head of attributes data
57bfa83a9eSThomas Graf  *   nlmsg_attrlen(nlh, hdrlen)		length of attributes data
58bfa83a9eSThomas Graf  *
59bfa83a9eSThomas Graf  * Message Parsing:
60bfa83a9eSThomas Graf  *   nlmsg_ok(nlh, remaining)		does nlh fit into remaining bytes?
61bfa83a9eSThomas Graf  *   nlmsg_next(nlh, remaining)		get next netlink message
62bfa83a9eSThomas Graf  *   nlmsg_parse()			parse attributes of a message
63bfa83a9eSThomas Graf  *   nlmsg_find_attr()			find an attribute in a message
64bfa83a9eSThomas Graf  *   nlmsg_for_each_msg()		loop over all messages
65bfa83a9eSThomas Graf  *   nlmsg_validate()			validate netlink message incl. attrs
66bfa83a9eSThomas Graf  *   nlmsg_for_each_attr()		loop over all attributes
67bfa83a9eSThomas Graf  *
6897676b6bSThomas Graf  * Misc:
6997676b6bSThomas Graf  *   nlmsg_report()			report back to application?
7097676b6bSThomas Graf  *
71bfa83a9eSThomas Graf  * ------------------------------------------------------------------------
72bfa83a9eSThomas Graf  *                          Attributes Interface
73bfa83a9eSThomas Graf  * ------------------------------------------------------------------------
74bfa83a9eSThomas Graf  *
75bfa83a9eSThomas Graf  * Attribute Format:
76bfa83a9eSThomas Graf  *    <------- nla_total_size(payload) ------->
77bfa83a9eSThomas Graf  *    <---- nla_attr_size(payload) ----->
78bfa83a9eSThomas Graf  *   +----------+- - -+- - - - - - - - - +- - -+-------- - -
79bfa83a9eSThomas Graf  *   |  Header  | Pad |     Payload      | Pad |  Header
80bfa83a9eSThomas Graf  *   +----------+- - -+- - - - - - - - - +- - -+-------- - -
81bfa83a9eSThomas Graf  *                     <- nla_len(nla) ->      ^
82bfa83a9eSThomas Graf  *   nla_data(nla)----^                        |
83bfa83a9eSThomas Graf  *   nla_next(nla)-----------------------------'
84bfa83a9eSThomas Graf  *
85bfa83a9eSThomas Graf  * Data Structures:
86bfa83a9eSThomas Graf  *   struct nlattr			netlink attribtue header
87bfa83a9eSThomas Graf  *
88bfa83a9eSThomas Graf  * Attribute Construction:
89fe4944e5SThomas Graf  *   nla_reserve(skb, type, len)	reserve room for an attribute
90fe4944e5SThomas Graf  *   nla_reserve_nohdr(skb, len)	reserve room for an attribute w/o hdr
91bfa83a9eSThomas Graf  *   nla_put(skb, type, len, data)	add attribute to skb
92fe4944e5SThomas Graf  *   nla_put_nohdr(skb, len, data)	add attribute w/o hdr
93bfa83a9eSThomas Graf  *
94bfa83a9eSThomas Graf  * Attribute Construction for Basic Types:
95bfa83a9eSThomas Graf  *   nla_put_u8(skb, type, value)	add u8 attribute to skb
96bfa83a9eSThomas Graf  *   nla_put_u16(skb, type, value)	add u16 attribute to skb
97bfa83a9eSThomas Graf  *   nla_put_u32(skb, type, value)	add u32 attribute to skb
98bfa83a9eSThomas Graf  *   nla_put_u64(skb, type, value)	add u64 attribute to skb
99bfa83a9eSThomas Graf  *   nla_put_string(skb, type, str)	add string attribute to skb
100bfa83a9eSThomas Graf  *   nla_put_flag(skb, type)		add flag attribute to skb
101bfa83a9eSThomas Graf  *   nla_put_msecs(skb, type, jiffies)	add msecs attribute to skb
102bfa83a9eSThomas Graf  *
103bfa83a9eSThomas Graf  * Exceptions Based Attribute Construction:
104bfa83a9eSThomas Graf  *   NLA_PUT(skb, type, len, data)	add attribute to skb
105bfa83a9eSThomas Graf  *   NLA_PUT_U8(skb, type, value)	add u8 attribute to skb
106bfa83a9eSThomas Graf  *   NLA_PUT_U16(skb, type, value)	add u16 attribute to skb
107bfa83a9eSThomas Graf  *   NLA_PUT_U32(skb, type, value)	add u32 attribute to skb
108bfa83a9eSThomas Graf  *   NLA_PUT_U64(skb, type, value)	add u64 attribute to skb
109bfa83a9eSThomas Graf  *   NLA_PUT_STRING(skb, type, str)	add string attribute to skb
110bfa83a9eSThomas Graf  *   NLA_PUT_FLAG(skb, type)		add flag attribute to skb
111bfa83a9eSThomas Graf  *   NLA_PUT_MSECS(skb, type, jiffies)	add msecs attribute to skb
112bfa83a9eSThomas Graf  *
113bfa83a9eSThomas Graf  *   The meaning of these functions is equal to their lower case
114bfa83a9eSThomas Graf  *   variants but they jump to the label nla_put_failure in case
115bfa83a9eSThomas Graf  *   of a failure.
116bfa83a9eSThomas Graf  *
117bfa83a9eSThomas Graf  * Nested Attributes Construction:
118bfa83a9eSThomas Graf  *   nla_nest_start(skb, type)		start a nested attribute
119bfa83a9eSThomas Graf  *   nla_nest_end(skb, nla)		finalize a nested attribute
120bfa83a9eSThomas Graf  *   nla_nest_cancel(skb, nla)		cancel nested attribute construction
121bfa83a9eSThomas Graf  *
122bfa83a9eSThomas Graf  * Attribute Length Calculations:
123bfa83a9eSThomas Graf  *   nla_attr_size(payload)		length of attribute w/o padding
124bfa83a9eSThomas Graf  *   nla_total_size(payload)		length of attribute w/ padding
125bfa83a9eSThomas Graf  *   nla_padlen(payload)		length of padding
126bfa83a9eSThomas Graf  *
127bfa83a9eSThomas Graf  * Attribute Payload Access:
128bfa83a9eSThomas Graf  *   nla_data(nla)			head of attribute payload
129bfa83a9eSThomas Graf  *   nla_len(nla)			length of attribute payload
130bfa83a9eSThomas Graf  *
131bfa83a9eSThomas Graf  * Attribute Payload Access for Basic Types:
132bfa83a9eSThomas Graf  *   nla_get_u8(nla)			get payload for a u8 attribute
133bfa83a9eSThomas Graf  *   nla_get_u16(nla)			get payload for a u16 attribute
134bfa83a9eSThomas Graf  *   nla_get_u32(nla)			get payload for a u32 attribute
135bfa83a9eSThomas Graf  *   nla_get_u64(nla)			get payload for a u64 attribute
136bfa83a9eSThomas Graf  *   nla_get_flag(nla)			return 1 if flag is true
137bfa83a9eSThomas Graf  *   nla_get_msecs(nla)			get payload for a msecs attribute
138bfa83a9eSThomas Graf  *
139bfa83a9eSThomas Graf  * Attribute Misc:
140bfa83a9eSThomas Graf  *   nla_memcpy(dest, nla, count)	copy attribute into memory
141bfa83a9eSThomas Graf  *   nla_memcmp(nla, data, size)	compare attribute with memory area
142bfa83a9eSThomas Graf  *   nla_strlcpy(dst, nla, size)	copy attribute to a sized string
143bfa83a9eSThomas Graf  *   nla_strcmp(nla, str)		compare attribute with string
144bfa83a9eSThomas Graf  *
145bfa83a9eSThomas Graf  * Attribute Parsing:
146bfa83a9eSThomas Graf  *   nla_ok(nla, remaining)		does nla fit into remaining bytes?
147bfa83a9eSThomas Graf  *   nla_next(nla, remaining)		get next netlink attribute
148bfa83a9eSThomas Graf  *   nla_validate()			validate a stream of attributes
149bfa83a9eSThomas Graf  *   nla_find()				find attribute in stream of attributes
150fe4944e5SThomas Graf  *   nla_find_nested()			find attribute in nested attributes
151bfa83a9eSThomas Graf  *   nla_parse()			parse and validate stream of attrs
152bfa83a9eSThomas Graf  *   nla_parse_nested()			parse nested attribuets
153bfa83a9eSThomas Graf  *   nla_for_each_attr()		loop over all attributes
154bfa83a9eSThomas Graf  *=========================================================================
155bfa83a9eSThomas Graf  */
156bfa83a9eSThomas Graf 
157bfa83a9eSThomas Graf  /**
158bfa83a9eSThomas Graf   * Standard attribute types to specify validation policy
159bfa83a9eSThomas Graf   */
160bfa83a9eSThomas Graf enum {
161bfa83a9eSThomas Graf 	NLA_UNSPEC,
162bfa83a9eSThomas Graf 	NLA_U8,
163bfa83a9eSThomas Graf 	NLA_U16,
164bfa83a9eSThomas Graf 	NLA_U32,
165bfa83a9eSThomas Graf 	NLA_U64,
166bfa83a9eSThomas Graf 	NLA_STRING,
167bfa83a9eSThomas Graf 	NLA_FLAG,
168bfa83a9eSThomas Graf 	NLA_MSECS,
169bfa83a9eSThomas Graf 	NLA_NESTED,
170a5531a5dSThomas Graf 	NLA_NUL_STRING,
171bfa83a9eSThomas Graf 	__NLA_TYPE_MAX,
172bfa83a9eSThomas Graf };
173bfa83a9eSThomas Graf 
174bfa83a9eSThomas Graf #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
175bfa83a9eSThomas Graf 
176bfa83a9eSThomas Graf /**
177bfa83a9eSThomas Graf  * struct nla_policy - attribute validation policy
178bfa83a9eSThomas Graf  * @type: Type of attribute or NLA_UNSPEC
179a5531a5dSThomas Graf  * @len: Type specific length of payload
180bfa83a9eSThomas Graf  *
181bfa83a9eSThomas Graf  * Policies are defined as arrays of this struct, the array must be
182bfa83a9eSThomas Graf  * accessible by attribute type up to the highest identifier to be expected.
183bfa83a9eSThomas Graf  *
184a5531a5dSThomas Graf  * Meaning of `len' field:
185a5531a5dSThomas Graf  *    NLA_STRING           Maximum length of string
186a5531a5dSThomas Graf  *    NLA_NUL_STRING       Maximum length of string (excluding NUL)
187a5531a5dSThomas Graf  *    NLA_FLAG             Unused
188a5531a5dSThomas Graf  *    All other            Exact length of attribute payload
189a5531a5dSThomas Graf  *
190bfa83a9eSThomas Graf  * Example:
191bfa83a9eSThomas Graf  * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = {
192bfa83a9eSThomas Graf  * 	[ATTR_FOO] = { .type = NLA_U16 },
193a5531a5dSThomas Graf  *	[ATTR_BAR] = { .type = NLA_STRING, len = BARSIZ },
194a5531a5dSThomas Graf  *	[ATTR_BAZ] = { .len = sizeof(struct mystruct) },
195bfa83a9eSThomas Graf  * };
196bfa83a9eSThomas Graf  */
197bfa83a9eSThomas Graf struct nla_policy {
198bfa83a9eSThomas Graf 	u16		type;
199a5531a5dSThomas Graf 	u16		len;
200bfa83a9eSThomas Graf };
201bfa83a9eSThomas Graf 
2024e902c57SThomas Graf /**
2034e902c57SThomas Graf  * struct nl_info - netlink source information
2044e902c57SThomas Graf  * @nlh: Netlink message header of original request
2054e902c57SThomas Graf  * @pid: Netlink PID of requesting application
2064e902c57SThomas Graf  */
2074e902c57SThomas Graf struct nl_info {
2084e902c57SThomas Graf 	struct nlmsghdr		*nlh;
2094e902c57SThomas Graf 	u32			pid;
2104e902c57SThomas Graf };
2114e902c57SThomas Graf 
21282ace47aSThomas Graf extern void		netlink_run_queue(struct sock *sk, unsigned int *qlen,
21382ace47aSThomas Graf 					  int (*cb)(struct sk_buff *,
21482ace47aSThomas Graf 						    struct nlmsghdr *, int *));
21582ace47aSThomas Graf extern void		netlink_queue_skip(struct nlmsghdr *nlh,
21682ace47aSThomas Graf 					   struct sk_buff *skb);
21797676b6bSThomas Graf extern int		nlmsg_notify(struct sock *sk, struct sk_buff *skb,
21897676b6bSThomas Graf 				     u32 pid, unsigned int group, int report,
21997676b6bSThomas Graf 				     gfp_t flags);
22082ace47aSThomas Graf 
221bfa83a9eSThomas Graf extern int		nla_validate(struct nlattr *head, int len, int maxtype,
222bfa83a9eSThomas Graf 				     struct nla_policy *policy);
223bfa83a9eSThomas Graf extern int		nla_parse(struct nlattr *tb[], int maxtype,
224bfa83a9eSThomas Graf 				  struct nlattr *head, int len,
225bfa83a9eSThomas Graf 				  struct nla_policy *policy);
226bfa83a9eSThomas Graf extern struct nlattr *	nla_find(struct nlattr *head, int len, int attrtype);
227bfa83a9eSThomas Graf extern size_t		nla_strlcpy(char *dst, const struct nlattr *nla,
228bfa83a9eSThomas Graf 				    size_t dstsize);
229bfa83a9eSThomas Graf extern int		nla_memcpy(void *dest, struct nlattr *src, int count);
230bfa83a9eSThomas Graf extern int		nla_memcmp(const struct nlattr *nla, const void *data,
231bfa83a9eSThomas Graf 				   size_t size);
232bfa83a9eSThomas Graf extern int		nla_strcmp(const struct nlattr *nla, const char *str);
233bfa83a9eSThomas Graf extern struct nlattr *	__nla_reserve(struct sk_buff *skb, int attrtype,
234bfa83a9eSThomas Graf 				      int attrlen);
235fe4944e5SThomas Graf extern void *		__nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
236bfa83a9eSThomas Graf extern struct nlattr *	nla_reserve(struct sk_buff *skb, int attrtype,
237bfa83a9eSThomas Graf 				    int attrlen);
238fe4944e5SThomas Graf extern void *		nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
239bfa83a9eSThomas Graf extern void		__nla_put(struct sk_buff *skb, int attrtype,
240bfa83a9eSThomas Graf 				  int attrlen, const void *data);
241fe4944e5SThomas Graf extern void		__nla_put_nohdr(struct sk_buff *skb, int attrlen,
242fe4944e5SThomas Graf 					const void *data);
243bfa83a9eSThomas Graf extern int		nla_put(struct sk_buff *skb, int attrtype,
244bfa83a9eSThomas Graf 				int attrlen, const void *data);
245fe4944e5SThomas Graf extern int		nla_put_nohdr(struct sk_buff *skb, int attrlen,
246fe4944e5SThomas Graf 				      const void *data);
247bfa83a9eSThomas Graf 
248bfa83a9eSThomas Graf /**************************************************************************
249bfa83a9eSThomas Graf  * Netlink Messages
250bfa83a9eSThomas Graf  **************************************************************************/
251bfa83a9eSThomas Graf 
252bfa83a9eSThomas Graf /**
253bfa83a9eSThomas Graf  * nlmsg_msg_size - length of netlink message not including padding
254bfa83a9eSThomas Graf  * @payload: length of message payload
255bfa83a9eSThomas Graf  */
256bfa83a9eSThomas Graf static inline int nlmsg_msg_size(int payload)
257bfa83a9eSThomas Graf {
258bfa83a9eSThomas Graf 	return NLMSG_HDRLEN + payload;
259bfa83a9eSThomas Graf }
260bfa83a9eSThomas Graf 
261bfa83a9eSThomas Graf /**
262bfa83a9eSThomas Graf  * nlmsg_total_size - length of netlink message including padding
263bfa83a9eSThomas Graf  * @payload: length of message payload
264bfa83a9eSThomas Graf  */
265bfa83a9eSThomas Graf static inline int nlmsg_total_size(int payload)
266bfa83a9eSThomas Graf {
267bfa83a9eSThomas Graf 	return NLMSG_ALIGN(nlmsg_msg_size(payload));
268bfa83a9eSThomas Graf }
269bfa83a9eSThomas Graf 
270bfa83a9eSThomas Graf /**
271bfa83a9eSThomas Graf  * nlmsg_padlen - length of padding at the message's tail
272bfa83a9eSThomas Graf  * @payload: length of message payload
273bfa83a9eSThomas Graf  */
274bfa83a9eSThomas Graf static inline int nlmsg_padlen(int payload)
275bfa83a9eSThomas Graf {
276bfa83a9eSThomas Graf 	return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
277bfa83a9eSThomas Graf }
278bfa83a9eSThomas Graf 
279bfa83a9eSThomas Graf /**
280bfa83a9eSThomas Graf  * nlmsg_data - head of message payload
281bfa83a9eSThomas Graf  * @nlh: netlink messsage header
282bfa83a9eSThomas Graf  */
283bfa83a9eSThomas Graf static inline void *nlmsg_data(const struct nlmsghdr *nlh)
284bfa83a9eSThomas Graf {
285bfa83a9eSThomas Graf 	return (unsigned char *) nlh + NLMSG_HDRLEN;
286bfa83a9eSThomas Graf }
287bfa83a9eSThomas Graf 
288bfa83a9eSThomas Graf /**
289bfa83a9eSThomas Graf  * nlmsg_len - length of message payload
290bfa83a9eSThomas Graf  * @nlh: netlink message header
291bfa83a9eSThomas Graf  */
292bfa83a9eSThomas Graf static inline int nlmsg_len(const struct nlmsghdr *nlh)
293bfa83a9eSThomas Graf {
294bfa83a9eSThomas Graf 	return nlh->nlmsg_len - NLMSG_HDRLEN;
295bfa83a9eSThomas Graf }
296bfa83a9eSThomas Graf 
297bfa83a9eSThomas Graf /**
298bfa83a9eSThomas Graf  * nlmsg_attrdata - head of attributes data
299bfa83a9eSThomas Graf  * @nlh: netlink message header
300bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
301bfa83a9eSThomas Graf  */
302bfa83a9eSThomas Graf static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
303bfa83a9eSThomas Graf 					    int hdrlen)
304bfa83a9eSThomas Graf {
305bfa83a9eSThomas Graf 	unsigned char *data = nlmsg_data(nlh);
306bfa83a9eSThomas Graf 	return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
307bfa83a9eSThomas Graf }
308bfa83a9eSThomas Graf 
309bfa83a9eSThomas Graf /**
310bfa83a9eSThomas Graf  * nlmsg_attrlen - length of attributes data
311bfa83a9eSThomas Graf  * @nlh: netlink message header
312bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
313bfa83a9eSThomas Graf  */
314bfa83a9eSThomas Graf static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
315bfa83a9eSThomas Graf {
316bfa83a9eSThomas Graf 	return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
317bfa83a9eSThomas Graf }
318bfa83a9eSThomas Graf 
319bfa83a9eSThomas Graf /**
320bfa83a9eSThomas Graf  * nlmsg_ok - check if the netlink message fits into the remaining bytes
321bfa83a9eSThomas Graf  * @nlh: netlink message header
322bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in message stream
323bfa83a9eSThomas Graf  */
324bfa83a9eSThomas Graf static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
325bfa83a9eSThomas Graf {
326bfa83a9eSThomas Graf 	return (remaining >= sizeof(struct nlmsghdr) &&
327bfa83a9eSThomas Graf 		nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
328bfa83a9eSThomas Graf 		nlh->nlmsg_len <= remaining);
329bfa83a9eSThomas Graf }
330bfa83a9eSThomas Graf 
331bfa83a9eSThomas Graf /**
332bfa83a9eSThomas Graf  * nlmsg_next - next netlink message in message stream
333bfa83a9eSThomas Graf  * @nlh: netlink message header
334bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in message stream
335bfa83a9eSThomas Graf  *
336bfa83a9eSThomas Graf  * Returns the next netlink message in the message stream and
337bfa83a9eSThomas Graf  * decrements remaining by the size of the current message.
338bfa83a9eSThomas Graf  */
339bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
340bfa83a9eSThomas Graf {
341bfa83a9eSThomas Graf 	int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
342bfa83a9eSThomas Graf 
343bfa83a9eSThomas Graf 	*remaining -= totlen;
344bfa83a9eSThomas Graf 
345bfa83a9eSThomas Graf 	return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
346bfa83a9eSThomas Graf }
347bfa83a9eSThomas Graf 
348bfa83a9eSThomas Graf /**
349bfa83a9eSThomas Graf  * nlmsg_parse - parse attributes of a netlink message
350bfa83a9eSThomas Graf  * @nlh: netlink message header
351bfa83a9eSThomas Graf  * @hdrlen: length of family specific header
352bfa83a9eSThomas Graf  * @tb: destination array with maxtype+1 elements
353bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
354bfa83a9eSThomas Graf  * @policy: validation policy
355bfa83a9eSThomas Graf  *
356bfa83a9eSThomas Graf  * See nla_parse()
357bfa83a9eSThomas Graf  */
358bfa83a9eSThomas Graf static inline int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen,
359bfa83a9eSThomas Graf 			      struct nlattr *tb[], int maxtype,
360bfa83a9eSThomas Graf 			      struct nla_policy *policy)
361bfa83a9eSThomas Graf {
362bfa83a9eSThomas Graf 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
363bfa83a9eSThomas Graf 		return -EINVAL;
364bfa83a9eSThomas Graf 
365bfa83a9eSThomas Graf 	return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
366bfa83a9eSThomas Graf 			 nlmsg_attrlen(nlh, hdrlen), policy);
367bfa83a9eSThomas Graf }
368bfa83a9eSThomas Graf 
369bfa83a9eSThomas Graf /**
370bfa83a9eSThomas Graf  * nlmsg_find_attr - find a specific attribute in a netlink message
371bfa83a9eSThomas Graf  * @nlh: netlink message header
372bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
373bfa83a9eSThomas Graf  * @attrtype: type of attribute to look for
374bfa83a9eSThomas Graf  *
375bfa83a9eSThomas Graf  * Returns the first attribute which matches the specified type.
376bfa83a9eSThomas Graf  */
377bfa83a9eSThomas Graf static inline struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh,
378bfa83a9eSThomas Graf 					     int hdrlen, int attrtype)
379bfa83a9eSThomas Graf {
380bfa83a9eSThomas Graf 	return nla_find(nlmsg_attrdata(nlh, hdrlen),
381bfa83a9eSThomas Graf 			nlmsg_attrlen(nlh, hdrlen), attrtype);
382bfa83a9eSThomas Graf }
383bfa83a9eSThomas Graf 
384bfa83a9eSThomas Graf /**
385bfa83a9eSThomas Graf  * nlmsg_validate - validate a netlink message including attributes
386bfa83a9eSThomas Graf  * @nlh: netlinket message header
387bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
388bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
389bfa83a9eSThomas Graf  * @policy: validation policy
390bfa83a9eSThomas Graf  */
391bfa83a9eSThomas Graf static inline int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
392bfa83a9eSThomas Graf 				 struct nla_policy *policy)
393bfa83a9eSThomas Graf {
394bfa83a9eSThomas Graf 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
395bfa83a9eSThomas Graf 		return -EINVAL;
396bfa83a9eSThomas Graf 
397bfa83a9eSThomas Graf 	return nla_validate(nlmsg_attrdata(nlh, hdrlen),
398bfa83a9eSThomas Graf 			    nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
399bfa83a9eSThomas Graf }
400bfa83a9eSThomas Graf 
401bfa83a9eSThomas Graf /**
40297676b6bSThomas Graf  * nlmsg_report - need to report back to application?
40397676b6bSThomas Graf  * @nlh: netlink message header
40497676b6bSThomas Graf  *
40597676b6bSThomas Graf  * Returns 1 if a report back to the application is requested.
40697676b6bSThomas Graf  */
40797676b6bSThomas Graf static inline int nlmsg_report(struct nlmsghdr *nlh)
40897676b6bSThomas Graf {
40997676b6bSThomas Graf 	return !!(nlh->nlmsg_flags & NLM_F_ECHO);
41097676b6bSThomas Graf }
41197676b6bSThomas Graf 
41297676b6bSThomas Graf /**
413bfa83a9eSThomas Graf  * nlmsg_for_each_attr - iterate over a stream of attributes
414bfa83a9eSThomas Graf  * @pos: loop counter, set to current attribute
415bfa83a9eSThomas Graf  * @nlh: netlink message header
416bfa83a9eSThomas Graf  * @hdrlen: length of familiy specific header
417bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
418bfa83a9eSThomas Graf  */
419bfa83a9eSThomas Graf #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
420bfa83a9eSThomas Graf 	nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
421bfa83a9eSThomas Graf 			  nlmsg_attrlen(nlh, hdrlen), rem)
422bfa83a9eSThomas Graf 
423bfa83a9eSThomas Graf #if 0
424bfa83a9eSThomas Graf /* FIXME: Enable once all users have been converted */
425bfa83a9eSThomas Graf 
426bfa83a9eSThomas Graf /**
427bfa83a9eSThomas Graf  * __nlmsg_put - Add a new netlink message to an skb
428bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
429bfa83a9eSThomas Graf  * @pid: netlink process id
430bfa83a9eSThomas Graf  * @seq: sequence number of message
431bfa83a9eSThomas Graf  * @type: message type
432bfa83a9eSThomas Graf  * @payload: length of message payload
433bfa83a9eSThomas Graf  * @flags: message flags
434bfa83a9eSThomas Graf  *
435bfa83a9eSThomas Graf  * The caller is responsible to ensure that the skb provides enough
436bfa83a9eSThomas Graf  * tailroom for both the netlink header and payload.
437bfa83a9eSThomas Graf  */
438bfa83a9eSThomas Graf static inline struct nlmsghdr *__nlmsg_put(struct sk_buff *skb, u32 pid,
439bfa83a9eSThomas Graf 					   u32 seq, int type, int payload,
440bfa83a9eSThomas Graf 					   int flags)
441bfa83a9eSThomas Graf {
442bfa83a9eSThomas Graf 	struct nlmsghdr *nlh;
443bfa83a9eSThomas Graf 
444bfa83a9eSThomas Graf 	nlh = (struct nlmsghdr *) skb_put(skb, nlmsg_total_size(payload));
445bfa83a9eSThomas Graf 	nlh->nlmsg_type = type;
446bfa83a9eSThomas Graf 	nlh->nlmsg_len = nlmsg_msg_size(payload);
447bfa83a9eSThomas Graf 	nlh->nlmsg_flags = flags;
448bfa83a9eSThomas Graf 	nlh->nlmsg_pid = pid;
449bfa83a9eSThomas Graf 	nlh->nlmsg_seq = seq;
450bfa83a9eSThomas Graf 
451bfa83a9eSThomas Graf 	memset((unsigned char *) nlmsg_data(nlh) + payload, 0,
452bfa83a9eSThomas Graf 	       nlmsg_padlen(payload));
453bfa83a9eSThomas Graf 
454bfa83a9eSThomas Graf 	return nlh;
455bfa83a9eSThomas Graf }
456bfa83a9eSThomas Graf #endif
457bfa83a9eSThomas Graf 
458bfa83a9eSThomas Graf /**
459bfa83a9eSThomas Graf  * nlmsg_put - Add a new netlink message to an skb
460bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
461bfa83a9eSThomas Graf  * @pid: netlink process id
462bfa83a9eSThomas Graf  * @seq: sequence number of message
463bfa83a9eSThomas Graf  * @type: message type
464bfa83a9eSThomas Graf  * @payload: length of message payload
465bfa83a9eSThomas Graf  * @flags: message flags
466bfa83a9eSThomas Graf  *
467bfa83a9eSThomas Graf  * Returns NULL if the tailroom of the skb is insufficient to store
468bfa83a9eSThomas Graf  * the message header and payload.
469bfa83a9eSThomas Graf  */
470bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
471bfa83a9eSThomas Graf 					 int type, int payload, int flags)
472bfa83a9eSThomas Graf {
473bfa83a9eSThomas Graf 	if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
474bfa83a9eSThomas Graf 		return NULL;
475bfa83a9eSThomas Graf 
476bfa83a9eSThomas Graf 	return __nlmsg_put(skb, pid, seq, type, payload, flags);
477bfa83a9eSThomas Graf }
478bfa83a9eSThomas Graf 
479bfa83a9eSThomas Graf /**
480bfa83a9eSThomas Graf  * nlmsg_put_answer - Add a new callback based netlink message to an skb
481bfa83a9eSThomas Graf  * @skb: socket buffer to store message in
482bfa83a9eSThomas Graf  * @cb: netlink callback
483bfa83a9eSThomas Graf  * @type: message type
484bfa83a9eSThomas Graf  * @payload: length of message payload
485bfa83a9eSThomas Graf  * @flags: message flags
486bfa83a9eSThomas Graf  *
487bfa83a9eSThomas Graf  * Returns NULL if the tailroom of the skb is insufficient to store
488bfa83a9eSThomas Graf  * the message header and payload.
489bfa83a9eSThomas Graf  */
490bfa83a9eSThomas Graf static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
491bfa83a9eSThomas Graf 						struct netlink_callback *cb,
492bfa83a9eSThomas Graf 						int type, int payload,
493bfa83a9eSThomas Graf 						int flags)
494bfa83a9eSThomas Graf {
495bfa83a9eSThomas Graf 	return nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
496bfa83a9eSThomas Graf 			 type, payload, flags);
497bfa83a9eSThomas Graf }
498bfa83a9eSThomas Graf 
499bfa83a9eSThomas Graf /**
500bfa83a9eSThomas Graf  * nlmsg_new - Allocate a new netlink message
501bfa83a9eSThomas Graf  * @size: maximum size of message
502fe4944e5SThomas Graf  * @flags: the type of memory to allocate.
503bfa83a9eSThomas Graf  *
504bfa83a9eSThomas Graf  * Use NLMSG_GOODSIZE if size isn't know and you need a good default size.
505bfa83a9eSThomas Graf  */
506fe4944e5SThomas Graf static inline struct sk_buff *nlmsg_new(int size, gfp_t flags)
507bfa83a9eSThomas Graf {
508fe4944e5SThomas Graf 	return alloc_skb(size, flags);
509bfa83a9eSThomas Graf }
510bfa83a9eSThomas Graf 
511bfa83a9eSThomas Graf /**
512bfa83a9eSThomas Graf  * nlmsg_end - Finalize a netlink message
513bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
514bfa83a9eSThomas Graf  * @nlh: netlink message header
515bfa83a9eSThomas Graf  *
516bfa83a9eSThomas Graf  * Corrects the netlink message header to include the appeneded
517bfa83a9eSThomas Graf  * attributes. Only necessary if attributes have been added to
518bfa83a9eSThomas Graf  * the message.
519bfa83a9eSThomas Graf  *
520bfa83a9eSThomas Graf  * Returns the total data length of the skb.
521bfa83a9eSThomas Graf  */
522bfa83a9eSThomas Graf static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
523bfa83a9eSThomas Graf {
524bfa83a9eSThomas Graf 	nlh->nlmsg_len = skb->tail - (unsigned char *) nlh;
525bfa83a9eSThomas Graf 
526bfa83a9eSThomas Graf 	return skb->len;
527bfa83a9eSThomas Graf }
528bfa83a9eSThomas Graf 
529bfa83a9eSThomas Graf /**
530fe4944e5SThomas Graf  * nlmsg_get_pos - return current position in netlink message
531fe4944e5SThomas Graf  * @skb: socket buffer the message is stored in
532fe4944e5SThomas Graf  *
533fe4944e5SThomas Graf  * Returns a pointer to the current tail of the message.
534fe4944e5SThomas Graf  */
535fe4944e5SThomas Graf static inline void *nlmsg_get_pos(struct sk_buff *skb)
536fe4944e5SThomas Graf {
537fe4944e5SThomas Graf 	return skb->tail;
538fe4944e5SThomas Graf }
539fe4944e5SThomas Graf 
540fe4944e5SThomas Graf /**
541fe4944e5SThomas Graf  * nlmsg_trim - Trim message to a mark
542fe4944e5SThomas Graf  * @skb: socket buffer the message is stored in
543fe4944e5SThomas Graf  * @mark: mark to trim to
544fe4944e5SThomas Graf  *
545fe4944e5SThomas Graf  * Trims the message to the provided mark. Returns -1.
546fe4944e5SThomas Graf  */
547fe4944e5SThomas Graf static inline int nlmsg_trim(struct sk_buff *skb, void *mark)
548fe4944e5SThomas Graf {
549fe4944e5SThomas Graf 	if (mark)
550fe4944e5SThomas Graf 		skb_trim(skb, (unsigned char *) mark - skb->data);
551fe4944e5SThomas Graf 
552fe4944e5SThomas Graf 	return -1;
553fe4944e5SThomas Graf }
554fe4944e5SThomas Graf 
555fe4944e5SThomas Graf /**
556bfa83a9eSThomas Graf  * nlmsg_cancel - Cancel construction of a netlink message
557bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
558bfa83a9eSThomas Graf  * @nlh: netlink message header
559bfa83a9eSThomas Graf  *
560bfa83a9eSThomas Graf  * Removes the complete netlink message including all
561bfa83a9eSThomas Graf  * attributes from the socket buffer again. Returns -1.
562bfa83a9eSThomas Graf  */
563bfa83a9eSThomas Graf static inline int nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
564bfa83a9eSThomas Graf {
565fe4944e5SThomas Graf 	return nlmsg_trim(skb, nlh);
566bfa83a9eSThomas Graf }
567bfa83a9eSThomas Graf 
568bfa83a9eSThomas Graf /**
569bfa83a9eSThomas Graf  * nlmsg_free - free a netlink message
570bfa83a9eSThomas Graf  * @skb: socket buffer of netlink message
571bfa83a9eSThomas Graf  */
572bfa83a9eSThomas Graf static inline void nlmsg_free(struct sk_buff *skb)
573bfa83a9eSThomas Graf {
574bfa83a9eSThomas Graf 	kfree_skb(skb);
575bfa83a9eSThomas Graf }
576bfa83a9eSThomas Graf 
577bfa83a9eSThomas Graf /**
578bfa83a9eSThomas Graf  * nlmsg_multicast - multicast a netlink message
579bfa83a9eSThomas Graf  * @sk: netlink socket to spread messages to
580bfa83a9eSThomas Graf  * @skb: netlink message as socket buffer
581bfa83a9eSThomas Graf  * @pid: own netlink pid to avoid sending to yourself
582bfa83a9eSThomas Graf  * @group: multicast group id
583d387f6adSThomas Graf  * @flags: allocation flags
584bfa83a9eSThomas Graf  */
585bfa83a9eSThomas Graf static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
586d387f6adSThomas Graf 				  u32 pid, unsigned int group, gfp_t flags)
587bfa83a9eSThomas Graf {
588bfa83a9eSThomas Graf 	int err;
589bfa83a9eSThomas Graf 
590bfa83a9eSThomas Graf 	NETLINK_CB(skb).dst_group = group;
591bfa83a9eSThomas Graf 
592d387f6adSThomas Graf 	err = netlink_broadcast(sk, skb, pid, group, flags);
593bfa83a9eSThomas Graf 	if (err > 0)
594bfa83a9eSThomas Graf 		err = 0;
595bfa83a9eSThomas Graf 
596bfa83a9eSThomas Graf 	return err;
597bfa83a9eSThomas Graf }
598bfa83a9eSThomas Graf 
599bfa83a9eSThomas Graf /**
600bfa83a9eSThomas Graf  * nlmsg_unicast - unicast a netlink message
601bfa83a9eSThomas Graf  * @sk: netlink socket to spread message to
602bfa83a9eSThomas Graf  * @skb: netlink message as socket buffer
603bfa83a9eSThomas Graf  * @pid: netlink pid of the destination socket
604bfa83a9eSThomas Graf  */
605bfa83a9eSThomas Graf static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 pid)
606bfa83a9eSThomas Graf {
607bfa83a9eSThomas Graf 	int err;
608bfa83a9eSThomas Graf 
609bfa83a9eSThomas Graf 	err = netlink_unicast(sk, skb, pid, MSG_DONTWAIT);
610bfa83a9eSThomas Graf 	if (err > 0)
611bfa83a9eSThomas Graf 		err = 0;
612bfa83a9eSThomas Graf 
613bfa83a9eSThomas Graf 	return err;
614bfa83a9eSThomas Graf }
615bfa83a9eSThomas Graf 
616bfa83a9eSThomas Graf /**
617bfa83a9eSThomas Graf  * nlmsg_for_each_msg - iterate over a stream of messages
618bfa83a9eSThomas Graf  * @pos: loop counter, set to current message
619bfa83a9eSThomas Graf  * @head: head of message stream
620bfa83a9eSThomas Graf  * @len: length of message stream
621bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
622bfa83a9eSThomas Graf  */
623bfa83a9eSThomas Graf #define nlmsg_for_each_msg(pos, head, len, rem) \
624bfa83a9eSThomas Graf 	for (pos = head, rem = len; \
625bfa83a9eSThomas Graf 	     nlmsg_ok(pos, rem); \
626bfa83a9eSThomas Graf 	     pos = nlmsg_next(pos, &(rem)))
627bfa83a9eSThomas Graf 
628bfa83a9eSThomas Graf /**************************************************************************
629bfa83a9eSThomas Graf  * Netlink Attributes
630bfa83a9eSThomas Graf  **************************************************************************/
631bfa83a9eSThomas Graf 
632bfa83a9eSThomas Graf /**
633bfa83a9eSThomas Graf  * nla_attr_size - length of attribute not including padding
634bfa83a9eSThomas Graf  * @payload: length of payload
635bfa83a9eSThomas Graf  */
636bfa83a9eSThomas Graf static inline int nla_attr_size(int payload)
637bfa83a9eSThomas Graf {
638bfa83a9eSThomas Graf 	return NLA_HDRLEN + payload;
639bfa83a9eSThomas Graf }
640bfa83a9eSThomas Graf 
641bfa83a9eSThomas Graf /**
642bfa83a9eSThomas Graf  * nla_total_size - total length of attribute including padding
643bfa83a9eSThomas Graf  * @payload: length of payload
644bfa83a9eSThomas Graf  */
645bfa83a9eSThomas Graf static inline int nla_total_size(int payload)
646bfa83a9eSThomas Graf {
647bfa83a9eSThomas Graf 	return NLA_ALIGN(nla_attr_size(payload));
648bfa83a9eSThomas Graf }
649bfa83a9eSThomas Graf 
650bfa83a9eSThomas Graf /**
651bfa83a9eSThomas Graf  * nla_padlen - length of padding at the tail of attribute
652bfa83a9eSThomas Graf  * @payload: length of payload
653bfa83a9eSThomas Graf  */
654bfa83a9eSThomas Graf static inline int nla_padlen(int payload)
655bfa83a9eSThomas Graf {
656bfa83a9eSThomas Graf 	return nla_total_size(payload) - nla_attr_size(payload);
657bfa83a9eSThomas Graf }
658bfa83a9eSThomas Graf 
659bfa83a9eSThomas Graf /**
660bfa83a9eSThomas Graf  * nla_data - head of payload
661bfa83a9eSThomas Graf  * @nla: netlink attribute
662bfa83a9eSThomas Graf  */
663bfa83a9eSThomas Graf static inline void *nla_data(const struct nlattr *nla)
664bfa83a9eSThomas Graf {
665bfa83a9eSThomas Graf 	return (char *) nla + NLA_HDRLEN;
666bfa83a9eSThomas Graf }
667bfa83a9eSThomas Graf 
668bfa83a9eSThomas Graf /**
669bfa83a9eSThomas Graf  * nla_len - length of payload
670bfa83a9eSThomas Graf  * @nla: netlink attribute
671bfa83a9eSThomas Graf  */
672bfa83a9eSThomas Graf static inline int nla_len(const struct nlattr *nla)
673bfa83a9eSThomas Graf {
674bfa83a9eSThomas Graf 	return nla->nla_len - NLA_HDRLEN;
675bfa83a9eSThomas Graf }
676bfa83a9eSThomas Graf 
677bfa83a9eSThomas Graf /**
678bfa83a9eSThomas Graf  * nla_ok - check if the netlink attribute fits into the remaining bytes
679bfa83a9eSThomas Graf  * @nla: netlink attribute
680bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in attribute stream
681bfa83a9eSThomas Graf  */
682bfa83a9eSThomas Graf static inline int nla_ok(const struct nlattr *nla, int remaining)
683bfa83a9eSThomas Graf {
684bfa83a9eSThomas Graf 	return remaining >= sizeof(*nla) &&
685bfa83a9eSThomas Graf 	       nla->nla_len >= sizeof(*nla) &&
686bfa83a9eSThomas Graf 	       nla->nla_len <= remaining;
687bfa83a9eSThomas Graf }
688bfa83a9eSThomas Graf 
689bfa83a9eSThomas Graf /**
690bfa83a9eSThomas Graf  * nla_next - next netlink attribte in attribute stream
691bfa83a9eSThomas Graf  * @nla: netlink attribute
692bfa83a9eSThomas Graf  * @remaining: number of bytes remaining in attribute stream
693bfa83a9eSThomas Graf  *
694bfa83a9eSThomas Graf  * Returns the next netlink attribute in the attribute stream and
695bfa83a9eSThomas Graf  * decrements remaining by the size of the current attribute.
696bfa83a9eSThomas Graf  */
697bfa83a9eSThomas Graf static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
698bfa83a9eSThomas Graf {
699bfa83a9eSThomas Graf 	int totlen = NLA_ALIGN(nla->nla_len);
700bfa83a9eSThomas Graf 
701bfa83a9eSThomas Graf 	*remaining -= totlen;
702bfa83a9eSThomas Graf 	return (struct nlattr *) ((char *) nla + totlen);
703bfa83a9eSThomas Graf }
704bfa83a9eSThomas Graf 
705bfa83a9eSThomas Graf /**
706fe4944e5SThomas Graf  * nla_find_nested - find attribute in a set of nested attributes
707fe4944e5SThomas Graf  * @nla: attribute containing the nested attributes
708fe4944e5SThomas Graf  * @attrtype: type of attribute to look for
709fe4944e5SThomas Graf  *
710fe4944e5SThomas Graf  * Returns the first attribute which matches the specified type.
711fe4944e5SThomas Graf  */
712fe4944e5SThomas Graf static inline struct nlattr *nla_find_nested(struct nlattr *nla, int attrtype)
713fe4944e5SThomas Graf {
714fe4944e5SThomas Graf 	return nla_find(nla_data(nla), nla_len(nla), attrtype);
715fe4944e5SThomas Graf }
716fe4944e5SThomas Graf 
717fe4944e5SThomas Graf /**
718bfa83a9eSThomas Graf  * nla_parse_nested - parse nested attributes
719bfa83a9eSThomas Graf  * @tb: destination array with maxtype+1 elements
720bfa83a9eSThomas Graf  * @maxtype: maximum attribute type to be expected
721bfa83a9eSThomas Graf  * @nla: attribute containing the nested attributes
722bfa83a9eSThomas Graf  * @policy: validation policy
723bfa83a9eSThomas Graf  *
724bfa83a9eSThomas Graf  * See nla_parse()
725bfa83a9eSThomas Graf  */
726bfa83a9eSThomas Graf static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
727bfa83a9eSThomas Graf 				   struct nlattr *nla,
728bfa83a9eSThomas Graf 				   struct nla_policy *policy)
729bfa83a9eSThomas Graf {
730bfa83a9eSThomas Graf 	return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
731bfa83a9eSThomas Graf }
732bfa83a9eSThomas Graf /**
733bfa83a9eSThomas Graf  * nla_put_u8 - Add a u16 netlink attribute to a socket buffer
734bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
735bfa83a9eSThomas Graf  * @attrtype: attribute type
736bfa83a9eSThomas Graf  * @value: numeric value
737bfa83a9eSThomas Graf  */
738bfa83a9eSThomas Graf static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
739bfa83a9eSThomas Graf {
740bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, sizeof(u8), &value);
741bfa83a9eSThomas Graf }
742bfa83a9eSThomas Graf 
743bfa83a9eSThomas Graf /**
744bfa83a9eSThomas Graf  * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
745bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
746bfa83a9eSThomas Graf  * @attrtype: attribute type
747bfa83a9eSThomas Graf  * @value: numeric value
748bfa83a9eSThomas Graf  */
749bfa83a9eSThomas Graf static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
750bfa83a9eSThomas Graf {
751bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, sizeof(u16), &value);
752bfa83a9eSThomas Graf }
753bfa83a9eSThomas Graf 
754bfa83a9eSThomas Graf /**
755bfa83a9eSThomas Graf  * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
756bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
757bfa83a9eSThomas Graf  * @attrtype: attribute type
758bfa83a9eSThomas Graf  * @value: numeric value
759bfa83a9eSThomas Graf  */
760bfa83a9eSThomas Graf static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
761bfa83a9eSThomas Graf {
762bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, sizeof(u32), &value);
763bfa83a9eSThomas Graf }
764bfa83a9eSThomas Graf 
765bfa83a9eSThomas Graf /**
766bfa83a9eSThomas Graf  * nla_put_64 - Add a u64 netlink attribute to a socket buffer
767bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
768bfa83a9eSThomas Graf  * @attrtype: attribute type
769bfa83a9eSThomas Graf  * @value: numeric value
770bfa83a9eSThomas Graf  */
771bfa83a9eSThomas Graf static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value)
772bfa83a9eSThomas Graf {
773bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, sizeof(u64), &value);
774bfa83a9eSThomas Graf }
775bfa83a9eSThomas Graf 
776bfa83a9eSThomas Graf /**
777bfa83a9eSThomas Graf  * nla_put_string - Add a string netlink attribute to a socket buffer
778bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
779bfa83a9eSThomas Graf  * @attrtype: attribute type
780bfa83a9eSThomas Graf  * @str: NUL terminated string
781bfa83a9eSThomas Graf  */
782bfa83a9eSThomas Graf static inline int nla_put_string(struct sk_buff *skb, int attrtype,
783bfa83a9eSThomas Graf 				 const char *str)
784bfa83a9eSThomas Graf {
785bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, strlen(str) + 1, str);
786bfa83a9eSThomas Graf }
787bfa83a9eSThomas Graf 
788bfa83a9eSThomas Graf /**
789bfa83a9eSThomas Graf  * nla_put_flag - Add a flag netlink attribute to a socket buffer
790bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
791bfa83a9eSThomas Graf  * @attrtype: attribute type
792bfa83a9eSThomas Graf  */
793bfa83a9eSThomas Graf static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
794bfa83a9eSThomas Graf {
795bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, 0, NULL);
796bfa83a9eSThomas Graf }
797bfa83a9eSThomas Graf 
798bfa83a9eSThomas Graf /**
799bfa83a9eSThomas Graf  * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
800bfa83a9eSThomas Graf  * @skb: socket buffer to add attribute to
801bfa83a9eSThomas Graf  * @attrtype: attribute type
802bfa83a9eSThomas Graf  * @jiffies: number of msecs in jiffies
803bfa83a9eSThomas Graf  */
804bfa83a9eSThomas Graf static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
805bfa83a9eSThomas Graf 				unsigned long jiffies)
806bfa83a9eSThomas Graf {
807bfa83a9eSThomas Graf 	u64 tmp = jiffies_to_msecs(jiffies);
808bfa83a9eSThomas Graf 	return nla_put(skb, attrtype, sizeof(u64), &tmp);
809bfa83a9eSThomas Graf }
810bfa83a9eSThomas Graf 
811bfa83a9eSThomas Graf #define NLA_PUT(skb, attrtype, attrlen, data) \
812bfa83a9eSThomas Graf 	do { \
813bfa83a9eSThomas Graf 		if (nla_put(skb, attrtype, attrlen, data) < 0) \
814bfa83a9eSThomas Graf 			goto nla_put_failure; \
815bfa83a9eSThomas Graf 	} while(0)
816bfa83a9eSThomas Graf 
817bfa83a9eSThomas Graf #define NLA_PUT_TYPE(skb, type, attrtype, value) \
818bfa83a9eSThomas Graf 	do { \
819bfa83a9eSThomas Graf 		type __tmp = value; \
820bfa83a9eSThomas Graf 		NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \
821bfa83a9eSThomas Graf 	} while(0)
822bfa83a9eSThomas Graf 
823bfa83a9eSThomas Graf #define NLA_PUT_U8(skb, attrtype, value) \
824bfa83a9eSThomas Graf 	NLA_PUT_TYPE(skb, u8, attrtype, value)
825bfa83a9eSThomas Graf 
826bfa83a9eSThomas Graf #define NLA_PUT_U16(skb, attrtype, value) \
827bfa83a9eSThomas Graf 	NLA_PUT_TYPE(skb, u16, attrtype, value)
828bfa83a9eSThomas Graf 
829bfa83a9eSThomas Graf #define NLA_PUT_U32(skb, attrtype, value) \
830bfa83a9eSThomas Graf 	NLA_PUT_TYPE(skb, u32, attrtype, value)
831bfa83a9eSThomas Graf 
832bfa83a9eSThomas Graf #define NLA_PUT_U64(skb, attrtype, value) \
833bfa83a9eSThomas Graf 	NLA_PUT_TYPE(skb, u64, attrtype, value)
834bfa83a9eSThomas Graf 
835bfa83a9eSThomas Graf #define NLA_PUT_STRING(skb, attrtype, value) \
836bfa83a9eSThomas Graf 	NLA_PUT(skb, attrtype, strlen(value) + 1, value)
837bfa83a9eSThomas Graf 
838ff5dfe73SJohannes Berg #define NLA_PUT_FLAG(skb, attrtype) \
839bfa83a9eSThomas Graf 	NLA_PUT(skb, attrtype, 0, NULL)
840bfa83a9eSThomas Graf 
841bfa83a9eSThomas Graf #define NLA_PUT_MSECS(skb, attrtype, jiffies) \
842bfa83a9eSThomas Graf 	NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies))
843bfa83a9eSThomas Graf 
844bfa83a9eSThomas Graf /**
845bfa83a9eSThomas Graf  * nla_get_u32 - return payload of u32 attribute
846bfa83a9eSThomas Graf  * @nla: u32 netlink attribute
847bfa83a9eSThomas Graf  */
848bfa83a9eSThomas Graf static inline u32 nla_get_u32(struct nlattr *nla)
849bfa83a9eSThomas Graf {
850bfa83a9eSThomas Graf 	return *(u32 *) nla_data(nla);
851bfa83a9eSThomas Graf }
852bfa83a9eSThomas Graf 
853bfa83a9eSThomas Graf /**
854bfa83a9eSThomas Graf  * nla_get_u16 - return payload of u16 attribute
855bfa83a9eSThomas Graf  * @nla: u16 netlink attribute
856bfa83a9eSThomas Graf  */
857bfa83a9eSThomas Graf static inline u16 nla_get_u16(struct nlattr *nla)
858bfa83a9eSThomas Graf {
859bfa83a9eSThomas Graf 	return *(u16 *) nla_data(nla);
860bfa83a9eSThomas Graf }
861bfa83a9eSThomas Graf 
862bfa83a9eSThomas Graf /**
863bfa83a9eSThomas Graf  * nla_get_u8 - return payload of u8 attribute
864bfa83a9eSThomas Graf  * @nla: u8 netlink attribute
865bfa83a9eSThomas Graf  */
866bfa83a9eSThomas Graf static inline u8 nla_get_u8(struct nlattr *nla)
867bfa83a9eSThomas Graf {
868bfa83a9eSThomas Graf 	return *(u8 *) nla_data(nla);
869bfa83a9eSThomas Graf }
870bfa83a9eSThomas Graf 
871bfa83a9eSThomas Graf /**
872bfa83a9eSThomas Graf  * nla_get_u64 - return payload of u64 attribute
873bfa83a9eSThomas Graf  * @nla: u64 netlink attribute
874bfa83a9eSThomas Graf  */
875bfa83a9eSThomas Graf static inline u64 nla_get_u64(struct nlattr *nla)
876bfa83a9eSThomas Graf {
877bfa83a9eSThomas Graf 	u64 tmp;
878bfa83a9eSThomas Graf 
879bfa83a9eSThomas Graf 	nla_memcpy(&tmp, nla, sizeof(tmp));
880bfa83a9eSThomas Graf 
881bfa83a9eSThomas Graf 	return tmp;
882bfa83a9eSThomas Graf }
883bfa83a9eSThomas Graf 
884bfa83a9eSThomas Graf /**
885bfa83a9eSThomas Graf  * nla_get_flag - return payload of flag attribute
886bfa83a9eSThomas Graf  * @nla: flag netlink attribute
887bfa83a9eSThomas Graf  */
888bfa83a9eSThomas Graf static inline int nla_get_flag(struct nlattr *nla)
889bfa83a9eSThomas Graf {
890bfa83a9eSThomas Graf 	return !!nla;
891bfa83a9eSThomas Graf }
892bfa83a9eSThomas Graf 
893bfa83a9eSThomas Graf /**
894bfa83a9eSThomas Graf  * nla_get_msecs - return payload of msecs attribute
895bfa83a9eSThomas Graf  * @nla: msecs netlink attribute
896bfa83a9eSThomas Graf  *
897bfa83a9eSThomas Graf  * Returns the number of milliseconds in jiffies.
898bfa83a9eSThomas Graf  */
899bfa83a9eSThomas Graf static inline unsigned long nla_get_msecs(struct nlattr *nla)
900bfa83a9eSThomas Graf {
901bfa83a9eSThomas Graf 	u64 msecs = nla_get_u64(nla);
902bfa83a9eSThomas Graf 
903bfa83a9eSThomas Graf 	return msecs_to_jiffies((unsigned long) msecs);
904bfa83a9eSThomas Graf }
905bfa83a9eSThomas Graf 
906bfa83a9eSThomas Graf /**
907bfa83a9eSThomas Graf  * nla_nest_start - Start a new level of nested attributes
908bfa83a9eSThomas Graf  * @skb: socket buffer to add attributes to
909bfa83a9eSThomas Graf  * @attrtype: attribute type of container
910bfa83a9eSThomas Graf  *
911bfa83a9eSThomas Graf  * Returns the container attribute
912bfa83a9eSThomas Graf  */
913bfa83a9eSThomas Graf static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
914bfa83a9eSThomas Graf {
915bfa83a9eSThomas Graf 	struct nlattr *start = (struct nlattr *) skb->tail;
916bfa83a9eSThomas Graf 
917bfa83a9eSThomas Graf 	if (nla_put(skb, attrtype, 0, NULL) < 0)
918bfa83a9eSThomas Graf 		return NULL;
919bfa83a9eSThomas Graf 
920bfa83a9eSThomas Graf 	return start;
921bfa83a9eSThomas Graf }
922bfa83a9eSThomas Graf 
923bfa83a9eSThomas Graf /**
924bfa83a9eSThomas Graf  * nla_nest_end - Finalize nesting of attributes
925bfa83a9eSThomas Graf  * @skb: socket buffer the attribtues are stored in
926bfa83a9eSThomas Graf  * @start: container attribute
927bfa83a9eSThomas Graf  *
928bfa83a9eSThomas Graf  * Corrects the container attribute header to include the all
929bfa83a9eSThomas Graf  * appeneded attributes.
930bfa83a9eSThomas Graf  *
931bfa83a9eSThomas Graf  * Returns the total data length of the skb.
932bfa83a9eSThomas Graf  */
933bfa83a9eSThomas Graf static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
934bfa83a9eSThomas Graf {
935bfa83a9eSThomas Graf 	start->nla_len = skb->tail - (unsigned char *) start;
936bfa83a9eSThomas Graf 	return skb->len;
937bfa83a9eSThomas Graf }
938bfa83a9eSThomas Graf 
939bfa83a9eSThomas Graf /**
940bfa83a9eSThomas Graf  * nla_nest_cancel - Cancel nesting of attributes
941bfa83a9eSThomas Graf  * @skb: socket buffer the message is stored in
942bfa83a9eSThomas Graf  * @start: container attribute
943bfa83a9eSThomas Graf  *
944bfa83a9eSThomas Graf  * Removes the container attribute and including all nested
945bfa83a9eSThomas Graf  * attributes. Returns -1.
946bfa83a9eSThomas Graf  */
947bfa83a9eSThomas Graf static inline int nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
948bfa83a9eSThomas Graf {
949fe4944e5SThomas Graf 	return nlmsg_trim(skb, start);
950bfa83a9eSThomas Graf }
951bfa83a9eSThomas Graf 
952bfa83a9eSThomas Graf /**
953bfa83a9eSThomas Graf  * nla_for_each_attr - iterate over a stream of attributes
954bfa83a9eSThomas Graf  * @pos: loop counter, set to current attribute
955bfa83a9eSThomas Graf  * @head: head of attribute stream
956bfa83a9eSThomas Graf  * @len: length of attribute stream
957bfa83a9eSThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
958bfa83a9eSThomas Graf  */
959bfa83a9eSThomas Graf #define nla_for_each_attr(pos, head, len, rem) \
960bfa83a9eSThomas Graf 	for (pos = head, rem = len; \
961bfa83a9eSThomas Graf 	     nla_ok(pos, rem); \
962bfa83a9eSThomas Graf 	     pos = nla_next(pos, &(rem)))
963bfa83a9eSThomas Graf 
964fe4944e5SThomas Graf /**
965fe4944e5SThomas Graf  * nla_for_each_nested - iterate over nested attributes
966fe4944e5SThomas Graf  * @pos: loop counter, set to current attribute
967fe4944e5SThomas Graf  * @nla: attribute containing the nested attributes
968fe4944e5SThomas Graf  * @rem: initialized to len, holds bytes currently remaining in stream
969fe4944e5SThomas Graf  */
970fe4944e5SThomas Graf #define nla_for_each_nested(pos, nla, rem) \
971fe4944e5SThomas Graf 	nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
972fe4944e5SThomas Graf 
973bfa83a9eSThomas Graf #endif
974