blob: 0f9ba7145dd885aeec9de424ed7a6b56569c74e0 [file] [log] [blame]
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +00001/*
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 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "defs.h"
33#include "msghdr.h"
34#include <arpa/inet.h>
35#include <netinet/in.h>
36
37#include "xlat/msg_flags.h"
38#include "xlat/scmvals.h"
39#include "xlat/ip_cmsg_types.h"
40
41#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
42struct cmsghdr32 {
43 uint32_t cmsg_len;
44 int cmsg_level;
45 int cmsg_type;
46};
47#endif
48
49typedef union {
50 char *ptr;
51 struct cmsghdr *cmsg;
52#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
53 struct cmsghdr32 *cmsg32;
54#endif
55} union_cmsghdr;
56
57static void
Dmitry V. Levinb7937bd2016-06-30 22:39:02 +000058print_scm_rights(struct tcb *tcp, const void *cmsg_data, const size_t data_len)
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +000059{
60 const int *fds = cmsg_data;
Dmitry V. Levin029d9792016-06-30 22:20:56 +000061 const size_t nfds = data_len / sizeof(*fds);
62 size_t i;
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +000063
Dmitry V. Levindf57a9b2016-06-30 22:49:36 +000064 tprints("[");
Dmitry V. Levin029d9792016-06-30 22:20:56 +000065
66 for (i = 0; i < nfds; ++i) {
67 if (i)
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +000068 tprints(", ");
Dmitry V. Levind35d5ec2016-07-02 21:14:26 +000069 if (abbrev(tcp) && i >= max_strlen) {
70 tprints("...");
71 break;
72 }
Dmitry V. Levin029d9792016-06-30 22:20:56 +000073 printfd(tcp, fds[i]);
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +000074 }
Dmitry V. Levin029d9792016-06-30 22:20:56 +000075
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +000076 tprints("]");
77}
78
79static void
Dmitry V. Levinb7937bd2016-06-30 22:39:02 +000080print_scm_creds(struct tcb *tcp, const void *cmsg_data, const size_t data_len)
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +000081{
82 const struct ucred *uc = cmsg_data;
83
Dmitry V. Levindf57a9b2016-06-30 22:49:36 +000084 tprintf("{pid=%u, uid=%u, gid=%u}",
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +000085 (unsigned) uc->pid, (unsigned) uc->uid, (unsigned) uc->gid);
86}
87
88static void
89print_scm_security(struct tcb *tcp, const void *cmsg_data,
90 const size_t data_len)
91{
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +000092 print_quoted_string(cmsg_data, data_len, 0);
93}
94
95static void
96print_cmsg_ip_pktinfo(struct tcb *tcp, const void *cmsg_data,
97 const size_t data_len)
98{
99 const struct in_pktinfo *info = cmsg_data;
100
Dmitry V. Levindf57a9b2016-06-30 22:49:36 +0000101 tprints("{ipi_ifindex=");
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000102 print_ifindex(info->ipi_ifindex);
Dmitry V. Levindb0e6e12016-06-29 22:07:20 +0000103 tprintf(", ipi_spec_dst=inet_addr(\"%s\")",
104 inet_ntoa(info->ipi_spec_dst));
105 tprintf(", ipi_addr=inet_addr(\"%s\")}",
106 inet_ntoa(info->ipi_addr));
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000107}
108
109static void
Dmitry V. Levinb7937bd2016-06-30 22:39:02 +0000110print_cmsg_uint(struct tcb *tcp, const void *cmsg_data, const size_t data_len)
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000111{
Dmitry V. Levinb7937bd2016-06-30 22:39:02 +0000112 const unsigned int *p = cmsg_data;
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000113
Dmitry V. Levindf57a9b2016-06-30 22:49:36 +0000114 tprintf("[%u]", *p);
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000115}
116
117static void
Dmitry V. Levindf57a9b2016-06-30 22:49:36 +0000118print_cmsg_uint8_t(struct tcb *tcp, const void *cmsg_data,
119 const size_t data_len)
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000120{
Dmitry V. Levindf57a9b2016-06-30 22:49:36 +0000121 const uint8_t *p = cmsg_data;
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000122
Dmitry V. Levindf57a9b2016-06-30 22:49:36 +0000123 tprintf("[%#x]", *p);
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000124}
125
126static void
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000127print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data,
128 const size_t data_len)
129{
130 const unsigned char *opts = cmsg_data;
131 size_t i;
132
Dmitry V. Levindf57a9b2016-06-30 22:49:36 +0000133 tprints("[");
Dmitry V. Levin2a897372016-06-28 22:25:10 +0000134 for (i = 0; i < data_len; ++i) {
135 if (i)
136 tprints(", ");
137 tprintf("0x%02x", opts[i]);
138 }
139 tprints("]");
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000140}
141
Dmitry V. Levindf57a9b2016-06-30 22:49:36 +0000142struct sock_ee {
143 uint32_t ee_errno;
144 uint8_t ee_origin;
145 uint8_t ee_type;
146 uint8_t ee_code;
147 uint8_t ee_pad;
148 uint32_t ee_info;
149 uint32_t ee_data;
150 struct sockaddr_in offender;
151};
152
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000153static void
154print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data,
155 const size_t data_len)
156{
Dmitry V. Levindf57a9b2016-06-30 22:49:36 +0000157 const struct sock_ee *const err = cmsg_data;
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000158
Dmitry V. Levindf57a9b2016-06-30 22:49:36 +0000159 tprintf("{ee_errno=%u, ee_origin=%u, ee_type=%u, ee_code=%u"
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000160 ", ee_info=%u, ee_data=%u, offender=",
161 err->ee_errno, err->ee_origin, err->ee_type,
162 err->ee_code, err->ee_info, err->ee_data);
163 print_sockaddr(tcp, &err->offender, sizeof(err->offender));
164 tprints("}");
165}
166
167static void
168print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data,
169 const size_t data_len)
170{
Dmitry V. Levindfeea692016-06-30 22:26:35 +0000171 const int addr_len =
172 data_len > sizeof(struct sockaddr_storage)
173 ? sizeof(struct sockaddr_storage) : data_len;
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000174
Dmitry V. Levindfeea692016-06-30 22:26:35 +0000175 print_sockaddr(tcp, cmsg_data, addr_len);
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000176}
177
Dmitry V. Levindf57a9b2016-06-30 22:49:36 +0000178typedef void (* const cmsg_printer)(struct tcb *, const void *, size_t);
179
180static const struct {
181 const cmsg_printer printer;
182 const size_t min_len;
183} cmsg_socket_printers[] = {
184 [SCM_RIGHTS] = { print_scm_rights, sizeof(int) },
185 [SCM_CREDENTIALS] = { print_scm_creds, sizeof(struct ucred) },
186 [SCM_SECURITY] = { print_scm_security, 1 }
187}, cmsg_ip_printers[] = {
188 [IP_PKTINFO] = { print_cmsg_ip_pktinfo, sizeof(struct in_pktinfo) },
189 [IP_TTL] = { print_cmsg_uint, sizeof(unsigned int) },
190 [IP_TOS] = { print_cmsg_uint8_t, 1 },
191 [IP_RECVOPTS] = { print_cmsg_ip_opts, 1 },
192 [IP_RETOPTS] = { print_cmsg_ip_opts, 1 },
193 [IP_RECVERR] = { print_cmsg_ip_recverr, sizeof(struct sock_ee) },
194 [IP_ORIGDSTADDR] = { print_cmsg_ip_origdstaddr, sizeof(struct sockaddr_in) },
195 [IP_CHECKSUM] = { print_cmsg_uint, sizeof(unsigned int) },
196 [SCM_SECURITY] = { print_scm_security, 1 }
197};
198
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000199static void
200print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
201 const void *cmsg_data, const size_t data_len)
202{
Dmitry V. Levindf57a9b2016-06-30 22:49:36 +0000203 const unsigned int utype = cmsg_type;
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000204 switch (cmsg_level) {
205 case SOL_SOCKET:
206 printxval(scmvals, cmsg_type, "SCM_???");
Dmitry V. Levindf57a9b2016-06-30 22:49:36 +0000207 if (utype < ARRAY_SIZE(cmsg_socket_printers)
208 && cmsg_socket_printers[utype].printer
209 && data_len >= cmsg_socket_printers[utype].min_len) {
210 tprints(", cmsg_data=");
211 cmsg_socket_printers[utype].printer(tcp, cmsg_data, data_len);
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000212 }
213 break;
214 case SOL_IP:
215 printxval(ip_cmsg_types, cmsg_type, "IP_???");
Dmitry V. Levindf57a9b2016-06-30 22:49:36 +0000216 if (utype < ARRAY_SIZE(cmsg_ip_printers)
217 && cmsg_ip_printers[utype].printer
218 && data_len >= cmsg_ip_printers[utype].min_len) {
219 tprints(", cmsg_data=");
220 cmsg_ip_printers[utype].printer(tcp, cmsg_data, data_len);
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000221 }
222 break;
223 default:
Dmitry V. Levin6c30aec2016-06-30 22:14:51 +0000224 tprintf("%#x", cmsg_type);
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000225 }
226}
227
228static void
Dmitry V. Levin1d532102016-06-30 22:34:27 +0000229decode_msg_control(struct tcb *tcp, unsigned long addr, const size_t control_len)
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000230{
Dmitry V. Levin1d532102016-06-30 22:34:27 +0000231 if (!control_len)
232 return;
233 tprints(", msg_control=");
234
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000235 const size_t cmsg_size =
236#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
237 (current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) :
238#endif
239 sizeof(struct cmsghdr);
240
Dmitry V. Levin1d532102016-06-30 22:34:27 +0000241 size_t buf_len = control_len;
242 char *buf = buf_len < cmsg_size ? NULL : malloc(buf_len);
243 if (!buf || umoven(tcp, addr, buf_len, buf) < 0) {
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000244 printaddr(addr);
245 free(buf);
246 return;
247 }
248
249 union_cmsghdr u = { .ptr = buf };
250
251 tprints("[");
Dmitry V. Levin1d532102016-06-30 22:34:27 +0000252 while (buf_len >= cmsg_size) {
253 const size_t cmsg_len =
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000254#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
255 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len :
256#endif
257 u.cmsg->cmsg_len;
Dmitry V. Levin1d532102016-06-30 22:34:27 +0000258 const int cmsg_level =
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000259#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
260 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level :
261#endif
262 u.cmsg->cmsg_level;
Dmitry V. Levin1d532102016-06-30 22:34:27 +0000263 const int cmsg_type =
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000264#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
265 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type :
266#endif
267 u.cmsg->cmsg_type;
268
269 if (u.ptr != buf)
270 tprints(", ");
271 tprintf("{cmsg_len=%lu, cmsg_level=", (unsigned long) cmsg_len);
272 printxval(socketlayers, cmsg_level, "SOL_???");
273 tprints(", cmsg_type=");
274
Dmitry V. Levin1d532102016-06-30 22:34:27 +0000275 size_t len = cmsg_len > buf_len ? buf_len : cmsg_len;
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000276
277 print_cmsg_type_data(tcp, cmsg_level, cmsg_type,
278 (const void *) (u.ptr + cmsg_size),
Dmitry V. Levin1d532102016-06-30 22:34:27 +0000279 len > cmsg_size ? len - cmsg_size: 0);
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000280 tprints("}");
281
Dmitry V. Levin1d532102016-06-30 22:34:27 +0000282 if (len < cmsg_size) {
283 buf_len -= cmsg_size;
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000284 break;
285 }
Dmitry V. Levin1d532102016-06-30 22:34:27 +0000286 len = (cmsg_len + current_wordsize - 1) &
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000287 (size_t) ~(current_wordsize - 1);
Dmitry V. Levin1d532102016-06-30 22:34:27 +0000288 if (len >= buf_len) {
289 buf_len = 0;
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000290 break;
291 }
Dmitry V. Levin1d532102016-06-30 22:34:27 +0000292 u.ptr += len;
293 buf_len -= len;
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000294 }
Dmitry V. Levin1d532102016-06-30 22:34:27 +0000295 if (buf_len) {
296 tprints(", ");
297 printaddr(addr + (control_len - buf_len));
298 }
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000299 tprints("]");
300 free(buf);
301}
302
303static void
304print_msghdr(struct tcb *tcp, struct msghdr *msg, unsigned long data_size)
305{
306 tprints("{msg_name=");
307 decode_sockaddr(tcp, (long)msg->msg_name, msg->msg_namelen);
308 tprintf(", msg_namelen=%d", msg->msg_namelen);
309
310 tprints(", msg_iov=");
311 tprint_iov_upto(tcp, (unsigned long) msg->msg_iovlen,
312 (unsigned long) msg->msg_iov, IOV_DECODE_STR, data_size);
313 tprintf(", msg_iovlen=%lu", (unsigned long) msg->msg_iovlen);
314
315 decode_msg_control(tcp, (unsigned long) msg->msg_control,
316 msg->msg_controllen);
317 tprintf(", msg_controllen=%lu", (unsigned long) msg->msg_controllen);
318
319 tprints(", msg_flags=");
320 printflags(msg_flags, msg->msg_flags, "MSG_???");
321 tprints("}");
322}
323
324void
325decode_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
326{
327 struct msghdr msg;
328
329 if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg))
330 print_msghdr(tcp, &msg, data_size);
331 else
332 printaddr(addr);
333}
334
335void
336dumpiov_in_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
337{
338 struct msghdr msg;
339
340 if (fetch_struct_msghdr(tcp, addr, &msg))
341 dumpiov_upto(tcp, msg.msg_iovlen, (long)msg.msg_iov, data_size);
342}
343
344static int
345decode_mmsghdr(struct tcb *tcp, long addr, bool use_msg_len)
346{
347 struct mmsghdr mmsg;
348 int fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
349
350 if (fetched) {
Dmitry V. Levina50ec342016-06-27 00:14:34 +0000351 tprints("{msg_hdr=");
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000352 print_msghdr(tcp, &mmsg.msg_hdr, use_msg_len ? mmsg.msg_len : -1UL);
Dmitry V. Levina50ec342016-06-27 00:14:34 +0000353 tprintf(", msg_len=%u}", mmsg.msg_len);
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000354 } else {
355 printaddr(addr);
356 }
357
358 return fetched;
359}
360
361void
362decode_mmsgvec(struct tcb *tcp, unsigned long addr, unsigned int len,
363 bool use_msg_len)
364{
365 if (syserror(tcp)) {
366 printaddr(addr);
367 } else {
368 unsigned int i, fetched;
369
370 tprints("[");
371 for (i = 0; i < len; ++i, addr += fetched) {
372 if (i)
373 tprints(", ");
374 fetched = decode_mmsghdr(tcp, addr, use_msg_len);
375 if (!fetched)
376 break;
377 }
378 tprints("]");
379 }
380}
381
382void
383dumpiov_in_mmsghdr(struct tcb *tcp, long addr)
384{
385 unsigned int len = tcp->u_rval;
386 unsigned int i, fetched;
387 struct mmsghdr mmsg;
388
389 for (i = 0; i < len; ++i, addr += fetched) {
390 fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
391 if (!fetched)
392 break;
393 tprintf(" = %lu buffers in vector %u\n",
394 (unsigned long)mmsg.msg_hdr.msg_iovlen, i);
395 dumpiov_upto(tcp, mmsg.msg_hdr.msg_iovlen,
396 (long)mmsg.msg_hdr.msg_iov, mmsg.msg_len);
397 }
398}