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