blob: bcaf67b7a19d131022d0f41f61d5c2e838a8a56b [file] [log] [blame]
Thomas Grafbfa83a92005-11-10 02:25:51 +01001#ifndef __NET_NETLINK_H
2#define __NET_NETLINK_H
3
4#include <linux/types.h>
5#include <linux/netlink.h>
Al Virod7fe0f22006-12-03 23:15:30 -05006#include <linux/jiffies.h>
Thomas Grafbfa83a92005-11-10 02:25:51 +01007
8/* ========================================================================
9 * Netlink Messages and Attributes Interface (As Seen On TV)
10 * ------------------------------------------------------------------------
11 * Messages Interface
12 * ------------------------------------------------------------------------
13 *
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)-----------------------+
22 *
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)---^
30 *
31 * Data Structures:
32 * struct nlmsghdr netlink message header
33 *
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
Thomas Graffe4944e2006-08-04 23:03:05 -070039 * nlmsg_get_pos() return current position in message
40 * nlmsg_trim() trim part of message
Thomas Grafbfa83a92005-11-10 02:25:51 +010041 * nlmsg_cancel() cancel message construction
42 * nlmsg_free() free a netlink message
43 *
44 * Message Sending:
45 * nlmsg_multicast() multicast message to several groups
46 * nlmsg_unicast() unicast a message to a single socket
Thomas Grafd387f6a2006-08-15 00:31:06 -070047 * nlmsg_notify() send notification message
Thomas Grafbfa83a92005-11-10 02:25:51 +010048 *
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
53 *
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
59 *
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
68 *
Thomas Graf97676b62006-08-15 00:31:41 -070069 * Misc:
70 * nlmsg_report() report back to application?
71 *
Thomas Grafbfa83a92005-11-10 02:25:51 +010072 * ------------------------------------------------------------------------
73 * Attributes Interface
74 * ------------------------------------------------------------------------
75 *
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)-----------------------------'
85 *
86 * Data Structures:
87 * struct nlattr netlink attribtue header
88 *
89 * Attribute Construction:
Thomas Graffe4944e2006-08-04 23:03:05 -070090 * nla_reserve(skb, type, len) reserve room for an attribute
91 * nla_reserve_nohdr(skb, len) reserve room for an attribute w/o hdr
Thomas Grafbfa83a92005-11-10 02:25:51 +010092 * nla_put(skb, type, len, data) add attribute to skb
Thomas Graffe4944e2006-08-04 23:03:05 -070093 * nla_put_nohdr(skb, len, data) add attribute w/o hdr
Thomas Grafbfa83a92005-11-10 02:25:51 +010094 *
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
103 *
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
113 *
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.
117 *
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
122 *
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
127 *
128 * Attribute Payload Access:
129 * nla_data(nla) head of attribute payload
130 * nla_len(nla) length of attribute payload
131 *
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
139 *
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
145 *
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
Paul Moore4fe5d5c2006-09-25 15:54:03 -0700150 * nla_validate_nested() validate a stream of nested attributes
Thomas Grafbfa83a92005-11-10 02:25:51 +0100151 * nla_find() find attribute in stream of attributes
Thomas Graffe4944e2006-08-04 23:03:05 -0700152 * nla_find_nested() find attribute in nested attributes
Thomas Grafbfa83a92005-11-10 02:25:51 +0100153 * nla_parse() parse and validate stream of attrs
154 * nla_parse_nested() parse nested attribuets
155 * nla_for_each_attr() loop over all attributes
Paul Moore22acb192006-09-25 15:53:37 -0700156 * nla_for_each_nested() loop over the nested attributes
Thomas Grafbfa83a92005-11-10 02:25:51 +0100157 *=========================================================================
158 */
159
160 /**
161 * Standard attribute types to specify validation policy
162 */
163enum {
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,
Thomas Grafa5531a52006-08-26 20:11:47 -0700173 NLA_NUL_STRING,
Thomas Grafbfa83a92005-11-10 02:25:51 +0100174 __NLA_TYPE_MAX,
175};
176
177#define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
178
179/**
180 * struct nla_policy - attribute validation policy
181 * @type: Type of attribute or NLA_UNSPEC
Thomas Grafa5531a52006-08-26 20:11:47 -0700182 * @len: Type specific length of payload
Thomas Grafbfa83a92005-11-10 02:25:51 +0100183 *
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.
186 *
Thomas Grafa5531a52006-08-26 20:11:47 -0700187 * 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
192 *
Thomas Grafbfa83a92005-11-10 02:25:51 +0100193 * Example:
194 * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = {
195 * [ATTR_FOO] = { .type = NLA_U16 },
Thomas Grafa5531a52006-08-26 20:11:47 -0700196 * [ATTR_BAR] = { .type = NLA_STRING, len = BARSIZ },
197 * [ATTR_BAZ] = { .len = sizeof(struct mystruct) },
Thomas Grafbfa83a92005-11-10 02:25:51 +0100198 * };
199 */
200struct nla_policy {
201 u16 type;
Thomas Grafa5531a52006-08-26 20:11:47 -0700202 u16 len;
Thomas Grafbfa83a92005-11-10 02:25:51 +0100203};
204
Thomas Graf4e902c52006-08-17 18:14:52 -0700205/**
206 * struct nl_info - netlink source information
207 * @nlh: Netlink message header of original request
208 * @pid: Netlink PID of requesting application
209 */
210struct nl_info {
211 struct nlmsghdr *nlh;
212 u32 pid;
213};
214
Thomas Graf82ace472005-11-10 02:25:53 +0100215extern void netlink_run_queue(struct sock *sk, unsigned int *qlen,
216 int (*cb)(struct sk_buff *,
217 struct nlmsghdr *, int *));
218extern void netlink_queue_skip(struct nlmsghdr *nlh,
219 struct sk_buff *skb);
Thomas Graf97676b62006-08-15 00:31:41 -0700220extern int nlmsg_notify(struct sock *sk, struct sk_buff *skb,
221 u32 pid, unsigned int group, int report,
222 gfp_t flags);
Thomas Graf82ace472005-11-10 02:25:53 +0100223
Thomas Grafbfa83a92005-11-10 02:25:51 +0100224extern int nla_validate(struct nlattr *head, int len, int maxtype,
225 struct nla_policy *policy);
226extern int nla_parse(struct nlattr *tb[], int maxtype,
227 struct nlattr *head, int len,
228 struct nla_policy *policy);
229extern struct nlattr * nla_find(struct nlattr *head, int len, int attrtype);
230extern size_t nla_strlcpy(char *dst, const struct nlattr *nla,
231 size_t dstsize);
232extern int nla_memcpy(void *dest, struct nlattr *src, int count);
233extern int nla_memcmp(const struct nlattr *nla, const void *data,
234 size_t size);
235extern int nla_strcmp(const struct nlattr *nla, const char *str);
236extern struct nlattr * __nla_reserve(struct sk_buff *skb, int attrtype,
237 int attrlen);
Thomas Graffe4944e2006-08-04 23:03:05 -0700238extern void * __nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100239extern struct nlattr * nla_reserve(struct sk_buff *skb, int attrtype,
240 int attrlen);
Thomas Graffe4944e2006-08-04 23:03:05 -0700241extern void * nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100242extern void __nla_put(struct sk_buff *skb, int attrtype,
243 int attrlen, const void *data);
Thomas Graffe4944e2006-08-04 23:03:05 -0700244extern void __nla_put_nohdr(struct sk_buff *skb, int attrlen,
245 const void *data);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100246extern int nla_put(struct sk_buff *skb, int attrtype,
247 int attrlen, const void *data);
Thomas Graffe4944e2006-08-04 23:03:05 -0700248extern int nla_put_nohdr(struct sk_buff *skb, int attrlen,
249 const void *data);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100250
251/**************************************************************************
252 * Netlink Messages
253 **************************************************************************/
254
255/**
256 * nlmsg_msg_size - length of netlink message not including padding
257 * @payload: length of message payload
258 */
259static inline int nlmsg_msg_size(int payload)
260{
261 return NLMSG_HDRLEN + payload;
262}
263
264/**
265 * nlmsg_total_size - length of netlink message including padding
266 * @payload: length of message payload
267 */
268static inline int nlmsg_total_size(int payload)
269{
270 return NLMSG_ALIGN(nlmsg_msg_size(payload));
271}
272
273/**
274 * nlmsg_padlen - length of padding at the message's tail
275 * @payload: length of message payload
276 */
277static inline int nlmsg_padlen(int payload)
278{
279 return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
280}
281
282/**
283 * nlmsg_data - head of message payload
284 * @nlh: netlink messsage header
285 */
286static inline void *nlmsg_data(const struct nlmsghdr *nlh)
287{
288 return (unsigned char *) nlh + NLMSG_HDRLEN;
289}
290
291/**
292 * nlmsg_len - length of message payload
293 * @nlh: netlink message header
294 */
295static inline int nlmsg_len(const struct nlmsghdr *nlh)
296{
297 return nlh->nlmsg_len - NLMSG_HDRLEN;
298}
299
300/**
301 * nlmsg_attrdata - head of attributes data
302 * @nlh: netlink message header
303 * @hdrlen: length of family specific header
304 */
305static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
306 int hdrlen)
307{
308 unsigned char *data = nlmsg_data(nlh);
309 return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
310}
311
312/**
313 * nlmsg_attrlen - length of attributes data
314 * @nlh: netlink message header
315 * @hdrlen: length of family specific header
316 */
317static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
318{
319 return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
320}
321
322/**
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
326 */
327static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
328{
329 return (remaining >= sizeof(struct nlmsghdr) &&
330 nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
331 nlh->nlmsg_len <= remaining);
332}
333
334/**
335 * nlmsg_next - next netlink message in message stream
336 * @nlh: netlink message header
337 * @remaining: number of bytes remaining in message stream
338 *
339 * Returns the next netlink message in the message stream and
340 * decrements remaining by the size of the current message.
341 */
342static inline struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
343{
344 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
345
346 *remaining -= totlen;
347
348 return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
349}
350
351/**
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
358 *
359 * See nla_parse()
360 */
361static inline int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen,
362 struct nlattr *tb[], int maxtype,
363 struct nla_policy *policy)
364{
365 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
366 return -EINVAL;
367
368 return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
369 nlmsg_attrlen(nlh, hdrlen), policy);
370}
371
372/**
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
377 *
378 * Returns the first attribute which matches the specified type.
379 */
380static inline struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh,
381 int hdrlen, int attrtype)
382{
383 return nla_find(nlmsg_attrdata(nlh, hdrlen),
384 nlmsg_attrlen(nlh, hdrlen), attrtype);
385}
386
387/**
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
393 */
394static inline int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
395 struct nla_policy *policy)
396{
397 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
398 return -EINVAL;
399
400 return nla_validate(nlmsg_attrdata(nlh, hdrlen),
401 nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
402}
403
404/**
Thomas Graf97676b62006-08-15 00:31:41 -0700405 * nlmsg_report - need to report back to application?
406 * @nlh: netlink message header
407 *
408 * Returns 1 if a report back to the application is requested.
409 */
410static inline int nlmsg_report(struct nlmsghdr *nlh)
411{
412 return !!(nlh->nlmsg_flags & NLM_F_ECHO);
413}
414
415/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100416 * 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
421 */
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)
425
426#if 0
427/* FIXME: Enable once all users have been converted */
428
429/**
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
437 *
438 * The caller is responsible to ensure that the skb provides enough
439 * tailroom for both the netlink header and payload.
440 */
441static inline struct nlmsghdr *__nlmsg_put(struct sk_buff *skb, u32 pid,
442 u32 seq, int type, int payload,
443 int flags)
444{
445 struct nlmsghdr *nlh;
446
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;
453
454 memset((unsigned char *) nlmsg_data(nlh) + payload, 0,
455 nlmsg_padlen(payload));
456
457 return nlh;
458}
459#endif
460
461/**
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
469 *
470 * Returns NULL if the tailroom of the skb is insufficient to store
471 * the message header and payload.
472 */
473static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
474 int type, int payload, int flags)
475{
476 if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
477 return NULL;
478
479 return __nlmsg_put(skb, pid, seq, type, payload, flags);
480}
481
482/**
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
489 *
490 * Returns NULL if the tailroom of the skb is insufficient to store
491 * the message header and payload.
492 */
493static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
494 struct netlink_callback *cb,
495 int type, int payload,
496 int flags)
497{
498 return nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
499 type, payload, flags);
500}
501
502/**
503 * nlmsg_new - Allocate a new netlink message
Thomas Graf339bf982006-11-10 14:10:15 -0800504 * @payload: size of the message payload
Thomas Graffe4944e2006-08-04 23:03:05 -0700505 * @flags: the type of memory to allocate.
Thomas Grafbfa83a92005-11-10 02:25:51 +0100506 *
Thomas Graf339bf982006-11-10 14:10:15 -0800507 * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
508 * and a good default is needed.
Thomas Grafbfa83a92005-11-10 02:25:51 +0100509 */
Thomas Graf339bf982006-11-10 14:10:15 -0800510static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100511{
Thomas Graf339bf982006-11-10 14:10:15 -0800512 return alloc_skb(nlmsg_total_size(payload), flags);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100513}
514
515/**
516 * nlmsg_end - Finalize a netlink message
517 * @skb: socket buffer the message is stored in
518 * @nlh: netlink message header
519 *
520 * Corrects the netlink message header to include the appeneded
521 * attributes. Only necessary if attributes have been added to
522 * the message.
523 *
524 * Returns the total data length of the skb.
525 */
526static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
527{
528 nlh->nlmsg_len = skb->tail - (unsigned char *) nlh;
529
530 return skb->len;
531}
532
533/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700534 * nlmsg_get_pos - return current position in netlink message
535 * @skb: socket buffer the message is stored in
536 *
537 * Returns a pointer to the current tail of the message.
538 */
539static inline void *nlmsg_get_pos(struct sk_buff *skb)
540{
541 return skb->tail;
542}
543
544/**
545 * nlmsg_trim - Trim message to a mark
546 * @skb: socket buffer the message is stored in
547 * @mark: mark to trim to
548 *
549 * Trims the message to the provided mark. Returns -1.
550 */
551static inline int nlmsg_trim(struct sk_buff *skb, void *mark)
552{
553 if (mark)
554 skb_trim(skb, (unsigned char *) mark - skb->data);
555
556 return -1;
557}
558
559/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100560 * nlmsg_cancel - Cancel construction of a netlink message
561 * @skb: socket buffer the message is stored in
562 * @nlh: netlink message header
563 *
564 * Removes the complete netlink message including all
565 * attributes from the socket buffer again. Returns -1.
566 */
567static inline int nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
568{
Thomas Graffe4944e2006-08-04 23:03:05 -0700569 return nlmsg_trim(skb, nlh);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100570}
571
572/**
573 * nlmsg_free - free a netlink message
574 * @skb: socket buffer of netlink message
575 */
576static inline void nlmsg_free(struct sk_buff *skb)
577{
578 kfree_skb(skb);
579}
580
581/**
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
Thomas Grafd387f6a2006-08-15 00:31:06 -0700587 * @flags: allocation flags
Thomas Grafbfa83a92005-11-10 02:25:51 +0100588 */
589static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
Thomas Grafd387f6a2006-08-15 00:31:06 -0700590 u32 pid, unsigned int group, gfp_t flags)
Thomas Grafbfa83a92005-11-10 02:25:51 +0100591{
592 int err;
593
594 NETLINK_CB(skb).dst_group = group;
595
Thomas Grafd387f6a2006-08-15 00:31:06 -0700596 err = netlink_broadcast(sk, skb, pid, group, flags);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100597 if (err > 0)
598 err = 0;
599
600 return err;
601}
602
603/**
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
608 */
609static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 pid)
610{
611 int err;
612
613 err = netlink_unicast(sk, skb, pid, MSG_DONTWAIT);
614 if (err > 0)
615 err = 0;
616
617 return err;
618}
619
620/**
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
626 */
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)))
631
632/**************************************************************************
633 * Netlink Attributes
634 **************************************************************************/
635
636/**
637 * nla_attr_size - length of attribute not including padding
638 * @payload: length of payload
639 */
640static inline int nla_attr_size(int payload)
641{
642 return NLA_HDRLEN + payload;
643}
644
645/**
646 * nla_total_size - total length of attribute including padding
647 * @payload: length of payload
648 */
649static inline int nla_total_size(int payload)
650{
651 return NLA_ALIGN(nla_attr_size(payload));
652}
653
654/**
655 * nla_padlen - length of padding at the tail of attribute
656 * @payload: length of payload
657 */
658static inline int nla_padlen(int payload)
659{
660 return nla_total_size(payload) - nla_attr_size(payload);
661}
662
663/**
664 * nla_data - head of payload
665 * @nla: netlink attribute
666 */
667static inline void *nla_data(const struct nlattr *nla)
668{
669 return (char *) nla + NLA_HDRLEN;
670}
671
672/**
673 * nla_len - length of payload
674 * @nla: netlink attribute
675 */
676static inline int nla_len(const struct nlattr *nla)
677{
678 return nla->nla_len - NLA_HDRLEN;
679}
680
681/**
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
685 */
686static inline int nla_ok(const struct nlattr *nla, int remaining)
687{
688 return remaining >= sizeof(*nla) &&
689 nla->nla_len >= sizeof(*nla) &&
690 nla->nla_len <= remaining;
691}
692
693/**
694 * nla_next - next netlink attribte in attribute stream
695 * @nla: netlink attribute
696 * @remaining: number of bytes remaining in attribute stream
697 *
698 * Returns the next netlink attribute in the attribute stream and
699 * decrements remaining by the size of the current attribute.
700 */
701static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
702{
703 int totlen = NLA_ALIGN(nla->nla_len);
704
705 *remaining -= totlen;
706 return (struct nlattr *) ((char *) nla + totlen);
707}
708
709/**
Thomas Graffe4944e2006-08-04 23:03:05 -0700710 * 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
713 *
714 * Returns the first attribute which matches the specified type.
715 */
716static inline struct nlattr *nla_find_nested(struct nlattr *nla, int attrtype)
717{
718 return nla_find(nla_data(nla), nla_len(nla), attrtype);
719}
720
721/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100722 * 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
727 *
728 * See nla_parse()
729 */
730static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
731 struct nlattr *nla,
732 struct nla_policy *policy)
733{
734 return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
735}
736/**
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
741 */
742static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
743{
744 return nla_put(skb, attrtype, sizeof(u8), &value);
745}
746
747/**
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
752 */
753static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
754{
755 return nla_put(skb, attrtype, sizeof(u16), &value);
756}
757
758/**
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
763 */
764static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
765{
766 return nla_put(skb, attrtype, sizeof(u32), &value);
767}
768
769/**
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
774 */
775static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value)
776{
777 return nla_put(skb, attrtype, sizeof(u64), &value);
778}
779
780/**
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
785 */
786static inline int nla_put_string(struct sk_buff *skb, int attrtype,
787 const char *str)
788{
789 return nla_put(skb, attrtype, strlen(str) + 1, str);
790}
791
792/**
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
796 */
797static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
798{
799 return nla_put(skb, attrtype, 0, NULL);
800}
801
802/**
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
807 */
808static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
809 unsigned long jiffies)
810{
811 u64 tmp = jiffies_to_msecs(jiffies);
812 return nla_put(skb, attrtype, sizeof(u64), &tmp);
813}
814
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)
820
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)
826
827#define NLA_PUT_U8(skb, attrtype, value) \
828 NLA_PUT_TYPE(skb, u8, attrtype, value)
829
830#define NLA_PUT_U16(skb, attrtype, value) \
831 NLA_PUT_TYPE(skb, u16, attrtype, value)
832
Thomas Graf4a89c252006-11-24 17:14:51 -0800833#define NLA_PUT_LE16(skb, attrtype, value) \
834 NLA_PUT_TYPE(skb, __le16, attrtype, value)
835
Thomas Grafbfa83a92005-11-10 02:25:51 +0100836#define NLA_PUT_U32(skb, attrtype, value) \
837 NLA_PUT_TYPE(skb, u32, attrtype, value)
838
Al Viro00012e52006-09-26 22:14:41 -0700839#define NLA_PUT_BE32(skb, attrtype, value) \
840 NLA_PUT_TYPE(skb, __be32, attrtype, value)
841
Thomas Grafbfa83a92005-11-10 02:25:51 +0100842#define NLA_PUT_U64(skb, attrtype, value) \
843 NLA_PUT_TYPE(skb, u64, attrtype, value)
844
845#define NLA_PUT_STRING(skb, attrtype, value) \
846 NLA_PUT(skb, attrtype, strlen(value) + 1, value)
847
Johannes Bergff5dfe72006-08-26 19:17:53 -0700848#define NLA_PUT_FLAG(skb, attrtype) \
Thomas Grafbfa83a92005-11-10 02:25:51 +0100849 NLA_PUT(skb, attrtype, 0, NULL)
850
851#define NLA_PUT_MSECS(skb, attrtype, jiffies) \
852 NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies))
853
854/**
855 * nla_get_u32 - return payload of u32 attribute
856 * @nla: u32 netlink attribute
857 */
858static inline u32 nla_get_u32(struct nlattr *nla)
859{
860 return *(u32 *) nla_data(nla);
861}
862
863/**
Al Viro00012e52006-09-26 22:14:41 -0700864 * nla_get_be32 - return payload of __be32 attribute
865 * @nla: __be32 netlink attribute
866 */
867static inline __be32 nla_get_be32(struct nlattr *nla)
868{
869 return *(__be32 *) nla_data(nla);
870}
871
872/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100873 * nla_get_u16 - return payload of u16 attribute
874 * @nla: u16 netlink attribute
875 */
876static inline u16 nla_get_u16(struct nlattr *nla)
877{
878 return *(u16 *) nla_data(nla);
879}
880
881/**
Thomas Graf4a89c252006-11-24 17:14:51 -0800882 * nla_get_le16 - return payload of __le16 attribute
883 * @nla: __le16 netlink attribute
884 */
885static inline __le16 nla_get_le16(struct nlattr *nla)
886{
887 return *(__le16 *) nla_data(nla);
888}
889
890/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100891 * nla_get_u8 - return payload of u8 attribute
892 * @nla: u8 netlink attribute
893 */
894static inline u8 nla_get_u8(struct nlattr *nla)
895{
896 return *(u8 *) nla_data(nla);
897}
898
899/**
900 * nla_get_u64 - return payload of u64 attribute
901 * @nla: u64 netlink attribute
902 */
903static inline u64 nla_get_u64(struct nlattr *nla)
904{
905 u64 tmp;
906
907 nla_memcpy(&tmp, nla, sizeof(tmp));
908
909 return tmp;
910}
911
912/**
913 * nla_get_flag - return payload of flag attribute
914 * @nla: flag netlink attribute
915 */
916static inline int nla_get_flag(struct nlattr *nla)
917{
918 return !!nla;
919}
920
921/**
922 * nla_get_msecs - return payload of msecs attribute
923 * @nla: msecs netlink attribute
924 *
925 * Returns the number of milliseconds in jiffies.
926 */
927static inline unsigned long nla_get_msecs(struct nlattr *nla)
928{
929 u64 msecs = nla_get_u64(nla);
930
931 return msecs_to_jiffies((unsigned long) msecs);
932}
933
934/**
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
938 *
939 * Returns the container attribute
940 */
941static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
942{
943 struct nlattr *start = (struct nlattr *) skb->tail;
944
945 if (nla_put(skb, attrtype, 0, NULL) < 0)
946 return NULL;
947
948 return start;
949}
950
951/**
952 * nla_nest_end - Finalize nesting of attributes
953 * @skb: socket buffer the attribtues are stored in
954 * @start: container attribute
955 *
956 * Corrects the container attribute header to include the all
957 * appeneded attributes.
958 *
959 * Returns the total data length of the skb.
960 */
961static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
962{
963 start->nla_len = skb->tail - (unsigned char *) start;
964 return skb->len;
965}
966
967/**
968 * nla_nest_cancel - Cancel nesting of attributes
969 * @skb: socket buffer the message is stored in
970 * @start: container attribute
971 *
972 * Removes the container attribute and including all nested
973 * attributes. Returns -1.
974 */
975static inline int nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
976{
Thomas Graffe4944e2006-08-04 23:03:05 -0700977 return nlmsg_trim(skb, start);
Thomas Grafbfa83a92005-11-10 02:25:51 +0100978}
979
980/**
Paul Moore4fe5d5c2006-09-25 15:54:03 -0700981 * 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
985 *
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.
989 *
990 * Returns 0 on success or a negative error code.
991 */
992static inline int nla_validate_nested(struct nlattr *start, int maxtype,
993 struct nla_policy *policy)
994{
995 return nla_validate(nla_data(start), nla_len(start), maxtype, policy);
996}
997
998/**
Thomas Grafbfa83a92005-11-10 02:25:51 +0100999 * 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
1004 */
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)))
1009
Thomas Graffe4944e2006-08-04 23:03:05 -07001010/**
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
1015 */
1016#define nla_for_each_nested(pos, nla, rem) \
1017 nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
1018
Thomas Grafbfa83a92005-11-10 02:25:51 +01001019#endif