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, const size_t data_len)
60 const int *fds = cmsg_data;
61 const size_t nfds = data_len / sizeof(*fds);
66 for (i = 0; i < nfds; ++i) {
69 if (abbrev(tcp) && i >= max_strlen) {
80 print_scm_creds(struct tcb *tcp, const void *cmsg_data, const size_t data_len)
82 const struct ucred *uc = cmsg_data;
84 tprintf("{pid=%u, uid=%u, gid=%u}",
85 (unsigned) uc->pid, (unsigned) uc->uid, (unsigned) uc->gid);
89 print_scm_security(struct tcb *tcp, const void *cmsg_data,
90 const size_t data_len)
92 print_quoted_string(cmsg_data, data_len, 0);
96 print_cmsg_ip_pktinfo(struct tcb *tcp, const void *cmsg_data,
97 const size_t data_len)
99 const struct in_pktinfo *info = cmsg_data;
101 tprints("{ipi_ifindex=");
102 print_ifindex(info->ipi_ifindex);
103 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));
110 print_cmsg_uint(struct tcb *tcp, const void *cmsg_data, const size_t data_len)
112 const unsigned int *p = cmsg_data;
118 print_cmsg_uint8_t(struct tcb *tcp, const void *cmsg_data,
119 const size_t data_len)
121 const uint8_t *p = cmsg_data;
123 tprintf("[%#x]", *p);
127 print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data,
128 const size_t data_len)
130 const unsigned char *opts = cmsg_data;
134 for (i = 0; i < data_len; ++i) {
137 if (abbrev(tcp) && i >= max_strlen) {
141 tprintf("0x%02x", opts[i]);
154 struct sockaddr_in offender;
158 print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data,
159 const size_t data_len)
161 const struct sock_ee *const err = cmsg_data;
163 tprintf("{ee_errno=%u, ee_origin=%u, ee_type=%u, ee_code=%u"
164 ", ee_info=%u, ee_data=%u, offender=",
165 err->ee_errno, err->ee_origin, err->ee_type,
166 err->ee_code, err->ee_info, err->ee_data);
167 print_sockaddr(tcp, &err->offender, sizeof(err->offender));
172 print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data,
173 const size_t data_len)
176 data_len > sizeof(struct sockaddr_storage)
177 ? sizeof(struct sockaddr_storage) : data_len;
179 print_sockaddr(tcp, cmsg_data, addr_len);
182 typedef void (* const cmsg_printer)(struct tcb *, const void *, size_t);
184 static const struct {
185 const cmsg_printer printer;
186 const size_t min_len;
187 } cmsg_socket_printers[] = {
188 [SCM_RIGHTS] = { print_scm_rights, sizeof(int) },
189 [SCM_CREDENTIALS] = { print_scm_creds, sizeof(struct ucred) },
190 [SCM_SECURITY] = { print_scm_security, 1 }
191 }, cmsg_ip_printers[] = {
192 [IP_PKTINFO] = { print_cmsg_ip_pktinfo, sizeof(struct in_pktinfo) },
193 [IP_TTL] = { print_cmsg_uint, sizeof(unsigned int) },
194 [IP_TOS] = { print_cmsg_uint8_t, 1 },
195 [IP_RECVOPTS] = { print_cmsg_ip_opts, 1 },
196 [IP_RETOPTS] = { print_cmsg_ip_opts, 1 },
197 [IP_RECVERR] = { print_cmsg_ip_recverr, sizeof(struct sock_ee) },
198 [IP_ORIGDSTADDR] = { print_cmsg_ip_origdstaddr, sizeof(struct sockaddr_in) },
199 [IP_CHECKSUM] = { print_cmsg_uint, sizeof(unsigned int) },
200 [SCM_SECURITY] = { print_scm_security, 1 }
204 print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
205 const void *cmsg_data, const size_t data_len)
207 const unsigned int utype = cmsg_type;
208 switch (cmsg_level) {
210 printxval(scmvals, cmsg_type, "SCM_???");
211 if (utype < ARRAY_SIZE(cmsg_socket_printers)
212 && cmsg_socket_printers[utype].printer
213 && data_len >= cmsg_socket_printers[utype].min_len) {
214 tprints(", cmsg_data=");
215 cmsg_socket_printers[utype].printer(tcp, cmsg_data, data_len);
219 printxval(ip_cmsg_types, cmsg_type, "IP_???");
220 if (utype < ARRAY_SIZE(cmsg_ip_printers)
221 && cmsg_ip_printers[utype].printer
222 && data_len >= cmsg_ip_printers[utype].min_len) {
223 tprints(", cmsg_data=");
224 cmsg_ip_printers[utype].printer(tcp, cmsg_data, data_len);
228 tprintf("%#x", cmsg_type);
233 # define UIO_MAXIOV 1024
239 static int optmem_max;
242 if (read_int_from_file("/proc/sys/net/core/optmem_max",
243 &optmem_max) || optmem_max <= 0) {
244 optmem_max = sizeof(long long) * (2 * UIO_MAXIOV + 512);
246 optmem_max = (optmem_max + sizeof(long long) - 1)
247 & ~(sizeof(long long) - 1);
255 decode_msg_control(struct tcb *tcp, unsigned long addr,
256 const size_t in_control_len)
260 tprints(", msg_control=");
262 const size_t cmsg_size =
263 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
264 (current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) :
266 sizeof(struct cmsghdr);
269 in_control_len > get_optmem_max()
270 ? get_optmem_max() : in_control_len;
271 size_t buf_len = control_len;
272 char *buf = buf_len < cmsg_size ? NULL : malloc(buf_len);
273 if (!buf || umoven(tcp, addr, buf_len, buf) < 0) {
279 union_cmsghdr u = { .ptr = buf };
282 while (buf_len >= cmsg_size) {
283 const size_t cmsg_len =
284 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
285 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len :
288 const int cmsg_level =
289 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
290 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level :
293 const int cmsg_type =
294 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
295 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type :
301 tprintf("{cmsg_len=%lu, cmsg_level=", (unsigned long) cmsg_len);
302 printxval(socketlayers, cmsg_level, "SOL_???");
303 tprints(", cmsg_type=");
305 size_t len = cmsg_len > buf_len ? buf_len : cmsg_len;
307 print_cmsg_type_data(tcp, cmsg_level, cmsg_type,
308 (const void *) (u.ptr + cmsg_size),
309 len > cmsg_size ? len - cmsg_size: 0);
312 if (len < cmsg_size) {
313 buf_len -= cmsg_size;
316 len = (cmsg_len + current_wordsize - 1) &
317 (size_t) ~(current_wordsize - 1);
318 if (len >= buf_len) {
327 printaddr(addr + (control_len - buf_len));
328 } else if (control_len < in_control_len) {
336 print_msghdr(struct tcb *tcp, struct msghdr *msg, unsigned long data_size)
338 tprints("{msg_name=");
339 decode_sockaddr(tcp, (long)msg->msg_name, msg->msg_namelen);
340 tprintf(", msg_namelen=%d", msg->msg_namelen);
342 tprints(", msg_iov=");
343 tprint_iov_upto(tcp, (unsigned long) msg->msg_iovlen,
344 (unsigned long) msg->msg_iov, IOV_DECODE_STR, data_size);
345 tprintf(", msg_iovlen=%lu", (unsigned long) msg->msg_iovlen);
347 decode_msg_control(tcp, (unsigned long) msg->msg_control,
348 msg->msg_controllen);
349 tprintf(", msg_controllen=%lu", (unsigned long) msg->msg_controllen);
351 tprints(", msg_flags=");
352 printflags(msg_flags, msg->msg_flags, "MSG_???");
357 decode_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
361 if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg))
362 print_msghdr(tcp, &msg, data_size);
368 dumpiov_in_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
372 if (fetch_struct_msghdr(tcp, addr, &msg))
373 dumpiov_upto(tcp, msg.msg_iovlen, (long)msg.msg_iov, data_size);
377 decode_mmsghdr(struct tcb *tcp, long addr, bool use_msg_len)
380 int fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
383 tprints("{msg_hdr=");
384 print_msghdr(tcp, &mmsg.msg_hdr, use_msg_len ? mmsg.msg_len : -1UL);
385 tprintf(", msg_len=%u}", mmsg.msg_len);
394 decode_mmsgvec(struct tcb *tcp, unsigned long addr, unsigned int len,
400 unsigned int i, fetched;
403 for (i = 0; i < len; ++i, addr += fetched) {
406 fetched = decode_mmsghdr(tcp, addr, use_msg_len);
415 dumpiov_in_mmsghdr(struct tcb *tcp, long addr)
417 unsigned int len = tcp->u_rval;
418 unsigned int i, fetched;
421 for (i = 0; i < len; ++i, addr += fetched) {
422 fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
425 tprintf(" = %lu buffers in vector %u\n",
426 (unsigned long)mmsg.msg_hdr.msg_iovlen, i);
427 dumpiov_upto(tcp, mmsg.msg_hdr.msg_iovlen,
428 (long)mmsg.msg_hdr.msg_iov, mmsg.msg_len);