blob: ad6f5a1626add8550e79512192c1ffd6fa4ea57f [file] [log] [blame]
Thomas Graf44d36242007-09-15 01:28:01 +02001/*
2 * lib/msg.c Netlink Messages Interface
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
7 * of the License.
8 *
Thomas Graf8a3efff2008-05-14 17:49:44 +02009 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
Thomas Graf44d36242007-09-15 01:28:01 +020010 */
11
12/**
13 * @ingroup nl
14 * @defgroup msg Messages
15 * Netlink Message Construction/Parsing Interface
16 *
17 * The following information is partly extracted from RFC3549
18 * (ftp://ftp.rfc-editor.org/in-notes/rfc3549.txt)
19 *
20 * @par Message Format
21 * Netlink messages consist of a byte stream with one or multiple
22 * Netlink headers and an associated payload. If the payload is too big
23 * to fit into a single message it, can be split over multiple Netlink
24 * messages, collectively called a multipart message. For multipart
25 * messages, the first and all following headers have the \c NLM_F_MULTI
26 * Netlink header flag set, except for the last header which has the
27 * Netlink header type \c NLMSG_DONE.
28 *
29 * @par
30 * The Netlink message header (\link nlmsghdr struct nlmsghdr\endlink) is shown below.
31 * @code
32 * 0 1 2 3
33 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * | Length |
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 * | Type | Flags |
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * | Sequence Number |
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | Process ID (PID) |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * @endcode
44 *
45 * @par
46 * The netlink message header and payload must be aligned properly:
47 * @code
48 * <------- NLMSG_ALIGN(hlen) ------> <---- NLMSG_ALIGN(len) --->
49 * +----------------------------+- - -+- - - - - - - - - - -+- - -+
50 * | Header | Pad | Payload | Pad |
51 * | struct nlmsghdr | | | |
52 * +----------------------------+- - -+- - - - - - - - - - -+- - -+
53 * @endcode
54 * @par
55 * Message Format:
56 * @code
57 * <--- nlmsg_total_size(payload) --->
58 * <-- nlmsg_msg_size(payload) ->
59 * +----------+- - -+-------------+- - -+-------- - -
60 * | nlmsghdr | Pad | Payload | Pad | nlmsghdr
61 * +----------+- - -+-------------+- - -+-------- - -
62 * nlmsg_data(nlh)---^ ^
63 * nlmsg_next(nlh)-----------------------+
64 * @endcode
65 * @par
66 * The payload may consist of arbitary data but may have strict
67 * alignment and formatting rules depening on the specific netlink
68 * families.
69 * @par
70 * @code
71 * <---------------------- nlmsg_len(nlh) --------------------->
72 * <------ hdrlen ------> <- nlmsg_attrlen(nlh, hdrlen) ->
73 * +----------------------+- - -+--------------------------------+
74 * | Family Header | Pad | Attributes |
75 * +----------------------+- - -+--------------------------------+
76 * nlmsg_attrdata(nlh, hdrlen)---^
77 * @endcode
78 * @par The ACK Netlink Message
79 * This message is actually used to denote both an ACK and a NACK.
80 * Typically, the direction is from FEC to CPC (in response to an ACK
81 * request message). However, the CPC should be able to send ACKs back
82 * to FEC when requested.
83 * @code
84 * 0 1 2 3
85 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
86 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
87 * | Netlink message header |
88 * | type = NLMSG_ERROR |
89 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 * | Error code |
91 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92 * | OLD Netlink message header |
93 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 * @endcode
95 *
Thomas Graf75a26f32008-01-14 16:46:17 +010096 * @par Example
Thomas Graf44d36242007-09-15 01:28:01 +020097 * @code
Thomas Graf75a26f32008-01-14 16:46:17 +010098 * // Various methods exist to create/allocate a new netlink
99 * // message.
100 * //
101 * // nlmsg_alloc() will allocate an empty netlink message with
102 * // a maximum payload size which defaults to the page size of
103 * // the system. This default size can be modified using the
104 * // function nlmsg_set_default_size().
Thomas Graf44d36242007-09-15 01:28:01 +0200105 * struct nl_msg *msg = nlmsg_alloc();
106 *
107 * // Very often, the message type and message flags are known
108 * // at allocation time while the other fields are auto generated:
109 * struct nl_msg *msg = nlmsg_alloc_simple(MY_TYPE, MY_FLAGS);
110 *
111 * // Alternatively an existing netlink message header can be used
Thomas Graf75a26f32008-01-14 16:46:17 +0100112 * // to inherit the header values:
Thomas Graf44d36242007-09-15 01:28:01 +0200113 * struct nlmsghdr hdr = {
114 * .nlmsg_type = MY_TYPE,
115 * .nlmsg_flags = MY_FLAGS,
116 * };
117 * struct nl_msg *msg = nlmsg_inherit(&hdr);
118 *
119 * // Last but not least, netlink messages received from netlink sockets
Thomas Graf75a26f32008-01-14 16:46:17 +0100120 * // can be converted into nl_msg objects using nlmsg_convert(). This
121 * // will create a message with a maximum payload size which equals the
122 * // length of the existing netlink message, therefore no more data can
123 * // be appened without calling nlmsg_expand() first.
Thomas Graf44d36242007-09-15 01:28:01 +0200124 * struct nl_msg *msg = nlmsg_convert(nlh_from_nl_sock);
125 *
Thomas Graf44d36242007-09-15 01:28:01 +0200126 * // Payload may be added to the message via nlmsg_append(). The fourth
127 * // parameter specifies the number of alignment bytes the data should
128 * // be padding with at the end. Common values are 0 to disable it or
129 * // NLMSG_ALIGNTO to ensure proper netlink message padding.
130 * nlmsg_append(msg, &mydata, sizeof(mydata), 0);
131 *
132 * // Sometimes it may be necessary to reserve room for data but defer
133 * // the actual copying to a later point, nlmsg_reserve() can be used
134 * // for this purpose:
135 * void *data = nlmsg_reserve(msg, sizeof(mydata), NLMSG_ALIGNTO);
Thomas Graf44d36242007-09-15 01:28:01 +0200136 *
Thomas Graf75a26f32008-01-14 16:46:17 +0100137 * // Attributes may be added using the attributes interface.
138 *
Thomas Graf44d36242007-09-15 01:28:01 +0200139 * // After successful use of the message, the memory must be freed
140 * // using nlmsg_free()
141 * nlmsg_free(msg);
142 * @endcode
143 *
144 * @par 4) Parsing messages
145 * @code
146 * int n;
147 * unsigned char *buf;
148 * struct nlmsghdr *hdr;
149 *
150 * n = nl_recv(handle, NULL, &buf);
151 *
152 * hdr = (struct nlmsghdr *) buf;
153 * while (nlmsg_ok(hdr, n)) {
154 * // Process message here...
155 * hdr = nlmsg_next(hdr, &n);
156 * }
157 * @endcode
158 * @{
159 */
160
161#include <netlink-local.h>
162#include <netlink/netlink.h>
163#include <netlink/utils.h>
164#include <netlink/cache.h>
165#include <netlink/attr.h>
166#include <linux/socket.h>
167
Thomas Graf6de17f32008-01-14 16:17:56 +0100168static size_t default_msg_size;
169
170static void __init init_msg_size(void)
171{
172 default_msg_size = getpagesize();
173}
174
Thomas Graf44d36242007-09-15 01:28:01 +0200175/**
176 * @name Size Calculations
177 * @{
178 */
179
180/**
181 * length of netlink message not including padding
182 * @arg payload length of message payload
183 */
184int nlmsg_msg_size(int payload)
185{
186 return NLMSG_HDRLEN + payload;
187}
188
189/**
190 * length of netlink message including padding
191 * @arg payload length of message payload
192 */
193int nlmsg_total_size(int payload)
194{
195 return NLMSG_ALIGN(nlmsg_msg_size(payload));
196}
197
198/**
199 * length of padding at the message's tail
200 * @arg payload length of message payload
201 */
202int nlmsg_padlen(int payload)
203{
204 return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
205}
206
207/** @} */
208
209/**
210 * @name Payload Access
211 * @{
212 */
213
214/**
215 * head of message payload
216 * @arg nlh netlink messsage header
217 */
218void *nlmsg_data(const struct nlmsghdr *nlh)
219{
220 return (unsigned char *) nlh + NLMSG_HDRLEN;
221}
222
223void *nlmsg_tail(const struct nlmsghdr *nlh)
224{
225 return (unsigned char *) nlh + NLMSG_ALIGN(nlh->nlmsg_len);
226}
227
228/**
229 * length of message payload
230 * @arg nlh netlink message header
231 */
232int nlmsg_len(const struct nlmsghdr *nlh)
233{
234 return nlh->nlmsg_len - NLMSG_HDRLEN;
235}
236
237/** @} */
238
239/**
240 * @name Attribute Access
241 * @{
242 */
243
244/**
245 * head of attributes data
246 * @arg nlh netlink message header
247 * @arg hdrlen length of family specific header
248 */
249struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, int hdrlen)
250{
251 unsigned char *data = nlmsg_data(nlh);
252 return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
253}
254
255/**
256 * length of attributes data
257 * @arg nlh netlink message header
258 * @arg hdrlen length of family specific header
259 */
260int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
261{
262 return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
263}
264
265/** @} */
266
267/**
268 * @name Message Parsing
269 * @{
270 */
271
Thomas Grafc8a0a5c2008-01-10 12:35:38 +0100272int nlmsg_valid_hdr(const struct nlmsghdr *nlh, int hdrlen)
273{
274 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
275 return 0;
276
277 return 1;
278}
279
Thomas Graf44d36242007-09-15 01:28:01 +0200280/**
281 * check if the netlink message fits into the remaining bytes
282 * @arg nlh netlink message header
283 * @arg remaining number of bytes remaining in message stream
284 */
285int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
286{
287 return (remaining >= sizeof(struct nlmsghdr) &&
288 nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
289 nlh->nlmsg_len <= remaining);
290}
291
292/**
293 * next netlink message in message stream
294 * @arg nlh netlink message header
295 * @arg remaining number of bytes remaining in message stream
296 *
297 * @returns the next netlink message in the message stream and
298 * decrements remaining by the size of the current message.
299 */
300struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
301{
302 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
303
304 *remaining -= totlen;
305
306 return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
307}
308
309/**
310 * parse attributes of a netlink message
311 * @arg nlh netlink message header
312 * @arg hdrlen length of family specific header
313 * @arg tb destination array with maxtype+1 elements
314 * @arg maxtype maximum attribute type to be expected
315 * @arg policy validation policy
316 *
317 * See nla_parse()
318 */
319int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[],
320 int maxtype, struct nla_policy *policy)
321{
Thomas Grafc8a0a5c2008-01-10 12:35:38 +0100322 if (!nlmsg_valid_hdr(nlh, hdrlen))
Thomas Graf8a3efff2008-05-14 17:49:44 +0200323 return -NLE_MSG_TOOSHORT;
Thomas Graf44d36242007-09-15 01:28:01 +0200324
325 return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
326 nlmsg_attrlen(nlh, hdrlen), policy);
327}
328
329/**
330 * nlmsg_find_attr - find a specific attribute in a netlink message
331 * @arg nlh netlink message header
332 * @arg hdrlen length of familiy specific header
333 * @arg attrtype type of attribute to look for
334 *
335 * Returns the first attribute which matches the specified type.
336 */
337struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh, int hdrlen, int attrtype)
338{
339 return nla_find(nlmsg_attrdata(nlh, hdrlen),
340 nlmsg_attrlen(nlh, hdrlen), attrtype);
341}
342
343/**
344 * nlmsg_validate - validate a netlink message including attributes
345 * @arg nlh netlinket message header
346 * @arg hdrlen length of familiy specific header
347 * @arg maxtype maximum attribute type to be expected
348 * @arg policy validation policy
349 */
350int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
351 struct nla_policy *policy)
352{
Thomas Grafc8a0a5c2008-01-10 12:35:38 +0100353 if (!nlmsg_valid_hdr(nlh, hdrlen))
Thomas Graf8a3efff2008-05-14 17:49:44 +0200354 return -NLE_MSG_TOOSHORT;
Thomas Graf44d36242007-09-15 01:28:01 +0200355
356 return nla_validate(nlmsg_attrdata(nlh, hdrlen),
357 nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
358}
359
360/** @} */
361
362/**
363 * @name Message Building/Access
364 * @{
365 */
366
367static struct nl_msg *__nlmsg_alloc(size_t len)
368{
369 struct nl_msg *nm;
370
371 nm = calloc(1, sizeof(*nm));
372 if (!nm)
373 goto errout;
374
Thomas Grafb7c5bf92008-05-07 13:18:30 +0200375 nm->nm_nlh = malloc(len);
Thomas Graf44d36242007-09-15 01:28:01 +0200376 if (!nm->nm_nlh)
377 goto errout;
378
Thomas Grafb7c5bf92008-05-07 13:18:30 +0200379 memset(nm->nm_nlh, 0, sizeof(struct nlmsghdr));
380
Thomas Graf44d36242007-09-15 01:28:01 +0200381 nm->nm_protocol = -1;
Thomas Graf6de17f32008-01-14 16:17:56 +0100382 nm->nm_size = len;
383 nm->nm_nlh->nlmsg_len = nlmsg_total_size(0);
Thomas Graf44d36242007-09-15 01:28:01 +0200384
Thomas Graf6de17f32008-01-14 16:17:56 +0100385 NL_DBG(2, "msg %p: Allocated new message, maxlen=%zu\n", nm, len);
Thomas Graf44d36242007-09-15 01:28:01 +0200386
387 return nm;
388errout:
389 free(nm);
Thomas Graf44d36242007-09-15 01:28:01 +0200390 return NULL;
391}
392
393/**
Thomas Graf6de17f32008-01-14 16:17:56 +0100394 * Allocate a new netlink message with the default maximum payload size.
Thomas Graf44d36242007-09-15 01:28:01 +0200395 *
Thomas Graf6de17f32008-01-14 16:17:56 +0100396 * Allocates a new netlink message without any further payload. The
397 * maximum payload size defaults to PAGESIZE or as otherwise specified
398 * with nlmsg_set_default_size().
Thomas Graf44d36242007-09-15 01:28:01 +0200399 *
400 * @return Newly allocated netlink message or NULL.
401 */
402struct nl_msg *nlmsg_alloc(void)
403{
Thomas Graf6de17f32008-01-14 16:17:56 +0100404 return __nlmsg_alloc(default_msg_size);
405}
406
407/**
408 * Allocate a new netlink message with maximum payload size specified.
409 */
410struct nl_msg *nlmsg_alloc_size(size_t max)
411{
412 return __nlmsg_alloc(max);
Thomas Graf44d36242007-09-15 01:28:01 +0200413}
414
415/**
416 * Allocate a new netlink message and inherit netlink message header
417 * @arg hdr Netlink message header template
418 *
Thomas Graf6de17f32008-01-14 16:17:56 +0100419 * Allocates a new netlink message and inherits the original message
420 * header. If \a hdr is not NULL it will be used as a template for
421 * the netlink message header, otherwise the header is left blank.
Thomas Graf44d36242007-09-15 01:28:01 +0200422 *
423 * @return Newly allocated netlink message or NULL
424 */
425struct nl_msg *nlmsg_inherit(struct nlmsghdr *hdr)
426{
427 struct nl_msg *nm;
428
429 nm = nlmsg_alloc();
430 if (nm && hdr) {
431 struct nlmsghdr *new = nm->nm_nlh;
432
433 new->nlmsg_type = hdr->nlmsg_type;
434 new->nlmsg_flags = hdr->nlmsg_flags;
435 new->nlmsg_seq = hdr->nlmsg_seq;
436 new->nlmsg_pid = hdr->nlmsg_pid;
437 }
438
439 return nm;
440}
441
442/**
443 * Allocate a new netlink message
444 * @arg nlmsgtype Netlink message type
445 * @arg flags Message flags.
446 *
447 * @return Newly allocated netlink message or NULL.
448 */
449struct nl_msg *nlmsg_alloc_simple(int nlmsgtype, int flags)
450{
451 struct nl_msg *msg;
452 struct nlmsghdr nlh = {
453 .nlmsg_type = nlmsgtype,
454 .nlmsg_flags = flags,
455 };
456
457 msg = nlmsg_inherit(&nlh);
458 if (msg)
459 NL_DBG(2, "msg %p: Allocated new simple message\n", msg);
460
461 return msg;
462}
463
464/**
Thomas Graf6de17f32008-01-14 16:17:56 +0100465 * Set the default maximum message payload size for allocated messages
466 * @arg max Size of payload in bytes.
467 */
468void nlmsg_set_default_size(size_t max)
469{
470 if (max < nlmsg_total_size(0))
471 max = nlmsg_total_size(0);
472
473 default_msg_size = max;
474}
475
476/**
Thomas Graf44d36242007-09-15 01:28:01 +0200477 * Convert a netlink message received from a netlink socket to a nl_msg
478 * @arg hdr Netlink message received from netlink socket.
479 *
480 * Allocates a new netlink message and copies all of the data pointed to
481 * by \a hdr into the new message object.
482 *
483 * @return Newly allocated netlink message or NULL.
484 */
485struct nl_msg *nlmsg_convert(struct nlmsghdr *hdr)
486{
487 struct nl_msg *nm;
488
489 nm = __nlmsg_alloc(NLMSG_ALIGN(hdr->nlmsg_len));
490 if (!nm)
491 goto errout;
492
493 memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len);
494
495 return nm;
496errout:
497 nlmsg_free(nm);
498 return NULL;
499}
500
501/**
502 * Reserve room for additional data in a netlink message
503 * @arg n netlink message
504 * @arg len length of additional data to reserve room for
505 * @arg pad number of bytes to align data to
506 *
507 * Reserves room for additional data at the tail of the an
508 * existing netlink message. Eventual padding required will
509 * be zeroed out.
510 *
Thomas Graf44d36242007-09-15 01:28:01 +0200511 * @return Pointer to start of additional data tailroom or NULL.
512 */
513void *nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
514{
Thomas Graf6de17f32008-01-14 16:17:56 +0100515 void *buf = n->nm_nlh;
516 size_t nlmsg_len = n->nm_nlh->nlmsg_len;
Thomas Graf44d36242007-09-15 01:28:01 +0200517 size_t tlen;
518
519 tlen = pad ? ((len + (pad - 1)) & ~(pad - 1)) : len;
520
Thomas Graf8a3efff2008-05-14 17:49:44 +0200521 if ((tlen + nlmsg_len) > n->nm_size)
Thomas Graf44d36242007-09-15 01:28:01 +0200522 return NULL;
Thomas Graf44d36242007-09-15 01:28:01 +0200523
Thomas Graf6de17f32008-01-14 16:17:56 +0100524 buf += nlmsg_len;
Thomas Graf44d36242007-09-15 01:28:01 +0200525 n->nm_nlh->nlmsg_len += tlen;
526
527 if (tlen > len)
Thomas Graf6de17f32008-01-14 16:17:56 +0100528 memset(buf + len, 0, tlen - len);
Thomas Graf44d36242007-09-15 01:28:01 +0200529
Patrick McHardy936c9842007-12-13 12:09:45 +0100530 NL_DBG(2, "msg %p: Reserved %zu bytes, pad=%d, nlmsg_len=%d\n",
Thomas Graf44d36242007-09-15 01:28:01 +0200531 n, len, pad, n->nm_nlh->nlmsg_len);
532
Thomas Graf6de17f32008-01-14 16:17:56 +0100533 return buf;
Thomas Graf44d36242007-09-15 01:28:01 +0200534}
535
536/**
537 * Append data to tail of a netlink message
538 * @arg n netlink message
539 * @arg data data to add
540 * @arg len length of data
541 * @arg pad Number of bytes to align data to.
542 *
543 * Extends the netlink message as needed and appends the data of given
544 * length to the message.
545 *
Thomas Graf44d36242007-09-15 01:28:01 +0200546 * @return 0 on success or a negative error code
547 */
548int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
549{
550 void *tmp;
551
552 tmp = nlmsg_reserve(n, len, pad);
553 if (tmp == NULL)
Thomas Graf8a3efff2008-05-14 17:49:44 +0200554 return -NLE_NOMEM;
Thomas Graf44d36242007-09-15 01:28:01 +0200555
556 memcpy(tmp, data, len);
Patrick McHardy936c9842007-12-13 12:09:45 +0100557 NL_DBG(2, "msg %p: Appended %zu bytes with padding %d\n", n, len, pad);
Thomas Graf44d36242007-09-15 01:28:01 +0200558
559 return 0;
560}
561
562/**
Thomas Graf75a26f32008-01-14 16:46:17 +0100563 * Expand maximum payload size of a netlink message
564 * @arg n Netlink message.
565 * @arg newlen New maximum payload size.
566 *
567 * Reallocates the payload section of a netlink message and increases
568 * the maximum payload size of the message.
569 *
570 * @note Any pointers pointing to old payload block will be stale and
571 * need to be refetched. Therfore, do not expand while constructing
572 * nested attributes or while reserved data blocks are held.
573 *
574 * @return 0 on success or a negative error code.
575 */
576int nlmsg_expand(struct nl_msg *n, size_t newlen)
577{
578 void *tmp;
579
580 if (newlen <= n->nm_size)
Thomas Graf8a3efff2008-05-14 17:49:44 +0200581 return -NLE_INVAL;
Thomas Graf75a26f32008-01-14 16:46:17 +0100582
583 tmp = realloc(n->nm_nlh, newlen);
584 if (tmp == NULL)
Thomas Graf8a3efff2008-05-14 17:49:44 +0200585 return -NLE_NOMEM;
Thomas Graf75a26f32008-01-14 16:46:17 +0100586
587 n->nm_nlh = tmp;
588 n->nm_size = newlen;
589
590 return 0;
591}
592
593/**
Thomas Graf44d36242007-09-15 01:28:01 +0200594 * Add a netlink message header to a netlink message
595 * @arg n netlink message
596 * @arg pid netlink process id or NL_AUTO_PID
597 * @arg seq sequence number of message or NL_AUTO_SEQ
598 * @arg type message type
599 * @arg payload length of message payload
600 * @arg flags message flags
601 *
602 * Adds or overwrites the netlink message header in an existing message
603 * object. If \a payload is greater-than zero additional room will be
604 * reserved, f.e. for family specific headers. It can be accesed via
605 * nlmsg_data().
606 *
607 * @return A pointer to the netlink message header or NULL.
608 */
609struct nlmsghdr *nlmsg_put(struct nl_msg *n, uint32_t pid, uint32_t seq,
610 int type, int payload, int flags)
611{
612 struct nlmsghdr *nlh;
613
614 if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN)
615 BUG();
616
617 nlh = (struct nlmsghdr *) n->nm_nlh;
618 nlh->nlmsg_type = type;
619 nlh->nlmsg_flags = flags;
620 nlh->nlmsg_pid = pid;
621 nlh->nlmsg_seq = seq;
622
623 NL_DBG(2, "msg %p: Added netlink header type=%d, flags=%d, pid=%d, "
624 "seq=%d\n", n, type, flags, pid, seq);
625
626 if (payload > 0 &&
627 nlmsg_reserve(n, payload, NLMSG_ALIGNTO) == NULL)
628 return NULL;
629
630 return nlh;
631}
632
633/**
634 * Return actual netlink message
635 * @arg n netlink message
636 *
637 * Returns the actual netlink message casted to the type of the netlink
638 * message header.
639 *
640 * @return A pointer to the netlink message.
641 */
642struct nlmsghdr *nlmsg_hdr(struct nl_msg *n)
643{
644 return n->nm_nlh;
645}
646
647/**
648 * Free a netlink message
649 * @arg n netlink message
650 *
651 * Destroys a netlink message and frees up all used memory.
652 *
653 * @pre The message must be unused.
654 */
655void nlmsg_free(struct nl_msg *n)
656{
657 if (!n)
658 return;
659
660 free(n->nm_nlh);
661 free(n);
662 NL_DBG(2, "msg %p: Freed\n", n);
663}
664
665/** @} */
666
667/**
668 * @name Attributes
669 * @{
670 */
671
672void nlmsg_set_proto(struct nl_msg *msg, int protocol)
673{
674 msg->nm_protocol = protocol;
675}
676
677int nlmsg_get_proto(struct nl_msg *msg)
678{
679 return msg->nm_protocol;
680}
681
Thomas Graf75a26f32008-01-14 16:46:17 +0100682size_t nlmsg_get_max_size(struct nl_msg *msg)
683{
684 return msg->nm_size;
685}
686
Thomas Graf44d36242007-09-15 01:28:01 +0200687void nlmsg_set_src(struct nl_msg *msg, struct sockaddr_nl *addr)
688{
689 memcpy(&msg->nm_src, addr, sizeof(*addr));
690}
691
692struct sockaddr_nl *nlmsg_get_src(struct nl_msg *msg)
693{
694 return &msg->nm_src;
695}
696
697void nlmsg_set_dst(struct nl_msg *msg, struct sockaddr_nl *addr)
698{
699 memcpy(&msg->nm_dst, addr, sizeof(*addr));
700}
701
702struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *msg)
703{
704 return &msg->nm_dst;
705}
706
707void nlmsg_set_creds(struct nl_msg *msg, struct ucred *creds)
708{
709 memcpy(&msg->nm_creds, creds, sizeof(*creds));
710 msg->nm_flags |= NL_MSG_CRED_PRESENT;
711}
712
713struct ucred *nlmsg_get_creds(struct nl_msg *msg)
714{
715 if (msg->nm_flags & NL_MSG_CRED_PRESENT)
716 return &msg->nm_creds;
717 return NULL;
718}
719
720/** @} */
721
722/**
723 * @name Netlink Message Type Translations
724 * @{
725 */
726
727static struct trans_tbl nl_msgtypes[] = {
728 __ADD(NLMSG_NOOP,NOOP)
729 __ADD(NLMSG_ERROR,ERROR)
730 __ADD(NLMSG_DONE,DONE)
731 __ADD(NLMSG_OVERRUN,OVERRUN)
732};
733
734char *nl_nlmsgtype2str(int type, char *buf, size_t size)
735{
736 return __type2str(type, buf, size, nl_msgtypes,
737 ARRAY_SIZE(nl_msgtypes));
738}
739
740int nl_str2nlmsgtype(const char *name)
741{
742 return __str2type(name, nl_msgtypes, ARRAY_SIZE(nl_msgtypes));
743}
744
745/** @} */
746
747/**
748 * @name Netlink Message Flags Translations
749 * @{
750 */
751
752char *nl_nlmsg_flags2str(int flags, char *buf, size_t len)
753{
754 memset(buf, 0, len);
755
756#define PRINT_FLAG(f) \
757 if (flags & NLM_F_##f) { \
758 flags &= ~NLM_F_##f; \
759 strncat(buf, #f, len - strlen(buf) - 1); \
760 if (flags) \
761 strncat(buf, ",", len - strlen(buf) - 1); \
762 }
763
764 PRINT_FLAG(REQUEST);
765 PRINT_FLAG(MULTI);
766 PRINT_FLAG(ACK);
767 PRINT_FLAG(ECHO);
768 PRINT_FLAG(ROOT);
769 PRINT_FLAG(MATCH);
770 PRINT_FLAG(ATOMIC);
771 PRINT_FLAG(REPLACE);
772 PRINT_FLAG(EXCL);
773 PRINT_FLAG(CREATE);
774 PRINT_FLAG(APPEND);
775
776 if (flags) {
777 char s[32];
778 snprintf(s, sizeof(s), "0x%x", flags);
779 strncat(buf, s, len - strlen(buf) - 1);
780 }
781#undef PRINT_FLAG
782
783 return buf;
784}
785
786/** @} */
787
788/**
789 * @name Direct Parsing
790 * @{
791 */
792
793/** @cond SKIP */
794struct dp_xdata {
795 void (*cb)(struct nl_object *, void *);
796 void *arg;
797};
798/** @endcond */
799
800static int parse_cb(struct nl_object *obj, struct nl_parser_param *p)
801{
802 struct dp_xdata *x = p->pp_arg;
803
804 x->cb(obj, x->arg);
Thomas Graf44d36242007-09-15 01:28:01 +0200805 return 0;
806}
807
808int nl_msg_parse(struct nl_msg *msg, void (*cb)(struct nl_object *, void *),
809 void *arg)
810{
811 struct nl_cache_ops *ops;
812 struct nl_parser_param p = {
813 .pp_cb = parse_cb
814 };
815 struct dp_xdata x = {
816 .cb = cb,
817 .arg = arg,
818 };
819
Thomas Grafd36d3962007-10-11 23:09:49 +0200820 ops = nl_cache_ops_associate(nlmsg_get_proto(msg),
821 nlmsg_hdr(msg)->nlmsg_type);
Thomas Graf44d36242007-09-15 01:28:01 +0200822 if (ops == NULL)
Thomas Graf8a3efff2008-05-14 17:49:44 +0200823 return -NLE_MSGTYPE_NOSUPPORT;
Thomas Graf44d36242007-09-15 01:28:01 +0200824 p.pp_arg = &x;
825
826 return nl_cache_parse(ops, NULL, nlmsg_hdr(msg), &p);
827}
828
829/** @} */
830
831/**
832 * @name Dumping
833 * @{
834 */
835
836static void prefix_line(FILE *ofd, int prefix)
837{
838 int i;
839
840 for (i = 0; i < prefix; i++)
841 fprintf(ofd, " ");
842}
843
844static inline void dump_hex(FILE *ofd, char *start, int len, int prefix)
845{
846 int i, a, c, limit;
847 char ascii[21] = {0};
848
849 limit = 18 - (prefix * 2);
850 prefix_line(ofd, prefix);
851 fprintf(ofd, " ");
852
853 for (i = 0, a = 0, c = 0; i < len; i++) {
854 int v = *(uint8_t *) (start + i);
855
856 fprintf(ofd, "%02x ", v);
857 ascii[a++] = isprint(v) ? v : '.';
858
859 if (c == limit-1) {
860 fprintf(ofd, "%s\n", ascii);
861 if (i < (len - 1)) {
862 prefix_line(ofd, prefix);
863 fprintf(ofd, " ");
864 }
865 a = c = 0;
866 memset(ascii, 0, sizeof(ascii));
867 } else
868 c++;
869 }
870
871 if (c != 0) {
872 for (i = 0; i < (limit - c); i++)
873 fprintf(ofd, " ");
874 fprintf(ofd, "%s\n", ascii);
875 }
876}
877
878static void print_hdr(FILE *ofd, struct nl_msg *msg)
879{
880 struct nlmsghdr *nlh = nlmsg_hdr(msg);
881 struct nl_cache_ops *ops;
Thomas Grafd36d3962007-10-11 23:09:49 +0200882 struct nl_msgtype *mt;
Thomas Graf44d36242007-09-15 01:28:01 +0200883 char buf[128];
884
885 fprintf(ofd, " .nlmsg_len = %d\n", nlh->nlmsg_len);
886
Thomas Grafd36d3962007-10-11 23:09:49 +0200887 ops = nl_cache_ops_associate(nlmsg_get_proto(msg), nlh->nlmsg_type);
888 if (ops) {
889 mt = nl_msgtype_lookup(ops, nlh->nlmsg_type);
890 if (!mt)
891 BUG();
Thomas Graf44d36242007-09-15 01:28:01 +0200892
Thomas Grafd36d3962007-10-11 23:09:49 +0200893 snprintf(buf, sizeof(buf), "%s::%s", ops->co_name, mt->mt_name);
894 } else
895 nl_nlmsgtype2str(nlh->nlmsg_type, buf, sizeof(buf));
896
897 fprintf(ofd, " .nlmsg_type = %d <%s>\n", nlh->nlmsg_type, buf);
Thomas Graf44d36242007-09-15 01:28:01 +0200898 fprintf(ofd, " .nlmsg_flags = %d <%s>\n", nlh->nlmsg_flags,
899 nl_nlmsg_flags2str(nlh->nlmsg_flags, buf, sizeof(buf)));
900 fprintf(ofd, " .nlmsg_seq = %d\n", nlh->nlmsg_seq);
901 fprintf(ofd, " .nlmsg_pid = %d\n", nlh->nlmsg_pid);
902
903}
904
905static void dump_attrs(FILE *ofd, struct nlattr *attrs, int attrlen,
906 int prefix)
907{
908 int rem;
909 struct nlattr *nla;
910
911 nla_for_each_attr(nla, attrs, attrlen, rem) {
912 int padlen, alen = nla_len(nla);
913
914 prefix_line(ofd, prefix);
915 fprintf(ofd, " [ATTR %02d%s] %d octets\n", nla_type(nla),
916 nla->nla_type & NLA_F_NESTED ? " NESTED" : "",
917 alen);
918
919 if (nla->nla_type & NLA_F_NESTED)
920 dump_attrs(ofd, nla_data(nla), alen, prefix+1);
921 else
922 dump_hex(ofd, nla_data(nla), alen, prefix);
923
924 padlen = nla_padlen(alen);
925 if (padlen > 0) {
926 prefix_line(ofd, prefix);
927 fprintf(ofd, " [PADDING] %d octets\n",
928 padlen);
929 dump_hex(ofd, nla_data(nla) + alen,
930 padlen, prefix);
931 }
932 }
933
934 if (rem) {
935 prefix_line(ofd, prefix);
936 fprintf(ofd, " [LEFTOVER] %d octets\n", rem);
937 }
938}
939
940/**
941 * Dump message in human readable format to file descriptor
942 * @arg msg Message to print
943 * @arg ofd File descriptor.
944 */
945void nl_msg_dump(struct nl_msg *msg, FILE *ofd)
946{
947 struct nlmsghdr *hdr = nlmsg_hdr(msg);
948
949 fprintf(ofd,
950 "-------------------------- BEGIN NETLINK MESSAGE "
951 "---------------------------\n");
952
953 fprintf(ofd, " [HEADER] %Zu octets\n", sizeof(struct nlmsghdr));
954 print_hdr(ofd, msg);
955
956 if (hdr->nlmsg_type == NLMSG_ERROR &&
957 hdr->nlmsg_len >= nlmsg_msg_size(sizeof(struct nlmsgerr))) {
958 struct nl_msg *errmsg;
959 struct nlmsgerr *err = nlmsg_data(hdr);
960
961 fprintf(ofd, " [ERRORMSG] %Zu octets\n", sizeof(*err));
962 fprintf(ofd, " .error = %d \"%s\"\n", err->error,
963 strerror(-err->error));
964 fprintf(ofd, " [ORIGINAL MESSAGE] %Zu octets\n", sizeof(*hdr));
965
966 errmsg = nlmsg_inherit(&err->msg);
967 print_hdr(ofd, errmsg);
968 nlmsg_free(errmsg);
969 } else if (nlmsg_len(hdr) > 0) {
970 struct nl_cache_ops *ops;
971 int payloadlen = nlmsg_len(hdr);
972 int attrlen = 0;
973
Thomas Grafd36d3962007-10-11 23:09:49 +0200974 ops = nl_cache_ops_associate(nlmsg_get_proto(msg),
975 hdr->nlmsg_type);
Thomas Graf44d36242007-09-15 01:28:01 +0200976 if (ops) {
977 attrlen = nlmsg_attrlen(hdr, ops->co_hdrsize);
978 payloadlen -= attrlen;
979 }
980
981 fprintf(ofd, " [PAYLOAD] %d octets\n", payloadlen);
982 dump_hex(ofd, nlmsg_data(hdr), payloadlen, 0);
983
984 if (attrlen) {
985 struct nlattr *attrs;
986 int attrlen;
987
988 attrs = nlmsg_attrdata(hdr, ops->co_hdrsize);
989 attrlen = nlmsg_attrlen(hdr, ops->co_hdrsize);
990 dump_attrs(ofd, attrs, attrlen, 0);
991 }
992 }
993
994 fprintf(ofd,
995 "--------------------------- END NETLINK MESSAGE "
996 "---------------------------\n");
997}
998
999/** @} */
1000
1001/** @} */