]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - include/net/netlink.h
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
68 * ------------------------------------------------------------------------
69 * Attributes Interface
70 * ------------------------------------------------------------------------
73 * <------- nla_total_size(payload) ------->
74 * <---- nla_attr_size(payload) ----->
75 * +----------+- - -+- - - - - - - - - +- - -+-------- - -
76 * | Header | Pad | Payload | Pad | Header
77 * +----------+- - -+- - - - - - - - - +- - -+-------- - -
78 * <- nla_len(nla) -> ^
79 * nla_data(nla)----^ |
80 * nla_next(nla)-----------------------------'
83 * struct nlattr netlink attribtue header
85 * Attribute Construction:
86 * nla_reserve(skb, type, len) reserve room for an attribute
87 * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr
88 * nla_put(skb, type, len, data) add attribute to skb
89 * nla_put_nohdr(skb, len, data) add attribute w/o hdr
91 * Attribute Construction for Basic Types:
92 * nla_put_u8(skb, type, value) add u8 attribute to skb
93 * nla_put_u16(skb, type, value) add u16 attribute to skb
94 * nla_put_u32(skb, type, value) add u32 attribute to skb
95 * nla_put_u64(skb, type, value) add u64 attribute to skb
96 * nla_put_string(skb, type, str) add string attribute to skb
97 * nla_put_flag(skb, type) add flag attribute to skb
98 * nla_put_msecs(skb, type, jiffies) add msecs attribute to skb
100 * Exceptions Based Attribute Construction:
101 * NLA_PUT(skb, type, len, data) add attribute to skb
102 * NLA_PUT_U8(skb, type, value) add u8 attribute to skb
103 * NLA_PUT_U16(skb, type, value) add u16 attribute to skb
104 * NLA_PUT_U32(skb, type, value) add u32 attribute to skb
105 * NLA_PUT_U64(skb, type, value) add u64 attribute to skb
106 * NLA_PUT_STRING(skb, type, str) add string attribute to skb
107 * NLA_PUT_FLAG(skb, type) add flag attribute to skb
108 * NLA_PUT_MSECS(skb, type, jiffies) add msecs attribute to skb
110 * The meaning of these functions is equal to their lower case
111 * variants but they jump to the label nla_put_failure in case
114 * Nested Attributes Construction:
115 * nla_nest_start(skb, type) start a nested attribute
116 * nla_nest_end(skb, nla) finalize a nested attribute
117 * nla_nest_cancel(skb, nla) cancel nested attribute construction
119 * Attribute Length Calculations:
120 * nla_attr_size(payload) length of attribute w/o padding
121 * nla_total_size(payload) length of attribute w/ padding
122 * nla_padlen(payload) length of padding
124 * Attribute Payload Access:
125 * nla_data(nla) head of attribute payload
126 * nla_len(nla) length of attribute payload
128 * Attribute Payload Access for Basic Types:
129 * nla_get_u8(nla) get payload for a u8 attribute
130 * nla_get_u16(nla) get payload for a u16 attribute
131 * nla_get_u32(nla) get payload for a u32 attribute
132 * nla_get_u64(nla) get payload for a u64 attribute
133 * nla_get_flag(nla) return 1 if flag is true
134 * nla_get_msecs(nla) get payload for a msecs attribute
137 * nla_memcpy(dest, nla, count) copy attribute into memory
138 * nla_memcmp(nla, data, size) compare attribute with memory area
139 * nla_strlcpy(dst, nla, size) copy attribute to a sized string
140 * nla_strcmp(nla, str) compare attribute with string
143 * nla_ok(nla, remaining) does nla fit into remaining bytes?
144 * nla_next(nla, remaining) get next netlink attribute
145 * nla_validate() validate a stream of attributes
146 * nla_find() find attribute in stream of attributes
147 * nla_find_nested() find attribute in nested attributes
148 * nla_parse() parse and validate stream of attrs
149 * nla_parse_nested() parse nested attribuets
150 * nla_for_each_attr() loop over all attributes
151 *=========================================================================
155 * Standard attribute types to specify validation policy
170 #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
173 * struct nla_policy - attribute validation policy
174 * @type: Type of attribute or NLA_UNSPEC
175 * @minlen: Minimal length of payload required to be available
177 * Policies are defined as arrays of this struct, the array must be
178 * accessible by attribute type up to the highest identifier to be expected.
181 * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = {
182 * [ATTR_FOO] = { .type = NLA_U16 },
183 * [ATTR_BAR] = { .type = NLA_STRING },
184 * [ATTR_BAZ] = { .minlen = sizeof(struct mystruct) },
192 extern void netlink_run_queue(struct sock
*sk
, unsigned int *qlen
,
193 int (*cb
)(struct sk_buff
*,
194 struct nlmsghdr
*, int *));
195 extern void netlink_queue_skip(struct nlmsghdr
*nlh
,
196 struct sk_buff
*skb
);
198 extern int nla_validate(struct nlattr
*head
, int len
, int maxtype
,
199 struct nla_policy
*policy
);
200 extern int nla_parse(struct nlattr
*tb
[], int maxtype
,
201 struct nlattr
*head
, int len
,
202 struct nla_policy
*policy
);
203 extern struct nlattr
* nla_find(struct nlattr
*head
, int len
, int attrtype
);
204 extern size_t nla_strlcpy(char *dst
, const struct nlattr
*nla
,
206 extern int nla_memcpy(void *dest
, struct nlattr
*src
, int count
);
207 extern int nla_memcmp(const struct nlattr
*nla
, const void *data
,
209 extern int nla_strcmp(const struct nlattr
*nla
, const char *str
);
210 extern struct nlattr
* __nla_reserve(struct sk_buff
*skb
, int attrtype
,
212 extern void * __nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
);
213 extern struct nlattr
* nla_reserve(struct sk_buff
*skb
, int attrtype
,
215 extern void * nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
);
216 extern void __nla_put(struct sk_buff
*skb
, int attrtype
,
217 int attrlen
, const void *data
);
218 extern void __nla_put_nohdr(struct sk_buff
*skb
, int attrlen
,
220 extern int nla_put(struct sk_buff
*skb
, int attrtype
,
221 int attrlen
, const void *data
);
222 extern int nla_put_nohdr(struct sk_buff
*skb
, int attrlen
,
225 /**************************************************************************
227 **************************************************************************/
230 * nlmsg_msg_size - length of netlink message not including padding
231 * @payload: length of message payload
233 static inline int nlmsg_msg_size(int payload
)
235 return NLMSG_HDRLEN
+ payload
;
239 * nlmsg_total_size - length of netlink message including padding
240 * @payload: length of message payload
242 static inline int nlmsg_total_size(int payload
)
244 return NLMSG_ALIGN(nlmsg_msg_size(payload
));
248 * nlmsg_padlen - length of padding at the message's tail
249 * @payload: length of message payload
251 static inline int nlmsg_padlen(int payload
)
253 return nlmsg_total_size(payload
) - nlmsg_msg_size(payload
);
257 * nlmsg_data - head of message payload
258 * @nlh: netlink messsage header
260 static inline void *nlmsg_data(const struct nlmsghdr
*nlh
)
262 return (unsigned char *) nlh
+ NLMSG_HDRLEN
;
266 * nlmsg_len - length of message payload
267 * @nlh: netlink message header
269 static inline int nlmsg_len(const struct nlmsghdr
*nlh
)
271 return nlh
->nlmsg_len
- NLMSG_HDRLEN
;
275 * nlmsg_attrdata - head of attributes data
276 * @nlh: netlink message header
277 * @hdrlen: length of family specific header
279 static inline struct nlattr
*nlmsg_attrdata(const struct nlmsghdr
*nlh
,
282 unsigned char *data
= nlmsg_data(nlh
);
283 return (struct nlattr
*) (data
+ NLMSG_ALIGN(hdrlen
));
287 * nlmsg_attrlen - length of attributes data
288 * @nlh: netlink message header
289 * @hdrlen: length of family specific header
291 static inline int nlmsg_attrlen(const struct nlmsghdr
*nlh
, int hdrlen
)
293 return nlmsg_len(nlh
) - NLMSG_ALIGN(hdrlen
);
297 * nlmsg_ok - check if the netlink message fits into the remaining bytes
298 * @nlh: netlink message header
299 * @remaining: number of bytes remaining in message stream
301 static inline int nlmsg_ok(const struct nlmsghdr
*nlh
, int remaining
)
303 return (remaining
>= sizeof(struct nlmsghdr
) &&
304 nlh
->nlmsg_len
>= sizeof(struct nlmsghdr
) &&
305 nlh
->nlmsg_len
<= remaining
);
309 * nlmsg_next - next netlink message in message stream
310 * @nlh: netlink message header
311 * @remaining: number of bytes remaining in message stream
313 * Returns the next netlink message in the message stream and
314 * decrements remaining by the size of the current message.
316 static inline struct nlmsghdr
*nlmsg_next(struct nlmsghdr
*nlh
, int *remaining
)
318 int totlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
320 *remaining
-= totlen
;
322 return (struct nlmsghdr
*) ((unsigned char *) nlh
+ totlen
);
326 * nlmsg_parse - parse attributes of a netlink message
327 * @nlh: netlink message header
328 * @hdrlen: length of family specific header
329 * @tb: destination array with maxtype+1 elements
330 * @maxtype: maximum attribute type to be expected
331 * @policy: validation policy
335 static inline int nlmsg_parse(struct nlmsghdr
*nlh
, int hdrlen
,
336 struct nlattr
*tb
[], int maxtype
,
337 struct nla_policy
*policy
)
339 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
342 return nla_parse(tb
, maxtype
, nlmsg_attrdata(nlh
, hdrlen
),
343 nlmsg_attrlen(nlh
, hdrlen
), policy
);
347 * nlmsg_find_attr - find a specific attribute in a netlink message
348 * @nlh: netlink message header
349 * @hdrlen: length of familiy specific header
350 * @attrtype: type of attribute to look for
352 * Returns the first attribute which matches the specified type.
354 static inline struct nlattr
*nlmsg_find_attr(struct nlmsghdr
*nlh
,
355 int hdrlen
, int attrtype
)
357 return nla_find(nlmsg_attrdata(nlh
, hdrlen
),
358 nlmsg_attrlen(nlh
, hdrlen
), attrtype
);
362 * nlmsg_validate - validate a netlink message including attributes
363 * @nlh: netlinket message header
364 * @hdrlen: length of familiy specific header
365 * @maxtype: maximum attribute type to be expected
366 * @policy: validation policy
368 static inline int nlmsg_validate(struct nlmsghdr
*nlh
, int hdrlen
, int maxtype
,
369 struct nla_policy
*policy
)
371 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
374 return nla_validate(nlmsg_attrdata(nlh
, hdrlen
),
375 nlmsg_attrlen(nlh
, hdrlen
), maxtype
, policy
);
379 * nlmsg_for_each_attr - iterate over a stream of attributes
380 * @pos: loop counter, set to current attribute
381 * @nlh: netlink message header
382 * @hdrlen: length of familiy specific header
383 * @rem: initialized to len, holds bytes currently remaining in stream
385 #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
386 nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
387 nlmsg_attrlen(nlh, hdrlen), rem)
390 /* FIXME: Enable once all users have been converted */
393 * __nlmsg_put - Add a new netlink message to an skb
394 * @skb: socket buffer to store message in
395 * @pid: netlink process id
396 * @seq: sequence number of message
397 * @type: message type
398 * @payload: length of message payload
399 * @flags: message flags
401 * The caller is responsible to ensure that the skb provides enough
402 * tailroom for both the netlink header and payload.
404 static inline struct nlmsghdr
*__nlmsg_put(struct sk_buff
*skb
, u32 pid
,
405 u32 seq
, int type
, int payload
,
408 struct nlmsghdr
*nlh
;
410 nlh
= (struct nlmsghdr
*) skb_put(skb
, nlmsg_total_size(payload
));
411 nlh
->nlmsg_type
= type
;
412 nlh
->nlmsg_len
= nlmsg_msg_size(payload
);
413 nlh
->nlmsg_flags
= flags
;
414 nlh
->nlmsg_pid
= pid
;
415 nlh
->nlmsg_seq
= seq
;
417 memset((unsigned char *) nlmsg_data(nlh
) + payload
, 0,
418 nlmsg_padlen(payload
));
425 * nlmsg_put - Add a new netlink message to an skb
426 * @skb: socket buffer to store message in
427 * @pid: netlink process id
428 * @seq: sequence number of message
429 * @type: message type
430 * @payload: length of message payload
431 * @flags: message flags
433 * Returns NULL if the tailroom of the skb is insufficient to store
434 * the message header and payload.
436 static inline struct nlmsghdr
*nlmsg_put(struct sk_buff
*skb
, u32 pid
, u32 seq
,
437 int type
, int payload
, int flags
)
439 if (unlikely(skb_tailroom(skb
) < nlmsg_total_size(payload
)))
442 return __nlmsg_put(skb
, pid
, seq
, type
, payload
, flags
);
446 * nlmsg_put_answer - Add a new callback based netlink message to an skb
447 * @skb: socket buffer to store message in
448 * @cb: netlink callback
449 * @type: message type
450 * @payload: length of message payload
451 * @flags: message flags
453 * Returns NULL if the tailroom of the skb is insufficient to store
454 * the message header and payload.
456 static inline struct nlmsghdr
*nlmsg_put_answer(struct sk_buff
*skb
,
457 struct netlink_callback
*cb
,
458 int type
, int payload
,
461 return nlmsg_put(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
462 type
, payload
, flags
);
466 * nlmsg_new - Allocate a new netlink message
467 * @size: maximum size of message
468 * @flags: the type of memory to allocate.
470 * Use NLMSG_GOODSIZE if size isn't know and you need a good default size.
472 static inline struct sk_buff
*nlmsg_new(int size
, gfp_t flags
)
474 return alloc_skb(size
, flags
);
478 * nlmsg_end - Finalize a netlink message
479 * @skb: socket buffer the message is stored in
480 * @nlh: netlink message header
482 * Corrects the netlink message header to include the appeneded
483 * attributes. Only necessary if attributes have been added to
486 * Returns the total data length of the skb.
488 static inline int nlmsg_end(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
490 nlh
->nlmsg_len
= skb
->tail
- (unsigned char *) nlh
;
496 * nlmsg_get_pos - return current position in netlink message
497 * @skb: socket buffer the message is stored in
499 * Returns a pointer to the current tail of the message.
501 static inline void *nlmsg_get_pos(struct sk_buff
*skb
)
507 * nlmsg_trim - Trim message to a mark
508 * @skb: socket buffer the message is stored in
509 * @mark: mark to trim to
511 * Trims the message to the provided mark. Returns -1.
513 static inline int nlmsg_trim(struct sk_buff
*skb
, void *mark
)
516 skb_trim(skb
, (unsigned char *) mark
- skb
->data
);
522 * nlmsg_cancel - Cancel construction of a netlink message
523 * @skb: socket buffer the message is stored in
524 * @nlh: netlink message header
526 * Removes the complete netlink message including all
527 * attributes from the socket buffer again. Returns -1.
529 static inline int nlmsg_cancel(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
531 return nlmsg_trim(skb
, nlh
);
535 * nlmsg_free - free a netlink message
536 * @skb: socket buffer of netlink message
538 static inline void nlmsg_free(struct sk_buff
*skb
)
544 * nlmsg_multicast - multicast a netlink message
545 * @sk: netlink socket to spread messages to
546 * @skb: netlink message as socket buffer
547 * @pid: own netlink pid to avoid sending to yourself
548 * @group: multicast group id
549 * @flags: allocation flags
551 static inline int nlmsg_multicast(struct sock
*sk
, struct sk_buff
*skb
,
552 u32 pid
, unsigned int group
, gfp_t flags
)
556 NETLINK_CB(skb
).dst_group
= group
;
558 err
= netlink_broadcast(sk
, skb
, pid
, group
, flags
);
566 * nlmsg_unicast - unicast a netlink message
567 * @sk: netlink socket to spread message to
568 * @skb: netlink message as socket buffer
569 * @pid: netlink pid of the destination socket
571 static inline int nlmsg_unicast(struct sock
*sk
, struct sk_buff
*skb
, u32 pid
)
575 err
= netlink_unicast(sk
, skb
, pid
, MSG_DONTWAIT
);
583 * nlmsg_for_each_msg - iterate over a stream of messages
584 * @pos: loop counter, set to current message
585 * @head: head of message stream
586 * @len: length of message stream
587 * @rem: initialized to len, holds bytes currently remaining in stream
589 #define nlmsg_for_each_msg(pos, head, len, rem) \
590 for (pos = head, rem = len; \
591 nlmsg_ok(pos, rem); \
592 pos = nlmsg_next(pos, &(rem)))
594 /**************************************************************************
596 **************************************************************************/
599 * nla_attr_size - length of attribute not including padding
600 * @payload: length of payload
602 static inline int nla_attr_size(int payload
)
604 return NLA_HDRLEN
+ payload
;
608 * nla_total_size - total length of attribute including padding
609 * @payload: length of payload
611 static inline int nla_total_size(int payload
)
613 return NLA_ALIGN(nla_attr_size(payload
));
617 * nla_padlen - length of padding at the tail of attribute
618 * @payload: length of payload
620 static inline int nla_padlen(int payload
)
622 return nla_total_size(payload
) - nla_attr_size(payload
);
626 * nla_data - head of payload
627 * @nla: netlink attribute
629 static inline void *nla_data(const struct nlattr
*nla
)
631 return (char *) nla
+ NLA_HDRLEN
;
635 * nla_len - length of payload
636 * @nla: netlink attribute
638 static inline int nla_len(const struct nlattr
*nla
)
640 return nla
->nla_len
- NLA_HDRLEN
;
644 * nla_ok - check if the netlink attribute fits into the remaining bytes
645 * @nla: netlink attribute
646 * @remaining: number of bytes remaining in attribute stream
648 static inline int nla_ok(const struct nlattr
*nla
, int remaining
)
650 return remaining
>= sizeof(*nla
) &&
651 nla
->nla_len
>= sizeof(*nla
) &&
652 nla
->nla_len
<= remaining
;
656 * nla_next - next netlink attribte in attribute stream
657 * @nla: netlink attribute
658 * @remaining: number of bytes remaining in attribute stream
660 * Returns the next netlink attribute in the attribute stream and
661 * decrements remaining by the size of the current attribute.
663 static inline struct nlattr
*nla_next(const struct nlattr
*nla
, int *remaining
)
665 int totlen
= NLA_ALIGN(nla
->nla_len
);
667 *remaining
-= totlen
;
668 return (struct nlattr
*) ((char *) nla
+ totlen
);
672 * nla_find_nested - find attribute in a set of nested attributes
673 * @nla: attribute containing the nested attributes
674 * @attrtype: type of attribute to look for
676 * Returns the first attribute which matches the specified type.
678 static inline struct nlattr
*nla_find_nested(struct nlattr
*nla
, int attrtype
)
680 return nla_find(nla_data(nla
), nla_len(nla
), attrtype
);
684 * nla_parse_nested - parse nested attributes
685 * @tb: destination array with maxtype+1 elements
686 * @maxtype: maximum attribute type to be expected
687 * @nla: attribute containing the nested attributes
688 * @policy: validation policy
692 static inline int nla_parse_nested(struct nlattr
*tb
[], int maxtype
,
694 struct nla_policy
*policy
)
696 return nla_parse(tb
, maxtype
, nla_data(nla
), nla_len(nla
), policy
);
699 * nla_put_u8 - Add a u16 netlink attribute to a socket buffer
700 * @skb: socket buffer to add attribute to
701 * @attrtype: attribute type
702 * @value: numeric value
704 static inline int nla_put_u8(struct sk_buff
*skb
, int attrtype
, u8 value
)
706 return nla_put(skb
, attrtype
, sizeof(u8
), &value
);
710 * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
711 * @skb: socket buffer to add attribute to
712 * @attrtype: attribute type
713 * @value: numeric value
715 static inline int nla_put_u16(struct sk_buff
*skb
, int attrtype
, u16 value
)
717 return nla_put(skb
, attrtype
, sizeof(u16
), &value
);
721 * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
722 * @skb: socket buffer to add attribute to
723 * @attrtype: attribute type
724 * @value: numeric value
726 static inline int nla_put_u32(struct sk_buff
*skb
, int attrtype
, u32 value
)
728 return nla_put(skb
, attrtype
, sizeof(u32
), &value
);
732 * nla_put_64 - Add a u64 netlink attribute to a socket buffer
733 * @skb: socket buffer to add attribute to
734 * @attrtype: attribute type
735 * @value: numeric value
737 static inline int nla_put_u64(struct sk_buff
*skb
, int attrtype
, u64 value
)
739 return nla_put(skb
, attrtype
, sizeof(u64
), &value
);
743 * nla_put_string - Add a string netlink attribute to a socket buffer
744 * @skb: socket buffer to add attribute to
745 * @attrtype: attribute type
746 * @str: NUL terminated string
748 static inline int nla_put_string(struct sk_buff
*skb
, int attrtype
,
751 return nla_put(skb
, attrtype
, strlen(str
) + 1, str
);
755 * nla_put_flag - Add a flag netlink attribute to a socket buffer
756 * @skb: socket buffer to add attribute to
757 * @attrtype: attribute type
759 static inline int nla_put_flag(struct sk_buff
*skb
, int attrtype
)
761 return nla_put(skb
, attrtype
, 0, NULL
);
765 * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
766 * @skb: socket buffer to add attribute to
767 * @attrtype: attribute type
768 * @jiffies: number of msecs in jiffies
770 static inline int nla_put_msecs(struct sk_buff
*skb
, int attrtype
,
771 unsigned long jiffies
)
773 u64 tmp
= jiffies_to_msecs(jiffies
);
774 return nla_put(skb
, attrtype
, sizeof(u64
), &tmp
);
777 #define NLA_PUT(skb, attrtype, attrlen, data) \
779 if (nla_put(skb, attrtype, attrlen, data) < 0) \
780 goto nla_put_failure; \
783 #define NLA_PUT_TYPE(skb, type, attrtype, value) \
785 type __tmp = value; \
786 NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \
789 #define NLA_PUT_U8(skb, attrtype, value) \
790 NLA_PUT_TYPE(skb, u8, attrtype, value)
792 #define NLA_PUT_U16(skb, attrtype, value) \
793 NLA_PUT_TYPE(skb, u16, attrtype, value)
795 #define NLA_PUT_U32(skb, attrtype, value) \
796 NLA_PUT_TYPE(skb, u32, attrtype, value)
798 #define NLA_PUT_U64(skb, attrtype, value) \
799 NLA_PUT_TYPE(skb, u64, attrtype, value)
801 #define NLA_PUT_STRING(skb, attrtype, value) \
802 NLA_PUT(skb, attrtype, strlen(value) + 1, value)
804 #define NLA_PUT_FLAG(skb, attrtype, value) \
805 NLA_PUT(skb, attrtype, 0, NULL)
807 #define NLA_PUT_MSECS(skb, attrtype, jiffies) \
808 NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies))
811 * nla_get_u32 - return payload of u32 attribute
812 * @nla: u32 netlink attribute
814 static inline u32
nla_get_u32(struct nlattr
*nla
)
816 return *(u32
*) nla_data(nla
);
820 * nla_get_u16 - return payload of u16 attribute
821 * @nla: u16 netlink attribute
823 static inline u16
nla_get_u16(struct nlattr
*nla
)
825 return *(u16
*) nla_data(nla
);
829 * nla_get_u8 - return payload of u8 attribute
830 * @nla: u8 netlink attribute
832 static inline u8
nla_get_u8(struct nlattr
*nla
)
834 return *(u8
*) nla_data(nla
);
838 * nla_get_u64 - return payload of u64 attribute
839 * @nla: u64 netlink attribute
841 static inline u64
nla_get_u64(struct nlattr
*nla
)
845 nla_memcpy(&tmp
, nla
, sizeof(tmp
));
851 * nla_get_flag - return payload of flag attribute
852 * @nla: flag netlink attribute
854 static inline int nla_get_flag(struct nlattr
*nla
)
860 * nla_get_msecs - return payload of msecs attribute
861 * @nla: msecs netlink attribute
863 * Returns the number of milliseconds in jiffies.
865 static inline unsigned long nla_get_msecs(struct nlattr
*nla
)
867 u64 msecs
= nla_get_u64(nla
);
869 return msecs_to_jiffies((unsigned long) msecs
);
873 * nla_nest_start - Start a new level of nested attributes
874 * @skb: socket buffer to add attributes to
875 * @attrtype: attribute type of container
877 * Returns the container attribute
879 static inline struct nlattr
*nla_nest_start(struct sk_buff
*skb
, int attrtype
)
881 struct nlattr
*start
= (struct nlattr
*) skb
->tail
;
883 if (nla_put(skb
, attrtype
, 0, NULL
) < 0)
890 * nla_nest_end - Finalize nesting of attributes
891 * @skb: socket buffer the attribtues are stored in
892 * @start: container attribute
894 * Corrects the container attribute header to include the all
895 * appeneded attributes.
897 * Returns the total data length of the skb.
899 static inline int nla_nest_end(struct sk_buff
*skb
, struct nlattr
*start
)
901 start
->nla_len
= skb
->tail
- (unsigned char *) start
;
906 * nla_nest_cancel - Cancel nesting of attributes
907 * @skb: socket buffer the message is stored in
908 * @start: container attribute
910 * Removes the container attribute and including all nested
911 * attributes. Returns -1.
913 static inline int nla_nest_cancel(struct sk_buff
*skb
, struct nlattr
*start
)
915 return nlmsg_trim(skb
, start
);
919 * nla_for_each_attr - iterate over a stream of attributes
920 * @pos: loop counter, set to current attribute
921 * @head: head of attribute stream
922 * @len: length of attribute stream
923 * @rem: initialized to len, holds bytes currently remaining in stream
925 #define nla_for_each_attr(pos, head, len, rem) \
926 for (pos = head, rem = len; \
928 pos = nla_next(pos, &(rem)))
931 * nla_for_each_nested - iterate over nested attributes
932 * @pos: loop counter, set to current attribute
933 * @nla: attribute containing the nested attributes
934 * @rem: initialized to len, holds bytes currently remaining in stream
936 #define nla_for_each_nested(pos, nla, rem) \
937 nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)