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>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
34 #include <arpa/inet.h>
35 #include <netinet/in.h>
37 #include "xlat/msg_flags.h"
38 #include "xlat/scmvals.h"
39 #include "xlat/ip_cmsg_types.h"
41 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
52 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
53 struct cmsghdr32 *cmsg32;
58 print_scm_rights(struct tcb *tcp, const void *cmsg_data,
59 const size_t data_len)
61 const int *fds = cmsg_data;
62 const char *end = (const char *) cmsg_data + data_len;
65 if (sizeof(*fds) > data_len)
68 tprints(", cmsg_data=[");
69 while ((const char *) fds < end) {
80 print_scm_creds(struct tcb *tcp, const void *cmsg_data,
81 const size_t data_len)
83 const struct ucred *uc = cmsg_data;
85 if (sizeof(*uc) > data_len)
88 tprintf(", cmsg_data={pid=%u, uid=%u, gid=%u}",
89 (unsigned) uc->pid, (unsigned) uc->uid, (unsigned) uc->gid);
93 print_scm_security(struct tcb *tcp, const void *cmsg_data,
94 const size_t data_len)
99 tprints(", cmsg_data=");
100 print_quoted_string(cmsg_data, data_len, 0);
104 print_cmsg_ip_pktinfo(struct tcb *tcp, const void *cmsg_data,
105 const size_t data_len)
107 const struct in_pktinfo *info = cmsg_data;
109 if (sizeof(*info) > data_len)
112 tprints(", cmsg_data={ipi_ifindex=");
113 print_ifindex(info->ipi_ifindex);
114 tprintf(", ipi_spec_dst=inet_addr(\"%s\"), ipi_addr=inet_addr(\"%s\")}",
115 inet_ntoa(info->ipi_spec_dst), inet_ntoa(info->ipi_addr));
119 print_cmsg_ip_ttl(struct tcb *tcp, const void *cmsg_data,
120 const size_t data_len)
122 const unsigned int *ttl = cmsg_data;
124 if (sizeof(*ttl) > data_len)
127 tprintf(", cmsg_data=[%u]", *ttl);
131 print_cmsg_ip_tos(struct tcb *tcp, const void *cmsg_data,
132 const size_t data_len)
134 const uint8_t *tos = cmsg_data;
136 if (sizeof(*tos) > data_len)
139 tprintf(", cmsg_data=[%#x]", *tos);
143 print_cmsg_ip_checksum(struct tcb *tcp, const void *cmsg_data,
144 const size_t data_len)
146 const uint32_t *csum = cmsg_data;
148 if (sizeof(*csum) > data_len)
151 tprintf(", cmsg_data=[%u]", *csum);
155 print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data,
156 const size_t data_len)
158 const unsigned char *opts = cmsg_data;
164 tprints(", cmsg_data=[");
165 for (i = 0; i < data_len; ++i) {
168 tprintf("0x%02x", opts[i]);
174 print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data,
175 const size_t data_len)
185 struct sockaddr_in offender;
188 if (sizeof(*err) > data_len)
191 tprintf(", cmsg_data={ee_errno=%u, ee_origin=%u, ee_type=%u, ee_code=%u"
192 ", ee_info=%u, ee_data=%u, offender=",
193 err->ee_errno, err->ee_origin, err->ee_type,
194 err->ee_code, err->ee_info, err->ee_data);
195 print_sockaddr(tcp, &err->offender, sizeof(err->offender));
200 print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data,
201 const size_t data_len)
203 if (sizeof(struct sockaddr_in) > data_len)
206 tprints(", cmsg_data=");
207 print_sockaddr(tcp, cmsg_data, data_len);
211 print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
212 const void *cmsg_data, const size_t data_len)
214 switch (cmsg_level) {
216 printxval(scmvals, cmsg_type, "SCM_???");
219 print_scm_rights(tcp, cmsg_data, data_len);
221 case SCM_CREDENTIALS:
222 print_scm_creds(tcp, cmsg_data, data_len);
225 print_scm_security(tcp, cmsg_data, data_len);
230 printxval(ip_cmsg_types, cmsg_type, "IP_???");
233 print_cmsg_ip_pktinfo(tcp, cmsg_data, data_len);
236 print_cmsg_ip_ttl(tcp, cmsg_data, data_len);
239 print_cmsg_ip_tos(tcp, cmsg_data, data_len);
243 print_cmsg_ip_opts(tcp, cmsg_data, data_len);
246 print_cmsg_ip_recverr(tcp, cmsg_data, data_len);
249 print_cmsg_ip_origdstaddr(tcp, cmsg_data, data_len);
252 print_cmsg_ip_checksum(tcp, cmsg_data, data_len);
255 print_scm_security(tcp, cmsg_data, data_len);
260 tprintf("%u", cmsg_type);
265 decode_msg_control(struct tcb *tcp, unsigned long addr, size_t len)
267 const size_t cmsg_size =
268 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
269 (current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) :
271 sizeof(struct cmsghdr);
275 tprints(", msg_control=");
277 char *buf = len < cmsg_size ? NULL : malloc(len);
278 if (!buf || umoven(tcp, addr, len, buf) < 0) {
284 union_cmsghdr u = { .ptr = buf };
287 while (len >= cmsg_size) {
289 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
290 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len :
294 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
295 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level :
299 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
300 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type :
306 tprintf("{cmsg_len=%lu, cmsg_level=", (unsigned long) cmsg_len);
307 printxval(socketlayers, cmsg_level, "SOL_???");
308 tprints(", cmsg_type=");
313 print_cmsg_type_data(tcp, cmsg_level, cmsg_type,
314 (const void *) (u.ptr + cmsg_size),
315 cmsg_len > cmsg_size ? cmsg_len - cmsg_size: 0);
318 if (cmsg_len < cmsg_size) {
322 cmsg_len = (cmsg_len + current_wordsize - 1) &
323 (size_t) ~(current_wordsize - 1);
324 if (cmsg_len >= len) {
338 print_msghdr(struct tcb *tcp, struct msghdr *msg, unsigned long data_size)
340 tprints("{msg_name=");
341 decode_sockaddr(tcp, (long)msg->msg_name, msg->msg_namelen);
342 tprintf(", msg_namelen=%d", msg->msg_namelen);
344 tprints(", msg_iov=");
345 tprint_iov_upto(tcp, (unsigned long) msg->msg_iovlen,
346 (unsigned long) msg->msg_iov, IOV_DECODE_STR, data_size);
347 tprintf(", msg_iovlen=%lu", (unsigned long) msg->msg_iovlen);
349 decode_msg_control(tcp, (unsigned long) msg->msg_control,
350 msg->msg_controllen);
351 tprintf(", msg_controllen=%lu", (unsigned long) msg->msg_controllen);
353 tprints(", msg_flags=");
354 printflags(msg_flags, msg->msg_flags, "MSG_???");
359 decode_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
363 if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg))
364 print_msghdr(tcp, &msg, data_size);
370 dumpiov_in_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
374 if (fetch_struct_msghdr(tcp, addr, &msg))
375 dumpiov_upto(tcp, msg.msg_iovlen, (long)msg.msg_iov, data_size);
379 decode_mmsghdr(struct tcb *tcp, long addr, bool use_msg_len)
382 int fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
385 tprints("{msg_hdr=");
386 print_msghdr(tcp, &mmsg.msg_hdr, use_msg_len ? mmsg.msg_len : -1UL);
387 tprintf(", msg_len=%u}", mmsg.msg_len);
396 decode_mmsgvec(struct tcb *tcp, unsigned long addr, unsigned int len,
402 unsigned int i, fetched;
405 for (i = 0; i < len; ++i, addr += fetched) {
408 fetched = decode_mmsghdr(tcp, addr, use_msg_len);
417 dumpiov_in_mmsghdr(struct tcb *tcp, long addr)
419 unsigned int len = tcp->u_rval;
420 unsigned int i, fetched;
423 for (i = 0; i < len; ++i, addr += fetched) {
424 fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
427 tprintf(" = %lu buffers in vector %u\n",
428 (unsigned long)mmsg.msg_hdr.msg_iovlen, i);
429 dumpiov_upto(tcp, mmsg.msg_hdr.msg_iovlen,
430 (long)mmsg.msg_hdr.msg_iov, mmsg.msg_len);