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