blob: 80b689f29f00544b28d558fa153149fcf273e4de [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 *
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10 */
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 *
96 * @par 1) Creating a new netlink message
97 * @code
98 * // Netlink messages can be allocated in various ways, you may
99 * // allocate an empty netlink message by using nlmsg_alloc():
100 * struct nl_msg *msg = nlmsg_alloc();
101 *
102 * // Very often, the message type and message flags are known
103 * // at allocation time while the other fields are auto generated:
104 * struct nl_msg *msg = nlmsg_alloc_simple(MY_TYPE, MY_FLAGS);
105 *
106 * // Alternatively an existing netlink message header can be used
107 * // to inherit header values from:
108 * struct nlmsghdr hdr = {
109 * .nlmsg_type = MY_TYPE,
110 * .nlmsg_flags = MY_FLAGS,
111 * };
112 * struct nl_msg *msg = nlmsg_inherit(&hdr);
113 *
114 * // Last but not least, netlink messages received from netlink sockets
115 * // can be converted into nl_msg objects using nlmsg_convert():
116 * struct nl_msg *msg = nlmsg_convert(nlh_from_nl_sock);
117 *
118 * // The header can later be retrieved with nlmsg_hdr() and changed again:
119 * nlmsg_hdr(msg)->nlmsg_flags |= YET_ANOTHER_FLAG;
120 * @endcode
121 *
122 * @par 2) Appending data to the message
123 * @code
124 * // Payload may be added to the message via nlmsg_append(). The fourth
125 * // parameter specifies the number of alignment bytes the data should
126 * // be padding with at the end. Common values are 0 to disable it or
127 * // NLMSG_ALIGNTO to ensure proper netlink message padding.
128 * nlmsg_append(msg, &mydata, sizeof(mydata), 0);
129 *
130 * // Sometimes it may be necessary to reserve room for data but defer
131 * // the actual copying to a later point, nlmsg_reserve() can be used
132 * // for this purpose:
133 * void *data = nlmsg_reserve(msg, sizeof(mydata), NLMSG_ALIGNTO);
134 * @endcode
135 *
136 * @par 3) Cleaning up message construction
137 * @code
138 * // After successful use of the message, the memory must be freed
139 * // using nlmsg_free()
140 * nlmsg_free(msg);
141 * @endcode
142 *
143 * @par 4) Parsing messages
144 * @code
145 * int n;
146 * unsigned char *buf;
147 * struct nlmsghdr *hdr;
148 *
149 * n = nl_recv(handle, NULL, &buf);
150 *
151 * hdr = (struct nlmsghdr *) buf;
152 * while (nlmsg_ok(hdr, n)) {
153 * // Process message here...
154 * hdr = nlmsg_next(hdr, &n);
155 * }
156 * @endcode
157 * @{
158 */
159
160#include <netlink-local.h>
161#include <netlink/netlink.h>
162#include <netlink/utils.h>
163#include <netlink/cache.h>
164#include <netlink/attr.h>
165#include <linux/socket.h>
166
Thomas Graf6de17f32008-01-14 16:17:56 +0100167static size_t default_msg_size;
168
169static void __init init_msg_size(void)
170{
171 default_msg_size = getpagesize();
172}
173
Thomas Graf44d36242007-09-15 01:28:01 +0200174/**
175 * @name Size Calculations
176 * @{
177 */
178
179/**
180 * length of netlink message not including padding
181 * @arg payload length of message payload
182 */
183int nlmsg_msg_size(int payload)
184{
185 return NLMSG_HDRLEN + payload;
186}
187
188/**
189 * length of netlink message including padding
190 * @arg payload length of message payload
191 */
192int nlmsg_total_size(int payload)
193{
194 return NLMSG_ALIGN(nlmsg_msg_size(payload));
195}
196
197/**
198 * length of padding at the message's tail
199 * @arg payload length of message payload
200 */
201int nlmsg_padlen(int payload)
202{
203 return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
204}
205
206/** @} */
207
208/**
209 * @name Payload Access
210 * @{
211 */
212
213/**
214 * head of message payload
215 * @arg nlh netlink messsage header
216 */
217void *nlmsg_data(const struct nlmsghdr *nlh)
218{
219 return (unsigned char *) nlh + NLMSG_HDRLEN;
220}
221
222void *nlmsg_tail(const struct nlmsghdr *nlh)
223{
224 return (unsigned char *) nlh + NLMSG_ALIGN(nlh->nlmsg_len);
225}
226
227/**
228 * length of message payload
229 * @arg nlh netlink message header
230 */
231int nlmsg_len(const struct nlmsghdr *nlh)
232{
233 return nlh->nlmsg_len - NLMSG_HDRLEN;
234}
235
236/** @} */
237
238/**
239 * @name Attribute Access
240 * @{
241 */
242
243/**
244 * head of attributes data
245 * @arg nlh netlink message header
246 * @arg hdrlen length of family specific header
247 */
248struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, int hdrlen)
249{
250 unsigned char *data = nlmsg_data(nlh);
251 return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
252}
253
254/**
255 * length of attributes data
256 * @arg nlh netlink message header
257 * @arg hdrlen length of family specific header
258 */
259int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
260{
261 return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
262}
263
264/** @} */
265
266/**
267 * @name Message Parsing
268 * @{
269 */
270
Thomas Grafc8a0a5c2008-01-10 12:35:38 +0100271int nlmsg_valid_hdr(const struct nlmsghdr *nlh, int hdrlen)
272{
273 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
274 return 0;
275
276 return 1;
277}
278
Thomas Graf44d36242007-09-15 01:28:01 +0200279/**
280 * check if the netlink message fits into the remaining bytes
281 * @arg nlh netlink message header
282 * @arg remaining number of bytes remaining in message stream
283 */
284int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
285{
286 return (remaining >= sizeof(struct nlmsghdr) &&
287 nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
288 nlh->nlmsg_len <= remaining);
289}
290
291/**
292 * next netlink message in message stream
293 * @arg nlh netlink message header
294 * @arg remaining number of bytes remaining in message stream
295 *
296 * @returns the next netlink message in the message stream and
297 * decrements remaining by the size of the current message.
298 */
299struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
300{
301 int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
302
303 *remaining -= totlen;
304
305 return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
306}
307
308/**
309 * parse attributes of a netlink message
310 * @arg nlh netlink message header
311 * @arg hdrlen length of family specific header
312 * @arg tb destination array with maxtype+1 elements
313 * @arg maxtype maximum attribute type to be expected
314 * @arg policy validation policy
315 *
316 * See nla_parse()
317 */
318int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[],
319 int maxtype, struct nla_policy *policy)
320{
Thomas Grafc8a0a5c2008-01-10 12:35:38 +0100321 if (!nlmsg_valid_hdr(nlh, hdrlen))
Thomas Graf44d36242007-09-15 01:28:01 +0200322 return nl_errno(EINVAL);
323
324 return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
325 nlmsg_attrlen(nlh, hdrlen), policy);
326}
327
328/**
329 * nlmsg_find_attr - find a specific attribute in a netlink message
330 * @arg nlh netlink message header
331 * @arg hdrlen length of familiy specific header
332 * @arg attrtype type of attribute to look for
333 *
334 * Returns the first attribute which matches the specified type.
335 */
336struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh, int hdrlen, int attrtype)
337{
338 return nla_find(nlmsg_attrdata(nlh, hdrlen),
339 nlmsg_attrlen(nlh, hdrlen), attrtype);
340}
341
342/**
343 * nlmsg_validate - validate a netlink message including attributes
344 * @arg nlh netlinket message header
345 * @arg hdrlen length of familiy specific header
346 * @arg maxtype maximum attribute type to be expected
347 * @arg policy validation policy
348 */
349int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
350 struct nla_policy *policy)
351{
Thomas Grafc8a0a5c2008-01-10 12:35:38 +0100352 if (!nlmsg_valid_hdr(nlh, hdrlen))
Thomas Graf44d36242007-09-15 01:28:01 +0200353 return nl_errno(EINVAL);
354
355 return nla_validate(nlmsg_attrdata(nlh, hdrlen),
356 nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
357}
358
359/** @} */
360
361/**
362 * @name Message Building/Access
363 * @{
364 */
365
366static struct nl_msg *__nlmsg_alloc(size_t len)
367{
368 struct nl_msg *nm;
369
370 nm = calloc(1, sizeof(*nm));
371 if (!nm)
372 goto errout;
373
374 nm->nm_nlh = calloc(1, len);
375 if (!nm->nm_nlh)
376 goto errout;
377
378 nm->nm_protocol = -1;
Thomas Graf6de17f32008-01-14 16:17:56 +0100379 nm->nm_size = len;
380 nm->nm_nlh->nlmsg_len = nlmsg_total_size(0);
Thomas Graf44d36242007-09-15 01:28:01 +0200381
Thomas Graf6de17f32008-01-14 16:17:56 +0100382 NL_DBG(2, "msg %p: Allocated new message, maxlen=%zu\n", nm, len);
Thomas Graf44d36242007-09-15 01:28:01 +0200383
384 return nm;
385errout:
386 free(nm);
387 nl_errno(ENOMEM);
388 return NULL;
389}
390
391/**
Thomas Graf6de17f32008-01-14 16:17:56 +0100392 * Allocate a new netlink message with the default maximum payload size.
Thomas Graf44d36242007-09-15 01:28:01 +0200393 *
Thomas Graf6de17f32008-01-14 16:17:56 +0100394 * Allocates a new netlink message without any further payload. The
395 * maximum payload size defaults to PAGESIZE or as otherwise specified
396 * with nlmsg_set_default_size().
Thomas Graf44d36242007-09-15 01:28:01 +0200397 *
398 * @return Newly allocated netlink message or NULL.
399 */
400struct nl_msg *nlmsg_alloc(void)
401{
Thomas Graf6de17f32008-01-14 16:17:56 +0100402 return __nlmsg_alloc(default_msg_size);
403}
404
405/**
406 * Allocate a new netlink message with maximum payload size specified.
407 */
408struct nl_msg *nlmsg_alloc_size(size_t max)
409{
410 return __nlmsg_alloc(max);
Thomas Graf44d36242007-09-15 01:28:01 +0200411}
412
413/**
414 * Allocate a new netlink message and inherit netlink message header
415 * @arg hdr Netlink message header template
416 *
Thomas Graf6de17f32008-01-14 16:17:56 +0100417 * Allocates a new netlink message and inherits the original message
418 * header. If \a hdr is not NULL it will be used as a template for
419 * the netlink message header, otherwise the header is left blank.
Thomas Graf44d36242007-09-15 01:28:01 +0200420 *
421 * @return Newly allocated netlink message or NULL
422 */
423struct nl_msg *nlmsg_inherit(struct nlmsghdr *hdr)
424{
425 struct nl_msg *nm;
426
427 nm = nlmsg_alloc();
428 if (nm && hdr) {
429 struct nlmsghdr *new = nm->nm_nlh;
430
431 new->nlmsg_type = hdr->nlmsg_type;
432 new->nlmsg_flags = hdr->nlmsg_flags;
433 new->nlmsg_seq = hdr->nlmsg_seq;
434 new->nlmsg_pid = hdr->nlmsg_pid;
435 }
436
437 return nm;
438}
439
440/**
441 * Allocate a new netlink message
442 * @arg nlmsgtype Netlink message type
443 * @arg flags Message flags.
444 *
445 * @return Newly allocated netlink message or NULL.
446 */
447struct nl_msg *nlmsg_alloc_simple(int nlmsgtype, int flags)
448{
449 struct nl_msg *msg;
450 struct nlmsghdr nlh = {
451 .nlmsg_type = nlmsgtype,
452 .nlmsg_flags = flags,
453 };
454
455 msg = nlmsg_inherit(&nlh);
456 if (msg)
457 NL_DBG(2, "msg %p: Allocated new simple message\n", msg);
458
459 return msg;
460}
461
462/**
Thomas Graf6de17f32008-01-14 16:17:56 +0100463 * Set the default maximum message payload size for allocated messages
464 * @arg max Size of payload in bytes.
465 */
466void nlmsg_set_default_size(size_t max)
467{
468 if (max < nlmsg_total_size(0))
469 max = nlmsg_total_size(0);
470
471 default_msg_size = max;
472}
473
474/**
Thomas Graf44d36242007-09-15 01:28:01 +0200475 * Convert a netlink message received from a netlink socket to a nl_msg
476 * @arg hdr Netlink message received from netlink socket.
477 *
478 * Allocates a new netlink message and copies all of the data pointed to
479 * by \a hdr into the new message object.
480 *
481 * @return Newly allocated netlink message or NULL.
482 */
483struct nl_msg *nlmsg_convert(struct nlmsghdr *hdr)
484{
485 struct nl_msg *nm;
486
487 nm = __nlmsg_alloc(NLMSG_ALIGN(hdr->nlmsg_len));
488 if (!nm)
489 goto errout;
490
491 memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len);
492
493 return nm;
494errout:
495 nlmsg_free(nm);
496 return NULL;
497}
498
499/**
500 * Reserve room for additional data in a netlink message
501 * @arg n netlink message
502 * @arg len length of additional data to reserve room for
503 * @arg pad number of bytes to align data to
504 *
505 * Reserves room for additional data at the tail of the an
506 * existing netlink message. Eventual padding required will
507 * be zeroed out.
508 *
Thomas Graf44d36242007-09-15 01:28:01 +0200509 * @return Pointer to start of additional data tailroom or NULL.
510 */
511void *nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
512{
Thomas Graf6de17f32008-01-14 16:17:56 +0100513 void *buf = n->nm_nlh;
514 size_t nlmsg_len = n->nm_nlh->nlmsg_len;
Thomas Graf44d36242007-09-15 01:28:01 +0200515 size_t tlen;
516
517 tlen = pad ? ((len + (pad - 1)) & ~(pad - 1)) : len;
518
Thomas Graf6de17f32008-01-14 16:17:56 +0100519 if ((tlen + nlmsg_len) > n->nm_size) {
520 nl_errno(ENOBUFS);
Thomas Graf44d36242007-09-15 01:28:01 +0200521 return NULL;
522 }
523
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)
554 return nl_errno(ENOMEM);
555
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/**
563 * Add a netlink message header to a netlink message
564 * @arg n netlink message
565 * @arg pid netlink process id or NL_AUTO_PID
566 * @arg seq sequence number of message or NL_AUTO_SEQ
567 * @arg type message type
568 * @arg payload length of message payload
569 * @arg flags message flags
570 *
571 * Adds or overwrites the netlink message header in an existing message
572 * object. If \a payload is greater-than zero additional room will be
573 * reserved, f.e. for family specific headers. It can be accesed via
574 * nlmsg_data().
575 *
576 * @return A pointer to the netlink message header or NULL.
577 */
578struct nlmsghdr *nlmsg_put(struct nl_msg *n, uint32_t pid, uint32_t seq,
579 int type, int payload, int flags)
580{
581 struct nlmsghdr *nlh;
582
583 if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN)
584 BUG();
585
586 nlh = (struct nlmsghdr *) n->nm_nlh;
587 nlh->nlmsg_type = type;
588 nlh->nlmsg_flags = flags;
589 nlh->nlmsg_pid = pid;
590 nlh->nlmsg_seq = seq;
591
592 NL_DBG(2, "msg %p: Added netlink header type=%d, flags=%d, pid=%d, "
593 "seq=%d\n", n, type, flags, pid, seq);
594
595 if (payload > 0 &&
596 nlmsg_reserve(n, payload, NLMSG_ALIGNTO) == NULL)
597 return NULL;
598
599 return nlh;
600}
601
602/**
603 * Return actual netlink message
604 * @arg n netlink message
605 *
606 * Returns the actual netlink message casted to the type of the netlink
607 * message header.
608 *
609 * @return A pointer to the netlink message.
610 */
611struct nlmsghdr *nlmsg_hdr(struct nl_msg *n)
612{
613 return n->nm_nlh;
614}
615
616/**
617 * Free a netlink message
618 * @arg n netlink message
619 *
620 * Destroys a netlink message and frees up all used memory.
621 *
622 * @pre The message must be unused.
623 */
624void nlmsg_free(struct nl_msg *n)
625{
626 if (!n)
627 return;
628
629 free(n->nm_nlh);
630 free(n);
631 NL_DBG(2, "msg %p: Freed\n", n);
632}
633
634/** @} */
635
636/**
637 * @name Attributes
638 * @{
639 */
640
641void nlmsg_set_proto(struct nl_msg *msg, int protocol)
642{
643 msg->nm_protocol = protocol;
644}
645
646int nlmsg_get_proto(struct nl_msg *msg)
647{
648 return msg->nm_protocol;
649}
650
651void nlmsg_set_src(struct nl_msg *msg, struct sockaddr_nl *addr)
652{
653 memcpy(&msg->nm_src, addr, sizeof(*addr));
654}
655
656struct sockaddr_nl *nlmsg_get_src(struct nl_msg *msg)
657{
658 return &msg->nm_src;
659}
660
661void nlmsg_set_dst(struct nl_msg *msg, struct sockaddr_nl *addr)
662{
663 memcpy(&msg->nm_dst, addr, sizeof(*addr));
664}
665
666struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *msg)
667{
668 return &msg->nm_dst;
669}
670
671void nlmsg_set_creds(struct nl_msg *msg, struct ucred *creds)
672{
673 memcpy(&msg->nm_creds, creds, sizeof(*creds));
674 msg->nm_flags |= NL_MSG_CRED_PRESENT;
675}
676
677struct ucred *nlmsg_get_creds(struct nl_msg *msg)
678{
679 if (msg->nm_flags & NL_MSG_CRED_PRESENT)
680 return &msg->nm_creds;
681 return NULL;
682}
683
684/** @} */
685
686/**
687 * @name Netlink Message Type Translations
688 * @{
689 */
690
691static struct trans_tbl nl_msgtypes[] = {
692 __ADD(NLMSG_NOOP,NOOP)
693 __ADD(NLMSG_ERROR,ERROR)
694 __ADD(NLMSG_DONE,DONE)
695 __ADD(NLMSG_OVERRUN,OVERRUN)
696};
697
698char *nl_nlmsgtype2str(int type, char *buf, size_t size)
699{
700 return __type2str(type, buf, size, nl_msgtypes,
701 ARRAY_SIZE(nl_msgtypes));
702}
703
704int nl_str2nlmsgtype(const char *name)
705{
706 return __str2type(name, nl_msgtypes, ARRAY_SIZE(nl_msgtypes));
707}
708
709/** @} */
710
711/**
712 * @name Netlink Message Flags Translations
713 * @{
714 */
715
716char *nl_nlmsg_flags2str(int flags, char *buf, size_t len)
717{
718 memset(buf, 0, len);
719
720#define PRINT_FLAG(f) \
721 if (flags & NLM_F_##f) { \
722 flags &= ~NLM_F_##f; \
723 strncat(buf, #f, len - strlen(buf) - 1); \
724 if (flags) \
725 strncat(buf, ",", len - strlen(buf) - 1); \
726 }
727
728 PRINT_FLAG(REQUEST);
729 PRINT_FLAG(MULTI);
730 PRINT_FLAG(ACK);
731 PRINT_FLAG(ECHO);
732 PRINT_FLAG(ROOT);
733 PRINT_FLAG(MATCH);
734 PRINT_FLAG(ATOMIC);
735 PRINT_FLAG(REPLACE);
736 PRINT_FLAG(EXCL);
737 PRINT_FLAG(CREATE);
738 PRINT_FLAG(APPEND);
739
740 if (flags) {
741 char s[32];
742 snprintf(s, sizeof(s), "0x%x", flags);
743 strncat(buf, s, len - strlen(buf) - 1);
744 }
745#undef PRINT_FLAG
746
747 return buf;
748}
749
750/** @} */
751
752/**
753 * @name Direct Parsing
754 * @{
755 */
756
757/** @cond SKIP */
758struct dp_xdata {
759 void (*cb)(struct nl_object *, void *);
760 void *arg;
761};
762/** @endcond */
763
764static int parse_cb(struct nl_object *obj, struct nl_parser_param *p)
765{
766 struct dp_xdata *x = p->pp_arg;
767
768 x->cb(obj, x->arg);
Thomas Graf44d36242007-09-15 01:28:01 +0200769 return 0;
770}
771
772int nl_msg_parse(struct nl_msg *msg, void (*cb)(struct nl_object *, void *),
773 void *arg)
774{
775 struct nl_cache_ops *ops;
776 struct nl_parser_param p = {
777 .pp_cb = parse_cb
778 };
779 struct dp_xdata x = {
780 .cb = cb,
781 .arg = arg,
782 };
783
Thomas Grafd36d3962007-10-11 23:09:49 +0200784 ops = nl_cache_ops_associate(nlmsg_get_proto(msg),
785 nlmsg_hdr(msg)->nlmsg_type);
Thomas Graf44d36242007-09-15 01:28:01 +0200786 if (ops == NULL)
787 return nl_error(ENOENT, "Unknown message type %d",
788 nlmsg_hdr(msg)->nlmsg_type);
789 p.pp_arg = &x;
790
791 return nl_cache_parse(ops, NULL, nlmsg_hdr(msg), &p);
792}
793
794/** @} */
795
796/**
797 * @name Dumping
798 * @{
799 */
800
801static void prefix_line(FILE *ofd, int prefix)
802{
803 int i;
804
805 for (i = 0; i < prefix; i++)
806 fprintf(ofd, " ");
807}
808
809static inline void dump_hex(FILE *ofd, char *start, int len, int prefix)
810{
811 int i, a, c, limit;
812 char ascii[21] = {0};
813
814 limit = 18 - (prefix * 2);
815 prefix_line(ofd, prefix);
816 fprintf(ofd, " ");
817
818 for (i = 0, a = 0, c = 0; i < len; i++) {
819 int v = *(uint8_t *) (start + i);
820
821 fprintf(ofd, "%02x ", v);
822 ascii[a++] = isprint(v) ? v : '.';
823
824 if (c == limit-1) {
825 fprintf(ofd, "%s\n", ascii);
826 if (i < (len - 1)) {
827 prefix_line(ofd, prefix);
828 fprintf(ofd, " ");
829 }
830 a = c = 0;
831 memset(ascii, 0, sizeof(ascii));
832 } else
833 c++;
834 }
835
836 if (c != 0) {
837 for (i = 0; i < (limit - c); i++)
838 fprintf(ofd, " ");
839 fprintf(ofd, "%s\n", ascii);
840 }
841}
842
843static void print_hdr(FILE *ofd, struct nl_msg *msg)
844{
845 struct nlmsghdr *nlh = nlmsg_hdr(msg);
846 struct nl_cache_ops *ops;
Thomas Grafd36d3962007-10-11 23:09:49 +0200847 struct nl_msgtype *mt;
Thomas Graf44d36242007-09-15 01:28:01 +0200848 char buf[128];
849
850 fprintf(ofd, " .nlmsg_len = %d\n", nlh->nlmsg_len);
851
Thomas Grafd36d3962007-10-11 23:09:49 +0200852 ops = nl_cache_ops_associate(nlmsg_get_proto(msg), nlh->nlmsg_type);
853 if (ops) {
854 mt = nl_msgtype_lookup(ops, nlh->nlmsg_type);
855 if (!mt)
856 BUG();
Thomas Graf44d36242007-09-15 01:28:01 +0200857
Thomas Grafd36d3962007-10-11 23:09:49 +0200858 snprintf(buf, sizeof(buf), "%s::%s", ops->co_name, mt->mt_name);
859 } else
860 nl_nlmsgtype2str(nlh->nlmsg_type, buf, sizeof(buf));
861
862 fprintf(ofd, " .nlmsg_type = %d <%s>\n", nlh->nlmsg_type, buf);
Thomas Graf44d36242007-09-15 01:28:01 +0200863 fprintf(ofd, " .nlmsg_flags = %d <%s>\n", nlh->nlmsg_flags,
864 nl_nlmsg_flags2str(nlh->nlmsg_flags, buf, sizeof(buf)));
865 fprintf(ofd, " .nlmsg_seq = %d\n", nlh->nlmsg_seq);
866 fprintf(ofd, " .nlmsg_pid = %d\n", nlh->nlmsg_pid);
867
868}
869
870static void dump_attrs(FILE *ofd, struct nlattr *attrs, int attrlen,
871 int prefix)
872{
873 int rem;
874 struct nlattr *nla;
875
876 nla_for_each_attr(nla, attrs, attrlen, rem) {
877 int padlen, alen = nla_len(nla);
878
879 prefix_line(ofd, prefix);
880 fprintf(ofd, " [ATTR %02d%s] %d octets\n", nla_type(nla),
881 nla->nla_type & NLA_F_NESTED ? " NESTED" : "",
882 alen);
883
884 if (nla->nla_type & NLA_F_NESTED)
885 dump_attrs(ofd, nla_data(nla), alen, prefix+1);
886 else
887 dump_hex(ofd, nla_data(nla), alen, prefix);
888
889 padlen = nla_padlen(alen);
890 if (padlen > 0) {
891 prefix_line(ofd, prefix);
892 fprintf(ofd, " [PADDING] %d octets\n",
893 padlen);
894 dump_hex(ofd, nla_data(nla) + alen,
895 padlen, prefix);
896 }
897 }
898
899 if (rem) {
900 prefix_line(ofd, prefix);
901 fprintf(ofd, " [LEFTOVER] %d octets\n", rem);
902 }
903}
904
905/**
906 * Dump message in human readable format to file descriptor
907 * @arg msg Message to print
908 * @arg ofd File descriptor.
909 */
910void nl_msg_dump(struct nl_msg *msg, FILE *ofd)
911{
912 struct nlmsghdr *hdr = nlmsg_hdr(msg);
913
914 fprintf(ofd,
915 "-------------------------- BEGIN NETLINK MESSAGE "
916 "---------------------------\n");
917
918 fprintf(ofd, " [HEADER] %Zu octets\n", sizeof(struct nlmsghdr));
919 print_hdr(ofd, msg);
920
921 if (hdr->nlmsg_type == NLMSG_ERROR &&
922 hdr->nlmsg_len >= nlmsg_msg_size(sizeof(struct nlmsgerr))) {
923 struct nl_msg *errmsg;
924 struct nlmsgerr *err = nlmsg_data(hdr);
925
926 fprintf(ofd, " [ERRORMSG] %Zu octets\n", sizeof(*err));
927 fprintf(ofd, " .error = %d \"%s\"\n", err->error,
928 strerror(-err->error));
929 fprintf(ofd, " [ORIGINAL MESSAGE] %Zu octets\n", sizeof(*hdr));
930
931 errmsg = nlmsg_inherit(&err->msg);
932 print_hdr(ofd, errmsg);
933 nlmsg_free(errmsg);
934 } else if (nlmsg_len(hdr) > 0) {
935 struct nl_cache_ops *ops;
936 int payloadlen = nlmsg_len(hdr);
937 int attrlen = 0;
938
Thomas Grafd36d3962007-10-11 23:09:49 +0200939 ops = nl_cache_ops_associate(nlmsg_get_proto(msg),
940 hdr->nlmsg_type);
Thomas Graf44d36242007-09-15 01:28:01 +0200941 if (ops) {
942 attrlen = nlmsg_attrlen(hdr, ops->co_hdrsize);
943 payloadlen -= attrlen;
944 }
945
946 fprintf(ofd, " [PAYLOAD] %d octets\n", payloadlen);
947 dump_hex(ofd, nlmsg_data(hdr), payloadlen, 0);
948
949 if (attrlen) {
950 struct nlattr *attrs;
951 int attrlen;
952
953 attrs = nlmsg_attrdata(hdr, ops->co_hdrsize);
954 attrlen = nlmsg_attrlen(hdr, ops->co_hdrsize);
955 dump_attrs(ofd, attrs, attrlen, 0);
956 }
957 }
958
959 fprintf(ofd,
960 "--------------------------- END NETLINK MESSAGE "
961 "---------------------------\n");
962}
963
964/** @} */
965
966/** @} */