]>
git.proxmox.com Git - mirror_ubuntu-bionic-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
47 * Message Length Calculations:
48 * nlmsg_msg_size(payload) length of message w/o padding
49 * nlmsg_total_size(payload) length of message w/ padding
50 * nlmsg_padlen(payload) length of padding at tail
52 * Message Payload Access:
53 * nlmsg_data(nlh) head of message payload
54 * nlmsg_len(nlh) length of message payload
55 * nlmsg_attrdata(nlh, hdrlen) head of attributes data
56 * nlmsg_attrlen(nlh, hdrlen) length of attributes data
59 * nlmsg_ok(nlh, remaining) does nlh fit into remaining bytes?
60 * nlmsg_next(nlh, remaining) get next netlink message
61 * nlmsg_parse() parse attributes of a message
62 * nlmsg_find_attr() find an attribute in a message
63 * nlmsg_for_each_msg() loop over all messages
64 * nlmsg_validate() validate netlink message incl. attrs
65 * nlmsg_for_each_attr() loop over all attributes
67 * ------------------------------------------------------------------------
68 * Attributes Interface
69 * ------------------------------------------------------------------------
72 * <------- nla_total_size(payload) ------->
73 * <---- nla_attr_size(payload) ----->
74 * +----------+- - -+- - - - - - - - - +- - -+-------- - -
75 * | Header | Pad | Payload | Pad | Header
76 * +----------+- - -+- - - - - - - - - +- - -+-------- - -
77 * <- nla_len(nla) -> ^
78 * nla_data(nla)----^ |
79 * nla_next(nla)-----------------------------'
82 * struct nlattr netlink attribtue header
84 * Attribute Construction:
85 * nla_reserve(skb, type, len) reserve room for an attribute
86 * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr
87 * nla_put(skb, type, len, data) add attribute to skb
88 * nla_put_nohdr(skb, len, data) add attribute w/o hdr
90 * Attribute Construction for Basic Types:
91 * nla_put_u8(skb, type, value) add u8 attribute to skb
92 * nla_put_u16(skb, type, value) add u16 attribute to skb
93 * nla_put_u32(skb, type, value) add u32 attribute to skb
94 * nla_put_u64(skb, type, value) add u64 attribute to skb
95 * nla_put_string(skb, type, str) add string attribute to skb
96 * nla_put_flag(skb, type) add flag attribute to skb
97 * nla_put_msecs(skb, type, jiffies) add msecs attribute to skb
99 * Exceptions Based Attribute Construction:
100 * NLA_PUT(skb, type, len, data) add attribute to skb
101 * NLA_PUT_U8(skb, type, value) add u8 attribute to skb
102 * NLA_PUT_U16(skb, type, value) add u16 attribute to skb
103 * NLA_PUT_U32(skb, type, value) add u32 attribute to skb
104 * NLA_PUT_U64(skb, type, value) add u64 attribute to skb
105 * NLA_PUT_STRING(skb, type, str) add string attribute to skb
106 * NLA_PUT_FLAG(skb, type) add flag attribute to skb
107 * NLA_PUT_MSECS(skb, type, jiffies) add msecs attribute to skb
109 * The meaning of these functions is equal to their lower case
110 * variants but they jump to the label nla_put_failure in case
113 * Nested Attributes Construction:
114 * nla_nest_start(skb, type) start a nested attribute
115 * nla_nest_end(skb, nla) finalize a nested attribute
116 * nla_nest_cancel(skb, nla) cancel nested attribute construction
118 * Attribute Length Calculations:
119 * nla_attr_size(payload) length of attribute w/o padding
120 * nla_total_size(payload) length of attribute w/ padding
121 * nla_padlen(payload) length of padding
123 * Attribute Payload Access:
124 * nla_data(nla) head of attribute payload
125 * nla_len(nla) length of attribute payload
127 * Attribute Payload Access for Basic Types:
128 * nla_get_u8(nla) get payload for a u8 attribute
129 * nla_get_u16(nla) get payload for a u16 attribute
130 * nla_get_u32(nla) get payload for a u32 attribute
131 * nla_get_u64(nla) get payload for a u64 attribute
132 * nla_get_flag(nla) return 1 if flag is true
133 * nla_get_msecs(nla) get payload for a msecs attribute
136 * nla_memcpy(dest, nla, count) copy attribute into memory
137 * nla_memcmp(nla, data, size) compare attribute with memory area
138 * nla_strlcpy(dst, nla, size) copy attribute to a sized string
139 * nla_strcmp(nla, str) compare attribute with string
142 * nla_ok(nla, remaining) does nla fit into remaining bytes?
143 * nla_next(nla, remaining) get next netlink attribute
144 * nla_validate() validate a stream of attributes
145 * nla_find() find attribute in stream of attributes
146 * nla_find_nested() find attribute in nested attributes
147 * nla_parse() parse and validate stream of attrs
148 * nla_parse_nested() parse nested attribuets
149 * nla_for_each_attr() loop over all attributes
150 *=========================================================================
154 * Standard attribute types to specify validation policy
169 #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
172 * struct nla_policy - attribute validation policy
173 * @type: Type of attribute or NLA_UNSPEC
174 * @minlen: Minimal length of payload required to be available
176 * Policies are defined as arrays of this struct, the array must be
177 * accessible by attribute type up to the highest identifier to be expected.
180 * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = {
181 * [ATTR_FOO] = { .type = NLA_U16 },
182 * [ATTR_BAR] = { .type = NLA_STRING },
183 * [ATTR_BAZ] = { .minlen = sizeof(struct mystruct) },
191 extern void netlink_run_queue(struct sock
*sk
, unsigned int *qlen
,
192 int (*cb
)(struct sk_buff
*,
193 struct nlmsghdr
*, int *));
194 extern void netlink_queue_skip(struct nlmsghdr
*nlh
,
195 struct sk_buff
*skb
);
197 extern int nla_validate(struct nlattr
*head
, int len
, int maxtype
,
198 struct nla_policy
*policy
);
199 extern int nla_parse(struct nlattr
*tb
[], int maxtype
,
200 struct nlattr
*head
, int len
,
201 struct nla_policy
*policy
);
202 extern struct nlattr
* nla_find(struct nlattr
*head
, int len
, int attrtype
);
203 extern size_t nla_strlcpy(char *dst
, const struct nlattr
*nla
,
205 extern int nla_memcpy(void *dest
, struct nlattr
*src
, int count
);
206 extern int nla_memcmp(const struct nlattr
*nla
, const void *data
,
208 extern int nla_strcmp(const struct nlattr
*nla
, const char *str
);
209 extern struct nlattr
* __nla_reserve(struct sk_buff
*skb
, int attrtype
,
211 extern void * __nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
);
212 extern struct nlattr
* nla_reserve(struct sk_buff
*skb
, int attrtype
,
214 extern void * nla_reserve_nohdr(struct sk_buff
*skb
, int attrlen
);
215 extern void __nla_put(struct sk_buff
*skb
, int attrtype
,
216 int attrlen
, const void *data
);
217 extern void __nla_put_nohdr(struct sk_buff
*skb
, int attrlen
,
219 extern int nla_put(struct sk_buff
*skb
, int attrtype
,
220 int attrlen
, const void *data
);
221 extern int nla_put_nohdr(struct sk_buff
*skb
, int attrlen
,
224 /**************************************************************************
226 **************************************************************************/
229 * nlmsg_msg_size - length of netlink message not including padding
230 * @payload: length of message payload
232 static inline int nlmsg_msg_size(int payload
)
234 return NLMSG_HDRLEN
+ payload
;
238 * nlmsg_total_size - length of netlink message including padding
239 * @payload: length of message payload
241 static inline int nlmsg_total_size(int payload
)
243 return NLMSG_ALIGN(nlmsg_msg_size(payload
));
247 * nlmsg_padlen - length of padding at the message's tail
248 * @payload: length of message payload
250 static inline int nlmsg_padlen(int payload
)
252 return nlmsg_total_size(payload
) - nlmsg_msg_size(payload
);
256 * nlmsg_data - head of message payload
257 * @nlh: netlink messsage header
259 static inline void *nlmsg_data(const struct nlmsghdr
*nlh
)
261 return (unsigned char *) nlh
+ NLMSG_HDRLEN
;
265 * nlmsg_len - length of message payload
266 * @nlh: netlink message header
268 static inline int nlmsg_len(const struct nlmsghdr
*nlh
)
270 return nlh
->nlmsg_len
- NLMSG_HDRLEN
;
274 * nlmsg_attrdata - head of attributes data
275 * @nlh: netlink message header
276 * @hdrlen: length of family specific header
278 static inline struct nlattr
*nlmsg_attrdata(const struct nlmsghdr
*nlh
,
281 unsigned char *data
= nlmsg_data(nlh
);
282 return (struct nlattr
*) (data
+ NLMSG_ALIGN(hdrlen
));
286 * nlmsg_attrlen - length of attributes data
287 * @nlh: netlink message header
288 * @hdrlen: length of family specific header
290 static inline int nlmsg_attrlen(const struct nlmsghdr
*nlh
, int hdrlen
)
292 return nlmsg_len(nlh
) - NLMSG_ALIGN(hdrlen
);
296 * nlmsg_ok - check if the netlink message fits into the remaining bytes
297 * @nlh: netlink message header
298 * @remaining: number of bytes remaining in message stream
300 static inline int nlmsg_ok(const struct nlmsghdr
*nlh
, int remaining
)
302 return (remaining
>= sizeof(struct nlmsghdr
) &&
303 nlh
->nlmsg_len
>= sizeof(struct nlmsghdr
) &&
304 nlh
->nlmsg_len
<= remaining
);
308 * nlmsg_next - next netlink message in message stream
309 * @nlh: netlink message header
310 * @remaining: number of bytes remaining in message stream
312 * Returns the next netlink message in the message stream and
313 * decrements remaining by the size of the current message.
315 static inline struct nlmsghdr
*nlmsg_next(struct nlmsghdr
*nlh
, int *remaining
)
317 int totlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
319 *remaining
-= totlen
;
321 return (struct nlmsghdr
*) ((unsigned char *) nlh
+ totlen
);
325 * nlmsg_parse - parse attributes of a netlink message
326 * @nlh: netlink message header
327 * @hdrlen: length of family specific header
328 * @tb: destination array with maxtype+1 elements
329 * @maxtype: maximum attribute type to be expected
330 * @policy: validation policy
334 static inline int nlmsg_parse(struct nlmsghdr
*nlh
, int hdrlen
,
335 struct nlattr
*tb
[], int maxtype
,
336 struct nla_policy
*policy
)
338 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
341 return nla_parse(tb
, maxtype
, nlmsg_attrdata(nlh
, hdrlen
),
342 nlmsg_attrlen(nlh
, hdrlen
), policy
);
346 * nlmsg_find_attr - find a specific attribute in a netlink message
347 * @nlh: netlink message header
348 * @hdrlen: length of familiy specific header
349 * @attrtype: type of attribute to look for
351 * Returns the first attribute which matches the specified type.
353 static inline struct nlattr
*nlmsg_find_attr(struct nlmsghdr
*nlh
,
354 int hdrlen
, int attrtype
)
356 return nla_find(nlmsg_attrdata(nlh
, hdrlen
),
357 nlmsg_attrlen(nlh
, hdrlen
), attrtype
);
361 * nlmsg_validate - validate a netlink message including attributes
362 * @nlh: netlinket message header
363 * @hdrlen: length of familiy specific header
364 * @maxtype: maximum attribute type to be expected
365 * @policy: validation policy
367 static inline int nlmsg_validate(struct nlmsghdr
*nlh
, int hdrlen
, int maxtype
,
368 struct nla_policy
*policy
)
370 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
373 return nla_validate(nlmsg_attrdata(nlh
, hdrlen
),
374 nlmsg_attrlen(nlh
, hdrlen
), maxtype
, policy
);
378 * nlmsg_for_each_attr - iterate over a stream of attributes
379 * @pos: loop counter, set to current attribute
380 * @nlh: netlink message header
381 * @hdrlen: length of familiy specific header
382 * @rem: initialized to len, holds bytes currently remaining in stream
384 #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
385 nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
386 nlmsg_attrlen(nlh, hdrlen), rem)
389 /* FIXME: Enable once all users have been converted */
392 * __nlmsg_put - Add a new netlink message to an skb
393 * @skb: socket buffer to store message in
394 * @pid: netlink process id
395 * @seq: sequence number of message
396 * @type: message type
397 * @payload: length of message payload
398 * @flags: message flags
400 * The caller is responsible to ensure that the skb provides enough
401 * tailroom for both the netlink header and payload.
403 static inline struct nlmsghdr
*__nlmsg_put(struct sk_buff
*skb
, u32 pid
,
404 u32 seq
, int type
, int payload
,
407 struct nlmsghdr
*nlh
;
409 nlh
= (struct nlmsghdr
*) skb_put(skb
, nlmsg_total_size(payload
));
410 nlh
->nlmsg_type
= type
;
411 nlh
->nlmsg_len
= nlmsg_msg_size(payload
);
412 nlh
->nlmsg_flags
= flags
;
413 nlh
->nlmsg_pid
= pid
;
414 nlh
->nlmsg_seq
= seq
;
416 memset((unsigned char *) nlmsg_data(nlh
) + payload
, 0,
417 nlmsg_padlen(payload
));
424 * nlmsg_put - Add a new netlink message to an skb
425 * @skb: socket buffer to store message in
426 * @pid: netlink process id
427 * @seq: sequence number of message
428 * @type: message type
429 * @payload: length of message payload
430 * @flags: message flags
432 * Returns NULL if the tailroom of the skb is insufficient to store
433 * the message header and payload.
435 static inline struct nlmsghdr
*nlmsg_put(struct sk_buff
*skb
, u32 pid
, u32 seq
,
436 int type
, int payload
, int flags
)
438 if (unlikely(skb_tailroom(skb
) < nlmsg_total_size(payload
)))
441 return __nlmsg_put(skb
, pid
, seq
, type
, payload
, flags
);
445 * nlmsg_put_answer - Add a new callback based netlink message to an skb
446 * @skb: socket buffer to store message in
447 * @cb: netlink callback
448 * @type: message type
449 * @payload: length of message payload
450 * @flags: message flags
452 * Returns NULL if the tailroom of the skb is insufficient to store
453 * the message header and payload.
455 static inline struct nlmsghdr
*nlmsg_put_answer(struct sk_buff
*skb
,
456 struct netlink_callback
*cb
,
457 int type
, int payload
,
460 return nlmsg_put(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
461 type
, payload
, flags
);
465 * nlmsg_new - Allocate a new netlink message
466 * @size: maximum size of message
467 * @flags: the type of memory to allocate.
469 * Use NLMSG_GOODSIZE if size isn't know and you need a good default size.
471 static inline struct sk_buff
*nlmsg_new(int size
, gfp_t flags
)
473 return alloc_skb(size
, flags
);
477 * nlmsg_end - Finalize a netlink message
478 * @skb: socket buffer the message is stored in
479 * @nlh: netlink message header
481 * Corrects the netlink message header to include the appeneded
482 * attributes. Only necessary if attributes have been added to
485 * Returns the total data length of the skb.
487 static inline int nlmsg_end(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
489 nlh
->nlmsg_len
= skb
->tail
- (unsigned char *) nlh
;
495 * nlmsg_get_pos - return current position in netlink message
496 * @skb: socket buffer the message is stored in
498 * Returns a pointer to the current tail of the message.
500 static inline void *nlmsg_get_pos(struct sk_buff
*skb
)
506 * nlmsg_trim - Trim message to a mark
507 * @skb: socket buffer the message is stored in
508 * @mark: mark to trim to
510 * Trims the message to the provided mark. Returns -1.
512 static inline int nlmsg_trim(struct sk_buff
*skb
, void *mark
)
515 skb_trim(skb
, (unsigned char *) mark
- skb
->data
);
521 * nlmsg_cancel - Cancel construction of a netlink message
522 * @skb: socket buffer the message is stored in
523 * @nlh: netlink message header
525 * Removes the complete netlink message including all
526 * attributes from the socket buffer again. Returns -1.
528 static inline int nlmsg_cancel(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
530 return nlmsg_trim(skb
, nlh
);
534 * nlmsg_free - free a netlink message
535 * @skb: socket buffer of netlink message
537 static inline void nlmsg_free(struct sk_buff
*skb
)
543 * nlmsg_multicast - multicast a netlink message
544 * @sk: netlink socket to spread messages to
545 * @skb: netlink message as socket buffer
546 * @pid: own netlink pid to avoid sending to yourself
547 * @group: multicast group id
549 static inline int nlmsg_multicast(struct sock
*sk
, struct sk_buff
*skb
,
550 u32 pid
, unsigned int group
)
554 NETLINK_CB(skb
).dst_group
= group
;
556 err
= netlink_broadcast(sk
, skb
, pid
, group
, GFP_KERNEL
);
564 * nlmsg_unicast - unicast a netlink message
565 * @sk: netlink socket to spread message to
566 * @skb: netlink message as socket buffer
567 * @pid: netlink pid of the destination socket
569 static inline int nlmsg_unicast(struct sock
*sk
, struct sk_buff
*skb
, u32 pid
)
573 err
= netlink_unicast(sk
, skb
, pid
, MSG_DONTWAIT
);
581 * nlmsg_for_each_msg - iterate over a stream of messages
582 * @pos: loop counter, set to current message
583 * @head: head of message stream
584 * @len: length of message stream
585 * @rem: initialized to len, holds bytes currently remaining in stream
587 #define nlmsg_for_each_msg(pos, head, len, rem) \
588 for (pos = head, rem = len; \
589 nlmsg_ok(pos, rem); \
590 pos = nlmsg_next(pos, &(rem)))
592 /**************************************************************************
594 **************************************************************************/
597 * nla_attr_size - length of attribute not including padding
598 * @payload: length of payload
600 static inline int nla_attr_size(int payload
)
602 return NLA_HDRLEN
+ payload
;
606 * nla_total_size - total length of attribute including padding
607 * @payload: length of payload
609 static inline int nla_total_size(int payload
)
611 return NLA_ALIGN(nla_attr_size(payload
));
615 * nla_padlen - length of padding at the tail of attribute
616 * @payload: length of payload
618 static inline int nla_padlen(int payload
)
620 return nla_total_size(payload
) - nla_attr_size(payload
);
624 * nla_data - head of payload
625 * @nla: netlink attribute
627 static inline void *nla_data(const struct nlattr
*nla
)
629 return (char *) nla
+ NLA_HDRLEN
;
633 * nla_len - length of payload
634 * @nla: netlink attribute
636 static inline int nla_len(const struct nlattr
*nla
)
638 return nla
->nla_len
- NLA_HDRLEN
;
642 * nla_ok - check if the netlink attribute fits into the remaining bytes
643 * @nla: netlink attribute
644 * @remaining: number of bytes remaining in attribute stream
646 static inline int nla_ok(const struct nlattr
*nla
, int remaining
)
648 return remaining
>= sizeof(*nla
) &&
649 nla
->nla_len
>= sizeof(*nla
) &&
650 nla
->nla_len
<= remaining
;
654 * nla_next - next netlink attribte in attribute stream
655 * @nla: netlink attribute
656 * @remaining: number of bytes remaining in attribute stream
658 * Returns the next netlink attribute in the attribute stream and
659 * decrements remaining by the size of the current attribute.
661 static inline struct nlattr
*nla_next(const struct nlattr
*nla
, int *remaining
)
663 int totlen
= NLA_ALIGN(nla
->nla_len
);
665 *remaining
-= totlen
;
666 return (struct nlattr
*) ((char *) nla
+ totlen
);
670 * nla_find_nested - find attribute in a set of nested attributes
671 * @nla: attribute containing the nested attributes
672 * @attrtype: type of attribute to look for
674 * Returns the first attribute which matches the specified type.
676 static inline struct nlattr
*nla_find_nested(struct nlattr
*nla
, int attrtype
)
678 return nla_find(nla_data(nla
), nla_len(nla
), attrtype
);
682 * nla_parse_nested - parse nested attributes
683 * @tb: destination array with maxtype+1 elements
684 * @maxtype: maximum attribute type to be expected
685 * @nla: attribute containing the nested attributes
686 * @policy: validation policy
690 static inline int nla_parse_nested(struct nlattr
*tb
[], int maxtype
,
692 struct nla_policy
*policy
)
694 return nla_parse(tb
, maxtype
, nla_data(nla
), nla_len(nla
), policy
);
697 * nla_put_u8 - Add a u16 netlink attribute to a socket buffer
698 * @skb: socket buffer to add attribute to
699 * @attrtype: attribute type
700 * @value: numeric value
702 static inline int nla_put_u8(struct sk_buff
*skb
, int attrtype
, u8 value
)
704 return nla_put(skb
, attrtype
, sizeof(u8
), &value
);
708 * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
709 * @skb: socket buffer to add attribute to
710 * @attrtype: attribute type
711 * @value: numeric value
713 static inline int nla_put_u16(struct sk_buff
*skb
, int attrtype
, u16 value
)
715 return nla_put(skb
, attrtype
, sizeof(u16
), &value
);
719 * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
720 * @skb: socket buffer to add attribute to
721 * @attrtype: attribute type
722 * @value: numeric value
724 static inline int nla_put_u32(struct sk_buff
*skb
, int attrtype
, u32 value
)
726 return nla_put(skb
, attrtype
, sizeof(u32
), &value
);
730 * nla_put_64 - Add a u64 netlink attribute to a socket buffer
731 * @skb: socket buffer to add attribute to
732 * @attrtype: attribute type
733 * @value: numeric value
735 static inline int nla_put_u64(struct sk_buff
*skb
, int attrtype
, u64 value
)
737 return nla_put(skb
, attrtype
, sizeof(u64
), &value
);
741 * nla_put_string - Add a string netlink attribute to a socket buffer
742 * @skb: socket buffer to add attribute to
743 * @attrtype: attribute type
744 * @str: NUL terminated string
746 static inline int nla_put_string(struct sk_buff
*skb
, int attrtype
,
749 return nla_put(skb
, attrtype
, strlen(str
) + 1, str
);
753 * nla_put_flag - Add a flag netlink attribute to a socket buffer
754 * @skb: socket buffer to add attribute to
755 * @attrtype: attribute type
757 static inline int nla_put_flag(struct sk_buff
*skb
, int attrtype
)
759 return nla_put(skb
, attrtype
, 0, NULL
);
763 * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
764 * @skb: socket buffer to add attribute to
765 * @attrtype: attribute type
766 * @jiffies: number of msecs in jiffies
768 static inline int nla_put_msecs(struct sk_buff
*skb
, int attrtype
,
769 unsigned long jiffies
)
771 u64 tmp
= jiffies_to_msecs(jiffies
);
772 return nla_put(skb
, attrtype
, sizeof(u64
), &tmp
);
775 #define NLA_PUT(skb, attrtype, attrlen, data) \
777 if (nla_put(skb, attrtype, attrlen, data) < 0) \
778 goto nla_put_failure; \
781 #define NLA_PUT_TYPE(skb, type, attrtype, value) \
783 type __tmp = value; \
784 NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \
787 #define NLA_PUT_U8(skb, attrtype, value) \
788 NLA_PUT_TYPE(skb, u8, attrtype, value)
790 #define NLA_PUT_U16(skb, attrtype, value) \
791 NLA_PUT_TYPE(skb, u16, attrtype, value)
793 #define NLA_PUT_U32(skb, attrtype, value) \
794 NLA_PUT_TYPE(skb, u32, attrtype, value)
796 #define NLA_PUT_U64(skb, attrtype, value) \
797 NLA_PUT_TYPE(skb, u64, attrtype, value)
799 #define NLA_PUT_STRING(skb, attrtype, value) \
800 NLA_PUT(skb, attrtype, strlen(value) + 1, value)
802 #define NLA_PUT_FLAG(skb, attrtype, value) \
803 NLA_PUT(skb, attrtype, 0, NULL)
805 #define NLA_PUT_MSECS(skb, attrtype, jiffies) \
806 NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies))
809 * nla_get_u32 - return payload of u32 attribute
810 * @nla: u32 netlink attribute
812 static inline u32
nla_get_u32(struct nlattr
*nla
)
814 return *(u32
*) nla_data(nla
);
818 * nla_get_u16 - return payload of u16 attribute
819 * @nla: u16 netlink attribute
821 static inline u16
nla_get_u16(struct nlattr
*nla
)
823 return *(u16
*) nla_data(nla
);
827 * nla_get_u8 - return payload of u8 attribute
828 * @nla: u8 netlink attribute
830 static inline u8
nla_get_u8(struct nlattr
*nla
)
832 return *(u8
*) nla_data(nla
);
836 * nla_get_u64 - return payload of u64 attribute
837 * @nla: u64 netlink attribute
839 static inline u64
nla_get_u64(struct nlattr
*nla
)
843 nla_memcpy(&tmp
, nla
, sizeof(tmp
));
849 * nla_get_flag - return payload of flag attribute
850 * @nla: flag netlink attribute
852 static inline int nla_get_flag(struct nlattr
*nla
)
858 * nla_get_msecs - return payload of msecs attribute
859 * @nla: msecs netlink attribute
861 * Returns the number of milliseconds in jiffies.
863 static inline unsigned long nla_get_msecs(struct nlattr
*nla
)
865 u64 msecs
= nla_get_u64(nla
);
867 return msecs_to_jiffies((unsigned long) msecs
);
871 * nla_nest_start - Start a new level of nested attributes
872 * @skb: socket buffer to add attributes to
873 * @attrtype: attribute type of container
875 * Returns the container attribute
877 static inline struct nlattr
*nla_nest_start(struct sk_buff
*skb
, int attrtype
)
879 struct nlattr
*start
= (struct nlattr
*) skb
->tail
;
881 if (nla_put(skb
, attrtype
, 0, NULL
) < 0)
888 * nla_nest_end - Finalize nesting of attributes
889 * @skb: socket buffer the attribtues are stored in
890 * @start: container attribute
892 * Corrects the container attribute header to include the all
893 * appeneded attributes.
895 * Returns the total data length of the skb.
897 static inline int nla_nest_end(struct sk_buff
*skb
, struct nlattr
*start
)
899 start
->nla_len
= skb
->tail
- (unsigned char *) start
;
904 * nla_nest_cancel - Cancel nesting of attributes
905 * @skb: socket buffer the message is stored in
906 * @start: container attribute
908 * Removes the container attribute and including all nested
909 * attributes. Returns -1.
911 static inline int nla_nest_cancel(struct sk_buff
*skb
, struct nlattr
*start
)
913 return nlmsg_trim(skb
, start
);
917 * nla_for_each_attr - iterate over a stream of attributes
918 * @pos: loop counter, set to current attribute
919 * @head: head of attribute stream
920 * @len: length of attribute stream
921 * @rem: initialized to len, holds bytes currently remaining in stream
923 #define nla_for_each_attr(pos, head, len, rem) \
924 for (pos = head, rem = len; \
926 pos = nla_next(pos, &(rem)))
929 * nla_for_each_nested - iterate over nested attributes
930 * @pos: loop counter, set to current attribute
931 * @nla: attribute containing the nested attributes
932 * @rem: initialized to len, holds bytes currently remaining in stream
934 #define nla_for_each_nested(pos, nla, rem) \
935 nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)