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