Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 1 | /* |
| 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 |
| 42 | struct cmsghdr32 { |
| 43 | uint32_t cmsg_len; |
| 44 | int cmsg_level; |
| 45 | int cmsg_type; |
| 46 | }; |
| 47 | #endif |
| 48 | |
| 49 | typedef 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 | |
| 57 | static void |
Dmitry V. Levin | b7937bd | 2016-06-30 22:39:02 +0000 | [diff] [blame] | 58 | print_scm_rights(struct tcb *tcp, const void *cmsg_data, const size_t data_len) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 59 | { |
| 60 | const int *fds = cmsg_data; |
Dmitry V. Levin | 029d979 | 2016-06-30 22:20:56 +0000 | [diff] [blame] | 61 | const size_t nfds = data_len / sizeof(*fds); |
| 62 | size_t i; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 63 | |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 64 | tprints("["); |
Dmitry V. Levin | 029d979 | 2016-06-30 22:20:56 +0000 | [diff] [blame] | 65 | |
| 66 | for (i = 0; i < nfds; ++i) { |
| 67 | if (i) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 68 | tprints(", "); |
Dmitry V. Levin | d35d5ec | 2016-07-02 21:14:26 +0000 | [diff] [blame] | 69 | if (abbrev(tcp) && i >= max_strlen) { |
| 70 | tprints("..."); |
| 71 | break; |
| 72 | } |
Dmitry V. Levin | 029d979 | 2016-06-30 22:20:56 +0000 | [diff] [blame] | 73 | printfd(tcp, fds[i]); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 74 | } |
Dmitry V. Levin | 029d979 | 2016-06-30 22:20:56 +0000 | [diff] [blame] | 75 | |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 76 | tprints("]"); |
| 77 | } |
| 78 | |
| 79 | static void |
Dmitry V. Levin | b7937bd | 2016-06-30 22:39:02 +0000 | [diff] [blame] | 80 | print_scm_creds(struct tcb *tcp, const void *cmsg_data, const size_t data_len) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 81 | { |
| 82 | const struct ucred *uc = cmsg_data; |
| 83 | |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 84 | tprintf("{pid=%u, uid=%u, gid=%u}", |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 85 | (unsigned) uc->pid, (unsigned) uc->uid, (unsigned) uc->gid); |
| 86 | } |
| 87 | |
| 88 | static void |
| 89 | print_scm_security(struct tcb *tcp, const void *cmsg_data, |
| 90 | const size_t data_len) |
| 91 | { |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 92 | print_quoted_string(cmsg_data, data_len, 0); |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | print_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. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 101 | tprints("{ipi_ifindex="); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 102 | print_ifindex(info->ipi_ifindex); |
Dmitry V. Levin | db0e6e1 | 2016-06-29 22:07:20 +0000 | [diff] [blame] | 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)); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static void |
Dmitry V. Levin | b7937bd | 2016-06-30 22:39:02 +0000 | [diff] [blame] | 110 | print_cmsg_uint(struct tcb *tcp, const void *cmsg_data, const size_t data_len) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 111 | { |
Dmitry V. Levin | b7937bd | 2016-06-30 22:39:02 +0000 | [diff] [blame] | 112 | const unsigned int *p = cmsg_data; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 113 | |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 114 | tprintf("[%u]", *p); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 118 | print_cmsg_uint8_t(struct tcb *tcp, const void *cmsg_data, |
| 119 | const size_t data_len) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 120 | { |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 121 | const uint8_t *p = cmsg_data; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 122 | |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 123 | tprintf("[%#x]", *p); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static void |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 127 | print_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. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 133 | tprints("["); |
Dmitry V. Levin | 2a89737 | 2016-06-28 22:25:10 +0000 | [diff] [blame] | 134 | for (i = 0; i < data_len; ++i) { |
| 135 | if (i) |
| 136 | tprints(", "); |
Dmitry V. Levin | 6713582 | 2016-07-02 21:14:48 +0000 | [diff] [blame] | 137 | if (abbrev(tcp) && i >= max_strlen) { |
| 138 | tprints("..."); |
| 139 | break; |
| 140 | } |
Dmitry V. Levin | 2a89737 | 2016-06-28 22:25:10 +0000 | [diff] [blame] | 141 | tprintf("0x%02x", opts[i]); |
| 142 | } |
| 143 | tprints("]"); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 146 | struct sock_ee { |
| 147 | uint32_t ee_errno; |
| 148 | uint8_t ee_origin; |
| 149 | uint8_t ee_type; |
| 150 | uint8_t ee_code; |
| 151 | uint8_t ee_pad; |
| 152 | uint32_t ee_info; |
| 153 | uint32_t ee_data; |
| 154 | struct sockaddr_in offender; |
| 155 | }; |
| 156 | |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 157 | static void |
| 158 | print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data, |
| 159 | const size_t data_len) |
| 160 | { |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 161 | const struct sock_ee *const err = cmsg_data; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 162 | |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 163 | tprintf("{ee_errno=%u, ee_origin=%u, ee_type=%u, ee_code=%u" |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 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)); |
| 168 | tprints("}"); |
| 169 | } |
| 170 | |
| 171 | static void |
| 172 | print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data, |
| 173 | const size_t data_len) |
| 174 | { |
Dmitry V. Levin | dfeea69 | 2016-06-30 22:26:35 +0000 | [diff] [blame] | 175 | const int addr_len = |
| 176 | data_len > sizeof(struct sockaddr_storage) |
| 177 | ? sizeof(struct sockaddr_storage) : data_len; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 178 | |
Dmitry V. Levin | dfeea69 | 2016-06-30 22:26:35 +0000 | [diff] [blame] | 179 | print_sockaddr(tcp, cmsg_data, addr_len); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 182 | typedef void (* const cmsg_printer)(struct tcb *, const void *, size_t); |
| 183 | |
| 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 } |
| 201 | }; |
| 202 | |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 203 | static void |
| 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) |
| 206 | { |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 207 | const unsigned int utype = cmsg_type; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 208 | switch (cmsg_level) { |
| 209 | case SOL_SOCKET: |
| 210 | printxval(scmvals, cmsg_type, "SCM_???"); |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 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); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 216 | } |
| 217 | break; |
| 218 | case SOL_IP: |
| 219 | printxval(ip_cmsg_types, cmsg_type, "IP_???"); |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 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); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 225 | } |
| 226 | break; |
| 227 | default: |
Dmitry V. Levin | 6c30aec | 2016-06-30 22:14:51 +0000 | [diff] [blame] | 228 | tprintf("%#x", cmsg_type); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Dmitry V. Levin | bf2698a | 2016-07-03 22:15:45 +0000 | [diff] [blame] | 232 | #ifndef UIO_MAXIOV |
| 233 | # define UIO_MAXIOV 1024 |
| 234 | #endif |
| 235 | |
| 236 | static unsigned int |
| 237 | get_optmem_max(void) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 238 | { |
Dmitry V. Levin | bf2698a | 2016-07-03 22:15:45 +0000 | [diff] [blame] | 239 | static int optmem_max; |
| 240 | |
| 241 | if (!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); |
| 245 | } else { |
| 246 | optmem_max = (optmem_max + sizeof(long long) - 1) |
| 247 | & ~(sizeof(long long) - 1); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return optmem_max; |
| 252 | } |
| 253 | |
| 254 | static void |
| 255 | decode_msg_control(struct tcb *tcp, unsigned long addr, |
| 256 | const size_t in_control_len) |
| 257 | { |
| 258 | if (!in_control_len) |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 259 | return; |
| 260 | tprints(", msg_control="); |
| 261 | |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 262 | const size_t cmsg_size = |
| 263 | #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 |
| 264 | (current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) : |
| 265 | #endif |
| 266 | sizeof(struct cmsghdr); |
| 267 | |
Dmitry V. Levin | bf2698a | 2016-07-03 22:15:45 +0000 | [diff] [blame] | 268 | size_t control_len = |
| 269 | in_control_len > get_optmem_max() |
| 270 | ? get_optmem_max() : in_control_len; |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 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) { |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 274 | printaddr(addr); |
| 275 | free(buf); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | union_cmsghdr u = { .ptr = buf }; |
| 280 | |
| 281 | tprints("["); |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 282 | while (buf_len >= cmsg_size) { |
| 283 | const size_t cmsg_len = |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 284 | #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 |
| 285 | (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len : |
| 286 | #endif |
| 287 | u.cmsg->cmsg_len; |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 288 | const int cmsg_level = |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 289 | #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 |
| 290 | (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level : |
| 291 | #endif |
| 292 | u.cmsg->cmsg_level; |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 293 | const int cmsg_type = |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 294 | #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 |
| 295 | (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type : |
| 296 | #endif |
| 297 | u.cmsg->cmsg_type; |
| 298 | |
| 299 | if (u.ptr != buf) |
| 300 | tprints(", "); |
| 301 | tprintf("{cmsg_len=%lu, cmsg_level=", (unsigned long) cmsg_len); |
| 302 | printxval(socketlayers, cmsg_level, "SOL_???"); |
| 303 | tprints(", cmsg_type="); |
| 304 | |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 305 | size_t len = cmsg_len > buf_len ? buf_len : cmsg_len; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 306 | |
| 307 | print_cmsg_type_data(tcp, cmsg_level, cmsg_type, |
| 308 | (const void *) (u.ptr + cmsg_size), |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 309 | len > cmsg_size ? len - cmsg_size: 0); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 310 | tprints("}"); |
| 311 | |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 312 | if (len < cmsg_size) { |
| 313 | buf_len -= cmsg_size; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 314 | break; |
| 315 | } |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 316 | len = (cmsg_len + current_wordsize - 1) & |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 317 | (size_t) ~(current_wordsize - 1); |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 318 | if (len >= buf_len) { |
| 319 | buf_len = 0; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 320 | break; |
| 321 | } |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 322 | u.ptr += len; |
| 323 | buf_len -= len; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 324 | } |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 325 | if (buf_len) { |
| 326 | tprints(", "); |
| 327 | printaddr(addr + (control_len - buf_len)); |
Dmitry V. Levin | bf2698a | 2016-07-03 22:15:45 +0000 | [diff] [blame] | 328 | } else if (control_len < in_control_len) { |
| 329 | tprints(", ..."); |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 330 | } |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 331 | tprints("]"); |
| 332 | free(buf); |
| 333 | } |
| 334 | |
Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 335 | void |
| 336 | print_struct_msghdr(struct tcb *tcp, const struct msghdr *msg, |
| 337 | const int *const p_user_msg_namelen, |
| 338 | const unsigned long data_size) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 339 | { |
Dmitry V. Levin | d8f77cd | 2016-07-13 21:56:16 +0000 | [diff] [blame] | 340 | const int msg_namelen = |
| 341 | p_user_msg_namelen && (int) msg->msg_namelen > *p_user_msg_namelen |
| 342 | ? *p_user_msg_namelen : (int) msg->msg_namelen; |
Fabien Siron | 2850f74 | 2016-07-06 15:49:22 +0000 | [diff] [blame] | 343 | |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 344 | tprints("{msg_name="); |
Dmitry V. Levin | d8f77cd | 2016-07-13 21:56:16 +0000 | [diff] [blame] | 345 | const int family = |
| 346 | decode_sockaddr(tcp, (long) msg->msg_name, msg_namelen); |
| 347 | const enum iov_decode decode = |
| 348 | (family == AF_NETLINK) ? IOV_DECODE_NETLINK : IOV_DECODE_STR; |
| 349 | |
| 350 | tprints(", msg_namelen="); |
| 351 | if (p_user_msg_namelen && *p_user_msg_namelen != (int) msg->msg_namelen) |
| 352 | tprintf("%d->", *p_user_msg_namelen); |
| 353 | tprintf("%d", msg->msg_namelen); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 354 | |
| 355 | tprints(", msg_iov="); |
Fabien Siron | 2850f74 | 2016-07-06 15:49:22 +0000 | [diff] [blame] | 356 | |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 357 | tprint_iov_upto(tcp, (unsigned long) msg->msg_iovlen, |
Fabien Siron | 2850f74 | 2016-07-06 15:49:22 +0000 | [diff] [blame] | 358 | (unsigned long) msg->msg_iov, decode, data_size); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 359 | tprintf(", msg_iovlen=%lu", (unsigned long) msg->msg_iovlen); |
| 360 | |
| 361 | decode_msg_control(tcp, (unsigned long) msg->msg_control, |
| 362 | msg->msg_controllen); |
| 363 | tprintf(", msg_controllen=%lu", (unsigned long) msg->msg_controllen); |
| 364 | |
| 365 | tprints(", msg_flags="); |
| 366 | printflags(msg_flags, msg->msg_flags, "MSG_???"); |
| 367 | tprints("}"); |
| 368 | } |
| 369 | |
Dmitry V. Levin | dc84fa3 | 2016-07-14 22:26:28 +0000 | [diff] [blame] | 370 | static bool |
Dmitry V. Levin | d8f77cd | 2016-07-13 21:56:16 +0000 | [diff] [blame] | 371 | fetch_msghdr_namelen(struct tcb *tcp, const long addr, int *const p_msg_namelen) |
| 372 | { |
| 373 | struct msghdr msg; |
| 374 | |
| 375 | if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg)) { |
| 376 | *p_msg_namelen = msg.msg_namelen; |
| 377 | return true; |
| 378 | } else { |
| 379 | return false; |
| 380 | } |
| 381 | } |
| 382 | |
Dmitry V. Levin | dc84fa3 | 2016-07-14 22:26:28 +0000 | [diff] [blame] | 383 | static void |
Dmitry V. Levin | d8f77cd | 2016-07-13 21:56:16 +0000 | [diff] [blame] | 384 | decode_msghdr(struct tcb *tcp, const int *const p_user_msg_namelen, |
| 385 | const long addr, const unsigned long data_size) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 386 | { |
| 387 | struct msghdr msg; |
| 388 | |
| 389 | if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg)) |
Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 390 | print_struct_msghdr(tcp, &msg, p_user_msg_namelen, data_size); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 391 | else |
| 392 | printaddr(addr); |
| 393 | } |
| 394 | |
| 395 | void |
| 396 | dumpiov_in_msghdr(struct tcb *tcp, long addr, unsigned long data_size) |
| 397 | { |
| 398 | struct msghdr msg; |
| 399 | |
| 400 | if (fetch_struct_msghdr(tcp, addr, &msg)) |
| 401 | dumpiov_upto(tcp, msg.msg_iovlen, (long)msg.msg_iov, data_size); |
| 402 | } |
Dmitry V. Levin | dc84fa3 | 2016-07-14 22:26:28 +0000 | [diff] [blame] | 403 | |
| 404 | SYS_FUNC(sendmsg) |
| 405 | { |
| 406 | printfd(tcp, tcp->u_arg[0]); |
| 407 | tprints(", "); |
| 408 | decode_msghdr(tcp, 0, tcp->u_arg[1], (unsigned long) -1L); |
| 409 | /* flags */ |
| 410 | tprints(", "); |
| 411 | printflags(msg_flags, tcp->u_arg[2], "MSG_???"); |
| 412 | |
| 413 | return RVAL_DECODED; |
| 414 | } |
| 415 | |
| 416 | SYS_FUNC(recvmsg) |
| 417 | { |
| 418 | int msg_namelen; |
| 419 | |
| 420 | if (entering(tcp)) { |
| 421 | printfd(tcp, tcp->u_arg[0]); |
| 422 | tprints(", "); |
| 423 | if (fetch_msghdr_namelen(tcp, tcp->u_arg[1], &msg_namelen)) { |
Dmitry V. Levin | b759d27 | 2016-07-15 16:08:19 +0000 | [diff] [blame^] | 424 | set_tcb_priv_ulong(tcp, msg_namelen); |
Dmitry V. Levin | dc84fa3 | 2016-07-14 22:26:28 +0000 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | printaddr(tcp->u_arg[1]); |
| 428 | } else { |
Dmitry V. Levin | b759d27 | 2016-07-15 16:08:19 +0000 | [diff] [blame^] | 429 | msg_namelen = get_tcb_priv_ulong(tcp); |
Dmitry V. Levin | dc84fa3 | 2016-07-14 22:26:28 +0000 | [diff] [blame] | 430 | |
| 431 | if (syserror(tcp)) |
| 432 | tprintf("{msg_namelen=%d}", msg_namelen); |
| 433 | else |
| 434 | decode_msghdr(tcp, &msg_namelen, tcp->u_arg[1], |
| 435 | tcp->u_rval); |
| 436 | } |
| 437 | |
| 438 | /* flags */ |
| 439 | tprints(", "); |
| 440 | printflags(msg_flags, tcp->u_arg[2], "MSG_???"); |
| 441 | |
| 442 | return RVAL_DECODED; |
| 443 | } |