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