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