blob: 3996aafdf757da7b28ddd8b73cac8f45f124ba89 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#ifndef __NET_NETLINK_H
2#define __NET_NETLINK_H
3
4#include <linux/types.h>
5#include <linux/netlink.h>
6#include <linux/jiffies.h>
7
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() finalize 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
43 *
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
48 *
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 *
69 * Misc:
70 * nlmsg_report() report back to application?
71 *
72 * ------------------------------------------------------------------------
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 attribute header
88 *
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
94 * nla_append(skb, len, data) append data to skb
95 *
96 * Attribute Construction for Basic Types:
97 * nla_put_u8(skb, type, value) add u8 attribute to skb
98 * nla_put_u16(skb, type, value) add u16 attribute to skb
99 * nla_put_u32(skb, type, value) add u32 attribute to skb
100 * nla_put_u64(skb, type, value) add u64 attribute to skb
101 * nla_put_string(skb, type, str) add string attribute to skb
102 * nla_put_flag(skb, type) add flag attribute to skb
103 * nla_put_msecs(skb, type, jiffies) add msecs attribute to skb
104 *
105 * Exceptions Based Attribute Construction:
106 * NLA_PUT(skb, type, len, data) add attribute to skb
107 * NLA_PUT_U8(skb, type, value) add u8 attribute to skb
108 * NLA_PUT_U16(skb, type, value) add u16 attribute to skb
109 * NLA_PUT_U32(skb, type, value) add u32 attribute to skb
110 * NLA_PUT_U64(skb, type, value) add u64 attribute to skb
111 * NLA_PUT_STRING(skb, type, str) add string attribute to skb
112 * NLA_PUT_FLAG(skb, type) add flag attribute to skb
113 * NLA_PUT_MSECS(skb, type, jiffies) add msecs attribute to skb
114 *
115 * The meaning of these functions is equal to their lower case
116 * variants but they jump to the label nla_put_failure in case
117 * of a failure.
118 *
119 * Nested Attributes Construction:
120 * nla_nest_start(skb, type) start a nested attribute
121 * nla_nest_end(skb, nla) finalize a nested attribute
122 * nla_nest_cancel(skb, nla) cancel nested attribute construction
123 *
124 * Attribute Length Calculations:
125 * nla_attr_size(payload) length of attribute w/o padding
126 * nla_total_size(payload) length of attribute w/ padding
127 * nla_padlen(payload) length of padding
128 *
129 * Attribute Payload Access:
130 * nla_data(nla) head of attribute payload
131 * nla_len(nla) length of attribute payload
132 *
133 * Attribute Payload Access for Basic Types:
134 * nla_get_u8(nla) get payload for a u8 attribute
135 * nla_get_u16(nla) get payload for a u16 attribute
136 * nla_get_u32(nla) get payload for a u32 attribute
137 * nla_get_u64(nla) get payload for a u64 attribute
138 * nla_get_flag(nla) return 1 if flag is true
139 * nla_get_msecs(nla) get payload for a msecs attribute
140 *
141 * Attribute Misc:
142 * nla_memcpy(dest, nla, count) copy attribute into memory
143 * nla_memcmp(nla, data, size) compare attribute with memory area
144 * nla_strlcpy(dst, nla, size) copy attribute to a sized string
145 * nla_strcmp(nla, str) compare attribute with string
146 *
147 * Attribute Parsing:
148 * nla_ok(nla, remaining) does nla fit into remaining bytes?
149 * nla_next(nla, remaining) get next netlink attribute
150 * nla_validate() validate a stream of attributes
151 * nla_validate_nested() validate a stream of nested attributes
152 * nla_find() find attribute in stream of attributes
153 * nla_find_nested() find attribute in nested attributes
154 * nla_parse() parse and validate stream of attrs
155 * nla_parse_nested() parse nested attribuets
156 * nla_for_each_attr() loop over all attributes
157 * nla_for_each_nested() loop over the nested attributes
158 *=========================================================================
159 */
160
161 /**
162 * Standard attribute types to specify validation policy
163 */
164enum {
165 NLA_UNSPEC,
166 NLA_U8,
167 NLA_U16,
168 NLA_U32,
169 NLA_U64,
170 NLA_STRING,
171 NLA_FLAG,
172 NLA_MSECS,
173 NLA_NESTED,
174 NLA_NESTED_COMPAT,
175 NLA_NUL_STRING,
176 NLA_BINARY,
177#if defined(CONFIG_AIC8800)
178 NLA_S8,
179 NLA_S16,
180 NLA_S32,
181 NLA_S64,
182 NLA_BITFIELD32,
183 NLA_REJECT,
184 NLA_EXACT_LEN,
185 NLA_EXACT_LEN_WARN,
186 NLA_MIN_LEN,
187#endif
188 __NLA_TYPE_MAX,
189};
190
191#define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
192
193/**
194 * struct nla_policy - attribute validation policy
195 * @type: Type of attribute or NLA_UNSPEC
196 * @len: Type specific length of payload
197 *
198 * Policies are defined as arrays of this struct, the array must be
199 * accessible by attribute type up to the highest identifier to be expected.
200 *
201 * Meaning of `len' field:
202 * NLA_STRING Maximum length of string
203 * NLA_NUL_STRING Maximum length of string (excluding NUL)
204 * NLA_FLAG Unused
205 * NLA_BINARY Maximum length of attribute payload
206 * NLA_NESTED Don't use `len' field -- length verification is
207 * done by checking len of nested header (or empty)
208 * NLA_NESTED_COMPAT Minimum length of structure payload
209 * NLA_U8, NLA_U16,
210 * NLA_U32, NLA_U64,
211 * NLA_MSECS Leaving the length field zero will verify the
212 * given type fits, using it verifies minimum length
213 * just like "All other"
214 * All other Minimum length of attribute payload
215 *
216 * Example:
217 * static const struct nla_policy my_policy[ATTR_MAX+1] = {
218 * [ATTR_FOO] = { .type = NLA_U16 },
219 * [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
220 * [ATTR_BAZ] = { .len = sizeof(struct mystruct) },
221 * };
222 */
223struct nla_policy {
224 u16 type;
225 u16 len;
226};
227
228/**
229 * struct nl_info - netlink source information
230 * @nlh: Netlink message header of original request
231 * @pid: Netlink PID of requesting application
232 */
233struct nl_info {
234 struct nlmsghdr *nlh;
235 struct net *nl_net;
236 u32 pid;
237};
238
239extern int netlink_rcv_skb(struct sk_buff *skb,
240 int (*cb)(struct sk_buff *,
241 struct nlmsghdr *));
242extern int nlmsg_notify(struct sock *sk, struct sk_buff *skb,
243 u32 pid, unsigned int group, int report,
244 gfp_t flags);
245
246extern int nla_validate(const struct nlattr *head,
247 int len, int maxtype,
248 const struct nla_policy *policy);
249extern int nla_parse(struct nlattr **tb, int maxtype,
250 const struct nlattr *head, int len,
251 const struct nla_policy *policy);
252extern int nla_policy_len(const struct nla_policy *, int);
253extern struct nlattr * nla_find(const struct nlattr *head,
254 int len, int attrtype);
255extern size_t nla_strlcpy(char *dst, const struct nlattr *nla,
256 size_t dstsize);
257extern int nla_memcpy(void *dest, const struct nlattr *src, int count);
258extern int nla_memcmp(const struct nlattr *nla, const void *data,
259 size_t size);
260extern int nla_strcmp(const struct nlattr *nla, const char *str);
261extern struct nlattr * __nla_reserve(struct sk_buff *skb, int attrtype,
262 int attrlen);
263extern void * __nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
264extern struct nlattr * nla_reserve(struct sk_buff *skb, int attrtype,
265 int attrlen);
266extern void * nla_reserve_nohdr(struct sk_buff *skb, int attrlen);
267extern void __nla_put(struct sk_buff *skb, int attrtype,
268 int attrlen, const void *data);
269extern void __nla_put_nohdr(struct sk_buff *skb, int attrlen,
270 const void *data);
271extern int nla_put(struct sk_buff *skb, int attrtype,
272 int attrlen, const void *data);
273extern int nla_put_nohdr(struct sk_buff *skb, int attrlen,
274 const void *data);
275extern int nla_append(struct sk_buff *skb, int attrlen,
276 const void *data);
277
278/**************************************************************************
279 * Netlink Messages
280 **************************************************************************/
281
282/**
283 * nlmsg_msg_size - length of netlink message not including padding
284 * @payload: length of message payload
285 */
286static inline int nlmsg_msg_size(int payload)
287{
288 return NLMSG_HDRLEN + payload;
289}
290
291/**
292 * nlmsg_total_size - length of netlink message including padding
293 * @payload: length of message payload
294 */
295static inline int nlmsg_total_size(int payload)
296{
297 return NLMSG_ALIGN(nlmsg_msg_size(payload));
298}
299
300/**
301 * nlmsg_padlen - length of padding at the message's tail
302 * @payload: length of message payload
303 */
304static inline int nlmsg_padlen(int payload)
305{
306 return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
307}
308
309/**
310 * nlmsg_data - head of message payload
311 * @nlh: netlink message header
312 */
313static inline void *nlmsg_data(const struct nlmsghdr *nlh)
314{
315 return (unsigned char *) nlh + NLMSG_HDRLEN;
316}
317
318/**
319 * nlmsg_len - length of message payload
320 * @nlh: netlink message header
321 */
322static inline int nlmsg_len(const struct nlmsghdr *nlh)
323{
324 return nlh->nlmsg_len - NLMSG_HDRLEN;
325}
326
327/**
328 * nlmsg_attrdata - head of attributes data
329 * @nlh: netlink message header
330 * @hdrlen: length of family specific header
331 */
332static inline struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh,
333 int hdrlen)
334{
335 unsigned char *data = nlmsg_data(nlh);
336 return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
337}
338
339/**
340 * nlmsg_attrlen - length of attributes data
341 * @nlh: netlink message header
342 * @hdrlen: length of family specific header
343 */
344static inline int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
345{
346 return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
347}
348
349/**
350 * nlmsg_ok - check if the netlink message fits into the remaining bytes
351 * @nlh: netlink message header
352 * @remaining: number of bytes remaining in message stream
353 */
354static inline int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
355{
356 return (remaining >= (int) sizeof(struct nlmsghdr) &&
357 nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
358 nlh->nlmsg_len <= remaining);
359}
360
361/**
362 * nlmsg_next - next netlink message in message stream
363 * @nlh: netlink message header
364 * @remaining: number of bytes remaining in message stream
365 *
366 * Returns the next netlink message in the message stream and
367 * decrements remaining by the size of the current message.
368 */
369static inline struct nlmsghdr *
370nlmsg_next(const struct nlmsghdr *nlh, int *remaining)
371{
372 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
373
374 *remaining -= totlen;
375
376 return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
377}
378
379/**
380 * nlmsg_parse - parse attributes of a netlink message
381 * @nlh: netlink message header
382 * @hdrlen: length of family specific header
383 * @tb: destination array with maxtype+1 elements
384 * @maxtype: maximum attribute type to be expected
385 * @policy: validation policy
386 *
387 * See nla_parse()
388 */
389static inline int nlmsg_parse(const struct nlmsghdr *nlh, int hdrlen,
390 struct nlattr *tb[], int maxtype,
391 const struct nla_policy *policy)
392{
393 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
394 return -EINVAL;
395
396 return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
397 nlmsg_attrlen(nlh, hdrlen), policy);
398}
399
400/**
401 * nlmsg_find_attr - find a specific attribute in a netlink message
402 * @nlh: netlink message header
403 * @hdrlen: length of familiy specific header
404 * @attrtype: type of attribute to look for
405 *
406 * Returns the first attribute which matches the specified type.
407 */
408static inline struct nlattr *nlmsg_find_attr(const struct nlmsghdr *nlh,
409 int hdrlen, int attrtype)
410{
411 return nla_find(nlmsg_attrdata(nlh, hdrlen),
412 nlmsg_attrlen(nlh, hdrlen), attrtype);
413}
414
415/**
416 * nlmsg_validate - validate a netlink message including attributes
417 * @nlh: netlinket message header
418 * @hdrlen: length of familiy specific header
419 * @maxtype: maximum attribute type to be expected
420 * @policy: validation policy
421 */
422static inline int nlmsg_validate(const struct nlmsghdr *nlh,
423 int hdrlen, int maxtype,
424 const struct nla_policy *policy)
425{
426 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
427 return -EINVAL;
428
429 return nla_validate(nlmsg_attrdata(nlh, hdrlen),
430 nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
431}
432
433/**
434 * nlmsg_report - need to report back to application?
435 * @nlh: netlink message header
436 *
437 * Returns 1 if a report back to the application is requested.
438 */
439static inline int nlmsg_report(const struct nlmsghdr *nlh)
440{
441 return !!(nlh->nlmsg_flags & NLM_F_ECHO);
442}
443
444/**
445 * nlmsg_for_each_attr - iterate over a stream of attributes
446 * @pos: loop counter, set to current attribute
447 * @nlh: netlink message header
448 * @hdrlen: length of familiy specific header
449 * @rem: initialized to len, holds bytes currently remaining in stream
450 */
451#define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
452 nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
453 nlmsg_attrlen(nlh, hdrlen), rem)
454
455/**
456 * nlmsg_put - Add a new netlink message to an skb
457 * @skb: socket buffer to store message in
458 * @pid: netlink process id
459 * @seq: sequence number of message
460 * @type: message type
461 * @payload: length of message payload
462 * @flags: message flags
463 *
464 * Returns NULL if the tailroom of the skb is insufficient to store
465 * the message header and payload.
466 */
467static inline struct nlmsghdr *nlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
468 int type, int payload, int flags)
469{
470 if (unlikely(skb_tailroom(skb) < nlmsg_total_size(payload)))
471 return NULL;
472
473 return __nlmsg_put(skb, pid, seq, type, payload, flags);
474}
475
476/**
477 * nlmsg_put_answer - Add a new callback based netlink message to an skb
478 * @skb: socket buffer to store message in
479 * @cb: netlink callback
480 * @type: message type
481 * @payload: length of message payload
482 * @flags: message flags
483 *
484 * Returns NULL if the tailroom of the skb is insufficient to store
485 * the message header and payload.
486 */
487static inline struct nlmsghdr *nlmsg_put_answer(struct sk_buff *skb,
488 struct netlink_callback *cb,
489 int type, int payload,
490 int flags)
491{
492 return nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
493 type, payload, flags);
494}
495
496/**
497 * nlmsg_new - Allocate a new netlink message
498 * @payload: size of the message payload
499 * @flags: the type of memory to allocate.
500 *
501 * Use NLMSG_DEFAULT_SIZE if the size of the payload isn't known
502 * and a good default is needed.
503 */
504static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
505{
506 return alloc_skb(nlmsg_total_size(payload), flags);
507}
508
509/**
510 * nlmsg_end - Finalize a netlink message
511 * @skb: socket buffer the message is stored in
512 * @nlh: netlink message header
513 *
514 * Corrects the netlink message header to include the appeneded
515 * attributes. Only necessary if attributes have been added to
516 * the message.
517 *
518 * Returns the total data length of the skb.
519 */
520static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
521{
522 nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
523
524 return skb->len;
525}
526
527/**
528 * nlmsg_get_pos - return current position in netlink message
529 * @skb: socket buffer the message is stored in
530 *
531 * Returns a pointer to the current tail of the message.
532 */
533static inline void *nlmsg_get_pos(struct sk_buff *skb)
534{
535 return skb_tail_pointer(skb);
536}
537
538/**
539 * nlmsg_trim - Trim message to a mark
540 * @skb: socket buffer the message is stored in
541 * @mark: mark to trim to
542 *
543 * Trims the message to the provided mark.
544 */
545static inline void nlmsg_trim(struct sk_buff *skb, const void *mark)
546{
547 if (mark)
548 skb_trim(skb, (unsigned char *) mark - skb->data);
549}
550
551/**
552 * nlmsg_cancel - Cancel construction of a netlink message
553 * @skb: socket buffer the message is stored in
554 * @nlh: netlink message header
555 *
556 * Removes the complete netlink message including all
557 * attributes from the socket buffer again.
558 */
559static inline void nlmsg_cancel(struct sk_buff *skb, struct nlmsghdr *nlh)
560{
561 nlmsg_trim(skb, nlh);
562}
563
564/**
565 * nlmsg_free - free a netlink message
566 * @skb: socket buffer of netlink message
567 */
568static inline void nlmsg_free(struct sk_buff *skb)
569{
570 kfree_skb(skb);
571}
572
573/**
574 * nlmsg_multicast - multicast a netlink message
575 * @sk: netlink socket to spread messages to
576 * @skb: netlink message as socket buffer
577 * @pid: own netlink pid to avoid sending to yourself
578 * @group: multicast group id
579 * @flags: allocation flags
580 */
581static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,
582 u32 pid, unsigned int group, gfp_t flags)
583{
584 int err;
585
586 NETLINK_CB(skb).dst_group = group;
587
588 err = netlink_broadcast(sk, skb, pid, group, flags);
589 if (err > 0)
590 err = 0;
591
592 return err;
593}
594
595/**
596 * nlmsg_unicast - unicast a netlink message
597 * @sk: netlink socket to spread message to
598 * @skb: netlink message as socket buffer
599 * @pid: netlink pid of the destination socket
600 */
601static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 pid)
602{
603 int err;
604
605 err = netlink_unicast(sk, skb, pid, MSG_DONTWAIT);
606 if (err > 0)
607 err = 0;
608
609 return err;
610}
611
612/**
613 * nlmsg_for_each_msg - iterate over a stream of messages
614 * @pos: loop counter, set to current message
615 * @head: head of message stream
616 * @len: length of message stream
617 * @rem: initialized to len, holds bytes currently remaining in stream
618 */
619#define nlmsg_for_each_msg(pos, head, len, rem) \
620 for (pos = head, rem = len; \
621 nlmsg_ok(pos, rem); \
622 pos = nlmsg_next(pos, &(rem)))
623
624/**
625 * nl_dump_check_consistent - check if sequence is consistent and advertise if not
626 * @cb: netlink callback structure that stores the sequence number
627 * @nlh: netlink message header to write the flag to
628 *
629 * This function checks if the sequence (generation) number changed during dump
630 * and if it did, advertises it in the netlink message header.
631 *
632 * The correct way to use it is to set cb->seq to the generation counter when
633 * all locks for dumping have been acquired, and then call this function for
634 * each message that is generated.
635 *
636 * Note that due to initialisation concerns, 0 is an invalid sequence number
637 * and must not be used by code that uses this functionality.
638 */
639static inline void
640nl_dump_check_consistent(struct netlink_callback *cb,
641 struct nlmsghdr *nlh)
642{
643 if (cb->prev_seq && cb->seq != cb->prev_seq)
644 nlh->nlmsg_flags |= NLM_F_DUMP_INTR;
645 cb->prev_seq = cb->seq;
646}
647
648/**************************************************************************
649 * Netlink Attributes
650 **************************************************************************/
651
652/**
653 * nla_attr_size - length of attribute not including padding
654 * @payload: length of payload
655 */
656static inline int nla_attr_size(int payload)
657{
658 return NLA_HDRLEN + payload;
659}
660
661/**
662 * nla_total_size - total length of attribute including padding
663 * @payload: length of payload
664 */
665static inline int nla_total_size(int payload)
666{
667 return NLA_ALIGN(nla_attr_size(payload));
668}
669
670/**
671 * nla_padlen - length of padding at the tail of attribute
672 * @payload: length of payload
673 */
674static inline int nla_padlen(int payload)
675{
676 return nla_total_size(payload) - nla_attr_size(payload);
677}
678
679/**
680 * nla_type - attribute type
681 * @nla: netlink attribute
682 */
683static inline int nla_type(const struct nlattr *nla)
684{
685 return nla->nla_type & NLA_TYPE_MASK;
686}
687
688/**
689 * nla_data - head of payload
690 * @nla: netlink attribute
691 */
692static inline void *nla_data(const struct nlattr *nla)
693{
694 return (char *) nla + NLA_HDRLEN;
695}
696
697/**
698 * nla_len - length of payload
699 * @nla: netlink attribute
700 */
701static inline int nla_len(const struct nlattr *nla)
702{
703 return nla->nla_len - NLA_HDRLEN;
704}
705
706/**
707 * nla_ok - check if the netlink attribute fits into the remaining bytes
708 * @nla: netlink attribute
709 * @remaining: number of bytes remaining in attribute stream
710 */
711static inline int nla_ok(const struct nlattr *nla, int remaining)
712{
713 return remaining >= (int) sizeof(*nla) &&
714 nla->nla_len >= sizeof(*nla) &&
715 nla->nla_len <= remaining;
716}
717
718/**
719 * nla_next - next netlink attribute in attribute stream
720 * @nla: netlink attribute
721 * @remaining: number of bytes remaining in attribute stream
722 *
723 * Returns the next netlink attribute in the attribute stream and
724 * decrements remaining by the size of the current attribute.
725 */
726static inline struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
727{
728 int totlen = NLA_ALIGN(nla->nla_len);
729
730 *remaining -= totlen;
731 return (struct nlattr *) ((char *) nla + totlen);
732}
733
734/**
735 * nla_find_nested - find attribute in a set of nested attributes
736 * @nla: attribute containing the nested attributes
737 * @attrtype: type of attribute to look for
738 *
739 * Returns the first attribute which matches the specified type.
740 */
741static inline struct nlattr *
742nla_find_nested(const struct nlattr *nla, int attrtype)
743{
744 return nla_find(nla_data(nla), nla_len(nla), attrtype);
745}
746
747/**
748 * nla_parse_nested - parse nested attributes
749 * @tb: destination array with maxtype+1 elements
750 * @maxtype: maximum attribute type to be expected
751 * @nla: attribute containing the nested attributes
752 * @policy: validation policy
753 *
754 * See nla_parse()
755 */
756static inline int nla_parse_nested(struct nlattr *tb[], int maxtype,
757 const struct nlattr *nla,
758 const struct nla_policy *policy)
759{
760 return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
761}
762
763/**
764 * nla_put_u8 - Add a u8 netlink attribute to a socket buffer
765 * @skb: socket buffer to add attribute to
766 * @attrtype: attribute type
767 * @value: numeric value
768 */
769static inline int nla_put_u8(struct sk_buff *skb, int attrtype, u8 value)
770{
771 return nla_put(skb, attrtype, sizeof(u8), &value);
772}
773
774/**
775 * nla_put_u16 - Add a u16 netlink attribute to a socket buffer
776 * @skb: socket buffer to add attribute to
777 * @attrtype: attribute type
778 * @value: numeric value
779 */
780static inline int nla_put_u16(struct sk_buff *skb, int attrtype, u16 value)
781{
782 return nla_put(skb, attrtype, sizeof(u16), &value);
783}
784
785/**
786 * nla_put_u32 - Add a u32 netlink attribute to a socket buffer
787 * @skb: socket buffer to add attribute to
788 * @attrtype: attribute type
789 * @value: numeric value
790 */
791static inline int nla_put_u32(struct sk_buff *skb, int attrtype, u32 value)
792{
793#if defined(CONFIG_AIC8800)
794 u32 tmp = value;
795
796 return nla_put(skb, attrtype, sizeof(u32), &tmp);
797#else
798 return nla_put(skb, attrtype, sizeof(u32), &value);
799#endif
800}
801
802/**
803 * nla_put_64 - Add a u64 netlink attribute to a socket buffer
804 * @skb: socket buffer to add attribute to
805 * @attrtype: attribute type
806 * @value: numeric value
807 */
808static inline int nla_put_u64(struct sk_buff *skb, int attrtype, u64 value)
809{
810 return nla_put(skb, attrtype, sizeof(u64), &value);
811}
812
813/**
814 * nla_put_string - Add a string netlink attribute to a socket buffer
815 * @skb: socket buffer to add attribute to
816 * @attrtype: attribute type
817 * @str: NUL terminated string
818 */
819static inline int nla_put_string(struct sk_buff *skb, int attrtype,
820 const char *str)
821{
822 return nla_put(skb, attrtype, strlen(str) + 1, str);
823}
824
825/**
826 * nla_put_flag - Add a flag netlink attribute to a socket buffer
827 * @skb: socket buffer to add attribute to
828 * @attrtype: attribute type
829 */
830static inline int nla_put_flag(struct sk_buff *skb, int attrtype)
831{
832 return nla_put(skb, attrtype, 0, NULL);
833}
834
835/**
836 * nla_put_msecs - Add a msecs netlink attribute to a socket buffer
837 * @skb: socket buffer to add attribute to
838 * @attrtype: attribute type
839 * @jiffies: number of msecs in jiffies
840 */
841static inline int nla_put_msecs(struct sk_buff *skb, int attrtype,
842 unsigned long jiffies)
843{
844 u64 tmp = jiffies_to_msecs(jiffies);
845 return nla_put(skb, attrtype, sizeof(u64), &tmp);
846}
847
848#define NLA_PUT(skb, attrtype, attrlen, data) \
849 do { \
850 if (unlikely(nla_put(skb, attrtype, attrlen, data) < 0)) \
851 goto nla_put_failure; \
852 } while(0)
853
854#define NLA_PUT_TYPE(skb, type, attrtype, value) \
855 do { \
856 type __tmp = value; \
857 NLA_PUT(skb, attrtype, sizeof(type), &__tmp); \
858 } while(0)
859
860#define NLA_PUT_U8(skb, attrtype, value) \
861 NLA_PUT_TYPE(skb, u8, attrtype, value)
862
863#define NLA_PUT_U16(skb, attrtype, value) \
864 NLA_PUT_TYPE(skb, u16, attrtype, value)
865
866#define NLA_PUT_LE16(skb, attrtype, value) \
867 NLA_PUT_TYPE(skb, __le16, attrtype, value)
868
869#define NLA_PUT_BE16(skb, attrtype, value) \
870 NLA_PUT_TYPE(skb, __be16, attrtype, value)
871
872#define NLA_PUT_NET16(skb, attrtype, value) \
873 NLA_PUT_BE16(skb, attrtype | NLA_F_NET_BYTEORDER, value)
874
875#define NLA_PUT_U32(skb, attrtype, value) \
876 NLA_PUT_TYPE(skb, u32, attrtype, value)
877
878#define NLA_PUT_BE32(skb, attrtype, value) \
879 NLA_PUT_TYPE(skb, __be32, attrtype, value)
880
881#define NLA_PUT_NET32(skb, attrtype, value) \
882 NLA_PUT_BE32(skb, attrtype | NLA_F_NET_BYTEORDER, value)
883
884#define NLA_PUT_U64(skb, attrtype, value) \
885 NLA_PUT_TYPE(skb, u64, attrtype, value)
886
887#define NLA_PUT_BE64(skb, attrtype, value) \
888 NLA_PUT_TYPE(skb, __be64, attrtype, value)
889
890#define NLA_PUT_NET64(skb, attrtype, value) \
891 NLA_PUT_BE64(skb, attrtype | NLA_F_NET_BYTEORDER, value)
892
893#define NLA_PUT_STRING(skb, attrtype, value) \
894 NLA_PUT(skb, attrtype, strlen(value) + 1, value)
895
896#define NLA_PUT_FLAG(skb, attrtype) \
897 NLA_PUT(skb, attrtype, 0, NULL)
898
899#define NLA_PUT_MSECS(skb, attrtype, jiffies) \
900 NLA_PUT_U64(skb, attrtype, jiffies_to_msecs(jiffies))
901
902/**
903 * nla_get_u32 - return payload of u32 attribute
904 * @nla: u32 netlink attribute
905 */
906static inline u32 nla_get_u32(const struct nlattr *nla)
907{
908 return *(u32 *) nla_data(nla);
909}
910
911/**
912 * nla_get_be32 - return payload of __be32 attribute
913 * @nla: __be32 netlink attribute
914 */
915static inline __be32 nla_get_be32(const struct nlattr *nla)
916{
917 return *(__be32 *) nla_data(nla);
918}
919
920/**
921 * nla_get_u16 - return payload of u16 attribute
922 * @nla: u16 netlink attribute
923 */
924static inline u16 nla_get_u16(const struct nlattr *nla)
925{
926 return *(u16 *) nla_data(nla);
927}
928
929/**
930 * nla_get_be16 - return payload of __be16 attribute
931 * @nla: __be16 netlink attribute
932 */
933static inline __be16 nla_get_be16(const struct nlattr *nla)
934{
935 return *(__be16 *) nla_data(nla);
936}
937
938/**
939 * nla_get_le16 - return payload of __le16 attribute
940 * @nla: __le16 netlink attribute
941 */
942static inline __le16 nla_get_le16(const struct nlattr *nla)
943{
944 return *(__le16 *) nla_data(nla);
945}
946
947/**
948 * nla_get_u8 - return payload of u8 attribute
949 * @nla: u8 netlink attribute
950 */
951static inline u8 nla_get_u8(const struct nlattr *nla)
952{
953 return *(u8 *) nla_data(nla);
954}
955
956/**
957 * nla_get_u64 - return payload of u64 attribute
958 * @nla: u64 netlink attribute
959 */
960static inline u64 nla_get_u64(const struct nlattr *nla)
961{
962 u64 tmp;
963
964 nla_memcpy(&tmp, nla, sizeof(tmp));
965
966 return tmp;
967}
968
969/**
970 * nla_get_be64 - return payload of __be64 attribute
971 * @nla: __be64 netlink attribute
972 */
973static inline __be64 nla_get_be64(const struct nlattr *nla)
974{
975 __be64 tmp;
976
977 nla_memcpy(&tmp, nla, sizeof(tmp));
978
979 return tmp;
980}
981
982/**
983 * nla_get_flag - return payload of flag attribute
984 * @nla: flag netlink attribute
985 */
986static inline int nla_get_flag(const struct nlattr *nla)
987{
988 return !!nla;
989}
990
991/**
992 * nla_get_msecs - return payload of msecs attribute
993 * @nla: msecs netlink attribute
994 *
995 * Returns the number of milliseconds in jiffies.
996 */
997static inline unsigned long nla_get_msecs(const struct nlattr *nla)
998{
999 u64 msecs = nla_get_u64(nla);
1000
1001 return msecs_to_jiffies((unsigned long) msecs);
1002}
1003
1004/**
1005 * nla_nest_start - Start a new level of nested attributes
1006 * @skb: socket buffer to add attributes to
1007 * @attrtype: attribute type of container
1008 *
1009 * Returns the container attribute
1010 */
1011static inline struct nlattr *nla_nest_start(struct sk_buff *skb, int attrtype)
1012{
1013 struct nlattr *start = (struct nlattr *)skb_tail_pointer(skb);
1014
1015 if (nla_put(skb, attrtype, 0, NULL) < 0)
1016 return NULL;
1017
1018 return start;
1019}
1020
1021/**
1022 * nla_nest_end - Finalize nesting of attributes
1023 * @skb: socket buffer the attributes are stored in
1024 * @start: container attribute
1025 *
1026 * Corrects the container attribute header to include the all
1027 * appeneded attributes.
1028 *
1029 * Returns the total data length of the skb.
1030 */
1031static inline int nla_nest_end(struct sk_buff *skb, struct nlattr *start)
1032{
1033 start->nla_len = skb_tail_pointer(skb) - (unsigned char *)start;
1034 return skb->len;
1035}
1036
1037/**
1038 * nla_nest_cancel - Cancel nesting of attributes
1039 * @skb: socket buffer the message is stored in
1040 * @start: container attribute
1041 *
1042 * Removes the container attribute and including all nested
1043 * attributes. Returns -EMSGSIZE
1044 */
1045static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start)
1046{
1047 nlmsg_trim(skb, start);
1048}
1049
1050/**
1051 * nla_validate_nested - Validate a stream of nested attributes
1052 * @start: container attribute
1053 * @maxtype: maximum attribute type to be expected
1054 * @policy: validation policy
1055 *
1056 * Validates all attributes in the nested attribute stream against the
1057 * specified policy. Attributes with a type exceeding maxtype will be
1058 * ignored. See documenation of struct nla_policy for more details.
1059 *
1060 * Returns 0 on success or a negative error code.
1061 */
1062static inline int nla_validate_nested(const struct nlattr *start, int maxtype,
1063 const struct nla_policy *policy)
1064{
1065 return nla_validate(nla_data(start), nla_len(start), maxtype, policy);
1066}
1067
1068/**
1069 * nla_for_each_attr - iterate over a stream of attributes
1070 * @pos: loop counter, set to current attribute
1071 * @head: head of attribute stream
1072 * @len: length of attribute stream
1073 * @rem: initialized to len, holds bytes currently remaining in stream
1074 */
1075#define nla_for_each_attr(pos, head, len, rem) \
1076 for (pos = head, rem = len; \
1077 nla_ok(pos, rem); \
1078 pos = nla_next(pos, &(rem)))
1079
1080/**
1081 * nla_for_each_nested - iterate over nested attributes
1082 * @pos: loop counter, set to current attribute
1083 * @nla: attribute containing the nested attributes
1084 * @rem: initialized to len, holds bytes currently remaining in stream
1085 */
1086#define nla_for_each_nested(pos, nla, rem) \
1087 nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem)
1088
1089#endif