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_cancel() cancel message construction
39 * nlmsg_free() free a netlink message
42 * nlmsg_multicast() multicast message to several groups
43 * nlmsg_unicast() unicast a message to a single socket
45 * Message Length Calculations:
46 * nlmsg_msg_size(payload) length of message w/o padding
47 * nlmsg_total_size(payload) length of message w/ padding
48 * nlmsg_padlen(payload) length of padding at tail
50 * Message Payload Access:
51 * nlmsg_data(nlh) head of message payload
52 * nlmsg_len(nlh) length of message payload
53 * nlmsg_attrdata(nlh, hdrlen) head of attributes data
54 * nlmsg_attrlen(nlh, hdrlen) length of attributes data
57 * nlmsg_ok(nlh, remaining) does nlh fit into remaining bytes?
58 * nlmsg_next(nlh, remaining) get next netlink message
59 * nlmsg_parse() parse attributes of a message
60 * nlmsg_find_attr() find an attribute in a message
61 * nlmsg_for_each_msg() loop over all messages
62 * nlmsg_validate() validate netlink message incl. attrs
63 * nlmsg_for_each_attr() loop over all attributes
65 * ------------------------------------------------------------------------
66 * Attributes Interface
67 * ------------------------------------------------------------------------
70 * <------- nla_total_size(payload) ------->
71 * <---- nla_attr_size(payload) ----->
72 * +----------+- - -+- - - - - - - - - +- - -+-------- - -
73 * | Header | Pad | Payload | Pad | Header
74 * +----------+- - -+- - - - - - - - - +- - -+-------- - -
75 * <- nla_len(nla) -> ^
76 * nla_data(nla)----^ |
77 * nla_next(nla)-----------------------------'
80 * struct nlattr netlink attribtue header
82 * Attribute Construction:
83 * nla_reserve(skb, type, len) reserve skb tailroom for an attribute
84 * nla_put(skb, type, len, data) add attribute to skb
86 * Attribute Construction for Basic Types:
87 * nla_put_u8(skb, type, value) add u8 attribute to skb
88 * nla_put_u16(skb, type, value) add u16 attribute to skb
89 * nla_put_u32(skb, type, value) add u32 attribute to skb
90 * nla_put_u64(skb, type, value) add u64 attribute to skb
91 * nla_put_string(skb, type, str) add string attribute to skb
92 * nla_put_flag(skb, type) add flag attribute to skb
93 * nla_put_msecs(skb, type, jiffies) add msecs attribute to skb
95 * Exceptions Based Attribute Construction:
96 * NLA_PUT(skb, type, len, data) add attribute to skb
97 * NLA_PUT_U8(skb, type, value) add u8 attribute to skb
98 * NLA_PUT_U16(skb, type, value) add u16 attribute to skb
99 * NLA_PUT_U32(skb, type, value) add u32 attribute to skb
100 * NLA_PUT_U64(skb, type, value) add u64 attribute to skb
101 * NLA_PUT_STRING(skb, type, str) add string attribute to skb
102 * NLA_PUT_FLAG(skb, type) add flag attribute to skb
103 * NLA_PUT_MSECS(skb, type, jiffies) add msecs attribute to skb
105 * The meaning of these functions is equal to their lower case
106 * variants but they jump to the label nla_put_failure in case
109 * Nested Attributes Construction:
110 * nla_nest_start(skb, type) start a nested attribute
111 * nla_nest_end(skb, nla) finalize a nested attribute
112 * nla_nest_cancel(skb, nla) cancel nested attribute construction
114 * Attribute Length Calculations:
115 * nla_attr_size(payload) length of attribute w/o padding
116 * nla_total_size(payload) length of attribute w/ padding
117 * nla_padlen(payload) length of padding
119 * Attribute Payload Access:
120 * nla_data(nla) head of attribute payload
121 * nla_len(nla) length of attribute payload
123 * Attribute Payload Access for Basic Types:
124 * nla_get_u8(nla) get payload for a u8 attribute
125 * nla_get_u16(nla) get payload for a u16 attribute
126 * nla_get_u32(nla) get payload for a u32 attribute
127 * nla_get_u64(nla) get payload for a u64 attribute
128 * nla_get_flag(nla) return 1 if flag is true
129 * nla_get_msecs(nla) get payload for a msecs attribute
132 * nla_memcpy(dest, nla, count) copy attribute into memory
133 * nla_memcmp(nla, data, size) compare attribute with memory area
134 * nla_strlcpy(dst, nla, size) copy attribute to a sized string
135 * nla_strcmp(nla, str) compare attribute with string
138 * nla_ok(nla, remaining) does nla fit into remaining bytes?
139 * nla_next(nla, remaining) get next netlink attribute
140 * nla_validate() validate a stream of attributes
141 * nla_find() find attribute in stream of attributes
142 * nla_parse() parse and validate stream of attrs
143 * nla_parse_nested() parse nested attribuets
144 * nla_for_each_attr() loop over all attributes
145 *=========================================================================
149 * Standard attribute types to specify validation policy
164 #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
167 * struct nla_policy - attribute validation policy
168 * @type: Type of attribute or NLA_UNSPEC
169 * @minlen: Minimal length of payload required to be available
171 * Policies are defined as arrays of this struct, the array must be
172 * accessible by attribute type up to the highest identifier to be expected.
175 * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = {
176 * [ATTR_FOO] = { .type = NLA_U16 },
177 * [ATTR_BAR] = { .type = NLA_STRING },
178 * [ATTR_BAZ] = { .minlen = sizeof(struct mystruct) },
186 extern void netlink_run_queue(struct sock
*sk
, unsigned int *qlen
,
187 int (*cb
)(struct sk_buff
*,
188 struct nlmsghdr
*, int *));
189 extern void netlink_queue_skip(struct nlmsghdr
*nlh
,
190 struct sk_buff
*skb
);
192 extern int nla_validate(struct nlattr
*head
, int len
, int maxtype
,
193 struct nla_policy
*policy
);
194 extern int nla_parse(struct nlattr
*tb
[], int maxtype
,
195 struct nlattr
*head
, int len
,
196 struct nla_policy
*policy
);
197 extern struct nlattr
* nla_find(struct nlattr
*head
, int len
, int attrtype
);
198 extern size_t nla_strlcpy(char *dst
, const struct nlattr
*nla
,
200 extern int nla_memcpy(void *dest
, struct nlattr
*src
, int count
);
201 extern int nla_memcmp(const struct nlattr
*nla
, const void *data
,
203 extern int nla_strcmp(const struct nlattr
*nla
, const char *str
);
204 extern struct nlattr
* __nla_reserve(struct sk_buff
*skb
, int attrtype
,
206 extern struct nlattr
* nla_reserve(struct sk_buff
*skb
, int attrtype
,
208 extern void __nla_put(struct sk_buff
*skb
, int attrtype
,
209 int attrlen
, const void *data
);
210 extern int nla_put(struct sk_buff
*skb
, int attrtype
,
211 int attrlen
, const void *data
);
213 /**************************************************************************
215 **************************************************************************/
218 * nlmsg_msg_size - length of netlink message not including padding
219 * @payload: length of message payload
221 static inline int nlmsg_msg_size(int payload
)
223 return NLMSG_HDRLEN
+ payload
;
227 * nlmsg_total_size - length of netlink message including padding
228 * @payload: length of message payload
230 static inline int nlmsg_total_size(int payload
)
232 return NLMSG_ALIGN(nlmsg_msg_size(payload
));
236 * nlmsg_padlen - length of padding at the message's tail
237 * @payload: length of message payload
239 static inline int nlmsg_padlen(int payload
)
241 return nlmsg_total_size(payload
) - nlmsg_msg_size(payload
);
245 * nlmsg_data - head of message payload
246 * @nlh: netlink messsage header
248 static inline void *nlmsg_data(const struct nlmsghdr
*nlh
)
250 return (unsigned char *) nlh
+ NLMSG_HDRLEN
;
254 * nlmsg_len - length of message payload
255 * @nlh: netlink message header
257 static inline int nlmsg_len(const struct nlmsghdr
*nlh
)
259 return nlh
->nlmsg_len
- NLMSG_HDRLEN
;
263 * nlmsg_attrdata - head of attributes data
264 * @nlh: netlink message header
265 * @hdrlen: length of family specific header
267 static inline struct nlattr
*nlmsg_attrdata(const struct nlmsghdr
*nlh
,
270 unsigned char *data
= nlmsg_data(nlh
);
271 return (struct nlattr
*) (data
+ NLMSG_ALIGN(hdrlen
));
275 * nlmsg_attrlen - length of attributes data
276 * @nlh: netlink message header
277 * @hdrlen: length of family specific header
279 static inline int nlmsg_attrlen(const struct nlmsghdr
*nlh
, int hdrlen
)
281 return nlmsg_len(nlh
) - NLMSG_ALIGN(hdrlen
);
285 * nlmsg_ok - check if the netlink message fits into the remaining bytes
286 * @nlh: netlink message header
287 * @remaining: number of bytes remaining in message stream
289 static inline int nlmsg_ok(const struct nlmsghdr
*nlh
, int remaining
)
291 return (remaining
>= sizeof(struct nlmsghdr
) &&
292 nlh
->nlmsg_len
>= sizeof(struct nlmsghdr
) &&
293 nlh
->nlmsg_len
<= remaining
);
297 * nlmsg_next - next netlink message in message stream
298 * @nlh: netlink message header
299 * @remaining: number of bytes remaining in message stream
301 * Returns the next netlink message in the message stream and
302 * decrements remaining by the size of the current message.
304 static inline struct nlmsghdr
*nlmsg_next(struct nlmsghdr
*nlh
, int *remaining
)
306 int totlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
308 *remaining
-= totlen
;
310 return (struct nlmsghdr
*) ((unsigned char *) nlh
+ totlen
);
314 * nlmsg_parse - parse attributes of a netlink message
315 * @nlh: netlink message header
316 * @hdrlen: length of family specific header
317 * @tb: destination array with maxtype+1 elements
318 * @maxtype: maximum attribute type to be expected
319 * @policy: validation policy
323 static inline int nlmsg_parse(struct nlmsghdr
*nlh
, int hdrlen
,
324 struct nlattr
*tb
[], int maxtype
,
325 struct nla_policy
*policy
)
327 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
330 return nla_parse(tb
, maxtype
, nlmsg_attrdata(nlh
, hdrlen
),
331 nlmsg_attrlen(nlh
, hdrlen
), policy
);
335 * nlmsg_find_attr - find a specific attribute in a netlink message
336 * @nlh: netlink message header
337 * @hdrlen: length of familiy specific header
338 * @attrtype: type of attribute to look for
340 * Returns the first attribute which matches the specified type.
342 static inline struct nlattr
*nlmsg_find_attr(struct nlmsghdr
*nlh
,
343 int hdrlen
, int attrtype
)
345 return nla_find(nlmsg_attrdata(nlh
, hdrlen
),
346 nlmsg_attrlen(nlh
, hdrlen
), attrtype
);
350 * nlmsg_validate - validate a netlink message including attributes
351 * @nlh: netlinket message header
352 * @hdrlen: length of familiy specific header
353 * @maxtype: maximum attribute type to be expected
354 * @policy: validation policy
356 static inline int nlmsg_validate(struct nlmsghdr
*nlh
, int hdrlen
, int maxtype
,
357 struct nla_policy
*policy
)
359 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
362 return nla_validate(nlmsg_attrdata(nlh
, hdrlen
),
363 nlmsg_attrlen(nlh
, hdrlen
), maxtype
, policy
);
367 * nlmsg_for_each_attr - iterate over a stream of attributes
368 * @pos: loop counter, set to current attribute
369 * @nlh: netlink message header
370 * @hdrlen: length of familiy specific header
371 * @rem: initialized to len, holds bytes currently remaining in stream
373 #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
374 nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
375 nlmsg_attrlen(nlh, hdrlen), rem)
378 /* FIXME: Enable once all users have been converted */
381 * __nlmsg_put - Add a new netlink message to an skb
382 * @skb: socket buffer to store message in
383 * @pid: netlink process id
384 * @seq: sequence number of message
385 * @type: message type
386 * @payload: length of message payload
387 * @flags: message flags
389 * The caller is responsible to ensure that the skb provides enough
390 * tailroom for both the netlink header and payload.
392 static inline struct nlmsghdr
*__nlmsg_put(struct sk_buff
*skb
, u32 pid
,
393 u32 seq
, int type
, int payload
,
396 struct nlmsghdr
*nlh
;
398 nlh
= (struct nlmsghdr
*) skb_put(skb
, nlmsg_total_size(payload
));
399 nlh
->nlmsg_type
= type
;
400 nlh
->nlmsg_len
= nlmsg_msg_size(payload
);
401 nlh
->nlmsg_flags
= flags
;
402 nlh
->nlmsg_pid
= pid
;
403 nlh
->nlmsg_seq
= seq
;
405 memset((unsigned char *) nlmsg_data(nlh
) + payload
, 0,
406 nlmsg_padlen(payload
));
413 * nlmsg_put - Add a new netlink message to an skb
414 * @skb: socket buffer to store message in
415 * @pid: netlink process id
416 * @seq: sequence number of message
417 * @type: message type
418 * @payload: length of message payload
419 * @flags: message flags
421 * Returns NULL if the tailroom of the skb is insufficient to store
422 * the message header and payload.
424 static inline struct nlmsghdr
*nlmsg_put(struct sk_buff
*skb
, u32 pid
, u32 seq
,
425 int type
, int payload
, int flags
)
427 if (unlikely(skb_tailroom(skb
) < nlmsg_total_size(payload
)))
430 return __nlmsg_put(skb
, pid
, seq
, type
, payload
, flags
);
434 * nlmsg_put_answer - Add a new callback based netlink message to an skb
435 * @skb: socket buffer to store message in
436 * @cb: netlink callback
437 * @type: message type
438 * @payload: length of message payload
439 * @flags: message flags
441 * Returns NULL if the tailroom of the skb is insufficient to store
442 * the message header and payload.
444 static inline struct nlmsghdr
*nlmsg_put_answer(struct sk_buff
*skb
,
445 struct netlink_callback
*cb
,
446 int type
, int payload
,
449 return nlmsg_put(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
450 type
, payload
, flags
);
454 * nlmsg_new - Allocate a new netlink message
455 * @size: maximum size of message
457 * Use NLMSG_GOODSIZE if size isn't know and you need a good default size.
459 static inline struct sk_buff
*nlmsg_new(int size
)
461 return alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
465 * nlmsg_end - Finalize a netlink message
466 * @skb: socket buffer the message is stored in
467 * @nlh: netlink message header
469 * Corrects the netlink message header to include the appeneded
470 * attributes. Only necessary if attributes have been added to
473 * Returns the total data length of the skb.
475 static inline int nlmsg_end(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
477 nlh
->nlmsg_len
= skb
->tail
- (unsigned char *) nlh
;
483 * nlmsg_cancel - Cancel construction of a netlink message
484 * @skb: socket buffer the message is stored in
485 * @nlh: netlink message header
487 * Removes the complete netlink message including all
488 * attributes from the socket buffer again. Returns -1.
490 static inline int nlmsg_cancel(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
492 skb_trim(skb
, (unsigned char *) nlh
- skb
->data
);
498 * nlmsg_free - free a netlink message
499 * @skb: socket buffer of netlink message
501 static inline void nlmsg_free(struct sk_buff
*skb
)
507 * nlmsg_multicast - multicast a netlink message
508 * @sk: netlink socket to spread messages to
509 * @skb: netlink message as socket buffer
510 * @pid: own netlink pid to avoid sending to yourself
511 * @group: multicast group id
513 static inline int nlmsg_multicast(struct sock
*sk
, struct sk_buff
*skb
,
514 u32 pid
, unsigned int group
)
518 NETLINK_CB(skb
).dst_group
= group
;
520 err
= netlink_broadcast(sk
, skb
, pid
, group
, GFP_KERNEL
);
528 * nlmsg_unicast - unicast a netlink message
529 * @sk: netlink socket to spread message to
530 * @skb: netlink message as socket buffer
531 * @pid: netlink pid of the destination socket
533 static inline int nlmsg_unicast(struct sock
*sk
, struct sk_buff
*skb
, u32 pid
)
537 err
= netlink_unicast(sk
, skb
, pid
, MSG_DONTWAIT
);
545 * nlmsg_for_each_msg - iterate over a stream of messages
546 * @pos: loop counter, set to current message
547 * @head: head of message stream
548 * @len: length of message stream
549 * @rem: initialized to len, holds bytes currently remaining in stream
551 #define nlmsg_for_each_msg(pos, head, len, rem) \
552 for (pos = head, rem = len; \
553 nlmsg_ok(pos, rem); \
554 pos = nlmsg_next(pos, &(rem)))
556 /**************************************************************************
558 **************************************************************************/
561 * nla_attr_size - length of attribute not including padding
562 * @payload: length of payload
564 static inline int nla_attr_size(int payload
)
566 return NLA_HDRLEN
+ payload
;
570 * nla_total_size - total length of attribute including padding
571 * @payload: length of payload
573 static inline int nla_total_size(int payload
)
575 return NLA_ALIGN(nla_attr_size(payload
));
579 * nla_padlen - length of padding at the tail of attribute
580 * @payload: length of payload
582 static inline int nla_padlen(int payload
)
584 return nla_total_size(payload
) - nla_attr_size(payload
);
588 * nla_data - head of payload
589 * @nla: netlink attribute
591 static inline void *nla_data(const struct nlattr
*nla
)
593 return (char *) nla
+ NLA_HDRLEN
;
597 * nla_len - length of payload
598 * @nla: netlink attribute
600 static inline int nla_len(const struct nlattr
*nla
)
602 return nla
->nla_len
- NLA_HDRLEN
;
606 * nla_ok - check if the netlink attribute fits into the remaining bytes
607 * @nla: netlink attribute
608 * @remaining: number of bytes remaining in attribute stream
610 static inline int nla_ok(const struct nlattr
*nla
, int remaining
)
612 return remaining
>= sizeof(*nla
) &&
613 nla
->nla_len
>= sizeof(*nla
) &&
614 nla
->nla_len
<= remaining
;
618 * nla_next - next netlink attribte in attribute stream
619 * @nla: netlink attribute
620 * @remaining: number of bytes remaining in attribute stream
622 * Returns the next netlink attribute in the attribute stream and
623 * decrements remaining by the size of the current attribute.
625 static inline struct nlattr
*nla_next(const struct nlattr
*nla
, int *remaining
)
627 int totlen
= NLA_ALIGN(nla
->nla_len
);
629 *remaining
-= totlen
;
630 return (struct nlattr
*) ((char *) nla
+ totlen
);
634 * nla_parse_nested - parse nested attributes
635 * @tb: destination array with maxtype+1 elements
636 * @maxtype: maximum attribute type to be expected
637 * @nla: attribute containing the nested attributes
638 * @policy: validation policy
642 static inline int nla_parse_nested(struct nlattr
*tb
[], int maxtype
,
644 struct nla_policy
*policy
)
646 return nla_parse(tb
, maxtype
, nla_data(nla
), nla_len(nla
), policy
);
649 * nla_put_u8 - Add a u16 netlink attribute to a socket buffer
650 * @skb: socket buffer to add attribute to
651 * @attrtype: attribute type
652 * @value: numeric value
654 static inline int nla_put_u8(struct sk_buff
*skb
, int attrtype
, u8 value
)
656 return nla_put(skb
, attrtype
, sizeof(u8
), &value
);
660 * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
661 * @skb: socket buffer to add attribute to
662 * @attrtype: attribute type
663 * @value: numeric value
665 static inline int nla_put_u16(struct sk_buff
*skb
, int attrtype
, u16 value
)
667 return nla_put(skb
, attrtype
, sizeof(u16
), &value
);
671 * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
672 * @skb: socket buffer to add attribute to
673 * @attrtype: attribute type
674 * @value: numeric value
676 static inline int nla_put_u32(struct sk_buff
*skb
, int attrtype
, u32 value
)
678 return nla_put(skb
, attrtype
, sizeof(u32
), &value
);
682 * nla_put_64 - Add a u64 netlink attribute to a socket buffer
683 * @skb: socket buffer to add attribute to
684 * @attrtype: attribute type
685 * @value: numeric value
687 static inline int nla_put_u64(struct sk_buff
*skb
, int attrtype
, u64 value
)
689 return nla_put(skb
, attrtype
, sizeof(u64
), &value
);
693 * nla_put_string - Add a string netlink attribute to a socket buffer
694 * @skb: socket buffer to add attribute to
695 * @attrtype: attribute type
696 * @str: NUL terminated string
698 static inline int nla_put_string(struct sk_buff
*skb
, int attrtype
,
701 return nla_put(skb
, attrtype
, strlen(str
) + 1, str
);
705 * nla_put_flag - Add a flag netlink attribute to a socket buffer
706 * @skb: socket buffer to add attribute to
707 * @attrtype: attribute type
709 static inline int nla_put_flag(struct sk_buff
*skb
, int attrtype
)
711 return nla_put(skb
, attrtype
, 0, NULL
);
715 * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
716 * @skb: socket buffer to add attribute to
717 * @attrtype: attribute type
718 * @jiffies: number of msecs in jiffies
720 static inline int nla_put_msecs(struct sk_buff
*skb
, int attrtype
,
721 unsigned long jiffies
)
723 u64 tmp
= jiffies_to_msecs(jiffies
);
724 return nla_put(skb
, attrtype
, sizeof(u64
), &tmp
);
727 #define NLA_PUT(skb, attrtype, attrlen, data) \
729 if (nla_put(skb, attrtype, attrlen, data) < 0) \
730 goto nla_put_failure; \
733 #define NLA_PUT_TYPE(skb, type, attrtype, value) \
735 type __tmp = value; \
736 NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \
739 #define NLA_PUT_U8(skb, attrtype, value) \
740 NLA_PUT_TYPE(skb, u8, attrtype, value)
742 #define NLA_PUT_U16(skb, attrtype, value) \
743 NLA_PUT_TYPE(skb, u16, attrtype, value)
745 #define NLA_PUT_U32(skb, attrtype, value) \
746 NLA_PUT_TYPE(skb, u32, attrtype, value)
748 #define NLA_PUT_U64(skb, attrtype, value) \
749 NLA_PUT_TYPE(skb, u64, attrtype, value)
751 #define NLA_PUT_STRING(skb, attrtype, value) \
752 NLA_PUT(skb, attrtype, strlen(value) + 1, value)
754 #define NLA_PUT_FLAG(skb, attrtype, value) \
755 NLA_PUT(skb, attrtype, 0, NULL)
757 #define NLA_PUT_MSECS(skb, attrtype, jiffies) \
758 NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies))
761 * nla_get_u32 - return payload of u32 attribute
762 * @nla: u32 netlink attribute
764 static inline u32
nla_get_u32(struct nlattr
*nla
)
766 return *(u32
*) nla_data(nla
);
770 * nla_get_u16 - return payload of u16 attribute
771 * @nla: u16 netlink attribute
773 static inline u16
nla_get_u16(struct nlattr
*nla
)
775 return *(u16
*) nla_data(nla
);
779 * nla_get_u8 - return payload of u8 attribute
780 * @nla: u8 netlink attribute
782 static inline u8
nla_get_u8(struct nlattr
*nla
)
784 return *(u8
*) nla_data(nla
);
788 * nla_get_u64 - return payload of u64 attribute
789 * @nla: u64 netlink attribute
791 static inline u64
nla_get_u64(struct nlattr
*nla
)
795 nla_memcpy(&tmp
, nla
, sizeof(tmp
));
801 * nla_get_flag - return payload of flag attribute
802 * @nla: flag netlink attribute
804 static inline int nla_get_flag(struct nlattr
*nla
)
810 * nla_get_msecs - return payload of msecs attribute
811 * @nla: msecs netlink attribute
813 * Returns the number of milliseconds in jiffies.
815 static inline unsigned long nla_get_msecs(struct nlattr
*nla
)
817 u64 msecs
= nla_get_u64(nla
);
819 return msecs_to_jiffies((unsigned long) msecs
);
823 * nla_nest_start - Start a new level of nested attributes
824 * @skb: socket buffer to add attributes to
825 * @attrtype: attribute type of container
827 * Returns the container attribute
829 static inline struct nlattr
*nla_nest_start(struct sk_buff
*skb
, int attrtype
)
831 struct nlattr
*start
= (struct nlattr
*) skb
->tail
;
833 if (nla_put(skb
, attrtype
, 0, NULL
) < 0)
840 * nla_nest_end - Finalize nesting of attributes
841 * @skb: socket buffer the attribtues are stored in
842 * @start: container attribute
844 * Corrects the container attribute header to include the all
845 * appeneded attributes.
847 * Returns the total data length of the skb.
849 static inline int nla_nest_end(struct sk_buff
*skb
, struct nlattr
*start
)
851 start
->nla_len
= skb
->tail
- (unsigned char *) start
;
856 * nla_nest_cancel - Cancel nesting of attributes
857 * @skb: socket buffer the message is stored in
858 * @start: container attribute
860 * Removes the container attribute and including all nested
861 * attributes. Returns -1.
863 static inline int nla_nest_cancel(struct sk_buff
*skb
, struct nlattr
*start
)
866 skb_trim(skb
, (unsigned char *) start
- skb
->data
);
872 * nla_for_each_attr - iterate over a stream of attributes
873 * @pos: loop counter, set to current attribute
874 * @head: head of attribute stream
875 * @len: length of attribute stream
876 * @rem: initialized to len, holds bytes currently remaining in stream
878 #define nla_for_each_attr(pos, head, len, rem) \
879 for (pos = head, rem = len; \
881 pos = nla_next(pos, &(rem)))