]> granicus.if.org Git - strace/blob - netlink.c
netlink: add decoding of NETLINK_XFRM message types
[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/audit.h>
33 #include <linux/netlink.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/xfrm.h>
36 #include "xlat/netlink_flags.h"
37 #include "xlat/netlink_protocols.h"
38 #include "xlat/netlink_types.h"
39 #include "xlat/nl_audit_types.h"
40 #include "xlat/nl_route_types.h"
41 #include "xlat/nl_selinux_types.h"
42 #include "xlat/nl_sock_diag_types.h"
43 #include "xlat/nl_xfrm_types.h"
44
45 #undef NLMSG_HDRLEN
46 #define NLMSG_HDRLEN NLMSG_ALIGN(sizeof(struct nlmsghdr))
47
48 /*
49  * Fetch a struct nlmsghdr from the given address.
50  */
51 static bool
52 fetch_nlmsghdr(struct tcb *const tcp, struct nlmsghdr *const nlmsghdr,
53                const kernel_ulong_t addr, const kernel_ulong_t len)
54 {
55         if (len < sizeof(struct nlmsghdr)) {
56                 printstrn(tcp, addr, len);
57                 return false;
58         }
59
60         if (umove_or_printaddr(tcp, addr, nlmsghdr))
61                 return false;
62
63         return true;
64 }
65
66 enum {
67         NL_FAMILY_ERROR = -1,
68         NL_FAMILY_DEFAULT = -2
69 };
70
71 static int
72 get_fd_nl_family(struct tcb *const tcp, const int fd)
73 {
74         const unsigned long inode = getfdinode(tcp, fd);
75         if (!inode)
76                 return NL_FAMILY_ERROR;
77
78         const char *const details = get_sockaddr_by_inode(tcp, fd, inode);
79         if (!details)
80                 return NL_FAMILY_ERROR;
81
82         const char *const nl_details = STR_STRIP_PREFIX(details, "NETLINK:[");
83         if (nl_details == details)
84                 return NL_FAMILY_ERROR;
85
86         const struct xlat *xlats = netlink_protocols;
87         for (; xlats->str; ++xlats) {
88                 const char *name = STR_STRIP_PREFIX(xlats->str, "NETLINK_");
89                 if (!strncmp(nl_details, name, strlen(name)))
90                         return xlats->val;
91         }
92
93         if (*nl_details >= '0' && *nl_details <= '9')
94                 return atoi(nl_details);
95
96         return NL_FAMILY_ERROR;
97 }
98
99 static const struct {
100         const struct xlat *const xlat;
101         const char *const dflt;
102 } nlmsg_types[] = {
103         [NETLINK_AUDIT] = { nl_audit_types, "AUDIT_???" },
104         [NETLINK_ROUTE] = { nl_route_types, "RTM_???" },
105         [NETLINK_SELINUX] = { nl_selinux_types, "SELNL_MSG_???" },
106         [NETLINK_SOCK_DIAG] = { nl_sock_diag_types, "SOCK_DIAG_???" },
107         [NETLINK_XFRM] = { nl_xfrm_types, "XFRM_MSG_???" }
108 };
109
110 /*
111  * As all valid netlink families are positive integers, use unsigned int
112  * for family here to filter out NL_FAMILY_ERROR and NL_FAMILY_DEFAULT.
113  */
114 static void
115 decode_nlmsg_type(const uint16_t type, const unsigned int family)
116 {
117         if (family < ARRAY_SIZE(nlmsg_types)
118             && nlmsg_types[family].xlat) {
119                 printxval(nlmsg_types[family].xlat, type, nlmsg_types[family].dflt);
120         } else {
121                 printxval(netlink_types, type, "NLMSG_???");
122         }
123 }
124
125 static int
126 print_nlmsghdr(struct tcb *tcp,
127                const int fd,
128                int family,
129                const struct nlmsghdr *const nlmsghdr)
130 {
131         /* print the whole structure regardless of its nlmsg_len */
132
133         tprintf("{len=%u, type=", nlmsghdr->nlmsg_len);
134
135         const int hdr_family = (nlmsghdr->nlmsg_type < NLMSG_MIN_TYPE)
136                                ? NL_FAMILY_DEFAULT
137                                : (family != NL_FAMILY_DEFAULT
138                                   ? family : get_fd_nl_family(tcp, fd));
139
140         decode_nlmsg_type(nlmsghdr->nlmsg_type, hdr_family);
141
142         tprints(", flags=");
143         printflags(netlink_flags, nlmsghdr->nlmsg_flags, "NLM_F_???");
144
145         tprintf(", seq=%u, pid=%u}", nlmsghdr->nlmsg_seq,
146                 nlmsghdr->nlmsg_pid);
147
148         return family != NL_FAMILY_DEFAULT ? family : hdr_family;
149 }
150
151 static void
152 decode_nlmsghdr_with_payload(struct tcb *const tcp,
153                              const int fd,
154                              int family,
155                              const struct nlmsghdr *const nlmsghdr,
156                              const kernel_ulong_t addr,
157                              const kernel_ulong_t len);
158
159 static void
160 decode_nlmsgerr(struct tcb *const tcp,
161                 const int fd,
162                 const int family,
163                 kernel_ulong_t addr,
164                 kernel_ulong_t len)
165 {
166         struct nlmsgerr err;
167
168         if (len < sizeof(err.error)) {
169                 printstrn(tcp, addr, len);
170                 return;
171         }
172
173         if (umove_or_printaddr(tcp, addr, &err.error))
174                 return;
175
176         tprints("{error=");
177         if (err.error < 0 && (unsigned) -err.error < nerrnos) {
178                 tprintf("-%s", errnoent[-err.error]);
179         } else {
180                 tprintf("%d", err.error);
181         }
182
183         addr += offsetof(struct nlmsgerr, msg);
184         len -= offsetof(struct nlmsgerr, msg);
185
186         if (len) {
187                 tprints(", msg=");
188                 if (fetch_nlmsghdr(tcp, &err.msg, addr, len)) {
189                         decode_nlmsghdr_with_payload(tcp, fd, family,
190                                                      &err.msg, addr, len);
191                 }
192         }
193
194         tprints("}");
195 }
196
197 static void
198 decode_payload(struct tcb *const tcp,
199                const int fd,
200                const int family,
201                const struct nlmsghdr *const nlmsghdr,
202                const kernel_ulong_t addr,
203                const kernel_ulong_t len)
204 {
205         if (nlmsghdr->nlmsg_type == NLMSG_ERROR) {
206                 decode_nlmsgerr(tcp, fd, family, addr, len);
207                 return;
208         } else if (nlmsghdr->nlmsg_type == NLMSG_DONE && len == sizeof(int)) {
209                 int num;
210
211                 if (!umove_or_printaddr(tcp, addr, &num))
212                         tprintf("%d", num);
213                 return;
214         }
215
216         printstrn(tcp, addr, len);
217 }
218
219 static void
220 decode_nlmsghdr_with_payload(struct tcb *const tcp,
221                              const int fd,
222                              int family,
223                              const struct nlmsghdr *const nlmsghdr,
224                              const kernel_ulong_t addr,
225                              const kernel_ulong_t len)
226 {
227         tprints("{");
228
229         family = print_nlmsghdr(tcp, fd, family, nlmsghdr);
230
231         unsigned int nlmsg_len =
232                 nlmsghdr->nlmsg_len > len ? len : nlmsghdr->nlmsg_len;
233         if (nlmsg_len > NLMSG_HDRLEN) {
234                 tprints(", ");
235                 decode_payload(tcp, fd, family, nlmsghdr, addr + NLMSG_HDRLEN,
236                                                      nlmsg_len - NLMSG_HDRLEN);
237         }
238
239         tprints("}");
240 }
241
242 void
243 decode_netlink(struct tcb *const tcp,
244                const int fd,
245                kernel_ulong_t addr,
246                kernel_ulong_t len)
247 {
248         struct nlmsghdr nlmsghdr;
249         bool print_array = false;
250         unsigned int elt;
251
252         for (elt = 0; fetch_nlmsghdr(tcp, &nlmsghdr, addr, len); elt++) {
253                 if (abbrev(tcp) && elt == max_strlen) {
254                         tprints("...");
255                         break;
256                 }
257
258                 unsigned int nlmsg_len = NLMSG_ALIGN(nlmsghdr.nlmsg_len);
259                 kernel_ulong_t next_addr = 0;
260                 kernel_ulong_t next_len = 0;
261
262                 if (nlmsghdr.nlmsg_len >= NLMSG_HDRLEN) {
263                         next_len = (len >= nlmsg_len) ? len - nlmsg_len : 0;
264
265                         if (next_len && addr + nlmsg_len > addr)
266                                 next_addr = addr + nlmsg_len;
267                 }
268
269                 if (!print_array && next_addr) {
270                         tprints("[");
271                         print_array = true;
272                 }
273
274                 decode_nlmsghdr_with_payload(tcp, fd, NL_FAMILY_DEFAULT,
275                                              &nlmsghdr, addr, len);
276
277                 if (!next_addr)
278                         break;
279
280                 tprints(", ");
281                 addr = next_addr;
282                 len = next_len;
283         }
284
285         if (print_array) {
286                 tprints("]");
287         }
288 }