blob: c15542cba8693a75bf712cf127e1682910bf06fc [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
Dmitry V. Levin08c09602016-06-28 22:25:10 +000068 tprints(", cmsg_data=[");
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +000069 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
Dmitry V. Levin08c09602016-06-28 22:25:10 +000088 tprintf(", cmsg_data={pid=%u, uid=%u, gid=%u}",
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +000089 (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
Dmitry V. Levin08c09602016-06-28 22:25:10 +000099 tprints(", cmsg_data=");
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000100 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
Dmitry V. Levin08c09602016-06-28 22:25:10 +0000112 tprints(", cmsg_data={ipi_ifindex=");
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000113 print_ifindex(info->ipi_ifindex);
Dmitry V. Levindb0e6e12016-06-29 22:07:20 +0000114 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));
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000118}
119
120static void
121print_cmsg_ip_ttl(struct tcb *tcp, const void *cmsg_data,
122 const size_t data_len)
123{
124 const unsigned int *ttl = cmsg_data;
125
126 if (sizeof(*ttl) > data_len)
127 return;
128
Dmitry V. Levin17e624c2016-06-28 22:25:10 +0000129 tprintf(", cmsg_data=[%u]", *ttl);
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000130}
131
132static void
133print_cmsg_ip_tos(struct tcb *tcp, const void *cmsg_data,
134 const size_t data_len)
135{
136 const uint8_t *tos = cmsg_data;
137
138 if (sizeof(*tos) > data_len)
139 return;
140
Dmitry V. Levin17e624c2016-06-28 22:25:10 +0000141 tprintf(", cmsg_data=[%#x]", *tos);
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000142}
143
144static void
145print_cmsg_ip_checksum(struct tcb *tcp, const void *cmsg_data,
146 const size_t data_len)
147{
148 const uint32_t *csum = cmsg_data;
149
150 if (sizeof(*csum) > data_len)
151 return;
152
Dmitry V. Levin17e624c2016-06-28 22:25:10 +0000153 tprintf(", cmsg_data=[%u]", *csum);
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000154}
155
156static void
157print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data,
158 const size_t data_len)
159{
160 const unsigned char *opts = cmsg_data;
161 size_t i;
162
163 if (!data_len)
164 return;
165
Dmitry V. Levin2a897372016-06-28 22:25:10 +0000166 tprints(", cmsg_data=[");
167 for (i = 0; i < data_len; ++i) {
168 if (i)
169 tprints(", ");
170 tprintf("0x%02x", opts[i]);
171 }
172 tprints("]");
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000173}
174
175static void
176print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data,
177 const size_t data_len)
178{
179 const struct {
180 uint32_t ee_errno;
181 uint8_t ee_origin;
182 uint8_t ee_type;
183 uint8_t ee_code;
184 uint8_t ee_pad;
185 uint32_t ee_info;
186 uint32_t ee_data;
187 struct sockaddr_in offender;
188 } *err = cmsg_data;
189
190 if (sizeof(*err) > data_len)
191 return;
192
Dmitry V. Levin08c09602016-06-28 22:25:10 +0000193 tprintf(", cmsg_data={ee_errno=%u, ee_origin=%u, ee_type=%u, ee_code=%u"
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000194 ", 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));
198 tprints("}");
199}
200
201static void
202print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data,
203 const size_t data_len)
204{
205 if (sizeof(struct sockaddr_in) > data_len)
206 return;
207
Dmitry V. Levin08c09602016-06-28 22:25:10 +0000208 tprints(", cmsg_data=");
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000209 print_sockaddr(tcp, cmsg_data, data_len);
210}
211
212static void
213print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
214 const void *cmsg_data, const size_t data_len)
215{
216 switch (cmsg_level) {
217 case SOL_SOCKET:
218 printxval(scmvals, cmsg_type, "SCM_???");
219 switch (cmsg_type) {
220 case SCM_RIGHTS:
221 print_scm_rights(tcp, cmsg_data, data_len);
222 break;
223 case SCM_CREDENTIALS:
224 print_scm_creds(tcp, cmsg_data, data_len);
225 break;
226 case SCM_SECURITY:
227 print_scm_security(tcp, cmsg_data, data_len);
228 break;
229 }
230 break;
231 case SOL_IP:
232 printxval(ip_cmsg_types, cmsg_type, "IP_???");
233 switch (cmsg_type) {
234 case IP_PKTINFO:
235 print_cmsg_ip_pktinfo(tcp, cmsg_data, data_len);
236 break;
237 case IP_TTL:
238 print_cmsg_ip_ttl(tcp, cmsg_data, data_len);
239 break;
240 case IP_TOS:
241 print_cmsg_ip_tos(tcp, cmsg_data, data_len);
242 break;
243 case IP_RECVOPTS:
244 case IP_RETOPTS:
245 print_cmsg_ip_opts(tcp, cmsg_data, data_len);
246 break;
247 case IP_RECVERR:
248 print_cmsg_ip_recverr(tcp, cmsg_data, data_len);
249 break;
250 case IP_ORIGDSTADDR:
251 print_cmsg_ip_origdstaddr(tcp, cmsg_data, data_len);
252 break;
253 case IP_CHECKSUM:
254 print_cmsg_ip_checksum(tcp, cmsg_data, data_len);
255 break;
256 case SCM_SECURITY:
257 print_scm_security(tcp, cmsg_data, data_len);
258 break;
259 }
260 break;
261 default:
262 tprintf("%u", cmsg_type);
263 }
264}
265
266static void
267decode_msg_control(struct tcb *tcp, unsigned long addr, size_t len)
268{
269 const size_t cmsg_size =
270#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
271 (current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) :
272#endif
273 sizeof(struct cmsghdr);
274
275 if (!len)
276 return;
277 tprints(", msg_control=");
278
279 char *buf = len < cmsg_size ? NULL : malloc(len);
280 if (!buf || umoven(tcp, addr, len, buf) < 0) {
281 printaddr(addr);
282 free(buf);
283 return;
284 }
285
286 union_cmsghdr u = { .ptr = buf };
287
288 tprints("[");
289 while (len >= cmsg_size) {
290 size_t cmsg_len =
291#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
292 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len :
293#endif
294 u.cmsg->cmsg_len;
295 int cmsg_level =
296#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
297 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level :
298#endif
299 u.cmsg->cmsg_level;
300 int cmsg_type =
301#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
302 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type :
303#endif
304 u.cmsg->cmsg_type;
305
306 if (u.ptr != buf)
307 tprints(", ");
308 tprintf("{cmsg_len=%lu, cmsg_level=", (unsigned long) cmsg_len);
309 printxval(socketlayers, cmsg_level, "SOL_???");
310 tprints(", cmsg_type=");
311
312 if (cmsg_len > len)
313 cmsg_len = len;
314
315 print_cmsg_type_data(tcp, cmsg_level, cmsg_type,
316 (const void *) (u.ptr + cmsg_size),
317 cmsg_len > cmsg_size ? cmsg_len - cmsg_size: 0);
318 tprints("}");
319
320 if (cmsg_len < cmsg_size) {
321 len -= cmsg_size;
322 break;
323 }
324 cmsg_len = (cmsg_len + current_wordsize - 1) &
325 (size_t) ~(current_wordsize - 1);
326 if (cmsg_len >= len) {
327 len = 0;
328 break;
329 }
330 u.ptr += cmsg_len;
331 len -= cmsg_len;
332 }
333 if (len)
334 tprints(", ...");
335 tprints("]");
336 free(buf);
337}
338
339static void
340print_msghdr(struct tcb *tcp, struct msghdr *msg, unsigned long data_size)
341{
342 tprints("{msg_name=");
343 decode_sockaddr(tcp, (long)msg->msg_name, msg->msg_namelen);
344 tprintf(", msg_namelen=%d", msg->msg_namelen);
345
346 tprints(", msg_iov=");
347 tprint_iov_upto(tcp, (unsigned long) msg->msg_iovlen,
348 (unsigned long) msg->msg_iov, IOV_DECODE_STR, data_size);
349 tprintf(", msg_iovlen=%lu", (unsigned long) msg->msg_iovlen);
350
351 decode_msg_control(tcp, (unsigned long) msg->msg_control,
352 msg->msg_controllen);
353 tprintf(", msg_controllen=%lu", (unsigned long) msg->msg_controllen);
354
355 tprints(", msg_flags=");
356 printflags(msg_flags, msg->msg_flags, "MSG_???");
357 tprints("}");
358}
359
360void
361decode_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
362{
363 struct msghdr msg;
364
365 if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg))
366 print_msghdr(tcp, &msg, data_size);
367 else
368 printaddr(addr);
369}
370
371void
372dumpiov_in_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
373{
374 struct msghdr msg;
375
376 if (fetch_struct_msghdr(tcp, addr, &msg))
377 dumpiov_upto(tcp, msg.msg_iovlen, (long)msg.msg_iov, data_size);
378}
379
380static int
381decode_mmsghdr(struct tcb *tcp, long addr, bool use_msg_len)
382{
383 struct mmsghdr mmsg;
384 int fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
385
386 if (fetched) {
Dmitry V. Levina50ec342016-06-27 00:14:34 +0000387 tprints("{msg_hdr=");
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000388 print_msghdr(tcp, &mmsg.msg_hdr, use_msg_len ? mmsg.msg_len : -1UL);
Dmitry V. Levina50ec342016-06-27 00:14:34 +0000389 tprintf(", msg_len=%u}", mmsg.msg_len);
Dmitry V. Levin95cce4f2016-06-27 00:02:55 +0000390 } else {
391 printaddr(addr);
392 }
393
394 return fetched;
395}
396
397void
398decode_mmsgvec(struct tcb *tcp, unsigned long addr, unsigned int len,
399 bool use_msg_len)
400{
401 if (syserror(tcp)) {
402 printaddr(addr);
403 } else {
404 unsigned int i, fetched;
405
406 tprints("[");
407 for (i = 0; i < len; ++i, addr += fetched) {
408 if (i)
409 tprints(", ");
410 fetched = decode_mmsghdr(tcp, addr, use_msg_len);
411 if (!fetched)
412 break;
413 }
414 tprints("]");
415 }
416}
417
418void
419dumpiov_in_mmsghdr(struct tcb *tcp, long addr)
420{
421 unsigned int len = tcp->u_rval;
422 unsigned int i, fetched;
423 struct mmsghdr mmsg;
424
425 for (i = 0; i < len; ++i, addr += fetched) {
426 fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
427 if (!fetched)
428 break;
429 tprintf(" = %lu buffers in vector %u\n",
430 (unsigned long)mmsg.msg_hdr.msg_iovlen, i);
431 dumpiov_upto(tcp, mmsg.msg_hdr.msg_iovlen,
432 (long)mmsg.msg_hdr.msg_iov, mmsg.msg_len);
433 }
434}