blob: 71573d474604fb7aac134e57a6413e0ddc4897e8 [file] [log] [blame]
Fabien Siron2850f742016-07-06 15:49:22 +00001/*
2 * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
Dmitry V. Levindeb0c182016-07-08 16:01:49 +00003 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
Fabien Siron2850f742016-07-06 15:49:22 +00004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "defs.h"
30#include <sys/socket.h>
31#include <linux/netlink.h>
32#include "xlat/netlink_flags.h"
33#include "xlat/netlink_types.h"
34
Dmitry V. Levindeb0c182016-07-08 16:01:49 +000035/*
36 * Fetch a struct nlmsghdr from the given address.
37 */
38static bool
39fetch_nlmsghdr(struct tcb *tcp, struct nlmsghdr *nlmsghdr,
40 const unsigned long addr, const unsigned long len)
Fabien Siron2850f742016-07-06 15:49:22 +000041{
Dmitry V. Levindeb0c182016-07-08 16:01:49 +000042 if (len < sizeof(struct nlmsghdr)) {
43 printstr(tcp, addr, len);
44 return false;
Fabien Siron2850f742016-07-06 15:49:22 +000045 }
Fabien Siron2850f742016-07-06 15:49:22 +000046
Dmitry V. Levindeb0c182016-07-08 16:01:49 +000047 if (umove_or_printaddr(tcp, addr, nlmsghdr))
48 return false;
Fabien Siron2850f742016-07-06 15:49:22 +000049
Dmitry V. Levindeb0c182016-07-08 16:01:49 +000050 return true;
51}
52
53static void
54print_nlmsghdr(struct tcb *tcp, const struct nlmsghdr *const nlmsghdr)
55{
56 /* print the whole structure regardless of its nlmsg_len */
57
58 tprintf("{len=%u, type=", nlmsghdr->nlmsg_len);
59
60 printxval(netlink_types, nlmsghdr->nlmsg_type, "NLMSG_???");
Fabien Siron2850f742016-07-06 15:49:22 +000061
62 tprints(", flags=");
Dmitry V. Levindeb0c182016-07-08 16:01:49 +000063 printflags(netlink_flags, nlmsghdr->nlmsg_flags, "NLM_F_???");
Fabien Siron2850f742016-07-06 15:49:22 +000064
Dmitry V. Levindeb0c182016-07-08 16:01:49 +000065 tprintf(", seq=%u, pid=%u}", nlmsghdr->nlmsg_seq,
66 nlmsghdr->nlmsg_pid);
67}
Fabien Siron2850f742016-07-06 15:49:22 +000068
Dmitry V. Levindeb0c182016-07-08 16:01:49 +000069static void
70decode_nlmsghdr_with_payload(struct tcb *tcp,
71 const struct nlmsghdr *const nlmsghdr,
72 const unsigned long addr,
73 const unsigned long len)
74{
75 tprints("{");
76
77 print_nlmsghdr(tcp, nlmsghdr);
78
79 unsigned long nlmsg_len =
80 nlmsghdr->nlmsg_len > len ? len : nlmsghdr->nlmsg_len;
81 if (nlmsg_len > sizeof(struct nlmsghdr)) {
Fabien Siron2850f742016-07-06 15:49:22 +000082 tprints(", ");
Dmitry V. Levindeb0c182016-07-08 16:01:49 +000083
Fabien Siron2850f742016-07-06 15:49:22 +000084 printstr(tcp, addr + sizeof(struct nlmsghdr),
Dmitry V. Levindeb0c182016-07-08 16:01:49 +000085 nlmsg_len - sizeof(struct nlmsghdr));
Fabien Siron2850f742016-07-06 15:49:22 +000086 }
87
88 tprints("}");
89}
Dmitry V. Levindeb0c182016-07-08 16:01:49 +000090
91void
92decode_netlink(struct tcb *tcp, unsigned long addr, unsigned long len)
93{
94 struct nlmsghdr nlmsghdr;
95 bool print_array = false;
96 unsigned int elt;
97
98 for (elt = 0; fetch_nlmsghdr(tcp, &nlmsghdr, addr, len); elt++) {
99 if (abbrev(tcp) && elt == max_strlen) {
100 tprints("...");
101 break;
102 }
103
104 unsigned long nlmsg_len = NLMSG_ALIGN(nlmsghdr.nlmsg_len);
105 unsigned long next_addr = 0, next_len = 0;
106
107 if (nlmsghdr.nlmsg_len >= sizeof(struct nlmsghdr)) {
108 next_len = (len >= nlmsg_len) ? len - nlmsg_len : 0;
109
110 if (next_len && addr + nlmsg_len > addr)
111 next_addr = addr + nlmsg_len;
112 }
113
114 if (!print_array && next_addr) {
115 tprints("[");
116 print_array = true;
117 }
118
119 decode_nlmsghdr_with_payload(tcp, &nlmsghdr, addr, len);
120
121 if (!next_addr)
122 break;
123
124 tprints(", ");
125 addr = next_addr;
126 len = next_len;
127 }
128
129 if (print_array) {
130 tprints("]");
131 }
132}