]> granicus.if.org Git - strace/blob - netlink.c
netlink: decode NLMSG_DONE messages
[strace] / netlink.c
1 /*
2  * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
4  * Copyright (c) 2016-2017 The strace developers.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "defs.h"
31 #include <sys/socket.h>
32 #include <linux/netlink.h>
33 #include "xlat/netlink_flags.h"
34 #include "xlat/netlink_types.h"
35
36 #undef NLMSG_HDRLEN
37 #define NLMSG_HDRLEN NLMSG_ALIGN(sizeof(struct nlmsghdr))
38
39 /*
40  * Fetch a struct nlmsghdr from the given address.
41  */
42 static bool
43 fetch_nlmsghdr(struct tcb *const tcp, struct nlmsghdr *const nlmsghdr,
44                const kernel_ulong_t addr, const kernel_ulong_t len)
45 {
46         if (len < sizeof(struct nlmsghdr)) {
47                 printstrn(tcp, addr, len);
48                 return false;
49         }
50
51         if (umove_or_printaddr(tcp, addr, nlmsghdr))
52                 return false;
53
54         return true;
55 }
56
57 static void
58 print_nlmsghdr(struct tcb *tcp, const struct nlmsghdr *const nlmsghdr)
59 {
60         /* print the whole structure regardless of its nlmsg_len */
61
62         tprintf("{len=%u, type=", nlmsghdr->nlmsg_len);
63
64         printxval(netlink_types, nlmsghdr->nlmsg_type, "NLMSG_???");
65
66         tprints(", flags=");
67         printflags(netlink_flags, nlmsghdr->nlmsg_flags, "NLM_F_???");
68
69         tprintf(", seq=%u, pid=%u}", nlmsghdr->nlmsg_seq,
70                 nlmsghdr->nlmsg_pid);
71 }
72
73 static void
74 decode_nlmsghdr_with_payload(struct tcb *const tcp,
75                              const struct nlmsghdr *const nlmsghdr,
76                              const kernel_ulong_t addr,
77                              const kernel_ulong_t len);
78
79 static void
80 decode_nlmsgerr(struct tcb *const tcp,
81                kernel_ulong_t addr,
82                kernel_ulong_t len)
83 {
84         struct nlmsgerr err;
85
86         if (len < sizeof(err.error)) {
87                 printstrn(tcp, addr, len);
88                 return;
89         }
90
91         if (umove_or_printaddr(tcp, addr, &err.error))
92                 return;
93
94         tprints("{error=");
95         if (err.error < 0 && (unsigned) -err.error < nerrnos) {
96                 tprintf("-%s", errnoent[-err.error]);
97         } else {
98                 tprintf("%d", err.error);
99         }
100
101         addr += offsetof(struct nlmsgerr, msg);
102         len -= offsetof(struct nlmsgerr, msg);
103
104         if (len) {
105                 tprints(", msg=");
106                 if (fetch_nlmsghdr(tcp, &err.msg, addr, len)) {
107                         decode_nlmsghdr_with_payload(tcp, &err.msg, addr, len);
108                 }
109         }
110
111         tprints("}");
112 }
113
114 static void
115 decode_payload(struct tcb *const tcp,
116                const struct nlmsghdr *const nlmsghdr,
117                const kernel_ulong_t addr,
118                const kernel_ulong_t len)
119 {
120         if (nlmsghdr->nlmsg_type == NLMSG_ERROR) {
121                 decode_nlmsgerr(tcp, addr, len);
122                 return;
123         } else if (nlmsghdr->nlmsg_type == NLMSG_DONE && len == sizeof(int)) {
124                 int num;
125
126                 if (!umove_or_printaddr(tcp, addr, &num))
127                         tprintf("%d", num);
128                 return;
129         }
130
131         printstrn(tcp, addr, len);
132 }
133
134 static void
135 decode_nlmsghdr_with_payload(struct tcb *const tcp,
136                              const struct nlmsghdr *const nlmsghdr,
137                              const kernel_ulong_t addr,
138                              const kernel_ulong_t len)
139 {
140         tprints("{");
141
142         print_nlmsghdr(tcp, nlmsghdr);
143
144         unsigned int nlmsg_len =
145                 nlmsghdr->nlmsg_len > len ? len : nlmsghdr->nlmsg_len;
146         if (nlmsg_len > NLMSG_HDRLEN) {
147                 tprints(", ");
148                 decode_payload(tcp, nlmsghdr, addr + NLMSG_HDRLEN,
149                                          nlmsg_len - NLMSG_HDRLEN);
150         }
151
152         tprints("}");
153 }
154
155 void
156 decode_netlink(struct tcb *const tcp, kernel_ulong_t addr, kernel_ulong_t len)
157 {
158         struct nlmsghdr nlmsghdr;
159         bool print_array = false;
160         unsigned int elt;
161
162         for (elt = 0; fetch_nlmsghdr(tcp, &nlmsghdr, addr, len); elt++) {
163                 if (abbrev(tcp) && elt == max_strlen) {
164                         tprints("...");
165                         break;
166                 }
167
168                 unsigned int nlmsg_len = NLMSG_ALIGN(nlmsghdr.nlmsg_len);
169                 kernel_ulong_t next_addr = 0;
170                 kernel_ulong_t next_len = 0;
171
172                 if (nlmsghdr.nlmsg_len >= NLMSG_HDRLEN) {
173                         next_len = (len >= nlmsg_len) ? len - nlmsg_len : 0;
174
175                         if (next_len && addr + nlmsg_len > addr)
176                                 next_addr = addr + nlmsg_len;
177                 }
178
179                 if (!print_array && next_addr) {
180                         tprints("[");
181                         print_array = true;
182                 }
183
184                 decode_nlmsghdr_with_payload(tcp, &nlmsghdr, addr, len);
185
186                 if (!next_addr)
187                         break;
188
189                 tprints(", ");
190                 addr = next_addr;
191                 len = next_len;
192         }
193
194         if (print_array) {
195                 tprints("]");
196         }
197 }