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