]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - include/net/netlink.h
47044da167c50793377d257029fd71e919236bb9
[mirror_ubuntu-zesty-kernel.git] / include / net / netlink.h
1 #ifndef __NET_NETLINK_H
2 #define __NET_NETLINK_H
3
4 #include <linux/types.h>
5 #include <linux/netlink.h>
6
7 /* ========================================================================
8 * Netlink Messages and Attributes Interface (As Seen On TV)
9 * ------------------------------------------------------------------------
10 * Messages Interface
11 * ------------------------------------------------------------------------
12 *
13 * Message Format:
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)-----------------------+
21 *
22 * Payload Format:
23 * <---------------------- nlmsg_len(nlh) --------------------->
24 * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) ->
25 * +----------------------+- - -+--------------------------------+
26 * | Family Header | Pad | Attributes |
27 * +----------------------+- - -+--------------------------------+
28 * nlmsg_attrdata(nlh, hdrlen)---^
29 *
30 * Data Structures:
31 * struct nlmsghdr netlink message header
32 *
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
42 *
43 * Message Sending:
44 * nlmsg_multicast() multicast message to several groups
45 * nlmsg_unicast() unicast a message to a single socket
46 * nlmsg_notify() send notification message
47 *
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
52 *
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
58 *
59 * Message Parsing:
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
67 *
68 * Misc:
69 * nlmsg_report() report back to application?
70 *
71 * ------------------------------------------------------------------------
72 * Attributes Interface
73 * ------------------------------------------------------------------------
74 *
75 * Attribute Format:
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)-----------------------------'
84 *
85 * Data Structures:
86 * struct nlattr netlink attribtue header
87 *
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
93 *
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
102 *
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
112 *
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
115 * of a failure.
116 *
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
121 *
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
126 *
127 * Attribute Payload Access:
128 * nla_data(nla) head of attribute payload
129 * nla_len(nla) length of attribute payload
130 *
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
138 *
139 * Attribute Misc:
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
144 *
145 * Attribute Parsing:
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 *=========================================================================
155 */
156
157 /**
158 * Standard attribute types to specify validation policy
159 */
160 enum {
161 NLA_UNSPEC,
162 NLA_U8,
163 NLA_U16,
164 NLA_U32,
165 NLA_U64,
166 NLA_STRING,
167 NLA_FLAG,
168 NLA_MSECS,
169 NLA_NESTED,
170 __NLA_TYPE_MAX,
171 };
172
173 #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
174
175 /**
176 * struct nla_policy - attribute validation policy
177 * @type: Type of attribute or NLA_UNSPEC
178 * @minlen: Minimal length of payload required to be available
179 *
180 * Policies are defined as arrays of this struct, the array must be
181 * accessible by attribute type up to the highest identifier to be expected.
182 *
183 * Example:
184 * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = {
185 * [ATTR_FOO] = { .type = NLA_U16 },
186 * [ATTR_BAR] = { .type = NLA_STRING },
187 * [ATTR_BAZ] = { .minlen = sizeof(struct mystruct) },
188 * };
189 */
190 struct nla_policy {
191 u16 type;
192 u16 minlen;
193 };
194
195 /**
196 * struct nl_info - netlink source information
197 * @nlh: Netlink message header of original request
198 * @pid: Netlink PID of requesting application
199 */
200 struct nl_info {
201 struct nlmsghdr *nlh;
202 u32 pid;
203 };
204
205 extern void netlink_run_queue(struct sock *sk, unsigned int *qlen,
206 int (*cb)(struct sk_buff *,
207 struct nlmsghdr *, int *));
208 extern void netlink_queue_skip(struct nlmsghdr *nlh,
209 struct sk_buff *skb);
210 extern int nlmsg_notify(struct sock *sk, struct sk_buff *skb,
211 u32 pid, unsigned int group, int report,
212 gfp_t flags);
213
214 extern int nla_validate(struct nlattr *head, int len, int maxtype,
215 struct nla_policy *policy);
216 extern int nla_parse(struct nlattr *tb[], int maxtype,
217 struct nlattr *head, int len,
218 struct nla_policy *policy);
219 extern struct nlattr * nla_find(struct nlattr *head, int len, int attrtype);
220 extern size_t nla_strlcpy(char *dst, const struct nlattr *nla,
221 size_t dstsize);
222 extern int nla_memcpy(void *dest, struct nlattr *src, int count);
223 extern int nla_memcmp(const struct nlattr *nla, const void *data,
224 size_t size);
225 extern int nla_strcmp(const struct nlattr *nla, const char *str);
226 extern struct nlattr * __nla_reserve(struct sk_buff *skb, int attrtype,
227 int attrlen);
228 extern void * __nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
229 extern struct nlattr * nla_reserve(struct sk_buff *skb, int attrtype,
230 int attrlen);
231 extern void * nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
232 extern void __nla_put(struct sk_buff *skb, int attrtype,
233 int attrlen, const void *data);
234 extern void __nla_put_nohdr(struct sk_buff *skb, int attrlen,
235 const void *data);
236 extern int nla_put(struct sk_buff *skb, int attrtype,
237 int attrlen, const void *data);
238 extern int nla_put_nohdr(struct sk_buff *skb, int attrlen,
239 const void *data);
240
241 /**************************************************************************
242 * Netlink Messages
243 **************************************************************************/
244
245 /**
246 * nlmsg_msg_size - length of netlink message not including padding
247 * @payload: length of message payload
248 */
249 static inline int nlmsg_msg_size(int payload)
250 {
251 return NLMSG_HDRLEN + payload;
252 }
253
254 /**
255 * nlmsg_total_size - length of netlink message including padding
256 * @payload: length of message payload
257 */
258 static inline int nlmsg_total_size(int payload)
259 {
260 return NLMSG_ALIGN(nlmsg_msg_size(payload));
261 }
262
263 /**
264 * nlmsg_padlen - length of padding at the message's tail
265 * @payload: length of message payload
266 */
267 static inline int nlmsg_padlen(int payload)
268 {
269 return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
270 }
271
272 /**
273 * nlmsg_data - head of message payload
274 * @nlh: netlink messsage header
275 */
276 static inline void *nlmsg_data(const struct nlmsghdr *nlh)
277 {
278 return (unsigned char *) nlh + NLMSG_HDRLEN;
279 }
280
281 /**
282 * nlmsg_len - length of message payload
283 * @nlh: netlink message header
284 */
285 static inline int nlmsg_len(const struct nlmsghdr *nlh)
286 {
287 return nlh->nlmsg_len - NLMSG_HDRLEN;
288 }
289
290 /**
291 * nlmsg_attrdata - head of attributes data
292 * @nlh: netlink message header
293 * @hdrlen: length of family specific header
294 */
295 static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
296 int hdrlen)
297 {
298 unsigned char *data = nlmsg_data(nlh);
299 return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
300 }
301
302 /**
303 * nlmsg_attrlen - length of attributes data
304 * @nlh: netlink message header
305 * @hdrlen: length of family specific header
306 */
307 static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
308 {
309 return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
310 }
311
312 /**
313 * nlmsg_ok - check if the netlink message fits into the remaining bytes
314 * @nlh: netlink message header
315 * @remaining: number of bytes remaining in message stream
316 */
317 static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
318 {
319 return (remaining >= sizeof(struct nlmsghdr) &&
320 nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
321 nlh->nlmsg_len <= remaining);
322 }
323
324 /**
325 * nlmsg_next - next netlink message in message stream
326 * @nlh: netlink message header
327 * @remaining: number of bytes remaining in message stream
328 *
329 * Returns the next netlink message in the message stream and
330 * decrements remaining by the size of the current message.
331 */
332 static inline struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
333 {
334 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
335
336 *remaining -= totlen;
337
338 return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
339 }
340
341 /**
342 * nlmsg_parse - parse attributes of a netlink message
343 * @nlh: netlink message header
344 * @hdrlen: length of family specific header
345 * @tb: destination array with maxtype+1 elements
346 * @maxtype: maximum attribute type to be expected
347 * @policy: validation policy
348 *
349 * See nla_parse()
350 */
351 static inline int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen,
352 struct nlattr *tb[], int maxtype,
353 struct nla_policy *policy)
354 {
355 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
356 return -EINVAL;
357
358 return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
359 nlmsg_attrlen(nlh, hdrlen), policy);
360 }
361
362 /**
363 * nlmsg_find_attr - find a specific attribute in a netlink message
364 * @nlh: netlink message header
365 * @hdrlen: length of familiy specific header
366 * @attrtype: type of attribute to look for
367 *
368 * Returns the first attribute which matches the specified type.
369 */
370 static inline struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh,
371 int hdrlen, int attrtype)
372 {
373 return nla_find(nlmsg_attrdata(nlh, hdrlen),
374 nlmsg_attrlen(nlh, hdrlen), attrtype);
375 }
376
377 /**
378 * nlmsg_validate - validate a netlink message including attributes
379 * @nlh: netlinket message header
380 * @hdrlen: length of familiy specific header
381 * @maxtype: maximum attribute type to be expected
382 * @policy: validation policy
383 */
384 static inline int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
385 struct nla_policy *policy)
386 {
387 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
388 return -EINVAL;
389
390 return nla_validate(nlmsg_attrdata(nlh, hdrlen),
391 nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
392 }
393
394 /**
395 * nlmsg_report - need to report back to application?
396 * @nlh: netlink message header
397 *
398 * Returns 1 if a report back to the application is requested.
399 */
400 static inline int nlmsg_report(struct nlmsghdr *nlh)
401 {
402 return !!(nlh->nlmsg_flags & NLM_F_ECHO);
403 }
404
405 /**
406 * nlmsg_for_each_attr - iterate over a stream of attributes
407 * @pos: loop counter, set to current attribute
408 * @nlh: netlink message header
409 * @hdrlen: length of familiy specific header
410 * @rem: initialized to len, holds bytes currently remaining in stream
411 */
412 #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
413 nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
414 nlmsg_attrlen(nlh, hdrlen), rem)
415
416 #if 0
417 /* FIXME: Enable once all users have been converted */
418
419 /**
420 * __nlmsg_put - Add a new netlink message to an skb
421 * @skb: socket buffer to store message in
422 * @pid: netlink process id
423 * @seq: sequence number of message
424 * @type: message type
425 * @payload: length of message payload
426 * @flags: message flags
427 *
428 * The caller is responsible to ensure that the skb provides enough
429 * tailroom for both the netlink header and payload.
430 */
431 static inline struct nlmsghdr *__nlmsg_put(struct sk_buff *skb, u32 pid,
432 u32 seq, int type, int payload,
433 int flags)
434 {
435 struct nlmsghdr *nlh;
436
437 nlh = (struct nlmsghdr *) skb_put(skb, nlmsg_total_size(payload));
438 nlh->nlmsg_type = type;
439 nlh->nlmsg_len = nlmsg_msg_size(payload);
440 nlh->nlmsg_flags = flags;
441 nlh->nlmsg_pid = pid;
442 nlh->nlmsg_seq = seq;
443
444 memset((unsigned char *) nlmsg_data(nlh) + payload, 0,
445 nlmsg_padlen(payload));
446
447 return nlh;
448 }
449 #endif
450
451 /**
452 * nlmsg_put - Add a new netlink message to an skb
453 * @skb: socket buffer to store message in
454 * @pid: netlink process id
455 * @seq: sequence number of message
456 * @type: message type
457 * @payload: length of message payload
458 * @flags: message flags
459 *
460 * Returns NULL if the tailroom of the skb is insufficient to store
461 * the message header and payload.
462 */
463 static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
464 int type, int payload, int flags)
465 {
466 if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
467 return NULL;
468
469 return __nlmsg_put(skb, pid, seq, type, payload, flags);
470 }
471
472 /**
473 * nlmsg_put_answer - Add a new callback based netlink message to an skb
474 * @skb: socket buffer to store message in
475 * @cb: netlink callback
476 * @type: message type
477 * @payload: length of message payload
478 * @flags: message flags
479 *
480 * Returns NULL if the tailroom of the skb is insufficient to store
481 * the message header and payload.
482 */
483 static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
484 struct netlink_callback *cb,
485 int type, int payload,
486 int flags)
487 {
488 return nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
489 type, payload, flags);
490 }
491
492 /**
493 * nlmsg_new - Allocate a new netlink message
494 * @size: maximum size of message
495 * @flags: the type of memory to allocate.
496 *
497 * Use NLMSG_GOODSIZE if size isn't know and you need a good default size.
498 */
499 static inline struct sk_buff *nlmsg_new(int size, gfp_t flags)
500 {
501 return alloc_skb(size, flags);
502 }
503
504 /**
505 * nlmsg_end - Finalize a netlink message
506 * @skb: socket buffer the message is stored in
507 * @nlh: netlink message header
508 *
509 * Corrects the netlink message header to include the appeneded
510 * attributes. Only necessary if attributes have been added to
511 * the message.
512 *
513 * Returns the total data length of the skb.
514 */
515 static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
516 {
517 nlh->nlmsg_len = skb->tail - (unsigned char *) nlh;
518
519 return skb->len;
520 }
521
522 /**
523 * nlmsg_get_pos - return current position in netlink message
524 * @skb: socket buffer the message is stored in
525 *
526 * Returns a pointer to the current tail of the message.
527 */
528 static inline void *nlmsg_get_pos(struct sk_buff *skb)
529 {
530 return skb->tail;
531 }
532
533 /**
534 * nlmsg_trim - Trim message to a mark
535 * @skb: socket buffer the message is stored in
536 * @mark: mark to trim to
537 *
538 * Trims the message to the provided mark. Returns -1.
539 */
540 static inline int nlmsg_trim(struct sk_buff *skb, void *mark)
541 {
542 if (mark)
543 skb_trim(skb, (unsigned char *) mark - skb->data);
544
545 return -1;
546 }
547
548 /**
549 * nlmsg_cancel - Cancel construction of a netlink message
550 * @skb: socket buffer the message is stored in
551 * @nlh: netlink message header
552 *
553 * Removes the complete netlink message including all
554 * attributes from the socket buffer again. Returns -1.
555 */
556 static inline int nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
557 {
558 return nlmsg_trim(skb, nlh);
559 }
560
561 /**
562 * nlmsg_free - free a netlink message
563 * @skb: socket buffer of netlink message
564 */
565 static inline void nlmsg_free(struct sk_buff *skb)
566 {
567 kfree_skb(skb);
568 }
569
570 /**
571 * nlmsg_multicast - multicast a netlink message
572 * @sk: netlink socket to spread messages to
573 * @skb: netlink message as socket buffer
574 * @pid: own netlink pid to avoid sending to yourself
575 * @group: multicast group id
576 * @flags: allocation flags
577 */
578 static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
579 u32 pid, unsigned int group, gfp_t flags)
580 {
581 int err;
582
583 NETLINK_CB(skb).dst_group = group;
584
585 err = netlink_broadcast(sk, skb, pid, group, flags);
586 if (err > 0)
587 err = 0;
588
589 return err;
590 }
591
592 /**
593 * nlmsg_unicast - unicast a netlink message
594 * @sk: netlink socket to spread message to
595 * @skb: netlink message as socket buffer
596 * @pid: netlink pid of the destination socket
597 */
598 static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 pid)
599 {
600 int err;
601
602 err = netlink_unicast(sk, skb, pid, MSG_DONTWAIT);
603 if (err > 0)
604 err = 0;
605
606 return err;
607 }
608
609 /**
610 * nlmsg_for_each_msg - iterate over a stream of messages
611 * @pos: loop counter, set to current message
612 * @head: head of message stream
613 * @len: length of message stream
614 * @rem: initialized to len, holds bytes currently remaining in stream
615 */
616 #define nlmsg_for_each_msg(pos, head, len, rem) \
617 for (pos = head, rem = len; \
618 nlmsg_ok(pos, rem); \
619 pos = nlmsg_next(pos, &(rem)))
620
621 /**************************************************************************
622 * Netlink Attributes
623 **************************************************************************/
624
625 /**
626 * nla_attr_size - length of attribute not including padding
627 * @payload: length of payload
628 */
629 static inline int nla_attr_size(int payload)
630 {
631 return NLA_HDRLEN + payload;
632 }
633
634 /**
635 * nla_total_size - total length of attribute including padding
636 * @payload: length of payload
637 */
638 static inline int nla_total_size(int payload)
639 {
640 return NLA_ALIGN(nla_attr_size(payload));
641 }
642
643 /**
644 * nla_padlen - length of padding at the tail of attribute
645 * @payload: length of payload
646 */
647 static inline int nla_padlen(int payload)
648 {
649 return nla_total_size(payload) - nla_attr_size(payload);
650 }
651
652 /**
653 * nla_data - head of payload
654 * @nla: netlink attribute
655 */
656 static inline void *nla_data(const struct nlattr *nla)
657 {
658 return (char *) nla + NLA_HDRLEN;
659 }
660
661 /**
662 * nla_len - length of payload
663 * @nla: netlink attribute
664 */
665 static inline int nla_len(const struct nlattr *nla)
666 {
667 return nla->nla_len - NLA_HDRLEN;
668 }
669
670 /**
671 * nla_ok - check if the netlink attribute fits into the remaining bytes
672 * @nla: netlink attribute
673 * @remaining: number of bytes remaining in attribute stream
674 */
675 static inline int nla_ok(const struct nlattr *nla, int remaining)
676 {
677 return remaining >= sizeof(*nla) &&
678 nla->nla_len >= sizeof(*nla) &&
679 nla->nla_len <= remaining;
680 }
681
682 /**
683 * nla_next - next netlink attribte in attribute stream
684 * @nla: netlink attribute
685 * @remaining: number of bytes remaining in attribute stream
686 *
687 * Returns the next netlink attribute in the attribute stream and
688 * decrements remaining by the size of the current attribute.
689 */
690 static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
691 {
692 int totlen = NLA_ALIGN(nla->nla_len);
693
694 *remaining -= totlen;
695 return (struct nlattr *) ((char *) nla + totlen);
696 }
697
698 /**
699 * nla_find_nested - find attribute in a set of nested attributes
700 * @nla: attribute containing the nested attributes
701 * @attrtype: type of attribute to look for
702 *
703 * Returns the first attribute which matches the specified type.
704 */
705 static inline struct nlattr *nla_find_nested(struct nlattr *nla, int attrtype)
706 {
707 return nla_find(nla_data(nla), nla_len(nla), attrtype);
708 }
709
710 /**
711 * nla_parse_nested - parse nested attributes
712 * @tb: destination array with maxtype+1 elements
713 * @maxtype: maximum attribute type to be expected
714 * @nla: attribute containing the nested attributes
715 * @policy: validation policy
716 *
717 * See nla_parse()
718 */
719 static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
720 struct nlattr *nla,
721 struct nla_policy *policy)
722 {
723 return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
724 }
725 /**
726 * nla_put_u8 - Add a u16 netlink attribute to a socket buffer
727 * @skb: socket buffer to add attribute to
728 * @attrtype: attribute type
729 * @value: numeric value
730 */
731 static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
732 {
733 return nla_put(skb, attrtype, sizeof(u8), &value);
734 }
735
736 /**
737 * nla_put_u16 - 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 */
742 static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
743 {
744 return nla_put(skb, attrtype, sizeof(u16), &value);
745 }
746
747 /**
748 * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
749 * @skb: socket buffer to add attribute to
750 * @attrtype: attribute type
751 * @value: numeric value
752 */
753 static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
754 {
755 return nla_put(skb, attrtype, sizeof(u32), &value);
756 }
757
758 /**
759 * nla_put_64 - Add a u64 netlink attribute to a socket buffer
760 * @skb: socket buffer to add attribute to
761 * @attrtype: attribute type
762 * @value: numeric value
763 */
764 static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value)
765 {
766 return nla_put(skb, attrtype, sizeof(u64), &value);
767 }
768
769 /**
770 * nla_put_string - Add a string netlink attribute to a socket buffer
771 * @skb: socket buffer to add attribute to
772 * @attrtype: attribute type
773 * @str: NUL terminated string
774 */
775 static inline int nla_put_string(struct sk_buff *skb, int attrtype,
776 const char *str)
777 {
778 return nla_put(skb, attrtype, strlen(str) + 1, str);
779 }
780
781 /**
782 * nla_put_flag - Add a flag netlink attribute to a socket buffer
783 * @skb: socket buffer to add attribute to
784 * @attrtype: attribute type
785 */
786 static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
787 {
788 return nla_put(skb, attrtype, 0, NULL);
789 }
790
791 /**
792 * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
793 * @skb: socket buffer to add attribute to
794 * @attrtype: attribute type
795 * @jiffies: number of msecs in jiffies
796 */
797 static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
798 unsigned long jiffies)
799 {
800 u64 tmp = jiffies_to_msecs(jiffies);
801 return nla_put(skb, attrtype, sizeof(u64), &tmp);
802 }
803
804 #define NLA_PUT(skb, attrtype, attrlen, data) \
805 do { \
806 if (nla_put(skb, attrtype, attrlen, data) < 0) \
807 goto nla_put_failure; \
808 } while(0)
809
810 #define NLA_PUT_TYPE(skb, type, attrtype, value) \
811 do { \
812 type __tmp = value; \
813 NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \
814 } while(0)
815
816 #define NLA_PUT_U8(skb, attrtype, value) \
817 NLA_PUT_TYPE(skb, u8, attrtype, value)
818
819 #define NLA_PUT_U16(skb, attrtype, value) \
820 NLA_PUT_TYPE(skb, u16, attrtype, value)
821
822 #define NLA_PUT_U32(skb, attrtype, value) \
823 NLA_PUT_TYPE(skb, u32, attrtype, value)
824
825 #define NLA_PUT_U64(skb, attrtype, value) \
826 NLA_PUT_TYPE(skb, u64, attrtype, value)
827
828 #define NLA_PUT_STRING(skb, attrtype, value) \
829 NLA_PUT(skb, attrtype, strlen(value) + 1, value)
830
831 #define NLA_PUT_FLAG(skb, attrtype, value) \
832 NLA_PUT(skb, attrtype, 0, NULL)
833
834 #define NLA_PUT_MSECS(skb, attrtype, jiffies) \
835 NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies))
836
837 /**
838 * nla_get_u32 - return payload of u32 attribute
839 * @nla: u32 netlink attribute
840 */
841 static inline u32 nla_get_u32(struct nlattr *nla)
842 {
843 return *(u32 *) nla_data(nla);
844 }
845
846 /**
847 * nla_get_u16 - return payload of u16 attribute
848 * @nla: u16 netlink attribute
849 */
850 static inline u16 nla_get_u16(struct nlattr *nla)
851 {
852 return *(u16 *) nla_data(nla);
853 }
854
855 /**
856 * nla_get_u8 - return payload of u8 attribute
857 * @nla: u8 netlink attribute
858 */
859 static inline u8 nla_get_u8(struct nlattr *nla)
860 {
861 return *(u8 *) nla_data(nla);
862 }
863
864 /**
865 * nla_get_u64 - return payload of u64 attribute
866 * @nla: u64 netlink attribute
867 */
868 static inline u64 nla_get_u64(struct nlattr *nla)
869 {
870 u64 tmp;
871
872 nla_memcpy(&tmp, nla, sizeof(tmp));
873
874 return tmp;
875 }
876
877 /**
878 * nla_get_flag - return payload of flag attribute
879 * @nla: flag netlink attribute
880 */
881 static inline int nla_get_flag(struct nlattr *nla)
882 {
883 return !!nla;
884 }
885
886 /**
887 * nla_get_msecs - return payload of msecs attribute
888 * @nla: msecs netlink attribute
889 *
890 * Returns the number of milliseconds in jiffies.
891 */
892 static inline unsigned long nla_get_msecs(struct nlattr *nla)
893 {
894 u64 msecs = nla_get_u64(nla);
895
896 return msecs_to_jiffies((unsigned long) msecs);
897 }
898
899 /**
900 * nla_nest_start - Start a new level of nested attributes
901 * @skb: socket buffer to add attributes to
902 * @attrtype: attribute type of container
903 *
904 * Returns the container attribute
905 */
906 static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
907 {
908 struct nlattr *start = (struct nlattr *) skb->tail;
909
910 if (nla_put(skb, attrtype, 0, NULL) < 0)
911 return NULL;
912
913 return start;
914 }
915
916 /**
917 * nla_nest_end - Finalize nesting of attributes
918 * @skb: socket buffer the attribtues are stored in
919 * @start: container attribute
920 *
921 * Corrects the container attribute header to include the all
922 * appeneded attributes.
923 *
924 * Returns the total data length of the skb.
925 */
926 static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
927 {
928 start->nla_len = skb->tail - (unsigned char *) start;
929 return skb->len;
930 }
931
932 /**
933 * nla_nest_cancel - Cancel nesting of attributes
934 * @skb: socket buffer the message is stored in
935 * @start: container attribute
936 *
937 * Removes the container attribute and including all nested
938 * attributes. Returns -1.
939 */
940 static inline int nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
941 {
942 return nlmsg_trim(skb, start);
943 }
944
945 /**
946 * nla_for_each_attr - iterate over a stream of attributes
947 * @pos: loop counter, set to current attribute
948 * @head: head of attribute stream
949 * @len: length of attribute stream
950 * @rem: initialized to len, holds bytes currently remaining in stream
951 */
952 #define nla_for_each_attr(pos, head, len, rem) \
953 for (pos = head, rem = len; \
954 nla_ok(pos, rem); \
955 pos = nla_next(pos, &(rem)))
956
957 /**
958 * nla_for_each_nested - iterate over nested attributes
959 * @pos: loop counter, set to current attribute
960 * @nla: attribute containing the nested attributes
961 * @rem: initialized to len, holds bytes currently remaining in stream
962 */
963 #define nla_for_each_nested(pos, nla, rem) \
964 nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
965
966 #endif