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 size_t nfds = data_len / sizeof(*fds);
68 tprints(", cmsg_data=[");
70 for (i = 0; i < nfds; ++i) {
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\")",
115 inet_ntoa(info->ipi_spec_dst));
116 tprintf(", ipi_addr=inet_addr(\"%s\")}",
117 inet_ntoa(info->ipi_addr));
121 print_cmsg_ip_ttl(struct tcb *tcp, const void *cmsg_data,
122 const size_t data_len)
124 const unsigned int *ttl = cmsg_data;
126 if (sizeof(*ttl) > data_len)
129 tprintf(", cmsg_data=[%u]", *ttl);
133 print_cmsg_ip_tos(struct tcb *tcp, const void *cmsg_data,
134 const size_t data_len)
136 const uint8_t *tos = cmsg_data;
138 if (sizeof(*tos) > data_len)
141 tprintf(", cmsg_data=[%#x]", *tos);
145 print_cmsg_ip_checksum(struct tcb *tcp, const void *cmsg_data,
146 const size_t data_len)
148 const uint32_t *csum = cmsg_data;
150 if (sizeof(*csum) > data_len)
153 tprintf(", cmsg_data=[%u]", *csum);
157 print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data,
158 const size_t data_len)
160 const unsigned char *opts = cmsg_data;
166 tprints(", cmsg_data=[");
167 for (i = 0; i < data_len; ++i) {
170 tprintf("0x%02x", opts[i]);
176 print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data,
177 const size_t data_len)
187 struct sockaddr_in offender;
190 if (sizeof(*err) > data_len)
193 tprintf(", cmsg_data={ee_errno=%u, ee_origin=%u, ee_type=%u, ee_code=%u"
194 ", ee_info=%u, ee_data=%u, offender=",
195 err->ee_errno, err->ee_origin, err->ee_type,
196 err->ee_code, err->ee_info, err->ee_data);
197 print_sockaddr(tcp, &err->offender, sizeof(err->offender));
202 print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data,
203 const size_t data_len)
206 data_len > sizeof(struct sockaddr_storage)
207 ? sizeof(struct sockaddr_storage) : data_len;
209 tprints(", cmsg_data=");
210 print_sockaddr(tcp, cmsg_data, addr_len);
214 print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
215 const void *cmsg_data, const size_t data_len)
217 switch (cmsg_level) {
219 printxval(scmvals, cmsg_type, "SCM_???");
222 print_scm_rights(tcp, cmsg_data, data_len);
224 case SCM_CREDENTIALS:
225 print_scm_creds(tcp, cmsg_data, data_len);
228 print_scm_security(tcp, cmsg_data, data_len);
233 printxval(ip_cmsg_types, cmsg_type, "IP_???");
236 print_cmsg_ip_pktinfo(tcp, cmsg_data, data_len);
239 print_cmsg_ip_ttl(tcp, cmsg_data, data_len);
242 print_cmsg_ip_tos(tcp, cmsg_data, data_len);
246 print_cmsg_ip_opts(tcp, cmsg_data, data_len);
249 print_cmsg_ip_recverr(tcp, cmsg_data, data_len);
252 print_cmsg_ip_origdstaddr(tcp, cmsg_data, data_len);
255 print_cmsg_ip_checksum(tcp, cmsg_data, data_len);
258 print_scm_security(tcp, cmsg_data, data_len);
263 tprintf("%#x", cmsg_type);
268 decode_msg_control(struct tcb *tcp, unsigned long addr, const size_t control_len)
272 tprints(", msg_control=");
274 const size_t cmsg_size =
275 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
276 (current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) :
278 sizeof(struct cmsghdr);
280 size_t buf_len = control_len;
281 char *buf = buf_len < cmsg_size ? NULL : malloc(buf_len);
282 if (!buf || umoven(tcp, addr, buf_len, buf) < 0) {
288 union_cmsghdr u = { .ptr = buf };
291 while (buf_len >= cmsg_size) {
292 const size_t cmsg_len =
293 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
294 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len :
297 const int cmsg_level =
298 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
299 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level :
302 const int cmsg_type =
303 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
304 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type :
310 tprintf("{cmsg_len=%lu, cmsg_level=", (unsigned long) cmsg_len);
311 printxval(socketlayers, cmsg_level, "SOL_???");
312 tprints(", cmsg_type=");
314 size_t len = cmsg_len > buf_len ? buf_len : cmsg_len;
316 print_cmsg_type_data(tcp, cmsg_level, cmsg_type,
317 (const void *) (u.ptr + cmsg_size),
318 len > cmsg_size ? len - cmsg_size: 0);
321 if (len < cmsg_size) {
322 buf_len -= cmsg_size;
325 len = (cmsg_len + current_wordsize - 1) &
326 (size_t) ~(current_wordsize - 1);
327 if (len >= buf_len) {
336 printaddr(addr + (control_len - buf_len));
343 print_msghdr(struct tcb *tcp, struct msghdr *msg, unsigned long data_size)
345 tprints("{msg_name=");
346 decode_sockaddr(tcp, (long)msg->msg_name, msg->msg_namelen);
347 tprintf(", msg_namelen=%d", msg->msg_namelen);
349 tprints(", msg_iov=");
350 tprint_iov_upto(tcp, (unsigned long) msg->msg_iovlen,
351 (unsigned long) msg->msg_iov, IOV_DECODE_STR, data_size);
352 tprintf(", msg_iovlen=%lu", (unsigned long) msg->msg_iovlen);
354 decode_msg_control(tcp, (unsigned long) msg->msg_control,
355 msg->msg_controllen);
356 tprintf(", msg_controllen=%lu", (unsigned long) msg->msg_controllen);
358 tprints(", msg_flags=");
359 printflags(msg_flags, msg->msg_flags, "MSG_???");
364 decode_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
368 if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg))
369 print_msghdr(tcp, &msg, data_size);
375 dumpiov_in_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
379 if (fetch_struct_msghdr(tcp, addr, &msg))
380 dumpiov_upto(tcp, msg.msg_iovlen, (long)msg.msg_iov, data_size);
384 decode_mmsghdr(struct tcb *tcp, long addr, bool use_msg_len)
387 int fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
390 tprints("{msg_hdr=");
391 print_msghdr(tcp, &mmsg.msg_hdr, use_msg_len ? mmsg.msg_len : -1UL);
392 tprintf(", msg_len=%u}", mmsg.msg_len);
401 decode_mmsgvec(struct tcb *tcp, unsigned long addr, unsigned int len,
407 unsigned int i, fetched;
410 for (i = 0; i < len; ++i, addr += fetched) {
413 fetched = decode_mmsghdr(tcp, addr, use_msg_len);
422 dumpiov_in_mmsghdr(struct tcb *tcp, long addr)
424 unsigned int len = tcp->u_rval;
425 unsigned int i, fetched;
428 for (i = 0; i < len; ++i, addr += fetched) {
429 fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
432 tprintf(" = %lu buffers in vector %u\n",
433 (unsigned long)mmsg.msg_hdr.msg_iovlen, i);
434 dumpiov_upto(tcp, mmsg.msg_hdr.msg_iovlen,
435 (long)mmsg.msg_hdr.msg_iov, mmsg.msg_len);