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