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