[NETLINK]: Remove error pointer from netlink message handler
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / include / net / netlink.h
blob1c11518fc8228fc6a30f24789eee8b80530e3d85
1 #ifndef __NET_NETLINK_H
2 #define __NET_NETLINK_H
4 #include <linux/types.h>
5 #include <linux/netlink.h>
6 #include <linux/jiffies.h>
8 /* ========================================================================
9 * Netlink Messages and Attributes Interface (As Seen On TV)
10 * ------------------------------------------------------------------------
11 * Messages Interface
12 * ------------------------------------------------------------------------
14 * Message Format:
15 * <--- nlmsg_total_size(payload) --->
16 * <-- nlmsg_msg_size(payload) ->
17 * +----------+- - -+-------------+- - -+-------- - -
18 * | nlmsghdr | Pad | Payload | Pad | nlmsghdr
19 * +----------+- - -+-------------+- - -+-------- - -
20 * nlmsg_data(nlh)---^ ^
21 * nlmsg_next(nlh)-----------------------+
23 * Payload Format:
24 * <---------------------- nlmsg_len(nlh) --------------------->
25 * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) ->
26 * +----------------------+- - -+--------------------------------+
27 * | Family Header | Pad | Attributes |
28 * +----------------------+- - -+--------------------------------+
29 * nlmsg_attrdata(nlh, hdrlen)---^
31 * Data Structures:
32 * struct nlmsghdr netlink message header
34 * Message Construction:
35 * nlmsg_new() create a new netlink message
36 * nlmsg_put() add a netlink message to an skb
37 * nlmsg_put_answer() callback based nlmsg_put()
38 * nlmsg_end() finanlize netlink message
39 * nlmsg_get_pos() return current position in message
40 * nlmsg_trim() trim part of message
41 * nlmsg_cancel() cancel message construction
42 * nlmsg_free() free a netlink message
44 * Message Sending:
45 * nlmsg_multicast() multicast message to several groups
46 * nlmsg_unicast() unicast a message to a single socket
47 * nlmsg_notify() send notification message
49 * Message Length Calculations:
50 * nlmsg_msg_size(payload) length of message w/o padding
51 * nlmsg_total_size(payload) length of message w/ padding
52 * nlmsg_padlen(payload) length of padding at tail
54 * Message Payload Access:
55 * nlmsg_data(nlh) head of message payload
56 * nlmsg_len(nlh) length of message payload
57 * nlmsg_attrdata(nlh, hdrlen) head of attributes data
58 * nlmsg_attrlen(nlh, hdrlen) length of attributes data
60 * Message Parsing:
61 * nlmsg_ok(nlh, remaining) does nlh fit into remaining bytes?
62 * nlmsg_next(nlh, remaining) get next netlink message
63 * nlmsg_parse() parse attributes of a message
64 * nlmsg_find_attr() find an attribute in a message
65 * nlmsg_for_each_msg() loop over all messages
66 * nlmsg_validate() validate netlink message incl. attrs
67 * nlmsg_for_each_attr() loop over all attributes
69 * Misc:
70 * nlmsg_report() report back to application?
72 * ------------------------------------------------------------------------
73 * Attributes Interface
74 * ------------------------------------------------------------------------
76 * Attribute Format:
77 * <------- nla_total_size(payload) ------->
78 * <---- nla_attr_size(payload) ----->
79 * +----------+- - -+- - - - - - - - - +- - -+-------- - -
80 * | Header | Pad | Payload | Pad | Header
81 * +----------+- - -+- - - - - - - - - +- - -+-------- - -
82 * <- nla_len(nla) -> ^
83 * nla_data(nla)----^ |
84 * nla_next(nla)-----------------------------'
86 * Data Structures:
87 * struct nlattr netlink attribtue header
89 * Attribute Construction:
90 * nla_reserve(skb, type, len) reserve room for an attribute
91 * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr
92 * nla_put(skb, type, len, data) add attribute to skb
93 * nla_put_nohdr(skb, len, data) add attribute w/o hdr
95 * Attribute Construction for Basic Types:
96 * nla_put_u8(skb, type, value) add u8 attribute to skb
97 * nla_put_u16(skb, type, value) add u16 attribute to skb
98 * nla_put_u32(skb, type, value) add u32 attribute to skb
99 * nla_put_u64(skb, type, value) add u64 attribute to skb
100 * nla_put_string(skb, type, str) add string attribute to skb
101 * nla_put_flag(skb, type) add flag attribute to skb
102 * nla_put_msecs(skb, type, jiffies) add msecs attribute to skb
104 * Exceptions Based Attribute Construction:
105 * NLA_PUT(skb, type, len, data) add attribute to skb
106 * NLA_PUT_U8(skb, type, value) add u8 attribute to skb
107 * NLA_PUT_U16(skb, type, value) add u16 attribute to skb
108 * NLA_PUT_U32(skb, type, value) add u32 attribute to skb
109 * NLA_PUT_U64(skb, type, value) add u64 attribute to skb
110 * NLA_PUT_STRING(skb, type, str) add string attribute to skb
111 * NLA_PUT_FLAG(skb, type) add flag attribute to skb
112 * NLA_PUT_MSECS(skb, type, jiffies) add msecs attribute to skb
114 * The meaning of these functions is equal to their lower case
115 * variants but they jump to the label nla_put_failure in case
116 * of a failure.
118 * Nested Attributes Construction:
119 * nla_nest_start(skb, type) start a nested attribute
120 * nla_nest_end(skb, nla) finalize a nested attribute
121 * nla_nest_cancel(skb, nla) cancel nested attribute construction
123 * Attribute Length Calculations:
124 * nla_attr_size(payload) length of attribute w/o padding
125 * nla_total_size(payload) length of attribute w/ padding
126 * nla_padlen(payload) length of padding
128 * Attribute Payload Access:
129 * nla_data(nla) head of attribute payload
130 * nla_len(nla) length of attribute payload
132 * Attribute Payload Access for Basic Types:
133 * nla_get_u8(nla) get payload for a u8 attribute
134 * nla_get_u16(nla) get payload for a u16 attribute
135 * nla_get_u32(nla) get payload for a u32 attribute
136 * nla_get_u64(nla) get payload for a u64 attribute
137 * nla_get_flag(nla) return 1 if flag is true
138 * nla_get_msecs(nla) get payload for a msecs attribute
140 * Attribute Misc:
141 * nla_memcpy(dest, nla, count) copy attribute into memory
142 * nla_memcmp(nla, data, size) compare attribute with memory area
143 * nla_strlcpy(dst, nla, size) copy attribute to a sized string
144 * nla_strcmp(nla, str) compare attribute with string
146 * Attribute Parsing:
147 * nla_ok(nla, remaining) does nla fit into remaining bytes?
148 * nla_next(nla, remaining) get next netlink attribute
149 * nla_validate() validate a stream of attributes
150 * nla_validate_nested() validate a stream of nested attributes
151 * nla_find() find attribute in stream of attributes
152 * nla_find_nested() find attribute in nested attributes
153 * nla_parse() parse and validate stream of attrs
154 * nla_parse_nested() parse nested attribuets
155 * nla_for_each_attr() loop over all attributes
156 * nla_for_each_nested() loop over the nested attributes
157 *=========================================================================
161 * Standard attribute types to specify validation policy
163 enum {
164 NLA_UNSPEC,
165 NLA_U8,
166 NLA_U16,
167 NLA_U32,
168 NLA_U64,
169 NLA_STRING,
170 NLA_FLAG,
171 NLA_MSECS,
172 NLA_NESTED,
173 NLA_NUL_STRING,
174 __NLA_TYPE_MAX,
177 #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
180 * struct nla_policy - attribute validation policy
181 * @type: Type of attribute or NLA_UNSPEC
182 * @len: Type specific length of payload
184 * Policies are defined as arrays of this struct, the array must be
185 * accessible by attribute type up to the highest identifier to be expected.
187 * Meaning of `len' field:
188 * NLA_STRING Maximum length of string
189 * NLA_NUL_STRING Maximum length of string (excluding NUL)
190 * NLA_FLAG Unused
191 * All other Exact length of attribute payload
193 * Example:
194 * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = {
195 * [ATTR_FOO] = { .type = NLA_U16 },
196 * [ATTR_BAR] = { .type = NLA_STRING, len = BARSIZ },
197 * [ATTR_BAZ] = { .len = sizeof(struct mystruct) },
198 * };
200 struct nla_policy {
201 u16 type;
202 u16 len;
206 * struct nl_info - netlink source information
207 * @nlh: Netlink message header of original request
208 * @pid: Netlink PID of requesting application
210 struct nl_info {
211 struct nlmsghdr *nlh;
212 u32 pid;
215 extern void netlink_run_queue(struct sock *sk, unsigned int *qlen,
216 int (*cb)(struct sk_buff *,
217 struct nlmsghdr *));
218 extern void netlink_queue_skip(struct nlmsghdr *nlh,
219 struct sk_buff *skb);
220 extern int nlmsg_notify(struct sock *sk, struct sk_buff *skb,
221 u32 pid, unsigned int group, int report,
222 gfp_t flags);
224 extern int nla_validate(struct nlattr *head, int len, int maxtype,
225 struct nla_policy *policy);
226 extern int nla_parse(struct nlattr *tb[], int maxtype,
227 struct nlattr *head, int len,
228 struct nla_policy *policy);
229 extern struct nlattr * nla_find(struct nlattr *head, int len, int attrtype);
230 extern size_t nla_strlcpy(char *dst, const struct nlattr *nla,
231 size_t dstsize);
232 extern int nla_memcpy(void *dest, struct nlattr *src, int count);
233 extern int nla_memcmp(const struct nlattr *nla, const void *data,
234 size_t size);
235 extern int nla_strcmp(const struct nlattr *nla, const char *str);
236 extern struct nlattr * __nla_reserve(struct sk_buff *skb, int attrtype,
237 int attrlen);
238 extern void * __nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
239 extern struct nlattr * nla_reserve(struct sk_buff *skb, int attrtype,
240 int attrlen);
241 extern void * nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
242 extern void __nla_put(struct sk_buff *skb, int attrtype,
243 int attrlen, const void *data);
244 extern void __nla_put_nohdr(struct sk_buff *skb, int attrlen,
245 const void *data);
246 extern int nla_put(struct sk_buff *skb, int attrtype,
247 int attrlen, const void *data);
248 extern int nla_put_nohdr(struct sk_buff *skb, int attrlen,
249 const void *data);
251 /**************************************************************************
252 * Netlink Messages
253 **************************************************************************/
256 * nlmsg_msg_size - length of netlink message not including padding
257 * @payload: length of message payload
259 static inline int nlmsg_msg_size(int payload)
261 return NLMSG_HDRLEN + payload;
265 * nlmsg_total_size - length of netlink message including padding
266 * @payload: length of message payload
268 static inline int nlmsg_total_size(int payload)
270 return NLMSG_ALIGN(nlmsg_msg_size(payload));
274 * nlmsg_padlen - length of padding at the message's tail
275 * @payload: length of message payload
277 static inline int nlmsg_padlen(int payload)
279 return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
283 * nlmsg_data - head of message payload
284 * @nlh: netlink messsage header
286 static inline void *nlmsg_data(const struct nlmsghdr *nlh)
288 return (unsigned char *) nlh + NLMSG_HDRLEN;
292 * nlmsg_len - length of message payload
293 * @nlh: netlink message header
295 static inline int nlmsg_len(const struct nlmsghdr *nlh)
297 return nlh->nlmsg_len - NLMSG_HDRLEN;
301 * nlmsg_attrdata - head of attributes data
302 * @nlh: netlink message header
303 * @hdrlen: length of family specific header
305 static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
306 int hdrlen)
308 unsigned char *data = nlmsg_data(nlh);
309 return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
313 * nlmsg_attrlen - length of attributes data
314 * @nlh: netlink message header
315 * @hdrlen: length of family specific header
317 static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
319 return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
323 * nlmsg_ok - check if the netlink message fits into the remaining bytes
324 * @nlh: netlink message header
325 * @remaining: number of bytes remaining in message stream
327 static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
329 return (remaining >= sizeof(struct nlmsghdr) &&
330 nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
331 nlh->nlmsg_len <= remaining);
335 * nlmsg_next - next netlink message in message stream
336 * @nlh: netlink message header
337 * @remaining: number of bytes remaining in message stream
339 * Returns the next netlink message in the message stream and
340 * decrements remaining by the size of the current message.
342 static inline struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
344 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
346 *remaining -= totlen;
348 return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
352 * nlmsg_parse - parse attributes of a netlink message
353 * @nlh: netlink message header
354 * @hdrlen: length of family specific header
355 * @tb: destination array with maxtype+1 elements
356 * @maxtype: maximum attribute type to be expected
357 * @policy: validation policy
359 * See nla_parse()
361 static inline int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen,
362 struct nlattr *tb[], int maxtype,
363 struct nla_policy *policy)
365 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
366 return -EINVAL;
368 return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
369 nlmsg_attrlen(nlh, hdrlen), policy);
373 * nlmsg_find_attr - find a specific attribute in a netlink message
374 * @nlh: netlink message header
375 * @hdrlen: length of familiy specific header
376 * @attrtype: type of attribute to look for
378 * Returns the first attribute which matches the specified type.
380 static inline struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh,
381 int hdrlen, int attrtype)
383 return nla_find(nlmsg_attrdata(nlh, hdrlen),
384 nlmsg_attrlen(nlh, hdrlen), attrtype);
388 * nlmsg_validate - validate a netlink message including attributes
389 * @nlh: netlinket message header
390 * @hdrlen: length of familiy specific header
391 * @maxtype: maximum attribute type to be expected
392 * @policy: validation policy
394 static inline int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
395 struct nla_policy *policy)
397 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
398 return -EINVAL;
400 return nla_validate(nlmsg_attrdata(nlh, hdrlen),
401 nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
405 * nlmsg_report - need to report back to application?
406 * @nlh: netlink message header
408 * Returns 1 if a report back to the application is requested.
410 static inline int nlmsg_report(struct nlmsghdr *nlh)
412 return !!(nlh->nlmsg_flags & NLM_F_ECHO);
416 * nlmsg_for_each_attr - iterate over a stream of attributes
417 * @pos: loop counter, set to current attribute
418 * @nlh: netlink message header
419 * @hdrlen: length of familiy specific header
420 * @rem: initialized to len, holds bytes currently remaining in stream
422 #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
423 nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
424 nlmsg_attrlen(nlh, hdrlen), rem)
426 #if 0
427 /* FIXME: Enable once all users have been converted */
430 * __nlmsg_put - Add a new netlink message to an skb
431 * @skb: socket buffer to store message in
432 * @pid: netlink process id
433 * @seq: sequence number of message
434 * @type: message type
435 * @payload: length of message payload
436 * @flags: message flags
438 * The caller is responsible to ensure that the skb provides enough
439 * tailroom for both the netlink header and payload.
441 static inline struct nlmsghdr *__nlmsg_put(struct sk_buff *skb, u32 pid,
442 u32 seq, int type, int payload,
443 int flags)
445 struct nlmsghdr *nlh;
447 nlh = (struct nlmsghdr *) skb_put(skb, nlmsg_total_size(payload));
448 nlh->nlmsg_type = type;
449 nlh->nlmsg_len = nlmsg_msg_size(payload);
450 nlh->nlmsg_flags = flags;
451 nlh->nlmsg_pid = pid;
452 nlh->nlmsg_seq = seq;
454 memset((unsigned char *) nlmsg_data(nlh) + payload, 0,
455 nlmsg_padlen(payload));
457 return nlh;
459 #endif
462 * nlmsg_put - Add a new netlink message to an skb
463 * @skb: socket buffer to store message in
464 * @pid: netlink process id
465 * @seq: sequence number of message
466 * @type: message type
467 * @payload: length of message payload
468 * @flags: message flags
470 * Returns NULL if the tailroom of the skb is insufficient to store
471 * the message header and payload.
473 static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
474 int type, int payload, int flags)
476 if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
477 return NULL;
479 return __nlmsg_put(skb, pid, seq, type, payload, flags);
483 * nlmsg_put_answer - Add a new callback based netlink message to an skb
484 * @skb: socket buffer to store message in
485 * @cb: netlink callback
486 * @type: message type
487 * @payload: length of message payload
488 * @flags: message flags
490 * Returns NULL if the tailroom of the skb is insufficient to store
491 * the message header and payload.
493 static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
494 struct netlink_callback *cb,
495 int type, int payload,
496 int flags)
498 return nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
499 type, payload, flags);
503 * nlmsg_new - Allocate a new netlink message
504 * @payload: size of the message payload
505 * @flags: the type of memory to allocate.
507 * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
508 * and a good default is needed.
510 static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
512 return alloc_skb(nlmsg_total_size(payload), flags);
516 * nlmsg_end - Finalize a netlink message
517 * @skb: socket buffer the message is stored in
518 * @nlh: netlink message header
520 * Corrects the netlink message header to include the appeneded
521 * attributes. Only necessary if attributes have been added to
522 * the message.
524 * Returns the total data length of the skb.
526 static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
528 nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
530 return skb->len;
534 * nlmsg_get_pos - return current position in netlink message
535 * @skb: socket buffer the message is stored in
537 * Returns a pointer to the current tail of the message.
539 static inline void *nlmsg_get_pos(struct sk_buff *skb)
541 return skb_tail_pointer(skb);
545 * nlmsg_trim - Trim message to a mark
546 * @skb: socket buffer the message is stored in
547 * @mark: mark to trim to
549 * Trims the message to the provided mark. Returns -1.
551 static inline int nlmsg_trim(struct sk_buff *skb, const void *mark)
553 if (mark)
554 skb_trim(skb, (unsigned char *) mark - skb->data);
556 return -1;
560 * nlmsg_cancel - Cancel construction of a netlink message
561 * @skb: socket buffer the message is stored in
562 * @nlh: netlink message header
564 * Removes the complete netlink message including all
565 * attributes from the socket buffer again. Returns -1.
567 static inline int nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
569 return nlmsg_trim(skb, nlh);
573 * nlmsg_free - free a netlink message
574 * @skb: socket buffer of netlink message
576 static inline void nlmsg_free(struct sk_buff *skb)
578 kfree_skb(skb);
582 * nlmsg_multicast - multicast a netlink message
583 * @sk: netlink socket to spread messages to
584 * @skb: netlink message as socket buffer
585 * @pid: own netlink pid to avoid sending to yourself
586 * @group: multicast group id
587 * @flags: allocation flags
589 static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
590 u32 pid, unsigned int group, gfp_t flags)
592 int err;
594 NETLINK_CB(skb).dst_group = group;
596 err = netlink_broadcast(sk, skb, pid, group, flags);
597 if (err > 0)
598 err = 0;
600 return err;
604 * nlmsg_unicast - unicast a netlink message
605 * @sk: netlink socket to spread message to
606 * @skb: netlink message as socket buffer
607 * @pid: netlink pid of the destination socket
609 static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 pid)
611 int err;
613 err = netlink_unicast(sk, skb, pid, MSG_DONTWAIT);
614 if (err > 0)
615 err = 0;
617 return err;
621 * nlmsg_for_each_msg - iterate over a stream of messages
622 * @pos: loop counter, set to current message
623 * @head: head of message stream
624 * @len: length of message stream
625 * @rem: initialized to len, holds bytes currently remaining in stream
627 #define nlmsg_for_each_msg(pos, head, len, rem) \
628 for (pos = head, rem = len; \
629 nlmsg_ok(pos, rem); \
630 pos = nlmsg_next(pos, &(rem)))
632 /**************************************************************************
633 * Netlink Attributes
634 **************************************************************************/
637 * nla_attr_size - length of attribute not including padding
638 * @payload: length of payload
640 static inline int nla_attr_size(int payload)
642 return NLA_HDRLEN + payload;
646 * nla_total_size - total length of attribute including padding
647 * @payload: length of payload
649 static inline int nla_total_size(int payload)
651 return NLA_ALIGN(nla_attr_size(payload));
655 * nla_padlen - length of padding at the tail of attribute
656 * @payload: length of payload
658 static inline int nla_padlen(int payload)
660 return nla_total_size(payload) - nla_attr_size(payload);
664 * nla_data - head of payload
665 * @nla: netlink attribute
667 static inline void *nla_data(const struct nlattr *nla)
669 return (char *) nla + NLA_HDRLEN;
673 * nla_len - length of payload
674 * @nla: netlink attribute
676 static inline int nla_len(const struct nlattr *nla)
678 return nla->nla_len - NLA_HDRLEN;
682 * nla_ok - check if the netlink attribute fits into the remaining bytes
683 * @nla: netlink attribute
684 * @remaining: number of bytes remaining in attribute stream
686 static inline int nla_ok(const struct nlattr *nla, int remaining)
688 return remaining >= sizeof(*nla) &&
689 nla->nla_len >= sizeof(*nla) &&
690 nla->nla_len <= remaining;
694 * nla_next - next netlink attribte in attribute stream
695 * @nla: netlink attribute
696 * @remaining: number of bytes remaining in attribute stream
698 * Returns the next netlink attribute in the attribute stream and
699 * decrements remaining by the size of the current attribute.
701 static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
703 int totlen = NLA_ALIGN(nla->nla_len);
705 *remaining -= totlen;
706 return (struct nlattr *) ((char *) nla + totlen);
710 * nla_find_nested - find attribute in a set of nested attributes
711 * @nla: attribute containing the nested attributes
712 * @attrtype: type of attribute to look for
714 * Returns the first attribute which matches the specified type.
716 static inline struct nlattr *nla_find_nested(struct nlattr *nla, int attrtype)
718 return nla_find(nla_data(nla), nla_len(nla), attrtype);
722 * nla_parse_nested - parse nested attributes
723 * @tb: destination array with maxtype+1 elements
724 * @maxtype: maximum attribute type to be expected
725 * @nla: attribute containing the nested attributes
726 * @policy: validation policy
728 * See nla_parse()
730 static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
731 struct nlattr *nla,
732 struct nla_policy *policy)
734 return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
737 * nla_put_u8 - Add a u16 netlink attribute to a socket buffer
738 * @skb: socket buffer to add attribute to
739 * @attrtype: attribute type
740 * @value: numeric value
742 static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
744 return nla_put(skb, attrtype, sizeof(u8), &value);
748 * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
749 * @skb: socket buffer to add attribute to
750 * @attrtype: attribute type
751 * @value: numeric value
753 static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
755 return nla_put(skb, attrtype, sizeof(u16), &value);
759 * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
760 * @skb: socket buffer to add attribute to
761 * @attrtype: attribute type
762 * @value: numeric value
764 static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
766 return nla_put(skb, attrtype, sizeof(u32), &value);
770 * nla_put_64 - Add a u64 netlink attribute to a socket buffer
771 * @skb: socket buffer to add attribute to
772 * @attrtype: attribute type
773 * @value: numeric value
775 static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value)
777 return nla_put(skb, attrtype, sizeof(u64), &value);
781 * nla_put_string - Add a string netlink attribute to a socket buffer
782 * @skb: socket buffer to add attribute to
783 * @attrtype: attribute type
784 * @str: NUL terminated string
786 static inline int nla_put_string(struct sk_buff *skb, int attrtype,
787 const char *str)
789 return nla_put(skb, attrtype, strlen(str) + 1, str);
793 * nla_put_flag - Add a flag netlink attribute to a socket buffer
794 * @skb: socket buffer to add attribute to
795 * @attrtype: attribute type
797 static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
799 return nla_put(skb, attrtype, 0, NULL);
803 * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
804 * @skb: socket buffer to add attribute to
805 * @attrtype: attribute type
806 * @jiffies: number of msecs in jiffies
808 static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
809 unsigned long jiffies)
811 u64 tmp = jiffies_to_msecs(jiffies);
812 return nla_put(skb, attrtype, sizeof(u64), &tmp);
815 #define NLA_PUT(skb, attrtype, attrlen, data) \
816 do { \
817 if (nla_put(skb, attrtype, attrlen, data) < 0) \
818 goto nla_put_failure; \
819 } while(0)
821 #define NLA_PUT_TYPE(skb, type, attrtype, value) \
822 do { \
823 type __tmp = value; \
824 NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \
825 } while(0)
827 #define NLA_PUT_U8(skb, attrtype, value) \
828 NLA_PUT_TYPE(skb, u8, attrtype, value)
830 #define NLA_PUT_U16(skb, attrtype, value) \
831 NLA_PUT_TYPE(skb, u16, attrtype, value)
833 #define NLA_PUT_LE16(skb, attrtype, value) \
834 NLA_PUT_TYPE(skb, __le16, attrtype, value)
836 #define NLA_PUT_U32(skb, attrtype, value) \
837 NLA_PUT_TYPE(skb, u32, attrtype, value)
839 #define NLA_PUT_BE32(skb, attrtype, value) \
840 NLA_PUT_TYPE(skb, __be32, attrtype, value)
842 #define NLA_PUT_U64(skb, attrtype, value) \
843 NLA_PUT_TYPE(skb, u64, attrtype, value)
845 #define NLA_PUT_STRING(skb, attrtype, value) \
846 NLA_PUT(skb, attrtype, strlen(value) + 1, value)
848 #define NLA_PUT_FLAG(skb, attrtype) \
849 NLA_PUT(skb, attrtype, 0, NULL)
851 #define NLA_PUT_MSECS(skb, attrtype, jiffies) \
852 NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies))
855 * nla_get_u32 - return payload of u32 attribute
856 * @nla: u32 netlink attribute
858 static inline u32 nla_get_u32(struct nlattr *nla)
860 return *(u32 *) nla_data(nla);
864 * nla_get_be32 - return payload of __be32 attribute
865 * @nla: __be32 netlink attribute
867 static inline __be32 nla_get_be32(struct nlattr *nla)
869 return *(__be32 *) nla_data(nla);
873 * nla_get_u16 - return payload of u16 attribute
874 * @nla: u16 netlink attribute
876 static inline u16 nla_get_u16(struct nlattr *nla)
878 return *(u16 *) nla_data(nla);
882 * nla_get_le16 - return payload of __le16 attribute
883 * @nla: __le16 netlink attribute
885 static inline __le16 nla_get_le16(struct nlattr *nla)
887 return *(__le16 *) nla_data(nla);
891 * nla_get_u8 - return payload of u8 attribute
892 * @nla: u8 netlink attribute
894 static inline u8 nla_get_u8(struct nlattr *nla)
896 return *(u8 *) nla_data(nla);
900 * nla_get_u64 - return payload of u64 attribute
901 * @nla: u64 netlink attribute
903 static inline u64 nla_get_u64(struct nlattr *nla)
905 u64 tmp;
907 nla_memcpy(&tmp, nla, sizeof(tmp));
909 return tmp;
913 * nla_get_flag - return payload of flag attribute
914 * @nla: flag netlink attribute
916 static inline int nla_get_flag(struct nlattr *nla)
918 return !!nla;
922 * nla_get_msecs - return payload of msecs attribute
923 * @nla: msecs netlink attribute
925 * Returns the number of milliseconds in jiffies.
927 static inline unsigned long nla_get_msecs(struct nlattr *nla)
929 u64 msecs = nla_get_u64(nla);
931 return msecs_to_jiffies((unsigned long) msecs);
935 * nla_nest_start - Start a new level of nested attributes
936 * @skb: socket buffer to add attributes to
937 * @attrtype: attribute type of container
939 * Returns the container attribute
941 static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
943 struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
945 if (nla_put(skb, attrtype, 0, NULL) < 0)
946 return NULL;
948 return start;
952 * nla_nest_end - Finalize nesting of attributes
953 * @skb: socket buffer the attribtues are stored in
954 * @start: container attribute
956 * Corrects the container attribute header to include the all
957 * appeneded attributes.
959 * Returns the total data length of the skb.
961 static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
963 start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
964 return skb->len;
968 * nla_nest_cancel - Cancel nesting of attributes
969 * @skb: socket buffer the message is stored in
970 * @start: container attribute
972 * Removes the container attribute and including all nested
973 * attributes. Returns -1.
975 static inline int nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
977 return nlmsg_trim(skb, start);
981 * nla_validate_nested - Validate a stream of nested attributes
982 * @start: container attribute
983 * @maxtype: maximum attribute type to be expected
984 * @policy: validation policy
986 * Validates all attributes in the nested attribute stream against the
987 * specified policy. Attributes with a type exceeding maxtype will be
988 * ignored. See documenation of struct nla_policy for more details.
990 * Returns 0 on success or a negative error code.
992 static inline int nla_validate_nested(struct nlattr *start, int maxtype,
993 struct nla_policy *policy)
995 return nla_validate(nla_data(start), nla_len(start), maxtype, policy);
999 * nla_for_each_attr - iterate over a stream of attributes
1000 * @pos: loop counter, set to current attribute
1001 * @head: head of attribute stream
1002 * @len: length of attribute stream
1003 * @rem: initialized to len, holds bytes currently remaining in stream
1005 #define nla_for_each_attr(pos, head, len, rem) \
1006 for (pos = head, rem = len; \
1007 nla_ok(pos, rem); \
1008 pos = nla_next(pos, &(rem)))
1011 * nla_for_each_nested - iterate over nested attributes
1012 * @pos: loop counter, set to current attribute
1013 * @nla: attribute containing the nested attributes
1014 * @rem: initialized to len, holds bytes currently remaining in stream
1016 #define nla_for_each_nested(pos, nla, rem) \
1017 nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
1019 #endif