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