1 #ifndef __NET_NETLINK_H
2 #define __NET_NETLINK_H
4 #include <linux/types.h>
5 #include <linux/netlink.h>
7 /* ========================================================================
8 * Netlink Messages and Attributes Interface (As Seen On TV)
9 * ------------------------------------------------------------------------
11 * ------------------------------------------------------------------------
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)-----------------------+
23 * <---------------------- nlmsg_len(nlh) --------------------->
24 * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) ->
25 * +----------------------+- - -+--------------------------------+
26 * | Family Header | Pad | Attributes |
27 * +----------------------+- - -+--------------------------------+
28 * nlmsg_attrdata(nlh, hdrlen)---^
31 * struct nlmsghdr netlink message header
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
44 * nlmsg_multicast() multicast message to several groups
45 * nlmsg_unicast() unicast a message to a single socket
46 * nlmsg_notify() send notification message
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
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
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
69 * nlmsg_report() report back to application?
71 * ------------------------------------------------------------------------
72 * Attributes Interface
73 * ------------------------------------------------------------------------
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)-----------------------------'
86 * struct nlattr netlink attribtue header
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
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
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
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
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
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
127 * Attribute Payload Access:
128 * nla_data(nla) head of attribute payload
129 * nla_len(nla) length of attribute payload
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
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
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 *=========================================================================
158 * Standard attribute types to specify validation policy
173 #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
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
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.
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) },
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,
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,
212 extern int nla_memcpy(void *dest, struct nlattr *src, int count);
213 extern int nla_memcmp(const struct nlattr *nla, const void *data,
215 extern int nla_strcmp(const struct nlattr *nla, const char *str);
216 extern struct nlattr * __nla_reserve(struct sk_buff *skb, int attrtype,
218 extern void * __nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
219 extern struct nlattr * nla_reserve(struct sk_buff *skb, int attrtype,
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,
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,
231 /**************************************************************************
233 **************************************************************************/
236 * nlmsg_msg_size - length of netlink message not including padding
237 * @payload: length of message payload
239 static inline int nlmsg_msg_size(int payload)
241 return NLMSG_HDRLEN + payload;
245 * nlmsg_total_size - length of netlink message including padding
246 * @payload: length of message payload
248 static inline int nlmsg_total_size(int payload)
250 return NLMSG_ALIGN(nlmsg_msg_size(payload));
254 * nlmsg_padlen - length of padding at the message's tail
255 * @payload: length of message payload
257 static inline int nlmsg_padlen(int payload)
259 return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
263 * nlmsg_data - head of message payload
264 * @nlh: netlink messsage header
266 static inline void *nlmsg_data(const struct nlmsghdr *nlh)
268 return (unsigned char *) nlh + NLMSG_HDRLEN;
272 * nlmsg_len - length of message payload
273 * @nlh: netlink message header
275 static inline int nlmsg_len(const struct nlmsghdr *nlh)
277 return nlh->nlmsg_len - NLMSG_HDRLEN;
281 * nlmsg_attrdata - head of attributes data
282 * @nlh: netlink message header
283 * @hdrlen: length of family specific header
285 static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
288 unsigned char *data = nlmsg_data(nlh);
289 return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
293 * nlmsg_attrlen - length of attributes data
294 * @nlh: netlink message header
295 * @hdrlen: length of family specific header
297 static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
299 return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
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
307 static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
309 return (remaining >= sizeof(struct nlmsghdr) &&
310 nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
311 nlh->nlmsg_len <= remaining);
315 * nlmsg_next - next netlink message in message stream
316 * @nlh: netlink message header
317 * @remaining: number of bytes remaining in message stream
319 * Returns the next netlink message in the message stream and
320 * decrements remaining by the size of the current message.
322 static inline struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
324 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
326 *remaining -= totlen;
328 return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
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
341 static inline int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen,
342 struct nlattr *tb[], int maxtype,
343 struct nla_policy *policy)
345 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
348 return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
349 nlmsg_attrlen(nlh, hdrlen), policy);
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
358 * Returns the first attribute which matches the specified type.
360 static inline struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh,
361 int hdrlen, int attrtype)
363 return nla_find(nlmsg_attrdata(nlh, hdrlen),
364 nlmsg_attrlen(nlh, hdrlen), attrtype);
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
374 static inline int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
375 struct nla_policy *policy)
377 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
380 return nla_validate(nlmsg_attrdata(nlh, hdrlen),
381 nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
385 * nlmsg_report - need to report back to application?
386 * @nlh: netlink message header
388 * Returns 1 if a report back to the application is requested.
390 static inline int nlmsg_report(struct nlmsghdr *nlh)
392 return !!(nlh->nlmsg_flags & NLM_F_ECHO);
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
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)
407 /* FIXME: Enable once all users have been converted */
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
418 * The caller is responsible to ensure that the skb provides enough
419 * tailroom for both the netlink header and payload.
421 static inline struct nlmsghdr *__nlmsg_put(struct sk_buff *skb, u32 pid,
422 u32 seq, int type, int payload,
425 struct nlmsghdr *nlh;
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;
434 memset((unsigned char *) nlmsg_data(nlh) + payload, 0,
435 nlmsg_padlen(payload));
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
450 * Returns NULL if the tailroom of the skb is insufficient to store
451 * the message header and payload.
453 static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
454 int type, int payload, int flags)
456 if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
459 return __nlmsg_put(skb, pid, seq, type, payload, flags);
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
470 * Returns NULL if the tailroom of the skb is insufficient to store
471 * the message header and payload.
473 static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
474 struct netlink_callback *cb,
475 int type, int payload,
478 return nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
479 type, payload, flags);
483 * nlmsg_new - Allocate a new netlink message
484 * @size: maximum size of message
485 * @flags: the type of memory to allocate.
487 * Use NLMSG_GOODSIZE if size isn't know and you need a good default size.
489 static inline struct sk_buff *nlmsg_new(int size, gfp_t flags)
491 return alloc_skb(size, flags);
495 * nlmsg_end - Finalize a netlink message
496 * @skb: socket buffer the message is stored in
497 * @nlh: netlink message header
499 * Corrects the netlink message header to include the appeneded
500 * attributes. Only necessary if attributes have been added to
503 * Returns the total data length of the skb.
505 static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
507 nlh->nlmsg_len = skb->tail - (unsigned char *) nlh;
513 * nlmsg_get_pos - return current position in netlink message
514 * @skb: socket buffer the message is stored in
516 * Returns a pointer to the current tail of the message.
518 static inline void *nlmsg_get_pos(struct sk_buff *skb)
524 * nlmsg_trim - Trim message to a mark
525 * @skb: socket buffer the message is stored in
526 * @mark: mark to trim to
528 * Trims the message to the provided mark. Returns -1.
530 static inline int nlmsg_trim(struct sk_buff *skb, void *mark)
533 skb_trim(skb, (unsigned char *) mark - skb->data);
539 * nlmsg_cancel - Cancel construction of a netlink message
540 * @skb: socket buffer the message is stored in
541 * @nlh: netlink message header
543 * Removes the complete netlink message including all
544 * attributes from the socket buffer again. Returns -1.
546 static inline int nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
548 return nlmsg_trim(skb, nlh);
552 * nlmsg_free - free a netlink message
553 * @skb: socket buffer of netlink message
555 static inline void nlmsg_free(struct sk_buff *skb)
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
568 static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
569 u32 pid, unsigned int group, gfp_t flags)
573 NETLINK_CB(skb).dst_group = group;
575 err = netlink_broadcast(sk, skb, pid, group, flags);
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
588 static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 pid)
592 err = netlink_unicast(sk, skb, pid, MSG_DONTWAIT);
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
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)))
611 /**************************************************************************
613 **************************************************************************/
616 * nla_attr_size - length of attribute not including padding
617 * @payload: length of payload
619 static inline int nla_attr_size(int payload)
621 return NLA_HDRLEN + payload;
625 * nla_total_size - total length of attribute including padding
626 * @payload: length of payload
628 static inline int nla_total_size(int payload)
630 return NLA_ALIGN(nla_attr_size(payload));
634 * nla_padlen - length of padding at the tail of attribute
635 * @payload: length of payload
637 static inline int nla_padlen(int payload)
639 return nla_total_size(payload) - nla_attr_size(payload);
643 * nla_data - head of payload
644 * @nla: netlink attribute
646 static inline void *nla_data(const struct nlattr *nla)
648 return (char *) nla + NLA_HDRLEN;
652 * nla_len - length of payload
653 * @nla: netlink attribute
655 static inline int nla_len(const struct nlattr *nla)
657 return nla->nla_len - NLA_HDRLEN;
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
665 static inline int nla_ok(const struct nlattr *nla, int remaining)
667 return remaining >= sizeof(*nla) &&
668 nla->nla_len >= sizeof(*nla) &&
669 nla->nla_len <= remaining;
673 * nla_next - next netlink attribte in attribute stream
674 * @nla: netlink attribute
675 * @remaining: number of bytes remaining in attribute stream
677 * Returns the next netlink attribute in the attribute stream and
678 * decrements remaining by the size of the current attribute.
680 static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
682 int totlen = NLA_ALIGN(nla->nla_len);
684 *remaining -= totlen;
685 return (struct nlattr *) ((char *) nla + totlen);
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
693 * Returns the first attribute which matches the specified type.
695 static inline struct nlattr *nla_find_nested(struct nlattr *nla, int attrtype)
697 return nla_find(nla_data(nla), nla_len(nla), attrtype);
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
709 static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
711 struct nla_policy *policy)
713 return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
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
721 static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
723 return nla_put(skb, attrtype, sizeof(u8), &value);
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
732 static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
734 return nla_put(skb, attrtype, sizeof(u16), &value);
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
743 static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
745 return nla_put(skb, attrtype, sizeof(u32), &value);
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
754 static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value)
756 return nla_put(skb, attrtype, sizeof(u64), &value);
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
765 static inline int nla_put_string(struct sk_buff *skb, int attrtype,
768 return nla_put(skb, attrtype, strlen(str) + 1, str);
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
776 static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
778 return nla_put(skb, attrtype, 0, NULL);
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
787 static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
788 unsigned long jiffies)
790 u64 tmp = jiffies_to_msecs(jiffies);
791 return nla_put(skb, attrtype, sizeof(u64), &tmp);
794 #define NLA_PUT(skb, attrtype, attrlen, data) \
796 if (nla_put(skb, attrtype, attrlen, data) < 0) \
797 goto nla_put_failure; \
800 #define NLA_PUT_TYPE(skb, type, attrtype, value) \
802 type __tmp = value; \
803 NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \
806 #define NLA_PUT_U8(skb, attrtype, value) \
807 NLA_PUT_TYPE(skb, u8, attrtype, value)
809 #define NLA_PUT_U16(skb, attrtype, value) \
810 NLA_PUT_TYPE(skb, u16, attrtype, value)
812 #define NLA_PUT_U32(skb, attrtype, value) \
813 NLA_PUT_TYPE(skb, u32, attrtype, value)
815 #define NLA_PUT_U64(skb, attrtype, value) \
816 NLA_PUT_TYPE(skb, u64, attrtype, value)
818 #define NLA_PUT_STRING(skb, attrtype, value) \
819 NLA_PUT(skb, attrtype, strlen(value) + 1, value)
821 #define NLA_PUT_FLAG(skb, attrtype, value) \
822 NLA_PUT(skb, attrtype, 0, NULL)
824 #define NLA_PUT_MSECS(skb, attrtype, jiffies) \
825 NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies))
828 * nla_get_u32 - return payload of u32 attribute
829 * @nla: u32 netlink attribute
831 static inline u32 nla_get_u32(struct nlattr *nla)
833 return *(u32 *) nla_data(nla);
837 * nla_get_u16 - return payload of u16 attribute
838 * @nla: u16 netlink attribute
840 static inline u16 nla_get_u16(struct nlattr *nla)
842 return *(u16 *) nla_data(nla);
846 * nla_get_u8 - return payload of u8 attribute
847 * @nla: u8 netlink attribute
849 static inline u8 nla_get_u8(struct nlattr *nla)
851 return *(u8 *) nla_data(nla);
855 * nla_get_u64 - return payload of u64 attribute
856 * @nla: u64 netlink attribute
858 static inline u64 nla_get_u64(struct nlattr *nla)
862 nla_memcpy(&tmp, nla, sizeof(tmp));
868 * nla_get_flag - return payload of flag attribute
869 * @nla: flag netlink attribute
871 static inline int nla_get_flag(struct nlattr *nla)
877 * nla_get_msecs - return payload of msecs attribute
878 * @nla: msecs netlink attribute
880 * Returns the number of milliseconds in jiffies.
882 static inline unsigned long nla_get_msecs(struct nlattr *nla)
884 u64 msecs = nla_get_u64(nla);
886 return msecs_to_jiffies((unsigned long) msecs);
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
894 * Returns the container attribute
896 static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
898 struct nlattr *start = (struct nlattr *) skb->tail;
900 if (nla_put(skb, attrtype, 0, NULL) < 0)
907 * nla_nest_end - Finalize nesting of attributes
908 * @skb: socket buffer the attribtues are stored in
909 * @start: container attribute
911 * Corrects the container attribute header to include the all
912 * appeneded attributes.
914 * Returns the total data length of the skb.
916 static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
918 start->nla_len = skb->tail - (unsigned char *) start;
923 * nla_nest_cancel - Cancel nesting of attributes
924 * @skb: socket buffer the message is stored in
925 * @start: container attribute
927 * Removes the container attribute and including all nested
928 * attributes. Returns -1.
930 static inline int nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
932 return nlmsg_trim(skb, start);
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
942 #define nla_for_each_attr(pos, head, len, rem) \
943 for (pos = head, rem = len; \
945 pos = nla_next(pos, &(rem)))
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
953 #define nla_for_each_nested(pos, nla, rem) \
954 nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)