2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-2000 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 2005-2016 Dmitry V. Levin <ldv@altlinux.org>
7 * Copyright (c) 2016-2017 The strace developers.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "print_fields.h"
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
40 #include "xlat/msg_flags.h"
41 #include "xlat/scmvals.h"
42 #include "xlat/ip_cmsg_types.h"
44 #ifndef current_wordsize
55 #ifndef current_wordsize
56 struct cmsghdr32 *cmsg32;
61 print_scm_rights(struct tcb *tcp, const void *cmsg_data,
62 const unsigned int data_len)
64 const int *fds = cmsg_data;
65 const unsigned int nfds = data_len / sizeof(*fds);
70 for (i = 0; i < nfds; ++i) {
73 if (abbrev(tcp) && i >= max_strlen) {
84 print_scm_creds(struct tcb *tcp, const void *cmsg_data,
85 const unsigned int data_len)
87 const struct ucred *uc = cmsg_data;
89 PRINT_FIELD_U("{", *uc, pid);
90 PRINT_FIELD_UID(", ", *uc, uid);
91 PRINT_FIELD_UID(", ", *uc, gid);
96 print_scm_security(struct tcb *tcp, const void *cmsg_data,
97 const unsigned int data_len)
99 print_quoted_string(cmsg_data, data_len, 0);
103 print_scm_timestamp(struct tcb *tcp, const void *cmsg_data,
104 const unsigned int data_len)
106 print_struct_timeval_data_size(cmsg_data, data_len);
110 print_scm_timestampns(struct tcb *tcp, const void *cmsg_data,
111 const unsigned int data_len)
113 print_struct_timespec_data_size(cmsg_data, data_len);
117 print_scm_timestamping(struct tcb *tcp, const void *cmsg_data,
118 const unsigned int data_len)
120 print_struct_timespec_array_data_size(cmsg_data, 3, data_len);
124 print_cmsg_ip_pktinfo(struct tcb *tcp, const void *cmsg_data,
125 const unsigned int data_len)
127 const struct in_pktinfo *info = cmsg_data;
129 PRINT_FIELD_IFINDEX("{", *info, ipi_ifindex);
130 PRINT_FIELD_INET4_ADDR(", ", *info, ipi_spec_dst);
131 PRINT_FIELD_INET4_ADDR(", ", *info, ipi_addr);
136 print_cmsg_uint(struct tcb *tcp, const void *cmsg_data,
137 const unsigned int data_len)
139 const unsigned int *p = cmsg_data;
145 print_cmsg_uint8_t(struct tcb *tcp, const void *cmsg_data,
146 const unsigned int data_len)
148 const uint8_t *p = cmsg_data;
150 tprintf("[%#x]", *p);
154 print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data,
155 const unsigned int data_len)
157 const unsigned char *opts = cmsg_data;
161 for (i = 0; i < data_len; ++i) {
164 if (abbrev(tcp) && i >= max_strlen) {
168 tprintf("0x%02x", opts[i]);
181 struct sockaddr_in offender;
185 print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data,
186 const unsigned int data_len)
188 const struct sock_ee *const err = cmsg_data;
190 PRINT_FIELD_U("{", *err, ee_errno);
191 PRINT_FIELD_U(", ", *err, ee_origin);
192 PRINT_FIELD_U(", ", *err, ee_type);
193 PRINT_FIELD_U(", ", *err, ee_code);
194 PRINT_FIELD_U(", ", *err, ee_info);
195 PRINT_FIELD_U(", ", *err, ee_data);
196 PRINT_FIELD_SOCKADDR(", ", *err, offender);
201 print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data,
202 const unsigned int data_len)
204 const unsigned int addr_len =
205 data_len > sizeof(struct sockaddr_storage)
206 ? sizeof(struct sockaddr_storage) : data_len;
208 print_sockaddr(cmsg_data, addr_len);
211 typedef void (* const cmsg_printer)(struct tcb *, const void *, unsigned int);
213 static const struct {
214 const cmsg_printer printer;
215 const unsigned int min_len;
216 } cmsg_socket_printers[] = {
217 [SCM_RIGHTS] = { print_scm_rights, sizeof(int) },
218 [SCM_CREDENTIALS] = { print_scm_creds, sizeof(struct ucred) },
219 [SCM_SECURITY] = { print_scm_security, 1 },
220 [SCM_TIMESTAMP] = { print_scm_timestamp, 1 },
221 [SCM_TIMESTAMPNS] = { print_scm_timestampns, 1 },
222 [SCM_TIMESTAMPING] = { print_scm_timestamping, 1 }
223 }, cmsg_ip_printers[] = {
224 [IP_PKTINFO] = { print_cmsg_ip_pktinfo, sizeof(struct in_pktinfo) },
225 [IP_TTL] = { print_cmsg_uint, sizeof(unsigned int) },
226 [IP_TOS] = { print_cmsg_uint8_t, 1 },
227 [IP_RECVOPTS] = { print_cmsg_ip_opts, 1 },
228 [IP_RETOPTS] = { print_cmsg_ip_opts, 1 },
229 [IP_RECVERR] = { print_cmsg_ip_recverr, sizeof(struct sock_ee) },
230 [IP_ORIGDSTADDR] = { print_cmsg_ip_origdstaddr, sizeof(struct sockaddr_in) },
231 [IP_CHECKSUM] = { print_cmsg_uint, sizeof(unsigned int) },
232 [SCM_SECURITY] = { print_scm_security, 1 }
236 print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
237 const void *cmsg_data, const unsigned int data_len)
239 const unsigned int utype = cmsg_type;
240 switch (cmsg_level) {
242 printxval(scmvals, cmsg_type, "SCM_???");
243 if (utype < ARRAY_SIZE(cmsg_socket_printers)
244 && cmsg_socket_printers[utype].printer
245 && data_len >= cmsg_socket_printers[utype].min_len) {
246 tprints(", cmsg_data=");
247 cmsg_socket_printers[utype].printer(tcp, cmsg_data, data_len);
251 printxval(ip_cmsg_types, cmsg_type, "IP_???");
252 if (utype < ARRAY_SIZE(cmsg_ip_printers)
253 && cmsg_ip_printers[utype].printer
254 && data_len >= cmsg_ip_printers[utype].min_len) {
255 tprints(", cmsg_data=");
256 cmsg_ip_printers[utype].printer(tcp, cmsg_data, data_len);
260 tprintf("%#x", cmsg_type);
265 get_optmem_max(struct tcb *tcp)
267 static int optmem_max;
270 if (read_int_from_file(tcp, "/proc/sys/net/core/optmem_max",
271 &optmem_max) || optmem_max <= 0) {
272 optmem_max = sizeof(long long) * (2 * IOV_MAX + 512);
274 optmem_max = (optmem_max + sizeof(long long) - 1)
275 & ~(sizeof(long long) - 1);
283 decode_msg_control(struct tcb *const tcp, const kernel_ulong_t addr,
284 const kernel_ulong_t in_control_len)
288 tprints(", msg_control=");
290 const unsigned int cmsg_size =
291 #ifndef current_wordsize
292 (current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) :
294 sizeof(struct cmsghdr);
296 unsigned int control_len = in_control_len > get_optmem_max(tcp)
297 ? get_optmem_max(tcp) : in_control_len;
298 unsigned int buf_len = control_len;
299 char *buf = buf_len < cmsg_size ? NULL : malloc(buf_len);
300 if (!buf || umoven(tcp, addr, buf_len, buf) < 0) {
306 union_cmsghdr u = { .ptr = buf };
309 while (buf_len >= cmsg_size) {
310 const kernel_ulong_t cmsg_len =
311 #ifndef current_wordsize
312 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len :
315 const int cmsg_level =
316 #ifndef current_wordsize
317 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level :
320 const int cmsg_type =
321 #ifndef current_wordsize
322 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type :
328 tprintf("{cmsg_len=%" PRI_klu ", cmsg_level=", cmsg_len);
329 printxval(socketlayers, cmsg_level, "SOL_???");
330 tprints(", cmsg_type=");
332 kernel_ulong_t len = cmsg_len > buf_len ? buf_len : cmsg_len;
334 print_cmsg_type_data(tcp, cmsg_level, cmsg_type,
335 (const void *) (u.ptr + cmsg_size),
336 len > cmsg_size ? len - cmsg_size : 0);
339 if (len < cmsg_size) {
340 buf_len -= cmsg_size;
343 len = (cmsg_len + current_wordsize - 1) &
344 ~((kernel_ulong_t) current_wordsize - 1);
345 if (len >= buf_len) {
354 printaddr(addr + (control_len - buf_len));
355 } else if (control_len < in_control_len) {
363 print_struct_msghdr(struct tcb *tcp, const struct msghdr *msg,
364 const int *const p_user_msg_namelen,
365 const kernel_ulong_t data_size)
367 const int msg_namelen =
368 p_user_msg_namelen && (int) msg->msg_namelen > *p_user_msg_namelen
369 ? *p_user_msg_namelen : (int) msg->msg_namelen;
371 tprints("{msg_name=");
373 decode_sockaddr(tcp, ptr_to_kulong(msg->msg_name), msg_namelen);
374 const enum iov_decode decode =
375 (family == AF_NETLINK) ? IOV_DECODE_NETLINK : IOV_DECODE_STR;
377 tprints(", msg_namelen=");
378 if (p_user_msg_namelen && *p_user_msg_namelen != (int) msg->msg_namelen)
379 tprintf("%d->", *p_user_msg_namelen);
380 tprintf("%d", msg->msg_namelen);
382 tprints(", msg_iov=");
383 tprint_iov_upto(tcp, msg->msg_iovlen,
384 ptr_to_kulong(msg->msg_iov), decode, data_size);
385 PRINT_FIELD_U(", ", *msg, msg_iovlen);
387 decode_msg_control(tcp, ptr_to_kulong(msg->msg_control),
388 msg->msg_controllen);
389 PRINT_FIELD_U(", ", *msg, msg_controllen);
391 PRINT_FIELD_FLAGS(", ", *msg, msg_flags, msg_flags, "MSG_???");
396 fetch_msghdr_namelen(struct tcb *const tcp, const kernel_ulong_t addr,
397 int *const p_msg_namelen)
401 if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg)) {
402 *p_msg_namelen = msg.msg_namelen;
410 decode_msghdr(struct tcb *const tcp, const int *const p_user_msg_namelen,
411 const kernel_ulong_t addr, const kernel_ulong_t data_size)
415 if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg))
416 print_struct_msghdr(tcp, &msg, p_user_msg_namelen, data_size);
422 dumpiov_in_msghdr(struct tcb *const tcp, const kernel_ulong_t addr,
423 const kernel_ulong_t data_size)
427 if (fetch_struct_msghdr(tcp, addr, &msg)) {
428 dumpiov_upto(tcp, msg.msg_iovlen,
429 ptr_to_kulong(msg.msg_iov), data_size);
435 printfd(tcp, tcp->u_arg[0]);
437 decode_msghdr(tcp, 0, tcp->u_arg[1], -1);
440 printflags(msg_flags, tcp->u_arg[2], "MSG_???");
450 printfd(tcp, tcp->u_arg[0]);
452 if (fetch_msghdr_namelen(tcp, tcp->u_arg[1], &msg_namelen)) {
453 set_tcb_priv_ulong(tcp, msg_namelen);
456 printaddr(tcp->u_arg[1]);
458 msg_namelen = get_tcb_priv_ulong(tcp);
461 tprintf("{msg_namelen=%d}", msg_namelen);
463 decode_msghdr(tcp, &msg_namelen, tcp->u_arg[1],
469 printflags(msg_flags, tcp->u_arg[2], "MSG_???");