blob: 931d3bd0e4b6f8dadb46e4507aa9f4e0d24efecc [file] [log] [blame]
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +00001/*
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
42struct cmsghdr32 {
43 uint32_t cmsg_len;
44 int cmsg_level;
45 int cmsg_type;
46};
47#endif
48
49typedef 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
57static void
58print_scm_rights(struct tcb *tcp, const void *cmsg_data,
59 const size_t data_len)
60{
61 const int *fds = cmsg_data;
62 const char *end = (const char *) cmsg_data + data_len;
63 bool seen = false;
64
65 if (sizeof(*fds) > data_len)
66 return;
67
68 tprints(", [");
69 while ((const char *) fds < end) {
70 if (seen)
71 tprints(", ");
72 else
73 seen = true;
74 printfd(tcp, *fds++);
75 }
76 tprints("]");
77}
78
79static void
80print_scm_creds(struct tcb *tcp, const void *cmsg_data,
81 const size_t data_len)
82{
83 const struct ucred *uc = cmsg_data;
84
85 if (sizeof(*uc) > data_len)
86 return;
87
88 tprintf(", {pid=%u, uid=%u, gid=%u}",
89 (unsigned) uc->pid, (unsigned) uc->uid, (unsigned) uc->gid);
90}
91
92static void
93print_scm_security(struct tcb *tcp, const void *cmsg_data,
94 const size_t data_len)
95{
96 if (!data_len)
97 return;
98
99 tprints(", ");
100 print_quoted_string(cmsg_data, data_len, 0);
101}
102
103static void
104print_cmsg_ip_pktinfo(struct tcb *tcp, const void *cmsg_data,
105 const size_t data_len)
106{
107 const struct in_pktinfo *info = cmsg_data;
108
109 if (sizeof(*info) > data_len)
110 return;
111
112 tprints(", {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));
116}
117
118static void
119print_cmsg_ip_ttl(struct tcb *tcp, const void *cmsg_data,
120 const size_t data_len)
121{
122 const unsigned int *ttl = cmsg_data;
123
124 if (sizeof(*ttl) > data_len)
125 return;
126
127 tprintf(", {ttl=%u}", *ttl);
128}
129
130static void
131print_cmsg_ip_tos(struct tcb *tcp, const void *cmsg_data,
132 const size_t data_len)
133{
134 const uint8_t *tos = cmsg_data;
135
136 if (sizeof(*tos) > data_len)
137 return;
138
139 tprintf(", {tos=%x}", *tos);
140}
141
142static void
143print_cmsg_ip_checksum(struct tcb *tcp, const void *cmsg_data,
144 const size_t data_len)
145{
146 const uint32_t *csum = cmsg_data;
147
148 if (sizeof(*csum) > data_len)
149 return;
150
151 tprintf(", {csum=%u}", *csum);
152}
153
154static void
155print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data,
156 const size_t data_len)
157{
158 const unsigned char *opts = cmsg_data;
159 size_t i;
160
161 if (!data_len)
162 return;
163
164 tprints(", {opts=0x");
165 for (i = 0; i < data_len; ++i)
166 tprintf("%02x", opts[i]);
167 tprints("}");
168}
169
170static void
171print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data,
172 const size_t data_len)
173{
174 const struct {
175 uint32_t ee_errno;
176 uint8_t ee_origin;
177 uint8_t ee_type;
178 uint8_t ee_code;
179 uint8_t ee_pad;
180 uint32_t ee_info;
181 uint32_t ee_data;
182 struct sockaddr_in offender;
183 } *err = cmsg_data;
184
185 if (sizeof(*err) > data_len)
186 return;
187
188 tprintf(", {ee_errno=%u, ee_origin=%u, ee_type=%u, ee_code=%u"
189 ", ee_info=%u, ee_data=%u, offender=",
190 err->ee_errno, err->ee_origin, err->ee_type,
191 err->ee_code, err->ee_info, err->ee_data);
192 print_sockaddr(tcp, &err->offender, sizeof(err->offender));
193 tprints("}");
194}
195
196static void
197print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data,
198 const size_t data_len)
199{
200 if (sizeof(struct sockaddr_in) > data_len)
201 return;
202
203 tprints(", ");
204 print_sockaddr(tcp, cmsg_data, data_len);
205}
206
207static void
208print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
209 const void *cmsg_data, const size_t data_len)
210{
211 switch (cmsg_level) {
212 case SOL_SOCKET:
213 printxval(scmvals, cmsg_type, "SCM_???");
214 switch (cmsg_type) {
215 case SCM_RIGHTS:
216 print_scm_rights(tcp, cmsg_data, data_len);
217 break;
218 case SCM_CREDENTIALS:
219 print_scm_creds(tcp, cmsg_data, data_len);
220 break;
221 case SCM_SECURITY:
222 print_scm_security(tcp, cmsg_data, data_len);
223 break;
224 }
225 break;
226 case SOL_IP:
227 printxval(ip_cmsg_types, cmsg_type, "IP_???");
228 switch (cmsg_type) {
229 case IP_PKTINFO:
230 print_cmsg_ip_pktinfo(tcp, cmsg_data, data_len);
231 break;
232 case IP_TTL:
233 print_cmsg_ip_ttl(tcp, cmsg_data, data_len);
234 break;
235 case IP_TOS:
236 print_cmsg_ip_tos(tcp, cmsg_data, data_len);
237 break;
238 case IP_RECVOPTS:
239 case IP_RETOPTS:
240 print_cmsg_ip_opts(tcp, cmsg_data, data_len);
241 break;
242 case IP_RECVERR:
243 print_cmsg_ip_recverr(tcp, cmsg_data, data_len);
244 break;
245 case IP_ORIGDSTADDR:
246 print_cmsg_ip_origdstaddr(tcp, cmsg_data, data_len);
247 break;
248 case IP_CHECKSUM:
249 print_cmsg_ip_checksum(tcp, cmsg_data, data_len);
250 break;
251 case SCM_SECURITY:
252 print_scm_security(tcp, cmsg_data, data_len);
253 break;
254 }
255 break;
256 default:
257 tprintf("%u", cmsg_type);
258 }
259}
260
261static void
262decode_msg_control(struct tcb *tcp, unsigned long addr, size_t len)
263{
264 const size_t cmsg_size =
265#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
266 (current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) :
267#endif
268 sizeof(struct cmsghdr);
269
270 if (!len)
271 return;
272 tprints(", msg_control=");
273
274 char *buf = len < cmsg_size ? NULL : malloc(len);
275 if (!buf || umoven(tcp, addr, len, buf) < 0) {
276 printaddr(addr);
277 free(buf);
278 return;
279 }
280
281 union_cmsghdr u = { .ptr = buf };
282
283 tprints("[");
284 while (len >= cmsg_size) {
285 size_t cmsg_len =
286#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
287 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len :
288#endif
289 u.cmsg->cmsg_len;
290 int cmsg_level =
291#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
292 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level :
293#endif
294 u.cmsg->cmsg_level;
295 int cmsg_type =
296#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
297 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type :
298#endif
299 u.cmsg->cmsg_type;
300
301 if (u.ptr != buf)
302 tprints(", ");
303 tprintf("{cmsg_len=%lu, cmsg_level=", (unsigned long) cmsg_len);
304 printxval(socketlayers, cmsg_level, "SOL_???");
305 tprints(", cmsg_type=");
306
307 if (cmsg_len > len)
308 cmsg_len = len;
309
310 print_cmsg_type_data(tcp, cmsg_level, cmsg_type,
311 (const void *) (u.ptr + cmsg_size),
312 cmsg_len > cmsg_size ? cmsg_len - cmsg_size: 0);
313 tprints("}");
314
315 if (cmsg_len < cmsg_size) {
316 len -= cmsg_size;
317 break;
318 }
319 cmsg_len = (cmsg_len + current_wordsize - 1) &
320 (size_t) ~(current_wordsize - 1);
321 if (cmsg_len >= len) {
322 len = 0;
323 break;
324 }
325 u.ptr += cmsg_len;
326 len -= cmsg_len;
327 }
328 if (len)
329 tprints(", ...");
330 tprints("]");
331 free(buf);
332}
333
334static void
335print_msghdr(struct tcb *tcp, struct msghdr *msg, unsigned long data_size)
336{
337 tprints("{msg_name=");
338 decode_sockaddr(tcp, (long)msg->msg_name, msg->msg_namelen);
339 tprintf(", msg_namelen=%d", msg->msg_namelen);
340
341 tprints(", msg_iov=");
342 tprint_iov_upto(tcp, (unsigned long) msg->msg_iovlen,
343 (unsigned long) msg->msg_iov, IOV_DECODE_STR, data_size);
344 tprintf(", msg_iovlen=%lu", (unsigned long) msg->msg_iovlen);
345
346 decode_msg_control(tcp, (unsigned long) msg->msg_control,
347 msg->msg_controllen);
348 tprintf(", msg_controllen=%lu", (unsigned long) msg->msg_controllen);
349
350 tprints(", msg_flags=");
351 printflags(msg_flags, msg->msg_flags, "MSG_???");
352 tprints("}");
353}
354
355void
356decode_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
357{
358 struct msghdr msg;
359
360 if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg))
361 print_msghdr(tcp, &msg, data_size);
362 else
363 printaddr(addr);
364}
365
366void
367dumpiov_in_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
368{
369 struct msghdr msg;
370
371 if (fetch_struct_msghdr(tcp, addr, &msg))
372 dumpiov_upto(tcp, msg.msg_iovlen, (long)msg.msg_iov, data_size);
373}
374
375static int
376decode_mmsghdr(struct tcb *tcp, long addr, bool use_msg_len)
377{
378 struct mmsghdr mmsg;
379 int fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
380
381 if (fetched) {
Dmitry V. Levina50ec342016-06-27 00:14:34 +0000382 tprints("{msg_hdr=");
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000383 print_msghdr(tcp, &mmsg.msg_hdr, use_msg_len ? mmsg.msg_len : -1UL);
Dmitry V. Levina50ec342016-06-27 00:14:34 +0000384 tprintf(", msg_len=%u}", mmsg.msg_len);
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000385 } else {
386 printaddr(addr);
387 }
388
389 return fetched;
390}
391
392void
393decode_mmsgvec(struct tcb *tcp, unsigned long addr, unsigned int len,
394 bool use_msg_len)
395{
396 if (syserror(tcp)) {
397 printaddr(addr);
398 } else {
399 unsigned int i, fetched;
400
401 tprints("[");
402 for (i = 0; i < len; ++i, addr += fetched) {
403 if (i)
404 tprints(", ");
405 fetched = decode_mmsghdr(tcp, addr, use_msg_len);
406 if (!fetched)
407 break;
408 }
409 tprints("]");
410 }
411}
412
413void
414dumpiov_in_mmsghdr(struct tcb *tcp, long addr)
415{
416 unsigned int len = tcp->u_rval;
417 unsigned int i, fetched;
418 struct mmsghdr mmsg;
419
420 for (i = 0; i < len; ++i, addr += fetched) {
421 fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
422 if (!fetched)
423 break;
424 tprintf(" = %lu buffers in vector %u\n",
425 (unsigned long)mmsg.msg_hdr.msg_iovlen, i);
426 dumpiov_upto(tcp, mmsg.msg_hdr.msg_iovlen,
427 (long)mmsg.msg_hdr.msg_iov, mmsg.msg_len);
428 }
429}