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 * nla_for_each_nested() loop over the nested attributes
155 *=========================================================================
159 * Standard attribute types to specify validation policy
175 #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
178 * struct nla_policy - attribute validation policy
179 * @type: Type of attribute or NLA_UNSPEC
180 * @len: Type specific length of payload
182 * Policies are defined as arrays of this struct, the array must be
183 * accessible by attribute type up to the highest identifier to be expected.
185 * Meaning of `len' field:
186 * NLA_STRING Maximum length of string
187 * NLA_NUL_STRING Maximum length of string (excluding NUL)
189 * All other Exact length of attribute payload
192 * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = {
193 * [ATTR_FOO] = { .type = NLA_U16 },
194 * [ATTR_BAR] = { .type = NLA_STRING, len = BARSIZ },
195 * [ATTR_BAZ] = { .len = sizeof(struct mystruct) },
204 * struct nl_info - netlink source information
205 * @nlh: Netlink message header of original request
206 * @pid: Netlink PID of requesting application
209 struct nlmsghdr *nlh;
213 extern void netlink_run_queue(struct sock *sk, unsigned int *qlen,
214 int (*cb)(struct sk_buff *,
215 struct nlmsghdr *, int *));
216 extern void netlink_queue_skip(struct nlmsghdr *nlh,
217 struct sk_buff *skb);
218 extern int nlmsg_notify(struct sock *sk, struct sk_buff *skb,
219 u32 pid, unsigned int group, int report,
222 extern int nla_validate(struct nlattr *head, int len, int maxtype,
223 struct nla_policy *policy);
224 extern int nla_parse(struct nlattr *tb[], int maxtype,
225 struct nlattr *head, int len,
226 struct nla_policy *policy);
227 extern struct nlattr * nla_find(struct nlattr *head, int len, int attrtype);
228 extern size_t nla_strlcpy(char *dst, const struct nlattr *nla,
230 extern int nla_memcpy(void *dest, struct nlattr *src, int count);
231 extern int nla_memcmp(const struct nlattr *nla, const void *data,
233 extern int nla_strcmp(const struct nlattr *nla, const char *str);
234 extern struct nlattr * __nla_reserve(struct sk_buff *skb, int attrtype,
236 extern void * __nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
237 extern struct nlattr * nla_reserve(struct sk_buff *skb, int attrtype,
239 extern void * nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
240 extern void __nla_put(struct sk_buff *skb, int attrtype,
241 int attrlen, const void *data);
242 extern void __nla_put_nohdr(struct sk_buff *skb, int attrlen,
244 extern int nla_put(struct sk_buff *skb, int attrtype,
245 int attrlen, const void *data);
246 extern int nla_put_nohdr(struct sk_buff *skb, int attrlen,
249 /**************************************************************************
251 **************************************************************************/
254 * nlmsg_msg_size - length of netlink message not including padding
255 * @payload: length of message payload
257 static inline int nlmsg_msg_size(int payload)
259 return NLMSG_HDRLEN + payload;
263 * nlmsg_total_size - length of netlink message including padding
264 * @payload: length of message payload
266 static inline int nlmsg_total_size(int payload)
268 return NLMSG_ALIGN(nlmsg_msg_size(payload));
272 * nlmsg_padlen - length of padding at the message's tail
273 * @payload: length of message payload
275 static inline int nlmsg_padlen(int payload)
277 return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
281 * nlmsg_data - head of message payload
282 * @nlh: netlink messsage header
284 static inline void *nlmsg_data(const struct nlmsghdr *nlh)
286 return (unsigned char *) nlh + NLMSG_HDRLEN;
290 * nlmsg_len - length of message payload
291 * @nlh: netlink message header
293 static inline int nlmsg_len(const struct nlmsghdr *nlh)
295 return nlh->nlmsg_len - NLMSG_HDRLEN;
299 * nlmsg_attrdata - head of attributes data
300 * @nlh: netlink message header
301 * @hdrlen: length of family specific header
303 static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
306 unsigned char *data = nlmsg_data(nlh);
307 return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
311 * nlmsg_attrlen - length of attributes data
312 * @nlh: netlink message header
313 * @hdrlen: length of family specific header
315 static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
317 return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
321 * nlmsg_ok - check if the netlink message fits into the remaining bytes
322 * @nlh: netlink message header
323 * @remaining: number of bytes remaining in message stream
325 static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
327 return (remaining >= sizeof(struct nlmsghdr) &&
328 nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
329 nlh->nlmsg_len <= remaining);
333 * nlmsg_next - next netlink message in message stream
334 * @nlh: netlink message header
335 * @remaining: number of bytes remaining in message stream
337 * Returns the next netlink message in the message stream and
338 * decrements remaining by the size of the current message.
340 static inline struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
342 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
344 *remaining -= totlen;
346 return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
350 * nlmsg_parse - parse attributes of a netlink message
351 * @nlh: netlink message header
352 * @hdrlen: length of family specific header
353 * @tb: destination array with maxtype+1 elements
354 * @maxtype: maximum attribute type to be expected
355 * @policy: validation policy
359 static inline int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen,
360 struct nlattr *tb[], int maxtype,
361 struct nla_policy *policy)
363 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
366 return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
367 nlmsg_attrlen(nlh, hdrlen), policy);
371 * nlmsg_find_attr - find a specific attribute in a netlink message
372 * @nlh: netlink message header
373 * @hdrlen: length of familiy specific header
374 * @attrtype: type of attribute to look for
376 * Returns the first attribute which matches the specified type.
378 static inline struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh,
379 int hdrlen, int attrtype)
381 return nla_find(nlmsg_attrdata(nlh, hdrlen),
382 nlmsg_attrlen(nlh, hdrlen), attrtype);
386 * nlmsg_validate - validate a netlink message including attributes
387 * @nlh: netlinket message header
388 * @hdrlen: length of familiy specific header
389 * @maxtype: maximum attribute type to be expected
390 * @policy: validation policy
392 static inline int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
393 struct nla_policy *policy)
395 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
398 return nla_validate(nlmsg_attrdata(nlh, hdrlen),
399 nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
403 * nlmsg_report - need to report back to application?
404 * @nlh: netlink message header
406 * Returns 1 if a report back to the application is requested.
408 static inline int nlmsg_report(struct nlmsghdr *nlh)
410 return !!(nlh->nlmsg_flags & NLM_F_ECHO);
414 * nlmsg_for_each_attr - iterate over a stream of attributes
415 * @pos: loop counter, set to current attribute
416 * @nlh: netlink message header
417 * @hdrlen: length of familiy specific header
418 * @rem: initialized to len, holds bytes currently remaining in stream
420 #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
421 nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
422 nlmsg_attrlen(nlh, hdrlen), rem)
425 /* FIXME: Enable once all users have been converted */
428 * __nlmsg_put - Add a new netlink message to an skb
429 * @skb: socket buffer to store message in
430 * @pid: netlink process id
431 * @seq: sequence number of message
432 * @type: message type
433 * @payload: length of message payload
434 * @flags: message flags
436 * The caller is responsible to ensure that the skb provides enough
437 * tailroom for both the netlink header and payload.
439 static inline struct nlmsghdr *__nlmsg_put(struct sk_buff *skb, u32 pid,
440 u32 seq, int type, int payload,
443 struct nlmsghdr *nlh;
445 nlh = (struct nlmsghdr *) skb_put(skb, nlmsg_total_size(payload));
446 nlh->nlmsg_type = type;
447 nlh->nlmsg_len = nlmsg_msg_size(payload);
448 nlh->nlmsg_flags = flags;
449 nlh->nlmsg_pid = pid;
450 nlh->nlmsg_seq = seq;
452 memset((unsigned char *) nlmsg_data(nlh) + payload, 0,
453 nlmsg_padlen(payload));
460 * nlmsg_put - Add a new netlink message to an skb
461 * @skb: socket buffer to store message in
462 * @pid: netlink process id
463 * @seq: sequence number of message
464 * @type: message type
465 * @payload: length of message payload
466 * @flags: message flags
468 * Returns NULL if the tailroom of the skb is insufficient to store
469 * the message header and payload.
471 static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
472 int type, int payload, int flags)
474 if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
477 return __nlmsg_put(skb, pid, seq, type, payload, flags);
481 * nlmsg_put_answer - Add a new callback based netlink message to an skb
482 * @skb: socket buffer to store message in
483 * @cb: netlink callback
484 * @type: message type
485 * @payload: length of message payload
486 * @flags: message flags
488 * Returns NULL if the tailroom of the skb is insufficient to store
489 * the message header and payload.
491 static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
492 struct netlink_callback *cb,
493 int type, int payload,
496 return nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
497 type, payload, flags);
501 * nlmsg_new - Allocate a new netlink message
502 * @size: maximum size of message
503 * @flags: the type of memory to allocate.
505 * Use NLMSG_GOODSIZE if size isn't know and you need a good default size.
507 static inline struct sk_buff *nlmsg_new(int size, gfp_t flags)
509 return alloc_skb(size, flags);
513 * nlmsg_end - Finalize a netlink message
514 * @skb: socket buffer the message is stored in
515 * @nlh: netlink message header
517 * Corrects the netlink message header to include the appeneded
518 * attributes. Only necessary if attributes have been added to
521 * Returns the total data length of the skb.
523 static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
525 nlh->nlmsg_len = skb->tail - (unsigned char *) nlh;
531 * nlmsg_get_pos - return current position in netlink message
532 * @skb: socket buffer the message is stored in
534 * Returns a pointer to the current tail of the message.
536 static inline void *nlmsg_get_pos(struct sk_buff *skb)
542 * nlmsg_trim - Trim message to a mark
543 * @skb: socket buffer the message is stored in
544 * @mark: mark to trim to
546 * Trims the message to the provided mark. Returns -1.
548 static inline int nlmsg_trim(struct sk_buff *skb, void *mark)
551 skb_trim(skb, (unsigned char *) mark - skb->data);
557 * nlmsg_cancel - Cancel construction of a netlink message
558 * @skb: socket buffer the message is stored in
559 * @nlh: netlink message header
561 * Removes the complete netlink message including all
562 * attributes from the socket buffer again. Returns -1.
564 static inline int nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
566 return nlmsg_trim(skb, nlh);
570 * nlmsg_free - free a netlink message
571 * @skb: socket buffer of netlink message
573 static inline void nlmsg_free(struct sk_buff *skb)
579 * nlmsg_multicast - multicast a netlink message
580 * @sk: netlink socket to spread messages to
581 * @skb: netlink message as socket buffer
582 * @pid: own netlink pid to avoid sending to yourself
583 * @group: multicast group id
584 * @flags: allocation flags
586 static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
587 u32 pid, unsigned int group, gfp_t flags)
591 NETLINK_CB(skb).dst_group = group;
593 err = netlink_broadcast(sk, skb, pid, group, flags);
601 * nlmsg_unicast - unicast a netlink message
602 * @sk: netlink socket to spread message to
603 * @skb: netlink message as socket buffer
604 * @pid: netlink pid of the destination socket
606 static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 pid)
610 err = netlink_unicast(sk, skb, pid, MSG_DONTWAIT);
618 * nlmsg_for_each_msg - iterate over a stream of messages
619 * @pos: loop counter, set to current message
620 * @head: head of message stream
621 * @len: length of message stream
622 * @rem: initialized to len, holds bytes currently remaining in stream
624 #define nlmsg_for_each_msg(pos, head, len, rem) \
625 for (pos = head, rem = len; \
626 nlmsg_ok(pos, rem); \
627 pos = nlmsg_next(pos, &(rem)))
629 /**************************************************************************
631 **************************************************************************/
634 * nla_attr_size - length of attribute not including padding
635 * @payload: length of payload
637 static inline int nla_attr_size(int payload)
639 return NLA_HDRLEN + payload;
643 * nla_total_size - total length of attribute including padding
644 * @payload: length of payload
646 static inline int nla_total_size(int payload)
648 return NLA_ALIGN(nla_attr_size(payload));
652 * nla_padlen - length of padding at the tail of attribute
653 * @payload: length of payload
655 static inline int nla_padlen(int payload)
657 return nla_total_size(payload) - nla_attr_size(payload);
661 * nla_data - head of payload
662 * @nla: netlink attribute
664 static inline void *nla_data(const struct nlattr *nla)
666 return (char *) nla + NLA_HDRLEN;
670 * nla_len - length of payload
671 * @nla: netlink attribute
673 static inline int nla_len(const struct nlattr *nla)
675 return nla->nla_len - NLA_HDRLEN;
679 * nla_ok - check if the netlink attribute fits into the remaining bytes
680 * @nla: netlink attribute
681 * @remaining: number of bytes remaining in attribute stream
683 static inline int nla_ok(const struct nlattr *nla, int remaining)
685 return remaining >= sizeof(*nla) &&
686 nla->nla_len >= sizeof(*nla) &&
687 nla->nla_len <= remaining;
691 * nla_next - next netlink attribte in attribute stream
692 * @nla: netlink attribute
693 * @remaining: number of bytes remaining in attribute stream
695 * Returns the next netlink attribute in the attribute stream and
696 * decrements remaining by the size of the current attribute.
698 static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
700 int totlen = NLA_ALIGN(nla->nla_len);
702 *remaining -= totlen;
703 return (struct nlattr *) ((char *) nla + totlen);
707 * nla_find_nested - find attribute in a set of nested attributes
708 * @nla: attribute containing the nested attributes
709 * @attrtype: type of attribute to look for
711 * Returns the first attribute which matches the specified type.
713 static inline struct nlattr *nla_find_nested(struct nlattr *nla, int attrtype)
715 return nla_find(nla_data(nla), nla_len(nla), attrtype);
719 * nla_parse_nested - parse nested attributes
720 * @tb: destination array with maxtype+1 elements
721 * @maxtype: maximum attribute type to be expected
722 * @nla: attribute containing the nested attributes
723 * @policy: validation policy
727 static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
729 struct nla_policy *policy)
731 return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
734 * nla_put_u8 - Add a u16 netlink attribute to a socket buffer
735 * @skb: socket buffer to add attribute to
736 * @attrtype: attribute type
737 * @value: numeric value
739 static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
741 return nla_put(skb, attrtype, sizeof(u8), &value);
745 * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
746 * @skb: socket buffer to add attribute to
747 * @attrtype: attribute type
748 * @value: numeric value
750 static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
752 return nla_put(skb, attrtype, sizeof(u16), &value);
756 * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
757 * @skb: socket buffer to add attribute to
758 * @attrtype: attribute type
759 * @value: numeric value
761 static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
763 return nla_put(skb, attrtype, sizeof(u32), &value);
767 * nla_put_64 - Add a u64 netlink attribute to a socket buffer
768 * @skb: socket buffer to add attribute to
769 * @attrtype: attribute type
770 * @value: numeric value
772 static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value)
774 return nla_put(skb, attrtype, sizeof(u64), &value);
778 * nla_put_string - Add a string netlink attribute to a socket buffer
779 * @skb: socket buffer to add attribute to
780 * @attrtype: attribute type
781 * @str: NUL terminated string
783 static inline int nla_put_string(struct sk_buff *skb, int attrtype,
786 return nla_put(skb, attrtype, strlen(str) + 1, str);
790 * nla_put_flag - Add a flag netlink attribute to a socket buffer
791 * @skb: socket buffer to add attribute to
792 * @attrtype: attribute type
794 static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
796 return nla_put(skb, attrtype, 0, NULL);
800 * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
801 * @skb: socket buffer to add attribute to
802 * @attrtype: attribute type
803 * @jiffies: number of msecs in jiffies
805 static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
806 unsigned long jiffies)
808 u64 tmp = jiffies_to_msecs(jiffies);
809 return nla_put(skb, attrtype, sizeof(u64), &tmp);
812 #define NLA_PUT(skb, attrtype, attrlen, data) \
814 if (nla_put(skb, attrtype, attrlen, data) < 0) \
815 goto nla_put_failure; \
818 #define NLA_PUT_TYPE(skb, type, attrtype, value) \
820 type __tmp = value; \
821 NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \
824 #define NLA_PUT_U8(skb, attrtype, value) \
825 NLA_PUT_TYPE(skb, u8, attrtype, value)
827 #define NLA_PUT_U16(skb, attrtype, value) \
828 NLA_PUT_TYPE(skb, u16, attrtype, value)
830 #define NLA_PUT_U32(skb, attrtype, value) \
831 NLA_PUT_TYPE(skb, u32, attrtype, value)
833 #define NLA_PUT_U64(skb, attrtype, value) \
834 NLA_PUT_TYPE(skb, u64, attrtype, value)
836 #define NLA_PUT_STRING(skb, attrtype, value) \
837 NLA_PUT(skb, attrtype, strlen(value) + 1, value)
839 #define NLA_PUT_FLAG(skb, attrtype) \
840 NLA_PUT(skb, attrtype, 0, NULL)
842 #define NLA_PUT_MSECS(skb, attrtype, jiffies) \
843 NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies))
846 * nla_get_u32 - return payload of u32 attribute
847 * @nla: u32 netlink attribute
849 static inline u32 nla_get_u32(struct nlattr *nla)
851 return *(u32 *) nla_data(nla);
855 * nla_get_u16 - return payload of u16 attribute
856 * @nla: u16 netlink attribute
858 static inline u16 nla_get_u16(struct nlattr *nla)
860 return *(u16 *) nla_data(nla);
864 * nla_get_u8 - return payload of u8 attribute
865 * @nla: u8 netlink attribute
867 static inline u8 nla_get_u8(struct nlattr *nla)
869 return *(u8 *) nla_data(nla);
873 * nla_get_u64 - return payload of u64 attribute
874 * @nla: u64 netlink attribute
876 static inline u64 nla_get_u64(struct nlattr *nla)
880 nla_memcpy(&tmp, nla, sizeof(tmp));
886 * nla_get_flag - return payload of flag attribute
887 * @nla: flag netlink attribute
889 static inline int nla_get_flag(struct nlattr *nla)
895 * nla_get_msecs - return payload of msecs attribute
896 * @nla: msecs netlink attribute
898 * Returns the number of milliseconds in jiffies.
900 static inline unsigned long nla_get_msecs(struct nlattr *nla)
902 u64 msecs = nla_get_u64(nla);
904 return msecs_to_jiffies((unsigned long) msecs);
908 * nla_nest_start - Start a new level of nested attributes
909 * @skb: socket buffer to add attributes to
910 * @attrtype: attribute type of container
912 * Returns the container attribute
914 static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
916 struct nlattr *start = (struct nlattr *) skb->tail;
918 if (nla_put(skb, attrtype, 0, NULL) < 0)
925 * nla_nest_end - Finalize nesting of attributes
926 * @skb: socket buffer the attribtues are stored in
927 * @start: container attribute
929 * Corrects the container attribute header to include the all
930 * appeneded attributes.
932 * Returns the total data length of the skb.
934 static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
936 start->nla_len = skb->tail - (unsigned char *) start;
941 * nla_nest_cancel - Cancel nesting of attributes
942 * @skb: socket buffer the message is stored in
943 * @start: container attribute
945 * Removes the container attribute and including all nested
946 * attributes. Returns -1.
948 static inline int nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
950 return nlmsg_trim(skb, start);
954 * nla_for_each_attr - iterate over a stream of attributes
955 * @pos: loop counter, set to current attribute
956 * @head: head of attribute stream
957 * @len: length of attribute stream
958 * @rem: initialized to len, holds bytes currently remaining in stream
960 #define nla_for_each_attr(pos, head, len, rem) \
961 for (pos = head, rem = len; \
963 pos = nla_next(pos, &(rem)))
966 * nla_for_each_nested - iterate over nested attributes
967 * @pos: loop counter, set to current attribute
968 * @nla: attribute containing the nested attributes
969 * @rem: initialized to len, holds bytes currently remaining in stream
971 #define nla_for_each_nested(pos, nla, rem) \
972 nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)